blob: 04c4e3fed09460968c4e9af6ccb283b19a37bbaa [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
Richard Leitner3ec72a22017-02-10 09:19:27 +010030
31#define USB251XB_ADDR_DEVICE_ID_LSB 0x04
32#define USB251XB_ADDR_DEVICE_ID_MSB 0x05
33#define USB251XB_DEF_DEVICE_ID 0x0BB3
34
35#define USB251XB_ADDR_CONFIG_DATA_1 0x06
36#define USB251XB_DEF_CONFIG_DATA_1 0x9B
37#define USB251XB_ADDR_CONFIG_DATA_2 0x07
38#define USB251XB_DEF_CONFIG_DATA_2 0x20
39#define USB251XB_ADDR_CONFIG_DATA_3 0x08
40#define USB251XB_DEF_CONFIG_DATA_3 0x02
41
42#define USB251XB_ADDR_NON_REMOVABLE_DEVICES 0x09
43#define USB251XB_DEF_NON_REMOVABLE_DEVICES 0x00
44
45#define USB251XB_ADDR_PORT_DISABLE_SELF 0x0A
46#define USB251XB_DEF_PORT_DISABLE_SELF 0x00
47#define USB251XB_ADDR_PORT_DISABLE_BUS 0x0B
48#define USB251XB_DEF_PORT_DISABLE_BUS 0x00
49
50#define USB251XB_ADDR_MAX_POWER_SELF 0x0C
51#define USB251XB_DEF_MAX_POWER_SELF 0x01
52#define USB251XB_ADDR_MAX_POWER_BUS 0x0D
53#define USB251XB_DEF_MAX_POWER_BUS 0x32
54
55#define USB251XB_ADDR_MAX_CURRENT_SELF 0x0E
56#define USB251XB_DEF_MAX_CURRENT_SELF 0x01
57#define USB251XB_ADDR_MAX_CURRENT_BUS 0x0F
58#define USB251XB_DEF_MAX_CURRENT_BUS 0x32
59
60#define USB251XB_ADDR_POWER_ON_TIME 0x10
61#define USB251XB_DEF_POWER_ON_TIME 0x32
62
63#define USB251XB_ADDR_LANGUAGE_ID_HIGH 0x11
64#define USB251XB_ADDR_LANGUAGE_ID_LOW 0x12
65#define USB251XB_DEF_LANGUAGE_ID 0x0000
66
67#define USB251XB_STRING_BUFSIZE 62
68#define USB251XB_ADDR_MANUFACTURER_STRING_LEN 0x13
69#define USB251XB_ADDR_MANUFACTURER_STRING 0x16
70#define USB251XB_DEF_MANUFACTURER_STRING "Microchip"
71
72#define USB251XB_ADDR_PRODUCT_STRING_LEN 0x14
73#define USB251XB_ADDR_PRODUCT_STRING 0x54
Richard Leitner3ec72a22017-02-10 09:19:27 +010074
75#define USB251XB_ADDR_SERIAL_STRING_LEN 0x15
76#define USB251XB_ADDR_SERIAL_STRING 0x92
77#define USB251XB_DEF_SERIAL_STRING ""
78
79#define USB251XB_ADDR_BATTERY_CHARGING_ENABLE 0xD0
80#define USB251XB_DEF_BATTERY_CHARGING_ENABLE 0x00
81
82#define USB251XB_ADDR_BOOST_UP 0xF6
83#define USB251XB_DEF_BOOST_UP 0x00
Serge Seminccdddc02017-10-22 23:38:07 +030084#define USB251XB_ADDR_BOOST_57 0xF7
85#define USB251XB_DEF_BOOST_57 0x00
86#define USB251XB_ADDR_BOOST_14 0xF8
87#define USB251XB_DEF_BOOST_14 0x00
Richard Leitner3ec72a22017-02-10 09:19:27 +010088
89#define USB251XB_ADDR_PORT_SWAP 0xFA
90#define USB251XB_DEF_PORT_SWAP 0x00
91
92#define USB251XB_ADDR_PORT_MAP_12 0xFB
93#define USB251XB_DEF_PORT_MAP_12 0x00
94#define USB251XB_ADDR_PORT_MAP_34 0xFC
Serge Semindb234b92017-10-22 23:38:06 +030095#define USB251XB_DEF_PORT_MAP_34 0x00 /* USB251{3B/i,4B/i,7/i} only */
96#define USB251XB_ADDR_PORT_MAP_56 0xFD
97#define USB251XB_DEF_PORT_MAP_56 0x00 /* USB2517/i only */
98#define USB251XB_ADDR_PORT_MAP_7 0xFE
99#define USB251XB_DEF_PORT_MAP_7 0x00 /* USB2517/i only */
Richard Leitner3ec72a22017-02-10 09:19:27 +0100100
101#define USB251XB_ADDR_STATUS_COMMAND 0xFF
102#define USB251XB_STATUS_COMMAND_SMBUS_DOWN 0x04
103#define USB251XB_STATUS_COMMAND_RESET 0x02
104#define USB251XB_STATUS_COMMAND_ATTACH 0x01
105
106#define USB251XB_I2C_REG_SZ 0x100
107#define USB251XB_I2C_WRITE_SZ 0x10
108
109#define DRIVER_NAME "usb251xb"
110#define DRIVER_DESC "Microchip USB 2.0 Hi-Speed Hub Controller"
Richard Leitner3ec72a22017-02-10 09:19:27 +0100111
112struct usb251xb {
113 struct device *dev;
114 struct i2c_client *i2c;
Marco Felsch4d7201c2019-09-17 16:44:47 +0200115 struct regulator *vdd;
Richard Leitner3ec72a22017-02-10 09:19:27 +0100116 u8 skip_config;
Serge Semin69a0c692017-10-22 23:38:12 +0300117 struct gpio_desc *gpio_reset;
Richard Leitner3ec72a22017-02-10 09:19:27 +0100118 u16 vendor_id;
119 u16 product_id;
120 u16 device_id;
121 u8 conf_data1;
122 u8 conf_data2;
123 u8 conf_data3;
124 u8 non_rem_dev;
125 u8 port_disable_sp;
126 u8 port_disable_bp;
127 u8 max_power_sp;
128 u8 max_power_bp;
129 u8 max_current_sp;
130 u8 max_current_bp;
131 u8 power_on_time;
132 u16 lang_id;
133 u8 manufacturer_len;
134 u8 product_len;
135 u8 serial_len;
136 char manufacturer[USB251XB_STRING_BUFSIZE];
137 char product[USB251XB_STRING_BUFSIZE];
138 char serial[USB251XB_STRING_BUFSIZE];
139 u8 bat_charge_en;
140 u8 boost_up;
Serge Seminccdddc02017-10-22 23:38:07 +0300141 u8 boost_57;
142 u8 boost_14;
Richard Leitner3ec72a22017-02-10 09:19:27 +0100143 u8 port_swap;
144 u8 port_map12;
145 u8 port_map34;
Serge Semindb234b92017-10-22 23:38:06 +0300146 u8 port_map56;
147 u8 port_map7;
Richard Leitner3ec72a22017-02-10 09:19:27 +0100148 u8 status;
149};
150
151struct usb251xb_data {
152 u16 product_id;
Serge Semin38481122017-10-22 23:38:05 +0300153 u8 port_cnt;
Serge Semin2818e132017-10-22 23:38:09 +0300154 bool led_support;
Serge Semindd99d102017-10-22 23:38:08 +0300155 bool bat_support;
Richard Leitner3ec72a22017-02-10 09:19:27 +0100156 char product_str[USB251XB_STRING_BUFSIZE / 2]; /* ASCII string */
157};
158
Uwe Kleine-Königcd7da3b2019-10-23 12:52:50 +0200159static const struct usb251xb_data usb2422_data = {
160 .product_id = 0x2422,
161 .port_cnt = 2,
162 .led_support = false,
163 .bat_support = true,
164 .product_str = "USB2422",
165};
166
Richard Leitner3ec72a22017-02-10 09:19:27 +0100167static const struct usb251xb_data usb2512b_data = {
168 .product_id = 0x2512,
Serge Semin38481122017-10-22 23:38:05 +0300169 .port_cnt = 2,
Serge Semin2818e132017-10-22 23:38:09 +0300170 .led_support = false,
Serge Semindd99d102017-10-22 23:38:08 +0300171 .bat_support = true,
Richard Leitner3ec72a22017-02-10 09:19:27 +0100172 .product_str = "USB2512B",
173};
174
175static const struct usb251xb_data usb2512bi_data = {
176 .product_id = 0x2512,
Serge Semin38481122017-10-22 23:38:05 +0300177 .port_cnt = 2,
Serge Semin2818e132017-10-22 23:38:09 +0300178 .led_support = false,
Serge Semindd99d102017-10-22 23:38:08 +0300179 .bat_support = true,
Richard Leitner3ec72a22017-02-10 09:19:27 +0100180 .product_str = "USB2512Bi",
181};
182
183static const struct usb251xb_data usb2513b_data = {
184 .product_id = 0x2513,
Serge Semin38481122017-10-22 23:38:05 +0300185 .port_cnt = 3,
Serge Semin2818e132017-10-22 23:38:09 +0300186 .led_support = false,
Serge Semindd99d102017-10-22 23:38:08 +0300187 .bat_support = true,
Richard Leitner3ec72a22017-02-10 09:19:27 +0100188 .product_str = "USB2513B",
189};
190
191static const struct usb251xb_data usb2513bi_data = {
192 .product_id = 0x2513,
Serge Semin38481122017-10-22 23:38:05 +0300193 .port_cnt = 3,
Serge Semin2818e132017-10-22 23:38:09 +0300194 .led_support = false,
Serge Semindd99d102017-10-22 23:38:08 +0300195 .bat_support = true,
Richard Leitner3ec72a22017-02-10 09:19:27 +0100196 .product_str = "USB2513Bi",
197};
198
199static const struct usb251xb_data usb2514b_data = {
200 .product_id = 0x2514,
Serge Semin38481122017-10-22 23:38:05 +0300201 .port_cnt = 4,
Serge Semin2818e132017-10-22 23:38:09 +0300202 .led_support = false,
Serge Semindd99d102017-10-22 23:38:08 +0300203 .bat_support = true,
Richard Leitner3ec72a22017-02-10 09:19:27 +0100204 .product_str = "USB2514B",
205};
206
207static const struct usb251xb_data usb2514bi_data = {
208 .product_id = 0x2514,
Serge Semin38481122017-10-22 23:38:05 +0300209 .port_cnt = 4,
Serge Semin2818e132017-10-22 23:38:09 +0300210 .led_support = false,
Serge Semindd99d102017-10-22 23:38:08 +0300211 .bat_support = true,
Richard Leitner3ec72a22017-02-10 09:19:27 +0100212 .product_str = "USB2514Bi",
213};
214
Serge Semin7fcf5582017-10-22 23:38:04 +0300215static const struct usb251xb_data usb2517_data = {
216 .product_id = 0x2517,
Serge Semin38481122017-10-22 23:38:05 +0300217 .port_cnt = 7,
Serge Semin2818e132017-10-22 23:38:09 +0300218 .led_support = true,
Serge Semindd99d102017-10-22 23:38:08 +0300219 .bat_support = false,
Serge Semin7fcf5582017-10-22 23:38:04 +0300220 .product_str = "USB2517",
221};
222
223static const struct usb251xb_data usb2517i_data = {
224 .product_id = 0x2517,
Serge Semin38481122017-10-22 23:38:05 +0300225 .port_cnt = 7,
Serge Semin2818e132017-10-22 23:38:09 +0300226 .led_support = true,
Serge Semindd99d102017-10-22 23:38:08 +0300227 .bat_support = false,
Serge Semin7fcf5582017-10-22 23:38:04 +0300228 .product_str = "USB2517i",
229};
230
Serge Semin95e060e2019-04-27 12:06:44 +0300231#ifdef CONFIG_GPIOLIB
Serge Semin6e3c8be2019-04-24 17:49:14 +0300232static int usb251xb_check_dev_children(struct device *dev, void *child)
233{
234 if (dev->type == &i2c_adapter_type) {
235 return device_for_each_child(dev, child,
236 usb251xb_check_dev_children);
237 }
238
239 return (dev == child);
240}
241
242static int usb251x_check_gpio_chip(struct usb251xb *hub)
243{
244 struct gpio_chip *gc = gpiod_to_chip(hub->gpio_reset);
245 struct i2c_adapter *adap = hub->i2c->adapter;
246 int ret;
247
248 if (!hub->gpio_reset)
249 return 0;
250
251 if (!gc)
252 return -EINVAL;
253
254 ret = usb251xb_check_dev_children(&adap->dev, gc->parent);
255 if (ret) {
256 dev_err(hub->dev, "Reset GPIO chip is at the same i2c-bus\n");
257 return -EINVAL;
258 }
259
260 return 0;
261}
Serge Semin95e060e2019-04-27 12:06:44 +0300262#else
263static int usb251x_check_gpio_chip(struct usb251xb *hub)
264{
265 return 0;
266}
267#endif
Serge Semin6e3c8be2019-04-24 17:49:14 +0300268
Marco Felscha9bab252019-09-17 16:44:48 +0200269static void usb251xb_reset(struct usb251xb *hub)
Richard Leitner3ec72a22017-02-10 09:19:27 +0100270{
Serge Semin69a0c692017-10-22 23:38:12 +0300271 if (!hub->gpio_reset)
Richard Leitner3ec72a22017-02-10 09:19:27 +0100272 return;
273
Serge Semin6e3c8be2019-04-24 17:49:14 +0300274 i2c_lock_bus(hub->i2c->adapter, I2C_LOCK_SEGMENT);
275
Marco Felscha9bab252019-09-17 16:44:48 +0200276 gpiod_set_value_cansleep(hub->gpio_reset, 1);
277 usleep_range(1, 10); /* >=1us RESET_N asserted */
278 gpiod_set_value_cansleep(hub->gpio_reset, 0);
Richard Leitner3ec72a22017-02-10 09:19:27 +0100279
280 /* wait for hub recovery/stabilization */
Marco Felscha9bab252019-09-17 16:44:48 +0200281 usleep_range(500, 750); /* >=500us after RESET_N deasserted */
Serge Semin6e3c8be2019-04-24 17:49:14 +0300282
283 i2c_unlock_bus(hub->i2c->adapter, I2C_LOCK_SEGMENT);
Richard Leitner3ec72a22017-02-10 09:19:27 +0100284}
285
286static int usb251xb_connect(struct usb251xb *hub)
287{
288 struct device *dev = hub->dev;
289 int err, i;
290 char i2c_wb[USB251XB_I2C_REG_SZ];
291
292 memset(i2c_wb, 0, USB251XB_I2C_REG_SZ);
293
294 if (hub->skip_config) {
295 dev_info(dev, "Skip hub configuration, only attach.\n");
296 i2c_wb[0] = 0x01;
297 i2c_wb[1] = USB251XB_STATUS_COMMAND_ATTACH;
298
Marco Felscha9bab252019-09-17 16:44:48 +0200299 usb251xb_reset(hub);
Richard Leitner3ec72a22017-02-10 09:19:27 +0100300
301 err = i2c_smbus_write_i2c_block_data(hub->i2c,
302 USB251XB_ADDR_STATUS_COMMAND, 2, i2c_wb);
303 if (err) {
304 dev_err(dev, "attaching hub failed: %d\n", err);
305 return err;
306 }
307 return 0;
308 }
309
310 i2c_wb[USB251XB_ADDR_VENDOR_ID_MSB] = (hub->vendor_id >> 8) & 0xFF;
311 i2c_wb[USB251XB_ADDR_VENDOR_ID_LSB] = hub->vendor_id & 0xFF;
312 i2c_wb[USB251XB_ADDR_PRODUCT_ID_MSB] = (hub->product_id >> 8) & 0xFF;
313 i2c_wb[USB251XB_ADDR_PRODUCT_ID_LSB] = hub->product_id & 0xFF;
314 i2c_wb[USB251XB_ADDR_DEVICE_ID_MSB] = (hub->device_id >> 8) & 0xFF;
315 i2c_wb[USB251XB_ADDR_DEVICE_ID_LSB] = hub->device_id & 0xFF;
316 i2c_wb[USB251XB_ADDR_CONFIG_DATA_1] = hub->conf_data1;
317 i2c_wb[USB251XB_ADDR_CONFIG_DATA_2] = hub->conf_data2;
318 i2c_wb[USB251XB_ADDR_CONFIG_DATA_3] = hub->conf_data3;
319 i2c_wb[USB251XB_ADDR_NON_REMOVABLE_DEVICES] = hub->non_rem_dev;
320 i2c_wb[USB251XB_ADDR_PORT_DISABLE_SELF] = hub->port_disable_sp;
321 i2c_wb[USB251XB_ADDR_PORT_DISABLE_BUS] = hub->port_disable_bp;
322 i2c_wb[USB251XB_ADDR_MAX_POWER_SELF] = hub->max_power_sp;
323 i2c_wb[USB251XB_ADDR_MAX_POWER_BUS] = hub->max_power_bp;
324 i2c_wb[USB251XB_ADDR_MAX_CURRENT_SELF] = hub->max_current_sp;
325 i2c_wb[USB251XB_ADDR_MAX_CURRENT_BUS] = hub->max_current_bp;
326 i2c_wb[USB251XB_ADDR_POWER_ON_TIME] = hub->power_on_time;
327 i2c_wb[USB251XB_ADDR_LANGUAGE_ID_HIGH] = (hub->lang_id >> 8) & 0xFF;
328 i2c_wb[USB251XB_ADDR_LANGUAGE_ID_LOW] = hub->lang_id & 0xFF;
329 i2c_wb[USB251XB_ADDR_MANUFACTURER_STRING_LEN] = hub->manufacturer_len;
330 i2c_wb[USB251XB_ADDR_PRODUCT_STRING_LEN] = hub->product_len;
331 i2c_wb[USB251XB_ADDR_SERIAL_STRING_LEN] = hub->serial_len;
332 memcpy(&i2c_wb[USB251XB_ADDR_MANUFACTURER_STRING], hub->manufacturer,
333 USB251XB_STRING_BUFSIZE);
334 memcpy(&i2c_wb[USB251XB_ADDR_SERIAL_STRING], hub->serial,
335 USB251XB_STRING_BUFSIZE);
336 memcpy(&i2c_wb[USB251XB_ADDR_PRODUCT_STRING], hub->product,
337 USB251XB_STRING_BUFSIZE);
338 i2c_wb[USB251XB_ADDR_BATTERY_CHARGING_ENABLE] = hub->bat_charge_en;
339 i2c_wb[USB251XB_ADDR_BOOST_UP] = hub->boost_up;
Serge Seminccdddc02017-10-22 23:38:07 +0300340 i2c_wb[USB251XB_ADDR_BOOST_57] = hub->boost_57;
341 i2c_wb[USB251XB_ADDR_BOOST_14] = hub->boost_14;
Richard Leitner3ec72a22017-02-10 09:19:27 +0100342 i2c_wb[USB251XB_ADDR_PORT_SWAP] = hub->port_swap;
343 i2c_wb[USB251XB_ADDR_PORT_MAP_12] = hub->port_map12;
344 i2c_wb[USB251XB_ADDR_PORT_MAP_34] = hub->port_map34;
Serge Semindb234b92017-10-22 23:38:06 +0300345 i2c_wb[USB251XB_ADDR_PORT_MAP_56] = hub->port_map56;
346 i2c_wb[USB251XB_ADDR_PORT_MAP_7] = hub->port_map7;
Richard Leitner3ec72a22017-02-10 09:19:27 +0100347 i2c_wb[USB251XB_ADDR_STATUS_COMMAND] = USB251XB_STATUS_COMMAND_ATTACH;
348
Marco Felscha9bab252019-09-17 16:44:48 +0200349 usb251xb_reset(hub);
Richard Leitner3ec72a22017-02-10 09:19:27 +0100350
351 /* write registers */
352 for (i = 0; i < (USB251XB_I2C_REG_SZ / USB251XB_I2C_WRITE_SZ); i++) {
353 int offset = i * USB251XB_I2C_WRITE_SZ;
354 char wbuf[USB251XB_I2C_WRITE_SZ + 1];
355
356 /* The first data byte transferred tells the hub how many data
357 * bytes will follow (byte count).
358 */
359 wbuf[0] = USB251XB_I2C_WRITE_SZ;
360 memcpy(&wbuf[1], &i2c_wb[offset], USB251XB_I2C_WRITE_SZ);
361
362 dev_dbg(dev, "writing %d byte block %d to 0x%02X\n",
363 USB251XB_I2C_WRITE_SZ, i, offset);
364
365 err = i2c_smbus_write_i2c_block_data(hub->i2c, offset,
366 USB251XB_I2C_WRITE_SZ + 1,
367 wbuf);
368 if (err)
369 goto out_err;
370 }
371
372 dev_info(dev, "Hub configuration was successful.\n");
373 return 0;
374
375out_err:
376 dev_err(dev, "configuring block %d failed: %d\n", i, err);
377 return err;
378}
379
380#ifdef CONFIG_OF
Serge Semin73d31de2019-04-25 00:12:06 +0300381static void usb251xb_get_ports_field(struct usb251xb *hub,
Lucas Stach4849ee62019-07-19 10:44:07 +0200382 const char *prop_name, u8 port_cnt,
383 bool ds_only, u8 *fld)
Serge Semin73d31de2019-04-25 00:12:06 +0300384{
385 struct device *dev = hub->dev;
386 struct property *prop;
387 const __be32 *p;
388 u32 port;
389
390 of_property_for_each_u32(dev->of_node, prop_name, prop, p, port) {
Lucas Stach4849ee62019-07-19 10:44:07 +0200391 if ((port >= ds_only ? 1 : 0) && (port <= port_cnt))
Serge Semin73d31de2019-04-25 00:12:06 +0300392 *fld |= BIT(port);
393 else
394 dev_warn(dev, "port %u doesn't exist\n", port);
395 }
396}
397
Richard Leitner3ec72a22017-02-10 09:19:27 +0100398static int usb251xb_get_ofdata(struct usb251xb *hub,
Stephen Boyd9243ead2021-01-22 19:44:26 -0800399 const struct usb251xb_data *data)
Richard Leitner3ec72a22017-02-10 09:19:27 +0100400{
401 struct device *dev = hub->dev;
402 struct device_node *np = dev->of_node;
Serge Semin73d31de2019-04-25 00:12:06 +0300403 int len, err;
404 u32 property_u32 = 0;
Richard Leitner3ec72a22017-02-10 09:19:27 +0100405 const char *cproperty_char;
406 char str[USB251XB_STRING_BUFSIZE / 2];
407
408 if (!np) {
409 dev_err(dev, "failed to get ofdata\n");
410 return -ENODEV;
411 }
412
413 if (of_get_property(np, "skip-config", NULL))
414 hub->skip_config = 1;
415 else
416 hub->skip_config = 0;
417
Serge Semin69a0c692017-10-22 23:38:12 +0300418 hub->gpio_reset = devm_gpiod_get_optional(dev, "reset", GPIOD_OUT_HIGH);
419 if (PTR_ERR(hub->gpio_reset) == -EPROBE_DEFER) {
Richard Leitner3ec72a22017-02-10 09:19:27 +0100420 return -EPROBE_DEFER;
Serge Semin69a0c692017-10-22 23:38:12 +0300421 } else if (IS_ERR(hub->gpio_reset)) {
422 err = PTR_ERR(hub->gpio_reset);
423 dev_err(dev, "unable to request GPIO reset pin (%d)\n", err);
424 return err;
Richard Leitner3ec72a22017-02-10 09:19:27 +0100425 }
426
427 if (of_property_read_u16_array(np, "vendor-id", &hub->vendor_id, 1))
428 hub->vendor_id = USB251XB_DEF_VENDOR_ID;
429
430 if (of_property_read_u16_array(np, "product-id",
431 &hub->product_id, 1))
432 hub->product_id = data->product_id;
433
434 if (of_property_read_u16_array(np, "device-id", &hub->device_id, 1))
435 hub->device_id = USB251XB_DEF_DEVICE_ID;
436
437 hub->conf_data1 = USB251XB_DEF_CONFIG_DATA_1;
438 if (of_get_property(np, "self-powered", NULL)) {
439 hub->conf_data1 |= BIT(7);
440
441 /* Configure Over-Current sens when self-powered */
442 hub->conf_data1 &= ~BIT(2);
443 if (of_get_property(np, "ganged-sensing", NULL))
444 hub->conf_data1 &= ~BIT(1);
445 else if (of_get_property(np, "individual-sensing", NULL))
446 hub->conf_data1 |= BIT(1);
447 } else if (of_get_property(np, "bus-powered", NULL)) {
448 hub->conf_data1 &= ~BIT(7);
449
450 /* Disable Over-Current sense when bus-powered */
451 hub->conf_data1 |= BIT(2);
452 }
453
454 if (of_get_property(np, "disable-hi-speed", NULL))
455 hub->conf_data1 |= BIT(5);
456
457 if (of_get_property(np, "multi-tt", NULL))
458 hub->conf_data1 |= BIT(4);
459 else if (of_get_property(np, "single-tt", NULL))
460 hub->conf_data1 &= ~BIT(4);
461
462 if (of_get_property(np, "disable-eop", NULL))
463 hub->conf_data1 |= BIT(3);
464
465 if (of_get_property(np, "individual-port-switching", NULL))
466 hub->conf_data1 |= BIT(0);
467 else if (of_get_property(np, "ganged-port-switching", NULL))
468 hub->conf_data1 &= ~BIT(0);
469
470 hub->conf_data2 = USB251XB_DEF_CONFIG_DATA_2;
471 if (of_get_property(np, "dynamic-power-switching", NULL))
472 hub->conf_data2 |= BIT(7);
473
Serge Semin287c2bb2017-10-22 23:38:10 +0300474 if (!of_property_read_u32(np, "oc-delay-us", &property_u32)) {
475 if (property_u32 == 100) {
Richard Leitner7f7d8ba2017-03-06 09:24:21 +0100476 /* 100 us*/
477 hub->conf_data2 &= ~BIT(5);
478 hub->conf_data2 &= ~BIT(4);
Serge Semin287c2bb2017-10-22 23:38:10 +0300479 } else if (property_u32 == 4000) {
Richard Leitner7f7d8ba2017-03-06 09:24:21 +0100480 /* 4 ms */
481 hub->conf_data2 &= ~BIT(5);
482 hub->conf_data2 |= BIT(4);
Serge Semin287c2bb2017-10-22 23:38:10 +0300483 } else if (property_u32 == 16000) {
Richard Leitner7f7d8ba2017-03-06 09:24:21 +0100484 /* 16 ms */
485 hub->conf_data2 |= BIT(5);
486 hub->conf_data2 |= BIT(4);
487 } else {
488 /* 8 ms (DEFAULT) */
489 hub->conf_data2 |= BIT(5);
490 hub->conf_data2 &= ~BIT(4);
491 }
Richard Leitner3ec72a22017-02-10 09:19:27 +0100492 }
493
494 if (of_get_property(np, "compound-device", NULL))
495 hub->conf_data2 |= BIT(3);
496
497 hub->conf_data3 = USB251XB_DEF_CONFIG_DATA_3;
498 if (of_get_property(np, "port-mapping-mode", NULL))
499 hub->conf_data3 |= BIT(3);
500
Serge Semin2818e132017-10-22 23:38:09 +0300501 if (data->led_support && of_get_property(np, "led-usb-mode", NULL))
502 hub->conf_data3 &= ~BIT(1);
503
Richard Leitner3ec72a22017-02-10 09:19:27 +0100504 if (of_get_property(np, "string-support", NULL))
505 hub->conf_data3 |= BIT(0);
506
507 hub->non_rem_dev = USB251XB_DEF_NON_REMOVABLE_DEVICES;
Serge Semin73d31de2019-04-25 00:12:06 +0300508 usb251xb_get_ports_field(hub, "non-removable-ports", data->port_cnt,
Lucas Stach4849ee62019-07-19 10:44:07 +0200509 true, &hub->non_rem_dev);
Richard Leitner3ec72a22017-02-10 09:19:27 +0100510
511 hub->port_disable_sp = USB251XB_DEF_PORT_DISABLE_SELF;
Serge Semin73d31de2019-04-25 00:12:06 +0300512 usb251xb_get_ports_field(hub, "sp-disabled-ports", data->port_cnt,
Lucas Stach4849ee62019-07-19 10:44:07 +0200513 true, &hub->port_disable_sp);
Richard Leitner3ec72a22017-02-10 09:19:27 +0100514
515 hub->port_disable_bp = USB251XB_DEF_PORT_DISABLE_BUS;
Serge Semin73d31de2019-04-25 00:12:06 +0300516 usb251xb_get_ports_field(hub, "bp-disabled-ports", data->port_cnt,
Lucas Stach4849ee62019-07-19 10:44:07 +0200517 true, &hub->port_disable_bp);
Richard Leitner3ec72a22017-02-10 09:19:27 +0100518
Serge Semin4ed466a2017-10-22 23:38:11 +0300519 hub->max_power_sp = USB251XB_DEF_MAX_POWER_SELF;
520 if (!of_property_read_u32(np, "sp-max-total-current-microamp",
521 &property_u32))
522 hub->max_power_sp = min_t(u8, property_u32 / 2000, 50);
523
524 hub->max_power_bp = USB251XB_DEF_MAX_POWER_BUS;
525 if (!of_property_read_u32(np, "bp-max-total-current-microamp",
526 &property_u32))
527 hub->max_power_bp = min_t(u8, property_u32 / 2000, 255);
528
529 hub->max_current_sp = USB251XB_DEF_MAX_CURRENT_SELF;
530 if (!of_property_read_u32(np, "sp-max-removable-current-microamp",
531 &property_u32))
532 hub->max_current_sp = min_t(u8, property_u32 / 2000, 50);
533
534 hub->max_current_bp = USB251XB_DEF_MAX_CURRENT_BUS;
535 if (!of_property_read_u32(np, "bp-max-removable-current-microamp",
536 &property_u32))
537 hub->max_current_bp = min_t(u8, property_u32 / 2000, 255);
538
Richard Leitner3ec72a22017-02-10 09:19:27 +0100539 hub->power_on_time = USB251XB_DEF_POWER_ON_TIME;
Serge Semin287c2bb2017-10-22 23:38:10 +0300540 if (!of_property_read_u32(np, "power-on-time-ms", &property_u32))
541 hub->power_on_time = min_t(u8, property_u32 / 2, 255);
Richard Leitner3ec72a22017-02-10 09:19:27 +0100542
543 if (of_property_read_u16_array(np, "language-id", &hub->lang_id, 1))
544 hub->lang_id = USB251XB_DEF_LANGUAGE_ID;
545
Tommaso Merciai5c2b9c62022-01-28 19:17:13 +0100546 if (of_property_read_u8(np, "boost-up", &hub->boost_up))
547 hub->boost_up = USB251XB_DEF_BOOST_UP;
548
Richard Leitner3ec72a22017-02-10 09:19:27 +0100549 cproperty_char = of_get_property(np, "manufacturer", NULL);
550 strlcpy(str, cproperty_char ? : USB251XB_DEF_MANUFACTURER_STRING,
551 sizeof(str));
552 hub->manufacturer_len = strlen(str) & 0xFF;
553 memset(hub->manufacturer, 0, USB251XB_STRING_BUFSIZE);
554 len = min_t(size_t, USB251XB_STRING_BUFSIZE / 2, strlen(str));
555 len = utf8s_to_utf16s(str, len, UTF16_LITTLE_ENDIAN,
556 (wchar_t *)hub->manufacturer,
557 USB251XB_STRING_BUFSIZE);
558
559 cproperty_char = of_get_property(np, "product", NULL);
560 strlcpy(str, cproperty_char ? : data->product_str, sizeof(str));
561 hub->product_len = strlen(str) & 0xFF;
562 memset(hub->product, 0, USB251XB_STRING_BUFSIZE);
563 len = min_t(size_t, USB251XB_STRING_BUFSIZE / 2, strlen(str));
564 len = utf8s_to_utf16s(str, len, UTF16_LITTLE_ENDIAN,
565 (wchar_t *)hub->product,
566 USB251XB_STRING_BUFSIZE);
567
568 cproperty_char = of_get_property(np, "serial", NULL);
569 strlcpy(str, cproperty_char ? : USB251XB_DEF_SERIAL_STRING,
570 sizeof(str));
571 hub->serial_len = strlen(str) & 0xFF;
572 memset(hub->serial, 0, USB251XB_STRING_BUFSIZE);
573 len = min_t(size_t, USB251XB_STRING_BUFSIZE / 2, strlen(str));
574 len = utf8s_to_utf16s(str, len, UTF16_LITTLE_ENDIAN,
575 (wchar_t *)hub->serial,
576 USB251XB_STRING_BUFSIZE);
577
Marco Felsch02a50b82019-01-16 15:20:37 +0100578 /*
579 * The datasheet documents the register as 'Port Swap' but in real the
580 * register controls the USB DP/DM signal swapping for each port.
581 */
582 hub->port_swap = USB251XB_DEF_PORT_SWAP;
Serge Semin73d31de2019-04-25 00:12:06 +0300583 usb251xb_get_ports_field(hub, "swap-dx-lanes", data->port_cnt,
Lucas Stach4849ee62019-07-19 10:44:07 +0200584 false, &hub->port_swap);
Marco Felsch02a50b82019-01-16 15:20:37 +0100585
Richard Leitner3ec72a22017-02-10 09:19:27 +0100586 /* The following parameters are currently not exposed to devicetree, but
587 * may be as soon as needed.
588 */
Richard Leitner3ec72a22017-02-10 09:19:27 +0100589 hub->bat_charge_en = USB251XB_DEF_BATTERY_CHARGING_ENABLE;
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 {
Uwe Kleine-Königcd7da3b2019-10-23 12:52:50 +0200602 .compatible = "microchip,usb2422",
603 .data = &usb2422_data,
604 }, {
Richard Leitner3ec72a22017-02-10 09:19:27 +0100605 .compatible = "microchip,usb2512b",
606 .data = &usb2512b_data,
607 }, {
608 .compatible = "microchip,usb2512bi",
609 .data = &usb2512bi_data,
610 }, {
611 .compatible = "microchip,usb2513b",
612 .data = &usb2513b_data,
613 }, {
614 .compatible = "microchip,usb2513bi",
615 .data = &usb2513bi_data,
616 }, {
617 .compatible = "microchip,usb2514b",
618 .data = &usb2514b_data,
619 }, {
620 .compatible = "microchip,usb2514bi",
621 .data = &usb2514bi_data,
622 }, {
Serge Semin7fcf5582017-10-22 23:38:04 +0300623 .compatible = "microchip,usb2517",
624 .data = &usb2517_data,
625 }, {
626 .compatible = "microchip,usb2517i",
627 .data = &usb2517i_data,
628 }, {
Richard Leitner3ec72a22017-02-10 09:19:27 +0100629 /* sentinel */
630 }
631};
632MODULE_DEVICE_TABLE(of, usb251xb_of_match);
633#else /* CONFIG_OF */
634static int usb251xb_get_ofdata(struct usb251xb *hub,
Stephen Boyd9243ead2021-01-22 19:44:26 -0800635 const struct usb251xb_data *data)
Richard Leitner3ec72a22017-02-10 09:19:27 +0100636{
637 return 0;
638}
639#endif /* CONFIG_OF */
640
Marco Felschf06947f2020-02-26 08:26:44 +0100641static void usb251xb_regulator_disable_action(void *data)
642{
643 struct usb251xb *hub = data;
644
645 regulator_disable(hub->vdd);
646}
647
Richard Leitner3ec72a22017-02-10 09:19:27 +0100648static int usb251xb_probe(struct usb251xb *hub)
649{
650 struct device *dev = hub->dev;
651 struct device_node *np = dev->of_node;
Stephen Boyd9243ead2021-01-22 19:44:26 -0800652 const struct usb251xb_data *usb_data = of_device_get_match_data(dev);
Richard Leitner3ec72a22017-02-10 09:19:27 +0100653 int err;
654
Stephen Boyd9243ead2021-01-22 19:44:26 -0800655 if (np && usb_data) {
656 err = usb251xb_get_ofdata(hub, usb_data);
Richard Leitner3ec72a22017-02-10 09:19:27 +0100657 if (err) {
658 dev_err(dev, "failed to get ofdata: %d\n", err);
659 return err;
660 }
661 }
662
Serge Semin6e3c8be2019-04-24 17:49:14 +0300663 /*
664 * usb251x SMBus-slave SCL lane is muxed with CFG_SEL0 pin. So if anyone
665 * tries to work with the bus at the moment the hub reset is released,
666 * it may cause an invalid config being latched by usb251x. Particularly
667 * one of the config modes makes the hub loading a default registers
668 * value without SMBus-slave interface activation. If the hub
669 * accidentally gets this mode, this will cause the driver SMBus-
670 * functions failure. Normally we could just lock the SMBus-segment the
671 * hub i2c-interface resides for the device-specific reset timing. But
672 * the GPIO controller, which is used to handle the hub reset, might be
673 * placed at the same i2c-bus segment. In this case an error should be
674 * returned since we can't safely use the GPIO controller to clear the
675 * reset state (it may affect the hub configuration) and we can't lock
676 * the i2c-bus segment (it will cause a deadlock).
677 */
678 err = usb251x_check_gpio_chip(hub);
679 if (err)
680 return err;
681
Marco Felschf06947f2020-02-26 08:26:44 +0100682 hub->vdd = devm_regulator_get(dev, "vdd");
683 if (IS_ERR(hub->vdd))
684 return PTR_ERR(hub->vdd);
685
Marco Felsch4d7201c2019-09-17 16:44:47 +0200686 err = regulator_enable(hub->vdd);
687 if (err)
688 return err;
689
Marco Felschf06947f2020-02-26 08:26:44 +0100690 err = devm_add_action_or_reset(dev,
691 usb251xb_regulator_disable_action, hub);
692 if (err)
693 return err;
694
Richard Leitner3ec72a22017-02-10 09:19:27 +0100695 err = usb251xb_connect(hub);
696 if (err) {
697 dev_err(dev, "Failed to connect hub (%d)\n", err);
698 return err;
699 }
700
701 dev_info(dev, "Hub probed successfully\n");
702
703 return 0;
704}
705
706static int usb251xb_i2c_probe(struct i2c_client *i2c,
707 const struct i2c_device_id *id)
708{
709 struct usb251xb *hub;
710
711 hub = devm_kzalloc(&i2c->dev, sizeof(struct usb251xb), GFP_KERNEL);
712 if (!hub)
713 return -ENOMEM;
714
715 i2c_set_clientdata(i2c, hub);
716 hub->dev = &i2c->dev;
717 hub->i2c = i2c;
718
719 return usb251xb_probe(hub);
720}
721
Marco Felsch905eccc2019-09-17 16:44:49 +0200722static int __maybe_unused usb251xb_suspend(struct device *dev)
723{
724 struct i2c_client *client = to_i2c_client(dev);
725 struct usb251xb *hub = i2c_get_clientdata(client);
726
727 return regulator_disable(hub->vdd);
728}
729
730static int __maybe_unused usb251xb_resume(struct device *dev)
731{
732 struct i2c_client *client = to_i2c_client(dev);
733 struct usb251xb *hub = i2c_get_clientdata(client);
734 int err;
735
736 err = regulator_enable(hub->vdd);
737 if (err)
738 return err;
739
740 return usb251xb_connect(hub);
741}
742
743static SIMPLE_DEV_PM_OPS(usb251xb_pm_ops, usb251xb_suspend, usb251xb_resume);
744
Richard Leitner3ec72a22017-02-10 09:19:27 +0100745static const struct i2c_device_id usb251xb_id[] = {
Uwe Kleine-Königcd7da3b2019-10-23 12:52:50 +0200746 { "usb2422", 0 },
Richard Leitner3ec72a22017-02-10 09:19:27 +0100747 { "usb2512b", 0 },
748 { "usb2512bi", 0 },
749 { "usb2513b", 0 },
750 { "usb2513bi", 0 },
751 { "usb2514b", 0 },
752 { "usb2514bi", 0 },
Serge Semin7fcf5582017-10-22 23:38:04 +0300753 { "usb2517", 0 },
754 { "usb2517i", 0 },
Richard Leitner3ec72a22017-02-10 09:19:27 +0100755 { /* sentinel */ }
756};
757MODULE_DEVICE_TABLE(i2c, usb251xb_id);
758
759static struct i2c_driver usb251xb_i2c_driver = {
760 .driver = {
761 .name = DRIVER_NAME,
762 .of_match_table = of_match_ptr(usb251xb_of_match),
Marco Felsch905eccc2019-09-17 16:44:49 +0200763 .pm = &usb251xb_pm_ops,
Richard Leitner3ec72a22017-02-10 09:19:27 +0100764 },
765 .probe = usb251xb_i2c_probe,
766 .id_table = usb251xb_id,
767};
768
769module_i2c_driver(usb251xb_i2c_driver);
770
771MODULE_AUTHOR("Richard Leitner <richard.leitner@skidata.com>");
Serge Semin7fcf5582017-10-22 23:38:04 +0300772MODULE_DESCRIPTION("USB251x/xBi USB 2.0 Hub Controller Driver");
Richard Leitner3ec72a22017-02-10 09:19:27 +0100773MODULE_LICENSE("GPL");