Greg Kroah-Hartman | 5fd54ac | 2017-11-03 11:28:30 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Richard Leitner | 3ec72a2 | 2017-02-10 09:19:27 +0100 | [diff] [blame] | 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/ |
Richard Leitner | 3ec72a2 | 2017-02-10 09:19:27 +0100 | [diff] [blame] | 11 | */ |
| 12 | |
| 13 | #include <linux/delay.h> |
Serge Semin | 69a0c69 | 2017-10-22 23:38:12 +0300 | [diff] [blame] | 14 | #include <linux/gpio/consumer.h> |
Serge Semin | 6e3c8be | 2019-04-24 17:49:14 +0300 | [diff] [blame] | 15 | #include <linux/gpio/driver.h> |
Richard Leitner | 3ec72a2 | 2017-02-10 09:19:27 +0100 | [diff] [blame] | 16 | #include <linux/i2c.h> |
| 17 | #include <linux/module.h> |
| 18 | #include <linux/nls.h> |
| 19 | #include <linux/of_device.h> |
Marco Felsch | 4d7201c | 2019-09-17 16:44:47 +0200 | [diff] [blame] | 20 | #include <linux/regulator/consumer.h> |
Richard Leitner | 3ec72a2 | 2017-02-10 09:19:27 +0100 | [diff] [blame] | 21 | #include <linux/slab.h> |
| 22 | |
| 23 | /* Internal Register Set Addresses & Default Values acc. to DS00001692C */ |
| 24 | #define USB251XB_ADDR_VENDOR_ID_LSB 0x00 |
| 25 | #define USB251XB_ADDR_VENDOR_ID_MSB 0x01 |
| 26 | #define USB251XB_DEF_VENDOR_ID 0x0424 |
| 27 | |
| 28 | #define USB251XB_ADDR_PRODUCT_ID_LSB 0x02 |
| 29 | #define USB251XB_ADDR_PRODUCT_ID_MSB 0x03 |
| 30 | #define USB251XB_DEF_PRODUCT_ID_12 0x2512 /* USB2512B/12Bi */ |
| 31 | #define USB251XB_DEF_PRODUCT_ID_13 0x2513 /* USB2513B/13Bi */ |
| 32 | #define USB251XB_DEF_PRODUCT_ID_14 0x2514 /* USB2514B/14Bi */ |
Serge Semin | 7fcf558 | 2017-10-22 23:38:04 +0300 | [diff] [blame] | 33 | #define USB251XB_DEF_PRODUCT_ID_17 0x2517 /* USB2517/17i */ |
Richard Leitner | 3ec72a2 | 2017-02-10 09:19:27 +0100 | [diff] [blame] | 34 | |
| 35 | #define USB251XB_ADDR_DEVICE_ID_LSB 0x04 |
| 36 | #define USB251XB_ADDR_DEVICE_ID_MSB 0x05 |
| 37 | #define USB251XB_DEF_DEVICE_ID 0x0BB3 |
| 38 | |
| 39 | #define USB251XB_ADDR_CONFIG_DATA_1 0x06 |
| 40 | #define USB251XB_DEF_CONFIG_DATA_1 0x9B |
| 41 | #define USB251XB_ADDR_CONFIG_DATA_2 0x07 |
| 42 | #define USB251XB_DEF_CONFIG_DATA_2 0x20 |
| 43 | #define USB251XB_ADDR_CONFIG_DATA_3 0x08 |
| 44 | #define USB251XB_DEF_CONFIG_DATA_3 0x02 |
| 45 | |
| 46 | #define USB251XB_ADDR_NON_REMOVABLE_DEVICES 0x09 |
| 47 | #define USB251XB_DEF_NON_REMOVABLE_DEVICES 0x00 |
| 48 | |
| 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 | |
| 54 | #define USB251XB_ADDR_MAX_POWER_SELF 0x0C |
| 55 | #define USB251XB_DEF_MAX_POWER_SELF 0x01 |
| 56 | #define USB251XB_ADDR_MAX_POWER_BUS 0x0D |
| 57 | #define USB251XB_DEF_MAX_POWER_BUS 0x32 |
| 58 | |
| 59 | #define USB251XB_ADDR_MAX_CURRENT_SELF 0x0E |
| 60 | #define USB251XB_DEF_MAX_CURRENT_SELF 0x01 |
| 61 | #define USB251XB_ADDR_MAX_CURRENT_BUS 0x0F |
| 62 | #define USB251XB_DEF_MAX_CURRENT_BUS 0x32 |
| 63 | |
| 64 | #define USB251XB_ADDR_POWER_ON_TIME 0x10 |
| 65 | #define USB251XB_DEF_POWER_ON_TIME 0x32 |
| 66 | |
| 67 | #define USB251XB_ADDR_LANGUAGE_ID_HIGH 0x11 |
| 68 | #define USB251XB_ADDR_LANGUAGE_ID_LOW 0x12 |
| 69 | #define USB251XB_DEF_LANGUAGE_ID 0x0000 |
| 70 | |
| 71 | #define USB251XB_STRING_BUFSIZE 62 |
| 72 | #define USB251XB_ADDR_MANUFACTURER_STRING_LEN 0x13 |
| 73 | #define USB251XB_ADDR_MANUFACTURER_STRING 0x16 |
| 74 | #define USB251XB_DEF_MANUFACTURER_STRING "Microchip" |
| 75 | |
| 76 | #define USB251XB_ADDR_PRODUCT_STRING_LEN 0x14 |
| 77 | #define USB251XB_ADDR_PRODUCT_STRING 0x54 |
Serge Semin | 7fcf558 | 2017-10-22 23:38:04 +0300 | [diff] [blame] | 78 | #define USB251XB_DEF_PRODUCT_STRING "USB251xB/xBi/7i" |
Richard Leitner | 3ec72a2 | 2017-02-10 09:19:27 +0100 | [diff] [blame] | 79 | |
| 80 | #define USB251XB_ADDR_SERIAL_STRING_LEN 0x15 |
| 81 | #define USB251XB_ADDR_SERIAL_STRING 0x92 |
| 82 | #define USB251XB_DEF_SERIAL_STRING "" |
| 83 | |
| 84 | #define USB251XB_ADDR_BATTERY_CHARGING_ENABLE 0xD0 |
| 85 | #define USB251XB_DEF_BATTERY_CHARGING_ENABLE 0x00 |
| 86 | |
| 87 | #define USB251XB_ADDR_BOOST_UP 0xF6 |
| 88 | #define USB251XB_DEF_BOOST_UP 0x00 |
Serge Semin | ccdddc0 | 2017-10-22 23:38:07 +0300 | [diff] [blame] | 89 | #define USB251XB_ADDR_BOOST_57 0xF7 |
| 90 | #define USB251XB_DEF_BOOST_57 0x00 |
| 91 | #define USB251XB_ADDR_BOOST_14 0xF8 |
| 92 | #define USB251XB_DEF_BOOST_14 0x00 |
Richard Leitner | 3ec72a2 | 2017-02-10 09:19:27 +0100 | [diff] [blame] | 93 | |
| 94 | #define USB251XB_ADDR_PORT_SWAP 0xFA |
| 95 | #define USB251XB_DEF_PORT_SWAP 0x00 |
| 96 | |
| 97 | #define USB251XB_ADDR_PORT_MAP_12 0xFB |
| 98 | #define USB251XB_DEF_PORT_MAP_12 0x00 |
| 99 | #define USB251XB_ADDR_PORT_MAP_34 0xFC |
Serge Semin | db234b9 | 2017-10-22 23:38:06 +0300 | [diff] [blame] | 100 | #define USB251XB_DEF_PORT_MAP_34 0x00 /* USB251{3B/i,4B/i,7/i} only */ |
| 101 | #define USB251XB_ADDR_PORT_MAP_56 0xFD |
| 102 | #define USB251XB_DEF_PORT_MAP_56 0x00 /* USB2517/i only */ |
| 103 | #define USB251XB_ADDR_PORT_MAP_7 0xFE |
| 104 | #define USB251XB_DEF_PORT_MAP_7 0x00 /* USB2517/i only */ |
Richard Leitner | 3ec72a2 | 2017-02-10 09:19:27 +0100 | [diff] [blame] | 105 | |
| 106 | #define USB251XB_ADDR_STATUS_COMMAND 0xFF |
| 107 | #define USB251XB_STATUS_COMMAND_SMBUS_DOWN 0x04 |
| 108 | #define USB251XB_STATUS_COMMAND_RESET 0x02 |
| 109 | #define USB251XB_STATUS_COMMAND_ATTACH 0x01 |
| 110 | |
| 111 | #define USB251XB_I2C_REG_SZ 0x100 |
| 112 | #define USB251XB_I2C_WRITE_SZ 0x10 |
| 113 | |
| 114 | #define DRIVER_NAME "usb251xb" |
| 115 | #define DRIVER_DESC "Microchip USB 2.0 Hi-Speed Hub Controller" |
Richard Leitner | 3ec72a2 | 2017-02-10 09:19:27 +0100 | [diff] [blame] | 116 | |
| 117 | struct usb251xb { |
| 118 | struct device *dev; |
| 119 | struct i2c_client *i2c; |
Marco Felsch | 4d7201c | 2019-09-17 16:44:47 +0200 | [diff] [blame] | 120 | struct regulator *vdd; |
Richard Leitner | 3ec72a2 | 2017-02-10 09:19:27 +0100 | [diff] [blame] | 121 | u8 skip_config; |
Serge Semin | 69a0c69 | 2017-10-22 23:38:12 +0300 | [diff] [blame] | 122 | struct gpio_desc *gpio_reset; |
Richard Leitner | 3ec72a2 | 2017-02-10 09:19:27 +0100 | [diff] [blame] | 123 | u16 vendor_id; |
| 124 | u16 product_id; |
| 125 | u16 device_id; |
| 126 | u8 conf_data1; |
| 127 | u8 conf_data2; |
| 128 | u8 conf_data3; |
| 129 | u8 non_rem_dev; |
| 130 | u8 port_disable_sp; |
| 131 | u8 port_disable_bp; |
| 132 | u8 max_power_sp; |
| 133 | u8 max_power_bp; |
| 134 | u8 max_current_sp; |
| 135 | u8 max_current_bp; |
| 136 | u8 power_on_time; |
| 137 | u16 lang_id; |
| 138 | u8 manufacturer_len; |
| 139 | u8 product_len; |
| 140 | u8 serial_len; |
| 141 | char manufacturer[USB251XB_STRING_BUFSIZE]; |
| 142 | char product[USB251XB_STRING_BUFSIZE]; |
| 143 | char serial[USB251XB_STRING_BUFSIZE]; |
| 144 | u8 bat_charge_en; |
| 145 | u8 boost_up; |
Serge Semin | ccdddc0 | 2017-10-22 23:38:07 +0300 | [diff] [blame] | 146 | u8 boost_57; |
| 147 | u8 boost_14; |
Richard Leitner | 3ec72a2 | 2017-02-10 09:19:27 +0100 | [diff] [blame] | 148 | u8 port_swap; |
| 149 | u8 port_map12; |
| 150 | u8 port_map34; |
Serge Semin | db234b9 | 2017-10-22 23:38:06 +0300 | [diff] [blame] | 151 | u8 port_map56; |
| 152 | u8 port_map7; |
Richard Leitner | 3ec72a2 | 2017-02-10 09:19:27 +0100 | [diff] [blame] | 153 | u8 status; |
| 154 | }; |
| 155 | |
| 156 | struct usb251xb_data { |
| 157 | u16 product_id; |
Serge Semin | 3848112 | 2017-10-22 23:38:05 +0300 | [diff] [blame] | 158 | u8 port_cnt; |
Serge Semin | 2818e13 | 2017-10-22 23:38:09 +0300 | [diff] [blame] | 159 | bool led_support; |
Serge Semin | dd99d10 | 2017-10-22 23:38:08 +0300 | [diff] [blame] | 160 | bool bat_support; |
Richard Leitner | 3ec72a2 | 2017-02-10 09:19:27 +0100 | [diff] [blame] | 161 | char product_str[USB251XB_STRING_BUFSIZE / 2]; /* ASCII string */ |
| 162 | }; |
| 163 | |
| 164 | static const struct usb251xb_data usb2512b_data = { |
| 165 | .product_id = 0x2512, |
Serge Semin | 3848112 | 2017-10-22 23:38:05 +0300 | [diff] [blame] | 166 | .port_cnt = 2, |
Serge Semin | 2818e13 | 2017-10-22 23:38:09 +0300 | [diff] [blame] | 167 | .led_support = false, |
Serge Semin | dd99d10 | 2017-10-22 23:38:08 +0300 | [diff] [blame] | 168 | .bat_support = true, |
Richard Leitner | 3ec72a2 | 2017-02-10 09:19:27 +0100 | [diff] [blame] | 169 | .product_str = "USB2512B", |
| 170 | }; |
| 171 | |
| 172 | static const struct usb251xb_data usb2512bi_data = { |
| 173 | .product_id = 0x2512, |
Serge Semin | 3848112 | 2017-10-22 23:38:05 +0300 | [diff] [blame] | 174 | .port_cnt = 2, |
Serge Semin | 2818e13 | 2017-10-22 23:38:09 +0300 | [diff] [blame] | 175 | .led_support = false, |
Serge Semin | dd99d10 | 2017-10-22 23:38:08 +0300 | [diff] [blame] | 176 | .bat_support = true, |
Richard Leitner | 3ec72a2 | 2017-02-10 09:19:27 +0100 | [diff] [blame] | 177 | .product_str = "USB2512Bi", |
| 178 | }; |
| 179 | |
| 180 | static const struct usb251xb_data usb2513b_data = { |
| 181 | .product_id = 0x2513, |
Serge Semin | 3848112 | 2017-10-22 23:38:05 +0300 | [diff] [blame] | 182 | .port_cnt = 3, |
Serge Semin | 2818e13 | 2017-10-22 23:38:09 +0300 | [diff] [blame] | 183 | .led_support = false, |
Serge Semin | dd99d10 | 2017-10-22 23:38:08 +0300 | [diff] [blame] | 184 | .bat_support = true, |
Richard Leitner | 3ec72a2 | 2017-02-10 09:19:27 +0100 | [diff] [blame] | 185 | .product_str = "USB2513B", |
| 186 | }; |
| 187 | |
| 188 | static const struct usb251xb_data usb2513bi_data = { |
| 189 | .product_id = 0x2513, |
Serge Semin | 3848112 | 2017-10-22 23:38:05 +0300 | [diff] [blame] | 190 | .port_cnt = 3, |
Serge Semin | 2818e13 | 2017-10-22 23:38:09 +0300 | [diff] [blame] | 191 | .led_support = false, |
Serge Semin | dd99d10 | 2017-10-22 23:38:08 +0300 | [diff] [blame] | 192 | .bat_support = true, |
Richard Leitner | 3ec72a2 | 2017-02-10 09:19:27 +0100 | [diff] [blame] | 193 | .product_str = "USB2513Bi", |
| 194 | }; |
| 195 | |
| 196 | static const struct usb251xb_data usb2514b_data = { |
| 197 | .product_id = 0x2514, |
Serge Semin | 3848112 | 2017-10-22 23:38:05 +0300 | [diff] [blame] | 198 | .port_cnt = 4, |
Serge Semin | 2818e13 | 2017-10-22 23:38:09 +0300 | [diff] [blame] | 199 | .led_support = false, |
Serge Semin | dd99d10 | 2017-10-22 23:38:08 +0300 | [diff] [blame] | 200 | .bat_support = true, |
Richard Leitner | 3ec72a2 | 2017-02-10 09:19:27 +0100 | [diff] [blame] | 201 | .product_str = "USB2514B", |
| 202 | }; |
| 203 | |
| 204 | static const struct usb251xb_data usb2514bi_data = { |
| 205 | .product_id = 0x2514, |
Serge Semin | 3848112 | 2017-10-22 23:38:05 +0300 | [diff] [blame] | 206 | .port_cnt = 4, |
Serge Semin | 2818e13 | 2017-10-22 23:38:09 +0300 | [diff] [blame] | 207 | .led_support = false, |
Serge Semin | dd99d10 | 2017-10-22 23:38:08 +0300 | [diff] [blame] | 208 | .bat_support = true, |
Richard Leitner | 3ec72a2 | 2017-02-10 09:19:27 +0100 | [diff] [blame] | 209 | .product_str = "USB2514Bi", |
| 210 | }; |
| 211 | |
Serge Semin | 7fcf558 | 2017-10-22 23:38:04 +0300 | [diff] [blame] | 212 | static const struct usb251xb_data usb2517_data = { |
| 213 | .product_id = 0x2517, |
Serge Semin | 3848112 | 2017-10-22 23:38:05 +0300 | [diff] [blame] | 214 | .port_cnt = 7, |
Serge Semin | 2818e13 | 2017-10-22 23:38:09 +0300 | [diff] [blame] | 215 | .led_support = true, |
Serge Semin | dd99d10 | 2017-10-22 23:38:08 +0300 | [diff] [blame] | 216 | .bat_support = false, |
Serge Semin | 7fcf558 | 2017-10-22 23:38:04 +0300 | [diff] [blame] | 217 | .product_str = "USB2517", |
| 218 | }; |
| 219 | |
| 220 | static const struct usb251xb_data usb2517i_data = { |
| 221 | .product_id = 0x2517, |
Serge Semin | 3848112 | 2017-10-22 23:38:05 +0300 | [diff] [blame] | 222 | .port_cnt = 7, |
Serge Semin | 2818e13 | 2017-10-22 23:38:09 +0300 | [diff] [blame] | 223 | .led_support = true, |
Serge Semin | dd99d10 | 2017-10-22 23:38:08 +0300 | [diff] [blame] | 224 | .bat_support = false, |
Serge Semin | 7fcf558 | 2017-10-22 23:38:04 +0300 | [diff] [blame] | 225 | .product_str = "USB2517i", |
| 226 | }; |
| 227 | |
Serge Semin | 95e060e | 2019-04-27 12:06:44 +0300 | [diff] [blame] | 228 | #ifdef CONFIG_GPIOLIB |
Serge Semin | 6e3c8be | 2019-04-24 17:49:14 +0300 | [diff] [blame] | 229 | static int usb251xb_check_dev_children(struct device *dev, void *child) |
| 230 | { |
| 231 | if (dev->type == &i2c_adapter_type) { |
| 232 | return device_for_each_child(dev, child, |
| 233 | usb251xb_check_dev_children); |
| 234 | } |
| 235 | |
| 236 | return (dev == child); |
| 237 | } |
| 238 | |
| 239 | static int usb251x_check_gpio_chip(struct usb251xb *hub) |
| 240 | { |
| 241 | struct gpio_chip *gc = gpiod_to_chip(hub->gpio_reset); |
| 242 | struct i2c_adapter *adap = hub->i2c->adapter; |
| 243 | int ret; |
| 244 | |
| 245 | if (!hub->gpio_reset) |
| 246 | return 0; |
| 247 | |
| 248 | if (!gc) |
| 249 | return -EINVAL; |
| 250 | |
| 251 | ret = usb251xb_check_dev_children(&adap->dev, gc->parent); |
| 252 | if (ret) { |
| 253 | dev_err(hub->dev, "Reset GPIO chip is at the same i2c-bus\n"); |
| 254 | return -EINVAL; |
| 255 | } |
| 256 | |
| 257 | return 0; |
| 258 | } |
Serge Semin | 95e060e | 2019-04-27 12:06:44 +0300 | [diff] [blame] | 259 | #else |
| 260 | static int usb251x_check_gpio_chip(struct usb251xb *hub) |
| 261 | { |
| 262 | return 0; |
| 263 | } |
| 264 | #endif |
Serge Semin | 6e3c8be | 2019-04-24 17:49:14 +0300 | [diff] [blame] | 265 | |
Marco Felsch | a9bab25 | 2019-09-17 16:44:48 +0200 | [diff] [blame^] | 266 | static void usb251xb_reset(struct usb251xb *hub) |
Richard Leitner | 3ec72a2 | 2017-02-10 09:19:27 +0100 | [diff] [blame] | 267 | { |
Serge Semin | 69a0c69 | 2017-10-22 23:38:12 +0300 | [diff] [blame] | 268 | if (!hub->gpio_reset) |
Richard Leitner | 3ec72a2 | 2017-02-10 09:19:27 +0100 | [diff] [blame] | 269 | return; |
| 270 | |
Serge Semin | 6e3c8be | 2019-04-24 17:49:14 +0300 | [diff] [blame] | 271 | i2c_lock_bus(hub->i2c->adapter, I2C_LOCK_SEGMENT); |
| 272 | |
Marco Felsch | a9bab25 | 2019-09-17 16:44:48 +0200 | [diff] [blame^] | 273 | gpiod_set_value_cansleep(hub->gpio_reset, 1); |
| 274 | usleep_range(1, 10); /* >=1us RESET_N asserted */ |
| 275 | gpiod_set_value_cansleep(hub->gpio_reset, 0); |
Richard Leitner | 3ec72a2 | 2017-02-10 09:19:27 +0100 | [diff] [blame] | 276 | |
| 277 | /* wait for hub recovery/stabilization */ |
Marco Felsch | a9bab25 | 2019-09-17 16:44:48 +0200 | [diff] [blame^] | 278 | usleep_range(500, 750); /* >=500us after RESET_N deasserted */ |
Serge Semin | 6e3c8be | 2019-04-24 17:49:14 +0300 | [diff] [blame] | 279 | |
| 280 | i2c_unlock_bus(hub->i2c->adapter, I2C_LOCK_SEGMENT); |
Richard Leitner | 3ec72a2 | 2017-02-10 09:19:27 +0100 | [diff] [blame] | 281 | } |
| 282 | |
| 283 | static int usb251xb_connect(struct usb251xb *hub) |
| 284 | { |
| 285 | struct device *dev = hub->dev; |
| 286 | int err, i; |
| 287 | char i2c_wb[USB251XB_I2C_REG_SZ]; |
| 288 | |
| 289 | memset(i2c_wb, 0, USB251XB_I2C_REG_SZ); |
| 290 | |
| 291 | if (hub->skip_config) { |
| 292 | dev_info(dev, "Skip hub configuration, only attach.\n"); |
| 293 | i2c_wb[0] = 0x01; |
| 294 | i2c_wb[1] = USB251XB_STATUS_COMMAND_ATTACH; |
| 295 | |
Marco Felsch | a9bab25 | 2019-09-17 16:44:48 +0200 | [diff] [blame^] | 296 | usb251xb_reset(hub); |
Richard Leitner | 3ec72a2 | 2017-02-10 09:19:27 +0100 | [diff] [blame] | 297 | |
| 298 | err = i2c_smbus_write_i2c_block_data(hub->i2c, |
| 299 | USB251XB_ADDR_STATUS_COMMAND, 2, i2c_wb); |
| 300 | if (err) { |
| 301 | dev_err(dev, "attaching hub failed: %d\n", err); |
| 302 | return err; |
| 303 | } |
| 304 | return 0; |
| 305 | } |
| 306 | |
| 307 | i2c_wb[USB251XB_ADDR_VENDOR_ID_MSB] = (hub->vendor_id >> 8) & 0xFF; |
| 308 | i2c_wb[USB251XB_ADDR_VENDOR_ID_LSB] = hub->vendor_id & 0xFF; |
| 309 | i2c_wb[USB251XB_ADDR_PRODUCT_ID_MSB] = (hub->product_id >> 8) & 0xFF; |
| 310 | i2c_wb[USB251XB_ADDR_PRODUCT_ID_LSB] = hub->product_id & 0xFF; |
| 311 | i2c_wb[USB251XB_ADDR_DEVICE_ID_MSB] = (hub->device_id >> 8) & 0xFF; |
| 312 | i2c_wb[USB251XB_ADDR_DEVICE_ID_LSB] = hub->device_id & 0xFF; |
| 313 | i2c_wb[USB251XB_ADDR_CONFIG_DATA_1] = hub->conf_data1; |
| 314 | i2c_wb[USB251XB_ADDR_CONFIG_DATA_2] = hub->conf_data2; |
| 315 | i2c_wb[USB251XB_ADDR_CONFIG_DATA_3] = hub->conf_data3; |
| 316 | i2c_wb[USB251XB_ADDR_NON_REMOVABLE_DEVICES] = hub->non_rem_dev; |
| 317 | i2c_wb[USB251XB_ADDR_PORT_DISABLE_SELF] = hub->port_disable_sp; |
| 318 | i2c_wb[USB251XB_ADDR_PORT_DISABLE_BUS] = hub->port_disable_bp; |
| 319 | i2c_wb[USB251XB_ADDR_MAX_POWER_SELF] = hub->max_power_sp; |
| 320 | i2c_wb[USB251XB_ADDR_MAX_POWER_BUS] = hub->max_power_bp; |
| 321 | i2c_wb[USB251XB_ADDR_MAX_CURRENT_SELF] = hub->max_current_sp; |
| 322 | i2c_wb[USB251XB_ADDR_MAX_CURRENT_BUS] = hub->max_current_bp; |
| 323 | i2c_wb[USB251XB_ADDR_POWER_ON_TIME] = hub->power_on_time; |
| 324 | i2c_wb[USB251XB_ADDR_LANGUAGE_ID_HIGH] = (hub->lang_id >> 8) & 0xFF; |
| 325 | i2c_wb[USB251XB_ADDR_LANGUAGE_ID_LOW] = hub->lang_id & 0xFF; |
| 326 | i2c_wb[USB251XB_ADDR_MANUFACTURER_STRING_LEN] = hub->manufacturer_len; |
| 327 | i2c_wb[USB251XB_ADDR_PRODUCT_STRING_LEN] = hub->product_len; |
| 328 | i2c_wb[USB251XB_ADDR_SERIAL_STRING_LEN] = hub->serial_len; |
| 329 | memcpy(&i2c_wb[USB251XB_ADDR_MANUFACTURER_STRING], hub->manufacturer, |
| 330 | USB251XB_STRING_BUFSIZE); |
| 331 | memcpy(&i2c_wb[USB251XB_ADDR_SERIAL_STRING], hub->serial, |
| 332 | USB251XB_STRING_BUFSIZE); |
| 333 | memcpy(&i2c_wb[USB251XB_ADDR_PRODUCT_STRING], hub->product, |
| 334 | USB251XB_STRING_BUFSIZE); |
| 335 | i2c_wb[USB251XB_ADDR_BATTERY_CHARGING_ENABLE] = hub->bat_charge_en; |
| 336 | i2c_wb[USB251XB_ADDR_BOOST_UP] = hub->boost_up; |
Serge Semin | ccdddc0 | 2017-10-22 23:38:07 +0300 | [diff] [blame] | 337 | i2c_wb[USB251XB_ADDR_BOOST_57] = hub->boost_57; |
| 338 | i2c_wb[USB251XB_ADDR_BOOST_14] = hub->boost_14; |
Richard Leitner | 3ec72a2 | 2017-02-10 09:19:27 +0100 | [diff] [blame] | 339 | i2c_wb[USB251XB_ADDR_PORT_SWAP] = hub->port_swap; |
| 340 | i2c_wb[USB251XB_ADDR_PORT_MAP_12] = hub->port_map12; |
| 341 | i2c_wb[USB251XB_ADDR_PORT_MAP_34] = hub->port_map34; |
Serge Semin | db234b9 | 2017-10-22 23:38:06 +0300 | [diff] [blame] | 342 | i2c_wb[USB251XB_ADDR_PORT_MAP_56] = hub->port_map56; |
| 343 | i2c_wb[USB251XB_ADDR_PORT_MAP_7] = hub->port_map7; |
Richard Leitner | 3ec72a2 | 2017-02-10 09:19:27 +0100 | [diff] [blame] | 344 | i2c_wb[USB251XB_ADDR_STATUS_COMMAND] = USB251XB_STATUS_COMMAND_ATTACH; |
| 345 | |
Marco Felsch | a9bab25 | 2019-09-17 16:44:48 +0200 | [diff] [blame^] | 346 | usb251xb_reset(hub); |
Richard Leitner | 3ec72a2 | 2017-02-10 09:19:27 +0100 | [diff] [blame] | 347 | |
| 348 | /* write registers */ |
| 349 | for (i = 0; i < (USB251XB_I2C_REG_SZ / USB251XB_I2C_WRITE_SZ); i++) { |
| 350 | int offset = i * USB251XB_I2C_WRITE_SZ; |
| 351 | char wbuf[USB251XB_I2C_WRITE_SZ + 1]; |
| 352 | |
| 353 | /* The first data byte transferred tells the hub how many data |
| 354 | * bytes will follow (byte count). |
| 355 | */ |
| 356 | wbuf[0] = USB251XB_I2C_WRITE_SZ; |
| 357 | memcpy(&wbuf[1], &i2c_wb[offset], USB251XB_I2C_WRITE_SZ); |
| 358 | |
| 359 | dev_dbg(dev, "writing %d byte block %d to 0x%02X\n", |
| 360 | USB251XB_I2C_WRITE_SZ, i, offset); |
| 361 | |
| 362 | err = i2c_smbus_write_i2c_block_data(hub->i2c, offset, |
| 363 | USB251XB_I2C_WRITE_SZ + 1, |
| 364 | wbuf); |
| 365 | if (err) |
| 366 | goto out_err; |
| 367 | } |
| 368 | |
| 369 | dev_info(dev, "Hub configuration was successful.\n"); |
| 370 | return 0; |
| 371 | |
| 372 | out_err: |
| 373 | dev_err(dev, "configuring block %d failed: %d\n", i, err); |
| 374 | return err; |
| 375 | } |
| 376 | |
| 377 | #ifdef CONFIG_OF |
Serge Semin | 73d31de | 2019-04-25 00:12:06 +0300 | [diff] [blame] | 378 | static void usb251xb_get_ports_field(struct usb251xb *hub, |
Lucas Stach | 4849ee6 | 2019-07-19 10:44:07 +0200 | [diff] [blame] | 379 | const char *prop_name, u8 port_cnt, |
| 380 | bool ds_only, u8 *fld) |
Serge Semin | 73d31de | 2019-04-25 00:12:06 +0300 | [diff] [blame] | 381 | { |
| 382 | struct device *dev = hub->dev; |
| 383 | struct property *prop; |
| 384 | const __be32 *p; |
| 385 | u32 port; |
| 386 | |
| 387 | of_property_for_each_u32(dev->of_node, prop_name, prop, p, port) { |
Lucas Stach | 4849ee6 | 2019-07-19 10:44:07 +0200 | [diff] [blame] | 388 | if ((port >= ds_only ? 1 : 0) && (port <= port_cnt)) |
Serge Semin | 73d31de | 2019-04-25 00:12:06 +0300 | [diff] [blame] | 389 | *fld |= BIT(port); |
| 390 | else |
| 391 | dev_warn(dev, "port %u doesn't exist\n", port); |
| 392 | } |
| 393 | } |
| 394 | |
Richard Leitner | 3ec72a2 | 2017-02-10 09:19:27 +0100 | [diff] [blame] | 395 | static int usb251xb_get_ofdata(struct usb251xb *hub, |
| 396 | struct usb251xb_data *data) |
| 397 | { |
| 398 | struct device *dev = hub->dev; |
| 399 | struct device_node *np = dev->of_node; |
Serge Semin | 73d31de | 2019-04-25 00:12:06 +0300 | [diff] [blame] | 400 | int len, err; |
| 401 | u32 property_u32 = 0; |
Richard Leitner | 3ec72a2 | 2017-02-10 09:19:27 +0100 | [diff] [blame] | 402 | const char *cproperty_char; |
| 403 | char str[USB251XB_STRING_BUFSIZE / 2]; |
| 404 | |
| 405 | if (!np) { |
| 406 | dev_err(dev, "failed to get ofdata\n"); |
| 407 | return -ENODEV; |
| 408 | } |
| 409 | |
| 410 | if (of_get_property(np, "skip-config", NULL)) |
| 411 | hub->skip_config = 1; |
| 412 | else |
| 413 | hub->skip_config = 0; |
| 414 | |
Serge Semin | 69a0c69 | 2017-10-22 23:38:12 +0300 | [diff] [blame] | 415 | hub->gpio_reset = devm_gpiod_get_optional(dev, "reset", GPIOD_OUT_HIGH); |
| 416 | if (PTR_ERR(hub->gpio_reset) == -EPROBE_DEFER) { |
Richard Leitner | 3ec72a2 | 2017-02-10 09:19:27 +0100 | [diff] [blame] | 417 | return -EPROBE_DEFER; |
Serge Semin | 69a0c69 | 2017-10-22 23:38:12 +0300 | [diff] [blame] | 418 | } else if (IS_ERR(hub->gpio_reset)) { |
| 419 | err = PTR_ERR(hub->gpio_reset); |
| 420 | dev_err(dev, "unable to request GPIO reset pin (%d)\n", err); |
| 421 | return err; |
Richard Leitner | 3ec72a2 | 2017-02-10 09:19:27 +0100 | [diff] [blame] | 422 | } |
| 423 | |
Marco Felsch | 4d7201c | 2019-09-17 16:44:47 +0200 | [diff] [blame] | 424 | hub->vdd = devm_regulator_get(dev, "vdd"); |
| 425 | if (IS_ERR(hub->vdd)) |
| 426 | return PTR_ERR(hub->vdd); |
| 427 | |
Richard Leitner | 3ec72a2 | 2017-02-10 09:19:27 +0100 | [diff] [blame] | 428 | if (of_property_read_u16_array(np, "vendor-id", &hub->vendor_id, 1)) |
| 429 | hub->vendor_id = USB251XB_DEF_VENDOR_ID; |
| 430 | |
| 431 | if (of_property_read_u16_array(np, "product-id", |
| 432 | &hub->product_id, 1)) |
| 433 | hub->product_id = data->product_id; |
| 434 | |
| 435 | if (of_property_read_u16_array(np, "device-id", &hub->device_id, 1)) |
| 436 | hub->device_id = USB251XB_DEF_DEVICE_ID; |
| 437 | |
| 438 | hub->conf_data1 = USB251XB_DEF_CONFIG_DATA_1; |
| 439 | if (of_get_property(np, "self-powered", NULL)) { |
| 440 | hub->conf_data1 |= BIT(7); |
| 441 | |
| 442 | /* Configure Over-Current sens when self-powered */ |
| 443 | hub->conf_data1 &= ~BIT(2); |
| 444 | if (of_get_property(np, "ganged-sensing", NULL)) |
| 445 | hub->conf_data1 &= ~BIT(1); |
| 446 | else if (of_get_property(np, "individual-sensing", NULL)) |
| 447 | hub->conf_data1 |= BIT(1); |
| 448 | } else if (of_get_property(np, "bus-powered", NULL)) { |
| 449 | hub->conf_data1 &= ~BIT(7); |
| 450 | |
| 451 | /* Disable Over-Current sense when bus-powered */ |
| 452 | hub->conf_data1 |= BIT(2); |
| 453 | } |
| 454 | |
| 455 | if (of_get_property(np, "disable-hi-speed", NULL)) |
| 456 | hub->conf_data1 |= BIT(5); |
| 457 | |
| 458 | if (of_get_property(np, "multi-tt", NULL)) |
| 459 | hub->conf_data1 |= BIT(4); |
| 460 | else if (of_get_property(np, "single-tt", NULL)) |
| 461 | hub->conf_data1 &= ~BIT(4); |
| 462 | |
| 463 | if (of_get_property(np, "disable-eop", NULL)) |
| 464 | hub->conf_data1 |= BIT(3); |
| 465 | |
| 466 | if (of_get_property(np, "individual-port-switching", NULL)) |
| 467 | hub->conf_data1 |= BIT(0); |
| 468 | else if (of_get_property(np, "ganged-port-switching", NULL)) |
| 469 | hub->conf_data1 &= ~BIT(0); |
| 470 | |
| 471 | hub->conf_data2 = USB251XB_DEF_CONFIG_DATA_2; |
| 472 | if (of_get_property(np, "dynamic-power-switching", NULL)) |
| 473 | hub->conf_data2 |= BIT(7); |
| 474 | |
Serge Semin | 287c2bb | 2017-10-22 23:38:10 +0300 | [diff] [blame] | 475 | if (!of_property_read_u32(np, "oc-delay-us", &property_u32)) { |
| 476 | if (property_u32 == 100) { |
Richard Leitner | 7f7d8ba | 2017-03-06 09:24:21 +0100 | [diff] [blame] | 477 | /* 100 us*/ |
| 478 | hub->conf_data2 &= ~BIT(5); |
| 479 | hub->conf_data2 &= ~BIT(4); |
Serge Semin | 287c2bb | 2017-10-22 23:38:10 +0300 | [diff] [blame] | 480 | } else if (property_u32 == 4000) { |
Richard Leitner | 7f7d8ba | 2017-03-06 09:24:21 +0100 | [diff] [blame] | 481 | /* 4 ms */ |
| 482 | hub->conf_data2 &= ~BIT(5); |
| 483 | hub->conf_data2 |= BIT(4); |
Serge Semin | 287c2bb | 2017-10-22 23:38:10 +0300 | [diff] [blame] | 484 | } else if (property_u32 == 16000) { |
Richard Leitner | 7f7d8ba | 2017-03-06 09:24:21 +0100 | [diff] [blame] | 485 | /* 16 ms */ |
| 486 | hub->conf_data2 |= BIT(5); |
| 487 | hub->conf_data2 |= BIT(4); |
| 488 | } else { |
| 489 | /* 8 ms (DEFAULT) */ |
| 490 | hub->conf_data2 |= BIT(5); |
| 491 | hub->conf_data2 &= ~BIT(4); |
| 492 | } |
Richard Leitner | 3ec72a2 | 2017-02-10 09:19:27 +0100 | [diff] [blame] | 493 | } |
| 494 | |
| 495 | if (of_get_property(np, "compound-device", NULL)) |
| 496 | hub->conf_data2 |= BIT(3); |
| 497 | |
| 498 | hub->conf_data3 = USB251XB_DEF_CONFIG_DATA_3; |
| 499 | if (of_get_property(np, "port-mapping-mode", NULL)) |
| 500 | hub->conf_data3 |= BIT(3); |
| 501 | |
Serge Semin | 2818e13 | 2017-10-22 23:38:09 +0300 | [diff] [blame] | 502 | if (data->led_support && of_get_property(np, "led-usb-mode", NULL)) |
| 503 | hub->conf_data3 &= ~BIT(1); |
| 504 | |
Richard Leitner | 3ec72a2 | 2017-02-10 09:19:27 +0100 | [diff] [blame] | 505 | if (of_get_property(np, "string-support", NULL)) |
| 506 | hub->conf_data3 |= BIT(0); |
| 507 | |
| 508 | hub->non_rem_dev = USB251XB_DEF_NON_REMOVABLE_DEVICES; |
Serge Semin | 73d31de | 2019-04-25 00:12:06 +0300 | [diff] [blame] | 509 | usb251xb_get_ports_field(hub, "non-removable-ports", data->port_cnt, |
Lucas Stach | 4849ee6 | 2019-07-19 10:44:07 +0200 | [diff] [blame] | 510 | true, &hub->non_rem_dev); |
Richard Leitner | 3ec72a2 | 2017-02-10 09:19:27 +0100 | [diff] [blame] | 511 | |
| 512 | hub->port_disable_sp = USB251XB_DEF_PORT_DISABLE_SELF; |
Serge Semin | 73d31de | 2019-04-25 00:12:06 +0300 | [diff] [blame] | 513 | usb251xb_get_ports_field(hub, "sp-disabled-ports", data->port_cnt, |
Lucas Stach | 4849ee6 | 2019-07-19 10:44:07 +0200 | [diff] [blame] | 514 | true, &hub->port_disable_sp); |
Richard Leitner | 3ec72a2 | 2017-02-10 09:19:27 +0100 | [diff] [blame] | 515 | |
| 516 | hub->port_disable_bp = USB251XB_DEF_PORT_DISABLE_BUS; |
Serge Semin | 73d31de | 2019-04-25 00:12:06 +0300 | [diff] [blame] | 517 | usb251xb_get_ports_field(hub, "bp-disabled-ports", data->port_cnt, |
Lucas Stach | 4849ee6 | 2019-07-19 10:44:07 +0200 | [diff] [blame] | 518 | true, &hub->port_disable_bp); |
Richard Leitner | 3ec72a2 | 2017-02-10 09:19:27 +0100 | [diff] [blame] | 519 | |
Serge Semin | 4ed466a | 2017-10-22 23:38:11 +0300 | [diff] [blame] | 520 | hub->max_power_sp = USB251XB_DEF_MAX_POWER_SELF; |
| 521 | if (!of_property_read_u32(np, "sp-max-total-current-microamp", |
| 522 | &property_u32)) |
| 523 | hub->max_power_sp = min_t(u8, property_u32 / 2000, 50); |
| 524 | |
| 525 | hub->max_power_bp = USB251XB_DEF_MAX_POWER_BUS; |
| 526 | if (!of_property_read_u32(np, "bp-max-total-current-microamp", |
| 527 | &property_u32)) |
| 528 | hub->max_power_bp = min_t(u8, property_u32 / 2000, 255); |
| 529 | |
| 530 | hub->max_current_sp = USB251XB_DEF_MAX_CURRENT_SELF; |
| 531 | if (!of_property_read_u32(np, "sp-max-removable-current-microamp", |
| 532 | &property_u32)) |
| 533 | hub->max_current_sp = min_t(u8, property_u32 / 2000, 50); |
| 534 | |
| 535 | hub->max_current_bp = USB251XB_DEF_MAX_CURRENT_BUS; |
| 536 | if (!of_property_read_u32(np, "bp-max-removable-current-microamp", |
| 537 | &property_u32)) |
| 538 | hub->max_current_bp = min_t(u8, property_u32 / 2000, 255); |
| 539 | |
Richard Leitner | 3ec72a2 | 2017-02-10 09:19:27 +0100 | [diff] [blame] | 540 | hub->power_on_time = USB251XB_DEF_POWER_ON_TIME; |
Serge Semin | 287c2bb | 2017-10-22 23:38:10 +0300 | [diff] [blame] | 541 | if (!of_property_read_u32(np, "power-on-time-ms", &property_u32)) |
| 542 | hub->power_on_time = min_t(u8, property_u32 / 2, 255); |
Richard Leitner | 3ec72a2 | 2017-02-10 09:19:27 +0100 | [diff] [blame] | 543 | |
| 544 | if (of_property_read_u16_array(np, "language-id", &hub->lang_id, 1)) |
| 545 | hub->lang_id = USB251XB_DEF_LANGUAGE_ID; |
| 546 | |
| 547 | cproperty_char = of_get_property(np, "manufacturer", NULL); |
| 548 | strlcpy(str, cproperty_char ? : USB251XB_DEF_MANUFACTURER_STRING, |
| 549 | sizeof(str)); |
| 550 | hub->manufacturer_len = strlen(str) & 0xFF; |
| 551 | memset(hub->manufacturer, 0, USB251XB_STRING_BUFSIZE); |
| 552 | len = min_t(size_t, USB251XB_STRING_BUFSIZE / 2, strlen(str)); |
| 553 | len = utf8s_to_utf16s(str, len, UTF16_LITTLE_ENDIAN, |
| 554 | (wchar_t *)hub->manufacturer, |
| 555 | USB251XB_STRING_BUFSIZE); |
| 556 | |
| 557 | cproperty_char = of_get_property(np, "product", NULL); |
| 558 | strlcpy(str, cproperty_char ? : data->product_str, sizeof(str)); |
| 559 | hub->product_len = strlen(str) & 0xFF; |
| 560 | memset(hub->product, 0, USB251XB_STRING_BUFSIZE); |
| 561 | len = min_t(size_t, USB251XB_STRING_BUFSIZE / 2, strlen(str)); |
| 562 | len = utf8s_to_utf16s(str, len, UTF16_LITTLE_ENDIAN, |
| 563 | (wchar_t *)hub->product, |
| 564 | USB251XB_STRING_BUFSIZE); |
| 565 | |
| 566 | cproperty_char = of_get_property(np, "serial", NULL); |
| 567 | strlcpy(str, cproperty_char ? : USB251XB_DEF_SERIAL_STRING, |
| 568 | sizeof(str)); |
| 569 | hub->serial_len = strlen(str) & 0xFF; |
| 570 | memset(hub->serial, 0, USB251XB_STRING_BUFSIZE); |
| 571 | len = min_t(size_t, USB251XB_STRING_BUFSIZE / 2, strlen(str)); |
| 572 | len = utf8s_to_utf16s(str, len, UTF16_LITTLE_ENDIAN, |
| 573 | (wchar_t *)hub->serial, |
| 574 | USB251XB_STRING_BUFSIZE); |
| 575 | |
Marco Felsch | 02a50b8 | 2019-01-16 15:20:37 +0100 | [diff] [blame] | 576 | /* |
| 577 | * The datasheet documents the register as 'Port Swap' but in real the |
| 578 | * register controls the USB DP/DM signal swapping for each port. |
| 579 | */ |
| 580 | hub->port_swap = USB251XB_DEF_PORT_SWAP; |
Serge Semin | 73d31de | 2019-04-25 00:12:06 +0300 | [diff] [blame] | 581 | usb251xb_get_ports_field(hub, "swap-dx-lanes", data->port_cnt, |
Lucas Stach | 4849ee6 | 2019-07-19 10:44:07 +0200 | [diff] [blame] | 582 | false, &hub->port_swap); |
Marco Felsch | 02a50b8 | 2019-01-16 15:20:37 +0100 | [diff] [blame] | 583 | |
Richard Leitner | 3ec72a2 | 2017-02-10 09:19:27 +0100 | [diff] [blame] | 584 | /* The following parameters are currently not exposed to devicetree, but |
| 585 | * may be as soon as needed. |
| 586 | */ |
Richard Leitner | 3ec72a2 | 2017-02-10 09:19:27 +0100 | [diff] [blame] | 587 | hub->bat_charge_en = USB251XB_DEF_BATTERY_CHARGING_ENABLE; |
| 588 | hub->boost_up = USB251XB_DEF_BOOST_UP; |
Serge Semin | ccdddc0 | 2017-10-22 23:38:07 +0300 | [diff] [blame] | 589 | hub->boost_57 = USB251XB_DEF_BOOST_57; |
| 590 | hub->boost_14 = USB251XB_DEF_BOOST_14; |
Richard Leitner | 3ec72a2 | 2017-02-10 09:19:27 +0100 | [diff] [blame] | 591 | hub->port_map12 = USB251XB_DEF_PORT_MAP_12; |
| 592 | hub->port_map34 = USB251XB_DEF_PORT_MAP_34; |
Serge Semin | db234b9 | 2017-10-22 23:38:06 +0300 | [diff] [blame] | 593 | hub->port_map56 = USB251XB_DEF_PORT_MAP_56; |
| 594 | hub->port_map7 = USB251XB_DEF_PORT_MAP_7; |
Richard Leitner | 3ec72a2 | 2017-02-10 09:19:27 +0100 | [diff] [blame] | 595 | |
| 596 | return 0; |
| 597 | } |
| 598 | |
| 599 | static const struct of_device_id usb251xb_of_match[] = { |
| 600 | { |
| 601 | .compatible = "microchip,usb2512b", |
| 602 | .data = &usb2512b_data, |
| 603 | }, { |
| 604 | .compatible = "microchip,usb2512bi", |
| 605 | .data = &usb2512bi_data, |
| 606 | }, { |
| 607 | .compatible = "microchip,usb2513b", |
| 608 | .data = &usb2513b_data, |
| 609 | }, { |
| 610 | .compatible = "microchip,usb2513bi", |
| 611 | .data = &usb2513bi_data, |
| 612 | }, { |
| 613 | .compatible = "microchip,usb2514b", |
| 614 | .data = &usb2514b_data, |
| 615 | }, { |
| 616 | .compatible = "microchip,usb2514bi", |
| 617 | .data = &usb2514bi_data, |
| 618 | }, { |
Serge Semin | 7fcf558 | 2017-10-22 23:38:04 +0300 | [diff] [blame] | 619 | .compatible = "microchip,usb2517", |
| 620 | .data = &usb2517_data, |
| 621 | }, { |
| 622 | .compatible = "microchip,usb2517i", |
| 623 | .data = &usb2517i_data, |
| 624 | }, { |
Richard Leitner | 3ec72a2 | 2017-02-10 09:19:27 +0100 | [diff] [blame] | 625 | /* sentinel */ |
| 626 | } |
| 627 | }; |
| 628 | MODULE_DEVICE_TABLE(of, usb251xb_of_match); |
| 629 | #else /* CONFIG_OF */ |
| 630 | static int usb251xb_get_ofdata(struct usb251xb *hub, |
| 631 | struct usb251xb_data *data) |
| 632 | { |
| 633 | return 0; |
| 634 | } |
| 635 | #endif /* CONFIG_OF */ |
| 636 | |
| 637 | static int usb251xb_probe(struct usb251xb *hub) |
| 638 | { |
| 639 | struct device *dev = hub->dev; |
| 640 | struct device_node *np = dev->of_node; |
| 641 | const struct of_device_id *of_id = of_match_device(usb251xb_of_match, |
| 642 | dev); |
| 643 | int err; |
| 644 | |
Aditya Pakki | 41f00e6 | 2019-03-20 10:27:11 -0500 | [diff] [blame] | 645 | if (np && of_id) { |
Richard Leitner | 3ec72a2 | 2017-02-10 09:19:27 +0100 | [diff] [blame] | 646 | err = usb251xb_get_ofdata(hub, |
| 647 | (struct usb251xb_data *)of_id->data); |
| 648 | if (err) { |
| 649 | dev_err(dev, "failed to get ofdata: %d\n", err); |
| 650 | return err; |
| 651 | } |
| 652 | } |
| 653 | |
Serge Semin | 6e3c8be | 2019-04-24 17:49:14 +0300 | [diff] [blame] | 654 | /* |
| 655 | * usb251x SMBus-slave SCL lane is muxed with CFG_SEL0 pin. So if anyone |
| 656 | * tries to work with the bus at the moment the hub reset is released, |
| 657 | * it may cause an invalid config being latched by usb251x. Particularly |
| 658 | * one of the config modes makes the hub loading a default registers |
| 659 | * value without SMBus-slave interface activation. If the hub |
| 660 | * accidentally gets this mode, this will cause the driver SMBus- |
| 661 | * functions failure. Normally we could just lock the SMBus-segment the |
| 662 | * hub i2c-interface resides for the device-specific reset timing. But |
| 663 | * the GPIO controller, which is used to handle the hub reset, might be |
| 664 | * placed at the same i2c-bus segment. In this case an error should be |
| 665 | * returned since we can't safely use the GPIO controller to clear the |
| 666 | * reset state (it may affect the hub configuration) and we can't lock |
| 667 | * the i2c-bus segment (it will cause a deadlock). |
| 668 | */ |
| 669 | err = usb251x_check_gpio_chip(hub); |
| 670 | if (err) |
| 671 | return err; |
| 672 | |
Marco Felsch | 4d7201c | 2019-09-17 16:44:47 +0200 | [diff] [blame] | 673 | err = regulator_enable(hub->vdd); |
| 674 | if (err) |
| 675 | return err; |
| 676 | |
Richard Leitner | 3ec72a2 | 2017-02-10 09:19:27 +0100 | [diff] [blame] | 677 | err = usb251xb_connect(hub); |
| 678 | if (err) { |
| 679 | dev_err(dev, "Failed to connect hub (%d)\n", err); |
| 680 | return err; |
| 681 | } |
| 682 | |
| 683 | dev_info(dev, "Hub probed successfully\n"); |
| 684 | |
| 685 | return 0; |
| 686 | } |
| 687 | |
| 688 | static int usb251xb_i2c_probe(struct i2c_client *i2c, |
| 689 | const struct i2c_device_id *id) |
| 690 | { |
| 691 | struct usb251xb *hub; |
| 692 | |
| 693 | hub = devm_kzalloc(&i2c->dev, sizeof(struct usb251xb), GFP_KERNEL); |
| 694 | if (!hub) |
| 695 | return -ENOMEM; |
| 696 | |
| 697 | i2c_set_clientdata(i2c, hub); |
| 698 | hub->dev = &i2c->dev; |
| 699 | hub->i2c = i2c; |
| 700 | |
| 701 | return usb251xb_probe(hub); |
| 702 | } |
| 703 | |
| 704 | static const struct i2c_device_id usb251xb_id[] = { |
| 705 | { "usb2512b", 0 }, |
| 706 | { "usb2512bi", 0 }, |
| 707 | { "usb2513b", 0 }, |
| 708 | { "usb2513bi", 0 }, |
| 709 | { "usb2514b", 0 }, |
| 710 | { "usb2514bi", 0 }, |
Serge Semin | 7fcf558 | 2017-10-22 23:38:04 +0300 | [diff] [blame] | 711 | { "usb2517", 0 }, |
| 712 | { "usb2517i", 0 }, |
Richard Leitner | 3ec72a2 | 2017-02-10 09:19:27 +0100 | [diff] [blame] | 713 | { /* sentinel */ } |
| 714 | }; |
| 715 | MODULE_DEVICE_TABLE(i2c, usb251xb_id); |
| 716 | |
| 717 | static struct i2c_driver usb251xb_i2c_driver = { |
| 718 | .driver = { |
| 719 | .name = DRIVER_NAME, |
| 720 | .of_match_table = of_match_ptr(usb251xb_of_match), |
| 721 | }, |
| 722 | .probe = usb251xb_i2c_probe, |
| 723 | .id_table = usb251xb_id, |
| 724 | }; |
| 725 | |
| 726 | module_i2c_driver(usb251xb_i2c_driver); |
| 727 | |
| 728 | MODULE_AUTHOR("Richard Leitner <richard.leitner@skidata.com>"); |
Serge Semin | 7fcf558 | 2017-10-22 23:38:04 +0300 | [diff] [blame] | 729 | MODULE_DESCRIPTION("USB251x/xBi USB 2.0 Hub Controller Driver"); |
Richard Leitner | 3ec72a2 | 2017-02-10 09:19:27 +0100 | [diff] [blame] | 730 | MODULE_LICENSE("GPL"); |