1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Mirics MSi001 silicon tuner driver
5 * Copyright (C) 2013 Antti Palosaari <crope@iki.fi>
6 * Copyright (C) 2014 Antti Palosaari <crope@iki.fi>
9 #include <linux/module.h>
10 #include <linux/gcd.h>
11 #include <media/v4l2-device.h>
12 #include <media/v4l2-ctrls.h>
14 static const struct v4l2_frequency_band bands
[] = {
16 .type
= V4L2_TUNER_RF
,
18 .capability
= V4L2_TUNER_CAP_1HZ
| V4L2_TUNER_CAP_FREQ_BANDS
,
20 .rangehigh
= 263000000,
22 .type
= V4L2_TUNER_RF
,
24 .capability
= V4L2_TUNER_CAP_1HZ
| V4L2_TUNER_CAP_FREQ_BANDS
,
25 .rangelow
= 390000000,
26 .rangehigh
= 960000000,
31 struct spi_device
*spi
;
32 struct v4l2_subdev sd
;
35 struct v4l2_ctrl_handler hdl
;
36 struct v4l2_ctrl
*bandwidth_auto
;
37 struct v4l2_ctrl
*bandwidth
;
38 struct v4l2_ctrl
*lna_gain
;
39 struct v4l2_ctrl
*mixer_gain
;
40 struct v4l2_ctrl
*if_gain
;
45 static inline struct msi001_dev
*sd_to_msi001_dev(struct v4l2_subdev
*sd
)
47 return container_of(sd
, struct msi001_dev
, sd
);
50 static int msi001_wreg(struct msi001_dev
*dev
, u32 data
)
52 /* Register format: 4 bits addr + 20 bits value */
53 return spi_write(dev
->spi
, &data
, 3);
56 static int msi001_set_gain(struct msi001_dev
*dev
, int lna_gain
, int mixer_gain
,
59 struct spi_device
*spi
= dev
->spi
;
63 dev_dbg(&spi
->dev
, "lna=%d mixer=%d if=%d\n",
64 lna_gain
, mixer_gain
, if_gain
);
67 reg
|= (59 - if_gain
) << 4;
69 reg
|= (1 - mixer_gain
) << 12;
70 reg
|= (1 - lna_gain
) << 13;
73 ret
= msi001_wreg(dev
, reg
);
79 dev_dbg(&spi
->dev
, "failed %d\n", ret
);
83 static int msi001_set_tuner(struct msi001_dev
*dev
)
85 struct spi_device
*spi
= dev
->spi
;
87 unsigned int uitmp
, div_n
, k
, k_thresh
, k_frac
, div_lo
, f_if1
;
97 { 50000000, 0xe1, 16}, /* AM_MODE2, antenna 2 */
98 {108000000, 0x42, 32}, /* VHF_MODE */
99 {330000000, 0x44, 16}, /* B3_MODE */
100 {960000000, 0x48, 4}, /* B45_MODE */
101 { ~0U, 0x50, 2}, /* BL_MODE */
103 static const struct {
107 { 0, 0x03}, /* Zero IF */
108 { 450000, 0x02}, /* 450 kHz IF */
109 {1620000, 0x01}, /* 1.62 MHz IF */
110 {2048000, 0x00}, /* 2.048 MHz IF */
112 static const struct {
115 } bandwidth_lut
[] = {
116 { 200000, 0x00}, /* 200 kHz */
117 { 300000, 0x01}, /* 300 kHz */
118 { 600000, 0x02}, /* 600 kHz */
119 {1536000, 0x03}, /* 1.536 MHz */
120 {5000000, 0x04}, /* 5 MHz */
121 {6000000, 0x05}, /* 6 MHz */
122 {7000000, 0x06}, /* 7 MHz */
123 {8000000, 0x07}, /* 8 MHz */
126 unsigned int f_rf
= dev
->f_tuner
;
130 * 200000, 300000, 600000, 1536000, 5000000, 6000000, 7000000, 8000000
132 unsigned int bandwidth
;
135 * intermediate frequency (Hz)
136 * 0, 450000, 1620000, 2048000
138 unsigned int f_if
= 0;
139 #define F_REF 24000000
141 #define F_VCO_STEP div_lo
143 dev_dbg(&spi
->dev
, "f_rf=%d f_if=%d\n", f_rf
, f_if
);
145 for (i
= 0; i
< ARRAY_SIZE(band_lut
); i
++) {
146 if (f_rf
<= band_lut
[i
].rf
) {
147 mode
= band_lut
[i
].mode
;
148 div_lo
= band_lut
[i
].div_lo
;
152 if (i
== ARRAY_SIZE(band_lut
)) {
157 /* AM_MODE is upconverted */
158 if ((mode
>> 0) & 0x1)
163 for (i
= 0; i
< ARRAY_SIZE(if_freq_lut
); i
++) {
164 if (f_if
== if_freq_lut
[i
].freq
) {
165 filter_mode
= if_freq_lut
[i
].filter_mode
;
169 if (i
== ARRAY_SIZE(if_freq_lut
)) {
175 bandwidth
= dev
->bandwidth
->val
;
176 bandwidth
= clamp(bandwidth
, 200000U, 8000000U);
178 for (i
= 0; i
< ARRAY_SIZE(bandwidth_lut
); i
++) {
179 if (bandwidth
<= bandwidth_lut
[i
].freq
) {
180 bandwidth
= bandwidth_lut
[i
].val
;
184 if (i
== ARRAY_SIZE(bandwidth_lut
)) {
189 dev
->bandwidth
->val
= bandwidth_lut
[i
].freq
;
191 dev_dbg(&spi
->dev
, "bandwidth selected=%d\n", bandwidth_lut
[i
].freq
);
194 * Fractional-N synthesizer
196 * +---------------------------------------+
198 * Fref +----+ +-------+ +----+ +------+ +---+
199 * ------> | PD | --> | VCO | ------> | /4 | --> | /N.F | <-- | K |
200 * +----+ +-------+ +----+ +------+ +---+
209 /* Calculate PLL integer and fractional control word. */
210 f_vco
= (u64
) (f_rf
+ f_if
+ f_if1
) * div_lo
;
211 div_n
= div_u64_rem(f_vco
, DIV_PRE_N
* F_REF
, &k
);
212 k_thresh
= (DIV_PRE_N
* F_REF
) / F_VCO_STEP
;
213 k_frac
= div_u64((u64
) k
* k_thresh
, (DIV_PRE_N
* F_REF
));
215 /* Find out greatest common divisor and divide to smaller. */
216 uitmp
= gcd(k_thresh
, k_frac
);
220 /* Force divide to reg max. Resolution will be reduced. */
221 uitmp
= DIV_ROUND_UP(k_thresh
, 4095);
222 k_thresh
= DIV_ROUND_CLOSEST(k_thresh
, uitmp
);
223 k_frac
= DIV_ROUND_CLOSEST(k_frac
, uitmp
);
225 /* Calculate real RF set. */
226 uitmp
= (unsigned int) F_REF
* DIV_PRE_N
* div_n
;
227 uitmp
+= (unsigned int) F_REF
* DIV_PRE_N
* k_frac
/ k_thresh
;
231 "f_rf=%u:%u f_vco=%llu div_n=%u k_thresh=%u k_frac=%u div_lo=%u\n",
232 f_rf
, uitmp
, f_vco
, div_n
, k_thresh
, k_frac
, div_lo
);
234 ret
= msi001_wreg(dev
, 0x00000e);
238 ret
= msi001_wreg(dev
, 0x000003);
244 reg
|= filter_mode
<< 12;
245 reg
|= bandwidth
<< 14;
248 ret
= msi001_wreg(dev
, reg
);
253 reg
|= k_thresh
<< 4;
256 ret
= msi001_wreg(dev
, reg
);
263 ret
= msi001_wreg(dev
, reg
);
267 ret
= msi001_set_gain(dev
, dev
->lna_gain
->cur
.val
,
268 dev
->mixer_gain
->cur
.val
, dev
->if_gain
->cur
.val
);
275 ret
= msi001_wreg(dev
, reg
);
281 dev_dbg(&spi
->dev
, "failed %d\n", ret
);
285 static int msi001_standby(struct v4l2_subdev
*sd
)
287 struct msi001_dev
*dev
= sd_to_msi001_dev(sd
);
289 return msi001_wreg(dev
, 0x000000);
292 static int msi001_g_tuner(struct v4l2_subdev
*sd
, struct v4l2_tuner
*v
)
294 struct msi001_dev
*dev
= sd_to_msi001_dev(sd
);
295 struct spi_device
*spi
= dev
->spi
;
297 dev_dbg(&spi
->dev
, "index=%d\n", v
->index
);
299 strscpy(v
->name
, "Mirics MSi001", sizeof(v
->name
));
300 v
->type
= V4L2_TUNER_RF
;
301 v
->capability
= V4L2_TUNER_CAP_1HZ
| V4L2_TUNER_CAP_FREQ_BANDS
;
302 v
->rangelow
= 49000000;
303 v
->rangehigh
= 960000000;
308 static int msi001_s_tuner(struct v4l2_subdev
*sd
, const struct v4l2_tuner
*v
)
310 struct msi001_dev
*dev
= sd_to_msi001_dev(sd
);
311 struct spi_device
*spi
= dev
->spi
;
313 dev_dbg(&spi
->dev
, "index=%d\n", v
->index
);
317 static int msi001_g_frequency(struct v4l2_subdev
*sd
, struct v4l2_frequency
*f
)
319 struct msi001_dev
*dev
= sd_to_msi001_dev(sd
);
320 struct spi_device
*spi
= dev
->spi
;
322 dev_dbg(&spi
->dev
, "tuner=%d\n", f
->tuner
);
323 f
->frequency
= dev
->f_tuner
;
327 static int msi001_s_frequency(struct v4l2_subdev
*sd
,
328 const struct v4l2_frequency
*f
)
330 struct msi001_dev
*dev
= sd_to_msi001_dev(sd
);
331 struct spi_device
*spi
= dev
->spi
;
334 dev_dbg(&spi
->dev
, "tuner=%d type=%d frequency=%u\n",
335 f
->tuner
, f
->type
, f
->frequency
);
337 if (f
->frequency
< ((bands
[0].rangehigh
+ bands
[1].rangelow
) / 2))
341 dev
->f_tuner
= clamp_t(unsigned int, f
->frequency
,
342 bands
[band
].rangelow
, bands
[band
].rangehigh
);
344 return msi001_set_tuner(dev
);
347 static int msi001_enum_freq_bands(struct v4l2_subdev
*sd
,
348 struct v4l2_frequency_band
*band
)
350 struct msi001_dev
*dev
= sd_to_msi001_dev(sd
);
351 struct spi_device
*spi
= dev
->spi
;
353 dev_dbg(&spi
->dev
, "tuner=%d type=%d index=%d\n",
354 band
->tuner
, band
->type
, band
->index
);
356 if (band
->index
>= ARRAY_SIZE(bands
))
359 band
->capability
= bands
[band
->index
].capability
;
360 band
->rangelow
= bands
[band
->index
].rangelow
;
361 band
->rangehigh
= bands
[band
->index
].rangehigh
;
366 static const struct v4l2_subdev_tuner_ops msi001_tuner_ops
= {
367 .standby
= msi001_standby
,
368 .g_tuner
= msi001_g_tuner
,
369 .s_tuner
= msi001_s_tuner
,
370 .g_frequency
= msi001_g_frequency
,
371 .s_frequency
= msi001_s_frequency
,
372 .enum_freq_bands
= msi001_enum_freq_bands
,
375 static const struct v4l2_subdev_ops msi001_ops
= {
376 .tuner
= &msi001_tuner_ops
,
379 static int msi001_s_ctrl(struct v4l2_ctrl
*ctrl
)
381 struct msi001_dev
*dev
= container_of(ctrl
->handler
, struct msi001_dev
, hdl
);
382 struct spi_device
*spi
= dev
->spi
;
386 dev_dbg(&spi
->dev
, "id=%d name=%s val=%d min=%lld max=%lld step=%lld\n",
387 ctrl
->id
, ctrl
->name
, ctrl
->val
, ctrl
->minimum
, ctrl
->maximum
,
391 case V4L2_CID_RF_TUNER_BANDWIDTH_AUTO
:
392 case V4L2_CID_RF_TUNER_BANDWIDTH
:
393 ret
= msi001_set_tuner(dev
);
395 case V4L2_CID_RF_TUNER_LNA_GAIN
:
396 ret
= msi001_set_gain(dev
, dev
->lna_gain
->val
,
397 dev
->mixer_gain
->cur
.val
,
398 dev
->if_gain
->cur
.val
);
400 case V4L2_CID_RF_TUNER_MIXER_GAIN
:
401 ret
= msi001_set_gain(dev
, dev
->lna_gain
->cur
.val
,
402 dev
->mixer_gain
->val
,
403 dev
->if_gain
->cur
.val
);
405 case V4L2_CID_RF_TUNER_IF_GAIN
:
406 ret
= msi001_set_gain(dev
, dev
->lna_gain
->cur
.val
,
407 dev
->mixer_gain
->cur
.val
,
411 dev_dbg(&spi
->dev
, "unknown control %d\n", ctrl
->id
);
418 static const struct v4l2_ctrl_ops msi001_ctrl_ops
= {
419 .s_ctrl
= msi001_s_ctrl
,
422 static int msi001_probe(struct spi_device
*spi
)
424 struct msi001_dev
*dev
;
427 dev_dbg(&spi
->dev
, "\n");
429 dev
= kzalloc(sizeof(*dev
), GFP_KERNEL
);
436 dev
->f_tuner
= bands
[0].rangelow
;
437 v4l2_spi_subdev_init(&dev
->sd
, spi
, &msi001_ops
);
439 /* Register controls */
440 v4l2_ctrl_handler_init(&dev
->hdl
, 5);
441 dev
->bandwidth_auto
= v4l2_ctrl_new_std(&dev
->hdl
, &msi001_ctrl_ops
,
442 V4L2_CID_RF_TUNER_BANDWIDTH_AUTO
, 0, 1, 1, 1);
443 dev
->bandwidth
= v4l2_ctrl_new_std(&dev
->hdl
, &msi001_ctrl_ops
,
444 V4L2_CID_RF_TUNER_BANDWIDTH
, 200000, 8000000, 1, 200000);
445 v4l2_ctrl_auto_cluster(2, &dev
->bandwidth_auto
, 0, false);
446 dev
->lna_gain
= v4l2_ctrl_new_std(&dev
->hdl
, &msi001_ctrl_ops
,
447 V4L2_CID_RF_TUNER_LNA_GAIN
, 0, 1, 1, 1);
448 dev
->mixer_gain
= v4l2_ctrl_new_std(&dev
->hdl
, &msi001_ctrl_ops
,
449 V4L2_CID_RF_TUNER_MIXER_GAIN
, 0, 1, 1, 1);
450 dev
->if_gain
= v4l2_ctrl_new_std(&dev
->hdl
, &msi001_ctrl_ops
,
451 V4L2_CID_RF_TUNER_IF_GAIN
, 0, 59, 1, 0);
452 if (dev
->hdl
.error
) {
453 ret
= dev
->hdl
.error
;
454 dev_err(&spi
->dev
, "Could not initialize controls\n");
455 /* control init failed, free handler */
456 goto err_ctrl_handler_free
;
459 dev
->sd
.ctrl_handler
= &dev
->hdl
;
461 err_ctrl_handler_free
:
462 v4l2_ctrl_handler_free(&dev
->hdl
);
468 static int msi001_remove(struct spi_device
*spi
)
470 struct v4l2_subdev
*sd
= spi_get_drvdata(spi
);
471 struct msi001_dev
*dev
= sd_to_msi001_dev(sd
);
473 dev_dbg(&spi
->dev
, "\n");
476 * Registered by v4l2_spi_new_subdev() from master driver, but we must
477 * unregister it from here. Weird.
479 v4l2_device_unregister_subdev(&dev
->sd
);
480 v4l2_ctrl_handler_free(&dev
->hdl
);
485 static const struct spi_device_id msi001_id_table
[] = {
489 MODULE_DEVICE_TABLE(spi
, msi001_id_table
);
491 static struct spi_driver msi001_driver
= {
494 .suppress_bind_attrs
= true,
496 .probe
= msi001_probe
,
497 .remove
= msi001_remove
,
498 .id_table
= msi001_id_table
,
500 module_spi_driver(msi001_driver
);
502 MODULE_AUTHOR("Antti Palosaari <crope@iki.fi>");
503 MODULE_DESCRIPTION("Mirics MSi001");
504 MODULE_LICENSE("GPL");