tda8083.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456
  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 "dvb_frontend.h"
  26. #include "tda8083.h"
  27. struct tda8083_state {
  28. struct i2c_adapter* i2c;
  29. struct dvb_frontend_ops ops;
  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 & 0x1f) == 0x1f)
  201. *status |= FE_HAS_LOCK;
  202. return 0;
  203. }
  204. static int tda8083_read_signal_strength(struct dvb_frontend* fe, u16* strength)
  205. {
  206. struct tda8083_state* state = fe->demodulator_priv;
  207. u8 signal = ~tda8083_readreg (state, 0x01);
  208. *strength = (signal << 8) | signal;
  209. return 0;
  210. }
  211. static int tda8083_read_snr(struct dvb_frontend* fe, u16* snr)
  212. {
  213. struct tda8083_state* state = fe->demodulator_priv;
  214. u8 _snr = tda8083_readreg (state, 0x08);
  215. *snr = (_snr << 8) | _snr;
  216. return 0;
  217. }
  218. static int tda8083_set_frontend(struct dvb_frontend* fe, struct dvb_frontend_parameters *p)
  219. {
  220. struct tda8083_state* state = fe->demodulator_priv;
  221. state->config->pll_set(fe, p);
  222. tda8083_set_inversion (state, p->inversion);
  223. tda8083_set_fec (state, p->u.qpsk.fec_inner);
  224. tda8083_set_symbolrate (state, p->u.qpsk.symbol_rate);
  225. tda8083_writereg (state, 0x00, 0x3c);
  226. tda8083_writereg (state, 0x00, 0x04);
  227. return 0;
  228. }
  229. static int tda8083_get_frontend(struct dvb_frontend* fe, struct dvb_frontend_parameters *p)
  230. {
  231. struct tda8083_state* state = fe->demodulator_priv;
  232. /* FIXME: get symbolrate & frequency offset...*/
  233. /*p->frequency = ???;*/
  234. p->inversion = (tda8083_readreg (state, 0x0e) & 0x80) ?
  235. INVERSION_ON : INVERSION_OFF;
  236. p->u.qpsk.fec_inner = tda8083_get_fec (state);
  237. /*p->u.qpsk.symbol_rate = tda8083_get_symbolrate (state);*/
  238. return 0;
  239. }
  240. static int tda8083_sleep(struct dvb_frontend* fe)
  241. {
  242. struct tda8083_state* state = fe->demodulator_priv;
  243. tda8083_writereg (state, 0x00, 0x02);
  244. return 0;
  245. }
  246. static int tda8083_init(struct dvb_frontend* fe)
  247. {
  248. struct tda8083_state* state = fe->demodulator_priv;
  249. int i;
  250. for (i=0; i<44; i++)
  251. tda8083_writereg (state, i, tda8083_init_tab[i]);
  252. if (state->config->pll_init) state->config->pll_init(fe);
  253. tda8083_writereg (state, 0x00, 0x3c);
  254. tda8083_writereg (state, 0x00, 0x04);
  255. return 0;
  256. }
  257. static int tda8083_diseqc_send_burst(struct dvb_frontend* fe, fe_sec_mini_cmd_t burst)
  258. {
  259. struct tda8083_state* state = fe->demodulator_priv;
  260. tda8083_send_diseqc_burst (state, burst);
  261. tda8083_writereg (state, 0x00, 0x3c);
  262. tda8083_writereg (state, 0x00, 0x04);
  263. return 0;
  264. }
  265. static int tda8083_diseqc_set_tone(struct dvb_frontend* fe, fe_sec_tone_mode_t tone)
  266. {
  267. struct tda8083_state* state = fe->demodulator_priv;
  268. tda8083_set_tone (state, tone);
  269. tda8083_writereg (state, 0x00, 0x3c);
  270. tda8083_writereg (state, 0x00, 0x04);
  271. return 0;
  272. }
  273. static int tda8083_diseqc_set_voltage(struct dvb_frontend* fe, fe_sec_voltage_t voltage)
  274. {
  275. struct tda8083_state* state = fe->demodulator_priv;
  276. tda8083_set_voltage (state, voltage);
  277. tda8083_writereg (state, 0x00, 0x3c);
  278. tda8083_writereg (state, 0x00, 0x04);
  279. return 0;
  280. }
  281. static void tda8083_release(struct dvb_frontend* fe)
  282. {
  283. struct tda8083_state* state = fe->demodulator_priv;
  284. kfree(state);
  285. }
  286. static struct dvb_frontend_ops tda8083_ops;
  287. struct dvb_frontend* tda8083_attach(const struct tda8083_config* config,
  288. struct i2c_adapter* i2c)
  289. {
  290. struct tda8083_state* state = NULL;
  291. /* allocate memory for the internal state */
  292. state = kmalloc(sizeof(struct tda8083_state), GFP_KERNEL);
  293. if (state == NULL) goto error;
  294. /* setup the state */
  295. state->config = config;
  296. state->i2c = i2c;
  297. memcpy(&state->ops, &tda8083_ops, sizeof(struct dvb_frontend_ops));
  298. /* check if the demod is there */
  299. if ((tda8083_readreg(state, 0x00)) != 0x05) goto error;
  300. /* create dvb_frontend */
  301. state->frontend.ops = &state->ops;
  302. state->frontend.demodulator_priv = state;
  303. return &state->frontend;
  304. error:
  305. kfree(state);
  306. return NULL;
  307. }
  308. static struct dvb_frontend_ops tda8083_ops = {
  309. .info = {
  310. .name = "Philips TDA8083 DVB-S",
  311. .type = FE_QPSK,
  312. .frequency_min = 950000, /* FIXME: guessed! */
  313. .frequency_max = 1400000, /* FIXME: guessed! */
  314. .frequency_stepsize = 125, /* kHz for QPSK frontends */
  315. /* .frequency_tolerance = ???,*/
  316. .symbol_rate_min = 1000000, /* FIXME: guessed! */
  317. .symbol_rate_max = 45000000, /* FIXME: guessed! */
  318. /* .symbol_rate_tolerance = ???,*/
  319. .caps = FE_CAN_INVERSION_AUTO |
  320. FE_CAN_FEC_1_2 | FE_CAN_FEC_2_3 | FE_CAN_FEC_3_4 |
  321. FE_CAN_FEC_4_5 | FE_CAN_FEC_5_6 | FE_CAN_FEC_6_7 |
  322. FE_CAN_FEC_7_8 | FE_CAN_FEC_8_9 | FE_CAN_FEC_AUTO |
  323. FE_CAN_QPSK | FE_CAN_MUTE_TS
  324. },
  325. .release = tda8083_release,
  326. .init = tda8083_init,
  327. .sleep = tda8083_sleep,
  328. .set_frontend = tda8083_set_frontend,
  329. .get_frontend = tda8083_get_frontend,
  330. .read_status = tda8083_read_status,
  331. .read_signal_strength = tda8083_read_signal_strength,
  332. .read_snr = tda8083_read_snr,
  333. .diseqc_send_master_cmd = tda8083_send_diseqc_msg,
  334. .diseqc_send_burst = tda8083_diseqc_send_burst,
  335. .set_tone = tda8083_diseqc_set_tone,
  336. .set_voltage = tda8083_diseqc_set_voltage,
  337. };
  338. module_param(debug, int, 0644);
  339. MODULE_PARM_DESC(debug, "Turn on/off frontend debugging (default:off).");
  340. MODULE_DESCRIPTION("Philips TDA8083 DVB-S Demodulator");
  341. MODULE_AUTHOR("Ralph Metzler, Holger Waechtler");
  342. MODULE_LICENSE("GPL");
  343. EXPORT_SYMBOL(tda8083_attach);