]> git.proxmox.com Git - mirror_ubuntu-jammy-kernel.git/blame - drivers/usb/misc/usb3503.c
usb: misc: usb3503: Fix up whitespace
[mirror_ubuntu-jammy-kernel.git] / drivers / usb / misc / usb3503.c
CommitLineData
6a099c63
DK
1/*
2 * Driver for SMSC USB3503 USB 2.0 hub controller driver
3 *
4 * Copyright (c) 2012-2013 Dongjin Kim (tobetter@gmail.com)
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 */
20
21#include <linux/i2c.h>
22#include <linux/gpio.h>
23#include <linux/delay.h>
24#include <linux/slab.h>
25#include <linux/module.h>
eab8050c 26#include <linux/of_gpio.h>
6a099c63
DK
27#include <linux/platform_device.h>
28#include <linux/platform_data/usb3503.h>
29
30#define USB3503_VIDL 0x00
31#define USB3503_VIDM 0x01
32#define USB3503_PIDL 0x02
33#define USB3503_PIDM 0x03
34#define USB3503_DIDL 0x04
35#define USB3503_DIDM 0x05
36
37#define USB3503_CFG1 0x06
38#define USB3503_SELF_BUS_PWR (1 << 7)
39
40#define USB3503_CFG2 0x07
41#define USB3503_CFG3 0x08
42#define USB3503_NRD 0x09
43
44#define USB3503_PDS 0x0a
6a099c63
DK
45
46#define USB3503_SP_ILOCK 0xe7
47#define USB3503_SPILOCK_CONNECT (1 << 1)
48#define USB3503_SPILOCK_CONFIG (1 << 0)
49
50#define USB3503_CFGP 0xee
51#define USB3503_CLKSUSP (1 << 7)
52
53struct usb3503 {
54 enum usb3503_mode mode;
55 struct i2c_client *client;
e8e44a48 56 u8 port_off_mask;
6a099c63
DK
57 int gpio_intn;
58 int gpio_reset;
59 int gpio_connect;
60};
61
62static int usb3503_write_register(struct i2c_client *client,
63 char reg, char data)
64{
65 return i2c_smbus_write_byte_data(client, reg, data);
66}
67
68static int usb3503_read_register(struct i2c_client *client, char reg)
69{
70 return i2c_smbus_read_byte_data(client, reg);
71}
72
73static int usb3503_set_bits(struct i2c_client *client, char reg, char req)
74{
75 int err;
76
77 err = usb3503_read_register(client, reg);
78 if (err < 0)
79 return err;
80
81 err = usb3503_write_register(client, reg, err | req);
82 if (err < 0)
83 return err;
84
85 return 0;
86}
87
88static int usb3503_clear_bits(struct i2c_client *client, char reg, char req)
89{
90 int err;
91
92 err = usb3503_read_register(client, reg);
93 if (err < 0)
94 return err;
95
96 err = usb3503_write_register(client, reg, err & ~req);
97 if (err < 0)
98 return err;
99
100 return 0;
101}
102
103static int usb3503_reset(int gpio_reset, int state)
104{
105 if (gpio_is_valid(gpio_reset))
106 gpio_set_value(gpio_reset, state);
107
108 /* Wait RefClk when RESET_N is released, otherwise Hub will
109 * not transition to Hub Communication Stage.
110 */
111 if (state)
112 msleep(100);
113
114 return 0;
115}
116
117static int usb3503_switch_mode(struct usb3503 *hub, enum usb3503_mode mode)
118{
119 struct i2c_client *i2c = hub->client;
120 int err = 0;
121
122 switch (mode) {
123 case USB3503_MODE_HUB:
124 usb3503_reset(hub->gpio_reset, 1);
125
126 /* SP_ILOCK: set connect_n, config_n for config */
127 err = usb3503_write_register(i2c, USB3503_SP_ILOCK,
128 (USB3503_SPILOCK_CONNECT
129 | USB3503_SPILOCK_CONFIG));
130 if (err < 0) {
131 dev_err(&i2c->dev, "SP_ILOCK failed (%d)\n", err);
132 goto err_hubmode;
133 }
134
e8e44a48
DK
135 /* PDS : Disable For Self Powered Operation */
136 if (hub->port_off_mask) {
137 err = usb3503_set_bits(i2c, USB3503_PDS,
138 hub->port_off_mask);
139 if (err < 0) {
140 dev_err(&i2c->dev, "PDS failed (%d)\n", err);
141 goto err_hubmode;
142 }
6a099c63
DK
143 }
144
145 /* CFG1 : SELF_BUS_PWR -> Self-Powerd operation */
146 err = usb3503_set_bits(i2c, USB3503_CFG1, USB3503_SELF_BUS_PWR);
147 if (err < 0) {
148 dev_err(&i2c->dev, "CFG1 failed (%d)\n", err);
149 goto err_hubmode;
150 }
151
152 /* SP_LOCK: clear connect_n, config_n for hub connect */
153 err = usb3503_clear_bits(i2c, USB3503_SP_ILOCK,
154 (USB3503_SPILOCK_CONNECT
155 | USB3503_SPILOCK_CONFIG));
156 if (err < 0) {
157 dev_err(&i2c->dev, "SP_ILOCK failed (%d)\n", err);
158 goto err_hubmode;
159 }
160
161 hub->mode = mode;
162 dev_info(&i2c->dev, "switched to HUB mode\n");
163 break;
164
165 case USB3503_MODE_STANDBY:
166 usb3503_reset(hub->gpio_reset, 0);
167
168 hub->mode = mode;
169 dev_info(&i2c->dev, "switched to STANDBY mode\n");
170 break;
171
172 default:
173 dev_err(&i2c->dev, "unknown mode is request\n");
174 err = -EINVAL;
175 break;
176 }
177
178err_hubmode:
179 return err;
180}
181
74ff31b8 182static int usb3503_probe(struct i2c_client *i2c, const struct i2c_device_id *id)
6a099c63
DK
183{
184 struct usb3503_platform_data *pdata = i2c->dev.platform_data;
eab8050c 185 struct device_node *np = i2c->dev.of_node;
6a099c63 186 struct usb3503 *hub;
7a8ea7eb 187 int err = -ENOMEM;
8ab03dd4 188 u32 mode = USB3503_MODE_UNKNOWN;
e8b58b49
DK
189 const u32 *property;
190 int len;
6a099c63
DK
191
192 hub = kzalloc(sizeof(struct usb3503), GFP_KERNEL);
193 if (!hub) {
194 dev_err(&i2c->dev, "private data alloc fail\n");
7a8ea7eb 195 return err;
6a099c63
DK
196 }
197
198 i2c_set_clientdata(i2c, hub);
199 hub->client = i2c;
200
eab8050c 201 if (pdata) {
e8e44a48 202 hub->port_off_mask = pdata->port_off_mask;
6a099c63
DK
203 hub->gpio_intn = pdata->gpio_intn;
204 hub->gpio_connect = pdata->gpio_connect;
205 hub->gpio_reset = pdata->gpio_reset;
206 hub->mode = pdata->initial_mode;
eab8050c 207 } else if (np) {
e8b58b49
DK
208 hub->port_off_mask = 0;
209
210 property = of_get_property(np, "disabled-ports", &len);
211 if (property && (len / sizeof(u32)) > 0) {
212 int i;
213 for (i = 0; i < len / sizeof(u32); i++) {
214 u32 port = be32_to_cpu(property[i]);
215 if ((1 <= port) && (port <= 3))
216 hub->port_off_mask |= (1 << port);
217 }
218 }
219
eab8050c
DK
220 hub->gpio_intn = of_get_named_gpio(np, "connect-gpios", 0);
221 if (hub->gpio_intn == -EPROBE_DEFER)
222 return -EPROBE_DEFER;
223 hub->gpio_connect = of_get_named_gpio(np, "intn-gpios", 0);
224 if (hub->gpio_connect == -EPROBE_DEFER)
225 return -EPROBE_DEFER;
ccf92c94 226 hub->gpio_reset = of_get_named_gpio(np, "reset-gpios", 0);
eab8050c
DK
227 if (hub->gpio_reset == -EPROBE_DEFER)
228 return -EPROBE_DEFER;
229 of_property_read_u32(np, "initial-mode", &mode);
230 hub->mode = mode;
6a099c63
DK
231 }
232
233 if (gpio_is_valid(hub->gpio_intn)) {
234 err = gpio_request_one(hub->gpio_intn,
235 GPIOF_OUT_INIT_HIGH, "usb3503 intn");
236 if (err) {
237 dev_err(&i2c->dev,
238 "unable to request GPIO %d as connect pin (%d)\n",
239 hub->gpio_intn, err);
7a8ea7eb 240 goto err_out;
6a099c63
DK
241 }
242 }
243
244 if (gpio_is_valid(hub->gpio_connect)) {
245 err = gpio_request_one(hub->gpio_connect,
246 GPIOF_OUT_INIT_HIGH, "usb3503 connect");
247 if (err) {
248 dev_err(&i2c->dev,
249 "unable to request GPIO %d as connect pin (%d)\n",
250 hub->gpio_connect, err);
251 goto err_gpio_connect;
252 }
253 }
254
255 if (gpio_is_valid(hub->gpio_reset)) {
256 err = gpio_request_one(hub->gpio_reset,
257 GPIOF_OUT_INIT_LOW, "usb3503 reset");
258 if (err) {
259 dev_err(&i2c->dev,
260 "unable to request GPIO %d as reset pin (%d)\n",
261 hub->gpio_reset, err);
262 goto err_gpio_reset;
263 }
264 }
265
eab8050c 266 usb3503_switch_mode(hub, hub->mode);
6a099c63
DK
267
268 dev_info(&i2c->dev, "%s: probed on %s mode\n", __func__,
269 (hub->mode == USB3503_MODE_HUB) ? "hub" : "standby");
270
271 return 0;
272
273err_gpio_reset:
274 if (gpio_is_valid(hub->gpio_connect))
275 gpio_free(hub->gpio_connect);
276err_gpio_connect:
277 if (gpio_is_valid(hub->gpio_intn))
278 gpio_free(hub->gpio_intn);
7a8ea7eb 279err_out:
6a099c63
DK
280 kfree(hub);
281
282 return err;
283}
284
285static int usb3503_remove(struct i2c_client *i2c)
286{
287 struct usb3503 *hub = i2c_get_clientdata(i2c);
288
289 if (gpio_is_valid(hub->gpio_intn))
290 gpio_free(hub->gpio_intn);
291 if (gpio_is_valid(hub->gpio_connect))
292 gpio_free(hub->gpio_connect);
293 if (gpio_is_valid(hub->gpio_reset))
294 gpio_free(hub->gpio_reset);
295
296 kfree(hub);
297
298 return 0;
299}
300
301static const struct i2c_device_id usb3503_id[] = {
302 { USB3503_I2C_NAME, 0 },
303 { }
304};
305MODULE_DEVICE_TABLE(i2c, usb3503_id);
306
eab8050c
DK
307#ifdef CONFIG_OF
308static const struct of_device_id usb3503_of_match[] = {
309 { .compatible = "smsc,usb3503", },
310 {},
311};
312MODULE_DEVICE_TABLE(of, usb3503_of_match);
313#endif
314
6a099c63
DK
315static struct i2c_driver usb3503_driver = {
316 .driver = {
317 .name = USB3503_I2C_NAME,
eab8050c 318 .of_match_table = of_match_ptr(usb3503_of_match),
6a099c63
DK
319 },
320 .probe = usb3503_probe,
321 .remove = usb3503_remove,
322 .id_table = usb3503_id,
323};
324
8244ac04 325module_i2c_driver(usb3503_driver);
6a099c63
DK
326
327MODULE_AUTHOR("Dongjin Kim <tobetter@gmail.com>");
328MODULE_DESCRIPTION("USB3503 USB HUB driver");
329MODULE_LICENSE("GPL");