qm1d1c0042.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465
  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. #define QM1D1C0042_NUM_REG_ROWS 2
  33. static const u8
  34. reg_initval[QM1D1C0042_NUM_REG_ROWS][QM1D1C0042_NUM_REGS] = { {
  35. 0x48, 0x1c, 0xa0, 0x10, 0xbc, 0xc5, 0x20, 0x33,
  36. 0x06, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00,
  37. 0x00, 0xff, 0xf3, 0x00, 0x2a, 0x64, 0xa6, 0x86,
  38. 0x8c, 0xcf, 0xb8, 0xf1, 0xa8, 0xf2, 0x89, 0x00
  39. }, {
  40. 0x68, 0x1c, 0xc0, 0x10, 0xbc, 0xc1, 0x11, 0x33,
  41. 0x03, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00,
  42. 0x00, 0xff, 0xf3, 0x00, 0x3f, 0x25, 0x5c, 0xd6,
  43. 0x55, 0xcf, 0x95, 0xf6, 0x36, 0xf2, 0x09, 0x00
  44. }
  45. };
  46. static int reg_index;
  47. static const struct qm1d1c0042_config default_cfg = {
  48. .xtal_freq = 16000,
  49. .lpf = 1,
  50. .fast_srch = 0,
  51. .lpf_wait = 20,
  52. .fast_srch_wait = 4,
  53. .normal_srch_wait = 15,
  54. };
  55. struct qm1d1c0042_state {
  56. struct qm1d1c0042_config cfg;
  57. struct i2c_client *i2c;
  58. u8 regs[QM1D1C0042_NUM_REGS];
  59. };
  60. static struct qm1d1c0042_state *cfg_to_state(struct qm1d1c0042_config *c)
  61. {
  62. return container_of(c, struct qm1d1c0042_state, cfg);
  63. }
  64. static int reg_write(struct qm1d1c0042_state *state, u8 reg, u8 val)
  65. {
  66. u8 wbuf[2] = { reg, val };
  67. int ret;
  68. ret = i2c_master_send(state->i2c, wbuf, sizeof(wbuf));
  69. if (ret >= 0 && ret < sizeof(wbuf))
  70. ret = -EIO;
  71. return (ret == sizeof(wbuf)) ? 0 : ret;
  72. }
  73. static int reg_read(struct qm1d1c0042_state *state, u8 reg, u8 *val)
  74. {
  75. struct i2c_msg msgs[2] = {
  76. {
  77. .addr = state->i2c->addr,
  78. .flags = 0,
  79. .buf = &reg,
  80. .len = 1,
  81. },
  82. {
  83. .addr = state->i2c->addr,
  84. .flags = I2C_M_RD,
  85. .buf = val,
  86. .len = 1,
  87. },
  88. };
  89. int ret;
  90. ret = i2c_transfer(state->i2c->adapter, msgs, ARRAY_SIZE(msgs));
  91. if (ret >= 0 && ret < ARRAY_SIZE(msgs))
  92. ret = -EIO;
  93. return (ret == ARRAY_SIZE(msgs)) ? 0 : ret;
  94. }
  95. static int qm1d1c0042_set_srch_mode(struct qm1d1c0042_state *state, bool fast)
  96. {
  97. if (fast)
  98. state->regs[0x03] |= 0x01; /* set fast search mode */
  99. else
  100. state->regs[0x03] &= ~0x01 & 0xff;
  101. return reg_write(state, 0x03, state->regs[0x03]);
  102. }
  103. static int qm1d1c0042_wakeup(struct qm1d1c0042_state *state)
  104. {
  105. int ret;
  106. state->regs[0x01] |= 1 << 3; /* BB_Reg_enable */
  107. state->regs[0x01] &= (~(1 << 0)) & 0xff; /* NORMAL (wake-up) */
  108. state->regs[0x05] &= (~(1 << 3)) & 0xff; /* pfd_rst NORMAL */
  109. ret = reg_write(state, 0x01, state->regs[0x01]);
  110. if (ret == 0)
  111. ret = reg_write(state, 0x05, state->regs[0x05]);
  112. if (ret < 0)
  113. dev_warn(&state->i2c->dev, "(%s) failed. [adap%d-fe%d]\n",
  114. __func__, state->cfg.fe->dvb->num, state->cfg.fe->id);
  115. return ret;
  116. }
  117. /* tuner_ops */
  118. static int qm1d1c0042_set_config(struct dvb_frontend *fe, void *priv_cfg)
  119. {
  120. struct qm1d1c0042_state *state;
  121. struct qm1d1c0042_config *cfg;
  122. state = fe->tuner_priv;
  123. cfg = priv_cfg;
  124. if (cfg->fe)
  125. state->cfg.fe = cfg->fe;
  126. if (cfg->xtal_freq != QM1D1C0042_CFG_XTAL_DFLT)
  127. dev_warn(&state->i2c->dev,
  128. "(%s) changing xtal_freq not supported. ", __func__);
  129. state->cfg.xtal_freq = default_cfg.xtal_freq;
  130. state->cfg.lpf = cfg->lpf;
  131. state->cfg.fast_srch = cfg->fast_srch;
  132. if (cfg->lpf_wait != QM1D1C0042_CFG_WAIT_DFLT)
  133. state->cfg.lpf_wait = cfg->lpf_wait;
  134. else
  135. state->cfg.lpf_wait = default_cfg.lpf_wait;
  136. if (cfg->fast_srch_wait != QM1D1C0042_CFG_WAIT_DFLT)
  137. state->cfg.fast_srch_wait = cfg->fast_srch_wait;
  138. else
  139. state->cfg.fast_srch_wait = default_cfg.fast_srch_wait;
  140. if (cfg->normal_srch_wait != QM1D1C0042_CFG_WAIT_DFLT)
  141. state->cfg.normal_srch_wait = cfg->normal_srch_wait;
  142. else
  143. state->cfg.normal_srch_wait = default_cfg.normal_srch_wait;
  144. return 0;
  145. }
  146. /* divisor, vco_band parameters */
  147. /* {maxfreq, param1(band?), param2(div?) */
  148. static const u32 conv_table[9][3] = {
  149. { 2151000, 1, 7 },
  150. { 1950000, 1, 6 },
  151. { 1800000, 1, 5 },
  152. { 1600000, 1, 4 },
  153. { 1450000, 1, 3 },
  154. { 1250000, 1, 2 },
  155. { 1200000, 0, 7 },
  156. { 975000, 0, 6 },
  157. { 950000, 0, 0 }
  158. };
  159. static int qm1d1c0042_set_params(struct dvb_frontend *fe)
  160. {
  161. struct qm1d1c0042_state *state;
  162. u32 freq;
  163. int i, ret;
  164. u8 val, mask;
  165. u32 a, sd;
  166. s32 b;
  167. state = fe->tuner_priv;
  168. freq = fe->dtv_property_cache.frequency;
  169. state->regs[0x08] &= 0xf0;
  170. state->regs[0x08] |= 0x09;
  171. state->regs[0x13] &= 0x9f;
  172. state->regs[0x13] |= 0x20;
  173. /* div2/vco_band */
  174. val = state->regs[0x02] & 0x0f;
  175. for (i = 0; i < 8; i++)
  176. if (freq < conv_table[i][0] && freq >= conv_table[i + 1][0]) {
  177. val |= conv_table[i][1] << 7;
  178. val |= conv_table[i][2] << 4;
  179. break;
  180. }
  181. ret = reg_write(state, 0x02, val);
  182. if (ret < 0)
  183. return ret;
  184. a = (freq + state->cfg.xtal_freq / 2) / state->cfg.xtal_freq;
  185. state->regs[0x06] &= 0x40;
  186. state->regs[0x06] |= (a - 12) / 4;
  187. ret = reg_write(state, 0x06, state->regs[0x06]);
  188. if (ret < 0)
  189. return ret;
  190. state->regs[0x07] &= 0xf0;
  191. state->regs[0x07] |= (a - 4 * ((a - 12) / 4 + 1) - 5) & 0x0f;
  192. ret = reg_write(state, 0x07, state->regs[0x07]);
  193. if (ret < 0)
  194. return ret;
  195. /* LPF */
  196. val = state->regs[0x08];
  197. if (state->cfg.lpf) {
  198. /* LPF_CLK, LPF_FC */
  199. val &= 0xf0;
  200. val |= 0x02;
  201. }
  202. ret = reg_write(state, 0x08, val);
  203. if (ret < 0)
  204. return ret;
  205. /*
  206. * b = (freq / state->cfg.xtal_freq - a) << 20;
  207. * sd = b (b >= 0)
  208. * 1<<22 + b (b < 0)
  209. */
  210. b = (s32)div64_s64(((s64) freq) << 20, state->cfg.xtal_freq)
  211. - (((s64) a) << 20);
  212. if (b >= 0)
  213. sd = b;
  214. else
  215. sd = (1 << 22) + b;
  216. state->regs[0x09] &= 0xc0;
  217. state->regs[0x09] |= (sd >> 16) & 0x3f;
  218. state->regs[0x0a] = (sd >> 8) & 0xff;
  219. state->regs[0x0b] = sd & 0xff;
  220. ret = reg_write(state, 0x09, state->regs[0x09]);
  221. if (ret == 0)
  222. ret = reg_write(state, 0x0a, state->regs[0x0a]);
  223. if (ret == 0)
  224. ret = reg_write(state, 0x0b, state->regs[0x0b]);
  225. if (ret != 0)
  226. return ret;
  227. if (!state->cfg.lpf) {
  228. /* CSEL_Offset */
  229. ret = reg_write(state, 0x13, state->regs[0x13]);
  230. if (ret < 0)
  231. return ret;
  232. }
  233. /* VCO_TM, LPF_TM */
  234. mask = state->cfg.lpf ? 0x3f : 0x7f;
  235. val = state->regs[0x0c] & mask;
  236. ret = reg_write(state, 0x0c, val);
  237. if (ret < 0)
  238. return ret;
  239. usleep_range(2000, 3000);
  240. val = state->regs[0x0c] | ~mask;
  241. ret = reg_write(state, 0x0c, val);
  242. if (ret < 0)
  243. return ret;
  244. if (state->cfg.lpf)
  245. msleep(state->cfg.lpf_wait);
  246. else if (state->regs[0x03] & 0x01)
  247. msleep(state->cfg.fast_srch_wait);
  248. else
  249. msleep(state->cfg.normal_srch_wait);
  250. if (state->cfg.lpf) {
  251. /* LPF_FC */
  252. ret = reg_write(state, 0x08, 0x09);
  253. if (ret < 0)
  254. return ret;
  255. /* CSEL_Offset */
  256. ret = reg_write(state, 0x13, state->regs[0x13]);
  257. if (ret < 0)
  258. return ret;
  259. }
  260. return 0;
  261. }
  262. static int qm1d1c0042_sleep(struct dvb_frontend *fe)
  263. {
  264. struct qm1d1c0042_state *state;
  265. int ret;
  266. state = fe->tuner_priv;
  267. state->regs[0x01] &= (~(1 << 3)) & 0xff; /* BB_Reg_disable */
  268. state->regs[0x01] |= 1 << 0; /* STDBY */
  269. state->regs[0x05] |= 1 << 3; /* pfd_rst STANDBY */
  270. ret = reg_write(state, 0x05, state->regs[0x05]);
  271. if (ret == 0)
  272. ret = reg_write(state, 0x01, state->regs[0x01]);
  273. if (ret < 0)
  274. dev_warn(&state->i2c->dev, "(%s) failed. [adap%d-fe%d]\n",
  275. __func__, fe->dvb->num, fe->id);
  276. return ret;
  277. }
  278. static int qm1d1c0042_init(struct dvb_frontend *fe)
  279. {
  280. struct qm1d1c0042_state *state;
  281. u8 val;
  282. int i, ret;
  283. state = fe->tuner_priv;
  284. reg_write(state, 0x01, 0x0c);
  285. reg_write(state, 0x01, 0x0c);
  286. ret = reg_write(state, 0x01, 0x0c); /* soft reset on */
  287. if (ret < 0)
  288. goto failed;
  289. usleep_range(2000, 3000);
  290. ret = reg_write(state, 0x01, 0x1c); /* soft reset off */
  291. if (ret < 0)
  292. goto failed;
  293. /* check ID and choose initial registers corresponding ID */
  294. ret = reg_read(state, 0x00, &val);
  295. if (ret < 0)
  296. goto failed;
  297. for (reg_index = 0; reg_index < QM1D1C0042_NUM_REG_ROWS;
  298. reg_index++) {
  299. if (val == reg_initval[reg_index][0x00])
  300. break;
  301. }
  302. if (reg_index >= QM1D1C0042_NUM_REG_ROWS)
  303. goto failed;
  304. memcpy(state->regs, reg_initval[reg_index], QM1D1C0042_NUM_REGS);
  305. usleep_range(2000, 3000);
  306. state->regs[0x0c] |= 0x40;
  307. ret = reg_write(state, 0x0c, state->regs[0x0c]);
  308. if (ret < 0)
  309. goto failed;
  310. msleep(state->cfg.lpf_wait);
  311. /* set all writable registers */
  312. for (i = 1; i <= 0x0c ; i++) {
  313. ret = reg_write(state, i, state->regs[i]);
  314. if (ret < 0)
  315. goto failed;
  316. }
  317. for (i = 0x11; i < QM1D1C0042_NUM_REGS; i++) {
  318. ret = reg_write(state, i, state->regs[i]);
  319. if (ret < 0)
  320. goto failed;
  321. }
  322. ret = qm1d1c0042_wakeup(state);
  323. if (ret < 0)
  324. goto failed;
  325. ret = qm1d1c0042_set_srch_mode(state, state->cfg.fast_srch);
  326. if (ret < 0)
  327. goto failed;
  328. return ret;
  329. failed:
  330. dev_warn(&state->i2c->dev, "(%s) failed. [adap%d-fe%d]\n",
  331. __func__, fe->dvb->num, fe->id);
  332. return ret;
  333. }
  334. /* I2C driver functions */
  335. static const struct dvb_tuner_ops qm1d1c0042_ops = {
  336. .info = {
  337. .name = "Sharp QM1D1C0042",
  338. .frequency_min = 950000,
  339. .frequency_max = 2150000,
  340. },
  341. .init = qm1d1c0042_init,
  342. .sleep = qm1d1c0042_sleep,
  343. .set_config = qm1d1c0042_set_config,
  344. .set_params = qm1d1c0042_set_params,
  345. };
  346. static int qm1d1c0042_probe(struct i2c_client *client,
  347. const struct i2c_device_id *id)
  348. {
  349. struct qm1d1c0042_state *state;
  350. struct qm1d1c0042_config *cfg;
  351. struct dvb_frontend *fe;
  352. state = kzalloc(sizeof(*state), GFP_KERNEL);
  353. if (!state)
  354. return -ENOMEM;
  355. state->i2c = client;
  356. cfg = client->dev.platform_data;
  357. fe = cfg->fe;
  358. fe->tuner_priv = state;
  359. qm1d1c0042_set_config(fe, cfg);
  360. memcpy(&fe->ops.tuner_ops, &qm1d1c0042_ops, sizeof(qm1d1c0042_ops));
  361. i2c_set_clientdata(client, &state->cfg);
  362. dev_info(&client->dev, "Sharp QM1D1C0042 attached.\n");
  363. return 0;
  364. }
  365. static int qm1d1c0042_remove(struct i2c_client *client)
  366. {
  367. struct qm1d1c0042_state *state;
  368. state = cfg_to_state(i2c_get_clientdata(client));
  369. state->cfg.fe->tuner_priv = NULL;
  370. kfree(state);
  371. return 0;
  372. }
  373. static const struct i2c_device_id qm1d1c0042_id[] = {
  374. {"qm1d1c0042", 0},
  375. {}
  376. };
  377. MODULE_DEVICE_TABLE(i2c, qm1d1c0042_id);
  378. static struct i2c_driver qm1d1c0042_driver = {
  379. .driver = {
  380. .name = "qm1d1c0042",
  381. },
  382. .probe = qm1d1c0042_probe,
  383. .remove = qm1d1c0042_remove,
  384. .id_table = qm1d1c0042_id,
  385. };
  386. module_i2c_driver(qm1d1c0042_driver);
  387. MODULE_DESCRIPTION("Sharp QM1D1C0042 tuner");
  388. MODULE_AUTHOR("Akihiro TSUKADA");
  389. MODULE_LICENSE("GPL");