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 int si470x_get_register(struct si470x_device *radio, int regnr)
93 {
94 	u16 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 int si470x_set_register(struct si470x_device *radio, int regnr)
117 {
118 	int i;
119 	u16 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 	u16 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 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 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 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 	retval = v4l2_device_register(&client->dev, &radio->v4l2_dev);
365 	if (retval < 0) {
366 		dev_err(&client->dev, "couldn't register v4l2_device\n");
367 		goto err_radio;
368 	}
369 
370 	v4l2_ctrl_handler_init(&radio->hdl, 2);
371 	v4l2_ctrl_new_std(&radio->hdl, &si470x_ctrl_ops,
372 			V4L2_CID_AUDIO_MUTE, 0, 1, 1, 1);
373 	v4l2_ctrl_new_std(&radio->hdl, &si470x_ctrl_ops,
374 			V4L2_CID_AUDIO_VOLUME, 0, 15, 1, 15);
375 	if (radio->hdl.error) {
376 		retval = radio->hdl.error;
377 		dev_err(&client->dev, "couldn't register control\n");
378 		goto err_dev;
379 	}
380 
381 	/* video device initialization */
382 	radio->videodev = si470x_viddev_template;
383 	radio->videodev.ctrl_handler = &radio->hdl;
384 	radio->videodev.lock = &radio->lock;
385 	radio->videodev.v4l2_dev = &radio->v4l2_dev;
386 	radio->videodev.release = video_device_release_empty;
387 	video_set_drvdata(&radio->videodev, radio);
388 
389 	/* power up : need 110ms */
390 	radio->registers[POWERCFG] = POWERCFG_ENABLE;
391 	if (si470x_set_register(radio, POWERCFG) < 0) {
392 		retval = -EIO;
393 		goto err_ctrl;
394 	}
395 	msleep(110);
396 
397 	/* get device and chip versions */
398 	if (si470x_get_all_registers(radio) < 0) {
399 		retval = -EIO;
400 		goto err_ctrl;
401 	}
402 	dev_info(&client->dev, "DeviceID=0x%4.4hx ChipID=0x%4.4hx\n",
403 			radio->registers[DEVICEID], radio->registers[SI_CHIPID]);
404 	if ((radio->registers[SI_CHIPID] & SI_CHIPID_FIRMWARE) < RADIO_FW_VERSION) {
405 		dev_warn(&client->dev,
406 			"This driver is known to work with firmware version %hu,\n",
407 			RADIO_FW_VERSION);
408 		dev_warn(&client->dev,
409 			"but the device has firmware version %hu.\n",
410 			radio->registers[SI_CHIPID] & SI_CHIPID_FIRMWARE);
411 		version_warning = 1;
412 	}
413 
414 	/* give out version warning */
415 	if (version_warning == 1) {
416 		dev_warn(&client->dev,
417 			"If you have some trouble using this driver,\n");
418 		dev_warn(&client->dev,
419 			"please report to V4L ML at linux-media@vger.kernel.org\n");
420 	}
421 
422 	/* set initial frequency */
423 	si470x_set_freq(radio, 87.5 * FREQ_MUL); /* available in all regions */
424 
425 	/* rds buffer allocation */
426 	radio->buf_size = rds_buf * 3;
427 	radio->buffer = kmalloc(radio->buf_size, GFP_KERNEL);
428 	if (!radio->buffer) {
429 		retval = -EIO;
430 		goto err_ctrl;
431 	}
432 
433 	/* rds buffer configuration */
434 	radio->wr_index = 0;
435 	radio->rd_index = 0;
436 	init_waitqueue_head(&radio->read_queue);
437 
438 	retval = request_threaded_irq(client->irq, NULL, si470x_i2c_interrupt,
439 			IRQF_TRIGGER_FALLING | IRQF_ONESHOT, DRIVER_NAME,
440 			radio);
441 	if (retval) {
442 		dev_err(&client->dev, "Failed to register interrupt\n");
443 		goto err_rds;
444 	}
445 
446 	/* register video device */
447 	retval = video_register_device(&radio->videodev, VFL_TYPE_RADIO,
448 			radio_nr);
449 	if (retval) {
450 		dev_warn(&client->dev, "Could not register video device\n");
451 		goto err_all;
452 	}
453 	i2c_set_clientdata(client, radio);
454 
455 	return 0;
456 err_all:
457 	free_irq(client->irq, radio);
458 err_rds:
459 	kfree(radio->buffer);
460 err_ctrl:
461 	v4l2_ctrl_handler_free(&radio->hdl);
462 err_dev:
463 	v4l2_device_unregister(&radio->v4l2_dev);
464 err_radio:
465 	kfree(radio);
466 err_initial:
467 	return retval;
468 }
469 
470 
471 /*
472  * si470x_i2c_remove - remove the device
473  */
474 static int si470x_i2c_remove(struct i2c_client *client)
475 {
476 	struct si470x_device *radio = i2c_get_clientdata(client);
477 
478 	free_irq(client->irq, radio);
479 	video_unregister_device(&radio->videodev);
480 	kfree(radio);
481 
482 	return 0;
483 }
484 
485 
486 #ifdef CONFIG_PM_SLEEP
487 /*
488  * si470x_i2c_suspend - suspend the device
489  */
490 static int si470x_i2c_suspend(struct device *dev)
491 {
492 	struct i2c_client *client = to_i2c_client(dev);
493 	struct si470x_device *radio = i2c_get_clientdata(client);
494 
495 	/* power down */
496 	radio->registers[POWERCFG] |= POWERCFG_DISABLE;
497 	if (si470x_set_register(radio, POWERCFG) < 0)
498 		return -EIO;
499 
500 	return 0;
501 }
502 
503 
504 /*
505  * si470x_i2c_resume - resume the device
506  */
507 static int si470x_i2c_resume(struct device *dev)
508 {
509 	struct i2c_client *client = to_i2c_client(dev);
510 	struct si470x_device *radio = i2c_get_clientdata(client);
511 
512 	/* power up : need 110ms */
513 	radio->registers[POWERCFG] |= POWERCFG_ENABLE;
514 	if (si470x_set_register(radio, POWERCFG) < 0)
515 		return -EIO;
516 	msleep(110);
517 
518 	return 0;
519 }
520 
521 static SIMPLE_DEV_PM_OPS(si470x_i2c_pm, si470x_i2c_suspend, si470x_i2c_resume);
522 #endif
523 
524 
525 /*
526  * si470x_i2c_driver - i2c driver interface
527  */
528 static struct i2c_driver si470x_i2c_driver = {
529 	.driver = {
530 		.name		= "si470x",
531 #ifdef CONFIG_PM_SLEEP
532 		.pm		= &si470x_i2c_pm,
533 #endif
534 	},
535 	.probe			= si470x_i2c_probe,
536 	.remove			= si470x_i2c_remove,
537 	.id_table		= si470x_i2c_id,
538 };
539 
540 module_i2c_driver(si470x_i2c_driver);
541 
542 MODULE_LICENSE("GPL");
543 MODULE_AUTHOR(DRIVER_AUTHOR);
544 MODULE_DESCRIPTION(DRIVER_DESC);
545 MODULE_VERSION(DRIVER_VERSION);
546