it913x.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477
  1. /*
  2. * ITE IT913X silicon tuner driver
  3. *
  4. * Copyright (C) 2011 Malcolm Priestley (tvboxspy@gmail.com)
  5. * IT9137 Copyright (C) ITE Tech Inc.
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. *
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.=
  21. */
  22. #include "it913x.h"
  23. #include <linux/regmap.h>
  24. struct it913x_dev {
  25. struct i2c_client *client;
  26. struct regmap *regmap;
  27. struct dvb_frontend *fe;
  28. u8 chip_ver:2;
  29. u8 role:2;
  30. u16 xtal;
  31. u8 fdiv;
  32. u8 clk_mode;
  33. u32 fn_min;
  34. bool active;
  35. };
  36. static int it913x_init(struct dvb_frontend *fe)
  37. {
  38. struct it913x_dev *dev = fe->tuner_priv;
  39. int ret;
  40. unsigned int utmp;
  41. u8 iqik_m_cal, nv_val, buf[2];
  42. static const u8 nv[] = {48, 32, 24, 16, 12, 8, 6, 4, 2};
  43. unsigned long timeout;
  44. dev_dbg(&dev->client->dev, "role %u\n", dev->role);
  45. ret = regmap_write(dev->regmap, 0x80ec4c, 0x68);
  46. if (ret)
  47. goto err;
  48. usleep_range(10000, 100000);
  49. ret = regmap_read(dev->regmap, 0x80ec86, &utmp);
  50. if (ret)
  51. goto err;
  52. switch (utmp) {
  53. case 0:
  54. /* 12.000 MHz */
  55. dev->clk_mode = utmp;
  56. dev->xtal = 2000;
  57. dev->fdiv = 3;
  58. iqik_m_cal = 16;
  59. break;
  60. case 1:
  61. /* 20.480 MHz */
  62. dev->clk_mode = utmp;
  63. dev->xtal = 640;
  64. dev->fdiv = 1;
  65. iqik_m_cal = 6;
  66. break;
  67. default:
  68. dev_err(&dev->client->dev, "unknown clock identifier %d\n", utmp);
  69. goto err;
  70. }
  71. ret = regmap_read(dev->regmap, 0x80ed03, &utmp);
  72. if (ret)
  73. goto err;
  74. else if (utmp < ARRAY_SIZE(nv))
  75. nv_val = nv[utmp];
  76. else
  77. nv_val = 2;
  78. #define TIMEOUT 50
  79. timeout = jiffies + msecs_to_jiffies(TIMEOUT);
  80. while (!time_after(jiffies, timeout)) {
  81. ret = regmap_bulk_read(dev->regmap, 0x80ed23, buf, 2);
  82. if (ret)
  83. goto err;
  84. utmp = (buf[1] << 8) | (buf[0] << 0);
  85. if (utmp)
  86. break;
  87. }
  88. dev_dbg(&dev->client->dev, "r_fbc_m_bdry took %u ms, val %u\n",
  89. jiffies_to_msecs(jiffies) -
  90. (jiffies_to_msecs(timeout) - TIMEOUT), utmp);
  91. dev->fn_min = dev->xtal * utmp;
  92. dev->fn_min /= (dev->fdiv * nv_val);
  93. dev->fn_min *= 1000;
  94. dev_dbg(&dev->client->dev, "fn_min %u\n", dev->fn_min);
  95. /*
  96. * Chip version BX never sets that flag so we just wait 50ms in that
  97. * case. It is possible poll BX similarly than AX and then timeout in
  98. * order to get 50ms delay, but that causes about 120 extra I2C
  99. * messages. As for now, we just wait and reduce IO.
  100. */
  101. if (dev->chip_ver == 1) {
  102. #define TIMEOUT 50
  103. timeout = jiffies + msecs_to_jiffies(TIMEOUT);
  104. while (!time_after(jiffies, timeout)) {
  105. ret = regmap_read(dev->regmap, 0x80ec82, &utmp);
  106. if (ret)
  107. goto err;
  108. if (utmp)
  109. break;
  110. }
  111. dev_dbg(&dev->client->dev, "p_tsm_init_mode took %u ms, val %u\n",
  112. jiffies_to_msecs(jiffies) -
  113. (jiffies_to_msecs(timeout) - TIMEOUT), utmp);
  114. } else {
  115. msleep(50);
  116. }
  117. ret = regmap_write(dev->regmap, 0x80ed81, iqik_m_cal);
  118. if (ret)
  119. goto err;
  120. ret = regmap_write(dev->regmap, 0x80ec57, 0x00);
  121. if (ret)
  122. goto err;
  123. ret = regmap_write(dev->regmap, 0x80ec58, 0x00);
  124. if (ret)
  125. goto err;
  126. ret = regmap_write(dev->regmap, 0x80ec40, 0x01);
  127. if (ret)
  128. goto err;
  129. dev->active = true;
  130. return 0;
  131. err:
  132. dev_dbg(&dev->client->dev, "failed %d\n", ret);
  133. return ret;
  134. }
  135. static int it913x_sleep(struct dvb_frontend *fe)
  136. {
  137. struct it913x_dev *dev = fe->tuner_priv;
  138. int ret, len;
  139. dev_dbg(&dev->client->dev, "role %u\n", dev->role);
  140. dev->active = false;
  141. ret = regmap_bulk_write(dev->regmap, 0x80ec40, "\x00", 1);
  142. if (ret)
  143. goto err;
  144. /*
  145. * Writing '0x00' to master tuner register '0x80ec08' causes slave tuner
  146. * communication lost. Due to that, we cannot put master full sleep.
  147. */
  148. if (dev->role == IT913X_ROLE_DUAL_MASTER)
  149. len = 4;
  150. else
  151. len = 15;
  152. dev_dbg(&dev->client->dev, "role %u, len %d\n", dev->role, len);
  153. ret = regmap_bulk_write(dev->regmap, 0x80ec02,
  154. "\x3f\x1f\x3f\x3e\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00",
  155. len);
  156. if (ret)
  157. goto err;
  158. ret = regmap_bulk_write(dev->regmap, 0x80ec12, "\x00\x00\x00\x00", 4);
  159. if (ret)
  160. goto err;
  161. ret = regmap_bulk_write(dev->regmap, 0x80ec17,
  162. "\x00\x00\x00\x00\x00\x00\x00\x00\x00", 9);
  163. if (ret)
  164. goto err;
  165. ret = regmap_bulk_write(dev->regmap, 0x80ec22,
  166. "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00", 10);
  167. if (ret)
  168. goto err;
  169. ret = regmap_bulk_write(dev->regmap, 0x80ec20, "\x00", 1);
  170. if (ret)
  171. goto err;
  172. ret = regmap_bulk_write(dev->regmap, 0x80ec3f, "\x01", 1);
  173. if (ret)
  174. goto err;
  175. return 0;
  176. err:
  177. dev_dbg(&dev->client->dev, "failed %d\n", ret);
  178. return ret;
  179. }
  180. static int it913x_set_params(struct dvb_frontend *fe)
  181. {
  182. struct it913x_dev *dev = fe->tuner_priv;
  183. struct dtv_frontend_properties *c = &fe->dtv_property_cache;
  184. int ret;
  185. unsigned int utmp;
  186. u32 pre_lo_freq, t_cal_freq;
  187. u16 iqik_m_cal, n_div;
  188. u8 u8tmp, n, l_band, lna_band;
  189. dev_dbg(&dev->client->dev, "role=%u, frequency %u, bandwidth_hz %u\n",
  190. dev->role, c->frequency, c->bandwidth_hz);
  191. if (!dev->active) {
  192. ret = -EINVAL;
  193. goto err;
  194. }
  195. if (c->frequency <= 74000000) {
  196. n_div = 48;
  197. n = 0;
  198. } else if (c->frequency <= 111000000) {
  199. n_div = 32;
  200. n = 1;
  201. } else if (c->frequency <= 148000000) {
  202. n_div = 24;
  203. n = 2;
  204. } else if (c->frequency <= 222000000) {
  205. n_div = 16;
  206. n = 3;
  207. } else if (c->frequency <= 296000000) {
  208. n_div = 12;
  209. n = 4;
  210. } else if (c->frequency <= 445000000) {
  211. n_div = 8;
  212. n = 5;
  213. } else if (c->frequency <= dev->fn_min) {
  214. n_div = 6;
  215. n = 6;
  216. } else if (c->frequency <= 950000000) {
  217. n_div = 4;
  218. n = 7;
  219. } else {
  220. n_div = 2;
  221. n = 0;
  222. }
  223. ret = regmap_read(dev->regmap, 0x80ed81, &utmp);
  224. if (ret)
  225. goto err;
  226. iqik_m_cal = utmp * n_div;
  227. if (utmp < 0x20) {
  228. if (dev->clk_mode == 0)
  229. iqik_m_cal = (iqik_m_cal * 9) >> 5;
  230. else
  231. iqik_m_cal >>= 1;
  232. } else {
  233. iqik_m_cal = 0x40 - iqik_m_cal;
  234. if (dev->clk_mode == 0)
  235. iqik_m_cal = ~((iqik_m_cal * 9) >> 5);
  236. else
  237. iqik_m_cal = ~(iqik_m_cal >> 1);
  238. }
  239. t_cal_freq = (c->frequency / 1000) * n_div * dev->fdiv;
  240. pre_lo_freq = t_cal_freq / dev->xtal;
  241. utmp = pre_lo_freq * dev->xtal;
  242. if ((t_cal_freq - utmp) >= (dev->xtal >> 1))
  243. pre_lo_freq++;
  244. pre_lo_freq += (u32) n << 13;
  245. /* Frequency OMEGA_IQIK_M_CAL_MID*/
  246. t_cal_freq = pre_lo_freq + (u32)iqik_m_cal;
  247. dev_dbg(&dev->client->dev, "t_cal_freq %u, pre_lo_freq %u\n",
  248. t_cal_freq, pre_lo_freq);
  249. if (c->frequency <= 440000000) {
  250. l_band = 0;
  251. lna_band = 0;
  252. } else if (c->frequency <= 484000000) {
  253. l_band = 1;
  254. lna_band = 1;
  255. } else if (c->frequency <= 533000000) {
  256. l_band = 1;
  257. lna_band = 2;
  258. } else if (c->frequency <= 587000000) {
  259. l_band = 1;
  260. lna_band = 3;
  261. } else if (c->frequency <= 645000000) {
  262. l_band = 1;
  263. lna_band = 4;
  264. } else if (c->frequency <= 710000000) {
  265. l_band = 1;
  266. lna_band = 5;
  267. } else if (c->frequency <= 782000000) {
  268. l_band = 1;
  269. lna_band = 6;
  270. } else if (c->frequency <= 860000000) {
  271. l_band = 1;
  272. lna_band = 7;
  273. } else if (c->frequency <= 1492000000) {
  274. l_band = 1;
  275. lna_band = 0;
  276. } else if (c->frequency <= 1685000000) {
  277. l_band = 1;
  278. lna_band = 1;
  279. } else {
  280. ret = -EINVAL;
  281. goto err;
  282. }
  283. /* XXX: latest windows driver does not set that at all */
  284. ret = regmap_write(dev->regmap, 0x80ee06, lna_band);
  285. if (ret)
  286. goto err;
  287. if (c->bandwidth_hz <= 5000000)
  288. u8tmp = 0;
  289. else if (c->bandwidth_hz <= 6000000)
  290. u8tmp = 2;
  291. else if (c->bandwidth_hz <= 7000000)
  292. u8tmp = 4;
  293. else
  294. u8tmp = 6; /* 8000000 */
  295. ret = regmap_write(dev->regmap, 0x80ec56, u8tmp);
  296. if (ret)
  297. goto err;
  298. /* XXX: latest windows driver sets different value (a8 != 68) */
  299. ret = regmap_write(dev->regmap, 0x80ec4c, 0xa0 | (l_band << 3));
  300. if (ret)
  301. goto err;
  302. ret = regmap_write(dev->regmap, 0x80ec4d, (t_cal_freq >> 0) & 0xff);
  303. if (ret)
  304. goto err;
  305. ret = regmap_write(dev->regmap, 0x80ec4e, (t_cal_freq >> 8) & 0xff);
  306. if (ret)
  307. goto err;
  308. ret = regmap_write(dev->regmap, 0x80011e, (pre_lo_freq >> 0) & 0xff);
  309. if (ret)
  310. goto err;
  311. ret = regmap_write(dev->regmap, 0x80011f, (pre_lo_freq >> 8) & 0xff);
  312. if (ret)
  313. goto err;
  314. return 0;
  315. err:
  316. dev_dbg(&dev->client->dev, "failed %d\n", ret);
  317. return ret;
  318. }
  319. static const struct dvb_tuner_ops it913x_tuner_ops = {
  320. .info = {
  321. .name = "ITE IT913X",
  322. .frequency_min = 174000000,
  323. .frequency_max = 862000000,
  324. },
  325. .init = it913x_init,
  326. .sleep = it913x_sleep,
  327. .set_params = it913x_set_params,
  328. };
  329. static int it913x_probe(struct i2c_client *client,
  330. const struct i2c_device_id *id)
  331. {
  332. struct it913x_config *cfg = client->dev.platform_data;
  333. struct dvb_frontend *fe = cfg->fe;
  334. struct it913x_dev *dev;
  335. int ret;
  336. char *chip_ver_str;
  337. static const struct regmap_config regmap_config = {
  338. .reg_bits = 24,
  339. .val_bits = 8,
  340. };
  341. dev = kzalloc(sizeof(struct it913x_dev), GFP_KERNEL);
  342. if (dev == NULL) {
  343. ret = -ENOMEM;
  344. dev_err(&client->dev, "kzalloc() failed\n");
  345. goto err;
  346. }
  347. dev->client = client;
  348. dev->fe = cfg->fe;
  349. dev->chip_ver = cfg->chip_ver;
  350. dev->role = cfg->role;
  351. dev->regmap = regmap_init_i2c(client, &regmap_config);
  352. if (IS_ERR(dev->regmap)) {
  353. ret = PTR_ERR(dev->regmap);
  354. goto err_kfree;
  355. }
  356. fe->tuner_priv = dev;
  357. memcpy(&fe->ops.tuner_ops, &it913x_tuner_ops,
  358. sizeof(struct dvb_tuner_ops));
  359. i2c_set_clientdata(client, dev);
  360. if (dev->chip_ver == 1)
  361. chip_ver_str = "AX";
  362. else if (dev->chip_ver == 2)
  363. chip_ver_str = "BX";
  364. else
  365. chip_ver_str = "??";
  366. dev_info(&dev->client->dev, "ITE IT913X %s successfully attached\n",
  367. chip_ver_str);
  368. dev_dbg(&dev->client->dev, "chip_ver %u, role %u\n",
  369. dev->chip_ver, dev->role);
  370. return 0;
  371. err_kfree:
  372. kfree(dev);
  373. err:
  374. dev_dbg(&client->dev, "failed %d\n", ret);
  375. return ret;
  376. }
  377. static int it913x_remove(struct i2c_client *client)
  378. {
  379. struct it913x_dev *dev = i2c_get_clientdata(client);
  380. struct dvb_frontend *fe = dev->fe;
  381. dev_dbg(&client->dev, "\n");
  382. memset(&fe->ops.tuner_ops, 0, sizeof(struct dvb_tuner_ops));
  383. fe->tuner_priv = NULL;
  384. regmap_exit(dev->regmap);
  385. kfree(dev);
  386. return 0;
  387. }
  388. static const struct i2c_device_id it913x_id_table[] = {
  389. {"it913x", 0},
  390. {}
  391. };
  392. MODULE_DEVICE_TABLE(i2c, it913x_id_table);
  393. static struct i2c_driver it913x_driver = {
  394. .driver = {
  395. .name = "it913x",
  396. },
  397. .probe = it913x_probe,
  398. .remove = it913x_remove,
  399. .id_table = it913x_id_table,
  400. };
  401. module_i2c_driver(it913x_driver);
  402. MODULE_DESCRIPTION("ITE IT913X silicon tuner driver");
  403. MODULE_AUTHOR("Antti Palosaari <crope@iki.fi>");
  404. MODULE_LICENSE("GPL");