stv6110.c 9.9 KB

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