PageRenderTime 28ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/release/src-rt-6.x/linux/linux-2.6/drivers/media/dvb/frontends/zl10353.c

https://gitlab.com/envieidoc/advancedtomato2
C | 429 lines | 318 code | 78 blank | 33 comment | 44 complexity | 27388ea3ae250f4b64aa663b0900c392 MD5 | raw file
  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;
  37. #define dprintk(args...) \
  38. do { \
  39. if (debug) printk(KERN_DEBUG "zl10353: " args); \
  40. } while (0)
  41. static int debug_regs = 0;
  42. static int zl10353_single_write(struct dvb_frontend *fe, u8 reg, u8 val)
  43. {
  44. struct zl10353_state *state = fe->demodulator_priv;
  45. u8 buf[2] = { reg, val };
  46. struct i2c_msg msg = { .addr = state->config.demod_address, .flags = 0,
  47. .buf = buf, .len = 2 };
  48. int err = i2c_transfer(state->i2c, &msg, 1);
  49. if (err != 1) {
  50. printk("zl10353: write to reg %x failed (err = %d)!\n", reg, err);
  51. return err;
  52. }
  53. return 0;
  54. }
  55. static int zl10353_write(struct dvb_frontend *fe, u8 *ibuf, int ilen)
  56. {
  57. int err, i;
  58. for (i = 0; i < ilen - 1; i++)
  59. if ((err = zl10353_single_write(fe, ibuf[0] + i, ibuf[i + 1])))
  60. return err;
  61. return 0;
  62. }
  63. static int zl10353_read_register(struct zl10353_state *state, u8 reg)
  64. {
  65. int ret;
  66. u8 b0[1] = { reg };
  67. u8 b1[1] = { 0 };
  68. struct i2c_msg msg[2] = { { .addr = state->config.demod_address,
  69. .flags = 0,
  70. .buf = b0, .len = 1 },
  71. { .addr = state->config.demod_address,
  72. .flags = I2C_M_RD,
  73. .buf = b1, .len = 1 } };
  74. ret = i2c_transfer(state->i2c, msg, 2);
  75. if (ret != 2) {
  76. printk("%s: readreg error (reg=%d, ret==%i)\n",
  77. __FUNCTION__, reg, ret);
  78. return ret;
  79. }
  80. return b1[0];
  81. }
  82. static void zl10353_dump_regs(struct dvb_frontend *fe)
  83. {
  84. struct zl10353_state *state = fe->demodulator_priv;
  85. char buf[52], buf2[4];
  86. int ret;
  87. u8 reg;
  88. /* Dump all registers. */
  89. for (reg = 0; ; reg++) {
  90. if (reg % 16 == 0) {
  91. if (reg)
  92. printk(KERN_DEBUG "%s\n", buf);
  93. sprintf(buf, "%02x: ", reg);
  94. }
  95. ret = zl10353_read_register(state, reg);
  96. if (ret >= 0)
  97. sprintf(buf2, "%02x ", (u8)ret);
  98. else
  99. strcpy(buf2, "-- ");
  100. strcat(buf, buf2);
  101. if (reg == 0xff)
  102. break;
  103. }
  104. printk(KERN_DEBUG "%s\n", buf);
  105. }
  106. static void zl10353_calc_nominal_rate(struct dvb_frontend *fe,
  107. enum fe_bandwidth bandwidth,
  108. u16 *nominal_rate)
  109. {
  110. u32 adc_clock = 22528; /* 20.480 MHz on the board(!?) */
  111. u8 bw;
  112. struct zl10353_state *state = fe->demodulator_priv;
  113. if (state->config.adc_clock)
  114. adc_clock = state->config.adc_clock;
  115. switch (bandwidth) {
  116. case BANDWIDTH_6_MHZ:
  117. bw = 6;
  118. break;
  119. case BANDWIDTH_7_MHZ:
  120. bw = 7;
  121. break;
  122. case BANDWIDTH_8_MHZ:
  123. default:
  124. bw = 8;
  125. break;
  126. }
  127. *nominal_rate = (64 * bw * (1<<16) / (7 * 8) * 4000 / adc_clock + 2) / 4;
  128. dprintk("%s: bw %d, adc_clock %d => 0x%x\n",
  129. __FUNCTION__, bw, adc_clock, *nominal_rate);
  130. }
  131. static int zl10353_sleep(struct dvb_frontend *fe)
  132. {
  133. static u8 zl10353_softdown[] = { 0x50, 0x0C, 0x44 };
  134. zl10353_write(fe, zl10353_softdown, sizeof(zl10353_softdown));
  135. return 0;
  136. }
  137. static int zl10353_set_parameters(struct dvb_frontend *fe,
  138. struct dvb_frontend_parameters *param)
  139. {
  140. struct zl10353_state *state = fe->demodulator_priv;
  141. u16 nominal_rate;
  142. u8 pllbuf[6] = { 0x67 };
  143. /* These settings set "auto-everything" and start the FSM. */
  144. zl10353_single_write(fe, 0x55, 0x80);
  145. udelay(200);
  146. zl10353_single_write(fe, 0xEA, 0x01);
  147. udelay(200);
  148. zl10353_single_write(fe, 0xEA, 0x00);
  149. zl10353_single_write(fe, 0x56, 0x28);
  150. zl10353_single_write(fe, 0x89, 0x20);
  151. zl10353_single_write(fe, 0x5E, 0x00);
  152. zl10353_calc_nominal_rate(fe, param->u.ofdm.bandwidth, &nominal_rate);
  153. zl10353_single_write(fe, TRL_NOMINAL_RATE_1, msb(nominal_rate));
  154. zl10353_single_write(fe, TRL_NOMINAL_RATE_0, lsb(nominal_rate));
  155. zl10353_single_write(fe, 0x6C, 0xCD);
  156. zl10353_single_write(fe, 0x6D, 0x7E);
  157. if (fe->ops.i2c_gate_ctrl)
  158. fe->ops.i2c_gate_ctrl(fe, 0);
  159. // if there is no attached secondary tuner, we call set_params to program
  160. // a potential tuner attached somewhere else
  161. if (state->config.no_tuner) {
  162. if (fe->ops.tuner_ops.set_params) {
  163. fe->ops.tuner_ops.set_params(fe, param);
  164. if (fe->ops.i2c_gate_ctrl)
  165. fe->ops.i2c_gate_ctrl(fe, 0);
  166. }
  167. }
  168. // if pllbuf is defined, retrieve the settings
  169. if (fe->ops.tuner_ops.calc_regs) {
  170. fe->ops.tuner_ops.calc_regs(fe, param, pllbuf+1, 5);
  171. pllbuf[1] <<= 1;
  172. } else {
  173. // fake pllbuf settings
  174. pllbuf[1] = 0x61 << 1;
  175. pllbuf[2] = 0;
  176. pllbuf[3] = 0;
  177. pllbuf[3] = 0;
  178. pllbuf[4] = 0;
  179. }
  180. // there is no call to _just_ start decoding, so we send the pllbuf anyway
  181. // even if there isn't a PLL attached to the secondary bus
  182. zl10353_write(fe, pllbuf, sizeof(pllbuf));
  183. zl10353_single_write(fe, 0x5F, 0x13);
  184. zl10353_single_write(fe, 0x70, 0x01);
  185. udelay(250);
  186. zl10353_single_write(fe, 0xE4, 0x00);
  187. zl10353_single_write(fe, 0xE5, 0x2A);
  188. zl10353_single_write(fe, 0xE9, 0x02);
  189. zl10353_single_write(fe, 0xE7, 0x40);
  190. zl10353_single_write(fe, 0xE8, 0x10);
  191. return 0;
  192. }
  193. static int zl10353_read_status(struct dvb_frontend *fe, fe_status_t *status)
  194. {
  195. struct zl10353_state *state = fe->demodulator_priv;
  196. int s6, s7, s8;
  197. if ((s6 = zl10353_read_register(state, STATUS_6)) < 0)
  198. return -EREMOTEIO;
  199. if ((s7 = zl10353_read_register(state, STATUS_7)) < 0)
  200. return -EREMOTEIO;
  201. if ((s8 = zl10353_read_register(state, STATUS_8)) < 0)
  202. return -EREMOTEIO;
  203. *status = 0;
  204. if (s6 & (1 << 2))
  205. *status |= FE_HAS_CARRIER;
  206. if (s6 & (1 << 1))
  207. *status |= FE_HAS_VITERBI;
  208. if (s6 & (1 << 5))
  209. *status |= FE_HAS_LOCK;
  210. if (s7 & (1 << 4))
  211. *status |= FE_HAS_SYNC;
  212. if (s8 & (1 << 6))
  213. *status |= FE_HAS_SIGNAL;
  214. if ((*status & (FE_HAS_CARRIER | FE_HAS_VITERBI | FE_HAS_SYNC)) !=
  215. (FE_HAS_CARRIER | FE_HAS_VITERBI | FE_HAS_SYNC))
  216. *status &= ~FE_HAS_LOCK;
  217. return 0;
  218. }
  219. static int zl10353_read_ber(struct dvb_frontend *fe, u32 *ber)
  220. {
  221. struct zl10353_state *state = fe->demodulator_priv;
  222. *ber = zl10353_read_register(state, RS_ERR_CNT_2) << 16 |
  223. zl10353_read_register(state, RS_ERR_CNT_1) << 8 |
  224. zl10353_read_register(state, RS_ERR_CNT_0);
  225. return 0;
  226. }
  227. static int zl10353_read_signal_strength(struct dvb_frontend *fe, u16 *strength)
  228. {
  229. struct zl10353_state *state = fe->demodulator_priv;
  230. u16 signal = zl10353_read_register(state, AGC_GAIN_1) << 10 |
  231. zl10353_read_register(state, AGC_GAIN_0) << 2 | 3;
  232. *strength = ~signal;
  233. return 0;
  234. }
  235. static int zl10353_read_snr(struct dvb_frontend *fe, u16 *snr)
  236. {
  237. struct zl10353_state *state = fe->demodulator_priv;
  238. u8 _snr;
  239. if (debug_regs)
  240. zl10353_dump_regs(fe);
  241. _snr = zl10353_read_register(state, SNR);
  242. *snr = (_snr << 8) | _snr;
  243. return 0;
  244. }
  245. static int zl10353_read_ucblocks(struct dvb_frontend *fe, u32 *ucblocks)
  246. {
  247. struct zl10353_state *state = fe->demodulator_priv;
  248. *ucblocks = zl10353_read_register(state, RS_UBC_1) << 8 |
  249. zl10353_read_register(state, RS_UBC_0);
  250. return 0;
  251. }
  252. static int zl10353_get_tune_settings(struct dvb_frontend *fe,
  253. struct dvb_frontend_tune_settings
  254. *fe_tune_settings)
  255. {
  256. fe_tune_settings->min_delay_ms = 1000;
  257. fe_tune_settings->step_size = 0;
  258. fe_tune_settings->max_drift = 0;
  259. return 0;
  260. }
  261. static int zl10353_init(struct dvb_frontend *fe)
  262. {
  263. struct zl10353_state *state = fe->demodulator_priv;
  264. u8 zl10353_reset_attach[6] = { 0x50, 0x03, 0x64, 0x46, 0x15, 0x0F };
  265. int rc = 0;
  266. if (debug_regs)
  267. zl10353_dump_regs(fe);
  268. if (state->config.parallel_ts)
  269. zl10353_reset_attach[2] &= ~0x20;
  270. /* Do a "hard" reset if not already done */
  271. if (zl10353_read_register(state, 0x50) != zl10353_reset_attach[1] ||
  272. zl10353_read_register(state, 0x51) != zl10353_reset_attach[2]) {
  273. rc = zl10353_write(fe, zl10353_reset_attach,
  274. sizeof(zl10353_reset_attach));
  275. if (debug_regs)
  276. zl10353_dump_regs(fe);
  277. }
  278. return 0;
  279. }
  280. static int zl10353_i2c_gate_ctrl(struct dvb_frontend* fe, int enable)
  281. {
  282. u8 val = 0x0a;
  283. if (enable)
  284. val |= 0x10;
  285. return zl10353_single_write(fe, 0x62, val);
  286. }
  287. static void zl10353_release(struct dvb_frontend *fe)
  288. {
  289. struct zl10353_state *state = fe->demodulator_priv;
  290. kfree(state);
  291. }
  292. static struct dvb_frontend_ops zl10353_ops;
  293. struct dvb_frontend *zl10353_attach(const struct zl10353_config *config,
  294. struct i2c_adapter *i2c)
  295. {
  296. struct zl10353_state *state = NULL;
  297. /* allocate memory for the internal state */
  298. state = kzalloc(sizeof(struct zl10353_state), GFP_KERNEL);
  299. if (state == NULL)
  300. goto error;
  301. /* setup the state */
  302. state->i2c = i2c;
  303. memcpy(&state->config, config, sizeof(struct zl10353_config));
  304. /* check if the demod is there */
  305. if (zl10353_read_register(state, CHIP_ID) != ID_ZL10353)
  306. goto error;
  307. /* create dvb_frontend */
  308. memcpy(&state->frontend.ops, &zl10353_ops, sizeof(struct dvb_frontend_ops));
  309. state->frontend.demodulator_priv = state;
  310. return &state->frontend;
  311. error:
  312. kfree(state);
  313. return NULL;
  314. }
  315. static struct dvb_frontend_ops zl10353_ops = {
  316. .info = {
  317. .name = "Zarlink ZL10353 DVB-T",
  318. .type = FE_OFDM,
  319. .frequency_min = 174000000,
  320. .frequency_max = 862000000,
  321. .frequency_stepsize = 166667,
  322. .frequency_tolerance = 0,
  323. .caps = FE_CAN_FEC_1_2 | FE_CAN_FEC_2_3 |
  324. FE_CAN_FEC_3_4 | FE_CAN_FEC_5_6 | FE_CAN_FEC_7_8 |
  325. FE_CAN_FEC_AUTO |
  326. FE_CAN_QPSK | FE_CAN_QAM_16 | FE_CAN_QAM_64 | FE_CAN_QAM_AUTO |
  327. FE_CAN_TRANSMISSION_MODE_AUTO | FE_CAN_GUARD_INTERVAL_AUTO |
  328. FE_CAN_HIERARCHY_AUTO | FE_CAN_RECOVER |
  329. FE_CAN_MUTE_TS
  330. },
  331. .release = zl10353_release,
  332. .init = zl10353_init,
  333. .sleep = zl10353_sleep,
  334. .i2c_gate_ctrl = zl10353_i2c_gate_ctrl,
  335. .write = zl10353_write,
  336. .set_frontend = zl10353_set_parameters,
  337. .get_tune_settings = zl10353_get_tune_settings,
  338. .read_status = zl10353_read_status,
  339. .read_ber = zl10353_read_ber,
  340. .read_signal_strength = zl10353_read_signal_strength,
  341. .read_snr = zl10353_read_snr,
  342. .read_ucblocks = zl10353_read_ucblocks,
  343. };
  344. module_param(debug, int, 0644);
  345. MODULE_PARM_DESC(debug, "Turn on/off frontend debugging (default:off).");
  346. module_param(debug_regs, int, 0644);
  347. MODULE_PARM_DESC(debug_regs, "Turn on/off frontend register dumps (default:off).");
  348. MODULE_DESCRIPTION("Zarlink ZL10353 DVB-T demodulator driver");
  349. MODULE_AUTHOR("Chris Pascoe");
  350. MODULE_LICENSE("GPL");
  351. EXPORT_SYMBOL(zl10353_attach);