blob: 530ef13b353d4782a1282cc5223d092af4001003 [file] [log] [blame]
Richard Leitner3ec72a22017-02-10 09:19:27 +01001/*
2 * Driver for Microchip USB251xB USB 2.0 Hi-Speed Hub Controller
3 * Configuration via SMBus.
4 *
5 * Copyright (c) 2017 SKIDATA AG
6 *
7 * This work is based on the USB3503 driver by Dongjin Kim and
8 * a not-accepted patch by Fabien Lahoudere, see:
9 * https://patchwork.kernel.org/patch/9257715/
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 */
21
22#include <linux/delay.h>
23#include <linux/gpio.h>
24#include <linux/i2c.h>
25#include <linux/module.h>
26#include <linux/nls.h>
27#include <linux/of_device.h>
28#include <linux/of_gpio.h>
29#include <linux/slab.h>
30
31/* Internal Register Set Addresses & Default Values acc. to DS00001692C */
32#define USB251XB_ADDR_VENDOR_ID_LSB 0x00
33#define USB251XB_ADDR_VENDOR_ID_MSB 0x01
34#define USB251XB_DEF_VENDOR_ID 0x0424
35
36#define USB251XB_ADDR_PRODUCT_ID_LSB 0x02
37#define USB251XB_ADDR_PRODUCT_ID_MSB 0x03
38#define USB251XB_DEF_PRODUCT_ID_12 0x2512 /* USB2512B/12Bi */
39#define USB251XB_DEF_PRODUCT_ID_13 0x2513 /* USB2513B/13Bi */
40#define USB251XB_DEF_PRODUCT_ID_14 0x2514 /* USB2514B/14Bi */
Serge Semin7fcf5582017-10-22 23:38:04 +030041#define USB251XB_DEF_PRODUCT_ID_17 0x2517 /* USB2517/17i */
Richard Leitner3ec72a22017-02-10 09:19:27 +010042
43#define USB251XB_ADDR_DEVICE_ID_LSB 0x04
44#define USB251XB_ADDR_DEVICE_ID_MSB 0x05
45#define USB251XB_DEF_DEVICE_ID 0x0BB3
46
47#define USB251XB_ADDR_CONFIG_DATA_1 0x06
48#define USB251XB_DEF_CONFIG_DATA_1 0x9B
49#define USB251XB_ADDR_CONFIG_DATA_2 0x07
50#define USB251XB_DEF_CONFIG_DATA_2 0x20
51#define USB251XB_ADDR_CONFIG_DATA_3 0x08
52#define USB251XB_DEF_CONFIG_DATA_3 0x02
53
54#define USB251XB_ADDR_NON_REMOVABLE_DEVICES 0x09
55#define USB251XB_DEF_NON_REMOVABLE_DEVICES 0x00
56
57#define USB251XB_ADDR_PORT_DISABLE_SELF 0x0A
58#define USB251XB_DEF_PORT_DISABLE_SELF 0x00
59#define USB251XB_ADDR_PORT_DISABLE_BUS 0x0B
60#define USB251XB_DEF_PORT_DISABLE_BUS 0x00
61
62#define USB251XB_ADDR_MAX_POWER_SELF 0x0C
63#define USB251XB_DEF_MAX_POWER_SELF 0x01
64#define USB251XB_ADDR_MAX_POWER_BUS 0x0D
65#define USB251XB_DEF_MAX_POWER_BUS 0x32
66
67#define USB251XB_ADDR_MAX_CURRENT_SELF 0x0E
68#define USB251XB_DEF_MAX_CURRENT_SELF 0x01
69#define USB251XB_ADDR_MAX_CURRENT_BUS 0x0F
70#define USB251XB_DEF_MAX_CURRENT_BUS 0x32
71
72#define USB251XB_ADDR_POWER_ON_TIME 0x10
73#define USB251XB_DEF_POWER_ON_TIME 0x32
74
75#define USB251XB_ADDR_LANGUAGE_ID_HIGH 0x11
76#define USB251XB_ADDR_LANGUAGE_ID_LOW 0x12
77#define USB251XB_DEF_LANGUAGE_ID 0x0000
78
79#define USB251XB_STRING_BUFSIZE 62
80#define USB251XB_ADDR_MANUFACTURER_STRING_LEN 0x13
81#define USB251XB_ADDR_MANUFACTURER_STRING 0x16
82#define USB251XB_DEF_MANUFACTURER_STRING "Microchip"
83
84#define USB251XB_ADDR_PRODUCT_STRING_LEN 0x14
85#define USB251XB_ADDR_PRODUCT_STRING 0x54
Serge Semin7fcf5582017-10-22 23:38:04 +030086#define USB251XB_DEF_PRODUCT_STRING "USB251xB/xBi/7i"
Richard Leitner3ec72a22017-02-10 09:19:27 +010087
88#define USB251XB_ADDR_SERIAL_STRING_LEN 0x15
89#define USB251XB_ADDR_SERIAL_STRING 0x92
90#define USB251XB_DEF_SERIAL_STRING ""
91
92#define USB251XB_ADDR_BATTERY_CHARGING_ENABLE 0xD0
93#define USB251XB_DEF_BATTERY_CHARGING_ENABLE 0x00
94
95#define USB251XB_ADDR_BOOST_UP 0xF6
96#define USB251XB_DEF_BOOST_UP 0x00
Serge Seminccdddc02017-10-22 23:38:07 +030097#define USB251XB_ADDR_BOOST_57 0xF7
98#define USB251XB_DEF_BOOST_57 0x00
99#define USB251XB_ADDR_BOOST_14 0xF8
100#define USB251XB_DEF_BOOST_14 0x00
Richard Leitner3ec72a22017-02-10 09:19:27 +0100101
102#define USB251XB_ADDR_PORT_SWAP 0xFA
103#define USB251XB_DEF_PORT_SWAP 0x00
104
105#define USB251XB_ADDR_PORT_MAP_12 0xFB
106#define USB251XB_DEF_PORT_MAP_12 0x00
107#define USB251XB_ADDR_PORT_MAP_34 0xFC
Serge Semindb234b92017-10-22 23:38:06 +0300108#define USB251XB_DEF_PORT_MAP_34 0x00 /* USB251{3B/i,4B/i,7/i} only */
109#define USB251XB_ADDR_PORT_MAP_56 0xFD
110#define USB251XB_DEF_PORT_MAP_56 0x00 /* USB2517/i only */
111#define USB251XB_ADDR_PORT_MAP_7 0xFE
112#define USB251XB_DEF_PORT_MAP_7 0x00 /* USB2517/i only */
Richard Leitner3ec72a22017-02-10 09:19:27 +0100113
114#define USB251XB_ADDR_STATUS_COMMAND 0xFF
115#define USB251XB_STATUS_COMMAND_SMBUS_DOWN 0x04
116#define USB251XB_STATUS_COMMAND_RESET 0x02
117#define USB251XB_STATUS_COMMAND_ATTACH 0x01
118
119#define USB251XB_I2C_REG_SZ 0x100
120#define USB251XB_I2C_WRITE_SZ 0x10
121
122#define DRIVER_NAME "usb251xb"
123#define DRIVER_DESC "Microchip USB 2.0 Hi-Speed Hub Controller"
Richard Leitner3ec72a22017-02-10 09:19:27 +0100124
125struct usb251xb {
126 struct device *dev;
127 struct i2c_client *i2c;
128 u8 skip_config;
129 int gpio_reset;
130 u16 vendor_id;
131 u16 product_id;
132 u16 device_id;
133 u8 conf_data1;
134 u8 conf_data2;
135 u8 conf_data3;
136 u8 non_rem_dev;
137 u8 port_disable_sp;
138 u8 port_disable_bp;
139 u8 max_power_sp;
140 u8 max_power_bp;
141 u8 max_current_sp;
142 u8 max_current_bp;
143 u8 power_on_time;
144 u16 lang_id;
145 u8 manufacturer_len;
146 u8 product_len;
147 u8 serial_len;
148 char manufacturer[USB251XB_STRING_BUFSIZE];
149 char product[USB251XB_STRING_BUFSIZE];
150 char serial[USB251XB_STRING_BUFSIZE];
151 u8 bat_charge_en;
152 u8 boost_up;
Serge Seminccdddc02017-10-22 23:38:07 +0300153 u8 boost_57;
154 u8 boost_14;
Richard Leitner3ec72a22017-02-10 09:19:27 +0100155 u8 port_swap;
156 u8 port_map12;
157 u8 port_map34;
Serge Semindb234b92017-10-22 23:38:06 +0300158 u8 port_map56;
159 u8 port_map7;
Richard Leitner3ec72a22017-02-10 09:19:27 +0100160 u8 status;
161};
162
163struct usb251xb_data {
164 u16 product_id;
Serge Semin38481122017-10-22 23:38:05 +0300165 u8 port_cnt;
Richard Leitner3ec72a22017-02-10 09:19:27 +0100166 char product_str[USB251XB_STRING_BUFSIZE / 2]; /* ASCII string */
167};
168
169static const struct usb251xb_data usb2512b_data = {
170 .product_id = 0x2512,
Serge Semin38481122017-10-22 23:38:05 +0300171 .port_cnt = 2,
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,
Richard Leitner3ec72a22017-02-10 09:19:27 +0100178 .product_str = "USB2512Bi",
179};
180
181static const struct usb251xb_data usb2513b_data = {
182 .product_id = 0x2513,
Serge Semin38481122017-10-22 23:38:05 +0300183 .port_cnt = 3,
Richard Leitner3ec72a22017-02-10 09:19:27 +0100184 .product_str = "USB2513B",
185};
186
187static const struct usb251xb_data usb2513bi_data = {
188 .product_id = 0x2513,
Serge Semin38481122017-10-22 23:38:05 +0300189 .port_cnt = 3,
Richard Leitner3ec72a22017-02-10 09:19:27 +0100190 .product_str = "USB2513Bi",
191};
192
193static const struct usb251xb_data usb2514b_data = {
194 .product_id = 0x2514,
Serge Semin38481122017-10-22 23:38:05 +0300195 .port_cnt = 4,
Richard Leitner3ec72a22017-02-10 09:19:27 +0100196 .product_str = "USB2514B",
197};
198
199static const struct usb251xb_data usb2514bi_data = {
200 .product_id = 0x2514,
Serge Semin38481122017-10-22 23:38:05 +0300201 .port_cnt = 4,
Richard Leitner3ec72a22017-02-10 09:19:27 +0100202 .product_str = "USB2514Bi",
203};
204
Serge Semin7fcf5582017-10-22 23:38:04 +0300205static const struct usb251xb_data usb2517_data = {
206 .product_id = 0x2517,
Serge Semin38481122017-10-22 23:38:05 +0300207 .port_cnt = 7,
Serge Semin7fcf5582017-10-22 23:38:04 +0300208 .product_str = "USB2517",
209};
210
211static const struct usb251xb_data usb2517i_data = {
212 .product_id = 0x2517,
Serge Semin38481122017-10-22 23:38:05 +0300213 .port_cnt = 7,
Serge Semin7fcf5582017-10-22 23:38:04 +0300214 .product_str = "USB2517i",
215};
216
Richard Leitner3ec72a22017-02-10 09:19:27 +0100217static void usb251xb_reset(struct usb251xb *hub, int state)
218{
219 if (!gpio_is_valid(hub->gpio_reset))
220 return;
221
222 gpio_set_value_cansleep(hub->gpio_reset, state);
223
224 /* wait for hub recovery/stabilization */
225 if (state)
226 usleep_range(500, 750); /* >=500us at power on */
227 else
228 usleep_range(1, 10); /* >=1us at power down */
229}
230
231static int usb251xb_connect(struct usb251xb *hub)
232{
233 struct device *dev = hub->dev;
234 int err, i;
235 char i2c_wb[USB251XB_I2C_REG_SZ];
236
237 memset(i2c_wb, 0, USB251XB_I2C_REG_SZ);
238
239 if (hub->skip_config) {
240 dev_info(dev, "Skip hub configuration, only attach.\n");
241 i2c_wb[0] = 0x01;
242 i2c_wb[1] = USB251XB_STATUS_COMMAND_ATTACH;
243
244 usb251xb_reset(hub, 1);
245
246 err = i2c_smbus_write_i2c_block_data(hub->i2c,
247 USB251XB_ADDR_STATUS_COMMAND, 2, i2c_wb);
248 if (err) {
249 dev_err(dev, "attaching hub failed: %d\n", err);
250 return err;
251 }
252 return 0;
253 }
254
255 i2c_wb[USB251XB_ADDR_VENDOR_ID_MSB] = (hub->vendor_id >> 8) & 0xFF;
256 i2c_wb[USB251XB_ADDR_VENDOR_ID_LSB] = hub->vendor_id & 0xFF;
257 i2c_wb[USB251XB_ADDR_PRODUCT_ID_MSB] = (hub->product_id >> 8) & 0xFF;
258 i2c_wb[USB251XB_ADDR_PRODUCT_ID_LSB] = hub->product_id & 0xFF;
259 i2c_wb[USB251XB_ADDR_DEVICE_ID_MSB] = (hub->device_id >> 8) & 0xFF;
260 i2c_wb[USB251XB_ADDR_DEVICE_ID_LSB] = hub->device_id & 0xFF;
261 i2c_wb[USB251XB_ADDR_CONFIG_DATA_1] = hub->conf_data1;
262 i2c_wb[USB251XB_ADDR_CONFIG_DATA_2] = hub->conf_data2;
263 i2c_wb[USB251XB_ADDR_CONFIG_DATA_3] = hub->conf_data3;
264 i2c_wb[USB251XB_ADDR_NON_REMOVABLE_DEVICES] = hub->non_rem_dev;
265 i2c_wb[USB251XB_ADDR_PORT_DISABLE_SELF] = hub->port_disable_sp;
266 i2c_wb[USB251XB_ADDR_PORT_DISABLE_BUS] = hub->port_disable_bp;
267 i2c_wb[USB251XB_ADDR_MAX_POWER_SELF] = hub->max_power_sp;
268 i2c_wb[USB251XB_ADDR_MAX_POWER_BUS] = hub->max_power_bp;
269 i2c_wb[USB251XB_ADDR_MAX_CURRENT_SELF] = hub->max_current_sp;
270 i2c_wb[USB251XB_ADDR_MAX_CURRENT_BUS] = hub->max_current_bp;
271 i2c_wb[USB251XB_ADDR_POWER_ON_TIME] = hub->power_on_time;
272 i2c_wb[USB251XB_ADDR_LANGUAGE_ID_HIGH] = (hub->lang_id >> 8) & 0xFF;
273 i2c_wb[USB251XB_ADDR_LANGUAGE_ID_LOW] = hub->lang_id & 0xFF;
274 i2c_wb[USB251XB_ADDR_MANUFACTURER_STRING_LEN] = hub->manufacturer_len;
275 i2c_wb[USB251XB_ADDR_PRODUCT_STRING_LEN] = hub->product_len;
276 i2c_wb[USB251XB_ADDR_SERIAL_STRING_LEN] = hub->serial_len;
277 memcpy(&i2c_wb[USB251XB_ADDR_MANUFACTURER_STRING], hub->manufacturer,
278 USB251XB_STRING_BUFSIZE);
279 memcpy(&i2c_wb[USB251XB_ADDR_SERIAL_STRING], hub->serial,
280 USB251XB_STRING_BUFSIZE);
281 memcpy(&i2c_wb[USB251XB_ADDR_PRODUCT_STRING], hub->product,
282 USB251XB_STRING_BUFSIZE);
283 i2c_wb[USB251XB_ADDR_BATTERY_CHARGING_ENABLE] = hub->bat_charge_en;
284 i2c_wb[USB251XB_ADDR_BOOST_UP] = hub->boost_up;
Serge Seminccdddc02017-10-22 23:38:07 +0300285 i2c_wb[USB251XB_ADDR_BOOST_57] = hub->boost_57;
286 i2c_wb[USB251XB_ADDR_BOOST_14] = hub->boost_14;
Richard Leitner3ec72a22017-02-10 09:19:27 +0100287 i2c_wb[USB251XB_ADDR_PORT_SWAP] = hub->port_swap;
288 i2c_wb[USB251XB_ADDR_PORT_MAP_12] = hub->port_map12;
289 i2c_wb[USB251XB_ADDR_PORT_MAP_34] = hub->port_map34;
Serge Semindb234b92017-10-22 23:38:06 +0300290 i2c_wb[USB251XB_ADDR_PORT_MAP_56] = hub->port_map56;
291 i2c_wb[USB251XB_ADDR_PORT_MAP_7] = hub->port_map7;
Richard Leitner3ec72a22017-02-10 09:19:27 +0100292 i2c_wb[USB251XB_ADDR_STATUS_COMMAND] = USB251XB_STATUS_COMMAND_ATTACH;
293
294 usb251xb_reset(hub, 1);
295
296 /* write registers */
297 for (i = 0; i < (USB251XB_I2C_REG_SZ / USB251XB_I2C_WRITE_SZ); i++) {
298 int offset = i * USB251XB_I2C_WRITE_SZ;
299 char wbuf[USB251XB_I2C_WRITE_SZ + 1];
300
301 /* The first data byte transferred tells the hub how many data
302 * bytes will follow (byte count).
303 */
304 wbuf[0] = USB251XB_I2C_WRITE_SZ;
305 memcpy(&wbuf[1], &i2c_wb[offset], USB251XB_I2C_WRITE_SZ);
306
307 dev_dbg(dev, "writing %d byte block %d to 0x%02X\n",
308 USB251XB_I2C_WRITE_SZ, i, offset);
309
310 err = i2c_smbus_write_i2c_block_data(hub->i2c, offset,
311 USB251XB_I2C_WRITE_SZ + 1,
312 wbuf);
313 if (err)
314 goto out_err;
315 }
316
317 dev_info(dev, "Hub configuration was successful.\n");
318 return 0;
319
320out_err:
321 dev_err(dev, "configuring block %d failed: %d\n", i, err);
322 return err;
323}
324
325#ifdef CONFIG_OF
326static int usb251xb_get_ofdata(struct usb251xb *hub,
327 struct usb251xb_data *data)
328{
329 struct device *dev = hub->dev;
330 struct device_node *np = dev->of_node;
331 int len, err, i;
332 u32 *property_u32 = NULL;
333 const u32 *cproperty_u32;
334 const char *cproperty_char;
335 char str[USB251XB_STRING_BUFSIZE / 2];
336
337 if (!np) {
338 dev_err(dev, "failed to get ofdata\n");
339 return -ENODEV;
340 }
341
342 if (of_get_property(np, "skip-config", NULL))
343 hub->skip_config = 1;
344 else
345 hub->skip_config = 0;
346
347 hub->gpio_reset = of_get_named_gpio(np, "reset-gpios", 0);
348 if (hub->gpio_reset == -EPROBE_DEFER)
349 return -EPROBE_DEFER;
350 if (gpio_is_valid(hub->gpio_reset)) {
351 err = devm_gpio_request_one(dev, hub->gpio_reset,
352 GPIOF_OUT_INIT_LOW,
353 "usb251xb reset");
354 if (err) {
355 dev_err(dev,
356 "unable to request GPIO %d as reset pin (%d)\n",
357 hub->gpio_reset, err);
358 return err;
359 }
360 }
361
362 if (of_property_read_u16_array(np, "vendor-id", &hub->vendor_id, 1))
363 hub->vendor_id = USB251XB_DEF_VENDOR_ID;
364
365 if (of_property_read_u16_array(np, "product-id",
366 &hub->product_id, 1))
367 hub->product_id = data->product_id;
368
369 if (of_property_read_u16_array(np, "device-id", &hub->device_id, 1))
370 hub->device_id = USB251XB_DEF_DEVICE_ID;
371
372 hub->conf_data1 = USB251XB_DEF_CONFIG_DATA_1;
373 if (of_get_property(np, "self-powered", NULL)) {
374 hub->conf_data1 |= BIT(7);
375
376 /* Configure Over-Current sens when self-powered */
377 hub->conf_data1 &= ~BIT(2);
378 if (of_get_property(np, "ganged-sensing", NULL))
379 hub->conf_data1 &= ~BIT(1);
380 else if (of_get_property(np, "individual-sensing", NULL))
381 hub->conf_data1 |= BIT(1);
382 } else if (of_get_property(np, "bus-powered", NULL)) {
383 hub->conf_data1 &= ~BIT(7);
384
385 /* Disable Over-Current sense when bus-powered */
386 hub->conf_data1 |= BIT(2);
387 }
388
389 if (of_get_property(np, "disable-hi-speed", NULL))
390 hub->conf_data1 |= BIT(5);
391
392 if (of_get_property(np, "multi-tt", NULL))
393 hub->conf_data1 |= BIT(4);
394 else if (of_get_property(np, "single-tt", NULL))
395 hub->conf_data1 &= ~BIT(4);
396
397 if (of_get_property(np, "disable-eop", NULL))
398 hub->conf_data1 |= BIT(3);
399
400 if (of_get_property(np, "individual-port-switching", NULL))
401 hub->conf_data1 |= BIT(0);
402 else if (of_get_property(np, "ganged-port-switching", NULL))
403 hub->conf_data1 &= ~BIT(0);
404
405 hub->conf_data2 = USB251XB_DEF_CONFIG_DATA_2;
406 if (of_get_property(np, "dynamic-power-switching", NULL))
407 hub->conf_data2 |= BIT(7);
408
Richard Leitner7f7d8ba2017-03-06 09:24:21 +0100409 if (!of_property_read_u32(np, "oc-delay-us", property_u32)) {
410 if (*property_u32 == 100) {
411 /* 100 us*/
412 hub->conf_data2 &= ~BIT(5);
413 hub->conf_data2 &= ~BIT(4);
414 } else if (*property_u32 == 4000) {
415 /* 4 ms */
416 hub->conf_data2 &= ~BIT(5);
417 hub->conf_data2 |= BIT(4);
418 } else if (*property_u32 == 16000) {
419 /* 16 ms */
420 hub->conf_data2 |= BIT(5);
421 hub->conf_data2 |= BIT(4);
422 } else {
423 /* 8 ms (DEFAULT) */
424 hub->conf_data2 |= BIT(5);
425 hub->conf_data2 &= ~BIT(4);
426 }
Richard Leitner3ec72a22017-02-10 09:19:27 +0100427 }
428
429 if (of_get_property(np, "compound-device", NULL))
430 hub->conf_data2 |= BIT(3);
431
432 hub->conf_data3 = USB251XB_DEF_CONFIG_DATA_3;
433 if (of_get_property(np, "port-mapping-mode", NULL))
434 hub->conf_data3 |= BIT(3);
435
436 if (of_get_property(np, "string-support", NULL))
437 hub->conf_data3 |= BIT(0);
438
439 hub->non_rem_dev = USB251XB_DEF_NON_REMOVABLE_DEVICES;
440 cproperty_u32 = of_get_property(np, "non-removable-ports", &len);
441 if (cproperty_u32 && (len / sizeof(u32)) > 0) {
442 for (i = 0; i < len / sizeof(u32); i++) {
443 u32 port = be32_to_cpu(cproperty_u32[i]);
444
Serge Semin38481122017-10-22 23:38:05 +0300445 if ((port >= 1) && (port <= data->port_cnt))
Richard Leitner3ec72a22017-02-10 09:19:27 +0100446 hub->non_rem_dev |= BIT(port);
Serge Semin38481122017-10-22 23:38:05 +0300447 else
448 dev_warn(dev, "NRD port %u doesn't exist\n",
449 port);
Richard Leitner3ec72a22017-02-10 09:19:27 +0100450 }
451 }
452
453 hub->port_disable_sp = USB251XB_DEF_PORT_DISABLE_SELF;
454 cproperty_u32 = of_get_property(np, "sp-disabled-ports", &len);
455 if (cproperty_u32 && (len / sizeof(u32)) > 0) {
456 for (i = 0; i < len / sizeof(u32); i++) {
457 u32 port = be32_to_cpu(cproperty_u32[i]);
458
Serge Semin38481122017-10-22 23:38:05 +0300459 if ((port >= 1) && (port <= data->port_cnt))
Richard Leitner3ec72a22017-02-10 09:19:27 +0100460 hub->port_disable_sp |= BIT(port);
Serge Semin38481122017-10-22 23:38:05 +0300461 else
462 dev_warn(dev, "PDS port %u doesn't exist\n",
463 port);
Richard Leitner3ec72a22017-02-10 09:19:27 +0100464 }
465 }
466
467 hub->port_disable_bp = USB251XB_DEF_PORT_DISABLE_BUS;
468 cproperty_u32 = of_get_property(np, "bp-disabled-ports", &len);
469 if (cproperty_u32 && (len / sizeof(u32)) > 0) {
470 for (i = 0; i < len / sizeof(u32); i++) {
471 u32 port = be32_to_cpu(cproperty_u32[i]);
472
Serge Semin38481122017-10-22 23:38:05 +0300473 if ((port >= 1) && (port <= data->port_cnt))
Richard Leitner3ec72a22017-02-10 09:19:27 +0100474 hub->port_disable_bp |= BIT(port);
Serge Semin38481122017-10-22 23:38:05 +0300475 else
476 dev_warn(dev, "PDB port %u doesn't exist\n",
477 port);
Richard Leitner3ec72a22017-02-10 09:19:27 +0100478 }
479 }
480
Richard Leitner3ec72a22017-02-10 09:19:27 +0100481 hub->power_on_time = USB251XB_DEF_POWER_ON_TIME;
Richard Leitner7f7d8ba2017-03-06 09:24:21 +0100482 if (!of_property_read_u32(np, "power-on-time-ms", property_u32))
483 hub->power_on_time = min_t(u8, *property_u32 / 2, 255);
Richard Leitner3ec72a22017-02-10 09:19:27 +0100484
485 if (of_property_read_u16_array(np, "language-id", &hub->lang_id, 1))
486 hub->lang_id = USB251XB_DEF_LANGUAGE_ID;
487
488 cproperty_char = of_get_property(np, "manufacturer", NULL);
489 strlcpy(str, cproperty_char ? : USB251XB_DEF_MANUFACTURER_STRING,
490 sizeof(str));
491 hub->manufacturer_len = strlen(str) & 0xFF;
492 memset(hub->manufacturer, 0, USB251XB_STRING_BUFSIZE);
493 len = min_t(size_t, USB251XB_STRING_BUFSIZE / 2, strlen(str));
494 len = utf8s_to_utf16s(str, len, UTF16_LITTLE_ENDIAN,
495 (wchar_t *)hub->manufacturer,
496 USB251XB_STRING_BUFSIZE);
497
498 cproperty_char = of_get_property(np, "product", NULL);
499 strlcpy(str, cproperty_char ? : data->product_str, sizeof(str));
500 hub->product_len = strlen(str) & 0xFF;
501 memset(hub->product, 0, USB251XB_STRING_BUFSIZE);
502 len = min_t(size_t, USB251XB_STRING_BUFSIZE / 2, strlen(str));
503 len = utf8s_to_utf16s(str, len, UTF16_LITTLE_ENDIAN,
504 (wchar_t *)hub->product,
505 USB251XB_STRING_BUFSIZE);
506
507 cproperty_char = of_get_property(np, "serial", NULL);
508 strlcpy(str, cproperty_char ? : USB251XB_DEF_SERIAL_STRING,
509 sizeof(str));
510 hub->serial_len = strlen(str) & 0xFF;
511 memset(hub->serial, 0, USB251XB_STRING_BUFSIZE);
512 len = min_t(size_t, USB251XB_STRING_BUFSIZE / 2, strlen(str));
513 len = utf8s_to_utf16s(str, len, UTF16_LITTLE_ENDIAN,
514 (wchar_t *)hub->serial,
515 USB251XB_STRING_BUFSIZE);
516
517 /* The following parameters are currently not exposed to devicetree, but
518 * may be as soon as needed.
519 */
Richard Leitnercfa47af2017-03-06 09:24:20 +0100520 hub->max_power_sp = USB251XB_DEF_MAX_POWER_SELF;
521 hub->max_power_bp = USB251XB_DEF_MAX_POWER_BUS;
522 hub->max_current_sp = USB251XB_DEF_MAX_CURRENT_SELF;
523 hub->max_current_bp = USB251XB_DEF_MAX_CURRENT_BUS;
Richard Leitner3ec72a22017-02-10 09:19:27 +0100524 hub->bat_charge_en = USB251XB_DEF_BATTERY_CHARGING_ENABLE;
525 hub->boost_up = USB251XB_DEF_BOOST_UP;
Serge Seminccdddc02017-10-22 23:38:07 +0300526 hub->boost_57 = USB251XB_DEF_BOOST_57;
527 hub->boost_14 = USB251XB_DEF_BOOST_14;
Richard Leitner3ec72a22017-02-10 09:19:27 +0100528 hub->port_swap = USB251XB_DEF_PORT_SWAP;
529 hub->port_map12 = USB251XB_DEF_PORT_MAP_12;
530 hub->port_map34 = USB251XB_DEF_PORT_MAP_34;
Serge Semindb234b92017-10-22 23:38:06 +0300531 hub->port_map56 = USB251XB_DEF_PORT_MAP_56;
532 hub->port_map7 = USB251XB_DEF_PORT_MAP_7;
Richard Leitner3ec72a22017-02-10 09:19:27 +0100533
534 return 0;
535}
536
537static const struct of_device_id usb251xb_of_match[] = {
538 {
539 .compatible = "microchip,usb2512b",
540 .data = &usb2512b_data,
541 }, {
542 .compatible = "microchip,usb2512bi",
543 .data = &usb2512bi_data,
544 }, {
545 .compatible = "microchip,usb2513b",
546 .data = &usb2513b_data,
547 }, {
548 .compatible = "microchip,usb2513bi",
549 .data = &usb2513bi_data,
550 }, {
551 .compatible = "microchip,usb2514b",
552 .data = &usb2514b_data,
553 }, {
554 .compatible = "microchip,usb2514bi",
555 .data = &usb2514bi_data,
556 }, {
Serge Semin7fcf5582017-10-22 23:38:04 +0300557 .compatible = "microchip,usb2517",
558 .data = &usb2517_data,
559 }, {
560 .compatible = "microchip,usb2517i",
561 .data = &usb2517i_data,
562 }, {
Richard Leitner3ec72a22017-02-10 09:19:27 +0100563 /* sentinel */
564 }
565};
566MODULE_DEVICE_TABLE(of, usb251xb_of_match);
567#else /* CONFIG_OF */
568static int usb251xb_get_ofdata(struct usb251xb *hub,
569 struct usb251xb_data *data)
570{
571 return 0;
572}
573#endif /* CONFIG_OF */
574
575static int usb251xb_probe(struct usb251xb *hub)
576{
577 struct device *dev = hub->dev;
578 struct device_node *np = dev->of_node;
579 const struct of_device_id *of_id = of_match_device(usb251xb_of_match,
580 dev);
581 int err;
582
583 if (np) {
584 err = usb251xb_get_ofdata(hub,
585 (struct usb251xb_data *)of_id->data);
586 if (err) {
587 dev_err(dev, "failed to get ofdata: %d\n", err);
588 return err;
589 }
590 }
591
592 err = usb251xb_connect(hub);
593 if (err) {
594 dev_err(dev, "Failed to connect hub (%d)\n", err);
595 return err;
596 }
597
598 dev_info(dev, "Hub probed successfully\n");
599
600 return 0;
601}
602
603static int usb251xb_i2c_probe(struct i2c_client *i2c,
604 const struct i2c_device_id *id)
605{
606 struct usb251xb *hub;
607
608 hub = devm_kzalloc(&i2c->dev, sizeof(struct usb251xb), GFP_KERNEL);
609 if (!hub)
610 return -ENOMEM;
611
612 i2c_set_clientdata(i2c, hub);
613 hub->dev = &i2c->dev;
614 hub->i2c = i2c;
615
616 return usb251xb_probe(hub);
617}
618
619static const struct i2c_device_id usb251xb_id[] = {
620 { "usb2512b", 0 },
621 { "usb2512bi", 0 },
622 { "usb2513b", 0 },
623 { "usb2513bi", 0 },
624 { "usb2514b", 0 },
625 { "usb2514bi", 0 },
Serge Semin7fcf5582017-10-22 23:38:04 +0300626 { "usb2517", 0 },
627 { "usb2517i", 0 },
Richard Leitner3ec72a22017-02-10 09:19:27 +0100628 { /* sentinel */ }
629};
630MODULE_DEVICE_TABLE(i2c, usb251xb_id);
631
632static struct i2c_driver usb251xb_i2c_driver = {
633 .driver = {
634 .name = DRIVER_NAME,
635 .of_match_table = of_match_ptr(usb251xb_of_match),
636 },
637 .probe = usb251xb_i2c_probe,
638 .id_table = usb251xb_id,
639};
640
641module_i2c_driver(usb251xb_i2c_driver);
642
643MODULE_AUTHOR("Richard Leitner <richard.leitner@skidata.com>");
Serge Semin7fcf5582017-10-22 23:38:04 +0300644MODULE_DESCRIPTION("USB251x/xBi USB 2.0 Hub Controller Driver");
Richard Leitner3ec72a22017-02-10 09:19:27 +0100645MODULE_LICENSE("GPL");