zl10353.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  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 dvb_frontend_ops ops;
  35. struct zl10353_config config;
  36. };
  37. static int debug_regs = 0;
  38. static int zl10353_single_write(struct dvb_frontend *fe, u8 reg, u8 val)
  39. {
  40. struct zl10353_state *state = fe->demodulator_priv;
  41. u8 buf[2] = { reg, val };
  42. struct i2c_msg msg = { .addr = state->config.demod_address, .flags = 0,
  43. .buf = buf, .len = 2 };
  44. int err = i2c_transfer(state->i2c, &msg, 1);
  45. if (err != 1) {
  46. printk("zl10353: write to reg %x failed (err = %d)!\n", reg, err);
  47. return err;
  48. }
  49. return 0;
  50. }
  51. int zl10353_write(struct dvb_frontend *fe, u8 *ibuf, int ilen)
  52. {
  53. int err, i;
  54. for (i = 0; i < ilen - 1; i++)
  55. if ((err = zl10353_single_write(fe, ibuf[0] + i, ibuf[i + 1])))
  56. return err;
  57. return 0;
  58. }
  59. static int zl10353_read_register(struct zl10353_state *state, u8 reg)
  60. {
  61. int ret;
  62. u8 b0[1] = { reg };
  63. u8 b1[1] = { 0 };
  64. struct i2c_msg msg[2] = { { .addr = state->config.demod_address,
  65. .flags = 0,
  66. .buf = b0, .len = 1 },
  67. { .addr = state->config.demod_address,
  68. .flags = I2C_M_RD,
  69. .buf = b1, .len = 1 } };
  70. ret = i2c_transfer(state->i2c, msg, 2);
  71. if (ret != 2) {
  72. printk("%s: readreg error (reg=%d, ret==%i)\n",
  73. __FUNCTION__, reg, ret);
  74. return ret;
  75. }
  76. return b1[0];
  77. }
  78. static void zl10353_dump_regs(struct dvb_frontend *fe)
  79. {
  80. struct zl10353_state *state = fe->demodulator_priv;
  81. char buf[52], buf2[4];
  82. int ret;
  83. u8 reg;
  84. /* Dump all registers. */
  85. for (reg = 0; ; reg++) {
  86. if (reg % 16 == 0) {
  87. if (reg)
  88. printk(KERN_DEBUG "%s\n", buf);
  89. sprintf(buf, "%02x: ", reg);
  90. }
  91. ret = zl10353_read_register(state, reg);
  92. if (ret >= 0)
  93. sprintf(buf2, "%02x ", (u8)ret);
  94. else
  95. strcpy(buf2, "-- ");
  96. strcat(buf, buf2);
  97. if (reg == 0xff)
  98. break;
  99. }
  100. printk(KERN_DEBUG "%s\n", buf);
  101. }
  102. static int zl10353_sleep(struct dvb_frontend *fe)
  103. {
  104. static u8 zl10353_softdown[] = { 0x50, 0x0C, 0x44 };
  105. zl10353_write(fe, zl10353_softdown, sizeof(zl10353_softdown));
  106. return 0;
  107. }
  108. static int zl10353_set_parameters(struct dvb_frontend *fe,
  109. struct dvb_frontend_parameters *param)
  110. {
  111. struct zl10353_state *state = fe->demodulator_priv;
  112. u8 pllbuf[6] = { 0x67 };
  113. /* These settings set "auto-everything" and start the FSM. */
  114. zl10353_single_write(fe, 0x55, 0x80);
  115. udelay(200);
  116. zl10353_single_write(fe, 0xEA, 0x01);
  117. udelay(200);
  118. zl10353_single_write(fe, 0xEA, 0x00);
  119. zl10353_single_write(fe, 0x56, 0x28);
  120. zl10353_single_write(fe, 0x89, 0x20);
  121. zl10353_single_write(fe, 0x5E, 0x00);
  122. zl10353_single_write(fe, 0x65, 0x5A);
  123. zl10353_single_write(fe, 0x66, 0xE9);
  124. zl10353_single_write(fe, 0x62, 0x0A);
  125. state->config.pll_set(fe, param, pllbuf + 1);
  126. zl10353_write(fe, pllbuf, sizeof(pllbuf));
  127. zl10353_single_write(fe, 0x70, 0x01);
  128. udelay(250);
  129. zl10353_single_write(fe, 0xE4, 0x00);
  130. zl10353_single_write(fe, 0xE5, 0x2A);
  131. zl10353_single_write(fe, 0xE9, 0x02);
  132. zl10353_single_write(fe, 0xE7, 0x40);
  133. zl10353_single_write(fe, 0xE8, 0x10);
  134. return 0;
  135. }
  136. static int zl10353_read_status(struct dvb_frontend *fe, fe_status_t *status)
  137. {
  138. struct zl10353_state *state = fe->demodulator_priv;
  139. int s6, s7, s8;
  140. if ((s6 = zl10353_read_register(state, STATUS_6)) < 0)
  141. return -EREMOTEIO;
  142. if ((s7 = zl10353_read_register(state, STATUS_7)) < 0)
  143. return -EREMOTEIO;
  144. if ((s8 = zl10353_read_register(state, STATUS_8)) < 0)
  145. return -EREMOTEIO;
  146. *status = 0;
  147. if (s6 & (1 << 2))
  148. *status |= FE_HAS_CARRIER;
  149. if (s6 & (1 << 1))
  150. *status |= FE_HAS_VITERBI;
  151. if (s6 & (1 << 5))
  152. *status |= FE_HAS_LOCK;
  153. if (s7 & (1 << 4))
  154. *status |= FE_HAS_SYNC;
  155. if (s8 & (1 << 6))
  156. *status |= FE_HAS_SIGNAL;
  157. if ((*status & (FE_HAS_CARRIER | FE_HAS_VITERBI | FE_HAS_SYNC)) !=
  158. (FE_HAS_CARRIER | FE_HAS_VITERBI | FE_HAS_SYNC))
  159. *status &= ~FE_HAS_LOCK;
  160. return 0;
  161. }
  162. static int zl10353_read_snr(struct dvb_frontend *fe, u16 *snr)
  163. {
  164. struct zl10353_state *state = fe->demodulator_priv;
  165. u8 _snr;
  166. if (debug_regs)
  167. zl10353_dump_regs(fe);
  168. _snr = zl10353_read_register(state, SNR);
  169. *snr = (_snr << 8) | _snr;
  170. return 0;
  171. }
  172. static int zl10353_get_tune_settings(struct dvb_frontend *fe,
  173. struct dvb_frontend_tune_settings
  174. *fe_tune_settings)
  175. {
  176. fe_tune_settings->min_delay_ms = 1000;
  177. fe_tune_settings->step_size = 0;
  178. fe_tune_settings->max_drift = 0;
  179. return 0;
  180. }
  181. static int zl10353_init(struct dvb_frontend *fe)
  182. {
  183. struct zl10353_state *state = fe->demodulator_priv;
  184. u8 zl10353_reset_attach[6] = { 0x50, 0x03, 0x64, 0x46, 0x15, 0x0F };
  185. int rc = 0;
  186. if (debug_regs)
  187. zl10353_dump_regs(fe);
  188. /* Do a "hard" reset if not already done */
  189. if (zl10353_read_register(state, 0x50) != 0x03) {
  190. rc = zl10353_write(fe, zl10353_reset_attach,
  191. sizeof(zl10353_reset_attach));
  192. if (debug_regs)
  193. zl10353_dump_regs(fe);
  194. }
  195. return 0;
  196. }
  197. static void zl10353_release(struct dvb_frontend *fe)
  198. {
  199. struct zl10353_state *state = fe->demodulator_priv;
  200. kfree(state);
  201. }
  202. static struct dvb_frontend_ops zl10353_ops;
  203. struct dvb_frontend *zl10353_attach(const struct zl10353_config *config,
  204. struct i2c_adapter *i2c)
  205. {
  206. struct zl10353_state *state = NULL;
  207. /* allocate memory for the internal state */
  208. state = kzalloc(sizeof(struct zl10353_state), GFP_KERNEL);
  209. if (state == NULL)
  210. goto error;
  211. /* setup the state */
  212. state->i2c = i2c;
  213. memcpy(&state->config, config, sizeof(struct zl10353_config));
  214. memcpy(&state->ops, &zl10353_ops, sizeof(struct dvb_frontend_ops));
  215. /* check if the demod is there */
  216. if (zl10353_read_register(state, CHIP_ID) != ID_ZL10353)
  217. goto error;
  218. /* create dvb_frontend */
  219. state->frontend.ops = &state->ops;
  220. state->frontend.demodulator_priv = state;
  221. return &state->frontend;
  222. error:
  223. kfree(state);
  224. return NULL;
  225. }
  226. static struct dvb_frontend_ops zl10353_ops = {
  227. .info = {
  228. .name = "Zarlink ZL10353 DVB-T",
  229. .type = FE_OFDM,
  230. .frequency_min = 174000000,
  231. .frequency_max = 862000000,
  232. .frequency_stepsize = 166667,
  233. .frequency_tolerance = 0,
  234. .caps = FE_CAN_FEC_1_2 | FE_CAN_FEC_2_3 |
  235. FE_CAN_FEC_3_4 | FE_CAN_FEC_5_6 | FE_CAN_FEC_7_8 |
  236. FE_CAN_FEC_AUTO |
  237. FE_CAN_QPSK | FE_CAN_QAM_16 | FE_CAN_QAM_64 | FE_CAN_QAM_AUTO |
  238. FE_CAN_TRANSMISSION_MODE_AUTO | FE_CAN_GUARD_INTERVAL_AUTO |
  239. FE_CAN_HIERARCHY_AUTO | FE_CAN_RECOVER |
  240. FE_CAN_MUTE_TS
  241. },
  242. .release = zl10353_release,
  243. .init = zl10353_init,
  244. .sleep = zl10353_sleep,
  245. .set_frontend = zl10353_set_parameters,
  246. .get_tune_settings = zl10353_get_tune_settings,
  247. .read_status = zl10353_read_status,
  248. .read_snr = zl10353_read_snr,
  249. };
  250. module_param(debug_regs, int, 0644);
  251. MODULE_PARM_DESC(debug_regs, "Turn on/off frontend register dumps (default:off).");
  252. MODULE_DESCRIPTION("Zarlink ZL10353 DVB-T demodulator driver");
  253. MODULE_AUTHOR("Chris Pascoe");
  254. MODULE_LICENSE("GPL");
  255. EXPORT_SYMBOL(zl10353_attach);
  256. EXPORT_SYMBOL(zl10353_write);