Linux Audio

Check our new training course

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