1 /*
2  *  Driver for the Auvitek AU0828 USB bridge
3  *
4  *  Copyright (c) 2008 Steven Toth <stoth@linuxtv.org>
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 
18 #include "au0828.h"
19 
20 #include <linux/module.h>
21 #include <linux/moduleparam.h>
22 #include <linux/init.h>
23 #include <linux/delay.h>
24 #include <linux/io.h>
25 
26 #include "media/tuner.h"
27 #include <media/v4l2-common.h>
28 
29 static int i2c_scan;
30 module_param(i2c_scan, int, 0444);
31 MODULE_PARM_DESC(i2c_scan, "scan i2c bus at insmod time");
32 
33 #define I2C_WAIT_DELAY 25
34 #define I2C_WAIT_RETRY 1000
35 
36 static inline int i2c_slave_did_write_ack(struct i2c_adapter *i2c_adap)
37 {
38 	struct au0828_dev *dev = i2c_adap->algo_data;
39 	return au0828_read(dev, AU0828_I2C_STATUS_201) &
40 		AU0828_I2C_STATUS_NO_WRITE_ACK ? 0 : 1;
41 }
42 
43 static inline int i2c_slave_did_read_ack(struct i2c_adapter *i2c_adap)
44 {
45 	struct au0828_dev *dev = i2c_adap->algo_data;
46 	return au0828_read(dev, AU0828_I2C_STATUS_201) &
47 		AU0828_I2C_STATUS_NO_READ_ACK ? 0 : 1;
48 }
49 
50 static int i2c_wait_read_ack(struct i2c_adapter *i2c_adap)
51 {
52 	int count;
53 
54 	for (count = 0; count < I2C_WAIT_RETRY; count++) {
55 		if (!i2c_slave_did_read_ack(i2c_adap))
56 			break;
57 		udelay(I2C_WAIT_DELAY);
58 	}
59 
60 	if (I2C_WAIT_RETRY == count)
61 		return 0;
62 
63 	return 1;
64 }
65 
66 static inline int i2c_is_read_busy(struct i2c_adapter *i2c_adap)
67 {
68 	struct au0828_dev *dev = i2c_adap->algo_data;
69 	return au0828_read(dev, AU0828_I2C_STATUS_201) &
70 		AU0828_I2C_STATUS_READ_DONE ? 0 : 1;
71 }
72 
73 static 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 
89 static 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, AU0828_I2C_STATUS_201) &
93 		AU0828_I2C_STATUS_WRITE_DONE ? 1 : 0;
94 }
95 
96 static int i2c_wait_write_done(struct i2c_adapter *i2c_adap)
97 {
98 	int count;
99 
100 	for (count = 0; count < I2C_WAIT_RETRY; count++) {
101 		if (i2c_is_write_done(i2c_adap))
102 			break;
103 		udelay(I2C_WAIT_DELAY);
104 	}
105 
106 	if (I2C_WAIT_RETRY == count)
107 		return 0;
108 
109 	return 1;
110 }
111 
112 static inline int i2c_is_busy(struct i2c_adapter *i2c_adap)
113 {
114 	struct au0828_dev *dev = i2c_adap->algo_data;
115 	return au0828_read(dev, AU0828_I2C_STATUS_201) &
116 		AU0828_I2C_STATUS_BUSY ? 1 : 0;
117 }
118 
119 static int i2c_wait_done(struct i2c_adapter *i2c_adap)
120 {
121 	int count;
122 
123 	for (count = 0; count < I2C_WAIT_RETRY; count++) {
124 		if (!i2c_is_busy(i2c_adap))
125 			break;
126 		udelay(I2C_WAIT_DELAY);
127 	}
128 
129 	if (I2C_WAIT_RETRY == count)
130 		return 0;
131 
132 	return 1;
133 }
134 
135 /* FIXME: Implement join handling correctly */
136 static int i2c_sendbytes(struct i2c_adapter *i2c_adap,
137 	const struct i2c_msg *msg, int joined_rlen)
138 {
139 	int i, strobe = 0;
140 	struct au0828_dev *dev = i2c_adap->algo_data;
141 	u8 i2c_speed = dev->board.i2c_clk_divider;
142 
143 	dprintk(4, "%s()\n", __func__);
144 
145 	au0828_write(dev, AU0828_I2C_MULTIBYTE_MODE_2FF, 0x01);
146 
147 	if (((dev->board.tuner_type == TUNER_XC5000) ||
148 	     (dev->board.tuner_type == TUNER_XC5000C)) &&
149 	    (dev->board.tuner_addr == msg->addr)) {
150 		/*
151 		 * Due to I2C clock stretch, we need to use a lower speed
152 		 * on xc5000 for commands. However, firmware transfer can
153 		 * speed up to 400 KHz.
154 		 */
155 		if (msg->len == 64)
156 			i2c_speed = AU0828_I2C_CLK_250KHZ;
157 		else
158 			i2c_speed = AU0828_I2C_CLK_20KHZ;
159 	}
160 	/* Set the I2C clock */
161 	au0828_write(dev, AU0828_I2C_CLK_DIVIDER_202, i2c_speed);
162 
163 	/* Hardware needs 8 bit addresses */
164 	au0828_write(dev, AU0828_I2C_DEST_ADDR_203, msg->addr << 1);
165 
166 	dprintk(4, "SEND: %02x\n", msg->addr);
167 
168 	/* 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 */
176 		au0828_write(dev, AU0828_I2C_TRIGGER_200,
177 			     AU0828_I2C_TRIGGER_READ);
178 
179 		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 
188 	for (i = 0; i < msg->len;) {
189 
190 		dprintk(4, " %02x\n", msg->buf[i]);
191 
192 		au0828_write(dev, AU0828_I2C_WRITE_FIFO_205, msg->buf[i]);
193 
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)
201 				au0828_write(dev, AU0828_I2C_TRIGGER_200,
202 					     AU0828_I2C_TRIGGER_WRITE |
203 					     AU0828_I2C_TRIGGER_HOLD);
204 			else
205 				au0828_write(dev, AU0828_I2C_TRIGGER_200,
206 					     AU0828_I2C_TRIGGER_WRITE);
207 
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 
220 	dprintk(4, "\n");
221 
222 	return msg->len;
223 }
224 
225 /* FIXME: Implement join handling correctly */
226 static 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 	u8 i2c_speed = dev->board.i2c_clk_divider;
231 	int i;
232 
233 	dprintk(4, "%s()\n", __func__);
234 
235 	au0828_write(dev, AU0828_I2C_MULTIBYTE_MODE_2FF, 0x01);
236 
237 	/*
238 	 * Due to xc5000c clock stretch, we cannot use full speed at
239 	 * readings from xc5000, as otherwise they'll fail.
240 	 */
241 	if (((dev->board.tuner_type == TUNER_XC5000) ||
242 	     (dev->board.tuner_type == TUNER_XC5000C)) &&
243 	    (dev->board.tuner_addr == msg->addr))
244 		i2c_speed = AU0828_I2C_CLK_20KHZ;
245 
246 	/* Set the I2C clock */
247 	au0828_write(dev, AU0828_I2C_CLK_DIVIDER_202, i2c_speed);
248 
249 	/* Hardware needs 8 bit addresses */
250 	au0828_write(dev, AU0828_I2C_DEST_ADDR_203, msg->addr << 1);
251 
252 	dprintk(4, " RECV:\n");
253 
254 	/* Deal with i2c_scan */
255 	if (msg->len == 0) {
256 		au0828_write(dev, AU0828_I2C_TRIGGER_200,
257 			     AU0828_I2C_TRIGGER_READ);
258 
259 		if (i2c_wait_read_ack(i2c_adap))
260 			return -EIO;
261 		return 0;
262 	}
263 
264 	for (i = 0; i < msg->len;) {
265 
266 		i++;
267 
268 		if (i < msg->len)
269 			au0828_write(dev, AU0828_I2C_TRIGGER_200,
270 				     AU0828_I2C_TRIGGER_READ |
271 				     AU0828_I2C_TRIGGER_HOLD);
272 		else
273 			au0828_write(dev, AU0828_I2C_TRIGGER_200,
274 				     AU0828_I2C_TRIGGER_READ);
275 
276 		if (!i2c_wait_read_done(i2c_adap))
277 			return -EIO;
278 
279 		msg->buf[i-1] = au0828_read(dev, AU0828_I2C_READ_FIFO_209) &
280 			0xff;
281 
282 		dprintk(4, " %02x\n", msg->buf[i-1]);
283 	}
284 	if (!i2c_wait_done(i2c_adap))
285 		return -EIO;
286 
287 	dprintk(4, "\n");
288 
289 	return msg->len;
290 }
291 
292 static int i2c_xfer(struct i2c_adapter *i2c_adap,
293 		    struct i2c_msg *msgs, int num)
294 {
295 	int i, retval = 0;
296 
297 	dprintk(4, "%s(num = %d)\n", __func__, num);
298 
299 	for (i = 0; i < num; i++) {
300 		dprintk(4, "%s(num = %d) addr = 0x%02x  len = 0x%x\n",
301 			__func__, num, msgs[i].addr, msgs[i].len);
302 		if (msgs[i].flags & I2C_M_RD) {
303 			/* read */
304 			retval = i2c_readbytes(i2c_adap, &msgs[i], 0);
305 		} else if (i + 1 < num && (msgs[i + 1].flags & I2C_M_RD) &&
306 			   msgs[i].addr == msgs[i + 1].addr) {
307 			/* write then read from same address */
308 			retval = i2c_sendbytes(i2c_adap, &msgs[i],
309 					       msgs[i + 1].len);
310 			if (retval < 0)
311 				goto err;
312 			i++;
313 			retval = i2c_readbytes(i2c_adap, &msgs[i], 1);
314 		} else {
315 			/* write */
316 			retval = i2c_sendbytes(i2c_adap, &msgs[i], 0);
317 		}
318 		if (retval < 0)
319 			goto err;
320 	}
321 	return num;
322 
323 err:
324 	return retval;
325 }
326 
327 static u32 au0828_functionality(struct i2c_adapter *adap)
328 {
329 	return I2C_FUNC_SMBUS_EMUL | I2C_FUNC_I2C;
330 }
331 
332 static struct i2c_algorithm au0828_i2c_algo_template = {
333 	.master_xfer	= i2c_xfer,
334 	.functionality	= au0828_functionality,
335 };
336 
337 /* ----------------------------------------------------------------------- */
338 
339 static struct i2c_adapter au0828_i2c_adap_template = {
340 	.name              = KBUILD_MODNAME,
341 	.owner             = THIS_MODULE,
342 	.algo              = &au0828_i2c_algo_template,
343 };
344 
345 static struct i2c_client au0828_i2c_client_template = {
346 	.name	= "au0828 internal",
347 };
348 
349 static char *i2c_devs[128] = {
350 	[0x8e >> 1] = "au8522",
351 	[0xa0 >> 1] = "eeprom",
352 	[0xc2 >> 1] = "tuner/xc5000",
353 };
354 
355 static void do_i2c_scan(char *name, struct i2c_client *c)
356 {
357 	unsigned char buf;
358 	int i, rc;
359 
360 	for (i = 0; i < 128; i++) {
361 		c->addr = i;
362 		rc = i2c_master_recv(c, &buf, 0);
363 		if (rc < 0)
364 			continue;
365 		pr_info("%s: i2c scan: found device @ 0x%x  [%s]\n",
366 		       name, i << 1, i2c_devs[i] ? i2c_devs[i] : "???");
367 	}
368 }
369 
370 /* init + register i2c adapter */
371 int au0828_i2c_register(struct au0828_dev *dev)
372 {
373 	dprintk(1, "%s()\n", __func__);
374 
375 	dev->i2c_adap = au0828_i2c_adap_template;
376 	dev->i2c_algo = au0828_i2c_algo_template;
377 	dev->i2c_client = au0828_i2c_client_template;
378 
379 	dev->i2c_adap.dev.parent = &dev->usbdev->dev;
380 
381 	strlcpy(dev->i2c_adap.name, KBUILD_MODNAME,
382 		sizeof(dev->i2c_adap.name));
383 
384 	dev->i2c_adap.algo = &dev->i2c_algo;
385 	dev->i2c_adap.algo_data = dev;
386 #ifdef CONFIG_VIDEO_AU0828_V4L2
387 	i2c_set_adapdata(&dev->i2c_adap, &dev->v4l2_dev);
388 #else
389 	i2c_set_adapdata(&dev->i2c_adap, dev);
390 #endif
391 	i2c_add_adapter(&dev->i2c_adap);
392 
393 	dev->i2c_client.adapter = &dev->i2c_adap;
394 
395 	if (0 == dev->i2c_rc) {
396 		pr_info("i2c bus registered\n");
397 		if (i2c_scan)
398 			do_i2c_scan(KBUILD_MODNAME, &dev->i2c_client);
399 	} else
400 		pr_info("i2c bus register FAILED\n");
401 
402 	return dev->i2c_rc;
403 }
404 
405 int au0828_i2c_unregister(struct au0828_dev *dev)
406 {
407 	i2c_del_adapter(&dev->i2c_adap);
408 	return 0;
409 }
410 
411