2 * drivers/media/radio/radio-si476x.c -- V4L2 driver for SI476X chips
4 * Copyright (C) 2012 Innovative Converged Devices(ICD)
5 * Copyright (C) 2013 Andrey Smirnov
7 * Author: Andrey Smirnov <andrew.smirnov@gmail.com>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; version 2 of the License.
13 * This program is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * General Public License for more details.
20 #include <linux/module.h>
21 #include <linux/delay.h>
22 #include <linux/interrupt.h>
23 #include <linux/slab.h>
24 #include <linux/atomic.h>
25 #include <linux/videodev2.h>
26 #include <linux/mutex.h>
27 #include <linux/debugfs.h>
28 #include <media/v4l2-common.h>
29 #include <media/v4l2-ioctl.h>
30 #include <media/v4l2-ctrls.h>
31 #include <media/v4l2-event.h>
32 #include <media/v4l2-device.h>
34 #include <media/si476x.h>
35 #include <linux/mfd/si476x-core.h>
37 #define FM_FREQ_RANGE_LOW 64000000
38 #define FM_FREQ_RANGE_HIGH 108000000
40 #define AM_FREQ_RANGE_LOW 520000
41 #define AM_FREQ_RANGE_HIGH 30000000
43 #define PWRLINEFLTR (1 << 8)
45 #define FREQ_MUL (10000000 / 625)
47 #define SI476X_PHDIV_STATUS_LINK_LOCKED(status) (0x80 & (status))
49 #define DRIVER_NAME "si476x-radio"
50 #define DRIVER_CARD "SI476x AM/FM Receiver"
52 enum si476x_freq_bands {
57 static const struct v4l2_frequency_band si476x_bands[] = {
59 .type = V4L2_TUNER_RADIO,
60 .index = SI476X_BAND_FM,
61 .capability = V4L2_TUNER_CAP_LOW
62 | V4L2_TUNER_CAP_STEREO
64 | V4L2_TUNER_CAP_RDS_BLOCK_IO
65 | V4L2_TUNER_CAP_FREQ_BANDS,
66 .rangelow = 64 * FREQ_MUL,
67 .rangehigh = 108 * FREQ_MUL,
68 .modulation = V4L2_BAND_MODULATION_FM,
71 .type = V4L2_TUNER_RADIO,
72 .index = SI476X_BAND_AM,
73 .capability = V4L2_TUNER_CAP_LOW
74 | V4L2_TUNER_CAP_FREQ_BANDS,
75 .rangelow = 0.52 * FREQ_MUL,
76 .rangehigh = 30 * FREQ_MUL,
77 .modulation = V4L2_BAND_MODULATION_AM,
81 static inline bool si476x_radio_freq_is_inside_of_the_band(u32 freq, int band)
83 return freq >= si476x_bands[band].rangelow &&
84 freq <= si476x_bands[band].rangehigh;
87 static inline bool si476x_radio_range_is_inside_of_the_band(u32 low, u32 high,
90 return low >= si476x_bands[band].rangelow &&
91 high <= si476x_bands[band].rangehigh;
94 static int si476x_radio_s_ctrl(struct v4l2_ctrl *ctrl);
95 static int si476x_radio_g_volatile_ctrl(struct v4l2_ctrl *ctrl);
97 enum phase_diversity_modes_idx {
98 SI476X_IDX_PHDIV_DISABLED,
99 SI476X_IDX_PHDIV_PRIMARY_COMBINING,
100 SI476X_IDX_PHDIV_PRIMARY_ANTENNA,
101 SI476X_IDX_PHDIV_SECONDARY_ANTENNA,
102 SI476X_IDX_PHDIV_SECONDARY_COMBINING,
105 static const char * const phase_diversity_modes[] = {
106 [SI476X_IDX_PHDIV_DISABLED] = "Disabled",
107 [SI476X_IDX_PHDIV_PRIMARY_COMBINING] = "Primary with Secondary",
108 [SI476X_IDX_PHDIV_PRIMARY_ANTENNA] = "Primary Antenna",
109 [SI476X_IDX_PHDIV_SECONDARY_ANTENNA] = "Secondary Antenna",
110 [SI476X_IDX_PHDIV_SECONDARY_COMBINING] = "Secondary with Primary",
113 static inline enum phase_diversity_modes_idx
114 si476x_phase_diversity_mode_to_idx(enum si476x_phase_diversity_mode mode)
117 default: /* FALLTHROUGH */
118 case SI476X_PHDIV_DISABLED:
119 return SI476X_IDX_PHDIV_DISABLED;
120 case SI476X_PHDIV_PRIMARY_COMBINING:
121 return SI476X_IDX_PHDIV_PRIMARY_COMBINING;
122 case SI476X_PHDIV_PRIMARY_ANTENNA:
123 return SI476X_IDX_PHDIV_PRIMARY_ANTENNA;
124 case SI476X_PHDIV_SECONDARY_ANTENNA:
125 return SI476X_IDX_PHDIV_SECONDARY_ANTENNA;
126 case SI476X_PHDIV_SECONDARY_COMBINING:
127 return SI476X_IDX_PHDIV_SECONDARY_COMBINING;
131 static inline enum si476x_phase_diversity_mode
132 si476x_phase_diversity_idx_to_mode(enum phase_diversity_modes_idx idx)
134 static const int idx_to_value[] = {
135 [SI476X_IDX_PHDIV_DISABLED] = SI476X_PHDIV_DISABLED,
136 [SI476X_IDX_PHDIV_PRIMARY_COMBINING] = SI476X_PHDIV_PRIMARY_COMBINING,
137 [SI476X_IDX_PHDIV_PRIMARY_ANTENNA] = SI476X_PHDIV_PRIMARY_ANTENNA,
138 [SI476X_IDX_PHDIV_SECONDARY_ANTENNA] = SI476X_PHDIV_SECONDARY_ANTENNA,
139 [SI476X_IDX_PHDIV_SECONDARY_COMBINING] = SI476X_PHDIV_SECONDARY_COMBINING,
142 return idx_to_value[idx];
145 static const struct v4l2_ctrl_ops si476x_ctrl_ops = {
146 .g_volatile_ctrl = si476x_radio_g_volatile_ctrl,
147 .s_ctrl = si476x_radio_s_ctrl,
151 enum si476x_ctrl_idx {
152 SI476X_IDX_RSSI_THRESHOLD,
153 SI476X_IDX_SNR_THRESHOLD,
154 SI476X_IDX_MAX_TUNE_ERROR,
155 SI476X_IDX_HARMONICS_COUNT,
156 SI476X_IDX_DIVERSITY_MODE,
157 SI476X_IDX_INTERCHIP_LINK,
159 static struct v4l2_ctrl_config si476x_ctrls[] = {
162 * SI476X during its station seeking(or tuning) process uses several
163 * parameters to detrmine if "the station" is valid:
165 * - Signal's SNR(in dBuV) must be lower than
166 * #V4L2_CID_SI476X_SNR_THRESHOLD
167 * - Signal's RSSI(in dBuV) must be greater than
168 * #V4L2_CID_SI476X_RSSI_THRESHOLD
169 * - Signal's frequency deviation(in units of 2ppm) must not be
170 * more than #V4L2_CID_SI476X_MAX_TUNE_ERROR
172 [SI476X_IDX_RSSI_THRESHOLD] = {
173 .ops = &si476x_ctrl_ops,
174 .id = V4L2_CID_SI476X_RSSI_THRESHOLD,
175 .name = "Valid RSSI Threshold",
176 .type = V4L2_CTRL_TYPE_INTEGER,
181 [SI476X_IDX_SNR_THRESHOLD] = {
182 .ops = &si476x_ctrl_ops,
183 .id = V4L2_CID_SI476X_SNR_THRESHOLD,
184 .type = V4L2_CTRL_TYPE_INTEGER,
185 .name = "Valid SNR Threshold",
190 [SI476X_IDX_MAX_TUNE_ERROR] = {
191 .ops = &si476x_ctrl_ops,
192 .id = V4L2_CID_SI476X_MAX_TUNE_ERROR,
193 .type = V4L2_CTRL_TYPE_INTEGER,
194 .name = "Max Tune Errors",
201 * #V4L2_CID_SI476X_HARMONICS_COUNT -- number of harmonics
202 * built-in power-line noise supression filter is to reject
203 * during AM-mode operation.
205 [SI476X_IDX_HARMONICS_COUNT] = {
206 .ops = &si476x_ctrl_ops,
207 .id = V4L2_CID_SI476X_HARMONICS_COUNT,
208 .type = V4L2_CTRL_TYPE_INTEGER,
210 .name = "Count of Harmonics to Reject",
217 * #V4L2_CID_SI476X_DIVERSITY_MODE -- configuration which
218 * two tuners working in diversity mode are to work in.
220 * - #SI476X_IDX_PHDIV_DISABLED diversity mode disabled
221 * - #SI476X_IDX_PHDIV_PRIMARY_COMBINING diversity mode is
222 * on, primary tuner's antenna is the main one.
223 * - #SI476X_IDX_PHDIV_PRIMARY_ANTENNA diversity mode is
224 * off, primary tuner's antenna is the main one.
225 * - #SI476X_IDX_PHDIV_SECONDARY_ANTENNA diversity mode is
226 * off, secondary tuner's antenna is the main one.
227 * - #SI476X_IDX_PHDIV_SECONDARY_COMBINING diversity mode is
228 * on, secondary tuner's antenna is the main one.
230 [SI476X_IDX_DIVERSITY_MODE] = {
231 .ops = &si476x_ctrl_ops,
232 .id = V4L2_CID_SI476X_DIVERSITY_MODE,
233 .type = V4L2_CTRL_TYPE_MENU,
234 .name = "Phase Diversity Mode",
235 .qmenu = phase_diversity_modes,
237 .max = ARRAY_SIZE(phase_diversity_modes) - 1,
241 * #V4L2_CID_SI476X_INTERCHIP_LINK -- inter-chip link in
242 * diversity mode indicator. Allows user to determine if two
243 * chips working in diversity mode have established a link
244 * between each other and if the system as a whole uses
245 * signals from both antennas to receive FM radio.
247 [SI476X_IDX_INTERCHIP_LINK] = {
248 .ops = &si476x_ctrl_ops,
249 .id = V4L2_CID_SI476X_INTERCHIP_LINK,
250 .type = V4L2_CTRL_TYPE_BOOLEAN,
251 .flags = V4L2_CTRL_FLAG_READ_ONLY | V4L2_CTRL_FLAG_VOLATILE,
252 .name = "Inter-Chip Link",
262 * struct si476x_radio_ops - vtable of tuner functions
264 * This table holds pointers to functions implementing particular
265 * operations depending on the mode in which the tuner chip was
266 * configured to start in. If the function is not supported
267 * corresponding element is set to #NULL.
269 * @tune_freq: Tune chip to a specific frequency
270 * @seek_start: Star station seeking
271 * @rsq_status: Get Recieved Signal Quality(RSQ) status
272 * @rds_blckcnt: Get recived RDS blocks count
273 * @phase_diversity: Change phase diversity mode of the tuner
274 * @phase_div_status: Get phase diversity mode status
275 * @acf_status: Get the status of Automatically Controlled
277 * @agc_status: Get Automatic Gain Control(AGC) status
279 struct si476x_radio_ops {
280 int (*tune_freq)(struct si476x_core *, struct si476x_tune_freq_args *);
281 int (*seek_start)(struct si476x_core *, bool, bool);
282 int (*rsq_status)(struct si476x_core *, struct si476x_rsq_status_args *,
283 struct si476x_rsq_status_report *);
284 int (*rds_blckcnt)(struct si476x_core *, bool,
285 struct si476x_rds_blockcount_report *);
287 int (*phase_diversity)(struct si476x_core *,
288 enum si476x_phase_diversity_mode);
289 int (*phase_div_status)(struct si476x_core *);
290 int (*acf_status)(struct si476x_core *,
291 struct si476x_acf_status_report *);
292 int (*agc_status)(struct si476x_core *,
293 struct si476x_agc_status_report *);
297 * struct si476x_radio - radio device
299 * @core: Pointer to underlying core device
300 * @videodev: Pointer to video device created by V4L2 subsystem
301 * @ops: Vtable of functions. See struct si476x_radio_ops for details
302 * @kref: Reference counter
303 * @core_lock: An r/w semaphore to brebvent the deletion of underlying
304 * core structure is the radio device is being used
306 struct si476x_radio {
307 struct v4l2_device v4l2dev;
308 struct video_device videodev;
309 struct v4l2_ctrl_handler ctrl_handler;
311 struct si476x_core *core;
312 /* This field should not be accesses unless core lock is held */
313 const struct si476x_radio_ops *ops;
315 struct dentry *debugfs;
319 static inline struct si476x_radio *
320 v4l2_dev_to_radio(struct v4l2_device *d)
322 return container_of(d, struct si476x_radio, v4l2dev);
325 static inline struct si476x_radio *
326 v4l2_ctrl_handler_to_radio(struct v4l2_ctrl_handler *d)
328 return container_of(d, struct si476x_radio, ctrl_handler);
332 * si476x_vidioc_querycap - query device capabilities
334 static int si476x_radio_querycap(struct file *file, void *priv,
335 struct v4l2_capability *capability)
337 struct si476x_radio *radio = video_drvdata(file);
339 strlcpy(capability->driver, radio->v4l2dev.name,
340 sizeof(capability->driver));
341 strlcpy(capability->card, DRIVER_CARD, sizeof(capability->card));
342 snprintf(capability->bus_info, sizeof(capability->bus_info),
343 "platform:%s", radio->v4l2dev.name);
345 capability->device_caps = V4L2_CAP_TUNER
347 | V4L2_CAP_HW_FREQ_SEEK;
349 si476x_core_lock(radio->core);
350 if (!si476x_core_is_a_secondary_tuner(radio->core))
351 capability->device_caps |= V4L2_CAP_RDS_CAPTURE
352 | V4L2_CAP_READWRITE;
353 si476x_core_unlock(radio->core);
355 capability->capabilities = capability->device_caps
356 | V4L2_CAP_DEVICE_CAPS;
360 static int si476x_radio_enum_freq_bands(struct file *file, void *priv,
361 struct v4l2_frequency_band *band)
364 struct si476x_radio *radio = video_drvdata(file);
366 if (band->tuner != 0)
369 switch (radio->core->chip_id) {
370 /* AM/FM tuners -- all bands are supported */
371 case SI476X_CHIP_SI4761:
372 case SI476X_CHIP_SI4764:
373 if (band->index < ARRAY_SIZE(si476x_bands)) {
374 *band = si476x_bands[band->index];
380 /* FM companion tuner chips -- only FM bands are
382 case SI476X_CHIP_SI4768:
383 if (band->index == SI476X_BAND_FM) {
384 *band = si476x_bands[band->index];
397 static int si476x_radio_g_tuner(struct file *file, void *priv,
398 struct v4l2_tuner *tuner)
401 struct si476x_rsq_status_report report;
402 struct si476x_radio *radio = video_drvdata(file);
404 struct si476x_rsq_status_args args = {
412 if (tuner->index != 0)
415 tuner->type = V4L2_TUNER_RADIO;
416 tuner->capability = V4L2_TUNER_CAP_LOW /* Measure frequencies
419 | V4L2_TUNER_CAP_STEREO
420 | V4L2_TUNER_CAP_HWSEEK_BOUNDED
421 | V4L2_TUNER_CAP_HWSEEK_WRAP
422 | V4L2_TUNER_CAP_HWSEEK_PROG_LIM;
424 si476x_core_lock(radio->core);
426 if (si476x_core_is_a_secondary_tuner(radio->core)) {
427 strlcpy(tuner->name, "FM (secondary)", sizeof(tuner->name));
428 tuner->rxsubchans = 0;
429 tuner->rangelow = si476x_bands[SI476X_BAND_FM].rangelow;
430 } else if (si476x_core_has_am(radio->core)) {
431 if (si476x_core_is_a_primary_tuner(radio->core))
432 strlcpy(tuner->name, "AM/FM (primary)",
433 sizeof(tuner->name));
435 strlcpy(tuner->name, "AM/FM", sizeof(tuner->name));
437 tuner->rxsubchans = V4L2_TUNER_SUB_MONO | V4L2_TUNER_SUB_STEREO
438 | V4L2_TUNER_SUB_RDS;
439 tuner->capability |= V4L2_TUNER_CAP_RDS
440 | V4L2_TUNER_CAP_RDS_BLOCK_IO
441 | V4L2_TUNER_CAP_FREQ_BANDS;
443 tuner->rangelow = si476x_bands[SI476X_BAND_AM].rangelow;
445 strlcpy(tuner->name, "FM", sizeof(tuner->name));
446 tuner->rxsubchans = V4L2_TUNER_SUB_RDS;
447 tuner->capability |= V4L2_TUNER_CAP_RDS
448 | V4L2_TUNER_CAP_RDS_BLOCK_IO
449 | V4L2_TUNER_CAP_FREQ_BANDS;
450 tuner->rangelow = si476x_bands[SI476X_BAND_FM].rangelow;
453 tuner->audmode = radio->audmode;
456 tuner->rangehigh = si476x_bands[SI476X_BAND_FM].rangehigh;
458 err = radio->ops->rsq_status(radio->core,
464 * tuner->signal value range: 0x0000 .. 0xFFFF,
465 * report.rssi: -128 .. 127
467 tuner->signal = (report.rssi + 128) * 257;
469 si476x_core_unlock(radio->core);
474 static int si476x_radio_s_tuner(struct file *file, void *priv,
475 const struct v4l2_tuner *tuner)
477 struct si476x_radio *radio = video_drvdata(file);
479 if (tuner->index != 0)
482 if (tuner->audmode == V4L2_TUNER_MODE_MONO ||
483 tuner->audmode == V4L2_TUNER_MODE_STEREO)
484 radio->audmode = tuner->audmode;
486 radio->audmode = V4L2_TUNER_MODE_STEREO;
491 static int si476x_radio_init_vtable(struct si476x_radio *radio,
492 enum si476x_func func)
494 static const struct si476x_radio_ops fm_ops = {
495 .tune_freq = si476x_core_cmd_fm_tune_freq,
496 .seek_start = si476x_core_cmd_fm_seek_start,
497 .rsq_status = si476x_core_cmd_fm_rsq_status,
498 .rds_blckcnt = si476x_core_cmd_fm_rds_blockcount,
499 .phase_diversity = si476x_core_cmd_fm_phase_diversity,
500 .phase_div_status = si476x_core_cmd_fm_phase_div_status,
501 .acf_status = si476x_core_cmd_fm_acf_status,
502 .agc_status = si476x_core_cmd_agc_status,
505 static const struct si476x_radio_ops am_ops = {
506 .tune_freq = si476x_core_cmd_am_tune_freq,
507 .seek_start = si476x_core_cmd_am_seek_start,
508 .rsq_status = si476x_core_cmd_am_rsq_status,
510 .phase_diversity = NULL,
511 .phase_div_status = NULL,
512 .acf_status = si476x_core_cmd_am_acf_status,
517 case SI476X_FUNC_FM_RECEIVER:
518 radio->ops = &fm_ops;
521 case SI476X_FUNC_AM_RECEIVER:
522 radio->ops = &am_ops;
525 WARN(1, "Unexpected tuner function value\n");
530 static int si476x_radio_pretune(struct si476x_radio *radio,
531 enum si476x_func func)
535 struct si476x_tune_freq_args args = {
538 .injside = SI476X_INJSIDE_AUTO,
539 .tunemode = SI476X_TM_VALIDATED_NORMAL_TUNE,
540 .smoothmetrics = SI476X_SM_INITIALIZE_AUDIO,
545 case SI476X_FUNC_FM_RECEIVER:
546 args.freq = v4l2_to_si476x(radio->core,
548 retval = radio->ops->tune_freq(radio->core, &args);
550 case SI476X_FUNC_AM_RECEIVER:
551 args.freq = v4l2_to_si476x(radio->core,
553 retval = radio->ops->tune_freq(radio->core, &args);
556 WARN(1, "Unexpected tuner function value\n");
562 static int si476x_radio_do_post_powerup_init(struct si476x_radio *radio,
563 enum si476x_func func)
567 /* regcache_mark_dirty(radio->core->regmap); */
568 err = regcache_sync_region(radio->core->regmap,
569 SI476X_PROP_DIGITAL_IO_INPUT_SAMPLE_RATE,
570 SI476X_PROP_DIGITAL_IO_OUTPUT_FORMAT);
574 err = regcache_sync_region(radio->core->regmap,
575 SI476X_PROP_AUDIO_DEEMPHASIS,
576 SI476X_PROP_AUDIO_PWR_LINE_FILTER);
580 err = regcache_sync_region(radio->core->regmap,
581 SI476X_PROP_INT_CTL_ENABLE,
582 SI476X_PROP_INT_CTL_ENABLE);
587 * Is there any point in restoring SNR and the like
588 * when switching between AM/FM?
590 err = regcache_sync_region(radio->core->regmap,
591 SI476X_PROP_VALID_MAX_TUNE_ERROR,
592 SI476X_PROP_VALID_MAX_TUNE_ERROR);
596 err = regcache_sync_region(radio->core->regmap,
597 SI476X_PROP_VALID_SNR_THRESHOLD,
598 SI476X_PROP_VALID_RSSI_THRESHOLD);
602 if (func == SI476X_FUNC_FM_RECEIVER) {
603 if (si476x_core_has_diversity(radio->core)) {
604 err = si476x_core_cmd_fm_phase_diversity(radio->core,
605 radio->core->diversity_mode);
610 err = regcache_sync_region(radio->core->regmap,
611 SI476X_PROP_FM_RDS_INTERRUPT_SOURCE,
612 SI476X_PROP_FM_RDS_CONFIG);
617 return si476x_radio_init_vtable(radio, func);
621 static int si476x_radio_change_func(struct si476x_radio *radio,
622 enum si476x_func func)
627 * Since power/up down is a very time consuming operation,
628 * try to avoid doing it if the requested mode matches the one
631 if (func == radio->core->power_up_parameters.func)
635 err = si476x_core_stop(radio->core, soft);
638 * OK, if the chip does not want to play nice let's
639 * try to reset it in more brutal way
642 err = si476x_core_stop(radio->core, soft);
647 Set the desired radio tuner function
649 radio->core->power_up_parameters.func = func;
651 err = si476x_core_start(radio->core, soft);
656 * No need to do the rest of manipulations for the bootlader
659 if (func != SI476X_FUNC_FM_RECEIVER &&
660 func != SI476X_FUNC_AM_RECEIVER)
663 return si476x_radio_do_post_powerup_init(radio, func);
666 static int si476x_radio_g_frequency(struct file *file, void *priv,
667 struct v4l2_frequency *f)
670 struct si476x_radio *radio = video_drvdata(file);
673 f->type != V4L2_TUNER_RADIO)
676 si476x_core_lock(radio->core);
678 if (radio->ops->rsq_status) {
679 struct si476x_rsq_status_report report;
680 struct si476x_rsq_status_args args = {
688 err = radio->ops->rsq_status(radio->core, &args, &report);
690 f->frequency = si476x_to_v4l2(radio->core,
696 si476x_core_unlock(radio->core);
701 static int si476x_radio_s_frequency(struct file *file, void *priv,
702 const struct v4l2_frequency *f)
705 u32 freq = f->frequency;
706 struct si476x_tune_freq_args args;
707 struct si476x_radio *radio = video_drvdata(file);
709 const u32 midrange = (si476x_bands[SI476X_BAND_AM].rangehigh +
710 si476x_bands[SI476X_BAND_FM].rangelow) / 2;
711 const int band = (freq > midrange) ?
712 SI476X_BAND_FM : SI476X_BAND_AM;
713 const enum si476x_func func = (band == SI476X_BAND_AM) ?
714 SI476X_FUNC_AM_RECEIVER : SI476X_FUNC_FM_RECEIVER;
717 f->type != V4L2_TUNER_RADIO)
720 si476x_core_lock(radio->core);
723 si476x_bands[band].rangelow,
724 si476x_bands[band].rangehigh);
726 if (si476x_radio_freq_is_inside_of_the_band(freq,
728 (!si476x_core_has_am(radio->core) ||
729 si476x_core_is_a_secondary_tuner(radio->core))) {
734 err = si476x_radio_change_func(radio, func);
740 args.injside = SI476X_INJSIDE_AUTO;
741 args.freq = v4l2_to_si476x(radio->core, freq);
742 args.tunemode = SI476X_TM_VALIDATED_NORMAL_TUNE;
743 args.smoothmetrics = SI476X_SM_INITIALIZE_AUDIO;
746 err = radio->ops->tune_freq(radio->core, &args);
749 si476x_core_unlock(radio->core);
753 static int si476x_radio_s_hw_freq_seek(struct file *file, void *priv,
754 const struct v4l2_hw_freq_seek *seek)
757 enum si476x_func func;
758 u32 rangelow, rangehigh;
759 struct si476x_radio *radio = video_drvdata(file);
761 if (file->f_flags & O_NONBLOCK)
764 if (seek->tuner != 0 ||
765 seek->type != V4L2_TUNER_RADIO)
768 si476x_core_lock(radio->core);
770 if (!seek->rangelow) {
771 err = regmap_read(radio->core->regmap,
772 SI476X_PROP_SEEK_BAND_BOTTOM,
775 rangelow = si476x_to_v4l2(radio->core, rangelow);
779 if (!seek->rangehigh) {
780 err = regmap_read(radio->core->regmap,
781 SI476X_PROP_SEEK_BAND_TOP,
784 rangehigh = si476x_to_v4l2(radio->core, rangehigh);
789 if (rangelow > rangehigh) {
794 if (si476x_radio_range_is_inside_of_the_band(rangelow, rangehigh,
796 func = SI476X_FUNC_FM_RECEIVER;
798 } else if (si476x_core_has_am(radio->core) &&
799 si476x_radio_range_is_inside_of_the_band(rangelow, rangehigh,
801 func = SI476X_FUNC_AM_RECEIVER;
807 err = si476x_radio_change_func(radio, func);
811 if (seek->rangehigh) {
812 err = regmap_write(radio->core->regmap,
813 SI476X_PROP_SEEK_BAND_TOP,
814 v4l2_to_si476x(radio->core,
819 if (seek->rangelow) {
820 err = regmap_write(radio->core->regmap,
821 SI476X_PROP_SEEK_BAND_BOTTOM,
822 v4l2_to_si476x(radio->core,
828 err = regmap_write(radio->core->regmap,
829 SI476X_PROP_SEEK_FREQUENCY_SPACING,
830 v4l2_to_si476x(radio->core,
836 err = radio->ops->seek_start(radio->core,
840 si476x_core_unlock(radio->core);
847 static int si476x_radio_g_volatile_ctrl(struct v4l2_ctrl *ctrl)
850 struct si476x_radio *radio = v4l2_ctrl_handler_to_radio(ctrl->handler);
852 si476x_core_lock(radio->core);
855 case V4L2_CID_SI476X_INTERCHIP_LINK:
856 if (si476x_core_has_diversity(radio->core)) {
857 if (radio->ops->phase_diversity) {
858 retval = radio->ops->phase_div_status(radio->core);
862 ctrl->val = !!SI476X_PHDIV_STATUS_LINK_LOCKED(retval);
876 si476x_core_unlock(radio->core);
881 static int si476x_radio_s_ctrl(struct v4l2_ctrl *ctrl)
884 enum si476x_phase_diversity_mode mode;
885 struct si476x_radio *radio = v4l2_ctrl_handler_to_radio(ctrl->handler);
887 si476x_core_lock(radio->core);
890 case V4L2_CID_SI476X_HARMONICS_COUNT:
891 retval = regmap_update_bits(radio->core->regmap,
892 SI476X_PROP_AUDIO_PWR_LINE_FILTER,
893 SI476X_PROP_PWR_HARMONICS_MASK,
896 case V4L2_CID_POWER_LINE_FREQUENCY:
898 case V4L2_CID_POWER_LINE_FREQUENCY_DISABLED:
899 retval = regmap_update_bits(radio->core->regmap,
900 SI476X_PROP_AUDIO_PWR_LINE_FILTER,
901 SI476X_PROP_PWR_ENABLE_MASK,
904 case V4L2_CID_POWER_LINE_FREQUENCY_50HZ:
905 retval = regmap_update_bits(radio->core->regmap,
906 SI476X_PROP_AUDIO_PWR_LINE_FILTER,
907 SI476X_PROP_PWR_GRID_MASK,
908 SI476X_PROP_PWR_GRID_50HZ);
910 case V4L2_CID_POWER_LINE_FREQUENCY_60HZ:
911 retval = regmap_update_bits(radio->core->regmap,
912 SI476X_PROP_AUDIO_PWR_LINE_FILTER,
913 SI476X_PROP_PWR_GRID_MASK,
914 SI476X_PROP_PWR_GRID_60HZ);
921 case V4L2_CID_SI476X_RSSI_THRESHOLD:
922 retval = regmap_write(radio->core->regmap,
923 SI476X_PROP_VALID_RSSI_THRESHOLD,
926 case V4L2_CID_SI476X_SNR_THRESHOLD:
927 retval = regmap_write(radio->core->regmap,
928 SI476X_PROP_VALID_SNR_THRESHOLD,
931 case V4L2_CID_SI476X_MAX_TUNE_ERROR:
932 retval = regmap_write(radio->core->regmap,
933 SI476X_PROP_VALID_MAX_TUNE_ERROR,
936 case V4L2_CID_RDS_RECEPTION:
938 * It looks like RDS related properties are
939 * inaccesable when tuner is in AM mode, so cache the
942 if (si476x_core_is_in_am_receiver_mode(radio->core))
943 regcache_cache_only(radio->core->regmap, true);
946 retval = regmap_write(radio->core->regmap,
947 SI476X_PROP_FM_RDS_INTERRUPT_FIFO_COUNT,
948 radio->core->rds_fifo_depth);
952 if (radio->core->client->irq) {
953 retval = regmap_write(radio->core->regmap,
954 SI476X_PROP_FM_RDS_INTERRUPT_SOURCE,
960 /* Drain RDS FIFO before enabling RDS processing */
961 retval = si476x_core_cmd_fm_rds_status(radio->core,
969 retval = regmap_update_bits(radio->core->regmap,
970 SI476X_PROP_FM_RDS_CONFIG,
971 SI476X_PROP_RDSEN_MASK,
974 retval = regmap_update_bits(radio->core->regmap,
975 SI476X_PROP_FM_RDS_CONFIG,
976 SI476X_PROP_RDSEN_MASK,
980 if (si476x_core_is_in_am_receiver_mode(radio->core))
981 regcache_cache_only(radio->core->regmap, false);
983 case V4L2_CID_TUNE_DEEMPHASIS:
984 retval = regmap_write(radio->core->regmap,
985 SI476X_PROP_AUDIO_DEEMPHASIS,
989 case V4L2_CID_SI476X_DIVERSITY_MODE:
990 mode = si476x_phase_diversity_idx_to_mode(ctrl->val);
992 if (mode == radio->core->diversity_mode) {
997 if (si476x_core_is_in_am_receiver_mode(radio->core)) {
999 * Diversity cannot be configured while tuner
1000 * is in AM mode so save the changes and carry on.
1002 radio->core->diversity_mode = mode;
1005 retval = radio->ops->phase_diversity(radio->core, mode);
1007 radio->core->diversity_mode = mode;
1016 si476x_core_unlock(radio->core);
1021 static int si476x_radio_g_chip_ident(struct file *file, void *fh,
1022 struct v4l2_dbg_chip_ident *chip)
1024 if (chip->match.type == V4L2_CHIP_MATCH_HOST &&
1025 v4l2_chip_match_host(&chip->match))
1031 #ifdef CONFIG_VIDEO_ADV_DEBUG
1032 static int si476x_radio_g_register(struct file *file, void *fh,
1033 struct v4l2_dbg_register *reg)
1037 struct si476x_radio *radio = video_drvdata(file);
1039 si476x_core_lock(radio->core);
1041 err = regmap_read(radio->core->regmap,
1042 (unsigned int)reg->reg, &value);
1044 si476x_core_unlock(radio->core);
1048 static int si476x_radio_s_register(struct file *file, void *fh,
1049 const struct v4l2_dbg_register *reg)
1053 struct si476x_radio *radio = video_drvdata(file);
1055 si476x_core_lock(radio->core);
1056 err = regmap_write(radio->core->regmap,
1057 (unsigned int)reg->reg,
1058 (unsigned int)reg->val);
1059 si476x_core_unlock(radio->core);
1065 static int si476x_radio_fops_open(struct file *file)
1067 struct si476x_radio *radio = video_drvdata(file);
1070 err = v4l2_fh_open(file);
1074 if (v4l2_fh_is_singular_file(file)) {
1075 si476x_core_lock(radio->core);
1076 err = si476x_core_set_power_state(radio->core,
1077 SI476X_POWER_UP_FULL);
1081 err = si476x_radio_do_post_powerup_init(radio,
1082 radio->core->power_up_parameters.func);
1086 err = si476x_radio_pretune(radio,
1087 radio->core->power_up_parameters.func);
1091 si476x_core_unlock(radio->core);
1092 /*Must be done after si476x_core_unlock to prevent a deadlock*/
1093 v4l2_ctrl_handler_setup(&radio->ctrl_handler);
1099 si476x_core_set_power_state(radio->core,
1102 si476x_core_unlock(radio->core);
1103 v4l2_fh_release(file);
1108 static int si476x_radio_fops_release(struct file *file)
1111 struct si476x_radio *radio = video_drvdata(file);
1113 if (v4l2_fh_is_singular_file(file) &&
1114 atomic_read(&radio->core->is_alive))
1115 si476x_core_set_power_state(radio->core,
1118 err = v4l2_fh_release(file);
1123 static ssize_t si476x_radio_fops_read(struct file *file, char __user *buf,
1124 size_t count, loff_t *ppos)
1128 unsigned int copied;
1130 struct si476x_radio *radio = video_drvdata(file);
1132 /* block if no new data available */
1133 if (kfifo_is_empty(&radio->core->rds_fifo)) {
1134 if (file->f_flags & O_NONBLOCK)
1135 return -EWOULDBLOCK;
1137 rval = wait_event_interruptible(radio->core->rds_read_queue,
1138 (!kfifo_is_empty(&radio->core->rds_fifo) ||
1139 !atomic_read(&radio->core->is_alive)));
1143 if (!atomic_read(&radio->core->is_alive))
1147 fifo_len = kfifo_len(&radio->core->rds_fifo);
1149 if (kfifo_to_user(&radio->core->rds_fifo, buf,
1150 min(fifo_len, count),
1152 dev_warn(&radio->videodev.dev,
1153 "Error during FIFO to userspace copy\n");
1156 rval = (ssize_t)copied;
1162 static unsigned int si476x_radio_fops_poll(struct file *file,
1163 struct poll_table_struct *pts)
1165 struct si476x_radio *radio = video_drvdata(file);
1166 unsigned long req_events = poll_requested_events(pts);
1167 unsigned int err = v4l2_ctrl_poll(file, pts);
1169 if (req_events & (POLLIN | POLLRDNORM)) {
1170 if (atomic_read(&radio->core->is_alive))
1171 poll_wait(file, &radio->core->rds_read_queue, pts);
1173 if (!atomic_read(&radio->core->is_alive))
1176 if (!kfifo_is_empty(&radio->core->rds_fifo))
1177 err = POLLIN | POLLRDNORM;
1183 static const struct v4l2_file_operations si476x_fops = {
1184 .owner = THIS_MODULE,
1185 .read = si476x_radio_fops_read,
1186 .poll = si476x_radio_fops_poll,
1187 .unlocked_ioctl = video_ioctl2,
1188 .open = si476x_radio_fops_open,
1189 .release = si476x_radio_fops_release,
1193 static const struct v4l2_ioctl_ops si4761_ioctl_ops = {
1194 .vidioc_querycap = si476x_radio_querycap,
1195 .vidioc_g_tuner = si476x_radio_g_tuner,
1196 .vidioc_s_tuner = si476x_radio_s_tuner,
1198 .vidioc_g_frequency = si476x_radio_g_frequency,
1199 .vidioc_s_frequency = si476x_radio_s_frequency,
1200 .vidioc_s_hw_freq_seek = si476x_radio_s_hw_freq_seek,
1201 .vidioc_enum_freq_bands = si476x_radio_enum_freq_bands,
1203 .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
1204 .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
1206 .vidioc_g_chip_ident = si476x_radio_g_chip_ident,
1207 #ifdef CONFIG_VIDEO_ADV_DEBUG
1208 .vidioc_g_register = si476x_radio_g_register,
1209 .vidioc_s_register = si476x_radio_s_register,
1214 static const struct video_device si476x_viddev_template = {
1215 .fops = &si476x_fops,
1216 .name = DRIVER_NAME,
1217 .release = video_device_release_empty,
1222 static ssize_t si476x_radio_read_acf_blob(struct file *file,
1223 char __user *user_buf,
1224 size_t count, loff_t *ppos)
1227 struct si476x_radio *radio = file->private_data;
1228 struct si476x_acf_status_report report;
1230 si476x_core_lock(radio->core);
1231 if (radio->ops->acf_status)
1232 err = radio->ops->acf_status(radio->core, &report);
1235 si476x_core_unlock(radio->core);
1240 return simple_read_from_buffer(user_buf, count, ppos, &report,
1244 static const struct file_operations radio_acf_fops = {
1245 .open = simple_open,
1246 .llseek = default_llseek,
1247 .read = si476x_radio_read_acf_blob,
1250 static ssize_t si476x_radio_read_rds_blckcnt_blob(struct file *file,
1251 char __user *user_buf,
1252 size_t count, loff_t *ppos)
1255 struct si476x_radio *radio = file->private_data;
1256 struct si476x_rds_blockcount_report report;
1258 si476x_core_lock(radio->core);
1259 if (radio->ops->rds_blckcnt)
1260 err = radio->ops->rds_blckcnt(radio->core, true,
1264 si476x_core_unlock(radio->core);
1269 return simple_read_from_buffer(user_buf, count, ppos, &report,
1273 static const struct file_operations radio_rds_blckcnt_fops = {
1274 .open = simple_open,
1275 .llseek = default_llseek,
1276 .read = si476x_radio_read_rds_blckcnt_blob,
1279 static ssize_t si476x_radio_read_agc_blob(struct file *file,
1280 char __user *user_buf,
1281 size_t count, loff_t *ppos)
1284 struct si476x_radio *radio = file->private_data;
1285 struct si476x_agc_status_report report;
1287 si476x_core_lock(radio->core);
1288 if (radio->ops->rds_blckcnt)
1289 err = radio->ops->agc_status(radio->core, &report);
1292 si476x_core_unlock(radio->core);
1297 return simple_read_from_buffer(user_buf, count, ppos, &report,
1301 static const struct file_operations radio_agc_fops = {
1302 .open = simple_open,
1303 .llseek = default_llseek,
1304 .read = si476x_radio_read_agc_blob,
1307 static ssize_t si476x_radio_read_rsq_blob(struct file *file,
1308 char __user *user_buf,
1309 size_t count, loff_t *ppos)
1312 struct si476x_radio *radio = file->private_data;
1313 struct si476x_rsq_status_report report;
1314 struct si476x_rsq_status_args args = {
1322 si476x_core_lock(radio->core);
1323 if (radio->ops->rds_blckcnt)
1324 err = radio->ops->rsq_status(radio->core, &args, &report);
1327 si476x_core_unlock(radio->core);
1332 return simple_read_from_buffer(user_buf, count, ppos, &report,
1336 static const struct file_operations radio_rsq_fops = {
1337 .open = simple_open,
1338 .llseek = default_llseek,
1339 .read = si476x_radio_read_rsq_blob,
1342 static ssize_t si476x_radio_read_rsq_primary_blob(struct file *file,
1343 char __user *user_buf,
1344 size_t count, loff_t *ppos)
1347 struct si476x_radio *radio = file->private_data;
1348 struct si476x_rsq_status_report report;
1349 struct si476x_rsq_status_args args = {
1357 si476x_core_lock(radio->core);
1358 if (radio->ops->rds_blckcnt)
1359 err = radio->ops->rsq_status(radio->core, &args, &report);
1362 si476x_core_unlock(radio->core);
1367 return simple_read_from_buffer(user_buf, count, ppos, &report,
1371 static const struct file_operations radio_rsq_primary_fops = {
1372 .open = simple_open,
1373 .llseek = default_llseek,
1374 .read = si476x_radio_read_rsq_primary_blob,
1378 static int si476x_radio_init_debugfs(struct si476x_radio *radio)
1380 struct dentry *dentry;
1383 dentry = debugfs_create_dir(dev_name(radio->v4l2dev.dev), NULL);
1384 if (IS_ERR(dentry)) {
1385 ret = PTR_ERR(dentry);
1388 radio->debugfs = dentry;
1390 dentry = debugfs_create_file("acf", S_IRUGO,
1391 radio->debugfs, radio, &radio_acf_fops);
1392 if (IS_ERR(dentry)) {
1393 ret = PTR_ERR(dentry);
1397 dentry = debugfs_create_file("rds_blckcnt", S_IRUGO,
1398 radio->debugfs, radio,
1399 &radio_rds_blckcnt_fops);
1400 if (IS_ERR(dentry)) {
1401 ret = PTR_ERR(dentry);
1405 dentry = debugfs_create_file("agc", S_IRUGO,
1406 radio->debugfs, radio, &radio_agc_fops);
1407 if (IS_ERR(dentry)) {
1408 ret = PTR_ERR(dentry);
1412 dentry = debugfs_create_file("rsq", S_IRUGO,
1413 radio->debugfs, radio, &radio_rsq_fops);
1414 if (IS_ERR(dentry)) {
1415 ret = PTR_ERR(dentry);
1419 dentry = debugfs_create_file("rsq_primary", S_IRUGO,
1420 radio->debugfs, radio,
1421 &radio_rsq_primary_fops);
1422 if (IS_ERR(dentry)) {
1423 ret = PTR_ERR(dentry);
1429 debugfs_remove_recursive(radio->debugfs);
1435 static int si476x_radio_add_new_custom(struct si476x_radio *radio,
1436 enum si476x_ctrl_idx idx)
1439 struct v4l2_ctrl *ctrl;
1441 ctrl = v4l2_ctrl_new_custom(&radio->ctrl_handler,
1444 rval = radio->ctrl_handler.error;
1445 if (ctrl == NULL && rval)
1446 dev_err(radio->v4l2dev.dev,
1447 "Could not initialize '%s' control %d\n",
1448 si476x_ctrls[idx].name, rval);
1453 static int si476x_radio_probe(struct platform_device *pdev)
1456 struct si476x_radio *radio;
1457 struct v4l2_ctrl *ctrl;
1459 static atomic_t instance = ATOMIC_INIT(0);
1461 radio = devm_kzalloc(&pdev->dev, sizeof(*radio), GFP_KERNEL);
1465 radio->core = i2c_mfd_cell_to_core(&pdev->dev);
1467 v4l2_device_set_name(&radio->v4l2dev, DRIVER_NAME, &instance);
1469 rval = v4l2_device_register(&pdev->dev, &radio->v4l2dev);
1471 dev_err(&pdev->dev, "Cannot register v4l2_device.\n");
1475 memcpy(&radio->videodev, &si476x_viddev_template,
1476 sizeof(struct video_device));
1478 radio->videodev.v4l2_dev = &radio->v4l2dev;
1479 radio->videodev.ioctl_ops = &si4761_ioctl_ops;
1481 video_set_drvdata(&radio->videodev, radio);
1482 platform_set_drvdata(pdev, radio);
1484 set_bit(V4L2_FL_USE_FH_PRIO, &radio->videodev.flags);
1486 radio->v4l2dev.ctrl_handler = &radio->ctrl_handler;
1487 v4l2_ctrl_handler_init(&radio->ctrl_handler,
1488 1 + ARRAY_SIZE(si476x_ctrls));
1490 if (si476x_core_has_am(radio->core)) {
1491 ctrl = v4l2_ctrl_new_std_menu(&radio->ctrl_handler,
1493 V4L2_CID_POWER_LINE_FREQUENCY,
1494 V4L2_CID_POWER_LINE_FREQUENCY_60HZ,
1496 rval = radio->ctrl_handler.error;
1497 if (ctrl == NULL && rval) {
1498 dev_err(&pdev->dev, "Could not initialize V4L2_CID_POWER_LINE_FREQUENCY control %d\n",
1503 rval = si476x_radio_add_new_custom(radio,
1504 SI476X_IDX_HARMONICS_COUNT);
1509 rval = si476x_radio_add_new_custom(radio, SI476X_IDX_RSSI_THRESHOLD);
1513 rval = si476x_radio_add_new_custom(radio, SI476X_IDX_SNR_THRESHOLD);
1517 rval = si476x_radio_add_new_custom(radio, SI476X_IDX_MAX_TUNE_ERROR);
1521 ctrl = v4l2_ctrl_new_std_menu(&radio->ctrl_handler,
1523 V4L2_CID_TUNE_DEEMPHASIS,
1524 V4L2_DEEMPHASIS_75_uS, 0, 0);
1525 rval = radio->ctrl_handler.error;
1526 if (ctrl == NULL && rval) {
1527 dev_err(&pdev->dev, "Could not initialize V4L2_CID_TUNE_DEEMPHASIS control %d\n",
1532 ctrl = v4l2_ctrl_new_std(&radio->ctrl_handler, &si476x_ctrl_ops,
1533 V4L2_CID_RDS_RECEPTION,
1535 rval = radio->ctrl_handler.error;
1536 if (ctrl == NULL && rval) {
1537 dev_err(&pdev->dev, "Could not initialize V4L2_CID_RDS_RECEPTION control %d\n",
1542 if (si476x_core_has_diversity(radio->core)) {
1543 si476x_ctrls[SI476X_IDX_DIVERSITY_MODE].def =
1544 si476x_phase_diversity_mode_to_idx(radio->core->diversity_mode);
1545 si476x_radio_add_new_custom(radio, SI476X_IDX_DIVERSITY_MODE);
1549 si476x_radio_add_new_custom(radio, SI476X_IDX_INTERCHIP_LINK);
1554 /* register video device */
1555 rval = video_register_device(&radio->videodev, VFL_TYPE_RADIO, -1);
1557 dev_err(&pdev->dev, "Could not register video device\n");
1561 rval = si476x_radio_init_debugfs(radio);
1563 dev_err(&pdev->dev, "Could not creat debugfs interface\n");
1569 v4l2_ctrl_handler_free(radio->videodev.ctrl_handler);
1573 static int si476x_radio_remove(struct platform_device *pdev)
1575 struct si476x_radio *radio = platform_get_drvdata(pdev);
1577 v4l2_ctrl_handler_free(radio->videodev.ctrl_handler);
1578 video_unregister_device(&radio->videodev);
1579 v4l2_device_unregister(&radio->v4l2dev);
1580 debugfs_remove_recursive(radio->debugfs);
1585 MODULE_ALIAS("platform:si476x-radio");
1587 static struct platform_driver si476x_radio_driver = {
1589 .name = DRIVER_NAME,
1590 .owner = THIS_MODULE,
1592 .probe = si476x_radio_probe,
1593 .remove = si476x_radio_remove,
1595 module_platform_driver(si476x_radio_driver);
1597 MODULE_AUTHOR("Andrey Smirnov <andrew.smirnov@gmail.com>");
1598 MODULE_DESCRIPTION("Driver for Si4761/64/68 AM/FM Radio MFD Cell");
1599 MODULE_LICENSE("GPL");