Commit | Line | Data |
---|---|---|
6a099c63 DK |
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> | |
26 | #include <linux/platform_device.h> | |
27 | #include <linux/platform_data/usb3503.h> | |
28 | ||
29 | #define USB3503_VIDL 0x00 | |
30 | #define USB3503_VIDM 0x01 | |
31 | #define USB3503_PIDL 0x02 | |
32 | #define USB3503_PIDM 0x03 | |
33 | #define USB3503_DIDL 0x04 | |
34 | #define USB3503_DIDM 0x05 | |
35 | ||
36 | #define USB3503_CFG1 0x06 | |
37 | #define USB3503_SELF_BUS_PWR (1 << 7) | |
38 | ||
39 | #define USB3503_CFG2 0x07 | |
40 | #define USB3503_CFG3 0x08 | |
41 | #define USB3503_NRD 0x09 | |
42 | ||
43 | #define USB3503_PDS 0x0a | |
44 | #define USB3503_PORT1 (1 << 1) | |
45 | #define USB3503_PORT2 (1 << 2) | |
46 | #define USB3503_PORT3 (1 << 3) | |
47 | ||
48 | #define USB3503_SP_ILOCK 0xe7 | |
49 | #define USB3503_SPILOCK_CONNECT (1 << 1) | |
50 | #define USB3503_SPILOCK_CONFIG (1 << 0) | |
51 | ||
52 | #define USB3503_CFGP 0xee | |
53 | #define USB3503_CLKSUSP (1 << 7) | |
54 | ||
55 | struct usb3503 { | |
56 | enum usb3503_mode mode; | |
57 | struct i2c_client *client; | |
58 | int gpio_intn; | |
59 | int gpio_reset; | |
60 | int gpio_connect; | |
61 | }; | |
62 | ||
63 | static int usb3503_write_register(struct i2c_client *client, | |
64 | char reg, char data) | |
65 | { | |
66 | return i2c_smbus_write_byte_data(client, reg, data); | |
67 | } | |
68 | ||
69 | static int usb3503_read_register(struct i2c_client *client, char reg) | |
70 | { | |
71 | return i2c_smbus_read_byte_data(client, reg); | |
72 | } | |
73 | ||
74 | static int usb3503_set_bits(struct i2c_client *client, char reg, char req) | |
75 | { | |
76 | int err; | |
77 | ||
78 | err = usb3503_read_register(client, reg); | |
79 | if (err < 0) | |
80 | return err; | |
81 | ||
82 | err = usb3503_write_register(client, reg, err | req); | |
83 | if (err < 0) | |
84 | return err; | |
85 | ||
86 | return 0; | |
87 | } | |
88 | ||
89 | static int usb3503_clear_bits(struct i2c_client *client, char reg, char req) | |
90 | { | |
91 | int err; | |
92 | ||
93 | err = usb3503_read_register(client, reg); | |
94 | if (err < 0) | |
95 | return err; | |
96 | ||
97 | err = usb3503_write_register(client, reg, err & ~req); | |
98 | if (err < 0) | |
99 | return err; | |
100 | ||
101 | return 0; | |
102 | } | |
103 | ||
104 | static int usb3503_reset(int gpio_reset, int state) | |
105 | { | |
106 | if (gpio_is_valid(gpio_reset)) | |
107 | gpio_set_value(gpio_reset, state); | |
108 | ||
109 | /* Wait RefClk when RESET_N is released, otherwise Hub will | |
110 | * not transition to Hub Communication Stage. | |
111 | */ | |
112 | if (state) | |
113 | msleep(100); | |
114 | ||
115 | return 0; | |
116 | } | |
117 | ||
118 | static 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: | |
125 | usb3503_reset(hub->gpio_reset, 1); | |
126 | ||
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 | ||
136 | /* PDS : Port2,3 Disable For Self Powered Operation */ | |
137 | err = usb3503_set_bits(i2c, USB3503_PDS, | |
138 | (USB3503_PORT2 | USB3503_PORT3)); | |
139 | if (err < 0) { | |
140 | dev_err(&i2c->dev, "PDS failed (%d)\n", err); | |
141 | goto err_hubmode; | |
142 | } | |
143 | ||
144 | /* CFG1 : SELF_BUS_PWR -> Self-Powerd operation */ | |
145 | err = usb3503_set_bits(i2c, USB3503_CFG1, USB3503_SELF_BUS_PWR); | |
146 | if (err < 0) { | |
147 | dev_err(&i2c->dev, "CFG1 failed (%d)\n", err); | |
148 | goto err_hubmode; | |
149 | } | |
150 | ||
151 | /* SP_LOCK: clear connect_n, config_n for hub connect */ | |
152 | err = usb3503_clear_bits(i2c, USB3503_SP_ILOCK, | |
153 | (USB3503_SPILOCK_CONNECT | |
154 | | USB3503_SPILOCK_CONFIG)); | |
155 | if (err < 0) { | |
156 | dev_err(&i2c->dev, "SP_ILOCK failed (%d)\n", err); | |
157 | goto err_hubmode; | |
158 | } | |
159 | ||
160 | hub->mode = mode; | |
161 | dev_info(&i2c->dev, "switched to HUB mode\n"); | |
162 | break; | |
163 | ||
164 | case USB3503_MODE_STANDBY: | |
165 | usb3503_reset(hub->gpio_reset, 0); | |
166 | ||
167 | hub->mode = mode; | |
168 | dev_info(&i2c->dev, "switched to STANDBY mode\n"); | |
169 | break; | |
170 | ||
171 | default: | |
172 | dev_err(&i2c->dev, "unknown mode is request\n"); | |
173 | err = -EINVAL; | |
174 | break; | |
175 | } | |
176 | ||
177 | err_hubmode: | |
178 | return err; | |
179 | } | |
180 | ||
74ff31b8 | 181 | static int usb3503_probe(struct i2c_client *i2c, const struct i2c_device_id *id) |
6a099c63 DK |
182 | { |
183 | struct usb3503_platform_data *pdata = i2c->dev.platform_data; | |
184 | struct usb3503 *hub; | |
7a8ea7eb | 185 | int err = -ENOMEM; |
6a099c63 DK |
186 | |
187 | hub = kzalloc(sizeof(struct usb3503), GFP_KERNEL); | |
188 | if (!hub) { | |
189 | dev_err(&i2c->dev, "private data alloc fail\n"); | |
7a8ea7eb | 190 | return err; |
6a099c63 DK |
191 | } |
192 | ||
193 | i2c_set_clientdata(i2c, hub); | |
194 | hub->client = i2c; | |
195 | ||
196 | if (!pdata) { | |
197 | dev_dbg(&i2c->dev, "missing platform data\n"); | |
7a8ea7eb | 198 | goto err_out; |
6a099c63 DK |
199 | } else { |
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; | |
204 | } | |
205 | ||
206 | if (gpio_is_valid(hub->gpio_intn)) { | |
207 | err = gpio_request_one(hub->gpio_intn, | |
208 | GPIOF_OUT_INIT_HIGH, "usb3503 intn"); | |
209 | if (err) { | |
210 | dev_err(&i2c->dev, | |
211 | "unable to request GPIO %d as connect pin (%d)\n", | |
212 | hub->gpio_intn, err); | |
7a8ea7eb | 213 | goto err_out; |
6a099c63 DK |
214 | } |
215 | } | |
216 | ||
217 | if (gpio_is_valid(hub->gpio_connect)) { | |
218 | err = gpio_request_one(hub->gpio_connect, | |
219 | GPIOF_OUT_INIT_HIGH, "usb3503 connect"); | |
220 | if (err) { | |
221 | dev_err(&i2c->dev, | |
222 | "unable to request GPIO %d as connect pin (%d)\n", | |
223 | hub->gpio_connect, err); | |
224 | goto err_gpio_connect; | |
225 | } | |
226 | } | |
227 | ||
228 | if (gpio_is_valid(hub->gpio_reset)) { | |
229 | err = gpio_request_one(hub->gpio_reset, | |
230 | GPIOF_OUT_INIT_LOW, "usb3503 reset"); | |
231 | if (err) { | |
232 | dev_err(&i2c->dev, | |
233 | "unable to request GPIO %d as reset pin (%d)\n", | |
234 | hub->gpio_reset, err); | |
235 | goto err_gpio_reset; | |
236 | } | |
237 | } | |
238 | ||
239 | usb3503_switch_mode(hub, pdata->initial_mode); | |
240 | ||
241 | dev_info(&i2c->dev, "%s: probed on %s mode\n", __func__, | |
242 | (hub->mode == USB3503_MODE_HUB) ? "hub" : "standby"); | |
243 | ||
244 | return 0; | |
245 | ||
246 | err_gpio_reset: | |
247 | if (gpio_is_valid(hub->gpio_connect)) | |
248 | gpio_free(hub->gpio_connect); | |
249 | err_gpio_connect: | |
250 | if (gpio_is_valid(hub->gpio_intn)) | |
251 | gpio_free(hub->gpio_intn); | |
7a8ea7eb | 252 | err_out: |
6a099c63 DK |
253 | kfree(hub); |
254 | ||
255 | return err; | |
256 | } | |
257 | ||
258 | static int usb3503_remove(struct i2c_client *i2c) | |
259 | { | |
260 | struct usb3503 *hub = i2c_get_clientdata(i2c); | |
261 | ||
262 | if (gpio_is_valid(hub->gpio_intn)) | |
263 | gpio_free(hub->gpio_intn); | |
264 | if (gpio_is_valid(hub->gpio_connect)) | |
265 | gpio_free(hub->gpio_connect); | |
266 | if (gpio_is_valid(hub->gpio_reset)) | |
267 | gpio_free(hub->gpio_reset); | |
268 | ||
269 | kfree(hub); | |
270 | ||
271 | return 0; | |
272 | } | |
273 | ||
274 | static const struct i2c_device_id usb3503_id[] = { | |
275 | { USB3503_I2C_NAME, 0 }, | |
276 | { } | |
277 | }; | |
278 | MODULE_DEVICE_TABLE(i2c, usb3503_id); | |
279 | ||
280 | static struct i2c_driver usb3503_driver = { | |
281 | .driver = { | |
282 | .name = USB3503_I2C_NAME, | |
283 | }, | |
284 | .probe = usb3503_probe, | |
285 | .remove = usb3503_remove, | |
286 | .id_table = usb3503_id, | |
287 | }; | |
288 | ||
289 | static int __init usb3503_init(void) | |
290 | { | |
291 | return i2c_add_driver(&usb3503_driver); | |
292 | } | |
293 | ||
294 | static void __exit usb3503_exit(void) | |
295 | { | |
296 | i2c_del_driver(&usb3503_driver); | |
297 | } | |
298 | ||
299 | module_init(usb3503_init); | |
300 | module_exit(usb3503_exit); | |
301 | ||
302 | MODULE_AUTHOR("Dongjin Kim <tobetter@gmail.com>"); | |
303 | MODULE_DESCRIPTION("USB3503 USB HUB driver"); | |
304 | MODULE_LICENSE("GPL"); |