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