aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/misc/usb3803.c
blob: 5a05b3864061dec4d2e5aaed95e87f36e3361474 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
/*
 * drivers/misc/usb3803.c - usb3803 usb hub driver
 *
 * This software is licensed under the terms of the GNU General Public
 * License version 2, as published by the Free Software Foundation, and
 * may be copied, distributed, and modified under those terms.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 */
#include <linux/i2c.h>
#include <linux/usb3803.h>
#include <linux/delay.h>

static struct i2c_client *this_client;
static struct usb3803_platform_data *pdata;
static int current_mode;

/* DEFINE RESISTERs */
#define CFG1_REG    0x06
#define SP_ILOCK_REG    0xE7
#define CFGP_REG    0xEE
#define PDS_REG     0x0A

/*
 * 0x06; only AP (disable CP and WiMax)
 * 0x0A; only CP (disable AP and Wimax)
 * 0x00; enable all port
 *
 * bit :  3   2   1     0
 *       AP, CP, WIMAX, RESERVED
 */
static int hub_port = 0x0;

static ssize_t mode_show(struct device *dev,
	struct device_attribute *attr, char *buf);
static ssize_t mode_store(struct device *dev,
	struct device_attribute *attr, const char *buf, size_t size);
static ssize_t port_show(struct device *dev,
	struct device_attribute *attr, char *buf);
static ssize_t port_store(struct device *dev,
	struct device_attribute *attr, const char *buf, size_t size);
static int usb3803_register_write(char reg, char data);
static int usb3803_register_read(char reg, char *data);


static DEVICE_ATTR(mode, 0664, mode_show, mode_store);

static ssize_t mode_show(struct device *dev,
	struct device_attribute *attr, char *buf)
{
	if (current_mode == USB_3803_MODE_HUB)
		return sprintf(buf, "%s", "hub");
	else if (current_mode == USB_3803_MODE_BYPASS)
		return sprintf(buf, "%s", "bypass");
	else if (current_mode == USB_3803_MODE_STANDBY)
		return sprintf(buf, "%s", "standby");

	return 0;
}

static ssize_t mode_store(
	struct device *dev, struct device_attribute *attr,
	const char *buf, size_t size)
{
	if (!strncmp(buf, "hub", 3)) {
		usb3803_set_mode(USB_3803_MODE_HUB);
		pr_debug("usb3803 mode set to hub\n");
	} else if (!strncmp(buf, "bypass", 6)) {
		usb3803_set_mode(USB_3803_MODE_BYPASS);
		pr_debug("usb3803 mode set to bypass\n");
	} else if (!strncmp(buf, "standby", 7)) {
		usb3803_set_mode(USB_3803_MODE_STANDBY);
		pr_debug("usb3803 mode set to standby\n");
	}
	return size;
}

static DEVICE_ATTR(port, 0664, port_show, port_store);

static ssize_t port_show(struct device *dev,
	struct device_attribute *attr, char *buf)
{
	return sprintf(buf, "hub_port=0x%x", hub_port);
}

static ssize_t port_store(
	struct device *dev, struct device_attribute *attr,
	const char *buf, size_t size)
{
	if (!strncmp(buf, "disable_cp", 10)) {
		hub_port = (0x1 << 2) | hub_port;
		pr_debug("usb3803 port disable cp\n");
	} else if (!strncmp(buf, "disable_wimax", 13)) {
		hub_port = (0x1 << 1) | hub_port;
		pr_debug("usb3803 port disable wimaxb\n");
	} else if (!strncmp(buf, "disable_ap", 10)) {
		hub_port = (0x1 << 3) | hub_port;
		pr_debug("usb3803 port disable ap\n");
	} else if (!strncmp(buf, "enable_cp", 9)) {
		hub_port = ~(0x1 << 2) & hub_port;
		pr_debug("usb3803 port enable cp\n");
	} else if (!strncmp(buf, "enable_wimax", 12)) {
		hub_port = ~(0x1 << 1) & hub_port;
		pr_debug("usb3803 port enable wimaxb\n");
	} else if (!strncmp(buf, "enable_ap", 9)) {
		hub_port = ~(0x1 << 3) & hub_port;
		pr_debug("usb3803 port enable ap\n");
	}

	if (current_mode == USB_3803_MODE_HUB)
		usb3803_set_mode(USB_3803_MODE_HUB);
	pr_debug("usb3803 mode setting (%s)\n", buf);

	return size;
}

