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