GUI: Fix Tomato RAF theme for all builds. Compilation typo.
[tomato.git] / release / src-rt-6.x.4708 / linux / linux-2.6.36 / drivers / media / radio / radio-tea5764.c
blob789d2ec66e1948d2d071c3424f92dc820b0bcc0b
1 /*
2 * driver/media/radio/radio-tea5764.c
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.
8 * Based in radio-tea5761.c Copyright (C) 2005 Nokia Corporation
10 * Copyright (c) 2008 Fabio Belavenuto <belavenuto@gmail.com>
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.
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.
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
26 * History:
27 * 2008-12-06 Fabio Belavenuto <belavenuto@gmail.com>
28 * initial code
30 * TODO:
31 * add platform_data support for IRQs platform dependencies
32 * add RDS support
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 */
44 #define DRIVER_VERSION "v0.01"
45 #define RADIO_VERSION KERNEL_VERSION(0, 0, 1)
47 #define DRIVER_AUTHOR "Fabio Belavenuto <belavenuto@gmail.com>"
48 #define DRIVER_DESC "A driver for the TEA5764 radio chip for EZX Phones."
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__)
60 /* Frequency limits in MHz -- these are European values. For Japanese
61 devices, that would be 76000 and 91000. */
62 #define FREQ_MIN 87500
63 #define FREQ_MAX 108000
64 #define FREQ_MUL 16
66 /* TEA5764 registers */
67 #define TEA5764_MANID 0x002b
68 #define TEA5764_CHIPID 0x5764
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
79 #define TEA5764_FRQSET_SUD 0x8000
80 #define TEA5764_FRQSET_SM 0x4000
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
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
105 #define TEA5764_TESTREG_TRIGFR 0x0800
107 struct 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));
124 struct 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));
133 #ifndef RADIO_TEA5764_XTAL
134 #define RADIO_TEA5764_XTAL 1
135 #endif
137 static int radio_nr = -1;
138 static int use_xtal = RADIO_TEA5764_XTAL;
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 int users;
148 /* I2C code related */
149 int tea5764_i2c_read(struct tea5764_device *radio)
151 int i;
152 u16 *p = (u16 *) &radio->regs;
154 struct i2c_msg msgs[1] = {
155 { radio->i2c_client->addr, I2C_M_RD, sizeof(radio->regs),
156 (void *)&radio->regs },
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]);
163 return 0;
166 int tea5764_i2c_write(struct tea5764_device *radio)
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 },
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;
184 /* V4L2 code related */
185 static struct v4l2_queryctrl radio_qctrl[] = {
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,
196 static void tea5764_power_up(struct tea5764_device *radio)
198 struct tea5764_regs *r = &radio->regs;
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;
208 r->tnctrl |= TEA5764_TNCTRL_PUPD0;
209 tea5764_i2c_write(radio);
213 static void tea5764_power_down(struct tea5764_device *radio)
215 struct tea5764_regs *r = &radio->regs;
217 if (r->tnctrl & TEA5764_TNCTRL_PUPD0) {
218 r->tnctrl &= ~TEA5764_TNCTRL_PUPD0;
219 tea5764_i2c_write(radio);
223 static void tea5764_set_freq(struct tea5764_device *radio, int freq)
225 struct tea5764_regs *r = &radio->regs;
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;
234 static int tea5764_get_freq(struct tea5764_device *radio)
236 struct tea5764_regs *r = &radio->regs;
238 if (r->tnctrl & TEA5764_TNCTRL_HLSI)
239 return (r->frqchk * 8192) - 225000;
240 else
241 return (r->frqchk * 8192) + 225000;
244 /* tune an frequency, freq is defined by v4l's TUNER_LOW, i.e. 1/16th kHz */
245 static void tea5764_tune(struct tea5764_device *radio, int freq)
247 tea5764_set_freq(radio, freq);
248 if (tea5764_i2c_write(radio))
249 PWARN("Could not set frequency!");
252 static void tea5764_set_audout_mode(struct tea5764_device *radio, int audmode)
254 struct tea5764_regs *r = &radio->regs;
255 int tnctrl = r->tnctrl;
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);
265 static int tea5764_get_audout_mode(struct tea5764_device *radio)
267 struct tea5764_regs *r = &radio->regs;
269 if (r->tnctrl & TEA5764_TNCTRL_MST)
270 return V4L2_TUNER_MODE_MONO;
271 else
272 return V4L2_TUNER_MODE_STEREO;
275 static void tea5764_mute(struct tea5764_device *radio, int on)
277 struct tea5764_regs *r = &radio->regs;
278 int tnctrl = r->tnctrl;
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);
288 static int tea5764_is_muted(struct tea5764_device *radio)
290 return radio->regs.tnctrl & TEA5764_TNCTRL_MU;
293 /* V4L2 vidioc */
294 static int vidioc_querycap(struct file *file, void *priv,
295 struct v4l2_capability *v)
297 struct tea5764_device *radio = video_drvdata(file);
298 struct video_device *dev = radio->videodev;
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;
309 static int vidioc_g_tuner(struct file *file, void *priv,
310 struct v4l2_tuner *v)
312 struct tea5764_device *radio = video_drvdata(file);
313 struct tea5764_regs *r = &radio->regs;
315 if (v->index > 0)
316 return -EINVAL;
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);
333 return 0;
336 static int vidioc_s_tuner(struct file *file, void *priv,
337 struct v4l2_tuner *v)
339 struct tea5764_device *radio = video_drvdata(file);
341 if (v->index > 0)
342 return -EINVAL;
344 tea5764_set_audout_mode(radio, v->audmode);
345 return 0;
348 static int vidioc_s_frequency(struct file *file, void *priv,
349 struct v4l2_frequency *f)
351 struct tea5764_device *radio = video_drvdata(file);
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);
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;
368 static int vidioc_g_frequency(struct file *file, void *priv,
369 struct v4l2_frequency *f)
371 struct tea5764_device *radio = video_drvdata(file);
372 struct tea5764_regs *r = &radio->regs;
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;
384 return 0;
387 static int vidioc_queryctrl(struct file *file, void *priv,
388 struct v4l2_queryctrl *qc)
390 int i;
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;
398 return -EINVAL;
401 static int vidioc_g_ctrl(struct file *file, void *priv,
402 struct v4l2_control *ctrl)
404 struct tea5764_device *radio = video_drvdata(file);
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;
412 return -EINVAL;
415 static int vidioc_s_ctrl(struct file *file, void *priv,
416 struct v4l2_control *ctrl)
418 struct tea5764_device *radio = video_drvdata(file);
420 switch (ctrl->id) {
421 case V4L2_CID_AUDIO_MUTE:
422 tea5764_mute(radio, ctrl->value);
423 return 0;
425 return -EINVAL;
428 static int vidioc_g_input(struct file *filp, void *priv, unsigned int *i)
430 *i = 0;
431 return 0;
434 static int vidioc_s_input(struct file *filp, void *priv, unsigned int i)
436 if (i != 0)
437 return -EINVAL;
438 return 0;
441 static int vidioc_g_audio(struct file *file, void *priv,
442 struct v4l2_audio *a)
444 if (a->index > 1)
445 return -EINVAL;
447 strcpy(a->name, "Radio");
448 a->capability = V4L2_AUDCAP_STEREO;
449 return 0;
452 static int vidioc_s_audio(struct file *file, void *priv,
453 struct v4l2_audio *a)
455 if (a->index != 0)
456 return -EINVAL;
458 return 0;
461 static int tea5764_open(struct file *file)
463 /* Currently we support only one device */
464 struct tea5764_device *radio = video_drvdata(file);
466 mutex_lock(&radio->mutex);
467 /* Only exclusive access */
468 if (radio->users) {
469 mutex_unlock(&radio->mutex);
470 return -EBUSY;
472 radio->users++;
473 mutex_unlock(&radio->mutex);
474 file->private_data = radio;
475 return 0;
478 static int tea5764_close(struct file *file)
480 struct tea5764_device *radio = video_drvdata(file);
482 if (!radio)
483 return -ENODEV;
484 mutex_lock(&radio->mutex);
485 radio->users--;
486 mutex_unlock(&radio->mutex);
487 return 0;
490 /* File system interface */
491 static const struct v4l2_file_operations tea5764_fops = {
492 .owner = THIS_MODULE,
493 .open = tea5764_open,
494 .release = tea5764_close,
495 .ioctl = video_ioctl2,
498 static 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,
513 /* V4L2 interface */
514 static 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,
521 /* I2C probe: check if the device exists and register with v4l if it is */
522 static int __devinit tea5764_i2c_probe(struct i2c_client *client,
523 const struct i2c_device_id *id)
525 struct tea5764_device *radio;
526 struct tea5764_regs *r;
527 int ret;
529 PDEBUG("probe");
530 radio = kmalloc(sizeof(struct tea5764_device), GFP_KERNEL);
531 if (!radio)
532 return -ENOMEM;
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;
548 radio->videodev = video_device_alloc();
549 if (!(radio->videodev)) {
550 ret = -ENOMEM;
551 goto errfr;
553 memcpy(radio->videodev, &tea5764_radio_template,
554 sizeof(tea5764_radio_template));
556 i2c_set_clientdata(client, radio);
557 video_set_drvdata(radio->videodev, radio);
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;
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);
571 PINFO("registered.");
572 return 0;
573 errrel:
574 video_device_release(radio->videodev);
575 errfr:
576 kfree(radio);
577 return ret;
580 static int __devexit tea5764_i2c_remove(struct i2c_client *client)
582 struct tea5764_device *radio = i2c_get_clientdata(client);
584 PDEBUG("remove");
585 if (radio) {
586 tea5764_power_down(radio);
587 video_unregister_device(radio->videodev);
588 kfree(radio);
590 return 0;
593 /* I2C subsystem interface */
594 static const struct i2c_device_id tea5764_id[] = {
595 { "radio-tea5764", 0 },
596 { } /* Terminating entry */
598 MODULE_DEVICE_TABLE(i2c, tea5764_id);
600 static struct i2c_driver tea5764_i2c_driver = {
601 .driver = {
602 .name = "radio-tea5764",
603 .owner = THIS_MODULE,
605 .probe = tea5764_i2c_probe,
606 .remove = __devexit_p(tea5764_i2c_remove),
607 .id_table = tea5764_id,
610 /* init the driver */
611 static int __init tea5764_init(void)
613 int ret = i2c_add_driver(&tea5764_i2c_driver);
615 printk(KERN_INFO KBUILD_MODNAME ": " DRIVER_VERSION ": "
616 DRIVER_DESC "\n");
617 return ret;
620 /* cleanup the driver */
621 static void __exit tea5764_exit(void)
623 i2c_del_driver(&tea5764_i2c_driver);
626 MODULE_AUTHOR(DRIVER_AUTHOR);
627 MODULE_DESCRIPTION(DRIVER_DESC);
628 MODULE_LICENSE("GPL");
630 module_param(use_xtal, int, 1);
631 MODULE_PARM_DESC(use_xtal, "Chip have a xtal connected in board");
632 module_param(radio_nr, int, 0);
633 MODULE_PARM_DESC(radio_nr, "video4linux device number to use");
635 module_init(tea5764_init);
636 module_exit(tea5764_exit);