blob: d4545550e0dde25a196e4c533e1f20d9c80ae485 [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"
Devin Heitmuellerfaaf01b2012-08-06 22:47:04 -030029#include "media/tuner.h"
Steven Toth265a6512008-04-18 21:34:00 -030030#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
Devin Heitmueller16af6f52009-04-01 00:11:31 -0300149 /* Set the I2C clock */
Devin Heitmuellerfaaf01b2012-08-06 22:47:04 -0300150 if ((dev->board.tuner_type == TUNER_XC5000) &&
151 (dev->board.tuner_addr == msg->addr) &&
152 (msg->len == 64)) {
153 /* Hack to speed up firmware load. The xc5000 lets us do up
154 to 400 KHz when in firmware download mode */
155 au0828_write(dev, AU0828_I2C_CLK_DIVIDER_202,
156 AU0828_I2C_CLK_250KHZ);
157 } else {
158 /* Use the i2c clock speed in the board configuration */
159 au0828_write(dev, AU0828_I2C_CLK_DIVIDER_202,
160 dev->board.i2c_clk_divider);
161 }
Steven Toth265a6512008-04-18 21:34:00 -0300162
163 /* Hardware needs 8 bit addresses */
Devin Heitmueller9beb0de2009-03-31 23:58:49 -0300164 au0828_write(dev, AU0828_I2C_DEST_ADDR_203, msg->addr << 1);
Steven Toth265a6512008-04-18 21:34:00 -0300165
Steven Tothbc3c6132008-04-18 21:39:11 -0300166 dprintk(4, "SEND: %02x\n", msg->addr);
Steven Toth265a6512008-04-18 21:34:00 -0300167
Devin Heitmuellerdc8685b2009-03-11 03:00:56 -0300168 /* Deal with i2c_scan */
169 if (msg->len == 0) {
170 /* The analog tuner detection code makes use of the SMBUS_QUICK
171 message (which involves a zero length i2c write). To avoid
172 checking the status register when we didn't strobe out any
173 actual bytes to the bus, just do a read check. This is
174 consistent with how I saw i2c device checking done in the
175 USB trace of the Windows driver */
Devin Heitmueller9beb0de2009-03-31 23:58:49 -0300176 au0828_write(dev, AU0828_I2C_TRIGGER_200,
177 AU0828_I2C_TRIGGER_READ);
178
Devin Heitmuellerdc8685b2009-03-11 03:00:56 -0300179 if (!i2c_wait_done(i2c_adap))
180 return -EIO;
181
182 if (i2c_wait_read_ack(i2c_adap))
183 return -EIO;
184
185 return 0;
186 }
187
Steven Totha9c36aa2008-04-17 21:41:28 -0300188 for (i = 0; i < msg->len;) {
Steven Toth265a6512008-04-18 21:34:00 -0300189
Steven Tothbc3c6132008-04-18 21:39:11 -0300190 dprintk(4, " %02x\n", msg->buf[i]);
Steven Toth265a6512008-04-18 21:34:00 -0300191
Devin Heitmueller9beb0de2009-03-31 23:58:49 -0300192 au0828_write(dev, AU0828_I2C_WRITE_FIFO_205, msg->buf[i]);
Steven Toth265a6512008-04-18 21:34:00 -0300193
194 strobe++;
195 i++;
196
197 if ((strobe >= 4) || (i >= msg->len)) {
198
199 /* Strobe the byte into the bus */
200 if (i < msg->len)
Devin Heitmueller9beb0de2009-03-31 23:58:49 -0300201 au0828_write(dev, AU0828_I2C_TRIGGER_200,
202 AU0828_I2C_TRIGGER_WRITE |
203 AU0828_I2C_TRIGGER_HOLD);
Steven Toth265a6512008-04-18 21:34:00 -0300204 else
Devin Heitmueller9beb0de2009-03-31 23:58:49 -0300205 au0828_write(dev, AU0828_I2C_TRIGGER_200,
206 AU0828_I2C_TRIGGER_WRITE);
Steven Toth265a6512008-04-18 21:34:00 -0300207
208 /* Reset strobe trigger */
209 strobe = 0;
210
211 if (!i2c_wait_write_done(i2c_adap))
212 return -EIO;
213
214 }
215
216 }
217 if (!i2c_wait_done(i2c_adap))
218 return -EIO;
219
Steven Tothbc3c6132008-04-18 21:39:11 -0300220 dprintk(4, "\n");
Steven Toth265a6512008-04-18 21:34:00 -0300221
222 return msg->len;
223}
224
225/* FIXME: Implement join handling correctly */
226static int i2c_readbytes(struct i2c_adapter *i2c_adap,
227 const struct i2c_msg *msg, int joined)
228{
229 struct au0828_dev *dev = i2c_adap->algo_data;
230 int i;
231
Michael Krufkyf07e8e42008-04-18 21:42:30 -0300232 dprintk(4, "%s()\n", __func__);
Steven Toth265a6512008-04-18 21:34:00 -0300233
Devin Heitmueller9beb0de2009-03-31 23:58:49 -0300234 au0828_write(dev, AU0828_I2C_MULTIBYTE_MODE_2FF, 0x01);
Devin Heitmueller32c000a2009-03-11 03:00:41 -0300235
Devin Heitmueller16af6f52009-04-01 00:11:31 -0300236 /* Set the I2C clock */
237 au0828_write(dev, AU0828_I2C_CLK_DIVIDER_202,
238 dev->board.i2c_clk_divider);
Steven Toth265a6512008-04-18 21:34:00 -0300239
240 /* Hardware needs 8 bit addresses */
Devin Heitmueller9beb0de2009-03-31 23:58:49 -0300241 au0828_write(dev, AU0828_I2C_DEST_ADDR_203, msg->addr << 1);
Steven Toth265a6512008-04-18 21:34:00 -0300242
Steven Tothbc3c6132008-04-18 21:39:11 -0300243 dprintk(4, " RECV:\n");
Steven Toth265a6512008-04-18 21:34:00 -0300244
245 /* Deal with i2c_scan */
246 if (msg->len == 0) {
Devin Heitmueller9beb0de2009-03-31 23:58:49 -0300247 au0828_write(dev, AU0828_I2C_TRIGGER_200,
248 AU0828_I2C_TRIGGER_READ);
249
Steven Toth265a6512008-04-18 21:34:00 -0300250 if (i2c_wait_read_ack(i2c_adap))
251 return -EIO;
252 return 0;
253 }
254
Steven Totha9c36aa2008-04-17 21:41:28 -0300255 for (i = 0; i < msg->len;) {
Steven Toth265a6512008-04-18 21:34:00 -0300256
257 i++;
258
259 if (i < msg->len)
Devin Heitmueller9beb0de2009-03-31 23:58:49 -0300260 au0828_write(dev, AU0828_I2C_TRIGGER_200,
261 AU0828_I2C_TRIGGER_READ |
262 AU0828_I2C_TRIGGER_HOLD);
Steven Toth265a6512008-04-18 21:34:00 -0300263 else
Devin Heitmueller9beb0de2009-03-31 23:58:49 -0300264 au0828_write(dev, AU0828_I2C_TRIGGER_200,
265 AU0828_I2C_TRIGGER_READ);
Steven Toth265a6512008-04-18 21:34:00 -0300266
267 if (!i2c_wait_read_done(i2c_adap))
268 return -EIO;
269
Devin Heitmueller9beb0de2009-03-31 23:58:49 -0300270 msg->buf[i-1] = au0828_read(dev, AU0828_I2C_READ_FIFO_209) &
271 0xff;
Steven Toth265a6512008-04-18 21:34:00 -0300272
Steven Tothbc3c6132008-04-18 21:39:11 -0300273 dprintk(4, " %02x\n", msg->buf[i-1]);
Steven Toth265a6512008-04-18 21:34:00 -0300274 }
275 if (!i2c_wait_done(i2c_adap))
276 return -EIO;
277
Steven Tothbc3c6132008-04-18 21:39:11 -0300278 dprintk(4, "\n");
Steven Toth265a6512008-04-18 21:34:00 -0300279
280 return msg->len;
281}
282
283static int i2c_xfer(struct i2c_adapter *i2c_adap,
284 struct i2c_msg *msgs, int num)
285{
286 int i, retval = 0;
287
Michael Krufkyf07e8e42008-04-18 21:42:30 -0300288 dprintk(4, "%s(num = %d)\n", __func__, num);
Steven Toth265a6512008-04-18 21:34:00 -0300289
Steven Totha9c36aa2008-04-17 21:41:28 -0300290 for (i = 0; i < num; i++) {
Steven Tothbc3c6132008-04-18 21:39:11 -0300291 dprintk(4, "%s(num = %d) addr = 0x%02x len = 0x%x\n",
Michael Krufkyf07e8e42008-04-18 21:42:30 -0300292 __func__, num, msgs[i].addr, msgs[i].len);
Steven Toth265a6512008-04-18 21:34:00 -0300293 if (msgs[i].flags & I2C_M_RD) {
294 /* read */
295 retval = i2c_readbytes(i2c_adap, &msgs[i], 0);
296 } else if (i + 1 < num && (msgs[i + 1].flags & I2C_M_RD) &&
297 msgs[i].addr == msgs[i + 1].addr) {
298 /* write then read from same address */
299 retval = i2c_sendbytes(i2c_adap, &msgs[i],
300 msgs[i + 1].len);
301 if (retval < 0)
302 goto err;
303 i++;
304 retval = i2c_readbytes(i2c_adap, &msgs[i], 1);
305 } else {
306 /* write */
307 retval = i2c_sendbytes(i2c_adap, &msgs[i], 0);
308 }
309 if (retval < 0)
310 goto err;
311 }
312 return num;
313
314err:
315 return retval;
316}
317
Steven Toth265a6512008-04-18 21:34:00 -0300318static u32 au0828_functionality(struct i2c_adapter *adap)
319{
320 return I2C_FUNC_SMBUS_EMUL | I2C_FUNC_I2C;
321}
322
323static struct i2c_algorithm au0828_i2c_algo_template = {
324 .master_xfer = i2c_xfer,
325 .functionality = au0828_functionality,
326};
327
328/* ----------------------------------------------------------------------- */
329
330static struct i2c_adapter au0828_i2c_adap_template = {
331 .name = DRIVER_NAME,
332 .owner = THIS_MODULE,
Steven Toth265a6512008-04-18 21:34:00 -0300333 .algo = &au0828_i2c_algo_template,
Steven Toth265a6512008-04-18 21:34:00 -0300334};
335
336static struct i2c_client au0828_i2c_client_template = {
337 .name = "au0828 internal",
338};
339
340static char *i2c_devs[128] = {
Mauro Carvalho Chehab18d73c52008-04-18 22:12:52 -0300341 [0x8e >> 1] = "au8522",
342 [0xa0 >> 1] = "eeprom",
343 [0xc2 >> 1] = "tuner/xc5000",
Steven Toth265a6512008-04-18 21:34:00 -0300344};
345
346static void do_i2c_scan(char *name, struct i2c_client *c)
347{
348 unsigned char buf;
349 int i, rc;
350
351 for (i = 0; i < 128; i++) {
352 c->addr = i;
353 rc = i2c_master_recv(c, &buf, 0);
354 if (rc < 0)
355 continue;
Mauro Carvalho Chehab18d73c52008-04-18 22:12:52 -0300356 printk(KERN_INFO "%s: i2c scan: found device @ 0x%x [%s]\n",
Steven Toth265a6512008-04-18 21:34:00 -0300357 name, i << 1, i2c_devs[i] ? i2c_devs[i] : "???");
358 }
359}
360
Jean Delvarea824f0f2011-11-09 13:27:53 -0300361/* init + register i2c adapter */
Steven Toth265a6512008-04-18 21:34:00 -0300362int au0828_i2c_register(struct au0828_dev *dev)
363{
Michael Krufkyf07e8e42008-04-18 21:42:30 -0300364 dprintk(1, "%s()\n", __func__);
Steven Toth265a6512008-04-18 21:34:00 -0300365
366 memcpy(&dev->i2c_adap, &au0828_i2c_adap_template,
367 sizeof(dev->i2c_adap));
368 memcpy(&dev->i2c_algo, &au0828_i2c_algo_template,
369 sizeof(dev->i2c_algo));
370 memcpy(&dev->i2c_client, &au0828_i2c_client_template,
371 sizeof(dev->i2c_client));
372
373 dev->i2c_adap.dev.parent = &dev->usbdev->dev;
374
375 strlcpy(dev->i2c_adap.name, DRIVER_NAME,
376 sizeof(dev->i2c_adap.name));
377
Devin Heitmuellerb14667f2009-03-11 03:01:04 -0300378 dev->i2c_adap.algo = &dev->i2c_algo;
Steven Toth265a6512008-04-18 21:34:00 -0300379 dev->i2c_adap.algo_data = dev;
Devin Heitmuellerb14667f2009-03-11 03:01:04 -0300380 i2c_set_adapdata(&dev->i2c_adap, &dev->v4l2_dev);
Steven Toth265a6512008-04-18 21:34:00 -0300381 i2c_add_adapter(&dev->i2c_adap);
382
383 dev->i2c_client.adapter = &dev->i2c_adap;
384
385 if (0 == dev->i2c_rc) {
Mauro Carvalho Chehab18d73c52008-04-18 22:12:52 -0300386 printk(KERN_INFO "%s: i2c bus registered\n", DRIVER_NAME);
Steven Toth265a6512008-04-18 21:34:00 -0300387 if (i2c_scan)
388 do_i2c_scan(DRIVER_NAME, &dev->i2c_client);
389 } else
Mauro Carvalho Chehab18d73c52008-04-18 22:12:52 -0300390 printk(KERN_INFO "%s: i2c bus register FAILED\n", DRIVER_NAME);
Steven Tothbc3c6132008-04-18 21:39:11 -0300391
Steven Toth265a6512008-04-18 21:34:00 -0300392 return dev->i2c_rc;
393}
394
395int au0828_i2c_unregister(struct au0828_dev *dev)
396{
397 i2c_del_adapter(&dev->i2c_adap);
398 return 0;
399}
400