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.
23 * 2008-12-06 Fabio Belavenuto <belavenuto@gmail.com>
27 * add platform_data support for IRQs platform dependencies
30 #include <linux/kernel.h>
31 #include <linux/slab.h>
32 #include <linux/module.h>
33 #include <linux/init.h> /* Initdata */
34 #include <linux/videodev2.h> /* kernel radio structs */
35 #include <linux/i2c.h> /* I2C */
36 #include <media/v4l2-common.h>
37 #include <media/v4l2-ioctl.h>
38 #include <media/v4l2-device.h>
39 #include <media/v4l2-ctrls.h>
40 #include <media/v4l2-event.h>
42 #define DRIVER_VERSION "0.0.2"
44 #define DRIVER_AUTHOR "Fabio Belavenuto <belavenuto@gmail.com>"
45 #define DRIVER_DESC "A driver for the TEA5764 radio chip for EZX Phones."
47 #define PINFO(format, ...)\
48 printk(KERN_INFO KBUILD_MODNAME ": "\
49 DRIVER_VERSION ": " format "\n", ## __VA_ARGS__)
50 #define PWARN(format, ...)\
51 printk(KERN_WARNING KBUILD_MODNAME ": "\
52 DRIVER_VERSION ": " format "\n", ## __VA_ARGS__)
53 # define PDEBUG(format, ...)\
54 printk(KERN_DEBUG KBUILD_MODNAME ": "\
55 DRIVER_VERSION ": " format "\n", ## __VA_ARGS__)
57 /* Frequency limits in MHz -- these are European values. For Japanese
58 devices, that would be 76000 and 91000. */
59 #define FREQ_MIN 87500U
60 #define FREQ_MAX 108000U
63 /* TEA5764 registers */
64 #define TEA5764_MANID 0x002b
65 #define TEA5764_CHIPID 0x5764
67 #define TEA5764_INTREG_BLMSK 0x0001
68 #define TEA5764_INTREG_FRRMSK 0x0002
69 #define TEA5764_INTREG_LEVMSK 0x0008
70 #define TEA5764_INTREG_IFMSK 0x0010
71 #define TEA5764_INTREG_BLMFLAG 0x0100
72 #define TEA5764_INTREG_FRRFLAG 0x0200
73 #define TEA5764_INTREG_LEVFLAG 0x0800
74 #define TEA5764_INTREG_IFFLAG 0x1000
76 #define TEA5764_FRQSET_SUD 0x8000
77 #define TEA5764_FRQSET_SM 0x4000
79 #define TEA5764_TNCTRL_PUPD1 0x8000
80 #define TEA5764_TNCTRL_PUPD0 0x4000
81 #define TEA5764_TNCTRL_BLIM 0x2000
82 #define TEA5764_TNCTRL_SWPM 0x1000
83 #define TEA5764_TNCTRL_IFCTC 0x0800
84 #define TEA5764_TNCTRL_AFM 0x0400
85 #define TEA5764_TNCTRL_SMUTE 0x0200
86 #define TEA5764_TNCTRL_SNC 0x0100
87 #define TEA5764_TNCTRL_MU 0x0080
88 #define TEA5764_TNCTRL_SSL1 0x0040
89 #define TEA5764_TNCTRL_SSL0 0x0020
90 #define TEA5764_TNCTRL_HLSI 0x0010
91 #define TEA5764_TNCTRL_MST 0x0008
92 #define TEA5764_TNCTRL_SWP 0x0004
93 #define TEA5764_TNCTRL_DTC 0x0002
94 #define TEA5764_TNCTRL_AHLSI 0x0001
96 #define TEA5764_TUNCHK_LEVEL(x) (((x) & 0x00F0) >> 4)
97 #define TEA5764_TUNCHK_IFCNT(x) (((x) & 0xFE00) >> 9)
98 #define TEA5764_TUNCHK_TUNTO 0x0100
99 #define TEA5764_TUNCHK_LD 0x0008
100 #define TEA5764_TUNCHK_STEREO 0x0004
102 #define TEA5764_TESTREG_TRIGFR 0x0800
104 struct tea5764_regs
{
105 u16 intreg
; /* INTFLAG & INTMSK */
106 u16 frqset
; /* FRQSETMSB & FRQSETLSB */
107 u16 tnctrl
; /* TNCTRL1 & TNCTRL2 */
108 u16 frqchk
; /* FRQCHKMSB & FRQCHKLSB */
109 u16 tunchk
; /* IFCHK & LEVCHK */
110 u16 testreg
; /* TESTBITS & TESTMODE */
111 u16 rdsstat
; /* RDSSTAT1 & RDSSTAT2 */
112 u16 rdslb
; /* RDSLBMSB & RDSLBLSB */
113 u16 rdspb
; /* RDSPBMSB & RDSPBLSB */
114 u16 rdsbc
; /* RDSBBC & RDSGBC */
115 u16 rdsctrl
; /* RDSCTRL1 & RDSCTRL2 */
116 u16 rdsbbl
; /* PAUSEDET & RDSBBL */
117 u16 manid
; /* MANID1 & MANID2 */
118 u16 chipid
; /* CHIPID1 & CHIPID2 */
119 } __attribute__ ((packed
));
121 struct tea5764_write_regs
{
122 u8 intreg
; /* INTMSK */
123 __be16 frqset
; /* FRQSETMSB & FRQSETLSB */
124 __be16 tnctrl
; /* TNCTRL1 & TNCTRL2 */
125 __be16 testreg
; /* TESTBITS & TESTMODE */
126 __be16 rdsctrl
; /* RDSCTRL1 & RDSCTRL2 */
127 __be16 rdsbbl
; /* PAUSEDET & RDSBBL */
128 } __attribute__ ((packed
));
130 #ifdef CONFIG_RADIO_TEA5764_XTAL
131 #define RADIO_TEA5764_XTAL 1
133 #define RADIO_TEA5764_XTAL 0
136 static int radio_nr
= -1;
137 static int use_xtal
= RADIO_TEA5764_XTAL
;
139 struct tea5764_device
{
140 struct v4l2_device v4l2_dev
;
141 struct v4l2_ctrl_handler ctrl_handler
;
142 struct i2c_client
*i2c_client
;
143 struct video_device vdev
;
144 struct tea5764_regs regs
;
148 /* I2C code related */
149 static int tea5764_i2c_read(struct tea5764_device
*radio
)
152 u16
*p
= (u16
*) &radio
->regs
;
154 struct i2c_msg msgs
[1] = {
155 { .addr
= radio
->i2c_client
->addr
,
157 .len
= sizeof(radio
->regs
),
158 .buf
= (void *)&radio
->regs
161 if (i2c_transfer(radio
->i2c_client
->adapter
, msgs
, 1) != 1)
163 for (i
= 0; i
< sizeof(struct tea5764_regs
) / sizeof(u16
); i
++)
164 p
[i
] = __be16_to_cpu((__force __be16
)p
[i
]);
169 static int tea5764_i2c_write(struct tea5764_device
*radio
)
171 struct tea5764_write_regs wr
;
172 struct tea5764_regs
*r
= &radio
->regs
;
173 struct i2c_msg msgs
[1] = {
175 .addr
= radio
->i2c_client
->addr
,
180 wr
.intreg
= r
->intreg
& 0xff;
181 wr
.frqset
= __cpu_to_be16(r
->frqset
);
182 wr
.tnctrl
= __cpu_to_be16(r
->tnctrl
);
183 wr
.testreg
= __cpu_to_be16(r
->testreg
);
184 wr
.rdsctrl
= __cpu_to_be16(r
->rdsctrl
);
185 wr
.rdsbbl
= __cpu_to_be16(r
->rdsbbl
);
186 if (i2c_transfer(radio
->i2c_client
->adapter
, msgs
, 1) != 1)
191 static void tea5764_power_up(struct tea5764_device
*radio
)
193 struct tea5764_regs
*r
= &radio
->regs
;
195 if (!(r
->tnctrl
& TEA5764_TNCTRL_PUPD0
)) {
196 r
->tnctrl
&= ~(TEA5764_TNCTRL_AFM
| TEA5764_TNCTRL_MU
|
197 TEA5764_TNCTRL_HLSI
);
199 r
->testreg
|= TEA5764_TESTREG_TRIGFR
;
201 r
->testreg
&= ~TEA5764_TESTREG_TRIGFR
;
203 r
->tnctrl
|= TEA5764_TNCTRL_PUPD0
;
204 tea5764_i2c_write(radio
);
208 static void tea5764_power_down(struct tea5764_device
*radio
)
210 struct tea5764_regs
*r
= &radio
->regs
;
212 if (r
->tnctrl
& TEA5764_TNCTRL_PUPD0
) {
213 r
->tnctrl
&= ~TEA5764_TNCTRL_PUPD0
;
214 tea5764_i2c_write(radio
);
218 static void tea5764_set_freq(struct tea5764_device
*radio
, int freq
)
220 struct tea5764_regs
*r
= &radio
->regs
;
222 /* formula: (freq [+ or -] 225000) / 8192 */
223 if (r
->tnctrl
& TEA5764_TNCTRL_HLSI
)
224 r
->frqset
= (freq
+ 225000) / 8192;
226 r
->frqset
= (freq
- 225000) / 8192;
229 static int tea5764_get_freq(struct tea5764_device
*radio
)
231 struct tea5764_regs
*r
= &radio
->regs
;
233 if (r
->tnctrl
& TEA5764_TNCTRL_HLSI
)
234 return (r
->frqchk
* 8192) - 225000;
236 return (r
->frqchk
* 8192) + 225000;
239 /* tune an frequency, freq is defined by v4l's TUNER_LOW, i.e. 1/16th kHz */
240 static void tea5764_tune(struct tea5764_device
*radio
, int freq
)
242 tea5764_set_freq(radio
, freq
);
243 if (tea5764_i2c_write(radio
))
244 PWARN("Could not set frequency!");
247 static void tea5764_set_audout_mode(struct tea5764_device
*radio
, int audmode
)
249 struct tea5764_regs
*r
= &radio
->regs
;
250 int tnctrl
= r
->tnctrl
;
252 if (audmode
== V4L2_TUNER_MODE_MONO
)
253 r
->tnctrl
|= TEA5764_TNCTRL_MST
;
255 r
->tnctrl
&= ~TEA5764_TNCTRL_MST
;
256 if (tnctrl
!= r
->tnctrl
)
257 tea5764_i2c_write(radio
);
260 static int tea5764_get_audout_mode(struct tea5764_device
*radio
)
262 struct tea5764_regs
*r
= &radio
->regs
;
264 if (r
->tnctrl
& TEA5764_TNCTRL_MST
)
265 return V4L2_TUNER_MODE_MONO
;
267 return V4L2_TUNER_MODE_STEREO
;
270 static void tea5764_mute(struct tea5764_device
*radio
, int on
)
272 struct tea5764_regs
*r
= &radio
->regs
;
273 int tnctrl
= r
->tnctrl
;
276 r
->tnctrl
|= TEA5764_TNCTRL_MU
;
278 r
->tnctrl
&= ~TEA5764_TNCTRL_MU
;
279 if (tnctrl
!= r
->tnctrl
)
280 tea5764_i2c_write(radio
);
284 static int vidioc_querycap(struct file
*file
, void *priv
,
285 struct v4l2_capability
*v
)
287 struct tea5764_device
*radio
= video_drvdata(file
);
288 struct video_device
*dev
= &radio
->vdev
;
290 strlcpy(v
->driver
, dev
->dev
.driver
->name
, sizeof(v
->driver
));
291 strlcpy(v
->card
, dev
->name
, sizeof(v
->card
));
292 snprintf(v
->bus_info
, sizeof(v
->bus_info
),
293 "I2C:%s", dev_name(&dev
->dev
));
294 v
->device_caps
= V4L2_CAP_TUNER
| V4L2_CAP_RADIO
;
295 v
->capabilities
= v
->device_caps
| V4L2_CAP_DEVICE_CAPS
;
299 static int vidioc_g_tuner(struct file
*file
, void *priv
,
300 struct v4l2_tuner
*v
)
302 struct tea5764_device
*radio
= video_drvdata(file
);
303 struct tea5764_regs
*r
= &radio
->regs
;
308 strlcpy(v
->name
, "FM", sizeof(v
->name
));
309 v
->type
= V4L2_TUNER_RADIO
;
310 tea5764_i2c_read(radio
);
311 v
->rangelow
= FREQ_MIN
* FREQ_MUL
;
312 v
->rangehigh
= FREQ_MAX
* FREQ_MUL
;
313 v
->capability
= V4L2_TUNER_CAP_LOW
| V4L2_TUNER_CAP_STEREO
;
314 if (r
->tunchk
& TEA5764_TUNCHK_STEREO
)
315 v
->rxsubchans
= V4L2_TUNER_SUB_STEREO
;
317 v
->rxsubchans
= V4L2_TUNER_SUB_MONO
;
318 v
->audmode
= tea5764_get_audout_mode(radio
);
319 v
->signal
= TEA5764_TUNCHK_LEVEL(r
->tunchk
) * 0xffff / 0xf;
320 v
->afc
= TEA5764_TUNCHK_IFCNT(r
->tunchk
);
325 static int vidioc_s_tuner(struct file
*file
, void *priv
,
326 const struct v4l2_tuner
*v
)
328 struct tea5764_device
*radio
= video_drvdata(file
);
333 tea5764_set_audout_mode(radio
, v
->audmode
);
337 static int vidioc_s_frequency(struct file
*file
, void *priv
,
338 const struct v4l2_frequency
*f
)
340 struct tea5764_device
*radio
= video_drvdata(file
);
341 unsigned freq
= f
->frequency
;
343 if (f
->tuner
!= 0 || f
->type
!= V4L2_TUNER_RADIO
)
346 /* We special case this as a power down control. */
347 tea5764_power_down(radio
);
348 /* Yes, that's what is returned in this case. This
349 whole special case is non-compliant and should really
350 be replaced with something better, but changing this
351 might well break code that depends on this behavior.
352 So we keep it as-is. */
355 freq
= clamp(freq
, FREQ_MIN
* FREQ_MUL
, FREQ_MAX
* FREQ_MUL
);
356 tea5764_power_up(radio
);
357 tea5764_tune(radio
, (freq
* 125) / 2);
361 static int vidioc_g_frequency(struct file
*file
, void *priv
,
362 struct v4l2_frequency
*f
)
364 struct tea5764_device
*radio
= video_drvdata(file
);
365 struct tea5764_regs
*r
= &radio
->regs
;
369 tea5764_i2c_read(radio
);
370 f
->type
= V4L2_TUNER_RADIO
;
371 if (r
->tnctrl
& TEA5764_TNCTRL_PUPD0
)
372 f
->frequency
= (tea5764_get_freq(radio
) * 2) / 125;
379 static int tea5764_s_ctrl(struct v4l2_ctrl
*ctrl
)
381 struct tea5764_device
*radio
=
382 container_of(ctrl
->handler
, struct tea5764_device
, ctrl_handler
);
385 case V4L2_CID_AUDIO_MUTE
:
386 tea5764_mute(radio
, ctrl
->val
);
392 static const struct v4l2_ctrl_ops tea5764_ctrl_ops
= {
393 .s_ctrl
= tea5764_s_ctrl
,
396 /* File system interface */
397 static const struct v4l2_file_operations tea5764_fops
= {
398 .owner
= THIS_MODULE
,
399 .open
= v4l2_fh_open
,
400 .release
= v4l2_fh_release
,
401 .poll
= v4l2_ctrl_poll
,
402 .unlocked_ioctl
= video_ioctl2
,
405 static const struct v4l2_ioctl_ops tea5764_ioctl_ops
= {
406 .vidioc_querycap
= vidioc_querycap
,
407 .vidioc_g_tuner
= vidioc_g_tuner
,
408 .vidioc_s_tuner
= vidioc_s_tuner
,
409 .vidioc_g_frequency
= vidioc_g_frequency
,
410 .vidioc_s_frequency
= vidioc_s_frequency
,
411 .vidioc_log_status
= v4l2_ctrl_log_status
,
412 .vidioc_subscribe_event
= v4l2_ctrl_subscribe_event
,
413 .vidioc_unsubscribe_event
= v4l2_event_unsubscribe
,
417 static struct video_device tea5764_radio_template
= {
418 .name
= "TEA5764 FM-Radio",
419 .fops
= &tea5764_fops
,
420 .ioctl_ops
= &tea5764_ioctl_ops
,
421 .release
= video_device_release_empty
,
424 /* I2C probe: check if the device exists and register with v4l if it is */
425 static int tea5764_i2c_probe(struct i2c_client
*client
,
426 const struct i2c_device_id
*id
)
428 struct tea5764_device
*radio
;
429 struct v4l2_device
*v4l2_dev
;
430 struct v4l2_ctrl_handler
*hdl
;
431 struct tea5764_regs
*r
;
435 radio
= kzalloc(sizeof(struct tea5764_device
), GFP_KERNEL
);
439 v4l2_dev
= &radio
->v4l2_dev
;
440 ret
= v4l2_device_register(&client
->dev
, v4l2_dev
);
442 v4l2_err(v4l2_dev
, "could not register v4l2_device\n");
446 hdl
= &radio
->ctrl_handler
;
447 v4l2_ctrl_handler_init(hdl
, 1);
448 v4l2_ctrl_new_std(hdl
, &tea5764_ctrl_ops
,
449 V4L2_CID_AUDIO_MUTE
, 0, 1, 1, 1);
450 v4l2_dev
->ctrl_handler
= hdl
;
453 v4l2_err(v4l2_dev
, "Could not register controls\n");
457 mutex_init(&radio
->mutex
);
458 radio
->i2c_client
= client
;
459 ret
= tea5764_i2c_read(radio
);
463 PDEBUG("chipid = %04X, manid = %04X", r
->chipid
, r
->manid
);
464 if (r
->chipid
!= TEA5764_CHIPID
||
465 (r
->manid
& 0x0fff) != TEA5764_MANID
) {
466 PWARN("This chip is not a TEA5764!");
471 radio
->vdev
= tea5764_radio_template
;
473 i2c_set_clientdata(client
, radio
);
474 video_set_drvdata(&radio
->vdev
, radio
);
475 radio
->vdev
.lock
= &radio
->mutex
;
476 radio
->vdev
.v4l2_dev
= v4l2_dev
;
478 /* initialize and power off the chip */
479 tea5764_i2c_read(radio
);
480 tea5764_set_audout_mode(radio
, V4L2_TUNER_MODE_STEREO
);
481 tea5764_mute(radio
, 1);
482 tea5764_power_down(radio
);
484 ret
= video_register_device(&radio
->vdev
, VFL_TYPE_RADIO
, radio_nr
);
486 PWARN("Could not register video device!");
490 PINFO("registered.");
493 v4l2_ctrl_handler_free(hdl
);
494 v4l2_device_unregister(v4l2_dev
);
500 static int tea5764_i2c_remove(struct i2c_client
*client
)
502 struct tea5764_device
*radio
= i2c_get_clientdata(client
);
506 tea5764_power_down(radio
);
507 video_unregister_device(&radio
->vdev
);
508 v4l2_ctrl_handler_free(&radio
->ctrl_handler
);
509 v4l2_device_unregister(&radio
->v4l2_dev
);
515 /* I2C subsystem interface */
516 static const struct i2c_device_id tea5764_id
[] = {
517 { "radio-tea5764", 0 },
518 { } /* Terminating entry */
520 MODULE_DEVICE_TABLE(i2c
, tea5764_id
);
522 static struct i2c_driver tea5764_i2c_driver
= {
524 .name
= "radio-tea5764",
526 .probe
= tea5764_i2c_probe
,
527 .remove
= tea5764_i2c_remove
,
528 .id_table
= tea5764_id
,
531 module_i2c_driver(tea5764_i2c_driver
);
533 MODULE_AUTHOR(DRIVER_AUTHOR
);
534 MODULE_DESCRIPTION(DRIVER_DESC
);
535 MODULE_LICENSE("GPL");
536 MODULE_VERSION(DRIVER_VERSION
);
538 module_param(use_xtal
, int, 0);
539 MODULE_PARM_DESC(use_xtal
, "Chip have a xtal connected in board");
540 module_param(radio_nr
, int, 0);
541 MODULE_PARM_DESC(radio_nr
, "video4linux device number to use");