blob: d4786b331467cfb9f24ac30f7dc71b284f8bc56d [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[] = {
Hartmut Hackmannde48eeb2005-11-08 21:37:48 -080031 0x42, 0x43, 0x4a, 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;
Hartmut Hackmann93df3412005-11-08 21:36:31 -0800192 case TUNER_PHILIPS_TD1316:
193 buffer[0] = 0x0b;
194 buffer[1] = 0xdc;
195 buffer[2] = 0x86;
196 buffer[3] = 0xa4;
197 i2c_master_send(c,buffer,4);
198 default_tuner_init(c);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 default:
200 default_tuner_init(c);
201 break;
202 }
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700203
204 if (t->mode_mask == T_UNINITIALIZED)
205 t->mode_mask = new_mode_mask;
206
207 set_freq(c, t->freq);
208 tuner_dbg("%s %s I2C addr 0x%02x with type %d used for 0x%02x\n",
209 c->adapter->name, c->driver->name, c->addr << 1, type,
210 t->mode_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211}
212
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700213/*
214 * This function apply tuner config to tuner specified
215 * by tun_setup structure. I addr is unset, then admin status
216 * and tun addr status is more precise then current status,
217 * it's applied. Otherwise status and type are applied only to
218 * tuner with exactly the same addr.
219*/
Mauro Carvalho Chehab56fc08c2005-06-23 22:05:07 -0700220
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700221static void set_addr(struct i2c_client *c, struct tuner_setup *tun_setup)
Mauro Carvalho Chehab56fc08c2005-06-23 22:05:07 -0700222{
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700223 struct tuner *t = i2c_get_clientdata(c);
224
Mauro Carvalho Chehab793cf9e2005-09-09 13:03:37 -0700225 if ((tun_setup->addr == ADDR_UNSET &&
226 (t->mode_mask & tun_setup->mode_mask)) ||
227 tun_setup->addr == c->addr) {
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700228 set_type(c, tun_setup->type, tun_setup->mode_mask);
Mauro Carvalho Chehab56fc08c2005-06-23 22:05:07 -0700229 }
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700230}
231
232static inline int check_mode(struct tuner *t, char *cmd)
233{
Mauro Carvalho Chehab793cf9e2005-09-09 13:03:37 -0700234 if ((1 << t->mode & t->mode_mask) == 0) {
235 return EINVAL;
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700236 }
Mauro Carvalho Chehab793cf9e2005-09-09 13:03:37 -0700237
238 switch (t->mode) {
239 case V4L2_TUNER_RADIO:
240 tuner_dbg("Cmd %s accepted for radio\n", cmd);
241 break;
242 case V4L2_TUNER_ANALOG_TV:
243 tuner_dbg("Cmd %s accepted for analog TV\n", cmd);
244 break;
245 case V4L2_TUNER_DIGITAL_TV:
246 tuner_dbg("Cmd %s accepted for digital TV\n", cmd);
247 break;
248 }
249 return 0;
Mauro Carvalho Chehab56fc08c2005-06-23 22:05:07 -0700250}
Mauro Carvalho Chehab56fc08c2005-06-23 22:05:07 -0700251
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252static char pal[] = "-";
Bert Wesarg975e0462005-04-16 15:25:43 -0700253module_param_string(pal, pal, sizeof(pal), 0644);
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700254static char secam[] = "-";
255module_param_string(secam, secam, sizeof(secam), 0644);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700257/* get more precise norm info from insmod option */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258static int tuner_fixup_std(struct tuner *t)
259{
260 if ((t->std & V4L2_STD_PAL) == V4L2_STD_PAL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 switch (pal[0]) {
262 case 'b':
263 case 'B':
264 case 'g':
265 case 'G':
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700266 tuner_dbg ("insmod fixup: PAL => PAL-BG\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 t->std = V4L2_STD_PAL_BG;
268 break;
269 case 'i':
270 case 'I':
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700271 tuner_dbg ("insmod fixup: PAL => PAL-I\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 t->std = V4L2_STD_PAL_I;
273 break;
274 case 'd':
275 case 'D':
276 case 'k':
277 case 'K':
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700278 tuner_dbg ("insmod fixup: PAL => PAL-DK\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 t->std = V4L2_STD_PAL_DK;
280 break;
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700281 case 'M':
282 case 'm':
283 tuner_dbg ("insmod fixup: PAL => PAL-M\n");
284 t->std = V4L2_STD_PAL_M;
285 break;
286 case 'N':
287 case 'n':
288 tuner_dbg ("insmod fixup: PAL => PAL-N\n");
289 t->std = V4L2_STD_PAL_N;
290 break;
Mauro Carvalho Chehab21d4df32005-09-09 13:03:59 -0700291 case '-':
292 /* default parameter, do nothing */
293 break;
294 default:
295 tuner_warn ("pal= argument not recognised\n");
296 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 }
298 }
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700299 if ((t->std & V4L2_STD_SECAM) == V4L2_STD_SECAM) {
300 switch (secam[0]) {
301 case 'd':
302 case 'D':
303 case 'k':
304 case 'K':
305 tuner_dbg ("insmod fixup: SECAM => SECAM-DK\n");
306 t->std = V4L2_STD_SECAM_DK;
307 break;
308 case 'l':
309 case 'L':
310 tuner_dbg ("insmod fixup: SECAM => SECAM-L\n");
311 t->std = V4L2_STD_SECAM_L;
312 break;
Mauro Carvalho Chehab21d4df32005-09-09 13:03:59 -0700313 case '-':
314 /* default parameter, do nothing */
315 break;
316 default:
317 tuner_warn ("secam= argument not recognised\n");
318 break;
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700319 }
320 }
321
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 return 0;
323}
324
325/* ---------------------------------------------------------------------- */
326
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700327/* static var Used only in tuner_attach and tuner_probe */
328static unsigned default_mode_mask;
329
330/* During client attach, set_type is called by adapter's attach_inform callback.
331 set_type must then be completed by tuner_attach.
332 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333static int tuner_attach(struct i2c_adapter *adap, int addr, int kind)
334{
335 struct tuner *t;
336
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700337 client_template.adapter = adap;
338 client_template.addr = addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700340 t = kmalloc(sizeof(struct tuner), GFP_KERNEL);
341 if (NULL == t)
342 return -ENOMEM;
343 memset(t, 0, sizeof(struct tuner));
344 memcpy(&t->i2c, &client_template, sizeof(struct i2c_client));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 i2c_set_clientdata(&t->i2c, t);
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700346 t->type = UNSET;
347 t->radio_if2 = 10700 * 1000; /* 10.7MHz - FM radio */
348 t->audmode = V4L2_TUNER_MODE_STEREO;
349 t->mode_mask = T_UNINITIALIZED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350
Mauro Carvalho Chehab56fc08c2005-06-23 22:05:07 -0700351
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700352 tuner_info("chip found @ 0x%x (%s)\n", addr << 1, adap->name);
353
Mauro Carvalho Chehabfd3113e2005-07-31 22:34:43 -0700354 if (show_i2c) {
355 unsigned char buffer[16];
356 int i,rc;
357
358 memset(buffer, 0, sizeof(buffer));
359 rc = i2c_master_recv(&t->i2c, buffer, sizeof(buffer));
360 printk("tuner-%04x I2C RECV = ",addr);
361 for (i=0;i<rc;i++)
362 printk("%02x ",buffer[i]);
363 printk("\n");
364 }
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700365 /* TEA5767 autodetection code - only for addr = 0xc0 */
Mauro Carvalho Chehabc5287ba2005-07-15 03:56:28 -0700366 if (!no_autodetect) {
Mauro Carvalho Chehab13dd38d2005-11-08 21:37:57 -0800367 switch (addr) {
368 case 0x60:
Mauro Carvalho Chehabc5287ba2005-07-15 03:56:28 -0700369 if (tea5767_autodetection(&t->i2c) != EINVAL) {
370 t->type = TUNER_TEA5767;
371 t->mode_mask = T_RADIO;
372 t->mode = T_STANDBY;
373 t->freq = 87.5 * 16; /* Sets freq to FM range */
374 default_mode_mask &= ~T_RADIO;
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700375
Mauro Carvalho Chehabc5287ba2005-07-15 03:56:28 -0700376 i2c_attach_client (&t->i2c);
377 set_type(&t->i2c,t->type, t->mode_mask);
378 return 0;
379 }
Mauro Carvalho Chehab13dd38d2005-11-08 21:37:57 -0800380 case 0x42:
381 case 0x43:
382 case 0x4a:
383 case 0x44:
384 if (tda8290_init(&t->i2c)<0) {
385 kfree(t);
386 return 0;
387 }
388
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700389 }
Mauro Carvalho Chehab13dd38d2005-11-08 21:37:57 -0800390
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700391 }
392
393 /* Initializes only the first adapter found */
394 if (default_mode_mask != T_UNINITIALIZED) {
395 tuner_dbg ("Setting mode_mask to 0x%02x\n", default_mode_mask);
396 t->mode_mask = default_mode_mask;
397 t->freq = 400 * 16; /* Sets freq to VHF High */
398 default_mode_mask = T_UNINITIALIZED;
399 }
400
401 /* Should be just before return */
402 i2c_attach_client (&t->i2c);
403 set_type (&t->i2c,t->type, t->mode_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 return 0;
405}
406
407static int tuner_probe(struct i2c_adapter *adap)
408{
409 if (0 != addr) {
Mauro Carvalho Chehabf5bec392005-06-23 22:05:13 -0700410 normal_i2c[0] = addr;
411 normal_i2c[1] = I2C_CLIENT_END;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700414 default_mode_mask = T_RADIO | T_ANALOG_TV | T_DIGITAL_TV;
Mauro Carvalho Chehab391cd722005-06-23 22:02:43 -0700415
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 if (adap->class & I2C_CLASS_TV_ANALOG)
417 return i2c_probe(adap, &addr_data, tuner_attach);
418 return 0;
419}
420
421static int tuner_detach(struct i2c_client *client)
422{
423 struct tuner *t = i2c_get_clientdata(client);
Mauro Carvalho Chehab391cd722005-06-23 22:02:43 -0700424 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700426 err = i2c_detach_client(&t->i2c);
Mauro Carvalho Chehab391cd722005-06-23 22:02:43 -0700427 if (err) {
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700428 tuner_warn
429 ("Client deregistration failed, client not detached.\n");
Mauro Carvalho Chehab391cd722005-06-23 22:02:43 -0700430 return err;
431 }
432
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 kfree(t);
434 return 0;
435}
436
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700437/*
438 * Switch tuner to other mode. If tuner support both tv and radio,
439 * set another frequency to some value (This is needed for some pal
440 * tuners to avoid locking). Otherwise, just put second tuner in
441 * standby mode.
442 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700444static inline int set_mode(struct i2c_client *client, struct tuner *t, int mode, char *cmd)
445{
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800446 if (mode == t->mode)
447 return 0;
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700448
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800449 t->mode = mode;
Mauro Carvalho Chehab793cf9e2005-09-09 13:03:37 -0700450
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800451 if (check_mode(t, cmd) == EINVAL) {
452 t->mode = T_STANDBY;
453 if (t->standby)
454 t->standby (client);
455 return EINVAL;
456 }
457 return 0;
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700458}
459
460#define switch_v4l2() if (!t->using_v4l2) \
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800461 tuner_dbg("switching to v4l2\n"); \
462 t->using_v4l2 = 1;
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700463
464static inline int check_v4l2(struct tuner *t)
465{
466 if (t->using_v4l2) {
467 tuner_dbg ("ignore v4l1 call\n");
468 return EINVAL;
469 }
470 return 0;
471}
472
473static int tuner_command(struct i2c_client *client, unsigned int cmd, void *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474{
475 struct tuner *t = i2c_get_clientdata(client);
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700476 unsigned int *iarg = (int *)arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700478 switch (cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 /* --- configuration --- */
Mauro Carvalho Chehab56fc08c2005-06-23 22:05:07 -0700480 case TUNER_SET_TYPE_ADDR:
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700481 tuner_dbg ("Calling set_type_addr for type=%d, addr=0x%02x, mode=0x%02x\n",
482 ((struct tuner_setup *)arg)->type,
483 ((struct tuner_setup *)arg)->addr,
484 ((struct tuner_setup *)arg)->mode_mask);
485
486 set_addr(client, (struct tuner_setup *)arg);
Mauro Carvalho Chehab391cd722005-06-23 22:02:43 -0700487 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 case AUDC_SET_RADIO:
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700489 set_mode(client,t,V4L2_TUNER_RADIO, "AUDC_SET_RADIO");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 break;
Mauro Carvalho Chehab793cf9e2005-09-09 13:03:37 -0700491 case TUNER_SET_STANDBY:
492 {
493 if (check_mode(t, "TUNER_SET_STANDBY") == EINVAL)
494 return 0;
495 if (t->standby)
496 t->standby (client);
497 break;
498 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 case AUDC_CONFIG_PINNACLE:
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700500 if (check_mode(t, "AUDC_CONFIG_PINNACLE") == EINVAL)
501 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 switch (*iarg) {
503 case 2:
504 tuner_dbg("pinnacle pal\n");
505 t->radio_if2 = 33300 * 1000;
506 break;
507 case 3:
508 tuner_dbg("pinnacle ntsc\n");
509 t->radio_if2 = 41300 * 1000;
510 break;
511 }
Mauro Carvalho Chehab586b0ca2005-06-28 20:45:21 -0700512 break;
Mauro Carvalho Chehabfd3113e2005-07-31 22:34:43 -0700513 case VIDIOCSAUDIO:
514 if (check_mode(t, "VIDIOCSAUDIO") == EINVAL)
515 return 0;
516 if (check_v4l2(t) == EINVAL)
517 return 0;
518
519 /* Should be implemented, since bttv calls it */
520 tuner_dbg("VIDIOCSAUDIO not implemented.\n");
521
522 break;
523 case MSP_SET_MATRIX:
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700524 case TDA9887_SET_CONFIG:
525 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 /* --- v4l ioctls --- */
527 /* take care: bttv does userspace copying, we'll get a
528 kernel pointer here... */
529 case VIDIOCSCHAN:
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700530 {
531 static const v4l2_std_id map[] = {
532 [VIDEO_MODE_PAL] = V4L2_STD_PAL,
533 [VIDEO_MODE_NTSC] = V4L2_STD_NTSC_M,
534 [VIDEO_MODE_SECAM] = V4L2_STD_SECAM,
535 [4 /* bttv */ ] = V4L2_STD_PAL_M,
536 [5 /* bttv */ ] = V4L2_STD_PAL_N,
537 [6 /* bttv */ ] = V4L2_STD_NTSC_M_JP,
538 };
539 struct video_channel *vc = arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700541 if (check_v4l2(t) == EINVAL)
542 return 0;
Mauro Carvalho Chehab56fc08c2005-06-23 22:05:07 -0700543
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700544 if (set_mode(client,t,V4L2_TUNER_ANALOG_TV, "VIDIOCSCHAN")==EINVAL)
545 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700547 if (vc->norm < ARRAY_SIZE(map))
548 t->std = map[vc->norm];
549 tuner_fixup_std(t);
550 if (t->freq)
551 set_tv_freq(client, t->freq);
552 return 0;
Mauro Carvalho Chehab56fc08c2005-06-23 22:05:07 -0700553 }
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700554 case VIDIOCSFREQ:
555 {
556 unsigned long *v = arg;
Mauro Carvalho Chehab56fc08c2005-06-23 22:05:07 -0700557
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700558 if (check_mode(t, "VIDIOCSFREQ") == EINVAL)
559 return 0;
560 if (check_v4l2(t) == EINVAL)
561 return 0;
562
563 set_freq(client, *v);
564 return 0;
565 }
566 case VIDIOCGTUNER:
567 {
568 struct video_tuner *vt = arg;
569
570 if (check_mode(t, "VIDIOCGTUNER") == EINVAL)
571 return 0;
572 if (check_v4l2(t) == EINVAL)
573 return 0;
574
575 if (V4L2_TUNER_RADIO == t->mode) {
576 if (t->has_signal)
577 vt->signal = t->has_signal(client);
578 if (t->is_stereo) {
579 if (t->is_stereo(client))
580 vt->flags |=
581 VIDEO_TUNER_STEREO_ON;
582 else
583 vt->flags &=
584 ~VIDEO_TUNER_STEREO_ON;
585 }
586 vt->flags |= VIDEO_TUNER_LOW; /* Allow freqs at 62.5 Hz */
587
588 vt->rangelow = radio_range[0] * 16000;
589 vt->rangehigh = radio_range[1] * 16000;
590
591 } else {
592 vt->rangelow = tv_range[0] * 16;
593 vt->rangehigh = tv_range[1] * 16;
594 }
595
596 return 0;
597 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 case VIDIOCGAUDIO:
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700599 {
600 struct video_audio *va = arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700602 if (check_mode(t, "VIDIOCGAUDIO") == EINVAL)
603 return 0;
604 if (check_v4l2(t) == EINVAL)
605 return 0;
606
607 if (V4L2_TUNER_RADIO == t->mode && t->is_stereo)
608 va->mode = t->is_stereo(client)
609 ? VIDEO_SOUND_STEREO : VIDEO_SOUND_MONO;
610 return 0;
611 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612
613 case VIDIOC_S_STD:
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700614 {
615 v4l2_std_id *id = arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700617 if (set_mode (client, t, V4L2_TUNER_ANALOG_TV, "VIDIOC_S_STD")
618 == EINVAL)
619 return 0;
Mauro Carvalho Chehab56fc08c2005-06-23 22:05:07 -0700620
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700621 switch_v4l2();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700623 t->std = *id;
624 tuner_fixup_std(t);
625 if (t->freq)
626 set_freq(client, t->freq);
627 break;
Mauro Carvalho Chehab56fc08c2005-06-23 22:05:07 -0700628 }
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700629 case VIDIOC_S_FREQUENCY:
630 {
631 struct v4l2_frequency *f = arg;
Mauro Carvalho Chehab586b0ca2005-06-28 20:45:21 -0700632
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700633 t->freq = f->frequency;
634 switch_v4l2();
635 if (V4L2_TUNER_RADIO == f->type &&
636 V4L2_TUNER_RADIO != t->mode) {
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800637 if (set_mode (client, t, f->type, "VIDIOC_S_FREQUENCY")
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700638 == EINVAL)
639 return 0;
640 }
641 set_freq(client,t->freq);
Mauro Carvalho Chehab586b0ca2005-06-28 20:45:21 -0700642
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700643 break;
644 }
645 case VIDIOC_G_FREQUENCY:
646 {
647 struct v4l2_frequency *f = arg;
Mauro Carvalho Chehab586b0ca2005-06-28 20:45:21 -0700648
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700649 if (check_mode(t, "VIDIOC_G_FREQUENCY") == EINVAL)
650 return 0;
651 switch_v4l2();
652 f->type = t->mode;
653 f->frequency = t->freq;
654 break;
655 }
656 case VIDIOC_G_TUNER:
657 {
658 struct v4l2_tuner *tuner = arg;
Mauro Carvalho Chehab586b0ca2005-06-28 20:45:21 -0700659
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700660 if (check_mode(t, "VIDIOC_G_TUNER") == EINVAL)
661 return 0;
662 switch_v4l2();
663
664 if (V4L2_TUNER_RADIO == t->mode) {
665
666 if (t->has_signal)
667 tuner->signal = t->has_signal(client);
668
669 if (t->is_stereo) {
670 if (t->is_stereo(client)) {
671 tuner->rxsubchans =
672 V4L2_TUNER_SUB_STEREO |
673 V4L2_TUNER_SUB_MONO;
674 } else {
675 tuner->rxsubchans =
676 V4L2_TUNER_SUB_MONO;
677 }
678 }
679
680 tuner->capability |=
681 V4L2_TUNER_CAP_LOW | V4L2_TUNER_CAP_STEREO;
682
683 tuner->audmode = t->audmode;
684
685 tuner->rangelow = radio_range[0] * 16000;
686 tuner->rangehigh = radio_range[1] * 16000;
687 } else {
688 tuner->rangelow = tv_range[0] * 16;
689 tuner->rangehigh = tv_range[1] * 16;
690 }
691 break;
692 }
693 case VIDIOC_S_TUNER:
694 {
695 struct v4l2_tuner *tuner = arg;
696
697 if (check_mode(t, "VIDIOC_S_TUNER") == EINVAL)
698 return 0;
699
700 switch_v4l2();
701
702 if (V4L2_TUNER_RADIO == t->mode) {
703 t->audmode = tuner->audmode;
704 set_radio_freq(client, t->freq);
705 }
706 break;
707 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 default:
Mauro Carvalho Chehabfd1c3882005-11-08 21:36:34 -0800709 tuner_dbg("Unimplemented IOCTL 0x%08x(dir=%d,tp='%c',nr=%d,sz=%d)\n",
Mauro Carvalho Chehabc5287ba2005-07-15 03:56:28 -0700710 cmd, _IOC_DIR(cmd), _IOC_TYPE(cmd),
711 _IOC_NR(cmd), _IOC_SIZE(cmd));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712 break;
713 }
714
715 return 0;
716}
717
Russell King9480e302005-10-28 09:52:56 -0700718static int tuner_suspend(struct device *dev, pm_message_t state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719{
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700720 struct i2c_client *c = container_of (dev, struct i2c_client, dev);
721 struct tuner *t = i2c_get_clientdata (c);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700723 tuner_dbg ("suspend\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724 /* FIXME: power down ??? */
725 return 0;
726}
727
Russell King9480e302005-10-28 09:52:56 -0700728static int tuner_resume(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729{
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700730 struct i2c_client *c = container_of (dev, struct i2c_client, dev);
731 struct tuner *t = i2c_get_clientdata (c);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700733 tuner_dbg ("resume\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 if (t->freq)
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700735 set_freq(c, t->freq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736 return 0;
737}
738
739/* ----------------------------------------------------------------------- */
740
741static struct i2c_driver driver = {
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700742 .owner = THIS_MODULE,
743 .name = "tuner",
744 .id = I2C_DRIVERID_TUNER,
745 .flags = I2C_DF_NOTIFY,
746 .attach_adapter = tuner_probe,
747 .detach_client = tuner_detach,
748 .command = tuner_command,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 .driver = {
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700750 .suspend = tuner_suspend,
751 .resume = tuner_resume,
752 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753};
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700754static struct i2c_client client_template = {
Jean Delvarefae91e72005-08-15 19:57:04 +0200755 .name = "(tuner unset)",
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700756 .flags = I2C_CLIENT_ALLOW_USE,
757 .driver = &driver,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758};
759
760static int __init tuner_init_module(void)
761{
762 return i2c_add_driver(&driver);
763}
764
765static void __exit tuner_cleanup_module(void)
766{
767 i2c_del_driver(&driver);
768}
769
770module_init(tuner_init_module);
771module_exit(tuner_cleanup_module);
772
773/*
774 * Overrides for Emacs so that we follow Linus's tabbing style.
775 * ---------------------------------------------------------------------------
776 * Local variables:
777 * c-basic-offset: 8
778 * End:
779 */