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