Linux 3.3-rc6
[linux/fpc-iii.git] / drivers / media / radio / si4713-i2c.c
blob27aba936fb2bcb95c1fb9faedb7832557abeab42
1 /*
2 * drivers/media/radio/si4713-i2c.c
4 * Silicon Labs Si4713 FM Radio Transmitter I2C commands.
6 * Copyright (c) 2009 Nokia Corporation
7 * Contact: Eduardo Valentin <eduardo.valentin@nokia.com>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 #include <linux/mutex.h>
25 #include <linux/completion.h>
26 #include <linux/delay.h>
27 #include <linux/interrupt.h>
28 #include <linux/i2c.h>
29 #include <linux/slab.h>
30 #include <linux/gpio.h>
31 #include <linux/regulator/consumer.h>
32 #include <linux/module.h>
33 #include <media/v4l2-device.h>
34 #include <media/v4l2-ioctl.h>
35 #include <media/v4l2-common.h>
37 #include "si4713-i2c.h"
39 /* module parameters */
40 static int debug;
41 module_param(debug, int, S_IRUGO | S_IWUSR);
42 MODULE_PARM_DESC(debug, "Debug level (0 - 2)");
44 MODULE_LICENSE("GPL");
45 MODULE_AUTHOR("Eduardo Valentin <eduardo.valentin@nokia.com>");
46 MODULE_DESCRIPTION("I2C driver for Si4713 FM Radio Transmitter");
47 MODULE_VERSION("0.0.1");
49 static const char *si4713_supply_names[SI4713_NUM_SUPPLIES] = {
50 "vio",
51 "vdd",
54 #define DEFAULT_RDS_PI 0x00
55 #define DEFAULT_RDS_PTY 0x00
56 #define DEFAULT_RDS_PS_NAME ""
57 #define DEFAULT_RDS_RADIO_TEXT DEFAULT_RDS_PS_NAME
58 #define DEFAULT_RDS_DEVIATION 0x00C8
59 #define DEFAULT_RDS_PS_REPEAT_COUNT 0x0003
60 #define DEFAULT_LIMITER_RTIME 0x1392
61 #define DEFAULT_LIMITER_DEV 0x102CA
62 #define DEFAULT_PILOT_FREQUENCY 0x4A38
63 #define DEFAULT_PILOT_DEVIATION 0x1A5E
64 #define DEFAULT_ACOMP_ATIME 0x0000
65 #define DEFAULT_ACOMP_RTIME 0xF4240L
66 #define DEFAULT_ACOMP_GAIN 0x0F
67 #define DEFAULT_ACOMP_THRESHOLD (-0x28)
68 #define DEFAULT_MUTE 0x01
69 #define DEFAULT_POWER_LEVEL 88
70 #define DEFAULT_FREQUENCY 8800
71 #define DEFAULT_PREEMPHASIS FMPE_EU
72 #define DEFAULT_TUNE_RNL 0xFF
74 #define to_si4713_device(sd) container_of(sd, struct si4713_device, sd)
76 /* frequency domain transformation (using times 10 to avoid floats) */
77 #define FREQDEV_UNIT 100000
78 #define FREQV4L2_MULTI 625
79 #define si4713_to_v4l2(f) ((f * FREQDEV_UNIT) / FREQV4L2_MULTI)
80 #define v4l2_to_si4713(f) ((f * FREQV4L2_MULTI) / FREQDEV_UNIT)
81 #define FREQ_RANGE_LOW 7600
82 #define FREQ_RANGE_HIGH 10800
84 #define MAX_ARGS 7
86 #define RDS_BLOCK 8
87 #define RDS_BLOCK_CLEAR 0x03
88 #define RDS_BLOCK_LOAD 0x04
89 #define RDS_RADIOTEXT_2A 0x20
90 #define RDS_RADIOTEXT_BLK_SIZE 4
91 #define RDS_RADIOTEXT_INDEX_MAX 0x0F
92 #define RDS_CARRIAGE_RETURN 0x0D
94 #define rds_ps_nblocks(len) ((len / RDS_BLOCK) + (len % RDS_BLOCK ? 1 : 0))
96 #define get_status_bit(p, b, m) (((p) & (m)) >> (b))
97 #define set_bits(p, v, b, m) (((p) & ~(m)) | ((v) << (b)))
99 #define ATTACK_TIME_UNIT 500
101 #define POWER_OFF 0x00
102 #define POWER_ON 0x01
104 #define msb(x) ((u8)((u16) x >> 8))
105 #define lsb(x) ((u8)((u16) x & 0x00FF))
106 #define compose_u16(msb, lsb) (((u16)msb << 8) | lsb)
107 #define check_command_failed(status) (!(status & SI4713_CTS) || \
108 (status & SI4713_ERR))
109 /* mute definition */
110 #define set_mute(p) ((p & 1) | ((p & 1) << 1));
111 #define get_mute(p) (p & 0x01)
113 #ifdef DEBUG
114 #define DBG_BUFFER(device, message, buffer, size) \
116 int i; \
117 char str[(size)*5]; \
118 for (i = 0; i < size; i++) \
119 sprintf(str + i * 5, " 0x%02x", buffer[i]); \
120 v4l2_dbg(2, debug, device, "%s:%s\n", message, str); \
122 #else
123 #define DBG_BUFFER(device, message, buffer, size)
124 #endif
127 * Values for limiter release time (sorted by second column)
128 * device release
129 * value time (us)
131 static long limiter_times[] = {
132 2000, 250,
133 1000, 500,
134 510, 1000,
135 255, 2000,
136 170, 3000,
137 127, 4020,
138 102, 5010,
139 85, 6020,
140 73, 7010,
141 64, 7990,
142 57, 8970,
143 51, 10030,
144 25, 20470,
145 17, 30110,
146 13, 39380,
147 10, 51190,
148 8, 63690,
149 7, 73140,
150 6, 85330,
151 5, 102390,
155 * Values for audio compression release time (sorted by second column)
156 * device release
157 * value time (us)
159 static unsigned long acomp_rtimes[] = {
160 0, 100000,
161 1, 200000,
162 2, 350000,
163 3, 525000,
164 4, 1000000,
168 * Values for preemphasis (sorted by second column)
169 * device preemphasis
170 * value value (v4l2)
172 static unsigned long preemphasis_values[] = {
173 FMPE_DISABLED, V4L2_PREEMPHASIS_DISABLED,
174 FMPE_EU, V4L2_PREEMPHASIS_50_uS,
175 FMPE_USA, V4L2_PREEMPHASIS_75_uS,
178 static int usecs_to_dev(unsigned long usecs, unsigned long const array[],
179 int size)
181 int i;
182 int rval = -EINVAL;
184 for (i = 0; i < size / 2; i++)
185 if (array[(i * 2) + 1] >= usecs) {
186 rval = array[i * 2];
187 break;
190 return rval;
193 static unsigned long dev_to_usecs(int value, unsigned long const array[],
194 int size)
196 int i;
197 int rval = -EINVAL;
199 for (i = 0; i < size / 2; i++)
200 if (array[i * 2] == value) {
201 rval = array[(i * 2) + 1];
202 break;
205 return rval;
208 /* si4713_handler: IRQ handler, just complete work */
209 static irqreturn_t si4713_handler(int irq, void *dev)
211 struct si4713_device *sdev = dev;
213 v4l2_dbg(2, debug, &sdev->sd,
214 "%s: sending signal to completion work.\n", __func__);
215 complete(&sdev->work);
217 return IRQ_HANDLED;
221 * si4713_send_command - sends a command to si4713 and waits its response
222 * @sdev: si4713_device structure for the device we are communicating
223 * @command: command id
224 * @args: command arguments we are sending (up to 7)
225 * @argn: actual size of @args
226 * @response: buffer to place the expected response from the device (up to 15)
227 * @respn: actual size of @response
228 * @usecs: amount of time to wait before reading the response (in usecs)
230 static int si4713_send_command(struct si4713_device *sdev, const u8 command,
231 const u8 args[], const int argn,
232 u8 response[], const int respn, const int usecs)
234 struct i2c_client *client = v4l2_get_subdevdata(&sdev->sd);
235 u8 data1[MAX_ARGS + 1];
236 int err;
238 if (!client->adapter)
239 return -ENODEV;
241 /* First send the command and its arguments */
242 data1[0] = command;
243 memcpy(data1 + 1, args, argn);
244 DBG_BUFFER(&sdev->sd, "Parameters", data1, argn + 1);
246 err = i2c_master_send(client, data1, argn + 1);
247 if (err != argn + 1) {
248 v4l2_err(&sdev->sd, "Error while sending command 0x%02x\n",
249 command);
250 return (err > 0) ? -EIO : err;
253 /* Wait response from interrupt */
254 if (!wait_for_completion_timeout(&sdev->work,
255 usecs_to_jiffies(usecs) + 1))
256 v4l2_warn(&sdev->sd,
257 "(%s) Device took too much time to answer.\n",
258 __func__);
260 /* Then get the response */
261 err = i2c_master_recv(client, response, respn);
262 if (err != respn) {
263 v4l2_err(&sdev->sd,
264 "Error while reading response for command 0x%02x\n",
265 command);
266 return (err > 0) ? -EIO : err;
269 DBG_BUFFER(&sdev->sd, "Response", response, respn);
270 if (check_command_failed(response[0]))
271 return -EBUSY;
273 return 0;
277 * si4713_read_property - reads a si4713 property
278 * @sdev: si4713_device structure for the device we are communicating
279 * @prop: property identification number
280 * @pv: property value to be returned on success
282 static int si4713_read_property(struct si4713_device *sdev, u16 prop, u32 *pv)
284 int err;
285 u8 val[SI4713_GET_PROP_NRESP];
287 * .First byte = 0
288 * .Second byte = property's MSB
289 * .Third byte = property's LSB
291 const u8 args[SI4713_GET_PROP_NARGS] = {
292 0x00,
293 msb(prop),
294 lsb(prop),
297 err = si4713_send_command(sdev, SI4713_CMD_GET_PROPERTY,
298 args, ARRAY_SIZE(args), val,
299 ARRAY_SIZE(val), DEFAULT_TIMEOUT);
301 if (err < 0)
302 return err;
304 *pv = compose_u16(val[2], val[3]);
306 v4l2_dbg(1, debug, &sdev->sd,
307 "%s: property=0x%02x value=0x%02x status=0x%02x\n",
308 __func__, prop, *pv, val[0]);
310 return err;
314 * si4713_write_property - modifies a si4713 property
315 * @sdev: si4713_device structure for the device we are communicating
316 * @prop: property identification number
317 * @val: new value for that property
319 static int si4713_write_property(struct si4713_device *sdev, u16 prop, u16 val)
321 int rval;
322 u8 resp[SI4713_SET_PROP_NRESP];
324 * .First byte = 0
325 * .Second byte = property's MSB
326 * .Third byte = property's LSB
327 * .Fourth byte = value's MSB
328 * .Fifth byte = value's LSB
330 const u8 args[SI4713_SET_PROP_NARGS] = {
331 0x00,
332 msb(prop),
333 lsb(prop),
334 msb(val),
335 lsb(val),
338 rval = si4713_send_command(sdev, SI4713_CMD_SET_PROPERTY,
339 args, ARRAY_SIZE(args),
340 resp, ARRAY_SIZE(resp),
341 DEFAULT_TIMEOUT);
343 if (rval < 0)
344 return rval;
346 v4l2_dbg(1, debug, &sdev->sd,
347 "%s: property=0x%02x value=0x%02x status=0x%02x\n",
348 __func__, prop, val, resp[0]);
351 * As there is no command response for SET_PROPERTY,
352 * wait Tcomp time to finish before proceed, in order
353 * to have property properly set.
355 msleep(TIMEOUT_SET_PROPERTY);
357 return rval;
361 * si4713_powerup - Powers the device up
362 * @sdev: si4713_device structure for the device we are communicating
364 static int si4713_powerup(struct si4713_device *sdev)
366 int err;
367 u8 resp[SI4713_PWUP_NRESP];
369 * .First byte = Enabled interrupts and boot function
370 * .Second byte = Input operation mode
372 const u8 args[SI4713_PWUP_NARGS] = {
373 SI4713_PWUP_CTSIEN | SI4713_PWUP_GPO2OEN | SI4713_PWUP_FUNC_TX,
374 SI4713_PWUP_OPMOD_ANALOG,
377 if (sdev->power_state)
378 return 0;
380 err = regulator_bulk_enable(ARRAY_SIZE(sdev->supplies),
381 sdev->supplies);
382 if (err) {
383 v4l2_err(&sdev->sd, "Failed to enable supplies: %d\n", err);
384 return err;
386 if (gpio_is_valid(sdev->gpio_reset)) {
387 udelay(50);
388 gpio_set_value(sdev->gpio_reset, 1);
391 err = si4713_send_command(sdev, SI4713_CMD_POWER_UP,
392 args, ARRAY_SIZE(args),
393 resp, ARRAY_SIZE(resp),
394 TIMEOUT_POWER_UP);
396 if (!err) {
397 v4l2_dbg(1, debug, &sdev->sd, "Powerup response: 0x%02x\n",
398 resp[0]);
399 v4l2_dbg(1, debug, &sdev->sd, "Device in power up mode\n");
400 sdev->power_state = POWER_ON;
402 err = si4713_write_property(sdev, SI4713_GPO_IEN,
403 SI4713_STC_INT | SI4713_CTS);
404 } else {
405 if (gpio_is_valid(sdev->gpio_reset))
406 gpio_set_value(sdev->gpio_reset, 0);
407 err = regulator_bulk_disable(ARRAY_SIZE(sdev->supplies),
408 sdev->supplies);
409 if (err)
410 v4l2_err(&sdev->sd,
411 "Failed to disable supplies: %d\n", err);
414 return err;
418 * si4713_powerdown - Powers the device down
419 * @sdev: si4713_device structure for the device we are communicating
421 static int si4713_powerdown(struct si4713_device *sdev)
423 int err;
424 u8 resp[SI4713_PWDN_NRESP];
426 if (!sdev->power_state)
427 return 0;
429 err = si4713_send_command(sdev, SI4713_CMD_POWER_DOWN,
430 NULL, 0,
431 resp, ARRAY_SIZE(resp),
432 DEFAULT_TIMEOUT);
434 if (!err) {
435 v4l2_dbg(1, debug, &sdev->sd, "Power down response: 0x%02x\n",
436 resp[0]);
437 v4l2_dbg(1, debug, &sdev->sd, "Device in reset mode\n");
438 if (gpio_is_valid(sdev->gpio_reset))
439 gpio_set_value(sdev->gpio_reset, 0);
440 err = regulator_bulk_disable(ARRAY_SIZE(sdev->supplies),
441 sdev->supplies);
442 if (err)
443 v4l2_err(&sdev->sd,
444 "Failed to disable supplies: %d\n", err);
445 sdev->power_state = POWER_OFF;
448 return err;
452 * si4713_checkrev - Checks if we are treating a device with the correct rev.
453 * @sdev: si4713_device structure for the device we are communicating
455 static int si4713_checkrev(struct si4713_device *sdev)
457 struct i2c_client *client = v4l2_get_subdevdata(&sdev->sd);
458 int rval;
459 u8 resp[SI4713_GETREV_NRESP];
461 mutex_lock(&sdev->mutex);
463 rval = si4713_send_command(sdev, SI4713_CMD_GET_REV,
464 NULL, 0,
465 resp, ARRAY_SIZE(resp),
466 DEFAULT_TIMEOUT);
468 if (rval < 0)
469 goto unlock;
471 if (resp[1] == SI4713_PRODUCT_NUMBER) {
472 v4l2_info(&sdev->sd, "chip found @ 0x%02x (%s)\n",
473 client->addr << 1, client->adapter->name);
474 } else {
475 v4l2_err(&sdev->sd, "Invalid product number\n");
476 rval = -EINVAL;
479 unlock:
480 mutex_unlock(&sdev->mutex);
481 return rval;
485 * si4713_wait_stc - Waits STC interrupt and clears status bits. Useful
486 * for TX_TUNE_POWER, TX_TUNE_FREQ and TX_TUNE_MEAS
487 * @sdev: si4713_device structure for the device we are communicating
488 * @usecs: timeout to wait for STC interrupt signal
490 static int si4713_wait_stc(struct si4713_device *sdev, const int usecs)
492 int err;
493 u8 resp[SI4713_GET_STATUS_NRESP];
495 /* Wait response from STC interrupt */
496 if (!wait_for_completion_timeout(&sdev->work,
497 usecs_to_jiffies(usecs) + 1))
498 v4l2_warn(&sdev->sd,
499 "%s: device took too much time to answer (%d usec).\n",
500 __func__, usecs);
502 /* Clear status bits */
503 err = si4713_send_command(sdev, SI4713_CMD_GET_INT_STATUS,
504 NULL, 0,
505 resp, ARRAY_SIZE(resp),
506 DEFAULT_TIMEOUT);
508 if (err < 0)
509 goto exit;
511 v4l2_dbg(1, debug, &sdev->sd,
512 "%s: status bits: 0x%02x\n", __func__, resp[0]);
514 if (!(resp[0] & SI4713_STC_INT))
515 err = -EIO;
517 exit:
518 return err;
522 * si4713_tx_tune_freq - Sets the state of the RF carrier and sets the tuning
523 * frequency between 76 and 108 MHz in 10 kHz units and
524 * steps of 50 kHz.
525 * @sdev: si4713_device structure for the device we are communicating
526 * @frequency: desired frequency (76 - 108 MHz, unit 10 KHz, step 50 kHz)
528 static int si4713_tx_tune_freq(struct si4713_device *sdev, u16 frequency)
530 int err;
531 u8 val[SI4713_TXFREQ_NRESP];
533 * .First byte = 0
534 * .Second byte = frequency's MSB
535 * .Third byte = frequency's LSB
537 const u8 args[SI4713_TXFREQ_NARGS] = {
538 0x00,
539 msb(frequency),
540 lsb(frequency),
543 err = si4713_send_command(sdev, SI4713_CMD_TX_TUNE_FREQ,
544 args, ARRAY_SIZE(args), val,
545 ARRAY_SIZE(val), DEFAULT_TIMEOUT);
547 if (err < 0)
548 return err;
550 v4l2_dbg(1, debug, &sdev->sd,
551 "%s: frequency=0x%02x status=0x%02x\n", __func__,
552 frequency, val[0]);
554 err = si4713_wait_stc(sdev, TIMEOUT_TX_TUNE);
555 if (err < 0)
556 return err;
558 return compose_u16(args[1], args[2]);
562 * si4713_tx_tune_power - Sets the RF voltage level between 88 and 115 dBuV in
563 * 1 dB units. A value of 0x00 indicates off. The command
564 * also sets the antenna tuning capacitance. A value of 0
565 * indicates autotuning, and a value of 1 - 191 indicates
566 * a manual override, which results in a tuning
567 * capacitance of 0.25 pF x @antcap.
568 * @sdev: si4713_device structure for the device we are communicating
569 * @power: tuning power (88 - 115 dBuV, unit/step 1 dB)
570 * @antcap: value of antenna tuning capacitor (0 - 191)
572 static int si4713_tx_tune_power(struct si4713_device *sdev, u8 power,
573 u8 antcap)
575 int err;
576 u8 val[SI4713_TXPWR_NRESP];
578 * .First byte = 0
579 * .Second byte = 0
580 * .Third byte = power
581 * .Fourth byte = antcap
583 const u8 args[SI4713_TXPWR_NARGS] = {
584 0x00,
585 0x00,
586 power,
587 antcap,
590 if (((power > 0) && (power < SI4713_MIN_POWER)) ||
591 power > SI4713_MAX_POWER || antcap > SI4713_MAX_ANTCAP)
592 return -EDOM;
594 err = si4713_send_command(sdev, SI4713_CMD_TX_TUNE_POWER,
595 args, ARRAY_SIZE(args), val,
596 ARRAY_SIZE(val), DEFAULT_TIMEOUT);
598 if (err < 0)
599 return err;
601 v4l2_dbg(1, debug, &sdev->sd,
602 "%s: power=0x%02x antcap=0x%02x status=0x%02x\n",
603 __func__, power, antcap, val[0]);
605 return si4713_wait_stc(sdev, TIMEOUT_TX_TUNE_POWER);
609 * si4713_tx_tune_measure - Enters receive mode and measures the received noise
610 * level in units of dBuV on the selected frequency.
611 * The Frequency must be between 76 and 108 MHz in 10 kHz
612 * units and steps of 50 kHz. The command also sets the
613 * antenna tuning capacitance. A value of 0 means
614 * autotuning, and a value of 1 to 191 indicates manual
615 * override.
616 * @sdev: si4713_device structure for the device we are communicating
617 * @frequency: desired frequency (76 - 108 MHz, unit 10 KHz, step 50 kHz)
618 * @antcap: value of antenna tuning capacitor (0 - 191)
620 static int si4713_tx_tune_measure(struct si4713_device *sdev, u16 frequency,
621 u8 antcap)
623 int err;
624 u8 val[SI4713_TXMEA_NRESP];
626 * .First byte = 0
627 * .Second byte = frequency's MSB
628 * .Third byte = frequency's LSB
629 * .Fourth byte = antcap
631 const u8 args[SI4713_TXMEA_NARGS] = {
632 0x00,
633 msb(frequency),
634 lsb(frequency),
635 antcap,
638 sdev->tune_rnl = DEFAULT_TUNE_RNL;
640 if (antcap > SI4713_MAX_ANTCAP)
641 return -EDOM;
643 err = si4713_send_command(sdev, SI4713_CMD_TX_TUNE_MEASURE,
644 args, ARRAY_SIZE(args), val,
645 ARRAY_SIZE(val), DEFAULT_TIMEOUT);
647 if (err < 0)
648 return err;
650 v4l2_dbg(1, debug, &sdev->sd,
651 "%s: frequency=0x%02x antcap=0x%02x status=0x%02x\n",
652 __func__, frequency, antcap, val[0]);
654 return si4713_wait_stc(sdev, TIMEOUT_TX_TUNE);
658 * si4713_tx_tune_status- Returns the status of the tx_tune_freq, tx_tune_mea or
659 * tx_tune_power commands. This command return the current
660 * frequency, output voltage in dBuV, the antenna tunning
661 * capacitance value and the received noise level. The
662 * command also clears the stcint interrupt bit when the
663 * first bit of its arguments is high.
664 * @sdev: si4713_device structure for the device we are communicating
665 * @intack: 0x01 to clear the seek/tune complete interrupt status indicator.
666 * @frequency: returned frequency
667 * @power: returned power
668 * @antcap: returned antenna capacitance
669 * @noise: returned noise level
671 static int si4713_tx_tune_status(struct si4713_device *sdev, u8 intack,
672 u16 *frequency, u8 *power,
673 u8 *antcap, u8 *noise)
675 int err;
676 u8 val[SI4713_TXSTATUS_NRESP];
678 * .First byte = intack bit
680 const u8 args[SI4713_TXSTATUS_NARGS] = {
681 intack & SI4713_INTACK_MASK,
684 err = si4713_send_command(sdev, SI4713_CMD_TX_TUNE_STATUS,
685 args, ARRAY_SIZE(args), val,
686 ARRAY_SIZE(val), DEFAULT_TIMEOUT);
688 if (!err) {
689 v4l2_dbg(1, debug, &sdev->sd,
690 "%s: status=0x%02x\n", __func__, val[0]);
691 *frequency = compose_u16(val[2], val[3]);
692 sdev->frequency = *frequency;
693 *power = val[5];
694 *antcap = val[6];
695 *noise = val[7];
696 v4l2_dbg(1, debug, &sdev->sd, "%s: response: %d x 10 kHz "
697 "(power %d, antcap %d, rnl %d)\n", __func__,
698 *frequency, *power, *antcap, *noise);
701 return err;
705 * si4713_tx_rds_buff - Loads the RDS group buffer FIFO or circular buffer.
706 * @sdev: si4713_device structure for the device we are communicating
707 * @mode: the buffer operation mode.
708 * @rdsb: RDS Block B
709 * @rdsc: RDS Block C
710 * @rdsd: RDS Block D
711 * @cbleft: returns the number of available circular buffer blocks minus the
712 * number of used circular buffer blocks.
714 static int si4713_tx_rds_buff(struct si4713_device *sdev, u8 mode, u16 rdsb,
715 u16 rdsc, u16 rdsd, s8 *cbleft)
717 int err;
718 u8 val[SI4713_RDSBUFF_NRESP];
720 const u8 args[SI4713_RDSBUFF_NARGS] = {
721 mode & SI4713_RDSBUFF_MODE_MASK,
722 msb(rdsb),
723 lsb(rdsb),
724 msb(rdsc),
725 lsb(rdsc),
726 msb(rdsd),
727 lsb(rdsd),
730 err = si4713_send_command(sdev, SI4713_CMD_TX_RDS_BUFF,
731 args, ARRAY_SIZE(args), val,
732 ARRAY_SIZE(val), DEFAULT_TIMEOUT);
734 if (!err) {
735 v4l2_dbg(1, debug, &sdev->sd,
736 "%s: status=0x%02x\n", __func__, val[0]);
737 *cbleft = (s8)val[2] - val[3];
738 v4l2_dbg(1, debug, &sdev->sd, "%s: response: interrupts"
739 " 0x%02x cb avail: %d cb used %d fifo avail"
740 " %d fifo used %d\n", __func__, val[1],
741 val[2], val[3], val[4], val[5]);
744 return err;
748 * si4713_tx_rds_ps - Loads the program service buffer.
749 * @sdev: si4713_device structure for the device we are communicating
750 * @psid: program service id to be loaded.
751 * @pschar: assumed 4 size char array to be loaded into the program service
753 static int si4713_tx_rds_ps(struct si4713_device *sdev, u8 psid,
754 unsigned char *pschar)
756 int err;
757 u8 val[SI4713_RDSPS_NRESP];
759 const u8 args[SI4713_RDSPS_NARGS] = {
760 psid & SI4713_RDSPS_PSID_MASK,
761 pschar[0],
762 pschar[1],
763 pschar[2],
764 pschar[3],
767 err = si4713_send_command(sdev, SI4713_CMD_TX_RDS_PS,
768 args, ARRAY_SIZE(args), val,
769 ARRAY_SIZE(val), DEFAULT_TIMEOUT);
771 if (err < 0)
772 return err;
774 v4l2_dbg(1, debug, &sdev->sd, "%s: status=0x%02x\n", __func__, val[0]);
776 return err;
779 static int si4713_set_power_state(struct si4713_device *sdev, u8 value)
781 int rval;
783 mutex_lock(&sdev->mutex);
785 if (value)
786 rval = si4713_powerup(sdev);
787 else
788 rval = si4713_powerdown(sdev);
790 mutex_unlock(&sdev->mutex);
791 return rval;
794 static int si4713_set_mute(struct si4713_device *sdev, u16 mute)
796 int rval = 0;
798 mute = set_mute(mute);
800 mutex_lock(&sdev->mutex);
802 if (sdev->power_state)
803 rval = si4713_write_property(sdev,
804 SI4713_TX_LINE_INPUT_MUTE, mute);
806 if (rval >= 0)
807 sdev->mute = get_mute(mute);
809 mutex_unlock(&sdev->mutex);
811 return rval;
814 static int si4713_set_rds_ps_name(struct si4713_device *sdev, char *ps_name)
816 int rval = 0, i;
817 u8 len = 0;
819 /* We want to clear the whole thing */
820 if (!strlen(ps_name))
821 memset(ps_name, 0, MAX_RDS_PS_NAME + 1);
823 mutex_lock(&sdev->mutex);
825 if (sdev->power_state) {
826 /* Write the new ps name and clear the padding */
827 for (i = 0; i < MAX_RDS_PS_NAME; i += (RDS_BLOCK / 2)) {
828 rval = si4713_tx_rds_ps(sdev, (i / (RDS_BLOCK / 2)),
829 ps_name + i);
830 if (rval < 0)
831 goto unlock;
834 /* Setup the size to be sent */
835 if (strlen(ps_name))
836 len = strlen(ps_name) - 1;
837 else
838 len = 1;
840 rval = si4713_write_property(sdev,
841 SI4713_TX_RDS_PS_MESSAGE_COUNT,
842 rds_ps_nblocks(len));
843 if (rval < 0)
844 goto unlock;
846 rval = si4713_write_property(sdev,
847 SI4713_TX_RDS_PS_REPEAT_COUNT,
848 DEFAULT_RDS_PS_REPEAT_COUNT * 2);
849 if (rval < 0)
850 goto unlock;
853 strncpy(sdev->rds_info.ps_name, ps_name, MAX_RDS_PS_NAME);
855 unlock:
856 mutex_unlock(&sdev->mutex);
857 return rval;
860 static int si4713_set_rds_radio_text(struct si4713_device *sdev, char *rt)
862 int rval = 0, i;
863 u16 t_index = 0;
864 u8 b_index = 0, cr_inserted = 0;
865 s8 left;
867 mutex_lock(&sdev->mutex);
869 if (!sdev->power_state)
870 goto copy;
872 rval = si4713_tx_rds_buff(sdev, RDS_BLOCK_CLEAR, 0, 0, 0, &left);
873 if (rval < 0)
874 goto unlock;
876 if (!strlen(rt))
877 goto copy;
879 do {
880 /* RDS spec says that if the last block isn't used,
881 * then apply a carriage return
883 if (t_index < (RDS_RADIOTEXT_INDEX_MAX *
884 RDS_RADIOTEXT_BLK_SIZE)) {
885 for (i = 0; i < RDS_RADIOTEXT_BLK_SIZE; i++) {
886 if (!rt[t_index + i] || rt[t_index + i] ==
887 RDS_CARRIAGE_RETURN) {
888 rt[t_index + i] = RDS_CARRIAGE_RETURN;
889 cr_inserted = 1;
890 break;
895 rval = si4713_tx_rds_buff(sdev, RDS_BLOCK_LOAD,
896 compose_u16(RDS_RADIOTEXT_2A, b_index++),
897 compose_u16(rt[t_index], rt[t_index + 1]),
898 compose_u16(rt[t_index + 2], rt[t_index + 3]),
899 &left);
900 if (rval < 0)
901 goto unlock;
903 t_index += RDS_RADIOTEXT_BLK_SIZE;
905 if (cr_inserted)
906 break;
907 } while (left > 0);
909 copy:
910 strncpy(sdev->rds_info.radio_text, rt, MAX_RDS_RADIO_TEXT);
912 unlock:
913 mutex_unlock(&sdev->mutex);
914 return rval;
917 static int si4713_choose_econtrol_action(struct si4713_device *sdev, u32 id,
918 u32 **shadow, s32 *bit, s32 *mask, u16 *property, int *mul,
919 unsigned long **table, int *size)
921 s32 rval = 0;
923 switch (id) {
924 /* FM_TX class controls */
925 case V4L2_CID_RDS_TX_PI:
926 *property = SI4713_TX_RDS_PI;
927 *mul = 1;
928 *shadow = &sdev->rds_info.pi;
929 break;
930 case V4L2_CID_AUDIO_COMPRESSION_THRESHOLD:
931 *property = SI4713_TX_ACOMP_THRESHOLD;
932 *mul = 1;
933 *shadow = &sdev->acomp_info.threshold;
934 break;
935 case V4L2_CID_AUDIO_COMPRESSION_GAIN:
936 *property = SI4713_TX_ACOMP_GAIN;
937 *mul = 1;
938 *shadow = &sdev->acomp_info.gain;
939 break;
940 case V4L2_CID_PILOT_TONE_FREQUENCY:
941 *property = SI4713_TX_PILOT_FREQUENCY;
942 *mul = 1;
943 *shadow = &sdev->pilot_info.frequency;
944 break;
945 case V4L2_CID_AUDIO_COMPRESSION_ATTACK_TIME:
946 *property = SI4713_TX_ACOMP_ATTACK_TIME;
947 *mul = ATTACK_TIME_UNIT;
948 *shadow = &sdev->acomp_info.attack_time;
949 break;
950 case V4L2_CID_PILOT_TONE_DEVIATION:
951 *property = SI4713_TX_PILOT_DEVIATION;
952 *mul = 10;
953 *shadow = &sdev->pilot_info.deviation;
954 break;
955 case V4L2_CID_AUDIO_LIMITER_DEVIATION:
956 *property = SI4713_TX_AUDIO_DEVIATION;
957 *mul = 10;
958 *shadow = &sdev->limiter_info.deviation;
959 break;
960 case V4L2_CID_RDS_TX_DEVIATION:
961 *property = SI4713_TX_RDS_DEVIATION;
962 *mul = 1;
963 *shadow = &sdev->rds_info.deviation;
964 break;
966 case V4L2_CID_RDS_TX_PTY:
967 *property = SI4713_TX_RDS_PS_MISC;
968 *bit = 5;
969 *mask = 0x1F << 5;
970 *shadow = &sdev->rds_info.pty;
971 break;
972 case V4L2_CID_AUDIO_LIMITER_ENABLED:
973 *property = SI4713_TX_ACOMP_ENABLE;
974 *bit = 1;
975 *mask = 1 << 1;
976 *shadow = &sdev->limiter_info.enabled;
977 break;
978 case V4L2_CID_AUDIO_COMPRESSION_ENABLED:
979 *property = SI4713_TX_ACOMP_ENABLE;
980 *bit = 0;
981 *mask = 1 << 0;
982 *shadow = &sdev->acomp_info.enabled;
983 break;
984 case V4L2_CID_PILOT_TONE_ENABLED:
985 *property = SI4713_TX_COMPONENT_ENABLE;
986 *bit = 0;
987 *mask = 1 << 0;
988 *shadow = &sdev->pilot_info.enabled;
989 break;
991 case V4L2_CID_AUDIO_LIMITER_RELEASE_TIME:
992 *property = SI4713_TX_LIMITER_RELEASE_TIME;
993 *table = limiter_times;
994 *size = ARRAY_SIZE(limiter_times);
995 *shadow = &sdev->limiter_info.release_time;
996 break;
997 case V4L2_CID_AUDIO_COMPRESSION_RELEASE_TIME:
998 *property = SI4713_TX_ACOMP_RELEASE_TIME;
999 *table = acomp_rtimes;
1000 *size = ARRAY_SIZE(acomp_rtimes);
1001 *shadow = &sdev->acomp_info.release_time;
1002 break;
1003 case V4L2_CID_TUNE_PREEMPHASIS:
1004 *property = SI4713_TX_PREEMPHASIS;
1005 *table = preemphasis_values;
1006 *size = ARRAY_SIZE(preemphasis_values);
1007 *shadow = &sdev->preemphasis;
1008 break;
1010 default:
1011 rval = -EINVAL;
1014 return rval;
1017 static int si4713_queryctrl(struct v4l2_subdev *sd, struct v4l2_queryctrl *qc);
1019 /* write string property */
1020 static int si4713_write_econtrol_string(struct si4713_device *sdev,
1021 struct v4l2_ext_control *control)
1023 struct v4l2_queryctrl vqc;
1024 int len;
1025 s32 rval = 0;
1027 vqc.id = control->id;
1028 rval = si4713_queryctrl(&sdev->sd, &vqc);
1029 if (rval < 0)
1030 goto exit;
1032 switch (control->id) {
1033 case V4L2_CID_RDS_TX_PS_NAME: {
1034 char ps_name[MAX_RDS_PS_NAME + 1];
1036 len = control->size - 1;
1037 if (len < 0 || len > MAX_RDS_PS_NAME) {
1038 rval = -ERANGE;
1039 goto exit;
1041 rval = copy_from_user(ps_name, control->string, len);
1042 if (rval) {
1043 rval = -EFAULT;
1044 goto exit;
1046 ps_name[len] = '\0';
1048 if (strlen(ps_name) % vqc.step) {
1049 rval = -ERANGE;
1050 goto exit;
1053 rval = si4713_set_rds_ps_name(sdev, ps_name);
1055 break;
1057 case V4L2_CID_RDS_TX_RADIO_TEXT: {
1058 char radio_text[MAX_RDS_RADIO_TEXT + 1];
1060 len = control->size - 1;
1061 if (len < 0 || len > MAX_RDS_RADIO_TEXT) {
1062 rval = -ERANGE;
1063 goto exit;
1065 rval = copy_from_user(radio_text, control->string, len);
1066 if (rval) {
1067 rval = -EFAULT;
1068 goto exit;
1070 radio_text[len] = '\0';
1072 if (strlen(radio_text) % vqc.step) {
1073 rval = -ERANGE;
1074 goto exit;
1077 rval = si4713_set_rds_radio_text(sdev, radio_text);
1079 break;
1081 default:
1082 rval = -EINVAL;
1083 break;
1086 exit:
1087 return rval;
1090 static int validate_range(struct v4l2_subdev *sd,
1091 struct v4l2_ext_control *control)
1093 struct v4l2_queryctrl vqc;
1094 int rval;
1096 vqc.id = control->id;
1097 rval = si4713_queryctrl(sd, &vqc);
1098 if (rval < 0)
1099 goto exit;
1101 if (control->value < vqc.minimum || control->value > vqc.maximum)
1102 rval = -ERANGE;
1104 exit:
1105 return rval;
1108 /* properties which use tx_tune_power*/
1109 static int si4713_write_econtrol_tune(struct si4713_device *sdev,
1110 struct v4l2_ext_control *control)
1112 s32 rval = 0;
1113 u8 power, antcap;
1115 rval = validate_range(&sdev->sd, control);
1116 if (rval < 0)
1117 goto exit;
1119 mutex_lock(&sdev->mutex);
1121 switch (control->id) {
1122 case V4L2_CID_TUNE_POWER_LEVEL:
1123 power = control->value;
1124 antcap = sdev->antenna_capacitor;
1125 break;
1126 case V4L2_CID_TUNE_ANTENNA_CAPACITOR:
1127 power = sdev->power_level;
1128 antcap = control->value;
1129 break;
1130 default:
1131 rval = -EINVAL;
1132 goto unlock;
1135 if (sdev->power_state)
1136 rval = si4713_tx_tune_power(sdev, power, antcap);
1138 if (rval == 0) {
1139 sdev->power_level = power;
1140 sdev->antenna_capacitor = antcap;
1143 unlock:
1144 mutex_unlock(&sdev->mutex);
1145 exit:
1146 return rval;
1149 static int si4713_write_econtrol_integers(struct si4713_device *sdev,
1150 struct v4l2_ext_control *control)
1152 s32 rval;
1153 u32 *shadow = NULL, val = 0;
1154 s32 bit = 0, mask = 0;
1155 u16 property = 0;
1156 int mul = 0;
1157 unsigned long *table = NULL;
1158 int size = 0;
1160 rval = validate_range(&sdev->sd, control);
1161 if (rval < 0)
1162 goto exit;
1164 rval = si4713_choose_econtrol_action(sdev, control->id, &shadow, &bit,
1165 &mask, &property, &mul, &table, &size);
1166 if (rval < 0)
1167 goto exit;
1169 val = control->value;
1170 if (mul) {
1171 val = control->value / mul;
1172 } else if (table) {
1173 rval = usecs_to_dev(control->value, table, size);
1174 if (rval < 0)
1175 goto exit;
1176 val = rval;
1177 rval = 0;
1180 mutex_lock(&sdev->mutex);
1182 if (sdev->power_state) {
1183 if (mask) {
1184 rval = si4713_read_property(sdev, property, &val);
1185 if (rval < 0)
1186 goto unlock;
1187 val = set_bits(val, control->value, bit, mask);
1190 rval = si4713_write_property(sdev, property, val);
1191 if (rval < 0)
1192 goto unlock;
1193 if (mask)
1194 val = control->value;
1197 if (mul) {
1198 *shadow = val * mul;
1199 } else if (table) {
1200 rval = dev_to_usecs(val, table, size);
1201 if (rval < 0)
1202 goto unlock;
1203 *shadow = rval;
1204 rval = 0;
1205 } else {
1206 *shadow = val;
1209 unlock:
1210 mutex_unlock(&sdev->mutex);
1211 exit:
1212 return rval;
1215 static int si4713_s_frequency(struct v4l2_subdev *sd, struct v4l2_frequency *f);
1216 static int si4713_s_modulator(struct v4l2_subdev *sd, struct v4l2_modulator *);
1218 * si4713_setup - Sets the device up with current configuration.
1219 * @sdev: si4713_device structure for the device we are communicating
1221 static int si4713_setup(struct si4713_device *sdev)
1223 struct v4l2_ext_control ctrl;
1224 struct v4l2_frequency f;
1225 struct v4l2_modulator vm;
1226 struct si4713_device *tmp;
1227 int rval = 0;
1229 tmp = kmalloc(sizeof(*tmp), GFP_KERNEL);
1230 if (!tmp)
1231 return -ENOMEM;
1233 /* Get a local copy to avoid race */
1234 mutex_lock(&sdev->mutex);
1235 memcpy(tmp, sdev, sizeof(*sdev));
1236 mutex_unlock(&sdev->mutex);
1238 ctrl.id = V4L2_CID_RDS_TX_PI;
1239 ctrl.value = tmp->rds_info.pi;
1240 rval |= si4713_write_econtrol_integers(sdev, &ctrl);
1242 ctrl.id = V4L2_CID_AUDIO_COMPRESSION_THRESHOLD;
1243 ctrl.value = tmp->acomp_info.threshold;
1244 rval |= si4713_write_econtrol_integers(sdev, &ctrl);
1246 ctrl.id = V4L2_CID_AUDIO_COMPRESSION_GAIN;
1247 ctrl.value = tmp->acomp_info.gain;
1248 rval |= si4713_write_econtrol_integers(sdev, &ctrl);
1250 ctrl.id = V4L2_CID_PILOT_TONE_FREQUENCY;
1251 ctrl.value = tmp->pilot_info.frequency;
1252 rval |= si4713_write_econtrol_integers(sdev, &ctrl);
1254 ctrl.id = V4L2_CID_AUDIO_COMPRESSION_ATTACK_TIME;
1255 ctrl.value = tmp->acomp_info.attack_time;
1256 rval |= si4713_write_econtrol_integers(sdev, &ctrl);
1258 ctrl.id = V4L2_CID_PILOT_TONE_DEVIATION;
1259 ctrl.value = tmp->pilot_info.deviation;
1260 rval |= si4713_write_econtrol_integers(sdev, &ctrl);
1262 ctrl.id = V4L2_CID_AUDIO_LIMITER_DEVIATION;
1263 ctrl.value = tmp->limiter_info.deviation;
1264 rval |= si4713_write_econtrol_integers(sdev, &ctrl);
1266 ctrl.id = V4L2_CID_RDS_TX_DEVIATION;
1267 ctrl.value = tmp->rds_info.deviation;
1268 rval |= si4713_write_econtrol_integers(sdev, &ctrl);
1270 ctrl.id = V4L2_CID_RDS_TX_PTY;
1271 ctrl.value = tmp->rds_info.pty;
1272 rval |= si4713_write_econtrol_integers(sdev, &ctrl);
1274 ctrl.id = V4L2_CID_AUDIO_LIMITER_ENABLED;
1275 ctrl.value = tmp->limiter_info.enabled;
1276 rval |= si4713_write_econtrol_integers(sdev, &ctrl);
1278 ctrl.id = V4L2_CID_AUDIO_COMPRESSION_ENABLED;
1279 ctrl.value = tmp->acomp_info.enabled;
1280 rval |= si4713_write_econtrol_integers(sdev, &ctrl);
1282 ctrl.id = V4L2_CID_PILOT_TONE_ENABLED;
1283 ctrl.value = tmp->pilot_info.enabled;
1284 rval |= si4713_write_econtrol_integers(sdev, &ctrl);
1286 ctrl.id = V4L2_CID_AUDIO_LIMITER_RELEASE_TIME;
1287 ctrl.value = tmp->limiter_info.release_time;
1288 rval |= si4713_write_econtrol_integers(sdev, &ctrl);
1290 ctrl.id = V4L2_CID_AUDIO_COMPRESSION_RELEASE_TIME;
1291 ctrl.value = tmp->acomp_info.release_time;
1292 rval |= si4713_write_econtrol_integers(sdev, &ctrl);
1294 ctrl.id = V4L2_CID_TUNE_PREEMPHASIS;
1295 ctrl.value = tmp->preemphasis;
1296 rval |= si4713_write_econtrol_integers(sdev, &ctrl);
1298 ctrl.id = V4L2_CID_RDS_TX_PS_NAME;
1299 rval |= si4713_set_rds_ps_name(sdev, tmp->rds_info.ps_name);
1301 ctrl.id = V4L2_CID_RDS_TX_RADIO_TEXT;
1302 rval |= si4713_set_rds_radio_text(sdev, tmp->rds_info.radio_text);
1304 /* Device procedure needs to set frequency first */
1305 f.frequency = tmp->frequency ? tmp->frequency : DEFAULT_FREQUENCY;
1306 f.frequency = si4713_to_v4l2(f.frequency);
1307 rval |= si4713_s_frequency(&sdev->sd, &f);
1309 ctrl.id = V4L2_CID_TUNE_POWER_LEVEL;
1310 ctrl.value = tmp->power_level;
1311 rval |= si4713_write_econtrol_tune(sdev, &ctrl);
1313 ctrl.id = V4L2_CID_TUNE_ANTENNA_CAPACITOR;
1314 ctrl.value = tmp->antenna_capacitor;
1315 rval |= si4713_write_econtrol_tune(sdev, &ctrl);
1317 vm.index = 0;
1318 if (tmp->stereo)
1319 vm.txsubchans = V4L2_TUNER_SUB_STEREO;
1320 else
1321 vm.txsubchans = V4L2_TUNER_SUB_MONO;
1322 if (tmp->rds_info.enabled)
1323 vm.txsubchans |= V4L2_TUNER_SUB_RDS;
1324 si4713_s_modulator(&sdev->sd, &vm);
1326 kfree(tmp);
1328 return rval;
1332 * si4713_initialize - Sets the device up with default configuration.
1333 * @sdev: si4713_device structure for the device we are communicating
1335 static int si4713_initialize(struct si4713_device *sdev)
1337 int rval;
1339 rval = si4713_set_power_state(sdev, POWER_ON);
1340 if (rval < 0)
1341 goto exit;
1343 rval = si4713_checkrev(sdev);
1344 if (rval < 0)
1345 goto exit;
1347 rval = si4713_set_power_state(sdev, POWER_OFF);
1348 if (rval < 0)
1349 goto exit;
1351 mutex_lock(&sdev->mutex);
1353 sdev->rds_info.pi = DEFAULT_RDS_PI;
1354 sdev->rds_info.pty = DEFAULT_RDS_PTY;
1355 sdev->rds_info.deviation = DEFAULT_RDS_DEVIATION;
1356 strlcpy(sdev->rds_info.ps_name, DEFAULT_RDS_PS_NAME, MAX_RDS_PS_NAME);
1357 strlcpy(sdev->rds_info.radio_text, DEFAULT_RDS_RADIO_TEXT,
1358 MAX_RDS_RADIO_TEXT);
1359 sdev->rds_info.enabled = 1;
1361 sdev->limiter_info.release_time = DEFAULT_LIMITER_RTIME;
1362 sdev->limiter_info.deviation = DEFAULT_LIMITER_DEV;
1363 sdev->limiter_info.enabled = 1;
1365 sdev->pilot_info.deviation = DEFAULT_PILOT_DEVIATION;
1366 sdev->pilot_info.frequency = DEFAULT_PILOT_FREQUENCY;
1367 sdev->pilot_info.enabled = 1;
1369 sdev->acomp_info.release_time = DEFAULT_ACOMP_RTIME;
1370 sdev->acomp_info.attack_time = DEFAULT_ACOMP_ATIME;
1371 sdev->acomp_info.threshold = DEFAULT_ACOMP_THRESHOLD;
1372 sdev->acomp_info.gain = DEFAULT_ACOMP_GAIN;
1373 sdev->acomp_info.enabled = 1;
1375 sdev->frequency = DEFAULT_FREQUENCY;
1376 sdev->preemphasis = DEFAULT_PREEMPHASIS;
1377 sdev->mute = DEFAULT_MUTE;
1378 sdev->power_level = DEFAULT_POWER_LEVEL;
1379 sdev->antenna_capacitor = 0;
1380 sdev->stereo = 1;
1381 sdev->tune_rnl = DEFAULT_TUNE_RNL;
1383 mutex_unlock(&sdev->mutex);
1385 exit:
1386 return rval;
1389 /* read string property */
1390 static int si4713_read_econtrol_string(struct si4713_device *sdev,
1391 struct v4l2_ext_control *control)
1393 s32 rval = 0;
1395 switch (control->id) {
1396 case V4L2_CID_RDS_TX_PS_NAME:
1397 if (strlen(sdev->rds_info.ps_name) + 1 > control->size) {
1398 control->size = MAX_RDS_PS_NAME + 1;
1399 rval = -ENOSPC;
1400 goto exit;
1402 rval = copy_to_user(control->string, sdev->rds_info.ps_name,
1403 strlen(sdev->rds_info.ps_name) + 1);
1404 if (rval)
1405 rval = -EFAULT;
1406 break;
1408 case V4L2_CID_RDS_TX_RADIO_TEXT:
1409 if (strlen(sdev->rds_info.radio_text) + 1 > control->size) {
1410 control->size = MAX_RDS_RADIO_TEXT + 1;
1411 rval = -ENOSPC;
1412 goto exit;
1414 rval = copy_to_user(control->string, sdev->rds_info.radio_text,
1415 strlen(sdev->rds_info.radio_text) + 1);
1416 if (rval)
1417 rval = -EFAULT;
1418 break;
1420 default:
1421 rval = -EINVAL;
1422 break;
1425 exit:
1426 return rval;
1430 * si4713_update_tune_status - update properties from tx_tune_status
1431 * command. Must be called with sdev->mutex held.
1432 * @sdev: si4713_device structure for the device we are communicating
1434 static int si4713_update_tune_status(struct si4713_device *sdev)
1436 int rval;
1437 u16 f = 0;
1438 u8 p = 0, a = 0, n = 0;
1440 rval = si4713_tx_tune_status(sdev, 0x00, &f, &p, &a, &n);
1442 if (rval < 0)
1443 goto exit;
1445 sdev->power_level = p;
1446 sdev->antenna_capacitor = a;
1447 sdev->tune_rnl = n;
1449 exit:
1450 return rval;
1453 /* properties which use tx_tune_status */
1454 static int si4713_read_econtrol_tune(struct si4713_device *sdev,
1455 struct v4l2_ext_control *control)
1457 s32 rval = 0;
1459 mutex_lock(&sdev->mutex);
1461 if (sdev->power_state) {
1462 rval = si4713_update_tune_status(sdev);
1463 if (rval < 0)
1464 goto unlock;
1467 switch (control->id) {
1468 case V4L2_CID_TUNE_POWER_LEVEL:
1469 control->value = sdev->power_level;
1470 break;
1471 case V4L2_CID_TUNE_ANTENNA_CAPACITOR:
1472 control->value = sdev->antenna_capacitor;
1473 break;
1474 default:
1475 rval = -EINVAL;
1478 unlock:
1479 mutex_unlock(&sdev->mutex);
1480 return rval;
1483 static int si4713_read_econtrol_integers(struct si4713_device *sdev,
1484 struct v4l2_ext_control *control)
1486 s32 rval;
1487 u32 *shadow = NULL, val = 0;
1488 s32 bit = 0, mask = 0;
1489 u16 property = 0;
1490 int mul = 0;
1491 unsigned long *table = NULL;
1492 int size = 0;
1494 rval = si4713_choose_econtrol_action(sdev, control->id, &shadow, &bit,
1495 &mask, &property, &mul, &table, &size);
1496 if (rval < 0)
1497 goto exit;
1499 mutex_lock(&sdev->mutex);
1501 if (sdev->power_state) {
1502 rval = si4713_read_property(sdev, property, &val);
1503 if (rval < 0)
1504 goto unlock;
1506 /* Keep negative values for threshold */
1507 if (control->id == V4L2_CID_AUDIO_COMPRESSION_THRESHOLD)
1508 *shadow = (s16)val;
1509 else if (mask)
1510 *shadow = get_status_bit(val, bit, mask);
1511 else if (mul)
1512 *shadow = val * mul;
1513 else
1514 *shadow = dev_to_usecs(val, table, size);
1517 control->value = *shadow;
1519 unlock:
1520 mutex_unlock(&sdev->mutex);
1521 exit:
1522 return rval;
1526 * Video4Linux Subdev Interface
1528 /* si4713_s_ext_ctrls - set extended controls value */
1529 static int si4713_s_ext_ctrls(struct v4l2_subdev *sd,
1530 struct v4l2_ext_controls *ctrls)
1532 struct si4713_device *sdev = to_si4713_device(sd);
1533 int i;
1535 if (ctrls->ctrl_class != V4L2_CTRL_CLASS_FM_TX)
1536 return -EINVAL;
1538 for (i = 0; i < ctrls->count; i++) {
1539 int err;
1541 switch ((ctrls->controls + i)->id) {
1542 case V4L2_CID_RDS_TX_PS_NAME:
1543 case V4L2_CID_RDS_TX_RADIO_TEXT:
1544 err = si4713_write_econtrol_string(sdev,
1545 ctrls->controls + i);
1546 break;
1547 case V4L2_CID_TUNE_ANTENNA_CAPACITOR:
1548 case V4L2_CID_TUNE_POWER_LEVEL:
1549 err = si4713_write_econtrol_tune(sdev,
1550 ctrls->controls + i);
1551 break;
1552 default:
1553 err = si4713_write_econtrol_integers(sdev,
1554 ctrls->controls + i);
1557 if (err < 0) {
1558 ctrls->error_idx = i;
1559 return err;
1563 return 0;
1566 /* si4713_g_ext_ctrls - get extended controls value */
1567 static int si4713_g_ext_ctrls(struct v4l2_subdev *sd,
1568 struct v4l2_ext_controls *ctrls)
1570 struct si4713_device *sdev = to_si4713_device(sd);
1571 int i;
1573 if (ctrls->ctrl_class != V4L2_CTRL_CLASS_FM_TX)
1574 return -EINVAL;
1576 for (i = 0; i < ctrls->count; i++) {
1577 int err;
1579 switch ((ctrls->controls + i)->id) {
1580 case V4L2_CID_RDS_TX_PS_NAME:
1581 case V4L2_CID_RDS_TX_RADIO_TEXT:
1582 err = si4713_read_econtrol_string(sdev,
1583 ctrls->controls + i);
1584 break;
1585 case V4L2_CID_TUNE_ANTENNA_CAPACITOR:
1586 case V4L2_CID_TUNE_POWER_LEVEL:
1587 err = si4713_read_econtrol_tune(sdev,
1588 ctrls->controls + i);
1589 break;
1590 default:
1591 err = si4713_read_econtrol_integers(sdev,
1592 ctrls->controls + i);
1595 if (err < 0) {
1596 ctrls->error_idx = i;
1597 return err;
1601 return 0;
1604 /* si4713_queryctrl - enumerate control items */
1605 static int si4713_queryctrl(struct v4l2_subdev *sd, struct v4l2_queryctrl *qc)
1607 int rval = 0;
1609 switch (qc->id) {
1610 /* User class controls */
1611 case V4L2_CID_AUDIO_MUTE:
1612 rval = v4l2_ctrl_query_fill(qc, 0, 1, 1, DEFAULT_MUTE);
1613 break;
1614 /* FM_TX class controls */
1615 case V4L2_CID_RDS_TX_PI:
1616 rval = v4l2_ctrl_query_fill(qc, 0, 0xFFFF, 1, DEFAULT_RDS_PI);
1617 break;
1618 case V4L2_CID_RDS_TX_PTY:
1619 rval = v4l2_ctrl_query_fill(qc, 0, 31, 1, DEFAULT_RDS_PTY);
1620 break;
1621 case V4L2_CID_RDS_TX_DEVIATION:
1622 rval = v4l2_ctrl_query_fill(qc, 0, MAX_RDS_DEVIATION,
1623 10, DEFAULT_RDS_DEVIATION);
1624 break;
1625 case V4L2_CID_RDS_TX_PS_NAME:
1627 * Report step as 8. From RDS spec, psname
1628 * should be 8. But there are receivers which scroll strings
1629 * sized as 8xN.
1631 rval = v4l2_ctrl_query_fill(qc, 0, MAX_RDS_PS_NAME, 8, 0);
1632 break;
1633 case V4L2_CID_RDS_TX_RADIO_TEXT:
1635 * Report step as 32 (2A block). From RDS spec,
1636 * radio text should be 32 for 2A block. But there are receivers
1637 * which scroll strings sized as 32xN. Setting default to 32.
1639 rval = v4l2_ctrl_query_fill(qc, 0, MAX_RDS_RADIO_TEXT, 32, 0);
1640 break;
1642 case V4L2_CID_AUDIO_LIMITER_ENABLED:
1643 rval = v4l2_ctrl_query_fill(qc, 0, 1, 1, 1);
1644 break;
1645 case V4L2_CID_AUDIO_LIMITER_RELEASE_TIME:
1646 rval = v4l2_ctrl_query_fill(qc, 250, MAX_LIMITER_RELEASE_TIME,
1647 50, DEFAULT_LIMITER_RTIME);
1648 break;
1649 case V4L2_CID_AUDIO_LIMITER_DEVIATION:
1650 rval = v4l2_ctrl_query_fill(qc, 0, MAX_LIMITER_DEVIATION,
1651 10, DEFAULT_LIMITER_DEV);
1652 break;
1654 case V4L2_CID_AUDIO_COMPRESSION_ENABLED:
1655 rval = v4l2_ctrl_query_fill(qc, 0, 1, 1, 1);
1656 break;
1657 case V4L2_CID_AUDIO_COMPRESSION_GAIN:
1658 rval = v4l2_ctrl_query_fill(qc, 0, MAX_ACOMP_GAIN, 1,
1659 DEFAULT_ACOMP_GAIN);
1660 break;
1661 case V4L2_CID_AUDIO_COMPRESSION_THRESHOLD:
1662 rval = v4l2_ctrl_query_fill(qc, MIN_ACOMP_THRESHOLD,
1663 MAX_ACOMP_THRESHOLD, 1,
1664 DEFAULT_ACOMP_THRESHOLD);
1665 break;
1666 case V4L2_CID_AUDIO_COMPRESSION_ATTACK_TIME:
1667 rval = v4l2_ctrl_query_fill(qc, 0, MAX_ACOMP_ATTACK_TIME,
1668 500, DEFAULT_ACOMP_ATIME);
1669 break;
1670 case V4L2_CID_AUDIO_COMPRESSION_RELEASE_TIME:
1671 rval = v4l2_ctrl_query_fill(qc, 100000, MAX_ACOMP_RELEASE_TIME,
1672 100000, DEFAULT_ACOMP_RTIME);
1673 break;
1675 case V4L2_CID_PILOT_TONE_ENABLED:
1676 rval = v4l2_ctrl_query_fill(qc, 0, 1, 1, 1);
1677 break;
1678 case V4L2_CID_PILOT_TONE_DEVIATION:
1679 rval = v4l2_ctrl_query_fill(qc, 0, MAX_PILOT_DEVIATION,
1680 10, DEFAULT_PILOT_DEVIATION);
1681 break;
1682 case V4L2_CID_PILOT_TONE_FREQUENCY:
1683 rval = v4l2_ctrl_query_fill(qc, 0, MAX_PILOT_FREQUENCY,
1684 1, DEFAULT_PILOT_FREQUENCY);
1685 break;
1687 case V4L2_CID_TUNE_PREEMPHASIS:
1688 rval = v4l2_ctrl_query_fill(qc, V4L2_PREEMPHASIS_DISABLED,
1689 V4L2_PREEMPHASIS_75_uS, 1,
1690 V4L2_PREEMPHASIS_50_uS);
1691 break;
1692 case V4L2_CID_TUNE_POWER_LEVEL:
1693 rval = v4l2_ctrl_query_fill(qc, 0, 120, 1, DEFAULT_POWER_LEVEL);
1694 break;
1695 case V4L2_CID_TUNE_ANTENNA_CAPACITOR:
1696 rval = v4l2_ctrl_query_fill(qc, 0, 191, 1, 0);
1697 break;
1698 default:
1699 rval = -EINVAL;
1700 break;
1703 return rval;
1706 /* si4713_g_ctrl - get the value of a control */
1707 static int si4713_g_ctrl(struct v4l2_subdev *sd, struct v4l2_control *ctrl)
1709 struct si4713_device *sdev = to_si4713_device(sd);
1710 int rval = 0;
1712 if (!sdev)
1713 return -ENODEV;
1715 mutex_lock(&sdev->mutex);
1717 if (sdev->power_state) {
1718 rval = si4713_read_property(sdev, SI4713_TX_LINE_INPUT_MUTE,
1719 &sdev->mute);
1721 if (rval < 0)
1722 goto unlock;
1725 switch (ctrl->id) {
1726 case V4L2_CID_AUDIO_MUTE:
1727 ctrl->value = get_mute(sdev->mute);
1728 break;
1731 unlock:
1732 mutex_unlock(&sdev->mutex);
1733 return rval;
1736 /* si4713_s_ctrl - set the value of a control */
1737 static int si4713_s_ctrl(struct v4l2_subdev *sd, struct v4l2_control *ctrl)
1739 struct si4713_device *sdev = to_si4713_device(sd);
1740 int rval = 0;
1742 if (!sdev)
1743 return -ENODEV;
1745 switch (ctrl->id) {
1746 case V4L2_CID_AUDIO_MUTE:
1747 if (ctrl->value) {
1748 rval = si4713_set_mute(sdev, ctrl->value);
1749 if (rval < 0)
1750 goto exit;
1752 rval = si4713_set_power_state(sdev, POWER_DOWN);
1753 } else {
1754 rval = si4713_set_power_state(sdev, POWER_UP);
1755 if (rval < 0)
1756 goto exit;
1758 rval = si4713_setup(sdev);
1759 if (rval < 0)
1760 goto exit;
1762 rval = si4713_set_mute(sdev, ctrl->value);
1764 break;
1767 exit:
1768 return rval;
1771 /* si4713_ioctl - deal with private ioctls (only rnl for now) */
1772 long si4713_ioctl(struct v4l2_subdev *sd, unsigned int cmd, void *arg)
1774 struct si4713_device *sdev = to_si4713_device(sd);
1775 struct si4713_rnl *rnl = arg;
1776 u16 frequency;
1777 int rval = 0;
1779 if (!arg)
1780 return -EINVAL;
1782 mutex_lock(&sdev->mutex);
1783 switch (cmd) {
1784 case SI4713_IOC_MEASURE_RNL:
1785 frequency = v4l2_to_si4713(rnl->frequency);
1787 if (sdev->power_state) {
1788 /* Set desired measurement frequency */
1789 rval = si4713_tx_tune_measure(sdev, frequency, 0);
1790 if (rval < 0)
1791 goto unlock;
1792 /* get results from tune status */
1793 rval = si4713_update_tune_status(sdev);
1794 if (rval < 0)
1795 goto unlock;
1797 rnl->rnl = sdev->tune_rnl;
1798 break;
1800 default:
1801 /* nothing */
1802 rval = -ENOIOCTLCMD;
1805 unlock:
1806 mutex_unlock(&sdev->mutex);
1807 return rval;
1810 static const struct v4l2_subdev_core_ops si4713_subdev_core_ops = {
1811 .queryctrl = si4713_queryctrl,
1812 .g_ext_ctrls = si4713_g_ext_ctrls,
1813 .s_ext_ctrls = si4713_s_ext_ctrls,
1814 .g_ctrl = si4713_g_ctrl,
1815 .s_ctrl = si4713_s_ctrl,
1816 .ioctl = si4713_ioctl,
1819 /* si4713_g_modulator - get modulator attributes */
1820 static int si4713_g_modulator(struct v4l2_subdev *sd, struct v4l2_modulator *vm)
1822 struct si4713_device *sdev = to_si4713_device(sd);
1823 int rval = 0;
1825 if (!sdev) {
1826 rval = -ENODEV;
1827 goto exit;
1830 if (vm->index > 0) {
1831 rval = -EINVAL;
1832 goto exit;
1835 strncpy(vm->name, "FM Modulator", 32);
1836 vm->capability = V4L2_TUNER_CAP_STEREO | V4L2_TUNER_CAP_LOW |
1837 V4L2_TUNER_CAP_RDS | V4L2_TUNER_CAP_RDS_CONTROLS;
1839 /* Report current frequency range limits */
1840 vm->rangelow = si4713_to_v4l2(FREQ_RANGE_LOW);
1841 vm->rangehigh = si4713_to_v4l2(FREQ_RANGE_HIGH);
1843 mutex_lock(&sdev->mutex);
1845 if (sdev->power_state) {
1846 u32 comp_en = 0;
1848 rval = si4713_read_property(sdev, SI4713_TX_COMPONENT_ENABLE,
1849 &comp_en);
1850 if (rval < 0)
1851 goto unlock;
1853 sdev->stereo = get_status_bit(comp_en, 1, 1 << 1);
1854 sdev->rds_info.enabled = get_status_bit(comp_en, 2, 1 << 2);
1857 /* Report current audio mode: mono or stereo */
1858 if (sdev->stereo)
1859 vm->txsubchans = V4L2_TUNER_SUB_STEREO;
1860 else
1861 vm->txsubchans = V4L2_TUNER_SUB_MONO;
1863 /* Report rds feature status */
1864 if (sdev->rds_info.enabled)
1865 vm->txsubchans |= V4L2_TUNER_SUB_RDS;
1866 else
1867 vm->txsubchans &= ~V4L2_TUNER_SUB_RDS;
1869 unlock:
1870 mutex_unlock(&sdev->mutex);
1871 exit:
1872 return rval;
1875 /* si4713_s_modulator - set modulator attributes */
1876 static int si4713_s_modulator(struct v4l2_subdev *sd, struct v4l2_modulator *vm)
1878 struct si4713_device *sdev = to_si4713_device(sd);
1879 int rval = 0;
1880 u16 stereo, rds;
1881 u32 p;
1883 if (!sdev)
1884 return -ENODEV;
1886 if (vm->index > 0)
1887 return -EINVAL;
1889 /* Set audio mode: mono or stereo */
1890 if (vm->txsubchans & V4L2_TUNER_SUB_STEREO)
1891 stereo = 1;
1892 else if (vm->txsubchans & V4L2_TUNER_SUB_MONO)
1893 stereo = 0;
1894 else
1895 return -EINVAL;
1897 rds = !!(vm->txsubchans & V4L2_TUNER_SUB_RDS);
1899 mutex_lock(&sdev->mutex);
1901 if (sdev->power_state) {
1902 rval = si4713_read_property(sdev,
1903 SI4713_TX_COMPONENT_ENABLE, &p);
1904 if (rval < 0)
1905 goto unlock;
1907 p = set_bits(p, stereo, 1, 1 << 1);
1908 p = set_bits(p, rds, 2, 1 << 2);
1910 rval = si4713_write_property(sdev,
1911 SI4713_TX_COMPONENT_ENABLE, p);
1912 if (rval < 0)
1913 goto unlock;
1916 sdev->stereo = stereo;
1917 sdev->rds_info.enabled = rds;
1919 unlock:
1920 mutex_unlock(&sdev->mutex);
1921 return rval;
1924 /* si4713_g_frequency - get tuner or modulator radio frequency */
1925 static int si4713_g_frequency(struct v4l2_subdev *sd, struct v4l2_frequency *f)
1927 struct si4713_device *sdev = to_si4713_device(sd);
1928 int rval = 0;
1930 f->type = V4L2_TUNER_RADIO;
1932 mutex_lock(&sdev->mutex);
1934 if (sdev->power_state) {
1935 u16 freq;
1936 u8 p, a, n;
1938 rval = si4713_tx_tune_status(sdev, 0x00, &freq, &p, &a, &n);
1939 if (rval < 0)
1940 goto unlock;
1942 sdev->frequency = freq;
1945 f->frequency = si4713_to_v4l2(sdev->frequency);
1947 unlock:
1948 mutex_unlock(&sdev->mutex);
1949 return rval;
1952 /* si4713_s_frequency - set tuner or modulator radio frequency */
1953 static int si4713_s_frequency(struct v4l2_subdev *sd, struct v4l2_frequency *f)
1955 struct si4713_device *sdev = to_si4713_device(sd);
1956 int rval = 0;
1957 u16 frequency = v4l2_to_si4713(f->frequency);
1959 /* Check frequency range */
1960 if (frequency < FREQ_RANGE_LOW || frequency > FREQ_RANGE_HIGH)
1961 return -EDOM;
1963 mutex_lock(&sdev->mutex);
1965 if (sdev->power_state) {
1966 rval = si4713_tx_tune_freq(sdev, frequency);
1967 if (rval < 0)
1968 goto unlock;
1969 frequency = rval;
1970 rval = 0;
1972 sdev->frequency = frequency;
1973 f->frequency = si4713_to_v4l2(frequency);
1975 unlock:
1976 mutex_unlock(&sdev->mutex);
1977 return rval;
1980 static const struct v4l2_subdev_tuner_ops si4713_subdev_tuner_ops = {
1981 .g_frequency = si4713_g_frequency,
1982 .s_frequency = si4713_s_frequency,
1983 .g_modulator = si4713_g_modulator,
1984 .s_modulator = si4713_s_modulator,
1987 static const struct v4l2_subdev_ops si4713_subdev_ops = {
1988 .core = &si4713_subdev_core_ops,
1989 .tuner = &si4713_subdev_tuner_ops,
1993 * I2C driver interface
1995 /* si4713_probe - probe for the device */
1996 static int si4713_probe(struct i2c_client *client,
1997 const struct i2c_device_id *id)
1999 struct si4713_device *sdev;
2000 struct si4713_platform_data *pdata = client->dev.platform_data;
2001 int rval, i;
2003 sdev = kzalloc(sizeof *sdev, GFP_KERNEL);
2004 if (!sdev) {
2005 dev_err(&client->dev, "Failed to alloc video device.\n");
2006 rval = -ENOMEM;
2007 goto exit;
2010 sdev->gpio_reset = -1;
2011 if (pdata && gpio_is_valid(pdata->gpio_reset)) {
2012 rval = gpio_request(pdata->gpio_reset, "si4713 reset");
2013 if (rval) {
2014 dev_err(&client->dev,
2015 "Failed to request gpio: %d\n", rval);
2016 goto free_sdev;
2018 sdev->gpio_reset = pdata->gpio_reset;
2019 gpio_direction_output(sdev->gpio_reset, 0);
2022 for (i = 0; i < ARRAY_SIZE(sdev->supplies); i++)
2023 sdev->supplies[i].supply = si4713_supply_names[i];
2025 rval = regulator_bulk_get(&client->dev, ARRAY_SIZE(sdev->supplies),
2026 sdev->supplies);
2027 if (rval) {
2028 dev_err(&client->dev, "Cannot get regulators: %d\n", rval);
2029 goto free_gpio;
2032 v4l2_i2c_subdev_init(&sdev->sd, client, &si4713_subdev_ops);
2034 mutex_init(&sdev->mutex);
2035 init_completion(&sdev->work);
2037 if (client->irq) {
2038 rval = request_irq(client->irq,
2039 si4713_handler, IRQF_TRIGGER_FALLING | IRQF_DISABLED,
2040 client->name, sdev);
2041 if (rval < 0) {
2042 v4l2_err(&sdev->sd, "Could not request IRQ\n");
2043 goto put_reg;
2045 v4l2_dbg(1, debug, &sdev->sd, "IRQ requested.\n");
2046 } else {
2047 v4l2_warn(&sdev->sd, "IRQ not configured. Using timeouts.\n");
2050 rval = si4713_initialize(sdev);
2051 if (rval < 0) {
2052 v4l2_err(&sdev->sd, "Failed to probe device information.\n");
2053 goto free_irq;
2056 return 0;
2058 free_irq:
2059 if (client->irq)
2060 free_irq(client->irq, sdev);
2061 put_reg:
2062 regulator_bulk_free(ARRAY_SIZE(sdev->supplies), sdev->supplies);
2063 free_gpio:
2064 if (gpio_is_valid(sdev->gpio_reset))
2065 gpio_free(sdev->gpio_reset);
2066 free_sdev:
2067 kfree(sdev);
2068 exit:
2069 return rval;
2072 /* si4713_remove - remove the device */
2073 static int si4713_remove(struct i2c_client *client)
2075 struct v4l2_subdev *sd = i2c_get_clientdata(client);
2076 struct si4713_device *sdev = to_si4713_device(sd);
2078 if (sdev->power_state)
2079 si4713_set_power_state(sdev, POWER_DOWN);
2081 if (client->irq > 0)
2082 free_irq(client->irq, sdev);
2084 v4l2_device_unregister_subdev(sd);
2085 regulator_bulk_free(ARRAY_SIZE(sdev->supplies), sdev->supplies);
2086 if (gpio_is_valid(sdev->gpio_reset))
2087 gpio_free(sdev->gpio_reset);
2088 kfree(sdev);
2090 return 0;
2093 /* si4713_i2c_driver - i2c driver interface */
2094 static const struct i2c_device_id si4713_id[] = {
2095 { "si4713" , 0 },
2096 { },
2098 MODULE_DEVICE_TABLE(i2c, si4713_id);
2100 static struct i2c_driver si4713_i2c_driver = {
2101 .driver = {
2102 .name = "si4713",
2104 .probe = si4713_probe,
2105 .remove = si4713_remove,
2106 .id_table = si4713_id,
2109 /* Module Interface */
2110 static int __init si4713_module_init(void)
2112 return i2c_add_driver(&si4713_i2c_driver);
2115 static void __exit si4713_module_exit(void)
2117 i2c_del_driver(&si4713_i2c_driver);
2120 module_init(si4713_module_init);
2121 module_exit(si4713_module_exit);