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