blob: 6e4df3335b1befd0da948428d58aca0593f5fbcd [file] [log] [blame]
Thomas Gleixnerc942fdd2019-05-27 08:55:06 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Thierry MERLEf2242ee2006-12-04 08:31:14 -03002/*
Thierry MERLE1ff16c22007-04-14 16:23:49 -03003 * usbvision_i2c.c
Mauro Carvalho Chehab781aa1d1a2006-12-04 08:30:53 -03004 * i2c algorithm for USB-I2C Bridges
5 *
Thierry MERLE1ff16c22007-04-14 16:23:49 -03006 * Copyright (c) 1999-2007 Joerg Heckenbach <joerg@heckenbach-aw.de>
Mauro Carvalho Chehab781aa1d1a2006-12-04 08:30:53 -03007 * Dwaine Garden <dwainegarden@rogers.com>
8 *
9 * This module is part of usbvision driver project.
10 * Updates to driver completed by Dwaine P. Garden
Mauro Carvalho Chehab781aa1d1a2006-12-04 08:30:53 -030011 */
12
13
14#include <linux/kernel.h>
15#include <linux/module.h>
16#include <linux/delay.h>
Mauro Carvalho Chehab781aa1d1a2006-12-04 08:30:53 -030017#include <linux/init.h>
Hans Verkuil6d6a48e2010-12-29 13:53:21 -030018#include <linux/uaccess.h>
Mauro Carvalho Chehab781aa1d1a2006-12-04 08:30:53 -030019#include <linux/ioport.h>
20#include <linux/errno.h>
Mauro Carvalho Chehab781aa1d1a2006-12-04 08:30:53 -030021#include <linux/usb.h>
22#include <linux/i2c.h>
Thierry MERLE483dfdb2006-12-04 08:31:45 -030023#include "usbvision.h"
Mauro Carvalho Chehab781aa1d1a2006-12-04 08:30:53 -030024
Hans Verkuil6d6a48e2010-12-29 13:53:21 -030025#define DBG_I2C (1 << 0)
Mauro Carvalho Chehab781aa1d1a2006-12-04 08:30:53 -030026
Douglas Schilling Landgrafff699e62008-04-22 14:41:48 -030027static int i2c_debug;
Mauro Carvalho Chehab781aa1d1a2006-12-04 08:30:53 -030028
Hans Verkuil6d6a48e2010-12-29 13:53:21 -030029module_param(i2c_debug, int, 0644); /* debug_i2c_usb mode of the device driver */
Thierry MERLE483dfdb2006-12-04 08:31:45 -030030MODULE_PARM_DESC(i2c_debug, "enable debug messages [i2c]");
31
Thierry MERLEdf18c312008-04-04 21:00:57 -030032#define PDEBUG(level, fmt, args...) { \
33 if (i2c_debug & (level)) \
Greg Kroah-Hartmana482f322008-10-10 05:08:23 -030034 printk(KERN_INFO KBUILD_MODNAME ":[%s:%d] " fmt, \
35 __func__, __LINE__ , ## args); \
Thierry MERLEdf18c312008-04-04 21:00:57 -030036 }
Mauro Carvalho Chehab781aa1d1a2006-12-04 08:30:53 -030037
Thierry MERLE1ff16c22007-04-14 16:23:49 -030038static int usbvision_i2c_write(struct usb_usbvision *usbvision, unsigned char addr, char *buf,
Mauro Carvalho Chehab2a7e9a22006-12-11 05:37:14 -030039 short len);
Thierry MERLE1ff16c22007-04-14 16:23:49 -030040static int usbvision_i2c_read(struct usb_usbvision *usbvision, unsigned char addr, char *buf,
Mauro Carvalho Chehab2a7e9a22006-12-11 05:37:14 -030041 short len);
42
Mauro Carvalho Chehab781aa1d1a2006-12-04 08:30:53 -030043static inline int try_write_address(struct i2c_adapter *i2c_adap,
44 unsigned char addr, int retries)
45{
Thierry MERLE1ff16c22007-04-14 16:23:49 -030046 struct usb_usbvision *usbvision;
Mauro Carvalho Chehab781aa1d1a2006-12-04 08:30:53 -030047 int i, ret = -1;
48 char buf[4];
49
Thierry MERLEbf8b20e2007-04-16 18:00:49 -030050 usbvision = (struct usb_usbvision *)i2c_get_adapdata(i2c_adap);
Mauro Carvalho Chehab781aa1d1a2006-12-04 08:30:53 -030051 buf[0] = 0x00;
52 for (i = 0; i <= retries; i++) {
Thierry MERLE1ff16c22007-04-14 16:23:49 -030053 ret = (usbvision_i2c_write(usbvision, addr, buf, 1));
Mauro Carvalho Chehab781aa1d1a2006-12-04 08:30:53 -030054 if (ret == 1)
55 break; /* success! */
Mauro Carvalho Chehabc6268462006-12-11 17:18:15 -030056 udelay(5);
Mauro Carvalho Chehab781aa1d1a2006-12-04 08:30:53 -030057 if (i == retries) /* no success */
58 break;
Mauro Carvalho Chehabc6268462006-12-11 17:18:15 -030059 udelay(10);
Mauro Carvalho Chehab781aa1d1a2006-12-04 08:30:53 -030060 }
Thierry MERLE483dfdb2006-12-04 08:31:45 -030061 if (i) {
Hans Verkuil6d6a48e2010-12-29 13:53:21 -030062 PDEBUG(DBG_I2C, "Needed %d retries for address %#2x", i, addr);
63 PDEBUG(DBG_I2C, "Maybe there's no device at this address");
Mauro Carvalho Chehab781aa1d1a2006-12-04 08:30:53 -030064 }
65 return ret;
66}
67
68static inline int try_read_address(struct i2c_adapter *i2c_adap,
69 unsigned char addr, int retries)
70{
Thierry MERLE1ff16c22007-04-14 16:23:49 -030071 struct usb_usbvision *usbvision;
Mauro Carvalho Chehab781aa1d1a2006-12-04 08:30:53 -030072 int i, ret = -1;
73 char buf[4];
74
Thierry MERLEbf8b20e2007-04-16 18:00:49 -030075 usbvision = (struct usb_usbvision *)i2c_get_adapdata(i2c_adap);
Mauro Carvalho Chehab781aa1d1a2006-12-04 08:30:53 -030076 for (i = 0; i <= retries; i++) {
Thierry MERLE1ff16c22007-04-14 16:23:49 -030077 ret = (usbvision_i2c_read(usbvision, addr, buf, 1));
Mauro Carvalho Chehab781aa1d1a2006-12-04 08:30:53 -030078 if (ret == 1)
79 break; /* success! */
Mauro Carvalho Chehabc6268462006-12-11 17:18:15 -030080 udelay(5);
Mauro Carvalho Chehab781aa1d1a2006-12-04 08:30:53 -030081 if (i == retries) /* no success */
82 break;
Mauro Carvalho Chehabc6268462006-12-11 17:18:15 -030083 udelay(10);
Mauro Carvalho Chehab781aa1d1a2006-12-04 08:30:53 -030084 }
Thierry MERLE483dfdb2006-12-04 08:31:45 -030085 if (i) {
Hans Verkuil6d6a48e2010-12-29 13:53:21 -030086 PDEBUG(DBG_I2C, "Needed %d retries for address %#2x", i, addr);
87 PDEBUG(DBG_I2C, "Maybe there's no device at this address");
Mauro Carvalho Chehab781aa1d1a2006-12-04 08:30:53 -030088 }
89 return ret;
90}
91
92static inline int usb_find_address(struct i2c_adapter *i2c_adap,
93 struct i2c_msg *msg, int retries,
94 unsigned char *add)
95{
96 unsigned short flags = msg->flags;
Thierry MERLEf2242ee2006-12-04 08:31:14 -030097
Mauro Carvalho Chehab781aa1d1a2006-12-04 08:30:53 -030098 unsigned char addr;
99 int ret;
Jean Delvare819cd822011-11-07 05:24:49 -0300100
101 addr = (msg->addr << 1);
102 if (flags & I2C_M_RD)
103 addr |= 1;
104
105 add[0] = addr;
106 if (flags & I2C_M_RD)
107 ret = try_read_address(i2c_adap, addr, retries);
108 else
Mauro Carvalho Chehab781aa1d1a2006-12-04 08:30:53 -0300109 ret = try_write_address(i2c_adap, addr, retries);
Mauro Carvalho Chehab781aa1d1a2006-12-04 08:30:53 -0300110
Jean Delvare819cd822011-11-07 05:24:49 -0300111 if (ret != 1)
112 return -EREMOTEIO;
Mauro Carvalho Chehab781aa1d1a2006-12-04 08:30:53 -0300113
Mauro Carvalho Chehab781aa1d1a2006-12-04 08:30:53 -0300114 return 0;
115}
116
117static int
Thierry MERLE1ff16c22007-04-14 16:23:49 -0300118usbvision_i2c_xfer(struct i2c_adapter *i2c_adap, struct i2c_msg msgs[], int num)
Mauro Carvalho Chehab781aa1d1a2006-12-04 08:30:53 -0300119{
120 struct i2c_msg *pmsg;
Thierry MERLE1ff16c22007-04-14 16:23:49 -0300121 struct usb_usbvision *usbvision;
Mauro Carvalho Chehab781aa1d1a2006-12-04 08:30:53 -0300122 int i, ret;
Hans Verkuilcf8e1932009-01-17 13:09:11 -0300123 unsigned char addr = 0;
Mauro Carvalho Chehab781aa1d1a2006-12-04 08:30:53 -0300124
Thierry MERLEbf8b20e2007-04-16 18:00:49 -0300125 usbvision = (struct usb_usbvision *)i2c_get_adapdata(i2c_adap);
126
Mauro Carvalho Chehab781aa1d1a2006-12-04 08:30:53 -0300127 for (i = 0; i < num; i++) {
128 pmsg = &msgs[i];
129 ret = usb_find_address(i2c_adap, pmsg, i2c_adap->retries, &addr);
130 if (ret != 0) {
Hans Verkuil6d6a48e2010-12-29 13:53:21 -0300131 PDEBUG(DBG_I2C, "got NAK from device, message #%d", i);
Mauro Carvalho Chehab781aa1d1a2006-12-04 08:30:53 -0300132 return (ret < 0) ? ret : -EREMOTEIO;
133 }
134
135 if (pmsg->flags & I2C_M_RD) {
136 /* read bytes into buffer */
Thierry MERLE1ff16c22007-04-14 16:23:49 -0300137 ret = (usbvision_i2c_read(usbvision, addr, pmsg->buf, pmsg->len));
Hans Verkuil6d6a48e2010-12-29 13:53:21 -0300138 if (ret < pmsg->len)
Mauro Carvalho Chehab781aa1d1a2006-12-04 08:30:53 -0300139 return (ret < 0) ? ret : -EREMOTEIO;
Mauro Carvalho Chehab781aa1d1a2006-12-04 08:30:53 -0300140 } else {
141 /* write bytes from buffer */
Thierry MERLE1ff16c22007-04-14 16:23:49 -0300142 ret = (usbvision_i2c_write(usbvision, addr, pmsg->buf, pmsg->len));
Hans Verkuil6d6a48e2010-12-29 13:53:21 -0300143 if (ret < pmsg->len)
Mauro Carvalho Chehab781aa1d1a2006-12-04 08:30:53 -0300144 return (ret < 0) ? ret : -EREMOTEIO;
Mauro Carvalho Chehab781aa1d1a2006-12-04 08:30:53 -0300145 }
146 }
147 return num;
148}
149
Thierry MERLE1ff16c22007-04-14 16:23:49 -0300150static u32 functionality(struct i2c_adapter *adap)
Mauro Carvalho Chehab781aa1d1a2006-12-04 08:30:53 -0300151{
Jean Delvare819cd822011-11-07 05:24:49 -0300152 return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;
Mauro Carvalho Chehab781aa1d1a2006-12-04 08:30:53 -0300153}
154
Mauro Carvalho Chehab781aa1d1a2006-12-04 08:30:53 -0300155/* -----exported algorithm data: ------------------------------------- */
156
Gustavo A. R. Silva830a45a2017-07-09 18:04:24 -0400157static const struct i2c_algorithm usbvision_algo = {
Thierry MERLE1ff16c22007-04-14 16:23:49 -0300158 .master_xfer = usbvision_i2c_xfer,
Mauro Carvalho Chehab781aa1d1a2006-12-04 08:30:53 -0300159 .smbus_xfer = NULL,
Thierry MERLE1ff16c22007-04-14 16:23:49 -0300160 .functionality = functionality,
Mauro Carvalho Chehab781aa1d1a2006-12-04 08:30:53 -0300161};
162
163
Thierry MERLE483dfdb2006-12-04 08:31:45 -0300164/* ----------------------------------------------------------------------- */
165/* usbvision specific I2C functions */
166/* ----------------------------------------------------------------------- */
Bhumika Goyal68438682017-08-19 06:34:15 -0400167static const struct i2c_adapter i2c_adap_template;
Thierry MERLE483dfdb2006-12-04 08:31:45 -0300168
Thierry MERLE1ff16c22007-04-14 16:23:49 -0300169int usbvision_i2c_register(struct usb_usbvision *usbvision)
Thierry MERLE483dfdb2006-12-04 08:31:45 -0300170{
Hans Verkuil1df79532009-02-21 18:11:31 -0300171 static unsigned short saa711x_addrs[] = {
172 0x4a >> 1, 0x48 >> 1, /* SAA7111, SAA7111A and SAA7113 */
173 0x42 >> 1, 0x40 >> 1, /* SAA7114, SAA7115 and SAA7118 */
174 I2C_CLIENT_END };
175
Hans Verkuil84034722010-09-17 15:07:28 -0300176 if (usbvision->registered_i2c)
177 return 0;
178
Ezequiel Garcia8fe392b2012-10-23 15:57:06 -0300179 usbvision->i2c_adap = i2c_adap_template;
Thierry MERLE483dfdb2006-12-04 08:31:45 -0300180
Arnd Bergmannd82cf242017-07-17 10:29:58 -0400181 snprintf(usbvision->i2c_adap.name, sizeof(usbvision->i2c_adap.name),
182 "usbvision-%d-%s",
183 usbvision->dev->bus->busnum, usbvision->dev->devpath);
Hans Verkuil6d6a48e2010-12-29 13:53:21 -0300184 PDEBUG(DBG_I2C, "Adaptername: %s", usbvision->i2c_adap.name);
Thierry MERLEbf8b20e2007-04-16 18:00:49 -0300185 usbvision->i2c_adap.dev.parent = &usbvision->dev->dev;
Thierry MERLE483dfdb2006-12-04 08:31:45 -0300186
Hans Verkuil1df79532009-02-21 18:11:31 -0300187 i2c_set_adapdata(&usbvision->i2c_adap, &usbvision->v4l2_dev);
Thierry MERLE483dfdb2006-12-04 08:31:45 -0300188
189 if (usbvision_write_reg(usbvision, USBVISION_SER_MODE, USBVISION_IIC_LRNACK) < 0) {
Ondrej Zary240d57b2011-04-27 17:36:05 -0300190 printk(KERN_ERR "usbvision_i2c_register: can't write reg\n");
Thierry MERLE483dfdb2006-12-04 08:31:45 -0300191 return -EBUSY;
192 }
193
Hans Verkuil1df79532009-02-21 18:11:31 -0300194 PDEBUG(DBG_I2C, "I2C debugging is enabled [i2c]");
195 PDEBUG(DBG_I2C, "ALGO debugging is enabled [i2c]");
196
197 /* register new adapter to i2c module... */
198
199 usbvision->i2c_adap.algo = &usbvision_algo;
200
201 usbvision->i2c_adap.timeout = 100; /* default values, should */
202 usbvision->i2c_adap.retries = 3; /* be replaced by defines */
203
204 i2c_add_adapter(&usbvision->i2c_adap);
205
206 PDEBUG(DBG_I2C, "i2c bus for %s registered", usbvision->i2c_adap.name);
207
Thierry MERLE483dfdb2006-12-04 08:31:45 -0300208 /* Request the load of the i2c modules we need */
Hans Verkuil5490a7c2010-12-19 20:21:36 -0300209 switch (usbvision_device_data[usbvision->dev_model].codec) {
Thierry MERLE483dfdb2006-12-04 08:31:45 -0300210 case CODEC_SAA7113:
Thierry MERLE483dfdb2006-12-04 08:31:45 -0300211 case CODEC_SAA7111:
Hans Verkuil4d6c8712010-05-02 09:29:27 -0300212 /* Without this delay the detection of the saa711x is
213 hit-and-miss. */
214 mdelay(10);
Hans Verkuil53dacb12009-08-10 02:49:08 -0300215 v4l2_i2c_new_subdev(&usbvision->v4l2_dev,
Laurent Pinchart9a1f8b32010-09-24 10:16:44 -0300216 &usbvision->i2c_adap,
Hans Verkuil53dacb12009-08-10 02:49:08 -0300217 "saa7115_auto", 0, saa711x_addrs);
Thierry MERLE483dfdb2006-12-04 08:31:45 -0300218 break;
219 }
Hans Verkuil5490a7c2010-12-19 20:21:36 -0300220 if (usbvision_device_data[usbvision->dev_model].tuner == 1) {
Hans Verkuil1df79532009-02-21 18:11:31 -0300221 struct v4l2_subdev *sd;
222 enum v4l2_i2c_tuner_type type;
223 struct tuner_setup tun_setup;
Thierry MERLE483dfdb2006-12-04 08:31:45 -0300224
Hans Verkuil53dacb12009-08-10 02:49:08 -0300225 sd = v4l2_i2c_new_subdev(&usbvision->v4l2_dev,
Laurent Pinchart9a1f8b32010-09-24 10:16:44 -0300226 &usbvision->i2c_adap,
Hans Verkuil53dacb12009-08-10 02:49:08 -0300227 "tuner", 0, v4l2_i2c_tuner_addrs(ADDRS_DEMOD));
Hans Verkuil1df79532009-02-21 18:11:31 -0300228 /* depending on whether we found a demod or not, select
229 the tuner type. */
230 type = sd ? ADDRS_TV_WITH_DEMOD : ADDRS_TV;
231
Hans Verkuil53dacb12009-08-10 02:49:08 -0300232 sd = v4l2_i2c_new_subdev(&usbvision->v4l2_dev,
Laurent Pinchart9a1f8b32010-09-24 10:16:44 -0300233 &usbvision->i2c_adap,
Hans Verkuil53dacb12009-08-10 02:49:08 -0300234 "tuner", 0, v4l2_i2c_tuner_addrs(type));
Hans Verkuil1df79532009-02-21 18:11:31 -0300235
Hans Verkuil84034722010-09-17 15:07:28 -0300236 if (sd == NULL)
237 return -ENODEV;
Hans Verkuil1df79532009-02-21 18:11:31 -0300238 if (usbvision->tuner_type != -1) {
239 tun_setup.mode_mask = T_ANALOG_TV | T_RADIO;
240 tun_setup.type = usbvision->tuner_type;
241 tun_setup.addr = v4l2_i2c_subdev_addr(sd);
242 call_all(usbvision, tuner, s_type_addr, &tun_setup);
243 }
244 }
Hans Verkuil84034722010-09-17 15:07:28 -0300245 usbvision->registered_i2c = 1;
Hans Verkuil1df79532009-02-21 18:11:31 -0300246
247 return 0;
Thierry MERLE1ff16c22007-04-14 16:23:49 -0300248}
249
250int usbvision_i2c_unregister(struct usb_usbvision *usbvision)
251{
Hans Verkuil84034722010-09-17 15:07:28 -0300252 if (!usbvision->registered_i2c)
253 return 0;
Thierry MERLE1ff16c22007-04-14 16:23:49 -0300254
255 i2c_del_adapter(&(usbvision->i2c_adap));
Hans Verkuil84034722010-09-17 15:07:28 -0300256 usbvision->registered_i2c = 0;
Thierry MERLE1ff16c22007-04-14 16:23:49 -0300257
Hans Verkuil6d6a48e2010-12-29 13:53:21 -0300258 PDEBUG(DBG_I2C, "i2c bus for %s unregistered", usbvision->i2c_adap.name);
Thierry MERLE1ff16c22007-04-14 16:23:49 -0300259
260 return 0;
Thierry MERLE483dfdb2006-12-04 08:31:45 -0300261}
262
Thierry MERLE483dfdb2006-12-04 08:31:45 -0300263static int
264usbvision_i2c_read_max4(struct usb_usbvision *usbvision, unsigned char addr,
265 char *buf, short len)
266{
267 int rc, retries;
268
269 for (retries = 5;;) {
270 rc = usbvision_write_reg(usbvision, USBVISION_SER_ADRS, addr);
271 if (rc < 0)
272 return rc;
273
274 /* Initiate byte read cycle */
275 /* USBVISION_SER_CONT <- d0-d2 n. of bytes to r/w */
276 /* d3 0=Wr 1=Rd */
277 rc = usbvision_write_reg(usbvision, USBVISION_SER_CONT,
278 (len & 0x07) | 0x18);
279 if (rc < 0)
280 return rc;
281
282 /* Test for Busy and ACK */
283 do {
284 /* USBVISION_SER_CONT -> d4 == 0 busy */
285 rc = usbvision_read_reg(usbvision, USBVISION_SER_CONT);
286 } while (rc > 0 && ((rc & 0x10) != 0)); /* Retry while busy */
287 if (rc < 0)
288 return rc;
289
290 /* USBVISION_SER_CONT -> d5 == 1 Not ack */
291 if ((rc & 0x20) == 0) /* Ack? */
292 break;
293
294 /* I2C abort */
295 rc = usbvision_write_reg(usbvision, USBVISION_SER_CONT, 0x00);
296 if (rc < 0)
297 return rc;
298
299 if (--retries < 0)
300 return -1;
301 }
302
303 switch (len) {
304 case 4:
305 buf[3] = usbvision_read_reg(usbvision, USBVISION_SER_DAT4);
Mauro Carvalho Chehab06eeefe2017-05-18 08:13:28 -0300306 /* fall through */
Thierry MERLE483dfdb2006-12-04 08:31:45 -0300307 case 3:
308 buf[2] = usbvision_read_reg(usbvision, USBVISION_SER_DAT3);
Mauro Carvalho Chehab06eeefe2017-05-18 08:13:28 -0300309 /* fall through */
Thierry MERLE483dfdb2006-12-04 08:31:45 -0300310 case 2:
311 buf[1] = usbvision_read_reg(usbvision, USBVISION_SER_DAT2);
Mauro Carvalho Chehab06eeefe2017-05-18 08:13:28 -0300312 /* fall through */
Thierry MERLE483dfdb2006-12-04 08:31:45 -0300313 case 1:
314 buf[0] = usbvision_read_reg(usbvision, USBVISION_SER_DAT1);
315 break;
316 default:
317 printk(KERN_ERR
318 "usbvision_i2c_read_max4: buffer length > 4\n");
319 }
320
321 if (i2c_debug & DBG_I2C) {
322 int idx;
Hans Verkuil6d6a48e2010-12-29 13:53:21 -0300323
324 for (idx = 0; idx < len; idx++)
325 PDEBUG(DBG_I2C, "read %x from address %x", (unsigned char)buf[idx], addr);
Thierry MERLE483dfdb2006-12-04 08:31:45 -0300326 }
327 return len;
328}
329
330
331static int usbvision_i2c_write_max4(struct usb_usbvision *usbvision,
332 unsigned char addr, const char *buf,
333 short len)
334{
335 int rc, retries;
336 int i;
Hans Verkuil8926e842015-07-20 09:59:36 -0300337 unsigned char *value = usbvision->ctrl_urb_buffer;
Thierry MERLE483dfdb2006-12-04 08:31:45 -0300338 unsigned char ser_cont;
339
340 ser_cont = (len & 0x07) | 0x10;
341
342 value[0] = addr;
343 value[1] = ser_cont;
344 for (i = 0; i < len; i++)
345 value[i + 2] = buf[i];
346
347 for (retries = 5;;) {
348 rc = usb_control_msg(usbvision->dev,
349 usb_sndctrlpipe(usbvision->dev, 1),
350 USBVISION_OP_CODE,
351 USB_DIR_OUT | USB_TYPE_VENDOR |
352 USB_RECIP_ENDPOINT, 0,
353 (__u16) USBVISION_SER_ADRS, value,
354 len + 2, HZ);
355
356 if (rc < 0)
357 return rc;
358
359 rc = usbvision_write_reg(usbvision, USBVISION_SER_CONT,
360 (len & 0x07) | 0x10);
361 if (rc < 0)
362 return rc;
363
364 /* Test for Busy and ACK */
365 do {
366 rc = usbvision_read_reg(usbvision, USBVISION_SER_CONT);
367 } while (rc > 0 && ((rc & 0x10) != 0)); /* Retry while busy */
368 if (rc < 0)
369 return rc;
370
371 if ((rc & 0x20) == 0) /* Ack? */
372 break;
373
374 /* I2C abort */
375 usbvision_write_reg(usbvision, USBVISION_SER_CONT, 0x00);
376
377 if (--retries < 0)
378 return -1;
379
380 }
381
382 if (i2c_debug & DBG_I2C) {
383 int idx;
Hans Verkuil6d6a48e2010-12-29 13:53:21 -0300384
385 for (idx = 0; idx < len; idx++)
386 PDEBUG(DBG_I2C, "wrote %x at address %x", (unsigned char)buf[idx], addr);
Thierry MERLE483dfdb2006-12-04 08:31:45 -0300387 }
388 return len;
389}
390
Thierry MERLE1ff16c22007-04-14 16:23:49 -0300391static int usbvision_i2c_write(struct usb_usbvision *usbvision, unsigned char addr, char *buf,
Thierry MERLE483dfdb2006-12-04 08:31:45 -0300392 short len)
393{
Hans Verkuil5490a7c2010-12-19 20:21:36 -0300394 char *buf_ptr = buf;
Thierry MERLE483dfdb2006-12-04 08:31:45 -0300395 int retval;
396 int wrcount = 0;
397 int count;
Hans Verkuil5490a7c2010-12-19 20:21:36 -0300398 int max_len = 4;
Thierry MERLE483dfdb2006-12-04 08:31:45 -0300399
400 while (len > 0) {
Hans Verkuil5490a7c2010-12-19 20:21:36 -0300401 count = (len > max_len) ? max_len : len;
402 retval = usbvision_i2c_write_max4(usbvision, addr, buf_ptr, count);
Thierry MERLE483dfdb2006-12-04 08:31:45 -0300403 if (retval > 0) {
404 len -= count;
Hans Verkuil5490a7c2010-12-19 20:21:36 -0300405 buf_ptr += count;
Thierry MERLE483dfdb2006-12-04 08:31:45 -0300406 wrcount += count;
407 } else
408 return (retval < 0) ? retval : -EFAULT;
409 }
410 return wrcount;
411}
412
Thierry MERLE1ff16c22007-04-14 16:23:49 -0300413static int usbvision_i2c_read(struct usb_usbvision *usbvision, unsigned char addr, char *buf,
Thierry MERLE483dfdb2006-12-04 08:31:45 -0300414 short len)
415{
416 char temp[4];
417 int retval, i;
418 int rdcount = 0;
419 int count;
Thierry MERLE483dfdb2006-12-04 08:31:45 -0300420
421 while (len > 0) {
422 count = (len > 3) ? 4 : len;
423 retval = usbvision_i2c_read_max4(usbvision, addr, temp, count);
424 if (retval > 0) {
425 for (i = 0; i < len; i++)
426 buf[rdcount + i] = temp[i];
427 len -= count;
428 rdcount += count;
429 } else
430 return (retval < 0) ? retval : -EFAULT;
431 }
432 return rdcount;
433}
434
Bhumika Goyal68438682017-08-19 06:34:15 -0400435static const struct i2c_adapter i2c_adap_template = {
Thierry MERLE483dfdb2006-12-04 08:31:45 -0300436 .owner = THIS_MODULE,
437 .name = "usbvision",
Thierry MERLE483dfdb2006-12-04 08:31:45 -0300438};