]>
Commit | Line | Data |
---|---|---|
5fd54ace | 1 | // SPDX-License-Identifier: GPL-2.0+ |
6a099c63 DK |
2 | /* |
3 | * Driver for SMSC USB3503 USB 2.0 hub controller driver | |
4 | * | |
5 | * Copyright (c) 2012-2013 Dongjin Kim (tobetter@gmail.com) | |
6 | * | |
7 | * This program is free software; you can redistribute it and/or modify | |
8 | * it under the terms of the GNU General Public License as published by | |
9 | * the Free Software Foundation; either version 2 of the License, or | |
10 | * (at your option) any later version. | |
11 | * | |
12 | * This program is distributed in the hope that it will be useful, | |
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 | * GNU General Public License for more details. | |
16 | * | |
17 | * You should have received a copy of the GNU General Public License | |
18 | * along with this program; if not, write to the Free Software | |
19 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
20 | */ | |
21 | ||
657d898a | 22 | #include <linux/clk.h> |
6a099c63 DK |
23 | #include <linux/i2c.h> |
24 | #include <linux/gpio.h> | |
25 | #include <linux/delay.h> | |
26 | #include <linux/slab.h> | |
27 | #include <linux/module.h> | |
eab8050c | 28 | #include <linux/of_gpio.h> |
6a099c63 DK |
29 | #include <linux/platform_device.h> |
30 | #include <linux/platform_data/usb3503.h> | |
68b14134 | 31 | #include <linux/regmap.h> |
6a099c63 DK |
32 | |
33 | #define USB3503_VIDL 0x00 | |
34 | #define USB3503_VIDM 0x01 | |
35 | #define USB3503_PIDL 0x02 | |
36 | #define USB3503_PIDM 0x03 | |
37 | #define USB3503_DIDL 0x04 | |
38 | #define USB3503_DIDM 0x05 | |
39 | ||
40 | #define USB3503_CFG1 0x06 | |
41 | #define USB3503_SELF_BUS_PWR (1 << 7) | |
42 | ||
43 | #define USB3503_CFG2 0x07 | |
44 | #define USB3503_CFG3 0x08 | |
45 | #define USB3503_NRD 0x09 | |
46 | ||
47 | #define USB3503_PDS 0x0a | |
6a099c63 DK |
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 | ||
68b14134 MB |
56 | #define USB3503_RESET 0xff |
57 | ||
6a099c63 DK |
58 | struct usb3503 { |
59 | enum usb3503_mode mode; | |
68b14134 MB |
60 | struct regmap *regmap; |
61 | struct device *dev; | |
657d898a | 62 | struct clk *clk; |
e8e44a48 | 63 | u8 port_off_mask; |
6a099c63 DK |
64 | int gpio_intn; |
65 | int gpio_reset; | |
66 | int gpio_connect; | |
657d898a | 67 | bool secondary_ref_clk; |
6a099c63 DK |
68 | }; |
69 | ||
8e7245b8 | 70 | static int usb3503_reset(struct usb3503 *hub, int state) |
6a099c63 | 71 | { |
8e7245b8 MB |
72 | if (!state && gpio_is_valid(hub->gpio_connect)) |
73 | gpio_set_value_cansleep(hub->gpio_connect, 0); | |
74 | ||
75 | if (gpio_is_valid(hub->gpio_reset)) | |
76 | gpio_set_value_cansleep(hub->gpio_reset, state); | |
6a099c63 | 77 | |
06a962fa | 78 | /* Wait T_HUBINIT == 4ms for hub logic to stabilize */ |
6a099c63 | 79 | if (state) |
06a962fa | 80 | usleep_range(4000, 10000); |
6a099c63 DK |
81 | |
82 | return 0; | |
83 | } | |
84 | ||
3f0d1c67 | 85 | static int usb3503_connect(struct usb3503 *hub) |
6a099c63 | 86 | { |
68b14134 | 87 | struct device *dev = hub->dev; |
3f0d1c67 | 88 | int err; |
6a099c63 | 89 | |
3f0d1c67 | 90 | usb3503_reset(hub, 1); |
6a099c63 | 91 | |
3f0d1c67 | 92 | if (hub->regmap) { |
6a099c63 | 93 | /* SP_ILOCK: set connect_n, config_n for config */ |
68b14134 | 94 | err = regmap_write(hub->regmap, USB3503_SP_ILOCK, |
3f0d1c67 | 95 | (USB3503_SPILOCK_CONNECT |
6a099c63 DK |
96 | | USB3503_SPILOCK_CONFIG)); |
97 | if (err < 0) { | |
68b14134 | 98 | dev_err(dev, "SP_ILOCK failed (%d)\n", err); |
3f0d1c67 | 99 | return err; |
6a099c63 DK |
100 | } |
101 | ||
4463e152 | 102 | /* PDS : Set the ports which are disabled in self-powered mode. */ |
e8e44a48 | 103 | if (hub->port_off_mask) { |
68b14134 MB |
104 | err = regmap_update_bits(hub->regmap, USB3503_PDS, |
105 | hub->port_off_mask, | |
e8e44a48 DK |
106 | hub->port_off_mask); |
107 | if (err < 0) { | |
68b14134 | 108 | dev_err(dev, "PDS failed (%d)\n", err); |
3f0d1c67 | 109 | return err; |
e8e44a48 | 110 | } |
6a099c63 DK |
111 | } |
112 | ||
4463e152 | 113 | /* CFG1 : Set SELF_BUS_PWR, this enables self-powered operation. */ |
68b14134 MB |
114 | err = regmap_update_bits(hub->regmap, USB3503_CFG1, |
115 | USB3503_SELF_BUS_PWR, | |
116 | USB3503_SELF_BUS_PWR); | |
6a099c63 | 117 | if (err < 0) { |
68b14134 | 118 | dev_err(dev, "CFG1 failed (%d)\n", err); |
3f0d1c67 | 119 | return err; |
6a099c63 DK |
120 | } |
121 | ||
122 | /* SP_LOCK: clear connect_n, config_n for hub connect */ | |
68b14134 MB |
123 | err = regmap_update_bits(hub->regmap, USB3503_SP_ILOCK, |
124 | (USB3503_SPILOCK_CONNECT | |
125 | | USB3503_SPILOCK_CONFIG), 0); | |
6a099c63 | 126 | if (err < 0) { |
68b14134 | 127 | dev_err(dev, "SP_ILOCK failed (%d)\n", err); |
3f0d1c67 | 128 | return err; |
6a099c63 | 129 | } |
3f0d1c67 | 130 | } |
6a099c63 | 131 | |
3f0d1c67 MB |
132 | if (gpio_is_valid(hub->gpio_connect)) |
133 | gpio_set_value_cansleep(hub->gpio_connect, 1); | |
8e7245b8 | 134 | |
3f0d1c67 MB |
135 | hub->mode = USB3503_MODE_HUB; |
136 | dev_info(dev, "switched to HUB mode\n"); | |
137 | ||
138 | return 0; | |
139 | } | |
140 | ||
141 | static int usb3503_switch_mode(struct usb3503 *hub, enum usb3503_mode mode) | |
142 | { | |
143 | struct device *dev = hub->dev; | |
144 | int err = 0; | |
145 | ||
146 | switch (mode) { | |
147 | case USB3503_MODE_HUB: | |
148 | err = usb3503_connect(hub); | |
6a099c63 DK |
149 | break; |
150 | ||
151 | case USB3503_MODE_STANDBY: | |
8e7245b8 | 152 | usb3503_reset(hub, 0); |
68b14134 | 153 | dev_info(dev, "switched to STANDBY mode\n"); |
6a099c63 DK |
154 | break; |
155 | ||
156 | default: | |
dd8e670d | 157 | dev_err(dev, "unknown mode is requested\n"); |
6a099c63 DK |
158 | err = -EINVAL; |
159 | break; | |
160 | } | |
161 | ||
6a099c63 DK |
162 | return err; |
163 | } | |
164 | ||
68b14134 MB |
165 | static const struct regmap_config usb3503_regmap_config = { |
166 | .reg_bits = 8, | |
167 | .val_bits = 8, | |
168 | ||
169 | .max_register = USB3503_RESET, | |
170 | }; | |
171 | ||
2487e3ee | 172 | static int usb3503_probe(struct usb3503 *hub) |
6a099c63 | 173 | { |
2487e3ee MB |
174 | struct device *dev = hub->dev; |
175 | struct usb3503_platform_data *pdata = dev_get_platdata(dev); | |
176 | struct device_node *np = dev->of_node; | |
177 | int err; | |
e5a0c874 | 178 | u32 mode = USB3503_MODE_HUB; |
e8b58b49 DK |
179 | const u32 *property; |
180 | int len; | |
6a099c63 | 181 | |
eab8050c | 182 | if (pdata) { |
e8e44a48 | 183 | hub->port_off_mask = pdata->port_off_mask; |
6a099c63 DK |
184 | hub->gpio_intn = pdata->gpio_intn; |
185 | hub->gpio_connect = pdata->gpio_connect; | |
186 | hub->gpio_reset = pdata->gpio_reset; | |
187 | hub->mode = pdata->initial_mode; | |
eab8050c | 188 | } else if (np) { |
657d898a | 189 | struct clk *clk; |
c0ab6bb0 | 190 | u32 rate = 0; |
e8b58b49 DK |
191 | hub->port_off_mask = 0; |
192 | ||
c0ab6bb0 BG |
193 | if (!of_property_read_u32(np, "refclk-frequency", &rate)) { |
194 | switch (rate) { | |
195 | case 38400000: | |
196 | case 26000000: | |
197 | case 19200000: | |
198 | case 12000000: | |
199 | hub->secondary_ref_clk = 0; | |
200 | break; | |
201 | case 24000000: | |
202 | case 27000000: | |
203 | case 25000000: | |
204 | case 50000000: | |
205 | hub->secondary_ref_clk = 1; | |
206 | break; | |
207 | default: | |
208 | dev_err(dev, | |
209 | "unsupported reference clock rate (%d)\n", | |
210 | (int) rate); | |
211 | return -EINVAL; | |
212 | } | |
213 | } | |
214 | ||
657d898a MS |
215 | clk = devm_clk_get(dev, "refclk"); |
216 | if (IS_ERR(clk) && PTR_ERR(clk) != -ENOENT) { | |
ec5734c4 TB |
217 | dev_err(dev, "unable to request refclk (%ld)\n", |
218 | PTR_ERR(clk)); | |
657d898a MS |
219 | return PTR_ERR(clk); |
220 | } | |
221 | ||
222 | if (!IS_ERR(clk)) { | |
657d898a MS |
223 | hub->clk = clk; |
224 | ||
c0ab6bb0 | 225 | if (rate != 0) { |
657d898a MS |
226 | err = clk_set_rate(hub->clk, rate); |
227 | if (err) { | |
228 | dev_err(dev, | |
229 | "unable to set reference clock rate to %d\n", | |
230 | (int) rate); | |
231 | return err; | |
232 | } | |
233 | } | |
234 | ||
235 | err = clk_prepare_enable(hub->clk); | |
236 | if (err) { | |
237 | dev_err(dev, | |
238 | "unable to enable reference clock\n"); | |
239 | return err; | |
240 | } | |
241 | } | |
242 | ||
e8b58b49 DK |
243 | property = of_get_property(np, "disabled-ports", &len); |
244 | if (property && (len / sizeof(u32)) > 0) { | |
245 | int i; | |
246 | for (i = 0; i < len / sizeof(u32); i++) { | |
247 | u32 port = be32_to_cpu(property[i]); | |
248 | if ((1 <= port) && (port <= 3)) | |
249 | hub->port_off_mask |= (1 << port); | |
250 | } | |
251 | } | |
252 | ||
42416cc8 | 253 | hub->gpio_intn = of_get_named_gpio(np, "intn-gpios", 0); |
eab8050c DK |
254 | if (hub->gpio_intn == -EPROBE_DEFER) |
255 | return -EPROBE_DEFER; | |
42416cc8 | 256 | hub->gpio_connect = of_get_named_gpio(np, "connect-gpios", 0); |
eab8050c DK |
257 | if (hub->gpio_connect == -EPROBE_DEFER) |
258 | return -EPROBE_DEFER; | |
ccf92c94 | 259 | hub->gpio_reset = of_get_named_gpio(np, "reset-gpios", 0); |
eab8050c DK |
260 | if (hub->gpio_reset == -EPROBE_DEFER) |
261 | return -EPROBE_DEFER; | |
262 | of_property_read_u32(np, "initial-mode", &mode); | |
263 | hub->mode = mode; | |
6a099c63 DK |
264 | } |
265 | ||
3f0d1c67 MB |
266 | if (hub->port_off_mask && !hub->regmap) |
267 | dev_err(dev, "Ports disabled with no control interface\n"); | |
268 | ||
6a099c63 | 269 | if (gpio_is_valid(hub->gpio_intn)) { |
657d898a MS |
270 | int val = hub->secondary_ref_clk ? GPIOF_OUT_INIT_LOW : |
271 | GPIOF_OUT_INIT_HIGH; | |
272 | err = devm_gpio_request_one(dev, hub->gpio_intn, val, | |
273 | "usb3503 intn"); | |
6a099c63 | 274 | if (err) { |
2487e3ee | 275 | dev_err(dev, |
6b084421 | 276 | "unable to request GPIO %d as interrupt pin (%d)\n", |
2487e3ee | 277 | hub->gpio_intn, err); |
cffedd67 | 278 | return err; |
6a099c63 DK |
279 | } |
280 | } | |
281 | ||
282 | if (gpio_is_valid(hub->gpio_connect)) { | |
2487e3ee | 283 | err = devm_gpio_request_one(dev, hub->gpio_connect, |
8e7245b8 | 284 | GPIOF_OUT_INIT_LOW, "usb3503 connect"); |
6a099c63 | 285 | if (err) { |
2487e3ee MB |
286 | dev_err(dev, |
287 | "unable to request GPIO %d as connect pin (%d)\n", | |
288 | hub->gpio_connect, err); | |
cffedd67 | 289 | return err; |
6a099c63 DK |
290 | } |
291 | } | |
292 | ||
293 | if (gpio_is_valid(hub->gpio_reset)) { | |
2487e3ee | 294 | err = devm_gpio_request_one(dev, hub->gpio_reset, |
6a099c63 DK |
295 | GPIOF_OUT_INIT_LOW, "usb3503 reset"); |
296 | if (err) { | |
2487e3ee MB |
297 | dev_err(dev, |
298 | "unable to request GPIO %d as reset pin (%d)\n", | |
299 | hub->gpio_reset, err); | |
cffedd67 | 300 | return err; |
6a099c63 DK |
301 | } |
302 | } | |
303 | ||
eab8050c | 304 | usb3503_switch_mode(hub, hub->mode); |
6a099c63 | 305 | |
dd8e670d | 306 | dev_info(dev, "%s: probed in %s mode\n", __func__, |
6a099c63 DK |
307 | (hub->mode == USB3503_MODE_HUB) ? "hub" : "standby"); |
308 | ||
309 | return 0; | |
6a099c63 DK |
310 | } |
311 | ||
2487e3ee MB |
312 | static int usb3503_i2c_probe(struct i2c_client *i2c, |
313 | const struct i2c_device_id *id) | |
314 | { | |
315 | struct usb3503 *hub; | |
316 | int err; | |
317 | ||
318 | hub = devm_kzalloc(&i2c->dev, sizeof(struct usb3503), GFP_KERNEL); | |
39d98bdc | 319 | if (!hub) |
2487e3ee | 320 | return -ENOMEM; |
2487e3ee MB |
321 | |
322 | i2c_set_clientdata(i2c, hub); | |
323 | hub->regmap = devm_regmap_init_i2c(i2c, &usb3503_regmap_config); | |
324 | if (IS_ERR(hub->regmap)) { | |
325 | err = PTR_ERR(hub->regmap); | |
326 | dev_err(&i2c->dev, "Failed to initialise regmap: %d\n", err); | |
327 | return err; | |
328 | } | |
329 | hub->dev = &i2c->dev; | |
330 | ||
331 | return usb3503_probe(hub); | |
332 | } | |
333 | ||
62c32e46 KK |
334 | static int usb3503_i2c_remove(struct i2c_client *i2c) |
335 | { | |
336 | struct usb3503 *hub; | |
337 | ||
338 | hub = i2c_get_clientdata(i2c); | |
339 | if (hub->clk) | |
340 | clk_disable_unprepare(hub->clk); | |
341 | ||
342 | return 0; | |
343 | } | |
344 | ||
3f0d1c67 MB |
345 | static int usb3503_platform_probe(struct platform_device *pdev) |
346 | { | |
347 | struct usb3503 *hub; | |
348 | ||
349 | hub = devm_kzalloc(&pdev->dev, sizeof(struct usb3503), GFP_KERNEL); | |
39d98bdc | 350 | if (!hub) |
3f0d1c67 | 351 | return -ENOMEM; |
3f0d1c67 | 352 | hub->dev = &pdev->dev; |
495660cb | 353 | platform_set_drvdata(pdev, hub); |
3f0d1c67 MB |
354 | |
355 | return usb3503_probe(hub); | |
356 | } | |
357 | ||
62c32e46 KK |
358 | static int usb3503_platform_remove(struct platform_device *pdev) |
359 | { | |
360 | struct usb3503 *hub; | |
361 | ||
362 | hub = platform_get_drvdata(pdev); | |
363 | if (hub->clk) | |
364 | clk_disable_unprepare(hub->clk); | |
365 | ||
366 | return 0; | |
367 | } | |
368 | ||
ecdc071d JS |
369 | #ifdef CONFIG_PM_SLEEP |
370 | static int usb3503_i2c_suspend(struct device *dev) | |
371 | { | |
372 | struct i2c_client *client = to_i2c_client(dev); | |
373 | struct usb3503 *hub = i2c_get_clientdata(client); | |
374 | ||
375 | usb3503_switch_mode(hub, USB3503_MODE_STANDBY); | |
376 | ||
377 | if (hub->clk) | |
378 | clk_disable_unprepare(hub->clk); | |
379 | ||
380 | return 0; | |
381 | } | |
382 | ||
383 | static int usb3503_i2c_resume(struct device *dev) | |
384 | { | |
385 | struct i2c_client *client = to_i2c_client(dev); | |
386 | struct usb3503 *hub = i2c_get_clientdata(client); | |
387 | ||
388 | if (hub->clk) | |
389 | clk_prepare_enable(hub->clk); | |
390 | ||
391 | usb3503_switch_mode(hub, hub->mode); | |
392 | ||
393 | return 0; | |
394 | } | |
395 | #endif | |
396 | ||
397 | static SIMPLE_DEV_PM_OPS(usb3503_i2c_pm_ops, usb3503_i2c_suspend, | |
398 | usb3503_i2c_resume); | |
399 | ||
6a099c63 DK |
400 | static const struct i2c_device_id usb3503_id[] = { |
401 | { USB3503_I2C_NAME, 0 }, | |
402 | { } | |
403 | }; | |
404 | MODULE_DEVICE_TABLE(i2c, usb3503_id); | |
405 | ||
eab8050c DK |
406 | #ifdef CONFIG_OF |
407 | static const struct of_device_id usb3503_of_match[] = { | |
408 | { .compatible = "smsc,usb3503", }, | |
5bdd1f4a | 409 | { .compatible = "smsc,usb3503a", }, |
eab8050c DK |
410 | {}, |
411 | }; | |
412 | MODULE_DEVICE_TABLE(of, usb3503_of_match); | |
413 | #endif | |
414 | ||
3f0d1c67 | 415 | static struct i2c_driver usb3503_i2c_driver = { |
6a099c63 DK |
416 | .driver = { |
417 | .name = USB3503_I2C_NAME, | |
ecdc071d | 418 | .pm = &usb3503_i2c_pm_ops, |
eab8050c | 419 | .of_match_table = of_match_ptr(usb3503_of_match), |
6a099c63 | 420 | }, |
2487e3ee | 421 | .probe = usb3503_i2c_probe, |
62c32e46 | 422 | .remove = usb3503_i2c_remove, |
6a099c63 DK |
423 | .id_table = usb3503_id, |
424 | }; | |
425 | ||
3f0d1c67 MB |
426 | static struct platform_driver usb3503_platform_driver = { |
427 | .driver = { | |
428 | .name = USB3503_I2C_NAME, | |
429 | .of_match_table = of_match_ptr(usb3503_of_match), | |
3f0d1c67 MB |
430 | }, |
431 | .probe = usb3503_platform_probe, | |
62c32e46 | 432 | .remove = usb3503_platform_remove, |
3f0d1c67 MB |
433 | }; |
434 | ||
435 | static int __init usb3503_init(void) | |
436 | { | |
437 | int err; | |
438 | ||
aa5b477d | 439 | err = i2c_add_driver(&usb3503_i2c_driver); |
3f0d1c67 MB |
440 | if (err != 0) |
441 | pr_err("usb3503: Failed to register I2C driver: %d\n", err); | |
442 | ||
443 | err = platform_driver_register(&usb3503_platform_driver); | |
444 | if (err != 0) | |
445 | pr_err("usb3503: Failed to register platform driver: %d\n", | |
446 | err); | |
447 | ||
448 | return 0; | |
449 | } | |
450 | module_init(usb3503_init); | |
451 | ||
452 | static void __exit usb3503_exit(void) | |
453 | { | |
454 | platform_driver_unregister(&usb3503_platform_driver); | |
455 | i2c_del_driver(&usb3503_i2c_driver); | |
456 | } | |
457 | module_exit(usb3503_exit); | |
6a099c63 DK |
458 | |
459 | MODULE_AUTHOR("Dongjin Kim <tobetter@gmail.com>"); | |
460 | MODULE_DESCRIPTION("USB3503 USB HUB driver"); | |
461 | MODULE_LICENSE("GPL"); |