stv6110.c 11 KB

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