V4L/DVB (6079): Cleanup: remove linux/moduleparam.h from drivers/media files
[linux-2.6/mini2440.git] / drivers / media / video / bt8xx / bttv-i2c.c
blob844f1762c45ab516cc99dbaf7383a2a53d2382c5
1 /*
3 bttv-i2c.c -- all the i2c code is here
5 bttv - Bt848 frame grabber driver
7 Copyright (C) 1996,97,98 Ralph Metzler (rjkm@thp.uni-koeln.de)
8 & Marcus Metzler (mocm@thp.uni-koeln.de)
9 (c) 1999-2003 Gerd Knorr <kraxel@bytesex.org>
11 (c) 2005 Mauro Carvalho Chehab <mchehab@infradead.org>
12 - Multituner support and i2c address binding
14 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.
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.
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.
30 #include <linux/module.h>
31 #include <linux/init.h>
32 #include <linux/delay.h>
34 #include "bttvp.h"
35 #include <media/v4l2-common.h>
36 #include <linux/jiffies.h>
37 #include <asm/io.h>
39 static struct i2c_algo_bit_data bttv_i2c_algo_bit_template;
40 static struct i2c_adapter bttv_i2c_adap_sw_template;
41 static struct i2c_adapter bttv_i2c_adap_hw_template;
42 static struct i2c_client bttv_i2c_client_template;
44 static int attach_inform(struct i2c_client *client);
46 static int i2c_debug;
47 static int i2c_hw;
48 static int i2c_scan;
49 module_param(i2c_debug, int, 0644);
50 MODULE_PARM_DESC(i2c_hw,"configure i2c debug level");
51 module_param(i2c_hw, int, 0444);
52 MODULE_PARM_DESC(i2c_hw,"force use of hardware i2c support, "
53 "instead of software bitbang");
54 module_param(i2c_scan, int, 0444);
55 MODULE_PARM_DESC(i2c_scan,"scan i2c bus at insmod time");
57 static unsigned int i2c_udelay = 5;
58 module_param(i2c_udelay, int, 0444);
59 MODULE_PARM_DESC(i2c_udelay,"soft i2c delay at insmod time, in usecs "
60 "(should be 5 or higher). Lower value means higher bus speed.");
62 /* ----------------------------------------------------------------------- */
63 /* I2C functions - bitbanging adapter (software i2c) */
65 static void bttv_bit_setscl(void *data, int state)
67 struct bttv *btv = (struct bttv*)data;
69 if (state)
70 btv->i2c_state |= 0x02;
71 else
72 btv->i2c_state &= ~0x02;
73 btwrite(btv->i2c_state, BT848_I2C);
74 btread(BT848_I2C);
77 static void bttv_bit_setsda(void *data, int state)
79 struct bttv *btv = (struct bttv*)data;
81 if (state)
82 btv->i2c_state |= 0x01;
83 else
84 btv->i2c_state &= ~0x01;
85 btwrite(btv->i2c_state, BT848_I2C);
86 btread(BT848_I2C);
89 static int bttv_bit_getscl(void *data)
91 struct bttv *btv = (struct bttv*)data;
92 int state;
94 state = btread(BT848_I2C) & 0x02 ? 1 : 0;
95 return state;
98 static int bttv_bit_getsda(void *data)
100 struct bttv *btv = (struct bttv*)data;
101 int state;
103 state = btread(BT848_I2C) & 0x01;
104 return state;
107 static struct i2c_algo_bit_data bttv_i2c_algo_bit_template = {
108 .setsda = bttv_bit_setsda,
109 .setscl = bttv_bit_setscl,
110 .getsda = bttv_bit_getsda,
111 .getscl = bttv_bit_getscl,
112 .udelay = 16,
113 .timeout = 200,
116 static struct i2c_adapter bttv_i2c_adap_sw_template = {
117 .owner = THIS_MODULE,
118 .class = I2C_CLASS_TV_ANALOG,
119 .name = "bttv",
120 .id = I2C_HW_B_BT848,
121 .client_register = attach_inform,
124 /* ----------------------------------------------------------------------- */
125 /* I2C functions - hardware i2c */
127 static int algo_control(struct i2c_adapter *adapter,
128 unsigned int cmd, unsigned long arg)
130 return 0;
133 static u32 functionality(struct i2c_adapter *adap)
135 return I2C_FUNC_SMBUS_EMUL;
138 static int
139 bttv_i2c_wait_done(struct bttv *btv)
141 int rc = 0;
143 /* timeout */
144 if (wait_event_interruptible_timeout(btv->i2c_queue,
145 btv->i2c_done, msecs_to_jiffies(85)) == -ERESTARTSYS)
147 rc = -EIO;
149 if (btv->i2c_done & BT848_INT_RACK)
150 rc = 1;
151 btv->i2c_done = 0;
152 return rc;
155 #define I2C_HW (BT878_I2C_MODE | BT848_I2C_SYNC |\
156 BT848_I2C_SCL | BT848_I2C_SDA)
158 static int
159 bttv_i2c_sendbytes(struct bttv *btv, const struct i2c_msg *msg, int last)
161 u32 xmit;
162 int retval,cnt;
164 /* sanity checks */
165 if (0 == msg->len)
166 return -EINVAL;
168 /* start, address + first byte */
169 xmit = (msg->addr << 25) | (msg->buf[0] << 16) | I2C_HW;
170 if (msg->len > 1 || !last)
171 xmit |= BT878_I2C_NOSTOP;
172 btwrite(xmit, BT848_I2C);
173 retval = bttv_i2c_wait_done(btv);
174 if (retval < 0)
175 goto err;
176 if (retval == 0)
177 goto eio;
178 if (i2c_debug) {
179 printk(" <W %02x %02x", msg->addr << 1, msg->buf[0]);
180 if (!(xmit & BT878_I2C_NOSTOP))
181 printk(" >\n");
184 for (cnt = 1; cnt < msg->len; cnt++ ) {
185 /* following bytes */
186 xmit = (msg->buf[cnt] << 24) | I2C_HW | BT878_I2C_NOSTART;
187 if (cnt < msg->len-1 || !last)
188 xmit |= BT878_I2C_NOSTOP;
189 btwrite(xmit, BT848_I2C);
190 retval = bttv_i2c_wait_done(btv);
191 if (retval < 0)
192 goto err;
193 if (retval == 0)
194 goto eio;
195 if (i2c_debug) {
196 printk(" %02x", msg->buf[cnt]);
197 if (!(xmit & BT878_I2C_NOSTOP))
198 printk(" >\n");
201 return msg->len;
203 eio:
204 retval = -EIO;
205 err:
206 if (i2c_debug)
207 printk(" ERR: %d\n",retval);
208 return retval;
211 static int
212 bttv_i2c_readbytes(struct bttv *btv, const struct i2c_msg *msg, int last)
214 u32 xmit;
215 u32 cnt;
216 int retval;
218 for(cnt = 0; cnt < msg->len; cnt++) {
219 xmit = (msg->addr << 25) | (1 << 24) | I2C_HW;
220 if (cnt < msg->len-1)
221 xmit |= BT848_I2C_W3B;
222 if (cnt < msg->len-1 || !last)
223 xmit |= BT878_I2C_NOSTOP;
224 if (cnt)
225 xmit |= BT878_I2C_NOSTART;
226 btwrite(xmit, BT848_I2C);
227 retval = bttv_i2c_wait_done(btv);
228 if (retval < 0)
229 goto err;
230 if (retval == 0)
231 goto eio;
232 msg->buf[cnt] = ((u32)btread(BT848_I2C) >> 8) & 0xff;
233 if (i2c_debug) {
234 if (!(xmit & BT878_I2C_NOSTART))
235 printk(" <R %02x", (msg->addr << 1) +1);
236 printk(" =%02x", msg->buf[cnt]);
237 if (!(xmit & BT878_I2C_NOSTOP))
238 printk(" >\n");
241 return msg->len;
243 eio:
244 retval = -EIO;
245 err:
246 if (i2c_debug)
247 printk(" ERR: %d\n",retval);
248 return retval;
251 static int bttv_i2c_xfer(struct i2c_adapter *i2c_adap, struct i2c_msg *msgs, int num)
253 struct bttv *btv = i2c_get_adapdata(i2c_adap);
254 int retval = 0;
255 int i;
257 if (i2c_debug)
258 printk("bt-i2c:");
259 btwrite(BT848_INT_I2CDONE|BT848_INT_RACK, BT848_INT_STAT);
260 for (i = 0 ; i < num; i++) {
261 if (msgs[i].flags & I2C_M_RD) {
262 /* read */
263 retval = bttv_i2c_readbytes(btv, &msgs[i], i+1 == num);
264 if (retval < 0)
265 goto err;
266 } else {
267 /* write */
268 retval = bttv_i2c_sendbytes(btv, &msgs[i], i+1 == num);
269 if (retval < 0)
270 goto err;
273 return num;
275 err:
276 return retval;
279 static struct i2c_algorithm bttv_algo = {
280 .master_xfer = bttv_i2c_xfer,
281 .algo_control = algo_control,
282 .functionality = functionality,
285 static struct i2c_adapter bttv_i2c_adap_hw_template = {
286 .owner = THIS_MODULE,
287 .class = I2C_CLASS_TV_ANALOG,
288 .name = "bt878",
289 .id = I2C_HW_B_BT848 /* FIXME */,
290 .algo = &bttv_algo,
291 .client_register = attach_inform,
294 /* ----------------------------------------------------------------------- */
295 /* I2C functions - common stuff */
297 static int attach_inform(struct i2c_client *client)
299 struct bttv *btv = i2c_get_adapdata(client->adapter);
300 int addr=ADDR_UNSET;
303 if (ADDR_UNSET != bttv_tvcards[btv->c.type].tuner_addr)
304 addr = bttv_tvcards[btv->c.type].tuner_addr;
307 if (bttv_debug)
308 printk(KERN_DEBUG "bttv%d: %s i2c attach [addr=0x%x,client=%s]\n",
309 btv->c.nr, client->driver->driver.name, client->addr,
310 client->name);
311 if (!client->driver->command)
312 return 0;
314 if (client->driver->id == I2C_DRIVERID_MSP3400)
315 btv->i2c_msp34xx_client = client;
316 if (client->driver->id == I2C_DRIVERID_TVAUDIO)
317 btv->i2c_tvaudio_client = client;
318 if (btv->tuner_type != UNSET) {
319 struct tuner_setup tun_setup;
321 if ((addr==ADDR_UNSET) ||
322 (addr==client->addr)) {
324 tun_setup.mode_mask = T_ANALOG_TV | T_DIGITAL_TV | T_RADIO;
325 tun_setup.type = btv->tuner_type;
326 tun_setup.addr = addr;
327 bttv_call_i2c_clients(btv, TUNER_SET_TYPE_ADDR, &tun_setup);
332 return 0;
335 void bttv_call_i2c_clients(struct bttv *btv, unsigned int cmd, void *arg)
337 if (0 != btv->i2c_rc)
338 return;
339 i2c_clients_command(&btv->c.i2c_adap, cmd, arg);
342 static struct i2c_client bttv_i2c_client_template = {
343 .name = "bttv internal",
347 /* read I2C */
348 int bttv_I2CRead(struct bttv *btv, unsigned char addr, char *probe_for)
350 unsigned char buffer = 0;
352 if (0 != btv->i2c_rc)
353 return -1;
354 if (bttv_verbose && NULL != probe_for)
355 printk(KERN_INFO "bttv%d: i2c: checking for %s @ 0x%02x... ",
356 btv->c.nr,probe_for,addr);
357 btv->i2c_client.addr = addr >> 1;
358 if (1 != i2c_master_recv(&btv->i2c_client, &buffer, 1)) {
359 if (NULL != probe_for) {
360 if (bttv_verbose)
361 printk("not found\n");
362 } else
363 printk(KERN_WARNING "bttv%d: i2c read 0x%x: error\n",
364 btv->c.nr,addr);
365 return -1;
367 if (bttv_verbose && NULL != probe_for)
368 printk("found\n");
369 return buffer;
372 /* write I2C */
373 int bttv_I2CWrite(struct bttv *btv, unsigned char addr, unsigned char b1,
374 unsigned char b2, int both)
376 unsigned char buffer[2];
377 int bytes = both ? 2 : 1;
379 if (0 != btv->i2c_rc)
380 return -1;
381 btv->i2c_client.addr = addr >> 1;
382 buffer[0] = b1;
383 buffer[1] = b2;
384 if (bytes != i2c_master_send(&btv->i2c_client, buffer, bytes))
385 return -1;
386 return 0;
389 /* read EEPROM content */
390 void __devinit bttv_readee(struct bttv *btv, unsigned char *eedata, int addr)
392 memset(eedata, 0, 256);
393 if (0 != btv->i2c_rc)
394 return;
395 btv->i2c_client.addr = addr >> 1;
396 tveeprom_read(&btv->i2c_client, eedata, 256);
399 static char *i2c_devs[128] = {
400 [ 0x1c >> 1 ] = "lgdt330x",
401 [ 0x30 >> 1 ] = "IR (hauppauge)",
402 [ 0x80 >> 1 ] = "msp34xx",
403 [ 0x86 >> 1 ] = "tda9887",
404 [ 0xa0 >> 1 ] = "eeprom",
405 [ 0xc0 >> 1 ] = "tuner (analog)",
406 [ 0xc2 >> 1 ] = "tuner (analog)",
409 static void do_i2c_scan(char *name, struct i2c_client *c)
411 unsigned char buf;
412 int i,rc;
414 for (i = 0; i < ARRAY_SIZE(i2c_devs); i++) {
415 c->addr = i;
416 rc = i2c_master_recv(c,&buf,0);
417 if (rc < 0)
418 continue;
419 printk("%s: i2c scan: found device @ 0x%x [%s]\n",
420 name, i << 1, i2c_devs[i] ? i2c_devs[i] : "???");
424 /* init + register i2c algo-bit adapter */
425 int __devinit init_bttv_i2c(struct bttv *btv)
427 memcpy(&btv->i2c_client, &bttv_i2c_client_template,
428 sizeof(bttv_i2c_client_template));
430 if (i2c_hw)
431 btv->use_i2c_hw = 1;
432 if (btv->use_i2c_hw) {
433 /* bt878 */
434 memcpy(&btv->c.i2c_adap, &bttv_i2c_adap_hw_template,
435 sizeof(bttv_i2c_adap_hw_template));
436 } else {
437 /* bt848 */
438 /* Prevents usage of invalid delay values */
439 if (i2c_udelay<5)
440 i2c_udelay=5;
441 bttv_i2c_algo_bit_template.udelay=i2c_udelay;
443 memcpy(&btv->c.i2c_adap, &bttv_i2c_adap_sw_template,
444 sizeof(bttv_i2c_adap_sw_template));
445 memcpy(&btv->i2c_algo, &bttv_i2c_algo_bit_template,
446 sizeof(bttv_i2c_algo_bit_template));
447 btv->i2c_algo.data = btv;
448 btv->c.i2c_adap.algo_data = &btv->i2c_algo;
451 btv->c.i2c_adap.dev.parent = &btv->c.pci->dev;
452 snprintf(btv->c.i2c_adap.name, sizeof(btv->c.i2c_adap.name),
453 "bt%d #%d [%s]", btv->id, btv->c.nr,
454 btv->use_i2c_hw ? "hw" : "sw");
456 i2c_set_adapdata(&btv->c.i2c_adap, btv);
457 btv->i2c_client.adapter = &btv->c.i2c_adap;
459 if (bttv_tvcards[btv->c.type].no_video)
460 btv->c.i2c_adap.class &= ~I2C_CLASS_TV_ANALOG;
461 if (bttv_tvcards[btv->c.type].has_dvb)
462 btv->c.i2c_adap.class |= I2C_CLASS_TV_DIGITAL;
464 if (btv->use_i2c_hw) {
465 btv->i2c_rc = i2c_add_adapter(&btv->c.i2c_adap);
466 } else {
467 bttv_bit_setscl(btv,1);
468 bttv_bit_setsda(btv,1);
469 btv->i2c_rc = i2c_bit_add_bus(&btv->c.i2c_adap);
471 if (0 == btv->i2c_rc && i2c_scan)
472 do_i2c_scan(btv->c.name,&btv->i2c_client);
473 return btv->i2c_rc;
476 int __devexit fini_bttv_i2c(struct bttv *btv)
478 if (0 != btv->i2c_rc)
479 return 0;
481 return i2c_del_adapter(&btv->c.i2c_adap);
485 * Local variables:
486 * c-basic-offset: 8
487 * End: