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/completion.h>
25 #include <linux/delay.h>
26 #include <linux/interrupt.h>
27 #include <linux/i2c.h>
28 #include <linux/slab.h>
29 #include <linux/gpio.h>
30 #include <linux/regulator/consumer.h>
31 #include <linux/module.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_DEVIATION 0x00C8
56 #define DEFAULT_RDS_PS_REPEAT_COUNT 0x0003
57 #define DEFAULT_LIMITER_RTIME 0x1392
58 #define DEFAULT_LIMITER_DEV 0x102CA
59 #define DEFAULT_PILOT_FREQUENCY 0x4A38
60 #define DEFAULT_PILOT_DEVIATION 0x1A5E
61 #define DEFAULT_ACOMP_ATIME 0x0000
62 #define DEFAULT_ACOMP_RTIME 0xF4240L
63 #define DEFAULT_ACOMP_GAIN 0x0F
64 #define DEFAULT_ACOMP_THRESHOLD (-0x28)
65 #define DEFAULT_MUTE 0x01
66 #define DEFAULT_POWER_LEVEL 88
67 #define DEFAULT_FREQUENCY 8800
68 #define DEFAULT_PREEMPHASIS FMPE_EU
69 #define DEFAULT_TUNE_RNL 0xFF
71 #define to_si4713_device(sd) container_of(sd, struct si4713_device, sd)
73 /* frequency domain transformation (using times 10 to avoid floats) */
74 #define FREQDEV_UNIT 100000
75 #define FREQV4L2_MULTI 625
76 #define si4713_to_v4l2(f) ((f * FREQDEV_UNIT) / FREQV4L2_MULTI)
77 #define v4l2_to_si4713(f) ((f * FREQV4L2_MULTI) / FREQDEV_UNIT)
78 #define FREQ_RANGE_LOW 7600
79 #define FREQ_RANGE_HIGH 10800
84 #define RDS_BLOCK_CLEAR 0x03
85 #define RDS_BLOCK_LOAD 0x04
86 #define RDS_RADIOTEXT_2A 0x20
87 #define RDS_RADIOTEXT_BLK_SIZE 4
88 #define RDS_RADIOTEXT_INDEX_MAX 0x0F
89 #define RDS_CARRIAGE_RETURN 0x0D
91 #define rds_ps_nblocks(len) ((len / RDS_BLOCK) + (len % RDS_BLOCK ? 1 : 0))
93 #define get_status_bit(p, b, m) (((p) & (m)) >> (b))
94 #define set_bits(p, v, b, m) (((p) & ~(m)) | ((v) << (b)))
96 #define ATTACK_TIME_UNIT 500
98 #define POWER_OFF 0x00
101 #define msb(x) ((u8)((u16) x >> 8))
102 #define lsb(x) ((u8)((u16) x & 0x00FF))
103 #define compose_u16(msb, lsb) (((u16)msb << 8) | lsb)
104 #define check_command_failed(status) (!(status & SI4713_CTS) || \
105 (status & SI4713_ERR))
106 /* mute definition */
107 #define set_mute(p) ((p & 1) | ((p & 1) << 1));
110 #define DBG_BUFFER(device, message, buffer, size) \
113 char str[(size)*5]; \
114 for (i = 0; i < size; i++) \
115 sprintf(str + i * 5, " 0x%02x", buffer[i]); \
116 v4l2_dbg(2, debug, device, "%s:%s\n", message, str); \
119 #define DBG_BUFFER(device, message, buffer, size)
123 * Values for limiter release time (sorted by second column)
127 static long limiter_times
[] = {
151 * Values for audio compression release time (sorted by second column)
155 static unsigned long acomp_rtimes
[] = {
164 * Values for preemphasis (sorted by second column)
168 static unsigned long preemphasis_values
[] = {
169 FMPE_DISABLED
, V4L2_PREEMPHASIS_DISABLED
,
170 FMPE_EU
, V4L2_PREEMPHASIS_50_uS
,
171 FMPE_USA
, V4L2_PREEMPHASIS_75_uS
,
174 static int usecs_to_dev(unsigned long usecs
, unsigned long const array
[],
180 for (i
= 0; i
< size
/ 2; i
++)
181 if (array
[(i
* 2) + 1] >= usecs
) {
189 /* si4713_handler: IRQ handler, just complete work */
190 static irqreturn_t
si4713_handler(int irq
, void *dev
)
192 struct si4713_device
*sdev
= dev
;
194 v4l2_dbg(2, debug
, &sdev
->sd
,
195 "%s: sending signal to completion work.\n", __func__
);
196 complete(&sdev
->work
);
202 * si4713_send_command - sends a command to si4713 and waits its response
203 * @sdev: si4713_device structure for the device we are communicating
204 * @command: command id
205 * @args: command arguments we are sending (up to 7)
206 * @argn: actual size of @args
207 * @response: buffer to place the expected response from the device (up to 15)
208 * @respn: actual size of @response
209 * @usecs: amount of time to wait before reading the response (in usecs)
211 static int si4713_send_command(struct si4713_device
*sdev
, const u8 command
,
212 const u8 args
[], const int argn
,
213 u8 response
[], const int respn
, const int usecs
)
215 struct i2c_client
*client
= v4l2_get_subdevdata(&sdev
->sd
);
216 u8 data1
[MAX_ARGS
+ 1];
219 if (!client
->adapter
)
222 /* First send the command and its arguments */
224 memcpy(data1
+ 1, args
, argn
);
225 DBG_BUFFER(&sdev
->sd
, "Parameters", data1
, argn
+ 1);
227 err
= i2c_master_send(client
, data1
, argn
+ 1);
228 if (err
!= argn
+ 1) {
229 v4l2_err(&sdev
->sd
, "Error while sending command 0x%02x\n",
231 return (err
> 0) ? -EIO
: err
;
234 /* Wait response from interrupt */
235 if (!wait_for_completion_timeout(&sdev
->work
,
236 usecs_to_jiffies(usecs
) + 1))
238 "(%s) Device took too much time to answer.\n",
241 /* Then get the response */
242 err
= i2c_master_recv(client
, response
, respn
);
245 "Error while reading response for command 0x%02x\n",
247 return (err
> 0) ? -EIO
: err
;
250 DBG_BUFFER(&sdev
->sd
, "Response", response
, respn
);
251 if (check_command_failed(response
[0]))
258 * si4713_read_property - reads a si4713 property
259 * @sdev: si4713_device structure for the device we are communicating
260 * @prop: property identification number
261 * @pv: property value to be returned on success
263 static int si4713_read_property(struct si4713_device
*sdev
, u16 prop
, u32
*pv
)
266 u8 val
[SI4713_GET_PROP_NRESP
];
269 * .Second byte = property's MSB
270 * .Third byte = property's LSB
272 const u8 args
[SI4713_GET_PROP_NARGS
] = {
278 err
= si4713_send_command(sdev
, SI4713_CMD_GET_PROPERTY
,
279 args
, ARRAY_SIZE(args
), val
,
280 ARRAY_SIZE(val
), DEFAULT_TIMEOUT
);
285 *pv
= compose_u16(val
[2], val
[3]);
287 v4l2_dbg(1, debug
, &sdev
->sd
,
288 "%s: property=0x%02x value=0x%02x status=0x%02x\n",
289 __func__
, prop
, *pv
, val
[0]);
295 * si4713_write_property - modifies a si4713 property
296 * @sdev: si4713_device structure for the device we are communicating
297 * @prop: property identification number
298 * @val: new value for that property
300 static int si4713_write_property(struct si4713_device
*sdev
, u16 prop
, u16 val
)
303 u8 resp
[SI4713_SET_PROP_NRESP
];
306 * .Second byte = property's MSB
307 * .Third byte = property's LSB
308 * .Fourth byte = value's MSB
309 * .Fifth byte = value's LSB
311 const u8 args
[SI4713_SET_PROP_NARGS
] = {
319 rval
= si4713_send_command(sdev
, SI4713_CMD_SET_PROPERTY
,
320 args
, ARRAY_SIZE(args
),
321 resp
, ARRAY_SIZE(resp
),
327 v4l2_dbg(1, debug
, &sdev
->sd
,
328 "%s: property=0x%02x value=0x%02x status=0x%02x\n",
329 __func__
, prop
, val
, resp
[0]);
332 * As there is no command response for SET_PROPERTY,
333 * wait Tcomp time to finish before proceed, in order
334 * to have property properly set.
336 msleep(TIMEOUT_SET_PROPERTY
);
342 * si4713_powerup - Powers the device up
343 * @sdev: si4713_device structure for the device we are communicating
345 static int si4713_powerup(struct si4713_device
*sdev
)
348 u8 resp
[SI4713_PWUP_NRESP
];
350 * .First byte = Enabled interrupts and boot function
351 * .Second byte = Input operation mode
353 const u8 args
[SI4713_PWUP_NARGS
] = {
354 SI4713_PWUP_CTSIEN
| SI4713_PWUP_GPO2OEN
| SI4713_PWUP_FUNC_TX
,
355 SI4713_PWUP_OPMOD_ANALOG
,
358 if (sdev
->power_state
)
361 err
= regulator_bulk_enable(ARRAY_SIZE(sdev
->supplies
),
364 v4l2_err(&sdev
->sd
, "Failed to enable supplies: %d\n", err
);
367 if (gpio_is_valid(sdev
->gpio_reset
)) {
369 gpio_set_value(sdev
->gpio_reset
, 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 if (gpio_is_valid(sdev
->gpio_reset
))
387 gpio_set_value(sdev
->gpio_reset
, 0);
388 err
= regulator_bulk_disable(ARRAY_SIZE(sdev
->supplies
),
392 "Failed to disable supplies: %d\n", err
);
399 * si4713_powerdown - Powers the device down
400 * @sdev: si4713_device structure for the device we are communicating
402 static int si4713_powerdown(struct si4713_device
*sdev
)
405 u8 resp
[SI4713_PWDN_NRESP
];
407 if (!sdev
->power_state
)
410 err
= si4713_send_command(sdev
, SI4713_CMD_POWER_DOWN
,
412 resp
, ARRAY_SIZE(resp
),
416 v4l2_dbg(1, debug
, &sdev
->sd
, "Power down response: 0x%02x\n",
418 v4l2_dbg(1, debug
, &sdev
->sd
, "Device in reset mode\n");
419 if (gpio_is_valid(sdev
->gpio_reset
))
420 gpio_set_value(sdev
->gpio_reset
, 0);
421 err
= regulator_bulk_disable(ARRAY_SIZE(sdev
->supplies
),
425 "Failed to disable supplies: %d\n", err
);
426 sdev
->power_state
= POWER_OFF
;
433 * si4713_checkrev - Checks if we are treating a device with the correct rev.
434 * @sdev: si4713_device structure for the device we are communicating
436 static int si4713_checkrev(struct si4713_device
*sdev
)
438 struct i2c_client
*client
= v4l2_get_subdevdata(&sdev
->sd
);
440 u8 resp
[SI4713_GETREV_NRESP
];
442 rval
= si4713_send_command(sdev
, SI4713_CMD_GET_REV
,
444 resp
, ARRAY_SIZE(resp
),
450 if (resp
[1] == SI4713_PRODUCT_NUMBER
) {
451 v4l2_info(&sdev
->sd
, "chip found @ 0x%02x (%s)\n",
452 client
->addr
<< 1, client
->adapter
->name
);
454 v4l2_err(&sdev
->sd
, "Invalid product number\n");
461 * si4713_wait_stc - Waits STC interrupt and clears status bits. Useful
462 * for TX_TUNE_POWER, TX_TUNE_FREQ and TX_TUNE_MEAS
463 * @sdev: si4713_device structure for the device we are communicating
464 * @usecs: timeout to wait for STC interrupt signal
466 static int si4713_wait_stc(struct si4713_device
*sdev
, const int usecs
)
469 u8 resp
[SI4713_GET_STATUS_NRESP
];
471 /* Wait response from STC interrupt */
472 if (!wait_for_completion_timeout(&sdev
->work
,
473 usecs_to_jiffies(usecs
) + 1))
475 "%s: device took too much time to answer (%d usec).\n",
478 /* Clear status bits */
479 err
= si4713_send_command(sdev
, SI4713_CMD_GET_INT_STATUS
,
481 resp
, ARRAY_SIZE(resp
),
487 v4l2_dbg(1, debug
, &sdev
->sd
,
488 "%s: status bits: 0x%02x\n", __func__
, resp
[0]);
490 if (!(resp
[0] & SI4713_STC_INT
))
498 * si4713_tx_tune_freq - Sets the state of the RF carrier and sets the tuning
499 * frequency between 76 and 108 MHz in 10 kHz units and
501 * @sdev: si4713_device structure for the device we are communicating
502 * @frequency: desired frequency (76 - 108 MHz, unit 10 KHz, step 50 kHz)
504 static int si4713_tx_tune_freq(struct si4713_device
*sdev
, u16 frequency
)
507 u8 val
[SI4713_TXFREQ_NRESP
];
510 * .Second byte = frequency's MSB
511 * .Third byte = frequency's LSB
513 const u8 args
[SI4713_TXFREQ_NARGS
] = {
519 err
= si4713_send_command(sdev
, SI4713_CMD_TX_TUNE_FREQ
,
520 args
, ARRAY_SIZE(args
), val
,
521 ARRAY_SIZE(val
), DEFAULT_TIMEOUT
);
526 v4l2_dbg(1, debug
, &sdev
->sd
,
527 "%s: frequency=0x%02x status=0x%02x\n", __func__
,
530 err
= si4713_wait_stc(sdev
, TIMEOUT_TX_TUNE
);
534 return compose_u16(args
[1], args
[2]);
538 * si4713_tx_tune_power - Sets the RF voltage level between 88 and 115 dBuV in
539 * 1 dB units. A value of 0x00 indicates off. The command
540 * also sets the antenna tuning capacitance. A value of 0
541 * indicates autotuning, and a value of 1 - 191 indicates
542 * a manual override, which results in a tuning
543 * capacitance of 0.25 pF x @antcap.
544 * @sdev: si4713_device structure for the device we are communicating
545 * @power: tuning power (88 - 115 dBuV, unit/step 1 dB)
546 * @antcap: value of antenna tuning capacitor (0 - 191)
548 static int si4713_tx_tune_power(struct si4713_device
*sdev
, u8 power
,
552 u8 val
[SI4713_TXPWR_NRESP
];
556 * .Third byte = power
557 * .Fourth byte = antcap
559 const u8 args
[SI4713_TXPWR_NARGS
] = {
566 if (((power
> 0) && (power
< SI4713_MIN_POWER
)) ||
567 power
> SI4713_MAX_POWER
|| antcap
> SI4713_MAX_ANTCAP
)
570 err
= si4713_send_command(sdev
, SI4713_CMD_TX_TUNE_POWER
,
571 args
, ARRAY_SIZE(args
), val
,
572 ARRAY_SIZE(val
), DEFAULT_TIMEOUT
);
577 v4l2_dbg(1, debug
, &sdev
->sd
,
578 "%s: power=0x%02x antcap=0x%02x status=0x%02x\n",
579 __func__
, power
, antcap
, val
[0]);
581 return si4713_wait_stc(sdev
, TIMEOUT_TX_TUNE_POWER
);
585 * si4713_tx_tune_measure - Enters receive mode and measures the received noise
586 * level in units of dBuV on the selected frequency.
587 * The Frequency must be between 76 and 108 MHz in 10 kHz
588 * units and steps of 50 kHz. The command also sets the
589 * antenna tuning capacitance. A value of 0 means
590 * autotuning, and a value of 1 to 191 indicates manual
592 * @sdev: si4713_device structure for the device we are communicating
593 * @frequency: desired frequency (76 - 108 MHz, unit 10 KHz, step 50 kHz)
594 * @antcap: value of antenna tuning capacitor (0 - 191)
596 static int si4713_tx_tune_measure(struct si4713_device
*sdev
, u16 frequency
,
600 u8 val
[SI4713_TXMEA_NRESP
];
603 * .Second byte = frequency's MSB
604 * .Third byte = frequency's LSB
605 * .Fourth byte = antcap
607 const u8 args
[SI4713_TXMEA_NARGS
] = {
614 sdev
->tune_rnl
= DEFAULT_TUNE_RNL
;
616 if (antcap
> SI4713_MAX_ANTCAP
)
619 err
= si4713_send_command(sdev
, SI4713_CMD_TX_TUNE_MEASURE
,
620 args
, ARRAY_SIZE(args
), val
,
621 ARRAY_SIZE(val
), DEFAULT_TIMEOUT
);
626 v4l2_dbg(1, debug
, &sdev
->sd
,
627 "%s: frequency=0x%02x antcap=0x%02x status=0x%02x\n",
628 __func__
, frequency
, antcap
, val
[0]);
630 return si4713_wait_stc(sdev
, TIMEOUT_TX_TUNE
);
634 * si4713_tx_tune_status- Returns the status of the tx_tune_freq, tx_tune_mea or
635 * tx_tune_power commands. This command return the current
636 * frequency, output voltage in dBuV, the antenna tunning
637 * capacitance value and the received noise level. The
638 * command also clears the stcint interrupt bit when the
639 * first bit of its arguments is high.
640 * @sdev: si4713_device structure for the device we are communicating
641 * @intack: 0x01 to clear the seek/tune complete interrupt status indicator.
642 * @frequency: returned frequency
643 * @power: returned power
644 * @antcap: returned antenna capacitance
645 * @noise: returned noise level
647 static int si4713_tx_tune_status(struct si4713_device
*sdev
, u8 intack
,
648 u16
*frequency
, u8
*power
,
649 u8
*antcap
, u8
*noise
)
652 u8 val
[SI4713_TXSTATUS_NRESP
];
654 * .First byte = intack bit
656 const u8 args
[SI4713_TXSTATUS_NARGS
] = {
657 intack
& SI4713_INTACK_MASK
,
660 err
= si4713_send_command(sdev
, SI4713_CMD_TX_TUNE_STATUS
,
661 args
, ARRAY_SIZE(args
), val
,
662 ARRAY_SIZE(val
), DEFAULT_TIMEOUT
);
665 v4l2_dbg(1, debug
, &sdev
->sd
,
666 "%s: status=0x%02x\n", __func__
, val
[0]);
667 *frequency
= compose_u16(val
[2], val
[3]);
668 sdev
->frequency
= *frequency
;
672 v4l2_dbg(1, debug
, &sdev
->sd
, "%s: response: %d x 10 kHz "
673 "(power %d, antcap %d, rnl %d)\n", __func__
,
674 *frequency
, *power
, *antcap
, *noise
);
681 * si4713_tx_rds_buff - Loads the RDS group buffer FIFO or circular buffer.
682 * @sdev: si4713_device structure for the device we are communicating
683 * @mode: the buffer operation mode.
687 * @cbleft: returns the number of available circular buffer blocks minus the
688 * number of used circular buffer blocks.
690 static int si4713_tx_rds_buff(struct si4713_device
*sdev
, u8 mode
, u16 rdsb
,
691 u16 rdsc
, u16 rdsd
, s8
*cbleft
)
694 u8 val
[SI4713_RDSBUFF_NRESP
];
696 const u8 args
[SI4713_RDSBUFF_NARGS
] = {
697 mode
& SI4713_RDSBUFF_MODE_MASK
,
706 err
= si4713_send_command(sdev
, SI4713_CMD_TX_RDS_BUFF
,
707 args
, ARRAY_SIZE(args
), val
,
708 ARRAY_SIZE(val
), DEFAULT_TIMEOUT
);
711 v4l2_dbg(1, debug
, &sdev
->sd
,
712 "%s: status=0x%02x\n", __func__
, val
[0]);
713 *cbleft
= (s8
)val
[2] - val
[3];
714 v4l2_dbg(1, debug
, &sdev
->sd
, "%s: response: interrupts"
715 " 0x%02x cb avail: %d cb used %d fifo avail"
716 " %d fifo used %d\n", __func__
, val
[1],
717 val
[2], val
[3], val
[4], val
[5]);
724 * si4713_tx_rds_ps - Loads the program service buffer.
725 * @sdev: si4713_device structure for the device we are communicating
726 * @psid: program service id to be loaded.
727 * @pschar: assumed 4 size char array to be loaded into the program service
729 static int si4713_tx_rds_ps(struct si4713_device
*sdev
, u8 psid
,
730 unsigned char *pschar
)
733 u8 val
[SI4713_RDSPS_NRESP
];
735 const u8 args
[SI4713_RDSPS_NARGS
] = {
736 psid
& SI4713_RDSPS_PSID_MASK
,
743 err
= si4713_send_command(sdev
, SI4713_CMD_TX_RDS_PS
,
744 args
, ARRAY_SIZE(args
), val
,
745 ARRAY_SIZE(val
), DEFAULT_TIMEOUT
);
750 v4l2_dbg(1, debug
, &sdev
->sd
, "%s: status=0x%02x\n", __func__
, val
[0]);
755 static int si4713_set_power_state(struct si4713_device
*sdev
, u8 value
)
758 return si4713_powerup(sdev
);
759 return si4713_powerdown(sdev
);
762 static int si4713_set_mute(struct si4713_device
*sdev
, u16 mute
)
766 mute
= set_mute(mute
);
768 if (sdev
->power_state
)
769 rval
= si4713_write_property(sdev
,
770 SI4713_TX_LINE_INPUT_MUTE
, mute
);
775 static int si4713_set_rds_ps_name(struct si4713_device
*sdev
, char *ps_name
)
780 /* We want to clear the whole thing */
781 if (!strlen(ps_name
))
782 memset(ps_name
, 0, MAX_RDS_PS_NAME
+ 1);
784 if (sdev
->power_state
) {
785 /* Write the new ps name and clear the padding */
786 for (i
= 0; i
< MAX_RDS_PS_NAME
; i
+= (RDS_BLOCK
/ 2)) {
787 rval
= si4713_tx_rds_ps(sdev
, (i
/ (RDS_BLOCK
/ 2)),
793 /* Setup the size to be sent */
795 len
= strlen(ps_name
) - 1;
799 rval
= si4713_write_property(sdev
,
800 SI4713_TX_RDS_PS_MESSAGE_COUNT
,
801 rds_ps_nblocks(len
));
805 rval
= si4713_write_property(sdev
,
806 SI4713_TX_RDS_PS_REPEAT_COUNT
,
807 DEFAULT_RDS_PS_REPEAT_COUNT
* 2);
815 static int si4713_set_rds_radio_text(struct si4713_device
*sdev
, char *rt
)
819 u8 b_index
= 0, cr_inserted
= 0;
822 if (!sdev
->power_state
)
825 rval
= si4713_tx_rds_buff(sdev
, RDS_BLOCK_CLEAR
, 0, 0, 0, &left
);
833 /* RDS spec says that if the last block isn't used,
834 * then apply a carriage return
836 if (t_index
< (RDS_RADIOTEXT_INDEX_MAX
* RDS_RADIOTEXT_BLK_SIZE
)) {
837 for (i
= 0; i
< RDS_RADIOTEXT_BLK_SIZE
; i
++) {
838 if (!rt
[t_index
+ i
] ||
839 rt
[t_index
+ i
] == RDS_CARRIAGE_RETURN
) {
840 rt
[t_index
+ i
] = RDS_CARRIAGE_RETURN
;
847 rval
= si4713_tx_rds_buff(sdev
, RDS_BLOCK_LOAD
,
848 compose_u16(RDS_RADIOTEXT_2A
, b_index
++),
849 compose_u16(rt
[t_index
], rt
[t_index
+ 1]),
850 compose_u16(rt
[t_index
+ 2], rt
[t_index
+ 3]),
855 t_index
+= RDS_RADIOTEXT_BLK_SIZE
;
865 * si4713_update_tune_status - update properties from tx_tune_status
866 * command. Must be called with sdev->mutex held.
867 * @sdev: si4713_device structure for the device we are communicating
869 static int si4713_update_tune_status(struct si4713_device
*sdev
)
873 u8 p
= 0, a
= 0, n
= 0;
875 rval
= si4713_tx_tune_status(sdev
, 0x00, &f
, &p
, &a
, &n
);
880 /* TODO: check that power_level and antenna_capacitor really are not
881 changed by the hardware. If they are, then these controls should become
883 sdev->power_level = p;
884 sdev->antenna_capacitor = a;*/
891 static int si4713_choose_econtrol_action(struct si4713_device
*sdev
, u32 id
,
892 s32
*bit
, s32
*mask
, u16
*property
, int *mul
,
893 unsigned long **table
, int *size
)
898 /* FM_TX class controls */
899 case V4L2_CID_RDS_TX_PI
:
900 *property
= SI4713_TX_RDS_PI
;
903 case V4L2_CID_AUDIO_COMPRESSION_THRESHOLD
:
904 *property
= SI4713_TX_ACOMP_THRESHOLD
;
907 case V4L2_CID_AUDIO_COMPRESSION_GAIN
:
908 *property
= SI4713_TX_ACOMP_GAIN
;
911 case V4L2_CID_PILOT_TONE_FREQUENCY
:
912 *property
= SI4713_TX_PILOT_FREQUENCY
;
915 case V4L2_CID_AUDIO_COMPRESSION_ATTACK_TIME
:
916 *property
= SI4713_TX_ACOMP_ATTACK_TIME
;
917 *mul
= ATTACK_TIME_UNIT
;
919 case V4L2_CID_PILOT_TONE_DEVIATION
:
920 *property
= SI4713_TX_PILOT_DEVIATION
;
923 case V4L2_CID_AUDIO_LIMITER_DEVIATION
:
924 *property
= SI4713_TX_AUDIO_DEVIATION
;
927 case V4L2_CID_RDS_TX_DEVIATION
:
928 *property
= SI4713_TX_RDS_DEVIATION
;
932 case V4L2_CID_RDS_TX_PTY
:
933 *property
= SI4713_TX_RDS_PS_MISC
;
937 case V4L2_CID_AUDIO_LIMITER_ENABLED
:
938 *property
= SI4713_TX_ACOMP_ENABLE
;
942 case V4L2_CID_AUDIO_COMPRESSION_ENABLED
:
943 *property
= SI4713_TX_ACOMP_ENABLE
;
947 case V4L2_CID_PILOT_TONE_ENABLED
:
948 *property
= SI4713_TX_COMPONENT_ENABLE
;
953 case V4L2_CID_AUDIO_LIMITER_RELEASE_TIME
:
954 *property
= SI4713_TX_LIMITER_RELEASE_TIME
;
955 *table
= limiter_times
;
956 *size
= ARRAY_SIZE(limiter_times
);
958 case V4L2_CID_AUDIO_COMPRESSION_RELEASE_TIME
:
959 *property
= SI4713_TX_ACOMP_RELEASE_TIME
;
960 *table
= acomp_rtimes
;
961 *size
= ARRAY_SIZE(acomp_rtimes
);
963 case V4L2_CID_TUNE_PREEMPHASIS
:
964 *property
= SI4713_TX_PREEMPHASIS
;
965 *table
= preemphasis_values
;
966 *size
= ARRAY_SIZE(preemphasis_values
);
977 static int si4713_s_frequency(struct v4l2_subdev
*sd
, const struct v4l2_frequency
*f
);
978 static int si4713_s_modulator(struct v4l2_subdev
*sd
, const struct v4l2_modulator
*);
980 * si4713_setup - Sets the device up with current configuration.
981 * @sdev: si4713_device structure for the device we are communicating
983 static int si4713_setup(struct si4713_device
*sdev
)
985 struct v4l2_frequency f
;
986 struct v4l2_modulator vm
;
989 /* Device procedure needs to set frequency first */
991 f
.frequency
= sdev
->frequency
? sdev
->frequency
: DEFAULT_FREQUENCY
;
992 f
.frequency
= si4713_to_v4l2(f
.frequency
);
993 rval
= si4713_s_frequency(&sdev
->sd
, &f
);
997 vm
.txsubchans
= V4L2_TUNER_SUB_STEREO
;
999 vm
.txsubchans
= V4L2_TUNER_SUB_MONO
;
1000 if (sdev
->rds_enabled
)
1001 vm
.txsubchans
|= V4L2_TUNER_SUB_RDS
;
1002 si4713_s_modulator(&sdev
->sd
, &vm
);
1008 * si4713_initialize - Sets the device up with default configuration.
1009 * @sdev: si4713_device structure for the device we are communicating
1011 static int si4713_initialize(struct si4713_device
*sdev
)
1015 rval
= si4713_set_power_state(sdev
, POWER_ON
);
1019 rval
= si4713_checkrev(sdev
);
1023 rval
= si4713_set_power_state(sdev
, POWER_OFF
);
1028 sdev
->frequency
= DEFAULT_FREQUENCY
;
1030 sdev
->tune_rnl
= DEFAULT_TUNE_RNL
;
1034 /* si4713_s_ctrl - set the value of a control */
1035 static int si4713_s_ctrl(struct v4l2_ctrl
*ctrl
)
1037 struct si4713_device
*sdev
=
1038 container_of(ctrl
->handler
, struct si4713_device
, ctrl_handler
);
1040 s32 bit
= 0, mask
= 0;
1043 unsigned long *table
= NULL
;
1049 if (ctrl
->id
!= V4L2_CID_AUDIO_MUTE
)
1053 ret
= si4713_set_mute(sdev
, ctrl
->val
);
1055 ret
= si4713_set_power_state(sdev
, POWER_DOWN
);
1058 ret
= si4713_set_power_state(sdev
, POWER_UP
);
1060 ret
= si4713_set_mute(sdev
, ctrl
->val
);
1062 ret
= si4713_setup(sdev
);
1068 if (!sdev
->power_state
)
1071 for (c
= 1; !ret
&& c
< ctrl
->ncontrols
; c
++) {
1072 ctrl
= ctrl
->cluster
[c
];
1074 if (!force
&& !ctrl
->is_new
)
1078 case V4L2_CID_RDS_TX_PS_NAME
:
1079 ret
= si4713_set_rds_ps_name(sdev
, ctrl
->string
);
1082 case V4L2_CID_RDS_TX_RADIO_TEXT
:
1083 ret
= si4713_set_rds_radio_text(sdev
, ctrl
->string
);
1086 case V4L2_CID_TUNE_ANTENNA_CAPACITOR
:
1087 /* don't handle this control if we force setting all
1088 * controls since in that case it will be handled by
1089 * V4L2_CID_TUNE_POWER_LEVEL. */
1093 case V4L2_CID_TUNE_POWER_LEVEL
:
1094 ret
= si4713_tx_tune_power(sdev
,
1095 sdev
->tune_pwr_level
->val
, sdev
->tune_ant_cap
->val
);
1097 /* Make sure we don't set this twice */
1098 sdev
->tune_ant_cap
->is_new
= false;
1099 sdev
->tune_pwr_level
->is_new
= false;
1104 ret
= si4713_choose_econtrol_action(sdev
, ctrl
->id
, &bit
,
1105 &mask
, &property
, &mul
, &table
, &size
);
1113 ret
= usecs_to_dev(val
, table
, size
);
1121 ret
= si4713_read_property(sdev
, property
, &val
);
1124 val
= set_bits(val
, ctrl
->val
, bit
, mask
);
1127 ret
= si4713_write_property(sdev
, property
, val
);
1139 /* si4713_ioctl - deal with private ioctls (only rnl for now) */
1140 static long si4713_ioctl(struct v4l2_subdev
*sd
, unsigned int cmd
, void *arg
)
1142 struct si4713_device
*sdev
= to_si4713_device(sd
);
1143 struct si4713_rnl
*rnl
= arg
;
1151 case SI4713_IOC_MEASURE_RNL
:
1152 frequency
= v4l2_to_si4713(rnl
->frequency
);
1154 if (sdev
->power_state
) {
1155 /* Set desired measurement frequency */
1156 rval
= si4713_tx_tune_measure(sdev
, frequency
, 0);
1159 /* get results from tune status */
1160 rval
= si4713_update_tune_status(sdev
);
1164 rnl
->rnl
= sdev
->tune_rnl
;
1169 rval
= -ENOIOCTLCMD
;
1175 /* si4713_g_modulator - get modulator attributes */
1176 static int si4713_g_modulator(struct v4l2_subdev
*sd
, struct v4l2_modulator
*vm
)
1178 struct si4713_device
*sdev
= to_si4713_device(sd
);
1187 strncpy(vm
->name
, "FM Modulator", 32);
1188 vm
->capability
= V4L2_TUNER_CAP_STEREO
| V4L2_TUNER_CAP_LOW
|
1189 V4L2_TUNER_CAP_RDS
| V4L2_TUNER_CAP_RDS_CONTROLS
;
1191 /* Report current frequency range limits */
1192 vm
->rangelow
= si4713_to_v4l2(FREQ_RANGE_LOW
);
1193 vm
->rangehigh
= si4713_to_v4l2(FREQ_RANGE_HIGH
);
1195 if (sdev
->power_state
) {
1198 rval
= si4713_read_property(sdev
, SI4713_TX_COMPONENT_ENABLE
,
1203 sdev
->stereo
= get_status_bit(comp_en
, 1, 1 << 1);
1206 /* Report current audio mode: mono or stereo */
1208 vm
->txsubchans
= V4L2_TUNER_SUB_STEREO
;
1210 vm
->txsubchans
= V4L2_TUNER_SUB_MONO
;
1212 /* Report rds feature status */
1213 if (sdev
->rds_enabled
)
1214 vm
->txsubchans
|= V4L2_TUNER_SUB_RDS
;
1216 vm
->txsubchans
&= ~V4L2_TUNER_SUB_RDS
;
1221 /* si4713_s_modulator - set modulator attributes */
1222 static int si4713_s_modulator(struct v4l2_subdev
*sd
, const struct v4l2_modulator
*vm
)
1224 struct si4713_device
*sdev
= to_si4713_device(sd
);
1235 /* Set audio mode: mono or stereo */
1236 if (vm
->txsubchans
& V4L2_TUNER_SUB_STEREO
)
1238 else if (vm
->txsubchans
& V4L2_TUNER_SUB_MONO
)
1243 rds
= !!(vm
->txsubchans
& V4L2_TUNER_SUB_RDS
);
1245 if (sdev
->power_state
) {
1246 rval
= si4713_read_property(sdev
,
1247 SI4713_TX_COMPONENT_ENABLE
, &p
);
1251 p
= set_bits(p
, stereo
, 1, 1 << 1);
1252 p
= set_bits(p
, rds
, 2, 1 << 2);
1254 rval
= si4713_write_property(sdev
,
1255 SI4713_TX_COMPONENT_ENABLE
, p
);
1260 sdev
->stereo
= stereo
;
1261 sdev
->rds_enabled
= rds
;
1266 /* si4713_g_frequency - get tuner or modulator radio frequency */
1267 static int si4713_g_frequency(struct v4l2_subdev
*sd
, struct v4l2_frequency
*f
)
1269 struct si4713_device
*sdev
= to_si4713_device(sd
);
1275 if (sdev
->power_state
) {
1279 rval
= si4713_tx_tune_status(sdev
, 0x00, &freq
, &p
, &a
, &n
);
1283 sdev
->frequency
= freq
;
1286 f
->frequency
= si4713_to_v4l2(sdev
->frequency
);
1291 /* si4713_s_frequency - set tuner or modulator radio frequency */
1292 static int si4713_s_frequency(struct v4l2_subdev
*sd
, const struct v4l2_frequency
*f
)
1294 struct si4713_device
*sdev
= to_si4713_device(sd
);
1296 u16 frequency
= v4l2_to_si4713(f
->frequency
);
1301 /* Check frequency range */
1302 frequency
= clamp_t(u16
, frequency
, FREQ_RANGE_LOW
, FREQ_RANGE_HIGH
);
1304 if (sdev
->power_state
) {
1305 rval
= si4713_tx_tune_freq(sdev
, frequency
);
1311 sdev
->frequency
= frequency
;
1316 static const struct v4l2_ctrl_ops si4713_ctrl_ops
= {
1317 .s_ctrl
= si4713_s_ctrl
,
1320 static const struct v4l2_subdev_core_ops si4713_subdev_core_ops
= {
1321 .ioctl
= si4713_ioctl
,
1324 static const struct v4l2_subdev_tuner_ops si4713_subdev_tuner_ops
= {
1325 .g_frequency
= si4713_g_frequency
,
1326 .s_frequency
= si4713_s_frequency
,
1327 .g_modulator
= si4713_g_modulator
,
1328 .s_modulator
= si4713_s_modulator
,
1331 static const struct v4l2_subdev_ops si4713_subdev_ops
= {
1332 .core
= &si4713_subdev_core_ops
,
1333 .tuner
= &si4713_subdev_tuner_ops
,
1337 * I2C driver interface
1339 /* si4713_probe - probe for the device */
1340 static int si4713_probe(struct i2c_client
*client
,
1341 const struct i2c_device_id
*id
)
1343 struct si4713_device
*sdev
;
1344 struct si4713_platform_data
*pdata
= client
->dev
.platform_data
;
1345 struct v4l2_ctrl_handler
*hdl
;
1348 sdev
= kzalloc(sizeof *sdev
, GFP_KERNEL
);
1350 dev_err(&client
->dev
, "Failed to alloc video device.\n");
1355 sdev
->gpio_reset
= -1;
1356 if (pdata
&& gpio_is_valid(pdata
->gpio_reset
)) {
1357 rval
= gpio_request(pdata
->gpio_reset
, "si4713 reset");
1359 dev_err(&client
->dev
,
1360 "Failed to request gpio: %d\n", rval
);
1363 sdev
->gpio_reset
= pdata
->gpio_reset
;
1364 gpio_direction_output(sdev
->gpio_reset
, 0);
1367 for (i
= 0; i
< ARRAY_SIZE(sdev
->supplies
); i
++)
1368 sdev
->supplies
[i
].supply
= si4713_supply_names
[i
];
1370 rval
= regulator_bulk_get(&client
->dev
, ARRAY_SIZE(sdev
->supplies
),
1373 dev_err(&client
->dev
, "Cannot get regulators: %d\n", rval
);
1377 v4l2_i2c_subdev_init(&sdev
->sd
, client
, &si4713_subdev_ops
);
1379 init_completion(&sdev
->work
);
1381 hdl
= &sdev
->ctrl_handler
;
1382 v4l2_ctrl_handler_init(hdl
, 20);
1383 sdev
->mute
= v4l2_ctrl_new_std(hdl
, &si4713_ctrl_ops
,
1384 V4L2_CID_AUDIO_MUTE
, 0, 1, 1, DEFAULT_MUTE
);
1386 sdev
->rds_pi
= v4l2_ctrl_new_std(hdl
, &si4713_ctrl_ops
,
1387 V4L2_CID_RDS_TX_PI
, 0, 0xffff, 1, DEFAULT_RDS_PI
);
1388 sdev
->rds_pty
= v4l2_ctrl_new_std(hdl
, &si4713_ctrl_ops
,
1389 V4L2_CID_RDS_TX_PTY
, 0, 31, 1, DEFAULT_RDS_PTY
);
1390 sdev
->rds_deviation
= v4l2_ctrl_new_std(hdl
, &si4713_ctrl_ops
,
1391 V4L2_CID_RDS_TX_DEVIATION
, 0, MAX_RDS_DEVIATION
,
1392 10, DEFAULT_RDS_DEVIATION
);
1394 * Report step as 8. From RDS spec, psname
1395 * should be 8. But there are receivers which scroll strings
1398 sdev
->rds_ps_name
= v4l2_ctrl_new_std(hdl
, &si4713_ctrl_ops
,
1399 V4L2_CID_RDS_TX_PS_NAME
, 0, MAX_RDS_PS_NAME
, 8, 0);
1401 * Report step as 32 (2A block). From RDS spec,
1402 * radio text should be 32 for 2A block. But there are receivers
1403 * which scroll strings sized as 32xN. Setting default to 32.
1405 sdev
->rds_radio_text
= v4l2_ctrl_new_std(hdl
, &si4713_ctrl_ops
,
1406 V4L2_CID_RDS_TX_RADIO_TEXT
, 0, MAX_RDS_RADIO_TEXT
, 32, 0);
1408 sdev
->limiter_enabled
= v4l2_ctrl_new_std(hdl
, &si4713_ctrl_ops
,
1409 V4L2_CID_AUDIO_LIMITER_ENABLED
, 0, 1, 1, 1);
1410 sdev
->limiter_release_time
= v4l2_ctrl_new_std(hdl
, &si4713_ctrl_ops
,
1411 V4L2_CID_AUDIO_LIMITER_RELEASE_TIME
, 250,
1412 MAX_LIMITER_RELEASE_TIME
, 10, DEFAULT_LIMITER_RTIME
);
1413 sdev
->limiter_deviation
= v4l2_ctrl_new_std(hdl
, &si4713_ctrl_ops
,
1414 V4L2_CID_AUDIO_LIMITER_DEVIATION
, 0,
1415 MAX_LIMITER_DEVIATION
, 10, DEFAULT_LIMITER_DEV
);
1417 sdev
->compression_enabled
= v4l2_ctrl_new_std(hdl
, &si4713_ctrl_ops
,
1418 V4L2_CID_AUDIO_COMPRESSION_ENABLED
, 0, 1, 1, 1);
1419 sdev
->compression_gain
= v4l2_ctrl_new_std(hdl
, &si4713_ctrl_ops
,
1420 V4L2_CID_AUDIO_COMPRESSION_GAIN
, 0, MAX_ACOMP_GAIN
, 1,
1421 DEFAULT_ACOMP_GAIN
);
1422 sdev
->compression_threshold
= v4l2_ctrl_new_std(hdl
, &si4713_ctrl_ops
,
1423 V4L2_CID_AUDIO_COMPRESSION_THRESHOLD
, MIN_ACOMP_THRESHOLD
,
1424 MAX_ACOMP_THRESHOLD
, 1,
1425 DEFAULT_ACOMP_THRESHOLD
);
1426 sdev
->compression_attack_time
= v4l2_ctrl_new_std(hdl
, &si4713_ctrl_ops
,
1427 V4L2_CID_AUDIO_COMPRESSION_ATTACK_TIME
, 0,
1428 MAX_ACOMP_ATTACK_TIME
, 500, DEFAULT_ACOMP_ATIME
);
1429 sdev
->compression_release_time
= v4l2_ctrl_new_std(hdl
, &si4713_ctrl_ops
,
1430 V4L2_CID_AUDIO_COMPRESSION_RELEASE_TIME
, 100000,
1431 MAX_ACOMP_RELEASE_TIME
, 100000, DEFAULT_ACOMP_RTIME
);
1433 sdev
->pilot_tone_enabled
= v4l2_ctrl_new_std(hdl
, &si4713_ctrl_ops
,
1434 V4L2_CID_PILOT_TONE_ENABLED
, 0, 1, 1, 1);
1435 sdev
->pilot_tone_deviation
= v4l2_ctrl_new_std(hdl
, &si4713_ctrl_ops
,
1436 V4L2_CID_PILOT_TONE_DEVIATION
, 0, MAX_PILOT_DEVIATION
,
1437 10, DEFAULT_PILOT_DEVIATION
);
1438 sdev
->pilot_tone_freq
= v4l2_ctrl_new_std(hdl
, &si4713_ctrl_ops
,
1439 V4L2_CID_PILOT_TONE_FREQUENCY
, 0, MAX_PILOT_FREQUENCY
,
1440 1, DEFAULT_PILOT_FREQUENCY
);
1442 sdev
->tune_preemphasis
= v4l2_ctrl_new_std_menu(hdl
, &si4713_ctrl_ops
,
1443 V4L2_CID_TUNE_PREEMPHASIS
,
1444 V4L2_PREEMPHASIS_75_uS
, 0, V4L2_PREEMPHASIS_50_uS
);
1445 sdev
->tune_pwr_level
= v4l2_ctrl_new_std(hdl
, &si4713_ctrl_ops
,
1446 V4L2_CID_TUNE_POWER_LEVEL
, 0, 120, 1, DEFAULT_POWER_LEVEL
);
1447 sdev
->tune_ant_cap
= v4l2_ctrl_new_std(hdl
, &si4713_ctrl_ops
,
1448 V4L2_CID_TUNE_ANTENNA_CAPACITOR
, 0, 191, 1, 0);
1454 v4l2_ctrl_cluster(20, &sdev
->mute
);
1455 sdev
->sd
.ctrl_handler
= hdl
;
1458 rval
= request_irq(client
->irq
,
1459 si4713_handler
, IRQF_TRIGGER_FALLING
| IRQF_DISABLED
,
1460 client
->name
, sdev
);
1462 v4l2_err(&sdev
->sd
, "Could not request IRQ\n");
1465 v4l2_dbg(1, debug
, &sdev
->sd
, "IRQ requested.\n");
1467 v4l2_warn(&sdev
->sd
, "IRQ not configured. Using timeouts.\n");
1470 rval
= si4713_initialize(sdev
);
1472 v4l2_err(&sdev
->sd
, "Failed to probe device information.\n");
1480 free_irq(client
->irq
, sdev
);
1482 v4l2_ctrl_handler_free(hdl
);
1484 regulator_bulk_free(ARRAY_SIZE(sdev
->supplies
), sdev
->supplies
);
1486 if (gpio_is_valid(sdev
->gpio_reset
))
1487 gpio_free(sdev
->gpio_reset
);
1494 /* si4713_remove - remove the device */
1495 static int si4713_remove(struct i2c_client
*client
)
1497 struct v4l2_subdev
*sd
= i2c_get_clientdata(client
);
1498 struct si4713_device
*sdev
= to_si4713_device(sd
);
1500 if (sdev
->power_state
)
1501 si4713_set_power_state(sdev
, POWER_DOWN
);
1503 if (client
->irq
> 0)
1504 free_irq(client
->irq
, sdev
);
1506 v4l2_device_unregister_subdev(sd
);
1507 v4l2_ctrl_handler_free(sd
->ctrl_handler
);
1508 regulator_bulk_free(ARRAY_SIZE(sdev
->supplies
), sdev
->supplies
);
1509 if (gpio_is_valid(sdev
->gpio_reset
))
1510 gpio_free(sdev
->gpio_reset
);
1516 /* si4713_i2c_driver - i2c driver interface */
1517 static const struct i2c_device_id si4713_id
[] = {
1521 MODULE_DEVICE_TABLE(i2c
, si4713_id
);
1523 static struct i2c_driver si4713_i2c_driver
= {
1527 .probe
= si4713_probe
,
1528 .remove
= si4713_remove
,
1529 .id_table
= si4713_id
,
1532 module_i2c_driver(si4713_i2c_driver
);