msi001.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514
  1. /*
  2. * Mirics MSi001 silicon tuner driver
  3. *
  4. * Copyright (C) 2013 Antti Palosaari <crope@iki.fi>
  5. * Copyright (C) 2014 Antti Palosaari <crope@iki.fi>
  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. * GNU General Public License for more details.
  16. */
  17. #include <linux/module.h>
  18. #include <linux/gcd.h>
  19. #include <media/v4l2-device.h>
  20. #include <media/v4l2-ctrls.h>
  21. static const struct v4l2_frequency_band bands[] = {
  22. {
  23. .type = V4L2_TUNER_RF,
  24. .index = 0,
  25. .capability = V4L2_TUNER_CAP_1HZ | V4L2_TUNER_CAP_FREQ_BANDS,
  26. .rangelow = 49000000,
  27. .rangehigh = 263000000,
  28. }, {
  29. .type = V4L2_TUNER_RF,
  30. .index = 1,
  31. .capability = V4L2_TUNER_CAP_1HZ | V4L2_TUNER_CAP_FREQ_BANDS,
  32. .rangelow = 390000000,
  33. .rangehigh = 960000000,
  34. },
  35. };
  36. struct msi001_dev {
  37. struct spi_device *spi;
  38. struct v4l2_subdev sd;
  39. /* Controls */
  40. struct v4l2_ctrl_handler hdl;
  41. struct v4l2_ctrl *bandwidth_auto;
  42. struct v4l2_ctrl *bandwidth;
  43. struct v4l2_ctrl *lna_gain;
  44. struct v4l2_ctrl *mixer_gain;
  45. struct v4l2_ctrl *if_gain;
  46. unsigned int f_tuner;
  47. };
  48. static inline struct msi001_dev *sd_to_msi001_dev(struct v4l2_subdev *sd)
  49. {
  50. return container_of(sd, struct msi001_dev, sd);
  51. }
  52. static int msi001_wreg(struct msi001_dev *dev, u32 data)
  53. {
  54. /* Register format: 4 bits addr + 20 bits value */
  55. return spi_write(dev->spi, &data, 3);
  56. };
  57. static int msi001_set_gain(struct msi001_dev *dev, int lna_gain, int mixer_gain,
  58. int if_gain)
  59. {
  60. struct spi_device *spi = dev->spi;
  61. int ret;
  62. u32 reg;
  63. dev_dbg(&spi->dev, "lna=%d mixer=%d if=%d\n",
  64. lna_gain, mixer_gain, if_gain);
  65. reg = 1 << 0;
  66. reg |= (59 - if_gain) << 4;
  67. reg |= 0 << 10;
  68. reg |= (1 - mixer_gain) << 12;
  69. reg |= (1 - lna_gain) << 13;
  70. reg |= 4 << 14;
  71. reg |= 0 << 17;
  72. ret = msi001_wreg(dev, reg);
  73. if (ret)
  74. goto err;
  75. return 0;
  76. err:
  77. dev_dbg(&spi->dev, "failed %d\n", ret);
  78. return ret;
  79. };
  80. static int msi001_set_tuner(struct msi001_dev *dev)
  81. {
  82. struct spi_device *spi = dev->spi;
  83. int ret, i;
  84. unsigned int uitmp, div_n, k, k_thresh, k_frac, div_lo, f_if1;
  85. u32 reg;
  86. u64 f_vco;
  87. u8 mode, filter_mode;
  88. static const struct {
  89. u32 rf;
  90. u8 mode;
  91. u8 div_lo;
  92. } band_lut[] = {
  93. { 50000000, 0xe1, 16}, /* AM_MODE2, antenna 2 */
  94. {108000000, 0x42, 32}, /* VHF_MODE */
  95. {330000000, 0x44, 16}, /* B3_MODE */
  96. {960000000, 0x48, 4}, /* B45_MODE */
  97. { ~0U, 0x50, 2}, /* BL_MODE */
  98. };
  99. static const struct {
  100. u32 freq;
  101. u8 filter_mode;
  102. } if_freq_lut[] = {
  103. { 0, 0x03}, /* Zero IF */
  104. { 450000, 0x02}, /* 450 kHz IF */
  105. {1620000, 0x01}, /* 1.62 MHz IF */
  106. {2048000, 0x00}, /* 2.048 MHz IF */
  107. };
  108. static const struct {
  109. u32 freq;
  110. u8 val;
  111. } bandwidth_lut[] = {
  112. { 200000, 0x00}, /* 200 kHz */
  113. { 300000, 0x01}, /* 300 kHz */
  114. { 600000, 0x02}, /* 600 kHz */
  115. {1536000, 0x03}, /* 1.536 MHz */
  116. {5000000, 0x04}, /* 5 MHz */
  117. {6000000, 0x05}, /* 6 MHz */
  118. {7000000, 0x06}, /* 7 MHz */
  119. {8000000, 0x07}, /* 8 MHz */
  120. };
  121. unsigned int f_rf = dev->f_tuner;
  122. /*
  123. * bandwidth (Hz)
  124. * 200000, 300000, 600000, 1536000, 5000000, 6000000, 7000000, 8000000
  125. */
  126. unsigned int bandwidth;
  127. /*
  128. * intermediate frequency (Hz)
  129. * 0, 450000, 1620000, 2048000
  130. */
  131. unsigned int f_if = 0;
  132. #define F_REF 24000000
  133. #define DIV_PRE_N 4
  134. #define F_VCO_STEP div_lo
  135. dev_dbg(&spi->dev, "f_rf=%d f_if=%d\n", f_rf, f_if);
  136. for (i = 0; i < ARRAY_SIZE(band_lut); i++) {
  137. if (f_rf <= band_lut[i].rf) {
  138. mode = band_lut[i].mode;
  139. div_lo = band_lut[i].div_lo;
  140. break;
  141. }
  142. }
  143. if (i == ARRAY_SIZE(band_lut)) {
  144. ret = -EINVAL;
  145. goto err;
  146. }
  147. /* AM_MODE is upconverted */
  148. if ((mode >> 0) & 0x1)
  149. f_if1 = 5 * F_REF;
  150. else
  151. f_if1 = 0;
  152. for (i = 0; i < ARRAY_SIZE(if_freq_lut); i++) {
  153. if (f_if == if_freq_lut[i].freq) {
  154. filter_mode = if_freq_lut[i].filter_mode;
  155. break;
  156. }
  157. }
  158. if (i == ARRAY_SIZE(if_freq_lut)) {
  159. ret = -EINVAL;
  160. goto err;
  161. }
  162. /* filters */
  163. bandwidth = dev->bandwidth->val;
  164. bandwidth = clamp(bandwidth, 200000U, 8000000U);
  165. for (i = 0; i < ARRAY_SIZE(bandwidth_lut); i++) {
  166. if (bandwidth <= bandwidth_lut[i].freq) {
  167. bandwidth = bandwidth_lut[i].val;
  168. break;
  169. }
  170. }
  171. if (i == ARRAY_SIZE(bandwidth_lut)) {
  172. ret = -EINVAL;
  173. goto err;
  174. }
  175. dev->bandwidth->val = bandwidth_lut[i].freq;
  176. dev_dbg(&spi->dev, "bandwidth selected=%d\n", bandwidth_lut[i].freq);
  177. /*
  178. * Fractional-N synthesizer
  179. *
  180. * +---------------------------------------+
  181. * v |
  182. * Fref +----+ +-------+ +----+ +------+ +---+
  183. * ------> | PD | --> | VCO | ------> | /4 | --> | /N.F | <-- | K |
  184. * +----+ +-------+ +----+ +------+ +---+
  185. * |
  186. * |
  187. * v
  188. * +-------+ Fout
  189. * | /Rout | ------>
  190. * +-------+
  191. */
  192. /* Calculate PLL integer and fractional control word. */
  193. f_vco = (u64) (f_rf + f_if + f_if1) * div_lo;
  194. div_n = div_u64_rem(f_vco, DIV_PRE_N * F_REF, &k);
  195. k_thresh = (DIV_PRE_N * F_REF) / F_VCO_STEP;
  196. k_frac = div_u64((u64) k * k_thresh, (DIV_PRE_N * F_REF));
  197. /* Find out greatest common divisor and divide to smaller. */
  198. uitmp = gcd(k_thresh, k_frac);
  199. k_thresh /= uitmp;
  200. k_frac /= uitmp;
  201. /* Force divide to reg max. Resolution will be reduced. */
  202. uitmp = DIV_ROUND_UP(k_thresh, 4095);
  203. k_thresh = DIV_ROUND_CLOSEST(k_thresh, uitmp);
  204. k_frac = DIV_ROUND_CLOSEST(k_frac, uitmp);
  205. /* Calculate real RF set. */
  206. uitmp = (unsigned int) F_REF * DIV_PRE_N * div_n;
  207. uitmp += (unsigned int) F_REF * DIV_PRE_N * k_frac / k_thresh;
  208. uitmp /= div_lo;
  209. dev_dbg(&spi->dev,
  210. "f_rf=%u:%u f_vco=%llu div_n=%u k_thresh=%u k_frac=%u div_lo=%u\n",
  211. f_rf, uitmp, f_vco, div_n, k_thresh, k_frac, div_lo);
  212. ret = msi001_wreg(dev, 0x00000e);
  213. if (ret)
  214. goto err;
  215. ret = msi001_wreg(dev, 0x000003);
  216. if (ret)
  217. goto err;
  218. reg = 0 << 0;
  219. reg |= mode << 4;
  220. reg |= filter_mode << 12;
  221. reg |= bandwidth << 14;
  222. reg |= 0x02 << 17;
  223. reg |= 0x00 << 20;
  224. ret = msi001_wreg(dev, reg);
  225. if (ret)
  226. goto err;
  227. reg = 5 << 0;
  228. reg |= k_thresh << 4;
  229. reg |= 1 << 19;
  230. reg |= 1 << 21;
  231. ret = msi001_wreg(dev, reg);
  232. if (ret)
  233. goto err;
  234. reg = 2 << 0;
  235. reg |= k_frac << 4;
  236. reg |= div_n << 16;
  237. ret = msi001_wreg(dev, reg);
  238. if (ret)
  239. goto err;
  240. ret = msi001_set_gain(dev, dev->lna_gain->cur.val,
  241. dev->mixer_gain->cur.val, dev->if_gain->cur.val);
  242. if (ret)
  243. goto err;
  244. reg = 6 << 0;
  245. reg |= 63 << 4;
  246. reg |= 4095 << 10;
  247. ret = msi001_wreg(dev, reg);
  248. if (ret)
  249. goto err;
  250. return 0;
  251. err:
  252. dev_dbg(&spi->dev, "failed %d\n", ret);
  253. return ret;
  254. }
  255. static int msi001_standby(struct v4l2_subdev *sd)
  256. {
  257. struct msi001_dev *dev = sd_to_msi001_dev(sd);
  258. return msi001_wreg(dev, 0x000000);
  259. }
  260. static int msi001_g_tuner(struct v4l2_subdev *sd, struct v4l2_tuner *v)
  261. {
  262. struct msi001_dev *dev = sd_to_msi001_dev(sd);
  263. struct spi_device *spi = dev->spi;
  264. dev_dbg(&spi->dev, "index=%d\n", v->index);
  265. strlcpy(v->name, "Mirics MSi001", sizeof(v->name));
  266. v->type = V4L2_TUNER_RF;
  267. v->capability = V4L2_TUNER_CAP_1HZ | V4L2_TUNER_CAP_FREQ_BANDS;
  268. v->rangelow = 49000000;
  269. v->rangehigh = 960000000;
  270. return 0;
  271. }
  272. static int msi001_s_tuner(struct v4l2_subdev *sd, const struct v4l2_tuner *v)
  273. {
  274. struct msi001_dev *dev = sd_to_msi001_dev(sd);
  275. struct spi_device *spi = dev->spi;
  276. dev_dbg(&spi->dev, "index=%d\n", v->index);
  277. return 0;
  278. }
  279. static int msi001_g_frequency(struct v4l2_subdev *sd, struct v4l2_frequency *f)
  280. {
  281. struct msi001_dev *dev = sd_to_msi001_dev(sd);
  282. struct spi_device *spi = dev->spi;
  283. dev_dbg(&spi->dev, "tuner=%d\n", f->tuner);
  284. f->frequency = dev->f_tuner;
  285. return 0;
  286. }
  287. static int msi001_s_frequency(struct v4l2_subdev *sd,
  288. const struct v4l2_frequency *f)
  289. {
  290. struct msi001_dev *dev = sd_to_msi001_dev(sd);
  291. struct spi_device *spi = dev->spi;
  292. unsigned int band;
  293. dev_dbg(&spi->dev, "tuner=%d type=%d frequency=%u\n",
  294. f->tuner, f->type, f->frequency);
  295. if (f->frequency < ((bands[0].rangehigh + bands[1].rangelow) / 2))
  296. band = 0;
  297. else
  298. band = 1;
  299. dev->f_tuner = clamp_t(unsigned int, f->frequency,
  300. bands[band].rangelow, bands[band].rangehigh);
  301. return msi001_set_tuner(dev);
  302. }
  303. static int msi001_enum_freq_bands(struct v4l2_subdev *sd,
  304. struct v4l2_frequency_band *band)
  305. {
  306. struct msi001_dev *dev = sd_to_msi001_dev(sd);
  307. struct spi_device *spi = dev->spi;
  308. dev_dbg(&spi->dev, "tuner=%d type=%d index=%d\n",
  309. band->tuner, band->type, band->index);
  310. if (band->index >= ARRAY_SIZE(bands))
  311. return -EINVAL;
  312. band->capability = bands[band->index].capability;
  313. band->rangelow = bands[band->index].rangelow;
  314. band->rangehigh = bands[band->index].rangehigh;
  315. return 0;
  316. }
  317. static const struct v4l2_subdev_tuner_ops msi001_tuner_ops = {
  318. .standby = msi001_standby,
  319. .g_tuner = msi001_g_tuner,
  320. .s_tuner = msi001_s_tuner,
  321. .g_frequency = msi001_g_frequency,
  322. .s_frequency = msi001_s_frequency,
  323. .enum_freq_bands = msi001_enum_freq_bands,
  324. };
  325. static const struct v4l2_subdev_ops msi001_ops = {
  326. .tuner = &msi001_tuner_ops,
  327. };
  328. static int msi001_s_ctrl(struct v4l2_ctrl *ctrl)
  329. {
  330. struct msi001_dev *dev = container_of(ctrl->handler, struct msi001_dev, hdl);
  331. struct spi_device *spi = dev->spi;
  332. int ret;
  333. dev_dbg(&spi->dev, "id=%d name=%s val=%d min=%lld max=%lld step=%lld\n",
  334. ctrl->id, ctrl->name, ctrl->val, ctrl->minimum, ctrl->maximum,
  335. ctrl->step);
  336. switch (ctrl->id) {
  337. case V4L2_CID_RF_TUNER_BANDWIDTH_AUTO:
  338. case V4L2_CID_RF_TUNER_BANDWIDTH:
  339. ret = msi001_set_tuner(dev);
  340. break;
  341. case V4L2_CID_RF_TUNER_LNA_GAIN:
  342. ret = msi001_set_gain(dev, dev->lna_gain->val,
  343. dev->mixer_gain->cur.val,
  344. dev->if_gain->cur.val);
  345. break;
  346. case V4L2_CID_RF_TUNER_MIXER_GAIN:
  347. ret = msi001_set_gain(dev, dev->lna_gain->cur.val,
  348. dev->mixer_gain->val,
  349. dev->if_gain->cur.val);
  350. break;
  351. case V4L2_CID_RF_TUNER_IF_GAIN:
  352. ret = msi001_set_gain(dev, dev->lna_gain->cur.val,
  353. dev->mixer_gain->cur.val,
  354. dev->if_gain->val);
  355. break;
  356. default:
  357. dev_dbg(&spi->dev, "unknown control %d\n", ctrl->id);
  358. ret = -EINVAL;
  359. }
  360. return ret;
  361. }
  362. static const struct v4l2_ctrl_ops msi001_ctrl_ops = {
  363. .s_ctrl = msi001_s_ctrl,
  364. };
  365. static int msi001_probe(struct spi_device *spi)
  366. {
  367. struct msi001_dev *dev;
  368. int ret;
  369. dev_dbg(&spi->dev, "\n");
  370. dev = kzalloc(sizeof(*dev), GFP_KERNEL);
  371. if (!dev) {
  372. ret = -ENOMEM;
  373. goto err;
  374. }
  375. dev->spi = spi;
  376. dev->f_tuner = bands[0].rangelow;
  377. v4l2_spi_subdev_init(&dev->sd, spi, &msi001_ops);
  378. /* Register controls */
  379. v4l2_ctrl_handler_init(&dev->hdl, 5);
  380. dev->bandwidth_auto = v4l2_ctrl_new_std(&dev->hdl, &msi001_ctrl_ops,
  381. V4L2_CID_RF_TUNER_BANDWIDTH_AUTO, 0, 1, 1, 1);
  382. dev->bandwidth = v4l2_ctrl_new_std(&dev->hdl, &msi001_ctrl_ops,
  383. V4L2_CID_RF_TUNER_BANDWIDTH, 200000, 8000000, 1, 200000);
  384. v4l2_ctrl_auto_cluster(2, &dev->bandwidth_auto, 0, false);
  385. dev->lna_gain = v4l2_ctrl_new_std(&dev->hdl, &msi001_ctrl_ops,
  386. V4L2_CID_RF_TUNER_LNA_GAIN, 0, 1, 1, 1);
  387. dev->mixer_gain = v4l2_ctrl_new_std(&dev->hdl, &msi001_ctrl_ops,
  388. V4L2_CID_RF_TUNER_MIXER_GAIN, 0, 1, 1, 1);
  389. dev->if_gain = v4l2_ctrl_new_std(&dev->hdl, &msi001_ctrl_ops,
  390. V4L2_CID_RF_TUNER_IF_GAIN, 0, 59, 1, 0);
  391. if (dev->hdl.error) {
  392. ret = dev->hdl.error;
  393. dev_err(&spi->dev, "Could not initialize controls\n");
  394. /* control init failed, free handler */
  395. goto err_ctrl_handler_free;
  396. }
  397. dev->sd.ctrl_handler = &dev->hdl;
  398. return 0;
  399. err_ctrl_handler_free:
  400. v4l2_ctrl_handler_free(&dev->hdl);
  401. kfree(dev);
  402. err:
  403. return ret;
  404. }
  405. static int msi001_remove(struct spi_device *spi)
  406. {
  407. struct v4l2_subdev *sd = spi_get_drvdata(spi);
  408. struct msi001_dev *dev = sd_to_msi001_dev(sd);
  409. dev_dbg(&spi->dev, "\n");
  410. /*
  411. * Registered by v4l2_spi_new_subdev() from master driver, but we must
  412. * unregister it from here. Weird.
  413. */
  414. v4l2_device_unregister_subdev(&dev->sd);
  415. v4l2_ctrl_handler_free(&dev->hdl);
  416. kfree(dev);
  417. return 0;
  418. }
  419. static const struct spi_device_id msi001_id_table[] = {
  420. {"msi001", 0},
  421. {}
  422. };
  423. MODULE_DEVICE_TABLE(spi, msi001_id_table);
  424. static struct spi_driver msi001_driver = {
  425. .driver = {
  426. .name = "msi001",
  427. .suppress_bind_attrs = true,
  428. },
  429. .probe = msi001_probe,
  430. .remove = msi001_remove,
  431. .id_table = msi001_id_table,
  432. };
  433. module_spi_driver(msi001_driver);
  434. MODULE_AUTHOR("Antti Palosaari <crope@iki.fi>");
  435. MODULE_DESCRIPTION("Mirics MSi001");
  436. MODULE_LICENSE("GPL");