msi001.c 12 KB

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