3e748248115278ea1c0af14875ba1f4e0c567aad
[safe/jmp/linux-2.6] / drivers / media / video / au0828 / au0828-i2c.c
1 /*
2  *  Driver for the Auvitek AU0828 USB bridge
3  *
4  *  Copyright (c) 2008 Steven Toth <stoth@hauppauge.com>
5  *
6  *  This program is free software; you can redistribute it and/or modify
7  *  it under the terms of the GNU General Public License as published by
8  *  the Free Software Foundation; either version 2 of the License, or
9  *  (at your option) any later version.
10  *
11  *  This program is distributed in the hope that it will be useful,
12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *
15  *  GNU General Public License for more details.
16  *
17  *  You should have received a copy of the GNU General Public License
18  *  along with this program; if not, write to the Free Software
19  *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20  */
21
22 #include <linux/module.h>
23 #include <linux/moduleparam.h>
24 #include <linux/init.h>
25 #include <linux/delay.h>
26 #include <asm/io.h>
27
28 #include "au0828.h"
29
30 #include <media/v4l2-common.h>
31
32 static unsigned int i2c_debug;
33 module_param(i2c_debug, int, 0644);
34 MODULE_PARM_DESC(i2c_debug, "enable debug messages [i2c]");
35
36 static unsigned int i2c_scan = 0;
37 module_param(i2c_scan, int, 0444);
38 MODULE_PARM_DESC(i2c_scan, "scan i2c bus at insmod time");
39
40 #define dprintk(level, fmt, arg...)\
41         do { if (i2c_debug >= level)\
42                 printk(KERN_DEBUG "%s/0: " fmt, DRIVER_NAME, ## arg);\
43         } while (0)
44
45 #define I2C_WAIT_DELAY 512
46 #define I2C_WAIT_RETRY 64
47
48 static inline int i2c_slave_did_write_ack(struct i2c_adapter *i2c_adap)
49 {
50         struct au0828_dev *dev = i2c_adap->algo_data;
51         return au0828_read(dev, REG_201) & 0x08 ? 0 : 1;
52 }
53
54 static inline int i2c_slave_did_read_ack(struct i2c_adapter *i2c_adap)
55 {
56         struct au0828_dev *dev = i2c_adap->algo_data;
57         return au0828_read(dev, REG_201) & 0x02 ? 0 : 1;
58 }
59
60 static int i2c_wait_read_ack(struct i2c_adapter *i2c_adap)
61 {
62         int count;
63
64         for (count = 0; count < I2C_WAIT_RETRY; count++) {
65                 if (!i2c_slave_did_read_ack(i2c_adap))
66                         break;
67                 udelay(I2C_WAIT_DELAY);
68         }
69
70         if (I2C_WAIT_RETRY == count)
71                 return 0;
72
73         return 1;
74 }
75
76 static inline int i2c_is_read_busy(struct i2c_adapter *i2c_adap)
77 {
78         struct au0828_dev *dev = i2c_adap->algo_data;
79         return au0828_read(dev, REG_201) & 0x01 ? 0 : 1;
80 }
81
82 static int i2c_wait_read_done(struct i2c_adapter *i2c_adap)
83 {
84         int count;
85
86         for (count = 0; count < I2C_WAIT_RETRY; count++) {
87                 if (!i2c_is_read_busy(i2c_adap))
88                         break;
89                 udelay(I2C_WAIT_DELAY);
90         }
91
92         if (I2C_WAIT_RETRY == count)
93                 return 0;
94
95         return 1;
96 }
97
98 static inline int i2c_is_write_done(struct i2c_adapter *i2c_adap)
99 {
100         struct au0828_dev *dev = i2c_adap->algo_data;
101         return au0828_read(dev, REG_201) & 0x04 ? 1 : 0;
102 }
103
104 static int i2c_wait_write_done(struct i2c_adapter *i2c_adap)
105 {
106         int count;
107
108         for (count = 0; count < I2C_WAIT_RETRY; count++) {
109                 if (i2c_is_write_done(i2c_adap))
110                         break;
111                 udelay(I2C_WAIT_DELAY);
112         }
113
114         if (I2C_WAIT_RETRY == count)
115                 return 0;
116
117         return 1;
118 }
119
120 static inline int i2c_is_busy(struct i2c_adapter *i2c_adap)
121 {
122         struct au0828_dev *dev = i2c_adap->algo_data;
123         return au0828_read(dev, REG_201) & 0x10 ? 1 : 0;
124 }
125
126 static int i2c_wait_done(struct i2c_adapter *i2c_adap)
127 {
128         int count;
129
130         for (count = 0; count < I2C_WAIT_RETRY; count++) {
131                 if (!i2c_is_busy(i2c_adap))
132                         break;
133                 udelay(I2C_WAIT_DELAY);
134         }
135
136         if (I2C_WAIT_RETRY == count)
137                 return 0;
138
139         return 1;
140 }
141
142 /* FIXME: Implement join handling correctly */
143 static int i2c_sendbytes(struct i2c_adapter *i2c_adap,
144         const struct i2c_msg *msg, int joined_rlen)
145 {
146         int i, strobe = 0;
147         struct au0828_dev *dev = i2c_adap->algo_data;
148
149         dprintk(1, "%s()\n", __FUNCTION__);
150
151         au0828_write(dev, REG_2FF, 0x01);
152         au0828_write(dev, REG_202, 0x07);
153
154         /* Hardware needs 8 bit addresses */
155         au0828_write(dev, REG_203, msg->addr << 1);
156
157         if (i2c_debug)
158                 dprintk(1, "SEND: %02x\n", msg->addr);
159
160         for (i=0; i < msg->len;) {
161
162                 if (i2c_debug)
163                         dprintk(1, " %02x\n", msg->buf[i]);
164
165                 au0828_write(dev, REG_205, msg->buf[i]);
166
167                 strobe++;
168                 i++;
169
170                 if ((strobe >= 4) || (i >= msg->len)) {
171
172                         /* Strobe the byte into the bus */
173                         if (i < msg->len)
174                                 au0828_write(dev, REG_200, 0x41);
175                         else
176                                 au0828_write(dev, REG_200, 0x01);
177
178                         /* Reset strobe trigger */
179                         strobe = 0;
180
181                         if (!i2c_wait_write_done(i2c_adap))
182                                 return -EIO;
183
184                 }
185
186         }
187         if (!i2c_wait_done(i2c_adap))
188                 return -EIO;
189
190         if (i2c_debug)
191                 dprintk(1, "\n");
192
193         return msg->len;
194 }
195
196 /* FIXME: Implement join handling correctly */
197 static int i2c_readbytes(struct i2c_adapter *i2c_adap,
198         const struct i2c_msg *msg, int joined)
199 {
200         struct au0828_dev *dev = i2c_adap->algo_data;
201         int i;
202
203         dprintk(1, "%s()\n", __FUNCTION__);
204
205         au0828_write(dev, REG_2FF, 0x01);
206         au0828_write(dev, REG_202, 0x07);
207
208         /* Hardware needs 8 bit addresses */
209         au0828_write(dev, REG_203, msg->addr << 1);
210
211         if (i2c_debug)
212                 dprintk(1, " RECV:\n");
213
214         /* Deal with i2c_scan */
215         if (msg->len == 0) {
216                 au0828_write(dev, REG_200, 0x20);
217                 if (i2c_wait_read_ack(i2c_adap))
218                         return -EIO;
219                 return 0;
220         }
221
222         for (i=0; i < msg->len;) {
223
224                 i++;
225
226                 if (i < msg->len)
227                         au0828_write(dev, REG_200, 0x60);
228                 else
229                         au0828_write(dev, REG_200, 0x20);
230
231                 if (!i2c_wait_read_done(i2c_adap))
232                         return -EIO;
233
234                 msg->buf[i-1] = au0828_read(dev, REG_209) & 0xff;
235
236                 if (i2c_debug)
237                         dprintk(1, " %02x\n", msg->buf[i-1]);
238         }
239         if (!i2c_wait_done(i2c_adap))
240                 return -EIO;
241
242         if (i2c_debug)
243                 dprintk(1, "\n");
244
245         return msg->len;
246 }
247
248 static int i2c_xfer(struct i2c_adapter *i2c_adap,
249                     struct i2c_msg *msgs, int num)
250 {
251         int i, retval = 0;
252
253         dprintk(1, "%s(num = %d)\n", __FUNCTION__, num);
254
255         for (i = 0 ; i < num; i++) {
256                 dprintk(1, "%s(num = %d) addr = 0x%02x  len = 0x%x\n",
257                         __FUNCTION__, num, msgs[i].addr, msgs[i].len);
258                 if (msgs[i].flags & I2C_M_RD) {
259                         /* read */
260                         retval = i2c_readbytes(i2c_adap, &msgs[i], 0);
261                 } else if (i + 1 < num && (msgs[i + 1].flags & I2C_M_RD) &&
262                            msgs[i].addr == msgs[i + 1].addr) {
263                         /* write then read from same address */
264                         retval = i2c_sendbytes(i2c_adap, &msgs[i],
265                                                msgs[i + 1].len);
266                         if (retval < 0)
267                                 goto err;
268                         i++;
269                         retval = i2c_readbytes(i2c_adap, &msgs[i], 1);
270                 } else {
271                         /* write */
272                         retval = i2c_sendbytes(i2c_adap, &msgs[i], 0);
273                 }
274                 if (retval < 0)
275                         goto err;
276         }
277         return num;
278
279 err:
280         return retval;
281 }
282
283 static int attach_inform(struct i2c_client *client)
284 {
285         dprintk(1, "%s i2c attach [addr=0x%x,client=%s]\n",
286                 client->driver->driver.name, client->addr, client->name);
287
288         if (!client->driver->command)
289                 return 0;
290
291         return 0;
292 }
293
294 static int detach_inform(struct i2c_client *client)
295 {
296         dprintk(1, "i2c detach [client=%s]\n", client->name);
297
298         return 0;
299 }
300
301 void au0828_call_i2c_clients(struct au0828_dev *dev,
302                               unsigned int cmd, void *arg)
303 {
304         if (dev->i2c_rc != 0)
305                 return;
306
307         i2c_clients_command(&dev->i2c_adap, cmd, arg);
308 }
309
310 static u32 au0828_functionality(struct i2c_adapter *adap)
311 {
312         return I2C_FUNC_SMBUS_EMUL | I2C_FUNC_I2C;
313 }
314
315 static struct i2c_algorithm au0828_i2c_algo_template = {
316         .master_xfer    = i2c_xfer,
317         .functionality  = au0828_functionality,
318 };
319
320 /* ----------------------------------------------------------------------- */
321
322 static struct i2c_adapter au0828_i2c_adap_template = {
323         .name              = DRIVER_NAME,
324         .owner             = THIS_MODULE,
325         .id                = I2C_HW_B_AU0828,
326         .algo              = &au0828_i2c_algo_template,
327         .class             = I2C_CLASS_TV_ANALOG,
328         .client_register   = attach_inform,
329         .client_unregister = detach_inform,
330 };
331
332 static struct i2c_client au0828_i2c_client_template = {
333         .name   = "au0828 internal",
334 };
335
336 static char *i2c_devs[128] = {
337         [ 0x8e >> 1 ] = "au8522",
338         [ 0xa0 >> 1 ] = "eeprom",
339         [ 0xc2 >> 1 ] = "tuner/xc5000",
340 };
341
342 static void do_i2c_scan(char *name, struct i2c_client *c)
343 {
344         unsigned char buf;
345         int i, rc;
346
347         for (i = 0; i < 128; i++) {
348                 c->addr = i;
349                 rc = i2c_master_recv(c, &buf, 0);
350                 if (rc < 0)
351                         continue;
352                 printk("%s: i2c scan: found device @ 0x%x  [%s]\n",
353                        name, i << 1, i2c_devs[i] ? i2c_devs[i] : "???");
354         }
355 }
356
357 /* init + register i2c algo-bit adapter */
358 int au0828_i2c_register(struct au0828_dev *dev)
359 {
360         dprintk(1, "%s()\n", __FUNCTION__);
361
362         memcpy(&dev->i2c_adap, &au0828_i2c_adap_template,
363                sizeof(dev->i2c_adap));
364         memcpy(&dev->i2c_algo, &au0828_i2c_algo_template,
365                sizeof(dev->i2c_algo));
366         memcpy(&dev->i2c_client, &au0828_i2c_client_template,
367                sizeof(dev->i2c_client));
368
369         dev->i2c_adap.dev.parent = &dev->usbdev->dev;
370
371         strlcpy(dev->i2c_adap.name, DRIVER_NAME,
372                 sizeof(dev->i2c_adap.name));
373
374         dev->i2c_algo.data = dev;
375         dev->i2c_adap.algo_data = dev;
376         i2c_set_adapdata(&dev->i2c_adap, dev);
377         i2c_add_adapter(&dev->i2c_adap);
378
379         dev->i2c_client.adapter = &dev->i2c_adap;
380
381         if (0 == dev->i2c_rc) {
382                 printk("%s: i2c bus registered\n", DRIVER_NAME);
383                 if (i2c_scan)
384                         do_i2c_scan(DRIVER_NAME, &dev->i2c_client);
385         } else
386                 printk("%s: i2c bus register FAILED\n", DRIVER_NAME);
387         return dev->i2c_rc;
388 }
389
390 int au0828_i2c_unregister(struct au0828_dev *dev)
391 {
392         i2c_del_adapter(&dev->i2c_adap);
393         return 0;
394 }
395
396 /* ----------------------------------------------------------------------- */
397
398 /*
399  * Local variables:
400  * c-basic-offset: 8
401  * End:
402  */