phy.c 21 KB

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