mxl301rf.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * MaxLinear MxL301RF OFDM tuner driver
  4. *
  5. * Copyright (C) 2014 Akihiro Tsukada <tskd08@gmail.com>
  6. */
  7. /*
  8. * NOTICE:
  9. * This driver is incomplete and lacks init/config of the chips,
  10. * as the necessary info is not disclosed.
  11. * Other features like get_if_frequency() are missing as well.
  12. * It assumes that users of this driver (such as a PCI bridge of
  13. * DTV receiver cards) properly init and configure the chip
  14. * via I2C *before* calling this driver's init() function.
  15. *
  16. * Currently, PT3 driver is the only one that uses this driver,
  17. * and contains init/config code in its firmware.
  18. * Thus some part of the code might be dependent on PT3 specific config.
  19. */
  20. #include <linux/kernel.h>
  21. #include "mxl301rf.h"
  22. struct mxl301rf_state {
  23. struct mxl301rf_config cfg;
  24. struct i2c_client *i2c;
  25. };
  26. static struct mxl301rf_state *cfg_to_state(struct mxl301rf_config *c)
  27. {
  28. return container_of(c, struct mxl301rf_state, cfg);
  29. }
  30. static int raw_write(struct mxl301rf_state *state, const u8 *buf, int len)
  31. {
  32. int ret;
  33. ret = i2c_master_send(state->i2c, buf, len);
  34. if (ret >= 0 && ret < len)
  35. ret = -EIO;
  36. return (ret == len) ? 0 : ret;
  37. }
  38. static int reg_write(struct mxl301rf_state *state, u8 reg, u8 val)
  39. {
  40. u8 buf[2] = { reg, val };
  41. return raw_write(state, buf, 2);
  42. }
  43. static int reg_read(struct mxl301rf_state *state, u8 reg, u8 *val)
  44. {
  45. u8 wbuf[2] = { 0xfb, reg };
  46. int ret;
  47. ret = raw_write(state, wbuf, sizeof(wbuf));
  48. if (ret == 0)
  49. ret = i2c_master_recv(state->i2c, val, 1);
  50. if (ret >= 0 && ret < 1)
  51. ret = -EIO;
  52. return (ret == 1) ? 0 : ret;
  53. }
  54. /* tuner_ops */
  55. /* get RSSI and update propery cache, set to *out in % */
  56. static int mxl301rf_get_rf_strength(struct dvb_frontend *fe, u16 *out)
  57. {
  58. struct mxl301rf_state *state;
  59. int ret;
  60. u8 rf_in1, rf_in2, rf_off1, rf_off2;
  61. u16 rf_in, rf_off;
  62. s64 level;
  63. struct dtv_fe_stats *rssi;
  64. rssi = &fe->dtv_property_cache.strength;
  65. rssi->len = 1;
  66. rssi->stat[0].scale = FE_SCALE_NOT_AVAILABLE;
  67. *out = 0;
  68. state = fe->tuner_priv;
  69. ret = reg_write(state, 0x14, 0x01);
  70. if (ret < 0)
  71. return ret;
  72. usleep_range(1000, 2000);
  73. ret = reg_read(state, 0x18, &rf_in1);
  74. if (ret == 0)
  75. ret = reg_read(state, 0x19, &rf_in2);
  76. if (ret == 0)
  77. ret = reg_read(state, 0xd6, &rf_off1);
  78. if (ret == 0)
  79. ret = reg_read(state, 0xd7, &rf_off2);
  80. if (ret != 0)
  81. return ret;
  82. rf_in = (rf_in2 & 0x07) << 8 | rf_in1;
  83. rf_off = (rf_off2 & 0x0f) << 5 | (rf_off1 >> 3);
  84. level = rf_in - rf_off - (113 << 3); /* x8 dBm */
  85. level = level * 1000 / 8;
  86. rssi->stat[0].svalue = level;
  87. rssi->stat[0].scale = FE_SCALE_DECIBEL;
  88. /* *out = (level - min) * 100 / (max - min) */
  89. *out = (rf_in - rf_off + (1 << 9) - 1) * 100 / ((5 << 9) - 2);
  90. return 0;
  91. }
  92. /* spur shift parameters */
  93. struct shf {
  94. u32 freq; /* Channel center frequency */
  95. u32 ofst_th; /* Offset frequency threshold */
  96. u8 shf_val; /* Spur shift value */
  97. u8 shf_dir; /* Spur shift direction */
  98. };
  99. static const struct shf shf_tab[] = {
  100. { 64500, 500, 0x92, 0x07 },
  101. { 191500, 300, 0xe2, 0x07 },
  102. { 205500, 500, 0x2c, 0x04 },
  103. { 212500, 500, 0x1e, 0x04 },
  104. { 226500, 500, 0xd4, 0x07 },
  105. { 99143, 500, 0x9c, 0x07 },
  106. { 173143, 500, 0xd4, 0x07 },
  107. { 191143, 300, 0xd4, 0x07 },
  108. { 207143, 500, 0xce, 0x07 },
  109. { 225143, 500, 0xce, 0x07 },
  110. { 243143, 500, 0xd4, 0x07 },
  111. { 261143, 500, 0xd4, 0x07 },
  112. { 291143, 500, 0xd4, 0x07 },
  113. { 339143, 500, 0x2c, 0x04 },
  114. { 117143, 500, 0x7a, 0x07 },
  115. { 135143, 300, 0x7a, 0x07 },
  116. { 153143, 500, 0x01, 0x07 }
  117. };
  118. struct reg_val {
  119. u8 reg;
  120. u8 val;
  121. } __attribute__ ((__packed__));
  122. static const struct reg_val set_idac[] = {
  123. { 0x0d, 0x00 },
  124. { 0x0c, 0x67 },
  125. { 0x6f, 0x89 },
  126. { 0x70, 0x0c },
  127. { 0x6f, 0x8a },
  128. { 0x70, 0x0e },
  129. { 0x6f, 0x8b },
  130. { 0x70, 0x1c },
  131. };
  132. static int mxl301rf_set_params(struct dvb_frontend *fe)
  133. {
  134. struct reg_val tune0[] = {
  135. { 0x13, 0x00 }, /* abort tuning */
  136. { 0x3b, 0xc0 },
  137. { 0x3b, 0x80 },
  138. { 0x10, 0x95 }, /* BW */
  139. { 0x1a, 0x05 },
  140. { 0x61, 0x00 }, /* spur shift value (placeholder) */
  141. { 0x62, 0xa0 } /* spur shift direction (placeholder) */
  142. };
  143. struct reg_val tune1[] = {
  144. { 0x11, 0x40 }, /* RF frequency L (placeholder) */
  145. { 0x12, 0x0e }, /* RF frequency H (placeholder) */
  146. { 0x13, 0x01 } /* start tune */
  147. };
  148. struct mxl301rf_state *state;
  149. u32 freq;
  150. u16 f;
  151. u32 tmp, div;
  152. int i, ret;
  153. state = fe->tuner_priv;
  154. freq = fe->dtv_property_cache.frequency;
  155. /* spur shift function (for analog) */
  156. for (i = 0; i < ARRAY_SIZE(shf_tab); i++) {
  157. if (freq >= (shf_tab[i].freq - shf_tab[i].ofst_th) * 1000 &&
  158. freq <= (shf_tab[i].freq + shf_tab[i].ofst_th) * 1000) {
  159. tune0[5].val = shf_tab[i].shf_val;
  160. tune0[6].val = 0xa0 | shf_tab[i].shf_dir;
  161. break;
  162. }
  163. }
  164. ret = raw_write(state, (u8 *) tune0, sizeof(tune0));
  165. if (ret < 0)
  166. goto failed;
  167. usleep_range(3000, 4000);
  168. /* convert freq to 10.6 fixed point float [MHz] */
  169. f = freq / 1000000;
  170. tmp = freq % 1000000;
  171. div = 1000000;
  172. for (i = 0; i < 6; i++) {
  173. f <<= 1;
  174. div >>= 1;
  175. if (tmp > div) {
  176. tmp -= div;
  177. f |= 1;
  178. }
  179. }
  180. if (tmp > 7812)
  181. f++;
  182. tune1[0].val = f & 0xff;
  183. tune1[1].val = f >> 8;
  184. ret = raw_write(state, (u8 *) tune1, sizeof(tune1));
  185. if (ret < 0)
  186. goto failed;
  187. msleep(31);
  188. ret = reg_write(state, 0x1a, 0x0d);
  189. if (ret < 0)
  190. goto failed;
  191. ret = raw_write(state, (u8 *) set_idac, sizeof(set_idac));
  192. if (ret < 0)
  193. goto failed;
  194. return 0;
  195. failed:
  196. dev_warn(&state->i2c->dev, "(%s) failed. [adap%d-fe%d]\n",
  197. __func__, fe->dvb->num, fe->id);
  198. return ret;
  199. }
  200. static const struct reg_val standby_data[] = {
  201. { 0x01, 0x00 },
  202. { 0x13, 0x00 }
  203. };
  204. static int mxl301rf_sleep(struct dvb_frontend *fe)
  205. {
  206. struct mxl301rf_state *state;
  207. int ret;
  208. state = fe->tuner_priv;
  209. ret = raw_write(state, (u8 *)standby_data, sizeof(standby_data));
  210. if (ret < 0)
  211. dev_warn(&state->i2c->dev, "(%s) failed. [adap%d-fe%d]\n",
  212. __func__, fe->dvb->num, fe->id);
  213. return ret;
  214. }
  215. /* init sequence is not public.
  216. * the parent must have init'ed the device.
  217. * just wake up here.
  218. */
  219. static int mxl301rf_init(struct dvb_frontend *fe)
  220. {
  221. struct mxl301rf_state *state;
  222. int ret;
  223. state = fe->tuner_priv;
  224. ret = reg_write(state, 0x01, 0x01);
  225. if (ret < 0) {
  226. dev_warn(&state->i2c->dev, "(%s) failed. [adap%d-fe%d]\n",
  227. __func__, fe->dvb->num, fe->id);
  228. return ret;
  229. }
  230. return 0;
  231. }
  232. /* I2C driver functions */
  233. static const struct dvb_tuner_ops mxl301rf_ops = {
  234. .info = {
  235. .name = "MaxLinear MxL301RF",
  236. .frequency_min_hz = 93 * MHz,
  237. .frequency_max_hz = 803 * MHz + 142857,
  238. },
  239. .init = mxl301rf_init,
  240. .sleep = mxl301rf_sleep,
  241. .set_params = mxl301rf_set_params,
  242. .get_rf_strength = mxl301rf_get_rf_strength,
  243. };
  244. static int mxl301rf_probe(struct i2c_client *client,
  245. const struct i2c_device_id *id)
  246. {
  247. struct mxl301rf_state *state;
  248. struct mxl301rf_config *cfg;
  249. struct dvb_frontend *fe;
  250. state = kzalloc(sizeof(*state), GFP_KERNEL);
  251. if (!state)
  252. return -ENOMEM;
  253. state->i2c = client;
  254. cfg = client->dev.platform_data;
  255. memcpy(&state->cfg, cfg, sizeof(state->cfg));
  256. fe = cfg->fe;
  257. fe->tuner_priv = state;
  258. memcpy(&fe->ops.tuner_ops, &mxl301rf_ops, sizeof(mxl301rf_ops));
  259. i2c_set_clientdata(client, &state->cfg);
  260. dev_info(&client->dev, "MaxLinear MxL301RF attached.\n");
  261. return 0;
  262. }
  263. static int mxl301rf_remove(struct i2c_client *client)
  264. {
  265. struct mxl301rf_state *state;
  266. state = cfg_to_state(i2c_get_clientdata(client));
  267. state->cfg.fe->tuner_priv = NULL;
  268. kfree(state);
  269. return 0;
  270. }
  271. static const struct i2c_device_id mxl301rf_id[] = {
  272. {"mxl301rf", 0},
  273. {}
  274. };
  275. MODULE_DEVICE_TABLE(i2c, mxl301rf_id);
  276. static struct i2c_driver mxl301rf_driver = {
  277. .driver = {
  278. .name = "mxl301rf",
  279. },
  280. .probe = mxl301rf_probe,
  281. .remove = mxl301rf_remove,
  282. .id_table = mxl301rf_id,
  283. };
  284. module_i2c_driver(mxl301rf_driver);
  285. MODULE_DESCRIPTION("MaxLinear MXL301RF tuner");
  286. MODULE_AUTHOR("Akihiro TSUKADA");
  287. MODULE_LICENSE("GPL");