1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * Driver for the Texas Instruments WL1273 FM radio. 4 * 5 * Copyright (C) 2011 Nokia Corporation 6 * Author: Matti J. Aaltonen <matti.j.aaltonen@nokia.com> 7 */ 8 9 #include <linux/delay.h> 10 #include <linux/firmware.h> 11 #include <linux/interrupt.h> 12 #include <linux/mfd/wl1273-core.h> 13 #include <linux/slab.h> 14 #include <linux/module.h> 15 #include <media/v4l2-common.h> 16 #include <media/v4l2-ctrls.h> 17 #include <media/v4l2-device.h> 18 #include <media/v4l2-ioctl.h> 19 20 #define DRIVER_DESC "Wl1273 FM Radio" 21 22 #define WL1273_POWER_SET_OFF 0 23 #define WL1273_POWER_SET_FM BIT(0) 24 #define WL1273_POWER_SET_RDS BIT(1) 25 #define WL1273_POWER_SET_RETENTION BIT(4) 26 27 #define WL1273_PUPD_SET_OFF 0x00 28 #define WL1273_PUPD_SET_ON 0x01 29 #define WL1273_PUPD_SET_RETENTION 0x10 30 31 #define WL1273_FREQ(x) (x * 10000 / 625) 32 #define WL1273_INV_FREQ(x) (x * 625 / 10000) 33 34 /* 35 * static int radio_nr - The number of the radio device 36 * 37 * The default is 0. 38 */ 39 static int radio_nr; 40 module_param(radio_nr, int, 0); 41 MODULE_PARM_DESC(radio_nr, "The number of the radio device. Default = 0"); 42 43 struct wl1273_device { 44 char *bus_type; 45 46 u8 forbidden; 47 unsigned int preemphasis; 48 unsigned int spacing; 49 unsigned int tx_power; 50 unsigned int rx_frequency; 51 unsigned int tx_frequency; 52 unsigned int rangelow; 53 unsigned int rangehigh; 54 unsigned int band; 55 bool stereo; 56 57 /* RDS */ 58 unsigned int rds_on; 59 60 wait_queue_head_t read_queue; 61 struct mutex lock; /* for serializing fm radio operations */ 62 struct completion busy; 63 64 unsigned char *buffer; 65 unsigned int buf_size; 66 unsigned int rd_index; 67 unsigned int wr_index; 68 69 /* Selected interrupts */ 70 u16 irq_flags; 71 u16 irq_received; 72 73 struct v4l2_ctrl_handler ctrl_handler; 74 struct v4l2_device v4l2dev; 75 struct video_device videodev; 76 struct device *dev; 77 struct wl1273_core *core; 78 struct file *owner; 79 char *write_buf; 80 unsigned int rds_users; 81 }; 82 83 #define WL1273_IRQ_MASK (WL1273_FR_EVENT | \ 84 WL1273_POW_ENB_EVENT) 85 86 /* 87 * static unsigned int rds_buf - the number of RDS buffer blocks used. 88 * 89 * The default number is 100. 90 */ 91 static unsigned int rds_buf = 100; 92 module_param(rds_buf, uint, 0); 93 MODULE_PARM_DESC(rds_buf, "Number of RDS buffer entries. Default = 100"); 94 95 static int wl1273_fm_write_fw(struct wl1273_core *core, 96 __u8 *fw, int len) 97 { 98 struct i2c_client *client = core->client; 99 struct i2c_msg msg; 100 int i, r = 0; 101 102 msg.addr = client->addr; 103 msg.flags = 0; 104 105 for (i = 0; i <= len; i++) { 106 msg.len = fw[0]; 107 msg.buf = fw + 1; 108 109 fw += msg.len + 1; 110 dev_dbg(&client->dev, "%s:len[%d]: %d\n", __func__, i, msg.len); 111 112 r = i2c_transfer(client->adapter, &msg, 1); 113 if (r < 0 && i < len + 1) 114 break; 115 } 116 117 dev_dbg(&client->dev, "%s: i: %d\n", __func__, i); 118 dev_dbg(&client->dev, "%s: len + 1: %d\n", __func__, len + 1); 119 120 /* Last transfer always fails. */ 121 if (i == len || r == 1) 122 r = 0; 123 124 return r; 125 } 126 127 #define WL1273_FIFO_HAS_DATA(status) (1 << 5 & status) 128 #define WL1273_RDS_CORRECTABLE_ERROR (1 << 3) 129 #define WL1273_RDS_UNCORRECTABLE_ERROR (1 << 4) 130 131 static int wl1273_fm_rds(struct wl1273_device *radio) 132 { 133 struct wl1273_core *core = radio->core; 134 struct i2c_client *client = core->client; 135 u16 val; 136 u8 b0 = WL1273_RDS_DATA_GET, status; 137 struct v4l2_rds_data rds = { 0, 0, 0 }; 138 struct i2c_msg msg[] = { 139 { 140 .addr = client->addr, 141 .flags = 0, 142 .buf = &b0, 143 .len = 1, 144 }, 145 { 146 .addr = client->addr, 147 .flags = I2C_M_RD, 148 .buf = (u8 *) &rds, 149 .len = sizeof(rds), 150 } 151 }; 152 int r; 153 154 if (core->mode != WL1273_MODE_RX) 155 return 0; 156 157 r = core->read(core, WL1273_RDS_SYNC_GET, &val); 158 if (r) 159 return r; 160 161 if ((val & 0x01) == 0) { 162 /* RDS decoder not synchronized */ 163 return -EAGAIN; 164 } 165 166 /* copy all four RDS blocks to internal buffer */ 167 do { 168 r = i2c_transfer(client->adapter, msg, ARRAY_SIZE(msg)); 169 if (r != ARRAY_SIZE(msg)) { 170 dev_err(radio->dev, WL1273_FM_DRIVER_NAME 171 ": %s: read_rds error r == %i)\n", 172 __func__, r); 173 } 174 175 status = rds.block; 176 177 if (!WL1273_FIFO_HAS_DATA(status)) 178 break; 179 180 /* copy bits 0-2 (the block ID) to bits 3-5 */ 181 rds.block = V4L2_RDS_BLOCK_MSK & status; 182 rds.block |= rds.block << 3; 183 184 /* copy the error bits to standard positions */ 185 if (WL1273_RDS_UNCORRECTABLE_ERROR & status) { 186 rds.block |= V4L2_RDS_BLOCK_ERROR; 187 rds.block &= ~V4L2_RDS_BLOCK_CORRECTED; 188 } else if (WL1273_RDS_CORRECTABLE_ERROR & status) { 189 rds.block &= ~V4L2_RDS_BLOCK_ERROR; 190 rds.block |= V4L2_RDS_BLOCK_CORRECTED; 191 } 192 193 /* copy RDS block to internal buffer */ 194 memcpy(&radio->buffer[radio->wr_index], &rds, RDS_BLOCK_SIZE); 195 radio->wr_index += 3; 196 197 /* wrap write pointer */ 198 if (radio->wr_index >= radio->buf_size) 199 radio->wr_index = 0; 200 201 /* check for overflow & start over */ 202 if (radio->wr_index == radio->rd_index) { 203 dev_dbg(radio->dev, "RDS OVERFLOW"); 204 205 radio->rd_index = 0; 206 radio->wr_index = 0; 207 break; 208 } 209 } while (WL1273_FIFO_HAS_DATA(status)); 210 211 /* wake up read queue */ 212 if (radio->wr_index != radio->rd_index) 213 wake_up_interruptible(&radio->read_queue); 214 215 return 0; 216 } 217 218 static irqreturn_t wl1273_fm_irq_thread_handler(int irq, void *dev_id) 219 { 220 struct wl1273_device *radio = dev_id; 221 struct wl1273_core *core = radio->core; 222 u16 flags; 223 int r; 224 225 r = core->read(core, WL1273_FLAG_GET, &flags); 226 if (r) 227 goto out; 228 229 if (flags & WL1273_BL_EVENT) { 230 radio->irq_received = flags; 231 dev_dbg(radio->dev, "IRQ: BL\n"); 232 } 233 234 if (flags & WL1273_RDS_EVENT) { 235 msleep(200); 236 237 wl1273_fm_rds(radio); 238 } 239 240 if (flags & WL1273_BBLK_EVENT) 241 dev_dbg(radio->dev, "IRQ: BBLK\n"); 242 243 if (flags & WL1273_LSYNC_EVENT) 244 dev_dbg(radio->dev, "IRQ: LSYNC\n"); 245 246 if (flags & WL1273_LEV_EVENT) { 247 u16 level; 248 249 r = core->read(core, WL1273_RSSI_LVL_GET, &level); 250 if (r) 251 goto out; 252 253 if (level > 14) 254 dev_dbg(radio->dev, "IRQ: LEV: 0x%x04\n", level); 255 } 256 257 if (flags & WL1273_IFFR_EVENT) 258 dev_dbg(radio->dev, "IRQ: IFFR\n"); 259 260 if (flags & WL1273_PI_EVENT) 261 dev_dbg(radio->dev, "IRQ: PI\n"); 262 263 if (flags & WL1273_PD_EVENT) 264 dev_dbg(radio->dev, "IRQ: PD\n"); 265 266 if (flags & WL1273_STIC_EVENT) 267 dev_dbg(radio->dev, "IRQ: STIC\n"); 268 269 if (flags & WL1273_MAL_EVENT) 270 dev_dbg(radio->dev, "IRQ: MAL\n"); 271 272 if (flags & WL1273_POW_ENB_EVENT) { 273 complete(&radio->busy); 274 dev_dbg(radio->dev, "NOT BUSY\n"); 275 dev_dbg(radio->dev, "IRQ: POW_ENB\n"); 276 } 277 278 if (flags & WL1273_SCAN_OVER_EVENT) 279 dev_dbg(radio->dev, "IRQ: SCAN_OVER\n"); 280 281 if (flags & WL1273_ERROR_EVENT) 282 dev_dbg(radio->dev, "IRQ: ERROR\n"); 283 284 if (flags & WL1273_FR_EVENT) { 285 u16 freq; 286 287 dev_dbg(radio->dev, "IRQ: FR:\n"); 288 289 if (core->mode == WL1273_MODE_RX) { 290 r = core->write(core, WL1273_TUNER_MODE_SET, 291 TUNER_MODE_STOP_SEARCH); 292 if (r) { 293 dev_err(radio->dev, 294 "%s: TUNER_MODE_SET fails: %d\n", 295 __func__, r); 296 goto out; 297 } 298 299 r = core->read(core, WL1273_FREQ_SET, &freq); 300 if (r) 301 goto out; 302 303 if (radio->band == WL1273_BAND_JAPAN) 304 radio->rx_frequency = WL1273_BAND_JAPAN_LOW + 305 freq * 50; 306 else 307 radio->rx_frequency = WL1273_BAND_OTHER_LOW + 308 freq * 50; 309 /* 310 * The driver works better with this msleep, 311 * the documentation doesn't mention it. 312 */ 313 usleep_range(10000, 15000); 314 315 dev_dbg(radio->dev, "%dkHz\n", radio->rx_frequency); 316 317 } else { 318 r = core->read(core, WL1273_CHANL_SET, &freq); 319 if (r) 320 goto out; 321 322 dev_dbg(radio->dev, "%dkHz\n", freq); 323 } 324 dev_dbg(radio->dev, "%s: NOT BUSY\n", __func__); 325 } 326 327 out: 328 core->write(core, WL1273_INT_MASK_SET, radio->irq_flags); 329 complete(&radio->busy); 330 331 return IRQ_HANDLED; 332 } 333 334 static int wl1273_fm_set_tx_freq(struct wl1273_device *radio, unsigned int freq) 335 { 336 struct wl1273_core *core = radio->core; 337 int r = 0; 338 unsigned long t; 339 340 if (freq < WL1273_BAND_TX_LOW) { 341 dev_err(radio->dev, 342 "Frequency out of range: %d < %d\n", freq, 343 WL1273_BAND_TX_LOW); 344 return -ERANGE; 345 } 346 347 if (freq > WL1273_BAND_TX_HIGH) { 348 dev_err(radio->dev, 349 "Frequency out of range: %d > %d\n", freq, 350 WL1273_BAND_TX_HIGH); 351 return -ERANGE; 352 } 353 354 /* 355 * The driver works better with this sleep, 356 * the documentation doesn't mention it. 357 */ 358 usleep_range(5000, 10000); 359 360 dev_dbg(radio->dev, "%s: freq: %d kHz\n", __func__, freq); 361 362 /* Set the current tx channel */ 363 r = core->write(core, WL1273_CHANL_SET, freq / 10); 364 if (r) 365 return r; 366 367 reinit_completion(&radio->busy); 368 369 /* wait for the FR IRQ */ 370 t = wait_for_completion_timeout(&radio->busy, msecs_to_jiffies(2000)); 371 if (!t) 372 return -ETIMEDOUT; 373 374 dev_dbg(radio->dev, "WL1273_CHANL_SET: %lu\n", t); 375 376 /* Enable the output power */ 377 r = core->write(core, WL1273_POWER_ENB_SET, 1); 378 if (r) 379 return r; 380 381 reinit_completion(&radio->busy); 382 383 /* wait for the POWER_ENB IRQ */ 384 t = wait_for_completion_timeout(&radio->busy, msecs_to_jiffies(1000)); 385 if (!t) 386 return -ETIMEDOUT; 387 388 radio->tx_frequency = freq; 389 dev_dbg(radio->dev, "WL1273_POWER_ENB_SET: %lu\n", t); 390 391 return 0; 392 } 393 394 static int wl1273_fm_set_rx_freq(struct wl1273_device *radio, unsigned int freq) 395 { 396 struct wl1273_core *core = radio->core; 397 int r, f; 398 unsigned long t; 399 400 if (freq < radio->rangelow) { 401 dev_err(radio->dev, 402 "Frequency out of range: %d < %d\n", freq, 403 radio->rangelow); 404 r = -ERANGE; 405 goto err; 406 } 407 408 if (freq > radio->rangehigh) { 409 dev_err(radio->dev, 410 "Frequency out of range: %d > %d\n", freq, 411 radio->rangehigh); 412 r = -ERANGE; 413 goto err; 414 } 415 416 dev_dbg(radio->dev, "%s: %dkHz\n", __func__, freq); 417 418 core->write(core, WL1273_INT_MASK_SET, radio->irq_flags); 419 420 if (radio->band == WL1273_BAND_JAPAN) 421 f = (freq - WL1273_BAND_JAPAN_LOW) / 50; 422 else 423 f = (freq - WL1273_BAND_OTHER_LOW) / 50; 424 425 r = core->write(core, WL1273_FREQ_SET, f); 426 if (r) { 427 dev_err(radio->dev, "FREQ_SET fails\n"); 428 goto err; 429 } 430 431 r = core->write(core, WL1273_TUNER_MODE_SET, TUNER_MODE_PRESET); 432 if (r) { 433 dev_err(radio->dev, "TUNER_MODE_SET fails\n"); 434 goto err; 435 } 436 437 reinit_completion(&radio->busy); 438 439 t = wait_for_completion_timeout(&radio->busy, msecs_to_jiffies(2000)); 440 if (!t) { 441 dev_err(radio->dev, "%s: TIMEOUT\n", __func__); 442 return -ETIMEDOUT; 443 } 444 445 radio->rd_index = 0; 446 radio->wr_index = 0; 447 radio->rx_frequency = freq; 448 return 0; 449 err: 450 return r; 451 } 452 453 static int wl1273_fm_get_freq(struct wl1273_device *radio) 454 { 455 struct wl1273_core *core = radio->core; 456 unsigned int freq; 457 u16 f; 458 int r; 459 460 if (core->mode == WL1273_MODE_RX) { 461 r = core->read(core, WL1273_FREQ_SET, &f); 462 if (r) 463 return r; 464 465 dev_dbg(radio->dev, "Freq get: 0x%04x\n", f); 466 if (radio->band == WL1273_BAND_JAPAN) 467 freq = WL1273_BAND_JAPAN_LOW + 50 * f; 468 else 469 freq = WL1273_BAND_OTHER_LOW + 50 * f; 470 } else { 471 r = core->read(core, WL1273_CHANL_SET, &f); 472 if (r) 473 return r; 474 475 freq = f * 10; 476 } 477 478 return freq; 479 } 480 481 /** 482 * wl1273_fm_upload_firmware_patch() - Upload the firmware. 483 * @radio: A pointer to the device struct. 484 * 485 * The firmware file consists of arrays of bytes where the first byte 486 * gives the array length. The first byte in the file gives the 487 * number of these arrays. 488 */ 489 static int wl1273_fm_upload_firmware_patch(struct wl1273_device *radio) 490 { 491 struct wl1273_core *core = radio->core; 492 unsigned int packet_num; 493 const struct firmware *fw_p; 494 const char *fw_name = "radio-wl1273-fw.bin"; 495 struct device *dev = radio->dev; 496 __u8 *ptr; 497 int r; 498 499 dev_dbg(dev, "%s:\n", __func__); 500 501 /* 502 * Uploading the firmware patch is not always necessary, 503 * so we only print an info message. 504 */ 505 if (request_firmware(&fw_p, fw_name, dev)) { 506 dev_info(dev, "%s - %s not found\n", __func__, fw_name); 507 508 return 0; 509 } 510 511 ptr = (__u8 *) fw_p->data; 512 packet_num = ptr[0]; 513 dev_dbg(dev, "%s: packets: %d\n", __func__, packet_num); 514 515 r = wl1273_fm_write_fw(core, ptr + 1, packet_num); 516 if (r) { 517 dev_err(dev, "FW upload error: %d\n", r); 518 goto out; 519 } 520 521 /* ignore possible error here */ 522 core->write(core, WL1273_RESET, 0); 523 524 dev_dbg(dev, "%s - download OK, r: %d\n", __func__, r); 525 out: 526 release_firmware(fw_p); 527 return r; 528 } 529 530 static int wl1273_fm_stop(struct wl1273_device *radio) 531 { 532 struct wl1273_core *core = radio->core; 533 534 if (core->mode == WL1273_MODE_RX) { 535 int r = core->write(core, WL1273_POWER_SET, 536 WL1273_POWER_SET_OFF); 537 if (r) 538 dev_err(radio->dev, "%s: POWER_SET fails: %d\n", 539 __func__, r); 540 } else if (core->mode == WL1273_MODE_TX) { 541 int r = core->write(core, WL1273_PUPD_SET, 542 WL1273_PUPD_SET_OFF); 543 if (r) 544 dev_err(radio->dev, 545 "%s: PUPD_SET fails: %d\n", __func__, r); 546 } 547 548 if (core->pdata->disable) { 549 core->pdata->disable(); 550 dev_dbg(radio->dev, "Back to reset\n"); 551 } 552 553 return 0; 554 } 555 556 static int wl1273_fm_start(struct wl1273_device *radio, int new_mode) 557 { 558 struct wl1273_core *core = radio->core; 559 struct wl1273_fm_platform_data *pdata = core->pdata; 560 struct device *dev = radio->dev; 561 int r = -EINVAL; 562 563 if (pdata->enable && core->mode == WL1273_MODE_OFF) { 564 dev_dbg(radio->dev, "Out of reset\n"); 565 566 pdata->enable(); 567 msleep(250); 568 } 569 570 if (new_mode == WL1273_MODE_RX) { 571 u16 val = WL1273_POWER_SET_FM; 572 573 if (radio->rds_on) 574 val |= WL1273_POWER_SET_RDS; 575 576 /* If this fails try again */ 577 r = core->write(core, WL1273_POWER_SET, val); 578 if (r) { 579 msleep(100); 580 581 r = core->write(core, WL1273_POWER_SET, val); 582 if (r) { 583 dev_err(dev, "%s: POWER_SET fails\n", __func__); 584 goto fail; 585 } 586 } 587 588 /* rds buffer configuration */ 589 radio->wr_index = 0; 590 radio->rd_index = 0; 591 592 } else if (new_mode == WL1273_MODE_TX) { 593 /* If this fails try again once */ 594 r = core->write(core, WL1273_PUPD_SET, WL1273_PUPD_SET_ON); 595 if (r) { 596 msleep(100); 597 r = core->write(core, WL1273_PUPD_SET, 598 WL1273_PUPD_SET_ON); 599 if (r) { 600 dev_err(dev, "%s: PUPD_SET fails\n", __func__); 601 goto fail; 602 } 603 } 604 605 if (radio->rds_on) { 606 r = core->write(core, WL1273_RDS_DATA_ENB, 1); 607 if (r) { 608 dev_err(dev, "%s: RDS_DATA_ENB ON fails\n", 609 __func__); 610 goto fail; 611 } 612 } else { 613 r = core->write(core, WL1273_RDS_DATA_ENB, 0); 614 if (r) { 615 dev_err(dev, "%s: RDS_DATA_ENB OFF fails\n", 616 __func__); 617 goto fail; 618 } 619 } 620 } else { 621 dev_warn(dev, "%s: Illegal mode.\n", __func__); 622 } 623 624 if (core->mode == WL1273_MODE_OFF) { 625 r = wl1273_fm_upload_firmware_patch(radio); 626 if (r) 627 dev_warn(dev, "Firmware upload failed.\n"); 628 629 /* 630 * Sometimes the chip is in a wrong power state at this point. 631 * So we set the power once again. 632 */ 633 if (new_mode == WL1273_MODE_RX) { 634 u16 val = WL1273_POWER_SET_FM; 635 636 if (radio->rds_on) 637 val |= WL1273_POWER_SET_RDS; 638 639 r = core->write(core, WL1273_POWER_SET, val); 640 if (r) { 641 dev_err(dev, "%s: POWER_SET fails\n", __func__); 642 goto fail; 643 } 644 } else if (new_mode == WL1273_MODE_TX) { 645 r = core->write(core, WL1273_PUPD_SET, 646 WL1273_PUPD_SET_ON); 647 if (r) { 648 dev_err(dev, "%s: PUPD_SET fails\n", __func__); 649 goto fail; 650 } 651 } 652 } 653 654 return 0; 655 fail: 656 if (pdata->disable) 657 pdata->disable(); 658 659 dev_dbg(dev, "%s: return: %d\n", __func__, r); 660 return r; 661 } 662 663 static int wl1273_fm_suspend(struct wl1273_device *radio) 664 { 665 struct wl1273_core *core = radio->core; 666 int r; 667 668 /* Cannot go from OFF to SUSPENDED */ 669 if (core->mode == WL1273_MODE_RX) 670 r = core->write(core, WL1273_POWER_SET, 671 WL1273_POWER_SET_RETENTION); 672 else if (core->mode == WL1273_MODE_TX) 673 r = core->write(core, WL1273_PUPD_SET, 674 WL1273_PUPD_SET_RETENTION); 675 else 676 r = -EINVAL; 677 678 if (r) { 679 dev_err(radio->dev, "%s: POWER_SET fails: %d\n", __func__, r); 680 goto out; 681 } 682 683 out: 684 return r; 685 } 686 687 static int wl1273_fm_set_mode(struct wl1273_device *radio, int mode) 688 { 689 struct wl1273_core *core = radio->core; 690 struct device *dev = radio->dev; 691 int old_mode; 692 int r; 693 694 dev_dbg(dev, "%s\n", __func__); 695 dev_dbg(dev, "Forbidden modes: 0x%02x\n", radio->forbidden); 696 697 old_mode = core->mode; 698 if (mode & radio->forbidden) { 699 r = -EPERM; 700 goto out; 701 } 702 703 switch (mode) { 704 case WL1273_MODE_RX: 705 case WL1273_MODE_TX: 706 r = wl1273_fm_start(radio, mode); 707 if (r) { 708 dev_err(dev, "%s: Cannot start.\n", __func__); 709 wl1273_fm_stop(radio); 710 goto out; 711 } 712 713 core->mode = mode; 714 r = core->write(core, WL1273_INT_MASK_SET, radio->irq_flags); 715 if (r) { 716 dev_err(dev, "INT_MASK_SET fails.\n"); 717 goto out; 718 } 719 720 /* remember previous settings */ 721 if (mode == WL1273_MODE_RX) { 722 r = wl1273_fm_set_rx_freq(radio, radio->rx_frequency); 723 if (r) { 724 dev_err(dev, "set freq fails: %d.\n", r); 725 goto out; 726 } 727 728 r = core->set_volume(core, core->volume); 729 if (r) { 730 dev_err(dev, "set volume fails: %d.\n", r); 731 goto out; 732 } 733 734 dev_dbg(dev, "%s: Set vol: %d.\n", __func__, 735 core->volume); 736 } else { 737 r = wl1273_fm_set_tx_freq(radio, radio->tx_frequency); 738 if (r) { 739 dev_err(dev, "set freq fails: %d.\n", r); 740 goto out; 741 } 742 } 743 744 dev_dbg(radio->dev, "%s: Set audio mode.\n", __func__); 745 746 r = core->set_audio(core, core->audio_mode); 747 if (r) 748 dev_err(dev, "Cannot set audio mode.\n"); 749 break; 750 751 case WL1273_MODE_OFF: 752 r = wl1273_fm_stop(radio); 753 if (r) 754 dev_err(dev, "%s: Off fails: %d\n", __func__, r); 755 else 756 core->mode = WL1273_MODE_OFF; 757 758 break; 759 760 case WL1273_MODE_SUSPENDED: 761 r = wl1273_fm_suspend(radio); 762 if (r) 763 dev_err(dev, "%s: Suspend fails: %d\n", __func__, r); 764 else 765 core->mode = WL1273_MODE_SUSPENDED; 766 767 break; 768 769 default: 770 dev_err(dev, "%s: Unknown mode: %d\n", __func__, mode); 771 r = -EINVAL; 772 break; 773 } 774 out: 775 if (r) 776 core->mode = old_mode; 777 778 return r; 779 } 780 781 static int wl1273_fm_set_seek(struct wl1273_device *radio, 782 unsigned int wrap_around, 783 unsigned int seek_upward, 784 int level) 785 { 786 struct wl1273_core *core = radio->core; 787 int r = 0; 788 unsigned int dir = (seek_upward == 0) ? 0 : 1; 789 unsigned int f; 790 791 f = radio->rx_frequency; 792 dev_dbg(radio->dev, "rx_frequency: %d\n", f); 793 794 if (dir && f + radio->spacing <= radio->rangehigh) 795 r = wl1273_fm_set_rx_freq(radio, f + radio->spacing); 796 else if (dir && wrap_around) 797 r = wl1273_fm_set_rx_freq(radio, radio->rangelow); 798 else if (f - radio->spacing >= radio->rangelow) 799 r = wl1273_fm_set_rx_freq(radio, f - radio->spacing); 800 else if (wrap_around) 801 r = wl1273_fm_set_rx_freq(radio, radio->rangehigh); 802 803 if (r) 804 goto out; 805 806 if (level < SCHAR_MIN || level > SCHAR_MAX) 807 return -EINVAL; 808 809 reinit_completion(&radio->busy); 810 dev_dbg(radio->dev, "%s: BUSY\n", __func__); 811 812 r = core->write(core, WL1273_INT_MASK_SET, radio->irq_flags); 813 if (r) 814 goto out; 815 816 dev_dbg(radio->dev, "%s\n", __func__); 817 818 r = core->write(core, WL1273_SEARCH_LVL_SET, level); 819 if (r) 820 goto out; 821 822 r = core->write(core, WL1273_SEARCH_DIR_SET, dir); 823 if (r) 824 goto out; 825 826 r = core->write(core, WL1273_TUNER_MODE_SET, TUNER_MODE_AUTO_SEEK); 827 if (r) 828 goto out; 829 830 /* wait for the FR IRQ */ 831 wait_for_completion_timeout(&radio->busy, msecs_to_jiffies(1000)); 832 if (!(radio->irq_received & WL1273_BL_EVENT)) { 833 r = -ETIMEDOUT; 834 goto out; 835 } 836 837 radio->irq_received &= ~WL1273_BL_EVENT; 838 839 if (!wrap_around) 840 goto out; 841 842 /* Wrap around */ 843 dev_dbg(radio->dev, "Wrap around in HW seek.\n"); 844 845 if (seek_upward) 846 f = radio->rangelow; 847 else 848 f = radio->rangehigh; 849 850 r = wl1273_fm_set_rx_freq(radio, f); 851 if (r) 852 goto out; 853 854 reinit_completion(&radio->busy); 855 dev_dbg(radio->dev, "%s: BUSY\n", __func__); 856 857 r = core->write(core, WL1273_TUNER_MODE_SET, TUNER_MODE_AUTO_SEEK); 858 if (r) 859 goto out; 860 861 /* wait for the FR IRQ */ 862 if (!wait_for_completion_timeout(&radio->busy, msecs_to_jiffies(1000))) 863 r = -ETIMEDOUT; 864 out: 865 dev_dbg(radio->dev, "%s: Err: %d\n", __func__, r); 866 return r; 867 } 868 869 /** 870 * wl1273_fm_get_tx_ctune() - Get the TX tuning capacitor value. 871 * @radio: A pointer to the device struct. 872 */ 873 static unsigned int wl1273_fm_get_tx_ctune(struct wl1273_device *radio) 874 { 875 struct wl1273_core *core = radio->core; 876 struct device *dev = radio->dev; 877 u16 val; 878 int r; 879 880 if (core->mode == WL1273_MODE_OFF || 881 core->mode == WL1273_MODE_SUSPENDED) 882 return -EPERM; 883 884 r = core->read(core, WL1273_READ_FMANT_TUNE_VALUE, &val); 885 if (r) { 886 dev_err(dev, "%s: read error: %d\n", __func__, r); 887 goto out; 888 } 889 890 out: 891 return val; 892 } 893 894 /** 895 * wl1273_fm_set_preemphasis() - Set the TX pre-emphasis value. 896 * @radio: A pointer to the device struct. 897 * @preemphasis: The new pre-amphasis value. 898 * 899 * Possible pre-emphasis values are: V4L2_PREEMPHASIS_DISABLED, 900 * V4L2_PREEMPHASIS_50_uS and V4L2_PREEMPHASIS_75_uS. 901 */ 902 static int wl1273_fm_set_preemphasis(struct wl1273_device *radio, 903 unsigned int preemphasis) 904 { 905 struct wl1273_core *core = radio->core; 906 int r; 907 u16 em; 908 909 if (core->mode == WL1273_MODE_OFF || 910 core->mode == WL1273_MODE_SUSPENDED) 911 return -EPERM; 912 913 mutex_lock(&core->lock); 914 915 switch (preemphasis) { 916 case V4L2_PREEMPHASIS_DISABLED: 917 em = 1; 918 break; 919 case V4L2_PREEMPHASIS_50_uS: 920 em = 0; 921 break; 922 case V4L2_PREEMPHASIS_75_uS: 923 em = 2; 924 break; 925 default: 926 r = -EINVAL; 927 goto out; 928 } 929 930 r = core->write(core, WL1273_PREMPH_SET, em); 931 if (r) 932 goto out; 933 934 radio->preemphasis = preemphasis; 935 936 out: 937 mutex_unlock(&core->lock); 938 return r; 939 } 940 941 static int wl1273_fm_rds_on(struct wl1273_device *radio) 942 { 943 struct wl1273_core *core = radio->core; 944 int r; 945 946 dev_dbg(radio->dev, "%s\n", __func__); 947 if (radio->rds_on) 948 return 0; 949 950 r = core->write(core, WL1273_POWER_SET, 951 WL1273_POWER_SET_FM | WL1273_POWER_SET_RDS); 952 if (r) 953 goto out; 954 955 r = wl1273_fm_set_rx_freq(radio, radio->rx_frequency); 956 if (r) 957 dev_err(radio->dev, "set freq fails: %d.\n", r); 958 out: 959 return r; 960 } 961 962 static int wl1273_fm_rds_off(struct wl1273_device *radio) 963 { 964 struct wl1273_core *core = radio->core; 965 int r; 966 967 if (!radio->rds_on) 968 return 0; 969 970 radio->irq_flags &= ~WL1273_RDS_EVENT; 971 972 r = core->write(core, WL1273_INT_MASK_SET, radio->irq_flags); 973 if (r) 974 goto out; 975 976 /* Service pending read */ 977 wake_up_interruptible(&radio->read_queue); 978 979 dev_dbg(radio->dev, "%s\n", __func__); 980 981 r = core->write(core, WL1273_POWER_SET, WL1273_POWER_SET_FM); 982 if (r) 983 goto out; 984 985 r = wl1273_fm_set_rx_freq(radio, radio->rx_frequency); 986 if (r) 987 dev_err(radio->dev, "set freq fails: %d.\n", r); 988 out: 989 dev_dbg(radio->dev, "%s: exiting...\n", __func__); 990 991 return r; 992 } 993 994 static int wl1273_fm_set_rds(struct wl1273_device *radio, unsigned int new_mode) 995 { 996 int r = 0; 997 struct wl1273_core *core = radio->core; 998 999 if (core->mode == WL1273_MODE_OFF || 1000 core->mode == WL1273_MODE_SUSPENDED) 1001 return -EPERM; 1002 1003 if (new_mode == WL1273_RDS_RESET) { 1004 r = core->write(core, WL1273_RDS_CNTRL_SET, 1); 1005 return r; 1006 } 1007 1008 if (core->mode == WL1273_MODE_TX && new_mode == WL1273_RDS_OFF) { 1009 r = core->write(core, WL1273_RDS_DATA_ENB, 0); 1010 } else if (core->mode == WL1273_MODE_TX && new_mode == WL1273_RDS_ON) { 1011 r = core->write(core, WL1273_RDS_DATA_ENB, 1); 1012 } else if (core->mode == WL1273_MODE_RX && new_mode == WL1273_RDS_OFF) { 1013 r = wl1273_fm_rds_off(radio); 1014 } else if (core->mode == WL1273_MODE_RX && new_mode == WL1273_RDS_ON) { 1015 r = wl1273_fm_rds_on(radio); 1016 } else { 1017 dev_err(radio->dev, "%s: Unknown mode: %d\n", 1018 __func__, new_mode); 1019 r = -EINVAL; 1020 } 1021 1022 if (!r) 1023 radio->rds_on = (new_mode == WL1273_RDS_ON) ? true : false; 1024 1025 return r; 1026 } 1027 1028 static ssize_t wl1273_fm_fops_write(struct file *file, const char __user *buf, 1029 size_t count, loff_t *ppos) 1030 { 1031 struct wl1273_device *radio = video_get_drvdata(video_devdata(file)); 1032 struct wl1273_core *core = radio->core; 1033 u16 val; 1034 int r; 1035 1036 dev_dbg(radio->dev, "%s\n", __func__); 1037 1038 if (core->mode != WL1273_MODE_TX) 1039 return count; 1040 1041 if (radio->rds_users == 0) { 1042 dev_warn(radio->dev, "%s: RDS not on.\n", __func__); 1043 return 0; 1044 } 1045 1046 if (mutex_lock_interruptible(&core->lock)) 1047 return -EINTR; 1048 /* 1049 * Multiple processes can open the device, but only 1050 * one gets to write to it. 1051 */ 1052 if (radio->owner && radio->owner != file) { 1053 r = -EBUSY; 1054 goto out; 1055 } 1056 radio->owner = file; 1057 1058 /* Manual Mode */ 1059 if (count > 255) 1060 val = 255; 1061 else 1062 val = count; 1063 1064 core->write(core, WL1273_RDS_CONFIG_DATA_SET, val); 1065 1066 if (copy_from_user(radio->write_buf + 1, buf, val)) { 1067 r = -EFAULT; 1068 goto out; 1069 } 1070 1071 dev_dbg(radio->dev, "Count: %d\n", val); 1072 dev_dbg(radio->dev, "From user: \"%s\"\n", radio->write_buf); 1073 1074 radio->write_buf[0] = WL1273_RDS_DATA_SET; 1075 core->write_data(core, radio->write_buf, val + 1); 1076 1077 r = val; 1078 out: 1079 mutex_unlock(&core->lock); 1080 1081 return r; 1082 } 1083 1084 static __poll_t wl1273_fm_fops_poll(struct file *file, 1085 struct poll_table_struct *pts) 1086 { 1087 struct wl1273_device *radio = video_get_drvdata(video_devdata(file)); 1088 struct wl1273_core *core = radio->core; 1089 1090 if (radio->owner && radio->owner != file) 1091 return EPOLLERR; 1092 1093 radio->owner = file; 1094 1095 if (core->mode == WL1273_MODE_RX) { 1096 poll_wait(file, &radio->read_queue, pts); 1097 1098 if (radio->rd_index != radio->wr_index) 1099 return EPOLLIN | EPOLLRDNORM; 1100 1101 } else if (core->mode == WL1273_MODE_TX) { 1102 return EPOLLOUT | EPOLLWRNORM; 1103 } 1104 1105 return 0; 1106 } 1107 1108 static int wl1273_fm_fops_open(struct file *file) 1109 { 1110 struct wl1273_device *radio = video_get_drvdata(video_devdata(file)); 1111 struct wl1273_core *core = radio->core; 1112 int r = 0; 1113 1114 dev_dbg(radio->dev, "%s\n", __func__); 1115 1116 if (core->mode == WL1273_MODE_RX && radio->rds_on && 1117 !radio->rds_users) { 1118 dev_dbg(radio->dev, "%s: Mode: %d\n", __func__, core->mode); 1119 1120 if (mutex_lock_interruptible(&core->lock)) 1121 return -EINTR; 1122 1123 radio->irq_flags |= WL1273_RDS_EVENT; 1124 1125 r = core->write(core, WL1273_INT_MASK_SET, 1126 radio->irq_flags); 1127 if (r) { 1128 mutex_unlock(&core->lock); 1129 goto out; 1130 } 1131 1132 radio->rds_users++; 1133 1134 mutex_unlock(&core->lock); 1135 } 1136 out: 1137 return r; 1138 } 1139 1140 static int wl1273_fm_fops_release(struct file *file) 1141 { 1142 struct wl1273_device *radio = video_get_drvdata(video_devdata(file)); 1143 struct wl1273_core *core = radio->core; 1144 int r = 0; 1145 1146 dev_dbg(radio->dev, "%s\n", __func__); 1147 1148 if (radio->rds_users > 0) { 1149 radio->rds_users--; 1150 if (radio->rds_users == 0) { 1151 if (mutex_lock_interruptible(&core->lock)) 1152 return -EINTR; 1153 1154 radio->irq_flags &= ~WL1273_RDS_EVENT; 1155 1156 if (core->mode == WL1273_MODE_RX) { 1157 r = core->write(core, 1158 WL1273_INT_MASK_SET, 1159 radio->irq_flags); 1160 if (r) { 1161 mutex_unlock(&core->lock); 1162 goto out; 1163 } 1164 } 1165 mutex_unlock(&core->lock); 1166 } 1167 } 1168 1169 if (file == radio->owner) 1170 radio->owner = NULL; 1171 out: 1172 return r; 1173 } 1174 1175 static ssize_t wl1273_fm_fops_read(struct file *file, char __user *buf, 1176 size_t count, loff_t *ppos) 1177 { 1178 int r = 0; 1179 struct wl1273_device *radio = video_get_drvdata(video_devdata(file)); 1180 struct wl1273_core *core = radio->core; 1181 unsigned int block_count = 0; 1182 u16 val; 1183 1184 dev_dbg(radio->dev, "%s\n", __func__); 1185 1186 if (core->mode != WL1273_MODE_RX) 1187 return 0; 1188 1189 if (radio->rds_users == 0) { 1190 dev_warn(radio->dev, "%s: RDS not on.\n", __func__); 1191 return 0; 1192 } 1193 1194 if (mutex_lock_interruptible(&core->lock)) 1195 return -EINTR; 1196 1197 /* 1198 * Multiple processes can open the device, but only 1199 * one at a time gets read access. 1200 */ 1201 if (radio->owner && radio->owner != file) { 1202 r = -EBUSY; 1203 goto out; 1204 } 1205 radio->owner = file; 1206 1207 r = core->read(core, WL1273_RDS_SYNC_GET, &val); 1208 if (r) { 1209 dev_err(radio->dev, "%s: Get RDS_SYNC fails.\n", __func__); 1210 goto out; 1211 } else if (val == 0) { 1212 dev_info(radio->dev, "RDS_SYNC: Not synchronized\n"); 1213 r = -ENODATA; 1214 goto out; 1215 } 1216 1217 /* block if no new data available */ 1218 while (radio->wr_index == radio->rd_index) { 1219 if (file->f_flags & O_NONBLOCK) { 1220 r = -EWOULDBLOCK; 1221 goto out; 1222 } 1223 1224 dev_dbg(radio->dev, "%s: Wait for RDS data.\n", __func__); 1225 if (wait_event_interruptible(radio->read_queue, 1226 radio->wr_index != 1227 radio->rd_index) < 0) { 1228 r = -EINTR; 1229 goto out; 1230 } 1231 } 1232 1233 /* calculate block count from byte count */ 1234 count /= RDS_BLOCK_SIZE; 1235 1236 /* copy RDS blocks from the internal buffer and to user buffer */ 1237 while (block_count < count) { 1238 if (radio->rd_index == radio->wr_index) 1239 break; 1240 1241 /* always transfer complete RDS blocks */ 1242 if (copy_to_user(buf, &radio->buffer[radio->rd_index], 1243 RDS_BLOCK_SIZE)) 1244 break; 1245 1246 /* increment and wrap the read pointer */ 1247 radio->rd_index += RDS_BLOCK_SIZE; 1248 if (radio->rd_index >= radio->buf_size) 1249 radio->rd_index = 0; 1250 1251 /* increment counters */ 1252 block_count++; 1253 buf += RDS_BLOCK_SIZE; 1254 r += RDS_BLOCK_SIZE; 1255 } 1256 1257 out: 1258 dev_dbg(radio->dev, "%s: exit\n", __func__); 1259 mutex_unlock(&core->lock); 1260 1261 return r; 1262 } 1263 1264 static const struct v4l2_file_operations wl1273_fops = { 1265 .owner = THIS_MODULE, 1266 .read = wl1273_fm_fops_read, 1267 .write = wl1273_fm_fops_write, 1268 .poll = wl1273_fm_fops_poll, 1269 .unlocked_ioctl = video_ioctl2, 1270 .open = wl1273_fm_fops_open, 1271 .release = wl1273_fm_fops_release, 1272 }; 1273 1274 static int wl1273_fm_vidioc_querycap(struct file *file, void *priv, 1275 struct v4l2_capability *capability) 1276 { 1277 struct wl1273_device *radio = video_get_drvdata(video_devdata(file)); 1278 1279 dev_dbg(radio->dev, "%s\n", __func__); 1280 1281 strscpy(capability->driver, WL1273_FM_DRIVER_NAME, 1282 sizeof(capability->driver)); 1283 strscpy(capability->card, "Texas Instruments Wl1273 FM Radio", 1284 sizeof(capability->card)); 1285 strscpy(capability->bus_info, radio->bus_type, 1286 sizeof(capability->bus_info)); 1287 return 0; 1288 } 1289 1290 static int wl1273_fm_vidioc_g_input(struct file *file, void *priv, 1291 unsigned int *i) 1292 { 1293 struct wl1273_device *radio = video_get_drvdata(video_devdata(file)); 1294 1295 dev_dbg(radio->dev, "%s\n", __func__); 1296 1297 *i = 0; 1298 1299 return 0; 1300 } 1301 1302 static int wl1273_fm_vidioc_s_input(struct file *file, void *priv, 1303 unsigned int i) 1304 { 1305 struct wl1273_device *radio = video_get_drvdata(video_devdata(file)); 1306 1307 dev_dbg(radio->dev, "%s\n", __func__); 1308 1309 if (i != 0) 1310 return -EINVAL; 1311 1312 return 0; 1313 } 1314 1315 /** 1316 * wl1273_fm_set_tx_power() - Set the transmission power value. 1317 * @radio: A pointer to the device struct. 1318 * @power: The new power value. 1319 */ 1320 static int wl1273_fm_set_tx_power(struct wl1273_device *radio, u16 power) 1321 { 1322 struct wl1273_core *core = radio->core; 1323 int r; 1324 1325 if (core->mode == WL1273_MODE_OFF || 1326 core->mode == WL1273_MODE_SUSPENDED) 1327 return -EPERM; 1328 1329 mutex_lock(&core->lock); 1330 1331 /* Convert the dBuV value to chip presentation */ 1332 r = core->write(core, WL1273_POWER_LEV_SET, 122 - power); 1333 if (r) 1334 goto out; 1335 1336 radio->tx_power = power; 1337 1338 out: 1339 mutex_unlock(&core->lock); 1340 return r; 1341 } 1342 1343 #define WL1273_SPACING_50kHz 1 1344 #define WL1273_SPACING_100kHz 2 1345 #define WL1273_SPACING_200kHz 4 1346 1347 static int wl1273_fm_tx_set_spacing(struct wl1273_device *radio, 1348 unsigned int spacing) 1349 { 1350 struct wl1273_core *core = radio->core; 1351 int r; 1352 1353 if (spacing == 0) { 1354 r = core->write(core, WL1273_SCAN_SPACING_SET, 1355 WL1273_SPACING_100kHz); 1356 radio->spacing = 100; 1357 } else if (spacing - 50000 < 25000) { 1358 r = core->write(core, WL1273_SCAN_SPACING_SET, 1359 WL1273_SPACING_50kHz); 1360 radio->spacing = 50; 1361 } else if (spacing - 100000 < 50000) { 1362 r = core->write(core, WL1273_SCAN_SPACING_SET, 1363 WL1273_SPACING_100kHz); 1364 radio->spacing = 100; 1365 } else { 1366 r = core->write(core, WL1273_SCAN_SPACING_SET, 1367 WL1273_SPACING_200kHz); 1368 radio->spacing = 200; 1369 } 1370 1371 return r; 1372 } 1373 1374 static int wl1273_fm_g_volatile_ctrl(struct v4l2_ctrl *ctrl) 1375 { 1376 struct wl1273_device *radio = ctrl->priv; 1377 struct wl1273_core *core = radio->core; 1378 1379 dev_dbg(radio->dev, "%s\n", __func__); 1380 1381 if (mutex_lock_interruptible(&core->lock)) 1382 return -EINTR; 1383 1384 switch (ctrl->id) { 1385 case V4L2_CID_TUNE_ANTENNA_CAPACITOR: 1386 ctrl->val = wl1273_fm_get_tx_ctune(radio); 1387 break; 1388 1389 default: 1390 dev_warn(radio->dev, "%s: Unknown IOCTL: %d\n", 1391 __func__, ctrl->id); 1392 break; 1393 } 1394 1395 mutex_unlock(&core->lock); 1396 1397 return 0; 1398 } 1399 1400 #define WL1273_MUTE_SOFT_ENABLE (1 << 0) 1401 #define WL1273_MUTE_AC (1 << 1) 1402 #define WL1273_MUTE_HARD_LEFT (1 << 2) 1403 #define WL1273_MUTE_HARD_RIGHT (1 << 3) 1404 #define WL1273_MUTE_SOFT_FORCE (1 << 4) 1405 1406 static inline struct wl1273_device *to_radio(struct v4l2_ctrl *ctrl) 1407 { 1408 return container_of(ctrl->handler, struct wl1273_device, ctrl_handler); 1409 } 1410 1411 static int wl1273_fm_vidioc_s_ctrl(struct v4l2_ctrl *ctrl) 1412 { 1413 struct wl1273_device *radio = to_radio(ctrl); 1414 struct wl1273_core *core = radio->core; 1415 int r = 0; 1416 1417 dev_dbg(radio->dev, "%s\n", __func__); 1418 1419 switch (ctrl->id) { 1420 case V4L2_CID_AUDIO_MUTE: 1421 if (mutex_lock_interruptible(&core->lock)) 1422 return -EINTR; 1423 1424 if (core->mode == WL1273_MODE_RX && ctrl->val) 1425 r = core->write(core, 1426 WL1273_MUTE_STATUS_SET, 1427 WL1273_MUTE_HARD_LEFT | 1428 WL1273_MUTE_HARD_RIGHT); 1429 else if (core->mode == WL1273_MODE_RX) 1430 r = core->write(core, 1431 WL1273_MUTE_STATUS_SET, 0x0); 1432 else if (core->mode == WL1273_MODE_TX && ctrl->val) 1433 r = core->write(core, WL1273_MUTE, 1); 1434 else if (core->mode == WL1273_MODE_TX) 1435 r = core->write(core, WL1273_MUTE, 0); 1436 1437 mutex_unlock(&core->lock); 1438 break; 1439 1440 case V4L2_CID_AUDIO_VOLUME: 1441 if (ctrl->val == 0) 1442 r = wl1273_fm_set_mode(radio, WL1273_MODE_OFF); 1443 else 1444 r = core->set_volume(core, core->volume); 1445 break; 1446 1447 case V4L2_CID_TUNE_PREEMPHASIS: 1448 r = wl1273_fm_set_preemphasis(radio, ctrl->val); 1449 break; 1450 1451 case V4L2_CID_TUNE_POWER_LEVEL: 1452 r = wl1273_fm_set_tx_power(radio, ctrl->val); 1453 break; 1454 1455 default: 1456 dev_warn(radio->dev, "%s: Unknown IOCTL: %d\n", 1457 __func__, ctrl->id); 1458 break; 1459 } 1460 1461 dev_dbg(radio->dev, "%s\n", __func__); 1462 return r; 1463 } 1464 1465 static int wl1273_fm_vidioc_g_audio(struct file *file, void *priv, 1466 struct v4l2_audio *audio) 1467 { 1468 struct wl1273_device *radio = video_get_drvdata(video_devdata(file)); 1469 1470 dev_dbg(radio->dev, "%s\n", __func__); 1471 1472 if (audio->index > 1) 1473 return -EINVAL; 1474 1475 strscpy(audio->name, "Radio", sizeof(audio->name)); 1476 audio->capability = V4L2_AUDCAP_STEREO; 1477 1478 return 0; 1479 } 1480 1481 static int wl1273_fm_vidioc_s_audio(struct file *file, void *priv, 1482 const struct v4l2_audio *audio) 1483 { 1484 struct wl1273_device *radio = video_get_drvdata(video_devdata(file)); 1485 1486 dev_dbg(radio->dev, "%s\n", __func__); 1487 1488 if (audio->index != 0) 1489 return -EINVAL; 1490 1491 return 0; 1492 } 1493 1494 #define WL1273_RDS_NOT_SYNCHRONIZED 0 1495 #define WL1273_RDS_SYNCHRONIZED 1 1496 1497 static int wl1273_fm_vidioc_g_tuner(struct file *file, void *priv, 1498 struct v4l2_tuner *tuner) 1499 { 1500 struct wl1273_device *radio = video_get_drvdata(video_devdata(file)); 1501 struct wl1273_core *core = radio->core; 1502 u16 val; 1503 int r; 1504 1505 dev_dbg(radio->dev, "%s\n", __func__); 1506 1507 if (tuner->index > 0) 1508 return -EINVAL; 1509 1510 strscpy(tuner->name, WL1273_FM_DRIVER_NAME, sizeof(tuner->name)); 1511 tuner->type = V4L2_TUNER_RADIO; 1512 1513 tuner->rangelow = WL1273_FREQ(WL1273_BAND_JAPAN_LOW); 1514 tuner->rangehigh = WL1273_FREQ(WL1273_BAND_OTHER_HIGH); 1515 1516 tuner->capability = V4L2_TUNER_CAP_LOW | V4L2_TUNER_CAP_RDS | 1517 V4L2_TUNER_CAP_STEREO | V4L2_TUNER_CAP_RDS_BLOCK_IO | 1518 V4L2_TUNER_CAP_HWSEEK_BOUNDED | V4L2_TUNER_CAP_HWSEEK_WRAP; 1519 1520 if (radio->stereo) 1521 tuner->audmode = V4L2_TUNER_MODE_STEREO; 1522 else 1523 tuner->audmode = V4L2_TUNER_MODE_MONO; 1524 1525 if (core->mode != WL1273_MODE_RX) 1526 return 0; 1527 1528 if (mutex_lock_interruptible(&core->lock)) 1529 return -EINTR; 1530 1531 r = core->read(core, WL1273_STEREO_GET, &val); 1532 if (r) 1533 goto out; 1534 1535 if (val == 1) 1536 tuner->rxsubchans = V4L2_TUNER_SUB_STEREO; 1537 else 1538 tuner->rxsubchans = V4L2_TUNER_SUB_MONO; 1539 1540 r = core->read(core, WL1273_RSSI_LVL_GET, &val); 1541 if (r) 1542 goto out; 1543 1544 tuner->signal = (s16) val; 1545 dev_dbg(radio->dev, "Signal: %d\n", tuner->signal); 1546 1547 tuner->afc = 0; 1548 1549 r = core->read(core, WL1273_RDS_SYNC_GET, &val); 1550 if (r) 1551 goto out; 1552 1553 if (val == WL1273_RDS_SYNCHRONIZED) 1554 tuner->rxsubchans |= V4L2_TUNER_SUB_RDS; 1555 out: 1556 mutex_unlock(&core->lock); 1557 1558 return r; 1559 } 1560 1561 static int wl1273_fm_vidioc_s_tuner(struct file *file, void *priv, 1562 const struct v4l2_tuner *tuner) 1563 { 1564 struct wl1273_device *radio = video_get_drvdata(video_devdata(file)); 1565 struct wl1273_core *core = radio->core; 1566 int r = 0; 1567 1568 dev_dbg(radio->dev, "%s\n", __func__); 1569 dev_dbg(radio->dev, "tuner->index: %d\n", tuner->index); 1570 dev_dbg(radio->dev, "tuner->name: %s\n", tuner->name); 1571 dev_dbg(radio->dev, "tuner->capability: 0x%04x\n", tuner->capability); 1572 dev_dbg(radio->dev, "tuner->rxsubchans: 0x%04x\n", tuner->rxsubchans); 1573 dev_dbg(radio->dev, "tuner->rangelow: %d\n", tuner->rangelow); 1574 dev_dbg(radio->dev, "tuner->rangehigh: %d\n", tuner->rangehigh); 1575 1576 if (tuner->index > 0) 1577 return -EINVAL; 1578 1579 if (mutex_lock_interruptible(&core->lock)) 1580 return -EINTR; 1581 1582 r = wl1273_fm_set_mode(radio, WL1273_MODE_RX); 1583 if (r) 1584 goto out; 1585 1586 if (tuner->rxsubchans & V4L2_TUNER_SUB_RDS) 1587 r = wl1273_fm_set_rds(radio, WL1273_RDS_ON); 1588 else 1589 r = wl1273_fm_set_rds(radio, WL1273_RDS_OFF); 1590 1591 if (r) 1592 dev_warn(radio->dev, "%s: RDS fails: %d\n", __func__, r); 1593 1594 if (tuner->audmode == V4L2_TUNER_MODE_MONO) { 1595 r = core->write(core, WL1273_MOST_MODE_SET, WL1273_RX_MONO); 1596 if (r < 0) { 1597 dev_warn(radio->dev, "%s: MOST_MODE fails: %d\n", 1598 __func__, r); 1599 goto out; 1600 } 1601 radio->stereo = false; 1602 } else if (tuner->audmode == V4L2_TUNER_MODE_STEREO) { 1603 r = core->write(core, WL1273_MOST_MODE_SET, WL1273_RX_STEREO); 1604 if (r < 0) { 1605 dev_warn(radio->dev, "%s: MOST_MODE fails: %d\n", 1606 __func__, r); 1607 goto out; 1608 } 1609 radio->stereo = true; 1610 } else { 1611 dev_err(radio->dev, "%s: tuner->audmode: %d\n", 1612 __func__, tuner->audmode); 1613 r = -EINVAL; 1614 goto out; 1615 } 1616 1617 out: 1618 mutex_unlock(&core->lock); 1619 1620 return r; 1621 } 1622 1623 static int wl1273_fm_vidioc_g_frequency(struct file *file, void *priv, 1624 struct v4l2_frequency *freq) 1625 { 1626 struct wl1273_device *radio = video_get_drvdata(video_devdata(file)); 1627 struct wl1273_core *core = radio->core; 1628 1629 dev_dbg(radio->dev, "%s\n", __func__); 1630 1631 if (mutex_lock_interruptible(&core->lock)) 1632 return -EINTR; 1633 1634 freq->type = V4L2_TUNER_RADIO; 1635 freq->frequency = WL1273_FREQ(wl1273_fm_get_freq(radio)); 1636 1637 mutex_unlock(&core->lock); 1638 1639 return 0; 1640 } 1641 1642 static int wl1273_fm_vidioc_s_frequency(struct file *file, void *priv, 1643 const struct v4l2_frequency *freq) 1644 { 1645 struct wl1273_device *radio = video_get_drvdata(video_devdata(file)); 1646 struct wl1273_core *core = radio->core; 1647 int r; 1648 1649 dev_dbg(radio->dev, "%s: %d\n", __func__, freq->frequency); 1650 1651 if (freq->type != V4L2_TUNER_RADIO) { 1652 dev_dbg(radio->dev, 1653 "freq->type != V4L2_TUNER_RADIO: %d\n", freq->type); 1654 return -EINVAL; 1655 } 1656 1657 if (mutex_lock_interruptible(&core->lock)) 1658 return -EINTR; 1659 1660 if (core->mode == WL1273_MODE_RX) { 1661 dev_dbg(radio->dev, "freq: %d\n", freq->frequency); 1662 1663 r = wl1273_fm_set_rx_freq(radio, 1664 WL1273_INV_FREQ(freq->frequency)); 1665 if (r) 1666 dev_warn(radio->dev, WL1273_FM_DRIVER_NAME 1667 ": set frequency failed with %d\n", r); 1668 } else { 1669 r = wl1273_fm_set_tx_freq(radio, 1670 WL1273_INV_FREQ(freq->frequency)); 1671 if (r) 1672 dev_warn(radio->dev, WL1273_FM_DRIVER_NAME 1673 ": set frequency failed with %d\n", r); 1674 } 1675 1676 mutex_unlock(&core->lock); 1677 1678 dev_dbg(radio->dev, "wl1273_vidioc_s_frequency: DONE\n"); 1679 return r; 1680 } 1681 1682 #define WL1273_DEFAULT_SEEK_LEVEL 7 1683 1684 static int wl1273_fm_vidioc_s_hw_freq_seek(struct file *file, void *priv, 1685 const struct v4l2_hw_freq_seek *seek) 1686 { 1687 struct wl1273_device *radio = video_get_drvdata(video_devdata(file)); 1688 struct wl1273_core *core = radio->core; 1689 int r; 1690 1691 dev_dbg(radio->dev, "%s\n", __func__); 1692 1693 if (seek->tuner != 0 || seek->type != V4L2_TUNER_RADIO) 1694 return -EINVAL; 1695 1696 if (file->f_flags & O_NONBLOCK) 1697 return -EWOULDBLOCK; 1698 1699 if (mutex_lock_interruptible(&core->lock)) 1700 return -EINTR; 1701 1702 r = wl1273_fm_set_mode(radio, WL1273_MODE_RX); 1703 if (r) 1704 goto out; 1705 1706 r = wl1273_fm_tx_set_spacing(radio, seek->spacing); 1707 if (r) 1708 dev_warn(radio->dev, "HW seek failed: %d\n", r); 1709 1710 r = wl1273_fm_set_seek(radio, seek->wrap_around, seek->seek_upward, 1711 WL1273_DEFAULT_SEEK_LEVEL); 1712 if (r) 1713 dev_warn(radio->dev, "HW seek failed: %d\n", r); 1714 1715 out: 1716 mutex_unlock(&core->lock); 1717 return r; 1718 } 1719 1720 static int wl1273_fm_vidioc_s_modulator(struct file *file, void *priv, 1721 const struct v4l2_modulator *modulator) 1722 { 1723 struct wl1273_device *radio = video_get_drvdata(video_devdata(file)); 1724 struct wl1273_core *core = radio->core; 1725 int r = 0; 1726 1727 dev_dbg(radio->dev, "%s\n", __func__); 1728 1729 if (modulator->index > 0) 1730 return -EINVAL; 1731 1732 if (mutex_lock_interruptible(&core->lock)) 1733 return -EINTR; 1734 1735 r = wl1273_fm_set_mode(radio, WL1273_MODE_TX); 1736 if (r) 1737 goto out; 1738 1739 if (modulator->txsubchans & V4L2_TUNER_SUB_RDS) 1740 r = wl1273_fm_set_rds(radio, WL1273_RDS_ON); 1741 else 1742 r = wl1273_fm_set_rds(radio, WL1273_RDS_OFF); 1743 1744 if (modulator->txsubchans & V4L2_TUNER_SUB_MONO) 1745 r = core->write(core, WL1273_MONO_SET, WL1273_TX_MONO); 1746 else 1747 r = core->write(core, WL1273_MONO_SET, 1748 WL1273_RX_STEREO); 1749 if (r < 0) 1750 dev_warn(radio->dev, WL1273_FM_DRIVER_NAME 1751 "MONO_SET fails: %d\n", r); 1752 out: 1753 mutex_unlock(&core->lock); 1754 1755 return r; 1756 } 1757 1758 static int wl1273_fm_vidioc_g_modulator(struct file *file, void *priv, 1759 struct v4l2_modulator *modulator) 1760 { 1761 struct wl1273_device *radio = video_get_drvdata(video_devdata(file)); 1762 struct wl1273_core *core = radio->core; 1763 u16 val; 1764 int r; 1765 1766 dev_dbg(radio->dev, "%s\n", __func__); 1767 1768 strscpy(modulator->name, WL1273_FM_DRIVER_NAME, 1769 sizeof(modulator->name)); 1770 1771 modulator->rangelow = WL1273_FREQ(WL1273_BAND_JAPAN_LOW); 1772 modulator->rangehigh = WL1273_FREQ(WL1273_BAND_OTHER_HIGH); 1773 1774 modulator->capability = V4L2_TUNER_CAP_LOW | V4L2_TUNER_CAP_RDS | 1775 V4L2_TUNER_CAP_STEREO | V4L2_TUNER_CAP_RDS_BLOCK_IO; 1776 1777 if (core->mode != WL1273_MODE_TX) 1778 return 0; 1779 1780 if (mutex_lock_interruptible(&core->lock)) 1781 return -EINTR; 1782 1783 r = core->read(core, WL1273_MONO_SET, &val); 1784 if (r) 1785 goto out; 1786 1787 if (val == WL1273_TX_STEREO) 1788 modulator->txsubchans = V4L2_TUNER_SUB_STEREO; 1789 else 1790 modulator->txsubchans = V4L2_TUNER_SUB_MONO; 1791 1792 if (radio->rds_on) 1793 modulator->txsubchans |= V4L2_TUNER_SUB_RDS; 1794 out: 1795 mutex_unlock(&core->lock); 1796 1797 return 0; 1798 } 1799 1800 static int wl1273_fm_vidioc_log_status(struct file *file, void *priv) 1801 { 1802 struct wl1273_device *radio = video_get_drvdata(video_devdata(file)); 1803 struct wl1273_core *core = radio->core; 1804 struct device *dev = radio->dev; 1805 u16 val; 1806 int r; 1807 1808 dev_info(dev, DRIVER_DESC); 1809 1810 if (core->mode == WL1273_MODE_OFF) { 1811 dev_info(dev, "Mode: Off\n"); 1812 return 0; 1813 } 1814 1815 if (core->mode == WL1273_MODE_SUSPENDED) { 1816 dev_info(dev, "Mode: Suspended\n"); 1817 return 0; 1818 } 1819 1820 r = core->read(core, WL1273_ASIC_ID_GET, &val); 1821 if (r) 1822 dev_err(dev, "%s: Get ASIC_ID fails.\n", __func__); 1823 else 1824 dev_info(dev, "ASIC_ID: 0x%04x\n", val); 1825 1826 r = core->read(core, WL1273_ASIC_VER_GET, &val); 1827 if (r) 1828 dev_err(dev, "%s: Get ASIC_VER fails.\n", __func__); 1829 else 1830 dev_info(dev, "ASIC Version: 0x%04x\n", val); 1831 1832 r = core->read(core, WL1273_FIRM_VER_GET, &val); 1833 if (r) 1834 dev_err(dev, "%s: Get FIRM_VER fails.\n", __func__); 1835 else 1836 dev_info(dev, "FW version: %d(0x%04x)\n", val, val); 1837 1838 r = core->read(core, WL1273_BAND_SET, &val); 1839 if (r) 1840 dev_err(dev, "%s: Get BAND fails.\n", __func__); 1841 else 1842 dev_info(dev, "BAND: %d\n", val); 1843 1844 if (core->mode == WL1273_MODE_TX) { 1845 r = core->read(core, WL1273_PUPD_SET, &val); 1846 if (r) 1847 dev_err(dev, "%s: Get PUPD fails.\n", __func__); 1848 else 1849 dev_info(dev, "PUPD: 0x%04x\n", val); 1850 1851 r = core->read(core, WL1273_CHANL_SET, &val); 1852 if (r) 1853 dev_err(dev, "%s: Get CHANL fails.\n", __func__); 1854 else 1855 dev_info(dev, "Tx frequency: %dkHz\n", val*10); 1856 } else if (core->mode == WL1273_MODE_RX) { 1857 int bf = radio->rangelow; 1858 1859 r = core->read(core, WL1273_FREQ_SET, &val); 1860 if (r) 1861 dev_err(dev, "%s: Get FREQ fails.\n", __func__); 1862 else 1863 dev_info(dev, "RX Frequency: %dkHz\n", bf + val*50); 1864 1865 r = core->read(core, WL1273_MOST_MODE_SET, &val); 1866 if (r) 1867 dev_err(dev, "%s: Get MOST_MODE fails.\n", 1868 __func__); 1869 else if (val == 0) 1870 dev_info(dev, "MOST_MODE: Stereo according to blend\n"); 1871 else if (val == 1) 1872 dev_info(dev, "MOST_MODE: Force mono output\n"); 1873 else 1874 dev_info(dev, "MOST_MODE: Unexpected value: %d\n", val); 1875 1876 r = core->read(core, WL1273_MOST_BLEND_SET, &val); 1877 if (r) 1878 dev_err(dev, "%s: Get MOST_BLEND fails.\n", __func__); 1879 else if (val == 0) 1880 dev_info(dev, 1881 "MOST_BLEND: Switched blend & hysteresis.\n"); 1882 else if (val == 1) 1883 dev_info(dev, "MOST_BLEND: Soft blend.\n"); 1884 else 1885 dev_info(dev, "MOST_BLEND: Unexpected val: %d\n", val); 1886 1887 r = core->read(core, WL1273_STEREO_GET, &val); 1888 if (r) 1889 dev_err(dev, "%s: Get STEREO fails.\n", __func__); 1890 else if (val == 0) 1891 dev_info(dev, "STEREO: Not detected\n"); 1892 else if (val == 1) 1893 dev_info(dev, "STEREO: Detected\n"); 1894 else 1895 dev_info(dev, "STEREO: Unexpected value: %d\n", val); 1896 1897 r = core->read(core, WL1273_RSSI_LVL_GET, &val); 1898 if (r) 1899 dev_err(dev, "%s: Get RSSI_LVL fails.\n", __func__); 1900 else 1901 dev_info(dev, "RX signal strength: %d\n", (s16) val); 1902 1903 r = core->read(core, WL1273_POWER_SET, &val); 1904 if (r) 1905 dev_err(dev, "%s: Get POWER fails.\n", __func__); 1906 else 1907 dev_info(dev, "POWER: 0x%04x\n", val); 1908 1909 r = core->read(core, WL1273_INT_MASK_SET, &val); 1910 if (r) 1911 dev_err(dev, "%s: Get INT_MASK fails.\n", __func__); 1912 else 1913 dev_info(dev, "INT_MASK: 0x%04x\n", val); 1914 1915 r = core->read(core, WL1273_RDS_SYNC_GET, &val); 1916 if (r) 1917 dev_err(dev, "%s: Get RDS_SYNC fails.\n", 1918 __func__); 1919 else if (val == 0) 1920 dev_info(dev, "RDS_SYNC: Not synchronized\n"); 1921 1922 else if (val == 1) 1923 dev_info(dev, "RDS_SYNC: Synchronized\n"); 1924 else 1925 dev_info(dev, "RDS_SYNC: Unexpected value: %d\n", val); 1926 1927 r = core->read(core, WL1273_I2S_MODE_CONFIG_SET, &val); 1928 if (r) 1929 dev_err(dev, "%s: Get I2S_MODE_CONFIG fails.\n", 1930 __func__); 1931 else 1932 dev_info(dev, "I2S_MODE_CONFIG: 0x%04x\n", val); 1933 1934 r = core->read(core, WL1273_VOLUME_SET, &val); 1935 if (r) 1936 dev_err(dev, "%s: Get VOLUME fails.\n", __func__); 1937 else 1938 dev_info(dev, "VOLUME: 0x%04x\n", val); 1939 } 1940 1941 return 0; 1942 } 1943 1944 static void wl1273_vdev_release(struct video_device *dev) 1945 { 1946 } 1947 1948 static const struct v4l2_ctrl_ops wl1273_ctrl_ops = { 1949 .s_ctrl = wl1273_fm_vidioc_s_ctrl, 1950 .g_volatile_ctrl = wl1273_fm_g_volatile_ctrl, 1951 }; 1952 1953 static const struct v4l2_ioctl_ops wl1273_ioctl_ops = { 1954 .vidioc_querycap = wl1273_fm_vidioc_querycap, 1955 .vidioc_g_input = wl1273_fm_vidioc_g_input, 1956 .vidioc_s_input = wl1273_fm_vidioc_s_input, 1957 .vidioc_g_audio = wl1273_fm_vidioc_g_audio, 1958 .vidioc_s_audio = wl1273_fm_vidioc_s_audio, 1959 .vidioc_g_tuner = wl1273_fm_vidioc_g_tuner, 1960 .vidioc_s_tuner = wl1273_fm_vidioc_s_tuner, 1961 .vidioc_g_frequency = wl1273_fm_vidioc_g_frequency, 1962 .vidioc_s_frequency = wl1273_fm_vidioc_s_frequency, 1963 .vidioc_s_hw_freq_seek = wl1273_fm_vidioc_s_hw_freq_seek, 1964 .vidioc_g_modulator = wl1273_fm_vidioc_g_modulator, 1965 .vidioc_s_modulator = wl1273_fm_vidioc_s_modulator, 1966 .vidioc_log_status = wl1273_fm_vidioc_log_status, 1967 }; 1968 1969 static const struct video_device wl1273_viddev_template = { 1970 .fops = &wl1273_fops, 1971 .ioctl_ops = &wl1273_ioctl_ops, 1972 .name = WL1273_FM_DRIVER_NAME, 1973 .release = wl1273_vdev_release, 1974 .vfl_dir = VFL_DIR_TX, 1975 .device_caps = V4L2_CAP_HW_FREQ_SEEK | V4L2_CAP_TUNER | 1976 V4L2_CAP_RADIO | V4L2_CAP_AUDIO | 1977 V4L2_CAP_RDS_CAPTURE | V4L2_CAP_MODULATOR | 1978 V4L2_CAP_RDS_OUTPUT, 1979 }; 1980 1981 static int wl1273_fm_radio_remove(struct platform_device *pdev) 1982 { 1983 struct wl1273_device *radio = platform_get_drvdata(pdev); 1984 struct wl1273_core *core = radio->core; 1985 1986 dev_info(&pdev->dev, "%s.\n", __func__); 1987 1988 free_irq(core->client->irq, radio); 1989 core->pdata->free_resources(); 1990 1991 v4l2_ctrl_handler_free(&radio->ctrl_handler); 1992 video_unregister_device(&radio->videodev); 1993 v4l2_device_unregister(&radio->v4l2dev); 1994 1995 return 0; 1996 } 1997 1998 static int wl1273_fm_radio_probe(struct platform_device *pdev) 1999 { 2000 struct wl1273_core **core = pdev->dev.platform_data; 2001 struct wl1273_device *radio; 2002 struct v4l2_ctrl *ctrl; 2003 int r = 0; 2004 2005 pr_debug("%s\n", __func__); 2006 2007 if (!core) { 2008 dev_err(&pdev->dev, "No platform data.\n"); 2009 r = -EINVAL; 2010 goto pdata_err; 2011 } 2012 2013 radio = devm_kzalloc(&pdev->dev, sizeof(*radio), GFP_KERNEL); 2014 if (!radio) { 2015 r = -ENOMEM; 2016 goto pdata_err; 2017 } 2018 2019 /* RDS buffer allocation */ 2020 radio->buf_size = rds_buf * RDS_BLOCK_SIZE; 2021 radio->buffer = devm_kzalloc(&pdev->dev, radio->buf_size, GFP_KERNEL); 2022 if (!radio->buffer) { 2023 pr_err("Cannot allocate memory for RDS buffer.\n"); 2024 r = -ENOMEM; 2025 goto pdata_err; 2026 } 2027 2028 radio->core = *core; 2029 radio->irq_flags = WL1273_IRQ_MASK; 2030 radio->dev = &radio->core->client->dev; 2031 radio->rds_on = false; 2032 radio->core->mode = WL1273_MODE_OFF; 2033 radio->tx_power = 118; 2034 radio->core->audio_mode = WL1273_AUDIO_ANALOG; 2035 radio->band = WL1273_BAND_OTHER; 2036 radio->core->i2s_mode = WL1273_I2S_DEF_MODE; 2037 radio->core->channel_number = 2; 2038 radio->core->volume = WL1273_DEFAULT_VOLUME; 2039 radio->rx_frequency = WL1273_BAND_OTHER_LOW; 2040 radio->tx_frequency = WL1273_BAND_OTHER_HIGH; 2041 radio->rangelow = WL1273_BAND_OTHER_LOW; 2042 radio->rangehigh = WL1273_BAND_OTHER_HIGH; 2043 radio->stereo = true; 2044 radio->bus_type = "I2C"; 2045 2046 if (radio->core->pdata->request_resources) { 2047 r = radio->core->pdata->request_resources(radio->core->client); 2048 if (r) { 2049 dev_err(radio->dev, WL1273_FM_DRIVER_NAME 2050 ": Cannot get platform data\n"); 2051 goto pdata_err; 2052 } 2053 2054 dev_dbg(radio->dev, "irq: %d\n", radio->core->client->irq); 2055 2056 r = request_threaded_irq(radio->core->client->irq, NULL, 2057 wl1273_fm_irq_thread_handler, 2058 IRQF_ONESHOT | IRQF_TRIGGER_FALLING, 2059 "wl1273-fm", radio); 2060 if (r < 0) { 2061 dev_err(radio->dev, WL1273_FM_DRIVER_NAME 2062 ": Unable to register IRQ handler: %d\n", r); 2063 goto err_request_irq; 2064 } 2065 } else { 2066 dev_err(radio->dev, WL1273_FM_DRIVER_NAME ": Core WL1273 IRQ not configured"); 2067 r = -EINVAL; 2068 goto pdata_err; 2069 } 2070 2071 init_completion(&radio->busy); 2072 init_waitqueue_head(&radio->read_queue); 2073 2074 radio->write_buf = devm_kzalloc(&pdev->dev, 256, GFP_KERNEL); 2075 if (!radio->write_buf) { 2076 r = -ENOMEM; 2077 goto write_buf_err; 2078 } 2079 2080 radio->dev = &pdev->dev; 2081 radio->v4l2dev.ctrl_handler = &radio->ctrl_handler; 2082 radio->rds_users = 0; 2083 2084 r = v4l2_device_register(&pdev->dev, &radio->v4l2dev); 2085 if (r) { 2086 dev_err(&pdev->dev, "Cannot register v4l2_device.\n"); 2087 goto write_buf_err; 2088 } 2089 2090 /* V4L2 configuration */ 2091 radio->videodev = wl1273_viddev_template; 2092 2093 radio->videodev.v4l2_dev = &radio->v4l2dev; 2094 2095 v4l2_ctrl_handler_init(&radio->ctrl_handler, 6); 2096 2097 /* add in ascending ID order */ 2098 v4l2_ctrl_new_std(&radio->ctrl_handler, &wl1273_ctrl_ops, 2099 V4L2_CID_AUDIO_VOLUME, 0, WL1273_MAX_VOLUME, 1, 2100 WL1273_DEFAULT_VOLUME); 2101 2102 v4l2_ctrl_new_std(&radio->ctrl_handler, &wl1273_ctrl_ops, 2103 V4L2_CID_AUDIO_MUTE, 0, 1, 1, 1); 2104 2105 v4l2_ctrl_new_std_menu(&radio->ctrl_handler, &wl1273_ctrl_ops, 2106 V4L2_CID_TUNE_PREEMPHASIS, 2107 V4L2_PREEMPHASIS_75_uS, 0x03, 2108 V4L2_PREEMPHASIS_50_uS); 2109 2110 v4l2_ctrl_new_std(&radio->ctrl_handler, &wl1273_ctrl_ops, 2111 V4L2_CID_TUNE_POWER_LEVEL, 91, 122, 1, 118); 2112 2113 ctrl = v4l2_ctrl_new_std(&radio->ctrl_handler, &wl1273_ctrl_ops, 2114 V4L2_CID_TUNE_ANTENNA_CAPACITOR, 2115 0, 255, 1, 255); 2116 if (ctrl) 2117 ctrl->flags |= V4L2_CTRL_FLAG_VOLATILE; 2118 2119 if (radio->ctrl_handler.error) { 2120 r = radio->ctrl_handler.error; 2121 dev_err(&pdev->dev, "Ctrl handler error: %d\n", r); 2122 goto handler_init_err; 2123 } 2124 2125 video_set_drvdata(&radio->videodev, radio); 2126 platform_set_drvdata(pdev, radio); 2127 2128 /* register video device */ 2129 r = video_register_device(&radio->videodev, VFL_TYPE_RADIO, radio_nr); 2130 if (r) { 2131 dev_err(&pdev->dev, WL1273_FM_DRIVER_NAME 2132 ": Could not register video device\n"); 2133 goto handler_init_err; 2134 } 2135 2136 return 0; 2137 2138 handler_init_err: 2139 v4l2_ctrl_handler_free(&radio->ctrl_handler); 2140 v4l2_device_unregister(&radio->v4l2dev); 2141 write_buf_err: 2142 free_irq(radio->core->client->irq, radio); 2143 err_request_irq: 2144 radio->core->pdata->free_resources(); 2145 pdata_err: 2146 return r; 2147 } 2148 2149 static struct platform_driver wl1273_fm_radio_driver = { 2150 .probe = wl1273_fm_radio_probe, 2151 .remove = wl1273_fm_radio_remove, 2152 .driver = { 2153 .name = "wl1273_fm_radio", 2154 }, 2155 }; 2156 2157 module_platform_driver(wl1273_fm_radio_driver); 2158 2159 MODULE_AUTHOR("Matti Aaltonen <matti.j.aaltonen@nokia.com>"); 2160 MODULE_DESCRIPTION(DRIVER_DESC); 2161 MODULE_LICENSE("GPL"); 2162 MODULE_ALIAS("platform:wl1273_fm_radio"); 2163