1/*
2 * usbvision_i2c.c
3 *  i2c algorithm for USB-I2C Bridges
4 *
5 * Copyright (c) 1999-2007 Joerg Heckenbach <joerg@heckenbach-aw.de>
6 *                         Dwaine Garden <dwainegarden@rogers.com>
7 *
8 * This module is part of usbvision driver project.
9 * Updates to driver completed by Dwaine P. Garden
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 */
25
26
27#include <linux/kernel.h>
28#include <linux/module.h>
29#include <linux/delay.h>
30#include <linux/init.h>
31#include <linux/uaccess.h>
32#include <linux/ioport.h>
33#include <linux/errno.h>
34#include <linux/usb.h>
35#include <linux/i2c.h>
36#include "usbvision.h"
37
38#define DBG_I2C		(1 << 0)
39
40static int i2c_debug;
41
42module_param(i2c_debug, int, 0644);			/* debug_i2c_usb mode of the device driver */
43MODULE_PARM_DESC(i2c_debug, "enable debug messages [i2c]");
44
45#define PDEBUG(level, fmt, args...) { \
46		if (i2c_debug & (level)) \
47			printk(KERN_INFO KBUILD_MODNAME ":[%s:%d] " fmt, \
48				__func__, __LINE__ , ## args); \
49	}
50
51static int usbvision_i2c_write(struct usb_usbvision *usbvision, unsigned char addr, char *buf,
52			    short len);
53static int usbvision_i2c_read(struct usb_usbvision *usbvision, unsigned char addr, char *buf,
54			   short len);
55
56static inline int try_write_address(struct i2c_adapter *i2c_adap,
57				    unsigned char addr, int retries)
58{
59	struct usb_usbvision *usbvision;
60	int i, ret = -1;
61	char buf[4];
62
63	usbvision = (struct usb_usbvision *)i2c_get_adapdata(i2c_adap);
64	buf[0] = 0x00;
65	for (i = 0; i <= retries; i++) {
66		ret = (usbvision_i2c_write(usbvision, addr, buf, 1));
67		if (ret == 1)
68			break;	/* success! */
69		udelay(5);
70		if (i == retries)	/* no success */
71			break;
72		udelay(10);
73	}
74	if (i) {
75		PDEBUG(DBG_I2C, "Needed %d retries for address %#2x", i, addr);
76		PDEBUG(DBG_I2C, "Maybe there's no device at this address");
77	}
78	return ret;
79}
80
81static inline int try_read_address(struct i2c_adapter *i2c_adap,
82				   unsigned char addr, int retries)
83{
84	struct usb_usbvision *usbvision;
85	int i, ret = -1;
86	char buf[4];
87
88	usbvision = (struct usb_usbvision *)i2c_get_adapdata(i2c_adap);
89	for (i = 0; i <= retries; i++) {
90		ret = (usbvision_i2c_read(usbvision, addr, buf, 1));
91		if (ret == 1)
92			break;	/* success! */
93		udelay(5);
94		if (i == retries)	/* no success */
95			break;
96		udelay(10);
97	}
98	if (i) {
99		PDEBUG(DBG_I2C, "Needed %d retries for address %#2x", i, addr);
100		PDEBUG(DBG_I2C, "Maybe there's no device at this address");
101	}
102	return ret;
103}
104
105static inline int usb_find_address(struct i2c_adapter *i2c_adap,
106				   struct i2c_msg *msg, int retries,
107				   unsigned char *add)
108{
109	unsigned short flags = msg->flags;
110
111	unsigned char addr;
112	int ret;
113
114	addr = (msg->addr << 1);
115	if (flags & I2C_M_RD)
116		addr |= 1;
117
118	add[0] = addr;
119	if (flags & I2C_M_RD)
120		ret = try_read_address(i2c_adap, addr, retries);
121	else
122		ret = try_write_address(i2c_adap, addr, retries);
123
124	if (ret != 1)
125		return -EREMOTEIO;
126
127	return 0;
128}
129
130static int
131usbvision_i2c_xfer(struct i2c_adapter *i2c_adap, struct i2c_msg msgs[], int num)
132{
133	struct i2c_msg *pmsg;
134	struct usb_usbvision *usbvision;
135	int i, ret;
136	unsigned char addr = 0;
137
138	usbvision = (struct usb_usbvision *)i2c_get_adapdata(i2c_adap);
139
140	for (i = 0; i < num; i++) {
141		pmsg = &msgs[i];
142		ret = usb_find_address(i2c_adap, pmsg, i2c_adap->retries, &addr);
143		if (ret != 0) {
144			PDEBUG(DBG_I2C, "got NAK from device, message #%d", i);
145			return (ret < 0) ? ret : -EREMOTEIO;
146		}
147
148		if (pmsg->flags & I2C_M_RD) {
149			/* read bytes into buffer */
150			ret = (usbvision_i2c_read(usbvision, addr, pmsg->buf, pmsg->len));
151			if (ret < pmsg->len)
152				return (ret < 0) ? ret : -EREMOTEIO;
153		} else {
154			/* write bytes from buffer */
155			ret = (usbvision_i2c_write(usbvision, addr, pmsg->buf, pmsg->len));
156			if (ret < pmsg->len)
157				return (ret < 0) ? ret : -EREMOTEIO;
158		}
159	}
160	return num;
161}
162
163static u32 functionality(struct i2c_adapter *adap)
164{
165	return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;
166}
167
168/* -----exported algorithm data: -------------------------------------	*/
169
170static struct i2c_algorithm usbvision_algo = {
171	.master_xfer   = usbvision_i2c_xfer,
172	.smbus_xfer    = NULL,
173	.functionality = functionality,
174};
175
176
177/* ----------------------------------------------------------------------- */
178/* usbvision specific I2C functions                                        */
179/* ----------------------------------------------------------------------- */
180static struct i2c_adapter i2c_adap_template;
181
182int usbvision_i2c_register(struct usb_usbvision *usbvision)
183{
184	static unsigned short saa711x_addrs[] = {
185		0x4a >> 1, 0x48 >> 1,	/* SAA7111, SAA7111A and SAA7113 */
186		0x42 >> 1, 0x40 >> 1,	/* SAA7114, SAA7115 and SAA7118 */
187		I2C_CLIENT_END };
188
189	if (usbvision->registered_i2c)
190		return 0;
191
192	usbvision->i2c_adap = i2c_adap_template;
193
194	sprintf(usbvision->i2c_adap.name, "%s-%d-%s", i2c_adap_template.name,
195		usbvision->dev->bus->busnum, usbvision->dev->devpath);
196	PDEBUG(DBG_I2C, "Adaptername: %s", usbvision->i2c_adap.name);
197	usbvision->i2c_adap.dev.parent = &usbvision->dev->dev;
198
199	i2c_set_adapdata(&usbvision->i2c_adap, &usbvision->v4l2_dev);
200
201	if (usbvision_write_reg(usbvision, USBVISION_SER_MODE, USBVISION_IIC_LRNACK) < 0) {
202		printk(KERN_ERR "usbvision_i2c_register: can't write reg\n");
203		return -EBUSY;
204	}
205
206	PDEBUG(DBG_I2C, "I2C   debugging is enabled [i2c]");
207	PDEBUG(DBG_I2C, "ALGO   debugging is enabled [i2c]");
208
209	/* register new adapter to i2c module... */
210
211	usbvision->i2c_adap.algo = &usbvision_algo;
212
213	usbvision->i2c_adap.timeout = 100;	/* default values, should       */
214	usbvision->i2c_adap.retries = 3;	/* be replaced by defines       */
215
216	i2c_add_adapter(&usbvision->i2c_adap);
217
218	PDEBUG(DBG_I2C, "i2c bus for %s registered", usbvision->i2c_adap.name);
219
220	/* Request the load of the i2c modules we need */
221	switch (usbvision_device_data[usbvision->dev_model].codec) {
222	case CODEC_SAA7113:
223	case CODEC_SAA7111:
224		/* Without this delay the detection of the saa711x is
225		   hit-and-miss. */
226		mdelay(10);
227		v4l2_i2c_new_subdev(&usbvision->v4l2_dev,
228				&usbvision->i2c_adap,
229				"saa7115_auto", 0, saa711x_addrs);
230		break;
231	}
232	if (usbvision_device_data[usbvision->dev_model].tuner == 1) {
233		struct v4l2_subdev *sd;
234		enum v4l2_i2c_tuner_type type;
235		struct tuner_setup tun_setup;
236
237		sd = v4l2_i2c_new_subdev(&usbvision->v4l2_dev,
238				&usbvision->i2c_adap,
239				"tuner", 0, v4l2_i2c_tuner_addrs(ADDRS_DEMOD));
240		/* depending on whether we found a demod or not, select
241		   the tuner type. */
242		type = sd ? ADDRS_TV_WITH_DEMOD : ADDRS_TV;
243
244		sd = v4l2_i2c_new_subdev(&usbvision->v4l2_dev,
245				&usbvision->i2c_adap,
246				"tuner", 0, v4l2_i2c_tuner_addrs(type));
247
248		if (sd == NULL)
249			return -ENODEV;
250		if (usbvision->tuner_type != -1) {
251			tun_setup.mode_mask = T_ANALOG_TV | T_RADIO;
252			tun_setup.type = usbvision->tuner_type;
253			tun_setup.addr = v4l2_i2c_subdev_addr(sd);
254			call_all(usbvision, tuner, s_type_addr, &tun_setup);
255		}
256	}
257	usbvision->registered_i2c = 1;
258
259	return 0;
260}
261
262int usbvision_i2c_unregister(struct usb_usbvision *usbvision)
263{
264	if (!usbvision->registered_i2c)
265		return 0;
266
267	i2c_del_adapter(&(usbvision->i2c_adap));
268	usbvision->registered_i2c = 0;
269
270	PDEBUG(DBG_I2C, "i2c bus for %s unregistered", usbvision->i2c_adap.name);
271
272	return 0;
273}
274
275static int
276usbvision_i2c_read_max4(struct usb_usbvision *usbvision, unsigned char addr,
277		     char *buf, short len)
278{
279	int rc, retries;
280
281	for (retries = 5;;) {
282		rc = usbvision_write_reg(usbvision, USBVISION_SER_ADRS, addr);
283		if (rc < 0)
284			return rc;
285
286		/* Initiate byte read cycle                    */
287		/* USBVISION_SER_CONT <- d0-d2 n. of bytes to r/w */
288		/*                    d3 0=Wr 1=Rd             */
289		rc = usbvision_write_reg(usbvision, USBVISION_SER_CONT,
290				      (len & 0x07) | 0x18);
291		if (rc < 0)
292			return rc;
293
294		/* Test for Busy and ACK */
295		do {
296			/* USBVISION_SER_CONT -> d4 == 0 busy */
297			rc = usbvision_read_reg(usbvision, USBVISION_SER_CONT);
298		} while (rc > 0 && ((rc & 0x10) != 0));	/* Retry while busy */
299		if (rc < 0)
300			return rc;
301
302		/* USBVISION_SER_CONT -> d5 == 1 Not ack */
303		if ((rc & 0x20) == 0)	/* Ack? */
304			break;
305
306		/* I2C abort */
307		rc = usbvision_write_reg(usbvision, USBVISION_SER_CONT, 0x00);
308		if (rc < 0)
309			return rc;
310
311		if (--retries < 0)
312			return -1;
313	}
314
315	switch (len) {
316	case 4:
317		buf[3] = usbvision_read_reg(usbvision, USBVISION_SER_DAT4);
318	case 3:
319		buf[2] = usbvision_read_reg(usbvision, USBVISION_SER_DAT3);
320	case 2:
321		buf[1] = usbvision_read_reg(usbvision, USBVISION_SER_DAT2);
322	case 1:
323		buf[0] = usbvision_read_reg(usbvision, USBVISION_SER_DAT1);
324		break;
325	default:
326		printk(KERN_ERR
327		       "usbvision_i2c_read_max4: buffer length > 4\n");
328	}
329
330	if (i2c_debug & DBG_I2C) {
331		int idx;
332
333		for (idx = 0; idx < len; idx++)
334			PDEBUG(DBG_I2C, "read %x from address %x", (unsigned char)buf[idx], addr);
335	}
336	return len;
337}
338
339
340static int usbvision_i2c_write_max4(struct usb_usbvision *usbvision,
341				 unsigned char addr, const char *buf,
342				 short len)
343{
344	int rc, retries;
345	int i;
346	unsigned char value[6];
347	unsigned char ser_cont;
348
349	ser_cont = (len & 0x07) | 0x10;
350
351	value[0] = addr;
352	value[1] = ser_cont;
353	for (i = 0; i < len; i++)
354		value[i + 2] = buf[i];
355
356	for (retries = 5;;) {
357		rc = usb_control_msg(usbvision->dev,
358				     usb_sndctrlpipe(usbvision->dev, 1),
359				     USBVISION_OP_CODE,
360				     USB_DIR_OUT | USB_TYPE_VENDOR |
361				     USB_RECIP_ENDPOINT, 0,
362				     (__u16) USBVISION_SER_ADRS, value,
363				     len + 2, HZ);
364
365		if (rc < 0)
366			return rc;
367
368		rc = usbvision_write_reg(usbvision, USBVISION_SER_CONT,
369				      (len & 0x07) | 0x10);
370		if (rc < 0)
371			return rc;
372
373		/* Test for Busy and ACK */
374		do {
375			rc = usbvision_read_reg(usbvision, USBVISION_SER_CONT);
376		} while (rc > 0 && ((rc & 0x10) != 0));	/* Retry while busy */
377		if (rc < 0)
378			return rc;
379
380		if ((rc & 0x20) == 0)	/* Ack? */
381			break;
382
383		/* I2C abort */
384		usbvision_write_reg(usbvision, USBVISION_SER_CONT, 0x00);
385
386		if (--retries < 0)
387			return -1;
388
389	}
390
391	if (i2c_debug & DBG_I2C) {
392		int idx;
393
394		for (idx = 0; idx < len; idx++)
395			PDEBUG(DBG_I2C, "wrote %x at address %x", (unsigned char)buf[idx], addr);
396	}
397	return len;
398}
399
400static int usbvision_i2c_write(struct usb_usbvision *usbvision, unsigned char addr, char *buf,
401			    short len)
402{
403	char *buf_ptr = buf;
404	int retval;
405	int wrcount = 0;
406	int count;
407	int max_len = 4;
408
409	while (len > 0) {
410		count = (len > max_len) ? max_len : len;
411		retval = usbvision_i2c_write_max4(usbvision, addr, buf_ptr, count);
412		if (retval > 0) {
413			len -= count;
414			buf_ptr += count;
415			wrcount += count;
416		} else
417			return (retval < 0) ? retval : -EFAULT;
418	}
419	return wrcount;
420}
421
422static int usbvision_i2c_read(struct usb_usbvision *usbvision, unsigned char addr, char *buf,
423			   short len)
424{
425	char temp[4];
426	int retval, i;
427	int rdcount = 0;
428	int count;
429
430	while (len > 0) {
431		count = (len > 3) ? 4 : len;
432		retval = usbvision_i2c_read_max4(usbvision, addr, temp, count);
433		if (retval > 0) {
434			for (i = 0; i < len; i++)
435				buf[rdcount + i] = temp[i];
436			len -= count;
437			rdcount += count;
438		} else
439			return (retval < 0) ? retval : -EFAULT;
440	}
441	return rdcount;
442}
443
444static struct i2c_adapter i2c_adap_template = {
445	.owner = THIS_MODULE,
446	.name              = "usbvision",
447};
448