tda8083.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. Driver for Philips TDA8083 based QPSK Demodulator
  4. Copyright (C) 2001 Convergence Integrated Media GmbH
  5. written by Ralph Metzler <[email protected]>
  6. adoption to the new DVB frontend API and diagnostic ioctl's
  7. by Holger Waechtler <[email protected]>
  8. */
  9. #include <linux/init.h>
  10. #include <linux/kernel.h>
  11. #include <linux/module.h>
  12. #include <linux/string.h>
  13. #include <linux/slab.h>
  14. #include <linux/jiffies.h>
  15. #include <media/dvb_frontend.h>
  16. #include "tda8083.h"
  17. struct tda8083_state {
  18. struct i2c_adapter* i2c;
  19. /* configuration settings */
  20. const struct tda8083_config* config;
  21. struct dvb_frontend frontend;
  22. };
  23. static int debug;
  24. #define dprintk(args...) \
  25. do { \
  26. if (debug) printk(KERN_DEBUG "tda8083: " args); \
  27. } while (0)
  28. static u8 tda8083_init_tab [] = {
  29. 0x04, 0x00, 0x4a, 0x79, 0x04, 0x00, 0xff, 0xea,
  30. 0x48, 0x42, 0x79, 0x60, 0x70, 0x52, 0x9a, 0x10,
  31. 0x0e, 0x10, 0xf2, 0xa7, 0x93, 0x0b, 0x05, 0xc8,
  32. 0x9d, 0x00, 0x42, 0x80, 0x00, 0x60, 0x40, 0x00,
  33. 0x00, 0x75, 0x00, 0xe0, 0x00, 0x00, 0x00, 0x00,
  34. 0x00, 0x00, 0x00, 0x00
  35. };
  36. static int tda8083_writereg (struct tda8083_state* state, u8 reg, u8 data)
  37. {
  38. int ret;
  39. u8 buf [] = { reg, data };
  40. struct i2c_msg msg = { .addr = state->config->demod_address, .flags = 0, .buf = buf, .len = 2 };
  41. ret = i2c_transfer(state->i2c, &msg, 1);
  42. if (ret != 1)
  43. dprintk ("%s: writereg error (reg %02x, ret == %i)\n",
  44. __func__, reg, ret);
  45. return (ret != 1) ? -1 : 0;
  46. }
  47. static int tda8083_readregs (struct tda8083_state* state, u8 reg1, u8 *b, u8 len)
  48. {
  49. int ret;
  50. struct i2c_msg msg [] = { { .addr = state->config->demod_address, .flags = 0, .buf = &reg1, .len = 1 },
  51. { .addr = state->config->demod_address, .flags = I2C_M_RD, .buf = b, .len = len } };
  52. ret = i2c_transfer(state->i2c, msg, 2);
  53. if (ret != 2)
  54. dprintk ("%s: readreg error (reg %02x, ret == %i)\n",
  55. __func__, reg1, ret);
  56. return ret == 2 ? 0 : -1;
  57. }
  58. static inline u8 tda8083_readreg (struct tda8083_state* state, u8 reg)
  59. {
  60. u8 val;
  61. tda8083_readregs (state, reg, &val, 1);
  62. return val;
  63. }
  64. static int tda8083_set_inversion(struct tda8083_state *state,
  65. enum fe_spectral_inversion inversion)
  66. {
  67. /* XXX FIXME: implement other modes than FEC_AUTO */
  68. if (inversion == INVERSION_AUTO)
  69. return 0;
  70. return -EINVAL;
  71. }
  72. static int tda8083_set_fec(struct tda8083_state *state, enum fe_code_rate fec)
  73. {
  74. if (fec == FEC_AUTO)
  75. return tda8083_writereg (state, 0x07, 0xff);
  76. if (fec >= FEC_1_2 && fec <= FEC_8_9)
  77. return tda8083_writereg (state, 0x07, 1 << (FEC_8_9 - fec));
  78. return -EINVAL;
  79. }
  80. static enum fe_code_rate tda8083_get_fec(struct tda8083_state *state)
  81. {
  82. u8 index;
  83. static enum fe_code_rate fec_tab[] = {
  84. FEC_8_9, FEC_1_2, FEC_2_3, FEC_3_4,
  85. FEC_4_5, FEC_5_6, FEC_6_7, FEC_7_8
  86. };
  87. index = tda8083_readreg(state, 0x0e) & 0x07;
  88. return fec_tab [index];
  89. }
  90. static int tda8083_set_symbolrate (struct tda8083_state* state, u32 srate)
  91. {
  92. u32 ratio;
  93. u32 tmp;
  94. u8 filter;
  95. if (srate > 32000000)
  96. srate = 32000000;
  97. if (srate < 500000)
  98. srate = 500000;
  99. filter = 0;
  100. if (srate < 24000000)
  101. filter = 2;
  102. if (srate < 16000000)
  103. filter = 3;
  104. tmp = 31250 << 16;
  105. ratio = tmp / srate;
  106. tmp = (tmp % srate) << 8;
  107. ratio = (ratio << 8) + tmp / srate;
  108. tmp = (tmp % srate) << 8;
  109. ratio = (ratio << 8) + tmp / srate;
  110. dprintk("tda8083: ratio == %08x\n", (unsigned int) ratio);
  111. tda8083_writereg (state, 0x05, filter);
  112. tda8083_writereg (state, 0x02, (ratio >> 16) & 0xff);
  113. tda8083_writereg (state, 0x03, (ratio >> 8) & 0xff);
  114. tda8083_writereg (state, 0x04, (ratio ) & 0xff);
  115. tda8083_writereg (state, 0x00, 0x3c);
  116. tda8083_writereg (state, 0x00, 0x04);
  117. return 1;
  118. }
  119. static void tda8083_wait_diseqc_fifo (struct tda8083_state* state, int timeout)
  120. {
  121. unsigned long start = jiffies;
  122. while (time_is_after_jiffies(start + timeout) &&
  123. !(tda8083_readreg(state, 0x02) & 0x80))
  124. {
  125. msleep(50);
  126. }
  127. }
  128. static int tda8083_set_tone(struct tda8083_state *state,
  129. enum fe_sec_tone_mode tone)
  130. {
  131. tda8083_writereg (state, 0x26, 0xf1);
  132. switch (tone) {
  133. case SEC_TONE_OFF:
  134. return tda8083_writereg (state, 0x29, 0x00);
  135. case SEC_TONE_ON:
  136. return tda8083_writereg (state, 0x29, 0x80);
  137. default:
  138. return -EINVAL;
  139. }
  140. }
  141. static int tda8083_set_voltage(struct tda8083_state *state,
  142. enum fe_sec_voltage voltage)
  143. {
  144. switch (voltage) {
  145. case SEC_VOLTAGE_13:
  146. return tda8083_writereg (state, 0x20, 0x00);
  147. case SEC_VOLTAGE_18:
  148. return tda8083_writereg (state, 0x20, 0x11);
  149. default:
  150. return -EINVAL;
  151. }
  152. }
  153. static int tda8083_send_diseqc_burst(struct tda8083_state *state,
  154. enum fe_sec_mini_cmd burst)
  155. {
  156. switch (burst) {
  157. case SEC_MINI_A:
  158. tda8083_writereg (state, 0x29, (5 << 2)); /* send burst A */
  159. break;
  160. case SEC_MINI_B:
  161. tda8083_writereg (state, 0x29, (7 << 2)); /* send B */
  162. break;
  163. default:
  164. return -EINVAL;
  165. }
  166. tda8083_wait_diseqc_fifo (state, 100);
  167. return 0;
  168. }
  169. static int tda8083_send_diseqc_msg(struct dvb_frontend *fe,
  170. struct dvb_diseqc_master_cmd *m)
  171. {
  172. struct tda8083_state* state = fe->demodulator_priv;
  173. int i;
  174. tda8083_writereg (state, 0x29, (m->msg_len - 3) | (1 << 2)); /* enable */
  175. for (i=0; i<m->msg_len; i++)
  176. tda8083_writereg (state, 0x23 + i, m->msg[i]);
  177. tda8083_writereg (state, 0x29, (m->msg_len - 3) | (3 << 2)); /* send!! */
  178. tda8083_wait_diseqc_fifo (state, 100);
  179. return 0;
  180. }
  181. static int tda8083_read_status(struct dvb_frontend *fe,
  182. enum fe_status *status)
  183. {
  184. struct tda8083_state* state = fe->demodulator_priv;
  185. u8 signal = ~tda8083_readreg (state, 0x01);
  186. u8 sync = tda8083_readreg (state, 0x02);
  187. *status = 0;
  188. if (signal > 10)
  189. *status |= FE_HAS_SIGNAL;
  190. if (sync & 0x01)
  191. *status |= FE_HAS_CARRIER;
  192. if (sync & 0x02)
  193. *status |= FE_HAS_VITERBI;
  194. if (sync & 0x10)
  195. *status |= FE_HAS_SYNC;
  196. if (sync & 0x20) /* frontend can not lock */
  197. *status |= FE_TIMEDOUT;
  198. if ((sync & 0x1f) == 0x1f)
  199. *status |= FE_HAS_LOCK;
  200. return 0;
  201. }
  202. static int tda8083_read_ber(struct dvb_frontend* fe, u32* ber)
  203. {
  204. struct tda8083_state* state = fe->demodulator_priv;
  205. int ret;
  206. u8 buf[3];
  207. if ((ret = tda8083_readregs(state, 0x0b, buf, sizeof(buf))))
  208. return ret;
  209. *ber = ((buf[0] & 0x1f) << 16) | (buf[1] << 8) | buf[2];
  210. return 0;
  211. }
  212. static int tda8083_read_signal_strength(struct dvb_frontend* fe, u16* strength)
  213. {
  214. struct tda8083_state* state = fe->demodulator_priv;
  215. u8 signal = ~tda8083_readreg (state, 0x01);
  216. *strength = (signal << 8) | signal;
  217. return 0;
  218. }
  219. static int tda8083_read_snr(struct dvb_frontend* fe, u16* snr)
  220. {
  221. struct tda8083_state* state = fe->demodulator_priv;
  222. u8 _snr = tda8083_readreg (state, 0x08);
  223. *snr = (_snr << 8) | _snr;
  224. return 0;
  225. }
  226. static int tda8083_read_ucblocks(struct dvb_frontend* fe, u32* ucblocks)
  227. {
  228. struct tda8083_state* state = fe->demodulator_priv;
  229. *ucblocks = tda8083_readreg(state, 0x0f);
  230. if (*ucblocks == 0xff)
  231. *ucblocks = 0xffffffff;
  232. return 0;
  233. }
  234. static int tda8083_set_frontend(struct dvb_frontend *fe)
  235. {
  236. struct dtv_frontend_properties *p = &fe->dtv_property_cache;
  237. struct tda8083_state* state = fe->demodulator_priv;
  238. if (fe->ops.tuner_ops.set_params) {
  239. fe->ops.tuner_ops.set_params(fe);
  240. if (fe->ops.i2c_gate_ctrl) fe->ops.i2c_gate_ctrl(fe, 0);
  241. }
  242. tda8083_set_inversion (state, p->inversion);
  243. tda8083_set_fec(state, p->fec_inner);
  244. tda8083_set_symbolrate(state, p->symbol_rate);
  245. tda8083_writereg (state, 0x00, 0x3c);
  246. tda8083_writereg (state, 0x00, 0x04);
  247. return 0;
  248. }
  249. static int tda8083_get_frontend(struct dvb_frontend *fe,
  250. struct dtv_frontend_properties *p)
  251. {
  252. struct tda8083_state* state = fe->demodulator_priv;
  253. /* FIXME: get symbolrate & frequency offset...*/
  254. /*p->frequency = ???;*/
  255. p->inversion = (tda8083_readreg (state, 0x0e) & 0x80) ?
  256. INVERSION_ON : INVERSION_OFF;
  257. p->fec_inner = tda8083_get_fec(state);
  258. /*p->symbol_rate = tda8083_get_symbolrate (state);*/
  259. return 0;
  260. }
  261. static int tda8083_sleep(struct dvb_frontend* fe)
  262. {
  263. struct tda8083_state* state = fe->demodulator_priv;
  264. tda8083_writereg (state, 0x00, 0x02);
  265. return 0;
  266. }
  267. static int tda8083_init(struct dvb_frontend* fe)
  268. {
  269. struct tda8083_state* state = fe->demodulator_priv;
  270. int i;
  271. for (i=0; i<44; i++)
  272. tda8083_writereg (state, i, tda8083_init_tab[i]);
  273. tda8083_writereg (state, 0x00, 0x3c);
  274. tda8083_writereg (state, 0x00, 0x04);
  275. return 0;
  276. }
  277. static int tda8083_diseqc_send_burst(struct dvb_frontend *fe,
  278. enum fe_sec_mini_cmd 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,
  287. enum fe_sec_tone_mode 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,
  296. enum fe_sec_voltage voltage)
  297. {
  298. struct tda8083_state* state = fe->demodulator_priv;
  299. tda8083_set_voltage (state, voltage);
  300. tda8083_writereg (state, 0x00, 0x3c);
  301. tda8083_writereg (state, 0x00, 0x04);
  302. return 0;
  303. }
  304. static void tda8083_release(struct dvb_frontend* fe)
  305. {
  306. struct tda8083_state* state = fe->demodulator_priv;
  307. kfree(state);
  308. }
  309. static const struct dvb_frontend_ops tda8083_ops;
  310. struct dvb_frontend* tda8083_attach(const struct tda8083_config* config,
  311. struct i2c_adapter* i2c)
  312. {
  313. struct tda8083_state* state = NULL;
  314. /* allocate memory for the internal state */
  315. state = kzalloc(sizeof(struct tda8083_state), GFP_KERNEL);
  316. if (state == NULL) goto error;
  317. /* setup the state */
  318. state->config = config;
  319. state->i2c = i2c;
  320. /* check if the demod is there */
  321. if ((tda8083_readreg(state, 0x00)) != 0x05) goto error;
  322. /* create dvb_frontend */
  323. memcpy(&state->frontend.ops, &tda8083_ops, sizeof(struct dvb_frontend_ops));
  324. state->frontend.demodulator_priv = state;
  325. return &state->frontend;
  326. error:
  327. kfree(state);
  328. return NULL;
  329. }
  330. static const struct dvb_frontend_ops tda8083_ops = {
  331. .delsys = { SYS_DVBS },
  332. .info = {
  333. .name = "Philips TDA8083 DVB-S",
  334. .frequency_min_hz = 920 * MHz, /* TDA8060 */
  335. .frequency_max_hz = 2200 * MHz, /* TDA8060 */
  336. .frequency_stepsize_hz = 125 * kHz,
  337. .symbol_rate_min = 12000000,
  338. .symbol_rate_max = 30000000,
  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_GPL(tda8083_attach);