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