xref: /openbmc/linux/drivers/usb/misc/usb3503.c (revision 22b6e7f3)
1 // SPDX-License-Identifier: GPL-2.0+
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 
8 #include <linux/clk.h>
9 #include <linux/i2c.h>
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>
17 
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
24 
25 #define USB3503_CFG1		0x06
26 #define USB3503_SELF_BUS_PWR	(1 << 7)
27 
28 #define USB3503_CFG2		0x07
29 #define USB3503_CFG3		0x08
30 #define USB3503_NRD		0x09
31 
32 #define USB3503_PDS		0x0a
33 
34 #define USB3503_SP_ILOCK	0xe7
35 #define USB3503_SPILOCK_CONNECT	(1 << 1)
36 #define USB3503_SPILOCK_CONFIG	(1 << 0)
37 
38 #define USB3503_CFGP		0xee
39 #define USB3503_CLKSUSP		(1 << 7)
40 
41 #define USB3503_RESET		0xff
42 
43 struct usb3503 {
44 	enum usb3503_mode	mode;
45 	struct regmap		*regmap;
46 	struct device		*dev;
47 	struct clk		*clk;
48 	u8	port_off_mask;
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;
54 };
55 
56 static int usb3503_connect(struct usb3503 *hub)
57 {
58 	struct device *dev = hub->dev;
59 	int err;
60 
61 	if (hub->regmap) {
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));
66 		if (err < 0) {
67 			dev_err(dev, "SP_ILOCK failed (%d)\n", err);
68 			return err;
69 		}
70 
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,
74 					hub->port_off_mask,
75 					hub->port_off_mask);
76 			if (err < 0) {
77 				dev_err(dev, "PDS failed (%d)\n", err);
78 				return err;
79 			}
80 		}
81 
82 		/* CFG1 : Set SELF_BUS_PWR, this enables self-powered operation. */
83 		err = regmap_update_bits(hub->regmap, USB3503_CFG1,
84 					 USB3503_SELF_BUS_PWR,
85 					 USB3503_SELF_BUS_PWR);
86 		if (err < 0) {
87 			dev_err(dev, "CFG1 failed (%d)\n", err);
88 			return err;
89 		}
90 
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);
95 		if (err < 0) {
96 			dev_err(dev, "SP_ILOCK failed (%d)\n", err);
97 			return err;
98 		}
99 	}
100 
101 	if (hub->connect)
102 		gpiod_set_value_cansleep(hub->connect, 1);
103 
104 	hub->mode = USB3503_MODE_HUB;
105 	dev_info(dev, "switched to HUB mode\n");
106 
107 	return 0;
108 }
109 
110 static int usb3503_switch_mode(struct usb3503 *hub, enum usb3503_mode mode)
111 {
112 	struct device *dev = hub->dev;
113 	int rst, bypass, conn;
114 
115 	switch (mode) {
116 	case USB3503_MODE_HUB:
117 		conn = 1;
118 		rst = 0;
119 		bypass = 0;
120 		break;
121 	case USB3503_MODE_STANDBY:
122 		conn = 0;
123 		rst = 1;
124 		bypass = 1;
125 		dev_info(dev, "switched to STANDBY mode\n");
126 		break;
127 	case USB3503_MODE_BYPASS:
128 		conn = 0;
129 		rst = 0;
130 		bypass = 1;
131 		break;
132 	default:
133 		dev_err(dev, "unknown mode is requested\n");
134 		return -EINVAL;
135 	}
136 
137 	if (!conn && hub->connect)
138 		gpiod_set_value_cansleep(hub->connect, 0);
139 
140 	if (hub->reset)
141 		gpiod_set_value_cansleep(hub->reset, rst);
142 
143 	if (hub->bypass)
144 		gpiod_set_value_cansleep(hub->bypass, bypass);
145 
146 	if (conn) {
147 		/* Wait T_HUBINIT == 4ms for hub logic to stabilize */
148 		usleep_range(4000, 10000);
149 		return usb3503_connect(hub);
150 	}
151 
152 	return 0;
153 }
154 
155 static const struct regmap_config usb3503_regmap_config = {
156 	.reg_bits = 8,
157 	.val_bits = 8,
158 
159 	.max_register = USB3503_RESET,
160 };
161 
162 static int usb3503_probe(struct usb3503 *hub)
163 {
164 	struct device *dev = hub->dev;
165 	struct usb3503_platform_data *pdata = dev_get_platdata(dev);
166 	struct device_node *np = dev->of_node;
167 	int err;
168 	bool is_clk_enabled = false;
169 	u32 mode = USB3503_MODE_HUB;
170 	const u32 *property;
171 	enum gpiod_flags flags;
172 	int len;
173 
174 	if (pdata) {
175 		hub->port_off_mask	= pdata->port_off_mask;
176 		hub->mode		= pdata->initial_mode;
177 	} else if (np) {
178 		u32 rate = 0;
179 		hub->port_off_mask = 0;
180 
181 		if (!of_property_read_u32(np, "refclk-frequency", &rate)) {
182 			switch (rate) {
183 			case 38400000:
184 			case 26000000:
185 			case 19200000:
186 			case 12000000:
187 				hub->secondary_ref_clk = 0;
188 				break;
189 			case 24000000:
190 			case 27000000:
191 			case 25000000:
192 			case 50000000:
193 				hub->secondary_ref_clk = 1;
194 				break;
195 			default:
196 				dev_err(dev,
197 					"unsupported reference clock rate (%d)\n",
198 					(int) rate);
199 				return -EINVAL;
200 			}
201 		}
202 
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",
206 					PTR_ERR(hub->clk));
207 			return PTR_ERR(hub->clk);
208 		}
209 
210 		if (rate != 0) {
211 			err = clk_set_rate(hub->clk, rate);
212 			if (err) {
213 				dev_err(dev,
214 					"unable to set reference clock rate to %d\n",
215 					(int)rate);
216 				return err;
217 			}
218 		}
219 
220 		err = clk_prepare_enable(hub->clk);
221 		if (err) {
222 			dev_err(dev, "unable to enable reference clock\n");
223 			return err;
224 		}
225 
226 		is_clk_enabled = true;
227 		property = of_get_property(np, "disabled-ports", &len);
228 		if (property && (len / sizeof(u32)) > 0) {
229 			int i;
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);
234 			}
235 		}
236 
237 		of_property_read_u32(np, "initial-mode", &mode);
238 		hub->mode = mode;
239 	}
240 
241 	if (hub->secondary_ref_clk)
242 		flags = GPIOD_OUT_LOW;
243 	else
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);
248 		goto err_clk;
249 	}
250 	if (hub->intn)
251 		gpiod_set_consumer_name(hub->intn, "usb3503 intn");
252 
253 	hub->connect = devm_gpiod_get_optional(dev, "connect", GPIOD_OUT_LOW);
254 	if (IS_ERR(hub->connect)) {
255 		err = PTR_ERR(hub->connect);
256 		goto err_clk;
257 	}
258 	if (hub->connect)
259 		gpiod_set_consumer_name(hub->connect, "usb3503 connect");
260 
261 	hub->bypass = devm_gpiod_get_optional(dev, "bypass", GPIOD_OUT_HIGH);
262 	if (IS_ERR(hub->bypass)) {
263 		err = PTR_ERR(hub->bypass);
264 		goto err_clk;
265 	}
266 	if (hub->bypass)
267 		gpiod_set_consumer_name(hub->bypass, "usb3503 bypass");
268 
269 	hub->reset = devm_gpiod_get_optional(dev, "reset", GPIOD_OUT_HIGH);
270 	if (IS_ERR(hub->reset)) {
271 		err = PTR_ERR(hub->reset);
272 		goto err_clk;
273 	}
274 	if (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");
278 	}
279 
280 	if (hub->port_off_mask && !hub->regmap)
281 		dev_err(dev, "Ports disabled with no control interface\n");
282 
283 	usb3503_switch_mode(hub, hub->mode);
284 
285 	dev_info(dev, "%s: probed in %s mode\n", __func__,
286 			(hub->mode == USB3503_MODE_HUB) ? "hub" : "standby");
287 
288 	return 0;
289 
290 err_clk:
291 	if (is_clk_enabled)
292 		clk_disable_unprepare(hub->clk);
293 	return err;
294 }
295 
296 static int usb3503_i2c_probe(struct i2c_client *i2c)
297 {
298 	struct usb3503 *hub;
299 	int err;
300 
301 	hub = devm_kzalloc(&i2c->dev, sizeof(struct usb3503), GFP_KERNEL);
302 	if (!hub)
303 		return -ENOMEM;
304 
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);
310 		return err;
311 	}
312 	hub->dev = &i2c->dev;
313 
314 	return usb3503_probe(hub);
315 }
316 
317 static void usb3503_i2c_remove(struct i2c_client *i2c)
318 {
319 	struct usb3503 *hub;
320 
321 	hub = i2c_get_clientdata(i2c);
322 	clk_disable_unprepare(hub->clk);
323 }
324 
325 static int usb3503_platform_probe(struct platform_device *pdev)
326 {
327 	struct usb3503 *hub;
328 
329 	hub = devm_kzalloc(&pdev->dev, sizeof(struct usb3503), GFP_KERNEL);
330 	if (!hub)
331 		return -ENOMEM;
332 	hub->dev = &pdev->dev;
333 	platform_set_drvdata(pdev, hub);
334 
335 	return usb3503_probe(hub);
336 }
337 
338 static void usb3503_platform_remove(struct platform_device *pdev)
339 {
340 	struct usb3503 *hub;
341 
342 	hub = platform_get_drvdata(pdev);
343 	clk_disable_unprepare(hub->clk);
344 }
345 
346 static int __maybe_unused usb3503_suspend(struct usb3503 *hub)
347 {
348 	usb3503_switch_mode(hub, USB3503_MODE_STANDBY);
349 	clk_disable_unprepare(hub->clk);
350 
351 	return 0;
352 }
353 
354 static int __maybe_unused usb3503_resume(struct usb3503 *hub)
355 {
356 	clk_prepare_enable(hub->clk);
357 	usb3503_switch_mode(hub, hub->mode);
358 
359 	return 0;
360 }
361 
362 static int __maybe_unused usb3503_i2c_suspend(struct device *dev)
363 {
364 	struct i2c_client *client = to_i2c_client(dev);
365 
366 	return usb3503_suspend(i2c_get_clientdata(client));
367 }
368 
369 static int __maybe_unused usb3503_i2c_resume(struct device *dev)
370 {
371 	struct i2c_client *client = to_i2c_client(dev);
372 
373 	return usb3503_resume(i2c_get_clientdata(client));
374 }
375 
376 static int __maybe_unused usb3503_platform_suspend(struct device *dev)
377 {
378 	return usb3503_suspend(dev_get_drvdata(dev));
379 }
380 
381 static int __maybe_unused usb3503_platform_resume(struct device *dev)
382 {
383 	return usb3503_resume(dev_get_drvdata(dev));
384 }
385 
386 static SIMPLE_DEV_PM_OPS(usb3503_i2c_pm_ops, usb3503_i2c_suspend,
387 		usb3503_i2c_resume);
388 
389 static SIMPLE_DEV_PM_OPS(usb3503_platform_pm_ops, usb3503_platform_suspend,
390 		usb3503_platform_resume);
391 
392 static const struct i2c_device_id usb3503_id[] = {
393 	{ USB3503_I2C_NAME, 0 },
394 	{ }
395 };
396 MODULE_DEVICE_TABLE(i2c, usb3503_id);
397 
398 #ifdef CONFIG_OF
399 static const struct of_device_id usb3503_of_match[] = {
400 	{ .compatible = "smsc,usb3503", },
401 	{ .compatible = "smsc,usb3503a", },
402 	{ .compatible = "smsc,usb3803", },
403 	{},
404 };
405 MODULE_DEVICE_TABLE(of, usb3503_of_match);
406 #endif
407 
408 static struct i2c_driver usb3503_i2c_driver = {
409 	.driver = {
410 		.name = USB3503_I2C_NAME,
411 		.pm = pm_ptr(&usb3503_i2c_pm_ops),
412 		.of_match_table = of_match_ptr(usb3503_of_match),
413 	},
414 	.probe		= usb3503_i2c_probe,
415 	.remove		= usb3503_i2c_remove,
416 	.id_table	= usb3503_id,
417 };
418 
419 static struct platform_driver usb3503_platform_driver = {
420 	.driver = {
421 		.name = USB3503_I2C_NAME,
422 		.of_match_table = of_match_ptr(usb3503_of_match),
423 		.pm = pm_ptr(&usb3503_platform_pm_ops),
424 	},
425 	.probe		= usb3503_platform_probe,
426 	.remove_new	= usb3503_platform_remove,
427 };
428 
429 static int __init usb3503_init(void)
430 {
431 	int err;
432 
433 	err = i2c_add_driver(&usb3503_i2c_driver);
434 	if (err) {
435 		pr_err("usb3503: Failed to register I2C driver: %d\n", err);
436 		return err;
437 	}
438 
439 	err = platform_driver_register(&usb3503_platform_driver);
440 	if (err) {
441 		pr_err("usb3503: Failed to register platform driver: %d\n",
442 		       err);
443 		i2c_del_driver(&usb3503_i2c_driver);
444 		return err;
445 	}
446 
447 	return 0;
448 }
449 module_init(usb3503_init);
450 
451 static void __exit usb3503_exit(void)
452 {
453 	platform_driver_unregister(&usb3503_platform_driver);
454 	i2c_del_driver(&usb3503_i2c_driver);
455 }
456 module_exit(usb3503_exit);
457 
458 MODULE_AUTHOR("Dongjin Kim <tobetter@gmail.com>");
459 MODULE_DESCRIPTION("USB3503 USB HUB driver");
460 MODULE_LICENSE("GPL");
461