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