ts2020.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744
  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 <media/dvb_frontend.h>
  18. #include "ts2020.h"
  19. #include <linux/regmap.h>
  20. #include <linux/math64.h>
  21. #define TS2020_XTAL_FREQ 27000 /* in kHz */
  22. #define FREQ_OFFSET_LOW_SYM_RATE 3000
  23. struct ts2020_priv {
  24. struct i2c_client *client;
  25. struct mutex regmap_mutex;
  26. struct regmap_config regmap_config;
  27. struct regmap *regmap;
  28. struct dvb_frontend *fe;
  29. struct delayed_work stat_work;
  30. int (*get_agc_pwm)(struct dvb_frontend *fe, u8 *_agc_pwm);
  31. /* i2c details */
  32. struct i2c_adapter *i2c;
  33. int i2c_address;
  34. bool loop_through:1;
  35. u8 clk_out:2;
  36. u8 clk_out_div:5;
  37. bool dont_poll:1;
  38. u32 frequency_div; /* LO output divider switch frequency */
  39. u32 frequency_khz; /* actual used LO frequency */
  40. #define TS2020_M88TS2020 0
  41. #define TS2020_M88TS2022 1
  42. u8 tuner;
  43. };
  44. struct ts2020_reg_val {
  45. u8 reg;
  46. u8 val;
  47. };
  48. static void ts2020_stat_work(struct work_struct *work);
  49. static void ts2020_release(struct dvb_frontend *fe)
  50. {
  51. struct ts2020_priv *priv = fe->tuner_priv;
  52. struct i2c_client *client = priv->client;
  53. dev_dbg(&client->dev, "\n");
  54. i2c_unregister_device(client);
  55. }
  56. static int ts2020_sleep(struct dvb_frontend *fe)
  57. {
  58. struct ts2020_priv *priv = fe->tuner_priv;
  59. int ret;
  60. u8 u8tmp;
  61. if (priv->tuner == TS2020_M88TS2020)
  62. u8tmp = 0x0a; /* XXX: probably wrong */
  63. else
  64. u8tmp = 0x00;
  65. ret = regmap_write(priv->regmap, u8tmp, 0x00);
  66. if (ret < 0)
  67. return ret;
  68. /* stop statistics polling */
  69. if (!priv->dont_poll)
  70. cancel_delayed_work_sync(&priv->stat_work);
  71. return 0;
  72. }
  73. static int ts2020_init(struct dvb_frontend *fe)
  74. {
  75. struct dtv_frontend_properties *c = &fe->dtv_property_cache;
  76. struct ts2020_priv *priv = fe->tuner_priv;
  77. int i;
  78. u8 u8tmp;
  79. if (priv->tuner == TS2020_M88TS2020) {
  80. regmap_write(priv->regmap, 0x42, 0x73);
  81. regmap_write(priv->regmap, 0x05, priv->clk_out_div);
  82. regmap_write(priv->regmap, 0x20, 0x27);
  83. regmap_write(priv->regmap, 0x07, 0x02);
  84. regmap_write(priv->regmap, 0x11, 0xff);
  85. regmap_write(priv->regmap, 0x60, 0xf9);
  86. regmap_write(priv->regmap, 0x08, 0x01);
  87. regmap_write(priv->regmap, 0x00, 0x41);
  88. } else {
  89. static const struct ts2020_reg_val reg_vals[] = {
  90. {0x7d, 0x9d},
  91. {0x7c, 0x9a},
  92. {0x7a, 0x76},
  93. {0x3b, 0x01},
  94. {0x63, 0x88},
  95. {0x61, 0x85},
  96. {0x22, 0x30},
  97. {0x30, 0x40},
  98. {0x20, 0x23},
  99. {0x24, 0x02},
  100. {0x12, 0xa0},
  101. };
  102. regmap_write(priv->regmap, 0x00, 0x01);
  103. regmap_write(priv->regmap, 0x00, 0x03);
  104. switch (priv->clk_out) {
  105. case TS2020_CLK_OUT_DISABLED:
  106. u8tmp = 0x60;
  107. break;
  108. case TS2020_CLK_OUT_ENABLED:
  109. u8tmp = 0x70;
  110. regmap_write(priv->regmap, 0x05, priv->clk_out_div);
  111. break;
  112. case TS2020_CLK_OUT_ENABLED_XTALOUT:
  113. u8tmp = 0x6c;
  114. break;
  115. default:
  116. u8tmp = 0x60;
  117. break;
  118. }
  119. regmap_write(priv->regmap, 0x42, u8tmp);
  120. if (priv->loop_through)
  121. u8tmp = 0xec;
  122. else
  123. u8tmp = 0x6c;
  124. regmap_write(priv->regmap, 0x62, u8tmp);
  125. for (i = 0; i < ARRAY_SIZE(reg_vals); i++)
  126. regmap_write(priv->regmap, reg_vals[i].reg,
  127. reg_vals[i].val);
  128. }
  129. /* Initialise v5 stats here */
  130. c->strength.len = 1;
  131. c->strength.stat[0].scale = FE_SCALE_DECIBEL;
  132. c->strength.stat[0].uvalue = 0;
  133. /* Start statistics polling by invoking the work function */
  134. ts2020_stat_work(&priv->stat_work.work);
  135. return 0;
  136. }
  137. static int ts2020_tuner_gate_ctrl(struct dvb_frontend *fe, u8 offset)
  138. {
  139. struct ts2020_priv *priv = fe->tuner_priv;
  140. int ret;
  141. ret = regmap_write(priv->regmap, 0x51, 0x1f - offset);
  142. ret |= regmap_write(priv->regmap, 0x51, 0x1f);
  143. ret |= regmap_write(priv->regmap, 0x50, offset);
  144. ret |= regmap_write(priv->regmap, 0x50, 0x00);
  145. msleep(20);
  146. return ret;
  147. }
  148. static int ts2020_set_tuner_rf(struct dvb_frontend *fe)
  149. {
  150. struct ts2020_priv *dev = fe->tuner_priv;
  151. int ret;
  152. unsigned int utmp;
  153. ret = regmap_read(dev->regmap, 0x3d, &utmp);
  154. utmp &= 0x7f;
  155. if (utmp < 0x16)
  156. utmp = 0xa1;
  157. else if (utmp == 0x16)
  158. utmp = 0x99;
  159. else
  160. utmp = 0xf9;
  161. regmap_write(dev->regmap, 0x60, utmp);
  162. ret = ts2020_tuner_gate_ctrl(fe, 0x08);
  163. return ret;
  164. }
  165. static int ts2020_set_params(struct dvb_frontend *fe)
  166. {
  167. struct dtv_frontend_properties *c = &fe->dtv_property_cache;
  168. struct ts2020_priv *priv = fe->tuner_priv;
  169. int ret;
  170. unsigned int utmp;
  171. u32 f3db, gdiv28;
  172. u16 u16tmp, value, lpf_coeff;
  173. u8 buf[3], reg10, lpf_mxdiv, mlpf_max, mlpf_min, nlpf;
  174. unsigned int f_ref_khz, f_vco_khz, div_ref, div_out, pll_n;
  175. unsigned int frequency_khz = c->frequency;
  176. /*
  177. * Integer-N PLL synthesizer
  178. * kHz is used for all calculations to keep calculations within 32-bit
  179. */
  180. f_ref_khz = TS2020_XTAL_FREQ;
  181. div_ref = DIV_ROUND_CLOSEST(f_ref_khz, 2000);
  182. /* select LO output divider */
  183. if (frequency_khz < priv->frequency_div) {
  184. div_out = 4;
  185. reg10 = 0x10;
  186. } else {
  187. div_out = 2;
  188. reg10 = 0x00;
  189. }
  190. f_vco_khz = frequency_khz * div_out;
  191. pll_n = f_vco_khz * div_ref / f_ref_khz;
  192. pll_n += pll_n % 2;
  193. priv->frequency_khz = pll_n * f_ref_khz / div_ref / div_out;
  194. pr_debug("frequency=%u offset=%d f_vco_khz=%u pll_n=%u div_ref=%u div_out=%u\n",
  195. priv->frequency_khz, priv->frequency_khz - c->frequency,
  196. f_vco_khz, pll_n, div_ref, div_out);
  197. if (priv->tuner == TS2020_M88TS2020) {
  198. lpf_coeff = 2766;
  199. reg10 |= 0x01;
  200. ret = regmap_write(priv->regmap, 0x10, reg10);
  201. } else {
  202. lpf_coeff = 3200;
  203. reg10 |= 0x0b;
  204. ret = regmap_write(priv->regmap, 0x10, reg10);
  205. ret |= regmap_write(priv->regmap, 0x11, 0x40);
  206. }
  207. u16tmp = pll_n - 1024;
  208. buf[0] = (u16tmp >> 8) & 0xff;
  209. buf[1] = (u16tmp >> 0) & 0xff;
  210. buf[2] = div_ref - 8;
  211. ret |= regmap_write(priv->regmap, 0x01, buf[0]);
  212. ret |= regmap_write(priv->regmap, 0x02, buf[1]);
  213. ret |= regmap_write(priv->regmap, 0x03, buf[2]);
  214. ret |= ts2020_tuner_gate_ctrl(fe, 0x10);
  215. if (ret < 0)
  216. return -ENODEV;
  217. ret |= ts2020_tuner_gate_ctrl(fe, 0x08);
  218. /* Tuner RF */
  219. if (priv->tuner == TS2020_M88TS2020)
  220. ret |= ts2020_set_tuner_rf(fe);
  221. gdiv28 = (TS2020_XTAL_FREQ / 1000 * 1694 + 500) / 1000;
  222. ret |= regmap_write(priv->regmap, 0x04, gdiv28 & 0xff);
  223. ret |= ts2020_tuner_gate_ctrl(fe, 0x04);
  224. if (ret < 0)
  225. return -ENODEV;
  226. if (priv->tuner == TS2020_M88TS2022) {
  227. ret = regmap_write(priv->regmap, 0x25, 0x00);
  228. ret |= regmap_write(priv->regmap, 0x27, 0x70);
  229. ret |= regmap_write(priv->regmap, 0x41, 0x09);
  230. ret |= regmap_write(priv->regmap, 0x08, 0x0b);
  231. if (ret < 0)
  232. return -ENODEV;
  233. }
  234. regmap_read(priv->regmap, 0x26, &utmp);
  235. value = utmp;
  236. f3db = (c->bandwidth_hz / 1000 / 2) + 2000;
  237. f3db += FREQ_OFFSET_LOW_SYM_RATE; /* FIXME: ~always too wide filter */
  238. f3db = clamp(f3db, 7000U, 40000U);
  239. gdiv28 = gdiv28 * 207 / (value * 2 + 151);
  240. mlpf_max = gdiv28 * 135 / 100;
  241. mlpf_min = gdiv28 * 78 / 100;
  242. if (mlpf_max > 63)
  243. mlpf_max = 63;
  244. nlpf = (f3db * gdiv28 * 2 / lpf_coeff /
  245. (TS2020_XTAL_FREQ / 1000) + 1) / 2;
  246. if (nlpf > 23)
  247. nlpf = 23;
  248. if (nlpf < 1)
  249. nlpf = 1;
  250. lpf_mxdiv = (nlpf * (TS2020_XTAL_FREQ / 1000)
  251. * lpf_coeff * 2 / f3db + 1) / 2;
  252. if (lpf_mxdiv < mlpf_min) {
  253. nlpf++;
  254. lpf_mxdiv = (nlpf * (TS2020_XTAL_FREQ / 1000)
  255. * lpf_coeff * 2 / f3db + 1) / 2;
  256. }
  257. if (lpf_mxdiv > mlpf_max)
  258. lpf_mxdiv = mlpf_max;
  259. ret = regmap_write(priv->regmap, 0x04, lpf_mxdiv);
  260. ret |= regmap_write(priv->regmap, 0x06, nlpf);
  261. ret |= ts2020_tuner_gate_ctrl(fe, 0x04);
  262. ret |= ts2020_tuner_gate_ctrl(fe, 0x01);
  263. msleep(80);
  264. return (ret < 0) ? -EINVAL : 0;
  265. }
  266. static int ts2020_get_frequency(struct dvb_frontend *fe, u32 *frequency)
  267. {
  268. struct ts2020_priv *priv = fe->tuner_priv;
  269. *frequency = priv->frequency_khz;
  270. return 0;
  271. }
  272. static int ts2020_get_if_frequency(struct dvb_frontend *fe, u32 *frequency)
  273. {
  274. *frequency = 0; /* Zero-IF */
  275. return 0;
  276. }
  277. /*
  278. * Get the tuner gain.
  279. * @fe: The front end for which we're determining the gain
  280. * @v_agc: The voltage of the AGC from the demodulator (0-2600mV)
  281. * @_gain: Where to store the gain (in 0.001dB units)
  282. *
  283. * Returns 0 or a negative error code.
  284. */
  285. static int ts2020_read_tuner_gain(struct dvb_frontend *fe, unsigned v_agc,
  286. __s64 *_gain)
  287. {
  288. struct ts2020_priv *priv = fe->tuner_priv;
  289. unsigned long gain1, gain2, gain3;
  290. unsigned utmp;
  291. int ret;
  292. /* Read the RF gain */
  293. ret = regmap_read(priv->regmap, 0x3d, &utmp);
  294. if (ret < 0)
  295. return ret;
  296. gain1 = utmp & 0x1f;
  297. /* Read the baseband gain */
  298. ret = regmap_read(priv->regmap, 0x21, &utmp);
  299. if (ret < 0)
  300. return ret;
  301. gain2 = utmp & 0x1f;
  302. switch (priv->tuner) {
  303. case TS2020_M88TS2020:
  304. gain1 = clamp_t(long, gain1, 0, 15);
  305. gain2 = clamp_t(long, gain2, 0, 13);
  306. v_agc = clamp_t(long, v_agc, 400, 1100);
  307. *_gain = -((__s64)gain1 * 2330 +
  308. gain2 * 3500 +
  309. v_agc * 24 / 10 * 10 +
  310. 10000);
  311. /* gain in range -19600 to -116850 in units of 0.001dB */
  312. break;
  313. case TS2020_M88TS2022:
  314. ret = regmap_read(priv->regmap, 0x66, &utmp);
  315. if (ret < 0)
  316. return ret;
  317. gain3 = (utmp >> 3) & 0x07;
  318. gain1 = clamp_t(long, gain1, 0, 15);
  319. gain2 = clamp_t(long, gain2, 2, 16);
  320. gain3 = clamp_t(long, gain3, 0, 6);
  321. v_agc = clamp_t(long, v_agc, 600, 1600);
  322. *_gain = -((__s64)gain1 * 2650 +
  323. gain2 * 3380 +
  324. gain3 * 2850 +
  325. v_agc * 176 / 100 * 10 -
  326. 30000);
  327. /* gain in range -47320 to -158950 in units of 0.001dB */
  328. break;
  329. }
  330. return 0;
  331. }
  332. /*
  333. * Get the AGC information from the demodulator and use that to calculate the
  334. * tuner gain.
  335. */
  336. static int ts2020_get_tuner_gain(struct dvb_frontend *fe, __s64 *_gain)
  337. {
  338. struct ts2020_priv *priv = fe->tuner_priv;
  339. int v_agc = 0, ret;
  340. u8 agc_pwm;
  341. /* Read the AGC PWM rate from the demodulator */
  342. if (priv->get_agc_pwm) {
  343. ret = priv->get_agc_pwm(fe, &agc_pwm);
  344. if (ret < 0)
  345. return ret;
  346. switch (priv->tuner) {
  347. case TS2020_M88TS2020:
  348. v_agc = (int)agc_pwm * 20 - 1166;
  349. break;
  350. case TS2020_M88TS2022:
  351. v_agc = (int)agc_pwm * 16 - 670;
  352. break;
  353. }
  354. if (v_agc < 0)
  355. v_agc = 0;
  356. }
  357. return ts2020_read_tuner_gain(fe, v_agc, _gain);
  358. }
  359. /*
  360. * Gather statistics on a regular basis
  361. */
  362. static void ts2020_stat_work(struct work_struct *work)
  363. {
  364. struct ts2020_priv *priv = container_of(work, struct ts2020_priv,
  365. stat_work.work);
  366. struct i2c_client *client = priv->client;
  367. struct dtv_frontend_properties *c = &priv->fe->dtv_property_cache;
  368. int ret;
  369. dev_dbg(&client->dev, "\n");
  370. ret = ts2020_get_tuner_gain(priv->fe, &c->strength.stat[0].svalue);
  371. if (ret < 0)
  372. goto err;
  373. c->strength.stat[0].scale = FE_SCALE_DECIBEL;
  374. if (!priv->dont_poll)
  375. schedule_delayed_work(&priv->stat_work, msecs_to_jiffies(2000));
  376. return;
  377. err:
  378. dev_dbg(&client->dev, "failed=%d\n", ret);
  379. }
  380. /*
  381. * Read TS2020 signal strength in v3 format.
  382. */
  383. static int ts2020_read_signal_strength(struct dvb_frontend *fe,
  384. u16 *_signal_strength)
  385. {
  386. struct dtv_frontend_properties *c = &fe->dtv_property_cache;
  387. struct ts2020_priv *priv = fe->tuner_priv;
  388. unsigned strength;
  389. __s64 gain;
  390. if (priv->dont_poll)
  391. ts2020_stat_work(&priv->stat_work.work);
  392. if (c->strength.stat[0].scale == FE_SCALE_NOT_AVAILABLE) {
  393. *_signal_strength = 0;
  394. return 0;
  395. }
  396. gain = c->strength.stat[0].svalue;
  397. /* Calculate the signal strength based on the total gain of the tuner */
  398. if (gain < -85000)
  399. /* 0%: no signal or weak signal */
  400. strength = 0;
  401. else if (gain < -65000)
  402. /* 0% - 60%: weak signal */
  403. strength = 0 + div64_s64((85000 + gain) * 3, 1000);
  404. else if (gain < -45000)
  405. /* 60% - 90%: normal signal */
  406. strength = 60 + div64_s64((65000 + gain) * 3, 2000);
  407. else
  408. /* 90% - 99%: strong signal */
  409. strength = 90 + div64_s64((45000 + gain), 5000);
  410. *_signal_strength = strength * 65535 / 100;
  411. return 0;
  412. }
  413. static const struct dvb_tuner_ops ts2020_tuner_ops = {
  414. .info = {
  415. .name = "TS2020",
  416. .frequency_min_hz = 950 * MHz,
  417. .frequency_max_hz = 2150 * MHz
  418. },
  419. .init = ts2020_init,
  420. .release = ts2020_release,
  421. .sleep = ts2020_sleep,
  422. .set_params = ts2020_set_params,
  423. .get_frequency = ts2020_get_frequency,
  424. .get_if_frequency = ts2020_get_if_frequency,
  425. .get_rf_strength = ts2020_read_signal_strength,
  426. };
  427. struct dvb_frontend *ts2020_attach(struct dvb_frontend *fe,
  428. const struct ts2020_config *config,
  429. struct i2c_adapter *i2c)
  430. {
  431. struct i2c_client *client;
  432. struct i2c_board_info board_info;
  433. /* This is only used by ts2020_probe() so can be on the stack */
  434. struct ts2020_config pdata;
  435. memcpy(&pdata, config, sizeof(pdata));
  436. pdata.fe = fe;
  437. pdata.attach_in_use = true;
  438. memset(&board_info, 0, sizeof(board_info));
  439. strlcpy(board_info.type, "ts2020", I2C_NAME_SIZE);
  440. board_info.addr = config->tuner_address;
  441. board_info.platform_data = &pdata;
  442. client = i2c_new_device(i2c, &board_info);
  443. if (!client || !client->dev.driver)
  444. return NULL;
  445. return fe;
  446. }
  447. EXPORT_SYMBOL(ts2020_attach);
  448. /*
  449. * We implement own regmap locking due to legacy DVB attach which uses frontend
  450. * gate control callback to control I2C bus access. We can open / close gate and
  451. * serialize whole open / I2C-operation / close sequence at the same.
  452. */
  453. static void ts2020_regmap_lock(void *__dev)
  454. {
  455. struct ts2020_priv *dev = __dev;
  456. mutex_lock(&dev->regmap_mutex);
  457. if (dev->fe->ops.i2c_gate_ctrl)
  458. dev->fe->ops.i2c_gate_ctrl(dev->fe, 1);
  459. }
  460. static void ts2020_regmap_unlock(void *__dev)
  461. {
  462. struct ts2020_priv *dev = __dev;
  463. if (dev->fe->ops.i2c_gate_ctrl)
  464. dev->fe->ops.i2c_gate_ctrl(dev->fe, 0);
  465. mutex_unlock(&dev->regmap_mutex);
  466. }
  467. static int ts2020_probe(struct i2c_client *client,
  468. const struct i2c_device_id *id)
  469. {
  470. struct ts2020_config *pdata = client->dev.platform_data;
  471. struct dvb_frontend *fe = pdata->fe;
  472. struct ts2020_priv *dev;
  473. int ret;
  474. u8 u8tmp;
  475. unsigned int utmp;
  476. char *chip_str;
  477. dev = kzalloc(sizeof(*dev), GFP_KERNEL);
  478. if (!dev) {
  479. ret = -ENOMEM;
  480. goto err;
  481. }
  482. /* create regmap */
  483. mutex_init(&dev->regmap_mutex);
  484. dev->regmap_config.reg_bits = 8,
  485. dev->regmap_config.val_bits = 8,
  486. dev->regmap_config.lock = ts2020_regmap_lock,
  487. dev->regmap_config.unlock = ts2020_regmap_unlock,
  488. dev->regmap_config.lock_arg = dev,
  489. dev->regmap = regmap_init_i2c(client, &dev->regmap_config);
  490. if (IS_ERR(dev->regmap)) {
  491. ret = PTR_ERR(dev->regmap);
  492. goto err_kfree;
  493. }
  494. dev->i2c = client->adapter;
  495. dev->i2c_address = client->addr;
  496. dev->loop_through = pdata->loop_through;
  497. dev->clk_out = pdata->clk_out;
  498. dev->clk_out_div = pdata->clk_out_div;
  499. dev->dont_poll = pdata->dont_poll;
  500. dev->frequency_div = pdata->frequency_div;
  501. dev->fe = fe;
  502. dev->get_agc_pwm = pdata->get_agc_pwm;
  503. fe->tuner_priv = dev;
  504. dev->client = client;
  505. INIT_DELAYED_WORK(&dev->stat_work, ts2020_stat_work);
  506. /* check if the tuner is there */
  507. ret = regmap_read(dev->regmap, 0x00, &utmp);
  508. if (ret)
  509. goto err_regmap_exit;
  510. if ((utmp & 0x03) == 0x00) {
  511. ret = regmap_write(dev->regmap, 0x00, 0x01);
  512. if (ret)
  513. goto err_regmap_exit;
  514. usleep_range(2000, 50000);
  515. }
  516. ret = regmap_write(dev->regmap, 0x00, 0x03);
  517. if (ret)
  518. goto err_regmap_exit;
  519. usleep_range(2000, 50000);
  520. ret = regmap_read(dev->regmap, 0x00, &utmp);
  521. if (ret)
  522. goto err_regmap_exit;
  523. dev_dbg(&client->dev, "chip_id=%02x\n", utmp);
  524. switch (utmp) {
  525. case 0x01:
  526. case 0x41:
  527. case 0x81:
  528. dev->tuner = TS2020_M88TS2020;
  529. chip_str = "TS2020";
  530. if (!dev->frequency_div)
  531. dev->frequency_div = 1060000;
  532. break;
  533. case 0xc3:
  534. case 0x83:
  535. dev->tuner = TS2020_M88TS2022;
  536. chip_str = "TS2022";
  537. if (!dev->frequency_div)
  538. dev->frequency_div = 1103000;
  539. break;
  540. default:
  541. ret = -ENODEV;
  542. goto err_regmap_exit;
  543. }
  544. if (dev->tuner == TS2020_M88TS2022) {
  545. switch (dev->clk_out) {
  546. case TS2020_CLK_OUT_DISABLED:
  547. u8tmp = 0x60;
  548. break;
  549. case TS2020_CLK_OUT_ENABLED:
  550. u8tmp = 0x70;
  551. ret = regmap_write(dev->regmap, 0x05, dev->clk_out_div);
  552. if (ret)
  553. goto err_regmap_exit;
  554. break;
  555. case TS2020_CLK_OUT_ENABLED_XTALOUT:
  556. u8tmp = 0x6c;
  557. break;
  558. default:
  559. ret = -EINVAL;
  560. goto err_regmap_exit;
  561. }
  562. ret = regmap_write(dev->regmap, 0x42, u8tmp);
  563. if (ret)
  564. goto err_regmap_exit;
  565. if (dev->loop_through)
  566. u8tmp = 0xec;
  567. else
  568. u8tmp = 0x6c;
  569. ret = regmap_write(dev->regmap, 0x62, u8tmp);
  570. if (ret)
  571. goto err_regmap_exit;
  572. }
  573. /* sleep */
  574. ret = regmap_write(dev->regmap, 0x00, 0x00);
  575. if (ret)
  576. goto err_regmap_exit;
  577. dev_info(&client->dev,
  578. "Montage Technology %s successfully identified\n", chip_str);
  579. memcpy(&fe->ops.tuner_ops, &ts2020_tuner_ops,
  580. sizeof(struct dvb_tuner_ops));
  581. if (!pdata->attach_in_use)
  582. fe->ops.tuner_ops.release = NULL;
  583. i2c_set_clientdata(client, dev);
  584. return 0;
  585. err_regmap_exit:
  586. regmap_exit(dev->regmap);
  587. err_kfree:
  588. kfree(dev);
  589. err:
  590. dev_dbg(&client->dev, "failed=%d\n", ret);
  591. return ret;
  592. }
  593. static int ts2020_remove(struct i2c_client *client)
  594. {
  595. struct ts2020_priv *dev = i2c_get_clientdata(client);
  596. dev_dbg(&client->dev, "\n");
  597. /* stop statistics polling */
  598. if (!dev->dont_poll)
  599. cancel_delayed_work_sync(&dev->stat_work);
  600. regmap_exit(dev->regmap);
  601. kfree(dev);
  602. return 0;
  603. }
  604. static const struct i2c_device_id ts2020_id_table[] = {
  605. {"ts2020", 0},
  606. {"ts2022", 0},
  607. {}
  608. };
  609. MODULE_DEVICE_TABLE(i2c, ts2020_id_table);
  610. static struct i2c_driver ts2020_driver = {
  611. .driver = {
  612. .name = "ts2020",
  613. },
  614. .probe = ts2020_probe,
  615. .remove = ts2020_remove,
  616. .id_table = ts2020_id_table,
  617. };
  618. module_i2c_driver(ts2020_driver);
  619. MODULE_AUTHOR("Konstantin Dimitrov <kosio.dimitrov@gmail.com>");
  620. MODULE_DESCRIPTION("Montage Technology TS2020 - Silicon tuner driver module");
  621. MODULE_LICENSE("GPL");