it913x.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467
  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. #include "it913x.h"
  19. #include <linux/platform_device.h>
  20. #include <linux/regmap.h>
  21. struct it913x_dev {
  22. struct platform_device *pdev;
  23. struct regmap *regmap;
  24. struct dvb_frontend *fe;
  25. u8 chip_ver:2;
  26. u8 role:2;
  27. u16 xtal;
  28. u8 fdiv;
  29. u8 clk_mode;
  30. u32 fn_min;
  31. bool active;
  32. };
  33. static int it913x_init(struct dvb_frontend *fe)
  34. {
  35. struct it913x_dev *dev = fe->tuner_priv;
  36. struct platform_device *pdev = dev->pdev;
  37. int ret;
  38. unsigned int utmp;
  39. u8 iqik_m_cal, nv_val, buf[2];
  40. static const u8 nv[] = {48, 32, 24, 16, 12, 8, 6, 4, 2};
  41. unsigned long timeout;
  42. dev_dbg(&pdev->dev, "role %u\n", dev->role);
  43. ret = regmap_write(dev->regmap, 0x80ec4c, 0x68);
  44. if (ret)
  45. goto err;
  46. usleep_range(10000, 100000);
  47. ret = regmap_read(dev->regmap, 0x80ec86, &utmp);
  48. if (ret)
  49. goto err;
  50. switch (utmp) {
  51. case 0:
  52. /* 12.000 MHz */
  53. dev->clk_mode = utmp;
  54. dev->xtal = 2000;
  55. dev->fdiv = 3;
  56. iqik_m_cal = 16;
  57. break;
  58. case 1:
  59. /* 20.480 MHz */
  60. dev->clk_mode = utmp;
  61. dev->xtal = 640;
  62. dev->fdiv = 1;
  63. iqik_m_cal = 6;
  64. break;
  65. default:
  66. dev_err(&pdev->dev, "unknown clock identifier %d\n", utmp);
  67. goto err;
  68. }
  69. ret = regmap_read(dev->regmap, 0x80ed03, &utmp);
  70. if (ret)
  71. goto err;
  72. else if (utmp < ARRAY_SIZE(nv))
  73. nv_val = nv[utmp];
  74. else
  75. nv_val = 2;
  76. #define TIMEOUT 50
  77. timeout = jiffies + msecs_to_jiffies(TIMEOUT);
  78. while (!time_after(jiffies, timeout)) {
  79. ret = regmap_bulk_read(dev->regmap, 0x80ed23, buf, 2);
  80. if (ret)
  81. goto err;
  82. utmp = (buf[1] << 8) | (buf[0] << 0);
  83. if (utmp)
  84. break;
  85. }
  86. dev_dbg(&pdev->dev, "r_fbc_m_bdry took %u ms, val %u\n",
  87. jiffies_to_msecs(jiffies) -
  88. (jiffies_to_msecs(timeout) - TIMEOUT), utmp);
  89. dev->fn_min = dev->xtal * utmp;
  90. dev->fn_min /= (dev->fdiv * nv_val);
  91. dev->fn_min *= 1000;
  92. dev_dbg(&pdev->dev, "fn_min %u\n", dev->fn_min);
  93. /*
  94. * Chip version BX never sets that flag so we just wait 50ms in that
  95. * case. It is possible poll BX similarly than AX and then timeout in
  96. * order to get 50ms delay, but that causes about 120 extra I2C
  97. * messages. As for now, we just wait and reduce IO.
  98. */
  99. if (dev->chip_ver == 1) {
  100. #define TIMEOUT 50
  101. timeout = jiffies + msecs_to_jiffies(TIMEOUT);
  102. while (!time_after(jiffies, timeout)) {
  103. ret = regmap_read(dev->regmap, 0x80ec82, &utmp);
  104. if (ret)
  105. goto err;
  106. if (utmp)
  107. break;
  108. }
  109. dev_dbg(&pdev->dev, "p_tsm_init_mode took %u ms, val %u\n",
  110. jiffies_to_msecs(jiffies) -
  111. (jiffies_to_msecs(timeout) - TIMEOUT), utmp);
  112. } else {
  113. msleep(50);
  114. }
  115. ret = regmap_write(dev->regmap, 0x80ed81, iqik_m_cal);
  116. if (ret)
  117. goto err;
  118. ret = regmap_write(dev->regmap, 0x80ec57, 0x00);
  119. if (ret)
  120. goto err;
  121. ret = regmap_write(dev->regmap, 0x80ec58, 0x00);
  122. if (ret)
  123. goto err;
  124. ret = regmap_write(dev->regmap, 0x80ec40, 0x01);
  125. if (ret)
  126. goto err;
  127. dev->active = true;
  128. return 0;
  129. err:
  130. dev_dbg(&pdev->dev, "failed %d\n", ret);
  131. return ret;
  132. }
  133. static int it913x_sleep(struct dvb_frontend *fe)
  134. {
  135. struct it913x_dev *dev = fe->tuner_priv;
  136. struct platform_device *pdev = dev->pdev;
  137. int ret, len;
  138. dev_dbg(&pdev->dev, "role %u\n", dev->role);
  139. dev->active = false;
  140. ret = regmap_bulk_write(dev->regmap, 0x80ec40, "\x00", 1);
  141. if (ret)
  142. goto err;
  143. /*
  144. * Writing '0x00' to master tuner register '0x80ec08' causes slave tuner
  145. * communication lost. Due to that, we cannot put master full sleep.
  146. */
  147. if (dev->role == IT913X_ROLE_DUAL_MASTER)
  148. len = 4;
  149. else
  150. len = 15;
  151. dev_dbg(&pdev->dev, "role %u, len %d\n", dev->role, len);
  152. ret = regmap_bulk_write(dev->regmap, 0x80ec02,
  153. "\x3f\x1f\x3f\x3e\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00",
  154. len);
  155. if (ret)
  156. goto err;
  157. ret = regmap_bulk_write(dev->regmap, 0x80ec12, "\x00\x00\x00\x00", 4);
  158. if (ret)
  159. goto err;
  160. ret = regmap_bulk_write(dev->regmap, 0x80ec17,
  161. "\x00\x00\x00\x00\x00\x00\x00\x00\x00", 9);
  162. if (ret)
  163. goto err;
  164. ret = regmap_bulk_write(dev->regmap, 0x80ec22,
  165. "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00", 10);
  166. if (ret)
  167. goto err;
  168. ret = regmap_bulk_write(dev->regmap, 0x80ec20, "\x00", 1);
  169. if (ret)
  170. goto err;
  171. ret = regmap_bulk_write(dev->regmap, 0x80ec3f, "\x01", 1);
  172. if (ret)
  173. goto err;
  174. return 0;
  175. err:
  176. dev_dbg(&pdev->dev, "failed %d\n", ret);
  177. return ret;
  178. }
  179. static int it913x_set_params(struct dvb_frontend *fe)
  180. {
  181. struct it913x_dev *dev = fe->tuner_priv;
  182. struct platform_device *pdev = dev->pdev;
  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(&pdev->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(&pdev->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(&pdev->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_hz = 174 * MHz,
  323. .frequency_max_hz = 862 * MHz,
  324. },
  325. .init = it913x_init,
  326. .sleep = it913x_sleep,
  327. .set_params = it913x_set_params,
  328. };
  329. static int it913x_probe(struct platform_device *pdev)
  330. {
  331. struct it913x_platform_data *pdata = pdev->dev.platform_data;
  332. struct dvb_frontend *fe = pdata->fe;
  333. struct it913x_dev *dev;
  334. const struct platform_device_id *id = platform_get_device_id(pdev);
  335. int ret;
  336. char *chip_ver_str;
  337. dev = kzalloc(sizeof(struct it913x_dev), GFP_KERNEL);
  338. if (dev == NULL) {
  339. ret = -ENOMEM;
  340. dev_err(&pdev->dev, "kzalloc() failed\n");
  341. goto err;
  342. }
  343. dev->pdev = pdev;
  344. dev->regmap = pdata->regmap;
  345. dev->fe = pdata->fe;
  346. dev->chip_ver = id->driver_data;
  347. dev->role = pdata->role;
  348. fe->tuner_priv = dev;
  349. memcpy(&fe->ops.tuner_ops, &it913x_tuner_ops,
  350. sizeof(struct dvb_tuner_ops));
  351. platform_set_drvdata(pdev, dev);
  352. if (dev->chip_ver == 1)
  353. chip_ver_str = "AX";
  354. else if (dev->chip_ver == 2)
  355. chip_ver_str = "BX";
  356. else
  357. chip_ver_str = "??";
  358. dev_info(&pdev->dev, "ITE IT913X %s successfully attached\n",
  359. chip_ver_str);
  360. dev_dbg(&pdev->dev, "chip_ver %u, role %u\n", dev->chip_ver, dev->role);
  361. return 0;
  362. err:
  363. dev_dbg(&pdev->dev, "failed %d\n", ret);
  364. return ret;
  365. }
  366. static int it913x_remove(struct platform_device *pdev)
  367. {
  368. struct it913x_dev *dev = platform_get_drvdata(pdev);
  369. struct dvb_frontend *fe = dev->fe;
  370. dev_dbg(&pdev->dev, "\n");
  371. memset(&fe->ops.tuner_ops, 0, sizeof(struct dvb_tuner_ops));
  372. fe->tuner_priv = NULL;
  373. kfree(dev);
  374. return 0;
  375. }
  376. static const struct platform_device_id it913x_id_table[] = {
  377. {"it9133ax-tuner", 1},
  378. {"it9133bx-tuner", 2},
  379. {},
  380. };
  381. MODULE_DEVICE_TABLE(platform, it913x_id_table);
  382. static struct platform_driver it913x_driver = {
  383. .driver = {
  384. .name = "it913x",
  385. .suppress_bind_attrs = true,
  386. },
  387. .probe = it913x_probe,
  388. .remove = it913x_remove,
  389. .id_table = it913x_id_table,
  390. };
  391. module_platform_driver(it913x_driver);
  392. MODULE_DESCRIPTION("ITE IT913X silicon tuner driver");
  393. MODULE_AUTHOR("Antti Palosaari <crope@iki.fi>");
  394. MODULE_LICENSE("GPL");