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