[media] si4713: Modified i2c driver to handle cases where interrupts are not used
[linux-2.6-microblaze.git] / drivers / media / radio / si4713 / si4713.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/completion.h>
25 #include <linux/delay.h>
26 #include <linux/interrupt.h>
27 #include <linux/i2c.h>
28 #include <linux/slab.h>
29 #include <linux/gpio.h>
30 #include <linux/module.h>
31 #include <media/v4l2-device.h>
32 #include <media/v4l2-ioctl.h>
33 #include <media/v4l2-common.h>
34 #include <linux/regulator/consumer.h>
35
36 #include "si4713.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_DEVIATION           0x00C8
56 #define DEFAULT_RDS_PS_REPEAT_COUNT     0x0003
57 #define DEFAULT_LIMITER_RTIME           0x1392
58 #define DEFAULT_LIMITER_DEV             0x102CA
59 #define DEFAULT_PILOT_FREQUENCY         0x4A38
60 #define DEFAULT_PILOT_DEVIATION         0x1A5E
61 #define DEFAULT_ACOMP_ATIME             0x0000
62 #define DEFAULT_ACOMP_RTIME             0xF4240L
63 #define DEFAULT_ACOMP_GAIN              0x0F
64 #define DEFAULT_ACOMP_THRESHOLD         (-0x28)
65 #define DEFAULT_MUTE                    0x01
66 #define DEFAULT_POWER_LEVEL             88
67 #define DEFAULT_FREQUENCY               8800
68 #define DEFAULT_PREEMPHASIS             FMPE_EU
69 #define DEFAULT_TUNE_RNL                0xFF
70
71 #define to_si4713_device(sd)    container_of(sd, struct si4713_device, sd)
72
73 /* frequency domain transformation (using times 10 to avoid floats) */
74 #define FREQDEV_UNIT    100000
75 #define FREQV4L2_MULTI  625
76 #define si4713_to_v4l2(f)       ((f * FREQDEV_UNIT) / FREQV4L2_MULTI)
77 #define v4l2_to_si4713(f)       ((f * FREQV4L2_MULTI) / FREQDEV_UNIT)
78 #define FREQ_RANGE_LOW                  7600
79 #define FREQ_RANGE_HIGH                 10800
80
81 #define MAX_ARGS 7
82
83 #define RDS_BLOCK                       8
84 #define RDS_BLOCK_CLEAR                 0x03
85 #define RDS_BLOCK_LOAD                  0x04
86 #define RDS_RADIOTEXT_2A                0x20
87 #define RDS_RADIOTEXT_BLK_SIZE          4
88 #define RDS_RADIOTEXT_INDEX_MAX         0x0F
89 #define RDS_CARRIAGE_RETURN             0x0D
90
91 #define rds_ps_nblocks(len)     ((len / RDS_BLOCK) + (len % RDS_BLOCK ? 1 : 0))
92
93 #define get_status_bit(p, b, m) (((p) & (m)) >> (b))
94 #define set_bits(p, v, b, m)    (((p) & ~(m)) | ((v) << (b)))
95
96 #define ATTACK_TIME_UNIT        500
97
98 #define POWER_OFF                       0x00
99 #define POWER_ON                        0x01
100
101 #define msb(x)                  ((u8)((u16) x >> 8))
102 #define lsb(x)                  ((u8)((u16) x &  0x00FF))
103 #define compose_u16(msb, lsb)   (((u16)msb << 8) | lsb)
104 #define check_command_failed(status)    (!(status & SI4713_CTS) || \
105                                         (status & SI4713_ERR))
106 /* mute definition */
107 #define set_mute(p)     ((p & 1) | ((p & 1) << 1));
108
109 #ifdef DEBUG
110 #define DBG_BUFFER(device, message, buffer, size)                       \
111         {                                                               \
112                 int i;                                                  \
113                 char str[(size)*5];                                     \
114                 for (i = 0; i < size; i++)                              \
115                         sprintf(str + i * 5, " 0x%02x", buffer[i]);     \
116                 v4l2_dbg(2, debug, device, "%s:%s\n", message, str);    \
117         }
118 #else
119 #define DBG_BUFFER(device, message, buffer, size)
120 #endif
121
122 /*
123  * Values for limiter release time (sorted by second column)
124  *      device  release
125  *      value   time (us)
126  */
127 static long limiter_times[] = {
128         2000,   250,
129         1000,   500,
130         510,    1000,
131         255,    2000,
132         170,    3000,
133         127,    4020,
134         102,    5010,
135         85,     6020,
136         73,     7010,
137         64,     7990,
138         57,     8970,
139         51,     10030,
140         25,     20470,
141         17,     30110,
142         13,     39380,
143         10,     51190,
144         8,      63690,
145         7,      73140,
146         6,      85330,
147         5,      102390,
148 };
149
150 /*
151  * Values for audio compression release time (sorted by second column)
152  *      device  release
153  *      value   time (us)
154  */
155 static unsigned long acomp_rtimes[] = {
156         0,      100000,
157         1,      200000,
158         2,      350000,
159         3,      525000,
160         4,      1000000,
161 };
162
163 /*
164  * Values for preemphasis (sorted by second column)
165  *      device  preemphasis
166  *      value   value (v4l2)
167  */
168 static unsigned long preemphasis_values[] = {
169         FMPE_DISABLED,  V4L2_PREEMPHASIS_DISABLED,
170         FMPE_EU,        V4L2_PREEMPHASIS_50_uS,
171         FMPE_USA,       V4L2_PREEMPHASIS_75_uS,
172 };
173
174 static int usecs_to_dev(unsigned long usecs, unsigned long const array[],
175                         int size)
176 {
177         int i;
178         int rval = -EINVAL;
179
180         for (i = 0; i < size / 2; i++)
181                 if (array[(i * 2) + 1] >= usecs) {
182                         rval = array[i * 2];
183                         break;
184                 }
185
186         return rval;
187 }
188
189 /* si4713_handler: IRQ handler, just complete work */
190 static irqreturn_t si4713_handler(int irq, void *dev)
191 {
192         struct si4713_device *sdev = dev;
193
194         v4l2_dbg(2, debug, &sdev->sd,
195                         "%s: sending signal to completion work.\n", __func__);
196         complete(&sdev->work);
197
198         return IRQ_HANDLED;
199 }
200
201 /*
202  * si4713_send_command - sends a command to si4713 and waits its response
203  * @sdev: si4713_device structure for the device we are communicating
204  * @command: command id
205  * @args: command arguments we are sending (up to 7)
206  * @argn: actual size of @args
207  * @response: buffer to place the expected response from the device (up to 15)
208  * @respn: actual size of @response
209  * @usecs: amount of time to wait before reading the response (in usecs)
210  */
211 static int si4713_send_command(struct si4713_device *sdev, const u8 command,
212                                 const u8 args[], const int argn,
213                                 u8 response[], const int respn, const int usecs)
214 {
215         struct i2c_client *client = v4l2_get_subdevdata(&sdev->sd);
216         unsigned long until_jiffies;
217         u8 data1[MAX_ARGS + 1];
218         int err;
219
220         if (!client->adapter)
221                 return -ENODEV;
222
223         /* First send the command and its arguments */
224         data1[0] = command;
225         memcpy(data1 + 1, args, argn);
226         DBG_BUFFER(&sdev->sd, "Parameters", data1, argn + 1);
227
228         err = i2c_master_send(client, data1, argn + 1);
229         if (err != argn + 1) {
230                 v4l2_err(&sdev->sd, "Error while sending command 0x%02x\n",
231                         command);
232                 return err < 0 ? err : -EIO;
233         }
234
235         until_jiffies = jiffies + usecs_to_jiffies(usecs) + 1;
236
237         /* Wait response from interrupt */
238         if (client->irq) {
239                 if (!wait_for_completion_timeout(&sdev->work,
240                                 usecs_to_jiffies(usecs) + 1))
241                         v4l2_warn(&sdev->sd,
242                                 "(%s) Device took too much time to answer.\n",
243                                 __func__);
244         }
245
246         do {
247                 err = i2c_master_recv(client, response, respn);
248                 if (err != respn) {
249                         v4l2_err(&sdev->sd,
250                                 "Error %d while reading response for command 0x%02x\n",
251                                 err, command);
252                         return err < 0 ? err : -EIO;
253                 }
254
255                 DBG_BUFFER(&sdev->sd, "Response", response, respn);
256                 if (!check_command_failed(response[0]))
257                         return 0;
258
259                 if (client->irq)
260                         return -EBUSY;
261                 msleep(1);
262         } while (jiffies <= until_jiffies);
263
264         return -EBUSY;
265 }
266
267 /*
268  * si4713_read_property - reads a si4713 property
269  * @sdev: si4713_device structure for the device we are communicating
270  * @prop: property identification number
271  * @pv: property value to be returned on success
272  */
273 static int si4713_read_property(struct si4713_device *sdev, u16 prop, u32 *pv)
274 {
275         int err;
276         u8 val[SI4713_GET_PROP_NRESP];
277         /*
278          *      .First byte = 0
279          *      .Second byte = property's MSB
280          *      .Third byte = property's LSB
281          */
282         const u8 args[SI4713_GET_PROP_NARGS] = {
283                 0x00,
284                 msb(prop),
285                 lsb(prop),
286         };
287
288         err = si4713_send_command(sdev, SI4713_CMD_GET_PROPERTY,
289                                   args, ARRAY_SIZE(args), val,
290                                   ARRAY_SIZE(val), DEFAULT_TIMEOUT);
291
292         if (err < 0)
293                 return err;
294
295         *pv = compose_u16(val[2], val[3]);
296
297         v4l2_dbg(1, debug, &sdev->sd,
298                         "%s: property=0x%02x value=0x%02x status=0x%02x\n",
299                         __func__, prop, *pv, val[0]);
300
301         return err;
302 }
303
304 /*
305  * si4713_write_property - modifies a si4713 property
306  * @sdev: si4713_device structure for the device we are communicating
307  * @prop: property identification number
308  * @val: new value for that property
309  */
310 static int si4713_write_property(struct si4713_device *sdev, u16 prop, u16 val)
311 {
312         int rval;
313         u8 resp[SI4713_SET_PROP_NRESP];
314         /*
315          *      .First byte = 0
316          *      .Second byte = property's MSB
317          *      .Third byte = property's LSB
318          *      .Fourth byte = value's MSB
319          *      .Fifth byte = value's LSB
320          */
321         const u8 args[SI4713_SET_PROP_NARGS] = {
322                 0x00,
323                 msb(prop),
324                 lsb(prop),
325                 msb(val),
326                 lsb(val),
327         };
328
329         rval = si4713_send_command(sdev, SI4713_CMD_SET_PROPERTY,
330                                         args, ARRAY_SIZE(args),
331                                         resp, ARRAY_SIZE(resp),
332                                         DEFAULT_TIMEOUT);
333
334         if (rval < 0)
335                 return rval;
336
337         v4l2_dbg(1, debug, &sdev->sd,
338                         "%s: property=0x%02x value=0x%02x status=0x%02x\n",
339                         __func__, prop, val, resp[0]);
340
341         /*
342          * As there is no command response for SET_PROPERTY,
343          * wait Tcomp time to finish before proceed, in order
344          * to have property properly set.
345          */
346         msleep(TIMEOUT_SET_PROPERTY);
347
348         return rval;
349 }
350
351 /*
352  * si4713_powerup - Powers the device up
353  * @sdev: si4713_device structure for the device we are communicating
354  */
355 static int si4713_powerup(struct si4713_device *sdev)
356 {
357         struct i2c_client *client = v4l2_get_subdevdata(&sdev->sd);
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         u8 args[SI4713_PWUP_NARGS] = {
365                 SI4713_PWUP_GPO2OEN | SI4713_PWUP_FUNC_TX,
366                 SI4713_PWUP_OPMOD_ANALOG,
367         };
368
369         if (sdev->power_state)
370                 return 0;
371
372         err = regulator_bulk_enable(ARRAY_SIZE(sdev->supplies),
373                                     sdev->supplies);
374         if (err) {
375                 v4l2_err(&sdev->sd, "Failed to enable supplies: %d\n", err);
376                 return err;
377         }
378         if (gpio_is_valid(sdev->gpio_reset)) {
379                 udelay(50);
380                 gpio_set_value(sdev->gpio_reset, 1);
381         }
382
383         if (client->irq)
384                 args[0] |= SI4713_PWUP_CTSIEN;
385
386         err = si4713_send_command(sdev, SI4713_CMD_POWER_UP,
387                                         args, ARRAY_SIZE(args),
388                                         resp, ARRAY_SIZE(resp),
389                                         TIMEOUT_POWER_UP);
390
391         if (!err) {
392                 v4l2_dbg(1, debug, &sdev->sd, "Powerup response: 0x%02x\n",
393                                 resp[0]);
394                 v4l2_dbg(1, debug, &sdev->sd, "Device in power up mode\n");
395                 sdev->power_state = POWER_ON;
396
397                 if (client->irq)
398                         err = si4713_write_property(sdev, SI4713_GPO_IEN,
399                                                 SI4713_STC_INT | SI4713_CTS);
400         } else {
401                 if (gpio_is_valid(sdev->gpio_reset))
402                         gpio_set_value(sdev->gpio_reset, 0);
403                 err = regulator_bulk_disable(ARRAY_SIZE(sdev->supplies),
404                                              sdev->supplies);
405                 if (err)
406                         v4l2_err(&sdev->sd,
407                                  "Failed to disable supplies: %d\n", err);
408         }
409
410         return err;
411 }
412
413 /*
414  * si4713_powerdown - Powers the device down
415  * @sdev: si4713_device structure for the device we are communicating
416  */
417 static int si4713_powerdown(struct si4713_device *sdev)
418 {
419         int err;
420         u8 resp[SI4713_PWDN_NRESP];
421
422         if (!sdev->power_state)
423                 return 0;
424
425         err = si4713_send_command(sdev, SI4713_CMD_POWER_DOWN,
426                                         NULL, 0,
427                                         resp, ARRAY_SIZE(resp),
428                                         DEFAULT_TIMEOUT);
429
430         if (!err) {
431                 v4l2_dbg(1, debug, &sdev->sd, "Power down response: 0x%02x\n",
432                                 resp[0]);
433                 v4l2_dbg(1, debug, &sdev->sd, "Device in reset mode\n");
434                 if (gpio_is_valid(sdev->gpio_reset))
435                         gpio_set_value(sdev->gpio_reset, 0);
436                 err = regulator_bulk_disable(ARRAY_SIZE(sdev->supplies),
437                                              sdev->supplies);
438                 if (err)
439                         v4l2_err(&sdev->sd,
440                                  "Failed to disable supplies: %d\n", err);
441                 sdev->power_state = POWER_OFF;
442         }
443
444         return err;
445 }
446
447 /*
448  * si4713_checkrev - Checks if we are treating a device with the correct rev.
449  * @sdev: si4713_device structure for the device we are communicating
450  */
451 static int si4713_checkrev(struct si4713_device *sdev)
452 {
453         struct i2c_client *client = v4l2_get_subdevdata(&sdev->sd);
454         int rval;
455         u8 resp[SI4713_GETREV_NRESP];
456
457         rval = si4713_send_command(sdev, SI4713_CMD_GET_REV,
458                                         NULL, 0,
459                                         resp, ARRAY_SIZE(resp),
460                                         DEFAULT_TIMEOUT);
461
462         if (rval < 0)
463                 return rval;
464
465         if (resp[1] == SI4713_PRODUCT_NUMBER) {
466                 v4l2_info(&sdev->sd, "chip found @ 0x%02x (%s)\n",
467                                 client->addr << 1, client->adapter->name);
468         } else {
469                 v4l2_err(&sdev->sd, "Invalid product number\n");
470                 rval = -EINVAL;
471         }
472         return rval;
473 }
474
475 /*
476  * si4713_wait_stc - Waits STC interrupt and clears status bits. Useful
477  *                   for TX_TUNE_POWER, TX_TUNE_FREQ and TX_TUNE_MEAS
478  * @sdev: si4713_device structure for the device we are communicating
479  * @usecs: timeout to wait for STC interrupt signal
480  */
481 static int si4713_wait_stc(struct si4713_device *sdev, const int usecs)
482 {
483         struct i2c_client *client = v4l2_get_subdevdata(&sdev->sd);
484         u8 resp[SI4713_GET_STATUS_NRESP];
485         unsigned long start_jiffies = jiffies;
486         int err;
487
488         if (client->irq &&
489             !wait_for_completion_timeout(&sdev->work, usecs_to_jiffies(usecs) + 1))
490                 v4l2_warn(&sdev->sd,
491                         "(%s) Device took too much time to answer.\n", __func__);
492
493         for (;;) {
494                 /* Clear status bits */
495                 err = si4713_send_command(sdev, SI4713_CMD_GET_INT_STATUS,
496                                 NULL, 0,
497                                 resp, ARRAY_SIZE(resp),
498                                 DEFAULT_TIMEOUT);
499                 /* The USB device returns errors when it waits for the
500                  * STC bit to be set. Hence polling */
501                 if (err >= 0) {
502                         v4l2_dbg(1, debug, &sdev->sd,
503                                 "%s: status bits: 0x%02x\n", __func__, resp[0]);
504
505                         if (resp[0] & SI4713_STC_INT)
506                                 return 0;
507                 }
508                 if (jiffies_to_usecs(jiffies - start_jiffies) > usecs)
509                         return err < 0 ? err : -EIO;
510                 /* We sleep here for 3 ms in order to avoid flooding the device
511                  * with USB requests. The si4713 USB driver was developed
512                  * by reverse engineering the Windows USB driver. The windows
513                  * driver also has a ~2.5 ms delay between responses. */
514                 msleep(3);
515         }
516 }
517
518 /*
519  * si4713_tx_tune_freq - Sets the state of the RF carrier and sets the tuning
520  *                      frequency between 76 and 108 MHz in 10 kHz units and
521  *                      steps of 50 kHz.
522  * @sdev: si4713_device structure for the device we are communicating
523  * @frequency: desired frequency (76 - 108 MHz, unit 10 KHz, step 50 kHz)
524  */
525 static int si4713_tx_tune_freq(struct si4713_device *sdev, u16 frequency)
526 {
527         int err;
528         u8 val[SI4713_TXFREQ_NRESP];
529         /*
530          *      .First byte = 0
531          *      .Second byte = frequency's MSB
532          *      .Third byte = frequency's LSB
533          */
534         const u8 args[SI4713_TXFREQ_NARGS] = {
535                 0x00,
536                 msb(frequency),
537                 lsb(frequency),
538         };
539
540         err = si4713_send_command(sdev, SI4713_CMD_TX_TUNE_FREQ,
541                                   args, ARRAY_SIZE(args), val,
542                                   ARRAY_SIZE(val), DEFAULT_TIMEOUT);
543
544         if (err < 0)
545                 return err;
546
547         v4l2_dbg(1, debug, &sdev->sd,
548                         "%s: frequency=0x%02x status=0x%02x\n", __func__,
549                         frequency, val[0]);
550
551         err = si4713_wait_stc(sdev, TIMEOUT_TX_TUNE);
552         if (err < 0)
553                 return err;
554
555         return compose_u16(args[1], args[2]);
556 }
557
558 /*
559  * si4713_tx_tune_power - Sets the RF voltage level between 88 and 115 dBuV in
560  *                      1 dB units. A value of 0x00 indicates off. The command
561  *                      also sets the antenna tuning capacitance. A value of 0
562  *                      indicates autotuning, and a value of 1 - 191 indicates
563  *                      a manual override, which results in a tuning
564  *                      capacitance of 0.25 pF x @antcap.
565  * @sdev: si4713_device structure for the device we are communicating
566  * @power: tuning power (88 - 115 dBuV, unit/step 1 dB)
567  * @antcap: value of antenna tuning capacitor (0 - 191)
568  */
569 static int si4713_tx_tune_power(struct si4713_device *sdev, u8 power,
570                                 u8 antcap)
571 {
572         int err;
573         u8 val[SI4713_TXPWR_NRESP];
574         /*
575          *      .First byte = 0
576          *      .Second byte = 0
577          *      .Third byte = power
578          *      .Fourth byte = antcap
579          */
580         const u8 args[SI4713_TXPWR_NARGS] = {
581                 0x00,
582                 0x00,
583                 power,
584                 antcap,
585         };
586
587         if (((power > 0) && (power < SI4713_MIN_POWER)) ||
588                 power > SI4713_MAX_POWER || antcap > SI4713_MAX_ANTCAP)
589                 return -EDOM;
590
591         err = si4713_send_command(sdev, SI4713_CMD_TX_TUNE_POWER,
592                                   args, ARRAY_SIZE(args), val,
593                                   ARRAY_SIZE(val), DEFAULT_TIMEOUT);
594
595         if (err < 0)
596                 return err;
597
598         v4l2_dbg(1, debug, &sdev->sd,
599                         "%s: power=0x%02x antcap=0x%02x status=0x%02x\n",
600                         __func__, power, antcap, val[0]);
601
602         return si4713_wait_stc(sdev, TIMEOUT_TX_TUNE_POWER);
603 }
604
605 /*
606  * si4713_tx_tune_measure - Enters receive mode and measures the received noise
607  *                      level in units of dBuV on the selected frequency.
608  *                      The Frequency must be between 76 and 108 MHz in 10 kHz
609  *                      units and steps of 50 kHz. The command also sets the
610  *                      antenna tuning capacitance. A value of 0 means
611  *                      autotuning, and a value of 1 to 191 indicates manual
612  *                      override.
613  * @sdev: si4713_device structure for the device we are communicating
614  * @frequency: desired frequency (76 - 108 MHz, unit 10 KHz, step 50 kHz)
615  * @antcap: value of antenna tuning capacitor (0 - 191)
616  */
617 static int si4713_tx_tune_measure(struct si4713_device *sdev, u16 frequency,
618                                         u8 antcap)
619 {
620         int err;
621         u8 val[SI4713_TXMEA_NRESP];
622         /*
623          *      .First byte = 0
624          *      .Second byte = frequency's MSB
625          *      .Third byte = frequency's LSB
626          *      .Fourth byte = antcap
627          */
628         const u8 args[SI4713_TXMEA_NARGS] = {
629                 0x00,
630                 msb(frequency),
631                 lsb(frequency),
632                 antcap,
633         };
634
635         sdev->tune_rnl = DEFAULT_TUNE_RNL;
636
637         if (antcap > SI4713_MAX_ANTCAP)
638                 return -EDOM;
639
640         err = si4713_send_command(sdev, SI4713_CMD_TX_TUNE_MEASURE,
641                                   args, ARRAY_SIZE(args), val,
642                                   ARRAY_SIZE(val), DEFAULT_TIMEOUT);
643
644         if (err < 0)
645                 return err;
646
647         v4l2_dbg(1, debug, &sdev->sd,
648                         "%s: frequency=0x%02x antcap=0x%02x status=0x%02x\n",
649                         __func__, frequency, antcap, val[0]);
650
651         return si4713_wait_stc(sdev, TIMEOUT_TX_TUNE);
652 }
653
654 /*
655  * si4713_tx_tune_status- Returns the status of the tx_tune_freq, tx_tune_mea or
656  *                      tx_tune_power commands. This command return the current
657  *                      frequency, output voltage in dBuV, the antenna tunning
658  *                      capacitance value and the received noise level. The
659  *                      command also clears the stcint interrupt bit when the
660  *                      first bit of its arguments is high.
661  * @sdev: si4713_device structure for the device we are communicating
662  * @intack: 0x01 to clear the seek/tune complete interrupt status indicator.
663  * @frequency: returned frequency
664  * @power: returned power
665  * @antcap: returned antenna capacitance
666  * @noise: returned noise level
667  */
668 static int si4713_tx_tune_status(struct si4713_device *sdev, u8 intack,
669                                         u16 *frequency, u8 *power,
670                                         u8 *antcap, u8 *noise)
671 {
672         int err;
673         u8 val[SI4713_TXSTATUS_NRESP];
674         /*
675          *      .First byte = intack bit
676          */
677         const u8 args[SI4713_TXSTATUS_NARGS] = {
678                 intack & SI4713_INTACK_MASK,
679         };
680
681         err = si4713_send_command(sdev, SI4713_CMD_TX_TUNE_STATUS,
682                                   args, ARRAY_SIZE(args), val,
683                                   ARRAY_SIZE(val), DEFAULT_TIMEOUT);
684
685         if (!err) {
686                 v4l2_dbg(1, debug, &sdev->sd,
687                         "%s: status=0x%02x\n", __func__, val[0]);
688                 *frequency = compose_u16(val[2], val[3]);
689                 sdev->frequency = *frequency;
690                 *power = val[5];
691                 *antcap = val[6];
692                 *noise = val[7];
693                 v4l2_dbg(1, debug, &sdev->sd, "%s: response: %d x 10 kHz "
694                                 "(power %d, antcap %d, rnl %d)\n", __func__,
695                                 *frequency, *power, *antcap, *noise);
696         }
697
698         return err;
699 }
700
701 /*
702  * si4713_tx_rds_buff - Loads the RDS group buffer FIFO or circular buffer.
703  * @sdev: si4713_device structure for the device we are communicating
704  * @mode: the buffer operation mode.
705  * @rdsb: RDS Block B
706  * @rdsc: RDS Block C
707  * @rdsd: RDS Block D
708  * @cbleft: returns the number of available circular buffer blocks minus the
709  *          number of used circular buffer blocks.
710  */
711 static int si4713_tx_rds_buff(struct si4713_device *sdev, u8 mode, u16 rdsb,
712                                 u16 rdsc, u16 rdsd, s8 *cbleft)
713 {
714         int err;
715         u8 val[SI4713_RDSBUFF_NRESP];
716
717         const u8 args[SI4713_RDSBUFF_NARGS] = {
718                 mode & SI4713_RDSBUFF_MODE_MASK,
719                 msb(rdsb),
720                 lsb(rdsb),
721                 msb(rdsc),
722                 lsb(rdsc),
723                 msb(rdsd),
724                 lsb(rdsd),
725         };
726
727         err = si4713_send_command(sdev, SI4713_CMD_TX_RDS_BUFF,
728                                   args, ARRAY_SIZE(args), val,
729                                   ARRAY_SIZE(val), DEFAULT_TIMEOUT);
730
731         if (!err) {
732                 v4l2_dbg(1, debug, &sdev->sd,
733                         "%s: status=0x%02x\n", __func__, val[0]);
734                 *cbleft = (s8)val[2] - val[3];
735                 v4l2_dbg(1, debug, &sdev->sd, "%s: response: interrupts"
736                                 " 0x%02x cb avail: %d cb used %d fifo avail"
737                                 " %d fifo used %d\n", __func__, val[1],
738                                 val[2], val[3], val[4], val[5]);
739         }
740
741         return err;
742 }
743
744 /*
745  * si4713_tx_rds_ps - Loads the program service buffer.
746  * @sdev: si4713_device structure for the device we are communicating
747  * @psid: program service id to be loaded.
748  * @pschar: assumed 4 size char array to be loaded into the program service
749  */
750 static int si4713_tx_rds_ps(struct si4713_device *sdev, u8 psid,
751                                 unsigned char *pschar)
752 {
753         int err;
754         u8 val[SI4713_RDSPS_NRESP];
755
756         const u8 args[SI4713_RDSPS_NARGS] = {
757                 psid & SI4713_RDSPS_PSID_MASK,
758                 pschar[0],
759                 pschar[1],
760                 pschar[2],
761                 pschar[3],
762         };
763
764         err = si4713_send_command(sdev, SI4713_CMD_TX_RDS_PS,
765                                   args, ARRAY_SIZE(args), val,
766                                   ARRAY_SIZE(val), DEFAULT_TIMEOUT);
767
768         if (err < 0)
769                 return err;
770
771         v4l2_dbg(1, debug, &sdev->sd, "%s: status=0x%02x\n", __func__, val[0]);
772
773         return err;
774 }
775
776 static int si4713_set_power_state(struct si4713_device *sdev, u8 value)
777 {
778         if (value)
779                 return si4713_powerup(sdev);
780         return si4713_powerdown(sdev);
781 }
782
783 static int si4713_set_mute(struct si4713_device *sdev, u16 mute)
784 {
785         int rval = 0;
786
787         mute = set_mute(mute);
788
789         if (sdev->power_state)
790                 rval = si4713_write_property(sdev,
791                                 SI4713_TX_LINE_INPUT_MUTE, mute);
792
793         return rval;
794 }
795
796 static int si4713_set_rds_ps_name(struct si4713_device *sdev, char *ps_name)
797 {
798         int rval = 0, i;
799         u8 len = 0;
800
801         /* We want to clear the whole thing */
802         if (!strlen(ps_name))
803                 memset(ps_name, 0, MAX_RDS_PS_NAME + 1);
804
805         if (sdev->power_state) {
806                 /* Write the new ps name and clear the padding */
807                 for (i = 0; i < MAX_RDS_PS_NAME; i += (RDS_BLOCK / 2)) {
808                         rval = si4713_tx_rds_ps(sdev, (i / (RDS_BLOCK / 2)),
809                                                 ps_name + i);
810                         if (rval < 0)
811                                 return rval;
812                 }
813
814                 /* Setup the size to be sent */
815                 if (strlen(ps_name))
816                         len = strlen(ps_name) - 1;
817                 else
818                         len = 1;
819
820                 rval = si4713_write_property(sdev,
821                                 SI4713_TX_RDS_PS_MESSAGE_COUNT,
822                                 rds_ps_nblocks(len));
823                 if (rval < 0)
824                         return rval;
825
826                 rval = si4713_write_property(sdev,
827                                 SI4713_TX_RDS_PS_REPEAT_COUNT,
828                                 DEFAULT_RDS_PS_REPEAT_COUNT * 2);
829                 if (rval < 0)
830                         return rval;
831         }
832
833         return rval;
834 }
835
836 static int si4713_set_rds_radio_text(struct si4713_device *sdev, char *rt)
837 {
838         int rval = 0, i;
839         u16 t_index = 0;
840         u8 b_index = 0, cr_inserted = 0;
841         s8 left;
842
843         if (!sdev->power_state)
844                 return rval;
845
846         rval = si4713_tx_rds_buff(sdev, RDS_BLOCK_CLEAR, 0, 0, 0, &left);
847         if (rval < 0)
848                 return rval;
849
850         if (!strlen(rt))
851                 return rval;
852
853         do {
854                 /* RDS spec says that if the last block isn't used,
855                  * then apply a carriage return
856                  */
857                 if (t_index < (RDS_RADIOTEXT_INDEX_MAX * RDS_RADIOTEXT_BLK_SIZE)) {
858                         for (i = 0; i < RDS_RADIOTEXT_BLK_SIZE; i++) {
859                                 if (!rt[t_index + i] ||
860                                     rt[t_index + i] == RDS_CARRIAGE_RETURN) {
861                                         rt[t_index + i] = RDS_CARRIAGE_RETURN;
862                                         cr_inserted = 1;
863                                         break;
864                                 }
865                         }
866                 }
867
868                 rval = si4713_tx_rds_buff(sdev, RDS_BLOCK_LOAD,
869                                 compose_u16(RDS_RADIOTEXT_2A, b_index++),
870                                 compose_u16(rt[t_index], rt[t_index + 1]),
871                                 compose_u16(rt[t_index + 2], rt[t_index + 3]),
872                                 &left);
873                 if (rval < 0)
874                         return rval;
875
876                 t_index += RDS_RADIOTEXT_BLK_SIZE;
877
878                 if (cr_inserted)
879                         break;
880         } while (left > 0);
881
882         return rval;
883 }
884
885 /*
886  * si4713_update_tune_status - update properties from tx_tune_status
887  * command. Must be called with sdev->mutex held.
888  * @sdev: si4713_device structure for the device we are communicating
889  */
890 static int si4713_update_tune_status(struct si4713_device *sdev)
891 {
892         int rval;
893         u16 f = 0;
894         u8 p = 0, a = 0, n = 0;
895
896         rval = si4713_tx_tune_status(sdev, 0x00, &f, &p, &a, &n);
897
898         if (rval < 0)
899                 goto exit;
900
901 /*      TODO: check that power_level and antenna_capacitor really are not
902         changed by the hardware. If they are, then these controls should become
903         volatiles.
904         sdev->power_level = p;
905         sdev->antenna_capacitor = a;*/
906         sdev->tune_rnl = n;
907
908 exit:
909         return rval;
910 }
911
912 static int si4713_choose_econtrol_action(struct si4713_device *sdev, u32 id,
913                 s32 *bit, s32 *mask, u16 *property, int *mul,
914                 unsigned long **table, int *size)
915 {
916         s32 rval = 0;
917
918         switch (id) {
919         /* FM_TX class controls */
920         case V4L2_CID_RDS_TX_PI:
921                 *property = SI4713_TX_RDS_PI;
922                 *mul = 1;
923                 break;
924         case V4L2_CID_AUDIO_COMPRESSION_THRESHOLD:
925                 *property = SI4713_TX_ACOMP_THRESHOLD;
926                 *mul = 1;
927                 break;
928         case V4L2_CID_AUDIO_COMPRESSION_GAIN:
929                 *property = SI4713_TX_ACOMP_GAIN;
930                 *mul = 1;
931                 break;
932         case V4L2_CID_PILOT_TONE_FREQUENCY:
933                 *property = SI4713_TX_PILOT_FREQUENCY;
934                 *mul = 1;
935                 break;
936         case V4L2_CID_AUDIO_COMPRESSION_ATTACK_TIME:
937                 *property = SI4713_TX_ACOMP_ATTACK_TIME;
938                 *mul = ATTACK_TIME_UNIT;
939                 break;
940         case V4L2_CID_PILOT_TONE_DEVIATION:
941                 *property = SI4713_TX_PILOT_DEVIATION;
942                 *mul = 10;
943                 break;
944         case V4L2_CID_AUDIO_LIMITER_DEVIATION:
945                 *property = SI4713_TX_AUDIO_DEVIATION;
946                 *mul = 10;
947                 break;
948         case V4L2_CID_RDS_TX_DEVIATION:
949                 *property = SI4713_TX_RDS_DEVIATION;
950                 *mul = 1;
951                 break;
952
953         case V4L2_CID_RDS_TX_PTY:
954                 *property = SI4713_TX_RDS_PS_MISC;
955                 *bit = 5;
956                 *mask = 0x1F << 5;
957                 break;
958         case V4L2_CID_AUDIO_LIMITER_ENABLED:
959                 *property = SI4713_TX_ACOMP_ENABLE;
960                 *bit = 1;
961                 *mask = 1 << 1;
962                 break;
963         case V4L2_CID_AUDIO_COMPRESSION_ENABLED:
964                 *property = SI4713_TX_ACOMP_ENABLE;
965                 *bit = 0;
966                 *mask = 1 << 0;
967                 break;
968         case V4L2_CID_PILOT_TONE_ENABLED:
969                 *property = SI4713_TX_COMPONENT_ENABLE;
970                 *bit = 0;
971                 *mask = 1 << 0;
972                 break;
973
974         case V4L2_CID_AUDIO_LIMITER_RELEASE_TIME:
975                 *property = SI4713_TX_LIMITER_RELEASE_TIME;
976                 *table = limiter_times;
977                 *size = ARRAY_SIZE(limiter_times);
978                 break;
979         case V4L2_CID_AUDIO_COMPRESSION_RELEASE_TIME:
980                 *property = SI4713_TX_ACOMP_RELEASE_TIME;
981                 *table = acomp_rtimes;
982                 *size = ARRAY_SIZE(acomp_rtimes);
983                 break;
984         case V4L2_CID_TUNE_PREEMPHASIS:
985                 *property = SI4713_TX_PREEMPHASIS;
986                 *table = preemphasis_values;
987                 *size = ARRAY_SIZE(preemphasis_values);
988                 break;
989
990         default:
991                 rval = -EINVAL;
992                 break;
993         }
994
995         return rval;
996 }
997
998 static int si4713_s_frequency(struct v4l2_subdev *sd, const struct v4l2_frequency *f);
999 static int si4713_s_modulator(struct v4l2_subdev *sd, const struct v4l2_modulator *);
1000 /*
1001  * si4713_setup - Sets the device up with current configuration.
1002  * @sdev: si4713_device structure for the device we are communicating
1003  */
1004 static int si4713_setup(struct si4713_device *sdev)
1005 {
1006         struct v4l2_frequency f;
1007         struct v4l2_modulator vm;
1008         int rval;
1009
1010         /* Device procedure needs to set frequency first */
1011         f.tuner = 0;
1012         f.frequency = sdev->frequency ? sdev->frequency : DEFAULT_FREQUENCY;
1013         f.frequency = si4713_to_v4l2(f.frequency);
1014         rval = si4713_s_frequency(&sdev->sd, &f);
1015
1016         vm.index = 0;
1017         if (sdev->stereo)
1018                 vm.txsubchans = V4L2_TUNER_SUB_STEREO;
1019         else
1020                 vm.txsubchans = V4L2_TUNER_SUB_MONO;
1021         if (sdev->rds_enabled)
1022                 vm.txsubchans |= V4L2_TUNER_SUB_RDS;
1023         si4713_s_modulator(&sdev->sd, &vm);
1024
1025         return rval;
1026 }
1027
1028 /*
1029  * si4713_initialize - Sets the device up with default configuration.
1030  * @sdev: si4713_device structure for the device we are communicating
1031  */
1032 static int si4713_initialize(struct si4713_device *sdev)
1033 {
1034         int rval;
1035
1036         rval = si4713_set_power_state(sdev, POWER_ON);
1037         if (rval < 0)
1038                 return rval;
1039
1040         rval = si4713_checkrev(sdev);
1041         if (rval < 0)
1042                 return rval;
1043
1044         rval = si4713_set_power_state(sdev, POWER_OFF);
1045         if (rval < 0)
1046                 return rval;
1047
1048         sdev->frequency = DEFAULT_FREQUENCY;
1049         sdev->stereo = 1;
1050         sdev->tune_rnl = DEFAULT_TUNE_RNL;
1051         return 0;
1052 }
1053
1054 /* si4713_s_ctrl - set the value of a control */
1055 static int si4713_s_ctrl(struct v4l2_ctrl *ctrl)
1056 {
1057         struct si4713_device *sdev =
1058                 container_of(ctrl->handler, struct si4713_device, ctrl_handler);
1059         u32 val = 0;
1060         s32 bit = 0, mask = 0;
1061         u16 property = 0;
1062         int mul = 0;
1063         unsigned long *table = NULL;
1064         int size = 0;
1065         bool force = false;
1066         int c;
1067         int ret = 0;
1068
1069         if (ctrl->id != V4L2_CID_AUDIO_MUTE)
1070                 return -EINVAL;
1071         if (ctrl->is_new) {
1072                 if (ctrl->val) {
1073                         ret = si4713_set_mute(sdev, ctrl->val);
1074                         if (!ret)
1075                                 ret = si4713_set_power_state(sdev, POWER_DOWN);
1076                         return ret;
1077                 }
1078                 ret = si4713_set_power_state(sdev, POWER_UP);
1079                 if (!ret)
1080                         ret = si4713_set_mute(sdev, ctrl->val);
1081                 if (!ret)
1082                         ret = si4713_setup(sdev);
1083                 if (ret)
1084                         return ret;
1085                 force = true;
1086         }
1087
1088         if (!sdev->power_state)
1089                 return 0;
1090
1091         for (c = 1; !ret && c < ctrl->ncontrols; c++) {
1092                 ctrl = ctrl->cluster[c];
1093
1094                 if (!force && !ctrl->is_new)
1095                         continue;
1096
1097                 switch (ctrl->id) {
1098                 case V4L2_CID_RDS_TX_PS_NAME:
1099                         ret = si4713_set_rds_ps_name(sdev, ctrl->string);
1100                         break;
1101
1102                 case V4L2_CID_RDS_TX_RADIO_TEXT:
1103                         ret = si4713_set_rds_radio_text(sdev, ctrl->string);
1104                         break;
1105
1106                 case V4L2_CID_TUNE_ANTENNA_CAPACITOR:
1107                         /* don't handle this control if we force setting all
1108                          * controls since in that case it will be handled by
1109                          * V4L2_CID_TUNE_POWER_LEVEL. */
1110                         if (force)
1111                                 break;
1112                         /* fall through */
1113                 case V4L2_CID_TUNE_POWER_LEVEL:
1114                         ret = si4713_tx_tune_power(sdev,
1115                                 sdev->tune_pwr_level->val, sdev->tune_ant_cap->val);
1116                         if (!ret) {
1117                                 /* Make sure we don't set this twice */
1118                                 sdev->tune_ant_cap->is_new = false;
1119                                 sdev->tune_pwr_level->is_new = false;
1120                         }
1121                         break;
1122
1123                 default:
1124                         ret = si4713_choose_econtrol_action(sdev, ctrl->id, &bit,
1125                                         &mask, &property, &mul, &table, &size);
1126                         if (ret < 0)
1127                                 break;
1128
1129                         val = ctrl->val;
1130                         if (mul) {
1131                                 val = val / mul;
1132                         } else if (table) {
1133                                 ret = usecs_to_dev(val, table, size);
1134                                 if (ret < 0)
1135                                         break;
1136                                 val = ret;
1137                                 ret = 0;
1138                         }
1139
1140                         if (mask) {
1141                                 ret = si4713_read_property(sdev, property, &val);
1142                                 if (ret < 0)
1143                                         break;
1144                                 val = set_bits(val, ctrl->val, bit, mask);
1145                         }
1146
1147                         ret = si4713_write_property(sdev, property, val);
1148                         if (ret < 0)
1149                                 break;
1150                         if (mask)
1151                                 val = ctrl->val;
1152                         break;
1153                 }
1154         }
1155
1156         return ret;
1157 }
1158
1159 /* si4713_ioctl - deal with private ioctls (only rnl for now) */
1160 static long si4713_ioctl(struct v4l2_subdev *sd, unsigned int cmd, void *arg)
1161 {
1162         struct si4713_device *sdev = to_si4713_device(sd);
1163         struct si4713_rnl *rnl = arg;
1164         u16 frequency;
1165         int rval = 0;
1166
1167         if (!arg)
1168                 return -EINVAL;
1169
1170         switch (cmd) {
1171         case SI4713_IOC_MEASURE_RNL:
1172                 frequency = v4l2_to_si4713(rnl->frequency);
1173
1174                 if (sdev->power_state) {
1175                         /* Set desired measurement frequency */
1176                         rval = si4713_tx_tune_measure(sdev, frequency, 0);
1177                         if (rval < 0)
1178                                 return rval;
1179                         /* get results from tune status */
1180                         rval = si4713_update_tune_status(sdev);
1181                         if (rval < 0)
1182                                 return rval;
1183                 }
1184                 rnl->rnl = sdev->tune_rnl;
1185                 break;
1186
1187         default:
1188                 /* nothing */
1189                 rval = -ENOIOCTLCMD;
1190         }
1191
1192         return rval;
1193 }
1194
1195 /* si4713_g_modulator - get modulator attributes */
1196 static int si4713_g_modulator(struct v4l2_subdev *sd, struct v4l2_modulator *vm)
1197 {
1198         struct si4713_device *sdev = to_si4713_device(sd);
1199         int rval = 0;
1200
1201         if (!sdev)
1202                 return -ENODEV;
1203
1204         if (vm->index > 0)
1205                 return -EINVAL;
1206
1207         strncpy(vm->name, "FM Modulator", 32);
1208         vm->capability = V4L2_TUNER_CAP_STEREO | V4L2_TUNER_CAP_LOW |
1209                 V4L2_TUNER_CAP_RDS | V4L2_TUNER_CAP_RDS_CONTROLS;
1210
1211         /* Report current frequency range limits */
1212         vm->rangelow = si4713_to_v4l2(FREQ_RANGE_LOW);
1213         vm->rangehigh = si4713_to_v4l2(FREQ_RANGE_HIGH);
1214
1215         if (sdev->power_state) {
1216                 u32 comp_en = 0;
1217
1218                 rval = si4713_read_property(sdev, SI4713_TX_COMPONENT_ENABLE,
1219                                                 &comp_en);
1220                 if (rval < 0)
1221                         return rval;
1222
1223                 sdev->stereo = get_status_bit(comp_en, 1, 1 << 1);
1224         }
1225
1226         /* Report current audio mode: mono or stereo */
1227         if (sdev->stereo)
1228                 vm->txsubchans = V4L2_TUNER_SUB_STEREO;
1229         else
1230                 vm->txsubchans = V4L2_TUNER_SUB_MONO;
1231
1232         /* Report rds feature status */
1233         if (sdev->rds_enabled)
1234                 vm->txsubchans |= V4L2_TUNER_SUB_RDS;
1235         else
1236                 vm->txsubchans &= ~V4L2_TUNER_SUB_RDS;
1237
1238         return rval;
1239 }
1240
1241 /* si4713_s_modulator - set modulator attributes */
1242 static int si4713_s_modulator(struct v4l2_subdev *sd, const struct v4l2_modulator *vm)
1243 {
1244         struct si4713_device *sdev = to_si4713_device(sd);
1245         int rval = 0;
1246         u16 stereo, rds;
1247         u32 p;
1248
1249         if (!sdev)
1250                 return -ENODEV;
1251
1252         if (vm->index > 0)
1253                 return -EINVAL;
1254
1255         /* Set audio mode: mono or stereo */
1256         if (vm->txsubchans & V4L2_TUNER_SUB_STEREO)
1257                 stereo = 1;
1258         else if (vm->txsubchans & V4L2_TUNER_SUB_MONO)
1259                 stereo = 0;
1260         else
1261                 return -EINVAL;
1262
1263         rds = !!(vm->txsubchans & V4L2_TUNER_SUB_RDS);
1264
1265         if (sdev->power_state) {
1266                 rval = si4713_read_property(sdev,
1267                                                 SI4713_TX_COMPONENT_ENABLE, &p);
1268                 if (rval < 0)
1269                         return rval;
1270
1271                 p = set_bits(p, stereo, 1, 1 << 1);
1272                 p = set_bits(p, rds, 2, 1 << 2);
1273
1274                 rval = si4713_write_property(sdev,
1275                                                 SI4713_TX_COMPONENT_ENABLE, p);
1276                 if (rval < 0)
1277                         return rval;
1278         }
1279
1280         sdev->stereo = stereo;
1281         sdev->rds_enabled = rds;
1282
1283         return rval;
1284 }
1285
1286 /* si4713_g_frequency - get tuner or modulator radio frequency */
1287 static int si4713_g_frequency(struct v4l2_subdev *sd, struct v4l2_frequency *f)
1288 {
1289         struct si4713_device *sdev = to_si4713_device(sd);
1290         int rval = 0;
1291
1292         if (f->tuner)
1293                 return -EINVAL;
1294
1295         if (sdev->power_state) {
1296                 u16 freq;
1297                 u8 p, a, n;
1298
1299                 rval = si4713_tx_tune_status(sdev, 0x00, &freq, &p, &a, &n);
1300                 if (rval < 0)
1301                         return rval;
1302
1303                 sdev->frequency = freq;
1304         }
1305
1306         f->frequency = si4713_to_v4l2(sdev->frequency);
1307
1308         return rval;
1309 }
1310
1311 /* si4713_s_frequency - set tuner or modulator radio frequency */
1312 static int si4713_s_frequency(struct v4l2_subdev *sd, const struct v4l2_frequency *f)
1313 {
1314         struct si4713_device *sdev = to_si4713_device(sd);
1315         int rval = 0;
1316         u16 frequency = v4l2_to_si4713(f->frequency);
1317
1318         if (f->tuner)
1319                 return -EINVAL;
1320
1321         /* Check frequency range */
1322         frequency = clamp_t(u16, frequency, FREQ_RANGE_LOW, FREQ_RANGE_HIGH);
1323
1324         if (sdev->power_state) {
1325                 rval = si4713_tx_tune_freq(sdev, frequency);
1326                 if (rval < 0)
1327                         return rval;
1328                 frequency = rval;
1329                 rval = 0;
1330         }
1331         sdev->frequency = frequency;
1332
1333         return rval;
1334 }
1335
1336 static const struct v4l2_ctrl_ops si4713_ctrl_ops = {
1337         .s_ctrl = si4713_s_ctrl,
1338 };
1339
1340 static const struct v4l2_subdev_core_ops si4713_subdev_core_ops = {
1341         .ioctl          = si4713_ioctl,
1342 };
1343
1344 static const struct v4l2_subdev_tuner_ops si4713_subdev_tuner_ops = {
1345         .g_frequency    = si4713_g_frequency,
1346         .s_frequency    = si4713_s_frequency,
1347         .g_modulator    = si4713_g_modulator,
1348         .s_modulator    = si4713_s_modulator,
1349 };
1350
1351 static const struct v4l2_subdev_ops si4713_subdev_ops = {
1352         .core           = &si4713_subdev_core_ops,
1353         .tuner          = &si4713_subdev_tuner_ops,
1354 };
1355
1356 /*
1357  * I2C driver interface
1358  */
1359 /* si4713_probe - probe for the device */
1360 static int si4713_probe(struct i2c_client *client,
1361                                         const struct i2c_device_id *id)
1362 {
1363         struct si4713_device *sdev;
1364         struct si4713_platform_data *pdata = client->dev.platform_data;
1365         struct v4l2_ctrl_handler *hdl;
1366         int rval, i;
1367
1368         sdev = kzalloc(sizeof *sdev, GFP_KERNEL);
1369         if (!sdev) {
1370                 dev_err(&client->dev, "Failed to alloc video device.\n");
1371                 rval = -ENOMEM;
1372                 goto exit;
1373         }
1374
1375         sdev->gpio_reset = -1;
1376         if (pdata && gpio_is_valid(pdata->gpio_reset)) {
1377                 rval = gpio_request(pdata->gpio_reset, "si4713 reset");
1378                 if (rval) {
1379                         dev_err(&client->dev,
1380                                 "Failed to request gpio: %d\n", rval);
1381                         goto free_sdev;
1382                 }
1383                 sdev->gpio_reset = pdata->gpio_reset;
1384                 gpio_direction_output(sdev->gpio_reset, 0);
1385         }
1386
1387         for (i = 0; i < ARRAY_SIZE(sdev->supplies); i++)
1388                 sdev->supplies[i].supply = si4713_supply_names[i];
1389
1390         rval = regulator_bulk_get(&client->dev, ARRAY_SIZE(sdev->supplies),
1391                                   sdev->supplies);
1392         if (rval) {
1393                 dev_err(&client->dev, "Cannot get regulators: %d\n", rval);
1394                 goto free_gpio;
1395         }
1396
1397         v4l2_i2c_subdev_init(&sdev->sd, client, &si4713_subdev_ops);
1398
1399         init_completion(&sdev->work);
1400
1401         hdl = &sdev->ctrl_handler;
1402         v4l2_ctrl_handler_init(hdl, 20);
1403         sdev->mute = v4l2_ctrl_new_std(hdl, &si4713_ctrl_ops,
1404                         V4L2_CID_AUDIO_MUTE, 0, 1, 1, DEFAULT_MUTE);
1405
1406         sdev->rds_pi = v4l2_ctrl_new_std(hdl, &si4713_ctrl_ops,
1407                         V4L2_CID_RDS_TX_PI, 0, 0xffff, 1, DEFAULT_RDS_PI);
1408         sdev->rds_pty = v4l2_ctrl_new_std(hdl, &si4713_ctrl_ops,
1409                         V4L2_CID_RDS_TX_PTY, 0, 31, 1, DEFAULT_RDS_PTY);
1410         sdev->rds_deviation = v4l2_ctrl_new_std(hdl, &si4713_ctrl_ops,
1411                         V4L2_CID_RDS_TX_DEVIATION, 0, MAX_RDS_DEVIATION,
1412                         10, DEFAULT_RDS_DEVIATION);
1413         /*
1414          * Report step as 8. From RDS spec, psname
1415          * should be 8. But there are receivers which scroll strings
1416          * sized as 8xN.
1417          */
1418         sdev->rds_ps_name = v4l2_ctrl_new_std(hdl, &si4713_ctrl_ops,
1419                         V4L2_CID_RDS_TX_PS_NAME, 0, MAX_RDS_PS_NAME, 8, 0);
1420         /*
1421          * Report step as 32 (2A block). From RDS spec,
1422          * radio text should be 32 for 2A block. But there are receivers
1423          * which scroll strings sized as 32xN. Setting default to 32.
1424          */
1425         sdev->rds_radio_text = v4l2_ctrl_new_std(hdl, &si4713_ctrl_ops,
1426                         V4L2_CID_RDS_TX_RADIO_TEXT, 0, MAX_RDS_RADIO_TEXT, 32, 0);
1427
1428         sdev->limiter_enabled = v4l2_ctrl_new_std(hdl, &si4713_ctrl_ops,
1429                         V4L2_CID_AUDIO_LIMITER_ENABLED, 0, 1, 1, 1);
1430         sdev->limiter_release_time = v4l2_ctrl_new_std(hdl, &si4713_ctrl_ops,
1431                         V4L2_CID_AUDIO_LIMITER_RELEASE_TIME, 250,
1432                         MAX_LIMITER_RELEASE_TIME, 10, DEFAULT_LIMITER_RTIME);
1433         sdev->limiter_deviation = v4l2_ctrl_new_std(hdl, &si4713_ctrl_ops,
1434                         V4L2_CID_AUDIO_LIMITER_DEVIATION, 0,
1435                         MAX_LIMITER_DEVIATION, 10, DEFAULT_LIMITER_DEV);
1436
1437         sdev->compression_enabled = v4l2_ctrl_new_std(hdl, &si4713_ctrl_ops,
1438                         V4L2_CID_AUDIO_COMPRESSION_ENABLED, 0, 1, 1, 1);
1439         sdev->compression_gain = v4l2_ctrl_new_std(hdl, &si4713_ctrl_ops,
1440                         V4L2_CID_AUDIO_COMPRESSION_GAIN, 0, MAX_ACOMP_GAIN, 1,
1441                         DEFAULT_ACOMP_GAIN);
1442         sdev->compression_threshold = v4l2_ctrl_new_std(hdl, &si4713_ctrl_ops,
1443                         V4L2_CID_AUDIO_COMPRESSION_THRESHOLD, MIN_ACOMP_THRESHOLD,
1444                         MAX_ACOMP_THRESHOLD, 1,
1445                         DEFAULT_ACOMP_THRESHOLD);
1446         sdev->compression_attack_time = v4l2_ctrl_new_std(hdl, &si4713_ctrl_ops,
1447                         V4L2_CID_AUDIO_COMPRESSION_ATTACK_TIME, 0,
1448                         MAX_ACOMP_ATTACK_TIME, 500, DEFAULT_ACOMP_ATIME);
1449         sdev->compression_release_time = v4l2_ctrl_new_std(hdl, &si4713_ctrl_ops,
1450                         V4L2_CID_AUDIO_COMPRESSION_RELEASE_TIME, 100000,
1451                         MAX_ACOMP_RELEASE_TIME, 100000, DEFAULT_ACOMP_RTIME);
1452
1453         sdev->pilot_tone_enabled = v4l2_ctrl_new_std(hdl, &si4713_ctrl_ops,
1454                         V4L2_CID_PILOT_TONE_ENABLED, 0, 1, 1, 1);
1455         sdev->pilot_tone_deviation = v4l2_ctrl_new_std(hdl, &si4713_ctrl_ops,
1456                         V4L2_CID_PILOT_TONE_DEVIATION, 0, MAX_PILOT_DEVIATION,
1457                         10, DEFAULT_PILOT_DEVIATION);
1458         sdev->pilot_tone_freq = v4l2_ctrl_new_std(hdl, &si4713_ctrl_ops,
1459                         V4L2_CID_PILOT_TONE_FREQUENCY, 0, MAX_PILOT_FREQUENCY,
1460                         1, DEFAULT_PILOT_FREQUENCY);
1461
1462         sdev->tune_preemphasis = v4l2_ctrl_new_std_menu(hdl, &si4713_ctrl_ops,
1463                         V4L2_CID_TUNE_PREEMPHASIS,
1464                         V4L2_PREEMPHASIS_75_uS, 0, V4L2_PREEMPHASIS_50_uS);
1465         sdev->tune_pwr_level = v4l2_ctrl_new_std(hdl, &si4713_ctrl_ops,
1466                         V4L2_CID_TUNE_POWER_LEVEL, 0, 120, 1, DEFAULT_POWER_LEVEL);
1467         sdev->tune_ant_cap = v4l2_ctrl_new_std(hdl, &si4713_ctrl_ops,
1468                         V4L2_CID_TUNE_ANTENNA_CAPACITOR, 0, 191, 1, 0);
1469
1470         if (hdl->error) {
1471                 rval = hdl->error;
1472                 goto free_ctrls;
1473         }
1474         v4l2_ctrl_cluster(20, &sdev->mute);
1475         sdev->sd.ctrl_handler = hdl;
1476
1477         if (client->irq) {
1478                 rval = request_irq(client->irq,
1479                         si4713_handler, IRQF_TRIGGER_FALLING,
1480                         client->name, sdev);
1481                 if (rval < 0) {
1482                         v4l2_err(&sdev->sd, "Could not request IRQ\n");
1483                         goto put_reg;
1484                 }
1485                 v4l2_dbg(1, debug, &sdev->sd, "IRQ requested.\n");
1486         } else {
1487                 v4l2_warn(&sdev->sd, "IRQ not configured. Using timeouts.\n");
1488         }
1489
1490         rval = si4713_initialize(sdev);
1491         if (rval < 0) {
1492                 v4l2_err(&sdev->sd, "Failed to probe device information.\n");
1493                 goto free_irq;
1494         }
1495
1496         return 0;
1497
1498 free_irq:
1499         if (client->irq)
1500                 free_irq(client->irq, sdev);
1501 free_ctrls:
1502         v4l2_ctrl_handler_free(hdl);
1503 put_reg:
1504         regulator_bulk_free(ARRAY_SIZE(sdev->supplies), sdev->supplies);
1505 free_gpio:
1506         if (gpio_is_valid(sdev->gpio_reset))
1507                 gpio_free(sdev->gpio_reset);
1508 free_sdev:
1509         kfree(sdev);
1510 exit:
1511         return rval;
1512 }
1513
1514 /* si4713_remove - remove the device */
1515 static int si4713_remove(struct i2c_client *client)
1516 {
1517         struct v4l2_subdev *sd = i2c_get_clientdata(client);
1518         struct si4713_device *sdev = to_si4713_device(sd);
1519
1520         if (sdev->power_state)
1521                 si4713_set_power_state(sdev, POWER_DOWN);
1522
1523         if (client->irq > 0)
1524                 free_irq(client->irq, sdev);
1525
1526         v4l2_device_unregister_subdev(sd);
1527         v4l2_ctrl_handler_free(sd->ctrl_handler);
1528         regulator_bulk_free(ARRAY_SIZE(sdev->supplies), sdev->supplies);
1529         if (gpio_is_valid(sdev->gpio_reset))
1530                 gpio_free(sdev->gpio_reset);
1531         kfree(sdev);
1532
1533         return 0;
1534 }
1535
1536 /* si4713_i2c_driver - i2c driver interface */
1537 static const struct i2c_device_id si4713_id[] = {
1538         { "si4713" , 0 },
1539         { },
1540 };
1541 MODULE_DEVICE_TABLE(i2c, si4713_id);
1542
1543 static struct i2c_driver si4713_i2c_driver = {
1544         .driver         = {
1545                 .name   = "si4713",
1546         },
1547         .probe          = si4713_probe,
1548         .remove         = si4713_remove,
1549         .id_table       = si4713_id,
1550 };
1551
1552 module_i2c_driver(si4713_i2c_driver);