ts2020.c 8.4 KB

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