blob: 939b3bedd4c888216ee1290a511e52c00e4fb4b5 [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>
Richard Leitner3ec72a22017-02-10 09:19:27 +010020#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 */
Serge Semin7fcf5582017-10-22 23:38:04 +030032#define USB251XB_DEF_PRODUCT_ID_17 0x2517 /* USB2517/17i */
Richard Leitner3ec72a22017-02-10 09:19:27 +010033
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
Serge Semin7fcf5582017-10-22 23:38:04 +030077#define USB251XB_DEF_PRODUCT_STRING "USB251xB/xBi/7i"
Richard Leitner3ec72a22017-02-10 09:19:27 +010078
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
Serge Seminccdddc02017-10-22 23:38:07 +030088#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
Richard Leitner3ec72a22017-02-10 09:19:27 +010092
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
Serge Semindb234b92017-10-22 23:38:06 +030099#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 */
Richard Leitner3ec72a22017-02-10 09:19:27 +0100104
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"
Richard Leitner3ec72a22017-02-10 09:19:27 +0100115
116struct usb251xb {
117 struct device *dev;
118 struct i2c_client *i2c;
119 u8 skip_config;
Serge Semin69a0c692017-10-22 23:38:12 +0300120 struct gpio_desc *gpio_reset;
Richard Leitner3ec72a22017-02-10 09:19:27 +0100121 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;
Serge Seminccdddc02017-10-22 23:38:07 +0300144 u8 boost_57;
145 u8 boost_14;
Richard Leitner3ec72a22017-02-10 09:19:27 +0100146 u8 port_swap;
147 u8 port_map12;
148 u8 port_map34;
Serge Semindb234b92017-10-22 23:38:06 +0300149 u8 port_map56;
150 u8 port_map7;
Richard Leitner3ec72a22017-02-10 09:19:27 +0100151 u8 status;
152};
153
154struct usb251xb_data {
155 u16 product_id;
Serge Semin38481122017-10-22 23:38:05 +0300156 u8 port_cnt;
Serge Semin2818e132017-10-22 23:38:09 +0300157 bool led_support;
Serge Semindd99d102017-10-22 23:38:08 +0300158 bool bat_support;
Richard Leitner3ec72a22017-02-10 09:19:27 +0100159 char product_str[USB251XB_STRING_BUFSIZE / 2]; /* ASCII string */
160};
161
162static const struct usb251xb_data usb2512b_data = {
163 .product_id = 0x2512,
Serge Semin38481122017-10-22 23:38:05 +0300164 .port_cnt = 2,
Serge Semin2818e132017-10-22 23:38:09 +0300165 .led_support = false,
Serge Semindd99d102017-10-22 23:38:08 +0300166 .bat_support = true,
Richard Leitner3ec72a22017-02-10 09:19:27 +0100167 .product_str = "USB2512B",
168};
169
170static const struct usb251xb_data usb2512bi_data = {
171 .product_id = 0x2512,
Serge Semin38481122017-10-22 23:38:05 +0300172 .port_cnt = 2,
Serge Semin2818e132017-10-22 23:38:09 +0300173 .led_support = false,
Serge Semindd99d102017-10-22 23:38:08 +0300174 .bat_support = true,
Richard Leitner3ec72a22017-02-10 09:19:27 +0100175 .product_str = "USB2512Bi",
176};
177
178static const struct usb251xb_data usb2513b_data = {
179 .product_id = 0x2513,
Serge Semin38481122017-10-22 23:38:05 +0300180 .port_cnt = 3,
Serge Semin2818e132017-10-22 23:38:09 +0300181 .led_support = false,
Serge Semindd99d102017-10-22 23:38:08 +0300182 .bat_support = true,
Richard Leitner3ec72a22017-02-10 09:19:27 +0100183 .product_str = "USB2513B",
184};
185
186static const struct usb251xb_data usb2513bi_data = {
187 .product_id = 0x2513,
Serge Semin38481122017-10-22 23:38:05 +0300188 .port_cnt = 3,
Serge Semin2818e132017-10-22 23:38:09 +0300189 .led_support = false,
Serge Semindd99d102017-10-22 23:38:08 +0300190 .bat_support = true,
Richard Leitner3ec72a22017-02-10 09:19:27 +0100191 .product_str = "USB2513Bi",
192};
193
194static const struct usb251xb_data usb2514b_data = {
195 .product_id = 0x2514,
Serge Semin38481122017-10-22 23:38:05 +0300196 .port_cnt = 4,
Serge Semin2818e132017-10-22 23:38:09 +0300197 .led_support = false,
Serge Semindd99d102017-10-22 23:38:08 +0300198 .bat_support = true,
Richard Leitner3ec72a22017-02-10 09:19:27 +0100199 .product_str = "USB2514B",
200};
201
202static const struct usb251xb_data usb2514bi_data = {
203 .product_id = 0x2514,
Serge Semin38481122017-10-22 23:38:05 +0300204 .port_cnt = 4,
Serge Semin2818e132017-10-22 23:38:09 +0300205 .led_support = false,
Serge Semindd99d102017-10-22 23:38:08 +0300206 .bat_support = true,
Richard Leitner3ec72a22017-02-10 09:19:27 +0100207 .product_str = "USB2514Bi",
208};
209
Serge Semin7fcf5582017-10-22 23:38:04 +0300210static const struct usb251xb_data usb2517_data = {
211 .product_id = 0x2517,
Serge Semin38481122017-10-22 23:38:05 +0300212 .port_cnt = 7,
Serge Semin2818e132017-10-22 23:38:09 +0300213 .led_support = true,
Serge Semindd99d102017-10-22 23:38:08 +0300214 .bat_support = false,
Serge Semin7fcf5582017-10-22 23:38:04 +0300215 .product_str = "USB2517",
216};
217
218static const struct usb251xb_data usb2517i_data = {
219 .product_id = 0x2517,
Serge Semin38481122017-10-22 23:38:05 +0300220 .port_cnt = 7,
Serge Semin2818e132017-10-22 23:38:09 +0300221 .led_support = true,
Serge Semindd99d102017-10-22 23:38:08 +0300222 .bat_support = false,
Serge Semin7fcf5582017-10-22 23:38:04 +0300223 .product_str = "USB2517i",
224};
225
Serge Semin6e3c8be2019-04-24 17:49:14 +0300226static int usb251xb_check_dev_children(struct device *dev, void *child)
227{
228 if (dev->type == &i2c_adapter_type) {
229 return device_for_each_child(dev, child,
230 usb251xb_check_dev_children);
231 }
232
233 return (dev == child);
234}
235
236static int usb251x_check_gpio_chip(struct usb251xb *hub)
237{
238 struct gpio_chip *gc = gpiod_to_chip(hub->gpio_reset);
239 struct i2c_adapter *adap = hub->i2c->adapter;
240 int ret;
241
242 if (!hub->gpio_reset)
243 return 0;
244
245 if (!gc)
246 return -EINVAL;
247
248 ret = usb251xb_check_dev_children(&adap->dev, gc->parent);
249 if (ret) {
250 dev_err(hub->dev, "Reset GPIO chip is at the same i2c-bus\n");
251 return -EINVAL;
252 }
253
254 return 0;
255}
256
Richard Leitner3ec72a22017-02-10 09:19:27 +0100257static void usb251xb_reset(struct usb251xb *hub, int state)
258{
Serge Semin69a0c692017-10-22 23:38:12 +0300259 if (!hub->gpio_reset)
Richard Leitner3ec72a22017-02-10 09:19:27 +0100260 return;
261
Serge Semin6e3c8be2019-04-24 17:49:14 +0300262 i2c_lock_bus(hub->i2c->adapter, I2C_LOCK_SEGMENT);
263
Serge Semin69a0c692017-10-22 23:38:12 +0300264 gpiod_set_value_cansleep(hub->gpio_reset, state);
Richard Leitner3ec72a22017-02-10 09:19:27 +0100265
266 /* wait for hub recovery/stabilization */
Serge Semin69a0c692017-10-22 23:38:12 +0300267 if (!state)
Richard Leitner3ec72a22017-02-10 09:19:27 +0100268 usleep_range(500, 750); /* >=500us at power on */
269 else
270 usleep_range(1, 10); /* >=1us at power down */
Serge Semin6e3c8be2019-04-24 17:49:14 +0300271
272 i2c_unlock_bus(hub->i2c->adapter, I2C_LOCK_SEGMENT);
Richard Leitner3ec72a22017-02-10 09:19:27 +0100273}
274
275static int usb251xb_connect(struct usb251xb *hub)
276{
277 struct device *dev = hub->dev;
278 int err, i;
279 char i2c_wb[USB251XB_I2C_REG_SZ];
280
281 memset(i2c_wb, 0, USB251XB_I2C_REG_SZ);
282
283 if (hub->skip_config) {
284 dev_info(dev, "Skip hub configuration, only attach.\n");
285 i2c_wb[0] = 0x01;
286 i2c_wb[1] = USB251XB_STATUS_COMMAND_ATTACH;
287
Serge Semin69a0c692017-10-22 23:38:12 +0300288 usb251xb_reset(hub, 0);
Richard Leitner3ec72a22017-02-10 09:19:27 +0100289
290 err = i2c_smbus_write_i2c_block_data(hub->i2c,
291 USB251XB_ADDR_STATUS_COMMAND, 2, i2c_wb);
292 if (err) {
293 dev_err(dev, "attaching hub failed: %d\n", err);
294 return err;
295 }
296 return 0;
297 }
298
299 i2c_wb[USB251XB_ADDR_VENDOR_ID_MSB] = (hub->vendor_id >> 8) & 0xFF;
300 i2c_wb[USB251XB_ADDR_VENDOR_ID_LSB] = hub->vendor_id & 0xFF;
301 i2c_wb[USB251XB_ADDR_PRODUCT_ID_MSB] = (hub->product_id >> 8) & 0xFF;
302 i2c_wb[USB251XB_ADDR_PRODUCT_ID_LSB] = hub->product_id & 0xFF;
303 i2c_wb[USB251XB_ADDR_DEVICE_ID_MSB] = (hub->device_id >> 8) & 0xFF;
304 i2c_wb[USB251XB_ADDR_DEVICE_ID_LSB] = hub->device_id & 0xFF;
305 i2c_wb[USB251XB_ADDR_CONFIG_DATA_1] = hub->conf_data1;
306 i2c_wb[USB251XB_ADDR_CONFIG_DATA_2] = hub->conf_data2;
307 i2c_wb[USB251XB_ADDR_CONFIG_DATA_3] = hub->conf_data3;
308 i2c_wb[USB251XB_ADDR_NON_REMOVABLE_DEVICES] = hub->non_rem_dev;
309 i2c_wb[USB251XB_ADDR_PORT_DISABLE_SELF] = hub->port_disable_sp;
310 i2c_wb[USB251XB_ADDR_PORT_DISABLE_BUS] = hub->port_disable_bp;
311 i2c_wb[USB251XB_ADDR_MAX_POWER_SELF] = hub->max_power_sp;
312 i2c_wb[USB251XB_ADDR_MAX_POWER_BUS] = hub->max_power_bp;
313 i2c_wb[USB251XB_ADDR_MAX_CURRENT_SELF] = hub->max_current_sp;
314 i2c_wb[USB251XB_ADDR_MAX_CURRENT_BUS] = hub->max_current_bp;
315 i2c_wb[USB251XB_ADDR_POWER_ON_TIME] = hub->power_on_time;
316 i2c_wb[USB251XB_ADDR_LANGUAGE_ID_HIGH] = (hub->lang_id >> 8) & 0xFF;
317 i2c_wb[USB251XB_ADDR_LANGUAGE_ID_LOW] = hub->lang_id & 0xFF;
318 i2c_wb[USB251XB_ADDR_MANUFACTURER_STRING_LEN] = hub->manufacturer_len;
319 i2c_wb[USB251XB_ADDR_PRODUCT_STRING_LEN] = hub->product_len;
320 i2c_wb[USB251XB_ADDR_SERIAL_STRING_LEN] = hub->serial_len;
321 memcpy(&i2c_wb[USB251XB_ADDR_MANUFACTURER_STRING], hub->manufacturer,
322 USB251XB_STRING_BUFSIZE);
323 memcpy(&i2c_wb[USB251XB_ADDR_SERIAL_STRING], hub->serial,
324 USB251XB_STRING_BUFSIZE);
325 memcpy(&i2c_wb[USB251XB_ADDR_PRODUCT_STRING], hub->product,
326 USB251XB_STRING_BUFSIZE);
327 i2c_wb[USB251XB_ADDR_BATTERY_CHARGING_ENABLE] = hub->bat_charge_en;
328 i2c_wb[USB251XB_ADDR_BOOST_UP] = hub->boost_up;
Serge Seminccdddc02017-10-22 23:38:07 +0300329 i2c_wb[USB251XB_ADDR_BOOST_57] = hub->boost_57;
330 i2c_wb[USB251XB_ADDR_BOOST_14] = hub->boost_14;
Richard Leitner3ec72a22017-02-10 09:19:27 +0100331 i2c_wb[USB251XB_ADDR_PORT_SWAP] = hub->port_swap;
332 i2c_wb[USB251XB_ADDR_PORT_MAP_12] = hub->port_map12;
333 i2c_wb[USB251XB_ADDR_PORT_MAP_34] = hub->port_map34;
Serge Semindb234b92017-10-22 23:38:06 +0300334 i2c_wb[USB251XB_ADDR_PORT_MAP_56] = hub->port_map56;
335 i2c_wb[USB251XB_ADDR_PORT_MAP_7] = hub->port_map7;
Richard Leitner3ec72a22017-02-10 09:19:27 +0100336 i2c_wb[USB251XB_ADDR_STATUS_COMMAND] = USB251XB_STATUS_COMMAND_ATTACH;
337
Serge Semin69a0c692017-10-22 23:38:12 +0300338 usb251xb_reset(hub, 0);
Richard Leitner3ec72a22017-02-10 09:19:27 +0100339
340 /* write registers */
341 for (i = 0; i < (USB251XB_I2C_REG_SZ / USB251XB_I2C_WRITE_SZ); i++) {
342 int offset = i * USB251XB_I2C_WRITE_SZ;
343 char wbuf[USB251XB_I2C_WRITE_SZ + 1];
344
345 /* The first data byte transferred tells the hub how many data
346 * bytes will follow (byte count).
347 */
348 wbuf[0] = USB251XB_I2C_WRITE_SZ;
349 memcpy(&wbuf[1], &i2c_wb[offset], USB251XB_I2C_WRITE_SZ);
350
351 dev_dbg(dev, "writing %d byte block %d to 0x%02X\n",
352 USB251XB_I2C_WRITE_SZ, i, offset);
353
354 err = i2c_smbus_write_i2c_block_data(hub->i2c, offset,
355 USB251XB_I2C_WRITE_SZ + 1,
356 wbuf);
357 if (err)
358 goto out_err;
359 }
360
361 dev_info(dev, "Hub configuration was successful.\n");
362 return 0;
363
364out_err:
365 dev_err(dev, "configuring block %d failed: %d\n", i, err);
366 return err;
367}
368
369#ifdef CONFIG_OF
370static int usb251xb_get_ofdata(struct usb251xb *hub,
371 struct usb251xb_data *data)
372{
373 struct device *dev = hub->dev;
374 struct device_node *np = dev->of_node;
375 int len, err, i;
Marco Felsch02a50b82019-01-16 15:20:37 +0100376 u32 port, property_u32 = 0;
Richard Leitner3ec72a22017-02-10 09:19:27 +0100377 const u32 *cproperty_u32;
378 const char *cproperty_char;
379 char str[USB251XB_STRING_BUFSIZE / 2];
Marco Felsch02a50b82019-01-16 15:20:37 +0100380 struct property *prop;
381 const __be32 *p;
Richard Leitner3ec72a22017-02-10 09:19:27 +0100382
383 if (!np) {
384 dev_err(dev, "failed to get ofdata\n");
385 return -ENODEV;
386 }
387
388 if (of_get_property(np, "skip-config", NULL))
389 hub->skip_config = 1;
390 else
391 hub->skip_config = 0;
392
Serge Semin69a0c692017-10-22 23:38:12 +0300393 hub->gpio_reset = devm_gpiod_get_optional(dev, "reset", GPIOD_OUT_HIGH);
394 if (PTR_ERR(hub->gpio_reset) == -EPROBE_DEFER) {
Richard Leitner3ec72a22017-02-10 09:19:27 +0100395 return -EPROBE_DEFER;
Serge Semin69a0c692017-10-22 23:38:12 +0300396 } else if (IS_ERR(hub->gpio_reset)) {
397 err = PTR_ERR(hub->gpio_reset);
398 dev_err(dev, "unable to request GPIO reset pin (%d)\n", err);
399 return err;
Richard Leitner3ec72a22017-02-10 09:19:27 +0100400 }
401
402 if (of_property_read_u16_array(np, "vendor-id", &hub->vendor_id, 1))
403 hub->vendor_id = USB251XB_DEF_VENDOR_ID;
404
405 if (of_property_read_u16_array(np, "product-id",
406 &hub->product_id, 1))
407 hub->product_id = data->product_id;
408
409 if (of_property_read_u16_array(np, "device-id", &hub->device_id, 1))
410 hub->device_id = USB251XB_DEF_DEVICE_ID;
411
412 hub->conf_data1 = USB251XB_DEF_CONFIG_DATA_1;
413 if (of_get_property(np, "self-powered", NULL)) {
414 hub->conf_data1 |= BIT(7);
415
416 /* Configure Over-Current sens when self-powered */
417 hub->conf_data1 &= ~BIT(2);
418 if (of_get_property(np, "ganged-sensing", NULL))
419 hub->conf_data1 &= ~BIT(1);
420 else if (of_get_property(np, "individual-sensing", NULL))
421 hub->conf_data1 |= BIT(1);
422 } else if (of_get_property(np, "bus-powered", NULL)) {
423 hub->conf_data1 &= ~BIT(7);
424
425 /* Disable Over-Current sense when bus-powered */
426 hub->conf_data1 |= BIT(2);
427 }
428
429 if (of_get_property(np, "disable-hi-speed", NULL))
430 hub->conf_data1 |= BIT(5);
431
432 if (of_get_property(np, "multi-tt", NULL))
433 hub->conf_data1 |= BIT(4);
434 else if (of_get_property(np, "single-tt", NULL))
435 hub->conf_data1 &= ~BIT(4);
436
437 if (of_get_property(np, "disable-eop", NULL))
438 hub->conf_data1 |= BIT(3);
439
440 if (of_get_property(np, "individual-port-switching", NULL))
441 hub->conf_data1 |= BIT(0);
442 else if (of_get_property(np, "ganged-port-switching", NULL))
443 hub->conf_data1 &= ~BIT(0);
444
445 hub->conf_data2 = USB251XB_DEF_CONFIG_DATA_2;
446 if (of_get_property(np, "dynamic-power-switching", NULL))
447 hub->conf_data2 |= BIT(7);
448
Serge Semin287c2bb2017-10-22 23:38:10 +0300449 if (!of_property_read_u32(np, "oc-delay-us", &property_u32)) {
450 if (property_u32 == 100) {
Richard Leitner7f7d8ba2017-03-06 09:24:21 +0100451 /* 100 us*/
452 hub->conf_data2 &= ~BIT(5);
453 hub->conf_data2 &= ~BIT(4);
Serge Semin287c2bb2017-10-22 23:38:10 +0300454 } else if (property_u32 == 4000) {
Richard Leitner7f7d8ba2017-03-06 09:24:21 +0100455 /* 4 ms */
456 hub->conf_data2 &= ~BIT(5);
457 hub->conf_data2 |= BIT(4);
Serge Semin287c2bb2017-10-22 23:38:10 +0300458 } else if (property_u32 == 16000) {
Richard Leitner7f7d8ba2017-03-06 09:24:21 +0100459 /* 16 ms */
460 hub->conf_data2 |= BIT(5);
461 hub->conf_data2 |= BIT(4);
462 } else {
463 /* 8 ms (DEFAULT) */
464 hub->conf_data2 |= BIT(5);
465 hub->conf_data2 &= ~BIT(4);
466 }
Richard Leitner3ec72a22017-02-10 09:19:27 +0100467 }
468
469 if (of_get_property(np, "compound-device", NULL))
470 hub->conf_data2 |= BIT(3);
471
472 hub->conf_data3 = USB251XB_DEF_CONFIG_DATA_3;
473 if (of_get_property(np, "port-mapping-mode", NULL))
474 hub->conf_data3 |= BIT(3);
475
Serge Semin2818e132017-10-22 23:38:09 +0300476 if (data->led_support && of_get_property(np, "led-usb-mode", NULL))
477 hub->conf_data3 &= ~BIT(1);
478
Richard Leitner3ec72a22017-02-10 09:19:27 +0100479 if (of_get_property(np, "string-support", NULL))
480 hub->conf_data3 |= BIT(0);
481
482 hub->non_rem_dev = USB251XB_DEF_NON_REMOVABLE_DEVICES;
483 cproperty_u32 = of_get_property(np, "non-removable-ports", &len);
484 if (cproperty_u32 && (len / sizeof(u32)) > 0) {
485 for (i = 0; i < len / sizeof(u32); i++) {
486 u32 port = be32_to_cpu(cproperty_u32[i]);
487
Serge Semin38481122017-10-22 23:38:05 +0300488 if ((port >= 1) && (port <= data->port_cnt))
Richard Leitner3ec72a22017-02-10 09:19:27 +0100489 hub->non_rem_dev |= BIT(port);
Serge Semin38481122017-10-22 23:38:05 +0300490 else
491 dev_warn(dev, "NRD port %u doesn't exist\n",
492 port);
Richard Leitner3ec72a22017-02-10 09:19:27 +0100493 }
494 }
495
496 hub->port_disable_sp = USB251XB_DEF_PORT_DISABLE_SELF;
497 cproperty_u32 = of_get_property(np, "sp-disabled-ports", &len);
498 if (cproperty_u32 && (len / sizeof(u32)) > 0) {
499 for (i = 0; i < len / sizeof(u32); i++) {
500 u32 port = be32_to_cpu(cproperty_u32[i]);
501
Serge Semin38481122017-10-22 23:38:05 +0300502 if ((port >= 1) && (port <= data->port_cnt))
Richard Leitner3ec72a22017-02-10 09:19:27 +0100503 hub->port_disable_sp |= BIT(port);
Serge Semin38481122017-10-22 23:38:05 +0300504 else
505 dev_warn(dev, "PDS port %u doesn't exist\n",
506 port);
Richard Leitner3ec72a22017-02-10 09:19:27 +0100507 }
508 }
509
510 hub->port_disable_bp = USB251XB_DEF_PORT_DISABLE_BUS;
511 cproperty_u32 = of_get_property(np, "bp-disabled-ports", &len);
512 if (cproperty_u32 && (len / sizeof(u32)) > 0) {
513 for (i = 0; i < len / sizeof(u32); i++) {
514 u32 port = be32_to_cpu(cproperty_u32[i]);
515
Serge Semin38481122017-10-22 23:38:05 +0300516 if ((port >= 1) && (port <= data->port_cnt))
Richard Leitner3ec72a22017-02-10 09:19:27 +0100517 hub->port_disable_bp |= BIT(port);
Serge Semin38481122017-10-22 23:38:05 +0300518 else
519 dev_warn(dev, "PDB port %u doesn't exist\n",
520 port);
Richard Leitner3ec72a22017-02-10 09:19:27 +0100521 }
522 }
523
Serge Semin4ed466a2017-10-22 23:38:11 +0300524 hub->max_power_sp = USB251XB_DEF_MAX_POWER_SELF;
525 if (!of_property_read_u32(np, "sp-max-total-current-microamp",
526 &property_u32))
527 hub->max_power_sp = min_t(u8, property_u32 / 2000, 50);
528
529 hub->max_power_bp = USB251XB_DEF_MAX_POWER_BUS;
530 if (!of_property_read_u32(np, "bp-max-total-current-microamp",
531 &property_u32))
532 hub->max_power_bp = min_t(u8, property_u32 / 2000, 255);
533
534 hub->max_current_sp = USB251XB_DEF_MAX_CURRENT_SELF;
535 if (!of_property_read_u32(np, "sp-max-removable-current-microamp",
536 &property_u32))
537 hub->max_current_sp = min_t(u8, property_u32 / 2000, 50);
538
539 hub->max_current_bp = USB251XB_DEF_MAX_CURRENT_BUS;
540 if (!of_property_read_u32(np, "bp-max-removable-current-microamp",
541 &property_u32))
542 hub->max_current_bp = min_t(u8, property_u32 / 2000, 255);
543
Richard Leitner3ec72a22017-02-10 09:19:27 +0100544 hub->power_on_time = USB251XB_DEF_POWER_ON_TIME;
Serge Semin287c2bb2017-10-22 23:38:10 +0300545 if (!of_property_read_u32(np, "power-on-time-ms", &property_u32))
546 hub->power_on_time = min_t(u8, property_u32 / 2, 255);
Richard Leitner3ec72a22017-02-10 09:19:27 +0100547
548 if (of_property_read_u16_array(np, "language-id", &hub->lang_id, 1))
549 hub->lang_id = USB251XB_DEF_LANGUAGE_ID;
550
551 cproperty_char = of_get_property(np, "manufacturer", NULL);
552 strlcpy(str, cproperty_char ? : USB251XB_DEF_MANUFACTURER_STRING,
553 sizeof(str));
554 hub->manufacturer_len = strlen(str) & 0xFF;
555 memset(hub->manufacturer, 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->manufacturer,
559 USB251XB_STRING_BUFSIZE);
560
561 cproperty_char = of_get_property(np, "product", NULL);
562 strlcpy(str, cproperty_char ? : data->product_str, sizeof(str));
563 hub->product_len = strlen(str) & 0xFF;
564 memset(hub->product, 0, USB251XB_STRING_BUFSIZE);
565 len = min_t(size_t, USB251XB_STRING_BUFSIZE / 2, strlen(str));
566 len = utf8s_to_utf16s(str, len, UTF16_LITTLE_ENDIAN,
567 (wchar_t *)hub->product,
568 USB251XB_STRING_BUFSIZE);
569
570 cproperty_char = of_get_property(np, "serial", NULL);
571 strlcpy(str, cproperty_char ? : USB251XB_DEF_SERIAL_STRING,
572 sizeof(str));
573 hub->serial_len = strlen(str) & 0xFF;
574 memset(hub->serial, 0, USB251XB_STRING_BUFSIZE);
575 len = min_t(size_t, USB251XB_STRING_BUFSIZE / 2, strlen(str));
576 len = utf8s_to_utf16s(str, len, UTF16_LITTLE_ENDIAN,
577 (wchar_t *)hub->serial,
578 USB251XB_STRING_BUFSIZE);
579
Marco Felsch02a50b82019-01-16 15:20:37 +0100580 /*
581 * The datasheet documents the register as 'Port Swap' but in real the
582 * register controls the USB DP/DM signal swapping for each port.
583 */
584 hub->port_swap = USB251XB_DEF_PORT_SWAP;
585 of_property_for_each_u32(np, "swap-dx-lanes", prop, p, port) {
Gustavo A. R. Silva22feda42019-03-18 09:50:24 -0500586 if (port <= data->port_cnt)
Marco Felsch02a50b82019-01-16 15:20:37 +0100587 hub->port_swap |= BIT(port);
588 }
589
Richard Leitner3ec72a22017-02-10 09:19:27 +0100590 /* The following parameters are currently not exposed to devicetree, but
591 * may be as soon as needed.
592 */
Richard Leitner3ec72a22017-02-10 09:19:27 +0100593 hub->bat_charge_en = USB251XB_DEF_BATTERY_CHARGING_ENABLE;
594 hub->boost_up = USB251XB_DEF_BOOST_UP;
Serge Seminccdddc02017-10-22 23:38:07 +0300595 hub->boost_57 = USB251XB_DEF_BOOST_57;
596 hub->boost_14 = USB251XB_DEF_BOOST_14;
Richard Leitner3ec72a22017-02-10 09:19:27 +0100597 hub->port_map12 = USB251XB_DEF_PORT_MAP_12;
598 hub->port_map34 = USB251XB_DEF_PORT_MAP_34;
Serge Semindb234b92017-10-22 23:38:06 +0300599 hub->port_map56 = USB251XB_DEF_PORT_MAP_56;
600 hub->port_map7 = USB251XB_DEF_PORT_MAP_7;
Richard Leitner3ec72a22017-02-10 09:19:27 +0100601
602 return 0;
603}
604
605static const struct of_device_id usb251xb_of_match[] = {
606 {
607 .compatible = "microchip,usb2512b",
608 .data = &usb2512b_data,
609 }, {
610 .compatible = "microchip,usb2512bi",
611 .data = &usb2512bi_data,
612 }, {
613 .compatible = "microchip,usb2513b",
614 .data = &usb2513b_data,
615 }, {
616 .compatible = "microchip,usb2513bi",
617 .data = &usb2513bi_data,
618 }, {
619 .compatible = "microchip,usb2514b",
620 .data = &usb2514b_data,
621 }, {
622 .compatible = "microchip,usb2514bi",
623 .data = &usb2514bi_data,
624 }, {
Serge Semin7fcf5582017-10-22 23:38:04 +0300625 .compatible = "microchip,usb2517",
626 .data = &usb2517_data,
627 }, {
628 .compatible = "microchip,usb2517i",
629 .data = &usb2517i_data,
630 }, {
Richard Leitner3ec72a22017-02-10 09:19:27 +0100631 /* sentinel */
632 }
633};
634MODULE_DEVICE_TABLE(of, usb251xb_of_match);
635#else /* CONFIG_OF */
636static int usb251xb_get_ofdata(struct usb251xb *hub,
637 struct usb251xb_data *data)
638{
639 return 0;
640}
641#endif /* CONFIG_OF */
642
643static int usb251xb_probe(struct usb251xb *hub)
644{
645 struct device *dev = hub->dev;
646 struct device_node *np = dev->of_node;
647 const struct of_device_id *of_id = of_match_device(usb251xb_of_match,
648 dev);
649 int err;
650
Aditya Pakki41f00e62019-03-20 10:27:11 -0500651 if (np && of_id) {
Richard Leitner3ec72a22017-02-10 09:19:27 +0100652 err = usb251xb_get_ofdata(hub,
653 (struct usb251xb_data *)of_id->data);
654 if (err) {
655 dev_err(dev, "failed to get ofdata: %d\n", err);
656 return err;
657 }
658 }
659
Serge Semin6e3c8be2019-04-24 17:49:14 +0300660 /*
661 * usb251x SMBus-slave SCL lane is muxed with CFG_SEL0 pin. So if anyone
662 * tries to work with the bus at the moment the hub reset is released,
663 * it may cause an invalid config being latched by usb251x. Particularly
664 * one of the config modes makes the hub loading a default registers
665 * value without SMBus-slave interface activation. If the hub
666 * accidentally gets this mode, this will cause the driver SMBus-
667 * functions failure. Normally we could just lock the SMBus-segment the
668 * hub i2c-interface resides for the device-specific reset timing. But
669 * the GPIO controller, which is used to handle the hub reset, might be
670 * placed at the same i2c-bus segment. In this case an error should be
671 * returned since we can't safely use the GPIO controller to clear the
672 * reset state (it may affect the hub configuration) and we can't lock
673 * the i2c-bus segment (it will cause a deadlock).
674 */
675 err = usb251x_check_gpio_chip(hub);
676 if (err)
677 return err;
678
Richard Leitner3ec72a22017-02-10 09:19:27 +0100679 err = usb251xb_connect(hub);
680 if (err) {
681 dev_err(dev, "Failed to connect hub (%d)\n", err);
682 return err;
683 }
684
685 dev_info(dev, "Hub probed successfully\n");
686
687 return 0;
688}
689
690static int usb251xb_i2c_probe(struct i2c_client *i2c,
691 const struct i2c_device_id *id)
692{
693 struct usb251xb *hub;
694
695 hub = devm_kzalloc(&i2c->dev, sizeof(struct usb251xb), GFP_KERNEL);
696 if (!hub)
697 return -ENOMEM;
698
699 i2c_set_clientdata(i2c, hub);
700 hub->dev = &i2c->dev;
701 hub->i2c = i2c;
702
703 return usb251xb_probe(hub);
704}
705
706static const struct i2c_device_id usb251xb_id[] = {
707 { "usb2512b", 0 },
708 { "usb2512bi", 0 },
709 { "usb2513b", 0 },
710 { "usb2513bi", 0 },
711 { "usb2514b", 0 },
712 { "usb2514bi", 0 },
Serge Semin7fcf5582017-10-22 23:38:04 +0300713 { "usb2517", 0 },
714 { "usb2517i", 0 },
Richard Leitner3ec72a22017-02-10 09:19:27 +0100715 { /* sentinel */ }
716};
717MODULE_DEVICE_TABLE(i2c, usb251xb_id);
718
719static struct i2c_driver usb251xb_i2c_driver = {
720 .driver = {
721 .name = DRIVER_NAME,
722 .of_match_table = of_match_ptr(usb251xb_of_match),
723 },
724 .probe = usb251xb_i2c_probe,
725 .id_table = usb251xb_id,
726};
727
728module_i2c_driver(usb251xb_i2c_driver);
729
730MODULE_AUTHOR("Richard Leitner <richard.leitner@skidata.com>");
Serge Semin7fcf5582017-10-22 23:38:04 +0300731MODULE_DESCRIPTION("USB251x/xBi USB 2.0 Hub Controller Driver");
Richard Leitner3ec72a22017-02-10 09:19:27 +0100732MODULE_LICENSE("GPL");