Path: blob/master/drivers/media/radio/si470x/radio-si470x-i2c.c
15157 views
/*1* drivers/media/radio/si470x/radio-si470x-i2c.c2*3* I2C driver for radios with Silicon Labs Si470x FM Radio Receivers4*5* Copyright (c) 2009 Samsung Electronics Co.Ltd6* Author: Joonyoung Shim <[email protected]>7*8* This program is free software; you can redistribute it and/or modify9* it under the terms of the GNU General Public License as published by10* the Free Software Foundation; either version 2 of the License, or11* (at your option) any later version.12*13* This program is distributed in the hope that it will be useful,14* but WITHOUT ANY WARRANTY; without even the implied warranty of15* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the16* GNU General Public License for more details.17*18* You should have received a copy of the GNU General Public License19* along with this program; if not, write to the Free Software20* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA21*/222324/* driver definitions */25#define DRIVER_AUTHOR "Joonyoung Shim <[email protected]>";26#define DRIVER_KERNEL_VERSION KERNEL_VERSION(1, 0, 1)27#define DRIVER_CARD "Silicon Labs Si470x FM Radio Receiver"28#define DRIVER_DESC "I2C radio driver for Si470x FM Radio Receivers"29#define DRIVER_VERSION "1.0.1"3031/* kernel includes */32#include <linux/i2c.h>33#include <linux/slab.h>34#include <linux/delay.h>35#include <linux/interrupt.h>3637#include "radio-si470x.h"383940/* 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);48495051/**************************************************************************52* Module Parameters53**************************************************************************/5455/* Radio Nr */56static int radio_nr = -1;57module_param(radio_nr, int, 0444);58MODULE_PARM_DESC(radio_nr, "Radio Nr");5960/* RDS buffer blocks */61static unsigned int rds_buf = 100;62module_param(rds_buf, uint, 0444);63MODULE_PARM_DESC(rds_buf, "RDS buffer entries: *100*");6465/* 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*");73747576/**************************************************************************77* I2C Definitions78**************************************************************************/7980/* Write starts with the upper byte of register 0x02 */81#define WRITE_REG_NUM 882#define WRITE_INDEX(i) (i + 0x02)8384/* Read starts with the upper byte of register 0x0a */85#define READ_REG_NUM RADIO_REGISTER_NUM86#define READ_INDEX(i) ((i + RADIO_REGISTER_NUM - 0x0a) % READ_REG_NUM)87888990/**************************************************************************91* General Driver Functions - REGISTERs92**************************************************************************/9394/*95* si470x_get_register - read register96*/97int si470x_get_register(struct si470x_device *radio, int regnr)98{99u16 buf[READ_REG_NUM];100struct i2c_msg msgs[1] = {101{ radio->client->addr, I2C_M_RD, sizeof(u16) * READ_REG_NUM,102(void *)buf },103};104105if (i2c_transfer(radio->client->adapter, msgs, 1) != 1)106return -EIO;107108radio->registers[regnr] = __be16_to_cpu(buf[READ_INDEX(regnr)]);109110return 0;111}112113114/*115* si470x_set_register - write register116*/117int si470x_set_register(struct si470x_device *radio, int regnr)118{119int i;120u16 buf[WRITE_REG_NUM];121struct i2c_msg msgs[1] = {122{ radio->client->addr, 0, sizeof(u16) * WRITE_REG_NUM,123(void *)buf },124};125126for (i = 0; i < WRITE_REG_NUM; i++)127buf[i] = __cpu_to_be16(radio->registers[WRITE_INDEX(i)]);128129if (i2c_transfer(radio->client->adapter, msgs, 1) != 1)130return -EIO;131132return 0;133}134135136137/**************************************************************************138* General Driver Functions - ENTIRE REGISTERS139**************************************************************************/140141/*142* si470x_get_all_registers - read entire registers143*/144static int si470x_get_all_registers(struct si470x_device *radio)145{146int i;147u16 buf[READ_REG_NUM];148struct i2c_msg msgs[1] = {149{ radio->client->addr, I2C_M_RD, sizeof(u16) * READ_REG_NUM,150(void *)buf },151};152153if (i2c_transfer(radio->client->adapter, msgs, 1) != 1)154return -EIO;155156for (i = 0; i < READ_REG_NUM; i++)157radio->registers[i] = __be16_to_cpu(buf[READ_INDEX(i)]);158159return 0;160}161162163164/**************************************************************************165* General Driver Functions - DISCONNECT_CHECK166**************************************************************************/167168/*169* si470x_disconnect_check - check whether radio disconnects170*/171int si470x_disconnect_check(struct si470x_device *radio)172{173return 0;174}175176177178/**************************************************************************179* File Operations Interface180**************************************************************************/181182/*183* si470x_fops_open - file open184*/185int si470x_fops_open(struct file *file)186{187struct si470x_device *radio = video_drvdata(file);188int retval = 0;189190mutex_lock(&radio->lock);191radio->users++;192193if (radio->users == 1) {194/* start radio */195retval = si470x_start(radio);196if (retval < 0)197goto done;198199/* enable RDS / STC interrupt */200radio->registers[SYSCONFIG1] |= SYSCONFIG1_RDSIEN;201radio->registers[SYSCONFIG1] |= SYSCONFIG1_STCIEN;202radio->registers[SYSCONFIG1] &= ~SYSCONFIG1_GPIO2;203radio->registers[SYSCONFIG1] |= 0x1 << 2;204retval = si470x_set_register(radio, SYSCONFIG1);205}206207done:208mutex_unlock(&radio->lock);209return retval;210}211212213/*214* si470x_fops_release - file release215*/216int si470x_fops_release(struct file *file)217{218struct si470x_device *radio = video_drvdata(file);219int retval = 0;220221/* safety check */222if (!radio)223return -ENODEV;224225mutex_lock(&radio->lock);226radio->users--;227if (radio->users == 0)228/* stop radio */229retval = si470x_stop(radio);230231mutex_unlock(&radio->lock);232233return retval;234}235236237238/**************************************************************************239* Video4Linux Interface240**************************************************************************/241242/*243* si470x_vidioc_querycap - query device capabilities244*/245int si470x_vidioc_querycap(struct file *file, void *priv,246struct v4l2_capability *capability)247{248strlcpy(capability->driver, DRIVER_NAME, sizeof(capability->driver));249strlcpy(capability->card, DRIVER_CARD, sizeof(capability->card));250capability->version = DRIVER_KERNEL_VERSION;251capability->capabilities = V4L2_CAP_HW_FREQ_SEEK |252V4L2_CAP_TUNER | V4L2_CAP_RADIO;253254return 0;255}256257258259/**************************************************************************260* I2C Interface261**************************************************************************/262263/*264* si470x_i2c_interrupt - interrupt handler265*/266static irqreturn_t si470x_i2c_interrupt(int irq, void *dev_id)267{268struct si470x_device *radio = dev_id;269unsigned char regnr;270unsigned char blocknum;271unsigned short bler; /* rds block errors */272unsigned short rds;273unsigned char tmpbuf[3];274int retval = 0;275276/* check Seek/Tune Complete */277retval = si470x_get_register(radio, STATUSRSSI);278if (retval < 0)279goto end;280281if (radio->registers[STATUSRSSI] & STATUSRSSI_STC)282complete(&radio->completion);283284/* safety checks */285if ((radio->registers[SYSCONFIG1] & SYSCONFIG1_RDS) == 0)286goto end;287288/* Update RDS registers */289for (regnr = 1; regnr < RDS_REGISTER_NUM; regnr++) {290retval = si470x_get_register(radio, STATUSRSSI + regnr);291if (retval < 0)292goto end;293}294295/* get rds blocks */296if ((radio->registers[STATUSRSSI] & STATUSRSSI_RDSR) == 0)297/* No RDS group ready, better luck next time */298goto end;299300for (blocknum = 0; blocknum < 4; blocknum++) {301switch (blocknum) {302default:303bler = (radio->registers[STATUSRSSI] &304STATUSRSSI_BLERA) >> 9;305rds = radio->registers[RDSA];306break;307case 1:308bler = (radio->registers[READCHAN] &309READCHAN_BLERB) >> 14;310rds = radio->registers[RDSB];311break;312case 2:313bler = (radio->registers[READCHAN] &314READCHAN_BLERC) >> 12;315rds = radio->registers[RDSC];316break;317case 3:318bler = (radio->registers[READCHAN] &319READCHAN_BLERD) >> 10;320rds = radio->registers[RDSD];321break;322};323324/* Fill the V4L2 RDS buffer */325put_unaligned_le16(rds, &tmpbuf);326tmpbuf[2] = blocknum; /* offset name */327tmpbuf[2] |= blocknum << 3; /* received offset */328if (bler > max_rds_errors)329tmpbuf[2] |= 0x80; /* uncorrectable errors */330else if (bler > 0)331tmpbuf[2] |= 0x40; /* corrected error(s) */332333/* copy RDS block to internal buffer */334memcpy(&radio->buffer[radio->wr_index], &tmpbuf, 3);335radio->wr_index += 3;336337/* wrap write pointer */338if (radio->wr_index >= radio->buf_size)339radio->wr_index = 0;340341/* check for overflow */342if (radio->wr_index == radio->rd_index) {343/* increment and wrap read pointer */344radio->rd_index += 3;345if (radio->rd_index >= radio->buf_size)346radio->rd_index = 0;347}348}349350if (radio->wr_index != radio->rd_index)351wake_up_interruptible(&radio->read_queue);352353end:354return IRQ_HANDLED;355}356357358/*359* si470x_i2c_probe - probe for the device360*/361static int __devinit si470x_i2c_probe(struct i2c_client *client,362const struct i2c_device_id *id)363{364struct si470x_device *radio;365int retval = 0;366unsigned char version_warning = 0;367368/* private data allocation and initialization */369radio = kzalloc(sizeof(struct si470x_device), GFP_KERNEL);370if (!radio) {371retval = -ENOMEM;372goto err_initial;373}374375radio->users = 0;376radio->client = client;377mutex_init(&radio->lock);378379/* video device allocation and initialization */380radio->videodev = video_device_alloc();381if (!radio->videodev) {382retval = -ENOMEM;383goto err_radio;384}385memcpy(radio->videodev, &si470x_viddev_template,386sizeof(si470x_viddev_template));387video_set_drvdata(radio->videodev, radio);388389/* power up : need 110ms */390radio->registers[POWERCFG] = POWERCFG_ENABLE;391if (si470x_set_register(radio, POWERCFG) < 0) {392retval = -EIO;393goto err_video;394}395msleep(110);396397/* get device and chip versions */398if (si470x_get_all_registers(radio) < 0) {399retval = -EIO;400goto err_video;401}402dev_info(&client->dev, "DeviceID=0x%4.4hx ChipID=0x%4.4hx\n",403radio->registers[DEVICEID], radio->registers[CHIPID]);404if ((radio->registers[CHIPID] & CHIPID_FIRMWARE) < RADIO_FW_VERSION) {405dev_warn(&client->dev,406"This driver is known to work with "407"firmware version %hu,\n", RADIO_FW_VERSION);408dev_warn(&client->dev,409"but the device has firmware version %hu.\n",410radio->registers[CHIPID] & CHIPID_FIRMWARE);411version_warning = 1;412}413414/* give out version warning */415if (version_warning == 1) {416dev_warn(&client->dev,417"If you have some trouble using this driver,\n");418dev_warn(&client->dev,419"please report to V4L ML at "420"[email protected]\n");421}422423/* set initial frequency */424si470x_set_freq(radio, 87.5 * FREQ_MUL); /* available in all regions */425426/* rds buffer allocation */427radio->buf_size = rds_buf * 3;428radio->buffer = kmalloc(radio->buf_size, GFP_KERNEL);429if (!radio->buffer) {430retval = -EIO;431goto err_video;432}433434/* rds buffer configuration */435radio->wr_index = 0;436radio->rd_index = 0;437init_waitqueue_head(&radio->read_queue);438439/* mark Seek/Tune Complete Interrupt enabled */440radio->stci_enabled = true;441init_completion(&radio->completion);442443retval = request_threaded_irq(client->irq, NULL, si470x_i2c_interrupt,444IRQF_TRIGGER_FALLING, DRIVER_NAME, radio);445if (retval) {446dev_err(&client->dev, "Failed to register interrupt\n");447goto err_rds;448}449450/* register video device */451retval = video_register_device(radio->videodev, VFL_TYPE_RADIO,452radio_nr);453if (retval) {454dev_warn(&client->dev, "Could not register video device\n");455goto err_all;456}457i2c_set_clientdata(client, radio);458459return 0;460err_all:461free_irq(client->irq, radio);462err_rds:463kfree(radio->buffer);464err_video:465video_device_release(radio->videodev);466err_radio:467kfree(radio);468err_initial:469return retval;470}471472473/*474* si470x_i2c_remove - remove the device475*/476static __devexit int si470x_i2c_remove(struct i2c_client *client)477{478struct si470x_device *radio = i2c_get_clientdata(client);479480free_irq(client->irq, radio);481video_unregister_device(radio->videodev);482kfree(radio);483484return 0;485}486487488#ifdef CONFIG_PM489/*490* si470x_i2c_suspend - suspend the device491*/492static int si470x_i2c_suspend(struct device *dev)493{494struct i2c_client *client = to_i2c_client(dev);495struct si470x_device *radio = i2c_get_clientdata(client);496497/* power down */498radio->registers[POWERCFG] |= POWERCFG_DISABLE;499if (si470x_set_register(radio, POWERCFG) < 0)500return -EIO;501502return 0;503}504505506/*507* si470x_i2c_resume - resume the device508*/509static int si470x_i2c_resume(struct device *dev)510{511struct i2c_client *client = to_i2c_client(dev);512struct si470x_device *radio = i2c_get_clientdata(client);513514/* power up : need 110ms */515radio->registers[POWERCFG] |= POWERCFG_ENABLE;516if (si470x_set_register(radio, POWERCFG) < 0)517return -EIO;518msleep(110);519520return 0;521}522523static SIMPLE_DEV_PM_OPS(si470x_i2c_pm, si470x_i2c_suspend, si470x_i2c_resume);524#endif525526527/*528* si470x_i2c_driver - i2c driver interface529*/530static struct i2c_driver si470x_i2c_driver = {531.driver = {532.name = "si470x",533.owner = THIS_MODULE,534#ifdef CONFIG_PM535.pm = &si470x_i2c_pm,536#endif537},538.probe = si470x_i2c_probe,539.remove = __devexit_p(si470x_i2c_remove),540.id_table = si470x_i2c_id,541};542543544545/**************************************************************************546* Module Interface547**************************************************************************/548549/*550* si470x_i2c_init - module init551*/552static int __init si470x_i2c_init(void)553{554printk(KERN_INFO DRIVER_DESC ", Version " DRIVER_VERSION "\n");555return i2c_add_driver(&si470x_i2c_driver);556}557558559/*560* si470x_i2c_exit - module exit561*/562static void __exit si470x_i2c_exit(void)563{564i2c_del_driver(&si470x_i2c_driver);565}566567568module_init(si470x_i2c_init);569module_exit(si470x_i2c_exit);570571MODULE_LICENSE("GPL");572MODULE_AUTHOR(DRIVER_AUTHOR);573MODULE_DESCRIPTION(DRIVER_DESC);574MODULE_VERSION(DRIVER_VERSION);575576577