mxl301rf.c 8.0 KB

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