2 * drivers/media/radio/si470x/radio-si470x-i2c.c
4 * I2C driver for radios with Silicon Labs Si470x FM Radio Receivers
6 * Copyright (c) 2009 Samsung Electronics Co.Ltd
7 * Author: Joonyoung Shim <jy0922.shim@samsung.com>
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.
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.
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
25 /* driver definitions */
26 #define DRIVER_AUTHOR "Joonyoung Shim <jy0922.shim@samsung.com>";
27 #define DRIVER_CARD "Silicon Labs Si470x FM Radio Receiver"
28 #define DRIVER_DESC "I2C radio driver for Si470x FM Radio Receivers"
29 #define DRIVER_VERSION "1.0.2"
32 #include <linux/i2c.h>
33 #include <linux/slab.h>
34 #include <linux/delay.h>
35 #include <linux/interrupt.h>
37 #include "radio-si470x.h"
40 /* I2C Device ID List */
41 static const struct i2c_device_id si470x_i2c_id[] = {
44 /* Terminating entry */
47 MODULE_DEVICE_TABLE(i2c, si470x_i2c_id);
51 /**************************************************************************
53 **************************************************************************/
56 static int radio_nr = -1;
57 module_param(radio_nr, int, 0444);
58 MODULE_PARM_DESC(radio_nr, "Radio Nr");
60 /* RDS buffer blocks */
61 static unsigned int rds_buf = 100;
62 module_param(rds_buf, uint, 0444);
63 MODULE_PARM_DESC(rds_buf, "RDS buffer entries: *100*");
65 /* RDS maximum block errors */
66 static unsigned short max_rds_errors = 1;
67 /* 0 means 0 errors requiring correction */
68 /* 1 means 1-2 errors requiring correction (used by original USBRadio.exe) */
69 /* 2 means 3-5 errors requiring correction */
70 /* 3 means 6+ errors or errors in checkword, correction not possible */
71 module_param(max_rds_errors, ushort, 0644);
72 MODULE_PARM_DESC(max_rds_errors, "RDS maximum block errors: *1*");
76 /**************************************************************************
78 **************************************************************************/
80 /* Write starts with the upper byte of register 0x02 */
81 #define WRITE_REG_NUM 8
82 #define WRITE_INDEX(i) (i + 0x02)
84 /* Read starts with the upper byte of register 0x0a */
85 #define READ_REG_NUM RADIO_REGISTER_NUM
86 #define READ_INDEX(i) ((i + RADIO_REGISTER_NUM - 0x0a) % READ_REG_NUM)
90 /**************************************************************************
91 * General Driver Functions - REGISTERs
92 **************************************************************************/
95 * si470x_get_register - read register
97 int si470x_get_register(struct si470x_device *radio, int regnr)
99 u16 buf[READ_REG_NUM];
100 struct i2c_msg msgs[1] = {
101 { radio->client->addr, I2C_M_RD, sizeof(u16) * READ_REG_NUM,
105 if (i2c_transfer(radio->client->adapter, msgs, 1) != 1)
108 radio->registers[regnr] = __be16_to_cpu(buf[READ_INDEX(regnr)]);
115 * si470x_set_register - write register
117 int si470x_set_register(struct si470x_device *radio, int regnr)
120 u16 buf[WRITE_REG_NUM];
121 struct i2c_msg msgs[1] = {
122 { radio->client->addr, 0, sizeof(u16) * WRITE_REG_NUM,
126 for (i = 0; i < WRITE_REG_NUM; i++)
127 buf[i] = __cpu_to_be16(radio->registers[WRITE_INDEX(i)]);
129 if (i2c_transfer(radio->client->adapter, msgs, 1) != 1)
137 /**************************************************************************
138 * General Driver Functions - ENTIRE REGISTERS
139 **************************************************************************/
142 * si470x_get_all_registers - read entire registers
144 static int si470x_get_all_registers(struct si470x_device *radio)
147 u16 buf[READ_REG_NUM];
148 struct i2c_msg msgs[1] = {
149 { radio->client->addr, I2C_M_RD, sizeof(u16) * READ_REG_NUM,
153 if (i2c_transfer(radio->client->adapter, msgs, 1) != 1)
156 for (i = 0; i < READ_REG_NUM; i++)
157 radio->registers[i] = __be16_to_cpu(buf[READ_INDEX(i)]);
164 /**************************************************************************
165 * File Operations Interface
166 **************************************************************************/
169 * si470x_fops_open - file open
171 int si470x_fops_open(struct file *file)
173 struct si470x_device *radio = video_drvdata(file);
174 int retval = v4l2_fh_open(file);
179 if (v4l2_fh_is_singular_file(file)) {
181 retval = si470x_start(radio);
185 /* enable RDS / STC interrupt */
186 radio->registers[SYSCONFIG1] |= SYSCONFIG1_RDSIEN;
187 radio->registers[SYSCONFIG1] |= SYSCONFIG1_STCIEN;
188 radio->registers[SYSCONFIG1] &= ~SYSCONFIG1_GPIO2;
189 radio->registers[SYSCONFIG1] |= 0x1 << 2;
190 retval = si470x_set_register(radio, SYSCONFIG1);
195 v4l2_fh_release(file);
201 * si470x_fops_release - file release
203 int si470x_fops_release(struct file *file)
205 struct si470x_device *radio = video_drvdata(file);
207 if (v4l2_fh_is_singular_file(file))
211 return v4l2_fh_release(file);
216 /**************************************************************************
217 * Video4Linux Interface
218 **************************************************************************/
221 * si470x_vidioc_querycap - query device capabilities
223 int si470x_vidioc_querycap(struct file *file, void *priv,
224 struct v4l2_capability *capability)
226 strlcpy(capability->driver, DRIVER_NAME, sizeof(capability->driver));
227 strlcpy(capability->card, DRIVER_CARD, sizeof(capability->card));
228 capability->device_caps = V4L2_CAP_HW_FREQ_SEEK | V4L2_CAP_READWRITE |
229 V4L2_CAP_TUNER | V4L2_CAP_RADIO | V4L2_CAP_RDS_CAPTURE;
230 capability->capabilities = capability->device_caps | V4L2_CAP_DEVICE_CAPS;
237 /**************************************************************************
239 **************************************************************************/
242 * si470x_i2c_interrupt - interrupt handler
244 static irqreturn_t si470x_i2c_interrupt(int irq, void *dev_id)
246 struct si470x_device *radio = dev_id;
248 unsigned char blocknum;
249 unsigned short bler; /* rds block errors */
251 unsigned char tmpbuf[3];
254 /* check Seek/Tune Complete */
255 retval = si470x_get_register(radio, STATUSRSSI);
259 if (radio->registers[STATUSRSSI] & STATUSRSSI_STC)
260 complete(&radio->completion);
263 if ((radio->registers[SYSCONFIG1] & SYSCONFIG1_RDS) == 0)
266 /* Update RDS registers */
267 for (regnr = 1; regnr < RDS_REGISTER_NUM; regnr++) {
268 retval = si470x_get_register(radio, STATUSRSSI + regnr);
274 if ((radio->registers[STATUSRSSI] & STATUSRSSI_RDSR) == 0)
275 /* No RDS group ready, better luck next time */
278 for (blocknum = 0; blocknum < 4; blocknum++) {
281 bler = (radio->registers[STATUSRSSI] &
282 STATUSRSSI_BLERA) >> 9;
283 rds = radio->registers[RDSA];
286 bler = (radio->registers[READCHAN] &
287 READCHAN_BLERB) >> 14;
288 rds = radio->registers[RDSB];
291 bler = (radio->registers[READCHAN] &
292 READCHAN_BLERC) >> 12;
293 rds = radio->registers[RDSC];
296 bler = (radio->registers[READCHAN] &
297 READCHAN_BLERD) >> 10;
298 rds = radio->registers[RDSD];
302 /* Fill the V4L2 RDS buffer */
303 put_unaligned_le16(rds, &tmpbuf);
304 tmpbuf[2] = blocknum; /* offset name */
305 tmpbuf[2] |= blocknum << 3; /* received offset */
306 if (bler > max_rds_errors)
307 tmpbuf[2] |= 0x80; /* uncorrectable errors */
309 tmpbuf[2] |= 0x40; /* corrected error(s) */
311 /* copy RDS block to internal buffer */
312 memcpy(&radio->buffer[radio->wr_index], &tmpbuf, 3);
313 radio->wr_index += 3;
315 /* wrap write pointer */
316 if (radio->wr_index >= radio->buf_size)
319 /* check for overflow */
320 if (radio->wr_index == radio->rd_index) {
321 /* increment and wrap read pointer */
322 radio->rd_index += 3;
323 if (radio->rd_index >= radio->buf_size)
328 if (radio->wr_index != radio->rd_index)
329 wake_up_interruptible(&radio->read_queue);
337 * si470x_i2c_probe - probe for the device
339 static int __devinit si470x_i2c_probe(struct i2c_client *client,
340 const struct i2c_device_id *id)
342 struct si470x_device *radio;
344 unsigned char version_warning = 0;
346 /* private data allocation and initialization */
347 radio = kzalloc(sizeof(struct si470x_device), GFP_KERNEL);
353 radio->client = client;
354 radio->band = 1; /* Default to 76 - 108 MHz */
355 mutex_init(&radio->lock);
356 init_completion(&radio->completion);
358 /* video device initialization */
359 radio->videodev = si470x_viddev_template;
360 video_set_drvdata(&radio->videodev, radio);
362 /* power up : need 110ms */
363 radio->registers[POWERCFG] = POWERCFG_ENABLE;
364 if (si470x_set_register(radio, POWERCFG) < 0) {
370 /* get device and chip versions */
371 if (si470x_get_all_registers(radio) < 0) {
375 dev_info(&client->dev, "DeviceID=0x%4.4hx ChipID=0x%4.4hx\n",
376 radio->registers[DEVICEID], radio->registers[CHIPID]);
377 if ((radio->registers[CHIPID] & CHIPID_FIRMWARE) < RADIO_FW_VERSION) {
378 dev_warn(&client->dev,
379 "This driver is known to work with "
380 "firmware version %hu,\n", RADIO_FW_VERSION);
381 dev_warn(&client->dev,
382 "but the device has firmware version %hu.\n",
383 radio->registers[CHIPID] & CHIPID_FIRMWARE);
387 /* give out version warning */
388 if (version_warning == 1) {
389 dev_warn(&client->dev,
390 "If you have some trouble using this driver,\n");
391 dev_warn(&client->dev,
392 "please report to V4L ML at "
393 "linux-media@vger.kernel.org\n");
396 /* set initial frequency */
397 si470x_set_freq(radio, 87.5 * FREQ_MUL); /* available in all regions */
399 /* rds buffer allocation */
400 radio->buf_size = rds_buf * 3;
401 radio->buffer = kmalloc(radio->buf_size, GFP_KERNEL);
402 if (!radio->buffer) {
407 /* rds buffer configuration */
410 init_waitqueue_head(&radio->read_queue);
412 retval = request_threaded_irq(client->irq, NULL, si470x_i2c_interrupt,
413 IRQF_TRIGGER_FALLING, DRIVER_NAME, radio);
415 dev_err(&client->dev, "Failed to register interrupt\n");
419 /* register video device */
420 retval = video_register_device(&radio->videodev, VFL_TYPE_RADIO,
423 dev_warn(&client->dev, "Could not register video device\n");
426 i2c_set_clientdata(client, radio);
430 free_irq(client->irq, radio);
432 kfree(radio->buffer);
441 * si470x_i2c_remove - remove the device
443 static __devexit int si470x_i2c_remove(struct i2c_client *client)
445 struct si470x_device *radio = i2c_get_clientdata(client);
447 free_irq(client->irq, radio);
448 video_unregister_device(&radio->videodev);
457 * si470x_i2c_suspend - suspend the device
459 static int si470x_i2c_suspend(struct device *dev)
461 struct i2c_client *client = to_i2c_client(dev);
462 struct si470x_device *radio = i2c_get_clientdata(client);
465 radio->registers[POWERCFG] |= POWERCFG_DISABLE;
466 if (si470x_set_register(radio, POWERCFG) < 0)
474 * si470x_i2c_resume - resume the device
476 static int si470x_i2c_resume(struct device *dev)
478 struct i2c_client *client = to_i2c_client(dev);
479 struct si470x_device *radio = i2c_get_clientdata(client);
481 /* power up : need 110ms */
482 radio->registers[POWERCFG] |= POWERCFG_ENABLE;
483 if (si470x_set_register(radio, POWERCFG) < 0)
490 static SIMPLE_DEV_PM_OPS(si470x_i2c_pm, si470x_i2c_suspend, si470x_i2c_resume);
495 * si470x_i2c_driver - i2c driver interface
497 static struct i2c_driver si470x_i2c_driver = {
500 .owner = THIS_MODULE,
502 .pm = &si470x_i2c_pm,
505 .probe = si470x_i2c_probe,
506 .remove = __devexit_p(si470x_i2c_remove),
507 .id_table = si470x_i2c_id,
510 module_i2c_driver(si470x_i2c_driver);
512 MODULE_LICENSE("GPL");
513 MODULE_AUTHOR(DRIVER_AUTHOR);
514 MODULE_DESCRIPTION(DRIVER_DESC);
515 MODULE_VERSION(DRIVER_VERSION);