2 * drivers/media/radio/si4713-i2c.c
4 * Silicon Labs Si4713 FM Radio Transmitter I2C commands.
6 * Copyright (c) 2009 Nokia Corporation
7 * Contact: Eduardo Valentin <eduardo.valentin@nokia.com>
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.
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.
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
24 #include <linux/mutex.h>
25 #include <linux/completion.h>
26 #include <linux/delay.h>
27 #include <linux/interrupt.h>
28 #include <linux/i2c.h>
29 #include <linux/slab.h>
30 #include <linux/gpio.h>
31 #include <linux/regulator/consumer.h>
32 #include <linux/module.h>
33 #include <media/v4l2-device.h>
34 #include <media/v4l2-ioctl.h>
35 #include <media/v4l2-common.h>
37 #include "si4713-i2c.h"
39 /* module parameters */
41 module_param(debug, int, S_IRUGO | S_IWUSR);
42 MODULE_PARM_DESC(debug, "Debug level (0 - 2)");
44 MODULE_LICENSE("GPL");
45 MODULE_AUTHOR("Eduardo Valentin <eduardo.valentin@nokia.com>");
46 MODULE_DESCRIPTION("I2C driver for Si4713 FM Radio Transmitter");
47 MODULE_VERSION("0.0.1");
49 static const char *si4713_supply_names[SI4713_NUM_SUPPLIES] = {
54 #define DEFAULT_RDS_PI 0x00
55 #define DEFAULT_RDS_PTY 0x00
56 #define DEFAULT_RDS_PS_NAME ""
57 #define DEFAULT_RDS_RADIO_TEXT DEFAULT_RDS_PS_NAME
58 #define DEFAULT_RDS_DEVIATION 0x00C8
59 #define DEFAULT_RDS_PS_REPEAT_COUNT 0x0003
60 #define DEFAULT_LIMITER_RTIME 0x1392
61 #define DEFAULT_LIMITER_DEV 0x102CA
62 #define DEFAULT_PILOT_FREQUENCY 0x4A38
63 #define DEFAULT_PILOT_DEVIATION 0x1A5E
64 #define DEFAULT_ACOMP_ATIME 0x0000
65 #define DEFAULT_ACOMP_RTIME 0xF4240L
66 #define DEFAULT_ACOMP_GAIN 0x0F
67 #define DEFAULT_ACOMP_THRESHOLD (-0x28)
68 #define DEFAULT_MUTE 0x01
69 #define DEFAULT_POWER_LEVEL 88
70 #define DEFAULT_FREQUENCY 8800
71 #define DEFAULT_PREEMPHASIS FMPE_EU
72 #define DEFAULT_TUNE_RNL 0xFF
74 #define to_si4713_device(sd) container_of(sd, struct si4713_device, sd)
76 /* frequency domain transformation (using times 10 to avoid floats) */
77 #define FREQDEV_UNIT 100000
78 #define FREQV4L2_MULTI 625
79 #define si4713_to_v4l2(f) ((f * FREQDEV_UNIT) / FREQV4L2_MULTI)
80 #define v4l2_to_si4713(f) ((f * FREQV4L2_MULTI) / FREQDEV_UNIT)
81 #define FREQ_RANGE_LOW 7600
82 #define FREQ_RANGE_HIGH 10800
87 #define RDS_BLOCK_CLEAR 0x03
88 #define RDS_BLOCK_LOAD 0x04
89 #define RDS_RADIOTEXT_2A 0x20
90 #define RDS_RADIOTEXT_BLK_SIZE 4
91 #define RDS_RADIOTEXT_INDEX_MAX 0x0F
92 #define RDS_CARRIAGE_RETURN 0x0D
94 #define rds_ps_nblocks(len) ((len / RDS_BLOCK) + (len % RDS_BLOCK ? 1 : 0))
96 #define get_status_bit(p, b, m) (((p) & (m)) >> (b))
97 #define set_bits(p, v, b, m) (((p) & ~(m)) | ((v) << (b)))
99 #define ATTACK_TIME_UNIT 500
101 #define POWER_OFF 0x00
102 #define POWER_ON 0x01
104 #define msb(x) ((u8)((u16) x >> 8))
105 #define lsb(x) ((u8)((u16) x & 0x00FF))
106 #define compose_u16(msb, lsb) (((u16)msb << 8) | lsb)
107 #define check_command_failed(status) (!(status & SI4713_CTS) || \
108 (status & SI4713_ERR))
109 /* mute definition */
110 #define set_mute(p) ((p & 1) | ((p & 1) << 1));
111 #define get_mute(p) (p & 0x01)
114 #define DBG_BUFFER(device, message, buffer, size) \
117 char str[(size)*5]; \
118 for (i = 0; i < size; i++) \
119 sprintf(str + i * 5, " 0x%02x", buffer[i]); \
120 v4l2_dbg(2, debug, device, "%s:%s\n", message, str); \
123 #define DBG_BUFFER(device, message, buffer, size)
127 * Values for limiter release time (sorted by second column)
131 static long limiter_times[] = {
155 * Values for audio compression release time (sorted by second column)
159 static unsigned long acomp_rtimes[] = {
168 * Values for preemphasis (sorted by second column)
172 static unsigned long preemphasis_values[] = {
173 FMPE_DISABLED, V4L2_PREEMPHASIS_DISABLED,
174 FMPE_EU, V4L2_PREEMPHASIS_50_uS,
175 FMPE_USA, V4L2_PREEMPHASIS_75_uS,
178 static int usecs_to_dev(unsigned long usecs, unsigned long const array[],
184 for (i = 0; i < size / 2; i++)
185 if (array[(i * 2) + 1] >= usecs) {
193 static unsigned long dev_to_usecs(int value, unsigned long const array[],
199 for (i = 0; i < size / 2; i++)
200 if (array[i * 2] == value) {
201 rval = array[(i * 2) + 1];
208 /* si4713_handler: IRQ handler, just complete work */
209 static irqreturn_t si4713_handler(int irq, void *dev)
211 struct si4713_device *sdev = dev;
213 v4l2_dbg(2, debug, &sdev->sd,
214 "%s: sending signal to completion work.\n", __func__);
215 complete(&sdev->work);
221 * si4713_send_command - sends a command to si4713 and waits its response
222 * @sdev: si4713_device structure for the device we are communicating
223 * @command: command id
224 * @args: command arguments we are sending (up to 7)
225 * @argn: actual size of @args
226 * @response: buffer to place the expected response from the device (up to 15)
227 * @respn: actual size of @response
228 * @usecs: amount of time to wait before reading the response (in usecs)
230 static int si4713_send_command(struct si4713_device *sdev, const u8 command,
231 const u8 args[], const int argn,
232 u8 response[], const int respn, const int usecs)
234 struct i2c_client *client = v4l2_get_subdevdata(&sdev->sd);
235 u8 data1[MAX_ARGS + 1];
238 if (!client->adapter)
241 /* First send the command and its arguments */
243 memcpy(data1 + 1, args, argn);
244 DBG_BUFFER(&sdev->sd, "Parameters", data1, argn + 1);
246 err = i2c_master_send(client, data1, argn + 1);
247 if (err != argn + 1) {
248 v4l2_err(&sdev->sd, "Error while sending command 0x%02x\n",
250 return (err > 0) ? -EIO : err;
253 /* Wait response from interrupt */
254 if (!wait_for_completion_timeout(&sdev->work,
255 usecs_to_jiffies(usecs) + 1))
257 "(%s) Device took too much time to answer.\n",
260 /* Then get the response */
261 err = i2c_master_recv(client, response, respn);
264 "Error while reading response for command 0x%02x\n",
266 return (err > 0) ? -EIO : err;
269 DBG_BUFFER(&sdev->sd, "Response", response, respn);
270 if (check_command_failed(response[0]))
277 * si4713_read_property - reads a si4713 property
278 * @sdev: si4713_device structure for the device we are communicating
279 * @prop: property identification number
280 * @pv: property value to be returned on success
282 static int si4713_read_property(struct si4713_device *sdev, u16 prop, u32 *pv)
285 u8 val[SI4713_GET_PROP_NRESP];
288 * .Second byte = property's MSB
289 * .Third byte = property's LSB
291 const u8 args[SI4713_GET_PROP_NARGS] = {
297 err = si4713_send_command(sdev, SI4713_CMD_GET_PROPERTY,
298 args, ARRAY_SIZE(args), val,
299 ARRAY_SIZE(val), DEFAULT_TIMEOUT);
304 *pv = compose_u16(val[2], val[3]);
306 v4l2_dbg(1, debug, &sdev->sd,
307 "%s: property=0x%02x value=0x%02x status=0x%02x\n",
308 __func__, prop, *pv, val[0]);
314 * si4713_write_property - modifies a si4713 property
315 * @sdev: si4713_device structure for the device we are communicating
316 * @prop: property identification number
317 * @val: new value for that property
319 static int si4713_write_property(struct si4713_device *sdev, u16 prop, u16 val)
322 u8 resp[SI4713_SET_PROP_NRESP];
325 * .Second byte = property's MSB
326 * .Third byte = property's LSB
327 * .Fourth byte = value's MSB
328 * .Fifth byte = value's LSB
330 const u8 args[SI4713_SET_PROP_NARGS] = {
338 rval = si4713_send_command(sdev, SI4713_CMD_SET_PROPERTY,
339 args, ARRAY_SIZE(args),
340 resp, ARRAY_SIZE(resp),
346 v4l2_dbg(1, debug, &sdev->sd,
347 "%s: property=0x%02x value=0x%02x status=0x%02x\n",
348 __func__, prop, val, resp[0]);
351 * As there is no command response for SET_PROPERTY,
352 * wait Tcomp time to finish before proceed, in order
353 * to have property properly set.
355 msleep(TIMEOUT_SET_PROPERTY);
361 * si4713_powerup - Powers the device up
362 * @sdev: si4713_device structure for the device we are communicating
364 static int si4713_powerup(struct si4713_device *sdev)
367 u8 resp[SI4713_PWUP_NRESP];
369 * .First byte = Enabled interrupts and boot function
370 * .Second byte = Input operation mode
372 const u8 args[SI4713_PWUP_NARGS] = {
373 SI4713_PWUP_CTSIEN | SI4713_PWUP_GPO2OEN | SI4713_PWUP_FUNC_TX,
374 SI4713_PWUP_OPMOD_ANALOG,
377 if (sdev->power_state)
380 err = regulator_bulk_enable(ARRAY_SIZE(sdev->supplies),
383 v4l2_err(&sdev->sd, "Failed to enable supplies: %d\n", err);
386 if (gpio_is_valid(sdev->gpio_reset)) {
388 gpio_set_value(sdev->gpio_reset, 1);
391 err = si4713_send_command(sdev, SI4713_CMD_POWER_UP,
392 args, ARRAY_SIZE(args),
393 resp, ARRAY_SIZE(resp),
397 v4l2_dbg(1, debug, &sdev->sd, "Powerup response: 0x%02x\n",
399 v4l2_dbg(1, debug, &sdev->sd, "Device in power up mode\n");
400 sdev->power_state = POWER_ON;
402 err = si4713_write_property(sdev, SI4713_GPO_IEN,
403 SI4713_STC_INT | SI4713_CTS);
405 if (gpio_is_valid(sdev->gpio_reset))
406 gpio_set_value(sdev->gpio_reset, 0);
407 err = regulator_bulk_disable(ARRAY_SIZE(sdev->supplies),
411 "Failed to disable supplies: %d\n", err);
418 * si4713_powerdown - Powers the device down
419 * @sdev: si4713_device structure for the device we are communicating
421 static int si4713_powerdown(struct si4713_device *sdev)
424 u8 resp[SI4713_PWDN_NRESP];
426 if (!sdev->power_state)
429 err = si4713_send_command(sdev, SI4713_CMD_POWER_DOWN,
431 resp, ARRAY_SIZE(resp),
435 v4l2_dbg(1, debug, &sdev->sd, "Power down response: 0x%02x\n",
437 v4l2_dbg(1, debug, &sdev->sd, "Device in reset mode\n");
438 if (gpio_is_valid(sdev->gpio_reset))
439 gpio_set_value(sdev->gpio_reset, 0);
440 err = regulator_bulk_disable(ARRAY_SIZE(sdev->supplies),
444 "Failed to disable supplies: %d\n", err);
445 sdev->power_state = POWER_OFF;
452 * si4713_checkrev - Checks if we are treating a device with the correct rev.
453 * @sdev: si4713_device structure for the device we are communicating
455 static int si4713_checkrev(struct si4713_device *sdev)
457 struct i2c_client *client = v4l2_get_subdevdata(&sdev->sd);
459 u8 resp[SI4713_GETREV_NRESP];
461 mutex_lock(&sdev->mutex);
463 rval = si4713_send_command(sdev, SI4713_CMD_GET_REV,
465 resp, ARRAY_SIZE(resp),
471 if (resp[1] == SI4713_PRODUCT_NUMBER) {
472 v4l2_info(&sdev->sd, "chip found @ 0x%02x (%s)\n",
473 client->addr << 1, client->adapter->name);
475 v4l2_err(&sdev->sd, "Invalid product number\n");
480 mutex_unlock(&sdev->mutex);
485 * si4713_wait_stc - Waits STC interrupt and clears status bits. Useful
486 * for TX_TUNE_POWER, TX_TUNE_FREQ and TX_TUNE_MEAS
487 * @sdev: si4713_device structure for the device we are communicating
488 * @usecs: timeout to wait for STC interrupt signal
490 static int si4713_wait_stc(struct si4713_device *sdev, const int usecs)
493 u8 resp[SI4713_GET_STATUS_NRESP];
495 /* Wait response from STC interrupt */
496 if (!wait_for_completion_timeout(&sdev->work,
497 usecs_to_jiffies(usecs) + 1))
499 "%s: device took too much time to answer (%d usec).\n",
502 /* Clear status bits */
503 err = si4713_send_command(sdev, SI4713_CMD_GET_INT_STATUS,
505 resp, ARRAY_SIZE(resp),
511 v4l2_dbg(1, debug, &sdev->sd,
512 "%s: status bits: 0x%02x\n", __func__, resp[0]);
514 if (!(resp[0] & SI4713_STC_INT))
522 * si4713_tx_tune_freq - Sets the state of the RF carrier and sets the tuning
523 * frequency between 76 and 108 MHz in 10 kHz units and
525 * @sdev: si4713_device structure for the device we are communicating
526 * @frequency: desired frequency (76 - 108 MHz, unit 10 KHz, step 50 kHz)
528 static int si4713_tx_tune_freq(struct si4713_device *sdev, u16 frequency)
531 u8 val[SI4713_TXFREQ_NRESP];
534 * .Second byte = frequency's MSB
535 * .Third byte = frequency's LSB
537 const u8 args[SI4713_TXFREQ_NARGS] = {
543 err = si4713_send_command(sdev, SI4713_CMD_TX_TUNE_FREQ,
544 args, ARRAY_SIZE(args), val,
545 ARRAY_SIZE(val), DEFAULT_TIMEOUT);
550 v4l2_dbg(1, debug, &sdev->sd,
551 "%s: frequency=0x%02x status=0x%02x\n", __func__,
554 err = si4713_wait_stc(sdev, TIMEOUT_TX_TUNE);
558 return compose_u16(args[1], args[2]);
562 * si4713_tx_tune_power - Sets the RF voltage level between 88 and 115 dBuV in
563 * 1 dB units. A value of 0x00 indicates off. The command
564 * also sets the antenna tuning capacitance. A value of 0
565 * indicates autotuning, and a value of 1 - 191 indicates
566 * a manual override, which results in a tuning
567 * capacitance of 0.25 pF x @antcap.
568 * @sdev: si4713_device structure for the device we are communicating
569 * @power: tuning power (88 - 115 dBuV, unit/step 1 dB)
570 * @antcap: value of antenna tuning capacitor (0 - 191)
572 static int si4713_tx_tune_power(struct si4713_device *sdev, u8 power,
576 u8 val[SI4713_TXPWR_NRESP];
580 * .Third byte = power
581 * .Fourth byte = antcap
583 const u8 args[SI4713_TXPWR_NARGS] = {
590 if (((power > 0) && (power < SI4713_MIN_POWER)) ||
591 power > SI4713_MAX_POWER || antcap > SI4713_MAX_ANTCAP)
594 err = si4713_send_command(sdev, SI4713_CMD_TX_TUNE_POWER,
595 args, ARRAY_SIZE(args), val,
596 ARRAY_SIZE(val), DEFAULT_TIMEOUT);
601 v4l2_dbg(1, debug, &sdev->sd,
602 "%s: power=0x%02x antcap=0x%02x status=0x%02x\n",
603 __func__, power, antcap, val[0]);
605 return si4713_wait_stc(sdev, TIMEOUT_TX_TUNE_POWER);
609 * si4713_tx_tune_measure - Enters receive mode and measures the received noise
610 * level in units of dBuV on the selected frequency.
611 * The Frequency must be between 76 and 108 MHz in 10 kHz
612 * units and steps of 50 kHz. The command also sets the
613 * antenna tuning capacitance. A value of 0 means
614 * autotuning, and a value of 1 to 191 indicates manual
616 * @sdev: si4713_device structure for the device we are communicating
617 * @frequency: desired frequency (76 - 108 MHz, unit 10 KHz, step 50 kHz)
618 * @antcap: value of antenna tuning capacitor (0 - 191)
620 static int si4713_tx_tune_measure(struct si4713_device *sdev, u16 frequency,
624 u8 val[SI4713_TXMEA_NRESP];
627 * .Second byte = frequency's MSB
628 * .Third byte = frequency's LSB
629 * .Fourth byte = antcap
631 const u8 args[SI4713_TXMEA_NARGS] = {
638 sdev->tune_rnl = DEFAULT_TUNE_RNL;
640 if (antcap > SI4713_MAX_ANTCAP)
643 err = si4713_send_command(sdev, SI4713_CMD_TX_TUNE_MEASURE,
644 args, ARRAY_SIZE(args), val,
645 ARRAY_SIZE(val), DEFAULT_TIMEOUT);
650 v4l2_dbg(1, debug, &sdev->sd,
651 "%s: frequency=0x%02x antcap=0x%02x status=0x%02x\n",
652 __func__, frequency, antcap, val[0]);
654 return si4713_wait_stc(sdev, TIMEOUT_TX_TUNE);
658 * si4713_tx_tune_status- Returns the status of the tx_tune_freq, tx_tune_mea or
659 * tx_tune_power commands. This command return the current
660 * frequency, output voltage in dBuV, the antenna tunning
661 * capacitance value and the received noise level. The
662 * command also clears the stcint interrupt bit when the
663 * first bit of its arguments is high.
664 * @sdev: si4713_device structure for the device we are communicating
665 * @intack: 0x01 to clear the seek/tune complete interrupt status indicator.
666 * @frequency: returned frequency
667 * @power: returned power
668 * @antcap: returned antenna capacitance
669 * @noise: returned noise level
671 static int si4713_tx_tune_status(struct si4713_device *sdev, u8 intack,
672 u16 *frequency, u8 *power,
673 u8 *antcap, u8 *noise)
676 u8 val[SI4713_TXSTATUS_NRESP];
678 * .First byte = intack bit
680 const u8 args[SI4713_TXSTATUS_NARGS] = {
681 intack & SI4713_INTACK_MASK,
684 err = si4713_send_command(sdev, SI4713_CMD_TX_TUNE_STATUS,
685 args, ARRAY_SIZE(args), val,
686 ARRAY_SIZE(val), DEFAULT_TIMEOUT);
689 v4l2_dbg(1, debug, &sdev->sd,
690 "%s: status=0x%02x\n", __func__, val[0]);
691 *frequency = compose_u16(val[2], val[3]);
692 sdev->frequency = *frequency;
696 v4l2_dbg(1, debug, &sdev->sd, "%s: response: %d x 10 kHz "
697 "(power %d, antcap %d, rnl %d)\n", __func__,
698 *frequency, *power, *antcap, *noise);
705 * si4713_tx_rds_buff - Loads the RDS group buffer FIFO or circular buffer.
706 * @sdev: si4713_device structure for the device we are communicating
707 * @mode: the buffer operation mode.
711 * @cbleft: returns the number of available circular buffer blocks minus the
712 * number of used circular buffer blocks.
714 static int si4713_tx_rds_buff(struct si4713_device *sdev, u8 mode, u16 rdsb,
715 u16 rdsc, u16 rdsd, s8 *cbleft)
718 u8 val[SI4713_RDSBUFF_NRESP];
720 const u8 args[SI4713_RDSBUFF_NARGS] = {
721 mode & SI4713_RDSBUFF_MODE_MASK,
730 err = si4713_send_command(sdev, SI4713_CMD_TX_RDS_BUFF,
731 args, ARRAY_SIZE(args), val,
732 ARRAY_SIZE(val), DEFAULT_TIMEOUT);
735 v4l2_dbg(1, debug, &sdev->sd,
736 "%s: status=0x%02x\n", __func__, val[0]);
737 *cbleft = (s8)val[2] - val[3];
738 v4l2_dbg(1, debug, &sdev->sd, "%s: response: interrupts"
739 " 0x%02x cb avail: %d cb used %d fifo avail"
740 " %d fifo used %d\n", __func__, val[1],
741 val[2], val[3], val[4], val[5]);
748 * si4713_tx_rds_ps - Loads the program service buffer.
749 * @sdev: si4713_device structure for the device we are communicating
750 * @psid: program service id to be loaded.
751 * @pschar: assumed 4 size char array to be loaded into the program service
753 static int si4713_tx_rds_ps(struct si4713_device *sdev, u8 psid,
754 unsigned char *pschar)
757 u8 val[SI4713_RDSPS_NRESP];
759 const u8 args[SI4713_RDSPS_NARGS] = {
760 psid & SI4713_RDSPS_PSID_MASK,
767 err = si4713_send_command(sdev, SI4713_CMD_TX_RDS_PS,
768 args, ARRAY_SIZE(args), val,
769 ARRAY_SIZE(val), DEFAULT_TIMEOUT);
774 v4l2_dbg(1, debug, &sdev->sd, "%s: status=0x%02x\n", __func__, val[0]);
779 static int si4713_set_power_state(struct si4713_device *sdev, u8 value)
783 mutex_lock(&sdev->mutex);
786 rval = si4713_powerup(sdev);
788 rval = si4713_powerdown(sdev);
790 mutex_unlock(&sdev->mutex);
794 static int si4713_set_mute(struct si4713_device *sdev, u16 mute)
798 mute = set_mute(mute);
800 mutex_lock(&sdev->mutex);
802 if (sdev->power_state)
803 rval = si4713_write_property(sdev,
804 SI4713_TX_LINE_INPUT_MUTE, mute);
807 sdev->mute = get_mute(mute);
809 mutex_unlock(&sdev->mutex);
814 static int si4713_set_rds_ps_name(struct si4713_device *sdev, char *ps_name)
819 /* We want to clear the whole thing */
820 if (!strlen(ps_name))
821 memset(ps_name, 0, MAX_RDS_PS_NAME + 1);
823 mutex_lock(&sdev->mutex);
825 if (sdev->power_state) {
826 /* Write the new ps name and clear the padding */
827 for (i = 0; i < MAX_RDS_PS_NAME; i += (RDS_BLOCK / 2)) {
828 rval = si4713_tx_rds_ps(sdev, (i / (RDS_BLOCK / 2)),
834 /* Setup the size to be sent */
836 len = strlen(ps_name) - 1;
840 rval = si4713_write_property(sdev,
841 SI4713_TX_RDS_PS_MESSAGE_COUNT,
842 rds_ps_nblocks(len));
846 rval = si4713_write_property(sdev,
847 SI4713_TX_RDS_PS_REPEAT_COUNT,
848 DEFAULT_RDS_PS_REPEAT_COUNT * 2);
853 strncpy(sdev->rds_info.ps_name, ps_name, MAX_RDS_PS_NAME);
856 mutex_unlock(&sdev->mutex);
860 static int si4713_set_rds_radio_text(struct si4713_device *sdev, char *rt)
864 u8 b_index = 0, cr_inserted = 0;
867 mutex_lock(&sdev->mutex);
869 if (!sdev->power_state)
872 rval = si4713_tx_rds_buff(sdev, RDS_BLOCK_CLEAR, 0, 0, 0, &left);
880 /* RDS spec says that if the last block isn't used,
881 * then apply a carriage return
883 if (t_index < (RDS_RADIOTEXT_INDEX_MAX *
884 RDS_RADIOTEXT_BLK_SIZE)) {
885 for (i = 0; i < RDS_RADIOTEXT_BLK_SIZE; i++) {
886 if (!rt[t_index + i] || rt[t_index + i] ==
887 RDS_CARRIAGE_RETURN) {
888 rt[t_index + i] = RDS_CARRIAGE_RETURN;
895 rval = si4713_tx_rds_buff(sdev, RDS_BLOCK_LOAD,
896 compose_u16(RDS_RADIOTEXT_2A, b_index++),
897 compose_u16(rt[t_index], rt[t_index + 1]),
898 compose_u16(rt[t_index + 2], rt[t_index + 3]),
903 t_index += RDS_RADIOTEXT_BLK_SIZE;
910 strncpy(sdev->rds_info.radio_text, rt, MAX_RDS_RADIO_TEXT);
913 mutex_unlock(&sdev->mutex);
917 static int si4713_choose_econtrol_action(struct si4713_device *sdev, u32 id,
918 u32 **shadow, s32 *bit, s32 *mask, u16 *property, int *mul,
919 unsigned long **table, int *size)
924 /* FM_TX class controls */
925 case V4L2_CID_RDS_TX_PI:
926 *property = SI4713_TX_RDS_PI;
928 *shadow = &sdev->rds_info.pi;
930 case V4L2_CID_AUDIO_COMPRESSION_THRESHOLD:
931 *property = SI4713_TX_ACOMP_THRESHOLD;
933 *shadow = &sdev->acomp_info.threshold;
935 case V4L2_CID_AUDIO_COMPRESSION_GAIN:
936 *property = SI4713_TX_ACOMP_GAIN;
938 *shadow = &sdev->acomp_info.gain;
940 case V4L2_CID_PILOT_TONE_FREQUENCY:
941 *property = SI4713_TX_PILOT_FREQUENCY;
943 *shadow = &sdev->pilot_info.frequency;
945 case V4L2_CID_AUDIO_COMPRESSION_ATTACK_TIME:
946 *property = SI4713_TX_ACOMP_ATTACK_TIME;
947 *mul = ATTACK_TIME_UNIT;
948 *shadow = &sdev->acomp_info.attack_time;
950 case V4L2_CID_PILOT_TONE_DEVIATION:
951 *property = SI4713_TX_PILOT_DEVIATION;
953 *shadow = &sdev->pilot_info.deviation;
955 case V4L2_CID_AUDIO_LIMITER_DEVIATION:
956 *property = SI4713_TX_AUDIO_DEVIATION;
958 *shadow = &sdev->limiter_info.deviation;
960 case V4L2_CID_RDS_TX_DEVIATION:
961 *property = SI4713_TX_RDS_DEVIATION;
963 *shadow = &sdev->rds_info.deviation;
966 case V4L2_CID_RDS_TX_PTY:
967 *property = SI4713_TX_RDS_PS_MISC;
970 *shadow = &sdev->rds_info.pty;
972 case V4L2_CID_AUDIO_LIMITER_ENABLED:
973 *property = SI4713_TX_ACOMP_ENABLE;
976 *shadow = &sdev->limiter_info.enabled;
978 case V4L2_CID_AUDIO_COMPRESSION_ENABLED:
979 *property = SI4713_TX_ACOMP_ENABLE;
982 *shadow = &sdev->acomp_info.enabled;
984 case V4L2_CID_PILOT_TONE_ENABLED:
985 *property = SI4713_TX_COMPONENT_ENABLE;
988 *shadow = &sdev->pilot_info.enabled;
991 case V4L2_CID_AUDIO_LIMITER_RELEASE_TIME:
992 *property = SI4713_TX_LIMITER_RELEASE_TIME;
993 *table = limiter_times;
994 *size = ARRAY_SIZE(limiter_times);
995 *shadow = &sdev->limiter_info.release_time;
997 case V4L2_CID_AUDIO_COMPRESSION_RELEASE_TIME:
998 *property = SI4713_TX_ACOMP_RELEASE_TIME;
999 *table = acomp_rtimes;
1000 *size = ARRAY_SIZE(acomp_rtimes);
1001 *shadow = &sdev->acomp_info.release_time;
1003 case V4L2_CID_TUNE_PREEMPHASIS:
1004 *property = SI4713_TX_PREEMPHASIS;
1005 *table = preemphasis_values;
1006 *size = ARRAY_SIZE(preemphasis_values);
1007 *shadow = &sdev->preemphasis;
1017 static int si4713_queryctrl(struct v4l2_subdev *sd, struct v4l2_queryctrl *qc);
1019 /* write string property */
1020 static int si4713_write_econtrol_string(struct si4713_device *sdev,
1021 struct v4l2_ext_control *control)
1023 struct v4l2_queryctrl vqc;
1027 vqc.id = control->id;
1028 rval = si4713_queryctrl(&sdev->sd, &vqc);
1032 switch (control->id) {
1033 case V4L2_CID_RDS_TX_PS_NAME: {
1034 char ps_name[MAX_RDS_PS_NAME + 1];
1036 len = control->size - 1;
1037 if (len < 0 || len > MAX_RDS_PS_NAME) {
1041 rval = copy_from_user(ps_name, control->string, len);
1046 ps_name[len] = '\0';
1048 if (strlen(ps_name) % vqc.step) {
1053 rval = si4713_set_rds_ps_name(sdev, ps_name);
1057 case V4L2_CID_RDS_TX_RADIO_TEXT: {
1058 char radio_text[MAX_RDS_RADIO_TEXT + 1];
1060 len = control->size - 1;
1061 if (len < 0 || len > MAX_RDS_RADIO_TEXT) {
1065 rval = copy_from_user(radio_text, control->string, len);
1070 radio_text[len] = '\0';
1072 if (strlen(radio_text) % vqc.step) {
1077 rval = si4713_set_rds_radio_text(sdev, radio_text);
1090 static int validate_range(struct v4l2_subdev *sd,
1091 struct v4l2_ext_control *control)
1093 struct v4l2_queryctrl vqc;
1096 vqc.id = control->id;
1097 rval = si4713_queryctrl(sd, &vqc);
1101 if (control->value < vqc.minimum || control->value > vqc.maximum)
1108 /* properties which use tx_tune_power*/
1109 static int si4713_write_econtrol_tune(struct si4713_device *sdev,
1110 struct v4l2_ext_control *control)
1115 rval = validate_range(&sdev->sd, control);
1119 mutex_lock(&sdev->mutex);
1121 switch (control->id) {
1122 case V4L2_CID_TUNE_POWER_LEVEL:
1123 power = control->value;
1124 antcap = sdev->antenna_capacitor;
1126 case V4L2_CID_TUNE_ANTENNA_CAPACITOR:
1127 power = sdev->power_level;
1128 antcap = control->value;
1135 if (sdev->power_state)
1136 rval = si4713_tx_tune_power(sdev, power, antcap);
1139 sdev->power_level = power;
1140 sdev->antenna_capacitor = antcap;
1144 mutex_unlock(&sdev->mutex);
1149 static int si4713_write_econtrol_integers(struct si4713_device *sdev,
1150 struct v4l2_ext_control *control)
1153 u32 *shadow = NULL, val = 0;
1154 s32 bit = 0, mask = 0;
1157 unsigned long *table = NULL;
1160 rval = validate_range(&sdev->sd, control);
1164 rval = si4713_choose_econtrol_action(sdev, control->id, &shadow, &bit,
1165 &mask, &property, &mul, &table, &size);
1169 val = control->value;
1171 val = control->value / mul;
1173 rval = usecs_to_dev(control->value, table, size);
1180 mutex_lock(&sdev->mutex);
1182 if (sdev->power_state) {
1184 rval = si4713_read_property(sdev, property, &val);
1187 val = set_bits(val, control->value, bit, mask);
1190 rval = si4713_write_property(sdev, property, val);
1194 val = control->value;
1198 *shadow = val * mul;
1200 rval = dev_to_usecs(val, table, size);
1210 mutex_unlock(&sdev->mutex);
1215 static int si4713_s_frequency(struct v4l2_subdev *sd, struct v4l2_frequency *f);
1216 static int si4713_s_modulator(struct v4l2_subdev *sd, struct v4l2_modulator *);
1218 * si4713_setup - Sets the device up with current configuration.
1219 * @sdev: si4713_device structure for the device we are communicating
1221 static int si4713_setup(struct si4713_device *sdev)
1223 struct v4l2_ext_control ctrl;
1224 struct v4l2_frequency f;
1225 struct v4l2_modulator vm;
1226 struct si4713_device *tmp;
1229 tmp = kmalloc(sizeof(*tmp), GFP_KERNEL);
1233 /* Get a local copy to avoid race */
1234 mutex_lock(&sdev->mutex);
1235 memcpy(tmp, sdev, sizeof(*sdev));
1236 mutex_unlock(&sdev->mutex);
1238 ctrl.id = V4L2_CID_RDS_TX_PI;
1239 ctrl.value = tmp->rds_info.pi;
1240 rval |= si4713_write_econtrol_integers(sdev, &ctrl);
1242 ctrl.id = V4L2_CID_AUDIO_COMPRESSION_THRESHOLD;
1243 ctrl.value = tmp->acomp_info.threshold;
1244 rval |= si4713_write_econtrol_integers(sdev, &ctrl);
1246 ctrl.id = V4L2_CID_AUDIO_COMPRESSION_GAIN;
1247 ctrl.value = tmp->acomp_info.gain;
1248 rval |= si4713_write_econtrol_integers(sdev, &ctrl);
1250 ctrl.id = V4L2_CID_PILOT_TONE_FREQUENCY;
1251 ctrl.value = tmp->pilot_info.frequency;
1252 rval |= si4713_write_econtrol_integers(sdev, &ctrl);
1254 ctrl.id = V4L2_CID_AUDIO_COMPRESSION_ATTACK_TIME;
1255 ctrl.value = tmp->acomp_info.attack_time;
1256 rval |= si4713_write_econtrol_integers(sdev, &ctrl);
1258 ctrl.id = V4L2_CID_PILOT_TONE_DEVIATION;
1259 ctrl.value = tmp->pilot_info.deviation;
1260 rval |= si4713_write_econtrol_integers(sdev, &ctrl);
1262 ctrl.id = V4L2_CID_AUDIO_LIMITER_DEVIATION;
1263 ctrl.value = tmp->limiter_info.deviation;
1264 rval |= si4713_write_econtrol_integers(sdev, &ctrl);
1266 ctrl.id = V4L2_CID_RDS_TX_DEVIATION;
1267 ctrl.value = tmp->rds_info.deviation;
1268 rval |= si4713_write_econtrol_integers(sdev, &ctrl);
1270 ctrl.id = V4L2_CID_RDS_TX_PTY;
1271 ctrl.value = tmp->rds_info.pty;
1272 rval |= si4713_write_econtrol_integers(sdev, &ctrl);
1274 ctrl.id = V4L2_CID_AUDIO_LIMITER_ENABLED;
1275 ctrl.value = tmp->limiter_info.enabled;
1276 rval |= si4713_write_econtrol_integers(sdev, &ctrl);
1278 ctrl.id = V4L2_CID_AUDIO_COMPRESSION_ENABLED;
1279 ctrl.value = tmp->acomp_info.enabled;
1280 rval |= si4713_write_econtrol_integers(sdev, &ctrl);
1282 ctrl.id = V4L2_CID_PILOT_TONE_ENABLED;
1283 ctrl.value = tmp->pilot_info.enabled;
1284 rval |= si4713_write_econtrol_integers(sdev, &ctrl);
1286 ctrl.id = V4L2_CID_AUDIO_LIMITER_RELEASE_TIME;
1287 ctrl.value = tmp->limiter_info.release_time;
1288 rval |= si4713_write_econtrol_integers(sdev, &ctrl);
1290 ctrl.id = V4L2_CID_AUDIO_COMPRESSION_RELEASE_TIME;
1291 ctrl.value = tmp->acomp_info.release_time;
1292 rval |= si4713_write_econtrol_integers(sdev, &ctrl);
1294 ctrl.id = V4L2_CID_TUNE_PREEMPHASIS;
1295 ctrl.value = tmp->preemphasis;
1296 rval |= si4713_write_econtrol_integers(sdev, &ctrl);
1298 ctrl.id = V4L2_CID_RDS_TX_PS_NAME;
1299 rval |= si4713_set_rds_ps_name(sdev, tmp->rds_info.ps_name);
1301 ctrl.id = V4L2_CID_RDS_TX_RADIO_TEXT;
1302 rval |= si4713_set_rds_radio_text(sdev, tmp->rds_info.radio_text);
1304 /* Device procedure needs to set frequency first */
1305 f.frequency = tmp->frequency ? tmp->frequency : DEFAULT_FREQUENCY;
1306 f.frequency = si4713_to_v4l2(f.frequency);
1307 rval |= si4713_s_frequency(&sdev->sd, &f);
1309 ctrl.id = V4L2_CID_TUNE_POWER_LEVEL;
1310 ctrl.value = tmp->power_level;
1311 rval |= si4713_write_econtrol_tune(sdev, &ctrl);
1313 ctrl.id = V4L2_CID_TUNE_ANTENNA_CAPACITOR;
1314 ctrl.value = tmp->antenna_capacitor;
1315 rval |= si4713_write_econtrol_tune(sdev, &ctrl);
1319 vm.txsubchans = V4L2_TUNER_SUB_STEREO;
1321 vm.txsubchans = V4L2_TUNER_SUB_MONO;
1322 if (tmp->rds_info.enabled)
1323 vm.txsubchans |= V4L2_TUNER_SUB_RDS;
1324 si4713_s_modulator(&sdev->sd, &vm);
1332 * si4713_initialize - Sets the device up with default configuration.
1333 * @sdev: si4713_device structure for the device we are communicating
1335 static int si4713_initialize(struct si4713_device *sdev)
1339 rval = si4713_set_power_state(sdev, POWER_ON);
1343 rval = si4713_checkrev(sdev);
1347 rval = si4713_set_power_state(sdev, POWER_OFF);
1351 mutex_lock(&sdev->mutex);
1353 sdev->rds_info.pi = DEFAULT_RDS_PI;
1354 sdev->rds_info.pty = DEFAULT_RDS_PTY;
1355 sdev->rds_info.deviation = DEFAULT_RDS_DEVIATION;
1356 strlcpy(sdev->rds_info.ps_name, DEFAULT_RDS_PS_NAME, MAX_RDS_PS_NAME);
1357 strlcpy(sdev->rds_info.radio_text, DEFAULT_RDS_RADIO_TEXT,
1358 MAX_RDS_RADIO_TEXT);
1359 sdev->rds_info.enabled = 1;
1361 sdev->limiter_info.release_time = DEFAULT_LIMITER_RTIME;
1362 sdev->limiter_info.deviation = DEFAULT_LIMITER_DEV;
1363 sdev->limiter_info.enabled = 1;
1365 sdev->pilot_info.deviation = DEFAULT_PILOT_DEVIATION;
1366 sdev->pilot_info.frequency = DEFAULT_PILOT_FREQUENCY;
1367 sdev->pilot_info.enabled = 1;
1369 sdev->acomp_info.release_time = DEFAULT_ACOMP_RTIME;
1370 sdev->acomp_info.attack_time = DEFAULT_ACOMP_ATIME;
1371 sdev->acomp_info.threshold = DEFAULT_ACOMP_THRESHOLD;
1372 sdev->acomp_info.gain = DEFAULT_ACOMP_GAIN;
1373 sdev->acomp_info.enabled = 1;
1375 sdev->frequency = DEFAULT_FREQUENCY;
1376 sdev->preemphasis = DEFAULT_PREEMPHASIS;
1377 sdev->mute = DEFAULT_MUTE;
1378 sdev->power_level = DEFAULT_POWER_LEVEL;
1379 sdev->antenna_capacitor = 0;
1381 sdev->tune_rnl = DEFAULT_TUNE_RNL;
1383 mutex_unlock(&sdev->mutex);
1389 /* read string property */
1390 static int si4713_read_econtrol_string(struct si4713_device *sdev,
1391 struct v4l2_ext_control *control)
1395 switch (control->id) {
1396 case V4L2_CID_RDS_TX_PS_NAME:
1397 if (strlen(sdev->rds_info.ps_name) + 1 > control->size) {
1398 control->size = MAX_RDS_PS_NAME + 1;
1402 rval = copy_to_user(control->string, sdev->rds_info.ps_name,
1403 strlen(sdev->rds_info.ps_name) + 1);
1408 case V4L2_CID_RDS_TX_RADIO_TEXT:
1409 if (strlen(sdev->rds_info.radio_text) + 1 > control->size) {
1410 control->size = MAX_RDS_RADIO_TEXT + 1;
1414 rval = copy_to_user(control->string, sdev->rds_info.radio_text,
1415 strlen(sdev->rds_info.radio_text) + 1);
1430 * si4713_update_tune_status - update properties from tx_tune_status
1431 * command. Must be called with sdev->mutex held.
1432 * @sdev: si4713_device structure for the device we are communicating
1434 static int si4713_update_tune_status(struct si4713_device *sdev)
1438 u8 p = 0, a = 0, n = 0;
1440 rval = si4713_tx_tune_status(sdev, 0x00, &f, &p, &a, &n);
1445 sdev->power_level = p;
1446 sdev->antenna_capacitor = a;
1453 /* properties which use tx_tune_status */
1454 static int si4713_read_econtrol_tune(struct si4713_device *sdev,
1455 struct v4l2_ext_control *control)
1459 mutex_lock(&sdev->mutex);
1461 if (sdev->power_state) {
1462 rval = si4713_update_tune_status(sdev);
1467 switch (control->id) {
1468 case V4L2_CID_TUNE_POWER_LEVEL:
1469 control->value = sdev->power_level;
1471 case V4L2_CID_TUNE_ANTENNA_CAPACITOR:
1472 control->value = sdev->antenna_capacitor;
1479 mutex_unlock(&sdev->mutex);
1483 static int si4713_read_econtrol_integers(struct si4713_device *sdev,
1484 struct v4l2_ext_control *control)
1487 u32 *shadow = NULL, val = 0;
1488 s32 bit = 0, mask = 0;
1491 unsigned long *table = NULL;
1494 rval = si4713_choose_econtrol_action(sdev, control->id, &shadow, &bit,
1495 &mask, &property, &mul, &table, &size);
1499 mutex_lock(&sdev->mutex);
1501 if (sdev->power_state) {
1502 rval = si4713_read_property(sdev, property, &val);
1506 /* Keep negative values for threshold */
1507 if (control->id == V4L2_CID_AUDIO_COMPRESSION_THRESHOLD)
1510 *shadow = get_status_bit(val, bit, mask);
1512 *shadow = val * mul;
1514 *shadow = dev_to_usecs(val, table, size);
1517 control->value = *shadow;
1520 mutex_unlock(&sdev->mutex);
1526 * Video4Linux Subdev Interface
1528 /* si4713_s_ext_ctrls - set extended controls value */
1529 static int si4713_s_ext_ctrls(struct v4l2_subdev *sd,
1530 struct v4l2_ext_controls *ctrls)
1532 struct si4713_device *sdev = to_si4713_device(sd);
1535 if (ctrls->ctrl_class != V4L2_CTRL_CLASS_FM_TX)
1538 for (i = 0; i < ctrls->count; i++) {
1541 switch ((ctrls->controls + i)->id) {
1542 case V4L2_CID_RDS_TX_PS_NAME:
1543 case V4L2_CID_RDS_TX_RADIO_TEXT:
1544 err = si4713_write_econtrol_string(sdev,
1545 ctrls->controls + i);
1547 case V4L2_CID_TUNE_ANTENNA_CAPACITOR:
1548 case V4L2_CID_TUNE_POWER_LEVEL:
1549 err = si4713_write_econtrol_tune(sdev,
1550 ctrls->controls + i);
1553 err = si4713_write_econtrol_integers(sdev,
1554 ctrls->controls + i);
1558 ctrls->error_idx = i;
1566 /* si4713_g_ext_ctrls - get extended controls value */
1567 static int si4713_g_ext_ctrls(struct v4l2_subdev *sd,
1568 struct v4l2_ext_controls *ctrls)
1570 struct si4713_device *sdev = to_si4713_device(sd);
1573 if (ctrls->ctrl_class != V4L2_CTRL_CLASS_FM_TX)
1576 for (i = 0; i < ctrls->count; i++) {
1579 switch ((ctrls->controls + i)->id) {
1580 case V4L2_CID_RDS_TX_PS_NAME:
1581 case V4L2_CID_RDS_TX_RADIO_TEXT:
1582 err = si4713_read_econtrol_string(sdev,
1583 ctrls->controls + i);
1585 case V4L2_CID_TUNE_ANTENNA_CAPACITOR:
1586 case V4L2_CID_TUNE_POWER_LEVEL:
1587 err = si4713_read_econtrol_tune(sdev,
1588 ctrls->controls + i);
1591 err = si4713_read_econtrol_integers(sdev,
1592 ctrls->controls + i);
1596 ctrls->error_idx = i;
1604 /* si4713_queryctrl - enumerate control items */
1605 static int si4713_queryctrl(struct v4l2_subdev *sd, struct v4l2_queryctrl *qc)
1610 /* User class controls */
1611 case V4L2_CID_AUDIO_MUTE:
1612 rval = v4l2_ctrl_query_fill(qc, 0, 1, 1, DEFAULT_MUTE);
1614 /* FM_TX class controls */
1615 case V4L2_CID_RDS_TX_PI:
1616 rval = v4l2_ctrl_query_fill(qc, 0, 0xFFFF, 1, DEFAULT_RDS_PI);
1618 case V4L2_CID_RDS_TX_PTY:
1619 rval = v4l2_ctrl_query_fill(qc, 0, 31, 1, DEFAULT_RDS_PTY);
1621 case V4L2_CID_RDS_TX_DEVIATION:
1622 rval = v4l2_ctrl_query_fill(qc, 0, MAX_RDS_DEVIATION,
1623 10, DEFAULT_RDS_DEVIATION);
1625 case V4L2_CID_RDS_TX_PS_NAME:
1627 * Report step as 8. From RDS spec, psname
1628 * should be 8. But there are receivers which scroll strings
1631 rval = v4l2_ctrl_query_fill(qc, 0, MAX_RDS_PS_NAME, 8, 0);
1633 case V4L2_CID_RDS_TX_RADIO_TEXT:
1635 * Report step as 32 (2A block). From RDS spec,
1636 * radio text should be 32 for 2A block. But there are receivers
1637 * which scroll strings sized as 32xN. Setting default to 32.
1639 rval = v4l2_ctrl_query_fill(qc, 0, MAX_RDS_RADIO_TEXT, 32, 0);
1642 case V4L2_CID_AUDIO_LIMITER_ENABLED:
1643 rval = v4l2_ctrl_query_fill(qc, 0, 1, 1, 1);
1645 case V4L2_CID_AUDIO_LIMITER_RELEASE_TIME:
1646 rval = v4l2_ctrl_query_fill(qc, 250, MAX_LIMITER_RELEASE_TIME,
1647 50, DEFAULT_LIMITER_RTIME);
1649 case V4L2_CID_AUDIO_LIMITER_DEVIATION:
1650 rval = v4l2_ctrl_query_fill(qc, 0, MAX_LIMITER_DEVIATION,
1651 10, DEFAULT_LIMITER_DEV);
1654 case V4L2_CID_AUDIO_COMPRESSION_ENABLED:
1655 rval = v4l2_ctrl_query_fill(qc, 0, 1, 1, 1);
1657 case V4L2_CID_AUDIO_COMPRESSION_GAIN:
1658 rval = v4l2_ctrl_query_fill(qc, 0, MAX_ACOMP_GAIN, 1,
1659 DEFAULT_ACOMP_GAIN);
1661 case V4L2_CID_AUDIO_COMPRESSION_THRESHOLD:
1662 rval = v4l2_ctrl_query_fill(qc, MIN_ACOMP_THRESHOLD,
1663 MAX_ACOMP_THRESHOLD, 1,
1664 DEFAULT_ACOMP_THRESHOLD);
1666 case V4L2_CID_AUDIO_COMPRESSION_ATTACK_TIME:
1667 rval = v4l2_ctrl_query_fill(qc, 0, MAX_ACOMP_ATTACK_TIME,
1668 500, DEFAULT_ACOMP_ATIME);
1670 case V4L2_CID_AUDIO_COMPRESSION_RELEASE_TIME:
1671 rval = v4l2_ctrl_query_fill(qc, 100000, MAX_ACOMP_RELEASE_TIME,
1672 100000, DEFAULT_ACOMP_RTIME);
1675 case V4L2_CID_PILOT_TONE_ENABLED:
1676 rval = v4l2_ctrl_query_fill(qc, 0, 1, 1, 1);
1678 case V4L2_CID_PILOT_TONE_DEVIATION:
1679 rval = v4l2_ctrl_query_fill(qc, 0, MAX_PILOT_DEVIATION,
1680 10, DEFAULT_PILOT_DEVIATION);
1682 case V4L2_CID_PILOT_TONE_FREQUENCY:
1683 rval = v4l2_ctrl_query_fill(qc, 0, MAX_PILOT_FREQUENCY,
1684 1, DEFAULT_PILOT_FREQUENCY);
1687 case V4L2_CID_TUNE_PREEMPHASIS:
1688 rval = v4l2_ctrl_query_fill(qc, V4L2_PREEMPHASIS_DISABLED,
1689 V4L2_PREEMPHASIS_75_uS, 1,
1690 V4L2_PREEMPHASIS_50_uS);
1692 case V4L2_CID_TUNE_POWER_LEVEL:
1693 rval = v4l2_ctrl_query_fill(qc, 0, 120, 1, DEFAULT_POWER_LEVEL);
1695 case V4L2_CID_TUNE_ANTENNA_CAPACITOR:
1696 rval = v4l2_ctrl_query_fill(qc, 0, 191, 1, 0);
1706 /* si4713_g_ctrl - get the value of a control */
1707 static int si4713_g_ctrl(struct v4l2_subdev *sd, struct v4l2_control *ctrl)
1709 struct si4713_device *sdev = to_si4713_device(sd);
1715 mutex_lock(&sdev->mutex);
1717 if (sdev->power_state) {
1718 rval = si4713_read_property(sdev, SI4713_TX_LINE_INPUT_MUTE,
1726 case V4L2_CID_AUDIO_MUTE:
1727 ctrl->value = get_mute(sdev->mute);
1732 mutex_unlock(&sdev->mutex);
1736 /* si4713_s_ctrl - set the value of a control */
1737 static int si4713_s_ctrl(struct v4l2_subdev *sd, struct v4l2_control *ctrl)
1739 struct si4713_device *sdev = to_si4713_device(sd);
1746 case V4L2_CID_AUDIO_MUTE:
1748 rval = si4713_set_mute(sdev, ctrl->value);
1752 rval = si4713_set_power_state(sdev, POWER_DOWN);
1754 rval = si4713_set_power_state(sdev, POWER_UP);
1758 rval = si4713_setup(sdev);
1762 rval = si4713_set_mute(sdev, ctrl->value);
1771 /* si4713_ioctl - deal with private ioctls (only rnl for now) */
1772 long si4713_ioctl(struct v4l2_subdev *sd, unsigned int cmd, void *arg)
1774 struct si4713_device *sdev = to_si4713_device(sd);
1775 struct si4713_rnl *rnl = arg;
1782 mutex_lock(&sdev->mutex);
1784 case SI4713_IOC_MEASURE_RNL:
1785 frequency = v4l2_to_si4713(rnl->frequency);
1787 if (sdev->power_state) {
1788 /* Set desired measurement frequency */
1789 rval = si4713_tx_tune_measure(sdev, frequency, 0);
1792 /* get results from tune status */
1793 rval = si4713_update_tune_status(sdev);
1797 rnl->rnl = sdev->tune_rnl;
1802 rval = -ENOIOCTLCMD;
1806 mutex_unlock(&sdev->mutex);
1810 static const struct v4l2_subdev_core_ops si4713_subdev_core_ops = {
1811 .queryctrl = si4713_queryctrl,
1812 .g_ext_ctrls = si4713_g_ext_ctrls,
1813 .s_ext_ctrls = si4713_s_ext_ctrls,
1814 .g_ctrl = si4713_g_ctrl,
1815 .s_ctrl = si4713_s_ctrl,
1816 .ioctl = si4713_ioctl,
1819 /* si4713_g_modulator - get modulator attributes */
1820 static int si4713_g_modulator(struct v4l2_subdev *sd, struct v4l2_modulator *vm)
1822 struct si4713_device *sdev = to_si4713_device(sd);
1830 if (vm->index > 0) {
1835 strncpy(vm->name, "FM Modulator", 32);
1836 vm->capability = V4L2_TUNER_CAP_STEREO | V4L2_TUNER_CAP_LOW |
1837 V4L2_TUNER_CAP_RDS | V4L2_TUNER_CAP_RDS_CONTROLS;
1839 /* Report current frequency range limits */
1840 vm->rangelow = si4713_to_v4l2(FREQ_RANGE_LOW);
1841 vm->rangehigh = si4713_to_v4l2(FREQ_RANGE_HIGH);
1843 mutex_lock(&sdev->mutex);
1845 if (sdev->power_state) {
1848 rval = si4713_read_property(sdev, SI4713_TX_COMPONENT_ENABLE,
1853 sdev->stereo = get_status_bit(comp_en, 1, 1 << 1);
1854 sdev->rds_info.enabled = get_status_bit(comp_en, 2, 1 << 2);
1857 /* Report current audio mode: mono or stereo */
1859 vm->txsubchans = V4L2_TUNER_SUB_STEREO;
1861 vm->txsubchans = V4L2_TUNER_SUB_MONO;
1863 /* Report rds feature status */
1864 if (sdev->rds_info.enabled)
1865 vm->txsubchans |= V4L2_TUNER_SUB_RDS;
1867 vm->txsubchans &= ~V4L2_TUNER_SUB_RDS;
1870 mutex_unlock(&sdev->mutex);
1875 /* si4713_s_modulator - set modulator attributes */
1876 static int si4713_s_modulator(struct v4l2_subdev *sd, struct v4l2_modulator *vm)
1878 struct si4713_device *sdev = to_si4713_device(sd);
1889 /* Set audio mode: mono or stereo */
1890 if (vm->txsubchans & V4L2_TUNER_SUB_STEREO)
1892 else if (vm->txsubchans & V4L2_TUNER_SUB_MONO)
1897 rds = !!(vm->txsubchans & V4L2_TUNER_SUB_RDS);
1899 mutex_lock(&sdev->mutex);
1901 if (sdev->power_state) {
1902 rval = si4713_read_property(sdev,
1903 SI4713_TX_COMPONENT_ENABLE, &p);
1907 p = set_bits(p, stereo, 1, 1 << 1);
1908 p = set_bits(p, rds, 2, 1 << 2);
1910 rval = si4713_write_property(sdev,
1911 SI4713_TX_COMPONENT_ENABLE, p);
1916 sdev->stereo = stereo;
1917 sdev->rds_info.enabled = rds;
1920 mutex_unlock(&sdev->mutex);
1924 /* si4713_g_frequency - get tuner or modulator radio frequency */
1925 static int si4713_g_frequency(struct v4l2_subdev *sd, struct v4l2_frequency *f)
1927 struct si4713_device *sdev = to_si4713_device(sd);
1930 f->type = V4L2_TUNER_RADIO;
1932 mutex_lock(&sdev->mutex);
1934 if (sdev->power_state) {
1938 rval = si4713_tx_tune_status(sdev, 0x00, &freq, &p, &a, &n);
1942 sdev->frequency = freq;
1945 f->frequency = si4713_to_v4l2(sdev->frequency);
1948 mutex_unlock(&sdev->mutex);
1952 /* si4713_s_frequency - set tuner or modulator radio frequency */
1953 static int si4713_s_frequency(struct v4l2_subdev *sd, struct v4l2_frequency *f)
1955 struct si4713_device *sdev = to_si4713_device(sd);
1957 u16 frequency = v4l2_to_si4713(f->frequency);
1959 /* Check frequency range */
1960 if (frequency < FREQ_RANGE_LOW || frequency > FREQ_RANGE_HIGH)
1963 mutex_lock(&sdev->mutex);
1965 if (sdev->power_state) {
1966 rval = si4713_tx_tune_freq(sdev, frequency);
1972 sdev->frequency = frequency;
1973 f->frequency = si4713_to_v4l2(frequency);
1976 mutex_unlock(&sdev->mutex);
1980 static const struct v4l2_subdev_tuner_ops si4713_subdev_tuner_ops = {
1981 .g_frequency = si4713_g_frequency,
1982 .s_frequency = si4713_s_frequency,
1983 .g_modulator = si4713_g_modulator,
1984 .s_modulator = si4713_s_modulator,
1987 static const struct v4l2_subdev_ops si4713_subdev_ops = {
1988 .core = &si4713_subdev_core_ops,
1989 .tuner = &si4713_subdev_tuner_ops,
1993 * I2C driver interface
1995 /* si4713_probe - probe for the device */
1996 static int si4713_probe(struct i2c_client *client,
1997 const struct i2c_device_id *id)
1999 struct si4713_device *sdev;
2000 struct si4713_platform_data *pdata = client->dev.platform_data;
2003 sdev = kzalloc(sizeof *sdev, GFP_KERNEL);
2005 dev_err(&client->dev, "Failed to alloc video device.\n");
2010 sdev->gpio_reset = -1;
2011 if (pdata && gpio_is_valid(pdata->gpio_reset)) {
2012 rval = gpio_request(pdata->gpio_reset, "si4713 reset");
2014 dev_err(&client->dev,
2015 "Failed to request gpio: %d\n", rval);
2018 sdev->gpio_reset = pdata->gpio_reset;
2019 gpio_direction_output(sdev->gpio_reset, 0);
2022 for (i = 0; i < ARRAY_SIZE(sdev->supplies); i++)
2023 sdev->supplies[i].supply = si4713_supply_names[i];
2025 rval = regulator_bulk_get(&client->dev, ARRAY_SIZE(sdev->supplies),
2028 dev_err(&client->dev, "Cannot get regulators: %d\n", rval);
2032 v4l2_i2c_subdev_init(&sdev->sd, client, &si4713_subdev_ops);
2034 mutex_init(&sdev->mutex);
2035 init_completion(&sdev->work);
2038 rval = request_irq(client->irq,
2039 si4713_handler, IRQF_TRIGGER_FALLING | IRQF_DISABLED,
2040 client->name, sdev);
2042 v4l2_err(&sdev->sd, "Could not request IRQ\n");
2045 v4l2_dbg(1, debug, &sdev->sd, "IRQ requested.\n");
2047 v4l2_warn(&sdev->sd, "IRQ not configured. Using timeouts.\n");
2050 rval = si4713_initialize(sdev);
2052 v4l2_err(&sdev->sd, "Failed to probe device information.\n");
2060 free_irq(client->irq, sdev);
2062 regulator_bulk_free(ARRAY_SIZE(sdev->supplies), sdev->supplies);
2064 if (gpio_is_valid(sdev->gpio_reset))
2065 gpio_free(sdev->gpio_reset);
2072 /* si4713_remove - remove the device */
2073 static int si4713_remove(struct i2c_client *client)
2075 struct v4l2_subdev *sd = i2c_get_clientdata(client);
2076 struct si4713_device *sdev = to_si4713_device(sd);
2078 if (sdev->power_state)
2079 si4713_set_power_state(sdev, POWER_DOWN);
2081 if (client->irq > 0)
2082 free_irq(client->irq, sdev);
2084 v4l2_device_unregister_subdev(sd);
2085 regulator_bulk_free(ARRAY_SIZE(sdev->supplies), sdev->supplies);
2086 if (gpio_is_valid(sdev->gpio_reset))
2087 gpio_free(sdev->gpio_reset);
2093 /* si4713_i2c_driver - i2c driver interface */
2094 static const struct i2c_device_id si4713_id[] = {
2098 MODULE_DEVICE_TABLE(i2c, si4713_id);
2100 static struct i2c_driver si4713_i2c_driver = {
2104 .probe = si4713_probe,
2105 .remove = si4713_remove,
2106 .id_table = si4713_id,
2109 module_i2c_driver(si4713_i2c_driver);