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/module.h>
31 #include <media/v4l2-device.h>
32 #include <media/v4l2-ioctl.h>
33 #include <media/v4l2-common.h>
37 /* module parameters */
39 module_param(debug, int, S_IRUGO | S_IWUSR);
40 MODULE_PARM_DESC(debug, "Debug level (0 - 2)");
42 MODULE_LICENSE("GPL");
43 MODULE_AUTHOR("Eduardo Valentin <eduardo.valentin@nokia.com>");
44 MODULE_DESCRIPTION("I2C driver for Si4713 FM Radio Transmitter");
45 MODULE_VERSION("0.0.1");
47 #define DEFAULT_RDS_PI 0x00
48 #define DEFAULT_RDS_PTY 0x00
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));
104 #define DBG_BUFFER(device, message, buffer, size) \
107 char str[(size)*5]; \
108 for (i = 0; i < size; i++) \
109 sprintf(str + i * 5, " 0x%02x", buffer[i]); \
110 v4l2_dbg(2, debug, device, "%s:%s\n", message, str); \
113 #define DBG_BUFFER(device, message, buffer, size)
117 * Values for limiter release time (sorted by second column)
121 static long limiter_times[] = {
145 * Values for audio compression release time (sorted by second column)
149 static unsigned long acomp_rtimes[] = {
158 * Values for preemphasis (sorted by second column)
162 static unsigned long preemphasis_values[] = {
163 FMPE_DISABLED, V4L2_PREEMPHASIS_DISABLED,
164 FMPE_EU, V4L2_PREEMPHASIS_50_uS,
165 FMPE_USA, V4L2_PREEMPHASIS_75_uS,
168 static int usecs_to_dev(unsigned long usecs, unsigned long const array[],
174 for (i = 0; i < size / 2; i++)
175 if (array[(i * 2) + 1] >= usecs) {
183 /* si4713_handler: IRQ handler, just complete work */
184 static irqreturn_t si4713_handler(int irq, void *dev)
186 struct si4713_device *sdev = dev;
188 v4l2_dbg(2, debug, &sdev->sd,
189 "%s: sending signal to completion work.\n", __func__);
190 complete(&sdev->work);
196 * si4713_send_command - sends a command to si4713 and waits its response
197 * @sdev: si4713_device structure for the device we are communicating
198 * @command: command id
199 * @args: command arguments we are sending (up to 7)
200 * @argn: actual size of @args
201 * @response: buffer to place the expected response from the device (up to 15)
202 * @respn: actual size of @response
203 * @usecs: amount of time to wait before reading the response (in usecs)
205 static int si4713_send_command(struct si4713_device *sdev, const u8 command,
206 const u8 args[], const int argn,
207 u8 response[], const int respn, const int usecs)
209 struct i2c_client *client = v4l2_get_subdevdata(&sdev->sd);
210 unsigned long until_jiffies;
211 u8 data1[MAX_ARGS + 1];
214 if (!client->adapter)
217 /* First send the command and its arguments */
219 memcpy(data1 + 1, args, argn);
220 DBG_BUFFER(&sdev->sd, "Parameters", data1, argn + 1);
222 err = i2c_master_send(client, data1, argn + 1);
223 if (err != argn + 1) {
224 v4l2_err(&sdev->sd, "Error while sending command 0x%02x\n",
226 return err < 0 ? err : -EIO;
229 until_jiffies = jiffies + usecs_to_jiffies(usecs) + 1;
231 /* Wait response from interrupt */
233 if (!wait_for_completion_timeout(&sdev->work,
234 usecs_to_jiffies(usecs) + 1))
236 "(%s) Device took too much time to answer.\n",
241 err = i2c_master_recv(client, response, respn);
244 "Error %d while reading response for command 0x%02x\n",
246 return err < 0 ? err : -EIO;
249 DBG_BUFFER(&sdev->sd, "Response", response, respn);
250 if (!check_command_failed(response[0]))
256 usleep_range(usecs, 1000);
258 usleep_range(1000, 2000);
259 } while (time_is_after_jiffies(until_jiffies));
265 * si4713_read_property - reads a si4713 property
266 * @sdev: si4713_device structure for the device we are communicating
267 * @prop: property identification number
268 * @pv: property value to be returned on success
270 static int si4713_read_property(struct si4713_device *sdev, u16 prop, u32 *pv)
273 u8 val[SI4713_GET_PROP_NRESP];
276 * .Second byte = property's MSB
277 * .Third byte = property's LSB
279 const u8 args[SI4713_GET_PROP_NARGS] = {
285 err = si4713_send_command(sdev, SI4713_CMD_GET_PROPERTY,
286 args, ARRAY_SIZE(args), val,
287 ARRAY_SIZE(val), DEFAULT_TIMEOUT);
292 *pv = compose_u16(val[2], val[3]);
294 v4l2_dbg(1, debug, &sdev->sd,
295 "%s: property=0x%02x value=0x%02x status=0x%02x\n",
296 __func__, prop, *pv, val[0]);
302 * si4713_write_property - modifies a si4713 property
303 * @sdev: si4713_device structure for the device we are communicating
304 * @prop: property identification number
305 * @val: new value for that property
307 static int si4713_write_property(struct si4713_device *sdev, u16 prop, u16 val)
310 u8 resp[SI4713_SET_PROP_NRESP];
313 * .Second byte = property's MSB
314 * .Third byte = property's LSB
315 * .Fourth byte = value's MSB
316 * .Fifth byte = value's LSB
318 const u8 args[SI4713_SET_PROP_NARGS] = {
326 rval = si4713_send_command(sdev, SI4713_CMD_SET_PROPERTY,
327 args, ARRAY_SIZE(args),
328 resp, ARRAY_SIZE(resp),
334 v4l2_dbg(1, debug, &sdev->sd,
335 "%s: property=0x%02x value=0x%02x status=0x%02x\n",
336 __func__, prop, val, resp[0]);
339 * As there is no command response for SET_PROPERTY,
340 * wait Tcomp time to finish before proceed, in order
341 * to have property properly set.
343 msleep(TIMEOUT_SET_PROPERTY);
349 * si4713_powerup - Powers the device up
350 * @sdev: si4713_device structure for the device we are communicating
352 static int si4713_powerup(struct si4713_device *sdev)
354 struct i2c_client *client = v4l2_get_subdevdata(&sdev->sd);
356 u8 resp[SI4713_PWUP_NRESP];
358 * .First byte = Enabled interrupts and boot function
359 * .Second byte = Input operation mode
361 u8 args[SI4713_PWUP_NARGS] = {
362 SI4713_PWUP_GPO2OEN | SI4713_PWUP_FUNC_TX,
363 SI4713_PWUP_OPMOD_ANALOG,
366 if (sdev->power_state)
369 if (sdev->supplies) {
370 err = regulator_bulk_enable(sdev->supplies, sdev->supply_data);
372 v4l2_err(&sdev->sd, "Failed to enable supplies: %d\n", err);
376 if (gpio_is_valid(sdev->gpio_reset)) {
378 gpio_set_value(sdev->gpio_reset, 1);
382 args[0] |= SI4713_PWUP_CTSIEN;
384 err = si4713_send_command(sdev, SI4713_CMD_POWER_UP,
385 args, ARRAY_SIZE(args),
386 resp, ARRAY_SIZE(resp),
390 v4l2_dbg(1, debug, &sdev->sd, "Powerup response: 0x%02x\n",
392 v4l2_dbg(1, debug, &sdev->sd, "Device in power up mode\n");
393 sdev->power_state = POWER_ON;
396 err = si4713_write_property(sdev, SI4713_GPO_IEN,
397 SI4713_STC_INT | SI4713_CTS);
400 if (gpio_is_valid(sdev->gpio_reset))
401 gpio_set_value(sdev->gpio_reset, 0);
402 if (sdev->supplies) {
403 err = regulator_bulk_disable(sdev->supplies, sdev->supply_data);
406 "Failed to disable supplies: %d\n", err);
413 * si4713_powerdown - Powers the device down
414 * @sdev: si4713_device structure for the device we are communicating
416 static int si4713_powerdown(struct si4713_device *sdev)
419 u8 resp[SI4713_PWDN_NRESP];
421 if (!sdev->power_state)
424 err = si4713_send_command(sdev, SI4713_CMD_POWER_DOWN,
426 resp, ARRAY_SIZE(resp),
430 v4l2_dbg(1, debug, &sdev->sd, "Power down response: 0x%02x\n",
432 v4l2_dbg(1, debug, &sdev->sd, "Device in reset mode\n");
433 if (gpio_is_valid(sdev->gpio_reset))
434 gpio_set_value(sdev->gpio_reset, 0);
435 if (sdev->supplies) {
436 err = regulator_bulk_disable(sdev->supplies,
440 "Failed to disable supplies: %d\n", err);
442 sdev->power_state = POWER_OFF;
449 * si4713_checkrev - Checks if we are treating a device with the correct rev.
450 * @sdev: si4713_device structure for the device we are communicating
452 static int si4713_checkrev(struct si4713_device *sdev)
454 struct i2c_client *client = v4l2_get_subdevdata(&sdev->sd);
456 u8 resp[SI4713_GETREV_NRESP];
458 rval = si4713_send_command(sdev, SI4713_CMD_GET_REV,
460 resp, ARRAY_SIZE(resp),
466 if (resp[1] == SI4713_PRODUCT_NUMBER) {
467 v4l2_info(&sdev->sd, "chip found @ 0x%02x (%s)\n",
468 client->addr << 1, client->adapter->name);
470 v4l2_err(&sdev->sd, "Invalid product number 0x%X\n", resp[1]);
477 * si4713_wait_stc - Waits STC interrupt and clears status bits. Useful
478 * for TX_TUNE_POWER, TX_TUNE_FREQ and TX_TUNE_MEAS
479 * @sdev: si4713_device structure for the device we are communicating
480 * @usecs: timeout to wait for STC interrupt signal
482 static int si4713_wait_stc(struct si4713_device *sdev, const int usecs)
484 struct i2c_client *client = v4l2_get_subdevdata(&sdev->sd);
485 u8 resp[SI4713_GET_STATUS_NRESP];
486 unsigned long start_jiffies = jiffies;
490 !wait_for_completion_timeout(&sdev->work, usecs_to_jiffies(usecs) + 1))
492 "(%s) Device took too much time to answer.\n", __func__);
495 /* Clear status bits */
496 err = si4713_send_command(sdev, SI4713_CMD_GET_INT_STATUS,
498 resp, ARRAY_SIZE(resp),
500 /* The USB device returns errors when it waits for the
501 * STC bit to be set. Hence polling */
503 v4l2_dbg(1, debug, &sdev->sd,
504 "%s: status bits: 0x%02x\n", __func__, resp[0]);
506 if (resp[0] & SI4713_STC_INT)
509 if (jiffies_to_usecs(jiffies - start_jiffies) > usecs)
510 return err < 0 ? err : -EIO;
511 /* We sleep here for 3-4 ms in order to avoid flooding the device
512 * with USB requests. The si4713 USB driver was developed
513 * by reverse engineering the Windows USB driver. The windows
514 * driver also has a ~2.5 ms delay between responses. */
515 usleep_range(3000, 4000);
520 * si4713_tx_tune_freq - Sets the state of the RF carrier and sets the tuning
521 * frequency between 76 and 108 MHz in 10 kHz units and
523 * @sdev: si4713_device structure for the device we are communicating
524 * @frequency: desired frequency (76 - 108 MHz, unit 10 KHz, step 50 kHz)
526 static int si4713_tx_tune_freq(struct si4713_device *sdev, u16 frequency)
529 u8 val[SI4713_TXFREQ_NRESP];
532 * .Second byte = frequency's MSB
533 * .Third byte = frequency's LSB
535 const u8 args[SI4713_TXFREQ_NARGS] = {
541 err = si4713_send_command(sdev, SI4713_CMD_TX_TUNE_FREQ,
542 args, ARRAY_SIZE(args), val,
543 ARRAY_SIZE(val), DEFAULT_TIMEOUT);
548 v4l2_dbg(1, debug, &sdev->sd,
549 "%s: frequency=0x%02x status=0x%02x\n", __func__,
552 err = si4713_wait_stc(sdev, TIMEOUT_TX_TUNE);
556 return compose_u16(args[1], args[2]);
560 * si4713_tx_tune_power - Sets the RF voltage level between 88 and 120 dBuV in
561 * 1 dB units. A value of 0x00 indicates off. The command
562 * also sets the antenna tuning capacitance. A value of 0
563 * indicates autotuning, and a value of 1 - 191 indicates
564 * a manual override, which results in a tuning
565 * capacitance of 0.25 pF x @antcap.
566 * @sdev: si4713_device structure for the device we are communicating
567 * @power: tuning power (88 - 120 dBuV, unit/step 1 dB)
568 * @antcap: value of antenna tuning capacitor (0 - 191)
570 static int si4713_tx_tune_power(struct si4713_device *sdev, u8 power,
574 u8 val[SI4713_TXPWR_NRESP];
578 * .Third byte = power
579 * .Fourth byte = antcap
581 u8 args[SI4713_TXPWR_NARGS] = {
588 /* Map power values 1-87 to MIN_POWER (88) */
589 if (power > 0 && power < SI4713_MIN_POWER)
590 args[2] = power = SI4713_MIN_POWER;
592 err = si4713_send_command(sdev, SI4713_CMD_TX_TUNE_POWER,
593 args, ARRAY_SIZE(args), val,
594 ARRAY_SIZE(val), DEFAULT_TIMEOUT);
599 v4l2_dbg(1, debug, &sdev->sd,
600 "%s: power=0x%02x antcap=0x%02x status=0x%02x\n",
601 __func__, power, antcap, val[0]);
603 return si4713_wait_stc(sdev, TIMEOUT_TX_TUNE_POWER);
607 * si4713_tx_tune_measure - Enters receive mode and measures the received noise
608 * level in units of dBuV on the selected frequency.
609 * The Frequency must be between 76 and 108 MHz in 10 kHz
610 * units and steps of 50 kHz. The command also sets the
611 * antenna tuning capacitance. A value of 0 means
612 * autotuning, and a value of 1 to 191 indicates manual
614 * @sdev: si4713_device structure for the device we are communicating
615 * @frequency: desired frequency (76 - 108 MHz, unit 10 KHz, step 50 kHz)
616 * @antcap: value of antenna tuning capacitor (0 - 191)
618 static int si4713_tx_tune_measure(struct si4713_device *sdev, u16 frequency,
622 u8 val[SI4713_TXMEA_NRESP];
625 * .Second byte = frequency's MSB
626 * .Third byte = frequency's LSB
627 * .Fourth byte = antcap
629 const u8 args[SI4713_TXMEA_NARGS] = {
636 sdev->tune_rnl = DEFAULT_TUNE_RNL;
638 if (antcap > SI4713_MAX_ANTCAP)
641 err = si4713_send_command(sdev, SI4713_CMD_TX_TUNE_MEASURE,
642 args, ARRAY_SIZE(args), val,
643 ARRAY_SIZE(val), DEFAULT_TIMEOUT);
648 v4l2_dbg(1, debug, &sdev->sd,
649 "%s: frequency=0x%02x antcap=0x%02x status=0x%02x\n",
650 __func__, frequency, antcap, val[0]);
652 return si4713_wait_stc(sdev, TIMEOUT_TX_TUNE);
656 * si4713_tx_tune_status- Returns the status of the tx_tune_freq, tx_tune_mea or
657 * tx_tune_power commands. This command return the current
658 * frequency, output voltage in dBuV, the antenna tunning
659 * capacitance value and the received noise level. The
660 * command also clears the stcint interrupt bit when the
661 * first bit of its arguments is high.
662 * @sdev: si4713_device structure for the device we are communicating
663 * @intack: 0x01 to clear the seek/tune complete interrupt status indicator.
664 * @frequency: returned frequency
665 * @power: returned power
666 * @antcap: returned antenna capacitance
667 * @noise: returned noise level
669 static int si4713_tx_tune_status(struct si4713_device *sdev, u8 intack,
670 u16 *frequency, u8 *power,
671 u8 *antcap, u8 *noise)
674 u8 val[SI4713_TXSTATUS_NRESP];
676 * .First byte = intack bit
678 const u8 args[SI4713_TXSTATUS_NARGS] = {
679 intack & SI4713_INTACK_MASK,
682 err = si4713_send_command(sdev, SI4713_CMD_TX_TUNE_STATUS,
683 args, ARRAY_SIZE(args), val,
684 ARRAY_SIZE(val), DEFAULT_TIMEOUT);
687 v4l2_dbg(1, debug, &sdev->sd,
688 "%s: status=0x%02x\n", __func__, val[0]);
689 *frequency = compose_u16(val[2], val[3]);
690 sdev->frequency = *frequency;
694 v4l2_dbg(1, debug, &sdev->sd, "%s: response: %d x 10 kHz "
695 "(power %d, antcap %d, rnl %d)\n", __func__,
696 *frequency, *power, *antcap, *noise);
703 * si4713_tx_rds_buff - Loads the RDS group buffer FIFO or circular buffer.
704 * @sdev: si4713_device structure for the device we are communicating
705 * @mode: the buffer operation mode.
709 * @cbleft: returns the number of available circular buffer blocks minus the
710 * number of used circular buffer blocks.
712 static int si4713_tx_rds_buff(struct si4713_device *sdev, u8 mode, u16 rdsb,
713 u16 rdsc, u16 rdsd, s8 *cbleft)
716 u8 val[SI4713_RDSBUFF_NRESP];
718 const u8 args[SI4713_RDSBUFF_NARGS] = {
719 mode & SI4713_RDSBUFF_MODE_MASK,
728 err = si4713_send_command(sdev, SI4713_CMD_TX_RDS_BUFF,
729 args, ARRAY_SIZE(args), val,
730 ARRAY_SIZE(val), DEFAULT_TIMEOUT);
733 v4l2_dbg(1, debug, &sdev->sd,
734 "%s: status=0x%02x\n", __func__, val[0]);
735 *cbleft = (s8)val[2] - val[3];
736 v4l2_dbg(1, debug, &sdev->sd, "%s: response: interrupts"
737 " 0x%02x cb avail: %d cb used %d fifo avail"
738 " %d fifo used %d\n", __func__, val[1],
739 val[2], val[3], val[4], val[5]);
746 * si4713_tx_rds_ps - Loads the program service buffer.
747 * @sdev: si4713_device structure for the device we are communicating
748 * @psid: program service id to be loaded.
749 * @pschar: assumed 4 size char array to be loaded into the program service
751 static int si4713_tx_rds_ps(struct si4713_device *sdev, u8 psid,
752 unsigned char *pschar)
755 u8 val[SI4713_RDSPS_NRESP];
757 const u8 args[SI4713_RDSPS_NARGS] = {
758 psid & SI4713_RDSPS_PSID_MASK,
765 err = si4713_send_command(sdev, SI4713_CMD_TX_RDS_PS,
766 args, ARRAY_SIZE(args), val,
767 ARRAY_SIZE(val), DEFAULT_TIMEOUT);
772 v4l2_dbg(1, debug, &sdev->sd, "%s: status=0x%02x\n", __func__, val[0]);
777 static int si4713_set_power_state(struct si4713_device *sdev, u8 value)
780 return si4713_powerup(sdev);
781 return si4713_powerdown(sdev);
784 static int si4713_set_mute(struct si4713_device *sdev, u16 mute)
788 mute = set_mute(mute);
790 if (sdev->power_state)
791 rval = si4713_write_property(sdev,
792 SI4713_TX_LINE_INPUT_MUTE, mute);
797 static int si4713_set_rds_ps_name(struct si4713_device *sdev, char *ps_name)
802 /* We want to clear the whole thing */
803 if (!strlen(ps_name))
804 memset(ps_name, 0, MAX_RDS_PS_NAME + 1);
806 if (sdev->power_state) {
807 /* Write the new ps name and clear the padding */
808 for (i = 0; i < MAX_RDS_PS_NAME; i += (RDS_BLOCK / 2)) {
809 rval = si4713_tx_rds_ps(sdev, (i / (RDS_BLOCK / 2)),
815 /* Setup the size to be sent */
817 len = strlen(ps_name) - 1;
821 rval = si4713_write_property(sdev,
822 SI4713_TX_RDS_PS_MESSAGE_COUNT,
823 rds_ps_nblocks(len));
827 rval = si4713_write_property(sdev,
828 SI4713_TX_RDS_PS_REPEAT_COUNT,
829 DEFAULT_RDS_PS_REPEAT_COUNT * 2);
837 static int si4713_set_rds_radio_text(struct si4713_device *sdev, const char *rt)
839 static const char cr[RDS_RADIOTEXT_BLK_SIZE] = { RDS_CARRIAGE_RETURN, 0 };
842 u8 b_index = 0, cr_inserted = 0;
845 if (!sdev->power_state)
848 rval = si4713_tx_rds_buff(sdev, RDS_BLOCK_CLEAR, 0, 0, 0, &left);
856 /* RDS spec says that if the last block isn't used,
857 * then apply a carriage return
859 if (t_index < (RDS_RADIOTEXT_INDEX_MAX * RDS_RADIOTEXT_BLK_SIZE)) {
860 for (i = 0; i < RDS_RADIOTEXT_BLK_SIZE; i++) {
861 if (!rt[t_index + i] ||
862 rt[t_index + i] == RDS_CARRIAGE_RETURN) {
870 rval = si4713_tx_rds_buff(sdev, RDS_BLOCK_LOAD,
871 compose_u16(RDS_RADIOTEXT_2A, b_index++),
872 compose_u16(rt[t_index], rt[t_index + 1]),
873 compose_u16(rt[t_index + 2], rt[t_index + 3]),
878 t_index += RDS_RADIOTEXT_BLK_SIZE;
888 * si4713_update_tune_status - update properties from tx_tune_status
889 * command. Must be called with sdev->mutex held.
890 * @sdev: si4713_device structure for the device we are communicating
892 static int si4713_update_tune_status(struct si4713_device *sdev)
896 u8 p = 0, a = 0, n = 0;
898 rval = si4713_tx_tune_status(sdev, 0x00, &f, &p, &a, &n);
903 /* TODO: check that power_level and antenna_capacitor really are not
904 changed by the hardware. If they are, then these controls should become
906 sdev->power_level = p;
907 sdev->antenna_capacitor = a;*/
914 static int si4713_choose_econtrol_action(struct si4713_device *sdev, u32 id,
915 s32 *bit, s32 *mask, u16 *property, int *mul,
916 unsigned long **table, int *size)
921 /* FM_TX class controls */
922 case V4L2_CID_RDS_TX_PI:
923 *property = SI4713_TX_RDS_PI;
926 case V4L2_CID_AUDIO_COMPRESSION_THRESHOLD:
927 *property = SI4713_TX_ACOMP_THRESHOLD;
930 case V4L2_CID_AUDIO_COMPRESSION_GAIN:
931 *property = SI4713_TX_ACOMP_GAIN;
934 case V4L2_CID_PILOT_TONE_FREQUENCY:
935 *property = SI4713_TX_PILOT_FREQUENCY;
938 case V4L2_CID_AUDIO_COMPRESSION_ATTACK_TIME:
939 *property = SI4713_TX_ACOMP_ATTACK_TIME;
940 *mul = ATTACK_TIME_UNIT;
942 case V4L2_CID_PILOT_TONE_DEVIATION:
943 *property = SI4713_TX_PILOT_DEVIATION;
946 case V4L2_CID_AUDIO_LIMITER_DEVIATION:
947 *property = SI4713_TX_AUDIO_DEVIATION;
950 case V4L2_CID_RDS_TX_DEVIATION:
951 *property = SI4713_TX_RDS_DEVIATION;
955 case V4L2_CID_RDS_TX_PTY:
956 *property = SI4713_TX_RDS_PS_MISC;
960 case V4L2_CID_AUDIO_LIMITER_ENABLED:
961 *property = SI4713_TX_ACOMP_ENABLE;
965 case V4L2_CID_AUDIO_COMPRESSION_ENABLED:
966 *property = SI4713_TX_ACOMP_ENABLE;
970 case V4L2_CID_PILOT_TONE_ENABLED:
971 *property = SI4713_TX_COMPONENT_ENABLE;
976 case V4L2_CID_AUDIO_LIMITER_RELEASE_TIME:
977 *property = SI4713_TX_LIMITER_RELEASE_TIME;
978 *table = limiter_times;
979 *size = ARRAY_SIZE(limiter_times);
981 case V4L2_CID_AUDIO_COMPRESSION_RELEASE_TIME:
982 *property = SI4713_TX_ACOMP_RELEASE_TIME;
983 *table = acomp_rtimes;
984 *size = ARRAY_SIZE(acomp_rtimes);
986 case V4L2_CID_TUNE_PREEMPHASIS:
987 *property = SI4713_TX_PREEMPHASIS;
988 *table = preemphasis_values;
989 *size = ARRAY_SIZE(preemphasis_values);
1000 static int si4713_s_frequency(struct v4l2_subdev *sd, const struct v4l2_frequency *f);
1001 static int si4713_s_modulator(struct v4l2_subdev *sd, const struct v4l2_modulator *);
1003 * si4713_setup - Sets the device up with current configuration.
1004 * @sdev: si4713_device structure for the device we are communicating
1006 static int si4713_setup(struct si4713_device *sdev)
1008 struct v4l2_frequency f;
1009 struct v4l2_modulator vm;
1012 /* Device procedure needs to set frequency first */
1014 f.frequency = sdev->frequency ? sdev->frequency : DEFAULT_FREQUENCY;
1015 f.frequency = si4713_to_v4l2(f.frequency);
1016 rval = si4713_s_frequency(&sdev->sd, &f);
1020 vm.txsubchans = V4L2_TUNER_SUB_STEREO;
1022 vm.txsubchans = V4L2_TUNER_SUB_MONO;
1023 if (sdev->rds_enabled)
1024 vm.txsubchans |= V4L2_TUNER_SUB_RDS;
1025 si4713_s_modulator(&sdev->sd, &vm);
1031 * si4713_initialize - Sets the device up with default configuration.
1032 * @sdev: si4713_device structure for the device we are communicating
1034 static int si4713_initialize(struct si4713_device *sdev)
1038 rval = si4713_set_power_state(sdev, POWER_ON);
1042 rval = si4713_checkrev(sdev);
1046 rval = si4713_set_power_state(sdev, POWER_OFF);
1050 sdev->frequency = DEFAULT_FREQUENCY;
1052 sdev->tune_rnl = DEFAULT_TUNE_RNL;
1056 /* si4713_s_ctrl - set the value of a control */
1057 static int si4713_s_ctrl(struct v4l2_ctrl *ctrl)
1059 struct si4713_device *sdev =
1060 container_of(ctrl->handler, struct si4713_device, ctrl_handler);
1062 s32 bit = 0, mask = 0;
1065 unsigned long *table = NULL;
1071 if (ctrl->id != V4L2_CID_AUDIO_MUTE)
1075 ret = si4713_set_mute(sdev, ctrl->val);
1077 ret = si4713_set_power_state(sdev, POWER_DOWN);
1080 ret = si4713_set_power_state(sdev, POWER_UP);
1082 ret = si4713_set_mute(sdev, ctrl->val);
1084 ret = si4713_setup(sdev);
1090 if (!sdev->power_state)
1093 for (c = 1; !ret && c < ctrl->ncontrols; c++) {
1094 ctrl = ctrl->cluster[c];
1096 if (!force && !ctrl->is_new)
1100 case V4L2_CID_RDS_TX_PS_NAME:
1101 ret = si4713_set_rds_ps_name(sdev, ctrl->string);
1104 case V4L2_CID_RDS_TX_RADIO_TEXT:
1105 ret = si4713_set_rds_radio_text(sdev, ctrl->string);
1108 case V4L2_CID_TUNE_ANTENNA_CAPACITOR:
1109 /* don't handle this control if we force setting all
1110 * controls since in that case it will be handled by
1111 * V4L2_CID_TUNE_POWER_LEVEL. */
1115 case V4L2_CID_TUNE_POWER_LEVEL:
1116 ret = si4713_tx_tune_power(sdev,
1117 sdev->tune_pwr_level->val, sdev->tune_ant_cap->val);
1119 /* Make sure we don't set this twice */
1120 sdev->tune_ant_cap->is_new = false;
1121 sdev->tune_pwr_level->is_new = false;
1126 ret = si4713_choose_econtrol_action(sdev, ctrl->id, &bit,
1127 &mask, &property, &mul, &table, &size);
1135 ret = usecs_to_dev(val, table, size);
1143 ret = si4713_read_property(sdev, property, &val);
1146 val = set_bits(val, ctrl->val, bit, mask);
1149 ret = si4713_write_property(sdev, property, val);
1161 /* si4713_ioctl - deal with private ioctls (only rnl for now) */
1162 static long si4713_ioctl(struct v4l2_subdev *sd, unsigned int cmd, void *arg)
1164 struct si4713_device *sdev = to_si4713_device(sd);
1165 struct si4713_rnl *rnl = arg;
1173 case SI4713_IOC_MEASURE_RNL:
1174 frequency = v4l2_to_si4713(rnl->frequency);
1176 if (sdev->power_state) {
1177 /* Set desired measurement frequency */
1178 rval = si4713_tx_tune_measure(sdev, frequency, 0);
1181 /* get results from tune status */
1182 rval = si4713_update_tune_status(sdev);
1186 rnl->rnl = sdev->tune_rnl;
1191 rval = -ENOIOCTLCMD;
1197 /* si4713_g_modulator - get modulator attributes */
1198 static int si4713_g_modulator(struct v4l2_subdev *sd, struct v4l2_modulator *vm)
1200 struct si4713_device *sdev = to_si4713_device(sd);
1209 strncpy(vm->name, "FM Modulator", 32);
1210 vm->capability = V4L2_TUNER_CAP_STEREO | V4L2_TUNER_CAP_LOW |
1211 V4L2_TUNER_CAP_RDS | V4L2_TUNER_CAP_RDS_CONTROLS;
1213 /* Report current frequency range limits */
1214 vm->rangelow = si4713_to_v4l2(FREQ_RANGE_LOW);
1215 vm->rangehigh = si4713_to_v4l2(FREQ_RANGE_HIGH);
1217 if (sdev->power_state) {
1220 rval = si4713_read_property(sdev, SI4713_TX_COMPONENT_ENABLE,
1225 sdev->stereo = get_status_bit(comp_en, 1, 1 << 1);
1228 /* Report current audio mode: mono or stereo */
1230 vm->txsubchans = V4L2_TUNER_SUB_STEREO;
1232 vm->txsubchans = V4L2_TUNER_SUB_MONO;
1234 /* Report rds feature status */
1235 if (sdev->rds_enabled)
1236 vm->txsubchans |= V4L2_TUNER_SUB_RDS;
1238 vm->txsubchans &= ~V4L2_TUNER_SUB_RDS;
1243 /* si4713_s_modulator - set modulator attributes */
1244 static int si4713_s_modulator(struct v4l2_subdev *sd, const struct v4l2_modulator *vm)
1246 struct si4713_device *sdev = to_si4713_device(sd);
1257 /* Set audio mode: mono or stereo */
1258 if (vm->txsubchans & V4L2_TUNER_SUB_STEREO)
1260 else if (vm->txsubchans & V4L2_TUNER_SUB_MONO)
1265 rds = !!(vm->txsubchans & V4L2_TUNER_SUB_RDS);
1267 if (sdev->power_state) {
1268 rval = si4713_read_property(sdev,
1269 SI4713_TX_COMPONENT_ENABLE, &p);
1273 p = set_bits(p, stereo, 1, 1 << 1);
1274 p = set_bits(p, rds, 2, 1 << 2);
1276 rval = si4713_write_property(sdev,
1277 SI4713_TX_COMPONENT_ENABLE, p);
1282 sdev->stereo = stereo;
1283 sdev->rds_enabled = rds;
1288 /* si4713_g_frequency - get tuner or modulator radio frequency */
1289 static int si4713_g_frequency(struct v4l2_subdev *sd, struct v4l2_frequency *f)
1291 struct si4713_device *sdev = to_si4713_device(sd);
1297 if (sdev->power_state) {
1301 rval = si4713_tx_tune_status(sdev, 0x00, &freq, &p, &a, &n);
1305 sdev->frequency = freq;
1308 f->frequency = si4713_to_v4l2(sdev->frequency);
1313 /* si4713_s_frequency - set tuner or modulator radio frequency */
1314 static int si4713_s_frequency(struct v4l2_subdev *sd, const struct v4l2_frequency *f)
1316 struct si4713_device *sdev = to_si4713_device(sd);
1318 u16 frequency = v4l2_to_si4713(f->frequency);
1323 /* Check frequency range */
1324 frequency = clamp_t(u16, frequency, FREQ_RANGE_LOW, FREQ_RANGE_HIGH);
1326 if (sdev->power_state) {
1327 rval = si4713_tx_tune_freq(sdev, frequency);
1333 sdev->frequency = frequency;
1338 static const struct v4l2_ctrl_ops si4713_ctrl_ops = {
1339 .s_ctrl = si4713_s_ctrl,
1342 static const struct v4l2_subdev_core_ops si4713_subdev_core_ops = {
1343 .ioctl = si4713_ioctl,
1346 static const struct v4l2_subdev_tuner_ops si4713_subdev_tuner_ops = {
1347 .g_frequency = si4713_g_frequency,
1348 .s_frequency = si4713_s_frequency,
1349 .g_modulator = si4713_g_modulator,
1350 .s_modulator = si4713_s_modulator,
1353 static const struct v4l2_subdev_ops si4713_subdev_ops = {
1354 .core = &si4713_subdev_core_ops,
1355 .tuner = &si4713_subdev_tuner_ops,
1359 * I2C driver interface
1361 /* si4713_probe - probe for the device */
1362 static int si4713_probe(struct i2c_client *client,
1363 const struct i2c_device_id *id)
1365 struct si4713_device *sdev;
1366 struct si4713_platform_data *pdata = client->dev.platform_data;
1367 struct v4l2_ctrl_handler *hdl;
1370 sdev = kzalloc(sizeof(*sdev), GFP_KERNEL);
1372 dev_err(&client->dev, "Failed to alloc video device.\n");
1377 sdev->gpio_reset = -1;
1378 if (pdata && gpio_is_valid(pdata->gpio_reset)) {
1379 rval = gpio_request(pdata->gpio_reset, "si4713 reset");
1381 dev_err(&client->dev,
1382 "Failed to request gpio: %d\n", rval);
1385 sdev->gpio_reset = pdata->gpio_reset;
1386 gpio_direction_output(sdev->gpio_reset, 0);
1387 sdev->supplies = pdata->supplies;
1390 for (i = 0; i < sdev->supplies; i++)
1391 sdev->supply_data[i].supply = pdata->supply_names[i];
1393 rval = regulator_bulk_get(&client->dev, sdev->supplies,
1396 dev_err(&client->dev, "Cannot get regulators: %d\n", rval);
1400 v4l2_i2c_subdev_init(&sdev->sd, client, &si4713_subdev_ops);
1402 init_completion(&sdev->work);
1404 hdl = &sdev->ctrl_handler;
1405 v4l2_ctrl_handler_init(hdl, 20);
1406 sdev->mute = v4l2_ctrl_new_std(hdl, &si4713_ctrl_ops,
1407 V4L2_CID_AUDIO_MUTE, 0, 1, 1, DEFAULT_MUTE);
1409 sdev->rds_pi = v4l2_ctrl_new_std(hdl, &si4713_ctrl_ops,
1410 V4L2_CID_RDS_TX_PI, 0, 0xffff, 1, DEFAULT_RDS_PI);
1411 sdev->rds_pty = v4l2_ctrl_new_std(hdl, &si4713_ctrl_ops,
1412 V4L2_CID_RDS_TX_PTY, 0, 31, 1, DEFAULT_RDS_PTY);
1413 sdev->rds_deviation = v4l2_ctrl_new_std(hdl, &si4713_ctrl_ops,
1414 V4L2_CID_RDS_TX_DEVIATION, 0, MAX_RDS_DEVIATION,
1415 10, DEFAULT_RDS_DEVIATION);
1417 * Report step as 8. From RDS spec, psname
1418 * should be 8. But there are receivers which scroll strings
1421 sdev->rds_ps_name = v4l2_ctrl_new_std(hdl, &si4713_ctrl_ops,
1422 V4L2_CID_RDS_TX_PS_NAME, 0, MAX_RDS_PS_NAME, 8, 0);
1424 * Report step as 32 (2A block). From RDS spec,
1425 * radio text should be 32 for 2A block. But there are receivers
1426 * which scroll strings sized as 32xN. Setting default to 32.
1428 sdev->rds_radio_text = v4l2_ctrl_new_std(hdl, &si4713_ctrl_ops,
1429 V4L2_CID_RDS_TX_RADIO_TEXT, 0, MAX_RDS_RADIO_TEXT, 32, 0);
1431 sdev->limiter_enabled = v4l2_ctrl_new_std(hdl, &si4713_ctrl_ops,
1432 V4L2_CID_AUDIO_LIMITER_ENABLED, 0, 1, 1, 1);
1433 sdev->limiter_release_time = v4l2_ctrl_new_std(hdl, &si4713_ctrl_ops,
1434 V4L2_CID_AUDIO_LIMITER_RELEASE_TIME, 250,
1435 MAX_LIMITER_RELEASE_TIME, 10, DEFAULT_LIMITER_RTIME);
1436 sdev->limiter_deviation = v4l2_ctrl_new_std(hdl, &si4713_ctrl_ops,
1437 V4L2_CID_AUDIO_LIMITER_DEVIATION, 0,
1438 MAX_LIMITER_DEVIATION, 10, DEFAULT_LIMITER_DEV);
1440 sdev->compression_enabled = v4l2_ctrl_new_std(hdl, &si4713_ctrl_ops,
1441 V4L2_CID_AUDIO_COMPRESSION_ENABLED, 0, 1, 1, 1);
1442 sdev->compression_gain = v4l2_ctrl_new_std(hdl, &si4713_ctrl_ops,
1443 V4L2_CID_AUDIO_COMPRESSION_GAIN, 0, MAX_ACOMP_GAIN, 1,
1444 DEFAULT_ACOMP_GAIN);
1445 sdev->compression_threshold = v4l2_ctrl_new_std(hdl, &si4713_ctrl_ops,
1446 V4L2_CID_AUDIO_COMPRESSION_THRESHOLD,
1447 MIN_ACOMP_THRESHOLD, MAX_ACOMP_THRESHOLD, 1,
1448 DEFAULT_ACOMP_THRESHOLD);
1449 sdev->compression_attack_time = v4l2_ctrl_new_std(hdl, &si4713_ctrl_ops,
1450 V4L2_CID_AUDIO_COMPRESSION_ATTACK_TIME, 0,
1451 MAX_ACOMP_ATTACK_TIME, 500, DEFAULT_ACOMP_ATIME);
1452 sdev->compression_release_time = v4l2_ctrl_new_std(hdl, &si4713_ctrl_ops,
1453 V4L2_CID_AUDIO_COMPRESSION_RELEASE_TIME, 100000,
1454 MAX_ACOMP_RELEASE_TIME, 100000, DEFAULT_ACOMP_RTIME);
1456 sdev->pilot_tone_enabled = v4l2_ctrl_new_std(hdl, &si4713_ctrl_ops,
1457 V4L2_CID_PILOT_TONE_ENABLED, 0, 1, 1, 1);
1458 sdev->pilot_tone_deviation = v4l2_ctrl_new_std(hdl, &si4713_ctrl_ops,
1459 V4L2_CID_PILOT_TONE_DEVIATION, 0, MAX_PILOT_DEVIATION,
1460 10, DEFAULT_PILOT_DEVIATION);
1461 sdev->pilot_tone_freq = v4l2_ctrl_new_std(hdl, &si4713_ctrl_ops,
1462 V4L2_CID_PILOT_TONE_FREQUENCY, 0, MAX_PILOT_FREQUENCY,
1463 1, DEFAULT_PILOT_FREQUENCY);
1465 sdev->tune_preemphasis = v4l2_ctrl_new_std_menu(hdl, &si4713_ctrl_ops,
1466 V4L2_CID_TUNE_PREEMPHASIS,
1467 V4L2_PREEMPHASIS_75_uS, 0, V4L2_PREEMPHASIS_50_uS);
1468 sdev->tune_pwr_level = v4l2_ctrl_new_std(hdl, &si4713_ctrl_ops,
1469 V4L2_CID_TUNE_POWER_LEVEL, 0, SI4713_MAX_POWER,
1470 1, DEFAULT_POWER_LEVEL);
1471 sdev->tune_ant_cap = v4l2_ctrl_new_std(hdl, &si4713_ctrl_ops,
1472 V4L2_CID_TUNE_ANTENNA_CAPACITOR, 0, SI4713_MAX_ANTCAP,
1479 v4l2_ctrl_cluster(20, &sdev->mute);
1480 sdev->sd.ctrl_handler = hdl;
1483 rval = request_irq(client->irq,
1484 si4713_handler, IRQF_TRIGGER_FALLING,
1485 client->name, sdev);
1487 v4l2_err(&sdev->sd, "Could not request IRQ\n");
1490 v4l2_dbg(1, debug, &sdev->sd, "IRQ requested.\n");
1492 v4l2_warn(&sdev->sd, "IRQ not configured. Using timeouts.\n");
1495 rval = si4713_initialize(sdev);
1497 v4l2_err(&sdev->sd, "Failed to probe device information.\n");
1505 free_irq(client->irq, sdev);
1507 v4l2_ctrl_handler_free(hdl);
1509 regulator_bulk_free(sdev->supplies, sdev->supply_data);
1511 if (gpio_is_valid(sdev->gpio_reset))
1512 gpio_free(sdev->gpio_reset);
1519 /* si4713_remove - remove the device */
1520 static int si4713_remove(struct i2c_client *client)
1522 struct v4l2_subdev *sd = i2c_get_clientdata(client);
1523 struct si4713_device *sdev = to_si4713_device(sd);
1525 if (sdev->power_state)
1526 si4713_set_power_state(sdev, POWER_DOWN);
1528 if (client->irq > 0)
1529 free_irq(client->irq, sdev);
1531 v4l2_device_unregister_subdev(sd);
1532 v4l2_ctrl_handler_free(sd->ctrl_handler);
1533 regulator_bulk_free(sdev->supplies, sdev->supply_data);
1534 if (gpio_is_valid(sdev->gpio_reset))
1535 gpio_free(sdev->gpio_reset);
1541 /* si4713_i2c_driver - i2c driver interface */
1542 static const struct i2c_device_id si4713_id[] = {
1546 MODULE_DEVICE_TABLE(i2c, si4713_id);
1548 static struct i2c_driver si4713_i2c_driver = {
1552 .probe = si4713_probe,
1553 .remove = si4713_remove,
1554 .id_table = si4713_id,
1557 module_i2c_driver(si4713_i2c_driver);