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