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