blob: e49158bb3a024b651cbb38a1491cf5fbeef6c670 [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
97#define USB251XB_ADDR_BOOST_X 0xF8
98#define USB251XB_DEF_BOOST_X 0x00
99
100#define USB251XB_ADDR_PORT_SWAP 0xFA
101#define USB251XB_DEF_PORT_SWAP 0x00
102
103#define USB251XB_ADDR_PORT_MAP_12 0xFB
104#define USB251XB_DEF_PORT_MAP_12 0x00
105#define USB251XB_ADDR_PORT_MAP_34 0xFC
106#define USB251XB_DEF_PORT_MAP_34 0x00 /* USB2513B/i & USB2514B/i only */
107
108#define USB251XB_ADDR_STATUS_COMMAND 0xFF
109#define USB251XB_STATUS_COMMAND_SMBUS_DOWN 0x04
110#define USB251XB_STATUS_COMMAND_RESET 0x02
111#define USB251XB_STATUS_COMMAND_ATTACH 0x01
112
113#define USB251XB_I2C_REG_SZ 0x100
114#define USB251XB_I2C_WRITE_SZ 0x10
115
116#define DRIVER_NAME "usb251xb"
117#define DRIVER_DESC "Microchip USB 2.0 Hi-Speed Hub Controller"
Richard Leitner3ec72a22017-02-10 09:19:27 +0100118
119struct usb251xb {
120 struct device *dev;
121 struct i2c_client *i2c;
122 u8 skip_config;
123 int gpio_reset;
124 u16 vendor_id;
125 u16 product_id;
126 u16 device_id;
127 u8 conf_data1;
128 u8 conf_data2;
129 u8 conf_data3;
130 u8 non_rem_dev;
131 u8 port_disable_sp;
132 u8 port_disable_bp;
133 u8 max_power_sp;
134 u8 max_power_bp;
135 u8 max_current_sp;
136 u8 max_current_bp;
137 u8 power_on_time;
138 u16 lang_id;
139 u8 manufacturer_len;
140 u8 product_len;
141 u8 serial_len;
142 char manufacturer[USB251XB_STRING_BUFSIZE];
143 char product[USB251XB_STRING_BUFSIZE];
144 char serial[USB251XB_STRING_BUFSIZE];
145 u8 bat_charge_en;
146 u8 boost_up;
147 u8 boost_x;
148 u8 port_swap;
149 u8 port_map12;
150 u8 port_map34;
151 u8 status;
152};
153
154struct usb251xb_data {
155 u16 product_id;
Serge Semin38481122017-10-22 23:38:05 +0300156 u8 port_cnt;
Richard Leitner3ec72a22017-02-10 09:19:27 +0100157 char product_str[USB251XB_STRING_BUFSIZE / 2]; /* ASCII string */
158};
159
160static const struct usb251xb_data usb2512b_data = {
161 .product_id = 0x2512,
Serge Semin38481122017-10-22 23:38:05 +0300162 .port_cnt = 2,
Richard Leitner3ec72a22017-02-10 09:19:27 +0100163 .product_str = "USB2512B",
164};
165
166static const struct usb251xb_data usb2512bi_data = {
167 .product_id = 0x2512,
Serge Semin38481122017-10-22 23:38:05 +0300168 .port_cnt = 2,
Richard Leitner3ec72a22017-02-10 09:19:27 +0100169 .product_str = "USB2512Bi",
170};
171
172static const struct usb251xb_data usb2513b_data = {
173 .product_id = 0x2513,
Serge Semin38481122017-10-22 23:38:05 +0300174 .port_cnt = 3,
Richard Leitner3ec72a22017-02-10 09:19:27 +0100175 .product_str = "USB2513B",
176};
177
178static const struct usb251xb_data usb2513bi_data = {
179 .product_id = 0x2513,
Serge Semin38481122017-10-22 23:38:05 +0300180 .port_cnt = 3,
Richard Leitner3ec72a22017-02-10 09:19:27 +0100181 .product_str = "USB2513Bi",
182};
183
184static const struct usb251xb_data usb2514b_data = {
185 .product_id = 0x2514,
Serge Semin38481122017-10-22 23:38:05 +0300186 .port_cnt = 4,
Richard Leitner3ec72a22017-02-10 09:19:27 +0100187 .product_str = "USB2514B",
188};
189
190static const struct usb251xb_data usb2514bi_data = {
191 .product_id = 0x2514,
Serge Semin38481122017-10-22 23:38:05 +0300192 .port_cnt = 4,
Richard Leitner3ec72a22017-02-10 09:19:27 +0100193 .product_str = "USB2514Bi",
194};
195
Serge Semin7fcf5582017-10-22 23:38:04 +0300196static const struct usb251xb_data usb2517_data = {
197 .product_id = 0x2517,
Serge Semin38481122017-10-22 23:38:05 +0300198 .port_cnt = 7,
Serge Semin7fcf5582017-10-22 23:38:04 +0300199 .product_str = "USB2517",
200};
201
202static const struct usb251xb_data usb2517i_data = {
203 .product_id = 0x2517,
Serge Semin38481122017-10-22 23:38:05 +0300204 .port_cnt = 7,
Serge Semin7fcf5582017-10-22 23:38:04 +0300205 .product_str = "USB2517i",
206};
207
Richard Leitner3ec72a22017-02-10 09:19:27 +0100208static void usb251xb_reset(struct usb251xb *hub, int state)
209{
210 if (!gpio_is_valid(hub->gpio_reset))
211 return;
212
213 gpio_set_value_cansleep(hub->gpio_reset, state);
214
215 /* wait for hub recovery/stabilization */
216 if (state)
217 usleep_range(500, 750); /* >=500us at power on */
218 else
219 usleep_range(1, 10); /* >=1us at power down */
220}
221
222static int usb251xb_connect(struct usb251xb *hub)
223{
224 struct device *dev = hub->dev;
225 int err, i;
226 char i2c_wb[USB251XB_I2C_REG_SZ];
227
228 memset(i2c_wb, 0, USB251XB_I2C_REG_SZ);
229
230 if (hub->skip_config) {
231 dev_info(dev, "Skip hub configuration, only attach.\n");
232 i2c_wb[0] = 0x01;
233 i2c_wb[1] = USB251XB_STATUS_COMMAND_ATTACH;
234
235 usb251xb_reset(hub, 1);
236
237 err = i2c_smbus_write_i2c_block_data(hub->i2c,
238 USB251XB_ADDR_STATUS_COMMAND, 2, i2c_wb);
239 if (err) {
240 dev_err(dev, "attaching hub failed: %d\n", err);
241 return err;
242 }
243 return 0;
244 }
245
246 i2c_wb[USB251XB_ADDR_VENDOR_ID_MSB] = (hub->vendor_id >> 8) & 0xFF;
247 i2c_wb[USB251XB_ADDR_VENDOR_ID_LSB] = hub->vendor_id & 0xFF;
248 i2c_wb[USB251XB_ADDR_PRODUCT_ID_MSB] = (hub->product_id >> 8) & 0xFF;
249 i2c_wb[USB251XB_ADDR_PRODUCT_ID_LSB] = hub->product_id & 0xFF;
250 i2c_wb[USB251XB_ADDR_DEVICE_ID_MSB] = (hub->device_id >> 8) & 0xFF;
251 i2c_wb[USB251XB_ADDR_DEVICE_ID_LSB] = hub->device_id & 0xFF;
252 i2c_wb[USB251XB_ADDR_CONFIG_DATA_1] = hub->conf_data1;
253 i2c_wb[USB251XB_ADDR_CONFIG_DATA_2] = hub->conf_data2;
254 i2c_wb[USB251XB_ADDR_CONFIG_DATA_3] = hub->conf_data3;
255 i2c_wb[USB251XB_ADDR_NON_REMOVABLE_DEVICES] = hub->non_rem_dev;
256 i2c_wb[USB251XB_ADDR_PORT_DISABLE_SELF] = hub->port_disable_sp;
257 i2c_wb[USB251XB_ADDR_PORT_DISABLE_BUS] = hub->port_disable_bp;
258 i2c_wb[USB251XB_ADDR_MAX_POWER_SELF] = hub->max_power_sp;
259 i2c_wb[USB251XB_ADDR_MAX_POWER_BUS] = hub->max_power_bp;
260 i2c_wb[USB251XB_ADDR_MAX_CURRENT_SELF] = hub->max_current_sp;
261 i2c_wb[USB251XB_ADDR_MAX_CURRENT_BUS] = hub->max_current_bp;
262 i2c_wb[USB251XB_ADDR_POWER_ON_TIME] = hub->power_on_time;
263 i2c_wb[USB251XB_ADDR_LANGUAGE_ID_HIGH] = (hub->lang_id >> 8) & 0xFF;
264 i2c_wb[USB251XB_ADDR_LANGUAGE_ID_LOW] = hub->lang_id & 0xFF;
265 i2c_wb[USB251XB_ADDR_MANUFACTURER_STRING_LEN] = hub->manufacturer_len;
266 i2c_wb[USB251XB_ADDR_PRODUCT_STRING_LEN] = hub->product_len;
267 i2c_wb[USB251XB_ADDR_SERIAL_STRING_LEN] = hub->serial_len;
268 memcpy(&i2c_wb[USB251XB_ADDR_MANUFACTURER_STRING], hub->manufacturer,
269 USB251XB_STRING_BUFSIZE);
270 memcpy(&i2c_wb[USB251XB_ADDR_SERIAL_STRING], hub->serial,
271 USB251XB_STRING_BUFSIZE);
272 memcpy(&i2c_wb[USB251XB_ADDR_PRODUCT_STRING], hub->product,
273 USB251XB_STRING_BUFSIZE);
274 i2c_wb[USB251XB_ADDR_BATTERY_CHARGING_ENABLE] = hub->bat_charge_en;
275 i2c_wb[USB251XB_ADDR_BOOST_UP] = hub->boost_up;
276 i2c_wb[USB251XB_ADDR_BOOST_X] = hub->boost_x;
277 i2c_wb[USB251XB_ADDR_PORT_SWAP] = hub->port_swap;
278 i2c_wb[USB251XB_ADDR_PORT_MAP_12] = hub->port_map12;
279 i2c_wb[USB251XB_ADDR_PORT_MAP_34] = hub->port_map34;
280 i2c_wb[USB251XB_ADDR_STATUS_COMMAND] = USB251XB_STATUS_COMMAND_ATTACH;
281
282 usb251xb_reset(hub, 1);
283
284 /* write registers */
285 for (i = 0; i < (USB251XB_I2C_REG_SZ / USB251XB_I2C_WRITE_SZ); i++) {
286 int offset = i * USB251XB_I2C_WRITE_SZ;
287 char wbuf[USB251XB_I2C_WRITE_SZ + 1];
288
289 /* The first data byte transferred tells the hub how many data
290 * bytes will follow (byte count).
291 */
292 wbuf[0] = USB251XB_I2C_WRITE_SZ;
293 memcpy(&wbuf[1], &i2c_wb[offset], USB251XB_I2C_WRITE_SZ);
294
295 dev_dbg(dev, "writing %d byte block %d to 0x%02X\n",
296 USB251XB_I2C_WRITE_SZ, i, offset);
297
298 err = i2c_smbus_write_i2c_block_data(hub->i2c, offset,
299 USB251XB_I2C_WRITE_SZ + 1,
300 wbuf);
301 if (err)
302 goto out_err;
303 }
304
305 dev_info(dev, "Hub configuration was successful.\n");
306 return 0;
307
308out_err:
309 dev_err(dev, "configuring block %d failed: %d\n", i, err);
310 return err;
311}
312
313#ifdef CONFIG_OF
314static int usb251xb_get_ofdata(struct usb251xb *hub,
315 struct usb251xb_data *data)
316{
317 struct device *dev = hub->dev;
318 struct device_node *np = dev->of_node;
319 int len, err, i;
320 u32 *property_u32 = NULL;
321 const u32 *cproperty_u32;
322 const char *cproperty_char;
323 char str[USB251XB_STRING_BUFSIZE / 2];
324
325 if (!np) {
326 dev_err(dev, "failed to get ofdata\n");
327 return -ENODEV;
328 }
329
330 if (of_get_property(np, "skip-config", NULL))
331 hub->skip_config = 1;
332 else
333 hub->skip_config = 0;
334
335 hub->gpio_reset = of_get_named_gpio(np, "reset-gpios", 0);
336 if (hub->gpio_reset == -EPROBE_DEFER)
337 return -EPROBE_DEFER;
338 if (gpio_is_valid(hub->gpio_reset)) {
339 err = devm_gpio_request_one(dev, hub->gpio_reset,
340 GPIOF_OUT_INIT_LOW,
341 "usb251xb reset");
342 if (err) {
343 dev_err(dev,
344 "unable to request GPIO %d as reset pin (%d)\n",
345 hub->gpio_reset, err);
346 return err;
347 }
348 }
349
350 if (of_property_read_u16_array(np, "vendor-id", &hub->vendor_id, 1))
351 hub->vendor_id = USB251XB_DEF_VENDOR_ID;
352
353 if (of_property_read_u16_array(np, "product-id",
354 &hub->product_id, 1))
355 hub->product_id = data->product_id;
356
357 if (of_property_read_u16_array(np, "device-id", &hub->device_id, 1))
358 hub->device_id = USB251XB_DEF_DEVICE_ID;
359
360 hub->conf_data1 = USB251XB_DEF_CONFIG_DATA_1;
361 if (of_get_property(np, "self-powered", NULL)) {
362 hub->conf_data1 |= BIT(7);
363
364 /* Configure Over-Current sens when self-powered */
365 hub->conf_data1 &= ~BIT(2);
366 if (of_get_property(np, "ganged-sensing", NULL))
367 hub->conf_data1 &= ~BIT(1);
368 else if (of_get_property(np, "individual-sensing", NULL))
369 hub->conf_data1 |= BIT(1);
370 } else if (of_get_property(np, "bus-powered", NULL)) {
371 hub->conf_data1 &= ~BIT(7);
372
373 /* Disable Over-Current sense when bus-powered */
374 hub->conf_data1 |= BIT(2);
375 }
376
377 if (of_get_property(np, "disable-hi-speed", NULL))
378 hub->conf_data1 |= BIT(5);
379
380 if (of_get_property(np, "multi-tt", NULL))
381 hub->conf_data1 |= BIT(4);
382 else if (of_get_property(np, "single-tt", NULL))
383 hub->conf_data1 &= ~BIT(4);
384
385 if (of_get_property(np, "disable-eop", NULL))
386 hub->conf_data1 |= BIT(3);
387
388 if (of_get_property(np, "individual-port-switching", NULL))
389 hub->conf_data1 |= BIT(0);
390 else if (of_get_property(np, "ganged-port-switching", NULL))
391 hub->conf_data1 &= ~BIT(0);
392
393 hub->conf_data2 = USB251XB_DEF_CONFIG_DATA_2;
394 if (of_get_property(np, "dynamic-power-switching", NULL))
395 hub->conf_data2 |= BIT(7);
396
Richard Leitner7f7d8ba2017-03-06 09:24:21 +0100397 if (!of_property_read_u32(np, "oc-delay-us", property_u32)) {
398 if (*property_u32 == 100) {
399 /* 100 us*/
400 hub->conf_data2 &= ~BIT(5);
401 hub->conf_data2 &= ~BIT(4);
402 } else if (*property_u32 == 4000) {
403 /* 4 ms */
404 hub->conf_data2 &= ~BIT(5);
405 hub->conf_data2 |= BIT(4);
406 } else if (*property_u32 == 16000) {
407 /* 16 ms */
408 hub->conf_data2 |= BIT(5);
409 hub->conf_data2 |= BIT(4);
410 } else {
411 /* 8 ms (DEFAULT) */
412 hub->conf_data2 |= BIT(5);
413 hub->conf_data2 &= ~BIT(4);
414 }
Richard Leitner3ec72a22017-02-10 09:19:27 +0100415 }
416
417 if (of_get_property(np, "compound-device", NULL))
418 hub->conf_data2 |= BIT(3);
419
420 hub->conf_data3 = USB251XB_DEF_CONFIG_DATA_3;
421 if (of_get_property(np, "port-mapping-mode", NULL))
422 hub->conf_data3 |= BIT(3);
423
424 if (of_get_property(np, "string-support", NULL))
425 hub->conf_data3 |= BIT(0);
426
427 hub->non_rem_dev = USB251XB_DEF_NON_REMOVABLE_DEVICES;
428 cproperty_u32 = of_get_property(np, "non-removable-ports", &len);
429 if (cproperty_u32 && (len / sizeof(u32)) > 0) {
430 for (i = 0; i < len / sizeof(u32); i++) {
431 u32 port = be32_to_cpu(cproperty_u32[i]);
432
Serge Semin38481122017-10-22 23:38:05 +0300433 if ((port >= 1) && (port <= data->port_cnt))
Richard Leitner3ec72a22017-02-10 09:19:27 +0100434 hub->non_rem_dev |= BIT(port);
Serge Semin38481122017-10-22 23:38:05 +0300435 else
436 dev_warn(dev, "NRD port %u doesn't exist\n",
437 port);
Richard Leitner3ec72a22017-02-10 09:19:27 +0100438 }
439 }
440
441 hub->port_disable_sp = USB251XB_DEF_PORT_DISABLE_SELF;
442 cproperty_u32 = of_get_property(np, "sp-disabled-ports", &len);
443 if (cproperty_u32 && (len / sizeof(u32)) > 0) {
444 for (i = 0; i < len / sizeof(u32); i++) {
445 u32 port = be32_to_cpu(cproperty_u32[i]);
446
Serge Semin38481122017-10-22 23:38:05 +0300447 if ((port >= 1) && (port <= data->port_cnt))
Richard Leitner3ec72a22017-02-10 09:19:27 +0100448 hub->port_disable_sp |= BIT(port);
Serge Semin38481122017-10-22 23:38:05 +0300449 else
450 dev_warn(dev, "PDS port %u doesn't exist\n",
451 port);
Richard Leitner3ec72a22017-02-10 09:19:27 +0100452 }
453 }
454
455 hub->port_disable_bp = USB251XB_DEF_PORT_DISABLE_BUS;
456 cproperty_u32 = of_get_property(np, "bp-disabled-ports", &len);
457 if (cproperty_u32 && (len / sizeof(u32)) > 0) {
458 for (i = 0; i < len / sizeof(u32); i++) {
459 u32 port = be32_to_cpu(cproperty_u32[i]);
460
Serge Semin38481122017-10-22 23:38:05 +0300461 if ((port >= 1) && (port <= data->port_cnt))
Richard Leitner3ec72a22017-02-10 09:19:27 +0100462 hub->port_disable_bp |= BIT(port);
Serge Semin38481122017-10-22 23:38:05 +0300463 else
464 dev_warn(dev, "PDB port %u doesn't exist\n",
465 port);
Richard Leitner3ec72a22017-02-10 09:19:27 +0100466 }
467 }
468
Richard Leitner3ec72a22017-02-10 09:19:27 +0100469 hub->power_on_time = USB251XB_DEF_POWER_ON_TIME;
Richard Leitner7f7d8ba2017-03-06 09:24:21 +0100470 if (!of_property_read_u32(np, "power-on-time-ms", property_u32))
471 hub->power_on_time = min_t(u8, *property_u32 / 2, 255);
Richard Leitner3ec72a22017-02-10 09:19:27 +0100472
473 if (of_property_read_u16_array(np, "language-id", &hub->lang_id, 1))
474 hub->lang_id = USB251XB_DEF_LANGUAGE_ID;
475
476 cproperty_char = of_get_property(np, "manufacturer", NULL);
477 strlcpy(str, cproperty_char ? : USB251XB_DEF_MANUFACTURER_STRING,
478 sizeof(str));
479 hub->manufacturer_len = strlen(str) & 0xFF;
480 memset(hub->manufacturer, 0, USB251XB_STRING_BUFSIZE);
481 len = min_t(size_t, USB251XB_STRING_BUFSIZE / 2, strlen(str));
482 len = utf8s_to_utf16s(str, len, UTF16_LITTLE_ENDIAN,
483 (wchar_t *)hub->manufacturer,
484 USB251XB_STRING_BUFSIZE);
485
486 cproperty_char = of_get_property(np, "product", NULL);
487 strlcpy(str, cproperty_char ? : data->product_str, sizeof(str));
488 hub->product_len = strlen(str) & 0xFF;
489 memset(hub->product, 0, USB251XB_STRING_BUFSIZE);
490 len = min_t(size_t, USB251XB_STRING_BUFSIZE / 2, strlen(str));
491 len = utf8s_to_utf16s(str, len, UTF16_LITTLE_ENDIAN,
492 (wchar_t *)hub->product,
493 USB251XB_STRING_BUFSIZE);
494
495 cproperty_char = of_get_property(np, "serial", NULL);
496 strlcpy(str, cproperty_char ? : USB251XB_DEF_SERIAL_STRING,
497 sizeof(str));
498 hub->serial_len = strlen(str) & 0xFF;
499 memset(hub->serial, 0, USB251XB_STRING_BUFSIZE);
500 len = min_t(size_t, USB251XB_STRING_BUFSIZE / 2, strlen(str));
501 len = utf8s_to_utf16s(str, len, UTF16_LITTLE_ENDIAN,
502 (wchar_t *)hub->serial,
503 USB251XB_STRING_BUFSIZE);
504
505 /* The following parameters are currently not exposed to devicetree, but
506 * may be as soon as needed.
507 */
Richard Leitnercfa47af2017-03-06 09:24:20 +0100508 hub->max_power_sp = USB251XB_DEF_MAX_POWER_SELF;
509 hub->max_power_bp = USB251XB_DEF_MAX_POWER_BUS;
510 hub->max_current_sp = USB251XB_DEF_MAX_CURRENT_SELF;
511 hub->max_current_bp = USB251XB_DEF_MAX_CURRENT_BUS;
Richard Leitner3ec72a22017-02-10 09:19:27 +0100512 hub->bat_charge_en = USB251XB_DEF_BATTERY_CHARGING_ENABLE;
513 hub->boost_up = USB251XB_DEF_BOOST_UP;
514 hub->boost_x = USB251XB_DEF_BOOST_X;
515 hub->port_swap = USB251XB_DEF_PORT_SWAP;
516 hub->port_map12 = USB251XB_DEF_PORT_MAP_12;
517 hub->port_map34 = USB251XB_DEF_PORT_MAP_34;
518
519 return 0;
520}
521
522static const struct of_device_id usb251xb_of_match[] = {
523 {
524 .compatible = "microchip,usb2512b",
525 .data = &usb2512b_data,
526 }, {
527 .compatible = "microchip,usb2512bi",
528 .data = &usb2512bi_data,
529 }, {
530 .compatible = "microchip,usb2513b",
531 .data = &usb2513b_data,
532 }, {
533 .compatible = "microchip,usb2513bi",
534 .data = &usb2513bi_data,
535 }, {
536 .compatible = "microchip,usb2514b",
537 .data = &usb2514b_data,
538 }, {
539 .compatible = "microchip,usb2514bi",
540 .data = &usb2514bi_data,
541 }, {
Serge Semin7fcf5582017-10-22 23:38:04 +0300542 .compatible = "microchip,usb2517",
543 .data = &usb2517_data,
544 }, {
545 .compatible = "microchip,usb2517i",
546 .data = &usb2517i_data,
547 }, {
Richard Leitner3ec72a22017-02-10 09:19:27 +0100548 /* sentinel */
549 }
550};
551MODULE_DEVICE_TABLE(of, usb251xb_of_match);
552#else /* CONFIG_OF */
553static int usb251xb_get_ofdata(struct usb251xb *hub,
554 struct usb251xb_data *data)
555{
556 return 0;
557}
558#endif /* CONFIG_OF */
559
560static int usb251xb_probe(struct usb251xb *hub)
561{
562 struct device *dev = hub->dev;
563 struct device_node *np = dev->of_node;
564 const struct of_device_id *of_id = of_match_device(usb251xb_of_match,
565 dev);
566 int err;
567
568 if (np) {
569 err = usb251xb_get_ofdata(hub,
570 (struct usb251xb_data *)of_id->data);
571 if (err) {
572 dev_err(dev, "failed to get ofdata: %d\n", err);
573 return err;
574 }
575 }
576
577 err = usb251xb_connect(hub);
578 if (err) {
579 dev_err(dev, "Failed to connect hub (%d)\n", err);
580 return err;
581 }
582
583 dev_info(dev, "Hub probed successfully\n");
584
585 return 0;
586}
587
588static int usb251xb_i2c_probe(struct i2c_client *i2c,
589 const struct i2c_device_id *id)
590{
591 struct usb251xb *hub;
592
593 hub = devm_kzalloc(&i2c->dev, sizeof(struct usb251xb), GFP_KERNEL);
594 if (!hub)
595 return -ENOMEM;
596
597 i2c_set_clientdata(i2c, hub);
598 hub->dev = &i2c->dev;
599 hub->i2c = i2c;
600
601 return usb251xb_probe(hub);
602}
603
604static const struct i2c_device_id usb251xb_id[] = {
605 { "usb2512b", 0 },
606 { "usb2512bi", 0 },
607 { "usb2513b", 0 },
608 { "usb2513bi", 0 },
609 { "usb2514b", 0 },
610 { "usb2514bi", 0 },
Serge Semin7fcf5582017-10-22 23:38:04 +0300611 { "usb2517", 0 },
612 { "usb2517i", 0 },
Richard Leitner3ec72a22017-02-10 09:19:27 +0100613 { /* sentinel */ }
614};
615MODULE_DEVICE_TABLE(i2c, usb251xb_id);
616
617static struct i2c_driver usb251xb_i2c_driver = {
618 .driver = {
619 .name = DRIVER_NAME,
620 .of_match_table = of_match_ptr(usb251xb_of_match),
621 },
622 .probe = usb251xb_i2c_probe,
623 .id_table = usb251xb_id,
624};
625
626module_i2c_driver(usb251xb_i2c_driver);
627
628MODULE_AUTHOR("Richard Leitner <richard.leitner@skidata.com>");
Serge Semin7fcf5582017-10-22 23:38:04 +0300629MODULE_DESCRIPTION("USB251x/xBi USB 2.0 Hub Controller Driver");
Richard Leitner3ec72a22017-02-10 09:19:27 +0100630MODULE_LICENSE("GPL");