deca2e06ff2203bba0caae2e5737499fd0f9cdc2
[linux-2.6.git] / drivers / media / radio / si4713-i2c.c
1 /*
2  * drivers/media/radio/si4713-i2c.c
3  *
4  * Silicon Labs Si4713 FM Radio Transmitter I2C commands.
5  *
6  * Copyright (c) 2009 Nokia Corporation
7  * Contact: Eduardo Valentin <eduardo.valentin@nokia.com>
8  *
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.
13  *
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.
18  *
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
22  */
23
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 <media/v4l2-device.h>
33 #include <media/v4l2-ioctl.h>
34 #include <media/v4l2-common.h>
35
36 #include "si4713-i2c.h"
37
38 /* module parameters */
39 static int debug;
40 module_param(debug, int, S_IRUGO | S_IWUSR);
41 MODULE_PARM_DESC(debug, "Debug level (0 - 2)");
42
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");
47
48 static const char *si4713_supply_names[SI4713_NUM_SUPPLIES] = {
49         "vio",
50         "vdd",
51 };
52
53 #define DEFAULT_RDS_PI                  0x00
54 #define DEFAULT_RDS_PTY                 0x00
55 #define DEFAULT_RDS_PS_NAME             ""
56 #define DEFAULT_RDS_RADIO_TEXT          DEFAULT_RDS_PS_NAME
57 #define DEFAULT_RDS_DEVIATION           0x00C8
58 #define DEFAULT_RDS_PS_REPEAT_COUNT     0x0003
59 #define DEFAULT_LIMITER_RTIME           0x1392
60 #define DEFAULT_LIMITER_DEV             0x102CA
61 #define DEFAULT_PILOT_FREQUENCY         0x4A38
62 #define DEFAULT_PILOT_DEVIATION         0x1A5E
63 #define DEFAULT_ACOMP_ATIME             0x0000
64 #define DEFAULT_ACOMP_RTIME             0xF4240L
65 #define DEFAULT_ACOMP_GAIN              0x0F
66 #define DEFAULT_ACOMP_THRESHOLD         (-0x28)
67 #define DEFAULT_MUTE                    0x01
68 #define DEFAULT_POWER_LEVEL             88
69 #define DEFAULT_FREQUENCY               8800
70 #define DEFAULT_PREEMPHASIS             FMPE_EU
71 #define DEFAULT_TUNE_RNL                0xFF
72
73 #define to_si4713_device(sd)    container_of(sd, struct si4713_device, sd)
74
75 /* frequency domain transformation (using times 10 to avoid floats) */
76 #define FREQDEV_UNIT    100000
77 #define FREQV4L2_MULTI  625
78 #define si4713_to_v4l2(f)       ((f * FREQDEV_UNIT) / FREQV4L2_MULTI)
79 #define v4l2_to_si4713(f)       ((f * FREQV4L2_MULTI) / FREQDEV_UNIT)
80 #define FREQ_RANGE_LOW                  7600
81 #define FREQ_RANGE_HIGH                 10800
82
83 #define MAX_ARGS 7
84
85 #define RDS_BLOCK                       8
86 #define RDS_BLOCK_CLEAR                 0x03
87 #define RDS_BLOCK_LOAD                  0x04
88 #define RDS_RADIOTEXT_2A                0x20
89 #define RDS_RADIOTEXT_BLK_SIZE          4
90 #define RDS_RADIOTEXT_INDEX_MAX         0x0F
91 #define RDS_CARRIAGE_RETURN             0x0D
92
93 #define rds_ps_nblocks(len)     ((len / RDS_BLOCK) + (len % RDS_BLOCK ? 1 : 0))
94
95 #define get_status_bit(p, b, m) (((p) & (m)) >> (b))
96 #define set_bits(p, v, b, m)    (((p) & ~(m)) | ((v) << (b)))
97
98 #define ATTACK_TIME_UNIT        500
99
100 #define POWER_OFF                       0x00
101 #define POWER_ON                        0x01
102
103 #define msb(x)                  ((u8)((u16) x >> 8))
104 #define lsb(x)                  ((u8)((u16) x &  0x00FF))
105 #define compose_u16(msb, lsb)   (((u16)msb << 8) | lsb)
106 #define check_command_failed(status)    (!(status & SI4713_CTS) || \
107                                         (status & SI4713_ERR))
108 /* mute definition */
109 #define set_mute(p)     ((p & 1) | ((p & 1) << 1));
110 #define get_mute(p)     (p & 0x01)
111
112 #ifdef DEBUG
113 #define DBG_BUFFER(device, message, buffer, size)                       \
114         {                                                               \
115                 int i;                                                  \
116                 char str[(size)*5];                                     \
117                 for (i = 0; i < size; i++)                              \
118                         sprintf(str + i * 5, " 0x%02x", buffer[i]);     \
119                 v4l2_dbg(2, debug, device, "%s:%s\n", message, str);    \
120         }
121 #else
122 #define DBG_BUFFER(device, message, buffer, size)
123 #endif
124
125 /*
126  * Values for limiter release time (sorted by second column)
127  *      device  release
128  *      value   time (us)
129  */
130 static long limiter_times[] = {
131         2000,   250,
132         1000,   500,
133         510,    1000,
134         255,    2000,
135         170,    3000,
136         127,    4020,
137         102,    5010,
138         85,     6020,
139         73,     7010,
140         64,     7990,
141         57,     8970,
142         51,     10030,
143         25,     20470,
144         17,     30110,
145         13,     39380,
146         10,     51190,
147         8,      63690,
148         7,      73140,
149         6,      85330,
150         5,      102390,
151 };
152
153 /*
154  * Values for audio compression release time (sorted by second column)
155  *      device  release
156  *      value   time (us)
157  */
158 static unsigned long acomp_rtimes[] = {
159         0,      100000,
160         1,      200000,
161         2,      350000,
162         3,      525000,
163         4,      1000000,
164 };
165
166 /*
167  * Values for preemphasis (sorted by second column)
168  *      device  preemphasis
169  *      value   value (v4l2)
170  */
171 static unsigned long preemphasis_values[] = {
172         FMPE_DISABLED,  V4L2_PREEMPHASIS_DISABLED,
173         FMPE_EU,        V4L2_PREEMPHASIS_50_uS,
174         FMPE_USA,       V4L2_PREEMPHASIS_75_uS,
175 };
176
177 static int usecs_to_dev(unsigned long usecs, unsigned long const array[],
178                         int size)
179 {
180         int i;
181         int rval = -EINVAL;
182
183         for (i = 0; i < size / 2; i++)
184                 if (array[(i * 2) + 1] >= usecs) {
185                         rval = array[i * 2];
186                         break;
187                 }
188
189         return rval;
190 }
191
192 static unsigned long dev_to_usecs(int value, unsigned long const array[],
193                         int size)
194 {
195         int i;
196         int rval = -EINVAL;
197
198         for (i = 0; i < size / 2; i++)
199                 if (array[i * 2] == value) {
200                         rval = array[(i * 2) + 1];
201                         break;
202                 }
203
204         return rval;
205 }
206
207 /* si4713_handler: IRQ handler, just complete work */
208 static irqreturn_t si4713_handler(int irq, void *dev)
209 {
210         struct si4713_device *sdev = dev;
211
212         v4l2_dbg(2, debug, &sdev->sd,
213                         "%s: sending signal to completion work.\n", __func__);
214         complete(&sdev->work);
215
216         return IRQ_HANDLED;
217 }
218
219 /*
220  * si4713_send_command - sends a command to si4713 and waits its response
221  * @sdev: si4713_device structure for the device we are communicating
222  * @command: command id
223  * @args: command arguments we are sending (up to 7)
224  * @argn: actual size of @args
225  * @response: buffer to place the expected response from the device (up to 15)
226  * @respn: actual size of @response
227  * @usecs: amount of time to wait before reading the response (in usecs)
228  */
229 static int si4713_send_command(struct si4713_device *sdev, const u8 command,
230                                 const u8 args[], const int argn,
231                                 u8 response[], const int respn, const int usecs)
232 {
233         struct i2c_client *client = v4l2_get_subdevdata(&sdev->sd);
234         u8 data1[MAX_ARGS + 1];
235         int err;
236
237         if (!client->adapter)
238                 return -ENODEV;
239
240         /* First send the command and its arguments */
241         data1[0] = command;
242         memcpy(data1 + 1, args, argn);
243         DBG_BUFFER(&sdev->sd, "Parameters", data1, argn + 1);
244
245         err = i2c_master_send(client, data1, argn + 1);
246         if (err != argn + 1) {
247                 v4l2_err(&sdev->sd, "Error while sending command 0x%02x\n",
248                         command);
249                 return (err > 0) ? -EIO : err;
250         }
251
252         /* Wait response from interrupt */
253         if (!wait_for_completion_timeout(&sdev->work,
254                                 usecs_to_jiffies(usecs) + 1))
255                 v4l2_warn(&sdev->sd,
256                                 "(%s) Device took too much time to answer.\n",
257                                 __func__);
258
259         /* Then get the response */
260         err = i2c_master_recv(client, response, respn);
261         if (err != respn) {
262                 v4l2_err(&sdev->sd,
263                         "Error while reading response for command 0x%02x\n",
264                         command);
265                 return (err > 0) ? -EIO : err;
266         }
267
268         DBG_BUFFER(&sdev->sd, "Response", response, respn);
269         if (check_command_failed(response[0]))
270                 return -EBUSY;
271
272         return 0;
273 }
274
275 /*
276  * si4713_read_property - reads a si4713 property
277  * @sdev: si4713_device structure for the device we are communicating
278  * @prop: property identification number
279  * @pv: property value to be returned on success
280  */
281 static int si4713_read_property(struct si4713_device *sdev, u16 prop, u32 *pv)
282 {
283         int err;
284         u8 val[SI4713_GET_PROP_NRESP];
285         /*
286          *      .First byte = 0
287          *      .Second byte = property's MSB
288          *      .Third byte = property's LSB
289          */
290         const u8 args[SI4713_GET_PROP_NARGS] = {
291                 0x00,
292                 msb(prop),
293                 lsb(prop),
294         };
295
296         err = si4713_send_command(sdev, SI4713_CMD_GET_PROPERTY,
297                                   args, ARRAY_SIZE(args), val,
298                                   ARRAY_SIZE(val), DEFAULT_TIMEOUT);
299
300         if (err < 0)
301                 return err;
302
303         *pv = compose_u16(val[2], val[3]);
304
305         v4l2_dbg(1, debug, &sdev->sd,
306                         "%s: property=0x%02x value=0x%02x status=0x%02x\n",
307                         __func__, prop, *pv, val[0]);
308
309         return err;
310 }
311
312 /*
313  * si4713_write_property - modifies a si4713 property
314  * @sdev: si4713_device structure for the device we are communicating
315  * @prop: property identification number
316  * @val: new value for that property
317  */
318 static int si4713_write_property(struct si4713_device *sdev, u16 prop, u16 val)
319 {
320         int rval;
321         u8 resp[SI4713_SET_PROP_NRESP];
322         /*
323          *      .First byte = 0
324          *      .Second byte = property's MSB
325          *      .Third byte = property's LSB
326          *      .Fourth byte = value's MSB
327          *      .Fifth byte = value's LSB
328          */
329         const u8 args[SI4713_SET_PROP_NARGS] = {
330                 0x00,
331                 msb(prop),
332                 lsb(prop),
333                 msb(val),
334                 lsb(val),
335         };
336
337         rval = si4713_send_command(sdev, SI4713_CMD_SET_PROPERTY,
338                                         args, ARRAY_SIZE(args),
339                                         resp, ARRAY_SIZE(resp),
340                                         DEFAULT_TIMEOUT);
341
342         if (rval < 0)
343                 return rval;
344
345         v4l2_dbg(1, debug, &sdev->sd,
346                         "%s: property=0x%02x value=0x%02x status=0x%02x\n",
347                         __func__, prop, val, resp[0]);
348
349         /*
350          * As there is no command response for SET_PROPERTY,
351          * wait Tcomp time to finish before proceed, in order
352          * to have property properly set.
353          */
354         msleep(TIMEOUT_SET_PROPERTY);
355
356         return rval;
357 }
358
359 /*
360  * si4713_powerup - Powers the device up
361  * @sdev: si4713_device structure for the device we are communicating
362  */
363 static int si4713_powerup(struct si4713_device *sdev)
364 {
365         int err;
366         u8 resp[SI4713_PWUP_NRESP];
367         /*
368          *      .First byte = Enabled interrupts and boot function
369          *      .Second byte = Input operation mode
370          */
371         const u8 args[SI4713_PWUP_NARGS] = {
372                 SI4713_PWUP_CTSIEN | SI4713_PWUP_GPO2OEN | SI4713_PWUP_FUNC_TX,
373                 SI4713_PWUP_OPMOD_ANALOG,
374         };
375
376         if (sdev->power_state)
377                 return 0;
378
379         err = regulator_bulk_enable(ARRAY_SIZE(sdev->supplies),
380                                     sdev->supplies);
381         if (err) {
382                 v4l2_err(&sdev->sd, "Failed to enable supplies: %d\n", err);
383                 return err;
384         }
385         if (gpio_is_valid(sdev->gpio_reset)) {
386                 udelay(50);
387                 gpio_set_value(sdev->gpio_reset, 1);
388         }
389
390         err = si4713_send_command(sdev, SI4713_CMD_POWER_UP,
391                                         args, ARRAY_SIZE(args),
392                                         resp, ARRAY_SIZE(resp),
393                                         TIMEOUT_POWER_UP);
394
395         if (!err) {
396                 v4l2_dbg(1, debug, &sdev->sd, "Powerup response: 0x%02x\n",
397                                 resp[0]);
398                 v4l2_dbg(1, debug, &sdev->sd, "Device in power up mode\n");
399                 sdev->power_state = POWER_ON;
400
401                 err = si4713_write_property(sdev, SI4713_GPO_IEN,
402                                                 SI4713_STC_INT | SI4713_CTS);
403         } else {
404                 if (gpio_is_valid(sdev->gpio_reset))
405                         gpio_set_value(sdev->gpio_reset, 0);
406                 err = regulator_bulk_disable(ARRAY_SIZE(sdev->supplies),
407                                              sdev->supplies);
408                 if (err)
409                         v4l2_err(&sdev->sd,
410                                  "Failed to disable supplies: %d\n", err);
411         }
412
413         return err;
414 }
415
416 /*
417  * si4713_powerdown - Powers the device down
418  * @sdev: si4713_device structure for the device we are communicating
419  */
420 static int si4713_powerdown(struct si4713_device *sdev)
421 {
422         int err;
423         u8 resp[SI4713_PWDN_NRESP];
424
425         if (!sdev->power_state)
426                 return 0;
427
428         err = si4713_send_command(sdev, SI4713_CMD_POWER_DOWN,
429                                         NULL, 0,
430                                         resp, ARRAY_SIZE(resp),
431                                         DEFAULT_TIMEOUT);
432
433         if (!err) {
434                 v4l2_dbg(1, debug, &sdev->sd, "Power down response: 0x%02x\n",
435                                 resp[0]);
436                 v4l2_dbg(1, debug, &sdev->sd, "Device in reset mode\n");
437                 if (gpio_is_valid(sdev->gpio_reset))
438                         gpio_set_value(sdev->gpio_reset, 0);
439                 err = regulator_bulk_disable(ARRAY_SIZE(sdev->supplies),
440                                              sdev->supplies);
441                 if (err)
442                         v4l2_err(&sdev->sd,
443                                  "Failed to disable supplies: %d\n", err);
444                 sdev->power_state = POWER_OFF;
445         }
446
447         return err;
448 }
449
450 /*
451  * si4713_checkrev - Checks if we are treating a device with the correct rev.
452  * @sdev: si4713_device structure for the device we are communicating
453  */
454 static int si4713_checkrev(struct si4713_device *sdev)
455 {
456         struct i2c_client *client = v4l2_get_subdevdata(&sdev->sd);
457         int rval;
458         u8 resp[SI4713_GETREV_NRESP];
459
460         mutex_lock(&sdev->mutex);
461
462         rval = si4713_send_command(sdev, SI4713_CMD_GET_REV,
463                                         NULL, 0,
464                                         resp, ARRAY_SIZE(resp),
465                                         DEFAULT_TIMEOUT);
466
467         if (rval < 0)
468                 goto unlock;
469
470         if (resp[1] == SI4713_PRODUCT_NUMBER) {
471                 v4l2_info(&sdev->sd, "chip found @ 0x%02x (%s)\n",
472                                 client->addr << 1, client->adapter->name);
473         } else {
474                 v4l2_err(&sdev->sd, "Invalid product number\n");
475                 rval = -EINVAL;
476         }
477
478 unlock:
479         mutex_unlock(&sdev->mutex);
480         return rval;
481 }
482
483 /*
484  * si4713_wait_stc - Waits STC interrupt and clears status bits. Useful
485  *                   for TX_TUNE_POWER, TX_TUNE_FREQ and TX_TUNE_MEAS
486  * @sdev: si4713_device structure for the device we are communicating
487  * @usecs: timeout to wait for STC interrupt signal
488  */
489 static int si4713_wait_stc(struct si4713_device *sdev, const int usecs)
490 {
491         int err;
492         u8 resp[SI4713_GET_STATUS_NRESP];
493
494         /* Wait response from STC interrupt */
495         if (!wait_for_completion_timeout(&sdev->work,
496                         usecs_to_jiffies(usecs) + 1))
497                 v4l2_warn(&sdev->sd,
498                         "%s: device took too much time to answer (%d usec).\n",
499                                 __func__, usecs);
500
501         /* Clear status bits */
502         err = si4713_send_command(sdev, SI4713_CMD_GET_INT_STATUS,
503                                         NULL, 0,
504                                         resp, ARRAY_SIZE(resp),
505                                         DEFAULT_TIMEOUT);
506
507         if (err < 0)
508                 goto exit;
509
510         v4l2_dbg(1, debug, &sdev->sd,
511                         "%s: status bits: 0x%02x\n", __func__, resp[0]);
512
513         if (!(resp[0] & SI4713_STC_INT))
514                 err = -EIO;
515
516 exit:
517         return err;
518 }
519
520 /*
521  * si4713_tx_tune_freq - Sets the state of the RF carrier and sets the tuning
522  *                      frequency between 76 and 108 MHz in 10 kHz units and
523  *                      steps of 50 kHz.
524  * @sdev: si4713_device structure for the device we are communicating
525  * @frequency: desired frequency (76 - 108 MHz, unit 10 KHz, step 50 kHz)
526  */
527 static int si4713_tx_tune_freq(struct si4713_device *sdev, u16 frequency)
528 {
529         int err;
530         u8 val[SI4713_TXFREQ_NRESP];
531         /*
532          *      .First byte = 0
533          *      .Second byte = frequency's MSB
534          *      .Third byte = frequency's LSB
535          */
536         const u8 args[SI4713_TXFREQ_NARGS] = {
537                 0x00,
538                 msb(frequency),
539                 lsb(frequency),
540         };
541
542         err = si4713_send_command(sdev, SI4713_CMD_TX_TUNE_FREQ,
543                                   args, ARRAY_SIZE(args), val,
544                                   ARRAY_SIZE(val), DEFAULT_TIMEOUT);
545
546         if (err < 0)
547                 return err;
548
549         v4l2_dbg(1, debug, &sdev->sd,
550                         "%s: frequency=0x%02x status=0x%02x\n", __func__,
551                         frequency, val[0]);
552
553         err = si4713_wait_stc(sdev, TIMEOUT_TX_TUNE);
554         if (err < 0)
555                 return err;
556
557         return compose_u16(args[1], args[2]);
558 }
559
560 /*
561  * si4713_tx_tune_power - Sets the RF voltage level between 88 and 115 dBuV in
562  *                      1 dB units. A value of 0x00 indicates off. The command
563  *                      also sets the antenna tuning capacitance. A value of 0
564  *                      indicates autotuning, and a value of 1 - 191 indicates
565  *                      a manual override, which results in a tuning
566  *                      capacitance of 0.25 pF x @antcap.
567  * @sdev: si4713_device structure for the device we are communicating
568  * @power: tuning power (88 - 115 dBuV, unit/step 1 dB)
569  * @antcap: value of antenna tuning capacitor (0 - 191)
570  */
571 static int si4713_tx_tune_power(struct si4713_device *sdev, u8 power,
572                                 u8 antcap)
573 {
574         int err;
575         u8 val[SI4713_TXPWR_NRESP];
576         /*
577          *      .First byte = 0
578          *      .Second byte = 0
579          *      .Third byte = power
580          *      .Fourth byte = antcap
581          */
582         const u8 args[SI4713_TXPWR_NARGS] = {
583                 0x00,
584                 0x00,
585                 power,
586                 antcap,
587         };
588
589         if (((power > 0) && (power < SI4713_MIN_POWER)) ||
590                 power > SI4713_MAX_POWER || antcap > SI4713_MAX_ANTCAP)
591                 return -EDOM;
592
593         err = si4713_send_command(sdev, SI4713_CMD_TX_TUNE_POWER,
594                                   args, ARRAY_SIZE(args), val,
595                                   ARRAY_SIZE(val), DEFAULT_TIMEOUT);
596
597         if (err < 0)
598                 return err;
599
600         v4l2_dbg(1, debug, &sdev->sd,
601                         "%s: power=0x%02x antcap=0x%02x status=0x%02x\n",
602                         __func__, power, antcap, val[0]);
603
604         return si4713_wait_stc(sdev, TIMEOUT_TX_TUNE_POWER);
605 }
606
607 /*
608  * si4713_tx_tune_measure - Enters receive mode and measures the received noise
609  *                      level in units of dBuV on the selected frequency.
610  *                      The Frequency must be between 76 and 108 MHz in 10 kHz
611  *                      units and steps of 50 kHz. The command also sets the
612  *                      antenna tuning capacitance. A value of 0 means
613  *                      autotuning, and a value of 1 to 191 indicates manual
614  *                      override.
615  * @sdev: si4713_device structure for the device we are communicating
616  * @frequency: desired frequency (76 - 108 MHz, unit 10 KHz, step 50 kHz)
617  * @antcap: value of antenna tuning capacitor (0 - 191)
618  */
619 static int si4713_tx_tune_measure(struct si4713_device *sdev, u16 frequency,
620                                         u8 antcap)
621 {
622         int err;
623         u8 val[SI4713_TXMEA_NRESP];
624         /*
625          *      .First byte = 0
626          *      .Second byte = frequency's MSB
627          *      .Third byte = frequency's LSB
628          *      .Fourth byte = antcap
629          */
630         const u8 args[SI4713_TXMEA_NARGS] = {
631                 0x00,
632                 msb(frequency),
633                 lsb(frequency),
634                 antcap,
635         };
636
637         sdev->tune_rnl = DEFAULT_TUNE_RNL;
638
639         if (antcap > SI4713_MAX_ANTCAP)
640                 return -EDOM;
641
642         err = si4713_send_command(sdev, SI4713_CMD_TX_TUNE_MEASURE,
643                                   args, ARRAY_SIZE(args), val,
644                                   ARRAY_SIZE(val), DEFAULT_TIMEOUT);
645
646         if (err < 0)
647                 return err;
648
649         v4l2_dbg(1, debug, &sdev->sd,
650                         "%s: frequency=0x%02x antcap=0x%02x status=0x%02x\n",
651                         __func__, frequency, antcap, val[0]);
652
653         return si4713_wait_stc(sdev, TIMEOUT_TX_TUNE);
654 }
655
656 /*
657  * si4713_tx_tune_status- Returns the status of the tx_tune_freq, tx_tune_mea or
658  *                      tx_tune_power commands. This command return the current
659  *                      frequency, output voltage in dBuV, the antenna tunning
660  *                      capacitance value and the received noise level. The
661  *                      command also clears the stcint interrupt bit when the
662  *                      first bit of its arguments is high.
663  * @sdev: si4713_device structure for the device we are communicating
664  * @intack: 0x01 to clear the seek/tune complete interrupt status indicator.
665  * @frequency: returned frequency
666  * @power: returned power
667  * @antcap: returned antenna capacitance
668  * @noise: returned noise level
669  */
670 static int si4713_tx_tune_status(struct si4713_device *sdev, u8 intack,
671                                         u16 *frequency, u8 *power,
672                                         u8 *antcap, u8 *noise)
673 {
674         int err;
675         u8 val[SI4713_TXSTATUS_NRESP];
676         /*
677          *      .First byte = intack bit
678          */
679         const u8 args[SI4713_TXSTATUS_NARGS] = {
680                 intack & SI4713_INTACK_MASK,
681         };
682
683         err = si4713_send_command(sdev, SI4713_CMD_TX_TUNE_STATUS,
684                                   args, ARRAY_SIZE(args), val,
685                                   ARRAY_SIZE(val), DEFAULT_TIMEOUT);
686
687         if (!err) {
688                 v4l2_dbg(1, debug, &sdev->sd,
689                         "%s: status=0x%02x\n", __func__, val[0]);
690                 *frequency = compose_u16(val[2], val[3]);
691                 sdev->frequency = *frequency;
692                 *power = val[5];
693                 *antcap = val[6];
694                 *noise = val[7];
695                 v4l2_dbg(1, debug, &sdev->sd, "%s: response: %d x 10 kHz "
696                                 "(power %d, antcap %d, rnl %d)\n", __func__,
697                                 *frequency, *power, *antcap, *noise);
698         }
699
700         return err;
701 }
702
703 /*
704  * si4713_tx_rds_buff - Loads the RDS group buffer FIFO or circular buffer.
705  * @sdev: si4713_device structure for the device we are communicating
706  * @mode: the buffer operation mode.
707  * @rdsb: RDS Block B
708  * @rdsc: RDS Block C
709  * @rdsd: RDS Block D
710  * @cbleft: returns the number of available circular buffer blocks minus the
711  *          number of used circular buffer blocks.
712  */
713 static int si4713_tx_rds_buff(struct si4713_device *sdev, u8 mode, u16 rdsb,
714                                 u16 rdsc, u16 rdsd, s8 *cbleft)
715 {
716         int err;
717         u8 val[SI4713_RDSBUFF_NRESP];
718
719         const u8 args[SI4713_RDSBUFF_NARGS] = {
720                 mode & SI4713_RDSBUFF_MODE_MASK,
721                 msb(rdsb),
722                 lsb(rdsb),
723                 msb(rdsc),
724                 lsb(rdsc),
725                 msb(rdsd),
726                 lsb(rdsd),
727         };
728
729         err = si4713_send_command(sdev, SI4713_CMD_TX_RDS_BUFF,
730                                   args, ARRAY_SIZE(args), val,
731                                   ARRAY_SIZE(val), DEFAULT_TIMEOUT);
732
733         if (!err) {
734                 v4l2_dbg(1, debug, &sdev->sd,
735                         "%s: status=0x%02x\n", __func__, val[0]);
736                 *cbleft = (s8)val[2] - val[3];
737                 v4l2_dbg(1, debug, &sdev->sd, "%s: response: interrupts"
738                                 " 0x%02x cb avail: %d cb used %d fifo avail"
739                                 " %d fifo used %d\n", __func__, val[1],
740                                 val[2], val[3], val[4], val[5]);
741         }
742
743         return err;
744 }
745
746 /*
747  * si4713_tx_rds_ps - Loads the program service buffer.
748  * @sdev: si4713_device structure for the device we are communicating
749  * @psid: program service id to be loaded.
750  * @pschar: assumed 4 size char array to be loaded into the program service
751  */
752 static int si4713_tx_rds_ps(struct si4713_device *sdev, u8 psid,
753                                 unsigned char *pschar)
754 {
755         int err;
756         u8 val[SI4713_RDSPS_NRESP];
757
758         const u8 args[SI4713_RDSPS_NARGS] = {
759                 psid & SI4713_RDSPS_PSID_MASK,
760                 pschar[0],
761                 pschar[1],
762                 pschar[2],
763                 pschar[3],
764         };
765
766         err = si4713_send_command(sdev, SI4713_CMD_TX_RDS_PS,
767                                   args, ARRAY_SIZE(args), val,
768                                   ARRAY_SIZE(val), DEFAULT_TIMEOUT);
769
770         if (err < 0)
771                 return err;
772
773         v4l2_dbg(1, debug, &sdev->sd, "%s: status=0x%02x\n", __func__, val[0]);
774
775         return err;
776 }
777
778 static int si4713_set_power_state(struct si4713_device *sdev, u8 value)
779 {
780         int rval;
781
782         mutex_lock(&sdev->mutex);
783
784         if (value)
785                 rval = si4713_powerup(sdev);
786         else
787                 rval = si4713_powerdown(sdev);
788
789         mutex_unlock(&sdev->mutex);
790         return rval;
791 }
792
793 static int si4713_set_mute(struct si4713_device *sdev, u16 mute)
794 {
795         int rval = 0;
796
797         mute = set_mute(mute);
798
799         mutex_lock(&sdev->mutex);
800
801         if (sdev->power_state)
802                 rval = si4713_write_property(sdev,
803                                 SI4713_TX_LINE_INPUT_MUTE, mute);
804
805         if (rval >= 0)
806                 sdev->mute = get_mute(mute);
807
808         mutex_unlock(&sdev->mutex);
809
810         return rval;
811 }
812
813 static int si4713_set_rds_ps_name(struct si4713_device *sdev, char *ps_name)
814 {
815         int rval = 0, i;
816         u8 len = 0;
817
818         /* We want to clear the whole thing */
819         if (!strlen(ps_name))
820                 memset(ps_name, 0, MAX_RDS_PS_NAME + 1);
821
822         mutex_lock(&sdev->mutex);
823
824         if (sdev->power_state) {
825                 /* Write the new ps name and clear the padding */
826                 for (i = 0; i < MAX_RDS_PS_NAME; i += (RDS_BLOCK / 2)) {
827                         rval = si4713_tx_rds_ps(sdev, (i / (RDS_BLOCK / 2)),
828                                                 ps_name + i);
829                         if (rval < 0)
830                                 goto unlock;
831                 }
832
833                 /* Setup the size to be sent */
834                 if (strlen(ps_name))
835                         len = strlen(ps_name) - 1;
836                 else
837                         len = 1;
838
839                 rval = si4713_write_property(sdev,
840                                 SI4713_TX_RDS_PS_MESSAGE_COUNT,
841                                 rds_ps_nblocks(len));
842                 if (rval < 0)
843                         goto unlock;
844
845                 rval = si4713_write_property(sdev,
846                                 SI4713_TX_RDS_PS_REPEAT_COUNT,
847                                 DEFAULT_RDS_PS_REPEAT_COUNT * 2);
848                 if (rval < 0)
849                         goto unlock;
850         }
851
852         strncpy(sdev->rds_info.ps_name, ps_name, MAX_RDS_PS_NAME);
853
854 unlock:
855         mutex_unlock(&sdev->mutex);
856         return rval;
857 }
858
859 static int si4713_set_rds_radio_text(struct si4713_device *sdev, char *rt)
860 {
861         int rval = 0, i;
862         u16 t_index = 0;
863         u8 b_index = 0, cr_inserted = 0;
864         s8 left;
865
866         mutex_lock(&sdev->mutex);
867
868         if (!sdev->power_state)
869                 goto copy;
870
871         rval = si4713_tx_rds_buff(sdev, RDS_BLOCK_CLEAR, 0, 0, 0, &left);
872         if (rval < 0)
873                 goto unlock;
874
875         if (!strlen(rt))
876                 goto copy;
877
878         do {
879                 /* RDS spec says that if the last block isn't used,
880                  * then apply a carriage return
881                  */
882                 if (t_index < (RDS_RADIOTEXT_INDEX_MAX *
883                         RDS_RADIOTEXT_BLK_SIZE)) {
884                         for (i = 0; i < RDS_RADIOTEXT_BLK_SIZE; i++) {
885                                 if (!rt[t_index + i] || rt[t_index + i] ==
886                                         RDS_CARRIAGE_RETURN) {
887                                         rt[t_index + i] = RDS_CARRIAGE_RETURN;
888                                         cr_inserted = 1;
889                                         break;
890                                 }
891                         }
892                 }
893
894                 rval = si4713_tx_rds_buff(sdev, RDS_BLOCK_LOAD,
895                                 compose_u16(RDS_RADIOTEXT_2A, b_index++),
896                                 compose_u16(rt[t_index], rt[t_index + 1]),
897                                 compose_u16(rt[t_index + 2], rt[t_index + 3]),
898                                 &left);
899                 if (rval < 0)
900                         goto unlock;
901
902                 t_index += RDS_RADIOTEXT_BLK_SIZE;
903
904                 if (cr_inserted)
905                         break;
906         } while (left > 0);
907
908 copy:
909         strncpy(sdev->rds_info.radio_text, rt, MAX_RDS_RADIO_TEXT);
910
911 unlock:
912         mutex_unlock(&sdev->mutex);
913         return rval;
914 }
915
916 static int si4713_choose_econtrol_action(struct si4713_device *sdev, u32 id,
917                 u32 **shadow, s32 *bit, s32 *mask, u16 *property, int *mul,
918                 unsigned long **table, int *size)
919 {
920         s32 rval = 0;
921
922         switch (id) {
923         /* FM_TX class controls */
924         case V4L2_CID_RDS_TX_PI:
925                 *property = SI4713_TX_RDS_PI;
926                 *mul = 1;
927                 *shadow = &sdev->rds_info.pi;
928                 break;
929         case V4L2_CID_AUDIO_COMPRESSION_THRESHOLD:
930                 *property = SI4713_TX_ACOMP_THRESHOLD;
931                 *mul = 1;
932                 *shadow = &sdev->acomp_info.threshold;
933                 break;
934         case V4L2_CID_AUDIO_COMPRESSION_GAIN:
935                 *property = SI4713_TX_ACOMP_GAIN;
936                 *mul = 1;
937                 *shadow = &sdev->acomp_info.gain;
938                 break;
939         case V4L2_CID_PILOT_TONE_FREQUENCY:
940                 *property = SI4713_TX_PILOT_FREQUENCY;
941                 *mul = 1;
942                 *shadow = &sdev->pilot_info.frequency;
943                 break;
944         case V4L2_CID_AUDIO_COMPRESSION_ATTACK_TIME:
945                 *property = SI4713_TX_ACOMP_ATTACK_TIME;
946                 *mul = ATTACK_TIME_UNIT;
947                 *shadow = &sdev->acomp_info.attack_time;
948                 break;
949         case V4L2_CID_PILOT_TONE_DEVIATION:
950                 *property = SI4713_TX_PILOT_DEVIATION;
951                 *mul = 10;
952                 *shadow = &sdev->pilot_info.deviation;
953                 break;
954         case V4L2_CID_AUDIO_LIMITER_DEVIATION:
955                 *property = SI4713_TX_AUDIO_DEVIATION;
956                 *mul = 10;
957                 *shadow = &sdev->limiter_info.deviation;
958                 break;
959         case V4L2_CID_RDS_TX_DEVIATION:
960                 *property = SI4713_TX_RDS_DEVIATION;
961                 *mul = 1;
962                 *shadow = &sdev->rds_info.deviation;
963                 break;
964
965         case V4L2_CID_RDS_TX_PTY:
966                 *property = SI4713_TX_RDS_PS_MISC;
967                 *bit = 5;
968                 *mask = 0x1F << 5;
969                 *shadow = &sdev->rds_info.pty;
970                 break;
971         case V4L2_CID_AUDIO_LIMITER_ENABLED:
972                 *property = SI4713_TX_ACOMP_ENABLE;
973                 *bit = 1;
974                 *mask = 1 << 1;
975                 *shadow = &sdev->limiter_info.enabled;
976                 break;
977         case V4L2_CID_AUDIO_COMPRESSION_ENABLED:
978                 *property = SI4713_TX_ACOMP_ENABLE;
979                 *bit = 0;
980                 *mask = 1 << 0;
981                 *shadow = &sdev->acomp_info.enabled;
982                 break;
983         case V4L2_CID_PILOT_TONE_ENABLED:
984                 *property = SI4713_TX_COMPONENT_ENABLE;
985                 *bit = 0;
986                 *mask = 1 << 0;
987                 *shadow = &sdev->pilot_info.enabled;
988                 break;
989
990         case V4L2_CID_AUDIO_LIMITER_RELEASE_TIME:
991                 *property = SI4713_TX_LIMITER_RELEASE_TIME;
992                 *table = limiter_times;
993                 *size = ARRAY_SIZE(limiter_times);
994                 *shadow = &sdev->limiter_info.release_time;
995                 break;
996         case V4L2_CID_AUDIO_COMPRESSION_RELEASE_TIME:
997                 *property = SI4713_TX_ACOMP_RELEASE_TIME;
998                 *table = acomp_rtimes;
999                 *size = ARRAY_SIZE(acomp_rtimes);
1000                 *shadow = &sdev->acomp_info.release_time;
1001                 break;
1002         case V4L2_CID_TUNE_PREEMPHASIS:
1003                 *property = SI4713_TX_PREEMPHASIS;
1004                 *table = preemphasis_values;
1005                 *size = ARRAY_SIZE(preemphasis_values);
1006                 *shadow = &sdev->preemphasis;
1007                 break;
1008
1009         default:
1010                 rval = -EINVAL;
1011         };
1012
1013         return rval;
1014 }
1015
1016 static int si4713_queryctrl(struct v4l2_subdev *sd, struct v4l2_queryctrl *qc);
1017
1018 /* write string property */
1019 static int si4713_write_econtrol_string(struct si4713_device *sdev,
1020                                 struct v4l2_ext_control *control)
1021 {
1022         struct v4l2_queryctrl vqc;
1023         int len;
1024         s32 rval = 0;
1025
1026         vqc.id = control->id;
1027         rval = si4713_queryctrl(&sdev->sd, &vqc);
1028         if (rval < 0)
1029                 goto exit;
1030
1031         switch (control->id) {
1032         case V4L2_CID_RDS_TX_PS_NAME: {
1033                 char ps_name[MAX_RDS_PS_NAME + 1];
1034
1035                 len = control->size - 1;
1036                 if (len > MAX_RDS_PS_NAME) {
1037                         rval = -ERANGE;
1038                         goto exit;
1039                 }
1040                 rval = copy_from_user(ps_name, control->string, len);
1041                 if (rval) {
1042                         rval = -EFAULT;
1043                         goto exit;
1044                 }
1045                 ps_name[len] = '\0';
1046
1047                 if (strlen(ps_name) % vqc.step) {
1048                         rval = -ERANGE;
1049                         goto exit;
1050                 }
1051
1052                 rval = si4713_set_rds_ps_name(sdev, ps_name);
1053         }
1054                 break;
1055
1056         case V4L2_CID_RDS_TX_RADIO_TEXT: {
1057                 char radio_text[MAX_RDS_RADIO_TEXT + 1];
1058
1059                 len = control->size - 1;
1060                 if (len > MAX_RDS_RADIO_TEXT) {
1061                         rval = -ERANGE;
1062                         goto exit;
1063                 }
1064                 rval = copy_from_user(radio_text, control->string, len);
1065                 if (rval) {
1066                         rval = -EFAULT;
1067                         goto exit;
1068                 }
1069                 radio_text[len] = '\0';
1070
1071                 if (strlen(radio_text) % vqc.step) {
1072                         rval = -ERANGE;
1073                         goto exit;
1074                 }
1075
1076                 rval = si4713_set_rds_radio_text(sdev, radio_text);
1077         }
1078                 break;
1079
1080         default:
1081                 rval = -EINVAL;
1082                 break;
1083         };
1084
1085 exit:
1086         return rval;
1087 }
1088
1089 static int validate_range(struct v4l2_subdev *sd,
1090                                         struct v4l2_ext_control *control)
1091 {
1092         struct v4l2_queryctrl vqc;
1093         int rval;
1094
1095         vqc.id = control->id;
1096         rval = si4713_queryctrl(sd, &vqc);
1097         if (rval < 0)
1098                 goto exit;
1099
1100         if (control->value < vqc.minimum || control->value > vqc.maximum)
1101                 rval = -ERANGE;
1102
1103 exit:
1104         return rval;
1105 }
1106
1107 /* properties which use tx_tune_power*/
1108 static int si4713_write_econtrol_tune(struct si4713_device *sdev,
1109                                 struct v4l2_ext_control *control)
1110 {
1111         s32 rval = 0;
1112         u8 power, antcap;
1113
1114         rval = validate_range(&sdev->sd, control);
1115         if (rval < 0)
1116                 goto exit;
1117
1118         mutex_lock(&sdev->mutex);
1119
1120         switch (control->id) {
1121         case V4L2_CID_TUNE_POWER_LEVEL:
1122                 power = control->value;
1123                 antcap = sdev->antenna_capacitor;
1124                 break;
1125         case V4L2_CID_TUNE_ANTENNA_CAPACITOR:
1126                 power = sdev->power_level;
1127                 antcap = control->value;
1128                 break;
1129         default:
1130                 rval = -EINVAL;
1131                 goto unlock;
1132         };
1133
1134         if (sdev->power_state)
1135                 rval = si4713_tx_tune_power(sdev, power, antcap);
1136
1137         if (rval == 0) {
1138                 sdev->power_level = power;
1139                 sdev->antenna_capacitor = antcap;
1140         }
1141
1142 unlock:
1143         mutex_unlock(&sdev->mutex);
1144 exit:
1145         return rval;
1146 }
1147
1148 static int si4713_write_econtrol_integers(struct si4713_device *sdev,
1149                                         struct v4l2_ext_control *control)
1150 {
1151         s32 rval;
1152         u32 *shadow = NULL, val = 0;
1153         s32 bit = 0, mask = 0;
1154         u16 property = 0;
1155         int mul = 0;
1156         unsigned long *table = NULL;
1157         int size = 0;
1158
1159         rval = validate_range(&sdev->sd, control);
1160         if (rval < 0)
1161                 goto exit;
1162
1163         rval = si4713_choose_econtrol_action(sdev, control->id, &shadow, &bit,
1164                         &mask, &property, &mul, &table, &size);
1165         if (rval < 0)
1166                 goto exit;
1167
1168         val = control->value;
1169         if (mul) {
1170                 val = control->value / mul;
1171         } else if (table) {
1172                 rval = usecs_to_dev(control->value, table, size);
1173                 if (rval < 0)
1174                         goto exit;
1175                 val = rval;
1176                 rval = 0;
1177         }
1178
1179         mutex_lock(&sdev->mutex);
1180
1181         if (sdev->power_state) {
1182                 if (mask) {
1183                         rval = si4713_read_property(sdev, property, &val);
1184                         if (rval < 0)
1185                                 goto unlock;
1186                         val = set_bits(val, control->value, bit, mask);
1187                 }
1188
1189                 rval = si4713_write_property(sdev, property, val);
1190                 if (rval < 0)
1191                         goto unlock;
1192                 if (mask)
1193                         val = control->value;
1194         }
1195
1196         if (mul) {
1197                 *shadow = val * mul;
1198         } else if (table) {
1199                 rval = dev_to_usecs(val, table, size);
1200                 if (rval < 0)
1201                         goto unlock;
1202                 *shadow = rval;
1203                 rval = 0;
1204         } else {
1205                 *shadow = val;
1206         }
1207
1208 unlock:
1209         mutex_unlock(&sdev->mutex);
1210 exit:
1211         return rval;
1212 }
1213
1214 static int si4713_s_frequency(struct v4l2_subdev *sd, struct v4l2_frequency *f);
1215 static int si4713_s_modulator(struct v4l2_subdev *sd, struct v4l2_modulator *);
1216 /*
1217  * si4713_setup - Sets the device up with current configuration.
1218  * @sdev: si4713_device structure for the device we are communicating
1219  */
1220 static int si4713_setup(struct si4713_device *sdev)
1221 {
1222         struct v4l2_ext_control ctrl;
1223         struct v4l2_frequency f;
1224         struct v4l2_modulator vm;
1225         struct si4713_device *tmp;
1226         int rval = 0;
1227
1228         tmp = kmalloc(sizeof(*tmp), GFP_KERNEL);
1229         if (!tmp)
1230                 return -ENOMEM;
1231
1232         /* Get a local copy to avoid race */
1233         mutex_lock(&sdev->mutex);
1234         memcpy(tmp, sdev, sizeof(*sdev));
1235         mutex_unlock(&sdev->mutex);
1236
1237         ctrl.id = V4L2_CID_RDS_TX_PI;
1238         ctrl.value = tmp->rds_info.pi;
1239         rval |= si4713_write_econtrol_integers(sdev, &ctrl);
1240
1241         ctrl.id = V4L2_CID_AUDIO_COMPRESSION_THRESHOLD;
1242         ctrl.value = tmp->acomp_info.threshold;
1243         rval |= si4713_write_econtrol_integers(sdev, &ctrl);
1244
1245         ctrl.id = V4L2_CID_AUDIO_COMPRESSION_GAIN;
1246         ctrl.value = tmp->acomp_info.gain;
1247         rval |= si4713_write_econtrol_integers(sdev, &ctrl);
1248
1249         ctrl.id = V4L2_CID_PILOT_TONE_FREQUENCY;
1250         ctrl.value = tmp->pilot_info.frequency;
1251         rval |= si4713_write_econtrol_integers(sdev, &ctrl);
1252
1253         ctrl.id = V4L2_CID_AUDIO_COMPRESSION_ATTACK_TIME;
1254         ctrl.value = tmp->acomp_info.attack_time;
1255         rval |= si4713_write_econtrol_integers(sdev, &ctrl);
1256
1257         ctrl.id = V4L2_CID_PILOT_TONE_DEVIATION;
1258         ctrl.value = tmp->pilot_info.deviation;
1259         rval |= si4713_write_econtrol_integers(sdev, &ctrl);
1260
1261         ctrl.id = V4L2_CID_AUDIO_LIMITER_DEVIATION;
1262         ctrl.value = tmp->limiter_info.deviation;
1263         rval |= si4713_write_econtrol_integers(sdev, &ctrl);
1264
1265         ctrl.id = V4L2_CID_RDS_TX_DEVIATION;
1266         ctrl.value = tmp->rds_info.deviation;
1267         rval |= si4713_write_econtrol_integers(sdev, &ctrl);
1268
1269         ctrl.id = V4L2_CID_RDS_TX_PTY;
1270         ctrl.value = tmp->rds_info.pty;
1271         rval |= si4713_write_econtrol_integers(sdev, &ctrl);
1272
1273         ctrl.id = V4L2_CID_AUDIO_LIMITER_ENABLED;
1274         ctrl.value = tmp->limiter_info.enabled;
1275         rval |= si4713_write_econtrol_integers(sdev, &ctrl);
1276
1277         ctrl.id = V4L2_CID_AUDIO_COMPRESSION_ENABLED;
1278         ctrl.value = tmp->acomp_info.enabled;
1279         rval |= si4713_write_econtrol_integers(sdev, &ctrl);
1280
1281         ctrl.id = V4L2_CID_PILOT_TONE_ENABLED;
1282         ctrl.value = tmp->pilot_info.enabled;
1283         rval |= si4713_write_econtrol_integers(sdev, &ctrl);
1284
1285         ctrl.id = V4L2_CID_AUDIO_LIMITER_RELEASE_TIME;
1286         ctrl.value = tmp->limiter_info.release_time;
1287         rval |= si4713_write_econtrol_integers(sdev, &ctrl);
1288
1289         ctrl.id = V4L2_CID_AUDIO_COMPRESSION_RELEASE_TIME;
1290         ctrl.value = tmp->acomp_info.release_time;
1291         rval |= si4713_write_econtrol_integers(sdev, &ctrl);
1292
1293         ctrl.id = V4L2_CID_TUNE_PREEMPHASIS;
1294         ctrl.value = tmp->preemphasis;
1295         rval |= si4713_write_econtrol_integers(sdev, &ctrl);
1296
1297         ctrl.id = V4L2_CID_RDS_TX_PS_NAME;
1298         rval |= si4713_set_rds_ps_name(sdev, tmp->rds_info.ps_name);
1299
1300         ctrl.id = V4L2_CID_RDS_TX_RADIO_TEXT;
1301         rval |= si4713_set_rds_radio_text(sdev, tmp->rds_info.radio_text);
1302
1303         /* Device procedure needs to set frequency first */
1304         f.frequency = tmp->frequency ? tmp->frequency : DEFAULT_FREQUENCY;
1305         f.frequency = si4713_to_v4l2(f.frequency);
1306         rval |= si4713_s_frequency(&sdev->sd, &f);
1307
1308         ctrl.id = V4L2_CID_TUNE_POWER_LEVEL;
1309         ctrl.value = tmp->power_level;
1310         rval |= si4713_write_econtrol_tune(sdev, &ctrl);
1311
1312         ctrl.id = V4L2_CID_TUNE_ANTENNA_CAPACITOR;
1313         ctrl.value = tmp->antenna_capacitor;
1314         rval |= si4713_write_econtrol_tune(sdev, &ctrl);
1315
1316         vm.index = 0;
1317         if (tmp->stereo)
1318                 vm.txsubchans = V4L2_TUNER_SUB_STEREO;
1319         else
1320                 vm.txsubchans = V4L2_TUNER_SUB_MONO;
1321         if (tmp->rds_info.enabled)
1322                 vm.txsubchans |= V4L2_TUNER_SUB_RDS;
1323         si4713_s_modulator(&sdev->sd, &vm);
1324
1325         kfree(tmp);
1326
1327         return rval;
1328 }
1329
1330 /*
1331  * si4713_initialize - Sets the device up with default configuration.
1332  * @sdev: si4713_device structure for the device we are communicating
1333  */
1334 static int si4713_initialize(struct si4713_device *sdev)
1335 {
1336         int rval;
1337
1338         rval = si4713_set_power_state(sdev, POWER_ON);
1339         if (rval < 0)
1340                 goto exit;
1341
1342         rval = si4713_checkrev(sdev);
1343         if (rval < 0)
1344                 goto exit;
1345
1346         rval = si4713_set_power_state(sdev, POWER_OFF);
1347         if (rval < 0)
1348                 goto exit;
1349
1350         mutex_lock(&sdev->mutex);
1351
1352         sdev->rds_info.pi = DEFAULT_RDS_PI;
1353         sdev->rds_info.pty = DEFAULT_RDS_PTY;
1354         sdev->rds_info.deviation = DEFAULT_RDS_DEVIATION;
1355         strlcpy(sdev->rds_info.ps_name, DEFAULT_RDS_PS_NAME, MAX_RDS_PS_NAME);
1356         strlcpy(sdev->rds_info.radio_text, DEFAULT_RDS_RADIO_TEXT,
1357                                                         MAX_RDS_RADIO_TEXT);
1358         sdev->rds_info.enabled = 1;
1359
1360         sdev->limiter_info.release_time = DEFAULT_LIMITER_RTIME;
1361         sdev->limiter_info.deviation = DEFAULT_LIMITER_DEV;
1362         sdev->limiter_info.enabled = 1;
1363
1364         sdev->pilot_info.deviation = DEFAULT_PILOT_DEVIATION;
1365         sdev->pilot_info.frequency = DEFAULT_PILOT_FREQUENCY;
1366         sdev->pilot_info.enabled = 1;
1367
1368         sdev->acomp_info.release_time = DEFAULT_ACOMP_RTIME;
1369         sdev->acomp_info.attack_time = DEFAULT_ACOMP_ATIME;
1370         sdev->acomp_info.threshold = DEFAULT_ACOMP_THRESHOLD;
1371         sdev->acomp_info.gain = DEFAULT_ACOMP_GAIN;
1372         sdev->acomp_info.enabled = 1;
1373
1374         sdev->frequency = DEFAULT_FREQUENCY;
1375         sdev->preemphasis = DEFAULT_PREEMPHASIS;
1376         sdev->mute = DEFAULT_MUTE;
1377         sdev->power_level = DEFAULT_POWER_LEVEL;
1378         sdev->antenna_capacitor = 0;
1379         sdev->stereo = 1;
1380         sdev->tune_rnl = DEFAULT_TUNE_RNL;
1381
1382         mutex_unlock(&sdev->mutex);
1383
1384 exit:
1385         return rval;
1386 }
1387
1388 /* read string property */
1389 static int si4713_read_econtrol_string(struct si4713_device *sdev,
1390                                 struct v4l2_ext_control *control)
1391 {
1392         s32 rval = 0;
1393
1394         switch (control->id) {
1395         case V4L2_CID_RDS_TX_PS_NAME:
1396                 if (strlen(sdev->rds_info.ps_name) + 1 > control->size) {
1397                         control->size = MAX_RDS_PS_NAME + 1;
1398                         rval = -ENOSPC;
1399                         goto exit;
1400                 }
1401                 rval = copy_to_user(control->string, sdev->rds_info.ps_name,
1402                                         strlen(sdev->rds_info.ps_name) + 1);
1403                 if (rval)
1404                         rval = -EFAULT;
1405                 break;
1406
1407         case V4L2_CID_RDS_TX_RADIO_TEXT:
1408                 if (strlen(sdev->rds_info.radio_text) + 1 > control->size) {
1409                         control->size = MAX_RDS_RADIO_TEXT + 1;
1410                         rval = -ENOSPC;
1411                         goto exit;
1412                 }
1413                 rval = copy_to_user(control->string, sdev->rds_info.radio_text,
1414                                         strlen(sdev->rds_info.radio_text) + 1);
1415                 if (rval)
1416                         rval = -EFAULT;
1417                 break;
1418
1419         default:
1420                 rval = -EINVAL;
1421                 break;
1422         };
1423
1424 exit:
1425         return rval;
1426 }
1427
1428 /*
1429  * si4713_update_tune_status - update properties from tx_tune_status
1430  * command. Must be called with sdev->mutex held.
1431  * @sdev: si4713_device structure for the device we are communicating
1432  */
1433 static int si4713_update_tune_status(struct si4713_device *sdev)
1434 {
1435         int rval;
1436         u16 f = 0;
1437         u8 p = 0, a = 0, n = 0;
1438
1439         rval = si4713_tx_tune_status(sdev, 0x00, &f, &p, &a, &n);
1440
1441         if (rval < 0)
1442                 goto exit;
1443
1444         sdev->power_level = p;
1445         sdev->antenna_capacitor = a;
1446         sdev->tune_rnl = n;
1447
1448 exit:
1449         return rval;
1450 }
1451
1452 /* properties which use tx_tune_status */
1453 static int si4713_read_econtrol_tune(struct si4713_device *sdev,
1454                                 struct v4l2_ext_control *control)
1455 {
1456         s32 rval = 0;
1457
1458         mutex_lock(&sdev->mutex);
1459
1460         if (sdev->power_state) {
1461                 rval = si4713_update_tune_status(sdev);
1462                 if (rval < 0)
1463                         goto unlock;
1464         }
1465
1466         switch (control->id) {
1467         case V4L2_CID_TUNE_POWER_LEVEL:
1468                 control->value = sdev->power_level;
1469                 break;
1470         case V4L2_CID_TUNE_ANTENNA_CAPACITOR:
1471                 control->value = sdev->antenna_capacitor;
1472                 break;
1473         default:
1474                 rval = -EINVAL;
1475         };
1476
1477 unlock:
1478         mutex_unlock(&sdev->mutex);
1479         return rval;
1480 }
1481
1482 static int si4713_read_econtrol_integers(struct si4713_device *sdev,
1483                                 struct v4l2_ext_control *control)
1484 {
1485         s32 rval;
1486         u32 *shadow = NULL, val = 0;
1487         s32 bit = 0, mask = 0;
1488         u16 property = 0;
1489         int mul = 0;
1490         unsigned long *table = NULL;
1491         int size = 0;
1492
1493         rval = si4713_choose_econtrol_action(sdev, control->id, &shadow, &bit,
1494                         &mask, &property, &mul, &table, &size);
1495         if (rval < 0)
1496                 goto exit;
1497
1498         mutex_lock(&sdev->mutex);
1499
1500         if (sdev->power_state) {
1501                 rval = si4713_read_property(sdev, property, &val);
1502                 if (rval < 0)
1503                         goto unlock;
1504
1505                 /* Keep negative values for threshold */
1506                 if (control->id == V4L2_CID_AUDIO_COMPRESSION_THRESHOLD)
1507                         *shadow = (s16)val;
1508                 else if (mask)
1509                         *shadow = get_status_bit(val, bit, mask);
1510                 else if (mul)
1511                         *shadow = val * mul;
1512                 else
1513                         *shadow = dev_to_usecs(val, table, size);
1514         }
1515
1516         control->value = *shadow;
1517
1518 unlock:
1519         mutex_unlock(&sdev->mutex);
1520 exit:
1521         return rval;
1522 }
1523
1524 /*
1525  * Video4Linux Subdev Interface
1526  */
1527 /* si4713_s_ext_ctrls - set extended controls value */
1528 static int si4713_s_ext_ctrls(struct v4l2_subdev *sd,
1529                                 struct v4l2_ext_controls *ctrls)
1530 {
1531         struct si4713_device *sdev = to_si4713_device(sd);
1532         int i;
1533
1534         if (ctrls->ctrl_class != V4L2_CTRL_CLASS_FM_TX)
1535                 return -EINVAL;
1536
1537         for (i = 0; i < ctrls->count; i++) {
1538                 int err;
1539
1540                 switch ((ctrls->controls + i)->id) {
1541                 case V4L2_CID_RDS_TX_PS_NAME:
1542                 case V4L2_CID_RDS_TX_RADIO_TEXT:
1543                         err = si4713_write_econtrol_string(sdev,
1544                                                         ctrls->controls + i);
1545                         break;
1546                 case V4L2_CID_TUNE_ANTENNA_CAPACITOR:
1547                 case V4L2_CID_TUNE_POWER_LEVEL:
1548                         err = si4713_write_econtrol_tune(sdev,
1549                                                         ctrls->controls + i);
1550                         break;
1551                 default:
1552                         err = si4713_write_econtrol_integers(sdev,
1553                                                         ctrls->controls + i);
1554                 }
1555
1556                 if (err < 0) {
1557                         ctrls->error_idx = i;
1558                         return err;
1559                 }
1560         }
1561
1562         return 0;
1563 }
1564
1565 /* si4713_g_ext_ctrls - get extended controls value */
1566 static int si4713_g_ext_ctrls(struct v4l2_subdev *sd,
1567                                 struct v4l2_ext_controls *ctrls)
1568 {
1569         struct si4713_device *sdev = to_si4713_device(sd);
1570         int i;
1571
1572         if (ctrls->ctrl_class != V4L2_CTRL_CLASS_FM_TX)
1573                 return -EINVAL;
1574
1575         for (i = 0; i < ctrls->count; i++) {
1576                 int err;
1577
1578                 switch ((ctrls->controls + i)->id) {
1579                 case V4L2_CID_RDS_TX_PS_NAME:
1580                 case V4L2_CID_RDS_TX_RADIO_TEXT:
1581                         err = si4713_read_econtrol_string(sdev,
1582                                                         ctrls->controls + i);
1583                         break;
1584                 case V4L2_CID_TUNE_ANTENNA_CAPACITOR:
1585                 case V4L2_CID_TUNE_POWER_LEVEL:
1586                         err = si4713_read_econtrol_tune(sdev,
1587                                                         ctrls->controls + i);
1588                         break;
1589                 default:
1590                         err = si4713_read_econtrol_integers(sdev,
1591                                                         ctrls->controls + i);
1592                 }
1593
1594                 if (err < 0) {
1595                         ctrls->error_idx = i;
1596                         return err;
1597                 }
1598         }
1599
1600         return 0;
1601 }
1602
1603 /* si4713_queryctrl - enumerate control items */
1604 static int si4713_queryctrl(struct v4l2_subdev *sd, struct v4l2_queryctrl *qc)
1605 {
1606         int rval = 0;
1607
1608         switch (qc->id) {
1609         /* User class controls */
1610         case V4L2_CID_AUDIO_MUTE:
1611                 rval = v4l2_ctrl_query_fill(qc, 0, 1, 1, DEFAULT_MUTE);
1612                 break;
1613         /* FM_TX class controls */
1614         case V4L2_CID_RDS_TX_PI:
1615                 rval = v4l2_ctrl_query_fill(qc, 0, 0xFFFF, 1, DEFAULT_RDS_PI);
1616                 break;
1617         case V4L2_CID_RDS_TX_PTY:
1618                 rval = v4l2_ctrl_query_fill(qc, 0, 31, 1, DEFAULT_RDS_PTY);
1619                 break;
1620         case V4L2_CID_RDS_TX_DEVIATION:
1621                 rval = v4l2_ctrl_query_fill(qc, 0, MAX_RDS_DEVIATION,
1622                                                 10, DEFAULT_RDS_DEVIATION);
1623                 break;
1624         case V4L2_CID_RDS_TX_PS_NAME:
1625                 /*
1626                  * Report step as 8. From RDS spec, psname
1627                  * should be 8. But there are receivers which scroll strings
1628                  * sized as 8xN.
1629                  */
1630                 rval = v4l2_ctrl_query_fill(qc, 0, MAX_RDS_PS_NAME, 8, 0);
1631                 break;
1632         case V4L2_CID_RDS_TX_RADIO_TEXT:
1633                 /*
1634                  * Report step as 32 (2A block). From RDS spec,
1635                  * radio text should be 32 for 2A block. But there are receivers
1636                  * which scroll strings sized as 32xN. Setting default to 32.
1637                  */
1638                 rval = v4l2_ctrl_query_fill(qc, 0, MAX_RDS_RADIO_TEXT, 32, 0);
1639                 break;
1640
1641         case V4L2_CID_AUDIO_LIMITER_ENABLED:
1642                 rval = v4l2_ctrl_query_fill(qc, 0, 1, 1, 1);
1643                 break;
1644         case V4L2_CID_AUDIO_LIMITER_RELEASE_TIME:
1645                 rval = v4l2_ctrl_query_fill(qc, 250, MAX_LIMITER_RELEASE_TIME,
1646                                                 50, DEFAULT_LIMITER_RTIME);
1647                 break;
1648         case V4L2_CID_AUDIO_LIMITER_DEVIATION:
1649                 rval = v4l2_ctrl_query_fill(qc, 0, MAX_LIMITER_DEVIATION,
1650                                                 10, DEFAULT_LIMITER_DEV);
1651                 break;
1652
1653         case V4L2_CID_AUDIO_COMPRESSION_ENABLED:
1654                 rval = v4l2_ctrl_query_fill(qc, 0, 1, 1, 1);
1655                 break;
1656         case V4L2_CID_AUDIO_COMPRESSION_GAIN:
1657                 rval = v4l2_ctrl_query_fill(qc, 0, MAX_ACOMP_GAIN, 1,
1658                                                 DEFAULT_ACOMP_GAIN);
1659                 break;
1660         case V4L2_CID_AUDIO_COMPRESSION_THRESHOLD:
1661                 rval = v4l2_ctrl_query_fill(qc, MIN_ACOMP_THRESHOLD,
1662                                                 MAX_ACOMP_THRESHOLD, 1,
1663                                                 DEFAULT_ACOMP_THRESHOLD);
1664                 break;
1665         case V4L2_CID_AUDIO_COMPRESSION_ATTACK_TIME:
1666                 rval = v4l2_ctrl_query_fill(qc, 0, MAX_ACOMP_ATTACK_TIME,
1667                                                 500, DEFAULT_ACOMP_ATIME);
1668                 break;
1669         case V4L2_CID_AUDIO_COMPRESSION_RELEASE_TIME:
1670                 rval = v4l2_ctrl_query_fill(qc, 100000, MAX_ACOMP_RELEASE_TIME,
1671                                                 100000, DEFAULT_ACOMP_RTIME);
1672                 break;
1673
1674         case V4L2_CID_PILOT_TONE_ENABLED:
1675                 rval = v4l2_ctrl_query_fill(qc, 0, 1, 1, 1);
1676                 break;
1677         case V4L2_CID_PILOT_TONE_DEVIATION:
1678                 rval = v4l2_ctrl_query_fill(qc, 0, MAX_PILOT_DEVIATION,
1679                                                 10, DEFAULT_PILOT_DEVIATION);
1680                 break;
1681         case V4L2_CID_PILOT_TONE_FREQUENCY:
1682                 rval = v4l2_ctrl_query_fill(qc, 0, MAX_PILOT_FREQUENCY,
1683                                                 1, DEFAULT_PILOT_FREQUENCY);
1684                 break;
1685
1686         case V4L2_CID_TUNE_PREEMPHASIS:
1687                 rval = v4l2_ctrl_query_fill(qc, V4L2_PREEMPHASIS_DISABLED,
1688                                                 V4L2_PREEMPHASIS_75_uS, 1,
1689                                                 V4L2_PREEMPHASIS_50_uS);
1690                 break;
1691         case V4L2_CID_TUNE_POWER_LEVEL:
1692                 rval = v4l2_ctrl_query_fill(qc, 0, 120, 1, DEFAULT_POWER_LEVEL);
1693                 break;
1694         case V4L2_CID_TUNE_ANTENNA_CAPACITOR:
1695                 rval = v4l2_ctrl_query_fill(qc, 0, 191, 1, 0);
1696                 break;
1697         default:
1698                 rval = -EINVAL;
1699                 break;
1700         };
1701
1702         return rval;
1703 }
1704
1705 /* si4713_g_ctrl - get the value of a control */
1706 static int si4713_g_ctrl(struct v4l2_subdev *sd, struct v4l2_control *ctrl)
1707 {
1708         struct si4713_device *sdev = to_si4713_device(sd);
1709         int rval = 0;
1710
1711         if (!sdev)
1712                 return -ENODEV;
1713
1714         mutex_lock(&sdev->mutex);
1715
1716         if (sdev->power_state) {
1717                 rval = si4713_read_property(sdev, SI4713_TX_LINE_INPUT_MUTE,
1718                                                 &sdev->mute);
1719
1720                 if (rval < 0)
1721                         goto unlock;
1722         }
1723
1724         switch (ctrl->id) {
1725         case V4L2_CID_AUDIO_MUTE:
1726                 ctrl->value = get_mute(sdev->mute);
1727                 break;
1728         }
1729
1730 unlock:
1731         mutex_unlock(&sdev->mutex);
1732         return rval;
1733 }
1734
1735 /* si4713_s_ctrl - set the value of a control */
1736 static int si4713_s_ctrl(struct v4l2_subdev *sd, struct v4l2_control *ctrl)
1737 {
1738         struct si4713_device *sdev = to_si4713_device(sd);
1739         int rval = 0;
1740
1741         if (!sdev)
1742                 return -ENODEV;
1743
1744         switch (ctrl->id) {
1745         case V4L2_CID_AUDIO_MUTE:
1746                 if (ctrl->value) {
1747                         rval = si4713_set_mute(sdev, ctrl->value);
1748                         if (rval < 0)
1749                                 goto exit;
1750
1751                         rval = si4713_set_power_state(sdev, POWER_DOWN);
1752                 } else {
1753                         rval = si4713_set_power_state(sdev, POWER_UP);
1754                         if (rval < 0)
1755                                 goto exit;
1756
1757                         rval = si4713_setup(sdev);
1758                         if (rval < 0)
1759                                 goto exit;
1760
1761                         rval = si4713_set_mute(sdev, ctrl->value);
1762                 }
1763                 break;
1764         }
1765
1766 exit:
1767         return rval;
1768 }
1769
1770 /* si4713_ioctl - deal with private ioctls (only rnl for now) */
1771 long si4713_ioctl(struct v4l2_subdev *sd, unsigned int cmd, void *arg)
1772 {
1773         struct si4713_device *sdev = to_si4713_device(sd);
1774         struct si4713_rnl *rnl = arg;
1775         u16 frequency;
1776         int rval = 0;
1777
1778         if (!arg)
1779                 return -EINVAL;
1780
1781         mutex_lock(&sdev->mutex);
1782         switch (cmd) {
1783         case SI4713_IOC_MEASURE_RNL:
1784                 frequency = v4l2_to_si4713(rnl->frequency);
1785
1786                 if (sdev->power_state) {
1787                         /* Set desired measurement frequency */
1788                         rval = si4713_tx_tune_measure(sdev, frequency, 0);
1789                         if (rval < 0)
1790                                 goto unlock;
1791                         /* get results from tune status */
1792                         rval = si4713_update_tune_status(sdev);
1793                         if (rval < 0)
1794                                 goto unlock;
1795                 }
1796                 rnl->rnl = sdev->tune_rnl;
1797                 break;
1798
1799         default:
1800                 /* nothing */
1801                 rval = -ENOIOCTLCMD;
1802         }
1803
1804 unlock:
1805         mutex_unlock(&sdev->mutex);
1806         return rval;
1807 }
1808
1809 static const struct v4l2_subdev_core_ops si4713_subdev_core_ops = {
1810         .queryctrl      = si4713_queryctrl,
1811         .g_ext_ctrls    = si4713_g_ext_ctrls,
1812         .s_ext_ctrls    = si4713_s_ext_ctrls,
1813         .g_ctrl         = si4713_g_ctrl,
1814         .s_ctrl         = si4713_s_ctrl,
1815         .ioctl          = si4713_ioctl,
1816 };
1817
1818 /* si4713_g_modulator - get modulator attributes */
1819 static int si4713_g_modulator(struct v4l2_subdev *sd, struct v4l2_modulator *vm)
1820 {
1821         struct si4713_device *sdev = to_si4713_device(sd);
1822         int rval = 0;
1823
1824         if (!sdev) {
1825                 rval = -ENODEV;
1826                 goto exit;
1827         }
1828
1829         if (vm->index > 0) {
1830                 rval = -EINVAL;
1831                 goto exit;
1832         }
1833
1834         strncpy(vm->name, "FM Modulator", 32);
1835         vm->capability = V4L2_TUNER_CAP_STEREO | V4L2_TUNER_CAP_LOW |
1836                 V4L2_TUNER_CAP_RDS | V4L2_TUNER_CAP_RDS_CONTROLS;
1837
1838         /* Report current frequency range limits */
1839         vm->rangelow = si4713_to_v4l2(FREQ_RANGE_LOW);
1840         vm->rangehigh = si4713_to_v4l2(FREQ_RANGE_HIGH);
1841
1842         mutex_lock(&sdev->mutex);
1843
1844         if (sdev->power_state) {
1845                 u32 comp_en = 0;
1846
1847                 rval = si4713_read_property(sdev, SI4713_TX_COMPONENT_ENABLE,
1848                                                 &comp_en);
1849                 if (rval < 0)
1850                         goto unlock;
1851
1852                 sdev->stereo = get_status_bit(comp_en, 1, 1 << 1);
1853                 sdev->rds_info.enabled = get_status_bit(comp_en, 2, 1 << 2);
1854         }
1855
1856         /* Report current audio mode: mono or stereo */
1857         if (sdev->stereo)
1858                 vm->txsubchans = V4L2_TUNER_SUB_STEREO;
1859         else
1860                 vm->txsubchans = V4L2_TUNER_SUB_MONO;
1861
1862         /* Report rds feature status */
1863         if (sdev->rds_info.enabled)
1864                 vm->txsubchans |= V4L2_TUNER_SUB_RDS;
1865         else
1866                 vm->txsubchans &= ~V4L2_TUNER_SUB_RDS;
1867
1868 unlock:
1869         mutex_unlock(&sdev->mutex);
1870 exit:
1871         return rval;
1872 }
1873
1874 /* si4713_s_modulator - set modulator attributes */
1875 static int si4713_s_modulator(struct v4l2_subdev *sd, struct v4l2_modulator *vm)
1876 {
1877         struct si4713_device *sdev = to_si4713_device(sd);
1878         int rval = 0;
1879         u16 stereo, rds;
1880         u32 p;
1881
1882         if (!sdev)
1883                 return -ENODEV;
1884
1885         if (vm->index > 0)
1886                 return -EINVAL;
1887
1888         /* Set audio mode: mono or stereo */
1889         if (vm->txsubchans & V4L2_TUNER_SUB_STEREO)
1890                 stereo = 1;
1891         else if (vm->txsubchans & V4L2_TUNER_SUB_MONO)
1892                 stereo = 0;
1893         else
1894                 return -EINVAL;
1895
1896         rds = !!(vm->txsubchans & V4L2_TUNER_SUB_RDS);
1897
1898         mutex_lock(&sdev->mutex);
1899
1900         if (sdev->power_state) {
1901                 rval = si4713_read_property(sdev,
1902                                                 SI4713_TX_COMPONENT_ENABLE, &p);
1903                 if (rval < 0)
1904                         goto unlock;
1905
1906                 p = set_bits(p, stereo, 1, 1 << 1);
1907                 p = set_bits(p, rds, 2, 1 << 2);
1908
1909                 rval = si4713_write_property(sdev,
1910                                                 SI4713_TX_COMPONENT_ENABLE, p);
1911                 if (rval < 0)
1912                         goto unlock;
1913         }
1914
1915         sdev->stereo = stereo;
1916         sdev->rds_info.enabled = rds;
1917
1918 unlock:
1919         mutex_unlock(&sdev->mutex);
1920         return rval;
1921 }
1922
1923 /* si4713_g_frequency - get tuner or modulator radio frequency */
1924 static int si4713_g_frequency(struct v4l2_subdev *sd, struct v4l2_frequency *f)
1925 {
1926         struct si4713_device *sdev = to_si4713_device(sd);
1927         int rval = 0;
1928
1929         f->type = V4L2_TUNER_RADIO;
1930
1931         mutex_lock(&sdev->mutex);
1932
1933         if (sdev->power_state) {
1934                 u16 freq;
1935                 u8 p, a, n;
1936
1937                 rval = si4713_tx_tune_status(sdev, 0x00, &freq, &p, &a, &n);
1938                 if (rval < 0)
1939                         goto unlock;
1940
1941                 sdev->frequency = freq;
1942         }
1943
1944         f->frequency = si4713_to_v4l2(sdev->frequency);
1945
1946 unlock:
1947         mutex_unlock(&sdev->mutex);
1948         return rval;
1949 }
1950
1951 /* si4713_s_frequency - set tuner or modulator radio frequency */
1952 static int si4713_s_frequency(struct v4l2_subdev *sd, struct v4l2_frequency *f)
1953 {
1954         struct si4713_device *sdev = to_si4713_device(sd);
1955         int rval = 0;
1956         u16 frequency = v4l2_to_si4713(f->frequency);
1957
1958         /* Check frequency range */
1959         if (frequency < FREQ_RANGE_LOW || frequency > FREQ_RANGE_HIGH)
1960                 return -EDOM;
1961
1962         mutex_lock(&sdev->mutex);
1963
1964         if (sdev->power_state) {
1965                 rval = si4713_tx_tune_freq(sdev, frequency);
1966                 if (rval < 0)
1967                         goto unlock;
1968                 frequency = rval;
1969                 rval = 0;
1970         }
1971         sdev->frequency = frequency;
1972         f->frequency = si4713_to_v4l2(frequency);
1973
1974 unlock:
1975         mutex_unlock(&sdev->mutex);
1976         return rval;
1977 }
1978
1979 static const struct v4l2_subdev_tuner_ops si4713_subdev_tuner_ops = {
1980         .g_frequency    = si4713_g_frequency,
1981         .s_frequency    = si4713_s_frequency,
1982         .g_modulator    = si4713_g_modulator,
1983         .s_modulator    = si4713_s_modulator,
1984 };
1985
1986 static const struct v4l2_subdev_ops si4713_subdev_ops = {
1987         .core           = &si4713_subdev_core_ops,
1988         .tuner          = &si4713_subdev_tuner_ops,
1989 };
1990
1991 /*
1992  * I2C driver interface
1993  */
1994 /* si4713_probe - probe for the device */
1995 static int si4713_probe(struct i2c_client *client,
1996                                         const struct i2c_device_id *id)
1997 {
1998         struct si4713_device *sdev;
1999         struct si4713_platform_data *pdata = client->dev.platform_data;
2000         int rval, i;
2001
2002         sdev = kzalloc(sizeof *sdev, GFP_KERNEL);
2003         if (!sdev) {
2004                 dev_err(&client->dev, "Failed to alloc video device.\n");
2005                 rval = -ENOMEM;
2006                 goto exit;
2007         }
2008
2009         sdev->gpio_reset = -1;
2010         if (pdata && gpio_is_valid(pdata->gpio_reset)) {
2011                 rval = gpio_request(pdata->gpio_reset, "si4713 reset");
2012                 if (rval) {
2013                         dev_err(&client->dev,
2014                                 "Failed to request gpio: %d\n", rval);
2015                         goto free_sdev;
2016                 }
2017                 sdev->gpio_reset = pdata->gpio_reset;
2018                 gpio_direction_output(sdev->gpio_reset, 0);
2019         }
2020
2021         for (i = 0; i < ARRAY_SIZE(sdev->supplies); i++)
2022                 sdev->supplies[i].supply = si4713_supply_names[i];
2023
2024         rval = regulator_bulk_get(&client->dev, ARRAY_SIZE(sdev->supplies),
2025                                   sdev->supplies);
2026         if (rval) {
2027                 dev_err(&client->dev, "Cannot get regulators: %d\n", rval);
2028                 goto free_gpio;
2029         }
2030
2031         v4l2_i2c_subdev_init(&sdev->sd, client, &si4713_subdev_ops);
2032
2033         mutex_init(&sdev->mutex);
2034         init_completion(&sdev->work);
2035
2036         if (client->irq) {
2037                 rval = request_irq(client->irq,
2038                         si4713_handler, IRQF_TRIGGER_FALLING | IRQF_DISABLED,
2039                         client->name, sdev);
2040                 if (rval < 0) {
2041                         v4l2_err(&sdev->sd, "Could not request IRQ\n");
2042                         goto put_reg;
2043                 }
2044                 v4l2_dbg(1, debug, &sdev->sd, "IRQ requested.\n");
2045         } else {
2046                 v4l2_warn(&sdev->sd, "IRQ not configured. Using timeouts.\n");
2047         }
2048
2049         rval = si4713_initialize(sdev);
2050         if (rval < 0) {
2051                 v4l2_err(&sdev->sd, "Failed to probe device information.\n");
2052                 goto free_irq;
2053         }
2054
2055         return 0;
2056
2057 free_irq:
2058         if (client->irq)
2059                 free_irq(client->irq, sdev);
2060 put_reg:
2061         regulator_bulk_free(ARRAY_SIZE(sdev->supplies), sdev->supplies);
2062 free_gpio:
2063         if (gpio_is_valid(sdev->gpio_reset))
2064                 gpio_free(sdev->gpio_reset);
2065 free_sdev:
2066         kfree(sdev);
2067 exit:
2068         return rval;
2069 }
2070
2071 /* si4713_remove - remove the device */
2072 static int si4713_remove(struct i2c_client *client)
2073 {
2074         struct v4l2_subdev *sd = i2c_get_clientdata(client);
2075         struct si4713_device *sdev = to_si4713_device(sd);
2076
2077         if (sdev->power_state)
2078                 si4713_set_power_state(sdev, POWER_DOWN);
2079
2080         if (client->irq > 0)
2081                 free_irq(client->irq, sdev);
2082
2083         v4l2_device_unregister_subdev(sd);
2084         regulator_bulk_free(ARRAY_SIZE(sdev->supplies), sdev->supplies);
2085         if (gpio_is_valid(sdev->gpio_reset))
2086                 gpio_free(sdev->gpio_reset);
2087         kfree(sdev);
2088
2089         return 0;
2090 }
2091
2092 /* si4713_i2c_driver - i2c driver interface */
2093 static const struct i2c_device_id si4713_id[] = {
2094         { "si4713" , 0 },
2095         { },
2096 };
2097 MODULE_DEVICE_TABLE(i2c, si4713_id);
2098
2099 static struct i2c_driver si4713_i2c_driver = {
2100         .driver         = {
2101                 .name   = "si4713",
2102         },
2103         .probe          = si4713_probe,
2104         .remove         = si4713_remove,
2105         .id_table       = si4713_id,
2106 };
2107
2108 /* Module Interface */
2109 static int __init si4713_module_init(void)
2110 {
2111         return i2c_add_driver(&si4713_i2c_driver);
2112 }
2113
2114 static void __exit si4713_module_exit(void)
2115 {
2116         i2c_del_driver(&si4713_i2c_driver);
2117 }
2118
2119 module_init(si4713_module_init);
2120 module_exit(si4713_module_exit);
2121