usb251xb.c 19 KB

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