xref: /linux/drivers/media/radio/si470x/radio-si470x-common.c (revision 2f73c7c582a685b3198b974cd6d964d0338f8ab5)
1b09cd163SJoonyoung Shim /*
2b09cd163SJoonyoung Shim  *  drivers/media/radio/si470x/radio-si470x-common.c
3b09cd163SJoonyoung Shim  *
4b09cd163SJoonyoung Shim  *  Driver for radios with Silicon Labs Si470x FM Radio Receivers
5b09cd163SJoonyoung Shim  *
6b09cd163SJoonyoung Shim  *  Copyright (c) 2009 Tobias Lorenz <tobias.lorenz@gmx.net>
7f140612dSHans de Goede  *  Copyright (c) 2012 Hans de Goede <hdegoede@redhat.com>
8b09cd163SJoonyoung Shim  *
9b09cd163SJoonyoung Shim  * This program is free software; you can redistribute it and/or modify
10b09cd163SJoonyoung Shim  * it under the terms of the GNU General Public License as published by
11b09cd163SJoonyoung Shim  * the Free Software Foundation; either version 2 of the License, or
12b09cd163SJoonyoung Shim  * (at your option) any later version.
13b09cd163SJoonyoung Shim  *
14b09cd163SJoonyoung Shim  * This program is distributed in the hope that it will be useful,
15b09cd163SJoonyoung Shim  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16b09cd163SJoonyoung Shim  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17b09cd163SJoonyoung Shim  * GNU General Public License for more details.
18b09cd163SJoonyoung Shim  *
19b09cd163SJoonyoung Shim  * You should have received a copy of the GNU General Public License
20b09cd163SJoonyoung Shim  * along with this program; if not, write to the Free Software
21b09cd163SJoonyoung Shim  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22b09cd163SJoonyoung Shim  */
23b09cd163SJoonyoung Shim 
24b09cd163SJoonyoung Shim 
25b09cd163SJoonyoung Shim /*
26b09cd163SJoonyoung Shim  * History:
27b09cd163SJoonyoung Shim  * 2008-01-12	Tobias Lorenz <tobias.lorenz@gmx.net>
28b09cd163SJoonyoung Shim  *		Version 1.0.0
29b09cd163SJoonyoung Shim  *		- First working version
30b09cd163SJoonyoung Shim  * 2008-01-13	Tobias Lorenz <tobias.lorenz@gmx.net>
31b09cd163SJoonyoung Shim  *		Version 1.0.1
32b09cd163SJoonyoung Shim  *		- Improved error handling, every function now returns errno
33b09cd163SJoonyoung Shim  *		- Improved multi user access (start/mute/stop)
34b09cd163SJoonyoung Shim  *		- Channel doesn't get lost anymore after start/mute/stop
35b09cd163SJoonyoung Shim  *		- RDS support added (polling mode via interrupt EP 1)
36b09cd163SJoonyoung Shim  *		- marked default module parameters with *value*
37b09cd163SJoonyoung Shim  *		- switched from bit structs to bit masks
38b09cd163SJoonyoung Shim  *		- header file cleaned and integrated
39b09cd163SJoonyoung Shim  * 2008-01-14	Tobias Lorenz <tobias.lorenz@gmx.net>
40b09cd163SJoonyoung Shim  * 		Version 1.0.2
41b09cd163SJoonyoung Shim  * 		- hex values are now lower case
42b09cd163SJoonyoung Shim  * 		- commented USB ID for ADS/Tech moved on todo list
43b09cd163SJoonyoung Shim  * 		- blacklisted si470x in hid-quirks.c
44b09cd163SJoonyoung Shim  * 		- rds buffer handling functions integrated into *_work, *_read
45b09cd163SJoonyoung Shim  * 		- rds_command in si470x_poll exchanged against simple retval
46b09cd163SJoonyoung Shim  * 		- check for firmware version 15
47b09cd163SJoonyoung Shim  * 		- code order and prototypes still remain the same
48b09cd163SJoonyoung Shim  * 		- spacing and bottom of band codes remain the same
49b09cd163SJoonyoung Shim  * 2008-01-16	Tobias Lorenz <tobias.lorenz@gmx.net>
50b09cd163SJoonyoung Shim  *		Version 1.0.3
51b09cd163SJoonyoung Shim  * 		- code reordered to avoid function prototypes
52b09cd163SJoonyoung Shim  *		- switch/case defaults are now more user-friendly
53b09cd163SJoonyoung Shim  *		- unified comment style
54b09cd163SJoonyoung Shim  *		- applied all checkpatch.pl v1.12 suggestions
55b09cd163SJoonyoung Shim  *		  except the warning about the too long lines with bit comments
56b09cd163SJoonyoung Shim  *		- renamed FMRADIO to RADIO to cut line length (checkpatch.pl)
57b09cd163SJoonyoung Shim  * 2008-01-22	Tobias Lorenz <tobias.lorenz@gmx.net>
58b09cd163SJoonyoung Shim  *		Version 1.0.4
59b09cd163SJoonyoung Shim  *		- avoid poss. locking when doing copy_to_user which may sleep
60b09cd163SJoonyoung Shim  *		- RDS is automatically activated on read now
61b09cd163SJoonyoung Shim  *		- code cleaned of unnecessary rds_commands
62b09cd163SJoonyoung Shim  *		- USB Vendor/Product ID for ADS/Tech FM Radio Receiver verified
63b09cd163SJoonyoung Shim  *		  (thanks to Guillaume RAMOUSSE)
64b09cd163SJoonyoung Shim  * 2008-01-27	Tobias Lorenz <tobias.lorenz@gmx.net>
65b09cd163SJoonyoung Shim  *		Version 1.0.5
66b09cd163SJoonyoung Shim  *		- number of seek_retries changed to tune_timeout
67b09cd163SJoonyoung Shim  *		- fixed problem with incomplete tune operations by own buffers
68b09cd163SJoonyoung Shim  *		- optimization of variables and printf types
69b09cd163SJoonyoung Shim  *		- improved error logging
70b09cd163SJoonyoung Shim  * 2008-01-31	Tobias Lorenz <tobias.lorenz@gmx.net>
71b09cd163SJoonyoung Shim  *		Oliver Neukum <oliver@neukum.org>
72b09cd163SJoonyoung Shim  *		Version 1.0.6
73b09cd163SJoonyoung Shim  *		- fixed coverity checker warnings in *_usb_driver_disconnect
74b09cd163SJoonyoung Shim  *		- probe()/open() race by correct ordering in probe()
75b09cd163SJoonyoung Shim  *		- DMA coherency rules by separate allocation of all buffers
76b09cd163SJoonyoung Shim  *		- use of endianness macros
77b09cd163SJoonyoung Shim  *		- abuse of spinlock, replaced by mutex
78b09cd163SJoonyoung Shim  *		- racy handling of timer in disconnect,
79b09cd163SJoonyoung Shim  *		  replaced by delayed_work
80b09cd163SJoonyoung Shim  *		- racy interruptible_sleep_on(),
81b09cd163SJoonyoung Shim  *		  replaced with wait_event_interruptible()
82b09cd163SJoonyoung Shim  *		- handle signals in read()
83b09cd163SJoonyoung Shim  * 2008-02-08	Tobias Lorenz <tobias.lorenz@gmx.net>
84b09cd163SJoonyoung Shim  *		Oliver Neukum <oliver@neukum.org>
85b09cd163SJoonyoung Shim  *		Version 1.0.7
86b09cd163SJoonyoung Shim  *		- usb autosuspend support
87b09cd163SJoonyoung Shim  *		- unplugging fixed
88b09cd163SJoonyoung Shim  * 2008-05-07	Tobias Lorenz <tobias.lorenz@gmx.net>
89b09cd163SJoonyoung Shim  *		Version 1.0.8
90b09cd163SJoonyoung Shim  *		- hardware frequency seek support
91b09cd163SJoonyoung Shim  *		- afc indication
92b09cd163SJoonyoung Shim  *		- more safety checks, let si470x_get_freq return errno
93b09cd163SJoonyoung Shim  *		- vidioc behavior corrected according to v4l2 spec
94b09cd163SJoonyoung Shim  * 2008-10-20	Alexey Klimov <klimov.linux@gmail.com>
95b09cd163SJoonyoung Shim  * 		- add support for KWorld USB FM Radio FM700
96b09cd163SJoonyoung Shim  * 		- blacklisted KWorld radio in hid-core.c and hid-ids.h
97b09cd163SJoonyoung Shim  * 2008-12-03	Mark Lord <mlord@pobox.com>
98b09cd163SJoonyoung Shim  *		- add support for DealExtreme USB Radio
99b09cd163SJoonyoung Shim  * 2009-01-31	Bob Ross <pigiron@gmx.com>
100b09cd163SJoonyoung Shim  *		- correction of stereo detection/setting
101b09cd163SJoonyoung Shim  *		- correction of signal strength indicator scaling
102b09cd163SJoonyoung Shim  * 2009-01-31	Rick Bronson <rick@efn.org>
103b09cd163SJoonyoung Shim  *		Tobias Lorenz <tobias.lorenz@gmx.net>
104b09cd163SJoonyoung Shim  *		- add LED status output
105b09cd163SJoonyoung Shim  *		- get HW/SW version from scratchpad
106b09cd163SJoonyoung Shim  * 2009-06-16   Edouard Lafargue <edouard@lafargue.name>
107b09cd163SJoonyoung Shim  *		Version 1.0.10
108b09cd163SJoonyoung Shim  *		- add support for interrupt mode for RDS endpoint,
109b09cd163SJoonyoung Shim  *                instead of polling.
110b09cd163SJoonyoung Shim  *                Improves RDS reception significantly
111b09cd163SJoonyoung Shim  */
112b09cd163SJoonyoung Shim 
113b09cd163SJoonyoung Shim 
114b09cd163SJoonyoung Shim /* kernel includes */
115b09cd163SJoonyoung Shim #include "radio-si470x.h"
116b09cd163SJoonyoung Shim 
117b09cd163SJoonyoung Shim 
118b09cd163SJoonyoung Shim 
119b09cd163SJoonyoung Shim /**************************************************************************
120b09cd163SJoonyoung Shim  * Module Parameters
121b09cd163SJoonyoung Shim  **************************************************************************/
122b09cd163SJoonyoung Shim 
123b09cd163SJoonyoung Shim /* Spacing (kHz) */
124b09cd163SJoonyoung Shim /* 0: 200 kHz (USA, Australia) */
125b09cd163SJoonyoung Shim /* 1: 100 kHz (Europe, Japan) */
126b09cd163SJoonyoung Shim /* 2:  50 kHz */
127b09cd163SJoonyoung Shim static unsigned short space = 2;
128b09cd163SJoonyoung Shim module_param(space, ushort, 0444);
129b09cd163SJoonyoung Shim MODULE_PARM_DESC(space, "Spacing: 0=200kHz 1=100kHz *2=50kHz*");
130b09cd163SJoonyoung Shim 
131b09cd163SJoonyoung Shim /* De-emphasis */
132b09cd163SJoonyoung Shim /* 0: 75 us (USA) */
133b09cd163SJoonyoung Shim /* 1: 50 us (Europe, Australia, Japan) */
134b09cd163SJoonyoung Shim static unsigned short de = 1;
135b09cd163SJoonyoung Shim module_param(de, ushort, 0444);
136b09cd163SJoonyoung Shim MODULE_PARM_DESC(de, "De-emphasis: 0=75us *1=50us*");
137b09cd163SJoonyoung Shim 
138b09cd163SJoonyoung Shim /* Tune timeout */
139b09cd163SJoonyoung Shim static unsigned int tune_timeout = 3000;
140b09cd163SJoonyoung Shim module_param(tune_timeout, uint, 0644);
141b09cd163SJoonyoung Shim MODULE_PARM_DESC(tune_timeout, "Tune timeout: *3000*");
142b09cd163SJoonyoung Shim 
143b09cd163SJoonyoung Shim /* Seek timeout */
144b09cd163SJoonyoung Shim static unsigned int seek_timeout = 5000;
145b09cd163SJoonyoung Shim module_param(seek_timeout, uint, 0644);
146b09cd163SJoonyoung Shim MODULE_PARM_DESC(seek_timeout, "Seek timeout: *5000*");
147b09cd163SJoonyoung Shim 
148f140612dSHans de Goede static const struct v4l2_frequency_band bands[] = {
149f140612dSHans de Goede 	{
150f140612dSHans de Goede 		.type = V4L2_TUNER_RADIO,
151f140612dSHans de Goede 		.index = 0,
152f140612dSHans de Goede 		.capability = V4L2_TUNER_CAP_LOW | V4L2_TUNER_CAP_STEREO |
153f140612dSHans de Goede 			    V4L2_TUNER_CAP_RDS | V4L2_TUNER_CAP_RDS_BLOCK_IO |
15432737810SHans Verkuil 			    V4L2_TUNER_CAP_FREQ_BANDS |
155f140612dSHans de Goede 			    V4L2_TUNER_CAP_HWSEEK_BOUNDED |
156f140612dSHans de Goede 			    V4L2_TUNER_CAP_HWSEEK_WRAP,
157f140612dSHans de Goede 		.rangelow   =  87500 * 16,
158f140612dSHans de Goede 		.rangehigh  = 108000 * 16,
159f140612dSHans de Goede 		.modulation = V4L2_BAND_MODULATION_FM,
160f140612dSHans de Goede 	},
161f140612dSHans de Goede 	{
162f140612dSHans de Goede 		.type = V4L2_TUNER_RADIO,
163f140612dSHans de Goede 		.index = 1,
164f140612dSHans de Goede 		.capability = V4L2_TUNER_CAP_LOW | V4L2_TUNER_CAP_STEREO |
165f140612dSHans de Goede 			    V4L2_TUNER_CAP_RDS | V4L2_TUNER_CAP_RDS_BLOCK_IO |
16632737810SHans Verkuil 			    V4L2_TUNER_CAP_FREQ_BANDS |
167f140612dSHans de Goede 			    V4L2_TUNER_CAP_HWSEEK_BOUNDED |
168f140612dSHans de Goede 			    V4L2_TUNER_CAP_HWSEEK_WRAP,
169f140612dSHans de Goede 		.rangelow   =  76000 * 16,
170f140612dSHans de Goede 		.rangehigh  = 108000 * 16,
171f140612dSHans de Goede 		.modulation = V4L2_BAND_MODULATION_FM,
172f140612dSHans de Goede 	},
173f140612dSHans de Goede 	{
174f140612dSHans de Goede 		.type = V4L2_TUNER_RADIO,
175f140612dSHans de Goede 		.index = 2,
176f140612dSHans de Goede 		.capability = V4L2_TUNER_CAP_LOW | V4L2_TUNER_CAP_STEREO |
177f140612dSHans de Goede 			    V4L2_TUNER_CAP_RDS | V4L2_TUNER_CAP_RDS_BLOCK_IO |
17832737810SHans Verkuil 			    V4L2_TUNER_CAP_FREQ_BANDS |
179f140612dSHans de Goede 			    V4L2_TUNER_CAP_HWSEEK_BOUNDED |
180f140612dSHans de Goede 			    V4L2_TUNER_CAP_HWSEEK_WRAP,
181f140612dSHans de Goede 		.rangelow   =  76000 * 16,
182f140612dSHans de Goede 		.rangehigh  =  90000 * 16,
183f140612dSHans de Goede 		.modulation = V4L2_BAND_MODULATION_FM,
184f140612dSHans de Goede 	},
185f140612dSHans de Goede };
186b09cd163SJoonyoung Shim 
187b09cd163SJoonyoung Shim /**************************************************************************
188b09cd163SJoonyoung Shim  * Generic Functions
189b09cd163SJoonyoung Shim  **************************************************************************/
190b09cd163SJoonyoung Shim 
191b09cd163SJoonyoung Shim /*
192f140612dSHans de Goede  * si470x_set_band - set the band
193f140612dSHans de Goede  */
194f140612dSHans de Goede static int si470x_set_band(struct si470x_device *radio, int band)
195f140612dSHans de Goede {
196f140612dSHans de Goede 	if (radio->band == band)
197f140612dSHans de Goede 		return 0;
198f140612dSHans de Goede 
199f140612dSHans de Goede 	radio->band = band;
200f140612dSHans de Goede 	radio->registers[SYSCONFIG2] &= ~SYSCONFIG2_BAND;
201f140612dSHans de Goede 	radio->registers[SYSCONFIG2] |= radio->band << 6;
202f140612dSHans de Goede 	return si470x_set_register(radio, SYSCONFIG2);
203f140612dSHans de Goede }
204f140612dSHans de Goede 
205f140612dSHans de Goede /*
206b09cd163SJoonyoung Shim  * si470x_set_chan - set the channel
207b09cd163SJoonyoung Shim  */
208b09cd163SJoonyoung Shim static int si470x_set_chan(struct si470x_device *radio, unsigned short chan)
209b09cd163SJoonyoung Shim {
210b09cd163SJoonyoung Shim 	int retval;
211b09cd163SJoonyoung Shim 	bool timed_out = 0;
212b09cd163SJoonyoung Shim 
213b09cd163SJoonyoung Shim 	/* start tuning */
214b09cd163SJoonyoung Shim 	radio->registers[CHANNEL] &= ~CHANNEL_CHAN;
215b09cd163SJoonyoung Shim 	radio->registers[CHANNEL] |= CHANNEL_TUNE | chan;
216b09cd163SJoonyoung Shim 	retval = si470x_set_register(radio, CHANNEL);
217b09cd163SJoonyoung Shim 	if (retval < 0)
218b09cd163SJoonyoung Shim 		goto done;
219b09cd163SJoonyoung Shim 
2200830be3fSJoonyoung Shim 	/* wait till tune operation has completed */
22177947111SHans de Goede 	INIT_COMPLETION(radio->completion);
2220830be3fSJoonyoung Shim 	retval = wait_for_completion_timeout(&radio->completion,
2230830be3fSJoonyoung Shim 			msecs_to_jiffies(tune_timeout));
2240830be3fSJoonyoung Shim 	if (!retval)
2250830be3fSJoonyoung Shim 		timed_out = true;
2260830be3fSJoonyoung Shim 
227b09cd163SJoonyoung Shim 	if ((radio->registers[STATUSRSSI] & STATUSRSSI_STC) == 0)
2284967d53dSHans Verkuil 		dev_warn(&radio->videodev.dev, "tune does not complete\n");
229b09cd163SJoonyoung Shim 	if (timed_out)
2304967d53dSHans Verkuil 		dev_warn(&radio->videodev.dev,
231a9d6fd5eSJoonyoung Shim 			"tune timed out after %u ms\n", tune_timeout);
232b09cd163SJoonyoung Shim 
233b09cd163SJoonyoung Shim 	/* stop tuning */
234b09cd163SJoonyoung Shim 	radio->registers[CHANNEL] &= ~CHANNEL_TUNE;
235b09cd163SJoonyoung Shim 	retval = si470x_set_register(radio, CHANNEL);
236b09cd163SJoonyoung Shim 
237b09cd163SJoonyoung Shim done:
238b09cd163SJoonyoung Shim 	return retval;
239b09cd163SJoonyoung Shim }
240b09cd163SJoonyoung Shim 
241f140612dSHans de Goede /*
242f140612dSHans de Goede  * si470x_get_step - get channel spacing
243f140612dSHans de Goede  */
244f140612dSHans de Goede static unsigned int si470x_get_step(struct si470x_device *radio)
245f140612dSHans de Goede {
246f140612dSHans de Goede 	/* Spacing (kHz) */
247f140612dSHans de Goede 	switch ((radio->registers[SYSCONFIG2] & SYSCONFIG2_SPACE) >> 4) {
248f140612dSHans de Goede 	/* 0: 200 kHz (USA, Australia) */
249f140612dSHans de Goede 	case 0:
250f140612dSHans de Goede 		return 200 * 16;
251f140612dSHans de Goede 	/* 1: 100 kHz (Europe, Japan) */
252f140612dSHans de Goede 	case 1:
253f140612dSHans de Goede 		return 100 * 16;
254f140612dSHans de Goede 	/* 2:  50 kHz */
255f140612dSHans de Goede 	default:
256f140612dSHans de Goede 		return 50 * 16;
257f140612dSHans de Goede 	};
258f140612dSHans de Goede }
259f140612dSHans de Goede 
260b09cd163SJoonyoung Shim 
261b09cd163SJoonyoung Shim /*
262b09cd163SJoonyoung Shim  * si470x_get_freq - get the frequency
263b09cd163SJoonyoung Shim  */
264b09cd163SJoonyoung Shim static int si470x_get_freq(struct si470x_device *radio, unsigned int *freq)
265b09cd163SJoonyoung Shim {
266f140612dSHans de Goede 	int chan, retval;
267b09cd163SJoonyoung Shim 
268b09cd163SJoonyoung Shim 	/* read channel */
269b09cd163SJoonyoung Shim 	retval = si470x_get_register(radio, READCHAN);
270b09cd163SJoonyoung Shim 	chan = radio->registers[READCHAN] & READCHAN_READCHAN;
271b09cd163SJoonyoung Shim 
272b09cd163SJoonyoung Shim 	/* Frequency (MHz) = Spacing (kHz) x Channel + Bottom of Band (MHz) */
273f140612dSHans de Goede 	*freq = chan * si470x_get_step(radio) + bands[radio->band].rangelow;
274b09cd163SJoonyoung Shim 
275b09cd163SJoonyoung Shim 	return retval;
276b09cd163SJoonyoung Shim }
277b09cd163SJoonyoung Shim 
278b09cd163SJoonyoung Shim 
279b09cd163SJoonyoung Shim /*
280b09cd163SJoonyoung Shim  * si470x_set_freq - set the frequency
281b09cd163SJoonyoung Shim  */
282b09cd163SJoonyoung Shim int si470x_set_freq(struct si470x_device *radio, unsigned int freq)
283b09cd163SJoonyoung Shim {
284b09cd163SJoonyoung Shim 	unsigned short chan;
285b09cd163SJoonyoung Shim 
286f140612dSHans de Goede 	freq = clamp(freq, bands[radio->band].rangelow,
287f140612dSHans de Goede 			   bands[radio->band].rangehigh);
288b09cd163SJoonyoung Shim 	/* Chan = [ Freq (Mhz) - Bottom of Band (MHz) ] / Spacing (kHz) */
289f140612dSHans de Goede 	chan = (freq - bands[radio->band].rangelow) / si470x_get_step(radio);
290b09cd163SJoonyoung Shim 
291b09cd163SJoonyoung Shim 	return si470x_set_chan(radio, chan);
292b09cd163SJoonyoung Shim }
293b09cd163SJoonyoung Shim 
294b09cd163SJoonyoung Shim 
295b09cd163SJoonyoung Shim /*
296b09cd163SJoonyoung Shim  * si470x_set_seek - set seek
297b09cd163SJoonyoung Shim  */
298b09cd163SJoonyoung Shim static int si470x_set_seek(struct si470x_device *radio,
299ec6f4328SHans Verkuil 			   const struct v4l2_hw_freq_seek *seek)
300b09cd163SJoonyoung Shim {
301f140612dSHans de Goede 	int band, retval;
302f140612dSHans de Goede 	unsigned int freq;
303b09cd163SJoonyoung Shim 	bool timed_out = 0;
304b09cd163SJoonyoung Shim 
305f140612dSHans de Goede 	/* set band */
306f140612dSHans de Goede 	if (seek->rangelow || seek->rangehigh) {
307f140612dSHans de Goede 		for (band = 0; band < ARRAY_SIZE(bands); band++) {
308f140612dSHans de Goede 			if (bands[band].rangelow  == seek->rangelow &&
309f140612dSHans de Goede 			    bands[band].rangehigh == seek->rangehigh)
310f140612dSHans de Goede 				break;
311f140612dSHans de Goede 		}
312f140612dSHans de Goede 		if (band == ARRAY_SIZE(bands))
313f140612dSHans de Goede 			return -EINVAL; /* No matching band found */
314f140612dSHans de Goede 	} else
315f140612dSHans de Goede 		band = 1; /* If nothing is specified seek 76 - 108 Mhz */
316f140612dSHans de Goede 
317f140612dSHans de Goede 	if (radio->band != band) {
318f140612dSHans de Goede 		retval = si470x_get_freq(radio, &freq);
319f140612dSHans de Goede 		if (retval)
320f140612dSHans de Goede 			return retval;
321f140612dSHans de Goede 		retval = si470x_set_band(radio, band);
322f140612dSHans de Goede 		if (retval)
323f140612dSHans de Goede 			return retval;
324f140612dSHans de Goede 		retval = si470x_set_freq(radio, freq);
325f140612dSHans de Goede 		if (retval)
326f140612dSHans de Goede 			return retval;
327f140612dSHans de Goede 	}
328f140612dSHans de Goede 
329b09cd163SJoonyoung Shim 	/* start seeking */
330b09cd163SJoonyoung Shim 	radio->registers[POWERCFG] |= POWERCFG_SEEK;
331f140612dSHans de Goede 	if (seek->wrap_around)
332b09cd163SJoonyoung Shim 		radio->registers[POWERCFG] &= ~POWERCFG_SKMODE;
333b09cd163SJoonyoung Shim 	else
334b09cd163SJoonyoung Shim 		radio->registers[POWERCFG] |= POWERCFG_SKMODE;
335f140612dSHans de Goede 	if (seek->seek_upward)
336b09cd163SJoonyoung Shim 		radio->registers[POWERCFG] |= POWERCFG_SEEKUP;
337b09cd163SJoonyoung Shim 	else
338b09cd163SJoonyoung Shim 		radio->registers[POWERCFG] &= ~POWERCFG_SEEKUP;
339b09cd163SJoonyoung Shim 	retval = si470x_set_register(radio, POWERCFG);
340b09cd163SJoonyoung Shim 	if (retval < 0)
341340bd4c1SHans Verkuil 		return retval;
342b09cd163SJoonyoung Shim 
34377947111SHans de Goede 	/* wait till tune operation has completed */
3440830be3fSJoonyoung Shim 	INIT_COMPLETION(radio->completion);
3450830be3fSJoonyoung Shim 	retval = wait_for_completion_timeout(&radio->completion,
3460830be3fSJoonyoung Shim 			msecs_to_jiffies(seek_timeout));
3470830be3fSJoonyoung Shim 	if (!retval)
3480830be3fSJoonyoung Shim 		timed_out = true;
3490830be3fSJoonyoung Shim 
350b09cd163SJoonyoung Shim 	if ((radio->registers[STATUSRSSI] & STATUSRSSI_STC) == 0)
3514967d53dSHans Verkuil 		dev_warn(&radio->videodev.dev, "seek does not complete\n");
352b09cd163SJoonyoung Shim 	if (radio->registers[STATUSRSSI] & STATUSRSSI_SF)
3534967d53dSHans Verkuil 		dev_warn(&radio->videodev.dev,
354a9d6fd5eSJoonyoung Shim 			"seek failed / band limit reached\n");
355b09cd163SJoonyoung Shim 
356b09cd163SJoonyoung Shim 	/* stop seeking */
357b09cd163SJoonyoung Shim 	radio->registers[POWERCFG] &= ~POWERCFG_SEEK;
358b09cd163SJoonyoung Shim 	retval = si470x_set_register(radio, POWERCFG);
359b09cd163SJoonyoung Shim 
360b09cd163SJoonyoung Shim 	/* try again, if timed out */
361340bd4c1SHans Verkuil 	if (retval == 0 && timed_out)
36254f6019bSHans Verkuil 		return -ENODATA;
363b09cd163SJoonyoung Shim 	return retval;
364b09cd163SJoonyoung Shim }
365b09cd163SJoonyoung Shim 
366b09cd163SJoonyoung Shim 
367b09cd163SJoonyoung Shim /*
368b09cd163SJoonyoung Shim  * si470x_start - switch on radio
369b09cd163SJoonyoung Shim  */
370b09cd163SJoonyoung Shim int si470x_start(struct si470x_device *radio)
371b09cd163SJoonyoung Shim {
372b09cd163SJoonyoung Shim 	int retval;
373b09cd163SJoonyoung Shim 
374b09cd163SJoonyoung Shim 	/* powercfg */
375b09cd163SJoonyoung Shim 	radio->registers[POWERCFG] =
376b09cd163SJoonyoung Shim 		POWERCFG_DMUTE | POWERCFG_ENABLE | POWERCFG_RDSM;
377b09cd163SJoonyoung Shim 	retval = si470x_set_register(radio, POWERCFG);
378b09cd163SJoonyoung Shim 	if (retval < 0)
379b09cd163SJoonyoung Shim 		goto done;
380b09cd163SJoonyoung Shim 
381b09cd163SJoonyoung Shim 	/* sysconfig 1 */
382131ddd1aSTobias Lorenz 	radio->registers[SYSCONFIG1] =
383131ddd1aSTobias Lorenz 		(de << 11) & SYSCONFIG1_DE;		/* DE*/
384b09cd163SJoonyoung Shim 	retval = si470x_set_register(radio, SYSCONFIG1);
385b09cd163SJoonyoung Shim 	if (retval < 0)
386b09cd163SJoonyoung Shim 		goto done;
387b09cd163SJoonyoung Shim 
388b09cd163SJoonyoung Shim 	/* sysconfig 2 */
389b09cd163SJoonyoung Shim 	radio->registers[SYSCONFIG2] =
390b9664259SHans de Goede 		(0x1f  << 8) |				/* SEEKTH */
391f140612dSHans de Goede 		((radio->band << 6) & SYSCONFIG2_BAND) |/* BAND */
392b09cd163SJoonyoung Shim 		((space << 4) & SYSCONFIG2_SPACE) |	/* SPACE */
393b09cd163SJoonyoung Shim 		15;					/* VOLUME (max) */
394b09cd163SJoonyoung Shim 	retval = si470x_set_register(radio, SYSCONFIG2);
395b09cd163SJoonyoung Shim 	if (retval < 0)
396b09cd163SJoonyoung Shim 		goto done;
397b09cd163SJoonyoung Shim 
398b09cd163SJoonyoung Shim 	/* reset last channel */
399b09cd163SJoonyoung Shim 	retval = si470x_set_chan(radio,
400b09cd163SJoonyoung Shim 		radio->registers[CHANNEL] & CHANNEL_CHAN);
401b09cd163SJoonyoung Shim 
402b09cd163SJoonyoung Shim done:
403b09cd163SJoonyoung Shim 	return retval;
404b09cd163SJoonyoung Shim }
405b09cd163SJoonyoung Shim 
406b09cd163SJoonyoung Shim 
407b09cd163SJoonyoung Shim /*
408b09cd163SJoonyoung Shim  * si470x_stop - switch off radio
409b09cd163SJoonyoung Shim  */
410b09cd163SJoonyoung Shim int si470x_stop(struct si470x_device *radio)
411b09cd163SJoonyoung Shim {
412b09cd163SJoonyoung Shim 	int retval;
413b09cd163SJoonyoung Shim 
414b09cd163SJoonyoung Shim 	/* sysconfig 1 */
415b09cd163SJoonyoung Shim 	radio->registers[SYSCONFIG1] &= ~SYSCONFIG1_RDS;
416b09cd163SJoonyoung Shim 	retval = si470x_set_register(radio, SYSCONFIG1);
417b09cd163SJoonyoung Shim 	if (retval < 0)
418b09cd163SJoonyoung Shim 		goto done;
419b09cd163SJoonyoung Shim 
420b09cd163SJoonyoung Shim 	/* powercfg */
421b09cd163SJoonyoung Shim 	radio->registers[POWERCFG] &= ~POWERCFG_DMUTE;
422b09cd163SJoonyoung Shim 	/* POWERCFG_ENABLE has to automatically go low */
423b09cd163SJoonyoung Shim 	radio->registers[POWERCFG] |= POWERCFG_ENABLE |	POWERCFG_DISABLE;
424b09cd163SJoonyoung Shim 	retval = si470x_set_register(radio, POWERCFG);
425b09cd163SJoonyoung Shim 
426b09cd163SJoonyoung Shim done:
427b09cd163SJoonyoung Shim 	return retval;
428b09cd163SJoonyoung Shim }
429b09cd163SJoonyoung Shim 
430b09cd163SJoonyoung Shim 
431b09cd163SJoonyoung Shim /*
432b09cd163SJoonyoung Shim  * si470x_rds_on - switch on rds reception
433b09cd163SJoonyoung Shim  */
434f2f8e850SMauro Carvalho Chehab static int si470x_rds_on(struct si470x_device *radio)
435b09cd163SJoonyoung Shim {
436b09cd163SJoonyoung Shim 	int retval;
437b09cd163SJoonyoung Shim 
438b09cd163SJoonyoung Shim 	/* sysconfig 1 */
439b09cd163SJoonyoung Shim 	radio->registers[SYSCONFIG1] |= SYSCONFIG1_RDS;
440b09cd163SJoonyoung Shim 	retval = si470x_set_register(radio, SYSCONFIG1);
441b09cd163SJoonyoung Shim 	if (retval < 0)
442b09cd163SJoonyoung Shim 		radio->registers[SYSCONFIG1] &= ~SYSCONFIG1_RDS;
443b09cd163SJoonyoung Shim 
444b09cd163SJoonyoung Shim 	return retval;
445b09cd163SJoonyoung Shim }
446b09cd163SJoonyoung Shim 
447b09cd163SJoonyoung Shim 
448b09cd163SJoonyoung Shim 
449b09cd163SJoonyoung Shim /**************************************************************************
4501aa925c9SJoonyoung Shim  * File Operations Interface
4511aa925c9SJoonyoung Shim  **************************************************************************/
4521aa925c9SJoonyoung Shim 
4531aa925c9SJoonyoung Shim /*
4541aa925c9SJoonyoung Shim  * si470x_fops_read - read RDS data
4551aa925c9SJoonyoung Shim  */
4561aa925c9SJoonyoung Shim static ssize_t si470x_fops_read(struct file *file, char __user *buf,
4571aa925c9SJoonyoung Shim 		size_t count, loff_t *ppos)
4581aa925c9SJoonyoung Shim {
4591aa925c9SJoonyoung Shim 	struct si470x_device *radio = video_drvdata(file);
4601aa925c9SJoonyoung Shim 	int retval = 0;
4611aa925c9SJoonyoung Shim 	unsigned int block_count = 0;
4621aa925c9SJoonyoung Shim 
4631aa925c9SJoonyoung Shim 	/* switch on rds reception */
4641aa925c9SJoonyoung Shim 	if ((radio->registers[SYSCONFIG1] & SYSCONFIG1_RDS) == 0)
4651aa925c9SJoonyoung Shim 		si470x_rds_on(radio);
4661aa925c9SJoonyoung Shim 
4671aa925c9SJoonyoung Shim 	/* block if no new data available */
4681aa925c9SJoonyoung Shim 	while (radio->wr_index == radio->rd_index) {
4691aa925c9SJoonyoung Shim 		if (file->f_flags & O_NONBLOCK) {
4701aa925c9SJoonyoung Shim 			retval = -EWOULDBLOCK;
4711aa925c9SJoonyoung Shim 			goto done;
4721aa925c9SJoonyoung Shim 		}
4731aa925c9SJoonyoung Shim 		if (wait_event_interruptible(radio->read_queue,
4741aa925c9SJoonyoung Shim 			radio->wr_index != radio->rd_index) < 0) {
4751aa925c9SJoonyoung Shim 			retval = -EINTR;
4761aa925c9SJoonyoung Shim 			goto done;
4771aa925c9SJoonyoung Shim 		}
4781aa925c9SJoonyoung Shim 	}
4791aa925c9SJoonyoung Shim 
4801aa925c9SJoonyoung Shim 	/* calculate block count from byte count */
4811aa925c9SJoonyoung Shim 	count /= 3;
4821aa925c9SJoonyoung Shim 
4831aa925c9SJoonyoung Shim 	/* copy RDS block out of internal buffer and to user buffer */
4841aa925c9SJoonyoung Shim 	while (block_count < count) {
4851aa925c9SJoonyoung Shim 		if (radio->rd_index == radio->wr_index)
4861aa925c9SJoonyoung Shim 			break;
4871aa925c9SJoonyoung Shim 
4881aa925c9SJoonyoung Shim 		/* always transfer rds complete blocks */
4891aa925c9SJoonyoung Shim 		if (copy_to_user(buf, &radio->buffer[radio->rd_index], 3))
4901aa925c9SJoonyoung Shim 			/* retval = -EFAULT; */
4911aa925c9SJoonyoung Shim 			break;
4921aa925c9SJoonyoung Shim 
4931aa925c9SJoonyoung Shim 		/* increment and wrap read pointer */
4941aa925c9SJoonyoung Shim 		radio->rd_index += 3;
4951aa925c9SJoonyoung Shim 		if (radio->rd_index >= radio->buf_size)
4961aa925c9SJoonyoung Shim 			radio->rd_index = 0;
4971aa925c9SJoonyoung Shim 
4981aa925c9SJoonyoung Shim 		/* increment counters */
4991aa925c9SJoonyoung Shim 		block_count++;
5001aa925c9SJoonyoung Shim 		buf += 3;
5011aa925c9SJoonyoung Shim 		retval += 3;
5021aa925c9SJoonyoung Shim 	}
5031aa925c9SJoonyoung Shim 
5041aa925c9SJoonyoung Shim done:
5051aa925c9SJoonyoung Shim 	return retval;
5061aa925c9SJoonyoung Shim }
5071aa925c9SJoonyoung Shim 
5081aa925c9SJoonyoung Shim 
5091aa925c9SJoonyoung Shim /*
5101aa925c9SJoonyoung Shim  * si470x_fops_poll - poll RDS data
5111aa925c9SJoonyoung Shim  */
5121aa925c9SJoonyoung Shim static unsigned int si470x_fops_poll(struct file *file,
5131aa925c9SJoonyoung Shim 		struct poll_table_struct *pts)
5141aa925c9SJoonyoung Shim {
5151aa925c9SJoonyoung Shim 	struct si470x_device *radio = video_drvdata(file);
516eae63ae0SHans Verkuil 	unsigned long req_events = poll_requested_events(pts);
517eae63ae0SHans Verkuil 	int retval = v4l2_ctrl_poll(file, pts);
5181aa925c9SJoonyoung Shim 
519eae63ae0SHans Verkuil 	if (req_events & (POLLIN | POLLRDNORM)) {
5201aa925c9SJoonyoung Shim 		/* switch on rds reception */
5211aa925c9SJoonyoung Shim 		if ((radio->registers[SYSCONFIG1] & SYSCONFIG1_RDS) == 0)
5221aa925c9SJoonyoung Shim 			si470x_rds_on(radio);
5231aa925c9SJoonyoung Shim 
5241aa925c9SJoonyoung Shim 		poll_wait(file, &radio->read_queue, pts);
5251aa925c9SJoonyoung Shim 
5261aa925c9SJoonyoung Shim 		if (radio->rd_index != radio->wr_index)
527eae63ae0SHans Verkuil 			retval |= POLLIN | POLLRDNORM;
528eae63ae0SHans Verkuil 	}
5291aa925c9SJoonyoung Shim 
5301aa925c9SJoonyoung Shim 	return retval;
5311aa925c9SJoonyoung Shim }
5321aa925c9SJoonyoung Shim 
5331aa925c9SJoonyoung Shim 
5341aa925c9SJoonyoung Shim /*
5351aa925c9SJoonyoung Shim  * si470x_fops - file operations interface
5361aa925c9SJoonyoung Shim  */
5371aa925c9SJoonyoung Shim static const struct v4l2_file_operations si470x_fops = {
5381aa925c9SJoonyoung Shim 	.owner			= THIS_MODULE,
5391aa925c9SJoonyoung Shim 	.read			= si470x_fops_read,
5401aa925c9SJoonyoung Shim 	.poll			= si470x_fops_poll,
541f2f8e850SMauro Carvalho Chehab 	.unlocked_ioctl		= video_ioctl2,
5421aa925c9SJoonyoung Shim 	.open			= si470x_fops_open,
5431aa925c9SJoonyoung Shim 	.release		= si470x_fops_release,
5441aa925c9SJoonyoung Shim };
5451aa925c9SJoonyoung Shim 
5461aa925c9SJoonyoung Shim 
5471aa925c9SJoonyoung Shim 
5481aa925c9SJoonyoung Shim /**************************************************************************
549b09cd163SJoonyoung Shim  * Video4Linux Interface
550b09cd163SJoonyoung Shim  **************************************************************************/
551b09cd163SJoonyoung Shim 
5524967d53dSHans Verkuil 
5534967d53dSHans Verkuil static int si470x_s_ctrl(struct v4l2_ctrl *ctrl)
554b09cd163SJoonyoung Shim {
5554967d53dSHans Verkuil 	struct si470x_device *radio =
5564967d53dSHans Verkuil 		container_of(ctrl->handler, struct si470x_device, hdl);
557b09cd163SJoonyoung Shim 
558b09cd163SJoonyoung Shim 	switch (ctrl->id) {
559b09cd163SJoonyoung Shim 	case V4L2_CID_AUDIO_VOLUME:
560b09cd163SJoonyoung Shim 		radio->registers[SYSCONFIG2] &= ~SYSCONFIG2_VOLUME;
5614967d53dSHans Verkuil 		radio->registers[SYSCONFIG2] |= ctrl->val;
5624967d53dSHans Verkuil 		return si470x_set_register(radio, SYSCONFIG2);
563b09cd163SJoonyoung Shim 	case V4L2_CID_AUDIO_MUTE:
5644967d53dSHans Verkuil 		if (ctrl->val)
565b09cd163SJoonyoung Shim 			radio->registers[POWERCFG] &= ~POWERCFG_DMUTE;
566b09cd163SJoonyoung Shim 		else
567b09cd163SJoonyoung Shim 			radio->registers[POWERCFG] |= POWERCFG_DMUTE;
5684967d53dSHans Verkuil 		return si470x_set_register(radio, POWERCFG);
569b09cd163SJoonyoung Shim 	default:
5704967d53dSHans Verkuil 		return -EINVAL;
571b09cd163SJoonyoung Shim 	}
572b09cd163SJoonyoung Shim }
573b09cd163SJoonyoung Shim 
574b09cd163SJoonyoung Shim 
575b09cd163SJoonyoung Shim /*
576b09cd163SJoonyoung Shim  * si470x_vidioc_g_tuner - get tuner attributes
577b09cd163SJoonyoung Shim  */
578b09cd163SJoonyoung Shim static int si470x_vidioc_g_tuner(struct file *file, void *priv,
579b09cd163SJoonyoung Shim 		struct v4l2_tuner *tuner)
580b09cd163SJoonyoung Shim {
581b09cd163SJoonyoung Shim 	struct si470x_device *radio = video_drvdata(file);
58286ef3f78SHans de Goede 	int retval = 0;
583b09cd163SJoonyoung Shim 
584340bd4c1SHans Verkuil 	if (tuner->index != 0)
585340bd4c1SHans Verkuil 		return -EINVAL;
586b09cd163SJoonyoung Shim 
58786ef3f78SHans de Goede 	if (!radio->status_rssi_auto_update) {
588b09cd163SJoonyoung Shim 		retval = si470x_get_register(radio, STATUSRSSI);
589b09cd163SJoonyoung Shim 		if (retval < 0)
590340bd4c1SHans Verkuil 			return retval;
59186ef3f78SHans de Goede 	}
592b09cd163SJoonyoung Shim 
593b09cd163SJoonyoung Shim 	/* driver constants */
594b09cd163SJoonyoung Shim 	strcpy(tuner->name, "FM");
595b09cd163SJoonyoung Shim 	tuner->type = V4L2_TUNER_RADIO;
596b09cd163SJoonyoung Shim 	tuner->capability = V4L2_TUNER_CAP_LOW | V4L2_TUNER_CAP_STEREO |
59754f6019bSHans Verkuil 			    V4L2_TUNER_CAP_RDS | V4L2_TUNER_CAP_RDS_BLOCK_IO |
59854f6019bSHans Verkuil 			    V4L2_TUNER_CAP_HWSEEK_BOUNDED |
59954f6019bSHans Verkuil 			    V4L2_TUNER_CAP_HWSEEK_WRAP;
600b09cd163SJoonyoung Shim 	tuner->rangelow  =  76 * FREQ_MUL;
601b09cd163SJoonyoung Shim 	tuner->rangehigh = 108 * FREQ_MUL;
602b09cd163SJoonyoung Shim 
603b09cd163SJoonyoung Shim 	/* stereo indicator == stereo (instead of mono) */
604b09cd163SJoonyoung Shim 	if ((radio->registers[STATUSRSSI] & STATUSRSSI_ST) == 0)
605b09cd163SJoonyoung Shim 		tuner->rxsubchans = V4L2_TUNER_SUB_MONO;
606b09cd163SJoonyoung Shim 	else
6074967d53dSHans Verkuil 		tuner->rxsubchans = V4L2_TUNER_SUB_STEREO;
608b09cd163SJoonyoung Shim 	/* If there is a reliable method of detecting an RDS channel,
609b09cd163SJoonyoung Shim 	   then this code should check for that before setting this
610b09cd163SJoonyoung Shim 	   RDS subchannel. */
611b09cd163SJoonyoung Shim 	tuner->rxsubchans |= V4L2_TUNER_SUB_RDS;
612b09cd163SJoonyoung Shim 
613b09cd163SJoonyoung Shim 	/* mono/stereo selector */
614b09cd163SJoonyoung Shim 	if ((radio->registers[POWERCFG] & POWERCFG_MONO) == 0)
615b09cd163SJoonyoung Shim 		tuner->audmode = V4L2_TUNER_MODE_STEREO;
616b09cd163SJoonyoung Shim 	else
617b09cd163SJoonyoung Shim 		tuner->audmode = V4L2_TUNER_MODE_MONO;
618b09cd163SJoonyoung Shim 
619b09cd163SJoonyoung Shim 	/* min is worst, max is best; signal:0..0xffff; rssi: 0..0xff */
620144dcdceSTobias Lorenz 	/* measured in units of dbµV in 1 db increments (max at ~75 dbµV) */
621b09cd163SJoonyoung Shim 	tuner->signal = (radio->registers[STATUSRSSI] & STATUSRSSI_RSSI);
622b09cd163SJoonyoung Shim 	/* the ideal factor is 0xffff/75 = 873,8 */
623b09cd163SJoonyoung Shim 	tuner->signal = (tuner->signal * 873) + (8 * tuner->signal / 10);
624eae63ae0SHans Verkuil 	if (tuner->signal > 0xffff)
625eae63ae0SHans Verkuil 		tuner->signal = 0xffff;
626b09cd163SJoonyoung Shim 
627b09cd163SJoonyoung Shim 	/* automatic frequency control: -1: freq to low, 1 freq to high */
628b09cd163SJoonyoung Shim 	/* AFCRL does only indicate that freq. differs, not if too low/high */
629b09cd163SJoonyoung Shim 	tuner->afc = (radio->registers[STATUSRSSI] & STATUSRSSI_AFCRL) ? 1 : 0;
630b09cd163SJoonyoung Shim 
631b09cd163SJoonyoung Shim 	return retval;
632b09cd163SJoonyoung Shim }
633b09cd163SJoonyoung Shim 
634b09cd163SJoonyoung Shim 
635b09cd163SJoonyoung Shim /*
636b09cd163SJoonyoung Shim  * si470x_vidioc_s_tuner - set tuner attributes
637b09cd163SJoonyoung Shim  */
638b09cd163SJoonyoung Shim static int si470x_vidioc_s_tuner(struct file *file, void *priv,
639*2f73c7c5SHans Verkuil 		const struct v4l2_tuner *tuner)
640b09cd163SJoonyoung Shim {
641b09cd163SJoonyoung Shim 	struct si470x_device *radio = video_drvdata(file);
642b09cd163SJoonyoung Shim 
643b09cd163SJoonyoung Shim 	if (tuner->index != 0)
644eae63ae0SHans Verkuil 		return -EINVAL;
645b09cd163SJoonyoung Shim 
646b09cd163SJoonyoung Shim 	/* mono/stereo selector */
647b09cd163SJoonyoung Shim 	switch (tuner->audmode) {
648b09cd163SJoonyoung Shim 	case V4L2_TUNER_MODE_MONO:
649b09cd163SJoonyoung Shim 		radio->registers[POWERCFG] |= POWERCFG_MONO;  /* force mono */
650b09cd163SJoonyoung Shim 		break;
651b09cd163SJoonyoung Shim 	case V4L2_TUNER_MODE_STEREO:
652eae63ae0SHans Verkuil 	default:
653b09cd163SJoonyoung Shim 		radio->registers[POWERCFG] &= ~POWERCFG_MONO; /* try stereo */
654b09cd163SJoonyoung Shim 		break;
655b09cd163SJoonyoung Shim 	}
656b09cd163SJoonyoung Shim 
657340bd4c1SHans Verkuil 	return si470x_set_register(radio, POWERCFG);
658b09cd163SJoonyoung Shim }
659b09cd163SJoonyoung Shim 
660b09cd163SJoonyoung Shim 
661b09cd163SJoonyoung Shim /*
662b09cd163SJoonyoung Shim  * si470x_vidioc_g_frequency - get tuner or modulator radio frequency
663b09cd163SJoonyoung Shim  */
664b09cd163SJoonyoung Shim static int si470x_vidioc_g_frequency(struct file *file, void *priv,
665b09cd163SJoonyoung Shim 		struct v4l2_frequency *freq)
666b09cd163SJoonyoung Shim {
667b09cd163SJoonyoung Shim 	struct si470x_device *radio = video_drvdata(file);
668b09cd163SJoonyoung Shim 
669340bd4c1SHans Verkuil 	if (freq->tuner != 0)
670340bd4c1SHans Verkuil 		return -EINVAL;
671b09cd163SJoonyoung Shim 
672b09cd163SJoonyoung Shim 	freq->type = V4L2_TUNER_RADIO;
673340bd4c1SHans Verkuil 	return si470x_get_freq(radio, &freq->frequency);
674b09cd163SJoonyoung Shim }
675b09cd163SJoonyoung Shim 
676b09cd163SJoonyoung Shim 
677b09cd163SJoonyoung Shim /*
678b09cd163SJoonyoung Shim  * si470x_vidioc_s_frequency - set tuner or modulator radio frequency
679b09cd163SJoonyoung Shim  */
680b09cd163SJoonyoung Shim static int si470x_vidioc_s_frequency(struct file *file, void *priv,
681b530a447SHans Verkuil 		const struct v4l2_frequency *freq)
682b09cd163SJoonyoung Shim {
683b09cd163SJoonyoung Shim 	struct si470x_device *radio = video_drvdata(file);
684f140612dSHans de Goede 	int retval;
685b09cd163SJoonyoung Shim 
686340bd4c1SHans Verkuil 	if (freq->tuner != 0)
687340bd4c1SHans Verkuil 		return -EINVAL;
688b09cd163SJoonyoung Shim 
689f140612dSHans de Goede 	if (freq->frequency < bands[radio->band].rangelow ||
690f140612dSHans de Goede 	    freq->frequency > bands[radio->band].rangehigh) {
691f140612dSHans de Goede 		/* Switch to band 1 which covers everything we support */
692f140612dSHans de Goede 		retval = si470x_set_band(radio, 1);
693f140612dSHans de Goede 		if (retval)
694f140612dSHans de Goede 			return retval;
695f140612dSHans de Goede 	}
696340bd4c1SHans Verkuil 	return si470x_set_freq(radio, freq->frequency);
697b09cd163SJoonyoung Shim }
698b09cd163SJoonyoung Shim 
699b09cd163SJoonyoung Shim 
700b09cd163SJoonyoung Shim /*
701b09cd163SJoonyoung Shim  * si470x_vidioc_s_hw_freq_seek - set hardware frequency seek
702b09cd163SJoonyoung Shim  */
703b09cd163SJoonyoung Shim static int si470x_vidioc_s_hw_freq_seek(struct file *file, void *priv,
704ec6f4328SHans Verkuil 		const struct v4l2_hw_freq_seek *seek)
705b09cd163SJoonyoung Shim {
706b09cd163SJoonyoung Shim 	struct si470x_device *radio = video_drvdata(file);
707b09cd163SJoonyoung Shim 
708340bd4c1SHans Verkuil 	if (seek->tuner != 0)
709340bd4c1SHans Verkuil 		return -EINVAL;
710b09cd163SJoonyoung Shim 
711617ade61SHans Verkuil 	if (file->f_flags & O_NONBLOCK)
712617ade61SHans Verkuil 		return -EWOULDBLOCK;
713617ade61SHans Verkuil 
714f140612dSHans de Goede 	return si470x_set_seek(radio, seek);
715f140612dSHans de Goede }
716f140612dSHans de Goede 
717f140612dSHans de Goede /*
718f140612dSHans de Goede  * si470x_vidioc_enum_freq_bands - enumerate supported bands
719f140612dSHans de Goede  */
720f140612dSHans de Goede static int si470x_vidioc_enum_freq_bands(struct file *file, void *priv,
721f140612dSHans de Goede 					 struct v4l2_frequency_band *band)
722f140612dSHans de Goede {
723f140612dSHans de Goede 	if (band->tuner != 0)
724f140612dSHans de Goede 		return -EINVAL;
725f140612dSHans de Goede 	if (band->index >= ARRAY_SIZE(bands))
726f140612dSHans de Goede 		return -EINVAL;
727f140612dSHans de Goede 	*band = bands[band->index];
728f140612dSHans de Goede 	return 0;
729b09cd163SJoonyoung Shim }
730b09cd163SJoonyoung Shim 
7314967d53dSHans Verkuil const struct v4l2_ctrl_ops si470x_ctrl_ops = {
7324967d53dSHans Verkuil 	.s_ctrl = si470x_s_ctrl,
7334967d53dSHans Verkuil };
734b09cd163SJoonyoung Shim 
735b09cd163SJoonyoung Shim /*
736b09cd163SJoonyoung Shim  * si470x_ioctl_ops - video device ioctl operations
737b09cd163SJoonyoung Shim  */
738b09cd163SJoonyoung Shim static const struct v4l2_ioctl_ops si470x_ioctl_ops = {
739b09cd163SJoonyoung Shim 	.vidioc_querycap	= si470x_vidioc_querycap,
740b09cd163SJoonyoung Shim 	.vidioc_g_tuner		= si470x_vidioc_g_tuner,
741b09cd163SJoonyoung Shim 	.vidioc_s_tuner		= si470x_vidioc_s_tuner,
742b09cd163SJoonyoung Shim 	.vidioc_g_frequency	= si470x_vidioc_g_frequency,
743b09cd163SJoonyoung Shim 	.vidioc_s_frequency	= si470x_vidioc_s_frequency,
744b09cd163SJoonyoung Shim 	.vidioc_s_hw_freq_seek	= si470x_vidioc_s_hw_freq_seek,
745f140612dSHans de Goede 	.vidioc_enum_freq_bands = si470x_vidioc_enum_freq_bands,
746eae63ae0SHans Verkuil 	.vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
747eae63ae0SHans Verkuil 	.vidioc_unsubscribe_event = v4l2_event_unsubscribe,
748b09cd163SJoonyoung Shim };
749b09cd163SJoonyoung Shim 
750b09cd163SJoonyoung Shim 
751b09cd163SJoonyoung Shim /*
752b09cd163SJoonyoung Shim  * si470x_viddev_template - video device interface
753b09cd163SJoonyoung Shim  */
754b09cd163SJoonyoung Shim struct video_device si470x_viddev_template = {
755b09cd163SJoonyoung Shim 	.fops			= &si470x_fops,
756b09cd163SJoonyoung Shim 	.name			= DRIVER_NAME,
7574967d53dSHans Verkuil 	.release		= video_device_release_empty,
758b09cd163SJoonyoung Shim 	.ioctl_ops		= &si470x_ioctl_ops,
759b09cd163SJoonyoung Shim };
760