blob: afc96bbb1c118cbd2029111021c61fb79b877378 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 *
3 * i2c tv tuner chip device driver
4 * core core, i.e. kernel interfaces, registering and so on
5 */
6
7#include <linux/module.h>
8#include <linux/moduleparam.h>
9#include <linux/kernel.h>
10#include <linux/sched.h>
11#include <linux/string.h>
12#include <linux/timer.h>
13#include <linux/delay.h>
14#include <linux/errno.h>
15#include <linux/slab.h>
16#include <linux/poll.h>
17#include <linux/i2c.h>
18#include <linux/types.h>
19#include <linux/videodev.h>
20#include <linux/init.h>
21
22#include <media/tuner.h>
23#include <media/audiochip.h>
24
Mauro Carvalho Chehabfd3113e2005-07-31 22:34:43 -070025#include "msp3400.h"
26
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#define UNSET (-1U)
28
29/* standard i2c insmod options */
30static unsigned short normal_i2c[] = {
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -070031 0x4b, /* tda8290 */
Mauro Carvalho Chehabf5bec392005-06-23 22:05:13 -070032 0x60, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67,
33 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f,
Linus Torvalds1da177e2005-04-16 15:20:36 -070034 I2C_CLIENT_END
35};
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -070036
Linus Torvalds1da177e2005-04-16 15:20:36 -070037I2C_CLIENT_INSMOD;
38
39/* insmod options used at init time => read/only */
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -070040static unsigned int addr = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070041module_param(addr, int, 0444);
42
Mauro Carvalho Chehabc5287ba2005-07-15 03:56:28 -070043static unsigned int no_autodetect = 0;
44module_param(no_autodetect, int, 0444);
45
Mauro Carvalho Chehabfd3113e2005-07-31 22:34:43 -070046static unsigned int show_i2c = 0;
47module_param(show_i2c, int, 0444);
48
Linus Torvalds1da177e2005-04-16 15:20:36 -070049/* insmod options used at runtime => read/write */
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -070050unsigned int tuner_debug = 0;
51module_param(tuner_debug, int, 0644);
Linus Torvalds1da177e2005-04-16 15:20:36 -070052
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -070053static unsigned int tv_range[2] = { 44, 958 };
Linus Torvalds1da177e2005-04-16 15:20:36 -070054static unsigned int radio_range[2] = { 65, 108 };
55
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -070056module_param_array(tv_range, int, NULL, 0644);
Linus Torvalds1da177e2005-04-16 15:20:36 -070057module_param_array(radio_range, int, NULL, 0644);
58
59MODULE_DESCRIPTION("device driver for various TV and TV+FM radio tuners");
60MODULE_AUTHOR("Ralph Metzler, Gerd Knorr, Gunther Mayer");
61MODULE_LICENSE("GPL");
62
Linus Torvalds1da177e2005-04-16 15:20:36 -070063static struct i2c_driver driver;
64static struct i2c_client client_template;
65
66/* ---------------------------------------------------------------------- */
67
Mauro Carvalho Chehab56fc08c2005-06-23 22:05:07 -070068/* Set tuner frequency, freq in Units of 62.5kHz = 1/16MHz */
Linus Torvalds1da177e2005-04-16 15:20:36 -070069static void set_tv_freq(struct i2c_client *c, unsigned int freq)
70{
71 struct tuner *t = i2c_get_clientdata(c);
72
73 if (t->type == UNSET) {
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -070074 tuner_warn ("tuner type not set\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070075 return;
76 }
77 if (NULL == t->tv_freq) {
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -070078 tuner_warn ("Tuner has no way to set tv freq\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070079 return;
80 }
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -070081 if (freq < tv_range[0] * 16 || freq > tv_range[1] * 16) {
82 tuner_dbg ("TV freq (%d.%02d) out of range (%d-%d)\n",
83 freq / 16, freq % 16 * 100 / 16, tv_range[0],
84 tv_range[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -070085 }
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -070086 t->tv_freq(c, freq);
Linus Torvalds1da177e2005-04-16 15:20:36 -070087}
88
89static void set_radio_freq(struct i2c_client *c, unsigned int freq)
90{
91 struct tuner *t = i2c_get_clientdata(c);
92
93 if (t->type == UNSET) {
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -070094 tuner_warn ("tuner type not set\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 return;
96 }
97 if (NULL == t->radio_freq) {
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -070098 tuner_warn ("tuner has no way to set radio frequency\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 return;
100 }
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700101 if (freq <= radio_range[0] * 16000 || freq >= radio_range[1] * 16000) {
102 tuner_dbg ("radio freq (%d.%02d) out of range (%d-%d)\n",
103 freq / 16000, freq % 16000 * 100 / 16000,
104 radio_range[0], radio_range[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 }
Mauro Carvalho Chehab586b0ca2005-06-28 20:45:21 -0700106
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700107 t->radio_freq(c, freq);
Mauro Carvalho Chehab586b0ca2005-06-28 20:45:21 -0700108 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109}
110
111static void set_freq(struct i2c_client *c, unsigned long freq)
112{
113 struct tuner *t = i2c_get_clientdata(c);
114
115 switch (t->mode) {
116 case V4L2_TUNER_RADIO:
117 tuner_dbg("radio freq set to %lu.%02lu\n",
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700118 freq / 16000, freq % 16000 * 100 / 16000);
119 set_radio_freq(c, freq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 break;
121 case V4L2_TUNER_ANALOG_TV:
122 case V4L2_TUNER_DIGITAL_TV:
123 tuner_dbg("tv freq set to %lu.%02lu\n",
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700124 freq / 16, freq % 16 * 100 / 16);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 set_tv_freq(c, freq);
126 break;
127 }
128 t->freq = freq;
129}
130
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700131static void set_type(struct i2c_client *c, unsigned int type,
132 unsigned int new_mode_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133{
134 struct tuner *t = i2c_get_clientdata(c);
Mauro Carvalho Chehab586b0ca2005-06-28 20:45:21 -0700135 unsigned char buffer[4];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700137 if (type == UNSET || type == TUNER_ABSENT) {
138 tuner_dbg ("tuner 0x%02x: Tuner type absent\n",c->addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 return;
140 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700142 if (type >= tuner_count) {
143 tuner_warn ("tuner 0x%02x: Tuner count greater than %d\n",c->addr,tuner_count);
144 return;
145 }
146
147 /* This code detects calls by card attach_inform */
148 if (NULL == t->i2c.dev.driver) {
149 tuner_dbg ("tuner 0x%02x: called during i2c_client register by adapter's attach_inform\n", c->addr);
150
151 t->type=type;
152 return;
153 }
Mauro Carvalho Chehab56fc08c2005-06-23 22:05:07 -0700154
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 t->type = type;
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700156
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 switch (t->type) {
158 case TUNER_MT2032:
159 microtune_init(c);
160 break;
161 case TUNER_PHILIPS_TDA8290:
162 tda8290_init(c);
163 break;
Mauro Carvalho Chehab586b0ca2005-06-28 20:45:21 -0700164 case TUNER_TEA5767:
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700165 if (tea5767_tuner_init(c) == EINVAL) {
166 t->type = TUNER_ABSENT;
167 t->mode_mask = T_UNINITIALIZED;
168 return;
169 }
170 t->mode_mask = T_RADIO;
Mauro Carvalho Chehab586b0ca2005-06-28 20:45:21 -0700171 break;
172 case TUNER_PHILIPS_FMD1216ME_MK3:
173 buffer[0] = 0x0b;
174 buffer[1] = 0xdc;
175 buffer[2] = 0x9c;
176 buffer[3] = 0x60;
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700177 i2c_master_send(c, buffer, 4);
Mauro Carvalho Chehab586b0ca2005-06-28 20:45:21 -0700178 mdelay(1);
179 buffer[2] = 0x86;
180 buffer[3] = 0x54;
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700181 i2c_master_send(c, buffer, 4);
Mauro Carvalho Chehab586b0ca2005-06-28 20:45:21 -0700182 default_tuner_init(c);
183 break;
Mauro Carvalho Chehab793cf9e2005-09-09 13:03:37 -0700184 case TUNER_LG_TDVS_H062F:
185 /* Set the Auxiliary Byte. */
186 buffer[2] &= ~0x20;
187 buffer[2] |= 0x18;
188 buffer[3] = 0x20;
189 i2c_master_send(c, buffer, 4);
190 default_tuner_init(c);
191 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 default:
193 default_tuner_init(c);
194 break;
195 }
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700196
197 if (t->mode_mask == T_UNINITIALIZED)
198 t->mode_mask = new_mode_mask;
199
200 set_freq(c, t->freq);
201 tuner_dbg("%s %s I2C addr 0x%02x with type %d used for 0x%02x\n",
202 c->adapter->name, c->driver->name, c->addr << 1, type,
203 t->mode_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204}
205
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700206/*
207 * This function apply tuner config to tuner specified
208 * by tun_setup structure. I addr is unset, then admin status
209 * and tun addr status is more precise then current status,
210 * it's applied. Otherwise status and type are applied only to
211 * tuner with exactly the same addr.
212*/
Mauro Carvalho Chehab56fc08c2005-06-23 22:05:07 -0700213
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700214static void set_addr(struct i2c_client *c, struct tuner_setup *tun_setup)
Mauro Carvalho Chehab56fc08c2005-06-23 22:05:07 -0700215{
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700216 struct tuner *t = i2c_get_clientdata(c);
217
Mauro Carvalho Chehab793cf9e2005-09-09 13:03:37 -0700218 if ((tun_setup->addr == ADDR_UNSET &&
219 (t->mode_mask & tun_setup->mode_mask)) ||
220 tun_setup->addr == c->addr) {
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700221 set_type(c, tun_setup->type, tun_setup->mode_mask);
Mauro Carvalho Chehab56fc08c2005-06-23 22:05:07 -0700222 }
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700223}
224
225static inline int check_mode(struct tuner *t, char *cmd)
226{
Mauro Carvalho Chehab793cf9e2005-09-09 13:03:37 -0700227 if ((1 << t->mode & t->mode_mask) == 0) {
228 return EINVAL;
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700229 }
Mauro Carvalho Chehab793cf9e2005-09-09 13:03:37 -0700230
231 switch (t->mode) {
232 case V4L2_TUNER_RADIO:
233 tuner_dbg("Cmd %s accepted for radio\n", cmd);
234 break;
235 case V4L2_TUNER_ANALOG_TV:
236 tuner_dbg("Cmd %s accepted for analog TV\n", cmd);
237 break;
238 case V4L2_TUNER_DIGITAL_TV:
239 tuner_dbg("Cmd %s accepted for digital TV\n", cmd);
240 break;
241 }
242 return 0;
Mauro Carvalho Chehab56fc08c2005-06-23 22:05:07 -0700243}
Mauro Carvalho Chehab56fc08c2005-06-23 22:05:07 -0700244
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245static char pal[] = "-";
Bert Wesarg975e0462005-04-16 15:25:43 -0700246module_param_string(pal, pal, sizeof(pal), 0644);
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700247static char secam[] = "-";
248module_param_string(secam, secam, sizeof(secam), 0644);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700250/* get more precise norm info from insmod option */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251static int tuner_fixup_std(struct tuner *t)
252{
253 if ((t->std & V4L2_STD_PAL) == V4L2_STD_PAL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 switch (pal[0]) {
255 case 'b':
256 case 'B':
257 case 'g':
258 case 'G':
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700259 tuner_dbg ("insmod fixup: PAL => PAL-BG\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 t->std = V4L2_STD_PAL_BG;
261 break;
262 case 'i':
263 case 'I':
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700264 tuner_dbg ("insmod fixup: PAL => PAL-I\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 t->std = V4L2_STD_PAL_I;
266 break;
267 case 'd':
268 case 'D':
269 case 'k':
270 case 'K':
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700271 tuner_dbg ("insmod fixup: PAL => PAL-DK\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 t->std = V4L2_STD_PAL_DK;
273 break;
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700274 case 'M':
275 case 'm':
276 tuner_dbg ("insmod fixup: PAL => PAL-M\n");
277 t->std = V4L2_STD_PAL_M;
278 break;
279 case 'N':
280 case 'n':
281 tuner_dbg ("insmod fixup: PAL => PAL-N\n");
282 t->std = V4L2_STD_PAL_N;
283 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 }
285 }
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700286 if ((t->std & V4L2_STD_SECAM) == V4L2_STD_SECAM) {
287 switch (secam[0]) {
288 case 'd':
289 case 'D':
290 case 'k':
291 case 'K':
292 tuner_dbg ("insmod fixup: SECAM => SECAM-DK\n");
293 t->std = V4L2_STD_SECAM_DK;
294 break;
295 case 'l':
296 case 'L':
297 tuner_dbg ("insmod fixup: SECAM => SECAM-L\n");
298 t->std = V4L2_STD_SECAM_L;
299 break;
300 }
301 }
302
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 return 0;
304}
305
306/* ---------------------------------------------------------------------- */
307
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700308/* static var Used only in tuner_attach and tuner_probe */
309static unsigned default_mode_mask;
310
311/* During client attach, set_type is called by adapter's attach_inform callback.
312 set_type must then be completed by tuner_attach.
313 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314static int tuner_attach(struct i2c_adapter *adap, int addr, int kind)
315{
316 struct tuner *t;
317
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700318 client_template.adapter = adap;
319 client_template.addr = addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700321 t = kmalloc(sizeof(struct tuner), GFP_KERNEL);
322 if (NULL == t)
323 return -ENOMEM;
324 memset(t, 0, sizeof(struct tuner));
325 memcpy(&t->i2c, &client_template, sizeof(struct i2c_client));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 i2c_set_clientdata(&t->i2c, t);
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700327 t->type = UNSET;
328 t->radio_if2 = 10700 * 1000; /* 10.7MHz - FM radio */
329 t->audmode = V4L2_TUNER_MODE_STEREO;
330 t->mode_mask = T_UNINITIALIZED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331
Mauro Carvalho Chehab56fc08c2005-06-23 22:05:07 -0700332
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700333 tuner_info("chip found @ 0x%x (%s)\n", addr << 1, adap->name);
334
Mauro Carvalho Chehabfd3113e2005-07-31 22:34:43 -0700335 if (show_i2c) {
336 unsigned char buffer[16];
337 int i,rc;
338
339 memset(buffer, 0, sizeof(buffer));
340 rc = i2c_master_recv(&t->i2c, buffer, sizeof(buffer));
341 printk("tuner-%04x I2C RECV = ",addr);
342 for (i=0;i<rc;i++)
343 printk("%02x ",buffer[i]);
344 printk("\n");
345 }
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700346 /* TEA5767 autodetection code - only for addr = 0xc0 */
Mauro Carvalho Chehabc5287ba2005-07-15 03:56:28 -0700347 if (!no_autodetect) {
348 if (addr == 0x60) {
349 if (tea5767_autodetection(&t->i2c) != EINVAL) {
350 t->type = TUNER_TEA5767;
351 t->mode_mask = T_RADIO;
352 t->mode = T_STANDBY;
353 t->freq = 87.5 * 16; /* Sets freq to FM range */
354 default_mode_mask &= ~T_RADIO;
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700355
Mauro Carvalho Chehabc5287ba2005-07-15 03:56:28 -0700356 i2c_attach_client (&t->i2c);
357 set_type(&t->i2c,t->type, t->mode_mask);
358 return 0;
359 }
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700360 }
361 }
362
363 /* Initializes only the first adapter found */
364 if (default_mode_mask != T_UNINITIALIZED) {
365 tuner_dbg ("Setting mode_mask to 0x%02x\n", default_mode_mask);
366 t->mode_mask = default_mode_mask;
367 t->freq = 400 * 16; /* Sets freq to VHF High */
368 default_mode_mask = T_UNINITIALIZED;
369 }
370
371 /* Should be just before return */
372 i2c_attach_client (&t->i2c);
373 set_type (&t->i2c,t->type, t->mode_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 return 0;
375}
376
377static int tuner_probe(struct i2c_adapter *adap)
378{
379 if (0 != addr) {
Mauro Carvalho Chehabf5bec392005-06-23 22:05:13 -0700380 normal_i2c[0] = addr;
381 normal_i2c[1] = I2C_CLIENT_END;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700384 default_mode_mask = T_RADIO | T_ANALOG_TV | T_DIGITAL_TV;
Mauro Carvalho Chehab391cd722005-06-23 22:02:43 -0700385
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 if (adap->class & I2C_CLASS_TV_ANALOG)
387 return i2c_probe(adap, &addr_data, tuner_attach);
388 return 0;
389}
390
391static int tuner_detach(struct i2c_client *client)
392{
393 struct tuner *t = i2c_get_clientdata(client);
Mauro Carvalho Chehab391cd722005-06-23 22:02:43 -0700394 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700396 err = i2c_detach_client(&t->i2c);
Mauro Carvalho Chehab391cd722005-06-23 22:02:43 -0700397 if (err) {
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700398 tuner_warn
399 ("Client deregistration failed, client not detached.\n");
Mauro Carvalho Chehab391cd722005-06-23 22:02:43 -0700400 return err;
401 }
402
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 kfree(t);
404 return 0;
405}
406
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700407/*
408 * Switch tuner to other mode. If tuner support both tv and radio,
409 * set another frequency to some value (This is needed for some pal
410 * tuners to avoid locking). Otherwise, just put second tuner in
411 * standby mode.
412 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700414static inline int set_mode(struct i2c_client *client, struct tuner *t, int mode, char *cmd)
415{
Mauro Carvalho Chehab793cf9e2005-09-09 13:03:37 -0700416 if (mode == t->mode)
417 return 0;
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700418
Mauro Carvalho Chehab793cf9e2005-09-09 13:03:37 -0700419 t->mode = mode;
420
421 if (check_mode(t, cmd) == EINVAL) {
422 t->mode = T_STANDBY;
423 if (t->standby)
424 t->standby (client);
425 return EINVAL;
426 }
427 return 0;
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700428}
429
430#define switch_v4l2() if (!t->using_v4l2) \
431 tuner_dbg("switching to v4l2\n"); \
432 t->using_v4l2 = 1;
433
434static inline int check_v4l2(struct tuner *t)
435{
436 if (t->using_v4l2) {
437 tuner_dbg ("ignore v4l1 call\n");
438 return EINVAL;
439 }
440 return 0;
441}
442
443static int tuner_command(struct i2c_client *client, unsigned int cmd, void *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444{
445 struct tuner *t = i2c_get_clientdata(client);
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700446 unsigned int *iarg = (int *)arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700448 switch (cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 /* --- configuration --- */
Mauro Carvalho Chehab56fc08c2005-06-23 22:05:07 -0700450 case TUNER_SET_TYPE_ADDR:
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700451 tuner_dbg ("Calling set_type_addr for type=%d, addr=0x%02x, mode=0x%02x\n",
452 ((struct tuner_setup *)arg)->type,
453 ((struct tuner_setup *)arg)->addr,
454 ((struct tuner_setup *)arg)->mode_mask);
455
456 set_addr(client, (struct tuner_setup *)arg);
Mauro Carvalho Chehab391cd722005-06-23 22:02:43 -0700457 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 case AUDC_SET_RADIO:
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700459 set_mode(client,t,V4L2_TUNER_RADIO, "AUDC_SET_RADIO");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 break;
Mauro Carvalho Chehab793cf9e2005-09-09 13:03:37 -0700461 case TUNER_SET_STANDBY:
462 {
463 if (check_mode(t, "TUNER_SET_STANDBY") == EINVAL)
464 return 0;
465 if (t->standby)
466 t->standby (client);
467 break;
468 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 case AUDC_CONFIG_PINNACLE:
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700470 if (check_mode(t, "AUDC_CONFIG_PINNACLE") == EINVAL)
471 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 switch (*iarg) {
473 case 2:
474 tuner_dbg("pinnacle pal\n");
475 t->radio_if2 = 33300 * 1000;
476 break;
477 case 3:
478 tuner_dbg("pinnacle ntsc\n");
479 t->radio_if2 = 41300 * 1000;
480 break;
481 }
Mauro Carvalho Chehab586b0ca2005-06-28 20:45:21 -0700482 break;
Mauro Carvalho Chehabfd3113e2005-07-31 22:34:43 -0700483 case VIDIOCSAUDIO:
484 if (check_mode(t, "VIDIOCSAUDIO") == EINVAL)
485 return 0;
486 if (check_v4l2(t) == EINVAL)
487 return 0;
488
489 /* Should be implemented, since bttv calls it */
490 tuner_dbg("VIDIOCSAUDIO not implemented.\n");
491
492 break;
493 case MSP_SET_MATRIX:
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700494 case TDA9887_SET_CONFIG:
495 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 /* --- v4l ioctls --- */
497 /* take care: bttv does userspace copying, we'll get a
498 kernel pointer here... */
499 case VIDIOCSCHAN:
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700500 {
501 static const v4l2_std_id map[] = {
502 [VIDEO_MODE_PAL] = V4L2_STD_PAL,
503 [VIDEO_MODE_NTSC] = V4L2_STD_NTSC_M,
504 [VIDEO_MODE_SECAM] = V4L2_STD_SECAM,
505 [4 /* bttv */ ] = V4L2_STD_PAL_M,
506 [5 /* bttv */ ] = V4L2_STD_PAL_N,
507 [6 /* bttv */ ] = V4L2_STD_NTSC_M_JP,
508 };
509 struct video_channel *vc = arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700511 if (check_v4l2(t) == EINVAL)
512 return 0;
Mauro Carvalho Chehab56fc08c2005-06-23 22:05:07 -0700513
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700514 if (set_mode(client,t,V4L2_TUNER_ANALOG_TV, "VIDIOCSCHAN")==EINVAL)
515 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700517 if (vc->norm < ARRAY_SIZE(map))
518 t->std = map[vc->norm];
519 tuner_fixup_std(t);
520 if (t->freq)
521 set_tv_freq(client, t->freq);
522 return 0;
Mauro Carvalho Chehab56fc08c2005-06-23 22:05:07 -0700523 }
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700524 case VIDIOCSFREQ:
525 {
526 unsigned long *v = arg;
Mauro Carvalho Chehab56fc08c2005-06-23 22:05:07 -0700527
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700528 if (check_mode(t, "VIDIOCSFREQ") == EINVAL)
529 return 0;
530 if (check_v4l2(t) == EINVAL)
531 return 0;
532
533 set_freq(client, *v);
534 return 0;
535 }
536 case VIDIOCGTUNER:
537 {
538 struct video_tuner *vt = arg;
539
540 if (check_mode(t, "VIDIOCGTUNER") == EINVAL)
541 return 0;
542 if (check_v4l2(t) == EINVAL)
543 return 0;
544
545 if (V4L2_TUNER_RADIO == t->mode) {
546 if (t->has_signal)
547 vt->signal = t->has_signal(client);
548 if (t->is_stereo) {
549 if (t->is_stereo(client))
550 vt->flags |=
551 VIDEO_TUNER_STEREO_ON;
552 else
553 vt->flags &=
554 ~VIDEO_TUNER_STEREO_ON;
555 }
556 vt->flags |= VIDEO_TUNER_LOW; /* Allow freqs at 62.5 Hz */
557
558 vt->rangelow = radio_range[0] * 16000;
559 vt->rangehigh = radio_range[1] * 16000;
560
561 } else {
562 vt->rangelow = tv_range[0] * 16;
563 vt->rangehigh = tv_range[1] * 16;
564 }
565
566 return 0;
567 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 case VIDIOCGAUDIO:
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700569 {
570 struct video_audio *va = arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700572 if (check_mode(t, "VIDIOCGAUDIO") == EINVAL)
573 return 0;
574 if (check_v4l2(t) == EINVAL)
575 return 0;
576
577 if (V4L2_TUNER_RADIO == t->mode && t->is_stereo)
578 va->mode = t->is_stereo(client)
579 ? VIDEO_SOUND_STEREO : VIDEO_SOUND_MONO;
580 return 0;
581 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582
583 case VIDIOC_S_STD:
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700584 {
585 v4l2_std_id *id = arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700587 if (set_mode (client, t, V4L2_TUNER_ANALOG_TV, "VIDIOC_S_STD")
588 == EINVAL)
589 return 0;
Mauro Carvalho Chehab56fc08c2005-06-23 22:05:07 -0700590
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700591 switch_v4l2();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700593 t->std = *id;
594 tuner_fixup_std(t);
595 if (t->freq)
596 set_freq(client, t->freq);
597 break;
Mauro Carvalho Chehab56fc08c2005-06-23 22:05:07 -0700598 }
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700599 case VIDIOC_S_FREQUENCY:
600 {
601 struct v4l2_frequency *f = arg;
Mauro Carvalho Chehab586b0ca2005-06-28 20:45:21 -0700602
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700603 t->freq = f->frequency;
604 switch_v4l2();
605 if (V4L2_TUNER_RADIO == f->type &&
606 V4L2_TUNER_RADIO != t->mode) {
607 if (set_mode (client, t, f->type, "VIDIOC_S_FREQUENCY")
608 == EINVAL)
609 return 0;
610 }
611 set_freq(client,t->freq);
Mauro Carvalho Chehab586b0ca2005-06-28 20:45:21 -0700612
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700613 break;
614 }
615 case VIDIOC_G_FREQUENCY:
616 {
617 struct v4l2_frequency *f = arg;
Mauro Carvalho Chehab586b0ca2005-06-28 20:45:21 -0700618
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700619 if (check_mode(t, "VIDIOC_G_FREQUENCY") == EINVAL)
620 return 0;
621 switch_v4l2();
622 f->type = t->mode;
623 f->frequency = t->freq;
624 break;
625 }
626 case VIDIOC_G_TUNER:
627 {
628 struct v4l2_tuner *tuner = arg;
Mauro Carvalho Chehab586b0ca2005-06-28 20:45:21 -0700629
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700630 if (check_mode(t, "VIDIOC_G_TUNER") == EINVAL)
631 return 0;
632 switch_v4l2();
633
634 if (V4L2_TUNER_RADIO == t->mode) {
635
636 if (t->has_signal)
637 tuner->signal = t->has_signal(client);
638
639 if (t->is_stereo) {
640 if (t->is_stereo(client)) {
641 tuner->rxsubchans =
642 V4L2_TUNER_SUB_STEREO |
643 V4L2_TUNER_SUB_MONO;
644 } else {
645 tuner->rxsubchans =
646 V4L2_TUNER_SUB_MONO;
647 }
648 }
649
650 tuner->capability |=
651 V4L2_TUNER_CAP_LOW | V4L2_TUNER_CAP_STEREO;
652
653 tuner->audmode = t->audmode;
654
655 tuner->rangelow = radio_range[0] * 16000;
656 tuner->rangehigh = radio_range[1] * 16000;
657 } else {
658 tuner->rangelow = tv_range[0] * 16;
659 tuner->rangehigh = tv_range[1] * 16;
660 }
661 break;
662 }
663 case VIDIOC_S_TUNER:
664 {
665 struct v4l2_tuner *tuner = arg;
666
667 if (check_mode(t, "VIDIOC_S_TUNER") == EINVAL)
668 return 0;
669
670 switch_v4l2();
671
672 if (V4L2_TUNER_RADIO == t->mode) {
673 t->audmode = tuner->audmode;
674 set_radio_freq(client, t->freq);
675 }
676 break;
677 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 default:
Mauro Carvalho Chehabc5287ba2005-07-15 03:56:28 -0700679 tuner_dbg("Unimplemented IOCTL 0x%08x(dir=%d,tp=0x%02x,nr=%d,sz=%d)\n",
680 cmd, _IOC_DIR(cmd), _IOC_TYPE(cmd),
681 _IOC_NR(cmd), _IOC_SIZE(cmd));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 break;
683 }
684
685 return 0;
686}
687
Pavel Machek829ca9a2005-09-03 15:56:56 -0700688static int tuner_suspend(struct device *dev, pm_message_t state, u32 level)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689{
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700690 struct i2c_client *c = container_of (dev, struct i2c_client, dev);
691 struct tuner *t = i2c_get_clientdata (c);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700693 tuner_dbg ("suspend\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 /* FIXME: power down ??? */
695 return 0;
696}
697
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700698static int tuner_resume(struct device *dev, u32 level)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699{
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700700 struct i2c_client *c = container_of (dev, struct i2c_client, dev);
701 struct tuner *t = i2c_get_clientdata (c);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700703 tuner_dbg ("resume\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 if (t->freq)
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700705 set_freq(c, t->freq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 return 0;
707}
708
709/* ----------------------------------------------------------------------- */
710
711static struct i2c_driver driver = {
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700712 .owner = THIS_MODULE,
713 .name = "tuner",
714 .id = I2C_DRIVERID_TUNER,
715 .flags = I2C_DF_NOTIFY,
716 .attach_adapter = tuner_probe,
717 .detach_client = tuner_detach,
718 .command = tuner_command,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 .driver = {
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700720 .suspend = tuner_suspend,
721 .resume = tuner_resume,
722 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723};
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700724static struct i2c_client client_template = {
Jean Delvarefae91e72005-08-15 19:57:04 +0200725 .name = "(tuner unset)",
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700726 .flags = I2C_CLIENT_ALLOW_USE,
727 .driver = &driver,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728};
729
730static int __init tuner_init_module(void)
731{
732 return i2c_add_driver(&driver);
733}
734
735static void __exit tuner_cleanup_module(void)
736{
737 i2c_del_driver(&driver);
738}
739
740module_init(tuner_init_module);
741module_exit(tuner_cleanup_module);
742
743/*
744 * Overrides for Emacs so that we follow Linus's tabbing style.
745 * ---------------------------------------------------------------------------
746 * Local variables:
747 * c-basic-offset: 8
748 * End:
749 */