1 // SPDX-License-Identifier: GPL-2.0+
3 * Driver for SMSC USB3503 USB 2.0 hub controller driver
5 * Copyright (c) 2012-2013 Dongjin Kim (tobetter@gmail.com)
10 #include <linux/gpio/consumer.h>
11 #include <linux/delay.h>
12 #include <linux/slab.h>
13 #include <linux/module.h>
14 #include <linux/platform_device.h>
15 #include <linux/platform_data/usb3503.h>
16 #include <linux/regmap.h>
18 #define USB3503_VIDL 0x00
19 #define USB3503_VIDM 0x01
20 #define USB3503_PIDL 0x02
21 #define USB3503_PIDM 0x03
22 #define USB3503_DIDL 0x04
23 #define USB3503_DIDM 0x05
25 #define USB3503_CFG1 0x06
26 #define USB3503_SELF_BUS_PWR (1 << 7)
28 #define USB3503_CFG2 0x07
29 #define USB3503_CFG3 0x08
30 #define USB3503_NRD 0x09
32 #define USB3503_PDS 0x0a
34 #define USB3503_SP_ILOCK 0xe7
35 #define USB3503_SPILOCK_CONNECT (1 << 1)
36 #define USB3503_SPILOCK_CONFIG (1 << 0)
38 #define USB3503_CFGP 0xee
39 #define USB3503_CLKSUSP (1 << 7)
41 #define USB3503_RESET 0xff
44 enum usb3503_mode mode
;
45 struct regmap
*regmap
;
49 struct gpio_desc
*bypass
;
50 struct gpio_desc
*intn
;
51 struct gpio_desc
*reset
;
52 struct gpio_desc
*connect
;
53 bool secondary_ref_clk
;
56 static int usb3503_connect(struct usb3503
*hub
)
58 struct device
*dev
= hub
->dev
;
62 /* SP_ILOCK: set connect_n, config_n for config */
63 err
= regmap_write(hub
->regmap
, USB3503_SP_ILOCK
,
64 (USB3503_SPILOCK_CONNECT
65 | USB3503_SPILOCK_CONFIG
));
67 dev_err(dev
, "SP_ILOCK failed (%d)\n", err
);
71 /* PDS : Set the ports which are disabled in self-powered mode. */
72 if (hub
->port_off_mask
) {
73 err
= regmap_update_bits(hub
->regmap
, USB3503_PDS
,
77 dev_err(dev
, "PDS failed (%d)\n", err
);
82 /* CFG1 : Set SELF_BUS_PWR, this enables self-powered operation. */
83 err
= regmap_update_bits(hub
->regmap
, USB3503_CFG1
,
85 USB3503_SELF_BUS_PWR
);
87 dev_err(dev
, "CFG1 failed (%d)\n", err
);
91 /* SP_LOCK: clear connect_n, config_n for hub connect */
92 err
= regmap_update_bits(hub
->regmap
, USB3503_SP_ILOCK
,
93 (USB3503_SPILOCK_CONNECT
94 | USB3503_SPILOCK_CONFIG
), 0);
96 dev_err(dev
, "SP_ILOCK failed (%d)\n", err
);
102 gpiod_set_value_cansleep(hub
->connect
, 1);
104 hub
->mode
= USB3503_MODE_HUB
;
105 dev_info(dev
, "switched to HUB mode\n");
110 static int usb3503_switch_mode(struct usb3503
*hub
, enum usb3503_mode mode
)
112 struct device
*dev
= hub
->dev
;
113 int rst
, bypass
, conn
;
116 case USB3503_MODE_HUB
:
121 case USB3503_MODE_STANDBY
:
125 dev_info(dev
, "switched to STANDBY mode\n");
127 case USB3503_MODE_BYPASS
:
133 dev_err(dev
, "unknown mode is requested\n");
137 if (!conn
&& hub
->connect
)
138 gpiod_set_value_cansleep(hub
->connect
, 0);
141 gpiod_set_value_cansleep(hub
->reset
, rst
);
144 gpiod_set_value_cansleep(hub
->bypass
, bypass
);
147 /* Wait T_HUBINIT == 4ms for hub logic to stabilize */
148 usleep_range(4000, 10000);
149 return usb3503_connect(hub
);
155 static const struct regmap_config usb3503_regmap_config
= {
159 .max_register
= USB3503_RESET
,
162 static int usb3503_probe(struct usb3503
*hub
)
164 struct device
*dev
= hub
->dev
;
165 struct usb3503_platform_data
*pdata
= dev_get_platdata(dev
);
166 struct device_node
*np
= dev
->of_node
;
168 bool is_clk_enabled
= false;
169 u32 mode
= USB3503_MODE_HUB
;
171 enum gpiod_flags flags
;
175 hub
->port_off_mask
= pdata
->port_off_mask
;
176 hub
->mode
= pdata
->initial_mode
;
179 hub
->port_off_mask
= 0;
181 if (!of_property_read_u32(np
, "refclk-frequency", &rate
)) {
187 hub
->secondary_ref_clk
= 0;
193 hub
->secondary_ref_clk
= 1;
197 "unsupported reference clock rate (%d)\n",
203 hub
->clk
= devm_clk_get_optional(dev
, "refclk");
204 if (IS_ERR(hub
->clk
)) {
205 dev_err(dev
, "unable to request refclk (%ld)\n",
207 return PTR_ERR(hub
->clk
);
211 err
= clk_set_rate(hub
->clk
, rate
);
214 "unable to set reference clock rate to %d\n",
220 err
= clk_prepare_enable(hub
->clk
);
222 dev_err(dev
, "unable to enable reference clock\n");
226 is_clk_enabled
= true;
227 property
= of_get_property(np
, "disabled-ports", &len
);
228 if (property
&& (len
/ sizeof(u32
)) > 0) {
230 for (i
= 0; i
< len
/ sizeof(u32
); i
++) {
231 u32 port
= be32_to_cpu(property
[i
]);
232 if ((1 <= port
) && (port
<= 3))
233 hub
->port_off_mask
|= (1 << port
);
237 of_property_read_u32(np
, "initial-mode", &mode
);
241 if (hub
->secondary_ref_clk
)
242 flags
= GPIOD_OUT_LOW
;
244 flags
= GPIOD_OUT_HIGH
;
245 hub
->intn
= devm_gpiod_get_optional(dev
, "intn", flags
);
246 if (IS_ERR(hub
->intn
)) {
247 err
= PTR_ERR(hub
->intn
);
251 gpiod_set_consumer_name(hub
->intn
, "usb3503 intn");
253 hub
->connect
= devm_gpiod_get_optional(dev
, "connect", GPIOD_OUT_LOW
);
254 if (IS_ERR(hub
->connect
)) {
255 err
= PTR_ERR(hub
->connect
);
259 gpiod_set_consumer_name(hub
->connect
, "usb3503 connect");
261 hub
->bypass
= devm_gpiod_get_optional(dev
, "bypass", GPIOD_OUT_HIGH
);
262 if (IS_ERR(hub
->bypass
)) {
263 err
= PTR_ERR(hub
->bypass
);
267 gpiod_set_consumer_name(hub
->bypass
, "usb3503 bypass");
269 hub
->reset
= devm_gpiod_get_optional(dev
, "reset", GPIOD_OUT_HIGH
);
270 if (IS_ERR(hub
->reset
)) {
271 err
= PTR_ERR(hub
->reset
);
275 /* Datasheet defines a hardware reset to be at least 100us */
276 usleep_range(100, 10000);
277 gpiod_set_consumer_name(hub
->reset
, "usb3503 reset");
280 if (hub
->port_off_mask
&& !hub
->regmap
)
281 dev_err(dev
, "Ports disabled with no control interface\n");
283 usb3503_switch_mode(hub
, hub
->mode
);
285 dev_info(dev
, "%s: probed in %s mode\n", __func__
,
286 (hub
->mode
== USB3503_MODE_HUB
) ? "hub" : "standby");
292 clk_disable_unprepare(hub
->clk
);
296 static int usb3503_i2c_probe(struct i2c_client
*i2c
)
301 hub
= devm_kzalloc(&i2c
->dev
, sizeof(struct usb3503
), GFP_KERNEL
);
305 i2c_set_clientdata(i2c
, hub
);
306 hub
->regmap
= devm_regmap_init_i2c(i2c
, &usb3503_regmap_config
);
307 if (IS_ERR(hub
->regmap
)) {
308 err
= PTR_ERR(hub
->regmap
);
309 dev_err(&i2c
->dev
, "Failed to initialise regmap: %d\n", err
);
312 hub
->dev
= &i2c
->dev
;
314 return usb3503_probe(hub
);
317 static void usb3503_i2c_remove(struct i2c_client
*i2c
)
321 hub
= i2c_get_clientdata(i2c
);
322 clk_disable_unprepare(hub
->clk
);
325 static int usb3503_platform_probe(struct platform_device
*pdev
)
329 hub
= devm_kzalloc(&pdev
->dev
, sizeof(struct usb3503
), GFP_KERNEL
);
332 hub
->dev
= &pdev
->dev
;
333 platform_set_drvdata(pdev
, hub
);
335 return usb3503_probe(hub
);
338 static void usb3503_platform_remove(struct platform_device
*pdev
)
342 hub
= platform_get_drvdata(pdev
);
343 clk_disable_unprepare(hub
->clk
);
346 static int __maybe_unused
usb3503_suspend(struct usb3503
*hub
)
348 usb3503_switch_mode(hub
, USB3503_MODE_STANDBY
);
349 clk_disable_unprepare(hub
->clk
);
354 static int __maybe_unused
usb3503_resume(struct usb3503
*hub
)
356 clk_prepare_enable(hub
->clk
);
357 usb3503_switch_mode(hub
, hub
->mode
);
362 static int __maybe_unused
usb3503_i2c_suspend(struct device
*dev
)
364 struct i2c_client
*client
= to_i2c_client(dev
);
366 return usb3503_suspend(i2c_get_clientdata(client
));
369 static int __maybe_unused
usb3503_i2c_resume(struct device
*dev
)
371 struct i2c_client
*client
= to_i2c_client(dev
);
373 return usb3503_resume(i2c_get_clientdata(client
));
376 static int __maybe_unused
usb3503_platform_suspend(struct device
*dev
)
378 return usb3503_suspend(dev_get_drvdata(dev
));
381 static int __maybe_unused
usb3503_platform_resume(struct device
*dev
)
383 return usb3503_resume(dev_get_drvdata(dev
));
386 static SIMPLE_DEV_PM_OPS(usb3503_i2c_pm_ops
, usb3503_i2c_suspend
,
389 static SIMPLE_DEV_PM_OPS(usb3503_platform_pm_ops
, usb3503_platform_suspend
,
390 usb3503_platform_resume
);
392 static const struct i2c_device_id usb3503_id
[] = {
393 { USB3503_I2C_NAME
},
396 MODULE_DEVICE_TABLE(i2c
, usb3503_id
);
399 static const struct of_device_id usb3503_of_match
[] = {
400 { .compatible
= "smsc,usb3503", },
401 { .compatible
= "smsc,usb3503a", },
402 { .compatible
= "smsc,usb3803", },
405 MODULE_DEVICE_TABLE(of
, usb3503_of_match
);
408 static struct i2c_driver usb3503_i2c_driver
= {
410 .name
= USB3503_I2C_NAME
,
411 .pm
= pm_ptr(&usb3503_i2c_pm_ops
),
412 .of_match_table
= of_match_ptr(usb3503_of_match
),
414 .probe
= usb3503_i2c_probe
,
415 .remove
= usb3503_i2c_remove
,
416 .id_table
= usb3503_id
,
419 static struct platform_driver usb3503_platform_driver
= {
421 .name
= USB3503_I2C_NAME
,
422 .of_match_table
= of_match_ptr(usb3503_of_match
),
423 .pm
= pm_ptr(&usb3503_platform_pm_ops
),
425 .probe
= usb3503_platform_probe
,
426 .remove
= usb3503_platform_remove
,
429 static int __init
usb3503_init(void)
433 err
= i2c_add_driver(&usb3503_i2c_driver
);
435 pr_err("usb3503: Failed to register I2C driver: %d\n", err
);
439 err
= platform_driver_register(&usb3503_platform_driver
);
441 pr_err("usb3503: Failed to register platform driver: %d\n",
443 i2c_del_driver(&usb3503_i2c_driver
);
449 module_init(usb3503_init
);
451 static void __exit
usb3503_exit(void)
453 platform_driver_unregister(&usb3503_platform_driver
);
454 i2c_del_driver(&usb3503_i2c_driver
);
456 module_exit(usb3503_exit
);
458 MODULE_AUTHOR("Dongjin Kim <tobetter@gmail.com>");
459 MODULE_DESCRIPTION("USB3503 USB HUB driver");
460 MODULE_LICENSE("GPL");