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