Richard Leitner | 3ec72a2 | 2017-02-10 09:19:27 +0100 | [diff] [blame] | 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 | |
| 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 Semin | 7fcf558 | 2017-10-22 23:38:04 +0300 | [diff] [blame^] | 41 | #define USB251XB_DEF_PRODUCT_ID_17 0x2517 /* USB2517/17i */ |
Richard Leitner | 3ec72a2 | 2017-02-10 09:19:27 +0100 | [diff] [blame] | 42 | |
| 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 Semin | 7fcf558 | 2017-10-22 23:38:04 +0300 | [diff] [blame^] | 86 | #define USB251XB_DEF_PRODUCT_STRING "USB251xB/xBi/7i" |
Richard Leitner | 3ec72a2 | 2017-02-10 09:19:27 +0100 | [diff] [blame] | 87 | |
| 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 Leitner | 3ec72a2 | 2017-02-10 09:19:27 +0100 | [diff] [blame] | 118 | |
| 119 | struct 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 | |
| 154 | struct usb251xb_data { |
| 155 | u16 product_id; |
| 156 | char product_str[USB251XB_STRING_BUFSIZE / 2]; /* ASCII string */ |
| 157 | }; |
| 158 | |
| 159 | static const struct usb251xb_data usb2512b_data = { |
| 160 | .product_id = 0x2512, |
| 161 | .product_str = "USB2512B", |
| 162 | }; |
| 163 | |
| 164 | static const struct usb251xb_data usb2512bi_data = { |
| 165 | .product_id = 0x2512, |
| 166 | .product_str = "USB2512Bi", |
| 167 | }; |
| 168 | |
| 169 | static const struct usb251xb_data usb2513b_data = { |
| 170 | .product_id = 0x2513, |
| 171 | .product_str = "USB2513B", |
| 172 | }; |
| 173 | |
| 174 | static const struct usb251xb_data usb2513bi_data = { |
| 175 | .product_id = 0x2513, |
| 176 | .product_str = "USB2513Bi", |
| 177 | }; |
| 178 | |
| 179 | static const struct usb251xb_data usb2514b_data = { |
| 180 | .product_id = 0x2514, |
| 181 | .product_str = "USB2514B", |
| 182 | }; |
| 183 | |
| 184 | static const struct usb251xb_data usb2514bi_data = { |
| 185 | .product_id = 0x2514, |
| 186 | .product_str = "USB2514Bi", |
| 187 | }; |
| 188 | |
Serge Semin | 7fcf558 | 2017-10-22 23:38:04 +0300 | [diff] [blame^] | 189 | static const struct usb251xb_data usb2517_data = { |
| 190 | .product_id = 0x2517, |
| 191 | .product_str = "USB2517", |
| 192 | }; |
| 193 | |
| 194 | static const struct usb251xb_data usb2517i_data = { |
| 195 | .product_id = 0x2517, |
| 196 | .product_str = "USB2517i", |
| 197 | }; |
| 198 | |
Richard Leitner | 3ec72a2 | 2017-02-10 09:19:27 +0100 | [diff] [blame] | 199 | static void usb251xb_reset(struct usb251xb *hub, int state) |
| 200 | { |
| 201 | if (!gpio_is_valid(hub->gpio_reset)) |
| 202 | return; |
| 203 | |
| 204 | gpio_set_value_cansleep(hub->gpio_reset, state); |
| 205 | |
| 206 | /* wait for hub recovery/stabilization */ |
| 207 | if (state) |
| 208 | usleep_range(500, 750); /* >=500us at power on */ |
| 209 | else |
| 210 | usleep_range(1, 10); /* >=1us at power down */ |
| 211 | } |
| 212 | |
| 213 | static int usb251xb_connect(struct usb251xb *hub) |
| 214 | { |
| 215 | struct device *dev = hub->dev; |
| 216 | int err, i; |
| 217 | char i2c_wb[USB251XB_I2C_REG_SZ]; |
| 218 | |
| 219 | memset(i2c_wb, 0, USB251XB_I2C_REG_SZ); |
| 220 | |
| 221 | if (hub->skip_config) { |
| 222 | dev_info(dev, "Skip hub configuration, only attach.\n"); |
| 223 | i2c_wb[0] = 0x01; |
| 224 | i2c_wb[1] = USB251XB_STATUS_COMMAND_ATTACH; |
| 225 | |
| 226 | usb251xb_reset(hub, 1); |
| 227 | |
| 228 | err = i2c_smbus_write_i2c_block_data(hub->i2c, |
| 229 | USB251XB_ADDR_STATUS_COMMAND, 2, i2c_wb); |
| 230 | if (err) { |
| 231 | dev_err(dev, "attaching hub failed: %d\n", err); |
| 232 | return err; |
| 233 | } |
| 234 | return 0; |
| 235 | } |
| 236 | |
| 237 | i2c_wb[USB251XB_ADDR_VENDOR_ID_MSB] = (hub->vendor_id >> 8) & 0xFF; |
| 238 | i2c_wb[USB251XB_ADDR_VENDOR_ID_LSB] = hub->vendor_id & 0xFF; |
| 239 | i2c_wb[USB251XB_ADDR_PRODUCT_ID_MSB] = (hub->product_id >> 8) & 0xFF; |
| 240 | i2c_wb[USB251XB_ADDR_PRODUCT_ID_LSB] = hub->product_id & 0xFF; |
| 241 | i2c_wb[USB251XB_ADDR_DEVICE_ID_MSB] = (hub->device_id >> 8) & 0xFF; |
| 242 | i2c_wb[USB251XB_ADDR_DEVICE_ID_LSB] = hub->device_id & 0xFF; |
| 243 | i2c_wb[USB251XB_ADDR_CONFIG_DATA_1] = hub->conf_data1; |
| 244 | i2c_wb[USB251XB_ADDR_CONFIG_DATA_2] = hub->conf_data2; |
| 245 | i2c_wb[USB251XB_ADDR_CONFIG_DATA_3] = hub->conf_data3; |
| 246 | i2c_wb[USB251XB_ADDR_NON_REMOVABLE_DEVICES] = hub->non_rem_dev; |
| 247 | i2c_wb[USB251XB_ADDR_PORT_DISABLE_SELF] = hub->port_disable_sp; |
| 248 | i2c_wb[USB251XB_ADDR_PORT_DISABLE_BUS] = hub->port_disable_bp; |
| 249 | i2c_wb[USB251XB_ADDR_MAX_POWER_SELF] = hub->max_power_sp; |
| 250 | i2c_wb[USB251XB_ADDR_MAX_POWER_BUS] = hub->max_power_bp; |
| 251 | i2c_wb[USB251XB_ADDR_MAX_CURRENT_SELF] = hub->max_current_sp; |
| 252 | i2c_wb[USB251XB_ADDR_MAX_CURRENT_BUS] = hub->max_current_bp; |
| 253 | i2c_wb[USB251XB_ADDR_POWER_ON_TIME] = hub->power_on_time; |
| 254 | i2c_wb[USB251XB_ADDR_LANGUAGE_ID_HIGH] = (hub->lang_id >> 8) & 0xFF; |
| 255 | i2c_wb[USB251XB_ADDR_LANGUAGE_ID_LOW] = hub->lang_id & 0xFF; |
| 256 | i2c_wb[USB251XB_ADDR_MANUFACTURER_STRING_LEN] = hub->manufacturer_len; |
| 257 | i2c_wb[USB251XB_ADDR_PRODUCT_STRING_LEN] = hub->product_len; |
| 258 | i2c_wb[USB251XB_ADDR_SERIAL_STRING_LEN] = hub->serial_len; |
| 259 | memcpy(&i2c_wb[USB251XB_ADDR_MANUFACTURER_STRING], hub->manufacturer, |
| 260 | USB251XB_STRING_BUFSIZE); |
| 261 | memcpy(&i2c_wb[USB251XB_ADDR_SERIAL_STRING], hub->serial, |
| 262 | USB251XB_STRING_BUFSIZE); |
| 263 | memcpy(&i2c_wb[USB251XB_ADDR_PRODUCT_STRING], hub->product, |
| 264 | USB251XB_STRING_BUFSIZE); |
| 265 | i2c_wb[USB251XB_ADDR_BATTERY_CHARGING_ENABLE] = hub->bat_charge_en; |
| 266 | i2c_wb[USB251XB_ADDR_BOOST_UP] = hub->boost_up; |
| 267 | i2c_wb[USB251XB_ADDR_BOOST_X] = hub->boost_x; |
| 268 | i2c_wb[USB251XB_ADDR_PORT_SWAP] = hub->port_swap; |
| 269 | i2c_wb[USB251XB_ADDR_PORT_MAP_12] = hub->port_map12; |
| 270 | i2c_wb[USB251XB_ADDR_PORT_MAP_34] = hub->port_map34; |
| 271 | i2c_wb[USB251XB_ADDR_STATUS_COMMAND] = USB251XB_STATUS_COMMAND_ATTACH; |
| 272 | |
| 273 | usb251xb_reset(hub, 1); |
| 274 | |
| 275 | /* write registers */ |
| 276 | for (i = 0; i < (USB251XB_I2C_REG_SZ / USB251XB_I2C_WRITE_SZ); i++) { |
| 277 | int offset = i * USB251XB_I2C_WRITE_SZ; |
| 278 | char wbuf[USB251XB_I2C_WRITE_SZ + 1]; |
| 279 | |
| 280 | /* The first data byte transferred tells the hub how many data |
| 281 | * bytes will follow (byte count). |
| 282 | */ |
| 283 | wbuf[0] = USB251XB_I2C_WRITE_SZ; |
| 284 | memcpy(&wbuf[1], &i2c_wb[offset], USB251XB_I2C_WRITE_SZ); |
| 285 | |
| 286 | dev_dbg(dev, "writing %d byte block %d to 0x%02X\n", |
| 287 | USB251XB_I2C_WRITE_SZ, i, offset); |
| 288 | |
| 289 | err = i2c_smbus_write_i2c_block_data(hub->i2c, offset, |
| 290 | USB251XB_I2C_WRITE_SZ + 1, |
| 291 | wbuf); |
| 292 | if (err) |
| 293 | goto out_err; |
| 294 | } |
| 295 | |
| 296 | dev_info(dev, "Hub configuration was successful.\n"); |
| 297 | return 0; |
| 298 | |
| 299 | out_err: |
| 300 | dev_err(dev, "configuring block %d failed: %d\n", i, err); |
| 301 | return err; |
| 302 | } |
| 303 | |
| 304 | #ifdef CONFIG_OF |
| 305 | static int usb251xb_get_ofdata(struct usb251xb *hub, |
| 306 | struct usb251xb_data *data) |
| 307 | { |
| 308 | struct device *dev = hub->dev; |
| 309 | struct device_node *np = dev->of_node; |
| 310 | int len, err, i; |
| 311 | u32 *property_u32 = NULL; |
| 312 | const u32 *cproperty_u32; |
| 313 | const char *cproperty_char; |
| 314 | char str[USB251XB_STRING_BUFSIZE / 2]; |
| 315 | |
| 316 | if (!np) { |
| 317 | dev_err(dev, "failed to get ofdata\n"); |
| 318 | return -ENODEV; |
| 319 | } |
| 320 | |
| 321 | if (of_get_property(np, "skip-config", NULL)) |
| 322 | hub->skip_config = 1; |
| 323 | else |
| 324 | hub->skip_config = 0; |
| 325 | |
| 326 | hub->gpio_reset = of_get_named_gpio(np, "reset-gpios", 0); |
| 327 | if (hub->gpio_reset == -EPROBE_DEFER) |
| 328 | return -EPROBE_DEFER; |
| 329 | if (gpio_is_valid(hub->gpio_reset)) { |
| 330 | err = devm_gpio_request_one(dev, hub->gpio_reset, |
| 331 | GPIOF_OUT_INIT_LOW, |
| 332 | "usb251xb reset"); |
| 333 | if (err) { |
| 334 | dev_err(dev, |
| 335 | "unable to request GPIO %d as reset pin (%d)\n", |
| 336 | hub->gpio_reset, err); |
| 337 | return err; |
| 338 | } |
| 339 | } |
| 340 | |
| 341 | if (of_property_read_u16_array(np, "vendor-id", &hub->vendor_id, 1)) |
| 342 | hub->vendor_id = USB251XB_DEF_VENDOR_ID; |
| 343 | |
| 344 | if (of_property_read_u16_array(np, "product-id", |
| 345 | &hub->product_id, 1)) |
| 346 | hub->product_id = data->product_id; |
| 347 | |
| 348 | if (of_property_read_u16_array(np, "device-id", &hub->device_id, 1)) |
| 349 | hub->device_id = USB251XB_DEF_DEVICE_ID; |
| 350 | |
| 351 | hub->conf_data1 = USB251XB_DEF_CONFIG_DATA_1; |
| 352 | if (of_get_property(np, "self-powered", NULL)) { |
| 353 | hub->conf_data1 |= BIT(7); |
| 354 | |
| 355 | /* Configure Over-Current sens when self-powered */ |
| 356 | hub->conf_data1 &= ~BIT(2); |
| 357 | if (of_get_property(np, "ganged-sensing", NULL)) |
| 358 | hub->conf_data1 &= ~BIT(1); |
| 359 | else if (of_get_property(np, "individual-sensing", NULL)) |
| 360 | hub->conf_data1 |= BIT(1); |
| 361 | } else if (of_get_property(np, "bus-powered", NULL)) { |
| 362 | hub->conf_data1 &= ~BIT(7); |
| 363 | |
| 364 | /* Disable Over-Current sense when bus-powered */ |
| 365 | hub->conf_data1 |= BIT(2); |
| 366 | } |
| 367 | |
| 368 | if (of_get_property(np, "disable-hi-speed", NULL)) |
| 369 | hub->conf_data1 |= BIT(5); |
| 370 | |
| 371 | if (of_get_property(np, "multi-tt", NULL)) |
| 372 | hub->conf_data1 |= BIT(4); |
| 373 | else if (of_get_property(np, "single-tt", NULL)) |
| 374 | hub->conf_data1 &= ~BIT(4); |
| 375 | |
| 376 | if (of_get_property(np, "disable-eop", NULL)) |
| 377 | hub->conf_data1 |= BIT(3); |
| 378 | |
| 379 | if (of_get_property(np, "individual-port-switching", NULL)) |
| 380 | hub->conf_data1 |= BIT(0); |
| 381 | else if (of_get_property(np, "ganged-port-switching", NULL)) |
| 382 | hub->conf_data1 &= ~BIT(0); |
| 383 | |
| 384 | hub->conf_data2 = USB251XB_DEF_CONFIG_DATA_2; |
| 385 | if (of_get_property(np, "dynamic-power-switching", NULL)) |
| 386 | hub->conf_data2 |= BIT(7); |
| 387 | |
Richard Leitner | 7f7d8ba | 2017-03-06 09:24:21 +0100 | [diff] [blame] | 388 | if (!of_property_read_u32(np, "oc-delay-us", property_u32)) { |
| 389 | if (*property_u32 == 100) { |
| 390 | /* 100 us*/ |
| 391 | hub->conf_data2 &= ~BIT(5); |
| 392 | hub->conf_data2 &= ~BIT(4); |
| 393 | } else if (*property_u32 == 4000) { |
| 394 | /* 4 ms */ |
| 395 | hub->conf_data2 &= ~BIT(5); |
| 396 | hub->conf_data2 |= BIT(4); |
| 397 | } else if (*property_u32 == 16000) { |
| 398 | /* 16 ms */ |
| 399 | hub->conf_data2 |= BIT(5); |
| 400 | hub->conf_data2 |= BIT(4); |
| 401 | } else { |
| 402 | /* 8 ms (DEFAULT) */ |
| 403 | hub->conf_data2 |= BIT(5); |
| 404 | hub->conf_data2 &= ~BIT(4); |
| 405 | } |
Richard Leitner | 3ec72a2 | 2017-02-10 09:19:27 +0100 | [diff] [blame] | 406 | } |
| 407 | |
| 408 | if (of_get_property(np, "compound-device", NULL)) |
| 409 | hub->conf_data2 |= BIT(3); |
| 410 | |
| 411 | hub->conf_data3 = USB251XB_DEF_CONFIG_DATA_3; |
| 412 | if (of_get_property(np, "port-mapping-mode", NULL)) |
| 413 | hub->conf_data3 |= BIT(3); |
| 414 | |
| 415 | if (of_get_property(np, "string-support", NULL)) |
| 416 | hub->conf_data3 |= BIT(0); |
| 417 | |
| 418 | hub->non_rem_dev = USB251XB_DEF_NON_REMOVABLE_DEVICES; |
| 419 | cproperty_u32 = of_get_property(np, "non-removable-ports", &len); |
| 420 | if (cproperty_u32 && (len / sizeof(u32)) > 0) { |
| 421 | for (i = 0; i < len / sizeof(u32); i++) { |
| 422 | u32 port = be32_to_cpu(cproperty_u32[i]); |
| 423 | |
| 424 | if ((port >= 1) && (port <= 4)) |
| 425 | hub->non_rem_dev |= BIT(port); |
| 426 | } |
| 427 | } |
| 428 | |
| 429 | hub->port_disable_sp = USB251XB_DEF_PORT_DISABLE_SELF; |
| 430 | cproperty_u32 = of_get_property(np, "sp-disabled-ports", &len); |
| 431 | if (cproperty_u32 && (len / sizeof(u32)) > 0) { |
| 432 | for (i = 0; i < len / sizeof(u32); i++) { |
| 433 | u32 port = be32_to_cpu(cproperty_u32[i]); |
| 434 | |
| 435 | if ((port >= 1) && (port <= 4)) |
| 436 | hub->port_disable_sp |= BIT(port); |
| 437 | } |
| 438 | } |
| 439 | |
| 440 | hub->port_disable_bp = USB251XB_DEF_PORT_DISABLE_BUS; |
| 441 | cproperty_u32 = of_get_property(np, "bp-disabled-ports", &len); |
| 442 | if (cproperty_u32 && (len / sizeof(u32)) > 0) { |
| 443 | for (i = 0; i < len / sizeof(u32); i++) { |
| 444 | u32 port = be32_to_cpu(cproperty_u32[i]); |
| 445 | |
| 446 | if ((port >= 1) && (port <= 4)) |
| 447 | hub->port_disable_bp |= BIT(port); |
| 448 | } |
| 449 | } |
| 450 | |
Richard Leitner | 3ec72a2 | 2017-02-10 09:19:27 +0100 | [diff] [blame] | 451 | hub->power_on_time = USB251XB_DEF_POWER_ON_TIME; |
Richard Leitner | 7f7d8ba | 2017-03-06 09:24:21 +0100 | [diff] [blame] | 452 | if (!of_property_read_u32(np, "power-on-time-ms", property_u32)) |
| 453 | hub->power_on_time = min_t(u8, *property_u32 / 2, 255); |
Richard Leitner | 3ec72a2 | 2017-02-10 09:19:27 +0100 | [diff] [blame] | 454 | |
| 455 | if (of_property_read_u16_array(np, "language-id", &hub->lang_id, 1)) |
| 456 | hub->lang_id = USB251XB_DEF_LANGUAGE_ID; |
| 457 | |
| 458 | cproperty_char = of_get_property(np, "manufacturer", NULL); |
| 459 | strlcpy(str, cproperty_char ? : USB251XB_DEF_MANUFACTURER_STRING, |
| 460 | sizeof(str)); |
| 461 | hub->manufacturer_len = strlen(str) & 0xFF; |
| 462 | memset(hub->manufacturer, 0, USB251XB_STRING_BUFSIZE); |
| 463 | len = min_t(size_t, USB251XB_STRING_BUFSIZE / 2, strlen(str)); |
| 464 | len = utf8s_to_utf16s(str, len, UTF16_LITTLE_ENDIAN, |
| 465 | (wchar_t *)hub->manufacturer, |
| 466 | USB251XB_STRING_BUFSIZE); |
| 467 | |
| 468 | cproperty_char = of_get_property(np, "product", NULL); |
| 469 | strlcpy(str, cproperty_char ? : data->product_str, sizeof(str)); |
| 470 | hub->product_len = strlen(str) & 0xFF; |
| 471 | memset(hub->product, 0, USB251XB_STRING_BUFSIZE); |
| 472 | len = min_t(size_t, USB251XB_STRING_BUFSIZE / 2, strlen(str)); |
| 473 | len = utf8s_to_utf16s(str, len, UTF16_LITTLE_ENDIAN, |
| 474 | (wchar_t *)hub->product, |
| 475 | USB251XB_STRING_BUFSIZE); |
| 476 | |
| 477 | cproperty_char = of_get_property(np, "serial", NULL); |
| 478 | strlcpy(str, cproperty_char ? : USB251XB_DEF_SERIAL_STRING, |
| 479 | sizeof(str)); |
| 480 | hub->serial_len = strlen(str) & 0xFF; |
| 481 | memset(hub->serial, 0, USB251XB_STRING_BUFSIZE); |
| 482 | len = min_t(size_t, USB251XB_STRING_BUFSIZE / 2, strlen(str)); |
| 483 | len = utf8s_to_utf16s(str, len, UTF16_LITTLE_ENDIAN, |
| 484 | (wchar_t *)hub->serial, |
| 485 | USB251XB_STRING_BUFSIZE); |
| 486 | |
| 487 | /* The following parameters are currently not exposed to devicetree, but |
| 488 | * may be as soon as needed. |
| 489 | */ |
Richard Leitner | cfa47af | 2017-03-06 09:24:20 +0100 | [diff] [blame] | 490 | hub->max_power_sp = USB251XB_DEF_MAX_POWER_SELF; |
| 491 | hub->max_power_bp = USB251XB_DEF_MAX_POWER_BUS; |
| 492 | hub->max_current_sp = USB251XB_DEF_MAX_CURRENT_SELF; |
| 493 | hub->max_current_bp = USB251XB_DEF_MAX_CURRENT_BUS; |
Richard Leitner | 3ec72a2 | 2017-02-10 09:19:27 +0100 | [diff] [blame] | 494 | hub->bat_charge_en = USB251XB_DEF_BATTERY_CHARGING_ENABLE; |
| 495 | hub->boost_up = USB251XB_DEF_BOOST_UP; |
| 496 | hub->boost_x = USB251XB_DEF_BOOST_X; |
| 497 | hub->port_swap = USB251XB_DEF_PORT_SWAP; |
| 498 | hub->port_map12 = USB251XB_DEF_PORT_MAP_12; |
| 499 | hub->port_map34 = USB251XB_DEF_PORT_MAP_34; |
| 500 | |
| 501 | return 0; |
| 502 | } |
| 503 | |
| 504 | static const struct of_device_id usb251xb_of_match[] = { |
| 505 | { |
| 506 | .compatible = "microchip,usb2512b", |
| 507 | .data = &usb2512b_data, |
| 508 | }, { |
| 509 | .compatible = "microchip,usb2512bi", |
| 510 | .data = &usb2512bi_data, |
| 511 | }, { |
| 512 | .compatible = "microchip,usb2513b", |
| 513 | .data = &usb2513b_data, |
| 514 | }, { |
| 515 | .compatible = "microchip,usb2513bi", |
| 516 | .data = &usb2513bi_data, |
| 517 | }, { |
| 518 | .compatible = "microchip,usb2514b", |
| 519 | .data = &usb2514b_data, |
| 520 | }, { |
| 521 | .compatible = "microchip,usb2514bi", |
| 522 | .data = &usb2514bi_data, |
| 523 | }, { |
Serge Semin | 7fcf558 | 2017-10-22 23:38:04 +0300 | [diff] [blame^] | 524 | .compatible = "microchip,usb2517", |
| 525 | .data = &usb2517_data, |
| 526 | }, { |
| 527 | .compatible = "microchip,usb2517i", |
| 528 | .data = &usb2517i_data, |
| 529 | }, { |
Richard Leitner | 3ec72a2 | 2017-02-10 09:19:27 +0100 | [diff] [blame] | 530 | /* sentinel */ |
| 531 | } |
| 532 | }; |
| 533 | MODULE_DEVICE_TABLE(of, usb251xb_of_match); |
| 534 | #else /* CONFIG_OF */ |
| 535 | static int usb251xb_get_ofdata(struct usb251xb *hub, |
| 536 | struct usb251xb_data *data) |
| 537 | { |
| 538 | return 0; |
| 539 | } |
| 540 | #endif /* CONFIG_OF */ |
| 541 | |
| 542 | static int usb251xb_probe(struct usb251xb *hub) |
| 543 | { |
| 544 | struct device *dev = hub->dev; |
| 545 | struct device_node *np = dev->of_node; |
| 546 | const struct of_device_id *of_id = of_match_device(usb251xb_of_match, |
| 547 | dev); |
| 548 | int err; |
| 549 | |
| 550 | if (np) { |
| 551 | err = usb251xb_get_ofdata(hub, |
| 552 | (struct usb251xb_data *)of_id->data); |
| 553 | if (err) { |
| 554 | dev_err(dev, "failed to get ofdata: %d\n", err); |
| 555 | return err; |
| 556 | } |
| 557 | } |
| 558 | |
| 559 | err = usb251xb_connect(hub); |
| 560 | if (err) { |
| 561 | dev_err(dev, "Failed to connect hub (%d)\n", err); |
| 562 | return err; |
| 563 | } |
| 564 | |
| 565 | dev_info(dev, "Hub probed successfully\n"); |
| 566 | |
| 567 | return 0; |
| 568 | } |
| 569 | |
| 570 | static int usb251xb_i2c_probe(struct i2c_client *i2c, |
| 571 | const struct i2c_device_id *id) |
| 572 | { |
| 573 | struct usb251xb *hub; |
| 574 | |
| 575 | hub = devm_kzalloc(&i2c->dev, sizeof(struct usb251xb), GFP_KERNEL); |
| 576 | if (!hub) |
| 577 | return -ENOMEM; |
| 578 | |
| 579 | i2c_set_clientdata(i2c, hub); |
| 580 | hub->dev = &i2c->dev; |
| 581 | hub->i2c = i2c; |
| 582 | |
| 583 | return usb251xb_probe(hub); |
| 584 | } |
| 585 | |
| 586 | static const struct i2c_device_id usb251xb_id[] = { |
| 587 | { "usb2512b", 0 }, |
| 588 | { "usb2512bi", 0 }, |
| 589 | { "usb2513b", 0 }, |
| 590 | { "usb2513bi", 0 }, |
| 591 | { "usb2514b", 0 }, |
| 592 | { "usb2514bi", 0 }, |
Serge Semin | 7fcf558 | 2017-10-22 23:38:04 +0300 | [diff] [blame^] | 593 | { "usb2517", 0 }, |
| 594 | { "usb2517i", 0 }, |
Richard Leitner | 3ec72a2 | 2017-02-10 09:19:27 +0100 | [diff] [blame] | 595 | { /* sentinel */ } |
| 596 | }; |
| 597 | MODULE_DEVICE_TABLE(i2c, usb251xb_id); |
| 598 | |
| 599 | static struct i2c_driver usb251xb_i2c_driver = { |
| 600 | .driver = { |
| 601 | .name = DRIVER_NAME, |
| 602 | .of_match_table = of_match_ptr(usb251xb_of_match), |
| 603 | }, |
| 604 | .probe = usb251xb_i2c_probe, |
| 605 | .id_table = usb251xb_id, |
| 606 | }; |
| 607 | |
| 608 | module_i2c_driver(usb251xb_i2c_driver); |
| 609 | |
| 610 | MODULE_AUTHOR("Richard Leitner <richard.leitner@skidata.com>"); |
Serge Semin | 7fcf558 | 2017-10-22 23:38:04 +0300 | [diff] [blame^] | 611 | MODULE_DESCRIPTION("USB251x/xBi USB 2.0 Hub Controller Driver"); |
Richard Leitner | 3ec72a2 | 2017-02-10 09:19:27 +0100 | [diff] [blame] | 612 | MODULE_LICENSE("GPL"); |