blob: e841ba5a64241de50020d95297b021743b7d05ad [file] [log] [blame]
Steven Toth265a6512008-04-18 21:34:00 -03001/*
2 * Driver for the Auvitek AU0828 USB bridge
3 *
Steven Toth6d897612008-09-03 17:12:12 -03004 * Copyright (c) 2008 Steven Toth <stoth@linuxtv.org>
Steven Toth265a6512008-04-18 21:34:00 -03005 *
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>
Mauro Carvalho Chehab18d73c52008-04-18 22:12:52 -030026#include <linux/io.h>
Steven Toth265a6512008-04-18 21:34:00 -030027
28#include "au0828.h"
29
30#include <media/v4l2-common.h>
31
Adrian Bunkb33d24c2008-04-25 19:06:03 -030032static int i2c_scan;
Steven Toth265a6512008-04-18 21:34:00 -030033module_param(i2c_scan, int, 0444);
34MODULE_PARM_DESC(i2c_scan, "scan i2c bus at insmod time");
35
Steven Toth265a6512008-04-18 21:34:00 -030036#define I2C_WAIT_DELAY 512
37#define I2C_WAIT_RETRY 64
38
39static inline int i2c_slave_did_write_ack(struct i2c_adapter *i2c_adap)
40{
41 struct au0828_dev *dev = i2c_adap->algo_data;
42 return au0828_read(dev, REG_201) & 0x08 ? 0 : 1;
43}
44
45static inline int i2c_slave_did_read_ack(struct i2c_adapter *i2c_adap)
46{
47 struct au0828_dev *dev = i2c_adap->algo_data;
48 return au0828_read(dev, REG_201) & 0x02 ? 0 : 1;
49}
50
51static int i2c_wait_read_ack(struct i2c_adapter *i2c_adap)
52{
53 int count;
54
55 for (count = 0; count < I2C_WAIT_RETRY; count++) {
56 if (!i2c_slave_did_read_ack(i2c_adap))
57 break;
58 udelay(I2C_WAIT_DELAY);
59 }
60
61 if (I2C_WAIT_RETRY == count)
62 return 0;
63
64 return 1;
65}
66
67static inline int i2c_is_read_busy(struct i2c_adapter *i2c_adap)
68{
69 struct au0828_dev *dev = i2c_adap->algo_data;
70 return au0828_read(dev, REG_201) & 0x01 ? 0 : 1;
71}
72
73static int i2c_wait_read_done(struct i2c_adapter *i2c_adap)
74{
75 int count;
76
77 for (count = 0; count < I2C_WAIT_RETRY; count++) {
78 if (!i2c_is_read_busy(i2c_adap))
79 break;
80 udelay(I2C_WAIT_DELAY);
81 }
82
83 if (I2C_WAIT_RETRY == count)
84 return 0;
85
86 return 1;
87}
88
89static inline int i2c_is_write_done(struct i2c_adapter *i2c_adap)
90{
91 struct au0828_dev *dev = i2c_adap->algo_data;
92 return au0828_read(dev, REG_201) & 0x04 ? 1 : 0;
93}
94
95static int i2c_wait_write_done(struct i2c_adapter *i2c_adap)
96{
97 int count;
98
99 for (count = 0; count < I2C_WAIT_RETRY; count++) {
100 if (i2c_is_write_done(i2c_adap))
101 break;
102 udelay(I2C_WAIT_DELAY);
103 }
104
105 if (I2C_WAIT_RETRY == count)
106 return 0;
107
108 return 1;
109}
110
111static inline int i2c_is_busy(struct i2c_adapter *i2c_adap)
112{
113 struct au0828_dev *dev = i2c_adap->algo_data;
114 return au0828_read(dev, REG_201) & 0x10 ? 1 : 0;
115}
116
117static int i2c_wait_done(struct i2c_adapter *i2c_adap)
118{
119 int count;
120
121 for (count = 0; count < I2C_WAIT_RETRY; count++) {
122 if (!i2c_is_busy(i2c_adap))
123 break;
124 udelay(I2C_WAIT_DELAY);
125 }
126
127 if (I2C_WAIT_RETRY == count)
128 return 0;
129
130 return 1;
131}
132
133/* FIXME: Implement join handling correctly */
134static int i2c_sendbytes(struct i2c_adapter *i2c_adap,
135 const struct i2c_msg *msg, int joined_rlen)
136{
137 int i, strobe = 0;
138 struct au0828_dev *dev = i2c_adap->algo_data;
139
Michael Krufkyf07e8e42008-04-18 21:42:30 -0300140 dprintk(4, "%s()\n", __func__);
Steven Toth265a6512008-04-18 21:34:00 -0300141
142 au0828_write(dev, REG_2FF, 0x01);
Devin Heitmueller32c000a2009-03-11 03:00:41 -0300143
144 /* FIXME: There is a problem with i2c communications with xc5000 that
145 requires us to slow down the i2c clock until we have a better
146 strategy (such as using the secondary i2c bus to do firmware
147 loading */
148 if ((msg->addr << 1) == 0xc2) {
149 au0828_write(dev, REG_202, 0x40);
150 } else {
151 au0828_write(dev, REG_202, 0x07);
152 }
Steven Toth265a6512008-04-18 21:34:00 -0300153
154 /* Hardware needs 8 bit addresses */
155 au0828_write(dev, REG_203, msg->addr << 1);
156
Steven Tothbc3c6132008-04-18 21:39:11 -0300157 dprintk(4, "SEND: %02x\n", msg->addr);
Steven Toth265a6512008-04-18 21:34:00 -0300158
Devin Heitmuellerdc8685b2009-03-11 03:00:56 -0300159 /* Deal with i2c_scan */
160 if (msg->len == 0) {
161 /* The analog tuner detection code makes use of the SMBUS_QUICK
162 message (which involves a zero length i2c write). To avoid
163 checking the status register when we didn't strobe out any
164 actual bytes to the bus, just do a read check. This is
165 consistent with how I saw i2c device checking done in the
166 USB trace of the Windows driver */
167 au0828_write(dev, REG_200, 0x20);
168 if (!i2c_wait_done(i2c_adap))
169 return -EIO;
170
171 if (i2c_wait_read_ack(i2c_adap))
172 return -EIO;
173
174 return 0;
175 }
176
Steven Totha9c36aa2008-04-17 21:41:28 -0300177 for (i = 0; i < msg->len;) {
Steven Toth265a6512008-04-18 21:34:00 -0300178
Steven Tothbc3c6132008-04-18 21:39:11 -0300179 dprintk(4, " %02x\n", msg->buf[i]);
Steven Toth265a6512008-04-18 21:34:00 -0300180
181 au0828_write(dev, REG_205, msg->buf[i]);
182
183 strobe++;
184 i++;
185
186 if ((strobe >= 4) || (i >= msg->len)) {
187
188 /* Strobe the byte into the bus */
189 if (i < msg->len)
190 au0828_write(dev, REG_200, 0x41);
191 else
192 au0828_write(dev, REG_200, 0x01);
193
194 /* Reset strobe trigger */
195 strobe = 0;
196
197 if (!i2c_wait_write_done(i2c_adap))
198 return -EIO;
199
200 }
201
202 }
203 if (!i2c_wait_done(i2c_adap))
204 return -EIO;
205
Steven Tothbc3c6132008-04-18 21:39:11 -0300206 dprintk(4, "\n");
Steven Toth265a6512008-04-18 21:34:00 -0300207
208 return msg->len;
209}
210
211/* FIXME: Implement join handling correctly */
212static int i2c_readbytes(struct i2c_adapter *i2c_adap,
213 const struct i2c_msg *msg, int joined)
214{
215 struct au0828_dev *dev = i2c_adap->algo_data;
216 int i;
217
Michael Krufkyf07e8e42008-04-18 21:42:30 -0300218 dprintk(4, "%s()\n", __func__);
Steven Toth265a6512008-04-18 21:34:00 -0300219
220 au0828_write(dev, REG_2FF, 0x01);
Devin Heitmueller32c000a2009-03-11 03:00:41 -0300221
222 /* FIXME: There is a problem with i2c communications with xc5000 that
223 requires us to slow down the i2c clock until we have a better
224 strategy (such as using the secondary i2c bus to do firmware
225 loading */
226 if ((msg->addr << 1) == 0xc2) {
227 au0828_write(dev, REG_202, 0x40);
228 } else {
229 au0828_write(dev, REG_202, 0x07);
230 }
Steven Toth265a6512008-04-18 21:34:00 -0300231
232 /* Hardware needs 8 bit addresses */
233 au0828_write(dev, REG_203, msg->addr << 1);
234
Steven Tothbc3c6132008-04-18 21:39:11 -0300235 dprintk(4, " RECV:\n");
Steven Toth265a6512008-04-18 21:34:00 -0300236
237 /* Deal with i2c_scan */
238 if (msg->len == 0) {
239 au0828_write(dev, REG_200, 0x20);
240 if (i2c_wait_read_ack(i2c_adap))
241 return -EIO;
242 return 0;
243 }
244
Steven Totha9c36aa2008-04-17 21:41:28 -0300245 for (i = 0; i < msg->len;) {
Steven Toth265a6512008-04-18 21:34:00 -0300246
247 i++;
248
249 if (i < msg->len)
250 au0828_write(dev, REG_200, 0x60);
251 else
252 au0828_write(dev, REG_200, 0x20);
253
254 if (!i2c_wait_read_done(i2c_adap))
255 return -EIO;
256
257 msg->buf[i-1] = au0828_read(dev, REG_209) & 0xff;
258
Steven Tothbc3c6132008-04-18 21:39:11 -0300259 dprintk(4, " %02x\n", msg->buf[i-1]);
Steven Toth265a6512008-04-18 21:34:00 -0300260 }
261 if (!i2c_wait_done(i2c_adap))
262 return -EIO;
263
Steven Tothbc3c6132008-04-18 21:39:11 -0300264 dprintk(4, "\n");
Steven Toth265a6512008-04-18 21:34:00 -0300265
266 return msg->len;
267}
268
269static int i2c_xfer(struct i2c_adapter *i2c_adap,
270 struct i2c_msg *msgs, int num)
271{
272 int i, retval = 0;
273
Michael Krufkyf07e8e42008-04-18 21:42:30 -0300274 dprintk(4, "%s(num = %d)\n", __func__, num);
Steven Toth265a6512008-04-18 21:34:00 -0300275
Steven Totha9c36aa2008-04-17 21:41:28 -0300276 for (i = 0; i < num; i++) {
Steven Tothbc3c6132008-04-18 21:39:11 -0300277 dprintk(4, "%s(num = %d) addr = 0x%02x len = 0x%x\n",
Michael Krufkyf07e8e42008-04-18 21:42:30 -0300278 __func__, num, msgs[i].addr, msgs[i].len);
Steven Toth265a6512008-04-18 21:34:00 -0300279 if (msgs[i].flags & I2C_M_RD) {
280 /* read */
281 retval = i2c_readbytes(i2c_adap, &msgs[i], 0);
282 } else if (i + 1 < num && (msgs[i + 1].flags & I2C_M_RD) &&
283 msgs[i].addr == msgs[i + 1].addr) {
284 /* write then read from same address */
285 retval = i2c_sendbytes(i2c_adap, &msgs[i],
286 msgs[i + 1].len);
287 if (retval < 0)
288 goto err;
289 i++;
290 retval = i2c_readbytes(i2c_adap, &msgs[i], 1);
291 } else {
292 /* write */
293 retval = i2c_sendbytes(i2c_adap, &msgs[i], 0);
294 }
295 if (retval < 0)
296 goto err;
297 }
298 return num;
299
300err:
301 return retval;
302}
303
304static int attach_inform(struct i2c_client *client)
305{
306 dprintk(1, "%s i2c attach [addr=0x%x,client=%s]\n",
307 client->driver->driver.name, client->addr, client->name);
308
309 if (!client->driver->command)
310 return 0;
311
312 return 0;
313}
314
315static int detach_inform(struct i2c_client *client)
316{
317 dprintk(1, "i2c detach [client=%s]\n", client->name);
318
319 return 0;
320}
321
322void au0828_call_i2c_clients(struct au0828_dev *dev,
323 unsigned int cmd, void *arg)
324{
325 if (dev->i2c_rc != 0)
326 return;
327
328 i2c_clients_command(&dev->i2c_adap, cmd, arg);
329}
330
331static u32 au0828_functionality(struct i2c_adapter *adap)
332{
333 return I2C_FUNC_SMBUS_EMUL | I2C_FUNC_I2C;
334}
335
336static struct i2c_algorithm au0828_i2c_algo_template = {
337 .master_xfer = i2c_xfer,
338 .functionality = au0828_functionality,
339};
340
341/* ----------------------------------------------------------------------- */
342
343static struct i2c_adapter au0828_i2c_adap_template = {
344 .name = DRIVER_NAME,
345 .owner = THIS_MODULE,
346 .id = I2C_HW_B_AU0828,
347 .algo = &au0828_i2c_algo_template,
Steven Toth265a6512008-04-18 21:34:00 -0300348 .client_register = attach_inform,
349 .client_unregister = detach_inform,
350};
351
352static struct i2c_client au0828_i2c_client_template = {
353 .name = "au0828 internal",
354};
355
356static char *i2c_devs[128] = {
Mauro Carvalho Chehab18d73c52008-04-18 22:12:52 -0300357 [0x8e >> 1] = "au8522",
358 [0xa0 >> 1] = "eeprom",
359 [0xc2 >> 1] = "tuner/xc5000",
Steven Toth265a6512008-04-18 21:34:00 -0300360};
361
362static void do_i2c_scan(char *name, struct i2c_client *c)
363{
364 unsigned char buf;
365 int i, rc;
366
367 for (i = 0; i < 128; i++) {
368 c->addr = i;
369 rc = i2c_master_recv(c, &buf, 0);
370 if (rc < 0)
371 continue;
Mauro Carvalho Chehab18d73c52008-04-18 22:12:52 -0300372 printk(KERN_INFO "%s: i2c scan: found device @ 0x%x [%s]\n",
Steven Toth265a6512008-04-18 21:34:00 -0300373 name, i << 1, i2c_devs[i] ? i2c_devs[i] : "???");
374 }
375}
376
377/* init + register i2c algo-bit adapter */
378int au0828_i2c_register(struct au0828_dev *dev)
379{
Michael Krufkyf07e8e42008-04-18 21:42:30 -0300380 dprintk(1, "%s()\n", __func__);
Steven Toth265a6512008-04-18 21:34:00 -0300381
382 memcpy(&dev->i2c_adap, &au0828_i2c_adap_template,
383 sizeof(dev->i2c_adap));
384 memcpy(&dev->i2c_algo, &au0828_i2c_algo_template,
385 sizeof(dev->i2c_algo));
386 memcpy(&dev->i2c_client, &au0828_i2c_client_template,
387 sizeof(dev->i2c_client));
388
389 dev->i2c_adap.dev.parent = &dev->usbdev->dev;
390
391 strlcpy(dev->i2c_adap.name, DRIVER_NAME,
392 sizeof(dev->i2c_adap.name));
393
Devin Heitmuellerb14667f2009-03-11 03:01:04 -0300394 dev->i2c_adap.algo = &dev->i2c_algo;
Steven Toth265a6512008-04-18 21:34:00 -0300395 dev->i2c_adap.algo_data = dev;
Devin Heitmuellerb14667f2009-03-11 03:01:04 -0300396 i2c_set_adapdata(&dev->i2c_adap, &dev->v4l2_dev);
Steven Toth265a6512008-04-18 21:34:00 -0300397 i2c_add_adapter(&dev->i2c_adap);
398
399 dev->i2c_client.adapter = &dev->i2c_adap;
400
401 if (0 == dev->i2c_rc) {
Mauro Carvalho Chehab18d73c52008-04-18 22:12:52 -0300402 printk(KERN_INFO "%s: i2c bus registered\n", DRIVER_NAME);
Steven Toth265a6512008-04-18 21:34:00 -0300403 if (i2c_scan)
404 do_i2c_scan(DRIVER_NAME, &dev->i2c_client);
405 } else
Mauro Carvalho Chehab18d73c52008-04-18 22:12:52 -0300406 printk(KERN_INFO "%s: i2c bus register FAILED\n", DRIVER_NAME);
Steven Tothbc3c6132008-04-18 21:39:11 -0300407
Steven Toth265a6512008-04-18 21:34:00 -0300408 return dev->i2c_rc;
409}
410
411int au0828_i2c_unregister(struct au0828_dev *dev)
412{
413 i2c_del_adapter(&dev->i2c_adap);
414 return 0;
415}
416