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_KERNEL_VERSION KERNEL_VERSION(1, 0, 1)
28 #define DRIVER_CARD "Silicon Labs Si470x FM Radio Receiver"
29 #define DRIVER_DESC "I2C radio driver for Si470x FM Radio Receivers"
30 #define DRIVER_VERSION "1.0.1"
33 #include <linux/i2c.h>
34 #include <linux/slab.h>
35 #include <linux/delay.h>
36 #include <linux/interrupt.h>
38 #include "radio-si470x.h"
41 /* I2C Device ID List */
42 static const struct i2c_device_id si470x_i2c_id
[] = {
45 /* Terminating entry */
48 MODULE_DEVICE_TABLE(i2c
, si470x_i2c_id
);
52 /**************************************************************************
54 **************************************************************************/
57 static int radio_nr
= -1;
58 module_param(radio_nr
, int, 0444);
59 MODULE_PARM_DESC(radio_nr
, "Radio Nr");
61 /* RDS buffer blocks */
62 static unsigned int rds_buf
= 100;
63 module_param(rds_buf
, uint
, 0444);
64 MODULE_PARM_DESC(rds_buf
, "RDS buffer entries: *100*");
66 /* RDS maximum block errors */
67 static unsigned short max_rds_errors
= 1;
68 /* 0 means 0 errors requiring correction */
69 /* 1 means 1-2 errors requiring correction (used by original USBRadio.exe) */
70 /* 2 means 3-5 errors requiring correction */
71 /* 3 means 6+ errors or errors in checkword, correction not possible */
72 module_param(max_rds_errors
, ushort
, 0644);
73 MODULE_PARM_DESC(max_rds_errors
, "RDS maximum block errors: *1*");
77 /**************************************************************************
79 **************************************************************************/
81 /* Write starts with the upper byte of register 0x02 */
82 #define WRITE_REG_NUM 8
83 #define WRITE_INDEX(i) (i + 0x02)
85 /* Read starts with the upper byte of register 0x0a */
86 #define READ_REG_NUM RADIO_REGISTER_NUM
87 #define READ_INDEX(i) ((i + RADIO_REGISTER_NUM - 0x0a) % READ_REG_NUM)
91 /**************************************************************************
92 * General Driver Functions - REGISTERs
93 **************************************************************************/
96 * si470x_get_register - read register
98 int si470x_get_register(struct si470x_device
*radio
, int regnr
)
100 u16 buf
[READ_REG_NUM
];
101 struct i2c_msg msgs
[1] = {
102 { radio
->client
->addr
, I2C_M_RD
, sizeof(u16
) * READ_REG_NUM
,
106 if (i2c_transfer(radio
->client
->adapter
, msgs
, 1) != 1)
109 radio
->registers
[regnr
] = __be16_to_cpu(buf
[READ_INDEX(regnr
)]);
116 * si470x_set_register - write register
118 int si470x_set_register(struct si470x_device
*radio
, int regnr
)
121 u16 buf
[WRITE_REG_NUM
];
122 struct i2c_msg msgs
[1] = {
123 { radio
->client
->addr
, 0, sizeof(u16
) * WRITE_REG_NUM
,
127 for (i
= 0; i
< WRITE_REG_NUM
; i
++)
128 buf
[i
] = __cpu_to_be16(radio
->registers
[WRITE_INDEX(i
)]);
130 if (i2c_transfer(radio
->client
->adapter
, msgs
, 1) != 1)
138 /**************************************************************************
139 * General Driver Functions - ENTIRE REGISTERS
140 **************************************************************************/
143 * si470x_get_all_registers - read entire registers
145 static int si470x_get_all_registers(struct si470x_device
*radio
)
148 u16 buf
[READ_REG_NUM
];
149 struct i2c_msg msgs
[1] = {
150 { radio
->client
->addr
, I2C_M_RD
, sizeof(u16
) * READ_REG_NUM
,
154 if (i2c_transfer(radio
->client
->adapter
, msgs
, 1) != 1)
157 for (i
= 0; i
< READ_REG_NUM
; i
++)
158 radio
->registers
[i
] = __be16_to_cpu(buf
[READ_INDEX(i
)]);
165 /**************************************************************************
166 * General Driver Functions - DISCONNECT_CHECK
167 **************************************************************************/
170 * si470x_disconnect_check - check whether radio disconnects
172 int si470x_disconnect_check(struct si470x_device
*radio
)
179 /**************************************************************************
180 * File Operations Interface
181 **************************************************************************/
184 * si470x_fops_open - file open
186 int si470x_fops_open(struct file
*file
)
188 struct si470x_device
*radio
= video_drvdata(file
);
191 mutex_lock(&radio
->lock
);
194 if (radio
->users
== 1) {
196 retval
= si470x_start(radio
);
200 /* enable RDS / STC interrupt */
201 radio
->registers
[SYSCONFIG1
] |= SYSCONFIG1_RDSIEN
;
202 radio
->registers
[SYSCONFIG1
] |= SYSCONFIG1_STCIEN
;
203 radio
->registers
[SYSCONFIG1
] &= ~SYSCONFIG1_GPIO2
;
204 radio
->registers
[SYSCONFIG1
] |= 0x1 << 2;
205 retval
= si470x_set_register(radio
, SYSCONFIG1
);
209 mutex_unlock(&radio
->lock
);
215 * si470x_fops_release - file release
217 int si470x_fops_release(struct file
*file
)
219 struct si470x_device
*radio
= video_drvdata(file
);
226 mutex_lock(&radio
->lock
);
228 if (radio
->users
== 0)
230 retval
= si470x_stop(radio
);
232 mutex_unlock(&radio
->lock
);
239 /**************************************************************************
240 * Video4Linux Interface
241 **************************************************************************/
244 * si470x_vidioc_querycap - query device capabilities
246 int si470x_vidioc_querycap(struct file
*file
, void *priv
,
247 struct v4l2_capability
*capability
)
249 strlcpy(capability
->driver
, DRIVER_NAME
, sizeof(capability
->driver
));
250 strlcpy(capability
->card
, DRIVER_CARD
, sizeof(capability
->card
));
251 capability
->version
= DRIVER_KERNEL_VERSION
;
252 capability
->capabilities
= V4L2_CAP_HW_FREQ_SEEK
|
253 V4L2_CAP_TUNER
| V4L2_CAP_RADIO
;
260 /**************************************************************************
262 **************************************************************************/
265 * si470x_i2c_interrupt - interrupt handler
267 static irqreturn_t
si470x_i2c_interrupt(int irq
, void *dev_id
)
269 struct si470x_device
*radio
= dev_id
;
271 unsigned char blocknum
;
272 unsigned short bler
; /* rds block errors */
274 unsigned char tmpbuf
[3];
277 /* check Seek/Tune Complete */
278 retval
= si470x_get_register(radio
, STATUSRSSI
);
282 if (radio
->registers
[STATUSRSSI
] & STATUSRSSI_STC
)
283 complete(&radio
->completion
);
286 if ((radio
->registers
[SYSCONFIG1
] & SYSCONFIG1_RDS
) == 0)
289 /* Update RDS registers */
290 for (regnr
= 1; regnr
< RDS_REGISTER_NUM
; regnr
++) {
291 retval
= si470x_get_register(radio
, STATUSRSSI
+ regnr
);
297 if ((radio
->registers
[STATUSRSSI
] & STATUSRSSI_RDSR
) == 0)
298 /* No RDS group ready, better luck next time */
301 for (blocknum
= 0; blocknum
< 4; blocknum
++) {
304 bler
= (radio
->registers
[STATUSRSSI
] &
305 STATUSRSSI_BLERA
) >> 9;
306 rds
= radio
->registers
[RDSA
];
309 bler
= (radio
->registers
[READCHAN
] &
310 READCHAN_BLERB
) >> 14;
311 rds
= radio
->registers
[RDSB
];
314 bler
= (radio
->registers
[READCHAN
] &
315 READCHAN_BLERC
) >> 12;
316 rds
= radio
->registers
[RDSC
];
319 bler
= (radio
->registers
[READCHAN
] &
320 READCHAN_BLERD
) >> 10;
321 rds
= radio
->registers
[RDSD
];
325 /* Fill the V4L2 RDS buffer */
326 put_unaligned_le16(rds
, &tmpbuf
);
327 tmpbuf
[2] = blocknum
; /* offset name */
328 tmpbuf
[2] |= blocknum
<< 3; /* received offset */
329 if (bler
> max_rds_errors
)
330 tmpbuf
[2] |= 0x80; /* uncorrectable errors */
332 tmpbuf
[2] |= 0x40; /* corrected error(s) */
334 /* copy RDS block to internal buffer */
335 memcpy(&radio
->buffer
[radio
->wr_index
], &tmpbuf
, 3);
336 radio
->wr_index
+= 3;
338 /* wrap write pointer */
339 if (radio
->wr_index
>= radio
->buf_size
)
342 /* check for overflow */
343 if (radio
->wr_index
== radio
->rd_index
) {
344 /* increment and wrap read pointer */
345 radio
->rd_index
+= 3;
346 if (radio
->rd_index
>= radio
->buf_size
)
351 if (radio
->wr_index
!= radio
->rd_index
)
352 wake_up_interruptible(&radio
->read_queue
);
360 * si470x_i2c_probe - probe for the device
362 static int __devinit
si470x_i2c_probe(struct i2c_client
*client
,
363 const struct i2c_device_id
*id
)
365 struct si470x_device
*radio
;
367 unsigned char version_warning
= 0;
369 /* private data allocation and initialization */
370 radio
= kzalloc(sizeof(struct si470x_device
), GFP_KERNEL
);
377 radio
->client
= client
;
378 mutex_init(&radio
->lock
);
380 /* video device allocation and initialization */
381 radio
->videodev
= video_device_alloc();
382 if (!radio
->videodev
) {
386 memcpy(radio
->videodev
, &si470x_viddev_template
,
387 sizeof(si470x_viddev_template
));
388 video_set_drvdata(radio
->videodev
, radio
);
390 /* power up : need 110ms */
391 radio
->registers
[POWERCFG
] = POWERCFG_ENABLE
;
392 if (si470x_set_register(radio
, POWERCFG
) < 0) {
398 /* get device and chip versions */
399 if (si470x_get_all_registers(radio
) < 0) {
403 dev_info(&client
->dev
, "DeviceID=0x%4.4hx ChipID=0x%4.4hx\n",
404 radio
->registers
[DEVICEID
], radio
->registers
[CHIPID
]);
405 if ((radio
->registers
[CHIPID
] & CHIPID_FIRMWARE
) < RADIO_FW_VERSION
) {
406 dev_warn(&client
->dev
,
407 "This driver is known to work with "
408 "firmware version %hu,\n", RADIO_FW_VERSION
);
409 dev_warn(&client
->dev
,
410 "but the device has firmware version %hu.\n",
411 radio
->registers
[CHIPID
] & CHIPID_FIRMWARE
);
415 /* give out version warning */
416 if (version_warning
== 1) {
417 dev_warn(&client
->dev
,
418 "If you have some trouble using this driver,\n");
419 dev_warn(&client
->dev
,
420 "please report to V4L ML at "
421 "linux-media@vger.kernel.org\n");
424 /* set initial frequency */
425 si470x_set_freq(radio
, 87.5 * FREQ_MUL
); /* available in all regions */
427 /* rds buffer allocation */
428 radio
->buf_size
= rds_buf
* 3;
429 radio
->buffer
= kmalloc(radio
->buf_size
, GFP_KERNEL
);
430 if (!radio
->buffer
) {
435 /* rds buffer configuration */
438 init_waitqueue_head(&radio
->read_queue
);
440 /* mark Seek/Tune Complete Interrupt enabled */
441 radio
->stci_enabled
= true;
442 init_completion(&radio
->completion
);
444 retval
= request_threaded_irq(client
->irq
, NULL
, si470x_i2c_interrupt
,
445 IRQF_TRIGGER_FALLING
, DRIVER_NAME
, radio
);
447 dev_err(&client
->dev
, "Failed to register interrupt\n");
451 /* register video device */
452 retval
= video_register_device(radio
->videodev
, VFL_TYPE_RADIO
,
455 dev_warn(&client
->dev
, "Could not register video device\n");
458 i2c_set_clientdata(client
, radio
);
462 free_irq(client
->irq
, radio
);
464 kfree(radio
->buffer
);
466 video_device_release(radio
->videodev
);
475 * si470x_i2c_remove - remove the device
477 static __devexit
int si470x_i2c_remove(struct i2c_client
*client
)
479 struct si470x_device
*radio
= i2c_get_clientdata(client
);
481 free_irq(client
->irq
, radio
);
482 video_unregister_device(radio
->videodev
);
491 * si470x_i2c_suspend - suspend the device
493 static int si470x_i2c_suspend(struct device
*dev
)
495 struct i2c_client
*client
= to_i2c_client(dev
);
496 struct si470x_device
*radio
= i2c_get_clientdata(client
);
499 radio
->registers
[POWERCFG
] |= POWERCFG_DISABLE
;
500 if (si470x_set_register(radio
, POWERCFG
) < 0)
508 * si470x_i2c_resume - resume the device
510 static int si470x_i2c_resume(struct device
*dev
)
512 struct i2c_client
*client
= to_i2c_client(dev
);
513 struct si470x_device
*radio
= i2c_get_clientdata(client
);
515 /* power up : need 110ms */
516 radio
->registers
[POWERCFG
] |= POWERCFG_ENABLE
;
517 if (si470x_set_register(radio
, POWERCFG
) < 0)
524 static SIMPLE_DEV_PM_OPS(si470x_i2c_pm
, si470x_i2c_suspend
, si470x_i2c_resume
);
529 * si470x_i2c_driver - i2c driver interface
531 static struct i2c_driver si470x_i2c_driver
= {
534 .owner
= THIS_MODULE
,
536 .pm
= &si470x_i2c_pm
,
539 .probe
= si470x_i2c_probe
,
540 .remove
= __devexit_p(si470x_i2c_remove
),
541 .id_table
= si470x_i2c_id
,
546 /**************************************************************************
548 **************************************************************************/
551 * si470x_i2c_init - module init
553 static int __init
si470x_i2c_init(void)
555 printk(KERN_INFO DRIVER_DESC
", Version " DRIVER_VERSION
"\n");
556 return i2c_add_driver(&si470x_i2c_driver
);
561 * si470x_i2c_exit - module exit
563 static void __exit
si470x_i2c_exit(void)
565 i2c_del_driver(&si470x_i2c_driver
);
569 module_init(si470x_i2c_init
);
570 module_exit(si470x_i2c_exit
);
572 MODULE_LICENSE("GPL");
573 MODULE_AUTHOR(DRIVER_AUTHOR
);
574 MODULE_DESCRIPTION(DRIVER_DESC
);
575 MODULE_VERSION(DRIVER_VERSION
);