Linux 4.19.133
[linux/fpc-iii.git] / drivers / media / radio / si470x / radio-si470x-i2c.c
blobaa12fd26638954c405ea449fb29f08a6f46a4562
1 /*
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"
27 /* kernel includes */
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[] = {
38 /* Generic Entry */
39 { "si470x", 0 },
40 /* Terminating entry */
41 { }
43 MODULE_DEVICE_TABLE(i2c, si470x_i2c_id);
46 /**************************************************************************
47 * Module Parameters
48 **************************************************************************/
50 /* Radio Nr */
51 static int radio_nr = -1;
52 module_param(radio_nr, int, 0444);
53 MODULE_PARM_DESC(radio_nr, "Radio Nr");
55 /* RDS buffer blocks */
56 static unsigned int rds_buf = 100;
57 module_param(rds_buf, uint, 0444);
58 MODULE_PARM_DESC(rds_buf, "RDS buffer entries: *100*");
60 /* RDS maximum block errors */
61 static unsigned short max_rds_errors = 1;
62 /* 0 means 0 errors requiring correction */
63 /* 1 means 1-2 errors requiring correction (used by original USBRadio.exe) */
64 /* 2 means 3-5 errors requiring correction */
65 /* 3 means 6+ errors or errors in checkword, correction not possible */
66 module_param(max_rds_errors, ushort, 0644);
67 MODULE_PARM_DESC(max_rds_errors, "RDS maximum block errors: *1*");
71 /**************************************************************************
72 * I2C Definitions
73 **************************************************************************/
75 /* Write starts with the upper byte of register 0x02 */
76 #define WRITE_REG_NUM 8
77 #define WRITE_INDEX(i) (i + 0x02)
79 /* Read starts with the upper byte of register 0x0a */
80 #define READ_REG_NUM RADIO_REGISTER_NUM
81 #define READ_INDEX(i) ((i + RADIO_REGISTER_NUM - 0x0a) % READ_REG_NUM)
85 /**************************************************************************
86 * General Driver Functions - REGISTERs
87 **************************************************************************/
90 * si470x_get_register - read register
92 static int si470x_get_register(struct si470x_device *radio, int regnr)
94 __be16 buf[READ_REG_NUM];
95 struct i2c_msg msgs[1] = {
97 .addr = radio->client->addr,
98 .flags = I2C_M_RD,
99 .len = sizeof(u16) * READ_REG_NUM,
100 .buf = (void *)buf
104 if (i2c_transfer(radio->client->adapter, msgs, 1) != 1)
105 return -EIO;
107 radio->registers[regnr] = __be16_to_cpu(buf[READ_INDEX(regnr)]);
109 return 0;
114 * si470x_set_register - write register
116 static int si470x_set_register(struct si470x_device *radio, int regnr)
118 int i;
119 __be16 buf[WRITE_REG_NUM];
120 struct i2c_msg msgs[1] = {
122 .addr = radio->client->addr,
123 .len = sizeof(u16) * WRITE_REG_NUM,
124 .buf = (void *)buf
128 for (i = 0; i < WRITE_REG_NUM; i++)
129 buf[i] = __cpu_to_be16(radio->registers[WRITE_INDEX(i)]);
131 if (i2c_transfer(radio->client->adapter, msgs, 1) != 1)
132 return -EIO;
134 return 0;
139 /**************************************************************************
140 * General Driver Functions - ENTIRE REGISTERS
141 **************************************************************************/
144 * si470x_get_all_registers - read entire registers
146 static int si470x_get_all_registers(struct si470x_device *radio)
148 int i;
149 __be16 buf[READ_REG_NUM];
150 struct i2c_msg msgs[1] = {
152 .addr = radio->client->addr,
153 .flags = I2C_M_RD,
154 .len = sizeof(u16) * READ_REG_NUM,
155 .buf = (void *)buf
159 if (i2c_transfer(radio->client->adapter, msgs, 1) != 1)
160 return -EIO;
162 for (i = 0; i < READ_REG_NUM; i++)
163 radio->registers[i] = __be16_to_cpu(buf[READ_INDEX(i)]);
165 return 0;
170 /**************************************************************************
171 * File Operations Interface
172 **************************************************************************/
175 * si470x_fops_open - file open
177 static int si470x_fops_open(struct file *file)
179 struct si470x_device *radio = video_drvdata(file);
180 int retval = v4l2_fh_open(file);
182 if (retval)
183 return retval;
185 if (v4l2_fh_is_singular_file(file)) {
186 /* start radio */
187 retval = si470x_start(radio);
188 if (retval < 0)
189 goto done;
191 /* enable RDS / STC interrupt */
192 radio->registers[SYSCONFIG1] |= SYSCONFIG1_RDSIEN;
193 radio->registers[SYSCONFIG1] |= SYSCONFIG1_STCIEN;
194 radio->registers[SYSCONFIG1] &= ~SYSCONFIG1_GPIO2;
195 radio->registers[SYSCONFIG1] |= 0x1 << 2;
196 retval = si470x_set_register(radio, SYSCONFIG1);
199 done:
200 if (retval)
201 v4l2_fh_release(file);
202 return retval;
207 * si470x_fops_release - file release
209 static int si470x_fops_release(struct file *file)
211 struct si470x_device *radio = video_drvdata(file);
213 if (v4l2_fh_is_singular_file(file))
214 /* stop radio */
215 si470x_stop(radio);
217 return v4l2_fh_release(file);
222 /**************************************************************************
223 * Video4Linux Interface
224 **************************************************************************/
227 * si470x_vidioc_querycap - query device capabilities
229 static int si470x_vidioc_querycap(struct file *file, void *priv,
230 struct v4l2_capability *capability)
232 strlcpy(capability->driver, DRIVER_NAME, sizeof(capability->driver));
233 strlcpy(capability->card, DRIVER_CARD, sizeof(capability->card));
234 capability->device_caps = V4L2_CAP_HW_FREQ_SEEK | V4L2_CAP_READWRITE |
235 V4L2_CAP_TUNER | V4L2_CAP_RADIO | V4L2_CAP_RDS_CAPTURE;
236 capability->capabilities = capability->device_caps | V4L2_CAP_DEVICE_CAPS;
238 return 0;
243 /**************************************************************************
244 * I2C Interface
245 **************************************************************************/
248 * si470x_i2c_interrupt - interrupt handler
250 static irqreturn_t si470x_i2c_interrupt(int irq, void *dev_id)
252 struct si470x_device *radio = dev_id;
253 unsigned char regnr;
254 unsigned char blocknum;
255 unsigned short bler; /* rds block errors */
256 unsigned short rds;
257 unsigned char tmpbuf[3];
258 int retval = 0;
260 /* check Seek/Tune Complete */
261 retval = si470x_get_register(radio, STATUSRSSI);
262 if (retval < 0)
263 goto end;
265 if (radio->registers[STATUSRSSI] & STATUSRSSI_STC)
266 complete(&radio->completion);
268 /* safety checks */
269 if ((radio->registers[SYSCONFIG1] & SYSCONFIG1_RDS) == 0)
270 goto end;
272 /* Update RDS registers */
273 for (regnr = 1; regnr < RDS_REGISTER_NUM; regnr++) {
274 retval = si470x_get_register(radio, STATUSRSSI + regnr);
275 if (retval < 0)
276 goto end;
279 /* get rds blocks */
280 if ((radio->registers[STATUSRSSI] & STATUSRSSI_RDSR) == 0)
281 /* No RDS group ready, better luck next time */
282 goto end;
284 for (blocknum = 0; blocknum < 4; blocknum++) {
285 switch (blocknum) {
286 default:
287 bler = (radio->registers[STATUSRSSI] &
288 STATUSRSSI_BLERA) >> 9;
289 rds = radio->registers[RDSA];
290 break;
291 case 1:
292 bler = (radio->registers[READCHAN] &
293 READCHAN_BLERB) >> 14;
294 rds = radio->registers[RDSB];
295 break;
296 case 2:
297 bler = (radio->registers[READCHAN] &
298 READCHAN_BLERC) >> 12;
299 rds = radio->registers[RDSC];
300 break;
301 case 3:
302 bler = (radio->registers[READCHAN] &
303 READCHAN_BLERD) >> 10;
304 rds = radio->registers[RDSD];
305 break;
308 /* Fill the V4L2 RDS buffer */
309 put_unaligned_le16(rds, &tmpbuf);
310 tmpbuf[2] = blocknum; /* offset name */
311 tmpbuf[2] |= blocknum << 3; /* received offset */
312 if (bler > max_rds_errors)
313 tmpbuf[2] |= 0x80; /* uncorrectable errors */
314 else if (bler > 0)
315 tmpbuf[2] |= 0x40; /* corrected error(s) */
317 /* copy RDS block to internal buffer */
318 memcpy(&radio->buffer[radio->wr_index], &tmpbuf, 3);
319 radio->wr_index += 3;
321 /* wrap write pointer */
322 if (radio->wr_index >= radio->buf_size)
323 radio->wr_index = 0;
325 /* check for overflow */
326 if (radio->wr_index == radio->rd_index) {
327 /* increment and wrap read pointer */
328 radio->rd_index += 3;
329 if (radio->rd_index >= radio->buf_size)
330 radio->rd_index = 0;
334 if (radio->wr_index != radio->rd_index)
335 wake_up_interruptible(&radio->read_queue);
337 end:
338 return IRQ_HANDLED;
343 * si470x_i2c_probe - probe for the device
345 static int si470x_i2c_probe(struct i2c_client *client,
346 const struct i2c_device_id *id)
348 struct si470x_device *radio;
349 int retval = 0;
350 unsigned char version_warning = 0;
352 /* private data allocation and initialization */
353 radio = kzalloc(sizeof(struct si470x_device), GFP_KERNEL);
354 if (!radio) {
355 retval = -ENOMEM;
356 goto err_initial;
359 radio->client = client;
360 radio->band = 1; /* Default to 76 - 108 MHz */
361 mutex_init(&radio->lock);
362 init_completion(&radio->completion);
364 radio->get_register = si470x_get_register;
365 radio->set_register = si470x_set_register;
366 radio->fops_open = si470x_fops_open;
367 radio->fops_release = si470x_fops_release;
368 radio->vidioc_querycap = si470x_vidioc_querycap;
370 retval = v4l2_device_register(&client->dev, &radio->v4l2_dev);
371 if (retval < 0) {
372 dev_err(&client->dev, "couldn't register v4l2_device\n");
373 goto err_radio;
376 v4l2_ctrl_handler_init(&radio->hdl, 2);
377 v4l2_ctrl_new_std(&radio->hdl, &si470x_ctrl_ops,
378 V4L2_CID_AUDIO_MUTE, 0, 1, 1, 1);
379 v4l2_ctrl_new_std(&radio->hdl, &si470x_ctrl_ops,
380 V4L2_CID_AUDIO_VOLUME, 0, 15, 1, 15);
381 if (radio->hdl.error) {
382 retval = radio->hdl.error;
383 dev_err(&client->dev, "couldn't register control\n");
384 goto err_dev;
387 /* video device initialization */
388 radio->videodev = si470x_viddev_template;
389 radio->videodev.ctrl_handler = &radio->hdl;
390 radio->videodev.lock = &radio->lock;
391 radio->videodev.v4l2_dev = &radio->v4l2_dev;
392 radio->videodev.release = video_device_release_empty;
393 video_set_drvdata(&radio->videodev, radio);
395 /* power up : need 110ms */
396 radio->registers[POWERCFG] = POWERCFG_ENABLE;
397 if (si470x_set_register(radio, POWERCFG) < 0) {
398 retval = -EIO;
399 goto err_ctrl;
401 msleep(110);
403 /* get device and chip versions */
404 if (si470x_get_all_registers(radio) < 0) {
405 retval = -EIO;
406 goto err_ctrl;
408 dev_info(&client->dev, "DeviceID=0x%4.4hx ChipID=0x%4.4hx\n",
409 radio->registers[DEVICEID], radio->registers[SI_CHIPID]);
410 if ((radio->registers[SI_CHIPID] & SI_CHIPID_FIRMWARE) < RADIO_FW_VERSION) {
411 dev_warn(&client->dev,
412 "This driver is known to work with firmware version %hu,\n",
413 RADIO_FW_VERSION);
414 dev_warn(&client->dev,
415 "but the device has firmware version %hu.\n",
416 radio->registers[SI_CHIPID] & SI_CHIPID_FIRMWARE);
417 version_warning = 1;
420 /* give out version warning */
421 if (version_warning == 1) {
422 dev_warn(&client->dev,
423 "If you have some trouble using this driver,\n");
424 dev_warn(&client->dev,
425 "please report to V4L ML at linux-media@vger.kernel.org\n");
428 /* set initial frequency */
429 si470x_set_freq(radio, 87.5 * FREQ_MUL); /* available in all regions */
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_ctrl;
439 /* rds buffer configuration */
440 radio->wr_index = 0;
441 radio->rd_index = 0;
442 init_waitqueue_head(&radio->read_queue);
444 retval = request_threaded_irq(client->irq, NULL, si470x_i2c_interrupt,
445 IRQF_TRIGGER_FALLING | IRQF_ONESHOT, DRIVER_NAME,
446 radio);
447 if (retval) {
448 dev_err(&client->dev, "Failed to register interrupt\n");
449 goto err_rds;
452 /* register video device */
453 retval = video_register_device(&radio->videodev, VFL_TYPE_RADIO,
454 radio_nr);
455 if (retval) {
456 dev_warn(&client->dev, "Could not register video device\n");
457 goto err_all;
459 i2c_set_clientdata(client, radio);
461 return 0;
462 err_all:
463 free_irq(client->irq, radio);
464 err_rds:
465 kfree(radio->buffer);
466 err_ctrl:
467 v4l2_ctrl_handler_free(&radio->hdl);
468 err_dev:
469 v4l2_device_unregister(&radio->v4l2_dev);
470 err_radio:
471 kfree(radio);
472 err_initial:
473 return retval;
478 * si470x_i2c_remove - remove the device
480 static int si470x_i2c_remove(struct i2c_client *client)
482 struct si470x_device *radio = i2c_get_clientdata(client);
484 free_irq(client->irq, radio);
485 video_unregister_device(&radio->videodev);
487 v4l2_ctrl_handler_free(&radio->hdl);
488 v4l2_device_unregister(&radio->v4l2_dev);
489 kfree(radio);
490 return 0;
494 #ifdef CONFIG_PM_SLEEP
496 * si470x_i2c_suspend - suspend the device
498 static int si470x_i2c_suspend(struct device *dev)
500 struct i2c_client *client = to_i2c_client(dev);
501 struct si470x_device *radio = i2c_get_clientdata(client);
503 /* power down */
504 radio->registers[POWERCFG] |= POWERCFG_DISABLE;
505 if (si470x_set_register(radio, POWERCFG) < 0)
506 return -EIO;
508 return 0;
513 * si470x_i2c_resume - resume the device
515 static int si470x_i2c_resume(struct device *dev)
517 struct i2c_client *client = to_i2c_client(dev);
518 struct si470x_device *radio = i2c_get_clientdata(client);
520 /* power up : need 110ms */
521 radio->registers[POWERCFG] |= POWERCFG_ENABLE;
522 if (si470x_set_register(radio, POWERCFG) < 0)
523 return -EIO;
524 msleep(110);
526 return 0;
529 static SIMPLE_DEV_PM_OPS(si470x_i2c_pm, si470x_i2c_suspend, si470x_i2c_resume);
530 #endif
534 * si470x_i2c_driver - i2c driver interface
536 static struct i2c_driver si470x_i2c_driver = {
537 .driver = {
538 .name = "si470x",
539 #ifdef CONFIG_PM_SLEEP
540 .pm = &si470x_i2c_pm,
541 #endif
543 .probe = si470x_i2c_probe,
544 .remove = si470x_i2c_remove,
545 .id_table = si470x_i2c_id,
548 module_i2c_driver(si470x_i2c_driver);
550 MODULE_LICENSE("GPL");
551 MODULE_AUTHOR(DRIVER_AUTHOR);
552 MODULE_DESCRIPTION(DRIVER_DESC);
553 MODULE_VERSION(DRIVER_VERSION);