blob: 4816a6d501c64dcefff73179f29e68c5474398f2 [file] [log] [blame]
Joonyoung Shimcc35bbd2009-08-09 14:23:35 -03001/*
2 * drivers/media/radio/si470x/radio-si470x-i2c.c
3 *
4 * I2C driver for radios with Silicon Labs Si470x FM Radio Receivers
5 *
Tobias Lorenz9dcb79c2009-08-10 18:44:14 -03006 * Copyright (c) 2009 Samsung Electronics Co.Ltd
Joonyoung Shimcc35bbd2009-08-09 14:23:35 -03007 * Author: Joonyoung Shim <jy0922.shim@samsung.com>
8 *
Tobias Lorenz9dcb79c2009-08-10 18:44:14 -03009 * 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.
Joonyoung Shimcc35bbd2009-08-09 14:23:35 -030013 *
Tobias Lorenz9dcb79c2009-08-10 18:44:14 -030014 * 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.
Joonyoung Shimcc35bbd2009-08-09 14:23:35 -030018 *
Tobias Lorenz9dcb79c2009-08-10 18:44:14 -030019 * 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
Joonyoung Shimcc35bbd2009-08-09 14:23:35 -030022 */
23
Tobias Lorenz9dcb79c2009-08-10 18:44:14 -030024
25/*
26 * ToDo:
27 * - RDS support
28 */
29
30
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"
37
38/* kernel includes */
Joonyoung Shimcc35bbd2009-08-09 14:23:35 -030039#include <linux/i2c.h>
40#include <linux/delay.h>
41
42#include "radio-si470x.h"
43
Joonyoung Shimcc35bbd2009-08-09 14:23:35 -030044
Tobias Lorenz9dcb79c2009-08-10 18:44:14 -030045/* I2C Device ID List */
46static const struct i2c_device_id si470x_i2c_id[] = {
47 /* Generic Entry */
48 { "si470x", 0 },
49 /* Terminating entry */
50 { }
51};
52MODULE_DEVICE_TABLE(i2c, si470x_i2c_id);
53
54
55
56/**************************************************************************
57 * Module Parameters
58 **************************************************************************/
59
60/* Radio Nr */
61static int radio_nr = -1;
62module_param(radio_nr, int, 0444);
63MODULE_PARM_DESC(radio_nr, "Radio Nr");
64
65
66
67/**************************************************************************
68 * I2C Definitions
69 **************************************************************************/
70
71/* Write starts with the upper byte of register 0x02 */
72#define WRITE_REG_NUM 8
73#define WRITE_INDEX(i) (i + 0x02)
74
75/* Read starts with the upper byte of register 0x0a */
Joonyoung Shimcc35bbd2009-08-09 14:23:35 -030076#define READ_REG_NUM RADIO_REGISTER_NUM
77#define READ_INDEX(i) ((i + RADIO_REGISTER_NUM - 0x0a) % READ_REG_NUM)
78
Joonyoung Shimcc35bbd2009-08-09 14:23:35 -030079
Joonyoung Shimcc35bbd2009-08-09 14:23:35 -030080
Tobias Lorenz9dcb79c2009-08-10 18:44:14 -030081/**************************************************************************
82 * General Driver Functions - REGISTERs
83 **************************************************************************/
Joonyoung Shimcc35bbd2009-08-09 14:23:35 -030084
Tobias Lorenz9dcb79c2009-08-10 18:44:14 -030085/*
86 * si470x_get_register - read register
87 */
Joonyoung Shimcc35bbd2009-08-09 14:23:35 -030088int si470x_get_register(struct si470x_device *radio, int regnr)
89{
90 u16 buf[READ_REG_NUM];
91 struct i2c_msg msgs[1] = {
92 { radio->client->addr, I2C_M_RD, sizeof(u16) * READ_REG_NUM,
93 (void *)buf },
94 };
95
96 if (i2c_transfer(radio->client->adapter, msgs, 1) != 1)
97 return -EIO;
98
99 radio->registers[regnr] = __be16_to_cpu(buf[READ_INDEX(regnr)]);
100
101 return 0;
102}
103
Joonyoung Shimcc35bbd2009-08-09 14:23:35 -0300104
Tobias Lorenz9dcb79c2009-08-10 18:44:14 -0300105/*
106 * si470x_set_register - write register
107 */
Joonyoung Shimcc35bbd2009-08-09 14:23:35 -0300108int si470x_set_register(struct si470x_device *radio, int regnr)
109{
110 int i;
111 u16 buf[WRITE_REG_NUM];
112 struct i2c_msg msgs[1] = {
113 { radio->client->addr, 0, sizeof(u16) * WRITE_REG_NUM,
114 (void *)buf },
115 };
116
117 for (i = 0; i < WRITE_REG_NUM; i++)
118 buf[i] = __cpu_to_be16(radio->registers[WRITE_INDEX(i)]);
119
120 if (i2c_transfer(radio->client->adapter, msgs, 1) != 1)
121 return -EIO;
122
123 return 0;
124}
125
Tobias Lorenz9dcb79c2009-08-10 18:44:14 -0300126
127
128/**************************************************************************
129 * General Driver Functions - ENTIRE REGISTERS
130 **************************************************************************/
131
132/*
133 * si470x_get_all_registers - read entire registers
134 */
135static int si470x_get_all_registers(struct si470x_device *radio)
136{
137 int i;
138 u16 buf[READ_REG_NUM];
139 struct i2c_msg msgs[1] = {
140 { radio->client->addr, I2C_M_RD, sizeof(u16) * READ_REG_NUM,
141 (void *)buf },
142 };
143
144 if (i2c_transfer(radio->client->adapter, msgs, 1) != 1)
145 return -EIO;
146
147 for (i = 0; i < READ_REG_NUM; i++)
148 radio->registers[i] = __be16_to_cpu(buf[READ_INDEX(i)]);
149
150 return 0;
151}
152
153
154
155/**************************************************************************
156 * General Driver Functions - DISCONNECT_CHECK
157 **************************************************************************/
158
159/*
160 * si470x_disconnect_check - check whether radio disconnects
161 */
Joonyoung Shimcc35bbd2009-08-09 14:23:35 -0300162int si470x_disconnect_check(struct si470x_device *radio)
163{
164 return 0;
165}
166
Tobias Lorenz9dcb79c2009-08-10 18:44:14 -0300167
168
169/**************************************************************************
170 * File Operations Interface
171 **************************************************************************/
172
173/*
174 * si470x_fops_open - file open
175 */
Joonyoung Shim1aa925c2009-12-10 16:49:34 -0300176int si470x_fops_open(struct file *file)
Joonyoung Shimcc35bbd2009-08-09 14:23:35 -0300177{
178 struct si470x_device *radio = video_drvdata(file);
179 int retval = 0;
180
181 mutex_lock(&radio->lock);
182 radio->users++;
183
184 if (radio->users == 1)
185 /* start radio */
186 retval = si470x_start(radio);
Tobias Lorenz9dcb79c2009-08-10 18:44:14 -0300187
Joonyoung Shimcc35bbd2009-08-09 14:23:35 -0300188 mutex_unlock(&radio->lock);
189
190 return retval;
191}
192
Tobias Lorenz9dcb79c2009-08-10 18:44:14 -0300193
194/*
195 * si470x_fops_release - file release
196 */
Joonyoung Shim1aa925c2009-12-10 16:49:34 -0300197int si470x_fops_release(struct file *file)
Joonyoung Shimcc35bbd2009-08-09 14:23:35 -0300198{
199 struct si470x_device *radio = video_drvdata(file);
200 int retval = 0;
201
202 /* safety check */
203 if (!radio)
204 return -ENODEV;
205
206 mutex_lock(&radio->lock);
207 radio->users--;
208 if (radio->users == 0)
209 /* stop radio */
210 retval = si470x_stop(radio);
Tobias Lorenz9dcb79c2009-08-10 18:44:14 -0300211
Joonyoung Shimcc35bbd2009-08-09 14:23:35 -0300212 mutex_unlock(&radio->lock);
213
214 return retval;
215}
216
Tobias Lorenz9dcb79c2009-08-10 18:44:14 -0300217
Tobias Lorenz9dcb79c2009-08-10 18:44:14 -0300218
219/**************************************************************************
220 * Video4Linux Interface
221 **************************************************************************/
222
223/*
224 * si470x_vidioc_querycap - query device capabilities
225 */
Joonyoung Shimcc35bbd2009-08-09 14:23:35 -0300226int si470x_vidioc_querycap(struct file *file, void *priv,
227 struct v4l2_capability *capability)
228{
229 strlcpy(capability->driver, DRIVER_NAME, sizeof(capability->driver));
230 strlcpy(capability->card, DRIVER_CARD, sizeof(capability->card));
231 capability->version = DRIVER_KERNEL_VERSION;
232 capability->capabilities = V4L2_CAP_HW_FREQ_SEEK |
233 V4L2_CAP_TUNER | V4L2_CAP_RADIO;
234
235 return 0;
236}
237
Tobias Lorenz9dcb79c2009-08-10 18:44:14 -0300238
239
240/**************************************************************************
241 * I2C Interface
242 **************************************************************************/
243
244/*
245 * si470x_i2c_probe - probe for the device
246 */
Joonyoung Shimcc35bbd2009-08-09 14:23:35 -0300247static int __devinit si470x_i2c_probe(struct i2c_client *client,
248 const struct i2c_device_id *id)
249{
250 struct si470x_device *radio;
251 int retval = 0;
Tobias Lorenz9dcb79c2009-08-10 18:44:14 -0300252 unsigned char version_warning = 0;
Joonyoung Shimcc35bbd2009-08-09 14:23:35 -0300253
254 /* private data allocation and initialization */
255 radio = kzalloc(sizeof(struct si470x_device), GFP_KERNEL);
256 if (!radio) {
257 retval = -ENOMEM;
258 goto err_initial;
259 }
Joonyoung Shimcc35bbd2009-08-09 14:23:35 -0300260 radio->users = 0;
Tobias Lorenz9dcb79c2009-08-10 18:44:14 -0300261 radio->client = client;
Joonyoung Shimcc35bbd2009-08-09 14:23:35 -0300262 mutex_init(&radio->lock);
263
264 /* video device allocation and initialization */
265 radio->videodev = video_device_alloc();
266 if (!radio->videodev) {
267 retval = -ENOMEM;
268 goto err_radio;
269 }
270 memcpy(radio->videodev, &si470x_viddev_template,
271 sizeof(si470x_viddev_template));
272 video_set_drvdata(radio->videodev, radio);
273
274 /* power up : need 110ms */
275 radio->registers[POWERCFG] = POWERCFG_ENABLE;
276 if (si470x_set_register(radio, POWERCFG) < 0) {
277 retval = -EIO;
278 goto err_all;
279 }
280 msleep(110);
281
Tobias Lorenz9dcb79c2009-08-10 18:44:14 -0300282 /* get device and chip versions */
Joonyoung Shimcc35bbd2009-08-09 14:23:35 -0300283 if (si470x_get_all_registers(radio) < 0) {
284 retval = -EIO;
Tobias Lorenz9dcb79c2009-08-10 18:44:14 -0300285 goto err_video;
Joonyoung Shimcc35bbd2009-08-09 14:23:35 -0300286 }
287 dev_info(&client->dev, "DeviceID=0x%4.4hx ChipID=0x%4.4hx\n",
288 radio->registers[DEVICEID], radio->registers[CHIPID]);
Tobias Lorenz9dcb79c2009-08-10 18:44:14 -0300289 if ((radio->registers[CHIPID] & CHIPID_FIRMWARE) < RADIO_FW_VERSION) {
290 dev_warn(&client->dev,
291 "This driver is known to work with "
292 "firmware version %hu,\n", RADIO_FW_VERSION);
293 dev_warn(&client->dev,
294 "but the device has firmware version %hu.\n",
295 radio->registers[CHIPID] & CHIPID_FIRMWARE);
296 version_warning = 1;
297 }
298
299 /* give out version warning */
300 if (version_warning == 1) {
301 dev_warn(&client->dev,
302 "If you have some trouble using this driver,\n");
303 dev_warn(&client->dev,
304 "please report to V4L ML at "
305 "linux-media@vger.kernel.org\n");
306 }
Joonyoung Shimcc35bbd2009-08-09 14:23:35 -0300307
308 /* set initial frequency */
309 si470x_set_freq(radio, 87.5 * FREQ_MUL); /* available in all regions */
310
311 /* register video device */
Tobias Lorenz9dcb79c2009-08-10 18:44:14 -0300312 retval = video_register_device(radio->videodev, VFL_TYPE_RADIO,
313 radio_nr);
Joonyoung Shimcc35bbd2009-08-09 14:23:35 -0300314 if (retval) {
315 dev_warn(&client->dev, "Could not register video device\n");
316 goto err_all;
317 }
Joonyoung Shimcc35bbd2009-08-09 14:23:35 -0300318 i2c_set_clientdata(client, radio);
319
320 return 0;
321err_all:
Tobias Lorenz9dcb79c2009-08-10 18:44:14 -0300322err_video:
Joonyoung Shimcc35bbd2009-08-09 14:23:35 -0300323 video_device_release(radio->videodev);
324err_radio:
325 kfree(radio);
326err_initial:
327 return retval;
328}
329
Tobias Lorenz9dcb79c2009-08-10 18:44:14 -0300330
331/*
332 * si470x_i2c_remove - remove the device
333 */
Joonyoung Shimcc35bbd2009-08-09 14:23:35 -0300334static __devexit int si470x_i2c_remove(struct i2c_client *client)
335{
336 struct si470x_device *radio = i2c_get_clientdata(client);
337
338 video_unregister_device(radio->videodev);
339 kfree(radio);
340 i2c_set_clientdata(client, NULL);
341
342 return 0;
343}
344
Joonyoung Shimcc35bbd2009-08-09 14:23:35 -0300345
Tobias Lorenz9dcb79c2009-08-10 18:44:14 -0300346/*
347 * si470x_i2c_driver - i2c driver interface
348 */
Joonyoung Shimcc35bbd2009-08-09 14:23:35 -0300349static struct i2c_driver si470x_i2c_driver = {
350 .driver = {
Tobias Lorenz9dcb79c2009-08-10 18:44:14 -0300351 .name = "si470x",
352 .owner = THIS_MODULE,
Joonyoung Shimcc35bbd2009-08-09 14:23:35 -0300353 },
Tobias Lorenz9dcb79c2009-08-10 18:44:14 -0300354 .probe = si470x_i2c_probe,
355 .remove = __devexit_p(si470x_i2c_remove),
356 .id_table = si470x_i2c_id,
Joonyoung Shimcc35bbd2009-08-09 14:23:35 -0300357};
358
Tobias Lorenz9dcb79c2009-08-10 18:44:14 -0300359
360
361/**************************************************************************
362 * Module Interface
363 **************************************************************************/
364
365/*
366 * si470x_i2c_init - module init
367 */
Joonyoung Shimcc35bbd2009-08-09 14:23:35 -0300368static int __init si470x_i2c_init(void)
369{
Tobias Lorenz9dcb79c2009-08-10 18:44:14 -0300370 printk(KERN_INFO DRIVER_DESC ", Version " DRIVER_VERSION "\n");
Joonyoung Shimcc35bbd2009-08-09 14:23:35 -0300371 return i2c_add_driver(&si470x_i2c_driver);
372}
Joonyoung Shimcc35bbd2009-08-09 14:23:35 -0300373
Tobias Lorenz9dcb79c2009-08-10 18:44:14 -0300374
375/*
376 * si470x_i2c_exit - module exit
377 */
Joonyoung Shimcc35bbd2009-08-09 14:23:35 -0300378static void __exit si470x_i2c_exit(void)
379{
380 i2c_del_driver(&si470x_i2c_driver);
381}
Tobias Lorenz9dcb79c2009-08-10 18:44:14 -0300382
383
384module_init(si470x_i2c_init);
Joonyoung Shimcc35bbd2009-08-09 14:23:35 -0300385module_exit(si470x_i2c_exit);
386
Joonyoung Shimcc35bbd2009-08-09 14:23:35 -0300387MODULE_LICENSE("GPL");
Tobias Lorenz9dcb79c2009-08-10 18:44:14 -0300388MODULE_AUTHOR(DRIVER_AUTHOR);
389MODULE_DESCRIPTION(DRIVER_DESC);
390MODULE_VERSION(DRIVER_VERSION);