0001
0002
0003
0004
0005
0006
0007 #include <linux/kernel.h>
0008 #include <linux/export.h>
0009 #include <linux/err.h>
0010 #include <linux/device.h>
0011 #include <linux/module.h>
0012 #include <linux/slab.h>
0013 #include <linux/of.h>
0014
0015 #include <linux/usb/phy.h>
0016
0017
0018 #define DEFAULT_SDP_CUR_MIN 2
0019 #define DEFAULT_SDP_CUR_MAX 500
0020 #define DEFAULT_SDP_CUR_MIN_SS 150
0021 #define DEFAULT_SDP_CUR_MAX_SS 900
0022 #define DEFAULT_DCP_CUR_MIN 500
0023 #define DEFAULT_DCP_CUR_MAX 5000
0024 #define DEFAULT_CDP_CUR_MIN 1500
0025 #define DEFAULT_CDP_CUR_MAX 5000
0026 #define DEFAULT_ACA_CUR_MIN 1500
0027 #define DEFAULT_ACA_CUR_MAX 5000
0028
0029 static LIST_HEAD(phy_list);
0030 static DEFINE_SPINLOCK(phy_lock);
0031
0032 struct phy_devm {
0033 struct usb_phy *phy;
0034 struct notifier_block *nb;
0035 };
0036
0037 static const char *const usb_chger_type[] = {
0038 [UNKNOWN_TYPE] = "USB_CHARGER_UNKNOWN_TYPE",
0039 [SDP_TYPE] = "USB_CHARGER_SDP_TYPE",
0040 [CDP_TYPE] = "USB_CHARGER_CDP_TYPE",
0041 [DCP_TYPE] = "USB_CHARGER_DCP_TYPE",
0042 [ACA_TYPE] = "USB_CHARGER_ACA_TYPE",
0043 };
0044
0045 static const char *const usb_chger_state[] = {
0046 [USB_CHARGER_DEFAULT] = "USB_CHARGER_DEFAULT",
0047 [USB_CHARGER_PRESENT] = "USB_CHARGER_PRESENT",
0048 [USB_CHARGER_ABSENT] = "USB_CHARGER_ABSENT",
0049 };
0050
0051 static struct usb_phy *__usb_find_phy(struct list_head *list,
0052 enum usb_phy_type type)
0053 {
0054 struct usb_phy *phy = NULL;
0055
0056 list_for_each_entry(phy, list, head) {
0057 if (phy->type != type)
0058 continue;
0059
0060 return phy;
0061 }
0062
0063 return ERR_PTR(-ENODEV);
0064 }
0065
0066 static struct usb_phy *__of_usb_find_phy(struct device_node *node)
0067 {
0068 struct usb_phy *phy;
0069
0070 if (!of_device_is_available(node))
0071 return ERR_PTR(-ENODEV);
0072
0073 list_for_each_entry(phy, &phy_list, head) {
0074 if (node != phy->dev->of_node)
0075 continue;
0076
0077 return phy;
0078 }
0079
0080 return ERR_PTR(-EPROBE_DEFER);
0081 }
0082
0083 static struct usb_phy *__device_to_usb_phy(struct device *dev)
0084 {
0085 struct usb_phy *usb_phy;
0086
0087 list_for_each_entry(usb_phy, &phy_list, head) {
0088 if (usb_phy->dev == dev)
0089 return usb_phy;
0090 }
0091
0092 return NULL;
0093 }
0094
0095 static void usb_phy_set_default_current(struct usb_phy *usb_phy)
0096 {
0097 usb_phy->chg_cur.sdp_min = DEFAULT_SDP_CUR_MIN;
0098 usb_phy->chg_cur.sdp_max = DEFAULT_SDP_CUR_MAX;
0099 usb_phy->chg_cur.dcp_min = DEFAULT_DCP_CUR_MIN;
0100 usb_phy->chg_cur.dcp_max = DEFAULT_DCP_CUR_MAX;
0101 usb_phy->chg_cur.cdp_min = DEFAULT_CDP_CUR_MIN;
0102 usb_phy->chg_cur.cdp_max = DEFAULT_CDP_CUR_MAX;
0103 usb_phy->chg_cur.aca_min = DEFAULT_ACA_CUR_MIN;
0104 usb_phy->chg_cur.aca_max = DEFAULT_ACA_CUR_MAX;
0105 }
0106
0107
0108
0109
0110
0111
0112
0113
0114
0115
0116
0117
0118
0119
0120
0121
0122
0123 static void usb_phy_notify_charger_work(struct work_struct *work)
0124 {
0125 struct usb_phy *usb_phy = container_of(work, struct usb_phy, chg_work);
0126 unsigned int min, max;
0127
0128 switch (usb_phy->chg_state) {
0129 case USB_CHARGER_PRESENT:
0130 usb_phy_get_charger_current(usb_phy, &min, &max);
0131
0132 atomic_notifier_call_chain(&usb_phy->notifier, max, usb_phy);
0133 break;
0134 case USB_CHARGER_ABSENT:
0135 usb_phy_set_default_current(usb_phy);
0136
0137 atomic_notifier_call_chain(&usb_phy->notifier, 0, usb_phy);
0138 break;
0139 default:
0140 dev_warn(usb_phy->dev, "Unknown USB charger state: %d\n",
0141 usb_phy->chg_state);
0142 return;
0143 }
0144
0145 kobject_uevent(&usb_phy->dev->kobj, KOBJ_CHANGE);
0146 }
0147
0148 static int usb_phy_uevent(struct device *dev, struct kobj_uevent_env *env)
0149 {
0150 struct usb_phy *usb_phy;
0151 char uchger_state[50] = { 0 };
0152 char uchger_type[50] = { 0 };
0153 unsigned long flags;
0154
0155 spin_lock_irqsave(&phy_lock, flags);
0156 usb_phy = __device_to_usb_phy(dev);
0157 spin_unlock_irqrestore(&phy_lock, flags);
0158
0159 if (!usb_phy)
0160 return -ENODEV;
0161
0162 snprintf(uchger_state, ARRAY_SIZE(uchger_state),
0163 "USB_CHARGER_STATE=%s", usb_chger_state[usb_phy->chg_state]);
0164
0165 snprintf(uchger_type, ARRAY_SIZE(uchger_type),
0166 "USB_CHARGER_TYPE=%s", usb_chger_type[usb_phy->chg_type]);
0167
0168 if (add_uevent_var(env, uchger_state))
0169 return -ENOMEM;
0170
0171 if (add_uevent_var(env, uchger_type))
0172 return -ENOMEM;
0173
0174 return 0;
0175 }
0176
0177 static void __usb_phy_get_charger_type(struct usb_phy *usb_phy)
0178 {
0179 if (extcon_get_state(usb_phy->edev, EXTCON_CHG_USB_SDP) > 0) {
0180 usb_phy->chg_type = SDP_TYPE;
0181 usb_phy->chg_state = USB_CHARGER_PRESENT;
0182 } else if (extcon_get_state(usb_phy->edev, EXTCON_CHG_USB_CDP) > 0) {
0183 usb_phy->chg_type = CDP_TYPE;
0184 usb_phy->chg_state = USB_CHARGER_PRESENT;
0185 } else if (extcon_get_state(usb_phy->edev, EXTCON_CHG_USB_DCP) > 0) {
0186 usb_phy->chg_type = DCP_TYPE;
0187 usb_phy->chg_state = USB_CHARGER_PRESENT;
0188 } else if (extcon_get_state(usb_phy->edev, EXTCON_CHG_USB_ACA) > 0) {
0189 usb_phy->chg_type = ACA_TYPE;
0190 usb_phy->chg_state = USB_CHARGER_PRESENT;
0191 } else {
0192 usb_phy->chg_type = UNKNOWN_TYPE;
0193 usb_phy->chg_state = USB_CHARGER_ABSENT;
0194 }
0195
0196 schedule_work(&usb_phy->chg_work);
0197 }
0198
0199
0200
0201
0202
0203
0204
0205
0206
0207
0208 static int usb_phy_get_charger_type(struct notifier_block *nb,
0209 unsigned long state, void *data)
0210 {
0211 struct usb_phy *usb_phy = container_of(nb, struct usb_phy, type_nb);
0212
0213 __usb_phy_get_charger_type(usb_phy);
0214 return NOTIFY_OK;
0215 }
0216
0217
0218
0219
0220
0221
0222
0223
0224
0225
0226
0227
0228
0229
0230
0231 void usb_phy_set_charger_current(struct usb_phy *usb_phy, unsigned int mA)
0232 {
0233 switch (usb_phy->chg_type) {
0234 case SDP_TYPE:
0235 if (usb_phy->chg_cur.sdp_max == mA)
0236 return;
0237
0238 usb_phy->chg_cur.sdp_max = (mA > DEFAULT_SDP_CUR_MAX_SS) ?
0239 DEFAULT_SDP_CUR_MAX_SS : mA;
0240 break;
0241 case DCP_TYPE:
0242 if (usb_phy->chg_cur.dcp_max == mA)
0243 return;
0244
0245 usb_phy->chg_cur.dcp_max = (mA > DEFAULT_DCP_CUR_MAX) ?
0246 DEFAULT_DCP_CUR_MAX : mA;
0247 break;
0248 case CDP_TYPE:
0249 if (usb_phy->chg_cur.cdp_max == mA)
0250 return;
0251
0252 usb_phy->chg_cur.cdp_max = (mA > DEFAULT_CDP_CUR_MAX) ?
0253 DEFAULT_CDP_CUR_MAX : mA;
0254 break;
0255 case ACA_TYPE:
0256 if (usb_phy->chg_cur.aca_max == mA)
0257 return;
0258
0259 usb_phy->chg_cur.aca_max = (mA > DEFAULT_ACA_CUR_MAX) ?
0260 DEFAULT_ACA_CUR_MAX : mA;
0261 break;
0262 default:
0263 return;
0264 }
0265
0266 schedule_work(&usb_phy->chg_work);
0267 }
0268 EXPORT_SYMBOL_GPL(usb_phy_set_charger_current);
0269
0270
0271
0272
0273
0274
0275
0276
0277
0278
0279
0280 void usb_phy_get_charger_current(struct usb_phy *usb_phy,
0281 unsigned int *min, unsigned int *max)
0282 {
0283 switch (usb_phy->chg_type) {
0284 case SDP_TYPE:
0285 *min = usb_phy->chg_cur.sdp_min;
0286 *max = usb_phy->chg_cur.sdp_max;
0287 break;
0288 case DCP_TYPE:
0289 *min = usb_phy->chg_cur.dcp_min;
0290 *max = usb_phy->chg_cur.dcp_max;
0291 break;
0292 case CDP_TYPE:
0293 *min = usb_phy->chg_cur.cdp_min;
0294 *max = usb_phy->chg_cur.cdp_max;
0295 break;
0296 case ACA_TYPE:
0297 *min = usb_phy->chg_cur.aca_min;
0298 *max = usb_phy->chg_cur.aca_max;
0299 break;
0300 default:
0301 *min = 0;
0302 *max = 0;
0303 break;
0304 }
0305 }
0306 EXPORT_SYMBOL_GPL(usb_phy_get_charger_current);
0307
0308
0309
0310
0311
0312
0313
0314
0315
0316
0317 void usb_phy_set_charger_state(struct usb_phy *usb_phy,
0318 enum usb_charger_state state)
0319 {
0320 if (usb_phy->chg_state == state || !usb_phy->charger_detect)
0321 return;
0322
0323 usb_phy->chg_state = state;
0324 if (usb_phy->chg_state == USB_CHARGER_PRESENT)
0325 usb_phy->chg_type = usb_phy->charger_detect(usb_phy);
0326 else
0327 usb_phy->chg_type = UNKNOWN_TYPE;
0328
0329 schedule_work(&usb_phy->chg_work);
0330 }
0331 EXPORT_SYMBOL_GPL(usb_phy_set_charger_state);
0332
0333 static void devm_usb_phy_release(struct device *dev, void *res)
0334 {
0335 struct usb_phy *phy = *(struct usb_phy **)res;
0336
0337 usb_put_phy(phy);
0338 }
0339
0340 static void devm_usb_phy_release2(struct device *dev, void *_res)
0341 {
0342 struct phy_devm *res = _res;
0343
0344 if (res->nb)
0345 usb_unregister_notifier(res->phy, res->nb);
0346 usb_put_phy(res->phy);
0347 }
0348
0349 static int devm_usb_phy_match(struct device *dev, void *res, void *match_data)
0350 {
0351 struct usb_phy **phy = res;
0352
0353 return *phy == match_data;
0354 }
0355
0356 static void usb_charger_init(struct usb_phy *usb_phy)
0357 {
0358 usb_phy->chg_type = UNKNOWN_TYPE;
0359 usb_phy->chg_state = USB_CHARGER_DEFAULT;
0360 usb_phy_set_default_current(usb_phy);
0361 INIT_WORK(&usb_phy->chg_work, usb_phy_notify_charger_work);
0362 }
0363
0364 static int usb_add_extcon(struct usb_phy *x)
0365 {
0366 int ret;
0367
0368 if (of_property_read_bool(x->dev->of_node, "extcon")) {
0369 x->edev = extcon_get_edev_by_phandle(x->dev, 0);
0370 if (IS_ERR(x->edev))
0371 return PTR_ERR(x->edev);
0372
0373 x->id_edev = extcon_get_edev_by_phandle(x->dev, 1);
0374 if (IS_ERR(x->id_edev)) {
0375 x->id_edev = NULL;
0376 dev_info(x->dev, "No separate ID extcon device\n");
0377 }
0378
0379 if (x->vbus_nb.notifier_call) {
0380 ret = devm_extcon_register_notifier(x->dev, x->edev,
0381 EXTCON_USB,
0382 &x->vbus_nb);
0383 if (ret < 0) {
0384 dev_err(x->dev,
0385 "register VBUS notifier failed\n");
0386 return ret;
0387 }
0388 } else {
0389 x->type_nb.notifier_call = usb_phy_get_charger_type;
0390
0391 ret = devm_extcon_register_notifier(x->dev, x->edev,
0392 EXTCON_CHG_USB_SDP,
0393 &x->type_nb);
0394 if (ret) {
0395 dev_err(x->dev,
0396 "register extcon USB SDP failed.\n");
0397 return ret;
0398 }
0399
0400 ret = devm_extcon_register_notifier(x->dev, x->edev,
0401 EXTCON_CHG_USB_CDP,
0402 &x->type_nb);
0403 if (ret) {
0404 dev_err(x->dev,
0405 "register extcon USB CDP failed.\n");
0406 return ret;
0407 }
0408
0409 ret = devm_extcon_register_notifier(x->dev, x->edev,
0410 EXTCON_CHG_USB_DCP,
0411 &x->type_nb);
0412 if (ret) {
0413 dev_err(x->dev,
0414 "register extcon USB DCP failed.\n");
0415 return ret;
0416 }
0417
0418 ret = devm_extcon_register_notifier(x->dev, x->edev,
0419 EXTCON_CHG_USB_ACA,
0420 &x->type_nb);
0421 if (ret) {
0422 dev_err(x->dev,
0423 "register extcon USB ACA failed.\n");
0424 return ret;
0425 }
0426 }
0427
0428 if (x->id_nb.notifier_call) {
0429 struct extcon_dev *id_ext;
0430
0431 if (x->id_edev)
0432 id_ext = x->id_edev;
0433 else
0434 id_ext = x->edev;
0435
0436 ret = devm_extcon_register_notifier(x->dev, id_ext,
0437 EXTCON_USB_HOST,
0438 &x->id_nb);
0439 if (ret < 0) {
0440 dev_err(x->dev,
0441 "register ID notifier failed\n");
0442 return ret;
0443 }
0444 }
0445 }
0446
0447 if (x->type_nb.notifier_call)
0448 __usb_phy_get_charger_type(x);
0449
0450 return 0;
0451 }
0452
0453
0454
0455
0456
0457
0458
0459
0460
0461
0462
0463
0464 struct usb_phy *devm_usb_get_phy(struct device *dev, enum usb_phy_type type)
0465 {
0466 struct usb_phy **ptr, *phy;
0467
0468 ptr = devres_alloc(devm_usb_phy_release, sizeof(*ptr), GFP_KERNEL);
0469 if (!ptr)
0470 return ERR_PTR(-ENOMEM);
0471
0472 phy = usb_get_phy(type);
0473 if (!IS_ERR(phy)) {
0474 *ptr = phy;
0475 devres_add(dev, ptr);
0476 } else
0477 devres_free(ptr);
0478
0479 return phy;
0480 }
0481 EXPORT_SYMBOL_GPL(devm_usb_get_phy);
0482
0483
0484
0485
0486
0487
0488
0489
0490
0491
0492
0493 struct usb_phy *usb_get_phy(enum usb_phy_type type)
0494 {
0495 struct usb_phy *phy = NULL;
0496 unsigned long flags;
0497
0498 spin_lock_irqsave(&phy_lock, flags);
0499
0500 phy = __usb_find_phy(&phy_list, type);
0501 if (IS_ERR(phy) || !try_module_get(phy->dev->driver->owner)) {
0502 pr_debug("PHY: unable to find transceiver of type %s\n",
0503 usb_phy_type_string(type));
0504 if (!IS_ERR(phy))
0505 phy = ERR_PTR(-ENODEV);
0506
0507 goto err0;
0508 }
0509
0510 get_device(phy->dev);
0511
0512 err0:
0513 spin_unlock_irqrestore(&phy_lock, flags);
0514
0515 return phy;
0516 }
0517 EXPORT_SYMBOL_GPL(usb_get_phy);
0518
0519
0520
0521
0522
0523
0524
0525
0526
0527
0528
0529
0530
0531
0532
0533
0534
0535 struct usb_phy *devm_usb_get_phy_by_node(struct device *dev,
0536 struct device_node *node,
0537 struct notifier_block *nb)
0538 {
0539 struct usb_phy *phy = ERR_PTR(-ENOMEM);
0540 struct phy_devm *ptr;
0541 unsigned long flags;
0542
0543 ptr = devres_alloc(devm_usb_phy_release2, sizeof(*ptr), GFP_KERNEL);
0544 if (!ptr) {
0545 dev_dbg(dev, "failed to allocate memory for devres\n");
0546 goto err0;
0547 }
0548
0549 spin_lock_irqsave(&phy_lock, flags);
0550
0551 phy = __of_usb_find_phy(node);
0552 if (IS_ERR(phy)) {
0553 devres_free(ptr);
0554 goto err1;
0555 }
0556
0557 if (!try_module_get(phy->dev->driver->owner)) {
0558 phy = ERR_PTR(-ENODEV);
0559 devres_free(ptr);
0560 goto err1;
0561 }
0562 if (nb)
0563 usb_register_notifier(phy, nb);
0564 ptr->phy = phy;
0565 ptr->nb = nb;
0566 devres_add(dev, ptr);
0567
0568 get_device(phy->dev);
0569
0570 err1:
0571 spin_unlock_irqrestore(&phy_lock, flags);
0572
0573 err0:
0574
0575 return phy;
0576 }
0577 EXPORT_SYMBOL_GPL(devm_usb_get_phy_by_node);
0578
0579
0580
0581
0582
0583
0584
0585
0586
0587
0588
0589
0590
0591
0592
0593
0594 struct usb_phy *devm_usb_get_phy_by_phandle(struct device *dev,
0595 const char *phandle, u8 index)
0596 {
0597 struct device_node *node;
0598 struct usb_phy *phy;
0599
0600 if (!dev->of_node) {
0601 dev_dbg(dev, "device does not have a device node entry\n");
0602 return ERR_PTR(-EINVAL);
0603 }
0604
0605 node = of_parse_phandle(dev->of_node, phandle, index);
0606 if (!node) {
0607 dev_dbg(dev, "failed to get %s phandle in %pOF node\n", phandle,
0608 dev->of_node);
0609 return ERR_PTR(-ENODEV);
0610 }
0611 phy = devm_usb_get_phy_by_node(dev, node, NULL);
0612 of_node_put(node);
0613 return phy;
0614 }
0615 EXPORT_SYMBOL_GPL(devm_usb_get_phy_by_phandle);
0616
0617
0618
0619
0620
0621
0622
0623
0624
0625
0626
0627 void devm_usb_put_phy(struct device *dev, struct usb_phy *phy)
0628 {
0629 int r;
0630
0631 r = devres_destroy(dev, devm_usb_phy_release, devm_usb_phy_match, phy);
0632 dev_WARN_ONCE(dev, r, "couldn't find PHY resource\n");
0633 }
0634 EXPORT_SYMBOL_GPL(devm_usb_put_phy);
0635
0636
0637
0638
0639
0640
0641
0642
0643
0644 void usb_put_phy(struct usb_phy *x)
0645 {
0646 if (x) {
0647 struct module *owner = x->dev->driver->owner;
0648
0649 put_device(x->dev);
0650 module_put(owner);
0651 }
0652 }
0653 EXPORT_SYMBOL_GPL(usb_put_phy);
0654
0655
0656
0657
0658
0659
0660
0661
0662
0663
0664 int usb_add_phy(struct usb_phy *x, enum usb_phy_type type)
0665 {
0666 int ret = 0;
0667 unsigned long flags;
0668 struct usb_phy *phy;
0669
0670 if (x->type != USB_PHY_TYPE_UNDEFINED) {
0671 dev_err(x->dev, "not accepting initialized PHY %s\n", x->label);
0672 return -EINVAL;
0673 }
0674
0675 usb_charger_init(x);
0676 ret = usb_add_extcon(x);
0677 if (ret)
0678 return ret;
0679
0680 ATOMIC_INIT_NOTIFIER_HEAD(&x->notifier);
0681
0682 spin_lock_irqsave(&phy_lock, flags);
0683
0684 list_for_each_entry(phy, &phy_list, head) {
0685 if (phy->type == type) {
0686 ret = -EBUSY;
0687 dev_err(x->dev, "transceiver type %s already exists\n",
0688 usb_phy_type_string(type));
0689 goto out;
0690 }
0691 }
0692
0693 x->type = type;
0694 list_add_tail(&x->head, &phy_list);
0695
0696 out:
0697 spin_unlock_irqrestore(&phy_lock, flags);
0698 return ret;
0699 }
0700 EXPORT_SYMBOL_GPL(usb_add_phy);
0701
0702 static struct device_type usb_phy_dev_type = {
0703 .name = "usb_phy",
0704 .uevent = usb_phy_uevent,
0705 };
0706
0707
0708
0709
0710
0711
0712
0713
0714
0715 int usb_add_phy_dev(struct usb_phy *x)
0716 {
0717 unsigned long flags;
0718 int ret;
0719
0720 if (!x->dev) {
0721 dev_err(x->dev, "no device provided for PHY\n");
0722 return -EINVAL;
0723 }
0724
0725 usb_charger_init(x);
0726 ret = usb_add_extcon(x);
0727 if (ret)
0728 return ret;
0729
0730 x->dev->type = &usb_phy_dev_type;
0731
0732 ATOMIC_INIT_NOTIFIER_HEAD(&x->notifier);
0733
0734 spin_lock_irqsave(&phy_lock, flags);
0735 list_add_tail(&x->head, &phy_list);
0736 spin_unlock_irqrestore(&phy_lock, flags);
0737
0738 return 0;
0739 }
0740 EXPORT_SYMBOL_GPL(usb_add_phy_dev);
0741
0742
0743
0744
0745
0746
0747
0748 void usb_remove_phy(struct usb_phy *x)
0749 {
0750 unsigned long flags;
0751
0752 spin_lock_irqsave(&phy_lock, flags);
0753 if (x)
0754 list_del(&x->head);
0755 spin_unlock_irqrestore(&phy_lock, flags);
0756 }
0757 EXPORT_SYMBOL_GPL(usb_remove_phy);
0758
0759
0760
0761
0762
0763
0764
0765
0766 void usb_phy_set_event(struct usb_phy *x, unsigned long event)
0767 {
0768 x->last_event = event;
0769 }
0770 EXPORT_SYMBOL_GPL(usb_phy_set_event);