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.
21 /* driver definitions */
22 #define DRIVER_AUTHOR "Joonyoung Shim <jy0922.shim@samsung.com>";
23 #define DRIVER_CARD "Silicon Labs Si470x FM Radio Receiver"
24 #define DRIVER_DESC "I2C radio driver for Si470x FM Radio Receivers"
25 #define DRIVER_VERSION "1.0.2"
28 #include <linux/i2c.h>
29 #include <linux/slab.h>
30 #include <linux/delay.h>
31 #include <linux/interrupt.h>
33 #include "radio-si470x.h"
36 /* I2C Device ID List */
37 static const struct i2c_device_id si470x_i2c_id
[] = {
40 /* Terminating entry */
43 MODULE_DEVICE_TABLE(i2c
, si470x_i2c_id
);
47 /**************************************************************************
49 **************************************************************************/
52 static int radio_nr
= -1;
53 module_param(radio_nr
, int, 0444);
54 MODULE_PARM_DESC(radio_nr
, "Radio Nr");
56 /* RDS buffer blocks */
57 static unsigned int rds_buf
= 100;
58 module_param(rds_buf
, uint
, 0444);
59 MODULE_PARM_DESC(rds_buf
, "RDS buffer entries: *100*");
61 /* RDS maximum block errors */
62 static unsigned short max_rds_errors
= 1;
63 /* 0 means 0 errors requiring correction */
64 /* 1 means 1-2 errors requiring correction (used by original USBRadio.exe) */
65 /* 2 means 3-5 errors requiring correction */
66 /* 3 means 6+ errors or errors in checkword, correction not possible */
67 module_param(max_rds_errors
, ushort
, 0644);
68 MODULE_PARM_DESC(max_rds_errors
, "RDS maximum block errors: *1*");
72 /**************************************************************************
74 **************************************************************************/
76 /* Write starts with the upper byte of register 0x02 */
77 #define WRITE_REG_NUM 8
78 #define WRITE_INDEX(i) (i + 0x02)
80 /* Read starts with the upper byte of register 0x0a */
81 #define READ_REG_NUM RADIO_REGISTER_NUM
82 #define READ_INDEX(i) ((i + RADIO_REGISTER_NUM - 0x0a) % READ_REG_NUM)
86 /**************************************************************************
87 * General Driver Functions - REGISTERs
88 **************************************************************************/
91 * si470x_get_register - read register
93 int si470x_get_register(struct si470x_device
*radio
, int regnr
)
95 u16 buf
[READ_REG_NUM
];
96 struct i2c_msg msgs
[1] = {
98 .addr
= radio
->client
->addr
,
100 .len
= 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] = {
123 .addr
= radio
->client
->addr
,
124 .len
= sizeof(u16
) * WRITE_REG_NUM
,
129 for (i
= 0; i
< WRITE_REG_NUM
; i
++)
130 buf
[i
] = __cpu_to_be16(radio
->registers
[WRITE_INDEX(i
)]);
132 if (i2c_transfer(radio
->client
->adapter
, msgs
, 1) != 1)
140 /**************************************************************************
141 * General Driver Functions - ENTIRE REGISTERS
142 **************************************************************************/
145 * si470x_get_all_registers - read entire registers
147 static int si470x_get_all_registers(struct si470x_device
*radio
)
150 u16 buf
[READ_REG_NUM
];
151 struct i2c_msg msgs
[1] = {
153 .addr
= radio
->client
->addr
,
155 .len
= sizeof(u16
) * READ_REG_NUM
,
160 if (i2c_transfer(radio
->client
->adapter
, msgs
, 1) != 1)
163 for (i
= 0; i
< READ_REG_NUM
; i
++)
164 radio
->registers
[i
] = __be16_to_cpu(buf
[READ_INDEX(i
)]);
171 /**************************************************************************
172 * File Operations Interface
173 **************************************************************************/
176 * si470x_fops_open - file open
178 int si470x_fops_open(struct file
*file
)
180 struct si470x_device
*radio
= video_drvdata(file
);
181 int retval
= v4l2_fh_open(file
);
186 if (v4l2_fh_is_singular_file(file
)) {
188 retval
= si470x_start(radio
);
192 /* enable RDS / STC interrupt */
193 radio
->registers
[SYSCONFIG1
] |= SYSCONFIG1_RDSIEN
;
194 radio
->registers
[SYSCONFIG1
] |= SYSCONFIG1_STCIEN
;
195 radio
->registers
[SYSCONFIG1
] &= ~SYSCONFIG1_GPIO2
;
196 radio
->registers
[SYSCONFIG1
] |= 0x1 << 2;
197 retval
= si470x_set_register(radio
, SYSCONFIG1
);
202 v4l2_fh_release(file
);
208 * si470x_fops_release - file release
210 int si470x_fops_release(struct file
*file
)
212 struct si470x_device
*radio
= video_drvdata(file
);
214 if (v4l2_fh_is_singular_file(file
))
218 return v4l2_fh_release(file
);
223 /**************************************************************************
224 * Video4Linux Interface
225 **************************************************************************/
228 * si470x_vidioc_querycap - query device capabilities
230 int si470x_vidioc_querycap(struct file
*file
, void *priv
,
231 struct v4l2_capability
*capability
)
233 strlcpy(capability
->driver
, DRIVER_NAME
, sizeof(capability
->driver
));
234 strlcpy(capability
->card
, DRIVER_CARD
, sizeof(capability
->card
));
235 capability
->device_caps
= V4L2_CAP_HW_FREQ_SEEK
| V4L2_CAP_READWRITE
|
236 V4L2_CAP_TUNER
| V4L2_CAP_RADIO
| V4L2_CAP_RDS_CAPTURE
;
237 capability
->capabilities
= capability
->device_caps
| V4L2_CAP_DEVICE_CAPS
;
244 /**************************************************************************
246 **************************************************************************/
249 * si470x_i2c_interrupt - interrupt handler
251 static irqreturn_t
si470x_i2c_interrupt(int irq
, void *dev_id
)
253 struct si470x_device
*radio
= dev_id
;
255 unsigned char blocknum
;
256 unsigned short bler
; /* rds block errors */
258 unsigned char tmpbuf
[3];
261 /* check Seek/Tune Complete */
262 retval
= si470x_get_register(radio
, STATUSRSSI
);
266 if (radio
->registers
[STATUSRSSI
] & STATUSRSSI_STC
)
267 complete(&radio
->completion
);
270 if ((radio
->registers
[SYSCONFIG1
] & SYSCONFIG1_RDS
) == 0)
273 /* Update RDS registers */
274 for (regnr
= 1; regnr
< RDS_REGISTER_NUM
; regnr
++) {
275 retval
= si470x_get_register(radio
, STATUSRSSI
+ regnr
);
281 if ((radio
->registers
[STATUSRSSI
] & STATUSRSSI_RDSR
) == 0)
282 /* No RDS group ready, better luck next time */
285 for (blocknum
= 0; blocknum
< 4; blocknum
++) {
288 bler
= (radio
->registers
[STATUSRSSI
] &
289 STATUSRSSI_BLERA
) >> 9;
290 rds
= radio
->registers
[RDSA
];
293 bler
= (radio
->registers
[READCHAN
] &
294 READCHAN_BLERB
) >> 14;
295 rds
= radio
->registers
[RDSB
];
298 bler
= (radio
->registers
[READCHAN
] &
299 READCHAN_BLERC
) >> 12;
300 rds
= radio
->registers
[RDSC
];
303 bler
= (radio
->registers
[READCHAN
] &
304 READCHAN_BLERD
) >> 10;
305 rds
= radio
->registers
[RDSD
];
309 /* Fill the V4L2 RDS buffer */
310 put_unaligned_le16(rds
, &tmpbuf
);
311 tmpbuf
[2] = blocknum
; /* offset name */
312 tmpbuf
[2] |= blocknum
<< 3; /* received offset */
313 if (bler
> max_rds_errors
)
314 tmpbuf
[2] |= 0x80; /* uncorrectable errors */
316 tmpbuf
[2] |= 0x40; /* corrected error(s) */
318 /* copy RDS block to internal buffer */
319 memcpy(&radio
->buffer
[radio
->wr_index
], &tmpbuf
, 3);
320 radio
->wr_index
+= 3;
322 /* wrap write pointer */
323 if (radio
->wr_index
>= radio
->buf_size
)
326 /* check for overflow */
327 if (radio
->wr_index
== radio
->rd_index
) {
328 /* increment and wrap read pointer */
329 radio
->rd_index
+= 3;
330 if (radio
->rd_index
>= radio
->buf_size
)
335 if (radio
->wr_index
!= radio
->rd_index
)
336 wake_up_interruptible(&radio
->read_queue
);
344 * si470x_i2c_probe - probe for the device
346 static int si470x_i2c_probe(struct i2c_client
*client
,
347 const struct i2c_device_id
*id
)
349 struct si470x_device
*radio
;
351 unsigned char version_warning
= 0;
353 /* private data allocation and initialization */
354 radio
= kzalloc(sizeof(struct si470x_device
), GFP_KERNEL
);
360 radio
->client
= client
;
361 radio
->band
= 1; /* Default to 76 - 108 MHz */
362 mutex_init(&radio
->lock
);
363 init_completion(&radio
->completion
);
365 /* video device initialization */
366 radio
->videodev
= si470x_viddev_template
;
367 video_set_drvdata(&radio
->videodev
, radio
);
369 /* power up : need 110ms */
370 radio
->registers
[POWERCFG
] = POWERCFG_ENABLE
;
371 if (si470x_set_register(radio
, POWERCFG
) < 0) {
377 /* get device and chip versions */
378 if (si470x_get_all_registers(radio
) < 0) {
382 dev_info(&client
->dev
, "DeviceID=0x%4.4hx ChipID=0x%4.4hx\n",
383 radio
->registers
[DEVICEID
], radio
->registers
[SI_CHIPID
]);
384 if ((radio
->registers
[SI_CHIPID
] & SI_CHIPID_FIRMWARE
) < RADIO_FW_VERSION
) {
385 dev_warn(&client
->dev
,
386 "This driver is known to work with firmware version %hu,\n",
388 dev_warn(&client
->dev
,
389 "but the device has firmware version %hu.\n",
390 radio
->registers
[SI_CHIPID
] & SI_CHIPID_FIRMWARE
);
394 /* give out version warning */
395 if (version_warning
== 1) {
396 dev_warn(&client
->dev
,
397 "If you have some trouble using this driver,\n");
398 dev_warn(&client
->dev
,
399 "please report to V4L ML at linux-media@vger.kernel.org\n");
402 /* set initial frequency */
403 si470x_set_freq(radio
, 87.5 * FREQ_MUL
); /* available in all regions */
405 /* rds buffer allocation */
406 radio
->buf_size
= rds_buf
* 3;
407 radio
->buffer
= kmalloc(radio
->buf_size
, GFP_KERNEL
);
408 if (!radio
->buffer
) {
413 /* rds buffer configuration */
416 init_waitqueue_head(&radio
->read_queue
);
418 retval
= request_threaded_irq(client
->irq
, NULL
, si470x_i2c_interrupt
,
419 IRQF_TRIGGER_FALLING
| IRQF_ONESHOT
, DRIVER_NAME
,
422 dev_err(&client
->dev
, "Failed to register interrupt\n");
426 /* register video device */
427 retval
= video_register_device(&radio
->videodev
, VFL_TYPE_RADIO
,
430 dev_warn(&client
->dev
, "Could not register video device\n");
433 i2c_set_clientdata(client
, radio
);
437 free_irq(client
->irq
, radio
);
439 kfree(radio
->buffer
);
448 * si470x_i2c_remove - remove the device
450 static int si470x_i2c_remove(struct i2c_client
*client
)
452 struct si470x_device
*radio
= i2c_get_clientdata(client
);
454 free_irq(client
->irq
, radio
);
455 video_unregister_device(&radio
->videodev
);
462 #ifdef CONFIG_PM_SLEEP
464 * si470x_i2c_suspend - suspend the device
466 static int si470x_i2c_suspend(struct device
*dev
)
468 struct i2c_client
*client
= to_i2c_client(dev
);
469 struct si470x_device
*radio
= i2c_get_clientdata(client
);
472 radio
->registers
[POWERCFG
] |= POWERCFG_DISABLE
;
473 if (si470x_set_register(radio
, POWERCFG
) < 0)
481 * si470x_i2c_resume - resume the device
483 static int si470x_i2c_resume(struct device
*dev
)
485 struct i2c_client
*client
= to_i2c_client(dev
);
486 struct si470x_device
*radio
= i2c_get_clientdata(client
);
488 /* power up : need 110ms */
489 radio
->registers
[POWERCFG
] |= POWERCFG_ENABLE
;
490 if (si470x_set_register(radio
, POWERCFG
) < 0)
497 static SIMPLE_DEV_PM_OPS(si470x_i2c_pm
, si470x_i2c_suspend
, si470x_i2c_resume
);
502 * si470x_i2c_driver - i2c driver interface
504 static struct i2c_driver si470x_i2c_driver
= {
507 #ifdef CONFIG_PM_SLEEP
508 .pm
= &si470x_i2c_pm
,
511 .probe
= si470x_i2c_probe
,
512 .remove
= si470x_i2c_remove
,
513 .id_table
= si470x_i2c_id
,
516 module_i2c_driver(si470x_i2c_driver
);
518 MODULE_LICENSE("GPL");
519 MODULE_AUTHOR(DRIVER_AUTHOR
);
520 MODULE_DESCRIPTION(DRIVER_DESC
);
521 MODULE_VERSION(DRIVER_VERSION
);