ts2020.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  1. /*
  2. Montage Technology TS2020 - Silicon Tuner driver
  3. Copyright (C) 2009-2012 Konstantin Dimitrov <kosio.dimitrov@gmail.com>
  4. Copyright (C) 2009-2012 TurboSight.com
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  16. */
  17. #include "dvb_frontend.h"
  18. #include "ts2020.h"
  19. #define TS2020_XTAL_FREQ 27000 /* in kHz */
  20. #define FREQ_OFFSET_LOW_SYM_RATE 3000
  21. struct ts2020_priv {
  22. /* i2c details */
  23. int i2c_address;
  24. struct i2c_adapter *i2c;
  25. u8 clk_out_div;
  26. u32 frequency;
  27. u32 frequency_div;
  28. };
  29. static int ts2020_release(struct dvb_frontend *fe)
  30. {
  31. kfree(fe->tuner_priv);
  32. fe->tuner_priv = NULL;
  33. return 0;
  34. }
  35. static int ts2020_writereg(struct dvb_frontend *fe, int reg, int data)
  36. {
  37. struct ts2020_priv *priv = fe->tuner_priv;
  38. u8 buf[] = { reg, data };
  39. struct i2c_msg msg[] = {
  40. {
  41. .addr = priv->i2c_address,
  42. .flags = 0,
  43. .buf = buf,
  44. .len = 2
  45. }
  46. };
  47. int err;
  48. if (fe->ops.i2c_gate_ctrl)
  49. fe->ops.i2c_gate_ctrl(fe, 1);
  50. err = i2c_transfer(priv->i2c, msg, 1);
  51. if (err != 1) {
  52. printk(KERN_ERR
  53. "%s: writereg error(err == %i, reg == 0x%02x, value == 0x%02x)\n",
  54. __func__, err, reg, data);
  55. return -EREMOTEIO;
  56. }
  57. if (fe->ops.i2c_gate_ctrl)
  58. fe->ops.i2c_gate_ctrl(fe, 0);
  59. return 0;
  60. }
  61. static int ts2020_readreg(struct dvb_frontend *fe, u8 reg)
  62. {
  63. struct ts2020_priv *priv = fe->tuner_priv;
  64. int ret;
  65. u8 b0[] = { reg };
  66. u8 b1[] = { 0 };
  67. struct i2c_msg msg[] = {
  68. {
  69. .addr = priv->i2c_address,
  70. .flags = 0,
  71. .buf = b0,
  72. .len = 1
  73. }, {
  74. .addr = priv->i2c_address,
  75. .flags = I2C_M_RD,
  76. .buf = b1,
  77. .len = 1
  78. }
  79. };
  80. if (fe->ops.i2c_gate_ctrl)
  81. fe->ops.i2c_gate_ctrl(fe, 1);
  82. ret = i2c_transfer(priv->i2c, msg, 2);
  83. if (ret != 2) {
  84. printk(KERN_ERR "%s: reg=0x%x(error=%d)\n",
  85. __func__, reg, ret);
  86. return ret;
  87. }
  88. if (fe->ops.i2c_gate_ctrl)
  89. fe->ops.i2c_gate_ctrl(fe, 0);
  90. return b1[0];
  91. }
  92. static int ts2020_sleep(struct dvb_frontend *fe)
  93. {
  94. struct ts2020_priv *priv = fe->tuner_priv;
  95. int ret;
  96. u8 buf[] = { 10, 0 };
  97. struct i2c_msg msg = {
  98. .addr = priv->i2c_address,
  99. .flags = 0,
  100. .buf = buf,
  101. .len = 2
  102. };
  103. if (fe->ops.i2c_gate_ctrl)
  104. fe->ops.i2c_gate_ctrl(fe, 1);
  105. ret = i2c_transfer(priv->i2c, &msg, 1);
  106. if (ret != 1)
  107. printk(KERN_ERR "%s: i2c error\n", __func__);
  108. if (fe->ops.i2c_gate_ctrl)
  109. fe->ops.i2c_gate_ctrl(fe, 0);
  110. return (ret == 1) ? 0 : ret;
  111. }
  112. static int ts2020_init(struct dvb_frontend *fe)
  113. {
  114. struct ts2020_priv *priv = fe->tuner_priv;
  115. ts2020_writereg(fe, 0x42, 0x73);
  116. ts2020_writereg(fe, 0x05, priv->clk_out_div);
  117. ts2020_writereg(fe, 0x20, 0x27);
  118. ts2020_writereg(fe, 0x07, 0x02);
  119. ts2020_writereg(fe, 0x11, 0xff);
  120. ts2020_writereg(fe, 0x60, 0xf9);
  121. ts2020_writereg(fe, 0x08, 0x01);
  122. ts2020_writereg(fe, 0x00, 0x41);
  123. return 0;
  124. }
  125. static int ts2020_tuner_gate_ctrl(struct dvb_frontend *fe, u8 offset)
  126. {
  127. int ret;
  128. ret = ts2020_writereg(fe, 0x51, 0x1f - offset);
  129. ret |= ts2020_writereg(fe, 0x51, 0x1f);
  130. ret |= ts2020_writereg(fe, 0x50, offset);
  131. ret |= ts2020_writereg(fe, 0x50, 0x00);
  132. msleep(20);
  133. return ret;
  134. }
  135. static int ts2020_set_tuner_rf(struct dvb_frontend *fe)
  136. {
  137. int reg;
  138. reg = ts2020_readreg(fe, 0x3d);
  139. reg &= 0x7f;
  140. if (reg < 0x16)
  141. reg = 0xa1;
  142. else if (reg == 0x16)
  143. reg = 0x99;
  144. else
  145. reg = 0xf9;
  146. ts2020_writereg(fe, 0x60, reg);
  147. reg = ts2020_tuner_gate_ctrl(fe, 0x08);
  148. return reg;
  149. }
  150. static int ts2020_set_params(struct dvb_frontend *fe)
  151. {
  152. struct dtv_frontend_properties *c = &fe->dtv_property_cache;
  153. struct ts2020_priv *priv = fe->tuner_priv;
  154. int ret;
  155. u32 frequency = c->frequency;
  156. s32 offset_khz;
  157. u32 symbol_rate = (c->symbol_rate / 1000);
  158. u32 f3db, gdiv28;
  159. u16 value, ndiv, lpf_coeff;
  160. u8 lpf_mxdiv, mlpf_max, mlpf_min, nlpf;
  161. u8 lo = 0x01, div4 = 0x0;
  162. /* Calculate frequency divider */
  163. if (frequency < priv->frequency_div) {
  164. lo |= 0x10;
  165. div4 = 0x1;
  166. ndiv = (frequency * 14 * 4) / TS2020_XTAL_FREQ;
  167. } else
  168. ndiv = (frequency * 14 * 2) / TS2020_XTAL_FREQ;
  169. ndiv = ndiv + ndiv % 2;
  170. ndiv = ndiv - 1024;
  171. ret = ts2020_writereg(fe, 0x10, 0x80 | lo);
  172. /* Set frequency divider */
  173. ret |= ts2020_writereg(fe, 0x01, (ndiv >> 8) & 0xf);
  174. ret |= ts2020_writereg(fe, 0x02, ndiv & 0xff);
  175. ret |= ts2020_writereg(fe, 0x03, 0x06);
  176. ret |= ts2020_tuner_gate_ctrl(fe, 0x10);
  177. if (ret < 0)
  178. return -ENODEV;
  179. /* Tuner Frequency Range */
  180. ret = ts2020_writereg(fe, 0x10, lo);
  181. ret |= ts2020_tuner_gate_ctrl(fe, 0x08);
  182. /* Tuner RF */
  183. ret |= ts2020_set_tuner_rf(fe);
  184. gdiv28 = (TS2020_XTAL_FREQ / 1000 * 1694 + 500) / 1000;
  185. ret |= ts2020_writereg(fe, 0x04, gdiv28 & 0xff);
  186. ret |= ts2020_tuner_gate_ctrl(fe, 0x04);
  187. if (ret < 0)
  188. return -ENODEV;
  189. value = ts2020_readreg(fe, 0x26);
  190. f3db = (symbol_rate * 135) / 200 + 2000;
  191. f3db += FREQ_OFFSET_LOW_SYM_RATE;
  192. if (f3db < 7000)
  193. f3db = 7000;
  194. if (f3db > 40000)
  195. f3db = 40000;
  196. gdiv28 = gdiv28 * 207 / (value * 2 + 151);
  197. mlpf_max = gdiv28 * 135 / 100;
  198. mlpf_min = gdiv28 * 78 / 100;
  199. if (mlpf_max > 63)
  200. mlpf_max = 63;
  201. lpf_coeff = 2766;
  202. nlpf = (f3db * gdiv28 * 2 / lpf_coeff /
  203. (TS2020_XTAL_FREQ / 1000) + 1) / 2;
  204. if (nlpf > 23)
  205. nlpf = 23;
  206. if (nlpf < 1)
  207. nlpf = 1;
  208. lpf_mxdiv = (nlpf * (TS2020_XTAL_FREQ / 1000)
  209. * lpf_coeff * 2 / f3db + 1) / 2;
  210. if (lpf_mxdiv < mlpf_min) {
  211. nlpf++;
  212. lpf_mxdiv = (nlpf * (TS2020_XTAL_FREQ / 1000)
  213. * lpf_coeff * 2 / f3db + 1) / 2;
  214. }
  215. if (lpf_mxdiv > mlpf_max)
  216. lpf_mxdiv = mlpf_max;
  217. ret = ts2020_writereg(fe, 0x04, lpf_mxdiv);
  218. ret |= ts2020_writereg(fe, 0x06, nlpf);
  219. ret |= ts2020_tuner_gate_ctrl(fe, 0x04);
  220. ret |= ts2020_tuner_gate_ctrl(fe, 0x01);
  221. msleep(80);
  222. /* calculate offset assuming 96000kHz*/
  223. offset_khz = (ndiv - ndiv % 2 + 1024) * TS2020_XTAL_FREQ
  224. / (6 + 8) / (div4 + 1) / 2;
  225. priv->frequency = offset_khz;
  226. return (ret < 0) ? -EINVAL : 0;
  227. }
  228. static int ts2020_get_frequency(struct dvb_frontend *fe, u32 *frequency)
  229. {
  230. struct ts2020_priv *priv = fe->tuner_priv;
  231. *frequency = priv->frequency;
  232. return 0;
  233. }
  234. /* read TS2020 signal strength */
  235. static int ts2020_read_signal_strength(struct dvb_frontend *fe,
  236. u16 *signal_strength)
  237. {
  238. u16 sig_reading, sig_strength;
  239. u8 rfgain, bbgain;
  240. rfgain = ts2020_readreg(fe, 0x3d) & 0x1f;
  241. bbgain = ts2020_readreg(fe, 0x21) & 0x1f;
  242. if (rfgain > 15)
  243. rfgain = 15;
  244. if (bbgain > 13)
  245. bbgain = 13;
  246. sig_reading = rfgain * 2 + bbgain * 3;
  247. sig_strength = 40 + (64 - sig_reading) * 50 / 64 ;
  248. /* cook the value to be suitable for szap-s2 human readable output */
  249. *signal_strength = sig_strength * 1000;
  250. return 0;
  251. }
  252. static struct dvb_tuner_ops ts2020_tuner_ops = {
  253. .info = {
  254. .name = "TS2020",
  255. .frequency_min = 950000,
  256. .frequency_max = 2150000
  257. },
  258. .init = ts2020_init,
  259. .release = ts2020_release,
  260. .sleep = ts2020_sleep,
  261. .set_params = ts2020_set_params,
  262. .get_frequency = ts2020_get_frequency,
  263. .get_rf_strength = ts2020_read_signal_strength,
  264. };
  265. struct dvb_frontend *ts2020_attach(struct dvb_frontend *fe,
  266. const struct ts2020_config *config,
  267. struct i2c_adapter *i2c)
  268. {
  269. struct ts2020_priv *priv = NULL;
  270. u8 buf;
  271. priv = kzalloc(sizeof(struct ts2020_priv), GFP_KERNEL);
  272. if (priv == NULL)
  273. return NULL;
  274. priv->i2c_address = config->tuner_address;
  275. priv->i2c = i2c;
  276. priv->clk_out_div = config->clk_out_div;
  277. priv->frequency_div = config->frequency_div;
  278. fe->tuner_priv = priv;
  279. if (!priv->frequency_div)
  280. priv->frequency_div = 1060000;
  281. /* Wake Up the tuner */
  282. if ((0x03 & ts2020_readreg(fe, 0x00)) == 0x00) {
  283. ts2020_writereg(fe, 0x00, 0x01);
  284. msleep(2);
  285. }
  286. ts2020_writereg(fe, 0x00, 0x03);
  287. msleep(2);
  288. /* Check the tuner version */
  289. buf = ts2020_readreg(fe, 0x00);
  290. if ((buf == 0x01) || (buf == 0x41) || (buf == 0x81))
  291. printk(KERN_INFO "%s: Find tuner TS2020!\n", __func__);
  292. else {
  293. printk(KERN_ERR "%s: Read tuner reg[0] = %d\n", __func__, buf);
  294. kfree(priv);
  295. return NULL;
  296. }
  297. memcpy(&fe->ops.tuner_ops, &ts2020_tuner_ops,
  298. sizeof(struct dvb_tuner_ops));
  299. return fe;
  300. }
  301. EXPORT_SYMBOL(ts2020_attach);
  302. MODULE_AUTHOR("Konstantin Dimitrov <kosio.dimitrov@gmail.com>");
  303. MODULE_DESCRIPTION("Montage Technology TS2020 - Silicon tuner driver module");
  304. MODULE_LICENSE("GPL");