1 /* 2 * drivers/media/radio/si4713-i2c.c 3 * 4 * Silicon Labs Si4713 FM Radio Transmitter I2C commands. 5 * 6 * Copyright (c) 2009 Nokia Corporation 7 * Contact: Eduardo Valentin <eduardo.valentin@nokia.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 * You should have received a copy of the GNU General Public License 20 * along with this program; if not, write to the Free Software 21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 22 */ 23 24 #include <linux/completion.h> 25 #include <linux/delay.h> 26 #include <linux/interrupt.h> 27 #include <linux/i2c.h> 28 #include <linux/slab.h> 29 #include <linux/gpio.h> 30 #include <linux/module.h> 31 #include <media/v4l2-device.h> 32 #include <media/v4l2-ioctl.h> 33 #include <media/v4l2-common.h> 34 35 #include "si4713.h" 36 37 /* module parameters */ 38 static int debug; 39 module_param(debug, int, S_IRUGO | S_IWUSR); 40 MODULE_PARM_DESC(debug, "Debug level (0 - 2)"); 41 42 MODULE_LICENSE("GPL"); 43 MODULE_AUTHOR("Eduardo Valentin <eduardo.valentin@nokia.com>"); 44 MODULE_DESCRIPTION("I2C driver for Si4713 FM Radio Transmitter"); 45 MODULE_VERSION("0.0.1"); 46 47 #define DEFAULT_RDS_PI 0x00 48 #define DEFAULT_RDS_PTY 0x00 49 #define DEFAULT_RDS_DEVIATION 0x00C8 50 #define DEFAULT_RDS_PS_REPEAT_COUNT 0x0003 51 #define DEFAULT_LIMITER_RTIME 0x1392 52 #define DEFAULT_LIMITER_DEV 0x102CA 53 #define DEFAULT_PILOT_FREQUENCY 0x4A38 54 #define DEFAULT_PILOT_DEVIATION 0x1A5E 55 #define DEFAULT_ACOMP_ATIME 0x0000 56 #define DEFAULT_ACOMP_RTIME 0xF4240L 57 #define DEFAULT_ACOMP_GAIN 0x0F 58 #define DEFAULT_ACOMP_THRESHOLD (-0x28) 59 #define DEFAULT_MUTE 0x01 60 #define DEFAULT_POWER_LEVEL 88 61 #define DEFAULT_FREQUENCY 8800 62 #define DEFAULT_PREEMPHASIS FMPE_EU 63 #define DEFAULT_TUNE_RNL 0xFF 64 65 #define to_si4713_device(sd) container_of(sd, struct si4713_device, sd) 66 67 /* frequency domain transformation (using times 10 to avoid floats) */ 68 #define FREQDEV_UNIT 100000 69 #define FREQV4L2_MULTI 625 70 #define si4713_to_v4l2(f) ((f * FREQDEV_UNIT) / FREQV4L2_MULTI) 71 #define v4l2_to_si4713(f) ((f * FREQV4L2_MULTI) / FREQDEV_UNIT) 72 #define FREQ_RANGE_LOW 7600 73 #define FREQ_RANGE_HIGH 10800 74 75 #define MAX_ARGS 7 76 77 #define RDS_BLOCK 8 78 #define RDS_BLOCK_CLEAR 0x03 79 #define RDS_BLOCK_LOAD 0x04 80 #define RDS_RADIOTEXT_2A 0x20 81 #define RDS_RADIOTEXT_BLK_SIZE 4 82 #define RDS_RADIOTEXT_INDEX_MAX 0x0F 83 #define RDS_CARRIAGE_RETURN 0x0D 84 85 #define rds_ps_nblocks(len) ((len / RDS_BLOCK) + (len % RDS_BLOCK ? 1 : 0)) 86 87 #define get_status_bit(p, b, m) (((p) & (m)) >> (b)) 88 #define set_bits(p, v, b, m) (((p) & ~(m)) | ((v) << (b))) 89 90 #define ATTACK_TIME_UNIT 500 91 92 #define POWER_OFF 0x00 93 #define POWER_ON 0x01 94 95 #define msb(x) ((u8)((u16) x >> 8)) 96 #define lsb(x) ((u8)((u16) x & 0x00FF)) 97 #define compose_u16(msb, lsb) (((u16)msb << 8) | lsb) 98 #define check_command_failed(status) (!(status & SI4713_CTS) || \ 99 (status & SI4713_ERR)) 100 /* mute definition */ 101 #define set_mute(p) ((p & 1) | ((p & 1) << 1)); 102 103 #ifdef DEBUG 104 #define DBG_BUFFER(device, message, buffer, size) \ 105 { \ 106 int i; \ 107 char str[(size)*5]; \ 108 for (i = 0; i < size; i++) \ 109 sprintf(str + i * 5, " 0x%02x", buffer[i]); \ 110 v4l2_dbg(2, debug, device, "%s:%s\n", message, str); \ 111 } 112 #else 113 #define DBG_BUFFER(device, message, buffer, size) 114 #endif 115 116 /* 117 * Values for limiter release time (sorted by second column) 118 * device release 119 * value time (us) 120 */ 121 static long limiter_times[] = { 122 2000, 250, 123 1000, 500, 124 510, 1000, 125 255, 2000, 126 170, 3000, 127 127, 4020, 128 102, 5010, 129 85, 6020, 130 73, 7010, 131 64, 7990, 132 57, 8970, 133 51, 10030, 134 25, 20470, 135 17, 30110, 136 13, 39380, 137 10, 51190, 138 8, 63690, 139 7, 73140, 140 6, 85330, 141 5, 102390, 142 }; 143 144 /* 145 * Values for audio compression release time (sorted by second column) 146 * device release 147 * value time (us) 148 */ 149 static unsigned long acomp_rtimes[] = { 150 0, 100000, 151 1, 200000, 152 2, 350000, 153 3, 525000, 154 4, 1000000, 155 }; 156 157 /* 158 * Values for preemphasis (sorted by second column) 159 * device preemphasis 160 * value value (v4l2) 161 */ 162 static unsigned long preemphasis_values[] = { 163 FMPE_DISABLED, V4L2_PREEMPHASIS_DISABLED, 164 FMPE_EU, V4L2_PREEMPHASIS_50_uS, 165 FMPE_USA, V4L2_PREEMPHASIS_75_uS, 166 }; 167 168 static int usecs_to_dev(unsigned long usecs, unsigned long const array[], 169 int size) 170 { 171 int i; 172 int rval = -EINVAL; 173 174 for (i = 0; i < size / 2; i++) 175 if (array[(i * 2) + 1] >= usecs) { 176 rval = array[i * 2]; 177 break; 178 } 179 180 return rval; 181 } 182 183 /* si4713_handler: IRQ handler, just complete work */ 184 static irqreturn_t si4713_handler(int irq, void *dev) 185 { 186 struct si4713_device *sdev = dev; 187 188 v4l2_dbg(2, debug, &sdev->sd, 189 "%s: sending signal to completion work.\n", __func__); 190 complete(&sdev->work); 191 192 return IRQ_HANDLED; 193 } 194 195 /* 196 * si4713_send_command - sends a command to si4713 and waits its response 197 * @sdev: si4713_device structure for the device we are communicating 198 * @command: command id 199 * @args: command arguments we are sending (up to 7) 200 * @argn: actual size of @args 201 * @response: buffer to place the expected response from the device (up to 15) 202 * @respn: actual size of @response 203 * @usecs: amount of time to wait before reading the response (in usecs) 204 */ 205 static int si4713_send_command(struct si4713_device *sdev, const u8 command, 206 const u8 args[], const int argn, 207 u8 response[], const int respn, const int usecs) 208 { 209 struct i2c_client *client = v4l2_get_subdevdata(&sdev->sd); 210 unsigned long until_jiffies; 211 u8 data1[MAX_ARGS + 1]; 212 int err; 213 214 if (!client->adapter) 215 return -ENODEV; 216 217 /* First send the command and its arguments */ 218 data1[0] = command; 219 memcpy(data1 + 1, args, argn); 220 DBG_BUFFER(&sdev->sd, "Parameters", data1, argn + 1); 221 222 err = i2c_master_send(client, data1, argn + 1); 223 if (err != argn + 1) { 224 v4l2_err(&sdev->sd, "Error while sending command 0x%02x\n", 225 command); 226 return err < 0 ? err : -EIO; 227 } 228 229 until_jiffies = jiffies + usecs_to_jiffies(usecs) + 1; 230 231 /* Wait response from interrupt */ 232 if (client->irq) { 233 if (!wait_for_completion_timeout(&sdev->work, 234 usecs_to_jiffies(usecs) + 1)) 235 v4l2_warn(&sdev->sd, 236 "(%s) Device took too much time to answer.\n", 237 __func__); 238 } 239 240 do { 241 err = i2c_master_recv(client, response, respn); 242 if (err != respn) { 243 v4l2_err(&sdev->sd, 244 "Error %d while reading response for command 0x%02x\n", 245 err, command); 246 return err < 0 ? err : -EIO; 247 } 248 249 DBG_BUFFER(&sdev->sd, "Response", response, respn); 250 if (!check_command_failed(response[0])) 251 return 0; 252 253 if (client->irq) 254 return -EBUSY; 255 if (usecs <= 1000) 256 usleep_range(usecs, 1000); 257 else 258 usleep_range(1000, 2000); 259 } while (time_is_after_jiffies(until_jiffies)); 260 261 return -EBUSY; 262 } 263 264 /* 265 * si4713_read_property - reads a si4713 property 266 * @sdev: si4713_device structure for the device we are communicating 267 * @prop: property identification number 268 * @pv: property value to be returned on success 269 */ 270 static int si4713_read_property(struct si4713_device *sdev, u16 prop, u32 *pv) 271 { 272 int err; 273 u8 val[SI4713_GET_PROP_NRESP]; 274 /* 275 * .First byte = 0 276 * .Second byte = property's MSB 277 * .Third byte = property's LSB 278 */ 279 const u8 args[SI4713_GET_PROP_NARGS] = { 280 0x00, 281 msb(prop), 282 lsb(prop), 283 }; 284 285 err = si4713_send_command(sdev, SI4713_CMD_GET_PROPERTY, 286 args, ARRAY_SIZE(args), val, 287 ARRAY_SIZE(val), DEFAULT_TIMEOUT); 288 289 if (err < 0) 290 return err; 291 292 *pv = compose_u16(val[2], val[3]); 293 294 v4l2_dbg(1, debug, &sdev->sd, 295 "%s: property=0x%02x value=0x%02x status=0x%02x\n", 296 __func__, prop, *pv, val[0]); 297 298 return err; 299 } 300 301 /* 302 * si4713_write_property - modifies a si4713 property 303 * @sdev: si4713_device structure for the device we are communicating 304 * @prop: property identification number 305 * @val: new value for that property 306 */ 307 static int si4713_write_property(struct si4713_device *sdev, u16 prop, u16 val) 308 { 309 int rval; 310 u8 resp[SI4713_SET_PROP_NRESP]; 311 /* 312 * .First byte = 0 313 * .Second byte = property's MSB 314 * .Third byte = property's LSB 315 * .Fourth byte = value's MSB 316 * .Fifth byte = value's LSB 317 */ 318 const u8 args[SI4713_SET_PROP_NARGS] = { 319 0x00, 320 msb(prop), 321 lsb(prop), 322 msb(val), 323 lsb(val), 324 }; 325 326 rval = si4713_send_command(sdev, SI4713_CMD_SET_PROPERTY, 327 args, ARRAY_SIZE(args), 328 resp, ARRAY_SIZE(resp), 329 DEFAULT_TIMEOUT); 330 331 if (rval < 0) 332 return rval; 333 334 v4l2_dbg(1, debug, &sdev->sd, 335 "%s: property=0x%02x value=0x%02x status=0x%02x\n", 336 __func__, prop, val, resp[0]); 337 338 /* 339 * As there is no command response for SET_PROPERTY, 340 * wait Tcomp time to finish before proceed, in order 341 * to have property properly set. 342 */ 343 msleep(TIMEOUT_SET_PROPERTY); 344 345 return rval; 346 } 347 348 /* 349 * si4713_powerup - Powers the device up 350 * @sdev: si4713_device structure for the device we are communicating 351 */ 352 static int si4713_powerup(struct si4713_device *sdev) 353 { 354 struct i2c_client *client = v4l2_get_subdevdata(&sdev->sd); 355 int err; 356 u8 resp[SI4713_PWUP_NRESP]; 357 /* 358 * .First byte = Enabled interrupts and boot function 359 * .Second byte = Input operation mode 360 */ 361 u8 args[SI4713_PWUP_NARGS] = { 362 SI4713_PWUP_GPO2OEN | SI4713_PWUP_FUNC_TX, 363 SI4713_PWUP_OPMOD_ANALOG, 364 }; 365 366 if (sdev->power_state) 367 return 0; 368 369 if (sdev->supplies) { 370 err = regulator_bulk_enable(sdev->supplies, sdev->supply_data); 371 if (err) { 372 v4l2_err(&sdev->sd, "Failed to enable supplies: %d\n", err); 373 return err; 374 } 375 } 376 if (gpio_is_valid(sdev->gpio_reset)) { 377 udelay(50); 378 gpio_set_value(sdev->gpio_reset, 1); 379 } 380 381 if (client->irq) 382 args[0] |= SI4713_PWUP_CTSIEN; 383 384 err = si4713_send_command(sdev, SI4713_CMD_POWER_UP, 385 args, ARRAY_SIZE(args), 386 resp, ARRAY_SIZE(resp), 387 TIMEOUT_POWER_UP); 388 389 if (!err) { 390 v4l2_dbg(1, debug, &sdev->sd, "Powerup response: 0x%02x\n", 391 resp[0]); 392 v4l2_dbg(1, debug, &sdev->sd, "Device in power up mode\n"); 393 sdev->power_state = POWER_ON; 394 395 if (client->irq) 396 err = si4713_write_property(sdev, SI4713_GPO_IEN, 397 SI4713_STC_INT | SI4713_CTS); 398 return err; 399 } 400 if (gpio_is_valid(sdev->gpio_reset)) 401 gpio_set_value(sdev->gpio_reset, 0); 402 if (sdev->supplies) { 403 err = regulator_bulk_disable(sdev->supplies, sdev->supply_data); 404 if (err) 405 v4l2_err(&sdev->sd, 406 "Failed to disable supplies: %d\n", err); 407 } 408 409 return err; 410 } 411 412 /* 413 * si4713_powerdown - Powers the device down 414 * @sdev: si4713_device structure for the device we are communicating 415 */ 416 static int si4713_powerdown(struct si4713_device *sdev) 417 { 418 int err; 419 u8 resp[SI4713_PWDN_NRESP]; 420 421 if (!sdev->power_state) 422 return 0; 423 424 err = si4713_send_command(sdev, SI4713_CMD_POWER_DOWN, 425 NULL, 0, 426 resp, ARRAY_SIZE(resp), 427 DEFAULT_TIMEOUT); 428 429 if (!err) { 430 v4l2_dbg(1, debug, &sdev->sd, "Power down response: 0x%02x\n", 431 resp[0]); 432 v4l2_dbg(1, debug, &sdev->sd, "Device in reset mode\n"); 433 if (gpio_is_valid(sdev->gpio_reset)) 434 gpio_set_value(sdev->gpio_reset, 0); 435 if (sdev->supplies) { 436 err = regulator_bulk_disable(sdev->supplies, 437 sdev->supply_data); 438 if (err) 439 v4l2_err(&sdev->sd, 440 "Failed to disable supplies: %d\n", err); 441 } 442 sdev->power_state = POWER_OFF; 443 } 444 445 return err; 446 } 447 448 /* 449 * si4713_checkrev - Checks if we are treating a device with the correct rev. 450 * @sdev: si4713_device structure for the device we are communicating 451 */ 452 static int si4713_checkrev(struct si4713_device *sdev) 453 { 454 struct i2c_client *client = v4l2_get_subdevdata(&sdev->sd); 455 int rval; 456 u8 resp[SI4713_GETREV_NRESP]; 457 458 rval = si4713_send_command(sdev, SI4713_CMD_GET_REV, 459 NULL, 0, 460 resp, ARRAY_SIZE(resp), 461 DEFAULT_TIMEOUT); 462 463 if (rval < 0) 464 return rval; 465 466 if (resp[1] == SI4713_PRODUCT_NUMBER) { 467 v4l2_info(&sdev->sd, "chip found @ 0x%02x (%s)\n", 468 client->addr << 1, client->adapter->name); 469 } else { 470 v4l2_err(&sdev->sd, "Invalid product number 0x%X\n", resp[1]); 471 rval = -EINVAL; 472 } 473 return rval; 474 } 475 476 /* 477 * si4713_wait_stc - Waits STC interrupt and clears status bits. Useful 478 * for TX_TUNE_POWER, TX_TUNE_FREQ and TX_TUNE_MEAS 479 * @sdev: si4713_device structure for the device we are communicating 480 * @usecs: timeout to wait for STC interrupt signal 481 */ 482 static int si4713_wait_stc(struct si4713_device *sdev, const int usecs) 483 { 484 struct i2c_client *client = v4l2_get_subdevdata(&sdev->sd); 485 u8 resp[SI4713_GET_STATUS_NRESP]; 486 unsigned long start_jiffies = jiffies; 487 int err; 488 489 if (client->irq && 490 !wait_for_completion_timeout(&sdev->work, usecs_to_jiffies(usecs) + 1)) 491 v4l2_warn(&sdev->sd, 492 "(%s) Device took too much time to answer.\n", __func__); 493 494 for (;;) { 495 /* Clear status bits */ 496 err = si4713_send_command(sdev, SI4713_CMD_GET_INT_STATUS, 497 NULL, 0, 498 resp, ARRAY_SIZE(resp), 499 DEFAULT_TIMEOUT); 500 /* The USB device returns errors when it waits for the 501 * STC bit to be set. Hence polling */ 502 if (err >= 0) { 503 v4l2_dbg(1, debug, &sdev->sd, 504 "%s: status bits: 0x%02x\n", __func__, resp[0]); 505 506 if (resp[0] & SI4713_STC_INT) 507 return 0; 508 } 509 if (jiffies_to_usecs(jiffies - start_jiffies) > usecs) 510 return err < 0 ? err : -EIO; 511 /* We sleep here for 3-4 ms in order to avoid flooding the device 512 * with USB requests. The si4713 USB driver was developed 513 * by reverse engineering the Windows USB driver. The windows 514 * driver also has a ~2.5 ms delay between responses. */ 515 usleep_range(3000, 4000); 516 } 517 } 518 519 /* 520 * si4713_tx_tune_freq - Sets the state of the RF carrier and sets the tuning 521 * frequency between 76 and 108 MHz in 10 kHz units and 522 * steps of 50 kHz. 523 * @sdev: si4713_device structure for the device we are communicating 524 * @frequency: desired frequency (76 - 108 MHz, unit 10 KHz, step 50 kHz) 525 */ 526 static int si4713_tx_tune_freq(struct si4713_device *sdev, u16 frequency) 527 { 528 int err; 529 u8 val[SI4713_TXFREQ_NRESP]; 530 /* 531 * .First byte = 0 532 * .Second byte = frequency's MSB 533 * .Third byte = frequency's LSB 534 */ 535 const u8 args[SI4713_TXFREQ_NARGS] = { 536 0x00, 537 msb(frequency), 538 lsb(frequency), 539 }; 540 541 err = si4713_send_command(sdev, SI4713_CMD_TX_TUNE_FREQ, 542 args, ARRAY_SIZE(args), val, 543 ARRAY_SIZE(val), DEFAULT_TIMEOUT); 544 545 if (err < 0) 546 return err; 547 548 v4l2_dbg(1, debug, &sdev->sd, 549 "%s: frequency=0x%02x status=0x%02x\n", __func__, 550 frequency, val[0]); 551 552 err = si4713_wait_stc(sdev, TIMEOUT_TX_TUNE); 553 if (err < 0) 554 return err; 555 556 return compose_u16(args[1], args[2]); 557 } 558 559 /* 560 * si4713_tx_tune_power - Sets the RF voltage level between 88 and 120 dBuV in 561 * 1 dB units. A value of 0x00 indicates off. The command 562 * also sets the antenna tuning capacitance. A value of 0 563 * indicates autotuning, and a value of 1 - 191 indicates 564 * a manual override, which results in a tuning 565 * capacitance of 0.25 pF x @antcap. 566 * @sdev: si4713_device structure for the device we are communicating 567 * @power: tuning power (88 - 120 dBuV, unit/step 1 dB) 568 * @antcap: value of antenna tuning capacitor (0 - 191) 569 */ 570 static int si4713_tx_tune_power(struct si4713_device *sdev, u8 power, 571 u8 antcap) 572 { 573 int err; 574 u8 val[SI4713_TXPWR_NRESP]; 575 /* 576 * .First byte = 0 577 * .Second byte = 0 578 * .Third byte = power 579 * .Fourth byte = antcap 580 */ 581 u8 args[SI4713_TXPWR_NARGS] = { 582 0x00, 583 0x00, 584 power, 585 antcap, 586 }; 587 588 /* Map power values 1-87 to MIN_POWER (88) */ 589 if (power > 0 && power < SI4713_MIN_POWER) 590 args[2] = power = SI4713_MIN_POWER; 591 592 err = si4713_send_command(sdev, SI4713_CMD_TX_TUNE_POWER, 593 args, ARRAY_SIZE(args), val, 594 ARRAY_SIZE(val), DEFAULT_TIMEOUT); 595 596 if (err < 0) 597 return err; 598 599 v4l2_dbg(1, debug, &sdev->sd, 600 "%s: power=0x%02x antcap=0x%02x status=0x%02x\n", 601 __func__, power, antcap, val[0]); 602 603 return si4713_wait_stc(sdev, TIMEOUT_TX_TUNE_POWER); 604 } 605 606 /* 607 * si4713_tx_tune_measure - Enters receive mode and measures the received noise 608 * level in units of dBuV on the selected frequency. 609 * The Frequency must be between 76 and 108 MHz in 10 kHz 610 * units and steps of 50 kHz. The command also sets the 611 * antenna tuning capacitance. A value of 0 means 612 * autotuning, and a value of 1 to 191 indicates manual 613 * override. 614 * @sdev: si4713_device structure for the device we are communicating 615 * @frequency: desired frequency (76 - 108 MHz, unit 10 KHz, step 50 kHz) 616 * @antcap: value of antenna tuning capacitor (0 - 191) 617 */ 618 static int si4713_tx_tune_measure(struct si4713_device *sdev, u16 frequency, 619 u8 antcap) 620 { 621 int err; 622 u8 val[SI4713_TXMEA_NRESP]; 623 /* 624 * .First byte = 0 625 * .Second byte = frequency's MSB 626 * .Third byte = frequency's LSB 627 * .Fourth byte = antcap 628 */ 629 const u8 args[SI4713_TXMEA_NARGS] = { 630 0x00, 631 msb(frequency), 632 lsb(frequency), 633 antcap, 634 }; 635 636 sdev->tune_rnl = DEFAULT_TUNE_RNL; 637 638 if (antcap > SI4713_MAX_ANTCAP) 639 return -EDOM; 640 641 err = si4713_send_command(sdev, SI4713_CMD_TX_TUNE_MEASURE, 642 args, ARRAY_SIZE(args), val, 643 ARRAY_SIZE(val), DEFAULT_TIMEOUT); 644 645 if (err < 0) 646 return err; 647 648 v4l2_dbg(1, debug, &sdev->sd, 649 "%s: frequency=0x%02x antcap=0x%02x status=0x%02x\n", 650 __func__, frequency, antcap, val[0]); 651 652 return si4713_wait_stc(sdev, TIMEOUT_TX_TUNE); 653 } 654 655 /* 656 * si4713_tx_tune_status- Returns the status of the tx_tune_freq, tx_tune_mea or 657 * tx_tune_power commands. This command return the current 658 * frequency, output voltage in dBuV, the antenna tunning 659 * capacitance value and the received noise level. The 660 * command also clears the stcint interrupt bit when the 661 * first bit of its arguments is high. 662 * @sdev: si4713_device structure for the device we are communicating 663 * @intack: 0x01 to clear the seek/tune complete interrupt status indicator. 664 * @frequency: returned frequency 665 * @power: returned power 666 * @antcap: returned antenna capacitance 667 * @noise: returned noise level 668 */ 669 static int si4713_tx_tune_status(struct si4713_device *sdev, u8 intack, 670 u16 *frequency, u8 *power, 671 u8 *antcap, u8 *noise) 672 { 673 int err; 674 u8 val[SI4713_TXSTATUS_NRESP]; 675 /* 676 * .First byte = intack bit 677 */ 678 const u8 args[SI4713_TXSTATUS_NARGS] = { 679 intack & SI4713_INTACK_MASK, 680 }; 681 682 err = si4713_send_command(sdev, SI4713_CMD_TX_TUNE_STATUS, 683 args, ARRAY_SIZE(args), val, 684 ARRAY_SIZE(val), DEFAULT_TIMEOUT); 685 686 if (!err) { 687 v4l2_dbg(1, debug, &sdev->sd, 688 "%s: status=0x%02x\n", __func__, val[0]); 689 *frequency = compose_u16(val[2], val[3]); 690 sdev->frequency = *frequency; 691 *power = val[5]; 692 *antcap = val[6]; 693 *noise = val[7]; 694 v4l2_dbg(1, debug, &sdev->sd, "%s: response: %d x 10 kHz " 695 "(power %d, antcap %d, rnl %d)\n", __func__, 696 *frequency, *power, *antcap, *noise); 697 } 698 699 return err; 700 } 701 702 /* 703 * si4713_tx_rds_buff - Loads the RDS group buffer FIFO or circular buffer. 704 * @sdev: si4713_device structure for the device we are communicating 705 * @mode: the buffer operation mode. 706 * @rdsb: RDS Block B 707 * @rdsc: RDS Block C 708 * @rdsd: RDS Block D 709 * @cbleft: returns the number of available circular buffer blocks minus the 710 * number of used circular buffer blocks. 711 */ 712 static int si4713_tx_rds_buff(struct si4713_device *sdev, u8 mode, u16 rdsb, 713 u16 rdsc, u16 rdsd, s8 *cbleft) 714 { 715 int err; 716 u8 val[SI4713_RDSBUFF_NRESP]; 717 718 const u8 args[SI4713_RDSBUFF_NARGS] = { 719 mode & SI4713_RDSBUFF_MODE_MASK, 720 msb(rdsb), 721 lsb(rdsb), 722 msb(rdsc), 723 lsb(rdsc), 724 msb(rdsd), 725 lsb(rdsd), 726 }; 727 728 err = si4713_send_command(sdev, SI4713_CMD_TX_RDS_BUFF, 729 args, ARRAY_SIZE(args), val, 730 ARRAY_SIZE(val), DEFAULT_TIMEOUT); 731 732 if (!err) { 733 v4l2_dbg(1, debug, &sdev->sd, 734 "%s: status=0x%02x\n", __func__, val[0]); 735 *cbleft = (s8)val[2] - val[3]; 736 v4l2_dbg(1, debug, &sdev->sd, "%s: response: interrupts" 737 " 0x%02x cb avail: %d cb used %d fifo avail" 738 " %d fifo used %d\n", __func__, val[1], 739 val[2], val[3], val[4], val[5]); 740 } 741 742 return err; 743 } 744 745 /* 746 * si4713_tx_rds_ps - Loads the program service buffer. 747 * @sdev: si4713_device structure for the device we are communicating 748 * @psid: program service id to be loaded. 749 * @pschar: assumed 4 size char array to be loaded into the program service 750 */ 751 static int si4713_tx_rds_ps(struct si4713_device *sdev, u8 psid, 752 unsigned char *pschar) 753 { 754 int err; 755 u8 val[SI4713_RDSPS_NRESP]; 756 757 const u8 args[SI4713_RDSPS_NARGS] = { 758 psid & SI4713_RDSPS_PSID_MASK, 759 pschar[0], 760 pschar[1], 761 pschar[2], 762 pschar[3], 763 }; 764 765 err = si4713_send_command(sdev, SI4713_CMD_TX_RDS_PS, 766 args, ARRAY_SIZE(args), val, 767 ARRAY_SIZE(val), DEFAULT_TIMEOUT); 768 769 if (err < 0) 770 return err; 771 772 v4l2_dbg(1, debug, &sdev->sd, "%s: status=0x%02x\n", __func__, val[0]); 773 774 return err; 775 } 776 777 static int si4713_set_power_state(struct si4713_device *sdev, u8 value) 778 { 779 if (value) 780 return si4713_powerup(sdev); 781 return si4713_powerdown(sdev); 782 } 783 784 static int si4713_set_mute(struct si4713_device *sdev, u16 mute) 785 { 786 int rval = 0; 787 788 mute = set_mute(mute); 789 790 if (sdev->power_state) 791 rval = si4713_write_property(sdev, 792 SI4713_TX_LINE_INPUT_MUTE, mute); 793 794 return rval; 795 } 796 797 static int si4713_set_rds_ps_name(struct si4713_device *sdev, char *ps_name) 798 { 799 int rval = 0, i; 800 u8 len = 0; 801 802 /* We want to clear the whole thing */ 803 if (!strlen(ps_name)) 804 memset(ps_name, 0, MAX_RDS_PS_NAME + 1); 805 806 if (sdev->power_state) { 807 /* Write the new ps name and clear the padding */ 808 for (i = 0; i < MAX_RDS_PS_NAME; i += (RDS_BLOCK / 2)) { 809 rval = si4713_tx_rds_ps(sdev, (i / (RDS_BLOCK / 2)), 810 ps_name + i); 811 if (rval < 0) 812 return rval; 813 } 814 815 /* Setup the size to be sent */ 816 if (strlen(ps_name)) 817 len = strlen(ps_name) - 1; 818 else 819 len = 1; 820 821 rval = si4713_write_property(sdev, 822 SI4713_TX_RDS_PS_MESSAGE_COUNT, 823 rds_ps_nblocks(len)); 824 if (rval < 0) 825 return rval; 826 827 rval = si4713_write_property(sdev, 828 SI4713_TX_RDS_PS_REPEAT_COUNT, 829 DEFAULT_RDS_PS_REPEAT_COUNT * 2); 830 if (rval < 0) 831 return rval; 832 } 833 834 return rval; 835 } 836 837 static int si4713_set_rds_radio_text(struct si4713_device *sdev, const char *rt) 838 { 839 static const char cr[RDS_RADIOTEXT_BLK_SIZE] = { RDS_CARRIAGE_RETURN, 0 }; 840 int rval = 0, i; 841 u16 t_index = 0; 842 u8 b_index = 0, cr_inserted = 0; 843 s8 left; 844 845 if (!sdev->power_state) 846 return rval; 847 848 rval = si4713_tx_rds_buff(sdev, RDS_BLOCK_CLEAR, 0, 0, 0, &left); 849 if (rval < 0) 850 return rval; 851 852 if (!strlen(rt)) 853 return rval; 854 855 do { 856 /* RDS spec says that if the last block isn't used, 857 * then apply a carriage return 858 */ 859 if (t_index < (RDS_RADIOTEXT_INDEX_MAX * RDS_RADIOTEXT_BLK_SIZE)) { 860 for (i = 0; i < RDS_RADIOTEXT_BLK_SIZE; i++) { 861 if (!rt[t_index + i] || 862 rt[t_index + i] == RDS_CARRIAGE_RETURN) { 863 rt = cr; 864 cr_inserted = 1; 865 break; 866 } 867 } 868 } 869 870 rval = si4713_tx_rds_buff(sdev, RDS_BLOCK_LOAD, 871 compose_u16(RDS_RADIOTEXT_2A, b_index++), 872 compose_u16(rt[t_index], rt[t_index + 1]), 873 compose_u16(rt[t_index + 2], rt[t_index + 3]), 874 &left); 875 if (rval < 0) 876 return rval; 877 878 t_index += RDS_RADIOTEXT_BLK_SIZE; 879 880 if (cr_inserted) 881 break; 882 } while (left > 0); 883 884 return rval; 885 } 886 887 /* 888 * si4713_update_tune_status - update properties from tx_tune_status 889 * command. Must be called with sdev->mutex held. 890 * @sdev: si4713_device structure for the device we are communicating 891 */ 892 static int si4713_update_tune_status(struct si4713_device *sdev) 893 { 894 int rval; 895 u16 f = 0; 896 u8 p = 0, a = 0, n = 0; 897 898 rval = si4713_tx_tune_status(sdev, 0x00, &f, &p, &a, &n); 899 900 if (rval < 0) 901 goto exit; 902 903 /* TODO: check that power_level and antenna_capacitor really are not 904 changed by the hardware. If they are, then these controls should become 905 volatiles. 906 sdev->power_level = p; 907 sdev->antenna_capacitor = a;*/ 908 sdev->tune_rnl = n; 909 910 exit: 911 return rval; 912 } 913 914 static int si4713_choose_econtrol_action(struct si4713_device *sdev, u32 id, 915 s32 *bit, s32 *mask, u16 *property, int *mul, 916 unsigned long **table, int *size) 917 { 918 s32 rval = 0; 919 920 switch (id) { 921 /* FM_TX class controls */ 922 case V4L2_CID_RDS_TX_PI: 923 *property = SI4713_TX_RDS_PI; 924 *mul = 1; 925 break; 926 case V4L2_CID_AUDIO_COMPRESSION_THRESHOLD: 927 *property = SI4713_TX_ACOMP_THRESHOLD; 928 *mul = 1; 929 break; 930 case V4L2_CID_AUDIO_COMPRESSION_GAIN: 931 *property = SI4713_TX_ACOMP_GAIN; 932 *mul = 1; 933 break; 934 case V4L2_CID_PILOT_TONE_FREQUENCY: 935 *property = SI4713_TX_PILOT_FREQUENCY; 936 *mul = 1; 937 break; 938 case V4L2_CID_AUDIO_COMPRESSION_ATTACK_TIME: 939 *property = SI4713_TX_ACOMP_ATTACK_TIME; 940 *mul = ATTACK_TIME_UNIT; 941 break; 942 case V4L2_CID_PILOT_TONE_DEVIATION: 943 *property = SI4713_TX_PILOT_DEVIATION; 944 *mul = 10; 945 break; 946 case V4L2_CID_AUDIO_LIMITER_DEVIATION: 947 *property = SI4713_TX_AUDIO_DEVIATION; 948 *mul = 10; 949 break; 950 case V4L2_CID_RDS_TX_DEVIATION: 951 *property = SI4713_TX_RDS_DEVIATION; 952 *mul = 1; 953 break; 954 955 case V4L2_CID_RDS_TX_PTY: 956 *property = SI4713_TX_RDS_PS_MISC; 957 *bit = 5; 958 *mask = 0x1F << 5; 959 break; 960 case V4L2_CID_RDS_TX_DYNAMIC_PTY: 961 *property = SI4713_TX_RDS_PS_MISC; 962 *bit = 15; 963 *mask = 1 << 15; 964 break; 965 case V4L2_CID_RDS_TX_COMPRESSED: 966 *property = SI4713_TX_RDS_PS_MISC; 967 *bit = 14; 968 *mask = 1 << 14; 969 break; 970 case V4L2_CID_RDS_TX_ARTIFICIAL_HEAD: 971 *property = SI4713_TX_RDS_PS_MISC; 972 *bit = 13; 973 *mask = 1 << 13; 974 break; 975 case V4L2_CID_RDS_TX_MONO_STEREO: 976 *property = SI4713_TX_RDS_PS_MISC; 977 *bit = 12; 978 *mask = 1 << 12; 979 break; 980 case V4L2_CID_RDS_TX_TRAFFIC_PROGRAM: 981 *property = SI4713_TX_RDS_PS_MISC; 982 *bit = 10; 983 *mask = 1 << 10; 984 break; 985 case V4L2_CID_RDS_TX_TRAFFIC_ANNOUNCEMENT: 986 *property = SI4713_TX_RDS_PS_MISC; 987 *bit = 4; 988 *mask = 1 << 4; 989 break; 990 case V4L2_CID_RDS_TX_MUSIC_SPEECH: 991 *property = SI4713_TX_RDS_PS_MISC; 992 *bit = 3; 993 *mask = 1 << 3; 994 break; 995 case V4L2_CID_AUDIO_LIMITER_ENABLED: 996 *property = SI4713_TX_ACOMP_ENABLE; 997 *bit = 1; 998 *mask = 1 << 1; 999 break; 1000 case V4L2_CID_AUDIO_COMPRESSION_ENABLED: 1001 *property = SI4713_TX_ACOMP_ENABLE; 1002 *bit = 0; 1003 *mask = 1 << 0; 1004 break; 1005 case V4L2_CID_PILOT_TONE_ENABLED: 1006 *property = SI4713_TX_COMPONENT_ENABLE; 1007 *bit = 0; 1008 *mask = 1 << 0; 1009 break; 1010 1011 case V4L2_CID_AUDIO_LIMITER_RELEASE_TIME: 1012 *property = SI4713_TX_LIMITER_RELEASE_TIME; 1013 *table = limiter_times; 1014 *size = ARRAY_SIZE(limiter_times); 1015 break; 1016 case V4L2_CID_AUDIO_COMPRESSION_RELEASE_TIME: 1017 *property = SI4713_TX_ACOMP_RELEASE_TIME; 1018 *table = acomp_rtimes; 1019 *size = ARRAY_SIZE(acomp_rtimes); 1020 break; 1021 case V4L2_CID_TUNE_PREEMPHASIS: 1022 *property = SI4713_TX_PREEMPHASIS; 1023 *table = preemphasis_values; 1024 *size = ARRAY_SIZE(preemphasis_values); 1025 break; 1026 1027 default: 1028 rval = -EINVAL; 1029 break; 1030 } 1031 1032 return rval; 1033 } 1034 1035 static int si4713_s_frequency(struct v4l2_subdev *sd, const struct v4l2_frequency *f); 1036 static int si4713_s_modulator(struct v4l2_subdev *sd, const struct v4l2_modulator *); 1037 /* 1038 * si4713_setup - Sets the device up with current configuration. 1039 * @sdev: si4713_device structure for the device we are communicating 1040 */ 1041 static int si4713_setup(struct si4713_device *sdev) 1042 { 1043 struct v4l2_frequency f; 1044 struct v4l2_modulator vm; 1045 int rval; 1046 1047 /* Device procedure needs to set frequency first */ 1048 f.tuner = 0; 1049 f.frequency = sdev->frequency ? sdev->frequency : DEFAULT_FREQUENCY; 1050 f.frequency = si4713_to_v4l2(f.frequency); 1051 rval = si4713_s_frequency(&sdev->sd, &f); 1052 1053 vm.index = 0; 1054 if (sdev->stereo) 1055 vm.txsubchans = V4L2_TUNER_SUB_STEREO; 1056 else 1057 vm.txsubchans = V4L2_TUNER_SUB_MONO; 1058 if (sdev->rds_enabled) 1059 vm.txsubchans |= V4L2_TUNER_SUB_RDS; 1060 si4713_s_modulator(&sdev->sd, &vm); 1061 1062 return rval; 1063 } 1064 1065 /* 1066 * si4713_initialize - Sets the device up with default configuration. 1067 * @sdev: si4713_device structure for the device we are communicating 1068 */ 1069 static int si4713_initialize(struct si4713_device *sdev) 1070 { 1071 int rval; 1072 1073 rval = si4713_set_power_state(sdev, POWER_ON); 1074 if (rval < 0) 1075 return rval; 1076 1077 rval = si4713_checkrev(sdev); 1078 if (rval < 0) 1079 return rval; 1080 1081 rval = si4713_set_power_state(sdev, POWER_OFF); 1082 if (rval < 0) 1083 return rval; 1084 1085 sdev->frequency = DEFAULT_FREQUENCY; 1086 sdev->stereo = 1; 1087 sdev->tune_rnl = DEFAULT_TUNE_RNL; 1088 return 0; 1089 } 1090 1091 /* si4713_s_ctrl - set the value of a control */ 1092 static int si4713_s_ctrl(struct v4l2_ctrl *ctrl) 1093 { 1094 struct si4713_device *sdev = 1095 container_of(ctrl->handler, struct si4713_device, ctrl_handler); 1096 u32 val = 0; 1097 s32 bit = 0, mask = 0; 1098 u16 property = 0; 1099 int mul = 0; 1100 unsigned long *table = NULL; 1101 int size = 0; 1102 bool force = false; 1103 int c; 1104 int ret = 0; 1105 1106 if (ctrl->id != V4L2_CID_AUDIO_MUTE) 1107 return -EINVAL; 1108 if (ctrl->is_new) { 1109 if (ctrl->val) { 1110 ret = si4713_set_mute(sdev, ctrl->val); 1111 if (!ret) 1112 ret = si4713_set_power_state(sdev, POWER_DOWN); 1113 return ret; 1114 } 1115 ret = si4713_set_power_state(sdev, POWER_UP); 1116 if (!ret) 1117 ret = si4713_set_mute(sdev, ctrl->val); 1118 if (!ret) 1119 ret = si4713_setup(sdev); 1120 if (ret) 1121 return ret; 1122 force = true; 1123 } 1124 1125 if (!sdev->power_state) 1126 return 0; 1127 1128 for (c = 1; !ret && c < ctrl->ncontrols; c++) { 1129 ctrl = ctrl->cluster[c]; 1130 1131 if (!force && !ctrl->is_new) 1132 continue; 1133 1134 switch (ctrl->id) { 1135 case V4L2_CID_RDS_TX_PS_NAME: 1136 ret = si4713_set_rds_ps_name(sdev, ctrl->p_new.p_char); 1137 break; 1138 1139 case V4L2_CID_RDS_TX_RADIO_TEXT: 1140 ret = si4713_set_rds_radio_text(sdev, ctrl->p_new.p_char); 1141 break; 1142 1143 case V4L2_CID_TUNE_ANTENNA_CAPACITOR: 1144 /* don't handle this control if we force setting all 1145 * controls since in that case it will be handled by 1146 * V4L2_CID_TUNE_POWER_LEVEL. */ 1147 if (force) 1148 break; 1149 /* fall through */ 1150 case V4L2_CID_TUNE_POWER_LEVEL: 1151 ret = si4713_tx_tune_power(sdev, 1152 sdev->tune_pwr_level->val, sdev->tune_ant_cap->val); 1153 if (!ret) { 1154 /* Make sure we don't set this twice */ 1155 sdev->tune_ant_cap->is_new = false; 1156 sdev->tune_pwr_level->is_new = false; 1157 } 1158 break; 1159 1160 case V4L2_CID_RDS_TX_ALT_FREQS_ENABLE: 1161 case V4L2_CID_RDS_TX_ALT_FREQS: 1162 if (sdev->rds_alt_freqs_enable->val) { 1163 val = sdev->rds_alt_freqs->p_new.p_u32[0]; 1164 val = val / 100 - 876 + 0xe101; 1165 } else { 1166 val = 0xe0e0; 1167 } 1168 ret = si4713_write_property(sdev, SI4713_TX_RDS_PS_AF, val); 1169 break; 1170 1171 default: 1172 ret = si4713_choose_econtrol_action(sdev, ctrl->id, &bit, 1173 &mask, &property, &mul, &table, &size); 1174 if (ret < 0) 1175 break; 1176 1177 val = ctrl->val; 1178 if (mul) { 1179 val = val / mul; 1180 } else if (table) { 1181 ret = usecs_to_dev(val, table, size); 1182 if (ret < 0) 1183 break; 1184 val = ret; 1185 ret = 0; 1186 } 1187 1188 if (mask) { 1189 ret = si4713_read_property(sdev, property, &val); 1190 if (ret < 0) 1191 break; 1192 val = set_bits(val, ctrl->val, bit, mask); 1193 } 1194 1195 ret = si4713_write_property(sdev, property, val); 1196 if (ret < 0) 1197 break; 1198 if (mask) 1199 val = ctrl->val; 1200 break; 1201 } 1202 } 1203 1204 return ret; 1205 } 1206 1207 /* si4713_ioctl - deal with private ioctls (only rnl for now) */ 1208 static long si4713_ioctl(struct v4l2_subdev *sd, unsigned int cmd, void *arg) 1209 { 1210 struct si4713_device *sdev = to_si4713_device(sd); 1211 struct si4713_rnl *rnl = arg; 1212 u16 frequency; 1213 int rval = 0; 1214 1215 if (!arg) 1216 return -EINVAL; 1217 1218 switch (cmd) { 1219 case SI4713_IOC_MEASURE_RNL: 1220 frequency = v4l2_to_si4713(rnl->frequency); 1221 1222 if (sdev->power_state) { 1223 /* Set desired measurement frequency */ 1224 rval = si4713_tx_tune_measure(sdev, frequency, 0); 1225 if (rval < 0) 1226 return rval; 1227 /* get results from tune status */ 1228 rval = si4713_update_tune_status(sdev); 1229 if (rval < 0) 1230 return rval; 1231 } 1232 rnl->rnl = sdev->tune_rnl; 1233 break; 1234 1235 default: 1236 /* nothing */ 1237 rval = -ENOIOCTLCMD; 1238 } 1239 1240 return rval; 1241 } 1242 1243 /* si4713_g_modulator - get modulator attributes */ 1244 static int si4713_g_modulator(struct v4l2_subdev *sd, struct v4l2_modulator *vm) 1245 { 1246 struct si4713_device *sdev = to_si4713_device(sd); 1247 int rval = 0; 1248 1249 if (!sdev) 1250 return -ENODEV; 1251 1252 if (vm->index > 0) 1253 return -EINVAL; 1254 1255 strncpy(vm->name, "FM Modulator", 32); 1256 vm->capability = V4L2_TUNER_CAP_STEREO | V4L2_TUNER_CAP_LOW | 1257 V4L2_TUNER_CAP_RDS | V4L2_TUNER_CAP_RDS_CONTROLS; 1258 1259 /* Report current frequency range limits */ 1260 vm->rangelow = si4713_to_v4l2(FREQ_RANGE_LOW); 1261 vm->rangehigh = si4713_to_v4l2(FREQ_RANGE_HIGH); 1262 1263 if (sdev->power_state) { 1264 u32 comp_en = 0; 1265 1266 rval = si4713_read_property(sdev, SI4713_TX_COMPONENT_ENABLE, 1267 &comp_en); 1268 if (rval < 0) 1269 return rval; 1270 1271 sdev->stereo = get_status_bit(comp_en, 1, 1 << 1); 1272 } 1273 1274 /* Report current audio mode: mono or stereo */ 1275 if (sdev->stereo) 1276 vm->txsubchans = V4L2_TUNER_SUB_STEREO; 1277 else 1278 vm->txsubchans = V4L2_TUNER_SUB_MONO; 1279 1280 /* Report rds feature status */ 1281 if (sdev->rds_enabled) 1282 vm->txsubchans |= V4L2_TUNER_SUB_RDS; 1283 else 1284 vm->txsubchans &= ~V4L2_TUNER_SUB_RDS; 1285 1286 return rval; 1287 } 1288 1289 /* si4713_s_modulator - set modulator attributes */ 1290 static int si4713_s_modulator(struct v4l2_subdev *sd, const struct v4l2_modulator *vm) 1291 { 1292 struct si4713_device *sdev = to_si4713_device(sd); 1293 int rval = 0; 1294 u16 stereo, rds; 1295 u32 p; 1296 1297 if (!sdev) 1298 return -ENODEV; 1299 1300 if (vm->index > 0) 1301 return -EINVAL; 1302 1303 /* Set audio mode: mono or stereo */ 1304 if (vm->txsubchans & V4L2_TUNER_SUB_STEREO) 1305 stereo = 1; 1306 else if (vm->txsubchans & V4L2_TUNER_SUB_MONO) 1307 stereo = 0; 1308 else 1309 return -EINVAL; 1310 1311 rds = !!(vm->txsubchans & V4L2_TUNER_SUB_RDS); 1312 1313 if (sdev->power_state) { 1314 rval = si4713_read_property(sdev, 1315 SI4713_TX_COMPONENT_ENABLE, &p); 1316 if (rval < 0) 1317 return rval; 1318 1319 p = set_bits(p, stereo, 1, 1 << 1); 1320 p = set_bits(p, rds, 2, 1 << 2); 1321 1322 rval = si4713_write_property(sdev, 1323 SI4713_TX_COMPONENT_ENABLE, p); 1324 if (rval < 0) 1325 return rval; 1326 } 1327 1328 sdev->stereo = stereo; 1329 sdev->rds_enabled = rds; 1330 1331 return rval; 1332 } 1333 1334 /* si4713_g_frequency - get tuner or modulator radio frequency */ 1335 static int si4713_g_frequency(struct v4l2_subdev *sd, struct v4l2_frequency *f) 1336 { 1337 struct si4713_device *sdev = to_si4713_device(sd); 1338 int rval = 0; 1339 1340 if (f->tuner) 1341 return -EINVAL; 1342 1343 if (sdev->power_state) { 1344 u16 freq; 1345 u8 p, a, n; 1346 1347 rval = si4713_tx_tune_status(sdev, 0x00, &freq, &p, &a, &n); 1348 if (rval < 0) 1349 return rval; 1350 1351 sdev->frequency = freq; 1352 } 1353 1354 f->frequency = si4713_to_v4l2(sdev->frequency); 1355 1356 return rval; 1357 } 1358 1359 /* si4713_s_frequency - set tuner or modulator radio frequency */ 1360 static int si4713_s_frequency(struct v4l2_subdev *sd, const struct v4l2_frequency *f) 1361 { 1362 struct si4713_device *sdev = to_si4713_device(sd); 1363 int rval = 0; 1364 u16 frequency = v4l2_to_si4713(f->frequency); 1365 1366 if (f->tuner) 1367 return -EINVAL; 1368 1369 /* Check frequency range */ 1370 frequency = clamp_t(u16, frequency, FREQ_RANGE_LOW, FREQ_RANGE_HIGH); 1371 1372 if (sdev->power_state) { 1373 rval = si4713_tx_tune_freq(sdev, frequency); 1374 if (rval < 0) 1375 return rval; 1376 frequency = rval; 1377 rval = 0; 1378 } 1379 sdev->frequency = frequency; 1380 1381 return rval; 1382 } 1383 1384 static const struct v4l2_ctrl_ops si4713_ctrl_ops = { 1385 .s_ctrl = si4713_s_ctrl, 1386 }; 1387 1388 static const struct v4l2_subdev_core_ops si4713_subdev_core_ops = { 1389 .ioctl = si4713_ioctl, 1390 }; 1391 1392 static const struct v4l2_subdev_tuner_ops si4713_subdev_tuner_ops = { 1393 .g_frequency = si4713_g_frequency, 1394 .s_frequency = si4713_s_frequency, 1395 .g_modulator = si4713_g_modulator, 1396 .s_modulator = si4713_s_modulator, 1397 }; 1398 1399 static const struct v4l2_subdev_ops si4713_subdev_ops = { 1400 .core = &si4713_subdev_core_ops, 1401 .tuner = &si4713_subdev_tuner_ops, 1402 }; 1403 1404 static const struct v4l2_ctrl_config si4713_alt_freqs_ctrl = { 1405 .id = V4L2_CID_RDS_TX_ALT_FREQS, 1406 .type = V4L2_CTRL_TYPE_U32, 1407 .min = 87600, 1408 .max = 107900, 1409 .step = 100, 1410 .def = 87600, 1411 .dims = { 1 }, 1412 .elem_size = sizeof(u32), 1413 }; 1414 1415 /* 1416 * I2C driver interface 1417 */ 1418 /* si4713_probe - probe for the device */ 1419 static int si4713_probe(struct i2c_client *client, 1420 const struct i2c_device_id *id) 1421 { 1422 struct si4713_device *sdev; 1423 struct si4713_platform_data *pdata = client->dev.platform_data; 1424 struct v4l2_ctrl_handler *hdl; 1425 int rval, i; 1426 1427 sdev = kzalloc(sizeof(*sdev), GFP_KERNEL); 1428 if (!sdev) { 1429 dev_err(&client->dev, "Failed to alloc video device.\n"); 1430 rval = -ENOMEM; 1431 goto exit; 1432 } 1433 1434 sdev->gpio_reset = -1; 1435 if (pdata && gpio_is_valid(pdata->gpio_reset)) { 1436 rval = gpio_request(pdata->gpio_reset, "si4713 reset"); 1437 if (rval) { 1438 dev_err(&client->dev, 1439 "Failed to request gpio: %d\n", rval); 1440 goto free_sdev; 1441 } 1442 sdev->gpio_reset = pdata->gpio_reset; 1443 gpio_direction_output(sdev->gpio_reset, 0); 1444 sdev->supplies = pdata->supplies; 1445 } 1446 1447 for (i = 0; i < sdev->supplies; i++) 1448 sdev->supply_data[i].supply = pdata->supply_names[i]; 1449 1450 rval = regulator_bulk_get(&client->dev, sdev->supplies, 1451 sdev->supply_data); 1452 if (rval) { 1453 dev_err(&client->dev, "Cannot get regulators: %d\n", rval); 1454 goto free_gpio; 1455 } 1456 1457 v4l2_i2c_subdev_init(&sdev->sd, client, &si4713_subdev_ops); 1458 1459 init_completion(&sdev->work); 1460 1461 hdl = &sdev->ctrl_handler; 1462 v4l2_ctrl_handler_init(hdl, 20); 1463 sdev->mute = v4l2_ctrl_new_std(hdl, &si4713_ctrl_ops, 1464 V4L2_CID_AUDIO_MUTE, 0, 1, 1, DEFAULT_MUTE); 1465 1466 sdev->rds_pi = v4l2_ctrl_new_std(hdl, &si4713_ctrl_ops, 1467 V4L2_CID_RDS_TX_PI, 0, 0xffff, 1, DEFAULT_RDS_PI); 1468 sdev->rds_pty = v4l2_ctrl_new_std(hdl, &si4713_ctrl_ops, 1469 V4L2_CID_RDS_TX_PTY, 0, 31, 1, DEFAULT_RDS_PTY); 1470 sdev->rds_compressed = v4l2_ctrl_new_std(hdl, &si4713_ctrl_ops, 1471 V4L2_CID_RDS_TX_COMPRESSED, 0, 1, 1, 0); 1472 sdev->rds_art_head = v4l2_ctrl_new_std(hdl, &si4713_ctrl_ops, 1473 V4L2_CID_RDS_TX_ARTIFICIAL_HEAD, 0, 1, 1, 0); 1474 sdev->rds_stereo = v4l2_ctrl_new_std(hdl, &si4713_ctrl_ops, 1475 V4L2_CID_RDS_TX_MONO_STEREO, 0, 1, 1, 1); 1476 sdev->rds_tp = v4l2_ctrl_new_std(hdl, &si4713_ctrl_ops, 1477 V4L2_CID_RDS_TX_TRAFFIC_PROGRAM, 0, 1, 1, 0); 1478 sdev->rds_ta = v4l2_ctrl_new_std(hdl, &si4713_ctrl_ops, 1479 V4L2_CID_RDS_TX_TRAFFIC_ANNOUNCEMENT, 0, 1, 1, 0); 1480 sdev->rds_ms = v4l2_ctrl_new_std(hdl, &si4713_ctrl_ops, 1481 V4L2_CID_RDS_TX_MUSIC_SPEECH, 0, 1, 1, 1); 1482 sdev->rds_dyn_pty = v4l2_ctrl_new_std(hdl, &si4713_ctrl_ops, 1483 V4L2_CID_RDS_TX_DYNAMIC_PTY, 0, 1, 1, 0); 1484 sdev->rds_alt_freqs_enable = v4l2_ctrl_new_std(hdl, &si4713_ctrl_ops, 1485 V4L2_CID_RDS_TX_ALT_FREQS_ENABLE, 0, 1, 1, 0); 1486 sdev->rds_alt_freqs = v4l2_ctrl_new_custom(hdl, &si4713_alt_freqs_ctrl, NULL); 1487 sdev->rds_deviation = v4l2_ctrl_new_std(hdl, &si4713_ctrl_ops, 1488 V4L2_CID_RDS_TX_DEVIATION, 0, MAX_RDS_DEVIATION, 1489 10, DEFAULT_RDS_DEVIATION); 1490 /* 1491 * Report step as 8. From RDS spec, psname 1492 * should be 8. But there are receivers which scroll strings 1493 * sized as 8xN. 1494 */ 1495 sdev->rds_ps_name = v4l2_ctrl_new_std(hdl, &si4713_ctrl_ops, 1496 V4L2_CID_RDS_TX_PS_NAME, 0, MAX_RDS_PS_NAME, 8, 0); 1497 /* 1498 * Report step as 32 (2A block). From RDS spec, 1499 * radio text should be 32 for 2A block. But there are receivers 1500 * which scroll strings sized as 32xN. Setting default to 32. 1501 */ 1502 sdev->rds_radio_text = v4l2_ctrl_new_std(hdl, &si4713_ctrl_ops, 1503 V4L2_CID_RDS_TX_RADIO_TEXT, 0, MAX_RDS_RADIO_TEXT, 32, 0); 1504 1505 sdev->limiter_enabled = v4l2_ctrl_new_std(hdl, &si4713_ctrl_ops, 1506 V4L2_CID_AUDIO_LIMITER_ENABLED, 0, 1, 1, 1); 1507 sdev->limiter_release_time = v4l2_ctrl_new_std(hdl, &si4713_ctrl_ops, 1508 V4L2_CID_AUDIO_LIMITER_RELEASE_TIME, 250, 1509 MAX_LIMITER_RELEASE_TIME, 10, DEFAULT_LIMITER_RTIME); 1510 sdev->limiter_deviation = v4l2_ctrl_new_std(hdl, &si4713_ctrl_ops, 1511 V4L2_CID_AUDIO_LIMITER_DEVIATION, 0, 1512 MAX_LIMITER_DEVIATION, 10, DEFAULT_LIMITER_DEV); 1513 1514 sdev->compression_enabled = v4l2_ctrl_new_std(hdl, &si4713_ctrl_ops, 1515 V4L2_CID_AUDIO_COMPRESSION_ENABLED, 0, 1, 1, 1); 1516 sdev->compression_gain = v4l2_ctrl_new_std(hdl, &si4713_ctrl_ops, 1517 V4L2_CID_AUDIO_COMPRESSION_GAIN, 0, MAX_ACOMP_GAIN, 1, 1518 DEFAULT_ACOMP_GAIN); 1519 sdev->compression_threshold = v4l2_ctrl_new_std(hdl, &si4713_ctrl_ops, 1520 V4L2_CID_AUDIO_COMPRESSION_THRESHOLD, 1521 MIN_ACOMP_THRESHOLD, MAX_ACOMP_THRESHOLD, 1, 1522 DEFAULT_ACOMP_THRESHOLD); 1523 sdev->compression_attack_time = v4l2_ctrl_new_std(hdl, &si4713_ctrl_ops, 1524 V4L2_CID_AUDIO_COMPRESSION_ATTACK_TIME, 0, 1525 MAX_ACOMP_ATTACK_TIME, 500, DEFAULT_ACOMP_ATIME); 1526 sdev->compression_release_time = v4l2_ctrl_new_std(hdl, &si4713_ctrl_ops, 1527 V4L2_CID_AUDIO_COMPRESSION_RELEASE_TIME, 100000, 1528 MAX_ACOMP_RELEASE_TIME, 100000, DEFAULT_ACOMP_RTIME); 1529 1530 sdev->pilot_tone_enabled = v4l2_ctrl_new_std(hdl, &si4713_ctrl_ops, 1531 V4L2_CID_PILOT_TONE_ENABLED, 0, 1, 1, 1); 1532 sdev->pilot_tone_deviation = v4l2_ctrl_new_std(hdl, &si4713_ctrl_ops, 1533 V4L2_CID_PILOT_TONE_DEVIATION, 0, MAX_PILOT_DEVIATION, 1534 10, DEFAULT_PILOT_DEVIATION); 1535 sdev->pilot_tone_freq = v4l2_ctrl_new_std(hdl, &si4713_ctrl_ops, 1536 V4L2_CID_PILOT_TONE_FREQUENCY, 0, MAX_PILOT_FREQUENCY, 1537 1, DEFAULT_PILOT_FREQUENCY); 1538 1539 sdev->tune_preemphasis = v4l2_ctrl_new_std_menu(hdl, &si4713_ctrl_ops, 1540 V4L2_CID_TUNE_PREEMPHASIS, 1541 V4L2_PREEMPHASIS_75_uS, 0, V4L2_PREEMPHASIS_50_uS); 1542 sdev->tune_pwr_level = v4l2_ctrl_new_std(hdl, &si4713_ctrl_ops, 1543 V4L2_CID_TUNE_POWER_LEVEL, 0, SI4713_MAX_POWER, 1544 1, DEFAULT_POWER_LEVEL); 1545 sdev->tune_ant_cap = v4l2_ctrl_new_std(hdl, &si4713_ctrl_ops, 1546 V4L2_CID_TUNE_ANTENNA_CAPACITOR, 0, SI4713_MAX_ANTCAP, 1547 1, 0); 1548 1549 if (hdl->error) { 1550 rval = hdl->error; 1551 goto free_ctrls; 1552 } 1553 v4l2_ctrl_cluster(29, &sdev->mute); 1554 sdev->sd.ctrl_handler = hdl; 1555 1556 if (client->irq) { 1557 rval = request_irq(client->irq, 1558 si4713_handler, IRQF_TRIGGER_FALLING, 1559 client->name, sdev); 1560 if (rval < 0) { 1561 v4l2_err(&sdev->sd, "Could not request IRQ\n"); 1562 goto put_reg; 1563 } 1564 v4l2_dbg(1, debug, &sdev->sd, "IRQ requested.\n"); 1565 } else { 1566 v4l2_warn(&sdev->sd, "IRQ not configured. Using timeouts.\n"); 1567 } 1568 1569 rval = si4713_initialize(sdev); 1570 if (rval < 0) { 1571 v4l2_err(&sdev->sd, "Failed to probe device information.\n"); 1572 goto free_irq; 1573 } 1574 1575 return 0; 1576 1577 free_irq: 1578 if (client->irq) 1579 free_irq(client->irq, sdev); 1580 free_ctrls: 1581 v4l2_ctrl_handler_free(hdl); 1582 put_reg: 1583 regulator_bulk_free(sdev->supplies, sdev->supply_data); 1584 free_gpio: 1585 if (gpio_is_valid(sdev->gpio_reset)) 1586 gpio_free(sdev->gpio_reset); 1587 free_sdev: 1588 kfree(sdev); 1589 exit: 1590 return rval; 1591 } 1592 1593 /* si4713_remove - remove the device */ 1594 static int si4713_remove(struct i2c_client *client) 1595 { 1596 struct v4l2_subdev *sd = i2c_get_clientdata(client); 1597 struct si4713_device *sdev = to_si4713_device(sd); 1598 1599 if (sdev->power_state) 1600 si4713_set_power_state(sdev, POWER_DOWN); 1601 1602 if (client->irq > 0) 1603 free_irq(client->irq, sdev); 1604 1605 v4l2_device_unregister_subdev(sd); 1606 v4l2_ctrl_handler_free(sd->ctrl_handler); 1607 regulator_bulk_free(sdev->supplies, sdev->supply_data); 1608 if (gpio_is_valid(sdev->gpio_reset)) 1609 gpio_free(sdev->gpio_reset); 1610 kfree(sdev); 1611 1612 return 0; 1613 } 1614 1615 /* si4713_i2c_driver - i2c driver interface */ 1616 static const struct i2c_device_id si4713_id[] = { 1617 { "si4713" , 0 }, 1618 { }, 1619 }; 1620 MODULE_DEVICE_TABLE(i2c, si4713_id); 1621 1622 static struct i2c_driver si4713_i2c_driver = { 1623 .driver = { 1624 .name = "si4713", 1625 }, 1626 .probe = si4713_probe, 1627 .remove = si4713_remove, 1628 .id_table = si4713_id, 1629 }; 1630 1631 module_i2c_driver(si4713_i2c_driver); 1632