qm1d1c0042.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448
  1. /*
  2. * Sharp QM1D1C0042 8PSK tuner driver
  3. *
  4. * Copyright (C) 2014 Akihiro Tsukada <tskd08@gmail.com>
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License as
  8. * published by the Free Software Foundation version 2.
  9. *
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. */
  16. /*
  17. * NOTICE:
  18. * As the disclosed information on the chip is very limited,
  19. * this driver lacks some features, including chip config like IF freq.
  20. * It assumes that users of this driver (such as a PCI bridge of
  21. * DTV receiver cards) know the relevant info and
  22. * configure the chip via I2C if necessary.
  23. *
  24. * Currently, PT3 driver is the only one that uses this driver,
  25. * and contains init/config code in its firmware.
  26. * Thus some part of the code might be dependent on PT3 specific config.
  27. */
  28. #include <linux/kernel.h>
  29. #include <linux/math64.h>
  30. #include "qm1d1c0042.h"
  31. #define QM1D1C0042_NUM_REGS 0x20
  32. static const u8 reg_initval[QM1D1C0042_NUM_REGS] = {
  33. 0x48, 0x1c, 0xa0, 0x10, 0xbc, 0xc5, 0x20, 0x33,
  34. 0x06, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00,
  35. 0x00, 0xff, 0xf3, 0x00, 0x2a, 0x64, 0xa6, 0x86,
  36. 0x8c, 0xcf, 0xb8, 0xf1, 0xa8, 0xf2, 0x89, 0x00
  37. };
  38. static const struct qm1d1c0042_config default_cfg = {
  39. .xtal_freq = 16000,
  40. .lpf = 1,
  41. .fast_srch = 0,
  42. .lpf_wait = 20,
  43. .fast_srch_wait = 4,
  44. .normal_srch_wait = 15,
  45. };
  46. struct qm1d1c0042_state {
  47. struct qm1d1c0042_config cfg;
  48. struct i2c_client *i2c;
  49. u8 regs[QM1D1C0042_NUM_REGS];
  50. };
  51. static struct qm1d1c0042_state *cfg_to_state(struct qm1d1c0042_config *c)
  52. {
  53. return container_of(c, struct qm1d1c0042_state, cfg);
  54. }
  55. static int reg_write(struct qm1d1c0042_state *state, u8 reg, u8 val)
  56. {
  57. u8 wbuf[2] = { reg, val };
  58. int ret;
  59. ret = i2c_master_send(state->i2c, wbuf, sizeof(wbuf));
  60. if (ret >= 0 && ret < sizeof(wbuf))
  61. ret = -EIO;
  62. return (ret == sizeof(wbuf)) ? 0 : ret;
  63. }
  64. static int reg_read(struct qm1d1c0042_state *state, u8 reg, u8 *val)
  65. {
  66. struct i2c_msg msgs[2] = {
  67. {
  68. .addr = state->i2c->addr,
  69. .flags = 0,
  70. .buf = &reg,
  71. .len = 1,
  72. },
  73. {
  74. .addr = state->i2c->addr,
  75. .flags = I2C_M_RD,
  76. .buf = val,
  77. .len = 1,
  78. },
  79. };
  80. int ret;
  81. ret = i2c_transfer(state->i2c->adapter, msgs, ARRAY_SIZE(msgs));
  82. if (ret >= 0 && ret < ARRAY_SIZE(msgs))
  83. ret = -EIO;
  84. return (ret == ARRAY_SIZE(msgs)) ? 0 : ret;
  85. }
  86. static int qm1d1c0042_set_srch_mode(struct qm1d1c0042_state *state, bool fast)
  87. {
  88. if (fast)
  89. state->regs[0x03] |= 0x01; /* set fast search mode */
  90. else
  91. state->regs[0x03] &= ~0x01 & 0xff;
  92. return reg_write(state, 0x03, state->regs[0x03]);
  93. }
  94. static int qm1d1c0042_wakeup(struct qm1d1c0042_state *state)
  95. {
  96. int ret;
  97. state->regs[0x01] |= 1 << 3; /* BB_Reg_enable */
  98. state->regs[0x01] &= (~(1 << 0)) & 0xff; /* NORMAL (wake-up) */
  99. state->regs[0x05] &= (~(1 << 3)) & 0xff; /* pfd_rst NORMAL */
  100. ret = reg_write(state, 0x01, state->regs[0x01]);
  101. if (ret == 0)
  102. ret = reg_write(state, 0x05, state->regs[0x05]);
  103. if (ret < 0)
  104. dev_warn(&state->i2c->dev, "(%s) failed. [adap%d-fe%d]\n",
  105. __func__, state->cfg.fe->dvb->num, state->cfg.fe->id);
  106. return ret;
  107. }
  108. /* tuner_ops */
  109. static int qm1d1c0042_set_config(struct dvb_frontend *fe, void *priv_cfg)
  110. {
  111. struct qm1d1c0042_state *state;
  112. struct qm1d1c0042_config *cfg;
  113. state = fe->tuner_priv;
  114. cfg = priv_cfg;
  115. if (cfg->fe)
  116. state->cfg.fe = cfg->fe;
  117. if (cfg->xtal_freq != QM1D1C0042_CFG_XTAL_DFLT)
  118. dev_warn(&state->i2c->dev,
  119. "(%s) changing xtal_freq not supported. ", __func__);
  120. state->cfg.xtal_freq = default_cfg.xtal_freq;
  121. state->cfg.lpf = cfg->lpf;
  122. state->cfg.fast_srch = cfg->fast_srch;
  123. if (cfg->lpf_wait != QM1D1C0042_CFG_WAIT_DFLT)
  124. state->cfg.lpf_wait = cfg->lpf_wait;
  125. else
  126. state->cfg.lpf_wait = default_cfg.lpf_wait;
  127. if (cfg->fast_srch_wait != QM1D1C0042_CFG_WAIT_DFLT)
  128. state->cfg.fast_srch_wait = cfg->fast_srch_wait;
  129. else
  130. state->cfg.fast_srch_wait = default_cfg.fast_srch_wait;
  131. if (cfg->normal_srch_wait != QM1D1C0042_CFG_WAIT_DFLT)
  132. state->cfg.normal_srch_wait = cfg->normal_srch_wait;
  133. else
  134. state->cfg.normal_srch_wait = default_cfg.normal_srch_wait;
  135. return 0;
  136. }
  137. /* divisor, vco_band parameters */
  138. /* {maxfreq, param1(band?), param2(div?) */
  139. static const u32 conv_table[9][3] = {
  140. { 2151000, 1, 7 },
  141. { 1950000, 1, 6 },
  142. { 1800000, 1, 5 },
  143. { 1600000, 1, 4 },
  144. { 1450000, 1, 3 },
  145. { 1250000, 1, 2 },
  146. { 1200000, 0, 7 },
  147. { 975000, 0, 6 },
  148. { 950000, 0, 0 }
  149. };
  150. static int qm1d1c0042_set_params(struct dvb_frontend *fe)
  151. {
  152. struct qm1d1c0042_state *state;
  153. u32 freq;
  154. int i, ret;
  155. u8 val, mask;
  156. u32 a, sd;
  157. s32 b;
  158. state = fe->tuner_priv;
  159. freq = fe->dtv_property_cache.frequency;
  160. state->regs[0x08] &= 0xf0;
  161. state->regs[0x08] |= 0x09;
  162. state->regs[0x13] &= 0x9f;
  163. state->regs[0x13] |= 0x20;
  164. /* div2/vco_band */
  165. val = state->regs[0x02] & 0x0f;
  166. for (i = 0; i < 8; i++)
  167. if (freq < conv_table[i][0] && freq >= conv_table[i + 1][0]) {
  168. val |= conv_table[i][1] << 7;
  169. val |= conv_table[i][2] << 4;
  170. break;
  171. }
  172. ret = reg_write(state, 0x02, val);
  173. if (ret < 0)
  174. return ret;
  175. a = (freq + state->cfg.xtal_freq / 2) / state->cfg.xtal_freq;
  176. state->regs[0x06] &= 0x40;
  177. state->regs[0x06] |= (a - 12) / 4;
  178. ret = reg_write(state, 0x06, state->regs[0x06]);
  179. if (ret < 0)
  180. return ret;
  181. state->regs[0x07] &= 0xf0;
  182. state->regs[0x07] |= (a - 4 * ((a - 12) / 4 + 1) - 5) & 0x0f;
  183. ret = reg_write(state, 0x07, state->regs[0x07]);
  184. if (ret < 0)
  185. return ret;
  186. /* LPF */
  187. val = state->regs[0x08];
  188. if (state->cfg.lpf) {
  189. /* LPF_CLK, LPF_FC */
  190. val &= 0xf0;
  191. val |= 0x02;
  192. }
  193. ret = reg_write(state, 0x08, val);
  194. if (ret < 0)
  195. return ret;
  196. /*
  197. * b = (freq / state->cfg.xtal_freq - a) << 20;
  198. * sd = b (b >= 0)
  199. * 1<<22 + b (b < 0)
  200. */
  201. b = (s32)div64_s64(((s64) freq) << 20, state->cfg.xtal_freq)
  202. - (((s64) a) << 20);
  203. if (b >= 0)
  204. sd = b;
  205. else
  206. sd = (1 << 22) + b;
  207. state->regs[0x09] &= 0xc0;
  208. state->regs[0x09] |= (sd >> 16) & 0x3f;
  209. state->regs[0x0a] = (sd >> 8) & 0xff;
  210. state->regs[0x0b] = sd & 0xff;
  211. ret = reg_write(state, 0x09, state->regs[0x09]);
  212. if (ret == 0)
  213. ret = reg_write(state, 0x0a, state->regs[0x0a]);
  214. if (ret == 0)
  215. ret = reg_write(state, 0x0b, state->regs[0x0b]);
  216. if (ret != 0)
  217. return ret;
  218. if (!state->cfg.lpf) {
  219. /* CSEL_Offset */
  220. ret = reg_write(state, 0x13, state->regs[0x13]);
  221. if (ret < 0)
  222. return ret;
  223. }
  224. /* VCO_TM, LPF_TM */
  225. mask = state->cfg.lpf ? 0x3f : 0x7f;
  226. val = state->regs[0x0c] & mask;
  227. ret = reg_write(state, 0x0c, val);
  228. if (ret < 0)
  229. return ret;
  230. usleep_range(2000, 3000);
  231. val = state->regs[0x0c] | ~mask;
  232. ret = reg_write(state, 0x0c, val);
  233. if (ret < 0)
  234. return ret;
  235. if (state->cfg.lpf)
  236. msleep(state->cfg.lpf_wait);
  237. else if (state->regs[0x03] & 0x01)
  238. msleep(state->cfg.fast_srch_wait);
  239. else
  240. msleep(state->cfg.normal_srch_wait);
  241. if (state->cfg.lpf) {
  242. /* LPF_FC */
  243. ret = reg_write(state, 0x08, 0x09);
  244. if (ret < 0)
  245. return ret;
  246. /* CSEL_Offset */
  247. ret = reg_write(state, 0x13, state->regs[0x13]);
  248. if (ret < 0)
  249. return ret;
  250. }
  251. return 0;
  252. }
  253. static int qm1d1c0042_sleep(struct dvb_frontend *fe)
  254. {
  255. struct qm1d1c0042_state *state;
  256. int ret;
  257. state = fe->tuner_priv;
  258. state->regs[0x01] &= (~(1 << 3)) & 0xff; /* BB_Reg_disable */
  259. state->regs[0x01] |= 1 << 0; /* STDBY */
  260. state->regs[0x05] |= 1 << 3; /* pfd_rst STANDBY */
  261. ret = reg_write(state, 0x05, state->regs[0x05]);
  262. if (ret == 0)
  263. ret = reg_write(state, 0x01, state->regs[0x01]);
  264. if (ret < 0)
  265. dev_warn(&state->i2c->dev, "(%s) failed. [adap%d-fe%d]\n",
  266. __func__, fe->dvb->num, fe->id);
  267. return ret;
  268. }
  269. static int qm1d1c0042_init(struct dvb_frontend *fe)
  270. {
  271. struct qm1d1c0042_state *state;
  272. u8 val;
  273. int i, ret;
  274. state = fe->tuner_priv;
  275. memcpy(state->regs, reg_initval, sizeof(reg_initval));
  276. reg_write(state, 0x01, 0x0c);
  277. reg_write(state, 0x01, 0x0c);
  278. ret = reg_write(state, 0x01, 0x0c); /* soft reset on */
  279. if (ret < 0)
  280. goto failed;
  281. usleep_range(2000, 3000);
  282. val = state->regs[0x01] | 0x10;
  283. ret = reg_write(state, 0x01, val); /* soft reset off */
  284. if (ret < 0)
  285. goto failed;
  286. /* check ID */
  287. ret = reg_read(state, 0x00, &val);
  288. if (ret < 0 || val != 0x48)
  289. goto failed;
  290. usleep_range(2000, 3000);
  291. state->regs[0x0c] |= 0x40;
  292. ret = reg_write(state, 0x0c, state->regs[0x0c]);
  293. if (ret < 0)
  294. goto failed;
  295. msleep(state->cfg.lpf_wait);
  296. /* set all writable registers */
  297. for (i = 1; i <= 0x0c ; i++) {
  298. ret = reg_write(state, i, state->regs[i]);
  299. if (ret < 0)
  300. goto failed;
  301. }
  302. for (i = 0x11; i < QM1D1C0042_NUM_REGS; i++) {
  303. ret = reg_write(state, i, state->regs[i]);
  304. if (ret < 0)
  305. goto failed;
  306. }
  307. ret = qm1d1c0042_wakeup(state);
  308. if (ret < 0)
  309. goto failed;
  310. ret = qm1d1c0042_set_srch_mode(state, state->cfg.fast_srch);
  311. if (ret < 0)
  312. goto failed;
  313. return ret;
  314. failed:
  315. dev_warn(&state->i2c->dev, "(%s) failed. [adap%d-fe%d]\n",
  316. __func__, fe->dvb->num, fe->id);
  317. return ret;
  318. }
  319. /* I2C driver functions */
  320. static const struct dvb_tuner_ops qm1d1c0042_ops = {
  321. .info = {
  322. .name = "Sharp QM1D1C0042",
  323. .frequency_min = 950000,
  324. .frequency_max = 2150000,
  325. },
  326. .init = qm1d1c0042_init,
  327. .sleep = qm1d1c0042_sleep,
  328. .set_config = qm1d1c0042_set_config,
  329. .set_params = qm1d1c0042_set_params,
  330. };
  331. static int qm1d1c0042_probe(struct i2c_client *client,
  332. const struct i2c_device_id *id)
  333. {
  334. struct qm1d1c0042_state *state;
  335. struct qm1d1c0042_config *cfg;
  336. struct dvb_frontend *fe;
  337. state = kzalloc(sizeof(*state), GFP_KERNEL);
  338. if (!state)
  339. return -ENOMEM;
  340. state->i2c = client;
  341. cfg = client->dev.platform_data;
  342. fe = cfg->fe;
  343. fe->tuner_priv = state;
  344. qm1d1c0042_set_config(fe, cfg);
  345. memcpy(&fe->ops.tuner_ops, &qm1d1c0042_ops, sizeof(qm1d1c0042_ops));
  346. i2c_set_clientdata(client, &state->cfg);
  347. dev_info(&client->dev, "Sharp QM1D1C0042 attached.\n");
  348. return 0;
  349. }
  350. static int qm1d1c0042_remove(struct i2c_client *client)
  351. {
  352. struct qm1d1c0042_state *state;
  353. state = cfg_to_state(i2c_get_clientdata(client));
  354. state->cfg.fe->tuner_priv = NULL;
  355. kfree(state);
  356. return 0;
  357. }
  358. static const struct i2c_device_id qm1d1c0042_id[] = {
  359. {"qm1d1c0042", 0},
  360. {}
  361. };
  362. MODULE_DEVICE_TABLE(i2c, qm1d1c0042_id);
  363. static struct i2c_driver qm1d1c0042_driver = {
  364. .driver = {
  365. .name = "qm1d1c0042",
  366. },
  367. .probe = qm1d1c0042_probe,
  368. .remove = qm1d1c0042_remove,
  369. .id_table = qm1d1c0042_id,
  370. };
  371. module_i2c_driver(qm1d1c0042_driver);
  372. MODULE_DESCRIPTION("Sharp QM1D1C0042 tuner");
  373. MODULE_AUTHOR("Akihiro TSUKADA");
  374. MODULE_LICENSE("GPL");