blob: 1b3e0a769518fde727d6b17a0a3280166649d3fa [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>
Manu Abrahamfc9d53a2005-05-05 16:16:01 -070031#include <linux/jiffies.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#include <asm/io.h>
33
34#include "bttvp.h"
35
36static struct i2c_algo_bit_data bttv_i2c_algo_bit_template;
37static struct i2c_adapter bttv_i2c_adap_sw_template;
38static struct i2c_adapter bttv_i2c_adap_hw_template;
39static struct i2c_client bttv_i2c_client_template;
40
41static int attach_inform(struct i2c_client *client);
42
43static int i2c_debug = 0;
44static int i2c_hw = 0;
45static int i2c_scan = 0;
46module_param(i2c_debug, int, 0644);
47module_param(i2c_hw, int, 0444);
48module_param(i2c_scan, int, 0444);
49MODULE_PARM_DESC(i2c_scan,"scan i2c bus at insmod time");
50
51/* ----------------------------------------------------------------------- */
52/* I2C functions - bitbanging adapter (software i2c) */
53
54static void bttv_bit_setscl(void *data, int state)
55{
56 struct bttv *btv = (struct bttv*)data;
57
58 if (state)
59 btv->i2c_state |= 0x02;
60 else
61 btv->i2c_state &= ~0x02;
62 btwrite(btv->i2c_state, BT848_I2C);
63 btread(BT848_I2C);
64}
65
66static void bttv_bit_setsda(void *data, int state)
67{
68 struct bttv *btv = (struct bttv*)data;
69
70 if (state)
71 btv->i2c_state |= 0x01;
72 else
73 btv->i2c_state &= ~0x01;
74 btwrite(btv->i2c_state, BT848_I2C);
75 btread(BT848_I2C);
76}
77
78static int bttv_bit_getscl(void *data)
79{
80 struct bttv *btv = (struct bttv*)data;
81 int state;
82
83 state = btread(BT848_I2C) & 0x02 ? 1 : 0;
84 return state;
85}
86
87static int bttv_bit_getsda(void *data)
88{
89 struct bttv *btv = (struct bttv*)data;
90 int state;
91
92 state = btread(BT848_I2C) & 0x01;
93 return state;
94}
95
96static struct i2c_algo_bit_data bttv_i2c_algo_bit_template = {
97 .setsda = bttv_bit_setsda,
98 .setscl = bttv_bit_setscl,
99 .getsda = bttv_bit_getsda,
100 .getscl = bttv_bit_getscl,
101 .udelay = 16,
102 .mdelay = 10,
103 .timeout = 200,
104};
105
106static struct i2c_adapter bttv_i2c_adap_sw_template = {
107 .owner = THIS_MODULE,
108#ifdef I2C_CLASS_TV_ANALOG
109 .class = I2C_CLASS_TV_ANALOG,
110#endif
Jean Delvarefae91e72005-08-15 19:57:04 +0200111 .name = "bt848",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 .id = I2C_HW_B_BT848,
113 .client_register = attach_inform,
114};
115
116/* ----------------------------------------------------------------------- */
117/* I2C functions - hardware i2c */
118
119static int algo_control(struct i2c_adapter *adapter,
120 unsigned int cmd, unsigned long arg)
121{
122 return 0;
123}
124
125static u32 functionality(struct i2c_adapter *adap)
126{
127 return I2C_FUNC_SMBUS_EMUL;
128}
129
130static int
131bttv_i2c_wait_done(struct bttv *btv)
132{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 int rc = 0;
134
Manu Abrahamfc9d53a2005-05-05 16:16:01 -0700135 /* timeout */
136 if (wait_event_interruptible_timeout(btv->i2c_queue,
137 btv->i2c_done, msecs_to_jiffies(85)) == -ERESTARTSYS)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138
Manu Abrahamfc9d53a2005-05-05 16:16:01 -0700139 rc = -EIO;
140
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 if (btv->i2c_done & BT848_INT_RACK)
142 rc = 1;
143 btv->i2c_done = 0;
144 return rc;
145}
146
147#define I2C_HW (BT878_I2C_MODE | BT848_I2C_SYNC |\
148 BT848_I2C_SCL | BT848_I2C_SDA)
149
150static int
151bttv_i2c_sendbytes(struct bttv *btv, const struct i2c_msg *msg, int last)
152{
153 u32 xmit;
154 int retval,cnt;
155
156 /* sanity checks */
157 if (0 == msg->len)
158 return -EINVAL;
159
160 /* start, address + first byte */
161 xmit = (msg->addr << 25) | (msg->buf[0] << 16) | I2C_HW;
162 if (msg->len > 1 || !last)
163 xmit |= BT878_I2C_NOSTOP;
164 btwrite(xmit, BT848_I2C);
165 retval = bttv_i2c_wait_done(btv);
166 if (retval < 0)
167 goto err;
168 if (retval == 0)
169 goto eio;
170 if (i2c_debug) {
171 printk(" <W %02x %02x", msg->addr << 1, msg->buf[0]);
172 if (!(xmit & BT878_I2C_NOSTOP))
173 printk(" >\n");
174 }
175
176 for (cnt = 1; cnt < msg->len; cnt++ ) {
177 /* following bytes */
178 xmit = (msg->buf[cnt] << 24) | I2C_HW | BT878_I2C_NOSTART;
179 if (cnt < msg->len-1 || !last)
180 xmit |= BT878_I2C_NOSTOP;
181 btwrite(xmit, BT848_I2C);
182 retval = bttv_i2c_wait_done(btv);
183 if (retval < 0)
184 goto err;
185 if (retval == 0)
186 goto eio;
187 if (i2c_debug) {
188 printk(" %02x", msg->buf[cnt]);
189 if (!(xmit & BT878_I2C_NOSTOP))
190 printk(" >\n");
191 }
192 }
193 return msg->len;
194
195 eio:
196 retval = -EIO;
197 err:
198 if (i2c_debug)
199 printk(" ERR: %d\n",retval);
200 return retval;
201}
202
203static int
204bttv_i2c_readbytes(struct bttv *btv, const struct i2c_msg *msg, int last)
205{
206 u32 xmit;
207 u32 cnt;
208 int retval;
209
210 for(cnt = 0; cnt < msg->len; cnt++) {
211 xmit = (msg->addr << 25) | (1 << 24) | I2C_HW;
212 if (cnt < msg->len-1)
213 xmit |= BT848_I2C_W3B;
214 if (cnt < msg->len-1 || !last)
215 xmit |= BT878_I2C_NOSTOP;
216 if (cnt)
217 xmit |= BT878_I2C_NOSTART;
218 btwrite(xmit, BT848_I2C);
219 retval = bttv_i2c_wait_done(btv);
220 if (retval < 0)
221 goto err;
222 if (retval == 0)
223 goto eio;
224 msg->buf[cnt] = ((u32)btread(BT848_I2C) >> 8) & 0xff;
225 if (i2c_debug) {
226 if (!(xmit & BT878_I2C_NOSTART))
227 printk(" <R %02x", (msg->addr << 1) +1);
228 printk(" =%02x", msg->buf[cnt]);
229 if (!(xmit & BT878_I2C_NOSTOP))
230 printk(" >\n");
231 }
232 }
233 return msg->len;
234
235 eio:
236 retval = -EIO;
237 err:
238 if (i2c_debug)
239 printk(" ERR: %d\n",retval);
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800240 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241}
242
243static int bttv_i2c_xfer(struct i2c_adapter *i2c_adap, struct i2c_msg *msgs, int num)
244{
245 struct bttv *btv = i2c_get_adapdata(i2c_adap);
246 int retval = 0;
247 int i;
248
249 if (i2c_debug)
250 printk("bt-i2c:");
251 btwrite(BT848_INT_I2CDONE|BT848_INT_RACK, BT848_INT_STAT);
252 for (i = 0 ; i < num; i++) {
253 if (msgs[i].flags & I2C_M_RD) {
254 /* read */
255 retval = bttv_i2c_readbytes(btv, &msgs[i], i+1 == num);
256 if (retval < 0)
257 goto err;
258 } else {
259 /* write */
260 retval = bttv_i2c_sendbytes(btv, &msgs[i], i+1 == num);
261 if (retval < 0)
262 goto err;
263 }
264 }
265 return num;
266
267 err:
268 return retval;
269}
270
271static struct i2c_algorithm bttv_algo = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 .master_xfer = bttv_i2c_xfer,
273 .algo_control = algo_control,
274 .functionality = functionality,
275};
276
277static struct i2c_adapter bttv_i2c_adap_hw_template = {
278 .owner = THIS_MODULE,
279#ifdef I2C_CLASS_TV_ANALOG
280 .class = I2C_CLASS_TV_ANALOG,
281#endif
Jean Delvarefae91e72005-08-15 19:57:04 +0200282 .name = "bt878",
Jean Delvarec7a46532005-08-11 23:41:56 +0200283 .id = I2C_HW_B_BT848 /* FIXME */,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 .algo = &bttv_algo,
285 .client_register = attach_inform,
286};
287
288/* ----------------------------------------------------------------------- */
289/* I2C functions - common stuff */
290
291static int attach_inform(struct i2c_client *client)
292{
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800293 struct bttv *btv = i2c_get_adapdata(client->adapter);
Ricardo Cerqueira291d1d72005-11-08 21:38:33 -0800294 int addr=ADDR_UNSET;
Mauro Carvalho Chehabaa8d5e72005-11-08 21:38:16 -0800295
Ricardo Cerqueira291d1d72005-11-08 21:38:33 -0800296
297 if (ADDR_UNSET != bttv_tvcards[btv->c.type].tuner_addr)
298 addr = bttv_tvcards[btv->c.type].tuner_addr;
299
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300
Mauro Carvalho Chehabfa9846a2005-07-12 13:58:42 -0700301 if (bttv_debug)
302 printk(KERN_DEBUG "bttv%d: %s i2c attach [addr=0x%x,client=%s]\n",
303 btv->c.nr,client->driver->name,client->addr,
Jean Delvarefae91e72005-08-15 19:57:04 +0200304 client->name);
Mauro Carvalho Chehabfa9846a2005-07-12 13:58:42 -0700305 if (!client->driver->command)
306 return 0;
307
308 if (btv->tuner_type != UNSET) {
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800309 struct tuner_setup tun_setup;
Mauro Carvalho Chehabfa9846a2005-07-12 13:58:42 -0700310
Ricardo Cerqueira291d1d72005-11-08 21:38:33 -0800311 if ((addr==ADDR_UNSET) ||
312 (addr==client->addr)) {
313
314 tun_setup.mode_mask = T_ANALOG_TV | T_DIGITAL_TV | T_RADIO;
315 tun_setup.type = btv->tuner_type;
316 tun_setup.addr = addr;
317 bttv_call_i2c_clients(btv, TUNER_SET_TYPE_ADDR, &tun_setup);
Mauro Carvalho Chehab85a2eb02005-11-08 21:38:18 -0800318 }
Ricardo Cerqueira291d1d72005-11-08 21:38:33 -0800319
Mauro Carvalho Chehabfa9846a2005-07-12 13:58:42 -0700320 }
321
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800322 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323}
324
325void bttv_call_i2c_clients(struct bttv *btv, unsigned int cmd, void *arg)
326{
327 if (0 != btv->i2c_rc)
328 return;
329 i2c_clients_command(&btv->c.i2c_adap, cmd, arg);
330}
331
332static struct i2c_client bttv_i2c_client_template = {
Jean Delvarefae91e72005-08-15 19:57:04 +0200333 .name = "bttv internal",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334};
335
336
337/* read I2C */
338int bttv_I2CRead(struct bttv *btv, unsigned char addr, char *probe_for)
339{
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800340 unsigned char buffer = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341
342 if (0 != btv->i2c_rc)
343 return -1;
344 if (bttv_verbose && NULL != probe_for)
345 printk(KERN_INFO "bttv%d: i2c: checking for %s @ 0x%02x... ",
346 btv->c.nr,probe_for,addr);
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800347 btv->i2c_client.addr = addr >> 1;
348 if (1 != i2c_master_recv(&btv->i2c_client, &buffer, 1)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 if (NULL != probe_for) {
350 if (bttv_verbose)
351 printk("not found\n");
352 } else
353 printk(KERN_WARNING "bttv%d: i2c read 0x%x: error\n",
354 btv->c.nr,addr);
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800355 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 }
357 if (bttv_verbose && NULL != probe_for)
358 printk("found\n");
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800359 return buffer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360}
361
362/* write I2C */
363int bttv_I2CWrite(struct bttv *btv, unsigned char addr, unsigned char b1,
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800364 unsigned char b2, int both)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365{
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800366 unsigned char buffer[2];
367 int bytes = both ? 2 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368
369 if (0 != btv->i2c_rc)
370 return -1;
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800371 btv->i2c_client.addr = addr >> 1;
372 buffer[0] = b1;
373 buffer[1] = b2;
374 if (bytes != i2c_master_send(&btv->i2c_client, buffer, bytes))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 return -1;
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800376 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377}
378
379/* read EEPROM content */
380void __devinit bttv_readee(struct bttv *btv, unsigned char *eedata, int addr)
381{
Gerd Knorr5daf05f2005-05-25 12:31:26 -0700382 memset(eedata, 0, 256);
383 if (0 != btv->i2c_rc)
384 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 btv->i2c_client.addr = addr >> 1;
386 tveeprom_read(&btv->i2c_client, eedata, 256);
387}
388
389static char *i2c_devs[128] = {
Mauro Carvalho Chehab24a70fd2005-09-09 13:03:39 -0700390 [ 0x1c >> 1 ] = "lgdt330x",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 [ 0x30 >> 1 ] = "IR (hauppauge)",
392 [ 0x80 >> 1 ] = "msp34xx",
393 [ 0x86 >> 1 ] = "tda9887",
394 [ 0xa0 >> 1 ] = "eeprom",
395 [ 0xc0 >> 1 ] = "tuner (analog)",
396 [ 0xc2 >> 1 ] = "tuner (analog)",
397};
398
399static void do_i2c_scan(char *name, struct i2c_client *c)
400{
401 unsigned char buf;
402 int i,rc;
403
404 for (i = 0; i < 128; i++) {
405 c->addr = i;
406 rc = i2c_master_recv(c,&buf,0);
407 if (rc < 0)
408 continue;
409 printk("%s: i2c scan: found device @ 0x%x [%s]\n",
410 name, i << 1, i2c_devs[i] ? i2c_devs[i] : "???");
411 }
412}
413
414/* init + register i2c algo-bit adapter */
415int __devinit init_bttv_i2c(struct bttv *btv)
416{
417 memcpy(&btv->i2c_client, &bttv_i2c_client_template,
418 sizeof(bttv_i2c_client_template));
419
420 if (i2c_hw)
421 btv->use_i2c_hw = 1;
422 if (btv->use_i2c_hw) {
423 /* bt878 */
424 memcpy(&btv->c.i2c_adap, &bttv_i2c_adap_hw_template,
425 sizeof(bttv_i2c_adap_hw_template));
426 } else {
427 /* bt848 */
428 memcpy(&btv->c.i2c_adap, &bttv_i2c_adap_sw_template,
429 sizeof(bttv_i2c_adap_sw_template));
430 memcpy(&btv->i2c_algo, &bttv_i2c_algo_bit_template,
431 sizeof(bttv_i2c_algo_bit_template));
432 btv->i2c_algo.data = btv;
433 btv->c.i2c_adap.algo_data = &btv->i2c_algo;
434 }
435
436 btv->c.i2c_adap.dev.parent = &btv->c.pci->dev;
437 snprintf(btv->c.i2c_adap.name, sizeof(btv->c.i2c_adap.name),
438 "bt%d #%d [%s]", btv->id, btv->c.nr,
439 btv->use_i2c_hw ? "hw" : "sw");
440
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800441 i2c_set_adapdata(&btv->c.i2c_adap, btv);
442 btv->i2c_client.adapter = &btv->c.i2c_adap;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443
444#ifdef I2C_CLASS_TV_ANALOG
445 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;
449#endif
450
451 if (btv->use_i2c_hw) {
452 btv->i2c_rc = i2c_add_adapter(&btv->c.i2c_adap);
453 } else {
454 bttv_bit_setscl(btv,1);
455 bttv_bit_setsda(btv,1);
456 btv->i2c_rc = i2c_bit_add_bus(&btv->c.i2c_adap);
457 }
458 if (0 == btv->i2c_rc && i2c_scan)
459 do_i2c_scan(btv->c.name,&btv->i2c_client);
460 return btv->i2c_rc;
461}
462
463int __devexit fini_bttv_i2c(struct bttv *btv)
464{
465 if (0 != btv->i2c_rc)
466 return 0;
467
468 if (btv->use_i2c_hw) {
469 return i2c_del_adapter(&btv->c.i2c_adap);
470 } else {
471 return i2c_bit_del_bus(&btv->c.i2c_adap);
472 }
473}
474
475/*
476 * Local variables:
477 * c-basic-offset: 8
478 * End:
479 */