phy.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * phy.c -- USB phy handling
  4. *
  5. * Copyright (C) 2004-2013 Texas Instruments
  6. */
  7. #include <linux/kernel.h>
  8. #include <linux/export.h>
  9. #include <linux/err.h>
  10. #include <linux/device.h>
  11. #include <linux/module.h>
  12. #include <linux/slab.h>
  13. #include <linux/of.h>
  14. #include <linux/usb/phy.h>
  15. /* Default current range by charger type. */
  16. #define DEFAULT_SDP_CUR_MIN 2
  17. #define DEFAULT_SDP_CUR_MAX 500
  18. #define DEFAULT_SDP_CUR_MIN_SS 150
  19. #define DEFAULT_SDP_CUR_MAX_SS 900
  20. #define DEFAULT_DCP_CUR_MIN 500
  21. #define DEFAULT_DCP_CUR_MAX 5000
  22. #define DEFAULT_CDP_CUR_MIN 1500
  23. #define DEFAULT_CDP_CUR_MAX 5000
  24. #define DEFAULT_ACA_CUR_MIN 1500
  25. #define DEFAULT_ACA_CUR_MAX 5000
  26. static LIST_HEAD(phy_list);
  27. static DEFINE_SPINLOCK(phy_lock);
  28. struct phy_devm {
  29. struct usb_phy *phy;
  30. struct notifier_block *nb;
  31. };
  32. static struct usb_phy *__usb_find_phy(struct list_head *list,
  33. enum usb_phy_type type)
  34. {
  35. struct usb_phy *phy = NULL;
  36. list_for_each_entry(phy, list, head) {
  37. if (phy->type != type)
  38. continue;
  39. return phy;
  40. }
  41. return ERR_PTR(-ENODEV);
  42. }
  43. static struct usb_phy *__of_usb_find_phy(struct device_node *node)
  44. {
  45. struct usb_phy *phy;
  46. if (!of_device_is_available(node))
  47. return ERR_PTR(-ENODEV);
  48. list_for_each_entry(phy, &phy_list, head) {
  49. if (node != phy->dev->of_node)
  50. continue;
  51. return phy;
  52. }
  53. return ERR_PTR(-EPROBE_DEFER);
  54. }
  55. static void usb_phy_set_default_current(struct usb_phy *usb_phy)
  56. {
  57. usb_phy->chg_cur.sdp_min = DEFAULT_SDP_CUR_MIN;
  58. usb_phy->chg_cur.sdp_max = DEFAULT_SDP_CUR_MAX;
  59. usb_phy->chg_cur.dcp_min = DEFAULT_DCP_CUR_MIN;
  60. usb_phy->chg_cur.dcp_max = DEFAULT_DCP_CUR_MAX;
  61. usb_phy->chg_cur.cdp_min = DEFAULT_CDP_CUR_MIN;
  62. usb_phy->chg_cur.cdp_max = DEFAULT_CDP_CUR_MAX;
  63. usb_phy->chg_cur.aca_min = DEFAULT_ACA_CUR_MIN;
  64. usb_phy->chg_cur.aca_max = DEFAULT_ACA_CUR_MAX;
  65. }
  66. /**
  67. * usb_phy_notify_charger_work - notify the USB charger state
  68. * @work - the charger work to notify the USB charger state
  69. *
  70. * This work can be issued when USB charger state has been changed or
  71. * USB charger current has been changed, then we can notify the current
  72. * what can be drawn to power user and the charger state to userspace.
  73. *
  74. * If we get the charger type from extcon subsystem, we can notify the
  75. * charger state to power user automatically by usb_phy_get_charger_type()
  76. * issuing from extcon subsystem.
  77. *
  78. * If we get the charger type from ->charger_detect() instead of extcon
  79. * subsystem, the usb phy driver should issue usb_phy_set_charger_state()
  80. * to set charger state when the charger state has been changed.
  81. */
  82. static void usb_phy_notify_charger_work(struct work_struct *work)
  83. {
  84. struct usb_phy *usb_phy = container_of(work, struct usb_phy, chg_work);
  85. char uchger_state[50] = { 0 };
  86. char *envp[] = { uchger_state, NULL };
  87. unsigned int min, max;
  88. switch (usb_phy->chg_state) {
  89. case USB_CHARGER_PRESENT:
  90. usb_phy_get_charger_current(usb_phy, &min, &max);
  91. atomic_notifier_call_chain(&usb_phy->notifier, max, usb_phy);
  92. snprintf(uchger_state, ARRAY_SIZE(uchger_state),
  93. "USB_CHARGER_STATE=%s", "USB_CHARGER_PRESENT");
  94. break;
  95. case USB_CHARGER_ABSENT:
  96. usb_phy_set_default_current(usb_phy);
  97. atomic_notifier_call_chain(&usb_phy->notifier, 0, usb_phy);
  98. snprintf(uchger_state, ARRAY_SIZE(uchger_state),
  99. "USB_CHARGER_STATE=%s", "USB_CHARGER_ABSENT");
  100. break;
  101. default:
  102. dev_warn(usb_phy->dev, "Unknown USB charger state: %d\n",
  103. usb_phy->chg_state);
  104. return;
  105. }
  106. kobject_uevent_env(&usb_phy->dev->kobj, KOBJ_CHANGE, envp);
  107. }
  108. static void __usb_phy_get_charger_type(struct usb_phy *usb_phy)
  109. {
  110. if (extcon_get_state(usb_phy->edev, EXTCON_CHG_USB_SDP) > 0) {
  111. usb_phy->chg_type = SDP_TYPE;
  112. usb_phy->chg_state = USB_CHARGER_PRESENT;
  113. } else if (extcon_get_state(usb_phy->edev, EXTCON_CHG_USB_CDP) > 0) {
  114. usb_phy->chg_type = CDP_TYPE;
  115. usb_phy->chg_state = USB_CHARGER_PRESENT;
  116. } else if (extcon_get_state(usb_phy->edev, EXTCON_CHG_USB_DCP) > 0) {
  117. usb_phy->chg_type = DCP_TYPE;
  118. usb_phy->chg_state = USB_CHARGER_PRESENT;
  119. } else if (extcon_get_state(usb_phy->edev, EXTCON_CHG_USB_ACA) > 0) {
  120. usb_phy->chg_type = ACA_TYPE;
  121. usb_phy->chg_state = USB_CHARGER_PRESENT;
  122. } else {
  123. usb_phy->chg_type = UNKNOWN_TYPE;
  124. usb_phy->chg_state = USB_CHARGER_ABSENT;
  125. }
  126. schedule_work(&usb_phy->chg_work);
  127. }
  128. /**
  129. * usb_phy_get_charger_type - get charger type from extcon subsystem
  130. * @nb -the notifier block to determine charger type
  131. * @state - the cable state
  132. * @data - private data
  133. *
  134. * Determin the charger type from extcon subsystem which also means the
  135. * charger state has been chaned, then we should notify this event.
  136. */
  137. static int usb_phy_get_charger_type(struct notifier_block *nb,
  138. unsigned long state, void *data)
  139. {
  140. struct usb_phy *usb_phy = container_of(nb, struct usb_phy, type_nb);
  141. __usb_phy_get_charger_type(usb_phy);
  142. return NOTIFY_OK;
  143. }
  144. /**
  145. * usb_phy_set_charger_current - set the USB charger current
  146. * @usb_phy - the USB phy to be used
  147. * @mA - the current need to be set
  148. *
  149. * Usually we only change the charger default current when USB finished the
  150. * enumeration as one SDP charger. As one SDP charger, usb_phy_set_power()
  151. * will issue this function to change charger current when after setting USB
  152. * configuration, or suspend/resume USB. For other type charger, we should
  153. * use the default charger current and we do not suggest to issue this function
  154. * to change the charger current.
  155. *
  156. * When USB charger current has been changed, we need to notify the power users.
  157. */
  158. void usb_phy_set_charger_current(struct usb_phy *usb_phy, unsigned int mA)
  159. {
  160. switch (usb_phy->chg_type) {
  161. case SDP_TYPE:
  162. if (usb_phy->chg_cur.sdp_max == mA)
  163. return;
  164. usb_phy->chg_cur.sdp_max = (mA > DEFAULT_SDP_CUR_MAX_SS) ?
  165. DEFAULT_SDP_CUR_MAX_SS : mA;
  166. break;
  167. case DCP_TYPE:
  168. if (usb_phy->chg_cur.dcp_max == mA)
  169. return;
  170. usb_phy->chg_cur.dcp_max = (mA > DEFAULT_DCP_CUR_MAX) ?
  171. DEFAULT_DCP_CUR_MAX : mA;
  172. break;
  173. case CDP_TYPE:
  174. if (usb_phy->chg_cur.cdp_max == mA)
  175. return;
  176. usb_phy->chg_cur.cdp_max = (mA > DEFAULT_CDP_CUR_MAX) ?
  177. DEFAULT_CDP_CUR_MAX : mA;
  178. break;
  179. case ACA_TYPE:
  180. if (usb_phy->chg_cur.aca_max == mA)
  181. return;
  182. usb_phy->chg_cur.aca_max = (mA > DEFAULT_ACA_CUR_MAX) ?
  183. DEFAULT_ACA_CUR_MAX : mA;
  184. break;
  185. default:
  186. return;
  187. }
  188. schedule_work(&usb_phy->chg_work);
  189. }
  190. EXPORT_SYMBOL_GPL(usb_phy_set_charger_current);
  191. /**
  192. * usb_phy_get_charger_current - get the USB charger current
  193. * @usb_phy - the USB phy to be used
  194. * @min - the minimum current
  195. * @max - the maximum current
  196. *
  197. * Usually we will notify the maximum current to power user, but for some
  198. * special case, power user also need the minimum current value. Then the
  199. * power user can issue this function to get the suitable current.
  200. */
  201. void usb_phy_get_charger_current(struct usb_phy *usb_phy,
  202. unsigned int *min, unsigned int *max)
  203. {
  204. switch (usb_phy->chg_type) {
  205. case SDP_TYPE:
  206. *min = usb_phy->chg_cur.sdp_min;
  207. *max = usb_phy->chg_cur.sdp_max;
  208. break;
  209. case DCP_TYPE:
  210. *min = usb_phy->chg_cur.dcp_min;
  211. *max = usb_phy->chg_cur.dcp_max;
  212. break;
  213. case CDP_TYPE:
  214. *min = usb_phy->chg_cur.cdp_min;
  215. *max = usb_phy->chg_cur.cdp_max;
  216. break;
  217. case ACA_TYPE:
  218. *min = usb_phy->chg_cur.aca_min;
  219. *max = usb_phy->chg_cur.aca_max;
  220. break;
  221. default:
  222. *min = 0;
  223. *max = 0;
  224. break;
  225. }
  226. }
  227. EXPORT_SYMBOL_GPL(usb_phy_get_charger_current);
  228. /**
  229. * usb_phy_set_charger_state - set the USB charger state
  230. * @usb_phy - the USB phy to be used
  231. * @state - the new state need to be set for charger
  232. *
  233. * The usb phy driver can issue this function when the usb phy driver
  234. * detected the charger state has been changed, in this case the charger
  235. * type should be get from ->charger_detect().
  236. */
  237. void usb_phy_set_charger_state(struct usb_phy *usb_phy,
  238. enum usb_charger_state state)
  239. {
  240. if (usb_phy->chg_state == state || !usb_phy->charger_detect)
  241. return;
  242. usb_phy->chg_state = state;
  243. if (usb_phy->chg_state == USB_CHARGER_PRESENT)
  244. usb_phy->chg_type = usb_phy->charger_detect(usb_phy);
  245. else
  246. usb_phy->chg_type = UNKNOWN_TYPE;
  247. schedule_work(&usb_phy->chg_work);
  248. }
  249. EXPORT_SYMBOL_GPL(usb_phy_set_charger_state);
  250. static void devm_usb_phy_release(struct device *dev, void *res)
  251. {
  252. struct usb_phy *phy = *(struct usb_phy **)res;
  253. usb_put_phy(phy);
  254. }
  255. static void devm_usb_phy_release2(struct device *dev, void *_res)
  256. {
  257. struct phy_devm *res = _res;
  258. if (res->nb)
  259. usb_unregister_notifier(res->phy, res->nb);
  260. usb_put_phy(res->phy);
  261. }
  262. static int devm_usb_phy_match(struct device *dev, void *res, void *match_data)
  263. {
  264. struct usb_phy **phy = res;
  265. return *phy == match_data;
  266. }
  267. static void usb_charger_init(struct usb_phy *usb_phy)
  268. {
  269. usb_phy->chg_type = UNKNOWN_TYPE;
  270. usb_phy->chg_state = USB_CHARGER_DEFAULT;
  271. usb_phy_set_default_current(usb_phy);
  272. INIT_WORK(&usb_phy->chg_work, usb_phy_notify_charger_work);
  273. }
  274. static int usb_add_extcon(struct usb_phy *x)
  275. {
  276. int ret;
  277. if (of_property_read_bool(x->dev->of_node, "extcon")) {
  278. x->edev = extcon_get_edev_by_phandle(x->dev, 0);
  279. if (IS_ERR(x->edev))
  280. return PTR_ERR(x->edev);
  281. x->id_edev = extcon_get_edev_by_phandle(x->dev, 1);
  282. if (IS_ERR(x->id_edev)) {
  283. x->id_edev = NULL;
  284. dev_info(x->dev, "No separate ID extcon device\n");
  285. }
  286. if (x->vbus_nb.notifier_call) {
  287. ret = devm_extcon_register_notifier(x->dev, x->edev,
  288. EXTCON_USB,
  289. &x->vbus_nb);
  290. if (ret < 0) {
  291. dev_err(x->dev,
  292. "register VBUS notifier failed\n");
  293. return ret;
  294. }
  295. } else {
  296. x->type_nb.notifier_call = usb_phy_get_charger_type;
  297. ret = devm_extcon_register_notifier(x->dev, x->edev,
  298. EXTCON_CHG_USB_SDP,
  299. &x->type_nb);
  300. if (ret) {
  301. dev_err(x->dev,
  302. "register extcon USB SDP failed.\n");
  303. return ret;
  304. }
  305. ret = devm_extcon_register_notifier(x->dev, x->edev,
  306. EXTCON_CHG_USB_CDP,
  307. &x->type_nb);
  308. if (ret) {
  309. dev_err(x->dev,
  310. "register extcon USB CDP failed.\n");
  311. return ret;
  312. }
  313. ret = devm_extcon_register_notifier(x->dev, x->edev,
  314. EXTCON_CHG_USB_DCP,
  315. &x->type_nb);
  316. if (ret) {
  317. dev_err(x->dev,
  318. "register extcon USB DCP failed.\n");
  319. return ret;
  320. }
  321. ret = devm_extcon_register_notifier(x->dev, x->edev,
  322. EXTCON_CHG_USB_ACA,
  323. &x->type_nb);
  324. if (ret) {
  325. dev_err(x->dev,
  326. "register extcon USB ACA failed.\n");
  327. return ret;
  328. }
  329. }
  330. if (x->id_nb.notifier_call) {
  331. struct extcon_dev *id_ext;
  332. if (x->id_edev)
  333. id_ext = x->id_edev;
  334. else
  335. id_ext = x->edev;
  336. ret = devm_extcon_register_notifier(x->dev, id_ext,
  337. EXTCON_USB_HOST,
  338. &x->id_nb);
  339. if (ret < 0) {
  340. dev_err(x->dev,
  341. "register ID notifier failed\n");
  342. return ret;
  343. }
  344. }
  345. }
  346. if (x->type_nb.notifier_call)
  347. __usb_phy_get_charger_type(x);
  348. return 0;
  349. }
  350. /**
  351. * devm_usb_get_phy - find the USB PHY
  352. * @dev - device that requests this phy
  353. * @type - the type of the phy the controller requires
  354. *
  355. * Gets the phy using usb_get_phy(), and associates a device with it using
  356. * devres. On driver detach, release function is invoked on the devres data,
  357. * then, devres data is freed.
  358. *
  359. * For use by USB host and peripheral drivers.
  360. */
  361. struct usb_phy *devm_usb_get_phy(struct device *dev, enum usb_phy_type type)
  362. {
  363. struct usb_phy **ptr, *phy;
  364. ptr = devres_alloc(devm_usb_phy_release, sizeof(*ptr), GFP_KERNEL);
  365. if (!ptr)
  366. return ERR_PTR(-ENOMEM);
  367. phy = usb_get_phy(type);
  368. if (!IS_ERR(phy)) {
  369. *ptr = phy;
  370. devres_add(dev, ptr);
  371. } else
  372. devres_free(ptr);
  373. return phy;
  374. }
  375. EXPORT_SYMBOL_GPL(devm_usb_get_phy);
  376. /**
  377. * usb_get_phy - find the USB PHY
  378. * @type - the type of the phy the controller requires
  379. *
  380. * Returns the phy driver, after getting a refcount to it; or
  381. * -ENODEV if there is no such phy. The caller is responsible for
  382. * calling usb_put_phy() to release that count.
  383. *
  384. * For use by USB host and peripheral drivers.
  385. */
  386. struct usb_phy *usb_get_phy(enum usb_phy_type type)
  387. {
  388. struct usb_phy *phy = NULL;
  389. unsigned long flags;
  390. spin_lock_irqsave(&phy_lock, flags);
  391. phy = __usb_find_phy(&phy_list, type);
  392. if (IS_ERR(phy) || !try_module_get(phy->dev->driver->owner)) {
  393. pr_debug("PHY: unable to find transceiver of type %s\n",
  394. usb_phy_type_string(type));
  395. if (!IS_ERR(phy))
  396. phy = ERR_PTR(-ENODEV);
  397. goto err0;
  398. }
  399. get_device(phy->dev);
  400. err0:
  401. spin_unlock_irqrestore(&phy_lock, flags);
  402. return phy;
  403. }
  404. EXPORT_SYMBOL_GPL(usb_get_phy);
  405. /**
  406. * devm_usb_get_phy_by_node - find the USB PHY by device_node
  407. * @dev - device that requests this phy
  408. * @node - the device_node for the phy device.
  409. * @nb - a notifier_block to register with the phy.
  410. *
  411. * Returns the phy driver associated with the given device_node,
  412. * after getting a refcount to it, -ENODEV if there is no such phy or
  413. * -EPROBE_DEFER if the device is not yet loaded. While at that, it
  414. * also associates the device with
  415. * the phy using devres. On driver detach, release function is invoked
  416. * on the devres data, then, devres data is freed.
  417. *
  418. * For use by peripheral drivers for devices related to a phy,
  419. * such as a charger.
  420. */
  421. struct usb_phy *devm_usb_get_phy_by_node(struct device *dev,
  422. struct device_node *node,
  423. struct notifier_block *nb)
  424. {
  425. struct usb_phy *phy = ERR_PTR(-ENOMEM);
  426. struct phy_devm *ptr;
  427. unsigned long flags;
  428. ptr = devres_alloc(devm_usb_phy_release2, sizeof(*ptr), GFP_KERNEL);
  429. if (!ptr) {
  430. dev_dbg(dev, "failed to allocate memory for devres\n");
  431. goto err0;
  432. }
  433. spin_lock_irqsave(&phy_lock, flags);
  434. phy = __of_usb_find_phy(node);
  435. if (IS_ERR(phy)) {
  436. devres_free(ptr);
  437. goto err1;
  438. }
  439. if (!try_module_get(phy->dev->driver->owner)) {
  440. phy = ERR_PTR(-ENODEV);
  441. devres_free(ptr);
  442. goto err1;
  443. }
  444. if (nb)
  445. usb_register_notifier(phy, nb);
  446. ptr->phy = phy;
  447. ptr->nb = nb;
  448. devres_add(dev, ptr);
  449. get_device(phy->dev);
  450. err1:
  451. spin_unlock_irqrestore(&phy_lock, flags);
  452. err0:
  453. return phy;
  454. }
  455. EXPORT_SYMBOL_GPL(devm_usb_get_phy_by_node);
  456. /**
  457. * devm_usb_get_phy_by_phandle - find the USB PHY by phandle
  458. * @dev - device that requests this phy
  459. * @phandle - name of the property holding the phy phandle value
  460. * @index - the index of the phy
  461. *
  462. * Returns the phy driver associated with the given phandle value,
  463. * after getting a refcount to it, -ENODEV if there is no such phy or
  464. * -EPROBE_DEFER if there is a phandle to the phy, but the device is
  465. * not yet loaded. While at that, it also associates the device with
  466. * the phy using devres. On driver detach, release function is invoked
  467. * on the devres data, then, devres data is freed.
  468. *
  469. * For use by USB host and peripheral drivers.
  470. */
  471. struct usb_phy *devm_usb_get_phy_by_phandle(struct device *dev,
  472. const char *phandle, u8 index)
  473. {
  474. struct device_node *node;
  475. struct usb_phy *phy;
  476. if (!dev->of_node) {
  477. dev_dbg(dev, "device does not have a device node entry\n");
  478. return ERR_PTR(-EINVAL);
  479. }
  480. node = of_parse_phandle(dev->of_node, phandle, index);
  481. if (!node) {
  482. dev_dbg(dev, "failed to get %s phandle in %pOF node\n", phandle,
  483. dev->of_node);
  484. return ERR_PTR(-ENODEV);
  485. }
  486. phy = devm_usb_get_phy_by_node(dev, node, NULL);
  487. of_node_put(node);
  488. return phy;
  489. }
  490. EXPORT_SYMBOL_GPL(devm_usb_get_phy_by_phandle);
  491. /**
  492. * devm_usb_put_phy - release the USB PHY
  493. * @dev - device that wants to release this phy
  494. * @phy - the phy returned by devm_usb_get_phy()
  495. *
  496. * destroys the devres associated with this phy and invokes usb_put_phy
  497. * to release the phy.
  498. *
  499. * For use by USB host and peripheral drivers.
  500. */
  501. void devm_usb_put_phy(struct device *dev, struct usb_phy *phy)
  502. {
  503. int r;
  504. r = devres_destroy(dev, devm_usb_phy_release, devm_usb_phy_match, phy);
  505. dev_WARN_ONCE(dev, r, "couldn't find PHY resource\n");
  506. }
  507. EXPORT_SYMBOL_GPL(devm_usb_put_phy);
  508. /**
  509. * usb_put_phy - release the USB PHY
  510. * @x: the phy returned by usb_get_phy()
  511. *
  512. * Releases a refcount the caller received from usb_get_phy().
  513. *
  514. * For use by USB host and peripheral drivers.
  515. */
  516. void usb_put_phy(struct usb_phy *x)
  517. {
  518. if (x) {
  519. struct module *owner = x->dev->driver->owner;
  520. put_device(x->dev);
  521. module_put(owner);
  522. }
  523. }
  524. EXPORT_SYMBOL_GPL(usb_put_phy);
  525. /**
  526. * usb_add_phy - declare the USB PHY
  527. * @x: the USB phy to be used; or NULL
  528. * @type - the type of this PHY
  529. *
  530. * This call is exclusively for use by phy drivers, which
  531. * coordinate the activities of drivers for host and peripheral
  532. * controllers, and in some cases for VBUS current regulation.
  533. */
  534. int usb_add_phy(struct usb_phy *x, enum usb_phy_type type)
  535. {
  536. int ret = 0;
  537. unsigned long flags;
  538. struct usb_phy *phy;
  539. if (x->type != USB_PHY_TYPE_UNDEFINED) {
  540. dev_err(x->dev, "not accepting initialized PHY %s\n", x->label);
  541. return -EINVAL;
  542. }
  543. usb_charger_init(x);
  544. ret = usb_add_extcon(x);
  545. if (ret)
  546. return ret;
  547. ATOMIC_INIT_NOTIFIER_HEAD(&x->notifier);
  548. spin_lock_irqsave(&phy_lock, flags);
  549. list_for_each_entry(phy, &phy_list, head) {
  550. if (phy->type == type) {
  551. ret = -EBUSY;
  552. dev_err(x->dev, "transceiver type %s already exists\n",
  553. usb_phy_type_string(type));
  554. goto out;
  555. }
  556. }
  557. x->type = type;
  558. list_add_tail(&x->head, &phy_list);
  559. out:
  560. spin_unlock_irqrestore(&phy_lock, flags);
  561. return ret;
  562. }
  563. EXPORT_SYMBOL_GPL(usb_add_phy);
  564. /**
  565. * usb_add_phy_dev - declare the USB PHY
  566. * @x: the USB phy to be used; or NULL
  567. *
  568. * This call is exclusively for use by phy drivers, which
  569. * coordinate the activities of drivers for host and peripheral
  570. * controllers, and in some cases for VBUS current regulation.
  571. */
  572. int usb_add_phy_dev(struct usb_phy *x)
  573. {
  574. unsigned long flags;
  575. int ret;
  576. if (!x->dev) {
  577. dev_err(x->dev, "no device provided for PHY\n");
  578. return -EINVAL;
  579. }
  580. usb_charger_init(x);
  581. ret = usb_add_extcon(x);
  582. if (ret)
  583. return ret;
  584. ATOMIC_INIT_NOTIFIER_HEAD(&x->notifier);
  585. spin_lock_irqsave(&phy_lock, flags);
  586. list_add_tail(&x->head, &phy_list);
  587. spin_unlock_irqrestore(&phy_lock, flags);
  588. return 0;
  589. }
  590. EXPORT_SYMBOL_GPL(usb_add_phy_dev);
  591. /**
  592. * usb_remove_phy - remove the OTG PHY
  593. * @x: the USB OTG PHY to be removed;
  594. *
  595. * This reverts the effects of usb_add_phy
  596. */
  597. void usb_remove_phy(struct usb_phy *x)
  598. {
  599. unsigned long flags;
  600. spin_lock_irqsave(&phy_lock, flags);
  601. if (x)
  602. list_del(&x->head);
  603. spin_unlock_irqrestore(&phy_lock, flags);
  604. }
  605. EXPORT_SYMBOL_GPL(usb_remove_phy);
  606. /**
  607. * usb_phy_set_event - set event to phy event
  608. * @x: the phy returned by usb_get_phy();
  609. *
  610. * This sets event to phy event
  611. */
  612. void usb_phy_set_event(struct usb_phy *x, unsigned long event)
  613. {
  614. x->last_event = event;
  615. }
  616. EXPORT_SYMBOL_GPL(usb_phy_set_event);