aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/media/video/bttv-i2c.c
diff options
context:
space:
mode:
authorLinus Torvalds <torvalds@ppc970.osdl.org>2005-04-16 18:20:36 -0400
committerLinus Torvalds <torvalds@ppc970.osdl.org>2005-04-16 18:20:36 -0400
commit1da177e4c3f41524e886b7f1b8a0c1fc7321cac2 (patch)
tree0bba044c4ce775e45a88a51686b5d9f90697ea9d /drivers/media/video/bttv-i2c.c
Linux-2.6.12-rc2v2.6.12-rc2
Initial git repository build. I'm not bothering with the full history, even though we have it. We can create a separate "historical" git archive of that later if we want to, and in the meantime it's about 3.2GB when imported into git - space that would just make the early git days unnecessarily complicated, when we don't have a lot of good infrastructure for it. Let it rip!
Diffstat (limited to 'drivers/media/video/bttv-i2c.c')
-rw-r--r--drivers/media/video/bttv-i2c.c461
1 files changed, 461 insertions, 0 deletions
diff --git a/drivers/media/video/bttv-i2c.c b/drivers/media/video/bttv-i2c.c
new file mode 100644
index 000000000000..e42f1ec13f3e
--- /dev/null
+++ b/drivers/media/video/bttv-i2c.c
@@ -0,0 +1,461 @@
1/*
2 $Id: bttv-i2c.c,v 1.18 2005/02/16 12:14:10 kraxel Exp $
3
4 bttv-i2c.c -- all the i2c code is here
5
6 bttv - Bt848 frame grabber driver
7
8 Copyright (C) 1996,97,98 Ralph Metzler (rjkm@thp.uni-koeln.de)
9 & Marcus Metzler (mocm@thp.uni-koeln.de)
10 (c) 1999-2003 Gerd Knorr <kraxel@bytesex.org>
11
12 This program is free software; you can redistribute it and/or modify
13 it under the terms of the GNU General Public License as published by
14 the Free Software Foundation; either version 2 of the License, or
15 (at your option) any later version.
16
17 This program is distributed in the hope that it will be useful,
18 but WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 GNU General Public License for more details.
21
22 You should have received a copy of the GNU General Public License
23 along with this program; if not, write to the Free Software
24 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25
26*/
27
28#include <linux/module.h>
29#include <linux/moduleparam.h>
30#include <linux/init.h>
31#include <linux/delay.h>
32#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
111 I2C_DEVNAME("bt848"),
112 .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{
133 DECLARE_WAITQUEUE(wait, current);
134 int rc = 0;
135
136 add_wait_queue(&btv->i2c_queue, &wait);
137 if (0 == btv->i2c_done)
138 msleep_interruptible(20);
139 remove_wait_queue(&btv->i2c_queue, &wait);
140
141 if (0 == btv->i2c_done)
142 /* timeout */
143 rc = -EIO;
144 if (btv->i2c_done & BT848_INT_RACK)
145 rc = 1;
146 btv->i2c_done = 0;
147 return rc;
148}
149
150#define I2C_HW (BT878_I2C_MODE | BT848_I2C_SYNC |\
151 BT848_I2C_SCL | BT848_I2C_SDA)
152
153static int
154bttv_i2c_sendbytes(struct bttv *btv, const struct i2c_msg *msg, int last)
155{
156 u32 xmit;
157 int retval,cnt;
158
159 /* sanity checks */
160 if (0 == msg->len)
161 return -EINVAL;
162
163 /* start, address + first byte */
164 xmit = (msg->addr << 25) | (msg->buf[0] << 16) | I2C_HW;
165 if (msg->len > 1 || !last)
166 xmit |= BT878_I2C_NOSTOP;
167 btwrite(xmit, BT848_I2C);
168 retval = bttv_i2c_wait_done(btv);
169 if (retval < 0)
170 goto err;
171 if (retval == 0)
172 goto eio;
173 if (i2c_debug) {
174 printk(" <W %02x %02x", msg->addr << 1, msg->buf[0]);
175 if (!(xmit & BT878_I2C_NOSTOP))
176 printk(" >\n");
177 }
178
179 for (cnt = 1; cnt < msg->len; cnt++ ) {
180 /* following bytes */
181 xmit = (msg->buf[cnt] << 24) | I2C_HW | BT878_I2C_NOSTART;
182 if (cnt < msg->len-1 || !last)
183 xmit |= BT878_I2C_NOSTOP;
184 btwrite(xmit, BT848_I2C);
185 retval = bttv_i2c_wait_done(btv);
186 if (retval < 0)
187 goto err;
188 if (retval == 0)
189 goto eio;
190 if (i2c_debug) {
191 printk(" %02x", msg->buf[cnt]);
192 if (!(xmit & BT878_I2C_NOSTOP))
193 printk(" >\n");
194 }
195 }
196 return msg->len;
197
198 eio:
199 retval = -EIO;
200 err:
201 if (i2c_debug)
202 printk(" ERR: %d\n",retval);
203 return retval;
204}
205
206static int
207bttv_i2c_readbytes(struct bttv *btv, const struct i2c_msg *msg, int last)
208{
209 u32 xmit;
210 u32 cnt;
211 int retval;
212
213 for(cnt = 0; cnt < msg->len; cnt++) {
214 xmit = (msg->addr << 25) | (1 << 24) | I2C_HW;
215 if (cnt < msg->len-1)
216 xmit |= BT848_I2C_W3B;
217 if (cnt < msg->len-1 || !last)
218 xmit |= BT878_I2C_NOSTOP;
219 if (cnt)
220 xmit |= BT878_I2C_NOSTART;
221 btwrite(xmit, BT848_I2C);
222 retval = bttv_i2c_wait_done(btv);
223 if (retval < 0)
224 goto err;
225 if (retval == 0)
226 goto eio;
227 msg->buf[cnt] = ((u32)btread(BT848_I2C) >> 8) & 0xff;
228 if (i2c_debug) {
229 if (!(xmit & BT878_I2C_NOSTART))
230 printk(" <R %02x", (msg->addr << 1) +1);
231 printk(" =%02x", msg->buf[cnt]);
232 if (!(xmit & BT878_I2C_NOSTOP))
233 printk(" >\n");
234 }
235 }
236 return msg->len;
237
238 eio:
239 retval = -EIO;
240 err:
241 if (i2c_debug)
242 printk(" ERR: %d\n",retval);
243 return retval;
244}
245
246static int bttv_i2c_xfer(struct i2c_adapter *i2c_adap, struct i2c_msg *msgs, int num)
247{
248 struct bttv *btv = i2c_get_adapdata(i2c_adap);
249 int retval = 0;
250 int i;
251
252 if (i2c_debug)
253 printk("bt-i2c:");
254 btwrite(BT848_INT_I2CDONE|BT848_INT_RACK, BT848_INT_STAT);
255 for (i = 0 ; i < num; i++) {
256 if (msgs[i].flags & I2C_M_RD) {
257 /* read */
258 retval = bttv_i2c_readbytes(btv, &msgs[i], i+1 == num);
259 if (retval < 0)
260 goto err;
261 } else {
262 /* write */
263 retval = bttv_i2c_sendbytes(btv, &msgs[i], i+1 == num);
264 if (retval < 0)
265 goto err;
266 }
267 }
268 return num;
269
270 err:
271 return retval;
272}
273
274static struct i2c_algorithm bttv_algo = {
275 .name = "bt878",
276 .id = I2C_ALGO_BIT | I2C_HW_B_BT848 /* FIXME */,
277 .master_xfer = bttv_i2c_xfer,
278 .algo_control = algo_control,
279 .functionality = functionality,
280};
281
282static struct i2c_adapter bttv_i2c_adap_hw_template = {
283 .owner = THIS_MODULE,
284#ifdef I2C_CLASS_TV_ANALOG
285 .class = I2C_CLASS_TV_ANALOG,
286#endif
287 I2C_DEVNAME("bt878"),
288 .id = I2C_ALGO_BIT | I2C_HW_B_BT848 /* FIXME */,
289 .algo = &bttv_algo,
290 .client_register = attach_inform,
291};
292
293/* ----------------------------------------------------------------------- */
294/* I2C functions - common stuff */
295
296static int attach_inform(struct i2c_client *client)
297{
298 struct bttv *btv = i2c_get_adapdata(client->adapter);
299
300 if (btv->tuner_type != UNSET)
301 bttv_call_i2c_clients(btv,TUNER_SET_TYPE,&btv->tuner_type);
302 if (btv->pinnacle_id != UNSET)
303 bttv_call_i2c_clients(btv,AUDC_CONFIG_PINNACLE,
304 &btv->pinnacle_id);
305 if (bttv_debug)
306 printk("bttv%d: i2c attach [client=%s]\n",
307 btv->c.nr, i2c_clientname(client));
308 return 0;
309}
310
311void bttv_call_i2c_clients(struct bttv *btv, unsigned int cmd, void *arg)
312{
313 if (0 != btv->i2c_rc)
314 return;
315 i2c_clients_command(&btv->c.i2c_adap, cmd, arg);
316}
317
318static struct i2c_client bttv_i2c_client_template = {
319 I2C_DEVNAME("bttv internal"),
320};
321
322
323/* read I2C */
324int bttv_I2CRead(struct bttv *btv, unsigned char addr, char *probe_for)
325{
326 unsigned char buffer = 0;
327
328 if (0 != btv->i2c_rc)
329 return -1;
330 if (bttv_verbose && NULL != probe_for)
331 printk(KERN_INFO "bttv%d: i2c: checking for %s @ 0x%02x... ",
332 btv->c.nr,probe_for,addr);
333 btv->i2c_client.addr = addr >> 1;
334 if (1 != i2c_master_recv(&btv->i2c_client, &buffer, 1)) {
335 if (NULL != probe_for) {
336 if (bttv_verbose)
337 printk("not found\n");
338 } else
339 printk(KERN_WARNING "bttv%d: i2c read 0x%x: error\n",
340 btv->c.nr,addr);
341 return -1;
342 }
343 if (bttv_verbose && NULL != probe_for)
344 printk("found\n");
345 return buffer;
346}
347
348/* write I2C */
349int bttv_I2CWrite(struct bttv *btv, unsigned char addr, unsigned char b1,
350 unsigned char b2, int both)
351{
352 unsigned char buffer[2];
353 int bytes = both ? 2 : 1;
354
355 if (0 != btv->i2c_rc)
356 return -1;
357 btv->i2c_client.addr = addr >> 1;
358 buffer[0] = b1;
359 buffer[1] = b2;
360 if (bytes != i2c_master_send(&btv->i2c_client, buffer, bytes))
361 return -1;
362 return 0;
363}
364
365/* read EEPROM content */
366void __devinit bttv_readee(struct bttv *btv, unsigned char *eedata, int addr)
367{
368 btv->i2c_client.addr = addr >> 1;
369 tveeprom_read(&btv->i2c_client, eedata, 256);
370}
371
372static char *i2c_devs[128] = {
373 [ 0x30 >> 1 ] = "IR (hauppauge)",
374 [ 0x80 >> 1 ] = "msp34xx",
375 [ 0x86 >> 1 ] = "tda9887",
376 [ 0xa0 >> 1 ] = "eeprom",
377 [ 0xc0 >> 1 ] = "tuner (analog)",
378 [ 0xc2 >> 1 ] = "tuner (analog)",
379};
380
381static void do_i2c_scan(char *name, struct i2c_client *c)
382{
383 unsigned char buf;
384 int i,rc;
385
386 for (i = 0; i < 128; i++) {
387 c->addr = i;
388 rc = i2c_master_recv(c,&buf,0);
389 if (rc < 0)
390 continue;
391 printk("%s: i2c scan: found device @ 0x%x [%s]\n",
392 name, i << 1, i2c_devs[i] ? i2c_devs[i] : "???");
393 }
394}
395
396/* init + register i2c algo-bit adapter */
397int __devinit init_bttv_i2c(struct bttv *btv)
398{
399 memcpy(&btv->i2c_client, &bttv_i2c_client_template,
400 sizeof(bttv_i2c_client_template));
401
402 if (i2c_hw)
403 btv->use_i2c_hw = 1;
404 if (btv->use_i2c_hw) {
405 /* bt878 */
406 memcpy(&btv->c.i2c_adap, &bttv_i2c_adap_hw_template,
407 sizeof(bttv_i2c_adap_hw_template));
408 } else {
409 /* bt848 */
410 memcpy(&btv->c.i2c_adap, &bttv_i2c_adap_sw_template,
411 sizeof(bttv_i2c_adap_sw_template));
412 memcpy(&btv->i2c_algo, &bttv_i2c_algo_bit_template,
413 sizeof(bttv_i2c_algo_bit_template));
414 btv->i2c_algo.data = btv;
415 btv->c.i2c_adap.algo_data = &btv->i2c_algo;
416 }
417
418 btv->c.i2c_adap.dev.parent = &btv->c.pci->dev;
419 snprintf(btv->c.i2c_adap.name, sizeof(btv->c.i2c_adap.name),
420 "bt%d #%d [%s]", btv->id, btv->c.nr,
421 btv->use_i2c_hw ? "hw" : "sw");
422
423 i2c_set_adapdata(&btv->c.i2c_adap, btv);
424 btv->i2c_client.adapter = &btv->c.i2c_adap;
425
426#ifdef I2C_CLASS_TV_ANALOG
427 if (bttv_tvcards[btv->c.type].no_video)
428 btv->c.i2c_adap.class &= ~I2C_CLASS_TV_ANALOG;
429 if (bttv_tvcards[btv->c.type].has_dvb)
430 btv->c.i2c_adap.class |= I2C_CLASS_TV_DIGITAL;
431#endif
432
433 if (btv->use_i2c_hw) {
434 btv->i2c_rc = i2c_add_adapter(&btv->c.i2c_adap);
435 } else {
436 bttv_bit_setscl(btv,1);
437 bttv_bit_setsda(btv,1);
438 btv->i2c_rc = i2c_bit_add_bus(&btv->c.i2c_adap);
439 }
440 if (0 == btv->i2c_rc && i2c_scan)
441 do_i2c_scan(btv->c.name,&btv->i2c_client);
442 return btv->i2c_rc;
443}
444
445int __devexit fini_bttv_i2c(struct bttv *btv)
446{
447 if (0 != btv->i2c_rc)
448 return 0;
449
450 if (btv->use_i2c_hw) {
451 return i2c_del_adapter(&btv->c.i2c_adap);
452 } else {
453 return i2c_bit_del_bus(&btv->c.i2c_adap);
454 }
455}
456
457/*
458 * Local variables:
459 * c-basic-offset: 8
460 * End:
461 */