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 determine 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. 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_ctrl_handler_to_radio(struct v4l2_ctrl_handler
*d
)
318 return container_of(d
, struct si476x_radio
, ctrl_handler
);
322 * si476x_vidioc_querycap - query device capabilities
324 static int si476x_radio_querycap(struct file
*file
, void *priv
,
325 struct v4l2_capability
*capability
)
327 struct si476x_radio
*radio
= video_drvdata(file
);
329 strscpy(capability
->driver
, radio
->v4l2dev
.name
,
330 sizeof(capability
->driver
));
331 strscpy(capability
->card
, DRIVER_CARD
, sizeof(capability
->card
));
335 static int si476x_radio_enum_freq_bands(struct file
*file
, void *priv
,
336 struct v4l2_frequency_band
*band
)
339 struct si476x_radio
*radio
= video_drvdata(file
);
341 if (band
->tuner
!= 0)
344 switch (radio
->core
->chip_id
) {
345 /* AM/FM tuners -- all bands are supported */
346 case SI476X_CHIP_SI4761
:
347 case SI476X_CHIP_SI4764
:
348 if (band
->index
< ARRAY_SIZE(si476x_bands
)) {
349 *band
= si476x_bands
[band
->index
];
355 /* FM companion tuner chips -- only FM bands are
357 case SI476X_CHIP_SI4768
:
358 if (band
->index
== SI476X_BAND_FM
) {
359 *band
= si476x_bands
[band
->index
];
372 static int si476x_radio_g_tuner(struct file
*file
, void *priv
,
373 struct v4l2_tuner
*tuner
)
376 struct si476x_rsq_status_report report
;
377 struct si476x_radio
*radio
= video_drvdata(file
);
379 struct si476x_rsq_status_args args
= {
387 if (tuner
->index
!= 0)
390 tuner
->type
= V4L2_TUNER_RADIO
;
391 tuner
->capability
= V4L2_TUNER_CAP_LOW
/* Measure frequencies
394 | V4L2_TUNER_CAP_STEREO
395 | V4L2_TUNER_CAP_HWSEEK_BOUNDED
396 | V4L2_TUNER_CAP_HWSEEK_WRAP
397 | V4L2_TUNER_CAP_HWSEEK_PROG_LIM
;
399 si476x_core_lock(radio
->core
);
401 if (si476x_core_is_a_secondary_tuner(radio
->core
)) {
402 strscpy(tuner
->name
, "FM (secondary)", sizeof(tuner
->name
));
403 tuner
->rxsubchans
= 0;
404 tuner
->rangelow
= si476x_bands
[SI476X_BAND_FM
].rangelow
;
405 } else if (si476x_core_has_am(radio
->core
)) {
406 if (si476x_core_is_a_primary_tuner(radio
->core
))
407 strscpy(tuner
->name
, "AM/FM (primary)",
408 sizeof(tuner
->name
));
410 strscpy(tuner
->name
, "AM/FM", sizeof(tuner
->name
));
412 tuner
->rxsubchans
= V4L2_TUNER_SUB_MONO
| V4L2_TUNER_SUB_STEREO
413 | V4L2_TUNER_SUB_RDS
;
414 tuner
->capability
|= V4L2_TUNER_CAP_RDS
415 | V4L2_TUNER_CAP_RDS_BLOCK_IO
416 | V4L2_TUNER_CAP_FREQ_BANDS
;
418 tuner
->rangelow
= si476x_bands
[SI476X_BAND_AM
].rangelow
;
420 strscpy(tuner
->name
, "FM", sizeof(tuner
->name
));
421 tuner
->rxsubchans
= V4L2_TUNER_SUB_RDS
;
422 tuner
->capability
|= V4L2_TUNER_CAP_RDS
423 | V4L2_TUNER_CAP_RDS_BLOCK_IO
424 | V4L2_TUNER_CAP_FREQ_BANDS
;
425 tuner
->rangelow
= si476x_bands
[SI476X_BAND_FM
].rangelow
;
428 tuner
->audmode
= radio
->audmode
;
431 tuner
->rangehigh
= si476x_bands
[SI476X_BAND_FM
].rangehigh
;
433 err
= radio
->ops
->rsq_status(radio
->core
,
439 * tuner->signal value range: 0x0000 .. 0xFFFF,
440 * report.rssi: -128 .. 127
442 tuner
->signal
= (report
.rssi
+ 128) * 257;
444 si476x_core_unlock(radio
->core
);
449 static int si476x_radio_s_tuner(struct file
*file
, void *priv
,
450 const struct v4l2_tuner
*tuner
)
452 struct si476x_radio
*radio
= video_drvdata(file
);
454 if (tuner
->index
!= 0)
457 if (tuner
->audmode
== V4L2_TUNER_MODE_MONO
||
458 tuner
->audmode
== V4L2_TUNER_MODE_STEREO
)
459 radio
->audmode
= tuner
->audmode
;
461 radio
->audmode
= V4L2_TUNER_MODE_STEREO
;
466 static int si476x_radio_init_vtable(struct si476x_radio
*radio
,
467 enum si476x_func func
)
469 static const struct si476x_radio_ops fm_ops
= {
470 .tune_freq
= si476x_core_cmd_fm_tune_freq
,
471 .seek_start
= si476x_core_cmd_fm_seek_start
,
472 .rsq_status
= si476x_core_cmd_fm_rsq_status
,
473 .rds_blckcnt
= si476x_core_cmd_fm_rds_blockcount
,
474 .phase_diversity
= si476x_core_cmd_fm_phase_diversity
,
475 .phase_div_status
= si476x_core_cmd_fm_phase_div_status
,
476 .acf_status
= si476x_core_cmd_fm_acf_status
,
477 .agc_status
= si476x_core_cmd_agc_status
,
480 static const struct si476x_radio_ops am_ops
= {
481 .tune_freq
= si476x_core_cmd_am_tune_freq
,
482 .seek_start
= si476x_core_cmd_am_seek_start
,
483 .rsq_status
= si476x_core_cmd_am_rsq_status
,
485 .phase_diversity
= NULL
,
486 .phase_div_status
= NULL
,
487 .acf_status
= si476x_core_cmd_am_acf_status
,
492 case SI476X_FUNC_FM_RECEIVER
:
493 radio
->ops
= &fm_ops
;
496 case SI476X_FUNC_AM_RECEIVER
:
497 radio
->ops
= &am_ops
;
500 WARN(1, "Unexpected tuner function value\n");
505 static int si476x_radio_pretune(struct si476x_radio
*radio
,
506 enum si476x_func func
)
510 struct si476x_tune_freq_args args
= {
513 .injside
= SI476X_INJSIDE_AUTO
,
514 .tunemode
= SI476X_TM_VALIDATED_NORMAL_TUNE
,
515 .smoothmetrics
= SI476X_SM_INITIALIZE_AUDIO
,
520 case SI476X_FUNC_FM_RECEIVER
:
521 args
.freq
= v4l2_to_si476x(radio
->core
,
523 retval
= radio
->ops
->tune_freq(radio
->core
, &args
);
525 case SI476X_FUNC_AM_RECEIVER
:
526 args
.freq
= v4l2_to_si476x(radio
->core
,
528 retval
= radio
->ops
->tune_freq(radio
->core
, &args
);
531 WARN(1, "Unexpected tuner function value\n");
537 static int si476x_radio_do_post_powerup_init(struct si476x_radio
*radio
,
538 enum si476x_func func
)
542 /* regcache_mark_dirty(radio->core->regmap); */
543 err
= regcache_sync_region(radio
->core
->regmap
,
544 SI476X_PROP_DIGITAL_IO_INPUT_SAMPLE_RATE
,
545 SI476X_PROP_DIGITAL_IO_OUTPUT_FORMAT
);
549 err
= regcache_sync_region(radio
->core
->regmap
,
550 SI476X_PROP_AUDIO_DEEMPHASIS
,
551 SI476X_PROP_AUDIO_PWR_LINE_FILTER
);
555 err
= regcache_sync_region(radio
->core
->regmap
,
556 SI476X_PROP_INT_CTL_ENABLE
,
557 SI476X_PROP_INT_CTL_ENABLE
);
562 * Is there any point in restoring SNR and the like
563 * when switching between AM/FM?
565 err
= regcache_sync_region(radio
->core
->regmap
,
566 SI476X_PROP_VALID_MAX_TUNE_ERROR
,
567 SI476X_PROP_VALID_MAX_TUNE_ERROR
);
571 err
= regcache_sync_region(radio
->core
->regmap
,
572 SI476X_PROP_VALID_SNR_THRESHOLD
,
573 SI476X_PROP_VALID_RSSI_THRESHOLD
);
577 if (func
== SI476X_FUNC_FM_RECEIVER
) {
578 if (si476x_core_has_diversity(radio
->core
)) {
579 err
= si476x_core_cmd_fm_phase_diversity(radio
->core
,
580 radio
->core
->diversity_mode
);
585 err
= regcache_sync_region(radio
->core
->regmap
,
586 SI476X_PROP_FM_RDS_INTERRUPT_SOURCE
,
587 SI476X_PROP_FM_RDS_CONFIG
);
592 return si476x_radio_init_vtable(radio
, func
);
596 static int si476x_radio_change_func(struct si476x_radio
*radio
,
597 enum si476x_func func
)
602 * Since power/up down is a very time consuming operation,
603 * try to avoid doing it if the requested mode matches the one
606 if (func
== radio
->core
->power_up_parameters
.func
)
610 err
= si476x_core_stop(radio
->core
, soft
);
613 * OK, if the chip does not want to play nice let's
614 * try to reset it in more brutal way
617 err
= si476x_core_stop(radio
->core
, soft
);
622 Set the desired radio tuner function
624 radio
->core
->power_up_parameters
.func
= func
;
626 err
= si476x_core_start(radio
->core
, soft
);
631 * No need to do the rest of manipulations for the bootlader
634 if (func
!= SI476X_FUNC_FM_RECEIVER
&&
635 func
!= SI476X_FUNC_AM_RECEIVER
)
638 return si476x_radio_do_post_powerup_init(radio
, func
);
641 static int si476x_radio_g_frequency(struct file
*file
, void *priv
,
642 struct v4l2_frequency
*f
)
645 struct si476x_radio
*radio
= video_drvdata(file
);
648 f
->type
!= V4L2_TUNER_RADIO
)
651 si476x_core_lock(radio
->core
);
653 if (radio
->ops
->rsq_status
) {
654 struct si476x_rsq_status_report report
;
655 struct si476x_rsq_status_args args
= {
663 err
= radio
->ops
->rsq_status(radio
->core
, &args
, &report
);
665 f
->frequency
= si476x_to_v4l2(radio
->core
,
671 si476x_core_unlock(radio
->core
);
676 static int si476x_radio_s_frequency(struct file
*file
, void *priv
,
677 const struct v4l2_frequency
*f
)
680 u32 freq
= f
->frequency
;
681 struct si476x_tune_freq_args args
;
682 struct si476x_radio
*radio
= video_drvdata(file
);
684 const u32 midrange
= (si476x_bands
[SI476X_BAND_AM
].rangehigh
+
685 si476x_bands
[SI476X_BAND_FM
].rangelow
) / 2;
686 const int band
= (freq
> midrange
) ?
687 SI476X_BAND_FM
: SI476X_BAND_AM
;
688 const enum si476x_func func
= (band
== SI476X_BAND_AM
) ?
689 SI476X_FUNC_AM_RECEIVER
: SI476X_FUNC_FM_RECEIVER
;
692 f
->type
!= V4L2_TUNER_RADIO
)
695 si476x_core_lock(radio
->core
);
698 si476x_bands
[band
].rangelow
,
699 si476x_bands
[band
].rangehigh
);
701 if (si476x_radio_freq_is_inside_of_the_band(freq
,
703 (!si476x_core_has_am(radio
->core
) ||
704 si476x_core_is_a_secondary_tuner(radio
->core
))) {
709 err
= si476x_radio_change_func(radio
, func
);
715 args
.injside
= SI476X_INJSIDE_AUTO
;
716 args
.freq
= v4l2_to_si476x(radio
->core
, freq
);
717 args
.tunemode
= SI476X_TM_VALIDATED_NORMAL_TUNE
;
718 args
.smoothmetrics
= SI476X_SM_INITIALIZE_AUDIO
;
721 err
= radio
->ops
->tune_freq(radio
->core
, &args
);
724 si476x_core_unlock(radio
->core
);
728 static int si476x_radio_s_hw_freq_seek(struct file
*file
, void *priv
,
729 const struct v4l2_hw_freq_seek
*seek
)
732 enum si476x_func func
;
733 u32 rangelow
= seek
->rangelow
, rangehigh
= seek
->rangehigh
;
734 struct si476x_radio
*radio
= video_drvdata(file
);
736 if (file
->f_flags
& O_NONBLOCK
)
739 if (seek
->tuner
!= 0 ||
740 seek
->type
!= V4L2_TUNER_RADIO
)
743 si476x_core_lock(radio
->core
);
746 err
= regmap_read(radio
->core
->regmap
,
747 SI476X_PROP_SEEK_BAND_BOTTOM
,
751 rangelow
= si476x_to_v4l2(radio
->core
, rangelow
);
754 err
= regmap_read(radio
->core
->regmap
,
755 SI476X_PROP_SEEK_BAND_TOP
,
759 rangehigh
= si476x_to_v4l2(radio
->core
, rangehigh
);
762 if (rangelow
> rangehigh
) {
767 if (si476x_radio_range_is_inside_of_the_band(rangelow
, rangehigh
,
769 func
= SI476X_FUNC_FM_RECEIVER
;
771 } else if (si476x_core_has_am(radio
->core
) &&
772 si476x_radio_range_is_inside_of_the_band(rangelow
, rangehigh
,
774 func
= SI476X_FUNC_AM_RECEIVER
;
780 err
= si476x_radio_change_func(radio
, func
);
784 if (seek
->rangehigh
) {
785 err
= regmap_write(radio
->core
->regmap
,
786 SI476X_PROP_SEEK_BAND_TOP
,
787 v4l2_to_si476x(radio
->core
,
792 if (seek
->rangelow
) {
793 err
= regmap_write(radio
->core
->regmap
,
794 SI476X_PROP_SEEK_BAND_BOTTOM
,
795 v4l2_to_si476x(radio
->core
,
801 err
= regmap_write(radio
->core
->regmap
,
802 SI476X_PROP_SEEK_FREQUENCY_SPACING
,
803 v4l2_to_si476x(radio
->core
,
809 err
= radio
->ops
->seek_start(radio
->core
,
813 si476x_core_unlock(radio
->core
);
820 static int si476x_radio_g_volatile_ctrl(struct v4l2_ctrl
*ctrl
)
823 struct si476x_radio
*radio
= v4l2_ctrl_handler_to_radio(ctrl
->handler
);
825 si476x_core_lock(radio
->core
);
828 case V4L2_CID_SI476X_INTERCHIP_LINK
:
829 if (si476x_core_has_diversity(radio
->core
)) {
830 if (radio
->ops
->phase_diversity
) {
831 retval
= radio
->ops
->phase_div_status(radio
->core
);
835 ctrl
->val
= !!SI476X_PHDIV_STATUS_LINK_LOCKED(retval
);
849 si476x_core_unlock(radio
->core
);
854 static int si476x_radio_s_ctrl(struct v4l2_ctrl
*ctrl
)
857 enum si476x_phase_diversity_mode mode
;
858 struct si476x_radio
*radio
= v4l2_ctrl_handler_to_radio(ctrl
->handler
);
860 si476x_core_lock(radio
->core
);
863 case V4L2_CID_SI476X_HARMONICS_COUNT
:
864 retval
= regmap_update_bits(radio
->core
->regmap
,
865 SI476X_PROP_AUDIO_PWR_LINE_FILTER
,
866 SI476X_PROP_PWR_HARMONICS_MASK
,
869 case V4L2_CID_POWER_LINE_FREQUENCY
:
871 case V4L2_CID_POWER_LINE_FREQUENCY_DISABLED
:
872 retval
= regmap_update_bits(radio
->core
->regmap
,
873 SI476X_PROP_AUDIO_PWR_LINE_FILTER
,
874 SI476X_PROP_PWR_ENABLE_MASK
,
877 case V4L2_CID_POWER_LINE_FREQUENCY_50HZ
:
878 retval
= regmap_update_bits(radio
->core
->regmap
,
879 SI476X_PROP_AUDIO_PWR_LINE_FILTER
,
880 SI476X_PROP_PWR_GRID_MASK
,
881 SI476X_PROP_PWR_GRID_50HZ
);
883 case V4L2_CID_POWER_LINE_FREQUENCY_60HZ
:
884 retval
= regmap_update_bits(radio
->core
->regmap
,
885 SI476X_PROP_AUDIO_PWR_LINE_FILTER
,
886 SI476X_PROP_PWR_GRID_MASK
,
887 SI476X_PROP_PWR_GRID_60HZ
);
894 case V4L2_CID_SI476X_RSSI_THRESHOLD
:
895 retval
= regmap_write(radio
->core
->regmap
,
896 SI476X_PROP_VALID_RSSI_THRESHOLD
,
899 case V4L2_CID_SI476X_SNR_THRESHOLD
:
900 retval
= regmap_write(radio
->core
->regmap
,
901 SI476X_PROP_VALID_SNR_THRESHOLD
,
904 case V4L2_CID_SI476X_MAX_TUNE_ERROR
:
905 retval
= regmap_write(radio
->core
->regmap
,
906 SI476X_PROP_VALID_MAX_TUNE_ERROR
,
909 case V4L2_CID_RDS_RECEPTION
:
911 * It looks like RDS related properties are
912 * inaccessible when tuner is in AM mode, so cache the
915 if (si476x_core_is_in_am_receiver_mode(radio
->core
))
916 regcache_cache_only(radio
->core
->regmap
, true);
919 retval
= regmap_write(radio
->core
->regmap
,
920 SI476X_PROP_FM_RDS_INTERRUPT_FIFO_COUNT
,
921 radio
->core
->rds_fifo_depth
);
925 if (radio
->core
->client
->irq
) {
926 retval
= regmap_write(radio
->core
->regmap
,
927 SI476X_PROP_FM_RDS_INTERRUPT_SOURCE
,
933 /* Drain RDS FIFO before enabling RDS processing */
934 retval
= si476x_core_cmd_fm_rds_status(radio
->core
,
942 retval
= regmap_update_bits(radio
->core
->regmap
,
943 SI476X_PROP_FM_RDS_CONFIG
,
944 SI476X_PROP_RDSEN_MASK
,
947 retval
= regmap_update_bits(radio
->core
->regmap
,
948 SI476X_PROP_FM_RDS_CONFIG
,
949 SI476X_PROP_RDSEN_MASK
,
953 if (si476x_core_is_in_am_receiver_mode(radio
->core
))
954 regcache_cache_only(radio
->core
->regmap
, false);
956 case V4L2_CID_TUNE_DEEMPHASIS
:
957 retval
= regmap_write(radio
->core
->regmap
,
958 SI476X_PROP_AUDIO_DEEMPHASIS
,
962 case V4L2_CID_SI476X_DIVERSITY_MODE
:
963 mode
= si476x_phase_diversity_idx_to_mode(ctrl
->val
);
965 if (mode
== radio
->core
->diversity_mode
) {
970 if (si476x_core_is_in_am_receiver_mode(radio
->core
)) {
972 * Diversity cannot be configured while tuner
973 * is in AM mode so save the changes and carry on.
975 radio
->core
->diversity_mode
= mode
;
978 retval
= radio
->ops
->phase_diversity(radio
->core
, mode
);
980 radio
->core
->diversity_mode
= mode
;
989 si476x_core_unlock(radio
->core
);
994 #ifdef CONFIG_VIDEO_ADV_DEBUG
995 static int si476x_radio_g_register(struct file
*file
, void *fh
,
996 struct v4l2_dbg_register
*reg
)
1000 struct si476x_radio
*radio
= video_drvdata(file
);
1002 si476x_core_lock(radio
->core
);
1004 err
= regmap_read(radio
->core
->regmap
,
1005 (unsigned int)reg
->reg
, &value
);
1007 si476x_core_unlock(radio
->core
);
1011 static int si476x_radio_s_register(struct file
*file
, void *fh
,
1012 const struct v4l2_dbg_register
*reg
)
1016 struct si476x_radio
*radio
= video_drvdata(file
);
1018 si476x_core_lock(radio
->core
);
1019 err
= regmap_write(radio
->core
->regmap
,
1020 (unsigned int)reg
->reg
,
1021 (unsigned int)reg
->val
);
1022 si476x_core_unlock(radio
->core
);
1028 static int si476x_radio_fops_open(struct file
*file
)
1030 struct si476x_radio
*radio
= video_drvdata(file
);
1033 err
= v4l2_fh_open(file
);
1037 if (v4l2_fh_is_singular_file(file
)) {
1038 si476x_core_lock(radio
->core
);
1039 err
= si476x_core_set_power_state(radio
->core
,
1040 SI476X_POWER_UP_FULL
);
1044 err
= si476x_radio_do_post_powerup_init(radio
,
1045 radio
->core
->power_up_parameters
.func
);
1049 err
= si476x_radio_pretune(radio
,
1050 radio
->core
->power_up_parameters
.func
);
1054 si476x_core_unlock(radio
->core
);
1055 /*Must be done after si476x_core_unlock to prevent a deadlock*/
1056 v4l2_ctrl_handler_setup(&radio
->ctrl_handler
);
1062 si476x_core_set_power_state(radio
->core
,
1065 si476x_core_unlock(radio
->core
);
1066 v4l2_fh_release(file
);
1071 static int si476x_radio_fops_release(struct file
*file
)
1073 struct si476x_radio
*radio
= video_drvdata(file
);
1075 if (v4l2_fh_is_singular_file(file
) &&
1076 atomic_read(&radio
->core
->is_alive
))
1077 si476x_core_set_power_state(radio
->core
,
1080 return v4l2_fh_release(file
);
1083 static ssize_t
si476x_radio_fops_read(struct file
*file
, char __user
*buf
,
1084 size_t count
, loff_t
*ppos
)
1088 unsigned int copied
;
1090 struct si476x_radio
*radio
= video_drvdata(file
);
1092 /* block if no new data available */
1093 if (kfifo_is_empty(&radio
->core
->rds_fifo
)) {
1094 if (file
->f_flags
& O_NONBLOCK
)
1095 return -EWOULDBLOCK
;
1097 rval
= wait_event_interruptible(radio
->core
->rds_read_queue
,
1098 (!kfifo_is_empty(&radio
->core
->rds_fifo
) ||
1099 !atomic_read(&radio
->core
->is_alive
)));
1103 if (!atomic_read(&radio
->core
->is_alive
))
1107 fifo_len
= kfifo_len(&radio
->core
->rds_fifo
);
1109 if (kfifo_to_user(&radio
->core
->rds_fifo
, buf
,
1110 min(fifo_len
, count
),
1112 dev_warn(&radio
->videodev
.dev
,
1113 "Error during FIFO to userspace copy\n");
1116 rval
= (ssize_t
)copied
;
1122 static __poll_t
si476x_radio_fops_poll(struct file
*file
,
1123 struct poll_table_struct
*pts
)
1125 struct si476x_radio
*radio
= video_drvdata(file
);
1126 __poll_t req_events
= poll_requested_events(pts
);
1127 __poll_t err
= v4l2_ctrl_poll(file
, pts
);
1129 if (req_events
& (EPOLLIN
| EPOLLRDNORM
)) {
1130 if (atomic_read(&radio
->core
->is_alive
))
1131 poll_wait(file
, &radio
->core
->rds_read_queue
, pts
);
1133 if (!atomic_read(&radio
->core
->is_alive
))
1136 if (!kfifo_is_empty(&radio
->core
->rds_fifo
))
1137 err
= EPOLLIN
| EPOLLRDNORM
;
1143 static const struct v4l2_file_operations si476x_fops
= {
1144 .owner
= THIS_MODULE
,
1145 .read
= si476x_radio_fops_read
,
1146 .poll
= si476x_radio_fops_poll
,
1147 .unlocked_ioctl
= video_ioctl2
,
1148 .open
= si476x_radio_fops_open
,
1149 .release
= si476x_radio_fops_release
,
1153 static const struct v4l2_ioctl_ops si4761_ioctl_ops
= {
1154 .vidioc_querycap
= si476x_radio_querycap
,
1155 .vidioc_g_tuner
= si476x_radio_g_tuner
,
1156 .vidioc_s_tuner
= si476x_radio_s_tuner
,
1158 .vidioc_g_frequency
= si476x_radio_g_frequency
,
1159 .vidioc_s_frequency
= si476x_radio_s_frequency
,
1160 .vidioc_s_hw_freq_seek
= si476x_radio_s_hw_freq_seek
,
1161 .vidioc_enum_freq_bands
= si476x_radio_enum_freq_bands
,
1163 .vidioc_subscribe_event
= v4l2_ctrl_subscribe_event
,
1164 .vidioc_unsubscribe_event
= v4l2_event_unsubscribe
,
1166 #ifdef CONFIG_VIDEO_ADV_DEBUG
1167 .vidioc_g_register
= si476x_radio_g_register
,
1168 .vidioc_s_register
= si476x_radio_s_register
,
1173 static const struct video_device si476x_viddev_template
= {
1174 .fops
= &si476x_fops
,
1175 .name
= DRIVER_NAME
,
1176 .release
= video_device_release_empty
,
1181 static ssize_t
si476x_radio_read_acf_blob(struct file
*file
,
1182 char __user
*user_buf
,
1183 size_t count
, loff_t
*ppos
)
1186 struct si476x_radio
*radio
= file
->private_data
;
1187 struct si476x_acf_status_report report
;
1189 si476x_core_lock(radio
->core
);
1190 if (radio
->ops
->acf_status
)
1191 err
= radio
->ops
->acf_status(radio
->core
, &report
);
1194 si476x_core_unlock(radio
->core
);
1199 return simple_read_from_buffer(user_buf
, count
, ppos
, &report
,
1203 static const struct file_operations radio_acf_fops
= {
1204 .open
= simple_open
,
1205 .llseek
= default_llseek
,
1206 .read
= si476x_radio_read_acf_blob
,
1209 static ssize_t
si476x_radio_read_rds_blckcnt_blob(struct file
*file
,
1210 char __user
*user_buf
,
1211 size_t count
, loff_t
*ppos
)
1214 struct si476x_radio
*radio
= file
->private_data
;
1215 struct si476x_rds_blockcount_report report
;
1217 si476x_core_lock(radio
->core
);
1218 if (radio
->ops
->rds_blckcnt
)
1219 err
= radio
->ops
->rds_blckcnt(radio
->core
, true,
1223 si476x_core_unlock(radio
->core
);
1228 return simple_read_from_buffer(user_buf
, count
, ppos
, &report
,
1232 static const struct file_operations radio_rds_blckcnt_fops
= {
1233 .open
= simple_open
,
1234 .llseek
= default_llseek
,
1235 .read
= si476x_radio_read_rds_blckcnt_blob
,
1238 static ssize_t
si476x_radio_read_agc_blob(struct file
*file
,
1239 char __user
*user_buf
,
1240 size_t count
, loff_t
*ppos
)
1243 struct si476x_radio
*radio
= file
->private_data
;
1244 struct si476x_agc_status_report report
;
1246 si476x_core_lock(radio
->core
);
1247 if (radio
->ops
->rds_blckcnt
)
1248 err
= radio
->ops
->agc_status(radio
->core
, &report
);
1251 si476x_core_unlock(radio
->core
);
1256 return simple_read_from_buffer(user_buf
, count
, ppos
, &report
,
1260 static const struct file_operations radio_agc_fops
= {
1261 .open
= simple_open
,
1262 .llseek
= default_llseek
,
1263 .read
= si476x_radio_read_agc_blob
,
1266 static ssize_t
si476x_radio_read_rsq_blob(struct file
*file
,
1267 char __user
*user_buf
,
1268 size_t count
, loff_t
*ppos
)
1271 struct si476x_radio
*radio
= file
->private_data
;
1272 struct si476x_rsq_status_report report
;
1273 struct si476x_rsq_status_args args
= {
1281 si476x_core_lock(radio
->core
);
1282 if (radio
->ops
->rds_blckcnt
)
1283 err
= radio
->ops
->rsq_status(radio
->core
, &args
, &report
);
1286 si476x_core_unlock(radio
->core
);
1291 return simple_read_from_buffer(user_buf
, count
, ppos
, &report
,
1295 static const struct file_operations radio_rsq_fops
= {
1296 .open
= simple_open
,
1297 .llseek
= default_llseek
,
1298 .read
= si476x_radio_read_rsq_blob
,
1301 static ssize_t
si476x_radio_read_rsq_primary_blob(struct file
*file
,
1302 char __user
*user_buf
,
1303 size_t count
, loff_t
*ppos
)
1306 struct si476x_radio
*radio
= file
->private_data
;
1307 struct si476x_rsq_status_report report
;
1308 struct si476x_rsq_status_args args
= {
1316 si476x_core_lock(radio
->core
);
1317 if (radio
->ops
->rds_blckcnt
)
1318 err
= radio
->ops
->rsq_status(radio
->core
, &args
, &report
);
1321 si476x_core_unlock(radio
->core
);
1326 return simple_read_from_buffer(user_buf
, count
, ppos
, &report
,
1330 static const struct file_operations radio_rsq_primary_fops
= {
1331 .open
= simple_open
,
1332 .llseek
= default_llseek
,
1333 .read
= si476x_radio_read_rsq_primary_blob
,
1337 static void si476x_radio_init_debugfs(struct si476x_radio
*radio
)
1339 radio
->debugfs
= debugfs_create_dir(dev_name(radio
->v4l2dev
.dev
), NULL
);
1341 debugfs_create_file("acf", S_IRUGO
, radio
->debugfs
, radio
,
1344 debugfs_create_file("rds_blckcnt", S_IRUGO
, radio
->debugfs
, radio
,
1345 &radio_rds_blckcnt_fops
);
1347 debugfs_create_file("agc", S_IRUGO
, radio
->debugfs
, radio
,
1350 debugfs_create_file("rsq", S_IRUGO
, radio
->debugfs
, radio
,
1353 debugfs_create_file("rsq_primary", S_IRUGO
, radio
->debugfs
, radio
,
1354 &radio_rsq_primary_fops
);
1358 static int si476x_radio_add_new_custom(struct si476x_radio
*radio
,
1359 enum si476x_ctrl_idx idx
)
1362 struct v4l2_ctrl
*ctrl
;
1364 ctrl
= v4l2_ctrl_new_custom(&radio
->ctrl_handler
,
1367 rval
= radio
->ctrl_handler
.error
;
1368 if (ctrl
== NULL
&& rval
)
1369 dev_err(radio
->v4l2dev
.dev
,
1370 "Could not initialize '%s' control %d\n",
1371 si476x_ctrls
[idx
].name
, rval
);
1376 static int si476x_radio_probe(struct platform_device
*pdev
)
1379 struct si476x_radio
*radio
;
1380 struct v4l2_ctrl
*ctrl
;
1382 static atomic_t instance
= ATOMIC_INIT(0);
1384 radio
= devm_kzalloc(&pdev
->dev
, sizeof(*radio
), GFP_KERNEL
);
1388 radio
->core
= i2c_mfd_cell_to_core(&pdev
->dev
);
1390 v4l2_device_set_name(&radio
->v4l2dev
, DRIVER_NAME
, &instance
);
1392 rval
= v4l2_device_register(&pdev
->dev
, &radio
->v4l2dev
);
1394 dev_err(&pdev
->dev
, "Cannot register v4l2_device.\n");
1398 memcpy(&radio
->videodev
, &si476x_viddev_template
,
1399 sizeof(struct video_device
));
1401 radio
->videodev
.v4l2_dev
= &radio
->v4l2dev
;
1402 radio
->videodev
.ioctl_ops
= &si4761_ioctl_ops
;
1403 radio
->videodev
.device_caps
= V4L2_CAP_TUNER
| V4L2_CAP_RADIO
|
1404 V4L2_CAP_HW_FREQ_SEEK
;
1406 si476x_core_lock(radio
->core
);
1407 if (!si476x_core_is_a_secondary_tuner(radio
->core
))
1408 radio
->videodev
.device_caps
|= V4L2_CAP_RDS_CAPTURE
|
1410 si476x_core_unlock(radio
->core
);
1412 video_set_drvdata(&radio
->videodev
, radio
);
1413 platform_set_drvdata(pdev
, radio
);
1416 radio
->v4l2dev
.ctrl_handler
= &radio
->ctrl_handler
;
1417 v4l2_ctrl_handler_init(&radio
->ctrl_handler
,
1418 1 + ARRAY_SIZE(si476x_ctrls
));
1420 if (si476x_core_has_am(radio
->core
)) {
1421 ctrl
= v4l2_ctrl_new_std_menu(&radio
->ctrl_handler
,
1423 V4L2_CID_POWER_LINE_FREQUENCY
,
1424 V4L2_CID_POWER_LINE_FREQUENCY_60HZ
,
1426 rval
= radio
->ctrl_handler
.error
;
1427 if (ctrl
== NULL
&& rval
) {
1428 dev_err(&pdev
->dev
, "Could not initialize V4L2_CID_POWER_LINE_FREQUENCY control %d\n",
1433 rval
= si476x_radio_add_new_custom(radio
,
1434 SI476X_IDX_HARMONICS_COUNT
);
1439 rval
= si476x_radio_add_new_custom(radio
, SI476X_IDX_RSSI_THRESHOLD
);
1443 rval
= si476x_radio_add_new_custom(radio
, SI476X_IDX_SNR_THRESHOLD
);
1447 rval
= si476x_radio_add_new_custom(radio
, SI476X_IDX_MAX_TUNE_ERROR
);
1451 ctrl
= v4l2_ctrl_new_std_menu(&radio
->ctrl_handler
,
1453 V4L2_CID_TUNE_DEEMPHASIS
,
1454 V4L2_DEEMPHASIS_75_uS
, 0, 0);
1455 rval
= radio
->ctrl_handler
.error
;
1456 if (ctrl
== NULL
&& rval
) {
1457 dev_err(&pdev
->dev
, "Could not initialize V4L2_CID_TUNE_DEEMPHASIS control %d\n",
1462 ctrl
= v4l2_ctrl_new_std(&radio
->ctrl_handler
, &si476x_ctrl_ops
,
1463 V4L2_CID_RDS_RECEPTION
,
1465 rval
= radio
->ctrl_handler
.error
;
1466 if (ctrl
== NULL
&& rval
) {
1467 dev_err(&pdev
->dev
, "Could not initialize V4L2_CID_RDS_RECEPTION control %d\n",
1472 if (si476x_core_has_diversity(radio
->core
)) {
1473 si476x_ctrls
[SI476X_IDX_DIVERSITY_MODE
].def
=
1474 si476x_phase_diversity_mode_to_idx(radio
->core
->diversity_mode
);
1475 rval
= si476x_radio_add_new_custom(radio
, SI476X_IDX_DIVERSITY_MODE
);
1479 rval
= si476x_radio_add_new_custom(radio
, SI476X_IDX_INTERCHIP_LINK
);
1484 /* register video device */
1485 rval
= video_register_device(&radio
->videodev
, VFL_TYPE_RADIO
, -1);
1487 dev_err(&pdev
->dev
, "Could not register video device\n");
1491 si476x_radio_init_debugfs(radio
);
1495 v4l2_ctrl_handler_free(radio
->videodev
.ctrl_handler
);
1499 static void si476x_radio_remove(struct platform_device
*pdev
)
1501 struct si476x_radio
*radio
= platform_get_drvdata(pdev
);
1503 v4l2_ctrl_handler_free(radio
->videodev
.ctrl_handler
);
1504 video_unregister_device(&radio
->videodev
);
1505 v4l2_device_unregister(&radio
->v4l2dev
);
1506 debugfs_remove_recursive(radio
->debugfs
);
1509 MODULE_ALIAS("platform:si476x-radio");
1511 static struct platform_driver si476x_radio_driver
= {
1513 .name
= DRIVER_NAME
,
1515 .probe
= si476x_radio_probe
,
1516 .remove
= si476x_radio_remove
,
1518 module_platform_driver(si476x_radio_driver
);
1520 MODULE_AUTHOR("Andrey Smirnov <andrew.smirnov@gmail.com>");
1521 MODULE_DESCRIPTION("Driver for Si4761/64/68 AM/FM Radio MFD Cell");
1522 MODULE_LICENSE("GPL");