Linux Kernel  3.7.1
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
si4713-i2c.c
Go to the documentation of this file.
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 <[email protected]>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22  */
23 
24 #include <linux/mutex.h>
25 #include <linux/completion.h>
26 #include <linux/delay.h>
27 #include <linux/interrupt.h>
28 #include <linux/i2c.h>
29 #include <linux/slab.h>
30 #include <linux/gpio.h>
32 #include <linux/module.h>
33 #include <media/v4l2-device.h>
34 #include <media/v4l2-ioctl.h>
35 #include <media/v4l2-common.h>
36 
37 #include "si4713-i2c.h"
38 
39 /* module parameters */
40 static int debug;
42 MODULE_PARM_DESC(debug, "Debug level (0 - 2)");
43 
44 MODULE_LICENSE("GPL");
45 MODULE_AUTHOR("Eduardo Valentin <[email protected]>");
46 MODULE_DESCRIPTION("I2C driver for Si4713 FM Radio Transmitter");
47 MODULE_VERSION("0.0.1");
48 
49 static const char *si4713_supply_names[SI4713_NUM_SUPPLIES] = {
50  "vio",
51  "vdd",
52 };
53 
54 #define DEFAULT_RDS_PI 0x00
55 #define DEFAULT_RDS_PTY 0x00
56 #define DEFAULT_RDS_PS_NAME ""
57 #define DEFAULT_RDS_RADIO_TEXT DEFAULT_RDS_PS_NAME
58 #define DEFAULT_RDS_DEVIATION 0x00C8
59 #define DEFAULT_RDS_PS_REPEAT_COUNT 0x0003
60 #define DEFAULT_LIMITER_RTIME 0x1392
61 #define DEFAULT_LIMITER_DEV 0x102CA
62 #define DEFAULT_PILOT_FREQUENCY 0x4A38
63 #define DEFAULT_PILOT_DEVIATION 0x1A5E
64 #define DEFAULT_ACOMP_ATIME 0x0000
65 #define DEFAULT_ACOMP_RTIME 0xF4240L
66 #define DEFAULT_ACOMP_GAIN 0x0F
67 #define DEFAULT_ACOMP_THRESHOLD (-0x28)
68 #define DEFAULT_MUTE 0x01
69 #define DEFAULT_POWER_LEVEL 88
70 #define DEFAULT_FREQUENCY 8800
71 #define DEFAULT_PREEMPHASIS FMPE_EU
72 #define DEFAULT_TUNE_RNL 0xFF
73 
74 #define to_si4713_device(sd) container_of(sd, struct si4713_device, sd)
75 
76 /* frequency domain transformation (using times 10 to avoid floats) */
77 #define FREQDEV_UNIT 100000
78 #define FREQV4L2_MULTI 625
79 #define si4713_to_v4l2(f) ((f * FREQDEV_UNIT) / FREQV4L2_MULTI)
80 #define v4l2_to_si4713(f) ((f * FREQV4L2_MULTI) / FREQDEV_UNIT)
81 #define FREQ_RANGE_LOW 7600
82 #define FREQ_RANGE_HIGH 10800
83 
84 #define MAX_ARGS 7
85 
86 #define RDS_BLOCK 8
87 #define RDS_BLOCK_CLEAR 0x03
88 #define RDS_BLOCK_LOAD 0x04
89 #define RDS_RADIOTEXT_2A 0x20
90 #define RDS_RADIOTEXT_BLK_SIZE 4
91 #define RDS_RADIOTEXT_INDEX_MAX 0x0F
92 #define RDS_CARRIAGE_RETURN 0x0D
93 
94 #define rds_ps_nblocks(len) ((len / RDS_BLOCK) + (len % RDS_BLOCK ? 1 : 0))
95 
96 #define get_status_bit(p, b, m) (((p) & (m)) >> (b))
97 #define set_bits(p, v, b, m) (((p) & ~(m)) | ((v) << (b)))
98 
99 #define ATTACK_TIME_UNIT 500
100 
101 #define POWER_OFF 0x00
102 #define POWER_ON 0x01
103 
104 #define msb(x) ((u8)((u16) x >> 8))
105 #define lsb(x) ((u8)((u16) x & 0x00FF))
106 #define compose_u16(msb, lsb) (((u16)msb << 8) | lsb)
107 #define check_command_failed(status) (!(status & SI4713_CTS) || \
108  (status & SI4713_ERR))
109 /* mute definition */
110 #define set_mute(p) ((p & 1) | ((p & 1) << 1));
111 #define get_mute(p) (p & 0x01)
112 
113 #ifdef DEBUG
114 #define DBG_BUFFER(device, message, buffer, size) \
115  { \
116  int i; \
117  char str[(size)*5]; \
118  for (i = 0; i < size; i++) \
119  sprintf(str + i * 5, " 0x%02x", buffer[i]); \
120  v4l2_dbg(2, debug, device, "%s:%s\n", message, str); \
121  }
122 #else
123 #define DBG_BUFFER(device, message, buffer, size)
124 #endif
125 
126 /*
127  * Values for limiter release time (sorted by second column)
128  * device release
129  * value time (us)
130  */
131 static long limiter_times[] = {
132  2000, 250,
133  1000, 500,
134  510, 1000,
135  255, 2000,
136  170, 3000,
137  127, 4020,
138  102, 5010,
139  85, 6020,
140  73, 7010,
141  64, 7990,
142  57, 8970,
143  51, 10030,
144  25, 20470,
145  17, 30110,
146  13, 39380,
147  10, 51190,
148  8, 63690,
149  7, 73140,
150  6, 85330,
151  5, 102390,
152 };
153 
154 /*
155  * Values for audio compression release time (sorted by second column)
156  * device release
157  * value time (us)
158  */
159 static unsigned long acomp_rtimes[] = {
160  0, 100000,
161  1, 200000,
162  2, 350000,
163  3, 525000,
164  4, 1000000,
165 };
166 
167 /*
168  * Values for preemphasis (sorted by second column)
169  * device preemphasis
170  * value value (v4l2)
171  */
172 static unsigned long preemphasis_values[] = {
176 };
177 
178 static int usecs_to_dev(unsigned long usecs, unsigned long const array[],
179  int size)
180 {
181  int i;
182  int rval = -EINVAL;
183 
184  for (i = 0; i < size / 2; i++)
185  if (array[(i * 2) + 1] >= usecs) {
186  rval = array[i * 2];
187  break;
188  }
189 
190  return rval;
191 }
192 
193 static unsigned long dev_to_usecs(int value, unsigned long const array[],
194  int size)
195 {
196  int i;
197  int rval = -EINVAL;
198 
199  for (i = 0; i < size / 2; i++)
200  if (array[i * 2] == value) {
201  rval = array[(i * 2) + 1];
202  break;
203  }
204 
205  return rval;
206 }
207 
208 /* si4713_handler: IRQ handler, just complete work */
209 static irqreturn_t si4713_handler(int irq, void *dev)
210 {
211  struct si4713_device *sdev = dev;
212 
213  v4l2_dbg(2, debug, &sdev->sd,
214  "%s: sending signal to completion work.\n", __func__);
215  complete(&sdev->work);
216 
217  return IRQ_HANDLED;
218 }
219 
220 /*
221  * si4713_send_command - sends a command to si4713 and waits its response
222  * @sdev: si4713_device structure for the device we are communicating
223  * @command: command id
224  * @args: command arguments we are sending (up to 7)
225  * @argn: actual size of @args
226  * @response: buffer to place the expected response from the device (up to 15)
227  * @respn: actual size of @response
228  * @usecs: amount of time to wait before reading the response (in usecs)
229  */
230 static int si4713_send_command(struct si4713_device *sdev, const u8 command,
231  const u8 args[], const int argn,
232  u8 response[], const int respn, const int usecs)
233 {
234  struct i2c_client *client = v4l2_get_subdevdata(&sdev->sd);
235  u8 data1[MAX_ARGS + 1];
236  int err;
237 
238  if (!client->adapter)
239  return -ENODEV;
240 
241  /* First send the command and its arguments */
242  data1[0] = command;
243  memcpy(data1 + 1, args, argn);
244  DBG_BUFFER(&sdev->sd, "Parameters", data1, argn + 1);
245 
246  err = i2c_master_send(client, data1, argn + 1);
247  if (err != argn + 1) {
248  v4l2_err(&sdev->sd, "Error while sending command 0x%02x\n",
249  command);
250  return (err > 0) ? -EIO : err;
251  }
252 
253  /* Wait response from interrupt */
254  if (!wait_for_completion_timeout(&sdev->work,
255  usecs_to_jiffies(usecs) + 1))
256  v4l2_warn(&sdev->sd,
257  "(%s) Device took too much time to answer.\n",
258  __func__);
259 
260  /* Then get the response */
261  err = i2c_master_recv(client, response, respn);
262  if (err != respn) {
263  v4l2_err(&sdev->sd,
264  "Error while reading response for command 0x%02x\n",
265  command);
266  return (err > 0) ? -EIO : err;
267  }
268 
269  DBG_BUFFER(&sdev->sd, "Response", response, respn);
270  if (check_command_failed(response[0]))
271  return -EBUSY;
272 
273  return 0;
274 }
275 
276 /*
277  * si4713_read_property - reads a si4713 property
278  * @sdev: si4713_device structure for the device we are communicating
279  * @prop: property identification number
280  * @pv: property value to be returned on success
281  */
282 static int si4713_read_property(struct si4713_device *sdev, u16 prop, u32 *pv)
283 {
284  int err;
286  /*
287  * .First byte = 0
288  * .Second byte = property's MSB
289  * .Third byte = property's LSB
290  */
291  const u8 args[SI4713_GET_PROP_NARGS] = {
292  0x00,
293  msb(prop),
294  lsb(prop),
295  };
296 
297  err = si4713_send_command(sdev, SI4713_CMD_GET_PROPERTY,
298  args, ARRAY_SIZE(args), val,
300 
301  if (err < 0)
302  return err;
303 
304  *pv = compose_u16(val[2], val[3]);
305 
306  v4l2_dbg(1, debug, &sdev->sd,
307  "%s: property=0x%02x value=0x%02x status=0x%02x\n",
308  __func__, prop, *pv, val[0]);
309 
310  return err;
311 }
312 
313 /*
314  * si4713_write_property - modifies a si4713 property
315  * @sdev: si4713_device structure for the device we are communicating
316  * @prop: property identification number
317  * @val: new value for that property
318  */
319 static int si4713_write_property(struct si4713_device *sdev, u16 prop, u16 val)
320 {
321  int rval;
323  /*
324  * .First byte = 0
325  * .Second byte = property's MSB
326  * .Third byte = property's LSB
327  * .Fourth byte = value's MSB
328  * .Fifth byte = value's LSB
329  */
330  const u8 args[SI4713_SET_PROP_NARGS] = {
331  0x00,
332  msb(prop),
333  lsb(prop),
334  msb(val),
335  lsb(val),
336  };
337 
338  rval = si4713_send_command(sdev, SI4713_CMD_SET_PROPERTY,
339  args, ARRAY_SIZE(args),
340  resp, ARRAY_SIZE(resp),
342 
343  if (rval < 0)
344  return rval;
345 
346  v4l2_dbg(1, debug, &sdev->sd,
347  "%s: property=0x%02x value=0x%02x status=0x%02x\n",
348  __func__, prop, val, resp[0]);
349 
350  /*
351  * As there is no command response for SET_PROPERTY,
352  * wait Tcomp time to finish before proceed, in order
353  * to have property properly set.
354  */
356 
357  return rval;
358 }
359 
360 /*
361  * si4713_powerup - Powers the device up
362  * @sdev: si4713_device structure for the device we are communicating
363  */
364 static int si4713_powerup(struct si4713_device *sdev)
365 {
366  int err;
367  u8 resp[SI4713_PWUP_NRESP];
368  /*
369  * .First byte = Enabled interrupts and boot function
370  * .Second byte = Input operation mode
371  */
372  const u8 args[SI4713_PWUP_NARGS] = {
375  };
376 
377  if (sdev->power_state)
378  return 0;
379 
381  sdev->supplies);
382  if (err) {
383  v4l2_err(&sdev->sd, "Failed to enable supplies: %d\n", err);
384  return err;
385  }
386  if (gpio_is_valid(sdev->gpio_reset)) {
387  udelay(50);
388  gpio_set_value(sdev->gpio_reset, 1);
389  }
390 
391  err = si4713_send_command(sdev, SI4713_CMD_POWER_UP,
392  args, ARRAY_SIZE(args),
393  resp, ARRAY_SIZE(resp),
395 
396  if (!err) {
397  v4l2_dbg(1, debug, &sdev->sd, "Powerup response: 0x%02x\n",
398  resp[0]);
399  v4l2_dbg(1, debug, &sdev->sd, "Device in power up mode\n");
400  sdev->power_state = POWER_ON;
401 
402  err = si4713_write_property(sdev, SI4713_GPO_IEN,
404  } else {
405  if (gpio_is_valid(sdev->gpio_reset))
406  gpio_set_value(sdev->gpio_reset, 0);
408  sdev->supplies);
409  if (err)
410  v4l2_err(&sdev->sd,
411  "Failed to disable supplies: %d\n", err);
412  }
413 
414  return err;
415 }
416 
417 /*
418  * si4713_powerdown - Powers the device down
419  * @sdev: si4713_device structure for the device we are communicating
420  */
421 static int si4713_powerdown(struct si4713_device *sdev)
422 {
423  int err;
424  u8 resp[SI4713_PWDN_NRESP];
425 
426  if (!sdev->power_state)
427  return 0;
428 
429  err = si4713_send_command(sdev, SI4713_CMD_POWER_DOWN,
430  NULL, 0,
431  resp, ARRAY_SIZE(resp),
433 
434  if (!err) {
435  v4l2_dbg(1, debug, &sdev->sd, "Power down response: 0x%02x\n",
436  resp[0]);
437  v4l2_dbg(1, debug, &sdev->sd, "Device in reset mode\n");
438  if (gpio_is_valid(sdev->gpio_reset))
439  gpio_set_value(sdev->gpio_reset, 0);
441  sdev->supplies);
442  if (err)
443  v4l2_err(&sdev->sd,
444  "Failed to disable supplies: %d\n", err);
445  sdev->power_state = POWER_OFF;
446  }
447 
448  return err;
449 }
450 
451 /*
452  * si4713_checkrev - Checks if we are treating a device with the correct rev.
453  * @sdev: si4713_device structure for the device we are communicating
454  */
455 static int si4713_checkrev(struct si4713_device *sdev)
456 {
457  struct i2c_client *client = v4l2_get_subdevdata(&sdev->sd);
458  int rval;
459  u8 resp[SI4713_GETREV_NRESP];
460 
461  mutex_lock(&sdev->mutex);
462 
463  rval = si4713_send_command(sdev, SI4713_CMD_GET_REV,
464  NULL, 0,
465  resp, ARRAY_SIZE(resp),
467 
468  if (rval < 0)
469  goto unlock;
470 
471  if (resp[1] == SI4713_PRODUCT_NUMBER) {
472  v4l2_info(&sdev->sd, "chip found @ 0x%02x (%s)\n",
473  client->addr << 1, client->adapter->name);
474  } else {
475  v4l2_err(&sdev->sd, "Invalid product number\n");
476  rval = -EINVAL;
477  }
478 
479 unlock:
480  mutex_unlock(&sdev->mutex);
481  return rval;
482 }
483 
484 /*
485  * si4713_wait_stc - Waits STC interrupt and clears status bits. Useful
486  * for TX_TUNE_POWER, TX_TUNE_FREQ and TX_TUNE_MEAS
487  * @sdev: si4713_device structure for the device we are communicating
488  * @usecs: timeout to wait for STC interrupt signal
489  */
490 static int si4713_wait_stc(struct si4713_device *sdev, const int usecs)
491 {
492  int err;
494 
495  /* Wait response from STC interrupt */
496  if (!wait_for_completion_timeout(&sdev->work,
497  usecs_to_jiffies(usecs) + 1))
498  v4l2_warn(&sdev->sd,
499  "%s: device took too much time to answer (%d usec).\n",
500  __func__, usecs);
501 
502  /* Clear status bits */
503  err = si4713_send_command(sdev, SI4713_CMD_GET_INT_STATUS,
504  NULL, 0,
505  resp, ARRAY_SIZE(resp),
507 
508  if (err < 0)
509  goto exit;
510 
511  v4l2_dbg(1, debug, &sdev->sd,
512  "%s: status bits: 0x%02x\n", __func__, resp[0]);
513 
514  if (!(resp[0] & SI4713_STC_INT))
515  err = -EIO;
516 
517 exit:
518  return err;
519 }
520 
521 /*
522  * si4713_tx_tune_freq - Sets the state of the RF carrier and sets the tuning
523  * frequency between 76 and 108 MHz in 10 kHz units and
524  * steps of 50 kHz.
525  * @sdev: si4713_device structure for the device we are communicating
526  * @frequency: desired frequency (76 - 108 MHz, unit 10 KHz, step 50 kHz)
527  */
528 static int si4713_tx_tune_freq(struct si4713_device *sdev, u16 frequency)
529 {
530  int err;
531  u8 val[SI4713_TXFREQ_NRESP];
532  /*
533  * .First byte = 0
534  * .Second byte = frequency's MSB
535  * .Third byte = frequency's LSB
536  */
537  const u8 args[SI4713_TXFREQ_NARGS] = {
538  0x00,
539  msb(frequency),
540  lsb(frequency),
541  };
542 
543  err = si4713_send_command(sdev, SI4713_CMD_TX_TUNE_FREQ,
544  args, ARRAY_SIZE(args), val,
546 
547  if (err < 0)
548  return err;
549 
550  v4l2_dbg(1, debug, &sdev->sd,
551  "%s: frequency=0x%02x status=0x%02x\n", __func__,
552  frequency, val[0]);
553 
554  err = si4713_wait_stc(sdev, TIMEOUT_TX_TUNE);
555  if (err < 0)
556  return err;
557 
558  return compose_u16(args[1], args[2]);
559 }
560 
561 /*
562  * si4713_tx_tune_power - Sets the RF voltage level between 88 and 115 dBuV in
563  * 1 dB units. A value of 0x00 indicates off. The command
564  * also sets the antenna tuning capacitance. A value of 0
565  * indicates autotuning, and a value of 1 - 191 indicates
566  * a manual override, which results in a tuning
567  * capacitance of 0.25 pF x @antcap.
568  * @sdev: si4713_device structure for the device we are communicating
569  * @power: tuning power (88 - 115 dBuV, unit/step 1 dB)
570  * @antcap: value of antenna tuning capacitor (0 - 191)
571  */
572 static int si4713_tx_tune_power(struct si4713_device *sdev, u8 power,
573  u8 antcap)
574 {
575  int err;
576  u8 val[SI4713_TXPWR_NRESP];
577  /*
578  * .First byte = 0
579  * .Second byte = 0
580  * .Third byte = power
581  * .Fourth byte = antcap
582  */
583  const u8 args[SI4713_TXPWR_NARGS] = {
584  0x00,
585  0x00,
586  power,
587  antcap,
588  };
589 
590  if (((power > 0) && (power < SI4713_MIN_POWER)) ||
591  power > SI4713_MAX_POWER || antcap > SI4713_MAX_ANTCAP)
592  return -EDOM;
593 
594  err = si4713_send_command(sdev, SI4713_CMD_TX_TUNE_POWER,
595  args, ARRAY_SIZE(args), val,
597 
598  if (err < 0)
599  return err;
600 
601  v4l2_dbg(1, debug, &sdev->sd,
602  "%s: power=0x%02x antcap=0x%02x status=0x%02x\n",
603  __func__, power, antcap, val[0]);
604 
605  return si4713_wait_stc(sdev, TIMEOUT_TX_TUNE_POWER);
606 }
607 
608 /*
609  * si4713_tx_tune_measure - Enters receive mode and measures the received noise
610  * level in units of dBuV on the selected frequency.
611  * The Frequency must be between 76 and 108 MHz in 10 kHz
612  * units and steps of 50 kHz. The command also sets the
613  * antenna tuning capacitance. A value of 0 means
614  * autotuning, and a value of 1 to 191 indicates manual
615  * override.
616  * @sdev: si4713_device structure for the device we are communicating
617  * @frequency: desired frequency (76 - 108 MHz, unit 10 KHz, step 50 kHz)
618  * @antcap: value of antenna tuning capacitor (0 - 191)
619  */
620 static int si4713_tx_tune_measure(struct si4713_device *sdev, u16 frequency,
621  u8 antcap)
622 {
623  int err;
624  u8 val[SI4713_TXMEA_NRESP];
625  /*
626  * .First byte = 0
627  * .Second byte = frequency's MSB
628  * .Third byte = frequency's LSB
629  * .Fourth byte = antcap
630  */
631  const u8 args[SI4713_TXMEA_NARGS] = {
632  0x00,
633  msb(frequency),
634  lsb(frequency),
635  antcap,
636  };
637 
638  sdev->tune_rnl = DEFAULT_TUNE_RNL;
639 
640  if (antcap > SI4713_MAX_ANTCAP)
641  return -EDOM;
642 
643  err = si4713_send_command(sdev, SI4713_CMD_TX_TUNE_MEASURE,
644  args, ARRAY_SIZE(args), val,
646 
647  if (err < 0)
648  return err;
649 
650  v4l2_dbg(1, debug, &sdev->sd,
651  "%s: frequency=0x%02x antcap=0x%02x status=0x%02x\n",
652  __func__, frequency, antcap, val[0]);
653 
654  return si4713_wait_stc(sdev, TIMEOUT_TX_TUNE);
655 }
656 
657 /*
658  * si4713_tx_tune_status- Returns the status of the tx_tune_freq, tx_tune_mea or
659  * tx_tune_power commands. This command return the current
660  * frequency, output voltage in dBuV, the antenna tunning
661  * capacitance value and the received noise level. The
662  * command also clears the stcint interrupt bit when the
663  * first bit of its arguments is high.
664  * @sdev: si4713_device structure for the device we are communicating
665  * @intack: 0x01 to clear the seek/tune complete interrupt status indicator.
666  * @frequency: returned frequency
667  * @power: returned power
668  * @antcap: returned antenna capacitance
669  * @noise: returned noise level
670  */
671 static int si4713_tx_tune_status(struct si4713_device *sdev, u8 intack,
672  u16 *frequency, u8 *power,
673  u8 *antcap, u8 *noise)
674 {
675  int err;
677  /*
678  * .First byte = intack bit
679  */
680  const u8 args[SI4713_TXSTATUS_NARGS] = {
681  intack & SI4713_INTACK_MASK,
682  };
683 
684  err = si4713_send_command(sdev, SI4713_CMD_TX_TUNE_STATUS,
685  args, ARRAY_SIZE(args), val,
687 
688  if (!err) {
689  v4l2_dbg(1, debug, &sdev->sd,
690  "%s: status=0x%02x\n", __func__, val[0]);
691  *frequency = compose_u16(val[2], val[3]);
692  sdev->frequency = *frequency;
693  *power = val[5];
694  *antcap = val[6];
695  *noise = val[7];
696  v4l2_dbg(1, debug, &sdev->sd, "%s: response: %d x 10 kHz "
697  "(power %d, antcap %d, rnl %d)\n", __func__,
698  *frequency, *power, *antcap, *noise);
699  }
700 
701  return err;
702 }
703 
704 /*
705  * si4713_tx_rds_buff - Loads the RDS group buffer FIFO or circular buffer.
706  * @sdev: si4713_device structure for the device we are communicating
707  * @mode: the buffer operation mode.
708  * @rdsb: RDS Block B
709  * @rdsc: RDS Block C
710  * @rdsd: RDS Block D
711  * @cbleft: returns the number of available circular buffer blocks minus the
712  * number of used circular buffer blocks.
713  */
714 static int si4713_tx_rds_buff(struct si4713_device *sdev, u8 mode, u16 rdsb,
715  u16 rdsc, u16 rdsd, s8 *cbleft)
716 {
717  int err;
719 
720  const u8 args[SI4713_RDSBUFF_NARGS] = {
722  msb(rdsb),
723  lsb(rdsb),
724  msb(rdsc),
725  lsb(rdsc),
726  msb(rdsd),
727  lsb(rdsd),
728  };
729 
730  err = si4713_send_command(sdev, SI4713_CMD_TX_RDS_BUFF,
731  args, ARRAY_SIZE(args), val,
733 
734  if (!err) {
735  v4l2_dbg(1, debug, &sdev->sd,
736  "%s: status=0x%02x\n", __func__, val[0]);
737  *cbleft = (s8)val[2] - val[3];
738  v4l2_dbg(1, debug, &sdev->sd, "%s: response: interrupts"
739  " 0x%02x cb avail: %d cb used %d fifo avail"
740  " %d fifo used %d\n", __func__, val[1],
741  val[2], val[3], val[4], val[5]);
742  }
743 
744  return err;
745 }
746 
747 /*
748  * si4713_tx_rds_ps - Loads the program service buffer.
749  * @sdev: si4713_device structure for the device we are communicating
750  * @psid: program service id to be loaded.
751  * @pschar: assumed 4 size char array to be loaded into the program service
752  */
753 static int si4713_tx_rds_ps(struct si4713_device *sdev, u8 psid,
754  unsigned char *pschar)
755 {
756  int err;
757  u8 val[SI4713_RDSPS_NRESP];
758 
759  const u8 args[SI4713_RDSPS_NARGS] = {
760  psid & SI4713_RDSPS_PSID_MASK,
761  pschar[0],
762  pschar[1],
763  pschar[2],
764  pschar[3],
765  };
766 
767  err = si4713_send_command(sdev, SI4713_CMD_TX_RDS_PS,
768  args, ARRAY_SIZE(args), val,
770 
771  if (err < 0)
772  return err;
773 
774  v4l2_dbg(1, debug, &sdev->sd, "%s: status=0x%02x\n", __func__, val[0]);
775 
776  return err;
777 }
778 
779 static int si4713_set_power_state(struct si4713_device *sdev, u8 value)
780 {
781  int rval;
782 
783  mutex_lock(&sdev->mutex);
784 
785  if (value)
786  rval = si4713_powerup(sdev);
787  else
788  rval = si4713_powerdown(sdev);
789 
790  mutex_unlock(&sdev->mutex);
791  return rval;
792 }
793 
794 static int si4713_set_mute(struct si4713_device *sdev, u16 mute)
795 {
796  int rval = 0;
797 
798  mute = set_mute(mute);
799 
800  mutex_lock(&sdev->mutex);
801 
802  if (sdev->power_state)
803  rval = si4713_write_property(sdev,
805 
806  if (rval >= 0)
807  sdev->mute = get_mute(mute);
808 
809  mutex_unlock(&sdev->mutex);
810 
811  return rval;
812 }
813 
814 static int si4713_set_rds_ps_name(struct si4713_device *sdev, char *ps_name)
815 {
816  int rval = 0, i;
817  u8 len = 0;
818 
819  /* We want to clear the whole thing */
820  if (!strlen(ps_name))
821  memset(ps_name, 0, MAX_RDS_PS_NAME + 1);
822 
823  mutex_lock(&sdev->mutex);
824 
825  if (sdev->power_state) {
826  /* Write the new ps name and clear the padding */
827  for (i = 0; i < MAX_RDS_PS_NAME; i += (RDS_BLOCK / 2)) {
828  rval = si4713_tx_rds_ps(sdev, (i / (RDS_BLOCK / 2)),
829  ps_name + i);
830  if (rval < 0)
831  goto unlock;
832  }
833 
834  /* Setup the size to be sent */
835  if (strlen(ps_name))
836  len = strlen(ps_name) - 1;
837  else
838  len = 1;
839 
840  rval = si4713_write_property(sdev,
842  rds_ps_nblocks(len));
843  if (rval < 0)
844  goto unlock;
845 
846  rval = si4713_write_property(sdev,
849  if (rval < 0)
850  goto unlock;
851  }
852 
853  strncpy(sdev->rds_info.ps_name, ps_name, MAX_RDS_PS_NAME);
854 
855 unlock:
856  mutex_unlock(&sdev->mutex);
857  return rval;
858 }
859 
860 static int si4713_set_rds_radio_text(struct si4713_device *sdev, char *rt)
861 {
862  int rval = 0, i;
863  u16 t_index = 0;
864  u8 b_index = 0, cr_inserted = 0;
865  s8 left;
866 
867  mutex_lock(&sdev->mutex);
868 
869  if (!sdev->power_state)
870  goto copy;
871 
872  rval = si4713_tx_rds_buff(sdev, RDS_BLOCK_CLEAR, 0, 0, 0, &left);
873  if (rval < 0)
874  goto unlock;
875 
876  if (!strlen(rt))
877  goto copy;
878 
879  do {
880  /* RDS spec says that if the last block isn't used,
881  * then apply a carriage return
882  */
883  if (t_index < (RDS_RADIOTEXT_INDEX_MAX *
885  for (i = 0; i < RDS_RADIOTEXT_BLK_SIZE; i++) {
886  if (!rt[t_index + i] || rt[t_index + i] ==
888  rt[t_index + i] = RDS_CARRIAGE_RETURN;
889  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)
901  goto unlock;
902 
903  t_index += RDS_RADIOTEXT_BLK_SIZE;
904 
905  if (cr_inserted)
906  break;
907  } while (left > 0);
908 
909 copy:
910  strncpy(sdev->rds_info.radio_text, rt, MAX_RDS_RADIO_TEXT);
911 
912 unlock:
913  mutex_unlock(&sdev->mutex);
914  return rval;
915 }
916 
917 static int si4713_choose_econtrol_action(struct si4713_device *sdev, u32 id,
918  u32 **shadow, s32 *bit, s32 *mask, u16 *property, int *mul,
919  unsigned long **table, int *size)
920 {
921  s32 rval = 0;
922 
923  switch (id) {
924  /* FM_TX class controls */
925  case V4L2_CID_RDS_TX_PI:
926  *property = SI4713_TX_RDS_PI;
927  *mul = 1;
928  *shadow = &sdev->rds_info.pi;
929  break;
931  *property = SI4713_TX_ACOMP_THRESHOLD;
932  *mul = 1;
933  *shadow = &sdev->acomp_info.threshold;
934  break;
936  *property = SI4713_TX_ACOMP_GAIN;
937  *mul = 1;
938  *shadow = &sdev->acomp_info.gain;
939  break;
941  *property = SI4713_TX_PILOT_FREQUENCY;
942  *mul = 1;
943  *shadow = &sdev->pilot_info.frequency;
944  break;
946  *property = SI4713_TX_ACOMP_ATTACK_TIME;
947  *mul = ATTACK_TIME_UNIT;
948  *shadow = &sdev->acomp_info.attack_time;
949  break;
951  *property = SI4713_TX_PILOT_DEVIATION;
952  *mul = 10;
953  *shadow = &sdev->pilot_info.deviation;
954  break;
956  *property = SI4713_TX_AUDIO_DEVIATION;
957  *mul = 10;
958  *shadow = &sdev->limiter_info.deviation;
959  break;
961  *property = SI4713_TX_RDS_DEVIATION;
962  *mul = 1;
963  *shadow = &sdev->rds_info.deviation;
964  break;
965 
966  case V4L2_CID_RDS_TX_PTY:
967  *property = SI4713_TX_RDS_PS_MISC;
968  *bit = 5;
969  *mask = 0x1F << 5;
970  *shadow = &sdev->rds_info.pty;
971  break;
973  *property = SI4713_TX_ACOMP_ENABLE;
974  *bit = 1;
975  *mask = 1 << 1;
976  *shadow = &sdev->limiter_info.enabled;
977  break;
979  *property = SI4713_TX_ACOMP_ENABLE;
980  *bit = 0;
981  *mask = 1 << 0;
982  *shadow = &sdev->acomp_info.enabled;
983  break;
985  *property = SI4713_TX_COMPONENT_ENABLE;
986  *bit = 0;
987  *mask = 1 << 0;
988  *shadow = &sdev->pilot_info.enabled;
989  break;
990 
992  *property = SI4713_TX_LIMITER_RELEASE_TIME;
993  *table = limiter_times;
994  *size = ARRAY_SIZE(limiter_times);
995  *shadow = &sdev->limiter_info.release_time;
996  break;
998  *property = SI4713_TX_ACOMP_RELEASE_TIME;
999  *table = acomp_rtimes;
1000  *size = ARRAY_SIZE(acomp_rtimes);
1001  *shadow = &sdev->acomp_info.release_time;
1002  break;
1004  *property = SI4713_TX_PREEMPHASIS;
1005  *table = preemphasis_values;
1006  *size = ARRAY_SIZE(preemphasis_values);
1007  *shadow = &sdev->preemphasis;
1008  break;
1009 
1010  default:
1011  rval = -EINVAL;
1012  }
1013 
1014  return rval;
1015 }
1016 
1017 static int si4713_queryctrl(struct v4l2_subdev *sd, struct v4l2_queryctrl *qc);
1018 
1019 /* write string property */
1020 static int si4713_write_econtrol_string(struct si4713_device *sdev,
1021  struct v4l2_ext_control *control)
1022 {
1023  struct v4l2_queryctrl vqc;
1024  int len;
1025  s32 rval = 0;
1026 
1027  vqc.id = control->id;
1028  rval = si4713_queryctrl(&sdev->sd, &vqc);
1029  if (rval < 0)
1030  goto exit;
1031 
1032  switch (control->id) {
1033  case V4L2_CID_RDS_TX_PS_NAME: {
1034  char ps_name[MAX_RDS_PS_NAME + 1];
1035 
1036  len = control->size - 1;
1037  if (len < 0 || len > MAX_RDS_PS_NAME) {
1038  rval = -ERANGE;
1039  goto exit;
1040  }
1041  rval = copy_from_user(ps_name, control->string, len);
1042  if (rval) {
1043  rval = -EFAULT;
1044  goto exit;
1045  }
1046  ps_name[len] = '\0';
1047 
1048  if (strlen(ps_name) % vqc.step) {
1049  rval = -ERANGE;
1050  goto exit;
1051  }
1052 
1053  rval = si4713_set_rds_ps_name(sdev, ps_name);
1054  }
1055  break;
1056 
1058  char radio_text[MAX_RDS_RADIO_TEXT + 1];
1059 
1060  len = control->size - 1;
1061  if (len < 0 || len > MAX_RDS_RADIO_TEXT) {
1062  rval = -ERANGE;
1063  goto exit;
1064  }
1065  rval = copy_from_user(radio_text, control->string, len);
1066  if (rval) {
1067  rval = -EFAULT;
1068  goto exit;
1069  }
1070  radio_text[len] = '\0';
1071 
1072  if (strlen(radio_text) % vqc.step) {
1073  rval = -ERANGE;
1074  goto exit;
1075  }
1076 
1077  rval = si4713_set_rds_radio_text(sdev, radio_text);
1078  }
1079  break;
1080 
1081  default:
1082  rval = -EINVAL;
1083  break;
1084  }
1085 
1086 exit:
1087  return rval;
1088 }
1089 
1090 static int validate_range(struct v4l2_subdev *sd,
1091  struct v4l2_ext_control *control)
1092 {
1093  struct v4l2_queryctrl vqc;
1094  int rval;
1095 
1096  vqc.id = control->id;
1097  rval = si4713_queryctrl(sd, &vqc);
1098  if (rval < 0)
1099  goto exit;
1100 
1101  if (control->value < vqc.minimum || control->value > vqc.maximum)
1102  rval = -ERANGE;
1103 
1104 exit:
1105  return rval;
1106 }
1107 
1108 /* properties which use tx_tune_power*/
1109 static int si4713_write_econtrol_tune(struct si4713_device *sdev,
1110  struct v4l2_ext_control *control)
1111 {
1112  s32 rval = 0;
1113  u8 power, antcap;
1114 
1115  rval = validate_range(&sdev->sd, control);
1116  if (rval < 0)
1117  goto exit;
1118 
1119  mutex_lock(&sdev->mutex);
1120 
1121  switch (control->id) {
1123  power = control->value;
1124  antcap = sdev->antenna_capacitor;
1125  break;
1127  power = sdev->power_level;
1128  antcap = control->value;
1129  break;
1130  default:
1131  rval = -EINVAL;
1132  goto unlock;
1133  }
1134 
1135  if (sdev->power_state)
1136  rval = si4713_tx_tune_power(sdev, power, antcap);
1137 
1138  if (rval == 0) {
1139  sdev->power_level = power;
1140  sdev->antenna_capacitor = antcap;
1141  }
1142 
1143 unlock:
1144  mutex_unlock(&sdev->mutex);
1145 exit:
1146  return rval;
1147 }
1148 
1149 static int si4713_write_econtrol_integers(struct si4713_device *sdev,
1150  struct v4l2_ext_control *control)
1151 {
1152  s32 rval;
1153  u32 *shadow = NULL, val = 0;
1154  s32 bit = 0, mask = 0;
1155  u16 property = 0;
1156  int mul = 0;
1157  unsigned long *table = NULL;
1158  int size = 0;
1159 
1160  rval = validate_range(&sdev->sd, control);
1161  if (rval < 0)
1162  goto exit;
1163 
1164  rval = si4713_choose_econtrol_action(sdev, control->id, &shadow, &bit,
1165  &mask, &property, &mul, &table, &size);
1166  if (rval < 0)
1167  goto exit;
1168 
1169  val = control->value;
1170  if (mul) {
1171  val = control->value / mul;
1172  } else if (table) {
1173  rval = usecs_to_dev(control->value, table, size);
1174  if (rval < 0)
1175  goto exit;
1176  val = rval;
1177  rval = 0;
1178  }
1179 
1180  mutex_lock(&sdev->mutex);
1181 
1182  if (sdev->power_state) {
1183  if (mask) {
1184  rval = si4713_read_property(sdev, property, &val);
1185  if (rval < 0)
1186  goto unlock;
1187  val = set_bits(val, control->value, bit, mask);
1188  }
1189 
1190  rval = si4713_write_property(sdev, property, val);
1191  if (rval < 0)
1192  goto unlock;
1193  if (mask)
1194  val = control->value;
1195  }
1196 
1197  if (mul) {
1198  *shadow = val * mul;
1199  } else if (table) {
1200  rval = dev_to_usecs(val, table, size);
1201  if (rval < 0)
1202  goto unlock;
1203  *shadow = rval;
1204  rval = 0;
1205  } else {
1206  *shadow = val;
1207  }
1208 
1209 unlock:
1210  mutex_unlock(&sdev->mutex);
1211 exit:
1212  return rval;
1213 }
1214 
1215 static int si4713_s_frequency(struct v4l2_subdev *sd, struct v4l2_frequency *f);
1216 static int si4713_s_modulator(struct v4l2_subdev *sd, const struct v4l2_modulator *);
1217 /*
1218  * si4713_setup - Sets the device up with current configuration.
1219  * @sdev: si4713_device structure for the device we are communicating
1220  */
1221 static int si4713_setup(struct si4713_device *sdev)
1222 {
1223  struct v4l2_ext_control ctrl;
1224  struct v4l2_frequency f;
1225  struct v4l2_modulator vm;
1226  struct si4713_device *tmp;
1227  int rval = 0;
1228 
1229  tmp = kmalloc(sizeof(*tmp), GFP_KERNEL);
1230  if (!tmp)
1231  return -ENOMEM;
1232 
1233  /* Get a local copy to avoid race */
1234  mutex_lock(&sdev->mutex);
1235  memcpy(tmp, sdev, sizeof(*sdev));
1236  mutex_unlock(&sdev->mutex);
1237 
1238  ctrl.id = V4L2_CID_RDS_TX_PI;
1239  ctrl.value = tmp->rds_info.pi;
1240  rval |= si4713_write_econtrol_integers(sdev, &ctrl);
1241 
1243  ctrl.value = tmp->acomp_info.threshold;
1244  rval |= si4713_write_econtrol_integers(sdev, &ctrl);
1245 
1247  ctrl.value = tmp->acomp_info.gain;
1248  rval |= si4713_write_econtrol_integers(sdev, &ctrl);
1249 
1251  ctrl.value = tmp->pilot_info.frequency;
1252  rval |= si4713_write_econtrol_integers(sdev, &ctrl);
1253 
1255  ctrl.value = tmp->acomp_info.attack_time;
1256  rval |= si4713_write_econtrol_integers(sdev, &ctrl);
1257 
1259  ctrl.value = tmp->pilot_info.deviation;
1260  rval |= si4713_write_econtrol_integers(sdev, &ctrl);
1261 
1263  ctrl.value = tmp->limiter_info.deviation;
1264  rval |= si4713_write_econtrol_integers(sdev, &ctrl);
1265 
1267  ctrl.value = tmp->rds_info.deviation;
1268  rval |= si4713_write_econtrol_integers(sdev, &ctrl);
1269 
1271  ctrl.value = tmp->rds_info.pty;
1272  rval |= si4713_write_econtrol_integers(sdev, &ctrl);
1273 
1275  ctrl.value = tmp->limiter_info.enabled;
1276  rval |= si4713_write_econtrol_integers(sdev, &ctrl);
1277 
1279  ctrl.value = tmp->acomp_info.enabled;
1280  rval |= si4713_write_econtrol_integers(sdev, &ctrl);
1281 
1283  ctrl.value = tmp->pilot_info.enabled;
1284  rval |= si4713_write_econtrol_integers(sdev, &ctrl);
1285 
1287  ctrl.value = tmp->limiter_info.release_time;
1288  rval |= si4713_write_econtrol_integers(sdev, &ctrl);
1289 
1291  ctrl.value = tmp->acomp_info.release_time;
1292  rval |= si4713_write_econtrol_integers(sdev, &ctrl);
1293 
1295  ctrl.value = tmp->preemphasis;
1296  rval |= si4713_write_econtrol_integers(sdev, &ctrl);
1297 
1299  rval |= si4713_set_rds_ps_name(sdev, tmp->rds_info.ps_name);
1300 
1302  rval |= si4713_set_rds_radio_text(sdev, tmp->rds_info.radio_text);
1303 
1304  /* Device procedure needs to set frequency first */
1305  f.frequency = tmp->frequency ? tmp->frequency : DEFAULT_FREQUENCY;
1306  f.frequency = si4713_to_v4l2(f.frequency);
1307  rval |= si4713_s_frequency(&sdev->sd, &f);
1308 
1310  ctrl.value = tmp->power_level;
1311  rval |= si4713_write_econtrol_tune(sdev, &ctrl);
1312 
1314  ctrl.value = tmp->antenna_capacitor;
1315  rval |= si4713_write_econtrol_tune(sdev, &ctrl);
1316 
1317  vm.index = 0;
1318  if (tmp->stereo)
1319  vm.txsubchans = V4L2_TUNER_SUB_STEREO;
1320  else
1321  vm.txsubchans = V4L2_TUNER_SUB_MONO;
1322  if (tmp->rds_info.enabled)
1323  vm.txsubchans |= V4L2_TUNER_SUB_RDS;
1324  si4713_s_modulator(&sdev->sd, &vm);
1325 
1326  kfree(tmp);
1327 
1328  return rval;
1329 }
1330 
1331 /*
1332  * si4713_initialize - Sets the device up with default configuration.
1333  * @sdev: si4713_device structure for the device we are communicating
1334  */
1335 static int si4713_initialize(struct si4713_device *sdev)
1336 {
1337  int rval;
1338 
1339  rval = si4713_set_power_state(sdev, POWER_ON);
1340  if (rval < 0)
1341  goto exit;
1342 
1343  rval = si4713_checkrev(sdev);
1344  if (rval < 0)
1345  goto exit;
1346 
1347  rval = si4713_set_power_state(sdev, POWER_OFF);
1348  if (rval < 0)
1349  goto exit;
1350 
1351  mutex_lock(&sdev->mutex);
1352 
1353  sdev->rds_info.pi = DEFAULT_RDS_PI;
1354  sdev->rds_info.pty = DEFAULT_RDS_PTY;
1355  sdev->rds_info.deviation = DEFAULT_RDS_DEVIATION;
1356  strlcpy(sdev->rds_info.ps_name, DEFAULT_RDS_PS_NAME, MAX_RDS_PS_NAME);
1357  strlcpy(sdev->rds_info.radio_text, DEFAULT_RDS_RADIO_TEXT,
1358  MAX_RDS_RADIO_TEXT);
1359  sdev->rds_info.enabled = 1;
1360 
1361  sdev->limiter_info.release_time = DEFAULT_LIMITER_RTIME;
1362  sdev->limiter_info.deviation = DEFAULT_LIMITER_DEV;
1363  sdev->limiter_info.enabled = 1;
1364 
1365  sdev->pilot_info.deviation = DEFAULT_PILOT_DEVIATION;
1366  sdev->pilot_info.frequency = DEFAULT_PILOT_FREQUENCY;
1367  sdev->pilot_info.enabled = 1;
1368 
1369  sdev->acomp_info.release_time = DEFAULT_ACOMP_RTIME;
1370  sdev->acomp_info.attack_time = DEFAULT_ACOMP_ATIME;
1371  sdev->acomp_info.threshold = DEFAULT_ACOMP_THRESHOLD;
1372  sdev->acomp_info.gain = DEFAULT_ACOMP_GAIN;
1373  sdev->acomp_info.enabled = 1;
1374 
1375  sdev->frequency = DEFAULT_FREQUENCY;
1377  sdev->mute = DEFAULT_MUTE;
1379  sdev->antenna_capacitor = 0;
1380  sdev->stereo = 1;
1381  sdev->tune_rnl = DEFAULT_TUNE_RNL;
1382 
1383  mutex_unlock(&sdev->mutex);
1384 
1385 exit:
1386  return rval;
1387 }
1388 
1389 /* read string property */
1390 static int si4713_read_econtrol_string(struct si4713_device *sdev,
1391  struct v4l2_ext_control *control)
1392 {
1393  s32 rval = 0;
1394 
1395  switch (control->id) {
1397  if (strlen(sdev->rds_info.ps_name) + 1 > control->size) {
1398  control->size = MAX_RDS_PS_NAME + 1;
1399  rval = -ENOSPC;
1400  goto exit;
1401  }
1402  rval = copy_to_user(control->string, sdev->rds_info.ps_name,
1403  strlen(sdev->rds_info.ps_name) + 1);
1404  if (rval)
1405  rval = -EFAULT;
1406  break;
1407 
1409  if (strlen(sdev->rds_info.radio_text) + 1 > control->size) {
1410  control->size = MAX_RDS_RADIO_TEXT + 1;
1411  rval = -ENOSPC;
1412  goto exit;
1413  }
1414  rval = copy_to_user(control->string, sdev->rds_info.radio_text,
1415  strlen(sdev->rds_info.radio_text) + 1);
1416  if (rval)
1417  rval = -EFAULT;
1418  break;
1419 
1420  default:
1421  rval = -EINVAL;
1422  break;
1423  }
1424 
1425 exit:
1426  return rval;
1427 }
1428 
1429 /*
1430  * si4713_update_tune_status - update properties from tx_tune_status
1431  * command. Must be called with sdev->mutex held.
1432  * @sdev: si4713_device structure for the device we are communicating
1433  */
1434 static int si4713_update_tune_status(struct si4713_device *sdev)
1435 {
1436  int rval;
1437  u16 f = 0;
1438  u8 p = 0, a = 0, n = 0;
1439 
1440  rval = si4713_tx_tune_status(sdev, 0x00, &f, &p, &a, &n);
1441 
1442  if (rval < 0)
1443  goto exit;
1444 
1445  sdev->power_level = p;
1446  sdev->antenna_capacitor = a;
1447  sdev->tune_rnl = n;
1448 
1449 exit:
1450  return rval;
1451 }
1452 
1453 /* properties which use tx_tune_status */
1454 static int si4713_read_econtrol_tune(struct si4713_device *sdev,
1455  struct v4l2_ext_control *control)
1456 {
1457  s32 rval = 0;
1458 
1459  mutex_lock(&sdev->mutex);
1460 
1461  if (sdev->power_state) {
1462  rval = si4713_update_tune_status(sdev);
1463  if (rval < 0)
1464  goto unlock;
1465  }
1466 
1467  switch (control->id) {
1469  control->value = sdev->power_level;
1470  break;
1472  control->value = sdev->antenna_capacitor;
1473  break;
1474  default:
1475  rval = -EINVAL;
1476  }
1477 
1478 unlock:
1479  mutex_unlock(&sdev->mutex);
1480  return rval;
1481 }
1482 
1483 static int si4713_read_econtrol_integers(struct si4713_device *sdev,
1484  struct v4l2_ext_control *control)
1485 {
1486  s32 rval;
1487  u32 *shadow = NULL, val = 0;
1488  s32 bit = 0, mask = 0;
1489  u16 property = 0;
1490  int mul = 0;
1491  unsigned long *table = NULL;
1492  int size = 0;
1493 
1494  rval = si4713_choose_econtrol_action(sdev, control->id, &shadow, &bit,
1495  &mask, &property, &mul, &table, &size);
1496  if (rval < 0)
1497  goto exit;
1498 
1499  mutex_lock(&sdev->mutex);
1500 
1501  if (sdev->power_state) {
1502  rval = si4713_read_property(sdev, property, &val);
1503  if (rval < 0)
1504  goto unlock;
1505 
1506  /* Keep negative values for threshold */
1507  if (control->id == V4L2_CID_AUDIO_COMPRESSION_THRESHOLD)
1508  *shadow = (s16)val;
1509  else if (mask)
1510  *shadow = get_status_bit(val, bit, mask);
1511  else if (mul)
1512  *shadow = val * mul;
1513  else
1514  *shadow = dev_to_usecs(val, table, size);
1515  }
1516 
1517  control->value = *shadow;
1518 
1519 unlock:
1520  mutex_unlock(&sdev->mutex);
1521 exit:
1522  return rval;
1523 }
1524 
1525 /*
1526  * Video4Linux Subdev Interface
1527  */
1528 /* si4713_s_ext_ctrls - set extended controls value */
1529 static int si4713_s_ext_ctrls(struct v4l2_subdev *sd,
1530  struct v4l2_ext_controls *ctrls)
1531 {
1532  struct si4713_device *sdev = to_si4713_device(sd);
1533  int i;
1534 
1535  if (ctrls->ctrl_class != V4L2_CTRL_CLASS_FM_TX)
1536  return -EINVAL;
1537 
1538  for (i = 0; i < ctrls->count; i++) {
1539  int err;
1540 
1541  switch ((ctrls->controls + i)->id) {
1544  err = si4713_write_econtrol_string(sdev,
1545  ctrls->controls + i);
1546  break;
1549  err = si4713_write_econtrol_tune(sdev,
1550  ctrls->controls + i);
1551  break;
1552  default:
1553  err = si4713_write_econtrol_integers(sdev,
1554  ctrls->controls + i);
1555  }
1556 
1557  if (err < 0) {
1558  ctrls->error_idx = i;
1559  return err;
1560  }
1561  }
1562 
1563  return 0;
1564 }
1565 
1566 /* si4713_g_ext_ctrls - get extended controls value */
1567 static int si4713_g_ext_ctrls(struct v4l2_subdev *sd,
1568  struct v4l2_ext_controls *ctrls)
1569 {
1570  struct si4713_device *sdev = to_si4713_device(sd);
1571  int i;
1572 
1573  if (ctrls->ctrl_class != V4L2_CTRL_CLASS_FM_TX)
1574  return -EINVAL;
1575 
1576  for (i = 0; i < ctrls->count; i++) {
1577  int err;
1578 
1579  switch ((ctrls->controls + i)->id) {
1582  err = si4713_read_econtrol_string(sdev,
1583  ctrls->controls + i);
1584  break;
1587  err = si4713_read_econtrol_tune(sdev,
1588  ctrls->controls + i);
1589  break;
1590  default:
1591  err = si4713_read_econtrol_integers(sdev,
1592  ctrls->controls + i);
1593  }
1594 
1595  if (err < 0) {
1596  ctrls->error_idx = i;
1597  return err;
1598  }
1599  }
1600 
1601  return 0;
1602 }
1603 
1604 /* si4713_queryctrl - enumerate control items */
1605 static int si4713_queryctrl(struct v4l2_subdev *sd, struct v4l2_queryctrl *qc)
1606 {
1607  int rval = 0;
1608 
1609  switch (qc->id) {
1610  /* User class controls */
1611  case V4L2_CID_AUDIO_MUTE:
1612  rval = v4l2_ctrl_query_fill(qc, 0, 1, 1, DEFAULT_MUTE);
1613  break;
1614  /* FM_TX class controls */
1615  case V4L2_CID_RDS_TX_PI:
1616  rval = v4l2_ctrl_query_fill(qc, 0, 0xFFFF, 1, DEFAULT_RDS_PI);
1617  break;
1618  case V4L2_CID_RDS_TX_PTY:
1619  rval = v4l2_ctrl_query_fill(qc, 0, 31, 1, DEFAULT_RDS_PTY);
1620  break;
1623  10, DEFAULT_RDS_DEVIATION);
1624  break;
1626  /*
1627  * Report step as 8. From RDS spec, psname
1628  * should be 8. But there are receivers which scroll strings
1629  * sized as 8xN.
1630  */
1631  rval = v4l2_ctrl_query_fill(qc, 0, MAX_RDS_PS_NAME, 8, 0);
1632  break;
1634  /*
1635  * Report step as 32 (2A block). From RDS spec,
1636  * radio text should be 32 for 2A block. But there are receivers
1637  * which scroll strings sized as 32xN. Setting default to 32.
1638  */
1639  rval = v4l2_ctrl_query_fill(qc, 0, MAX_RDS_RADIO_TEXT, 32, 0);
1640  break;
1641 
1643  rval = v4l2_ctrl_query_fill(qc, 0, 1, 1, 1);
1644  break;
1647  50, DEFAULT_LIMITER_RTIME);
1648  break;
1651  10, DEFAULT_LIMITER_DEV);
1652  break;
1653 
1655  rval = v4l2_ctrl_query_fill(qc, 0, 1, 1, 1);
1656  break;
1658  rval = v4l2_ctrl_query_fill(qc, 0, MAX_ACOMP_GAIN, 1,
1660  break;
1665  break;
1668  500, DEFAULT_ACOMP_ATIME);
1669  break;
1671  rval = v4l2_ctrl_query_fill(qc, 100000, MAX_ACOMP_RELEASE_TIME,
1672  100000, DEFAULT_ACOMP_RTIME);
1673  break;
1674 
1676  rval = v4l2_ctrl_query_fill(qc, 0, 1, 1, 1);
1677  break;
1681  break;
1685  break;
1686 
1691  break;
1693  rval = v4l2_ctrl_query_fill(qc, 0, 120, 1, DEFAULT_POWER_LEVEL);
1694  break;
1696  rval = v4l2_ctrl_query_fill(qc, 0, 191, 1, 0);
1697  break;
1698  default:
1699  rval = -EINVAL;
1700  break;
1701  }
1702 
1703  return rval;
1704 }
1705 
1706 /* si4713_g_ctrl - get the value of a control */
1707 static int si4713_g_ctrl(struct v4l2_subdev *sd, struct v4l2_control *ctrl)
1708 {
1709  struct si4713_device *sdev = to_si4713_device(sd);
1710  int rval = 0;
1711 
1712  if (!sdev)
1713  return -ENODEV;
1714 
1715  mutex_lock(&sdev->mutex);
1716 
1717  if (sdev->power_state) {
1718  rval = si4713_read_property(sdev, SI4713_TX_LINE_INPUT_MUTE,
1719  &sdev->mute);
1720 
1721  if (rval < 0)
1722  goto unlock;
1723  }
1724 
1725  switch (ctrl->id) {
1726  case V4L2_CID_AUDIO_MUTE:
1727  ctrl->value = get_mute(sdev->mute);
1728  break;
1729  }
1730 
1731 unlock:
1732  mutex_unlock(&sdev->mutex);
1733  return rval;
1734 }
1735 
1736 /* si4713_s_ctrl - set the value of a control */
1737 static int si4713_s_ctrl(struct v4l2_subdev *sd, struct v4l2_control *ctrl)
1738 {
1739  struct si4713_device *sdev = to_si4713_device(sd);
1740  int rval = 0;
1741 
1742  if (!sdev)
1743  return -ENODEV;
1744 
1745  switch (ctrl->id) {
1746  case V4L2_CID_AUDIO_MUTE:
1747  if (ctrl->value) {
1748  rval = si4713_set_mute(sdev, ctrl->value);
1749  if (rval < 0)
1750  goto exit;
1751 
1752  rval = si4713_set_power_state(sdev, POWER_DOWN);
1753  } else {
1754  rval = si4713_set_power_state(sdev, POWER_UP);
1755  if (rval < 0)
1756  goto exit;
1757 
1758  rval = si4713_setup(sdev);
1759  if (rval < 0)
1760  goto exit;
1761 
1762  rval = si4713_set_mute(sdev, ctrl->value);
1763  }
1764  break;
1765  }
1766 
1767 exit:
1768  return rval;
1769 }
1770 
1771 /* si4713_ioctl - deal with private ioctls (only rnl for now) */
1772 long si4713_ioctl(struct v4l2_subdev *sd, unsigned int cmd, void *arg)
1773 {
1774  struct si4713_device *sdev = to_si4713_device(sd);
1775  struct si4713_rnl *rnl = arg;
1776  u16 frequency;
1777  int rval = 0;
1778 
1779  if (!arg)
1780  return -EINVAL;
1781 
1782  mutex_lock(&sdev->mutex);
1783  switch (cmd) {
1785  frequency = v4l2_to_si4713(rnl->frequency);
1786 
1787  if (sdev->power_state) {
1788  /* Set desired measurement frequency */
1789  rval = si4713_tx_tune_measure(sdev, frequency, 0);
1790  if (rval < 0)
1791  goto unlock;
1792  /* get results from tune status */
1793  rval = si4713_update_tune_status(sdev);
1794  if (rval < 0)
1795  goto unlock;
1796  }
1797  rnl->rnl = sdev->tune_rnl;
1798  break;
1799 
1800  default:
1801  /* nothing */
1802  rval = -ENOIOCTLCMD;
1803  }
1804 
1805 unlock:
1806  mutex_unlock(&sdev->mutex);
1807  return rval;
1808 }
1809 
1810 static const struct v4l2_subdev_core_ops si4713_subdev_core_ops = {
1811  .queryctrl = si4713_queryctrl,
1812  .g_ext_ctrls = si4713_g_ext_ctrls,
1813  .s_ext_ctrls = si4713_s_ext_ctrls,
1814  .g_ctrl = si4713_g_ctrl,
1815  .s_ctrl = si4713_s_ctrl,
1816  .ioctl = si4713_ioctl,
1817 };
1818 
1819 /* si4713_g_modulator - get modulator attributes */
1820 static int si4713_g_modulator(struct v4l2_subdev *sd, struct v4l2_modulator *vm)
1821 {
1822  struct si4713_device *sdev = to_si4713_device(sd);
1823  int rval = 0;
1824 
1825  if (!sdev) {
1826  rval = -ENODEV;
1827  goto exit;
1828  }
1829 
1830  if (vm->index > 0) {
1831  rval = -EINVAL;
1832  goto exit;
1833  }
1834 
1835  strncpy(vm->name, "FM Modulator", 32);
1838 
1839  /* Report current frequency range limits */
1842 
1843  mutex_lock(&sdev->mutex);
1844 
1845  if (sdev->power_state) {
1846  u32 comp_en = 0;
1847 
1848  rval = si4713_read_property(sdev, SI4713_TX_COMPONENT_ENABLE,
1849  &comp_en);
1850  if (rval < 0)
1851  goto unlock;
1852 
1853  sdev->stereo = get_status_bit(comp_en, 1, 1 << 1);
1854  sdev->rds_info.enabled = get_status_bit(comp_en, 2, 1 << 2);
1855  }
1856 
1857  /* Report current audio mode: mono or stereo */
1858  if (sdev->stereo)
1860  else
1862 
1863  /* Report rds feature status */
1864  if (sdev->rds_info.enabled)
1866  else
1868 
1869 unlock:
1870  mutex_unlock(&sdev->mutex);
1871 exit:
1872  return rval;
1873 }
1874 
1875 /* si4713_s_modulator - set modulator attributes */
1876 static int si4713_s_modulator(struct v4l2_subdev *sd, const struct v4l2_modulator *vm)
1877 {
1878  struct si4713_device *sdev = to_si4713_device(sd);
1879  int rval = 0;
1880  u16 stereo, rds;
1881  u32 p;
1882 
1883  if (!sdev)
1884  return -ENODEV;
1885 
1886  if (vm->index > 0)
1887  return -EINVAL;
1888 
1889  /* Set audio mode: mono or stereo */
1891  stereo = 1;
1892  else if (vm->txsubchans & V4L2_TUNER_SUB_MONO)
1893  stereo = 0;
1894  else
1895  return -EINVAL;
1896 
1897  rds = !!(vm->txsubchans & V4L2_TUNER_SUB_RDS);
1898 
1899  mutex_lock(&sdev->mutex);
1900 
1901  if (sdev->power_state) {
1902  rval = si4713_read_property(sdev,
1904  if (rval < 0)
1905  goto unlock;
1906 
1907  p = set_bits(p, stereo, 1, 1 << 1);
1908  p = set_bits(p, rds, 2, 1 << 2);
1909 
1910  rval = si4713_write_property(sdev,
1912  if (rval < 0)
1913  goto unlock;
1914  }
1915 
1916  sdev->stereo = stereo;
1917  sdev->rds_info.enabled = rds;
1918 
1919 unlock:
1920  mutex_unlock(&sdev->mutex);
1921  return rval;
1922 }
1923 
1924 /* si4713_g_frequency - get tuner or modulator radio frequency */
1925 static int si4713_g_frequency(struct v4l2_subdev *sd, struct v4l2_frequency *f)
1926 {
1927  struct si4713_device *sdev = to_si4713_device(sd);
1928  int rval = 0;
1929 
1930  f->type = V4L2_TUNER_RADIO;
1931 
1932  mutex_lock(&sdev->mutex);
1933 
1934  if (sdev->power_state) {
1935  u16 freq;
1936  u8 p, a, n;
1937 
1938  rval = si4713_tx_tune_status(sdev, 0x00, &freq, &p, &a, &n);
1939  if (rval < 0)
1940  goto unlock;
1941 
1942  sdev->frequency = freq;
1943  }
1944 
1945  f->frequency = si4713_to_v4l2(sdev->frequency);
1946 
1947 unlock:
1948  mutex_unlock(&sdev->mutex);
1949  return rval;
1950 }
1951 
1952 /* si4713_s_frequency - set tuner or modulator radio frequency */
1953 static int si4713_s_frequency(struct v4l2_subdev *sd, struct v4l2_frequency *f)
1954 {
1955  struct si4713_device *sdev = to_si4713_device(sd);
1956  int rval = 0;
1957  u16 frequency = v4l2_to_si4713(f->frequency);
1958 
1959  /* Check frequency range */
1960  if (frequency < FREQ_RANGE_LOW || frequency > FREQ_RANGE_HIGH)
1961  return -EDOM;
1962 
1963  mutex_lock(&sdev->mutex);
1964 
1965  if (sdev->power_state) {
1966  rval = si4713_tx_tune_freq(sdev, frequency);
1967  if (rval < 0)
1968  goto unlock;
1969  frequency = rval;
1970  rval = 0;
1971  }
1972  sdev->frequency = frequency;
1973  f->frequency = si4713_to_v4l2(frequency);
1974 
1975 unlock:
1976  mutex_unlock(&sdev->mutex);
1977  return rval;
1978 }
1979 
1980 static const struct v4l2_subdev_tuner_ops si4713_subdev_tuner_ops = {
1981  .g_frequency = si4713_g_frequency,
1982  .s_frequency = si4713_s_frequency,
1983  .g_modulator = si4713_g_modulator,
1984  .s_modulator = si4713_s_modulator,
1985 };
1986 
1987 static const struct v4l2_subdev_ops si4713_subdev_ops = {
1988  .core = &si4713_subdev_core_ops,
1989  .tuner = &si4713_subdev_tuner_ops,
1990 };
1991 
1992 /*
1993  * I2C driver interface
1994  */
1995 /* si4713_probe - probe for the device */
1996 static int si4713_probe(struct i2c_client *client,
1997  const struct i2c_device_id *id)
1998 {
1999  struct si4713_device *sdev;
2000  struct si4713_platform_data *pdata = client->dev.platform_data;
2001  int rval, i;
2002 
2003  sdev = kzalloc(sizeof *sdev, GFP_KERNEL);
2004  if (!sdev) {
2005  dev_err(&client->dev, "Failed to alloc video device.\n");
2006  rval = -ENOMEM;
2007  goto exit;
2008  }
2009 
2010  sdev->gpio_reset = -1;
2011  if (pdata && gpio_is_valid(pdata->gpio_reset)) {
2012  rval = gpio_request(pdata->gpio_reset, "si4713 reset");
2013  if (rval) {
2014  dev_err(&client->dev,
2015  "Failed to request gpio: %d\n", rval);
2016  goto free_sdev;
2017  }
2018  sdev->gpio_reset = pdata->gpio_reset;
2020  }
2021 
2022  for (i = 0; i < ARRAY_SIZE(sdev->supplies); i++)
2023  sdev->supplies[i].supply = si4713_supply_names[i];
2024 
2025  rval = regulator_bulk_get(&client->dev, ARRAY_SIZE(sdev->supplies),
2026  sdev->supplies);
2027  if (rval) {
2028  dev_err(&client->dev, "Cannot get regulators: %d\n", rval);
2029  goto free_gpio;
2030  }
2031 
2032  v4l2_i2c_subdev_init(&sdev->sd, client, &si4713_subdev_ops);
2033 
2034  mutex_init(&sdev->mutex);
2035  init_completion(&sdev->work);
2036 
2037  if (client->irq) {
2038  rval = request_irq(client->irq,
2039  si4713_handler, IRQF_TRIGGER_FALLING | IRQF_DISABLED,
2040  client->name, sdev);
2041  if (rval < 0) {
2042  v4l2_err(&sdev->sd, "Could not request IRQ\n");
2043  goto put_reg;
2044  }
2045  v4l2_dbg(1, debug, &sdev->sd, "IRQ requested.\n");
2046  } else {
2047  v4l2_warn(&sdev->sd, "IRQ not configured. Using timeouts.\n");
2048  }
2049 
2050  rval = si4713_initialize(sdev);
2051  if (rval < 0) {
2052  v4l2_err(&sdev->sd, "Failed to probe device information.\n");
2053  goto free_irq;
2054  }
2055 
2056  return 0;
2057 
2058 free_irq:
2059  if (client->irq)
2060  free_irq(client->irq, sdev);
2061 put_reg:
2063 free_gpio:
2064  if (gpio_is_valid(sdev->gpio_reset))
2065  gpio_free(sdev->gpio_reset);
2066 free_sdev:
2067  kfree(sdev);
2068 exit:
2069  return rval;
2070 }
2071 
2072 /* si4713_remove - remove the device */
2073 static int si4713_remove(struct i2c_client *client)
2074 {
2075  struct v4l2_subdev *sd = i2c_get_clientdata(client);
2076  struct si4713_device *sdev = to_si4713_device(sd);
2077 
2078  if (sdev->power_state)
2079  si4713_set_power_state(sdev, POWER_DOWN);
2080 
2081  if (client->irq > 0)
2082  free_irq(client->irq, sdev);
2083 
2086  if (gpio_is_valid(sdev->gpio_reset))
2087  gpio_free(sdev->gpio_reset);
2088  kfree(sdev);
2089 
2090  return 0;
2091 }
2092 
2093 /* si4713_i2c_driver - i2c driver interface */
2094 static const struct i2c_device_id si4713_id[] = {
2095  { "si4713" , 0 },
2096  { },
2097 };
2098 MODULE_DEVICE_TABLE(i2c, si4713_id);
2099 
2100 static struct i2c_driver si4713_i2c_driver = {
2101  .driver = {
2102  .name = "si4713",
2103  },
2104  .probe = si4713_probe,
2105  .remove = si4713_remove,
2106  .id_table = si4713_id,
2107 };
2108 
2109 module_i2c_driver(si4713_i2c_driver);