1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
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
39#include <linux/i2c.h>
40#include <linux/delay.h>
41
42#include "radio-si470x.h"
43
44
45
46static const struct i2c_device_id si470x_i2c_id[] = {
47
48 { "si470x", 0 },
49
50 { }
51};
52MODULE_DEVICE_TABLE(i2c, si470x_i2c_id);
53
54
55
56
57
58
59
60
61static int radio_nr = -1;
62module_param(radio_nr, int, 0444);
63MODULE_PARM_DESC(radio_nr, "Radio Nr");
64
65
66
67
68
69
70
71
72#define WRITE_REG_NUM 8
73#define WRITE_INDEX(i) (i + 0x02)
74
75
76#define READ_REG_NUM RADIO_REGISTER_NUM
77#define READ_INDEX(i) ((i + RADIO_REGISTER_NUM - 0x0a) % READ_REG_NUM)
78
79
80
81
82
83
84
85
86
87
88int 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
104
105
106
107
108int 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
126
127
128
129
130
131
132
133
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
157
158
159
160
161
162int si470x_disconnect_check(struct si470x_device *radio)
163{
164 return 0;
165}
166
167
168
169
170
171
172
173
174
175
176static int si470x_fops_open(struct file *file)
177{
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
186 retval = si470x_start(radio);
187
188 mutex_unlock(&radio->lock);
189
190 return retval;
191}
192
193
194
195
196
197static int si470x_fops_release(struct file *file)
198{
199 struct si470x_device *radio = video_drvdata(file);
200 int retval = 0;
201
202
203 if (!radio)
204 return -ENODEV;
205
206 mutex_lock(&radio->lock);
207 radio->users--;
208 if (radio->users == 0)
209
210 retval = si470x_stop(radio);
211
212 mutex_unlock(&radio->lock);
213
214 return retval;
215}
216
217
218
219
220
221const struct v4l2_file_operations si470x_fops = {
222 .owner = THIS_MODULE,
223 .ioctl = video_ioctl2,
224 .open = si470x_fops_open,
225 .release = si470x_fops_release,
226};
227
228
229
230
231
232
233
234
235
236
237int si470x_vidioc_querycap(struct file *file, void *priv,
238 struct v4l2_capability *capability)
239{
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;
245
246 return 0;
247}
248
249
250
251
252
253
254
255
256
257
258static int __devinit si470x_i2c_probe(struct i2c_client *client,
259 const struct i2c_device_id *id)
260{
261 struct si470x_device *radio;
262 int retval = 0;
263 unsigned char version_warning = 0;
264
265
266 radio = kzalloc(sizeof(struct si470x_device), GFP_KERNEL);
267 if (!radio) {
268 retval = -ENOMEM;
269 goto err_initial;
270 }
271 radio->users = 0;
272 radio->client = client;
273 mutex_init(&radio->lock);
274
275
276 radio->videodev = video_device_alloc();
277 if (!radio->videodev) {
278 retval = -ENOMEM;
279 goto err_radio;
280 }
281 memcpy(radio->videodev, &si470x_viddev_template,
282 sizeof(si470x_viddev_template));
283 video_set_drvdata(radio->videodev, radio);
284
285
286 radio->registers[POWERCFG] = POWERCFG_ENABLE;
287 if (si470x_set_register(radio, POWERCFG) < 0) {
288 retval = -EIO;
289 goto err_all;
290 }
291 msleep(110);
292
293
294 if (si470x_get_all_registers(radio) < 0) {
295 retval = -EIO;
296 goto err_video;
297 }
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);
307 version_warning = 1;
308 }
309
310
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");
317 }
318
319
320 si470x_set_freq(radio, 87.5 * FREQ_MUL);
321
322
323 retval = video_register_device(radio->videodev, VFL_TYPE_RADIO,
324 radio_nr);
325 if (retval) {
326 dev_warn(&client->dev, "Could not register video device\n");
327 goto err_all;
328 }
329 i2c_set_clientdata(client, radio);
330
331 return 0;
332err_all:
333err_video:
334 video_device_release(radio->videodev);
335err_radio:
336 kfree(radio);
337err_initial:
338 return retval;
339}
340
341
342
343
344
345static __devexit int si470x_i2c_remove(struct i2c_client *client)
346{
347 struct si470x_device *radio = i2c_get_clientdata(client);
348
349 video_unregister_device(radio->videodev);
350 kfree(radio);
351 i2c_set_clientdata(client, NULL);
352
353 return 0;
354}
355
356
357
358
359
360static struct i2c_driver si470x_i2c_driver = {
361 .driver = {
362 .name = "si470x",
363 .owner = THIS_MODULE,
364 },
365 .probe = si470x_i2c_probe,
366 .remove = __devexit_p(si470x_i2c_remove),
367 .id_table = si470x_i2c_id,
368};
369
370
371
372
373
374
375
376
377
378
379static int __init si470x_i2c_init(void)
380{
381 printk(KERN_INFO DRIVER_DESC ", Version " DRIVER_VERSION "\n");
382 return i2c_add_driver(&si470x_i2c_driver);
383}
384
385
386
387
388
389static void __exit si470x_i2c_exit(void)
390{
391 i2c_del_driver(&si470x_i2c_driver);
392}
393
394
395module_init(si470x_i2c_init);
396module_exit(si470x_i2c_exit);
397
398MODULE_LICENSE("GPL");
399MODULE_AUTHOR(DRIVER_AUTHOR);
400MODULE_DESCRIPTION(DRIVER_DESC);
401MODULE_VERSION(DRIVER_VERSION);
402