stv6110.c 11 KB

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