static int usb3803_register_write(char reg, char data)
{
	int ret;
	char buf[2];
	struct i2c_msg msg[] = {
		{
			.addr = this_client->addr,
			.flags = 0,
			.len = 2,
			.buf = buf,
		},
	};

	buf[0] = reg;
	buf[1] = data;

	ret = i2c_transfer(this_client->adapter, msg, 1);

	if (ret < 0)
			pr_err("[%s] reg:0x%x data:0x%x write failed\n",
					__func__, reg, data);

	return ret;
}

static int usb3803_register_read(char reg, char *data)
{
	int ret;

	struct i2c_msg msgs[] = {
		{
			.addr = this_client->addr,
			.flags = 0,
			.len = 1,
			.buf = &reg,
		},
		{
			.addr = this_client->addr,
			.flags = I2C_M_RD,
			.len = 1,
			.buf = data,
		},
	};

	ret = i2c_transfer(this_client->adapter, msgs, 2);

	if (ret < 0)
			pr_err("[%s] 0x%x read failed\n", __func__, reg);

	return ret;
}

int usb3803_set_mode(int mode)
{
	int ret = 0;

	switch (mode) {
	case USB_3803_MODE_HUB:
		pdata->reset_n(0);
		msleep(20);

		/* enable clock */
		pdata->clock_en(1);
		/* reset assert */
		pdata->reset_n(1);
		/* bypass mode disable */
		pdata->bypass_n(1);

		/* there is a major glitch in the ES version of USB3803 */
		/* below is the work-around. CS version doesn't
		need this code! */
		if (pdata->es_ver) {
			int i;
			char data;
			usleep_range(5000, 5000);

			/* Step 1. Write value 0x03 to register
			0xE7 after RESET_N transitioning to high. */
			for (i = 0; i < 3; i++) {
				pr_info("[%s] write SP_ILOCK_REG (attempt %d)\n",
					__func__, i);
				usb3803_register_write(SP_ILOCK_REG, 0x03);
				usb3803_register_read(SP_ILOCK_REG, &data);
				pr_info("[%s] read  SP_ILOCK_REG : 0x%x\n",
					__func__, data);

				if (data == 0x3)
					break;
			}
			if (i >= 3) {
				pr_crit("[%s] SP_ILOCK_REG set failed! aborted!\n",
					__func__);
				ret = -1;
				break;
			}

			/* Step 2. Set bit 7 (ClkSusp) of register 0xEE
			to high. */
			usb3803_register_read(CFGP_REG, &data);
			pr_info("[%s] read  CFGP_REG : 0x%x (default)\n",
				__func__, data);
			data |= 0x80;
			usb3803_register_write(CFGP_REG, data);
			usb3803_register_read(CFGP_REG, &data);
			pr_info("[%s] read  CFGP_REG : 0x%x\n", __func__, data);

			/* Step 2.5
			 * Port Disable For Self Powered Operation
			 */
			usb3803_register_read(PDS_REG, &data);
			pr_info("[%s] read  PDS_REG : 0x%x (default)\n"
				, __func__, data);
			usb3803_register_write(PDS_REG, hub_port);
			usb3803_register_read(PDS_REG, &data);
			pr_info("[%s] hub_port : 0x%x\n"
				, __func__, hub_port);
			pr_info("[%s] read  PDS_REG : 0x%x\n"
				, __func__, data);

			/* Step 3. Set bit 7 (SELF_BUS_PWR) of
			register 0x06 to high.*/
			usb3803_register_read(CFG1_REG, &data);
			pr_info("[%s] read  CFG1_REG : 0x%x (default)\n",
				__func__, data);
			data |= 0x80;
			usb3803_register_write(CFG1_REG, data);
			usb3803_register_read(CFG1_REG, &data);
			pr_info("[%s] read  CFG1_REG : 0x%x\n", __func__, data);

			/* Step 4. Clear bit 0 (config_n) & bit 1
			(connect_n) of register 0xE7. */
			usb3803_register_write(SP_ILOCK_REG, 0x00);
			usb3803_register_read(SP_ILOCK_REG, &data);
			pr_info("[%s] read  SP_ILOCK_REG : 0x%x\n",
					__func__, data);
			current_mode = mode;
		}
		break;

	case USB_3803_MODE_BYPASS:
		/* disable clock */
		pdata->clock_en(0);
		/* reset assert */
		pdata->reset_n(1);
		/* bypass mode enable */
		pdata->bypass_n(0);
		current_mode = mode;
		break;

	case USB_3803_MODE_STANDBY:
		pdata->reset_n(0);
		/* disable clock */
		pdata->clock_en(0);
		pdata->bypass_n(0);
		current_mode = mode;
		break;

	default:
		pr_err("[%s] Invalid mode %d\n", __func__, mode);
		break;
	}

	return ret;
}
EXPORT_SYMBOL(usb3803_set_mode);

