Merge branch 'for-john' of git://git.kernel.org/pub/scm/linux/kernel/git/jberg/mac802...
[sfrench/cifs-2.6.git] / drivers / media / radio / si470x / radio-si470x-common.c
1 /*
2  *  drivers/media/radio/si470x/radio-si470x-common.c
3  *
4  *  Driver for radios with Silicon Labs Si470x FM Radio Receivers
5  *
6  *  Copyright (c) 2009 Tobias Lorenz <tobias.lorenz@gmx.net>
7  *  Copyright (c) 2012 Hans de Goede <hdegoede@redhat.com>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22  */
23
24
25 /*
26  * History:
27  * 2008-01-12   Tobias Lorenz <tobias.lorenz@gmx.net>
28  *              Version 1.0.0
29  *              - First working version
30  * 2008-01-13   Tobias Lorenz <tobias.lorenz@gmx.net>
31  *              Version 1.0.1
32  *              - Improved error handling, every function now returns errno
33  *              - Improved multi user access (start/mute/stop)
34  *              - Channel doesn't get lost anymore after start/mute/stop
35  *              - RDS support added (polling mode via interrupt EP 1)
36  *              - marked default module parameters with *value*
37  *              - switched from bit structs to bit masks
38  *              - header file cleaned and integrated
39  * 2008-01-14   Tobias Lorenz <tobias.lorenz@gmx.net>
40  *              Version 1.0.2
41  *              - hex values are now lower case
42  *              - commented USB ID for ADS/Tech moved on todo list
43  *              - blacklisted si470x in hid-quirks.c
44  *              - rds buffer handling functions integrated into *_work, *_read
45  *              - rds_command in si470x_poll exchanged against simple retval
46  *              - check for firmware version 15
47  *              - code order and prototypes still remain the same
48  *              - spacing and bottom of band codes remain the same
49  * 2008-01-16   Tobias Lorenz <tobias.lorenz@gmx.net>
50  *              Version 1.0.3
51  *              - code reordered to avoid function prototypes
52  *              - switch/case defaults are now more user-friendly
53  *              - unified comment style
54  *              - applied all checkpatch.pl v1.12 suggestions
55  *                except the warning about the too long lines with bit comments
56  *              - renamed FMRADIO to RADIO to cut line length (checkpatch.pl)
57  * 2008-01-22   Tobias Lorenz <tobias.lorenz@gmx.net>
58  *              Version 1.0.4
59  *              - avoid poss. locking when doing copy_to_user which may sleep
60  *              - RDS is automatically activated on read now
61  *              - code cleaned of unnecessary rds_commands
62  *              - USB Vendor/Product ID for ADS/Tech FM Radio Receiver verified
63  *                (thanks to Guillaume RAMOUSSE)
64  * 2008-01-27   Tobias Lorenz <tobias.lorenz@gmx.net>
65  *              Version 1.0.5
66  *              - number of seek_retries changed to tune_timeout
67  *              - fixed problem with incomplete tune operations by own buffers
68  *              - optimization of variables and printf types
69  *              - improved error logging
70  * 2008-01-31   Tobias Lorenz <tobias.lorenz@gmx.net>
71  *              Oliver Neukum <oliver@neukum.org>
72  *              Version 1.0.6
73  *              - fixed coverity checker warnings in *_usb_driver_disconnect
74  *              - probe()/open() race by correct ordering in probe()
75  *              - DMA coherency rules by separate allocation of all buffers
76  *              - use of endianness macros
77  *              - abuse of spinlock, replaced by mutex
78  *              - racy handling of timer in disconnect,
79  *                replaced by delayed_work
80  *              - racy interruptible_sleep_on(),
81  *                replaced with wait_event_interruptible()
82  *              - handle signals in read()
83  * 2008-02-08   Tobias Lorenz <tobias.lorenz@gmx.net>
84  *              Oliver Neukum <oliver@neukum.org>
85  *              Version 1.0.7
86  *              - usb autosuspend support
87  *              - unplugging fixed
88  * 2008-05-07   Tobias Lorenz <tobias.lorenz@gmx.net>
89  *              Version 1.0.8
90  *              - hardware frequency seek support
91  *              - afc indication
92  *              - more safety checks, let si470x_get_freq return errno
93  *              - vidioc behavior corrected according to v4l2 spec
94  * 2008-10-20   Alexey Klimov <klimov.linux@gmail.com>
95  *              - add support for KWorld USB FM Radio FM700
96  *              - blacklisted KWorld radio in hid-core.c and hid-ids.h
97  * 2008-12-03   Mark Lord <mlord@pobox.com>
98  *              - add support for DealExtreme USB Radio
99  * 2009-01-31   Bob Ross <pigiron@gmx.com>
100  *              - correction of stereo detection/setting
101  *              - correction of signal strength indicator scaling
102  * 2009-01-31   Rick Bronson <rick@efn.org>
103  *              Tobias Lorenz <tobias.lorenz@gmx.net>
104  *              - add LED status output
105  *              - get HW/SW version from scratchpad
106  * 2009-06-16   Edouard Lafargue <edouard@lafargue.name>
107  *              Version 1.0.10
108  *              - add support for interrupt mode for RDS endpoint,
109  *                instead of polling.
110  *                Improves RDS reception significantly
111  */
112
113
114 /* kernel includes */
115 #include "radio-si470x.h"
116
117
118
119 /**************************************************************************
120  * Module Parameters
121  **************************************************************************/
122
123 /* Spacing (kHz) */
124 /* 0: 200 kHz (USA, Australia) */
125 /* 1: 100 kHz (Europe, Japan) */
126 /* 2:  50 kHz */
127 static unsigned short space = 2;
128 module_param(space, ushort, 0444);
129 MODULE_PARM_DESC(space, "Spacing: 0=200kHz 1=100kHz *2=50kHz*");
130
131 /* De-emphasis */
132 /* 0: 75 us (USA) */
133 /* 1: 50 us (Europe, Australia, Japan) */
134 static unsigned short de = 1;
135 module_param(de, ushort, 0444);
136 MODULE_PARM_DESC(de, "De-emphasis: 0=75us *1=50us*");
137
138 /* Tune timeout */
139 static unsigned int tune_timeout = 3000;
140 module_param(tune_timeout, uint, 0644);
141 MODULE_PARM_DESC(tune_timeout, "Tune timeout: *3000*");
142
143 /* Seek timeout */
144 static unsigned int seek_timeout = 5000;
145 module_param(seek_timeout, uint, 0644);
146 MODULE_PARM_DESC(seek_timeout, "Seek timeout: *5000*");
147
148 static const struct v4l2_frequency_band bands[] = {
149         {
150                 .type = V4L2_TUNER_RADIO,
151                 .index = 0,
152                 .capability = V4L2_TUNER_CAP_LOW | V4L2_TUNER_CAP_STEREO |
153                             V4L2_TUNER_CAP_RDS | V4L2_TUNER_CAP_RDS_BLOCK_IO |
154                             V4L2_TUNER_CAP_HWSEEK_BOUNDED |
155                             V4L2_TUNER_CAP_HWSEEK_WRAP,
156                 .rangelow   =  87500 * 16,
157                 .rangehigh  = 108000 * 16,
158                 .modulation = V4L2_BAND_MODULATION_FM,
159         },
160         {
161                 .type = V4L2_TUNER_RADIO,
162                 .index = 1,
163                 .capability = V4L2_TUNER_CAP_LOW | V4L2_TUNER_CAP_STEREO |
164                             V4L2_TUNER_CAP_RDS | V4L2_TUNER_CAP_RDS_BLOCK_IO |
165                             V4L2_TUNER_CAP_HWSEEK_BOUNDED |
166                             V4L2_TUNER_CAP_HWSEEK_WRAP,
167                 .rangelow   =  76000 * 16,
168                 .rangehigh  = 108000 * 16,
169                 .modulation = V4L2_BAND_MODULATION_FM,
170         },
171         {
172                 .type = V4L2_TUNER_RADIO,
173                 .index = 2,
174                 .capability = V4L2_TUNER_CAP_LOW | V4L2_TUNER_CAP_STEREO |
175                             V4L2_TUNER_CAP_RDS | V4L2_TUNER_CAP_RDS_BLOCK_IO |
176                             V4L2_TUNER_CAP_HWSEEK_BOUNDED |
177                             V4L2_TUNER_CAP_HWSEEK_WRAP,
178                 .rangelow   =  76000 * 16,
179                 .rangehigh  =  90000 * 16,
180                 .modulation = V4L2_BAND_MODULATION_FM,
181         },
182 };
183
184 /**************************************************************************
185  * Generic Functions
186  **************************************************************************/
187
188 /*
189  * si470x_set_band - set the band
190  */
191 static int si470x_set_band(struct si470x_device *radio, int band)
192 {
193         if (radio->band == band)
194                 return 0;
195
196         radio->band = band;
197         radio->registers[SYSCONFIG2] &= ~SYSCONFIG2_BAND;
198         radio->registers[SYSCONFIG2] |= radio->band << 6;
199         return si470x_set_register(radio, SYSCONFIG2);
200 }
201
202 /*
203  * si470x_set_chan - set the channel
204  */
205 static int si470x_set_chan(struct si470x_device *radio, unsigned short chan)
206 {
207         int retval;
208         bool timed_out = 0;
209
210         /* start tuning */
211         radio->registers[CHANNEL] &= ~CHANNEL_CHAN;
212         radio->registers[CHANNEL] |= CHANNEL_TUNE | chan;
213         retval = si470x_set_register(radio, CHANNEL);
214         if (retval < 0)
215                 goto done;
216
217         /* wait till tune operation has completed */
218         INIT_COMPLETION(radio->completion);
219         retval = wait_for_completion_timeout(&radio->completion,
220                         msecs_to_jiffies(tune_timeout));
221         if (!retval)
222                 timed_out = true;
223
224         if ((radio->registers[STATUSRSSI] & STATUSRSSI_STC) == 0)
225                 dev_warn(&radio->videodev.dev, "tune does not complete\n");
226         if (timed_out)
227                 dev_warn(&radio->videodev.dev,
228                         "tune timed out after %u ms\n", tune_timeout);
229
230         /* stop tuning */
231         radio->registers[CHANNEL] &= ~CHANNEL_TUNE;
232         retval = si470x_set_register(radio, CHANNEL);
233
234 done:
235         return retval;
236 }
237
238 /*
239  * si470x_get_step - get channel spacing
240  */
241 static unsigned int si470x_get_step(struct si470x_device *radio)
242 {
243         /* Spacing (kHz) */
244         switch ((radio->registers[SYSCONFIG2] & SYSCONFIG2_SPACE) >> 4) {
245         /* 0: 200 kHz (USA, Australia) */
246         case 0:
247                 return 200 * 16;
248         /* 1: 100 kHz (Europe, Japan) */
249         case 1:
250                 return 100 * 16;
251         /* 2:  50 kHz */
252         default:
253                 return 50 * 16;
254         };
255 }
256
257
258 /*
259  * si470x_get_freq - get the frequency
260  */
261 static int si470x_get_freq(struct si470x_device *radio, unsigned int *freq)
262 {
263         int chan, retval;
264
265         /* read channel */
266         retval = si470x_get_register(radio, READCHAN);
267         chan = radio->registers[READCHAN] & READCHAN_READCHAN;
268
269         /* Frequency (MHz) = Spacing (kHz) x Channel + Bottom of Band (MHz) */
270         *freq = chan * si470x_get_step(radio) + bands[radio->band].rangelow;
271
272         return retval;
273 }
274
275
276 /*
277  * si470x_set_freq - set the frequency
278  */
279 int si470x_set_freq(struct si470x_device *radio, unsigned int freq)
280 {
281         unsigned short chan;
282
283         freq = clamp(freq, bands[radio->band].rangelow,
284                            bands[radio->band].rangehigh);
285         /* Chan = [ Freq (Mhz) - Bottom of Band (MHz) ] / Spacing (kHz) */
286         chan = (freq - bands[radio->band].rangelow) / si470x_get_step(radio);
287
288         return si470x_set_chan(radio, chan);
289 }
290
291
292 /*
293  * si470x_set_seek - set seek
294  */
295 static int si470x_set_seek(struct si470x_device *radio,
296                            struct v4l2_hw_freq_seek *seek)
297 {
298         int band, retval;
299         unsigned int freq;
300         bool timed_out = 0;
301
302         /* set band */
303         if (seek->rangelow || seek->rangehigh) {
304                 for (band = 0; band < ARRAY_SIZE(bands); band++) {
305                         if (bands[band].rangelow  == seek->rangelow &&
306                             bands[band].rangehigh == seek->rangehigh)
307                                 break;
308                 }
309                 if (band == ARRAY_SIZE(bands))
310                         return -EINVAL; /* No matching band found */
311         } else
312                 band = 1; /* If nothing is specified seek 76 - 108 Mhz */
313
314         if (radio->band != band) {
315                 retval = si470x_get_freq(radio, &freq);
316                 if (retval)
317                         return retval;
318                 retval = si470x_set_band(radio, band);
319                 if (retval)
320                         return retval;
321                 retval = si470x_set_freq(radio, freq);
322                 if (retval)
323                         return retval;
324         }
325
326         /* start seeking */
327         radio->registers[POWERCFG] |= POWERCFG_SEEK;
328         if (seek->wrap_around)
329                 radio->registers[POWERCFG] &= ~POWERCFG_SKMODE;
330         else
331                 radio->registers[POWERCFG] |= POWERCFG_SKMODE;
332         if (seek->seek_upward)
333                 radio->registers[POWERCFG] |= POWERCFG_SEEKUP;
334         else
335                 radio->registers[POWERCFG] &= ~POWERCFG_SEEKUP;
336         retval = si470x_set_register(radio, POWERCFG);
337         if (retval < 0)
338                 return retval;
339
340         /* wait till tune operation has completed */
341         INIT_COMPLETION(radio->completion);
342         retval = wait_for_completion_timeout(&radio->completion,
343                         msecs_to_jiffies(seek_timeout));
344         if (!retval)
345                 timed_out = true;
346
347         if ((radio->registers[STATUSRSSI] & STATUSRSSI_STC) == 0)
348                 dev_warn(&radio->videodev.dev, "seek does not complete\n");
349         if (radio->registers[STATUSRSSI] & STATUSRSSI_SF)
350                 dev_warn(&radio->videodev.dev,
351                         "seek failed / band limit reached\n");
352
353         /* stop seeking */
354         radio->registers[POWERCFG] &= ~POWERCFG_SEEK;
355         retval = si470x_set_register(radio, POWERCFG);
356
357         /* try again, if timed out */
358         if (retval == 0 && timed_out)
359                 return -ENODATA;
360         return retval;
361 }
362
363
364 /*
365  * si470x_start - switch on radio
366  */
367 int si470x_start(struct si470x_device *radio)
368 {
369         int retval;
370
371         /* powercfg */
372         radio->registers[POWERCFG] =
373                 POWERCFG_DMUTE | POWERCFG_ENABLE | POWERCFG_RDSM;
374         retval = si470x_set_register(radio, POWERCFG);
375         if (retval < 0)
376                 goto done;
377
378         /* sysconfig 1 */
379         radio->registers[SYSCONFIG1] =
380                 (de << 11) & SYSCONFIG1_DE;             /* DE*/
381         retval = si470x_set_register(radio, SYSCONFIG1);
382         if (retval < 0)
383                 goto done;
384
385         /* sysconfig 2 */
386         radio->registers[SYSCONFIG2] =
387                 (0x1f  << 8) |                          /* SEEKTH */
388                 ((radio->band << 6) & SYSCONFIG2_BAND) |/* BAND */
389                 ((space << 4) & SYSCONFIG2_SPACE) |     /* SPACE */
390                 15;                                     /* VOLUME (max) */
391         retval = si470x_set_register(radio, SYSCONFIG2);
392         if (retval < 0)
393                 goto done;
394
395         /* reset last channel */
396         retval = si470x_set_chan(radio,
397                 radio->registers[CHANNEL] & CHANNEL_CHAN);
398
399 done:
400         return retval;
401 }
402
403
404 /*
405  * si470x_stop - switch off radio
406  */
407 int si470x_stop(struct si470x_device *radio)
408 {
409         int retval;
410
411         /* sysconfig 1 */
412         radio->registers[SYSCONFIG1] &= ~SYSCONFIG1_RDS;
413         retval = si470x_set_register(radio, SYSCONFIG1);
414         if (retval < 0)
415                 goto done;
416
417         /* powercfg */
418         radio->registers[POWERCFG] &= ~POWERCFG_DMUTE;
419         /* POWERCFG_ENABLE has to automatically go low */
420         radio->registers[POWERCFG] |= POWERCFG_ENABLE | POWERCFG_DISABLE;
421         retval = si470x_set_register(radio, POWERCFG);
422
423 done:
424         return retval;
425 }
426
427
428 /*
429  * si470x_rds_on - switch on rds reception
430  */
431 static int si470x_rds_on(struct si470x_device *radio)
432 {
433         int retval;
434
435         /* sysconfig 1 */
436         radio->registers[SYSCONFIG1] |= SYSCONFIG1_RDS;
437         retval = si470x_set_register(radio, SYSCONFIG1);
438         if (retval < 0)
439                 radio->registers[SYSCONFIG1] &= ~SYSCONFIG1_RDS;
440
441         return retval;
442 }
443
444
445
446 /**************************************************************************
447  * File Operations Interface
448  **************************************************************************/
449
450 /*
451  * si470x_fops_read - read RDS data
452  */
453 static ssize_t si470x_fops_read(struct file *file, char __user *buf,
454                 size_t count, loff_t *ppos)
455 {
456         struct si470x_device *radio = video_drvdata(file);
457         int retval = 0;
458         unsigned int block_count = 0;
459
460         /* switch on rds reception */
461         if ((radio->registers[SYSCONFIG1] & SYSCONFIG1_RDS) == 0)
462                 si470x_rds_on(radio);
463
464         /* block if no new data available */
465         while (radio->wr_index == radio->rd_index) {
466                 if (file->f_flags & O_NONBLOCK) {
467                         retval = -EWOULDBLOCK;
468                         goto done;
469                 }
470                 if (wait_event_interruptible(radio->read_queue,
471                         radio->wr_index != radio->rd_index) < 0) {
472                         retval = -EINTR;
473                         goto done;
474                 }
475         }
476
477         /* calculate block count from byte count */
478         count /= 3;
479
480         /* copy RDS block out of internal buffer and to user buffer */
481         while (block_count < count) {
482                 if (radio->rd_index == radio->wr_index)
483                         break;
484
485                 /* always transfer rds complete blocks */
486                 if (copy_to_user(buf, &radio->buffer[radio->rd_index], 3))
487                         /* retval = -EFAULT; */
488                         break;
489
490                 /* increment and wrap read pointer */
491                 radio->rd_index += 3;
492                 if (radio->rd_index >= radio->buf_size)
493                         radio->rd_index = 0;
494
495                 /* increment counters */
496                 block_count++;
497                 buf += 3;
498                 retval += 3;
499         }
500
501 done:
502         return retval;
503 }
504
505
506 /*
507  * si470x_fops_poll - poll RDS data
508  */
509 static unsigned int si470x_fops_poll(struct file *file,
510                 struct poll_table_struct *pts)
511 {
512         struct si470x_device *radio = video_drvdata(file);
513         unsigned long req_events = poll_requested_events(pts);
514         int retval = v4l2_ctrl_poll(file, pts);
515
516         if (req_events & (POLLIN | POLLRDNORM)) {
517                 /* switch on rds reception */
518                 if ((radio->registers[SYSCONFIG1] & SYSCONFIG1_RDS) == 0)
519                         si470x_rds_on(radio);
520
521                 poll_wait(file, &radio->read_queue, pts);
522
523                 if (radio->rd_index != radio->wr_index)
524                         retval |= POLLIN | POLLRDNORM;
525         }
526
527         return retval;
528 }
529
530
531 /*
532  * si470x_fops - file operations interface
533  */
534 static const struct v4l2_file_operations si470x_fops = {
535         .owner                  = THIS_MODULE,
536         .read                   = si470x_fops_read,
537         .poll                   = si470x_fops_poll,
538         .unlocked_ioctl         = video_ioctl2,
539         .open                   = si470x_fops_open,
540         .release                = si470x_fops_release,
541 };
542
543
544
545 /**************************************************************************
546  * Video4Linux Interface
547  **************************************************************************/
548
549
550 static int si470x_s_ctrl(struct v4l2_ctrl *ctrl)
551 {
552         struct si470x_device *radio =
553                 container_of(ctrl->handler, struct si470x_device, hdl);
554
555         switch (ctrl->id) {
556         case V4L2_CID_AUDIO_VOLUME:
557                 radio->registers[SYSCONFIG2] &= ~SYSCONFIG2_VOLUME;
558                 radio->registers[SYSCONFIG2] |= ctrl->val;
559                 return si470x_set_register(radio, SYSCONFIG2);
560         case V4L2_CID_AUDIO_MUTE:
561                 if (ctrl->val)
562                         radio->registers[POWERCFG] &= ~POWERCFG_DMUTE;
563                 else
564                         radio->registers[POWERCFG] |= POWERCFG_DMUTE;
565                 return si470x_set_register(radio, POWERCFG);
566         default:
567                 return -EINVAL;
568         }
569 }
570
571
572 /*
573  * si470x_vidioc_g_tuner - get tuner attributes
574  */
575 static int si470x_vidioc_g_tuner(struct file *file, void *priv,
576                 struct v4l2_tuner *tuner)
577 {
578         struct si470x_device *radio = video_drvdata(file);
579         int retval = 0;
580
581         if (tuner->index != 0)
582                 return -EINVAL;
583
584         if (!radio->status_rssi_auto_update) {
585                 retval = si470x_get_register(radio, STATUSRSSI);
586                 if (retval < 0)
587                         return retval;
588         }
589
590         /* driver constants */
591         strcpy(tuner->name, "FM");
592         tuner->type = V4L2_TUNER_RADIO;
593         tuner->capability = V4L2_TUNER_CAP_LOW | V4L2_TUNER_CAP_STEREO |
594                             V4L2_TUNER_CAP_RDS | V4L2_TUNER_CAP_RDS_BLOCK_IO |
595                             V4L2_TUNER_CAP_HWSEEK_BOUNDED |
596                             V4L2_TUNER_CAP_HWSEEK_WRAP;
597         tuner->rangelow  =  76 * FREQ_MUL;
598         tuner->rangehigh = 108 * FREQ_MUL;
599
600         /* stereo indicator == stereo (instead of mono) */
601         if ((radio->registers[STATUSRSSI] & STATUSRSSI_ST) == 0)
602                 tuner->rxsubchans = V4L2_TUNER_SUB_MONO;
603         else
604                 tuner->rxsubchans = V4L2_TUNER_SUB_STEREO;
605         /* If there is a reliable method of detecting an RDS channel,
606            then this code should check for that before setting this
607            RDS subchannel. */
608         tuner->rxsubchans |= V4L2_TUNER_SUB_RDS;
609
610         /* mono/stereo selector */
611         if ((radio->registers[POWERCFG] & POWERCFG_MONO) == 0)
612                 tuner->audmode = V4L2_TUNER_MODE_STEREO;
613         else
614                 tuner->audmode = V4L2_TUNER_MODE_MONO;
615
616         /* min is worst, max is best; signal:0..0xffff; rssi: 0..0xff */
617         /* measured in units of dbµV in 1 db increments (max at ~75 dbµV) */
618         tuner->signal = (radio->registers[STATUSRSSI] & STATUSRSSI_RSSI);
619         /* the ideal factor is 0xffff/75 = 873,8 */
620         tuner->signal = (tuner->signal * 873) + (8 * tuner->signal / 10);
621         if (tuner->signal > 0xffff)
622                 tuner->signal = 0xffff;
623
624         /* automatic frequency control: -1: freq to low, 1 freq to high */
625         /* AFCRL does only indicate that freq. differs, not if too low/high */
626         tuner->afc = (radio->registers[STATUSRSSI] & STATUSRSSI_AFCRL) ? 1 : 0;
627
628         return retval;
629 }
630
631
632 /*
633  * si470x_vidioc_s_tuner - set tuner attributes
634  */
635 static int si470x_vidioc_s_tuner(struct file *file, void *priv,
636                 struct v4l2_tuner *tuner)
637 {
638         struct si470x_device *radio = video_drvdata(file);
639
640         if (tuner->index != 0)
641                 return -EINVAL;
642
643         /* mono/stereo selector */
644         switch (tuner->audmode) {
645         case V4L2_TUNER_MODE_MONO:
646                 radio->registers[POWERCFG] |= POWERCFG_MONO;  /* force mono */
647                 break;
648         case V4L2_TUNER_MODE_STEREO:
649         default:
650                 radio->registers[POWERCFG] &= ~POWERCFG_MONO; /* try stereo */
651                 break;
652         }
653
654         return si470x_set_register(radio, POWERCFG);
655 }
656
657
658 /*
659  * si470x_vidioc_g_frequency - get tuner or modulator radio frequency
660  */
661 static int si470x_vidioc_g_frequency(struct file *file, void *priv,
662                 struct v4l2_frequency *freq)
663 {
664         struct si470x_device *radio = video_drvdata(file);
665
666         if (freq->tuner != 0)
667                 return -EINVAL;
668
669         freq->type = V4L2_TUNER_RADIO;
670         return si470x_get_freq(radio, &freq->frequency);
671 }
672
673
674 /*
675  * si470x_vidioc_s_frequency - set tuner or modulator radio frequency
676  */
677 static int si470x_vidioc_s_frequency(struct file *file, void *priv,
678                 struct v4l2_frequency *freq)
679 {
680         struct si470x_device *radio = video_drvdata(file);
681         int retval;
682
683         if (freq->tuner != 0)
684                 return -EINVAL;
685
686         if (freq->frequency < bands[radio->band].rangelow ||
687             freq->frequency > bands[radio->band].rangehigh) {
688                 /* Switch to band 1 which covers everything we support */
689                 retval = si470x_set_band(radio, 1);
690                 if (retval)
691                         return retval;
692         }
693         return si470x_set_freq(radio, freq->frequency);
694 }
695
696
697 /*
698  * si470x_vidioc_s_hw_freq_seek - set hardware frequency seek
699  */
700 static int si470x_vidioc_s_hw_freq_seek(struct file *file, void *priv,
701                 struct v4l2_hw_freq_seek *seek)
702 {
703         struct si470x_device *radio = video_drvdata(file);
704
705         if (seek->tuner != 0)
706                 return -EINVAL;
707
708         return si470x_set_seek(radio, seek);
709 }
710
711 /*
712  * si470x_vidioc_enum_freq_bands - enumerate supported bands
713  */
714 static int si470x_vidioc_enum_freq_bands(struct file *file, void *priv,
715                                          struct v4l2_frequency_band *band)
716 {
717         if (band->tuner != 0)
718                 return -EINVAL;
719         if (band->index >= ARRAY_SIZE(bands))
720                 return -EINVAL;
721         *band = bands[band->index];
722         return 0;
723 }
724
725 const struct v4l2_ctrl_ops si470x_ctrl_ops = {
726         .s_ctrl = si470x_s_ctrl,
727 };
728
729 /*
730  * si470x_ioctl_ops - video device ioctl operations
731  */
732 static const struct v4l2_ioctl_ops si470x_ioctl_ops = {
733         .vidioc_querycap        = si470x_vidioc_querycap,
734         .vidioc_g_tuner         = si470x_vidioc_g_tuner,
735         .vidioc_s_tuner         = si470x_vidioc_s_tuner,
736         .vidioc_g_frequency     = si470x_vidioc_g_frequency,
737         .vidioc_s_frequency     = si470x_vidioc_s_frequency,
738         .vidioc_s_hw_freq_seek  = si470x_vidioc_s_hw_freq_seek,
739         .vidioc_enum_freq_bands = si470x_vidioc_enum_freq_bands,
740         .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
741         .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
742 };
743
744
745 /*
746  * si470x_viddev_template - video device interface
747  */
748 struct video_device si470x_viddev_template = {
749         .fops                   = &si470x_fops,
750         .name                   = DRIVER_NAME,
751         .release                = video_device_release_empty,
752         .ioctl_ops              = &si470x_ioctl_ops,
753 };