zl10353.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  1. /*
  2. * Driver for Zarlink DVB-T ZL10353 demodulator
  3. *
  4. * Copyright (C) 2006 Christopher Pascoe <c.pascoe@itee.uq.edu.au>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  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. *
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.=
  20. */
  21. #include <linux/kernel.h>
  22. #include <linux/module.h>
  23. #include <linux/moduleparam.h>
  24. #include <linux/init.h>
  25. #include <linux/delay.h>
  26. #include <linux/string.h>
  27. #include <linux/slab.h>
  28. #include "dvb_frontend.h"
  29. #include "zl10353_priv.h"
  30. #include "zl10353.h"
  31. struct zl10353_state {
  32. struct i2c_adapter *i2c;
  33. struct dvb_frontend frontend;
  34. struct zl10353_config config;
  35. };
  36. static int debug_regs = 0;
  37. static int zl10353_single_write(struct dvb_frontend *fe, u8 reg, u8 val)
  38. {
  39. struct zl10353_state *state = fe->demodulator_priv;
  40. u8 buf[2] = { reg, val };
  41. struct i2c_msg msg = { .addr = state->config.demod_address, .flags = 0,
  42. .buf = buf, .len = 2 };
  43. int err = i2c_transfer(state->i2c, &msg, 1);
  44. if (err != 1) {
  45. printk("zl10353: write to reg %x failed (err = %d)!\n", reg, err);
  46. return err;
  47. }
  48. return 0;
  49. }
  50. int zl10353_write(struct dvb_frontend *fe, u8 *ibuf, int ilen)
  51. {
  52. int err, i;
  53. for (i = 0; i < ilen - 1; i++)
  54. if ((err = zl10353_single_write(fe, ibuf[0] + i, ibuf[i + 1])))
  55. return err;
  56. return 0;
  57. }
  58. static int zl10353_read_register(struct zl10353_state *state, u8 reg)
  59. {
  60. int ret;
  61. u8 b0[1] = { reg };
  62. u8 b1[1] = { 0 };
  63. struct i2c_msg msg[2] = { { .addr = state->config.demod_address,
  64. .flags = 0,
  65. .buf = b0, .len = 1 },
  66. { .addr = state->config.demod_address,
  67. .flags = I2C_M_RD,
  68. .buf = b1, .len = 1 } };
  69. ret = i2c_transfer(state->i2c, msg, 2);
  70. if (ret != 2) {
  71. printk("%s: readreg error (reg=%d, ret==%i)\n",
  72. __FUNCTION__, reg, ret);
  73. return ret;
  74. }
  75. return b1[0];
  76. }
  77. static void zl10353_dump_regs(struct dvb_frontend *fe)
  78. {
  79. struct zl10353_state *state = fe->demodulator_priv;
  80. char buf[52], buf2[4];
  81. int ret;
  82. u8 reg;
  83. /* Dump all registers. */
  84. for (reg = 0; ; reg++) {
  85. if (reg % 16 == 0) {
  86. if (reg)
  87. printk(KERN_DEBUG "%s\n", buf);
  88. sprintf(buf, "%02x: ", reg);
  89. }
  90. ret = zl10353_read_register(state, reg);
  91. if (ret >= 0)
  92. sprintf(buf2, "%02x ", (u8)ret);
  93. else
  94. strcpy(buf2, "-- ");
  95. strcat(buf, buf2);
  96. if (reg == 0xff)
  97. break;
  98. }
  99. printk(KERN_DEBUG "%s\n", buf);
  100. }
  101. static int zl10353_sleep(struct dvb_frontend *fe)
  102. {
  103. static u8 zl10353_softdown[] = { 0x50, 0x0C, 0x44 };
  104. zl10353_write(fe, zl10353_softdown, sizeof(zl10353_softdown));
  105. return 0;
  106. }
  107. static int zl10353_set_parameters(struct dvb_frontend *fe,
  108. struct dvb_frontend_parameters *param)
  109. {
  110. struct zl10353_state *state = fe->demodulator_priv;
  111. u8 pllbuf[6] = { 0x67 };
  112. /* These settings set "auto-everything" and start the FSM. */
  113. zl10353_single_write(fe, 0x55, 0x80);
  114. udelay(200);
  115. zl10353_single_write(fe, 0xEA, 0x01);
  116. udelay(200);
  117. zl10353_single_write(fe, 0xEA, 0x00);
  118. zl10353_single_write(fe, 0x56, 0x28);
  119. zl10353_single_write(fe, 0x89, 0x20);
  120. zl10353_single_write(fe, 0x5E, 0x00);
  121. zl10353_single_write(fe, 0x65, 0x5A);
  122. zl10353_single_write(fe, 0x66, 0xE9);
  123. zl10353_single_write(fe, 0x6C, 0xCD);
  124. zl10353_single_write(fe, 0x6D, 0x7E);
  125. zl10353_single_write(fe, 0x62, 0x0A);
  126. // if there is no attached secondary tuner, we call set_params to program
  127. // a potential tuner attached somewhere else
  128. if (state->config.no_tuner) {
  129. if (fe->ops.tuner_ops.set_params) {
  130. fe->ops.tuner_ops.set_params(fe, param);
  131. if (fe->ops.i2c_gate_ctrl) fe->ops.i2c_gate_ctrl(fe, 0);
  132. }
  133. }
  134. // if pllbuf is defined, retrieve the settings
  135. if (fe->ops.tuner_ops.calc_regs) {
  136. fe->ops.tuner_ops.calc_regs(fe, param, pllbuf+1, 5);
  137. pllbuf[1] <<= 1;
  138. } else {
  139. // fake pllbuf settings
  140. pllbuf[1] = 0x61 << 1;
  141. pllbuf[2] = 0;
  142. pllbuf[3] = 0;
  143. pllbuf[3] = 0;
  144. pllbuf[4] = 0;
  145. }
  146. // there is no call to _just_ start decoding, so we send the pllbuf anyway
  147. // even if there isn't a PLL attached to the secondary bus
  148. zl10353_write(fe, pllbuf, sizeof(pllbuf));
  149. zl10353_single_write(fe, 0x5F, 0x13);
  150. zl10353_single_write(fe, 0x70, 0x01);
  151. udelay(250);
  152. zl10353_single_write(fe, 0xE4, 0x00);
  153. zl10353_single_write(fe, 0xE5, 0x2A);
  154. zl10353_single_write(fe, 0xE9, 0x02);
  155. zl10353_single_write(fe, 0xE7, 0x40);
  156. zl10353_single_write(fe, 0xE8, 0x10);
  157. return 0;
  158. }
  159. static int zl10353_read_status(struct dvb_frontend *fe, fe_status_t *status)
  160. {
  161. struct zl10353_state *state = fe->demodulator_priv;
  162. int s6, s7, s8;
  163. if ((s6 = zl10353_read_register(state, STATUS_6)) < 0)
  164. return -EREMOTEIO;
  165. if ((s7 = zl10353_read_register(state, STATUS_7)) < 0)
  166. return -EREMOTEIO;
  167. if ((s8 = zl10353_read_register(state, STATUS_8)) < 0)
  168. return -EREMOTEIO;
  169. *status = 0;
  170. if (s6 & (1 << 2))
  171. *status |= FE_HAS_CARRIER;
  172. if (s6 & (1 << 1))
  173. *status |= FE_HAS_VITERBI;
  174. if (s6 & (1 << 5))
  175. *status |= FE_HAS_LOCK;
  176. if (s7 & (1 << 4))
  177. *status |= FE_HAS_SYNC;
  178. if (s8 & (1 << 6))
  179. *status |= FE_HAS_SIGNAL;
  180. if ((*status & (FE_HAS_CARRIER | FE_HAS_VITERBI | FE_HAS_SYNC)) !=
  181. (FE_HAS_CARRIER | FE_HAS_VITERBI | FE_HAS_SYNC))
  182. *status &= ~FE_HAS_LOCK;
  183. return 0;
  184. }
  185. static int zl10353_read_snr(struct dvb_frontend *fe, u16 *snr)
  186. {
  187. struct zl10353_state *state = fe->demodulator_priv;
  188. u8 _snr;
  189. if (debug_regs)
  190. zl10353_dump_regs(fe);
  191. _snr = zl10353_read_register(state, SNR);
  192. *snr = (_snr << 8) | _snr;
  193. return 0;
  194. }
  195. static int zl10353_get_tune_settings(struct dvb_frontend *fe,
  196. struct dvb_frontend_tune_settings
  197. *fe_tune_settings)
  198. {
  199. fe_tune_settings->min_delay_ms = 1000;
  200. fe_tune_settings->step_size = 0;
  201. fe_tune_settings->max_drift = 0;
  202. return 0;
  203. }
  204. static int zl10353_init(struct dvb_frontend *fe)
  205. {
  206. struct zl10353_state *state = fe->demodulator_priv;
  207. u8 zl10353_reset_attach[6] = { 0x50, 0x03, 0x64, 0x46, 0x15, 0x0F };
  208. int rc = 0;
  209. if (debug_regs)
  210. zl10353_dump_regs(fe);
  211. if (state->config.parallel_ts)
  212. zl10353_reset_attach[2] &= ~0x20;
  213. /* Do a "hard" reset if not already done */
  214. if (zl10353_read_register(state, 0x50) != zl10353_reset_attach[1] ||
  215. zl10353_read_register(state, 0x51) != zl10353_reset_attach[2]) {
  216. rc = zl10353_write(fe, zl10353_reset_attach,
  217. sizeof(zl10353_reset_attach));
  218. if (debug_regs)
  219. zl10353_dump_regs(fe);
  220. }
  221. return 0;
  222. }
  223. static void zl10353_release(struct dvb_frontend *fe)
  224. {
  225. struct zl10353_state *state = fe->demodulator_priv;
  226. kfree(state);
  227. }
  228. static struct dvb_frontend_ops zl10353_ops;
  229. struct dvb_frontend *zl10353_attach(const struct zl10353_config *config,
  230. struct i2c_adapter *i2c)
  231. {
  232. struct zl10353_state *state = NULL;
  233. /* allocate memory for the internal state */
  234. state = kzalloc(sizeof(struct zl10353_state), GFP_KERNEL);
  235. if (state == NULL)
  236. goto error;
  237. /* setup the state */
  238. state->i2c = i2c;
  239. memcpy(&state->config, config, sizeof(struct zl10353_config));
  240. /* check if the demod is there */
  241. if (zl10353_read_register(state, CHIP_ID) != ID_ZL10353)
  242. goto error;
  243. /* create dvb_frontend */
  244. memcpy(&state->frontend.ops, &zl10353_ops, sizeof(struct dvb_frontend_ops));
  245. state->frontend.demodulator_priv = state;
  246. return &state->frontend;
  247. error:
  248. kfree(state);
  249. return NULL;
  250. }
  251. static struct dvb_frontend_ops zl10353_ops = {
  252. .info = {
  253. .name = "Zarlink ZL10353 DVB-T",
  254. .type = FE_OFDM,
  255. .frequency_min = 174000000,
  256. .frequency_max = 862000000,
  257. .frequency_stepsize = 166667,
  258. .frequency_tolerance = 0,
  259. .caps = FE_CAN_FEC_1_2 | FE_CAN_FEC_2_3 |
  260. FE_CAN_FEC_3_4 | FE_CAN_FEC_5_6 | FE_CAN_FEC_7_8 |
  261. FE_CAN_FEC_AUTO |
  262. FE_CAN_QPSK | FE_CAN_QAM_16 | FE_CAN_QAM_64 | FE_CAN_QAM_AUTO |
  263. FE_CAN_TRANSMISSION_MODE_AUTO | FE_CAN_GUARD_INTERVAL_AUTO |
  264. FE_CAN_HIERARCHY_AUTO | FE_CAN_RECOVER |
  265. FE_CAN_MUTE_TS
  266. },
  267. .release = zl10353_release,
  268. .init = zl10353_init,
  269. .sleep = zl10353_sleep,
  270. .write = zl10353_write,
  271. .set_frontend = zl10353_set_parameters,
  272. .get_tune_settings = zl10353_get_tune_settings,
  273. .read_status = zl10353_read_status,
  274. .read_snr = zl10353_read_snr,
  275. };
  276. module_param(debug_regs, int, 0644);
  277. MODULE_PARM_DESC(debug_regs, "Turn on/off frontend register dumps (default:off).");
  278. MODULE_DESCRIPTION("Zarlink ZL10353 DVB-T demodulator driver");
  279. MODULE_AUTHOR("Chris Pascoe");
  280. MODULE_LICENSE("GPL");
  281. EXPORT_SYMBOL(zl10353_attach);