blob: 03ee170181f2f89ed5552491f5ed874897683c87 [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
Mauro Carvalho Chehab141276b2006-09-06 19:04:28 -030011 (c) 2005 Mauro Carvalho Chehab <mchehab@infradead.org>
12 - Multituner support and i2c address binding
13
Linus Torvalds1da177e2005-04-16 15:20:36 -070014 This program is free software; you can redistribute it and/or modify
15 it under the terms of the GNU General Public License as published by
16 the Free Software Foundation; either version 2 of the License, or
17 (at your option) any later version.
18
19 This program is distributed in the hope that it will be useful,
20 but WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 GNU General Public License for more details.
23
24 You should have received a copy of the GNU General Public License
25 along with this program; if not, write to the Free Software
26 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
27
28*/
29
30#include <linux/module.h>
31#include <linux/moduleparam.h>
32#include <linux/init.h>
33#include <linux/delay.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070034
35#include "bttvp.h"
Michael Krufky5e453dc2006-01-09 15:32:31 -020036#include <media/v4l2-common.h>
Mauro Carvalho Chehabb5b8ab82006-01-09 15:25:20 -020037#include <linux/jiffies.h>
38#include <asm/io.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039
40static struct i2c_algo_bit_data bttv_i2c_algo_bit_template;
41static struct i2c_adapter bttv_i2c_adap_sw_template;
42static struct i2c_adapter bttv_i2c_adap_hw_template;
43static struct i2c_client bttv_i2c_client_template;
44
45static int attach_inform(struct i2c_client *client);
46
Mauro Carvalho Chehaba5ed4252006-01-13 14:10:19 -020047static int i2c_debug;
48static int i2c_hw;
49static int i2c_scan;
Linus Torvalds1da177e2005-04-16 15:20:36 -070050module_param(i2c_debug, int, 0644);
Mauro Carvalho Chehab141276b2006-09-06 19:04:28 -030051MODULE_PARM_DESC(i2c_hw,"configure i2c debug level");
Linus Torvalds1da177e2005-04-16 15:20:36 -070052module_param(i2c_hw, int, 0444);
Mauro Carvalho Chehab141276b2006-09-06 19:04:28 -030053MODULE_PARM_DESC(i2c_hw,"force use of hardware i2c support, "
54 "instead of software bitbang");
Linus Torvalds1da177e2005-04-16 15:20:36 -070055module_param(i2c_scan, int, 0444);
56MODULE_PARM_DESC(i2c_scan,"scan i2c bus at insmod time");
57
Mauro Carvalho Chehab141276b2006-09-06 19:04:28 -030058static unsigned int i2c_udelay = 5;
59module_param(i2c_udelay, int, 0444);
60MODULE_PARM_DESC(i2c_udelay,"soft i2c delay at insmod time, in usecs "
61 "(should be 5 or higher). Lower value means higher bus speed.");
62
Linus Torvalds1da177e2005-04-16 15:20:36 -070063/* ----------------------------------------------------------------------- */
64/* I2C functions - bitbanging adapter (software i2c) */
65
66static void bttv_bit_setscl(void *data, int state)
67{
68 struct bttv *btv = (struct bttv*)data;
69
70 if (state)
71 btv->i2c_state |= 0x02;
72 else
73 btv->i2c_state &= ~0x02;
74 btwrite(btv->i2c_state, BT848_I2C);
75 btread(BT848_I2C);
76}
77
78static void bttv_bit_setsda(void *data, int state)
79{
80 struct bttv *btv = (struct bttv*)data;
81
82 if (state)
83 btv->i2c_state |= 0x01;
84 else
85 btv->i2c_state &= ~0x01;
86 btwrite(btv->i2c_state, BT848_I2C);
87 btread(BT848_I2C);
88}
89
90static int bttv_bit_getscl(void *data)
91{
92 struct bttv *btv = (struct bttv*)data;
93 int state;
94
95 state = btread(BT848_I2C) & 0x02 ? 1 : 0;
96 return state;
97}
98
99static int bttv_bit_getsda(void *data)
100{
101 struct bttv *btv = (struct bttv*)data;
102 int state;
103
104 state = btread(BT848_I2C) & 0x01;
105 return state;
106}
107
108static struct i2c_algo_bit_data bttv_i2c_algo_bit_template = {
109 .setsda = bttv_bit_setsda,
110 .setscl = bttv_bit_setscl,
111 .getsda = bttv_bit_getsda,
112 .getscl = bttv_bit_getscl,
113 .udelay = 16,
114 .mdelay = 10,
115 .timeout = 200,
116};
117
118static struct i2c_adapter bttv_i2c_adap_sw_template = {
119 .owner = THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 .class = I2C_CLASS_TV_ANALOG,
Mauro Carvalho Chehabcab462f2006-01-09 15:53:26 -0200121 .name = "bttv",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 .id = I2C_HW_B_BT848,
123 .client_register = attach_inform,
124};
125
126/* ----------------------------------------------------------------------- */
127/* I2C functions - hardware i2c */
128
129static int algo_control(struct i2c_adapter *adapter,
130 unsigned int cmd, unsigned long arg)
131{
132 return 0;
133}
134
135static u32 functionality(struct i2c_adapter *adap)
136{
137 return I2C_FUNC_SMBUS_EMUL;
138}
139
140static int
141bttv_i2c_wait_done(struct bttv *btv)
142{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 int rc = 0;
144
Manu Abrahamfc9d53a2005-05-05 16:16:01 -0700145 /* timeout */
146 if (wait_event_interruptible_timeout(btv->i2c_queue,
147 btv->i2c_done, msecs_to_jiffies(85)) == -ERESTARTSYS)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148
Manu Abrahamfc9d53a2005-05-05 16:16:01 -0700149 rc = -EIO;
150
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 if (btv->i2c_done & BT848_INT_RACK)
152 rc = 1;
153 btv->i2c_done = 0;
154 return rc;
155}
156
157#define I2C_HW (BT878_I2C_MODE | BT848_I2C_SYNC |\
158 BT848_I2C_SCL | BT848_I2C_SDA)
159
160static int
161bttv_i2c_sendbytes(struct bttv *btv, const struct i2c_msg *msg, int last)
162{
163 u32 xmit;
164 int retval,cnt;
165
166 /* sanity checks */
167 if (0 == msg->len)
168 return -EINVAL;
169
170 /* start, address + first byte */
171 xmit = (msg->addr << 25) | (msg->buf[0] << 16) | I2C_HW;
172 if (msg->len > 1 || !last)
173 xmit |= BT878_I2C_NOSTOP;
174 btwrite(xmit, BT848_I2C);
175 retval = bttv_i2c_wait_done(btv);
176 if (retval < 0)
177 goto err;
178 if (retval == 0)
179 goto eio;
180 if (i2c_debug) {
181 printk(" <W %02x %02x", msg->addr << 1, msg->buf[0]);
182 if (!(xmit & BT878_I2C_NOSTOP))
183 printk(" >\n");
184 }
185
186 for (cnt = 1; cnt < msg->len; cnt++ ) {
187 /* following bytes */
188 xmit = (msg->buf[cnt] << 24) | I2C_HW | BT878_I2C_NOSTART;
189 if (cnt < msg->len-1 || !last)
190 xmit |= BT878_I2C_NOSTOP;
191 btwrite(xmit, BT848_I2C);
192 retval = bttv_i2c_wait_done(btv);
193 if (retval < 0)
194 goto err;
195 if (retval == 0)
196 goto eio;
197 if (i2c_debug) {
198 printk(" %02x", msg->buf[cnt]);
199 if (!(xmit & BT878_I2C_NOSTOP))
200 printk(" >\n");
201 }
202 }
203 return msg->len;
204
205 eio:
206 retval = -EIO;
207 err:
208 if (i2c_debug)
209 printk(" ERR: %d\n",retval);
210 return retval;
211}
212
213static int
214bttv_i2c_readbytes(struct bttv *btv, const struct i2c_msg *msg, int last)
215{
216 u32 xmit;
217 u32 cnt;
218 int retval;
219
220 for(cnt = 0; cnt < msg->len; cnt++) {
221 xmit = (msg->addr << 25) | (1 << 24) | I2C_HW;
222 if (cnt < msg->len-1)
223 xmit |= BT848_I2C_W3B;
224 if (cnt < msg->len-1 || !last)
225 xmit |= BT878_I2C_NOSTOP;
226 if (cnt)
227 xmit |= BT878_I2C_NOSTART;
228 btwrite(xmit, BT848_I2C);
229 retval = bttv_i2c_wait_done(btv);
230 if (retval < 0)
231 goto err;
232 if (retval == 0)
233 goto eio;
234 msg->buf[cnt] = ((u32)btread(BT848_I2C) >> 8) & 0xff;
235 if (i2c_debug) {
236 if (!(xmit & BT878_I2C_NOSTART))
237 printk(" <R %02x", (msg->addr << 1) +1);
238 printk(" =%02x", msg->buf[cnt]);
239 if (!(xmit & BT878_I2C_NOSTOP))
240 printk(" >\n");
241 }
242 }
243 return msg->len;
244
245 eio:
246 retval = -EIO;
247 err:
248 if (i2c_debug)
249 printk(" ERR: %d\n",retval);
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800250 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251}
252
253static int bttv_i2c_xfer(struct i2c_adapter *i2c_adap, struct i2c_msg *msgs, int num)
254{
255 struct bttv *btv = i2c_get_adapdata(i2c_adap);
256 int retval = 0;
257 int i;
258
259 if (i2c_debug)
260 printk("bt-i2c:");
261 btwrite(BT848_INT_I2CDONE|BT848_INT_RACK, BT848_INT_STAT);
262 for (i = 0 ; i < num; i++) {
263 if (msgs[i].flags & I2C_M_RD) {
264 /* read */
265 retval = bttv_i2c_readbytes(btv, &msgs[i], i+1 == num);
266 if (retval < 0)
267 goto err;
268 } else {
269 /* write */
270 retval = bttv_i2c_sendbytes(btv, &msgs[i], i+1 == num);
271 if (retval < 0)
272 goto err;
273 }
274 }
275 return num;
276
277 err:
278 return retval;
279}
280
281static struct i2c_algorithm bttv_algo = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 .master_xfer = bttv_i2c_xfer,
283 .algo_control = algo_control,
284 .functionality = functionality,
285};
286
287static struct i2c_adapter bttv_i2c_adap_hw_template = {
Mauro Carvalho Chehabcab462f2006-01-09 15:53:26 -0200288 .owner = THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 .class = I2C_CLASS_TV_ANALOG,
Jean Delvarefae91e72005-08-15 19:57:04 +0200290 .name = "bt878",
Jean Delvarec7a46532005-08-11 23:41:56 +0200291 .id = I2C_HW_B_BT848 /* FIXME */,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 .algo = &bttv_algo,
293 .client_register = attach_inform,
294};
295
296/* ----------------------------------------------------------------------- */
297/* I2C functions - common stuff */
298
299static int attach_inform(struct i2c_client *client)
300{
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800301 struct bttv *btv = i2c_get_adapdata(client->adapter);
Ricardo Cerqueira291d1d72005-11-08 21:38:33 -0800302 int addr=ADDR_UNSET;
Mauro Carvalho Chehabaa8d5e72005-11-08 21:38:16 -0800303
Ricardo Cerqueira291d1d72005-11-08 21:38:33 -0800304
Lubomir Bulej7418f342005-11-08 21:38:34 -0800305 if (ADDR_UNSET != bttv_tvcards[btv->c.type].tuner_addr)
306 addr = bttv_tvcards[btv->c.type].tuner_addr;
Ricardo Cerqueira291d1d72005-11-08 21:38:33 -0800307
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308
Mauro Carvalho Chehabfa9846a2005-07-12 13:58:42 -0700309 if (bttv_debug)
310 printk(KERN_DEBUG "bttv%d: %s i2c attach [addr=0x%x,client=%s]\n",
Laurent Riffard604f28e2005-11-26 20:43:39 +0100311 btv->c.nr, client->driver->driver.name, client->addr,
Jean Delvarefae91e72005-08-15 19:57:04 +0200312 client->name);
Mauro Carvalho Chehabfa9846a2005-07-12 13:58:42 -0700313 if (!client->driver->command)
314 return 0;
315
Hans Verkuil8bf2f8e2006-03-18 21:31:00 -0300316 if (client->driver->id == I2C_DRIVERID_MSP3400)
317 btv->i2c_msp34xx_client = client;
318 if (client->driver->id == I2C_DRIVERID_TVAUDIO)
319 btv->i2c_tvaudio_client = client;
Mauro Carvalho Chehabfa9846a2005-07-12 13:58:42 -0700320 if (btv->tuner_type != UNSET) {
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800321 struct tuner_setup tun_setup;
Mauro Carvalho Chehabfa9846a2005-07-12 13:58:42 -0700322
Ricardo Cerqueira291d1d72005-11-08 21:38:33 -0800323 if ((addr==ADDR_UNSET) ||
324 (addr==client->addr)) {
325
Lubomir Bulej7418f342005-11-08 21:38:34 -0800326 tun_setup.mode_mask = T_ANALOG_TV | T_DIGITAL_TV | T_RADIO;
327 tun_setup.type = btv->tuner_type;
Ricardo Cerqueira291d1d72005-11-08 21:38:33 -0800328 tun_setup.addr = addr;
Lubomir Bulej7418f342005-11-08 21:38:34 -0800329 bttv_call_i2c_clients(btv, TUNER_SET_TYPE_ADDR, &tun_setup);
Mauro Carvalho Chehab85a2eb02005-11-08 21:38:18 -0800330 }
Ricardo Cerqueira291d1d72005-11-08 21:38:33 -0800331
Mauro Carvalho Chehabfa9846a2005-07-12 13:58:42 -0700332 }
333
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800334 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335}
336
337void bttv_call_i2c_clients(struct bttv *btv, unsigned int cmd, void *arg)
338{
339 if (0 != btv->i2c_rc)
340 return;
341 i2c_clients_command(&btv->c.i2c_adap, cmd, arg);
342}
343
344static struct i2c_client bttv_i2c_client_template = {
Jean Delvarefae91e72005-08-15 19:57:04 +0200345 .name = "bttv internal",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346};
347
348
349/* read I2C */
350int bttv_I2CRead(struct bttv *btv, unsigned char addr, char *probe_for)
351{
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800352 unsigned char buffer = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353
354 if (0 != btv->i2c_rc)
355 return -1;
356 if (bttv_verbose && NULL != probe_for)
357 printk(KERN_INFO "bttv%d: i2c: checking for %s @ 0x%02x... ",
358 btv->c.nr,probe_for,addr);
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800359 btv->i2c_client.addr = addr >> 1;
360 if (1 != i2c_master_recv(&btv->i2c_client, &buffer, 1)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 if (NULL != probe_for) {
362 if (bttv_verbose)
363 printk("not found\n");
364 } else
365 printk(KERN_WARNING "bttv%d: i2c read 0x%x: error\n",
366 btv->c.nr,addr);
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800367 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 }
369 if (bttv_verbose && NULL != probe_for)
370 printk("found\n");
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800371 return buffer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372}
373
374/* write I2C */
375int bttv_I2CWrite(struct bttv *btv, unsigned char addr, unsigned char b1,
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800376 unsigned char b2, int both)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377{
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800378 unsigned char buffer[2];
379 int bytes = both ? 2 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380
381 if (0 != btv->i2c_rc)
382 return -1;
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800383 btv->i2c_client.addr = addr >> 1;
384 buffer[0] = b1;
385 buffer[1] = b2;
386 if (bytes != i2c_master_send(&btv->i2c_client, buffer, bytes))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 return -1;
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800388 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389}
390
391/* read EEPROM content */
392void __devinit bttv_readee(struct bttv *btv, unsigned char *eedata, int addr)
393{
Gerd Knorr5daf05f2005-05-25 12:31:26 -0700394 memset(eedata, 0, 256);
395 if (0 != btv->i2c_rc)
396 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 btv->i2c_client.addr = addr >> 1;
398 tveeprom_read(&btv->i2c_client, eedata, 256);
399}
400
401static char *i2c_devs[128] = {
Mauro Carvalho Chehab24a70fd2005-09-09 13:03:39 -0700402 [ 0x1c >> 1 ] = "lgdt330x",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 [ 0x30 >> 1 ] = "IR (hauppauge)",
404 [ 0x80 >> 1 ] = "msp34xx",
405 [ 0x86 >> 1 ] = "tda9887",
406 [ 0xa0 >> 1 ] = "eeprom",
407 [ 0xc0 >> 1 ] = "tuner (analog)",
408 [ 0xc2 >> 1 ] = "tuner (analog)",
409};
410
411static void do_i2c_scan(char *name, struct i2c_client *c)
412{
413 unsigned char buf;
414 int i,rc;
415
416 for (i = 0; i < 128; i++) {
417 c->addr = i;
418 rc = i2c_master_recv(c,&buf,0);
419 if (rc < 0)
420 continue;
421 printk("%s: i2c scan: found device @ 0x%x [%s]\n",
422 name, i << 1, i2c_devs[i] ? i2c_devs[i] : "???");
423 }
424}
425
426/* init + register i2c algo-bit adapter */
427int __devinit init_bttv_i2c(struct bttv *btv)
428{
429 memcpy(&btv->i2c_client, &bttv_i2c_client_template,
430 sizeof(bttv_i2c_client_template));
431
432 if (i2c_hw)
433 btv->use_i2c_hw = 1;
434 if (btv->use_i2c_hw) {
435 /* bt878 */
436 memcpy(&btv->c.i2c_adap, &bttv_i2c_adap_hw_template,
437 sizeof(bttv_i2c_adap_hw_template));
438 } else {
439 /* bt848 */
Mauro Carvalho Chehab141276b2006-09-06 19:04:28 -0300440 /* Prevents usage of invalid delay values */
441 if (i2c_udelay<5)
442 i2c_udelay=5;
443 bttv_i2c_algo_bit_template.udelay=i2c_udelay;
444
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 memcpy(&btv->c.i2c_adap, &bttv_i2c_adap_sw_template,
446 sizeof(bttv_i2c_adap_sw_template));
447 memcpy(&btv->i2c_algo, &bttv_i2c_algo_bit_template,
448 sizeof(bttv_i2c_algo_bit_template));
449 btv->i2c_algo.data = btv;
450 btv->c.i2c_adap.algo_data = &btv->i2c_algo;
451 }
452
453 btv->c.i2c_adap.dev.parent = &btv->c.pci->dev;
454 snprintf(btv->c.i2c_adap.name, sizeof(btv->c.i2c_adap.name),
455 "bt%d #%d [%s]", btv->id, btv->c.nr,
456 btv->use_i2c_hw ? "hw" : "sw");
457
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800458 i2c_set_adapdata(&btv->c.i2c_adap, btv);
459 btv->i2c_client.adapter = &btv->c.i2c_adap;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 if (bttv_tvcards[btv->c.type].no_video)
462 btv->c.i2c_adap.class &= ~I2C_CLASS_TV_ANALOG;
463 if (bttv_tvcards[btv->c.type].has_dvb)
464 btv->c.i2c_adap.class |= I2C_CLASS_TV_DIGITAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465
466 if (btv->use_i2c_hw) {
467 btv->i2c_rc = i2c_add_adapter(&btv->c.i2c_adap);
468 } else {
469 bttv_bit_setscl(btv,1);
470 bttv_bit_setsda(btv,1);
471 btv->i2c_rc = i2c_bit_add_bus(&btv->c.i2c_adap);
472 }
473 if (0 == btv->i2c_rc && i2c_scan)
474 do_i2c_scan(btv->c.name,&btv->i2c_client);
475 return btv->i2c_rc;
476}
477
478int __devexit fini_bttv_i2c(struct bttv *btv)
479{
480 if (0 != btv->i2c_rc)
481 return 0;
482
483 if (btv->use_i2c_hw) {
484 return i2c_del_adapter(&btv->c.i2c_adap);
485 } else {
486 return i2c_bit_del_bus(&btv->c.i2c_adap);
487 }
488}
489
490/*
491 * Local variables:
492 * c-basic-offset: 8
493 * End:
494 */