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