1
2
3
4
5
6
7
8
9
10
11#include <linux/kernel.h>
12#include <linux/export.h>
13#include <linux/err.h>
14#include <linux/device.h>
15#include <linux/module.h>
16#include <linux/slab.h>
17#include <linux/of.h>
18
19#include <linux/usb/phy.h>
20
21
22#define DEFAULT_SDP_CUR_MIN 2
23#define DEFAULT_SDP_CUR_MAX 500
24#define DEFAULT_SDP_CUR_MIN_SS 150
25#define DEFAULT_SDP_CUR_MAX_SS 900
26#define DEFAULT_DCP_CUR_MIN 500
27#define DEFAULT_DCP_CUR_MAX 5000
28#define DEFAULT_CDP_CUR_MIN 1500
29#define DEFAULT_CDP_CUR_MAX 5000
30#define DEFAULT_ACA_CUR_MIN 1500
31#define DEFAULT_ACA_CUR_MAX 5000
32
33static LIST_HEAD(phy_list);
34static LIST_HEAD(phy_bind_list);
35static DEFINE_SPINLOCK(phy_lock);
36
37struct phy_devm {
38 struct usb_phy *phy;
39 struct notifier_block *nb;
40};
41
42static struct usb_phy *__usb_find_phy(struct list_head *list,
43 enum usb_phy_type type)
44{
45 struct usb_phy *phy = NULL;
46
47 list_for_each_entry(phy, list, head) {
48 if (phy->type != type)
49 continue;
50
51 return phy;
52 }
53
54 return ERR_PTR(-ENODEV);
55}
56
57static struct usb_phy *__usb_find_phy_dev(struct device *dev,
58 struct list_head *list, u8 index)
59{
60 struct usb_phy_bind *phy_bind = NULL;
61
62 list_for_each_entry(phy_bind, list, list) {
63 if (!(strcmp(phy_bind->dev_name, dev_name(dev))) &&
64 phy_bind->index == index) {
65 if (phy_bind->phy)
66 return phy_bind->phy;
67 else
68 return ERR_PTR(-EPROBE_DEFER);
69 }
70 }
71
72 return ERR_PTR(-ENODEV);
73}
74
75static struct usb_phy *__of_usb_find_phy(struct device_node *node)
76{
77 struct usb_phy *phy;
78
79 if (!of_device_is_available(node))
80 return ERR_PTR(-ENODEV);
81
82 list_for_each_entry(phy, &phy_list, head) {
83 if (node != phy->dev->of_node)
84 continue;
85
86 return phy;
87 }
88
89 return ERR_PTR(-EPROBE_DEFER);
90}
91
92static void usb_phy_set_default_current(struct usb_phy *usb_phy)
93{
94 usb_phy->chg_cur.sdp_min = DEFAULT_SDP_CUR_MIN;
95 usb_phy->chg_cur.sdp_max = DEFAULT_SDP_CUR_MAX;
96 usb_phy->chg_cur.dcp_min = DEFAULT_DCP_CUR_MIN;
97 usb_phy->chg_cur.dcp_max = DEFAULT_DCP_CUR_MAX;
98 usb_phy->chg_cur.cdp_min = DEFAULT_CDP_CUR_MIN;
99 usb_phy->chg_cur.cdp_max = DEFAULT_CDP_CUR_MAX;
100 usb_phy->chg_cur.aca_min = DEFAULT_ACA_CUR_MIN;
101 usb_phy->chg_cur.aca_max = DEFAULT_ACA_CUR_MAX;
102}
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120static void usb_phy_notify_charger_work(struct work_struct *work)
121{
122 struct usb_phy *usb_phy = container_of(work, struct usb_phy, chg_work);
123 char uchger_state[50] = { 0 };
124 char *envp[] = { uchger_state, NULL };
125 unsigned int min, max;
126
127 switch (usb_phy->chg_state) {
128 case USB_CHARGER_PRESENT:
129 usb_phy_get_charger_current(usb_phy, &min, &max);
130
131 atomic_notifier_call_chain(&usb_phy->notifier, max, usb_phy);
132 snprintf(uchger_state, ARRAY_SIZE(uchger_state),
133 "USB_CHARGER_STATE=%s", "USB_CHARGER_PRESENT");
134 break;
135 case USB_CHARGER_ABSENT:
136 usb_phy_set_default_current(usb_phy);
137
138 atomic_notifier_call_chain(&usb_phy->notifier, 0, usb_phy);
139 snprintf(uchger_state, ARRAY_SIZE(uchger_state),
140 "USB_CHARGER_STATE=%s", "USB_CHARGER_ABSENT");
141 break;
142 default:
143 dev_warn(usb_phy->dev, "Unknown USB charger state: %d\n",
144 usb_phy->chg_state);
145 return;
146 }
147
148 kobject_uevent_env(&usb_phy->dev->kobj, KOBJ_CHANGE, envp);
149}
150
151static void __usb_phy_get_charger_type(struct usb_phy *usb_phy)
152{
153 if (extcon_get_state(usb_phy->edev, EXTCON_CHG_USB_SDP) > 0) {
154 usb_phy->chg_type = SDP_TYPE;
155 usb_phy->chg_state = USB_CHARGER_PRESENT;
156 } else if (extcon_get_state(usb_phy->edev, EXTCON_CHG_USB_CDP) > 0) {
157 usb_phy->chg_type = CDP_TYPE;
158 usb_phy->chg_state = USB_CHARGER_PRESENT;
159 } else if (extcon_get_state(usb_phy->edev, EXTCON_CHG_USB_DCP) > 0) {
160 usb_phy->chg_type = DCP_TYPE;
161 usb_phy->chg_state = USB_CHARGER_PRESENT;
162 } else if (extcon_get_state(usb_phy->edev, EXTCON_CHG_USB_ACA) > 0) {
163 usb_phy->chg_type = ACA_TYPE;
164 usb_phy->chg_state = USB_CHARGER_PRESENT;
165 } else {
166 usb_phy->chg_type = UNKNOWN_TYPE;
167 usb_phy->chg_state = USB_CHARGER_ABSENT;
168 }
169
170 schedule_work(&usb_phy->chg_work);
171}
172
173
174
175
176
177
178
179
180
181
182static int usb_phy_get_charger_type(struct notifier_block *nb,
183 unsigned long state, void *data)
184{
185 struct usb_phy *usb_phy = container_of(nb, struct usb_phy, type_nb);
186
187 __usb_phy_get_charger_type(usb_phy);
188 return NOTIFY_OK;
189}
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205void usb_phy_set_charger_current(struct usb_phy *usb_phy, unsigned int mA)
206{
207 switch (usb_phy->chg_type) {
208 case SDP_TYPE:
209 if (usb_phy->chg_cur.sdp_max == mA)
210 return;
211
212 usb_phy->chg_cur.sdp_max = (mA > DEFAULT_SDP_CUR_MAX_SS) ?
213 DEFAULT_SDP_CUR_MAX_SS : mA;
214 break;
215 case DCP_TYPE:
216 if (usb_phy->chg_cur.dcp_max == mA)
217 return;
218
219 usb_phy->chg_cur.dcp_max = (mA > DEFAULT_DCP_CUR_MAX) ?
220 DEFAULT_DCP_CUR_MAX : mA;
221 break;
222 case CDP_TYPE:
223 if (usb_phy->chg_cur.cdp_max == mA)
224 return;
225
226 usb_phy->chg_cur.cdp_max = (mA > DEFAULT_CDP_CUR_MAX) ?
227 DEFAULT_CDP_CUR_MAX : mA;
228 break;
229 case ACA_TYPE:
230 if (usb_phy->chg_cur.aca_max == mA)
231 return;
232
233 usb_phy->chg_cur.aca_max = (mA > DEFAULT_ACA_CUR_MAX) ?
234 DEFAULT_ACA_CUR_MAX : mA;
235 break;
236 default:
237 return;
238 }
239
240 schedule_work(&usb_phy->chg_work);
241}
242EXPORT_SYMBOL_GPL(usb_phy_set_charger_current);
243
244
245
246
247
248
249
250
251
252
253
254void usb_phy_get_charger_current(struct usb_phy *usb_phy,
255 unsigned int *min, unsigned int *max)
256{
257 switch (usb_phy->chg_type) {
258 case SDP_TYPE:
259 *min = usb_phy->chg_cur.sdp_min;
260 *max = usb_phy->chg_cur.sdp_max;
261 break;
262 case DCP_TYPE:
263 *min = usb_phy->chg_cur.dcp_min;
264 *max = usb_phy->chg_cur.dcp_max;
265 break;
266 case CDP_TYPE:
267 *min = usb_phy->chg_cur.cdp_min;
268 *max = usb_phy->chg_cur.cdp_max;
269 break;
270 case ACA_TYPE:
271 *min = usb_phy->chg_cur.aca_min;
272 *max = usb_phy->chg_cur.aca_max;
273 break;
274 default:
275 *min = 0;
276 *max = 0;
277 break;
278 }
279}
280EXPORT_SYMBOL_GPL(usb_phy_get_charger_current);
281
282
283
284
285
286
287
288
289
290
291void usb_phy_set_charger_state(struct usb_phy *usb_phy,
292 enum usb_charger_state state)
293{
294 if (usb_phy->chg_state == state || !usb_phy->charger_detect)
295 return;
296
297 usb_phy->chg_state = state;
298 if (usb_phy->chg_state == USB_CHARGER_PRESENT)
299 usb_phy->chg_type = usb_phy->charger_detect(usb_phy);
300 else
301 usb_phy->chg_type = UNKNOWN_TYPE;
302
303 schedule_work(&usb_phy->chg_work);
304}
305EXPORT_SYMBOL_GPL(usb_phy_set_charger_state);
306
307static void devm_usb_phy_release(struct device *dev, void *res)
308{
309 struct usb_phy *phy = *(struct usb_phy **)res;
310
311 usb_put_phy(phy);
312}
313
314static void devm_usb_phy_release2(struct device *dev, void *_res)
315{
316 struct phy_devm *res = _res;
317
318 if (res->nb)
319 usb_unregister_notifier(res->phy, res->nb);
320 usb_put_phy(res->phy);
321}
322
323static int devm_usb_phy_match(struct device *dev, void *res, void *match_data)
324{
325 struct usb_phy **phy = res;
326
327 return *phy == match_data;
328}
329
330static int usb_add_extcon(struct usb_phy *x)
331{
332 int ret;
333
334 if (of_property_read_bool(x->dev->of_node, "extcon")) {
335 x->edev = extcon_get_edev_by_phandle(x->dev, 0);
336 if (IS_ERR(x->edev))
337 return PTR_ERR(x->edev);
338
339 x->id_edev = extcon_get_edev_by_phandle(x->dev, 1);
340 if (IS_ERR(x->id_edev)) {
341 x->id_edev = NULL;
342 dev_info(x->dev, "No separate ID extcon device\n");
343 }
344
345 if (x->vbus_nb.notifier_call) {
346 ret = devm_extcon_register_notifier(x->dev, x->edev,
347 EXTCON_USB,
348 &x->vbus_nb);
349 if (ret < 0) {
350 dev_err(x->dev,
351 "register VBUS notifier failed\n");
352 return ret;
353 }
354 } else {
355 x->type_nb.notifier_call = usb_phy_get_charger_type;
356
357 ret = devm_extcon_register_notifier(x->dev, x->edev,
358 EXTCON_CHG_USB_SDP,
359 &x->type_nb);
360 if (ret) {
361 dev_err(x->dev,
362 "register extcon USB SDP failed.\n");
363 return ret;
364 }
365
366 ret = devm_extcon_register_notifier(x->dev, x->edev,
367 EXTCON_CHG_USB_CDP,
368 &x->type_nb);
369 if (ret) {
370 dev_err(x->dev,
371 "register extcon USB CDP failed.\n");
372 return ret;
373 }
374
375 ret = devm_extcon_register_notifier(x->dev, x->edev,
376 EXTCON_CHG_USB_DCP,
377 &x->type_nb);
378 if (ret) {
379 dev_err(x->dev,
380 "register extcon USB DCP failed.\n");
381 return ret;
382 }
383
384 ret = devm_extcon_register_notifier(x->dev, x->edev,
385 EXTCON_CHG_USB_ACA,
386 &x->type_nb);
387 if (ret) {
388 dev_err(x->dev,
389 "register extcon USB ACA failed.\n");
390 return ret;
391 }
392 }
393
394 if (x->id_nb.notifier_call) {
395 struct extcon_dev *id_ext;
396
397 if (x->id_edev)
398 id_ext = x->id_edev;
399 else
400 id_ext = x->edev;
401
402 ret = devm_extcon_register_notifier(x->dev, id_ext,
403 EXTCON_USB_HOST,
404 &x->id_nb);
405 if (ret < 0) {
406 dev_err(x->dev,
407 "register ID notifier failed\n");
408 return ret;
409 }
410 }
411 }
412
413 usb_phy_set_default_current(x);
414 INIT_WORK(&x->chg_work, usb_phy_notify_charger_work);
415 x->chg_type = UNKNOWN_TYPE;
416 x->chg_state = USB_CHARGER_DEFAULT;
417 if (x->type_nb.notifier_call)
418 __usb_phy_get_charger_type(x);
419
420 return 0;
421}
422
423
424
425
426
427
428
429
430
431
432
433
434struct usb_phy *devm_usb_get_phy(struct device *dev, enum usb_phy_type type)
435{
436 struct usb_phy **ptr, *phy;
437
438 ptr = devres_alloc(devm_usb_phy_release, sizeof(*ptr), GFP_KERNEL);
439 if (!ptr)
440 return ERR_PTR(-ENOMEM);
441
442 phy = usb_get_phy(type);
443 if (!IS_ERR(phy)) {
444 *ptr = phy;
445 devres_add(dev, ptr);
446 } else
447 devres_free(ptr);
448
449 return phy;
450}
451EXPORT_SYMBOL_GPL(devm_usb_get_phy);
452
453
454
455
456
457
458
459
460
461
462
463struct usb_phy *usb_get_phy(enum usb_phy_type type)
464{
465 struct usb_phy *phy = NULL;
466 unsigned long flags;
467
468 spin_lock_irqsave(&phy_lock, flags);
469
470 phy = __usb_find_phy(&phy_list, type);
471 if (IS_ERR(phy) || !try_module_get(phy->dev->driver->owner)) {
472 pr_debug("PHY: unable to find transceiver of type %s\n",
473 usb_phy_type_string(type));
474 if (!IS_ERR(phy))
475 phy = ERR_PTR(-ENODEV);
476
477 goto err0;
478 }
479
480 get_device(phy->dev);
481
482err0:
483 spin_unlock_irqrestore(&phy_lock, flags);
484
485 return phy;
486}
487EXPORT_SYMBOL_GPL(usb_get_phy);
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505struct usb_phy *devm_usb_get_phy_by_node(struct device *dev,
506 struct device_node *node,
507 struct notifier_block *nb)
508{
509 struct usb_phy *phy = ERR_PTR(-ENOMEM);
510 struct phy_devm *ptr;
511 unsigned long flags;
512
513 ptr = devres_alloc(devm_usb_phy_release2, sizeof(*ptr), GFP_KERNEL);
514 if (!ptr) {
515 dev_dbg(dev, "failed to allocate memory for devres\n");
516 goto err0;
517 }
518
519 spin_lock_irqsave(&phy_lock, flags);
520
521 phy = __of_usb_find_phy(node);
522 if (IS_ERR(phy)) {
523 devres_free(ptr);
524 goto err1;
525 }
526
527 if (!try_module_get(phy->dev->driver->owner)) {
528 phy = ERR_PTR(-ENODEV);
529 devres_free(ptr);
530 goto err1;
531 }
532 if (nb)
533 usb_register_notifier(phy, nb);
534 ptr->phy = phy;
535 ptr->nb = nb;
536 devres_add(dev, ptr);
537
538 get_device(phy->dev);
539
540err1:
541 spin_unlock_irqrestore(&phy_lock, flags);
542
543err0:
544
545 return phy;
546}
547EXPORT_SYMBOL_GPL(devm_usb_get_phy_by_node);
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564struct usb_phy *devm_usb_get_phy_by_phandle(struct device *dev,
565 const char *phandle, u8 index)
566{
567 struct device_node *node;
568 struct usb_phy *phy;
569
570 if (!dev->of_node) {
571 dev_dbg(dev, "device does not have a device node entry\n");
572 return ERR_PTR(-EINVAL);
573 }
574
575 node = of_parse_phandle(dev->of_node, phandle, index);
576 if (!node) {
577 dev_dbg(dev, "failed to get %s phandle in %pOF node\n", phandle,
578 dev->of_node);
579 return ERR_PTR(-ENODEV);
580 }
581 phy = devm_usb_get_phy_by_node(dev, node, NULL);
582 of_node_put(node);
583 return phy;
584}
585EXPORT_SYMBOL_GPL(devm_usb_get_phy_by_phandle);
586
587
588
589
590
591
592
593
594
595
596
597
598struct usb_phy *usb_get_phy_dev(struct device *dev, u8 index)
599{
600 struct usb_phy *phy = NULL;
601 unsigned long flags;
602
603 spin_lock_irqsave(&phy_lock, flags);
604
605 phy = __usb_find_phy_dev(dev, &phy_bind_list, index);
606 if (IS_ERR(phy) || !try_module_get(phy->dev->driver->owner)) {
607 dev_dbg(dev, "unable to find transceiver\n");
608 if (!IS_ERR(phy))
609 phy = ERR_PTR(-ENODEV);
610
611 goto err0;
612 }
613
614 get_device(phy->dev);
615
616err0:
617 spin_unlock_irqrestore(&phy_lock, flags);
618
619 return phy;
620}
621EXPORT_SYMBOL_GPL(usb_get_phy_dev);
622
623
624
625
626
627
628
629
630
631
632
633
634struct usb_phy *devm_usb_get_phy_dev(struct device *dev, u8 index)
635{
636 struct usb_phy **ptr, *phy;
637
638 ptr = devres_alloc(devm_usb_phy_release, sizeof(*ptr), GFP_KERNEL);
639 if (!ptr)
640 return NULL;
641
642 phy = usb_get_phy_dev(dev, index);
643 if (!IS_ERR(phy)) {
644 *ptr = phy;
645 devres_add(dev, ptr);
646 } else
647 devres_free(ptr);
648
649 return phy;
650}
651EXPORT_SYMBOL_GPL(devm_usb_get_phy_dev);
652
653
654
655
656
657
658
659
660
661
662
663void devm_usb_put_phy(struct device *dev, struct usb_phy *phy)
664{
665 int r;
666
667 r = devres_destroy(dev, devm_usb_phy_release, devm_usb_phy_match, phy);
668 dev_WARN_ONCE(dev, r, "couldn't find PHY resource\n");
669}
670EXPORT_SYMBOL_GPL(devm_usb_put_phy);
671
672
673
674
675
676
677
678
679
680void usb_put_phy(struct usb_phy *x)
681{
682 if (x) {
683 struct module *owner = x->dev->driver->owner;
684
685 put_device(x->dev);
686 module_put(owner);
687 }
688}
689EXPORT_SYMBOL_GPL(usb_put_phy);
690
691
692
693
694
695
696
697
698
699
700int usb_add_phy(struct usb_phy *x, enum usb_phy_type type)
701{
702 int ret = 0;
703 unsigned long flags;
704 struct usb_phy *phy;
705
706 if (x->type != USB_PHY_TYPE_UNDEFINED) {
707 dev_err(x->dev, "not accepting initialized PHY %s\n", x->label);
708 return -EINVAL;
709 }
710
711 ret = usb_add_extcon(x);
712 if (ret)
713 return ret;
714
715 ATOMIC_INIT_NOTIFIER_HEAD(&x->notifier);
716
717 spin_lock_irqsave(&phy_lock, flags);
718
719 list_for_each_entry(phy, &phy_list, head) {
720 if (phy->type == type) {
721 ret = -EBUSY;
722 dev_err(x->dev, "transceiver type %s already exists\n",
723 usb_phy_type_string(type));
724 goto out;
725 }
726 }
727
728 x->type = type;
729 list_add_tail(&x->head, &phy_list);
730
731out:
732 spin_unlock_irqrestore(&phy_lock, flags);
733 return ret;
734}
735EXPORT_SYMBOL_GPL(usb_add_phy);
736
737
738
739
740
741
742
743
744
745int usb_add_phy_dev(struct usb_phy *x)
746{
747 struct usb_phy_bind *phy_bind;
748 unsigned long flags;
749 int ret;
750
751 if (!x->dev) {
752 dev_err(x->dev, "no device provided for PHY\n");
753 return -EINVAL;
754 }
755
756 ret = usb_add_extcon(x);
757 if (ret)
758 return ret;
759
760 ATOMIC_INIT_NOTIFIER_HEAD(&x->notifier);
761
762 spin_lock_irqsave(&phy_lock, flags);
763 list_for_each_entry(phy_bind, &phy_bind_list, list)
764 if (!(strcmp(phy_bind->phy_dev_name, dev_name(x->dev))))
765 phy_bind->phy = x;
766
767 list_add_tail(&x->head, &phy_list);
768
769 spin_unlock_irqrestore(&phy_lock, flags);
770 return 0;
771}
772EXPORT_SYMBOL_GPL(usb_add_phy_dev);
773
774
775
776
777
778
779
780void usb_remove_phy(struct usb_phy *x)
781{
782 unsigned long flags;
783 struct usb_phy_bind *phy_bind;
784
785 spin_lock_irqsave(&phy_lock, flags);
786 if (x) {
787 list_for_each_entry(phy_bind, &phy_bind_list, list)
788 if (phy_bind->phy == x)
789 phy_bind->phy = NULL;
790 list_del(&x->head);
791 }
792 spin_unlock_irqrestore(&phy_lock, flags);
793}
794EXPORT_SYMBOL_GPL(usb_remove_phy);
795
796
797
798
799
800
801
802
803
804
805
806
807
808int usb_bind_phy(const char *dev_name, u8 index,
809 const char *phy_dev_name)
810{
811 struct usb_phy_bind *phy_bind;
812 unsigned long flags;
813
814 phy_bind = kzalloc(sizeof(*phy_bind), GFP_KERNEL);
815 if (!phy_bind)
816 return -ENOMEM;
817
818 phy_bind->dev_name = dev_name;
819 phy_bind->phy_dev_name = phy_dev_name;
820 phy_bind->index = index;
821
822 spin_lock_irqsave(&phy_lock, flags);
823 list_add_tail(&phy_bind->list, &phy_bind_list);
824 spin_unlock_irqrestore(&phy_lock, flags);
825
826 return 0;
827}
828EXPORT_SYMBOL_GPL(usb_bind_phy);
829
830
831
832
833
834
835
836void usb_phy_set_event(struct usb_phy *x, unsigned long event)
837{
838 x->last_event = event;
839}
840EXPORT_SYMBOL_GPL(usb_phy_set_event);
841