stv6110.c 11 KB

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