blob: 84ab3d5776dadcaced7186deaad01044b3027d62 [file] [log] [blame]
Joonyoung Shimb09cd162009-08-09 14:22:20 -03001/*
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 */
126static unsigned short space = 2;
127module_param(space, ushort, 0444);
128MODULE_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) */
134static unsigned short band = 1;
135module_param(band, ushort, 0444);
136MODULE_PARM_DESC(band, "Band: 0=87.5..108MHz *1=76..108MHz* 2=76..90MHz");
137
138/* De-emphasis */
139/* 0: 75 us (USA) */
140/* 1: 50 us (Europe, Australia, Japan) */
141static unsigned short de = 1;
142module_param(de, ushort, 0444);
143MODULE_PARM_DESC(de, "De-emphasis: 0=75us *1=50us*");
144
145/* Tune timeout */
146static unsigned int tune_timeout = 3000;
147module_param(tune_timeout, uint, 0644);
148MODULE_PARM_DESC(tune_timeout, "Tune timeout: *3000*");
149
150/* Seek timeout */
151static unsigned int seek_timeout = 5000;
152module_param(seek_timeout, uint, 0644);
153MODULE_PARM_DESC(seek_timeout, "Seek timeout: *5000*");
154
155
156
157/**************************************************************************
158 * Generic Functions
159 **************************************************************************/
160
161/*
162 * si470x_set_chan - set the channel
163 */
164static int si470x_set_chan(struct si470x_device *radio, unsigned short chan)
165{
166 int retval;
Joonyoung Shimb09cd162009-08-09 14:22:20 -0300167 bool timed_out = 0;
168
169 /* start tuning */
170 radio->registers[CHANNEL] &= ~CHANNEL_CHAN;
171 radio->registers[CHANNEL] |= CHANNEL_TUNE | chan;
172 retval = si470x_set_register(radio, CHANNEL);
173 if (retval < 0)
174 goto done;
175
Hans de Goede77947112012-06-14 09:43:12 -0300176 /* wait till tune operation has completed */
177 INIT_COMPLETION(radio->completion);
178 retval = wait_for_completion_timeout(&radio->completion,
179 msecs_to_jiffies(tune_timeout));
180 if (!retval)
181 timed_out = true;
Joonyoung Shim0830be32011-03-11 03:54:46 -0300182
Joonyoung Shimb09cd162009-08-09 14:22:20 -0300183 if ((radio->registers[STATUSRSSI] & STATUSRSSI_STC) == 0)
Hans Verkuil4967d532012-05-04 09:16:57 -0300184 dev_warn(&radio->videodev.dev, "tune does not complete\n");
Joonyoung Shimb09cd162009-08-09 14:22:20 -0300185 if (timed_out)
Hans Verkuil4967d532012-05-04 09:16:57 -0300186 dev_warn(&radio->videodev.dev,
Joonyoung Shima9d6fd52009-08-09 14:22:41 -0300187 "tune timed out after %u ms\n", tune_timeout);
Joonyoung Shimb09cd162009-08-09 14:22:20 -0300188
Joonyoung Shimb09cd162009-08-09 14:22:20 -0300189 /* stop tuning */
190 radio->registers[CHANNEL] &= ~CHANNEL_TUNE;
191 retval = si470x_set_register(radio, CHANNEL);
192
193done:
194 return retval;
195}
196
197
198/*
199 * si470x_get_freq - get the frequency
200 */
201static int si470x_get_freq(struct si470x_device *radio, unsigned int *freq)
202{
203 unsigned int spacing, band_bottom;
204 unsigned short chan;
205 int retval;
206
207 /* Spacing (kHz) */
208 switch ((radio->registers[SYSCONFIG2] & SYSCONFIG2_SPACE) >> 4) {
209 /* 0: 200 kHz (USA, Australia) */
210 case 0:
211 spacing = 0.200 * FREQ_MUL; break;
212 /* 1: 100 kHz (Europe, Japan) */
213 case 1:
214 spacing = 0.100 * FREQ_MUL; break;
215 /* 2: 50 kHz */
216 default:
217 spacing = 0.050 * FREQ_MUL; break;
218 };
219
220 /* Bottom of Band (MHz) */
221 switch ((radio->registers[SYSCONFIG2] & SYSCONFIG2_BAND) >> 6) {
222 /* 0: 87.5 - 108 MHz (USA, Europe) */
223 case 0:
224 band_bottom = 87.5 * FREQ_MUL; break;
225 /* 1: 76 - 108 MHz (Japan wide band) */
226 default:
227 band_bottom = 76 * FREQ_MUL; break;
228 /* 2: 76 - 90 MHz (Japan) */
229 case 2:
230 band_bottom = 76 * FREQ_MUL; break;
231 };
232
233 /* read channel */
234 retval = si470x_get_register(radio, READCHAN);
235 chan = radio->registers[READCHAN] & READCHAN_READCHAN;
236
237 /* Frequency (MHz) = Spacing (kHz) x Channel + Bottom of Band (MHz) */
238 *freq = chan * spacing + band_bottom;
239
240 return retval;
241}
242
243
244/*
245 * si470x_set_freq - set the frequency
246 */
247int si470x_set_freq(struct si470x_device *radio, unsigned int freq)
248{
Hans Verkuileae63ae2012-05-04 09:20:53 -0300249 unsigned int spacing, band_bottom, band_top;
Joonyoung Shimb09cd162009-08-09 14:22:20 -0300250 unsigned short chan;
251
252 /* Spacing (kHz) */
253 switch ((radio->registers[SYSCONFIG2] & SYSCONFIG2_SPACE) >> 4) {
254 /* 0: 200 kHz (USA, Australia) */
255 case 0:
256 spacing = 0.200 * FREQ_MUL; break;
257 /* 1: 100 kHz (Europe, Japan) */
258 case 1:
259 spacing = 0.100 * FREQ_MUL; break;
260 /* 2: 50 kHz */
261 default:
262 spacing = 0.050 * FREQ_MUL; break;
263 };
264
Hans Verkuileae63ae2012-05-04 09:20:53 -0300265 /* Bottom/Top of Band (MHz) */
Joonyoung Shimb09cd162009-08-09 14:22:20 -0300266 switch ((radio->registers[SYSCONFIG2] & SYSCONFIG2_BAND) >> 6) {
267 /* 0: 87.5 - 108 MHz (USA, Europe) */
268 case 0:
Hans Verkuileae63ae2012-05-04 09:20:53 -0300269 band_bottom = 87.5 * FREQ_MUL;
270 band_top = 108 * FREQ_MUL;
271 break;
Joonyoung Shimb09cd162009-08-09 14:22:20 -0300272 /* 1: 76 - 108 MHz (Japan wide band) */
273 default:
Hans Verkuileae63ae2012-05-04 09:20:53 -0300274 band_bottom = 76 * FREQ_MUL;
275 band_top = 108 * FREQ_MUL;
276 break;
Joonyoung Shimb09cd162009-08-09 14:22:20 -0300277 /* 2: 76 - 90 MHz (Japan) */
278 case 2:
Hans Verkuileae63ae2012-05-04 09:20:53 -0300279 band_bottom = 76 * FREQ_MUL;
280 band_top = 90 * FREQ_MUL;
281 break;
Joonyoung Shimb09cd162009-08-09 14:22:20 -0300282 };
283
Hans Verkuileae63ae2012-05-04 09:20:53 -0300284 freq = clamp(freq, band_bottom, band_top);
Joonyoung Shimb09cd162009-08-09 14:22:20 -0300285 /* Chan = [ Freq (Mhz) - Bottom of Band (MHz) ] / Spacing (kHz) */
286 chan = (freq - band_bottom) / spacing;
287
288 return si470x_set_chan(radio, chan);
289}
290
291
292/*
293 * si470x_set_seek - set seek
294 */
295static int si470x_set_seek(struct si470x_device *radio,
296 unsigned int wrap_around, unsigned int seek_upward)
297{
298 int retval = 0;
Joonyoung Shimb09cd162009-08-09 14:22:20 -0300299 bool timed_out = 0;
300
301 /* start seeking */
302 radio->registers[POWERCFG] |= POWERCFG_SEEK;
303 if (wrap_around == 1)
304 radio->registers[POWERCFG] &= ~POWERCFG_SKMODE;
305 else
306 radio->registers[POWERCFG] |= POWERCFG_SKMODE;
307 if (seek_upward == 1)
308 radio->registers[POWERCFG] |= POWERCFG_SEEKUP;
309 else
310 radio->registers[POWERCFG] &= ~POWERCFG_SEEKUP;
311 retval = si470x_set_register(radio, POWERCFG);
312 if (retval < 0)
Hans Verkuil340bd4c2012-04-30 19:49:27 -0300313 return retval;
Joonyoung Shimb09cd162009-08-09 14:22:20 -0300314
Hans de Goede77947112012-06-14 09:43:12 -0300315 /* wait till tune operation has completed */
316 INIT_COMPLETION(radio->completion);
317 retval = wait_for_completion_timeout(&radio->completion,
318 msecs_to_jiffies(seek_timeout));
319 if (!retval)
320 timed_out = true;
Joonyoung Shim0830be32011-03-11 03:54:46 -0300321
Joonyoung Shimb09cd162009-08-09 14:22:20 -0300322 if ((radio->registers[STATUSRSSI] & STATUSRSSI_STC) == 0)
Hans Verkuil4967d532012-05-04 09:16:57 -0300323 dev_warn(&radio->videodev.dev, "seek does not complete\n");
Joonyoung Shimb09cd162009-08-09 14:22:20 -0300324 if (radio->registers[STATUSRSSI] & STATUSRSSI_SF)
Hans Verkuil4967d532012-05-04 09:16:57 -0300325 dev_warn(&radio->videodev.dev,
Joonyoung Shima9d6fd52009-08-09 14:22:41 -0300326 "seek failed / band limit reached\n");
Joonyoung Shimb09cd162009-08-09 14:22:20 -0300327
Joonyoung Shimb09cd162009-08-09 14:22:20 -0300328 /* stop seeking */
329 radio->registers[POWERCFG] &= ~POWERCFG_SEEK;
330 retval = si470x_set_register(radio, POWERCFG);
331
Joonyoung Shimb09cd162009-08-09 14:22:20 -0300332 /* try again, if timed out */
Hans Verkuil340bd4c2012-04-30 19:49:27 -0300333 if (retval == 0 && timed_out)
Hans Verkuil54f60192012-05-27 07:25:06 -0300334 return -ENODATA;
Joonyoung Shimb09cd162009-08-09 14:22:20 -0300335 return retval;
336}
337
338
339/*
340 * si470x_start - switch on radio
341 */
342int si470x_start(struct si470x_device *radio)
343{
344 int retval;
345
346 /* powercfg */
347 radio->registers[POWERCFG] =
348 POWERCFG_DMUTE | POWERCFG_ENABLE | POWERCFG_RDSM;
349 retval = si470x_set_register(radio, POWERCFG);
350 if (retval < 0)
351 goto done;
352
353 /* sysconfig 1 */
Tobias Lorenz131ddd12011-01-08 14:12:30 -0300354 radio->registers[SYSCONFIG1] =
355 (de << 11) & SYSCONFIG1_DE; /* DE*/
Joonyoung Shimb09cd162009-08-09 14:22:20 -0300356 retval = si470x_set_register(radio, SYSCONFIG1);
357 if (retval < 0)
358 goto done;
359
360 /* sysconfig 2 */
361 radio->registers[SYSCONFIG2] =
Hans de Goedeb9664252012-06-14 09:43:13 -0300362 (0x1f << 8) | /* SEEKTH */
Joonyoung Shimb09cd162009-08-09 14:22:20 -0300363 ((band << 6) & SYSCONFIG2_BAND) | /* BAND */
364 ((space << 4) & SYSCONFIG2_SPACE) | /* SPACE */
365 15; /* VOLUME (max) */
366 retval = si470x_set_register(radio, SYSCONFIG2);
367 if (retval < 0)
368 goto done;
369
370 /* reset last channel */
371 retval = si470x_set_chan(radio,
372 radio->registers[CHANNEL] & CHANNEL_CHAN);
373
374done:
375 return retval;
376}
377
378
379/*
380 * si470x_stop - switch off radio
381 */
382int si470x_stop(struct si470x_device *radio)
383{
384 int retval;
385
386 /* sysconfig 1 */
387 radio->registers[SYSCONFIG1] &= ~SYSCONFIG1_RDS;
388 retval = si470x_set_register(radio, SYSCONFIG1);
389 if (retval < 0)
390 goto done;
391
392 /* powercfg */
393 radio->registers[POWERCFG] &= ~POWERCFG_DMUTE;
394 /* POWERCFG_ENABLE has to automatically go low */
395 radio->registers[POWERCFG] |= POWERCFG_ENABLE | POWERCFG_DISABLE;
396 retval = si470x_set_register(radio, POWERCFG);
397
398done:
399 return retval;
400}
401
402
403/*
404 * si470x_rds_on - switch on rds reception
405 */
Mauro Carvalho Chehabf2f8e852010-09-15 08:35:45 -0300406static int si470x_rds_on(struct si470x_device *radio)
Joonyoung Shimb09cd162009-08-09 14:22:20 -0300407{
408 int retval;
409
410 /* sysconfig 1 */
Joonyoung Shimb09cd162009-08-09 14:22:20 -0300411 radio->registers[SYSCONFIG1] |= SYSCONFIG1_RDS;
412 retval = si470x_set_register(radio, SYSCONFIG1);
413 if (retval < 0)
414 radio->registers[SYSCONFIG1] &= ~SYSCONFIG1_RDS;
Joonyoung Shimb09cd162009-08-09 14:22:20 -0300415
416 return retval;
417}
418
419
420
421/**************************************************************************
Joonyoung Shim1aa925c2009-12-10 16:49:34 -0300422 * File Operations Interface
423 **************************************************************************/
424
425/*
426 * si470x_fops_read - read RDS data
427 */
428static ssize_t si470x_fops_read(struct file *file, char __user *buf,
429 size_t count, loff_t *ppos)
430{
431 struct si470x_device *radio = video_drvdata(file);
432 int retval = 0;
433 unsigned int block_count = 0;
434
435 /* switch on rds reception */
436 if ((radio->registers[SYSCONFIG1] & SYSCONFIG1_RDS) == 0)
437 si470x_rds_on(radio);
438
439 /* block if no new data available */
440 while (radio->wr_index == radio->rd_index) {
441 if (file->f_flags & O_NONBLOCK) {
442 retval = -EWOULDBLOCK;
443 goto done;
444 }
445 if (wait_event_interruptible(radio->read_queue,
446 radio->wr_index != radio->rd_index) < 0) {
447 retval = -EINTR;
448 goto done;
449 }
450 }
451
452 /* calculate block count from byte count */
453 count /= 3;
454
455 /* copy RDS block out of internal buffer and to user buffer */
Joonyoung Shim1aa925c2009-12-10 16:49:34 -0300456 while (block_count < count) {
457 if (radio->rd_index == radio->wr_index)
458 break;
459
460 /* always transfer rds complete blocks */
461 if (copy_to_user(buf, &radio->buffer[radio->rd_index], 3))
462 /* retval = -EFAULT; */
463 break;
464
465 /* increment and wrap read pointer */
466 radio->rd_index += 3;
467 if (radio->rd_index >= radio->buf_size)
468 radio->rd_index = 0;
469
470 /* increment counters */
471 block_count++;
472 buf += 3;
473 retval += 3;
474 }
Joonyoung Shim1aa925c2009-12-10 16:49:34 -0300475
476done:
477 return retval;
478}
479
480
481/*
482 * si470x_fops_poll - poll RDS data
483 */
484static unsigned int si470x_fops_poll(struct file *file,
485 struct poll_table_struct *pts)
486{
487 struct si470x_device *radio = video_drvdata(file);
Hans Verkuileae63ae2012-05-04 09:20:53 -0300488 unsigned long req_events = poll_requested_events(pts);
489 int retval = v4l2_ctrl_poll(file, pts);
Joonyoung Shim1aa925c2009-12-10 16:49:34 -0300490
Hans Verkuileae63ae2012-05-04 09:20:53 -0300491 if (req_events & (POLLIN | POLLRDNORM)) {
492 /* switch on rds reception */
493 if ((radio->registers[SYSCONFIG1] & SYSCONFIG1_RDS) == 0)
494 si470x_rds_on(radio);
Mauro Carvalho Chehabf2f8e852010-09-15 08:35:45 -0300495
Hans Verkuileae63ae2012-05-04 09:20:53 -0300496 poll_wait(file, &radio->read_queue, pts);
Joonyoung Shim1aa925c2009-12-10 16:49:34 -0300497
Hans Verkuileae63ae2012-05-04 09:20:53 -0300498 if (radio->rd_index != radio->wr_index)
499 retval |= POLLIN | POLLRDNORM;
500 }
Joonyoung Shim1aa925c2009-12-10 16:49:34 -0300501
502 return retval;
503}
504
505
506/*
507 * si470x_fops - file operations interface
508 */
509static const struct v4l2_file_operations si470x_fops = {
510 .owner = THIS_MODULE,
511 .read = si470x_fops_read,
512 .poll = si470x_fops_poll,
Mauro Carvalho Chehabf2f8e852010-09-15 08:35:45 -0300513 .unlocked_ioctl = video_ioctl2,
Joonyoung Shim1aa925c2009-12-10 16:49:34 -0300514 .open = si470x_fops_open,
515 .release = si470x_fops_release,
516};
517
518
519
520/**************************************************************************
Joonyoung Shimb09cd162009-08-09 14:22:20 -0300521 * Video4Linux Interface
522 **************************************************************************/
523
Hans Verkuil4967d532012-05-04 09:16:57 -0300524
525static int si470x_s_ctrl(struct v4l2_ctrl *ctrl)
Joonyoung Shimb09cd162009-08-09 14:22:20 -0300526{
Hans Verkuil4967d532012-05-04 09:16:57 -0300527 struct si470x_device *radio =
528 container_of(ctrl->handler, struct si470x_device, hdl);
Joonyoung Shimb09cd162009-08-09 14:22:20 -0300529
530 switch (ctrl->id) {
531 case V4L2_CID_AUDIO_VOLUME:
532 radio->registers[SYSCONFIG2] &= ~SYSCONFIG2_VOLUME;
Hans Verkuil4967d532012-05-04 09:16:57 -0300533 radio->registers[SYSCONFIG2] |= ctrl->val;
534 return si470x_set_register(radio, SYSCONFIG2);
Joonyoung Shimb09cd162009-08-09 14:22:20 -0300535 case V4L2_CID_AUDIO_MUTE:
Hans Verkuil4967d532012-05-04 09:16:57 -0300536 if (ctrl->val)
Joonyoung Shimb09cd162009-08-09 14:22:20 -0300537 radio->registers[POWERCFG] &= ~POWERCFG_DMUTE;
538 else
539 radio->registers[POWERCFG] |= POWERCFG_DMUTE;
Hans Verkuil4967d532012-05-04 09:16:57 -0300540 return si470x_set_register(radio, POWERCFG);
Joonyoung Shimb09cd162009-08-09 14:22:20 -0300541 default:
Hans Verkuil4967d532012-05-04 09:16:57 -0300542 return -EINVAL;
Joonyoung Shimb09cd162009-08-09 14:22:20 -0300543 }
Joonyoung Shimb09cd162009-08-09 14:22:20 -0300544}
545
546
547/*
548 * si470x_vidioc_g_tuner - get tuner attributes
549 */
550static int si470x_vidioc_g_tuner(struct file *file, void *priv,
551 struct v4l2_tuner *tuner)
552{
553 struct si470x_device *radio = video_drvdata(file);
Hans de Goede86ef3f72012-06-14 09:43:11 -0300554 int retval = 0;
Joonyoung Shimb09cd162009-08-09 14:22:20 -0300555
Hans Verkuil340bd4c2012-04-30 19:49:27 -0300556 if (tuner->index != 0)
557 return -EINVAL;
Joonyoung Shimb09cd162009-08-09 14:22:20 -0300558
Hans de Goede86ef3f72012-06-14 09:43:11 -0300559 if (!radio->status_rssi_auto_update) {
560 retval = si470x_get_register(radio, STATUSRSSI);
561 if (retval < 0)
562 return retval;
563 }
Joonyoung Shimb09cd162009-08-09 14:22:20 -0300564
565 /* driver constants */
566 strcpy(tuner->name, "FM");
567 tuner->type = V4L2_TUNER_RADIO;
568 tuner->capability = V4L2_TUNER_CAP_LOW | V4L2_TUNER_CAP_STEREO |
Hans Verkuil54f60192012-05-27 07:25:06 -0300569 V4L2_TUNER_CAP_RDS | V4L2_TUNER_CAP_RDS_BLOCK_IO |
570 V4L2_TUNER_CAP_HWSEEK_BOUNDED |
571 V4L2_TUNER_CAP_HWSEEK_WRAP;
Joonyoung Shimb09cd162009-08-09 14:22:20 -0300572
573 /* range limits */
574 switch ((radio->registers[SYSCONFIG2] & SYSCONFIG2_BAND) >> 6) {
575 /* 0: 87.5 - 108 MHz (USA, Europe, default) */
576 default:
577 tuner->rangelow = 87.5 * FREQ_MUL;
578 tuner->rangehigh = 108 * FREQ_MUL;
579 break;
580 /* 1: 76 - 108 MHz (Japan wide band) */
581 case 1:
582 tuner->rangelow = 76 * FREQ_MUL;
583 tuner->rangehigh = 108 * FREQ_MUL;
584 break;
585 /* 2: 76 - 90 MHz (Japan) */
586 case 2:
587 tuner->rangelow = 76 * FREQ_MUL;
588 tuner->rangehigh = 90 * FREQ_MUL;
589 break;
590 };
591
592 /* stereo indicator == stereo (instead of mono) */
593 if ((radio->registers[STATUSRSSI] & STATUSRSSI_ST) == 0)
594 tuner->rxsubchans = V4L2_TUNER_SUB_MONO;
595 else
Hans Verkuil4967d532012-05-04 09:16:57 -0300596 tuner->rxsubchans = V4L2_TUNER_SUB_STEREO;
Joonyoung Shimb09cd162009-08-09 14:22:20 -0300597 /* If there is a reliable method of detecting an RDS channel,
598 then this code should check for that before setting this
599 RDS subchannel. */
600 tuner->rxsubchans |= V4L2_TUNER_SUB_RDS;
601
602 /* mono/stereo selector */
603 if ((radio->registers[POWERCFG] & POWERCFG_MONO) == 0)
604 tuner->audmode = V4L2_TUNER_MODE_STEREO;
605 else
606 tuner->audmode = V4L2_TUNER_MODE_MONO;
607
608 /* min is worst, max is best; signal:0..0xffff; rssi: 0..0xff */
Tobias Lorenz144dcdc2010-02-18 16:33:46 -0300609 /* measured in units of dbµV in 1 db increments (max at ~75 dbµV) */
Joonyoung Shimb09cd162009-08-09 14:22:20 -0300610 tuner->signal = (radio->registers[STATUSRSSI] & STATUSRSSI_RSSI);
611 /* the ideal factor is 0xffff/75 = 873,8 */
612 tuner->signal = (tuner->signal * 873) + (8 * tuner->signal / 10);
Hans Verkuileae63ae2012-05-04 09:20:53 -0300613 if (tuner->signal > 0xffff)
614 tuner->signal = 0xffff;
Joonyoung Shimb09cd162009-08-09 14:22:20 -0300615
616 /* automatic frequency control: -1: freq to low, 1 freq to high */
617 /* AFCRL does only indicate that freq. differs, not if too low/high */
618 tuner->afc = (radio->registers[STATUSRSSI] & STATUSRSSI_AFCRL) ? 1 : 0;
619
Joonyoung Shimb09cd162009-08-09 14:22:20 -0300620 return retval;
621}
622
623
624/*
625 * si470x_vidioc_s_tuner - set tuner attributes
626 */
627static int si470x_vidioc_s_tuner(struct file *file, void *priv,
628 struct v4l2_tuner *tuner)
629{
630 struct si470x_device *radio = video_drvdata(file);
Joonyoung Shimb09cd162009-08-09 14:22:20 -0300631
Joonyoung Shimb09cd162009-08-09 14:22:20 -0300632 if (tuner->index != 0)
Hans Verkuileae63ae2012-05-04 09:20:53 -0300633 return -EINVAL;
Joonyoung Shimb09cd162009-08-09 14:22:20 -0300634
635 /* mono/stereo selector */
636 switch (tuner->audmode) {
637 case V4L2_TUNER_MODE_MONO:
638 radio->registers[POWERCFG] |= POWERCFG_MONO; /* force mono */
639 break;
640 case V4L2_TUNER_MODE_STEREO:
Hans Verkuileae63ae2012-05-04 09:20:53 -0300641 default:
Joonyoung Shimb09cd162009-08-09 14:22:20 -0300642 radio->registers[POWERCFG] &= ~POWERCFG_MONO; /* try stereo */
643 break;
Joonyoung Shimb09cd162009-08-09 14:22:20 -0300644 }
645
Hans Verkuil340bd4c2012-04-30 19:49:27 -0300646 return si470x_set_register(radio, POWERCFG);
Joonyoung Shimb09cd162009-08-09 14:22:20 -0300647}
648
649
650/*
651 * si470x_vidioc_g_frequency - get tuner or modulator radio frequency
652 */
653static int si470x_vidioc_g_frequency(struct file *file, void *priv,
654 struct v4l2_frequency *freq)
655{
656 struct si470x_device *radio = video_drvdata(file);
Joonyoung Shimb09cd162009-08-09 14:22:20 -0300657
Hans Verkuil340bd4c2012-04-30 19:49:27 -0300658 if (freq->tuner != 0)
659 return -EINVAL;
Joonyoung Shimb09cd162009-08-09 14:22:20 -0300660
661 freq->type = V4L2_TUNER_RADIO;
Hans Verkuil340bd4c2012-04-30 19:49:27 -0300662 return si470x_get_freq(radio, &freq->frequency);
Joonyoung Shimb09cd162009-08-09 14:22:20 -0300663}
664
665
666/*
667 * si470x_vidioc_s_frequency - set tuner or modulator radio frequency
668 */
669static int si470x_vidioc_s_frequency(struct file *file, void *priv,
670 struct v4l2_frequency *freq)
671{
672 struct si470x_device *radio = video_drvdata(file);
Joonyoung Shimb09cd162009-08-09 14:22:20 -0300673
Hans Verkuil340bd4c2012-04-30 19:49:27 -0300674 if (freq->tuner != 0)
675 return -EINVAL;
Joonyoung Shimb09cd162009-08-09 14:22:20 -0300676
Hans Verkuil340bd4c2012-04-30 19:49:27 -0300677 return si470x_set_freq(radio, freq->frequency);
Joonyoung Shimb09cd162009-08-09 14:22:20 -0300678}
679
680
681/*
682 * si470x_vidioc_s_hw_freq_seek - set hardware frequency seek
683 */
684static int si470x_vidioc_s_hw_freq_seek(struct file *file, void *priv,
685 struct v4l2_hw_freq_seek *seek)
686{
687 struct si470x_device *radio = video_drvdata(file);
Joonyoung Shimb09cd162009-08-09 14:22:20 -0300688
Hans Verkuil340bd4c2012-04-30 19:49:27 -0300689 if (seek->tuner != 0)
690 return -EINVAL;
Joonyoung Shimb09cd162009-08-09 14:22:20 -0300691
Hans Verkuil340bd4c2012-04-30 19:49:27 -0300692 return si470x_set_seek(radio, seek->wrap_around, seek->seek_upward);
Joonyoung Shimb09cd162009-08-09 14:22:20 -0300693}
694
Hans Verkuil4967d532012-05-04 09:16:57 -0300695const struct v4l2_ctrl_ops si470x_ctrl_ops = {
696 .s_ctrl = si470x_s_ctrl,
697};
Joonyoung Shimb09cd162009-08-09 14:22:20 -0300698
699/*
700 * si470x_ioctl_ops - video device ioctl operations
701 */
702static const struct v4l2_ioctl_ops si470x_ioctl_ops = {
703 .vidioc_querycap = si470x_vidioc_querycap,
Joonyoung Shimb09cd162009-08-09 14:22:20 -0300704 .vidioc_g_tuner = si470x_vidioc_g_tuner,
705 .vidioc_s_tuner = si470x_vidioc_s_tuner,
706 .vidioc_g_frequency = si470x_vidioc_g_frequency,
707 .vidioc_s_frequency = si470x_vidioc_s_frequency,
708 .vidioc_s_hw_freq_seek = si470x_vidioc_s_hw_freq_seek,
Hans Verkuileae63ae2012-05-04 09:20:53 -0300709 .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
710 .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
Joonyoung Shimb09cd162009-08-09 14:22:20 -0300711};
712
713
714/*
715 * si470x_viddev_template - video device interface
716 */
717struct video_device si470x_viddev_template = {
718 .fops = &si470x_fops,
719 .name = DRIVER_NAME,
Hans Verkuil4967d532012-05-04 09:16:57 -0300720 .release = video_device_release_empty,
Joonyoung Shimb09cd162009-08-09 14:22:20 -0300721 .ioctl_ops = &si470x_ioctl_ops,
722};