xref: /openbmc/linux/drivers/usb/misc/usb251xb.c (revision 9a8f3203)
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Driver for Microchip USB251xB USB 2.0 Hi-Speed Hub Controller
4  * Configuration via SMBus.
5  *
6  * Copyright (c) 2017 SKIDATA AG
7  *
8  * This work is based on the USB3503 driver by Dongjin Kim and
9  * a not-accepted patch by Fabien Lahoudere, see:
10  * https://patchwork.kernel.org/patch/9257715/
11  */
12 
13 #include <linux/delay.h>
14 #include <linux/gpio/consumer.h>
15 #include <linux/i2c.h>
16 #include <linux/module.h>
17 #include <linux/nls.h>
18 #include <linux/of_device.h>
19 #include <linux/slab.h>
20 
21 /* Internal Register Set Addresses & Default Values acc. to DS00001692C */
22 #define USB251XB_ADDR_VENDOR_ID_LSB	0x00
23 #define USB251XB_ADDR_VENDOR_ID_MSB	0x01
24 #define USB251XB_DEF_VENDOR_ID		0x0424
25 
26 #define USB251XB_ADDR_PRODUCT_ID_LSB	0x02
27 #define USB251XB_ADDR_PRODUCT_ID_MSB	0x03
28 #define USB251XB_DEF_PRODUCT_ID_12	0x2512 /* USB2512B/12Bi */
29 #define USB251XB_DEF_PRODUCT_ID_13	0x2513 /* USB2513B/13Bi */
30 #define USB251XB_DEF_PRODUCT_ID_14	0x2514 /* USB2514B/14Bi */
31 #define USB251XB_DEF_PRODUCT_ID_17	0x2517 /* USB2517/17i */
32 
33 #define USB251XB_ADDR_DEVICE_ID_LSB	0x04
34 #define USB251XB_ADDR_DEVICE_ID_MSB	0x05
35 #define USB251XB_DEF_DEVICE_ID		0x0BB3
36 
37 #define USB251XB_ADDR_CONFIG_DATA_1	0x06
38 #define USB251XB_DEF_CONFIG_DATA_1	0x9B
39 #define USB251XB_ADDR_CONFIG_DATA_2	0x07
40 #define USB251XB_DEF_CONFIG_DATA_2	0x20
41 #define USB251XB_ADDR_CONFIG_DATA_3	0x08
42 #define USB251XB_DEF_CONFIG_DATA_3	0x02
43 
44 #define USB251XB_ADDR_NON_REMOVABLE_DEVICES	0x09
45 #define USB251XB_DEF_NON_REMOVABLE_DEVICES	0x00
46 
47 #define USB251XB_ADDR_PORT_DISABLE_SELF	0x0A
48 #define USB251XB_DEF_PORT_DISABLE_SELF	0x00
49 #define USB251XB_ADDR_PORT_DISABLE_BUS	0x0B
50 #define USB251XB_DEF_PORT_DISABLE_BUS	0x00
51 
52 #define USB251XB_ADDR_MAX_POWER_SELF	0x0C
53 #define USB251XB_DEF_MAX_POWER_SELF	0x01
54 #define USB251XB_ADDR_MAX_POWER_BUS	0x0D
55 #define USB251XB_DEF_MAX_POWER_BUS	0x32
56 
57 #define USB251XB_ADDR_MAX_CURRENT_SELF	0x0E
58 #define USB251XB_DEF_MAX_CURRENT_SELF	0x01
59 #define USB251XB_ADDR_MAX_CURRENT_BUS	0x0F
60 #define USB251XB_DEF_MAX_CURRENT_BUS	0x32
61 
62 #define USB251XB_ADDR_POWER_ON_TIME	0x10
63 #define USB251XB_DEF_POWER_ON_TIME	0x32
64 
65 #define USB251XB_ADDR_LANGUAGE_ID_HIGH	0x11
66 #define USB251XB_ADDR_LANGUAGE_ID_LOW	0x12
67 #define USB251XB_DEF_LANGUAGE_ID	0x0000
68 
69 #define USB251XB_STRING_BUFSIZE			62
70 #define USB251XB_ADDR_MANUFACTURER_STRING_LEN	0x13
71 #define USB251XB_ADDR_MANUFACTURER_STRING	0x16
72 #define USB251XB_DEF_MANUFACTURER_STRING	"Microchip"
73 
74 #define USB251XB_ADDR_PRODUCT_STRING_LEN	0x14
75 #define USB251XB_ADDR_PRODUCT_STRING		0x54
76 #define USB251XB_DEF_PRODUCT_STRING		"USB251xB/xBi/7i"
77 
78 #define USB251XB_ADDR_SERIAL_STRING_LEN		0x15
79 #define USB251XB_ADDR_SERIAL_STRING		0x92
80 #define USB251XB_DEF_SERIAL_STRING		""
81 
82 #define USB251XB_ADDR_BATTERY_CHARGING_ENABLE	0xD0
83 #define USB251XB_DEF_BATTERY_CHARGING_ENABLE	0x00
84 
85 #define USB251XB_ADDR_BOOST_UP	0xF6
86 #define USB251XB_DEF_BOOST_UP	0x00
87 #define USB251XB_ADDR_BOOST_57	0xF7
88 #define USB251XB_DEF_BOOST_57	0x00
89 #define USB251XB_ADDR_BOOST_14	0xF8
90 #define USB251XB_DEF_BOOST_14	0x00
91 
92 #define USB251XB_ADDR_PORT_SWAP	0xFA
93 #define USB251XB_DEF_PORT_SWAP	0x00
94 
95 #define USB251XB_ADDR_PORT_MAP_12	0xFB
96 #define USB251XB_DEF_PORT_MAP_12	0x00
97 #define USB251XB_ADDR_PORT_MAP_34	0xFC
98 #define USB251XB_DEF_PORT_MAP_34	0x00 /* USB251{3B/i,4B/i,7/i} only */
99 #define USB251XB_ADDR_PORT_MAP_56	0xFD
100 #define USB251XB_DEF_PORT_MAP_56	0x00 /* USB2517/i only */
101 #define USB251XB_ADDR_PORT_MAP_7	0xFE
102 #define USB251XB_DEF_PORT_MAP_7		0x00 /* USB2517/i only */
103 
104 #define USB251XB_ADDR_STATUS_COMMAND		0xFF
105 #define USB251XB_STATUS_COMMAND_SMBUS_DOWN	0x04
106 #define USB251XB_STATUS_COMMAND_RESET		0x02
107 #define USB251XB_STATUS_COMMAND_ATTACH		0x01
108 
109 #define USB251XB_I2C_REG_SZ	0x100
110 #define USB251XB_I2C_WRITE_SZ	0x10
111 
112 #define DRIVER_NAME	"usb251xb"
113 #define DRIVER_DESC	"Microchip USB 2.0 Hi-Speed Hub Controller"
114 
115 struct usb251xb {
116 	struct device *dev;
117 	struct i2c_client *i2c;
118 	u8 skip_config;
119 	struct gpio_desc *gpio_reset;
120 	u16 vendor_id;
121 	u16 product_id;
122 	u16 device_id;
123 	u8  conf_data1;
124 	u8  conf_data2;
125 	u8  conf_data3;
126 	u8  non_rem_dev;
127 	u8  port_disable_sp;
128 	u8  port_disable_bp;
129 	u8  max_power_sp;
130 	u8  max_power_bp;
131 	u8  max_current_sp;
132 	u8  max_current_bp;
133 	u8  power_on_time;
134 	u16 lang_id;
135 	u8 manufacturer_len;
136 	u8 product_len;
137 	u8 serial_len;
138 	char manufacturer[USB251XB_STRING_BUFSIZE];
139 	char product[USB251XB_STRING_BUFSIZE];
140 	char serial[USB251XB_STRING_BUFSIZE];
141 	u8  bat_charge_en;
142 	u8  boost_up;
143 	u8  boost_57;
144 	u8  boost_14;
145 	u8  port_swap;
146 	u8  port_map12;
147 	u8  port_map34;
148 	u8  port_map56;
149 	u8  port_map7;
150 	u8  status;
151 };
152 
153 struct usb251xb_data {
154 	u16 product_id;
155 	u8 port_cnt;
156 	bool led_support;
157 	bool bat_support;
158 	char product_str[USB251XB_STRING_BUFSIZE / 2]; /* ASCII string */
159 };
160 
161 static const struct usb251xb_data usb2512b_data = {
162 	.product_id = 0x2512,
163 	.port_cnt = 2,
164 	.led_support = false,
165 	.bat_support = true,
166 	.product_str = "USB2512B",
167 };
168 
169 static const struct usb251xb_data usb2512bi_data = {
170 	.product_id = 0x2512,
171 	.port_cnt = 2,
172 	.led_support = false,
173 	.bat_support = true,
174 	.product_str = "USB2512Bi",
175 };
176 
177 static const struct usb251xb_data usb2513b_data = {
178 	.product_id = 0x2513,
179 	.port_cnt = 3,
180 	.led_support = false,
181 	.bat_support = true,
182 	.product_str = "USB2513B",
183 };
184 
185 static const struct usb251xb_data usb2513bi_data = {
186 	.product_id = 0x2513,
187 	.port_cnt = 3,
188 	.led_support = false,
189 	.bat_support = true,
190 	.product_str = "USB2513Bi",
191 };
192 
193 static const struct usb251xb_data usb2514b_data = {
194 	.product_id = 0x2514,
195 	.port_cnt = 4,
196 	.led_support = false,
197 	.bat_support = true,
198 	.product_str = "USB2514B",
199 };
200 
201 static const struct usb251xb_data usb2514bi_data = {
202 	.product_id = 0x2514,
203 	.port_cnt = 4,
204 	.led_support = false,
205 	.bat_support = true,
206 	.product_str = "USB2514Bi",
207 };
208 
209 static const struct usb251xb_data usb2517_data = {
210 	.product_id = 0x2517,
211 	.port_cnt = 7,
212 	.led_support = true,
213 	.bat_support = false,
214 	.product_str = "USB2517",
215 };
216 
217 static const struct usb251xb_data usb2517i_data = {
218 	.product_id = 0x2517,
219 	.port_cnt = 7,
220 	.led_support = true,
221 	.bat_support = false,
222 	.product_str = "USB2517i",
223 };
224 
225 static void usb251xb_reset(struct usb251xb *hub, int state)
226 {
227 	if (!hub->gpio_reset)
228 		return;
229 
230 	gpiod_set_value_cansleep(hub->gpio_reset, state);
231 
232 	/* wait for hub recovery/stabilization */
233 	if (!state)
234 		usleep_range(500, 750);	/* >=500us at power on */
235 	else
236 		usleep_range(1, 10);	/* >=1us at power down */
237 }
238 
239 static int usb251xb_connect(struct usb251xb *hub)
240 {
241 	struct device *dev = hub->dev;
242 	int err, i;
243 	char i2c_wb[USB251XB_I2C_REG_SZ];
244 
245 	memset(i2c_wb, 0, USB251XB_I2C_REG_SZ);
246 
247 	if (hub->skip_config) {
248 		dev_info(dev, "Skip hub configuration, only attach.\n");
249 		i2c_wb[0] = 0x01;
250 		i2c_wb[1] = USB251XB_STATUS_COMMAND_ATTACH;
251 
252 		usb251xb_reset(hub, 0);
253 
254 		err = i2c_smbus_write_i2c_block_data(hub->i2c,
255 				USB251XB_ADDR_STATUS_COMMAND, 2, i2c_wb);
256 		if (err) {
257 			dev_err(dev, "attaching hub failed: %d\n", err);
258 			return err;
259 		}
260 		return 0;
261 	}
262 
263 	i2c_wb[USB251XB_ADDR_VENDOR_ID_MSB]     = (hub->vendor_id >> 8) & 0xFF;
264 	i2c_wb[USB251XB_ADDR_VENDOR_ID_LSB]     = hub->vendor_id & 0xFF;
265 	i2c_wb[USB251XB_ADDR_PRODUCT_ID_MSB]    = (hub->product_id >> 8) & 0xFF;
266 	i2c_wb[USB251XB_ADDR_PRODUCT_ID_LSB]    = hub->product_id & 0xFF;
267 	i2c_wb[USB251XB_ADDR_DEVICE_ID_MSB]     = (hub->device_id >> 8) & 0xFF;
268 	i2c_wb[USB251XB_ADDR_DEVICE_ID_LSB]     = hub->device_id & 0xFF;
269 	i2c_wb[USB251XB_ADDR_CONFIG_DATA_1]     = hub->conf_data1;
270 	i2c_wb[USB251XB_ADDR_CONFIG_DATA_2]     = hub->conf_data2;
271 	i2c_wb[USB251XB_ADDR_CONFIG_DATA_3]     = hub->conf_data3;
272 	i2c_wb[USB251XB_ADDR_NON_REMOVABLE_DEVICES] = hub->non_rem_dev;
273 	i2c_wb[USB251XB_ADDR_PORT_DISABLE_SELF] = hub->port_disable_sp;
274 	i2c_wb[USB251XB_ADDR_PORT_DISABLE_BUS]  = hub->port_disable_bp;
275 	i2c_wb[USB251XB_ADDR_MAX_POWER_SELF]    = hub->max_power_sp;
276 	i2c_wb[USB251XB_ADDR_MAX_POWER_BUS]     = hub->max_power_bp;
277 	i2c_wb[USB251XB_ADDR_MAX_CURRENT_SELF]  = hub->max_current_sp;
278 	i2c_wb[USB251XB_ADDR_MAX_CURRENT_BUS]   = hub->max_current_bp;
279 	i2c_wb[USB251XB_ADDR_POWER_ON_TIME]     = hub->power_on_time;
280 	i2c_wb[USB251XB_ADDR_LANGUAGE_ID_HIGH]  = (hub->lang_id >> 8) & 0xFF;
281 	i2c_wb[USB251XB_ADDR_LANGUAGE_ID_LOW]   = hub->lang_id & 0xFF;
282 	i2c_wb[USB251XB_ADDR_MANUFACTURER_STRING_LEN] = hub->manufacturer_len;
283 	i2c_wb[USB251XB_ADDR_PRODUCT_STRING_LEN]      = hub->product_len;
284 	i2c_wb[USB251XB_ADDR_SERIAL_STRING_LEN]       = hub->serial_len;
285 	memcpy(&i2c_wb[USB251XB_ADDR_MANUFACTURER_STRING], hub->manufacturer,
286 	       USB251XB_STRING_BUFSIZE);
287 	memcpy(&i2c_wb[USB251XB_ADDR_SERIAL_STRING], hub->serial,
288 	       USB251XB_STRING_BUFSIZE);
289 	memcpy(&i2c_wb[USB251XB_ADDR_PRODUCT_STRING], hub->product,
290 	       USB251XB_STRING_BUFSIZE);
291 	i2c_wb[USB251XB_ADDR_BATTERY_CHARGING_ENABLE] = hub->bat_charge_en;
292 	i2c_wb[USB251XB_ADDR_BOOST_UP]          = hub->boost_up;
293 	i2c_wb[USB251XB_ADDR_BOOST_57]          = hub->boost_57;
294 	i2c_wb[USB251XB_ADDR_BOOST_14]          = hub->boost_14;
295 	i2c_wb[USB251XB_ADDR_PORT_SWAP]         = hub->port_swap;
296 	i2c_wb[USB251XB_ADDR_PORT_MAP_12]       = hub->port_map12;
297 	i2c_wb[USB251XB_ADDR_PORT_MAP_34]       = hub->port_map34;
298 	i2c_wb[USB251XB_ADDR_PORT_MAP_56]       = hub->port_map56;
299 	i2c_wb[USB251XB_ADDR_PORT_MAP_7]        = hub->port_map7;
300 	i2c_wb[USB251XB_ADDR_STATUS_COMMAND] = USB251XB_STATUS_COMMAND_ATTACH;
301 
302 	usb251xb_reset(hub, 0);
303 
304 	/* write registers */
305 	for (i = 0; i < (USB251XB_I2C_REG_SZ / USB251XB_I2C_WRITE_SZ); i++) {
306 		int offset = i * USB251XB_I2C_WRITE_SZ;
307 		char wbuf[USB251XB_I2C_WRITE_SZ + 1];
308 
309 		/* The first data byte transferred tells the hub how many data
310 		 * bytes will follow (byte count).
311 		 */
312 		wbuf[0] = USB251XB_I2C_WRITE_SZ;
313 		memcpy(&wbuf[1], &i2c_wb[offset], USB251XB_I2C_WRITE_SZ);
314 
315 		dev_dbg(dev, "writing %d byte block %d to 0x%02X\n",
316 			USB251XB_I2C_WRITE_SZ, i, offset);
317 
318 		err = i2c_smbus_write_i2c_block_data(hub->i2c, offset,
319 						     USB251XB_I2C_WRITE_SZ + 1,
320 						     wbuf);
321 		if (err)
322 			goto out_err;
323 	}
324 
325 	dev_info(dev, "Hub configuration was successful.\n");
326 	return 0;
327 
328 out_err:
329 	dev_err(dev, "configuring block %d failed: %d\n", i, err);
330 	return err;
331 }
332 
333 #ifdef CONFIG_OF
334 static int usb251xb_get_ofdata(struct usb251xb *hub,
335 			       struct usb251xb_data *data)
336 {
337 	struct device *dev = hub->dev;
338 	struct device_node *np = dev->of_node;
339 	int len, err, i;
340 	u32 port, property_u32 = 0;
341 	const u32 *cproperty_u32;
342 	const char *cproperty_char;
343 	char str[USB251XB_STRING_BUFSIZE / 2];
344 	struct property *prop;
345 	const __be32 *p;
346 
347 	if (!np) {
348 		dev_err(dev, "failed to get ofdata\n");
349 		return -ENODEV;
350 	}
351 
352 	if (of_get_property(np, "skip-config", NULL))
353 		hub->skip_config = 1;
354 	else
355 		hub->skip_config = 0;
356 
357 	hub->gpio_reset = devm_gpiod_get_optional(dev, "reset", GPIOD_OUT_HIGH);
358 	if (PTR_ERR(hub->gpio_reset) == -EPROBE_DEFER) {
359 		return -EPROBE_DEFER;
360 	} else if (IS_ERR(hub->gpio_reset)) {
361 		err = PTR_ERR(hub->gpio_reset);
362 		dev_err(dev, "unable to request GPIO reset pin (%d)\n", err);
363 		return err;
364 	}
365 
366 	if (of_property_read_u16_array(np, "vendor-id", &hub->vendor_id, 1))
367 		hub->vendor_id = USB251XB_DEF_VENDOR_ID;
368 
369 	if (of_property_read_u16_array(np, "product-id",
370 				       &hub->product_id, 1))
371 		hub->product_id = data->product_id;
372 
373 	if (of_property_read_u16_array(np, "device-id", &hub->device_id, 1))
374 		hub->device_id = USB251XB_DEF_DEVICE_ID;
375 
376 	hub->conf_data1 = USB251XB_DEF_CONFIG_DATA_1;
377 	if (of_get_property(np, "self-powered", NULL)) {
378 		hub->conf_data1 |= BIT(7);
379 
380 		/* Configure Over-Current sens when self-powered */
381 		hub->conf_data1 &= ~BIT(2);
382 		if (of_get_property(np, "ganged-sensing", NULL))
383 			hub->conf_data1 &= ~BIT(1);
384 		else if (of_get_property(np, "individual-sensing", NULL))
385 			hub->conf_data1 |= BIT(1);
386 	} else if (of_get_property(np, "bus-powered", NULL)) {
387 		hub->conf_data1 &= ~BIT(7);
388 
389 		/* Disable Over-Current sense when bus-powered */
390 		hub->conf_data1 |= BIT(2);
391 	}
392 
393 	if (of_get_property(np, "disable-hi-speed", NULL))
394 		hub->conf_data1 |= BIT(5);
395 
396 	if (of_get_property(np, "multi-tt", NULL))
397 		hub->conf_data1 |= BIT(4);
398 	else if (of_get_property(np, "single-tt", NULL))
399 		hub->conf_data1 &= ~BIT(4);
400 
401 	if (of_get_property(np, "disable-eop", NULL))
402 		hub->conf_data1 |= BIT(3);
403 
404 	if (of_get_property(np, "individual-port-switching", NULL))
405 		hub->conf_data1 |= BIT(0);
406 	else if (of_get_property(np, "ganged-port-switching", NULL))
407 		hub->conf_data1 &= ~BIT(0);
408 
409 	hub->conf_data2 = USB251XB_DEF_CONFIG_DATA_2;
410 	if (of_get_property(np, "dynamic-power-switching", NULL))
411 		hub->conf_data2 |= BIT(7);
412 
413 	if (!of_property_read_u32(np, "oc-delay-us", &property_u32)) {
414 		if (property_u32 == 100) {
415 			/* 100 us*/
416 			hub->conf_data2 &= ~BIT(5);
417 			hub->conf_data2 &= ~BIT(4);
418 		} else if (property_u32 == 4000) {
419 			/* 4 ms */
420 			hub->conf_data2 &= ~BIT(5);
421 			hub->conf_data2 |= BIT(4);
422 		} else if (property_u32 == 16000) {
423 			/* 16 ms */
424 			hub->conf_data2 |= BIT(5);
425 			hub->conf_data2 |= BIT(4);
426 		} else {
427 			/* 8 ms (DEFAULT) */
428 			hub->conf_data2 |= BIT(5);
429 			hub->conf_data2 &= ~BIT(4);
430 		}
431 	}
432 
433 	if (of_get_property(np, "compound-device", NULL))
434 		hub->conf_data2 |= BIT(3);
435 
436 	hub->conf_data3 = USB251XB_DEF_CONFIG_DATA_3;
437 	if (of_get_property(np, "port-mapping-mode", NULL))
438 		hub->conf_data3 |= BIT(3);
439 
440 	if (data->led_support && of_get_property(np, "led-usb-mode", NULL))
441 		hub->conf_data3 &= ~BIT(1);
442 
443 	if (of_get_property(np, "string-support", NULL))
444 		hub->conf_data3 |= BIT(0);
445 
446 	hub->non_rem_dev = USB251XB_DEF_NON_REMOVABLE_DEVICES;
447 	cproperty_u32 = of_get_property(np, "non-removable-ports", &len);
448 	if (cproperty_u32 && (len / sizeof(u32)) > 0) {
449 		for (i = 0; i < len / sizeof(u32); i++) {
450 			u32 port = be32_to_cpu(cproperty_u32[i]);
451 
452 			if ((port >= 1) && (port <= data->port_cnt))
453 				hub->non_rem_dev |= BIT(port);
454 			else
455 				dev_warn(dev, "NRD port %u doesn't exist\n",
456 					port);
457 		}
458 	}
459 
460 	hub->port_disable_sp = USB251XB_DEF_PORT_DISABLE_SELF;
461 	cproperty_u32 = of_get_property(np, "sp-disabled-ports", &len);
462 	if (cproperty_u32 && (len / sizeof(u32)) > 0) {
463 		for (i = 0; i < len / sizeof(u32); i++) {
464 			u32 port = be32_to_cpu(cproperty_u32[i]);
465 
466 			if ((port >= 1) && (port <= data->port_cnt))
467 				hub->port_disable_sp |= BIT(port);
468 			else
469 				dev_warn(dev, "PDS port %u doesn't exist\n",
470 					port);
471 		}
472 	}
473 
474 	hub->port_disable_bp = USB251XB_DEF_PORT_DISABLE_BUS;
475 	cproperty_u32 = of_get_property(np, "bp-disabled-ports", &len);
476 	if (cproperty_u32 && (len / sizeof(u32)) > 0) {
477 		for (i = 0; i < len / sizeof(u32); i++) {
478 			u32 port = be32_to_cpu(cproperty_u32[i]);
479 
480 			if ((port >= 1) && (port <= data->port_cnt))
481 				hub->port_disable_bp |= BIT(port);
482 			else
483 				dev_warn(dev, "PDB port %u doesn't exist\n",
484 					port);
485 		}
486 	}
487 
488 	hub->max_power_sp = USB251XB_DEF_MAX_POWER_SELF;
489 	if (!of_property_read_u32(np, "sp-max-total-current-microamp",
490 	    &property_u32))
491 		hub->max_power_sp = min_t(u8, property_u32 / 2000, 50);
492 
493 	hub->max_power_bp = USB251XB_DEF_MAX_POWER_BUS;
494 	if (!of_property_read_u32(np, "bp-max-total-current-microamp",
495 	    &property_u32))
496 		hub->max_power_bp = min_t(u8, property_u32 / 2000, 255);
497 
498 	hub->max_current_sp = USB251XB_DEF_MAX_CURRENT_SELF;
499 	if (!of_property_read_u32(np, "sp-max-removable-current-microamp",
500 	    &property_u32))
501 		hub->max_current_sp = min_t(u8, property_u32 / 2000, 50);
502 
503 	hub->max_current_bp = USB251XB_DEF_MAX_CURRENT_BUS;
504 	if (!of_property_read_u32(np, "bp-max-removable-current-microamp",
505 	    &property_u32))
506 		hub->max_current_bp = min_t(u8, property_u32 / 2000, 255);
507 
508 	hub->power_on_time = USB251XB_DEF_POWER_ON_TIME;
509 	if (!of_property_read_u32(np, "power-on-time-ms", &property_u32))
510 		hub->power_on_time = min_t(u8, property_u32 / 2, 255);
511 
512 	if (of_property_read_u16_array(np, "language-id", &hub->lang_id, 1))
513 		hub->lang_id = USB251XB_DEF_LANGUAGE_ID;
514 
515 	cproperty_char = of_get_property(np, "manufacturer", NULL);
516 	strlcpy(str, cproperty_char ? : USB251XB_DEF_MANUFACTURER_STRING,
517 		sizeof(str));
518 	hub->manufacturer_len = strlen(str) & 0xFF;
519 	memset(hub->manufacturer, 0, USB251XB_STRING_BUFSIZE);
520 	len = min_t(size_t, USB251XB_STRING_BUFSIZE / 2, strlen(str));
521 	len = utf8s_to_utf16s(str, len, UTF16_LITTLE_ENDIAN,
522 			      (wchar_t *)hub->manufacturer,
523 			      USB251XB_STRING_BUFSIZE);
524 
525 	cproperty_char = of_get_property(np, "product", NULL);
526 	strlcpy(str, cproperty_char ? : data->product_str, sizeof(str));
527 	hub->product_len = strlen(str) & 0xFF;
528 	memset(hub->product, 0, USB251XB_STRING_BUFSIZE);
529 	len = min_t(size_t, USB251XB_STRING_BUFSIZE / 2, strlen(str));
530 	len = utf8s_to_utf16s(str, len, UTF16_LITTLE_ENDIAN,
531 			      (wchar_t *)hub->product,
532 			      USB251XB_STRING_BUFSIZE);
533 
534 	cproperty_char = of_get_property(np, "serial", NULL);
535 	strlcpy(str, cproperty_char ? : USB251XB_DEF_SERIAL_STRING,
536 		sizeof(str));
537 	hub->serial_len = strlen(str) & 0xFF;
538 	memset(hub->serial, 0, USB251XB_STRING_BUFSIZE);
539 	len = min_t(size_t, USB251XB_STRING_BUFSIZE / 2, strlen(str));
540 	len = utf8s_to_utf16s(str, len, UTF16_LITTLE_ENDIAN,
541 			      (wchar_t *)hub->serial,
542 			      USB251XB_STRING_BUFSIZE);
543 
544 	/*
545 	 * The datasheet documents the register as 'Port Swap' but in real the
546 	 * register controls the USB DP/DM signal swapping for each port.
547 	 */
548 	hub->port_swap = USB251XB_DEF_PORT_SWAP;
549 	of_property_for_each_u32(np, "swap-dx-lanes", prop, p, port) {
550 		if (port <= data->port_cnt)
551 			hub->port_swap |= BIT(port);
552 	}
553 
554 	/* The following parameters are currently not exposed to devicetree, but
555 	 * may be as soon as needed.
556 	 */
557 	hub->bat_charge_en = USB251XB_DEF_BATTERY_CHARGING_ENABLE;
558 	hub->boost_up = USB251XB_DEF_BOOST_UP;
559 	hub->boost_57 = USB251XB_DEF_BOOST_57;
560 	hub->boost_14 = USB251XB_DEF_BOOST_14;
561 	hub->port_map12 = USB251XB_DEF_PORT_MAP_12;
562 	hub->port_map34 = USB251XB_DEF_PORT_MAP_34;
563 	hub->port_map56 = USB251XB_DEF_PORT_MAP_56;
564 	hub->port_map7  = USB251XB_DEF_PORT_MAP_7;
565 
566 	return 0;
567 }
568 
569 static const struct of_device_id usb251xb_of_match[] = {
570 	{
571 		.compatible = "microchip,usb2512b",
572 		.data = &usb2512b_data,
573 	}, {
574 		.compatible = "microchip,usb2512bi",
575 		.data = &usb2512bi_data,
576 	}, {
577 		.compatible = "microchip,usb2513b",
578 		.data = &usb2513b_data,
579 	}, {
580 		.compatible = "microchip,usb2513bi",
581 		.data = &usb2513bi_data,
582 	}, {
583 		.compatible = "microchip,usb2514b",
584 		.data = &usb2514b_data,
585 	}, {
586 		.compatible = "microchip,usb2514bi",
587 		.data = &usb2514bi_data,
588 	}, {
589 		.compatible = "microchip,usb2517",
590 		.data = &usb2517_data,
591 	}, {
592 		.compatible = "microchip,usb2517i",
593 		.data = &usb2517i_data,
594 	}, {
595 		/* sentinel */
596 	}
597 };
598 MODULE_DEVICE_TABLE(of, usb251xb_of_match);
599 #else /* CONFIG_OF */
600 static int usb251xb_get_ofdata(struct usb251xb *hub,
601 			       struct usb251xb_data *data)
602 {
603 	return 0;
604 }
605 #endif /* CONFIG_OF */
606 
607 static int usb251xb_probe(struct usb251xb *hub)
608 {
609 	struct device *dev = hub->dev;
610 	struct device_node *np = dev->of_node;
611 	const struct of_device_id *of_id = of_match_device(usb251xb_of_match,
612 							   dev);
613 	int err;
614 
615 	if (np && of_id) {
616 		err = usb251xb_get_ofdata(hub,
617 					  (struct usb251xb_data *)of_id->data);
618 		if (err) {
619 			dev_err(dev, "failed to get ofdata: %d\n", err);
620 			return err;
621 		}
622 	}
623 
624 	err = usb251xb_connect(hub);
625 	if (err) {
626 		dev_err(dev, "Failed to connect hub (%d)\n", err);
627 		return err;
628 	}
629 
630 	dev_info(dev, "Hub probed successfully\n");
631 
632 	return 0;
633 }
634 
635 static int usb251xb_i2c_probe(struct i2c_client *i2c,
636 			      const struct i2c_device_id *id)
637 {
638 	struct usb251xb *hub;
639 
640 	hub = devm_kzalloc(&i2c->dev, sizeof(struct usb251xb), GFP_KERNEL);
641 	if (!hub)
642 		return -ENOMEM;
643 
644 	i2c_set_clientdata(i2c, hub);
645 	hub->dev = &i2c->dev;
646 	hub->i2c = i2c;
647 
648 	return usb251xb_probe(hub);
649 }
650 
651 static const struct i2c_device_id usb251xb_id[] = {
652 	{ "usb2512b", 0 },
653 	{ "usb2512bi", 0 },
654 	{ "usb2513b", 0 },
655 	{ "usb2513bi", 0 },
656 	{ "usb2514b", 0 },
657 	{ "usb2514bi", 0 },
658 	{ "usb2517", 0 },
659 	{ "usb2517i", 0 },
660 	{ /* sentinel */ }
661 };
662 MODULE_DEVICE_TABLE(i2c, usb251xb_id);
663 
664 static struct i2c_driver usb251xb_i2c_driver = {
665 	.driver = {
666 		.name = DRIVER_NAME,
667 		.of_match_table = of_match_ptr(usb251xb_of_match),
668 	},
669 	.probe    = usb251xb_i2c_probe,
670 	.id_table = usb251xb_id,
671 };
672 
673 module_i2c_driver(usb251xb_i2c_driver);
674 
675 MODULE_AUTHOR("Richard Leitner <richard.leitner@skidata.com>");
676 MODULE_DESCRIPTION("USB251x/xBi USB 2.0 Hub Controller Driver");
677 MODULE_LICENSE("GPL");
678