blob: 0b04b56571da865961d4f4be3f0b8b70f1b102ff [file] [log] [blame]
Eduardo Valentin02bee892009-08-08 08:46:53 -03001/*
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
Eduardo Valentin02bee892009-08-08 08:46:53 -030024#include <linux/completion.h>
25#include <linux/delay.h>
Sebastian Reicheld4471de2014-10-21 12:07:00 -030026#include <linux/err.h>
Eduardo Valentin02bee892009-08-08 08:46:53 -030027#include <linux/interrupt.h>
28#include <linux/i2c.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090029#include <linux/slab.h>
Jarkko Nikula00df0552010-10-29 11:31:39 -030030#include <linux/gpio.h>
Paul Gortmaker7a707b82011-07-03 14:03:12 -040031#include <linux/module.h>
Eduardo Valentin02bee892009-08-08 08:46:53 -030032#include <media/v4l2-device.h>
33#include <media/v4l2-ioctl.h>
34#include <media/v4l2-common.h>
35
Dinesh Ram73912322013-10-15 12:24:37 -030036#include "si4713.h"
Eduardo Valentin02bee892009-08-08 08:46:53 -030037
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
Eduardo Valentin02bee892009-08-08 08:46:53 -030050#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
Hans Verkuil9ca33cc2013-12-06 06:48:49 -030054#define DEFAULT_PILOT_FREQUENCY 0x4A38
Eduardo Valentin02bee892009-08-08 08:46:53 -030055#define DEFAULT_PILOT_DEVIATION 0x1A5E
56#define DEFAULT_ACOMP_ATIME 0x0000
57#define DEFAULT_ACOMP_RTIME 0xF4240L
58#define DEFAULT_ACOMP_GAIN 0x0F
Hans Verkuil9ca33cc2013-12-06 06:48:49 -030059#define DEFAULT_ACOMP_THRESHOLD (-0x28)
Eduardo Valentin02bee892009-08-08 08:46:53 -030060#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));
Eduardo Valentin02bee892009-08-08 08:46:53 -0300103
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
Eduardo Valentin02bee892009-08-08 08:46:53 -0300184/* 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);
Dinesh Ram51733322013-10-15 12:24:38 -0300211 unsigned long until_jiffies;
Eduardo Valentin02bee892009-08-08 08:46:53 -0300212 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);
Dinesh Ram51733322013-10-15 12:24:38 -0300227 return err < 0 ? err : -EIO;
Eduardo Valentin02bee892009-08-08 08:46:53 -0300228 }
229
Dinesh Ram51733322013-10-15 12:24:38 -0300230 until_jiffies = jiffies + usecs_to_jiffies(usecs) + 1;
231
Eduardo Valentin02bee892009-08-08 08:46:53 -0300232 /* Wait response from interrupt */
Dinesh Ram51733322013-10-15 12:24:38 -0300233 if (client->irq) {
234 if (!wait_for_completion_timeout(&sdev->work,
Eduardo Valentin02bee892009-08-08 08:46:53 -0300235 usecs_to_jiffies(usecs) + 1))
Dinesh Ram51733322013-10-15 12:24:38 -0300236 v4l2_warn(&sdev->sd,
Eduardo Valentin02bee892009-08-08 08:46:53 -0300237 "(%s) Device took too much time to answer.\n",
238 __func__);
Eduardo Valentin02bee892009-08-08 08:46:53 -0300239 }
240
Dinesh Ram51733322013-10-15 12:24:38 -0300241 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 }
Eduardo Valentin02bee892009-08-08 08:46:53 -0300249
Dinesh Ram51733322013-10-15 12:24:38 -0300250 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;
Hans Verkuilcb15da32013-12-06 06:48:49 -0300256 if (usecs <= 1000)
257 usleep_range(usecs, 1000);
258 else
259 usleep_range(1000, 2000);
260 } while (time_is_after_jiffies(until_jiffies));
Dinesh Ram51733322013-10-15 12:24:38 -0300261
262 return -EBUSY;
Eduardo Valentin02bee892009-08-08 08:46:53 -0300263}
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 /*
Hans Verkuil9ca33cc2013-12-06 06:48:49 -0300276 * .First byte = 0
277 * .Second byte = property's MSB
278 * .Third byte = property's LSB
Eduardo Valentin02bee892009-08-08 08:46:53 -0300279 */
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 /*
Hans Verkuil9ca33cc2013-12-06 06:48:49 -0300313 * .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
Eduardo Valentin02bee892009-08-08 08:46:53 -0300318 */
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{
Dinesh Ram51733322013-10-15 12:24:38 -0300355 struct i2c_client *client = v4l2_get_subdevdata(&sdev->sd);
Eduardo Valentin02bee892009-08-08 08:46:53 -0300356 int err;
357 u8 resp[SI4713_PWUP_NRESP];
358 /*
Hans Verkuil9ca33cc2013-12-06 06:48:49 -0300359 * .First byte = Enabled interrupts and boot function
360 * .Second byte = Input operation mode
Eduardo Valentin02bee892009-08-08 08:46:53 -0300361 */
Dinesh Ram51733322013-10-15 12:24:38 -0300362 u8 args[SI4713_PWUP_NARGS] = {
363 SI4713_PWUP_GPO2OEN | SI4713_PWUP_FUNC_TX,
Eduardo Valentin02bee892009-08-08 08:46:53 -0300364 SI4713_PWUP_OPMOD_ANALOG,
365 };
366
367 if (sdev->power_state)
368 return 0;
369
Sebastian Reicheld4471de2014-10-21 12:07:00 -0300370 if (sdev->vdd) {
371 err = regulator_enable(sdev->vdd);
Dinesh Ramcc6d6182013-10-15 12:24:44 -0300372 if (err) {
Sebastian Reicheld4471de2014-10-21 12:07:00 -0300373 v4l2_err(&sdev->sd, "Failed to enable vdd: %d\n", err);
Dinesh Ramcc6d6182013-10-15 12:24:44 -0300374 return err;
375 }
Jarkko Nikula00df0552010-10-29 11:31:39 -0300376 }
Sebastian Reicheld4471de2014-10-21 12:07:00 -0300377
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
Uwe Kleine-Königb36ae852015-03-26 17:47:53 -0300386 if (sdev->gpio_reset) {
Jarkko Nikula00df0552010-10-29 11:31:39 -0300387 udelay(50);
Sebastian Reichelfbe14a12014-10-21 12:07:01 -0300388 gpiod_set_value(sdev->gpio_reset, 1);
Jarkko Nikula00df0552010-10-29 11:31:39 -0300389 }
390
Dinesh Ram51733322013-10-15 12:24:38 -0300391 if (client->irq)
392 args[0] |= SI4713_PWUP_CTSIEN;
393
Eduardo Valentin02bee892009-08-08 08:46:53 -0300394 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
Dinesh Ram51733322013-10-15 12:24:38 -0300405 if (client->irq)
406 err = si4713_write_property(sdev, SI4713_GPO_IEN,
Eduardo Valentin02bee892009-08-08 08:46:53 -0300407 SI4713_STC_INT | SI4713_CTS);
Dinesh Ramcc6d6182013-10-15 12:24:44 -0300408 return err;
409 }
Uwe Kleine-Königb36ae852015-03-26 17:47:53 -0300410 gpiod_set_value(sdev->gpio_reset, 0);
Sebastian Reicheld4471de2014-10-21 12:07:00 -0300411
412
413 if (sdev->vdd) {
414 err = regulator_disable(sdev->vdd);
Jarkko Nikula00df0552010-10-29 11:31:39 -0300415 if (err)
Sebastian Reicheld4471de2014-10-21 12:07:00 -0300416 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);
Eduardo Valentin02bee892009-08-08 08:46:53 -0300423 }
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 */
432static 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");
Uwe Kleine-Königb36ae852015-03-26 17:47:53 -0300449 if (sdev->gpio_reset)
Sebastian Reichelfbe14a12014-10-21 12:07:01 -0300450 gpiod_set_value(sdev->gpio_reset, 0);
Sebastian Reicheld4471de2014-10-21 12:07:00 -0300451
452 if (sdev->vdd) {
453 err = regulator_disable(sdev->vdd);
454 if (err) {
Dinesh Ramcc6d6182013-10-15 12:24:44 -0300455 v4l2_err(&sdev->sd,
Sebastian Reicheld4471de2014-10-21 12:07:00 -0300456 "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 }
Dinesh Ramcc6d6182013-10-15 12:24:44 -0300466 }
Eduardo Valentin02bee892009-08-08 08:46:53 -0300467 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 */
477static 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
Eduardo Valentin02bee892009-08-08 08:46:53 -0300483 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)
Hans Verkuil55b2a312013-04-08 06:31:30 -0300489 return rval;
Eduardo Valentin02bee892009-08-08 08:46:53 -0300490
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 {
Eduardo Valentinb2c6eed2013-11-05 11:18:43 -0300495 v4l2_err(&sdev->sd, "Invalid product number 0x%X\n", resp[1]);
Eduardo Valentin02bee892009-08-08 08:46:53 -0300496 rval = -EINVAL;
497 }
Eduardo Valentin02bee892009-08-08 08:46:53 -0300498 return rval;
499}
500
501/*
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300502 * si4713_wait_stc - Waits STC interrupt and clears status bits. Useful
Eduardo Valentin02bee892009-08-08 08:46:53 -0300503 * 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 */
507static int si4713_wait_stc(struct si4713_device *sdev, const int usecs)
508{
Dinesh Ram51733322013-10-15 12:24:38 -0300509 struct i2c_client *client = v4l2_get_subdevdata(&sdev->sd);
Eduardo Valentin02bee892009-08-08 08:46:53 -0300510 u8 resp[SI4713_GET_STATUS_NRESP];
Dinesh Ram51733322013-10-15 12:24:38 -0300511 unsigned long start_jiffies = jiffies;
512 int err;
Eduardo Valentin02bee892009-08-08 08:46:53 -0300513
Dinesh Ram51733322013-10-15 12:24:38 -0300514 if (client->irq &&
515 !wait_for_completion_timeout(&sdev->work, usecs_to_jiffies(usecs) + 1))
Eduardo Valentin02bee892009-08-08 08:46:53 -0300516 v4l2_warn(&sdev->sd,
Dinesh Ram51733322013-10-15 12:24:38 -0300517 "(%s) Device took too much time to answer.\n", __func__);
Eduardo Valentin02bee892009-08-08 08:46:53 -0300518
Dinesh Ram51733322013-10-15 12:24:38 -0300519 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]);
Eduardo Valentin02bee892009-08-08 08:46:53 -0300530
Dinesh Ram51733322013-10-15 12:24:38 -0300531 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;
Hans Verkuilcb15da32013-12-06 06:48:49 -0300536 /* We sleep here for 3-4 ms in order to avoid flooding the device
Dinesh Ram51733322013-10-15 12:24:38 -0300537 * 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. */
Hans Verkuilcb15da32013-12-06 06:48:49 -0300540 usleep_range(3000, 4000);
Dinesh Ram51733322013-10-15 12:24:38 -0300541 }
Eduardo Valentin02bee892009-08-08 08:46:53 -0300542}
543
544/*
545 * si4713_tx_tune_freq - Sets the state of the RF carrier and sets the tuning
Hans Verkuil9ca33cc2013-12-06 06:48:49 -0300546 * frequency between 76 and 108 MHz in 10 kHz units and
547 * steps of 50 kHz.
Eduardo Valentin02bee892009-08-08 08:46:53 -0300548 * @sdev: si4713_device structure for the device we are communicating
549 * @frequency: desired frequency (76 - 108 MHz, unit 10 KHz, step 50 kHz)
550 */
551static int si4713_tx_tune_freq(struct si4713_device *sdev, u16 frequency)
552{
553 int err;
554 u8 val[SI4713_TXFREQ_NRESP];
555 /*
Hans Verkuil9ca33cc2013-12-06 06:48:49 -0300556 * .First byte = 0
557 * .Second byte = frequency's MSB
558 * .Third byte = frequency's LSB
Eduardo Valentin02bee892009-08-08 08:46:53 -0300559 */
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/*
Dinesh Rame26fa6d2013-10-15 12:24:40 -0300585 * si4713_tx_tune_power - Sets the RF voltage level between 88 and 120 dBuV in
Hans Verkuil9ca33cc2013-12-06 06:48:49 -0300586 * 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.
Eduardo Valentin02bee892009-08-08 08:46:53 -0300591 * @sdev: si4713_device structure for the device we are communicating
Dinesh Rame26fa6d2013-10-15 12:24:40 -0300592 * @power: tuning power (88 - 120 dBuV, unit/step 1 dB)
Eduardo Valentin02bee892009-08-08 08:46:53 -0300593 * @antcap: value of antenna tuning capacitor (0 - 191)
594 */
595static 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 /*
Hans Verkuil9ca33cc2013-12-06 06:48:49 -0300601 * .First byte = 0
602 * .Second byte = 0
603 * .Third byte = power
604 * .Fourth byte = antcap
Eduardo Valentin02bee892009-08-08 08:46:53 -0300605 */
Dinesh Rame26fa6d2013-10-15 12:24:40 -0300606 u8 args[SI4713_TXPWR_NARGS] = {
Eduardo Valentin02bee892009-08-08 08:46:53 -0300607 0x00,
608 0x00,
609 power,
610 antcap,
611 };
612
Dinesh Rame26fa6d2013-10-15 12:24:40 -0300613 /* Map power values 1-87 to MIN_POWER (88) */
614 if (power > 0 && power < SI4713_MIN_POWER)
615 args[2] = power = SI4713_MIN_POWER;
Eduardo Valentin02bee892009-08-08 08:46:53 -0300616
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
Hans Verkuil9ca33cc2013-12-06 06:48:49 -0300633 * 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.
Eduardo Valentin02bee892009-08-08 08:46:53 -0300639 * @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 */
643static 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 /*
Hans Verkuil9ca33cc2013-12-06 06:48:49 -0300649 * .First byte = 0
650 * .Second byte = frequency's MSB
651 * .Third byte = frequency's LSB
652 * .Fourth byte = antcap
Eduardo Valentin02bee892009-08-08 08:46:53 -0300653 */
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
Hans Verkuil9ca33cc2013-12-06 06:48:49 -0300682 * 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.
Eduardo Valentin02bee892009-08-08 08:46:53 -0300687 * @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 */
694static 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 /*
Hans Verkuil9ca33cc2013-12-06 06:48:49 -0300701 * .First byte = intack bit
Eduardo Valentin02bee892009-08-08 08:46:53 -0300702 */
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, "%s: response: %d x 10 kHz "
720 "(power %d, antcap %d, rnl %d)\n", __func__,
721 *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 */
737static 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, "%s: response: interrupts"
762 " 0x%02x cb avail: %d cb used %d fifo avail"
763 " %d fifo used %d\n", __func__, val[1],
764 val[2], val[3], val[4], val[5]);
765 }
766
767 return err;
768}
769
770/*
771 * si4713_tx_rds_ps - Loads the program service buffer.
772 * @sdev: si4713_device structure for the device we are communicating
773 * @psid: program service id to be loaded.
774 * @pschar: assumed 4 size char array to be loaded into the program service
775 */
776static int si4713_tx_rds_ps(struct si4713_device *sdev, u8 psid,
777 unsigned char *pschar)
778{
779 int err;
780 u8 val[SI4713_RDSPS_NRESP];
781
782 const u8 args[SI4713_RDSPS_NARGS] = {
783 psid & SI4713_RDSPS_PSID_MASK,
784 pschar[0],
785 pschar[1],
786 pschar[2],
787 pschar[3],
788 };
789
790 err = si4713_send_command(sdev, SI4713_CMD_TX_RDS_PS,
791 args, ARRAY_SIZE(args), val,
792 ARRAY_SIZE(val), DEFAULT_TIMEOUT);
793
794 if (err < 0)
795 return err;
796
797 v4l2_dbg(1, debug, &sdev->sd, "%s: status=0x%02x\n", __func__, val[0]);
798
799 return err;
800}
801
802static int si4713_set_power_state(struct si4713_device *sdev, u8 value)
803{
Eduardo Valentin02bee892009-08-08 08:46:53 -0300804 if (value)
Hans Verkuil55b2a312013-04-08 06:31:30 -0300805 return si4713_powerup(sdev);
806 return si4713_powerdown(sdev);
Eduardo Valentin02bee892009-08-08 08:46:53 -0300807}
808
809static int si4713_set_mute(struct si4713_device *sdev, u16 mute)
810{
811 int rval = 0;
812
813 mute = set_mute(mute);
814
Eduardo Valentin02bee892009-08-08 08:46:53 -0300815 if (sdev->power_state)
816 rval = si4713_write_property(sdev,
817 SI4713_TX_LINE_INPUT_MUTE, mute);
818
Eduardo Valentin02bee892009-08-08 08:46:53 -0300819 return rval;
820}
821
822static int si4713_set_rds_ps_name(struct si4713_device *sdev, char *ps_name)
823{
824 int rval = 0, i;
825 u8 len = 0;
826
827 /* We want to clear the whole thing */
828 if (!strlen(ps_name))
829 memset(ps_name, 0, MAX_RDS_PS_NAME + 1);
830
Eduardo Valentin02bee892009-08-08 08:46:53 -0300831 if (sdev->power_state) {
832 /* Write the new ps name and clear the padding */
833 for (i = 0; i < MAX_RDS_PS_NAME; i += (RDS_BLOCK / 2)) {
834 rval = si4713_tx_rds_ps(sdev, (i / (RDS_BLOCK / 2)),
835 ps_name + i);
836 if (rval < 0)
Hans Verkuil55b2a312013-04-08 06:31:30 -0300837 return rval;
Eduardo Valentin02bee892009-08-08 08:46:53 -0300838 }
839
840 /* Setup the size to be sent */
841 if (strlen(ps_name))
842 len = strlen(ps_name) - 1;
843 else
844 len = 1;
845
846 rval = si4713_write_property(sdev,
847 SI4713_TX_RDS_PS_MESSAGE_COUNT,
848 rds_ps_nblocks(len));
849 if (rval < 0)
Hans Verkuil55b2a312013-04-08 06:31:30 -0300850 return rval;
Eduardo Valentin02bee892009-08-08 08:46:53 -0300851
852 rval = si4713_write_property(sdev,
853 SI4713_TX_RDS_PS_REPEAT_COUNT,
854 DEFAULT_RDS_PS_REPEAT_COUNT * 2);
855 if (rval < 0)
Hans Verkuil55b2a312013-04-08 06:31:30 -0300856 return rval;
Eduardo Valentin02bee892009-08-08 08:46:53 -0300857 }
858
Eduardo Valentin02bee892009-08-08 08:46:53 -0300859 return rval;
860}
861
Hans Verkuil8aab72b2013-10-15 12:24:45 -0300862static int si4713_set_rds_radio_text(struct si4713_device *sdev, const char *rt)
Eduardo Valentin02bee892009-08-08 08:46:53 -0300863{
Hans Verkuil8aab72b2013-10-15 12:24:45 -0300864 static const char cr[RDS_RADIOTEXT_BLK_SIZE] = { RDS_CARRIAGE_RETURN, 0 };
Eduardo Valentin02bee892009-08-08 08:46:53 -0300865 int rval = 0, i;
866 u16 t_index = 0;
867 u8 b_index = 0, cr_inserted = 0;
868 s8 left;
869
Eduardo Valentin02bee892009-08-08 08:46:53 -0300870 if (!sdev->power_state)
Hans Verkuil03aa1bc2013-04-08 17:27:18 -0300871 return rval;
Eduardo Valentin02bee892009-08-08 08:46:53 -0300872
873 rval = si4713_tx_rds_buff(sdev, RDS_BLOCK_CLEAR, 0, 0, 0, &left);
874 if (rval < 0)
Hans Verkuil55b2a312013-04-08 06:31:30 -0300875 return rval;
Eduardo Valentin02bee892009-08-08 08:46:53 -0300876
877 if (!strlen(rt))
Hans Verkuil03aa1bc2013-04-08 17:27:18 -0300878 return rval;
Eduardo Valentin02bee892009-08-08 08:46:53 -0300879
880 do {
881 /* RDS spec says that if the last block isn't used,
882 * then apply a carriage return
883 */
Hans Verkuil03aa1bc2013-04-08 17:27:18 -0300884 if (t_index < (RDS_RADIOTEXT_INDEX_MAX * RDS_RADIOTEXT_BLK_SIZE)) {
Eduardo Valentin02bee892009-08-08 08:46:53 -0300885 for (i = 0; i < RDS_RADIOTEXT_BLK_SIZE; i++) {
Hans Verkuil03aa1bc2013-04-08 17:27:18 -0300886 if (!rt[t_index + i] ||
887 rt[t_index + i] == RDS_CARRIAGE_RETURN) {
Hans Verkuil8aab72b2013-10-15 12:24:45 -0300888 rt = cr;
Eduardo Valentin02bee892009-08-08 08:46:53 -0300889 cr_inserted = 1;
890 break;
891 }
892 }
893 }
894
895 rval = si4713_tx_rds_buff(sdev, RDS_BLOCK_LOAD,
896 compose_u16(RDS_RADIOTEXT_2A, b_index++),
897 compose_u16(rt[t_index], rt[t_index + 1]),
898 compose_u16(rt[t_index + 2], rt[t_index + 3]),
899 &left);
900 if (rval < 0)
Hans Verkuil55b2a312013-04-08 06:31:30 -0300901 return rval;
Eduardo Valentin02bee892009-08-08 08:46:53 -0300902
903 t_index += RDS_RADIOTEXT_BLK_SIZE;
904
905 if (cr_inserted)
906 break;
907 } while (left > 0);
908
Hans Verkuil03aa1bc2013-04-08 17:27:18 -0300909 return rval;
910}
911
912/*
913 * si4713_update_tune_status - update properties from tx_tune_status
914 * command. Must be called with sdev->mutex held.
915 * @sdev: si4713_device structure for the device we are communicating
916 */
917static int si4713_update_tune_status(struct si4713_device *sdev)
918{
919 int rval;
920 u16 f = 0;
921 u8 p = 0, a = 0, n = 0;
922
923 rval = si4713_tx_tune_status(sdev, 0x00, &f, &p, &a, &n);
924
925 if (rval < 0)
926 goto exit;
927
928/* TODO: check that power_level and antenna_capacitor really are not
929 changed by the hardware. If they are, then these controls should become
930 volatiles.
931 sdev->power_level = p;
932 sdev->antenna_capacitor = a;*/
933 sdev->tune_rnl = n;
934
935exit:
Eduardo Valentin02bee892009-08-08 08:46:53 -0300936 return rval;
937}
938
939static int si4713_choose_econtrol_action(struct si4713_device *sdev, u32 id,
Hans Verkuil03aa1bc2013-04-08 17:27:18 -0300940 s32 *bit, s32 *mask, u16 *property, int *mul,
Eduardo Valentin02bee892009-08-08 08:46:53 -0300941 unsigned long **table, int *size)
942{
943 s32 rval = 0;
944
945 switch (id) {
946 /* FM_TX class controls */
947 case V4L2_CID_RDS_TX_PI:
948 *property = SI4713_TX_RDS_PI;
949 *mul = 1;
Eduardo Valentin02bee892009-08-08 08:46:53 -0300950 break;
951 case V4L2_CID_AUDIO_COMPRESSION_THRESHOLD:
952 *property = SI4713_TX_ACOMP_THRESHOLD;
953 *mul = 1;
Eduardo Valentin02bee892009-08-08 08:46:53 -0300954 break;
955 case V4L2_CID_AUDIO_COMPRESSION_GAIN:
956 *property = SI4713_TX_ACOMP_GAIN;
957 *mul = 1;
Eduardo Valentin02bee892009-08-08 08:46:53 -0300958 break;
959 case V4L2_CID_PILOT_TONE_FREQUENCY:
960 *property = SI4713_TX_PILOT_FREQUENCY;
961 *mul = 1;
Eduardo Valentin02bee892009-08-08 08:46:53 -0300962 break;
963 case V4L2_CID_AUDIO_COMPRESSION_ATTACK_TIME:
964 *property = SI4713_TX_ACOMP_ATTACK_TIME;
965 *mul = ATTACK_TIME_UNIT;
Eduardo Valentin02bee892009-08-08 08:46:53 -0300966 break;
967 case V4L2_CID_PILOT_TONE_DEVIATION:
968 *property = SI4713_TX_PILOT_DEVIATION;
969 *mul = 10;
Eduardo Valentin02bee892009-08-08 08:46:53 -0300970 break;
971 case V4L2_CID_AUDIO_LIMITER_DEVIATION:
972 *property = SI4713_TX_AUDIO_DEVIATION;
973 *mul = 10;
Eduardo Valentin02bee892009-08-08 08:46:53 -0300974 break;
975 case V4L2_CID_RDS_TX_DEVIATION:
976 *property = SI4713_TX_RDS_DEVIATION;
977 *mul = 1;
Eduardo Valentin02bee892009-08-08 08:46:53 -0300978 break;
979
980 case V4L2_CID_RDS_TX_PTY:
981 *property = SI4713_TX_RDS_PS_MISC;
982 *bit = 5;
983 *mask = 0x1F << 5;
Eduardo Valentin02bee892009-08-08 08:46:53 -0300984 break;
Hans Verkuil1abba282014-07-21 10:45:39 -0300985 case V4L2_CID_RDS_TX_DYNAMIC_PTY:
986 *property = SI4713_TX_RDS_PS_MISC;
987 *bit = 15;
988 *mask = 1 << 15;
989 break;
990 case V4L2_CID_RDS_TX_COMPRESSED:
991 *property = SI4713_TX_RDS_PS_MISC;
992 *bit = 14;
993 *mask = 1 << 14;
994 break;
995 case V4L2_CID_RDS_TX_ARTIFICIAL_HEAD:
996 *property = SI4713_TX_RDS_PS_MISC;
997 *bit = 13;
998 *mask = 1 << 13;
999 break;
1000 case V4L2_CID_RDS_TX_MONO_STEREO:
1001 *property = SI4713_TX_RDS_PS_MISC;
1002 *bit = 12;
1003 *mask = 1 << 12;
1004 break;
1005 case V4L2_CID_RDS_TX_TRAFFIC_PROGRAM:
1006 *property = SI4713_TX_RDS_PS_MISC;
1007 *bit = 10;
1008 *mask = 1 << 10;
1009 break;
1010 case V4L2_CID_RDS_TX_TRAFFIC_ANNOUNCEMENT:
1011 *property = SI4713_TX_RDS_PS_MISC;
1012 *bit = 4;
1013 *mask = 1 << 4;
1014 break;
1015 case V4L2_CID_RDS_TX_MUSIC_SPEECH:
1016 *property = SI4713_TX_RDS_PS_MISC;
1017 *bit = 3;
1018 *mask = 1 << 3;
1019 break;
Eduardo Valentin02bee892009-08-08 08:46:53 -03001020 case V4L2_CID_AUDIO_LIMITER_ENABLED:
1021 *property = SI4713_TX_ACOMP_ENABLE;
1022 *bit = 1;
1023 *mask = 1 << 1;
Eduardo Valentin02bee892009-08-08 08:46:53 -03001024 break;
1025 case V4L2_CID_AUDIO_COMPRESSION_ENABLED:
1026 *property = SI4713_TX_ACOMP_ENABLE;
1027 *bit = 0;
1028 *mask = 1 << 0;
Eduardo Valentin02bee892009-08-08 08:46:53 -03001029 break;
1030 case V4L2_CID_PILOT_TONE_ENABLED:
1031 *property = SI4713_TX_COMPONENT_ENABLE;
1032 *bit = 0;
1033 *mask = 1 << 0;
Eduardo Valentin02bee892009-08-08 08:46:53 -03001034 break;
1035
1036 case V4L2_CID_AUDIO_LIMITER_RELEASE_TIME:
1037 *property = SI4713_TX_LIMITER_RELEASE_TIME;
1038 *table = limiter_times;
1039 *size = ARRAY_SIZE(limiter_times);
Eduardo Valentin02bee892009-08-08 08:46:53 -03001040 break;
1041 case V4L2_CID_AUDIO_COMPRESSION_RELEASE_TIME:
1042 *property = SI4713_TX_ACOMP_RELEASE_TIME;
1043 *table = acomp_rtimes;
1044 *size = ARRAY_SIZE(acomp_rtimes);
Eduardo Valentin02bee892009-08-08 08:46:53 -03001045 break;
1046 case V4L2_CID_TUNE_PREEMPHASIS:
1047 *property = SI4713_TX_PREEMPHASIS;
1048 *table = preemphasis_values;
1049 *size = ARRAY_SIZE(preemphasis_values);
Eduardo Valentin02bee892009-08-08 08:46:53 -03001050 break;
1051
1052 default:
1053 rval = -EINVAL;
1054 break;
Peter Senna Tschudincb9e40f2012-09-06 12:09:11 -03001055 }
Eduardo Valentin02bee892009-08-08 08:46:53 -03001056
Eduardo Valentin02bee892009-08-08 08:46:53 -03001057 return rval;
1058}
1059
Hans Verkuilb530a442013-03-19 04:09:26 -03001060static int si4713_s_frequency(struct v4l2_subdev *sd, const struct v4l2_frequency *f);
Hans Verkuil3f70e1f52012-09-04 12:08:47 -03001061static int si4713_s_modulator(struct v4l2_subdev *sd, const struct v4l2_modulator *);
Eduardo Valentin02bee892009-08-08 08:46:53 -03001062/*
1063 * si4713_setup - Sets the device up with current configuration.
1064 * @sdev: si4713_device structure for the device we are communicating
1065 */
1066static int si4713_setup(struct si4713_device *sdev)
1067{
Eduardo Valentin02bee892009-08-08 08:46:53 -03001068 struct v4l2_frequency f;
1069 struct v4l2_modulator vm;
Hans Verkuil03aa1bc2013-04-08 17:27:18 -03001070 int rval;
Eduardo Valentin02bee892009-08-08 08:46:53 -03001071
1072 /* Device procedure needs to set frequency first */
Hans Verkuilb3877542013-04-08 17:25:05 -03001073 f.tuner = 0;
Hans Verkuil03aa1bc2013-04-08 17:27:18 -03001074 f.frequency = sdev->frequency ? sdev->frequency : DEFAULT_FREQUENCY;
Eduardo Valentin02bee892009-08-08 08:46:53 -03001075 f.frequency = si4713_to_v4l2(f.frequency);
Hans Verkuil03aa1bc2013-04-08 17:27:18 -03001076 rval = si4713_s_frequency(&sdev->sd, &f);
Eduardo Valentin02bee892009-08-08 08:46:53 -03001077
1078 vm.index = 0;
Hans Verkuil03aa1bc2013-04-08 17:27:18 -03001079 if (sdev->stereo)
Eduardo Valentin02bee892009-08-08 08:46:53 -03001080 vm.txsubchans = V4L2_TUNER_SUB_STEREO;
1081 else
1082 vm.txsubchans = V4L2_TUNER_SUB_MONO;
Hans Verkuil03aa1bc2013-04-08 17:27:18 -03001083 if (sdev->rds_enabled)
Eduardo Valentin02bee892009-08-08 08:46:53 -03001084 vm.txsubchans |= V4L2_TUNER_SUB_RDS;
1085 si4713_s_modulator(&sdev->sd, &vm);
1086
Eduardo Valentin02bee892009-08-08 08:46:53 -03001087 return rval;
1088}
1089
1090/*
1091 * si4713_initialize - Sets the device up with default configuration.
1092 * @sdev: si4713_device structure for the device we are communicating
1093 */
1094static int si4713_initialize(struct si4713_device *sdev)
1095{
1096 int rval;
1097
1098 rval = si4713_set_power_state(sdev, POWER_ON);
1099 if (rval < 0)
Hans Verkuil55b2a312013-04-08 06:31:30 -03001100 return rval;
Eduardo Valentin02bee892009-08-08 08:46:53 -03001101
1102 rval = si4713_checkrev(sdev);
1103 if (rval < 0)
Hans Verkuil55b2a312013-04-08 06:31:30 -03001104 return rval;
Eduardo Valentin02bee892009-08-08 08:46:53 -03001105
1106 rval = si4713_set_power_state(sdev, POWER_OFF);
1107 if (rval < 0)
Hans Verkuil55b2a312013-04-08 06:31:30 -03001108 return rval;
Eduardo Valentin02bee892009-08-08 08:46:53 -03001109
Eduardo Valentin02bee892009-08-08 08:46:53 -03001110 sdev->frequency = DEFAULT_FREQUENCY;
Eduardo Valentin02bee892009-08-08 08:46:53 -03001111 sdev->stereo = 1;
1112 sdev->tune_rnl = DEFAULT_TUNE_RNL;
Hans Verkuil03aa1bc2013-04-08 17:27:18 -03001113 return 0;
Eduardo Valentin02bee892009-08-08 08:46:53 -03001114}
1115
Hans Verkuil03aa1bc2013-04-08 17:27:18 -03001116/* si4713_s_ctrl - set the value of a control */
1117static int si4713_s_ctrl(struct v4l2_ctrl *ctrl)
Eduardo Valentin02bee892009-08-08 08:46:53 -03001118{
Hans Verkuil03aa1bc2013-04-08 17:27:18 -03001119 struct si4713_device *sdev =
1120 container_of(ctrl->handler, struct si4713_device, ctrl_handler);
1121 u32 val = 0;
Eduardo Valentin02bee892009-08-08 08:46:53 -03001122 s32 bit = 0, mask = 0;
1123 u16 property = 0;
1124 int mul = 0;
1125 unsigned long *table = NULL;
1126 int size = 0;
Hans Verkuil03aa1bc2013-04-08 17:27:18 -03001127 bool force = false;
1128 int c;
1129 int ret = 0;
Eduardo Valentin02bee892009-08-08 08:46:53 -03001130
Hans Verkuil03aa1bc2013-04-08 17:27:18 -03001131 if (ctrl->id != V4L2_CID_AUDIO_MUTE)
Eduardo Valentin02bee892009-08-08 08:46:53 -03001132 return -EINVAL;
Hans Verkuil03aa1bc2013-04-08 17:27:18 -03001133 if (ctrl->is_new) {
1134 if (ctrl->val) {
1135 ret = si4713_set_mute(sdev, ctrl->val);
1136 if (!ret)
1137 ret = si4713_set_power_state(sdev, POWER_DOWN);
1138 return ret;
1139 }
1140 ret = si4713_set_power_state(sdev, POWER_UP);
1141 if (!ret)
1142 ret = si4713_set_mute(sdev, ctrl->val);
1143 if (!ret)
1144 ret = si4713_setup(sdev);
1145 if (ret)
1146 return ret;
1147 force = true;
1148 }
Eduardo Valentin02bee892009-08-08 08:46:53 -03001149
Hans Verkuil03aa1bc2013-04-08 17:27:18 -03001150 if (!sdev->power_state)
1151 return 0;
Eduardo Valentin02bee892009-08-08 08:46:53 -03001152
Hans Verkuil03aa1bc2013-04-08 17:27:18 -03001153 for (c = 1; !ret && c < ctrl->ncontrols; c++) {
1154 ctrl = ctrl->cluster[c];
1155
1156 if (!force && !ctrl->is_new)
1157 continue;
1158
1159 switch (ctrl->id) {
Eduardo Valentin02bee892009-08-08 08:46:53 -03001160 case V4L2_CID_RDS_TX_PS_NAME:
Hans Verkuil2a9ec372014-04-27 03:38:13 -03001161 ret = si4713_set_rds_ps_name(sdev, ctrl->p_new.p_char);
Hans Verkuil03aa1bc2013-04-08 17:27:18 -03001162 break;
1163
Eduardo Valentin02bee892009-08-08 08:46:53 -03001164 case V4L2_CID_RDS_TX_RADIO_TEXT:
Hans Verkuil2a9ec372014-04-27 03:38:13 -03001165 ret = si4713_set_rds_radio_text(sdev, ctrl->p_new.p_char);
Eduardo Valentin02bee892009-08-08 08:46:53 -03001166 break;
Hans Verkuil03aa1bc2013-04-08 17:27:18 -03001167
Eduardo Valentin02bee892009-08-08 08:46:53 -03001168 case V4L2_CID_TUNE_ANTENNA_CAPACITOR:
Hans Verkuil03aa1bc2013-04-08 17:27:18 -03001169 /* don't handle this control if we force setting all
1170 * controls since in that case it will be handled by
1171 * V4L2_CID_TUNE_POWER_LEVEL. */
1172 if (force)
1173 break;
1174 /* fall through */
Eduardo Valentin02bee892009-08-08 08:46:53 -03001175 case V4L2_CID_TUNE_POWER_LEVEL:
Hans Verkuil03aa1bc2013-04-08 17:27:18 -03001176 ret = si4713_tx_tune_power(sdev,
1177 sdev->tune_pwr_level->val, sdev->tune_ant_cap->val);
1178 if (!ret) {
1179 /* Make sure we don't set this twice */
1180 sdev->tune_ant_cap->is_new = false;
1181 sdev->tune_pwr_level->is_new = false;
1182 }
Eduardo Valentin02bee892009-08-08 08:46:53 -03001183 break;
Hans Verkuil03aa1bc2013-04-08 17:27:18 -03001184
Hans Verkuil1abba282014-07-21 10:45:39 -03001185 case V4L2_CID_RDS_TX_ALT_FREQS_ENABLE:
1186 case V4L2_CID_RDS_TX_ALT_FREQS:
1187 if (sdev->rds_alt_freqs_enable->val) {
1188 val = sdev->rds_alt_freqs->p_new.p_u32[0];
1189 val = val / 100 - 876 + 0xe101;
1190 } else {
1191 val = 0xe0e0;
1192 }
1193 ret = si4713_write_property(sdev, SI4713_TX_RDS_PS_AF, val);
1194 break;
1195
Eduardo Valentin02bee892009-08-08 08:46:53 -03001196 default:
Hans Verkuil03aa1bc2013-04-08 17:27:18 -03001197 ret = si4713_choose_econtrol_action(sdev, ctrl->id, &bit,
1198 &mask, &property, &mul, &table, &size);
1199 if (ret < 0)
1200 break;
Eduardo Valentin02bee892009-08-08 08:46:53 -03001201
Hans Verkuil03aa1bc2013-04-08 17:27:18 -03001202 val = ctrl->val;
1203 if (mul) {
1204 val = val / mul;
1205 } else if (table) {
1206 ret = usecs_to_dev(val, table, size);
1207 if (ret < 0)
1208 break;
1209 val = ret;
1210 ret = 0;
1211 }
Eduardo Valentin02bee892009-08-08 08:46:53 -03001212
Hans Verkuil03aa1bc2013-04-08 17:27:18 -03001213 if (mask) {
1214 ret = si4713_read_property(sdev, property, &val);
1215 if (ret < 0)
1216 break;
1217 val = set_bits(val, ctrl->val, bit, mask);
1218 }
Eduardo Valentin02bee892009-08-08 08:46:53 -03001219
Hans Verkuil03aa1bc2013-04-08 17:27:18 -03001220 ret = si4713_write_property(sdev, property, val);
1221 if (ret < 0)
1222 break;
1223 if (mask)
1224 val = ctrl->val;
Eduardo Valentin02bee892009-08-08 08:46:53 -03001225 break;
Eduardo Valentin02bee892009-08-08 08:46:53 -03001226 }
1227 }
1228
Hans Verkuil03aa1bc2013-04-08 17:27:18 -03001229 return ret;
Eduardo Valentin02bee892009-08-08 08:46:53 -03001230}
1231
1232/* si4713_ioctl - deal with private ioctls (only rnl for now) */
Mauro Carvalho Chehab0dec8682012-10-27 18:35:46 -02001233static long si4713_ioctl(struct v4l2_subdev *sd, unsigned int cmd, void *arg)
Eduardo Valentin02bee892009-08-08 08:46:53 -03001234{
1235 struct si4713_device *sdev = to_si4713_device(sd);
1236 struct si4713_rnl *rnl = arg;
1237 u16 frequency;
1238 int rval = 0;
1239
1240 if (!arg)
1241 return -EINVAL;
1242
Eduardo Valentin02bee892009-08-08 08:46:53 -03001243 switch (cmd) {
1244 case SI4713_IOC_MEASURE_RNL:
1245 frequency = v4l2_to_si4713(rnl->frequency);
1246
1247 if (sdev->power_state) {
1248 /* Set desired measurement frequency */
1249 rval = si4713_tx_tune_measure(sdev, frequency, 0);
1250 if (rval < 0)
Hans Verkuil55b2a312013-04-08 06:31:30 -03001251 return rval;
Eduardo Valentin02bee892009-08-08 08:46:53 -03001252 /* get results from tune status */
1253 rval = si4713_update_tune_status(sdev);
1254 if (rval < 0)
Hans Verkuil55b2a312013-04-08 06:31:30 -03001255 return rval;
Eduardo Valentin02bee892009-08-08 08:46:53 -03001256 }
1257 rnl->rnl = sdev->tune_rnl;
1258 break;
1259
1260 default:
1261 /* nothing */
1262 rval = -ENOIOCTLCMD;
1263 }
1264
Eduardo Valentin02bee892009-08-08 08:46:53 -03001265 return rval;
1266}
1267
Eduardo Valentin02bee892009-08-08 08:46:53 -03001268/* si4713_g_modulator - get modulator attributes */
1269static int si4713_g_modulator(struct v4l2_subdev *sd, struct v4l2_modulator *vm)
1270{
1271 struct si4713_device *sdev = to_si4713_device(sd);
1272 int rval = 0;
1273
Hans Verkuil55b2a312013-04-08 06:31:30 -03001274 if (!sdev)
1275 return -ENODEV;
Eduardo Valentin02bee892009-08-08 08:46:53 -03001276
Hans Verkuil55b2a312013-04-08 06:31:30 -03001277 if (vm->index > 0)
1278 return -EINVAL;
Eduardo Valentin02bee892009-08-08 08:46:53 -03001279
1280 strncpy(vm->name, "FM Modulator", 32);
1281 vm->capability = V4L2_TUNER_CAP_STEREO | V4L2_TUNER_CAP_LOW |
Matti Aaltonencb0ed222010-10-18 06:54:14 -03001282 V4L2_TUNER_CAP_RDS | V4L2_TUNER_CAP_RDS_CONTROLS;
Eduardo Valentin02bee892009-08-08 08:46:53 -03001283
1284 /* Report current frequency range limits */
1285 vm->rangelow = si4713_to_v4l2(FREQ_RANGE_LOW);
1286 vm->rangehigh = si4713_to_v4l2(FREQ_RANGE_HIGH);
1287
Eduardo Valentin02bee892009-08-08 08:46:53 -03001288 if (sdev->power_state) {
1289 u32 comp_en = 0;
1290
1291 rval = si4713_read_property(sdev, SI4713_TX_COMPONENT_ENABLE,
1292 &comp_en);
1293 if (rval < 0)
Hans Verkuil55b2a312013-04-08 06:31:30 -03001294 return rval;
Eduardo Valentin02bee892009-08-08 08:46:53 -03001295
1296 sdev->stereo = get_status_bit(comp_en, 1, 1 << 1);
Eduardo Valentin02bee892009-08-08 08:46:53 -03001297 }
1298
1299 /* Report current audio mode: mono or stereo */
1300 if (sdev->stereo)
1301 vm->txsubchans = V4L2_TUNER_SUB_STEREO;
1302 else
1303 vm->txsubchans = V4L2_TUNER_SUB_MONO;
1304
1305 /* Report rds feature status */
Hans Verkuil03aa1bc2013-04-08 17:27:18 -03001306 if (sdev->rds_enabled)
Eduardo Valentin02bee892009-08-08 08:46:53 -03001307 vm->txsubchans |= V4L2_TUNER_SUB_RDS;
1308 else
1309 vm->txsubchans &= ~V4L2_TUNER_SUB_RDS;
1310
Eduardo Valentin02bee892009-08-08 08:46:53 -03001311 return rval;
1312}
1313
1314/* si4713_s_modulator - set modulator attributes */
Hans Verkuil3f70e1f52012-09-04 12:08:47 -03001315static int si4713_s_modulator(struct v4l2_subdev *sd, const struct v4l2_modulator *vm)
Eduardo Valentin02bee892009-08-08 08:46:53 -03001316{
1317 struct si4713_device *sdev = to_si4713_device(sd);
1318 int rval = 0;
1319 u16 stereo, rds;
1320 u32 p;
1321
Hans Verkuila65f3152009-08-31 17:21:04 -03001322 if (!sdev)
1323 return -ENODEV;
Eduardo Valentin02bee892009-08-08 08:46:53 -03001324
Hans Verkuila65f3152009-08-31 17:21:04 -03001325 if (vm->index > 0)
1326 return -EINVAL;
Eduardo Valentin02bee892009-08-08 08:46:53 -03001327
1328 /* Set audio mode: mono or stereo */
1329 if (vm->txsubchans & V4L2_TUNER_SUB_STEREO)
1330 stereo = 1;
1331 else if (vm->txsubchans & V4L2_TUNER_SUB_MONO)
1332 stereo = 0;
1333 else
Hans Verkuila65f3152009-08-31 17:21:04 -03001334 return -EINVAL;
Eduardo Valentin02bee892009-08-08 08:46:53 -03001335
1336 rds = !!(vm->txsubchans & V4L2_TUNER_SUB_RDS);
1337
Eduardo Valentin02bee892009-08-08 08:46:53 -03001338 if (sdev->power_state) {
1339 rval = si4713_read_property(sdev,
1340 SI4713_TX_COMPONENT_ENABLE, &p);
1341 if (rval < 0)
Hans Verkuil55b2a312013-04-08 06:31:30 -03001342 return rval;
Eduardo Valentin02bee892009-08-08 08:46:53 -03001343
1344 p = set_bits(p, stereo, 1, 1 << 1);
1345 p = set_bits(p, rds, 2, 1 << 2);
1346
1347 rval = si4713_write_property(sdev,
1348 SI4713_TX_COMPONENT_ENABLE, p);
1349 if (rval < 0)
Hans Verkuil55b2a312013-04-08 06:31:30 -03001350 return rval;
Eduardo Valentin02bee892009-08-08 08:46:53 -03001351 }
1352
1353 sdev->stereo = stereo;
Hans Verkuil03aa1bc2013-04-08 17:27:18 -03001354 sdev->rds_enabled = rds;
Eduardo Valentin02bee892009-08-08 08:46:53 -03001355
Eduardo Valentin02bee892009-08-08 08:46:53 -03001356 return rval;
1357}
1358
1359/* si4713_g_frequency - get tuner or modulator radio frequency */
1360static int si4713_g_frequency(struct v4l2_subdev *sd, struct v4l2_frequency *f)
1361{
1362 struct si4713_device *sdev = to_si4713_device(sd);
1363 int rval = 0;
1364
Hans Verkuilb3877542013-04-08 17:25:05 -03001365 if (f->tuner)
1366 return -EINVAL;
Eduardo Valentin02bee892009-08-08 08:46:53 -03001367
Eduardo Valentin02bee892009-08-08 08:46:53 -03001368 if (sdev->power_state) {
1369 u16 freq;
1370 u8 p, a, n;
1371
1372 rval = si4713_tx_tune_status(sdev, 0x00, &freq, &p, &a, &n);
1373 if (rval < 0)
Hans Verkuil55b2a312013-04-08 06:31:30 -03001374 return rval;
Eduardo Valentin02bee892009-08-08 08:46:53 -03001375
1376 sdev->frequency = freq;
1377 }
1378
1379 f->frequency = si4713_to_v4l2(sdev->frequency);
1380
Eduardo Valentin02bee892009-08-08 08:46:53 -03001381 return rval;
1382}
1383
1384/* si4713_s_frequency - set tuner or modulator radio frequency */
Hans Verkuilb530a442013-03-19 04:09:26 -03001385static int si4713_s_frequency(struct v4l2_subdev *sd, const struct v4l2_frequency *f)
Eduardo Valentin02bee892009-08-08 08:46:53 -03001386{
1387 struct si4713_device *sdev = to_si4713_device(sd);
1388 int rval = 0;
1389 u16 frequency = v4l2_to_si4713(f->frequency);
1390
Hans Verkuilb3877542013-04-08 17:25:05 -03001391 if (f->tuner)
1392 return -EINVAL;
1393
Eduardo Valentin02bee892009-08-08 08:46:53 -03001394 /* Check frequency range */
Hans Verkuilb3877542013-04-08 17:25:05 -03001395 frequency = clamp_t(u16, frequency, FREQ_RANGE_LOW, FREQ_RANGE_HIGH);
Eduardo Valentin02bee892009-08-08 08:46:53 -03001396
Eduardo Valentin02bee892009-08-08 08:46:53 -03001397 if (sdev->power_state) {
1398 rval = si4713_tx_tune_freq(sdev, frequency);
1399 if (rval < 0)
Hans Verkuil55b2a312013-04-08 06:31:30 -03001400 return rval;
Eduardo Valentin02bee892009-08-08 08:46:53 -03001401 frequency = rval;
1402 rval = 0;
1403 }
1404 sdev->frequency = frequency;
Eduardo Valentin02bee892009-08-08 08:46:53 -03001405
Eduardo Valentin02bee892009-08-08 08:46:53 -03001406 return rval;
1407}
1408
Hans Verkuil03aa1bc2013-04-08 17:27:18 -03001409static const struct v4l2_ctrl_ops si4713_ctrl_ops = {
1410 .s_ctrl = si4713_s_ctrl,
1411};
1412
1413static const struct v4l2_subdev_core_ops si4713_subdev_core_ops = {
1414 .ioctl = si4713_ioctl,
1415};
1416
Eduardo Valentin02bee892009-08-08 08:46:53 -03001417static const struct v4l2_subdev_tuner_ops si4713_subdev_tuner_ops = {
1418 .g_frequency = si4713_g_frequency,
1419 .s_frequency = si4713_s_frequency,
1420 .g_modulator = si4713_g_modulator,
1421 .s_modulator = si4713_s_modulator,
1422};
1423
1424static const struct v4l2_subdev_ops si4713_subdev_ops = {
1425 .core = &si4713_subdev_core_ops,
1426 .tuner = &si4713_subdev_tuner_ops,
1427};
1428
Hans Verkuil1abba282014-07-21 10:45:39 -03001429static const struct v4l2_ctrl_config si4713_alt_freqs_ctrl = {
1430 .id = V4L2_CID_RDS_TX_ALT_FREQS,
1431 .type = V4L2_CTRL_TYPE_U32,
1432 .min = 87600,
1433 .max = 107900,
1434 .step = 100,
1435 .def = 87600,
1436 .dims = { 1 },
1437 .elem_size = sizeof(u32),
1438};
1439
Eduardo Valentin02bee892009-08-08 08:46:53 -03001440/*
1441 * I2C driver interface
1442 */
1443/* si4713_probe - probe for the device */
1444static int si4713_probe(struct i2c_client *client,
1445 const struct i2c_device_id *id)
1446{
1447 struct si4713_device *sdev;
Hans Verkuil03aa1bc2013-04-08 17:27:18 -03001448 struct v4l2_ctrl_handler *hdl;
Sebastian Reichel98bea622014-11-10 17:34:41 -03001449 struct si4713_platform_data *pdata = client->dev.platform_data;
1450 struct device_node *np = client->dev.of_node;
1451 struct radio_si4713_platform_data si4713_pdev_pdata;
1452 struct platform_device *si4713_pdev;
Sebastian Reicheld4471de2014-10-21 12:07:00 -03001453 int rval;
Eduardo Valentin02bee892009-08-08 08:46:53 -03001454
Sebastian Reichel80cd5c72014-10-21 12:07:02 -03001455 sdev = devm_kzalloc(&client->dev, sizeof(*sdev), GFP_KERNEL);
Eduardo Valentin02bee892009-08-08 08:46:53 -03001456 if (!sdev) {
1457 dev_err(&client->dev, "Failed to alloc video device.\n");
1458 rval = -ENOMEM;
1459 goto exit;
1460 }
1461
Uwe Kleine-Königb36ae852015-03-26 17:47:53 -03001462 sdev->gpio_reset = devm_gpiod_get_optional(&client->dev, "reset",
1463 GPIOD_OUT_LOW);
1464 if (IS_ERR(sdev->gpio_reset)) {
Sebastian Reichelfbe14a12014-10-21 12:07:01 -03001465 rval = PTR_ERR(sdev->gpio_reset);
1466 dev_err(&client->dev, "Failed to request gpio: %d\n", rval);
Sebastian Reichel80cd5c72014-10-21 12:07:02 -03001467 goto exit;
Jarkko Nikula00df0552010-10-29 11:31:39 -03001468 }
1469
Sebastian Reicheld4471de2014-10-21 12:07:00 -03001470 sdev->vdd = devm_regulator_get_optional(&client->dev, "vdd");
1471 if (IS_ERR(sdev->vdd)) {
1472 rval = PTR_ERR(sdev->vdd);
1473 if (rval == -EPROBE_DEFER)
1474 goto exit;
Jarkko Nikula00df0552010-10-29 11:31:39 -03001475
Sebastian Reicheld4471de2014-10-21 12:07:00 -03001476 dev_dbg(&client->dev, "no vdd regulator found: %d\n", rval);
1477 sdev->vdd = NULL;
1478 }
1479
1480 sdev->vio = devm_regulator_get_optional(&client->dev, "vio");
1481 if (IS_ERR(sdev->vio)) {
1482 rval = PTR_ERR(sdev->vio);
1483 if (rval == -EPROBE_DEFER)
1484 goto exit;
1485
1486 dev_dbg(&client->dev, "no vio regulator found: %d\n", rval);
1487 sdev->vio = NULL;
Eduardo Valentin02bee892009-08-08 08:46:53 -03001488 }
1489
1490 v4l2_i2c_subdev_init(&sdev->sd, client, &si4713_subdev_ops);
1491
Eduardo Valentin02bee892009-08-08 08:46:53 -03001492 init_completion(&sdev->work);
1493
Hans Verkuil03aa1bc2013-04-08 17:27:18 -03001494 hdl = &sdev->ctrl_handler;
1495 v4l2_ctrl_handler_init(hdl, 20);
1496 sdev->mute = v4l2_ctrl_new_std(hdl, &si4713_ctrl_ops,
1497 V4L2_CID_AUDIO_MUTE, 0, 1, 1, DEFAULT_MUTE);
1498
1499 sdev->rds_pi = v4l2_ctrl_new_std(hdl, &si4713_ctrl_ops,
1500 V4L2_CID_RDS_TX_PI, 0, 0xffff, 1, DEFAULT_RDS_PI);
1501 sdev->rds_pty = v4l2_ctrl_new_std(hdl, &si4713_ctrl_ops,
1502 V4L2_CID_RDS_TX_PTY, 0, 31, 1, DEFAULT_RDS_PTY);
Hans Verkuil1abba282014-07-21 10:45:39 -03001503 sdev->rds_compressed = v4l2_ctrl_new_std(hdl, &si4713_ctrl_ops,
1504 V4L2_CID_RDS_TX_COMPRESSED, 0, 1, 1, 0);
1505 sdev->rds_art_head = v4l2_ctrl_new_std(hdl, &si4713_ctrl_ops,
1506 V4L2_CID_RDS_TX_ARTIFICIAL_HEAD, 0, 1, 1, 0);
1507 sdev->rds_stereo = v4l2_ctrl_new_std(hdl, &si4713_ctrl_ops,
1508 V4L2_CID_RDS_TX_MONO_STEREO, 0, 1, 1, 1);
1509 sdev->rds_tp = v4l2_ctrl_new_std(hdl, &si4713_ctrl_ops,
1510 V4L2_CID_RDS_TX_TRAFFIC_PROGRAM, 0, 1, 1, 0);
1511 sdev->rds_ta = v4l2_ctrl_new_std(hdl, &si4713_ctrl_ops,
1512 V4L2_CID_RDS_TX_TRAFFIC_ANNOUNCEMENT, 0, 1, 1, 0);
1513 sdev->rds_ms = v4l2_ctrl_new_std(hdl, &si4713_ctrl_ops,
1514 V4L2_CID_RDS_TX_MUSIC_SPEECH, 0, 1, 1, 1);
1515 sdev->rds_dyn_pty = v4l2_ctrl_new_std(hdl, &si4713_ctrl_ops,
1516 V4L2_CID_RDS_TX_DYNAMIC_PTY, 0, 1, 1, 0);
1517 sdev->rds_alt_freqs_enable = v4l2_ctrl_new_std(hdl, &si4713_ctrl_ops,
1518 V4L2_CID_RDS_TX_ALT_FREQS_ENABLE, 0, 1, 1, 0);
1519 sdev->rds_alt_freqs = v4l2_ctrl_new_custom(hdl, &si4713_alt_freqs_ctrl, NULL);
Hans Verkuil03aa1bc2013-04-08 17:27:18 -03001520 sdev->rds_deviation = v4l2_ctrl_new_std(hdl, &si4713_ctrl_ops,
1521 V4L2_CID_RDS_TX_DEVIATION, 0, MAX_RDS_DEVIATION,
1522 10, DEFAULT_RDS_DEVIATION);
1523 /*
1524 * Report step as 8. From RDS spec, psname
1525 * should be 8. But there are receivers which scroll strings
1526 * sized as 8xN.
1527 */
1528 sdev->rds_ps_name = v4l2_ctrl_new_std(hdl, &si4713_ctrl_ops,
1529 V4L2_CID_RDS_TX_PS_NAME, 0, MAX_RDS_PS_NAME, 8, 0);
1530 /*
1531 * Report step as 32 (2A block). From RDS spec,
1532 * radio text should be 32 for 2A block. But there are receivers
1533 * which scroll strings sized as 32xN. Setting default to 32.
1534 */
1535 sdev->rds_radio_text = v4l2_ctrl_new_std(hdl, &si4713_ctrl_ops,
1536 V4L2_CID_RDS_TX_RADIO_TEXT, 0, MAX_RDS_RADIO_TEXT, 32, 0);
1537
1538 sdev->limiter_enabled = v4l2_ctrl_new_std(hdl, &si4713_ctrl_ops,
1539 V4L2_CID_AUDIO_LIMITER_ENABLED, 0, 1, 1, 1);
1540 sdev->limiter_release_time = v4l2_ctrl_new_std(hdl, &si4713_ctrl_ops,
1541 V4L2_CID_AUDIO_LIMITER_RELEASE_TIME, 250,
1542 MAX_LIMITER_RELEASE_TIME, 10, DEFAULT_LIMITER_RTIME);
1543 sdev->limiter_deviation = v4l2_ctrl_new_std(hdl, &si4713_ctrl_ops,
1544 V4L2_CID_AUDIO_LIMITER_DEVIATION, 0,
1545 MAX_LIMITER_DEVIATION, 10, DEFAULT_LIMITER_DEV);
1546
1547 sdev->compression_enabled = v4l2_ctrl_new_std(hdl, &si4713_ctrl_ops,
1548 V4L2_CID_AUDIO_COMPRESSION_ENABLED, 0, 1, 1, 1);
1549 sdev->compression_gain = v4l2_ctrl_new_std(hdl, &si4713_ctrl_ops,
1550 V4L2_CID_AUDIO_COMPRESSION_GAIN, 0, MAX_ACOMP_GAIN, 1,
1551 DEFAULT_ACOMP_GAIN);
1552 sdev->compression_threshold = v4l2_ctrl_new_std(hdl, &si4713_ctrl_ops,
Hans Verkuil9ca33cc2013-12-06 06:48:49 -03001553 V4L2_CID_AUDIO_COMPRESSION_THRESHOLD,
1554 MIN_ACOMP_THRESHOLD, MAX_ACOMP_THRESHOLD, 1,
Hans Verkuil03aa1bc2013-04-08 17:27:18 -03001555 DEFAULT_ACOMP_THRESHOLD);
1556 sdev->compression_attack_time = v4l2_ctrl_new_std(hdl, &si4713_ctrl_ops,
1557 V4L2_CID_AUDIO_COMPRESSION_ATTACK_TIME, 0,
1558 MAX_ACOMP_ATTACK_TIME, 500, DEFAULT_ACOMP_ATIME);
1559 sdev->compression_release_time = v4l2_ctrl_new_std(hdl, &si4713_ctrl_ops,
1560 V4L2_CID_AUDIO_COMPRESSION_RELEASE_TIME, 100000,
1561 MAX_ACOMP_RELEASE_TIME, 100000, DEFAULT_ACOMP_RTIME);
1562
1563 sdev->pilot_tone_enabled = v4l2_ctrl_new_std(hdl, &si4713_ctrl_ops,
1564 V4L2_CID_PILOT_TONE_ENABLED, 0, 1, 1, 1);
1565 sdev->pilot_tone_deviation = v4l2_ctrl_new_std(hdl, &si4713_ctrl_ops,
1566 V4L2_CID_PILOT_TONE_DEVIATION, 0, MAX_PILOT_DEVIATION,
1567 10, DEFAULT_PILOT_DEVIATION);
1568 sdev->pilot_tone_freq = v4l2_ctrl_new_std(hdl, &si4713_ctrl_ops,
1569 V4L2_CID_PILOT_TONE_FREQUENCY, 0, MAX_PILOT_FREQUENCY,
1570 1, DEFAULT_PILOT_FREQUENCY);
1571
1572 sdev->tune_preemphasis = v4l2_ctrl_new_std_menu(hdl, &si4713_ctrl_ops,
1573 V4L2_CID_TUNE_PREEMPHASIS,
1574 V4L2_PREEMPHASIS_75_uS, 0, V4L2_PREEMPHASIS_50_uS);
1575 sdev->tune_pwr_level = v4l2_ctrl_new_std(hdl, &si4713_ctrl_ops,
Hans Verkuil9ca33cc2013-12-06 06:48:49 -03001576 V4L2_CID_TUNE_POWER_LEVEL, 0, SI4713_MAX_POWER,
1577 1, DEFAULT_POWER_LEVEL);
Hans Verkuil03aa1bc2013-04-08 17:27:18 -03001578 sdev->tune_ant_cap = v4l2_ctrl_new_std(hdl, &si4713_ctrl_ops,
Hans Verkuil9ca33cc2013-12-06 06:48:49 -03001579 V4L2_CID_TUNE_ANTENNA_CAPACITOR, 0, SI4713_MAX_ANTCAP,
1580 1, 0);
Hans Verkuil03aa1bc2013-04-08 17:27:18 -03001581
1582 if (hdl->error) {
1583 rval = hdl->error;
1584 goto free_ctrls;
1585 }
Hans Verkuil1abba282014-07-21 10:45:39 -03001586 v4l2_ctrl_cluster(29, &sdev->mute);
Hans Verkuil03aa1bc2013-04-08 17:27:18 -03001587 sdev->sd.ctrl_handler = hdl;
1588
Eduardo Valentin02bee892009-08-08 08:46:53 -03001589 if (client->irq) {
Sebastian Reichelc3a5baf2014-10-21 12:07:03 -03001590 rval = devm_request_irq(&client->dev, client->irq,
Michael Opdenacker0798ec62013-10-13 03:05:11 -03001591 si4713_handler, IRQF_TRIGGER_FALLING,
Eduardo Valentin02bee892009-08-08 08:46:53 -03001592 client->name, sdev);
1593 if (rval < 0) {
1594 v4l2_err(&sdev->sd, "Could not request IRQ\n");
Sebastian Reicheld4471de2014-10-21 12:07:00 -03001595 goto free_ctrls;
Eduardo Valentin02bee892009-08-08 08:46:53 -03001596 }
1597 v4l2_dbg(1, debug, &sdev->sd, "IRQ requested.\n");
1598 } else {
1599 v4l2_warn(&sdev->sd, "IRQ not configured. Using timeouts.\n");
1600 }
1601
1602 rval = si4713_initialize(sdev);
1603 if (rval < 0) {
1604 v4l2_err(&sdev->sd, "Failed to probe device information.\n");
Sebastian Reichelc3a5baf2014-10-21 12:07:03 -03001605 goto free_ctrls;
Eduardo Valentin02bee892009-08-08 08:46:53 -03001606 }
1607
Sebastian Reichel98bea622014-11-10 17:34:41 -03001608 if (!np && (!pdata || !pdata->is_platform_device))
1609 return 0;
1610
1611 si4713_pdev = platform_device_alloc("radio-si4713", -1);
Julia Lawall50f24682015-04-05 09:06:22 -03001612 if (!si4713_pdev) {
1613 rval = -ENOMEM;
Sebastian Reichel98bea622014-11-10 17:34:41 -03001614 goto put_main_pdev;
Julia Lawall50f24682015-04-05 09:06:22 -03001615 }
Sebastian Reichel98bea622014-11-10 17:34:41 -03001616
1617 si4713_pdev_pdata.subdev = client;
1618 rval = platform_device_add_data(si4713_pdev, &si4713_pdev_pdata,
1619 sizeof(si4713_pdev_pdata));
1620 if (rval)
1621 goto put_main_pdev;
1622
1623 rval = platform_device_add(si4713_pdev);
1624 if (rval)
1625 goto put_main_pdev;
1626
1627 sdev->pd = si4713_pdev;
1628
Eduardo Valentin02bee892009-08-08 08:46:53 -03001629 return 0;
1630
Sebastian Reichel98bea622014-11-10 17:34:41 -03001631put_main_pdev:
1632 platform_device_put(si4713_pdev);
1633 v4l2_device_unregister_subdev(&sdev->sd);
Hans Verkuil03aa1bc2013-04-08 17:27:18 -03001634free_ctrls:
1635 v4l2_ctrl_handler_free(hdl);
Eduardo Valentin02bee892009-08-08 08:46:53 -03001636exit:
1637 return rval;
1638}
1639
1640/* si4713_remove - remove the device */
1641static int si4713_remove(struct i2c_client *client)
1642{
1643 struct v4l2_subdev *sd = i2c_get_clientdata(client);
1644 struct si4713_device *sdev = to_si4713_device(sd);
1645
Sebastian Reichel98bea622014-11-10 17:34:41 -03001646 platform_device_unregister(sdev->pd);
1647
Eduardo Valentin02bee892009-08-08 08:46:53 -03001648 if (sdev->power_state)
1649 si4713_set_power_state(sdev, POWER_DOWN);
1650
Eduardo Valentin02bee892009-08-08 08:46:53 -03001651 v4l2_device_unregister_subdev(sd);
Hans Verkuil03aa1bc2013-04-08 17:27:18 -03001652 v4l2_ctrl_handler_free(sd->ctrl_handler);
Eduardo Valentin02bee892009-08-08 08:46:53 -03001653
1654 return 0;
1655}
1656
1657/* si4713_i2c_driver - i2c driver interface */
1658static const struct i2c_device_id si4713_id[] = {
1659 { "si4713" , 0 },
1660 { },
1661};
1662MODULE_DEVICE_TABLE(i2c, si4713_id);
1663
1664static struct i2c_driver si4713_i2c_driver = {
1665 .driver = {
1666 .name = "si4713",
1667 },
1668 .probe = si4713_probe,
1669 .remove = si4713_remove,
1670 .id_table = si4713_id,
1671};
1672
Axel Linc6e8d862012-02-12 06:56:32 -03001673module_i2c_driver(si4713_i2c_driver);