stv6110.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453
  1. /*
  2. * stv6110.c
  3. *
  4. * Driver for ST STV6110 satellite tuner IC.
  5. *
  6. * Copyright (C) 2009 NetUP Inc.
  7. * Copyright (C) 2009 Igor M. Liplianin <liplianin@netup.ru>
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 2 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. *
  18. * GNU General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with this program; if not, write to the Free Software
  22. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  23. */
  24. #include <linux/slab.h>
  25. #include <linux/module.h>
  26. #include <linux/dvb/frontend.h>
  27. #include <linux/types.h>
  28. #include "stv6110.h"
  29. static int debug;
  30. struct stv6110_priv {
  31. int i2c_address;
  32. struct i2c_adapter *i2c;
  33. u32 mclk;
  34. u8 clk_div;
  35. u8 gain;
  36. u8 regs[8];
  37. };
  38. #define dprintk(args...) \
  39. do { \
  40. if (debug) \
  41. printk(KERN_DEBUG args); \
  42. } while (0)
  43. static s32 abssub(s32 a, s32 b)
  44. {
  45. if (a > b)
  46. return a - b;
  47. else
  48. return b - a;
  49. };
  50. static int stv6110_release(struct dvb_frontend *fe)
  51. {
  52. kfree(fe->tuner_priv);
  53. fe->tuner_priv = NULL;
  54. return 0;
  55. }
  56. static int stv6110_write_regs(struct dvb_frontend *fe, u8 buf[],
  57. int start, int len)
  58. {
  59. struct stv6110_priv *priv = fe->tuner_priv;
  60. int rc;
  61. u8 cmdbuf[len + 1];
  62. struct i2c_msg msg = {
  63. .addr = priv->i2c_address,
  64. .flags = 0,
  65. .buf = cmdbuf,
  66. .len = len + 1
  67. };
  68. dprintk("%s\n", __func__);
  69. if (start + len > 8)
  70. return -EINVAL;
  71. memcpy(&cmdbuf[1], buf, len);
  72. cmdbuf[0] = start;
  73. if (fe->ops.i2c_gate_ctrl)
  74. fe->ops.i2c_gate_ctrl(fe, 1);
  75. rc = i2c_transfer(priv->i2c, &msg, 1);
  76. if (rc != 1)
  77. dprintk("%s: i2c error\n", __func__);
  78. if (fe->ops.i2c_gate_ctrl)
  79. fe->ops.i2c_gate_ctrl(fe, 0);
  80. return 0;
  81. }
  82. static int stv6110_read_regs(struct dvb_frontend *fe, u8 regs[],
  83. int start, int len)
  84. {
  85. struct stv6110_priv *priv = fe->tuner_priv;
  86. int rc;
  87. u8 reg[] = { start };
  88. struct i2c_msg msg[] = {
  89. {
  90. .addr = priv->i2c_address,
  91. .flags = 0,
  92. .buf = reg,
  93. .len = 1,
  94. }, {
  95. .addr = priv->i2c_address,
  96. .flags = I2C_M_RD,
  97. .buf = regs,
  98. .len = len,
  99. },
  100. };
  101. if (fe->ops.i2c_gate_ctrl)
  102. fe->ops.i2c_gate_ctrl(fe, 1);
  103. rc = i2c_transfer(priv->i2c, msg, 2);
  104. if (rc != 2)
  105. dprintk("%s: i2c error\n", __func__);
  106. if (fe->ops.i2c_gate_ctrl)
  107. fe->ops.i2c_gate_ctrl(fe, 0);
  108. memcpy(&priv->regs[start], regs, len);
  109. return 0;
  110. }
  111. static int stv6110_read_reg(struct dvb_frontend *fe, int start)
  112. {
  113. u8 buf[] = { 0 };
  114. stv6110_read_regs(fe, buf, start, 1);
  115. return buf[0];
  116. }
  117. static int stv6110_sleep(struct dvb_frontend *fe)
  118. {
  119. u8 reg[] = { 0 };
  120. stv6110_write_regs(fe, reg, 0, 1);
  121. return 0;
  122. }
  123. static u32 carrier_width(u32 symbol_rate, fe_rolloff_t rolloff)
  124. {
  125. u32 rlf;
  126. switch (rolloff) {
  127. case ROLLOFF_20:
  128. rlf = 20;
  129. break;
  130. case ROLLOFF_25:
  131. rlf = 25;
  132. break;
  133. default:
  134. rlf = 35;
  135. break;
  136. }
  137. return symbol_rate + ((symbol_rate * rlf) / 100);
  138. }
  139. static int stv6110_set_bandwidth(struct dvb_frontend *fe, u32 bandwidth)
  140. {
  141. struct stv6110_priv *priv = fe->tuner_priv;
  142. u8 r8, ret = 0x04;
  143. int i;
  144. if ((bandwidth / 2) > 36000000) /*BW/2 max=31+5=36 mhz for r8=31*/
  145. r8 = 31;
  146. else if ((bandwidth / 2) < 5000000) /* BW/2 min=5Mhz for F=0 */
  147. r8 = 0;
  148. else /*if 5 < BW/2 < 36*/
  149. r8 = (bandwidth / 2) / 1000000 - 5;
  150. /* ctrl3, RCCLKOFF = 0 Activate the calibration Clock */
  151. /* ctrl3, CF = r8 Set the LPF value */
  152. priv->regs[RSTV6110_CTRL3] &= ~((1 << 6) | 0x1f);
  153. priv->regs[RSTV6110_CTRL3] |= (r8 & 0x1f);
  154. stv6110_write_regs(fe, &priv->regs[RSTV6110_CTRL3], RSTV6110_CTRL3, 1);
  155. /* stat1, CALRCSTRT = 1 Start LPF auto calibration*/
  156. priv->regs[RSTV6110_STAT1] |= 0x02;
  157. stv6110_write_regs(fe, &priv->regs[RSTV6110_STAT1], RSTV6110_STAT1, 1);
  158. i = 0;
  159. /* Wait for CALRCSTRT == 0 */
  160. while ((i < 10) && (ret != 0)) {
  161. ret = ((stv6110_read_reg(fe, RSTV6110_STAT1)) & 0x02);
  162. mdelay(1); /* wait for LPF auto calibration */
  163. i++;
  164. }
  165. /* RCCLKOFF = 1 calibration done, desactivate the calibration Clock */
  166. priv->regs[RSTV6110_CTRL3] |= (1 << 6);
  167. stv6110_write_regs(fe, &priv->regs[RSTV6110_CTRL3], RSTV6110_CTRL3, 1);
  168. return 0;
  169. }
  170. static int stv6110_init(struct dvb_frontend *fe)
  171. {
  172. struct stv6110_priv *priv = fe->tuner_priv;
  173. u8 buf0[] = { 0x07, 0x11, 0xdc, 0x85, 0x17, 0x01, 0xe6, 0x1e };
  174. memcpy(priv->regs, buf0, 8);
  175. /* K = (Reference / 1000000) - 16 */
  176. priv->regs[RSTV6110_CTRL1] &= ~(0x1f << 3);
  177. priv->regs[RSTV6110_CTRL1] |=
  178. ((((priv->mclk / 1000000) - 16) & 0x1f) << 3);
  179. /* divisor value for the output clock */
  180. priv->regs[RSTV6110_CTRL2] &= ~0xc0;
  181. priv->regs[RSTV6110_CTRL2] |= (priv->clk_div << 6);
  182. stv6110_write_regs(fe, &priv->regs[RSTV6110_CTRL1], RSTV6110_CTRL1, 8);
  183. msleep(1);
  184. stv6110_set_bandwidth(fe, 72000000);
  185. return 0;
  186. }
  187. static int stv6110_get_frequency(struct dvb_frontend *fe, u32 *frequency)
  188. {
  189. struct stv6110_priv *priv = fe->tuner_priv;
  190. u32 nbsteps, divider, psd2, freq;
  191. u8 regs[] = { 0, 0, 0, 0, 0, 0, 0, 0 };
  192. stv6110_read_regs(fe, regs, 0, 8);
  193. /*N*/
  194. divider = (priv->regs[RSTV6110_TUNING2] & 0x0f) << 8;
  195. divider += priv->regs[RSTV6110_TUNING1];
  196. /*R*/
  197. nbsteps = (priv->regs[RSTV6110_TUNING2] >> 6) & 3;
  198. /*p*/
  199. psd2 = (priv->regs[RSTV6110_TUNING2] >> 4) & 1;
  200. freq = divider * (priv->mclk / 1000);
  201. freq /= (1 << (nbsteps + psd2));
  202. freq /= 4;
  203. *frequency = freq;
  204. return 0;
  205. }
  206. static int stv6110_set_frequency(struct dvb_frontend *fe, u32 frequency)
  207. {
  208. struct stv6110_priv *priv = fe->tuner_priv;
  209. struct dtv_frontend_properties *c = &fe->dtv_property_cache;
  210. u8 ret = 0x04;
  211. u32 divider, ref, p, presc, i, result_freq, vco_freq;
  212. s32 p_calc, p_calc_opt = 1000, r_div, r_div_opt = 0, p_val;
  213. s32 srate;
  214. dprintk("%s, freq=%d kHz, mclk=%d Hz\n", __func__,
  215. frequency, priv->mclk);
  216. /* K = (Reference / 1000000) - 16 */
  217. priv->regs[RSTV6110_CTRL1] &= ~(0x1f << 3);
  218. priv->regs[RSTV6110_CTRL1] |=
  219. ((((priv->mclk / 1000000) - 16) & 0x1f) << 3);
  220. /* BB_GAIN = db/2 */
  221. if (fe->ops.set_property && fe->ops.get_property) {
  222. srate = c->symbol_rate;
  223. dprintk("%s: Get Frontend parameters: srate=%d\n",
  224. __func__, srate);
  225. } else
  226. srate = 15000000;
  227. priv->regs[RSTV6110_CTRL2] &= ~0x0f;
  228. priv->regs[RSTV6110_CTRL2] |= (priv->gain & 0x0f);
  229. if (frequency <= 1023000) {
  230. p = 1;
  231. presc = 0;
  232. } else if (frequency <= 1300000) {
  233. p = 1;
  234. presc = 1;
  235. } else if (frequency <= 2046000) {
  236. p = 0;
  237. presc = 0;
  238. } else {
  239. p = 0;
  240. presc = 1;
  241. }
  242. /* DIV4SEL = p*/
  243. priv->regs[RSTV6110_TUNING2] &= ~(1 << 4);
  244. priv->regs[RSTV6110_TUNING2] |= (p << 4);
  245. /* PRESC32ON = presc */
  246. priv->regs[RSTV6110_TUNING2] &= ~(1 << 5);
  247. priv->regs[RSTV6110_TUNING2] |= (presc << 5);
  248. p_val = (int)(1 << (p + 1)) * 10;/* P = 2 or P = 4 */
  249. for (r_div = 0; r_div <= 3; r_div++) {
  250. p_calc = (priv->mclk / 100000);
  251. p_calc /= (1 << (r_div + 1));
  252. if ((abssub(p_calc, p_val)) < (abssub(p_calc_opt, p_val)))
  253. r_div_opt = r_div;
  254. p_calc_opt = (priv->mclk / 100000);
  255. p_calc_opt /= (1 << (r_div_opt + 1));
  256. }
  257. ref = priv->mclk / ((1 << (r_div_opt + 1)) * (1 << (p + 1)));
  258. divider = (((frequency * 1000) + (ref >> 1)) / ref);
  259. /* RDIV = r_div_opt */
  260. priv->regs[RSTV6110_TUNING2] &= ~(3 << 6);
  261. priv->regs[RSTV6110_TUNING2] |= (((r_div_opt) & 3) << 6);
  262. /* NDIV_MSB = MSB(divider) */
  263. priv->regs[RSTV6110_TUNING2] &= ~0x0f;
  264. priv->regs[RSTV6110_TUNING2] |= (((divider) >> 8) & 0x0f);
  265. /* NDIV_LSB, LSB(divider) */
  266. priv->regs[RSTV6110_TUNING1] = (divider & 0xff);
  267. /* CALVCOSTRT = 1 VCO Auto Calibration */
  268. priv->regs[RSTV6110_STAT1] |= 0x04;
  269. stv6110_write_regs(fe, &priv->regs[RSTV6110_CTRL1],
  270. RSTV6110_CTRL1, 8);
  271. i = 0;
  272. /* Wait for CALVCOSTRT == 0 */
  273. while ((i < 10) && (ret != 0)) {
  274. ret = ((stv6110_read_reg(fe, RSTV6110_STAT1)) & 0x04);
  275. msleep(1); /* wait for VCO auto calibration */
  276. i++;
  277. }
  278. ret = stv6110_read_reg(fe, RSTV6110_STAT1);
  279. stv6110_get_frequency(fe, &result_freq);
  280. vco_freq = divider * ((priv->mclk / 1000) / ((1 << (r_div_opt + 1))));
  281. dprintk("%s, stat1=%x, lo_freq=%d kHz, vco_frec=%d kHz\n", __func__,
  282. ret, result_freq, vco_freq);
  283. return 0;
  284. }
  285. static int stv6110_set_params(struct dvb_frontend *fe,
  286. struct dvb_frontend_parameters *params)
  287. {
  288. struct dtv_frontend_properties *c = &fe->dtv_property_cache;
  289. u32 bandwidth = carrier_width(c->symbol_rate, c->rolloff);
  290. stv6110_set_frequency(fe, c->frequency);
  291. stv6110_set_bandwidth(fe, bandwidth);
  292. return 0;
  293. }
  294. static int stv6110_get_bandwidth(struct dvb_frontend *fe, u32 *bandwidth)
  295. {
  296. struct stv6110_priv *priv = fe->tuner_priv;
  297. u8 r8 = 0;
  298. u8 regs[] = { 0, 0, 0, 0, 0, 0, 0, 0 };
  299. stv6110_read_regs(fe, regs, 0, 8);
  300. /* CF */
  301. r8 = priv->regs[RSTV6110_CTRL3] & 0x1f;
  302. *bandwidth = (r8 + 5) * 2000000;/* x2 for ZIF tuner BW/2 = F+5 Mhz */
  303. return 0;
  304. }
  305. static struct dvb_tuner_ops stv6110_tuner_ops = {
  306. .info = {
  307. .name = "ST STV6110",
  308. .frequency_min = 950000,
  309. .frequency_max = 2150000,
  310. .frequency_step = 1000,
  311. },
  312. .init = stv6110_init,
  313. .release = stv6110_release,
  314. .sleep = stv6110_sleep,
  315. .set_params = stv6110_set_params,
  316. .get_frequency = stv6110_get_frequency,
  317. .set_frequency = stv6110_set_frequency,
  318. .get_bandwidth = stv6110_get_bandwidth,
  319. .set_bandwidth = stv6110_set_bandwidth,
  320. };
  321. struct dvb_frontend *stv6110_attach(struct dvb_frontend *fe,
  322. const struct stv6110_config *config,
  323. struct i2c_adapter *i2c)
  324. {
  325. struct stv6110_priv *priv = NULL;
  326. u8 reg0[] = { 0x00, 0x07, 0x11, 0xdc, 0x85, 0x17, 0x01, 0xe6, 0x1e };
  327. struct i2c_msg msg[] = {
  328. {
  329. .addr = config->i2c_address,
  330. .flags = 0,
  331. .buf = reg0,
  332. .len = 9
  333. }
  334. };
  335. int ret;
  336. /* divisor value for the output clock */
  337. reg0[2] &= ~0xc0;
  338. reg0[2] |= (config->clk_div << 6);
  339. if (fe->ops.i2c_gate_ctrl)
  340. fe->ops.i2c_gate_ctrl(fe, 1);
  341. ret = i2c_transfer(i2c, msg, 1);
  342. if (fe->ops.i2c_gate_ctrl)
  343. fe->ops.i2c_gate_ctrl(fe, 0);
  344. if (ret != 1)
  345. return NULL;
  346. priv = kzalloc(sizeof(struct stv6110_priv), GFP_KERNEL);
  347. if (priv == NULL)
  348. return NULL;
  349. priv->i2c_address = config->i2c_address;
  350. priv->i2c = i2c;
  351. priv->mclk = config->mclk;
  352. priv->clk_div = config->clk_div;
  353. priv->gain = config->gain;
  354. memcpy(&priv->regs, &reg0[1], 8);
  355. memcpy(&fe->ops.tuner_ops, &stv6110_tuner_ops,
  356. sizeof(struct dvb_tuner_ops));
  357. fe->tuner_priv = priv;
  358. printk(KERN_INFO "STV6110 attached on addr=%x!\n", priv->i2c_address);
  359. return fe;
  360. }
  361. EXPORT_SYMBOL(stv6110_attach);
  362. module_param(debug, int, 0644);
  363. MODULE_PARM_DESC(debug, "Turn on/off frontend debugging (default:off).");
  364. MODULE_DESCRIPTION("ST STV6110 driver");
  365. MODULE_AUTHOR("Igor M. Liplianin");
  366. MODULE_LICENSE("GPL");