blob: b25a9c08ac022fe7adaf21670781770a2d9af46a [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Mauro Carvalho Chehabc5287ba2005-07-15 03:56:28 -07002 * $Id: tuner-core.c,v 1.58 2005/07/14 03:06:43 mchehab Exp $
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 *
4 * i2c tv tuner chip device driver
5 * core core, i.e. kernel interfaces, registering and so on
6 */
7
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>
22
23#include <media/tuner.h>
24#include <media/audiochip.h>
25
26#define UNSET (-1U)
27
28/* standard i2c insmod options */
29static unsigned short normal_i2c[] = {
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -070030 0x4b, /* tda8290 */
Mauro Carvalho Chehabf5bec392005-06-23 22:05:13 -070031 0x60, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67,
32 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f,
Linus Torvalds1da177e2005-04-16 15:20:36 -070033 I2C_CLIENT_END
34};
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -070035
Linus Torvalds1da177e2005-04-16 15:20:36 -070036I2C_CLIENT_INSMOD;
37
38/* insmod options used at init time => read/only */
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -070039static unsigned int addr = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070040module_param(addr, int, 0444);
41
Mauro Carvalho Chehabc5287ba2005-07-15 03:56:28 -070042static unsigned int no_autodetect = 0;
43module_param(no_autodetect, int, 0444);
44
Linus Torvalds1da177e2005-04-16 15:20:36 -070045/* insmod options used at runtime => read/write */
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -070046unsigned int tuner_debug = 0;
47module_param(tuner_debug, int, 0644);
Linus Torvalds1da177e2005-04-16 15:20:36 -070048
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -070049static unsigned int tv_range[2] = { 44, 958 };
Linus Torvalds1da177e2005-04-16 15:20:36 -070050static unsigned int radio_range[2] = { 65, 108 };
51
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -070052module_param_array(tv_range, int, NULL, 0644);
Linus Torvalds1da177e2005-04-16 15:20:36 -070053module_param_array(radio_range, int, NULL, 0644);
54
55MODULE_DESCRIPTION("device driver for various TV and TV+FM radio tuners");
56MODULE_AUTHOR("Ralph Metzler, Gerd Knorr, Gunther Mayer");
57MODULE_LICENSE("GPL");
58
Linus Torvalds1da177e2005-04-16 15:20:36 -070059static struct i2c_driver driver;
60static struct i2c_client client_template;
61
62/* ---------------------------------------------------------------------- */
63
Mauro Carvalho Chehab56fc08c2005-06-23 22:05:07 -070064/* Set tuner frequency, freq in Units of 62.5kHz = 1/16MHz */
Linus Torvalds1da177e2005-04-16 15:20:36 -070065static void set_tv_freq(struct i2c_client *c, unsigned int freq)
66{
67 struct tuner *t = i2c_get_clientdata(c);
68
69 if (t->type == UNSET) {
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -070070 tuner_warn ("tuner type not set\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070071 return;
72 }
73 if (NULL == t->tv_freq) {
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -070074 tuner_warn ("Tuner has no way to set tv freq\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070075 return;
76 }
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -070077 if (freq < tv_range[0] * 16 || freq > tv_range[1] * 16) {
78 tuner_dbg ("TV freq (%d.%02d) out of range (%d-%d)\n",
79 freq / 16, freq % 16 * 100 / 16, tv_range[0],
80 tv_range[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -070081 }
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -070082 t->tv_freq(c, freq);
Linus Torvalds1da177e2005-04-16 15:20:36 -070083}
84
85static void set_radio_freq(struct i2c_client *c, unsigned int freq)
86{
87 struct tuner *t = i2c_get_clientdata(c);
88
89 if (t->type == UNSET) {
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -070090 tuner_warn ("tuner type not set\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070091 return;
92 }
93 if (NULL == t->radio_freq) {
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -070094 tuner_warn ("tuner has no way to set radio frequency\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 return;
96 }
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -070097 if (freq <= radio_range[0] * 16000 || freq >= radio_range[1] * 16000) {
98 tuner_dbg ("radio freq (%d.%02d) out of range (%d-%d)\n",
99 freq / 16000, freq % 16000 * 100 / 16000,
100 radio_range[0], radio_range[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 }
Mauro Carvalho Chehab586b0ca2005-06-28 20:45:21 -0700102
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700103 t->radio_freq(c, freq);
Mauro Carvalho Chehab586b0ca2005-06-28 20:45:21 -0700104 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105}
106
107static void set_freq(struct i2c_client *c, unsigned long freq)
108{
109 struct tuner *t = i2c_get_clientdata(c);
110
111 switch (t->mode) {
112 case V4L2_TUNER_RADIO:
113 tuner_dbg("radio freq set to %lu.%02lu\n",
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700114 freq / 16000, freq % 16000 * 100 / 16000);
115 set_radio_freq(c, freq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 break;
117 case V4L2_TUNER_ANALOG_TV:
118 case V4L2_TUNER_DIGITAL_TV:
119 tuner_dbg("tv freq set to %lu.%02lu\n",
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700120 freq / 16, freq % 16 * 100 / 16);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 set_tv_freq(c, freq);
122 break;
123 }
124 t->freq = freq;
125}
126
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700127static void set_type(struct i2c_client *c, unsigned int type,
128 unsigned int new_mode_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129{
130 struct tuner *t = i2c_get_clientdata(c);
Mauro Carvalho Chehab586b0ca2005-06-28 20:45:21 -0700131 unsigned char buffer[4];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700133 if (type == UNSET || type == TUNER_ABSENT) {
134 tuner_dbg ("tuner 0x%02x: Tuner type absent\n",c->addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 return;
136 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700138 if (type >= tuner_count) {
139 tuner_warn ("tuner 0x%02x: Tuner count greater than %d\n",c->addr,tuner_count);
140 return;
141 }
142
143 /* This code detects calls by card attach_inform */
144 if (NULL == t->i2c.dev.driver) {
145 tuner_dbg ("tuner 0x%02x: called during i2c_client register by adapter's attach_inform\n", c->addr);
146
147 t->type=type;
148 return;
149 }
Mauro Carvalho Chehab56fc08c2005-06-23 22:05:07 -0700150
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 t->type = type;
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700152
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 switch (t->type) {
154 case TUNER_MT2032:
155 microtune_init(c);
156 break;
157 case TUNER_PHILIPS_TDA8290:
158 tda8290_init(c);
159 break;
Mauro Carvalho Chehab586b0ca2005-06-28 20:45:21 -0700160 case TUNER_TEA5767:
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700161 if (tea5767_tuner_init(c) == EINVAL) {
162 t->type = TUNER_ABSENT;
163 t->mode_mask = T_UNINITIALIZED;
164 return;
165 }
166 t->mode_mask = T_RADIO;
Mauro Carvalho Chehab586b0ca2005-06-28 20:45:21 -0700167 break;
168 case TUNER_PHILIPS_FMD1216ME_MK3:
169 buffer[0] = 0x0b;
170 buffer[1] = 0xdc;
171 buffer[2] = 0x9c;
172 buffer[3] = 0x60;
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700173 i2c_master_send(c, buffer, 4);
Mauro Carvalho Chehab586b0ca2005-06-28 20:45:21 -0700174 mdelay(1);
175 buffer[2] = 0x86;
176 buffer[3] = 0x54;
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700177 i2c_master_send(c, buffer, 4);
Mauro Carvalho Chehab586b0ca2005-06-28 20:45:21 -0700178 default_tuner_init(c);
179 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 default:
181 default_tuner_init(c);
182 break;
183 }
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700184
185 if (t->mode_mask == T_UNINITIALIZED)
186 t->mode_mask = new_mode_mask;
187
188 set_freq(c, t->freq);
189 tuner_dbg("%s %s I2C addr 0x%02x with type %d used for 0x%02x\n",
190 c->adapter->name, c->driver->name, c->addr << 1, type,
191 t->mode_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192}
193
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700194/*
195 * This function apply tuner config to tuner specified
196 * by tun_setup structure. I addr is unset, then admin status
197 * and tun addr status is more precise then current status,
198 * it's applied. Otherwise status and type are applied only to
199 * tuner with exactly the same addr.
200*/
Mauro Carvalho Chehab56fc08c2005-06-23 22:05:07 -0700201
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700202static void set_addr(struct i2c_client *c, struct tuner_setup *tun_setup)
Mauro Carvalho Chehab56fc08c2005-06-23 22:05:07 -0700203{
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700204 struct tuner *t = i2c_get_clientdata(c);
205
206 if (tun_setup->addr == ADDR_UNSET) {
207 if (t->mode_mask & tun_setup->mode_mask)
208 set_type(c, tun_setup->type, tun_setup->mode_mask);
209 } else if (tun_setup->addr == c->addr) {
210 set_type(c, tun_setup->type, tun_setup->mode_mask);
Mauro Carvalho Chehab56fc08c2005-06-23 22:05:07 -0700211 }
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700212}
213
214static inline int check_mode(struct tuner *t, char *cmd)
215{
216 if (1 << t->mode & t->mode_mask) {
217 switch (t->mode) {
218 case V4L2_TUNER_RADIO:
219 tuner_dbg("Cmd %s accepted for radio\n", cmd);
220 break;
221 case V4L2_TUNER_ANALOG_TV:
222 tuner_dbg("Cmd %s accepted for analog TV\n", cmd);
223 break;
224 case V4L2_TUNER_DIGITAL_TV:
225 tuner_dbg("Cmd %s accepted for digital TV\n", cmd);
226 break;
227 }
228 return 0;
229 }
230 return EINVAL;
Mauro Carvalho Chehab56fc08c2005-06-23 22:05:07 -0700231}
Mauro Carvalho Chehab56fc08c2005-06-23 22:05:07 -0700232
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233static char pal[] = "-";
Bert Wesarg975e0462005-04-16 15:25:43 -0700234module_param_string(pal, pal, sizeof(pal), 0644);
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700235static char secam[] = "-";
236module_param_string(secam, secam, sizeof(secam), 0644);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700238/* get more precise norm info from insmod option */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239static int tuner_fixup_std(struct tuner *t)
240{
241 if ((t->std & V4L2_STD_PAL) == V4L2_STD_PAL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 switch (pal[0]) {
243 case 'b':
244 case 'B':
245 case 'g':
246 case 'G':
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700247 tuner_dbg ("insmod fixup: PAL => PAL-BG\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 t->std = V4L2_STD_PAL_BG;
249 break;
250 case 'i':
251 case 'I':
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700252 tuner_dbg ("insmod fixup: PAL => PAL-I\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 t->std = V4L2_STD_PAL_I;
254 break;
255 case 'd':
256 case 'D':
257 case 'k':
258 case 'K':
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700259 tuner_dbg ("insmod fixup: PAL => PAL-DK\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 t->std = V4L2_STD_PAL_DK;
261 break;
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700262 case 'M':
263 case 'm':
264 tuner_dbg ("insmod fixup: PAL => PAL-M\n");
265 t->std = V4L2_STD_PAL_M;
266 break;
267 case 'N':
268 case 'n':
269 tuner_dbg ("insmod fixup: PAL => PAL-N\n");
270 t->std = V4L2_STD_PAL_N;
271 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 }
273 }
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700274 if ((t->std & V4L2_STD_SECAM) == V4L2_STD_SECAM) {
275 switch (secam[0]) {
276 case 'd':
277 case 'D':
278 case 'k':
279 case 'K':
280 tuner_dbg ("insmod fixup: SECAM => SECAM-DK\n");
281 t->std = V4L2_STD_SECAM_DK;
282 break;
283 case 'l':
284 case 'L':
285 tuner_dbg ("insmod fixup: SECAM => SECAM-L\n");
286 t->std = V4L2_STD_SECAM_L;
287 break;
288 }
289 }
290
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 return 0;
292}
293
294/* ---------------------------------------------------------------------- */
295
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700296/* static var Used only in tuner_attach and tuner_probe */
297static unsigned default_mode_mask;
298
299/* During client attach, set_type is called by adapter's attach_inform callback.
300 set_type must then be completed by tuner_attach.
301 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302static int tuner_attach(struct i2c_adapter *adap, int addr, int kind)
303{
304 struct tuner *t;
305
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700306 client_template.adapter = adap;
307 client_template.addr = addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700309 t = kmalloc(sizeof(struct tuner), GFP_KERNEL);
310 if (NULL == t)
311 return -ENOMEM;
312 memset(t, 0, sizeof(struct tuner));
313 memcpy(&t->i2c, &client_template, sizeof(struct i2c_client));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 i2c_set_clientdata(&t->i2c, t);
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700315 t->type = UNSET;
316 t->radio_if2 = 10700 * 1000; /* 10.7MHz - FM radio */
317 t->audmode = V4L2_TUNER_MODE_STEREO;
318 t->mode_mask = T_UNINITIALIZED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319
Mauro Carvalho Chehab56fc08c2005-06-23 22:05:07 -0700320
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700321 tuner_info("chip found @ 0x%x (%s)\n", addr << 1, adap->name);
322
323 /* TEA5767 autodetection code - only for addr = 0xc0 */
Mauro Carvalho Chehabc5287ba2005-07-15 03:56:28 -0700324 if (!no_autodetect) {
325 if (addr == 0x60) {
326 if (tea5767_autodetection(&t->i2c) != EINVAL) {
327 t->type = TUNER_TEA5767;
328 t->mode_mask = T_RADIO;
329 t->mode = T_STANDBY;
330 t->freq = 87.5 * 16; /* Sets freq to FM range */
331 default_mode_mask &= ~T_RADIO;
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700332
Mauro Carvalho Chehabc5287ba2005-07-15 03:56:28 -0700333 i2c_attach_client (&t->i2c);
334 set_type(&t->i2c,t->type, t->mode_mask);
335 return 0;
336 }
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700337 }
338 }
339
340 /* Initializes only the first adapter found */
341 if (default_mode_mask != T_UNINITIALIZED) {
342 tuner_dbg ("Setting mode_mask to 0x%02x\n", default_mode_mask);
343 t->mode_mask = default_mode_mask;
344 t->freq = 400 * 16; /* Sets freq to VHF High */
345 default_mode_mask = T_UNINITIALIZED;
346 }
347
348 /* Should be just before return */
349 i2c_attach_client (&t->i2c);
350 set_type (&t->i2c,t->type, t->mode_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 return 0;
352}
353
354static int tuner_probe(struct i2c_adapter *adap)
355{
356 if (0 != addr) {
Mauro Carvalho Chehabf5bec392005-06-23 22:05:13 -0700357 normal_i2c[0] = addr;
358 normal_i2c[1] = I2C_CLIENT_END;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700361 default_mode_mask = T_RADIO | T_ANALOG_TV | T_DIGITAL_TV;
Mauro Carvalho Chehab391cd722005-06-23 22:02:43 -0700362
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 if (adap->class & I2C_CLASS_TV_ANALOG)
364 return i2c_probe(adap, &addr_data, tuner_attach);
365 return 0;
366}
367
368static int tuner_detach(struct i2c_client *client)
369{
370 struct tuner *t = i2c_get_clientdata(client);
Mauro Carvalho Chehab391cd722005-06-23 22:02:43 -0700371 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700373 err = i2c_detach_client(&t->i2c);
Mauro Carvalho Chehab391cd722005-06-23 22:02:43 -0700374 if (err) {
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700375 tuner_warn
376 ("Client deregistration failed, client not detached.\n");
Mauro Carvalho Chehab391cd722005-06-23 22:02:43 -0700377 return err;
378 }
379
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 kfree(t);
381 return 0;
382}
383
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700384/*
385 * Switch tuner to other mode. If tuner support both tv and radio,
386 * set another frequency to some value (This is needed for some pal
387 * tuners to avoid locking). Otherwise, just put second tuner in
388 * standby mode.
389 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700391static inline int set_mode(struct i2c_client *client, struct tuner *t, int mode, char *cmd)
392{
393 if (mode != t->mode) {
394
395 t->mode = mode;
396 if (check_mode(t, cmd) == EINVAL) {
397 t->mode = T_STANDBY;
398 if (V4L2_TUNER_RADIO == mode) {
399 set_tv_freq(client, 400 * 16);
400 } else {
401 set_radio_freq(client, 87.5 * 16000);
402 }
403 return EINVAL;
404 }
405 }
406 return 0;
407}
408
409#define switch_v4l2() if (!t->using_v4l2) \
410 tuner_dbg("switching to v4l2\n"); \
411 t->using_v4l2 = 1;
412
413static inline int check_v4l2(struct tuner *t)
414{
415 if (t->using_v4l2) {
416 tuner_dbg ("ignore v4l1 call\n");
417 return EINVAL;
418 }
419 return 0;
420}
421
422static int tuner_command(struct i2c_client *client, unsigned int cmd, void *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423{
424 struct tuner *t = i2c_get_clientdata(client);
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700425 unsigned int *iarg = (int *)arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700427 switch (cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 /* --- configuration --- */
Mauro Carvalho Chehab56fc08c2005-06-23 22:05:07 -0700429 case TUNER_SET_TYPE_ADDR:
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700430 tuner_dbg ("Calling set_type_addr for type=%d, addr=0x%02x, mode=0x%02x\n",
431 ((struct tuner_setup *)arg)->type,
432 ((struct tuner_setup *)arg)->addr,
433 ((struct tuner_setup *)arg)->mode_mask);
434
435 set_addr(client, (struct tuner_setup *)arg);
Mauro Carvalho Chehab391cd722005-06-23 22:02:43 -0700436 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 case AUDC_SET_RADIO:
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700438 set_mode(client,t,V4L2_TUNER_RADIO, "AUDC_SET_RADIO");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 break;
440 case AUDC_CONFIG_PINNACLE:
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700441 if (check_mode(t, "AUDC_CONFIG_PINNACLE") == EINVAL)
442 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 switch (*iarg) {
444 case 2:
445 tuner_dbg("pinnacle pal\n");
446 t->radio_if2 = 33300 * 1000;
447 break;
448 case 3:
449 tuner_dbg("pinnacle ntsc\n");
450 t->radio_if2 = 41300 * 1000;
451 break;
452 }
Mauro Carvalho Chehab586b0ca2005-06-28 20:45:21 -0700453 break;
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700454 case TDA9887_SET_CONFIG:
455 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 /* --- v4l ioctls --- */
457 /* take care: bttv does userspace copying, we'll get a
458 kernel pointer here... */
459 case VIDIOCSCHAN:
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700460 {
461 static const v4l2_std_id map[] = {
462 [VIDEO_MODE_PAL] = V4L2_STD_PAL,
463 [VIDEO_MODE_NTSC] = V4L2_STD_NTSC_M,
464 [VIDEO_MODE_SECAM] = V4L2_STD_SECAM,
465 [4 /* bttv */ ] = V4L2_STD_PAL_M,
466 [5 /* bttv */ ] = V4L2_STD_PAL_N,
467 [6 /* bttv */ ] = V4L2_STD_NTSC_M_JP,
468 };
469 struct video_channel *vc = arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700471 if (check_v4l2(t) == EINVAL)
472 return 0;
Mauro Carvalho Chehab56fc08c2005-06-23 22:05:07 -0700473
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700474 if (set_mode(client,t,V4L2_TUNER_ANALOG_TV, "VIDIOCSCHAN")==EINVAL)
475 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700477 if (vc->norm < ARRAY_SIZE(map))
478 t->std = map[vc->norm];
479 tuner_fixup_std(t);
480 if (t->freq)
481 set_tv_freq(client, t->freq);
482 return 0;
Mauro Carvalho Chehab56fc08c2005-06-23 22:05:07 -0700483 }
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700484 case VIDIOCSFREQ:
485 {
486 unsigned long *v = arg;
Mauro Carvalho Chehab56fc08c2005-06-23 22:05:07 -0700487
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700488 if (check_mode(t, "VIDIOCSFREQ") == EINVAL)
489 return 0;
490 if (check_v4l2(t) == EINVAL)
491 return 0;
492
493 set_freq(client, *v);
494 return 0;
495 }
496 case VIDIOCGTUNER:
497 {
498 struct video_tuner *vt = arg;
499
500 if (check_mode(t, "VIDIOCGTUNER") == EINVAL)
501 return 0;
502 if (check_v4l2(t) == EINVAL)
503 return 0;
504
505 if (V4L2_TUNER_RADIO == t->mode) {
506 if (t->has_signal)
507 vt->signal = t->has_signal(client);
508 if (t->is_stereo) {
509 if (t->is_stereo(client))
510 vt->flags |=
511 VIDEO_TUNER_STEREO_ON;
512 else
513 vt->flags &=
514 ~VIDEO_TUNER_STEREO_ON;
515 }
516 vt->flags |= VIDEO_TUNER_LOW; /* Allow freqs at 62.5 Hz */
517
518 vt->rangelow = radio_range[0] * 16000;
519 vt->rangehigh = radio_range[1] * 16000;
520
521 } else {
522 vt->rangelow = tv_range[0] * 16;
523 vt->rangehigh = tv_range[1] * 16;
524 }
525
526 return 0;
527 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 case VIDIOCGAUDIO:
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700529 {
530 struct video_audio *va = arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700532 if (check_mode(t, "VIDIOCGAUDIO") == EINVAL)
533 return 0;
534 if (check_v4l2(t) == EINVAL)
535 return 0;
536
537 if (V4L2_TUNER_RADIO == t->mode && t->is_stereo)
538 va->mode = t->is_stereo(client)
539 ? VIDEO_SOUND_STEREO : VIDEO_SOUND_MONO;
540 return 0;
541 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542
543 case VIDIOC_S_STD:
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700544 {
545 v4l2_std_id *id = arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700547 if (set_mode (client, t, V4L2_TUNER_ANALOG_TV, "VIDIOC_S_STD")
548 == EINVAL)
549 return 0;
Mauro Carvalho Chehab56fc08c2005-06-23 22:05:07 -0700550
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700551 switch_v4l2();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700553 t->std = *id;
554 tuner_fixup_std(t);
555 if (t->freq)
556 set_freq(client, t->freq);
557 break;
Mauro Carvalho Chehab56fc08c2005-06-23 22:05:07 -0700558 }
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700559 case VIDIOC_S_FREQUENCY:
560 {
561 struct v4l2_frequency *f = arg;
Mauro Carvalho Chehab586b0ca2005-06-28 20:45:21 -0700562
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700563 t->freq = f->frequency;
564 switch_v4l2();
565 if (V4L2_TUNER_RADIO == f->type &&
566 V4L2_TUNER_RADIO != t->mode) {
567 if (set_mode (client, t, f->type, "VIDIOC_S_FREQUENCY")
568 == EINVAL)
569 return 0;
570 }
571 set_freq(client,t->freq);
Mauro Carvalho Chehab586b0ca2005-06-28 20:45:21 -0700572
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700573 break;
574 }
575 case VIDIOC_G_FREQUENCY:
576 {
577 struct v4l2_frequency *f = arg;
Mauro Carvalho Chehab586b0ca2005-06-28 20:45:21 -0700578
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700579 if (check_mode(t, "VIDIOC_G_FREQUENCY") == EINVAL)
580 return 0;
581 switch_v4l2();
582 f->type = t->mode;
583 f->frequency = t->freq;
584 break;
585 }
586 case VIDIOC_G_TUNER:
587 {
588 struct v4l2_tuner *tuner = arg;
Mauro Carvalho Chehab586b0ca2005-06-28 20:45:21 -0700589
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700590 if (check_mode(t, "VIDIOC_G_TUNER") == EINVAL)
591 return 0;
592 switch_v4l2();
593
594 if (V4L2_TUNER_RADIO == t->mode) {
595
596 if (t->has_signal)
597 tuner->signal = t->has_signal(client);
598
599 if (t->is_stereo) {
600 if (t->is_stereo(client)) {
601 tuner->rxsubchans =
602 V4L2_TUNER_SUB_STEREO |
603 V4L2_TUNER_SUB_MONO;
604 } else {
605 tuner->rxsubchans =
606 V4L2_TUNER_SUB_MONO;
607 }
608 }
609
610 tuner->capability |=
611 V4L2_TUNER_CAP_LOW | V4L2_TUNER_CAP_STEREO;
612
613 tuner->audmode = t->audmode;
614
615 tuner->rangelow = radio_range[0] * 16000;
616 tuner->rangehigh = radio_range[1] * 16000;
617 } else {
618 tuner->rangelow = tv_range[0] * 16;
619 tuner->rangehigh = tv_range[1] * 16;
620 }
621 break;
622 }
623 case VIDIOC_S_TUNER:
624 {
625 struct v4l2_tuner *tuner = arg;
626
627 if (check_mode(t, "VIDIOC_S_TUNER") == EINVAL)
628 return 0;
629
630 switch_v4l2();
631
632 if (V4L2_TUNER_RADIO == t->mode) {
633 t->audmode = tuner->audmode;
634 set_radio_freq(client, t->freq);
635 }
636 break;
637 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 default:
Mauro Carvalho Chehabc5287ba2005-07-15 03:56:28 -0700639 tuner_dbg("Unimplemented IOCTL 0x%08x(dir=%d,tp=0x%02x,nr=%d,sz=%d)\n",
640 cmd, _IOC_DIR(cmd), _IOC_TYPE(cmd),
641 _IOC_NR(cmd), _IOC_SIZE(cmd));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 break;
643 }
644
645 return 0;
646}
647
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700648static int tuner_suspend(struct device *dev, u32 state, u32 level)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649{
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700650 struct i2c_client *c = container_of (dev, struct i2c_client, dev);
651 struct tuner *t = i2c_get_clientdata (c);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700653 tuner_dbg ("suspend\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 /* FIXME: power down ??? */
655 return 0;
656}
657
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700658static int tuner_resume(struct device *dev, u32 level)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659{
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700660 struct i2c_client *c = container_of (dev, struct i2c_client, dev);
661 struct tuner *t = i2c_get_clientdata (c);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700663 tuner_dbg ("resume\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 if (t->freq)
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700665 set_freq(c, t->freq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 return 0;
667}
668
669/* ----------------------------------------------------------------------- */
670
671static struct i2c_driver driver = {
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700672 .owner = THIS_MODULE,
673 .name = "tuner",
674 .id = I2C_DRIVERID_TUNER,
675 .flags = I2C_DF_NOTIFY,
676 .attach_adapter = tuner_probe,
677 .detach_client = tuner_detach,
678 .command = tuner_command,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 .driver = {
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700680 .suspend = tuner_suspend,
681 .resume = tuner_resume,
682 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683};
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700684static struct i2c_client client_template = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 I2C_DEVNAME("(tuner unset)"),
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700686 .flags = I2C_CLIENT_ALLOW_USE,
687 .driver = &driver,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688};
689
690static int __init tuner_init_module(void)
691{
692 return i2c_add_driver(&driver);
693}
694
695static void __exit tuner_cleanup_module(void)
696{
697 i2c_del_driver(&driver);
698}
699
700module_init(tuner_init_module);
701module_exit(tuner_cleanup_module);
702
703/*
704 * Overrides for Emacs so that we follow Linus's tabbing style.
705 * ---------------------------------------------------------------------------
706 * Local variables:
707 * c-basic-offset: 8
708 * End:
709 */