blob: 05819167604d70efb1159d2d67513b9dd4c751cc [file] [log] [blame]
Greg Kroah-Hartman5fd54ac2017-11-03 11:28:30 +01001// SPDX-License-Identifier: GPL-2.0+
Richard Leitner3ec72a22017-02-10 09:19:27 +01002/*
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/
Richard Leitner3ec72a22017-02-10 09:19:27 +010011 */
12
13#include <linux/delay.h>
Serge Semin69a0c692017-10-22 23:38:12 +030014#include <linux/gpio/consumer.h>
Serge Semin6e3c8be2019-04-24 17:49:14 +030015#include <linux/gpio/driver.h>
Richard Leitner3ec72a22017-02-10 09:19:27 +010016#include <linux/i2c.h>
17#include <linux/module.h>
18#include <linux/nls.h>
19#include <linux/of_device.h>
Marco Felsch4d7201c2019-09-17 16:44:47 +020020#include <linux/regulator/consumer.h>
Richard Leitner3ec72a22017-02-10 09:19:27 +010021#include <linux/slab.h>
22
23/* Internal Register Set Addresses & Default Values acc. to DS00001692C */
24#define USB251XB_ADDR_VENDOR_ID_LSB 0x00
25#define USB251XB_ADDR_VENDOR_ID_MSB 0x01
26#define USB251XB_DEF_VENDOR_ID 0x0424
27
28#define USB251XB_ADDR_PRODUCT_ID_LSB 0x02
29#define USB251XB_ADDR_PRODUCT_ID_MSB 0x03
30#define USB251XB_DEF_PRODUCT_ID_12 0x2512 /* USB2512B/12Bi */
31#define USB251XB_DEF_PRODUCT_ID_13 0x2513 /* USB2513B/13Bi */
32#define USB251XB_DEF_PRODUCT_ID_14 0x2514 /* USB2514B/14Bi */
Serge Semin7fcf5582017-10-22 23:38:04 +030033#define USB251XB_DEF_PRODUCT_ID_17 0x2517 /* USB2517/17i */
Richard Leitner3ec72a22017-02-10 09:19:27 +010034
35#define USB251XB_ADDR_DEVICE_ID_LSB 0x04
36#define USB251XB_ADDR_DEVICE_ID_MSB 0x05
37#define USB251XB_DEF_DEVICE_ID 0x0BB3
38
39#define USB251XB_ADDR_CONFIG_DATA_1 0x06
40#define USB251XB_DEF_CONFIG_DATA_1 0x9B
41#define USB251XB_ADDR_CONFIG_DATA_2 0x07
42#define USB251XB_DEF_CONFIG_DATA_2 0x20
43#define USB251XB_ADDR_CONFIG_DATA_3 0x08
44#define USB251XB_DEF_CONFIG_DATA_3 0x02
45
46#define USB251XB_ADDR_NON_REMOVABLE_DEVICES 0x09
47#define USB251XB_DEF_NON_REMOVABLE_DEVICES 0x00
48
49#define USB251XB_ADDR_PORT_DISABLE_SELF 0x0A
50#define USB251XB_DEF_PORT_DISABLE_SELF 0x00
51#define USB251XB_ADDR_PORT_DISABLE_BUS 0x0B
52#define USB251XB_DEF_PORT_DISABLE_BUS 0x00
53
54#define USB251XB_ADDR_MAX_POWER_SELF 0x0C
55#define USB251XB_DEF_MAX_POWER_SELF 0x01
56#define USB251XB_ADDR_MAX_POWER_BUS 0x0D
57#define USB251XB_DEF_MAX_POWER_BUS 0x32
58
59#define USB251XB_ADDR_MAX_CURRENT_SELF 0x0E
60#define USB251XB_DEF_MAX_CURRENT_SELF 0x01
61#define USB251XB_ADDR_MAX_CURRENT_BUS 0x0F
62#define USB251XB_DEF_MAX_CURRENT_BUS 0x32
63
64#define USB251XB_ADDR_POWER_ON_TIME 0x10
65#define USB251XB_DEF_POWER_ON_TIME 0x32
66
67#define USB251XB_ADDR_LANGUAGE_ID_HIGH 0x11
68#define USB251XB_ADDR_LANGUAGE_ID_LOW 0x12
69#define USB251XB_DEF_LANGUAGE_ID 0x0000
70
71#define USB251XB_STRING_BUFSIZE 62
72#define USB251XB_ADDR_MANUFACTURER_STRING_LEN 0x13
73#define USB251XB_ADDR_MANUFACTURER_STRING 0x16
74#define USB251XB_DEF_MANUFACTURER_STRING "Microchip"
75
76#define USB251XB_ADDR_PRODUCT_STRING_LEN 0x14
77#define USB251XB_ADDR_PRODUCT_STRING 0x54
Serge Semin7fcf5582017-10-22 23:38:04 +030078#define USB251XB_DEF_PRODUCT_STRING "USB251xB/xBi/7i"
Richard Leitner3ec72a22017-02-10 09:19:27 +010079
80#define USB251XB_ADDR_SERIAL_STRING_LEN 0x15
81#define USB251XB_ADDR_SERIAL_STRING 0x92
82#define USB251XB_DEF_SERIAL_STRING ""
83
84#define USB251XB_ADDR_BATTERY_CHARGING_ENABLE 0xD0
85#define USB251XB_DEF_BATTERY_CHARGING_ENABLE 0x00
86
87#define USB251XB_ADDR_BOOST_UP 0xF6
88#define USB251XB_DEF_BOOST_UP 0x00
Serge Seminccdddc02017-10-22 23:38:07 +030089#define USB251XB_ADDR_BOOST_57 0xF7
90#define USB251XB_DEF_BOOST_57 0x00
91#define USB251XB_ADDR_BOOST_14 0xF8
92#define USB251XB_DEF_BOOST_14 0x00
Richard Leitner3ec72a22017-02-10 09:19:27 +010093
94#define USB251XB_ADDR_PORT_SWAP 0xFA
95#define USB251XB_DEF_PORT_SWAP 0x00
96
97#define USB251XB_ADDR_PORT_MAP_12 0xFB
98#define USB251XB_DEF_PORT_MAP_12 0x00
99#define USB251XB_ADDR_PORT_MAP_34 0xFC
Serge Semindb234b92017-10-22 23:38:06 +0300100#define USB251XB_DEF_PORT_MAP_34 0x00 /* USB251{3B/i,4B/i,7/i} only */
101#define USB251XB_ADDR_PORT_MAP_56 0xFD
102#define USB251XB_DEF_PORT_MAP_56 0x00 /* USB2517/i only */
103#define USB251XB_ADDR_PORT_MAP_7 0xFE
104#define USB251XB_DEF_PORT_MAP_7 0x00 /* USB2517/i only */
Richard Leitner3ec72a22017-02-10 09:19:27 +0100105
106#define USB251XB_ADDR_STATUS_COMMAND 0xFF
107#define USB251XB_STATUS_COMMAND_SMBUS_DOWN 0x04
108#define USB251XB_STATUS_COMMAND_RESET 0x02
109#define USB251XB_STATUS_COMMAND_ATTACH 0x01
110
111#define USB251XB_I2C_REG_SZ 0x100
112#define USB251XB_I2C_WRITE_SZ 0x10
113
114#define DRIVER_NAME "usb251xb"
115#define DRIVER_DESC "Microchip USB 2.0 Hi-Speed Hub Controller"
Richard Leitner3ec72a22017-02-10 09:19:27 +0100116
117struct usb251xb {
118 struct device *dev;
119 struct i2c_client *i2c;
Marco Felsch4d7201c2019-09-17 16:44:47 +0200120 struct regulator *vdd;
Richard Leitner3ec72a22017-02-10 09:19:27 +0100121 u8 skip_config;
Serge Semin69a0c692017-10-22 23:38:12 +0300122 struct gpio_desc *gpio_reset;
Richard Leitner3ec72a22017-02-10 09:19:27 +0100123 u16 vendor_id;
124 u16 product_id;
125 u16 device_id;
126 u8 conf_data1;
127 u8 conf_data2;
128 u8 conf_data3;
129 u8 non_rem_dev;
130 u8 port_disable_sp;
131 u8 port_disable_bp;
132 u8 max_power_sp;
133 u8 max_power_bp;
134 u8 max_current_sp;
135 u8 max_current_bp;
136 u8 power_on_time;
137 u16 lang_id;
138 u8 manufacturer_len;
139 u8 product_len;
140 u8 serial_len;
141 char manufacturer[USB251XB_STRING_BUFSIZE];
142 char product[USB251XB_STRING_BUFSIZE];
143 char serial[USB251XB_STRING_BUFSIZE];
144 u8 bat_charge_en;
145 u8 boost_up;
Serge Seminccdddc02017-10-22 23:38:07 +0300146 u8 boost_57;
147 u8 boost_14;
Richard Leitner3ec72a22017-02-10 09:19:27 +0100148 u8 port_swap;
149 u8 port_map12;
150 u8 port_map34;
Serge Semindb234b92017-10-22 23:38:06 +0300151 u8 port_map56;
152 u8 port_map7;
Richard Leitner3ec72a22017-02-10 09:19:27 +0100153 u8 status;
154};
155
156struct usb251xb_data {
157 u16 product_id;
Serge Semin38481122017-10-22 23:38:05 +0300158 u8 port_cnt;
Serge Semin2818e132017-10-22 23:38:09 +0300159 bool led_support;
Serge Semindd99d102017-10-22 23:38:08 +0300160 bool bat_support;
Richard Leitner3ec72a22017-02-10 09:19:27 +0100161 char product_str[USB251XB_STRING_BUFSIZE / 2]; /* ASCII string */
162};
163
164static const struct usb251xb_data usb2512b_data = {
165 .product_id = 0x2512,
Serge Semin38481122017-10-22 23:38:05 +0300166 .port_cnt = 2,
Serge Semin2818e132017-10-22 23:38:09 +0300167 .led_support = false,
Serge Semindd99d102017-10-22 23:38:08 +0300168 .bat_support = true,
Richard Leitner3ec72a22017-02-10 09:19:27 +0100169 .product_str = "USB2512B",
170};
171
172static const struct usb251xb_data usb2512bi_data = {
173 .product_id = 0x2512,
Serge Semin38481122017-10-22 23:38:05 +0300174 .port_cnt = 2,
Serge Semin2818e132017-10-22 23:38:09 +0300175 .led_support = false,
Serge Semindd99d102017-10-22 23:38:08 +0300176 .bat_support = true,
Richard Leitner3ec72a22017-02-10 09:19:27 +0100177 .product_str = "USB2512Bi",
178};
179
180static const struct usb251xb_data usb2513b_data = {
181 .product_id = 0x2513,
Serge Semin38481122017-10-22 23:38:05 +0300182 .port_cnt = 3,
Serge Semin2818e132017-10-22 23:38:09 +0300183 .led_support = false,
Serge Semindd99d102017-10-22 23:38:08 +0300184 .bat_support = true,
Richard Leitner3ec72a22017-02-10 09:19:27 +0100185 .product_str = "USB2513B",
186};
187
188static const struct usb251xb_data usb2513bi_data = {
189 .product_id = 0x2513,
Serge Semin38481122017-10-22 23:38:05 +0300190 .port_cnt = 3,
Serge Semin2818e132017-10-22 23:38:09 +0300191 .led_support = false,
Serge Semindd99d102017-10-22 23:38:08 +0300192 .bat_support = true,
Richard Leitner3ec72a22017-02-10 09:19:27 +0100193 .product_str = "USB2513Bi",
194};
195
196static const struct usb251xb_data usb2514b_data = {
197 .product_id = 0x2514,
Serge Semin38481122017-10-22 23:38:05 +0300198 .port_cnt = 4,
Serge Semin2818e132017-10-22 23:38:09 +0300199 .led_support = false,
Serge Semindd99d102017-10-22 23:38:08 +0300200 .bat_support = true,
Richard Leitner3ec72a22017-02-10 09:19:27 +0100201 .product_str = "USB2514B",
202};
203
204static const struct usb251xb_data usb2514bi_data = {
205 .product_id = 0x2514,
Serge Semin38481122017-10-22 23:38:05 +0300206 .port_cnt = 4,
Serge Semin2818e132017-10-22 23:38:09 +0300207 .led_support = false,
Serge Semindd99d102017-10-22 23:38:08 +0300208 .bat_support = true,
Richard Leitner3ec72a22017-02-10 09:19:27 +0100209 .product_str = "USB2514Bi",
210};
211
Serge Semin7fcf5582017-10-22 23:38:04 +0300212static const struct usb251xb_data usb2517_data = {
213 .product_id = 0x2517,
Serge Semin38481122017-10-22 23:38:05 +0300214 .port_cnt = 7,
Serge Semin2818e132017-10-22 23:38:09 +0300215 .led_support = true,
Serge Semindd99d102017-10-22 23:38:08 +0300216 .bat_support = false,
Serge Semin7fcf5582017-10-22 23:38:04 +0300217 .product_str = "USB2517",
218};
219
220static const struct usb251xb_data usb2517i_data = {
221 .product_id = 0x2517,
Serge Semin38481122017-10-22 23:38:05 +0300222 .port_cnt = 7,
Serge Semin2818e132017-10-22 23:38:09 +0300223 .led_support = true,
Serge Semindd99d102017-10-22 23:38:08 +0300224 .bat_support = false,
Serge Semin7fcf5582017-10-22 23:38:04 +0300225 .product_str = "USB2517i",
226};
227
Serge Semin95e060e2019-04-27 12:06:44 +0300228#ifdef CONFIG_GPIOLIB
Serge Semin6e3c8be2019-04-24 17:49:14 +0300229static int usb251xb_check_dev_children(struct device *dev, void *child)
230{
231 if (dev->type == &i2c_adapter_type) {
232 return device_for_each_child(dev, child,
233 usb251xb_check_dev_children);
234 }
235
236 return (dev == child);
237}
238
239static int usb251x_check_gpio_chip(struct usb251xb *hub)
240{
241 struct gpio_chip *gc = gpiod_to_chip(hub->gpio_reset);
242 struct i2c_adapter *adap = hub->i2c->adapter;
243 int ret;
244
245 if (!hub->gpio_reset)
246 return 0;
247
248 if (!gc)
249 return -EINVAL;
250
251 ret = usb251xb_check_dev_children(&adap->dev, gc->parent);
252 if (ret) {
253 dev_err(hub->dev, "Reset GPIO chip is at the same i2c-bus\n");
254 return -EINVAL;
255 }
256
257 return 0;
258}
Serge Semin95e060e2019-04-27 12:06:44 +0300259#else
260static int usb251x_check_gpio_chip(struct usb251xb *hub)
261{
262 return 0;
263}
264#endif
Serge Semin6e3c8be2019-04-24 17:49:14 +0300265
Richard Leitner3ec72a22017-02-10 09:19:27 +0100266static void usb251xb_reset(struct usb251xb *hub, int state)
267{
Serge Semin69a0c692017-10-22 23:38:12 +0300268 if (!hub->gpio_reset)
Richard Leitner3ec72a22017-02-10 09:19:27 +0100269 return;
270
Serge Semin6e3c8be2019-04-24 17:49:14 +0300271 i2c_lock_bus(hub->i2c->adapter, I2C_LOCK_SEGMENT);
272
Serge Semin69a0c692017-10-22 23:38:12 +0300273 gpiod_set_value_cansleep(hub->gpio_reset, state);
Richard Leitner3ec72a22017-02-10 09:19:27 +0100274
275 /* wait for hub recovery/stabilization */
Serge Semin69a0c692017-10-22 23:38:12 +0300276 if (!state)
Richard Leitner3ec72a22017-02-10 09:19:27 +0100277 usleep_range(500, 750); /* >=500us at power on */
278 else
279 usleep_range(1, 10); /* >=1us at power down */
Serge Semin6e3c8be2019-04-24 17:49:14 +0300280
281 i2c_unlock_bus(hub->i2c->adapter, I2C_LOCK_SEGMENT);
Richard Leitner3ec72a22017-02-10 09:19:27 +0100282}
283
284static int usb251xb_connect(struct usb251xb *hub)
285{
286 struct device *dev = hub->dev;
287 int err, i;
288 char i2c_wb[USB251XB_I2C_REG_SZ];
289
290 memset(i2c_wb, 0, USB251XB_I2C_REG_SZ);
291
292 if (hub->skip_config) {
293 dev_info(dev, "Skip hub configuration, only attach.\n");
294 i2c_wb[0] = 0x01;
295 i2c_wb[1] = USB251XB_STATUS_COMMAND_ATTACH;
296
Serge Semin69a0c692017-10-22 23:38:12 +0300297 usb251xb_reset(hub, 0);
Richard Leitner3ec72a22017-02-10 09:19:27 +0100298
299 err = i2c_smbus_write_i2c_block_data(hub->i2c,
300 USB251XB_ADDR_STATUS_COMMAND, 2, i2c_wb);
301 if (err) {
302 dev_err(dev, "attaching hub failed: %d\n", err);
303 return err;
304 }
305 return 0;
306 }
307
308 i2c_wb[USB251XB_ADDR_VENDOR_ID_MSB] = (hub->vendor_id >> 8) & 0xFF;
309 i2c_wb[USB251XB_ADDR_VENDOR_ID_LSB] = hub->vendor_id & 0xFF;
310 i2c_wb[USB251XB_ADDR_PRODUCT_ID_MSB] = (hub->product_id >> 8) & 0xFF;
311 i2c_wb[USB251XB_ADDR_PRODUCT_ID_LSB] = hub->product_id & 0xFF;
312 i2c_wb[USB251XB_ADDR_DEVICE_ID_MSB] = (hub->device_id >> 8) & 0xFF;
313 i2c_wb[USB251XB_ADDR_DEVICE_ID_LSB] = hub->device_id & 0xFF;
314 i2c_wb[USB251XB_ADDR_CONFIG_DATA_1] = hub->conf_data1;
315 i2c_wb[USB251XB_ADDR_CONFIG_DATA_2] = hub->conf_data2;
316 i2c_wb[USB251XB_ADDR_CONFIG_DATA_3] = hub->conf_data3;
317 i2c_wb[USB251XB_ADDR_NON_REMOVABLE_DEVICES] = hub->non_rem_dev;
318 i2c_wb[USB251XB_ADDR_PORT_DISABLE_SELF] = hub->port_disable_sp;
319 i2c_wb[USB251XB_ADDR_PORT_DISABLE_BUS] = hub->port_disable_bp;
320 i2c_wb[USB251XB_ADDR_MAX_POWER_SELF] = hub->max_power_sp;
321 i2c_wb[USB251XB_ADDR_MAX_POWER_BUS] = hub->max_power_bp;
322 i2c_wb[USB251XB_ADDR_MAX_CURRENT_SELF] = hub->max_current_sp;
323 i2c_wb[USB251XB_ADDR_MAX_CURRENT_BUS] = hub->max_current_bp;
324 i2c_wb[USB251XB_ADDR_POWER_ON_TIME] = hub->power_on_time;
325 i2c_wb[USB251XB_ADDR_LANGUAGE_ID_HIGH] = (hub->lang_id >> 8) & 0xFF;
326 i2c_wb[USB251XB_ADDR_LANGUAGE_ID_LOW] = hub->lang_id & 0xFF;
327 i2c_wb[USB251XB_ADDR_MANUFACTURER_STRING_LEN] = hub->manufacturer_len;
328 i2c_wb[USB251XB_ADDR_PRODUCT_STRING_LEN] = hub->product_len;
329 i2c_wb[USB251XB_ADDR_SERIAL_STRING_LEN] = hub->serial_len;
330 memcpy(&i2c_wb[USB251XB_ADDR_MANUFACTURER_STRING], hub->manufacturer,
331 USB251XB_STRING_BUFSIZE);
332 memcpy(&i2c_wb[USB251XB_ADDR_SERIAL_STRING], hub->serial,
333 USB251XB_STRING_BUFSIZE);
334 memcpy(&i2c_wb[USB251XB_ADDR_PRODUCT_STRING], hub->product,
335 USB251XB_STRING_BUFSIZE);
336 i2c_wb[USB251XB_ADDR_BATTERY_CHARGING_ENABLE] = hub->bat_charge_en;
337 i2c_wb[USB251XB_ADDR_BOOST_UP] = hub->boost_up;
Serge Seminccdddc02017-10-22 23:38:07 +0300338 i2c_wb[USB251XB_ADDR_BOOST_57] = hub->boost_57;
339 i2c_wb[USB251XB_ADDR_BOOST_14] = hub->boost_14;
Richard Leitner3ec72a22017-02-10 09:19:27 +0100340 i2c_wb[USB251XB_ADDR_PORT_SWAP] = hub->port_swap;
341 i2c_wb[USB251XB_ADDR_PORT_MAP_12] = hub->port_map12;
342 i2c_wb[USB251XB_ADDR_PORT_MAP_34] = hub->port_map34;
Serge Semindb234b92017-10-22 23:38:06 +0300343 i2c_wb[USB251XB_ADDR_PORT_MAP_56] = hub->port_map56;
344 i2c_wb[USB251XB_ADDR_PORT_MAP_7] = hub->port_map7;
Richard Leitner3ec72a22017-02-10 09:19:27 +0100345 i2c_wb[USB251XB_ADDR_STATUS_COMMAND] = USB251XB_STATUS_COMMAND_ATTACH;
346
Serge Semin69a0c692017-10-22 23:38:12 +0300347 usb251xb_reset(hub, 0);
Richard Leitner3ec72a22017-02-10 09:19:27 +0100348
349 /* write registers */
350 for (i = 0; i < (USB251XB_I2C_REG_SZ / USB251XB_I2C_WRITE_SZ); i++) {
351 int offset = i * USB251XB_I2C_WRITE_SZ;
352 char wbuf[USB251XB_I2C_WRITE_SZ + 1];
353
354 /* The first data byte transferred tells the hub how many data
355 * bytes will follow (byte count).
356 */
357 wbuf[0] = USB251XB_I2C_WRITE_SZ;
358 memcpy(&wbuf[1], &i2c_wb[offset], USB251XB_I2C_WRITE_SZ);
359
360 dev_dbg(dev, "writing %d byte block %d to 0x%02X\n",
361 USB251XB_I2C_WRITE_SZ, i, offset);
362
363 err = i2c_smbus_write_i2c_block_data(hub->i2c, offset,
364 USB251XB_I2C_WRITE_SZ + 1,
365 wbuf);
366 if (err)
367 goto out_err;
368 }
369
370 dev_info(dev, "Hub configuration was successful.\n");
371 return 0;
372
373out_err:
374 dev_err(dev, "configuring block %d failed: %d\n", i, err);
375 return err;
376}
377
378#ifdef CONFIG_OF
Serge Semin73d31de2019-04-25 00:12:06 +0300379static void usb251xb_get_ports_field(struct usb251xb *hub,
Lucas Stach4849ee62019-07-19 10:44:07 +0200380 const char *prop_name, u8 port_cnt,
381 bool ds_only, u8 *fld)
Serge Semin73d31de2019-04-25 00:12:06 +0300382{
383 struct device *dev = hub->dev;
384 struct property *prop;
385 const __be32 *p;
386 u32 port;
387
388 of_property_for_each_u32(dev->of_node, prop_name, prop, p, port) {
Lucas Stach4849ee62019-07-19 10:44:07 +0200389 if ((port >= ds_only ? 1 : 0) && (port <= port_cnt))
Serge Semin73d31de2019-04-25 00:12:06 +0300390 *fld |= BIT(port);
391 else
392 dev_warn(dev, "port %u doesn't exist\n", port);
393 }
394}
395
Richard Leitner3ec72a22017-02-10 09:19:27 +0100396static int usb251xb_get_ofdata(struct usb251xb *hub,
397 struct usb251xb_data *data)
398{
399 struct device *dev = hub->dev;
400 struct device_node *np = dev->of_node;
Serge Semin73d31de2019-04-25 00:12:06 +0300401 int len, err;
402 u32 property_u32 = 0;
Richard Leitner3ec72a22017-02-10 09:19:27 +0100403 const char *cproperty_char;
404 char str[USB251XB_STRING_BUFSIZE / 2];
405
406 if (!np) {
407 dev_err(dev, "failed to get ofdata\n");
408 return -ENODEV;
409 }
410
411 if (of_get_property(np, "skip-config", NULL))
412 hub->skip_config = 1;
413 else
414 hub->skip_config = 0;
415
Serge Semin69a0c692017-10-22 23:38:12 +0300416 hub->gpio_reset = devm_gpiod_get_optional(dev, "reset", GPIOD_OUT_HIGH);
417 if (PTR_ERR(hub->gpio_reset) == -EPROBE_DEFER) {
Richard Leitner3ec72a22017-02-10 09:19:27 +0100418 return -EPROBE_DEFER;
Serge Semin69a0c692017-10-22 23:38:12 +0300419 } else if (IS_ERR(hub->gpio_reset)) {
420 err = PTR_ERR(hub->gpio_reset);
421 dev_err(dev, "unable to request GPIO reset pin (%d)\n", err);
422 return err;
Richard Leitner3ec72a22017-02-10 09:19:27 +0100423 }
424
Marco Felsch4d7201c2019-09-17 16:44:47 +0200425 hub->vdd = devm_regulator_get(dev, "vdd");
426 if (IS_ERR(hub->vdd))
427 return PTR_ERR(hub->vdd);
428
Richard Leitner3ec72a22017-02-10 09:19:27 +0100429 if (of_property_read_u16_array(np, "vendor-id", &hub->vendor_id, 1))
430 hub->vendor_id = USB251XB_DEF_VENDOR_ID;
431
432 if (of_property_read_u16_array(np, "product-id",
433 &hub->product_id, 1))
434 hub->product_id = data->product_id;
435
436 if (of_property_read_u16_array(np, "device-id", &hub->device_id, 1))
437 hub->device_id = USB251XB_DEF_DEVICE_ID;
438
439 hub->conf_data1 = USB251XB_DEF_CONFIG_DATA_1;
440 if (of_get_property(np, "self-powered", NULL)) {
441 hub->conf_data1 |= BIT(7);
442
443 /* Configure Over-Current sens when self-powered */
444 hub->conf_data1 &= ~BIT(2);
445 if (of_get_property(np, "ganged-sensing", NULL))
446 hub->conf_data1 &= ~BIT(1);
447 else if (of_get_property(np, "individual-sensing", NULL))
448 hub->conf_data1 |= BIT(1);
449 } else if (of_get_property(np, "bus-powered", NULL)) {
450 hub->conf_data1 &= ~BIT(7);
451
452 /* Disable Over-Current sense when bus-powered */
453 hub->conf_data1 |= BIT(2);
454 }
455
456 if (of_get_property(np, "disable-hi-speed", NULL))
457 hub->conf_data1 |= BIT(5);
458
459 if (of_get_property(np, "multi-tt", NULL))
460 hub->conf_data1 |= BIT(4);
461 else if (of_get_property(np, "single-tt", NULL))
462 hub->conf_data1 &= ~BIT(4);
463
464 if (of_get_property(np, "disable-eop", NULL))
465 hub->conf_data1 |= BIT(3);
466
467 if (of_get_property(np, "individual-port-switching", NULL))
468 hub->conf_data1 |= BIT(0);
469 else if (of_get_property(np, "ganged-port-switching", NULL))
470 hub->conf_data1 &= ~BIT(0);
471
472 hub->conf_data2 = USB251XB_DEF_CONFIG_DATA_2;
473 if (of_get_property(np, "dynamic-power-switching", NULL))
474 hub->conf_data2 |= BIT(7);
475
Serge Semin287c2bb2017-10-22 23:38:10 +0300476 if (!of_property_read_u32(np, "oc-delay-us", &property_u32)) {
477 if (property_u32 == 100) {
Richard Leitner7f7d8ba2017-03-06 09:24:21 +0100478 /* 100 us*/
479 hub->conf_data2 &= ~BIT(5);
480 hub->conf_data2 &= ~BIT(4);
Serge Semin287c2bb2017-10-22 23:38:10 +0300481 } else if (property_u32 == 4000) {
Richard Leitner7f7d8ba2017-03-06 09:24:21 +0100482 /* 4 ms */
483 hub->conf_data2 &= ~BIT(5);
484 hub->conf_data2 |= BIT(4);
Serge Semin287c2bb2017-10-22 23:38:10 +0300485 } else if (property_u32 == 16000) {
Richard Leitner7f7d8ba2017-03-06 09:24:21 +0100486 /* 16 ms */
487 hub->conf_data2 |= BIT(5);
488 hub->conf_data2 |= BIT(4);
489 } else {
490 /* 8 ms (DEFAULT) */
491 hub->conf_data2 |= BIT(5);
492 hub->conf_data2 &= ~BIT(4);
493 }
Richard Leitner3ec72a22017-02-10 09:19:27 +0100494 }
495
496 if (of_get_property(np, "compound-device", NULL))
497 hub->conf_data2 |= BIT(3);
498
499 hub->conf_data3 = USB251XB_DEF_CONFIG_DATA_3;
500 if (of_get_property(np, "port-mapping-mode", NULL))
501 hub->conf_data3 |= BIT(3);
502
Serge Semin2818e132017-10-22 23:38:09 +0300503 if (data->led_support && of_get_property(np, "led-usb-mode", NULL))
504 hub->conf_data3 &= ~BIT(1);
505
Richard Leitner3ec72a22017-02-10 09:19:27 +0100506 if (of_get_property(np, "string-support", NULL))
507 hub->conf_data3 |= BIT(0);
508
509 hub->non_rem_dev = USB251XB_DEF_NON_REMOVABLE_DEVICES;
Serge Semin73d31de2019-04-25 00:12:06 +0300510 usb251xb_get_ports_field(hub, "non-removable-ports", data->port_cnt,
Lucas Stach4849ee62019-07-19 10:44:07 +0200511 true, &hub->non_rem_dev);
Richard Leitner3ec72a22017-02-10 09:19:27 +0100512
513 hub->port_disable_sp = USB251XB_DEF_PORT_DISABLE_SELF;
Serge Semin73d31de2019-04-25 00:12:06 +0300514 usb251xb_get_ports_field(hub, "sp-disabled-ports", data->port_cnt,
Lucas Stach4849ee62019-07-19 10:44:07 +0200515 true, &hub->port_disable_sp);
Richard Leitner3ec72a22017-02-10 09:19:27 +0100516
517 hub->port_disable_bp = USB251XB_DEF_PORT_DISABLE_BUS;
Serge Semin73d31de2019-04-25 00:12:06 +0300518 usb251xb_get_ports_field(hub, "bp-disabled-ports", data->port_cnt,
Lucas Stach4849ee62019-07-19 10:44:07 +0200519 true, &hub->port_disable_bp);
Richard Leitner3ec72a22017-02-10 09:19:27 +0100520
Serge Semin4ed466a2017-10-22 23:38:11 +0300521 hub->max_power_sp = USB251XB_DEF_MAX_POWER_SELF;
522 if (!of_property_read_u32(np, "sp-max-total-current-microamp",
523 &property_u32))
524 hub->max_power_sp = min_t(u8, property_u32 / 2000, 50);
525
526 hub->max_power_bp = USB251XB_DEF_MAX_POWER_BUS;
527 if (!of_property_read_u32(np, "bp-max-total-current-microamp",
528 &property_u32))
529 hub->max_power_bp = min_t(u8, property_u32 / 2000, 255);
530
531 hub->max_current_sp = USB251XB_DEF_MAX_CURRENT_SELF;
532 if (!of_property_read_u32(np, "sp-max-removable-current-microamp",
533 &property_u32))
534 hub->max_current_sp = min_t(u8, property_u32 / 2000, 50);
535
536 hub->max_current_bp = USB251XB_DEF_MAX_CURRENT_BUS;
537 if (!of_property_read_u32(np, "bp-max-removable-current-microamp",
538 &property_u32))
539 hub->max_current_bp = min_t(u8, property_u32 / 2000, 255);
540
Richard Leitner3ec72a22017-02-10 09:19:27 +0100541 hub->power_on_time = USB251XB_DEF_POWER_ON_TIME;
Serge Semin287c2bb2017-10-22 23:38:10 +0300542 if (!of_property_read_u32(np, "power-on-time-ms", &property_u32))
543 hub->power_on_time = min_t(u8, property_u32 / 2, 255);
Richard Leitner3ec72a22017-02-10 09:19:27 +0100544
545 if (of_property_read_u16_array(np, "language-id", &hub->lang_id, 1))
546 hub->lang_id = USB251XB_DEF_LANGUAGE_ID;
547
548 cproperty_char = of_get_property(np, "manufacturer", NULL);
549 strlcpy(str, cproperty_char ? : USB251XB_DEF_MANUFACTURER_STRING,
550 sizeof(str));
551 hub->manufacturer_len = strlen(str) & 0xFF;
552 memset(hub->manufacturer, 0, USB251XB_STRING_BUFSIZE);
553 len = min_t(size_t, USB251XB_STRING_BUFSIZE / 2, strlen(str));
554 len = utf8s_to_utf16s(str, len, UTF16_LITTLE_ENDIAN,
555 (wchar_t *)hub->manufacturer,
556 USB251XB_STRING_BUFSIZE);
557
558 cproperty_char = of_get_property(np, "product", NULL);
559 strlcpy(str, cproperty_char ? : data->product_str, sizeof(str));
560 hub->product_len = strlen(str) & 0xFF;
561 memset(hub->product, 0, USB251XB_STRING_BUFSIZE);
562 len = min_t(size_t, USB251XB_STRING_BUFSIZE / 2, strlen(str));
563 len = utf8s_to_utf16s(str, len, UTF16_LITTLE_ENDIAN,
564 (wchar_t *)hub->product,
565 USB251XB_STRING_BUFSIZE);
566
567 cproperty_char = of_get_property(np, "serial", NULL);
568 strlcpy(str, cproperty_char ? : USB251XB_DEF_SERIAL_STRING,
569 sizeof(str));
570 hub->serial_len = strlen(str) & 0xFF;
571 memset(hub->serial, 0, USB251XB_STRING_BUFSIZE);
572 len = min_t(size_t, USB251XB_STRING_BUFSIZE / 2, strlen(str));
573 len = utf8s_to_utf16s(str, len, UTF16_LITTLE_ENDIAN,
574 (wchar_t *)hub->serial,
575 USB251XB_STRING_BUFSIZE);
576
Marco Felsch02a50b82019-01-16 15:20:37 +0100577 /*
578 * The datasheet documents the register as 'Port Swap' but in real the
579 * register controls the USB DP/DM signal swapping for each port.
580 */
581 hub->port_swap = USB251XB_DEF_PORT_SWAP;
Serge Semin73d31de2019-04-25 00:12:06 +0300582 usb251xb_get_ports_field(hub, "swap-dx-lanes", data->port_cnt,
Lucas Stach4849ee62019-07-19 10:44:07 +0200583 false, &hub->port_swap);
Marco Felsch02a50b82019-01-16 15:20:37 +0100584
Richard Leitner3ec72a22017-02-10 09:19:27 +0100585 /* The following parameters are currently not exposed to devicetree, but
586 * may be as soon as needed.
587 */
Richard Leitner3ec72a22017-02-10 09:19:27 +0100588 hub->bat_charge_en = USB251XB_DEF_BATTERY_CHARGING_ENABLE;
589 hub->boost_up = USB251XB_DEF_BOOST_UP;
Serge Seminccdddc02017-10-22 23:38:07 +0300590 hub->boost_57 = USB251XB_DEF_BOOST_57;
591 hub->boost_14 = USB251XB_DEF_BOOST_14;
Richard Leitner3ec72a22017-02-10 09:19:27 +0100592 hub->port_map12 = USB251XB_DEF_PORT_MAP_12;
593 hub->port_map34 = USB251XB_DEF_PORT_MAP_34;
Serge Semindb234b92017-10-22 23:38:06 +0300594 hub->port_map56 = USB251XB_DEF_PORT_MAP_56;
595 hub->port_map7 = USB251XB_DEF_PORT_MAP_7;
Richard Leitner3ec72a22017-02-10 09:19:27 +0100596
597 return 0;
598}
599
600static const struct of_device_id usb251xb_of_match[] = {
601 {
602 .compatible = "microchip,usb2512b",
603 .data = &usb2512b_data,
604 }, {
605 .compatible = "microchip,usb2512bi",
606 .data = &usb2512bi_data,
607 }, {
608 .compatible = "microchip,usb2513b",
609 .data = &usb2513b_data,
610 }, {
611 .compatible = "microchip,usb2513bi",
612 .data = &usb2513bi_data,
613 }, {
614 .compatible = "microchip,usb2514b",
615 .data = &usb2514b_data,
616 }, {
617 .compatible = "microchip,usb2514bi",
618 .data = &usb2514bi_data,
619 }, {
Serge Semin7fcf5582017-10-22 23:38:04 +0300620 .compatible = "microchip,usb2517",
621 .data = &usb2517_data,
622 }, {
623 .compatible = "microchip,usb2517i",
624 .data = &usb2517i_data,
625 }, {
Richard Leitner3ec72a22017-02-10 09:19:27 +0100626 /* sentinel */
627 }
628};
629MODULE_DEVICE_TABLE(of, usb251xb_of_match);
630#else /* CONFIG_OF */
631static int usb251xb_get_ofdata(struct usb251xb *hub,
632 struct usb251xb_data *data)
633{
634 return 0;
635}
636#endif /* CONFIG_OF */
637
638static int usb251xb_probe(struct usb251xb *hub)
639{
640 struct device *dev = hub->dev;
641 struct device_node *np = dev->of_node;
642 const struct of_device_id *of_id = of_match_device(usb251xb_of_match,
643 dev);
644 int err;
645
Aditya Pakki41f00e62019-03-20 10:27:11 -0500646 if (np && of_id) {
Richard Leitner3ec72a22017-02-10 09:19:27 +0100647 err = usb251xb_get_ofdata(hub,
648 (struct usb251xb_data *)of_id->data);
649 if (err) {
650 dev_err(dev, "failed to get ofdata: %d\n", err);
651 return err;
652 }
653 }
654
Serge Semin6e3c8be2019-04-24 17:49:14 +0300655 /*
656 * usb251x SMBus-slave SCL lane is muxed with CFG_SEL0 pin. So if anyone
657 * tries to work with the bus at the moment the hub reset is released,
658 * it may cause an invalid config being latched by usb251x. Particularly
659 * one of the config modes makes the hub loading a default registers
660 * value without SMBus-slave interface activation. If the hub
661 * accidentally gets this mode, this will cause the driver SMBus-
662 * functions failure. Normally we could just lock the SMBus-segment the
663 * hub i2c-interface resides for the device-specific reset timing. But
664 * the GPIO controller, which is used to handle the hub reset, might be
665 * placed at the same i2c-bus segment. In this case an error should be
666 * returned since we can't safely use the GPIO controller to clear the
667 * reset state (it may affect the hub configuration) and we can't lock
668 * the i2c-bus segment (it will cause a deadlock).
669 */
670 err = usb251x_check_gpio_chip(hub);
671 if (err)
672 return err;
673
Marco Felsch4d7201c2019-09-17 16:44:47 +0200674 err = regulator_enable(hub->vdd);
675 if (err)
676 return err;
677
Richard Leitner3ec72a22017-02-10 09:19:27 +0100678 err = usb251xb_connect(hub);
679 if (err) {
680 dev_err(dev, "Failed to connect hub (%d)\n", err);
681 return err;
682 }
683
684 dev_info(dev, "Hub probed successfully\n");
685
686 return 0;
687}
688
689static int usb251xb_i2c_probe(struct i2c_client *i2c,
690 const struct i2c_device_id *id)
691{
692 struct usb251xb *hub;
693
694 hub = devm_kzalloc(&i2c->dev, sizeof(struct usb251xb), GFP_KERNEL);
695 if (!hub)
696 return -ENOMEM;
697
698 i2c_set_clientdata(i2c, hub);
699 hub->dev = &i2c->dev;
700 hub->i2c = i2c;
701
702 return usb251xb_probe(hub);
703}
704
705static const struct i2c_device_id usb251xb_id[] = {
706 { "usb2512b", 0 },
707 { "usb2512bi", 0 },
708 { "usb2513b", 0 },
709 { "usb2513bi", 0 },
710 { "usb2514b", 0 },
711 { "usb2514bi", 0 },
Serge Semin7fcf5582017-10-22 23:38:04 +0300712 { "usb2517", 0 },
713 { "usb2517i", 0 },
Richard Leitner3ec72a22017-02-10 09:19:27 +0100714 { /* sentinel */ }
715};
716MODULE_DEVICE_TABLE(i2c, usb251xb_id);
717
718static struct i2c_driver usb251xb_i2c_driver = {
719 .driver = {
720 .name = DRIVER_NAME,
721 .of_match_table = of_match_ptr(usb251xb_of_match),
722 },
723 .probe = usb251xb_i2c_probe,
724 .id_table = usb251xb_id,
725};
726
727module_i2c_driver(usb251xb_i2c_driver);
728
729MODULE_AUTHOR("Richard Leitner <richard.leitner@skidata.com>");
Serge Semin7fcf5582017-10-22 23:38:04 +0300730MODULE_DESCRIPTION("USB251x/xBi USB 2.0 Hub Controller Driver");
Richard Leitner3ec72a22017-02-10 09:19:27 +0100731MODULE_LICENSE("GPL");