aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/media/radio/si4713
diff options
context:
space:
mode:
Diffstat (limited to 'drivers/media/radio/si4713')
-rw-r--r--drivers/media/radio/si4713/Kconfig40
-rw-r--r--drivers/media/radio/si4713/Makefile7
-rw-r--r--drivers/media/radio/si4713/radio-platform-si4713.c246
-rw-r--r--drivers/media/radio/si4713/radio-usb-si4713.c540
-rw-r--r--drivers/media/radio/si4713/si4713.c1557
-rw-r--r--drivers/media/radio/si4713/si4713.h240
6 files changed, 2630 insertions, 0 deletions
diff --git a/drivers/media/radio/si4713/Kconfig b/drivers/media/radio/si4713/Kconfig
new file mode 100644
index 000000000000..a7c3ba85d12b
--- /dev/null
+++ b/drivers/media/radio/si4713/Kconfig
@@ -0,0 +1,40 @@
1config USB_SI4713
2 tristate "Silicon Labs Si4713 FM Radio Transmitter support with USB"
3 depends on USB && RADIO_SI4713
4 select SI4713
5 ---help---
6 This is a driver for USB devices with the Silicon Labs SI4713
7 chip. Currently these devices are known to work.
8 - 10c4:8244: Silicon Labs FM Transmitter USB device.
9
10 Say Y here if you want to connect this type of radio to your
11 computer's USB port.
12
13 To compile this driver as a module, choose M here: the
14 module will be called radio-usb-si4713.
15
16config PLATFORM_SI4713
17 tristate "Silicon Labs Si4713 FM Radio Transmitter support with I2C"
18 depends on I2C && RADIO_SI4713
19 select SI4713
20 ---help---
21 This is a driver for I2C devices with the Silicon Labs SI4713
22 chip.
23
24 Say Y here if you want to connect this type of radio to your
25 computer's I2C port.
26
27 To compile this driver as a module, choose M here: the
28 module will be called radio-platform-si4713.
29
30config I2C_SI4713
31 tristate "Silicon Labs Si4713 FM Radio Transmitter support"
32 depends on I2C && RADIO_SI4713
33 ---help---
34 Say Y here if you want support to Si4713 FM Radio Transmitter.
35 This device can transmit audio through FM. It can transmit
36 RDS and RBDS signals as well. This module is the v4l2 radio
37 interface for the i2c driver of this device.
38
39 To compile this driver as a module, choose M here: the
40 module will be called si4713.
diff --git a/drivers/media/radio/si4713/Makefile b/drivers/media/radio/si4713/Makefile
new file mode 100644
index 000000000000..ddaaf925e883
--- /dev/null
+++ b/drivers/media/radio/si4713/Makefile
@@ -0,0 +1,7 @@
1#
2# Makefile for radios with Silicon Labs Si4713 FM Radio Transmitters
3#
4
5obj-$(CONFIG_I2C_SI4713) += si4713.o
6obj-$(CONFIG_USB_SI4713) += radio-usb-si4713.o
7obj-$(CONFIG_PLATFORM_SI4713) += radio-platform-si4713.o
diff --git a/drivers/media/radio/si4713/radio-platform-si4713.c b/drivers/media/radio/si4713/radio-platform-si4713.c
new file mode 100644
index 000000000000..ba4cfc946868
--- /dev/null
+++ b/drivers/media/radio/si4713/radio-platform-si4713.c
@@ -0,0 +1,246 @@
1/*
2 * drivers/media/radio/radio-si4713.c
3 *
4 * Platform Driver for Silicon Labs Si4713 FM Radio Transmitter:
5 *
6 * Copyright (c) 2008 Instituto Nokia de Tecnologia - INdT
7 * Contact: Eduardo Valentin <eduardo.valentin@nokia.com>
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 */
23
24#include <linux/kernel.h>
25#include <linux/module.h>
26#include <linux/init.h>
27#include <linux/platform_device.h>
28#include <linux/i2c.h>
29#include <linux/videodev2.h>
30#include <linux/slab.h>
31#include <media/v4l2-device.h>
32#include <media/v4l2-common.h>
33#include <media/v4l2-ioctl.h>
34#include <media/v4l2-fh.h>
35#include <media/v4l2-ctrls.h>
36#include <media/v4l2-event.h>
37#include <media/radio-si4713.h>
38
39/* module parameters */
40static int radio_nr = -1; /* radio device minor (-1 ==> auto assign) */
41module_param(radio_nr, int, 0);
42MODULE_PARM_DESC(radio_nr,
43 "Minor number for radio device (-1 ==> auto assign)");
44
45MODULE_LICENSE("GPL v2");
46MODULE_AUTHOR("Eduardo Valentin <eduardo.valentin@nokia.com>");
47MODULE_DESCRIPTION("Platform driver for Si4713 FM Radio Transmitter");
48MODULE_VERSION("0.0.1");
49MODULE_ALIAS("platform:radio-si4713");
50
51/* Driver state struct */
52struct radio_si4713_device {
53 struct v4l2_device v4l2_dev;
54 struct video_device radio_dev;
55 struct mutex lock;
56};
57
58/* radio_si4713_fops - file operations interface */
59static const struct v4l2_file_operations radio_si4713_fops = {
60 .owner = THIS_MODULE,
61 .open = v4l2_fh_open,
62 .release = v4l2_fh_release,
63 .poll = v4l2_ctrl_poll,
64 /* Note: locking is done at the subdev level in the i2c driver. */
65 .unlocked_ioctl = video_ioctl2,
66};
67
68/* Video4Linux Interface */
69
70/* radio_si4713_querycap - query device capabilities */
71static int radio_si4713_querycap(struct file *file, void *priv,
72 struct v4l2_capability *capability)
73{
74 strlcpy(capability->driver, "radio-si4713", sizeof(capability->driver));
75 strlcpy(capability->card, "Silicon Labs Si4713 Modulator",
76 sizeof(capability->card));
77 strlcpy(capability->bus_info, "platform:radio-si4713",
78 sizeof(capability->bus_info));
79 capability->device_caps = V4L2_CAP_MODULATOR | V4L2_CAP_RDS_OUTPUT;
80 capability->capabilities = capability->device_caps | V4L2_CAP_DEVICE_CAPS;
81
82 return 0;
83}
84
85/*
86 * v4l2 ioctl call backs.
87 * we are just a wrapper for v4l2_sub_devs.
88 */
89static inline struct v4l2_device *get_v4l2_dev(struct file *file)
90{
91 return &((struct radio_si4713_device *)video_drvdata(file))->v4l2_dev;
92}
93
94static int radio_si4713_g_modulator(struct file *file, void *p,
95 struct v4l2_modulator *vm)
96{
97 return v4l2_device_call_until_err(get_v4l2_dev(file), 0, tuner,
98 g_modulator, vm);
99}
100
101static int radio_si4713_s_modulator(struct file *file, void *p,
102 const struct v4l2_modulator *vm)
103{
104 return v4l2_device_call_until_err(get_v4l2_dev(file), 0, tuner,
105 s_modulator, vm);
106}
107
108static int radio_si4713_g_frequency(struct file *file, void *p,
109 struct v4l2_frequency *vf)
110{
111 return v4l2_device_call_until_err(get_v4l2_dev(file), 0, tuner,
112 g_frequency, vf);
113}
114
115static int radio_si4713_s_frequency(struct file *file, void *p,
116 const struct v4l2_frequency *vf)
117{
118 return v4l2_device_call_until_err(get_v4l2_dev(file), 0, tuner,
119 s_frequency, vf);
120}
121
122static long radio_si4713_default(struct file *file, void *p,
123 bool valid_prio, unsigned int cmd, void *arg)
124{
125 return v4l2_device_call_until_err(get_v4l2_dev(file), 0, core,
126 ioctl, cmd, arg);
127}
128
129static struct v4l2_ioctl_ops radio_si4713_ioctl_ops = {
130 .vidioc_querycap = radio_si4713_querycap,
131 .vidioc_g_modulator = radio_si4713_g_modulator,
132 .vidioc_s_modulator = radio_si4713_s_modulator,
133 .vidioc_g_frequency = radio_si4713_g_frequency,
134 .vidioc_s_frequency = radio_si4713_s_frequency,
135 .vidioc_log_status = v4l2_ctrl_log_status,
136 .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
137 .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
138 .vidioc_default = radio_si4713_default,
139};
140
141/* radio_si4713_vdev_template - video device interface */
142static struct video_device radio_si4713_vdev_template = {
143 .fops = &radio_si4713_fops,
144 .name = "radio-si4713",
145 .release = video_device_release_empty,
146 .ioctl_ops = &radio_si4713_ioctl_ops,
147 .vfl_dir = VFL_DIR_TX,
148};
149
150/* Platform driver interface */
151/* radio_si4713_pdriver_probe - probe for the device */
152static int radio_si4713_pdriver_probe(struct platform_device *pdev)
153{
154 struct radio_si4713_platform_data *pdata = pdev->dev.platform_data;
155 struct radio_si4713_device *rsdev;
156 struct i2c_adapter *adapter;
157 struct v4l2_subdev *sd;
158 int rval = 0;
159
160 if (!pdata) {
161 dev_err(&pdev->dev, "Cannot proceed without platform data.\n");
162 rval = -EINVAL;
163 goto exit;
164 }
165
166 rsdev = devm_kzalloc(&pdev->dev, sizeof(*rsdev), GFP_KERNEL);
167 if (!rsdev) {
168 dev_err(&pdev->dev, "Failed to alloc video device.\n");
169 rval = -ENOMEM;
170 goto exit;
171 }
172 mutex_init(&rsdev->lock);
173
174 rval = v4l2_device_register(&pdev->dev, &rsdev->v4l2_dev);
175 if (rval) {
176 dev_err(&pdev->dev, "Failed to register v4l2 device.\n");
177 goto exit;
178 }
179
180 adapter = i2c_get_adapter(pdata->i2c_bus);
181 if (!adapter) {
182 dev_err(&pdev->dev, "Cannot get i2c adapter %d\n",
183 pdata->i2c_bus);
184 rval = -ENODEV;
185 goto unregister_v4l2_dev;
186 }
187
188 sd = v4l2_i2c_new_subdev_board(&rsdev->v4l2_dev, adapter,
189 pdata->subdev_board_info, NULL);
190 if (!sd) {
191 dev_err(&pdev->dev, "Cannot get v4l2 subdevice\n");
192 rval = -ENODEV;
193 goto put_adapter;
194 }
195
196 rsdev->radio_dev = radio_si4713_vdev_template;
197 rsdev->radio_dev.v4l2_dev = &rsdev->v4l2_dev;
198 rsdev->radio_dev.ctrl_handler = sd->ctrl_handler;
199 set_bit(V4L2_FL_USE_FH_PRIO, &rsdev->radio_dev.flags);
200 /* Serialize all access to the si4713 */
201 rsdev->radio_dev.lock = &rsdev->lock;
202 video_set_drvdata(&rsdev->radio_dev, rsdev);
203 if (video_register_device(&rsdev->radio_dev, VFL_TYPE_RADIO, radio_nr)) {
204 dev_err(&pdev->dev, "Could not register video device.\n");
205 rval = -EIO;
206 goto put_adapter;
207 }
208 dev_info(&pdev->dev, "New device successfully probed\n");
209
210 goto exit;
211
212put_adapter:
213 i2c_put_adapter(adapter);
214unregister_v4l2_dev:
215 v4l2_device_unregister(&rsdev->v4l2_dev);
216exit:
217 return rval;
218}
219
220/* radio_si4713_pdriver_remove - remove the device */
221static int radio_si4713_pdriver_remove(struct platform_device *pdev)
222{
223 struct v4l2_device *v4l2_dev = platform_get_drvdata(pdev);
224 struct v4l2_subdev *sd = list_entry(v4l2_dev->subdevs.next,
225 struct v4l2_subdev, list);
226 struct i2c_client *client = v4l2_get_subdevdata(sd);
227 struct radio_si4713_device *rsdev;
228
229 rsdev = container_of(v4l2_dev, struct radio_si4713_device, v4l2_dev);
230 video_unregister_device(&rsdev->radio_dev);
231 i2c_put_adapter(client->adapter);
232 v4l2_device_unregister(&rsdev->v4l2_dev);
233
234 return 0;
235}
236
237static struct platform_driver radio_si4713_pdriver = {
238 .driver = {
239 .name = "radio-si4713",
240 .owner = THIS_MODULE,
241 },
242 .probe = radio_si4713_pdriver_probe,
243 .remove = radio_si4713_pdriver_remove,
244};
245
246module_platform_driver(radio_si4713_pdriver);
diff --git a/drivers/media/radio/si4713/radio-usb-si4713.c b/drivers/media/radio/si4713/radio-usb-si4713.c
new file mode 100644
index 000000000000..779855b74bcd
--- /dev/null
+++ b/drivers/media/radio/si4713/radio-usb-si4713.c
@@ -0,0 +1,540 @@
1/*
2 * Copyright 2013 Cisco Systems, Inc. and/or its affiliates.
3 * All rights reserved.
4 *
5 * This program is free software; you may redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; version 2 of the License.
8 *
9 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
10 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
11 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
12 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
13 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
14 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
15 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
16 * SOFTWARE.
17 */
18
19/* kernel includes */
20#include <linux/kernel.h>
21#include <linux/module.h>
22#include <linux/usb.h>
23#include <linux/init.h>
24#include <linux/slab.h>
25#include <linux/input.h>
26#include <linux/mutex.h>
27#include <linux/i2c.h>
28/* V4l includes */
29#include <linux/videodev2.h>
30#include <media/v4l2-common.h>
31#include <media/v4l2-device.h>
32#include <media/v4l2-ioctl.h>
33#include <media/v4l2-event.h>
34#include <media/si4713.h>
35
36#include "si4713.h"
37
38/* driver and module definitions */
39MODULE_AUTHOR("Dinesh Ram <dinesh.ram@cern.ch>");
40MODULE_DESCRIPTION("Si4713 FM Transmitter USB driver");
41MODULE_LICENSE("GPL v2");
42
43/* The Device announces itself as Cygnal Integrated Products, Inc. */
44#define USB_SI4713_VENDOR 0x10c4
45#define USB_SI4713_PRODUCT 0x8244
46
47#define BUFFER_LENGTH 64
48#define USB_TIMEOUT 1000
49#define USB_RESP_TIMEOUT 50000
50
51/* USB Device ID List */
52static struct usb_device_id usb_si4713_usb_device_table[] = {
53 {USB_DEVICE_AND_INTERFACE_INFO(USB_SI4713_VENDOR, USB_SI4713_PRODUCT,
54 USB_CLASS_HID, 0, 0) },
55 { } /* Terminating entry */
56};
57
58MODULE_DEVICE_TABLE(usb, usb_si4713_usb_device_table);
59
60struct si4713_usb_device {
61 struct usb_device *usbdev;
62 struct usb_interface *intf;
63 struct video_device vdev;
64 struct v4l2_device v4l2_dev;
65 struct v4l2_subdev *v4l2_subdev;
66 struct mutex lock;
67 struct i2c_adapter i2c_adapter;
68
69 u8 *buffer;
70};
71
72static inline struct si4713_usb_device *to_si4713_dev(struct v4l2_device *v4l2_dev)
73{
74 return container_of(v4l2_dev, struct si4713_usb_device, v4l2_dev);
75}
76
77static int vidioc_querycap(struct file *file, void *priv,
78 struct v4l2_capability *v)
79{
80 struct si4713_usb_device *radio = video_drvdata(file);
81
82 strlcpy(v->driver, "radio-usb-si4713", sizeof(v->driver));
83 strlcpy(v->card, "Si4713 FM Transmitter", sizeof(v->card));
84 usb_make_path(radio->usbdev, v->bus_info, sizeof(v->bus_info));
85 v->device_caps = V4L2_CAP_MODULATOR | V4L2_CAP_RDS_OUTPUT;
86 v->capabilities = v->device_caps | V4L2_CAP_DEVICE_CAPS;
87
88 return 0;
89}
90
91static int vidioc_g_modulator(struct file *file, void *priv,
92 struct v4l2_modulator *vm)
93{
94 struct si4713_usb_device *radio = video_drvdata(file);
95
96 return v4l2_subdev_call(radio->v4l2_subdev, tuner, g_modulator, vm);
97}
98
99static int vidioc_s_modulator(struct file *file, void *priv,
100 const struct v4l2_modulator *vm)
101{
102 struct si4713_usb_device *radio = video_drvdata(file);
103
104 return v4l2_subdev_call(radio->v4l2_subdev, tuner, s_modulator, vm);
105}
106
107static int vidioc_s_frequency(struct file *file, void *priv,
108 const struct v4l2_frequency *vf)
109{
110 struct si4713_usb_device *radio = video_drvdata(file);
111
112 return v4l2_subdev_call(radio->v4l2_subdev, tuner, s_frequency, vf);
113}
114
115static int vidioc_g_frequency(struct file *file, void *priv,
116 struct v4l2_frequency *vf)
117{
118 struct si4713_usb_device *radio = video_drvdata(file);
119
120 return v4l2_subdev_call(radio->v4l2_subdev, tuner, g_frequency, vf);
121}
122
123static const struct v4l2_ioctl_ops usb_si4713_ioctl_ops = {
124 .vidioc_querycap = vidioc_querycap,
125 .vidioc_g_modulator = vidioc_g_modulator,
126 .vidioc_s_modulator = vidioc_s_modulator,
127 .vidioc_g_frequency = vidioc_g_frequency,
128 .vidioc_s_frequency = vidioc_s_frequency,
129 .vidioc_log_status = v4l2_ctrl_log_status,
130 .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
131 .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
132};
133
134/* File system interface */
135static const struct v4l2_file_operations usb_si4713_fops = {
136 .owner = THIS_MODULE,
137 .open = v4l2_fh_open,
138 .release = v4l2_fh_release,
139 .poll = v4l2_ctrl_poll,
140 .unlocked_ioctl = video_ioctl2,
141};
142
143static void usb_si4713_video_device_release(struct v4l2_device *v4l2_dev)
144{
145 struct si4713_usb_device *radio = to_si4713_dev(v4l2_dev);
146 struct i2c_adapter *adapter = &radio->i2c_adapter;
147
148 i2c_del_adapter(adapter);
149 v4l2_device_unregister(&radio->v4l2_dev);
150 kfree(radio->buffer);
151 kfree(radio);
152}
153
154/*
155 * This command sequence emulates the behaviour of the Windows driver.
156 * The structure of these commands was determined by sniffing the
157 * usb traffic of the device during startup.
158 * Most likely, these commands make some queries to the device.
159 * Commands are sent to enquire parameters like the bus mode,
160 * component revision, boot mode, the device serial number etc.
161 *
162 * These commands are necessary to be sent in this order during startup.
163 * The device fails to powerup if these commands are not sent.
164 *
165 * The complete list of startup commands is given in the start_seq table below.
166 */
167static int si4713_send_startup_command(struct si4713_usb_device *radio)
168{
169 unsigned long until_jiffies = jiffies + usecs_to_jiffies(USB_RESP_TIMEOUT) + 1;
170 u8 *buffer = radio->buffer;
171 int retval;
172
173 /* send the command */
174 retval = usb_control_msg(radio->usbdev, usb_sndctrlpipe(radio->usbdev, 0),
175 0x09, 0x21, 0x033f, 0, radio->buffer,
176 BUFFER_LENGTH, USB_TIMEOUT);
177 if (retval < 0)
178 return retval;
179
180 for (;;) {
181 /* receive the response */
182 retval = usb_control_msg(radio->usbdev, usb_rcvctrlpipe(radio->usbdev, 0),
183 0x01, 0xa1, 0x033f, 0, radio->buffer,
184 BUFFER_LENGTH, USB_TIMEOUT);
185 if (retval < 0)
186 return retval;
187 if (!radio->buffer[1]) {
188 /* USB traffic sniffing showed that some commands require
189 * additional checks. */
190 switch (buffer[1]) {
191 case 0x32:
192 if (radio->buffer[2] == 0)
193 return 0;
194 break;
195 case 0x14:
196 case 0x12:
197 if (radio->buffer[2] & SI4713_CTS)
198 return 0;
199 break;
200 case 0x06:
201 if ((radio->buffer[2] & SI4713_CTS) && radio->buffer[9] == 0x08)
202 return 0;
203 break;
204 default:
205 return 0;
206 }
207 }
208 if (time_is_before_jiffies(until_jiffies))
209 return -EIO;
210 msleep(3);
211 }
212
213 return retval;
214}
215
216struct si4713_start_seq_table {
217 int len;
218 u8 payload[8];
219};
220
221/*
222 * Some of the startup commands that could be recognized are :
223 * (0x03): Get serial number of the board (Response : CB000-00-00)
224 * (0x06, 0x03, 0x03, 0x08, 0x01, 0x0f) : Get Component revision
225 */
226static struct si4713_start_seq_table start_seq[] = {
227
228 { 1, { 0x03 } },
229 { 2, { 0x32, 0x7f } },
230 { 6, { 0x06, 0x03, 0x03, 0x08, 0x01, 0x0f } },
231 { 2, { 0x14, 0x02 } },
232 { 2, { 0x09, 0x90 } },
233 { 3, { 0x08, 0x90, 0xfa } },
234 { 2, { 0x36, 0x01 } },
235 { 2, { 0x05, 0x03 } },
236 { 7, { 0x06, 0x00, 0x06, 0x0e, 0x01, 0x0f, 0x05 } },
237 { 1, { 0x12 } },
238 /* Commands that are sent after pressing the 'Initialize'
239 button in the windows application */
240 { 1, { 0x03 } },
241 { 1, { 0x01 } },
242 { 2, { 0x09, 0x90 } },
243 { 3, { 0x08, 0x90, 0xfa } },
244 { 1, { 0x34 } },
245 { 2, { 0x35, 0x01 } },
246 { 2, { 0x36, 0x01 } },
247 { 2, { 0x30, 0x09 } },
248 { 4, { 0x30, 0x06, 0x00, 0xe2 } },
249 { 3, { 0x31, 0x01, 0x30 } },
250 { 3, { 0x31, 0x04, 0x09 } },
251 { 2, { 0x05, 0x02 } },
252 { 6, { 0x06, 0x03, 0x03, 0x08, 0x01, 0x0f } },
253};
254
255static int si4713_start_seq(struct si4713_usb_device *radio)
256{
257 int retval = 0;
258 int i;
259
260 radio->buffer[0] = 0x3f;
261
262 for (i = 0; i < ARRAY_SIZE(start_seq); i++) {
263 int len = start_seq[i].len;
264 u8 *payload = start_seq[i].payload;
265
266 memcpy(radio->buffer + 1, payload, len);
267 memset(radio->buffer + len + 1, 0, BUFFER_LENGTH - 1 - len);
268 retval = si4713_send_startup_command(radio);
269 }
270
271 return retval;
272}
273
274static struct i2c_board_info si4713_board_info = {
275 I2C_BOARD_INFO("si4713", SI4713_I2C_ADDR_BUSEN_HIGH),
276};
277
278struct si4713_command_table {
279 int command_id;
280 u8 payload[8];
281};
282
283/*
284 * Structure of a command :
285 * Byte 1 : 0x3f (always)
286 * Byte 2 : 0x06 (send a command)
287 * Byte 3 : Unknown
288 * Byte 4 : Number of arguments + 1 (for the command byte)
289 * Byte 5 : Number of response bytes
290 */
291static struct si4713_command_table command_table[] = {
292
293 { SI4713_CMD_POWER_UP, { 0x00, SI4713_PWUP_NARGS + 1, SI4713_PWUP_NRESP} },
294 { SI4713_CMD_GET_REV, { 0x03, 0x01, SI4713_GETREV_NRESP } },
295 { SI4713_CMD_POWER_DOWN, { 0x00, 0x01, SI4713_PWDN_NRESP} },
296 { SI4713_CMD_SET_PROPERTY, { 0x00, SI4713_SET_PROP_NARGS + 1, SI4713_SET_PROP_NRESP } },
297 { SI4713_CMD_GET_PROPERTY, { 0x00, SI4713_GET_PROP_NARGS + 1, SI4713_GET_PROP_NRESP } },
298 { SI4713_CMD_TX_TUNE_FREQ, { 0x03, SI4713_TXFREQ_NARGS + 1, SI4713_TXFREQ_NRESP } },
299 { SI4713_CMD_TX_TUNE_POWER, { 0x03, SI4713_TXPWR_NARGS + 1, SI4713_TXPWR_NRESP } },
300 { SI4713_CMD_TX_TUNE_MEASURE, { 0x03, SI4713_TXMEA_NARGS + 1, SI4713_TXMEA_NRESP } },
301 { SI4713_CMD_TX_TUNE_STATUS, { 0x00, SI4713_TXSTATUS_NARGS + 1, SI4713_TXSTATUS_NRESP } },
302 { SI4713_CMD_TX_ASQ_STATUS, { 0x03, SI4713_ASQSTATUS_NARGS + 1, SI4713_ASQSTATUS_NRESP } },
303 { SI4713_CMD_GET_INT_STATUS, { 0x03, 0x01, SI4713_GET_STATUS_NRESP } },
304 { SI4713_CMD_TX_RDS_BUFF, { 0x03, SI4713_RDSBUFF_NARGS + 1, SI4713_RDSBUFF_NRESP } },
305 { SI4713_CMD_TX_RDS_PS, { 0x00, SI4713_RDSPS_NARGS + 1, SI4713_RDSPS_NRESP } },
306};
307
308static int send_command(struct si4713_usb_device *radio, u8 *payload, char *data, int len)
309{
310 int retval;
311
312 radio->buffer[0] = 0x3f;
313 radio->buffer[1] = 0x06;
314
315 memcpy(radio->buffer + 2, payload, 3);
316 memcpy(radio->buffer + 5, data, len);
317 memset(radio->buffer + 5 + len, 0, BUFFER_LENGTH - 5 - len);
318
319 /* send the command */
320 retval = usb_control_msg(radio->usbdev, usb_sndctrlpipe(radio->usbdev, 0),
321 0x09, 0x21, 0x033f, 0, radio->buffer,
322 BUFFER_LENGTH, USB_TIMEOUT);
323
324 return retval < 0 ? retval : 0;
325}
326
327static int si4713_i2c_read(struct si4713_usb_device *radio, char *data, int len)
328{
329 unsigned long until_jiffies = jiffies + usecs_to_jiffies(USB_RESP_TIMEOUT) + 1;
330 int retval;
331
332 /* receive the response */
333 for (;;) {
334 retval = usb_control_msg(radio->usbdev,
335 usb_rcvctrlpipe(radio->usbdev, 0),
336 0x01, 0xa1, 0x033f, 0, radio->buffer,
337 BUFFER_LENGTH, USB_TIMEOUT);
338 if (retval < 0)
339 return retval;
340
341 /*
342 * Check that we get a valid reply back (buffer[1] == 0) and
343 * that CTS is set before returning, otherwise we wait and try
344 * again. The i2c driver also does the CTS check, but the timeouts
345 * used there are much too small for this USB driver, so we wait
346 * for it here.
347 */
348 if (radio->buffer[1] == 0 && (radio->buffer[2] & SI4713_CTS)) {
349 memcpy(data, radio->buffer + 2, len);
350 return 0;
351 }
352 if (time_is_before_jiffies(until_jiffies)) {
353 /* Zero the status value, ensuring CTS isn't set */
354 data[0] = 0;
355 return 0;
356 }
357 msleep(3);
358 }
359}
360
361static int si4713_i2c_write(struct si4713_usb_device *radio, char *data, int len)
362{
363 int retval = -EINVAL;
364 int i;
365
366 if (len > BUFFER_LENGTH - 5)
367 return -EINVAL;
368
369 for (i = 0; i < ARRAY_SIZE(command_table); i++) {
370 if (data[0] == command_table[i].command_id)
371 retval = send_command(radio, command_table[i].payload,
372 data, len);
373 }
374
375 return retval < 0 ? retval : 0;
376}
377
378static int si4713_transfer(struct i2c_adapter *i2c_adapter,
379 struct i2c_msg *msgs, int num)
380{
381 struct si4713_usb_device *radio = i2c_get_adapdata(i2c_adapter);
382 int retval = -EINVAL;
383 int i;
384
385 if (num <= 0)
386 return 0;
387
388 for (i = 0; i < num; i++) {
389 if (msgs[i].flags & I2C_M_RD)
390 retval = si4713_i2c_read(radio, msgs[i].buf, msgs[i].len);
391 else
392 retval = si4713_i2c_write(radio, msgs[i].buf, msgs[i].len);
393 if (retval)
394 break;
395 }
396
397 return retval ? retval : num;
398}
399
400static u32 si4713_functionality(struct i2c_adapter *adapter)
401{
402 return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;
403}
404
405static struct i2c_algorithm si4713_algo = {
406 .master_xfer = si4713_transfer,
407 .functionality = si4713_functionality,
408};
409
410/* This name value shows up in the sysfs filename associated
411 with this I2C adapter */
412static struct i2c_adapter si4713_i2c_adapter_template = {
413 .name = "si4713-i2c",
414 .owner = THIS_MODULE,
415 .algo = &si4713_algo,
416};
417
418static int si4713_register_i2c_adapter(struct si4713_usb_device *radio)
419{
420 radio->i2c_adapter = si4713_i2c_adapter_template;
421 /* set up sysfs linkage to our parent device */
422 radio->i2c_adapter.dev.parent = &radio->usbdev->dev;
423 i2c_set_adapdata(&radio->i2c_adapter, radio);
424
425 return i2c_add_adapter(&radio->i2c_adapter);
426}
427
428/* check if the device is present and register with v4l and usb if it is */
429static int usb_si4713_probe(struct usb_interface *intf,
430 const struct usb_device_id *id)
431{
432 struct si4713_usb_device *radio;
433 struct i2c_adapter *adapter;
434 struct v4l2_subdev *sd;
435 int retval = -ENOMEM;
436
437 dev_info(&intf->dev, "Si4713 development board discovered: (%04X:%04X)\n",
438 id->idVendor, id->idProduct);
439
440 /* Initialize local device structure */
441 radio = kzalloc(sizeof(struct si4713_usb_device), GFP_KERNEL);
442 if (radio)
443 radio->buffer = kmalloc(BUFFER_LENGTH, GFP_KERNEL);
444
445 if (!radio || !radio->buffer) {
446 dev_err(&intf->dev, "kmalloc for si4713_usb_device failed\n");
447 kfree(radio);
448 return -ENOMEM;
449 }
450
451 mutex_init(&radio->lock);
452
453 radio->usbdev = interface_to_usbdev(intf);
454 radio->intf = intf;
455 usb_set_intfdata(intf, &radio->v4l2_dev);
456
457 retval = si4713_start_seq(radio);
458 if (retval < 0)
459 goto err_v4l2;
460
461 retval = v4l2_device_register(&intf->dev, &radio->v4l2_dev);
462 if (retval < 0) {
463 dev_err(&intf->dev, "couldn't register v4l2_device\n");
464 goto err_v4l2;
465 }
466
467 retval = si4713_register_i2c_adapter(radio);
468 if (retval < 0) {
469 dev_err(&intf->dev, "could not register i2c device\n");
470 goto err_i2cdev;
471 }
472
473 adapter = &radio->i2c_adapter;
474 sd = v4l2_i2c_new_subdev_board(&radio->v4l2_dev, adapter,
475 &si4713_board_info, NULL);
476 radio->v4l2_subdev = sd;
477 if (!sd) {
478 dev_err(&intf->dev, "cannot get v4l2 subdevice\n");
479 retval = -ENODEV;
480 goto del_adapter;
481 }
482
483 radio->vdev.ctrl_handler = sd->ctrl_handler;
484 radio->v4l2_dev.release = usb_si4713_video_device_release;
485 strlcpy(radio->vdev.name, radio->v4l2_dev.name,
486 sizeof(radio->vdev.name));
487 radio->vdev.v4l2_dev = &radio->v4l2_dev;
488 radio->vdev.fops = &usb_si4713_fops;
489 radio->vdev.ioctl_ops = &usb_si4713_ioctl_ops;
490 radio->vdev.lock = &radio->lock;
491 radio->vdev.release = video_device_release_empty;
492 radio->vdev.vfl_dir = VFL_DIR_TX;
493
494 video_set_drvdata(&radio->vdev, radio);
495 set_bit(V4L2_FL_USE_FH_PRIO, &radio->vdev.flags);
496
497 retval = video_register_device(&radio->vdev, VFL_TYPE_RADIO, -1);
498 if (retval < 0) {
499 dev_err(&intf->dev, "could not register video device\n");
500 goto del_adapter;
501 }
502
503 dev_info(&intf->dev, "V4L2 device registered as %s\n",
504 video_device_node_name(&radio->vdev));
505
506 return 0;
507
508del_adapter:
509 i2c_del_adapter(adapter);
510err_i2cdev:
511 v4l2_device_unregister(&radio->v4l2_dev);
512err_v4l2:
513 kfree(radio->buffer);
514 kfree(radio);
515 return retval;
516}
517
518static void usb_si4713_disconnect(struct usb_interface *intf)
519{
520 struct si4713_usb_device *radio = to_si4713_dev(usb_get_intfdata(intf));
521
522 dev_info(&intf->dev, "Si4713 development board now disconnected\n");
523
524 mutex_lock(&radio->lock);
525 usb_set_intfdata(intf, NULL);
526 video_unregister_device(&radio->vdev);
527 v4l2_device_disconnect(&radio->v4l2_dev);
528 mutex_unlock(&radio->lock);
529 v4l2_device_put(&radio->v4l2_dev);
530}
531
532/* USB subsystem interface */
533static struct usb_driver usb_si4713_driver = {
534 .name = "radio-usb-si4713",
535 .probe = usb_si4713_probe,
536 .disconnect = usb_si4713_disconnect,
537 .id_table = usb_si4713_usb_device_table,
538};
539
540module_usb_driver(usb_si4713_driver);
diff --git a/drivers/media/radio/si4713/si4713.c b/drivers/media/radio/si4713/si4713.c
new file mode 100644
index 000000000000..07d5153811e8
--- /dev/null
+++ b/drivers/media/radio/si4713/si4713.c
@@ -0,0 +1,1557 @@
1/*
2 * drivers/media/radio/si4713-i2c.c
3 *
4 * Silicon Labs Si4713 FM Radio Transmitter I2C commands.
5 *
6 * Copyright (c) 2009 Nokia Corporation
7 * Contact: Eduardo Valentin <eduardo.valentin@nokia.com>
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 */
23
24#include <linux/completion.h>
25#include <linux/delay.h>
26#include <linux/interrupt.h>
27#include <linux/i2c.h>
28#include <linux/slab.h>
29#include <linux/gpio.h>
30#include <linux/module.h>
31#include <media/v4l2-device.h>
32#include <media/v4l2-ioctl.h>
33#include <media/v4l2-common.h>
34
35#include "si4713.h"
36
37/* module parameters */
38static int debug;
39module_param(debug, int, S_IRUGO | S_IWUSR);
40MODULE_PARM_DESC(debug, "Debug level (0 - 2)");
41
42MODULE_LICENSE("GPL");
43MODULE_AUTHOR("Eduardo Valentin <eduardo.valentin@nokia.com>");
44MODULE_DESCRIPTION("I2C driver for Si4713 FM Radio Transmitter");
45MODULE_VERSION("0.0.1");
46
47#define DEFAULT_RDS_PI 0x00
48#define DEFAULT_RDS_PTY 0x00
49#define DEFAULT_RDS_DEVIATION 0x00C8
50#define DEFAULT_RDS_PS_REPEAT_COUNT 0x0003
51#define DEFAULT_LIMITER_RTIME 0x1392
52#define DEFAULT_LIMITER_DEV 0x102CA
53#define DEFAULT_PILOT_FREQUENCY 0x4A38
54#define DEFAULT_PILOT_DEVIATION 0x1A5E
55#define DEFAULT_ACOMP_ATIME 0x0000
56#define DEFAULT_ACOMP_RTIME 0xF4240L
57#define DEFAULT_ACOMP_GAIN 0x0F
58#define DEFAULT_ACOMP_THRESHOLD (-0x28)
59#define DEFAULT_MUTE 0x01
60#define DEFAULT_POWER_LEVEL 88
61#define DEFAULT_FREQUENCY 8800
62#define DEFAULT_PREEMPHASIS FMPE_EU
63#define DEFAULT_TUNE_RNL 0xFF
64
65#define to_si4713_device(sd) container_of(sd, struct si4713_device, sd)
66
67/* frequency domain transformation (using times 10 to avoid floats) */
68#define FREQDEV_UNIT 100000
69#define FREQV4L2_MULTI 625
70#define si4713_to_v4l2(f) ((f * FREQDEV_UNIT) / FREQV4L2_MULTI)
71#define v4l2_to_si4713(f) ((f * FREQV4L2_MULTI) / FREQDEV_UNIT)
72#define FREQ_RANGE_LOW 7600
73#define FREQ_RANGE_HIGH 10800
74
75#define MAX_ARGS 7
76
77#define RDS_BLOCK 8
78#define RDS_BLOCK_CLEAR 0x03
79#define RDS_BLOCK_LOAD 0x04
80#define RDS_RADIOTEXT_2A 0x20
81#define RDS_RADIOTEXT_BLK_SIZE 4
82#define RDS_RADIOTEXT_INDEX_MAX 0x0F
83#define RDS_CARRIAGE_RETURN 0x0D
84
85#define rds_ps_nblocks(len) ((len / RDS_BLOCK) + (len % RDS_BLOCK ? 1 : 0))
86
87#define get_status_bit(p, b, m) (((p) & (m)) >> (b))
88#define set_bits(p, v, b, m) (((p) & ~(m)) | ((v) << (b)))
89
90#define ATTACK_TIME_UNIT 500
91
92#define POWER_OFF 0x00
93#define POWER_ON 0x01
94
95#define msb(x) ((u8)((u16) x >> 8))
96#define lsb(x) ((u8)((u16) x & 0x00FF))
97#define compose_u16(msb, lsb) (((u16)msb << 8) | lsb)
98#define check_command_failed(status) (!(status & SI4713_CTS) || \
99 (status & SI4713_ERR))
100/* mute definition */
101#define set_mute(p) ((p & 1) | ((p & 1) << 1));
102
103#ifdef DEBUG
104#define DBG_BUFFER(device, message, buffer, size) \
105 { \
106 int i; \
107 char str[(size)*5]; \
108 for (i = 0; i < size; i++) \
109 sprintf(str + i * 5, " 0x%02x", buffer[i]); \
110 v4l2_dbg(2, debug, device, "%s:%s\n", message, str); \
111 }
112#else
113#define DBG_BUFFER(device, message, buffer, size)
114#endif
115
116/*
117 * Values for limiter release time (sorted by second column)
118 * device release
119 * value time (us)
120 */
121static long limiter_times[] = {
122 2000, 250,
123 1000, 500,
124 510, 1000,
125 255, 2000,
126 170, 3000,
127 127, 4020,
128 102, 5010,
129 85, 6020,
130 73, 7010,
131 64, 7990,
132 57, 8970,
133 51, 10030,
134 25, 20470,
135 17, 30110,
136 13, 39380,
137 10, 51190,
138 8, 63690,
139 7, 73140,
140 6, 85330,
141 5, 102390,
142};
143
144/*
145 * Values for audio compression release time (sorted by second column)
146 * device release
147 * value time (us)
148 */
149static unsigned long acomp_rtimes[] = {
150 0, 100000,
151 1, 200000,
152 2, 350000,
153 3, 525000,
154 4, 1000000,
155};
156
157/*
158 * Values for preemphasis (sorted by second column)
159 * device preemphasis
160 * value value (v4l2)
161 */
162static unsigned long preemphasis_values[] = {
163 FMPE_DISABLED, V4L2_PREEMPHASIS_DISABLED,
164 FMPE_EU, V4L2_PREEMPHASIS_50_uS,
165 FMPE_USA, V4L2_PREEMPHASIS_75_uS,
166};
167
168static int usecs_to_dev(unsigned long usecs, unsigned long const array[],
169 int size)
170{
171 int i;
172 int rval = -EINVAL;
173
174 for (i = 0; i < size / 2; i++)
175 if (array[(i * 2) + 1] >= usecs) {
176 rval = array[i * 2];
177 break;
178 }
179
180 return rval;
181}
182
183/* si4713_handler: IRQ handler, just complete work */
184static irqreturn_t si4713_handler(int irq, void *dev)
185{
186 struct si4713_device *sdev = dev;
187
188 v4l2_dbg(2, debug, &sdev->sd,
189 "%s: sending signal to completion work.\n", __func__);
190 complete(&sdev->work);
191
192 return IRQ_HANDLED;
193}
194
195/*
196 * si4713_send_command - sends a command to si4713 and waits its response
197 * @sdev: si4713_device structure for the device we are communicating
198 * @command: command id
199 * @args: command arguments we are sending (up to 7)
200 * @argn: actual size of @args
201 * @response: buffer to place the expected response from the device (up to 15)
202 * @respn: actual size of @response
203 * @usecs: amount of time to wait before reading the response (in usecs)
204 */
205static int si4713_send_command(struct si4713_device *sdev, const u8 command,
206 const u8 args[], const int argn,
207 u8 response[], const int respn, const int usecs)
208{
209 struct i2c_client *client = v4l2_get_subdevdata(&sdev->sd);
210 unsigned long until_jiffies;
211 u8 data1[MAX_ARGS + 1];
212 int err;
213
214 if (!client->adapter)
215 return -ENODEV;
216
217 /* First send the command and its arguments */
218 data1[0] = command;
219 memcpy(data1 + 1, args, argn);
220 DBG_BUFFER(&sdev->sd, "Parameters", data1, argn + 1);
221
222 err = i2c_master_send(client, data1, argn + 1);
223 if (err != argn + 1) {
224 v4l2_err(&sdev->sd, "Error while sending command 0x%02x\n",
225 command);
226 return err < 0 ? err : -EIO;
227 }
228
229 until_jiffies = jiffies + usecs_to_jiffies(usecs) + 1;
230
231 /* Wait response from interrupt */
232 if (client->irq) {
233 if (!wait_for_completion_timeout(&sdev->work,
234 usecs_to_jiffies(usecs) + 1))
235 v4l2_warn(&sdev->sd,
236 "(%s) Device took too much time to answer.\n",
237 __func__);
238 }
239
240 do {
241 err = i2c_master_recv(client, response, respn);
242 if (err != respn) {
243 v4l2_err(&sdev->sd,
244 "Error %d while reading response for command 0x%02x\n",
245 err, command);
246 return err < 0 ? err : -EIO;
247 }
248
249 DBG_BUFFER(&sdev->sd, "Response", response, respn);
250 if (!check_command_failed(response[0]))
251 return 0;
252
253 if (client->irq)
254 return -EBUSY;
255 if (usecs <= 1000)
256 usleep_range(usecs, 1000);
257 else
258 usleep_range(1000, 2000);
259 } while (time_is_after_jiffies(until_jiffies));
260
261 return -EBUSY;
262}
263
264/*
265 * si4713_read_property - reads a si4713 property
266 * @sdev: si4713_device structure for the device we are communicating
267 * @prop: property identification number
268 * @pv: property value to be returned on success
269 */
270static int si4713_read_property(struct si4713_device *sdev, u16 prop, u32 *pv)
271{
272 int err;
273 u8 val[SI4713_GET_PROP_NRESP];
274 /*
275 * .First byte = 0
276 * .Second byte = property's MSB
277 * .Third byte = property's LSB
278 */
279 const u8 args[SI4713_GET_PROP_NARGS] = {
280 0x00,
281 msb(prop),
282 lsb(prop),
283 };
284
285 err = si4713_send_command(sdev, SI4713_CMD_GET_PROPERTY,
286 args, ARRAY_SIZE(args), val,
287 ARRAY_SIZE(val), DEFAULT_TIMEOUT);
288
289 if (err < 0)
290 return err;
291
292 *pv = compose_u16(val[2], val[3]);
293
294 v4l2_dbg(1, debug, &sdev->sd,
295 "%s: property=0x%02x value=0x%02x status=0x%02x\n",
296 __func__, prop, *pv, val[0]);
297
298 return err;
299}
300
301/*
302 * si4713_write_property - modifies a si4713 property
303 * @sdev: si4713_device structure for the device we are communicating
304 * @prop: property identification number
305 * @val: new value for that property
306 */
307static int si4713_write_property(struct si4713_device *sdev, u16 prop, u16 val)
308{
309 int rval;
310 u8 resp[SI4713_SET_PROP_NRESP];
311 /*
312 * .First byte = 0
313 * .Second byte = property's MSB
314 * .Third byte = property's LSB
315 * .Fourth byte = value's MSB
316 * .Fifth byte = value's LSB
317 */
318 const u8 args[SI4713_SET_PROP_NARGS] = {
319 0x00,
320 msb(prop),
321 lsb(prop),
322 msb(val),
323 lsb(val),
324 };
325
326 rval = si4713_send_command(sdev, SI4713_CMD_SET_PROPERTY,
327 args, ARRAY_SIZE(args),
328 resp, ARRAY_SIZE(resp),
329 DEFAULT_TIMEOUT);
330
331 if (rval < 0)
332 return rval;
333
334 v4l2_dbg(1, debug, &sdev->sd,
335 "%s: property=0x%02x value=0x%02x status=0x%02x\n",
336 __func__, prop, val, resp[0]);
337
338 /*
339 * As there is no command response for SET_PROPERTY,
340 * wait Tcomp time to finish before proceed, in order
341 * to have property properly set.
342 */
343 msleep(TIMEOUT_SET_PROPERTY);
344
345 return rval;
346}
347
348/*
349 * si4713_powerup - Powers the device up
350 * @sdev: si4713_device structure for the device we are communicating
351 */
352static int si4713_powerup(struct si4713_device *sdev)
353{
354 struct i2c_client *client = v4l2_get_subdevdata(&sdev->sd);
355 int err;
356 u8 resp[SI4713_PWUP_NRESP];
357 /*
358 * .First byte = Enabled interrupts and boot function
359 * .Second byte = Input operation mode
360 */
361 u8 args[SI4713_PWUP_NARGS] = {
362 SI4713_PWUP_GPO2OEN | SI4713_PWUP_FUNC_TX,
363 SI4713_PWUP_OPMOD_ANALOG,
364 };
365
366 if (sdev->power_state)
367 return 0;
368
369 if (sdev->supplies) {
370 err = regulator_bulk_enable(sdev->supplies, sdev->supply_data);
371 if (err) {
372 v4l2_err(&sdev->sd, "Failed to enable supplies: %d\n", err);
373 return err;
374 }
375 }
376 if (gpio_is_valid(sdev->gpio_reset)) {
377 udelay(50);
378 gpio_set_value(sdev->gpio_reset, 1);
379 }
380
381 if (client->irq)
382 args[0] |= SI4713_PWUP_CTSIEN;
383
384 err = si4713_send_command(sdev, SI4713_CMD_POWER_UP,
385 args, ARRAY_SIZE(args),
386 resp, ARRAY_SIZE(resp),
387 TIMEOUT_POWER_UP);
388
389 if (!err) {
390 v4l2_dbg(1, debug, &sdev->sd, "Powerup response: 0x%02x\n",
391 resp[0]);
392 v4l2_dbg(1, debug, &sdev->sd, "Device in power up mode\n");
393 sdev->power_state = POWER_ON;
394
395 if (client->irq)
396 err = si4713_write_property(sdev, SI4713_GPO_IEN,
397 SI4713_STC_INT | SI4713_CTS);
398 return err;
399 }
400 if (gpio_is_valid(sdev->gpio_reset))
401 gpio_set_value(sdev->gpio_reset, 0);
402 if (sdev->supplies) {
403 err = regulator_bulk_disable(sdev->supplies, sdev->supply_data);
404 if (err)
405 v4l2_err(&sdev->sd,
406 "Failed to disable supplies: %d\n", err);
407 }
408
409 return err;
410}
411
412/*
413 * si4713_powerdown - Powers the device down
414 * @sdev: si4713_device structure for the device we are communicating
415 */
416static int si4713_powerdown(struct si4713_device *sdev)
417{
418 int err;
419 u8 resp[SI4713_PWDN_NRESP];
420
421 if (!sdev->power_state)
422 return 0;
423
424 err = si4713_send_command(sdev, SI4713_CMD_POWER_DOWN,
425 NULL, 0,
426 resp, ARRAY_SIZE(resp),
427 DEFAULT_TIMEOUT);
428
429 if (!err) {
430 v4l2_dbg(1, debug, &sdev->sd, "Power down response: 0x%02x\n",
431 resp[0]);
432 v4l2_dbg(1, debug, &sdev->sd, "Device in reset mode\n");
433 if (gpio_is_valid(sdev->gpio_reset))
434 gpio_set_value(sdev->gpio_reset, 0);
435 if (sdev->supplies) {
436 err = regulator_bulk_disable(sdev->supplies,
437 sdev->supply_data);
438 if (err)
439 v4l2_err(&sdev->sd,
440 "Failed to disable supplies: %d\n", err);
441 }
442 sdev->power_state = POWER_OFF;
443 }
444
445 return err;
446}
447
448/*
449 * si4713_checkrev - Checks if we are treating a device with the correct rev.
450 * @sdev: si4713_device structure for the device we are communicating
451 */
452static int si4713_checkrev(struct si4713_device *sdev)
453{
454 struct i2c_client *client = v4l2_get_subdevdata(&sdev->sd);
455 int rval;
456 u8 resp[SI4713_GETREV_NRESP];
457
458 rval = si4713_send_command(sdev, SI4713_CMD_GET_REV,
459 NULL, 0,
460 resp, ARRAY_SIZE(resp),
461 DEFAULT_TIMEOUT);
462
463 if (rval < 0)
464 return rval;
465
466 if (resp[1] == SI4713_PRODUCT_NUMBER) {
467 v4l2_info(&sdev->sd, "chip found @ 0x%02x (%s)\n",
468 client->addr << 1, client->adapter->name);
469 } else {
470 v4l2_err(&sdev->sd, "Invalid product number 0x%X\n", resp[1]);
471 rval = -EINVAL;
472 }
473 return rval;
474}
475
476/*
477 * si4713_wait_stc - Waits STC interrupt and clears status bits. Useful
478 * for TX_TUNE_POWER, TX_TUNE_FREQ and TX_TUNE_MEAS
479 * @sdev: si4713_device structure for the device we are communicating
480 * @usecs: timeout to wait for STC interrupt signal
481 */
482static int si4713_wait_stc(struct si4713_device *sdev, const int usecs)
483{
484 struct i2c_client *client = v4l2_get_subdevdata(&sdev->sd);
485 u8 resp[SI4713_GET_STATUS_NRESP];
486 unsigned long start_jiffies = jiffies;
487 int err;
488
489 if (client->irq &&
490 !wait_for_completion_timeout(&sdev->work, usecs_to_jiffies(usecs) + 1))
491 v4l2_warn(&sdev->sd,
492 "(%s) Device took too much time to answer.\n", __func__);
493
494 for (;;) {
495 /* Clear status bits */
496 err = si4713_send_command(sdev, SI4713_CMD_GET_INT_STATUS,
497 NULL, 0,
498 resp, ARRAY_SIZE(resp),
499 DEFAULT_TIMEOUT);
500 /* The USB device returns errors when it waits for the
501 * STC bit to be set. Hence polling */
502 if (err >= 0) {
503 v4l2_dbg(1, debug, &sdev->sd,
504 "%s: status bits: 0x%02x\n", __func__, resp[0]);
505
506 if (resp[0] & SI4713_STC_INT)
507 return 0;
508 }
509 if (jiffies_to_usecs(jiffies - start_jiffies) > usecs)
510 return err < 0 ? err : -EIO;
511 /* We sleep here for 3-4 ms in order to avoid flooding the device
512 * with USB requests. The si4713 USB driver was developed
513 * by reverse engineering the Windows USB driver. The windows
514 * driver also has a ~2.5 ms delay between responses. */
515 usleep_range(3000, 4000);
516 }
517}
518
519/*
520 * si4713_tx_tune_freq - Sets the state of the RF carrier and sets the tuning
521 * frequency between 76 and 108 MHz in 10 kHz units and
522 * steps of 50 kHz.
523 * @sdev: si4713_device structure for the device we are communicating
524 * @frequency: desired frequency (76 - 108 MHz, unit 10 KHz, step 50 kHz)
525 */
526static int si4713_tx_tune_freq(struct si4713_device *sdev, u16 frequency)
527{
528 int err;
529 u8 val[SI4713_TXFREQ_NRESP];
530 /*
531 * .First byte = 0
532 * .Second byte = frequency's MSB
533 * .Third byte = frequency's LSB
534 */
535 const u8 args[SI4713_TXFREQ_NARGS] = {
536 0x00,
537 msb(frequency),
538 lsb(frequency),
539 };
540
541 err = si4713_send_command(sdev, SI4713_CMD_TX_TUNE_FREQ,
542 args, ARRAY_SIZE(args), val,
543 ARRAY_SIZE(val), DEFAULT_TIMEOUT);
544
545 if (err < 0)
546 return err;
547
548 v4l2_dbg(1, debug, &sdev->sd,
549 "%s: frequency=0x%02x status=0x%02x\n", __func__,
550 frequency, val[0]);
551
552 err = si4713_wait_stc(sdev, TIMEOUT_TX_TUNE);
553 if (err < 0)
554 return err;
555
556 return compose_u16(args[1], args[2]);
557}
558
559/*
560 * si4713_tx_tune_power - Sets the RF voltage level between 88 and 120 dBuV in
561 * 1 dB units. A value of 0x00 indicates off. The command
562 * also sets the antenna tuning capacitance. A value of 0
563 * indicates autotuning, and a value of 1 - 191 indicates
564 * a manual override, which results in a tuning
565 * capacitance of 0.25 pF x @antcap.
566 * @sdev: si4713_device structure for the device we are communicating
567 * @power: tuning power (88 - 120 dBuV, unit/step 1 dB)
568 * @antcap: value of antenna tuning capacitor (0 - 191)
569 */
570static int si4713_tx_tune_power(struct si4713_device *sdev, u8 power,
571 u8 antcap)
572{
573 int err;
574 u8 val[SI4713_TXPWR_NRESP];
575 /*
576 * .First byte = 0
577 * .Second byte = 0
578 * .Third byte = power
579 * .Fourth byte = antcap
580 */
581 u8 args[SI4713_TXPWR_NARGS] = {
582 0x00,
583 0x00,
584 power,
585 antcap,
586 };
587
588 /* Map power values 1-87 to MIN_POWER (88) */
589 if (power > 0 && power < SI4713_MIN_POWER)
590 args[2] = power = SI4713_MIN_POWER;
591
592 err = si4713_send_command(sdev, SI4713_CMD_TX_TUNE_POWER,
593 args, ARRAY_SIZE(args), val,
594 ARRAY_SIZE(val), DEFAULT_TIMEOUT);
595
596 if (err < 0)
597 return err;
598
599 v4l2_dbg(1, debug, &sdev->sd,
600 "%s: power=0x%02x antcap=0x%02x status=0x%02x\n",
601 __func__, power, antcap, val[0]);
602
603 return si4713_wait_stc(sdev, TIMEOUT_TX_TUNE_POWER);
604}
605
606/*
607 * si4713_tx_tune_measure - Enters receive mode and measures the received noise
608 * level in units of dBuV on the selected frequency.
609 * The Frequency must be between 76 and 108 MHz in 10 kHz
610 * units and steps of 50 kHz. The command also sets the
611 * antenna tuning capacitance. A value of 0 means
612 * autotuning, and a value of 1 to 191 indicates manual
613 * override.
614 * @sdev: si4713_device structure for the device we are communicating
615 * @frequency: desired frequency (76 - 108 MHz, unit 10 KHz, step 50 kHz)
616 * @antcap: value of antenna tuning capacitor (0 - 191)
617 */
618static int si4713_tx_tune_measure(struct si4713_device *sdev, u16 frequency,
619 u8 antcap)
620{
621 int err;
622 u8 val[SI4713_TXMEA_NRESP];
623 /*
624 * .First byte = 0
625 * .Second byte = frequency's MSB
626 * .Third byte = frequency's LSB
627 * .Fourth byte = antcap
628 */
629 const u8 args[SI4713_TXMEA_NARGS] = {
630 0x00,
631 msb(frequency),
632 lsb(frequency),
633 antcap,
634 };
635
636 sdev->tune_rnl = DEFAULT_TUNE_RNL;
637
638 if (antcap > SI4713_MAX_ANTCAP)
639 return -EDOM;
640
641 err = si4713_send_command(sdev, SI4713_CMD_TX_TUNE_MEASURE,
642 args, ARRAY_SIZE(args), val,
643 ARRAY_SIZE(val), DEFAULT_TIMEOUT);
644
645 if (err < 0)
646 return err;
647
648 v4l2_dbg(1, debug, &sdev->sd,
649 "%s: frequency=0x%02x antcap=0x%02x status=0x%02x\n",
650 __func__, frequency, antcap, val[0]);
651
652 return si4713_wait_stc(sdev, TIMEOUT_TX_TUNE);
653}
654
655/*
656 * si4713_tx_tune_status- Returns the status of the tx_tune_freq, tx_tune_mea or
657 * tx_tune_power commands. This command return the current
658 * frequency, output voltage in dBuV, the antenna tunning
659 * capacitance value and the received noise level. The
660 * command also clears the stcint interrupt bit when the
661 * first bit of its arguments is high.
662 * @sdev: si4713_device structure for the device we are communicating
663 * @intack: 0x01 to clear the seek/tune complete interrupt status indicator.
664 * @frequency: returned frequency
665 * @power: returned power
666 * @antcap: returned antenna capacitance
667 * @noise: returned noise level
668 */
669static int si4713_tx_tune_status(struct si4713_device *sdev, u8 intack,
670 u16 *frequency, u8 *power,
671 u8 *antcap, u8 *noise)
672{
673 int err;
674 u8 val[SI4713_TXSTATUS_NRESP];
675 /*
676 * .First byte = intack bit
677 */
678 const u8 args[SI4713_TXSTATUS_NARGS] = {
679 intack & SI4713_INTACK_MASK,
680 };
681
682 err = si4713_send_command(sdev, SI4713_CMD_TX_TUNE_STATUS,
683 args, ARRAY_SIZE(args), val,
684 ARRAY_SIZE(val), DEFAULT_TIMEOUT);
685
686 if (!err) {
687 v4l2_dbg(1, debug, &sdev->sd,
688 "%s: status=0x%02x\n", __func__, val[0]);
689 *frequency = compose_u16(val[2], val[3]);
690 sdev->frequency = *frequency;
691 *power = val[5];
692 *antcap = val[6];
693 *noise = val[7];
694 v4l2_dbg(1, debug, &sdev->sd, "%s: response: %d x 10 kHz "
695 "(power %d, antcap %d, rnl %d)\n", __func__,
696 *frequency, *power, *antcap, *noise);
697 }
698
699 return err;
700}
701
702/*
703 * si4713_tx_rds_buff - Loads the RDS group buffer FIFO or circular buffer.
704 * @sdev: si4713_device structure for the device we are communicating
705 * @mode: the buffer operation mode.
706 * @rdsb: RDS Block B
707 * @rdsc: RDS Block C
708 * @rdsd: RDS Block D
709 * @cbleft: returns the number of available circular buffer blocks minus the
710 * number of used circular buffer blocks.
711 */
712static int si4713_tx_rds_buff(struct si4713_device *sdev, u8 mode, u16 rdsb,
713 u16 rdsc, u16 rdsd, s8 *cbleft)
714{
715 int err;
716 u8 val[SI4713_RDSBUFF_NRESP];
717
718 const u8 args[SI4713_RDSBUFF_NARGS] = {
719 mode & SI4713_RDSBUFF_MODE_MASK,
720 msb(rdsb),
721 lsb(rdsb),
722 msb(rdsc),
723 lsb(rdsc),
724 msb(rdsd),
725 lsb(rdsd),
726 };
727
728 err = si4713_send_command(sdev, SI4713_CMD_TX_RDS_BUFF,
729 args, ARRAY_SIZE(args), val,
730 ARRAY_SIZE(val), DEFAULT_TIMEOUT);
731
732 if (!err) {
733 v4l2_dbg(1, debug, &sdev->sd,
734 "%s: status=0x%02x\n", __func__, val[0]);
735 *cbleft = (s8)val[2] - val[3];
736 v4l2_dbg(1, debug, &sdev->sd, "%s: response: interrupts"
737 " 0x%02x cb avail: %d cb used %d fifo avail"
738 " %d fifo used %d\n", __func__, val[1],
739 val[2], val[3], val[4], val[5]);
740 }
741
742 return err;
743}
744
745/*
746 * si4713_tx_rds_ps - Loads the program service buffer.
747 * @sdev: si4713_device structure for the device we are communicating
748 * @psid: program service id to be loaded.
749 * @pschar: assumed 4 size char array to be loaded into the program service
750 */
751static int si4713_tx_rds_ps(struct si4713_device *sdev, u8 psid,
752 unsigned char *pschar)
753{
754 int err;
755 u8 val[SI4713_RDSPS_NRESP];
756
757 const u8 args[SI4713_RDSPS_NARGS] = {
758 psid & SI4713_RDSPS_PSID_MASK,
759 pschar[0],
760 pschar[1],
761 pschar[2],
762 pschar[3],
763 };
764
765 err = si4713_send_command(sdev, SI4713_CMD_TX_RDS_PS,
766 args, ARRAY_SIZE(args), val,
767 ARRAY_SIZE(val), DEFAULT_TIMEOUT);
768
769 if (err < 0)
770 return err;
771
772 v4l2_dbg(1, debug, &sdev->sd, "%s: status=0x%02x\n", __func__, val[0]);
773
774 return err;
775}
776
777static int si4713_set_power_state(struct si4713_device *sdev, u8 value)
778{
779 if (value)
780 return si4713_powerup(sdev);
781 return si4713_powerdown(sdev);
782}
783
784static int si4713_set_mute(struct si4713_device *sdev, u16 mute)
785{
786 int rval = 0;
787
788 mute = set_mute(mute);
789
790 if (sdev->power_state)
791 rval = si4713_write_property(sdev,
792 SI4713_TX_LINE_INPUT_MUTE, mute);
793
794 return rval;
795}
796
797static int si4713_set_rds_ps_name(struct si4713_device *sdev, char *ps_name)
798{
799 int rval = 0, i;
800 u8 len = 0;
801
802 /* We want to clear the whole thing */
803 if (!strlen(ps_name))
804 memset(ps_name, 0, MAX_RDS_PS_NAME + 1);
805
806 if (sdev->power_state) {
807 /* Write the new ps name and clear the padding */
808 for (i = 0; i < MAX_RDS_PS_NAME; i += (RDS_BLOCK / 2)) {
809 rval = si4713_tx_rds_ps(sdev, (i / (RDS_BLOCK / 2)),
810 ps_name + i);
811 if (rval < 0)
812 return rval;
813 }
814
815 /* Setup the size to be sent */
816 if (strlen(ps_name))
817 len = strlen(ps_name) - 1;
818 else
819 len = 1;
820
821 rval = si4713_write_property(sdev,
822 SI4713_TX_RDS_PS_MESSAGE_COUNT,
823 rds_ps_nblocks(len));
824 if (rval < 0)
825 return rval;
826
827 rval = si4713_write_property(sdev,
828 SI4713_TX_RDS_PS_REPEAT_COUNT,
829 DEFAULT_RDS_PS_REPEAT_COUNT * 2);
830 if (rval < 0)
831 return rval;
832 }
833
834 return rval;
835}
836
837static int si4713_set_rds_radio_text(struct si4713_device *sdev, const char *rt)
838{
839 static const char cr[RDS_RADIOTEXT_BLK_SIZE] = { RDS_CARRIAGE_RETURN, 0 };
840 int rval = 0, i;
841 u16 t_index = 0;
842 u8 b_index = 0, cr_inserted = 0;
843 s8 left;
844
845 if (!sdev->power_state)
846 return rval;
847
848 rval = si4713_tx_rds_buff(sdev, RDS_BLOCK_CLEAR, 0, 0, 0, &left);
849 if (rval < 0)
850 return rval;
851
852 if (!strlen(rt))
853 return rval;
854
855 do {
856 /* RDS spec says that if the last block isn't used,
857 * then apply a carriage return
858 */
859 if (t_index < (RDS_RADIOTEXT_INDEX_MAX * RDS_RADIOTEXT_BLK_SIZE)) {
860 for (i = 0; i < RDS_RADIOTEXT_BLK_SIZE; i++) {
861 if (!rt[t_index + i] ||
862 rt[t_index + i] == RDS_CARRIAGE_RETURN) {
863 rt = cr;
864 cr_inserted = 1;
865 break;
866 }
867 }
868 }
869
870 rval = si4713_tx_rds_buff(sdev, RDS_BLOCK_LOAD,
871 compose_u16(RDS_RADIOTEXT_2A, b_index++),
872 compose_u16(rt[t_index], rt[t_index + 1]),
873 compose_u16(rt[t_index + 2], rt[t_index + 3]),
874 &left);
875 if (rval < 0)
876 return rval;
877
878 t_index += RDS_RADIOTEXT_BLK_SIZE;
879
880 if (cr_inserted)
881 break;
882 } while (left > 0);
883
884 return rval;
885}
886
887/*
888 * si4713_update_tune_status - update properties from tx_tune_status
889 * command. Must be called with sdev->mutex held.
890 * @sdev: si4713_device structure for the device we are communicating
891 */
892static int si4713_update_tune_status(struct si4713_device *sdev)
893{
894 int rval;
895 u16 f = 0;
896 u8 p = 0, a = 0, n = 0;
897
898 rval = si4713_tx_tune_status(sdev, 0x00, &f, &p, &a, &n);
899
900 if (rval < 0)
901 goto exit;
902
903/* TODO: check that power_level and antenna_capacitor really are not
904 changed by the hardware. If they are, then these controls should become
905 volatiles.
906 sdev->power_level = p;
907 sdev->antenna_capacitor = a;*/
908 sdev->tune_rnl = n;
909
910exit:
911 return rval;
912}
913
914static int si4713_choose_econtrol_action(struct si4713_device *sdev, u32 id,
915 s32 *bit, s32 *mask, u16 *property, int *mul,
916 unsigned long **table, int *size)
917{
918 s32 rval = 0;
919
920 switch (id) {
921 /* FM_TX class controls */
922 case V4L2_CID_RDS_TX_PI:
923 *property = SI4713_TX_RDS_PI;
924 *mul = 1;
925 break;
926 case V4L2_CID_AUDIO_COMPRESSION_THRESHOLD:
927 *property = SI4713_TX_ACOMP_THRESHOLD;
928 *mul = 1;
929 break;
930 case V4L2_CID_AUDIO_COMPRESSION_GAIN:
931 *property = SI4713_TX_ACOMP_GAIN;
932 *mul = 1;
933 break;
934 case V4L2_CID_PILOT_TONE_FREQUENCY:
935 *property = SI4713_TX_PILOT_FREQUENCY;
936 *mul = 1;
937 break;
938 case V4L2_CID_AUDIO_COMPRESSION_ATTACK_TIME:
939 *property = SI4713_TX_ACOMP_ATTACK_TIME;
940 *mul = ATTACK_TIME_UNIT;
941 break;
942 case V4L2_CID_PILOT_TONE_DEVIATION:
943 *property = SI4713_TX_PILOT_DEVIATION;
944 *mul = 10;
945 break;
946 case V4L2_CID_AUDIO_LIMITER_DEVIATION:
947 *property = SI4713_TX_AUDIO_DEVIATION;
948 *mul = 10;
949 break;
950 case V4L2_CID_RDS_TX_DEVIATION:
951 *property = SI4713_TX_RDS_DEVIATION;
952 *mul = 1;
953 break;
954
955 case V4L2_CID_RDS_TX_PTY:
956 *property = SI4713_TX_RDS_PS_MISC;
957 *bit = 5;
958 *mask = 0x1F << 5;
959 break;
960 case V4L2_CID_AUDIO_LIMITER_ENABLED:
961 *property = SI4713_TX_ACOMP_ENABLE;
962 *bit = 1;
963 *mask = 1 << 1;
964 break;
965 case V4L2_CID_AUDIO_COMPRESSION_ENABLED:
966 *property = SI4713_TX_ACOMP_ENABLE;
967 *bit = 0;
968 *mask = 1 << 0;
969 break;
970 case V4L2_CID_PILOT_TONE_ENABLED:
971 *property = SI4713_TX_COMPONENT_ENABLE;
972 *bit = 0;
973 *mask = 1 << 0;
974 break;
975
976 case V4L2_CID_AUDIO_LIMITER_RELEASE_TIME:
977 *property = SI4713_TX_LIMITER_RELEASE_TIME;
978 *table = limiter_times;
979 *size = ARRAY_SIZE(limiter_times);
980 break;
981 case V4L2_CID_AUDIO_COMPRESSION_RELEASE_TIME:
982 *property = SI4713_TX_ACOMP_RELEASE_TIME;
983 *table = acomp_rtimes;
984 *size = ARRAY_SIZE(acomp_rtimes);
985 break;
986 case V4L2_CID_TUNE_PREEMPHASIS:
987 *property = SI4713_TX_PREEMPHASIS;
988 *table = preemphasis_values;
989 *size = ARRAY_SIZE(preemphasis_values);
990 break;
991
992 default:
993 rval = -EINVAL;
994 break;
995 }
996
997 return rval;
998}
999
1000static int si4713_s_frequency(struct v4l2_subdev *sd, const struct v4l2_frequency *f);
1001static int si4713_s_modulator(struct v4l2_subdev *sd, const struct v4l2_modulator *);
1002/*
1003 * si4713_setup - Sets the device up with current configuration.
1004 * @sdev: si4713_device structure for the device we are communicating
1005 */
1006static int si4713_setup(struct si4713_device *sdev)
1007{
1008 struct v4l2_frequency f;
1009 struct v4l2_modulator vm;
1010 int rval;
1011
1012 /* Device procedure needs to set frequency first */
1013 f.tuner = 0;
1014 f.frequency = sdev->frequency ? sdev->frequency : DEFAULT_FREQUENCY;
1015 f.frequency = si4713_to_v4l2(f.frequency);
1016 rval = si4713_s_frequency(&sdev->sd, &f);
1017
1018 vm.index = 0;
1019 if (sdev->stereo)
1020 vm.txsubchans = V4L2_TUNER_SUB_STEREO;
1021 else
1022 vm.txsubchans = V4L2_TUNER_SUB_MONO;
1023 if (sdev->rds_enabled)
1024 vm.txsubchans |= V4L2_TUNER_SUB_RDS;
1025 si4713_s_modulator(&sdev->sd, &vm);
1026
1027 return rval;
1028}
1029
1030/*
1031 * si4713_initialize - Sets the device up with default configuration.
1032 * @sdev: si4713_device structure for the device we are communicating
1033 */
1034static int si4713_initialize(struct si4713_device *sdev)
1035{
1036 int rval;
1037
1038 rval = si4713_set_power_state(sdev, POWER_ON);
1039 if (rval < 0)
1040 return rval;
1041
1042 rval = si4713_checkrev(sdev);
1043 if (rval < 0)
1044 return rval;
1045
1046 rval = si4713_set_power_state(sdev, POWER_OFF);
1047 if (rval < 0)
1048 return rval;
1049
1050 sdev->frequency = DEFAULT_FREQUENCY;
1051 sdev->stereo = 1;
1052 sdev->tune_rnl = DEFAULT_TUNE_RNL;
1053 return 0;
1054}
1055
1056/* si4713_s_ctrl - set the value of a control */
1057static int si4713_s_ctrl(struct v4l2_ctrl *ctrl)
1058{
1059 struct si4713_device *sdev =
1060 container_of(ctrl->handler, struct si4713_device, ctrl_handler);
1061 u32 val = 0;
1062 s32 bit = 0, mask = 0;
1063 u16 property = 0;
1064 int mul = 0;
1065 unsigned long *table = NULL;
1066 int size = 0;
1067 bool force = false;
1068 int c;
1069 int ret = 0;
1070
1071 if (ctrl->id != V4L2_CID_AUDIO_MUTE)
1072 return -EINVAL;
1073 if (ctrl->is_new) {
1074 if (ctrl->val) {
1075 ret = si4713_set_mute(sdev, ctrl->val);
1076 if (!ret)
1077 ret = si4713_set_power_state(sdev, POWER_DOWN);
1078 return ret;
1079 }
1080 ret = si4713_set_power_state(sdev, POWER_UP);
1081 if (!ret)
1082 ret = si4713_set_mute(sdev, ctrl->val);
1083 if (!ret)
1084 ret = si4713_setup(sdev);
1085 if (ret)
1086 return ret;
1087 force = true;
1088 }
1089
1090 if (!sdev->power_state)
1091 return 0;
1092
1093 for (c = 1; !ret && c < ctrl->ncontrols; c++) {
1094 ctrl = ctrl->cluster[c];
1095
1096 if (!force && !ctrl->is_new)
1097 continue;
1098
1099 switch (ctrl->id) {
1100 case V4L2_CID_RDS_TX_PS_NAME:
1101 ret = si4713_set_rds_ps_name(sdev, ctrl->string);
1102 break;
1103
1104 case V4L2_CID_RDS_TX_RADIO_TEXT:
1105 ret = si4713_set_rds_radio_text(sdev, ctrl->string);
1106 break;
1107
1108 case V4L2_CID_TUNE_ANTENNA_CAPACITOR:
1109 /* don't handle this control if we force setting all
1110 * controls since in that case it will be handled by
1111 * V4L2_CID_TUNE_POWER_LEVEL. */
1112 if (force)
1113 break;
1114 /* fall through */
1115 case V4L2_CID_TUNE_POWER_LEVEL:
1116 ret = si4713_tx_tune_power(sdev,
1117 sdev->tune_pwr_level->val, sdev->tune_ant_cap->val);
1118 if (!ret) {
1119 /* Make sure we don't set this twice */
1120 sdev->tune_ant_cap->is_new = false;
1121 sdev->tune_pwr_level->is_new = false;
1122 }
1123 break;
1124
1125 default:
1126 ret = si4713_choose_econtrol_action(sdev, ctrl->id, &bit,
1127 &mask, &property, &mul, &table, &size);
1128 if (ret < 0)
1129 break;
1130
1131 val = ctrl->val;
1132 if (mul) {
1133 val = val / mul;
1134 } else if (table) {
1135 ret = usecs_to_dev(val, table, size);
1136 if (ret < 0)
1137 break;
1138 val = ret;
1139 ret = 0;
1140 }
1141
1142 if (mask) {
1143 ret = si4713_read_property(sdev, property, &val);
1144 if (ret < 0)
1145 break;
1146 val = set_bits(val, ctrl->val, bit, mask);
1147 }
1148
1149 ret = si4713_write_property(sdev, property, val);
1150 if (ret < 0)
1151 break;
1152 if (mask)
1153 val = ctrl->val;
1154 break;
1155 }
1156 }
1157
1158 return ret;
1159}
1160
1161/* si4713_ioctl - deal with private ioctls (only rnl for now) */
1162static long si4713_ioctl(struct v4l2_subdev *sd, unsigned int cmd, void *arg)
1163{
1164 struct si4713_device *sdev = to_si4713_device(sd);
1165 struct si4713_rnl *rnl = arg;
1166 u16 frequency;
1167 int rval = 0;
1168
1169 if (!arg)
1170 return -EINVAL;
1171
1172 switch (cmd) {
1173 case SI4713_IOC_MEASURE_RNL:
1174 frequency = v4l2_to_si4713(rnl->frequency);
1175
1176 if (sdev->power_state) {
1177 /* Set desired measurement frequency */
1178 rval = si4713_tx_tune_measure(sdev, frequency, 0);
1179 if (rval < 0)
1180 return rval;
1181 /* get results from tune status */
1182 rval = si4713_update_tune_status(sdev);
1183 if (rval < 0)
1184 return rval;
1185 }
1186 rnl->rnl = sdev->tune_rnl;
1187 break;
1188
1189 default:
1190 /* nothing */
1191 rval = -ENOIOCTLCMD;
1192 }
1193
1194 return rval;
1195}
1196
1197/* si4713_g_modulator - get modulator attributes */
1198static int si4713_g_modulator(struct v4l2_subdev *sd, struct v4l2_modulator *vm)
1199{
1200 struct si4713_device *sdev = to_si4713_device(sd);
1201 int rval = 0;
1202
1203 if (!sdev)
1204 return -ENODEV;
1205
1206 if (vm->index > 0)
1207 return -EINVAL;
1208
1209 strncpy(vm->name, "FM Modulator", 32);
1210 vm->capability = V4L2_TUNER_CAP_STEREO | V4L2_TUNER_CAP_LOW |
1211 V4L2_TUNER_CAP_RDS | V4L2_TUNER_CAP_RDS_CONTROLS;
1212
1213 /* Report current frequency range limits */
1214 vm->rangelow = si4713_to_v4l2(FREQ_RANGE_LOW);
1215 vm->rangehigh = si4713_to_v4l2(FREQ_RANGE_HIGH);
1216
1217 if (sdev->power_state) {
1218 u32 comp_en = 0;
1219
1220 rval = si4713_read_property(sdev, SI4713_TX_COMPONENT_ENABLE,
1221 &comp_en);
1222 if (rval < 0)
1223 return rval;
1224
1225 sdev->stereo = get_status_bit(comp_en, 1, 1 << 1);
1226 }
1227
1228 /* Report current audio mode: mono or stereo */
1229 if (sdev->stereo)
1230 vm->txsubchans = V4L2_TUNER_SUB_STEREO;
1231 else
1232 vm->txsubchans = V4L2_TUNER_SUB_MONO;
1233
1234 /* Report rds feature status */
1235 if (sdev->rds_enabled)
1236 vm->txsubchans |= V4L2_TUNER_SUB_RDS;
1237 else
1238 vm->txsubchans &= ~V4L2_TUNER_SUB_RDS;
1239
1240 return rval;
1241}
1242
1243/* si4713_s_modulator - set modulator attributes */
1244static int si4713_s_modulator(struct v4l2_subdev *sd, const struct v4l2_modulator *vm)
1245{
1246 struct si4713_device *sdev = to_si4713_device(sd);
1247 int rval = 0;
1248 u16 stereo, rds;
1249 u32 p;
1250
1251 if (!sdev)
1252 return -ENODEV;
1253
1254 if (vm->index > 0)
1255 return -EINVAL;
1256
1257 /* Set audio mode: mono or stereo */
1258 if (vm->txsubchans & V4L2_TUNER_SUB_STEREO)
1259 stereo = 1;
1260 else if (vm->txsubchans & V4L2_TUNER_SUB_MONO)
1261 stereo = 0;
1262 else
1263 return -EINVAL;
1264
1265 rds = !!(vm->txsubchans & V4L2_TUNER_SUB_RDS);
1266
1267 if (sdev->power_state) {
1268 rval = si4713_read_property(sdev,
1269 SI4713_TX_COMPONENT_ENABLE, &p);
1270 if (rval < 0)
1271 return rval;
1272
1273 p = set_bits(p, stereo, 1, 1 << 1);
1274 p = set_bits(p, rds, 2, 1 << 2);
1275
1276 rval = si4713_write_property(sdev,
1277 SI4713_TX_COMPONENT_ENABLE, p);
1278 if (rval < 0)
1279 return rval;
1280 }
1281
1282 sdev->stereo = stereo;
1283 sdev->rds_enabled = rds;
1284
1285 return rval;
1286}
1287
1288/* si4713_g_frequency - get tuner or modulator radio frequency */
1289static int si4713_g_frequency(struct v4l2_subdev *sd, struct v4l2_frequency *f)
1290{
1291 struct si4713_device *sdev = to_si4713_device(sd);
1292 int rval = 0;
1293
1294 if (f->tuner)
1295 return -EINVAL;
1296
1297 if (sdev->power_state) {
1298 u16 freq;
1299 u8 p, a, n;
1300
1301 rval = si4713_tx_tune_status(sdev, 0x00, &freq, &p, &a, &n);
1302 if (rval < 0)
1303 return rval;
1304
1305 sdev->frequency = freq;
1306 }
1307
1308 f->frequency = si4713_to_v4l2(sdev->frequency);
1309
1310 return rval;
1311}
1312
1313/* si4713_s_frequency - set tuner or modulator radio frequency */
1314static int si4713_s_frequency(struct v4l2_subdev *sd, const struct v4l2_frequency *f)
1315{
1316 struct si4713_device *sdev = to_si4713_device(sd);
1317 int rval = 0;
1318 u16 frequency = v4l2_to_si4713(f->frequency);
1319
1320 if (f->tuner)
1321 return -EINVAL;
1322
1323 /* Check frequency range */
1324 frequency = clamp_t(u16, frequency, FREQ_RANGE_LOW, FREQ_RANGE_HIGH);
1325
1326 if (sdev->power_state) {
1327 rval = si4713_tx_tune_freq(sdev, frequency);
1328 if (rval < 0)
1329 return rval;
1330 frequency = rval;
1331 rval = 0;
1332 }
1333 sdev->frequency = frequency;
1334
1335 return rval;
1336}
1337
1338static const struct v4l2_ctrl_ops si4713_ctrl_ops = {
1339 .s_ctrl = si4713_s_ctrl,
1340};
1341
1342static const struct v4l2_subdev_core_ops si4713_subdev_core_ops = {
1343 .ioctl = si4713_ioctl,
1344};
1345
1346static const struct v4l2_subdev_tuner_ops si4713_subdev_tuner_ops = {
1347 .g_frequency = si4713_g_frequency,
1348 .s_frequency = si4713_s_frequency,
1349 .g_modulator = si4713_g_modulator,
1350 .s_modulator = si4713_s_modulator,
1351};
1352
1353static const struct v4l2_subdev_ops si4713_subdev_ops = {
1354 .core = &si4713_subdev_core_ops,
1355 .tuner = &si4713_subdev_tuner_ops,
1356};
1357
1358/*
1359 * I2C driver interface
1360 */
1361/* si4713_probe - probe for the device */
1362static int si4713_probe(struct i2c_client *client,
1363 const struct i2c_device_id *id)
1364{
1365 struct si4713_device *sdev;
1366 struct si4713_platform_data *pdata = client->dev.platform_data;
1367 struct v4l2_ctrl_handler *hdl;
1368 int rval, i;
1369
1370 sdev = kzalloc(sizeof(*sdev), GFP_KERNEL);
1371 if (!sdev) {
1372 dev_err(&client->dev, "Failed to alloc video device.\n");
1373 rval = -ENOMEM;
1374 goto exit;
1375 }
1376
1377 sdev->gpio_reset = -1;
1378 if (pdata && gpio_is_valid(pdata->gpio_reset)) {
1379 rval = gpio_request(pdata->gpio_reset, "si4713 reset");
1380 if (rval) {
1381 dev_err(&client->dev,
1382 "Failed to request gpio: %d\n", rval);
1383 goto free_sdev;
1384 }
1385 sdev->gpio_reset = pdata->gpio_reset;
1386 gpio_direction_output(sdev->gpio_reset, 0);
1387 sdev->supplies = pdata->supplies;
1388 }
1389
1390 for (i = 0; i < sdev->supplies; i++)
1391 sdev->supply_data[i].supply = pdata->supply_names[i];
1392
1393 rval = regulator_bulk_get(&client->dev, sdev->supplies,
1394 sdev->supply_data);
1395 if (rval) {
1396 dev_err(&client->dev, "Cannot get regulators: %d\n", rval);
1397 goto free_gpio;
1398 }
1399
1400 v4l2_i2c_subdev_init(&sdev->sd, client, &si4713_subdev_ops);
1401
1402 init_completion(&sdev->work);
1403
1404 hdl = &sdev->ctrl_handler;
1405 v4l2_ctrl_handler_init(hdl, 20);
1406 sdev->mute = v4l2_ctrl_new_std(hdl, &si4713_ctrl_ops,
1407 V4L2_CID_AUDIO_MUTE, 0, 1, 1, DEFAULT_MUTE);
1408
1409 sdev->rds_pi = v4l2_ctrl_new_std(hdl, &si4713_ctrl_ops,
1410 V4L2_CID_RDS_TX_PI, 0, 0xffff, 1, DEFAULT_RDS_PI);
1411 sdev->rds_pty = v4l2_ctrl_new_std(hdl, &si4713_ctrl_ops,
1412 V4L2_CID_RDS_TX_PTY, 0, 31, 1, DEFAULT_RDS_PTY);
1413 sdev->rds_deviation = v4l2_ctrl_new_std(hdl, &si4713_ctrl_ops,
1414 V4L2_CID_RDS_TX_DEVIATION, 0, MAX_RDS_DEVIATION,
1415 10, DEFAULT_RDS_DEVIATION);
1416 /*
1417 * Report step as 8. From RDS spec, psname
1418 * should be 8. But there are receivers which scroll strings
1419 * sized as 8xN.
1420 */
1421 sdev->rds_ps_name = v4l2_ctrl_new_std(hdl, &si4713_ctrl_ops,
1422 V4L2_CID_RDS_TX_PS_NAME, 0, MAX_RDS_PS_NAME, 8, 0);
1423 /*
1424 * Report step as 32 (2A block). From RDS spec,
1425 * radio text should be 32 for 2A block. But there are receivers
1426 * which scroll strings sized as 32xN. Setting default to 32.
1427 */
1428 sdev->rds_radio_text = v4l2_ctrl_new_std(hdl, &si4713_ctrl_ops,
1429 V4L2_CID_RDS_TX_RADIO_TEXT, 0, MAX_RDS_RADIO_TEXT, 32, 0);
1430
1431 sdev->limiter_enabled = v4l2_ctrl_new_std(hdl, &si4713_ctrl_ops,
1432 V4L2_CID_AUDIO_LIMITER_ENABLED, 0, 1, 1, 1);
1433 sdev->limiter_release_time = v4l2_ctrl_new_std(hdl, &si4713_ctrl_ops,
1434 V4L2_CID_AUDIO_LIMITER_RELEASE_TIME, 250,
1435 MAX_LIMITER_RELEASE_TIME, 10, DEFAULT_LIMITER_RTIME);
1436 sdev->limiter_deviation = v4l2_ctrl_new_std(hdl, &si4713_ctrl_ops,
1437 V4L2_CID_AUDIO_LIMITER_DEVIATION, 0,
1438 MAX_LIMITER_DEVIATION, 10, DEFAULT_LIMITER_DEV);
1439
1440 sdev->compression_enabled = v4l2_ctrl_new_std(hdl, &si4713_ctrl_ops,
1441 V4L2_CID_AUDIO_COMPRESSION_ENABLED, 0, 1, 1, 1);
1442 sdev->compression_gain = v4l2_ctrl_new_std(hdl, &si4713_ctrl_ops,
1443 V4L2_CID_AUDIO_COMPRESSION_GAIN, 0, MAX_ACOMP_GAIN, 1,
1444 DEFAULT_ACOMP_GAIN);
1445 sdev->compression_threshold = v4l2_ctrl_new_std(hdl, &si4713_ctrl_ops,
1446 V4L2_CID_AUDIO_COMPRESSION_THRESHOLD,
1447 MIN_ACOMP_THRESHOLD, MAX_ACOMP_THRESHOLD, 1,
1448 DEFAULT_ACOMP_THRESHOLD);
1449 sdev->compression_attack_time = v4l2_ctrl_new_std(hdl, &si4713_ctrl_ops,
1450 V4L2_CID_AUDIO_COMPRESSION_ATTACK_TIME, 0,
1451 MAX_ACOMP_ATTACK_TIME, 500, DEFAULT_ACOMP_ATIME);
1452 sdev->compression_release_time = v4l2_ctrl_new_std(hdl, &si4713_ctrl_ops,
1453 V4L2_CID_AUDIO_COMPRESSION_RELEASE_TIME, 100000,
1454 MAX_ACOMP_RELEASE_TIME, 100000, DEFAULT_ACOMP_RTIME);
1455
1456 sdev->pilot_tone_enabled = v4l2_ctrl_new_std(hdl, &si4713_ctrl_ops,
1457 V4L2_CID_PILOT_TONE_ENABLED, 0, 1, 1, 1);
1458 sdev->pilot_tone_deviation = v4l2_ctrl_new_std(hdl, &si4713_ctrl_ops,
1459 V4L2_CID_PILOT_TONE_DEVIATION, 0, MAX_PILOT_DEVIATION,
1460 10, DEFAULT_PILOT_DEVIATION);
1461 sdev->pilot_tone_freq = v4l2_ctrl_new_std(hdl, &si4713_ctrl_ops,
1462 V4L2_CID_PILOT_TONE_FREQUENCY, 0, MAX_PILOT_FREQUENCY,
1463 1, DEFAULT_PILOT_FREQUENCY);
1464
1465 sdev->tune_preemphasis = v4l2_ctrl_new_std_menu(hdl, &si4713_ctrl_ops,
1466 V4L2_CID_TUNE_PREEMPHASIS,
1467 V4L2_PREEMPHASIS_75_uS, 0, V4L2_PREEMPHASIS_50_uS);
1468 sdev->tune_pwr_level = v4l2_ctrl_new_std(hdl, &si4713_ctrl_ops,
1469 V4L2_CID_TUNE_POWER_LEVEL, 0, SI4713_MAX_POWER,
1470 1, DEFAULT_POWER_LEVEL);
1471 sdev->tune_ant_cap = v4l2_ctrl_new_std(hdl, &si4713_ctrl_ops,
1472 V4L2_CID_TUNE_ANTENNA_CAPACITOR, 0, SI4713_MAX_ANTCAP,
1473 1, 0);
1474
1475 if (hdl->error) {
1476 rval = hdl->error;
1477 goto free_ctrls;
1478 }
1479 v4l2_ctrl_cluster(20, &sdev->mute);
1480 sdev->sd.ctrl_handler = hdl;
1481
1482 if (client->irq) {
1483 rval = request_irq(client->irq,
1484 si4713_handler, IRQF_TRIGGER_FALLING,
1485 client->name, sdev);
1486 if (rval < 0) {
1487 v4l2_err(&sdev->sd, "Could not request IRQ\n");
1488 goto put_reg;
1489 }
1490 v4l2_dbg(1, debug, &sdev->sd, "IRQ requested.\n");
1491 } else {
1492 v4l2_warn(&sdev->sd, "IRQ not configured. Using timeouts.\n");
1493 }
1494
1495 rval = si4713_initialize(sdev);
1496 if (rval < 0) {
1497 v4l2_err(&sdev->sd, "Failed to probe device information.\n");
1498 goto free_irq;
1499 }
1500
1501 return 0;
1502
1503free_irq:
1504 if (client->irq)
1505 free_irq(client->irq, sdev);
1506free_ctrls:
1507 v4l2_ctrl_handler_free(hdl);
1508put_reg:
1509 regulator_bulk_free(sdev->supplies, sdev->supply_data);
1510free_gpio:
1511 if (gpio_is_valid(sdev->gpio_reset))
1512 gpio_free(sdev->gpio_reset);
1513free_sdev:
1514 kfree(sdev);
1515exit:
1516 return rval;
1517}
1518
1519/* si4713_remove - remove the device */
1520static int si4713_remove(struct i2c_client *client)
1521{
1522 struct v4l2_subdev *sd = i2c_get_clientdata(client);
1523 struct si4713_device *sdev = to_si4713_device(sd);
1524
1525 if (sdev->power_state)
1526 si4713_set_power_state(sdev, POWER_DOWN);
1527
1528 if (client->irq > 0)
1529 free_irq(client->irq, sdev);
1530
1531 v4l2_device_unregister_subdev(sd);
1532 v4l2_ctrl_handler_free(sd->ctrl_handler);
1533 regulator_bulk_free(sdev->supplies, sdev->supply_data);
1534 if (gpio_is_valid(sdev->gpio_reset))
1535 gpio_free(sdev->gpio_reset);
1536 kfree(sdev);
1537
1538 return 0;
1539}
1540
1541/* si4713_i2c_driver - i2c driver interface */
1542static const struct i2c_device_id si4713_id[] = {
1543 { "si4713" , 0 },
1544 { },
1545};
1546MODULE_DEVICE_TABLE(i2c, si4713_id);
1547
1548static struct i2c_driver si4713_i2c_driver = {
1549 .driver = {
1550 .name = "si4713",
1551 },
1552 .probe = si4713_probe,
1553 .remove = si4713_remove,
1554 .id_table = si4713_id,
1555};
1556
1557module_i2c_driver(si4713_i2c_driver);
diff --git a/drivers/media/radio/si4713/si4713.h b/drivers/media/radio/si4713/si4713.h
new file mode 100644
index 000000000000..4837cf6e0e1b
--- /dev/null
+++ b/drivers/media/radio/si4713/si4713.h
@@ -0,0 +1,240 @@
1/*
2 * drivers/media/radio/si4713-i2c.h
3 *
4 * Property and commands definitions for Si4713 radio transmitter chip.
5 *
6 * Copyright (c) 2008 Instituto Nokia de Tecnologia - INdT
7 * Contact: Eduardo Valentin <eduardo.valentin@nokia.com>
8 *
9 * This file is licensed under the terms of the GNU General Public License
10 * version 2. This program is licensed "as is" without any warranty of any
11 * kind, whether express or implied.
12 *
13 */
14
15#ifndef SI4713_I2C_H
16#define SI4713_I2C_H
17
18#include <linux/regulator/consumer.h>
19#include <media/v4l2-subdev.h>
20#include <media/v4l2-ctrls.h>
21#include <media/si4713.h>
22
23#define SI4713_PRODUCT_NUMBER 0x0D
24
25/* Command Timeouts */
26#define DEFAULT_TIMEOUT 500
27#define TIMEOUT_SET_PROPERTY 20
28#define TIMEOUT_TX_TUNE_POWER 30000
29#define TIMEOUT_TX_TUNE 110000
30#define TIMEOUT_POWER_UP 200000
31
32/*
33 * Command and its arguments definitions
34 */
35#define SI4713_PWUP_CTSIEN (1<<7)
36#define SI4713_PWUP_GPO2OEN (1<<6)
37#define SI4713_PWUP_PATCH (1<<5)
38#define SI4713_PWUP_XOSCEN (1<<4)
39#define SI4713_PWUP_FUNC_TX 0x02
40#define SI4713_PWUP_FUNC_PATCH 0x0F
41#define SI4713_PWUP_OPMOD_ANALOG 0x50
42#define SI4713_PWUP_OPMOD_DIGITAL 0x0F
43#define SI4713_PWUP_NARGS 2
44#define SI4713_PWUP_NRESP 1
45#define SI4713_CMD_POWER_UP 0x01
46
47#define SI4713_GETREV_NRESP 9
48#define SI4713_CMD_GET_REV 0x10
49
50#define SI4713_PWDN_NRESP 1
51#define SI4713_CMD_POWER_DOWN 0x11
52
53#define SI4713_SET_PROP_NARGS 5
54#define SI4713_SET_PROP_NRESP 1
55#define SI4713_CMD_SET_PROPERTY 0x12
56
57#define SI4713_GET_PROP_NARGS 3
58#define SI4713_GET_PROP_NRESP 4
59#define SI4713_CMD_GET_PROPERTY 0x13
60
61#define SI4713_GET_STATUS_NRESP 1
62#define SI4713_CMD_GET_INT_STATUS 0x14
63
64#define SI4713_CMD_PATCH_ARGS 0x15
65#define SI4713_CMD_PATCH_DATA 0x16
66
67#define SI4713_MAX_FREQ 10800
68#define SI4713_MIN_FREQ 7600
69#define SI4713_TXFREQ_NARGS 3
70#define SI4713_TXFREQ_NRESP 1
71#define SI4713_CMD_TX_TUNE_FREQ 0x30
72
73#define SI4713_MAX_POWER 120
74#define SI4713_MIN_POWER 88
75#define SI4713_MAX_ANTCAP 191
76#define SI4713_MIN_ANTCAP 0
77#define SI4713_TXPWR_NARGS 4
78#define SI4713_TXPWR_NRESP 1
79#define SI4713_CMD_TX_TUNE_POWER 0x31
80
81#define SI4713_TXMEA_NARGS 4
82#define SI4713_TXMEA_NRESP 1
83#define SI4713_CMD_TX_TUNE_MEASURE 0x32
84
85#define SI4713_INTACK_MASK 0x01
86#define SI4713_TXSTATUS_NARGS 1
87#define SI4713_TXSTATUS_NRESP 8
88#define SI4713_CMD_TX_TUNE_STATUS 0x33
89
90#define SI4713_OVERMOD_BIT (1 << 2)
91#define SI4713_IALH_BIT (1 << 1)
92#define SI4713_IALL_BIT (1 << 0)
93#define SI4713_ASQSTATUS_NARGS 1
94#define SI4713_ASQSTATUS_NRESP 5
95#define SI4713_CMD_TX_ASQ_STATUS 0x34
96
97#define SI4713_RDSBUFF_MODE_MASK 0x87
98#define SI4713_RDSBUFF_NARGS 7
99#define SI4713_RDSBUFF_NRESP 6
100#define SI4713_CMD_TX_RDS_BUFF 0x35
101
102#define SI4713_RDSPS_PSID_MASK 0x1F
103#define SI4713_RDSPS_NARGS 5
104#define SI4713_RDSPS_NRESP 1
105#define SI4713_CMD_TX_RDS_PS 0x36
106
107#define SI4713_CMD_GPO_CTL 0x80
108#define SI4713_CMD_GPO_SET 0x81
109
110/*
111 * Bits from status response
112 */
113#define SI4713_CTS (1<<7)
114#define SI4713_ERR (1<<6)
115#define SI4713_RDS_INT (1<<2)
116#define SI4713_ASQ_INT (1<<1)
117#define SI4713_STC_INT (1<<0)
118
119/*
120 * Property definitions
121 */
122#define SI4713_GPO_IEN 0x0001
123#define SI4713_DIG_INPUT_FORMAT 0x0101
124#define SI4713_DIG_INPUT_SAMPLE_RATE 0x0103
125#define SI4713_REFCLK_FREQ 0x0201
126#define SI4713_REFCLK_PRESCALE 0x0202
127#define SI4713_TX_COMPONENT_ENABLE 0x2100
128#define SI4713_TX_AUDIO_DEVIATION 0x2101
129#define SI4713_TX_PILOT_DEVIATION 0x2102
130#define SI4713_TX_RDS_DEVIATION 0x2103
131#define SI4713_TX_LINE_INPUT_LEVEL 0x2104
132#define SI4713_TX_LINE_INPUT_MUTE 0x2105
133#define SI4713_TX_PREEMPHASIS 0x2106
134#define SI4713_TX_PILOT_FREQUENCY 0x2107
135#define SI4713_TX_ACOMP_ENABLE 0x2200
136#define SI4713_TX_ACOMP_THRESHOLD 0x2201
137#define SI4713_TX_ACOMP_ATTACK_TIME 0x2202
138#define SI4713_TX_ACOMP_RELEASE_TIME 0x2203
139#define SI4713_TX_ACOMP_GAIN 0x2204
140#define SI4713_TX_LIMITER_RELEASE_TIME 0x2205
141#define SI4713_TX_ASQ_INTERRUPT_SOURCE 0x2300
142#define SI4713_TX_ASQ_LEVEL_LOW 0x2301
143#define SI4713_TX_ASQ_DURATION_LOW 0x2302
144#define SI4713_TX_ASQ_LEVEL_HIGH 0x2303
145#define SI4713_TX_ASQ_DURATION_HIGH 0x2304
146#define SI4713_TX_RDS_INTERRUPT_SOURCE 0x2C00
147#define SI4713_TX_RDS_PI 0x2C01
148#define SI4713_TX_RDS_PS_MIX 0x2C02
149#define SI4713_TX_RDS_PS_MISC 0x2C03
150#define SI4713_TX_RDS_PS_REPEAT_COUNT 0x2C04
151#define SI4713_TX_RDS_PS_MESSAGE_COUNT 0x2C05
152#define SI4713_TX_RDS_PS_AF 0x2C06
153#define SI4713_TX_RDS_FIFO_SIZE 0x2C07
154
155#define PREEMPHASIS_USA 75
156#define PREEMPHASIS_EU 50
157#define PREEMPHASIS_DISABLED 0
158#define FMPE_USA 0x00
159#define FMPE_EU 0x01
160#define FMPE_DISABLED 0x02
161
162#define POWER_UP 0x01
163#define POWER_DOWN 0x00
164
165#define MAX_RDS_PTY 31
166#define MAX_RDS_DEVIATION 90000
167
168/*
169 * PSNAME is known to be defined as 8 character sized (RDS Spec).
170 * However, there is receivers which scroll PSNAME 8xN sized.
171 */
172#define MAX_RDS_PS_NAME 96
173
174/*
175 * MAX_RDS_RADIO_TEXT is known to be defined as 32 (2A group) or 64 (2B group)
176 * character sized (RDS Spec).
177 * However, there is receivers which scroll them as well.
178 */
179#define MAX_RDS_RADIO_TEXT 384
180
181#define MAX_LIMITER_RELEASE_TIME 102390
182#define MAX_LIMITER_DEVIATION 90000
183
184#define MAX_PILOT_DEVIATION 90000
185#define MAX_PILOT_FREQUENCY 19000
186
187#define MAX_ACOMP_RELEASE_TIME 1000000
188#define MAX_ACOMP_ATTACK_TIME 5000
189#define MAX_ACOMP_THRESHOLD 0
190#define MIN_ACOMP_THRESHOLD (-40)
191#define MAX_ACOMP_GAIN 20
192
193#define SI4713_NUM_SUPPLIES 2
194
195/*
196 * si4713_device - private data
197 */
198struct si4713_device {
199 /* v4l2_subdev and i2c reference (v4l2_subdev priv data) */
200 struct v4l2_subdev sd;
201 struct v4l2_ctrl_handler ctrl_handler;
202 /* private data structures */
203 struct { /* si4713 control cluster */
204 /* This is one big cluster since the mute control
205 * powers off the device and after unmuting again all
206 * controls need to be set at once. The only way of doing
207 * that is by making it one big cluster. */
208 struct v4l2_ctrl *mute;
209 struct v4l2_ctrl *rds_ps_name;
210 struct v4l2_ctrl *rds_radio_text;
211 struct v4l2_ctrl *rds_pi;
212 struct v4l2_ctrl *rds_deviation;
213 struct v4l2_ctrl *rds_pty;
214 struct v4l2_ctrl *compression_enabled;
215 struct v4l2_ctrl *compression_threshold;
216 struct v4l2_ctrl *compression_gain;
217 struct v4l2_ctrl *compression_attack_time;
218 struct v4l2_ctrl *compression_release_time;
219 struct v4l2_ctrl *pilot_tone_enabled;
220 struct v4l2_ctrl *pilot_tone_freq;
221 struct v4l2_ctrl *pilot_tone_deviation;
222 struct v4l2_ctrl *limiter_enabled;
223 struct v4l2_ctrl *limiter_deviation;
224 struct v4l2_ctrl *limiter_release_time;
225 struct v4l2_ctrl *tune_preemphasis;
226 struct v4l2_ctrl *tune_pwr_level;
227 struct v4l2_ctrl *tune_ant_cap;
228 };
229 struct completion work;
230 unsigned supplies;
231 struct regulator_bulk_data supply_data[SI4713_NUM_SUPPLIES];
232 int gpio_reset;
233 u32 power_state;
234 u32 rds_enabled;
235 u32 frequency;
236 u32 preemphasis;
237 u32 stereo;
238 u32 tune_rnl;
239};
240#endif /* ifndef SI4713_I2C_H */