tda8083.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487
  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, fe_spectral_inversion_t inversion)
  75. {
  76. /* XXX FIXME: implement other modes than FEC_AUTO */
  77. if (inversion == INVERSION_AUTO)
  78. return 0;
  79. return -EINVAL;
  80. }
  81. static int tda8083_set_fec (struct tda8083_state* state, fe_code_rate_t fec)
  82. {
  83. if (fec == FEC_AUTO)
  84. return tda8083_writereg (state, 0x07, 0xff);
  85. if (fec >= FEC_1_2 && fec <= FEC_8_9)
  86. return tda8083_writereg (state, 0x07, 1 << (FEC_8_9 - fec));
  87. return -EINVAL;
  88. }
  89. static fe_code_rate_t tda8083_get_fec (struct tda8083_state* state)
  90. {
  91. u8 index;
  92. static fe_code_rate_t fec_tab [] = { FEC_8_9, FEC_1_2, FEC_2_3, FEC_3_4,
  93. FEC_4_5, FEC_5_6, FEC_6_7, FEC_7_8 };
  94. index = tda8083_readreg(state, 0x0e) & 0x07;
  95. return fec_tab [index];
  96. }
  97. static int tda8083_set_symbolrate (struct tda8083_state* state, u32 srate)
  98. {
  99. u32 ratio;
  100. u32 tmp;
  101. u8 filter;
  102. if (srate > 32000000)
  103. srate = 32000000;
  104. if (srate < 500000)
  105. srate = 500000;
  106. filter = 0;
  107. if (srate < 24000000)
  108. filter = 2;
  109. if (srate < 16000000)
  110. filter = 3;
  111. tmp = 31250 << 16;
  112. ratio = tmp / srate;
  113. tmp = (tmp % srate) << 8;
  114. ratio = (ratio << 8) + tmp / srate;
  115. tmp = (tmp % srate) << 8;
  116. ratio = (ratio << 8) + tmp / srate;
  117. dprintk("tda8083: ratio == %08x\n", (unsigned int) ratio);
  118. tda8083_writereg (state, 0x05, filter);
  119. tda8083_writereg (state, 0x02, (ratio >> 16) & 0xff);
  120. tda8083_writereg (state, 0x03, (ratio >> 8) & 0xff);
  121. tda8083_writereg (state, 0x04, (ratio ) & 0xff);
  122. tda8083_writereg (state, 0x00, 0x3c);
  123. tda8083_writereg (state, 0x00, 0x04);
  124. return 1;
  125. }
  126. static void tda8083_wait_diseqc_fifo (struct tda8083_state* state, int timeout)
  127. {
  128. unsigned long start = jiffies;
  129. while (jiffies - start < timeout &&
  130. !(tda8083_readreg(state, 0x02) & 0x80))
  131. {
  132. msleep(50);
  133. };
  134. }
  135. static int tda8083_set_tone (struct tda8083_state* state, fe_sec_tone_mode_t tone)
  136. {
  137. tda8083_writereg (state, 0x26, 0xf1);
  138. switch (tone) {
  139. case SEC_TONE_OFF:
  140. return tda8083_writereg (state, 0x29, 0x00);
  141. case SEC_TONE_ON:
  142. return tda8083_writereg (state, 0x29, 0x80);
  143. default:
  144. return -EINVAL;
  145. };
  146. }
  147. static int tda8083_set_voltage (struct tda8083_state* state, fe_sec_voltage_t voltage)
  148. {
  149. switch (voltage) {
  150. case SEC_VOLTAGE_13:
  151. return tda8083_writereg (state, 0x20, 0x00);
  152. case SEC_VOLTAGE_18:
  153. return tda8083_writereg (state, 0x20, 0x11);
  154. default:
  155. return -EINVAL;
  156. };
  157. }
  158. static int tda8083_send_diseqc_burst (struct tda8083_state* state, fe_sec_mini_cmd_t burst)
  159. {
  160. switch (burst) {
  161. case SEC_MINI_A:
  162. tda8083_writereg (state, 0x29, (5 << 2)); /* send burst A */
  163. break;
  164. case SEC_MINI_B:
  165. tda8083_writereg (state, 0x29, (7 << 2)); /* send B */
  166. break;
  167. default:
  168. return -EINVAL;
  169. };
  170. tda8083_wait_diseqc_fifo (state, 100);
  171. return 0;
  172. }
  173. static int tda8083_send_diseqc_msg (struct dvb_frontend* fe,
  174. struct dvb_diseqc_master_cmd *m)
  175. {
  176. struct tda8083_state* state = fe->demodulator_priv;
  177. int i;
  178. tda8083_writereg (state, 0x29, (m->msg_len - 3) | (1 << 2)); /* enable */
  179. for (i=0; i<m->msg_len; i++)
  180. tda8083_writereg (state, 0x23 + i, m->msg[i]);
  181. tda8083_writereg (state, 0x29, (m->msg_len - 3) | (3 << 2)); /* send!! */
  182. tda8083_wait_diseqc_fifo (state, 100);
  183. return 0;
  184. }
  185. static int tda8083_read_status(struct dvb_frontend* fe, fe_status_t* status)
  186. {
  187. struct tda8083_state* state = fe->demodulator_priv;
  188. u8 signal = ~tda8083_readreg (state, 0x01);
  189. u8 sync = tda8083_readreg (state, 0x02);
  190. *status = 0;
  191. if (signal > 10)
  192. *status |= FE_HAS_SIGNAL;
  193. if (sync & 0x01)
  194. *status |= FE_HAS_CARRIER;
  195. if (sync & 0x02)
  196. *status |= FE_HAS_VITERBI;
  197. if (sync & 0x10)
  198. *status |= FE_HAS_SYNC;
  199. if (sync & 0x20) /* frontend can not lock */
  200. *status |= FE_TIMEDOUT;
  201. if ((sync & 0x1f) == 0x1f)
  202. *status |= FE_HAS_LOCK;
  203. return 0;
  204. }
  205. static int tda8083_read_ber(struct dvb_frontend* fe, u32* ber)
  206. {
  207. struct tda8083_state* state = fe->demodulator_priv;
  208. int ret;
  209. u8 buf[3];
  210. if ((ret = tda8083_readregs(state, 0x0b, buf, sizeof(buf))))
  211. return ret;
  212. *ber = ((buf[0] & 0x1f) << 16) | (buf[1] << 8) | buf[2];
  213. return 0;
  214. }
  215. static int tda8083_read_signal_strength(struct dvb_frontend* fe, u16* strength)
  216. {
  217. struct tda8083_state* state = fe->demodulator_priv;
  218. u8 signal = ~tda8083_readreg (state, 0x01);
  219. *strength = (signal << 8) | signal;
  220. return 0;
  221. }
  222. static int tda8083_read_snr(struct dvb_frontend* fe, u16* snr)
  223. {
  224. struct tda8083_state* state = fe->demodulator_priv;
  225. u8 _snr = tda8083_readreg (state, 0x08);
  226. *snr = (_snr << 8) | _snr;
  227. return 0;
  228. }
  229. static int tda8083_read_ucblocks(struct dvb_frontend* fe, u32* ucblocks)
  230. {
  231. struct tda8083_state* state = fe->demodulator_priv;
  232. *ucblocks = tda8083_readreg(state, 0x0f);
  233. if (*ucblocks == 0xff)
  234. *ucblocks = 0xffffffff;
  235. return 0;
  236. }
  237. static int tda8083_set_frontend(struct dvb_frontend* fe, struct dvb_frontend_parameters *p)
  238. {
  239. struct tda8083_state* state = fe->demodulator_priv;
  240. if (fe->ops.tuner_ops.set_params) {
  241. fe->ops.tuner_ops.set_params(fe, p);
  242. if (fe->ops.i2c_gate_ctrl) fe->ops.i2c_gate_ctrl(fe, 0);
  243. }
  244. tda8083_set_inversion (state, p->inversion);
  245. tda8083_set_fec (state, p->u.qpsk.fec_inner);
  246. tda8083_set_symbolrate (state, p->u.qpsk.symbol_rate);
  247. tda8083_writereg (state, 0x00, 0x3c);
  248. tda8083_writereg (state, 0x00, 0x04);
  249. return 0;
  250. }
  251. static int tda8083_get_frontend(struct dvb_frontend* fe, struct dvb_frontend_parameters *p)
  252. {
  253. struct tda8083_state* state = fe->demodulator_priv;
  254. /* FIXME: get symbolrate & frequency offset...*/
  255. /*p->frequency = ???;*/
  256. p->inversion = (tda8083_readreg (state, 0x0e) & 0x80) ?
  257. INVERSION_ON : INVERSION_OFF;
  258. p->u.qpsk.fec_inner = tda8083_get_fec (state);
  259. /*p->u.qpsk.symbol_rate = tda8083_get_symbolrate (state);*/
  260. return 0;
  261. }
  262. static int tda8083_sleep(struct dvb_frontend* fe)
  263. {
  264. struct tda8083_state* state = fe->demodulator_priv;
  265. tda8083_writereg (state, 0x00, 0x02);
  266. return 0;
  267. }
  268. static int tda8083_init(struct dvb_frontend* fe)
  269. {
  270. struct tda8083_state* state = fe->demodulator_priv;
  271. int i;
  272. for (i=0; i<44; i++)
  273. tda8083_writereg (state, i, tda8083_init_tab[i]);
  274. tda8083_writereg (state, 0x00, 0x3c);
  275. tda8083_writereg (state, 0x00, 0x04);
  276. return 0;
  277. }
  278. static int tda8083_diseqc_send_burst(struct dvb_frontend* fe, fe_sec_mini_cmd_t burst)
  279. {
  280. struct tda8083_state* state = fe->demodulator_priv;
  281. tda8083_send_diseqc_burst (state, burst);
  282. tda8083_writereg (state, 0x00, 0x3c);
  283. tda8083_writereg (state, 0x00, 0x04);
  284. return 0;
  285. }
  286. static int tda8083_diseqc_set_tone(struct dvb_frontend* fe, fe_sec_tone_mode_t tone)
  287. {
  288. struct tda8083_state* state = fe->demodulator_priv;
  289. tda8083_set_tone (state, tone);
  290. tda8083_writereg (state, 0x00, 0x3c);
  291. tda8083_writereg (state, 0x00, 0x04);
  292. return 0;
  293. }
  294. static int tda8083_diseqc_set_voltage(struct dvb_frontend* fe, fe_sec_voltage_t voltage)
  295. {
  296. struct tda8083_state* state = fe->demodulator_priv;
  297. tda8083_set_voltage (state, voltage);
  298. tda8083_writereg (state, 0x00, 0x3c);
  299. tda8083_writereg (state, 0x00, 0x04);
  300. return 0;
  301. }
  302. static void tda8083_release(struct dvb_frontend* fe)
  303. {
  304. struct tda8083_state* state = fe->demodulator_priv;
  305. kfree(state);
  306. }
  307. static struct dvb_frontend_ops tda8083_ops;
  308. struct dvb_frontend* tda8083_attach(const struct tda8083_config* config,
  309. struct i2c_adapter* i2c)
  310. {
  311. struct tda8083_state* state = NULL;
  312. /* allocate memory for the internal state */
  313. state = kzalloc(sizeof(struct tda8083_state), GFP_KERNEL);
  314. if (state == NULL) goto error;
  315. /* setup the state */
  316. state->config = config;
  317. state->i2c = i2c;
  318. /* check if the demod is there */
  319. if ((tda8083_readreg(state, 0x00)) != 0x05) goto error;
  320. /* create dvb_frontend */
  321. memcpy(&state->frontend.ops, &tda8083_ops, sizeof(struct dvb_frontend_ops));
  322. state->frontend.demodulator_priv = state;
  323. return &state->frontend;
  324. error:
  325. kfree(state);
  326. return NULL;
  327. }
  328. static struct dvb_frontend_ops tda8083_ops = {
  329. .info = {
  330. .name = "Philips TDA8083 DVB-S",
  331. .type = FE_QPSK,
  332. .frequency_min = 920000, /* TDA8060 */
  333. .frequency_max = 2200000, /* TDA8060 */
  334. .frequency_stepsize = 125, /* kHz for QPSK frontends */
  335. /* .frequency_tolerance = ???,*/
  336. .symbol_rate_min = 12000000,
  337. .symbol_rate_max = 30000000,
  338. /* .symbol_rate_tolerance = ???,*/
  339. .caps = FE_CAN_INVERSION_AUTO |
  340. FE_CAN_FEC_1_2 | FE_CAN_FEC_2_3 | FE_CAN_FEC_3_4 |
  341. FE_CAN_FEC_4_5 | FE_CAN_FEC_5_6 | FE_CAN_FEC_6_7 |
  342. FE_CAN_FEC_7_8 | FE_CAN_FEC_8_9 | FE_CAN_FEC_AUTO |
  343. FE_CAN_QPSK | FE_CAN_MUTE_TS
  344. },
  345. .release = tda8083_release,
  346. .init = tda8083_init,
  347. .sleep = tda8083_sleep,
  348. .set_frontend = tda8083_set_frontend,
  349. .get_frontend = tda8083_get_frontend,
  350. .read_status = tda8083_read_status,
  351. .read_signal_strength = tda8083_read_signal_strength,
  352. .read_snr = tda8083_read_snr,
  353. .read_ber = tda8083_read_ber,
  354. .read_ucblocks = tda8083_read_ucblocks,
  355. .diseqc_send_master_cmd = tda8083_send_diseqc_msg,
  356. .diseqc_send_burst = tda8083_diseqc_send_burst,
  357. .set_tone = tda8083_diseqc_set_tone,
  358. .set_voltage = tda8083_diseqc_set_voltage,
  359. };
  360. module_param(debug, int, 0644);
  361. MODULE_PARM_DESC(debug, "Turn on/off frontend debugging (default:off).");
  362. MODULE_DESCRIPTION("Philips TDA8083 DVB-S Demodulator");
  363. MODULE_AUTHOR("Ralph Metzler, Holger Waechtler");
  364. MODULE_LICENSE("GPL");
  365. EXPORT_SYMBOL(tda8083_attach);