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