Dongjin Kim | 6a099c6 | 2012-12-08 05:18:44 +0900 | [diff] [blame] | 1 | /* |
| 2 | * Driver for SMSC USB3503 USB 2.0 hub controller driver |
| 3 | * |
| 4 | * Copyright (c) 2012-2013 Dongjin Kim (tobetter@gmail.com) |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License as published by |
| 8 | * the Free Software Foundation; either version 2 of the License, or |
| 9 | * (at your option) any later version. |
| 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | * GNU General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License |
| 17 | * along with this program; if not, write to the Free Software |
| 18 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 19 | */ |
| 20 | |
| 21 | #include <linux/i2c.h> |
| 22 | #include <linux/gpio.h> |
| 23 | #include <linux/delay.h> |
| 24 | #include <linux/slab.h> |
| 25 | #include <linux/module.h> |
Dongjin Kim | eab8050 | 2013-01-24 02:47:10 +0900 | [diff] [blame] | 26 | #include <linux/of_gpio.h> |
Dongjin Kim | 6a099c6 | 2012-12-08 05:18:44 +0900 | [diff] [blame] | 27 | #include <linux/platform_device.h> |
| 28 | #include <linux/platform_data/usb3503.h> |
| 29 | |
| 30 | #define USB3503_VIDL 0x00 |
| 31 | #define USB3503_VIDM 0x01 |
| 32 | #define USB3503_PIDL 0x02 |
| 33 | #define USB3503_PIDM 0x03 |
| 34 | #define USB3503_DIDL 0x04 |
| 35 | #define USB3503_DIDM 0x05 |
| 36 | |
| 37 | #define USB3503_CFG1 0x06 |
| 38 | #define USB3503_SELF_BUS_PWR (1 << 7) |
| 39 | |
| 40 | #define USB3503_CFG2 0x07 |
| 41 | #define USB3503_CFG3 0x08 |
| 42 | #define USB3503_NRD 0x09 |
| 43 | |
| 44 | #define USB3503_PDS 0x0a |
| 45 | #define USB3503_PORT1 (1 << 1) |
| 46 | #define USB3503_PORT2 (1 << 2) |
| 47 | #define USB3503_PORT3 (1 << 3) |
| 48 | |
| 49 | #define USB3503_SP_ILOCK 0xe7 |
| 50 | #define USB3503_SPILOCK_CONNECT (1 << 1) |
| 51 | #define USB3503_SPILOCK_CONFIG (1 << 0) |
| 52 | |
| 53 | #define USB3503_CFGP 0xee |
| 54 | #define USB3503_CLKSUSP (1 << 7) |
| 55 | |
| 56 | struct usb3503 { |
| 57 | enum usb3503_mode mode; |
| 58 | struct i2c_client *client; |
| 59 | int gpio_intn; |
| 60 | int gpio_reset; |
| 61 | int gpio_connect; |
| 62 | }; |
| 63 | |
| 64 | static int usb3503_write_register(struct i2c_client *client, |
| 65 | char reg, char data) |
| 66 | { |
| 67 | return i2c_smbus_write_byte_data(client, reg, data); |
| 68 | } |
| 69 | |
| 70 | static int usb3503_read_register(struct i2c_client *client, char reg) |
| 71 | { |
| 72 | return i2c_smbus_read_byte_data(client, reg); |
| 73 | } |
| 74 | |
| 75 | static int usb3503_set_bits(struct i2c_client *client, char reg, char req) |
| 76 | { |
| 77 | int err; |
| 78 | |
| 79 | err = usb3503_read_register(client, reg); |
| 80 | if (err < 0) |
| 81 | return err; |
| 82 | |
| 83 | err = usb3503_write_register(client, reg, err | req); |
| 84 | if (err < 0) |
| 85 | return err; |
| 86 | |
| 87 | return 0; |
| 88 | } |
| 89 | |
| 90 | static int usb3503_clear_bits(struct i2c_client *client, char reg, char req) |
| 91 | { |
| 92 | int err; |
| 93 | |
| 94 | err = usb3503_read_register(client, reg); |
| 95 | if (err < 0) |
| 96 | return err; |
| 97 | |
| 98 | err = usb3503_write_register(client, reg, err & ~req); |
| 99 | if (err < 0) |
| 100 | return err; |
| 101 | |
| 102 | return 0; |
| 103 | } |
| 104 | |
| 105 | static int usb3503_reset(int gpio_reset, int state) |
| 106 | { |
| 107 | if (gpio_is_valid(gpio_reset)) |
| 108 | gpio_set_value(gpio_reset, state); |
| 109 | |
| 110 | /* Wait RefClk when RESET_N is released, otherwise Hub will |
| 111 | * not transition to Hub Communication Stage. |
| 112 | */ |
| 113 | if (state) |
| 114 | msleep(100); |
| 115 | |
| 116 | return 0; |
| 117 | } |
| 118 | |
| 119 | static int usb3503_switch_mode(struct usb3503 *hub, enum usb3503_mode mode) |
| 120 | { |
| 121 | struct i2c_client *i2c = hub->client; |
| 122 | int err = 0; |
| 123 | |
| 124 | switch (mode) { |
| 125 | case USB3503_MODE_HUB: |
| 126 | usb3503_reset(hub->gpio_reset, 1); |
| 127 | |
| 128 | /* SP_ILOCK: set connect_n, config_n for config */ |
| 129 | err = usb3503_write_register(i2c, USB3503_SP_ILOCK, |
| 130 | (USB3503_SPILOCK_CONNECT |
| 131 | | USB3503_SPILOCK_CONFIG)); |
| 132 | if (err < 0) { |
| 133 | dev_err(&i2c->dev, "SP_ILOCK failed (%d)\n", err); |
| 134 | goto err_hubmode; |
| 135 | } |
| 136 | |
| 137 | /* PDS : Port2,3 Disable For Self Powered Operation */ |
| 138 | err = usb3503_set_bits(i2c, USB3503_PDS, |
| 139 | (USB3503_PORT2 | USB3503_PORT3)); |
| 140 | if (err < 0) { |
| 141 | dev_err(&i2c->dev, "PDS failed (%d)\n", err); |
| 142 | goto err_hubmode; |
| 143 | } |
| 144 | |
| 145 | /* CFG1 : SELF_BUS_PWR -> Self-Powerd operation */ |
| 146 | err = usb3503_set_bits(i2c, USB3503_CFG1, USB3503_SELF_BUS_PWR); |
| 147 | if (err < 0) { |
| 148 | dev_err(&i2c->dev, "CFG1 failed (%d)\n", err); |
| 149 | goto err_hubmode; |
| 150 | } |
| 151 | |
| 152 | /* SP_LOCK: clear connect_n, config_n for hub connect */ |
| 153 | err = usb3503_clear_bits(i2c, USB3503_SP_ILOCK, |
| 154 | (USB3503_SPILOCK_CONNECT |
| 155 | | USB3503_SPILOCK_CONFIG)); |
| 156 | if (err < 0) { |
| 157 | dev_err(&i2c->dev, "SP_ILOCK failed (%d)\n", err); |
| 158 | goto err_hubmode; |
| 159 | } |
| 160 | |
| 161 | hub->mode = mode; |
| 162 | dev_info(&i2c->dev, "switched to HUB mode\n"); |
| 163 | break; |
| 164 | |
| 165 | case USB3503_MODE_STANDBY: |
| 166 | usb3503_reset(hub->gpio_reset, 0); |
| 167 | |
| 168 | hub->mode = mode; |
| 169 | dev_info(&i2c->dev, "switched to STANDBY mode\n"); |
| 170 | break; |
| 171 | |
| 172 | default: |
| 173 | dev_err(&i2c->dev, "unknown mode is request\n"); |
| 174 | err = -EINVAL; |
| 175 | break; |
| 176 | } |
| 177 | |
| 178 | err_hubmode: |
| 179 | return err; |
| 180 | } |
| 181 | |
Fengguang Wu | 74ff31b | 2013-01-12 13:15:09 +0800 | [diff] [blame] | 182 | static int usb3503_probe(struct i2c_client *i2c, const struct i2c_device_id *id) |
Dongjin Kim | 6a099c6 | 2012-12-08 05:18:44 +0900 | [diff] [blame] | 183 | { |
| 184 | struct usb3503_platform_data *pdata = i2c->dev.platform_data; |
Dongjin Kim | eab8050 | 2013-01-24 02:47:10 +0900 | [diff] [blame] | 185 | struct device_node *np = i2c->dev.of_node; |
Dongjin Kim | 6a099c6 | 2012-12-08 05:18:44 +0900 | [diff] [blame] | 186 | struct usb3503 *hub; |
Dongjin Kim | 7a8ea7e | 2013-01-12 20:54:33 +0900 | [diff] [blame] | 187 | int err = -ENOMEM; |
Dongjin Kim | 8ab03dd | 2013-01-26 01:53:03 +0900 | [diff] [blame] | 188 | u32 mode = USB3503_MODE_UNKNOWN; |
Dongjin Kim | 6a099c6 | 2012-12-08 05:18:44 +0900 | [diff] [blame] | 189 | |
| 190 | hub = kzalloc(sizeof(struct usb3503), GFP_KERNEL); |
| 191 | if (!hub) { |
| 192 | dev_err(&i2c->dev, "private data alloc fail\n"); |
Dongjin Kim | 7a8ea7e | 2013-01-12 20:54:33 +0900 | [diff] [blame] | 193 | return err; |
Dongjin Kim | 6a099c6 | 2012-12-08 05:18:44 +0900 | [diff] [blame] | 194 | } |
| 195 | |
| 196 | i2c_set_clientdata(i2c, hub); |
| 197 | hub->client = i2c; |
| 198 | |
Dongjin Kim | eab8050 | 2013-01-24 02:47:10 +0900 | [diff] [blame] | 199 | if (pdata) { |
Dongjin Kim | 6a099c6 | 2012-12-08 05:18:44 +0900 | [diff] [blame] | 200 | hub->gpio_intn = pdata->gpio_intn; |
| 201 | hub->gpio_connect = pdata->gpio_connect; |
| 202 | hub->gpio_reset = pdata->gpio_reset; |
| 203 | hub->mode = pdata->initial_mode; |
Dongjin Kim | eab8050 | 2013-01-24 02:47:10 +0900 | [diff] [blame] | 204 | } else if (np) { |
| 205 | hub->gpio_intn = of_get_named_gpio(np, "connect-gpios", 0); |
| 206 | if (hub->gpio_intn == -EPROBE_DEFER) |
| 207 | return -EPROBE_DEFER; |
| 208 | hub->gpio_connect = of_get_named_gpio(np, "intn-gpios", 0); |
| 209 | if (hub->gpio_connect == -EPROBE_DEFER) |
| 210 | return -EPROBE_DEFER; |
| 211 | hub->gpio_reset = of_get_named_gpio(np, "reset-gpios", 0); |
| 212 | if (hub->gpio_reset == -EPROBE_DEFER) |
| 213 | return -EPROBE_DEFER; |
| 214 | of_property_read_u32(np, "initial-mode", &mode); |
| 215 | hub->mode = mode; |
Dongjin Kim | 6a099c6 | 2012-12-08 05:18:44 +0900 | [diff] [blame] | 216 | } |
| 217 | |
| 218 | if (gpio_is_valid(hub->gpio_intn)) { |
| 219 | err = gpio_request_one(hub->gpio_intn, |
| 220 | GPIOF_OUT_INIT_HIGH, "usb3503 intn"); |
| 221 | if (err) { |
| 222 | dev_err(&i2c->dev, |
| 223 | "unable to request GPIO %d as connect pin (%d)\n", |
| 224 | hub->gpio_intn, err); |
Dongjin Kim | 7a8ea7e | 2013-01-12 20:54:33 +0900 | [diff] [blame] | 225 | goto err_out; |
Dongjin Kim | 6a099c6 | 2012-12-08 05:18:44 +0900 | [diff] [blame] | 226 | } |
| 227 | } |
| 228 | |
| 229 | if (gpio_is_valid(hub->gpio_connect)) { |
| 230 | err = gpio_request_one(hub->gpio_connect, |
| 231 | GPIOF_OUT_INIT_HIGH, "usb3503 connect"); |
| 232 | if (err) { |
| 233 | dev_err(&i2c->dev, |
| 234 | "unable to request GPIO %d as connect pin (%d)\n", |
| 235 | hub->gpio_connect, err); |
| 236 | goto err_gpio_connect; |
| 237 | } |
| 238 | } |
| 239 | |
| 240 | if (gpio_is_valid(hub->gpio_reset)) { |
| 241 | err = gpio_request_one(hub->gpio_reset, |
| 242 | GPIOF_OUT_INIT_LOW, "usb3503 reset"); |
| 243 | if (err) { |
| 244 | dev_err(&i2c->dev, |
| 245 | "unable to request GPIO %d as reset pin (%d)\n", |
| 246 | hub->gpio_reset, err); |
| 247 | goto err_gpio_reset; |
| 248 | } |
| 249 | } |
| 250 | |
Dongjin Kim | eab8050 | 2013-01-24 02:47:10 +0900 | [diff] [blame] | 251 | usb3503_switch_mode(hub, hub->mode); |
Dongjin Kim | 6a099c6 | 2012-12-08 05:18:44 +0900 | [diff] [blame] | 252 | |
| 253 | dev_info(&i2c->dev, "%s: probed on %s mode\n", __func__, |
| 254 | (hub->mode == USB3503_MODE_HUB) ? "hub" : "standby"); |
| 255 | |
| 256 | return 0; |
| 257 | |
| 258 | err_gpio_reset: |
| 259 | if (gpio_is_valid(hub->gpio_connect)) |
| 260 | gpio_free(hub->gpio_connect); |
| 261 | err_gpio_connect: |
| 262 | if (gpio_is_valid(hub->gpio_intn)) |
| 263 | gpio_free(hub->gpio_intn); |
Dongjin Kim | 7a8ea7e | 2013-01-12 20:54:33 +0900 | [diff] [blame] | 264 | err_out: |
Dongjin Kim | 6a099c6 | 2012-12-08 05:18:44 +0900 | [diff] [blame] | 265 | kfree(hub); |
| 266 | |
| 267 | return err; |
| 268 | } |
| 269 | |
| 270 | static int usb3503_remove(struct i2c_client *i2c) |
| 271 | { |
| 272 | struct usb3503 *hub = i2c_get_clientdata(i2c); |
| 273 | |
| 274 | if (gpio_is_valid(hub->gpio_intn)) |
| 275 | gpio_free(hub->gpio_intn); |
| 276 | if (gpio_is_valid(hub->gpio_connect)) |
| 277 | gpio_free(hub->gpio_connect); |
| 278 | if (gpio_is_valid(hub->gpio_reset)) |
| 279 | gpio_free(hub->gpio_reset); |
| 280 | |
| 281 | kfree(hub); |
| 282 | |
| 283 | return 0; |
| 284 | } |
| 285 | |
| 286 | static const struct i2c_device_id usb3503_id[] = { |
| 287 | { USB3503_I2C_NAME, 0 }, |
| 288 | { } |
| 289 | }; |
| 290 | MODULE_DEVICE_TABLE(i2c, usb3503_id); |
| 291 | |
Dongjin Kim | eab8050 | 2013-01-24 02:47:10 +0900 | [diff] [blame] | 292 | #ifdef CONFIG_OF |
| 293 | static const struct of_device_id usb3503_of_match[] = { |
| 294 | { .compatible = "smsc,usb3503", }, |
| 295 | {}, |
| 296 | }; |
| 297 | MODULE_DEVICE_TABLE(of, usb3503_of_match); |
| 298 | #endif |
| 299 | |
Dongjin Kim | 6a099c6 | 2012-12-08 05:18:44 +0900 | [diff] [blame] | 300 | static struct i2c_driver usb3503_driver = { |
| 301 | .driver = { |
| 302 | .name = USB3503_I2C_NAME, |
Dongjin Kim | eab8050 | 2013-01-24 02:47:10 +0900 | [diff] [blame] | 303 | .of_match_table = of_match_ptr(usb3503_of_match), |
Dongjin Kim | 6a099c6 | 2012-12-08 05:18:44 +0900 | [diff] [blame] | 304 | }, |
| 305 | .probe = usb3503_probe, |
| 306 | .remove = usb3503_remove, |
| 307 | .id_table = usb3503_id, |
| 308 | }; |
| 309 | |
| 310 | static int __init usb3503_init(void) |
| 311 | { |
| 312 | return i2c_add_driver(&usb3503_driver); |
| 313 | } |
| 314 | |
| 315 | static void __exit usb3503_exit(void) |
| 316 | { |
| 317 | i2c_del_driver(&usb3503_driver); |
| 318 | } |
| 319 | |
| 320 | module_init(usb3503_init); |
| 321 | module_exit(usb3503_exit); |
| 322 | |
| 323 | MODULE_AUTHOR("Dongjin Kim <tobetter@gmail.com>"); |
| 324 | MODULE_DESCRIPTION("USB3503 USB HUB driver"); |
| 325 | MODULE_LICENSE("GPL"); |