blob: 4b562b386fcf6d044882077359763bb2e84ac241 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002
3 bttv-i2c.c -- all the i2c code is here
4
5 bttv - Bt848 frame grabber driver
6
7 Copyright (C) 1996,97,98 Ralph Metzler (rjkm@thp.uni-koeln.de)
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08008 & Marcus Metzler (mocm@thp.uni-koeln.de)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009 (c) 1999-2003 Gerd Knorr <kraxel@bytesex.org>
10
11 This program is free software; you can redistribute it and/or modify
12 it under the terms of the GNU General Public License as published by
13 the Free Software Foundation; either version 2 of the License, or
14 (at your option) any later version.
15
16 This program is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 GNU General Public License for more details.
20
21 You should have received a copy of the GNU General Public License
22 along with this program; if not, write to the Free Software
23 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24
25*/
26
27#include <linux/module.h>
28#include <linux/moduleparam.h>
29#include <linux/init.h>
30#include <linux/delay.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070031
32#include "bttvp.h"
Michael Krufky5e453dc2006-01-09 15:32:31 -020033#include <media/v4l2-common.h>
Mauro Carvalho Chehabb5b8ab82006-01-09 15:25:20 -020034#include <linux/jiffies.h>
35#include <asm/io.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070036
37static struct i2c_algo_bit_data bttv_i2c_algo_bit_template;
38static struct i2c_adapter bttv_i2c_adap_sw_template;
39static struct i2c_adapter bttv_i2c_adap_hw_template;
40static struct i2c_client bttv_i2c_client_template;
41
42static int attach_inform(struct i2c_client *client);
43
Mauro Carvalho Chehaba5ed4252006-01-13 14:10:19 -020044static int i2c_debug;
45static int i2c_hw;
46static int i2c_scan;
Linus Torvalds1da177e2005-04-16 15:20:36 -070047module_param(i2c_debug, int, 0644);
48module_param(i2c_hw, int, 0444);
49module_param(i2c_scan, int, 0444);
50MODULE_PARM_DESC(i2c_scan,"scan i2c bus at insmod time");
51
52/* ----------------------------------------------------------------------- */
53/* I2C functions - bitbanging adapter (software i2c) */
54
55static void bttv_bit_setscl(void *data, int state)
56{
57 struct bttv *btv = (struct bttv*)data;
58
59 if (state)
60 btv->i2c_state |= 0x02;
61 else
62 btv->i2c_state &= ~0x02;
63 btwrite(btv->i2c_state, BT848_I2C);
64 btread(BT848_I2C);
65}
66
67static void bttv_bit_setsda(void *data, int state)
68{
69 struct bttv *btv = (struct bttv*)data;
70
71 if (state)
72 btv->i2c_state |= 0x01;
73 else
74 btv->i2c_state &= ~0x01;
75 btwrite(btv->i2c_state, BT848_I2C);
76 btread(BT848_I2C);
77}
78
79static int bttv_bit_getscl(void *data)
80{
81 struct bttv *btv = (struct bttv*)data;
82 int state;
83
84 state = btread(BT848_I2C) & 0x02 ? 1 : 0;
85 return state;
86}
87
88static int bttv_bit_getsda(void *data)
89{
90 struct bttv *btv = (struct bttv*)data;
91 int state;
92
93 state = btread(BT848_I2C) & 0x01;
94 return state;
95}
96
97static struct i2c_algo_bit_data bttv_i2c_algo_bit_template = {
98 .setsda = bttv_bit_setsda,
99 .setscl = bttv_bit_setscl,
100 .getsda = bttv_bit_getsda,
101 .getscl = bttv_bit_getscl,
102 .udelay = 16,
103 .mdelay = 10,
104 .timeout = 200,
105};
106
107static struct i2c_adapter bttv_i2c_adap_sw_template = {
108 .owner = THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 .class = I2C_CLASS_TV_ANALOG,
Mauro Carvalho Chehabcab462f2006-01-09 15:53:26 -0200110 .name = "bttv",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 .id = I2C_HW_B_BT848,
112 .client_register = attach_inform,
113};
114
115/* ----------------------------------------------------------------------- */
116/* I2C functions - hardware i2c */
117
118static int algo_control(struct i2c_adapter *adapter,
119 unsigned int cmd, unsigned long arg)
120{
121 return 0;
122}
123
124static u32 functionality(struct i2c_adapter *adap)
125{
126 return I2C_FUNC_SMBUS_EMUL;
127}
128
129static int
130bttv_i2c_wait_done(struct bttv *btv)
131{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 int rc = 0;
133
Manu Abrahamfc9d53a2005-05-05 16:16:01 -0700134 /* timeout */
135 if (wait_event_interruptible_timeout(btv->i2c_queue,
136 btv->i2c_done, msecs_to_jiffies(85)) == -ERESTARTSYS)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137
Manu Abrahamfc9d53a2005-05-05 16:16:01 -0700138 rc = -EIO;
139
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 if (btv->i2c_done & BT848_INT_RACK)
141 rc = 1;
142 btv->i2c_done = 0;
143 return rc;
144}
145
146#define I2C_HW (BT878_I2C_MODE | BT848_I2C_SYNC |\
147 BT848_I2C_SCL | BT848_I2C_SDA)
148
149static int
150bttv_i2c_sendbytes(struct bttv *btv, const struct i2c_msg *msg, int last)
151{
152 u32 xmit;
153 int retval,cnt;
154
155 /* sanity checks */
156 if (0 == msg->len)
157 return -EINVAL;
158
159 /* start, address + first byte */
160 xmit = (msg->addr << 25) | (msg->buf[0] << 16) | I2C_HW;
161 if (msg->len > 1 || !last)
162 xmit |= BT878_I2C_NOSTOP;
163 btwrite(xmit, BT848_I2C);
164 retval = bttv_i2c_wait_done(btv);
165 if (retval < 0)
166 goto err;
167 if (retval == 0)
168 goto eio;
169 if (i2c_debug) {
170 printk(" <W %02x %02x", msg->addr << 1, msg->buf[0]);
171 if (!(xmit & BT878_I2C_NOSTOP))
172 printk(" >\n");
173 }
174
175 for (cnt = 1; cnt < msg->len; cnt++ ) {
176 /* following bytes */
177 xmit = (msg->buf[cnt] << 24) | I2C_HW | BT878_I2C_NOSTART;
178 if (cnt < msg->len-1 || !last)
179 xmit |= BT878_I2C_NOSTOP;
180 btwrite(xmit, BT848_I2C);
181 retval = bttv_i2c_wait_done(btv);
182 if (retval < 0)
183 goto err;
184 if (retval == 0)
185 goto eio;
186 if (i2c_debug) {
187 printk(" %02x", msg->buf[cnt]);
188 if (!(xmit & BT878_I2C_NOSTOP))
189 printk(" >\n");
190 }
191 }
192 return msg->len;
193
194 eio:
195 retval = -EIO;
196 err:
197 if (i2c_debug)
198 printk(" ERR: %d\n",retval);
199 return retval;
200}
201
202static int
203bttv_i2c_readbytes(struct bttv *btv, const struct i2c_msg *msg, int last)
204{
205 u32 xmit;
206 u32 cnt;
207 int retval;
208
209 for(cnt = 0; cnt < msg->len; cnt++) {
210 xmit = (msg->addr << 25) | (1 << 24) | I2C_HW;
211 if (cnt < msg->len-1)
212 xmit |= BT848_I2C_W3B;
213 if (cnt < msg->len-1 || !last)
214 xmit |= BT878_I2C_NOSTOP;
215 if (cnt)
216 xmit |= BT878_I2C_NOSTART;
217 btwrite(xmit, BT848_I2C);
218 retval = bttv_i2c_wait_done(btv);
219 if (retval < 0)
220 goto err;
221 if (retval == 0)
222 goto eio;
223 msg->buf[cnt] = ((u32)btread(BT848_I2C) >> 8) & 0xff;
224 if (i2c_debug) {
225 if (!(xmit & BT878_I2C_NOSTART))
226 printk(" <R %02x", (msg->addr << 1) +1);
227 printk(" =%02x", msg->buf[cnt]);
228 if (!(xmit & BT878_I2C_NOSTOP))
229 printk(" >\n");
230 }
231 }
232 return msg->len;
233
234 eio:
235 retval = -EIO;
236 err:
237 if (i2c_debug)
238 printk(" ERR: %d\n",retval);
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800239 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240}
241
242static int bttv_i2c_xfer(struct i2c_adapter *i2c_adap, struct i2c_msg *msgs, int num)
243{
244 struct bttv *btv = i2c_get_adapdata(i2c_adap);
245 int retval = 0;
246 int i;
247
248 if (i2c_debug)
249 printk("bt-i2c:");
250 btwrite(BT848_INT_I2CDONE|BT848_INT_RACK, BT848_INT_STAT);
251 for (i = 0 ; i < num; i++) {
252 if (msgs[i].flags & I2C_M_RD) {
253 /* read */
254 retval = bttv_i2c_readbytes(btv, &msgs[i], i+1 == num);
255 if (retval < 0)
256 goto err;
257 } else {
258 /* write */
259 retval = bttv_i2c_sendbytes(btv, &msgs[i], i+1 == num);
260 if (retval < 0)
261 goto err;
262 }
263 }
264 return num;
265
266 err:
267 return retval;
268}
269
270static struct i2c_algorithm bttv_algo = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 .master_xfer = bttv_i2c_xfer,
272 .algo_control = algo_control,
273 .functionality = functionality,
274};
275
276static struct i2c_adapter bttv_i2c_adap_hw_template = {
Mauro Carvalho Chehabcab462f2006-01-09 15:53:26 -0200277 .owner = THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 .class = I2C_CLASS_TV_ANALOG,
Jean Delvarefae91e72005-08-15 19:57:04 +0200279 .name = "bt878",
Jean Delvarec7a46532005-08-11 23:41:56 +0200280 .id = I2C_HW_B_BT848 /* FIXME */,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 .algo = &bttv_algo,
282 .client_register = attach_inform,
283};
284
285/* ----------------------------------------------------------------------- */
286/* I2C functions - common stuff */
287
288static int attach_inform(struct i2c_client *client)
289{
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800290 struct bttv *btv = i2c_get_adapdata(client->adapter);
Ricardo Cerqueira291d1d72005-11-08 21:38:33 -0800291 int addr=ADDR_UNSET;
Mauro Carvalho Chehabaa8d5e72005-11-08 21:38:16 -0800292
Ricardo Cerqueira291d1d72005-11-08 21:38:33 -0800293
Lubomir Bulej7418f342005-11-08 21:38:34 -0800294 if (ADDR_UNSET != bttv_tvcards[btv->c.type].tuner_addr)
295 addr = bttv_tvcards[btv->c.type].tuner_addr;
Ricardo Cerqueira291d1d72005-11-08 21:38:33 -0800296
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297
Mauro Carvalho Chehabfa9846a2005-07-12 13:58:42 -0700298 if (bttv_debug)
299 printk(KERN_DEBUG "bttv%d: %s i2c attach [addr=0x%x,client=%s]\n",
Laurent Riffard604f28e2005-11-26 20:43:39 +0100300 btv->c.nr, client->driver->driver.name, client->addr,
Jean Delvarefae91e72005-08-15 19:57:04 +0200301 client->name);
Mauro Carvalho Chehabfa9846a2005-07-12 13:58:42 -0700302 if (!client->driver->command)
303 return 0;
304
Hans Verkuil8bf2f8e2006-03-18 21:31:00 -0300305 if (client->driver->id == I2C_DRIVERID_MSP3400)
306 btv->i2c_msp34xx_client = client;
307 if (client->driver->id == I2C_DRIVERID_TVAUDIO)
308 btv->i2c_tvaudio_client = client;
Mauro Carvalho Chehabfa9846a2005-07-12 13:58:42 -0700309 if (btv->tuner_type != UNSET) {
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800310 struct tuner_setup tun_setup;
Mauro Carvalho Chehabfa9846a2005-07-12 13:58:42 -0700311
Ricardo Cerqueira291d1d72005-11-08 21:38:33 -0800312 if ((addr==ADDR_UNSET) ||
313 (addr==client->addr)) {
314
Lubomir Bulej7418f342005-11-08 21:38:34 -0800315 tun_setup.mode_mask = T_ANALOG_TV | T_DIGITAL_TV | T_RADIO;
316 tun_setup.type = btv->tuner_type;
Ricardo Cerqueira291d1d72005-11-08 21:38:33 -0800317 tun_setup.addr = addr;
Lubomir Bulej7418f342005-11-08 21:38:34 -0800318 bttv_call_i2c_clients(btv, TUNER_SET_TYPE_ADDR, &tun_setup);
Mauro Carvalho Chehab85a2eb02005-11-08 21:38:18 -0800319 }
Ricardo Cerqueira291d1d72005-11-08 21:38:33 -0800320
Mauro Carvalho Chehabfa9846a2005-07-12 13:58:42 -0700321 }
322
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800323 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324}
325
326void bttv_call_i2c_clients(struct bttv *btv, unsigned int cmd, void *arg)
327{
328 if (0 != btv->i2c_rc)
329 return;
330 i2c_clients_command(&btv->c.i2c_adap, cmd, arg);
331}
332
333static struct i2c_client bttv_i2c_client_template = {
Jean Delvarefae91e72005-08-15 19:57:04 +0200334 .name = "bttv internal",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335};
336
337
338/* read I2C */
339int bttv_I2CRead(struct bttv *btv, unsigned char addr, char *probe_for)
340{
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800341 unsigned char buffer = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342
343 if (0 != btv->i2c_rc)
344 return -1;
345 if (bttv_verbose && NULL != probe_for)
346 printk(KERN_INFO "bttv%d: i2c: checking for %s @ 0x%02x... ",
347 btv->c.nr,probe_for,addr);
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800348 btv->i2c_client.addr = addr >> 1;
349 if (1 != i2c_master_recv(&btv->i2c_client, &buffer, 1)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 if (NULL != probe_for) {
351 if (bttv_verbose)
352 printk("not found\n");
353 } else
354 printk(KERN_WARNING "bttv%d: i2c read 0x%x: error\n",
355 btv->c.nr,addr);
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800356 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 }
358 if (bttv_verbose && NULL != probe_for)
359 printk("found\n");
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800360 return buffer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361}
362
363/* write I2C */
364int bttv_I2CWrite(struct bttv *btv, unsigned char addr, unsigned char b1,
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800365 unsigned char b2, int both)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366{
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800367 unsigned char buffer[2];
368 int bytes = both ? 2 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369
370 if (0 != btv->i2c_rc)
371 return -1;
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800372 btv->i2c_client.addr = addr >> 1;
373 buffer[0] = b1;
374 buffer[1] = b2;
375 if (bytes != i2c_master_send(&btv->i2c_client, buffer, bytes))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 return -1;
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800377 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378}
379
380/* read EEPROM content */
381void __devinit bttv_readee(struct bttv *btv, unsigned char *eedata, int addr)
382{
Gerd Knorr5daf05f2005-05-25 12:31:26 -0700383 memset(eedata, 0, 256);
384 if (0 != btv->i2c_rc)
385 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 btv->i2c_client.addr = addr >> 1;
387 tveeprom_read(&btv->i2c_client, eedata, 256);
388}
389
390static char *i2c_devs[128] = {
Mauro Carvalho Chehab24a70fd2005-09-09 13:03:39 -0700391 [ 0x1c >> 1 ] = "lgdt330x",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 [ 0x30 >> 1 ] = "IR (hauppauge)",
393 [ 0x80 >> 1 ] = "msp34xx",
394 [ 0x86 >> 1 ] = "tda9887",
395 [ 0xa0 >> 1 ] = "eeprom",
396 [ 0xc0 >> 1 ] = "tuner (analog)",
397 [ 0xc2 >> 1 ] = "tuner (analog)",
398};
399
400static void do_i2c_scan(char *name, struct i2c_client *c)
401{
402 unsigned char buf;
403 int i,rc;
404
405 for (i = 0; i < 128; i++) {
406 c->addr = i;
407 rc = i2c_master_recv(c,&buf,0);
408 if (rc < 0)
409 continue;
410 printk("%s: i2c scan: found device @ 0x%x [%s]\n",
411 name, i << 1, i2c_devs[i] ? i2c_devs[i] : "???");
412 }
413}
414
415/* init + register i2c algo-bit adapter */
416int __devinit init_bttv_i2c(struct bttv *btv)
417{
418 memcpy(&btv->i2c_client, &bttv_i2c_client_template,
419 sizeof(bttv_i2c_client_template));
420
421 if (i2c_hw)
422 btv->use_i2c_hw = 1;
423 if (btv->use_i2c_hw) {
424 /* bt878 */
425 memcpy(&btv->c.i2c_adap, &bttv_i2c_adap_hw_template,
426 sizeof(bttv_i2c_adap_hw_template));
427 } else {
428 /* bt848 */
429 memcpy(&btv->c.i2c_adap, &bttv_i2c_adap_sw_template,
430 sizeof(bttv_i2c_adap_sw_template));
431 memcpy(&btv->i2c_algo, &bttv_i2c_algo_bit_template,
432 sizeof(bttv_i2c_algo_bit_template));
433 btv->i2c_algo.data = btv;
434 btv->c.i2c_adap.algo_data = &btv->i2c_algo;
435 }
436
437 btv->c.i2c_adap.dev.parent = &btv->c.pci->dev;
438 snprintf(btv->c.i2c_adap.name, sizeof(btv->c.i2c_adap.name),
439 "bt%d #%d [%s]", btv->id, btv->c.nr,
440 btv->use_i2c_hw ? "hw" : "sw");
441
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800442 i2c_set_adapdata(&btv->c.i2c_adap, btv);
443 btv->i2c_client.adapter = &btv->c.i2c_adap;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 if (bttv_tvcards[btv->c.type].no_video)
446 btv->c.i2c_adap.class &= ~I2C_CLASS_TV_ANALOG;
447 if (bttv_tvcards[btv->c.type].has_dvb)
448 btv->c.i2c_adap.class |= I2C_CLASS_TV_DIGITAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449
450 if (btv->use_i2c_hw) {
451 btv->i2c_rc = i2c_add_adapter(&btv->c.i2c_adap);
452 } else {
453 bttv_bit_setscl(btv,1);
454 bttv_bit_setsda(btv,1);
455 btv->i2c_rc = i2c_bit_add_bus(&btv->c.i2c_adap);
456 }
457 if (0 == btv->i2c_rc && i2c_scan)
458 do_i2c_scan(btv->c.name,&btv->i2c_client);
459 return btv->i2c_rc;
460}
461
462int __devexit fini_bttv_i2c(struct bttv *btv)
463{
464 if (0 != btv->i2c_rc)
465 return 0;
466
467 if (btv->use_i2c_hw) {
468 return i2c_del_adapter(&btv->c.i2c_adap);
469 } else {
470 return i2c_bit_del_bus(&btv->c.i2c_adap);
471 }
472}
473
474/*
475 * Local variables:
476 * c-basic-offset: 8
477 * End:
478 */