int usb3803_suspend(struct i2c_client *client, pm_message_t mesg)
{
	pdata->reset_n(0);
	pdata->clock_en(0);

	pr_info("USB3803 suspended\n");

	return 0;
}

int usb3803_resume(struct i2c_client *client)
{
	if (current_mode == USB_3803_MODE_HUB)
		pr_info("USB3803 skip hub mode setting\n");
	else
		usb3803_set_mode(current_mode);
	if (current_mode == USB_3803_MODE_HUB)
		pr_info("USB3803 resumed to mode %s\n", "hub");
	else if (current_mode == USB_3803_MODE_BYPASS)
		pr_info("USB3803 resumed to mode %s\n", "bypass");
	else if (current_mode == USB_3803_MODE_STANDBY)
		pr_info("USB3803 resumed to mode %s\n", "standby");
	return 0;
}


int usb3803_probe(struct i2c_client *client, const struct i2c_device_id *id)
{
	int err = 0;

	if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
		err = -ENODEV;
		goto exit_check_functionality_failed;
	}

	pdata = client->dev.platform_data;
	if (pdata == NULL) {
		pr_err("USB3803 device's platform data is NULL!\n");
		err = -ENODEV;
		goto exit_pdata_null;
	}

	pdata->hw_config();

	this_client = client;

	if (pdata->init_needed)
		usb3803_set_mode(pdata->inital_mode);

	current_mode = pdata->inital_mode;

	err = device_create_file(&client->dev, &dev_attr_mode);
	err = device_create_file(&client->dev, &dev_attr_port);

	if (current_mode == USB_3803_MODE_HUB)
		pr_info("USB3803 probed on mode %s\n", "hub");
	else if (current_mode == USB_3803_MODE_BYPASS)
		pr_info("USB3803 probed on mode %s\n", "bypass");
	else if (current_mode == USB_3803_MODE_STANDBY)
		pr_info("USB3803 probed on mode %s\n", "standby");

 exit_pdata_null:
 exit_check_functionality_failed:
	return err;

}

static int usb3803_remove(struct i2c_client *client)
{
	return 0;
}
static const struct i2c_device_id usb3803_id[] = {
	{ USB3803_I2C_NAME, 0 },
	{ }
};

static struct i2c_driver usb3803_driver = {
	.probe		= usb3803_probe,
	.remove		= usb3803_remove,
	.suspend	= usb3803_suspend,
	.resume		= usb3803_resume,
	.id_table = usb3803_id,
	.driver		= {
		.name		= USB3803_I2C_NAME,
	},
};

static int __init usb3803_init(void)
{
	pr_info("USB3803 USB HUB driver init\n");
	return i2c_add_driver(&usb3803_driver);
}

static void __exit usb3803_exit(void)
{
	pr_info("USB3803 USB HUB driver exit\n");
	i2c_del_driver(&usb3803_driver);
}

module_init(usb3803_init);
module_exit(usb3803_exit);

MODULE_DESCRIPTION("USB3803 USB HUB driver");
MODULE_LICENSE("GPL");