upload tizen1.0 source
[kernel/linux-2.6.36.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  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21  */
22
23
24 /*
25  * History:
26  * 2008-01-12   Tobias Lorenz <tobias.lorenz@gmx.net>
27  *              Version 1.0.0
28  *              - First working version
29  * 2008-01-13   Tobias Lorenz <tobias.lorenz@gmx.net>
30  *              Version 1.0.1
31  *              - Improved error handling, every function now returns errno
32  *              - Improved multi user access (start/mute/stop)
33  *              - Channel doesn't get lost anymore after start/mute/stop
34  *              - RDS support added (polling mode via interrupt EP 1)
35  *              - marked default module parameters with *value*
36  *              - switched from bit structs to bit masks
37  *              - header file cleaned and integrated
38  * 2008-01-14   Tobias Lorenz <tobias.lorenz@gmx.net>
39  *              Version 1.0.2
40  *              - hex values are now lower case
41  *              - commented USB ID for ADS/Tech moved on todo list
42  *              - blacklisted si470x in hid-quirks.c
43  *              - rds buffer handling functions integrated into *_work, *_read
44  *              - rds_command in si470x_poll exchanged against simple retval
45  *              - check for firmware version 15
46  *              - code order and prototypes still remain the same
47  *              - spacing and bottom of band codes remain the same
48  * 2008-01-16   Tobias Lorenz <tobias.lorenz@gmx.net>
49  *              Version 1.0.3
50  *              - code reordered to avoid function prototypes
51  *              - switch/case defaults are now more user-friendly
52  *              - unified comment style
53  *              - applied all checkpatch.pl v1.12 suggestions
54  *                except the warning about the too long lines with bit comments
55  *              - renamed FMRADIO to RADIO to cut line length (checkpatch.pl)
56  * 2008-01-22   Tobias Lorenz <tobias.lorenz@gmx.net>
57  *              Version 1.0.4
58  *              - avoid poss. locking when doing copy_to_user which may sleep
59  *              - RDS is automatically activated on read now
60  *              - code cleaned of unnecessary rds_commands
61  *              - USB Vendor/Product ID for ADS/Tech FM Radio Receiver verified
62  *                (thanks to Guillaume RAMOUSSE)
63  * 2008-01-27   Tobias Lorenz <tobias.lorenz@gmx.net>
64  *              Version 1.0.5
65  *              - number of seek_retries changed to tune_timeout
66  *              - fixed problem with incomplete tune operations by own buffers
67  *              - optimization of variables and printf types
68  *              - improved error logging
69  * 2008-01-31   Tobias Lorenz <tobias.lorenz@gmx.net>
70  *              Oliver Neukum <oliver@neukum.org>
71  *              Version 1.0.6
72  *              - fixed coverity checker warnings in *_usb_driver_disconnect
73  *              - probe()/open() race by correct ordering in probe()
74  *              - DMA coherency rules by separate allocation of all buffers
75  *              - use of endianness macros
76  *              - abuse of spinlock, replaced by mutex
77  *              - racy handling of timer in disconnect,
78  *                replaced by delayed_work
79  *              - racy interruptible_sleep_on(),
80  *                replaced with wait_event_interruptible()
81  *              - handle signals in read()
82  * 2008-02-08   Tobias Lorenz <tobias.lorenz@gmx.net>
83  *              Oliver Neukum <oliver@neukum.org>
84  *              Version 1.0.7
85  *              - usb autosuspend support
86  *              - unplugging fixed
87  * 2008-05-07   Tobias Lorenz <tobias.lorenz@gmx.net>
88  *              Version 1.0.8
89  *              - hardware frequency seek support
90  *              - afc indication
91  *              - more safety checks, let si470x_get_freq return errno
92  *              - vidioc behavior corrected according to v4l2 spec
93  * 2008-10-20   Alexey Klimov <klimov.linux@gmail.com>
94  *              - add support for KWorld USB FM Radio FM700
95  *              - blacklisted KWorld radio in hid-core.c and hid-ids.h
96  * 2008-12-03   Mark Lord <mlord@pobox.com>
97  *              - add support for DealExtreme USB Radio
98  * 2009-01-31   Bob Ross <pigiron@gmx.com>
99  *              - correction of stereo detection/setting
100  *              - correction of signal strength indicator scaling
101  * 2009-01-31   Rick Bronson <rick@efn.org>
102  *              Tobias Lorenz <tobias.lorenz@gmx.net>
103  *              - add LED status output
104  *              - get HW/SW version from scratchpad
105  * 2009-06-16   Edouard Lafargue <edouard@lafargue.name>
106  *              Version 1.0.10
107  *              - add support for interrupt mode for RDS endpoint,
108  *                instead of polling.
109  *                Improves RDS reception significantly
110  */
111
112
113 /* kernel includes */
114 #include "radio-si470x.h"
115
116
117
118 /**************************************************************************
119  * Module Parameters
120  **************************************************************************/
121
122 /* Spacing (kHz) */
123 /* 0: 200 kHz (USA, Australia) */
124 /* 1: 100 kHz (Europe, Japan) */
125 /* 2:  50 kHz */
126 static unsigned short space = 1;
127 module_param(space, ushort, 0444);
128 MODULE_PARM_DESC(space, "Spacing: 0=200kHz 1=100kHz *2=50kHz*");
129
130 /* Bottom of Band (MHz) */
131 /* 0: 87.5 - 108 MHz (USA, Europe)*/
132 /* 1: 76   - 108 MHz (Japan wide band) */
133 /* 2: 76   -  90 MHz (Japan) */
134 static unsigned short band = 0;
135 module_param(band, ushort, 0444);
136 MODULE_PARM_DESC(band, "Band: 0=87.5..108MHz *1=76..108MHz* 2=76..90MHz");
137
138 /* De-emphasis */
139 /* 0: V4L2_DEEMPHASIS_DISABLED */
140 /* 1: V4L2_DEEMPHASIS_50_uS (Europe, Australia, Japan) */
141 /* 2: V4L2_DEEMPHASIS_75_uS (South Korea, USA) */
142 static unsigned short de = 1;
143 module_param(de, ushort, 0444);
144 MODULE_PARM_DESC(de, "De-emphasis: 0=Disabled *1=50us* 2=75us");
145
146 /* Tune timeout */
147 #ifdef SI470X_INT_ENA_MODE
148 static unsigned int tune_timeout = 3000;
149 #else   
150 static unsigned int tune_timeout = 3000;
151 #endif
152 module_param(tune_timeout, uint, 0644);
153 MODULE_PARM_DESC(tune_timeout, "Tune timeout: *3000*");
154
155 /* Seek timeout */
156 static unsigned int seek_timeout = 10000;
157 module_param(seek_timeout, uint, 0644);
158 MODULE_PARM_DESC(seek_timeout, "Seek timeout: *10000*");
159
160
161
162 /**************************************************************************
163  * Generic Functions
164  **************************************************************************/
165
166 /*
167  * si470x_set_deemphasis - set de-emphasis
168  */
169 static int si470x_set_deemphasis(struct si470x_device *radio,
170                                 unsigned int deemphasis)
171 {
172         int retval;
173         unsigned short chip_de;
174
175         switch (deemphasis) {
176         case V4L2_DEEMPHASIS_DISABLED:
177                 return 0;
178         case V4L2_DEEMPHASIS_50_uS: /* Europe, Australia, Japan */
179                 chip_de = 1;
180                 break;
181         case V4L2_DEEMPHASIS_75_uS: /* South Korea, USA */
182                 chip_de = 0;
183                 break;
184         default:
185                 retval = -EINVAL;
186                 goto done;
187         }
188         /* clear de bit */
189         radio->registers[SYSCONFIG1] &= ~SYSCONFIG1_DE;
190         /* set de bit */
191         radio->registers[SYSCONFIG1] |= ((chip_de << 11) & SYSCONFIG1_DE);
192
193         retval = si470x_set_register(radio, SYSCONFIG1);
194 done:
195         if (retval < 0)
196                 dev_warn(&radio->videodev->dev,
197                         "set de-emphasis failed with %d\n", retval);
198
199         return retval;
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         unsigned long timeout;
209         bool timed_out = 0;
210
211         /* start tuning */
212         radio->registers[CHANNEL] &= ~CHANNEL_CHAN;
213         radio->registers[CHANNEL] |= CHANNEL_TUNE | chan;
214         retval = si470x_set_register(radio, CHANNEL);
215         if (retval < 0)
216                 goto done;
217
218         /* wait till tune operation has completed */
219         timeout = jiffies + msecs_to_jiffies(tune_timeout);
220         
221 #ifdef SI470X_INT_ENA_MODE
222         radio->stc_flag = TUNE_WAITING;
223         retval = si470x_get_register(radio, STATUSRSSI);
224         if (retval < 0)
225                 goto stop;
226         wait_event_interruptible_timeout(radio->stc_waitq, 
227                 (radio->stc_flag == WAIT_OVER), msecs_to_jiffies(tune_timeout));
228         timed_out = time_after(jiffies, timeout);
229 #else   
230         do {
231                 retval = si470x_get_register(radio, STATUSRSSI);
232                 if (retval < 0)
233                         goto stop;
234                 msleep(30);
235                 timed_out = time_after(jiffies, timeout);
236         } while (((radio->registers[STATUSRSSI] & STATUSRSSI_STC) == 0) &&
237                 (!timed_out));
238 #endif
239         if ((radio->registers[STATUSRSSI] & STATUSRSSI_STC) == 0)
240                 dev_warn(&radio->videodev->dev, "tune does not complete\n");
241         if (timed_out)
242                 dev_warn(&radio->videodev->dev,
243                         "tune timed out after %u ms\n", tune_timeout);
244
245 stop:
246         /* stop tuning */
247         radio->registers[CHANNEL] &= ~CHANNEL_TUNE;
248         retval = si470x_set_register(radio, CHANNEL);
249 #ifdef SI470X_INT_ENA_MODE
250         radio->stc_flag = WAIT_OVER;
251 #endif
252
253         /* RDS buffer flush */
254         mutex_lock(&radio->lock);
255         radio->wr_index = 0;
256         radio->rd_index = 0;
257         mutex_unlock(&radio->lock);
258
259 done:
260         return retval;
261 }
262
263
264 /*
265  * si470x_get_freq - get the frequency
266  */
267 static int si470x_get_freq(struct si470x_device *radio, unsigned int *freq)
268 {
269         unsigned int spacing, band_bottom;
270         unsigned short chan;
271         int retval;
272
273         /* Spacing (kHz) */
274         switch ((radio->registers[SYSCONFIG2] & SYSCONFIG2_SPACE) >> 4) {
275         /* 0: 200 kHz (USA, Australia) */
276         case 0:
277                 spacing = 0.200 * FREQ_MUL; break;
278         /* 1: 100 kHz (Europe, Japan) */
279         case 1:
280                 spacing = 0.100 * FREQ_MUL; break;
281         /* 2:  50 kHz */
282         default:
283                 spacing = 0.050 * FREQ_MUL; break;
284         };
285
286         /* Bottom of Band (MHz) */
287         switch ((radio->registers[SYSCONFIG2] & SYSCONFIG2_BAND) >> 6) {
288         /* 0: 87.5 - 108 MHz (USA, Europe) */
289         case 0:
290                 band_bottom = 87.5 * FREQ_MUL; break;
291         /* 1: 76   - 108 MHz (Japan wide band) */
292         default:
293                 band_bottom = 76   * FREQ_MUL; break;
294         /* 2: 76   -  90 MHz (Japan) */
295         case 2:
296                 band_bottom = 76   * FREQ_MUL; break;
297         };
298
299         /* read channel */
300         retval = si470x_get_register(radio, READCHAN);
301         chan = radio->registers[READCHAN] & READCHAN_READCHAN;
302
303         /* Frequency (MHz) = Spacing (kHz) x Channel + Bottom of Band (MHz) */
304         *freq = chan * spacing + band_bottom;
305
306         return retval;
307 }
308
309
310 /*
311  * si470x_set_freq - set the frequency
312  */
313 int si470x_set_freq(struct si470x_device *radio, unsigned int freq)
314 {
315         unsigned int spacing, band_bottom;
316         unsigned short chan;
317
318         /* Spacing (kHz) */
319         switch ((radio->registers[SYSCONFIG2] & SYSCONFIG2_SPACE) >> 4) {
320         /* 0: 200 kHz (USA, Australia) */
321         case 0:
322                 spacing = 0.200 * FREQ_MUL; break;
323         /* 1: 100 kHz (Europe, Japan) */
324         case 1:
325                 spacing = 0.100 * FREQ_MUL; break;
326         /* 2:  50 kHz */
327         default:
328                 spacing = 0.050 * FREQ_MUL; break;
329         };
330
331         /* Bottom of Band (MHz) */
332         switch ((radio->registers[SYSCONFIG2] & SYSCONFIG2_BAND) >> 6) {
333         /* 0: 87.5 - 108 MHz (USA, Europe) */
334         case 0:
335                 band_bottom = 87.5 * FREQ_MUL; break;
336         /* 1: 76   - 108 MHz (Japan wide band) */
337         default:
338                 band_bottom = 76   * FREQ_MUL; break;
339         /* 2: 76   -  90 MHz (Japan) */
340         case 2:
341                 band_bottom = 76   * FREQ_MUL; break;
342         };
343
344         /* Chan = [ Freq (Mhz) - Bottom of Band (MHz) ] / Spacing (kHz) */
345         chan = (freq - band_bottom) / spacing;
346
347         return si470x_set_chan(radio, chan);
348 }
349
350
351 /*
352  * si470x_set_seek - set seek
353  */
354 static int si470x_set_seek(struct si470x_device *radio,
355                 unsigned int wrap_around, unsigned int seek_upward)
356 {
357         int retval = 0;
358         unsigned long timeout;
359         bool timed_out = 0;
360
361         /* start seeking */
362         radio->registers[POWERCFG] |= POWERCFG_SEEK;
363         if (wrap_around == 1)
364                 radio->registers[POWERCFG] &= ~POWERCFG_SKMODE;
365         else
366                 radio->registers[POWERCFG] |= POWERCFG_SKMODE;
367         if (seek_upward == 1)
368                 radio->registers[POWERCFG] |= POWERCFG_SEEKUP;
369         else
370                 radio->registers[POWERCFG] &= ~POWERCFG_SEEKUP;
371         retval = si470x_set_register(radio, POWERCFG);
372         if (retval < 0)
373                 goto done;
374
375         /* wait till seek operation has completed */
376         timeout = jiffies + msecs_to_jiffies(seek_timeout);
377 #ifdef SI470X_INT_ENA_MODE
378         radio->stc_flag = SEEK_WAITING;
379         retval = si470x_get_register(radio, STATUSRSSI);
380         if (retval < 0)
381                 goto stop;
382         wait_event_interruptible_timeout(radio->stc_waitq, 
383                 (radio->stc_flag == WAIT_OVER), msecs_to_jiffies(tune_timeout));
384 #else   
385         do {
386                 retval = si470x_get_register(radio, STATUSRSSI);
387                 if (retval < 0)
388                         goto stop;
389                 msleep(30);
390                 timed_out = time_after(jiffies, timeout);
391         } while (((radio->registers[STATUSRSSI] & STATUSRSSI_STC) == 0) &&
392                 (!timed_out));
393 #endif
394         if ((radio->registers[STATUSRSSI] & STATUSRSSI_STC) == 0)
395                 dev_warn(&radio->videodev->dev, "seek does not complete\n");
396         if (radio->registers[STATUSRSSI] & STATUSRSSI_SF)
397                 dev_warn(&radio->videodev->dev,
398                         "seek failed / band limit reached\n");
399         if (timed_out)
400                 dev_warn(&radio->videodev->dev,
401                         "seek timed out after %u ms\n", seek_timeout);
402
403 stop:
404         /* stop seeking */
405         radio->registers[POWERCFG] &= ~POWERCFG_SEEK;
406         retval = si470x_set_register(radio, POWERCFG);
407 #ifdef SI470X_INT_ENA_MODE
408         radio->stc_flag = WAIT_OVER;
409 #endif
410
411         /* RDS buffer flush */
412         mutex_lock(&radio->lock);
413         radio->wr_index = 0;
414         radio->rd_index = 0;
415         mutex_unlock(&radio->lock);
416
417 done:
418         /* try again, if timed out */
419         if ((retval == 0) && timed_out)
420                 retval = -EAGAIN;
421
422         return retval;
423 }
424
425
426 /*
427  * si470x_start - switch on radio
428  */
429 int si470x_start(struct si470x_device *radio)
430 {
431         int retval;
432
433         /* powercfg */
434         radio->registers[POWERCFG] =
435                 POWERCFG_DMUTE | POWERCFG_ENABLE | POWERCFG_RDSM;
436         retval = si470x_set_register(radio, POWERCFG);
437         if (retval < 0)
438                 goto done;
439
440         /* sysconfig 1 */
441 #ifdef SI470X_INT_ENA_MODE
442         radio->registers[SYSCONFIG1] = 
443                 SYSCONFIG1_RDSIEN | /* RDS Intrrupt Enable */
444                 SYSCONFIG1_STCIEN | /* Seek, Tune Intrrupt enable */
445                 SYSCONFIG1_RDS | /* RDS Enable */
446                 ((de << 11) & SYSCONFIG1_DE) |  /* DE */
447                 ((1 << 2) & SYSCONFIG1_GPIO2);  /* set GPO as Interrupt pin */
448 #else
449         radio->registers[SYSCONFIG1] = (de << 11) & SYSCONFIG1_DE;      /* DE */
450 #endif
451         retval = si470x_set_register(radio, SYSCONFIG1);
452         if (retval < 0)
453                 goto done;
454
455         /* sysconfig 2 */
456         radio->registers[SYSCONFIG2] =
457                 (0x00  << 8) |                          /* SEEKTH */
458                 ((band  << 6) & SYSCONFIG2_BAND)  |     /* BAND */
459                 ((space << 4) & SYSCONFIG2_SPACE) |     /* SPACE */
460                 15;                                     /* VOLUME (max) */
461         retval = si470x_set_register(radio, SYSCONFIG2);
462         if (retval < 0)
463                 goto done;
464
465         radio->registers[SYSCONFIG3] = (0x01 << 4 & SYSCONFIG3_SKSNR);
466         retval = si470x_set_register(radio, SYSCONFIG3);
467         if(retval < 0)
468                 goto done;
469
470         if (radio->registers[CHANNEL] & CHANNEL_CHAN) {
471                 /* reset last channel */
472                 retval = si470x_set_chan(radio,
473                         radio->registers[CHANNEL] & CHANNEL_CHAN);
474         } else {
475                 /* set initial frequency */
476                 si470x_set_freq(radio, 87.5 * FREQ_MUL); 
477         }
478
479
480 done:
481         return retval;
482 }
483
484
485 /*
486  * si470x_stop - switch off radio
487  */
488 int si470x_stop(struct si470x_device *radio)
489 {
490         int retval;
491
492         /* sysconfig 1 */
493         radio->registers[SYSCONFIG1] &= ~SYSCONFIG1_RDS;
494         retval = si470x_set_register(radio, SYSCONFIG1);
495         if (retval < 0)
496                 goto done;
497
498         /* powercfg */
499         radio->registers[POWERCFG] &= ~POWERCFG_DMUTE;
500         /* POWERCFG_ENABLE has to automatically go low */
501         radio->registers[POWERCFG] |= POWERCFG_ENABLE | POWERCFG_DISABLE;
502         retval = si470x_set_register(radio, POWERCFG);
503
504 done:
505         return retval;
506 }
507
508
509 /*
510  * si470x_rds_on - switch on rds reception
511  */
512 int si470x_rds_on(struct si470x_device *radio)
513 {
514         int retval;
515
516         /* sysconfig 1 */
517         mutex_lock(&radio->lock);
518         radio->registers[SYSCONFIG1] |= SYSCONFIG1_RDS;
519         retval = si470x_set_register(radio, SYSCONFIG1);
520         if (retval < 0)
521                 radio->registers[SYSCONFIG1] &= ~SYSCONFIG1_RDS;
522         mutex_unlock(&radio->lock);
523
524         return retval;
525 }
526
527
528
529 /**************************************************************************
530  * File Operations Interface
531  **************************************************************************/
532
533 /*
534  * si470x_fops_read - read RDS data
535  */
536 static ssize_t si470x_fops_read(struct file *file, char __user *buf,
537                 size_t count, loff_t *ppos)
538 {
539         struct si470x_device *radio = video_drvdata(file);
540         int retval = 0;
541         unsigned int block_count = 0;
542
543         /* switch on rds reception */
544         if ((radio->registers[SYSCONFIG1] & SYSCONFIG1_RDS) == 0)
545                 si470x_rds_on(radio);
546
547         /* block if no new data available */
548         while (radio->wr_index == radio->rd_index) {
549                 if (file->f_flags & O_NONBLOCK) {
550                         retval = -EWOULDBLOCK;
551                         goto done;
552                 }
553                 if (wait_event_interruptible(radio->read_queue,
554                         radio->wr_index != radio->rd_index) < 0) {
555                         retval = -EINTR;
556                         goto done;
557                 }
558         }
559
560         /* calculate block count from byte count */
561         count /= 3;
562
563         /* copy RDS block out of internal buffer and to user buffer */
564         mutex_lock(&radio->lock);
565         while (block_count < count) {
566                 if (radio->rd_index == radio->wr_index)
567                         break;
568
569                 /* always transfer rds complete blocks */
570                 if (copy_to_user(buf, &radio->buffer[radio->rd_index], 3))
571                         /* retval = -EFAULT; */
572                         break;
573
574                 /* increment and wrap read pointer */
575                 radio->rd_index += 3;
576                 if (radio->rd_index >= radio->buf_size)
577                         radio->rd_index = 0;
578
579                 /* increment counters */
580                 block_count++;
581                 buf += 3;
582                 retval += 3;
583         }
584         mutex_unlock(&radio->lock);
585
586 done:
587         return retval;
588 }
589
590
591 /*
592  * si470x_fops_poll - poll RDS data
593  */
594 static unsigned int si470x_fops_poll(struct file *file,
595                 struct poll_table_struct *pts)
596 {
597         struct si470x_device *radio = video_drvdata(file);
598         int retval = 0;
599
600         /* switch on rds reception */
601         if ((radio->registers[SYSCONFIG1] & SYSCONFIG1_RDS) == 0)
602                 si470x_rds_on(radio);
603
604         poll_wait(file, &radio->read_queue, pts);
605
606         if (radio->rd_index != radio->wr_index)
607                 retval = POLLIN | POLLRDNORM;
608
609         return retval;
610 }
611
612
613 /*
614  * si470x_fops - file operations interface
615  */
616 static const struct v4l2_file_operations si470x_fops = {
617         .owner                  = THIS_MODULE,
618         .read                   = si470x_fops_read,
619         .poll                   = si470x_fops_poll,
620         .unlocked_ioctl         = video_ioctl2,
621         .open                   = si470x_fops_open,
622         .release                = si470x_fops_release,
623 };
624
625
626
627 /**************************************************************************
628  * Video4Linux Interface
629  **************************************************************************/
630
631 /*
632  * si470x_vidioc_queryctrl - enumerate control items
633  */
634 static int si470x_vidioc_queryctrl(struct file *file, void *priv,
635                 struct v4l2_queryctrl *qc)
636 {
637         struct si470x_device *radio = video_drvdata(file);
638         int retval = -EINVAL;
639
640         /* abort if qc->id is below V4L2_CID_BASE */
641         if (qc->id < V4L2_CID_BASE)
642                 goto done;
643
644         /* search video control */
645         switch (qc->id) {
646         case V4L2_CID_AUDIO_VOLUME:
647                 return v4l2_ctrl_query_fill(qc, 0, 15, 1, 15);
648         case V4L2_CID_AUDIO_MUTE:
649                 return v4l2_ctrl_query_fill(qc, 0, 1, 1, 1);
650         }
651
652         /* disable unsupported base controls */
653         /* to satisfy kradio and such apps */
654         if ((retval == -EINVAL) && (qc->id < V4L2_CID_LASTP1)) {
655                 qc->flags = V4L2_CTRL_FLAG_DISABLED;
656                 retval = 0;
657         }
658
659 done:
660         if (retval < 0)
661                 dev_warn(&radio->videodev->dev,
662                         "query controls failed with %d\n", retval);
663         return retval;
664 }
665
666
667 /*
668  * si470x_vidioc_g_ctrl - get the value of a control
669  */
670 static int si470x_vidioc_g_ctrl(struct file *file, void *priv,
671                 struct v4l2_control *ctrl)
672 {
673         struct si470x_device *radio = video_drvdata(file);
674         int retval = 0;
675
676         /* safety checks */
677         retval = si470x_disconnect_check(radio);
678         if (retval)
679                 goto done;
680
681         switch (ctrl->id) {
682         case V4L2_CID_AUDIO_VOLUME:
683                 ctrl->value = radio->registers[SYSCONFIG2] &
684                                 SYSCONFIG2_VOLUME;
685                 break;
686         case V4L2_CID_AUDIO_MUTE:
687                 ctrl->value = ((radio->registers[POWERCFG] &
688                                 POWERCFG_DMUTE) == 0) ? 1 : 0;
689                 break;
690         default:
691                 retval = -EINVAL;
692         }
693
694 done:
695         if (retval < 0)
696                 dev_warn(&radio->videodev->dev,
697                         "get control failed with %d\n", retval);
698         return retval;
699 }
700
701
702 /*
703  * si470x_vidioc_s_ctrl - set the value of a control
704  */
705 static int si470x_vidioc_s_ctrl(struct file *file, void *priv,
706                 struct v4l2_control *ctrl)
707 {
708         struct si470x_device *radio = video_drvdata(file);
709         int retval = 0;
710
711         /* safety checks */
712         retval = si470x_disconnect_check(radio);
713         if (retval)
714                 goto done;
715
716         switch (ctrl->id) {
717         case V4L2_CID_AUDIO_VOLUME:
718                 radio->registers[SYSCONFIG2] &= ~SYSCONFIG2_VOLUME;
719                 radio->registers[SYSCONFIG2] |= ctrl->value;
720                 retval = si470x_set_register(radio, SYSCONFIG2);
721                 break;
722         case V4L2_CID_AUDIO_MUTE:
723                 if (ctrl->value == 1)
724                         radio->registers[POWERCFG] &= ~POWERCFG_DMUTE;
725                 else
726                         radio->registers[POWERCFG] |= POWERCFG_DMUTE;
727                 retval = si470x_set_register(radio, POWERCFG);
728                 break;
729         case V4L2_CID_TUNE_DEEMPHASIS:
730                 retval = si470x_set_deemphasis(radio, ctrl->value);
731                 break;
732         default:
733                 retval = -EINVAL;
734         }
735
736 done:
737         if (retval < 0)
738                 dev_warn(&radio->videodev->dev,
739                         "set control failed with %d\n", retval);
740         return retval;
741 }
742
743
744 /*
745  * si470x_vidioc_g_audio - get audio attributes
746  */
747 static int si470x_vidioc_g_audio(struct file *file, void *priv,
748                 struct v4l2_audio *audio)
749 {
750         /* driver constants */
751         audio->index = 0;
752         strcpy(audio->name, "Radio");
753         audio->capability = V4L2_AUDCAP_STEREO;
754         audio->mode = 0;
755
756         return 0;
757 }
758
759
760 /*
761  * si470x_vidioc_g_tuner - get tuner attributes
762  */
763 static int si470x_vidioc_g_tuner(struct file *file, void *priv,
764                 struct v4l2_tuner *tuner)
765 {
766         struct si470x_device *radio = video_drvdata(file);
767         int retval = 0;
768
769         /* safety checks */
770         retval = si470x_disconnect_check(radio);
771         if (retval)
772                 goto done;
773
774         if (tuner->index != 0) {
775                 retval = -EINVAL;
776                 goto done;
777         }
778
779         retval = si470x_get_register(radio, STATUSRSSI);
780         if (retval < 0)
781                 goto done;
782
783         /* driver constants */
784         strcpy(tuner->name, "FM");
785         tuner->type = V4L2_TUNER_RADIO;
786         tuner->capability = V4L2_TUNER_CAP_LOW | V4L2_TUNER_CAP_STEREO |
787                             V4L2_TUNER_CAP_RDS;
788
789         /* range limits */
790         switch ((radio->registers[SYSCONFIG2] & SYSCONFIG2_BAND) >> 6) {
791         /* 0: 87.5 - 108 MHz (USA, Europe, default) */
792         default:
793                 tuner->rangelow  =  87.5 * FREQ_MUL;
794                 tuner->rangehigh = 108   * FREQ_MUL;
795                 break;
796         /* 1: 76   - 108 MHz (Japan wide band) */
797         case 1:
798                 tuner->rangelow  =  76   * FREQ_MUL;
799                 tuner->rangehigh = 108   * FREQ_MUL;
800                 break;
801         /* 2: 76   -  90 MHz (Japan) */
802         case 2:
803                 tuner->rangelow  =  76   * FREQ_MUL;
804                 tuner->rangehigh =  90   * FREQ_MUL;
805                 break;
806         };
807
808         /* stereo indicator == stereo (instead of mono) */
809         if ((radio->registers[STATUSRSSI] & STATUSRSSI_ST) == 0)
810                 tuner->rxsubchans = V4L2_TUNER_SUB_MONO;
811         else
812                 tuner->rxsubchans = V4L2_TUNER_SUB_MONO | V4L2_TUNER_SUB_STEREO;
813         /* If there is a reliable method of detecting an RDS channel,
814            then this code should check for that before setting this
815            RDS subchannel. */
816         tuner->rxsubchans |= V4L2_TUNER_SUB_RDS;
817
818         /* mono/stereo selector */
819         if ((radio->registers[POWERCFG] & POWERCFG_MONO) == 0)
820                 tuner->audmode = V4L2_TUNER_MODE_STEREO;
821         else
822                 tuner->audmode = V4L2_TUNER_MODE_MONO;
823
824         /* set signal strength in units of dbµV */
825         tuner->signal = (radio->registers[STATUSRSSI] & STATUSRSSI_RSSI);
826
827         /* automatic frequency control: -1: freq to low, 1 freq to high */
828         /* AFCRL does only indicate that freq. differs, not if too low/high */
829         tuner->afc = (radio->registers[STATUSRSSI] & STATUSRSSI_AFCRL) ? 1 : 0;
830
831 done:
832         if (retval < 0)
833                 dev_warn(&radio->videodev->dev,
834                         "get tuner failed with %d\n", retval);
835         return retval;
836 }
837
838 /*
839  * si470x_vidioc_s_tuner - set tuner attributes
840  */
841 static int si470x_vidioc_s_tuner(struct file *file, void *priv,
842                 struct v4l2_tuner *tuner)
843 {
844         struct si470x_device *radio = video_drvdata(file);
845         int retval = -EINVAL;
846
847         /* safety checks */
848         retval = si470x_disconnect_check(radio);
849         if (retval)
850                 goto done;
851
852         if (tuner->index != 0)
853                 goto done;
854
855         /* mono/stereo selector */
856         switch (tuner->audmode) {
857         case V4L2_TUNER_MODE_MONO:
858                 radio->registers[POWERCFG] |= POWERCFG_MONO;  /* force mono */
859                 break;
860         case V4L2_TUNER_MODE_STEREO:
861                 radio->registers[POWERCFG] &= ~POWERCFG_MONO; /* try stereo */
862                 break;
863         default:
864                 goto done;
865         }
866
867         retval = si470x_set_register(radio, POWERCFG);
868
869         /* range limit */
870         if (tuner->rangelow && tuner->rangehigh) {
871                 if ((tuner->rangelow == 76 * FREQ_MUL) &&
872                         (tuner->rangehigh == 108 * FREQ_MUL))
873                         band = 1;
874                 else if ((tuner->rangelow == 76 * FREQ_MUL) &&
875                         (tuner->rangehigh == 90 * FREQ_MUL))
876                         band = 2;
877                 else
878                         band = 0;
879
880                 /* clear */
881                 radio->registers[SYSCONFIG2] &= ~SYSCONFIG2_BAND;
882                 /* set */
883                 radio->registers[SYSCONFIG2] |= ((band << 6) & SYSCONFIG2_BAND);
884
885                 retval = si470x_set_register(radio, SYSCONFIG2);
886         }
887
888 done:
889         if (retval < 0)
890                 dev_warn(&radio->videodev->dev,
891                         "set tuner failed with %d\n", retval);
892         return retval;
893 }
894
895
896 /*
897  * si470x_vidioc_g_frequency - get tuner or modulator radio frequency
898  */
899 static int si470x_vidioc_g_frequency(struct file *file, void *priv,
900                 struct v4l2_frequency *freq)
901 {
902         struct si470x_device *radio = video_drvdata(file);
903         int retval = 0;
904
905         /* safety checks */
906         retval = si470x_disconnect_check(radio);
907         if (retval)
908                 goto done;
909
910         if (freq->tuner != 0) {
911                 retval = -EINVAL;
912                 goto done;
913         }
914
915         freq->type = V4L2_TUNER_RADIO;
916         retval = si470x_get_freq(radio, &freq->frequency);
917
918 done:
919         if (retval < 0)
920                 dev_warn(&radio->videodev->dev,
921                         "get frequency failed with %d\n", retval);
922         return retval;
923 }
924
925
926 /*
927  * si470x_vidioc_s_frequency - set tuner or modulator radio frequency
928  */
929 static int si470x_vidioc_s_frequency(struct file *file, void *priv,
930                 struct v4l2_frequency *freq)
931 {
932         struct si470x_device *radio = video_drvdata(file);
933         int retval = 0;
934
935         /* safety checks */
936         retval = si470x_disconnect_check(radio);
937         if (retval)
938                 goto done;
939
940         if (freq->tuner != 0) {
941                 retval = -EINVAL;
942                 goto done;
943         }
944
945         retval = si470x_set_freq(radio, freq->frequency);
946
947 done:
948         if (retval < 0)
949                 dev_warn(&radio->videodev->dev,
950                         "set frequency failed with %d\n", retval);
951         return retval;
952 }
953
954
955 /*
956  * si470x_vidioc_s_hw_freq_seek - set hardware frequency seek
957  */
958 static int si470x_vidioc_s_hw_freq_seek(struct file *file, void *priv,
959                 struct v4l2_hw_freq_seek *seek)
960 {
961         struct si470x_device *radio = video_drvdata(file);
962         int retval = 0;
963
964         /* safety checks */
965         retval = si470x_disconnect_check(radio);
966         if (retval)
967                 goto done;
968
969         if (seek->tuner != 0) {
970                 retval = -EINVAL;
971                 goto done;
972         }
973
974         retval = si470x_set_seek(radio, seek->wrap_around, seek->seek_upward);
975
976 done:
977         if (retval < 0)
978                 dev_warn(&radio->videodev->dev,
979                         "set hardware frequency seek failed with %d\n", retval);
980         return retval;
981 }
982
983
984 /*
985  * si470x_ioctl_ops - video device ioctl operations
986  */
987 static const struct v4l2_ioctl_ops si470x_ioctl_ops = {
988         .vidioc_querycap        = si470x_vidioc_querycap,
989         .vidioc_queryctrl       = si470x_vidioc_queryctrl,
990         .vidioc_g_ctrl          = si470x_vidioc_g_ctrl,
991         .vidioc_s_ctrl          = si470x_vidioc_s_ctrl,
992         .vidioc_g_audio         = si470x_vidioc_g_audio,
993         .vidioc_g_tuner         = si470x_vidioc_g_tuner,
994         .vidioc_s_tuner         = si470x_vidioc_s_tuner,
995         .vidioc_g_frequency     = si470x_vidioc_g_frequency,
996         .vidioc_s_frequency     = si470x_vidioc_s_frequency,
997         .vidioc_s_hw_freq_seek  = si470x_vidioc_s_hw_freq_seek,
998 #ifdef CONFIG_VIDEO_ADV_DEBUG
999         /* FIXME: usb side? */
1000 #if defined(CONFIG_I2C_SI470X) || defined(CONFIG_I2C_SI470X_MODULE)
1001         .vidioc_g_register      = si470x_vidioc_g_register,
1002         .vidioc_s_register      = si470x_vidioc_s_register,
1003 #endif
1004 #endif
1005 };
1006
1007
1008 /*
1009  * si470x_viddev_template - video device interface
1010  */
1011 struct video_device si470x_viddev_template = {
1012         .fops                   = &si470x_fops,
1013         .name                   = DRIVER_NAME,
1014         .release                = video_device_release,
1015         .ioctl_ops              = &si470x_ioctl_ops,
1016 };