blob: 0e750aef656a6f8a053aafbd965b4e9e1fe64f23 [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>
Hans de Goedef1406122012-07-12 16:55:48 -03007 * Copyright (c) 2012 Hans de Goede <hdegoede@redhat.com>
Joonyoung Shimb09cd162009-08-09 14:22:20 -03008 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 */
23
24
25/*
26 * History:
27 * 2008-01-12 Tobias Lorenz <tobias.lorenz@gmx.net>
28 * Version 1.0.0
29 * - First working version
30 * 2008-01-13 Tobias Lorenz <tobias.lorenz@gmx.net>
31 * Version 1.0.1
32 * - Improved error handling, every function now returns errno
33 * - Improved multi user access (start/mute/stop)
34 * - Channel doesn't get lost anymore after start/mute/stop
35 * - RDS support added (polling mode via interrupt EP 1)
36 * - marked default module parameters with *value*
37 * - switched from bit structs to bit masks
38 * - header file cleaned and integrated
39 * 2008-01-14 Tobias Lorenz <tobias.lorenz@gmx.net>
40 * Version 1.0.2
41 * - hex values are now lower case
42 * - commented USB ID for ADS/Tech moved on todo list
43 * - blacklisted si470x in hid-quirks.c
44 * - rds buffer handling functions integrated into *_work, *_read
45 * - rds_command in si470x_poll exchanged against simple retval
46 * - check for firmware version 15
47 * - code order and prototypes still remain the same
48 * - spacing and bottom of band codes remain the same
49 * 2008-01-16 Tobias Lorenz <tobias.lorenz@gmx.net>
50 * Version 1.0.3
51 * - code reordered to avoid function prototypes
52 * - switch/case defaults are now more user-friendly
53 * - unified comment style
54 * - applied all checkpatch.pl v1.12 suggestions
55 * except the warning about the too long lines with bit comments
56 * - renamed FMRADIO to RADIO to cut line length (checkpatch.pl)
57 * 2008-01-22 Tobias Lorenz <tobias.lorenz@gmx.net>
58 * Version 1.0.4
59 * - avoid poss. locking when doing copy_to_user which may sleep
60 * - RDS is automatically activated on read now
61 * - code cleaned of unnecessary rds_commands
62 * - USB Vendor/Product ID for ADS/Tech FM Radio Receiver verified
63 * (thanks to Guillaume RAMOUSSE)
64 * 2008-01-27 Tobias Lorenz <tobias.lorenz@gmx.net>
65 * Version 1.0.5
66 * - number of seek_retries changed to tune_timeout
67 * - fixed problem with incomplete tune operations by own buffers
68 * - optimization of variables and printf types
69 * - improved error logging
70 * 2008-01-31 Tobias Lorenz <tobias.lorenz@gmx.net>
71 * Oliver Neukum <oliver@neukum.org>
72 * Version 1.0.6
73 * - fixed coverity checker warnings in *_usb_driver_disconnect
74 * - probe()/open() race by correct ordering in probe()
75 * - DMA coherency rules by separate allocation of all buffers
76 * - use of endianness macros
77 * - abuse of spinlock, replaced by mutex
78 * - racy handling of timer in disconnect,
79 * replaced by delayed_work
80 * - racy interruptible_sleep_on(),
81 * replaced with wait_event_interruptible()
82 * - handle signals in read()
83 * 2008-02-08 Tobias Lorenz <tobias.lorenz@gmx.net>
84 * Oliver Neukum <oliver@neukum.org>
85 * Version 1.0.7
86 * - usb autosuspend support
87 * - unplugging fixed
88 * 2008-05-07 Tobias Lorenz <tobias.lorenz@gmx.net>
89 * Version 1.0.8
90 * - hardware frequency seek support
91 * - afc indication
92 * - more safety checks, let si470x_get_freq return errno
93 * - vidioc behavior corrected according to v4l2 spec
94 * 2008-10-20 Alexey Klimov <klimov.linux@gmail.com>
95 * - add support for KWorld USB FM Radio FM700
96 * - blacklisted KWorld radio in hid-core.c and hid-ids.h
97 * 2008-12-03 Mark Lord <mlord@pobox.com>
98 * - add support for DealExtreme USB Radio
99 * 2009-01-31 Bob Ross <pigiron@gmx.com>
100 * - correction of stereo detection/setting
101 * - correction of signal strength indicator scaling
102 * 2009-01-31 Rick Bronson <rick@efn.org>
103 * Tobias Lorenz <tobias.lorenz@gmx.net>
104 * - add LED status output
105 * - get HW/SW version from scratchpad
106 * 2009-06-16 Edouard Lafargue <edouard@lafargue.name>
107 * Version 1.0.10
108 * - add support for interrupt mode for RDS endpoint,
109 * instead of polling.
110 * Improves RDS reception significantly
111 */
112
113
114/* kernel includes */
115#include "radio-si470x.h"
116
117
118
119/**************************************************************************
120 * Module Parameters
121 **************************************************************************/
122
123/* Spacing (kHz) */
124/* 0: 200 kHz (USA, Australia) */
125/* 1: 100 kHz (Europe, Japan) */
126/* 2: 50 kHz */
127static unsigned short space = 2;
128module_param(space, ushort, 0444);
129MODULE_PARM_DESC(space, "Spacing: 0=200kHz 1=100kHz *2=50kHz*");
130
Joonyoung Shimb09cd162009-08-09 14:22:20 -0300131/* De-emphasis */
132/* 0: 75 us (USA) */
133/* 1: 50 us (Europe, Australia, Japan) */
134static unsigned short de = 1;
135module_param(de, ushort, 0444);
136MODULE_PARM_DESC(de, "De-emphasis: 0=75us *1=50us*");
137
138/* Tune timeout */
139static unsigned int tune_timeout = 3000;
140module_param(tune_timeout, uint, 0644);
141MODULE_PARM_DESC(tune_timeout, "Tune timeout: *3000*");
142
143/* Seek timeout */
144static unsigned int seek_timeout = 5000;
145module_param(seek_timeout, uint, 0644);
146MODULE_PARM_DESC(seek_timeout, "Seek timeout: *5000*");
147
Hans de Goedef1406122012-07-12 16:55:48 -0300148static const struct v4l2_frequency_band bands[] = {
149 {
150 .type = V4L2_TUNER_RADIO,
151 .index = 0,
152 .capability = V4L2_TUNER_CAP_LOW | V4L2_TUNER_CAP_STEREO |
153 V4L2_TUNER_CAP_RDS | V4L2_TUNER_CAP_RDS_BLOCK_IO |
Hans Verkuil32737812012-08-03 11:16:59 -0300154 V4L2_TUNER_CAP_FREQ_BANDS |
Hans de Goedef1406122012-07-12 16:55:48 -0300155 V4L2_TUNER_CAP_HWSEEK_BOUNDED |
156 V4L2_TUNER_CAP_HWSEEK_WRAP,
157 .rangelow = 87500 * 16,
158 .rangehigh = 108000 * 16,
159 .modulation = V4L2_BAND_MODULATION_FM,
160 },
161 {
162 .type = V4L2_TUNER_RADIO,
163 .index = 1,
164 .capability = V4L2_TUNER_CAP_LOW | V4L2_TUNER_CAP_STEREO |
165 V4L2_TUNER_CAP_RDS | V4L2_TUNER_CAP_RDS_BLOCK_IO |
Hans Verkuil32737812012-08-03 11:16:59 -0300166 V4L2_TUNER_CAP_FREQ_BANDS |
Hans de Goedef1406122012-07-12 16:55:48 -0300167 V4L2_TUNER_CAP_HWSEEK_BOUNDED |
168 V4L2_TUNER_CAP_HWSEEK_WRAP,
169 .rangelow = 76000 * 16,
170 .rangehigh = 108000 * 16,
171 .modulation = V4L2_BAND_MODULATION_FM,
172 },
173 {
174 .type = V4L2_TUNER_RADIO,
175 .index = 2,
176 .capability = V4L2_TUNER_CAP_LOW | V4L2_TUNER_CAP_STEREO |
177 V4L2_TUNER_CAP_RDS | V4L2_TUNER_CAP_RDS_BLOCK_IO |
Hans Verkuil32737812012-08-03 11:16:59 -0300178 V4L2_TUNER_CAP_FREQ_BANDS |
Hans de Goedef1406122012-07-12 16:55:48 -0300179 V4L2_TUNER_CAP_HWSEEK_BOUNDED |
180 V4L2_TUNER_CAP_HWSEEK_WRAP,
181 .rangelow = 76000 * 16,
182 .rangehigh = 90000 * 16,
183 .modulation = V4L2_BAND_MODULATION_FM,
184 },
185};
Joonyoung Shimb09cd162009-08-09 14:22:20 -0300186
187/**************************************************************************
188 * Generic Functions
189 **************************************************************************/
190
191/*
Hans de Goedef1406122012-07-12 16:55:48 -0300192 * si470x_set_band - set the band
193 */
194static int si470x_set_band(struct si470x_device *radio, int band)
195{
196 if (radio->band == band)
197 return 0;
198
199 radio->band = band;
200 radio->registers[SYSCONFIG2] &= ~SYSCONFIG2_BAND;
201 radio->registers[SYSCONFIG2] |= radio->band << 6;
202 return si470x_set_register(radio, SYSCONFIG2);
203}
204
205/*
Joonyoung Shimb09cd162009-08-09 14:22:20 -0300206 * si470x_set_chan - set the channel
207 */
208static int si470x_set_chan(struct si470x_device *radio, unsigned short chan)
209{
210 int retval;
Joonyoung Shimb09cd162009-08-09 14:22:20 -0300211 bool timed_out = 0;
212
213 /* start tuning */
214 radio->registers[CHANNEL] &= ~CHANNEL_CHAN;
215 radio->registers[CHANNEL] |= CHANNEL_TUNE | chan;
216 retval = si470x_set_register(radio, CHANNEL);
217 if (retval < 0)
218 goto done;
219
Hans de Goede77947112012-06-14 09:43:12 -0300220 /* wait till tune operation has completed */
Wolfram Sang16735d02013-11-14 14:32:02 -0800221 reinit_completion(&radio->completion);
Hans de Goede77947112012-06-14 09:43:12 -0300222 retval = wait_for_completion_timeout(&radio->completion,
223 msecs_to_jiffies(tune_timeout));
224 if (!retval)
225 timed_out = true;
Joonyoung Shim0830be32011-03-11 03:54:46 -0300226
Joonyoung Shimb09cd162009-08-09 14:22:20 -0300227 if ((radio->registers[STATUSRSSI] & STATUSRSSI_STC) == 0)
Hans Verkuil4967d532012-05-04 09:16:57 -0300228 dev_warn(&radio->videodev.dev, "tune does not complete\n");
Joonyoung Shimb09cd162009-08-09 14:22:20 -0300229 if (timed_out)
Hans Verkuil4967d532012-05-04 09:16:57 -0300230 dev_warn(&radio->videodev.dev,
Joonyoung Shima9d6fd52009-08-09 14:22:41 -0300231 "tune timed out after %u ms\n", tune_timeout);
Joonyoung Shimb09cd162009-08-09 14:22:20 -0300232
Joonyoung Shimb09cd162009-08-09 14:22:20 -0300233 /* stop tuning */
234 radio->registers[CHANNEL] &= ~CHANNEL_TUNE;
235 retval = si470x_set_register(radio, CHANNEL);
236
237done:
238 return retval;
239}
240
Hans de Goedef1406122012-07-12 16:55:48 -0300241/*
242 * si470x_get_step - get channel spacing
243 */
244static unsigned int si470x_get_step(struct si470x_device *radio)
245{
246 /* Spacing (kHz) */
247 switch ((radio->registers[SYSCONFIG2] & SYSCONFIG2_SPACE) >> 4) {
248 /* 0: 200 kHz (USA, Australia) */
249 case 0:
250 return 200 * 16;
251 /* 1: 100 kHz (Europe, Japan) */
252 case 1:
253 return 100 * 16;
254 /* 2: 50 kHz */
255 default:
256 return 50 * 16;
Joe Perches2028c712013-10-08 20:29:08 -0300257 }
Hans de Goedef1406122012-07-12 16:55:48 -0300258}
259
Joonyoung Shimb09cd162009-08-09 14:22:20 -0300260
261/*
262 * si470x_get_freq - get the frequency
263 */
264static int si470x_get_freq(struct si470x_device *radio, unsigned int *freq)
265{
Hans de Goedef1406122012-07-12 16:55:48 -0300266 int chan, retval;
Joonyoung Shimb09cd162009-08-09 14:22:20 -0300267
268 /* read channel */
269 retval = si470x_get_register(radio, READCHAN);
270 chan = radio->registers[READCHAN] & READCHAN_READCHAN;
271
272 /* Frequency (MHz) = Spacing (kHz) x Channel + Bottom of Band (MHz) */
Hans de Goedef1406122012-07-12 16:55:48 -0300273 *freq = chan * si470x_get_step(radio) + bands[radio->band].rangelow;
Joonyoung Shimb09cd162009-08-09 14:22:20 -0300274
275 return retval;
276}
277
278
279/*
280 * si470x_set_freq - set the frequency
281 */
282int si470x_set_freq(struct si470x_device *radio, unsigned int freq)
283{
Joonyoung Shimb09cd162009-08-09 14:22:20 -0300284 unsigned short chan;
285
Hans de Goedef1406122012-07-12 16:55:48 -0300286 freq = clamp(freq, bands[radio->band].rangelow,
287 bands[radio->band].rangehigh);
Joonyoung Shimb09cd162009-08-09 14:22:20 -0300288 /* Chan = [ Freq (Mhz) - Bottom of Band (MHz) ] / Spacing (kHz) */
Hans de Goedef1406122012-07-12 16:55:48 -0300289 chan = (freq - bands[radio->band].rangelow) / si470x_get_step(radio);
Joonyoung Shimb09cd162009-08-09 14:22:20 -0300290
291 return si470x_set_chan(radio, chan);
292}
293
294
295/*
296 * si470x_set_seek - set seek
297 */
298static int si470x_set_seek(struct si470x_device *radio,
Hans Verkuilec6f4322012-09-14 07:41:18 -0300299 const struct v4l2_hw_freq_seek *seek)
Joonyoung Shimb09cd162009-08-09 14:22:20 -0300300{
Hans de Goedef1406122012-07-12 16:55:48 -0300301 int band, retval;
302 unsigned int freq;
Joonyoung Shimb09cd162009-08-09 14:22:20 -0300303 bool timed_out = 0;
304
Hans de Goedef1406122012-07-12 16:55:48 -0300305 /* set band */
306 if (seek->rangelow || seek->rangehigh) {
307 for (band = 0; band < ARRAY_SIZE(bands); band++) {
308 if (bands[band].rangelow == seek->rangelow &&
309 bands[band].rangehigh == seek->rangehigh)
310 break;
311 }
312 if (band == ARRAY_SIZE(bands))
313 return -EINVAL; /* No matching band found */
314 } else
315 band = 1; /* If nothing is specified seek 76 - 108 Mhz */
316
317 if (radio->band != band) {
318 retval = si470x_get_freq(radio, &freq);
319 if (retval)
320 return retval;
321 retval = si470x_set_band(radio, band);
322 if (retval)
323 return retval;
324 retval = si470x_set_freq(radio, freq);
325 if (retval)
326 return retval;
327 }
328
Joonyoung Shimb09cd162009-08-09 14:22:20 -0300329 /* start seeking */
330 radio->registers[POWERCFG] |= POWERCFG_SEEK;
Hans de Goedef1406122012-07-12 16:55:48 -0300331 if (seek->wrap_around)
Joonyoung Shimb09cd162009-08-09 14:22:20 -0300332 radio->registers[POWERCFG] &= ~POWERCFG_SKMODE;
333 else
334 radio->registers[POWERCFG] |= POWERCFG_SKMODE;
Hans de Goedef1406122012-07-12 16:55:48 -0300335 if (seek->seek_upward)
Joonyoung Shimb09cd162009-08-09 14:22:20 -0300336 radio->registers[POWERCFG] |= POWERCFG_SEEKUP;
337 else
338 radio->registers[POWERCFG] &= ~POWERCFG_SEEKUP;
339 retval = si470x_set_register(radio, POWERCFG);
340 if (retval < 0)
Hans Verkuil340bd4c2012-04-30 19:49:27 -0300341 return retval;
Joonyoung Shimb09cd162009-08-09 14:22:20 -0300342
Hans de Goede77947112012-06-14 09:43:12 -0300343 /* wait till tune operation has completed */
Wolfram Sang16735d02013-11-14 14:32:02 -0800344 reinit_completion(&radio->completion);
Hans de Goede77947112012-06-14 09:43:12 -0300345 retval = wait_for_completion_timeout(&radio->completion,
346 msecs_to_jiffies(seek_timeout));
347 if (!retval)
348 timed_out = true;
Joonyoung Shim0830be32011-03-11 03:54:46 -0300349
Joonyoung Shimb09cd162009-08-09 14:22:20 -0300350 if ((radio->registers[STATUSRSSI] & STATUSRSSI_STC) == 0)
Hans Verkuil4967d532012-05-04 09:16:57 -0300351 dev_warn(&radio->videodev.dev, "seek does not complete\n");
Joonyoung Shimb09cd162009-08-09 14:22:20 -0300352 if (radio->registers[STATUSRSSI] & STATUSRSSI_SF)
Hans Verkuil4967d532012-05-04 09:16:57 -0300353 dev_warn(&radio->videodev.dev,
Joonyoung Shima9d6fd52009-08-09 14:22:41 -0300354 "seek failed / band limit reached\n");
Joonyoung Shimb09cd162009-08-09 14:22:20 -0300355
Joonyoung Shimb09cd162009-08-09 14:22:20 -0300356 /* stop seeking */
357 radio->registers[POWERCFG] &= ~POWERCFG_SEEK;
358 retval = si470x_set_register(radio, POWERCFG);
359
Joonyoung Shimb09cd162009-08-09 14:22:20 -0300360 /* try again, if timed out */
Hans Verkuil340bd4c2012-04-30 19:49:27 -0300361 if (retval == 0 && timed_out)
Hans Verkuil54f60192012-05-27 07:25:06 -0300362 return -ENODATA;
Joonyoung Shimb09cd162009-08-09 14:22:20 -0300363 return retval;
364}
365
366
367/*
368 * si470x_start - switch on radio
369 */
370int si470x_start(struct si470x_device *radio)
371{
372 int retval;
373
374 /* powercfg */
375 radio->registers[POWERCFG] =
376 POWERCFG_DMUTE | POWERCFG_ENABLE | POWERCFG_RDSM;
377 retval = si470x_set_register(radio, POWERCFG);
378 if (retval < 0)
379 goto done;
380
381 /* sysconfig 1 */
Tobias Lorenz131ddd12011-01-08 14:12:30 -0300382 radio->registers[SYSCONFIG1] =
383 (de << 11) & SYSCONFIG1_DE; /* DE*/
Joonyoung Shimb09cd162009-08-09 14:22:20 -0300384 retval = si470x_set_register(radio, SYSCONFIG1);
385 if (retval < 0)
386 goto done;
387
388 /* sysconfig 2 */
389 radio->registers[SYSCONFIG2] =
Hans de Goedeb9664252012-06-14 09:43:13 -0300390 (0x1f << 8) | /* SEEKTH */
Hans de Goedef1406122012-07-12 16:55:48 -0300391 ((radio->band << 6) & SYSCONFIG2_BAND) |/* BAND */
Joonyoung Shimb09cd162009-08-09 14:22:20 -0300392 ((space << 4) & SYSCONFIG2_SPACE) | /* SPACE */
393 15; /* VOLUME (max) */
394 retval = si470x_set_register(radio, SYSCONFIG2);
395 if (retval < 0)
396 goto done;
397
398 /* reset last channel */
399 retval = si470x_set_chan(radio,
400 radio->registers[CHANNEL] & CHANNEL_CHAN);
401
402done:
403 return retval;
404}
405
406
407/*
408 * si470x_stop - switch off radio
409 */
410int si470x_stop(struct si470x_device *radio)
411{
412 int retval;
413
414 /* sysconfig 1 */
415 radio->registers[SYSCONFIG1] &= ~SYSCONFIG1_RDS;
416 retval = si470x_set_register(radio, SYSCONFIG1);
417 if (retval < 0)
418 goto done;
419
420 /* powercfg */
421 radio->registers[POWERCFG] &= ~POWERCFG_DMUTE;
422 /* POWERCFG_ENABLE has to automatically go low */
423 radio->registers[POWERCFG] |= POWERCFG_ENABLE | POWERCFG_DISABLE;
424 retval = si470x_set_register(radio, POWERCFG);
425
426done:
427 return retval;
428}
429
430
431/*
432 * si470x_rds_on - switch on rds reception
433 */
Mauro Carvalho Chehabf2f8e852010-09-15 08:35:45 -0300434static int si470x_rds_on(struct si470x_device *radio)
Joonyoung Shimb09cd162009-08-09 14:22:20 -0300435{
436 int retval;
437
438 /* sysconfig 1 */
Joonyoung Shimb09cd162009-08-09 14:22:20 -0300439 radio->registers[SYSCONFIG1] |= SYSCONFIG1_RDS;
440 retval = si470x_set_register(radio, SYSCONFIG1);
441 if (retval < 0)
442 radio->registers[SYSCONFIG1] &= ~SYSCONFIG1_RDS;
Joonyoung Shimb09cd162009-08-09 14:22:20 -0300443
444 return retval;
445}
446
447
448
449/**************************************************************************
Joonyoung Shim1aa925c2009-12-10 16:49:34 -0300450 * File Operations Interface
451 **************************************************************************/
452
453/*
454 * si470x_fops_read - read RDS data
455 */
456static ssize_t si470x_fops_read(struct file *file, char __user *buf,
457 size_t count, loff_t *ppos)
458{
459 struct si470x_device *radio = video_drvdata(file);
460 int retval = 0;
461 unsigned int block_count = 0;
462
463 /* switch on rds reception */
464 if ((radio->registers[SYSCONFIG1] & SYSCONFIG1_RDS) == 0)
465 si470x_rds_on(radio);
466
467 /* block if no new data available */
468 while (radio->wr_index == radio->rd_index) {
469 if (file->f_flags & O_NONBLOCK) {
470 retval = -EWOULDBLOCK;
471 goto done;
472 }
473 if (wait_event_interruptible(radio->read_queue,
474 radio->wr_index != radio->rd_index) < 0) {
475 retval = -EINTR;
476 goto done;
477 }
478 }
479
480 /* calculate block count from byte count */
481 count /= 3;
482
483 /* copy RDS block out of internal buffer and to user buffer */
Joonyoung Shim1aa925c2009-12-10 16:49:34 -0300484 while (block_count < count) {
485 if (radio->rd_index == radio->wr_index)
486 break;
487
488 /* always transfer rds complete blocks */
489 if (copy_to_user(buf, &radio->buffer[radio->rd_index], 3))
490 /* retval = -EFAULT; */
491 break;
492
493 /* increment and wrap read pointer */
494 radio->rd_index += 3;
495 if (radio->rd_index >= radio->buf_size)
496 radio->rd_index = 0;
497
498 /* increment counters */
499 block_count++;
500 buf += 3;
501 retval += 3;
502 }
Joonyoung Shim1aa925c2009-12-10 16:49:34 -0300503
504done:
505 return retval;
506}
507
508
509/*
510 * si470x_fops_poll - poll RDS data
511 */
512static unsigned int si470x_fops_poll(struct file *file,
513 struct poll_table_struct *pts)
514{
515 struct si470x_device *radio = video_drvdata(file);
Hans Verkuileae63ae2012-05-04 09:20:53 -0300516 unsigned long req_events = poll_requested_events(pts);
517 int retval = v4l2_ctrl_poll(file, pts);
Joonyoung Shim1aa925c2009-12-10 16:49:34 -0300518
Hans Verkuileae63ae2012-05-04 09:20:53 -0300519 if (req_events & (POLLIN | POLLRDNORM)) {
520 /* switch on rds reception */
521 if ((radio->registers[SYSCONFIG1] & SYSCONFIG1_RDS) == 0)
522 si470x_rds_on(radio);
Mauro Carvalho Chehabf2f8e852010-09-15 08:35:45 -0300523
Hans Verkuileae63ae2012-05-04 09:20:53 -0300524 poll_wait(file, &radio->read_queue, pts);
Joonyoung Shim1aa925c2009-12-10 16:49:34 -0300525
Hans Verkuileae63ae2012-05-04 09:20:53 -0300526 if (radio->rd_index != radio->wr_index)
527 retval |= POLLIN | POLLRDNORM;
528 }
Joonyoung Shim1aa925c2009-12-10 16:49:34 -0300529
530 return retval;
531}
532
533
534/*
535 * si470x_fops - file operations interface
536 */
537static const struct v4l2_file_operations si470x_fops = {
538 .owner = THIS_MODULE,
539 .read = si470x_fops_read,
540 .poll = si470x_fops_poll,
Mauro Carvalho Chehabf2f8e852010-09-15 08:35:45 -0300541 .unlocked_ioctl = video_ioctl2,
Joonyoung Shim1aa925c2009-12-10 16:49:34 -0300542 .open = si470x_fops_open,
543 .release = si470x_fops_release,
544};
545
546
547
548/**************************************************************************
Joonyoung Shimb09cd162009-08-09 14:22:20 -0300549 * Video4Linux Interface
550 **************************************************************************/
551
Hans Verkuil4967d532012-05-04 09:16:57 -0300552
553static int si470x_s_ctrl(struct v4l2_ctrl *ctrl)
Joonyoung Shimb09cd162009-08-09 14:22:20 -0300554{
Hans Verkuil4967d532012-05-04 09:16:57 -0300555 struct si470x_device *radio =
556 container_of(ctrl->handler, struct si470x_device, hdl);
Joonyoung Shimb09cd162009-08-09 14:22:20 -0300557
558 switch (ctrl->id) {
559 case V4L2_CID_AUDIO_VOLUME:
560 radio->registers[SYSCONFIG2] &= ~SYSCONFIG2_VOLUME;
Hans Verkuil4967d532012-05-04 09:16:57 -0300561 radio->registers[SYSCONFIG2] |= ctrl->val;
562 return si470x_set_register(radio, SYSCONFIG2);
Joonyoung Shimb09cd162009-08-09 14:22:20 -0300563 case V4L2_CID_AUDIO_MUTE:
Hans Verkuil4967d532012-05-04 09:16:57 -0300564 if (ctrl->val)
Joonyoung Shimb09cd162009-08-09 14:22:20 -0300565 radio->registers[POWERCFG] &= ~POWERCFG_DMUTE;
566 else
567 radio->registers[POWERCFG] |= POWERCFG_DMUTE;
Hans Verkuil4967d532012-05-04 09:16:57 -0300568 return si470x_set_register(radio, POWERCFG);
Joonyoung Shimb09cd162009-08-09 14:22:20 -0300569 default:
Hans Verkuil4967d532012-05-04 09:16:57 -0300570 return -EINVAL;
Joonyoung Shimb09cd162009-08-09 14:22:20 -0300571 }
Joonyoung Shimb09cd162009-08-09 14:22:20 -0300572}
573
574
575/*
576 * si470x_vidioc_g_tuner - get tuner attributes
577 */
578static int si470x_vidioc_g_tuner(struct file *file, void *priv,
579 struct v4l2_tuner *tuner)
580{
581 struct si470x_device *radio = video_drvdata(file);
Hans de Goede86ef3f72012-06-14 09:43:11 -0300582 int retval = 0;
Joonyoung Shimb09cd162009-08-09 14:22:20 -0300583
Hans Verkuil340bd4c2012-04-30 19:49:27 -0300584 if (tuner->index != 0)
585 return -EINVAL;
Joonyoung Shimb09cd162009-08-09 14:22:20 -0300586
Hans de Goede86ef3f72012-06-14 09:43:11 -0300587 if (!radio->status_rssi_auto_update) {
588 retval = si470x_get_register(radio, STATUSRSSI);
589 if (retval < 0)
590 return retval;
591 }
Joonyoung Shimb09cd162009-08-09 14:22:20 -0300592
593 /* driver constants */
594 strcpy(tuner->name, "FM");
595 tuner->type = V4L2_TUNER_RADIO;
596 tuner->capability = V4L2_TUNER_CAP_LOW | V4L2_TUNER_CAP_STEREO |
Hans Verkuil54f60192012-05-27 07:25:06 -0300597 V4L2_TUNER_CAP_RDS | V4L2_TUNER_CAP_RDS_BLOCK_IO |
598 V4L2_TUNER_CAP_HWSEEK_BOUNDED |
599 V4L2_TUNER_CAP_HWSEEK_WRAP;
Hans de Goedef1406122012-07-12 16:55:48 -0300600 tuner->rangelow = 76 * FREQ_MUL;
601 tuner->rangehigh = 108 * FREQ_MUL;
Joonyoung Shimb09cd162009-08-09 14:22:20 -0300602
603 /* stereo indicator == stereo (instead of mono) */
604 if ((radio->registers[STATUSRSSI] & STATUSRSSI_ST) == 0)
605 tuner->rxsubchans = V4L2_TUNER_SUB_MONO;
606 else
Hans Verkuil4967d532012-05-04 09:16:57 -0300607 tuner->rxsubchans = V4L2_TUNER_SUB_STEREO;
Joonyoung Shimb09cd162009-08-09 14:22:20 -0300608 /* If there is a reliable method of detecting an RDS channel,
609 then this code should check for that before setting this
610 RDS subchannel. */
611 tuner->rxsubchans |= V4L2_TUNER_SUB_RDS;
612
613 /* mono/stereo selector */
614 if ((radio->registers[POWERCFG] & POWERCFG_MONO) == 0)
615 tuner->audmode = V4L2_TUNER_MODE_STEREO;
616 else
617 tuner->audmode = V4L2_TUNER_MODE_MONO;
618
619 /* min is worst, max is best; signal:0..0xffff; rssi: 0..0xff */
Tobias Lorenz144dcdc2010-02-18 16:33:46 -0300620 /* measured in units of dbµV in 1 db increments (max at ~75 dbµV) */
Joonyoung Shimb09cd162009-08-09 14:22:20 -0300621 tuner->signal = (radio->registers[STATUSRSSI] & STATUSRSSI_RSSI);
622 /* the ideal factor is 0xffff/75 = 873,8 */
623 tuner->signal = (tuner->signal * 873) + (8 * tuner->signal / 10);
Hans Verkuileae63ae2012-05-04 09:20:53 -0300624 if (tuner->signal > 0xffff)
625 tuner->signal = 0xffff;
Joonyoung Shimb09cd162009-08-09 14:22:20 -0300626
627 /* automatic frequency control: -1: freq to low, 1 freq to high */
628 /* AFCRL does only indicate that freq. differs, not if too low/high */
629 tuner->afc = (radio->registers[STATUSRSSI] & STATUSRSSI_AFCRL) ? 1 : 0;
630
Joonyoung Shimb09cd162009-08-09 14:22:20 -0300631 return retval;
632}
633
634
635/*
636 * si470x_vidioc_s_tuner - set tuner attributes
637 */
638static int si470x_vidioc_s_tuner(struct file *file, void *priv,
Hans Verkuil2f73c7c2013-03-15 06:10:06 -0300639 const struct v4l2_tuner *tuner)
Joonyoung Shimb09cd162009-08-09 14:22:20 -0300640{
641 struct si470x_device *radio = video_drvdata(file);
Joonyoung Shimb09cd162009-08-09 14:22:20 -0300642
Joonyoung Shimb09cd162009-08-09 14:22:20 -0300643 if (tuner->index != 0)
Hans Verkuileae63ae2012-05-04 09:20:53 -0300644 return -EINVAL;
Joonyoung Shimb09cd162009-08-09 14:22:20 -0300645
646 /* mono/stereo selector */
647 switch (tuner->audmode) {
648 case V4L2_TUNER_MODE_MONO:
649 radio->registers[POWERCFG] |= POWERCFG_MONO; /* force mono */
650 break;
651 case V4L2_TUNER_MODE_STEREO:
Hans Verkuileae63ae2012-05-04 09:20:53 -0300652 default:
Joonyoung Shimb09cd162009-08-09 14:22:20 -0300653 radio->registers[POWERCFG] &= ~POWERCFG_MONO; /* try stereo */
654 break;
Joonyoung Shimb09cd162009-08-09 14:22:20 -0300655 }
656
Hans Verkuil340bd4c2012-04-30 19:49:27 -0300657 return si470x_set_register(radio, POWERCFG);
Joonyoung Shimb09cd162009-08-09 14:22:20 -0300658}
659
660
661/*
662 * si470x_vidioc_g_frequency - get tuner or modulator radio frequency
663 */
664static int si470x_vidioc_g_frequency(struct file *file, void *priv,
665 struct v4l2_frequency *freq)
666{
667 struct si470x_device *radio = video_drvdata(file);
Joonyoung Shimb09cd162009-08-09 14:22:20 -0300668
Hans Verkuil340bd4c2012-04-30 19:49:27 -0300669 if (freq->tuner != 0)
670 return -EINVAL;
Joonyoung Shimb09cd162009-08-09 14:22:20 -0300671
672 freq->type = V4L2_TUNER_RADIO;
Hans Verkuil340bd4c2012-04-30 19:49:27 -0300673 return si470x_get_freq(radio, &freq->frequency);
Joonyoung Shimb09cd162009-08-09 14:22:20 -0300674}
675
676
677/*
678 * si470x_vidioc_s_frequency - set tuner or modulator radio frequency
679 */
680static int si470x_vidioc_s_frequency(struct file *file, void *priv,
Hans Verkuilb530a442013-03-19 04:09:26 -0300681 const struct v4l2_frequency *freq)
Joonyoung Shimb09cd162009-08-09 14:22:20 -0300682{
683 struct si470x_device *radio = video_drvdata(file);
Hans de Goedef1406122012-07-12 16:55:48 -0300684 int retval;
Joonyoung Shimb09cd162009-08-09 14:22:20 -0300685
Hans Verkuil340bd4c2012-04-30 19:49:27 -0300686 if (freq->tuner != 0)
687 return -EINVAL;
Joonyoung Shimb09cd162009-08-09 14:22:20 -0300688
Hans de Goedef1406122012-07-12 16:55:48 -0300689 if (freq->frequency < bands[radio->band].rangelow ||
690 freq->frequency > bands[radio->band].rangehigh) {
691 /* Switch to band 1 which covers everything we support */
692 retval = si470x_set_band(radio, 1);
693 if (retval)
694 return retval;
695 }
Hans Verkuil340bd4c2012-04-30 19:49:27 -0300696 return si470x_set_freq(radio, freq->frequency);
Joonyoung Shimb09cd162009-08-09 14:22:20 -0300697}
698
699
700/*
701 * si470x_vidioc_s_hw_freq_seek - set hardware frequency seek
702 */
703static int si470x_vidioc_s_hw_freq_seek(struct file *file, void *priv,
Hans Verkuilec6f4322012-09-14 07:41:18 -0300704 const struct v4l2_hw_freq_seek *seek)
Joonyoung Shimb09cd162009-08-09 14:22:20 -0300705{
706 struct si470x_device *radio = video_drvdata(file);
Joonyoung Shimb09cd162009-08-09 14:22:20 -0300707
Hans Verkuil340bd4c2012-04-30 19:49:27 -0300708 if (seek->tuner != 0)
709 return -EINVAL;
Joonyoung Shimb09cd162009-08-09 14:22:20 -0300710
Hans Verkuil617ade62012-09-21 09:33:35 -0300711 if (file->f_flags & O_NONBLOCK)
712 return -EWOULDBLOCK;
713
Hans de Goedef1406122012-07-12 16:55:48 -0300714 return si470x_set_seek(radio, seek);
715}
716
717/*
718 * si470x_vidioc_enum_freq_bands - enumerate supported bands
719 */
720static int si470x_vidioc_enum_freq_bands(struct file *file, void *priv,
721 struct v4l2_frequency_band *band)
722{
723 if (band->tuner != 0)
724 return -EINVAL;
725 if (band->index >= ARRAY_SIZE(bands))
726 return -EINVAL;
727 *band = bands[band->index];
728 return 0;
Joonyoung Shimb09cd162009-08-09 14:22:20 -0300729}
730
Hans Verkuil4967d532012-05-04 09:16:57 -0300731const struct v4l2_ctrl_ops si470x_ctrl_ops = {
732 .s_ctrl = si470x_s_ctrl,
733};
Joonyoung Shimb09cd162009-08-09 14:22:20 -0300734
735/*
736 * si470x_ioctl_ops - video device ioctl operations
737 */
738static const struct v4l2_ioctl_ops si470x_ioctl_ops = {
739 .vidioc_querycap = si470x_vidioc_querycap,
Joonyoung Shimb09cd162009-08-09 14:22:20 -0300740 .vidioc_g_tuner = si470x_vidioc_g_tuner,
741 .vidioc_s_tuner = si470x_vidioc_s_tuner,
742 .vidioc_g_frequency = si470x_vidioc_g_frequency,
743 .vidioc_s_frequency = si470x_vidioc_s_frequency,
744 .vidioc_s_hw_freq_seek = si470x_vidioc_s_hw_freq_seek,
Hans de Goedef1406122012-07-12 16:55:48 -0300745 .vidioc_enum_freq_bands = si470x_vidioc_enum_freq_bands,
Hans Verkuileae63ae2012-05-04 09:20:53 -0300746 .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
747 .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
Joonyoung Shimb09cd162009-08-09 14:22:20 -0300748};
749
750
751/*
752 * si470x_viddev_template - video device interface
753 */
754struct video_device si470x_viddev_template = {
755 .fops = &si470x_fops,
756 .name = DRIVER_NAME,
Hans Verkuil4967d532012-05-04 09:16:57 -0300757 .release = video_device_release_empty,
Joonyoung Shimb09cd162009-08-09 14:22:20 -0300758 .ioctl_ops = &si470x_ioctl_ops,
759};