blob: 796d58c95b63dd118addf6d4256b5d90c843cfd9 [file] [log] [blame]
Dongjin Kim6a099c62012-12-08 05:18:44 +09001/*
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>
26#include <linux/platform_device.h>
27#include <linux/platform_data/usb3503.h>
28
29#define USB3503_VIDL 0x00
30#define USB3503_VIDM 0x01
31#define USB3503_PIDL 0x02
32#define USB3503_PIDM 0x03
33#define USB3503_DIDL 0x04
34#define USB3503_DIDM 0x05
35
36#define USB3503_CFG1 0x06
37#define USB3503_SELF_BUS_PWR (1 << 7)
38
39#define USB3503_CFG2 0x07
40#define USB3503_CFG3 0x08
41#define USB3503_NRD 0x09
42
43#define USB3503_PDS 0x0a
44#define USB3503_PORT1 (1 << 1)
45#define USB3503_PORT2 (1 << 2)
46#define USB3503_PORT3 (1 << 3)
47
48#define USB3503_SP_ILOCK 0xe7
49#define USB3503_SPILOCK_CONNECT (1 << 1)
50#define USB3503_SPILOCK_CONFIG (1 << 0)
51
52#define USB3503_CFGP 0xee
53#define USB3503_CLKSUSP (1 << 7)
54
55struct usb3503 {
56 enum usb3503_mode mode;
57 struct i2c_client *client;
58 int gpio_intn;
59 int gpio_reset;
60 int gpio_connect;
61};
62
63static int usb3503_write_register(struct i2c_client *client,
64 char reg, char data)
65{
66 return i2c_smbus_write_byte_data(client, reg, data);
67}
68
69static int usb3503_read_register(struct i2c_client *client, char reg)
70{
71 return i2c_smbus_read_byte_data(client, reg);
72}
73
74static int usb3503_set_bits(struct i2c_client *client, char reg, char req)
75{
76 int err;
77
78 err = usb3503_read_register(client, reg);
79 if (err < 0)
80 return err;
81
82 err = usb3503_write_register(client, reg, err | req);
83 if (err < 0)
84 return err;
85
86 return 0;
87}
88
89static int usb3503_clear_bits(struct i2c_client *client, char reg, char req)
90{
91 int err;
92
93 err = usb3503_read_register(client, reg);
94 if (err < 0)
95 return err;
96
97 err = usb3503_write_register(client, reg, err & ~req);
98 if (err < 0)
99 return err;
100
101 return 0;
102}
103
104static int usb3503_reset(int gpio_reset, int state)
105{
106 if (gpio_is_valid(gpio_reset))
107 gpio_set_value(gpio_reset, state);
108
109 /* Wait RefClk when RESET_N is released, otherwise Hub will
110 * not transition to Hub Communication Stage.
111 */
112 if (state)
113 msleep(100);
114
115 return 0;
116}
117
118static int usb3503_switch_mode(struct usb3503 *hub, enum usb3503_mode mode)
119{
120 struct i2c_client *i2c = hub->client;
121 int err = 0;
122
123 switch (mode) {
124 case USB3503_MODE_HUB:
125 usb3503_reset(hub->gpio_reset, 1);
126
127 /* SP_ILOCK: set connect_n, config_n for config */
128 err = usb3503_write_register(i2c, USB3503_SP_ILOCK,
129 (USB3503_SPILOCK_CONNECT
130 | USB3503_SPILOCK_CONFIG));
131 if (err < 0) {
132 dev_err(&i2c->dev, "SP_ILOCK failed (%d)\n", err);
133 goto err_hubmode;
134 }
135
136 /* PDS : Port2,3 Disable For Self Powered Operation */
137 err = usb3503_set_bits(i2c, USB3503_PDS,
138 (USB3503_PORT2 | USB3503_PORT3));
139 if (err < 0) {
140 dev_err(&i2c->dev, "PDS failed (%d)\n", err);
141 goto err_hubmode;
142 }
143
144 /* CFG1 : SELF_BUS_PWR -> Self-Powerd operation */
145 err = usb3503_set_bits(i2c, USB3503_CFG1, USB3503_SELF_BUS_PWR);
146 if (err < 0) {
147 dev_err(&i2c->dev, "CFG1 failed (%d)\n", err);
148 goto err_hubmode;
149 }
150
151 /* SP_LOCK: clear connect_n, config_n for hub connect */
152 err = usb3503_clear_bits(i2c, USB3503_SP_ILOCK,
153 (USB3503_SPILOCK_CONNECT
154 | USB3503_SPILOCK_CONFIG));
155 if (err < 0) {
156 dev_err(&i2c->dev, "SP_ILOCK failed (%d)\n", err);
157 goto err_hubmode;
158 }
159
160 hub->mode = mode;
161 dev_info(&i2c->dev, "switched to HUB mode\n");
162 break;
163
164 case USB3503_MODE_STANDBY:
165 usb3503_reset(hub->gpio_reset, 0);
166
167 hub->mode = mode;
168 dev_info(&i2c->dev, "switched to STANDBY mode\n");
169 break;
170
171 default:
172 dev_err(&i2c->dev, "unknown mode is request\n");
173 err = -EINVAL;
174 break;
175 }
176
177err_hubmode:
178 return err;
179}
180
181int usb3503_probe(struct i2c_client *i2c, const struct i2c_device_id *id)
182{
183 struct usb3503_platform_data *pdata = i2c->dev.platform_data;
184 struct usb3503 *hub;
185 int err;
186
187 hub = kzalloc(sizeof(struct usb3503), GFP_KERNEL);
188 if (!hub) {
189 dev_err(&i2c->dev, "private data alloc fail\n");
190 return -ENOMEM;
191 }
192
193 i2c_set_clientdata(i2c, hub);
194 hub->client = i2c;
195
196 if (!pdata) {
197 dev_dbg(&i2c->dev, "missing platform data\n");
198 } else {
199 hub->gpio_intn = pdata->gpio_intn;
200 hub->gpio_connect = pdata->gpio_connect;
201 hub->gpio_reset = pdata->gpio_reset;
202 hub->mode = pdata->initial_mode;
203 }
204
205 if (gpio_is_valid(hub->gpio_intn)) {
206 err = gpio_request_one(hub->gpio_intn,
207 GPIOF_OUT_INIT_HIGH, "usb3503 intn");
208 if (err) {
209 dev_err(&i2c->dev,
210 "unable to request GPIO %d as connect pin (%d)\n",
211 hub->gpio_intn, err);
212 goto err_gpio_intn;
213 }
214 }
215
216 if (gpio_is_valid(hub->gpio_connect)) {
217 err = gpio_request_one(hub->gpio_connect,
218 GPIOF_OUT_INIT_HIGH, "usb3503 connect");
219 if (err) {
220 dev_err(&i2c->dev,
221 "unable to request GPIO %d as connect pin (%d)\n",
222 hub->gpio_connect, err);
223 goto err_gpio_connect;
224 }
225 }
226
227 if (gpio_is_valid(hub->gpio_reset)) {
228 err = gpio_request_one(hub->gpio_reset,
229 GPIOF_OUT_INIT_LOW, "usb3503 reset");
230 if (err) {
231 dev_err(&i2c->dev,
232 "unable to request GPIO %d as reset pin (%d)\n",
233 hub->gpio_reset, err);
234 goto err_gpio_reset;
235 }
236 }
237
238 usb3503_switch_mode(hub, pdata->initial_mode);
239
240 dev_info(&i2c->dev, "%s: probed on %s mode\n", __func__,
241 (hub->mode == USB3503_MODE_HUB) ? "hub" : "standby");
242
243 return 0;
244
245err_gpio_reset:
246 if (gpio_is_valid(hub->gpio_connect))
247 gpio_free(hub->gpio_connect);
248err_gpio_connect:
249 if (gpio_is_valid(hub->gpio_intn))
250 gpio_free(hub->gpio_intn);
251err_gpio_intn:
252 kfree(hub);
253
254 return err;
255}
256
257static int usb3503_remove(struct i2c_client *i2c)
258{
259 struct usb3503 *hub = i2c_get_clientdata(i2c);
260
261 if (gpio_is_valid(hub->gpio_intn))
262 gpio_free(hub->gpio_intn);
263 if (gpio_is_valid(hub->gpio_connect))
264 gpio_free(hub->gpio_connect);
265 if (gpio_is_valid(hub->gpio_reset))
266 gpio_free(hub->gpio_reset);
267
268 kfree(hub);
269
270 return 0;
271}
272
273static const struct i2c_device_id usb3503_id[] = {
274 { USB3503_I2C_NAME, 0 },
275 { }
276};
277MODULE_DEVICE_TABLE(i2c, usb3503_id);
278
279static struct i2c_driver usb3503_driver = {
280 .driver = {
281 .name = USB3503_I2C_NAME,
282 },
283 .probe = usb3503_probe,
284 .remove = usb3503_remove,
285 .id_table = usb3503_id,
286};
287
288static int __init usb3503_init(void)
289{
290 return i2c_add_driver(&usb3503_driver);
291}
292
293static void __exit usb3503_exit(void)
294{
295 i2c_del_driver(&usb3503_driver);
296}
297
298module_init(usb3503_init);
299module_exit(usb3503_exit);
300
301MODULE_AUTHOR("Dongjin Kim <tobetter@gmail.com>");
302MODULE_DESCRIPTION("USB3503 USB HUB driver");
303MODULE_LICENSE("GPL");