tda8083.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497
  1. /*
  2. Driver for Philips TDA8083 based QPSK Demodulator
  3. Copyright (C) 2001 Convergence Integrated Media GmbH
  4. written by Ralph Metzler <ralph@convergence.de>
  5. adoption to the new DVB frontend API and diagnostic ioctl's
  6. by Holger Waechtler <holger@convergence.de>
  7. This program is free software; you can redistribute it and/or modify
  8. it under the terms of the GNU General Public License as published by
  9. the Free Software Foundation; either version 2 of the License, or
  10. (at your option) any later version.
  11. This program is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. GNU General Public License for more details.
  15. You should have received a copy of the GNU General Public License
  16. along with this program; if not, write to the Free Software
  17. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  18. */
  19. #include <linux/init.h>
  20. #include <linux/kernel.h>
  21. #include <linux/module.h>
  22. #include <linux/string.h>
  23. #include <linux/slab.h>
  24. #include <linux/jiffies.h>
  25. #include "dvb_frontend.h"
  26. #include "tda8083.h"
  27. struct tda8083_state {
  28. struct i2c_adapter* i2c;
  29. /* configuration settings */
  30. const struct tda8083_config* config;
  31. struct dvb_frontend frontend;
  32. };
  33. static int debug;
  34. #define dprintk(args...) \
  35. do { \
  36. if (debug) printk(KERN_DEBUG "tda8083: " args); \
  37. } while (0)
  38. static u8 tda8083_init_tab [] = {
  39. 0x04, 0x00, 0x4a, 0x79, 0x04, 0x00, 0xff, 0xea,
  40. 0x48, 0x42, 0x79, 0x60, 0x70, 0x52, 0x9a, 0x10,
  41. 0x0e, 0x10, 0xf2, 0xa7, 0x93, 0x0b, 0x05, 0xc8,
  42. 0x9d, 0x00, 0x42, 0x80, 0x00, 0x60, 0x40, 0x00,
  43. 0x00, 0x75, 0x00, 0xe0, 0x00, 0x00, 0x00, 0x00,
  44. 0x00, 0x00, 0x00, 0x00
  45. };
  46. static int tda8083_writereg (struct tda8083_state* state, u8 reg, u8 data)
  47. {
  48. int ret;
  49. u8 buf [] = { reg, data };
  50. struct i2c_msg msg = { .addr = state->config->demod_address, .flags = 0, .buf = buf, .len = 2 };
  51. ret = i2c_transfer(state->i2c, &msg, 1);
  52. if (ret != 1)
  53. dprintk ("%s: writereg error (reg %02x, ret == %i)\n",
  54. __func__, reg, ret);
  55. return (ret != 1) ? -1 : 0;
  56. }
  57. static int tda8083_readregs (struct tda8083_state* state, u8 reg1, u8 *b, u8 len)
  58. {
  59. int ret;
  60. struct i2c_msg msg [] = { { .addr = state->config->demod_address, .flags = 0, .buf = &reg1, .len = 1 },
  61. { .addr = state->config->demod_address, .flags = I2C_M_RD, .buf = b, .len = len } };
  62. ret = i2c_transfer(state->i2c, msg, 2);
  63. if (ret != 2)
  64. dprintk ("%s: readreg error (reg %02x, ret == %i)\n",
  65. __func__, reg1, ret);
  66. return ret == 2 ? 0 : -1;
  67. }
  68. static inline u8 tda8083_readreg (struct tda8083_state* state, u8 reg)
  69. {
  70. u8 val;
  71. tda8083_readregs (state, reg, &val, 1);
  72. return val;
  73. }
  74. static int tda8083_set_inversion(struct tda8083_state *state,
  75. enum fe_spectral_inversion inversion)
  76. {
  77. /* XXX FIXME: implement other modes than FEC_AUTO */
  78. if (inversion == INVERSION_AUTO)
  79. return 0;
  80. return -EINVAL;
  81. }
  82. static int tda8083_set_fec(struct tda8083_state *state, enum fe_code_rate fec)
  83. {
  84. if (fec == FEC_AUTO)
  85. return tda8083_writereg (state, 0x07, 0xff);
  86. if (fec >= FEC_1_2 && fec <= FEC_8_9)
  87. return tda8083_writereg (state, 0x07, 1 << (FEC_8_9 - fec));
  88. return -EINVAL;
  89. }
  90. static enum fe_code_rate tda8083_get_fec(struct tda8083_state *state)
  91. {
  92. u8 index;
  93. static enum fe_code_rate fec_tab[] = {
  94. FEC_8_9, FEC_1_2, FEC_2_3, FEC_3_4,
  95. FEC_4_5, FEC_5_6, FEC_6_7, FEC_7_8
  96. };
  97. index = tda8083_readreg(state, 0x0e) & 0x07;
  98. return fec_tab [index];
  99. }
  100. static int tda8083_set_symbolrate (struct tda8083_state* state, u32 srate)
  101. {
  102. u32 ratio;
  103. u32 tmp;
  104. u8 filter;
  105. if (srate > 32000000)
  106. srate = 32000000;
  107. if (srate < 500000)
  108. srate = 500000;
  109. filter = 0;
  110. if (srate < 24000000)
  111. filter = 2;
  112. if (srate < 16000000)
  113. filter = 3;
  114. tmp = 31250 << 16;
  115. ratio = tmp / srate;
  116. tmp = (tmp % srate) << 8;
  117. ratio = (ratio << 8) + tmp / srate;
  118. tmp = (tmp % srate) << 8;
  119. ratio = (ratio << 8) + tmp / srate;
  120. dprintk("tda8083: ratio == %08x\n", (unsigned int) ratio);
  121. tda8083_writereg (state, 0x05, filter);
  122. tda8083_writereg (state, 0x02, (ratio >> 16) & 0xff);
  123. tda8083_writereg (state, 0x03, (ratio >> 8) & 0xff);
  124. tda8083_writereg (state, 0x04, (ratio ) & 0xff);
  125. tda8083_writereg (state, 0x00, 0x3c);
  126. tda8083_writereg (state, 0x00, 0x04);
  127. return 1;
  128. }
  129. static void tda8083_wait_diseqc_fifo (struct tda8083_state* state, int timeout)
  130. {
  131. unsigned long start = jiffies;
  132. while (jiffies - start < timeout &&
  133. !(tda8083_readreg(state, 0x02) & 0x80))
  134. {
  135. msleep(50);
  136. }
  137. }
  138. static int tda8083_set_tone(struct tda8083_state *state,
  139. enum fe_sec_tone_mode tone)
  140. {
  141. tda8083_writereg (state, 0x26, 0xf1);
  142. switch (tone) {
  143. case SEC_TONE_OFF:
  144. return tda8083_writereg (state, 0x29, 0x00);
  145. case SEC_TONE_ON:
  146. return tda8083_writereg (state, 0x29, 0x80);
  147. default:
  148. return -EINVAL;
  149. }
  150. }
  151. static int tda8083_set_voltage(struct tda8083_state *state,
  152. enum fe_sec_voltage voltage)
  153. {
  154. switch (voltage) {
  155. case SEC_VOLTAGE_13:
  156. return tda8083_writereg (state, 0x20, 0x00);
  157. case SEC_VOLTAGE_18:
  158. return tda8083_writereg (state, 0x20, 0x11);
  159. default:
  160. return -EINVAL;
  161. }
  162. }
  163. static int tda8083_send_diseqc_burst(struct tda8083_state *state,
  164. enum fe_sec_mini_cmd burst)
  165. {
  166. switch (burst) {
  167. case SEC_MINI_A:
  168. tda8083_writereg (state, 0x29, (5 << 2)); /* send burst A */
  169. break;
  170. case SEC_MINI_B:
  171. tda8083_writereg (state, 0x29, (7 << 2)); /* send B */
  172. break;
  173. default:
  174. return -EINVAL;
  175. }
  176. tda8083_wait_diseqc_fifo (state, 100);
  177. return 0;
  178. }
  179. static int tda8083_send_diseqc_msg(struct dvb_frontend *fe,
  180. struct dvb_diseqc_master_cmd *m)
  181. {
  182. struct tda8083_state* state = fe->demodulator_priv;
  183. int i;
  184. tda8083_writereg (state, 0x29, (m->msg_len - 3) | (1 << 2)); /* enable */
  185. for (i=0; i<m->msg_len; i++)
  186. tda8083_writereg (state, 0x23 + i, m->msg[i]);
  187. tda8083_writereg (state, 0x29, (m->msg_len - 3) | (3 << 2)); /* send!! */
  188. tda8083_wait_diseqc_fifo (state, 100);
  189. return 0;
  190. }
  191. static int tda8083_read_status(struct dvb_frontend *fe,
  192. enum fe_status *status)
  193. {
  194. struct tda8083_state* state = fe->demodulator_priv;
  195. u8 signal = ~tda8083_readreg (state, 0x01);
  196. u8 sync = tda8083_readreg (state, 0x02);
  197. *status = 0;
  198. if (signal > 10)
  199. *status |= FE_HAS_SIGNAL;
  200. if (sync & 0x01)
  201. *status |= FE_HAS_CARRIER;
  202. if (sync & 0x02)
  203. *status |= FE_HAS_VITERBI;
  204. if (sync & 0x10)
  205. *status |= FE_HAS_SYNC;
  206. if (sync & 0x20) /* frontend can not lock */
  207. *status |= FE_TIMEDOUT;
  208. if ((sync & 0x1f) == 0x1f)
  209. *status |= FE_HAS_LOCK;
  210. return 0;
  211. }
  212. static int tda8083_read_ber(struct dvb_frontend* fe, u32* ber)
  213. {
  214. struct tda8083_state* state = fe->demodulator_priv;
  215. int ret;
  216. u8 buf[3];
  217. if ((ret = tda8083_readregs(state, 0x0b, buf, sizeof(buf))))
  218. return ret;
  219. *ber = ((buf[0] & 0x1f) << 16) | (buf[1] << 8) | buf[2];
  220. return 0;
  221. }
  222. static int tda8083_read_signal_strength(struct dvb_frontend* fe, u16* strength)
  223. {
  224. struct tda8083_state* state = fe->demodulator_priv;
  225. u8 signal = ~tda8083_readreg (state, 0x01);
  226. *strength = (signal << 8) | signal;
  227. return 0;
  228. }
  229. static int tda8083_read_snr(struct dvb_frontend* fe, u16* snr)
  230. {
  231. struct tda8083_state* state = fe->demodulator_priv;
  232. u8 _snr = tda8083_readreg (state, 0x08);
  233. *snr = (_snr << 8) | _snr;
  234. return 0;
  235. }
  236. static int tda8083_read_ucblocks(struct dvb_frontend* fe, u32* ucblocks)
  237. {
  238. struct tda8083_state* state = fe->demodulator_priv;
  239. *ucblocks = tda8083_readreg(state, 0x0f);
  240. if (*ucblocks == 0xff)
  241. *ucblocks = 0xffffffff;
  242. return 0;
  243. }
  244. static int tda8083_set_frontend(struct dvb_frontend *fe)
  245. {
  246. struct dtv_frontend_properties *p = &fe->dtv_property_cache;
  247. struct tda8083_state* state = fe->demodulator_priv;
  248. if (fe->ops.tuner_ops.set_params) {
  249. fe->ops.tuner_ops.set_params(fe);
  250. if (fe->ops.i2c_gate_ctrl) fe->ops.i2c_gate_ctrl(fe, 0);
  251. }
  252. tda8083_set_inversion (state, p->inversion);
  253. tda8083_set_fec(state, p->fec_inner);
  254. tda8083_set_symbolrate(state, p->symbol_rate);
  255. tda8083_writereg (state, 0x00, 0x3c);
  256. tda8083_writereg (state, 0x00, 0x04);
  257. return 0;
  258. }
  259. static int tda8083_get_frontend(struct dvb_frontend *fe)
  260. {
  261. struct dtv_frontend_properties *p = &fe->dtv_property_cache;
  262. struct tda8083_state* state = fe->demodulator_priv;
  263. /* FIXME: get symbolrate & frequency offset...*/
  264. /*p->frequency = ???;*/
  265. p->inversion = (tda8083_readreg (state, 0x0e) & 0x80) ?
  266. INVERSION_ON : INVERSION_OFF;
  267. p->fec_inner = tda8083_get_fec(state);
  268. /*p->symbol_rate = tda8083_get_symbolrate (state);*/
  269. return 0;
  270. }
  271. static int tda8083_sleep(struct dvb_frontend* fe)
  272. {
  273. struct tda8083_state* state = fe->demodulator_priv;
  274. tda8083_writereg (state, 0x00, 0x02);
  275. return 0;
  276. }
  277. static int tda8083_init(struct dvb_frontend* fe)
  278. {
  279. struct tda8083_state* state = fe->demodulator_priv;
  280. int i;
  281. for (i=0; i<44; i++)
  282. tda8083_writereg (state, i, tda8083_init_tab[i]);
  283. tda8083_writereg (state, 0x00, 0x3c);
  284. tda8083_writereg (state, 0x00, 0x04);
  285. return 0;
  286. }
  287. static int tda8083_diseqc_send_burst(struct dvb_frontend *fe,
  288. enum fe_sec_mini_cmd burst)
  289. {
  290. struct tda8083_state* state = fe->demodulator_priv;
  291. tda8083_send_diseqc_burst (state, burst);
  292. tda8083_writereg (state, 0x00, 0x3c);
  293. tda8083_writereg (state, 0x00, 0x04);
  294. return 0;
  295. }
  296. static int tda8083_diseqc_set_tone(struct dvb_frontend *fe,
  297. enum fe_sec_tone_mode tone)
  298. {
  299. struct tda8083_state* state = fe->demodulator_priv;
  300. tda8083_set_tone (state, tone);
  301. tda8083_writereg (state, 0x00, 0x3c);
  302. tda8083_writereg (state, 0x00, 0x04);
  303. return 0;
  304. }
  305. static int tda8083_diseqc_set_voltage(struct dvb_frontend *fe,
  306. enum fe_sec_voltage voltage)
  307. {
  308. struct tda8083_state* state = fe->demodulator_priv;
  309. tda8083_set_voltage (state, voltage);
  310. tda8083_writereg (state, 0x00, 0x3c);
  311. tda8083_writereg (state, 0x00, 0x04);
  312. return 0;
  313. }
  314. static void tda8083_release(struct dvb_frontend* fe)
  315. {
  316. struct tda8083_state* state = fe->demodulator_priv;
  317. kfree(state);
  318. }
  319. static struct dvb_frontend_ops tda8083_ops;
  320. struct dvb_frontend* tda8083_attach(const struct tda8083_config* config,
  321. struct i2c_adapter* i2c)
  322. {
  323. struct tda8083_state* state = NULL;
  324. /* allocate memory for the internal state */
  325. state = kzalloc(sizeof(struct tda8083_state), GFP_KERNEL);
  326. if (state == NULL) goto error;
  327. /* setup the state */
  328. state->config = config;
  329. state->i2c = i2c;
  330. /* check if the demod is there */
  331. if ((tda8083_readreg(state, 0x00)) != 0x05) goto error;
  332. /* create dvb_frontend */
  333. memcpy(&state->frontend.ops, &tda8083_ops, sizeof(struct dvb_frontend_ops));
  334. state->frontend.demodulator_priv = state;
  335. return &state->frontend;
  336. error:
  337. kfree(state);
  338. return NULL;
  339. }
  340. static struct dvb_frontend_ops tda8083_ops = {
  341. .delsys = { SYS_DVBS },
  342. .info = {
  343. .name = "Philips TDA8083 DVB-S",
  344. .frequency_min = 920000, /* TDA8060 */
  345. .frequency_max = 2200000, /* TDA8060 */
  346. .frequency_stepsize = 125, /* kHz for QPSK frontends */
  347. /* .frequency_tolerance = ???,*/
  348. .symbol_rate_min = 12000000,
  349. .symbol_rate_max = 30000000,
  350. /* .symbol_rate_tolerance = ???,*/
  351. .caps = FE_CAN_INVERSION_AUTO |
  352. FE_CAN_FEC_1_2 | FE_CAN_FEC_2_3 | FE_CAN_FEC_3_4 |
  353. FE_CAN_FEC_4_5 | FE_CAN_FEC_5_6 | FE_CAN_FEC_6_7 |
  354. FE_CAN_FEC_7_8 | FE_CAN_FEC_8_9 | FE_CAN_FEC_AUTO |
  355. FE_CAN_QPSK | FE_CAN_MUTE_TS
  356. },
  357. .release = tda8083_release,
  358. .init = tda8083_init,
  359. .sleep = tda8083_sleep,
  360. .set_frontend = tda8083_set_frontend,
  361. .get_frontend = tda8083_get_frontend,
  362. .read_status = tda8083_read_status,
  363. .read_signal_strength = tda8083_read_signal_strength,
  364. .read_snr = tda8083_read_snr,
  365. .read_ber = tda8083_read_ber,
  366. .read_ucblocks = tda8083_read_ucblocks,
  367. .diseqc_send_master_cmd = tda8083_send_diseqc_msg,
  368. .diseqc_send_burst = tda8083_diseqc_send_burst,
  369. .set_tone = tda8083_diseqc_set_tone,
  370. .set_voltage = tda8083_diseqc_set_voltage,
  371. };
  372. module_param(debug, int, 0644);
  373. MODULE_PARM_DESC(debug, "Turn on/off frontend debugging (default:off).");
  374. MODULE_DESCRIPTION("Philips TDA8083 DVB-S Demodulator");
  375. MODULE_AUTHOR("Ralph Metzler, Holger Waechtler");
  376. MODULE_LICENSE("GPL");
  377. EXPORT_SYMBOL(tda8083_attach);