phy.c 21 KB

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