1 /* 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 * Copyright (c) 2012 Hans de Goede <hdegoede@redhat.com> 8 * 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 20 21 /* 22 * History: 23 * 2008-01-12 Tobias Lorenz <tobias.lorenz@gmx.net> 24 * Version 1.0.0 25 * - First working version 26 * 2008-01-13 Tobias Lorenz <tobias.lorenz@gmx.net> 27 * Version 1.0.1 28 * - Improved error handling, every function now returns errno 29 * - Improved multi user access (start/mute/stop) 30 * - Channel doesn't get lost anymore after start/mute/stop 31 * - RDS support added (polling mode via interrupt EP 1) 32 * - marked default module parameters with *value* 33 * - switched from bit structs to bit masks 34 * - header file cleaned and integrated 35 * 2008-01-14 Tobias Lorenz <tobias.lorenz@gmx.net> 36 * Version 1.0.2 37 * - hex values are now lower case 38 * - commented USB ID for ADS/Tech moved on todo list 39 * - blacklisted si470x in hid-quirks.c 40 * - rds buffer handling functions integrated into *_work, *_read 41 * - rds_command in si470x_poll exchanged against simple retval 42 * - check for firmware version 15 43 * - code order and prototypes still remain the same 44 * - spacing and bottom of band codes remain the same 45 * 2008-01-16 Tobias Lorenz <tobias.lorenz@gmx.net> 46 * Version 1.0.3 47 * - code reordered to avoid function prototypes 48 * - switch/case defaults are now more user-friendly 49 * - unified comment style 50 * - applied all checkpatch.pl v1.12 suggestions 51 * except the warning about the too long lines with bit comments 52 * - renamed FMRADIO to RADIO to cut line length (checkpatch.pl) 53 * 2008-01-22 Tobias Lorenz <tobias.lorenz@gmx.net> 54 * Version 1.0.4 55 * - avoid poss. locking when doing copy_to_user which may sleep 56 * - RDS is automatically activated on read now 57 * - code cleaned of unnecessary rds_commands 58 * - USB Vendor/Product ID for ADS/Tech FM Radio Receiver verified 59 * (thanks to Guillaume RAMOUSSE) 60 * 2008-01-27 Tobias Lorenz <tobias.lorenz@gmx.net> 61 * Version 1.0.5 62 * - number of seek_retries changed to tune_timeout 63 * - fixed problem with incomplete tune operations by own buffers 64 * - optimization of variables and printf types 65 * - improved error logging 66 * 2008-01-31 Tobias Lorenz <tobias.lorenz@gmx.net> 67 * Oliver Neukum <oliver@neukum.org> 68 * Version 1.0.6 69 * - fixed coverity checker warnings in *_usb_driver_disconnect 70 * - probe()/open() race by correct ordering in probe() 71 * - DMA coherency rules by separate allocation of all buffers 72 * - use of endianness macros 73 * - abuse of spinlock, replaced by mutex 74 * - racy handling of timer in disconnect, 75 * replaced by delayed_work 76 * - racy interruptible_sleep_on(), 77 * replaced with wait_event_interruptible() 78 * - handle signals in read() 79 * 2008-02-08 Tobias Lorenz <tobias.lorenz@gmx.net> 80 * Oliver Neukum <oliver@neukum.org> 81 * Version 1.0.7 82 * - usb autosuspend support 83 * - unplugging fixed 84 * 2008-05-07 Tobias Lorenz <tobias.lorenz@gmx.net> 85 * Version 1.0.8 86 * - hardware frequency seek support 87 * - afc indication 88 * - more safety checks, let si470x_get_freq return errno 89 * - vidioc behavior corrected according to v4l2 spec 90 * 2008-10-20 Alexey Klimov <klimov.linux@gmail.com> 91 * - add support for KWorld USB FM Radio FM700 92 * - blacklisted KWorld radio in hid-core.c and hid-ids.h 93 * 2008-12-03 Mark Lord <mlord@pobox.com> 94 * - add support for DealExtreme USB Radio 95 * 2009-01-31 Bob Ross <pigiron@gmx.com> 96 * - correction of stereo detection/setting 97 * - correction of signal strength indicator scaling 98 * 2009-01-31 Rick Bronson <rick@efn.org> 99 * Tobias Lorenz <tobias.lorenz@gmx.net> 100 * - add LED status output 101 * - get HW/SW version from scratchpad 102 * 2009-06-16 Edouard Lafargue <edouard@lafargue.name> 103 * Version 1.0.10 104 * - add support for interrupt mode for RDS endpoint, 105 * instead of polling. 106 * Improves RDS reception significantly 107 */ 108 109 110 /* kernel includes */ 111 #include "radio-si470x.h" 112 113 /************************************************************************** 114 * Module Parameters 115 **************************************************************************/ 116 117 /* Spacing (kHz) */ 118 /* 0: 200 kHz (USA, Australia) */ 119 /* 1: 100 kHz (Europe, Japan) */ 120 /* 2: 50 kHz */ 121 static unsigned short space = 2; 122 module_param(space, ushort, 0444); 123 MODULE_PARM_DESC(space, "Spacing: 0=200kHz 1=100kHz *2=50kHz*"); 124 125 /* De-emphasis */ 126 /* 0: 75 us (USA) */ 127 /* 1: 50 us (Europe, Australia, Japan) */ 128 static unsigned short de = 1; 129 module_param(de, ushort, 0444); 130 MODULE_PARM_DESC(de, "De-emphasis: 0=75us *1=50us*"); 131 132 /* Tune timeout */ 133 static unsigned int tune_timeout = 3000; 134 module_param(tune_timeout, uint, 0644); 135 MODULE_PARM_DESC(tune_timeout, "Tune timeout: *3000*"); 136 137 /* Seek timeout */ 138 static unsigned int seek_timeout = 5000; 139 module_param(seek_timeout, uint, 0644); 140 MODULE_PARM_DESC(seek_timeout, "Seek timeout: *5000*"); 141 142 static const struct v4l2_frequency_band bands[] = { 143 { 144 .type = V4L2_TUNER_RADIO, 145 .index = 0, 146 .capability = V4L2_TUNER_CAP_LOW | V4L2_TUNER_CAP_STEREO | 147 V4L2_TUNER_CAP_RDS | V4L2_TUNER_CAP_RDS_BLOCK_IO | 148 V4L2_TUNER_CAP_FREQ_BANDS | 149 V4L2_TUNER_CAP_HWSEEK_BOUNDED | 150 V4L2_TUNER_CAP_HWSEEK_WRAP, 151 .rangelow = 87500 * 16, 152 .rangehigh = 108000 * 16, 153 .modulation = V4L2_BAND_MODULATION_FM, 154 }, 155 { 156 .type = V4L2_TUNER_RADIO, 157 .index = 1, 158 .capability = V4L2_TUNER_CAP_LOW | V4L2_TUNER_CAP_STEREO | 159 V4L2_TUNER_CAP_RDS | V4L2_TUNER_CAP_RDS_BLOCK_IO | 160 V4L2_TUNER_CAP_FREQ_BANDS | 161 V4L2_TUNER_CAP_HWSEEK_BOUNDED | 162 V4L2_TUNER_CAP_HWSEEK_WRAP, 163 .rangelow = 76000 * 16, 164 .rangehigh = 108000 * 16, 165 .modulation = V4L2_BAND_MODULATION_FM, 166 }, 167 { 168 .type = V4L2_TUNER_RADIO, 169 .index = 2, 170 .capability = V4L2_TUNER_CAP_LOW | V4L2_TUNER_CAP_STEREO | 171 V4L2_TUNER_CAP_RDS | V4L2_TUNER_CAP_RDS_BLOCK_IO | 172 V4L2_TUNER_CAP_FREQ_BANDS | 173 V4L2_TUNER_CAP_HWSEEK_BOUNDED | 174 V4L2_TUNER_CAP_HWSEEK_WRAP, 175 .rangelow = 76000 * 16, 176 .rangehigh = 90000 * 16, 177 .modulation = V4L2_BAND_MODULATION_FM, 178 }, 179 }; 180 181 /************************************************************************** 182 * Generic Functions 183 **************************************************************************/ 184 185 /* 186 * si470x_set_band - set the band 187 */ 188 static int si470x_set_band(struct si470x_device *radio, int band) 189 { 190 if (radio->band == band) 191 return 0; 192 193 radio->band = band; 194 radio->registers[SYSCONFIG2] &= ~SYSCONFIG2_BAND; 195 radio->registers[SYSCONFIG2] |= radio->band << 6; 196 return radio->set_register(radio, SYSCONFIG2); 197 } 198 199 /* 200 * si470x_set_chan - set the channel 201 */ 202 static int si470x_set_chan(struct si470x_device *radio, unsigned short chan) 203 { 204 int retval; 205 unsigned long time_left; 206 bool timed_out = false; 207 208 retval = radio->get_register(radio, POWERCFG); 209 if (retval) 210 return retval; 211 212 if ((radio->registers[POWERCFG] & (POWERCFG_ENABLE|POWERCFG_DMUTE)) 213 != (POWERCFG_ENABLE|POWERCFG_DMUTE)) { 214 return 0; 215 } 216 217 /* start tuning */ 218 radio->registers[CHANNEL] &= ~CHANNEL_CHAN; 219 radio->registers[CHANNEL] |= CHANNEL_TUNE | chan; 220 retval = radio->set_register(radio, CHANNEL); 221 if (retval < 0) 222 goto done; 223 224 /* wait till tune operation has completed */ 225 reinit_completion(&radio->completion); 226 time_left = wait_for_completion_timeout(&radio->completion, 227 msecs_to_jiffies(tune_timeout)); 228 if (time_left == 0) 229 timed_out = true; 230 231 if ((radio->registers[STATUSRSSI] & STATUSRSSI_STC) == 0) 232 dev_warn(&radio->videodev.dev, "tune does not complete\n"); 233 if (timed_out) 234 dev_warn(&radio->videodev.dev, 235 "tune timed out after %u ms\n", tune_timeout); 236 237 /* stop tuning */ 238 radio->registers[CHANNEL] &= ~CHANNEL_TUNE; 239 retval = radio->set_register(radio, CHANNEL); 240 241 done: 242 return retval; 243 } 244 245 /* 246 * si470x_get_step - get channel spacing 247 */ 248 static unsigned int si470x_get_step(struct si470x_device *radio) 249 { 250 /* Spacing (kHz) */ 251 switch ((radio->registers[SYSCONFIG2] & SYSCONFIG2_SPACE) >> 4) { 252 /* 0: 200 kHz (USA, Australia) */ 253 case 0: 254 return 200 * 16; 255 /* 1: 100 kHz (Europe, Japan) */ 256 case 1: 257 return 100 * 16; 258 /* 2: 50 kHz */ 259 default: 260 return 50 * 16; 261 } 262 } 263 264 265 /* 266 * si470x_get_freq - get the frequency 267 */ 268 static int si470x_get_freq(struct si470x_device *radio, unsigned int *freq) 269 { 270 int chan, retval; 271 272 /* read channel */ 273 retval = radio->get_register(radio, READCHAN); 274 chan = radio->registers[READCHAN] & READCHAN_READCHAN; 275 276 /* Frequency (MHz) = Spacing (kHz) x Channel + Bottom of Band (MHz) */ 277 *freq = chan * si470x_get_step(radio) + bands[radio->band].rangelow; 278 279 return retval; 280 } 281 282 283 /* 284 * si470x_set_freq - set the frequency 285 */ 286 int si470x_set_freq(struct si470x_device *radio, unsigned int freq) 287 { 288 unsigned short chan; 289 290 freq = clamp(freq, bands[radio->band].rangelow, 291 bands[radio->band].rangehigh); 292 /* Chan = [ Freq (Mhz) - Bottom of Band (MHz) ] / Spacing (kHz) */ 293 chan = (freq - bands[radio->band].rangelow) / si470x_get_step(radio); 294 295 return si470x_set_chan(radio, chan); 296 } 297 EXPORT_SYMBOL_GPL(si470x_set_freq); 298 299 300 /* 301 * si470x_set_seek - set seek 302 */ 303 static int si470x_set_seek(struct si470x_device *radio, 304 const struct v4l2_hw_freq_seek *seek) 305 { 306 int band, retval; 307 unsigned int freq; 308 bool timed_out = false; 309 unsigned long time_left; 310 311 /* set band */ 312 if (seek->rangelow || seek->rangehigh) { 313 for (band = 0; band < ARRAY_SIZE(bands); band++) { 314 if (bands[band].rangelow == seek->rangelow && 315 bands[band].rangehigh == seek->rangehigh) 316 break; 317 } 318 if (band == ARRAY_SIZE(bands)) 319 return -EINVAL; /* No matching band found */ 320 } else 321 band = 1; /* If nothing is specified seek 76 - 108 Mhz */ 322 323 if (radio->band != band) { 324 retval = si470x_get_freq(radio, &freq); 325 if (retval) 326 return retval; 327 retval = si470x_set_band(radio, band); 328 if (retval) 329 return retval; 330 retval = si470x_set_freq(radio, freq); 331 if (retval) 332 return retval; 333 } 334 335 /* start seeking */ 336 radio->registers[POWERCFG] |= POWERCFG_SEEK; 337 if (seek->wrap_around) 338 radio->registers[POWERCFG] &= ~POWERCFG_SKMODE; 339 else 340 radio->registers[POWERCFG] |= POWERCFG_SKMODE; 341 if (seek->seek_upward) 342 radio->registers[POWERCFG] |= POWERCFG_SEEKUP; 343 else 344 radio->registers[POWERCFG] &= ~POWERCFG_SEEKUP; 345 retval = radio->set_register(radio, POWERCFG); 346 if (retval < 0) 347 return retval; 348 349 /* wait till tune operation has completed */ 350 reinit_completion(&radio->completion); 351 time_left = wait_for_completion_timeout(&radio->completion, 352 msecs_to_jiffies(seek_timeout)); 353 if (time_left == 0) 354 timed_out = true; 355 356 if ((radio->registers[STATUSRSSI] & STATUSRSSI_STC) == 0) 357 dev_warn(&radio->videodev.dev, "seek does not complete\n"); 358 if (radio->registers[STATUSRSSI] & STATUSRSSI_SF) 359 dev_warn(&radio->videodev.dev, 360 "seek failed / band limit reached\n"); 361 362 /* stop seeking */ 363 radio->registers[POWERCFG] &= ~POWERCFG_SEEK; 364 retval = radio->set_register(radio, POWERCFG); 365 366 /* try again, if timed out */ 367 if (retval == 0 && timed_out) 368 return -ENODATA; 369 return retval; 370 } 371 372 373 /* 374 * si470x_start - switch on radio 375 */ 376 int si470x_start(struct si470x_device *radio) 377 { 378 int retval; 379 380 /* powercfg */ 381 radio->registers[POWERCFG] = 382 POWERCFG_DMUTE | POWERCFG_ENABLE | POWERCFG_RDSM; 383 retval = radio->set_register(radio, POWERCFG); 384 if (retval < 0) 385 goto done; 386 387 /* sysconfig 1 */ 388 radio->registers[SYSCONFIG1] |= SYSCONFIG1_RDSIEN | SYSCONFIG1_STCIEN | 389 SYSCONFIG1_RDS; 390 radio->registers[SYSCONFIG1] &= ~SYSCONFIG1_GPIO2; 391 radio->registers[SYSCONFIG1] |= SYSCONFIG1_GPIO2_INT; 392 if (de) 393 radio->registers[SYSCONFIG1] |= SYSCONFIG1_DE; 394 retval = radio->set_register(radio, SYSCONFIG1); 395 if (retval < 0) 396 goto done; 397 398 /* sysconfig 2 */ 399 radio->registers[SYSCONFIG2] = 400 (0x1f << 8) | /* SEEKTH */ 401 ((radio->band << 6) & SYSCONFIG2_BAND) |/* BAND */ 402 ((space << 4) & SYSCONFIG2_SPACE) | /* SPACE */ 403 15; /* VOLUME (max) */ 404 retval = radio->set_register(radio, SYSCONFIG2); 405 if (retval < 0) 406 goto done; 407 408 /* reset last channel */ 409 retval = si470x_set_chan(radio, 410 radio->registers[CHANNEL] & CHANNEL_CHAN); 411 412 done: 413 return retval; 414 } 415 EXPORT_SYMBOL_GPL(si470x_start); 416 417 418 /* 419 * si470x_stop - switch off radio 420 */ 421 int si470x_stop(struct si470x_device *radio) 422 { 423 int retval; 424 425 /* sysconfig 1 */ 426 radio->registers[SYSCONFIG1] &= ~SYSCONFIG1_RDS; 427 retval = radio->set_register(radio, SYSCONFIG1); 428 if (retval < 0) 429 goto done; 430 431 /* powercfg */ 432 radio->registers[POWERCFG] &= ~POWERCFG_DMUTE; 433 /* POWERCFG_ENABLE has to automatically go low */ 434 radio->registers[POWERCFG] |= POWERCFG_ENABLE | POWERCFG_DISABLE; 435 retval = radio->set_register(radio, POWERCFG); 436 437 done: 438 return retval; 439 } 440 EXPORT_SYMBOL_GPL(si470x_stop); 441 442 443 /* 444 * si470x_rds_on - switch on rds reception 445 */ 446 static int si470x_rds_on(struct si470x_device *radio) 447 { 448 int retval; 449 450 /* sysconfig 1 */ 451 radio->registers[SYSCONFIG1] |= SYSCONFIG1_RDS; 452 retval = radio->set_register(radio, SYSCONFIG1); 453 if (retval < 0) 454 radio->registers[SYSCONFIG1] &= ~SYSCONFIG1_RDS; 455 456 return retval; 457 } 458 459 460 461 /************************************************************************** 462 * File Operations Interface 463 **************************************************************************/ 464 465 /* 466 * si470x_fops_read - read RDS data 467 */ 468 static ssize_t si470x_fops_read(struct file *file, char __user *buf, 469 size_t count, loff_t *ppos) 470 { 471 struct si470x_device *radio = video_drvdata(file); 472 int retval = 0; 473 unsigned int block_count = 0; 474 475 /* switch on rds reception */ 476 if ((radio->registers[SYSCONFIG1] & SYSCONFIG1_RDS) == 0) 477 si470x_rds_on(radio); 478 479 /* block if no new data available */ 480 while (radio->wr_index == radio->rd_index) { 481 if (file->f_flags & O_NONBLOCK) { 482 retval = -EWOULDBLOCK; 483 goto done; 484 } 485 if (wait_event_interruptible(radio->read_queue, 486 radio->wr_index != radio->rd_index) < 0) { 487 retval = -EINTR; 488 goto done; 489 } 490 } 491 492 /* calculate block count from byte count */ 493 count /= 3; 494 495 /* copy RDS block out of internal buffer and to user buffer */ 496 while (block_count < count) { 497 if (radio->rd_index == radio->wr_index) 498 break; 499 500 /* always transfer rds complete blocks */ 501 if (copy_to_user(buf, &radio->buffer[radio->rd_index], 3)) 502 /* retval = -EFAULT; */ 503 break; 504 505 /* increment and wrap read pointer */ 506 radio->rd_index += 3; 507 if (radio->rd_index >= radio->buf_size) 508 radio->rd_index = 0; 509 510 /* increment counters */ 511 block_count++; 512 buf += 3; 513 retval += 3; 514 } 515 516 done: 517 return retval; 518 } 519 520 521 /* 522 * si470x_fops_poll - poll RDS data 523 */ 524 static __poll_t si470x_fops_poll(struct file *file, 525 struct poll_table_struct *pts) 526 { 527 struct si470x_device *radio = video_drvdata(file); 528 __poll_t req_events = poll_requested_events(pts); 529 __poll_t retval = v4l2_ctrl_poll(file, pts); 530 531 if (req_events & (EPOLLIN | EPOLLRDNORM)) { 532 /* switch on rds reception */ 533 if ((radio->registers[SYSCONFIG1] & SYSCONFIG1_RDS) == 0) 534 si470x_rds_on(radio); 535 536 poll_wait(file, &radio->read_queue, pts); 537 538 if (radio->rd_index != radio->wr_index) 539 retval |= EPOLLIN | EPOLLRDNORM; 540 } 541 542 return retval; 543 } 544 545 546 static int si470x_fops_open(struct file *file) 547 { 548 struct si470x_device *radio = video_drvdata(file); 549 550 return radio->fops_open(file); 551 } 552 553 554 /* 555 * si470x_fops_release - file release 556 */ 557 static int si470x_fops_release(struct file *file) 558 { 559 struct si470x_device *radio = video_drvdata(file); 560 561 return radio->fops_release(file); 562 } 563 564 565 /* 566 * si470x_fops - file operations interface 567 */ 568 static const struct v4l2_file_operations si470x_fops = { 569 .owner = THIS_MODULE, 570 .read = si470x_fops_read, 571 .poll = si470x_fops_poll, 572 .unlocked_ioctl = video_ioctl2, 573 .open = si470x_fops_open, 574 .release = si470x_fops_release, 575 }; 576 577 578 579 /************************************************************************** 580 * Video4Linux Interface 581 **************************************************************************/ 582 583 584 static int si470x_s_ctrl(struct v4l2_ctrl *ctrl) 585 { 586 struct si470x_device *radio = 587 container_of(ctrl->handler, struct si470x_device, hdl); 588 589 switch (ctrl->id) { 590 case V4L2_CID_AUDIO_VOLUME: 591 radio->registers[SYSCONFIG2] &= ~SYSCONFIG2_VOLUME; 592 radio->registers[SYSCONFIG2] |= ctrl->val; 593 return radio->set_register(radio, SYSCONFIG2); 594 case V4L2_CID_AUDIO_MUTE: 595 if (ctrl->val) 596 radio->registers[POWERCFG] &= ~POWERCFG_DMUTE; 597 else 598 radio->registers[POWERCFG] |= POWERCFG_DMUTE; 599 return radio->set_register(radio, POWERCFG); 600 default: 601 return -EINVAL; 602 } 603 } 604 605 606 /* 607 * si470x_vidioc_g_tuner - get tuner attributes 608 */ 609 static int si470x_vidioc_g_tuner(struct file *file, void *priv, 610 struct v4l2_tuner *tuner) 611 { 612 struct si470x_device *radio = video_drvdata(file); 613 int retval = 0; 614 615 if (tuner->index != 0) 616 return -EINVAL; 617 618 if (!radio->status_rssi_auto_update) { 619 retval = radio->get_register(radio, STATUSRSSI); 620 if (retval < 0) 621 return retval; 622 } 623 624 /* driver constants */ 625 strcpy(tuner->name, "FM"); 626 tuner->type = V4L2_TUNER_RADIO; 627 tuner->capability = V4L2_TUNER_CAP_LOW | V4L2_TUNER_CAP_STEREO | 628 V4L2_TUNER_CAP_RDS | V4L2_TUNER_CAP_RDS_BLOCK_IO | 629 V4L2_TUNER_CAP_HWSEEK_BOUNDED | 630 V4L2_TUNER_CAP_HWSEEK_WRAP; 631 tuner->rangelow = 76 * FREQ_MUL; 632 tuner->rangehigh = 108 * FREQ_MUL; 633 634 /* stereo indicator == stereo (instead of mono) */ 635 if ((radio->registers[STATUSRSSI] & STATUSRSSI_ST) == 0) 636 tuner->rxsubchans = V4L2_TUNER_SUB_MONO; 637 else 638 tuner->rxsubchans = V4L2_TUNER_SUB_STEREO; 639 /* If there is a reliable method of detecting an RDS channel, 640 then this code should check for that before setting this 641 RDS subchannel. */ 642 tuner->rxsubchans |= V4L2_TUNER_SUB_RDS; 643 644 /* mono/stereo selector */ 645 if ((radio->registers[POWERCFG] & POWERCFG_MONO) == 0) 646 tuner->audmode = V4L2_TUNER_MODE_STEREO; 647 else 648 tuner->audmode = V4L2_TUNER_MODE_MONO; 649 650 /* min is worst, max is best; signal:0..0xffff; rssi: 0..0xff */ 651 /* measured in units of dbµV in 1 db increments (max at ~75 dbµV) */ 652 tuner->signal = (radio->registers[STATUSRSSI] & STATUSRSSI_RSSI); 653 /* the ideal factor is 0xffff/75 = 873,8 */ 654 tuner->signal = (tuner->signal * 873) + (8 * tuner->signal / 10); 655 if (tuner->signal > 0xffff) 656 tuner->signal = 0xffff; 657 658 /* automatic frequency control: -1: freq to low, 1 freq to high */ 659 /* AFCRL does only indicate that freq. differs, not if too low/high */ 660 tuner->afc = (radio->registers[STATUSRSSI] & STATUSRSSI_AFCRL) ? 1 : 0; 661 662 return retval; 663 } 664 665 666 /* 667 * si470x_vidioc_s_tuner - set tuner attributes 668 */ 669 static int si470x_vidioc_s_tuner(struct file *file, void *priv, 670 const struct v4l2_tuner *tuner) 671 { 672 struct si470x_device *radio = video_drvdata(file); 673 674 if (tuner->index != 0) 675 return -EINVAL; 676 677 /* mono/stereo selector */ 678 switch (tuner->audmode) { 679 case V4L2_TUNER_MODE_MONO: 680 radio->registers[POWERCFG] |= POWERCFG_MONO; /* force mono */ 681 break; 682 case V4L2_TUNER_MODE_STEREO: 683 default: 684 radio->registers[POWERCFG] &= ~POWERCFG_MONO; /* try stereo */ 685 break; 686 } 687 688 return radio->set_register(radio, POWERCFG); 689 } 690 691 692 /* 693 * si470x_vidioc_g_frequency - get tuner or modulator radio frequency 694 */ 695 static int si470x_vidioc_g_frequency(struct file *file, void *priv, 696 struct v4l2_frequency *freq) 697 { 698 struct si470x_device *radio = video_drvdata(file); 699 700 if (freq->tuner != 0) 701 return -EINVAL; 702 703 freq->type = V4L2_TUNER_RADIO; 704 return si470x_get_freq(radio, &freq->frequency); 705 } 706 707 708 /* 709 * si470x_vidioc_s_frequency - set tuner or modulator radio frequency 710 */ 711 static int si470x_vidioc_s_frequency(struct file *file, void *priv, 712 const struct v4l2_frequency *freq) 713 { 714 struct si470x_device *radio = video_drvdata(file); 715 int retval; 716 717 if (freq->tuner != 0) 718 return -EINVAL; 719 720 if (freq->frequency < bands[radio->band].rangelow || 721 freq->frequency > bands[radio->band].rangehigh) { 722 /* Switch to band 1 which covers everything we support */ 723 retval = si470x_set_band(radio, 1); 724 if (retval) 725 return retval; 726 } 727 return si470x_set_freq(radio, freq->frequency); 728 } 729 730 731 /* 732 * si470x_vidioc_s_hw_freq_seek - set hardware frequency seek 733 */ 734 static int si470x_vidioc_s_hw_freq_seek(struct file *file, void *priv, 735 const struct v4l2_hw_freq_seek *seek) 736 { 737 struct si470x_device *radio = video_drvdata(file); 738 739 if (seek->tuner != 0) 740 return -EINVAL; 741 742 if (file->f_flags & O_NONBLOCK) 743 return -EWOULDBLOCK; 744 745 return si470x_set_seek(radio, seek); 746 } 747 748 /* 749 * si470x_vidioc_enum_freq_bands - enumerate supported bands 750 */ 751 static int si470x_vidioc_enum_freq_bands(struct file *file, void *priv, 752 struct v4l2_frequency_band *band) 753 { 754 if (band->tuner != 0) 755 return -EINVAL; 756 if (band->index >= ARRAY_SIZE(bands)) 757 return -EINVAL; 758 *band = bands[band->index]; 759 return 0; 760 } 761 762 const struct v4l2_ctrl_ops si470x_ctrl_ops = { 763 .s_ctrl = si470x_s_ctrl, 764 }; 765 EXPORT_SYMBOL_GPL(si470x_ctrl_ops); 766 767 static int si470x_vidioc_querycap(struct file *file, void *priv, 768 struct v4l2_capability *capability) 769 { 770 struct si470x_device *radio = video_drvdata(file); 771 772 return radio->vidioc_querycap(file, priv, capability); 773 }; 774 775 /* 776 * si470x_ioctl_ops - video device ioctl operations 777 */ 778 static const struct v4l2_ioctl_ops si470x_ioctl_ops = { 779 .vidioc_querycap = si470x_vidioc_querycap, 780 .vidioc_g_tuner = si470x_vidioc_g_tuner, 781 .vidioc_s_tuner = si470x_vidioc_s_tuner, 782 .vidioc_g_frequency = si470x_vidioc_g_frequency, 783 .vidioc_s_frequency = si470x_vidioc_s_frequency, 784 .vidioc_s_hw_freq_seek = si470x_vidioc_s_hw_freq_seek, 785 .vidioc_enum_freq_bands = si470x_vidioc_enum_freq_bands, 786 .vidioc_subscribe_event = v4l2_ctrl_subscribe_event, 787 .vidioc_unsubscribe_event = v4l2_event_unsubscribe, 788 }; 789 790 791 /* 792 * si470x_viddev_template - video device interface 793 */ 794 const struct video_device si470x_viddev_template = { 795 .fops = &si470x_fops, 796 .name = DRIVER_NAME, 797 .release = video_device_release_empty, 798 .ioctl_ops = &si470x_ioctl_ops, 799 }; 800 EXPORT_SYMBOL_GPL(si470x_viddev_template); 801 802 MODULE_LICENSE("GPL"); 803