blob: 27dcfc69cd801ac6928f7a3e55818a97466b2469 [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;
Devin Heitmueller9beb0de2009-03-31 23:58:49 -030042 return au0828_read(dev, AU0828_I2C_STATUS_201) &
43 AU0828_I2C_STATUS_NO_WRITE_ACK ? 0 : 1;
Steven Toth265a6512008-04-18 21:34:00 -030044}
45
46static inline int i2c_slave_did_read_ack(struct i2c_adapter *i2c_adap)
47{
48 struct au0828_dev *dev = i2c_adap->algo_data;
Devin Heitmueller9beb0de2009-03-31 23:58:49 -030049 return au0828_read(dev, AU0828_I2C_STATUS_201) &
50 AU0828_I2C_STATUS_NO_READ_ACK ? 0 : 1;
Steven Toth265a6512008-04-18 21:34:00 -030051}
52
53static int i2c_wait_read_ack(struct i2c_adapter *i2c_adap)
54{
55 int count;
56
57 for (count = 0; count < I2C_WAIT_RETRY; count++) {
58 if (!i2c_slave_did_read_ack(i2c_adap))
59 break;
60 udelay(I2C_WAIT_DELAY);
61 }
62
63 if (I2C_WAIT_RETRY == count)
64 return 0;
65
66 return 1;
67}
68
69static inline int i2c_is_read_busy(struct i2c_adapter *i2c_adap)
70{
71 struct au0828_dev *dev = i2c_adap->algo_data;
Devin Heitmueller9beb0de2009-03-31 23:58:49 -030072 return au0828_read(dev, AU0828_I2C_STATUS_201) &
73 AU0828_I2C_STATUS_READ_DONE ? 0 : 1;
Steven Toth265a6512008-04-18 21:34:00 -030074}
75
76static int i2c_wait_read_done(struct i2c_adapter *i2c_adap)
77{
78 int count;
79
80 for (count = 0; count < I2C_WAIT_RETRY; count++) {
81 if (!i2c_is_read_busy(i2c_adap))
82 break;
83 udelay(I2C_WAIT_DELAY);
84 }
85
86 if (I2C_WAIT_RETRY == count)
87 return 0;
88
89 return 1;
90}
91
92static inline int i2c_is_write_done(struct i2c_adapter *i2c_adap)
93{
94 struct au0828_dev *dev = i2c_adap->algo_data;
Devin Heitmueller9beb0de2009-03-31 23:58:49 -030095 return au0828_read(dev, AU0828_I2C_STATUS_201) &
96 AU0828_I2C_STATUS_WRITE_DONE ? 1 : 0;
Steven Toth265a6512008-04-18 21:34:00 -030097}
98
99static int i2c_wait_write_done(struct i2c_adapter *i2c_adap)
100{
101 int count;
102
103 for (count = 0; count < I2C_WAIT_RETRY; count++) {
104 if (i2c_is_write_done(i2c_adap))
105 break;
106 udelay(I2C_WAIT_DELAY);
107 }
108
109 if (I2C_WAIT_RETRY == count)
110 return 0;
111
112 return 1;
113}
114
115static inline int i2c_is_busy(struct i2c_adapter *i2c_adap)
116{
117 struct au0828_dev *dev = i2c_adap->algo_data;
Devin Heitmueller9beb0de2009-03-31 23:58:49 -0300118 return au0828_read(dev, AU0828_I2C_STATUS_201) &
119 AU0828_I2C_STATUS_BUSY ? 1 : 0;
Steven Toth265a6512008-04-18 21:34:00 -0300120}
121
122static int i2c_wait_done(struct i2c_adapter *i2c_adap)
123{
124 int count;
125
126 for (count = 0; count < I2C_WAIT_RETRY; count++) {
127 if (!i2c_is_busy(i2c_adap))
128 break;
129 udelay(I2C_WAIT_DELAY);
130 }
131
132 if (I2C_WAIT_RETRY == count)
133 return 0;
134
135 return 1;
136}
137
138/* FIXME: Implement join handling correctly */
139static int i2c_sendbytes(struct i2c_adapter *i2c_adap,
140 const struct i2c_msg *msg, int joined_rlen)
141{
142 int i, strobe = 0;
143 struct au0828_dev *dev = i2c_adap->algo_data;
144
Michael Krufkyf07e8e42008-04-18 21:42:30 -0300145 dprintk(4, "%s()\n", __func__);
Steven Toth265a6512008-04-18 21:34:00 -0300146
Devin Heitmueller9beb0de2009-03-31 23:58:49 -0300147 au0828_write(dev, AU0828_I2C_MULTIBYTE_MODE_2FF, 0x01);
Devin Heitmueller32c000a2009-03-11 03:00:41 -0300148
149 /* FIXME: There is a problem with i2c communications with xc5000 that
150 requires us to slow down the i2c clock until we have a better
151 strategy (such as using the secondary i2c bus to do firmware
152 loading */
Devin Heitmueller62899a22009-03-15 18:48:52 -0300153 if ((msg->addr << 1) == 0xc2)
Devin Heitmueller9beb0de2009-03-31 23:58:49 -0300154 au0828_write(dev, AU0828_I2C_CLK_DIVIDER_202,
155 AU0828_I2C_CLK_30KHZ);
Devin Heitmueller62899a22009-03-15 18:48:52 -0300156 else
Devin Heitmueller9beb0de2009-03-31 23:58:49 -0300157 au0828_write(dev, AU0828_I2C_CLK_DIVIDER_202,
158 AU0828_I2C_CLK_250KHZ);
Steven Toth265a6512008-04-18 21:34:00 -0300159
160 /* Hardware needs 8 bit addresses */
Devin Heitmueller9beb0de2009-03-31 23:58:49 -0300161 au0828_write(dev, AU0828_I2C_DEST_ADDR_203, msg->addr << 1);
Steven Toth265a6512008-04-18 21:34:00 -0300162
Steven Tothbc3c6132008-04-18 21:39:11 -0300163 dprintk(4, "SEND: %02x\n", msg->addr);
Steven Toth265a6512008-04-18 21:34:00 -0300164
Devin Heitmuellerdc8685b2009-03-11 03:00:56 -0300165 /* Deal with i2c_scan */
166 if (msg->len == 0) {
167 /* The analog tuner detection code makes use of the SMBUS_QUICK
168 message (which involves a zero length i2c write). To avoid
169 checking the status register when we didn't strobe out any
170 actual bytes to the bus, just do a read check. This is
171 consistent with how I saw i2c device checking done in the
172 USB trace of the Windows driver */
Devin Heitmueller9beb0de2009-03-31 23:58:49 -0300173 au0828_write(dev, AU0828_I2C_TRIGGER_200,
174 AU0828_I2C_TRIGGER_READ);
175
Devin Heitmuellerdc8685b2009-03-11 03:00:56 -0300176 if (!i2c_wait_done(i2c_adap))
177 return -EIO;
178
179 if (i2c_wait_read_ack(i2c_adap))
180 return -EIO;
181
182 return 0;
183 }
184
Steven Totha9c36aa2008-04-17 21:41:28 -0300185 for (i = 0; i < msg->len;) {
Steven Toth265a6512008-04-18 21:34:00 -0300186
Steven Tothbc3c6132008-04-18 21:39:11 -0300187 dprintk(4, " %02x\n", msg->buf[i]);
Steven Toth265a6512008-04-18 21:34:00 -0300188
Devin Heitmueller9beb0de2009-03-31 23:58:49 -0300189 au0828_write(dev, AU0828_I2C_WRITE_FIFO_205, msg->buf[i]);
Steven Toth265a6512008-04-18 21:34:00 -0300190
191 strobe++;
192 i++;
193
194 if ((strobe >= 4) || (i >= msg->len)) {
195
196 /* Strobe the byte into the bus */
197 if (i < msg->len)
Devin Heitmueller9beb0de2009-03-31 23:58:49 -0300198 au0828_write(dev, AU0828_I2C_TRIGGER_200,
199 AU0828_I2C_TRIGGER_WRITE |
200 AU0828_I2C_TRIGGER_HOLD);
Steven Toth265a6512008-04-18 21:34:00 -0300201 else
Devin Heitmueller9beb0de2009-03-31 23:58:49 -0300202 au0828_write(dev, AU0828_I2C_TRIGGER_200,
203 AU0828_I2C_TRIGGER_WRITE);
Steven Toth265a6512008-04-18 21:34:00 -0300204
205 /* Reset strobe trigger */
206 strobe = 0;
207
208 if (!i2c_wait_write_done(i2c_adap))
209 return -EIO;
210
211 }
212
213 }
214 if (!i2c_wait_done(i2c_adap))
215 return -EIO;
216
Steven Tothbc3c6132008-04-18 21:39:11 -0300217 dprintk(4, "\n");
Steven Toth265a6512008-04-18 21:34:00 -0300218
219 return msg->len;
220}
221
222/* FIXME: Implement join handling correctly */
223static int i2c_readbytes(struct i2c_adapter *i2c_adap,
224 const struct i2c_msg *msg, int joined)
225{
226 struct au0828_dev *dev = i2c_adap->algo_data;
227 int i;
228
Michael Krufkyf07e8e42008-04-18 21:42:30 -0300229 dprintk(4, "%s()\n", __func__);
Steven Toth265a6512008-04-18 21:34:00 -0300230
Devin Heitmueller9beb0de2009-03-31 23:58:49 -0300231 au0828_write(dev, AU0828_I2C_MULTIBYTE_MODE_2FF, 0x01);
Devin Heitmueller32c000a2009-03-11 03:00:41 -0300232
233 /* FIXME: There is a problem with i2c communications with xc5000 that
234 requires us to slow down the i2c clock until we have a better
235 strategy (such as using the secondary i2c bus to do firmware
236 loading */
Devin Heitmueller62899a22009-03-15 18:48:52 -0300237 if ((msg->addr << 1) == 0xc2)
Devin Heitmueller9beb0de2009-03-31 23:58:49 -0300238 au0828_write(dev, AU0828_I2C_CLK_DIVIDER_202,
239 AU0828_I2C_CLK_30KHZ);
Devin Heitmueller62899a22009-03-15 18:48:52 -0300240 else
Devin Heitmueller9beb0de2009-03-31 23:58:49 -0300241 au0828_write(dev, AU0828_I2C_CLK_DIVIDER_202,
242 AU0828_I2C_CLK_250KHZ);
Steven Toth265a6512008-04-18 21:34:00 -0300243
244 /* Hardware needs 8 bit addresses */
Devin Heitmueller9beb0de2009-03-31 23:58:49 -0300245 au0828_write(dev, AU0828_I2C_DEST_ADDR_203, msg->addr << 1);
Steven Toth265a6512008-04-18 21:34:00 -0300246
Steven Tothbc3c6132008-04-18 21:39:11 -0300247 dprintk(4, " RECV:\n");
Steven Toth265a6512008-04-18 21:34:00 -0300248
249 /* Deal with i2c_scan */
250 if (msg->len == 0) {
Devin Heitmueller9beb0de2009-03-31 23:58:49 -0300251 au0828_write(dev, AU0828_I2C_TRIGGER_200,
252 AU0828_I2C_TRIGGER_READ);
253
Steven Toth265a6512008-04-18 21:34:00 -0300254 if (i2c_wait_read_ack(i2c_adap))
255 return -EIO;
256 return 0;
257 }
258
Steven Totha9c36aa2008-04-17 21:41:28 -0300259 for (i = 0; i < msg->len;) {
Steven Toth265a6512008-04-18 21:34:00 -0300260
261 i++;
262
263 if (i < msg->len)
Devin Heitmueller9beb0de2009-03-31 23:58:49 -0300264 au0828_write(dev, AU0828_I2C_TRIGGER_200,
265 AU0828_I2C_TRIGGER_READ |
266 AU0828_I2C_TRIGGER_HOLD);
Steven Toth265a6512008-04-18 21:34:00 -0300267 else
Devin Heitmueller9beb0de2009-03-31 23:58:49 -0300268 au0828_write(dev, AU0828_I2C_TRIGGER_200,
269 AU0828_I2C_TRIGGER_READ);
Steven Toth265a6512008-04-18 21:34:00 -0300270
271 if (!i2c_wait_read_done(i2c_adap))
272 return -EIO;
273
Devin Heitmueller9beb0de2009-03-31 23:58:49 -0300274 msg->buf[i-1] = au0828_read(dev, AU0828_I2C_READ_FIFO_209) &
275 0xff;
Steven Toth265a6512008-04-18 21:34:00 -0300276
Steven Tothbc3c6132008-04-18 21:39:11 -0300277 dprintk(4, " %02x\n", msg->buf[i-1]);
Steven Toth265a6512008-04-18 21:34:00 -0300278 }
279 if (!i2c_wait_done(i2c_adap))
280 return -EIO;
281
Steven Tothbc3c6132008-04-18 21:39:11 -0300282 dprintk(4, "\n");
Steven Toth265a6512008-04-18 21:34:00 -0300283
284 return msg->len;
285}
286
287static int i2c_xfer(struct i2c_adapter *i2c_adap,
288 struct i2c_msg *msgs, int num)
289{
290 int i, retval = 0;
291
Michael Krufkyf07e8e42008-04-18 21:42:30 -0300292 dprintk(4, "%s(num = %d)\n", __func__, num);
Steven Toth265a6512008-04-18 21:34:00 -0300293
Steven Totha9c36aa2008-04-17 21:41:28 -0300294 for (i = 0; i < num; i++) {
Steven Tothbc3c6132008-04-18 21:39:11 -0300295 dprintk(4, "%s(num = %d) addr = 0x%02x len = 0x%x\n",
Michael Krufkyf07e8e42008-04-18 21:42:30 -0300296 __func__, num, msgs[i].addr, msgs[i].len);
Steven Toth265a6512008-04-18 21:34:00 -0300297 if (msgs[i].flags & I2C_M_RD) {
298 /* read */
299 retval = i2c_readbytes(i2c_adap, &msgs[i], 0);
300 } else if (i + 1 < num && (msgs[i + 1].flags & I2C_M_RD) &&
301 msgs[i].addr == msgs[i + 1].addr) {
302 /* write then read from same address */
303 retval = i2c_sendbytes(i2c_adap, &msgs[i],
304 msgs[i + 1].len);
305 if (retval < 0)
306 goto err;
307 i++;
308 retval = i2c_readbytes(i2c_adap, &msgs[i], 1);
309 } else {
310 /* write */
311 retval = i2c_sendbytes(i2c_adap, &msgs[i], 0);
312 }
313 if (retval < 0)
314 goto err;
315 }
316 return num;
317
318err:
319 return retval;
320}
321
Steven Toth265a6512008-04-18 21:34:00 -0300322static u32 au0828_functionality(struct i2c_adapter *adap)
323{
324 return I2C_FUNC_SMBUS_EMUL | I2C_FUNC_I2C;
325}
326
327static struct i2c_algorithm au0828_i2c_algo_template = {
328 .master_xfer = i2c_xfer,
329 .functionality = au0828_functionality,
330};
331
332/* ----------------------------------------------------------------------- */
333
334static struct i2c_adapter au0828_i2c_adap_template = {
335 .name = DRIVER_NAME,
336 .owner = THIS_MODULE,
337 .id = I2C_HW_B_AU0828,
338 .algo = &au0828_i2c_algo_template,
Steven Toth265a6512008-04-18 21:34:00 -0300339};
340
341static struct i2c_client au0828_i2c_client_template = {
342 .name = "au0828 internal",
343};
344
345static char *i2c_devs[128] = {
Mauro Carvalho Chehab18d73c52008-04-18 22:12:52 -0300346 [0x8e >> 1] = "au8522",
347 [0xa0 >> 1] = "eeprom",
348 [0xc2 >> 1] = "tuner/xc5000",
Steven Toth265a6512008-04-18 21:34:00 -0300349};
350
351static void do_i2c_scan(char *name, struct i2c_client *c)
352{
353 unsigned char buf;
354 int i, rc;
355
356 for (i = 0; i < 128; i++) {
357 c->addr = i;
358 rc = i2c_master_recv(c, &buf, 0);
359 if (rc < 0)
360 continue;
Mauro Carvalho Chehab18d73c52008-04-18 22:12:52 -0300361 printk(KERN_INFO "%s: i2c scan: found device @ 0x%x [%s]\n",
Steven Toth265a6512008-04-18 21:34:00 -0300362 name, i << 1, i2c_devs[i] ? i2c_devs[i] : "???");
363 }
364}
365
366/* init + register i2c algo-bit adapter */
367int au0828_i2c_register(struct au0828_dev *dev)
368{
Michael Krufkyf07e8e42008-04-18 21:42:30 -0300369 dprintk(1, "%s()\n", __func__);
Steven Toth265a6512008-04-18 21:34:00 -0300370
371 memcpy(&dev->i2c_adap, &au0828_i2c_adap_template,
372 sizeof(dev->i2c_adap));
373 memcpy(&dev->i2c_algo, &au0828_i2c_algo_template,
374 sizeof(dev->i2c_algo));
375 memcpy(&dev->i2c_client, &au0828_i2c_client_template,
376 sizeof(dev->i2c_client));
377
378 dev->i2c_adap.dev.parent = &dev->usbdev->dev;
379
380 strlcpy(dev->i2c_adap.name, DRIVER_NAME,
381 sizeof(dev->i2c_adap.name));
382
Devin Heitmuellerb14667f2009-03-11 03:01:04 -0300383 dev->i2c_adap.algo = &dev->i2c_algo;
Steven Toth265a6512008-04-18 21:34:00 -0300384 dev->i2c_adap.algo_data = dev;
Devin Heitmuellerb14667f2009-03-11 03:01:04 -0300385 i2c_set_adapdata(&dev->i2c_adap, &dev->v4l2_dev);
Steven Toth265a6512008-04-18 21:34:00 -0300386 i2c_add_adapter(&dev->i2c_adap);
387
388 dev->i2c_client.adapter = &dev->i2c_adap;
389
390 if (0 == dev->i2c_rc) {
Mauro Carvalho Chehab18d73c52008-04-18 22:12:52 -0300391 printk(KERN_INFO "%s: i2c bus registered\n", DRIVER_NAME);
Steven Toth265a6512008-04-18 21:34:00 -0300392 if (i2c_scan)
393 do_i2c_scan(DRIVER_NAME, &dev->i2c_client);
394 } else
Mauro Carvalho Chehab18d73c52008-04-18 22:12:52 -0300395 printk(KERN_INFO "%s: i2c bus register FAILED\n", DRIVER_NAME);
Steven Tothbc3c6132008-04-18 21:39:11 -0300396
Steven Toth265a6512008-04-18 21:34:00 -0300397 return dev->i2c_rc;
398}
399
400int au0828_i2c_unregister(struct au0828_dev *dev)
401{
402 i2c_del_adapter(&dev->i2c_adap);
403 return 0;
404}
405