1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Driver for the Diolan u2c-12 USB-I2C adapter
4  *
5  * Copyright (c) 2010-2011 Ericsson AB
6  *
7  * Derived from:
8  *  i2c-tiny-usb.c
9  *  Copyright (C) 2006-2007 Till Harbaum (Till@Harbaum.org)
10  */
11 
12 #include <linux/kernel.h>
13 #include <linux/errno.h>
14 #include <linux/module.h>
15 #include <linux/types.h>
16 #include <linux/slab.h>
17 #include <linux/usb.h>
18 #include <linux/i2c.h>
19 
20 #define DRIVER_NAME		"i2c-diolan-u2c"
21 
22 #define USB_VENDOR_ID_DIOLAN		0x0abf
23 #define USB_DEVICE_ID_DIOLAN_U2C	0x3370
24 
25 
26 /* commands via USB, must match command ids in the firmware */
27 #define CMD_I2C_READ		0x01
28 #define CMD_I2C_WRITE		0x02
29 #define CMD_I2C_SCAN		0x03	/* Returns list of detected devices */
30 #define CMD_I2C_RELEASE_SDA	0x04
31 #define CMD_I2C_RELEASE_SCL	0x05
32 #define CMD_I2C_DROP_SDA	0x06
33 #define CMD_I2C_DROP_SCL	0x07
34 #define CMD_I2C_READ_SDA	0x08
35 #define CMD_I2C_READ_SCL	0x09
36 #define CMD_GET_FW_VERSION	0x0a
37 #define CMD_GET_SERIAL		0x0b
38 #define CMD_I2C_START		0x0c
39 #define CMD_I2C_STOP		0x0d
40 #define CMD_I2C_REPEATED_START	0x0e
41 #define CMD_I2C_PUT_BYTE	0x0f
42 #define CMD_I2C_GET_BYTE	0x10
43 #define CMD_I2C_PUT_ACK		0x11
44 #define CMD_I2C_GET_ACK		0x12
45 #define CMD_I2C_PUT_BYTE_ACK	0x13
46 #define CMD_I2C_GET_BYTE_ACK	0x14
47 #define CMD_I2C_SET_SPEED	0x1b
48 #define CMD_I2C_GET_SPEED	0x1c
49 #define CMD_I2C_SET_CLK_SYNC	0x24
50 #define CMD_I2C_GET_CLK_SYNC	0x25
51 #define CMD_I2C_SET_CLK_SYNC_TO	0x26
52 #define CMD_I2C_GET_CLK_SYNC_TO	0x27
53 
54 #define RESP_OK			0x00
55 #define RESP_FAILED		0x01
56 #define RESP_BAD_MEMADDR	0x04
57 #define RESP_DATA_ERR		0x05
58 #define RESP_NOT_IMPLEMENTED	0x06
59 #define RESP_NACK		0x07
60 #define RESP_TIMEOUT		0x09
61 
62 #define U2C_I2C_SPEED_FAST	0	/* 400 kHz */
63 #define U2C_I2C_SPEED_STD	1	/* 100 kHz */
64 #define U2C_I2C_SPEED_2KHZ	242	/* 2 kHz, minimum speed */
65 #define U2C_I2C_SPEED(f)	((DIV_ROUND_UP(1000000, (f)) - 10) / 2 + 1)
66 
67 #define U2C_I2C_FREQ(s)		(1000000 / (2 * (s - 1) + 10))
68 
69 #define DIOLAN_USB_TIMEOUT	100	/* in ms */
70 #define DIOLAN_SYNC_TIMEOUT	20	/* in ms */
71 
72 #define DIOLAN_OUTBUF_LEN	128
73 #define DIOLAN_FLUSH_LEN	(DIOLAN_OUTBUF_LEN - 4)
74 #define DIOLAN_INBUF_LEN	256	/* Maximum supported receive length */
75 
76 /* Structure to hold all of our device specific stuff */
77 struct i2c_diolan_u2c {
78 	u8 obuffer[DIOLAN_OUTBUF_LEN];	/* output buffer */
79 	u8 ibuffer[DIOLAN_INBUF_LEN];	/* input buffer */
80 	int ep_in, ep_out;              /* Endpoints    */
81 	struct usb_device *usb_dev;	/* the usb device for this device */
82 	struct usb_interface *interface;/* the interface for this device */
83 	struct i2c_adapter adapter;	/* i2c related things */
84 	int olen;			/* Output buffer length */
85 	int ocount;			/* Number of enqueued messages */
86 };
87 
88 static uint frequency = I2C_MAX_STANDARD_MODE_FREQ;	/* I2C clock frequency in Hz */
89 
90 module_param(frequency, uint, S_IRUGO | S_IWUSR);
91 MODULE_PARM_DESC(frequency, "I2C clock frequency in hertz");
92 
93 /* usb layer */
94 
95 /* Send command to device, and get response. */
diolan_usb_transfer(struct i2c_diolan_u2c * dev)96 static int diolan_usb_transfer(struct i2c_diolan_u2c *dev)
97 {
98 	int ret = 0;
99 	int actual;
100 	int i;
101 
102 	if (!dev->olen || !dev->ocount)
103 		return -EINVAL;
104 
105 	ret = usb_bulk_msg(dev->usb_dev,
106 			   usb_sndbulkpipe(dev->usb_dev, dev->ep_out),
107 			   dev->obuffer, dev->olen, &actual,
108 			   DIOLAN_USB_TIMEOUT);
109 	if (!ret) {
110 		for (i = 0; i < dev->ocount; i++) {
111 			int tmpret;
112 
113 			tmpret = usb_bulk_msg(dev->usb_dev,
114 					      usb_rcvbulkpipe(dev->usb_dev,
115 							      dev->ep_in),
116 					      dev->ibuffer,
117 					      sizeof(dev->ibuffer), &actual,
118 					      DIOLAN_USB_TIMEOUT);
119 			/*
120 			 * Stop command processing if a previous command
121 			 * returned an error.
122 			 * Note that we still need to retrieve all messages.
123 			 */
124 			if (ret < 0)
125 				continue;
126 			ret = tmpret;
127 			if (ret == 0 && actual > 0) {
128 				switch (dev->ibuffer[actual - 1]) {
129 				case RESP_NACK:
130 					/*
131 					 * Return ENXIO if NACK was received as
132 					 * response to the address phase,
133 					 * EIO otherwise
134 					 */
135 					ret = i == 1 ? -ENXIO : -EIO;
136 					break;
137 				case RESP_TIMEOUT:
138 					ret = -ETIMEDOUT;
139 					break;
140 				case RESP_OK:
141 					/* strip off return code */
142 					ret = actual - 1;
143 					break;
144 				default:
145 					ret = -EIO;
146 					break;
147 				}
148 			}
149 		}
150 	}
151 	dev->olen = 0;
152 	dev->ocount = 0;
153 	return ret;
154 }
155 
diolan_write_cmd(struct i2c_diolan_u2c * dev,bool flush)156 static int diolan_write_cmd(struct i2c_diolan_u2c *dev, bool flush)
157 {
158 	if (flush || dev->olen >= DIOLAN_FLUSH_LEN)
159 		return diolan_usb_transfer(dev);
160 	return 0;
161 }
162 
163 /* Send command (no data) */
diolan_usb_cmd(struct i2c_diolan_u2c * dev,u8 command,bool flush)164 static int diolan_usb_cmd(struct i2c_diolan_u2c *dev, u8 command, bool flush)
165 {
166 	dev->obuffer[dev->olen++] = command;
167 	dev->ocount++;
168 	return diolan_write_cmd(dev, flush);
169 }
170 
171 /* Send command with one byte of data */
diolan_usb_cmd_data(struct i2c_diolan_u2c * dev,u8 command,u8 data,bool flush)172 static int diolan_usb_cmd_data(struct i2c_diolan_u2c *dev, u8 command, u8 data,
173 			       bool flush)
174 {
175 	dev->obuffer[dev->olen++] = command;
176 	dev->obuffer[dev->olen++] = data;
177 	dev->ocount++;
178 	return diolan_write_cmd(dev, flush);
179 }
180 
181 /* Send command with two bytes of data */
diolan_usb_cmd_data2(struct i2c_diolan_u2c * dev,u8 command,u8 d1,u8 d2,bool flush)182 static int diolan_usb_cmd_data2(struct i2c_diolan_u2c *dev, u8 command, u8 d1,
183 				u8 d2, bool flush)
184 {
185 	dev->obuffer[dev->olen++] = command;
186 	dev->obuffer[dev->olen++] = d1;
187 	dev->obuffer[dev->olen++] = d2;
188 	dev->ocount++;
189 	return diolan_write_cmd(dev, flush);
190 }
191 
192 /*
193  * Flush input queue.
194  * If we don't do this at startup and the controller has queued up
195  * messages which were not retrieved, it will stop responding
196  * at some point.
197  */
diolan_flush_input(struct i2c_diolan_u2c * dev)198 static void diolan_flush_input(struct i2c_diolan_u2c *dev)
199 {
200 	int i;
201 
202 	for (i = 0; i < 10; i++) {
203 		int actual = 0;
204 		int ret;
205 
206 		ret = usb_bulk_msg(dev->usb_dev,
207 				   usb_rcvbulkpipe(dev->usb_dev, dev->ep_in),
208 				   dev->ibuffer, sizeof(dev->ibuffer), &actual,
209 				   DIOLAN_USB_TIMEOUT);
210 		if (ret < 0 || actual == 0)
211 			break;
212 	}
213 	if (i == 10)
214 		dev_err(&dev->interface->dev, "Failed to flush input buffer\n");
215 }
216 
diolan_i2c_start(struct i2c_diolan_u2c * dev)217 static int diolan_i2c_start(struct i2c_diolan_u2c *dev)
218 {
219 	return diolan_usb_cmd(dev, CMD_I2C_START, false);
220 }
221 
diolan_i2c_repeated_start(struct i2c_diolan_u2c * dev)222 static int diolan_i2c_repeated_start(struct i2c_diolan_u2c *dev)
223 {
224 	return diolan_usb_cmd(dev, CMD_I2C_REPEATED_START, false);
225 }
226 
diolan_i2c_stop(struct i2c_diolan_u2c * dev)227 static int diolan_i2c_stop(struct i2c_diolan_u2c *dev)
228 {
229 	return diolan_usb_cmd(dev, CMD_I2C_STOP, true);
230 }
231 
diolan_i2c_get_byte_ack(struct i2c_diolan_u2c * dev,bool ack,u8 * byte)232 static int diolan_i2c_get_byte_ack(struct i2c_diolan_u2c *dev, bool ack,
233 				   u8 *byte)
234 {
235 	int ret;
236 
237 	ret = diolan_usb_cmd_data(dev, CMD_I2C_GET_BYTE_ACK, ack, true);
238 	if (ret > 0)
239 		*byte = dev->ibuffer[0];
240 	else if (ret == 0)
241 		ret = -EIO;
242 
243 	return ret;
244 }
245 
diolan_i2c_put_byte_ack(struct i2c_diolan_u2c * dev,u8 byte)246 static int diolan_i2c_put_byte_ack(struct i2c_diolan_u2c *dev, u8 byte)
247 {
248 	return diolan_usb_cmd_data(dev, CMD_I2C_PUT_BYTE_ACK, byte, false);
249 }
250 
diolan_set_speed(struct i2c_diolan_u2c * dev,u8 speed)251 static int diolan_set_speed(struct i2c_diolan_u2c *dev, u8 speed)
252 {
253 	return diolan_usb_cmd_data(dev, CMD_I2C_SET_SPEED, speed, true);
254 }
255 
256 /* Enable or disable clock synchronization (stretching) */
diolan_set_clock_synch(struct i2c_diolan_u2c * dev,bool enable)257 static int diolan_set_clock_synch(struct i2c_diolan_u2c *dev, bool enable)
258 {
259 	return diolan_usb_cmd_data(dev, CMD_I2C_SET_CLK_SYNC, enable, true);
260 }
261 
262 /* Set clock synchronization timeout in ms */
diolan_set_clock_synch_timeout(struct i2c_diolan_u2c * dev,int ms)263 static int diolan_set_clock_synch_timeout(struct i2c_diolan_u2c *dev, int ms)
264 {
265 	int to_val = ms * 10;
266 
267 	return diolan_usb_cmd_data2(dev, CMD_I2C_SET_CLK_SYNC_TO,
268 				    to_val & 0xff, (to_val >> 8) & 0xff, true);
269 }
270 
diolan_fw_version(struct i2c_diolan_u2c * dev)271 static void diolan_fw_version(struct i2c_diolan_u2c *dev)
272 {
273 	int ret;
274 
275 	ret = diolan_usb_cmd(dev, CMD_GET_FW_VERSION, true);
276 	if (ret >= 2)
277 		dev_info(&dev->interface->dev,
278 			 "Diolan U2C firmware version %u.%u\n",
279 			 (unsigned int)dev->ibuffer[0],
280 			 (unsigned int)dev->ibuffer[1]);
281 }
282 
diolan_get_serial(struct i2c_diolan_u2c * dev)283 static void diolan_get_serial(struct i2c_diolan_u2c *dev)
284 {
285 	int ret;
286 	u32 serial;
287 
288 	ret = diolan_usb_cmd(dev, CMD_GET_SERIAL, true);
289 	if (ret >= 4) {
290 		serial = le32_to_cpu(*(u32 *)dev->ibuffer);
291 		dev_info(&dev->interface->dev,
292 			 "Diolan U2C serial number %u\n", serial);
293 	}
294 }
295 
diolan_init(struct i2c_diolan_u2c * dev)296 static int diolan_init(struct i2c_diolan_u2c *dev)
297 {
298 	int speed, ret;
299 
300 	if (frequency >= 2 * I2C_MAX_STANDARD_MODE_FREQ) {
301 		speed = U2C_I2C_SPEED_FAST;
302 		frequency = I2C_MAX_FAST_MODE_FREQ;
303 	} else if (frequency >= I2C_MAX_STANDARD_MODE_FREQ || frequency == 0) {
304 		speed = U2C_I2C_SPEED_STD;
305 		frequency = I2C_MAX_STANDARD_MODE_FREQ;
306 	} else {
307 		speed = U2C_I2C_SPEED(frequency);
308 		if (speed > U2C_I2C_SPEED_2KHZ)
309 			speed = U2C_I2C_SPEED_2KHZ;
310 		frequency = U2C_I2C_FREQ(speed);
311 	}
312 
313 	dev_info(&dev->interface->dev,
314 		 "Diolan U2C at USB bus %03d address %03d speed %d Hz\n",
315 		 dev->usb_dev->bus->busnum, dev->usb_dev->devnum, frequency);
316 
317 	diolan_flush_input(dev);
318 	diolan_fw_version(dev);
319 	diolan_get_serial(dev);
320 
321 	/* Set I2C speed */
322 	ret = diolan_set_speed(dev, speed);
323 	if (ret < 0)
324 		return ret;
325 
326 	/* Configure I2C clock synchronization */
327 	ret = diolan_set_clock_synch(dev, speed != U2C_I2C_SPEED_FAST);
328 	if (ret < 0)
329 		return ret;
330 
331 	if (speed != U2C_I2C_SPEED_FAST)
332 		ret = diolan_set_clock_synch_timeout(dev, DIOLAN_SYNC_TIMEOUT);
333 
334 	return ret;
335 }
336 
337 /* i2c layer */
338 
diolan_usb_xfer(struct i2c_adapter * adapter,struct i2c_msg * msgs,int num)339 static int diolan_usb_xfer(struct i2c_adapter *adapter, struct i2c_msg *msgs,
340 			   int num)
341 {
342 	struct i2c_diolan_u2c *dev = i2c_get_adapdata(adapter);
343 	struct i2c_msg *pmsg;
344 	int i, j;
345 	int ret, sret;
346 
347 	ret = diolan_i2c_start(dev);
348 	if (ret < 0)
349 		return ret;
350 
351 	for (i = 0; i < num; i++) {
352 		pmsg = &msgs[i];
353 		if (i) {
354 			ret = diolan_i2c_repeated_start(dev);
355 			if (ret < 0)
356 				goto abort;
357 		}
358 		ret = diolan_i2c_put_byte_ack(dev,
359 					      i2c_8bit_addr_from_msg(pmsg));
360 		if (ret < 0)
361 			goto abort;
362 		if (pmsg->flags & I2C_M_RD) {
363 			for (j = 0; j < pmsg->len; j++) {
364 				u8 byte;
365 				bool ack = j < pmsg->len - 1;
366 
367 				/*
368 				 * Don't send NACK if this is the first byte
369 				 * of a SMBUS_BLOCK message.
370 				 */
371 				if (j == 0 && (pmsg->flags & I2C_M_RECV_LEN))
372 					ack = true;
373 
374 				ret = diolan_i2c_get_byte_ack(dev, ack, &byte);
375 				if (ret < 0)
376 					goto abort;
377 				/*
378 				 * Adjust count if first received byte is length
379 				 */
380 				if (j == 0 && (pmsg->flags & I2C_M_RECV_LEN)) {
381 					if (byte == 0
382 					    || byte > I2C_SMBUS_BLOCK_MAX) {
383 						ret = -EPROTO;
384 						goto abort;
385 					}
386 					pmsg->len += byte;
387 				}
388 				pmsg->buf[j] = byte;
389 			}
390 		} else {
391 			for (j = 0; j < pmsg->len; j++) {
392 				ret = diolan_i2c_put_byte_ack(dev,
393 							      pmsg->buf[j]);
394 				if (ret < 0)
395 					goto abort;
396 			}
397 		}
398 	}
399 	ret = num;
400 abort:
401 	sret = diolan_i2c_stop(dev);
402 	if (sret < 0 && ret >= 0)
403 		ret = sret;
404 	return ret;
405 }
406 
407 /*
408  * Return list of supported functionality.
409  */
diolan_usb_func(struct i2c_adapter * a)410 static u32 diolan_usb_func(struct i2c_adapter *a)
411 {
412 	return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL |
413 	       I2C_FUNC_SMBUS_READ_BLOCK_DATA | I2C_FUNC_SMBUS_BLOCK_PROC_CALL;
414 }
415 
416 static const struct i2c_algorithm diolan_usb_algorithm = {
417 	.master_xfer = diolan_usb_xfer,
418 	.functionality = diolan_usb_func,
419 };
420 
421 /* device layer */
422 
423 static const struct usb_device_id diolan_u2c_table[] = {
424 	{ USB_DEVICE(USB_VENDOR_ID_DIOLAN, USB_DEVICE_ID_DIOLAN_U2C) },
425 	{ }
426 };
427 
428 MODULE_DEVICE_TABLE(usb, diolan_u2c_table);
429 
diolan_u2c_free(struct i2c_diolan_u2c * dev)430 static void diolan_u2c_free(struct i2c_diolan_u2c *dev)
431 {
432 	usb_put_dev(dev->usb_dev);
433 	kfree(dev);
434 }
435 
diolan_u2c_probe(struct usb_interface * interface,const struct usb_device_id * id)436 static int diolan_u2c_probe(struct usb_interface *interface,
437 			    const struct usb_device_id *id)
438 {
439 	struct usb_host_interface *hostif = interface->cur_altsetting;
440 	struct i2c_diolan_u2c *dev;
441 	int ret;
442 
443 	if (hostif->desc.bInterfaceNumber != 0
444 	    || hostif->desc.bNumEndpoints < 2)
445 		return -ENODEV;
446 
447 	/* allocate memory for our device state and initialize it */
448 	dev = kzalloc(sizeof(*dev), GFP_KERNEL);
449 	if (dev == NULL) {
450 		ret = -ENOMEM;
451 		goto error;
452 	}
453 	dev->ep_out = hostif->endpoint[0].desc.bEndpointAddress;
454 	dev->ep_in = hostif->endpoint[1].desc.bEndpointAddress;
455 
456 	dev->usb_dev = usb_get_dev(interface_to_usbdev(interface));
457 	dev->interface = interface;
458 
459 	/* save our data pointer in this interface device */
460 	usb_set_intfdata(interface, dev);
461 
462 	/* setup i2c adapter description */
463 	dev->adapter.owner = THIS_MODULE;
464 	dev->adapter.class = I2C_CLASS_HWMON;
465 	dev->adapter.algo = &diolan_usb_algorithm;
466 	i2c_set_adapdata(&dev->adapter, dev);
467 	snprintf(dev->adapter.name, sizeof(dev->adapter.name),
468 		 DRIVER_NAME " at bus %03d device %03d",
469 		 dev->usb_dev->bus->busnum, dev->usb_dev->devnum);
470 
471 	dev->adapter.dev.parent = &dev->interface->dev;
472 
473 	/* initialize diolan i2c interface */
474 	ret = diolan_init(dev);
475 	if (ret < 0) {
476 		dev_err(&interface->dev, "failed to initialize adapter\n");
477 		goto error_free;
478 	}
479 
480 	/* and finally attach to i2c layer */
481 	ret = i2c_add_adapter(&dev->adapter);
482 	if (ret < 0)
483 		goto error_free;
484 
485 	dev_dbg(&interface->dev, "connected " DRIVER_NAME "\n");
486 
487 	return 0;
488 
489 error_free:
490 	usb_set_intfdata(interface, NULL);
491 	diolan_u2c_free(dev);
492 error:
493 	return ret;
494 }
495 
diolan_u2c_disconnect(struct usb_interface * interface)496 static void diolan_u2c_disconnect(struct usb_interface *interface)
497 {
498 	struct i2c_diolan_u2c *dev = usb_get_intfdata(interface);
499 
500 	i2c_del_adapter(&dev->adapter);
501 	usb_set_intfdata(interface, NULL);
502 	diolan_u2c_free(dev);
503 
504 	dev_dbg(&interface->dev, "disconnected\n");
505 }
506 
507 static struct usb_driver diolan_u2c_driver = {
508 	.name = DRIVER_NAME,
509 	.probe = diolan_u2c_probe,
510 	.disconnect = diolan_u2c_disconnect,
511 	.id_table = diolan_u2c_table,
512 };
513 
514 module_usb_driver(diolan_u2c_driver);
515 
516 MODULE_AUTHOR("Guenter Roeck <linux@roeck-us.net>");
517 MODULE_DESCRIPTION(DRIVER_NAME " driver");
518 MODULE_LICENSE("GPL");
519