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] = {
102 .addr
= radio
->client
->addr
,
104 .len
= sizeof(u16
) * READ_REG_NUM
,
109 if (i2c_transfer(radio
->client
->adapter
, msgs
, 1) != 1)
112 radio
->registers
[regnr
] = __be16_to_cpu(buf
[READ_INDEX(regnr
)]);
119 * si470x_set_register - write register
121 int si470x_set_register(struct si470x_device
*radio
, int regnr
)
124 u16 buf
[WRITE_REG_NUM
];
125 struct i2c_msg msgs
[1] = {
127 .addr
= radio
->client
->addr
,
128 .len
= sizeof(u16
) * WRITE_REG_NUM
,
133 for (i
= 0; i
< WRITE_REG_NUM
; i
++)
134 buf
[i
] = __cpu_to_be16(radio
->registers
[WRITE_INDEX(i
)]);
136 if (i2c_transfer(radio
->client
->adapter
, msgs
, 1) != 1)
144 /**************************************************************************
145 * General Driver Functions - ENTIRE REGISTERS
146 **************************************************************************/
149 * si470x_get_all_registers - read entire registers
151 static int si470x_get_all_registers(struct si470x_device
*radio
)
154 u16 buf
[READ_REG_NUM
];
155 struct i2c_msg msgs
[1] = {
157 .addr
= radio
->client
->addr
,
159 .len
= sizeof(u16
) * READ_REG_NUM
,
164 if (i2c_transfer(radio
->client
->adapter
, msgs
, 1) != 1)
167 for (i
= 0; i
< READ_REG_NUM
; i
++)
168 radio
->registers
[i
] = __be16_to_cpu(buf
[READ_INDEX(i
)]);
175 /**************************************************************************
176 * File Operations Interface
177 **************************************************************************/
180 * si470x_fops_open - file open
182 int si470x_fops_open(struct file
*file
)
184 struct si470x_device
*radio
= video_drvdata(file
);
185 int retval
= v4l2_fh_open(file
);
190 if (v4l2_fh_is_singular_file(file
)) {
192 retval
= si470x_start(radio
);
196 /* enable RDS / STC interrupt */
197 radio
->registers
[SYSCONFIG1
] |= SYSCONFIG1_RDSIEN
;
198 radio
->registers
[SYSCONFIG1
] |= SYSCONFIG1_STCIEN
;
199 radio
->registers
[SYSCONFIG1
] &= ~SYSCONFIG1_GPIO2
;
200 radio
->registers
[SYSCONFIG1
] |= 0x1 << 2;
201 retval
= si470x_set_register(radio
, SYSCONFIG1
);
206 v4l2_fh_release(file
);
212 * si470x_fops_release - file release
214 int si470x_fops_release(struct file
*file
)
216 struct si470x_device
*radio
= video_drvdata(file
);
218 if (v4l2_fh_is_singular_file(file
))
222 return v4l2_fh_release(file
);
227 /**************************************************************************
228 * Video4Linux Interface
229 **************************************************************************/
232 * si470x_vidioc_querycap - query device capabilities
234 int si470x_vidioc_querycap(struct file
*file
, void *priv
,
235 struct v4l2_capability
*capability
)
237 strlcpy(capability
->driver
, DRIVER_NAME
, sizeof(capability
->driver
));
238 strlcpy(capability
->card
, DRIVER_CARD
, sizeof(capability
->card
));
239 capability
->device_caps
= V4L2_CAP_HW_FREQ_SEEK
| V4L2_CAP_READWRITE
|
240 V4L2_CAP_TUNER
| V4L2_CAP_RADIO
| V4L2_CAP_RDS_CAPTURE
;
241 capability
->capabilities
= capability
->device_caps
| V4L2_CAP_DEVICE_CAPS
;
248 /**************************************************************************
250 **************************************************************************/
253 * si470x_i2c_interrupt - interrupt handler
255 static irqreturn_t
si470x_i2c_interrupt(int irq
, void *dev_id
)
257 struct si470x_device
*radio
= dev_id
;
259 unsigned char blocknum
;
260 unsigned short bler
; /* rds block errors */
262 unsigned char tmpbuf
[3];
265 /* check Seek/Tune Complete */
266 retval
= si470x_get_register(radio
, STATUSRSSI
);
270 if (radio
->registers
[STATUSRSSI
] & STATUSRSSI_STC
)
271 complete(&radio
->completion
);
274 if ((radio
->registers
[SYSCONFIG1
] & SYSCONFIG1_RDS
) == 0)
277 /* Update RDS registers */
278 for (regnr
= 1; regnr
< RDS_REGISTER_NUM
; regnr
++) {
279 retval
= si470x_get_register(radio
, STATUSRSSI
+ regnr
);
285 if ((radio
->registers
[STATUSRSSI
] & STATUSRSSI_RDSR
) == 0)
286 /* No RDS group ready, better luck next time */
289 for (blocknum
= 0; blocknum
< 4; blocknum
++) {
292 bler
= (radio
->registers
[STATUSRSSI
] &
293 STATUSRSSI_BLERA
) >> 9;
294 rds
= radio
->registers
[RDSA
];
297 bler
= (radio
->registers
[READCHAN
] &
298 READCHAN_BLERB
) >> 14;
299 rds
= radio
->registers
[RDSB
];
302 bler
= (radio
->registers
[READCHAN
] &
303 READCHAN_BLERC
) >> 12;
304 rds
= radio
->registers
[RDSC
];
307 bler
= (radio
->registers
[READCHAN
] &
308 READCHAN_BLERD
) >> 10;
309 rds
= radio
->registers
[RDSD
];
313 /* Fill the V4L2 RDS buffer */
314 put_unaligned_le16(rds
, &tmpbuf
);
315 tmpbuf
[2] = blocknum
; /* offset name */
316 tmpbuf
[2] |= blocknum
<< 3; /* received offset */
317 if (bler
> max_rds_errors
)
318 tmpbuf
[2] |= 0x80; /* uncorrectable errors */
320 tmpbuf
[2] |= 0x40; /* corrected error(s) */
322 /* copy RDS block to internal buffer */
323 memcpy(&radio
->buffer
[radio
->wr_index
], &tmpbuf
, 3);
324 radio
->wr_index
+= 3;
326 /* wrap write pointer */
327 if (radio
->wr_index
>= radio
->buf_size
)
330 /* check for overflow */
331 if (radio
->wr_index
== radio
->rd_index
) {
332 /* increment and wrap read pointer */
333 radio
->rd_index
+= 3;
334 if (radio
->rd_index
>= radio
->buf_size
)
339 if (radio
->wr_index
!= radio
->rd_index
)
340 wake_up_interruptible(&radio
->read_queue
);
348 * si470x_i2c_probe - probe for the device
350 static int si470x_i2c_probe(struct i2c_client
*client
,
351 const struct i2c_device_id
*id
)
353 struct si470x_device
*radio
;
355 unsigned char version_warning
= 0;
357 /* private data allocation and initialization */
358 radio
= kzalloc(sizeof(struct si470x_device
), GFP_KERNEL
);
364 radio
->client
= client
;
365 radio
->band
= 1; /* Default to 76 - 108 MHz */
366 mutex_init(&radio
->lock
);
367 init_completion(&radio
->completion
);
369 /* video device initialization */
370 radio
->videodev
= si470x_viddev_template
;
371 video_set_drvdata(&radio
->videodev
, radio
);
373 /* power up : need 110ms */
374 radio
->registers
[POWERCFG
] = POWERCFG_ENABLE
;
375 if (si470x_set_register(radio
, POWERCFG
) < 0) {
381 /* get device and chip versions */
382 if (si470x_get_all_registers(radio
) < 0) {
386 dev_info(&client
->dev
, "DeviceID=0x%4.4hx ChipID=0x%4.4hx\n",
387 radio
->registers
[DEVICEID
], radio
->registers
[CHIPID
]);
388 if ((radio
->registers
[CHIPID
] & CHIPID_FIRMWARE
) < RADIO_FW_VERSION
) {
389 dev_warn(&client
->dev
,
390 "This driver is known to work with "
391 "firmware version %hu,\n", RADIO_FW_VERSION
);
392 dev_warn(&client
->dev
,
393 "but the device has firmware version %hu.\n",
394 radio
->registers
[CHIPID
] & CHIPID_FIRMWARE
);
398 /* give out version warning */
399 if (version_warning
== 1) {
400 dev_warn(&client
->dev
,
401 "If you have some trouble using this driver,\n");
402 dev_warn(&client
->dev
,
403 "please report to V4L ML at "
404 "linux-media@vger.kernel.org\n");
407 /* set initial frequency */
408 si470x_set_freq(radio
, 87.5 * FREQ_MUL
); /* available in all regions */
410 /* rds buffer allocation */
411 radio
->buf_size
= rds_buf
* 3;
412 radio
->buffer
= kmalloc(radio
->buf_size
, GFP_KERNEL
);
413 if (!radio
->buffer
) {
418 /* rds buffer configuration */
421 init_waitqueue_head(&radio
->read_queue
);
423 retval
= request_threaded_irq(client
->irq
, NULL
, si470x_i2c_interrupt
,
424 IRQF_TRIGGER_FALLING
, DRIVER_NAME
, radio
);
426 dev_err(&client
->dev
, "Failed to register interrupt\n");
430 /* register video device */
431 retval
= video_register_device(&radio
->videodev
, VFL_TYPE_RADIO
,
434 dev_warn(&client
->dev
, "Could not register video device\n");
437 i2c_set_clientdata(client
, radio
);
441 free_irq(client
->irq
, radio
);
443 kfree(radio
->buffer
);
452 * si470x_i2c_remove - remove the device
454 static int si470x_i2c_remove(struct i2c_client
*client
)
456 struct si470x_device
*radio
= i2c_get_clientdata(client
);
458 free_irq(client
->irq
, radio
);
459 video_unregister_device(&radio
->videodev
);
466 #ifdef CONFIG_PM_SLEEP
468 * si470x_i2c_suspend - suspend the device
470 static int si470x_i2c_suspend(struct device
*dev
)
472 struct i2c_client
*client
= to_i2c_client(dev
);
473 struct si470x_device
*radio
= i2c_get_clientdata(client
);
476 radio
->registers
[POWERCFG
] |= POWERCFG_DISABLE
;
477 if (si470x_set_register(radio
, POWERCFG
) < 0)
485 * si470x_i2c_resume - resume the device
487 static int si470x_i2c_resume(struct device
*dev
)
489 struct i2c_client
*client
= to_i2c_client(dev
);
490 struct si470x_device
*radio
= i2c_get_clientdata(client
);
492 /* power up : need 110ms */
493 radio
->registers
[POWERCFG
] |= POWERCFG_ENABLE
;
494 if (si470x_set_register(radio
, POWERCFG
) < 0)
501 static SIMPLE_DEV_PM_OPS(si470x_i2c_pm
, si470x_i2c_suspend
, si470x_i2c_resume
);
506 * si470x_i2c_driver - i2c driver interface
508 static struct i2c_driver si470x_i2c_driver
= {
511 .owner
= THIS_MODULE
,
512 #ifdef CONFIG_PM_SLEEP
513 .pm
= &si470x_i2c_pm
,
516 .probe
= si470x_i2c_probe
,
517 .remove
= si470x_i2c_remove
,
518 .id_table
= si470x_i2c_id
,
521 module_i2c_driver(si470x_i2c_driver
);
523 MODULE_LICENSE("GPL");
524 MODULE_AUTHOR(DRIVER_AUTHOR
);
525 MODULE_DESCRIPTION(DRIVER_DESC
);
526 MODULE_VERSION(DRIVER_VERSION
);