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 <media/v4l2-device.h>
30 #include <media/v4l2-ioctl.h>
31 #include <media/v4l2-common.h>
33 #include "si4713-i2c.h"
35 /* module parameters */
37 module_param(debug, int, S_IRUGO | S_IWUSR);
38 MODULE_PARM_DESC(debug, "Debug level (0 - 2)");
40 MODULE_LICENSE("GPL");
41 MODULE_AUTHOR("Eduardo Valentin <eduardo.valentin@nokia.com>");
42 MODULE_DESCRIPTION("I2C driver for Si4713 FM Radio Transmitter");
43 MODULE_VERSION("0.0.1");
45 #define DEFAULT_RDS_PI 0x00
46 #define DEFAULT_RDS_PTY 0x00
47 #define DEFAULT_RDS_PS_NAME ""
48 #define DEFAULT_RDS_RADIO_TEXT DEFAULT_RDS_PS_NAME
49 #define DEFAULT_RDS_DEVIATION 0x00C8
50 #define DEFAULT_RDS_PS_REPEAT_COUNT 0x0003
51 #define DEFAULT_LIMITER_RTIME 0x1392
52 #define DEFAULT_LIMITER_DEV 0x102CA
53 #define DEFAULT_PILOT_FREQUENCY 0x4A38
54 #define DEFAULT_PILOT_DEVIATION 0x1A5E
55 #define DEFAULT_ACOMP_ATIME 0x0000
56 #define DEFAULT_ACOMP_RTIME 0xF4240L
57 #define DEFAULT_ACOMP_GAIN 0x0F
58 #define DEFAULT_ACOMP_THRESHOLD (-0x28)
59 #define DEFAULT_MUTE 0x01
60 #define DEFAULT_POWER_LEVEL 88
61 #define DEFAULT_FREQUENCY 8800
62 #define DEFAULT_PREEMPHASIS FMPE_EU
63 #define DEFAULT_TUNE_RNL 0xFF
65 #define to_si4713_device(sd) container_of(sd, struct si4713_device, sd)
67 /* frequency domain transformation (using times 10 to avoid floats) */
68 #define FREQDEV_UNIT 100000
69 #define FREQV4L2_MULTI 625
70 #define si4713_to_v4l2(f) ((f * FREQDEV_UNIT) / FREQV4L2_MULTI)
71 #define v4l2_to_si4713(f) ((f * FREQV4L2_MULTI) / FREQDEV_UNIT)
72 #define FREQ_RANGE_LOW 7600
73 #define FREQ_RANGE_HIGH 10800
78 #define RDS_BLOCK_CLEAR 0x03
79 #define RDS_BLOCK_LOAD 0x04
80 #define RDS_RADIOTEXT_2A 0x20
81 #define RDS_RADIOTEXT_BLK_SIZE 4
82 #define RDS_RADIOTEXT_INDEX_MAX 0x0F
83 #define RDS_CARRIAGE_RETURN 0x0D
85 #define rds_ps_nblocks(len) ((len / RDS_BLOCK) + (len % RDS_BLOCK ? 1 : 0))
87 #define get_status_bit(p, b, m) (((p) & (m)) >> (b))
88 #define set_bits(p, v, b, m) (((p) & ~(m)) | ((v) << (b)))
90 #define ATTACK_TIME_UNIT 500
92 #define POWER_OFF 0x00
95 #define msb(x) ((u8)((u16) x >> 8))
96 #define lsb(x) ((u8)((u16) x & 0x00FF))
97 #define compose_u16(msb, lsb) (((u16)msb << 8) | lsb)
98 #define check_command_failed(status) (!(status & SI4713_CTS) || \
99 (status & SI4713_ERR))
100 /* mute definition */
101 #define set_mute(p) ((p & 1) | ((p & 1) << 1));
102 #define get_mute(p) (p & 0x01)
105 #define DBG_BUFFER(device, message, buffer, size) \
108 char str[(size)*5]; \
109 for (i = 0; i < size; i++) \
110 sprintf(str + i * 5, " 0x%02x", buffer[i]); \
111 v4l2_dbg(2, debug, device, "%s:%s\n", message, str); \
114 #define DBG_BUFFER(device, message, buffer, size)
118 * Values for limiter release time (sorted by second column)
122 static long limiter_times[] = {
146 * Values for audio compression release time (sorted by second column)
150 static unsigned long acomp_rtimes[] = {
159 * Values for preemphasis (sorted by second column)
163 static unsigned long preemphasis_values[] = {
164 FMPE_DISABLED, V4L2_PREEMPHASIS_DISABLED,
165 FMPE_EU, V4L2_PREEMPHASIS_50_uS,
166 FMPE_USA, V4L2_PREEMPHASIS_75_uS,
169 static int usecs_to_dev(unsigned long usecs, unsigned long const array[],
175 for (i = 0; i < size / 2; i++)
176 if (array[(i * 2) + 1] >= usecs) {
184 static unsigned long dev_to_usecs(int value, unsigned long const array[],
190 for (i = 0; i < size / 2; i++)
191 if (array[i * 2] == value) {
192 rval = array[(i * 2) + 1];
199 /* si4713_handler: IRQ handler, just complete work */
200 static irqreturn_t si4713_handler(int irq, void *dev)
202 struct si4713_device *sdev = dev;
204 v4l2_dbg(2, debug, &sdev->sd,
205 "%s: sending signal to completion work.\n", __func__);
206 complete(&sdev->work);
212 * si4713_send_command - sends a command to si4713 and waits its response
213 * @sdev: si4713_device structure for the device we are communicating
214 * @command: command id
215 * @args: command arguments we are sending (up to 7)
216 * @argn: actual size of @args
217 * @response: buffer to place the expected response from the device (up to 15)
218 * @respn: actual size of @response
219 * @usecs: amount of time to wait before reading the response (in usecs)
221 static int si4713_send_command(struct si4713_device *sdev, const u8 command,
222 const u8 args[], const int argn,
223 u8 response[], const int respn, const int usecs)
225 struct i2c_client *client = v4l2_get_subdevdata(&sdev->sd);
226 u8 data1[MAX_ARGS + 1];
229 if (!client->adapter)
232 /* First send the command and its arguments */
234 memcpy(data1 + 1, args, argn);
235 DBG_BUFFER(&sdev->sd, "Parameters", data1, argn + 1);
237 err = i2c_master_send(client, data1, argn + 1);
238 if (err != argn + 1) {
239 v4l2_err(&sdev->sd, "Error while sending command 0x%02x\n",
241 return (err > 0) ? -EIO : err;
244 /* Wait response from interrupt */
245 if (!wait_for_completion_timeout(&sdev->work,
246 usecs_to_jiffies(usecs) + 1))
248 "(%s) Device took too much time to answer.\n",
251 /* Then get the response */
252 err = i2c_master_recv(client, response, respn);
255 "Error while reading response for command 0x%02x\n",
257 return (err > 0) ? -EIO : err;
260 DBG_BUFFER(&sdev->sd, "Response", response, respn);
261 if (check_command_failed(response[0]))
268 * si4713_read_property - reads a si4713 property
269 * @sdev: si4713_device structure for the device we are communicating
270 * @prop: property identification number
271 * @pv: property value to be returned on success
273 static int si4713_read_property(struct si4713_device *sdev, u16 prop, u32 *pv)
276 u8 val[SI4713_GET_PROP_NRESP];
279 * .Second byte = property's MSB
280 * .Third byte = property's LSB
282 const u8 args[SI4713_GET_PROP_NARGS] = {
288 err = si4713_send_command(sdev, SI4713_CMD_GET_PROPERTY,
289 args, ARRAY_SIZE(args), val,
290 ARRAY_SIZE(val), DEFAULT_TIMEOUT);
295 *pv = compose_u16(val[2], val[3]);
297 v4l2_dbg(1, debug, &sdev->sd,
298 "%s: property=0x%02x value=0x%02x status=0x%02x\n",
299 __func__, prop, *pv, val[0]);
305 * si4713_write_property - modifies a si4713 property
306 * @sdev: si4713_device structure for the device we are communicating
307 * @prop: property identification number
308 * @val: new value for that property
310 static int si4713_write_property(struct si4713_device *sdev, u16 prop, u16 val)
313 u8 resp[SI4713_SET_PROP_NRESP];
316 * .Second byte = property's MSB
317 * .Third byte = property's LSB
318 * .Fourth byte = value's MSB
319 * .Fifth byte = value's LSB
321 const u8 args[SI4713_SET_PROP_NARGS] = {
329 rval = si4713_send_command(sdev, SI4713_CMD_SET_PROPERTY,
330 args, ARRAY_SIZE(args),
331 resp, ARRAY_SIZE(resp),
337 v4l2_dbg(1, debug, &sdev->sd,
338 "%s: property=0x%02x value=0x%02x status=0x%02x\n",
339 __func__, prop, val, resp[0]);
342 * As there is no command response for SET_PROPERTY,
343 * wait Tcomp time to finish before proceed, in order
344 * to have property properly set.
346 msleep(TIMEOUT_SET_PROPERTY);
352 * si4713_powerup - Powers the device up
353 * @sdev: si4713_device structure for the device we are communicating
355 static int si4713_powerup(struct si4713_device *sdev)
358 u8 resp[SI4713_PWUP_NRESP];
360 * .First byte = Enabled interrupts and boot function
361 * .Second byte = Input operation mode
363 const u8 args[SI4713_PWUP_NARGS] = {
364 SI4713_PWUP_CTSIEN | SI4713_PWUP_GPO2OEN | SI4713_PWUP_FUNC_TX,
365 SI4713_PWUP_OPMOD_ANALOG,
368 if (sdev->power_state)
371 sdev->platform_data->set_power(1);
372 err = si4713_send_command(sdev, SI4713_CMD_POWER_UP,
373 args, ARRAY_SIZE(args),
374 resp, ARRAY_SIZE(resp),
378 v4l2_dbg(1, debug, &sdev->sd, "Powerup response: 0x%02x\n",
380 v4l2_dbg(1, debug, &sdev->sd, "Device in power up mode\n");
381 sdev->power_state = POWER_ON;
383 err = si4713_write_property(sdev, SI4713_GPO_IEN,
384 SI4713_STC_INT | SI4713_CTS);
386 sdev->platform_data->set_power(0);
393 * si4713_powerdown - Powers the device down
394 * @sdev: si4713_device structure for the device we are communicating
396 static int si4713_powerdown(struct si4713_device *sdev)
399 u8 resp[SI4713_PWDN_NRESP];
401 if (!sdev->power_state)
404 err = si4713_send_command(sdev, SI4713_CMD_POWER_DOWN,
406 resp, ARRAY_SIZE(resp),
410 v4l2_dbg(1, debug, &sdev->sd, "Power down response: 0x%02x\n",
412 v4l2_dbg(1, debug, &sdev->sd, "Device in reset mode\n");
413 sdev->platform_data->set_power(0);
414 sdev->power_state = POWER_OFF;
421 * si4713_checkrev - Checks if we are treating a device with the correct rev.
422 * @sdev: si4713_device structure for the device we are communicating
424 static int si4713_checkrev(struct si4713_device *sdev)
426 struct i2c_client *client = v4l2_get_subdevdata(&sdev->sd);
428 u8 resp[SI4713_GETREV_NRESP];
430 mutex_lock(&sdev->mutex);
432 rval = si4713_send_command(sdev, SI4713_CMD_GET_REV,
434 resp, ARRAY_SIZE(resp),
440 if (resp[1] == SI4713_PRODUCT_NUMBER) {
441 v4l2_info(&sdev->sd, "chip found @ 0x%02x (%s)\n",
442 client->addr << 1, client->adapter->name);
444 v4l2_err(&sdev->sd, "Invalid product number\n");
449 mutex_unlock(&sdev->mutex);
454 * si4713_wait_stc - Waits STC interrupt and clears status bits. Usefull
455 * for TX_TUNE_POWER, TX_TUNE_FREQ and TX_TUNE_MEAS
456 * @sdev: si4713_device structure for the device we are communicating
457 * @usecs: timeout to wait for STC interrupt signal
459 static int si4713_wait_stc(struct si4713_device *sdev, const int usecs)
462 u8 resp[SI4713_GET_STATUS_NRESP];
464 /* Wait response from STC interrupt */
465 if (!wait_for_completion_timeout(&sdev->work,
466 usecs_to_jiffies(usecs) + 1))
468 "%s: device took too much time to answer (%d usec).\n",
471 /* Clear status bits */
472 err = si4713_send_command(sdev, SI4713_CMD_GET_INT_STATUS,
474 resp, ARRAY_SIZE(resp),
480 v4l2_dbg(1, debug, &sdev->sd,
481 "%s: status bits: 0x%02x\n", __func__, resp[0]);
483 if (!(resp[0] & SI4713_STC_INT))
491 * si4713_tx_tune_freq - Sets the state of the RF carrier and sets the tuning
492 * frequency between 76 and 108 MHz in 10 kHz units and
494 * @sdev: si4713_device structure for the device we are communicating
495 * @frequency: desired frequency (76 - 108 MHz, unit 10 KHz, step 50 kHz)
497 static int si4713_tx_tune_freq(struct si4713_device *sdev, u16 frequency)
500 u8 val[SI4713_TXFREQ_NRESP];
503 * .Second byte = frequency's MSB
504 * .Third byte = frequency's LSB
506 const u8 args[SI4713_TXFREQ_NARGS] = {
512 err = si4713_send_command(sdev, SI4713_CMD_TX_TUNE_FREQ,
513 args, ARRAY_SIZE(args), val,
514 ARRAY_SIZE(val), DEFAULT_TIMEOUT);
519 v4l2_dbg(1, debug, &sdev->sd,
520 "%s: frequency=0x%02x status=0x%02x\n", __func__,
523 err = si4713_wait_stc(sdev, TIMEOUT_TX_TUNE);
527 return compose_u16(args[1], args[2]);
531 * si4713_tx_tune_power - Sets the RF voltage level between 88 and 115 dBuV in
532 * 1 dB units. A value of 0x00 indicates off. The command
533 * also sets the antenna tuning capacitance. A value of 0
534 * indicates autotuning, and a value of 1 - 191 indicates
535 * a manual override, which results in a tuning
536 * capacitance of 0.25 pF x @antcap.
537 * @sdev: si4713_device structure for the device we are communicating
538 * @power: tuning power (88 - 115 dBuV, unit/step 1 dB)
539 * @antcap: value of antenna tuning capacitor (0 - 191)
541 static int si4713_tx_tune_power(struct si4713_device *sdev, u8 power,
545 u8 val[SI4713_TXPWR_NRESP];
549 * .Third byte = power
550 * .Fourth byte = antcap
552 const u8 args[SI4713_TXPWR_NARGS] = {
559 if (((power > 0) && (power < SI4713_MIN_POWER)) ||
560 power > SI4713_MAX_POWER || antcap > SI4713_MAX_ANTCAP)
563 err = si4713_send_command(sdev, SI4713_CMD_TX_TUNE_POWER,
564 args, ARRAY_SIZE(args), val,
565 ARRAY_SIZE(val), DEFAULT_TIMEOUT);
570 v4l2_dbg(1, debug, &sdev->sd,
571 "%s: power=0x%02x antcap=0x%02x status=0x%02x\n",
572 __func__, power, antcap, val[0]);
574 return si4713_wait_stc(sdev, TIMEOUT_TX_TUNE_POWER);
578 * si4713_tx_tune_measure - Enters receive mode and measures the received noise
579 * level in units of dBuV on the selected frequency.
580 * The Frequency must be between 76 and 108 MHz in 10 kHz
581 * units and steps of 50 kHz. The command also sets the
582 * antenna tuning capacitance. A value of 0 means
583 * autotuning, and a value of 1 to 191 indicates manual
585 * @sdev: si4713_device structure for the device we are communicating
586 * @frequency: desired frequency (76 - 108 MHz, unit 10 KHz, step 50 kHz)
587 * @antcap: value of antenna tuning capacitor (0 - 191)
589 static int si4713_tx_tune_measure(struct si4713_device *sdev, u16 frequency,
593 u8 val[SI4713_TXMEA_NRESP];
596 * .Second byte = frequency's MSB
597 * .Third byte = frequency's LSB
598 * .Fourth byte = antcap
600 const u8 args[SI4713_TXMEA_NARGS] = {
607 sdev->tune_rnl = DEFAULT_TUNE_RNL;
609 if (antcap > SI4713_MAX_ANTCAP)
612 err = si4713_send_command(sdev, SI4713_CMD_TX_TUNE_MEASURE,
613 args, ARRAY_SIZE(args), val,
614 ARRAY_SIZE(val), DEFAULT_TIMEOUT);
619 v4l2_dbg(1, debug, &sdev->sd,
620 "%s: frequency=0x%02x antcap=0x%02x status=0x%02x\n",
621 __func__, frequency, antcap, val[0]);
623 return si4713_wait_stc(sdev, TIMEOUT_TX_TUNE);
627 * si4713_tx_tune_status- Returns the status of the tx_tune_freq, tx_tune_mea or
628 * tx_tune_power commands. This command return the current
629 * frequency, output voltage in dBuV, the antenna tunning
630 * capacitance value and the received noise level. The
631 * command also clears the stcint interrupt bit when the
632 * first bit of its arguments is high.
633 * @sdev: si4713_device structure for the device we are communicating
634 * @intack: 0x01 to clear the seek/tune complete interrupt status indicator.
635 * @frequency: returned frequency
636 * @power: returned power
637 * @antcap: returned antenna capacitance
638 * @noise: returned noise level
640 static int si4713_tx_tune_status(struct si4713_device *sdev, u8 intack,
641 u16 *frequency, u8 *power,
642 u8 *antcap, u8 *noise)
645 u8 val[SI4713_TXSTATUS_NRESP];
647 * .First byte = intack bit
649 const u8 args[SI4713_TXSTATUS_NARGS] = {
650 intack & SI4713_INTACK_MASK,
653 err = si4713_send_command(sdev, SI4713_CMD_TX_TUNE_STATUS,
654 args, ARRAY_SIZE(args), val,
655 ARRAY_SIZE(val), DEFAULT_TIMEOUT);
658 v4l2_dbg(1, debug, &sdev->sd,
659 "%s: status=0x%02x\n", __func__, val[0]);
660 *frequency = compose_u16(val[2], val[3]);
661 sdev->frequency = *frequency;
665 v4l2_dbg(1, debug, &sdev->sd, "%s: response: %d x 10 kHz "
666 "(power %d, antcap %d, rnl %d)\n", __func__,
667 *frequency, *power, *antcap, *noise);
674 * si4713_tx_rds_buff - Loads the RDS group buffer FIFO or circular buffer.
675 * @sdev: si4713_device structure for the device we are communicating
676 * @mode: the buffer operation mode.
680 * @cbleft: returns the number of available circular buffer blocks minus the
681 * number of used circular buffer blocks.
683 static int si4713_tx_rds_buff(struct si4713_device *sdev, u8 mode, u16 rdsb,
684 u16 rdsc, u16 rdsd, s8 *cbleft)
687 u8 val[SI4713_RDSBUFF_NRESP];
689 const u8 args[SI4713_RDSBUFF_NARGS] = {
690 mode & SI4713_RDSBUFF_MODE_MASK,
699 err = si4713_send_command(sdev, SI4713_CMD_TX_RDS_BUFF,
700 args, ARRAY_SIZE(args), val,
701 ARRAY_SIZE(val), DEFAULT_TIMEOUT);
704 v4l2_dbg(1, debug, &sdev->sd,
705 "%s: status=0x%02x\n", __func__, val[0]);
706 *cbleft = (s8)val[2] - val[3];
707 v4l2_dbg(1, debug, &sdev->sd, "%s: response: interrupts"
708 " 0x%02x cb avail: %d cb used %d fifo avail"
709 " %d fifo used %d\n", __func__, val[1],
710 val[2], val[3], val[4], val[5]);
717 * si4713_tx_rds_ps - Loads the program service buffer.
718 * @sdev: si4713_device structure for the device we are communicating
719 * @psid: program service id to be loaded.
720 * @pschar: assumed 4 size char array to be loaded into the program service
722 static int si4713_tx_rds_ps(struct si4713_device *sdev, u8 psid,
723 unsigned char *pschar)
726 u8 val[SI4713_RDSPS_NRESP];
728 const u8 args[SI4713_RDSPS_NARGS] = {
729 psid & SI4713_RDSPS_PSID_MASK,
736 err = si4713_send_command(sdev, SI4713_CMD_TX_RDS_PS,
737 args, ARRAY_SIZE(args), val,
738 ARRAY_SIZE(val), DEFAULT_TIMEOUT);
743 v4l2_dbg(1, debug, &sdev->sd, "%s: status=0x%02x\n", __func__, val[0]);
748 static int si4713_set_power_state(struct si4713_device *sdev, u8 value)
752 mutex_lock(&sdev->mutex);
755 rval = si4713_powerup(sdev);
757 rval = si4713_powerdown(sdev);
759 mutex_unlock(&sdev->mutex);
763 static int si4713_set_mute(struct si4713_device *sdev, u16 mute)
767 mute = set_mute(mute);
769 mutex_lock(&sdev->mutex);
771 if (sdev->power_state)
772 rval = si4713_write_property(sdev,
773 SI4713_TX_LINE_INPUT_MUTE, mute);
776 sdev->mute = get_mute(mute);
778 mutex_unlock(&sdev->mutex);
783 static int si4713_set_rds_ps_name(struct si4713_device *sdev, char *ps_name)
788 /* We want to clear the whole thing */
789 if (!strlen(ps_name))
790 memset(ps_name, 0, MAX_RDS_PS_NAME + 1);
792 mutex_lock(&sdev->mutex);
794 if (sdev->power_state) {
795 /* Write the new ps name and clear the padding */
796 for (i = 0; i < MAX_RDS_PS_NAME; i += (RDS_BLOCK / 2)) {
797 rval = si4713_tx_rds_ps(sdev, (i / (RDS_BLOCK / 2)),
803 /* Setup the size to be sent */
805 len = strlen(ps_name) - 1;
809 rval = si4713_write_property(sdev,
810 SI4713_TX_RDS_PS_MESSAGE_COUNT,
811 rds_ps_nblocks(len));
815 rval = si4713_write_property(sdev,
816 SI4713_TX_RDS_PS_REPEAT_COUNT,
817 DEFAULT_RDS_PS_REPEAT_COUNT * 2);
822 strncpy(sdev->rds_info.ps_name, ps_name, MAX_RDS_PS_NAME);
825 mutex_unlock(&sdev->mutex);
829 static int si4713_set_rds_radio_text(struct si4713_device *sdev, char *rt)
833 u8 b_index = 0, cr_inserted = 0;
836 mutex_lock(&sdev->mutex);
838 if (!sdev->power_state)
841 rval = si4713_tx_rds_buff(sdev, RDS_BLOCK_CLEAR, 0, 0, 0, &left);
849 /* RDS spec says that if the last block isn't used,
850 * then apply a carriage return
852 if (t_index < (RDS_RADIOTEXT_INDEX_MAX *
853 RDS_RADIOTEXT_BLK_SIZE)) {
854 for (i = 0; i < RDS_RADIOTEXT_BLK_SIZE; i++) {
855 if (!rt[t_index + i] || rt[t_index + i] ==
856 RDS_CARRIAGE_RETURN) {
857 rt[t_index + i] = RDS_CARRIAGE_RETURN;
864 rval = si4713_tx_rds_buff(sdev, RDS_BLOCK_LOAD,
865 compose_u16(RDS_RADIOTEXT_2A, b_index++),
866 compose_u16(rt[t_index], rt[t_index + 1]),
867 compose_u16(rt[t_index + 2], rt[t_index + 3]),
872 t_index += RDS_RADIOTEXT_BLK_SIZE;
879 strncpy(sdev->rds_info.radio_text, rt, MAX_RDS_RADIO_TEXT);
882 mutex_unlock(&sdev->mutex);
886 static int si4713_choose_econtrol_action(struct si4713_device *sdev, u32 id,
887 u32 **shadow, s32 *bit, s32 *mask, u16 *property, int *mul,
888 unsigned long **table, int *size)
893 /* FM_TX class controls */
894 case V4L2_CID_RDS_TX_PI:
895 *property = SI4713_TX_RDS_PI;
897 *shadow = &sdev->rds_info.pi;
899 case V4L2_CID_AUDIO_COMPRESSION_THRESHOLD:
900 *property = SI4713_TX_ACOMP_THRESHOLD;
902 *shadow = &sdev->acomp_info.threshold;
904 case V4L2_CID_AUDIO_COMPRESSION_GAIN:
905 *property = SI4713_TX_ACOMP_GAIN;
907 *shadow = &sdev->acomp_info.gain;
909 case V4L2_CID_PILOT_TONE_FREQUENCY:
910 *property = SI4713_TX_PILOT_FREQUENCY;
912 *shadow = &sdev->pilot_info.frequency;
914 case V4L2_CID_AUDIO_COMPRESSION_ATTACK_TIME:
915 *property = SI4713_TX_ACOMP_ATTACK_TIME;
916 *mul = ATTACK_TIME_UNIT;
917 *shadow = &sdev->acomp_info.attack_time;
919 case V4L2_CID_PILOT_TONE_DEVIATION:
920 *property = SI4713_TX_PILOT_DEVIATION;
922 *shadow = &sdev->pilot_info.deviation;
924 case V4L2_CID_AUDIO_LIMITER_DEVIATION:
925 *property = SI4713_TX_AUDIO_DEVIATION;
927 *shadow = &sdev->limiter_info.deviation;
929 case V4L2_CID_RDS_TX_DEVIATION:
930 *property = SI4713_TX_RDS_DEVIATION;
932 *shadow = &sdev->rds_info.deviation;
935 case V4L2_CID_RDS_TX_PTY:
936 *property = SI4713_TX_RDS_PS_MISC;
939 *shadow = &sdev->rds_info.pty;
941 case V4L2_CID_AUDIO_LIMITER_ENABLED:
942 *property = SI4713_TX_ACOMP_ENABLE;
945 *shadow = &sdev->limiter_info.enabled;
947 case V4L2_CID_AUDIO_COMPRESSION_ENABLED:
948 *property = SI4713_TX_ACOMP_ENABLE;
951 *shadow = &sdev->acomp_info.enabled;
953 case V4L2_CID_PILOT_TONE_ENABLED:
954 *property = SI4713_TX_COMPONENT_ENABLE;
957 *shadow = &sdev->pilot_info.enabled;
960 case V4L2_CID_AUDIO_LIMITER_RELEASE_TIME:
961 *property = SI4713_TX_LIMITER_RELEASE_TIME;
962 *table = limiter_times;
963 *size = ARRAY_SIZE(limiter_times);
964 *shadow = &sdev->limiter_info.release_time;
966 case V4L2_CID_AUDIO_COMPRESSION_RELEASE_TIME:
967 *property = SI4713_TX_ACOMP_RELEASE_TIME;
968 *table = acomp_rtimes;
969 *size = ARRAY_SIZE(acomp_rtimes);
970 *shadow = &sdev->acomp_info.release_time;
972 case V4L2_CID_TUNE_PREEMPHASIS:
973 *property = SI4713_TX_PREEMPHASIS;
974 *table = preemphasis_values;
975 *size = ARRAY_SIZE(preemphasis_values);
976 *shadow = &sdev->preemphasis;
986 static int si4713_queryctrl(struct v4l2_subdev *sd, struct v4l2_queryctrl *qc);
988 /* write string property */
989 static int si4713_write_econtrol_string(struct si4713_device *sdev,
990 struct v4l2_ext_control *control)
992 struct v4l2_queryctrl vqc;
996 vqc.id = control->id;
997 rval = si4713_queryctrl(&sdev->sd, &vqc);
1001 switch (control->id) {
1002 case V4L2_CID_RDS_TX_PS_NAME: {
1003 char ps_name[MAX_RDS_PS_NAME + 1];
1005 len = control->size - 1;
1006 if (len > MAX_RDS_PS_NAME) {
1010 rval = copy_from_user(ps_name, control->string, len);
1013 ps_name[len] = '\0';
1015 if (strlen(ps_name) % vqc.step) {
1020 rval = si4713_set_rds_ps_name(sdev, ps_name);
1024 case V4L2_CID_RDS_TX_RADIO_TEXT: {
1025 char radio_text[MAX_RDS_RADIO_TEXT + 1];
1027 len = control->size - 1;
1028 if (len > MAX_RDS_RADIO_TEXT) {
1032 rval = copy_from_user(radio_text, control->string, len);
1035 radio_text[len] = '\0';
1037 if (strlen(radio_text) % vqc.step) {
1042 rval = si4713_set_rds_radio_text(sdev, radio_text);
1055 static int validate_range(struct v4l2_subdev *sd,
1056 struct v4l2_ext_control *control)
1058 struct v4l2_queryctrl vqc;
1061 vqc.id = control->id;
1062 rval = si4713_queryctrl(sd, &vqc);
1066 if (control->value < vqc.minimum || control->value > vqc.maximum)
1073 /* properties which use tx_tune_power*/
1074 static int si4713_write_econtrol_tune(struct si4713_device *sdev,
1075 struct v4l2_ext_control *control)
1080 rval = validate_range(&sdev->sd, control);
1084 mutex_lock(&sdev->mutex);
1086 switch (control->id) {
1087 case V4L2_CID_TUNE_POWER_LEVEL:
1088 power = control->value;
1089 antcap = sdev->antenna_capacitor;
1091 case V4L2_CID_TUNE_ANTENNA_CAPACITOR:
1092 power = sdev->power_level;
1093 antcap = control->value;
1100 if (sdev->power_state)
1101 rval = si4713_tx_tune_power(sdev, power, antcap);
1104 sdev->power_level = power;
1105 sdev->antenna_capacitor = antcap;
1109 mutex_unlock(&sdev->mutex);
1114 static int si4713_write_econtrol_integers(struct si4713_device *sdev,
1115 struct v4l2_ext_control *control)
1118 u32 *shadow = NULL, val = 0;
1119 s32 bit = 0, mask = 0;
1122 unsigned long *table = NULL;
1125 rval = validate_range(&sdev->sd, control);
1129 rval = si4713_choose_econtrol_action(sdev, control->id, &shadow, &bit,
1130 &mask, &property, &mul, &table, &size);
1134 val = control->value;
1136 val = control->value / mul;
1138 rval = usecs_to_dev(control->value, table, size);
1145 mutex_lock(&sdev->mutex);
1147 if (sdev->power_state) {
1149 rval = si4713_read_property(sdev, property, &val);
1152 val = set_bits(val, control->value, bit, mask);
1155 rval = si4713_write_property(sdev, property, val);
1159 val = control->value;
1163 *shadow = val * mul;
1165 rval = dev_to_usecs(val, table, size);
1175 mutex_unlock(&sdev->mutex);
1180 static int si4713_s_frequency(struct v4l2_subdev *sd, struct v4l2_frequency *f);
1181 static int si4713_s_modulator(struct v4l2_subdev *sd, struct v4l2_modulator *);
1183 * si4713_setup - Sets the device up with current configuration.
1184 * @sdev: si4713_device structure for the device we are communicating
1186 static int si4713_setup(struct si4713_device *sdev)
1188 struct v4l2_ext_control ctrl;
1189 struct v4l2_frequency f;
1190 struct v4l2_modulator vm;
1191 struct si4713_device *tmp;
1194 tmp = kmalloc(sizeof(*tmp), GFP_KERNEL);
1198 /* Get a local copy to avoid race */
1199 mutex_lock(&sdev->mutex);
1200 memcpy(tmp, sdev, sizeof(*sdev));
1201 mutex_unlock(&sdev->mutex);
1203 ctrl.id = V4L2_CID_RDS_TX_PI;
1204 ctrl.value = tmp->rds_info.pi;
1205 rval |= si4713_write_econtrol_integers(sdev, &ctrl);
1207 ctrl.id = V4L2_CID_AUDIO_COMPRESSION_THRESHOLD;
1208 ctrl.value = tmp->acomp_info.threshold;
1209 rval |= si4713_write_econtrol_integers(sdev, &ctrl);
1211 ctrl.id = V4L2_CID_AUDIO_COMPRESSION_GAIN;
1212 ctrl.value = tmp->acomp_info.gain;
1213 rval |= si4713_write_econtrol_integers(sdev, &ctrl);
1215 ctrl.id = V4L2_CID_PILOT_TONE_FREQUENCY;
1216 ctrl.value = tmp->pilot_info.frequency;
1217 rval |= si4713_write_econtrol_integers(sdev, &ctrl);
1219 ctrl.id = V4L2_CID_AUDIO_COMPRESSION_ATTACK_TIME;
1220 ctrl.value = tmp->acomp_info.attack_time;
1221 rval |= si4713_write_econtrol_integers(sdev, &ctrl);
1223 ctrl.id = V4L2_CID_PILOT_TONE_DEVIATION;
1224 ctrl.value = tmp->pilot_info.deviation;
1225 rval |= si4713_write_econtrol_integers(sdev, &ctrl);
1227 ctrl.id = V4L2_CID_AUDIO_LIMITER_DEVIATION;
1228 ctrl.value = tmp->limiter_info.deviation;
1229 rval |= si4713_write_econtrol_integers(sdev, &ctrl);
1231 ctrl.id = V4L2_CID_RDS_TX_DEVIATION;
1232 ctrl.value = tmp->rds_info.deviation;
1233 rval |= si4713_write_econtrol_integers(sdev, &ctrl);
1235 ctrl.id = V4L2_CID_RDS_TX_PTY;
1236 ctrl.value = tmp->rds_info.pty;
1237 rval |= si4713_write_econtrol_integers(sdev, &ctrl);
1239 ctrl.id = V4L2_CID_AUDIO_LIMITER_ENABLED;
1240 ctrl.value = tmp->limiter_info.enabled;
1241 rval |= si4713_write_econtrol_integers(sdev, &ctrl);
1243 ctrl.id = V4L2_CID_AUDIO_COMPRESSION_ENABLED;
1244 ctrl.value = tmp->acomp_info.enabled;
1245 rval |= si4713_write_econtrol_integers(sdev, &ctrl);
1247 ctrl.id = V4L2_CID_PILOT_TONE_ENABLED;
1248 ctrl.value = tmp->pilot_info.enabled;
1249 rval |= si4713_write_econtrol_integers(sdev, &ctrl);
1251 ctrl.id = V4L2_CID_AUDIO_LIMITER_RELEASE_TIME;
1252 ctrl.value = tmp->limiter_info.release_time;
1253 rval |= si4713_write_econtrol_integers(sdev, &ctrl);
1255 ctrl.id = V4L2_CID_AUDIO_COMPRESSION_RELEASE_TIME;
1256 ctrl.value = tmp->acomp_info.release_time;
1257 rval |= si4713_write_econtrol_integers(sdev, &ctrl);
1259 ctrl.id = V4L2_CID_TUNE_PREEMPHASIS;
1260 ctrl.value = tmp->preemphasis;
1261 rval |= si4713_write_econtrol_integers(sdev, &ctrl);
1263 ctrl.id = V4L2_CID_RDS_TX_PS_NAME;
1264 rval |= si4713_set_rds_ps_name(sdev, tmp->rds_info.ps_name);
1266 ctrl.id = V4L2_CID_RDS_TX_RADIO_TEXT;
1267 rval |= si4713_set_rds_radio_text(sdev, tmp->rds_info.radio_text);
1269 /* Device procedure needs to set frequency first */
1270 f.frequency = tmp->frequency ? tmp->frequency : DEFAULT_FREQUENCY;
1271 f.frequency = si4713_to_v4l2(f.frequency);
1272 rval |= si4713_s_frequency(&sdev->sd, &f);
1274 ctrl.id = V4L2_CID_TUNE_POWER_LEVEL;
1275 ctrl.value = tmp->power_level;
1276 rval |= si4713_write_econtrol_tune(sdev, &ctrl);
1278 ctrl.id = V4L2_CID_TUNE_ANTENNA_CAPACITOR;
1279 ctrl.value = tmp->antenna_capacitor;
1280 rval |= si4713_write_econtrol_tune(sdev, &ctrl);
1284 vm.txsubchans = V4L2_TUNER_SUB_STEREO;
1286 vm.txsubchans = V4L2_TUNER_SUB_MONO;
1287 if (tmp->rds_info.enabled)
1288 vm.txsubchans |= V4L2_TUNER_SUB_RDS;
1289 si4713_s_modulator(&sdev->sd, &vm);
1297 * si4713_initialize - Sets the device up with default configuration.
1298 * @sdev: si4713_device structure for the device we are communicating
1300 static int si4713_initialize(struct si4713_device *sdev)
1304 rval = si4713_set_power_state(sdev, POWER_ON);
1308 rval = si4713_checkrev(sdev);
1312 rval = si4713_set_power_state(sdev, POWER_OFF);
1316 mutex_lock(&sdev->mutex);
1318 sdev->rds_info.pi = DEFAULT_RDS_PI;
1319 sdev->rds_info.pty = DEFAULT_RDS_PTY;
1320 sdev->rds_info.deviation = DEFAULT_RDS_DEVIATION;
1321 strlcpy(sdev->rds_info.ps_name, DEFAULT_RDS_PS_NAME, MAX_RDS_PS_NAME);
1322 strlcpy(sdev->rds_info.radio_text, DEFAULT_RDS_RADIO_TEXT,
1323 MAX_RDS_RADIO_TEXT);
1324 sdev->rds_info.enabled = 1;
1326 sdev->limiter_info.release_time = DEFAULT_LIMITER_RTIME;
1327 sdev->limiter_info.deviation = DEFAULT_LIMITER_DEV;
1328 sdev->limiter_info.enabled = 1;
1330 sdev->pilot_info.deviation = DEFAULT_PILOT_DEVIATION;
1331 sdev->pilot_info.frequency = DEFAULT_PILOT_FREQUENCY;
1332 sdev->pilot_info.enabled = 1;
1334 sdev->acomp_info.release_time = DEFAULT_ACOMP_RTIME;
1335 sdev->acomp_info.attack_time = DEFAULT_ACOMP_ATIME;
1336 sdev->acomp_info.threshold = DEFAULT_ACOMP_THRESHOLD;
1337 sdev->acomp_info.gain = DEFAULT_ACOMP_GAIN;
1338 sdev->acomp_info.enabled = 1;
1340 sdev->frequency = DEFAULT_FREQUENCY;
1341 sdev->preemphasis = DEFAULT_PREEMPHASIS;
1342 sdev->mute = DEFAULT_MUTE;
1343 sdev->power_level = DEFAULT_POWER_LEVEL;
1344 sdev->antenna_capacitor = 0;
1346 sdev->tune_rnl = DEFAULT_TUNE_RNL;
1348 mutex_unlock(&sdev->mutex);
1354 /* read string property */
1355 static int si4713_read_econtrol_string(struct si4713_device *sdev,
1356 struct v4l2_ext_control *control)
1360 switch (control->id) {
1361 case V4L2_CID_RDS_TX_PS_NAME:
1362 if (strlen(sdev->rds_info.ps_name) + 1 > control->size) {
1363 control->size = MAX_RDS_PS_NAME + 1;
1367 rval = copy_to_user(control->string, sdev->rds_info.ps_name,
1368 strlen(sdev->rds_info.ps_name) + 1);
1371 case V4L2_CID_RDS_TX_RADIO_TEXT:
1372 if (strlen(sdev->rds_info.radio_text) + 1 > control->size) {
1373 control->size = MAX_RDS_RADIO_TEXT + 1;
1377 rval = copy_to_user(control->string, sdev->rds_info.radio_text,
1378 strlen(sdev->rds_info.radio_text) + 1);
1391 * si4713_update_tune_status - update properties from tx_tune_status
1392 * command. Must be called with sdev->mutex held.
1393 * @sdev: si4713_device structure for the device we are communicating
1395 static int si4713_update_tune_status(struct si4713_device *sdev)
1399 u8 p = 0, a = 0, n = 0;
1401 rval = si4713_tx_tune_status(sdev, 0x00, &f, &p, &a, &n);
1406 sdev->power_level = p;
1407 sdev->antenna_capacitor = a;
1414 /* properties which use tx_tune_status */
1415 static int si4713_read_econtrol_tune(struct si4713_device *sdev,
1416 struct v4l2_ext_control *control)
1420 mutex_lock(&sdev->mutex);
1422 if (sdev->power_state) {
1423 rval = si4713_update_tune_status(sdev);
1428 switch (control->id) {
1429 case V4L2_CID_TUNE_POWER_LEVEL:
1430 control->value = sdev->power_level;
1432 case V4L2_CID_TUNE_ANTENNA_CAPACITOR:
1433 control->value = sdev->antenna_capacitor;
1440 mutex_unlock(&sdev->mutex);
1444 static int si4713_read_econtrol_integers(struct si4713_device *sdev,
1445 struct v4l2_ext_control *control)
1448 u32 *shadow = NULL, val = 0;
1449 s32 bit = 0, mask = 0;
1452 unsigned long *table = NULL;
1455 rval = si4713_choose_econtrol_action(sdev, control->id, &shadow, &bit,
1456 &mask, &property, &mul, &table, &size);
1460 mutex_lock(&sdev->mutex);
1462 if (sdev->power_state) {
1463 rval = si4713_read_property(sdev, property, &val);
1467 /* Keep negative values for threshold */
1468 if (control->id == V4L2_CID_AUDIO_COMPRESSION_THRESHOLD)
1471 *shadow = get_status_bit(val, bit, mask);
1473 *shadow = val * mul;
1475 *shadow = dev_to_usecs(val, table, size);
1478 control->value = *shadow;
1481 mutex_unlock(&sdev->mutex);
1487 * Video4Linux Subdev Interface
1489 /* si4713_s_ext_ctrls - set extended controls value */
1490 static int si4713_s_ext_ctrls(struct v4l2_subdev *sd,
1491 struct v4l2_ext_controls *ctrls)
1493 struct si4713_device *sdev = to_si4713_device(sd);
1496 if (ctrls->ctrl_class != V4L2_CTRL_CLASS_FM_TX)
1499 for (i = 0; i < ctrls->count; i++) {
1502 switch ((ctrls->controls + i)->id) {
1503 case V4L2_CID_RDS_TX_PS_NAME:
1504 case V4L2_CID_RDS_TX_RADIO_TEXT:
1505 err = si4713_write_econtrol_string(sdev,
1506 ctrls->controls + i);
1508 case V4L2_CID_TUNE_ANTENNA_CAPACITOR:
1509 case V4L2_CID_TUNE_POWER_LEVEL:
1510 err = si4713_write_econtrol_tune(sdev,
1511 ctrls->controls + i);
1514 err = si4713_write_econtrol_integers(sdev,
1515 ctrls->controls + i);
1519 ctrls->error_idx = i;
1527 /* si4713_g_ext_ctrls - get extended controls value */
1528 static int si4713_g_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_read_econtrol_string(sdev,
1544 ctrls->controls + i);
1546 case V4L2_CID_TUNE_ANTENNA_CAPACITOR:
1547 case V4L2_CID_TUNE_POWER_LEVEL:
1548 err = si4713_read_econtrol_tune(sdev,
1549 ctrls->controls + i);
1552 err = si4713_read_econtrol_integers(sdev,
1553 ctrls->controls + i);
1557 ctrls->error_idx = i;
1565 /* si4713_queryctrl - enumerate control items */
1566 static int si4713_queryctrl(struct v4l2_subdev *sd, struct v4l2_queryctrl *qc)
1571 /* User class controls */
1572 case V4L2_CID_AUDIO_MUTE:
1573 rval = v4l2_ctrl_query_fill(qc, 0, 1, 1, DEFAULT_MUTE);
1575 /* FM_TX class controls */
1576 case V4L2_CID_RDS_TX_PI:
1577 rval = v4l2_ctrl_query_fill(qc, 0, 0xFFFF, 1, DEFAULT_RDS_PI);
1579 case V4L2_CID_RDS_TX_PTY:
1580 rval = v4l2_ctrl_query_fill(qc, 0, 31, 1, DEFAULT_RDS_PTY);
1582 case V4L2_CID_RDS_TX_DEVIATION:
1583 rval = v4l2_ctrl_query_fill(qc, 0, MAX_RDS_DEVIATION,
1584 10, DEFAULT_RDS_DEVIATION);
1586 case V4L2_CID_RDS_TX_PS_NAME:
1588 * Report step as 8. From RDS spec, psname
1589 * should be 8. But there are receivers which scroll strings
1592 rval = v4l2_ctrl_query_fill(qc, 0, MAX_RDS_PS_NAME, 8, 0);
1594 case V4L2_CID_RDS_TX_RADIO_TEXT:
1596 * Report step as 32 (2A block). From RDS spec,
1597 * radio text should be 32 for 2A block. But there are receivers
1598 * which scroll strings sized as 32xN. Setting default to 32.
1600 rval = v4l2_ctrl_query_fill(qc, 0, MAX_RDS_RADIO_TEXT, 32, 0);
1603 case V4L2_CID_AUDIO_LIMITER_ENABLED:
1604 rval = v4l2_ctrl_query_fill(qc, 0, 1, 1, 1);
1606 case V4L2_CID_AUDIO_LIMITER_RELEASE_TIME:
1607 rval = v4l2_ctrl_query_fill(qc, 250, MAX_LIMITER_RELEASE_TIME,
1608 50, DEFAULT_LIMITER_RTIME);
1610 case V4L2_CID_AUDIO_LIMITER_DEVIATION:
1611 rval = v4l2_ctrl_query_fill(qc, 0, MAX_LIMITER_DEVIATION,
1612 10, DEFAULT_LIMITER_DEV);
1615 case V4L2_CID_AUDIO_COMPRESSION_ENABLED:
1616 rval = v4l2_ctrl_query_fill(qc, 0, 1, 1, 1);
1618 case V4L2_CID_AUDIO_COMPRESSION_GAIN:
1619 rval = v4l2_ctrl_query_fill(qc, 0, MAX_ACOMP_GAIN, 1,
1620 DEFAULT_ACOMP_GAIN);
1622 case V4L2_CID_AUDIO_COMPRESSION_THRESHOLD:
1623 rval = v4l2_ctrl_query_fill(qc, MIN_ACOMP_THRESHOLD,
1624 MAX_ACOMP_THRESHOLD, 1,
1625 DEFAULT_ACOMP_THRESHOLD);
1627 case V4L2_CID_AUDIO_COMPRESSION_ATTACK_TIME:
1628 rval = v4l2_ctrl_query_fill(qc, 0, MAX_ACOMP_ATTACK_TIME,
1629 500, DEFAULT_ACOMP_ATIME);
1631 case V4L2_CID_AUDIO_COMPRESSION_RELEASE_TIME:
1632 rval = v4l2_ctrl_query_fill(qc, 100000, MAX_ACOMP_RELEASE_TIME,
1633 100000, DEFAULT_ACOMP_RTIME);
1636 case V4L2_CID_PILOT_TONE_ENABLED:
1637 rval = v4l2_ctrl_query_fill(qc, 0, 1, 1, 1);
1639 case V4L2_CID_PILOT_TONE_DEVIATION:
1640 rval = v4l2_ctrl_query_fill(qc, 0, MAX_PILOT_DEVIATION,
1641 10, DEFAULT_PILOT_DEVIATION);
1643 case V4L2_CID_PILOT_TONE_FREQUENCY:
1644 rval = v4l2_ctrl_query_fill(qc, 0, MAX_PILOT_FREQUENCY,
1645 1, DEFAULT_PILOT_FREQUENCY);
1648 case V4L2_CID_TUNE_PREEMPHASIS:
1649 rval = v4l2_ctrl_query_fill(qc, V4L2_PREEMPHASIS_DISABLED,
1650 V4L2_PREEMPHASIS_75_uS, 1,
1651 V4L2_PREEMPHASIS_50_uS);
1653 case V4L2_CID_TUNE_POWER_LEVEL:
1654 rval = v4l2_ctrl_query_fill(qc, 0, 120, 1, DEFAULT_POWER_LEVEL);
1656 case V4L2_CID_TUNE_ANTENNA_CAPACITOR:
1657 rval = v4l2_ctrl_query_fill(qc, 0, 191, 1, 0);
1667 /* si4713_g_ctrl - get the value of a control */
1668 static int si4713_g_ctrl(struct v4l2_subdev *sd, struct v4l2_control *ctrl)
1670 struct si4713_device *sdev = to_si4713_device(sd);
1676 mutex_lock(&sdev->mutex);
1678 if (sdev->power_state) {
1679 rval = si4713_read_property(sdev, SI4713_TX_LINE_INPUT_MUTE,
1687 case V4L2_CID_AUDIO_MUTE:
1688 ctrl->value = get_mute(sdev->mute);
1693 mutex_unlock(&sdev->mutex);
1697 /* si4713_s_ctrl - set the value of a control */
1698 static int si4713_s_ctrl(struct v4l2_subdev *sd, struct v4l2_control *ctrl)
1700 struct si4713_device *sdev = to_si4713_device(sd);
1707 case V4L2_CID_AUDIO_MUTE:
1709 rval = si4713_set_mute(sdev, ctrl->value);
1713 rval = si4713_set_power_state(sdev, POWER_DOWN);
1715 rval = si4713_set_power_state(sdev, POWER_UP);
1719 rval = si4713_setup(sdev);
1723 rval = si4713_set_mute(sdev, ctrl->value);
1732 /* si4713_ioctl - deal with private ioctls (only rnl for now) */
1733 long si4713_ioctl(struct v4l2_subdev *sd, unsigned int cmd, void *arg)
1735 struct si4713_device *sdev = to_si4713_device(sd);
1736 struct si4713_rnl *rnl = arg;
1743 mutex_lock(&sdev->mutex);
1745 case SI4713_IOC_MEASURE_RNL:
1746 frequency = v4l2_to_si4713(rnl->frequency);
1748 if (sdev->power_state) {
1749 /* Set desired measurement frequency */
1750 rval = si4713_tx_tune_measure(sdev, frequency, 0);
1753 /* get results from tune status */
1754 rval = si4713_update_tune_status(sdev);
1758 rnl->rnl = sdev->tune_rnl;
1763 rval = -ENOIOCTLCMD;
1767 mutex_unlock(&sdev->mutex);
1771 static const struct v4l2_subdev_core_ops si4713_subdev_core_ops = {
1772 .queryctrl = si4713_queryctrl,
1773 .g_ext_ctrls = si4713_g_ext_ctrls,
1774 .s_ext_ctrls = si4713_s_ext_ctrls,
1775 .g_ctrl = si4713_g_ctrl,
1776 .s_ctrl = si4713_s_ctrl,
1777 .ioctl = si4713_ioctl,
1780 /* si4713_g_modulator - get modulator attributes */
1781 static int si4713_g_modulator(struct v4l2_subdev *sd, struct v4l2_modulator *vm)
1783 struct si4713_device *sdev = to_si4713_device(sd);
1791 if (vm->index > 0) {
1796 strncpy(vm->name, "FM Modulator", 32);
1797 vm->capability = V4L2_TUNER_CAP_STEREO | V4L2_TUNER_CAP_LOW |
1800 /* Report current frequency range limits */
1801 vm->rangelow = si4713_to_v4l2(FREQ_RANGE_LOW);
1802 vm->rangehigh = si4713_to_v4l2(FREQ_RANGE_HIGH);
1804 mutex_lock(&sdev->mutex);
1806 if (sdev->power_state) {
1809 rval = si4713_read_property(sdev, SI4713_TX_COMPONENT_ENABLE,
1814 sdev->stereo = get_status_bit(comp_en, 1, 1 << 1);
1815 sdev->rds_info.enabled = get_status_bit(comp_en, 2, 1 << 2);
1818 /* Report current audio mode: mono or stereo */
1820 vm->txsubchans = V4L2_TUNER_SUB_STEREO;
1822 vm->txsubchans = V4L2_TUNER_SUB_MONO;
1824 /* Report rds feature status */
1825 if (sdev->rds_info.enabled)
1826 vm->txsubchans |= V4L2_TUNER_SUB_RDS;
1828 vm->txsubchans &= ~V4L2_TUNER_SUB_RDS;
1831 mutex_unlock(&sdev->mutex);
1836 /* si4713_s_modulator - set modulator attributes */
1837 static int si4713_s_modulator(struct v4l2_subdev *sd, struct v4l2_modulator *vm)
1839 struct si4713_device *sdev = to_si4713_device(sd);
1850 /* Set audio mode: mono or stereo */
1851 if (vm->txsubchans & V4L2_TUNER_SUB_STEREO)
1853 else if (vm->txsubchans & V4L2_TUNER_SUB_MONO)
1858 rds = !!(vm->txsubchans & V4L2_TUNER_SUB_RDS);
1860 mutex_lock(&sdev->mutex);
1862 if (sdev->power_state) {
1863 rval = si4713_read_property(sdev,
1864 SI4713_TX_COMPONENT_ENABLE, &p);
1868 p = set_bits(p, stereo, 1, 1 << 1);
1869 p = set_bits(p, rds, 2, 1 << 2);
1871 rval = si4713_write_property(sdev,
1872 SI4713_TX_COMPONENT_ENABLE, p);
1877 sdev->stereo = stereo;
1878 sdev->rds_info.enabled = rds;
1881 mutex_unlock(&sdev->mutex);
1885 /* si4713_g_frequency - get tuner or modulator radio frequency */
1886 static int si4713_g_frequency(struct v4l2_subdev *sd, struct v4l2_frequency *f)
1888 struct si4713_device *sdev = to_si4713_device(sd);
1891 f->type = V4L2_TUNER_RADIO;
1893 mutex_lock(&sdev->mutex);
1895 if (sdev->power_state) {
1899 rval = si4713_tx_tune_status(sdev, 0x00, &freq, &p, &a, &n);
1903 sdev->frequency = freq;
1906 f->frequency = si4713_to_v4l2(sdev->frequency);
1909 mutex_unlock(&sdev->mutex);
1913 /* si4713_s_frequency - set tuner or modulator radio frequency */
1914 static int si4713_s_frequency(struct v4l2_subdev *sd, struct v4l2_frequency *f)
1916 struct si4713_device *sdev = to_si4713_device(sd);
1918 u16 frequency = v4l2_to_si4713(f->frequency);
1920 /* Check frequency range */
1921 if (frequency < FREQ_RANGE_LOW || frequency > FREQ_RANGE_HIGH)
1924 mutex_lock(&sdev->mutex);
1926 if (sdev->power_state) {
1927 rval = si4713_tx_tune_freq(sdev, frequency);
1933 sdev->frequency = frequency;
1934 f->frequency = si4713_to_v4l2(frequency);
1937 mutex_unlock(&sdev->mutex);
1941 static const struct v4l2_subdev_tuner_ops si4713_subdev_tuner_ops = {
1942 .g_frequency = si4713_g_frequency,
1943 .s_frequency = si4713_s_frequency,
1944 .g_modulator = si4713_g_modulator,
1945 .s_modulator = si4713_s_modulator,
1948 static const struct v4l2_subdev_ops si4713_subdev_ops = {
1949 .core = &si4713_subdev_core_ops,
1950 .tuner = &si4713_subdev_tuner_ops,
1954 * I2C driver interface
1956 /* si4713_probe - probe for the device */
1957 static int si4713_probe(struct i2c_client *client,
1958 const struct i2c_device_id *id)
1960 struct si4713_device *sdev;
1963 sdev = kzalloc(sizeof *sdev, GFP_KERNEL);
1965 dev_err(&client->dev, "Failed to alloc video device.\n");
1970 sdev->platform_data = client->dev.platform_data;
1971 if (!sdev->platform_data) {
1972 v4l2_err(&sdev->sd, "No platform data registered.\n");
1977 v4l2_i2c_subdev_init(&sdev->sd, client, &si4713_subdev_ops);
1979 mutex_init(&sdev->mutex);
1980 init_completion(&sdev->work);
1983 rval = request_irq(client->irq,
1984 si4713_handler, IRQF_TRIGGER_FALLING | IRQF_DISABLED,
1985 client->name, sdev);
1987 v4l2_err(&sdev->sd, "Could not request IRQ\n");
1990 v4l2_dbg(1, debug, &sdev->sd, "IRQ requested.\n");
1992 v4l2_warn(&sdev->sd, "IRQ not configured. Using timeouts.\n");
1995 rval = si4713_initialize(sdev);
1997 v4l2_err(&sdev->sd, "Failed to probe device information.\n");
2005 free_irq(client->irq, sdev);
2012 /* si4713_remove - remove the device */
2013 static int si4713_remove(struct i2c_client *client)
2015 struct v4l2_subdev *sd = i2c_get_clientdata(client);
2016 struct si4713_device *sdev = to_si4713_device(sd);
2018 if (sdev->power_state)
2019 si4713_set_power_state(sdev, POWER_DOWN);
2021 if (client->irq > 0)
2022 free_irq(client->irq, sdev);
2024 v4l2_device_unregister_subdev(sd);
2031 /* si4713_i2c_driver - i2c driver interface */
2032 static const struct i2c_device_id si4713_id[] = {
2036 MODULE_DEVICE_TABLE(i2c, si4713_id);
2038 static struct i2c_driver si4713_i2c_driver = {
2042 .probe = si4713_probe,
2043 .remove = si4713_remove,
2044 .id_table = si4713_id,
2047 /* Module Interface */
2048 static int __init si4713_module_init(void)
2050 return i2c_add_driver(&si4713_i2c_driver);
2053 static void __exit si4713_module_exit(void)
2055 i2c_del_driver(&si4713_i2c_driver);
2058 module_init(si4713_module_init);
2059 module_exit(si4713_module_exit);