2 * Driver for SMSC USB3503 USB 2.0 hub controller driver
4 * Copyright (c) 2012-2013 Dongjin Kim (tobetter@gmail.com)
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.
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.
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
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>
26 #include <linux/of_gpio.h>
27 #include <linux/platform_device.h>
28 #include <linux/platform_data/usb3503.h>
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
37 #define USB3503_CFG1 0x06
38 #define USB3503_SELF_BUS_PWR (1 << 7)
40 #define USB3503_CFG2 0x07
41 #define USB3503_CFG3 0x08
42 #define USB3503_NRD 0x09
44 #define USB3503_PDS 0x0a
46 #define USB3503_SP_ILOCK 0xe7
47 #define USB3503_SPILOCK_CONNECT (1 << 1)
48 #define USB3503_SPILOCK_CONFIG (1 << 0)
50 #define USB3503_CFGP 0xee
51 #define USB3503_CLKSUSP (1 << 7)
54 enum usb3503_mode mode;
55 struct i2c_client *client;
62 static int usb3503_write_register(struct i2c_client *client,
65 return i2c_smbus_write_byte_data(client, reg, data);
68 static int usb3503_read_register(struct i2c_client *client, char reg)
70 return i2c_smbus_read_byte_data(client, reg);
73 static int usb3503_set_bits(struct i2c_client *client, char reg, char req)
77 err = usb3503_read_register(client, reg);
81 err = usb3503_write_register(client, reg, err | req);
88 static int usb3503_clear_bits(struct i2c_client *client, char reg, char req)
92 err = usb3503_read_register(client, reg);
96 err = usb3503_write_register(client, reg, err & ~req);
103 static int usb3503_reset(int gpio_reset, int state)
105 if (gpio_is_valid(gpio_reset))
106 gpio_set_value(gpio_reset, state);
108 /* Wait T_HUBINIT == 4ms for hub logic to stabilize */
110 usleep_range(4000, 10000);
115 static int usb3503_switch_mode(struct usb3503 *hub, enum usb3503_mode mode)
117 struct i2c_client *i2c = hub->client;
121 case USB3503_MODE_HUB:
122 usb3503_reset(hub->gpio_reset, 1);
124 /* SP_ILOCK: set connect_n, config_n for config */
125 err = usb3503_write_register(i2c, USB3503_SP_ILOCK,
126 (USB3503_SPILOCK_CONNECT
127 | USB3503_SPILOCK_CONFIG));
129 dev_err(&i2c->dev, "SP_ILOCK failed (%d)\n", err);
133 /* PDS : Disable For Self Powered Operation */
134 if (hub->port_off_mask) {
135 err = usb3503_set_bits(i2c, USB3503_PDS,
138 dev_err(&i2c->dev, "PDS failed (%d)\n", err);
143 /* CFG1 : SELF_BUS_PWR -> Self-Powerd operation */
144 err = usb3503_set_bits(i2c, USB3503_CFG1, USB3503_SELF_BUS_PWR);
146 dev_err(&i2c->dev, "CFG1 failed (%d)\n", err);
150 /* SP_LOCK: clear connect_n, config_n for hub connect */
151 err = usb3503_clear_bits(i2c, USB3503_SP_ILOCK,
152 (USB3503_SPILOCK_CONNECT
153 | USB3503_SPILOCK_CONFIG));
155 dev_err(&i2c->dev, "SP_ILOCK failed (%d)\n", err);
160 dev_info(&i2c->dev, "switched to HUB mode\n");
163 case USB3503_MODE_STANDBY:
164 usb3503_reset(hub->gpio_reset, 0);
167 dev_info(&i2c->dev, "switched to STANDBY mode\n");
171 dev_err(&i2c->dev, "unknown mode is request\n");
180 static int usb3503_probe(struct i2c_client *i2c, const struct i2c_device_id *id)
182 struct usb3503_platform_data *pdata = i2c->dev.platform_data;
183 struct device_node *np = i2c->dev.of_node;
186 u32 mode = USB3503_MODE_UNKNOWN;
190 hub = kzalloc(sizeof(struct usb3503), GFP_KERNEL);
192 dev_err(&i2c->dev, "private data alloc fail\n");
196 i2c_set_clientdata(i2c, hub);
200 hub->port_off_mask = pdata->port_off_mask;
201 hub->gpio_intn = pdata->gpio_intn;
202 hub->gpio_connect = pdata->gpio_connect;
203 hub->gpio_reset = pdata->gpio_reset;
204 hub->mode = pdata->initial_mode;
206 hub->port_off_mask = 0;
208 property = of_get_property(np, "disabled-ports", &len);
209 if (property && (len / sizeof(u32)) > 0) {
211 for (i = 0; i < len / sizeof(u32); i++) {
212 u32 port = be32_to_cpu(property[i]);
213 if ((1 <= port) && (port <= 3))
214 hub->port_off_mask |= (1 << port);
218 hub->gpio_intn = of_get_named_gpio(np, "connect-gpios", 0);
219 if (hub->gpio_intn == -EPROBE_DEFER)
220 return -EPROBE_DEFER;
221 hub->gpio_connect = of_get_named_gpio(np, "intn-gpios", 0);
222 if (hub->gpio_connect == -EPROBE_DEFER)
223 return -EPROBE_DEFER;
224 hub->gpio_reset = of_get_named_gpio(np, "reset-gpios", 0);
225 if (hub->gpio_reset == -EPROBE_DEFER)
226 return -EPROBE_DEFER;
227 of_property_read_u32(np, "initial-mode", &mode);
231 if (gpio_is_valid(hub->gpio_intn)) {
232 err = gpio_request_one(hub->gpio_intn,
233 GPIOF_OUT_INIT_HIGH, "usb3503 intn");
236 "unable to request GPIO %d as connect pin (%d)\n",
237 hub->gpio_intn, err);
242 if (gpio_is_valid(hub->gpio_connect)) {
243 err = gpio_request_one(hub->gpio_connect,
244 GPIOF_OUT_INIT_HIGH, "usb3503 connect");
247 "unable to request GPIO %d as connect pin (%d)\n",
248 hub->gpio_connect, err);
249 goto err_gpio_connect;
253 if (gpio_is_valid(hub->gpio_reset)) {
254 err = gpio_request_one(hub->gpio_reset,
255 GPIOF_OUT_INIT_LOW, "usb3503 reset");
258 "unable to request GPIO %d as reset pin (%d)\n",
259 hub->gpio_reset, err);
264 usb3503_switch_mode(hub, hub->mode);
266 dev_info(&i2c->dev, "%s: probed on %s mode\n", __func__,
267 (hub->mode == USB3503_MODE_HUB) ? "hub" : "standby");
272 if (gpio_is_valid(hub->gpio_connect))
273 gpio_free(hub->gpio_connect);
275 if (gpio_is_valid(hub->gpio_intn))
276 gpio_free(hub->gpio_intn);
283 static int usb3503_remove(struct i2c_client *i2c)
285 struct usb3503 *hub = i2c_get_clientdata(i2c);
287 if (gpio_is_valid(hub->gpio_intn))
288 gpio_free(hub->gpio_intn);
289 if (gpio_is_valid(hub->gpio_connect))
290 gpio_free(hub->gpio_connect);
291 if (gpio_is_valid(hub->gpio_reset))
292 gpio_free(hub->gpio_reset);
299 static const struct i2c_device_id usb3503_id[] = {
300 { USB3503_I2C_NAME, 0 },
303 MODULE_DEVICE_TABLE(i2c, usb3503_id);
306 static const struct of_device_id usb3503_of_match[] = {
307 { .compatible = "smsc,usb3503", },
310 MODULE_DEVICE_TABLE(of, usb3503_of_match);
313 static struct i2c_driver usb3503_driver = {
315 .name = USB3503_I2C_NAME,
316 .of_match_table = of_match_ptr(usb3503_of_match),
318 .probe = usb3503_probe,
319 .remove = usb3503_remove,
320 .id_table = usb3503_id,
323 module_i2c_driver(usb3503_driver);
325 MODULE_AUTHOR("Dongjin Kim <tobetter@gmail.com>");
326 MODULE_DESCRIPTION("USB3503 USB HUB driver");
327 MODULE_LICENSE("GPL");