usb251xb.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594
  1. /*
  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. #include <linux/delay.h>
  22. #include <linux/gpio.h>
  23. #include <linux/i2c.h>
  24. #include <linux/module.h>
  25. #include <linux/nls.h>
  26. #include <linux/of_device.h>
  27. #include <linux/of_gpio.h>
  28. #include <linux/slab.h>
  29. /* Internal Register Set Addresses & Default Values acc. to DS00001692C */
  30. #define USB251XB_ADDR_VENDOR_ID_LSB 0x00
  31. #define USB251XB_ADDR_VENDOR_ID_MSB 0x01
  32. #define USB251XB_DEF_VENDOR_ID 0x0424
  33. #define USB251XB_ADDR_PRODUCT_ID_LSB 0x02
  34. #define USB251XB_ADDR_PRODUCT_ID_MSB 0x03
  35. #define USB251XB_DEF_PRODUCT_ID_12 0x2512 /* USB2512B/12Bi */
  36. #define USB251XB_DEF_PRODUCT_ID_13 0x2513 /* USB2513B/13Bi */
  37. #define USB251XB_DEF_PRODUCT_ID_14 0x2514 /* USB2514B/14Bi */
  38. #define USB251XB_ADDR_DEVICE_ID_LSB 0x04
  39. #define USB251XB_ADDR_DEVICE_ID_MSB 0x05
  40. #define USB251XB_DEF_DEVICE_ID 0x0BB3
  41. #define USB251XB_ADDR_CONFIG_DATA_1 0x06
  42. #define USB251XB_DEF_CONFIG_DATA_1 0x9B
  43. #define USB251XB_ADDR_CONFIG_DATA_2 0x07
  44. #define USB251XB_DEF_CONFIG_DATA_2 0x20
  45. #define USB251XB_ADDR_CONFIG_DATA_3 0x08
  46. #define USB251XB_DEF_CONFIG_DATA_3 0x02
  47. #define USB251XB_ADDR_NON_REMOVABLE_DEVICES 0x09
  48. #define USB251XB_DEF_NON_REMOVABLE_DEVICES 0x00
  49. #define USB251XB_ADDR_PORT_DISABLE_SELF 0x0A
  50. #define USB251XB_DEF_PORT_DISABLE_SELF 0x00
  51. #define USB251XB_ADDR_PORT_DISABLE_BUS 0x0B
  52. #define USB251XB_DEF_PORT_DISABLE_BUS 0x00
  53. #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. #define USB251XB_ADDR_MAX_CURRENT_SELF 0x0E
  58. #define USB251XB_DEF_MAX_CURRENT_SELF 0x01
  59. #define USB251XB_ADDR_MAX_CURRENT_BUS 0x0F
  60. #define USB251XB_DEF_MAX_CURRENT_BUS 0x32
  61. #define USB251XB_ADDR_POWER_ON_TIME 0x10
  62. #define USB251XB_DEF_POWER_ON_TIME 0x32
  63. #define USB251XB_ADDR_LANGUAGE_ID_HIGH 0x11
  64. #define USB251XB_ADDR_LANGUAGE_ID_LOW 0x12
  65. #define USB251XB_DEF_LANGUAGE_ID 0x0000
  66. #define USB251XB_STRING_BUFSIZE 62
  67. #define USB251XB_ADDR_MANUFACTURER_STRING_LEN 0x13
  68. #define USB251XB_ADDR_MANUFACTURER_STRING 0x16
  69. #define USB251XB_DEF_MANUFACTURER_STRING "Microchip"
  70. #define USB251XB_ADDR_PRODUCT_STRING_LEN 0x14
  71. #define USB251XB_ADDR_PRODUCT_STRING 0x54
  72. #define USB251XB_DEF_PRODUCT_STRING "USB251xB/xBi"
  73. #define USB251XB_ADDR_SERIAL_STRING_LEN 0x15
  74. #define USB251XB_ADDR_SERIAL_STRING 0x92
  75. #define USB251XB_DEF_SERIAL_STRING ""
  76. #define USB251XB_ADDR_BATTERY_CHARGING_ENABLE 0xD0
  77. #define USB251XB_DEF_BATTERY_CHARGING_ENABLE 0x00
  78. #define USB251XB_ADDR_BOOST_UP 0xF6
  79. #define USB251XB_DEF_BOOST_UP 0x00
  80. #define USB251XB_ADDR_BOOST_X 0xF8
  81. #define USB251XB_DEF_BOOST_X 0x00
  82. #define USB251XB_ADDR_PORT_SWAP 0xFA
  83. #define USB251XB_DEF_PORT_SWAP 0x00
  84. #define USB251XB_ADDR_PORT_MAP_12 0xFB
  85. #define USB251XB_DEF_PORT_MAP_12 0x00
  86. #define USB251XB_ADDR_PORT_MAP_34 0xFC
  87. #define USB251XB_DEF_PORT_MAP_34 0x00 /* USB2513B/i & USB2514B/i only */
  88. #define USB251XB_ADDR_STATUS_COMMAND 0xFF
  89. #define USB251XB_STATUS_COMMAND_SMBUS_DOWN 0x04
  90. #define USB251XB_STATUS_COMMAND_RESET 0x02
  91. #define USB251XB_STATUS_COMMAND_ATTACH 0x01
  92. #define USB251XB_I2C_REG_SZ 0x100
  93. #define USB251XB_I2C_WRITE_SZ 0x10
  94. #define DRIVER_NAME "usb251xb"
  95. #define DRIVER_DESC "Microchip USB 2.0 Hi-Speed Hub Controller"
  96. struct usb251xb {
  97. struct device *dev;
  98. struct i2c_client *i2c;
  99. u8 skip_config;
  100. int gpio_reset;
  101. u16 vendor_id;
  102. u16 product_id;
  103. u16 device_id;
  104. u8 conf_data1;
  105. u8 conf_data2;
  106. u8 conf_data3;
  107. u8 non_rem_dev;
  108. u8 port_disable_sp;
  109. u8 port_disable_bp;
  110. u8 max_power_sp;
  111. u8 max_power_bp;
  112. u8 max_current_sp;
  113. u8 max_current_bp;
  114. u8 power_on_time;
  115. u16 lang_id;
  116. u8 manufacturer_len;
  117. u8 product_len;
  118. u8 serial_len;
  119. char manufacturer[USB251XB_STRING_BUFSIZE];
  120. char product[USB251XB_STRING_BUFSIZE];
  121. char serial[USB251XB_STRING_BUFSIZE];
  122. u8 bat_charge_en;
  123. u8 boost_up;
  124. u8 boost_x;
  125. u8 port_swap;
  126. u8 port_map12;
  127. u8 port_map34;
  128. u8 status;
  129. };
  130. struct usb251xb_data {
  131. u16 product_id;
  132. char product_str[USB251XB_STRING_BUFSIZE / 2]; /* ASCII string */
  133. };
  134. static const struct usb251xb_data usb2512b_data = {
  135. .product_id = 0x2512,
  136. .product_str = "USB2512B",
  137. };
  138. static const struct usb251xb_data usb2512bi_data = {
  139. .product_id = 0x2512,
  140. .product_str = "USB2512Bi",
  141. };
  142. static const struct usb251xb_data usb2513b_data = {
  143. .product_id = 0x2513,
  144. .product_str = "USB2513B",
  145. };
  146. static const struct usb251xb_data usb2513bi_data = {
  147. .product_id = 0x2513,
  148. .product_str = "USB2513Bi",
  149. };
  150. static const struct usb251xb_data usb2514b_data = {
  151. .product_id = 0x2514,
  152. .product_str = "USB2514B",
  153. };
  154. static const struct usb251xb_data usb2514bi_data = {
  155. .product_id = 0x2514,
  156. .product_str = "USB2514Bi",
  157. };
  158. static void usb251xb_reset(struct usb251xb *hub, int state)
  159. {
  160. if (!gpio_is_valid(hub->gpio_reset))
  161. return;
  162. gpio_set_value_cansleep(hub->gpio_reset, state);
  163. /* wait for hub recovery/stabilization */
  164. if (state)
  165. usleep_range(500, 750); /* >=500us at power on */
  166. else
  167. usleep_range(1, 10); /* >=1us at power down */
  168. }
  169. static int usb251xb_connect(struct usb251xb *hub)
  170. {
  171. struct device *dev = hub->dev;
  172. int err, i;
  173. char i2c_wb[USB251XB_I2C_REG_SZ];
  174. memset(i2c_wb, 0, USB251XB_I2C_REG_SZ);
  175. if (hub->skip_config) {
  176. dev_info(dev, "Skip hub configuration, only attach.\n");
  177. i2c_wb[0] = 0x01;
  178. i2c_wb[1] = USB251XB_STATUS_COMMAND_ATTACH;
  179. usb251xb_reset(hub, 1);
  180. err = i2c_smbus_write_i2c_block_data(hub->i2c,
  181. USB251XB_ADDR_STATUS_COMMAND, 2, i2c_wb);
  182. if (err) {
  183. dev_err(dev, "attaching hub failed: %d\n", err);
  184. return err;
  185. }
  186. return 0;
  187. }
  188. i2c_wb[USB251XB_ADDR_VENDOR_ID_MSB] = (hub->vendor_id >> 8) & 0xFF;
  189. i2c_wb[USB251XB_ADDR_VENDOR_ID_LSB] = hub->vendor_id & 0xFF;
  190. i2c_wb[USB251XB_ADDR_PRODUCT_ID_MSB] = (hub->product_id >> 8) & 0xFF;
  191. i2c_wb[USB251XB_ADDR_PRODUCT_ID_LSB] = hub->product_id & 0xFF;
  192. i2c_wb[USB251XB_ADDR_DEVICE_ID_MSB] = (hub->device_id >> 8) & 0xFF;
  193. i2c_wb[USB251XB_ADDR_DEVICE_ID_LSB] = hub->device_id & 0xFF;
  194. i2c_wb[USB251XB_ADDR_CONFIG_DATA_1] = hub->conf_data1;
  195. i2c_wb[USB251XB_ADDR_CONFIG_DATA_2] = hub->conf_data2;
  196. i2c_wb[USB251XB_ADDR_CONFIG_DATA_3] = hub->conf_data3;
  197. i2c_wb[USB251XB_ADDR_NON_REMOVABLE_DEVICES] = hub->non_rem_dev;
  198. i2c_wb[USB251XB_ADDR_PORT_DISABLE_SELF] = hub->port_disable_sp;
  199. i2c_wb[USB251XB_ADDR_PORT_DISABLE_BUS] = hub->port_disable_bp;
  200. i2c_wb[USB251XB_ADDR_MAX_POWER_SELF] = hub->max_power_sp;
  201. i2c_wb[USB251XB_ADDR_MAX_POWER_BUS] = hub->max_power_bp;
  202. i2c_wb[USB251XB_ADDR_MAX_CURRENT_SELF] = hub->max_current_sp;
  203. i2c_wb[USB251XB_ADDR_MAX_CURRENT_BUS] = hub->max_current_bp;
  204. i2c_wb[USB251XB_ADDR_POWER_ON_TIME] = hub->power_on_time;
  205. i2c_wb[USB251XB_ADDR_LANGUAGE_ID_HIGH] = (hub->lang_id >> 8) & 0xFF;
  206. i2c_wb[USB251XB_ADDR_LANGUAGE_ID_LOW] = hub->lang_id & 0xFF;
  207. i2c_wb[USB251XB_ADDR_MANUFACTURER_STRING_LEN] = hub->manufacturer_len;
  208. i2c_wb[USB251XB_ADDR_PRODUCT_STRING_LEN] = hub->product_len;
  209. i2c_wb[USB251XB_ADDR_SERIAL_STRING_LEN] = hub->serial_len;
  210. memcpy(&i2c_wb[USB251XB_ADDR_MANUFACTURER_STRING], hub->manufacturer,
  211. USB251XB_STRING_BUFSIZE);
  212. memcpy(&i2c_wb[USB251XB_ADDR_SERIAL_STRING], hub->serial,
  213. USB251XB_STRING_BUFSIZE);
  214. memcpy(&i2c_wb[USB251XB_ADDR_PRODUCT_STRING], hub->product,
  215. USB251XB_STRING_BUFSIZE);
  216. i2c_wb[USB251XB_ADDR_BATTERY_CHARGING_ENABLE] = hub->bat_charge_en;
  217. i2c_wb[USB251XB_ADDR_BOOST_UP] = hub->boost_up;
  218. i2c_wb[USB251XB_ADDR_BOOST_X] = hub->boost_x;
  219. i2c_wb[USB251XB_ADDR_PORT_SWAP] = hub->port_swap;
  220. i2c_wb[USB251XB_ADDR_PORT_MAP_12] = hub->port_map12;
  221. i2c_wb[USB251XB_ADDR_PORT_MAP_34] = hub->port_map34;
  222. i2c_wb[USB251XB_ADDR_STATUS_COMMAND] = USB251XB_STATUS_COMMAND_ATTACH;
  223. usb251xb_reset(hub, 1);
  224. /* write registers */
  225. for (i = 0; i < (USB251XB_I2C_REG_SZ / USB251XB_I2C_WRITE_SZ); i++) {
  226. int offset = i * USB251XB_I2C_WRITE_SZ;
  227. char wbuf[USB251XB_I2C_WRITE_SZ + 1];
  228. /* The first data byte transferred tells the hub how many data
  229. * bytes will follow (byte count).
  230. */
  231. wbuf[0] = USB251XB_I2C_WRITE_SZ;
  232. memcpy(&wbuf[1], &i2c_wb[offset], USB251XB_I2C_WRITE_SZ);
  233. dev_dbg(dev, "writing %d byte block %d to 0x%02X\n",
  234. USB251XB_I2C_WRITE_SZ, i, offset);
  235. err = i2c_smbus_write_i2c_block_data(hub->i2c, offset,
  236. USB251XB_I2C_WRITE_SZ + 1,
  237. wbuf);
  238. if (err)
  239. goto out_err;
  240. }
  241. dev_info(dev, "Hub configuration was successful.\n");
  242. return 0;
  243. out_err:
  244. dev_err(dev, "configuring block %d failed: %d\n", i, err);
  245. return err;
  246. }
  247. #ifdef CONFIG_OF
  248. static int usb251xb_get_ofdata(struct usb251xb *hub,
  249. struct usb251xb_data *data)
  250. {
  251. struct device *dev = hub->dev;
  252. struct device_node *np = dev->of_node;
  253. int len, err, i;
  254. u32 *property_u32 = NULL;
  255. const u32 *cproperty_u32;
  256. const char *cproperty_char;
  257. char str[USB251XB_STRING_BUFSIZE / 2];
  258. if (!np) {
  259. dev_err(dev, "failed to get ofdata\n");
  260. return -ENODEV;
  261. }
  262. if (of_get_property(np, "skip-config", NULL))
  263. hub->skip_config = 1;
  264. else
  265. hub->skip_config = 0;
  266. hub->gpio_reset = of_get_named_gpio(np, "reset-gpios", 0);
  267. if (hub->gpio_reset == -EPROBE_DEFER)
  268. return -EPROBE_DEFER;
  269. if (gpio_is_valid(hub->gpio_reset)) {
  270. err = devm_gpio_request_one(dev, hub->gpio_reset,
  271. GPIOF_OUT_INIT_LOW,
  272. "usb251xb reset");
  273. if (err) {
  274. dev_err(dev,
  275. "unable to request GPIO %d as reset pin (%d)\n",
  276. hub->gpio_reset, err);
  277. return err;
  278. }
  279. }
  280. if (of_property_read_u16_array(np, "vendor-id", &hub->vendor_id, 1))
  281. hub->vendor_id = USB251XB_DEF_VENDOR_ID;
  282. if (of_property_read_u16_array(np, "product-id",
  283. &hub->product_id, 1))
  284. hub->product_id = data->product_id;
  285. if (of_property_read_u16_array(np, "device-id", &hub->device_id, 1))
  286. hub->device_id = USB251XB_DEF_DEVICE_ID;
  287. hub->conf_data1 = USB251XB_DEF_CONFIG_DATA_1;
  288. if (of_get_property(np, "self-powered", NULL)) {
  289. hub->conf_data1 |= BIT(7);
  290. /* Configure Over-Current sens when self-powered */
  291. hub->conf_data1 &= ~BIT(2);
  292. if (of_get_property(np, "ganged-sensing", NULL))
  293. hub->conf_data1 &= ~BIT(1);
  294. else if (of_get_property(np, "individual-sensing", NULL))
  295. hub->conf_data1 |= BIT(1);
  296. } else if (of_get_property(np, "bus-powered", NULL)) {
  297. hub->conf_data1 &= ~BIT(7);
  298. /* Disable Over-Current sense when bus-powered */
  299. hub->conf_data1 |= BIT(2);
  300. }
  301. if (of_get_property(np, "disable-hi-speed", NULL))
  302. hub->conf_data1 |= BIT(5);
  303. if (of_get_property(np, "multi-tt", NULL))
  304. hub->conf_data1 |= BIT(4);
  305. else if (of_get_property(np, "single-tt", NULL))
  306. hub->conf_data1 &= ~BIT(4);
  307. if (of_get_property(np, "disable-eop", NULL))
  308. hub->conf_data1 |= BIT(3);
  309. if (of_get_property(np, "individual-port-switching", NULL))
  310. hub->conf_data1 |= BIT(0);
  311. else if (of_get_property(np, "ganged-port-switching", NULL))
  312. hub->conf_data1 &= ~BIT(0);
  313. hub->conf_data2 = USB251XB_DEF_CONFIG_DATA_2;
  314. if (of_get_property(np, "dynamic-power-switching", NULL))
  315. hub->conf_data2 |= BIT(7);
  316. if (!of_property_read_u32(np, "oc-delay-us", property_u32)) {
  317. if (*property_u32 == 100) {
  318. /* 100 us*/
  319. hub->conf_data2 &= ~BIT(5);
  320. hub->conf_data2 &= ~BIT(4);
  321. } else if (*property_u32 == 4000) {
  322. /* 4 ms */
  323. hub->conf_data2 &= ~BIT(5);
  324. hub->conf_data2 |= BIT(4);
  325. } else if (*property_u32 == 16000) {
  326. /* 16 ms */
  327. hub->conf_data2 |= BIT(5);
  328. hub->conf_data2 |= BIT(4);
  329. } else {
  330. /* 8 ms (DEFAULT) */
  331. hub->conf_data2 |= BIT(5);
  332. hub->conf_data2 &= ~BIT(4);
  333. }
  334. }
  335. if (of_get_property(np, "compound-device", NULL))
  336. hub->conf_data2 |= BIT(3);
  337. hub->conf_data3 = USB251XB_DEF_CONFIG_DATA_3;
  338. if (of_get_property(np, "port-mapping-mode", NULL))
  339. hub->conf_data3 |= BIT(3);
  340. if (of_get_property(np, "string-support", NULL))
  341. hub->conf_data3 |= BIT(0);
  342. hub->non_rem_dev = USB251XB_DEF_NON_REMOVABLE_DEVICES;
  343. cproperty_u32 = of_get_property(np, "non-removable-ports", &len);
  344. if (cproperty_u32 && (len / sizeof(u32)) > 0) {
  345. for (i = 0; i < len / sizeof(u32); i++) {
  346. u32 port = be32_to_cpu(cproperty_u32[i]);
  347. if ((port >= 1) && (port <= 4))
  348. hub->non_rem_dev |= BIT(port);
  349. }
  350. }
  351. hub->port_disable_sp = USB251XB_DEF_PORT_DISABLE_SELF;
  352. cproperty_u32 = of_get_property(np, "sp-disabled-ports", &len);
  353. if (cproperty_u32 && (len / sizeof(u32)) > 0) {
  354. for (i = 0; i < len / sizeof(u32); i++) {
  355. u32 port = be32_to_cpu(cproperty_u32[i]);
  356. if ((port >= 1) && (port <= 4))
  357. hub->port_disable_sp |= BIT(port);
  358. }
  359. }
  360. hub->port_disable_bp = USB251XB_DEF_PORT_DISABLE_BUS;
  361. cproperty_u32 = of_get_property(np, "bp-disabled-ports", &len);
  362. if (cproperty_u32 && (len / sizeof(u32)) > 0) {
  363. for (i = 0; i < len / sizeof(u32); i++) {
  364. u32 port = be32_to_cpu(cproperty_u32[i]);
  365. if ((port >= 1) && (port <= 4))
  366. hub->port_disable_bp |= BIT(port);
  367. }
  368. }
  369. hub->power_on_time = USB251XB_DEF_POWER_ON_TIME;
  370. if (!of_property_read_u32(np, "power-on-time-ms", property_u32))
  371. hub->power_on_time = min_t(u8, *property_u32 / 2, 255);
  372. if (of_property_read_u16_array(np, "language-id", &hub->lang_id, 1))
  373. hub->lang_id = USB251XB_DEF_LANGUAGE_ID;
  374. cproperty_char = of_get_property(np, "manufacturer", NULL);
  375. strlcpy(str, cproperty_char ? : USB251XB_DEF_MANUFACTURER_STRING,
  376. sizeof(str));
  377. hub->manufacturer_len = strlen(str) & 0xFF;
  378. memset(hub->manufacturer, 0, USB251XB_STRING_BUFSIZE);
  379. len = min_t(size_t, USB251XB_STRING_BUFSIZE / 2, strlen(str));
  380. len = utf8s_to_utf16s(str, len, UTF16_LITTLE_ENDIAN,
  381. (wchar_t *)hub->manufacturer,
  382. USB251XB_STRING_BUFSIZE);
  383. cproperty_char = of_get_property(np, "product", NULL);
  384. strlcpy(str, cproperty_char ? : data->product_str, sizeof(str));
  385. hub->product_len = strlen(str) & 0xFF;
  386. memset(hub->product, 0, USB251XB_STRING_BUFSIZE);
  387. len = min_t(size_t, USB251XB_STRING_BUFSIZE / 2, strlen(str));
  388. len = utf8s_to_utf16s(str, len, UTF16_LITTLE_ENDIAN,
  389. (wchar_t *)hub->product,
  390. USB251XB_STRING_BUFSIZE);
  391. cproperty_char = of_get_property(np, "serial", NULL);
  392. strlcpy(str, cproperty_char ? : USB251XB_DEF_SERIAL_STRING,
  393. sizeof(str));
  394. hub->serial_len = strlen(str) & 0xFF;
  395. memset(hub->serial, 0, USB251XB_STRING_BUFSIZE);
  396. len = min_t(size_t, USB251XB_STRING_BUFSIZE / 2, strlen(str));
  397. len = utf8s_to_utf16s(str, len, UTF16_LITTLE_ENDIAN,
  398. (wchar_t *)hub->serial,
  399. USB251XB_STRING_BUFSIZE);
  400. /* The following parameters are currently not exposed to devicetree, but
  401. * may be as soon as needed.
  402. */
  403. hub->max_power_sp = USB251XB_DEF_MAX_POWER_SELF;
  404. hub->max_power_bp = USB251XB_DEF_MAX_POWER_BUS;
  405. hub->max_current_sp = USB251XB_DEF_MAX_CURRENT_SELF;
  406. hub->max_current_bp = USB251XB_DEF_MAX_CURRENT_BUS;
  407. hub->bat_charge_en = USB251XB_DEF_BATTERY_CHARGING_ENABLE;
  408. hub->boost_up = USB251XB_DEF_BOOST_UP;
  409. hub->boost_x = USB251XB_DEF_BOOST_X;
  410. hub->port_swap = USB251XB_DEF_PORT_SWAP;
  411. hub->port_map12 = USB251XB_DEF_PORT_MAP_12;
  412. hub->port_map34 = USB251XB_DEF_PORT_MAP_34;
  413. return 0;
  414. }
  415. static const struct of_device_id usb251xb_of_match[] = {
  416. {
  417. .compatible = "microchip,usb2512b",
  418. .data = &usb2512b_data,
  419. }, {
  420. .compatible = "microchip,usb2512bi",
  421. .data = &usb2512bi_data,
  422. }, {
  423. .compatible = "microchip,usb2513b",
  424. .data = &usb2513b_data,
  425. }, {
  426. .compatible = "microchip,usb2513bi",
  427. .data = &usb2513bi_data,
  428. }, {
  429. .compatible = "microchip,usb2514b",
  430. .data = &usb2514b_data,
  431. }, {
  432. .compatible = "microchip,usb2514bi",
  433. .data = &usb2514bi_data,
  434. }, {
  435. /* sentinel */
  436. }
  437. };
  438. MODULE_DEVICE_TABLE(of, usb251xb_of_match);
  439. #else /* CONFIG_OF */
  440. static int usb251xb_get_ofdata(struct usb251xb *hub,
  441. struct usb251xb_data *data)
  442. {
  443. return 0;
  444. }
  445. #endif /* CONFIG_OF */
  446. static int usb251xb_probe(struct usb251xb *hub)
  447. {
  448. struct device *dev = hub->dev;
  449. struct device_node *np = dev->of_node;
  450. const struct of_device_id *of_id = of_match_device(usb251xb_of_match,
  451. dev);
  452. int err;
  453. if (np && of_id) {
  454. err = usb251xb_get_ofdata(hub,
  455. (struct usb251xb_data *)of_id->data);
  456. if (err) {
  457. dev_err(dev, "failed to get ofdata: %d\n", err);
  458. return err;
  459. }
  460. }
  461. err = usb251xb_connect(hub);
  462. if (err) {
  463. dev_err(dev, "Failed to connect hub (%d)\n", err);
  464. return err;
  465. }
  466. dev_info(dev, "Hub probed successfully\n");
  467. return 0;
  468. }
  469. static int usb251xb_i2c_probe(struct i2c_client *i2c,
  470. const struct i2c_device_id *id)
  471. {
  472. struct usb251xb *hub;
  473. hub = devm_kzalloc(&i2c->dev, sizeof(struct usb251xb), GFP_KERNEL);
  474. if (!hub)
  475. return -ENOMEM;
  476. i2c_set_clientdata(i2c, hub);
  477. hub->dev = &i2c->dev;
  478. hub->i2c = i2c;
  479. return usb251xb_probe(hub);
  480. }
  481. static const struct i2c_device_id usb251xb_id[] = {
  482. { "usb2512b", 0 },
  483. { "usb2512bi", 0 },
  484. { "usb2513b", 0 },
  485. { "usb2513bi", 0 },
  486. { "usb2514b", 0 },
  487. { "usb2514bi", 0 },
  488. { /* sentinel */ }
  489. };
  490. MODULE_DEVICE_TABLE(i2c, usb251xb_id);
  491. static struct i2c_driver usb251xb_i2c_driver = {
  492. .driver = {
  493. .name = DRIVER_NAME,
  494. .of_match_table = of_match_ptr(usb251xb_of_match),
  495. },
  496. .probe = usb251xb_i2c_probe,
  497. .id_table = usb251xb_id,
  498. };
  499. module_i2c_driver(usb251xb_i2c_driver);
  500. MODULE_AUTHOR("Richard Leitner <richard.leitner@skidata.com>");
  501. MODULE_DESCRIPTION("USB251xB/xBi USB 2.0 Hub Controller Driver");
  502. MODULE_LICENSE("GPL");