2 * drivers/media/radio/radio-si470x.c
4 * Driver for USB radios for the Silicon Labs Si470x FM Radio Receivers:
5 * - Silicon Labs USB FM Radio Reference Design
6 * - ADS/Tech FM Radio Receiver (formerly Instant FM Music) (RDX-155-EF)
8 * Copyright (c) 2008 Tobias Lorenz <tobias.lorenz@gmx.net>
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
28 * 2008-01-12 Tobias Lorenz <tobias.lorenz@gmx.net>
30 * - First working version
31 * 2008-01-13 Tobias Lorenz <tobias.lorenz@gmx.net>
33 * - Improved error handling, every function now returns errno
34 * - Improved multi user access (start/mute/stop)
35 * - Channel doesn't get lost anymore after start/mute/stop
36 * - RDS support added (polling mode via interrupt EP 1)
37 * - marked default module parameters with *value*
38 * - switched from bit structs to bit masks
39 * - header file cleaned and integrated
40 * 2008-01-14 Tobias Lorenz <tobias.lorenz@gmx.net>
42 * - hex values are now lower case
43 * - commented USB ID for ADS/Tech moved on todo list
44 * - blacklisted si470x in hid-quirks.c
45 * - rds buffer handling functions integrated into *_work, *_read
46 * - rds_command in si470x_poll exchanged against simple retval
47 * - check for firmware version 15
48 * - code order and prototypes still remain the same
49 * - spacing and bottom of band codes remain the same
50 * 2008-01-16 Tobias Lorenz <tobias.lorenz@gmx.net>
52 * - code reordered to avoid function prototypes
53 * - switch/case defaults are now more user-friendly
54 * - unified comment style
55 * - applied all checkpatch.pl v1.12 suggestions
56 * except the warning about the too long lines with bit comments
57 * - renamed FMRADIO to RADIO to cut line length (checkpatch.pl)
58 * 2008-01-22 Tobias Lorenz <tobias.lorenz@gmx.net>
60 * - avoid poss. locking when doing copy_to_user which may sleep
61 * - RDS is automatically activated on read now
62 * - code cleaned of unnecessary rds_commands
63 * - USB Vendor/Product ID for ADS/Tech FM Radio Receiver verified
64 * (thanks to Guillaume RAMOUSSE)
67 * - add seeking support
68 * - add firmware download/update support
69 * - RDS support: interrupt mode, instead of polling
70 * - add LED status output (check if that's not already done in firmware)
74 /* driver definitions */
75 #define DRIVER_AUTHOR "Tobias Lorenz <tobias.lorenz@gmx.net>"
76 #define DRIVER_NAME "radio-si470x"
77 #define DRIVER_VERSION KERNEL_VERSION(1, 0, 4)
78 #define DRIVER_CARD "Silicon Labs Si470x FM Radio Receiver"
79 #define DRIVER_DESC "USB radio driver for Si470x FM Radio Receivers"
83 #include <linux/kernel.h>
84 #include <linux/module.h>
85 #include <linux/init.h>
86 #include <linux/slab.h>
87 #include <linux/input.h>
88 #include <linux/usb.h>
89 #include <linux/hid.h>
90 #include <linux/version.h>
91 #include <linux/videodev2.h>
92 #include <media/v4l2-common.h>
93 #include <media/rds.h>
96 /* USB Device ID List */
97 static struct usb_device_id si470x_usb_driver_id_table
[] = {
98 /* Silicon Labs USB FM Radio Reference Design */
99 { USB_DEVICE_AND_INTERFACE_INFO(0x10c4, 0x818a, USB_CLASS_HID
, 0, 0) },
100 /* ADS/Tech FM Radio Receiver (formerly Instant FM Music) */
101 { USB_DEVICE_AND_INTERFACE_INFO(0x06e1, 0xa155, USB_CLASS_HID
, 0, 0) },
102 /* Terminating entry */
105 MODULE_DEVICE_TABLE(usb
, si470x_usb_driver_id_table
);
109 /**************************************************************************
111 **************************************************************************/
114 static int radio_nr
= -1;
115 module_param(radio_nr
, int, 0);
116 MODULE_PARM_DESC(radio_nr
, "Radio Nr");
119 /* 0: 200 kHz (USA, Australia) */
120 /* 1: 100 kHz (Europe, Japan) */
122 static int space
= 2;
123 module_param(space
, int, 0);
124 MODULE_PARM_DESC(radio_nr
, "Spacing: 0=200kHz 1=100kHz *2=50kHz*");
126 /* Bottom of Band (MHz) */
127 /* 0: 87.5 - 108 MHz (USA, Europe)*/
128 /* 1: 76 - 108 MHz (Japan wide band) */
129 /* 2: 76 - 90 MHz (Japan) */
131 module_param(band
, int, 0);
132 MODULE_PARM_DESC(radio_nr
, "Band: 0=87.5..108MHz *1=76..108MHz* 2=76..90MHz");
136 /* 1: 50 us (Europe, Australia, Japan) */
138 module_param(de
, int, 0);
139 MODULE_PARM_DESC(radio_nr
, "De-emphasis: 0=75us *1=50us*");
142 static int usb_timeout
= 500;
143 module_param(usb_timeout
, int, 0);
144 MODULE_PARM_DESC(usb_timeout
, "USB timeout (ms): *500*");
147 static int seek_retries
= 100;
148 module_param(seek_retries
, int, 0);
149 MODULE_PARM_DESC(seek_retries
, "Seek retries: *100*");
151 /* RDS buffer blocks */
152 static int rds_buf
= 100;
153 module_param(rds_buf
, int, 0);
154 MODULE_PARM_DESC(rds_buf
, "RDS buffer entries: *100*");
156 /* RDS maximum block errors */
157 static int max_rds_errors
= 1;
158 /* 0 means 0 errors requiring correction */
159 /* 1 means 1-2 errors requiring correction (used by original USBRadio.exe) */
160 /* 2 means 3-5 errors requiring correction */
161 /* 3 means 6+ errors or errors in checkword, correction not possible */
162 module_param(max_rds_errors
, int, 0);
163 MODULE_PARM_DESC(max_rds_errors
, "RDS maximum block errors: *1*");
165 /* RDS poll frequency */
166 static int rds_poll_time
= 40;
167 /* 40 is used by the original USBRadio.exe */
168 /* 50 is used by radio-cadet */
169 /* 75 should be okay */
170 /* 80 is the usual RDS receive interval */
171 module_param(rds_poll_time
, int, 0);
172 MODULE_PARM_DESC(rds_poll_time
, "RDS poll time (ms): *40*");
176 /**************************************************************************
177 * Register Definitions
178 **************************************************************************/
179 #define RADIO_REGISTER_SIZE 2 /* 16 register bit width */
180 #define RADIO_REGISTER_NUM 16 /* DEVICEID ... RDSD */
181 #define RDS_REGISTER_NUM 6 /* STATUSRSSI ... RDSD */
183 #define DEVICEID 0 /* Device ID */
184 #define DEVICEID_PN 0xf000 /* bits 15..12: Part Number */
185 #define DEVICEID_MFGID 0x0fff /* bits 11..00: Manufacturer ID */
187 #define CHIPID 1 /* Chip ID */
188 #define CHIPID_REV 0xfc00 /* bits 15..10: Chip Version */
189 #define CHIPID_DEV 0x0200 /* bits 09..09: Device */
190 #define CHIPID_FIRMWARE 0x01ff /* bits 08..00: Firmware Version */
192 #define POWERCFG 2 /* Power Configuration */
193 #define POWERCFG_DSMUTE 0x8000 /* bits 15..15: Softmute Disable */
194 #define POWERCFG_DMUTE 0x4000 /* bits 14..14: Mute Disable */
195 #define POWERCFG_MONO 0x2000 /* bits 13..13: Mono Select */
196 #define POWERCFG_RDSM 0x0800 /* bits 11..11: RDS Mode (Si4701 only) */
197 #define POWERCFG_SKMODE 0x0400 /* bits 10..10: Seek Mode */
198 #define POWERCFG_SEEKUP 0x0200 /* bits 09..09: Seek Direction */
199 #define POWERCFG_SEEK 0x0100 /* bits 08..08: Seek */
200 #define POWERCFG_DISABLE 0x0040 /* bits 06..06: Powerup Disable */
201 #define POWERCFG_ENABLE 0x0001 /* bits 00..00: Powerup Enable */
203 #define CHANNEL 3 /* Channel */
204 #define CHANNEL_TUNE 0x8000 /* bits 15..15: Tune */
205 #define CHANNEL_CHAN 0x03ff /* bits 09..00: Channel Select */
207 #define SYSCONFIG1 4 /* System Configuration 1 */
208 #define SYSCONFIG1_RDSIEN 0x8000 /* bits 15..15: RDS Interrupt Enable (Si4701 only) */
209 #define SYSCONFIG1_STCIEN 0x4000 /* bits 14..14: Seek/Tune Complete Interrupt Enable */
210 #define SYSCONFIG1_RDS 0x1000 /* bits 12..12: RDS Enable (Si4701 only) */
211 #define SYSCONFIG1_DE 0x0800 /* bits 11..11: De-emphasis (0=75us 1=50us) */
212 #define SYSCONFIG1_AGCD 0x0400 /* bits 10..10: AGC Disable */
213 #define SYSCONFIG1_BLNDADJ 0x00c0 /* bits 07..06: Stereo/Mono Blend Level Adjustment */
214 #define SYSCONFIG1_GPIO3 0x0030 /* bits 05..04: General Purpose I/O 3 */
215 #define SYSCONFIG1_GPIO2 0x000c /* bits 03..02: General Purpose I/O 2 */
216 #define SYSCONFIG1_GPIO1 0x0003 /* bits 01..00: General Purpose I/O 1 */
218 #define SYSCONFIG2 5 /* System Configuration 2 */
219 #define SYSCONFIG2_SEEKTH 0xff00 /* bits 15..08: RSSI Seek Threshold */
220 #define SYSCONFIG2_BAND 0x0080 /* bits 07..06: Band Select */
221 #define SYSCONFIG2_SPACE 0x0030 /* bits 05..04: Channel Spacing */
222 #define SYSCONFIG2_VOLUME 0x000f /* bits 03..00: Volume */
224 #define SYSCONFIG3 6 /* System Configuration 3 */
225 #define SYSCONFIG3_SMUTER 0xc000 /* bits 15..14: Softmute Attack/Recover Rate */
226 #define SYSCONFIG3_SMUTEA 0x3000 /* bits 13..12: Softmute Attenuation */
227 #define SYSCONFIG3_SKSNR 0x00f0 /* bits 07..04: Seek SNR Threshold */
228 #define SYSCONFIG3_SKCNT 0x000f /* bits 03..00: Seek FM Impulse Detection Threshold */
230 #define TEST1 7 /* Test 1 */
231 #define TEST1_AHIZEN 0x4000 /* bits 14..14: Audio High-Z Enable */
233 #define TEST2 8 /* Test 2 */
234 /* TEST2 only contains reserved bits */
236 #define BOOTCONFIG 9 /* Boot Configuration */
237 /* BOOTCONFIG only contains reserved bits */
239 #define STATUSRSSI 10 /* Status RSSI */
240 #define STATUSRSSI_RDSR 0x8000 /* bits 15..15: RDS Ready (Si4701 only) */
241 #define STATUSRSSI_STC 0x4000 /* bits 14..14: Seek/Tune Complete */
242 #define STATUSRSSI_SF 0x2000 /* bits 13..13: Seek Fail/Band Limit */
243 #define STATUSRSSI_AFCRL 0x1000 /* bits 12..12: AFC Rail */
244 #define STATUSRSSI_RDSS 0x0800 /* bits 11..11: RDS Synchronized (Si4701 only) */
245 #define STATUSRSSI_BLERA 0x0600 /* bits 10..09: RDS Block A Errors (Si4701 only) */
246 #define STATUSRSSI_ST 0x0100 /* bits 08..08: Stereo Indicator */
247 #define STATUSRSSI_RSSI 0x00ff /* bits 07..00: RSSI (Received Signal Strength Indicator) */
249 #define READCHAN 11 /* Read Channel */
250 #define READCHAN_BLERB 0xc000 /* bits 15..14: RDS Block D Errors (Si4701 only) */
251 #define READCHAN_BLERC 0x3000 /* bits 13..12: RDS Block C Errors (Si4701 only) */
252 #define READCHAN_BLERD 0x0c00 /* bits 11..10: RDS Block B Errors (Si4701 only) */
253 #define READCHAN_READCHAN 0x03ff /* bits 09..00: Read Channel */
255 #define RDSA 12 /* RDSA */
256 #define RDSA_RDSA 0xffff /* bits 15..00: RDS Block A Data (Si4701 only) */
258 #define RDSB 13 /* RDSB */
259 #define RDSB_RDSB 0xffff /* bits 15..00: RDS Block B Data (Si4701 only) */
261 #define RDSC 14 /* RDSC */
262 #define RDSC_RDSC 0xffff /* bits 15..00: RDS Block C Data (Si4701 only) */
264 #define RDSD 15 /* RDSD */
265 #define RDSD_RDSD 0xffff /* bits 15..00: RDS Block D Data (Si4701 only) */
269 /**************************************************************************
271 **************************************************************************/
273 /* Reports 1-16 give direct read/write access to the 16 Si470x registers */
274 /* with the (REPORT_ID - 1) corresponding to the register address across USB */
275 /* endpoint 0 using GET_REPORT and SET_REPORT */
276 #define REGISTER_REPORT_SIZE (RADIO_REGISTER_SIZE + 1)
277 #define REGISTER_REPORT(reg) ((reg) + 1)
279 /* Report 17 gives direct read/write access to the entire Si470x register */
280 /* map across endpoint 0 using GET_REPORT and SET_REPORT */
281 #define ENTIRE_REPORT_SIZE (RADIO_REGISTER_NUM * RADIO_REGISTER_SIZE + 1)
282 #define ENTIRE_REPORT 17
284 /* Report 18 is used to send the lowest 6 Si470x registers up the HID */
285 /* interrupt endpoint 1 to Windows every 20 milliseconds for status */
286 #define RDS_REPORT_SIZE (RDS_REGISTER_NUM * RADIO_REGISTER_SIZE + 1)
287 #define RDS_REPORT 18
289 /* Report 19: LED state */
290 #define LED_REPORT_SIZE 3
291 #define LED_REPORT 19
293 /* Report 19: stream */
294 #define STREAM_REPORT_SIZE 3
295 #define STREAM_REPORT 19
297 /* Report 20: scratch */
298 #define SCRATCH_PAGE_SIZE 63
299 #define SCRATCH_REPORT_SIZE (SCRATCH_PAGE_SIZE + 1)
300 #define SCRATCH_REPORT 20
302 /* Reports 19-22: flash upgrade of the C8051F321 */
303 #define WRITE_REPORT 19
304 #define FLASH_REPORT 20
305 #define CRC_REPORT 21
306 #define RESPONSE_REPORT 22
308 /* Report 23: currently unused, but can accept 60 byte reports on the HID */
309 /* interrupt out endpoint 2 every 1 millisecond */
310 #define UNUSED_REPORT 23
314 /**************************************************************************
315 * Software/Hardware Versions
316 **************************************************************************/
317 #define RADIO_SW_VERSION_NOT_BOOTLOADABLE 6
318 #define RADIO_SW_VERSION 7
319 #define RADIO_SW_VERSION_CURRENT 15
320 #define RADIO_HW_VERSION 1
322 #define SCRATCH_PAGE_SW_VERSION 1
323 #define SCRATCH_PAGE_HW_VERSION 2
327 /**************************************************************************
328 * LED State Definitions
329 **************************************************************************/
330 #define LED_COMMAND 0x35
332 #define NO_CHANGE_LED 0x00
333 #define ALL_COLOR_LED 0x01 /* streaming state */
334 #define BLINK_GREEN_LED 0x02 /* connect state */
335 #define BLINK_RED_LED 0x04
336 #define BLINK_ORANGE_LED 0x10 /* disconnect state */
337 #define SOLID_GREEN_LED 0x20 /* tuning/seeking state */
338 #define SOLID_RED_LED 0x40 /* bootload state */
339 #define SOLID_ORANGE_LED 0x80
343 /**************************************************************************
344 * Stream State Definitions
345 **************************************************************************/
346 #define STREAM_COMMAND 0x36
347 #define STREAM_VIDPID 0x00
348 #define STREAM_AUDIO 0xff
352 /**************************************************************************
353 * Bootloader / Flash Commands
354 **************************************************************************/
356 /* unique id sent to bootloader and required to put into a bootload state */
357 #define UNIQUE_BL_ID 0x34
359 /* mask for the flash data */
360 #define FLASH_DATA_MASK 0x55
362 /* bootloader commands */
363 #define GET_SW_VERSION_COMMAND 0x00
364 #define SET_PAGE_COMMAND 0x01
365 #define ERASE_PAGE_COMMAND 0x02
366 #define WRITE_PAGE_COMMAND 0x03
367 #define CRC_ON_PAGE_COMMAND 0x04
368 #define READ_FLASH_BYTE_COMMAND 0x05
369 #define RESET_DEVICE_COMMAND 0x06
370 #define GET_HW_VERSION_COMMAND 0x07
373 /* bootloader command responses */
374 #define COMMAND_OK 0x01
375 #define COMMAND_FAILED 0x02
376 #define COMMAND_PENDING 0x03
379 #define COMMAND_BUFFER_SIZE 4
380 #define RESPONSE_BUFFER_SIZE 2
381 #define FLASH_BUFFER_SIZE 64
382 #define CRC_BUFFER_SIZE 3
386 /**************************************************************************
387 * General Driver Definitions
388 **************************************************************************/
391 * si470x_device - private data
393 struct si470x_device
{
394 /* reference to USB and video device */
395 struct usb_device
*usbdev
;
396 struct video_device
*videodev
;
398 /* are these really necessary ? */
401 /* report buffer (maximum 64 bytes) */
402 unsigned char buf
[64];
404 /* Silabs internal registers (0..15) */
405 unsigned short registers
[RADIO_REGISTER_NUM
];
407 /* RDS receive buffer */
408 struct work_struct work
;
409 wait_queue_head_t read_queue
;
410 struct timer_list timer
;
411 spinlock_t lock
; /* buffer locking */
412 unsigned char *buffer
; /* size is always multiple of three */
413 unsigned int buf_size
;
414 unsigned int rd_index
;
415 unsigned int wr_index
;
420 * The frequency is set in units of 62.5 Hz when using V4L2_TUNER_CAP_LOW,
421 * 62.5 kHz otherwise.
422 * The tuner is able to have a channel spacing of 50, 100 or 200 kHz.
423 * tuner->capability is therefore set to V4L2_TUNER_CAP_LOW
424 * The FREQ_MUL is then: 1 MHz / 62.5 Hz = 16000
426 #define FREQ_MUL (1000000 / 62.5)
430 /**************************************************************************
431 * General Driver Functions
432 **************************************************************************/
435 * si470x_get_report - receive a HID report
437 static int si470x_get_report(struct si470x_device
*radio
, int size
)
439 return usb_control_msg(radio
->usbdev
,
440 usb_rcvctrlpipe(radio
->usbdev
, 0),
442 USB_TYPE_CLASS
| USB_RECIP_INTERFACE
| USB_DIR_IN
,
444 radio
->buf
, size
, usb_timeout
);
449 * si470x_set_report - send a HID report
451 static int si470x_set_report(struct si470x_device
*radio
, int size
)
453 return usb_control_msg(radio
->usbdev
,
454 usb_sndctrlpipe(radio
->usbdev
, 0),
456 USB_TYPE_CLASS
| USB_RECIP_INTERFACE
| USB_DIR_OUT
,
458 radio
->buf
, size
, usb_timeout
);
463 * si470x_get_register - read register
465 static int si470x_get_register(struct si470x_device
*radio
, int regnr
)
469 radio
->buf
[0] = REGISTER_REPORT(regnr
);
471 retval
= si470x_get_report(radio
, REGISTER_REPORT_SIZE
);
473 radio
->registers
[regnr
] = (radio
->buf
[1] << 8) | radio
->buf
[2];
475 return (retval
< 0) ? -EINVAL
: 0;
480 * si470x_set_register - write register
482 static int si470x_set_register(struct si470x_device
*radio
, int regnr
)
486 radio
->buf
[0] = REGISTER_REPORT(regnr
);
487 radio
->buf
[1] = (radio
->registers
[regnr
] & 0xff00) >> 8;
488 radio
->buf
[2] = (radio
->registers
[regnr
] & 0x00ff);
490 retval
= si470x_set_report(radio
, REGISTER_REPORT_SIZE
);
492 return (retval
< 0) ? -EINVAL
: 0;
497 * si470x_get_all_registers - read entire registers
499 static int si470x_get_all_registers(struct si470x_device
*radio
)
504 radio
->buf
[0] = ENTIRE_REPORT
;
506 retval
= si470x_get_report(radio
, ENTIRE_REPORT_SIZE
);
509 for (regnr
= 0; regnr
< RADIO_REGISTER_NUM
; regnr
++)
510 radio
->registers
[regnr
] =
511 (radio
->buf
[regnr
* RADIO_REGISTER_SIZE
+ 1] << 8) |
512 radio
->buf
[regnr
* RADIO_REGISTER_SIZE
+ 2];
514 return (retval
< 0) ? -EINVAL
: 0;
519 * si470x_get_rds_registers - read rds registers
521 static int si470x_get_rds_registers(struct si470x_device
*radio
)
527 radio
->buf
[0] = RDS_REPORT
;
529 retval
= usb_interrupt_msg(radio
->usbdev
,
530 usb_rcvctrlpipe(radio
->usbdev
, 1),
531 radio
->buf
, RDS_REPORT_SIZE
, &size
, usb_timeout
);
534 for (regnr
= 0; regnr
< RDS_REGISTER_NUM
; regnr
++)
535 radio
->registers
[STATUSRSSI
+ regnr
] =
536 (radio
->buf
[regnr
* RADIO_REGISTER_SIZE
+ 1] << 8) |
537 radio
->buf
[regnr
* RADIO_REGISTER_SIZE
+ 2];
539 return (retval
< 0) ? -EINVAL
: 0;
544 * si470x_set_chan - set the channel
546 static int si470x_set_chan(struct si470x_device
*radio
, int chan
)
551 radio
->registers
[CHANNEL
] &= ~CHANNEL_CHAN
;
552 radio
->registers
[CHANNEL
] |= CHANNEL_TUNE
| chan
;
553 retval
= si470x_set_register(radio
, CHANNEL
);
557 /* wait till seek operation has completed */
560 retval
= si470x_get_register(radio
, STATUSRSSI
);
563 } while ((radio
->registers
[STATUSRSSI
] & STATUSRSSI_STC
) &&
564 (++i
< seek_retries
));
565 if (i
>= seek_retries
)
566 printk(KERN_WARNING DRIVER_NAME
567 ": seek does not finish after %d tries\n", i
);
570 radio
->registers
[CHANNEL
] &= ~CHANNEL_TUNE
;
571 return si470x_set_register(radio
, CHANNEL
);
576 * si470x_get_freq - get the frequency
578 static int si470x_get_freq(struct si470x_device
*radio
)
580 int spacing
, band_bottom
, chan
, freq
;
585 /* 0: 200 kHz (USA, Australia) */
586 case 0 : spacing
= 0.200 * FREQ_MUL
; break;
587 /* 1: 100 kHz (Europe, Japan) */
588 case 1 : spacing
= 0.100 * FREQ_MUL
; break;
590 default: spacing
= 0.050 * FREQ_MUL
; break;
593 /* Bottom of Band (MHz) */
595 /* 0: 87.5 - 108 MHz (USA, Europe) */
596 case 0 : band_bottom
= 87.5 * FREQ_MUL
; break;
597 /* 1: 76 - 108 MHz (Japan wide band) */
598 default: band_bottom
= 76 * FREQ_MUL
; break;
599 /* 2: 76 - 90 MHz (Japan) */
600 case 2 : band_bottom
= 76 * FREQ_MUL
; break;
604 retval
= si470x_get_register(radio
, READCHAN
);
607 chan
= radio
->registers
[READCHAN
] & READCHAN_READCHAN
;
609 /* Frequency (MHz) = Spacing (kHz) x Channel + Bottom of Band (MHz) */
610 freq
= chan
* spacing
+ band_bottom
;
617 * si470x_set_freq - set the frequency
619 static int si470x_set_freq(struct si470x_device
*radio
, int freq
)
621 int spacing
, band_bottom
, chan
;
625 /* 0: 200 kHz (USA, Australia) */
626 case 0 : spacing
= 0.200 * FREQ_MUL
; break;
627 /* 1: 100 kHz (Europe, Japan) */
628 case 1 : spacing
= 0.100 * FREQ_MUL
; break;
630 default: spacing
= 0.050 * FREQ_MUL
; break;
633 /* Bottom of Band (MHz) */
635 /* 0: 87.5 - 108 MHz (USA, Europe) */
636 case 0 : band_bottom
= 87.5 * FREQ_MUL
; break;
637 /* 1: 76 - 108 MHz (Japan wide band) */
638 default: band_bottom
= 76 * FREQ_MUL
; break;
639 /* 2: 76 - 90 MHz (Japan) */
640 case 2 : band_bottom
= 76 * FREQ_MUL
; break;
643 /* Chan = [ Freq (Mhz) - Bottom of Band (MHz) ] / Spacing (kHz) */
644 chan
= (freq
- band_bottom
) / spacing
;
646 return si470x_set_chan(radio
, chan
);
651 * si470x_start - switch on radio
653 static int si470x_start(struct si470x_device
*radio
)
658 radio
->registers
[POWERCFG
] =
659 POWERCFG_DMUTE
| POWERCFG_ENABLE
| POWERCFG_RDSM
;
660 retval
= si470x_set_register(radio
, POWERCFG
);
665 radio
->registers
[SYSCONFIG1
] = SYSCONFIG1_DE
;
666 retval
= si470x_set_register(radio
, SYSCONFIG1
);
671 radio
->registers
[SYSCONFIG2
] =
672 (0x3f << 8) | /* SEEKTH */
673 (band
<< 6) | /* BAND */
674 (space
<< 4) | /* SPACE */
675 15; /* VOLUME (max) */
676 retval
= si470x_set_register(radio
, SYSCONFIG2
);
680 /* reset last channel */
681 return si470x_set_chan(radio
,
682 radio
->registers
[CHANNEL
] & CHANNEL_CHAN
);
687 * si470x_stop - switch off radio
689 static int si470x_stop(struct si470x_device
*radio
)
694 radio
->registers
[SYSCONFIG1
] &= ~SYSCONFIG1_RDS
;
695 retval
= si470x_set_register(radio
, SYSCONFIG1
);
700 radio
->registers
[POWERCFG
] &= ~POWERCFG_DMUTE
;
701 /* POWERCFG_ENABLE has to automatically go low */
702 radio
->registers
[POWERCFG
] |= POWERCFG_ENABLE
| POWERCFG_DISABLE
;
703 return si470x_set_register(radio
, POWERCFG
);
708 * si470x_rds_on - switch on rds reception
710 static int si470x_rds_on(struct si470x_device
*radio
)
713 radio
->registers
[SYSCONFIG1
] |= SYSCONFIG1_RDS
;
714 return si470x_set_register(radio
, SYSCONFIG1
);
719 /**************************************************************************
720 * RDS Driver Functions
721 **************************************************************************/
724 * si470x_rds - rds processing function
726 static void si470x_rds(struct si470x_device
*radio
)
728 unsigned char tmpbuf
[3];
729 unsigned char blocknum
;
730 unsigned char bler
; /* rds block errors */
735 if (si470x_get_rds_registers(radio
) < 0)
737 if ((radio
->registers
[STATUSRSSI
] & STATUSRSSI_RDSR
) == 0) {
738 /* No RDS group ready */
741 if ((radio
->registers
[STATUSRSSI
] & STATUSRSSI_RDSS
) == 0) {
742 /* RDS decoder not synchronized */
746 /* copy four RDS blocks to internal buffer */
747 if (spin_trylock(&radio
->lock
)) {
748 /* process each rds block */
749 for (blocknum
= 0; blocknum
< 4; blocknum
++) {
752 bler
= (radio
->registers
[STATUSRSSI
] &
753 STATUSRSSI_BLERA
) >> 9;
754 rds
= radio
->registers
[RDSA
];
757 bler
= (radio
->registers
[READCHAN
] &
758 READCHAN_BLERB
) >> 14;
759 rds
= radio
->registers
[RDSB
];
762 bler
= (radio
->registers
[READCHAN
] &
763 READCHAN_BLERC
) >> 12;
764 rds
= radio
->registers
[RDSC
];
767 bler
= (radio
->registers
[READCHAN
] &
768 READCHAN_BLERD
) >> 10;
769 rds
= radio
->registers
[RDSD
];
773 /* Fill the V4L2 RDS buffer */
774 tmpbuf
[0] = rds
& 0x00ff; /* LSB */
775 tmpbuf
[1] = (rds
& 0xff00) >> 8;/* MSB */
776 tmpbuf
[2] = blocknum
; /* offset name */
777 tmpbuf
[2] |= blocknum
<< 3; /* received offset */
778 if (bler
> max_rds_errors
)
779 tmpbuf
[2] |= 0x80; /* uncorrectable errors */
781 tmpbuf
[2] |= 0x40; /* corrected error(s) */
783 /* copy RDS block to internal buffer */
784 for (i
= 0; i
< 3; i
++) {
785 radio
->buffer
[radio
->wr_index
] = tmpbuf
[i
];
789 /* wrap write pointer */
790 if (radio
->wr_index
>= radio
->buf_size
)
793 /* check for overflow */
794 if (radio
->wr_index
== radio
->rd_index
) {
795 /* increment and wrap read pointer */
796 radio
->rd_index
+= 3;
797 if (radio
->rd_index
>= radio
->buf_size
)
801 spin_unlock(&radio
->lock
);
804 /* wake up read queue */
805 if (radio
->wr_index
!= radio
->rd_index
)
806 wake_up_interruptible(&radio
->read_queue
);
811 * si470x_timer - rds timer function
813 static void si470x_timer(unsigned long data
)
815 struct si470x_device
*radio
= (struct si470x_device
*) data
;
817 schedule_work(&radio
->work
);
822 * si470x_work - rds work function
824 static void si470x_work(struct work_struct
*work
)
826 struct si470x_device
*radio
= container_of(work
, struct si470x_device
,
829 if ((radio
->registers
[SYSCONFIG1
] & SYSCONFIG1_RDS
) == 0)
833 mod_timer(&radio
->timer
, jiffies
+ msecs_to_jiffies(rds_poll_time
));
838 /**************************************************************************
839 * File Operations Interface
840 **************************************************************************/
843 * si470x_fops_read - read RDS data
845 static ssize_t
si470x_fops_read(struct file
*file
, char __user
*buf
,
846 size_t count
, loff_t
*ppos
)
848 struct si470x_device
*radio
= video_get_drvdata(video_devdata(file
));
850 unsigned int block_count
= 0;
852 /* switch on rds reception */
853 if ((radio
->registers
[SYSCONFIG1
] & SYSCONFIG1_RDS
) == 0) {
854 si470x_rds_on(radio
);
855 schedule_work(&radio
->work
);
858 /* block if no new data available */
859 while (radio
->wr_index
== radio
->rd_index
) {
860 if (file
->f_flags
& O_NONBLOCK
)
862 interruptible_sleep_on(&radio
->read_queue
);
865 /* calculate block count from byte count */
868 /* copy RDS block out of internal buffer and to user buffer */
869 if (spin_trylock(&radio
->lock
)) {
870 while (block_count
< count
) {
871 if (radio
->rd_index
== radio
->wr_index
)
874 /* always transfer rds complete blocks */
875 if (copy_to_user(buf
,
876 &radio
->buffer
[radio
->rd_index
], 3))
877 /* retval = -EFAULT; */
880 /* increment and wrap read pointer */
881 radio
->rd_index
+= 3;
882 if (radio
->rd_index
>= radio
->buf_size
)
885 /* increment counters */
891 spin_unlock(&radio
->lock
);
899 * si470x_fops_poll - poll RDS data
901 static unsigned int si470x_fops_poll(struct file
*file
,
902 struct poll_table_struct
*pts
)
904 struct si470x_device
*radio
= video_get_drvdata(video_devdata(file
));
906 /* switch on rds reception */
907 if ((radio
->registers
[SYSCONFIG1
] & SYSCONFIG1_RDS
) == 0) {
908 si470x_rds_on(radio
);
909 schedule_work(&radio
->work
);
912 poll_wait(file
, &radio
->read_queue
, pts
);
914 if (radio
->rd_index
!= radio
->wr_index
)
915 return POLLIN
| POLLRDNORM
;
922 * si470x_fops_open - file open
924 static int si470x_fops_open(struct inode
*inode
, struct file
*file
)
926 struct si470x_device
*radio
= video_get_drvdata(video_devdata(file
));
929 if (radio
->users
== 1)
930 return si470x_start(radio
);
937 * si470x_fops_release - file release
939 static int si470x_fops_release(struct inode
*inode
, struct file
*file
)
941 struct si470x_device
*radio
= video_get_drvdata(video_devdata(file
));
947 if (radio
->users
== 0) {
948 /* stop rds reception */
949 del_timer_sync(&radio
->timer
);
950 flush_scheduled_work();
952 /* cancel read processes */
953 wake_up_interruptible(&radio
->read_queue
);
955 return si470x_stop(radio
);
963 * si470x_fops - file operations interface
965 static const struct file_operations si470x_fops
= {
966 .owner
= THIS_MODULE
,
968 .read
= si470x_fops_read
,
969 .poll
= si470x_fops_poll
,
970 .ioctl
= video_ioctl2
,
971 .compat_ioctl
= v4l_compat_ioctl32
,
972 .open
= si470x_fops_open
,
973 .release
= si470x_fops_release
,
978 /**************************************************************************
979 * Video4Linux Interface
980 **************************************************************************/
983 * si470x_v4l2_queryctrl - query control
985 static struct v4l2_queryctrl si470x_v4l2_queryctrl
[] = {
986 /* HINT: the disabled controls are only here to satify kradio and such apps */
988 .id
= V4L2_CID_AUDIO_VOLUME
,
989 .type
= V4L2_CTRL_TYPE_INTEGER
,
997 .id
= V4L2_CID_AUDIO_BALANCE
,
998 .flags
= V4L2_CTRL_FLAG_DISABLED
,
1001 .id
= V4L2_CID_AUDIO_BASS
,
1002 .flags
= V4L2_CTRL_FLAG_DISABLED
,
1005 .id
= V4L2_CID_AUDIO_TREBLE
,
1006 .flags
= V4L2_CTRL_FLAG_DISABLED
,
1009 .id
= V4L2_CID_AUDIO_MUTE
,
1010 .type
= V4L2_CTRL_TYPE_BOOLEAN
,
1018 .id
= V4L2_CID_AUDIO_LOUDNESS
,
1019 .flags
= V4L2_CTRL_FLAG_DISABLED
,
1025 * si470x_vidioc_querycap - query device capabilities
1027 static int si470x_vidioc_querycap(struct file
*file
, void *priv
,
1028 struct v4l2_capability
*capability
)
1030 strlcpy(capability
->driver
, DRIVER_NAME
, sizeof(capability
->driver
));
1031 strlcpy(capability
->card
, DRIVER_CARD
, sizeof(capability
->card
));
1032 sprintf(capability
->bus_info
, "USB");
1033 capability
->version
= DRIVER_VERSION
;
1034 capability
->capabilities
= V4L2_CAP_TUNER
| V4L2_CAP_RADIO
;
1041 * si470x_vidioc_g_input - get input
1043 static int si470x_vidioc_g_input(struct file
*filp
, void *priv
,
1053 * si470x_vidioc_s_input - set input
1055 static int si470x_vidioc_s_input(struct file
*filp
, void *priv
, unsigned int i
)
1065 * si470x_vidioc_queryctrl - enumerate control items
1067 static int si470x_vidioc_queryctrl(struct file
*file
, void *priv
,
1068 struct v4l2_queryctrl
*qc
)
1072 for (i
= 0; i
< ARRAY_SIZE(si470x_v4l2_queryctrl
); i
++) {
1073 if (qc
->id
&& qc
->id
== si470x_v4l2_queryctrl
[i
].id
) {
1074 memcpy(qc
, &(si470x_v4l2_queryctrl
[i
]), sizeof(*qc
));
1084 * si470x_vidioc_g_ctrl - get the value of a control
1086 static int si470x_vidioc_g_ctrl(struct file
*file
, void *priv
,
1087 struct v4l2_control
*ctrl
)
1089 struct si470x_device
*radio
= video_get_drvdata(video_devdata(file
));
1092 case V4L2_CID_AUDIO_VOLUME
:
1093 ctrl
->value
= radio
->registers
[SYSCONFIG2
] &
1096 case V4L2_CID_AUDIO_MUTE
:
1097 ctrl
->value
= ((radio
->registers
[POWERCFG
] &
1098 POWERCFG_DMUTE
) == 0) ? 1 : 0;
1107 * si470x_vidioc_s_ctrl - set the value of a control
1109 static int si470x_vidioc_s_ctrl(struct file
*file
, void *priv
,
1110 struct v4l2_control
*ctrl
)
1112 struct si470x_device
*radio
= video_get_drvdata(video_devdata(file
));
1115 case V4L2_CID_AUDIO_VOLUME
:
1116 radio
->registers
[SYSCONFIG2
] &= ~SYSCONFIG2_VOLUME
;
1117 radio
->registers
[SYSCONFIG2
] |= ctrl
->value
;
1118 return si470x_set_register(radio
, SYSCONFIG2
);
1119 case V4L2_CID_AUDIO_MUTE
:
1120 if (ctrl
->value
== 1)
1121 radio
->registers
[POWERCFG
] &= ~POWERCFG_DMUTE
;
1123 radio
->registers
[POWERCFG
] |= POWERCFG_DMUTE
;
1124 return si470x_set_register(radio
, POWERCFG
);
1132 * si470x_vidioc_g_audio - get audio attributes
1134 static int si470x_vidioc_g_audio(struct file
*file
, void *priv
,
1135 struct v4l2_audio
*audio
)
1137 if (audio
->index
> 1)
1140 strcpy(audio
->name
, "Radio");
1141 audio
->capability
= V4L2_AUDCAP_STEREO
;
1148 * si470x_vidioc_s_audio - set audio attributes
1150 static int si470x_vidioc_s_audio(struct file
*file
, void *priv
,
1151 struct v4l2_audio
*audio
)
1153 if (audio
->index
!= 0)
1161 * si470x_vidioc_g_tuner - get tuner attributes
1163 static int si470x_vidioc_g_tuner(struct file
*file
, void *priv
,
1164 struct v4l2_tuner
*tuner
)
1167 struct si470x_device
*radio
= video_get_drvdata(video_devdata(file
));
1169 if (tuner
->index
> 0)
1172 /* read status rssi */
1173 retval
= si470x_get_register(radio
, STATUSRSSI
);
1177 strcpy(tuner
->name
, "FM");
1178 tuner
->type
= V4L2_TUNER_RADIO
;
1180 /* 0: 87.5 - 108 MHz (USA, Europe, default) */
1182 tuner
->rangelow
= 87.5 * FREQ_MUL
;
1183 tuner
->rangehigh
= 108 * FREQ_MUL
;
1185 /* 1: 76 - 108 MHz (Japan wide band) */
1187 tuner
->rangelow
= 76 * FREQ_MUL
;
1188 tuner
->rangehigh
= 108 * FREQ_MUL
;
1190 /* 2: 76 - 90 MHz (Japan) */
1192 tuner
->rangelow
= 76 * FREQ_MUL
;
1193 tuner
->rangehigh
= 90 * FREQ_MUL
;
1196 tuner
->rxsubchans
= V4L2_TUNER_SUB_MONO
| V4L2_TUNER_SUB_STEREO
;
1197 tuner
->capability
= V4L2_TUNER_CAP_LOW
;
1199 /* Stereo indicator == Stereo (instead of Mono) */
1200 if ((radio
->registers
[STATUSRSSI
] & STATUSRSSI_ST
) == 1)
1201 tuner
->audmode
= V4L2_TUNER_MODE_STEREO
;
1203 tuner
->audmode
= V4L2_TUNER_MODE_MONO
;
1205 /* min is worst, max is best; signal:0..0xffff; rssi: 0..0xff */
1206 tuner
->signal
= (radio
->registers
[STATUSRSSI
] & STATUSRSSI_RSSI
)
1209 /* automatic frequency control: -1: freq to low, 1 freq to high */
1217 * si470x_vidioc_s_tuner - set tuner attributes
1219 static int si470x_vidioc_s_tuner(struct file
*file
, void *priv
,
1220 struct v4l2_tuner
*tuner
)
1222 struct si470x_device
*radio
= video_get_drvdata(video_devdata(file
));
1224 if (tuner
->index
> 0)
1227 if (tuner
->audmode
== V4L2_TUNER_MODE_MONO
)
1228 radio
->registers
[POWERCFG
] |= POWERCFG_MONO
; /* force mono */
1230 radio
->registers
[POWERCFG
] &= ~POWERCFG_MONO
; /* try stereo */
1232 return si470x_set_register(radio
, POWERCFG
);
1237 * si470x_vidioc_g_frequency - get tuner or modulator radio frequency
1239 static int si470x_vidioc_g_frequency(struct file
*file
, void *priv
,
1240 struct v4l2_frequency
*freq
)
1242 struct si470x_device
*radio
= video_get_drvdata(video_devdata(file
));
1244 freq
->type
= V4L2_TUNER_RADIO
;
1245 freq
->frequency
= si470x_get_freq(radio
);
1252 * si470x_vidioc_s_frequency - set tuner or modulator radio frequency
1254 static int si470x_vidioc_s_frequency(struct file
*file
, void *priv
,
1255 struct v4l2_frequency
*freq
)
1257 struct si470x_device
*radio
= video_get_drvdata(video_devdata(file
));
1259 if (freq
->type
!= V4L2_TUNER_RADIO
)
1262 return si470x_set_freq(radio
, freq
->frequency
);
1267 * si470x_viddev_tamples - video device interface
1269 static struct video_device si470x_viddev_template
= {
1270 .fops
= &si470x_fops
,
1271 .name
= DRIVER_NAME
,
1272 .type
= VID_TYPE_TUNER
,
1273 .release
= video_device_release
,
1274 .vidioc_querycap
= si470x_vidioc_querycap
,
1275 .vidioc_g_input
= si470x_vidioc_g_input
,
1276 .vidioc_s_input
= si470x_vidioc_s_input
,
1277 .vidioc_queryctrl
= si470x_vidioc_queryctrl
,
1278 .vidioc_g_ctrl
= si470x_vidioc_g_ctrl
,
1279 .vidioc_s_ctrl
= si470x_vidioc_s_ctrl
,
1280 .vidioc_g_audio
= si470x_vidioc_g_audio
,
1281 .vidioc_s_audio
= si470x_vidioc_s_audio
,
1282 .vidioc_g_tuner
= si470x_vidioc_g_tuner
,
1283 .vidioc_s_tuner
= si470x_vidioc_s_tuner
,
1284 .vidioc_g_frequency
= si470x_vidioc_g_frequency
,
1285 .vidioc_s_frequency
= si470x_vidioc_s_frequency
,
1286 .owner
= THIS_MODULE
,
1291 /**************************************************************************
1293 **************************************************************************/
1296 * si470x_usb_driver_probe - probe for the device
1298 static int si470x_usb_driver_probe(struct usb_interface
*intf
,
1299 const struct usb_device_id
*id
)
1301 struct si470x_device
*radio
;
1303 /* memory and interface allocations */
1304 radio
= kmalloc(sizeof(struct si470x_device
), GFP_KERNEL
);
1307 radio
->videodev
= video_device_alloc();
1308 if (!radio
->videodev
) {
1312 memcpy(radio
->videodev
, &si470x_viddev_template
,
1313 sizeof(si470x_viddev_template
));
1315 radio
->usbdev
= interface_to_usbdev(intf
);
1316 video_set_drvdata(radio
->videodev
, radio
);
1317 if (video_register_device(radio
->videodev
, VFL_TYPE_RADIO
, radio_nr
)) {
1318 printk(KERN_WARNING DRIVER_NAME
1319 ": Could not register video device\n");
1320 video_device_release(radio
->videodev
);
1324 usb_set_intfdata(intf
, radio
);
1326 /* show some infos about the specific device */
1327 if (si470x_get_all_registers(radio
) < 0) {
1328 video_device_release(radio
->videodev
);
1332 printk(KERN_INFO DRIVER_NAME
": DeviceID=0x%4.4x ChipID=0x%4.4x\n",
1333 radio
->registers
[DEVICEID
], radio
->registers
[CHIPID
]);
1335 /* check if firmware is current */
1336 if ((radio
->registers
[CHIPID
] & CHIPID_FIRMWARE
)
1337 < RADIO_SW_VERSION_CURRENT
)
1338 printk(KERN_WARNING DRIVER_NAME
1339 ": This driver is known to work with chip version %d, "
1340 "but the device has firmware %d.\n"
1342 "If you have some trouble using this driver, please "
1343 "report to V4L ML at video4linux-list@redhat.com\n",
1344 radio
->registers
[CHIPID
] & CHIPID_FIRMWARE
,
1345 RADIO_SW_VERSION_CURRENT
);
1347 /* set initial frequency */
1348 si470x_set_freq(radio
, 87.5 * FREQ_MUL
); /* available in all regions */
1350 /* rds initialization */
1351 radio
->buf_size
= rds_buf
* 3;
1352 radio
->buffer
= kmalloc(radio
->buf_size
, GFP_KERNEL
);
1353 if (!radio
->buffer
) {
1354 video_device_release(radio
->videodev
);
1358 radio
->wr_index
= 0;
1359 radio
->rd_index
= 0;
1360 init_waitqueue_head(&radio
->read_queue
);
1362 /* prepare polling via eventd */
1363 INIT_WORK(&radio
->work
, si470x_work
);
1364 init_timer(&radio
->timer
);
1365 radio
->timer
.function
= si470x_timer
;
1366 radio
->timer
.data
= (unsigned long) radio
;
1373 * si470x_usb_driver_disconnect - disconnect the device
1375 static void si470x_usb_driver_disconnect(struct usb_interface
*intf
)
1377 struct si470x_device
*radio
= usb_get_intfdata(intf
);
1379 del_timer_sync(&radio
->timer
);
1380 flush_scheduled_work();
1382 usb_set_intfdata(intf
, NULL
);
1384 video_unregister_device(radio
->videodev
);
1385 kfree(radio
->buffer
);
1392 * si470x_usb_driver - usb driver interface
1394 static struct usb_driver si470x_usb_driver
= {
1395 .name
= DRIVER_NAME
,
1396 .probe
= si470x_usb_driver_probe
,
1397 .disconnect
= si470x_usb_driver_disconnect
,
1398 .id_table
= si470x_usb_driver_id_table
,
1403 /**************************************************************************
1405 **************************************************************************/
1408 * si470x_module_init - module init
1410 static int __init
si470x_module_init(void)
1412 printk(KERN_INFO DRIVER_DESC
"\n");
1413 return usb_register(&si470x_usb_driver
);
1418 * si470x_module_exit - module exit
1420 static void __exit
si470x_module_exit(void)
1422 usb_deregister(&si470x_usb_driver
);
1426 module_init(si470x_module_init
);
1427 module_exit(si470x_module_exit
);
1429 MODULE_LICENSE("GPL");
1430 MODULE_AUTHOR(DRIVER_AUTHOR
);
1431 MODULE_DESCRIPTION(DRIVER_DESC
);
1432 MODULE_VERSION("1.0.4");