2 * drivers/media/radio/si470x/radio-si470x-common.c
4 * Driver for radios with Silicon Labs Si470x FM Radio Receivers
6 * Copyright (c) 2009 Tobias Lorenz <tobias.lorenz@gmx.net>
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.
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.
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
26 * 2008-01-12 Tobias Lorenz <tobias.lorenz@gmx.net>
28 * - First working version
29 * 2008-01-13 Tobias Lorenz <tobias.lorenz@gmx.net>
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>
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>
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>
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>
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>
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>
85 * - usb autosuspend support
87 * 2008-05-07 Tobias Lorenz <tobias.lorenz@gmx.net>
89 * - hardware frequency seek support
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>
107 * - add support for interrupt mode for RDS endpoint,
108 * instead of polling.
109 * Improves RDS reception significantly
113 /* kernel includes */
114 #include "radio-si470x.h"
118 /**************************************************************************
120 **************************************************************************/
123 /* 0: 200 kHz (USA, Australia) */
124 /* 1: 100 kHz (Europe, Japan) */
126 static unsigned short space = 1;
127 module_param(space, ushort, 0444);
128 MODULE_PARM_DESC(space, "Spacing: 0=200kHz 1=100kHz *2=50kHz*");
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");
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");
147 #ifdef SI470X_INT_ENA_MODE
148 static unsigned int tune_timeout = 3000;
150 static unsigned int tune_timeout = 3000;
152 module_param(tune_timeout, uint, 0644);
153 MODULE_PARM_DESC(tune_timeout, "Tune timeout: *3000*");
156 static unsigned int seek_timeout = 10000;
157 module_param(seek_timeout, uint, 0644);
158 MODULE_PARM_DESC(seek_timeout, "Seek timeout: *10000*");
162 /**************************************************************************
164 **************************************************************************/
167 * si470x_set_deemphasis - set de-emphasis
169 static int si470x_set_deemphasis(struct si470x_device *radio,
170 unsigned int deemphasis)
173 unsigned short chip_de;
175 switch (deemphasis) {
176 case V4L2_DEEMPHASIS_DISABLED:
178 case V4L2_DEEMPHASIS_50_uS: /* Europe, Australia, Japan */
181 case V4L2_DEEMPHASIS_75_uS: /* South Korea, USA */
189 radio->registers[SYSCONFIG1] &= ~SYSCONFIG1_DE;
191 radio->registers[SYSCONFIG1] |= ((chip_de << 11) & SYSCONFIG1_DE);
193 retval = si470x_set_register(radio, SYSCONFIG1);
196 dev_warn(&radio->videodev->dev,
197 "set de-emphasis failed with %d\n", retval);
203 * si470x_set_chan - set the channel
205 static int si470x_set_chan(struct si470x_device *radio, unsigned short chan)
208 unsigned long timeout;
212 radio->registers[CHANNEL] &= ~CHANNEL_CHAN;
213 radio->registers[CHANNEL] |= CHANNEL_TUNE | chan;
214 retval = si470x_set_register(radio, CHANNEL);
218 /* wait till tune operation has completed */
219 timeout = jiffies + msecs_to_jiffies(tune_timeout);
221 #ifdef SI470X_INT_ENA_MODE
222 radio->stc_flag = TUNE_WAITING;
223 retval = si470x_get_register(radio, STATUSRSSI);
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);
231 retval = si470x_get_register(radio, STATUSRSSI);
235 timed_out = time_after(jiffies, timeout);
236 } while (((radio->registers[STATUSRSSI] & STATUSRSSI_STC) == 0) &&
239 if ((radio->registers[STATUSRSSI] & STATUSRSSI_STC) == 0)
240 dev_warn(&radio->videodev->dev, "tune does not complete\n");
242 dev_warn(&radio->videodev->dev,
243 "tune timed out after %u ms\n", tune_timeout);
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;
253 /* RDS buffer flush */
254 mutex_lock(&radio->lock);
257 mutex_unlock(&radio->lock);
265 * si470x_get_freq - get the frequency
267 static int si470x_get_freq(struct si470x_device *radio, unsigned int *freq)
269 unsigned int spacing, band_bottom;
274 switch ((radio->registers[SYSCONFIG2] & SYSCONFIG2_SPACE) >> 4) {
275 /* 0: 200 kHz (USA, Australia) */
277 spacing = 0.200 * FREQ_MUL; break;
278 /* 1: 100 kHz (Europe, Japan) */
280 spacing = 0.100 * FREQ_MUL; break;
283 spacing = 0.050 * FREQ_MUL; break;
286 /* Bottom of Band (MHz) */
287 switch ((radio->registers[SYSCONFIG2] & SYSCONFIG2_BAND) >> 6) {
288 /* 0: 87.5 - 108 MHz (USA, Europe) */
290 band_bottom = 87.5 * FREQ_MUL; break;
291 /* 1: 76 - 108 MHz (Japan wide band) */
293 band_bottom = 76 * FREQ_MUL; break;
294 /* 2: 76 - 90 MHz (Japan) */
296 band_bottom = 76 * FREQ_MUL; break;
300 retval = si470x_get_register(radio, READCHAN);
301 chan = radio->registers[READCHAN] & READCHAN_READCHAN;
303 /* Frequency (MHz) = Spacing (kHz) x Channel + Bottom of Band (MHz) */
304 *freq = chan * spacing + band_bottom;
311 * si470x_set_freq - set the frequency
313 int si470x_set_freq(struct si470x_device *radio, unsigned int freq)
315 unsigned int spacing, band_bottom;
319 switch ((radio->registers[SYSCONFIG2] & SYSCONFIG2_SPACE) >> 4) {
320 /* 0: 200 kHz (USA, Australia) */
322 spacing = 0.200 * FREQ_MUL; break;
323 /* 1: 100 kHz (Europe, Japan) */
325 spacing = 0.100 * FREQ_MUL; break;
328 spacing = 0.050 * FREQ_MUL; break;
331 /* Bottom of Band (MHz) */
332 switch ((radio->registers[SYSCONFIG2] & SYSCONFIG2_BAND) >> 6) {
333 /* 0: 87.5 - 108 MHz (USA, Europe) */
335 band_bottom = 87.5 * FREQ_MUL; break;
336 /* 1: 76 - 108 MHz (Japan wide band) */
338 band_bottom = 76 * FREQ_MUL; break;
339 /* 2: 76 - 90 MHz (Japan) */
341 band_bottom = 76 * FREQ_MUL; break;
344 /* Chan = [ Freq (Mhz) - Bottom of Band (MHz) ] / Spacing (kHz) */
345 chan = (freq - band_bottom) / spacing;
347 return si470x_set_chan(radio, chan);
352 * si470x_set_seek - set seek
354 static int si470x_set_seek(struct si470x_device *radio,
355 unsigned int wrap_around, unsigned int seek_upward)
358 unsigned long timeout;
362 radio->registers[POWERCFG] |= POWERCFG_SEEK;
363 if (wrap_around == 1)
364 radio->registers[POWERCFG] &= ~POWERCFG_SKMODE;
366 radio->registers[POWERCFG] |= POWERCFG_SKMODE;
367 if (seek_upward == 1)
368 radio->registers[POWERCFG] |= POWERCFG_SEEKUP;
370 radio->registers[POWERCFG] &= ~POWERCFG_SEEKUP;
371 retval = si470x_set_register(radio, POWERCFG);
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);
382 wait_event_interruptible_timeout(radio->stc_waitq,
383 (radio->stc_flag == WAIT_OVER), msecs_to_jiffies(tune_timeout));
386 retval = si470x_get_register(radio, STATUSRSSI);
390 timed_out = time_after(jiffies, timeout);
391 } while (((radio->registers[STATUSRSSI] & STATUSRSSI_STC) == 0) &&
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");
400 dev_warn(&radio->videodev->dev,
401 "seek timed out after %u ms\n", seek_timeout);
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;
411 /* RDS buffer flush */
412 mutex_lock(&radio->lock);
415 mutex_unlock(&radio->lock);
418 /* try again, if timed out */
419 if ((retval == 0) && timed_out)
427 * si470x_start - switch on radio
429 int si470x_start(struct si470x_device *radio)
434 radio->registers[POWERCFG] =
435 POWERCFG_DMUTE | POWERCFG_ENABLE | POWERCFG_RDSM;
436 retval = si470x_set_register(radio, POWERCFG);
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 */
449 radio->registers[SYSCONFIG1] = (de << 11) & SYSCONFIG1_DE; /* DE */
451 retval = si470x_set_register(radio, SYSCONFIG1);
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);
465 radio->registers[SYSCONFIG3] = (0x01 << 4 & SYSCONFIG3_SKSNR);
466 retval = si470x_set_register(radio, SYSCONFIG3);
470 if (radio->registers[CHANNEL] & CHANNEL_CHAN) {
471 /* reset last channel */
472 retval = si470x_set_chan(radio,
473 radio->registers[CHANNEL] & CHANNEL_CHAN);
475 /* set initial frequency */
476 si470x_set_freq(radio, 87.5 * FREQ_MUL);
486 * si470x_stop - switch off radio
488 int si470x_stop(struct si470x_device *radio)
493 radio->registers[SYSCONFIG1] &= ~SYSCONFIG1_RDS;
494 retval = si470x_set_register(radio, SYSCONFIG1);
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);
510 * si470x_rds_on - switch on rds reception
512 int si470x_rds_on(struct si470x_device *radio)
517 mutex_lock(&radio->lock);
518 radio->registers[SYSCONFIG1] |= SYSCONFIG1_RDS;
519 retval = si470x_set_register(radio, SYSCONFIG1);
521 radio->registers[SYSCONFIG1] &= ~SYSCONFIG1_RDS;
522 mutex_unlock(&radio->lock);
529 /**************************************************************************
530 * File Operations Interface
531 **************************************************************************/
534 * si470x_fops_read - read RDS data
536 static ssize_t si470x_fops_read(struct file *file, char __user *buf,
537 size_t count, loff_t *ppos)
539 struct si470x_device *radio = video_drvdata(file);
541 unsigned int block_count = 0;
543 /* switch on rds reception */
544 if ((radio->registers[SYSCONFIG1] & SYSCONFIG1_RDS) == 0)
545 si470x_rds_on(radio);
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;
553 if (wait_event_interruptible(radio->read_queue,
554 radio->wr_index != radio->rd_index) < 0) {
560 /* calculate block count from byte count */
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)
569 /* always transfer rds complete blocks */
570 if (copy_to_user(buf, &radio->buffer[radio->rd_index], 3))
571 /* retval = -EFAULT; */
574 /* increment and wrap read pointer */
575 radio->rd_index += 3;
576 if (radio->rd_index >= radio->buf_size)
579 /* increment counters */
584 mutex_unlock(&radio->lock);
592 * si470x_fops_poll - poll RDS data
594 static unsigned int si470x_fops_poll(struct file *file,
595 struct poll_table_struct *pts)
597 struct si470x_device *radio = video_drvdata(file);
600 /* switch on rds reception */
601 if ((radio->registers[SYSCONFIG1] & SYSCONFIG1_RDS) == 0)
602 si470x_rds_on(radio);
604 poll_wait(file, &radio->read_queue, pts);
606 if (radio->rd_index != radio->wr_index)
607 retval = POLLIN | POLLRDNORM;
614 * si470x_fops - file operations interface
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,
627 /**************************************************************************
628 * Video4Linux Interface
629 **************************************************************************/
632 * si470x_vidioc_queryctrl - enumerate control items
634 static int si470x_vidioc_queryctrl(struct file *file, void *priv,
635 struct v4l2_queryctrl *qc)
637 struct si470x_device *radio = video_drvdata(file);
638 int retval = -EINVAL;
640 /* abort if qc->id is below V4L2_CID_BASE */
641 if (qc->id < V4L2_CID_BASE)
644 /* search video control */
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);
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;
661 dev_warn(&radio->videodev->dev,
662 "query controls failed with %d\n", retval);
668 * si470x_vidioc_g_ctrl - get the value of a control
670 static int si470x_vidioc_g_ctrl(struct file *file, void *priv,
671 struct v4l2_control *ctrl)
673 struct si470x_device *radio = video_drvdata(file);
677 retval = si470x_disconnect_check(radio);
682 case V4L2_CID_AUDIO_VOLUME:
683 ctrl->value = radio->registers[SYSCONFIG2] &
686 case V4L2_CID_AUDIO_MUTE:
687 ctrl->value = ((radio->registers[POWERCFG] &
688 POWERCFG_DMUTE) == 0) ? 1 : 0;
696 dev_warn(&radio->videodev->dev,
697 "get control failed with %d\n", retval);
703 * si470x_vidioc_s_ctrl - set the value of a control
705 static int si470x_vidioc_s_ctrl(struct file *file, void *priv,
706 struct v4l2_control *ctrl)
708 struct si470x_device *radio = video_drvdata(file);
712 retval = si470x_disconnect_check(radio);
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);
722 case V4L2_CID_AUDIO_MUTE:
723 if (ctrl->value == 1)
724 radio->registers[POWERCFG] &= ~POWERCFG_DMUTE;
726 radio->registers[POWERCFG] |= POWERCFG_DMUTE;
727 retval = si470x_set_register(radio, POWERCFG);
729 case V4L2_CID_TUNE_DEEMPHASIS:
730 retval = si470x_set_deemphasis(radio, ctrl->value);
738 dev_warn(&radio->videodev->dev,
739 "set control failed with %d\n", retval);
745 * si470x_vidioc_g_audio - get audio attributes
747 static int si470x_vidioc_g_audio(struct file *file, void *priv,
748 struct v4l2_audio *audio)
750 /* driver constants */
752 strcpy(audio->name, "Radio");
753 audio->capability = V4L2_AUDCAP_STEREO;
761 * si470x_vidioc_g_tuner - get tuner attributes
763 static int si470x_vidioc_g_tuner(struct file *file, void *priv,
764 struct v4l2_tuner *tuner)
766 struct si470x_device *radio = video_drvdata(file);
770 retval = si470x_disconnect_check(radio);
774 if (tuner->index != 0) {
779 retval = si470x_get_register(radio, STATUSRSSI);
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 |
790 switch ((radio->registers[SYSCONFIG2] & SYSCONFIG2_BAND) >> 6) {
791 /* 0: 87.5 - 108 MHz (USA, Europe, default) */
793 tuner->rangelow = 87.5 * FREQ_MUL;
794 tuner->rangehigh = 108 * FREQ_MUL;
796 /* 1: 76 - 108 MHz (Japan wide band) */
798 tuner->rangelow = 76 * FREQ_MUL;
799 tuner->rangehigh = 108 * FREQ_MUL;
801 /* 2: 76 - 90 MHz (Japan) */
803 tuner->rangelow = 76 * FREQ_MUL;
804 tuner->rangehigh = 90 * FREQ_MUL;
808 /* stereo indicator == stereo (instead of mono) */
809 if ((radio->registers[STATUSRSSI] & STATUSRSSI_ST) == 0)
810 tuner->rxsubchans = V4L2_TUNER_SUB_MONO;
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
816 tuner->rxsubchans |= V4L2_TUNER_SUB_RDS;
818 /* mono/stereo selector */
819 if ((radio->registers[POWERCFG] & POWERCFG_MONO) == 0)
820 tuner->audmode = V4L2_TUNER_MODE_STEREO;
822 tuner->audmode = V4L2_TUNER_MODE_MONO;
824 /* set signal strength in units of dbµV */
825 tuner->signal = (radio->registers[STATUSRSSI] & STATUSRSSI_RSSI);
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;
833 dev_warn(&radio->videodev->dev,
834 "get tuner failed with %d\n", retval);
839 * si470x_vidioc_s_tuner - set tuner attributes
841 static int si470x_vidioc_s_tuner(struct file *file, void *priv,
842 struct v4l2_tuner *tuner)
844 struct si470x_device *radio = video_drvdata(file);
845 int retval = -EINVAL;
848 retval = si470x_disconnect_check(radio);
852 if (tuner->index != 0)
855 /* mono/stereo selector */
856 switch (tuner->audmode) {
857 case V4L2_TUNER_MODE_MONO:
858 radio->registers[POWERCFG] |= POWERCFG_MONO; /* force mono */
860 case V4L2_TUNER_MODE_STEREO:
861 radio->registers[POWERCFG] &= ~POWERCFG_MONO; /* try stereo */
867 retval = si470x_set_register(radio, POWERCFG);
870 if (tuner->rangelow && tuner->rangehigh) {
871 if ((tuner->rangelow == 76 * FREQ_MUL) &&
872 (tuner->rangehigh == 108 * FREQ_MUL))
874 else if ((tuner->rangelow == 76 * FREQ_MUL) &&
875 (tuner->rangehigh == 90 * FREQ_MUL))
881 radio->registers[SYSCONFIG2] &= ~SYSCONFIG2_BAND;
883 radio->registers[SYSCONFIG2] |= ((band << 6) & SYSCONFIG2_BAND);
885 retval = si470x_set_register(radio, SYSCONFIG2);
890 dev_warn(&radio->videodev->dev,
891 "set tuner failed with %d\n", retval);
897 * si470x_vidioc_g_frequency - get tuner or modulator radio frequency
899 static int si470x_vidioc_g_frequency(struct file *file, void *priv,
900 struct v4l2_frequency *freq)
902 struct si470x_device *radio = video_drvdata(file);
906 retval = si470x_disconnect_check(radio);
910 if (freq->tuner != 0) {
915 freq->type = V4L2_TUNER_RADIO;
916 retval = si470x_get_freq(radio, &freq->frequency);
920 dev_warn(&radio->videodev->dev,
921 "get frequency failed with %d\n", retval);
927 * si470x_vidioc_s_frequency - set tuner or modulator radio frequency
929 static int si470x_vidioc_s_frequency(struct file *file, void *priv,
930 struct v4l2_frequency *freq)
932 struct si470x_device *radio = video_drvdata(file);
936 retval = si470x_disconnect_check(radio);
940 if (freq->tuner != 0) {
945 retval = si470x_set_freq(radio, freq->frequency);
949 dev_warn(&radio->videodev->dev,
950 "set frequency failed with %d\n", retval);
956 * si470x_vidioc_s_hw_freq_seek - set hardware frequency seek
958 static int si470x_vidioc_s_hw_freq_seek(struct file *file, void *priv,
959 struct v4l2_hw_freq_seek *seek)
961 struct si470x_device *radio = video_drvdata(file);
965 retval = si470x_disconnect_check(radio);
969 if (seek->tuner != 0) {
974 retval = si470x_set_seek(radio, seek->wrap_around, seek->seek_upward);
978 dev_warn(&radio->videodev->dev,
979 "set hardware frequency seek failed with %d\n", retval);
985 * si470x_ioctl_ops - video device ioctl operations
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,
1009 * si470x_viddev_template - video device interface
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,