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 <media/v4l2-device.h>
31 #include <media/v4l2-ioctl.h>
32 #include <media/v4l2-common.h>
34 #include "si4713-i2c.h"
36 /* module parameters */
38 module_param(debug, int, S_IRUGO | S_IWUSR);
39 MODULE_PARM_DESC(debug, "Debug level (0 - 2)");
41 MODULE_LICENSE("GPL");
42 MODULE_AUTHOR("Eduardo Valentin <eduardo.valentin@nokia.com>");
43 MODULE_DESCRIPTION("I2C driver for Si4713 FM Radio Transmitter");
44 MODULE_VERSION("0.0.1");
46 #define DEFAULT_RDS_PI 0x00
47 #define DEFAULT_RDS_PTY 0x00
48 #define DEFAULT_RDS_PS_NAME ""
49 #define DEFAULT_RDS_RADIO_TEXT DEFAULT_RDS_PS_NAME
50 #define DEFAULT_RDS_DEVIATION 0x00C8
51 #define DEFAULT_RDS_PS_REPEAT_COUNT 0x0003
52 #define DEFAULT_LIMITER_RTIME 0x1392
53 #define DEFAULT_LIMITER_DEV 0x102CA
54 #define DEFAULT_PILOT_FREQUENCY 0x4A38
55 #define DEFAULT_PILOT_DEVIATION 0x1A5E
56 #define DEFAULT_ACOMP_ATIME 0x0000
57 #define DEFAULT_ACOMP_RTIME 0xF4240L
58 #define DEFAULT_ACOMP_GAIN 0x0F
59 #define DEFAULT_ACOMP_THRESHOLD (-0x28)
60 #define DEFAULT_MUTE 0x01
61 #define DEFAULT_POWER_LEVEL 88
62 #define DEFAULT_FREQUENCY 8800
63 #define DEFAULT_PREEMPHASIS FMPE_EU
64 #define DEFAULT_TUNE_RNL 0xFF
66 #define to_si4713_device(sd) container_of(sd, struct si4713_device, sd)
68 /* frequency domain transformation (using times 10 to avoid floats) */
69 #define FREQDEV_UNIT 100000
70 #define FREQV4L2_MULTI 625
71 #define si4713_to_v4l2(f) ((f * FREQDEV_UNIT) / FREQV4L2_MULTI)
72 #define v4l2_to_si4713(f) ((f * FREQV4L2_MULTI) / FREQDEV_UNIT)
73 #define FREQ_RANGE_LOW 7600
74 #define FREQ_RANGE_HIGH 10800
79 #define RDS_BLOCK_CLEAR 0x03
80 #define RDS_BLOCK_LOAD 0x04
81 #define RDS_RADIOTEXT_2A 0x20
82 #define RDS_RADIOTEXT_BLK_SIZE 4
83 #define RDS_RADIOTEXT_INDEX_MAX 0x0F
84 #define RDS_CARRIAGE_RETURN 0x0D
86 #define rds_ps_nblocks(len) ((len / RDS_BLOCK) + (len % RDS_BLOCK ? 1 : 0))
88 #define get_status_bit(p, b, m) (((p) & (m)) >> (b))
89 #define set_bits(p, v, b, m) (((p) & ~(m)) | ((v) << (b)))
91 #define ATTACK_TIME_UNIT 500
93 #define POWER_OFF 0x00
96 #define msb(x) ((u8)((u16) x >> 8))
97 #define lsb(x) ((u8)((u16) x & 0x00FF))
98 #define compose_u16(msb, lsb) (((u16)msb << 8) | lsb)
99 #define check_command_failed(status) (!(status & SI4713_CTS) || \
100 (status & SI4713_ERR))
101 /* mute definition */
102 #define set_mute(p) ((p & 1) | ((p & 1) << 1));
103 #define get_mute(p) (p & 0x01)
106 #define DBG_BUFFER(device, message, buffer, size) \
109 char str[(size)*5]; \
110 for (i = 0; i < size; i++) \
111 sprintf(str + i * 5, " 0x%02x", buffer[i]); \
112 v4l2_dbg(2, debug, device, "%s:%s\n", message, str); \
115 #define DBG_BUFFER(device, message, buffer, size)
119 * Values for limiter release time (sorted by second column)
123 static long limiter_times[] = {
147 * Values for audio compression release time (sorted by second column)
151 static unsigned long acomp_rtimes[] = {
160 * Values for preemphasis (sorted by second column)
164 static unsigned long preemphasis_values[] = {
165 FMPE_DISABLED, V4L2_PREEMPHASIS_DISABLED,
166 FMPE_EU, V4L2_PREEMPHASIS_50_uS,
167 FMPE_USA, V4L2_PREEMPHASIS_75_uS,
170 static int usecs_to_dev(unsigned long usecs, unsigned long const array[],
176 for (i = 0; i < size / 2; i++)
177 if (array[(i * 2) + 1] >= usecs) {
185 static unsigned long dev_to_usecs(int value, unsigned long const array[],
191 for (i = 0; i < size / 2; i++)
192 if (array[i * 2] == value) {
193 rval = array[(i * 2) + 1];
200 /* si4713_handler: IRQ handler, just complete work */
201 static irqreturn_t si4713_handler(int irq, void *dev)
203 struct si4713_device *sdev = dev;
205 v4l2_dbg(2, debug, &sdev->sd,
206 "%s: sending signal to completion work.\n", __func__);
207 complete(&sdev->work);
213 * si4713_send_command - sends a command to si4713 and waits its response
214 * @sdev: si4713_device structure for the device we are communicating
215 * @command: command id
216 * @args: command arguments we are sending (up to 7)
217 * @argn: actual size of @args
218 * @response: buffer to place the expected response from the device (up to 15)
219 * @respn: actual size of @response
220 * @usecs: amount of time to wait before reading the response (in usecs)
222 static int si4713_send_command(struct si4713_device *sdev, const u8 command,
223 const u8 args[], const int argn,
224 u8 response[], const int respn, const int usecs)
226 struct i2c_client *client = v4l2_get_subdevdata(&sdev->sd);
227 u8 data1[MAX_ARGS + 1];
230 if (!client->adapter)
233 /* First send the command and its arguments */
235 memcpy(data1 + 1, args, argn);
236 DBG_BUFFER(&sdev->sd, "Parameters", data1, argn + 1);
238 err = i2c_master_send(client, data1, argn + 1);
239 if (err != argn + 1) {
240 v4l2_err(&sdev->sd, "Error while sending command 0x%02x\n",
242 return (err > 0) ? -EIO : err;
245 /* Wait response from interrupt */
246 if (!wait_for_completion_timeout(&sdev->work,
247 usecs_to_jiffies(usecs) + 1))
249 "(%s) Device took too much time to answer.\n",
252 /* Then get the response */
253 err = i2c_master_recv(client, response, respn);
256 "Error while reading response for command 0x%02x\n",
258 return (err > 0) ? -EIO : err;
261 DBG_BUFFER(&sdev->sd, "Response", response, respn);
262 if (check_command_failed(response[0]))
269 * si4713_read_property - reads a si4713 property
270 * @sdev: si4713_device structure for the device we are communicating
271 * @prop: property identification number
272 * @pv: property value to be returned on success
274 static int si4713_read_property(struct si4713_device *sdev, u16 prop, u32 *pv)
277 u8 val[SI4713_GET_PROP_NRESP];
280 * .Second byte = property's MSB
281 * .Third byte = property's LSB
283 const u8 args[SI4713_GET_PROP_NARGS] = {
289 err = si4713_send_command(sdev, SI4713_CMD_GET_PROPERTY,
290 args, ARRAY_SIZE(args), val,
291 ARRAY_SIZE(val), DEFAULT_TIMEOUT);
296 *pv = compose_u16(val[2], val[3]);
298 v4l2_dbg(1, debug, &sdev->sd,
299 "%s: property=0x%02x value=0x%02x status=0x%02x\n",
300 __func__, prop, *pv, val[0]);
306 * si4713_write_property - modifies a si4713 property
307 * @sdev: si4713_device structure for the device we are communicating
308 * @prop: property identification number
309 * @val: new value for that property
311 static int si4713_write_property(struct si4713_device *sdev, u16 prop, u16 val)
314 u8 resp[SI4713_SET_PROP_NRESP];
317 * .Second byte = property's MSB
318 * .Third byte = property's LSB
319 * .Fourth byte = value's MSB
320 * .Fifth byte = value's LSB
322 const u8 args[SI4713_SET_PROP_NARGS] = {
330 rval = si4713_send_command(sdev, SI4713_CMD_SET_PROPERTY,
331 args, ARRAY_SIZE(args),
332 resp, ARRAY_SIZE(resp),
338 v4l2_dbg(1, debug, &sdev->sd,
339 "%s: property=0x%02x value=0x%02x status=0x%02x\n",
340 __func__, prop, val, resp[0]);
343 * As there is no command response for SET_PROPERTY,
344 * wait Tcomp time to finish before proceed, in order
345 * to have property properly set.
347 msleep(TIMEOUT_SET_PROPERTY);
353 * si4713_powerup - Powers the device up
354 * @sdev: si4713_device structure for the device we are communicating
356 static int si4713_powerup(struct si4713_device *sdev)
359 u8 resp[SI4713_PWUP_NRESP];
361 * .First byte = Enabled interrupts and boot function
362 * .Second byte = Input operation mode
364 const u8 args[SI4713_PWUP_NARGS] = {
365 SI4713_PWUP_CTSIEN | SI4713_PWUP_GPO2OEN | SI4713_PWUP_FUNC_TX,
366 SI4713_PWUP_OPMOD_ANALOG,
369 if (sdev->power_state)
372 sdev->platform_data->set_power(1);
373 err = si4713_send_command(sdev, SI4713_CMD_POWER_UP,
374 args, ARRAY_SIZE(args),
375 resp, ARRAY_SIZE(resp),
379 v4l2_dbg(1, debug, &sdev->sd, "Powerup response: 0x%02x\n",
381 v4l2_dbg(1, debug, &sdev->sd, "Device in power up mode\n");
382 sdev->power_state = POWER_ON;
384 err = si4713_write_property(sdev, SI4713_GPO_IEN,
385 SI4713_STC_INT | SI4713_CTS);
387 sdev->platform_data->set_power(0);
394 * si4713_powerdown - Powers the device down
395 * @sdev: si4713_device structure for the device we are communicating
397 static int si4713_powerdown(struct si4713_device *sdev)
400 u8 resp[SI4713_PWDN_NRESP];
402 if (!sdev->power_state)
405 err = si4713_send_command(sdev, SI4713_CMD_POWER_DOWN,
407 resp, ARRAY_SIZE(resp),
411 v4l2_dbg(1, debug, &sdev->sd, "Power down response: 0x%02x\n",
413 v4l2_dbg(1, debug, &sdev->sd, "Device in reset mode\n");
414 sdev->platform_data->set_power(0);
415 sdev->power_state = POWER_OFF;
422 * si4713_checkrev - Checks if we are treating a device with the correct rev.
423 * @sdev: si4713_device structure for the device we are communicating
425 static int si4713_checkrev(struct si4713_device *sdev)
427 struct i2c_client *client = v4l2_get_subdevdata(&sdev->sd);
429 u8 resp[SI4713_GETREV_NRESP];
431 mutex_lock(&sdev->mutex);
433 rval = si4713_send_command(sdev, SI4713_CMD_GET_REV,
435 resp, ARRAY_SIZE(resp),
441 if (resp[1] == SI4713_PRODUCT_NUMBER) {
442 v4l2_info(&sdev->sd, "chip found @ 0x%02x (%s)\n",
443 client->addr << 1, client->adapter->name);
445 v4l2_err(&sdev->sd, "Invalid product number\n");
450 mutex_unlock(&sdev->mutex);
455 * si4713_wait_stc - Waits STC interrupt and clears status bits. Usefull
456 * for TX_TUNE_POWER, TX_TUNE_FREQ and TX_TUNE_MEAS
457 * @sdev: si4713_device structure for the device we are communicating
458 * @usecs: timeout to wait for STC interrupt signal
460 static int si4713_wait_stc(struct si4713_device *sdev, const int usecs)
463 u8 resp[SI4713_GET_STATUS_NRESP];
465 /* Wait response from STC interrupt */
466 if (!wait_for_completion_timeout(&sdev->work,
467 usecs_to_jiffies(usecs) + 1))
469 "%s: device took too much time to answer (%d usec).\n",
472 /* Clear status bits */
473 err = si4713_send_command(sdev, SI4713_CMD_GET_INT_STATUS,
475 resp, ARRAY_SIZE(resp),
481 v4l2_dbg(1, debug, &sdev->sd,
482 "%s: status bits: 0x%02x\n", __func__, resp[0]);
484 if (!(resp[0] & SI4713_STC_INT))
492 * si4713_tx_tune_freq - Sets the state of the RF carrier and sets the tuning
493 * frequency between 76 and 108 MHz in 10 kHz units and
495 * @sdev: si4713_device structure for the device we are communicating
496 * @frequency: desired frequency (76 - 108 MHz, unit 10 KHz, step 50 kHz)
498 static int si4713_tx_tune_freq(struct si4713_device *sdev, u16 frequency)
501 u8 val[SI4713_TXFREQ_NRESP];
504 * .Second byte = frequency's MSB
505 * .Third byte = frequency's LSB
507 const u8 args[SI4713_TXFREQ_NARGS] = {
513 err = si4713_send_command(sdev, SI4713_CMD_TX_TUNE_FREQ,
514 args, ARRAY_SIZE(args), val,
515 ARRAY_SIZE(val), DEFAULT_TIMEOUT);
520 v4l2_dbg(1, debug, &sdev->sd,
521 "%s: frequency=0x%02x status=0x%02x\n", __func__,
524 err = si4713_wait_stc(sdev, TIMEOUT_TX_TUNE);
528 return compose_u16(args[1], args[2]);
532 * si4713_tx_tune_power - Sets the RF voltage level between 88 and 115 dBuV in
533 * 1 dB units. A value of 0x00 indicates off. The command
534 * also sets the antenna tuning capacitance. A value of 0
535 * indicates autotuning, and a value of 1 - 191 indicates
536 * a manual override, which results in a tuning
537 * capacitance of 0.25 pF x @antcap.
538 * @sdev: si4713_device structure for the device we are communicating
539 * @power: tuning power (88 - 115 dBuV, unit/step 1 dB)
540 * @antcap: value of antenna tuning capacitor (0 - 191)
542 static int si4713_tx_tune_power(struct si4713_device *sdev, u8 power,
546 u8 val[SI4713_TXPWR_NRESP];
550 * .Third byte = power
551 * .Fourth byte = antcap
553 const u8 args[SI4713_TXPWR_NARGS] = {
560 if (((power > 0) && (power < SI4713_MIN_POWER)) ||
561 power > SI4713_MAX_POWER || antcap > SI4713_MAX_ANTCAP)
564 err = si4713_send_command(sdev, SI4713_CMD_TX_TUNE_POWER,
565 args, ARRAY_SIZE(args), val,
566 ARRAY_SIZE(val), DEFAULT_TIMEOUT);
571 v4l2_dbg(1, debug, &sdev->sd,
572 "%s: power=0x%02x antcap=0x%02x status=0x%02x\n",
573 __func__, power, antcap, val[0]);
575 return si4713_wait_stc(sdev, TIMEOUT_TX_TUNE_POWER);
579 * si4713_tx_tune_measure - Enters receive mode and measures the received noise
580 * level in units of dBuV on the selected frequency.
581 * The Frequency must be between 76 and 108 MHz in 10 kHz
582 * units and steps of 50 kHz. The command also sets the
583 * antenna tuning capacitance. A value of 0 means
584 * autotuning, and a value of 1 to 191 indicates manual
586 * @sdev: si4713_device structure for the device we are communicating
587 * @frequency: desired frequency (76 - 108 MHz, unit 10 KHz, step 50 kHz)
588 * @antcap: value of antenna tuning capacitor (0 - 191)
590 static int si4713_tx_tune_measure(struct si4713_device *sdev, u16 frequency,
594 u8 val[SI4713_TXMEA_NRESP];
597 * .Second byte = frequency's MSB
598 * .Third byte = frequency's LSB
599 * .Fourth byte = antcap
601 const u8 args[SI4713_TXMEA_NARGS] = {
608 sdev->tune_rnl = DEFAULT_TUNE_RNL;
610 if (antcap > SI4713_MAX_ANTCAP)
613 err = si4713_send_command(sdev, SI4713_CMD_TX_TUNE_MEASURE,
614 args, ARRAY_SIZE(args), val,
615 ARRAY_SIZE(val), DEFAULT_TIMEOUT);
620 v4l2_dbg(1, debug, &sdev->sd,
621 "%s: frequency=0x%02x antcap=0x%02x status=0x%02x\n",
622 __func__, frequency, antcap, val[0]);
624 return si4713_wait_stc(sdev, TIMEOUT_TX_TUNE);
628 * si4713_tx_tune_status- Returns the status of the tx_tune_freq, tx_tune_mea or
629 * tx_tune_power commands. This command return the current
630 * frequency, output voltage in dBuV, the antenna tunning
631 * capacitance value and the received noise level. The
632 * command also clears the stcint interrupt bit when the
633 * first bit of its arguments is high.
634 * @sdev: si4713_device structure for the device we are communicating
635 * @intack: 0x01 to clear the seek/tune complete interrupt status indicator.
636 * @frequency: returned frequency
637 * @power: returned power
638 * @antcap: returned antenna capacitance
639 * @noise: returned noise level
641 static int si4713_tx_tune_status(struct si4713_device *sdev, u8 intack,
642 u16 *frequency, u8 *power,
643 u8 *antcap, u8 *noise)
646 u8 val[SI4713_TXSTATUS_NRESP];
648 * .First byte = intack bit
650 const u8 args[SI4713_TXSTATUS_NARGS] = {
651 intack & SI4713_INTACK_MASK,
654 err = si4713_send_command(sdev, SI4713_CMD_TX_TUNE_STATUS,
655 args, ARRAY_SIZE(args), val,
656 ARRAY_SIZE(val), DEFAULT_TIMEOUT);
659 v4l2_dbg(1, debug, &sdev->sd,
660 "%s: status=0x%02x\n", __func__, val[0]);
661 *frequency = compose_u16(val[2], val[3]);
662 sdev->frequency = *frequency;
666 v4l2_dbg(1, debug, &sdev->sd, "%s: response: %d x 10 kHz "
667 "(power %d, antcap %d, rnl %d)\n", __func__,
668 *frequency, *power, *antcap, *noise);
675 * si4713_tx_rds_buff - Loads the RDS group buffer FIFO or circular buffer.
676 * @sdev: si4713_device structure for the device we are communicating
677 * @mode: the buffer operation mode.
681 * @cbleft: returns the number of available circular buffer blocks minus the
682 * number of used circular buffer blocks.
684 static int si4713_tx_rds_buff(struct si4713_device *sdev, u8 mode, u16 rdsb,
685 u16 rdsc, u16 rdsd, s8 *cbleft)
688 u8 val[SI4713_RDSBUFF_NRESP];
690 const u8 args[SI4713_RDSBUFF_NARGS] = {
691 mode & SI4713_RDSBUFF_MODE_MASK,
700 err = si4713_send_command(sdev, SI4713_CMD_TX_RDS_BUFF,
701 args, ARRAY_SIZE(args), val,
702 ARRAY_SIZE(val), DEFAULT_TIMEOUT);
705 v4l2_dbg(1, debug, &sdev->sd,
706 "%s: status=0x%02x\n", __func__, val[0]);
707 *cbleft = (s8)val[2] - val[3];
708 v4l2_dbg(1, debug, &sdev->sd, "%s: response: interrupts"
709 " 0x%02x cb avail: %d cb used %d fifo avail"
710 " %d fifo used %d\n", __func__, val[1],
711 val[2], val[3], val[4], val[5]);
718 * si4713_tx_rds_ps - Loads the program service buffer.
719 * @sdev: si4713_device structure for the device we are communicating
720 * @psid: program service id to be loaded.
721 * @pschar: assumed 4 size char array to be loaded into the program service
723 static int si4713_tx_rds_ps(struct si4713_device *sdev, u8 psid,
724 unsigned char *pschar)
727 u8 val[SI4713_RDSPS_NRESP];
729 const u8 args[SI4713_RDSPS_NARGS] = {
730 psid & SI4713_RDSPS_PSID_MASK,
737 err = si4713_send_command(sdev, SI4713_CMD_TX_RDS_PS,
738 args, ARRAY_SIZE(args), val,
739 ARRAY_SIZE(val), DEFAULT_TIMEOUT);
744 v4l2_dbg(1, debug, &sdev->sd, "%s: status=0x%02x\n", __func__, val[0]);
749 static int si4713_set_power_state(struct si4713_device *sdev, u8 value)
753 mutex_lock(&sdev->mutex);
756 rval = si4713_powerup(sdev);
758 rval = si4713_powerdown(sdev);
760 mutex_unlock(&sdev->mutex);
764 static int si4713_set_mute(struct si4713_device *sdev, u16 mute)
768 mute = set_mute(mute);
770 mutex_lock(&sdev->mutex);
772 if (sdev->power_state)
773 rval = si4713_write_property(sdev,
774 SI4713_TX_LINE_INPUT_MUTE, mute);
777 sdev->mute = get_mute(mute);
779 mutex_unlock(&sdev->mutex);
784 static int si4713_set_rds_ps_name(struct si4713_device *sdev, char *ps_name)
789 /* We want to clear the whole thing */
790 if (!strlen(ps_name))
791 memset(ps_name, 0, MAX_RDS_PS_NAME + 1);
793 mutex_lock(&sdev->mutex);
795 if (sdev->power_state) {
796 /* Write the new ps name and clear the padding */
797 for (i = 0; i < MAX_RDS_PS_NAME; i += (RDS_BLOCK / 2)) {
798 rval = si4713_tx_rds_ps(sdev, (i / (RDS_BLOCK / 2)),
804 /* Setup the size to be sent */
806 len = strlen(ps_name) - 1;
810 rval = si4713_write_property(sdev,
811 SI4713_TX_RDS_PS_MESSAGE_COUNT,
812 rds_ps_nblocks(len));
816 rval = si4713_write_property(sdev,
817 SI4713_TX_RDS_PS_REPEAT_COUNT,
818 DEFAULT_RDS_PS_REPEAT_COUNT * 2);
823 strncpy(sdev->rds_info.ps_name, ps_name, MAX_RDS_PS_NAME);
826 mutex_unlock(&sdev->mutex);
830 static int si4713_set_rds_radio_text(struct si4713_device *sdev, char *rt)
834 u8 b_index = 0, cr_inserted = 0;
837 mutex_lock(&sdev->mutex);
839 if (!sdev->power_state)
842 rval = si4713_tx_rds_buff(sdev, RDS_BLOCK_CLEAR, 0, 0, 0, &left);
850 /* RDS spec says that if the last block isn't used,
851 * then apply a carriage return
853 if (t_index < (RDS_RADIOTEXT_INDEX_MAX *
854 RDS_RADIOTEXT_BLK_SIZE)) {
855 for (i = 0; i < RDS_RADIOTEXT_BLK_SIZE; i++) {
856 if (!rt[t_index + i] || rt[t_index + i] ==
857 RDS_CARRIAGE_RETURN) {
858 rt[t_index + i] = RDS_CARRIAGE_RETURN;
865 rval = si4713_tx_rds_buff(sdev, RDS_BLOCK_LOAD,
866 compose_u16(RDS_RADIOTEXT_2A, b_index++),
867 compose_u16(rt[t_index], rt[t_index + 1]),
868 compose_u16(rt[t_index + 2], rt[t_index + 3]),
873 t_index += RDS_RADIOTEXT_BLK_SIZE;
880 strncpy(sdev->rds_info.radio_text, rt, MAX_RDS_RADIO_TEXT);
883 mutex_unlock(&sdev->mutex);
887 static int si4713_choose_econtrol_action(struct si4713_device *sdev, u32 id,
888 u32 **shadow, s32 *bit, s32 *mask, u16 *property, int *mul,
889 unsigned long **table, int *size)
894 /* FM_TX class controls */
895 case V4L2_CID_RDS_TX_PI:
896 *property = SI4713_TX_RDS_PI;
898 *shadow = &sdev->rds_info.pi;
900 case V4L2_CID_AUDIO_COMPRESSION_THRESHOLD:
901 *property = SI4713_TX_ACOMP_THRESHOLD;
903 *shadow = &sdev->acomp_info.threshold;
905 case V4L2_CID_AUDIO_COMPRESSION_GAIN:
906 *property = SI4713_TX_ACOMP_GAIN;
908 *shadow = &sdev->acomp_info.gain;
910 case V4L2_CID_PILOT_TONE_FREQUENCY:
911 *property = SI4713_TX_PILOT_FREQUENCY;
913 *shadow = &sdev->pilot_info.frequency;
915 case V4L2_CID_AUDIO_COMPRESSION_ATTACK_TIME:
916 *property = SI4713_TX_ACOMP_ATTACK_TIME;
917 *mul = ATTACK_TIME_UNIT;
918 *shadow = &sdev->acomp_info.attack_time;
920 case V4L2_CID_PILOT_TONE_DEVIATION:
921 *property = SI4713_TX_PILOT_DEVIATION;
923 *shadow = &sdev->pilot_info.deviation;
925 case V4L2_CID_AUDIO_LIMITER_DEVIATION:
926 *property = SI4713_TX_AUDIO_DEVIATION;
928 *shadow = &sdev->limiter_info.deviation;
930 case V4L2_CID_RDS_TX_DEVIATION:
931 *property = SI4713_TX_RDS_DEVIATION;
933 *shadow = &sdev->rds_info.deviation;
936 case V4L2_CID_RDS_TX_PTY:
937 *property = SI4713_TX_RDS_PS_MISC;
940 *shadow = &sdev->rds_info.pty;
942 case V4L2_CID_AUDIO_LIMITER_ENABLED:
943 *property = SI4713_TX_ACOMP_ENABLE;
946 *shadow = &sdev->limiter_info.enabled;
948 case V4L2_CID_AUDIO_COMPRESSION_ENABLED:
949 *property = SI4713_TX_ACOMP_ENABLE;
952 *shadow = &sdev->acomp_info.enabled;
954 case V4L2_CID_PILOT_TONE_ENABLED:
955 *property = SI4713_TX_COMPONENT_ENABLE;
958 *shadow = &sdev->pilot_info.enabled;
961 case V4L2_CID_AUDIO_LIMITER_RELEASE_TIME:
962 *property = SI4713_TX_LIMITER_RELEASE_TIME;
963 *table = limiter_times;
964 *size = ARRAY_SIZE(limiter_times);
965 *shadow = &sdev->limiter_info.release_time;
967 case V4L2_CID_AUDIO_COMPRESSION_RELEASE_TIME:
968 *property = SI4713_TX_ACOMP_RELEASE_TIME;
969 *table = acomp_rtimes;
970 *size = ARRAY_SIZE(acomp_rtimes);
971 *shadow = &sdev->acomp_info.release_time;
973 case V4L2_CID_TUNE_PREEMPHASIS:
974 *property = SI4713_TX_PREEMPHASIS;
975 *table = preemphasis_values;
976 *size = ARRAY_SIZE(preemphasis_values);
977 *shadow = &sdev->preemphasis;
987 static int si4713_queryctrl(struct v4l2_subdev *sd, struct v4l2_queryctrl *qc);
989 /* write string property */
990 static int si4713_write_econtrol_string(struct si4713_device *sdev,
991 struct v4l2_ext_control *control)
993 struct v4l2_queryctrl vqc;
997 vqc.id = control->id;
998 rval = si4713_queryctrl(&sdev->sd, &vqc);
1002 switch (control->id) {
1003 case V4L2_CID_RDS_TX_PS_NAME: {
1004 char ps_name[MAX_RDS_PS_NAME + 1];
1006 len = control->size - 1;
1007 if (len > MAX_RDS_PS_NAME) {
1011 rval = copy_from_user(ps_name, control->string, len);
1016 ps_name[len] = '\0';
1018 if (strlen(ps_name) % vqc.step) {
1023 rval = si4713_set_rds_ps_name(sdev, ps_name);
1027 case V4L2_CID_RDS_TX_RADIO_TEXT: {
1028 char radio_text[MAX_RDS_RADIO_TEXT + 1];
1030 len = control->size - 1;
1031 if (len > MAX_RDS_RADIO_TEXT) {
1035 rval = copy_from_user(radio_text, control->string, len);
1040 radio_text[len] = '\0';
1042 if (strlen(radio_text) % vqc.step) {
1047 rval = si4713_set_rds_radio_text(sdev, radio_text);
1060 static int validate_range(struct v4l2_subdev *sd,
1061 struct v4l2_ext_control *control)
1063 struct v4l2_queryctrl vqc;
1066 vqc.id = control->id;
1067 rval = si4713_queryctrl(sd, &vqc);
1071 if (control->value < vqc.minimum || control->value > vqc.maximum)
1078 /* properties which use tx_tune_power*/
1079 static int si4713_write_econtrol_tune(struct si4713_device *sdev,
1080 struct v4l2_ext_control *control)
1085 rval = validate_range(&sdev->sd, control);
1089 mutex_lock(&sdev->mutex);
1091 switch (control->id) {
1092 case V4L2_CID_TUNE_POWER_LEVEL:
1093 power = control->value;
1094 antcap = sdev->antenna_capacitor;
1096 case V4L2_CID_TUNE_ANTENNA_CAPACITOR:
1097 power = sdev->power_level;
1098 antcap = control->value;
1105 if (sdev->power_state)
1106 rval = si4713_tx_tune_power(sdev, power, antcap);
1109 sdev->power_level = power;
1110 sdev->antenna_capacitor = antcap;
1114 mutex_unlock(&sdev->mutex);
1119 static int si4713_write_econtrol_integers(struct si4713_device *sdev,
1120 struct v4l2_ext_control *control)
1123 u32 *shadow = NULL, val = 0;
1124 s32 bit = 0, mask = 0;
1127 unsigned long *table = NULL;
1130 rval = validate_range(&sdev->sd, control);
1134 rval = si4713_choose_econtrol_action(sdev, control->id, &shadow, &bit,
1135 &mask, &property, &mul, &table, &size);
1139 val = control->value;
1141 val = control->value / mul;
1143 rval = usecs_to_dev(control->value, table, size);
1150 mutex_lock(&sdev->mutex);
1152 if (sdev->power_state) {
1154 rval = si4713_read_property(sdev, property, &val);
1157 val = set_bits(val, control->value, bit, mask);
1160 rval = si4713_write_property(sdev, property, val);
1164 val = control->value;
1168 *shadow = val * mul;
1170 rval = dev_to_usecs(val, table, size);
1180 mutex_unlock(&sdev->mutex);
1185 static int si4713_s_frequency(struct v4l2_subdev *sd, struct v4l2_frequency *f);
1186 static int si4713_s_modulator(struct v4l2_subdev *sd, struct v4l2_modulator *);
1188 * si4713_setup - Sets the device up with current configuration.
1189 * @sdev: si4713_device structure for the device we are communicating
1191 static int si4713_setup(struct si4713_device *sdev)
1193 struct v4l2_ext_control ctrl;
1194 struct v4l2_frequency f;
1195 struct v4l2_modulator vm;
1196 struct si4713_device *tmp;
1199 tmp = kmalloc(sizeof(*tmp), GFP_KERNEL);
1203 /* Get a local copy to avoid race */
1204 mutex_lock(&sdev->mutex);
1205 memcpy(tmp, sdev, sizeof(*sdev));
1206 mutex_unlock(&sdev->mutex);
1208 ctrl.id = V4L2_CID_RDS_TX_PI;
1209 ctrl.value = tmp->rds_info.pi;
1210 rval |= si4713_write_econtrol_integers(sdev, &ctrl);
1212 ctrl.id = V4L2_CID_AUDIO_COMPRESSION_THRESHOLD;
1213 ctrl.value = tmp->acomp_info.threshold;
1214 rval |= si4713_write_econtrol_integers(sdev, &ctrl);
1216 ctrl.id = V4L2_CID_AUDIO_COMPRESSION_GAIN;
1217 ctrl.value = tmp->acomp_info.gain;
1218 rval |= si4713_write_econtrol_integers(sdev, &ctrl);
1220 ctrl.id = V4L2_CID_PILOT_TONE_FREQUENCY;
1221 ctrl.value = tmp->pilot_info.frequency;
1222 rval |= si4713_write_econtrol_integers(sdev, &ctrl);
1224 ctrl.id = V4L2_CID_AUDIO_COMPRESSION_ATTACK_TIME;
1225 ctrl.value = tmp->acomp_info.attack_time;
1226 rval |= si4713_write_econtrol_integers(sdev, &ctrl);
1228 ctrl.id = V4L2_CID_PILOT_TONE_DEVIATION;
1229 ctrl.value = tmp->pilot_info.deviation;
1230 rval |= si4713_write_econtrol_integers(sdev, &ctrl);
1232 ctrl.id = V4L2_CID_AUDIO_LIMITER_DEVIATION;
1233 ctrl.value = tmp->limiter_info.deviation;
1234 rval |= si4713_write_econtrol_integers(sdev, &ctrl);
1236 ctrl.id = V4L2_CID_RDS_TX_DEVIATION;
1237 ctrl.value = tmp->rds_info.deviation;
1238 rval |= si4713_write_econtrol_integers(sdev, &ctrl);
1240 ctrl.id = V4L2_CID_RDS_TX_PTY;
1241 ctrl.value = tmp->rds_info.pty;
1242 rval |= si4713_write_econtrol_integers(sdev, &ctrl);
1244 ctrl.id = V4L2_CID_AUDIO_LIMITER_ENABLED;
1245 ctrl.value = tmp->limiter_info.enabled;
1246 rval |= si4713_write_econtrol_integers(sdev, &ctrl);
1248 ctrl.id = V4L2_CID_AUDIO_COMPRESSION_ENABLED;
1249 ctrl.value = tmp->acomp_info.enabled;
1250 rval |= si4713_write_econtrol_integers(sdev, &ctrl);
1252 ctrl.id = V4L2_CID_PILOT_TONE_ENABLED;
1253 ctrl.value = tmp->pilot_info.enabled;
1254 rval |= si4713_write_econtrol_integers(sdev, &ctrl);
1256 ctrl.id = V4L2_CID_AUDIO_LIMITER_RELEASE_TIME;
1257 ctrl.value = tmp->limiter_info.release_time;
1258 rval |= si4713_write_econtrol_integers(sdev, &ctrl);
1260 ctrl.id = V4L2_CID_AUDIO_COMPRESSION_RELEASE_TIME;
1261 ctrl.value = tmp->acomp_info.release_time;
1262 rval |= si4713_write_econtrol_integers(sdev, &ctrl);
1264 ctrl.id = V4L2_CID_TUNE_PREEMPHASIS;
1265 ctrl.value = tmp->preemphasis;
1266 rval |= si4713_write_econtrol_integers(sdev, &ctrl);
1268 ctrl.id = V4L2_CID_RDS_TX_PS_NAME;
1269 rval |= si4713_set_rds_ps_name(sdev, tmp->rds_info.ps_name);
1271 ctrl.id = V4L2_CID_RDS_TX_RADIO_TEXT;
1272 rval |= si4713_set_rds_radio_text(sdev, tmp->rds_info.radio_text);
1274 /* Device procedure needs to set frequency first */
1275 f.frequency = tmp->frequency ? tmp->frequency : DEFAULT_FREQUENCY;
1276 f.frequency = si4713_to_v4l2(f.frequency);
1277 rval |= si4713_s_frequency(&sdev->sd, &f);
1279 ctrl.id = V4L2_CID_TUNE_POWER_LEVEL;
1280 ctrl.value = tmp->power_level;
1281 rval |= si4713_write_econtrol_tune(sdev, &ctrl);
1283 ctrl.id = V4L2_CID_TUNE_ANTENNA_CAPACITOR;
1284 ctrl.value = tmp->antenna_capacitor;
1285 rval |= si4713_write_econtrol_tune(sdev, &ctrl);
1289 vm.txsubchans = V4L2_TUNER_SUB_STEREO;
1291 vm.txsubchans = V4L2_TUNER_SUB_MONO;
1292 if (tmp->rds_info.enabled)
1293 vm.txsubchans |= V4L2_TUNER_SUB_RDS;
1294 si4713_s_modulator(&sdev->sd, &vm);
1302 * si4713_initialize - Sets the device up with default configuration.
1303 * @sdev: si4713_device structure for the device we are communicating
1305 static int si4713_initialize(struct si4713_device *sdev)
1309 rval = si4713_set_power_state(sdev, POWER_ON);
1313 rval = si4713_checkrev(sdev);
1317 rval = si4713_set_power_state(sdev, POWER_OFF);
1321 mutex_lock(&sdev->mutex);
1323 sdev->rds_info.pi = DEFAULT_RDS_PI;
1324 sdev->rds_info.pty = DEFAULT_RDS_PTY;
1325 sdev->rds_info.deviation = DEFAULT_RDS_DEVIATION;
1326 strlcpy(sdev->rds_info.ps_name, DEFAULT_RDS_PS_NAME, MAX_RDS_PS_NAME);
1327 strlcpy(sdev->rds_info.radio_text, DEFAULT_RDS_RADIO_TEXT,
1328 MAX_RDS_RADIO_TEXT);
1329 sdev->rds_info.enabled = 1;
1331 sdev->limiter_info.release_time = DEFAULT_LIMITER_RTIME;
1332 sdev->limiter_info.deviation = DEFAULT_LIMITER_DEV;
1333 sdev->limiter_info.enabled = 1;
1335 sdev->pilot_info.deviation = DEFAULT_PILOT_DEVIATION;
1336 sdev->pilot_info.frequency = DEFAULT_PILOT_FREQUENCY;
1337 sdev->pilot_info.enabled = 1;
1339 sdev->acomp_info.release_time = DEFAULT_ACOMP_RTIME;
1340 sdev->acomp_info.attack_time = DEFAULT_ACOMP_ATIME;
1341 sdev->acomp_info.threshold = DEFAULT_ACOMP_THRESHOLD;
1342 sdev->acomp_info.gain = DEFAULT_ACOMP_GAIN;
1343 sdev->acomp_info.enabled = 1;
1345 sdev->frequency = DEFAULT_FREQUENCY;
1346 sdev->preemphasis = DEFAULT_PREEMPHASIS;
1347 sdev->mute = DEFAULT_MUTE;
1348 sdev->power_level = DEFAULT_POWER_LEVEL;
1349 sdev->antenna_capacitor = 0;
1351 sdev->tune_rnl = DEFAULT_TUNE_RNL;
1353 mutex_unlock(&sdev->mutex);
1359 /* read string property */
1360 static int si4713_read_econtrol_string(struct si4713_device *sdev,
1361 struct v4l2_ext_control *control)
1365 switch (control->id) {
1366 case V4L2_CID_RDS_TX_PS_NAME:
1367 if (strlen(sdev->rds_info.ps_name) + 1 > control->size) {
1368 control->size = MAX_RDS_PS_NAME + 1;
1372 rval = copy_to_user(control->string, sdev->rds_info.ps_name,
1373 strlen(sdev->rds_info.ps_name) + 1);
1378 case V4L2_CID_RDS_TX_RADIO_TEXT:
1379 if (strlen(sdev->rds_info.radio_text) + 1 > control->size) {
1380 control->size = MAX_RDS_RADIO_TEXT + 1;
1384 rval = copy_to_user(control->string, sdev->rds_info.radio_text,
1385 strlen(sdev->rds_info.radio_text) + 1);
1400 * si4713_update_tune_status - update properties from tx_tune_status
1401 * command. Must be called with sdev->mutex held.
1402 * @sdev: si4713_device structure for the device we are communicating
1404 static int si4713_update_tune_status(struct si4713_device *sdev)
1408 u8 p = 0, a = 0, n = 0;
1410 rval = si4713_tx_tune_status(sdev, 0x00, &f, &p, &a, &n);
1415 sdev->power_level = p;
1416 sdev->antenna_capacitor = a;
1423 /* properties which use tx_tune_status */
1424 static int si4713_read_econtrol_tune(struct si4713_device *sdev,
1425 struct v4l2_ext_control *control)
1429 mutex_lock(&sdev->mutex);
1431 if (sdev->power_state) {
1432 rval = si4713_update_tune_status(sdev);
1437 switch (control->id) {
1438 case V4L2_CID_TUNE_POWER_LEVEL:
1439 control->value = sdev->power_level;
1441 case V4L2_CID_TUNE_ANTENNA_CAPACITOR:
1442 control->value = sdev->antenna_capacitor;
1449 mutex_unlock(&sdev->mutex);
1453 static int si4713_read_econtrol_integers(struct si4713_device *sdev,
1454 struct v4l2_ext_control *control)
1457 u32 *shadow = NULL, val = 0;
1458 s32 bit = 0, mask = 0;
1461 unsigned long *table = NULL;
1464 rval = si4713_choose_econtrol_action(sdev, control->id, &shadow, &bit,
1465 &mask, &property, &mul, &table, &size);
1469 mutex_lock(&sdev->mutex);
1471 if (sdev->power_state) {
1472 rval = si4713_read_property(sdev, property, &val);
1476 /* Keep negative values for threshold */
1477 if (control->id == V4L2_CID_AUDIO_COMPRESSION_THRESHOLD)
1480 *shadow = get_status_bit(val, bit, mask);
1482 *shadow = val * mul;
1484 *shadow = dev_to_usecs(val, table, size);
1487 control->value = *shadow;
1490 mutex_unlock(&sdev->mutex);
1496 * Video4Linux Subdev Interface
1498 /* si4713_s_ext_ctrls - set extended controls value */
1499 static int si4713_s_ext_ctrls(struct v4l2_subdev *sd,
1500 struct v4l2_ext_controls *ctrls)
1502 struct si4713_device *sdev = to_si4713_device(sd);
1505 if (ctrls->ctrl_class != V4L2_CTRL_CLASS_FM_TX)
1508 for (i = 0; i < ctrls->count; i++) {
1511 switch ((ctrls->controls + i)->id) {
1512 case V4L2_CID_RDS_TX_PS_NAME:
1513 case V4L2_CID_RDS_TX_RADIO_TEXT:
1514 err = si4713_write_econtrol_string(sdev,
1515 ctrls->controls + i);
1517 case V4L2_CID_TUNE_ANTENNA_CAPACITOR:
1518 case V4L2_CID_TUNE_POWER_LEVEL:
1519 err = si4713_write_econtrol_tune(sdev,
1520 ctrls->controls + i);
1523 err = si4713_write_econtrol_integers(sdev,
1524 ctrls->controls + i);
1528 ctrls->error_idx = i;
1536 /* si4713_g_ext_ctrls - get extended controls value */
1537 static int si4713_g_ext_ctrls(struct v4l2_subdev *sd,
1538 struct v4l2_ext_controls *ctrls)
1540 struct si4713_device *sdev = to_si4713_device(sd);
1543 if (ctrls->ctrl_class != V4L2_CTRL_CLASS_FM_TX)
1546 for (i = 0; i < ctrls->count; i++) {
1549 switch ((ctrls->controls + i)->id) {
1550 case V4L2_CID_RDS_TX_PS_NAME:
1551 case V4L2_CID_RDS_TX_RADIO_TEXT:
1552 err = si4713_read_econtrol_string(sdev,
1553 ctrls->controls + i);
1555 case V4L2_CID_TUNE_ANTENNA_CAPACITOR:
1556 case V4L2_CID_TUNE_POWER_LEVEL:
1557 err = si4713_read_econtrol_tune(sdev,
1558 ctrls->controls + i);
1561 err = si4713_read_econtrol_integers(sdev,
1562 ctrls->controls + i);
1566 ctrls->error_idx = i;
1574 /* si4713_queryctrl - enumerate control items */
1575 static int si4713_queryctrl(struct v4l2_subdev *sd, struct v4l2_queryctrl *qc)
1580 /* User class controls */
1581 case V4L2_CID_AUDIO_MUTE:
1582 rval = v4l2_ctrl_query_fill(qc, 0, 1, 1, DEFAULT_MUTE);
1584 /* FM_TX class controls */
1585 case V4L2_CID_RDS_TX_PI:
1586 rval = v4l2_ctrl_query_fill(qc, 0, 0xFFFF, 1, DEFAULT_RDS_PI);
1588 case V4L2_CID_RDS_TX_PTY:
1589 rval = v4l2_ctrl_query_fill(qc, 0, 31, 1, DEFAULT_RDS_PTY);
1591 case V4L2_CID_RDS_TX_DEVIATION:
1592 rval = v4l2_ctrl_query_fill(qc, 0, MAX_RDS_DEVIATION,
1593 10, DEFAULT_RDS_DEVIATION);
1595 case V4L2_CID_RDS_TX_PS_NAME:
1597 * Report step as 8. From RDS spec, psname
1598 * should be 8. But there are receivers which scroll strings
1601 rval = v4l2_ctrl_query_fill(qc, 0, MAX_RDS_PS_NAME, 8, 0);
1603 case V4L2_CID_RDS_TX_RADIO_TEXT:
1605 * Report step as 32 (2A block). From RDS spec,
1606 * radio text should be 32 for 2A block. But there are receivers
1607 * which scroll strings sized as 32xN. Setting default to 32.
1609 rval = v4l2_ctrl_query_fill(qc, 0, MAX_RDS_RADIO_TEXT, 32, 0);
1612 case V4L2_CID_AUDIO_LIMITER_ENABLED:
1613 rval = v4l2_ctrl_query_fill(qc, 0, 1, 1, 1);
1615 case V4L2_CID_AUDIO_LIMITER_RELEASE_TIME:
1616 rval = v4l2_ctrl_query_fill(qc, 250, MAX_LIMITER_RELEASE_TIME,
1617 50, DEFAULT_LIMITER_RTIME);
1619 case V4L2_CID_AUDIO_LIMITER_DEVIATION:
1620 rval = v4l2_ctrl_query_fill(qc, 0, MAX_LIMITER_DEVIATION,
1621 10, DEFAULT_LIMITER_DEV);
1624 case V4L2_CID_AUDIO_COMPRESSION_ENABLED:
1625 rval = v4l2_ctrl_query_fill(qc, 0, 1, 1, 1);
1627 case V4L2_CID_AUDIO_COMPRESSION_GAIN:
1628 rval = v4l2_ctrl_query_fill(qc, 0, MAX_ACOMP_GAIN, 1,
1629 DEFAULT_ACOMP_GAIN);
1631 case V4L2_CID_AUDIO_COMPRESSION_THRESHOLD:
1632 rval = v4l2_ctrl_query_fill(qc, MIN_ACOMP_THRESHOLD,
1633 MAX_ACOMP_THRESHOLD, 1,
1634 DEFAULT_ACOMP_THRESHOLD);
1636 case V4L2_CID_AUDIO_COMPRESSION_ATTACK_TIME:
1637 rval = v4l2_ctrl_query_fill(qc, 0, MAX_ACOMP_ATTACK_TIME,
1638 500, DEFAULT_ACOMP_ATIME);
1640 case V4L2_CID_AUDIO_COMPRESSION_RELEASE_TIME:
1641 rval = v4l2_ctrl_query_fill(qc, 100000, MAX_ACOMP_RELEASE_TIME,
1642 100000, DEFAULT_ACOMP_RTIME);
1645 case V4L2_CID_PILOT_TONE_ENABLED:
1646 rval = v4l2_ctrl_query_fill(qc, 0, 1, 1, 1);
1648 case V4L2_CID_PILOT_TONE_DEVIATION:
1649 rval = v4l2_ctrl_query_fill(qc, 0, MAX_PILOT_DEVIATION,
1650 10, DEFAULT_PILOT_DEVIATION);
1652 case V4L2_CID_PILOT_TONE_FREQUENCY:
1653 rval = v4l2_ctrl_query_fill(qc, 0, MAX_PILOT_FREQUENCY,
1654 1, DEFAULT_PILOT_FREQUENCY);
1657 case V4L2_CID_TUNE_PREEMPHASIS:
1658 rval = v4l2_ctrl_query_fill(qc, V4L2_PREEMPHASIS_DISABLED,
1659 V4L2_PREEMPHASIS_75_uS, 1,
1660 V4L2_PREEMPHASIS_50_uS);
1662 case V4L2_CID_TUNE_POWER_LEVEL:
1663 rval = v4l2_ctrl_query_fill(qc, 0, 120, 1, DEFAULT_POWER_LEVEL);
1665 case V4L2_CID_TUNE_ANTENNA_CAPACITOR:
1666 rval = v4l2_ctrl_query_fill(qc, 0, 191, 1, 0);
1676 /* si4713_g_ctrl - get the value of a control */
1677 static int si4713_g_ctrl(struct v4l2_subdev *sd, struct v4l2_control *ctrl)
1679 struct si4713_device *sdev = to_si4713_device(sd);
1685 mutex_lock(&sdev->mutex);
1687 if (sdev->power_state) {
1688 rval = si4713_read_property(sdev, SI4713_TX_LINE_INPUT_MUTE,
1696 case V4L2_CID_AUDIO_MUTE:
1697 ctrl->value = get_mute(sdev->mute);
1702 mutex_unlock(&sdev->mutex);
1706 /* si4713_s_ctrl - set the value of a control */
1707 static int si4713_s_ctrl(struct v4l2_subdev *sd, struct v4l2_control *ctrl)
1709 struct si4713_device *sdev = to_si4713_device(sd);
1716 case V4L2_CID_AUDIO_MUTE:
1718 rval = si4713_set_mute(sdev, ctrl->value);
1722 rval = si4713_set_power_state(sdev, POWER_DOWN);
1724 rval = si4713_set_power_state(sdev, POWER_UP);
1728 rval = si4713_setup(sdev);
1732 rval = si4713_set_mute(sdev, ctrl->value);
1741 /* si4713_ioctl - deal with private ioctls (only rnl for now) */
1742 long si4713_ioctl(struct v4l2_subdev *sd, unsigned int cmd, void *arg)
1744 struct si4713_device *sdev = to_si4713_device(sd);
1745 struct si4713_rnl *rnl = arg;
1752 mutex_lock(&sdev->mutex);
1754 case SI4713_IOC_MEASURE_RNL:
1755 frequency = v4l2_to_si4713(rnl->frequency);
1757 if (sdev->power_state) {
1758 /* Set desired measurement frequency */
1759 rval = si4713_tx_tune_measure(sdev, frequency, 0);
1762 /* get results from tune status */
1763 rval = si4713_update_tune_status(sdev);
1767 rnl->rnl = sdev->tune_rnl;
1772 rval = -ENOIOCTLCMD;
1776 mutex_unlock(&sdev->mutex);
1780 static const struct v4l2_subdev_core_ops si4713_subdev_core_ops = {
1781 .queryctrl = si4713_queryctrl,
1782 .g_ext_ctrls = si4713_g_ext_ctrls,
1783 .s_ext_ctrls = si4713_s_ext_ctrls,
1784 .g_ctrl = si4713_g_ctrl,
1785 .s_ctrl = si4713_s_ctrl,
1786 .ioctl = si4713_ioctl,
1789 /* si4713_g_modulator - get modulator attributes */
1790 static int si4713_g_modulator(struct v4l2_subdev *sd, struct v4l2_modulator *vm)
1792 struct si4713_device *sdev = to_si4713_device(sd);
1800 if (vm->index > 0) {
1805 strncpy(vm->name, "FM Modulator", 32);
1806 vm->capability = V4L2_TUNER_CAP_STEREO | V4L2_TUNER_CAP_LOW |
1809 /* Report current frequency range limits */
1810 vm->rangelow = si4713_to_v4l2(FREQ_RANGE_LOW);
1811 vm->rangehigh = si4713_to_v4l2(FREQ_RANGE_HIGH);
1813 mutex_lock(&sdev->mutex);
1815 if (sdev->power_state) {
1818 rval = si4713_read_property(sdev, SI4713_TX_COMPONENT_ENABLE,
1823 sdev->stereo = get_status_bit(comp_en, 1, 1 << 1);
1824 sdev->rds_info.enabled = get_status_bit(comp_en, 2, 1 << 2);
1827 /* Report current audio mode: mono or stereo */
1829 vm->txsubchans = V4L2_TUNER_SUB_STEREO;
1831 vm->txsubchans = V4L2_TUNER_SUB_MONO;
1833 /* Report rds feature status */
1834 if (sdev->rds_info.enabled)
1835 vm->txsubchans |= V4L2_TUNER_SUB_RDS;
1837 vm->txsubchans &= ~V4L2_TUNER_SUB_RDS;
1840 mutex_unlock(&sdev->mutex);
1845 /* si4713_s_modulator - set modulator attributes */
1846 static int si4713_s_modulator(struct v4l2_subdev *sd, struct v4l2_modulator *vm)
1848 struct si4713_device *sdev = to_si4713_device(sd);
1859 /* Set audio mode: mono or stereo */
1860 if (vm->txsubchans & V4L2_TUNER_SUB_STEREO)
1862 else if (vm->txsubchans & V4L2_TUNER_SUB_MONO)
1867 rds = !!(vm->txsubchans & V4L2_TUNER_SUB_RDS);
1869 mutex_lock(&sdev->mutex);
1871 if (sdev->power_state) {
1872 rval = si4713_read_property(sdev,
1873 SI4713_TX_COMPONENT_ENABLE, &p);
1877 p = set_bits(p, stereo, 1, 1 << 1);
1878 p = set_bits(p, rds, 2, 1 << 2);
1880 rval = si4713_write_property(sdev,
1881 SI4713_TX_COMPONENT_ENABLE, p);
1886 sdev->stereo = stereo;
1887 sdev->rds_info.enabled = rds;
1890 mutex_unlock(&sdev->mutex);
1894 /* si4713_g_frequency - get tuner or modulator radio frequency */
1895 static int si4713_g_frequency(struct v4l2_subdev *sd, struct v4l2_frequency *f)
1897 struct si4713_device *sdev = to_si4713_device(sd);
1900 f->type = V4L2_TUNER_RADIO;
1902 mutex_lock(&sdev->mutex);
1904 if (sdev->power_state) {
1908 rval = si4713_tx_tune_status(sdev, 0x00, &freq, &p, &a, &n);
1912 sdev->frequency = freq;
1915 f->frequency = si4713_to_v4l2(sdev->frequency);
1918 mutex_unlock(&sdev->mutex);
1922 /* si4713_s_frequency - set tuner or modulator radio frequency */
1923 static int si4713_s_frequency(struct v4l2_subdev *sd, struct v4l2_frequency *f)
1925 struct si4713_device *sdev = to_si4713_device(sd);
1927 u16 frequency = v4l2_to_si4713(f->frequency);
1929 /* Check frequency range */
1930 if (frequency < FREQ_RANGE_LOW || frequency > FREQ_RANGE_HIGH)
1933 mutex_lock(&sdev->mutex);
1935 if (sdev->power_state) {
1936 rval = si4713_tx_tune_freq(sdev, frequency);
1942 sdev->frequency = frequency;
1943 f->frequency = si4713_to_v4l2(frequency);
1946 mutex_unlock(&sdev->mutex);
1950 static const struct v4l2_subdev_tuner_ops si4713_subdev_tuner_ops = {
1951 .g_frequency = si4713_g_frequency,
1952 .s_frequency = si4713_s_frequency,
1953 .g_modulator = si4713_g_modulator,
1954 .s_modulator = si4713_s_modulator,
1957 static const struct v4l2_subdev_ops si4713_subdev_ops = {
1958 .core = &si4713_subdev_core_ops,
1959 .tuner = &si4713_subdev_tuner_ops,
1963 * I2C driver interface
1965 /* si4713_probe - probe for the device */
1966 static int si4713_probe(struct i2c_client *client,
1967 const struct i2c_device_id *id)
1969 struct si4713_device *sdev;
1972 sdev = kzalloc(sizeof *sdev, GFP_KERNEL);
1974 dev_err(&client->dev, "Failed to alloc video device.\n");
1979 sdev->platform_data = client->dev.platform_data;
1980 if (!sdev->platform_data) {
1981 v4l2_err(&sdev->sd, "No platform data registered.\n");
1986 v4l2_i2c_subdev_init(&sdev->sd, client, &si4713_subdev_ops);
1988 mutex_init(&sdev->mutex);
1989 init_completion(&sdev->work);
1992 rval = request_irq(client->irq,
1993 si4713_handler, IRQF_TRIGGER_FALLING | IRQF_DISABLED,
1994 client->name, sdev);
1996 v4l2_err(&sdev->sd, "Could not request IRQ\n");
1999 v4l2_dbg(1, debug, &sdev->sd, "IRQ requested.\n");
2001 v4l2_warn(&sdev->sd, "IRQ not configured. Using timeouts.\n");
2004 rval = si4713_initialize(sdev);
2006 v4l2_err(&sdev->sd, "Failed to probe device information.\n");
2014 free_irq(client->irq, sdev);
2021 /* si4713_remove - remove the device */
2022 static int si4713_remove(struct i2c_client *client)
2024 struct v4l2_subdev *sd = i2c_get_clientdata(client);
2025 struct si4713_device *sdev = to_si4713_device(sd);
2027 if (sdev->power_state)
2028 si4713_set_power_state(sdev, POWER_DOWN);
2030 if (client->irq > 0)
2031 free_irq(client->irq, sdev);
2033 v4l2_device_unregister_subdev(sd);
2040 /* si4713_i2c_driver - i2c driver interface */
2041 static const struct i2c_device_id si4713_id[] = {
2045 MODULE_DEVICE_TABLE(i2c, si4713_id);
2047 static struct i2c_driver si4713_i2c_driver = {
2051 .probe = si4713_probe,
2052 .remove = si4713_remove,
2053 .id_table = si4713_id,
2056 /* Module Interface */
2057 static int __init si4713_module_init(void)
2059 return i2c_add_driver(&si4713_i2c_driver);
2062 static void __exit si4713_module_exit(void)
2064 i2c_del_driver(&si4713_i2c_driver);
2067 module_init(si4713_module_init);
2068 module_exit(si4713_module_exit);