phy.c 19 KB

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