[media] tea5764: Fix module parameter permissions
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / media / radio / radio-tea5764.c
blob95ddcc4845d34d7563b6843ea5f51d8255555cdc
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>
43 #define DRIVER_VERSION "0.0.2"
45 #define DRIVER_AUTHOR "Fabio Belavenuto <belavenuto@gmail.com>"
46 #define DRIVER_DESC "A driver for the TEA5764 radio chip for EZX Phones."
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__)
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
64 /* TEA5764 registers */
65 #define TEA5764_MANID 0x002b
66 #define TEA5764_CHIPID 0x5764
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
77 #define TEA5764_FRQSET_SUD 0x8000
78 #define TEA5764_FRQSET_SM 0x4000
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
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
103 #define TEA5764_TESTREG_TRIGFR 0x0800
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));
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));
131 #ifndef RADIO_TEA5764_XTAL
132 #define RADIO_TEA5764_XTAL 1
133 #endif
135 static int radio_nr = -1;
136 static int use_xtal = RADIO_TEA5764_XTAL;
138 struct tea5764_device {
139 struct i2c_client *i2c_client;
140 struct video_device *videodev;
141 struct tea5764_regs regs;
142 struct mutex mutex;
145 /* I2C code related */
146 int tea5764_i2c_read(struct tea5764_device *radio)
148 int i;
149 u16 *p = (u16 *) &radio->regs;
151 struct i2c_msg msgs[1] = {
152 { radio->i2c_client->addr, I2C_M_RD, sizeof(radio->regs),
153 (void *)&radio->regs },
155 if (i2c_transfer(radio->i2c_client->adapter, msgs, 1) != 1)
156 return -EIO;
157 for (i = 0; i < sizeof(struct tea5764_regs) / sizeof(u16); i++)
158 p[i] = __be16_to_cpu(p[i]);
160 return 0;
163 int tea5764_i2c_write(struct tea5764_device *radio)
165 struct tea5764_write_regs wr;
166 struct tea5764_regs *r = &radio->regs;
167 struct i2c_msg msgs[1] = {
168 { radio->i2c_client->addr, 0, sizeof(wr), (void *) &wr },
170 wr.intreg = r->intreg & 0xff;
171 wr.frqset = __cpu_to_be16(r->frqset);
172 wr.tnctrl = __cpu_to_be16(r->tnctrl);
173 wr.testreg = __cpu_to_be16(r->testreg);
174 wr.rdsctrl = __cpu_to_be16(r->rdsctrl);
175 wr.rdsbbl = __cpu_to_be16(r->rdsbbl);
176 if (i2c_transfer(radio->i2c_client->adapter, msgs, 1) != 1)
177 return -EIO;
178 return 0;
181 /* V4L2 code related */
182 static struct v4l2_queryctrl radio_qctrl[] = {
184 .id = V4L2_CID_AUDIO_MUTE,
185 .name = "Mute",
186 .minimum = 0,
187 .maximum = 1,
188 .default_value = 1,
189 .type = V4L2_CTRL_TYPE_BOOLEAN,
193 static void tea5764_power_up(struct tea5764_device *radio)
195 struct tea5764_regs *r = &radio->regs;
197 if (!(r->tnctrl & TEA5764_TNCTRL_PUPD0)) {
198 r->tnctrl &= ~(TEA5764_TNCTRL_AFM | TEA5764_TNCTRL_MU |
199 TEA5764_TNCTRL_HLSI);
200 if (!use_xtal)
201 r->testreg |= TEA5764_TESTREG_TRIGFR;
202 else
203 r->testreg &= ~TEA5764_TESTREG_TRIGFR;
205 r->tnctrl |= TEA5764_TNCTRL_PUPD0;
206 tea5764_i2c_write(radio);
210 static void tea5764_power_down(struct tea5764_device *radio)
212 struct tea5764_regs *r = &radio->regs;
214 if (r->tnctrl & TEA5764_TNCTRL_PUPD0) {
215 r->tnctrl &= ~TEA5764_TNCTRL_PUPD0;
216 tea5764_i2c_write(radio);
220 static void tea5764_set_freq(struct tea5764_device *radio, int freq)
222 struct tea5764_regs *r = &radio->regs;
224 /* formula: (freq [+ or -] 225000) / 8192 */
225 if (r->tnctrl & TEA5764_TNCTRL_HLSI)
226 r->frqset = (freq + 225000) / 8192;
227 else
228 r->frqset = (freq - 225000) / 8192;
231 static int tea5764_get_freq(struct tea5764_device *radio)
233 struct tea5764_regs *r = &radio->regs;
235 if (r->tnctrl & TEA5764_TNCTRL_HLSI)
236 return (r->frqchk * 8192) - 225000;
237 else
238 return (r->frqchk * 8192) + 225000;
241 /* tune an frequency, freq is defined by v4l's TUNER_LOW, i.e. 1/16th kHz */
242 static void tea5764_tune(struct tea5764_device *radio, int freq)
244 tea5764_set_freq(radio, freq);
245 if (tea5764_i2c_write(radio))
246 PWARN("Could not set frequency!");
249 static void tea5764_set_audout_mode(struct tea5764_device *radio, int audmode)
251 struct tea5764_regs *r = &radio->regs;
252 int tnctrl = r->tnctrl;
254 if (audmode == V4L2_TUNER_MODE_MONO)
255 r->tnctrl |= TEA5764_TNCTRL_MST;
256 else
257 r->tnctrl &= ~TEA5764_TNCTRL_MST;
258 if (tnctrl != r->tnctrl)
259 tea5764_i2c_write(radio);
262 static int tea5764_get_audout_mode(struct tea5764_device *radio)
264 struct tea5764_regs *r = &radio->regs;
266 if (r->tnctrl & TEA5764_TNCTRL_MST)
267 return V4L2_TUNER_MODE_MONO;
268 else
269 return V4L2_TUNER_MODE_STEREO;
272 static void tea5764_mute(struct tea5764_device *radio, int on)
274 struct tea5764_regs *r = &radio->regs;
275 int tnctrl = r->tnctrl;
277 if (on)
278 r->tnctrl |= TEA5764_TNCTRL_MU;
279 else
280 r->tnctrl &= ~TEA5764_TNCTRL_MU;
281 if (tnctrl != r->tnctrl)
282 tea5764_i2c_write(radio);
285 static int tea5764_is_muted(struct tea5764_device *radio)
287 return radio->regs.tnctrl & TEA5764_TNCTRL_MU;
290 /* V4L2 vidioc */
291 static int vidioc_querycap(struct file *file, void *priv,
292 struct v4l2_capability *v)
294 struct tea5764_device *radio = video_drvdata(file);
295 struct video_device *dev = radio->videodev;
297 strlcpy(v->driver, dev->dev.driver->name, sizeof(v->driver));
298 strlcpy(v->card, dev->name, sizeof(v->card));
299 snprintf(v->bus_info, sizeof(v->bus_info),
300 "I2C:%s", dev_name(&dev->dev));
301 v->capabilities = V4L2_CAP_TUNER | V4L2_CAP_RADIO;
302 return 0;
305 static int vidioc_g_tuner(struct file *file, void *priv,
306 struct v4l2_tuner *v)
308 struct tea5764_device *radio = video_drvdata(file);
309 struct tea5764_regs *r = &radio->regs;
311 if (v->index > 0)
312 return -EINVAL;
314 memset(v, 0, sizeof(*v));
315 strcpy(v->name, "FM");
316 v->type = V4L2_TUNER_RADIO;
317 tea5764_i2c_read(radio);
318 v->rangelow = FREQ_MIN * FREQ_MUL;
319 v->rangehigh = FREQ_MAX * FREQ_MUL;
320 v->capability = V4L2_TUNER_CAP_LOW | V4L2_TUNER_CAP_STEREO;
321 if (r->tunchk & TEA5764_TUNCHK_STEREO)
322 v->rxsubchans = V4L2_TUNER_SUB_STEREO;
323 else
324 v->rxsubchans = V4L2_TUNER_SUB_MONO;
325 v->audmode = tea5764_get_audout_mode(radio);
326 v->signal = TEA5764_TUNCHK_LEVEL(r->tunchk) * 0xffff / 0xf;
327 v->afc = TEA5764_TUNCHK_IFCNT(r->tunchk);
329 return 0;
332 static int vidioc_s_tuner(struct file *file, void *priv,
333 struct v4l2_tuner *v)
335 struct tea5764_device *radio = video_drvdata(file);
337 if (v->index > 0)
338 return -EINVAL;
340 tea5764_set_audout_mode(radio, v->audmode);
341 return 0;
344 static int vidioc_s_frequency(struct file *file, void *priv,
345 struct v4l2_frequency *f)
347 struct tea5764_device *radio = video_drvdata(file);
349 if (f->tuner != 0 || f->type != V4L2_TUNER_RADIO)
350 return -EINVAL;
351 if (f->frequency == 0) {
352 /* We special case this as a power down control. */
353 tea5764_power_down(radio);
355 if (f->frequency < (FREQ_MIN * FREQ_MUL))
356 return -EINVAL;
357 if (f->frequency > (FREQ_MAX * FREQ_MUL))
358 return -EINVAL;
359 tea5764_power_up(radio);
360 tea5764_tune(radio, (f->frequency * 125) / 2);
361 return 0;
364 static int vidioc_g_frequency(struct file *file, void *priv,
365 struct v4l2_frequency *f)
367 struct tea5764_device *radio = video_drvdata(file);
368 struct tea5764_regs *r = &radio->regs;
370 if (f->tuner != 0)
371 return -EINVAL;
372 tea5764_i2c_read(radio);
373 memset(f, 0, sizeof(*f));
374 f->type = V4L2_TUNER_RADIO;
375 if (r->tnctrl & TEA5764_TNCTRL_PUPD0)
376 f->frequency = (tea5764_get_freq(radio) * 2) / 125;
377 else
378 f->frequency = 0;
380 return 0;
383 static int vidioc_queryctrl(struct file *file, void *priv,
384 struct v4l2_queryctrl *qc)
386 int i;
388 for (i = 0; i < ARRAY_SIZE(radio_qctrl); i++) {
389 if (qc->id && qc->id == radio_qctrl[i].id) {
390 memcpy(qc, &(radio_qctrl[i]), sizeof(*qc));
391 return 0;
394 return -EINVAL;
397 static int vidioc_g_ctrl(struct file *file, void *priv,
398 struct v4l2_control *ctrl)
400 struct tea5764_device *radio = video_drvdata(file);
402 switch (ctrl->id) {
403 case V4L2_CID_AUDIO_MUTE:
404 tea5764_i2c_read(radio);
405 ctrl->value = tea5764_is_muted(radio) ? 1 : 0;
406 return 0;
408 return -EINVAL;
411 static int vidioc_s_ctrl(struct file *file, void *priv,
412 struct v4l2_control *ctrl)
414 struct tea5764_device *radio = video_drvdata(file);
416 switch (ctrl->id) {
417 case V4L2_CID_AUDIO_MUTE:
418 tea5764_mute(radio, ctrl->value);
419 return 0;
421 return -EINVAL;
424 static int vidioc_g_input(struct file *filp, void *priv, unsigned int *i)
426 *i = 0;
427 return 0;
430 static int vidioc_s_input(struct file *filp, void *priv, unsigned int i)
432 if (i != 0)
433 return -EINVAL;
434 return 0;
437 static int vidioc_g_audio(struct file *file, void *priv,
438 struct v4l2_audio *a)
440 if (a->index > 1)
441 return -EINVAL;
443 strcpy(a->name, "Radio");
444 a->capability = V4L2_AUDCAP_STEREO;
445 return 0;
448 static int vidioc_s_audio(struct file *file, void *priv,
449 struct v4l2_audio *a)
451 if (a->index != 0)
452 return -EINVAL;
454 return 0;
457 /* File system interface */
458 static const struct v4l2_file_operations tea5764_fops = {
459 .owner = THIS_MODULE,
460 .unlocked_ioctl = video_ioctl2,
463 static const struct v4l2_ioctl_ops tea5764_ioctl_ops = {
464 .vidioc_querycap = vidioc_querycap,
465 .vidioc_g_tuner = vidioc_g_tuner,
466 .vidioc_s_tuner = vidioc_s_tuner,
467 .vidioc_g_audio = vidioc_g_audio,
468 .vidioc_s_audio = vidioc_s_audio,
469 .vidioc_g_input = vidioc_g_input,
470 .vidioc_s_input = vidioc_s_input,
471 .vidioc_g_frequency = vidioc_g_frequency,
472 .vidioc_s_frequency = vidioc_s_frequency,
473 .vidioc_queryctrl = vidioc_queryctrl,
474 .vidioc_g_ctrl = vidioc_g_ctrl,
475 .vidioc_s_ctrl = vidioc_s_ctrl,
478 /* V4L2 interface */
479 static struct video_device tea5764_radio_template = {
480 .name = "TEA5764 FM-Radio",
481 .fops = &tea5764_fops,
482 .ioctl_ops = &tea5764_ioctl_ops,
483 .release = video_device_release,
486 /* I2C probe: check if the device exists and register with v4l if it is */
487 static int __devinit tea5764_i2c_probe(struct i2c_client *client,
488 const struct i2c_device_id *id)
490 struct tea5764_device *radio;
491 struct tea5764_regs *r;
492 int ret;
494 PDEBUG("probe");
495 radio = kzalloc(sizeof(struct tea5764_device), GFP_KERNEL);
496 if (!radio)
497 return -ENOMEM;
499 mutex_init(&radio->mutex);
500 radio->i2c_client = client;
501 ret = tea5764_i2c_read(radio);
502 if (ret)
503 goto errfr;
504 r = &radio->regs;
505 PDEBUG("chipid = %04X, manid = %04X", r->chipid, r->manid);
506 if (r->chipid != TEA5764_CHIPID ||
507 (r->manid & 0x0fff) != TEA5764_MANID) {
508 PWARN("This chip is not a TEA5764!");
509 ret = -EINVAL;
510 goto errfr;
513 radio->videodev = video_device_alloc();
514 if (!(radio->videodev)) {
515 ret = -ENOMEM;
516 goto errfr;
518 memcpy(radio->videodev, &tea5764_radio_template,
519 sizeof(tea5764_radio_template));
521 i2c_set_clientdata(client, radio);
522 video_set_drvdata(radio->videodev, radio);
523 radio->videodev->lock = &radio->mutex;
525 /* initialize and power off the chip */
526 tea5764_i2c_read(radio);
527 tea5764_set_audout_mode(radio, V4L2_TUNER_MODE_STEREO);
528 tea5764_mute(radio, 1);
529 tea5764_power_down(radio);
531 ret = video_register_device(radio->videodev, VFL_TYPE_RADIO, radio_nr);
532 if (ret < 0) {
533 PWARN("Could not register video device!");
534 goto errrel;
537 PINFO("registered.");
538 return 0;
539 errrel:
540 video_device_release(radio->videodev);
541 errfr:
542 kfree(radio);
543 return ret;
546 static int __devexit tea5764_i2c_remove(struct i2c_client *client)
548 struct tea5764_device *radio = i2c_get_clientdata(client);
550 PDEBUG("remove");
551 if (radio) {
552 tea5764_power_down(radio);
553 video_unregister_device(radio->videodev);
554 kfree(radio);
556 return 0;
559 /* I2C subsystem interface */
560 static const struct i2c_device_id tea5764_id[] = {
561 { "radio-tea5764", 0 },
562 { } /* Terminating entry */
564 MODULE_DEVICE_TABLE(i2c, tea5764_id);
566 static struct i2c_driver tea5764_i2c_driver = {
567 .driver = {
568 .name = "radio-tea5764",
569 .owner = THIS_MODULE,
571 .probe = tea5764_i2c_probe,
572 .remove = __devexit_p(tea5764_i2c_remove),
573 .id_table = tea5764_id,
576 /* init the driver */
577 static int __init tea5764_init(void)
579 int ret = i2c_add_driver(&tea5764_i2c_driver);
581 printk(KERN_INFO KBUILD_MODNAME ": " DRIVER_VERSION ": "
582 DRIVER_DESC "\n");
583 return ret;
586 /* cleanup the driver */
587 static void __exit tea5764_exit(void)
589 i2c_del_driver(&tea5764_i2c_driver);
592 MODULE_AUTHOR(DRIVER_AUTHOR);
593 MODULE_DESCRIPTION(DRIVER_DESC);
594 MODULE_LICENSE("GPL");
595 MODULE_VERSION(DRIVER_VERSION);
597 module_param(use_xtal, int, 0);
598 MODULE_PARM_DESC(use_xtal, "Chip have a xtal connected in board");
599 module_param(radio_nr, int, 0);
600 MODULE_PARM_DESC(radio_nr, "video4linux device number to use");
602 module_init(tea5764_init);
603 module_exit(tea5764_exit);