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 <media/v4l2-device.h>
33 #include <media/v4l2-ioctl.h>
34 #include <media/v4l2-common.h>
36 #include "si4713-i2c.h"
38 /* module parameters */
40 module_param(debug, int, S_IRUGO | S_IWUSR);
41 MODULE_PARM_DESC(debug, "Debug level (0 - 2)");
43 MODULE_LICENSE("GPL");
44 MODULE_AUTHOR("Eduardo Valentin <eduardo.valentin@nokia.com>");
45 MODULE_DESCRIPTION("I2C driver for Si4713 FM Radio Transmitter");
46 MODULE_VERSION("0.0.1");
48 static const char *si4713_supply_names[SI4713_NUM_SUPPLIES] = {
53 #define DEFAULT_RDS_PI 0x00
54 #define DEFAULT_RDS_PTY 0x00
55 #define DEFAULT_RDS_PS_NAME ""
56 #define DEFAULT_RDS_RADIO_TEXT DEFAULT_RDS_PS_NAME
57 #define DEFAULT_RDS_DEVIATION 0x00C8
58 #define DEFAULT_RDS_PS_REPEAT_COUNT 0x0003
59 #define DEFAULT_LIMITER_RTIME 0x1392
60 #define DEFAULT_LIMITER_DEV 0x102CA
61 #define DEFAULT_PILOT_FREQUENCY 0x4A38
62 #define DEFAULT_PILOT_DEVIATION 0x1A5E
63 #define DEFAULT_ACOMP_ATIME 0x0000
64 #define DEFAULT_ACOMP_RTIME 0xF4240L
65 #define DEFAULT_ACOMP_GAIN 0x0F
66 #define DEFAULT_ACOMP_THRESHOLD (-0x28)
67 #define DEFAULT_MUTE 0x01
68 #define DEFAULT_POWER_LEVEL 88
69 #define DEFAULT_FREQUENCY 8800
70 #define DEFAULT_PREEMPHASIS FMPE_EU
71 #define DEFAULT_TUNE_RNL 0xFF
73 #define to_si4713_device(sd) container_of(sd, struct si4713_device, sd)
75 /* frequency domain transformation (using times 10 to avoid floats) */
76 #define FREQDEV_UNIT 100000
77 #define FREQV4L2_MULTI 625
78 #define si4713_to_v4l2(f) ((f * FREQDEV_UNIT) / FREQV4L2_MULTI)
79 #define v4l2_to_si4713(f) ((f * FREQV4L2_MULTI) / FREQDEV_UNIT)
80 #define FREQ_RANGE_LOW 7600
81 #define FREQ_RANGE_HIGH 10800
86 #define RDS_BLOCK_CLEAR 0x03
87 #define RDS_BLOCK_LOAD 0x04
88 #define RDS_RADIOTEXT_2A 0x20
89 #define RDS_RADIOTEXT_BLK_SIZE 4
90 #define RDS_RADIOTEXT_INDEX_MAX 0x0F
91 #define RDS_CARRIAGE_RETURN 0x0D
93 #define rds_ps_nblocks(len) ((len / RDS_BLOCK) + (len % RDS_BLOCK ? 1 : 0))
95 #define get_status_bit(p, b, m) (((p) & (m)) >> (b))
96 #define set_bits(p, v, b, m) (((p) & ~(m)) | ((v) << (b)))
98 #define ATTACK_TIME_UNIT 500
100 #define POWER_OFF 0x00
101 #define POWER_ON 0x01
103 #define msb(x) ((u8)((u16) x >> 8))
104 #define lsb(x) ((u8)((u16) x & 0x00FF))
105 #define compose_u16(msb, lsb) (((u16)msb << 8) | lsb)
106 #define check_command_failed(status) (!(status & SI4713_CTS) || \
107 (status & SI4713_ERR))
108 /* mute definition */
109 #define set_mute(p) ((p & 1) | ((p & 1) << 1));
110 #define get_mute(p) (p & 0x01)
113 #define DBG_BUFFER(device, message, buffer, size) \
116 char str[(size)*5]; \
117 for (i = 0; i < size; i++) \
118 sprintf(str + i * 5, " 0x%02x", buffer[i]); \
119 v4l2_dbg(2, debug, device, "%s:%s\n", message, str); \
122 #define DBG_BUFFER(device, message, buffer, size)
126 * Values for limiter release time (sorted by second column)
130 static long limiter_times[] = {
154 * Values for audio compression release time (sorted by second column)
158 static unsigned long acomp_rtimes[] = {
167 * Values for preemphasis (sorted by second column)
171 static unsigned long preemphasis_values[] = {
172 FMPE_DISABLED, V4L2_PREEMPHASIS_DISABLED,
173 FMPE_EU, V4L2_PREEMPHASIS_50_uS,
174 FMPE_USA, V4L2_PREEMPHASIS_75_uS,
177 static int usecs_to_dev(unsigned long usecs, unsigned long const array[],
183 for (i = 0; i < size / 2; i++)
184 if (array[(i * 2) + 1] >= usecs) {
192 static unsigned long dev_to_usecs(int value, unsigned long const array[],
198 for (i = 0; i < size / 2; i++)
199 if (array[i * 2] == value) {
200 rval = array[(i * 2) + 1];
207 /* si4713_handler: IRQ handler, just complete work */
208 static irqreturn_t si4713_handler(int irq, void *dev)
210 struct si4713_device *sdev = dev;
212 v4l2_dbg(2, debug, &sdev->sd,
213 "%s: sending signal to completion work.\n", __func__);
214 complete(&sdev->work);
220 * si4713_send_command - sends a command to si4713 and waits its response
221 * @sdev: si4713_device structure for the device we are communicating
222 * @command: command id
223 * @args: command arguments we are sending (up to 7)
224 * @argn: actual size of @args
225 * @response: buffer to place the expected response from the device (up to 15)
226 * @respn: actual size of @response
227 * @usecs: amount of time to wait before reading the response (in usecs)
229 static int si4713_send_command(struct si4713_device *sdev, const u8 command,
230 const u8 args[], const int argn,
231 u8 response[], const int respn, const int usecs)
233 struct i2c_client *client = v4l2_get_subdevdata(&sdev->sd);
234 u8 data1[MAX_ARGS + 1];
237 if (!client->adapter)
240 /* First send the command and its arguments */
242 memcpy(data1 + 1, args, argn);
243 DBG_BUFFER(&sdev->sd, "Parameters", data1, argn + 1);
245 err = i2c_master_send(client, data1, argn + 1);
246 if (err != argn + 1) {
247 v4l2_err(&sdev->sd, "Error while sending command 0x%02x\n",
249 return (err > 0) ? -EIO : err;
252 /* Wait response from interrupt */
253 if (!wait_for_completion_timeout(&sdev->work,
254 usecs_to_jiffies(usecs) + 1))
256 "(%s) Device took too much time to answer.\n",
259 /* Then get the response */
260 err = i2c_master_recv(client, response, respn);
263 "Error while reading response for command 0x%02x\n",
265 return (err > 0) ? -EIO : err;
268 DBG_BUFFER(&sdev->sd, "Response", response, respn);
269 if (check_command_failed(response[0]))
276 * si4713_read_property - reads a si4713 property
277 * @sdev: si4713_device structure for the device we are communicating
278 * @prop: property identification number
279 * @pv: property value to be returned on success
281 static int si4713_read_property(struct si4713_device *sdev, u16 prop, u32 *pv)
284 u8 val[SI4713_GET_PROP_NRESP];
287 * .Second byte = property's MSB
288 * .Third byte = property's LSB
290 const u8 args[SI4713_GET_PROP_NARGS] = {
296 err = si4713_send_command(sdev, SI4713_CMD_GET_PROPERTY,
297 args, ARRAY_SIZE(args), val,
298 ARRAY_SIZE(val), DEFAULT_TIMEOUT);
303 *pv = compose_u16(val[2], val[3]);
305 v4l2_dbg(1, debug, &sdev->sd,
306 "%s: property=0x%02x value=0x%02x status=0x%02x\n",
307 __func__, prop, *pv, val[0]);
313 * si4713_write_property - modifies a si4713 property
314 * @sdev: si4713_device structure for the device we are communicating
315 * @prop: property identification number
316 * @val: new value for that property
318 static int si4713_write_property(struct si4713_device *sdev, u16 prop, u16 val)
321 u8 resp[SI4713_SET_PROP_NRESP];
324 * .Second byte = property's MSB
325 * .Third byte = property's LSB
326 * .Fourth byte = value's MSB
327 * .Fifth byte = value's LSB
329 const u8 args[SI4713_SET_PROP_NARGS] = {
337 rval = si4713_send_command(sdev, SI4713_CMD_SET_PROPERTY,
338 args, ARRAY_SIZE(args),
339 resp, ARRAY_SIZE(resp),
345 v4l2_dbg(1, debug, &sdev->sd,
346 "%s: property=0x%02x value=0x%02x status=0x%02x\n",
347 __func__, prop, val, resp[0]);
350 * As there is no command response for SET_PROPERTY,
351 * wait Tcomp time to finish before proceed, in order
352 * to have property properly set.
354 msleep(TIMEOUT_SET_PROPERTY);
360 * si4713_powerup - Powers the device up
361 * @sdev: si4713_device structure for the device we are communicating
363 static int si4713_powerup(struct si4713_device *sdev)
366 u8 resp[SI4713_PWUP_NRESP];
368 * .First byte = Enabled interrupts and boot function
369 * .Second byte = Input operation mode
371 const u8 args[SI4713_PWUP_NARGS] = {
372 SI4713_PWUP_CTSIEN | SI4713_PWUP_GPO2OEN | SI4713_PWUP_FUNC_TX,
373 SI4713_PWUP_OPMOD_ANALOG,
376 if (sdev->power_state)
379 err = regulator_bulk_enable(ARRAY_SIZE(sdev->supplies),
382 v4l2_err(&sdev->sd, "Failed to enable supplies: %d\n", err);
385 if (gpio_is_valid(sdev->gpio_reset)) {
387 gpio_set_value(sdev->gpio_reset, 1);
390 err = si4713_send_command(sdev, SI4713_CMD_POWER_UP,
391 args, ARRAY_SIZE(args),
392 resp, ARRAY_SIZE(resp),
396 v4l2_dbg(1, debug, &sdev->sd, "Powerup response: 0x%02x\n",
398 v4l2_dbg(1, debug, &sdev->sd, "Device in power up mode\n");
399 sdev->power_state = POWER_ON;
401 err = si4713_write_property(sdev, SI4713_GPO_IEN,
402 SI4713_STC_INT | SI4713_CTS);
404 if (gpio_is_valid(sdev->gpio_reset))
405 gpio_set_value(sdev->gpio_reset, 0);
406 err = regulator_bulk_disable(ARRAY_SIZE(sdev->supplies),
410 "Failed to disable supplies: %d\n", err);
417 * si4713_powerdown - Powers the device down
418 * @sdev: si4713_device structure for the device we are communicating
420 static int si4713_powerdown(struct si4713_device *sdev)
423 u8 resp[SI4713_PWDN_NRESP];
425 if (!sdev->power_state)
428 err = si4713_send_command(sdev, SI4713_CMD_POWER_DOWN,
430 resp, ARRAY_SIZE(resp),
434 v4l2_dbg(1, debug, &sdev->sd, "Power down response: 0x%02x\n",
436 v4l2_dbg(1, debug, &sdev->sd, "Device in reset mode\n");
437 if (gpio_is_valid(sdev->gpio_reset))
438 gpio_set_value(sdev->gpio_reset, 0);
439 err = regulator_bulk_disable(ARRAY_SIZE(sdev->supplies),
443 "Failed to disable supplies: %d\n", err);
444 sdev->power_state = POWER_OFF;
451 * si4713_checkrev - Checks if we are treating a device with the correct rev.
452 * @sdev: si4713_device structure for the device we are communicating
454 static int si4713_checkrev(struct si4713_device *sdev)
456 struct i2c_client *client = v4l2_get_subdevdata(&sdev->sd);
458 u8 resp[SI4713_GETREV_NRESP];
460 mutex_lock(&sdev->mutex);
462 rval = si4713_send_command(sdev, SI4713_CMD_GET_REV,
464 resp, ARRAY_SIZE(resp),
470 if (resp[1] == SI4713_PRODUCT_NUMBER) {
471 v4l2_info(&sdev->sd, "chip found @ 0x%02x (%s)\n",
472 client->addr << 1, client->adapter->name);
474 v4l2_err(&sdev->sd, "Invalid product number\n");
479 mutex_unlock(&sdev->mutex);
484 * si4713_wait_stc - Waits STC interrupt and clears status bits. Useful
485 * for TX_TUNE_POWER, TX_TUNE_FREQ and TX_TUNE_MEAS
486 * @sdev: si4713_device structure for the device we are communicating
487 * @usecs: timeout to wait for STC interrupt signal
489 static int si4713_wait_stc(struct si4713_device *sdev, const int usecs)
492 u8 resp[SI4713_GET_STATUS_NRESP];
494 /* Wait response from STC interrupt */
495 if (!wait_for_completion_timeout(&sdev->work,
496 usecs_to_jiffies(usecs) + 1))
498 "%s: device took too much time to answer (%d usec).\n",
501 /* Clear status bits */
502 err = si4713_send_command(sdev, SI4713_CMD_GET_INT_STATUS,
504 resp, ARRAY_SIZE(resp),
510 v4l2_dbg(1, debug, &sdev->sd,
511 "%s: status bits: 0x%02x\n", __func__, resp[0]);
513 if (!(resp[0] & SI4713_STC_INT))
521 * si4713_tx_tune_freq - Sets the state of the RF carrier and sets the tuning
522 * frequency between 76 and 108 MHz in 10 kHz units and
524 * @sdev: si4713_device structure for the device we are communicating
525 * @frequency: desired frequency (76 - 108 MHz, unit 10 KHz, step 50 kHz)
527 static int si4713_tx_tune_freq(struct si4713_device *sdev, u16 frequency)
530 u8 val[SI4713_TXFREQ_NRESP];
533 * .Second byte = frequency's MSB
534 * .Third byte = frequency's LSB
536 const u8 args[SI4713_TXFREQ_NARGS] = {
542 err = si4713_send_command(sdev, SI4713_CMD_TX_TUNE_FREQ,
543 args, ARRAY_SIZE(args), val,
544 ARRAY_SIZE(val), DEFAULT_TIMEOUT);
549 v4l2_dbg(1, debug, &sdev->sd,
550 "%s: frequency=0x%02x status=0x%02x\n", __func__,
553 err = si4713_wait_stc(sdev, TIMEOUT_TX_TUNE);
557 return compose_u16(args[1], args[2]);
561 * si4713_tx_tune_power - Sets the RF voltage level between 88 and 115 dBuV in
562 * 1 dB units. A value of 0x00 indicates off. The command
563 * also sets the antenna tuning capacitance. A value of 0
564 * indicates autotuning, and a value of 1 - 191 indicates
565 * a manual override, which results in a tuning
566 * capacitance of 0.25 pF x @antcap.
567 * @sdev: si4713_device structure for the device we are communicating
568 * @power: tuning power (88 - 115 dBuV, unit/step 1 dB)
569 * @antcap: value of antenna tuning capacitor (0 - 191)
571 static int si4713_tx_tune_power(struct si4713_device *sdev, u8 power,
575 u8 val[SI4713_TXPWR_NRESP];
579 * .Third byte = power
580 * .Fourth byte = antcap
582 const u8 args[SI4713_TXPWR_NARGS] = {
589 if (((power > 0) && (power < SI4713_MIN_POWER)) ||
590 power > SI4713_MAX_POWER || antcap > SI4713_MAX_ANTCAP)
593 err = si4713_send_command(sdev, SI4713_CMD_TX_TUNE_POWER,
594 args, ARRAY_SIZE(args), val,
595 ARRAY_SIZE(val), DEFAULT_TIMEOUT);
600 v4l2_dbg(1, debug, &sdev->sd,
601 "%s: power=0x%02x antcap=0x%02x status=0x%02x\n",
602 __func__, power, antcap, val[0]);
604 return si4713_wait_stc(sdev, TIMEOUT_TX_TUNE_POWER);
608 * si4713_tx_tune_measure - Enters receive mode and measures the received noise
609 * level in units of dBuV on the selected frequency.
610 * The Frequency must be between 76 and 108 MHz in 10 kHz
611 * units and steps of 50 kHz. The command also sets the
612 * antenna tuning capacitance. A value of 0 means
613 * autotuning, and a value of 1 to 191 indicates manual
615 * @sdev: si4713_device structure for the device we are communicating
616 * @frequency: desired frequency (76 - 108 MHz, unit 10 KHz, step 50 kHz)
617 * @antcap: value of antenna tuning capacitor (0 - 191)
619 static int si4713_tx_tune_measure(struct si4713_device *sdev, u16 frequency,
623 u8 val[SI4713_TXMEA_NRESP];
626 * .Second byte = frequency's MSB
627 * .Third byte = frequency's LSB
628 * .Fourth byte = antcap
630 const u8 args[SI4713_TXMEA_NARGS] = {
637 sdev->tune_rnl = DEFAULT_TUNE_RNL;
639 if (antcap > SI4713_MAX_ANTCAP)
642 err = si4713_send_command(sdev, SI4713_CMD_TX_TUNE_MEASURE,
643 args, ARRAY_SIZE(args), val,
644 ARRAY_SIZE(val), DEFAULT_TIMEOUT);
649 v4l2_dbg(1, debug, &sdev->sd,
650 "%s: frequency=0x%02x antcap=0x%02x status=0x%02x\n",
651 __func__, frequency, antcap, val[0]);
653 return si4713_wait_stc(sdev, TIMEOUT_TX_TUNE);
657 * si4713_tx_tune_status- Returns the status of the tx_tune_freq, tx_tune_mea or
658 * tx_tune_power commands. This command return the current
659 * frequency, output voltage in dBuV, the antenna tunning
660 * capacitance value and the received noise level. The
661 * command also clears the stcint interrupt bit when the
662 * first bit of its arguments is high.
663 * @sdev: si4713_device structure for the device we are communicating
664 * @intack: 0x01 to clear the seek/tune complete interrupt status indicator.
665 * @frequency: returned frequency
666 * @power: returned power
667 * @antcap: returned antenna capacitance
668 * @noise: returned noise level
670 static int si4713_tx_tune_status(struct si4713_device *sdev, u8 intack,
671 u16 *frequency, u8 *power,
672 u8 *antcap, u8 *noise)
675 u8 val[SI4713_TXSTATUS_NRESP];
677 * .First byte = intack bit
679 const u8 args[SI4713_TXSTATUS_NARGS] = {
680 intack & SI4713_INTACK_MASK,
683 err = si4713_send_command(sdev, SI4713_CMD_TX_TUNE_STATUS,
684 args, ARRAY_SIZE(args), val,
685 ARRAY_SIZE(val), DEFAULT_TIMEOUT);
688 v4l2_dbg(1, debug, &sdev->sd,
689 "%s: status=0x%02x\n", __func__, val[0]);
690 *frequency = compose_u16(val[2], val[3]);
691 sdev->frequency = *frequency;
695 v4l2_dbg(1, debug, &sdev->sd, "%s: response: %d x 10 kHz "
696 "(power %d, antcap %d, rnl %d)\n", __func__,
697 *frequency, *power, *antcap, *noise);
704 * si4713_tx_rds_buff - Loads the RDS group buffer FIFO or circular buffer.
705 * @sdev: si4713_device structure for the device we are communicating
706 * @mode: the buffer operation mode.
710 * @cbleft: returns the number of available circular buffer blocks minus the
711 * number of used circular buffer blocks.
713 static int si4713_tx_rds_buff(struct si4713_device *sdev, u8 mode, u16 rdsb,
714 u16 rdsc, u16 rdsd, s8 *cbleft)
717 u8 val[SI4713_RDSBUFF_NRESP];
719 const u8 args[SI4713_RDSBUFF_NARGS] = {
720 mode & SI4713_RDSBUFF_MODE_MASK,
729 err = si4713_send_command(sdev, SI4713_CMD_TX_RDS_BUFF,
730 args, ARRAY_SIZE(args), val,
731 ARRAY_SIZE(val), DEFAULT_TIMEOUT);
734 v4l2_dbg(1, debug, &sdev->sd,
735 "%s: status=0x%02x\n", __func__, val[0]);
736 *cbleft = (s8)val[2] - val[3];
737 v4l2_dbg(1, debug, &sdev->sd, "%s: response: interrupts"
738 " 0x%02x cb avail: %d cb used %d fifo avail"
739 " %d fifo used %d\n", __func__, val[1],
740 val[2], val[3], val[4], val[5]);
747 * si4713_tx_rds_ps - Loads the program service buffer.
748 * @sdev: si4713_device structure for the device we are communicating
749 * @psid: program service id to be loaded.
750 * @pschar: assumed 4 size char array to be loaded into the program service
752 static int si4713_tx_rds_ps(struct si4713_device *sdev, u8 psid,
753 unsigned char *pschar)
756 u8 val[SI4713_RDSPS_NRESP];
758 const u8 args[SI4713_RDSPS_NARGS] = {
759 psid & SI4713_RDSPS_PSID_MASK,
766 err = si4713_send_command(sdev, SI4713_CMD_TX_RDS_PS,
767 args, ARRAY_SIZE(args), val,
768 ARRAY_SIZE(val), DEFAULT_TIMEOUT);
773 v4l2_dbg(1, debug, &sdev->sd, "%s: status=0x%02x\n", __func__, val[0]);
778 static int si4713_set_power_state(struct si4713_device *sdev, u8 value)
782 mutex_lock(&sdev->mutex);
785 rval = si4713_powerup(sdev);
787 rval = si4713_powerdown(sdev);
789 mutex_unlock(&sdev->mutex);
793 static int si4713_set_mute(struct si4713_device *sdev, u16 mute)
797 mute = set_mute(mute);
799 mutex_lock(&sdev->mutex);
801 if (sdev->power_state)
802 rval = si4713_write_property(sdev,
803 SI4713_TX_LINE_INPUT_MUTE, mute);
806 sdev->mute = get_mute(mute);
808 mutex_unlock(&sdev->mutex);
813 static int si4713_set_rds_ps_name(struct si4713_device *sdev, char *ps_name)
818 /* We want to clear the whole thing */
819 if (!strlen(ps_name))
820 memset(ps_name, 0, MAX_RDS_PS_NAME + 1);
822 mutex_lock(&sdev->mutex);
824 if (sdev->power_state) {
825 /* Write the new ps name and clear the padding */
826 for (i = 0; i < MAX_RDS_PS_NAME; i += (RDS_BLOCK / 2)) {
827 rval = si4713_tx_rds_ps(sdev, (i / (RDS_BLOCK / 2)),
833 /* Setup the size to be sent */
835 len = strlen(ps_name) - 1;
839 rval = si4713_write_property(sdev,
840 SI4713_TX_RDS_PS_MESSAGE_COUNT,
841 rds_ps_nblocks(len));
845 rval = si4713_write_property(sdev,
846 SI4713_TX_RDS_PS_REPEAT_COUNT,
847 DEFAULT_RDS_PS_REPEAT_COUNT * 2);
852 strncpy(sdev->rds_info.ps_name, ps_name, MAX_RDS_PS_NAME);
855 mutex_unlock(&sdev->mutex);
859 static int si4713_set_rds_radio_text(struct si4713_device *sdev, char *rt)
863 u8 b_index = 0, cr_inserted = 0;
866 mutex_lock(&sdev->mutex);
868 if (!sdev->power_state)
871 rval = si4713_tx_rds_buff(sdev, RDS_BLOCK_CLEAR, 0, 0, 0, &left);
879 /* RDS spec says that if the last block isn't used,
880 * then apply a carriage return
882 if (t_index < (RDS_RADIOTEXT_INDEX_MAX *
883 RDS_RADIOTEXT_BLK_SIZE)) {
884 for (i = 0; i < RDS_RADIOTEXT_BLK_SIZE; i++) {
885 if (!rt[t_index + i] || rt[t_index + i] ==
886 RDS_CARRIAGE_RETURN) {
887 rt[t_index + i] = RDS_CARRIAGE_RETURN;
894 rval = si4713_tx_rds_buff(sdev, RDS_BLOCK_LOAD,
895 compose_u16(RDS_RADIOTEXT_2A, b_index++),
896 compose_u16(rt[t_index], rt[t_index + 1]),
897 compose_u16(rt[t_index + 2], rt[t_index + 3]),
902 t_index += RDS_RADIOTEXT_BLK_SIZE;
909 strncpy(sdev->rds_info.radio_text, rt, MAX_RDS_RADIO_TEXT);
912 mutex_unlock(&sdev->mutex);
916 static int si4713_choose_econtrol_action(struct si4713_device *sdev, u32 id,
917 u32 **shadow, s32 *bit, s32 *mask, u16 *property, int *mul,
918 unsigned long **table, int *size)
923 /* FM_TX class controls */
924 case V4L2_CID_RDS_TX_PI:
925 *property = SI4713_TX_RDS_PI;
927 *shadow = &sdev->rds_info.pi;
929 case V4L2_CID_AUDIO_COMPRESSION_THRESHOLD:
930 *property = SI4713_TX_ACOMP_THRESHOLD;
932 *shadow = &sdev->acomp_info.threshold;
934 case V4L2_CID_AUDIO_COMPRESSION_GAIN:
935 *property = SI4713_TX_ACOMP_GAIN;
937 *shadow = &sdev->acomp_info.gain;
939 case V4L2_CID_PILOT_TONE_FREQUENCY:
940 *property = SI4713_TX_PILOT_FREQUENCY;
942 *shadow = &sdev->pilot_info.frequency;
944 case V4L2_CID_AUDIO_COMPRESSION_ATTACK_TIME:
945 *property = SI4713_TX_ACOMP_ATTACK_TIME;
946 *mul = ATTACK_TIME_UNIT;
947 *shadow = &sdev->acomp_info.attack_time;
949 case V4L2_CID_PILOT_TONE_DEVIATION:
950 *property = SI4713_TX_PILOT_DEVIATION;
952 *shadow = &sdev->pilot_info.deviation;
954 case V4L2_CID_AUDIO_LIMITER_DEVIATION:
955 *property = SI4713_TX_AUDIO_DEVIATION;
957 *shadow = &sdev->limiter_info.deviation;
959 case V4L2_CID_RDS_TX_DEVIATION:
960 *property = SI4713_TX_RDS_DEVIATION;
962 *shadow = &sdev->rds_info.deviation;
965 case V4L2_CID_RDS_TX_PTY:
966 *property = SI4713_TX_RDS_PS_MISC;
969 *shadow = &sdev->rds_info.pty;
971 case V4L2_CID_AUDIO_LIMITER_ENABLED:
972 *property = SI4713_TX_ACOMP_ENABLE;
975 *shadow = &sdev->limiter_info.enabled;
977 case V4L2_CID_AUDIO_COMPRESSION_ENABLED:
978 *property = SI4713_TX_ACOMP_ENABLE;
981 *shadow = &sdev->acomp_info.enabled;
983 case V4L2_CID_PILOT_TONE_ENABLED:
984 *property = SI4713_TX_COMPONENT_ENABLE;
987 *shadow = &sdev->pilot_info.enabled;
990 case V4L2_CID_AUDIO_LIMITER_RELEASE_TIME:
991 *property = SI4713_TX_LIMITER_RELEASE_TIME;
992 *table = limiter_times;
993 *size = ARRAY_SIZE(limiter_times);
994 *shadow = &sdev->limiter_info.release_time;
996 case V4L2_CID_AUDIO_COMPRESSION_RELEASE_TIME:
997 *property = SI4713_TX_ACOMP_RELEASE_TIME;
998 *table = acomp_rtimes;
999 *size = ARRAY_SIZE(acomp_rtimes);
1000 *shadow = &sdev->acomp_info.release_time;
1002 case V4L2_CID_TUNE_PREEMPHASIS:
1003 *property = SI4713_TX_PREEMPHASIS;
1004 *table = preemphasis_values;
1005 *size = ARRAY_SIZE(preemphasis_values);
1006 *shadow = &sdev->preemphasis;
1016 static int si4713_queryctrl(struct v4l2_subdev *sd, struct v4l2_queryctrl *qc);
1018 /* write string property */
1019 static int si4713_write_econtrol_string(struct si4713_device *sdev,
1020 struct v4l2_ext_control *control)
1022 struct v4l2_queryctrl vqc;
1026 vqc.id = control->id;
1027 rval = si4713_queryctrl(&sdev->sd, &vqc);
1031 switch (control->id) {
1032 case V4L2_CID_RDS_TX_PS_NAME: {
1033 char ps_name[MAX_RDS_PS_NAME + 1];
1035 len = control->size - 1;
1036 if (len < 0 || len > MAX_RDS_PS_NAME) {
1040 rval = copy_from_user(ps_name, control->string, len);
1045 ps_name[len] = '\0';
1047 if (strlen(ps_name) % vqc.step) {
1052 rval = si4713_set_rds_ps_name(sdev, ps_name);
1056 case V4L2_CID_RDS_TX_RADIO_TEXT: {
1057 char radio_text[MAX_RDS_RADIO_TEXT + 1];
1059 len = control->size - 1;
1060 if (len < 0 || len > MAX_RDS_RADIO_TEXT) {
1064 rval = copy_from_user(radio_text, control->string, len);
1069 radio_text[len] = '\0';
1071 if (strlen(radio_text) % vqc.step) {
1076 rval = si4713_set_rds_radio_text(sdev, radio_text);
1089 static int validate_range(struct v4l2_subdev *sd,
1090 struct v4l2_ext_control *control)
1092 struct v4l2_queryctrl vqc;
1095 vqc.id = control->id;
1096 rval = si4713_queryctrl(sd, &vqc);
1100 if (control->value < vqc.minimum || control->value > vqc.maximum)
1107 /* properties which use tx_tune_power*/
1108 static int si4713_write_econtrol_tune(struct si4713_device *sdev,
1109 struct v4l2_ext_control *control)
1114 rval = validate_range(&sdev->sd, control);
1118 mutex_lock(&sdev->mutex);
1120 switch (control->id) {
1121 case V4L2_CID_TUNE_POWER_LEVEL:
1122 power = control->value;
1123 antcap = sdev->antenna_capacitor;
1125 case V4L2_CID_TUNE_ANTENNA_CAPACITOR:
1126 power = sdev->power_level;
1127 antcap = control->value;
1134 if (sdev->power_state)
1135 rval = si4713_tx_tune_power(sdev, power, antcap);
1138 sdev->power_level = power;
1139 sdev->antenna_capacitor = antcap;
1143 mutex_unlock(&sdev->mutex);
1148 static int si4713_write_econtrol_integers(struct si4713_device *sdev,
1149 struct v4l2_ext_control *control)
1152 u32 *shadow = NULL, val = 0;
1153 s32 bit = 0, mask = 0;
1156 unsigned long *table = NULL;
1159 rval = validate_range(&sdev->sd, control);
1163 rval = si4713_choose_econtrol_action(sdev, control->id, &shadow, &bit,
1164 &mask, &property, &mul, &table, &size);
1168 val = control->value;
1170 val = control->value / mul;
1172 rval = usecs_to_dev(control->value, table, size);
1179 mutex_lock(&sdev->mutex);
1181 if (sdev->power_state) {
1183 rval = si4713_read_property(sdev, property, &val);
1186 val = set_bits(val, control->value, bit, mask);
1189 rval = si4713_write_property(sdev, property, val);
1193 val = control->value;
1197 *shadow = val * mul;
1199 rval = dev_to_usecs(val, table, size);
1209 mutex_unlock(&sdev->mutex);
1214 static int si4713_s_frequency(struct v4l2_subdev *sd, struct v4l2_frequency *f);
1215 static int si4713_s_modulator(struct v4l2_subdev *sd, struct v4l2_modulator *);
1217 * si4713_setup - Sets the device up with current configuration.
1218 * @sdev: si4713_device structure for the device we are communicating
1220 static int si4713_setup(struct si4713_device *sdev)
1222 struct v4l2_ext_control ctrl;
1223 struct v4l2_frequency f;
1224 struct v4l2_modulator vm;
1225 struct si4713_device *tmp;
1228 tmp = kmalloc(sizeof(*tmp), GFP_KERNEL);
1232 /* Get a local copy to avoid race */
1233 mutex_lock(&sdev->mutex);
1234 memcpy(tmp, sdev, sizeof(*sdev));
1235 mutex_unlock(&sdev->mutex);
1237 ctrl.id = V4L2_CID_RDS_TX_PI;
1238 ctrl.value = tmp->rds_info.pi;
1239 rval |= si4713_write_econtrol_integers(sdev, &ctrl);
1241 ctrl.id = V4L2_CID_AUDIO_COMPRESSION_THRESHOLD;
1242 ctrl.value = tmp->acomp_info.threshold;
1243 rval |= si4713_write_econtrol_integers(sdev, &ctrl);
1245 ctrl.id = V4L2_CID_AUDIO_COMPRESSION_GAIN;
1246 ctrl.value = tmp->acomp_info.gain;
1247 rval |= si4713_write_econtrol_integers(sdev, &ctrl);
1249 ctrl.id = V4L2_CID_PILOT_TONE_FREQUENCY;
1250 ctrl.value = tmp->pilot_info.frequency;
1251 rval |= si4713_write_econtrol_integers(sdev, &ctrl);
1253 ctrl.id = V4L2_CID_AUDIO_COMPRESSION_ATTACK_TIME;
1254 ctrl.value = tmp->acomp_info.attack_time;
1255 rval |= si4713_write_econtrol_integers(sdev, &ctrl);
1257 ctrl.id = V4L2_CID_PILOT_TONE_DEVIATION;
1258 ctrl.value = tmp->pilot_info.deviation;
1259 rval |= si4713_write_econtrol_integers(sdev, &ctrl);
1261 ctrl.id = V4L2_CID_AUDIO_LIMITER_DEVIATION;
1262 ctrl.value = tmp->limiter_info.deviation;
1263 rval |= si4713_write_econtrol_integers(sdev, &ctrl);
1265 ctrl.id = V4L2_CID_RDS_TX_DEVIATION;
1266 ctrl.value = tmp->rds_info.deviation;
1267 rval |= si4713_write_econtrol_integers(sdev, &ctrl);
1269 ctrl.id = V4L2_CID_RDS_TX_PTY;
1270 ctrl.value = tmp->rds_info.pty;
1271 rval |= si4713_write_econtrol_integers(sdev, &ctrl);
1273 ctrl.id = V4L2_CID_AUDIO_LIMITER_ENABLED;
1274 ctrl.value = tmp->limiter_info.enabled;
1275 rval |= si4713_write_econtrol_integers(sdev, &ctrl);
1277 ctrl.id = V4L2_CID_AUDIO_COMPRESSION_ENABLED;
1278 ctrl.value = tmp->acomp_info.enabled;
1279 rval |= si4713_write_econtrol_integers(sdev, &ctrl);
1281 ctrl.id = V4L2_CID_PILOT_TONE_ENABLED;
1282 ctrl.value = tmp->pilot_info.enabled;
1283 rval |= si4713_write_econtrol_integers(sdev, &ctrl);
1285 ctrl.id = V4L2_CID_AUDIO_LIMITER_RELEASE_TIME;
1286 ctrl.value = tmp->limiter_info.release_time;
1287 rval |= si4713_write_econtrol_integers(sdev, &ctrl);
1289 ctrl.id = V4L2_CID_AUDIO_COMPRESSION_RELEASE_TIME;
1290 ctrl.value = tmp->acomp_info.release_time;
1291 rval |= si4713_write_econtrol_integers(sdev, &ctrl);
1293 ctrl.id = V4L2_CID_TUNE_PREEMPHASIS;
1294 ctrl.value = tmp->preemphasis;
1295 rval |= si4713_write_econtrol_integers(sdev, &ctrl);
1297 ctrl.id = V4L2_CID_RDS_TX_PS_NAME;
1298 rval |= si4713_set_rds_ps_name(sdev, tmp->rds_info.ps_name);
1300 ctrl.id = V4L2_CID_RDS_TX_RADIO_TEXT;
1301 rval |= si4713_set_rds_radio_text(sdev, tmp->rds_info.radio_text);
1303 /* Device procedure needs to set frequency first */
1304 f.frequency = tmp->frequency ? tmp->frequency : DEFAULT_FREQUENCY;
1305 f.frequency = si4713_to_v4l2(f.frequency);
1306 rval |= si4713_s_frequency(&sdev->sd, &f);
1308 ctrl.id = V4L2_CID_TUNE_POWER_LEVEL;
1309 ctrl.value = tmp->power_level;
1310 rval |= si4713_write_econtrol_tune(sdev, &ctrl);
1312 ctrl.id = V4L2_CID_TUNE_ANTENNA_CAPACITOR;
1313 ctrl.value = tmp->antenna_capacitor;
1314 rval |= si4713_write_econtrol_tune(sdev, &ctrl);
1318 vm.txsubchans = V4L2_TUNER_SUB_STEREO;
1320 vm.txsubchans = V4L2_TUNER_SUB_MONO;
1321 if (tmp->rds_info.enabled)
1322 vm.txsubchans |= V4L2_TUNER_SUB_RDS;
1323 si4713_s_modulator(&sdev->sd, &vm);
1331 * si4713_initialize - Sets the device up with default configuration.
1332 * @sdev: si4713_device structure for the device we are communicating
1334 static int si4713_initialize(struct si4713_device *sdev)
1338 rval = si4713_set_power_state(sdev, POWER_ON);
1342 rval = si4713_checkrev(sdev);
1346 rval = si4713_set_power_state(sdev, POWER_OFF);
1350 mutex_lock(&sdev->mutex);
1352 sdev->rds_info.pi = DEFAULT_RDS_PI;
1353 sdev->rds_info.pty = DEFAULT_RDS_PTY;
1354 sdev->rds_info.deviation = DEFAULT_RDS_DEVIATION;
1355 strlcpy(sdev->rds_info.ps_name, DEFAULT_RDS_PS_NAME, MAX_RDS_PS_NAME);
1356 strlcpy(sdev->rds_info.radio_text, DEFAULT_RDS_RADIO_TEXT,
1357 MAX_RDS_RADIO_TEXT);
1358 sdev->rds_info.enabled = 1;
1360 sdev->limiter_info.release_time = DEFAULT_LIMITER_RTIME;
1361 sdev->limiter_info.deviation = DEFAULT_LIMITER_DEV;
1362 sdev->limiter_info.enabled = 1;
1364 sdev->pilot_info.deviation = DEFAULT_PILOT_DEVIATION;
1365 sdev->pilot_info.frequency = DEFAULT_PILOT_FREQUENCY;
1366 sdev->pilot_info.enabled = 1;
1368 sdev->acomp_info.release_time = DEFAULT_ACOMP_RTIME;
1369 sdev->acomp_info.attack_time = DEFAULT_ACOMP_ATIME;
1370 sdev->acomp_info.threshold = DEFAULT_ACOMP_THRESHOLD;
1371 sdev->acomp_info.gain = DEFAULT_ACOMP_GAIN;
1372 sdev->acomp_info.enabled = 1;
1374 sdev->frequency = DEFAULT_FREQUENCY;
1375 sdev->preemphasis = DEFAULT_PREEMPHASIS;
1376 sdev->mute = DEFAULT_MUTE;
1377 sdev->power_level = DEFAULT_POWER_LEVEL;
1378 sdev->antenna_capacitor = 0;
1380 sdev->tune_rnl = DEFAULT_TUNE_RNL;
1382 mutex_unlock(&sdev->mutex);
1388 /* read string property */
1389 static int si4713_read_econtrol_string(struct si4713_device *sdev,
1390 struct v4l2_ext_control *control)
1394 switch (control->id) {
1395 case V4L2_CID_RDS_TX_PS_NAME:
1396 if (strlen(sdev->rds_info.ps_name) + 1 > control->size) {
1397 control->size = MAX_RDS_PS_NAME + 1;
1401 rval = copy_to_user(control->string, sdev->rds_info.ps_name,
1402 strlen(sdev->rds_info.ps_name) + 1);
1407 case V4L2_CID_RDS_TX_RADIO_TEXT:
1408 if (strlen(sdev->rds_info.radio_text) + 1 > control->size) {
1409 control->size = MAX_RDS_RADIO_TEXT + 1;
1413 rval = copy_to_user(control->string, sdev->rds_info.radio_text,
1414 strlen(sdev->rds_info.radio_text) + 1);
1429 * si4713_update_tune_status - update properties from tx_tune_status
1430 * command. Must be called with sdev->mutex held.
1431 * @sdev: si4713_device structure for the device we are communicating
1433 static int si4713_update_tune_status(struct si4713_device *sdev)
1437 u8 p = 0, a = 0, n = 0;
1439 rval = si4713_tx_tune_status(sdev, 0x00, &f, &p, &a, &n);
1444 sdev->power_level = p;
1445 sdev->antenna_capacitor = a;
1452 /* properties which use tx_tune_status */
1453 static int si4713_read_econtrol_tune(struct si4713_device *sdev,
1454 struct v4l2_ext_control *control)
1458 mutex_lock(&sdev->mutex);
1460 if (sdev->power_state) {
1461 rval = si4713_update_tune_status(sdev);
1466 switch (control->id) {
1467 case V4L2_CID_TUNE_POWER_LEVEL:
1468 control->value = sdev->power_level;
1470 case V4L2_CID_TUNE_ANTENNA_CAPACITOR:
1471 control->value = sdev->antenna_capacitor;
1478 mutex_unlock(&sdev->mutex);
1482 static int si4713_read_econtrol_integers(struct si4713_device *sdev,
1483 struct v4l2_ext_control *control)
1486 u32 *shadow = NULL, val = 0;
1487 s32 bit = 0, mask = 0;
1490 unsigned long *table = NULL;
1493 rval = si4713_choose_econtrol_action(sdev, control->id, &shadow, &bit,
1494 &mask, &property, &mul, &table, &size);
1498 mutex_lock(&sdev->mutex);
1500 if (sdev->power_state) {
1501 rval = si4713_read_property(sdev, property, &val);
1505 /* Keep negative values for threshold */
1506 if (control->id == V4L2_CID_AUDIO_COMPRESSION_THRESHOLD)
1509 *shadow = get_status_bit(val, bit, mask);
1511 *shadow = val * mul;
1513 *shadow = dev_to_usecs(val, table, size);
1516 control->value = *shadow;
1519 mutex_unlock(&sdev->mutex);
1525 * Video4Linux Subdev Interface
1527 /* si4713_s_ext_ctrls - set extended controls value */
1528 static int si4713_s_ext_ctrls(struct v4l2_subdev *sd,
1529 struct v4l2_ext_controls *ctrls)
1531 struct si4713_device *sdev = to_si4713_device(sd);
1534 if (ctrls->ctrl_class != V4L2_CTRL_CLASS_FM_TX)
1537 for (i = 0; i < ctrls->count; i++) {
1540 switch ((ctrls->controls + i)->id) {
1541 case V4L2_CID_RDS_TX_PS_NAME:
1542 case V4L2_CID_RDS_TX_RADIO_TEXT:
1543 err = si4713_write_econtrol_string(sdev,
1544 ctrls->controls + i);
1546 case V4L2_CID_TUNE_ANTENNA_CAPACITOR:
1547 case V4L2_CID_TUNE_POWER_LEVEL:
1548 err = si4713_write_econtrol_tune(sdev,
1549 ctrls->controls + i);
1552 err = si4713_write_econtrol_integers(sdev,
1553 ctrls->controls + i);
1557 ctrls->error_idx = i;
1565 /* si4713_g_ext_ctrls - get extended controls value */
1566 static int si4713_g_ext_ctrls(struct v4l2_subdev *sd,
1567 struct v4l2_ext_controls *ctrls)
1569 struct si4713_device *sdev = to_si4713_device(sd);
1572 if (ctrls->ctrl_class != V4L2_CTRL_CLASS_FM_TX)
1575 for (i = 0; i < ctrls->count; i++) {
1578 switch ((ctrls->controls + i)->id) {
1579 case V4L2_CID_RDS_TX_PS_NAME:
1580 case V4L2_CID_RDS_TX_RADIO_TEXT:
1581 err = si4713_read_econtrol_string(sdev,
1582 ctrls->controls + i);
1584 case V4L2_CID_TUNE_ANTENNA_CAPACITOR:
1585 case V4L2_CID_TUNE_POWER_LEVEL:
1586 err = si4713_read_econtrol_tune(sdev,
1587 ctrls->controls + i);
1590 err = si4713_read_econtrol_integers(sdev,
1591 ctrls->controls + i);
1595 ctrls->error_idx = i;
1603 /* si4713_queryctrl - enumerate control items */
1604 static int si4713_queryctrl(struct v4l2_subdev *sd, struct v4l2_queryctrl *qc)
1609 /* User class controls */
1610 case V4L2_CID_AUDIO_MUTE:
1611 rval = v4l2_ctrl_query_fill(qc, 0, 1, 1, DEFAULT_MUTE);
1613 /* FM_TX class controls */
1614 case V4L2_CID_RDS_TX_PI:
1615 rval = v4l2_ctrl_query_fill(qc, 0, 0xFFFF, 1, DEFAULT_RDS_PI);
1617 case V4L2_CID_RDS_TX_PTY:
1618 rval = v4l2_ctrl_query_fill(qc, 0, 31, 1, DEFAULT_RDS_PTY);
1620 case V4L2_CID_RDS_TX_DEVIATION:
1621 rval = v4l2_ctrl_query_fill(qc, 0, MAX_RDS_DEVIATION,
1622 10, DEFAULT_RDS_DEVIATION);
1624 case V4L2_CID_RDS_TX_PS_NAME:
1626 * Report step as 8. From RDS spec, psname
1627 * should be 8. But there are receivers which scroll strings
1630 rval = v4l2_ctrl_query_fill(qc, 0, MAX_RDS_PS_NAME, 8, 0);
1632 case V4L2_CID_RDS_TX_RADIO_TEXT:
1634 * Report step as 32 (2A block). From RDS spec,
1635 * radio text should be 32 for 2A block. But there are receivers
1636 * which scroll strings sized as 32xN. Setting default to 32.
1638 rval = v4l2_ctrl_query_fill(qc, 0, MAX_RDS_RADIO_TEXT, 32, 0);
1641 case V4L2_CID_AUDIO_LIMITER_ENABLED:
1642 rval = v4l2_ctrl_query_fill(qc, 0, 1, 1, 1);
1644 case V4L2_CID_AUDIO_LIMITER_RELEASE_TIME:
1645 rval = v4l2_ctrl_query_fill(qc, 250, MAX_LIMITER_RELEASE_TIME,
1646 50, DEFAULT_LIMITER_RTIME);
1648 case V4L2_CID_AUDIO_LIMITER_DEVIATION:
1649 rval = v4l2_ctrl_query_fill(qc, 0, MAX_LIMITER_DEVIATION,
1650 10, DEFAULT_LIMITER_DEV);
1653 case V4L2_CID_AUDIO_COMPRESSION_ENABLED:
1654 rval = v4l2_ctrl_query_fill(qc, 0, 1, 1, 1);
1656 case V4L2_CID_AUDIO_COMPRESSION_GAIN:
1657 rval = v4l2_ctrl_query_fill(qc, 0, MAX_ACOMP_GAIN, 1,
1658 DEFAULT_ACOMP_GAIN);
1660 case V4L2_CID_AUDIO_COMPRESSION_THRESHOLD:
1661 rval = v4l2_ctrl_query_fill(qc, MIN_ACOMP_THRESHOLD,
1662 MAX_ACOMP_THRESHOLD, 1,
1663 DEFAULT_ACOMP_THRESHOLD);
1665 case V4L2_CID_AUDIO_COMPRESSION_ATTACK_TIME:
1666 rval = v4l2_ctrl_query_fill(qc, 0, MAX_ACOMP_ATTACK_TIME,
1667 500, DEFAULT_ACOMP_ATIME);
1669 case V4L2_CID_AUDIO_COMPRESSION_RELEASE_TIME:
1670 rval = v4l2_ctrl_query_fill(qc, 100000, MAX_ACOMP_RELEASE_TIME,
1671 100000, DEFAULT_ACOMP_RTIME);
1674 case V4L2_CID_PILOT_TONE_ENABLED:
1675 rval = v4l2_ctrl_query_fill(qc, 0, 1, 1, 1);
1677 case V4L2_CID_PILOT_TONE_DEVIATION:
1678 rval = v4l2_ctrl_query_fill(qc, 0, MAX_PILOT_DEVIATION,
1679 10, DEFAULT_PILOT_DEVIATION);
1681 case V4L2_CID_PILOT_TONE_FREQUENCY:
1682 rval = v4l2_ctrl_query_fill(qc, 0, MAX_PILOT_FREQUENCY,
1683 1, DEFAULT_PILOT_FREQUENCY);
1686 case V4L2_CID_TUNE_PREEMPHASIS:
1687 rval = v4l2_ctrl_query_fill(qc, V4L2_PREEMPHASIS_DISABLED,
1688 V4L2_PREEMPHASIS_75_uS, 1,
1689 V4L2_PREEMPHASIS_50_uS);
1691 case V4L2_CID_TUNE_POWER_LEVEL:
1692 rval = v4l2_ctrl_query_fill(qc, 0, 120, 1, DEFAULT_POWER_LEVEL);
1694 case V4L2_CID_TUNE_ANTENNA_CAPACITOR:
1695 rval = v4l2_ctrl_query_fill(qc, 0, 191, 1, 0);
1705 /* si4713_g_ctrl - get the value of a control */
1706 static int si4713_g_ctrl(struct v4l2_subdev *sd, struct v4l2_control *ctrl)
1708 struct si4713_device *sdev = to_si4713_device(sd);
1714 mutex_lock(&sdev->mutex);
1716 if (sdev->power_state) {
1717 rval = si4713_read_property(sdev, SI4713_TX_LINE_INPUT_MUTE,
1725 case V4L2_CID_AUDIO_MUTE:
1726 ctrl->value = get_mute(sdev->mute);
1731 mutex_unlock(&sdev->mutex);
1735 /* si4713_s_ctrl - set the value of a control */
1736 static int si4713_s_ctrl(struct v4l2_subdev *sd, struct v4l2_control *ctrl)
1738 struct si4713_device *sdev = to_si4713_device(sd);
1745 case V4L2_CID_AUDIO_MUTE:
1747 rval = si4713_set_mute(sdev, ctrl->value);
1751 rval = si4713_set_power_state(sdev, POWER_DOWN);
1753 rval = si4713_set_power_state(sdev, POWER_UP);
1757 rval = si4713_setup(sdev);
1761 rval = si4713_set_mute(sdev, ctrl->value);
1770 /* si4713_ioctl - deal with private ioctls (only rnl for now) */
1771 long si4713_ioctl(struct v4l2_subdev *sd, unsigned int cmd, void *arg)
1773 struct si4713_device *sdev = to_si4713_device(sd);
1774 struct si4713_rnl *rnl = arg;
1781 mutex_lock(&sdev->mutex);
1783 case SI4713_IOC_MEASURE_RNL:
1784 frequency = v4l2_to_si4713(rnl->frequency);
1786 if (sdev->power_state) {
1787 /* Set desired measurement frequency */
1788 rval = si4713_tx_tune_measure(sdev, frequency, 0);
1791 /* get results from tune status */
1792 rval = si4713_update_tune_status(sdev);
1796 rnl->rnl = sdev->tune_rnl;
1801 rval = -ENOIOCTLCMD;
1805 mutex_unlock(&sdev->mutex);
1809 static const struct v4l2_subdev_core_ops si4713_subdev_core_ops = {
1810 .queryctrl = si4713_queryctrl,
1811 .g_ext_ctrls = si4713_g_ext_ctrls,
1812 .s_ext_ctrls = si4713_s_ext_ctrls,
1813 .g_ctrl = si4713_g_ctrl,
1814 .s_ctrl = si4713_s_ctrl,
1815 .ioctl = si4713_ioctl,
1818 /* si4713_g_modulator - get modulator attributes */
1819 static int si4713_g_modulator(struct v4l2_subdev *sd, struct v4l2_modulator *vm)
1821 struct si4713_device *sdev = to_si4713_device(sd);
1829 if (vm->index > 0) {
1834 strncpy(vm->name, "FM Modulator", 32);
1835 vm->capability = V4L2_TUNER_CAP_STEREO | V4L2_TUNER_CAP_LOW |
1836 V4L2_TUNER_CAP_RDS | V4L2_TUNER_CAP_RDS_CONTROLS;
1838 /* Report current frequency range limits */
1839 vm->rangelow = si4713_to_v4l2(FREQ_RANGE_LOW);
1840 vm->rangehigh = si4713_to_v4l2(FREQ_RANGE_HIGH);
1842 mutex_lock(&sdev->mutex);
1844 if (sdev->power_state) {
1847 rval = si4713_read_property(sdev, SI4713_TX_COMPONENT_ENABLE,
1852 sdev->stereo = get_status_bit(comp_en, 1, 1 << 1);
1853 sdev->rds_info.enabled = get_status_bit(comp_en, 2, 1 << 2);
1856 /* Report current audio mode: mono or stereo */
1858 vm->txsubchans = V4L2_TUNER_SUB_STEREO;
1860 vm->txsubchans = V4L2_TUNER_SUB_MONO;
1862 /* Report rds feature status */
1863 if (sdev->rds_info.enabled)
1864 vm->txsubchans |= V4L2_TUNER_SUB_RDS;
1866 vm->txsubchans &= ~V4L2_TUNER_SUB_RDS;
1869 mutex_unlock(&sdev->mutex);
1874 /* si4713_s_modulator - set modulator attributes */
1875 static int si4713_s_modulator(struct v4l2_subdev *sd, struct v4l2_modulator *vm)
1877 struct si4713_device *sdev = to_si4713_device(sd);
1888 /* Set audio mode: mono or stereo */
1889 if (vm->txsubchans & V4L2_TUNER_SUB_STEREO)
1891 else if (vm->txsubchans & V4L2_TUNER_SUB_MONO)
1896 rds = !!(vm->txsubchans & V4L2_TUNER_SUB_RDS);
1898 mutex_lock(&sdev->mutex);
1900 if (sdev->power_state) {
1901 rval = si4713_read_property(sdev,
1902 SI4713_TX_COMPONENT_ENABLE, &p);
1906 p = set_bits(p, stereo, 1, 1 << 1);
1907 p = set_bits(p, rds, 2, 1 << 2);
1909 rval = si4713_write_property(sdev,
1910 SI4713_TX_COMPONENT_ENABLE, p);
1915 sdev->stereo = stereo;
1916 sdev->rds_info.enabled = rds;
1919 mutex_unlock(&sdev->mutex);
1923 /* si4713_g_frequency - get tuner or modulator radio frequency */
1924 static int si4713_g_frequency(struct v4l2_subdev *sd, struct v4l2_frequency *f)
1926 struct si4713_device *sdev = to_si4713_device(sd);
1929 f->type = V4L2_TUNER_RADIO;
1931 mutex_lock(&sdev->mutex);
1933 if (sdev->power_state) {
1937 rval = si4713_tx_tune_status(sdev, 0x00, &freq, &p, &a, &n);
1941 sdev->frequency = freq;
1944 f->frequency = si4713_to_v4l2(sdev->frequency);
1947 mutex_unlock(&sdev->mutex);
1951 /* si4713_s_frequency - set tuner or modulator radio frequency */
1952 static int si4713_s_frequency(struct v4l2_subdev *sd, struct v4l2_frequency *f)
1954 struct si4713_device *sdev = to_si4713_device(sd);
1956 u16 frequency = v4l2_to_si4713(f->frequency);
1958 /* Check frequency range */
1959 if (frequency < FREQ_RANGE_LOW || frequency > FREQ_RANGE_HIGH)
1962 mutex_lock(&sdev->mutex);
1964 if (sdev->power_state) {
1965 rval = si4713_tx_tune_freq(sdev, frequency);
1971 sdev->frequency = frequency;
1972 f->frequency = si4713_to_v4l2(frequency);
1975 mutex_unlock(&sdev->mutex);
1979 static const struct v4l2_subdev_tuner_ops si4713_subdev_tuner_ops = {
1980 .g_frequency = si4713_g_frequency,
1981 .s_frequency = si4713_s_frequency,
1982 .g_modulator = si4713_g_modulator,
1983 .s_modulator = si4713_s_modulator,
1986 static const struct v4l2_subdev_ops si4713_subdev_ops = {
1987 .core = &si4713_subdev_core_ops,
1988 .tuner = &si4713_subdev_tuner_ops,
1992 * I2C driver interface
1994 /* si4713_probe - probe for the device */
1995 static int si4713_probe(struct i2c_client *client,
1996 const struct i2c_device_id *id)
1998 struct si4713_device *sdev;
1999 struct si4713_platform_data *pdata = client->dev.platform_data;
2002 sdev = kzalloc(sizeof *sdev, GFP_KERNEL);
2004 dev_err(&client->dev, "Failed to alloc video device.\n");
2009 sdev->gpio_reset = -1;
2010 if (pdata && gpio_is_valid(pdata->gpio_reset)) {
2011 rval = gpio_request(pdata->gpio_reset, "si4713 reset");
2013 dev_err(&client->dev,
2014 "Failed to request gpio: %d\n", rval);
2017 sdev->gpio_reset = pdata->gpio_reset;
2018 gpio_direction_output(sdev->gpio_reset, 0);
2021 for (i = 0; i < ARRAY_SIZE(sdev->supplies); i++)
2022 sdev->supplies[i].supply = si4713_supply_names[i];
2024 rval = regulator_bulk_get(&client->dev, ARRAY_SIZE(sdev->supplies),
2027 dev_err(&client->dev, "Cannot get regulators: %d\n", rval);
2031 v4l2_i2c_subdev_init(&sdev->sd, client, &si4713_subdev_ops);
2033 mutex_init(&sdev->mutex);
2034 init_completion(&sdev->work);
2037 rval = request_irq(client->irq,
2038 si4713_handler, IRQF_TRIGGER_FALLING | IRQF_DISABLED,
2039 client->name, sdev);
2041 v4l2_err(&sdev->sd, "Could not request IRQ\n");
2044 v4l2_dbg(1, debug, &sdev->sd, "IRQ requested.\n");
2046 v4l2_warn(&sdev->sd, "IRQ not configured. Using timeouts.\n");
2049 rval = si4713_initialize(sdev);
2051 v4l2_err(&sdev->sd, "Failed to probe device information.\n");
2059 free_irq(client->irq, sdev);
2061 regulator_bulk_free(ARRAY_SIZE(sdev->supplies), sdev->supplies);
2063 if (gpio_is_valid(sdev->gpio_reset))
2064 gpio_free(sdev->gpio_reset);
2071 /* si4713_remove - remove the device */
2072 static int si4713_remove(struct i2c_client *client)
2074 struct v4l2_subdev *sd = i2c_get_clientdata(client);
2075 struct si4713_device *sdev = to_si4713_device(sd);
2077 if (sdev->power_state)
2078 si4713_set_power_state(sdev, POWER_DOWN);
2080 if (client->irq > 0)
2081 free_irq(client->irq, sdev);
2083 v4l2_device_unregister_subdev(sd);
2084 regulator_bulk_free(ARRAY_SIZE(sdev->supplies), sdev->supplies);
2085 if (gpio_is_valid(sdev->gpio_reset))
2086 gpio_free(sdev->gpio_reset);
2092 /* si4713_i2c_driver - i2c driver interface */
2093 static const struct i2c_device_id si4713_id[] = {
2097 MODULE_DEVICE_TABLE(i2c, si4713_id);
2099 static struct i2c_driver si4713_i2c_driver = {
2103 .probe = si4713_probe,
2104 .remove = si4713_remove,
2105 .id_table = si4713_id,
2108 /* Module Interface */
2109 static int __init si4713_module_init(void)
2111 return i2c_add_driver(&si4713_i2c_driver);
2114 static void __exit si4713_module_exit(void)
2116 i2c_del_driver(&si4713_i2c_driver);
2119 module_init(si4713_module_init);
2120 module_exit(si4713_module_exit);