1 /*
2 * drivers/usb/sunxi_usb/manager/usb_hw_scan.c
3 * (C) Copyright 2010-2015
4 * Allwinner Technology Co., Ltd. <www.allwinnertech.com>
5 * javen, 2011-4-14, create this file
6 *
7 * usb detect module.
8 *
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License as
11 * published by the Free Software Foundation; either version 2 of
12 * the License, or (at your option) any later version.
13 *
14 */
15
16 #include <linux/module.h>
17 #include <linux/kernel.h>
18 #include <linux/delay.h>
19 #include <linux/ioport.h>
20 #include <linux/sched.h>
21 #include <linux/slab.h>
22 #include <linux/errno.h>
23 #include <linux/init.h>
24 #include <linux/timer.h>
25 #include <linux/list.h>
26 #include <linux/interrupt.h>
27 #include <linux/platform_device.h>
28 #include <linux/clk.h>
29
30 #include <linux/debugfs.h>
31 #include <linux/seq_file.h>
32
33 #include <asm/byteorder.h>
34 #include <asm/io.h>
35 #include <asm/irq.h>
36 #include <asm/unaligned.h>
37 #include <linux/gpio.h>
38 #include "../include/sunxi_usb_config.h"
39 #include "usb_manager.h"
40 #include "usb_hw_scan.h"
41 #include "usb_msg_center.h"
42
43 #include <linux/power_supply.h>
44
45 static struct usb_scan_info g_usb_scan_info;
46
47 struct power_supply *psy;
48
49 int device_insmod_delay;
50 static void (*__usb_hw_scan)(struct usb_scan_info *);
51
52 #ifndef SUNXI_USB_FPGA
get_pin_data(struct usb_gpio * usb_gpio)53 static __u32 get_pin_data(struct usb_gpio *usb_gpio)
54 {
55 return __gpio_get_value(usb_gpio->gpio_set.gpio);
56 }
57
58 #if IS_ENABLED(CONFIG_USB_G_ANDROID) || IS_ENABLED(CONFIG_USB_MASS_STORAGE)
get_usb_gadget_functions(void)59 static int get_usb_gadget_functions(void)
60 {
61 struct file *filep;
62 loff_t pos;
63 char buf[32] = {0};
64
65 filep = filp_open("/sys/class/android_usb/android0/functions",
66 O_RDONLY, 0);
67 if (IS_ERR(filep))
68 return 0;
69
70 pos = 0;
71 vfs_read(filep, (char __user *)buf, 32, &pos);
72 filp_close(filep, NULL);
73
74 if (strlen(buf) == 0)
75 return 0;
76 else
77 return 1;
78 }
79 #endif
80
81 /**
82 * filter the PIO burr
83 * @usb_gpio: .
84 * @value: store the value to be read
85 *
86 * try 10 times, if value is the same, then consider no shake. return any one.
87 * if not the same, then the current read is invalid
88 *
89 * return: 0 - the valude is the same, valid, 1 - the value change, invalid.
90 */
PIODataIn_debounce(struct usb_gpio * usb_gpio,__u32 * value)91 static __u32 PIODataIn_debounce(struct usb_gpio *usb_gpio, __u32 *value)
92 {
93 __u32 retry = 0;
94 __u32 time = 10;
95 __u32 temp1 = 0;
96 __u32 cnt = 0;
97 __u32 change = 0; /* if have shake */
98
99 /**
100 * try 10 times, if value is the same,
101 * then current read is valid; otherwise invalid.
102 */
103 if (usb_gpio->valid) {
104 retry = time;
105 while (retry--) {
106 temp1 = get_pin_data(usb_gpio);
107 if (temp1)
108 cnt++;
109 }
110
111 /* 10 times, the value is all 0 or 1 */
112 if ((cnt == time) || (cnt == 0))
113 change = 0;
114 else
115 change = 1;
116 } else {
117 change = 1;
118 }
119
120 if (!change)
121 *value = temp1;
122
123 DMSG_DBG_MANAGER("usb_gpio->valid = %x, cnt = %x, change= %d, temp1 = %x\n",
124 usb_gpio->valid, cnt, change, temp1);
125
126 return change;
127 }
128
get_id_state(struct usb_scan_info * info)129 static u32 get_id_state(struct usb_scan_info *info)
130 {
131 enum usb_id_state id_state = USB_DEVICE_MODE;
132 __u32 pin_data = 0;
133
134 if (info->cfg->port.id_type == USB_ID_TYPE_GPIO) {
135 if (info->cfg->port.id.valid) {
136 if (!PIODataIn_debounce(&info->cfg->port.id, &pin_data)) {
137 if (pin_data)
138 id_state = USB_DEVICE_MODE;
139 else
140 id_state = USB_HOST_MODE;
141
142 info->id_old_state = id_state;
143 } else {
144 id_state = info->id_old_state;
145 }
146 }
147 } else if (info->cfg->port.id_type == USB_ID_TYPE_AXP) {
148 #if IS_ENABLED(CONFIG_TYPE_C)
149 if (axp_usb_cc_status())
150 id_state = USB_DEVICE_MODE;
151 else
152 id_state = USB_HOST_MODE;
153 #endif
154 } else {
155 id_state = info->id_old_state;
156 }
157
158 return id_state;
159 }
160
get_detect_vbus_state(struct usb_scan_info * info)161 static u32 get_detect_vbus_state(struct usb_scan_info *info)
162 {
163 enum usb_det_vbus_state det_vbus_state = USB_DET_VBUS_INVALID;
164 __u32 pin_data = 0;
165 union power_supply_propval temp;
166
167 if (info->cfg->port.detect_mode == USB_DETECT_MODE_THREAD) {
168 if (info->cfg->port.det_vbus_type == USB_DET_VBUS_TYPE_GPIO) {
169 if (info->cfg->port.det_vbus.valid) {
170 if (!PIODataIn_debounce(&info->cfg->port.det_vbus, &pin_data)) {
171 if (pin_data)
172 det_vbus_state = USB_DET_VBUS_VALID;
173 else
174 det_vbus_state = USB_DET_VBUS_INVALID;
175
176 info->det_vbus_old_state = det_vbus_state;
177 } else {
178 det_vbus_state = info->det_vbus_old_state;
179 }
180 }
181 } else if (info->cfg->port.det_vbus_type
182 == USB_DET_VBUS_TYPE_AXP) {
183 /*det_vbus is axp, get data only once*/
184 if (!psy) {
185 if (of_find_property(info->cfg->pdev->dev.of_node,
186 "det_vbus_supply", NULL))
187 psy = devm_power_supply_get_by_phandle(&info->cfg->pdev->dev,
188 "det_vbus_supply");
189
190 if (!psy || IS_ERR(psy)) {
191 DMSG_INFO("%s()%d WARN: "
192 "get power supply failed\n",
193 __func__, __LINE__);
194 } else {
195 goto get_property;
196 }
197
198 } else {
199 get_property:
200 power_supply_get_property(psy,
201 POWER_SUPPLY_PROP_ONLINE, &temp);
202
203 if (temp.intval)
204 det_vbus_state = USB_DET_VBUS_VALID;
205 else
206 det_vbus_state = USB_DET_VBUS_INVALID;
207 }
208
209 } else {
210 det_vbus_state = info->det_vbus_old_state;
211 }
212 } else if (info->cfg->port.detect_mode == USB_DETECT_MODE_INTR) {
213 det_vbus_state = USB_DET_VBUS_VALID;
214 } else {
215 DMSG_PANIC("ERR: get_detect_vbus_state, ");
216 DMSG_PANIC("usb det mode isn't supported\n");
217 }
218
219 return det_vbus_state;
220 }
221
get_dp_dm_status(struct usb_scan_info * info)222 static u32 get_dp_dm_status(struct usb_scan_info *info)
223 {
224 u32 ret = 0;
225 u32 ret0 = 0;
226 u32 ret1 = 0;
227 u32 ret2 = 0;
228
229 ret0 = get_dp_dm_status_normal();
230 ret1 = get_dp_dm_status_normal();
231 ret2 = get_dp_dm_status_normal();
232
233 /* continuous 3 times, to avoid the voltage sudden changes */
234 if ((ret0 == ret1) && (ret0 == ret2)) {
235 ret = ret0;
236 } else if (ret2 == 0x11) {
237 if (get_usb_role() == USB_ROLE_DEVICE) {
238 ret = 0x11;
239 DMSG_PANIC("ERR: dp/dm status is continuous(0x11)\n");
240 }
241 } else {
242 ret = ret2;
243 }
244
245 return ret;
246 }
247 #endif
248
do_vbus0_id0(struct usb_scan_info * info)249 static void do_vbus0_id0(struct usb_scan_info *info)
250 {
251 enum usb_role role = USB_ROLE_NULL;
252
253 role = get_usb_role();
254 device_insmod_delay = 0;
255
256 switch (role) {
257 case USB_ROLE_NULL:
258 /* delay for vbus is stably */
259 if (atomic_read(&thread_suspend_flag))
260 break;
261 if (info->host_insmod_delay
262 < USB_SCAN_INSMOD_HOST_DRIVER_DELAY) {
263 info->host_insmod_delay++;
264 break;
265 }
266 info->host_insmod_delay = 0;
267
268 /* insmod usb host */
269 hw_insmod_usb_host();
270 break;
271
272 case USB_ROLE_HOST:
273 /* nothing to do */
274 break;
275
276 case USB_ROLE_DEVICE:
277 /* rmmod usb device */
278 if (atomic_read(&thread_suspend_flag))
279 break;
280 hw_rmmod_usb_device();
281 break;
282
283 default:
284 DMSG_PANIC("ERR: unknown usb role(%d)\n", role);
285 }
286 }
287
do_vbus0_id1(struct usb_scan_info * info)288 static void do_vbus0_id1(struct usb_scan_info *info)
289 {
290 enum usb_role role = USB_ROLE_NULL;
291
292 role = get_usb_role();
293 device_insmod_delay = 0;
294 info->host_insmod_delay = 0;
295
296 switch (role) {
297 case USB_ROLE_NULL:
298 /* nothing to do */
299 break;
300 case USB_ROLE_HOST:
301 if (atomic_read(&thread_suspend_flag))
302 break;
303 hw_rmmod_usb_host();
304 break;
305 case USB_ROLE_DEVICE:
306 if (atomic_read(&thread_suspend_flag))
307 break;
308 hw_rmmod_usb_device();
309 break;
310 default:
311 DMSG_PANIC("ERR: unknown usb role(%d)\n", role);
312 }
313 }
314
do_vbus1_id0(struct usb_scan_info * info)315 static void do_vbus1_id0(struct usb_scan_info *info)
316 {
317 enum usb_role role = USB_ROLE_NULL;
318
319 role = get_usb_role();
320 device_insmod_delay = 0;
321
322 switch (role) {
323 case USB_ROLE_NULL:
324 if (info->cfg->port.detect_mode == USB_DETECT_MODE_THREAD) {
325 if (atomic_read(&thread_suspend_flag))
326 break;
327 /* delay for vbus is stably */
328 if (info->host_insmod_delay
329 < USB_SCAN_INSMOD_HOST_DRIVER_DELAY) {
330 info->host_insmod_delay++;
331 break;
332 }
333 info->host_insmod_delay = 0;
334
335 hw_insmod_usb_host();
336 } else if (info->cfg->port.detect_mode
337 == USB_DETECT_MODE_INTR) {
338 hw_insmod_usb_host();
339 } else {
340 DMSG_PANIC("ERR: do_vbus1_id0, ");
341 DMSG_PANIC("usb det mode isn't supported, role=%d\n",
342 role);
343 }
344 break;
345 case USB_ROLE_HOST:
346 /* nothing to do */
347 break;
348 case USB_ROLE_DEVICE:
349 if (info->cfg->port.detect_mode == USB_DETECT_MODE_THREAD) {
350 if (atomic_read(&thread_suspend_flag))
351 break;
352 hw_rmmod_usb_device();
353 } else if (info->cfg->port.detect_mode
354 == USB_DETECT_MODE_INTR) {
355 hw_rmmod_usb_device();
356 } else {
357 DMSG_PANIC("ERR: do_vbus1_id0, ");
358 DMSG_PANIC("usb det mode isn't supported, role=%d\n",
359 role);
360 }
361 break;
362 default:
363 DMSG_PANIC("ERR: unknown usb role(%d)\n", role);
364 }
365 }
366
do_vbus1_id1(struct usb_scan_info * info)367 static void do_vbus1_id1(struct usb_scan_info *info)
368 {
369 enum usb_role role = USB_ROLE_NULL;
370
371 role = get_usb_role();
372 info->host_insmod_delay = 0;
373
374 switch (role) {
375 case USB_ROLE_NULL:
376 #ifndef SUNXI_USB_FPGA
377 if (info->cfg->port.detect_mode == USB_DETECT_MODE_THREAD) {
378 if (get_dp_dm_status(info) == 0x00) {
379 if (atomic_read(&thread_suspend_flag))
380 break;
381 /* delay for vbus is stably */
382 if (device_insmod_delay < USB_SCAN_INSMOD_DEVICE_DRIVER_DELAY) {
383 device_insmod_delay++;
384 break;
385 }
386
387 device_insmod_delay = 0;
388 #if IS_ENABLED(CONFIG_USB_G_ANDROID) || IS_ENABLED(CONFIG_USB_MASS_STORAGE)
389 if (get_usb_gadget_functions())
390 #endif
391 hw_insmod_usb_device();
392 }
393 } else if (info->cfg->port.detect_mode
394 == USB_DETECT_MODE_INTR) {
395 hw_insmod_usb_device();
396 } else {
397 DMSG_PANIC("ERR: do_vbus1_id1, ");
398 DMSG_PANIC("usb det mode isn't supported, role=%d\n",
399 role);
400 }
401 #else
402 hw_insmod_usb_device();
403 #endif
404 break;
405 case USB_ROLE_HOST:
406 if (info->cfg->port.detect_mode == USB_DETECT_MODE_THREAD) {
407 if (atomic_read(&thread_suspend_flag))
408 break;
409 hw_rmmod_usb_host();
410 } else if (info->cfg->port.detect_mode
411 == USB_DETECT_MODE_INTR) {
412 hw_rmmod_usb_host();
413 } else {
414 DMSG_PANIC("ERR: do_vbus1_id1, ");
415 DMSG_PANIC("usb det mode isn't supported, role=%d\n",
416 role);
417 }
418 break;
419 case USB_ROLE_DEVICE:
420 /* nothing to do */
421 break;
422 default:
423 DMSG_PANIC("ERR: unknown usb role(%d)\n", role);
424 }
425 }
426
427 #ifdef SUNXI_USB_FPGA
428 static u32 usb_vbus_id_state = 1;
set_vbus_id_state(u32 state)429 __u32 set_vbus_id_state(u32 state)
430 {
431 usb_vbus_id_state = state;
432 return 0;
433 }
get_vbus_id_state(struct usb_scan_info * info)434 static __u32 get_vbus_id_state(struct usb_scan_info *info)
435 {
436 return usb_vbus_id_state;
437 }
438 #else
get_vbus_id_state(struct usb_scan_info * info)439 static __u32 get_vbus_id_state(struct usb_scan_info *info)
440 {
441 u32 state = 0;
442
443 if (get_id_state(info) == USB_DEVICE_MODE)
444 x_set_bit(state, 0);
445
446 if (get_detect_vbus_state(info) == USB_DET_VBUS_VALID)
447 x_set_bit(state, 1);
448
449 return state;
450 }
451 #endif
452
vbus_id_hw_scan(struct usb_scan_info * info)453 static void vbus_id_hw_scan(struct usb_scan_info *info)
454 {
455 __u32 vbus_id_state = 0;
456
457 vbus_id_state = get_vbus_id_state(info);
458
459 if (usb_hw_scan_debug)
460 DMSG_INFO("Id=%d,role=%d\n", vbus_id_state, get_usb_role());
461
462 switch (vbus_id_state) {
463 case 0x00:
464 do_vbus0_id0(info);
465 break;
466 case 0x01:
467 do_vbus0_id1(info);
468 break;
469 case 0x02:
470 do_vbus1_id0(info);
471 break;
472 case 0x03:
473 do_vbus1_id1(info);
474 break;
475 default:
476 DMSG_PANIC("ERR: vbus_id_hw_scan: ");
477 DMSG_PANIC("unknown vbus_id_state(0x%x)\n",
478 vbus_id_state);
479 }
480 }
481
null_hw_scan(struct usb_scan_info * info)482 static void null_hw_scan(struct usb_scan_info *info)
483 {
484 DMSG_DBG_MANAGER("null_hw_scan\n");
485 }
486
usb_hw_scan(struct usb_cfg * cfg)487 void usb_hw_scan(struct usb_cfg *cfg)
488 {
489 __usb_hw_scan(&g_usb_scan_info);
490 }
491
usb_hw_scan_init(struct usb_cfg * cfg)492 __s32 usb_hw_scan_init(struct usb_cfg *cfg)
493 {
494 struct usb_scan_info *scan_info = &g_usb_scan_info;
495 struct usb_port_info *port_info = NULL;
496 __s32 ret = 0;
497
498 memset(scan_info, 0, sizeof(struct usb_scan_info));
499 device_insmod_delay = 0;
500 scan_info->cfg = cfg;
501 scan_info->id_old_state = USB_DEVICE_MODE;
502 scan_info->det_vbus_old_state = USB_DET_VBUS_INVALID;
503
504 port_info = &(cfg->port);
505 switch (port_info->port_type) {
506 case USB_PORT_TYPE_DEVICE:
507 __usb_hw_scan = null_hw_scan;
508 break;
509 case USB_PORT_TYPE_HOST:
510 __usb_hw_scan = null_hw_scan;
511 break;
512 case USB_PORT_TYPE_OTG:
513 #ifdef SUNXI_USB_FPGA
514 {
515 __usb_hw_scan = vbus_id_hw_scan;
516 }
517 #else
518 {
519 if (port_info->detect_mode == USB_DETECT_MODE_THREAD) {
520 __u32 need_det_vbus_req_gpio = 1;
521
522 if (port_info->det_vbus_type == USB_DET_VBUS_TYPE_GPIO
523 && port_info->id_type == USB_ID_TYPE_GPIO) {
524 if ((port_info->id.valid == 0) || (port_info->det_vbus.valid == 0)) {
525 DMSG_PANIC("ERR: usb detect tpye is vbus/id, but id(%d)/vbus(%d) is invalid\n",
526 port_info->id.valid,
527 port_info->det_vbus.valid);
528 ret = -1;
529 port_info->id.valid = 0;
530 port_info->det_vbus.valid = 0;
531 goto failed;
532 }
533
534 /**
535 * if id and vbus use the same pin,
536 * then need not to pull pio.
537 */
538 if (port_info->id.gpio_set.gpio == port_info->det_vbus.gpio_set.gpio) {
539 /**
540 * when id and det_vbus reuse,
541 * the det bus need not request
542 * gpio again.
543 */
544 need_det_vbus_req_gpio = 0;
545 }
546 }
547
548 /* request id gpio */
549 if (port_info->id.valid) {
550 ret = gpio_request(
551 port_info->id.gpio_set.gpio,
552 "otg_id");
553 if (ret != 0) {
554 DMSG_PANIC("ERR: id gpio_request failed\n");
555 ret = -1;
556 port_info->id.valid = 0;
557 port_info->det_vbus.valid = 0;
558 goto failed;
559 }
560
561 /* set config, input */
562 gpio_direction_input(
563 port_info->id.gpio_set.gpio);
564 __gpio_set_value(
565 port_info->id.gpio_set.gpio,
566 1);
567 }
568
569 /* request det_vbus gpio */
570 if (port_info->det_vbus.valid
571 && need_det_vbus_req_gpio) {
572 ret = gpio_request(port_info->det_vbus.gpio_set.gpio, "otg_det");
573 if (ret != 0) {
574 DMSG_PANIC("ERR: det_vbus gpio_request failed\n");
575 ret = -1;
576 port_info->det_vbus.valid = 0;
577 goto failed;
578 }
579
580 /* set config, input */
581 gpio_direction_input(port_info->det_vbus.gpio_set.gpio);
582 }
583 } else if (port_info->detect_mode == USB_DETECT_MODE_INTR) {
584 if (port_info->id.valid) {
585 char pin_name[SUNXI_PIN_NAME_MAX_LEN];
586
587 ret = gpio_request(
588 port_info->id.gpio_set.gpio,
589 "otg_id");
590 if (ret != 0) {
591 DMSG_PANIC("ERR: id gpio_request failed\n");
592 ret = -1;
593 port_info->id.valid = 0;
594 port_info->det_vbus.valid = 0;
595 goto failed;
596 }
597
598 sunxi_gpio_to_name(
599 port_info->id.gpio_set.gpio,
600 pin_name);
601
602 /* set id gpio pull up */
603 }
604 }
605
606 __usb_hw_scan = vbus_id_hw_scan;
607 }
608 #endif
609 break;
610 default:
611 DMSG_PANIC("ERR: unknown port_type(%d)\n", cfg->port.port_type);
612 ret = -1;
613 goto failed;
614 }
615
616 return 0;
617
618 failed:
619 #ifndef SUNXI_USB_FPGA
620 if (port_info->id.valid)
621 gpio_free(port_info->id.gpio_set.gpio);
622
623 if (port_info->det_vbus.valid)
624 gpio_free(port_info->det_vbus.gpio_set.gpio);
625 #endif
626 __usb_hw_scan = null_hw_scan;
627 return ret;
628 }
629
630 #ifdef SUNXI_USB_FPGA
usb_hw_scan_exit(struct usb_cfg * cfg)631 __s32 usb_hw_scan_exit(struct usb_cfg *cfg)
632 {
633 return 0;
634 }
635 #else
usb_hw_scan_exit(struct usb_cfg * cfg)636 __s32 usb_hw_scan_exit(struct usb_cfg *cfg)
637 {
638 if (cfg->port.id.valid)
639 gpio_free(cfg->port.id.gpio_set.gpio);
640
641 if (cfg->port.det_vbus.valid)
642 gpio_free(cfg->port.det_vbus.gpio_set.gpio);
643
644 return 0;
645 }
646 #endif
647