]> bbs.cooldavid.org Git - net-next-2.6.git/blame - drivers/media/radio/si470x/radio-si470x-i2c.c
Merge branch 'for-2.6.36' of git://linux-nfs.org/~bfields/linux
[net-next-2.6.git] / drivers / media / radio / si470x / radio-si470x-i2c.c
CommitLineData
cc35bbdd
JS
1/*
2 * drivers/media/radio/si470x/radio-si470x-i2c.c
3 *
4 * I2C driver for radios with Silicon Labs Si470x FM Radio Receivers
5 *
9dcb79c2 6 * Copyright (c) 2009 Samsung Electronics Co.Ltd
cc35bbdd
JS
7 * Author: Joonyoung Shim <jy0922.shim@samsung.com>
8 *
9dcb79c2
TL
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.
cc35bbdd 13 *
9dcb79c2
TL
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.
cc35bbdd 18 *
9dcb79c2
TL
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
9dcb79c2
TL
25/* driver definitions */
26#define DRIVER_AUTHOR "Joonyoung Shim <jy0922.shim@samsung.com>";
fe2137dd 27#define DRIVER_KERNEL_VERSION KERNEL_VERSION(1, 0, 1)
9dcb79c2
TL
28#define DRIVER_CARD "Silicon Labs Si470x FM Radio Receiver"
29#define DRIVER_DESC "I2C radio driver for Si470x FM Radio Receivers"
fe2137dd 30#define DRIVER_VERSION "1.0.1"
9dcb79c2
TL
31
32/* kernel includes */
cc35bbdd 33#include <linux/i2c.h>
5a0e3ad6 34#include <linux/slab.h>
cc35bbdd 35#include <linux/delay.h>
fe2137dd 36#include <linux/interrupt.h>
cc35bbdd
JS
37
38#include "radio-si470x.h"
39
cc35bbdd 40
9dcb79c2
TL
41/* I2C Device ID List */
42static const struct i2c_device_id si470x_i2c_id[] = {
43 /* Generic Entry */
44 { "si470x", 0 },
45 /* Terminating entry */
46 { }
47};
48MODULE_DEVICE_TABLE(i2c, si470x_i2c_id);
49
50
51
52/**************************************************************************
53 * Module Parameters
54 **************************************************************************/
55
56/* Radio Nr */
57static int radio_nr = -1;
58module_param(radio_nr, int, 0444);
59MODULE_PARM_DESC(radio_nr, "Radio Nr");
60
fe2137dd
JS
61/* RDS buffer blocks */
62static unsigned int rds_buf = 100;
63module_param(rds_buf, uint, 0444);
64MODULE_PARM_DESC(rds_buf, "RDS buffer entries: *100*");
65
66/* RDS maximum block errors */
67static 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 */
72module_param(max_rds_errors, ushort, 0644);
73MODULE_PARM_DESC(max_rds_errors, "RDS maximum block errors: *1*");
74
9dcb79c2
TL
75
76
77/**************************************************************************
78 * I2C Definitions
79 **************************************************************************/
80
81/* Write starts with the upper byte of register 0x02 */
82#define WRITE_REG_NUM 8
83#define WRITE_INDEX(i) (i + 0x02)
84
85/* Read starts with the upper byte of register 0x0a */
cc35bbdd
JS
86#define READ_REG_NUM RADIO_REGISTER_NUM
87#define READ_INDEX(i) ((i + RADIO_REGISTER_NUM - 0x0a) % READ_REG_NUM)
88
cc35bbdd 89
cc35bbdd 90
9dcb79c2
TL
91/**************************************************************************
92 * General Driver Functions - REGISTERs
93 **************************************************************************/
cc35bbdd 94
9dcb79c2
TL
95/*
96 * si470x_get_register - read register
97 */
cc35bbdd
JS
98int si470x_get_register(struct si470x_device *radio, int regnr)
99{
100 u16 buf[READ_REG_NUM];
101 struct i2c_msg msgs[1] = {
102 { radio->client->addr, I2C_M_RD, sizeof(u16) * READ_REG_NUM,
103 (void *)buf },
104 };
105
106 if (i2c_transfer(radio->client->adapter, msgs, 1) != 1)
107 return -EIO;
108
109 radio->registers[regnr] = __be16_to_cpu(buf[READ_INDEX(regnr)]);
110
111 return 0;
112}
113
cc35bbdd 114
9dcb79c2
TL
115/*
116 * si470x_set_register - write register
117 */
cc35bbdd
JS
118int si470x_set_register(struct si470x_device *radio, int regnr)
119{
120 int i;
121 u16 buf[WRITE_REG_NUM];
122 struct i2c_msg msgs[1] = {
123 { radio->client->addr, 0, sizeof(u16) * WRITE_REG_NUM,
124 (void *)buf },
125 };
126
127 for (i = 0; i < WRITE_REG_NUM; i++)
128 buf[i] = __cpu_to_be16(radio->registers[WRITE_INDEX(i)]);
129
130 if (i2c_transfer(radio->client->adapter, msgs, 1) != 1)
131 return -EIO;
132
133 return 0;
134}
135
9dcb79c2
TL
136
137
138/**************************************************************************
139 * General Driver Functions - ENTIRE REGISTERS
140 **************************************************************************/
141
142/*
143 * si470x_get_all_registers - read entire registers
144 */
145static int si470x_get_all_registers(struct si470x_device *radio)
146{
147 int i;
148 u16 buf[READ_REG_NUM];
149 struct i2c_msg msgs[1] = {
150 { radio->client->addr, I2C_M_RD, sizeof(u16) * READ_REG_NUM,
151 (void *)buf },
152 };
153
154 if (i2c_transfer(radio->client->adapter, msgs, 1) != 1)
155 return -EIO;
156
157 for (i = 0; i < READ_REG_NUM; i++)
158 radio->registers[i] = __be16_to_cpu(buf[READ_INDEX(i)]);
159
160 return 0;
161}
162
163
164
165/**************************************************************************
166 * General Driver Functions - DISCONNECT_CHECK
167 **************************************************************************/
168
169/*
170 * si470x_disconnect_check - check whether radio disconnects
171 */
cc35bbdd
JS
172int si470x_disconnect_check(struct si470x_device *radio)
173{
174 return 0;
175}
176
9dcb79c2
TL
177
178
179/**************************************************************************
180 * File Operations Interface
181 **************************************************************************/
182
183/*
184 * si470x_fops_open - file open
185 */
1aa925c9 186int si470x_fops_open(struct file *file)
cc35bbdd
JS
187{
188 struct si470x_device *radio = video_drvdata(file);
189 int retval = 0;
190
191 mutex_lock(&radio->lock);
192 radio->users++;
193
fe2137dd 194 if (radio->users == 1) {
cc35bbdd
JS
195 /* start radio */
196 retval = si470x_start(radio);
fe2137dd
JS
197 if (retval < 0)
198 goto done;
199
200 /* enable RDS interrupt */
201 radio->registers[SYSCONFIG1] |= SYSCONFIG1_RDSIEN;
202 radio->registers[SYSCONFIG1] &= ~SYSCONFIG1_GPIO2;
203 radio->registers[SYSCONFIG1] |= 0x1 << 2;
204 retval = si470x_set_register(radio, SYSCONFIG1);
205 }
9dcb79c2 206
fe2137dd 207done:
cc35bbdd 208 mutex_unlock(&radio->lock);
cc35bbdd
JS
209 return retval;
210}
211
9dcb79c2
TL
212
213/*
214 * si470x_fops_release - file release
215 */
1aa925c9 216int si470x_fops_release(struct file *file)
cc35bbdd
JS
217{
218 struct si470x_device *radio = video_drvdata(file);
219 int retval = 0;
220
221 /* safety check */
222 if (!radio)
223 return -ENODEV;
224
225 mutex_lock(&radio->lock);
226 radio->users--;
227 if (radio->users == 0)
228 /* stop radio */
229 retval = si470x_stop(radio);
9dcb79c2 230
cc35bbdd
JS
231 mutex_unlock(&radio->lock);
232
233 return retval;
234}
235
9dcb79c2 236
9dcb79c2
TL
237
238/**************************************************************************
239 * Video4Linux Interface
240 **************************************************************************/
241
242/*
243 * si470x_vidioc_querycap - query device capabilities
244 */
cc35bbdd
JS
245int si470x_vidioc_querycap(struct file *file, void *priv,
246 struct v4l2_capability *capability)
247{
248 strlcpy(capability->driver, DRIVER_NAME, sizeof(capability->driver));
249 strlcpy(capability->card, DRIVER_CARD, sizeof(capability->card));
250 capability->version = DRIVER_KERNEL_VERSION;
251 capability->capabilities = V4L2_CAP_HW_FREQ_SEEK |
252 V4L2_CAP_TUNER | V4L2_CAP_RADIO;
253
254 return 0;
255}
256
9dcb79c2
TL
257
258
259/**************************************************************************
260 * I2C Interface
261 **************************************************************************/
262
fe2137dd
JS
263/*
264 * si470x_i2c_interrupt_work - rds processing function
265 */
266static void si470x_i2c_interrupt_work(struct work_struct *work)
267{
268 struct si470x_device *radio = container_of(work,
269 struct si470x_device, radio_work);
270 unsigned char regnr;
271 unsigned char blocknum;
272 unsigned short bler; /* rds block errors */
273 unsigned short rds;
274 unsigned char tmpbuf[3];
275 int retval = 0;
276
277 /* safety checks */
278 if ((radio->registers[SYSCONFIG1] & SYSCONFIG1_RDS) == 0)
279 return;
280
281 /* Update RDS registers */
282 for (regnr = 0; regnr < RDS_REGISTER_NUM; regnr++) {
283 retval = si470x_get_register(radio, STATUSRSSI + regnr);
284 if (retval < 0)
285 return;
286 }
287
288 /* get rds blocks */
289 if ((radio->registers[STATUSRSSI] & STATUSRSSI_RDSR) == 0)
290 /* No RDS group ready, better luck next time */
291 return;
292
293 for (blocknum = 0; blocknum < 4; blocknum++) {
294 switch (blocknum) {
295 default:
296 bler = (radio->registers[STATUSRSSI] &
297 STATUSRSSI_BLERA) >> 9;
298 rds = radio->registers[RDSA];
299 break;
300 case 1:
301 bler = (radio->registers[READCHAN] &
302 READCHAN_BLERB) >> 14;
303 rds = radio->registers[RDSB];
304 break;
305 case 2:
306 bler = (radio->registers[READCHAN] &
307 READCHAN_BLERC) >> 12;
308 rds = radio->registers[RDSC];
309 break;
310 case 3:
311 bler = (radio->registers[READCHAN] &
312 READCHAN_BLERD) >> 10;
313 rds = radio->registers[RDSD];
314 break;
315 };
316
317 /* Fill the V4L2 RDS buffer */
318 put_unaligned_le16(rds, &tmpbuf);
319 tmpbuf[2] = blocknum; /* offset name */
320 tmpbuf[2] |= blocknum << 3; /* received offset */
321 if (bler > max_rds_errors)
322 tmpbuf[2] |= 0x80; /* uncorrectable errors */
323 else if (bler > 0)
324 tmpbuf[2] |= 0x40; /* corrected error(s) */
325
326 /* copy RDS block to internal buffer */
327 memcpy(&radio->buffer[radio->wr_index], &tmpbuf, 3);
328 radio->wr_index += 3;
329
330 /* wrap write pointer */
331 if (radio->wr_index >= radio->buf_size)
332 radio->wr_index = 0;
333
334 /* check for overflow */
335 if (radio->wr_index == radio->rd_index) {
336 /* increment and wrap read pointer */
337 radio->rd_index += 3;
338 if (radio->rd_index >= radio->buf_size)
339 radio->rd_index = 0;
340 }
341 }
342
343 if (radio->wr_index != radio->rd_index)
344 wake_up_interruptible(&radio->read_queue);
345}
346
347
348/*
349 * si470x_i2c_interrupt - interrupt handler
350 */
351static irqreturn_t si470x_i2c_interrupt(int irq, void *dev_id)
352{
353 struct si470x_device *radio = dev_id;
354
355 if (!work_pending(&radio->radio_work))
356 schedule_work(&radio->radio_work);
357
358 return IRQ_HANDLED;
359}
360
361
9dcb79c2
TL
362/*
363 * si470x_i2c_probe - probe for the device
364 */
cc35bbdd
JS
365static int __devinit si470x_i2c_probe(struct i2c_client *client,
366 const struct i2c_device_id *id)
367{
368 struct si470x_device *radio;
369 int retval = 0;
9dcb79c2 370 unsigned char version_warning = 0;
cc35bbdd
JS
371
372 /* private data allocation and initialization */
373 radio = kzalloc(sizeof(struct si470x_device), GFP_KERNEL);
374 if (!radio) {
375 retval = -ENOMEM;
376 goto err_initial;
377 }
fe2137dd
JS
378
379 INIT_WORK(&radio->radio_work, si470x_i2c_interrupt_work);
cc35bbdd 380 radio->users = 0;
9dcb79c2 381 radio->client = client;
cc35bbdd
JS
382 mutex_init(&radio->lock);
383
384 /* video device allocation and initialization */
385 radio->videodev = video_device_alloc();
386 if (!radio->videodev) {
387 retval = -ENOMEM;
388 goto err_radio;
389 }
390 memcpy(radio->videodev, &si470x_viddev_template,
391 sizeof(si470x_viddev_template));
392 video_set_drvdata(radio->videodev, radio);
393
394 /* power up : need 110ms */
395 radio->registers[POWERCFG] = POWERCFG_ENABLE;
396 if (si470x_set_register(radio, POWERCFG) < 0) {
397 retval = -EIO;
cc6e853c 398 goto err_video;
cc35bbdd
JS
399 }
400 msleep(110);
401
9dcb79c2 402 /* get device and chip versions */
cc35bbdd
JS
403 if (si470x_get_all_registers(radio) < 0) {
404 retval = -EIO;
9dcb79c2 405 goto err_video;
cc35bbdd
JS
406 }
407 dev_info(&client->dev, "DeviceID=0x%4.4hx ChipID=0x%4.4hx\n",
408 radio->registers[DEVICEID], radio->registers[CHIPID]);
9dcb79c2
TL
409 if ((radio->registers[CHIPID] & CHIPID_FIRMWARE) < RADIO_FW_VERSION) {
410 dev_warn(&client->dev,
411 "This driver is known to work with "
412 "firmware version %hu,\n", RADIO_FW_VERSION);
413 dev_warn(&client->dev,
414 "but the device has firmware version %hu.\n",
415 radio->registers[CHIPID] & CHIPID_FIRMWARE);
416 version_warning = 1;
417 }
418
419 /* give out version warning */
420 if (version_warning == 1) {
421 dev_warn(&client->dev,
422 "If you have some trouble using this driver,\n");
423 dev_warn(&client->dev,
424 "please report to V4L ML at "
425 "linux-media@vger.kernel.org\n");
426 }
cc35bbdd
JS
427
428 /* set initial frequency */
429 si470x_set_freq(radio, 87.5 * FREQ_MUL); /* available in all regions */
430
fe2137dd
JS
431 /* rds buffer allocation */
432 radio->buf_size = rds_buf * 3;
433 radio->buffer = kmalloc(radio->buf_size, GFP_KERNEL);
434 if (!radio->buffer) {
435 retval = -EIO;
436 goto err_video;
437 }
438
439 /* rds buffer configuration */
440 radio->wr_index = 0;
441 radio->rd_index = 0;
442 init_waitqueue_head(&radio->read_queue);
443
444 retval = request_irq(client->irq, si470x_i2c_interrupt,
445 IRQF_TRIGGER_FALLING, DRIVER_NAME, radio);
446 if (retval) {
447 dev_err(&client->dev, "Failed to register interrupt\n");
448 goto err_rds;
449 }
450
cc35bbdd 451 /* register video device */
9dcb79c2
TL
452 retval = video_register_device(radio->videodev, VFL_TYPE_RADIO,
453 radio_nr);
cc35bbdd
JS
454 if (retval) {
455 dev_warn(&client->dev, "Could not register video device\n");
456 goto err_all;
457 }
cc35bbdd
JS
458 i2c_set_clientdata(client, radio);
459
460 return 0;
461err_all:
fe2137dd
JS
462 free_irq(client->irq, radio);
463err_rds:
464 kfree(radio->buffer);
9dcb79c2 465err_video:
cc35bbdd
JS
466 video_device_release(radio->videodev);
467err_radio:
468 kfree(radio);
469err_initial:
470 return retval;
471}
472
9dcb79c2
TL
473
474/*
475 * si470x_i2c_remove - remove the device
476 */
cc35bbdd
JS
477static __devexit int si470x_i2c_remove(struct i2c_client *client)
478{
479 struct si470x_device *radio = i2c_get_clientdata(client);
480
fe2137dd
JS
481 free_irq(client->irq, radio);
482 cancel_work_sync(&radio->radio_work);
cc35bbdd
JS
483 video_unregister_device(radio->videodev);
484 kfree(radio);
cc35bbdd
JS
485
486 return 0;
487}
488
cc35bbdd 489
d1471f02
JS
490#ifdef CONFIG_PM
491/*
492 * si470x_i2c_suspend - suspend the device
493 */
494static int si470x_i2c_suspend(struct i2c_client *client, pm_message_t mesg)
495{
496 struct si470x_device *radio = i2c_get_clientdata(client);
497
498 /* power down */
499 radio->registers[POWERCFG] |= POWERCFG_DISABLE;
500 if (si470x_set_register(radio, POWERCFG) < 0)
501 return -EIO;
502
503 return 0;
504}
505
506
507/*
508 * si470x_i2c_resume - resume the device
509 */
510static int si470x_i2c_resume(struct i2c_client *client)
511{
512 struct si470x_device *radio = i2c_get_clientdata(client);
513
514 /* power up : need 110ms */
515 radio->registers[POWERCFG] |= POWERCFG_ENABLE;
516 if (si470x_set_register(radio, POWERCFG) < 0)
517 return -EIO;
518 msleep(110);
519
520 return 0;
521}
522#else
523#define si470x_i2c_suspend NULL
524#define si470x_i2c_resume NULL
525#endif
526
527
9dcb79c2
TL
528/*
529 * si470x_i2c_driver - i2c driver interface
530 */
cc35bbdd
JS
531static struct i2c_driver si470x_i2c_driver = {
532 .driver = {
9dcb79c2
TL
533 .name = "si470x",
534 .owner = THIS_MODULE,
cc35bbdd 535 },
9dcb79c2
TL
536 .probe = si470x_i2c_probe,
537 .remove = __devexit_p(si470x_i2c_remove),
d1471f02
JS
538 .suspend = si470x_i2c_suspend,
539 .resume = si470x_i2c_resume,
9dcb79c2 540 .id_table = si470x_i2c_id,
cc35bbdd
JS
541};
542
9dcb79c2
TL
543
544
545/**************************************************************************
546 * Module Interface
547 **************************************************************************/
548
549/*
550 * si470x_i2c_init - module init
551 */
cc35bbdd
JS
552static int __init si470x_i2c_init(void)
553{
9dcb79c2 554 printk(KERN_INFO DRIVER_DESC ", Version " DRIVER_VERSION "\n");
cc35bbdd
JS
555 return i2c_add_driver(&si470x_i2c_driver);
556}
cc35bbdd 557
9dcb79c2
TL
558
559/*
560 * si470x_i2c_exit - module exit
561 */
cc35bbdd
JS
562static void __exit si470x_i2c_exit(void)
563{
564 i2c_del_driver(&si470x_i2c_driver);
565}
9dcb79c2
TL
566
567
568module_init(si470x_i2c_init);
cc35bbdd
JS
569module_exit(si470x_i2c_exit);
570
cc35bbdd 571MODULE_LICENSE("GPL");
9dcb79c2
TL
572MODULE_AUTHOR(DRIVER_AUTHOR);
573MODULE_DESCRIPTION(DRIVER_DESC);
574MODULE_VERSION(DRIVER_VERSION);