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