xref: /linux/drivers/usb/misc/usb3503.c (revision 5fd54ace4721fc5ce2bb5aef6318fcf17f421460)
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  * 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 
22 #include <linux/clk.h>
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>
28 #include <linux/of_gpio.h>
29 #include <linux/platform_device.h>
30 #include <linux/platform_data/usb3503.h>
31 #include <linux/regmap.h>
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
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 
56 #define USB3503_RESET		0xff
57 
58 struct usb3503 {
59 	enum usb3503_mode	mode;
60 	struct regmap		*regmap;
61 	struct device		*dev;
62 	struct clk		*clk;
63 	u8	port_off_mask;
64 	int	gpio_intn;
65 	int	gpio_reset;
66 	int	gpio_connect;
67 	bool	secondary_ref_clk;
68 };
69 
70 static int usb3503_reset(struct usb3503 *hub, int state)
71 {
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);
77 
78 	/* Wait T_HUBINIT == 4ms for hub logic to stabilize */
79 	if (state)
80 		usleep_range(4000, 10000);
81 
82 	return 0;
83 }
84 
85 static int usb3503_connect(struct usb3503 *hub)
86 {
87 	struct device *dev = hub->dev;
88 	int err;
89 
90 	usb3503_reset(hub, 1);
91 
92 	if (hub->regmap) {
93 		/* SP_ILOCK: set connect_n, config_n for config */
94 		err = regmap_write(hub->regmap, USB3503_SP_ILOCK,
95 			   (USB3503_SPILOCK_CONNECT
96 				 | USB3503_SPILOCK_CONFIG));
97 		if (err < 0) {
98 			dev_err(dev, "SP_ILOCK failed (%d)\n", err);
99 			return err;
100 		}
101 
102 		/* PDS : Set the ports which are disabled in self-powered mode. */
103 		if (hub->port_off_mask) {
104 			err = regmap_update_bits(hub->regmap, USB3503_PDS,
105 					hub->port_off_mask,
106 					hub->port_off_mask);
107 			if (err < 0) {
108 				dev_err(dev, "PDS failed (%d)\n", err);
109 				return err;
110 			}
111 		}
112 
113 		/* CFG1 : Set SELF_BUS_PWR, this enables self-powered operation. */
114 		err = regmap_update_bits(hub->regmap, USB3503_CFG1,
115 					 USB3503_SELF_BUS_PWR,
116 					 USB3503_SELF_BUS_PWR);
117 		if (err < 0) {
118 			dev_err(dev, "CFG1 failed (%d)\n", err);
119 			return err;
120 		}
121 
122 		/* SP_LOCK: clear connect_n, config_n for hub connect */
123 		err = regmap_update_bits(hub->regmap, USB3503_SP_ILOCK,
124 					 (USB3503_SPILOCK_CONNECT
125 					  | USB3503_SPILOCK_CONFIG), 0);
126 		if (err < 0) {
127 			dev_err(dev, "SP_ILOCK failed (%d)\n", err);
128 			return err;
129 		}
130 	}
131 
132 	if (gpio_is_valid(hub->gpio_connect))
133 		gpio_set_value_cansleep(hub->gpio_connect, 1);
134 
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);
149 		break;
150 
151 	case USB3503_MODE_STANDBY:
152 		usb3503_reset(hub, 0);
153 		dev_info(dev, "switched to STANDBY mode\n");
154 		break;
155 
156 	default:
157 		dev_err(dev, "unknown mode is requested\n");
158 		err = -EINVAL;
159 		break;
160 	}
161 
162 	return err;
163 }
164 
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 
172 static int usb3503_probe(struct usb3503 *hub)
173 {
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;
178 	u32 mode = USB3503_MODE_HUB;
179 	const u32 *property;
180 	int len;
181 
182 	if (pdata) {
183 		hub->port_off_mask	= pdata->port_off_mask;
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;
188 	} else if (np) {
189 		struct clk *clk;
190 		u32 rate = 0;
191 		hub->port_off_mask = 0;
192 
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 
215 		clk = devm_clk_get(dev, "refclk");
216 		if (IS_ERR(clk) && PTR_ERR(clk) != -ENOENT) {
217 			dev_err(dev, "unable to request refclk (%ld)\n",
218 					PTR_ERR(clk));
219 			return PTR_ERR(clk);
220 		}
221 
222 		if (!IS_ERR(clk)) {
223 			hub->clk = clk;
224 
225 			if (rate != 0) {
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 
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 
253 		hub->gpio_intn	= of_get_named_gpio(np, "intn-gpios", 0);
254 		if (hub->gpio_intn == -EPROBE_DEFER)
255 			return -EPROBE_DEFER;
256 		hub->gpio_connect = of_get_named_gpio(np, "connect-gpios", 0);
257 		if (hub->gpio_connect == -EPROBE_DEFER)
258 			return -EPROBE_DEFER;
259 		hub->gpio_reset = of_get_named_gpio(np, "reset-gpios", 0);
260 		if (hub->gpio_reset == -EPROBE_DEFER)
261 			return -EPROBE_DEFER;
262 		of_property_read_u32(np, "initial-mode", &mode);
263 		hub->mode = mode;
264 	}
265 
266 	if (hub->port_off_mask && !hub->regmap)
267 		dev_err(dev, "Ports disabled with no control interface\n");
268 
269 	if (gpio_is_valid(hub->gpio_intn)) {
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");
274 		if (err) {
275 			dev_err(dev,
276 				"unable to request GPIO %d as interrupt pin (%d)\n",
277 				hub->gpio_intn, err);
278 			return err;
279 		}
280 	}
281 
282 	if (gpio_is_valid(hub->gpio_connect)) {
283 		err = devm_gpio_request_one(dev, hub->gpio_connect,
284 				GPIOF_OUT_INIT_LOW, "usb3503 connect");
285 		if (err) {
286 			dev_err(dev,
287 				"unable to request GPIO %d as connect pin (%d)\n",
288 				hub->gpio_connect, err);
289 			return err;
290 		}
291 	}
292 
293 	if (gpio_is_valid(hub->gpio_reset)) {
294 		err = devm_gpio_request_one(dev, hub->gpio_reset,
295 				GPIOF_OUT_INIT_LOW, "usb3503 reset");
296 		if (err) {
297 			dev_err(dev,
298 				"unable to request GPIO %d as reset pin (%d)\n",
299 				hub->gpio_reset, err);
300 			return err;
301 		}
302 	}
303 
304 	usb3503_switch_mode(hub, hub->mode);
305 
306 	dev_info(dev, "%s: probed in %s mode\n", __func__,
307 			(hub->mode == USB3503_MODE_HUB) ? "hub" : "standby");
308 
309 	return 0;
310 }
311 
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);
319 	if (!hub)
320 		return -ENOMEM;
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 
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 
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);
350 	if (!hub)
351 		return -ENOMEM;
352 	hub->dev = &pdev->dev;
353 	platform_set_drvdata(pdev, hub);
354 
355 	return usb3503_probe(hub);
356 }
357 
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 
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 
400 static const struct i2c_device_id usb3503_id[] = {
401 	{ USB3503_I2C_NAME, 0 },
402 	{ }
403 };
404 MODULE_DEVICE_TABLE(i2c, usb3503_id);
405 
406 #ifdef CONFIG_OF
407 static const struct of_device_id usb3503_of_match[] = {
408 	{ .compatible = "smsc,usb3503", },
409 	{ .compatible = "smsc,usb3503a", },
410 	{},
411 };
412 MODULE_DEVICE_TABLE(of, usb3503_of_match);
413 #endif
414 
415 static struct i2c_driver usb3503_i2c_driver = {
416 	.driver = {
417 		.name = USB3503_I2C_NAME,
418 		.pm = &usb3503_i2c_pm_ops,
419 		.of_match_table = of_match_ptr(usb3503_of_match),
420 	},
421 	.probe		= usb3503_i2c_probe,
422 	.remove		= usb3503_i2c_remove,
423 	.id_table	= usb3503_id,
424 };
425 
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),
430 	},
431 	.probe		= usb3503_platform_probe,
432 	.remove		= usb3503_platform_remove,
433 };
434 
435 static int __init usb3503_init(void)
436 {
437 	int err;
438 
439 	err = i2c_add_driver(&usb3503_i2c_driver);
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);
458 
459 MODULE_AUTHOR("Dongjin Kim <tobetter@gmail.com>");
460 MODULE_DESCRIPTION("USB3503 USB HUB driver");
461 MODULE_LICENSE("GPL");
462