xref: /openbmc/linux/drivers/media/radio/radio-tea5764.c (revision ce932d0c5589e9766e089c22c66890dfc48fbd94)
1 /*
2  * driver/media/radio/radio-tea5764.c
3  *
4  * Driver for TEA5764 radio chip for linux 2.6.
5  * This driver is for TEA5764 chip from NXP, used in EZX phones from Motorola.
6  * The I2C protocol is used for communicate with chip.
7  *
8  * Based in radio-tea5761.c Copyright (C) 2005 Nokia Corporation
9  *
10  *  Copyright (c) 2008 Fabio Belavenuto <belavenuto@gmail.com>
11  *
12  * This program is free software; you can redistribute it and/or modify
13  * it under the terms of the GNU General Public License as published by
14  * the Free Software Foundation; either version 2 of the License, or
15  * (at your option) any later version.
16  *
17  * This program is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20  * GNU General Public License for more details.
21  *
22  * You should have received a copy of the GNU General Public License
23  * along with this program; if not, write to the Free Software
24  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25  *
26  * History:
27  * 2008-12-06   Fabio Belavenuto <belavenuto@gmail.com>
28  *              initial code
29  *
30  * TODO:
31  *  add platform_data support for IRQs platform dependencies
32  *  add RDS support
33  */
34 #include <linux/kernel.h>
35 #include <linux/slab.h>
36 #include <linux/module.h>
37 #include <linux/init.h>			/* Initdata			*/
38 #include <linux/videodev2.h>		/* kernel radio structs		*/
39 #include <linux/i2c.h>			/* I2C				*/
40 #include <media/v4l2-common.h>
41 #include <media/v4l2-ioctl.h>
42 
43 #define DRIVER_VERSION	"0.0.2"
44 
45 #define DRIVER_AUTHOR	"Fabio Belavenuto <belavenuto@gmail.com>"
46 #define DRIVER_DESC	"A driver for the TEA5764 radio chip for EZX Phones."
47 
48 #define PINFO(format, ...)\
49 	printk(KERN_INFO KBUILD_MODNAME ": "\
50 		DRIVER_VERSION ": " format "\n", ## __VA_ARGS__)
51 #define PWARN(format, ...)\
52 	printk(KERN_WARNING KBUILD_MODNAME ": "\
53 		DRIVER_VERSION ": " format "\n", ## __VA_ARGS__)
54 # define PDEBUG(format, ...)\
55 	printk(KERN_DEBUG KBUILD_MODNAME ": "\
56 		DRIVER_VERSION ": " format "\n", ## __VA_ARGS__)
57 
58 /* Frequency limits in MHz -- these are European values.  For Japanese
59 devices, that would be 76000 and 91000.  */
60 #define FREQ_MIN  87500
61 #define FREQ_MAX 108000
62 #define FREQ_MUL 16
63 
64 /* TEA5764 registers */
65 #define TEA5764_MANID		0x002b
66 #define TEA5764_CHIPID		0x5764
67 
68 #define TEA5764_INTREG_BLMSK	0x0001
69 #define TEA5764_INTREG_FRRMSK	0x0002
70 #define TEA5764_INTREG_LEVMSK	0x0008
71 #define TEA5764_INTREG_IFMSK	0x0010
72 #define TEA5764_INTREG_BLMFLAG	0x0100
73 #define TEA5764_INTREG_FRRFLAG	0x0200
74 #define TEA5764_INTREG_LEVFLAG	0x0800
75 #define TEA5764_INTREG_IFFLAG	0x1000
76 
77 #define TEA5764_FRQSET_SUD	0x8000
78 #define TEA5764_FRQSET_SM	0x4000
79 
80 #define TEA5764_TNCTRL_PUPD1	0x8000
81 #define TEA5764_TNCTRL_PUPD0	0x4000
82 #define TEA5764_TNCTRL_BLIM	0x2000
83 #define TEA5764_TNCTRL_SWPM	0x1000
84 #define TEA5764_TNCTRL_IFCTC	0x0800
85 #define TEA5764_TNCTRL_AFM	0x0400
86 #define TEA5764_TNCTRL_SMUTE	0x0200
87 #define TEA5764_TNCTRL_SNC	0x0100
88 #define TEA5764_TNCTRL_MU	0x0080
89 #define TEA5764_TNCTRL_SSL1	0x0040
90 #define TEA5764_TNCTRL_SSL0	0x0020
91 #define TEA5764_TNCTRL_HLSI	0x0010
92 #define TEA5764_TNCTRL_MST	0x0008
93 #define TEA5764_TNCTRL_SWP	0x0004
94 #define TEA5764_TNCTRL_DTC	0x0002
95 #define TEA5764_TNCTRL_AHLSI	0x0001
96 
97 #define TEA5764_TUNCHK_LEVEL(x)	(((x) & 0x00F0) >> 4)
98 #define TEA5764_TUNCHK_IFCNT(x) (((x) & 0xFE00) >> 9)
99 #define TEA5764_TUNCHK_TUNTO	0x0100
100 #define TEA5764_TUNCHK_LD	0x0008
101 #define TEA5764_TUNCHK_STEREO	0x0004
102 
103 #define TEA5764_TESTREG_TRIGFR	0x0800
104 
105 struct tea5764_regs {
106 	u16 intreg;				/* INTFLAG & INTMSK */
107 	u16 frqset;				/* FRQSETMSB & FRQSETLSB */
108 	u16 tnctrl;				/* TNCTRL1 & TNCTRL2 */
109 	u16 frqchk;				/* FRQCHKMSB & FRQCHKLSB */
110 	u16 tunchk;				/* IFCHK & LEVCHK */
111 	u16 testreg;				/* TESTBITS & TESTMODE */
112 	u16 rdsstat;				/* RDSSTAT1 & RDSSTAT2 */
113 	u16 rdslb;				/* RDSLBMSB & RDSLBLSB */
114 	u16 rdspb;				/* RDSPBMSB & RDSPBLSB */
115 	u16 rdsbc;				/* RDSBBC & RDSGBC */
116 	u16 rdsctrl;				/* RDSCTRL1 & RDSCTRL2 */
117 	u16 rdsbbl;				/* PAUSEDET & RDSBBL */
118 	u16 manid;				/* MANID1 & MANID2 */
119 	u16 chipid;				/* CHIPID1 & CHIPID2 */
120 } __attribute__ ((packed));
121 
122 struct tea5764_write_regs {
123 	u8 intreg;				/* INTMSK */
124 	u16 frqset;				/* FRQSETMSB & FRQSETLSB */
125 	u16 tnctrl;				/* TNCTRL1 & TNCTRL2 */
126 	u16 testreg;				/* TESTBITS & TESTMODE */
127 	u16 rdsctrl;				/* RDSCTRL1 & RDSCTRL2 */
128 	u16 rdsbbl;				/* PAUSEDET & RDSBBL */
129 } __attribute__ ((packed));
130 
131 #ifdef CONFIG_RADIO_TEA5764_XTAL
132 #define RADIO_TEA5764_XTAL 1
133 #else
134 #define RADIO_TEA5764_XTAL 0
135 #endif
136 
137 static int radio_nr = -1;
138 static int use_xtal = RADIO_TEA5764_XTAL;
139 
140 struct tea5764_device {
141 	struct i2c_client		*i2c_client;
142 	struct video_device		*videodev;
143 	struct tea5764_regs		regs;
144 	struct mutex			mutex;
145 };
146 
147 /* I2C code related */
148 int tea5764_i2c_read(struct tea5764_device *radio)
149 {
150 	int i;
151 	u16 *p = (u16 *) &radio->regs;
152 
153 	struct i2c_msg msgs[1] = {
154 		{ radio->i2c_client->addr, I2C_M_RD, sizeof(radio->regs),
155 			(void *)&radio->regs },
156 	};
157 	if (i2c_transfer(radio->i2c_client->adapter, msgs, 1) != 1)
158 		return -EIO;
159 	for (i = 0; i < sizeof(struct tea5764_regs) / sizeof(u16); i++)
160 		p[i] = __be16_to_cpu(p[i]);
161 
162 	return 0;
163 }
164 
165 int tea5764_i2c_write(struct tea5764_device *radio)
166 {
167 	struct tea5764_write_regs wr;
168 	struct tea5764_regs *r = &radio->regs;
169 	struct i2c_msg msgs[1] = {
170 		{ radio->i2c_client->addr, 0, sizeof(wr), (void *) &wr },
171 	};
172 	wr.intreg  = r->intreg & 0xff;
173 	wr.frqset  = __cpu_to_be16(r->frqset);
174 	wr.tnctrl  = __cpu_to_be16(r->tnctrl);
175 	wr.testreg = __cpu_to_be16(r->testreg);
176 	wr.rdsctrl = __cpu_to_be16(r->rdsctrl);
177 	wr.rdsbbl  = __cpu_to_be16(r->rdsbbl);
178 	if (i2c_transfer(radio->i2c_client->adapter, msgs, 1) != 1)
179 		return -EIO;
180 	return 0;
181 }
182 
183 /* V4L2 code related */
184 static struct v4l2_queryctrl radio_qctrl[] = {
185 	{
186 		.id            = V4L2_CID_AUDIO_MUTE,
187 		.name          = "Mute",
188 		.minimum       = 0,
189 		.maximum       = 1,
190 		.default_value = 1,
191 		.type          = V4L2_CTRL_TYPE_BOOLEAN,
192 	}
193 };
194 
195 static void tea5764_power_up(struct tea5764_device *radio)
196 {
197 	struct tea5764_regs *r = &radio->regs;
198 
199 	if (!(r->tnctrl & TEA5764_TNCTRL_PUPD0)) {
200 		r->tnctrl &= ~(TEA5764_TNCTRL_AFM | TEA5764_TNCTRL_MU |
201 			       TEA5764_TNCTRL_HLSI);
202 		if (!use_xtal)
203 			r->testreg |= TEA5764_TESTREG_TRIGFR;
204 		else
205 			r->testreg &= ~TEA5764_TESTREG_TRIGFR;
206 
207 		r->tnctrl |= TEA5764_TNCTRL_PUPD0;
208 		tea5764_i2c_write(radio);
209 	}
210 }
211 
212 static void tea5764_power_down(struct tea5764_device *radio)
213 {
214 	struct tea5764_regs *r = &radio->regs;
215 
216 	if (r->tnctrl & TEA5764_TNCTRL_PUPD0) {
217 		r->tnctrl &= ~TEA5764_TNCTRL_PUPD0;
218 		tea5764_i2c_write(radio);
219 	}
220 }
221 
222 static void tea5764_set_freq(struct tea5764_device *radio, int freq)
223 {
224 	struct tea5764_regs *r = &radio->regs;
225 
226 	/* formula: (freq [+ or -] 225000) / 8192 */
227 	if (r->tnctrl & TEA5764_TNCTRL_HLSI)
228 		r->frqset = (freq + 225000) / 8192;
229 	else
230 		r->frqset = (freq - 225000) / 8192;
231 }
232 
233 static int tea5764_get_freq(struct tea5764_device *radio)
234 {
235 	struct tea5764_regs *r = &radio->regs;
236 
237 	if (r->tnctrl & TEA5764_TNCTRL_HLSI)
238 		return (r->frqchk * 8192) - 225000;
239 	else
240 		return (r->frqchk * 8192) + 225000;
241 }
242 
243 /* tune an frequency, freq is defined by v4l's TUNER_LOW, i.e. 1/16th kHz */
244 static void tea5764_tune(struct tea5764_device *radio, int freq)
245 {
246 	tea5764_set_freq(radio, freq);
247 	if (tea5764_i2c_write(radio))
248 		PWARN("Could not set frequency!");
249 }
250 
251 static void tea5764_set_audout_mode(struct tea5764_device *radio, int audmode)
252 {
253 	struct tea5764_regs *r = &radio->regs;
254 	int tnctrl = r->tnctrl;
255 
256 	if (audmode == V4L2_TUNER_MODE_MONO)
257 		r->tnctrl |= TEA5764_TNCTRL_MST;
258 	else
259 		r->tnctrl &= ~TEA5764_TNCTRL_MST;
260 	if (tnctrl != r->tnctrl)
261 		tea5764_i2c_write(radio);
262 }
263 
264 static int tea5764_get_audout_mode(struct tea5764_device *radio)
265 {
266 	struct tea5764_regs *r = &radio->regs;
267 
268 	if (r->tnctrl & TEA5764_TNCTRL_MST)
269 		return V4L2_TUNER_MODE_MONO;
270 	else
271 		return V4L2_TUNER_MODE_STEREO;
272 }
273 
274 static void tea5764_mute(struct tea5764_device *radio, int on)
275 {
276 	struct tea5764_regs *r = &radio->regs;
277 	int tnctrl = r->tnctrl;
278 
279 	if (on)
280 		r->tnctrl |= TEA5764_TNCTRL_MU;
281 	else
282 		r->tnctrl &= ~TEA5764_TNCTRL_MU;
283 	if (tnctrl != r->tnctrl)
284 		tea5764_i2c_write(radio);
285 }
286 
287 static int tea5764_is_muted(struct tea5764_device *radio)
288 {
289 	return radio->regs.tnctrl & TEA5764_TNCTRL_MU;
290 }
291 
292 /* V4L2 vidioc */
293 static int vidioc_querycap(struct file *file, void  *priv,
294 					struct v4l2_capability *v)
295 {
296 	struct tea5764_device *radio = video_drvdata(file);
297 	struct video_device *dev = radio->videodev;
298 
299 	strlcpy(v->driver, dev->dev.driver->name, sizeof(v->driver));
300 	strlcpy(v->card, dev->name, sizeof(v->card));
301 	snprintf(v->bus_info, sizeof(v->bus_info),
302 		 "I2C:%s", dev_name(&dev->dev));
303 	v->capabilities = V4L2_CAP_TUNER | V4L2_CAP_RADIO;
304 	return 0;
305 }
306 
307 static int vidioc_g_tuner(struct file *file, void *priv,
308 				struct v4l2_tuner *v)
309 {
310 	struct tea5764_device *radio = video_drvdata(file);
311 	struct tea5764_regs *r = &radio->regs;
312 
313 	if (v->index > 0)
314 		return -EINVAL;
315 
316 	memset(v, 0, sizeof(*v));
317 	strcpy(v->name, "FM");
318 	v->type = V4L2_TUNER_RADIO;
319 	tea5764_i2c_read(radio);
320 	v->rangelow   = FREQ_MIN * FREQ_MUL;
321 	v->rangehigh  = FREQ_MAX * FREQ_MUL;
322 	v->capability = V4L2_TUNER_CAP_LOW | V4L2_TUNER_CAP_STEREO;
323 	if (r->tunchk & TEA5764_TUNCHK_STEREO)
324 		v->rxsubchans = V4L2_TUNER_SUB_STEREO;
325 	else
326 		v->rxsubchans = V4L2_TUNER_SUB_MONO;
327 	v->audmode = tea5764_get_audout_mode(radio);
328 	v->signal = TEA5764_TUNCHK_LEVEL(r->tunchk) * 0xffff / 0xf;
329 	v->afc = TEA5764_TUNCHK_IFCNT(r->tunchk);
330 
331 	return 0;
332 }
333 
334 static int vidioc_s_tuner(struct file *file, void *priv,
335 				struct v4l2_tuner *v)
336 {
337 	struct tea5764_device *radio = video_drvdata(file);
338 
339 	if (v->index > 0)
340 		return -EINVAL;
341 
342 	tea5764_set_audout_mode(radio, v->audmode);
343 	return 0;
344 }
345 
346 static int vidioc_s_frequency(struct file *file, void *priv,
347 				struct v4l2_frequency *f)
348 {
349 	struct tea5764_device *radio = video_drvdata(file);
350 
351 	if (f->tuner != 0 || f->type != V4L2_TUNER_RADIO)
352 		return -EINVAL;
353 	if (f->frequency == 0) {
354 		/* We special case this as a power down control. */
355 		tea5764_power_down(radio);
356 	}
357 	if (f->frequency < (FREQ_MIN * FREQ_MUL))
358 		return -EINVAL;
359 	if (f->frequency > (FREQ_MAX * FREQ_MUL))
360 		return -EINVAL;
361 	tea5764_power_up(radio);
362 	tea5764_tune(radio, (f->frequency * 125) / 2);
363 	return 0;
364 }
365 
366 static int vidioc_g_frequency(struct file *file, void *priv,
367 				struct v4l2_frequency *f)
368 {
369 	struct tea5764_device *radio = video_drvdata(file);
370 	struct tea5764_regs *r = &radio->regs;
371 
372 	if (f->tuner != 0)
373 		return -EINVAL;
374 	tea5764_i2c_read(radio);
375 	memset(f, 0, sizeof(*f));
376 	f->type = V4L2_TUNER_RADIO;
377 	if (r->tnctrl & TEA5764_TNCTRL_PUPD0)
378 		f->frequency = (tea5764_get_freq(radio) * 2) / 125;
379 	else
380 		f->frequency = 0;
381 
382 	return 0;
383 }
384 
385 static int vidioc_queryctrl(struct file *file, void *priv,
386 			    struct v4l2_queryctrl *qc)
387 {
388 	int i;
389 
390 	for (i = 0; i < ARRAY_SIZE(radio_qctrl); i++) {
391 		if (qc->id && qc->id == radio_qctrl[i].id) {
392 			memcpy(qc, &(radio_qctrl[i]), sizeof(*qc));
393 			return 0;
394 		}
395 	}
396 	return -EINVAL;
397 }
398 
399 static int vidioc_g_ctrl(struct file *file, void *priv,
400 			    struct v4l2_control *ctrl)
401 {
402 	struct tea5764_device *radio = video_drvdata(file);
403 
404 	switch (ctrl->id) {
405 	case V4L2_CID_AUDIO_MUTE:
406 		tea5764_i2c_read(radio);
407 		ctrl->value = tea5764_is_muted(radio) ? 1 : 0;
408 		return 0;
409 	}
410 	return -EINVAL;
411 }
412 
413 static int vidioc_s_ctrl(struct file *file, void *priv,
414 			    struct v4l2_control *ctrl)
415 {
416 	struct tea5764_device *radio = video_drvdata(file);
417 
418 	switch (ctrl->id) {
419 	case V4L2_CID_AUDIO_MUTE:
420 		tea5764_mute(radio, ctrl->value);
421 		return 0;
422 	}
423 	return -EINVAL;
424 }
425 
426 static int vidioc_g_input(struct file *filp, void *priv, unsigned int *i)
427 {
428 	*i = 0;
429 	return 0;
430 }
431 
432 static int vidioc_s_input(struct file *filp, void *priv, unsigned int i)
433 {
434 	if (i != 0)
435 		return -EINVAL;
436 	return 0;
437 }
438 
439 static int vidioc_g_audio(struct file *file, void *priv,
440 			   struct v4l2_audio *a)
441 {
442 	if (a->index > 1)
443 		return -EINVAL;
444 
445 	strcpy(a->name, "Radio");
446 	a->capability = V4L2_AUDCAP_STEREO;
447 	return 0;
448 }
449 
450 static int vidioc_s_audio(struct file *file, void *priv,
451 			   struct v4l2_audio *a)
452 {
453 	if (a->index != 0)
454 		return -EINVAL;
455 
456 	return 0;
457 }
458 
459 /* File system interface */
460 static const struct v4l2_file_operations tea5764_fops = {
461 	.owner		= THIS_MODULE,
462 	.unlocked_ioctl	= video_ioctl2,
463 };
464 
465 static const struct v4l2_ioctl_ops tea5764_ioctl_ops = {
466 	.vidioc_querycap    = vidioc_querycap,
467 	.vidioc_g_tuner     = vidioc_g_tuner,
468 	.vidioc_s_tuner     = vidioc_s_tuner,
469 	.vidioc_g_audio     = vidioc_g_audio,
470 	.vidioc_s_audio     = vidioc_s_audio,
471 	.vidioc_g_input     = vidioc_g_input,
472 	.vidioc_s_input     = vidioc_s_input,
473 	.vidioc_g_frequency = vidioc_g_frequency,
474 	.vidioc_s_frequency = vidioc_s_frequency,
475 	.vidioc_queryctrl   = vidioc_queryctrl,
476 	.vidioc_g_ctrl      = vidioc_g_ctrl,
477 	.vidioc_s_ctrl      = vidioc_s_ctrl,
478 };
479 
480 /* V4L2 interface */
481 static struct video_device tea5764_radio_template = {
482 	.name		= "TEA5764 FM-Radio",
483 	.fops           = &tea5764_fops,
484 	.ioctl_ops 	= &tea5764_ioctl_ops,
485 	.release	= video_device_release,
486 };
487 
488 /* I2C probe: check if the device exists and register with v4l if it is */
489 static int __devinit tea5764_i2c_probe(struct i2c_client *client,
490 					const struct i2c_device_id *id)
491 {
492 	struct tea5764_device *radio;
493 	struct tea5764_regs *r;
494 	int ret;
495 
496 	PDEBUG("probe");
497 	radio = kzalloc(sizeof(struct tea5764_device), GFP_KERNEL);
498 	if (!radio)
499 		return -ENOMEM;
500 
501 	mutex_init(&radio->mutex);
502 	radio->i2c_client = client;
503 	ret = tea5764_i2c_read(radio);
504 	if (ret)
505 		goto errfr;
506 	r = &radio->regs;
507 	PDEBUG("chipid = %04X, manid = %04X", r->chipid, r->manid);
508 	if (r->chipid != TEA5764_CHIPID ||
509 		(r->manid & 0x0fff) != TEA5764_MANID) {
510 		PWARN("This chip is not a TEA5764!");
511 		ret = -EINVAL;
512 		goto errfr;
513 	}
514 
515 	radio->videodev = video_device_alloc();
516 	if (!(radio->videodev)) {
517 		ret = -ENOMEM;
518 		goto errfr;
519 	}
520 	memcpy(radio->videodev, &tea5764_radio_template,
521 		sizeof(tea5764_radio_template));
522 
523 	i2c_set_clientdata(client, radio);
524 	video_set_drvdata(radio->videodev, radio);
525 	radio->videodev->lock = &radio->mutex;
526 
527 	/* initialize and power off the chip */
528 	tea5764_i2c_read(radio);
529 	tea5764_set_audout_mode(radio, V4L2_TUNER_MODE_STEREO);
530 	tea5764_mute(radio, 1);
531 	tea5764_power_down(radio);
532 
533 	ret = video_register_device(radio->videodev, VFL_TYPE_RADIO, radio_nr);
534 	if (ret < 0) {
535 		PWARN("Could not register video device!");
536 		goto errrel;
537 	}
538 
539 	PINFO("registered.");
540 	return 0;
541 errrel:
542 	video_device_release(radio->videodev);
543 errfr:
544 	kfree(radio);
545 	return ret;
546 }
547 
548 static int __devexit tea5764_i2c_remove(struct i2c_client *client)
549 {
550 	struct tea5764_device *radio = i2c_get_clientdata(client);
551 
552 	PDEBUG("remove");
553 	if (radio) {
554 		tea5764_power_down(radio);
555 		video_unregister_device(radio->videodev);
556 		kfree(radio);
557 	}
558 	return 0;
559 }
560 
561 /* I2C subsystem interface */
562 static const struct i2c_device_id tea5764_id[] = {
563 	{ "radio-tea5764", 0 },
564 	{ }					/* Terminating entry */
565 };
566 MODULE_DEVICE_TABLE(i2c, tea5764_id);
567 
568 static struct i2c_driver tea5764_i2c_driver = {
569 	.driver = {
570 		.name = "radio-tea5764",
571 		.owner = THIS_MODULE,
572 	},
573 	.probe = tea5764_i2c_probe,
574 	.remove = __devexit_p(tea5764_i2c_remove),
575 	.id_table = tea5764_id,
576 };
577 
578 module_i2c_driver(tea5764_i2c_driver);
579 
580 MODULE_AUTHOR(DRIVER_AUTHOR);
581 MODULE_DESCRIPTION(DRIVER_DESC);
582 MODULE_LICENSE("GPL");
583 MODULE_VERSION(DRIVER_VERSION);
584 
585 module_param(use_xtal, int, 0);
586 MODULE_PARM_DESC(use_xtal, "Chip have a xtal connected in board");
587 module_param(radio_nr, int, 0);
588 MODULE_PARM_DESC(radio_nr, "video4linux device number to use");
589