VServer 1.9.2 (patch-2.6.8.1-vs1.9.2.diff)
[linux-2.6.git] / drivers / media / video / cx88 / cx88-i2c.c
1 /*
2     cx88-i2c.c  --  all the i2c code is here
3
4     Copyright (C) 1996,97,98 Ralph  Metzler (rjkm@thp.uni-koeln.de)
5                            & Marcus Metzler (mocm@thp.uni-koeln.de)
6     (c) 2002 Yurij Sysoev <yurij@naturesoft.net>
7     (c) 1999-2003 Gerd Knorr <kraxel@bytesex.org>
8
9     This program is free software; you can redistribute it and/or modify
10     it under the terms of the GNU General Public License as published by
11     the Free Software Foundation; either version 2 of the License, or
12     (at your option) any later version.
13
14     This program is distributed in the hope that it will be useful,
15     but WITHOUT ANY WARRANTY; without even the implied warranty of
16     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17     GNU General Public License for more details.
18
19     You should have received a copy of the GNU General Public License
20     along with this program; if not, write to the Free Software
21     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22     
23 */
24
25 #include <linux/module.h>
26 #include <linux/init.h>
27
28 #include <asm/io.h>
29
30 #include "cx88.h"
31
32 /* ----------------------------------------------------------------------- */
33
34 void cx8800_bit_setscl(void *data, int state)
35 {
36         struct cx8800_dev *dev = data;
37
38         if (state)
39                 dev->i2c_state |= 0x02;
40         else
41                 dev->i2c_state &= ~0x02;
42         cx_write(MO_I2C, dev->i2c_state);
43         cx_read(MO_I2C);
44 }
45
46 void cx8800_bit_setsda(void *data, int state)
47 {
48         struct cx8800_dev *dev = data;
49
50         if (state)
51                 dev->i2c_state |= 0x01;
52         else
53                 dev->i2c_state &= ~0x01;
54         cx_write(MO_I2C, dev->i2c_state);
55         cx_read(MO_I2C);
56 }
57
58 static int cx8800_bit_getscl(void *data)
59 {
60         struct cx8800_dev *dev = data;
61         u32 state;
62         
63         state = cx_read(MO_I2C);
64         return state & 0x02 ? 1 : 0;
65 }
66
67 static int cx8800_bit_getsda(void *data)
68 {
69         struct cx8800_dev *dev = data;
70         u32 state;
71
72         state = cx_read(MO_I2C);
73         return state & 0x01;
74 }
75
76 /* ----------------------------------------------------------------------- */
77
78 #ifndef I2C_PEC
79 static void cx8800_inc_use(struct i2c_adapter *adap)
80 {
81         MOD_INC_USE_COUNT;
82 }
83
84 static void cx8800_dec_use(struct i2c_adapter *adap)
85 {
86         MOD_DEC_USE_COUNT;
87 }
88 #endif
89
90 static int attach_inform(struct i2c_client *client)
91 {
92         struct cx8800_dev *dev = i2c_get_adapdata(client->adapter);
93
94         if (dev->tuner_type != UNSET)
95                 cx8800_call_i2c_clients(dev,TUNER_SET_TYPE,&dev->tuner_type);
96
97         if (1 /* fixme: debug */)
98                 printk("%s: i2c attach [client=%s]\n",
99                        dev->name, i2c_clientname(client));
100         return 0;
101 }
102
103 void cx8800_call_i2c_clients(struct cx8800_dev *dev, unsigned int cmd, void *arg)
104 {
105         if (0 != dev->i2c_rc)
106                 return;
107         i2c_clients_command(&dev->i2c_adap, cmd, arg);
108 }
109
110 static struct i2c_algo_bit_data cx8800_i2c_algo_template = {
111         .setsda  = cx8800_bit_setsda,
112         .setscl  = cx8800_bit_setscl,
113         .getsda  = cx8800_bit_getsda,
114         .getscl  = cx8800_bit_getscl,
115         .udelay  = 16,
116         .mdelay  = 10,
117         .timeout = 200,
118 };
119
120 /* ----------------------------------------------------------------------- */
121
122 static struct i2c_adapter cx8800_i2c_adap_template = {
123 #ifdef I2C_PEC
124         .owner             = THIS_MODULE,
125 #else
126         .inc_use           = cx8800_inc_use,
127         .dec_use           = cx8800_dec_use,
128 #endif
129 #ifdef I2C_CLASS_TV_ANALOG
130         .class             = I2C_CLASS_TV_ANALOG,
131 #endif
132         I2C_DEVNAME("cx2388x"),
133         .id                = I2C_HW_B_BT848,
134         .client_register   = attach_inform,
135 };
136
137 static struct i2c_client cx8800_i2c_client_template = {
138         I2C_DEVNAME("cx88xx internal"),
139         .id   = -1,
140 };
141
142 /* init + register i2c algo-bit adapter */
143 int __devinit cx8800_i2c_init(struct cx8800_dev *dev)
144 {
145         memcpy(&dev->i2c_adap, &cx8800_i2c_adap_template,
146                sizeof(dev->i2c_adap));
147         memcpy(&dev->i2c_algo, &cx8800_i2c_algo_template,
148                sizeof(dev->i2c_algo));
149         memcpy(&dev->i2c_client, &cx8800_i2c_client_template,
150                sizeof(dev->i2c_client));
151
152         dev->i2c_adap.dev.parent = &dev->pci->dev;
153         strlcpy(dev->i2c_adap.name,dev->name,sizeof(dev->i2c_adap.name));
154         dev->i2c_algo.data = dev;
155         i2c_set_adapdata(&dev->i2c_adap,dev);
156         dev->i2c_adap.algo_data = &dev->i2c_algo;
157         dev->i2c_client.adapter = &dev->i2c_adap;
158
159         cx8800_bit_setscl(dev,1);
160         cx8800_bit_setsda(dev,1);
161
162         dev->i2c_rc = i2c_bit_add_bus(&dev->i2c_adap);
163         printk("%s: i2c register %s\n", dev->name,
164                (0 == dev->i2c_rc) ? "ok" : "FAILED");
165         return dev->i2c_rc;
166 }
167
168 /*
169  * Local variables:
170  * c-basic-offset: 8
171  * End:
172  */