patch-2_6_7-vs1_9_1_12
[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 #define __NO_VERSION__ 1
26
27 #include <linux/module.h>
28 #include <linux/init.h>
29
30 #include <asm/io.h>
31
32 #include "cx88.h"
33
34 /* ----------------------------------------------------------------------- */
35
36 void cx8800_bit_setscl(void *data, int state)
37 {
38         struct cx8800_dev *dev = data;
39
40         if (state)
41                 dev->i2c_state |= 0x02;
42         else
43                 dev->i2c_state &= ~0x02;
44         cx_write(MO_I2C, dev->i2c_state);
45         cx_read(MO_I2C);
46 }
47
48 void cx8800_bit_setsda(void *data, int state)
49 {
50         struct cx8800_dev *dev = data;
51
52         if (state)
53                 dev->i2c_state |= 0x01;
54         else
55                 dev->i2c_state &= ~0x01;
56         cx_write(MO_I2C, dev->i2c_state);
57         cx_read(MO_I2C);
58 }
59
60 static int cx8800_bit_getscl(void *data)
61 {
62         struct cx8800_dev *dev = data;
63         u32 state;
64         
65         state = cx_read(MO_I2C);
66         return state & 0x02 ? 1 : 0;
67 }
68
69 static int cx8800_bit_getsda(void *data)
70 {
71         struct cx8800_dev *dev = data;
72         u32 state;
73
74         state = cx_read(MO_I2C);
75         return state & 0x01;
76 }
77
78 /* ----------------------------------------------------------------------- */
79
80 #ifndef I2C_PEC
81 static void cx8800_inc_use(struct i2c_adapter *adap)
82 {
83         MOD_INC_USE_COUNT;
84 }
85
86 static void cx8800_dec_use(struct i2c_adapter *adap)
87 {
88         MOD_DEC_USE_COUNT;
89 }
90 #endif
91
92 static int attach_inform(struct i2c_client *client)
93 {
94         struct cx8800_dev *dev = i2c_get_adapdata(client->adapter);
95
96         if (dev->tuner_type != UNSET)
97                 cx8800_call_i2c_clients(dev,TUNER_SET_TYPE,&dev->tuner_type);
98
99         if (1 /* fixme: debug */)
100                 printk("%s: i2c attach [client=%s]\n",
101                        dev->name, i2c_clientname(client));
102         return 0;
103 }
104
105 void cx8800_call_i2c_clients(struct cx8800_dev *dev, unsigned int cmd, void *arg)
106 {
107         if (0 != dev->i2c_rc)
108                 return;
109         i2c_clients_command(&dev->i2c_adap, cmd, arg);
110 }
111
112 static struct i2c_algo_bit_data cx8800_i2c_algo_template = {
113         .setsda  = cx8800_bit_setsda,
114         .setscl  = cx8800_bit_setscl,
115         .getsda  = cx8800_bit_getsda,
116         .getscl  = cx8800_bit_getscl,
117         .udelay  = 16,
118         .mdelay  = 10,
119         .timeout = 200,
120 };
121
122 /* ----------------------------------------------------------------------- */
123
124 static struct i2c_adapter cx8800_i2c_adap_template = {
125 #ifdef I2C_PEC
126         .owner             = THIS_MODULE,
127 #else
128         .inc_use           = cx8800_inc_use,
129         .dec_use           = cx8800_dec_use,
130 #endif
131 #ifdef I2C_CLASS_TV_ANALOG
132         .class             = I2C_CLASS_TV_ANALOG,
133 #endif
134         I2C_DEVNAME("cx2388x"),
135         .id                = I2C_HW_B_BT848,
136         .client_register   = attach_inform,
137 };
138
139 static struct i2c_client cx8800_i2c_client_template = {
140         I2C_DEVNAME("cx88xx internal"),
141         .id   = -1,
142 };
143
144 /* init + register i2c algo-bit adapter */
145 int __devinit cx8800_i2c_init(struct cx8800_dev *dev)
146 {
147         memcpy(&dev->i2c_adap, &cx8800_i2c_adap_template,
148                sizeof(dev->i2c_adap));
149         memcpy(&dev->i2c_algo, &cx8800_i2c_algo_template,
150                sizeof(dev->i2c_algo));
151         memcpy(&dev->i2c_client, &cx8800_i2c_client_template,
152                sizeof(dev->i2c_client));
153
154         dev->i2c_adap.dev.parent = &dev->pci->dev;
155         strlcpy(dev->i2c_adap.name,dev->name,sizeof(dev->i2c_adap.name));
156         dev->i2c_algo.data = dev;
157         i2c_set_adapdata(&dev->i2c_adap,dev);
158         dev->i2c_adap.algo_data = &dev->i2c_algo;
159         dev->i2c_client.adapter = &dev->i2c_adap;
160
161         cx8800_bit_setscl(dev,1);
162         cx8800_bit_setsda(dev,1);
163
164         dev->i2c_rc = i2c_bit_add_bus(&dev->i2c_adap);
165         printk("%s: i2c register %s\n", dev->name,
166                (0 == dev->i2c_rc) ? "ok" : "FAILED");
167         return dev->i2c_rc;
168 }
169
170 /*
171  * Local variables:
172  * c-basic-offset: 8
173  * End:
174  */