xref: /linux/drivers/usb/misc/usb4604.c (revision 5fd54ace4721fc5ce2bb5aef6318fcf17f421460)
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Driver for SMSC USB4604 USB HSIC 4-port 2.0 hub controller driver
4  * Based on usb3503 driver
5  *
6  * Copyright (c) 2012-2013 Dongjin Kim (tobetter@gmail.com)
7  * Copyright (c) 2016 Linaro Ltd.
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  */
19 
20 #include <linux/i2c.h>
21 #include <linux/delay.h>
22 #include <linux/slab.h>
23 #include <linux/module.h>
24 #include <linux/gpio/consumer.h>
25 
26 enum usb4604_mode {
27 	USB4604_MODE_UNKNOWN,
28 	USB4604_MODE_HUB,
29 	USB4604_MODE_STANDBY,
30 };
31 
32 struct usb4604 {
33 	enum usb4604_mode	mode;
34 	struct device		*dev;
35 	struct gpio_desc	*gpio_reset;
36 };
37 
38 static void usb4604_reset(struct usb4604 *hub, int state)
39 {
40 	gpiod_set_value_cansleep(hub->gpio_reset, state);
41 
42 	/* Wait for i2c logic to come up */
43 	if (state)
44 		msleep(250);
45 }
46 
47 static int usb4604_connect(struct usb4604 *hub)
48 {
49 	struct device *dev = hub->dev;
50 	struct i2c_client *client = to_i2c_client(dev);
51 	int err;
52 	u8 connect_cmd[] = { 0xaa, 0x55, 0x00 };
53 
54 	usb4604_reset(hub, 1);
55 
56 	err = i2c_master_send(client, connect_cmd, ARRAY_SIZE(connect_cmd));
57 	if (err < 0) {
58 		usb4604_reset(hub, 0);
59 		return err;
60 	}
61 
62 	hub->mode = USB4604_MODE_HUB;
63 	dev_dbg(dev, "switched to HUB mode\n");
64 
65 	return 0;
66 }
67 
68 static int usb4604_switch_mode(struct usb4604 *hub, enum usb4604_mode mode)
69 {
70 	struct device *dev = hub->dev;
71 	int err = 0;
72 
73 	switch (mode) {
74 	case USB4604_MODE_HUB:
75 		err = usb4604_connect(hub);
76 		break;
77 
78 	case USB4604_MODE_STANDBY:
79 		usb4604_reset(hub, 0);
80 		dev_dbg(dev, "switched to STANDBY mode\n");
81 		break;
82 
83 	default:
84 		dev_err(dev, "unknown mode is requested\n");
85 		err = -EINVAL;
86 		break;
87 	}
88 
89 	return err;
90 }
91 
92 static int usb4604_probe(struct usb4604 *hub)
93 {
94 	struct device *dev = hub->dev;
95 	struct device_node *np = dev->of_node;
96 	struct gpio_desc *gpio;
97 	u32 mode = USB4604_MODE_HUB;
98 
99 	gpio = devm_gpiod_get_optional(dev, "reset", GPIOD_OUT_LOW);
100 	if (IS_ERR(gpio))
101 		return PTR_ERR(gpio);
102 	hub->gpio_reset = gpio;
103 
104 	if (of_property_read_u32(np, "initial-mode", &hub->mode))
105 		hub->mode = mode;
106 
107 	return usb4604_switch_mode(hub, hub->mode);
108 }
109 
110 static int usb4604_i2c_probe(struct i2c_client *i2c,
111 			     const struct i2c_device_id *id)
112 {
113 	struct usb4604 *hub;
114 
115 	hub = devm_kzalloc(&i2c->dev, sizeof(*hub), GFP_KERNEL);
116 	if (!hub)
117 		return -ENOMEM;
118 
119 	i2c_set_clientdata(i2c, hub);
120 	hub->dev = &i2c->dev;
121 
122 	return usb4604_probe(hub);
123 }
124 
125 #ifdef CONFIG_PM_SLEEP
126 static int usb4604_i2c_suspend(struct device *dev)
127 {
128 	struct i2c_client *client = to_i2c_client(dev);
129 	struct usb4604 *hub = i2c_get_clientdata(client);
130 
131 	usb4604_switch_mode(hub, USB4604_MODE_STANDBY);
132 
133 	return 0;
134 }
135 
136 static int usb4604_i2c_resume(struct device *dev)
137 {
138 	struct i2c_client *client = to_i2c_client(dev);
139 	struct usb4604 *hub = i2c_get_clientdata(client);
140 
141 	usb4604_switch_mode(hub, hub->mode);
142 
143 	return 0;
144 }
145 #endif
146 
147 static SIMPLE_DEV_PM_OPS(usb4604_i2c_pm_ops, usb4604_i2c_suspend,
148 		usb4604_i2c_resume);
149 
150 static const struct i2c_device_id usb4604_id[] = {
151 	{ "usb4604", 0 },
152 	{ }
153 };
154 MODULE_DEVICE_TABLE(i2c, usb4604_id);
155 
156 #ifdef CONFIG_OF
157 static const struct of_device_id usb4604_of_match[] = {
158 	{ .compatible = "smsc,usb4604" },
159 	{}
160 };
161 MODULE_DEVICE_TABLE(of, usb4604_of_match);
162 #endif
163 
164 static struct i2c_driver usb4604_i2c_driver = {
165 	.driver = {
166 		.name = "usb4604",
167 		.pm = &usb4604_i2c_pm_ops,
168 		.of_match_table = of_match_ptr(usb4604_of_match),
169 	},
170 	.probe		= usb4604_i2c_probe,
171 	.id_table	= usb4604_id,
172 };
173 module_i2c_driver(usb4604_i2c_driver);
174 
175 MODULE_DESCRIPTION("USB4604 USB HUB driver");
176 MODULE_LICENSE("GPL v2");
177