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