blob: 9e38132afec66488299ebd4227a94e9a2e5d3a11 [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 |
154 V4L2_TUNER_CAP_HWSEEK_BOUNDED |
155 V4L2_TUNER_CAP_HWSEEK_WRAP,
156 .rangelow = 87500 * 16,
157 .rangehigh = 108000 * 16,
158 .modulation = V4L2_BAND_MODULATION_FM,
159 },
160 {
161 .type = V4L2_TUNER_RADIO,
162 .index = 1,
163 .capability = V4L2_TUNER_CAP_LOW | V4L2_TUNER_CAP_STEREO |
164 V4L2_TUNER_CAP_RDS | V4L2_TUNER_CAP_RDS_BLOCK_IO |
165 V4L2_TUNER_CAP_HWSEEK_BOUNDED |
166 V4L2_TUNER_CAP_HWSEEK_WRAP,
167 .rangelow = 76000 * 16,
168 .rangehigh = 108000 * 16,
169 .modulation = V4L2_BAND_MODULATION_FM,
170 },
171 {
172 .type = V4L2_TUNER_RADIO,
173 .index = 2,
174 .capability = V4L2_TUNER_CAP_LOW | V4L2_TUNER_CAP_STEREO |
175 V4L2_TUNER_CAP_RDS | V4L2_TUNER_CAP_RDS_BLOCK_IO |
176 V4L2_TUNER_CAP_HWSEEK_BOUNDED |
177 V4L2_TUNER_CAP_HWSEEK_WRAP,
178 .rangelow = 76000 * 16,
179 .rangehigh = 90000 * 16,
180 .modulation = V4L2_BAND_MODULATION_FM,
181 },
182};
Joonyoung Shimb09cd162009-08-09 14:22:20 -0300183
184/**************************************************************************
185 * Generic Functions
186 **************************************************************************/
187
188/*
Hans de Goedef1406122012-07-12 16:55:48 -0300189 * si470x_set_band - set the band
190 */
191static int si470x_set_band(struct si470x_device *radio, int band)
192{
193 if (radio->band == band)
194 return 0;
195
196 radio->band = band;
197 radio->registers[SYSCONFIG2] &= ~SYSCONFIG2_BAND;
198 radio->registers[SYSCONFIG2] |= radio->band << 6;
199 return si470x_set_register(radio, SYSCONFIG2);
200}
201
202/*
Joonyoung Shimb09cd162009-08-09 14:22:20 -0300203 * si470x_set_chan - set the channel
204 */
205static int si470x_set_chan(struct si470x_device *radio, unsigned short chan)
206{
207 int retval;
Joonyoung Shimb09cd162009-08-09 14:22:20 -0300208 bool timed_out = 0;
209
210 /* start tuning */
211 radio->registers[CHANNEL] &= ~CHANNEL_CHAN;
212 radio->registers[CHANNEL] |= CHANNEL_TUNE | chan;
213 retval = si470x_set_register(radio, CHANNEL);
214 if (retval < 0)
215 goto done;
216
Hans de Goede77947112012-06-14 09:43:12 -0300217 /* wait till tune operation has completed */
218 INIT_COMPLETION(radio->completion);
219 retval = wait_for_completion_timeout(&radio->completion,
220 msecs_to_jiffies(tune_timeout));
221 if (!retval)
222 timed_out = true;
Joonyoung Shim0830be32011-03-11 03:54:46 -0300223
Joonyoung Shimb09cd162009-08-09 14:22:20 -0300224 if ((radio->registers[STATUSRSSI] & STATUSRSSI_STC) == 0)
Hans Verkuil4967d532012-05-04 09:16:57 -0300225 dev_warn(&radio->videodev.dev, "tune does not complete\n");
Joonyoung Shimb09cd162009-08-09 14:22:20 -0300226 if (timed_out)
Hans Verkuil4967d532012-05-04 09:16:57 -0300227 dev_warn(&radio->videodev.dev,
Joonyoung Shima9d6fd52009-08-09 14:22:41 -0300228 "tune timed out after %u ms\n", tune_timeout);
Joonyoung Shimb09cd162009-08-09 14:22:20 -0300229
Joonyoung Shimb09cd162009-08-09 14:22:20 -0300230 /* stop tuning */
231 radio->registers[CHANNEL] &= ~CHANNEL_TUNE;
232 retval = si470x_set_register(radio, CHANNEL);
233
234done:
235 return retval;
236}
237
Hans de Goedef1406122012-07-12 16:55:48 -0300238/*
239 * si470x_get_step - get channel spacing
240 */
241static unsigned int si470x_get_step(struct si470x_device *radio)
242{
243 /* Spacing (kHz) */
244 switch ((radio->registers[SYSCONFIG2] & SYSCONFIG2_SPACE) >> 4) {
245 /* 0: 200 kHz (USA, Australia) */
246 case 0:
247 return 200 * 16;
248 /* 1: 100 kHz (Europe, Japan) */
249 case 1:
250 return 100 * 16;
251 /* 2: 50 kHz */
252 default:
253 return 50 * 16;
254 };
255}
256
Joonyoung Shimb09cd162009-08-09 14:22:20 -0300257
258/*
259 * si470x_get_freq - get the frequency
260 */
261static int si470x_get_freq(struct si470x_device *radio, unsigned int *freq)
262{
Hans de Goedef1406122012-07-12 16:55:48 -0300263 int chan, retval;
Joonyoung Shimb09cd162009-08-09 14:22:20 -0300264
265 /* read channel */
266 retval = si470x_get_register(radio, READCHAN);
267 chan = radio->registers[READCHAN] & READCHAN_READCHAN;
268
269 /* Frequency (MHz) = Spacing (kHz) x Channel + Bottom of Band (MHz) */
Hans de Goedef1406122012-07-12 16:55:48 -0300270 *freq = chan * si470x_get_step(radio) + bands[radio->band].rangelow;
Joonyoung Shimb09cd162009-08-09 14:22:20 -0300271
272 return retval;
273}
274
275
276/*
277 * si470x_set_freq - set the frequency
278 */
279int si470x_set_freq(struct si470x_device *radio, unsigned int freq)
280{
Joonyoung Shimb09cd162009-08-09 14:22:20 -0300281 unsigned short chan;
282
Hans de Goedef1406122012-07-12 16:55:48 -0300283 freq = clamp(freq, bands[radio->band].rangelow,
284 bands[radio->band].rangehigh);
Joonyoung Shimb09cd162009-08-09 14:22:20 -0300285 /* Chan = [ Freq (Mhz) - Bottom of Band (MHz) ] / Spacing (kHz) */
Hans de Goedef1406122012-07-12 16:55:48 -0300286 chan = (freq - bands[radio->band].rangelow) / si470x_get_step(radio);
Joonyoung Shimb09cd162009-08-09 14:22:20 -0300287
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,
Hans de Goedef1406122012-07-12 16:55:48 -0300296 struct v4l2_hw_freq_seek *seek)
Joonyoung Shimb09cd162009-08-09 14:22:20 -0300297{
Hans de Goedef1406122012-07-12 16:55:48 -0300298 int band, retval;
299 unsigned int freq;
Joonyoung Shimb09cd162009-08-09 14:22:20 -0300300 bool timed_out = 0;
301
Hans de Goedef1406122012-07-12 16:55:48 -0300302 /* set band */
303 if (seek->rangelow || seek->rangehigh) {
304 for (band = 0; band < ARRAY_SIZE(bands); band++) {
305 if (bands[band].rangelow == seek->rangelow &&
306 bands[band].rangehigh == seek->rangehigh)
307 break;
308 }
309 if (band == ARRAY_SIZE(bands))
310 return -EINVAL; /* No matching band found */
311 } else
312 band = 1; /* If nothing is specified seek 76 - 108 Mhz */
313
314 if (radio->band != band) {
315 retval = si470x_get_freq(radio, &freq);
316 if (retval)
317 return retval;
318 retval = si470x_set_band(radio, band);
319 if (retval)
320 return retval;
321 retval = si470x_set_freq(radio, freq);
322 if (retval)
323 return retval;
324 }
325
Joonyoung Shimb09cd162009-08-09 14:22:20 -0300326 /* start seeking */
327 radio->registers[POWERCFG] |= POWERCFG_SEEK;
Hans de Goedef1406122012-07-12 16:55:48 -0300328 if (seek->wrap_around)
Joonyoung Shimb09cd162009-08-09 14:22:20 -0300329 radio->registers[POWERCFG] &= ~POWERCFG_SKMODE;
330 else
331 radio->registers[POWERCFG] |= POWERCFG_SKMODE;
Hans de Goedef1406122012-07-12 16:55:48 -0300332 if (seek->seek_upward)
Joonyoung Shimb09cd162009-08-09 14:22:20 -0300333 radio->registers[POWERCFG] |= POWERCFG_SEEKUP;
334 else
335 radio->registers[POWERCFG] &= ~POWERCFG_SEEKUP;
336 retval = si470x_set_register(radio, POWERCFG);
337 if (retval < 0)
Hans Verkuil340bd4c2012-04-30 19:49:27 -0300338 return retval;
Joonyoung Shimb09cd162009-08-09 14:22:20 -0300339
Hans de Goede77947112012-06-14 09:43:12 -0300340 /* wait till tune operation has completed */
341 INIT_COMPLETION(radio->completion);
342 retval = wait_for_completion_timeout(&radio->completion,
343 msecs_to_jiffies(seek_timeout));
344 if (!retval)
345 timed_out = true;
Joonyoung Shim0830be32011-03-11 03:54:46 -0300346
Joonyoung Shimb09cd162009-08-09 14:22:20 -0300347 if ((radio->registers[STATUSRSSI] & STATUSRSSI_STC) == 0)
Hans Verkuil4967d532012-05-04 09:16:57 -0300348 dev_warn(&radio->videodev.dev, "seek does not complete\n");
Joonyoung Shimb09cd162009-08-09 14:22:20 -0300349 if (radio->registers[STATUSRSSI] & STATUSRSSI_SF)
Hans Verkuil4967d532012-05-04 09:16:57 -0300350 dev_warn(&radio->videodev.dev,
Joonyoung Shima9d6fd52009-08-09 14:22:41 -0300351 "seek failed / band limit reached\n");
Joonyoung Shimb09cd162009-08-09 14:22:20 -0300352
Joonyoung Shimb09cd162009-08-09 14:22:20 -0300353 /* stop seeking */
354 radio->registers[POWERCFG] &= ~POWERCFG_SEEK;
355 retval = si470x_set_register(radio, POWERCFG);
356
Joonyoung Shimb09cd162009-08-09 14:22:20 -0300357 /* try again, if timed out */
Hans Verkuil340bd4c2012-04-30 19:49:27 -0300358 if (retval == 0 && timed_out)
Hans Verkuil54f60192012-05-27 07:25:06 -0300359 return -ENODATA;
Joonyoung Shimb09cd162009-08-09 14:22:20 -0300360 return retval;
361}
362
363
364/*
365 * si470x_start - switch on radio
366 */
367int si470x_start(struct si470x_device *radio)
368{
369 int retval;
370
371 /* powercfg */
372 radio->registers[POWERCFG] =
373 POWERCFG_DMUTE | POWERCFG_ENABLE | POWERCFG_RDSM;
374 retval = si470x_set_register(radio, POWERCFG);
375 if (retval < 0)
376 goto done;
377
378 /* sysconfig 1 */
Tobias Lorenz131ddd12011-01-08 14:12:30 -0300379 radio->registers[SYSCONFIG1] =
380 (de << 11) & SYSCONFIG1_DE; /* DE*/
Joonyoung Shimb09cd162009-08-09 14:22:20 -0300381 retval = si470x_set_register(radio, SYSCONFIG1);
382 if (retval < 0)
383 goto done;
384
385 /* sysconfig 2 */
386 radio->registers[SYSCONFIG2] =
Hans de Goedeb9664252012-06-14 09:43:13 -0300387 (0x1f << 8) | /* SEEKTH */
Hans de Goedef1406122012-07-12 16:55:48 -0300388 ((radio->band << 6) & SYSCONFIG2_BAND) |/* BAND */
Joonyoung Shimb09cd162009-08-09 14:22:20 -0300389 ((space << 4) & SYSCONFIG2_SPACE) | /* SPACE */
390 15; /* VOLUME (max) */
391 retval = si470x_set_register(radio, SYSCONFIG2);
392 if (retval < 0)
393 goto done;
394
395 /* reset last channel */
396 retval = si470x_set_chan(radio,
397 radio->registers[CHANNEL] & CHANNEL_CHAN);
398
399done:
400 return retval;
401}
402
403
404/*
405 * si470x_stop - switch off radio
406 */
407int si470x_stop(struct si470x_device *radio)
408{
409 int retval;
410
411 /* sysconfig 1 */
412 radio->registers[SYSCONFIG1] &= ~SYSCONFIG1_RDS;
413 retval = si470x_set_register(radio, SYSCONFIG1);
414 if (retval < 0)
415 goto done;
416
417 /* powercfg */
418 radio->registers[POWERCFG] &= ~POWERCFG_DMUTE;
419 /* POWERCFG_ENABLE has to automatically go low */
420 radio->registers[POWERCFG] |= POWERCFG_ENABLE | POWERCFG_DISABLE;
421 retval = si470x_set_register(radio, POWERCFG);
422
423done:
424 return retval;
425}
426
427
428/*
429 * si470x_rds_on - switch on rds reception
430 */
Mauro Carvalho Chehabf2f8e852010-09-15 08:35:45 -0300431static int si470x_rds_on(struct si470x_device *radio)
Joonyoung Shimb09cd162009-08-09 14:22:20 -0300432{
433 int retval;
434
435 /* sysconfig 1 */
Joonyoung Shimb09cd162009-08-09 14:22:20 -0300436 radio->registers[SYSCONFIG1] |= SYSCONFIG1_RDS;
437 retval = si470x_set_register(radio, SYSCONFIG1);
438 if (retval < 0)
439 radio->registers[SYSCONFIG1] &= ~SYSCONFIG1_RDS;
Joonyoung Shimb09cd162009-08-09 14:22:20 -0300440
441 return retval;
442}
443
444
445
446/**************************************************************************
Joonyoung Shim1aa925c2009-12-10 16:49:34 -0300447 * File Operations Interface
448 **************************************************************************/
449
450/*
451 * si470x_fops_read - read RDS data
452 */
453static ssize_t si470x_fops_read(struct file *file, char __user *buf,
454 size_t count, loff_t *ppos)
455{
456 struct si470x_device *radio = video_drvdata(file);
457 int retval = 0;
458 unsigned int block_count = 0;
459
460 /* switch on rds reception */
461 if ((radio->registers[SYSCONFIG1] & SYSCONFIG1_RDS) == 0)
462 si470x_rds_on(radio);
463
464 /* block if no new data available */
465 while (radio->wr_index == radio->rd_index) {
466 if (file->f_flags & O_NONBLOCK) {
467 retval = -EWOULDBLOCK;
468 goto done;
469 }
470 if (wait_event_interruptible(radio->read_queue,
471 radio->wr_index != radio->rd_index) < 0) {
472 retval = -EINTR;
473 goto done;
474 }
475 }
476
477 /* calculate block count from byte count */
478 count /= 3;
479
480 /* copy RDS block out of internal buffer and to user buffer */
Joonyoung Shim1aa925c2009-12-10 16:49:34 -0300481 while (block_count < count) {
482 if (radio->rd_index == radio->wr_index)
483 break;
484
485 /* always transfer rds complete blocks */
486 if (copy_to_user(buf, &radio->buffer[radio->rd_index], 3))
487 /* retval = -EFAULT; */
488 break;
489
490 /* increment and wrap read pointer */
491 radio->rd_index += 3;
492 if (radio->rd_index >= radio->buf_size)
493 radio->rd_index = 0;
494
495 /* increment counters */
496 block_count++;
497 buf += 3;
498 retval += 3;
499 }
Joonyoung Shim1aa925c2009-12-10 16:49:34 -0300500
501done:
502 return retval;
503}
504
505
506/*
507 * si470x_fops_poll - poll RDS data
508 */
509static unsigned int si470x_fops_poll(struct file *file,
510 struct poll_table_struct *pts)
511{
512 struct si470x_device *radio = video_drvdata(file);
Hans Verkuileae63ae2012-05-04 09:20:53 -0300513 unsigned long req_events = poll_requested_events(pts);
514 int retval = v4l2_ctrl_poll(file, pts);
Joonyoung Shim1aa925c2009-12-10 16:49:34 -0300515
Hans Verkuileae63ae2012-05-04 09:20:53 -0300516 if (req_events & (POLLIN | POLLRDNORM)) {
517 /* switch on rds reception */
518 if ((radio->registers[SYSCONFIG1] & SYSCONFIG1_RDS) == 0)
519 si470x_rds_on(radio);
Mauro Carvalho Chehabf2f8e852010-09-15 08:35:45 -0300520
Hans Verkuileae63ae2012-05-04 09:20:53 -0300521 poll_wait(file, &radio->read_queue, pts);
Joonyoung Shim1aa925c2009-12-10 16:49:34 -0300522
Hans Verkuileae63ae2012-05-04 09:20:53 -0300523 if (radio->rd_index != radio->wr_index)
524 retval |= POLLIN | POLLRDNORM;
525 }
Joonyoung Shim1aa925c2009-12-10 16:49:34 -0300526
527 return retval;
528}
529
530
531/*
532 * si470x_fops - file operations interface
533 */
534static const struct v4l2_file_operations si470x_fops = {
535 .owner = THIS_MODULE,
536 .read = si470x_fops_read,
537 .poll = si470x_fops_poll,
Mauro Carvalho Chehabf2f8e852010-09-15 08:35:45 -0300538 .unlocked_ioctl = video_ioctl2,
Joonyoung Shim1aa925c2009-12-10 16:49:34 -0300539 .open = si470x_fops_open,
540 .release = si470x_fops_release,
541};
542
543
544
545/**************************************************************************
Joonyoung Shimb09cd162009-08-09 14:22:20 -0300546 * Video4Linux Interface
547 **************************************************************************/
548
Hans Verkuil4967d532012-05-04 09:16:57 -0300549
550static int si470x_s_ctrl(struct v4l2_ctrl *ctrl)
Joonyoung Shimb09cd162009-08-09 14:22:20 -0300551{
Hans Verkuil4967d532012-05-04 09:16:57 -0300552 struct si470x_device *radio =
553 container_of(ctrl->handler, struct si470x_device, hdl);
Joonyoung Shimb09cd162009-08-09 14:22:20 -0300554
555 switch (ctrl->id) {
556 case V4L2_CID_AUDIO_VOLUME:
557 radio->registers[SYSCONFIG2] &= ~SYSCONFIG2_VOLUME;
Hans Verkuil4967d532012-05-04 09:16:57 -0300558 radio->registers[SYSCONFIG2] |= ctrl->val;
559 return si470x_set_register(radio, SYSCONFIG2);
Joonyoung Shimb09cd162009-08-09 14:22:20 -0300560 case V4L2_CID_AUDIO_MUTE:
Hans Verkuil4967d532012-05-04 09:16:57 -0300561 if (ctrl->val)
Joonyoung Shimb09cd162009-08-09 14:22:20 -0300562 radio->registers[POWERCFG] &= ~POWERCFG_DMUTE;
563 else
564 radio->registers[POWERCFG] |= POWERCFG_DMUTE;
Hans Verkuil4967d532012-05-04 09:16:57 -0300565 return si470x_set_register(radio, POWERCFG);
Joonyoung Shimb09cd162009-08-09 14:22:20 -0300566 default:
Hans Verkuil4967d532012-05-04 09:16:57 -0300567 return -EINVAL;
Joonyoung Shimb09cd162009-08-09 14:22:20 -0300568 }
Joonyoung Shimb09cd162009-08-09 14:22:20 -0300569}
570
571
572/*
573 * si470x_vidioc_g_tuner - get tuner attributes
574 */
575static int si470x_vidioc_g_tuner(struct file *file, void *priv,
576 struct v4l2_tuner *tuner)
577{
578 struct si470x_device *radio = video_drvdata(file);
Hans de Goede86ef3f72012-06-14 09:43:11 -0300579 int retval = 0;
Joonyoung Shimb09cd162009-08-09 14:22:20 -0300580
Hans Verkuil340bd4c2012-04-30 19:49:27 -0300581 if (tuner->index != 0)
582 return -EINVAL;
Joonyoung Shimb09cd162009-08-09 14:22:20 -0300583
Hans de Goede86ef3f72012-06-14 09:43:11 -0300584 if (!radio->status_rssi_auto_update) {
585 retval = si470x_get_register(radio, STATUSRSSI);
586 if (retval < 0)
587 return retval;
588 }
Joonyoung Shimb09cd162009-08-09 14:22:20 -0300589
590 /* driver constants */
591 strcpy(tuner->name, "FM");
592 tuner->type = V4L2_TUNER_RADIO;
593 tuner->capability = V4L2_TUNER_CAP_LOW | V4L2_TUNER_CAP_STEREO |
Hans Verkuil54f60192012-05-27 07:25:06 -0300594 V4L2_TUNER_CAP_RDS | V4L2_TUNER_CAP_RDS_BLOCK_IO |
595 V4L2_TUNER_CAP_HWSEEK_BOUNDED |
596 V4L2_TUNER_CAP_HWSEEK_WRAP;
Hans de Goedef1406122012-07-12 16:55:48 -0300597 tuner->rangelow = 76 * FREQ_MUL;
598 tuner->rangehigh = 108 * FREQ_MUL;
Joonyoung Shimb09cd162009-08-09 14:22:20 -0300599
600 /* stereo indicator == stereo (instead of mono) */
601 if ((radio->registers[STATUSRSSI] & STATUSRSSI_ST) == 0)
602 tuner->rxsubchans = V4L2_TUNER_SUB_MONO;
603 else
Hans Verkuil4967d532012-05-04 09:16:57 -0300604 tuner->rxsubchans = V4L2_TUNER_SUB_STEREO;
Joonyoung Shimb09cd162009-08-09 14:22:20 -0300605 /* If there is a reliable method of detecting an RDS channel,
606 then this code should check for that before setting this
607 RDS subchannel. */
608 tuner->rxsubchans |= V4L2_TUNER_SUB_RDS;
609
610 /* mono/stereo selector */
611 if ((radio->registers[POWERCFG] & POWERCFG_MONO) == 0)
612 tuner->audmode = V4L2_TUNER_MODE_STEREO;
613 else
614 tuner->audmode = V4L2_TUNER_MODE_MONO;
615
616 /* min is worst, max is best; signal:0..0xffff; rssi: 0..0xff */
Tobias Lorenz144dcdc2010-02-18 16:33:46 -0300617 /* measured in units of dbµV in 1 db increments (max at ~75 dbµV) */
Joonyoung Shimb09cd162009-08-09 14:22:20 -0300618 tuner->signal = (radio->registers[STATUSRSSI] & STATUSRSSI_RSSI);
619 /* the ideal factor is 0xffff/75 = 873,8 */
620 tuner->signal = (tuner->signal * 873) + (8 * tuner->signal / 10);
Hans Verkuileae63ae2012-05-04 09:20:53 -0300621 if (tuner->signal > 0xffff)
622 tuner->signal = 0xffff;
Joonyoung Shimb09cd162009-08-09 14:22:20 -0300623
624 /* automatic frequency control: -1: freq to low, 1 freq to high */
625 /* AFCRL does only indicate that freq. differs, not if too low/high */
626 tuner->afc = (radio->registers[STATUSRSSI] & STATUSRSSI_AFCRL) ? 1 : 0;
627
Joonyoung Shimb09cd162009-08-09 14:22:20 -0300628 return retval;
629}
630
631
632/*
633 * si470x_vidioc_s_tuner - set tuner attributes
634 */
635static int si470x_vidioc_s_tuner(struct file *file, void *priv,
636 struct v4l2_tuner *tuner)
637{
638 struct si470x_device *radio = video_drvdata(file);
Joonyoung Shimb09cd162009-08-09 14:22:20 -0300639
Joonyoung Shimb09cd162009-08-09 14:22:20 -0300640 if (tuner->index != 0)
Hans Verkuileae63ae2012-05-04 09:20:53 -0300641 return -EINVAL;
Joonyoung Shimb09cd162009-08-09 14:22:20 -0300642
643 /* mono/stereo selector */
644 switch (tuner->audmode) {
645 case V4L2_TUNER_MODE_MONO:
646 radio->registers[POWERCFG] |= POWERCFG_MONO; /* force mono */
647 break;
648 case V4L2_TUNER_MODE_STEREO:
Hans Verkuileae63ae2012-05-04 09:20:53 -0300649 default:
Joonyoung Shimb09cd162009-08-09 14:22:20 -0300650 radio->registers[POWERCFG] &= ~POWERCFG_MONO; /* try stereo */
651 break;
Joonyoung Shimb09cd162009-08-09 14:22:20 -0300652 }
653
Hans Verkuil340bd4c2012-04-30 19:49:27 -0300654 return si470x_set_register(radio, POWERCFG);
Joonyoung Shimb09cd162009-08-09 14:22:20 -0300655}
656
657
658/*
659 * si470x_vidioc_g_frequency - get tuner or modulator radio frequency
660 */
661static int si470x_vidioc_g_frequency(struct file *file, void *priv,
662 struct v4l2_frequency *freq)
663{
664 struct si470x_device *radio = video_drvdata(file);
Joonyoung Shimb09cd162009-08-09 14:22:20 -0300665
Hans Verkuil340bd4c2012-04-30 19:49:27 -0300666 if (freq->tuner != 0)
667 return -EINVAL;
Joonyoung Shimb09cd162009-08-09 14:22:20 -0300668
669 freq->type = V4L2_TUNER_RADIO;
Hans Verkuil340bd4c2012-04-30 19:49:27 -0300670 return si470x_get_freq(radio, &freq->frequency);
Joonyoung Shimb09cd162009-08-09 14:22:20 -0300671}
672
673
674/*
675 * si470x_vidioc_s_frequency - set tuner or modulator radio frequency
676 */
677static int si470x_vidioc_s_frequency(struct file *file, void *priv,
678 struct v4l2_frequency *freq)
679{
680 struct si470x_device *radio = video_drvdata(file);
Hans de Goedef1406122012-07-12 16:55:48 -0300681 int retval;
Joonyoung Shimb09cd162009-08-09 14:22:20 -0300682
Hans Verkuil340bd4c2012-04-30 19:49:27 -0300683 if (freq->tuner != 0)
684 return -EINVAL;
Joonyoung Shimb09cd162009-08-09 14:22:20 -0300685
Hans de Goedef1406122012-07-12 16:55:48 -0300686 if (freq->frequency < bands[radio->band].rangelow ||
687 freq->frequency > bands[radio->band].rangehigh) {
688 /* Switch to band 1 which covers everything we support */
689 retval = si470x_set_band(radio, 1);
690 if (retval)
691 return retval;
692 }
Hans Verkuil340bd4c2012-04-30 19:49:27 -0300693 return si470x_set_freq(radio, freq->frequency);
Joonyoung Shimb09cd162009-08-09 14:22:20 -0300694}
695
696
697/*
698 * si470x_vidioc_s_hw_freq_seek - set hardware frequency seek
699 */
700static int si470x_vidioc_s_hw_freq_seek(struct file *file, void *priv,
701 struct v4l2_hw_freq_seek *seek)
702{
703 struct si470x_device *radio = video_drvdata(file);
Joonyoung Shimb09cd162009-08-09 14:22:20 -0300704
Hans Verkuil340bd4c2012-04-30 19:49:27 -0300705 if (seek->tuner != 0)
706 return -EINVAL;
Joonyoung Shimb09cd162009-08-09 14:22:20 -0300707
Hans de Goedef1406122012-07-12 16:55:48 -0300708 return si470x_set_seek(radio, seek);
709}
710
711/*
712 * si470x_vidioc_enum_freq_bands - enumerate supported bands
713 */
714static int si470x_vidioc_enum_freq_bands(struct file *file, void *priv,
715 struct v4l2_frequency_band *band)
716{
717 if (band->tuner != 0)
718 return -EINVAL;
719 if (band->index >= ARRAY_SIZE(bands))
720 return -EINVAL;
721 *band = bands[band->index];
722 return 0;
Joonyoung Shimb09cd162009-08-09 14:22:20 -0300723}
724
Hans Verkuil4967d532012-05-04 09:16:57 -0300725const struct v4l2_ctrl_ops si470x_ctrl_ops = {
726 .s_ctrl = si470x_s_ctrl,
727};
Joonyoung Shimb09cd162009-08-09 14:22:20 -0300728
729/*
730 * si470x_ioctl_ops - video device ioctl operations
731 */
732static const struct v4l2_ioctl_ops si470x_ioctl_ops = {
733 .vidioc_querycap = si470x_vidioc_querycap,
Joonyoung Shimb09cd162009-08-09 14:22:20 -0300734 .vidioc_g_tuner = si470x_vidioc_g_tuner,
735 .vidioc_s_tuner = si470x_vidioc_s_tuner,
736 .vidioc_g_frequency = si470x_vidioc_g_frequency,
737 .vidioc_s_frequency = si470x_vidioc_s_frequency,
738 .vidioc_s_hw_freq_seek = si470x_vidioc_s_hw_freq_seek,
Hans de Goedef1406122012-07-12 16:55:48 -0300739 .vidioc_enum_freq_bands = si470x_vidioc_enum_freq_bands,
Hans Verkuileae63ae2012-05-04 09:20:53 -0300740 .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
741 .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
Joonyoung Shimb09cd162009-08-09 14:22:20 -0300742};
743
744
745/*
746 * si470x_viddev_template - video device interface
747 */
748struct video_device si470x_viddev_template = {
749 .fops = &si470x_fops,
750 .name = DRIVER_NAME,
Hans Verkuil4967d532012-05-04 09:16:57 -0300751 .release = video_device_release_empty,
Joonyoung Shimb09cd162009-08-09 14:22:20 -0300752 .ioctl_ops = &si470x_ioctl_ops,
753};