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.
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
31 /* driver definitions */
32 #define DRIVER_AUTHOR "Joonyoung Shim <jy0922.shim@samsung.com>";
33 #define DRIVER_KERNEL_VERSION KERNEL_VERSION(1, 0, 0)
34 #define DRIVER_CARD "Silicon Labs Si470x FM Radio Receiver"
35 #define DRIVER_DESC "I2C radio driver for Si470x FM Radio Receivers"
36 #define DRIVER_VERSION "1.0.0"
39 #include <linux/i2c.h>
40 #include <linux/delay.h>
42 #include "radio-si470x.h"
45 /* I2C Device ID List */
46 static const struct i2c_device_id si470x_i2c_id[] = {
49 /* Terminating entry */
52 MODULE_DEVICE_TABLE(i2c, si470x_i2c_id);
56 /**************************************************************************
58 **************************************************************************/
61 static int radio_nr = -1;
62 module_param(radio_nr, int, 0444);
63 MODULE_PARM_DESC(radio_nr, "Radio Nr");
67 /**************************************************************************
69 **************************************************************************/
71 /* Write starts with the upper byte of register 0x02 */
72 #define WRITE_REG_NUM 8
73 #define WRITE_INDEX(i) (i + 0x02)
75 /* Read starts with the upper byte of register 0x0a */
76 #define READ_REG_NUM RADIO_REGISTER_NUM
77 #define READ_INDEX(i) ((i + RADIO_REGISTER_NUM - 0x0a) % READ_REG_NUM)
81 /**************************************************************************
82 * General Driver Functions - REGISTERs
83 **************************************************************************/
86 * si470x_get_register - read register
88 int si470x_get_register(struct si470x_device *radio, int regnr)
90 u16 buf[READ_REG_NUM];
91 struct i2c_msg msgs[1] = {
92 { radio->client->addr, I2C_M_RD, sizeof(u16) * READ_REG_NUM,
96 if (i2c_transfer(radio->client->adapter, msgs, 1) != 1)
99 radio->registers[regnr] = __be16_to_cpu(buf[READ_INDEX(regnr)]);
106 * si470x_set_register - write register
108 int si470x_set_register(struct si470x_device *radio, int regnr)
111 u16 buf[WRITE_REG_NUM];
112 struct i2c_msg msgs[1] = {
113 { radio->client->addr, 0, sizeof(u16) * WRITE_REG_NUM,
117 for (i = 0; i < WRITE_REG_NUM; i++)
118 buf[i] = __cpu_to_be16(radio->registers[WRITE_INDEX(i)]);
120 if (i2c_transfer(radio->client->adapter, msgs, 1) != 1)
128 /**************************************************************************
129 * General Driver Functions - ENTIRE REGISTERS
130 **************************************************************************/
133 * si470x_get_all_registers - read entire registers
135 static int si470x_get_all_registers(struct si470x_device *radio)
138 u16 buf[READ_REG_NUM];
139 struct i2c_msg msgs[1] = {
140 { radio->client->addr, I2C_M_RD, sizeof(u16) * READ_REG_NUM,
144 if (i2c_transfer(radio->client->adapter, msgs, 1) != 1)
147 for (i = 0; i < READ_REG_NUM; i++)
148 radio->registers[i] = __be16_to_cpu(buf[READ_INDEX(i)]);
155 /**************************************************************************
156 * General Driver Functions - DISCONNECT_CHECK
157 **************************************************************************/
160 * si470x_disconnect_check - check whether radio disconnects
162 int si470x_disconnect_check(struct si470x_device *radio)
169 /**************************************************************************
170 * File Operations Interface
171 **************************************************************************/
174 * si470x_fops_open - file open
176 static int si470x_fops_open(struct file *file)
178 struct si470x_device *radio = video_drvdata(file);
181 mutex_lock(&radio->lock);
184 if (radio->users == 1)
186 retval = si470x_start(radio);
188 mutex_unlock(&radio->lock);
195 * si470x_fops_release - file release
197 static int si470x_fops_release(struct file *file)
199 struct si470x_device *radio = video_drvdata(file);
206 mutex_lock(&radio->lock);
208 if (radio->users == 0)
210 retval = si470x_stop(radio);
212 mutex_unlock(&radio->lock);
219 * si470x_fops - file operations interface
221 const struct v4l2_file_operations si470x_fops = {
222 .owner = THIS_MODULE,
223 .ioctl = video_ioctl2,
224 .open = si470x_fops_open,
225 .release = si470x_fops_release,
230 /**************************************************************************
231 * Video4Linux Interface
232 **************************************************************************/
235 * si470x_vidioc_querycap - query device capabilities
237 int si470x_vidioc_querycap(struct file *file, void *priv,
238 struct v4l2_capability *capability)
240 strlcpy(capability->driver, DRIVER_NAME, sizeof(capability->driver));
241 strlcpy(capability->card, DRIVER_CARD, sizeof(capability->card));
242 capability->version = DRIVER_KERNEL_VERSION;
243 capability->capabilities = V4L2_CAP_HW_FREQ_SEEK |
244 V4L2_CAP_TUNER | V4L2_CAP_RADIO;
251 /**************************************************************************
253 **************************************************************************/
256 * si470x_i2c_probe - probe for the device
258 static int __devinit si470x_i2c_probe(struct i2c_client *client,
259 const struct i2c_device_id *id)
261 struct si470x_device *radio;
263 unsigned char version_warning = 0;
265 /* private data allocation and initialization */
266 radio = kzalloc(sizeof(struct si470x_device), GFP_KERNEL);
272 radio->client = client;
273 mutex_init(&radio->lock);
275 /* video device allocation and initialization */
276 radio->videodev = video_device_alloc();
277 if (!radio->videodev) {
281 memcpy(radio->videodev, &si470x_viddev_template,
282 sizeof(si470x_viddev_template));
283 video_set_drvdata(radio->videodev, radio);
285 /* power up : need 110ms */
286 radio->registers[POWERCFG] = POWERCFG_ENABLE;
287 if (si470x_set_register(radio, POWERCFG) < 0) {
293 /* get device and chip versions */
294 if (si470x_get_all_registers(radio) < 0) {
298 dev_info(&client->dev, "DeviceID=0x%4.4hx ChipID=0x%4.4hx\n",
299 radio->registers[DEVICEID], radio->registers[CHIPID]);
300 if ((radio->registers[CHIPID] & CHIPID_FIRMWARE) < RADIO_FW_VERSION) {
301 dev_warn(&client->dev,
302 "This driver is known to work with "
303 "firmware version %hu,\n", RADIO_FW_VERSION);
304 dev_warn(&client->dev,
305 "but the device has firmware version %hu.\n",
306 radio->registers[CHIPID] & CHIPID_FIRMWARE);
310 /* give out version warning */
311 if (version_warning == 1) {
312 dev_warn(&client->dev,
313 "If you have some trouble using this driver,\n");
314 dev_warn(&client->dev,
315 "please report to V4L ML at "
316 "linux-media@vger.kernel.org\n");
319 /* set initial frequency */
320 si470x_set_freq(radio, 87.5 * FREQ_MUL); /* available in all regions */
322 /* register video device */
323 retval = video_register_device(radio->videodev, VFL_TYPE_RADIO,
326 dev_warn(&client->dev, "Could not register video device\n");
329 i2c_set_clientdata(client, radio);
334 video_device_release(radio->videodev);
343 * si470x_i2c_remove - remove the device
345 static __devexit int si470x_i2c_remove(struct i2c_client *client)
347 struct si470x_device *radio = i2c_get_clientdata(client);
349 video_unregister_device(radio->videodev);
351 i2c_set_clientdata(client, NULL);
358 * si470x_i2c_driver - i2c driver interface
360 static struct i2c_driver si470x_i2c_driver = {
363 .owner = THIS_MODULE,
365 .probe = si470x_i2c_probe,
366 .remove = __devexit_p(si470x_i2c_remove),
367 .id_table = si470x_i2c_id,
372 /**************************************************************************
374 **************************************************************************/
377 * si470x_i2c_init - module init
379 static int __init si470x_i2c_init(void)
381 printk(KERN_INFO DRIVER_DESC ", Version " DRIVER_VERSION "\n");
382 return i2c_add_driver(&si470x_i2c_driver);
387 * si470x_i2c_exit - module exit
389 static void __exit si470x_i2c_exit(void)
391 i2c_del_driver(&si470x_i2c_driver);
395 module_init(si470x_i2c_init);
396 module_exit(si470x_i2c_exit);
398 MODULE_LICENSE("GPL");
399 MODULE_AUTHOR(DRIVER_AUTHOR);
400 MODULE_DESCRIPTION(DRIVER_DESC);
401 MODULE_VERSION(DRIVER_VERSION);