2 * $Id: tuner-core.c,v 1.5 2005/02/15 15:59:35 kraxel Exp $
4 * i2c tv tuner chip device driver
5 * core core, i.e. kernel interfaces, registering and so on
8 #include <linux/module.h>
9 #include <linux/moduleparam.h>
10 #include <linux/kernel.h>
11 #include <linux/sched.h>
12 #include <linux/string.h>
13 #include <linux/timer.h>
14 #include <linux/delay.h>
15 #include <linux/errno.h>
16 #include <linux/slab.h>
17 #include <linux/poll.h>
18 #include <linux/i2c.h>
19 #include <linux/types.h>
20 #include <linux/videodev.h>
21 #include <linux/init.h>
23 #include <media/tuner.h>
24 #include <media/audiochip.h>
28 /* standard i2c insmod options */
29 static unsigned short normal_i2c
[] = {
33 static unsigned short normal_i2c_range
[] = {
39 /* insmod options used at init time => read/only */
40 static unsigned int addr
= 0;
41 module_param(addr
, int, 0444);
43 /* insmod options used at runtime => read/write */
44 unsigned int tuner_debug
= 0;
45 module_param(tuner_debug
, int, 0644);
47 static unsigned int tv_range
[2] = { 44, 958 };
48 static unsigned int radio_range
[2] = { 65, 108 };
50 module_param_array(tv_range
, int, NULL
, 0644);
51 module_param_array(radio_range
, int, NULL
, 0644);
53 MODULE_DESCRIPTION("device driver for various TV and TV+FM radio tuners");
54 MODULE_AUTHOR("Ralph Metzler, Gerd Knorr, Gunther Mayer");
55 MODULE_LICENSE("GPL");
59 static struct i2c_driver driver
;
60 static struct i2c_client client_template
;
62 /* ---------------------------------------------------------------------- */
64 // Set tuner frequency, freq in Units of 62.5kHz = 1/16MHz
65 static void set_tv_freq(struct i2c_client
*c
, unsigned int freq
)
67 struct tuner
*t
= i2c_get_clientdata(c
);
69 if (t
->type
== UNSET
) {
70 tuner_info("tuner type not set\n");
73 if (NULL
== t
->tv_freq
) {
74 tuner_info("Huh? tv_set is NULL?\n");
77 if (freq
< tv_range
[0]*16 || freq
> tv_range
[1]*16) {
78 /* FIXME: better do that chip-specific, but
79 right now we don't have that in the config
80 struct and this way is still better than no
82 tuner_info("TV freq (%d.%02d) out of range (%d-%d)\n",
83 freq
/16,freq
%16*100/16,tv_range
[0],tv_range
[1]);
89 static void set_radio_freq(struct i2c_client
*c
, unsigned int freq
)
91 struct tuner
*t
= i2c_get_clientdata(c
);
93 if (t
->type
== UNSET
) {
94 tuner_info("tuner type not set\n");
97 if (NULL
== t
->radio_freq
) {
98 tuner_info("no radio tuning for this one, sorry.\n");
101 if (freq
< radio_range
[0]*16 || freq
> radio_range
[1]*16) {
102 tuner_info("radio freq (%d.%02d) out of range (%d-%d)\n",
103 freq
/16,freq
%16*100/16,
104 radio_range
[0],radio_range
[1]);
107 t
->radio_freq(c
,freq
);
110 static void set_freq(struct i2c_client
*c
, unsigned long freq
)
112 struct tuner
*t
= i2c_get_clientdata(c
);
115 case V4L2_TUNER_RADIO
:
116 tuner_dbg("radio freq set to %lu.%02lu\n",
117 freq
/16,freq
%16*100/16);
118 set_radio_freq(c
,freq
);
120 case V4L2_TUNER_ANALOG_TV
:
121 case V4L2_TUNER_DIGITAL_TV
:
122 tuner_dbg("tv freq set to %lu.%02lu\n",
123 freq
/16,freq
%16*100/16);
124 set_tv_freq(c
, freq
);
130 static void set_type(struct i2c_client
*c
, unsigned int type
)
132 struct tuner
*t
= i2c_get_clientdata(c
);
135 if (type
== UNSET
|| type
== TUNER_ABSENT
)
137 if (type
>= tuner_count
)
140 if (NULL
== t
->i2c
.dev
.driver
) {
141 /* not registered yet */
155 case TUNER_PHILIPS_TDA8290
:
159 default_tuner_init(c
);
164 static char pal
[] = "-";
165 module_param_string(pal
, pal
, sizeof(pal
), 0644);
167 static int tuner_fixup_std(struct tuner
*t
)
169 if ((t
->std
& V4L2_STD_PAL
) == V4L2_STD_PAL
) {
170 /* get more precise norm info from insmod option */
176 tuner_dbg("insmod fixup: PAL => PAL-BG\n");
177 t
->std
= V4L2_STD_PAL_BG
;
181 tuner_dbg("insmod fixup: PAL => PAL-I\n");
182 t
->std
= V4L2_STD_PAL_I
;
188 tuner_dbg("insmod fixup: PAL => PAL-DK\n");
189 t
->std
= V4L2_STD_PAL_DK
;
196 /* ---------------------------------------------------------------------- */
198 static int tuner_attach(struct i2c_adapter
*adap
, int addr
, int kind
)
206 client_template
.adapter
= adap
;
207 client_template
.addr
= addr
;
209 t
= kmalloc(sizeof(struct tuner
),GFP_KERNEL
);
212 memset(t
,0,sizeof(struct tuner
));
213 memcpy(&t
->i2c
,&client_template
,sizeof(struct i2c_client
));
214 i2c_set_clientdata(&t
->i2c
, t
);
216 t
->radio_if2
= 10700*1000; // 10.7MHz - FM radio
218 i2c_attach_client(&t
->i2c
);
219 tuner_info("chip found @ 0x%x (%s)\n",
220 addr
<< 1, adap
->name
);
221 set_type(&t
->i2c
, t
->type
);
225 static int tuner_probe(struct i2c_adapter
*adap
)
228 normal_i2c
[0] = addr
;
229 normal_i2c_range
[0] = addr
;
230 normal_i2c_range
[1] = addr
;
234 if (adap
->class & I2C_CLASS_TV_ANALOG
)
235 return i2c_probe(adap
, &addr_data
, tuner_attach
);
239 static int tuner_detach(struct i2c_client
*client
)
241 struct tuner
*t
= i2c_get_clientdata(client
);
243 i2c_detach_client(&t
->i2c
);
248 #define SWITCH_V4L2 if (!t->using_v4l2 && tuner_debug) \
249 tuner_info("switching to v4l2\n"); \
251 #define CHECK_V4L2 if (t->using_v4l2) { if (tuner_debug) \
252 tuner_info("ignore v4l1 call\n"); \
256 tuner_command(struct i2c_client
*client
, unsigned int cmd
, void *arg
)
258 struct tuner
*t
= i2c_get_clientdata(client
);
259 unsigned int *iarg
= (int*)arg
;
263 /* --- configuration --- */
265 set_type(client
,*iarg
);
268 if (V4L2_TUNER_RADIO
!= t
->mode
) {
269 set_tv_freq(client
,400 * 16);
270 t
->mode
= V4L2_TUNER_RADIO
;
273 case AUDC_CONFIG_PINNACLE
:
276 tuner_dbg("pinnacle pal\n");
277 t
->radio_if2
= 33300 * 1000;
280 tuner_dbg("pinnacle ntsc\n");
281 t
->radio_if2
= 41300 * 1000;
286 /* --- v4l ioctls --- */
287 /* take care: bttv does userspace copying, we'll get a
288 kernel pointer here... */
291 static const v4l2_std_id map
[] = {
292 [ VIDEO_MODE_PAL
] = V4L2_STD_PAL
,
293 [ VIDEO_MODE_NTSC
] = V4L2_STD_NTSC_M
,
294 [ VIDEO_MODE_SECAM
] = V4L2_STD_SECAM
,
295 [ 4 /* bttv */ ] = V4L2_STD_PAL_M
,
296 [ 5 /* bttv */ ] = V4L2_STD_PAL_N
,
297 [ 6 /* bttv */ ] = V4L2_STD_NTSC_M_JP
,
299 struct video_channel
*vc
= arg
;
302 t
->mode
= V4L2_TUNER_ANALOG_TV
;
303 if (vc
->norm
< ARRAY_SIZE(map
))
304 t
->std
= map
[vc
->norm
];
307 set_tv_freq(client
,t
->freq
);
312 unsigned long *v
= arg
;
320 struct video_tuner
*vt
= arg
;
323 if (V4L2_TUNER_RADIO
== t
->mode
&& t
->has_signal
)
324 vt
->signal
= t
->has_signal(client
);
329 struct video_audio
*va
= arg
;
332 if (V4L2_TUNER_RADIO
== t
->mode
&& t
->is_stereo
)
333 va
->mode
= t
->is_stereo(client
)
341 v4l2_std_id
*id
= arg
;
344 t
->mode
= V4L2_TUNER_ANALOG_TV
;
348 set_freq(client
,t
->freq
);
351 case VIDIOC_S_FREQUENCY
:
353 struct v4l2_frequency
*f
= arg
;
356 if (V4L2_TUNER_RADIO
== f
->type
&&
357 V4L2_TUNER_RADIO
!= t
->mode
)
358 set_tv_freq(client
,400*16);
360 t
->freq
= f
->frequency
;
361 set_freq(client
,t
->freq
);
366 struct v4l2_tuner
*tuner
= arg
;
369 if (V4L2_TUNER_RADIO
== t
->mode
&& t
->has_signal
)
370 tuner
->signal
= t
->has_signal(client
);
381 static int tuner_suspend(struct device
* dev
, pm_message_t state
, u32 level
)
383 struct i2c_client
*c
= container_of(dev
, struct i2c_client
, dev
);
384 struct tuner
*t
= i2c_get_clientdata(c
);
386 tuner_dbg("suspend\n");
387 /* FIXME: power down ??? */
391 static int tuner_resume(struct device
* dev
, u32 level
)
393 struct i2c_client
*c
= container_of(dev
, struct i2c_client
, dev
);
394 struct tuner
*t
= i2c_get_clientdata(c
);
396 tuner_dbg("resume\n");
402 /* ----------------------------------------------------------------------- */
404 static struct i2c_driver driver
= {
405 .owner
= THIS_MODULE
,
407 .id
= I2C_DRIVERID_TUNER
,
408 .flags
= I2C_DF_NOTIFY
,
409 .attach_adapter
= tuner_probe
,
410 .detach_client
= tuner_detach
,
411 .command
= tuner_command
,
413 .suspend
= tuner_suspend
,
414 .resume
= tuner_resume
,
417 static struct i2c_client client_template
=
419 I2C_DEVNAME("(tuner unset)"),
420 .flags
= I2C_CLIENT_ALLOW_USE
,
424 static int __init
tuner_init_module(void)
426 return i2c_add_driver(&driver
);
429 static void __exit
tuner_cleanup_module(void)
431 i2c_del_driver(&driver
);
434 module_init(tuner_init_module
);
435 module_exit(tuner_cleanup_module
);
438 * Overrides for Emacs so that we follow Linus's tabbing style.
439 * ---------------------------------------------------------------------------