msi001.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526
  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_s_power(struct v4l2_subdev *sd, int on)
  256. {
  257. struct msi001_dev *dev = sd_to_msi001_dev(sd);
  258. struct spi_device *spi = dev->spi;
  259. int ret;
  260. dev_dbg(&spi->dev, "on=%d\n", on);
  261. if (on)
  262. ret = 0;
  263. else
  264. ret = msi001_wreg(dev, 0x000000);
  265. return ret;
  266. }
  267. static const struct v4l2_subdev_core_ops msi001_core_ops = {
  268. .s_power = msi001_s_power,
  269. };
  270. static int msi001_g_tuner(struct v4l2_subdev *sd, struct v4l2_tuner *v)
  271. {
  272. struct msi001_dev *dev = sd_to_msi001_dev(sd);
  273. struct spi_device *spi = dev->spi;
  274. dev_dbg(&spi->dev, "index=%d\n", v->index);
  275. strlcpy(v->name, "Mirics MSi001", sizeof(v->name));
  276. v->type = V4L2_TUNER_RF;
  277. v->capability = V4L2_TUNER_CAP_1HZ | V4L2_TUNER_CAP_FREQ_BANDS;
  278. v->rangelow = 49000000;
  279. v->rangehigh = 960000000;
  280. return 0;
  281. }
  282. static int msi001_s_tuner(struct v4l2_subdev *sd, const struct v4l2_tuner *v)
  283. {
  284. struct msi001_dev *dev = sd_to_msi001_dev(sd);
  285. struct spi_device *spi = dev->spi;
  286. dev_dbg(&spi->dev, "index=%d\n", v->index);
  287. return 0;
  288. }
  289. static int msi001_g_frequency(struct v4l2_subdev *sd, struct v4l2_frequency *f)
  290. {
  291. struct msi001_dev *dev = sd_to_msi001_dev(sd);
  292. struct spi_device *spi = dev->spi;
  293. dev_dbg(&spi->dev, "tuner=%d\n", f->tuner);
  294. f->frequency = dev->f_tuner;
  295. return 0;
  296. }
  297. static int msi001_s_frequency(struct v4l2_subdev *sd,
  298. const struct v4l2_frequency *f)
  299. {
  300. struct msi001_dev *dev = sd_to_msi001_dev(sd);
  301. struct spi_device *spi = dev->spi;
  302. unsigned int band;
  303. dev_dbg(&spi->dev, "tuner=%d type=%d frequency=%u\n",
  304. f->tuner, f->type, f->frequency);
  305. if (f->frequency < ((bands[0].rangehigh + bands[1].rangelow) / 2))
  306. band = 0;
  307. else
  308. band = 1;
  309. dev->f_tuner = clamp_t(unsigned int, f->frequency,
  310. bands[band].rangelow, bands[band].rangehigh);
  311. return msi001_set_tuner(dev);
  312. }
  313. static int msi001_enum_freq_bands(struct v4l2_subdev *sd,
  314. struct v4l2_frequency_band *band)
  315. {
  316. struct msi001_dev *dev = sd_to_msi001_dev(sd);
  317. struct spi_device *spi = dev->spi;
  318. dev_dbg(&spi->dev, "tuner=%d type=%d index=%d\n",
  319. band->tuner, band->type, band->index);
  320. if (band->index >= ARRAY_SIZE(bands))
  321. return -EINVAL;
  322. band->capability = bands[band->index].capability;
  323. band->rangelow = bands[band->index].rangelow;
  324. band->rangehigh = bands[band->index].rangehigh;
  325. return 0;
  326. }
  327. static const struct v4l2_subdev_tuner_ops msi001_tuner_ops = {
  328. .g_tuner = msi001_g_tuner,
  329. .s_tuner = msi001_s_tuner,
  330. .g_frequency = msi001_g_frequency,
  331. .s_frequency = msi001_s_frequency,
  332. .enum_freq_bands = msi001_enum_freq_bands,
  333. };
  334. static const struct v4l2_subdev_ops msi001_ops = {
  335. .core = &msi001_core_ops,
  336. .tuner = &msi001_tuner_ops,
  337. };
  338. static int msi001_s_ctrl(struct v4l2_ctrl *ctrl)
  339. {
  340. struct msi001_dev *dev = container_of(ctrl->handler, struct msi001_dev, hdl);
  341. struct spi_device *spi = dev->spi;
  342. int ret;
  343. dev_dbg(&spi->dev, "id=%d name=%s val=%d min=%lld max=%lld step=%lld\n",
  344. ctrl->id, ctrl->name, ctrl->val, ctrl->minimum, ctrl->maximum,
  345. ctrl->step);
  346. switch (ctrl->id) {
  347. case V4L2_CID_RF_TUNER_BANDWIDTH_AUTO:
  348. case V4L2_CID_RF_TUNER_BANDWIDTH:
  349. ret = msi001_set_tuner(dev);
  350. break;
  351. case V4L2_CID_RF_TUNER_LNA_GAIN:
  352. ret = msi001_set_gain(dev, dev->lna_gain->val,
  353. dev->mixer_gain->cur.val,
  354. dev->if_gain->cur.val);
  355. break;
  356. case V4L2_CID_RF_TUNER_MIXER_GAIN:
  357. ret = msi001_set_gain(dev, dev->lna_gain->cur.val,
  358. dev->mixer_gain->val,
  359. dev->if_gain->cur.val);
  360. break;
  361. case V4L2_CID_RF_TUNER_IF_GAIN:
  362. ret = msi001_set_gain(dev, dev->lna_gain->cur.val,
  363. dev->mixer_gain->cur.val,
  364. dev->if_gain->val);
  365. break;
  366. default:
  367. dev_dbg(&spi->dev, "unknown control %d\n", ctrl->id);
  368. ret = -EINVAL;
  369. }
  370. return ret;
  371. }
  372. static const struct v4l2_ctrl_ops msi001_ctrl_ops = {
  373. .s_ctrl = msi001_s_ctrl,
  374. };
  375. static int msi001_probe(struct spi_device *spi)
  376. {
  377. struct msi001_dev *dev;
  378. int ret;
  379. dev_dbg(&spi->dev, "\n");
  380. dev = kzalloc(sizeof(*dev), GFP_KERNEL);
  381. if (!dev) {
  382. ret = -ENOMEM;
  383. goto err;
  384. }
  385. dev->spi = spi;
  386. dev->f_tuner = bands[0].rangelow;
  387. v4l2_spi_subdev_init(&dev->sd, spi, &msi001_ops);
  388. /* Register controls */
  389. v4l2_ctrl_handler_init(&dev->hdl, 5);
  390. dev->bandwidth_auto = v4l2_ctrl_new_std(&dev->hdl, &msi001_ctrl_ops,
  391. V4L2_CID_RF_TUNER_BANDWIDTH_AUTO, 0, 1, 1, 1);
  392. dev->bandwidth = v4l2_ctrl_new_std(&dev->hdl, &msi001_ctrl_ops,
  393. V4L2_CID_RF_TUNER_BANDWIDTH, 200000, 8000000, 1, 200000);
  394. v4l2_ctrl_auto_cluster(2, &dev->bandwidth_auto, 0, false);
  395. dev->lna_gain = v4l2_ctrl_new_std(&dev->hdl, &msi001_ctrl_ops,
  396. V4L2_CID_RF_TUNER_LNA_GAIN, 0, 1, 1, 1);
  397. dev->mixer_gain = v4l2_ctrl_new_std(&dev->hdl, &msi001_ctrl_ops,
  398. V4L2_CID_RF_TUNER_MIXER_GAIN, 0, 1, 1, 1);
  399. dev->if_gain = v4l2_ctrl_new_std(&dev->hdl, &msi001_ctrl_ops,
  400. V4L2_CID_RF_TUNER_IF_GAIN, 0, 59, 1, 0);
  401. if (dev->hdl.error) {
  402. ret = dev->hdl.error;
  403. dev_err(&spi->dev, "Could not initialize controls\n");
  404. /* control init failed, free handler */
  405. goto err_ctrl_handler_free;
  406. }
  407. dev->sd.ctrl_handler = &dev->hdl;
  408. return 0;
  409. err_ctrl_handler_free:
  410. v4l2_ctrl_handler_free(&dev->hdl);
  411. kfree(dev);
  412. err:
  413. return ret;
  414. }
  415. static int msi001_remove(struct spi_device *spi)
  416. {
  417. struct v4l2_subdev *sd = spi_get_drvdata(spi);
  418. struct msi001_dev *dev = sd_to_msi001_dev(sd);
  419. dev_dbg(&spi->dev, "\n");
  420. /*
  421. * Registered by v4l2_spi_new_subdev() from master driver, but we must
  422. * unregister it from here. Weird.
  423. */
  424. v4l2_device_unregister_subdev(&dev->sd);
  425. v4l2_ctrl_handler_free(&dev->hdl);
  426. kfree(dev);
  427. return 0;
  428. }
  429. static const struct spi_device_id msi001_id_table[] = {
  430. {"msi001", 0},
  431. {}
  432. };
  433. MODULE_DEVICE_TABLE(spi, msi001_id_table);
  434. static struct spi_driver msi001_driver = {
  435. .driver = {
  436. .name = "msi001",
  437. .suppress_bind_attrs = true,
  438. },
  439. .probe = msi001_probe,
  440. .remove = msi001_remove,
  441. .id_table = msi001_id_table,
  442. };
  443. module_spi_driver(msi001_driver);
  444. MODULE_AUTHOR("Antti Palosaari <crope@iki.fi>");
  445. MODULE_DESCRIPTION("Mirics MSi001");
  446. MODULE_LICENSE("GPL");