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