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 
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 
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
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
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
155*30bcc510SXiaofeng 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
258*30bcc510SXiaofeng 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 *
316b879a9c2SAndrey Smirnov v4l2_dev_to_radio(struct v4l2_device *d)
317b879a9c2SAndrey Smirnov {
318b879a9c2SAndrey Smirnov 	return container_of(d, struct si476x_radio, v4l2dev);
319b879a9c2SAndrey Smirnov }
320b879a9c2SAndrey Smirnov 
321b879a9c2SAndrey Smirnov static inline struct si476x_radio *
322b879a9c2SAndrey Smirnov v4l2_ctrl_handler_to_radio(struct v4l2_ctrl_handler *d)
323b879a9c2SAndrey Smirnov {
324b879a9c2SAndrey Smirnov 	return container_of(d, struct si476x_radio, ctrl_handler);
325b879a9c2SAndrey Smirnov }
326b879a9c2SAndrey Smirnov 
327b879a9c2SAndrey Smirnov /*
328b879a9c2SAndrey Smirnov  * si476x_vidioc_querycap - query device capabilities
329b879a9c2SAndrey Smirnov  */
330b879a9c2SAndrey Smirnov static int si476x_radio_querycap(struct file *file, void *priv,
331b879a9c2SAndrey Smirnov 				 struct v4l2_capability *capability)
332b879a9c2SAndrey Smirnov {
333b879a9c2SAndrey Smirnov 	struct si476x_radio *radio = video_drvdata(file);
334b879a9c2SAndrey Smirnov 
335c0decac1SMauro Carvalho Chehab 	strscpy(capability->driver, radio->v4l2dev.name,
336b879a9c2SAndrey Smirnov 		sizeof(capability->driver));
337c0decac1SMauro Carvalho Chehab 	strscpy(capability->card,   DRIVER_CARD, sizeof(capability->card));
338b879a9c2SAndrey Smirnov 	snprintf(capability->bus_info, sizeof(capability->bus_info),
339b879a9c2SAndrey Smirnov 		 "platform:%s", radio->v4l2dev.name);
340b879a9c2SAndrey Smirnov 	return 0;
341b879a9c2SAndrey Smirnov }
342b879a9c2SAndrey Smirnov 
343b879a9c2SAndrey Smirnov static int si476x_radio_enum_freq_bands(struct file *file, void *priv,
344b879a9c2SAndrey Smirnov 					struct v4l2_frequency_band *band)
345b879a9c2SAndrey Smirnov {
346b879a9c2SAndrey Smirnov 	int err;
347b879a9c2SAndrey Smirnov 	struct si476x_radio *radio = video_drvdata(file);
348b879a9c2SAndrey Smirnov 
349b879a9c2SAndrey Smirnov 	if (band->tuner != 0)
350b879a9c2SAndrey Smirnov 		return -EINVAL;
351b879a9c2SAndrey Smirnov 
352b879a9c2SAndrey Smirnov 	switch (radio->core->chip_id) {
353b879a9c2SAndrey Smirnov 		/* AM/FM tuners -- all bands are supported */
354b879a9c2SAndrey Smirnov 	case SI476X_CHIP_SI4761:
355b879a9c2SAndrey Smirnov 	case SI476X_CHIP_SI4764:
356b879a9c2SAndrey Smirnov 		if (band->index < ARRAY_SIZE(si476x_bands)) {
357b879a9c2SAndrey Smirnov 			*band = si476x_bands[band->index];
358b879a9c2SAndrey Smirnov 			err = 0;
359b879a9c2SAndrey Smirnov 		} else {
360b879a9c2SAndrey Smirnov 			err = -EINVAL;
361b879a9c2SAndrey Smirnov 		}
362b879a9c2SAndrey Smirnov 		break;
363b879a9c2SAndrey Smirnov 		/* FM companion tuner chips -- only FM bands are
364b879a9c2SAndrey Smirnov 		 * supported */
365b879a9c2SAndrey Smirnov 	case SI476X_CHIP_SI4768:
366b879a9c2SAndrey Smirnov 		if (band->index == SI476X_BAND_FM) {
367b879a9c2SAndrey Smirnov 			*band = si476x_bands[band->index];
368b879a9c2SAndrey Smirnov 			err = 0;
369b879a9c2SAndrey Smirnov 		} else {
370b879a9c2SAndrey Smirnov 			err = -EINVAL;
371b879a9c2SAndrey Smirnov 		}
372b879a9c2SAndrey Smirnov 		break;
373b879a9c2SAndrey Smirnov 	default:
374b879a9c2SAndrey Smirnov 		err = -EINVAL;
375b879a9c2SAndrey Smirnov 	}
376b879a9c2SAndrey Smirnov 
377b879a9c2SAndrey Smirnov 	return err;
378b879a9c2SAndrey Smirnov }
379b879a9c2SAndrey Smirnov 
380b879a9c2SAndrey Smirnov static int si476x_radio_g_tuner(struct file *file, void *priv,
381b879a9c2SAndrey Smirnov 				struct v4l2_tuner *tuner)
382b879a9c2SAndrey Smirnov {
383b879a9c2SAndrey Smirnov 	int err;
384b879a9c2SAndrey Smirnov 	struct si476x_rsq_status_report report;
385b879a9c2SAndrey Smirnov 	struct si476x_radio *radio = video_drvdata(file);
386b879a9c2SAndrey Smirnov 
387b879a9c2SAndrey Smirnov 	struct si476x_rsq_status_args args = {
388b879a9c2SAndrey Smirnov 		.primary	= false,
389b879a9c2SAndrey Smirnov 		.rsqack		= false,
390b879a9c2SAndrey Smirnov 		.attune		= false,
391b879a9c2SAndrey Smirnov 		.cancel		= false,
392b879a9c2SAndrey Smirnov 		.stcack		= false,
393b879a9c2SAndrey Smirnov 	};
394b879a9c2SAndrey Smirnov 
395b879a9c2SAndrey Smirnov 	if (tuner->index != 0)
396b879a9c2SAndrey Smirnov 		return -EINVAL;
397b879a9c2SAndrey Smirnov 
398b879a9c2SAndrey Smirnov 	tuner->type       = V4L2_TUNER_RADIO;
399b879a9c2SAndrey Smirnov 	tuner->capability = V4L2_TUNER_CAP_LOW /* Measure frequencies
400b879a9c2SAndrey Smirnov 						 * in multiples of
401b879a9c2SAndrey Smirnov 						 * 62.5 Hz */
402b879a9c2SAndrey Smirnov 		| V4L2_TUNER_CAP_STEREO
403b879a9c2SAndrey Smirnov 		| V4L2_TUNER_CAP_HWSEEK_BOUNDED
404b879a9c2SAndrey Smirnov 		| V4L2_TUNER_CAP_HWSEEK_WRAP
405b879a9c2SAndrey Smirnov 		| V4L2_TUNER_CAP_HWSEEK_PROG_LIM;
406b879a9c2SAndrey Smirnov 
407b879a9c2SAndrey Smirnov 	si476x_core_lock(radio->core);
408b879a9c2SAndrey Smirnov 
409b879a9c2SAndrey Smirnov 	if (si476x_core_is_a_secondary_tuner(radio->core)) {
410c0decac1SMauro Carvalho Chehab 		strscpy(tuner->name, "FM (secondary)", sizeof(tuner->name));
411b879a9c2SAndrey Smirnov 		tuner->rxsubchans = 0;
412b879a9c2SAndrey Smirnov 		tuner->rangelow = si476x_bands[SI476X_BAND_FM].rangelow;
413b879a9c2SAndrey Smirnov 	} else if (si476x_core_has_am(radio->core)) {
414b879a9c2SAndrey Smirnov 		if (si476x_core_is_a_primary_tuner(radio->core))
415c0decac1SMauro Carvalho Chehab 			strscpy(tuner->name, "AM/FM (primary)",
416b879a9c2SAndrey Smirnov 				sizeof(tuner->name));
417b879a9c2SAndrey Smirnov 		else
418c0decac1SMauro Carvalho Chehab 			strscpy(tuner->name, "AM/FM", sizeof(tuner->name));
419b879a9c2SAndrey Smirnov 
420b879a9c2SAndrey Smirnov 		tuner->rxsubchans = V4L2_TUNER_SUB_MONO | V4L2_TUNER_SUB_STEREO
421b879a9c2SAndrey Smirnov 			| V4L2_TUNER_SUB_RDS;
422b879a9c2SAndrey Smirnov 		tuner->capability |= V4L2_TUNER_CAP_RDS
423b879a9c2SAndrey Smirnov 			| V4L2_TUNER_CAP_RDS_BLOCK_IO
424b879a9c2SAndrey Smirnov 			| V4L2_TUNER_CAP_FREQ_BANDS;
425b879a9c2SAndrey Smirnov 
426b879a9c2SAndrey Smirnov 		tuner->rangelow = si476x_bands[SI476X_BAND_AM].rangelow;
427b879a9c2SAndrey Smirnov 	} else {
428c0decac1SMauro Carvalho Chehab 		strscpy(tuner->name, "FM", sizeof(tuner->name));
429b879a9c2SAndrey Smirnov 		tuner->rxsubchans = V4L2_TUNER_SUB_RDS;
430b879a9c2SAndrey Smirnov 		tuner->capability |= V4L2_TUNER_CAP_RDS
431b879a9c2SAndrey Smirnov 			| V4L2_TUNER_CAP_RDS_BLOCK_IO
432b879a9c2SAndrey Smirnov 			| V4L2_TUNER_CAP_FREQ_BANDS;
433b879a9c2SAndrey Smirnov 		tuner->rangelow = si476x_bands[SI476X_BAND_FM].rangelow;
434b879a9c2SAndrey Smirnov 	}
435b879a9c2SAndrey Smirnov 
436b879a9c2SAndrey Smirnov 	tuner->audmode = radio->audmode;
437b879a9c2SAndrey Smirnov 
438b879a9c2SAndrey Smirnov 	tuner->afc = 1;
439b879a9c2SAndrey Smirnov 	tuner->rangehigh = si476x_bands[SI476X_BAND_FM].rangehigh;
440b879a9c2SAndrey Smirnov 
441b879a9c2SAndrey Smirnov 	err = radio->ops->rsq_status(radio->core,
442b879a9c2SAndrey Smirnov 				     &args, &report);
443b879a9c2SAndrey Smirnov 	if (err < 0) {
444b879a9c2SAndrey Smirnov 		tuner->signal = 0;
445b879a9c2SAndrey Smirnov 	} else {
446b879a9c2SAndrey Smirnov 		/*
447b879a9c2SAndrey Smirnov 		 * tuner->signal value range: 0x0000 .. 0xFFFF,
448b879a9c2SAndrey Smirnov 		 * report.rssi: -128 .. 127
449b879a9c2SAndrey Smirnov 		 */
450b879a9c2SAndrey Smirnov 		tuner->signal = (report.rssi + 128) * 257;
451b879a9c2SAndrey Smirnov 	}
452b879a9c2SAndrey Smirnov 	si476x_core_unlock(radio->core);
453b879a9c2SAndrey Smirnov 
454b879a9c2SAndrey Smirnov 	return err;
455b879a9c2SAndrey Smirnov }
456b879a9c2SAndrey Smirnov 
457b879a9c2SAndrey Smirnov static int si476x_radio_s_tuner(struct file *file, void *priv,
458b879a9c2SAndrey Smirnov 				const struct v4l2_tuner *tuner)
459b879a9c2SAndrey Smirnov {
460b879a9c2SAndrey Smirnov 	struct si476x_radio *radio = video_drvdata(file);
461b879a9c2SAndrey Smirnov 
462b879a9c2SAndrey Smirnov 	if (tuner->index != 0)
463b879a9c2SAndrey Smirnov 		return -EINVAL;
464b879a9c2SAndrey Smirnov 
465b879a9c2SAndrey Smirnov 	if (tuner->audmode == V4L2_TUNER_MODE_MONO ||
466b879a9c2SAndrey Smirnov 	    tuner->audmode == V4L2_TUNER_MODE_STEREO)
467b879a9c2SAndrey Smirnov 		radio->audmode = tuner->audmode;
468b879a9c2SAndrey Smirnov 	else
469b879a9c2SAndrey Smirnov 		radio->audmode = V4L2_TUNER_MODE_STEREO;
470b879a9c2SAndrey Smirnov 
471b879a9c2SAndrey Smirnov 	return 0;
472b879a9c2SAndrey Smirnov }
473b879a9c2SAndrey Smirnov 
474b879a9c2SAndrey Smirnov static int si476x_radio_init_vtable(struct si476x_radio *radio,
475b879a9c2SAndrey Smirnov 				    enum si476x_func func)
476b879a9c2SAndrey Smirnov {
477b879a9c2SAndrey Smirnov 	static const struct si476x_radio_ops fm_ops = {
478b879a9c2SAndrey Smirnov 		.tune_freq		= si476x_core_cmd_fm_tune_freq,
479b879a9c2SAndrey Smirnov 		.seek_start		= si476x_core_cmd_fm_seek_start,
480b879a9c2SAndrey Smirnov 		.rsq_status		= si476x_core_cmd_fm_rsq_status,
481b879a9c2SAndrey Smirnov 		.rds_blckcnt		= si476x_core_cmd_fm_rds_blockcount,
482b879a9c2SAndrey Smirnov 		.phase_diversity	= si476x_core_cmd_fm_phase_diversity,
483b879a9c2SAndrey Smirnov 		.phase_div_status	= si476x_core_cmd_fm_phase_div_status,
484b879a9c2SAndrey Smirnov 		.acf_status		= si476x_core_cmd_fm_acf_status,
485b879a9c2SAndrey Smirnov 		.agc_status		= si476x_core_cmd_agc_status,
486b879a9c2SAndrey Smirnov 	};
487b879a9c2SAndrey Smirnov 
488b879a9c2SAndrey Smirnov 	static const struct si476x_radio_ops am_ops = {
489b879a9c2SAndrey Smirnov 		.tune_freq		= si476x_core_cmd_am_tune_freq,
490b879a9c2SAndrey Smirnov 		.seek_start		= si476x_core_cmd_am_seek_start,
491b879a9c2SAndrey Smirnov 		.rsq_status		= si476x_core_cmd_am_rsq_status,
492b879a9c2SAndrey Smirnov 		.rds_blckcnt		= NULL,
493b879a9c2SAndrey Smirnov 		.phase_diversity	= NULL,
494b879a9c2SAndrey Smirnov 		.phase_div_status	= NULL,
495b879a9c2SAndrey Smirnov 		.acf_status		= si476x_core_cmd_am_acf_status,
496b879a9c2SAndrey Smirnov 		.agc_status		= NULL,
497b879a9c2SAndrey Smirnov 	};
498b879a9c2SAndrey Smirnov 
499b879a9c2SAndrey Smirnov 	switch (func) {
500b879a9c2SAndrey Smirnov 	case SI476X_FUNC_FM_RECEIVER:
501b879a9c2SAndrey Smirnov 		radio->ops = &fm_ops;
502b879a9c2SAndrey Smirnov 		return 0;
503b879a9c2SAndrey Smirnov 
504b879a9c2SAndrey Smirnov 	case SI476X_FUNC_AM_RECEIVER:
505b879a9c2SAndrey Smirnov 		radio->ops = &am_ops;
506b879a9c2SAndrey Smirnov 		return 0;
507b879a9c2SAndrey Smirnov 	default:
508b879a9c2SAndrey Smirnov 		WARN(1, "Unexpected tuner function value\n");
509b879a9c2SAndrey Smirnov 		return -EINVAL;
510b879a9c2SAndrey Smirnov 	}
511b879a9c2SAndrey Smirnov }
512b879a9c2SAndrey Smirnov 
513b879a9c2SAndrey Smirnov static int si476x_radio_pretune(struct si476x_radio *radio,
514b879a9c2SAndrey Smirnov 				enum si476x_func func)
515b879a9c2SAndrey Smirnov {
516b879a9c2SAndrey Smirnov 	int retval;
517b879a9c2SAndrey Smirnov 
518b879a9c2SAndrey Smirnov 	struct si476x_tune_freq_args args = {
519b879a9c2SAndrey Smirnov 		.zifsr		= false,
520b879a9c2SAndrey Smirnov 		.hd		= false,
521b879a9c2SAndrey Smirnov 		.injside	= SI476X_INJSIDE_AUTO,
522b879a9c2SAndrey Smirnov 		.tunemode	= SI476X_TM_VALIDATED_NORMAL_TUNE,
523b879a9c2SAndrey Smirnov 		.smoothmetrics	= SI476X_SM_INITIALIZE_AUDIO,
524b879a9c2SAndrey Smirnov 		.antcap		= 0,
525b879a9c2SAndrey Smirnov 	};
526b879a9c2SAndrey Smirnov 
527b879a9c2SAndrey Smirnov 	switch (func) {
528b879a9c2SAndrey Smirnov 	case SI476X_FUNC_FM_RECEIVER:
529b879a9c2SAndrey Smirnov 		args.freq = v4l2_to_si476x(radio->core,
530b879a9c2SAndrey Smirnov 					   92 * FREQ_MUL);
531b879a9c2SAndrey Smirnov 		retval = radio->ops->tune_freq(radio->core, &args);
532b879a9c2SAndrey Smirnov 		break;
533b879a9c2SAndrey Smirnov 	case SI476X_FUNC_AM_RECEIVER:
534b879a9c2SAndrey Smirnov 		args.freq = v4l2_to_si476x(radio->core,
535b879a9c2SAndrey Smirnov 					   0.6 * FREQ_MUL);
536b879a9c2SAndrey Smirnov 		retval = radio->ops->tune_freq(radio->core, &args);
537b879a9c2SAndrey Smirnov 		break;
538b879a9c2SAndrey Smirnov 	default:
539b879a9c2SAndrey Smirnov 		WARN(1, "Unexpected tuner function value\n");
540b879a9c2SAndrey Smirnov 		retval = -EINVAL;
541b879a9c2SAndrey Smirnov 	}
542b879a9c2SAndrey Smirnov 
543b879a9c2SAndrey Smirnov 	return retval;
544b879a9c2SAndrey Smirnov }
545b879a9c2SAndrey Smirnov static int si476x_radio_do_post_powerup_init(struct si476x_radio *radio,
546b879a9c2SAndrey Smirnov 					     enum si476x_func func)
547b879a9c2SAndrey Smirnov {
548b879a9c2SAndrey Smirnov 	int err;
549b879a9c2SAndrey Smirnov 
550b879a9c2SAndrey Smirnov 	/* regcache_mark_dirty(radio->core->regmap); */
551b879a9c2SAndrey Smirnov 	err = regcache_sync_region(radio->core->regmap,
552b879a9c2SAndrey Smirnov 				   SI476X_PROP_DIGITAL_IO_INPUT_SAMPLE_RATE,
553b879a9c2SAndrey Smirnov 				   SI476X_PROP_DIGITAL_IO_OUTPUT_FORMAT);
554b879a9c2SAndrey Smirnov 	if (err < 0)
555b879a9c2SAndrey Smirnov 		return err;
556b879a9c2SAndrey Smirnov 
557b879a9c2SAndrey Smirnov 	err = regcache_sync_region(radio->core->regmap,
558b879a9c2SAndrey Smirnov 				   SI476X_PROP_AUDIO_DEEMPHASIS,
559b879a9c2SAndrey Smirnov 				   SI476X_PROP_AUDIO_PWR_LINE_FILTER);
560b879a9c2SAndrey Smirnov 	if (err < 0)
561b879a9c2SAndrey Smirnov 		return err;
562b879a9c2SAndrey Smirnov 
563b879a9c2SAndrey Smirnov 	err = regcache_sync_region(radio->core->regmap,
564b879a9c2SAndrey Smirnov 				   SI476X_PROP_INT_CTL_ENABLE,
565b879a9c2SAndrey Smirnov 				   SI476X_PROP_INT_CTL_ENABLE);
566b879a9c2SAndrey Smirnov 	if (err < 0)
567b879a9c2SAndrey Smirnov 		return err;
568b879a9c2SAndrey Smirnov 
569b879a9c2SAndrey Smirnov 	/*
570b879a9c2SAndrey Smirnov 	 * Is there any point in restoring SNR and the like
571b879a9c2SAndrey Smirnov 	 * when switching between AM/FM?
572b879a9c2SAndrey Smirnov 	 */
573b879a9c2SAndrey Smirnov 	err = regcache_sync_region(radio->core->regmap,
574b879a9c2SAndrey Smirnov 				   SI476X_PROP_VALID_MAX_TUNE_ERROR,
575b879a9c2SAndrey Smirnov 				   SI476X_PROP_VALID_MAX_TUNE_ERROR);
576b879a9c2SAndrey Smirnov 	if (err < 0)
577b879a9c2SAndrey Smirnov 		return err;
578b879a9c2SAndrey Smirnov 
579b879a9c2SAndrey Smirnov 	err = regcache_sync_region(radio->core->regmap,
580b879a9c2SAndrey Smirnov 				   SI476X_PROP_VALID_SNR_THRESHOLD,
581b879a9c2SAndrey Smirnov 				   SI476X_PROP_VALID_RSSI_THRESHOLD);
582b879a9c2SAndrey Smirnov 	if (err < 0)
583b879a9c2SAndrey Smirnov 		return err;
584b879a9c2SAndrey Smirnov 
585b879a9c2SAndrey Smirnov 	if (func == SI476X_FUNC_FM_RECEIVER) {
586b879a9c2SAndrey Smirnov 		if (si476x_core_has_diversity(radio->core)) {
587b879a9c2SAndrey Smirnov 			err = si476x_core_cmd_fm_phase_diversity(radio->core,
588b879a9c2SAndrey Smirnov 								 radio->core->diversity_mode);
589b879a9c2SAndrey Smirnov 			if (err < 0)
590b879a9c2SAndrey Smirnov 				return err;
591b879a9c2SAndrey Smirnov 		}
592b879a9c2SAndrey Smirnov 
593b879a9c2SAndrey Smirnov 		err = regcache_sync_region(radio->core->regmap,
594b879a9c2SAndrey Smirnov 					   SI476X_PROP_FM_RDS_INTERRUPT_SOURCE,
595b879a9c2SAndrey Smirnov 					   SI476X_PROP_FM_RDS_CONFIG);
596b879a9c2SAndrey Smirnov 		if (err < 0)
597b879a9c2SAndrey Smirnov 			return err;
598b879a9c2SAndrey Smirnov 	}
599b879a9c2SAndrey Smirnov 
600b879a9c2SAndrey Smirnov 	return si476x_radio_init_vtable(radio, func);
601b879a9c2SAndrey Smirnov 
602b879a9c2SAndrey Smirnov }
603b879a9c2SAndrey Smirnov 
604b879a9c2SAndrey Smirnov static int si476x_radio_change_func(struct si476x_radio *radio,
605b879a9c2SAndrey Smirnov 				    enum si476x_func func)
606b879a9c2SAndrey Smirnov {
607b879a9c2SAndrey Smirnov 	int err;
608b879a9c2SAndrey Smirnov 	bool soft;
609b879a9c2SAndrey Smirnov 	/*
610b879a9c2SAndrey Smirnov 	 * Since power/up down is a very time consuming operation,
611b879a9c2SAndrey Smirnov 	 * try to avoid doing it if the requested mode matches the one
612b879a9c2SAndrey Smirnov 	 * the tuner is in
613b879a9c2SAndrey Smirnov 	 */
614b879a9c2SAndrey Smirnov 	if (func == radio->core->power_up_parameters.func)
615b879a9c2SAndrey Smirnov 		return 0;
616b879a9c2SAndrey Smirnov 
617b879a9c2SAndrey Smirnov 	soft = true;
618b879a9c2SAndrey Smirnov 	err = si476x_core_stop(radio->core, soft);
619b879a9c2SAndrey Smirnov 	if (err < 0) {
620b879a9c2SAndrey Smirnov 		/*
621b879a9c2SAndrey Smirnov 		 * OK, if the chip does not want to play nice let's
622b879a9c2SAndrey Smirnov 		 * try to reset it in more brutal way
623b879a9c2SAndrey Smirnov 		 */
624b879a9c2SAndrey Smirnov 		soft = false;
625b879a9c2SAndrey Smirnov 		err = si476x_core_stop(radio->core, soft);
626b879a9c2SAndrey Smirnov 		if (err < 0)
627b879a9c2SAndrey Smirnov 			return err;
628b879a9c2SAndrey Smirnov 	}
629b879a9c2SAndrey Smirnov 	/*
630b879a9c2SAndrey Smirnov 	  Set the desired radio tuner function
631b879a9c2SAndrey Smirnov 	 */
632b879a9c2SAndrey Smirnov 	radio->core->power_up_parameters.func = func;
633b879a9c2SAndrey Smirnov 
634b879a9c2SAndrey Smirnov 	err = si476x_core_start(radio->core, soft);
635b879a9c2SAndrey Smirnov 	if (err < 0)
636b879a9c2SAndrey Smirnov 		return err;
637b879a9c2SAndrey Smirnov 
638b879a9c2SAndrey Smirnov 	/*
639b879a9c2SAndrey Smirnov 	 * No need to do the rest of manipulations for the bootlader
640b879a9c2SAndrey Smirnov 	 * mode
641b879a9c2SAndrey Smirnov 	 */
642b879a9c2SAndrey Smirnov 	if (func != SI476X_FUNC_FM_RECEIVER &&
643b879a9c2SAndrey Smirnov 	    func != SI476X_FUNC_AM_RECEIVER)
644b879a9c2SAndrey Smirnov 		return err;
645b879a9c2SAndrey Smirnov 
646b879a9c2SAndrey Smirnov 	return si476x_radio_do_post_powerup_init(radio, func);
647b879a9c2SAndrey Smirnov }
648b879a9c2SAndrey Smirnov 
649b879a9c2SAndrey Smirnov static int si476x_radio_g_frequency(struct file *file, void *priv,
650b879a9c2SAndrey Smirnov 			      struct v4l2_frequency *f)
651b879a9c2SAndrey Smirnov {
652b879a9c2SAndrey Smirnov 	int err;
653b879a9c2SAndrey Smirnov 	struct si476x_radio *radio = video_drvdata(file);
654b879a9c2SAndrey Smirnov 
655b879a9c2SAndrey Smirnov 	if (f->tuner != 0 ||
656b879a9c2SAndrey Smirnov 	    f->type  != V4L2_TUNER_RADIO)
657b879a9c2SAndrey Smirnov 		return -EINVAL;
658b879a9c2SAndrey Smirnov 
659b879a9c2SAndrey Smirnov 	si476x_core_lock(radio->core);
660b879a9c2SAndrey Smirnov 
661b879a9c2SAndrey Smirnov 	if (radio->ops->rsq_status) {
662b879a9c2SAndrey Smirnov 		struct si476x_rsq_status_report report;
663b879a9c2SAndrey Smirnov 		struct si476x_rsq_status_args   args = {
664b879a9c2SAndrey Smirnov 			.primary	= false,
665b879a9c2SAndrey Smirnov 			.rsqack		= false,
666b879a9c2SAndrey Smirnov 			.attune		= true,
667b879a9c2SAndrey Smirnov 			.cancel		= false,
668b879a9c2SAndrey Smirnov 			.stcack		= false,
669b879a9c2SAndrey Smirnov 		};
670b879a9c2SAndrey Smirnov 
671b879a9c2SAndrey Smirnov 		err = radio->ops->rsq_status(radio->core, &args, &report);
672b879a9c2SAndrey Smirnov 		if (!err)
673b879a9c2SAndrey Smirnov 			f->frequency = si476x_to_v4l2(radio->core,
674b879a9c2SAndrey Smirnov 						      report.readfreq);
675b879a9c2SAndrey Smirnov 	} else {
676b879a9c2SAndrey Smirnov 		err = -EINVAL;
677b879a9c2SAndrey Smirnov 	}
678b879a9c2SAndrey Smirnov 
679b879a9c2SAndrey Smirnov 	si476x_core_unlock(radio->core);
680b879a9c2SAndrey Smirnov 
681b879a9c2SAndrey Smirnov 	return err;
682b879a9c2SAndrey Smirnov }
683b879a9c2SAndrey Smirnov 
684b879a9c2SAndrey Smirnov static int si476x_radio_s_frequency(struct file *file, void *priv,
685b879a9c2SAndrey Smirnov 				    const struct v4l2_frequency *f)
686b879a9c2SAndrey Smirnov {
687b879a9c2SAndrey Smirnov 	int err;
688b879a9c2SAndrey Smirnov 	u32 freq = f->frequency;
689b879a9c2SAndrey Smirnov 	struct si476x_tune_freq_args args;
690b879a9c2SAndrey Smirnov 	struct si476x_radio *radio = video_drvdata(file);
691b879a9c2SAndrey Smirnov 
692b879a9c2SAndrey Smirnov 	const u32 midrange = (si476x_bands[SI476X_BAND_AM].rangehigh +
693b879a9c2SAndrey Smirnov 			      si476x_bands[SI476X_BAND_FM].rangelow) / 2;
694b879a9c2SAndrey Smirnov 	const int band = (freq > midrange) ?
695b879a9c2SAndrey Smirnov 		SI476X_BAND_FM : SI476X_BAND_AM;
696b879a9c2SAndrey Smirnov 	const enum si476x_func func = (band == SI476X_BAND_AM) ?
697b879a9c2SAndrey Smirnov 		SI476X_FUNC_AM_RECEIVER : SI476X_FUNC_FM_RECEIVER;
698b879a9c2SAndrey Smirnov 
699b879a9c2SAndrey Smirnov 	if (f->tuner != 0 ||
700b879a9c2SAndrey Smirnov 	    f->type  != V4L2_TUNER_RADIO)
701b879a9c2SAndrey Smirnov 		return -EINVAL;
702b879a9c2SAndrey Smirnov 
703b879a9c2SAndrey Smirnov 	si476x_core_lock(radio->core);
704b879a9c2SAndrey Smirnov 
705b879a9c2SAndrey Smirnov 	freq = clamp(freq,
706b879a9c2SAndrey Smirnov 		     si476x_bands[band].rangelow,
707b879a9c2SAndrey Smirnov 		     si476x_bands[band].rangehigh);
708b879a9c2SAndrey Smirnov 
709b879a9c2SAndrey Smirnov 	if (si476x_radio_freq_is_inside_of_the_band(freq,
710b879a9c2SAndrey Smirnov 						    SI476X_BAND_AM) &&
711b879a9c2SAndrey Smirnov 	    (!si476x_core_has_am(radio->core) ||
712b879a9c2SAndrey Smirnov 	     si476x_core_is_a_secondary_tuner(radio->core))) {
713b879a9c2SAndrey Smirnov 		err = -EINVAL;
714b879a9c2SAndrey Smirnov 		goto unlock;
715b879a9c2SAndrey Smirnov 	}
716b879a9c2SAndrey Smirnov 
717b879a9c2SAndrey Smirnov 	err = si476x_radio_change_func(radio, func);
718b879a9c2SAndrey Smirnov 	if (err < 0)
719b879a9c2SAndrey Smirnov 		goto unlock;
720b879a9c2SAndrey Smirnov 
721b879a9c2SAndrey Smirnov 	args.zifsr		= false;
722b879a9c2SAndrey Smirnov 	args.hd			= false;
723b879a9c2SAndrey Smirnov 	args.injside		= SI476X_INJSIDE_AUTO;
724b879a9c2SAndrey Smirnov 	args.freq		= v4l2_to_si476x(radio->core, freq);
725b879a9c2SAndrey Smirnov 	args.tunemode		= SI476X_TM_VALIDATED_NORMAL_TUNE;
726b879a9c2SAndrey Smirnov 	args.smoothmetrics	= SI476X_SM_INITIALIZE_AUDIO;
727b879a9c2SAndrey Smirnov 	args.antcap		= 0;
728b879a9c2SAndrey Smirnov 
729b879a9c2SAndrey Smirnov 	err = radio->ops->tune_freq(radio->core, &args);
730b879a9c2SAndrey Smirnov 
731b879a9c2SAndrey Smirnov unlock:
732b879a9c2SAndrey Smirnov 	si476x_core_unlock(radio->core);
733b879a9c2SAndrey Smirnov 	return err;
734b879a9c2SAndrey Smirnov }
735b879a9c2SAndrey Smirnov 
736b879a9c2SAndrey Smirnov static int si476x_radio_s_hw_freq_seek(struct file *file, void *priv,
737b879a9c2SAndrey Smirnov 				       const struct v4l2_hw_freq_seek *seek)
738b879a9c2SAndrey Smirnov {
739b879a9c2SAndrey Smirnov 	int err;
740b879a9c2SAndrey Smirnov 	enum si476x_func func;
74102d73243SMauro Carvalho Chehab 	u32 rangelow = seek->rangelow, rangehigh = seek->rangehigh;
742b879a9c2SAndrey Smirnov 	struct si476x_radio *radio = video_drvdata(file);
743b879a9c2SAndrey Smirnov 
744b879a9c2SAndrey Smirnov 	if (file->f_flags & O_NONBLOCK)
745b879a9c2SAndrey Smirnov 		return -EAGAIN;
746b879a9c2SAndrey Smirnov 
747b879a9c2SAndrey Smirnov 	if (seek->tuner != 0 ||
748b879a9c2SAndrey Smirnov 	    seek->type  != V4L2_TUNER_RADIO)
749b879a9c2SAndrey Smirnov 		return -EINVAL;
750b879a9c2SAndrey Smirnov 
751b879a9c2SAndrey Smirnov 	si476x_core_lock(radio->core);
752b879a9c2SAndrey Smirnov 
75302d73243SMauro Carvalho Chehab 	if (!rangelow) {
754b879a9c2SAndrey Smirnov 		err = regmap_read(radio->core->regmap,
755b879a9c2SAndrey Smirnov 				  SI476X_PROP_SEEK_BAND_BOTTOM,
756b879a9c2SAndrey Smirnov 				  &rangelow);
75702d73243SMauro Carvalho Chehab 		if (err)
758b879a9c2SAndrey Smirnov 			goto unlock;
75902d73243SMauro Carvalho Chehab 		rangelow = si476x_to_v4l2(radio->core, rangelow);
760b879a9c2SAndrey Smirnov 	}
76102d73243SMauro Carvalho Chehab 	if (!rangehigh) {
762b879a9c2SAndrey Smirnov 		err = regmap_read(radio->core->regmap,
763b879a9c2SAndrey Smirnov 				  SI476X_PROP_SEEK_BAND_TOP,
764b879a9c2SAndrey Smirnov 				  &rangehigh);
76502d73243SMauro Carvalho Chehab 		if (err)
766b879a9c2SAndrey Smirnov 			goto unlock;
76702d73243SMauro Carvalho Chehab 		rangehigh = si476x_to_v4l2(radio->core, rangehigh);
768b879a9c2SAndrey Smirnov 	}
769b879a9c2SAndrey Smirnov 
770b879a9c2SAndrey Smirnov 	if (rangelow > rangehigh) {
771b879a9c2SAndrey Smirnov 		err = -EINVAL;
772b879a9c2SAndrey Smirnov 		goto unlock;
773b879a9c2SAndrey Smirnov 	}
774b879a9c2SAndrey Smirnov 
775b879a9c2SAndrey Smirnov 	if (si476x_radio_range_is_inside_of_the_band(rangelow, rangehigh,
776b879a9c2SAndrey Smirnov 						     SI476X_BAND_FM)) {
777b879a9c2SAndrey Smirnov 		func = SI476X_FUNC_FM_RECEIVER;
778b879a9c2SAndrey Smirnov 
779b879a9c2SAndrey Smirnov 	} else if (si476x_core_has_am(radio->core) &&
780b879a9c2SAndrey Smirnov 		   si476x_radio_range_is_inside_of_the_band(rangelow, rangehigh,
781b879a9c2SAndrey Smirnov 							    SI476X_BAND_AM)) {
782b879a9c2SAndrey Smirnov 		func = SI476X_FUNC_AM_RECEIVER;
783b879a9c2SAndrey Smirnov 	} else {
784b879a9c2SAndrey Smirnov 		err = -EINVAL;
785b879a9c2SAndrey Smirnov 		goto unlock;
786b879a9c2SAndrey Smirnov 	}
787b879a9c2SAndrey Smirnov 
788b879a9c2SAndrey Smirnov 	err = si476x_radio_change_func(radio, func);
789b879a9c2SAndrey Smirnov 	if (err < 0)
790b879a9c2SAndrey Smirnov 		goto unlock;
791b879a9c2SAndrey Smirnov 
792b879a9c2SAndrey Smirnov 	if (seek->rangehigh) {
793b879a9c2SAndrey Smirnov 		err = regmap_write(radio->core->regmap,
794b879a9c2SAndrey Smirnov 				   SI476X_PROP_SEEK_BAND_TOP,
795b879a9c2SAndrey Smirnov 				   v4l2_to_si476x(radio->core,
796b879a9c2SAndrey Smirnov 						  seek->rangehigh));
797b879a9c2SAndrey Smirnov 		if (err)
798b879a9c2SAndrey Smirnov 			goto unlock;
799b879a9c2SAndrey Smirnov 	}
800b879a9c2SAndrey Smirnov 	if (seek->rangelow) {
801b879a9c2SAndrey Smirnov 		err = regmap_write(radio->core->regmap,
802b879a9c2SAndrey Smirnov 				   SI476X_PROP_SEEK_BAND_BOTTOM,
803b879a9c2SAndrey Smirnov 				   v4l2_to_si476x(radio->core,
804b879a9c2SAndrey Smirnov 						  seek->rangelow));
805b879a9c2SAndrey Smirnov 		if (err)
806b879a9c2SAndrey Smirnov 			goto unlock;
807b879a9c2SAndrey Smirnov 	}
808b879a9c2SAndrey Smirnov 	if (seek->spacing) {
809b879a9c2SAndrey Smirnov 		err = regmap_write(radio->core->regmap,
810b879a9c2SAndrey Smirnov 				     SI476X_PROP_SEEK_FREQUENCY_SPACING,
811b879a9c2SAndrey Smirnov 				     v4l2_to_si476x(radio->core,
812b879a9c2SAndrey Smirnov 						    seek->spacing));
813b879a9c2SAndrey Smirnov 		if (err)
814b879a9c2SAndrey Smirnov 			goto unlock;
815b879a9c2SAndrey Smirnov 	}
816b879a9c2SAndrey Smirnov 
817b879a9c2SAndrey Smirnov 	err = radio->ops->seek_start(radio->core,
818b879a9c2SAndrey Smirnov 				     seek->seek_upward,
819b879a9c2SAndrey Smirnov 				     seek->wrap_around);
820b879a9c2SAndrey Smirnov unlock:
821b879a9c2SAndrey Smirnov 	si476x_core_unlock(radio->core);
822b879a9c2SAndrey Smirnov 
823b879a9c2SAndrey Smirnov 
824b879a9c2SAndrey Smirnov 
825b879a9c2SAndrey Smirnov 	return err;
826b879a9c2SAndrey Smirnov }
827b879a9c2SAndrey Smirnov 
828b879a9c2SAndrey Smirnov static int si476x_radio_g_volatile_ctrl(struct v4l2_ctrl *ctrl)
829b879a9c2SAndrey Smirnov {
830b879a9c2SAndrey Smirnov 	int retval;
831b879a9c2SAndrey Smirnov 	struct si476x_radio *radio = v4l2_ctrl_handler_to_radio(ctrl->handler);
832b879a9c2SAndrey Smirnov 
833b879a9c2SAndrey Smirnov 	si476x_core_lock(radio->core);
834b879a9c2SAndrey Smirnov 
835b879a9c2SAndrey Smirnov 	switch (ctrl->id) {
836b879a9c2SAndrey Smirnov 	case V4L2_CID_SI476X_INTERCHIP_LINK:
837b879a9c2SAndrey Smirnov 		if (si476x_core_has_diversity(radio->core)) {
838b879a9c2SAndrey Smirnov 			if (radio->ops->phase_diversity) {
839b879a9c2SAndrey Smirnov 				retval = radio->ops->phase_div_status(radio->core);
840b879a9c2SAndrey Smirnov 				if (retval < 0)
841b879a9c2SAndrey Smirnov 					break;
842b879a9c2SAndrey Smirnov 
843b879a9c2SAndrey Smirnov 				ctrl->val = !!SI476X_PHDIV_STATUS_LINK_LOCKED(retval);
844b879a9c2SAndrey Smirnov 				retval = 0;
845b879a9c2SAndrey Smirnov 				break;
846b879a9c2SAndrey Smirnov 			} else {
847b879a9c2SAndrey Smirnov 				retval = -ENOTTY;
848b879a9c2SAndrey Smirnov 				break;
849b879a9c2SAndrey Smirnov 			}
850b879a9c2SAndrey Smirnov 		}
851b879a9c2SAndrey Smirnov 		retval = -EINVAL;
852b879a9c2SAndrey Smirnov 		break;
853b879a9c2SAndrey Smirnov 	default:
854b879a9c2SAndrey Smirnov 		retval = -EINVAL;
855b879a9c2SAndrey Smirnov 		break;
856b879a9c2SAndrey Smirnov 	}
857b879a9c2SAndrey Smirnov 	si476x_core_unlock(radio->core);
858b879a9c2SAndrey Smirnov 	return retval;
859b879a9c2SAndrey Smirnov 
860b879a9c2SAndrey Smirnov }
861b879a9c2SAndrey Smirnov 
862b879a9c2SAndrey Smirnov static int si476x_radio_s_ctrl(struct v4l2_ctrl *ctrl)
863b879a9c2SAndrey Smirnov {
864b879a9c2SAndrey Smirnov 	int retval;
865b879a9c2SAndrey Smirnov 	enum si476x_phase_diversity_mode mode;
866b879a9c2SAndrey Smirnov 	struct si476x_radio *radio = v4l2_ctrl_handler_to_radio(ctrl->handler);
867b879a9c2SAndrey Smirnov 
868b879a9c2SAndrey Smirnov 	si476x_core_lock(radio->core);
869b879a9c2SAndrey Smirnov 
870b879a9c2SAndrey Smirnov 	switch (ctrl->id) {
871b879a9c2SAndrey Smirnov 	case V4L2_CID_SI476X_HARMONICS_COUNT:
872b879a9c2SAndrey Smirnov 		retval = regmap_update_bits(radio->core->regmap,
873b879a9c2SAndrey Smirnov 					    SI476X_PROP_AUDIO_PWR_LINE_FILTER,
874b879a9c2SAndrey Smirnov 					    SI476X_PROP_PWR_HARMONICS_MASK,
875b879a9c2SAndrey Smirnov 					    ctrl->val);
876b879a9c2SAndrey Smirnov 		break;
877b879a9c2SAndrey Smirnov 	case V4L2_CID_POWER_LINE_FREQUENCY:
878b879a9c2SAndrey Smirnov 		switch (ctrl->val) {
879b879a9c2SAndrey Smirnov 		case V4L2_CID_POWER_LINE_FREQUENCY_DISABLED:
880b879a9c2SAndrey Smirnov 			retval = regmap_update_bits(radio->core->regmap,
881b879a9c2SAndrey Smirnov 						    SI476X_PROP_AUDIO_PWR_LINE_FILTER,
882b879a9c2SAndrey Smirnov 						    SI476X_PROP_PWR_ENABLE_MASK,
883b879a9c2SAndrey Smirnov 						    0);
884b879a9c2SAndrey Smirnov 			break;
885b879a9c2SAndrey Smirnov 		case V4L2_CID_POWER_LINE_FREQUENCY_50HZ:
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_50HZ);
890b879a9c2SAndrey Smirnov 			break;
891b879a9c2SAndrey Smirnov 		case V4L2_CID_POWER_LINE_FREQUENCY_60HZ:
892b879a9c2SAndrey Smirnov 			retval = regmap_update_bits(radio->core->regmap,
893b879a9c2SAndrey Smirnov 						    SI476X_PROP_AUDIO_PWR_LINE_FILTER,
894b879a9c2SAndrey Smirnov 						    SI476X_PROP_PWR_GRID_MASK,
895b879a9c2SAndrey Smirnov 						    SI476X_PROP_PWR_GRID_60HZ);
896b879a9c2SAndrey Smirnov 			break;
897b879a9c2SAndrey Smirnov 		default:
898b879a9c2SAndrey Smirnov 			retval = -EINVAL;
899b879a9c2SAndrey Smirnov 			break;
900b879a9c2SAndrey Smirnov 		}
901b879a9c2SAndrey Smirnov 		break;
902b879a9c2SAndrey Smirnov 	case V4L2_CID_SI476X_RSSI_THRESHOLD:
903b879a9c2SAndrey Smirnov 		retval = regmap_write(radio->core->regmap,
904b879a9c2SAndrey Smirnov 				      SI476X_PROP_VALID_RSSI_THRESHOLD,
905b879a9c2SAndrey Smirnov 				      ctrl->val);
906b879a9c2SAndrey Smirnov 		break;
907b879a9c2SAndrey Smirnov 	case V4L2_CID_SI476X_SNR_THRESHOLD:
908b879a9c2SAndrey Smirnov 		retval = regmap_write(radio->core->regmap,
909b879a9c2SAndrey Smirnov 				      SI476X_PROP_VALID_SNR_THRESHOLD,
910b879a9c2SAndrey Smirnov 				      ctrl->val);
911b879a9c2SAndrey Smirnov 		break;
912b879a9c2SAndrey Smirnov 	case V4L2_CID_SI476X_MAX_TUNE_ERROR:
913b879a9c2SAndrey Smirnov 		retval = regmap_write(radio->core->regmap,
914b879a9c2SAndrey Smirnov 				      SI476X_PROP_VALID_MAX_TUNE_ERROR,
915b879a9c2SAndrey Smirnov 				      ctrl->val);
916b879a9c2SAndrey Smirnov 		break;
917b879a9c2SAndrey Smirnov 	case V4L2_CID_RDS_RECEPTION:
918b879a9c2SAndrey Smirnov 		/*
919b879a9c2SAndrey Smirnov 		 * It looks like RDS related properties are
920*30bcc510SXiaofeng Cao 		 * inaccessible when tuner is in AM mode, so cache the
921b879a9c2SAndrey Smirnov 		 * changes
922b879a9c2SAndrey Smirnov 		 */
923b879a9c2SAndrey Smirnov 		if (si476x_core_is_in_am_receiver_mode(radio->core))
924b879a9c2SAndrey Smirnov 			regcache_cache_only(radio->core->regmap, true);
925b879a9c2SAndrey Smirnov 
926b879a9c2SAndrey Smirnov 		if (ctrl->val) {
927b879a9c2SAndrey Smirnov 			retval = regmap_write(radio->core->regmap,
928b879a9c2SAndrey Smirnov 					      SI476X_PROP_FM_RDS_INTERRUPT_FIFO_COUNT,
929b879a9c2SAndrey Smirnov 					      radio->core->rds_fifo_depth);
930b879a9c2SAndrey Smirnov 			if (retval < 0)
931b879a9c2SAndrey Smirnov 				break;
932b879a9c2SAndrey Smirnov 
933b879a9c2SAndrey Smirnov 			if (radio->core->client->irq) {
934b879a9c2SAndrey Smirnov 				retval = regmap_write(radio->core->regmap,
935b879a9c2SAndrey Smirnov 						      SI476X_PROP_FM_RDS_INTERRUPT_SOURCE,
936b879a9c2SAndrey Smirnov 						      SI476X_RDSRECV);
937b879a9c2SAndrey Smirnov 				if (retval < 0)
938b879a9c2SAndrey Smirnov 					break;
939b879a9c2SAndrey Smirnov 			}
940b879a9c2SAndrey Smirnov 
941b879a9c2SAndrey Smirnov 			/* Drain RDS FIFO before enabling RDS processing */
942b879a9c2SAndrey Smirnov 			retval = si476x_core_cmd_fm_rds_status(radio->core,
943b879a9c2SAndrey Smirnov 							       false,
944b879a9c2SAndrey Smirnov 							       true,
945b879a9c2SAndrey Smirnov 							       true,
946b879a9c2SAndrey Smirnov 							       NULL);
947b879a9c2SAndrey Smirnov 			if (retval < 0)
948b879a9c2SAndrey Smirnov 				break;
949b879a9c2SAndrey Smirnov 
950b879a9c2SAndrey Smirnov 			retval = regmap_update_bits(radio->core->regmap,
951b879a9c2SAndrey Smirnov 						    SI476X_PROP_FM_RDS_CONFIG,
952b879a9c2SAndrey Smirnov 						    SI476X_PROP_RDSEN_MASK,
953b879a9c2SAndrey Smirnov 						    SI476X_PROP_RDSEN);
954b879a9c2SAndrey Smirnov 		} else {
955b879a9c2SAndrey Smirnov 			retval = regmap_update_bits(radio->core->regmap,
956b879a9c2SAndrey Smirnov 						    SI476X_PROP_FM_RDS_CONFIG,
957b879a9c2SAndrey Smirnov 						    SI476X_PROP_RDSEN_MASK,
958b879a9c2SAndrey Smirnov 						    !SI476X_PROP_RDSEN);
959b879a9c2SAndrey Smirnov 		}
960b879a9c2SAndrey Smirnov 
961b879a9c2SAndrey Smirnov 		if (si476x_core_is_in_am_receiver_mode(radio->core))
962b879a9c2SAndrey Smirnov 			regcache_cache_only(radio->core->regmap, false);
963b879a9c2SAndrey Smirnov 		break;
964b879a9c2SAndrey Smirnov 	case V4L2_CID_TUNE_DEEMPHASIS:
965b879a9c2SAndrey Smirnov 		retval = regmap_write(radio->core->regmap,
966b879a9c2SAndrey Smirnov 				      SI476X_PROP_AUDIO_DEEMPHASIS,
967b879a9c2SAndrey Smirnov 				      ctrl->val);
968b879a9c2SAndrey Smirnov 		break;
969b879a9c2SAndrey Smirnov 
970b879a9c2SAndrey Smirnov 	case V4L2_CID_SI476X_DIVERSITY_MODE:
971b879a9c2SAndrey Smirnov 		mode = si476x_phase_diversity_idx_to_mode(ctrl->val);
972b879a9c2SAndrey Smirnov 
973b879a9c2SAndrey Smirnov 		if (mode == radio->core->diversity_mode) {
974b879a9c2SAndrey Smirnov 			retval = 0;
975b879a9c2SAndrey Smirnov 			break;
976b879a9c2SAndrey Smirnov 		}
977b879a9c2SAndrey Smirnov 
978b879a9c2SAndrey Smirnov 		if (si476x_core_is_in_am_receiver_mode(radio->core)) {
979b879a9c2SAndrey Smirnov 			/*
980b879a9c2SAndrey Smirnov 			 * Diversity cannot be configured while tuner
981b879a9c2SAndrey Smirnov 			 * is in AM mode so save the changes and carry on.
982b879a9c2SAndrey Smirnov 			 */
983b879a9c2SAndrey Smirnov 			radio->core->diversity_mode = mode;
984b879a9c2SAndrey Smirnov 			retval = 0;
985b879a9c2SAndrey Smirnov 		} else {
986b879a9c2SAndrey Smirnov 			retval = radio->ops->phase_diversity(radio->core, mode);
987b879a9c2SAndrey Smirnov 			if (!retval)
988b879a9c2SAndrey Smirnov 				radio->core->diversity_mode = mode;
989b879a9c2SAndrey Smirnov 		}
990b879a9c2SAndrey Smirnov 		break;
991b879a9c2SAndrey Smirnov 
992b879a9c2SAndrey Smirnov 	default:
993b879a9c2SAndrey Smirnov 		retval = -EINVAL;
994b879a9c2SAndrey Smirnov 		break;
995b879a9c2SAndrey Smirnov 	}
996b879a9c2SAndrey Smirnov 
997b879a9c2SAndrey Smirnov 	si476x_core_unlock(radio->core);
998b879a9c2SAndrey Smirnov 
999b879a9c2SAndrey Smirnov 	return retval;
1000b879a9c2SAndrey Smirnov }
1001b879a9c2SAndrey Smirnov 
1002b879a9c2SAndrey Smirnov #ifdef CONFIG_VIDEO_ADV_DEBUG
1003b879a9c2SAndrey Smirnov static int si476x_radio_g_register(struct file *file, void *fh,
1004b879a9c2SAndrey Smirnov 				   struct v4l2_dbg_register *reg)
1005b879a9c2SAndrey Smirnov {
1006b879a9c2SAndrey Smirnov 	int err;
1007b879a9c2SAndrey Smirnov 	unsigned int value;
1008b879a9c2SAndrey Smirnov 	struct si476x_radio *radio = video_drvdata(file);
1009b879a9c2SAndrey Smirnov 
1010b879a9c2SAndrey Smirnov 	si476x_core_lock(radio->core);
1011b879a9c2SAndrey Smirnov 	reg->size = 2;
1012b879a9c2SAndrey Smirnov 	err = regmap_read(radio->core->regmap,
1013b879a9c2SAndrey Smirnov 			  (unsigned int)reg->reg, &value);
1014b879a9c2SAndrey Smirnov 	reg->val = value;
1015b879a9c2SAndrey Smirnov 	si476x_core_unlock(radio->core);
1016b879a9c2SAndrey Smirnov 
1017b879a9c2SAndrey Smirnov 	return err;
1018b879a9c2SAndrey Smirnov }
1019b879a9c2SAndrey Smirnov static int si476x_radio_s_register(struct file *file, void *fh,
1020b879a9c2SAndrey Smirnov 				   const struct v4l2_dbg_register *reg)
1021b879a9c2SAndrey Smirnov {
1022b879a9c2SAndrey Smirnov 
1023b879a9c2SAndrey Smirnov 	int err;
1024b879a9c2SAndrey Smirnov 	struct si476x_radio *radio = video_drvdata(file);
1025b879a9c2SAndrey Smirnov 
1026b879a9c2SAndrey Smirnov 	si476x_core_lock(radio->core);
1027b879a9c2SAndrey Smirnov 	err = regmap_write(radio->core->regmap,
1028b879a9c2SAndrey Smirnov 			   (unsigned int)reg->reg,
1029b879a9c2SAndrey Smirnov 			   (unsigned int)reg->val);
1030b879a9c2SAndrey Smirnov 	si476x_core_unlock(radio->core);
1031b879a9c2SAndrey Smirnov 
1032b879a9c2SAndrey Smirnov 	return err;
1033b879a9c2SAndrey Smirnov }
1034b879a9c2SAndrey Smirnov #endif
1035b879a9c2SAndrey Smirnov 
1036b879a9c2SAndrey Smirnov static int si476x_radio_fops_open(struct file *file)
1037b879a9c2SAndrey Smirnov {
1038b879a9c2SAndrey Smirnov 	struct si476x_radio *radio = video_drvdata(file);
1039b879a9c2SAndrey Smirnov 	int err;
1040b879a9c2SAndrey Smirnov 
1041b879a9c2SAndrey Smirnov 	err = v4l2_fh_open(file);
1042b879a9c2SAndrey Smirnov 	if (err)
1043b879a9c2SAndrey Smirnov 		return err;
1044b879a9c2SAndrey Smirnov 
1045b879a9c2SAndrey Smirnov 	if (v4l2_fh_is_singular_file(file)) {
1046b879a9c2SAndrey Smirnov 		si476x_core_lock(radio->core);
1047b879a9c2SAndrey Smirnov 		err = si476x_core_set_power_state(radio->core,
1048b879a9c2SAndrey Smirnov 						  SI476X_POWER_UP_FULL);
1049b879a9c2SAndrey Smirnov 		if (err < 0)
1050b879a9c2SAndrey Smirnov 			goto done;
1051b879a9c2SAndrey Smirnov 
1052b879a9c2SAndrey Smirnov 		err = si476x_radio_do_post_powerup_init(radio,
1053b879a9c2SAndrey Smirnov 							radio->core->power_up_parameters.func);
1054b879a9c2SAndrey Smirnov 		if (err < 0)
1055b879a9c2SAndrey Smirnov 			goto power_down;
1056b879a9c2SAndrey Smirnov 
1057b879a9c2SAndrey Smirnov 		err = si476x_radio_pretune(radio,
1058b879a9c2SAndrey Smirnov 					   radio->core->power_up_parameters.func);
1059b879a9c2SAndrey Smirnov 		if (err < 0)
1060b879a9c2SAndrey Smirnov 			goto power_down;
1061b879a9c2SAndrey Smirnov 
1062b879a9c2SAndrey Smirnov 		si476x_core_unlock(radio->core);
1063b879a9c2SAndrey Smirnov 		/*Must be done after si476x_core_unlock to prevent a deadlock*/
1064b879a9c2SAndrey Smirnov 		v4l2_ctrl_handler_setup(&radio->ctrl_handler);
1065b879a9c2SAndrey Smirnov 	}
1066b879a9c2SAndrey Smirnov 
1067b879a9c2SAndrey Smirnov 	return err;
1068b879a9c2SAndrey Smirnov 
1069b879a9c2SAndrey Smirnov power_down:
1070b879a9c2SAndrey Smirnov 	si476x_core_set_power_state(radio->core,
1071b879a9c2SAndrey Smirnov 				    SI476X_POWER_DOWN);
1072b879a9c2SAndrey Smirnov done:
1073b879a9c2SAndrey Smirnov 	si476x_core_unlock(radio->core);
1074b879a9c2SAndrey Smirnov 	v4l2_fh_release(file);
1075b879a9c2SAndrey Smirnov 
1076b879a9c2SAndrey Smirnov 	return err;
1077b879a9c2SAndrey Smirnov }
1078b879a9c2SAndrey Smirnov 
1079b879a9c2SAndrey Smirnov static int si476x_radio_fops_release(struct file *file)
1080b879a9c2SAndrey Smirnov {
1081b879a9c2SAndrey Smirnov 	int err;
1082b879a9c2SAndrey Smirnov 	struct si476x_radio *radio = video_drvdata(file);
1083b879a9c2SAndrey Smirnov 
1084b879a9c2SAndrey Smirnov 	if (v4l2_fh_is_singular_file(file) &&
1085b879a9c2SAndrey Smirnov 	    atomic_read(&radio->core->is_alive))
1086b879a9c2SAndrey Smirnov 		si476x_core_set_power_state(radio->core,
1087b879a9c2SAndrey Smirnov 					    SI476X_POWER_DOWN);
1088b879a9c2SAndrey Smirnov 
1089b879a9c2SAndrey Smirnov 	err = v4l2_fh_release(file);
1090b879a9c2SAndrey Smirnov 
1091b879a9c2SAndrey Smirnov 	return err;
1092b879a9c2SAndrey Smirnov }
1093b879a9c2SAndrey Smirnov 
1094b879a9c2SAndrey Smirnov static ssize_t si476x_radio_fops_read(struct file *file, char __user *buf,
1095b879a9c2SAndrey Smirnov 				      size_t count, loff_t *ppos)
1096b879a9c2SAndrey Smirnov {
1097b879a9c2SAndrey Smirnov 	ssize_t      rval;
1098b879a9c2SAndrey Smirnov 	size_t       fifo_len;
1099b879a9c2SAndrey Smirnov 	unsigned int copied;
1100b879a9c2SAndrey Smirnov 
1101b879a9c2SAndrey Smirnov 	struct si476x_radio *radio = video_drvdata(file);
1102b879a9c2SAndrey Smirnov 
1103b879a9c2SAndrey Smirnov 	/* block if no new data available */
1104b879a9c2SAndrey Smirnov 	if (kfifo_is_empty(&radio->core->rds_fifo)) {
1105b879a9c2SAndrey Smirnov 		if (file->f_flags & O_NONBLOCK)
1106b879a9c2SAndrey Smirnov 			return -EWOULDBLOCK;
1107b879a9c2SAndrey Smirnov 
1108b879a9c2SAndrey Smirnov 		rval = wait_event_interruptible(radio->core->rds_read_queue,
1109b879a9c2SAndrey Smirnov 						(!kfifo_is_empty(&radio->core->rds_fifo) ||
1110b879a9c2SAndrey Smirnov 						 !atomic_read(&radio->core->is_alive)));
1111b879a9c2SAndrey Smirnov 		if (rval < 0)
1112b879a9c2SAndrey Smirnov 			return -EINTR;
1113b879a9c2SAndrey Smirnov 
1114b879a9c2SAndrey Smirnov 		if (!atomic_read(&radio->core->is_alive))
1115b879a9c2SAndrey Smirnov 			return -ENODEV;
1116b879a9c2SAndrey Smirnov 	}
1117b879a9c2SAndrey Smirnov 
1118b879a9c2SAndrey Smirnov 	fifo_len = kfifo_len(&radio->core->rds_fifo);
1119b879a9c2SAndrey Smirnov 
1120b879a9c2SAndrey Smirnov 	if (kfifo_to_user(&radio->core->rds_fifo, buf,
1121b879a9c2SAndrey Smirnov 			  min(fifo_len, count),
1122b879a9c2SAndrey Smirnov 			  &copied) != 0) {
1123b879a9c2SAndrey Smirnov 		dev_warn(&radio->videodev.dev,
1124b879a9c2SAndrey Smirnov 			 "Error during FIFO to userspace copy\n");
1125b879a9c2SAndrey Smirnov 		rval = -EIO;
1126b879a9c2SAndrey Smirnov 	} else {
1127b879a9c2SAndrey Smirnov 		rval = (ssize_t)copied;
1128b879a9c2SAndrey Smirnov 	}
1129b879a9c2SAndrey Smirnov 
1130b879a9c2SAndrey Smirnov 	return rval;
1131b879a9c2SAndrey Smirnov }
1132b879a9c2SAndrey Smirnov 
1133c23e0cb8SAl Viro static __poll_t si476x_radio_fops_poll(struct file *file,
1134b879a9c2SAndrey Smirnov 				struct poll_table_struct *pts)
1135b879a9c2SAndrey Smirnov {
1136b879a9c2SAndrey Smirnov 	struct si476x_radio *radio = video_drvdata(file);
113701699437SAl Viro 	__poll_t req_events = poll_requested_events(pts);
1138c23e0cb8SAl Viro 	__poll_t err = v4l2_ctrl_poll(file, pts);
1139b879a9c2SAndrey Smirnov 
1140a9a08845SLinus Torvalds 	if (req_events & (EPOLLIN | EPOLLRDNORM)) {
1141b879a9c2SAndrey Smirnov 		if (atomic_read(&radio->core->is_alive))
1142b879a9c2SAndrey Smirnov 			poll_wait(file, &radio->core->rds_read_queue, pts);
1143b879a9c2SAndrey Smirnov 
1144b879a9c2SAndrey Smirnov 		if (!atomic_read(&radio->core->is_alive))
1145a9a08845SLinus Torvalds 			err = EPOLLHUP;
1146b879a9c2SAndrey Smirnov 
1147b879a9c2SAndrey Smirnov 		if (!kfifo_is_empty(&radio->core->rds_fifo))
1148a9a08845SLinus Torvalds 			err = EPOLLIN | EPOLLRDNORM;
1149b879a9c2SAndrey Smirnov 	}
1150b879a9c2SAndrey Smirnov 
1151b879a9c2SAndrey Smirnov 	return err;
1152b879a9c2SAndrey Smirnov }
1153b879a9c2SAndrey Smirnov 
1154b879a9c2SAndrey Smirnov static const struct v4l2_file_operations si476x_fops = {
1155b879a9c2SAndrey Smirnov 	.owner			= THIS_MODULE,
1156b879a9c2SAndrey Smirnov 	.read			= si476x_radio_fops_read,
1157b879a9c2SAndrey Smirnov 	.poll			= si476x_radio_fops_poll,
1158b879a9c2SAndrey Smirnov 	.unlocked_ioctl		= video_ioctl2,
1159b879a9c2SAndrey Smirnov 	.open			= si476x_radio_fops_open,
1160b879a9c2SAndrey Smirnov 	.release		= si476x_radio_fops_release,
1161b879a9c2SAndrey Smirnov };
1162b879a9c2SAndrey Smirnov 
1163b879a9c2SAndrey Smirnov 
1164b879a9c2SAndrey Smirnov static const struct v4l2_ioctl_ops si4761_ioctl_ops = {
1165b879a9c2SAndrey Smirnov 	.vidioc_querycap		= si476x_radio_querycap,
1166b879a9c2SAndrey Smirnov 	.vidioc_g_tuner			= si476x_radio_g_tuner,
1167b879a9c2SAndrey Smirnov 	.vidioc_s_tuner			= si476x_radio_s_tuner,
1168b879a9c2SAndrey Smirnov 
1169b879a9c2SAndrey Smirnov 	.vidioc_g_frequency		= si476x_radio_g_frequency,
1170b879a9c2SAndrey Smirnov 	.vidioc_s_frequency		= si476x_radio_s_frequency,
1171b879a9c2SAndrey Smirnov 	.vidioc_s_hw_freq_seek		= si476x_radio_s_hw_freq_seek,
1172b879a9c2SAndrey Smirnov 	.vidioc_enum_freq_bands		= si476x_radio_enum_freq_bands,
1173b879a9c2SAndrey Smirnov 
1174b879a9c2SAndrey Smirnov 	.vidioc_subscribe_event		= v4l2_ctrl_subscribe_event,
1175b879a9c2SAndrey Smirnov 	.vidioc_unsubscribe_event	= v4l2_event_unsubscribe,
1176b879a9c2SAndrey Smirnov 
1177b879a9c2SAndrey Smirnov #ifdef CONFIG_VIDEO_ADV_DEBUG
1178b879a9c2SAndrey Smirnov 	.vidioc_g_register		= si476x_radio_g_register,
1179b879a9c2SAndrey Smirnov 	.vidioc_s_register		= si476x_radio_s_register,
1180b879a9c2SAndrey Smirnov #endif
1181b879a9c2SAndrey Smirnov };
1182b879a9c2SAndrey Smirnov 
1183b879a9c2SAndrey Smirnov 
1184b879a9c2SAndrey Smirnov static const struct video_device si476x_viddev_template = {
1185b879a9c2SAndrey Smirnov 	.fops			= &si476x_fops,
1186b879a9c2SAndrey Smirnov 	.name			= DRIVER_NAME,
1187b879a9c2SAndrey Smirnov 	.release		= video_device_release_empty,
1188b879a9c2SAndrey Smirnov };
1189b879a9c2SAndrey Smirnov 
1190b879a9c2SAndrey Smirnov 
1191b879a9c2SAndrey Smirnov 
1192b879a9c2SAndrey Smirnov static ssize_t si476x_radio_read_acf_blob(struct file *file,
1193b879a9c2SAndrey Smirnov 					  char __user *user_buf,
1194b879a9c2SAndrey Smirnov 					  size_t count, loff_t *ppos)
1195b879a9c2SAndrey Smirnov {
1196b879a9c2SAndrey Smirnov 	int err;
1197b879a9c2SAndrey Smirnov 	struct si476x_radio *radio = file->private_data;
1198b879a9c2SAndrey Smirnov 	struct si476x_acf_status_report report;
1199b879a9c2SAndrey Smirnov 
1200b879a9c2SAndrey Smirnov 	si476x_core_lock(radio->core);
1201b879a9c2SAndrey Smirnov 	if (radio->ops->acf_status)
1202b879a9c2SAndrey Smirnov 		err = radio->ops->acf_status(radio->core, &report);
1203b879a9c2SAndrey Smirnov 	else
1204b879a9c2SAndrey Smirnov 		err = -ENOENT;
1205b879a9c2SAndrey Smirnov 	si476x_core_unlock(radio->core);
1206b879a9c2SAndrey Smirnov 
1207b879a9c2SAndrey Smirnov 	if (err < 0)
1208b879a9c2SAndrey Smirnov 		return err;
1209b879a9c2SAndrey Smirnov 
1210b879a9c2SAndrey Smirnov 	return simple_read_from_buffer(user_buf, count, ppos, &report,
1211b879a9c2SAndrey Smirnov 				       sizeof(report));
1212b879a9c2SAndrey Smirnov }
1213b879a9c2SAndrey Smirnov 
1214b879a9c2SAndrey Smirnov static const struct file_operations radio_acf_fops = {
1215b879a9c2SAndrey Smirnov 	.open	= simple_open,
1216b879a9c2SAndrey Smirnov 	.llseek = default_llseek,
1217b879a9c2SAndrey Smirnov 	.read	= si476x_radio_read_acf_blob,
1218b879a9c2SAndrey Smirnov };
1219b879a9c2SAndrey Smirnov 
1220b879a9c2SAndrey Smirnov static ssize_t si476x_radio_read_rds_blckcnt_blob(struct file *file,
1221b879a9c2SAndrey Smirnov 						  char __user *user_buf,
1222b879a9c2SAndrey Smirnov 						  size_t count, loff_t *ppos)
1223b879a9c2SAndrey Smirnov {
1224b879a9c2SAndrey Smirnov 	int err;
1225b879a9c2SAndrey Smirnov 	struct si476x_radio *radio = file->private_data;
1226b879a9c2SAndrey Smirnov 	struct si476x_rds_blockcount_report report;
1227b879a9c2SAndrey Smirnov 
1228b879a9c2SAndrey Smirnov 	si476x_core_lock(radio->core);
1229b879a9c2SAndrey Smirnov 	if (radio->ops->rds_blckcnt)
1230b879a9c2SAndrey Smirnov 		err = radio->ops->rds_blckcnt(radio->core, true,
1231b879a9c2SAndrey Smirnov 					       &report);
1232b879a9c2SAndrey Smirnov 	else
1233b879a9c2SAndrey Smirnov 		err = -ENOENT;
1234b879a9c2SAndrey Smirnov 	si476x_core_unlock(radio->core);
1235b879a9c2SAndrey Smirnov 
1236b879a9c2SAndrey Smirnov 	if (err < 0)
1237b879a9c2SAndrey Smirnov 		return err;
1238b879a9c2SAndrey Smirnov 
1239b879a9c2SAndrey Smirnov 	return simple_read_from_buffer(user_buf, count, ppos, &report,
1240b879a9c2SAndrey Smirnov 				       sizeof(report));
1241b879a9c2SAndrey Smirnov }
1242b879a9c2SAndrey Smirnov 
1243b879a9c2SAndrey Smirnov static const struct file_operations radio_rds_blckcnt_fops = {
1244b879a9c2SAndrey Smirnov 	.open	= simple_open,
1245b879a9c2SAndrey Smirnov 	.llseek = default_llseek,
1246b879a9c2SAndrey Smirnov 	.read	= si476x_radio_read_rds_blckcnt_blob,
1247b879a9c2SAndrey Smirnov };
1248b879a9c2SAndrey Smirnov 
1249b879a9c2SAndrey Smirnov static ssize_t si476x_radio_read_agc_blob(struct file *file,
1250b879a9c2SAndrey Smirnov 					  char __user *user_buf,
1251b879a9c2SAndrey Smirnov 					  size_t count, loff_t *ppos)
1252b879a9c2SAndrey Smirnov {
1253b879a9c2SAndrey Smirnov 	int err;
1254b879a9c2SAndrey Smirnov 	struct si476x_radio *radio = file->private_data;
1255b879a9c2SAndrey Smirnov 	struct si476x_agc_status_report report;
1256b879a9c2SAndrey Smirnov 
1257b879a9c2SAndrey Smirnov 	si476x_core_lock(radio->core);
1258b879a9c2SAndrey Smirnov 	if (radio->ops->rds_blckcnt)
1259b879a9c2SAndrey Smirnov 		err = radio->ops->agc_status(radio->core, &report);
1260b879a9c2SAndrey Smirnov 	else
1261b879a9c2SAndrey Smirnov 		err = -ENOENT;
1262b879a9c2SAndrey Smirnov 	si476x_core_unlock(radio->core);
1263b879a9c2SAndrey Smirnov 
1264b879a9c2SAndrey Smirnov 	if (err < 0)
1265b879a9c2SAndrey Smirnov 		return err;
1266b879a9c2SAndrey Smirnov 
1267b879a9c2SAndrey Smirnov 	return simple_read_from_buffer(user_buf, count, ppos, &report,
1268b879a9c2SAndrey Smirnov 				       sizeof(report));
1269b879a9c2SAndrey Smirnov }
1270b879a9c2SAndrey Smirnov 
1271b879a9c2SAndrey Smirnov static const struct file_operations radio_agc_fops = {
1272b879a9c2SAndrey Smirnov 	.open	= simple_open,
1273b879a9c2SAndrey Smirnov 	.llseek = default_llseek,
1274b879a9c2SAndrey Smirnov 	.read	= si476x_radio_read_agc_blob,
1275b879a9c2SAndrey Smirnov };
1276b879a9c2SAndrey Smirnov 
1277b879a9c2SAndrey Smirnov static ssize_t si476x_radio_read_rsq_blob(struct file *file,
1278b879a9c2SAndrey Smirnov 					  char __user *user_buf,
1279b879a9c2SAndrey Smirnov 					  size_t count, loff_t *ppos)
1280b879a9c2SAndrey Smirnov {
1281b879a9c2SAndrey Smirnov 	int err;
1282b879a9c2SAndrey Smirnov 	struct si476x_radio *radio = file->private_data;
1283b879a9c2SAndrey Smirnov 	struct si476x_rsq_status_report report;
1284b879a9c2SAndrey Smirnov 	struct si476x_rsq_status_args args = {
1285b879a9c2SAndrey Smirnov 		.primary	= false,
1286b879a9c2SAndrey Smirnov 		.rsqack		= false,
1287b879a9c2SAndrey Smirnov 		.attune		= false,
1288b879a9c2SAndrey Smirnov 		.cancel		= false,
1289b879a9c2SAndrey Smirnov 		.stcack		= false,
1290b879a9c2SAndrey Smirnov 	};
1291b879a9c2SAndrey Smirnov 
1292b879a9c2SAndrey Smirnov 	si476x_core_lock(radio->core);
1293b879a9c2SAndrey Smirnov 	if (radio->ops->rds_blckcnt)
1294b879a9c2SAndrey Smirnov 		err = radio->ops->rsq_status(radio->core, &args, &report);
1295b879a9c2SAndrey Smirnov 	else
1296b879a9c2SAndrey Smirnov 		err = -ENOENT;
1297b879a9c2SAndrey Smirnov 	si476x_core_unlock(radio->core);
1298b879a9c2SAndrey Smirnov 
1299b879a9c2SAndrey Smirnov 	if (err < 0)
1300b879a9c2SAndrey Smirnov 		return err;
1301b879a9c2SAndrey Smirnov 
1302b879a9c2SAndrey Smirnov 	return simple_read_from_buffer(user_buf, count, ppos, &report,
1303b879a9c2SAndrey Smirnov 				       sizeof(report));
1304b879a9c2SAndrey Smirnov }
1305b879a9c2SAndrey Smirnov 
1306b879a9c2SAndrey Smirnov static const struct file_operations radio_rsq_fops = {
1307b879a9c2SAndrey Smirnov 	.open	= simple_open,
1308b879a9c2SAndrey Smirnov 	.llseek = default_llseek,
1309b879a9c2SAndrey Smirnov 	.read	= si476x_radio_read_rsq_blob,
1310b879a9c2SAndrey Smirnov };
1311b879a9c2SAndrey Smirnov 
1312b879a9c2SAndrey Smirnov static ssize_t si476x_radio_read_rsq_primary_blob(struct file *file,
1313b879a9c2SAndrey Smirnov 						  char __user *user_buf,
1314b879a9c2SAndrey Smirnov 						  size_t count, loff_t *ppos)
1315b879a9c2SAndrey Smirnov {
1316b879a9c2SAndrey Smirnov 	int err;
1317b879a9c2SAndrey Smirnov 	struct si476x_radio *radio = file->private_data;
1318b879a9c2SAndrey Smirnov 	struct si476x_rsq_status_report report;
1319b879a9c2SAndrey Smirnov 	struct si476x_rsq_status_args args = {
1320b879a9c2SAndrey Smirnov 		.primary	= true,
1321b879a9c2SAndrey Smirnov 		.rsqack		= false,
1322b879a9c2SAndrey Smirnov 		.attune		= false,
1323b879a9c2SAndrey Smirnov 		.cancel		= false,
1324b879a9c2SAndrey Smirnov 		.stcack		= false,
1325b879a9c2SAndrey Smirnov 	};
1326b879a9c2SAndrey Smirnov 
1327b879a9c2SAndrey Smirnov 	si476x_core_lock(radio->core);
1328b879a9c2SAndrey Smirnov 	if (radio->ops->rds_blckcnt)
1329b879a9c2SAndrey Smirnov 		err = radio->ops->rsq_status(radio->core, &args, &report);
1330b879a9c2SAndrey Smirnov 	else
1331b879a9c2SAndrey Smirnov 		err = -ENOENT;
1332b879a9c2SAndrey Smirnov 	si476x_core_unlock(radio->core);
1333b879a9c2SAndrey Smirnov 
1334b879a9c2SAndrey Smirnov 	if (err < 0)
1335b879a9c2SAndrey Smirnov 		return err;
1336b879a9c2SAndrey Smirnov 
1337b879a9c2SAndrey Smirnov 	return simple_read_from_buffer(user_buf, count, ppos, &report,
1338b879a9c2SAndrey Smirnov 				       sizeof(report));
1339b879a9c2SAndrey Smirnov }
1340b879a9c2SAndrey Smirnov 
1341b879a9c2SAndrey Smirnov static const struct file_operations radio_rsq_primary_fops = {
1342b879a9c2SAndrey Smirnov 	.open	= simple_open,
1343b879a9c2SAndrey Smirnov 	.llseek = default_llseek,
1344b879a9c2SAndrey Smirnov 	.read	= si476x_radio_read_rsq_primary_blob,
1345b879a9c2SAndrey Smirnov };
1346b879a9c2SAndrey Smirnov 
1347b879a9c2SAndrey Smirnov 
13481d8f95c4SGreg Kroah-Hartman static void si476x_radio_init_debugfs(struct si476x_radio *radio)
1349b879a9c2SAndrey Smirnov {
13501d8f95c4SGreg Kroah-Hartman 	radio->debugfs = debugfs_create_dir(dev_name(radio->v4l2dev.dev), NULL);
1351b879a9c2SAndrey Smirnov 
13521d8f95c4SGreg Kroah-Hartman 	debugfs_create_file("acf", S_IRUGO, radio->debugfs, radio,
13531d8f95c4SGreg Kroah-Hartman 			    &radio_acf_fops);
1354b879a9c2SAndrey Smirnov 
13551d8f95c4SGreg Kroah-Hartman 	debugfs_create_file("rds_blckcnt", S_IRUGO, radio->debugfs, radio,
1356b879a9c2SAndrey Smirnov 			    &radio_rds_blckcnt_fops);
1357b879a9c2SAndrey Smirnov 
13581d8f95c4SGreg Kroah-Hartman 	debugfs_create_file("agc", S_IRUGO, radio->debugfs, radio,
13591d8f95c4SGreg Kroah-Hartman 			    &radio_agc_fops);
1360b879a9c2SAndrey Smirnov 
13611d8f95c4SGreg Kroah-Hartman 	debugfs_create_file("rsq", S_IRUGO, radio->debugfs, radio,
13621d8f95c4SGreg Kroah-Hartman 			    &radio_rsq_fops);
1363b879a9c2SAndrey Smirnov 
13641d8f95c4SGreg Kroah-Hartman 	debugfs_create_file("rsq_primary", S_IRUGO, radio->debugfs, radio,
1365b879a9c2SAndrey Smirnov 			    &radio_rsq_primary_fops);
1366b879a9c2SAndrey Smirnov }
1367b879a9c2SAndrey Smirnov 
1368b879a9c2SAndrey Smirnov 
1369b879a9c2SAndrey Smirnov static int si476x_radio_add_new_custom(struct si476x_radio *radio,
1370b879a9c2SAndrey Smirnov 				       enum si476x_ctrl_idx idx)
1371b879a9c2SAndrey Smirnov {
1372b879a9c2SAndrey Smirnov 	int rval;
1373b879a9c2SAndrey Smirnov 	struct v4l2_ctrl *ctrl;
1374b879a9c2SAndrey Smirnov 
1375b879a9c2SAndrey Smirnov 	ctrl = v4l2_ctrl_new_custom(&radio->ctrl_handler,
1376b879a9c2SAndrey Smirnov 				    &si476x_ctrls[idx],
1377b879a9c2SAndrey Smirnov 				    NULL);
1378b879a9c2SAndrey Smirnov 	rval = radio->ctrl_handler.error;
1379b879a9c2SAndrey Smirnov 	if (ctrl == NULL && rval)
1380b879a9c2SAndrey Smirnov 		dev_err(radio->v4l2dev.dev,
1381b879a9c2SAndrey Smirnov 			"Could not initialize '%s' control %d\n",
1382b879a9c2SAndrey Smirnov 			si476x_ctrls[idx].name, rval);
1383b879a9c2SAndrey Smirnov 
1384b879a9c2SAndrey Smirnov 	return rval;
1385b879a9c2SAndrey Smirnov }
1386b879a9c2SAndrey Smirnov 
1387b879a9c2SAndrey Smirnov static int si476x_radio_probe(struct platform_device *pdev)
1388b879a9c2SAndrey Smirnov {
1389b879a9c2SAndrey Smirnov 	int rval;
1390b879a9c2SAndrey Smirnov 	struct si476x_radio *radio;
1391b879a9c2SAndrey Smirnov 	struct v4l2_ctrl *ctrl;
1392b879a9c2SAndrey Smirnov 
1393b879a9c2SAndrey Smirnov 	static atomic_t instance = ATOMIC_INIT(0);
1394b879a9c2SAndrey Smirnov 
1395b879a9c2SAndrey Smirnov 	radio = devm_kzalloc(&pdev->dev, sizeof(*radio), GFP_KERNEL);
1396b879a9c2SAndrey Smirnov 	if (!radio)
1397b879a9c2SAndrey Smirnov 		return -ENOMEM;
1398b879a9c2SAndrey Smirnov 
1399b879a9c2SAndrey Smirnov 	radio->core = i2c_mfd_cell_to_core(&pdev->dev);
1400b879a9c2SAndrey Smirnov 
1401b879a9c2SAndrey Smirnov 	v4l2_device_set_name(&radio->v4l2dev, DRIVER_NAME, &instance);
1402b879a9c2SAndrey Smirnov 
1403b879a9c2SAndrey Smirnov 	rval = v4l2_device_register(&pdev->dev, &radio->v4l2dev);
1404b879a9c2SAndrey Smirnov 	if (rval) {
1405b879a9c2SAndrey Smirnov 		dev_err(&pdev->dev, "Cannot register v4l2_device.\n");
1406b879a9c2SAndrey Smirnov 		return rval;
1407b879a9c2SAndrey Smirnov 	}
1408b879a9c2SAndrey Smirnov 
1409b879a9c2SAndrey Smirnov 	memcpy(&radio->videodev, &si476x_viddev_template,
1410b879a9c2SAndrey Smirnov 	       sizeof(struct video_device));
1411b879a9c2SAndrey Smirnov 
1412b879a9c2SAndrey Smirnov 	radio->videodev.v4l2_dev  = &radio->v4l2dev;
1413b879a9c2SAndrey Smirnov 	radio->videodev.ioctl_ops = &si4761_ioctl_ops;
1414e83ce300SHans Verkuil 	radio->videodev.device_caps = V4L2_CAP_TUNER | V4L2_CAP_RADIO |
1415e83ce300SHans Verkuil 				      V4L2_CAP_HW_FREQ_SEEK;
1416e83ce300SHans Verkuil 
1417e83ce300SHans Verkuil 	si476x_core_lock(radio->core);
1418e83ce300SHans Verkuil 	if (!si476x_core_is_a_secondary_tuner(radio->core))
1419e83ce300SHans Verkuil 		radio->videodev.device_caps |= V4L2_CAP_RDS_CAPTURE |
1420e83ce300SHans Verkuil 					       V4L2_CAP_READWRITE;
1421e83ce300SHans Verkuil 	si476x_core_unlock(radio->core);
1422b879a9c2SAndrey Smirnov 
1423b879a9c2SAndrey Smirnov 	video_set_drvdata(&radio->videodev, radio);
1424b879a9c2SAndrey Smirnov 	platform_set_drvdata(pdev, radio);
1425b879a9c2SAndrey Smirnov 
1426b879a9c2SAndrey Smirnov 
1427b879a9c2SAndrey Smirnov 	radio->v4l2dev.ctrl_handler = &radio->ctrl_handler;
1428b879a9c2SAndrey Smirnov 	v4l2_ctrl_handler_init(&radio->ctrl_handler,
1429b879a9c2SAndrey Smirnov 			       1 + ARRAY_SIZE(si476x_ctrls));
1430b879a9c2SAndrey Smirnov 
1431b879a9c2SAndrey Smirnov 	if (si476x_core_has_am(radio->core)) {
1432b879a9c2SAndrey Smirnov 		ctrl = v4l2_ctrl_new_std_menu(&radio->ctrl_handler,
1433b879a9c2SAndrey Smirnov 					      &si476x_ctrl_ops,
1434b879a9c2SAndrey Smirnov 					      V4L2_CID_POWER_LINE_FREQUENCY,
1435b879a9c2SAndrey Smirnov 					      V4L2_CID_POWER_LINE_FREQUENCY_60HZ,
1436b879a9c2SAndrey Smirnov 					      0, 0);
1437b879a9c2SAndrey Smirnov 		rval = radio->ctrl_handler.error;
1438b879a9c2SAndrey Smirnov 		if (ctrl == NULL && rval) {
1439b879a9c2SAndrey Smirnov 			dev_err(&pdev->dev, "Could not initialize V4L2_CID_POWER_LINE_FREQUENCY control %d\n",
1440b879a9c2SAndrey Smirnov 				rval);
1441b879a9c2SAndrey Smirnov 			goto exit;
1442b879a9c2SAndrey Smirnov 		}
1443b879a9c2SAndrey Smirnov 
1444b879a9c2SAndrey Smirnov 		rval = si476x_radio_add_new_custom(radio,
1445b879a9c2SAndrey Smirnov 						   SI476X_IDX_HARMONICS_COUNT);
1446b879a9c2SAndrey Smirnov 		if (rval < 0)
1447b879a9c2SAndrey Smirnov 			goto exit;
1448b879a9c2SAndrey Smirnov 	}
1449b879a9c2SAndrey Smirnov 
1450b879a9c2SAndrey Smirnov 	rval = si476x_radio_add_new_custom(radio, SI476X_IDX_RSSI_THRESHOLD);
1451b879a9c2SAndrey Smirnov 	if (rval < 0)
1452b879a9c2SAndrey Smirnov 		goto exit;
1453b879a9c2SAndrey Smirnov 
1454b879a9c2SAndrey Smirnov 	rval = si476x_radio_add_new_custom(radio, SI476X_IDX_SNR_THRESHOLD);
1455b879a9c2SAndrey Smirnov 	if (rval < 0)
1456b879a9c2SAndrey Smirnov 		goto exit;
1457b879a9c2SAndrey Smirnov 
1458b879a9c2SAndrey Smirnov 	rval = si476x_radio_add_new_custom(radio, SI476X_IDX_MAX_TUNE_ERROR);
1459b879a9c2SAndrey Smirnov 	if (rval < 0)
1460b879a9c2SAndrey Smirnov 		goto exit;
1461b879a9c2SAndrey Smirnov 
1462b879a9c2SAndrey Smirnov 	ctrl = v4l2_ctrl_new_std_menu(&radio->ctrl_handler,
1463b879a9c2SAndrey Smirnov 				      &si476x_ctrl_ops,
1464b879a9c2SAndrey Smirnov 				      V4L2_CID_TUNE_DEEMPHASIS,
1465b879a9c2SAndrey Smirnov 				      V4L2_DEEMPHASIS_75_uS, 0, 0);
1466b879a9c2SAndrey Smirnov 	rval = radio->ctrl_handler.error;
1467b879a9c2SAndrey Smirnov 	if (ctrl == NULL && rval) {
1468b879a9c2SAndrey Smirnov 		dev_err(&pdev->dev, "Could not initialize V4L2_CID_TUNE_DEEMPHASIS control %d\n",
1469b879a9c2SAndrey Smirnov 			rval);
1470b879a9c2SAndrey Smirnov 		goto exit;
1471b879a9c2SAndrey Smirnov 	}
1472b879a9c2SAndrey Smirnov 
1473b879a9c2SAndrey Smirnov 	ctrl = v4l2_ctrl_new_std(&radio->ctrl_handler, &si476x_ctrl_ops,
1474b879a9c2SAndrey Smirnov 				 V4L2_CID_RDS_RECEPTION,
1475b879a9c2SAndrey Smirnov 				 0, 1, 1, 1);
1476b879a9c2SAndrey Smirnov 	rval = radio->ctrl_handler.error;
1477b879a9c2SAndrey Smirnov 	if (ctrl == NULL && rval) {
1478b879a9c2SAndrey Smirnov 		dev_err(&pdev->dev, "Could not initialize V4L2_CID_RDS_RECEPTION control %d\n",
1479b879a9c2SAndrey Smirnov 			rval);
1480b879a9c2SAndrey Smirnov 		goto exit;
1481b879a9c2SAndrey Smirnov 	}
1482b879a9c2SAndrey Smirnov 
1483b879a9c2SAndrey Smirnov 	if (si476x_core_has_diversity(radio->core)) {
1484b879a9c2SAndrey Smirnov 		si476x_ctrls[SI476X_IDX_DIVERSITY_MODE].def =
1485b879a9c2SAndrey Smirnov 			si476x_phase_diversity_mode_to_idx(radio->core->diversity_mode);
1486949cf706SFugang Duan 		rval = si476x_radio_add_new_custom(radio, SI476X_IDX_DIVERSITY_MODE);
1487b879a9c2SAndrey Smirnov 		if (rval < 0)
1488b879a9c2SAndrey Smirnov 			goto exit;
1489b879a9c2SAndrey Smirnov 
1490949cf706SFugang Duan 		rval = si476x_radio_add_new_custom(radio, SI476X_IDX_INTERCHIP_LINK);
1491b879a9c2SAndrey Smirnov 		if (rval < 0)
1492b879a9c2SAndrey Smirnov 			goto exit;
1493b879a9c2SAndrey Smirnov 	}
1494b879a9c2SAndrey Smirnov 
1495b879a9c2SAndrey Smirnov 	/* register video device */
1496b879a9c2SAndrey Smirnov 	rval = video_register_device(&radio->videodev, VFL_TYPE_RADIO, -1);
1497b879a9c2SAndrey Smirnov 	if (rval < 0) {
1498b879a9c2SAndrey Smirnov 		dev_err(&pdev->dev, "Could not register video device\n");
1499b879a9c2SAndrey Smirnov 		goto exit;
1500b879a9c2SAndrey Smirnov 	}
1501b879a9c2SAndrey Smirnov 
15021d8f95c4SGreg Kroah-Hartman 	si476x_radio_init_debugfs(radio);
1503b879a9c2SAndrey Smirnov 
1504b879a9c2SAndrey Smirnov 	return 0;
1505b879a9c2SAndrey Smirnov exit:
1506b879a9c2SAndrey Smirnov 	v4l2_ctrl_handler_free(radio->videodev.ctrl_handler);
1507b879a9c2SAndrey Smirnov 	return rval;
1508b879a9c2SAndrey Smirnov }
1509b879a9c2SAndrey Smirnov 
1510b879a9c2SAndrey Smirnov static int si476x_radio_remove(struct platform_device *pdev)
1511b879a9c2SAndrey Smirnov {
1512b879a9c2SAndrey Smirnov 	struct si476x_radio *radio = platform_get_drvdata(pdev);
1513b879a9c2SAndrey Smirnov 
1514b879a9c2SAndrey Smirnov 	v4l2_ctrl_handler_free(radio->videodev.ctrl_handler);
1515b879a9c2SAndrey Smirnov 	video_unregister_device(&radio->videodev);
1516b879a9c2SAndrey Smirnov 	v4l2_device_unregister(&radio->v4l2dev);
1517b879a9c2SAndrey Smirnov 	debugfs_remove_recursive(radio->debugfs);
1518b879a9c2SAndrey Smirnov 
1519b879a9c2SAndrey Smirnov 	return 0;
1520b879a9c2SAndrey Smirnov }
1521b879a9c2SAndrey Smirnov 
1522b879a9c2SAndrey Smirnov MODULE_ALIAS("platform:si476x-radio");
1523b879a9c2SAndrey Smirnov 
1524b879a9c2SAndrey Smirnov static struct platform_driver si476x_radio_driver = {
1525b879a9c2SAndrey Smirnov 	.driver		= {
1526b879a9c2SAndrey Smirnov 		.name	= DRIVER_NAME,
1527b879a9c2SAndrey Smirnov 	},
1528b879a9c2SAndrey Smirnov 	.probe		= si476x_radio_probe,
1529b879a9c2SAndrey Smirnov 	.remove		= si476x_radio_remove,
1530b879a9c2SAndrey Smirnov };
1531b879a9c2SAndrey Smirnov module_platform_driver(si476x_radio_driver);
1532b879a9c2SAndrey Smirnov 
1533b879a9c2SAndrey Smirnov MODULE_AUTHOR("Andrey Smirnov <andrew.smirnov@gmail.com>");
1534b879a9c2SAndrey Smirnov MODULE_DESCRIPTION("Driver for Si4761/64/68 AM/FM Radio MFD Cell");
1535b879a9c2SAndrey Smirnov MODULE_LICENSE("GPL");
1536