2 * Mirics MSi001 silicon tuner driver
4 * Copyright (C) 2013 Antti Palosaari <crope@iki.fi>
5 * Copyright (C) 2014 Antti Palosaari <crope@iki.fi>
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.
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.
18 #include <linux/module.h>
19 #include <linux/gcd.h>
20 #include <media/v4l2-device.h>
21 #include <media/v4l2-ctrls.h>
23 static const struct v4l2_frequency_band bands[] = {
25 .type = V4L2_TUNER_RF,
27 .capability = V4L2_TUNER_CAP_1HZ | V4L2_TUNER_CAP_FREQ_BANDS,
29 .rangehigh = 263000000,
31 .type = V4L2_TUNER_RF,
33 .capability = V4L2_TUNER_CAP_1HZ | V4L2_TUNER_CAP_FREQ_BANDS,
34 .rangelow = 390000000,
35 .rangehigh = 960000000,
40 struct spi_device *spi;
41 struct v4l2_subdev sd;
44 struct v4l2_ctrl_handler hdl;
45 struct v4l2_ctrl *bandwidth_auto;
46 struct v4l2_ctrl *bandwidth;
47 struct v4l2_ctrl *lna_gain;
48 struct v4l2_ctrl *mixer_gain;
49 struct v4l2_ctrl *if_gain;
54 static inline struct msi001 *sd_to_msi001(struct v4l2_subdev *sd)
56 return container_of(sd, struct msi001, sd);
59 static int msi001_wreg(struct msi001 *s, u32 data)
61 /* Register format: 4 bits addr + 20 bits value */
62 return spi_write(s->spi, &data, 3);
65 static int msi001_set_gain(struct msi001 *s, int lna_gain, int mixer_gain,
70 dev_dbg(&s->spi->dev, "%s: lna=%d mixer=%d if=%d\n", __func__,
71 lna_gain, mixer_gain, if_gain);
74 reg |= (59 - if_gain) << 4;
76 reg |= (1 - mixer_gain) << 12;
77 reg |= (1 - lna_gain) << 13;
80 ret = msi001_wreg(s, reg);
86 dev_dbg(&s->spi->dev, "%s: failed %d\n", __func__, ret);
90 static int msi001_set_tuner(struct msi001 *s)
93 unsigned int n, m, thresh, frac, vco_step, tmp, f_if1;
96 u8 mode, filter_mode, lo_div;
102 { 50000000, 0xe1, 16}, /* AM_MODE2, antenna 2 */
103 {108000000, 0x42, 32}, /* VHF_MODE */
104 {330000000, 0x44, 16}, /* B3_MODE */
105 {960000000, 0x48, 4}, /* B45_MODE */
106 { ~0U, 0x50, 2}, /* BL_MODE */
108 static const struct {
112 { 0, 0x03}, /* Zero IF */
113 { 450000, 0x02}, /* 450 kHz IF */
114 {1620000, 0x01}, /* 1.62 MHz IF */
115 {2048000, 0x00}, /* 2.048 MHz IF */
117 static const struct {
120 } bandwidth_lut[] = {
121 { 200000, 0x00}, /* 200 kHz */
122 { 300000, 0x01}, /* 300 kHz */
123 { 600000, 0x02}, /* 600 kHz */
124 {1536000, 0x03}, /* 1.536 MHz */
125 {5000000, 0x04}, /* 5 MHz */
126 {6000000, 0x05}, /* 6 MHz */
127 {7000000, 0x06}, /* 7 MHz */
128 {8000000, 0x07}, /* 8 MHz */
131 unsigned int f_rf = s->f_tuner;
135 * 200000, 300000, 600000, 1536000, 5000000, 6000000, 7000000, 8000000
137 unsigned int bandwidth;
140 * intermediate frequency (Hz)
141 * 0, 450000, 1620000, 2048000
143 unsigned int f_if = 0;
144 #define F_REF 24000000
148 dev_dbg(&s->spi->dev,
149 "%s: f_rf=%d f_if=%d\n",
150 __func__, f_rf, f_if);
152 for (i = 0; i < ARRAY_SIZE(band_lut); i++) {
153 if (f_rf <= band_lut[i].rf) {
154 mode = band_lut[i].mode;
155 lo_div = band_lut[i].lo_div;
160 if (i == ARRAY_SIZE(band_lut)) {
165 /* AM_MODE is upconverted */
166 if ((mode >> 0) & 0x1)
171 for (i = 0; i < ARRAY_SIZE(if_freq_lut); i++) {
172 if (f_if == if_freq_lut[i].freq) {
173 filter_mode = if_freq_lut[i].filter_mode;
178 if (i == ARRAY_SIZE(if_freq_lut)) {
184 bandwidth = s->bandwidth->val;
185 bandwidth = clamp(bandwidth, 200000U, 8000000U);
187 for (i = 0; i < ARRAY_SIZE(bandwidth_lut); i++) {
188 if (bandwidth <= bandwidth_lut[i].freq) {
189 bandwidth = bandwidth_lut[i].val;
194 if (i == ARRAY_SIZE(bandwidth_lut)) {
199 s->bandwidth->val = bandwidth_lut[i].freq;
201 dev_dbg(&s->spi->dev, "%s: bandwidth selected=%d\n",
202 __func__, bandwidth_lut[i].freq);
204 f_vco = (u64) (f_rf + f_if + f_if1) * lo_div;
206 m = do_div(tmp64, F_REF * R_REF);
207 n = (unsigned int) tmp64;
209 vco_step = F_OUT_STEP * lo_div;
210 thresh = (F_REF * R_REF) / vco_step;
211 frac = 1ul * thresh * m / (F_REF * R_REF);
213 /* Find out greatest common divisor and divide to smaller. */
214 tmp = gcd(thresh, frac);
218 /* Force divide to reg max. Resolution will be reduced. */
219 tmp = DIV_ROUND_UP(thresh, 4095);
220 thresh = DIV_ROUND_CLOSEST(thresh, tmp);
221 frac = DIV_ROUND_CLOSEST(frac, tmp);
223 /* calc real RF set */
224 tmp = 1ul * F_REF * R_REF * n;
225 tmp += 1ul * F_REF * R_REF * frac / thresh;
228 dev_dbg(&s->spi->dev,
229 "%s: rf=%u:%u n=%d thresh=%d frac=%d\n",
230 __func__, f_rf, tmp, n, thresh, frac);
232 ret = msi001_wreg(s, 0x00000e);
236 ret = msi001_wreg(s, 0x000003);
242 reg |= filter_mode << 12;
243 reg |= bandwidth << 14;
246 ret = msi001_wreg(s, reg);
254 ret = msi001_wreg(s, reg);
261 ret = msi001_wreg(s, reg);
265 ret = msi001_set_gain(s, s->lna_gain->cur.val, s->mixer_gain->cur.val,
266 s->if_gain->cur.val);
273 ret = msi001_wreg(s, reg);
279 dev_dbg(&s->spi->dev, "%s: failed %d\n", __func__, ret);
283 static int msi001_s_power(struct v4l2_subdev *sd, int on)
285 struct msi001 *s = sd_to_msi001(sd);
287 dev_dbg(&s->spi->dev, "%s: on=%d\n", __func__, on);
292 ret = msi001_wreg(s, 0x000000);
297 static const struct v4l2_subdev_core_ops msi001_core_ops = {
298 .s_power = msi001_s_power,
301 static int msi001_g_tuner(struct v4l2_subdev *sd, struct v4l2_tuner *v)
303 struct msi001 *s = sd_to_msi001(sd);
304 dev_dbg(&s->spi->dev, "%s: index=%d\n", __func__, v->index);
306 strlcpy(v->name, "Mirics MSi001", sizeof(v->name));
307 v->type = V4L2_TUNER_RF;
308 v->capability = V4L2_TUNER_CAP_1HZ | V4L2_TUNER_CAP_FREQ_BANDS;
309 v->rangelow = 49000000;
310 v->rangehigh = 960000000;
315 static int msi001_s_tuner(struct v4l2_subdev *sd, const struct v4l2_tuner *v)
317 struct msi001 *s = sd_to_msi001(sd);
318 dev_dbg(&s->spi->dev, "%s: index=%d\n", __func__, v->index);
322 static int msi001_g_frequency(struct v4l2_subdev *sd, struct v4l2_frequency *f)
324 struct msi001 *s = sd_to_msi001(sd);
325 dev_dbg(&s->spi->dev, "%s: tuner=%d\n", __func__, f->tuner);
326 f->frequency = s->f_tuner;
330 static int msi001_s_frequency(struct v4l2_subdev *sd,
331 const struct v4l2_frequency *f)
333 struct msi001 *s = sd_to_msi001(sd);
335 dev_dbg(&s->spi->dev, "%s: tuner=%d type=%d frequency=%u\n",
336 __func__, f->tuner, f->type, f->frequency);
338 if (f->frequency < ((bands[0].rangehigh + bands[1].rangelow) / 2))
342 s->f_tuner = clamp_t(unsigned int, f->frequency,
343 bands[band].rangelow, bands[band].rangehigh);
345 return msi001_set_tuner(s);
348 static int msi001_enum_freq_bands(struct v4l2_subdev *sd,
349 struct v4l2_frequency_band *band)
351 struct msi001 *s = sd_to_msi001(sd);
352 dev_dbg(&s->spi->dev, "%s: tuner=%d type=%d index=%d\n",
353 __func__, band->tuner, band->type, band->index);
355 if (band->index >= ARRAY_SIZE(bands))
358 band->capability = bands[band->index].capability;
359 band->rangelow = bands[band->index].rangelow;
360 band->rangehigh = bands[band->index].rangehigh;
365 static const struct v4l2_subdev_tuner_ops msi001_tuner_ops = {
366 .g_tuner = msi001_g_tuner,
367 .s_tuner = msi001_s_tuner,
368 .g_frequency = msi001_g_frequency,
369 .s_frequency = msi001_s_frequency,
370 .enum_freq_bands = msi001_enum_freq_bands,
373 static const struct v4l2_subdev_ops msi001_ops = {
374 .core = &msi001_core_ops,
375 .tuner = &msi001_tuner_ops,
378 static int msi001_s_ctrl(struct v4l2_ctrl *ctrl)
380 struct msi001 *s = container_of(ctrl->handler, struct msi001, hdl);
383 dev_dbg(&s->spi->dev,
384 "%s: id=%d name=%s val=%d min=%lld max=%lld step=%lld\n",
385 __func__, ctrl->id, ctrl->name, ctrl->val,
386 ctrl->minimum, ctrl->maximum, ctrl->step);
389 case V4L2_CID_RF_TUNER_BANDWIDTH_AUTO:
390 case V4L2_CID_RF_TUNER_BANDWIDTH:
391 ret = msi001_set_tuner(s);
393 case V4L2_CID_RF_TUNER_LNA_GAIN:
394 ret = msi001_set_gain(s, s->lna_gain->val,
395 s->mixer_gain->cur.val, s->if_gain->cur.val);
397 case V4L2_CID_RF_TUNER_MIXER_GAIN:
398 ret = msi001_set_gain(s, s->lna_gain->cur.val,
399 s->mixer_gain->val, s->if_gain->cur.val);
401 case V4L2_CID_RF_TUNER_IF_GAIN:
402 ret = msi001_set_gain(s, s->lna_gain->cur.val,
403 s->mixer_gain->cur.val, s->if_gain->val);
406 dev_dbg(&s->spi->dev, "%s: unkown control %d\n",
414 static const struct v4l2_ctrl_ops msi001_ctrl_ops = {
415 .s_ctrl = msi001_s_ctrl,
418 static int msi001_probe(struct spi_device *spi)
422 dev_dbg(&spi->dev, "%s:\n", __func__);
424 s = kzalloc(sizeof(struct msi001), GFP_KERNEL);
427 dev_dbg(&spi->dev, "Could not allocate memory for msi001\n");
432 s->f_tuner = bands[0].rangelow;
433 v4l2_spi_subdev_init(&s->sd, spi, &msi001_ops);
435 /* Register controls */
436 v4l2_ctrl_handler_init(&s->hdl, 5);
437 s->bandwidth_auto = v4l2_ctrl_new_std(&s->hdl, &msi001_ctrl_ops,
438 V4L2_CID_RF_TUNER_BANDWIDTH_AUTO, 0, 1, 1, 1);
439 s->bandwidth = v4l2_ctrl_new_std(&s->hdl, &msi001_ctrl_ops,
440 V4L2_CID_RF_TUNER_BANDWIDTH, 200000, 8000000, 1, 200000);
441 v4l2_ctrl_auto_cluster(2, &s->bandwidth_auto, 0, false);
442 s->lna_gain = v4l2_ctrl_new_std(&s->hdl, &msi001_ctrl_ops,
443 V4L2_CID_RF_TUNER_LNA_GAIN, 0, 1, 1, 1);
444 s->mixer_gain = v4l2_ctrl_new_std(&s->hdl, &msi001_ctrl_ops,
445 V4L2_CID_RF_TUNER_MIXER_GAIN, 0, 1, 1, 1);
446 s->if_gain = v4l2_ctrl_new_std(&s->hdl, &msi001_ctrl_ops,
447 V4L2_CID_RF_TUNER_IF_GAIN, 0, 59, 1, 0);
450 dev_err(&s->spi->dev, "Could not initialize controls\n");
451 /* control init failed, free handler */
452 goto err_ctrl_handler_free;
455 s->sd.ctrl_handler = &s->hdl;
458 err_ctrl_handler_free:
459 v4l2_ctrl_handler_free(&s->hdl);
465 static int msi001_remove(struct spi_device *spi)
467 struct v4l2_subdev *sd = spi_get_drvdata(spi);
468 struct msi001 *s = sd_to_msi001(sd);
469 dev_dbg(&spi->dev, "%s:\n", __func__);
472 * Registered by v4l2_spi_new_subdev() from master driver, but we must
473 * unregister it from here. Weird.
475 v4l2_device_unregister_subdev(&s->sd);
476 v4l2_ctrl_handler_free(&s->hdl);
481 static const struct spi_device_id msi001_id[] = {
485 MODULE_DEVICE_TABLE(spi, msi001_id);
487 static struct spi_driver msi001_driver = {
490 .owner = THIS_MODULE,
492 .probe = msi001_probe,
493 .remove = msi001_remove,
494 .id_table = msi001_id,
496 module_spi_driver(msi001_driver);
498 MODULE_AUTHOR("Antti Palosaari <crope@iki.fi>");
499 MODULE_DESCRIPTION("Mirics MSi001");
500 MODULE_LICENSE("GPL");