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_dev
*sd_to_msi001_dev(struct v4l2_subdev
*sd
)
56 return container_of(sd
, struct msi001_dev
, sd
);
59 static int msi001_wreg(struct msi001_dev
*dev
, u32 data
)
61 /* Register format: 4 bits addr + 20 bits value */
62 return spi_write(dev
->spi
, &data
, 3);
65 static int msi001_set_gain(struct msi001_dev
*dev
, int lna_gain
, int mixer_gain
,
68 struct spi_device
*spi
= dev
->spi
;
72 dev_dbg(&spi
->dev
, "lna=%d mixer=%d if=%d\n",
73 lna_gain
, mixer_gain
, if_gain
);
76 reg
|= (59 - if_gain
) << 4;
78 reg
|= (1 - mixer_gain
) << 12;
79 reg
|= (1 - lna_gain
) << 13;
82 ret
= msi001_wreg(dev
, reg
);
88 dev_dbg(&spi
->dev
, "failed %d\n", ret
);
92 static int msi001_set_tuner(struct msi001_dev
*dev
)
94 struct spi_device
*spi
= dev
->spi
;
96 unsigned int uitmp
, div_n
, k
, k_thresh
, k_frac
, div_lo
, f_if1
;
101 static const struct {
106 { 50000000, 0xe1, 16}, /* AM_MODE2, antenna 2 */
107 {108000000, 0x42, 32}, /* VHF_MODE */
108 {330000000, 0x44, 16}, /* B3_MODE */
109 {960000000, 0x48, 4}, /* B45_MODE */
110 { ~0U, 0x50, 2}, /* BL_MODE */
112 static const struct {
116 { 0, 0x03}, /* Zero IF */
117 { 450000, 0x02}, /* 450 kHz IF */
118 {1620000, 0x01}, /* 1.62 MHz IF */
119 {2048000, 0x00}, /* 2.048 MHz IF */
121 static const struct {
124 } bandwidth_lut
[] = {
125 { 200000, 0x00}, /* 200 kHz */
126 { 300000, 0x01}, /* 300 kHz */
127 { 600000, 0x02}, /* 600 kHz */
128 {1536000, 0x03}, /* 1.536 MHz */
129 {5000000, 0x04}, /* 5 MHz */
130 {6000000, 0x05}, /* 6 MHz */
131 {7000000, 0x06}, /* 7 MHz */
132 {8000000, 0x07}, /* 8 MHz */
135 unsigned int f_rf
= dev
->f_tuner
;
139 * 200000, 300000, 600000, 1536000, 5000000, 6000000, 7000000, 8000000
141 unsigned int bandwidth
;
144 * intermediate frequency (Hz)
145 * 0, 450000, 1620000, 2048000
147 unsigned int f_if
= 0;
148 #define F_REF 24000000
150 #define F_VCO_STEP div_lo
152 dev_dbg(&spi
->dev
, "f_rf=%d f_if=%d\n", f_rf
, f_if
);
154 for (i
= 0; i
< ARRAY_SIZE(band_lut
); i
++) {
155 if (f_rf
<= band_lut
[i
].rf
) {
156 mode
= band_lut
[i
].mode
;
157 div_lo
= band_lut
[i
].div_lo
;
161 if (i
== ARRAY_SIZE(band_lut
)) {
166 /* AM_MODE is upconverted */
167 if ((mode
>> 0) & 0x1)
172 for (i
= 0; i
< ARRAY_SIZE(if_freq_lut
); i
++) {
173 if (f_if
== if_freq_lut
[i
].freq
) {
174 filter_mode
= if_freq_lut
[i
].filter_mode
;
178 if (i
== ARRAY_SIZE(if_freq_lut
)) {
184 bandwidth
= dev
->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
;
193 if (i
== ARRAY_SIZE(bandwidth_lut
)) {
198 dev
->bandwidth
->val
= bandwidth_lut
[i
].freq
;
200 dev_dbg(&spi
->dev
, "bandwidth selected=%d\n", bandwidth_lut
[i
].freq
);
203 * Fractional-N synthesizer
205 * +---------------------------------------+
207 * Fref +----+ +-------+ +----+ +------+ +---+
208 * ------> | PD | --> | VCO | ------> | /4 | --> | /N.F | <-- | K |
209 * +----+ +-------+ +----+ +------+ +---+
218 /* Calculate PLL integer and fractional control word. */
219 f_vco
= (u64
) (f_rf
+ f_if
+ f_if1
) * div_lo
;
220 div_n
= div_u64_rem(f_vco
, DIV_PRE_N
* F_REF
, &k
);
221 k_thresh
= (DIV_PRE_N
* F_REF
) / F_VCO_STEP
;
222 k_frac
= div_u64((u64
) k
* k_thresh
, (DIV_PRE_N
* F_REF
));
224 /* Find out greatest common divisor and divide to smaller. */
225 uitmp
= gcd(k_thresh
, k_frac
);
229 /* Force divide to reg max. Resolution will be reduced. */
230 uitmp
= DIV_ROUND_UP(k_thresh
, 4095);
231 k_thresh
= DIV_ROUND_CLOSEST(k_thresh
, uitmp
);
232 k_frac
= DIV_ROUND_CLOSEST(k_frac
, uitmp
);
234 /* Calculate real RF set. */
235 uitmp
= (unsigned int) F_REF
* DIV_PRE_N
* div_n
;
236 uitmp
+= (unsigned int) F_REF
* DIV_PRE_N
* k_frac
/ k_thresh
;
240 "f_rf=%u:%u f_vco=%llu div_n=%u k_thresh=%u k_frac=%u div_lo=%u\n",
241 f_rf
, uitmp
, f_vco
, div_n
, k_thresh
, k_frac
, div_lo
);
243 ret
= msi001_wreg(dev
, 0x00000e);
247 ret
= msi001_wreg(dev
, 0x000003);
253 reg
|= filter_mode
<< 12;
254 reg
|= bandwidth
<< 14;
257 ret
= msi001_wreg(dev
, reg
);
262 reg
|= k_thresh
<< 4;
265 ret
= msi001_wreg(dev
, reg
);
272 ret
= msi001_wreg(dev
, reg
);
276 ret
= msi001_set_gain(dev
, dev
->lna_gain
->cur
.val
,
277 dev
->mixer_gain
->cur
.val
, dev
->if_gain
->cur
.val
);
284 ret
= msi001_wreg(dev
, reg
);
290 dev_dbg(&spi
->dev
, "failed %d\n", ret
);
294 static int msi001_standby(struct v4l2_subdev
*sd
)
296 struct msi001_dev
*dev
= sd_to_msi001_dev(sd
);
298 return msi001_wreg(dev
, 0x000000);
301 static int msi001_g_tuner(struct v4l2_subdev
*sd
, struct v4l2_tuner
*v
)
303 struct msi001_dev
*dev
= sd_to_msi001_dev(sd
);
304 struct spi_device
*spi
= dev
->spi
;
306 dev_dbg(&spi
->dev
, "index=%d\n", v
->index
);
308 strlcpy(v
->name
, "Mirics MSi001", sizeof(v
->name
));
309 v
->type
= V4L2_TUNER_RF
;
310 v
->capability
= V4L2_TUNER_CAP_1HZ
| V4L2_TUNER_CAP_FREQ_BANDS
;
311 v
->rangelow
= 49000000;
312 v
->rangehigh
= 960000000;
317 static int msi001_s_tuner(struct v4l2_subdev
*sd
, const struct v4l2_tuner
*v
)
319 struct msi001_dev
*dev
= sd_to_msi001_dev(sd
);
320 struct spi_device
*spi
= dev
->spi
;
322 dev_dbg(&spi
->dev
, "index=%d\n", v
->index
);
326 static int msi001_g_frequency(struct v4l2_subdev
*sd
, struct v4l2_frequency
*f
)
328 struct msi001_dev
*dev
= sd_to_msi001_dev(sd
);
329 struct spi_device
*spi
= dev
->spi
;
331 dev_dbg(&spi
->dev
, "tuner=%d\n", f
->tuner
);
332 f
->frequency
= dev
->f_tuner
;
336 static int msi001_s_frequency(struct v4l2_subdev
*sd
,
337 const struct v4l2_frequency
*f
)
339 struct msi001_dev
*dev
= sd_to_msi001_dev(sd
);
340 struct spi_device
*spi
= dev
->spi
;
343 dev_dbg(&spi
->dev
, "tuner=%d type=%d frequency=%u\n",
344 f
->tuner
, f
->type
, f
->frequency
);
346 if (f
->frequency
< ((bands
[0].rangehigh
+ bands
[1].rangelow
) / 2))
350 dev
->f_tuner
= clamp_t(unsigned int, f
->frequency
,
351 bands
[band
].rangelow
, bands
[band
].rangehigh
);
353 return msi001_set_tuner(dev
);
356 static int msi001_enum_freq_bands(struct v4l2_subdev
*sd
,
357 struct v4l2_frequency_band
*band
)
359 struct msi001_dev
*dev
= sd_to_msi001_dev(sd
);
360 struct spi_device
*spi
= dev
->spi
;
362 dev_dbg(&spi
->dev
, "tuner=%d type=%d index=%d\n",
363 band
->tuner
, band
->type
, band
->index
);
365 if (band
->index
>= ARRAY_SIZE(bands
))
368 band
->capability
= bands
[band
->index
].capability
;
369 band
->rangelow
= bands
[band
->index
].rangelow
;
370 band
->rangehigh
= bands
[band
->index
].rangehigh
;
375 static const struct v4l2_subdev_tuner_ops msi001_tuner_ops
= {
376 .standby
= msi001_standby
,
377 .g_tuner
= msi001_g_tuner
,
378 .s_tuner
= msi001_s_tuner
,
379 .g_frequency
= msi001_g_frequency
,
380 .s_frequency
= msi001_s_frequency
,
381 .enum_freq_bands
= msi001_enum_freq_bands
,
384 static const struct v4l2_subdev_ops msi001_ops
= {
385 .tuner
= &msi001_tuner_ops
,
388 static int msi001_s_ctrl(struct v4l2_ctrl
*ctrl
)
390 struct msi001_dev
*dev
= container_of(ctrl
->handler
, struct msi001_dev
, hdl
);
391 struct spi_device
*spi
= dev
->spi
;
395 dev_dbg(&spi
->dev
, "id=%d name=%s val=%d min=%lld max=%lld step=%lld\n",
396 ctrl
->id
, ctrl
->name
, ctrl
->val
, ctrl
->minimum
, ctrl
->maximum
,
400 case V4L2_CID_RF_TUNER_BANDWIDTH_AUTO
:
401 case V4L2_CID_RF_TUNER_BANDWIDTH
:
402 ret
= msi001_set_tuner(dev
);
404 case V4L2_CID_RF_TUNER_LNA_GAIN
:
405 ret
= msi001_set_gain(dev
, dev
->lna_gain
->val
,
406 dev
->mixer_gain
->cur
.val
,
407 dev
->if_gain
->cur
.val
);
409 case V4L2_CID_RF_TUNER_MIXER_GAIN
:
410 ret
= msi001_set_gain(dev
, dev
->lna_gain
->cur
.val
,
411 dev
->mixer_gain
->val
,
412 dev
->if_gain
->cur
.val
);
414 case V4L2_CID_RF_TUNER_IF_GAIN
:
415 ret
= msi001_set_gain(dev
, dev
->lna_gain
->cur
.val
,
416 dev
->mixer_gain
->cur
.val
,
420 dev_dbg(&spi
->dev
, "unknown control %d\n", ctrl
->id
);
427 static const struct v4l2_ctrl_ops msi001_ctrl_ops
= {
428 .s_ctrl
= msi001_s_ctrl
,
431 static int msi001_probe(struct spi_device
*spi
)
433 struct msi001_dev
*dev
;
436 dev_dbg(&spi
->dev
, "\n");
438 dev
= kzalloc(sizeof(*dev
), GFP_KERNEL
);
445 dev
->f_tuner
= bands
[0].rangelow
;
446 v4l2_spi_subdev_init(&dev
->sd
, spi
, &msi001_ops
);
448 /* Register controls */
449 v4l2_ctrl_handler_init(&dev
->hdl
, 5);
450 dev
->bandwidth_auto
= v4l2_ctrl_new_std(&dev
->hdl
, &msi001_ctrl_ops
,
451 V4L2_CID_RF_TUNER_BANDWIDTH_AUTO
, 0, 1, 1, 1);
452 dev
->bandwidth
= v4l2_ctrl_new_std(&dev
->hdl
, &msi001_ctrl_ops
,
453 V4L2_CID_RF_TUNER_BANDWIDTH
, 200000, 8000000, 1, 200000);
454 v4l2_ctrl_auto_cluster(2, &dev
->bandwidth_auto
, 0, false);
455 dev
->lna_gain
= v4l2_ctrl_new_std(&dev
->hdl
, &msi001_ctrl_ops
,
456 V4L2_CID_RF_TUNER_LNA_GAIN
, 0, 1, 1, 1);
457 dev
->mixer_gain
= v4l2_ctrl_new_std(&dev
->hdl
, &msi001_ctrl_ops
,
458 V4L2_CID_RF_TUNER_MIXER_GAIN
, 0, 1, 1, 1);
459 dev
->if_gain
= v4l2_ctrl_new_std(&dev
->hdl
, &msi001_ctrl_ops
,
460 V4L2_CID_RF_TUNER_IF_GAIN
, 0, 59, 1, 0);
461 if (dev
->hdl
.error
) {
462 ret
= dev
->hdl
.error
;
463 dev_err(&spi
->dev
, "Could not initialize controls\n");
464 /* control init failed, free handler */
465 goto err_ctrl_handler_free
;
468 dev
->sd
.ctrl_handler
= &dev
->hdl
;
470 err_ctrl_handler_free
:
471 v4l2_ctrl_handler_free(&dev
->hdl
);
477 static int msi001_remove(struct spi_device
*spi
)
479 struct v4l2_subdev
*sd
= spi_get_drvdata(spi
);
480 struct msi001_dev
*dev
= sd_to_msi001_dev(sd
);
482 dev_dbg(&spi
->dev
, "\n");
485 * Registered by v4l2_spi_new_subdev() from master driver, but we must
486 * unregister it from here. Weird.
488 v4l2_device_unregister_subdev(&dev
->sd
);
489 v4l2_ctrl_handler_free(&dev
->hdl
);
494 static const struct spi_device_id msi001_id_table
[] = {
498 MODULE_DEVICE_TABLE(spi
, msi001_id_table
);
500 static struct spi_driver msi001_driver
= {
503 .suppress_bind_attrs
= true,
505 .probe
= msi001_probe
,
506 .remove
= msi001_remove
,
507 .id_table
= msi001_id_table
,
509 module_spi_driver(msi001_driver
);
511 MODULE_AUTHOR("Antti Palosaari <crope@iki.fi>");
512 MODULE_DESCRIPTION("Mirics MSi001");
513 MODULE_LICENSE("GPL");