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#define DRIVER_AUTHOR "Joonyoung Shim <jy0922.shim@samsung.com>";
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.2"
30
31
32#include <linux/i2c.h>
33#include <linux/slab.h>
34#include <linux/delay.h>
35#include <linux/interrupt.h>
36
37#include "radio-si470x.h"
38
39
40
41static const struct i2c_device_id si470x_i2c_id[] = {
42
43 { "si470x", 0 },
44
45 { }
46};
47MODULE_DEVICE_TABLE(i2c, si470x_i2c_id);
48
49
50
51
52
53
54
55
56static int radio_nr = -1;
57module_param(radio_nr, int, 0444);
58MODULE_PARM_DESC(radio_nr, "Radio Nr");
59
60
61static unsigned int rds_buf = 100;
62module_param(rds_buf, uint, 0444);
63MODULE_PARM_DESC(rds_buf, "RDS buffer entries: *100*");
64
65
66static unsigned short max_rds_errors = 1;
67
68
69
70
71module_param(max_rds_errors, ushort, 0644);
72MODULE_PARM_DESC(max_rds_errors, "RDS maximum block errors: *1*");
73
74
75
76
77
78
79
80
81#define WRITE_REG_NUM 8
82#define WRITE_INDEX(i) (i + 0x02)
83
84
85#define READ_REG_NUM RADIO_REGISTER_NUM
86#define READ_INDEX(i) ((i + RADIO_REGISTER_NUM - 0x0a) % READ_REG_NUM)
87
88
89
90
91
92
93
94
95
96
97int si470x_get_register(struct si470x_device *radio, int regnr)
98{
99 u16 buf[READ_REG_NUM];
100 struct i2c_msg msgs[1] = {
101 {
102 .addr = radio->client->addr,
103 .flags = I2C_M_RD,
104 .len = sizeof(u16) * READ_REG_NUM,
105 .buf = (void *)buf
106 },
107 };
108
109 if (i2c_transfer(radio->client->adapter, msgs, 1) != 1)
110 return -EIO;
111
112 radio->registers[regnr] = __be16_to_cpu(buf[READ_INDEX(regnr)]);
113
114 return 0;
115}
116
117
118
119
120
121int si470x_set_register(struct si470x_device *radio, int regnr)
122{
123 int i;
124 u16 buf[WRITE_REG_NUM];
125 struct i2c_msg msgs[1] = {
126 {
127 .addr = radio->client->addr,
128 .len = sizeof(u16) * WRITE_REG_NUM,
129 .buf = (void *)buf
130 },
131 };
132
133 for (i = 0; i < WRITE_REG_NUM; i++)
134 buf[i] = __cpu_to_be16(radio->registers[WRITE_INDEX(i)]);
135
136 if (i2c_transfer(radio->client->adapter, msgs, 1) != 1)
137 return -EIO;
138
139 return 0;
140}
141
142
143
144
145
146
147
148
149
150
151static int si470x_get_all_registers(struct si470x_device *radio)
152{
153 int i;
154 u16 buf[READ_REG_NUM];
155 struct i2c_msg msgs[1] = {
156 {
157 .addr = radio->client->addr,
158 .flags = I2C_M_RD,
159 .len = sizeof(u16) * READ_REG_NUM,
160 .buf = (void *)buf
161 },
162 };
163
164 if (i2c_transfer(radio->client->adapter, msgs, 1) != 1)
165 return -EIO;
166
167 for (i = 0; i < READ_REG_NUM; i++)
168 radio->registers[i] = __be16_to_cpu(buf[READ_INDEX(i)]);
169
170 return 0;
171}
172
173
174
175
176
177
178
179
180
181
182int si470x_fops_open(struct file *file)
183{
184 struct si470x_device *radio = video_drvdata(file);
185 int retval = v4l2_fh_open(file);
186
187 if (retval)
188 return retval;
189
190 if (v4l2_fh_is_singular_file(file)) {
191
192 retval = si470x_start(radio);
193 if (retval < 0)
194 goto done;
195
196
197 radio->registers[SYSCONFIG1] |= SYSCONFIG1_RDSIEN;
198 radio->registers[SYSCONFIG1] |= SYSCONFIG1_STCIEN;
199 radio->registers[SYSCONFIG1] &= ~SYSCONFIG1_GPIO2;
200 radio->registers[SYSCONFIG1] |= 0x1 << 2;
201 retval = si470x_set_register(radio, SYSCONFIG1);
202 }
203
204done:
205 if (retval)
206 v4l2_fh_release(file);
207 return retval;
208}
209
210
211
212
213
214int si470x_fops_release(struct file *file)
215{
216 struct si470x_device *radio = video_drvdata(file);
217
218 if (v4l2_fh_is_singular_file(file))
219
220 si470x_stop(radio);
221
222 return v4l2_fh_release(file);
223}
224
225
226
227
228
229
230
231
232
233
234int si470x_vidioc_querycap(struct file *file, void *priv,
235 struct v4l2_capability *capability)
236{
237 strlcpy(capability->driver, DRIVER_NAME, sizeof(capability->driver));
238 strlcpy(capability->card, DRIVER_CARD, sizeof(capability->card));
239 capability->device_caps = V4L2_CAP_HW_FREQ_SEEK | V4L2_CAP_READWRITE |
240 V4L2_CAP_TUNER | V4L2_CAP_RADIO | V4L2_CAP_RDS_CAPTURE;
241 capability->capabilities = capability->device_caps | V4L2_CAP_DEVICE_CAPS;
242
243 return 0;
244}
245
246
247
248
249
250
251
252
253
254
255static irqreturn_t si470x_i2c_interrupt(int irq, void *dev_id)
256{
257 struct si470x_device *radio = dev_id;
258 unsigned char regnr;
259 unsigned char blocknum;
260 unsigned short bler;
261 unsigned short rds;
262 unsigned char tmpbuf[3];
263 int retval = 0;
264
265
266 retval = si470x_get_register(radio, STATUSRSSI);
267 if (retval < 0)
268 goto end;
269
270 if (radio->registers[STATUSRSSI] & STATUSRSSI_STC)
271 complete(&radio->completion);
272
273
274 if ((radio->registers[SYSCONFIG1] & SYSCONFIG1_RDS) == 0)
275 goto end;
276
277
278 for (regnr = 1; regnr < RDS_REGISTER_NUM; regnr++) {
279 retval = si470x_get_register(radio, STATUSRSSI + regnr);
280 if (retval < 0)
281 goto end;
282 }
283
284
285 if ((radio->registers[STATUSRSSI] & STATUSRSSI_RDSR) == 0)
286
287 goto end;
288
289 for (blocknum = 0; blocknum < 4; blocknum++) {
290 switch (blocknum) {
291 default:
292 bler = (radio->registers[STATUSRSSI] &
293 STATUSRSSI_BLERA) >> 9;
294 rds = radio->registers[RDSA];
295 break;
296 case 1:
297 bler = (radio->registers[READCHAN] &
298 READCHAN_BLERB) >> 14;
299 rds = radio->registers[RDSB];
300 break;
301 case 2:
302 bler = (radio->registers[READCHAN] &
303 READCHAN_BLERC) >> 12;
304 rds = radio->registers[RDSC];
305 break;
306 case 3:
307 bler = (radio->registers[READCHAN] &
308 READCHAN_BLERD) >> 10;
309 rds = radio->registers[RDSD];
310 break;
311 }
312
313
314 put_unaligned_le16(rds, &tmpbuf);
315 tmpbuf[2] = blocknum;
316 tmpbuf[2] |= blocknum << 3;
317 if (bler > max_rds_errors)
318 tmpbuf[2] |= 0x80;
319 else if (bler > 0)
320 tmpbuf[2] |= 0x40;
321
322
323 memcpy(&radio->buffer[radio->wr_index], &tmpbuf, 3);
324 radio->wr_index += 3;
325
326
327 if (radio->wr_index >= radio->buf_size)
328 radio->wr_index = 0;
329
330
331 if (radio->wr_index == radio->rd_index) {
332
333 radio->rd_index += 3;
334 if (radio->rd_index >= radio->buf_size)
335 radio->rd_index = 0;
336 }
337 }
338
339 if (radio->wr_index != radio->rd_index)
340 wake_up_interruptible(&radio->read_queue);
341
342end:
343 return IRQ_HANDLED;
344}
345
346
347
348
349
350static int si470x_i2c_probe(struct i2c_client *client,
351 const struct i2c_device_id *id)
352{
353 struct si470x_device *radio;
354 int retval = 0;
355 unsigned char version_warning = 0;
356
357
358 radio = kzalloc(sizeof(struct si470x_device), GFP_KERNEL);
359 if (!radio) {
360 retval = -ENOMEM;
361 goto err_initial;
362 }
363
364 radio->client = client;
365 radio->band = 1;
366 mutex_init(&radio->lock);
367 init_completion(&radio->completion);
368
369
370 radio->videodev = si470x_viddev_template;
371 video_set_drvdata(&radio->videodev, radio);
372
373
374 radio->registers[POWERCFG] = POWERCFG_ENABLE;
375 if (si470x_set_register(radio, POWERCFG) < 0) {
376 retval = -EIO;
377 goto err_radio;
378 }
379 msleep(110);
380
381
382 if (si470x_get_all_registers(radio) < 0) {
383 retval = -EIO;
384 goto err_radio;
385 }
386 dev_info(&client->dev, "DeviceID=0x%4.4hx ChipID=0x%4.4hx\n",
387 radio->registers[DEVICEID], radio->registers[CHIPID]);
388 if ((radio->registers[CHIPID] & CHIPID_FIRMWARE) < RADIO_FW_VERSION) {
389 dev_warn(&client->dev,
390 "This driver is known to work with "
391 "firmware version %hu,\n", RADIO_FW_VERSION);
392 dev_warn(&client->dev,
393 "but the device has firmware version %hu.\n",
394 radio->registers[CHIPID] & CHIPID_FIRMWARE);
395 version_warning = 1;
396 }
397
398
399 if (version_warning == 1) {
400 dev_warn(&client->dev,
401 "If you have some trouble using this driver,\n");
402 dev_warn(&client->dev,
403 "please report to V4L ML at "
404 "linux-media@vger.kernel.org\n");
405 }
406
407
408 si470x_set_freq(radio, 87.5 * FREQ_MUL);
409
410
411 radio->buf_size = rds_buf * 3;
412 radio->buffer = kmalloc(radio->buf_size, GFP_KERNEL);
413 if (!radio->buffer) {
414 retval = -EIO;
415 goto err_radio;
416 }
417
418
419 radio->wr_index = 0;
420 radio->rd_index = 0;
421 init_waitqueue_head(&radio->read_queue);
422
423 retval = request_threaded_irq(client->irq, NULL, si470x_i2c_interrupt,
424 IRQF_TRIGGER_FALLING, DRIVER_NAME, radio);
425 if (retval) {
426 dev_err(&client->dev, "Failed to register interrupt\n");
427 goto err_rds;
428 }
429
430
431 retval = video_register_device(&radio->videodev, VFL_TYPE_RADIO,
432 radio_nr);
433 if (retval) {
434 dev_warn(&client->dev, "Could not register video device\n");
435 goto err_all;
436 }
437 i2c_set_clientdata(client, radio);
438
439 return 0;
440err_all:
441 free_irq(client->irq, radio);
442err_rds:
443 kfree(radio->buffer);
444err_radio:
445 kfree(radio);
446err_initial:
447 return retval;
448}
449
450
451
452
453
454static int si470x_i2c_remove(struct i2c_client *client)
455{
456 struct si470x_device *radio = i2c_get_clientdata(client);
457
458 free_irq(client->irq, radio);
459 video_unregister_device(&radio->videodev);
460 kfree(radio);
461
462 return 0;
463}
464
465
466#ifdef CONFIG_PM_SLEEP
467
468
469
470static int si470x_i2c_suspend(struct device *dev)
471{
472 struct i2c_client *client = to_i2c_client(dev);
473 struct si470x_device *radio = i2c_get_clientdata(client);
474
475
476 radio->registers[POWERCFG] |= POWERCFG_DISABLE;
477 if (si470x_set_register(radio, POWERCFG) < 0)
478 return -EIO;
479
480 return 0;
481}
482
483
484
485
486
487static int si470x_i2c_resume(struct device *dev)
488{
489 struct i2c_client *client = to_i2c_client(dev);
490 struct si470x_device *radio = i2c_get_clientdata(client);
491
492
493 radio->registers[POWERCFG] |= POWERCFG_ENABLE;
494 if (si470x_set_register(radio, POWERCFG) < 0)
495 return -EIO;
496 msleep(110);
497
498 return 0;
499}
500
501static SIMPLE_DEV_PM_OPS(si470x_i2c_pm, si470x_i2c_suspend, si470x_i2c_resume);
502#endif
503
504
505
506
507
508static struct i2c_driver si470x_i2c_driver = {
509 .driver = {
510 .name = "si470x",
511 .owner = THIS_MODULE,
512#ifdef CONFIG_PM_SLEEP
513 .pm = &si470x_i2c_pm,
514#endif
515 },
516 .probe = si470x_i2c_probe,
517 .remove = si470x_i2c_remove,
518 .id_table = si470x_i2c_id,
519};
520
521module_i2c_driver(si470x_i2c_driver);
522
523MODULE_LICENSE("GPL");
524MODULE_AUTHOR(DRIVER_AUTHOR);
525MODULE_DESCRIPTION(DRIVER_DESC);
526MODULE_VERSION(DRIVER_VERSION);
527