blob: a155e99a263b5bfb26a006c42065da2017fda76c [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Mauro Carvalho Chehabfd3113e2005-07-31 22:34:43 -07002 * $Id: tuner-core.c,v 1.63 2005/07/28 18:19:55 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
Mauro Carvalho Chehabfd3113e2005-07-31 22:34:43 -070026#include "msp3400.h"
27
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#define UNSET (-1U)
29
30/* standard i2c insmod options */
31static unsigned short normal_i2c[] = {
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -070032 0x4b, /* tda8290 */
Mauro Carvalho Chehabf5bec392005-06-23 22:05:13 -070033 0x60, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67,
34 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f,
Linus Torvalds1da177e2005-04-16 15:20:36 -070035 I2C_CLIENT_END
36};
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -070037
Linus Torvalds1da177e2005-04-16 15:20:36 -070038I2C_CLIENT_INSMOD;
39
40/* insmod options used at init time => read/only */
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -070041static unsigned int addr = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070042module_param(addr, int, 0444);
43
Mauro Carvalho Chehabc5287ba2005-07-15 03:56:28 -070044static unsigned int no_autodetect = 0;
45module_param(no_autodetect, int, 0444);
46
Mauro Carvalho Chehabfd3113e2005-07-31 22:34:43 -070047static unsigned int show_i2c = 0;
48module_param(show_i2c, int, 0444);
49
Linus Torvalds1da177e2005-04-16 15:20:36 -070050/* insmod options used at runtime => read/write */
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -070051unsigned int tuner_debug = 0;
52module_param(tuner_debug, int, 0644);
Linus Torvalds1da177e2005-04-16 15:20:36 -070053
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -070054static unsigned int tv_range[2] = { 44, 958 };
Linus Torvalds1da177e2005-04-16 15:20:36 -070055static unsigned int radio_range[2] = { 65, 108 };
56
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -070057module_param_array(tv_range, int, NULL, 0644);
Linus Torvalds1da177e2005-04-16 15:20:36 -070058module_param_array(radio_range, int, NULL, 0644);
59
60MODULE_DESCRIPTION("device driver for various TV and TV+FM radio tuners");
61MODULE_AUTHOR("Ralph Metzler, Gerd Knorr, Gunther Mayer");
62MODULE_LICENSE("GPL");
63
Linus Torvalds1da177e2005-04-16 15:20:36 -070064static struct i2c_driver driver;
65static struct i2c_client client_template;
66
67/* ---------------------------------------------------------------------- */
68
Mauro Carvalho Chehab56fc08c2005-06-23 22:05:07 -070069/* Set tuner frequency, freq in Units of 62.5kHz = 1/16MHz */
Linus Torvalds1da177e2005-04-16 15:20:36 -070070static void set_tv_freq(struct i2c_client *c, unsigned int freq)
71{
72 struct tuner *t = i2c_get_clientdata(c);
73
74 if (t->type == UNSET) {
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -070075 tuner_warn ("tuner type not set\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070076 return;
77 }
78 if (NULL == t->tv_freq) {
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -070079 tuner_warn ("Tuner has no way to set tv freq\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070080 return;
81 }
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -070082 if (freq < tv_range[0] * 16 || freq > tv_range[1] * 16) {
83 tuner_dbg ("TV freq (%d.%02d) out of range (%d-%d)\n",
84 freq / 16, freq % 16 * 100 / 16, tv_range[0],
85 tv_range[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -070086 }
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -070087 t->tv_freq(c, freq);
Linus Torvalds1da177e2005-04-16 15:20:36 -070088}
89
90static void set_radio_freq(struct i2c_client *c, unsigned int freq)
91{
92 struct tuner *t = i2c_get_clientdata(c);
93
94 if (t->type == UNSET) {
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -070095 tuner_warn ("tuner type not set\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 return;
97 }
98 if (NULL == t->radio_freq) {
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -070099 tuner_warn ("tuner has no way to set radio frequency\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 return;
101 }
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700102 if (freq <= radio_range[0] * 16000 || freq >= radio_range[1] * 16000) {
103 tuner_dbg ("radio freq (%d.%02d) out of range (%d-%d)\n",
104 freq / 16000, freq % 16000 * 100 / 16000,
105 radio_range[0], radio_range[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 }
Mauro Carvalho Chehab586b0ca2005-06-28 20:45:21 -0700107
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700108 t->radio_freq(c, freq);
Mauro Carvalho Chehab586b0ca2005-06-28 20:45:21 -0700109 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110}
111
112static void set_freq(struct i2c_client *c, unsigned long freq)
113{
114 struct tuner *t = i2c_get_clientdata(c);
115
116 switch (t->mode) {
117 case V4L2_TUNER_RADIO:
118 tuner_dbg("radio freq set to %lu.%02lu\n",
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700119 freq / 16000, freq % 16000 * 100 / 16000);
120 set_radio_freq(c, freq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 break;
122 case V4L2_TUNER_ANALOG_TV:
123 case V4L2_TUNER_DIGITAL_TV:
124 tuner_dbg("tv freq set to %lu.%02lu\n",
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700125 freq / 16, freq % 16 * 100 / 16);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 set_tv_freq(c, freq);
127 break;
128 }
129 t->freq = freq;
130}
131
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700132static void set_type(struct i2c_client *c, unsigned int type,
133 unsigned int new_mode_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134{
135 struct tuner *t = i2c_get_clientdata(c);
Mauro Carvalho Chehab586b0ca2005-06-28 20:45:21 -0700136 unsigned char buffer[4];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700138 if (type == UNSET || type == TUNER_ABSENT) {
139 tuner_dbg ("tuner 0x%02x: Tuner type absent\n",c->addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 return;
141 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700143 if (type >= tuner_count) {
144 tuner_warn ("tuner 0x%02x: Tuner count greater than %d\n",c->addr,tuner_count);
145 return;
146 }
147
148 /* This code detects calls by card attach_inform */
149 if (NULL == t->i2c.dev.driver) {
150 tuner_dbg ("tuner 0x%02x: called during i2c_client register by adapter's attach_inform\n", c->addr);
151
152 t->type=type;
153 return;
154 }
Mauro Carvalho Chehab56fc08c2005-06-23 22:05:07 -0700155
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 t->type = type;
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700157
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 switch (t->type) {
159 case TUNER_MT2032:
160 microtune_init(c);
161 break;
162 case TUNER_PHILIPS_TDA8290:
163 tda8290_init(c);
164 break;
Mauro Carvalho Chehab586b0ca2005-06-28 20:45:21 -0700165 case TUNER_TEA5767:
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700166 if (tea5767_tuner_init(c) == EINVAL) {
167 t->type = TUNER_ABSENT;
168 t->mode_mask = T_UNINITIALIZED;
169 return;
170 }
171 t->mode_mask = T_RADIO;
Mauro Carvalho Chehab586b0ca2005-06-28 20:45:21 -0700172 break;
173 case TUNER_PHILIPS_FMD1216ME_MK3:
174 buffer[0] = 0x0b;
175 buffer[1] = 0xdc;
176 buffer[2] = 0x9c;
177 buffer[3] = 0x60;
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700178 i2c_master_send(c, buffer, 4);
Mauro Carvalho Chehab586b0ca2005-06-28 20:45:21 -0700179 mdelay(1);
180 buffer[2] = 0x86;
181 buffer[3] = 0x54;
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700182 i2c_master_send(c, buffer, 4);
Mauro Carvalho Chehab586b0ca2005-06-28 20:45:21 -0700183 default_tuner_init(c);
184 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 default:
186 default_tuner_init(c);
187 break;
188 }
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700189
190 if (t->mode_mask == T_UNINITIALIZED)
191 t->mode_mask = new_mode_mask;
192
193 set_freq(c, t->freq);
194 tuner_dbg("%s %s I2C addr 0x%02x with type %d used for 0x%02x\n",
195 c->adapter->name, c->driver->name, c->addr << 1, type,
196 t->mode_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197}
198
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700199/*
200 * This function apply tuner config to tuner specified
201 * by tun_setup structure. I addr is unset, then admin status
202 * and tun addr status is more precise then current status,
203 * it's applied. Otherwise status and type are applied only to
204 * tuner with exactly the same addr.
205*/
Mauro Carvalho Chehab56fc08c2005-06-23 22:05:07 -0700206
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700207static void set_addr(struct i2c_client *c, struct tuner_setup *tun_setup)
Mauro Carvalho Chehab56fc08c2005-06-23 22:05:07 -0700208{
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700209 struct tuner *t = i2c_get_clientdata(c);
210
211 if (tun_setup->addr == ADDR_UNSET) {
212 if (t->mode_mask & tun_setup->mode_mask)
213 set_type(c, tun_setup->type, tun_setup->mode_mask);
214 } else if (tun_setup->addr == c->addr) {
215 set_type(c, tun_setup->type, tun_setup->mode_mask);
Mauro Carvalho Chehab56fc08c2005-06-23 22:05:07 -0700216 }
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700217}
218
219static inline int check_mode(struct tuner *t, char *cmd)
220{
221 if (1 << t->mode & t->mode_mask) {
222 switch (t->mode) {
223 case V4L2_TUNER_RADIO:
224 tuner_dbg("Cmd %s accepted for radio\n", cmd);
225 break;
226 case V4L2_TUNER_ANALOG_TV:
227 tuner_dbg("Cmd %s accepted for analog TV\n", cmd);
228 break;
229 case V4L2_TUNER_DIGITAL_TV:
230 tuner_dbg("Cmd %s accepted for digital TV\n", cmd);
231 break;
232 }
233 return 0;
234 }
235 return EINVAL;
Mauro Carvalho Chehab56fc08c2005-06-23 22:05:07 -0700236}
Mauro Carvalho Chehab56fc08c2005-06-23 22:05:07 -0700237
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238static char pal[] = "-";
Bert Wesarg975e0462005-04-16 15:25:43 -0700239module_param_string(pal, pal, sizeof(pal), 0644);
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700240static char secam[] = "-";
241module_param_string(secam, secam, sizeof(secam), 0644);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700243/* get more precise norm info from insmod option */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244static int tuner_fixup_std(struct tuner *t)
245{
246 if ((t->std & V4L2_STD_PAL) == V4L2_STD_PAL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 switch (pal[0]) {
248 case 'b':
249 case 'B':
250 case 'g':
251 case 'G':
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700252 tuner_dbg ("insmod fixup: PAL => PAL-BG\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 t->std = V4L2_STD_PAL_BG;
254 break;
255 case 'i':
256 case 'I':
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700257 tuner_dbg ("insmod fixup: PAL => PAL-I\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 t->std = V4L2_STD_PAL_I;
259 break;
260 case 'd':
261 case 'D':
262 case 'k':
263 case 'K':
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700264 tuner_dbg ("insmod fixup: PAL => PAL-DK\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 t->std = V4L2_STD_PAL_DK;
266 break;
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700267 case 'M':
268 case 'm':
269 tuner_dbg ("insmod fixup: PAL => PAL-M\n");
270 t->std = V4L2_STD_PAL_M;
271 break;
272 case 'N':
273 case 'n':
274 tuner_dbg ("insmod fixup: PAL => PAL-N\n");
275 t->std = V4L2_STD_PAL_N;
276 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 }
278 }
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700279 if ((t->std & V4L2_STD_SECAM) == V4L2_STD_SECAM) {
280 switch (secam[0]) {
281 case 'd':
282 case 'D':
283 case 'k':
284 case 'K':
285 tuner_dbg ("insmod fixup: SECAM => SECAM-DK\n");
286 t->std = V4L2_STD_SECAM_DK;
287 break;
288 case 'l':
289 case 'L':
290 tuner_dbg ("insmod fixup: SECAM => SECAM-L\n");
291 t->std = V4L2_STD_SECAM_L;
292 break;
293 }
294 }
295
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 return 0;
297}
298
299/* ---------------------------------------------------------------------- */
300
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700301/* static var Used only in tuner_attach and tuner_probe */
302static unsigned default_mode_mask;
303
304/* During client attach, set_type is called by adapter's attach_inform callback.
305 set_type must then be completed by tuner_attach.
306 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307static int tuner_attach(struct i2c_adapter *adap, int addr, int kind)
308{
309 struct tuner *t;
310
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700311 client_template.adapter = adap;
312 client_template.addr = addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700314 t = kmalloc(sizeof(struct tuner), GFP_KERNEL);
315 if (NULL == t)
316 return -ENOMEM;
317 memset(t, 0, sizeof(struct tuner));
318 memcpy(&t->i2c, &client_template, sizeof(struct i2c_client));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 i2c_set_clientdata(&t->i2c, t);
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700320 t->type = UNSET;
321 t->radio_if2 = 10700 * 1000; /* 10.7MHz - FM radio */
322 t->audmode = V4L2_TUNER_MODE_STEREO;
323 t->mode_mask = T_UNINITIALIZED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324
Mauro Carvalho Chehab56fc08c2005-06-23 22:05:07 -0700325
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700326 tuner_info("chip found @ 0x%x (%s)\n", addr << 1, adap->name);
327
Mauro Carvalho Chehabfd3113e2005-07-31 22:34:43 -0700328 if (show_i2c) {
329 unsigned char buffer[16];
330 int i,rc;
331
332 memset(buffer, 0, sizeof(buffer));
333 rc = i2c_master_recv(&t->i2c, buffer, sizeof(buffer));
334 printk("tuner-%04x I2C RECV = ",addr);
335 for (i=0;i<rc;i++)
336 printk("%02x ",buffer[i]);
337 printk("\n");
338 }
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700339 /* TEA5767 autodetection code - only for addr = 0xc0 */
Mauro Carvalho Chehabc5287ba2005-07-15 03:56:28 -0700340 if (!no_autodetect) {
341 if (addr == 0x60) {
342 if (tea5767_autodetection(&t->i2c) != EINVAL) {
343 t->type = TUNER_TEA5767;
344 t->mode_mask = T_RADIO;
345 t->mode = T_STANDBY;
346 t->freq = 87.5 * 16; /* Sets freq to FM range */
347 default_mode_mask &= ~T_RADIO;
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700348
Mauro Carvalho Chehabc5287ba2005-07-15 03:56:28 -0700349 i2c_attach_client (&t->i2c);
350 set_type(&t->i2c,t->type, t->mode_mask);
351 return 0;
352 }
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700353 }
354 }
355
356 /* Initializes only the first adapter found */
357 if (default_mode_mask != T_UNINITIALIZED) {
358 tuner_dbg ("Setting mode_mask to 0x%02x\n", default_mode_mask);
359 t->mode_mask = default_mode_mask;
360 t->freq = 400 * 16; /* Sets freq to VHF High */
361 default_mode_mask = T_UNINITIALIZED;
362 }
363
364 /* Should be just before return */
365 i2c_attach_client (&t->i2c);
366 set_type (&t->i2c,t->type, t->mode_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 return 0;
368}
369
370static int tuner_probe(struct i2c_adapter *adap)
371{
372 if (0 != addr) {
Mauro Carvalho Chehabf5bec392005-06-23 22:05:13 -0700373 normal_i2c[0] = addr;
374 normal_i2c[1] = I2C_CLIENT_END;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700377 default_mode_mask = T_RADIO | T_ANALOG_TV | T_DIGITAL_TV;
Mauro Carvalho Chehab391cd722005-06-23 22:02:43 -0700378
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 if (adap->class & I2C_CLASS_TV_ANALOG)
380 return i2c_probe(adap, &addr_data, tuner_attach);
381 return 0;
382}
383
384static int tuner_detach(struct i2c_client *client)
385{
386 struct tuner *t = i2c_get_clientdata(client);
Mauro Carvalho Chehab391cd722005-06-23 22:02:43 -0700387 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700389 err = i2c_detach_client(&t->i2c);
Mauro Carvalho Chehab391cd722005-06-23 22:02:43 -0700390 if (err) {
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700391 tuner_warn
392 ("Client deregistration failed, client not detached.\n");
Mauro Carvalho Chehab391cd722005-06-23 22:02:43 -0700393 return err;
394 }
395
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 kfree(t);
397 return 0;
398}
399
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700400/*
401 * Switch tuner to other mode. If tuner support both tv and radio,
402 * set another frequency to some value (This is needed for some pal
403 * tuners to avoid locking). Otherwise, just put second tuner in
404 * standby mode.
405 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700407static inline int set_mode(struct i2c_client *client, struct tuner *t, int mode, char *cmd)
408{
409 if (mode != t->mode) {
410
411 t->mode = mode;
412 if (check_mode(t, cmd) == EINVAL) {
413 t->mode = T_STANDBY;
414 if (V4L2_TUNER_RADIO == mode) {
415 set_tv_freq(client, 400 * 16);
416 } else {
417 set_radio_freq(client, 87.5 * 16000);
418 }
419 return EINVAL;
420 }
421 }
422 return 0;
423}
424
425#define switch_v4l2() if (!t->using_v4l2) \
426 tuner_dbg("switching to v4l2\n"); \
427 t->using_v4l2 = 1;
428
429static inline int check_v4l2(struct tuner *t)
430{
431 if (t->using_v4l2) {
432 tuner_dbg ("ignore v4l1 call\n");
433 return EINVAL;
434 }
435 return 0;
436}
437
438static int tuner_command(struct i2c_client *client, unsigned int cmd, void *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439{
440 struct tuner *t = i2c_get_clientdata(client);
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700441 unsigned int *iarg = (int *)arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700443 switch (cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 /* --- configuration --- */
Mauro Carvalho Chehab56fc08c2005-06-23 22:05:07 -0700445 case TUNER_SET_TYPE_ADDR:
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700446 tuner_dbg ("Calling set_type_addr for type=%d, addr=0x%02x, mode=0x%02x\n",
447 ((struct tuner_setup *)arg)->type,
448 ((struct tuner_setup *)arg)->addr,
449 ((struct tuner_setup *)arg)->mode_mask);
450
451 set_addr(client, (struct tuner_setup *)arg);
Mauro Carvalho Chehab391cd722005-06-23 22:02:43 -0700452 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 case AUDC_SET_RADIO:
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700454 set_mode(client,t,V4L2_TUNER_RADIO, "AUDC_SET_RADIO");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 break;
456 case AUDC_CONFIG_PINNACLE:
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700457 if (check_mode(t, "AUDC_CONFIG_PINNACLE") == EINVAL)
458 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 switch (*iarg) {
460 case 2:
461 tuner_dbg("pinnacle pal\n");
462 t->radio_if2 = 33300 * 1000;
463 break;
464 case 3:
465 tuner_dbg("pinnacle ntsc\n");
466 t->radio_if2 = 41300 * 1000;
467 break;
468 }
Mauro Carvalho Chehab586b0ca2005-06-28 20:45:21 -0700469 break;
Mauro Carvalho Chehabfd3113e2005-07-31 22:34:43 -0700470 case VIDIOCSAUDIO:
471 if (check_mode(t, "VIDIOCSAUDIO") == EINVAL)
472 return 0;
473 if (check_v4l2(t) == EINVAL)
474 return 0;
475
476 /* Should be implemented, since bttv calls it */
477 tuner_dbg("VIDIOCSAUDIO not implemented.\n");
478
479 break;
480 case MSP_SET_MATRIX:
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700481 case TDA9887_SET_CONFIG:
482 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 /* --- v4l ioctls --- */
484 /* take care: bttv does userspace copying, we'll get a
485 kernel pointer here... */
486 case VIDIOCSCHAN:
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700487 {
488 static const v4l2_std_id map[] = {
489 [VIDEO_MODE_PAL] = V4L2_STD_PAL,
490 [VIDEO_MODE_NTSC] = V4L2_STD_NTSC_M,
491 [VIDEO_MODE_SECAM] = V4L2_STD_SECAM,
492 [4 /* bttv */ ] = V4L2_STD_PAL_M,
493 [5 /* bttv */ ] = V4L2_STD_PAL_N,
494 [6 /* bttv */ ] = V4L2_STD_NTSC_M_JP,
495 };
496 struct video_channel *vc = arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700498 if (check_v4l2(t) == EINVAL)
499 return 0;
Mauro Carvalho Chehab56fc08c2005-06-23 22:05:07 -0700500
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700501 if (set_mode(client,t,V4L2_TUNER_ANALOG_TV, "VIDIOCSCHAN")==EINVAL)
502 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700504 if (vc->norm < ARRAY_SIZE(map))
505 t->std = map[vc->norm];
506 tuner_fixup_std(t);
507 if (t->freq)
508 set_tv_freq(client, t->freq);
509 return 0;
Mauro Carvalho Chehab56fc08c2005-06-23 22:05:07 -0700510 }
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700511 case VIDIOCSFREQ:
512 {
513 unsigned long *v = arg;
Mauro Carvalho Chehab56fc08c2005-06-23 22:05:07 -0700514
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700515 if (check_mode(t, "VIDIOCSFREQ") == EINVAL)
516 return 0;
517 if (check_v4l2(t) == EINVAL)
518 return 0;
519
520 set_freq(client, *v);
521 return 0;
522 }
523 case VIDIOCGTUNER:
524 {
525 struct video_tuner *vt = arg;
526
527 if (check_mode(t, "VIDIOCGTUNER") == EINVAL)
528 return 0;
529 if (check_v4l2(t) == EINVAL)
530 return 0;
531
532 if (V4L2_TUNER_RADIO == t->mode) {
533 if (t->has_signal)
534 vt->signal = t->has_signal(client);
535 if (t->is_stereo) {
536 if (t->is_stereo(client))
537 vt->flags |=
538 VIDEO_TUNER_STEREO_ON;
539 else
540 vt->flags &=
541 ~VIDEO_TUNER_STEREO_ON;
542 }
543 vt->flags |= VIDEO_TUNER_LOW; /* Allow freqs at 62.5 Hz */
544
545 vt->rangelow = radio_range[0] * 16000;
546 vt->rangehigh = radio_range[1] * 16000;
547
548 } else {
549 vt->rangelow = tv_range[0] * 16;
550 vt->rangehigh = tv_range[1] * 16;
551 }
552
553 return 0;
554 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 case VIDIOCGAUDIO:
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700556 {
557 struct video_audio *va = arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700559 if (check_mode(t, "VIDIOCGAUDIO") == EINVAL)
560 return 0;
561 if (check_v4l2(t) == EINVAL)
562 return 0;
563
564 if (V4L2_TUNER_RADIO == t->mode && t->is_stereo)
565 va->mode = t->is_stereo(client)
566 ? VIDEO_SOUND_STEREO : VIDEO_SOUND_MONO;
567 return 0;
568 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569
570 case VIDIOC_S_STD:
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700571 {
572 v4l2_std_id *id = arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700574 if (set_mode (client, t, V4L2_TUNER_ANALOG_TV, "VIDIOC_S_STD")
575 == EINVAL)
576 return 0;
Mauro Carvalho Chehab56fc08c2005-06-23 22:05:07 -0700577
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700578 switch_v4l2();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700580 t->std = *id;
581 tuner_fixup_std(t);
582 if (t->freq)
583 set_freq(client, t->freq);
584 break;
Mauro Carvalho Chehab56fc08c2005-06-23 22:05:07 -0700585 }
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700586 case VIDIOC_S_FREQUENCY:
587 {
588 struct v4l2_frequency *f = arg;
Mauro Carvalho Chehab586b0ca2005-06-28 20:45:21 -0700589
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700590 t->freq = f->frequency;
591 switch_v4l2();
592 if (V4L2_TUNER_RADIO == f->type &&
593 V4L2_TUNER_RADIO != t->mode) {
594 if (set_mode (client, t, f->type, "VIDIOC_S_FREQUENCY")
595 == EINVAL)
596 return 0;
597 }
598 set_freq(client,t->freq);
Mauro Carvalho Chehab586b0ca2005-06-28 20:45:21 -0700599
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700600 break;
601 }
602 case VIDIOC_G_FREQUENCY:
603 {
604 struct v4l2_frequency *f = arg;
Mauro Carvalho Chehab586b0ca2005-06-28 20:45:21 -0700605
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700606 if (check_mode(t, "VIDIOC_G_FREQUENCY") == EINVAL)
607 return 0;
608 switch_v4l2();
609 f->type = t->mode;
610 f->frequency = t->freq;
611 break;
612 }
613 case VIDIOC_G_TUNER:
614 {
615 struct v4l2_tuner *tuner = arg;
Mauro Carvalho Chehab586b0ca2005-06-28 20:45:21 -0700616
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700617 if (check_mode(t, "VIDIOC_G_TUNER") == EINVAL)
618 return 0;
619 switch_v4l2();
620
621 if (V4L2_TUNER_RADIO == t->mode) {
622
623 if (t->has_signal)
624 tuner->signal = t->has_signal(client);
625
626 if (t->is_stereo) {
627 if (t->is_stereo(client)) {
628 tuner->rxsubchans =
629 V4L2_TUNER_SUB_STEREO |
630 V4L2_TUNER_SUB_MONO;
631 } else {
632 tuner->rxsubchans =
633 V4L2_TUNER_SUB_MONO;
634 }
635 }
636
637 tuner->capability |=
638 V4L2_TUNER_CAP_LOW | V4L2_TUNER_CAP_STEREO;
639
640 tuner->audmode = t->audmode;
641
642 tuner->rangelow = radio_range[0] * 16000;
643 tuner->rangehigh = radio_range[1] * 16000;
644 } else {
645 tuner->rangelow = tv_range[0] * 16;
646 tuner->rangehigh = tv_range[1] * 16;
647 }
648 break;
649 }
650 case VIDIOC_S_TUNER:
651 {
652 struct v4l2_tuner *tuner = arg;
653
654 if (check_mode(t, "VIDIOC_S_TUNER") == EINVAL)
655 return 0;
656
657 switch_v4l2();
658
659 if (V4L2_TUNER_RADIO == t->mode) {
660 t->audmode = tuner->audmode;
661 set_radio_freq(client, t->freq);
662 }
663 break;
664 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 default:
Mauro Carvalho Chehabc5287ba2005-07-15 03:56:28 -0700666 tuner_dbg("Unimplemented IOCTL 0x%08x(dir=%d,tp=0x%02x,nr=%d,sz=%d)\n",
667 cmd, _IOC_DIR(cmd), _IOC_TYPE(cmd),
668 _IOC_NR(cmd), _IOC_SIZE(cmd));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669 break;
670 }
671
672 return 0;
673}
674
Pavel Machek829ca9a2005-09-03 15:56:56 -0700675static int tuner_suspend(struct device *dev, pm_message_t state, u32 level)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676{
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700677 struct i2c_client *c = container_of (dev, struct i2c_client, dev);
678 struct tuner *t = i2c_get_clientdata (c);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700680 tuner_dbg ("suspend\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 /* FIXME: power down ??? */
682 return 0;
683}
684
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700685static int tuner_resume(struct device *dev, u32 level)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686{
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700687 struct i2c_client *c = container_of (dev, struct i2c_client, dev);
688 struct tuner *t = i2c_get_clientdata (c);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700690 tuner_dbg ("resume\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 if (t->freq)
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700692 set_freq(c, t->freq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 return 0;
694}
695
696/* ----------------------------------------------------------------------- */
697
698static struct i2c_driver driver = {
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700699 .owner = THIS_MODULE,
700 .name = "tuner",
701 .id = I2C_DRIVERID_TUNER,
702 .flags = I2C_DF_NOTIFY,
703 .attach_adapter = tuner_probe,
704 .detach_client = tuner_detach,
705 .command = tuner_command,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 .driver = {
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700707 .suspend = tuner_suspend,
708 .resume = tuner_resume,
709 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710};
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700711static struct i2c_client client_template = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712 I2C_DEVNAME("(tuner unset)"),
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700713 .flags = I2C_CLIENT_ALLOW_USE,
714 .driver = &driver,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715};
716
717static int __init tuner_init_module(void)
718{
719 return i2c_add_driver(&driver);
720}
721
722static void __exit tuner_cleanup_module(void)
723{
724 i2c_del_driver(&driver);
725}
726
727module_init(tuner_init_module);
728module_exit(tuner_cleanup_module);
729
730/*
731 * Overrides for Emacs so that we follow Linus's tabbing style.
732 * ---------------------------------------------------------------------------
733 * Local variables:
734 * c-basic-offset: 8
735 * End:
736 */