tda8083.c 11 KB

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