1 // SPDX-License-Identifier: GPL-2.0-only
3 * drivers/media/radio/radio-si476x.c -- V4L2 driver for SI476X chips
5 * Copyright (C) 2012 Innovative Converged Devices(ICD)
6 * Copyright (C) 2013 Andrey Smirnov
8 * Author: Andrey Smirnov <andrew.smirnov@gmail.com>
11 #include <linux/module.h>
12 #include <linux/delay.h>
13 #include <linux/interrupt.h>
14 #include <linux/slab.h>
15 #include <linux/atomic.h>
16 #include <linux/videodev2.h>
17 #include <linux/mutex.h>
18 #include <linux/debugfs.h>
19 #include <media/v4l2-common.h>
20 #include <media/v4l2-ioctl.h>
21 #include <media/v4l2-ctrls.h>
22 #include <media/v4l2-event.h>
23 #include <media/v4l2-device.h>
25 #include <media/drv-intf/si476x.h>
26 #include <linux/mfd/si476x-core.h>
28 #define FM_FREQ_RANGE_LOW 64000000
29 #define FM_FREQ_RANGE_HIGH 108000000
31 #define AM_FREQ_RANGE_LOW 520000
32 #define AM_FREQ_RANGE_HIGH 30000000
34 #define PWRLINEFLTR (1 << 8)
36 #define FREQ_MUL (10000000 / 625)
38 #define SI476X_PHDIV_STATUS_LINK_LOCKED(status) (0x80 & (status))
40 #define DRIVER_NAME "si476x-radio"
41 #define DRIVER_CARD "SI476x AM/FM Receiver"
43 enum si476x_freq_bands {
48 static const struct v4l2_frequency_band si476x_bands[] = {
50 .type = V4L2_TUNER_RADIO,
51 .index = SI476X_BAND_FM,
52 .capability = V4L2_TUNER_CAP_LOW
53 | V4L2_TUNER_CAP_STEREO
55 | V4L2_TUNER_CAP_RDS_BLOCK_IO
56 | V4L2_TUNER_CAP_FREQ_BANDS,
57 .rangelow = 64 * FREQ_MUL,
58 .rangehigh = 108 * FREQ_MUL,
59 .modulation = V4L2_BAND_MODULATION_FM,
62 .type = V4L2_TUNER_RADIO,
63 .index = SI476X_BAND_AM,
64 .capability = V4L2_TUNER_CAP_LOW
65 | V4L2_TUNER_CAP_FREQ_BANDS,
66 .rangelow = 0.52 * FREQ_MUL,
67 .rangehigh = 30 * FREQ_MUL,
68 .modulation = V4L2_BAND_MODULATION_AM,
72 static inline bool si476x_radio_freq_is_inside_of_the_band(u32 freq, int band)
74 return freq >= si476x_bands[band].rangelow &&
75 freq <= si476x_bands[band].rangehigh;
78 static inline bool si476x_radio_range_is_inside_of_the_band(u32 low, u32 high,
81 return low >= si476x_bands[band].rangelow &&
82 high <= si476x_bands[band].rangehigh;
85 static int si476x_radio_s_ctrl(struct v4l2_ctrl *ctrl);
86 static int si476x_radio_g_volatile_ctrl(struct v4l2_ctrl *ctrl);
88 enum phase_diversity_modes_idx {
89 SI476X_IDX_PHDIV_DISABLED,
90 SI476X_IDX_PHDIV_PRIMARY_COMBINING,
91 SI476X_IDX_PHDIV_PRIMARY_ANTENNA,
92 SI476X_IDX_PHDIV_SECONDARY_ANTENNA,
93 SI476X_IDX_PHDIV_SECONDARY_COMBINING,
96 static const char * const phase_diversity_modes[] = {
97 [SI476X_IDX_PHDIV_DISABLED] = "Disabled",
98 [SI476X_IDX_PHDIV_PRIMARY_COMBINING] = "Primary with Secondary",
99 [SI476X_IDX_PHDIV_PRIMARY_ANTENNA] = "Primary Antenna",
100 [SI476X_IDX_PHDIV_SECONDARY_ANTENNA] = "Secondary Antenna",
101 [SI476X_IDX_PHDIV_SECONDARY_COMBINING] = "Secondary with Primary",
104 static inline enum phase_diversity_modes_idx
105 si476x_phase_diversity_mode_to_idx(enum si476x_phase_diversity_mode mode)
110 case SI476X_PHDIV_DISABLED:
111 return SI476X_IDX_PHDIV_DISABLED;
112 case SI476X_PHDIV_PRIMARY_COMBINING:
113 return SI476X_IDX_PHDIV_PRIMARY_COMBINING;
114 case SI476X_PHDIV_PRIMARY_ANTENNA:
115 return SI476X_IDX_PHDIV_PRIMARY_ANTENNA;
116 case SI476X_PHDIV_SECONDARY_ANTENNA:
117 return SI476X_IDX_PHDIV_SECONDARY_ANTENNA;
118 case SI476X_PHDIV_SECONDARY_COMBINING:
119 return SI476X_IDX_PHDIV_SECONDARY_COMBINING;
123 static inline enum si476x_phase_diversity_mode
124 si476x_phase_diversity_idx_to_mode(enum phase_diversity_modes_idx idx)
126 static const int idx_to_value[] = {
127 [SI476X_IDX_PHDIV_DISABLED] = SI476X_PHDIV_DISABLED,
128 [SI476X_IDX_PHDIV_PRIMARY_COMBINING] = SI476X_PHDIV_PRIMARY_COMBINING,
129 [SI476X_IDX_PHDIV_PRIMARY_ANTENNA] = SI476X_PHDIV_PRIMARY_ANTENNA,
130 [SI476X_IDX_PHDIV_SECONDARY_ANTENNA] = SI476X_PHDIV_SECONDARY_ANTENNA,
131 [SI476X_IDX_PHDIV_SECONDARY_COMBINING] = SI476X_PHDIV_SECONDARY_COMBINING,
134 return idx_to_value[idx];
137 static const struct v4l2_ctrl_ops si476x_ctrl_ops = {
138 .g_volatile_ctrl = si476x_radio_g_volatile_ctrl,
139 .s_ctrl = si476x_radio_s_ctrl,
143 enum si476x_ctrl_idx {
144 SI476X_IDX_RSSI_THRESHOLD,
145 SI476X_IDX_SNR_THRESHOLD,
146 SI476X_IDX_MAX_TUNE_ERROR,
147 SI476X_IDX_HARMONICS_COUNT,
148 SI476X_IDX_DIVERSITY_MODE,
149 SI476X_IDX_INTERCHIP_LINK,
151 static struct v4l2_ctrl_config si476x_ctrls[] = {
154 * SI476X during its station seeking(or tuning) process uses several
155 * parameters to detrmine if "the station" is valid:
157 * - Signal's SNR(in dBuV) must be lower than
158 * #V4L2_CID_SI476X_SNR_THRESHOLD
159 * - Signal's RSSI(in dBuV) must be greater than
160 * #V4L2_CID_SI476X_RSSI_THRESHOLD
161 * - Signal's frequency deviation(in units of 2ppm) must not be
162 * more than #V4L2_CID_SI476X_MAX_TUNE_ERROR
164 [SI476X_IDX_RSSI_THRESHOLD] = {
165 .ops = &si476x_ctrl_ops,
166 .id = V4L2_CID_SI476X_RSSI_THRESHOLD,
167 .name = "Valid RSSI Threshold",
168 .type = V4L2_CTRL_TYPE_INTEGER,
173 [SI476X_IDX_SNR_THRESHOLD] = {
174 .ops = &si476x_ctrl_ops,
175 .id = V4L2_CID_SI476X_SNR_THRESHOLD,
176 .type = V4L2_CTRL_TYPE_INTEGER,
177 .name = "Valid SNR Threshold",
182 [SI476X_IDX_MAX_TUNE_ERROR] = {
183 .ops = &si476x_ctrl_ops,
184 .id = V4L2_CID_SI476X_MAX_TUNE_ERROR,
185 .type = V4L2_CTRL_TYPE_INTEGER,
186 .name = "Max Tune Errors",
193 * #V4L2_CID_SI476X_HARMONICS_COUNT -- number of harmonics
194 * built-in power-line noise supression filter is to reject
195 * during AM-mode operation.
197 [SI476X_IDX_HARMONICS_COUNT] = {
198 .ops = &si476x_ctrl_ops,
199 .id = V4L2_CID_SI476X_HARMONICS_COUNT,
200 .type = V4L2_CTRL_TYPE_INTEGER,
202 .name = "Count of Harmonics to Reject",
209 * #V4L2_CID_SI476X_DIVERSITY_MODE -- configuration which
210 * two tuners working in diversity mode are to work in.
212 * - #SI476X_IDX_PHDIV_DISABLED diversity mode disabled
213 * - #SI476X_IDX_PHDIV_PRIMARY_COMBINING diversity mode is
214 * on, primary tuner's antenna is the main one.
215 * - #SI476X_IDX_PHDIV_PRIMARY_ANTENNA diversity mode is
216 * off, primary tuner's antenna is the main one.
217 * - #SI476X_IDX_PHDIV_SECONDARY_ANTENNA diversity mode is
218 * off, secondary tuner's antenna is the main one.
219 * - #SI476X_IDX_PHDIV_SECONDARY_COMBINING diversity mode is
220 * on, secondary tuner's antenna is the main one.
222 [SI476X_IDX_DIVERSITY_MODE] = {
223 .ops = &si476x_ctrl_ops,
224 .id = V4L2_CID_SI476X_DIVERSITY_MODE,
225 .type = V4L2_CTRL_TYPE_MENU,
226 .name = "Phase Diversity Mode",
227 .qmenu = phase_diversity_modes,
229 .max = ARRAY_SIZE(phase_diversity_modes) - 1,
233 * #V4L2_CID_SI476X_INTERCHIP_LINK -- inter-chip link in
234 * diversity mode indicator. Allows user to determine if two
235 * chips working in diversity mode have established a link
236 * between each other and if the system as a whole uses
237 * signals from both antennas to receive FM radio.
239 [SI476X_IDX_INTERCHIP_LINK] = {
240 .ops = &si476x_ctrl_ops,
241 .id = V4L2_CID_SI476X_INTERCHIP_LINK,
242 .type = V4L2_CTRL_TYPE_BOOLEAN,
243 .flags = V4L2_CTRL_FLAG_READ_ONLY | V4L2_CTRL_FLAG_VOLATILE,
244 .name = "Inter-Chip Link",
254 * struct si476x_radio_ops - vtable of tuner functions
256 * This table holds pointers to functions implementing particular
257 * operations depending on the mode in which the tuner chip was
258 * configured to start in. If the function is not supported
259 * corresponding element is set to #NULL.
261 * @tune_freq: Tune chip to a specific frequency
262 * @seek_start: Star station seeking
263 * @rsq_status: Get Received Signal Quality(RSQ) status
264 * @rds_blckcnt: Get received RDS blocks count
265 * @phase_diversity: Change phase diversity mode of the tuner
266 * @phase_div_status: Get phase diversity mode status
267 * @acf_status: Get the status of Automatically Controlled
269 * @agc_status: Get Automatic Gain Control(AGC) status
271 struct si476x_radio_ops {
272 int (*tune_freq)(struct si476x_core *, struct si476x_tune_freq_args *);
273 int (*seek_start)(struct si476x_core *, bool, bool);
274 int (*rsq_status)(struct si476x_core *, struct si476x_rsq_status_args *,
275 struct si476x_rsq_status_report *);
276 int (*rds_blckcnt)(struct si476x_core *, bool,
277 struct si476x_rds_blockcount_report *);
279 int (*phase_diversity)(struct si476x_core *,
280 enum si476x_phase_diversity_mode);
281 int (*phase_div_status)(struct si476x_core *);
282 int (*acf_status)(struct si476x_core *,
283 struct si476x_acf_status_report *);
284 int (*agc_status)(struct si476x_core *,
285 struct si476x_agc_status_report *);
289 * struct si476x_radio - radio device
291 * @v4l2dev: Pointer to V4L2 device created by V4L2 subsystem
292 * @videodev: Pointer to video device created by V4L2 subsystem
293 * @ctrl_handler: V4L2 controls handler
294 * @core: Pointer to underlying core device
295 * @ops: Vtable of functions. See struct si476x_radio_ops for details
296 * @debugfs: pointer to &strucd dentry for debugfs
297 * @audmode: audio mode, as defined for the rxsubchans field
300 * core structure is the radio device is being used
302 struct si476x_radio {
303 struct v4l2_device v4l2dev;
304 struct video_device videodev;
305 struct v4l2_ctrl_handler ctrl_handler;
307 struct si476x_core *core;
308 /* This field should not be accesses unless core lock is held */
309 const struct si476x_radio_ops *ops;
311 struct dentry *debugfs;
315 static inline struct si476x_radio *
316 v4l2_dev_to_radio(struct v4l2_device *d)
318 return container_of(d, struct si476x_radio, v4l2dev);
321 static inline struct si476x_radio *
322 v4l2_ctrl_handler_to_radio(struct v4l2_ctrl_handler *d)
324 return container_of(d, struct si476x_radio, ctrl_handler);
328 * si476x_vidioc_querycap - query device capabilities
330 static int si476x_radio_querycap(struct file *file, void *priv,
331 struct v4l2_capability *capability)
333 struct si476x_radio *radio = video_drvdata(file);
335 strscpy(capability->driver, radio->v4l2dev.name,
336 sizeof(capability->driver));
337 strscpy(capability->card, DRIVER_CARD, sizeof(capability->card));
338 snprintf(capability->bus_info, sizeof(capability->bus_info),
339 "platform:%s", radio->v4l2dev.name);
343 static int si476x_radio_enum_freq_bands(struct file *file, void *priv,
344 struct v4l2_frequency_band *band)
347 struct si476x_radio *radio = video_drvdata(file);
349 if (band->tuner != 0)
352 switch (radio->core->chip_id) {
353 /* AM/FM tuners -- all bands are supported */
354 case SI476X_CHIP_SI4761:
355 case SI476X_CHIP_SI4764:
356 if (band->index < ARRAY_SIZE(si476x_bands)) {
357 *band = si476x_bands[band->index];
363 /* FM companion tuner chips -- only FM bands are
365 case SI476X_CHIP_SI4768:
366 if (band->index == SI476X_BAND_FM) {
367 *band = si476x_bands[band->index];
380 static int si476x_radio_g_tuner(struct file *file, void *priv,
381 struct v4l2_tuner *tuner)
384 struct si476x_rsq_status_report report;
385 struct si476x_radio *radio = video_drvdata(file);
387 struct si476x_rsq_status_args args = {
395 if (tuner->index != 0)
398 tuner->type = V4L2_TUNER_RADIO;
399 tuner->capability = V4L2_TUNER_CAP_LOW /* Measure frequencies
402 | V4L2_TUNER_CAP_STEREO
403 | V4L2_TUNER_CAP_HWSEEK_BOUNDED
404 | V4L2_TUNER_CAP_HWSEEK_WRAP
405 | V4L2_TUNER_CAP_HWSEEK_PROG_LIM;
407 si476x_core_lock(radio->core);
409 if (si476x_core_is_a_secondary_tuner(radio->core)) {
410 strscpy(tuner->name, "FM (secondary)", sizeof(tuner->name));
411 tuner->rxsubchans = 0;
412 tuner->rangelow = si476x_bands[SI476X_BAND_FM].rangelow;
413 } else if (si476x_core_has_am(radio->core)) {
414 if (si476x_core_is_a_primary_tuner(radio->core))
415 strscpy(tuner->name, "AM/FM (primary)",
416 sizeof(tuner->name));
418 strscpy(tuner->name, "AM/FM", sizeof(tuner->name));
420 tuner->rxsubchans = V4L2_TUNER_SUB_MONO | V4L2_TUNER_SUB_STEREO
421 | V4L2_TUNER_SUB_RDS;
422 tuner->capability |= V4L2_TUNER_CAP_RDS
423 | V4L2_TUNER_CAP_RDS_BLOCK_IO
424 | V4L2_TUNER_CAP_FREQ_BANDS;
426 tuner->rangelow = si476x_bands[SI476X_BAND_AM].rangelow;
428 strscpy(tuner->name, "FM", sizeof(tuner->name));
429 tuner->rxsubchans = V4L2_TUNER_SUB_RDS;
430 tuner->capability |= V4L2_TUNER_CAP_RDS
431 | V4L2_TUNER_CAP_RDS_BLOCK_IO
432 | V4L2_TUNER_CAP_FREQ_BANDS;
433 tuner->rangelow = si476x_bands[SI476X_BAND_FM].rangelow;
436 tuner->audmode = radio->audmode;
439 tuner->rangehigh = si476x_bands[SI476X_BAND_FM].rangehigh;
441 err = radio->ops->rsq_status(radio->core,
447 * tuner->signal value range: 0x0000 .. 0xFFFF,
448 * report.rssi: -128 .. 127
450 tuner->signal = (report.rssi + 128) * 257;
452 si476x_core_unlock(radio->core);
457 static int si476x_radio_s_tuner(struct file *file, void *priv,
458 const struct v4l2_tuner *tuner)
460 struct si476x_radio *radio = video_drvdata(file);
462 if (tuner->index != 0)
465 if (tuner->audmode == V4L2_TUNER_MODE_MONO ||
466 tuner->audmode == V4L2_TUNER_MODE_STEREO)
467 radio->audmode = tuner->audmode;
469 radio->audmode = V4L2_TUNER_MODE_STEREO;
474 static int si476x_radio_init_vtable(struct si476x_radio *radio,
475 enum si476x_func func)
477 static const struct si476x_radio_ops fm_ops = {
478 .tune_freq = si476x_core_cmd_fm_tune_freq,
479 .seek_start = si476x_core_cmd_fm_seek_start,
480 .rsq_status = si476x_core_cmd_fm_rsq_status,
481 .rds_blckcnt = si476x_core_cmd_fm_rds_blockcount,
482 .phase_diversity = si476x_core_cmd_fm_phase_diversity,
483 .phase_div_status = si476x_core_cmd_fm_phase_div_status,
484 .acf_status = si476x_core_cmd_fm_acf_status,
485 .agc_status = si476x_core_cmd_agc_status,
488 static const struct si476x_radio_ops am_ops = {
489 .tune_freq = si476x_core_cmd_am_tune_freq,
490 .seek_start = si476x_core_cmd_am_seek_start,
491 .rsq_status = si476x_core_cmd_am_rsq_status,
493 .phase_diversity = NULL,
494 .phase_div_status = NULL,
495 .acf_status = si476x_core_cmd_am_acf_status,
500 case SI476X_FUNC_FM_RECEIVER:
501 radio->ops = &fm_ops;
504 case SI476X_FUNC_AM_RECEIVER:
505 radio->ops = &am_ops;
508 WARN(1, "Unexpected tuner function value\n");
513 static int si476x_radio_pretune(struct si476x_radio *radio,
514 enum si476x_func func)
518 struct si476x_tune_freq_args args = {
521 .injside = SI476X_INJSIDE_AUTO,
522 .tunemode = SI476X_TM_VALIDATED_NORMAL_TUNE,
523 .smoothmetrics = SI476X_SM_INITIALIZE_AUDIO,
528 case SI476X_FUNC_FM_RECEIVER:
529 args.freq = v4l2_to_si476x(radio->core,
531 retval = radio->ops->tune_freq(radio->core, &args);
533 case SI476X_FUNC_AM_RECEIVER:
534 args.freq = v4l2_to_si476x(radio->core,
536 retval = radio->ops->tune_freq(radio->core, &args);
539 WARN(1, "Unexpected tuner function value\n");
545 static int si476x_radio_do_post_powerup_init(struct si476x_radio *radio,
546 enum si476x_func func)
550 /* regcache_mark_dirty(radio->core->regmap); */
551 err = regcache_sync_region(radio->core->regmap,
552 SI476X_PROP_DIGITAL_IO_INPUT_SAMPLE_RATE,
553 SI476X_PROP_DIGITAL_IO_OUTPUT_FORMAT);
557 err = regcache_sync_region(radio->core->regmap,
558 SI476X_PROP_AUDIO_DEEMPHASIS,
559 SI476X_PROP_AUDIO_PWR_LINE_FILTER);
563 err = regcache_sync_region(radio->core->regmap,
564 SI476X_PROP_INT_CTL_ENABLE,
565 SI476X_PROP_INT_CTL_ENABLE);
570 * Is there any point in restoring SNR and the like
571 * when switching between AM/FM?
573 err = regcache_sync_region(radio->core->regmap,
574 SI476X_PROP_VALID_MAX_TUNE_ERROR,
575 SI476X_PROP_VALID_MAX_TUNE_ERROR);
579 err = regcache_sync_region(radio->core->regmap,
580 SI476X_PROP_VALID_SNR_THRESHOLD,
581 SI476X_PROP_VALID_RSSI_THRESHOLD);
585 if (func == SI476X_FUNC_FM_RECEIVER) {
586 if (si476x_core_has_diversity(radio->core)) {
587 err = si476x_core_cmd_fm_phase_diversity(radio->core,
588 radio->core->diversity_mode);
593 err = regcache_sync_region(radio->core->regmap,
594 SI476X_PROP_FM_RDS_INTERRUPT_SOURCE,
595 SI476X_PROP_FM_RDS_CONFIG);
600 return si476x_radio_init_vtable(radio, func);
604 static int si476x_radio_change_func(struct si476x_radio *radio,
605 enum si476x_func func)
610 * Since power/up down is a very time consuming operation,
611 * try to avoid doing it if the requested mode matches the one
614 if (func == radio->core->power_up_parameters.func)
618 err = si476x_core_stop(radio->core, soft);
621 * OK, if the chip does not want to play nice let's
622 * try to reset it in more brutal way
625 err = si476x_core_stop(radio->core, soft);
630 Set the desired radio tuner function
632 radio->core->power_up_parameters.func = func;
634 err = si476x_core_start(radio->core, soft);
639 * No need to do the rest of manipulations for the bootlader
642 if (func != SI476X_FUNC_FM_RECEIVER &&
643 func != SI476X_FUNC_AM_RECEIVER)
646 return si476x_radio_do_post_powerup_init(radio, func);
649 static int si476x_radio_g_frequency(struct file *file, void *priv,
650 struct v4l2_frequency *f)
653 struct si476x_radio *radio = video_drvdata(file);
656 f->type != V4L2_TUNER_RADIO)
659 si476x_core_lock(radio->core);
661 if (radio->ops->rsq_status) {
662 struct si476x_rsq_status_report report;
663 struct si476x_rsq_status_args args = {
671 err = radio->ops->rsq_status(radio->core, &args, &report);
673 f->frequency = si476x_to_v4l2(radio->core,
679 si476x_core_unlock(radio->core);
684 static int si476x_radio_s_frequency(struct file *file, void *priv,
685 const struct v4l2_frequency *f)
688 u32 freq = f->frequency;
689 struct si476x_tune_freq_args args;
690 struct si476x_radio *radio = video_drvdata(file);
692 const u32 midrange = (si476x_bands[SI476X_BAND_AM].rangehigh +
693 si476x_bands[SI476X_BAND_FM].rangelow) / 2;
694 const int band = (freq > midrange) ?
695 SI476X_BAND_FM : SI476X_BAND_AM;
696 const enum si476x_func func = (band == SI476X_BAND_AM) ?
697 SI476X_FUNC_AM_RECEIVER : SI476X_FUNC_FM_RECEIVER;
700 f->type != V4L2_TUNER_RADIO)
703 si476x_core_lock(radio->core);
706 si476x_bands[band].rangelow,
707 si476x_bands[band].rangehigh);
709 if (si476x_radio_freq_is_inside_of_the_band(freq,
711 (!si476x_core_has_am(radio->core) ||
712 si476x_core_is_a_secondary_tuner(radio->core))) {
717 err = si476x_radio_change_func(radio, func);
723 args.injside = SI476X_INJSIDE_AUTO;
724 args.freq = v4l2_to_si476x(radio->core, freq);
725 args.tunemode = SI476X_TM_VALIDATED_NORMAL_TUNE;
726 args.smoothmetrics = SI476X_SM_INITIALIZE_AUDIO;
729 err = radio->ops->tune_freq(radio->core, &args);
732 si476x_core_unlock(radio->core);
736 static int si476x_radio_s_hw_freq_seek(struct file *file, void *priv,
737 const struct v4l2_hw_freq_seek *seek)
740 enum si476x_func func;
741 u32 rangelow = seek->rangelow, rangehigh = seek->rangehigh;
742 struct si476x_radio *radio = video_drvdata(file);
744 if (file->f_flags & O_NONBLOCK)
747 if (seek->tuner != 0 ||
748 seek->type != V4L2_TUNER_RADIO)
751 si476x_core_lock(radio->core);
754 err = regmap_read(radio->core->regmap,
755 SI476X_PROP_SEEK_BAND_BOTTOM,
759 rangelow = si476x_to_v4l2(radio->core, rangelow);
762 err = regmap_read(radio->core->regmap,
763 SI476X_PROP_SEEK_BAND_TOP,
767 rangehigh = si476x_to_v4l2(radio->core, rangehigh);
770 if (rangelow > rangehigh) {
775 if (si476x_radio_range_is_inside_of_the_band(rangelow, rangehigh,
777 func = SI476X_FUNC_FM_RECEIVER;
779 } else if (si476x_core_has_am(radio->core) &&
780 si476x_radio_range_is_inside_of_the_band(rangelow, rangehigh,
782 func = SI476X_FUNC_AM_RECEIVER;
788 err = si476x_radio_change_func(radio, func);
792 if (seek->rangehigh) {
793 err = regmap_write(radio->core->regmap,
794 SI476X_PROP_SEEK_BAND_TOP,
795 v4l2_to_si476x(radio->core,
800 if (seek->rangelow) {
801 err = regmap_write(radio->core->regmap,
802 SI476X_PROP_SEEK_BAND_BOTTOM,
803 v4l2_to_si476x(radio->core,
809 err = regmap_write(radio->core->regmap,
810 SI476X_PROP_SEEK_FREQUENCY_SPACING,
811 v4l2_to_si476x(radio->core,
817 err = radio->ops->seek_start(radio->core,
821 si476x_core_unlock(radio->core);
828 static int si476x_radio_g_volatile_ctrl(struct v4l2_ctrl *ctrl)
831 struct si476x_radio *radio = v4l2_ctrl_handler_to_radio(ctrl->handler);
833 si476x_core_lock(radio->core);
836 case V4L2_CID_SI476X_INTERCHIP_LINK:
837 if (si476x_core_has_diversity(radio->core)) {
838 if (radio->ops->phase_diversity) {
839 retval = radio->ops->phase_div_status(radio->core);
843 ctrl->val = !!SI476X_PHDIV_STATUS_LINK_LOCKED(retval);
857 si476x_core_unlock(radio->core);
862 static int si476x_radio_s_ctrl(struct v4l2_ctrl *ctrl)
865 enum si476x_phase_diversity_mode mode;
866 struct si476x_radio *radio = v4l2_ctrl_handler_to_radio(ctrl->handler);
868 si476x_core_lock(radio->core);
871 case V4L2_CID_SI476X_HARMONICS_COUNT:
872 retval = regmap_update_bits(radio->core->regmap,
873 SI476X_PROP_AUDIO_PWR_LINE_FILTER,
874 SI476X_PROP_PWR_HARMONICS_MASK,
877 case V4L2_CID_POWER_LINE_FREQUENCY:
879 case V4L2_CID_POWER_LINE_FREQUENCY_DISABLED:
880 retval = regmap_update_bits(radio->core->regmap,
881 SI476X_PROP_AUDIO_PWR_LINE_FILTER,
882 SI476X_PROP_PWR_ENABLE_MASK,
885 case V4L2_CID_POWER_LINE_FREQUENCY_50HZ:
886 retval = regmap_update_bits(radio->core->regmap,
887 SI476X_PROP_AUDIO_PWR_LINE_FILTER,
888 SI476X_PROP_PWR_GRID_MASK,
889 SI476X_PROP_PWR_GRID_50HZ);
891 case V4L2_CID_POWER_LINE_FREQUENCY_60HZ:
892 retval = regmap_update_bits(radio->core->regmap,
893 SI476X_PROP_AUDIO_PWR_LINE_FILTER,
894 SI476X_PROP_PWR_GRID_MASK,
895 SI476X_PROP_PWR_GRID_60HZ);
902 case V4L2_CID_SI476X_RSSI_THRESHOLD:
903 retval = regmap_write(radio->core->regmap,
904 SI476X_PROP_VALID_RSSI_THRESHOLD,
907 case V4L2_CID_SI476X_SNR_THRESHOLD:
908 retval = regmap_write(radio->core->regmap,
909 SI476X_PROP_VALID_SNR_THRESHOLD,
912 case V4L2_CID_SI476X_MAX_TUNE_ERROR:
913 retval = regmap_write(radio->core->regmap,
914 SI476X_PROP_VALID_MAX_TUNE_ERROR,
917 case V4L2_CID_RDS_RECEPTION:
919 * It looks like RDS related properties are
920 * inaccesable when tuner is in AM mode, so cache the
923 if (si476x_core_is_in_am_receiver_mode(radio->core))
924 regcache_cache_only(radio->core->regmap, true);
927 retval = regmap_write(radio->core->regmap,
928 SI476X_PROP_FM_RDS_INTERRUPT_FIFO_COUNT,
929 radio->core->rds_fifo_depth);
933 if (radio->core->client->irq) {
934 retval = regmap_write(radio->core->regmap,
935 SI476X_PROP_FM_RDS_INTERRUPT_SOURCE,
941 /* Drain RDS FIFO before enabling RDS processing */
942 retval = si476x_core_cmd_fm_rds_status(radio->core,
950 retval = regmap_update_bits(radio->core->regmap,
951 SI476X_PROP_FM_RDS_CONFIG,
952 SI476X_PROP_RDSEN_MASK,
955 retval = regmap_update_bits(radio->core->regmap,
956 SI476X_PROP_FM_RDS_CONFIG,
957 SI476X_PROP_RDSEN_MASK,
961 if (si476x_core_is_in_am_receiver_mode(radio->core))
962 regcache_cache_only(radio->core->regmap, false);
964 case V4L2_CID_TUNE_DEEMPHASIS:
965 retval = regmap_write(radio->core->regmap,
966 SI476X_PROP_AUDIO_DEEMPHASIS,
970 case V4L2_CID_SI476X_DIVERSITY_MODE:
971 mode = si476x_phase_diversity_idx_to_mode(ctrl->val);
973 if (mode == radio->core->diversity_mode) {
978 if (si476x_core_is_in_am_receiver_mode(radio->core)) {
980 * Diversity cannot be configured while tuner
981 * is in AM mode so save the changes and carry on.
983 radio->core->diversity_mode = mode;
986 retval = radio->ops->phase_diversity(radio->core, mode);
988 radio->core->diversity_mode = mode;
997 si476x_core_unlock(radio->core);
1002 #ifdef CONFIG_VIDEO_ADV_DEBUG
1003 static int si476x_radio_g_register(struct file *file, void *fh,
1004 struct v4l2_dbg_register *reg)
1008 struct si476x_radio *radio = video_drvdata(file);
1010 si476x_core_lock(radio->core);
1012 err = regmap_read(radio->core->regmap,
1013 (unsigned int)reg->reg, &value);
1015 si476x_core_unlock(radio->core);
1019 static int si476x_radio_s_register(struct file *file, void *fh,
1020 const struct v4l2_dbg_register *reg)
1024 struct si476x_radio *radio = video_drvdata(file);
1026 si476x_core_lock(radio->core);
1027 err = regmap_write(radio->core->regmap,
1028 (unsigned int)reg->reg,
1029 (unsigned int)reg->val);
1030 si476x_core_unlock(radio->core);
1036 static int si476x_radio_fops_open(struct file *file)
1038 struct si476x_radio *radio = video_drvdata(file);
1041 err = v4l2_fh_open(file);
1045 if (v4l2_fh_is_singular_file(file)) {
1046 si476x_core_lock(radio->core);
1047 err = si476x_core_set_power_state(radio->core,
1048 SI476X_POWER_UP_FULL);
1052 err = si476x_radio_do_post_powerup_init(radio,
1053 radio->core->power_up_parameters.func);
1057 err = si476x_radio_pretune(radio,
1058 radio->core->power_up_parameters.func);
1062 si476x_core_unlock(radio->core);
1063 /*Must be done after si476x_core_unlock to prevent a deadlock*/
1064 v4l2_ctrl_handler_setup(&radio->ctrl_handler);
1070 si476x_core_set_power_state(radio->core,
1073 si476x_core_unlock(radio->core);
1074 v4l2_fh_release(file);
1079 static int si476x_radio_fops_release(struct file *file)
1082 struct si476x_radio *radio = video_drvdata(file);
1084 if (v4l2_fh_is_singular_file(file) &&
1085 atomic_read(&radio->core->is_alive))
1086 si476x_core_set_power_state(radio->core,
1089 err = v4l2_fh_release(file);
1094 static ssize_t si476x_radio_fops_read(struct file *file, char __user *buf,
1095 size_t count, loff_t *ppos)
1099 unsigned int copied;
1101 struct si476x_radio *radio = video_drvdata(file);
1103 /* block if no new data available */
1104 if (kfifo_is_empty(&radio->core->rds_fifo)) {
1105 if (file->f_flags & O_NONBLOCK)
1106 return -EWOULDBLOCK;
1108 rval = wait_event_interruptible(radio->core->rds_read_queue,
1109 (!kfifo_is_empty(&radio->core->rds_fifo) ||
1110 !atomic_read(&radio->core->is_alive)));
1114 if (!atomic_read(&radio->core->is_alive))
1118 fifo_len = kfifo_len(&radio->core->rds_fifo);
1120 if (kfifo_to_user(&radio->core->rds_fifo, buf,
1121 min(fifo_len, count),
1123 dev_warn(&radio->videodev.dev,
1124 "Error during FIFO to userspace copy\n");
1127 rval = (ssize_t)copied;
1133 static __poll_t si476x_radio_fops_poll(struct file *file,
1134 struct poll_table_struct *pts)
1136 struct si476x_radio *radio = video_drvdata(file);
1137 __poll_t req_events = poll_requested_events(pts);
1138 __poll_t err = v4l2_ctrl_poll(file, pts);
1140 if (req_events & (EPOLLIN | EPOLLRDNORM)) {
1141 if (atomic_read(&radio->core->is_alive))
1142 poll_wait(file, &radio->core->rds_read_queue, pts);
1144 if (!atomic_read(&radio->core->is_alive))
1147 if (!kfifo_is_empty(&radio->core->rds_fifo))
1148 err = EPOLLIN | EPOLLRDNORM;
1154 static const struct v4l2_file_operations si476x_fops = {
1155 .owner = THIS_MODULE,
1156 .read = si476x_radio_fops_read,
1157 .poll = si476x_radio_fops_poll,
1158 .unlocked_ioctl = video_ioctl2,
1159 .open = si476x_radio_fops_open,
1160 .release = si476x_radio_fops_release,
1164 static const struct v4l2_ioctl_ops si4761_ioctl_ops = {
1165 .vidioc_querycap = si476x_radio_querycap,
1166 .vidioc_g_tuner = si476x_radio_g_tuner,
1167 .vidioc_s_tuner = si476x_radio_s_tuner,
1169 .vidioc_g_frequency = si476x_radio_g_frequency,
1170 .vidioc_s_frequency = si476x_radio_s_frequency,
1171 .vidioc_s_hw_freq_seek = si476x_radio_s_hw_freq_seek,
1172 .vidioc_enum_freq_bands = si476x_radio_enum_freq_bands,
1174 .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
1175 .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
1177 #ifdef CONFIG_VIDEO_ADV_DEBUG
1178 .vidioc_g_register = si476x_radio_g_register,
1179 .vidioc_s_register = si476x_radio_s_register,
1184 static const struct video_device si476x_viddev_template = {
1185 .fops = &si476x_fops,
1186 .name = DRIVER_NAME,
1187 .release = video_device_release_empty,
1192 static ssize_t si476x_radio_read_acf_blob(struct file *file,
1193 char __user *user_buf,
1194 size_t count, loff_t *ppos)
1197 struct si476x_radio *radio = file->private_data;
1198 struct si476x_acf_status_report report;
1200 si476x_core_lock(radio->core);
1201 if (radio->ops->acf_status)
1202 err = radio->ops->acf_status(radio->core, &report);
1205 si476x_core_unlock(radio->core);
1210 return simple_read_from_buffer(user_buf, count, ppos, &report,
1214 static const struct file_operations radio_acf_fops = {
1215 .open = simple_open,
1216 .llseek = default_llseek,
1217 .read = si476x_radio_read_acf_blob,
1220 static ssize_t si476x_radio_read_rds_blckcnt_blob(struct file *file,
1221 char __user *user_buf,
1222 size_t count, loff_t *ppos)
1225 struct si476x_radio *radio = file->private_data;
1226 struct si476x_rds_blockcount_report report;
1228 si476x_core_lock(radio->core);
1229 if (radio->ops->rds_blckcnt)
1230 err = radio->ops->rds_blckcnt(radio->core, true,
1234 si476x_core_unlock(radio->core);
1239 return simple_read_from_buffer(user_buf, count, ppos, &report,
1243 static const struct file_operations radio_rds_blckcnt_fops = {
1244 .open = simple_open,
1245 .llseek = default_llseek,
1246 .read = si476x_radio_read_rds_blckcnt_blob,
1249 static ssize_t si476x_radio_read_agc_blob(struct file *file,
1250 char __user *user_buf,
1251 size_t count, loff_t *ppos)
1254 struct si476x_radio *radio = file->private_data;
1255 struct si476x_agc_status_report report;
1257 si476x_core_lock(radio->core);
1258 if (radio->ops->rds_blckcnt)
1259 err = radio->ops->agc_status(radio->core, &report);
1262 si476x_core_unlock(radio->core);
1267 return simple_read_from_buffer(user_buf, count, ppos, &report,
1271 static const struct file_operations radio_agc_fops = {
1272 .open = simple_open,
1273 .llseek = default_llseek,
1274 .read = si476x_radio_read_agc_blob,
1277 static ssize_t si476x_radio_read_rsq_blob(struct file *file,
1278 char __user *user_buf,
1279 size_t count, loff_t *ppos)
1282 struct si476x_radio *radio = file->private_data;
1283 struct si476x_rsq_status_report report;
1284 struct si476x_rsq_status_args args = {
1292 si476x_core_lock(radio->core);
1293 if (radio->ops->rds_blckcnt)
1294 err = radio->ops->rsq_status(radio->core, &args, &report);
1297 si476x_core_unlock(radio->core);
1302 return simple_read_from_buffer(user_buf, count, ppos, &report,
1306 static const struct file_operations radio_rsq_fops = {
1307 .open = simple_open,
1308 .llseek = default_llseek,
1309 .read = si476x_radio_read_rsq_blob,
1312 static ssize_t si476x_radio_read_rsq_primary_blob(struct file *file,
1313 char __user *user_buf,
1314 size_t count, loff_t *ppos)
1317 struct si476x_radio *radio = file->private_data;
1318 struct si476x_rsq_status_report report;
1319 struct si476x_rsq_status_args args = {
1327 si476x_core_lock(radio->core);
1328 if (radio->ops->rds_blckcnt)
1329 err = radio->ops->rsq_status(radio->core, &args, &report);
1332 si476x_core_unlock(radio->core);
1337 return simple_read_from_buffer(user_buf, count, ppos, &report,
1341 static const struct file_operations radio_rsq_primary_fops = {
1342 .open = simple_open,
1343 .llseek = default_llseek,
1344 .read = si476x_radio_read_rsq_primary_blob,
1348 static void si476x_radio_init_debugfs(struct si476x_radio *radio)
1350 radio->debugfs = debugfs_create_dir(dev_name(radio->v4l2dev.dev), NULL);
1352 debugfs_create_file("acf", S_IRUGO, radio->debugfs, radio,
1355 debugfs_create_file("rds_blckcnt", S_IRUGO, radio->debugfs, radio,
1356 &radio_rds_blckcnt_fops);
1358 debugfs_create_file("agc", S_IRUGO, radio->debugfs, radio,
1361 debugfs_create_file("rsq", S_IRUGO, radio->debugfs, radio,
1364 debugfs_create_file("rsq_primary", S_IRUGO, radio->debugfs, radio,
1365 &radio_rsq_primary_fops);
1369 static int si476x_radio_add_new_custom(struct si476x_radio *radio,
1370 enum si476x_ctrl_idx idx)
1373 struct v4l2_ctrl *ctrl;
1375 ctrl = v4l2_ctrl_new_custom(&radio->ctrl_handler,
1378 rval = radio->ctrl_handler.error;
1379 if (ctrl == NULL && rval)
1380 dev_err(radio->v4l2dev.dev,
1381 "Could not initialize '%s' control %d\n",
1382 si476x_ctrls[idx].name, rval);
1387 static int si476x_radio_probe(struct platform_device *pdev)
1390 struct si476x_radio *radio;
1391 struct v4l2_ctrl *ctrl;
1393 static atomic_t instance = ATOMIC_INIT(0);
1395 radio = devm_kzalloc(&pdev->dev, sizeof(*radio), GFP_KERNEL);
1399 radio->core = i2c_mfd_cell_to_core(&pdev->dev);
1401 v4l2_device_set_name(&radio->v4l2dev, DRIVER_NAME, &instance);
1403 rval = v4l2_device_register(&pdev->dev, &radio->v4l2dev);
1405 dev_err(&pdev->dev, "Cannot register v4l2_device.\n");
1409 memcpy(&radio->videodev, &si476x_viddev_template,
1410 sizeof(struct video_device));
1412 radio->videodev.v4l2_dev = &radio->v4l2dev;
1413 radio->videodev.ioctl_ops = &si4761_ioctl_ops;
1414 radio->videodev.device_caps = V4L2_CAP_TUNER | V4L2_CAP_RADIO |
1415 V4L2_CAP_HW_FREQ_SEEK;
1417 si476x_core_lock(radio->core);
1418 if (!si476x_core_is_a_secondary_tuner(radio->core))
1419 radio->videodev.device_caps |= V4L2_CAP_RDS_CAPTURE |
1421 si476x_core_unlock(radio->core);
1423 video_set_drvdata(&radio->videodev, radio);
1424 platform_set_drvdata(pdev, radio);
1427 radio->v4l2dev.ctrl_handler = &radio->ctrl_handler;
1428 v4l2_ctrl_handler_init(&radio->ctrl_handler,
1429 1 + ARRAY_SIZE(si476x_ctrls));
1431 if (si476x_core_has_am(radio->core)) {
1432 ctrl = v4l2_ctrl_new_std_menu(&radio->ctrl_handler,
1434 V4L2_CID_POWER_LINE_FREQUENCY,
1435 V4L2_CID_POWER_LINE_FREQUENCY_60HZ,
1437 rval = radio->ctrl_handler.error;
1438 if (ctrl == NULL && rval) {
1439 dev_err(&pdev->dev, "Could not initialize V4L2_CID_POWER_LINE_FREQUENCY control %d\n",
1444 rval = si476x_radio_add_new_custom(radio,
1445 SI476X_IDX_HARMONICS_COUNT);
1450 rval = si476x_radio_add_new_custom(radio, SI476X_IDX_RSSI_THRESHOLD);
1454 rval = si476x_radio_add_new_custom(radio, SI476X_IDX_SNR_THRESHOLD);
1458 rval = si476x_radio_add_new_custom(radio, SI476X_IDX_MAX_TUNE_ERROR);
1462 ctrl = v4l2_ctrl_new_std_menu(&radio->ctrl_handler,
1464 V4L2_CID_TUNE_DEEMPHASIS,
1465 V4L2_DEEMPHASIS_75_uS, 0, 0);
1466 rval = radio->ctrl_handler.error;
1467 if (ctrl == NULL && rval) {
1468 dev_err(&pdev->dev, "Could not initialize V4L2_CID_TUNE_DEEMPHASIS control %d\n",
1473 ctrl = v4l2_ctrl_new_std(&radio->ctrl_handler, &si476x_ctrl_ops,
1474 V4L2_CID_RDS_RECEPTION,
1476 rval = radio->ctrl_handler.error;
1477 if (ctrl == NULL && rval) {
1478 dev_err(&pdev->dev, "Could not initialize V4L2_CID_RDS_RECEPTION control %d\n",
1483 if (si476x_core_has_diversity(radio->core)) {
1484 si476x_ctrls[SI476X_IDX_DIVERSITY_MODE].def =
1485 si476x_phase_diversity_mode_to_idx(radio->core->diversity_mode);
1486 rval = si476x_radio_add_new_custom(radio, SI476X_IDX_DIVERSITY_MODE);
1490 rval = si476x_radio_add_new_custom(radio, SI476X_IDX_INTERCHIP_LINK);
1495 /* register video device */
1496 rval = video_register_device(&radio->videodev, VFL_TYPE_RADIO, -1);
1498 dev_err(&pdev->dev, "Could not register video device\n");
1502 si476x_radio_init_debugfs(radio);
1506 v4l2_ctrl_handler_free(radio->videodev.ctrl_handler);
1510 static int si476x_radio_remove(struct platform_device *pdev)
1512 struct si476x_radio *radio = platform_get_drvdata(pdev);
1514 v4l2_ctrl_handler_free(radio->videodev.ctrl_handler);
1515 video_unregister_device(&radio->videodev);
1516 v4l2_device_unregister(&radio->v4l2dev);
1517 debugfs_remove_recursive(radio->debugfs);
1522 MODULE_ALIAS("platform:si476x-radio");
1524 static struct platform_driver si476x_radio_driver = {
1526 .name = DRIVER_NAME,
1528 .probe = si476x_radio_probe,
1529 .remove = si476x_radio_remove,
1531 module_platform_driver(si476x_radio_driver);
1533 MODULE_AUTHOR("Andrey Smirnov <andrew.smirnov@gmail.com>");
1534 MODULE_DESCRIPTION("Driver for Si4761/64/68 AM/FM Radio MFD Cell");
1535 MODULE_LICENSE("GPL");