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