blob: 4b6572a37e876d4481647bb0e398f20e26cb2189 [file] [log] [blame]
Dongjin Kim6a099c62012-12-08 05:18:44 +09001/*
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 Kimeab80502013-01-24 02:47:10 +090026#include <linux/of_gpio.h>
Dongjin Kim6a099c62012-12-08 05:18:44 +090027#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
Dongjin Kim6a099c62012-12-08 05:18:44 +090045
46#define USB3503_SP_ILOCK 0xe7
47#define USB3503_SPILOCK_CONNECT (1 << 1)
48#define USB3503_SPILOCK_CONFIG (1 << 0)
49
50#define USB3503_CFGP 0xee
51#define USB3503_CLKSUSP (1 << 7)
52
53struct usb3503 {
54 enum usb3503_mode mode;
55 struct i2c_client *client;
Dongjin Kime8e44a42013-05-22 05:20:08 +090056 u8 port_off_mask;
Dongjin Kim6a099c62012-12-08 05:18:44 +090057 int gpio_intn;
58 int gpio_reset;
59 int gpio_connect;
60};
61
62static int usb3503_write_register(struct i2c_client *client,
63 char reg, char data)
64{
65 return i2c_smbus_write_byte_data(client, reg, data);
66}
67
68static int usb3503_read_register(struct i2c_client *client, char reg)
69{
70 return i2c_smbus_read_byte_data(client, reg);
71}
72
73static int usb3503_set_bits(struct i2c_client *client, char reg, char req)
74{
75 int err;
76
77 err = usb3503_read_register(client, reg);
78 if (err < 0)
79 return err;
80
81 err = usb3503_write_register(client, reg, err | req);
82 if (err < 0)
83 return err;
84
85 return 0;
86}
87
88static int usb3503_clear_bits(struct i2c_client *client, char reg, char req)
89{
90 int err;
91
92 err = usb3503_read_register(client, reg);
93 if (err < 0)
94 return err;
95
96 err = usb3503_write_register(client, reg, err & ~req);
97 if (err < 0)
98 return err;
99
100 return 0;
101}
102
Mark Brown8e7245b2013-08-09 11:41:51 +0100103static int usb3503_reset(struct usb3503 *hub, int state)
Dongjin Kim6a099c62012-12-08 05:18:44 +0900104{
Mark Brown8e7245b2013-08-09 11:41:51 +0100105 if (!state && gpio_is_valid(hub->gpio_connect))
106 gpio_set_value_cansleep(hub->gpio_connect, 0);
107
108 if (gpio_is_valid(hub->gpio_reset))
109 gpio_set_value_cansleep(hub->gpio_reset, state);
Dongjin Kim6a099c62012-12-08 05:18:44 +0900110
Julius Werner06a962f2013-05-31 18:34:52 -0700111 /* Wait T_HUBINIT == 4ms for hub logic to stabilize */
Dongjin Kim6a099c62012-12-08 05:18:44 +0900112 if (state)
Julius Werner06a962f2013-05-31 18:34:52 -0700113 usleep_range(4000, 10000);
Dongjin Kim6a099c62012-12-08 05:18:44 +0900114
115 return 0;
116}
117
118static int usb3503_switch_mode(struct usb3503 *hub, enum usb3503_mode mode)
119{
120 struct i2c_client *i2c = hub->client;
121 int err = 0;
122
123 switch (mode) {
124 case USB3503_MODE_HUB:
Mark Brown8e7245b2013-08-09 11:41:51 +0100125 usb3503_reset(hub, 1);
Dongjin Kim6a099c62012-12-08 05:18:44 +0900126
127 /* SP_ILOCK: set connect_n, config_n for config */
128 err = usb3503_write_register(i2c, USB3503_SP_ILOCK,
129 (USB3503_SPILOCK_CONNECT
130 | USB3503_SPILOCK_CONFIG));
131 if (err < 0) {
132 dev_err(&i2c->dev, "SP_ILOCK failed (%d)\n", err);
133 goto err_hubmode;
134 }
135
Dongjin Kime8e44a42013-05-22 05:20:08 +0900136 /* PDS : Disable For Self Powered Operation */
137 if (hub->port_off_mask) {
138 err = usb3503_set_bits(i2c, USB3503_PDS,
139 hub->port_off_mask);
140 if (err < 0) {
141 dev_err(&i2c->dev, "PDS failed (%d)\n", err);
142 goto err_hubmode;
143 }
Dongjin Kim6a099c62012-12-08 05:18:44 +0900144 }
145
146 /* CFG1 : SELF_BUS_PWR -> Self-Powerd operation */
147 err = usb3503_set_bits(i2c, USB3503_CFG1, USB3503_SELF_BUS_PWR);
148 if (err < 0) {
149 dev_err(&i2c->dev, "CFG1 failed (%d)\n", err);
150 goto err_hubmode;
151 }
152
153 /* SP_LOCK: clear connect_n, config_n for hub connect */
154 err = usb3503_clear_bits(i2c, USB3503_SP_ILOCK,
155 (USB3503_SPILOCK_CONNECT
156 | USB3503_SPILOCK_CONFIG));
157 if (err < 0) {
158 dev_err(&i2c->dev, "SP_ILOCK failed (%d)\n", err);
159 goto err_hubmode;
160 }
161
Mark Brown8e7245b2013-08-09 11:41:51 +0100162 if (gpio_is_valid(hub->gpio_connect))
163 gpio_set_value_cansleep(hub->gpio_connect, 1);
164
Dongjin Kim6a099c62012-12-08 05:18:44 +0900165 hub->mode = mode;
166 dev_info(&i2c->dev, "switched to HUB mode\n");
167 break;
168
169 case USB3503_MODE_STANDBY:
Mark Brown8e7245b2013-08-09 11:41:51 +0100170 usb3503_reset(hub, 0);
Dongjin Kim6a099c62012-12-08 05:18:44 +0900171
172 hub->mode = mode;
173 dev_info(&i2c->dev, "switched to STANDBY mode\n");
174 break;
175
176 default:
177 dev_err(&i2c->dev, "unknown mode is request\n");
178 err = -EINVAL;
179 break;
180 }
181
182err_hubmode:
183 return err;
184}
185
Fengguang Wu74ff31b2013-01-12 13:15:09 +0800186static int usb3503_probe(struct i2c_client *i2c, const struct i2c_device_id *id)
Dongjin Kim6a099c62012-12-08 05:18:44 +0900187{
Jingoo Hanb977a302013-07-30 17:05:28 +0900188 struct usb3503_platform_data *pdata = dev_get_platdata(&i2c->dev);
Dongjin Kimeab80502013-01-24 02:47:10 +0900189 struct device_node *np = i2c->dev.of_node;
Dongjin Kim6a099c62012-12-08 05:18:44 +0900190 struct usb3503 *hub;
Dongjin Kim7a8ea7e2013-01-12 20:54:33 +0900191 int err = -ENOMEM;
Dongjin Kim8ab03dd2013-01-26 01:53:03 +0900192 u32 mode = USB3503_MODE_UNKNOWN;
Dongjin Kime8b58b42013-05-22 05:20:09 +0900193 const u32 *property;
194 int len;
Dongjin Kim6a099c62012-12-08 05:18:44 +0900195
Mark Browncffedd62013-08-07 22:02:54 +0100196 hub = devm_kzalloc(&i2c->dev, sizeof(struct usb3503), GFP_KERNEL);
Dongjin Kim6a099c62012-12-08 05:18:44 +0900197 if (!hub) {
198 dev_err(&i2c->dev, "private data alloc fail\n");
Dongjin Kim7a8ea7e2013-01-12 20:54:33 +0900199 return err;
Dongjin Kim6a099c62012-12-08 05:18:44 +0900200 }
201
202 i2c_set_clientdata(i2c, hub);
203 hub->client = i2c;
204
Dongjin Kimeab80502013-01-24 02:47:10 +0900205 if (pdata) {
Dongjin Kime8e44a42013-05-22 05:20:08 +0900206 hub->port_off_mask = pdata->port_off_mask;
Dongjin Kim6a099c62012-12-08 05:18:44 +0900207 hub->gpio_intn = pdata->gpio_intn;
208 hub->gpio_connect = pdata->gpio_connect;
209 hub->gpio_reset = pdata->gpio_reset;
210 hub->mode = pdata->initial_mode;
Dongjin Kimeab80502013-01-24 02:47:10 +0900211 } else if (np) {
Dongjin Kime8b58b42013-05-22 05:20:09 +0900212 hub->port_off_mask = 0;
213
214 property = of_get_property(np, "disabled-ports", &len);
215 if (property && (len / sizeof(u32)) > 0) {
216 int i;
217 for (i = 0; i < len / sizeof(u32); i++) {
218 u32 port = be32_to_cpu(property[i]);
219 if ((1 <= port) && (port <= 3))
220 hub->port_off_mask |= (1 << port);
221 }
222 }
223
Mark Brown42416cc2013-08-07 20:28:24 +0100224 hub->gpio_intn = of_get_named_gpio(np, "intn-gpios", 0);
Dongjin Kimeab80502013-01-24 02:47:10 +0900225 if (hub->gpio_intn == -EPROBE_DEFER)
226 return -EPROBE_DEFER;
Mark Brown42416cc2013-08-07 20:28:24 +0100227 hub->gpio_connect = of_get_named_gpio(np, "connect-gpios", 0);
Dongjin Kimeab80502013-01-24 02:47:10 +0900228 if (hub->gpio_connect == -EPROBE_DEFER)
229 return -EPROBE_DEFER;
Julius Wernerccf92c92013-05-31 18:34:50 -0700230 hub->gpio_reset = of_get_named_gpio(np, "reset-gpios", 0);
Dongjin Kimeab80502013-01-24 02:47:10 +0900231 if (hub->gpio_reset == -EPROBE_DEFER)
232 return -EPROBE_DEFER;
233 of_property_read_u32(np, "initial-mode", &mode);
234 hub->mode = mode;
Dongjin Kim6a099c62012-12-08 05:18:44 +0900235 }
236
237 if (gpio_is_valid(hub->gpio_intn)) {
Mark Browncffedd62013-08-07 22:02:54 +0100238 err = devm_gpio_request_one(&i2c->dev, hub->gpio_intn,
Dongjin Kim6a099c62012-12-08 05:18:44 +0900239 GPIOF_OUT_INIT_HIGH, "usb3503 intn");
240 if (err) {
241 dev_err(&i2c->dev,
242 "unable to request GPIO %d as connect pin (%d)\n",
243 hub->gpio_intn, err);
Mark Browncffedd62013-08-07 22:02:54 +0100244 return err;
Dongjin Kim6a099c62012-12-08 05:18:44 +0900245 }
246 }
247
248 if (gpio_is_valid(hub->gpio_connect)) {
Mark Browncffedd62013-08-07 22:02:54 +0100249 err = devm_gpio_request_one(&i2c->dev, hub->gpio_connect,
Mark Brown8e7245b2013-08-09 11:41:51 +0100250 GPIOF_OUT_INIT_LOW, "usb3503 connect");
Dongjin Kim6a099c62012-12-08 05:18:44 +0900251 if (err) {
252 dev_err(&i2c->dev,
253 "unable to request GPIO %d as connect pin (%d)\n",
254 hub->gpio_connect, err);
Mark Browncffedd62013-08-07 22:02:54 +0100255 return err;
Dongjin Kim6a099c62012-12-08 05:18:44 +0900256 }
257 }
258
259 if (gpio_is_valid(hub->gpio_reset)) {
Mark Browncffedd62013-08-07 22:02:54 +0100260 err = devm_gpio_request_one(&i2c->dev, hub->gpio_reset,
Dongjin Kim6a099c62012-12-08 05:18:44 +0900261 GPIOF_OUT_INIT_LOW, "usb3503 reset");
262 if (err) {
263 dev_err(&i2c->dev,
264 "unable to request GPIO %d as reset pin (%d)\n",
265 hub->gpio_reset, err);
Mark Browncffedd62013-08-07 22:02:54 +0100266 return err;
Dongjin Kim6a099c62012-12-08 05:18:44 +0900267 }
268 }
269
Dongjin Kimeab80502013-01-24 02:47:10 +0900270 usb3503_switch_mode(hub, hub->mode);
Dongjin Kim6a099c62012-12-08 05:18:44 +0900271
272 dev_info(&i2c->dev, "%s: probed on %s mode\n", __func__,
273 (hub->mode == USB3503_MODE_HUB) ? "hub" : "standby");
274
275 return 0;
Dongjin Kim6a099c62012-12-08 05:18:44 +0900276}
277
278static const struct i2c_device_id usb3503_id[] = {
279 { USB3503_I2C_NAME, 0 },
280 { }
281};
282MODULE_DEVICE_TABLE(i2c, usb3503_id);
283
Dongjin Kimeab80502013-01-24 02:47:10 +0900284#ifdef CONFIG_OF
285static const struct of_device_id usb3503_of_match[] = {
286 { .compatible = "smsc,usb3503", },
287 {},
288};
289MODULE_DEVICE_TABLE(of, usb3503_of_match);
290#endif
291
Dongjin Kim6a099c62012-12-08 05:18:44 +0900292static struct i2c_driver usb3503_driver = {
293 .driver = {
294 .name = USB3503_I2C_NAME,
Dongjin Kimeab80502013-01-24 02:47:10 +0900295 .of_match_table = of_match_ptr(usb3503_of_match),
Dongjin Kim6a099c62012-12-08 05:18:44 +0900296 },
297 .probe = usb3503_probe,
Dongjin Kim6a099c62012-12-08 05:18:44 +0900298 .id_table = usb3503_id,
299};
300
Wei Yongjun8244ac02013-03-11 23:23:23 +0800301module_i2c_driver(usb3503_driver);
Dongjin Kim6a099c62012-12-08 05:18:44 +0900302
303MODULE_AUTHOR("Dongjin Kim <tobetter@gmail.com>");
304MODULE_DESCRIPTION("USB3503 USB HUB driver");
305MODULE_LICENSE("GPL");