blob: fd18a882668ea7eea2e0929d7bfddb4ef686abfc [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>
Michael Krufky5e453dc2006-01-09 15:32:31 -020023#include <media/v4l2-common.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#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[] = {
Hartmut Hackmannde48eeb2005-11-08 21:37:48 -080032 0x42, 0x43, 0x4a, 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;
Mauro Carvalho Chehabc5287ba2005-07-15 03:56:28 -070042static unsigned int no_autodetect = 0;
Mauro Carvalho Chehabfd3113e2005-07-31 22:34:43 -070043static unsigned int show_i2c = 0;
Mauro Carvalho Chehabfd3113e2005-07-31 22:34:43 -070044
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;
Linus Torvalds1da177e2005-04-16 15:20:36 -070047
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -070048static unsigned int tv_range[2] = { 44, 958 };
Linus Torvalds1da177e2005-04-16 15:20:36 -070049static unsigned int radio_range[2] = { 65, 108 };
50
Mauro Carvalho Chehab7e578192006-01-09 15:25:27 -020051static char pal[] = "--";
52static char secam[] = "--";
53static char ntsc[] = "-";
54
55module_param(addr, int, 0444);
56module_param(no_autodetect, int, 0444);
57module_param(show_i2c, int, 0444);
58module_param(tuner_debug, int, 0644);
59
60module_param_string(pal, pal, sizeof(pal), 0644);
61module_param_string(secam, secam, sizeof(secam), 0644);
62module_param_string(ntsc, ntsc, sizeof(ntsc), 0644);
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -070063module_param_array(tv_range, int, NULL, 0644);
Linus Torvalds1da177e2005-04-16 15:20:36 -070064module_param_array(radio_range, int, NULL, 0644);
65
66MODULE_DESCRIPTION("device driver for various TV and TV+FM radio tuners");
67MODULE_AUTHOR("Ralph Metzler, Gerd Knorr, Gunther Mayer");
68MODULE_LICENSE("GPL");
69
Linus Torvalds1da177e2005-04-16 15:20:36 -070070static struct i2c_driver driver;
71static struct i2c_client client_template;
72
73/* ---------------------------------------------------------------------- */
74
Mauro Carvalho Chehab56fc08c2005-06-23 22:05:07 -070075/* Set tuner frequency, freq in Units of 62.5kHz = 1/16MHz */
Linus Torvalds1da177e2005-04-16 15:20:36 -070076static void set_tv_freq(struct i2c_client *c, unsigned int freq)
77{
78 struct tuner *t = i2c_get_clientdata(c);
79
80 if (t->type == UNSET) {
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -070081 tuner_warn ("tuner type not set\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070082 return;
83 }
84 if (NULL == t->tv_freq) {
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -070085 tuner_warn ("Tuner has no way to set tv freq\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070086 return;
87 }
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -070088 if (freq < tv_range[0] * 16 || freq > tv_range[1] * 16) {
89 tuner_dbg ("TV freq (%d.%02d) out of range (%d-%d)\n",
90 freq / 16, freq % 16 * 100 / 16, tv_range[0],
91 tv_range[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 }
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -070093 t->tv_freq(c, freq);
Linus Torvalds1da177e2005-04-16 15:20:36 -070094}
95
96static void set_radio_freq(struct i2c_client *c, unsigned int freq)
97{
98 struct tuner *t = i2c_get_clientdata(c);
99
100 if (t->type == UNSET) {
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700101 tuner_warn ("tuner type not set\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 return;
103 }
104 if (NULL == t->radio_freq) {
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700105 tuner_warn ("tuner has no way to set radio frequency\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 return;
107 }
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700108 if (freq <= radio_range[0] * 16000 || freq >= radio_range[1] * 16000) {
109 tuner_dbg ("radio freq (%d.%02d) out of range (%d-%d)\n",
110 freq / 16000, freq % 16000 * 100 / 16000,
111 radio_range[0], radio_range[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 }
Mauro Carvalho Chehab586b0ca2005-06-28 20:45:21 -0700113
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700114 t->radio_freq(c, freq);
Mauro Carvalho Chehab586b0ca2005-06-28 20:45:21 -0700115 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116}
117
118static void set_freq(struct i2c_client *c, unsigned long freq)
119{
120 struct tuner *t = i2c_get_clientdata(c);
121
122 switch (t->mode) {
123 case V4L2_TUNER_RADIO:
124 tuner_dbg("radio freq set to %lu.%02lu\n",
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700125 freq / 16000, freq % 16000 * 100 / 16000);
126 set_radio_freq(c, freq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 break;
128 case V4L2_TUNER_ANALOG_TV:
129 case V4L2_TUNER_DIGITAL_TV:
130 tuner_dbg("tv freq set to %lu.%02lu\n",
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700131 freq / 16, freq % 16 * 100 / 16);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 set_tv_freq(c, freq);
133 break;
134 }
135 t->freq = freq;
136}
137
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700138static void set_type(struct i2c_client *c, unsigned int type,
139 unsigned int new_mode_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140{
141 struct tuner *t = i2c_get_clientdata(c);
Mauro Carvalho Chehab586b0ca2005-06-28 20:45:21 -0700142 unsigned char buffer[4];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700144 if (type == UNSET || type == TUNER_ABSENT) {
145 tuner_dbg ("tuner 0x%02x: Tuner type absent\n",c->addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 return;
147 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700149 if (type >= tuner_count) {
150 tuner_warn ("tuner 0x%02x: Tuner count greater than %d\n",c->addr,tuner_count);
151 return;
152 }
153
154 /* This code detects calls by card attach_inform */
155 if (NULL == t->i2c.dev.driver) {
156 tuner_dbg ("tuner 0x%02x: called during i2c_client register by adapter's attach_inform\n", c->addr);
157
158 t->type=type;
159 return;
160 }
Mauro Carvalho Chehab56fc08c2005-06-23 22:05:07 -0700161
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 t->type = type;
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700163
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 switch (t->type) {
165 case TUNER_MT2032:
166 microtune_init(c);
167 break;
168 case TUNER_PHILIPS_TDA8290:
169 tda8290_init(c);
170 break;
Mauro Carvalho Chehab586b0ca2005-06-28 20:45:21 -0700171 case TUNER_TEA5767:
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700172 if (tea5767_tuner_init(c) == EINVAL) {
173 t->type = TUNER_ABSENT;
174 t->mode_mask = T_UNINITIALIZED;
175 return;
176 }
177 t->mode_mask = T_RADIO;
Mauro Carvalho Chehab586b0ca2005-06-28 20:45:21 -0700178 break;
179 case TUNER_PHILIPS_FMD1216ME_MK3:
180 buffer[0] = 0x0b;
181 buffer[1] = 0xdc;
182 buffer[2] = 0x9c;
183 buffer[3] = 0x60;
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700184 i2c_master_send(c, buffer, 4);
Mauro Carvalho Chehab586b0ca2005-06-28 20:45:21 -0700185 mdelay(1);
186 buffer[2] = 0x86;
187 buffer[3] = 0x54;
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700188 i2c_master_send(c, buffer, 4);
Mauro Carvalho Chehab586b0ca2005-06-28 20:45:21 -0700189 default_tuner_init(c);
190 break;
Mauro Carvalho Chehab793cf9e2005-09-09 13:03:37 -0700191 case TUNER_LG_TDVS_H062F:
192 /* Set the Auxiliary Byte. */
193 buffer[2] &= ~0x20;
194 buffer[2] |= 0x18;
195 buffer[3] = 0x20;
196 i2c_master_send(c, buffer, 4);
197 default_tuner_init(c);
198 break;
Hartmut Hackmann93df3412005-11-08 21:36:31 -0800199 case TUNER_PHILIPS_TD1316:
200 buffer[0] = 0x0b;
201 buffer[1] = 0xdc;
202 buffer[2] = 0x86;
203 buffer[3] = 0xa4;
204 i2c_master_send(c,buffer,4);
205 default_tuner_init(c);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 default:
207 default_tuner_init(c);
208 break;
209 }
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700210
211 if (t->mode_mask == T_UNINITIALIZED)
212 t->mode_mask = new_mode_mask;
213
214 set_freq(c, t->freq);
215 tuner_dbg("%s %s I2C addr 0x%02x with type %d used for 0x%02x\n",
Laurent Riffard604f28e2005-11-26 20:43:39 +0100216 c->adapter->name, c->driver->driver.name, c->addr << 1, type,
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700217 t->mode_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218}
219
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700220/*
221 * This function apply tuner config to tuner specified
222 * by tun_setup structure. I addr is unset, then admin status
223 * and tun addr status is more precise then current status,
224 * it's applied. Otherwise status and type are applied only to
225 * tuner with exactly the same addr.
226*/
Mauro Carvalho Chehab56fc08c2005-06-23 22:05:07 -0700227
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700228static void set_addr(struct i2c_client *c, struct tuner_setup *tun_setup)
Mauro Carvalho Chehab56fc08c2005-06-23 22:05:07 -0700229{
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700230 struct tuner *t = i2c_get_clientdata(c);
231
Ricardo Cerqueira291d1d72005-11-08 21:38:33 -0800232 if ( t->type == UNSET && ((tun_setup->addr == ADDR_UNSET &&
Mauro Carvalho Chehab793cf9e2005-09-09 13:03:37 -0700233 (t->mode_mask & tun_setup->mode_mask)) ||
Ricardo Cerqueira291d1d72005-11-08 21:38:33 -0800234 tun_setup->addr == c->addr)) {
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700235 set_type(c, tun_setup->type, tun_setup->mode_mask);
Mauro Carvalho Chehab56fc08c2005-06-23 22:05:07 -0700236 }
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700237}
238
239static inline int check_mode(struct tuner *t, char *cmd)
240{
Mauro Carvalho Chehab793cf9e2005-09-09 13:03:37 -0700241 if ((1 << t->mode & t->mode_mask) == 0) {
242 return EINVAL;
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700243 }
Mauro Carvalho Chehab793cf9e2005-09-09 13:03:37 -0700244
245 switch (t->mode) {
246 case V4L2_TUNER_RADIO:
247 tuner_dbg("Cmd %s accepted for radio\n", cmd);
248 break;
249 case V4L2_TUNER_ANALOG_TV:
250 tuner_dbg("Cmd %s accepted for analog TV\n", cmd);
251 break;
252 case V4L2_TUNER_DIGITAL_TV:
253 tuner_dbg("Cmd %s accepted for digital TV\n", cmd);
254 break;
255 }
256 return 0;
Mauro Carvalho Chehab56fc08c2005-06-23 22:05:07 -0700257}
Mauro Carvalho Chehab56fc08c2005-06-23 22:05:07 -0700258
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700259/* get more precise norm info from insmod option */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260static int tuner_fixup_std(struct tuner *t)
261{
262 if ((t->std & V4L2_STD_PAL) == V4L2_STD_PAL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 switch (pal[0]) {
264 case 'b':
265 case 'B':
266 case 'g':
267 case 'G':
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700268 tuner_dbg ("insmod fixup: PAL => PAL-BG\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 t->std = V4L2_STD_PAL_BG;
270 break;
271 case 'i':
272 case 'I':
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700273 tuner_dbg ("insmod fixup: PAL => PAL-I\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 t->std = V4L2_STD_PAL_I;
275 break;
276 case 'd':
277 case 'D':
278 case 'k':
279 case 'K':
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700280 tuner_dbg ("insmod fixup: PAL => PAL-DK\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 t->std = V4L2_STD_PAL_DK;
282 break;
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700283 case 'M':
284 case 'm':
285 tuner_dbg ("insmod fixup: PAL => PAL-M\n");
286 t->std = V4L2_STD_PAL_M;
287 break;
288 case 'N':
289 case 'n':
Mauro Carvalho Chehab7e578192006-01-09 15:25:27 -0200290 if (pal[1] == 'c' || pal[1] == 'C') {
291 tuner_dbg("insmod fixup: PAL => PAL-Nc\n");
292 t->std = V4L2_STD_PAL_Nc;
293 } else {
294 tuner_dbg ("insmod fixup: PAL => PAL-N\n");
295 t->std = V4L2_STD_PAL_N;
296 }
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700297 break;
Mauro Carvalho Chehab21d4df32005-09-09 13:03:59 -0700298 case '-':
299 /* default parameter, do nothing */
300 break;
301 default:
302 tuner_warn ("pal= argument not recognised\n");
303 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 }
305 }
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700306 if ((t->std & V4L2_STD_SECAM) == V4L2_STD_SECAM) {
307 switch (secam[0]) {
Mauro Carvalho Chehab7e578192006-01-09 15:25:27 -0200308 case 'b':
309 case 'B':
310 case 'g':
311 case 'G':
312 case 'h':
313 case 'H':
314 tuner_dbg("insmod fixup: SECAM => SECAM-BGH\n");
315 t->std = V4L2_STD_SECAM_B | V4L2_STD_SECAM_G | V4L2_STD_SECAM_H;
316 break;
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700317 case 'd':
318 case 'D':
319 case 'k':
320 case 'K':
321 tuner_dbg ("insmod fixup: SECAM => SECAM-DK\n");
322 t->std = V4L2_STD_SECAM_DK;
323 break;
324 case 'l':
325 case 'L':
Mauro Carvalho Chehab800d3c62005-11-13 16:07:48 -0800326 if ((secam[1]=='C')||(secam[1]=='c')) {
327 tuner_dbg ("insmod fixup: SECAM => SECAM-L'\n");
328 t->std = V4L2_STD_SECAM_LC;
329 } else {
330 tuner_dbg ("insmod fixup: SECAM => SECAM-L\n");
331 t->std = V4L2_STD_SECAM_L;
332 }
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700333 break;
Mauro Carvalho Chehab21d4df32005-09-09 13:03:59 -0700334 case '-':
335 /* default parameter, do nothing */
336 break;
337 default:
338 tuner_warn ("secam= argument not recognised\n");
339 break;
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700340 }
341 }
342
Mauro Carvalho Chehab7e578192006-01-09 15:25:27 -0200343 if ((t->std & V4L2_STD_NTSC) == V4L2_STD_NTSC) {
344 switch (ntsc[0]) {
345 case 'm':
346 case 'M':
347 tuner_dbg("insmod fixup: NTSC => NTSC-M\n");
348 t->std = V4L2_STD_NTSC_M;
349 break;
350 case 'j':
351 case 'J':
352 tuner_dbg("insmod fixup: NTSC => NTSC_M_JP\n");
353 t->std = V4L2_STD_NTSC_M_JP;
354 break;
355 case '-':
356 /* default parameter, do nothing */
357 break;
358 default:
359 tuner_info("ntsc= argument not recognised\n");
360 break;
361 }
362 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 return 0;
364}
365
Mauro Carvalho Chehab7e578192006-01-09 15:25:27 -0200366static void tuner_status(struct i2c_client *client)
367{
368 struct tuner *t = i2c_get_clientdata(client);
369 unsigned long freq, freq_fraction;
370 const char *p;
371
372 switch (t->mode) {
373 case V4L2_TUNER_RADIO: p = "radio"; break;
374 case V4L2_TUNER_ANALOG_TV: p = "analog TV"; break;
375 case V4L2_TUNER_DIGITAL_TV: p = "digital TV"; break;
376 default: p = "undefined"; break;
377 }
378 if (t->mode == V4L2_TUNER_RADIO) {
379 freq = t->freq / 16000;
380 freq_fraction = (t->freq % 16000) * 100 / 16000;
381 } else {
382 freq = t->freq / 16;
383 freq_fraction = (t->freq % 16) * 100 / 16;
384 }
385 tuner_info("Tuner mode: %s\n", p);
386 tuner_info("Frequency: %lu.%02lu MHz\n", freq, freq_fraction);
387 tuner_info("Standard: 0x%08llx\n", t->std);
388 if (t->mode == V4L2_TUNER_RADIO) {
389 if (t->has_signal) {
390 tuner_info("Signal strength: %d\n", t->has_signal(client));
391 }
392 if (t->is_stereo) {
393 tuner_info("Stereo: %s\n", t->is_stereo(client) ? "yes" : "no");
394 }
395 }
396}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397/* ---------------------------------------------------------------------- */
398
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700399/* static var Used only in tuner_attach and tuner_probe */
400static unsigned default_mode_mask;
401
402/* During client attach, set_type is called by adapter's attach_inform callback.
403 set_type must then be completed by tuner_attach.
404 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405static int tuner_attach(struct i2c_adapter *adap, int addr, int kind)
406{
407 struct tuner *t;
408
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700409 client_template.adapter = adap;
410 client_template.addr = addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700412 t = kmalloc(sizeof(struct tuner), GFP_KERNEL);
413 if (NULL == t)
414 return -ENOMEM;
415 memset(t, 0, sizeof(struct tuner));
416 memcpy(&t->i2c, &client_template, sizeof(struct i2c_client));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 i2c_set_clientdata(&t->i2c, t);
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700418 t->type = UNSET;
419 t->radio_if2 = 10700 * 1000; /* 10.7MHz - FM radio */
420 t->audmode = V4L2_TUNER_MODE_STEREO;
421 t->mode_mask = T_UNINITIALIZED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422
Mauro Carvalho Chehabfd3113e2005-07-31 22:34:43 -0700423 if (show_i2c) {
424 unsigned char buffer[16];
425 int i,rc;
426
427 memset(buffer, 0, sizeof(buffer));
428 rc = i2c_master_recv(&t->i2c, buffer, sizeof(buffer));
Mauro Carvalho Chehab67678362005-11-08 21:38:02 -0800429 tuner_info("I2C RECV = ");
Mauro Carvalho Chehabfd3113e2005-07-31 22:34:43 -0700430 for (i=0;i<rc;i++)
431 printk("%02x ",buffer[i]);
432 printk("\n");
433 }
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700434 /* TEA5767 autodetection code - only for addr = 0xc0 */
Mauro Carvalho Chehabc5287ba2005-07-15 03:56:28 -0700435 if (!no_autodetect) {
Mauro Carvalho Chehab13dd38d2005-11-08 21:37:57 -0800436 switch (addr) {
Hartmut Hackmann07345f52005-11-08 21:38:09 -0800437 case 0x42:
438 case 0x43:
439 case 0x4a:
440 case 0x4b:
441 /* If chip is not tda8290, don't register.
442 since it can be tda9887*/
443 if (tda8290_probe(&t->i2c) != 0) {
Nickolay V. Shmyrevb228ede2005-11-08 21:38:11 -0800444 tuner_dbg("chip at addr %x is not a tda8290\n", addr);
Hartmut Hackmann07345f52005-11-08 21:38:09 -0800445 kfree(t);
446 return 0;
447 }
448 break;
Mauro Carvalho Chehab13dd38d2005-11-08 21:37:57 -0800449 case 0x60:
Mauro Carvalho Chehabc5287ba2005-07-15 03:56:28 -0700450 if (tea5767_autodetection(&t->i2c) != EINVAL) {
451 t->type = TUNER_TEA5767;
452 t->mode_mask = T_RADIO;
453 t->mode = T_STANDBY;
454 t->freq = 87.5 * 16; /* Sets freq to FM range */
455 default_mode_mask &= ~T_RADIO;
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700456
Mauro Carvalho Chehab67678362005-11-08 21:38:02 -0800457 goto register_client;
Mauro Carvalho Chehabc5287ba2005-07-15 03:56:28 -0700458 }
Hartmut Hackmann07345f52005-11-08 21:38:09 -0800459 break;
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700460 }
461 }
462
463 /* Initializes only the first adapter found */
464 if (default_mode_mask != T_UNINITIALIZED) {
465 tuner_dbg ("Setting mode_mask to 0x%02x\n", default_mode_mask);
466 t->mode_mask = default_mode_mask;
467 t->freq = 400 * 16; /* Sets freq to VHF High */
468 default_mode_mask = T_UNINITIALIZED;
469 }
470
471 /* Should be just before return */
Mauro Carvalho Chehab67678362005-11-08 21:38:02 -0800472register_client:
473 tuner_info("chip found @ 0x%x (%s)\n", addr << 1, adap->name);
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700474 i2c_attach_client (&t->i2c);
475 set_type (&t->i2c,t->type, t->mode_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 return 0;
477}
478
479static int tuner_probe(struct i2c_adapter *adap)
480{
481 if (0 != addr) {
Mauro Carvalho Chehabf5bec392005-06-23 22:05:13 -0700482 normal_i2c[0] = addr;
483 normal_i2c[1] = I2C_CLIENT_END;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700486 default_mode_mask = T_RADIO | T_ANALOG_TV | T_DIGITAL_TV;
Mauro Carvalho Chehab391cd722005-06-23 22:02:43 -0700487
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 if (adap->class & I2C_CLASS_TV_ANALOG)
489 return i2c_probe(adap, &addr_data, tuner_attach);
490 return 0;
491}
492
493static int tuner_detach(struct i2c_client *client)
494{
495 struct tuner *t = i2c_get_clientdata(client);
Mauro Carvalho Chehab391cd722005-06-23 22:02:43 -0700496 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700498 err = i2c_detach_client(&t->i2c);
Mauro Carvalho Chehab391cd722005-06-23 22:02:43 -0700499 if (err) {
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700500 tuner_warn
501 ("Client deregistration failed, client not detached.\n");
Mauro Carvalho Chehab391cd722005-06-23 22:02:43 -0700502 return err;
503 }
504
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 kfree(t);
506 return 0;
507}
508
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700509/*
510 * Switch tuner to other mode. If tuner support both tv and radio,
511 * set another frequency to some value (This is needed for some pal
512 * tuners to avoid locking). Otherwise, just put second tuner in
513 * standby mode.
514 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700516static inline int set_mode(struct i2c_client *client, struct tuner *t, int mode, char *cmd)
517{
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800518 if (mode == t->mode)
519 return 0;
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700520
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800521 t->mode = mode;
Mauro Carvalho Chehab793cf9e2005-09-09 13:03:37 -0700522
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800523 if (check_mode(t, cmd) == EINVAL) {
524 t->mode = T_STANDBY;
525 if (t->standby)
526 t->standby (client);
527 return EINVAL;
528 }
529 return 0;
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700530}
531
532#define switch_v4l2() if (!t->using_v4l2) \
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800533 tuner_dbg("switching to v4l2\n"); \
534 t->using_v4l2 = 1;
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700535
536static inline int check_v4l2(struct tuner *t)
537{
538 if (t->using_v4l2) {
539 tuner_dbg ("ignore v4l1 call\n");
540 return EINVAL;
541 }
542 return 0;
543}
544
545static int tuner_command(struct i2c_client *client, unsigned int cmd, void *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546{
547 struct tuner *t = i2c_get_clientdata(client);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548
Michael Krufky5e453dc2006-01-09 15:32:31 -0200549 if (tuner_debug>1)
550 v4l_i2c_print_ioctl(&(t->i2c),cmd);
551
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700552 switch (cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 /* --- configuration --- */
Mauro Carvalho Chehab56fc08c2005-06-23 22:05:07 -0700554 case TUNER_SET_TYPE_ADDR:
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700555 tuner_dbg ("Calling set_type_addr for type=%d, addr=0x%02x, mode=0x%02x\n",
556 ((struct tuner_setup *)arg)->type,
557 ((struct tuner_setup *)arg)->addr,
558 ((struct tuner_setup *)arg)->mode_mask);
559
560 set_addr(client, (struct tuner_setup *)arg);
Mauro Carvalho Chehab391cd722005-06-23 22:02:43 -0700561 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 case AUDC_SET_RADIO:
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700563 set_mode(client,t,V4L2_TUNER_RADIO, "AUDC_SET_RADIO");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 break;
Mauro Carvalho Chehab793cf9e2005-09-09 13:03:37 -0700565 case TUNER_SET_STANDBY:
566 {
567 if (check_mode(t, "TUNER_SET_STANDBY") == EINVAL)
568 return 0;
569 if (t->standby)
570 t->standby (client);
571 break;
572 }
Mauro Carvalho Chehabfd3113e2005-07-31 22:34:43 -0700573 case VIDIOCSAUDIO:
574 if (check_mode(t, "VIDIOCSAUDIO") == EINVAL)
575 return 0;
576 if (check_v4l2(t) == EINVAL)
577 return 0;
578
579 /* Should be implemented, since bttv calls it */
580 tuner_dbg("VIDIOCSAUDIO not implemented.\n");
581
582 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 /* --- v4l ioctls --- */
584 /* take care: bttv does userspace copying, we'll get a
585 kernel pointer here... */
586 case VIDIOCSCHAN:
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700587 {
588 static const v4l2_std_id map[] = {
589 [VIDEO_MODE_PAL] = V4L2_STD_PAL,
590 [VIDEO_MODE_NTSC] = V4L2_STD_NTSC_M,
591 [VIDEO_MODE_SECAM] = V4L2_STD_SECAM,
592 [4 /* bttv */ ] = V4L2_STD_PAL_M,
593 [5 /* bttv */ ] = V4L2_STD_PAL_N,
594 [6 /* bttv */ ] = V4L2_STD_NTSC_M_JP,
595 };
596 struct video_channel *vc = arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700598 if (check_v4l2(t) == EINVAL)
599 return 0;
Mauro Carvalho Chehab56fc08c2005-06-23 22:05:07 -0700600
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700601 if (set_mode(client,t,V4L2_TUNER_ANALOG_TV, "VIDIOCSCHAN")==EINVAL)
602 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700604 if (vc->norm < ARRAY_SIZE(map))
605 t->std = map[vc->norm];
606 tuner_fixup_std(t);
607 if (t->freq)
608 set_tv_freq(client, t->freq);
609 return 0;
Mauro Carvalho Chehab56fc08c2005-06-23 22:05:07 -0700610 }
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700611 case VIDIOCSFREQ:
612 {
613 unsigned long *v = arg;
Mauro Carvalho Chehab56fc08c2005-06-23 22:05:07 -0700614
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700615 if (check_mode(t, "VIDIOCSFREQ") == EINVAL)
616 return 0;
617 if (check_v4l2(t) == EINVAL)
618 return 0;
619
620 set_freq(client, *v);
621 return 0;
622 }
623 case VIDIOCGTUNER:
624 {
625 struct video_tuner *vt = arg;
626
627 if (check_mode(t, "VIDIOCGTUNER") == EINVAL)
628 return 0;
629 if (check_v4l2(t) == EINVAL)
630 return 0;
631
632 if (V4L2_TUNER_RADIO == t->mode) {
633 if (t->has_signal)
634 vt->signal = t->has_signal(client);
635 if (t->is_stereo) {
636 if (t->is_stereo(client))
637 vt->flags |=
638 VIDEO_TUNER_STEREO_ON;
639 else
640 vt->flags &=
641 ~VIDEO_TUNER_STEREO_ON;
642 }
643 vt->flags |= VIDEO_TUNER_LOW; /* Allow freqs at 62.5 Hz */
644
645 vt->rangelow = radio_range[0] * 16000;
646 vt->rangehigh = radio_range[1] * 16000;
647
648 } else {
649 vt->rangelow = tv_range[0] * 16;
650 vt->rangehigh = tv_range[1] * 16;
651 }
652
653 return 0;
654 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 case VIDIOCGAUDIO:
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700656 {
657 struct video_audio *va = arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700659 if (check_mode(t, "VIDIOCGAUDIO") == EINVAL)
660 return 0;
661 if (check_v4l2(t) == EINVAL)
662 return 0;
663
664 if (V4L2_TUNER_RADIO == t->mode && t->is_stereo)
665 va->mode = t->is_stereo(client)
666 ? VIDEO_SOUND_STEREO : VIDEO_SOUND_MONO;
667 return 0;
668 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669
670 case VIDIOC_S_STD:
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700671 {
672 v4l2_std_id *id = arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700674 if (set_mode (client, t, V4L2_TUNER_ANALOG_TV, "VIDIOC_S_STD")
675 == EINVAL)
676 return 0;
Mauro Carvalho Chehab56fc08c2005-06-23 22:05:07 -0700677
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700678 switch_v4l2();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700680 t->std = *id;
681 tuner_fixup_std(t);
682 if (t->freq)
683 set_freq(client, t->freq);
684 break;
Mauro Carvalho Chehab56fc08c2005-06-23 22:05:07 -0700685 }
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700686 case VIDIOC_S_FREQUENCY:
687 {
688 struct v4l2_frequency *f = arg;
Mauro Carvalho Chehab586b0ca2005-06-28 20:45:21 -0700689
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700690 t->freq = f->frequency;
691 switch_v4l2();
692 if (V4L2_TUNER_RADIO == f->type &&
693 V4L2_TUNER_RADIO != t->mode) {
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800694 if (set_mode (client, t, f->type, "VIDIOC_S_FREQUENCY")
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700695 == EINVAL)
696 return 0;
697 }
698 set_freq(client,t->freq);
Mauro Carvalho Chehab586b0ca2005-06-28 20:45:21 -0700699
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700700 break;
701 }
702 case VIDIOC_G_FREQUENCY:
703 {
704 struct v4l2_frequency *f = arg;
Mauro Carvalho Chehab586b0ca2005-06-28 20:45:21 -0700705
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700706 if (check_mode(t, "VIDIOC_G_FREQUENCY") == EINVAL)
707 return 0;
708 switch_v4l2();
709 f->type = t->mode;
710 f->frequency = t->freq;
711 break;
712 }
713 case VIDIOC_G_TUNER:
714 {
715 struct v4l2_tuner *tuner = arg;
Mauro Carvalho Chehab586b0ca2005-06-28 20:45:21 -0700716
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700717 if (check_mode(t, "VIDIOC_G_TUNER") == EINVAL)
718 return 0;
719 switch_v4l2();
720
721 if (V4L2_TUNER_RADIO == t->mode) {
722
723 if (t->has_signal)
724 tuner->signal = t->has_signal(client);
725
726 if (t->is_stereo) {
727 if (t->is_stereo(client)) {
728 tuner->rxsubchans =
729 V4L2_TUNER_SUB_STEREO |
730 V4L2_TUNER_SUB_MONO;
731 } else {
732 tuner->rxsubchans =
733 V4L2_TUNER_SUB_MONO;
734 }
735 }
736
737 tuner->capability |=
738 V4L2_TUNER_CAP_LOW | V4L2_TUNER_CAP_STEREO;
739
740 tuner->audmode = t->audmode;
741
742 tuner->rangelow = radio_range[0] * 16000;
743 tuner->rangehigh = radio_range[1] * 16000;
744 } else {
745 tuner->rangelow = tv_range[0] * 16;
746 tuner->rangehigh = tv_range[1] * 16;
747 }
748 break;
749 }
750 case VIDIOC_S_TUNER:
751 {
752 struct v4l2_tuner *tuner = arg;
753
754 if (check_mode(t, "VIDIOC_S_TUNER") == EINVAL)
755 return 0;
756
757 switch_v4l2();
758
759 if (V4L2_TUNER_RADIO == t->mode) {
760 t->audmode = tuner->audmode;
761 set_radio_freq(client, t->freq);
762 }
763 break;
764 }
Hans Verkuilcd43c3f2006-01-09 15:25:15 -0200765 case VIDIOC_LOG_STATUS:
766 tuner_status(client);
767 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 }
769
770 return 0;
771}
772
Russell King9480e302005-10-28 09:52:56 -0700773static int tuner_suspend(struct device *dev, pm_message_t state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774{
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700775 struct i2c_client *c = container_of (dev, struct i2c_client, dev);
776 struct tuner *t = i2c_get_clientdata (c);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700778 tuner_dbg ("suspend\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 /* FIXME: power down ??? */
780 return 0;
781}
782
Russell King9480e302005-10-28 09:52:56 -0700783static int tuner_resume(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784{
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700785 struct i2c_client *c = container_of (dev, struct i2c_client, dev);
786 struct tuner *t = i2c_get_clientdata (c);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700788 tuner_dbg ("resume\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789 if (t->freq)
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700790 set_freq(c, t->freq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791 return 0;
792}
793
794/* ----------------------------------------------------------------------- */
795
796static struct i2c_driver driver = {
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700797 .id = I2C_DRIVERID_TUNER,
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700798 .attach_adapter = tuner_probe,
799 .detach_client = tuner_detach,
800 .command = tuner_command,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801 .driver = {
Laurent Riffard604f28e2005-11-26 20:43:39 +0100802 .name = "tuner",
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700803 .suspend = tuner_suspend,
804 .resume = tuner_resume,
805 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806};
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700807static struct i2c_client client_template = {
Jean Delvarefae91e72005-08-15 19:57:04 +0200808 .name = "(tuner unset)",
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700809 .driver = &driver,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810};
811
812static int __init tuner_init_module(void)
813{
814 return i2c_add_driver(&driver);
815}
816
817static void __exit tuner_cleanup_module(void)
818{
819 i2c_del_driver(&driver);
820}
821
822module_init(tuner_init_module);
823module_exit(tuner_cleanup_module);
824
825/*
826 * Overrides for Emacs so that we follow Linus's tabbing style.
827 * ---------------------------------------------------------------------------
828 * Local variables:
829 * c-basic-offset: 8
830 * End:
831 */