18e8e69d6SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
2b879a9c2SAndrey Smirnov /*
3b879a9c2SAndrey Smirnov  * drivers/media/radio/radio-si476x.c -- V4L2 driver for SI476X chips
4b879a9c2SAndrey Smirnov  *
5b879a9c2SAndrey Smirnov  * Copyright (C) 2012 Innovative Converged Devices(ICD)
6b879a9c2SAndrey Smirnov  * Copyright (C) 2013 Andrey Smirnov
7b879a9c2SAndrey Smirnov  *
8b879a9c2SAndrey Smirnov  * Author: Andrey Smirnov <andrew.smirnov@gmail.com>
9b879a9c2SAndrey Smirnov  */
10b879a9c2SAndrey Smirnov 
11b879a9c2SAndrey Smirnov #include <linux/module.h>
12b879a9c2SAndrey Smirnov #include <linux/delay.h>
13b879a9c2SAndrey Smirnov #include <linux/interrupt.h>
14b879a9c2SAndrey Smirnov #include <linux/slab.h>
15b879a9c2SAndrey Smirnov #include <linux/atomic.h>
16b879a9c2SAndrey Smirnov #include <linux/videodev2.h>
17b879a9c2SAndrey Smirnov #include <linux/mutex.h>
18b879a9c2SAndrey Smirnov #include <linux/debugfs.h>
19b879a9c2SAndrey Smirnov #include <media/v4l2-common.h>
20b879a9c2SAndrey Smirnov #include <media/v4l2-ioctl.h>
21b879a9c2SAndrey Smirnov #include <media/v4l2-ctrls.h>
22b879a9c2SAndrey Smirnov #include <media/v4l2-event.h>
23b879a9c2SAndrey Smirnov #include <media/v4l2-device.h>
24b879a9c2SAndrey Smirnov 
25d647f0b7SMauro Carvalho Chehab #include <media/drv-intf/si476x.h>
26b879a9c2SAndrey Smirnov #include <linux/mfd/si476x-core.h>
27b879a9c2SAndrey Smirnov 
28b879a9c2SAndrey Smirnov #define FM_FREQ_RANGE_LOW   64000000
29b879a9c2SAndrey Smirnov #define FM_FREQ_RANGE_HIGH 108000000
30b879a9c2SAndrey Smirnov 
31b879a9c2SAndrey Smirnov #define AM_FREQ_RANGE_LOW    520000
32b879a9c2SAndrey Smirnov #define AM_FREQ_RANGE_HIGH 30000000
33b879a9c2SAndrey Smirnov 
34b879a9c2SAndrey Smirnov #define PWRLINEFLTR (1 << 8)
35b879a9c2SAndrey Smirnov 
36b879a9c2SAndrey Smirnov #define FREQ_MUL (10000000 / 625)
37b879a9c2SAndrey Smirnov 
38ab671788SGeert Uytterhoeven #define SI476X_PHDIV_STATUS_LINK_LOCKED(status) (0x80 & (status))
39b879a9c2SAndrey Smirnov 
40b879a9c2SAndrey Smirnov #define DRIVER_NAME "si476x-radio"
41b879a9c2SAndrey Smirnov #define DRIVER_CARD "SI476x AM/FM Receiver"
42b879a9c2SAndrey Smirnov 
43b879a9c2SAndrey Smirnov enum si476x_freq_bands {
44b879a9c2SAndrey Smirnov 	SI476X_BAND_FM,
45b879a9c2SAndrey Smirnov 	SI476X_BAND_AM,
46b879a9c2SAndrey Smirnov };
47b879a9c2SAndrey Smirnov 
48b879a9c2SAndrey Smirnov static const struct v4l2_frequency_band si476x_bands[] = {
49b879a9c2SAndrey Smirnov 	[SI476X_BAND_FM] = {
50b879a9c2SAndrey Smirnov 		.type		= V4L2_TUNER_RADIO,
51b879a9c2SAndrey Smirnov 		.index		= SI476X_BAND_FM,
52b879a9c2SAndrey Smirnov 		.capability	= V4L2_TUNER_CAP_LOW
53b879a9c2SAndrey Smirnov 		| V4L2_TUNER_CAP_STEREO
54b879a9c2SAndrey Smirnov 		| V4L2_TUNER_CAP_RDS
55b879a9c2SAndrey Smirnov 		| V4L2_TUNER_CAP_RDS_BLOCK_IO
56b879a9c2SAndrey Smirnov 		| V4L2_TUNER_CAP_FREQ_BANDS,
57b879a9c2SAndrey Smirnov 		.rangelow	=  64 * FREQ_MUL,
58b879a9c2SAndrey Smirnov 		.rangehigh	= 108 * FREQ_MUL,
59b879a9c2SAndrey Smirnov 		.modulation	= V4L2_BAND_MODULATION_FM,
60b879a9c2SAndrey Smirnov 	},
61b879a9c2SAndrey Smirnov 	[SI476X_BAND_AM] = {
62b879a9c2SAndrey Smirnov 		.type		= V4L2_TUNER_RADIO,
63b879a9c2SAndrey Smirnov 		.index		= SI476X_BAND_AM,
64b879a9c2SAndrey Smirnov 		.capability	= V4L2_TUNER_CAP_LOW
65b879a9c2SAndrey Smirnov 		| V4L2_TUNER_CAP_FREQ_BANDS,
66b879a9c2SAndrey Smirnov 		.rangelow	= 0.52 * FREQ_MUL,
67b879a9c2SAndrey Smirnov 		.rangehigh	= 30 * FREQ_MUL,
68b879a9c2SAndrey Smirnov 		.modulation	= V4L2_BAND_MODULATION_AM,
69b879a9c2SAndrey Smirnov 	},
70b879a9c2SAndrey Smirnov };
71b879a9c2SAndrey Smirnov 
si476x_radio_freq_is_inside_of_the_band(u32 freq,int band)72b879a9c2SAndrey Smirnov static inline bool si476x_radio_freq_is_inside_of_the_band(u32 freq, int band)
73b879a9c2SAndrey Smirnov {
74b879a9c2SAndrey Smirnov 	return freq >= si476x_bands[band].rangelow &&
75b879a9c2SAndrey Smirnov 		freq <= si476x_bands[band].rangehigh;
76b879a9c2SAndrey Smirnov }
77b879a9c2SAndrey Smirnov 
si476x_radio_range_is_inside_of_the_band(u32 low,u32 high,int band)78b879a9c2SAndrey Smirnov static inline bool si476x_radio_range_is_inside_of_the_band(u32 low, u32 high,
79b879a9c2SAndrey Smirnov 							    int band)
80b879a9c2SAndrey Smirnov {
81b879a9c2SAndrey Smirnov 	return low  >= si476x_bands[band].rangelow &&
82b879a9c2SAndrey Smirnov 		high <= si476x_bands[band].rangehigh;
83b879a9c2SAndrey Smirnov }
84b879a9c2SAndrey Smirnov 
85b879a9c2SAndrey Smirnov static int si476x_radio_s_ctrl(struct v4l2_ctrl *ctrl);
86b879a9c2SAndrey Smirnov static int si476x_radio_g_volatile_ctrl(struct v4l2_ctrl *ctrl);
87b879a9c2SAndrey Smirnov 
88b879a9c2SAndrey Smirnov enum phase_diversity_modes_idx {
89b879a9c2SAndrey Smirnov 	SI476X_IDX_PHDIV_DISABLED,
90b879a9c2SAndrey Smirnov 	SI476X_IDX_PHDIV_PRIMARY_COMBINING,
91b879a9c2SAndrey Smirnov 	SI476X_IDX_PHDIV_PRIMARY_ANTENNA,
92b879a9c2SAndrey Smirnov 	SI476X_IDX_PHDIV_SECONDARY_ANTENNA,
93b879a9c2SAndrey Smirnov 	SI476X_IDX_PHDIV_SECONDARY_COMBINING,
94b879a9c2SAndrey Smirnov };
95b879a9c2SAndrey Smirnov 
96b879a9c2SAndrey Smirnov static const char * const phase_diversity_modes[] = {
97b879a9c2SAndrey Smirnov 	[SI476X_IDX_PHDIV_DISABLED]		= "Disabled",
98b879a9c2SAndrey Smirnov 	[SI476X_IDX_PHDIV_PRIMARY_COMBINING]	= "Primary with Secondary",
99b879a9c2SAndrey Smirnov 	[SI476X_IDX_PHDIV_PRIMARY_ANTENNA]	= "Primary Antenna",
100b879a9c2SAndrey Smirnov 	[SI476X_IDX_PHDIV_SECONDARY_ANTENNA]	= "Secondary Antenna",
101b879a9c2SAndrey Smirnov 	[SI476X_IDX_PHDIV_SECONDARY_COMBINING]	= "Secondary with Primary",
102b879a9c2SAndrey Smirnov };
103b879a9c2SAndrey Smirnov 
104b879a9c2SAndrey Smirnov static inline enum phase_diversity_modes_idx
si476x_phase_diversity_mode_to_idx(enum si476x_phase_diversity_mode mode)105b879a9c2SAndrey Smirnov si476x_phase_diversity_mode_to_idx(enum si476x_phase_diversity_mode mode)
106b879a9c2SAndrey Smirnov {
107b879a9c2SAndrey Smirnov 	switch (mode) {
108df561f66SGustavo A. R. Silva 	default:
109df561f66SGustavo A. R. Silva 		fallthrough;
110b879a9c2SAndrey Smirnov 	case SI476X_PHDIV_DISABLED:
111b879a9c2SAndrey Smirnov 		return SI476X_IDX_PHDIV_DISABLED;
112b879a9c2SAndrey Smirnov 	case SI476X_PHDIV_PRIMARY_COMBINING:
113b879a9c2SAndrey Smirnov 		return SI476X_IDX_PHDIV_PRIMARY_COMBINING;
114b879a9c2SAndrey Smirnov 	case SI476X_PHDIV_PRIMARY_ANTENNA:
115b879a9c2SAndrey Smirnov 		return SI476X_IDX_PHDIV_PRIMARY_ANTENNA;
116b879a9c2SAndrey Smirnov 	case SI476X_PHDIV_SECONDARY_ANTENNA:
117b879a9c2SAndrey Smirnov 		return SI476X_IDX_PHDIV_SECONDARY_ANTENNA;
118b879a9c2SAndrey Smirnov 	case SI476X_PHDIV_SECONDARY_COMBINING:
119b879a9c2SAndrey Smirnov 		return SI476X_IDX_PHDIV_SECONDARY_COMBINING;
120b879a9c2SAndrey Smirnov 	}
121b879a9c2SAndrey Smirnov }
122b879a9c2SAndrey Smirnov 
123b879a9c2SAndrey Smirnov static inline enum si476x_phase_diversity_mode
si476x_phase_diversity_idx_to_mode(enum phase_diversity_modes_idx idx)124b879a9c2SAndrey Smirnov si476x_phase_diversity_idx_to_mode(enum phase_diversity_modes_idx idx)
125b879a9c2SAndrey Smirnov {
126b879a9c2SAndrey Smirnov 	static const int idx_to_value[] = {
127b879a9c2SAndrey Smirnov 		[SI476X_IDX_PHDIV_DISABLED]		= SI476X_PHDIV_DISABLED,
128b879a9c2SAndrey Smirnov 		[SI476X_IDX_PHDIV_PRIMARY_COMBINING]	= SI476X_PHDIV_PRIMARY_COMBINING,
129b879a9c2SAndrey Smirnov 		[SI476X_IDX_PHDIV_PRIMARY_ANTENNA]	= SI476X_PHDIV_PRIMARY_ANTENNA,
130b879a9c2SAndrey Smirnov 		[SI476X_IDX_PHDIV_SECONDARY_ANTENNA]	= SI476X_PHDIV_SECONDARY_ANTENNA,
131b879a9c2SAndrey Smirnov 		[SI476X_IDX_PHDIV_SECONDARY_COMBINING]	= SI476X_PHDIV_SECONDARY_COMBINING,
132b879a9c2SAndrey Smirnov 	};
133b879a9c2SAndrey Smirnov 
134b879a9c2SAndrey Smirnov 	return idx_to_value[idx];
135b879a9c2SAndrey Smirnov }
136b879a9c2SAndrey Smirnov 
137b879a9c2SAndrey Smirnov static const struct v4l2_ctrl_ops si476x_ctrl_ops = {
138b879a9c2SAndrey Smirnov 	.g_volatile_ctrl	= si476x_radio_g_volatile_ctrl,
139b879a9c2SAndrey Smirnov 	.s_ctrl			= si476x_radio_s_ctrl,
140b879a9c2SAndrey Smirnov };
141b879a9c2SAndrey Smirnov 
142b879a9c2SAndrey Smirnov 
143b879a9c2SAndrey Smirnov enum si476x_ctrl_idx {
144b879a9c2SAndrey Smirnov 	SI476X_IDX_RSSI_THRESHOLD,
145b879a9c2SAndrey Smirnov 	SI476X_IDX_SNR_THRESHOLD,
146b879a9c2SAndrey Smirnov 	SI476X_IDX_MAX_TUNE_ERROR,
147b879a9c2SAndrey Smirnov 	SI476X_IDX_HARMONICS_COUNT,
148b879a9c2SAndrey Smirnov 	SI476X_IDX_DIVERSITY_MODE,
149b879a9c2SAndrey Smirnov 	SI476X_IDX_INTERCHIP_LINK,
150b879a9c2SAndrey Smirnov };
151b879a9c2SAndrey Smirnov static struct v4l2_ctrl_config si476x_ctrls[] = {
152b879a9c2SAndrey Smirnov 
153d156f293SMauro Carvalho Chehab 	/*
154b879a9c2SAndrey Smirnov 	 * SI476X during its station seeking(or tuning) process uses several
15530bcc510SXiaofeng Cao 	 * parameters to determine if "the station" is valid:
156b879a9c2SAndrey Smirnov 	 *
157b879a9c2SAndrey Smirnov 	 *	- Signal's SNR(in dBuV) must be lower than
158b879a9c2SAndrey Smirnov 	 *	#V4L2_CID_SI476X_SNR_THRESHOLD
159b879a9c2SAndrey Smirnov 	 *	- Signal's RSSI(in dBuV) must be greater than
160b879a9c2SAndrey Smirnov 	 *	#V4L2_CID_SI476X_RSSI_THRESHOLD
161b879a9c2SAndrey Smirnov 	 *	- Signal's frequency deviation(in units of 2ppm) must not be
162b879a9c2SAndrey Smirnov 	 *	more than #V4L2_CID_SI476X_MAX_TUNE_ERROR
163b879a9c2SAndrey Smirnov 	 */
164b879a9c2SAndrey Smirnov 	[SI476X_IDX_RSSI_THRESHOLD] = {
165b879a9c2SAndrey Smirnov 		.ops	= &si476x_ctrl_ops,
166b879a9c2SAndrey Smirnov 		.id	= V4L2_CID_SI476X_RSSI_THRESHOLD,
167b879a9c2SAndrey Smirnov 		.name	= "Valid RSSI Threshold",
168b879a9c2SAndrey Smirnov 		.type	= V4L2_CTRL_TYPE_INTEGER,
169b879a9c2SAndrey Smirnov 		.min	= -128,
170b879a9c2SAndrey Smirnov 		.max	= 127,
171b879a9c2SAndrey Smirnov 		.step	= 1,
172b879a9c2SAndrey Smirnov 	},
173b879a9c2SAndrey Smirnov 	[SI476X_IDX_SNR_THRESHOLD] = {
174b879a9c2SAndrey Smirnov 		.ops	= &si476x_ctrl_ops,
175b879a9c2SAndrey Smirnov 		.id	= V4L2_CID_SI476X_SNR_THRESHOLD,
176b879a9c2SAndrey Smirnov 		.type	= V4L2_CTRL_TYPE_INTEGER,
177b879a9c2SAndrey Smirnov 		.name	= "Valid SNR Threshold",
178b879a9c2SAndrey Smirnov 		.min	= -128,
179b879a9c2SAndrey Smirnov 		.max	= 127,
180b879a9c2SAndrey Smirnov 		.step	= 1,
181b879a9c2SAndrey Smirnov 	},
182b879a9c2SAndrey Smirnov 	[SI476X_IDX_MAX_TUNE_ERROR] = {
183b879a9c2SAndrey Smirnov 		.ops	= &si476x_ctrl_ops,
184b879a9c2SAndrey Smirnov 		.id	= V4L2_CID_SI476X_MAX_TUNE_ERROR,
185b879a9c2SAndrey Smirnov 		.type	= V4L2_CTRL_TYPE_INTEGER,
186b879a9c2SAndrey Smirnov 		.name	= "Max Tune Errors",
187b879a9c2SAndrey Smirnov 		.min	= 0,
188b879a9c2SAndrey Smirnov 		.max	= 126 * 2,
189b879a9c2SAndrey Smirnov 		.step	= 2,
190b879a9c2SAndrey Smirnov 	},
191b879a9c2SAndrey Smirnov 
192d156f293SMauro Carvalho Chehab 	/*
193b879a9c2SAndrey Smirnov 	 * #V4L2_CID_SI476X_HARMONICS_COUNT -- number of harmonics
194b879a9c2SAndrey Smirnov 	 * built-in power-line noise supression filter is to reject
195b879a9c2SAndrey Smirnov 	 * during AM-mode operation.
196b879a9c2SAndrey Smirnov 	 */
197b879a9c2SAndrey Smirnov 	[SI476X_IDX_HARMONICS_COUNT] = {
198b879a9c2SAndrey Smirnov 		.ops	= &si476x_ctrl_ops,
199b879a9c2SAndrey Smirnov 		.id	= V4L2_CID_SI476X_HARMONICS_COUNT,
200b879a9c2SAndrey Smirnov 		.type	= V4L2_CTRL_TYPE_INTEGER,
201b879a9c2SAndrey Smirnov 
202b879a9c2SAndrey Smirnov 		.name	= "Count of Harmonics to Reject",
203b879a9c2SAndrey Smirnov 		.min	= 0,
204b879a9c2SAndrey Smirnov 		.max	= 20,
205b879a9c2SAndrey Smirnov 		.step	= 1,
206b879a9c2SAndrey Smirnov 	},
207b879a9c2SAndrey Smirnov 
208d156f293SMauro Carvalho Chehab 	/*
209b879a9c2SAndrey Smirnov 	 * #V4L2_CID_SI476X_DIVERSITY_MODE -- configuration which
210b879a9c2SAndrey Smirnov 	 * two tuners working in diversity mode are to work in.
211b879a9c2SAndrey Smirnov 	 *
212b879a9c2SAndrey Smirnov 	 *  - #SI476X_IDX_PHDIV_DISABLED diversity mode disabled
213b879a9c2SAndrey Smirnov 	 *  - #SI476X_IDX_PHDIV_PRIMARY_COMBINING diversity mode is
214b879a9c2SAndrey Smirnov 	 *  on, primary tuner's antenna is the main one.
215b879a9c2SAndrey Smirnov 	 *  - #SI476X_IDX_PHDIV_PRIMARY_ANTENNA diversity mode is
216b879a9c2SAndrey Smirnov 	 *  off, primary tuner's antenna is the main one.
217b879a9c2SAndrey Smirnov 	 *  - #SI476X_IDX_PHDIV_SECONDARY_ANTENNA diversity mode is
218b879a9c2SAndrey Smirnov 	 *  off, secondary tuner's antenna is the main one.
219b879a9c2SAndrey Smirnov 	 *  - #SI476X_IDX_PHDIV_SECONDARY_COMBINING diversity mode is
220b879a9c2SAndrey Smirnov 	 *  on, secondary tuner's antenna is the main one.
221b879a9c2SAndrey Smirnov 	 */
222b879a9c2SAndrey Smirnov 	[SI476X_IDX_DIVERSITY_MODE] = {
223b879a9c2SAndrey Smirnov 		.ops	= &si476x_ctrl_ops,
224b879a9c2SAndrey Smirnov 		.id	= V4L2_CID_SI476X_DIVERSITY_MODE,
225b879a9c2SAndrey Smirnov 		.type	= V4L2_CTRL_TYPE_MENU,
226b879a9c2SAndrey Smirnov 		.name	= "Phase Diversity Mode",
227b879a9c2SAndrey Smirnov 		.qmenu	= phase_diversity_modes,
228b879a9c2SAndrey Smirnov 		.min	= 0,
229b879a9c2SAndrey Smirnov 		.max	= ARRAY_SIZE(phase_diversity_modes) - 1,
230b879a9c2SAndrey Smirnov 	},
231b879a9c2SAndrey Smirnov 
232d156f293SMauro Carvalho Chehab 	/*
233b879a9c2SAndrey Smirnov 	 * #V4L2_CID_SI476X_INTERCHIP_LINK -- inter-chip link in
234b879a9c2SAndrey Smirnov 	 * diversity mode indicator. Allows user to determine if two
235b879a9c2SAndrey Smirnov 	 * chips working in diversity mode have established a link
236b879a9c2SAndrey Smirnov 	 * between each other and if the system as a whole uses
237b879a9c2SAndrey Smirnov 	 * signals from both antennas to receive FM radio.
238b879a9c2SAndrey Smirnov 	 */
239b879a9c2SAndrey Smirnov 	[SI476X_IDX_INTERCHIP_LINK] = {
240b879a9c2SAndrey Smirnov 		.ops	= &si476x_ctrl_ops,
241b879a9c2SAndrey Smirnov 		.id	= V4L2_CID_SI476X_INTERCHIP_LINK,
242b879a9c2SAndrey Smirnov 		.type	= V4L2_CTRL_TYPE_BOOLEAN,
243b879a9c2SAndrey Smirnov 		.flags  = V4L2_CTRL_FLAG_READ_ONLY | V4L2_CTRL_FLAG_VOLATILE,
244b879a9c2SAndrey Smirnov 		.name	= "Inter-Chip Link",
245b879a9c2SAndrey Smirnov 		.min	= 0,
246b879a9c2SAndrey Smirnov 		.max	= 1,
247b879a9c2SAndrey Smirnov 		.step	= 1,
248b879a9c2SAndrey Smirnov 	},
249b879a9c2SAndrey Smirnov };
250b879a9c2SAndrey Smirnov 
251b879a9c2SAndrey Smirnov struct si476x_radio;
252b879a9c2SAndrey Smirnov 
253b879a9c2SAndrey Smirnov /**
254b879a9c2SAndrey Smirnov  * struct si476x_radio_ops - vtable of tuner functions
255b879a9c2SAndrey Smirnov  *
256b879a9c2SAndrey Smirnov  * This table holds pointers to functions implementing particular
257b879a9c2SAndrey Smirnov  * operations depending on the mode in which the tuner chip was
25830bcc510SXiaofeng Cao  * configured to start. If the function is not supported
259b879a9c2SAndrey Smirnov  * corresponding element is set to #NULL.
260b879a9c2SAndrey Smirnov  *
261b879a9c2SAndrey Smirnov  * @tune_freq: Tune chip to a specific frequency
262b879a9c2SAndrey Smirnov  * @seek_start: Star station seeking
26339c1cb2bSJonathan McCrohan  * @rsq_status: Get Received Signal Quality(RSQ) status
26439c1cb2bSJonathan McCrohan  * @rds_blckcnt: Get received RDS blocks count
265b879a9c2SAndrey Smirnov  * @phase_diversity: Change phase diversity mode of the tuner
266b879a9c2SAndrey Smirnov  * @phase_div_status: Get phase diversity mode status
267b879a9c2SAndrey Smirnov  * @acf_status: Get the status of Automatically Controlled
268b879a9c2SAndrey Smirnov  * Features(ACF)
269b879a9c2SAndrey Smirnov  * @agc_status: Get Automatic Gain Control(AGC) status
270b879a9c2SAndrey Smirnov  */
271b879a9c2SAndrey Smirnov struct si476x_radio_ops {
272b879a9c2SAndrey Smirnov 	int (*tune_freq)(struct si476x_core *, struct si476x_tune_freq_args *);
273b879a9c2SAndrey Smirnov 	int (*seek_start)(struct si476x_core *, bool, bool);
274b879a9c2SAndrey Smirnov 	int (*rsq_status)(struct si476x_core *, struct si476x_rsq_status_args *,
275b879a9c2SAndrey Smirnov 			  struct si476x_rsq_status_report *);
276b879a9c2SAndrey Smirnov 	int (*rds_blckcnt)(struct si476x_core *, bool,
277b879a9c2SAndrey Smirnov 			   struct si476x_rds_blockcount_report *);
278b879a9c2SAndrey Smirnov 
279b879a9c2SAndrey Smirnov 	int (*phase_diversity)(struct si476x_core *,
280b879a9c2SAndrey Smirnov 			       enum si476x_phase_diversity_mode);
281b879a9c2SAndrey Smirnov 	int (*phase_div_status)(struct si476x_core *);
282b879a9c2SAndrey Smirnov 	int (*acf_status)(struct si476x_core *,
283b879a9c2SAndrey Smirnov 			  struct si476x_acf_status_report *);
284b879a9c2SAndrey Smirnov 	int (*agc_status)(struct si476x_core *,
285b879a9c2SAndrey Smirnov 			  struct si476x_agc_status_report *);
286b879a9c2SAndrey Smirnov };
287b879a9c2SAndrey Smirnov 
288b879a9c2SAndrey Smirnov /**
289b879a9c2SAndrey Smirnov  * struct si476x_radio - radio device
290b879a9c2SAndrey Smirnov  *
291d156f293SMauro Carvalho Chehab  * @v4l2dev: Pointer to V4L2 device created by V4L2 subsystem
292b879a9c2SAndrey Smirnov  * @videodev: Pointer to video device created by V4L2 subsystem
293d156f293SMauro Carvalho Chehab  * @ctrl_handler: V4L2 controls handler
294d156f293SMauro Carvalho Chehab  * @core: Pointer to underlying core device
295b879a9c2SAndrey Smirnov  * @ops: Vtable of functions. See struct si476x_radio_ops for details
296d156f293SMauro Carvalho Chehab  * @debugfs: pointer to &strucd dentry for debugfs
297d156f293SMauro Carvalho Chehab  * @audmode: audio mode, as defined for the rxsubchans field
298d156f293SMauro Carvalho Chehab  *	     at videodev2.h
299d156f293SMauro Carvalho Chehab  *
300b879a9c2SAndrey Smirnov  * core structure is the radio device is being used
301b879a9c2SAndrey Smirnov  */
302b879a9c2SAndrey Smirnov struct si476x_radio {
303b879a9c2SAndrey Smirnov 	struct v4l2_device v4l2dev;
304b879a9c2SAndrey Smirnov 	struct video_device videodev;
305b879a9c2SAndrey Smirnov 	struct v4l2_ctrl_handler ctrl_handler;
306b879a9c2SAndrey Smirnov 
307b879a9c2SAndrey Smirnov 	struct si476x_core  *core;
308b879a9c2SAndrey Smirnov 	/* This field should not be accesses unless core lock is held */
309b879a9c2SAndrey Smirnov 	const struct si476x_radio_ops *ops;
310b879a9c2SAndrey Smirnov 
311b879a9c2SAndrey Smirnov 	struct dentry	*debugfs;
312b879a9c2SAndrey Smirnov 	u32 audmode;
313b879a9c2SAndrey Smirnov };
314b879a9c2SAndrey Smirnov 
315b879a9c2SAndrey Smirnov static inline struct si476x_radio *
v4l2_ctrl_handler_to_radio(struct v4l2_ctrl_handler * d)316b879a9c2SAndrey Smirnov v4l2_ctrl_handler_to_radio(struct v4l2_ctrl_handler *d)
317b879a9c2SAndrey Smirnov {
318b879a9c2SAndrey Smirnov 	return container_of(d, struct si476x_radio, ctrl_handler);
319b879a9c2SAndrey Smirnov }
320b879a9c2SAndrey Smirnov 
321b879a9c2SAndrey Smirnov /*
322b879a9c2SAndrey Smirnov  * si476x_vidioc_querycap - query device capabilities
323b879a9c2SAndrey Smirnov  */
si476x_radio_querycap(struct file * file,void * priv,struct v4l2_capability * capability)324b879a9c2SAndrey Smirnov static int si476x_radio_querycap(struct file *file, void *priv,
325b879a9c2SAndrey Smirnov 				 struct v4l2_capability *capability)
326b879a9c2SAndrey Smirnov {
327b879a9c2SAndrey Smirnov 	struct si476x_radio *radio = video_drvdata(file);
328b879a9c2SAndrey Smirnov 
329c0decac1SMauro Carvalho Chehab 	strscpy(capability->driver, radio->v4l2dev.name,
330b879a9c2SAndrey Smirnov 		sizeof(capability->driver));
331c0decac1SMauro Carvalho Chehab 	strscpy(capability->card,   DRIVER_CARD, sizeof(capability->card));
332b879a9c2SAndrey Smirnov 	snprintf(capability->bus_info, sizeof(capability->bus_info),
333b879a9c2SAndrey Smirnov 		 "platform:%s", radio->v4l2dev.name);
334b879a9c2SAndrey Smirnov 	return 0;
335b879a9c2SAndrey Smirnov }
336b879a9c2SAndrey Smirnov 
si476x_radio_enum_freq_bands(struct file * file,void * priv,struct v4l2_frequency_band * band)337b879a9c2SAndrey Smirnov static int si476x_radio_enum_freq_bands(struct file *file, void *priv,
338b879a9c2SAndrey Smirnov 					struct v4l2_frequency_band *band)
339b879a9c2SAndrey Smirnov {
340b879a9c2SAndrey Smirnov 	int err;
341b879a9c2SAndrey Smirnov 	struct si476x_radio *radio = video_drvdata(file);
342b879a9c2SAndrey Smirnov 
343b879a9c2SAndrey Smirnov 	if (band->tuner != 0)
344b879a9c2SAndrey Smirnov 		return -EINVAL;
345b879a9c2SAndrey Smirnov 
346b879a9c2SAndrey Smirnov 	switch (radio->core->chip_id) {
347b879a9c2SAndrey Smirnov 		/* AM/FM tuners -- all bands are supported */
348b879a9c2SAndrey Smirnov 	case SI476X_CHIP_SI4761:
349b879a9c2SAndrey Smirnov 	case SI476X_CHIP_SI4764:
350b879a9c2SAndrey Smirnov 		if (band->index < ARRAY_SIZE(si476x_bands)) {
351b879a9c2SAndrey Smirnov 			*band = si476x_bands[band->index];
352b879a9c2SAndrey Smirnov 			err = 0;
353b879a9c2SAndrey Smirnov 		} else {
354b879a9c2SAndrey Smirnov 			err = -EINVAL;
355b879a9c2SAndrey Smirnov 		}
356b879a9c2SAndrey Smirnov 		break;
357b879a9c2SAndrey Smirnov 		/* FM companion tuner chips -- only FM bands are
358b879a9c2SAndrey Smirnov 		 * supported */
359b879a9c2SAndrey Smirnov 	case SI476X_CHIP_SI4768:
360b879a9c2SAndrey Smirnov 		if (band->index == SI476X_BAND_FM) {
361b879a9c2SAndrey Smirnov 			*band = si476x_bands[band->index];
362b879a9c2SAndrey Smirnov 			err = 0;
363b879a9c2SAndrey Smirnov 		} else {
364b879a9c2SAndrey Smirnov 			err = -EINVAL;
365b879a9c2SAndrey Smirnov 		}
366b879a9c2SAndrey Smirnov 		break;
367b879a9c2SAndrey Smirnov 	default:
368b879a9c2SAndrey Smirnov 		err = -EINVAL;
369b879a9c2SAndrey Smirnov 	}
370b879a9c2SAndrey Smirnov 
371b879a9c2SAndrey Smirnov 	return err;
372b879a9c2SAndrey Smirnov }
373b879a9c2SAndrey Smirnov 
si476x_radio_g_tuner(struct file * file,void * priv,struct v4l2_tuner * tuner)374b879a9c2SAndrey Smirnov static int si476x_radio_g_tuner(struct file *file, void *priv,
375b879a9c2SAndrey Smirnov 				struct v4l2_tuner *tuner)
376b879a9c2SAndrey Smirnov {
377b879a9c2SAndrey Smirnov 	int err;
378b879a9c2SAndrey Smirnov 	struct si476x_rsq_status_report report;
379b879a9c2SAndrey Smirnov 	struct si476x_radio *radio = video_drvdata(file);
380b879a9c2SAndrey Smirnov 
381b879a9c2SAndrey Smirnov 	struct si476x_rsq_status_args args = {
382b879a9c2SAndrey Smirnov 		.primary	= false,
383b879a9c2SAndrey Smirnov 		.rsqack		= false,
384b879a9c2SAndrey Smirnov 		.attune		= false,
385b879a9c2SAndrey Smirnov 		.cancel		= false,
386b879a9c2SAndrey Smirnov 		.stcack		= false,
387b879a9c2SAndrey Smirnov 	};
388b879a9c2SAndrey Smirnov 
389b879a9c2SAndrey Smirnov 	if (tuner->index != 0)
390b879a9c2SAndrey Smirnov 		return -EINVAL;
391b879a9c2SAndrey Smirnov 
392b879a9c2SAndrey Smirnov 	tuner->type       = V4L2_TUNER_RADIO;
393b879a9c2SAndrey Smirnov 	tuner->capability = V4L2_TUNER_CAP_LOW /* Measure frequencies
394b879a9c2SAndrey Smirnov 						 * in multiples of
395b879a9c2SAndrey Smirnov 						 * 62.5 Hz */
396b879a9c2SAndrey Smirnov 		| V4L2_TUNER_CAP_STEREO
397b879a9c2SAndrey Smirnov 		| V4L2_TUNER_CAP_HWSEEK_BOUNDED
398b879a9c2SAndrey Smirnov 		| V4L2_TUNER_CAP_HWSEEK_WRAP
399b879a9c2SAndrey Smirnov 		| V4L2_TUNER_CAP_HWSEEK_PROG_LIM;
400b879a9c2SAndrey Smirnov 
401b879a9c2SAndrey Smirnov 	si476x_core_lock(radio->core);
402b879a9c2SAndrey Smirnov 
403b879a9c2SAndrey Smirnov 	if (si476x_core_is_a_secondary_tuner(radio->core)) {
404c0decac1SMauro Carvalho Chehab 		strscpy(tuner->name, "FM (secondary)", sizeof(tuner->name));
405b879a9c2SAndrey Smirnov 		tuner->rxsubchans = 0;
406b879a9c2SAndrey Smirnov 		tuner->rangelow = si476x_bands[SI476X_BAND_FM].rangelow;
407b879a9c2SAndrey Smirnov 	} else if (si476x_core_has_am(radio->core)) {
408b879a9c2SAndrey Smirnov 		if (si476x_core_is_a_primary_tuner(radio->core))
409c0decac1SMauro Carvalho Chehab 			strscpy(tuner->name, "AM/FM (primary)",
410b879a9c2SAndrey Smirnov 				sizeof(tuner->name));
411b879a9c2SAndrey Smirnov 		else
412c0decac1SMauro Carvalho Chehab 			strscpy(tuner->name, "AM/FM", sizeof(tuner->name));
413b879a9c2SAndrey Smirnov 
414b879a9c2SAndrey Smirnov 		tuner->rxsubchans = V4L2_TUNER_SUB_MONO | V4L2_TUNER_SUB_STEREO
415b879a9c2SAndrey Smirnov 			| V4L2_TUNER_SUB_RDS;
416b879a9c2SAndrey Smirnov 		tuner->capability |= V4L2_TUNER_CAP_RDS
417b879a9c2SAndrey Smirnov 			| V4L2_TUNER_CAP_RDS_BLOCK_IO
418b879a9c2SAndrey Smirnov 			| V4L2_TUNER_CAP_FREQ_BANDS;
419b879a9c2SAndrey Smirnov 
420b879a9c2SAndrey Smirnov 		tuner->rangelow = si476x_bands[SI476X_BAND_AM].rangelow;
421b879a9c2SAndrey Smirnov 	} else {
422c0decac1SMauro Carvalho Chehab 		strscpy(tuner->name, "FM", sizeof(tuner->name));
423b879a9c2SAndrey Smirnov 		tuner->rxsubchans = V4L2_TUNER_SUB_RDS;
424b879a9c2SAndrey Smirnov 		tuner->capability |= V4L2_TUNER_CAP_RDS
425b879a9c2SAndrey Smirnov 			| V4L2_TUNER_CAP_RDS_BLOCK_IO
426b879a9c2SAndrey Smirnov 			| V4L2_TUNER_CAP_FREQ_BANDS;
427b879a9c2SAndrey Smirnov 		tuner->rangelow = si476x_bands[SI476X_BAND_FM].rangelow;
428b879a9c2SAndrey Smirnov 	}
429b879a9c2SAndrey Smirnov 
430b879a9c2SAndrey Smirnov 	tuner->audmode = radio->audmode;
431b879a9c2SAndrey Smirnov 
432b879a9c2SAndrey Smirnov 	tuner->afc = 1;
433b879a9c2SAndrey Smirnov 	tuner->rangehigh = si476x_bands[SI476X_BAND_FM].rangehigh;
434b879a9c2SAndrey Smirnov 
435b879a9c2SAndrey Smirnov 	err = radio->ops->rsq_status(radio->core,
436b879a9c2SAndrey Smirnov 				     &args, &report);
437b879a9c2SAndrey Smirnov 	if (err < 0) {
438b879a9c2SAndrey Smirnov 		tuner->signal = 0;
439b879a9c2SAndrey Smirnov 	} else {
440b879a9c2SAndrey Smirnov 		/*
441b879a9c2SAndrey Smirnov 		 * tuner->signal value range: 0x0000 .. 0xFFFF,
442b879a9c2SAndrey Smirnov 		 * report.rssi: -128 .. 127
443b879a9c2SAndrey Smirnov 		 */
444b879a9c2SAndrey Smirnov 		tuner->signal = (report.rssi + 128) * 257;
445b879a9c2SAndrey Smirnov 	}
446b879a9c2SAndrey Smirnov 	si476x_core_unlock(radio->core);
447b879a9c2SAndrey Smirnov 
448b879a9c2SAndrey Smirnov 	return err;
449b879a9c2SAndrey Smirnov }
450b879a9c2SAndrey Smirnov 
si476x_radio_s_tuner(struct file * file,void * priv,const struct v4l2_tuner * tuner)451b879a9c2SAndrey Smirnov static int si476x_radio_s_tuner(struct file *file, void *priv,
452b879a9c2SAndrey Smirnov 				const struct v4l2_tuner *tuner)
453b879a9c2SAndrey Smirnov {
454b879a9c2SAndrey Smirnov 	struct si476x_radio *radio = video_drvdata(file);
455b879a9c2SAndrey Smirnov 
456b879a9c2SAndrey Smirnov 	if (tuner->index != 0)
457b879a9c2SAndrey Smirnov 		return -EINVAL;
458b879a9c2SAndrey Smirnov 
459b879a9c2SAndrey Smirnov 	if (tuner->audmode == V4L2_TUNER_MODE_MONO ||
460b879a9c2SAndrey Smirnov 	    tuner->audmode == V4L2_TUNER_MODE_STEREO)
461b879a9c2SAndrey Smirnov 		radio->audmode = tuner->audmode;
462b879a9c2SAndrey Smirnov 	else
463b879a9c2SAndrey Smirnov 		radio->audmode = V4L2_TUNER_MODE_STEREO;
464b879a9c2SAndrey Smirnov 
465b879a9c2SAndrey Smirnov 	return 0;
466b879a9c2SAndrey Smirnov }
467b879a9c2SAndrey Smirnov 
si476x_radio_init_vtable(struct si476x_radio * radio,enum si476x_func func)468b879a9c2SAndrey Smirnov static int si476x_radio_init_vtable(struct si476x_radio *radio,
469b879a9c2SAndrey Smirnov 				    enum si476x_func func)
470b879a9c2SAndrey Smirnov {
471b879a9c2SAndrey Smirnov 	static const struct si476x_radio_ops fm_ops = {
472b879a9c2SAndrey Smirnov 		.tune_freq		= si476x_core_cmd_fm_tune_freq,
473b879a9c2SAndrey Smirnov 		.seek_start		= si476x_core_cmd_fm_seek_start,
474b879a9c2SAndrey Smirnov 		.rsq_status		= si476x_core_cmd_fm_rsq_status,
475b879a9c2SAndrey Smirnov 		.rds_blckcnt		= si476x_core_cmd_fm_rds_blockcount,
476b879a9c2SAndrey Smirnov 		.phase_diversity	= si476x_core_cmd_fm_phase_diversity,
477b879a9c2SAndrey Smirnov 		.phase_div_status	= si476x_core_cmd_fm_phase_div_status,
478b879a9c2SAndrey Smirnov 		.acf_status		= si476x_core_cmd_fm_acf_status,
479b879a9c2SAndrey Smirnov 		.agc_status		= si476x_core_cmd_agc_status,
480b879a9c2SAndrey Smirnov 	};
481b879a9c2SAndrey Smirnov 
482b879a9c2SAndrey Smirnov 	static const struct si476x_radio_ops am_ops = {
483b879a9c2SAndrey Smirnov 		.tune_freq		= si476x_core_cmd_am_tune_freq,
484b879a9c2SAndrey Smirnov 		.seek_start		= si476x_core_cmd_am_seek_start,
485b879a9c2SAndrey Smirnov 		.rsq_status		= si476x_core_cmd_am_rsq_status,
486b879a9c2SAndrey Smirnov 		.rds_blckcnt		= NULL,
487b879a9c2SAndrey Smirnov 		.phase_diversity	= NULL,
488b879a9c2SAndrey Smirnov 		.phase_div_status	= NULL,
489b879a9c2SAndrey Smirnov 		.acf_status		= si476x_core_cmd_am_acf_status,
490b879a9c2SAndrey Smirnov 		.agc_status		= NULL,
491b879a9c2SAndrey Smirnov 	};
492b879a9c2SAndrey Smirnov 
493b879a9c2SAndrey Smirnov 	switch (func) {
494b879a9c2SAndrey Smirnov 	case SI476X_FUNC_FM_RECEIVER:
495b879a9c2SAndrey Smirnov 		radio->ops = &fm_ops;
496b879a9c2SAndrey Smirnov 		return 0;
497b879a9c2SAndrey Smirnov 
498b879a9c2SAndrey Smirnov 	case SI476X_FUNC_AM_RECEIVER:
499b879a9c2SAndrey Smirnov 		radio->ops = &am_ops;
500b879a9c2SAndrey Smirnov 		return 0;
501b879a9c2SAndrey Smirnov 	default:
502b879a9c2SAndrey Smirnov 		WARN(1, "Unexpected tuner function value\n");
503b879a9c2SAndrey Smirnov 		return -EINVAL;
504b879a9c2SAndrey Smirnov 	}
505b879a9c2SAndrey Smirnov }
506b879a9c2SAndrey Smirnov 
si476x_radio_pretune(struct si476x_radio * radio,enum si476x_func func)507b879a9c2SAndrey Smirnov static int si476x_radio_pretune(struct si476x_radio *radio,
508b879a9c2SAndrey Smirnov 				enum si476x_func func)
509b879a9c2SAndrey Smirnov {
510b879a9c2SAndrey Smirnov 	int retval;
511b879a9c2SAndrey Smirnov 
512b879a9c2SAndrey Smirnov 	struct si476x_tune_freq_args args = {
513b879a9c2SAndrey Smirnov 		.zifsr		= false,
514b879a9c2SAndrey Smirnov 		.hd		= false,
515b879a9c2SAndrey Smirnov 		.injside	= SI476X_INJSIDE_AUTO,
516b879a9c2SAndrey Smirnov 		.tunemode	= SI476X_TM_VALIDATED_NORMAL_TUNE,
517b879a9c2SAndrey Smirnov 		.smoothmetrics	= SI476X_SM_INITIALIZE_AUDIO,
518b879a9c2SAndrey Smirnov 		.antcap		= 0,
519b879a9c2SAndrey Smirnov 	};
520b879a9c2SAndrey Smirnov 
521b879a9c2SAndrey Smirnov 	switch (func) {
522b879a9c2SAndrey Smirnov 	case SI476X_FUNC_FM_RECEIVER:
523b879a9c2SAndrey Smirnov 		args.freq = v4l2_to_si476x(radio->core,
524b879a9c2SAndrey Smirnov 					   92 * FREQ_MUL);
525b879a9c2SAndrey Smirnov 		retval = radio->ops->tune_freq(radio->core, &args);
526b879a9c2SAndrey Smirnov 		break;
527b879a9c2SAndrey Smirnov 	case SI476X_FUNC_AM_RECEIVER:
528b879a9c2SAndrey Smirnov 		args.freq = v4l2_to_si476x(radio->core,
529b879a9c2SAndrey Smirnov 					   0.6 * FREQ_MUL);
530b879a9c2SAndrey Smirnov 		retval = radio->ops->tune_freq(radio->core, &args);
531b879a9c2SAndrey Smirnov 		break;
532b879a9c2SAndrey Smirnov 	default:
533b879a9c2SAndrey Smirnov 		WARN(1, "Unexpected tuner function value\n");
534b879a9c2SAndrey Smirnov 		retval = -EINVAL;
535b879a9c2SAndrey Smirnov 	}
536b879a9c2SAndrey Smirnov 
537b879a9c2SAndrey Smirnov 	return retval;
538b879a9c2SAndrey Smirnov }
si476x_radio_do_post_powerup_init(struct si476x_radio * radio,enum si476x_func func)539b879a9c2SAndrey Smirnov static int si476x_radio_do_post_powerup_init(struct si476x_radio *radio,
540b879a9c2SAndrey Smirnov 					     enum si476x_func func)
541b879a9c2SAndrey Smirnov {
542b879a9c2SAndrey Smirnov 	int err;
543b879a9c2SAndrey Smirnov 
544b879a9c2SAndrey Smirnov 	/* regcache_mark_dirty(radio->core->regmap); */
545b879a9c2SAndrey Smirnov 	err = regcache_sync_region(radio->core->regmap,
546b879a9c2SAndrey Smirnov 				   SI476X_PROP_DIGITAL_IO_INPUT_SAMPLE_RATE,
547b879a9c2SAndrey Smirnov 				   SI476X_PROP_DIGITAL_IO_OUTPUT_FORMAT);
548b879a9c2SAndrey Smirnov 	if (err < 0)
549b879a9c2SAndrey Smirnov 		return err;
550b879a9c2SAndrey Smirnov 
551b879a9c2SAndrey Smirnov 	err = regcache_sync_region(radio->core->regmap,
552b879a9c2SAndrey Smirnov 				   SI476X_PROP_AUDIO_DEEMPHASIS,
553b879a9c2SAndrey Smirnov 				   SI476X_PROP_AUDIO_PWR_LINE_FILTER);
554b879a9c2SAndrey Smirnov 	if (err < 0)
555b879a9c2SAndrey Smirnov 		return err;
556b879a9c2SAndrey Smirnov 
557b879a9c2SAndrey Smirnov 	err = regcache_sync_region(radio->core->regmap,
558b879a9c2SAndrey Smirnov 				   SI476X_PROP_INT_CTL_ENABLE,
559b879a9c2SAndrey Smirnov 				   SI476X_PROP_INT_CTL_ENABLE);
560b879a9c2SAndrey Smirnov 	if (err < 0)
561b879a9c2SAndrey Smirnov 		return err;
562b879a9c2SAndrey Smirnov 
563b879a9c2SAndrey Smirnov 	/*
564b879a9c2SAndrey Smirnov 	 * Is there any point in restoring SNR and the like
565b879a9c2SAndrey Smirnov 	 * when switching between AM/FM?
566b879a9c2SAndrey Smirnov 	 */
567b879a9c2SAndrey Smirnov 	err = regcache_sync_region(radio->core->regmap,
568b879a9c2SAndrey Smirnov 				   SI476X_PROP_VALID_MAX_TUNE_ERROR,
569b879a9c2SAndrey Smirnov 				   SI476X_PROP_VALID_MAX_TUNE_ERROR);
570b879a9c2SAndrey Smirnov 	if (err < 0)
571b879a9c2SAndrey Smirnov 		return err;
572b879a9c2SAndrey Smirnov 
573b879a9c2SAndrey Smirnov 	err = regcache_sync_region(radio->core->regmap,
574b879a9c2SAndrey Smirnov 				   SI476X_PROP_VALID_SNR_THRESHOLD,
575b879a9c2SAndrey Smirnov 				   SI476X_PROP_VALID_RSSI_THRESHOLD);
576b879a9c2SAndrey Smirnov 	if (err < 0)
577b879a9c2SAndrey Smirnov 		return err;
578b879a9c2SAndrey Smirnov 
579b879a9c2SAndrey Smirnov 	if (func == SI476X_FUNC_FM_RECEIVER) {
580b879a9c2SAndrey Smirnov 		if (si476x_core_has_diversity(radio->core)) {
581b879a9c2SAndrey Smirnov 			err = si476x_core_cmd_fm_phase_diversity(radio->core,
582b879a9c2SAndrey Smirnov 								 radio->core->diversity_mode);
583b879a9c2SAndrey Smirnov 			if (err < 0)
584b879a9c2SAndrey Smirnov 				return err;
585b879a9c2SAndrey Smirnov 		}
586b879a9c2SAndrey Smirnov 
587b879a9c2SAndrey Smirnov 		err = regcache_sync_region(radio->core->regmap,
588b879a9c2SAndrey Smirnov 					   SI476X_PROP_FM_RDS_INTERRUPT_SOURCE,
589b879a9c2SAndrey Smirnov 					   SI476X_PROP_FM_RDS_CONFIG);
590b879a9c2SAndrey Smirnov 		if (err < 0)
591b879a9c2SAndrey Smirnov 			return err;
592b879a9c2SAndrey Smirnov 	}
593b879a9c2SAndrey Smirnov 
594b879a9c2SAndrey Smirnov 	return si476x_radio_init_vtable(radio, func);
595b879a9c2SAndrey Smirnov 
596b879a9c2SAndrey Smirnov }
597b879a9c2SAndrey Smirnov 
si476x_radio_change_func(struct si476x_radio * radio,enum si476x_func func)598b879a9c2SAndrey Smirnov static int si476x_radio_change_func(struct si476x_radio *radio,
599b879a9c2SAndrey Smirnov 				    enum si476x_func func)
600b879a9c2SAndrey Smirnov {
601b879a9c2SAndrey Smirnov 	int err;
602b879a9c2SAndrey Smirnov 	bool soft;
603b879a9c2SAndrey Smirnov 	/*
604b879a9c2SAndrey Smirnov 	 * Since power/up down is a very time consuming operation,
605b879a9c2SAndrey Smirnov 	 * try to avoid doing it if the requested mode matches the one
606b879a9c2SAndrey Smirnov 	 * the tuner is in
607b879a9c2SAndrey Smirnov 	 */
608b879a9c2SAndrey Smirnov 	if (func == radio->core->power_up_parameters.func)
609b879a9c2SAndrey Smirnov 		return 0;
610b879a9c2SAndrey Smirnov 
611b879a9c2SAndrey Smirnov 	soft = true;
612b879a9c2SAndrey Smirnov 	err = si476x_core_stop(radio->core, soft);
613b879a9c2SAndrey Smirnov 	if (err < 0) {
614b879a9c2SAndrey Smirnov 		/*
615b879a9c2SAndrey Smirnov 		 * OK, if the chip does not want to play nice let's
616b879a9c2SAndrey Smirnov 		 * try to reset it in more brutal way
617b879a9c2SAndrey Smirnov 		 */
618b879a9c2SAndrey Smirnov 		soft = false;
619b879a9c2SAndrey Smirnov 		err = si476x_core_stop(radio->core, soft);
620b879a9c2SAndrey Smirnov 		if (err < 0)
621b879a9c2SAndrey Smirnov 			return err;
622b879a9c2SAndrey Smirnov 	}
623b879a9c2SAndrey Smirnov 	/*
624b879a9c2SAndrey Smirnov 	  Set the desired radio tuner function
625b879a9c2SAndrey Smirnov 	 */
626b879a9c2SAndrey Smirnov 	radio->core->power_up_parameters.func = func;
627b879a9c2SAndrey Smirnov 
628b879a9c2SAndrey Smirnov 	err = si476x_core_start(radio->core, soft);
629b879a9c2SAndrey Smirnov 	if (err < 0)
630b879a9c2SAndrey Smirnov 		return err;
631b879a9c2SAndrey Smirnov 
632b879a9c2SAndrey Smirnov 	/*
633b879a9c2SAndrey Smirnov 	 * No need to do the rest of manipulations for the bootlader
634b879a9c2SAndrey Smirnov 	 * mode
635b879a9c2SAndrey Smirnov 	 */
636b879a9c2SAndrey Smirnov 	if (func != SI476X_FUNC_FM_RECEIVER &&
637b879a9c2SAndrey Smirnov 	    func != SI476X_FUNC_AM_RECEIVER)
638b879a9c2SAndrey Smirnov 		return err;
639b879a9c2SAndrey Smirnov 
640b879a9c2SAndrey Smirnov 	return si476x_radio_do_post_powerup_init(radio, func);
641b879a9c2SAndrey Smirnov }
642b879a9c2SAndrey Smirnov 
si476x_radio_g_frequency(struct file * file,void * priv,struct v4l2_frequency * f)643b879a9c2SAndrey Smirnov static int si476x_radio_g_frequency(struct file *file, void *priv,
644b879a9c2SAndrey Smirnov 			      struct v4l2_frequency *f)
645b879a9c2SAndrey Smirnov {
646b879a9c2SAndrey Smirnov 	int err;
647b879a9c2SAndrey Smirnov 	struct si476x_radio *radio = video_drvdata(file);
648b879a9c2SAndrey Smirnov 
649b879a9c2SAndrey Smirnov 	if (f->tuner != 0 ||
650b879a9c2SAndrey Smirnov 	    f->type  != V4L2_TUNER_RADIO)
651b879a9c2SAndrey Smirnov 		return -EINVAL;
652b879a9c2SAndrey Smirnov 
653b879a9c2SAndrey Smirnov 	si476x_core_lock(radio->core);
654b879a9c2SAndrey Smirnov 
655b879a9c2SAndrey Smirnov 	if (radio->ops->rsq_status) {
656b879a9c2SAndrey Smirnov 		struct si476x_rsq_status_report report;
657b879a9c2SAndrey Smirnov 		struct si476x_rsq_status_args   args = {
658b879a9c2SAndrey Smirnov 			.primary	= false,
659b879a9c2SAndrey Smirnov 			.rsqack		= false,
660b879a9c2SAndrey Smirnov 			.attune		= true,
661b879a9c2SAndrey Smirnov 			.cancel		= false,
662b879a9c2SAndrey Smirnov 			.stcack		= false,
663b879a9c2SAndrey Smirnov 		};
664b879a9c2SAndrey Smirnov 
665b879a9c2SAndrey Smirnov 		err = radio->ops->rsq_status(radio->core, &args, &report);
666b879a9c2SAndrey Smirnov 		if (!err)
667b879a9c2SAndrey Smirnov 			f->frequency = si476x_to_v4l2(radio->core,
668b879a9c2SAndrey Smirnov 						      report.readfreq);
669b879a9c2SAndrey Smirnov 	} else {
670b879a9c2SAndrey Smirnov 		err = -EINVAL;
671b879a9c2SAndrey Smirnov 	}
672b879a9c2SAndrey Smirnov 
673b879a9c2SAndrey Smirnov 	si476x_core_unlock(radio->core);
674b879a9c2SAndrey Smirnov 
675b879a9c2SAndrey Smirnov 	return err;
676b879a9c2SAndrey Smirnov }
677b879a9c2SAndrey Smirnov 
si476x_radio_s_frequency(struct file * file,void * priv,const struct v4l2_frequency * f)678b879a9c2SAndrey Smirnov static int si476x_radio_s_frequency(struct file *file, void *priv,
679b879a9c2SAndrey Smirnov 				    const struct v4l2_frequency *f)
680b879a9c2SAndrey Smirnov {
681b879a9c2SAndrey Smirnov 	int err;
682b879a9c2SAndrey Smirnov 	u32 freq = f->frequency;
683b879a9c2SAndrey Smirnov 	struct si476x_tune_freq_args args;
684b879a9c2SAndrey Smirnov 	struct si476x_radio *radio = video_drvdata(file);
685b879a9c2SAndrey Smirnov 
686b879a9c2SAndrey Smirnov 	const u32 midrange = (si476x_bands[SI476X_BAND_AM].rangehigh +
687b879a9c2SAndrey Smirnov 			      si476x_bands[SI476X_BAND_FM].rangelow) / 2;
688b879a9c2SAndrey Smirnov 	const int band = (freq > midrange) ?
689b879a9c2SAndrey Smirnov 		SI476X_BAND_FM : SI476X_BAND_AM;
690b879a9c2SAndrey Smirnov 	const enum si476x_func func = (band == SI476X_BAND_AM) ?
691b879a9c2SAndrey Smirnov 		SI476X_FUNC_AM_RECEIVER : SI476X_FUNC_FM_RECEIVER;
692b879a9c2SAndrey Smirnov 
693b879a9c2SAndrey Smirnov 	if (f->tuner != 0 ||
694b879a9c2SAndrey Smirnov 	    f->type  != V4L2_TUNER_RADIO)
695b879a9c2SAndrey Smirnov 		return -EINVAL;
696b879a9c2SAndrey Smirnov 
697b879a9c2SAndrey Smirnov 	si476x_core_lock(radio->core);
698b879a9c2SAndrey Smirnov 
699b879a9c2SAndrey Smirnov 	freq = clamp(freq,
700b879a9c2SAndrey Smirnov 		     si476x_bands[band].rangelow,
701b879a9c2SAndrey Smirnov 		     si476x_bands[band].rangehigh);
702b879a9c2SAndrey Smirnov 
703b879a9c2SAndrey Smirnov 	if (si476x_radio_freq_is_inside_of_the_band(freq,
704b879a9c2SAndrey Smirnov 						    SI476X_BAND_AM) &&
705b879a9c2SAndrey Smirnov 	    (!si476x_core_has_am(radio->core) ||
706b879a9c2SAndrey Smirnov 	     si476x_core_is_a_secondary_tuner(radio->core))) {
707b879a9c2SAndrey Smirnov 		err = -EINVAL;
708b879a9c2SAndrey Smirnov 		goto unlock;
709b879a9c2SAndrey Smirnov 	}
710b879a9c2SAndrey Smirnov 
711b879a9c2SAndrey Smirnov 	err = si476x_radio_change_func(radio, func);
712b879a9c2SAndrey Smirnov 	if (err < 0)
713b879a9c2SAndrey Smirnov 		goto unlock;
714b879a9c2SAndrey Smirnov 
715b879a9c2SAndrey Smirnov 	args.zifsr		= false;
716b879a9c2SAndrey Smirnov 	args.hd			= false;
717b879a9c2SAndrey Smirnov 	args.injside		= SI476X_INJSIDE_AUTO;
718b879a9c2SAndrey Smirnov 	args.freq		= v4l2_to_si476x(radio->core, freq);
719b879a9c2SAndrey Smirnov 	args.tunemode		= SI476X_TM_VALIDATED_NORMAL_TUNE;
720b879a9c2SAndrey Smirnov 	args.smoothmetrics	= SI476X_SM_INITIALIZE_AUDIO;
721b879a9c2SAndrey Smirnov 	args.antcap		= 0;
722b879a9c2SAndrey Smirnov 
723b879a9c2SAndrey Smirnov 	err = radio->ops->tune_freq(radio->core, &args);
724b879a9c2SAndrey Smirnov 
725b879a9c2SAndrey Smirnov unlock:
726b879a9c2SAndrey Smirnov 	si476x_core_unlock(radio->core);
727b879a9c2SAndrey Smirnov 	return err;
728b879a9c2SAndrey Smirnov }
729b879a9c2SAndrey Smirnov 
si476x_radio_s_hw_freq_seek(struct file * file,void * priv,const struct v4l2_hw_freq_seek * seek)730b879a9c2SAndrey Smirnov static int si476x_radio_s_hw_freq_seek(struct file *file, void *priv,
731b879a9c2SAndrey Smirnov 				       const struct v4l2_hw_freq_seek *seek)
732b879a9c2SAndrey Smirnov {
733b879a9c2SAndrey Smirnov 	int err;
734b879a9c2SAndrey Smirnov 	enum si476x_func func;
73502d73243SMauro Carvalho Chehab 	u32 rangelow = seek->rangelow, rangehigh = seek->rangehigh;
736b879a9c2SAndrey Smirnov 	struct si476x_radio *radio = video_drvdata(file);
737b879a9c2SAndrey Smirnov 
738b879a9c2SAndrey Smirnov 	if (file->f_flags & O_NONBLOCK)
739b879a9c2SAndrey Smirnov 		return -EAGAIN;
740b879a9c2SAndrey Smirnov 
741b879a9c2SAndrey Smirnov 	if (seek->tuner != 0 ||
742b879a9c2SAndrey Smirnov 	    seek->type  != V4L2_TUNER_RADIO)
743b879a9c2SAndrey Smirnov 		return -EINVAL;
744b879a9c2SAndrey Smirnov 
745b879a9c2SAndrey Smirnov 	si476x_core_lock(radio->core);
746b879a9c2SAndrey Smirnov 
74702d73243SMauro Carvalho Chehab 	if (!rangelow) {
748b879a9c2SAndrey Smirnov 		err = regmap_read(radio->core->regmap,
749b879a9c2SAndrey Smirnov 				  SI476X_PROP_SEEK_BAND_BOTTOM,
750b879a9c2SAndrey Smirnov 				  &rangelow);
75102d73243SMauro Carvalho Chehab 		if (err)
752b879a9c2SAndrey Smirnov 			goto unlock;
75302d73243SMauro Carvalho Chehab 		rangelow = si476x_to_v4l2(radio->core, rangelow);
754b879a9c2SAndrey Smirnov 	}
75502d73243SMauro Carvalho Chehab 	if (!rangehigh) {
756b879a9c2SAndrey Smirnov 		err = regmap_read(radio->core->regmap,
757b879a9c2SAndrey Smirnov 				  SI476X_PROP_SEEK_BAND_TOP,
758b879a9c2SAndrey Smirnov 				  &rangehigh);
75902d73243SMauro Carvalho Chehab 		if (err)
760b879a9c2SAndrey Smirnov 			goto unlock;
76102d73243SMauro Carvalho Chehab 		rangehigh = si476x_to_v4l2(radio->core, rangehigh);
762b879a9c2SAndrey Smirnov 	}
763b879a9c2SAndrey Smirnov 
764b879a9c2SAndrey Smirnov 	if (rangelow > rangehigh) {
765b879a9c2SAndrey Smirnov 		err = -EINVAL;
766b879a9c2SAndrey Smirnov 		goto unlock;
767b879a9c2SAndrey Smirnov 	}
768b879a9c2SAndrey Smirnov 
769b879a9c2SAndrey Smirnov 	if (si476x_radio_range_is_inside_of_the_band(rangelow, rangehigh,
770b879a9c2SAndrey Smirnov 						     SI476X_BAND_FM)) {
771b879a9c2SAndrey Smirnov 		func = SI476X_FUNC_FM_RECEIVER;
772b879a9c2SAndrey Smirnov 
773b879a9c2SAndrey Smirnov 	} else if (si476x_core_has_am(radio->core) &&
774b879a9c2SAndrey Smirnov 		   si476x_radio_range_is_inside_of_the_band(rangelow, rangehigh,
775b879a9c2SAndrey Smirnov 							    SI476X_BAND_AM)) {
776b879a9c2SAndrey Smirnov 		func = SI476X_FUNC_AM_RECEIVER;
777b879a9c2SAndrey Smirnov 	} else {
778b879a9c2SAndrey Smirnov 		err = -EINVAL;
779b879a9c2SAndrey Smirnov 		goto unlock;
780b879a9c2SAndrey Smirnov 	}
781b879a9c2SAndrey Smirnov 
782b879a9c2SAndrey Smirnov 	err = si476x_radio_change_func(radio, func);
783b879a9c2SAndrey Smirnov 	if (err < 0)
784b879a9c2SAndrey Smirnov 		goto unlock;
785b879a9c2SAndrey Smirnov 
786b879a9c2SAndrey Smirnov 	if (seek->rangehigh) {
787b879a9c2SAndrey Smirnov 		err = regmap_write(radio->core->regmap,
788b879a9c2SAndrey Smirnov 				   SI476X_PROP_SEEK_BAND_TOP,
789b879a9c2SAndrey Smirnov 				   v4l2_to_si476x(radio->core,
790b879a9c2SAndrey Smirnov 						  seek->rangehigh));
791b879a9c2SAndrey Smirnov 		if (err)
792b879a9c2SAndrey Smirnov 			goto unlock;
793b879a9c2SAndrey Smirnov 	}
794b879a9c2SAndrey Smirnov 	if (seek->rangelow) {
795b879a9c2SAndrey Smirnov 		err = regmap_write(radio->core->regmap,
796b879a9c2SAndrey Smirnov 				   SI476X_PROP_SEEK_BAND_BOTTOM,
797b879a9c2SAndrey Smirnov 				   v4l2_to_si476x(radio->core,
798b879a9c2SAndrey Smirnov 						  seek->rangelow));
799b879a9c2SAndrey Smirnov 		if (err)
800b879a9c2SAndrey Smirnov 			goto unlock;
801b879a9c2SAndrey Smirnov 	}
802b879a9c2SAndrey Smirnov 	if (seek->spacing) {
803b879a9c2SAndrey Smirnov 		err = regmap_write(radio->core->regmap,
804b879a9c2SAndrey Smirnov 				     SI476X_PROP_SEEK_FREQUENCY_SPACING,
805b879a9c2SAndrey Smirnov 				     v4l2_to_si476x(radio->core,
806b879a9c2SAndrey Smirnov 						    seek->spacing));
807b879a9c2SAndrey Smirnov 		if (err)
808b879a9c2SAndrey Smirnov 			goto unlock;
809b879a9c2SAndrey Smirnov 	}
810b879a9c2SAndrey Smirnov 
811b879a9c2SAndrey Smirnov 	err = radio->ops->seek_start(radio->core,
812b879a9c2SAndrey Smirnov 				     seek->seek_upward,
813b879a9c2SAndrey Smirnov 				     seek->wrap_around);
814b879a9c2SAndrey Smirnov unlock:
815b879a9c2SAndrey Smirnov 	si476x_core_unlock(radio->core);
816b879a9c2SAndrey Smirnov 
817b879a9c2SAndrey Smirnov 
818b879a9c2SAndrey Smirnov 
819b879a9c2SAndrey Smirnov 	return err;
820b879a9c2SAndrey Smirnov }
821b879a9c2SAndrey Smirnov 
si476x_radio_g_volatile_ctrl(struct v4l2_ctrl * ctrl)822b879a9c2SAndrey Smirnov static int si476x_radio_g_volatile_ctrl(struct v4l2_ctrl *ctrl)
823b879a9c2SAndrey Smirnov {
824b879a9c2SAndrey Smirnov 	int retval;
825b879a9c2SAndrey Smirnov 	struct si476x_radio *radio = v4l2_ctrl_handler_to_radio(ctrl->handler);
826b879a9c2SAndrey Smirnov 
827b879a9c2SAndrey Smirnov 	si476x_core_lock(radio->core);
828b879a9c2SAndrey Smirnov 
829b879a9c2SAndrey Smirnov 	switch (ctrl->id) {
830b879a9c2SAndrey Smirnov 	case V4L2_CID_SI476X_INTERCHIP_LINK:
831b879a9c2SAndrey Smirnov 		if (si476x_core_has_diversity(radio->core)) {
832b879a9c2SAndrey Smirnov 			if (radio->ops->phase_diversity) {
833b879a9c2SAndrey Smirnov 				retval = radio->ops->phase_div_status(radio->core);
834b879a9c2SAndrey Smirnov 				if (retval < 0)
835b879a9c2SAndrey Smirnov 					break;
836b879a9c2SAndrey Smirnov 
837b879a9c2SAndrey Smirnov 				ctrl->val = !!SI476X_PHDIV_STATUS_LINK_LOCKED(retval);
838b879a9c2SAndrey Smirnov 				retval = 0;
839b879a9c2SAndrey Smirnov 				break;
840b879a9c2SAndrey Smirnov 			} else {
841b879a9c2SAndrey Smirnov 				retval = -ENOTTY;
842b879a9c2SAndrey Smirnov 				break;
843b879a9c2SAndrey Smirnov 			}
844b879a9c2SAndrey Smirnov 		}
845b879a9c2SAndrey Smirnov 		retval = -EINVAL;
846b879a9c2SAndrey Smirnov 		break;
847b879a9c2SAndrey Smirnov 	default:
848b879a9c2SAndrey Smirnov 		retval = -EINVAL;
849b879a9c2SAndrey Smirnov 		break;
850b879a9c2SAndrey Smirnov 	}
851b879a9c2SAndrey Smirnov 	si476x_core_unlock(radio->core);
852b879a9c2SAndrey Smirnov 	return retval;
853b879a9c2SAndrey Smirnov 
854b879a9c2SAndrey Smirnov }
855b879a9c2SAndrey Smirnov 
si476x_radio_s_ctrl(struct v4l2_ctrl * ctrl)856b879a9c2SAndrey Smirnov static int si476x_radio_s_ctrl(struct v4l2_ctrl *ctrl)
857b879a9c2SAndrey Smirnov {
858b879a9c2SAndrey Smirnov 	int retval;
859b879a9c2SAndrey Smirnov 	enum si476x_phase_diversity_mode mode;
860b879a9c2SAndrey Smirnov 	struct si476x_radio *radio = v4l2_ctrl_handler_to_radio(ctrl->handler);
861b879a9c2SAndrey Smirnov 
862b879a9c2SAndrey Smirnov 	si476x_core_lock(radio->core);
863b879a9c2SAndrey Smirnov 
864b879a9c2SAndrey Smirnov 	switch (ctrl->id) {
865b879a9c2SAndrey Smirnov 	case V4L2_CID_SI476X_HARMONICS_COUNT:
866b879a9c2SAndrey Smirnov 		retval = regmap_update_bits(radio->core->regmap,
867b879a9c2SAndrey Smirnov 					    SI476X_PROP_AUDIO_PWR_LINE_FILTER,
868b879a9c2SAndrey Smirnov 					    SI476X_PROP_PWR_HARMONICS_MASK,
869b879a9c2SAndrey Smirnov 					    ctrl->val);
870b879a9c2SAndrey Smirnov 		break;
871b879a9c2SAndrey Smirnov 	case V4L2_CID_POWER_LINE_FREQUENCY:
872b879a9c2SAndrey Smirnov 		switch (ctrl->val) {
873b879a9c2SAndrey Smirnov 		case V4L2_CID_POWER_LINE_FREQUENCY_DISABLED:
874b879a9c2SAndrey Smirnov 			retval = regmap_update_bits(radio->core->regmap,
875b879a9c2SAndrey Smirnov 						    SI476X_PROP_AUDIO_PWR_LINE_FILTER,
876b879a9c2SAndrey Smirnov 						    SI476X_PROP_PWR_ENABLE_MASK,
877b879a9c2SAndrey Smirnov 						    0);
878b879a9c2SAndrey Smirnov 			break;
879b879a9c2SAndrey Smirnov 		case V4L2_CID_POWER_LINE_FREQUENCY_50HZ:
880b879a9c2SAndrey Smirnov 			retval = regmap_update_bits(radio->core->regmap,
881b879a9c2SAndrey Smirnov 						    SI476X_PROP_AUDIO_PWR_LINE_FILTER,
882b879a9c2SAndrey Smirnov 						    SI476X_PROP_PWR_GRID_MASK,
883b879a9c2SAndrey Smirnov 						    SI476X_PROP_PWR_GRID_50HZ);
884b879a9c2SAndrey Smirnov 			break;
885b879a9c2SAndrey Smirnov 		case V4L2_CID_POWER_LINE_FREQUENCY_60HZ:
886b879a9c2SAndrey Smirnov 			retval = regmap_update_bits(radio->core->regmap,
887b879a9c2SAndrey Smirnov 						    SI476X_PROP_AUDIO_PWR_LINE_FILTER,
888b879a9c2SAndrey Smirnov 						    SI476X_PROP_PWR_GRID_MASK,
889b879a9c2SAndrey Smirnov 						    SI476X_PROP_PWR_GRID_60HZ);
890b879a9c2SAndrey Smirnov 			break;
891b879a9c2SAndrey Smirnov 		default:
892b879a9c2SAndrey Smirnov 			retval = -EINVAL;
893b879a9c2SAndrey Smirnov 			break;
894b879a9c2SAndrey Smirnov 		}
895b879a9c2SAndrey Smirnov 		break;
896b879a9c2SAndrey Smirnov 	case V4L2_CID_SI476X_RSSI_THRESHOLD:
897b879a9c2SAndrey Smirnov 		retval = regmap_write(radio->core->regmap,
898b879a9c2SAndrey Smirnov 				      SI476X_PROP_VALID_RSSI_THRESHOLD,
899b879a9c2SAndrey Smirnov 				      ctrl->val);
900b879a9c2SAndrey Smirnov 		break;
901b879a9c2SAndrey Smirnov 	case V4L2_CID_SI476X_SNR_THRESHOLD:
902b879a9c2SAndrey Smirnov 		retval = regmap_write(radio->core->regmap,
903b879a9c2SAndrey Smirnov 				      SI476X_PROP_VALID_SNR_THRESHOLD,
904b879a9c2SAndrey Smirnov 				      ctrl->val);
905b879a9c2SAndrey Smirnov 		break;
906b879a9c2SAndrey Smirnov 	case V4L2_CID_SI476X_MAX_TUNE_ERROR:
907b879a9c2SAndrey Smirnov 		retval = regmap_write(radio->core->regmap,
908b879a9c2SAndrey Smirnov 				      SI476X_PROP_VALID_MAX_TUNE_ERROR,
909b879a9c2SAndrey Smirnov 				      ctrl->val);
910b879a9c2SAndrey Smirnov 		break;
911b879a9c2SAndrey Smirnov 	case V4L2_CID_RDS_RECEPTION:
912b879a9c2SAndrey Smirnov 		/*
913b879a9c2SAndrey Smirnov 		 * It looks like RDS related properties are
91430bcc510SXiaofeng Cao 		 * inaccessible when tuner is in AM mode, so cache the
915b879a9c2SAndrey Smirnov 		 * changes
916b879a9c2SAndrey Smirnov 		 */
917b879a9c2SAndrey Smirnov 		if (si476x_core_is_in_am_receiver_mode(radio->core))
918b879a9c2SAndrey Smirnov 			regcache_cache_only(radio->core->regmap, true);
919b879a9c2SAndrey Smirnov 
920b879a9c2SAndrey Smirnov 		if (ctrl->val) {
921b879a9c2SAndrey Smirnov 			retval = regmap_write(radio->core->regmap,
922b879a9c2SAndrey Smirnov 					      SI476X_PROP_FM_RDS_INTERRUPT_FIFO_COUNT,
923b879a9c2SAndrey Smirnov 					      radio->core->rds_fifo_depth);
924b879a9c2SAndrey Smirnov 			if (retval < 0)
925b879a9c2SAndrey Smirnov 				break;
926b879a9c2SAndrey Smirnov 
927b879a9c2SAndrey Smirnov 			if (radio->core->client->irq) {
928b879a9c2SAndrey Smirnov 				retval = regmap_write(radio->core->regmap,
929b879a9c2SAndrey Smirnov 						      SI476X_PROP_FM_RDS_INTERRUPT_SOURCE,
930b879a9c2SAndrey Smirnov 						      SI476X_RDSRECV);
931b879a9c2SAndrey Smirnov 				if (retval < 0)
932b879a9c2SAndrey Smirnov 					break;
933b879a9c2SAndrey Smirnov 			}
934b879a9c2SAndrey Smirnov 
935b879a9c2SAndrey Smirnov 			/* Drain RDS FIFO before enabling RDS processing */
936b879a9c2SAndrey Smirnov 			retval = si476x_core_cmd_fm_rds_status(radio->core,
937b879a9c2SAndrey Smirnov 							       false,
938b879a9c2SAndrey Smirnov 							       true,
939b879a9c2SAndrey Smirnov 							       true,
940b879a9c2SAndrey Smirnov 							       NULL);
941b879a9c2SAndrey Smirnov 			if (retval < 0)
942b879a9c2SAndrey Smirnov 				break;
943b879a9c2SAndrey Smirnov 
944b879a9c2SAndrey Smirnov 			retval = regmap_update_bits(radio->core->regmap,
945b879a9c2SAndrey Smirnov 						    SI476X_PROP_FM_RDS_CONFIG,
946b879a9c2SAndrey Smirnov 						    SI476X_PROP_RDSEN_MASK,
947b879a9c2SAndrey Smirnov 						    SI476X_PROP_RDSEN);
948b879a9c2SAndrey Smirnov 		} else {
949b879a9c2SAndrey Smirnov 			retval = regmap_update_bits(radio->core->regmap,
950b879a9c2SAndrey Smirnov 						    SI476X_PROP_FM_RDS_CONFIG,
951b879a9c2SAndrey Smirnov 						    SI476X_PROP_RDSEN_MASK,
952b879a9c2SAndrey Smirnov 						    !SI476X_PROP_RDSEN);
953b879a9c2SAndrey Smirnov 		}
954b879a9c2SAndrey Smirnov 
955b879a9c2SAndrey Smirnov 		if (si476x_core_is_in_am_receiver_mode(radio->core))
956b879a9c2SAndrey Smirnov 			regcache_cache_only(radio->core->regmap, false);
957b879a9c2SAndrey Smirnov 		break;
958b879a9c2SAndrey Smirnov 	case V4L2_CID_TUNE_DEEMPHASIS:
959b879a9c2SAndrey Smirnov 		retval = regmap_write(radio->core->regmap,
960b879a9c2SAndrey Smirnov 				      SI476X_PROP_AUDIO_DEEMPHASIS,
961b879a9c2SAndrey Smirnov 				      ctrl->val);
962b879a9c2SAndrey Smirnov 		break;
963b879a9c2SAndrey Smirnov 
964b879a9c2SAndrey Smirnov 	case V4L2_CID_SI476X_DIVERSITY_MODE:
965b879a9c2SAndrey Smirnov 		mode = si476x_phase_diversity_idx_to_mode(ctrl->val);
966b879a9c2SAndrey Smirnov 
967b879a9c2SAndrey Smirnov 		if (mode == radio->core->diversity_mode) {
968b879a9c2SAndrey Smirnov 			retval = 0;
969b879a9c2SAndrey Smirnov 			break;
970b879a9c2SAndrey Smirnov 		}
971b879a9c2SAndrey Smirnov 
972b879a9c2SAndrey Smirnov 		if (si476x_core_is_in_am_receiver_mode(radio->core)) {
973b879a9c2SAndrey Smirnov 			/*
974b879a9c2SAndrey Smirnov 			 * Diversity cannot be configured while tuner
975b879a9c2SAndrey Smirnov 			 * is in AM mode so save the changes and carry on.
976b879a9c2SAndrey Smirnov 			 */
977b879a9c2SAndrey Smirnov 			radio->core->diversity_mode = mode;
978b879a9c2SAndrey Smirnov 			retval = 0;
979b879a9c2SAndrey Smirnov 		} else {
980b879a9c2SAndrey Smirnov 			retval = radio->ops->phase_diversity(radio->core, mode);
981b879a9c2SAndrey Smirnov 			if (!retval)
982b879a9c2SAndrey Smirnov 				radio->core->diversity_mode = mode;
983b879a9c2SAndrey Smirnov 		}
984b879a9c2SAndrey Smirnov 		break;
985b879a9c2SAndrey Smirnov 
986b879a9c2SAndrey Smirnov 	default:
987b879a9c2SAndrey Smirnov 		retval = -EINVAL;
988b879a9c2SAndrey Smirnov 		break;
989b879a9c2SAndrey Smirnov 	}
990b879a9c2SAndrey Smirnov 
991b879a9c2SAndrey Smirnov 	si476x_core_unlock(radio->core);
992b879a9c2SAndrey Smirnov 
993b879a9c2SAndrey Smirnov 	return retval;
994b879a9c2SAndrey Smirnov }
995b879a9c2SAndrey Smirnov 
996b879a9c2SAndrey Smirnov #ifdef CONFIG_VIDEO_ADV_DEBUG
si476x_radio_g_register(struct file * file,void * fh,struct v4l2_dbg_register * reg)997b879a9c2SAndrey Smirnov static int si476x_radio_g_register(struct file *file, void *fh,
998b879a9c2SAndrey Smirnov 				   struct v4l2_dbg_register *reg)
999b879a9c2SAndrey Smirnov {
1000b879a9c2SAndrey Smirnov 	int err;
1001b879a9c2SAndrey Smirnov 	unsigned int value;
1002b879a9c2SAndrey Smirnov 	struct si476x_radio *radio = video_drvdata(file);
1003b879a9c2SAndrey Smirnov 
1004b879a9c2SAndrey Smirnov 	si476x_core_lock(radio->core);
1005b879a9c2SAndrey Smirnov 	reg->size = 2;
1006b879a9c2SAndrey Smirnov 	err = regmap_read(radio->core->regmap,
1007b879a9c2SAndrey Smirnov 			  (unsigned int)reg->reg, &value);
1008b879a9c2SAndrey Smirnov 	reg->val = value;
1009b879a9c2SAndrey Smirnov 	si476x_core_unlock(radio->core);
1010b879a9c2SAndrey Smirnov 
1011b879a9c2SAndrey Smirnov 	return err;
1012b879a9c2SAndrey Smirnov }
si476x_radio_s_register(struct file * file,void * fh,const struct v4l2_dbg_register * reg)1013b879a9c2SAndrey Smirnov static int si476x_radio_s_register(struct file *file, void *fh,
1014b879a9c2SAndrey Smirnov 				   const struct v4l2_dbg_register *reg)
1015b879a9c2SAndrey Smirnov {
1016b879a9c2SAndrey Smirnov 
1017b879a9c2SAndrey Smirnov 	int err;
1018b879a9c2SAndrey Smirnov 	struct si476x_radio *radio = video_drvdata(file);
1019b879a9c2SAndrey Smirnov 
1020b879a9c2SAndrey Smirnov 	si476x_core_lock(radio->core);
1021b879a9c2SAndrey Smirnov 	err = regmap_write(radio->core->regmap,
1022b879a9c2SAndrey Smirnov 			   (unsigned int)reg->reg,
1023b879a9c2SAndrey Smirnov 			   (unsigned int)reg->val);
1024b879a9c2SAndrey Smirnov 	si476x_core_unlock(radio->core);
1025b879a9c2SAndrey Smirnov 
1026b879a9c2SAndrey Smirnov 	return err;
1027b879a9c2SAndrey Smirnov }
1028b879a9c2SAndrey Smirnov #endif
1029b879a9c2SAndrey Smirnov 
si476x_radio_fops_open(struct file * file)1030b879a9c2SAndrey Smirnov static int si476x_radio_fops_open(struct file *file)
1031b879a9c2SAndrey Smirnov {
1032b879a9c2SAndrey Smirnov 	struct si476x_radio *radio = video_drvdata(file);
1033b879a9c2SAndrey Smirnov 	int err;
1034b879a9c2SAndrey Smirnov 
1035b879a9c2SAndrey Smirnov 	err = v4l2_fh_open(file);
1036b879a9c2SAndrey Smirnov 	if (err)
1037b879a9c2SAndrey Smirnov 		return err;
1038b879a9c2SAndrey Smirnov 
1039b879a9c2SAndrey Smirnov 	if (v4l2_fh_is_singular_file(file)) {
1040b879a9c2SAndrey Smirnov 		si476x_core_lock(radio->core);
1041b879a9c2SAndrey Smirnov 		err = si476x_core_set_power_state(radio->core,
1042b879a9c2SAndrey Smirnov 						  SI476X_POWER_UP_FULL);
1043b879a9c2SAndrey Smirnov 		if (err < 0)
1044b879a9c2SAndrey Smirnov 			goto done;
1045b879a9c2SAndrey Smirnov 
1046b879a9c2SAndrey Smirnov 		err = si476x_radio_do_post_powerup_init(radio,
1047b879a9c2SAndrey Smirnov 							radio->core->power_up_parameters.func);
1048b879a9c2SAndrey Smirnov 		if (err < 0)
1049b879a9c2SAndrey Smirnov 			goto power_down;
1050b879a9c2SAndrey Smirnov 
1051b879a9c2SAndrey Smirnov 		err = si476x_radio_pretune(radio,
1052b879a9c2SAndrey Smirnov 					   radio->core->power_up_parameters.func);
1053b879a9c2SAndrey Smirnov 		if (err < 0)
1054b879a9c2SAndrey Smirnov 			goto power_down;
1055b879a9c2SAndrey Smirnov 
1056b879a9c2SAndrey Smirnov 		si476x_core_unlock(radio->core);
1057b879a9c2SAndrey Smirnov 		/*Must be done after si476x_core_unlock to prevent a deadlock*/
1058b879a9c2SAndrey Smirnov 		v4l2_ctrl_handler_setup(&radio->ctrl_handler);
1059b879a9c2SAndrey Smirnov 	}
1060b879a9c2SAndrey Smirnov 
1061b879a9c2SAndrey Smirnov 	return err;
1062b879a9c2SAndrey Smirnov 
1063b879a9c2SAndrey Smirnov power_down:
1064b879a9c2SAndrey Smirnov 	si476x_core_set_power_state(radio->core,
1065b879a9c2SAndrey Smirnov 				    SI476X_POWER_DOWN);
1066b879a9c2SAndrey Smirnov done:
1067b879a9c2SAndrey Smirnov 	si476x_core_unlock(radio->core);
1068b879a9c2SAndrey Smirnov 	v4l2_fh_release(file);
1069b879a9c2SAndrey Smirnov 
1070b879a9c2SAndrey Smirnov 	return err;
1071b879a9c2SAndrey Smirnov }
1072b879a9c2SAndrey Smirnov 
si476x_radio_fops_release(struct file * file)1073b879a9c2SAndrey Smirnov static int si476x_radio_fops_release(struct file *file)
1074b879a9c2SAndrey Smirnov {
1075b879a9c2SAndrey Smirnov 	struct si476x_radio *radio = video_drvdata(file);
1076b879a9c2SAndrey Smirnov 
1077b879a9c2SAndrey Smirnov 	if (v4l2_fh_is_singular_file(file) &&
1078b879a9c2SAndrey Smirnov 	    atomic_read(&radio->core->is_alive))
1079b879a9c2SAndrey Smirnov 		si476x_core_set_power_state(radio->core,
1080b879a9c2SAndrey Smirnov 					    SI476X_POWER_DOWN);
1081b879a9c2SAndrey Smirnov 
1082bf4ed9e3Sye xingchen 	return v4l2_fh_release(file);
1083b879a9c2SAndrey Smirnov }
1084b879a9c2SAndrey Smirnov 
si476x_radio_fops_read(struct file * file,char __user * buf,size_t count,loff_t * ppos)1085b879a9c2SAndrey Smirnov static ssize_t si476x_radio_fops_read(struct file *file, char __user *buf,
1086b879a9c2SAndrey Smirnov 				      size_t count, loff_t *ppos)
1087b879a9c2SAndrey Smirnov {
1088b879a9c2SAndrey Smirnov 	ssize_t      rval;
1089b879a9c2SAndrey Smirnov 	size_t       fifo_len;
1090b879a9c2SAndrey Smirnov 	unsigned int copied;
1091b879a9c2SAndrey Smirnov 
1092b879a9c2SAndrey Smirnov 	struct si476x_radio *radio = video_drvdata(file);
1093b879a9c2SAndrey Smirnov 
1094b879a9c2SAndrey Smirnov 	/* block if no new data available */
1095b879a9c2SAndrey Smirnov 	if (kfifo_is_empty(&radio->core->rds_fifo)) {
1096b879a9c2SAndrey Smirnov 		if (file->f_flags & O_NONBLOCK)
1097b879a9c2SAndrey Smirnov 			return -EWOULDBLOCK;
1098b879a9c2SAndrey Smirnov 
1099b879a9c2SAndrey Smirnov 		rval = wait_event_interruptible(radio->core->rds_read_queue,
1100b879a9c2SAndrey Smirnov 						(!kfifo_is_empty(&radio->core->rds_fifo) ||
1101b879a9c2SAndrey Smirnov 						 !atomic_read(&radio->core->is_alive)));
1102b879a9c2SAndrey Smirnov 		if (rval < 0)
1103b879a9c2SAndrey Smirnov 			return -EINTR;
1104b879a9c2SAndrey Smirnov 
1105b879a9c2SAndrey Smirnov 		if (!atomic_read(&radio->core->is_alive))
1106b879a9c2SAndrey Smirnov 			return -ENODEV;
1107b879a9c2SAndrey Smirnov 	}
1108b879a9c2SAndrey Smirnov 
1109b879a9c2SAndrey Smirnov 	fifo_len = kfifo_len(&radio->core->rds_fifo);
1110b879a9c2SAndrey Smirnov 
1111b879a9c2SAndrey Smirnov 	if (kfifo_to_user(&radio->core->rds_fifo, buf,
1112b879a9c2SAndrey Smirnov 			  min(fifo_len, count),
1113b879a9c2SAndrey Smirnov 			  &copied) != 0) {
1114b879a9c2SAndrey Smirnov 		dev_warn(&radio->videodev.dev,
1115b879a9c2SAndrey Smirnov 			 "Error during FIFO to userspace copy\n");
1116b879a9c2SAndrey Smirnov 		rval = -EIO;
1117b879a9c2SAndrey Smirnov 	} else {
1118b879a9c2SAndrey Smirnov 		rval = (ssize_t)copied;
1119b879a9c2SAndrey Smirnov 	}
1120b879a9c2SAndrey Smirnov 
1121b879a9c2SAndrey Smirnov 	return rval;
1122b879a9c2SAndrey Smirnov }
1123b879a9c2SAndrey Smirnov 
si476x_radio_fops_poll(struct file * file,struct poll_table_struct * pts)1124c23e0cb8SAl Viro static __poll_t si476x_radio_fops_poll(struct file *file,
1125b879a9c2SAndrey Smirnov 				struct poll_table_struct *pts)
1126b879a9c2SAndrey Smirnov {
1127b879a9c2SAndrey Smirnov 	struct si476x_radio *radio = video_drvdata(file);
112801699437SAl Viro 	__poll_t req_events = poll_requested_events(pts);
1129c23e0cb8SAl Viro 	__poll_t err = v4l2_ctrl_poll(file, pts);
1130b879a9c2SAndrey Smirnov 
1131a9a08845SLinus Torvalds 	if (req_events & (EPOLLIN | EPOLLRDNORM)) {
1132b879a9c2SAndrey Smirnov 		if (atomic_read(&radio->core->is_alive))
1133b879a9c2SAndrey Smirnov 			poll_wait(file, &radio->core->rds_read_queue, pts);
1134b879a9c2SAndrey Smirnov 
1135b879a9c2SAndrey Smirnov 		if (!atomic_read(&radio->core->is_alive))
1136a9a08845SLinus Torvalds 			err = EPOLLHUP;
1137b879a9c2SAndrey Smirnov 
1138b879a9c2SAndrey Smirnov 		if (!kfifo_is_empty(&radio->core->rds_fifo))
1139a9a08845SLinus Torvalds 			err = EPOLLIN | EPOLLRDNORM;
1140b879a9c2SAndrey Smirnov 	}
1141b879a9c2SAndrey Smirnov 
1142b879a9c2SAndrey Smirnov 	return err;
1143b879a9c2SAndrey Smirnov }
1144b879a9c2SAndrey Smirnov 
1145b879a9c2SAndrey Smirnov static const struct v4l2_file_operations si476x_fops = {
1146b879a9c2SAndrey Smirnov 	.owner			= THIS_MODULE,
1147b879a9c2SAndrey Smirnov 	.read			= si476x_radio_fops_read,
1148b879a9c2SAndrey Smirnov 	.poll			= si476x_radio_fops_poll,
1149b879a9c2SAndrey Smirnov 	.unlocked_ioctl		= video_ioctl2,
1150b879a9c2SAndrey Smirnov 	.open			= si476x_radio_fops_open,
1151b879a9c2SAndrey Smirnov 	.release		= si476x_radio_fops_release,
1152b879a9c2SAndrey Smirnov };
1153b879a9c2SAndrey Smirnov 
1154b879a9c2SAndrey Smirnov 
1155b879a9c2SAndrey Smirnov static const struct v4l2_ioctl_ops si4761_ioctl_ops = {
1156b879a9c2SAndrey Smirnov 	.vidioc_querycap		= si476x_radio_querycap,
1157b879a9c2SAndrey Smirnov 	.vidioc_g_tuner			= si476x_radio_g_tuner,
1158b879a9c2SAndrey Smirnov 	.vidioc_s_tuner			= si476x_radio_s_tuner,
1159b879a9c2SAndrey Smirnov 
1160b879a9c2SAndrey Smirnov 	.vidioc_g_frequency		= si476x_radio_g_frequency,
1161b879a9c2SAndrey Smirnov 	.vidioc_s_frequency		= si476x_radio_s_frequency,
1162b879a9c2SAndrey Smirnov 	.vidioc_s_hw_freq_seek		= si476x_radio_s_hw_freq_seek,
1163b879a9c2SAndrey Smirnov 	.vidioc_enum_freq_bands		= si476x_radio_enum_freq_bands,
1164b879a9c2SAndrey Smirnov 
1165b879a9c2SAndrey Smirnov 	.vidioc_subscribe_event		= v4l2_ctrl_subscribe_event,
1166b879a9c2SAndrey Smirnov 	.vidioc_unsubscribe_event	= v4l2_event_unsubscribe,
1167b879a9c2SAndrey Smirnov 
1168b879a9c2SAndrey Smirnov #ifdef CONFIG_VIDEO_ADV_DEBUG
1169b879a9c2SAndrey Smirnov 	.vidioc_g_register		= si476x_radio_g_register,
1170b879a9c2SAndrey Smirnov 	.vidioc_s_register		= si476x_radio_s_register,
1171b879a9c2SAndrey Smirnov #endif
1172b879a9c2SAndrey Smirnov };
1173b879a9c2SAndrey Smirnov 
1174b879a9c2SAndrey Smirnov 
1175b879a9c2SAndrey Smirnov static const struct video_device si476x_viddev_template = {
1176b879a9c2SAndrey Smirnov 	.fops			= &si476x_fops,
1177b879a9c2SAndrey Smirnov 	.name			= DRIVER_NAME,
1178b879a9c2SAndrey Smirnov 	.release		= video_device_release_empty,
1179b879a9c2SAndrey Smirnov };
1180b879a9c2SAndrey Smirnov 
1181b879a9c2SAndrey Smirnov 
1182b879a9c2SAndrey Smirnov 
si476x_radio_read_acf_blob(struct file * file,char __user * user_buf,size_t count,loff_t * ppos)1183b879a9c2SAndrey Smirnov static ssize_t si476x_radio_read_acf_blob(struct file *file,
1184b879a9c2SAndrey Smirnov 					  char __user *user_buf,
1185b879a9c2SAndrey Smirnov 					  size_t count, loff_t *ppos)
1186b879a9c2SAndrey Smirnov {
1187b879a9c2SAndrey Smirnov 	int err;
1188b879a9c2SAndrey Smirnov 	struct si476x_radio *radio = file->private_data;
1189b879a9c2SAndrey Smirnov 	struct si476x_acf_status_report report;
1190b879a9c2SAndrey Smirnov 
1191b879a9c2SAndrey Smirnov 	si476x_core_lock(radio->core);
1192b879a9c2SAndrey Smirnov 	if (radio->ops->acf_status)
1193b879a9c2SAndrey Smirnov 		err = radio->ops->acf_status(radio->core, &report);
1194b879a9c2SAndrey Smirnov 	else
1195b879a9c2SAndrey Smirnov 		err = -ENOENT;
1196b879a9c2SAndrey Smirnov 	si476x_core_unlock(radio->core);
1197b879a9c2SAndrey Smirnov 
1198b879a9c2SAndrey Smirnov 	if (err < 0)
1199b879a9c2SAndrey Smirnov 		return err;
1200b879a9c2SAndrey Smirnov 
1201b879a9c2SAndrey Smirnov 	return simple_read_from_buffer(user_buf, count, ppos, &report,
1202b879a9c2SAndrey Smirnov 				       sizeof(report));
1203b879a9c2SAndrey Smirnov }
1204b879a9c2SAndrey Smirnov 
1205b879a9c2SAndrey Smirnov static const struct file_operations radio_acf_fops = {
1206b879a9c2SAndrey Smirnov 	.open	= simple_open,
1207b879a9c2SAndrey Smirnov 	.llseek = default_llseek,
1208b879a9c2SAndrey Smirnov 	.read	= si476x_radio_read_acf_blob,
1209b879a9c2SAndrey Smirnov };
1210b879a9c2SAndrey Smirnov 
si476x_radio_read_rds_blckcnt_blob(struct file * file,char __user * user_buf,size_t count,loff_t * ppos)1211b879a9c2SAndrey Smirnov static ssize_t si476x_radio_read_rds_blckcnt_blob(struct file *file,
1212b879a9c2SAndrey Smirnov 						  char __user *user_buf,
1213b879a9c2SAndrey Smirnov 						  size_t count, loff_t *ppos)
1214b879a9c2SAndrey Smirnov {
1215b879a9c2SAndrey Smirnov 	int err;
1216b879a9c2SAndrey Smirnov 	struct si476x_radio *radio = file->private_data;
1217b879a9c2SAndrey Smirnov 	struct si476x_rds_blockcount_report report;
1218b879a9c2SAndrey Smirnov 
1219b879a9c2SAndrey Smirnov 	si476x_core_lock(radio->core);
1220b879a9c2SAndrey Smirnov 	if (radio->ops->rds_blckcnt)
1221b879a9c2SAndrey Smirnov 		err = radio->ops->rds_blckcnt(radio->core, true,
1222b879a9c2SAndrey Smirnov 					       &report);
1223b879a9c2SAndrey Smirnov 	else
1224b879a9c2SAndrey Smirnov 		err = -ENOENT;
1225b879a9c2SAndrey Smirnov 	si476x_core_unlock(radio->core);
1226b879a9c2SAndrey Smirnov 
1227b879a9c2SAndrey Smirnov 	if (err < 0)
1228b879a9c2SAndrey Smirnov 		return err;
1229b879a9c2SAndrey Smirnov 
1230b879a9c2SAndrey Smirnov 	return simple_read_from_buffer(user_buf, count, ppos, &report,
1231b879a9c2SAndrey Smirnov 				       sizeof(report));
1232b879a9c2SAndrey Smirnov }
1233b879a9c2SAndrey Smirnov 
1234b879a9c2SAndrey Smirnov static const struct file_operations radio_rds_blckcnt_fops = {
1235b879a9c2SAndrey Smirnov 	.open	= simple_open,
1236b879a9c2SAndrey Smirnov 	.llseek = default_llseek,
1237b879a9c2SAndrey Smirnov 	.read	= si476x_radio_read_rds_blckcnt_blob,
1238b879a9c2SAndrey Smirnov };
1239b879a9c2SAndrey Smirnov 
si476x_radio_read_agc_blob(struct file * file,char __user * user_buf,size_t count,loff_t * ppos)1240b879a9c2SAndrey Smirnov static ssize_t si476x_radio_read_agc_blob(struct file *file,
1241b879a9c2SAndrey Smirnov 					  char __user *user_buf,
1242b879a9c2SAndrey Smirnov 					  size_t count, loff_t *ppos)
1243b879a9c2SAndrey Smirnov {
1244b879a9c2SAndrey Smirnov 	int err;
1245b879a9c2SAndrey Smirnov 	struct si476x_radio *radio = file->private_data;
1246b879a9c2SAndrey Smirnov 	struct si476x_agc_status_report report;
1247b879a9c2SAndrey Smirnov 
1248b879a9c2SAndrey Smirnov 	si476x_core_lock(radio->core);
1249b879a9c2SAndrey Smirnov 	if (radio->ops->rds_blckcnt)
1250b879a9c2SAndrey Smirnov 		err = radio->ops->agc_status(radio->core, &report);
1251b879a9c2SAndrey Smirnov 	else
1252b879a9c2SAndrey Smirnov 		err = -ENOENT;
1253b879a9c2SAndrey Smirnov 	si476x_core_unlock(radio->core);
1254b879a9c2SAndrey Smirnov 
1255b879a9c2SAndrey Smirnov 	if (err < 0)
1256b879a9c2SAndrey Smirnov 		return err;
1257b879a9c2SAndrey Smirnov 
1258b879a9c2SAndrey Smirnov 	return simple_read_from_buffer(user_buf, count, ppos, &report,
1259b879a9c2SAndrey Smirnov 				       sizeof(report));
1260b879a9c2SAndrey Smirnov }
1261b879a9c2SAndrey Smirnov 
1262b879a9c2SAndrey Smirnov static const struct file_operations radio_agc_fops = {
1263b879a9c2SAndrey Smirnov 	.open	= simple_open,
1264b879a9c2SAndrey Smirnov 	.llseek = default_llseek,
1265b879a9c2SAndrey Smirnov 	.read	= si476x_radio_read_agc_blob,
1266b879a9c2SAndrey Smirnov };
1267b879a9c2SAndrey Smirnov 
si476x_radio_read_rsq_blob(struct file * file,char __user * user_buf,size_t count,loff_t * ppos)1268b879a9c2SAndrey Smirnov static ssize_t si476x_radio_read_rsq_blob(struct file *file,
1269b879a9c2SAndrey Smirnov 					  char __user *user_buf,
1270b879a9c2SAndrey Smirnov 					  size_t count, loff_t *ppos)
1271b879a9c2SAndrey Smirnov {
1272b879a9c2SAndrey Smirnov 	int err;
1273b879a9c2SAndrey Smirnov 	struct si476x_radio *radio = file->private_data;
1274b879a9c2SAndrey Smirnov 	struct si476x_rsq_status_report report;
1275b879a9c2SAndrey Smirnov 	struct si476x_rsq_status_args args = {
1276b879a9c2SAndrey Smirnov 		.primary	= false,
1277b879a9c2SAndrey Smirnov 		.rsqack		= false,
1278b879a9c2SAndrey Smirnov 		.attune		= false,
1279b879a9c2SAndrey Smirnov 		.cancel		= false,
1280b879a9c2SAndrey Smirnov 		.stcack		= false,
1281b879a9c2SAndrey Smirnov 	};
1282b879a9c2SAndrey Smirnov 
1283b879a9c2SAndrey Smirnov 	si476x_core_lock(radio->core);
1284b879a9c2SAndrey Smirnov 	if (radio->ops->rds_blckcnt)
1285b879a9c2SAndrey Smirnov 		err = radio->ops->rsq_status(radio->core, &args, &report);
1286b879a9c2SAndrey Smirnov 	else
1287b879a9c2SAndrey Smirnov 		err = -ENOENT;
1288b879a9c2SAndrey Smirnov 	si476x_core_unlock(radio->core);
1289b879a9c2SAndrey Smirnov 
1290b879a9c2SAndrey Smirnov 	if (err < 0)
1291b879a9c2SAndrey Smirnov 		return err;
1292b879a9c2SAndrey Smirnov 
1293b879a9c2SAndrey Smirnov 	return simple_read_from_buffer(user_buf, count, ppos, &report,
1294b879a9c2SAndrey Smirnov 				       sizeof(report));
1295b879a9c2SAndrey Smirnov }
1296b879a9c2SAndrey Smirnov 
1297b879a9c2SAndrey Smirnov static const struct file_operations radio_rsq_fops = {
1298b879a9c2SAndrey Smirnov 	.open	= simple_open,
1299b879a9c2SAndrey Smirnov 	.llseek = default_llseek,
1300b879a9c2SAndrey Smirnov 	.read	= si476x_radio_read_rsq_blob,
1301b879a9c2SAndrey Smirnov };
1302b879a9c2SAndrey Smirnov 
si476x_radio_read_rsq_primary_blob(struct file * file,char __user * user_buf,size_t count,loff_t * ppos)1303b879a9c2SAndrey Smirnov static ssize_t si476x_radio_read_rsq_primary_blob(struct file *file,
1304b879a9c2SAndrey Smirnov 						  char __user *user_buf,
1305b879a9c2SAndrey Smirnov 						  size_t count, loff_t *ppos)
1306b879a9c2SAndrey Smirnov {
1307b879a9c2SAndrey Smirnov 	int err;
1308b879a9c2SAndrey Smirnov 	struct si476x_radio *radio = file->private_data;
1309b879a9c2SAndrey Smirnov 	struct si476x_rsq_status_report report;
1310b879a9c2SAndrey Smirnov 	struct si476x_rsq_status_args args = {
1311b879a9c2SAndrey Smirnov 		.primary	= true,
1312b879a9c2SAndrey Smirnov 		.rsqack		= false,
1313b879a9c2SAndrey Smirnov 		.attune		= false,
1314b879a9c2SAndrey Smirnov 		.cancel		= false,
1315b879a9c2SAndrey Smirnov 		.stcack		= false,
1316b879a9c2SAndrey Smirnov 	};
1317b879a9c2SAndrey Smirnov 
1318b879a9c2SAndrey Smirnov 	si476x_core_lock(radio->core);
1319b879a9c2SAndrey Smirnov 	if (radio->ops->rds_blckcnt)
1320b879a9c2SAndrey Smirnov 		err = radio->ops->rsq_status(radio->core, &args, &report);
1321b879a9c2SAndrey Smirnov 	else
1322b879a9c2SAndrey Smirnov 		err = -ENOENT;
1323b879a9c2SAndrey Smirnov 	si476x_core_unlock(radio->core);
1324b879a9c2SAndrey Smirnov 
1325b879a9c2SAndrey Smirnov 	if (err < 0)
1326b879a9c2SAndrey Smirnov 		return err;
1327b879a9c2SAndrey Smirnov 
1328b879a9c2SAndrey Smirnov 	return simple_read_from_buffer(user_buf, count, ppos, &report,
1329b879a9c2SAndrey Smirnov 				       sizeof(report));
1330b879a9c2SAndrey Smirnov }
1331b879a9c2SAndrey Smirnov 
1332b879a9c2SAndrey Smirnov static const struct file_operations radio_rsq_primary_fops = {
1333b879a9c2SAndrey Smirnov 	.open	= simple_open,
1334b879a9c2SAndrey Smirnov 	.llseek = default_llseek,
1335b879a9c2SAndrey Smirnov 	.read	= si476x_radio_read_rsq_primary_blob,
1336b879a9c2SAndrey Smirnov };
1337b879a9c2SAndrey Smirnov 
1338b879a9c2SAndrey Smirnov 
si476x_radio_init_debugfs(struct si476x_radio * radio)13391d8f95c4SGreg Kroah-Hartman static void si476x_radio_init_debugfs(struct si476x_radio *radio)
1340b879a9c2SAndrey Smirnov {
13411d8f95c4SGreg Kroah-Hartman 	radio->debugfs = debugfs_create_dir(dev_name(radio->v4l2dev.dev), NULL);
1342b879a9c2SAndrey Smirnov 
13431d8f95c4SGreg Kroah-Hartman 	debugfs_create_file("acf", S_IRUGO, radio->debugfs, radio,
13441d8f95c4SGreg Kroah-Hartman 			    &radio_acf_fops);
1345b879a9c2SAndrey Smirnov 
13461d8f95c4SGreg Kroah-Hartman 	debugfs_create_file("rds_blckcnt", S_IRUGO, radio->debugfs, radio,
1347b879a9c2SAndrey Smirnov 			    &radio_rds_blckcnt_fops);
1348b879a9c2SAndrey Smirnov 
13491d8f95c4SGreg Kroah-Hartman 	debugfs_create_file("agc", S_IRUGO, radio->debugfs, radio,
13501d8f95c4SGreg Kroah-Hartman 			    &radio_agc_fops);
1351b879a9c2SAndrey Smirnov 
13521d8f95c4SGreg Kroah-Hartman 	debugfs_create_file("rsq", S_IRUGO, radio->debugfs, radio,
13531d8f95c4SGreg Kroah-Hartman 			    &radio_rsq_fops);
1354b879a9c2SAndrey Smirnov 
13551d8f95c4SGreg Kroah-Hartman 	debugfs_create_file("rsq_primary", S_IRUGO, radio->debugfs, radio,
1356b879a9c2SAndrey Smirnov 			    &radio_rsq_primary_fops);
1357b879a9c2SAndrey Smirnov }
1358b879a9c2SAndrey Smirnov 
1359b879a9c2SAndrey Smirnov 
si476x_radio_add_new_custom(struct si476x_radio * radio,enum si476x_ctrl_idx idx)1360b879a9c2SAndrey Smirnov static int si476x_radio_add_new_custom(struct si476x_radio *radio,
1361b879a9c2SAndrey Smirnov 				       enum si476x_ctrl_idx idx)
1362b879a9c2SAndrey Smirnov {
1363b879a9c2SAndrey Smirnov 	int rval;
1364b879a9c2SAndrey Smirnov 	struct v4l2_ctrl *ctrl;
1365b879a9c2SAndrey Smirnov 
1366b879a9c2SAndrey Smirnov 	ctrl = v4l2_ctrl_new_custom(&radio->ctrl_handler,
1367b879a9c2SAndrey Smirnov 				    &si476x_ctrls[idx],
1368b879a9c2SAndrey Smirnov 				    NULL);
1369b879a9c2SAndrey Smirnov 	rval = radio->ctrl_handler.error;
1370b879a9c2SAndrey Smirnov 	if (ctrl == NULL && rval)
1371b879a9c2SAndrey Smirnov 		dev_err(radio->v4l2dev.dev,
1372b879a9c2SAndrey Smirnov 			"Could not initialize '%s' control %d\n",
1373b879a9c2SAndrey Smirnov 			si476x_ctrls[idx].name, rval);
1374b879a9c2SAndrey Smirnov 
1375b879a9c2SAndrey Smirnov 	return rval;
1376b879a9c2SAndrey Smirnov }
1377b879a9c2SAndrey Smirnov 
si476x_radio_probe(struct platform_device * pdev)1378b879a9c2SAndrey Smirnov static int si476x_radio_probe(struct platform_device *pdev)
1379b879a9c2SAndrey Smirnov {
1380b879a9c2SAndrey Smirnov 	int rval;
1381b879a9c2SAndrey Smirnov 	struct si476x_radio *radio;
1382b879a9c2SAndrey Smirnov 	struct v4l2_ctrl *ctrl;
1383b879a9c2SAndrey Smirnov 
1384b879a9c2SAndrey Smirnov 	static atomic_t instance = ATOMIC_INIT(0);
1385b879a9c2SAndrey Smirnov 
1386b879a9c2SAndrey Smirnov 	radio = devm_kzalloc(&pdev->dev, sizeof(*radio), GFP_KERNEL);
1387b879a9c2SAndrey Smirnov 	if (!radio)
1388b879a9c2SAndrey Smirnov 		return -ENOMEM;
1389b879a9c2SAndrey Smirnov 
1390b879a9c2SAndrey Smirnov 	radio->core = i2c_mfd_cell_to_core(&pdev->dev);
1391b879a9c2SAndrey Smirnov 
1392b879a9c2SAndrey Smirnov 	v4l2_device_set_name(&radio->v4l2dev, DRIVER_NAME, &instance);
1393b879a9c2SAndrey Smirnov 
1394b879a9c2SAndrey Smirnov 	rval = v4l2_device_register(&pdev->dev, &radio->v4l2dev);
1395b879a9c2SAndrey Smirnov 	if (rval) {
1396b879a9c2SAndrey Smirnov 		dev_err(&pdev->dev, "Cannot register v4l2_device.\n");
1397b879a9c2SAndrey Smirnov 		return rval;
1398b879a9c2SAndrey Smirnov 	}
1399b879a9c2SAndrey Smirnov 
1400b879a9c2SAndrey Smirnov 	memcpy(&radio->videodev, &si476x_viddev_template,
1401b879a9c2SAndrey Smirnov 	       sizeof(struct video_device));
1402b879a9c2SAndrey Smirnov 
1403b879a9c2SAndrey Smirnov 	radio->videodev.v4l2_dev  = &radio->v4l2dev;
1404b879a9c2SAndrey Smirnov 	radio->videodev.ioctl_ops = &si4761_ioctl_ops;
1405e83ce300SHans Verkuil 	radio->videodev.device_caps = V4L2_CAP_TUNER | V4L2_CAP_RADIO |
1406e83ce300SHans Verkuil 				      V4L2_CAP_HW_FREQ_SEEK;
1407e83ce300SHans Verkuil 
1408e83ce300SHans Verkuil 	si476x_core_lock(radio->core);
1409e83ce300SHans Verkuil 	if (!si476x_core_is_a_secondary_tuner(radio->core))
1410e83ce300SHans Verkuil 		radio->videodev.device_caps |= V4L2_CAP_RDS_CAPTURE |
1411e83ce300SHans Verkuil 					       V4L2_CAP_READWRITE;
1412e83ce300SHans Verkuil 	si476x_core_unlock(radio->core);
1413b879a9c2SAndrey Smirnov 
1414b879a9c2SAndrey Smirnov 	video_set_drvdata(&radio->videodev, radio);
1415b879a9c2SAndrey Smirnov 	platform_set_drvdata(pdev, radio);
1416b879a9c2SAndrey Smirnov 
1417b879a9c2SAndrey Smirnov 
1418b879a9c2SAndrey Smirnov 	radio->v4l2dev.ctrl_handler = &radio->ctrl_handler;
1419b879a9c2SAndrey Smirnov 	v4l2_ctrl_handler_init(&radio->ctrl_handler,
1420b879a9c2SAndrey Smirnov 			       1 + ARRAY_SIZE(si476x_ctrls));
1421b879a9c2SAndrey Smirnov 
1422b879a9c2SAndrey Smirnov 	if (si476x_core_has_am(radio->core)) {
1423b879a9c2SAndrey Smirnov 		ctrl = v4l2_ctrl_new_std_menu(&radio->ctrl_handler,
1424b879a9c2SAndrey Smirnov 					      &si476x_ctrl_ops,
1425b879a9c2SAndrey Smirnov 					      V4L2_CID_POWER_LINE_FREQUENCY,
1426b879a9c2SAndrey Smirnov 					      V4L2_CID_POWER_LINE_FREQUENCY_60HZ,
1427b879a9c2SAndrey Smirnov 					      0, 0);
1428b879a9c2SAndrey Smirnov 		rval = radio->ctrl_handler.error;
1429b879a9c2SAndrey Smirnov 		if (ctrl == NULL && rval) {
1430b879a9c2SAndrey Smirnov 			dev_err(&pdev->dev, "Could not initialize V4L2_CID_POWER_LINE_FREQUENCY control %d\n",
1431b879a9c2SAndrey Smirnov 				rval);
1432b879a9c2SAndrey Smirnov 			goto exit;
1433b879a9c2SAndrey Smirnov 		}
1434b879a9c2SAndrey Smirnov 
1435b879a9c2SAndrey Smirnov 		rval = si476x_radio_add_new_custom(radio,
1436b879a9c2SAndrey Smirnov 						   SI476X_IDX_HARMONICS_COUNT);
1437b879a9c2SAndrey Smirnov 		if (rval < 0)
1438b879a9c2SAndrey Smirnov 			goto exit;
1439b879a9c2SAndrey Smirnov 	}
1440b879a9c2SAndrey Smirnov 
1441b879a9c2SAndrey Smirnov 	rval = si476x_radio_add_new_custom(radio, SI476X_IDX_RSSI_THRESHOLD);
1442b879a9c2SAndrey Smirnov 	if (rval < 0)
1443b879a9c2SAndrey Smirnov 		goto exit;
1444b879a9c2SAndrey Smirnov 
1445b879a9c2SAndrey Smirnov 	rval = si476x_radio_add_new_custom(radio, SI476X_IDX_SNR_THRESHOLD);
1446b879a9c2SAndrey Smirnov 	if (rval < 0)
1447b879a9c2SAndrey Smirnov 		goto exit;
1448b879a9c2SAndrey Smirnov 
1449b879a9c2SAndrey Smirnov 	rval = si476x_radio_add_new_custom(radio, SI476X_IDX_MAX_TUNE_ERROR);
1450b879a9c2SAndrey Smirnov 	if (rval < 0)
1451b879a9c2SAndrey Smirnov 		goto exit;
1452b879a9c2SAndrey Smirnov 
1453b879a9c2SAndrey Smirnov 	ctrl = v4l2_ctrl_new_std_menu(&radio->ctrl_handler,
1454b879a9c2SAndrey Smirnov 				      &si476x_ctrl_ops,
1455b879a9c2SAndrey Smirnov 				      V4L2_CID_TUNE_DEEMPHASIS,
1456b879a9c2SAndrey Smirnov 				      V4L2_DEEMPHASIS_75_uS, 0, 0);
1457b879a9c2SAndrey Smirnov 	rval = radio->ctrl_handler.error;
1458b879a9c2SAndrey Smirnov 	if (ctrl == NULL && rval) {
1459b879a9c2SAndrey Smirnov 		dev_err(&pdev->dev, "Could not initialize V4L2_CID_TUNE_DEEMPHASIS control %d\n",
1460b879a9c2SAndrey Smirnov 			rval);
1461b879a9c2SAndrey Smirnov 		goto exit;
1462b879a9c2SAndrey Smirnov 	}
1463b879a9c2SAndrey Smirnov 
1464b879a9c2SAndrey Smirnov 	ctrl = v4l2_ctrl_new_std(&radio->ctrl_handler, &si476x_ctrl_ops,
1465b879a9c2SAndrey Smirnov 				 V4L2_CID_RDS_RECEPTION,
1466b879a9c2SAndrey Smirnov 				 0, 1, 1, 1);
1467b879a9c2SAndrey Smirnov 	rval = radio->ctrl_handler.error;
1468b879a9c2SAndrey Smirnov 	if (ctrl == NULL && rval) {
1469b879a9c2SAndrey Smirnov 		dev_err(&pdev->dev, "Could not initialize V4L2_CID_RDS_RECEPTION control %d\n",
1470b879a9c2SAndrey Smirnov 			rval);
1471b879a9c2SAndrey Smirnov 		goto exit;
1472b879a9c2SAndrey Smirnov 	}
1473b879a9c2SAndrey Smirnov 
1474b879a9c2SAndrey Smirnov 	if (si476x_core_has_diversity(radio->core)) {
1475b879a9c2SAndrey Smirnov 		si476x_ctrls[SI476X_IDX_DIVERSITY_MODE].def =
1476b879a9c2SAndrey Smirnov 			si476x_phase_diversity_mode_to_idx(radio->core->diversity_mode);
1477949cf706SFugang Duan 		rval = si476x_radio_add_new_custom(radio, SI476X_IDX_DIVERSITY_MODE);
1478b879a9c2SAndrey Smirnov 		if (rval < 0)
1479b879a9c2SAndrey Smirnov 			goto exit;
1480b879a9c2SAndrey Smirnov 
1481949cf706SFugang Duan 		rval = si476x_radio_add_new_custom(radio, SI476X_IDX_INTERCHIP_LINK);
1482b879a9c2SAndrey Smirnov 		if (rval < 0)
1483b879a9c2SAndrey Smirnov 			goto exit;
1484b879a9c2SAndrey Smirnov 	}
1485b879a9c2SAndrey Smirnov 
1486b879a9c2SAndrey Smirnov 	/* register video device */
1487b879a9c2SAndrey Smirnov 	rval = video_register_device(&radio->videodev, VFL_TYPE_RADIO, -1);
1488b879a9c2SAndrey Smirnov 	if (rval < 0) {
1489b879a9c2SAndrey Smirnov 		dev_err(&pdev->dev, "Could not register video device\n");
1490b879a9c2SAndrey Smirnov 		goto exit;
1491b879a9c2SAndrey Smirnov 	}
1492b879a9c2SAndrey Smirnov 
14931d8f95c4SGreg Kroah-Hartman 	si476x_radio_init_debugfs(radio);
1494b879a9c2SAndrey Smirnov 
1495b879a9c2SAndrey Smirnov 	return 0;
1496b879a9c2SAndrey Smirnov exit:
1497b879a9c2SAndrey Smirnov 	v4l2_ctrl_handler_free(radio->videodev.ctrl_handler);
1498b879a9c2SAndrey Smirnov 	return rval;
1499b879a9c2SAndrey Smirnov }
1500b879a9c2SAndrey Smirnov 
si476x_radio_remove(struct platform_device * pdev)1501*830d1151SUwe Kleine-König static void si476x_radio_remove(struct platform_device *pdev)
1502b879a9c2SAndrey Smirnov {
1503b879a9c2SAndrey Smirnov 	struct si476x_radio *radio = platform_get_drvdata(pdev);
1504b879a9c2SAndrey Smirnov 
1505b879a9c2SAndrey Smirnov 	v4l2_ctrl_handler_free(radio->videodev.ctrl_handler);
1506b879a9c2SAndrey Smirnov 	video_unregister_device(&radio->videodev);
1507b879a9c2SAndrey Smirnov 	v4l2_device_unregister(&radio->v4l2dev);
1508b879a9c2SAndrey Smirnov 	debugfs_remove_recursive(radio->debugfs);
1509b879a9c2SAndrey Smirnov }
1510b879a9c2SAndrey Smirnov 
1511b879a9c2SAndrey Smirnov MODULE_ALIAS("platform:si476x-radio");
1512b879a9c2SAndrey Smirnov 
1513b879a9c2SAndrey Smirnov static struct platform_driver si476x_radio_driver = {
1514b879a9c2SAndrey Smirnov 	.driver		= {
1515b879a9c2SAndrey Smirnov 		.name	= DRIVER_NAME,
1516b879a9c2SAndrey Smirnov 	},
1517b879a9c2SAndrey Smirnov 	.probe		= si476x_radio_probe,
1518*830d1151SUwe Kleine-König 	.remove_new	= si476x_radio_remove,
1519b879a9c2SAndrey Smirnov };
1520b879a9c2SAndrey Smirnov module_platform_driver(si476x_radio_driver);
1521b879a9c2SAndrey Smirnov 
1522b879a9c2SAndrey Smirnov MODULE_AUTHOR("Andrey Smirnov <andrew.smirnov@gmail.com>");
1523b879a9c2SAndrey Smirnov MODULE_DESCRIPTION("Driver for Si4761/64/68 AM/FM Radio MFD Cell");
1524b879a9c2SAndrey Smirnov MODULE_LICENSE("GPL");
1525