28 #include <linux/i2c.h>
29 #include <linux/slab.h>
32 #include <linux/module.h>
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
74 #define to_si4713_device(sd) container_of(sd, struct si4713_device, sd)
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
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
94 #define rds_ps_nblocks(len) ((len / RDS_BLOCK) + (len % RDS_BLOCK ? 1 : 0))
96 #define get_status_bit(p, b, m) (((p) & (m)) >> (b))
97 #define set_bits(p, v, b, m) (((p) & ~(m)) | ((v) << (b)))
99 #define ATTACK_TIME_UNIT 500
101 #define POWER_OFF 0x00
102 #define POWER_ON 0x01
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))
110 #define set_mute(p) ((p & 1) | ((p & 1) << 1));
111 #define get_mute(p) (p & 0x01)
114 #define DBG_BUFFER(device, message, buffer, size) \
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); \
123 #define DBG_BUFFER(device, message, buffer, size)
131 static long limiter_times[] = {
159 static unsigned long acomp_rtimes[] = {
172 static unsigned long preemphasis_values[] = {
178 static int usecs_to_dev(
unsigned long usecs,
unsigned long const array[],
184 for (i = 0; i < size / 2; i++)
185 if (array[(i * 2) + 1] >= usecs) {
193 static unsigned long dev_to_usecs(
int value,
unsigned long const array[],
199 for (i = 0; i < size / 2; i++)
200 if (array[i * 2] == value) {
201 rval = array[(i * 2) + 1];
214 "%s: sending signal to completion work.\n", __func__);
231 const u8 args[],
const int argn,
232 u8 response[],
const int respn,
const int usecs)
243 memcpy(data1 + 1, args, argn);
247 if (err != argn + 1) {
248 v4l2_err(&sdev->
sd,
"Error while sending command 0x%02x\n",
250 return (err > 0) ? -
EIO :
err;
257 "(%s) Device took too much time to answer.\n",
264 "Error while reading response for command 0x%02x\n",
266 return (err > 0) ? -
EIO :
err;
307 "%s: property=0x%02x value=0x%02x status=0x%02x\n",
308 __func__, prop, *pv, val[0]);
347 "%s: property=0x%02x value=0x%02x status=0x%02x\n",
348 __func__, prop, val, resp[0]);
383 v4l2_err(&sdev->
sd,
"Failed to enable supplies: %d\n", err);
411 "Failed to disable supplies: %d\n", err);
444 "Failed to disable supplies: %d\n", err);
457 struct i2c_client *client = v4l2_get_subdevdata(&sdev->
sd);
475 v4l2_err(&sdev->
sd,
"Invalid product number\n");
490 static int si4713_wait_stc(
struct si4713_device *sdev,
const int usecs)
499 "%s: device took too much time to answer (%d usec).\n",
512 "%s: status bits: 0x%02x\n", __func__, resp[0]);
551 "%s: frequency=0x%02x status=0x%02x\n", __func__,
602 "%s: power=0x%02x antcap=0x%02x status=0x%02x\n",
603 __func__, power, antcap, val[0]);
620 static int si4713_tx_tune_measure(
struct si4713_device *sdev,
u16 frequency,
651 "%s: frequency=0x%02x antcap=0x%02x status=0x%02x\n",
652 __func__, frequency, antcap, val[0]);
672 u16 *frequency,
u8 *power,
673 u8 *antcap,
u8 *noise)
690 "%s: status=0x%02x\n", __func__, val[0]);
697 "(power %d, antcap %d, rnl %d)\n", __func__,
698 *frequency, *power, *antcap, *noise);
736 "%s: status=0x%02x\n", __func__, val[0]);
737 *cbleft = (
s8)val[2] - val[3];
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]);
754 unsigned char *pschar)
774 v4l2_dbg(1,
debug, &sdev->
sd,
"%s: status=0x%02x\n", __func__, val[0]);
779 static int si4713_set_power_state(
struct si4713_device *sdev,
u8 value)
786 rval = si4713_powerup(sdev);
788 rval = si4713_powerdown(sdev);
803 rval = si4713_write_property(sdev,
814 static int si4713_set_rds_ps_name(
struct si4713_device *sdev,
char *ps_name)
828 rval = si4713_tx_rds_ps(sdev, (i / (
RDS_BLOCK / 2)),
836 len =
strlen(ps_name) - 1;
840 rval = si4713_write_property(sdev,
846 rval = si4713_write_property(sdev,
860 static int si4713_set_rds_radio_text(
struct si4713_device *sdev,
char *rt)
864 u8 b_index = 0, cr_inserted = 0;
886 if (!rt[t_index + i] || rt[t_index + i] ==
917 static int si4713_choose_econtrol_action(
struct si4713_device *sdev,
u32 id,
919 unsigned long **
table,
int *size)
963 *shadow = &sdev->
rds_info.deviation;
993 *table = limiter_times;
999 *table = acomp_rtimes;
1005 *table = preemphasis_values;
1020 static int si4713_write_econtrol_string(
struct si4713_device *sdev,
1027 vqc.
id = control->
id;
1028 rval = si4713_queryctrl(&sdev->
sd, &vqc);
1032 switch (control->
id) {
1034 char ps_name[MAX_RDS_PS_NAME + 1];
1036 len = control->
size - 1;
1037 if (len < 0 || len > MAX_RDS_PS_NAME) {
1046 ps_name[len] =
'\0';
1048 if (
strlen(ps_name) % vqc.step) {
1053 rval = si4713_set_rds_ps_name(sdev, ps_name);
1060 len = control->
size - 1;
1070 radio_text[len] =
'\0';
1072 if (
strlen(radio_text) % vqc.step) {
1077 rval = si4713_set_rds_radio_text(sdev, radio_text);
1096 vqc.
id = control->
id;
1097 rval = si4713_queryctrl(sd, &vqc);
1101 if (control->
value < vqc.minimum || control->
value > vqc.maximum)
1109 static int si4713_write_econtrol_tune(
struct si4713_device *sdev,
1115 rval = validate_range(&sdev->
sd, control);
1121 switch (control->
id) {
1123 power = control->
value;
1128 antcap = control->
value;
1136 rval = si4713_tx_tune_power(sdev, power, antcap);
1149 static int si4713_write_econtrol_integers(
struct si4713_device *sdev,
1154 s32 bit = 0, mask = 0;
1157 unsigned long *table =
NULL;
1160 rval = validate_range(&sdev->
sd, control);
1164 rval = si4713_choose_econtrol_action(sdev, control->
id, &shadow, &bit,
1165 &mask, &property, &mul, &table, &size);
1169 val = control->
value;
1171 val = control->
value / mul;
1173 rval = usecs_to_dev(control->
value, table, size);
1184 rval = si4713_read_property(sdev, property, &val);
1190 rval = si4713_write_property(sdev, property, val);
1194 val = control->
value;
1198 *shadow = val * mul;
1200 rval = dev_to_usecs(val, table, size);
1235 memcpy(tmp, sdev,
sizeof(*sdev));
1240 rval |= si4713_write_econtrol_integers(sdev, &
ctrl);
1244 rval |= si4713_write_econtrol_integers(sdev, &
ctrl);
1248 rval |= si4713_write_econtrol_integers(sdev, &
ctrl);
1252 rval |= si4713_write_econtrol_integers(sdev, &
ctrl);
1256 rval |= si4713_write_econtrol_integers(sdev, &
ctrl);
1260 rval |= si4713_write_econtrol_integers(sdev, &
ctrl);
1264 rval |= si4713_write_econtrol_integers(sdev, &
ctrl);
1268 rval |= si4713_write_econtrol_integers(sdev, &
ctrl);
1272 rval |= si4713_write_econtrol_integers(sdev, &
ctrl);
1276 rval |= si4713_write_econtrol_integers(sdev, &
ctrl);
1280 rval |= si4713_write_econtrol_integers(sdev, &
ctrl);
1284 rval |= si4713_write_econtrol_integers(sdev, &
ctrl);
1288 rval |= si4713_write_econtrol_integers(sdev, &
ctrl);
1292 rval |= si4713_write_econtrol_integers(sdev, &
ctrl);
1296 rval |= si4713_write_econtrol_integers(sdev, &
ctrl);
1299 rval |= si4713_set_rds_ps_name(sdev, tmp->
rds_info.ps_name);
1302 rval |= si4713_set_rds_radio_text(sdev, tmp->
rds_info.radio_text);
1307 rval |= si4713_s_frequency(&sdev->
sd, &
f);
1311 rval |= si4713_write_econtrol_tune(sdev, &
ctrl);
1315 rval |= si4713_write_econtrol_tune(sdev, &
ctrl);
1324 si4713_s_modulator(&sdev->
sd, &
vm);
1339 rval = si4713_set_power_state(sdev,
POWER_ON);
1343 rval = si4713_checkrev(sdev);
1347 rval = si4713_set_power_state(sdev,
POWER_OFF);
1358 MAX_RDS_RADIO_TEXT);
1390 static int si4713_read_econtrol_string(
struct si4713_device *sdev,
1395 switch (control->
id) {
1398 control->
size = MAX_RDS_PS_NAME + 1;
1410 control->
size = MAX_RDS_RADIO_TEXT + 1;
1434 static int si4713_update_tune_status(
struct si4713_device *sdev)
1438 u8 p = 0,
a = 0,
n = 0;
1440 rval = si4713_tx_tune_status(sdev, 0x00, &f, &p, &
a, &
n);
1454 static int si4713_read_econtrol_tune(
struct si4713_device *sdev,
1462 rval = si4713_update_tune_status(sdev);
1467 switch (control->
id) {
1483 static int si4713_read_econtrol_integers(
struct si4713_device *sdev,
1488 s32 bit = 0, mask = 0;
1491 unsigned long *table =
NULL;
1494 rval = si4713_choose_econtrol_action(sdev, control->
id, &shadow, &bit,
1495 &mask, &property, &mul, &table, &size);
1502 rval = si4713_read_property(sdev, property, &val);
1512 *shadow = val * mul;
1514 *shadow = dev_to_usecs(val, table, size);
1529 static int si4713_s_ext_ctrls(
struct v4l2_subdev *sd,
1538 for (i = 0; i < ctrls->
count; i++) {
1541 switch ((ctrls->
controls + i)->id) {
1544 err = si4713_write_econtrol_string(sdev,
1549 err = si4713_write_econtrol_tune(sdev,
1553 err = si4713_write_econtrol_integers(sdev,
1567 static int si4713_g_ext_ctrls(
struct v4l2_subdev *sd,
1576 for (i = 0; i < ctrls->
count; i++) {
1579 switch ((ctrls->
controls + i)->id) {
1582 err = si4713_read_econtrol_string(sdev,
1587 err = si4713_read_econtrol_tune(sdev,
1591 err = si4713_read_econtrol_integers(sdev,
1748 rval = si4713_set_mute(sdev, ctrl->
value);
1752 rval = si4713_set_power_state(sdev,
POWER_DOWN);
1754 rval = si4713_set_power_state(sdev,
POWER_UP);
1758 rval = si4713_setup(sdev);
1762 rval = si4713_set_mute(sdev, ctrl->
value);
1789 rval = si4713_tx_tune_measure(sdev, frequency, 0);
1793 rval = si4713_update_tune_status(sdev);
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,
1830 if (vm->
index > 0) {
1902 rval = si4713_read_property(sdev,
1907 p =
set_bits(p, stereo, 1, 1 << 1);
1910 rval = si4713_write_property(sdev,
1938 rval = si4713_tx_tune_status(sdev, 0x00, &freq, &p, &a, &n);
1966 rval = si4713_tx_tune_freq(sdev, frequency);
1981 .g_frequency = si4713_g_frequency,
1982 .s_frequency = si4713_s_frequency,
1983 .g_modulator = si4713_g_modulator,
1984 .s_modulator = si4713_s_modulator,
1988 .core = &si4713_subdev_core_ops,
1989 .tuner = &si4713_subdev_tuner_ops,
1996 static int si4713_probe(
struct i2c_client *client,
2005 dev_err(&client->
dev,
"Failed to alloc video device.\n");
2011 if (pdata && gpio_is_valid(pdata->
gpio_reset)) {
2015 "Failed to request gpio: %d\n", rval);
2023 sdev->
supplies[i].supply = si4713_supply_names[i];
2028 dev_err(&client->
dev,
"Cannot get regulators: %d\n", rval);
2035 init_completion(&sdev->
work);
2040 client->
name, sdev);
2042 v4l2_err(&sdev->
sd,
"Could not request IRQ\n");
2047 v4l2_warn(&sdev->
sd,
"IRQ not configured. Using timeouts.\n");
2050 rval = si4713_initialize(sdev);
2052 v4l2_err(&sdev->
sd,
"Failed to probe device information.\n");
2073 static int si4713_remove(
struct i2c_client *client)
2075 struct v4l2_subdev *sd = i2c_get_clientdata(client);
2081 if (client->
irq > 0)
2100 static struct i2c_driver si4713_i2c_driver = {
2104 .probe = si4713_probe,
2105 .remove = si4713_remove,
2106 .id_table = si4713_id,