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