blob: e70badf7c6e91eb08b1d09a386e30b3bb432cb18 [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;
167 unsigned long timeout;
168 bool timed_out = 0;
169
170 /* start tuning */
171 radio->registers[CHANNEL] &= ~CHANNEL_CHAN;
172 radio->registers[CHANNEL] |= CHANNEL_TUNE | chan;
173 retval = si470x_set_register(radio, CHANNEL);
174 if (retval < 0)
175 goto done;
176
Joonyoung Shim0830be32011-03-11 03:54:46 -0300177 /* currently I2C driver only uses interrupt way to tune */
178 if (radio->stci_enabled) {
179 INIT_COMPLETION(radio->completion);
180
181 /* wait till tune operation has completed */
182 retval = wait_for_completion_timeout(&radio->completion,
183 msecs_to_jiffies(tune_timeout));
184 if (!retval)
185 timed_out = true;
186 } else {
187 /* wait till tune operation has completed */
188 timeout = jiffies + msecs_to_jiffies(tune_timeout);
189 do {
190 retval = si470x_get_register(radio, STATUSRSSI);
191 if (retval < 0)
192 goto stop;
193 timed_out = time_after(jiffies, timeout);
194 } while (((radio->registers[STATUSRSSI] & STATUSRSSI_STC) == 0)
195 && (!timed_out));
196 }
197
Joonyoung Shimb09cd162009-08-09 14:22:20 -0300198 if ((radio->registers[STATUSRSSI] & STATUSRSSI_STC) == 0)
Hans Verkuil4967d532012-05-04 09:16:57 -0300199 dev_warn(&radio->videodev.dev, "tune does not complete\n");
Joonyoung Shimb09cd162009-08-09 14:22:20 -0300200 if (timed_out)
Hans Verkuil4967d532012-05-04 09:16:57 -0300201 dev_warn(&radio->videodev.dev,
Joonyoung Shima9d6fd52009-08-09 14:22:41 -0300202 "tune timed out after %u ms\n", tune_timeout);
Joonyoung Shimb09cd162009-08-09 14:22:20 -0300203
204stop:
205 /* stop tuning */
206 radio->registers[CHANNEL] &= ~CHANNEL_TUNE;
207 retval = si470x_set_register(radio, CHANNEL);
208
209done:
210 return retval;
211}
212
213
214/*
215 * si470x_get_freq - get the frequency
216 */
217static int si470x_get_freq(struct si470x_device *radio, unsigned int *freq)
218{
219 unsigned int spacing, band_bottom;
220 unsigned short chan;
221 int retval;
222
223 /* Spacing (kHz) */
224 switch ((radio->registers[SYSCONFIG2] & SYSCONFIG2_SPACE) >> 4) {
225 /* 0: 200 kHz (USA, Australia) */
226 case 0:
227 spacing = 0.200 * FREQ_MUL; break;
228 /* 1: 100 kHz (Europe, Japan) */
229 case 1:
230 spacing = 0.100 * FREQ_MUL; break;
231 /* 2: 50 kHz */
232 default:
233 spacing = 0.050 * FREQ_MUL; break;
234 };
235
236 /* Bottom of Band (MHz) */
237 switch ((radio->registers[SYSCONFIG2] & SYSCONFIG2_BAND) >> 6) {
238 /* 0: 87.5 - 108 MHz (USA, Europe) */
239 case 0:
240 band_bottom = 87.5 * FREQ_MUL; break;
241 /* 1: 76 - 108 MHz (Japan wide band) */
242 default:
243 band_bottom = 76 * FREQ_MUL; break;
244 /* 2: 76 - 90 MHz (Japan) */
245 case 2:
246 band_bottom = 76 * FREQ_MUL; break;
247 };
248
249 /* read channel */
250 retval = si470x_get_register(radio, READCHAN);
251 chan = radio->registers[READCHAN] & READCHAN_READCHAN;
252
253 /* Frequency (MHz) = Spacing (kHz) x Channel + Bottom of Band (MHz) */
254 *freq = chan * spacing + band_bottom;
255
256 return retval;
257}
258
259
260/*
261 * si470x_set_freq - set the frequency
262 */
263int si470x_set_freq(struct si470x_device *radio, unsigned int freq)
264{
Hans Verkuileae63ae2012-05-04 09:20:53 -0300265 unsigned int spacing, band_bottom, band_top;
Joonyoung Shimb09cd162009-08-09 14:22:20 -0300266 unsigned short chan;
267
268 /* Spacing (kHz) */
269 switch ((radio->registers[SYSCONFIG2] & SYSCONFIG2_SPACE) >> 4) {
270 /* 0: 200 kHz (USA, Australia) */
271 case 0:
272 spacing = 0.200 * FREQ_MUL; break;
273 /* 1: 100 kHz (Europe, Japan) */
274 case 1:
275 spacing = 0.100 * FREQ_MUL; break;
276 /* 2: 50 kHz */
277 default:
278 spacing = 0.050 * FREQ_MUL; break;
279 };
280
Hans Verkuileae63ae2012-05-04 09:20:53 -0300281 /* Bottom/Top of Band (MHz) */
Joonyoung Shimb09cd162009-08-09 14:22:20 -0300282 switch ((radio->registers[SYSCONFIG2] & SYSCONFIG2_BAND) >> 6) {
283 /* 0: 87.5 - 108 MHz (USA, Europe) */
284 case 0:
Hans Verkuileae63ae2012-05-04 09:20:53 -0300285 band_bottom = 87.5 * FREQ_MUL;
286 band_top = 108 * FREQ_MUL;
287 break;
Joonyoung Shimb09cd162009-08-09 14:22:20 -0300288 /* 1: 76 - 108 MHz (Japan wide band) */
289 default:
Hans Verkuileae63ae2012-05-04 09:20:53 -0300290 band_bottom = 76 * FREQ_MUL;
291 band_top = 108 * FREQ_MUL;
292 break;
Joonyoung Shimb09cd162009-08-09 14:22:20 -0300293 /* 2: 76 - 90 MHz (Japan) */
294 case 2:
Hans Verkuileae63ae2012-05-04 09:20:53 -0300295 band_bottom = 76 * FREQ_MUL;
296 band_top = 90 * FREQ_MUL;
297 break;
Joonyoung Shimb09cd162009-08-09 14:22:20 -0300298 };
299
Hans Verkuileae63ae2012-05-04 09:20:53 -0300300 freq = clamp(freq, band_bottom, band_top);
Joonyoung Shimb09cd162009-08-09 14:22:20 -0300301 /* Chan = [ Freq (Mhz) - Bottom of Band (MHz) ] / Spacing (kHz) */
302 chan = (freq - band_bottom) / spacing;
303
304 return si470x_set_chan(radio, chan);
305}
306
307
308/*
309 * si470x_set_seek - set seek
310 */
311static int si470x_set_seek(struct si470x_device *radio,
312 unsigned int wrap_around, unsigned int seek_upward)
313{
314 int retval = 0;
315 unsigned long timeout;
316 bool timed_out = 0;
317
318 /* start seeking */
319 radio->registers[POWERCFG] |= POWERCFG_SEEK;
320 if (wrap_around == 1)
321 radio->registers[POWERCFG] &= ~POWERCFG_SKMODE;
322 else
323 radio->registers[POWERCFG] |= POWERCFG_SKMODE;
324 if (seek_upward == 1)
325 radio->registers[POWERCFG] |= POWERCFG_SEEKUP;
326 else
327 radio->registers[POWERCFG] &= ~POWERCFG_SEEKUP;
328 retval = si470x_set_register(radio, POWERCFG);
329 if (retval < 0)
330 goto done;
331
Joonyoung Shim0830be32011-03-11 03:54:46 -0300332 /* currently I2C driver only uses interrupt way to seek */
333 if (radio->stci_enabled) {
334 INIT_COMPLETION(radio->completion);
335
336 /* wait till seek operation has completed */
337 retval = wait_for_completion_timeout(&radio->completion,
338 msecs_to_jiffies(seek_timeout));
339 if (!retval)
340 timed_out = true;
341 } else {
342 /* wait till seek operation has completed */
343 timeout = jiffies + msecs_to_jiffies(seek_timeout);
344 do {
345 retval = si470x_get_register(radio, STATUSRSSI);
346 if (retval < 0)
347 goto stop;
348 timed_out = time_after(jiffies, timeout);
349 } while (((radio->registers[STATUSRSSI] & STATUSRSSI_STC) == 0)
350 && (!timed_out));
351 }
352
Joonyoung Shimb09cd162009-08-09 14:22:20 -0300353 if ((radio->registers[STATUSRSSI] & STATUSRSSI_STC) == 0)
Hans Verkuil4967d532012-05-04 09:16:57 -0300354 dev_warn(&radio->videodev.dev, "seek does not complete\n");
Joonyoung Shimb09cd162009-08-09 14:22:20 -0300355 if (radio->registers[STATUSRSSI] & STATUSRSSI_SF)
Hans Verkuil4967d532012-05-04 09:16:57 -0300356 dev_warn(&radio->videodev.dev,
Joonyoung Shima9d6fd52009-08-09 14:22:41 -0300357 "seek failed / band limit reached\n");
Joonyoung Shimb09cd162009-08-09 14:22:20 -0300358 if (timed_out)
Hans Verkuil4967d532012-05-04 09:16:57 -0300359 dev_warn(&radio->videodev.dev,
Joonyoung Shima9d6fd52009-08-09 14:22:41 -0300360 "seek timed out after %u ms\n", seek_timeout);
Joonyoung Shimb09cd162009-08-09 14:22:20 -0300361
362stop:
363 /* stop seeking */
364 radio->registers[POWERCFG] &= ~POWERCFG_SEEK;
365 retval = si470x_set_register(radio, POWERCFG);
366
367done:
368 /* try again, if timed out */
369 if ((retval == 0) && timed_out)
370 retval = -EAGAIN;
371
372 return retval;
373}
374
375
376/*
377 * si470x_start - switch on radio
378 */
379int si470x_start(struct si470x_device *radio)
380{
381 int retval;
382
383 /* powercfg */
384 radio->registers[POWERCFG] =
385 POWERCFG_DMUTE | POWERCFG_ENABLE | POWERCFG_RDSM;
386 retval = si470x_set_register(radio, POWERCFG);
387 if (retval < 0)
388 goto done;
389
390 /* sysconfig 1 */
Tobias Lorenz131ddd12011-01-08 14:12:30 -0300391 radio->registers[SYSCONFIG1] =
392 (de << 11) & SYSCONFIG1_DE; /* DE*/
Joonyoung Shimb09cd162009-08-09 14:22:20 -0300393 retval = si470x_set_register(radio, SYSCONFIG1);
394 if (retval < 0)
395 goto done;
396
397 /* sysconfig 2 */
398 radio->registers[SYSCONFIG2] =
399 (0x3f << 8) | /* SEEKTH */
400 ((band << 6) & SYSCONFIG2_BAND) | /* BAND */
401 ((space << 4) & SYSCONFIG2_SPACE) | /* SPACE */
402 15; /* VOLUME (max) */
403 retval = si470x_set_register(radio, SYSCONFIG2);
404 if (retval < 0)
405 goto done;
406
407 /* reset last channel */
408 retval = si470x_set_chan(radio,
409 radio->registers[CHANNEL] & CHANNEL_CHAN);
410
411done:
412 return retval;
413}
414
415
416/*
417 * si470x_stop - switch off radio
418 */
419int si470x_stop(struct si470x_device *radio)
420{
421 int retval;
422
423 /* sysconfig 1 */
424 radio->registers[SYSCONFIG1] &= ~SYSCONFIG1_RDS;
425 retval = si470x_set_register(radio, SYSCONFIG1);
426 if (retval < 0)
427 goto done;
428
429 /* powercfg */
430 radio->registers[POWERCFG] &= ~POWERCFG_DMUTE;
431 /* POWERCFG_ENABLE has to automatically go low */
432 radio->registers[POWERCFG] |= POWERCFG_ENABLE | POWERCFG_DISABLE;
433 retval = si470x_set_register(radio, POWERCFG);
434
435done:
436 return retval;
437}
438
439
440/*
441 * si470x_rds_on - switch on rds reception
442 */
Mauro Carvalho Chehabf2f8e852010-09-15 08:35:45 -0300443static int si470x_rds_on(struct si470x_device *radio)
Joonyoung Shimb09cd162009-08-09 14:22:20 -0300444{
445 int retval;
446
447 /* sysconfig 1 */
Joonyoung Shimb09cd162009-08-09 14:22:20 -0300448 radio->registers[SYSCONFIG1] |= SYSCONFIG1_RDS;
449 retval = si470x_set_register(radio, SYSCONFIG1);
450 if (retval < 0)
451 radio->registers[SYSCONFIG1] &= ~SYSCONFIG1_RDS;
Joonyoung Shimb09cd162009-08-09 14:22:20 -0300452
453 return retval;
454}
455
456
457
458/**************************************************************************
Joonyoung Shim1aa925c2009-12-10 16:49:34 -0300459 * File Operations Interface
460 **************************************************************************/
461
462/*
463 * si470x_fops_read - read RDS data
464 */
465static ssize_t si470x_fops_read(struct file *file, char __user *buf,
466 size_t count, loff_t *ppos)
467{
468 struct si470x_device *radio = video_drvdata(file);
469 int retval = 0;
470 unsigned int block_count = 0;
471
472 /* switch on rds reception */
473 if ((radio->registers[SYSCONFIG1] & SYSCONFIG1_RDS) == 0)
474 si470x_rds_on(radio);
475
476 /* block if no new data available */
477 while (radio->wr_index == radio->rd_index) {
478 if (file->f_flags & O_NONBLOCK) {
479 retval = -EWOULDBLOCK;
480 goto done;
481 }
482 if (wait_event_interruptible(radio->read_queue,
483 radio->wr_index != radio->rd_index) < 0) {
484 retval = -EINTR;
485 goto done;
486 }
487 }
488
489 /* calculate block count from byte count */
490 count /= 3;
491
492 /* copy RDS block out of internal buffer and to user buffer */
Joonyoung Shim1aa925c2009-12-10 16:49:34 -0300493 while (block_count < count) {
494 if (radio->rd_index == radio->wr_index)
495 break;
496
497 /* always transfer rds complete blocks */
498 if (copy_to_user(buf, &radio->buffer[radio->rd_index], 3))
499 /* retval = -EFAULT; */
500 break;
501
502 /* increment and wrap read pointer */
503 radio->rd_index += 3;
504 if (radio->rd_index >= radio->buf_size)
505 radio->rd_index = 0;
506
507 /* increment counters */
508 block_count++;
509 buf += 3;
510 retval += 3;
511 }
Joonyoung Shim1aa925c2009-12-10 16:49:34 -0300512
513done:
514 return retval;
515}
516
517
518/*
519 * si470x_fops_poll - poll RDS data
520 */
521static unsigned int si470x_fops_poll(struct file *file,
522 struct poll_table_struct *pts)
523{
524 struct si470x_device *radio = video_drvdata(file);
Hans Verkuileae63ae2012-05-04 09:20:53 -0300525 unsigned long req_events = poll_requested_events(pts);
526 int retval = v4l2_ctrl_poll(file, pts);
Joonyoung Shim1aa925c2009-12-10 16:49:34 -0300527
Hans Verkuileae63ae2012-05-04 09:20:53 -0300528 if (req_events & (POLLIN | POLLRDNORM)) {
529 /* switch on rds reception */
530 if ((radio->registers[SYSCONFIG1] & SYSCONFIG1_RDS) == 0)
531 si470x_rds_on(radio);
Mauro Carvalho Chehabf2f8e852010-09-15 08:35:45 -0300532
Hans Verkuileae63ae2012-05-04 09:20:53 -0300533 poll_wait(file, &radio->read_queue, pts);
Joonyoung Shim1aa925c2009-12-10 16:49:34 -0300534
Hans Verkuileae63ae2012-05-04 09:20:53 -0300535 if (radio->rd_index != radio->wr_index)
536 retval |= POLLIN | POLLRDNORM;
537 }
Joonyoung Shim1aa925c2009-12-10 16:49:34 -0300538
539 return retval;
540}
541
542
543/*
544 * si470x_fops - file operations interface
545 */
546static const struct v4l2_file_operations si470x_fops = {
547 .owner = THIS_MODULE,
548 .read = si470x_fops_read,
549 .poll = si470x_fops_poll,
Mauro Carvalho Chehabf2f8e852010-09-15 08:35:45 -0300550 .unlocked_ioctl = video_ioctl2,
Joonyoung Shim1aa925c2009-12-10 16:49:34 -0300551 .open = si470x_fops_open,
552 .release = si470x_fops_release,
553};
554
555
556
557/**************************************************************************
Joonyoung Shimb09cd162009-08-09 14:22:20 -0300558 * Video4Linux Interface
559 **************************************************************************/
560
Hans Verkuil4967d532012-05-04 09:16:57 -0300561
562static int si470x_s_ctrl(struct v4l2_ctrl *ctrl)
Joonyoung Shimb09cd162009-08-09 14:22:20 -0300563{
Hans Verkuil4967d532012-05-04 09:16:57 -0300564 struct si470x_device *radio =
565 container_of(ctrl->handler, struct si470x_device, hdl);
Joonyoung Shimb09cd162009-08-09 14:22:20 -0300566
567 switch (ctrl->id) {
568 case V4L2_CID_AUDIO_VOLUME:
569 radio->registers[SYSCONFIG2] &= ~SYSCONFIG2_VOLUME;
Hans Verkuil4967d532012-05-04 09:16:57 -0300570 radio->registers[SYSCONFIG2] |= ctrl->val;
571 return si470x_set_register(radio, SYSCONFIG2);
Joonyoung Shimb09cd162009-08-09 14:22:20 -0300572 case V4L2_CID_AUDIO_MUTE:
Hans Verkuil4967d532012-05-04 09:16:57 -0300573 if (ctrl->val)
Joonyoung Shimb09cd162009-08-09 14:22:20 -0300574 radio->registers[POWERCFG] &= ~POWERCFG_DMUTE;
575 else
576 radio->registers[POWERCFG] |= POWERCFG_DMUTE;
Hans Verkuil4967d532012-05-04 09:16:57 -0300577 return si470x_set_register(radio, POWERCFG);
Joonyoung Shimb09cd162009-08-09 14:22:20 -0300578 break;
579 default:
Hans Verkuil4967d532012-05-04 09:16:57 -0300580 return -EINVAL;
Joonyoung Shimb09cd162009-08-09 14:22:20 -0300581 }
Joonyoung Shimb09cd162009-08-09 14:22:20 -0300582}
583
584
585/*
586 * si470x_vidioc_g_tuner - get tuner attributes
587 */
588static int si470x_vidioc_g_tuner(struct file *file, void *priv,
589 struct v4l2_tuner *tuner)
590{
591 struct si470x_device *radio = video_drvdata(file);
592 int retval = 0;
593
Joonyoung Shimb09cd162009-08-09 14:22:20 -0300594 if (tuner->index != 0) {
595 retval = -EINVAL;
596 goto done;
597 }
598
599 retval = si470x_get_register(radio, STATUSRSSI);
600 if (retval < 0)
601 goto done;
602
603 /* driver constants */
604 strcpy(tuner->name, "FM");
605 tuner->type = V4L2_TUNER_RADIO;
606 tuner->capability = V4L2_TUNER_CAP_LOW | V4L2_TUNER_CAP_STEREO |
Matti Aaltonencb0ed222010-10-18 06:54:14 -0300607 V4L2_TUNER_CAP_RDS | V4L2_TUNER_CAP_RDS_BLOCK_IO;
Joonyoung Shimb09cd162009-08-09 14:22:20 -0300608
609 /* range limits */
610 switch ((radio->registers[SYSCONFIG2] & SYSCONFIG2_BAND) >> 6) {
611 /* 0: 87.5 - 108 MHz (USA, Europe, default) */
612 default:
613 tuner->rangelow = 87.5 * FREQ_MUL;
614 tuner->rangehigh = 108 * FREQ_MUL;
615 break;
616 /* 1: 76 - 108 MHz (Japan wide band) */
617 case 1:
618 tuner->rangelow = 76 * FREQ_MUL;
619 tuner->rangehigh = 108 * FREQ_MUL;
620 break;
621 /* 2: 76 - 90 MHz (Japan) */
622 case 2:
623 tuner->rangelow = 76 * FREQ_MUL;
624 tuner->rangehigh = 90 * FREQ_MUL;
625 break;
626 };
627
628 /* stereo indicator == stereo (instead of mono) */
629 if ((radio->registers[STATUSRSSI] & STATUSRSSI_ST) == 0)
630 tuner->rxsubchans = V4L2_TUNER_SUB_MONO;
631 else
Hans Verkuil4967d532012-05-04 09:16:57 -0300632 tuner->rxsubchans = V4L2_TUNER_SUB_STEREO;
Joonyoung Shimb09cd162009-08-09 14:22:20 -0300633 /* If there is a reliable method of detecting an RDS channel,
634 then this code should check for that before setting this
635 RDS subchannel. */
636 tuner->rxsubchans |= V4L2_TUNER_SUB_RDS;
637
638 /* mono/stereo selector */
639 if ((radio->registers[POWERCFG] & POWERCFG_MONO) == 0)
640 tuner->audmode = V4L2_TUNER_MODE_STEREO;
641 else
642 tuner->audmode = V4L2_TUNER_MODE_MONO;
643
644 /* min is worst, max is best; signal:0..0xffff; rssi: 0..0xff */
Tobias Lorenz144dcdc2010-02-18 16:33:46 -0300645 /* measured in units of dbµV in 1 db increments (max at ~75 dbµV) */
Joonyoung Shimb09cd162009-08-09 14:22:20 -0300646 tuner->signal = (radio->registers[STATUSRSSI] & STATUSRSSI_RSSI);
647 /* the ideal factor is 0xffff/75 = 873,8 */
648 tuner->signal = (tuner->signal * 873) + (8 * tuner->signal / 10);
Hans Verkuileae63ae2012-05-04 09:20:53 -0300649 if (tuner->signal > 0xffff)
650 tuner->signal = 0xffff;
Joonyoung Shimb09cd162009-08-09 14:22:20 -0300651
652 /* automatic frequency control: -1: freq to low, 1 freq to high */
653 /* AFCRL does only indicate that freq. differs, not if too low/high */
654 tuner->afc = (radio->registers[STATUSRSSI] & STATUSRSSI_AFCRL) ? 1 : 0;
655
656done:
657 if (retval < 0)
Hans Verkuil4967d532012-05-04 09:16:57 -0300658 dev_warn(&radio->videodev.dev,
Joonyoung Shima9d6fd52009-08-09 14:22:41 -0300659 "get tuner failed with %d\n", retval);
Joonyoung Shimb09cd162009-08-09 14:22:20 -0300660 return retval;
661}
662
663
664/*
665 * si470x_vidioc_s_tuner - set tuner attributes
666 */
667static int si470x_vidioc_s_tuner(struct file *file, void *priv,
668 struct v4l2_tuner *tuner)
669{
670 struct si470x_device *radio = video_drvdata(file);
Tobias Lorenz73c994e2010-07-10 13:01:33 -0300671 int retval = 0;
Joonyoung Shimb09cd162009-08-09 14:22:20 -0300672
Joonyoung Shimb09cd162009-08-09 14:22:20 -0300673 if (tuner->index != 0)
Hans Verkuileae63ae2012-05-04 09:20:53 -0300674 return -EINVAL;
Joonyoung Shimb09cd162009-08-09 14:22:20 -0300675
676 /* mono/stereo selector */
677 switch (tuner->audmode) {
678 case V4L2_TUNER_MODE_MONO:
679 radio->registers[POWERCFG] |= POWERCFG_MONO; /* force mono */
680 break;
681 case V4L2_TUNER_MODE_STEREO:
Hans Verkuileae63ae2012-05-04 09:20:53 -0300682 default:
Joonyoung Shimb09cd162009-08-09 14:22:20 -0300683 radio->registers[POWERCFG] &= ~POWERCFG_MONO; /* try stereo */
684 break;
Joonyoung Shimb09cd162009-08-09 14:22:20 -0300685 }
686
687 retval = si470x_set_register(radio, POWERCFG);
688
Joonyoung Shimb09cd162009-08-09 14:22:20 -0300689 if (retval < 0)
Hans Verkuil4967d532012-05-04 09:16:57 -0300690 dev_warn(&radio->videodev.dev,
Joonyoung Shima9d6fd52009-08-09 14:22:41 -0300691 "set tuner failed with %d\n", retval);
Joonyoung Shimb09cd162009-08-09 14:22:20 -0300692 return retval;
693}
694
695
696/*
697 * si470x_vidioc_g_frequency - get tuner or modulator radio frequency
698 */
699static int si470x_vidioc_g_frequency(struct file *file, void *priv,
700 struct v4l2_frequency *freq)
701{
702 struct si470x_device *radio = video_drvdata(file);
703 int retval = 0;
704
Joonyoung Shimb09cd162009-08-09 14:22:20 -0300705 if (freq->tuner != 0) {
706 retval = -EINVAL;
707 goto done;
708 }
709
710 freq->type = V4L2_TUNER_RADIO;
711 retval = si470x_get_freq(radio, &freq->frequency);
712
713done:
714 if (retval < 0)
Hans Verkuil4967d532012-05-04 09:16:57 -0300715 dev_warn(&radio->videodev.dev,
Joonyoung Shima9d6fd52009-08-09 14:22:41 -0300716 "get frequency failed with %d\n", retval);
Joonyoung Shimb09cd162009-08-09 14:22:20 -0300717 return retval;
718}
719
720
721/*
722 * si470x_vidioc_s_frequency - set tuner or modulator radio frequency
723 */
724static int si470x_vidioc_s_frequency(struct file *file, void *priv,
725 struct v4l2_frequency *freq)
726{
727 struct si470x_device *radio = video_drvdata(file);
728 int retval = 0;
729
Joonyoung Shimb09cd162009-08-09 14:22:20 -0300730 if (freq->tuner != 0) {
731 retval = -EINVAL;
732 goto done;
733 }
734
735 retval = si470x_set_freq(radio, freq->frequency);
736
737done:
738 if (retval < 0)
Hans Verkuil4967d532012-05-04 09:16:57 -0300739 dev_warn(&radio->videodev.dev,
Joonyoung Shima9d6fd52009-08-09 14:22:41 -0300740 "set frequency failed with %d\n", retval);
Joonyoung Shimb09cd162009-08-09 14:22:20 -0300741 return retval;
742}
743
744
745/*
746 * si470x_vidioc_s_hw_freq_seek - set hardware frequency seek
747 */
748static int si470x_vidioc_s_hw_freq_seek(struct file *file, void *priv,
749 struct v4l2_hw_freq_seek *seek)
750{
751 struct si470x_device *radio = video_drvdata(file);
752 int retval = 0;
753
Joonyoung Shimb09cd162009-08-09 14:22:20 -0300754 if (seek->tuner != 0) {
755 retval = -EINVAL;
756 goto done;
757 }
758
759 retval = si470x_set_seek(radio, seek->wrap_around, seek->seek_upward);
760
761done:
762 if (retval < 0)
Hans Verkuil4967d532012-05-04 09:16:57 -0300763 dev_warn(&radio->videodev.dev,
Joonyoung Shima9d6fd52009-08-09 14:22:41 -0300764 "set hardware frequency seek failed with %d\n", retval);
Joonyoung Shimb09cd162009-08-09 14:22:20 -0300765 return retval;
766}
767
Hans Verkuil4967d532012-05-04 09:16:57 -0300768const struct v4l2_ctrl_ops si470x_ctrl_ops = {
769 .s_ctrl = si470x_s_ctrl,
770};
Joonyoung Shimb09cd162009-08-09 14:22:20 -0300771
772/*
773 * si470x_ioctl_ops - video device ioctl operations
774 */
775static const struct v4l2_ioctl_ops si470x_ioctl_ops = {
776 .vidioc_querycap = si470x_vidioc_querycap,
Joonyoung Shimb09cd162009-08-09 14:22:20 -0300777 .vidioc_g_tuner = si470x_vidioc_g_tuner,
778 .vidioc_s_tuner = si470x_vidioc_s_tuner,
779 .vidioc_g_frequency = si470x_vidioc_g_frequency,
780 .vidioc_s_frequency = si470x_vidioc_s_frequency,
781 .vidioc_s_hw_freq_seek = si470x_vidioc_s_hw_freq_seek,
Hans Verkuileae63ae2012-05-04 09:20:53 -0300782 .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
783 .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
Joonyoung Shimb09cd162009-08-09 14:22:20 -0300784};
785
786
787/*
788 * si470x_viddev_template - video device interface
789 */
790struct video_device si470x_viddev_template = {
791 .fops = &si470x_fops,
792 .name = DRIVER_NAME,
Hans Verkuil4967d532012-05-04 09:16:57 -0300793 .release = video_device_release_empty,
Joonyoung Shimb09cd162009-08-09 14:22:20 -0300794 .ioctl_ops = &si470x_ioctl_ops,
795};