phy-core.c 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * phy-core.c -- Generic Phy framework.
  4. *
  5. * Copyright (C) 2013 Texas Instruments Incorporated - http://www.ti.com
  6. *
  7. * Author: Kishon Vijay Abraham I <kishon@ti.com>
  8. */
  9. #include <linux/kernel.h>
  10. #include <linux/export.h>
  11. #include <linux/module.h>
  12. #include <linux/err.h>
  13. #include <linux/device.h>
  14. #include <linux/slab.h>
  15. #include <linux/of.h>
  16. #include <linux/phy/phy.h>
  17. #include <linux/idr.h>
  18. #include <linux/pm_runtime.h>
  19. #include <linux/regulator/consumer.h>
  20. static struct class *phy_class;
  21. static DEFINE_MUTEX(phy_provider_mutex);
  22. static LIST_HEAD(phy_provider_list);
  23. static LIST_HEAD(phys);
  24. static DEFINE_IDA(phy_ida);
  25. static void devm_phy_release(struct device *dev, void *res)
  26. {
  27. struct phy *phy = *(struct phy **)res;
  28. phy_put(dev, phy);
  29. }
  30. static void devm_phy_provider_release(struct device *dev, void *res)
  31. {
  32. struct phy_provider *phy_provider = *(struct phy_provider **)res;
  33. of_phy_provider_unregister(phy_provider);
  34. }
  35. static void devm_phy_consume(struct device *dev, void *res)
  36. {
  37. struct phy *phy = *(struct phy **)res;
  38. phy_destroy(phy);
  39. }
  40. static int devm_phy_match(struct device *dev, void *res, void *match_data)
  41. {
  42. struct phy **phy = res;
  43. return *phy == match_data;
  44. }
  45. /**
  46. * phy_create_lookup() - allocate and register PHY/device association
  47. * @phy: the phy of the association
  48. * @con_id: connection ID string on device
  49. * @dev_id: the device of the association
  50. *
  51. * Creates and registers phy_lookup entry.
  52. */
  53. int phy_create_lookup(struct phy *phy, const char *con_id, const char *dev_id)
  54. {
  55. struct phy_lookup *pl;
  56. if (!phy || !dev_id || !con_id)
  57. return -EINVAL;
  58. pl = kzalloc(sizeof(*pl), GFP_KERNEL);
  59. if (!pl)
  60. return -ENOMEM;
  61. pl->dev_id = dev_id;
  62. pl->con_id = con_id;
  63. pl->phy = phy;
  64. mutex_lock(&phy_provider_mutex);
  65. list_add_tail(&pl->node, &phys);
  66. mutex_unlock(&phy_provider_mutex);
  67. return 0;
  68. }
  69. EXPORT_SYMBOL_GPL(phy_create_lookup);
  70. /**
  71. * phy_remove_lookup() - find and remove PHY/device association
  72. * @phy: the phy of the association
  73. * @con_id: connection ID string on device
  74. * @dev_id: the device of the association
  75. *
  76. * Finds and unregisters phy_lookup entry that was created with
  77. * phy_create_lookup().
  78. */
  79. void phy_remove_lookup(struct phy *phy, const char *con_id, const char *dev_id)
  80. {
  81. struct phy_lookup *pl;
  82. if (!phy || !dev_id || !con_id)
  83. return;
  84. mutex_lock(&phy_provider_mutex);
  85. list_for_each_entry(pl, &phys, node)
  86. if (pl->phy == phy && !strcmp(pl->dev_id, dev_id) &&
  87. !strcmp(pl->con_id, con_id)) {
  88. list_del(&pl->node);
  89. kfree(pl);
  90. break;
  91. }
  92. mutex_unlock(&phy_provider_mutex);
  93. }
  94. EXPORT_SYMBOL_GPL(phy_remove_lookup);
  95. static struct phy *phy_find(struct device *dev, const char *con_id)
  96. {
  97. const char *dev_id = dev_name(dev);
  98. struct phy_lookup *p, *pl = NULL;
  99. mutex_lock(&phy_provider_mutex);
  100. list_for_each_entry(p, &phys, node)
  101. if (!strcmp(p->dev_id, dev_id) && !strcmp(p->con_id, con_id)) {
  102. pl = p;
  103. break;
  104. }
  105. mutex_unlock(&phy_provider_mutex);
  106. return pl ? pl->phy : ERR_PTR(-ENODEV);
  107. }
  108. static struct phy_provider *of_phy_provider_lookup(struct device_node *node)
  109. {
  110. struct phy_provider *phy_provider;
  111. struct device_node *child;
  112. list_for_each_entry(phy_provider, &phy_provider_list, list) {
  113. if (phy_provider->dev->of_node == node)
  114. return phy_provider;
  115. for_each_child_of_node(phy_provider->children, child)
  116. if (child == node)
  117. return phy_provider;
  118. }
  119. return ERR_PTR(-EPROBE_DEFER);
  120. }
  121. int phy_pm_runtime_get(struct phy *phy)
  122. {
  123. int ret;
  124. if (!phy)
  125. return 0;
  126. if (!pm_runtime_enabled(&phy->dev))
  127. return -ENOTSUPP;
  128. ret = pm_runtime_get(&phy->dev);
  129. if (ret < 0 && ret != -EINPROGRESS)
  130. pm_runtime_put_noidle(&phy->dev);
  131. return ret;
  132. }
  133. EXPORT_SYMBOL_GPL(phy_pm_runtime_get);
  134. int phy_pm_runtime_get_sync(struct phy *phy)
  135. {
  136. int ret;
  137. if (!phy)
  138. return 0;
  139. if (!pm_runtime_enabled(&phy->dev))
  140. return -ENOTSUPP;
  141. ret = pm_runtime_get_sync(&phy->dev);
  142. if (ret < 0)
  143. pm_runtime_put_sync(&phy->dev);
  144. return ret;
  145. }
  146. EXPORT_SYMBOL_GPL(phy_pm_runtime_get_sync);
  147. int phy_pm_runtime_put(struct phy *phy)
  148. {
  149. if (!phy)
  150. return 0;
  151. if (!pm_runtime_enabled(&phy->dev))
  152. return -ENOTSUPP;
  153. return pm_runtime_put(&phy->dev);
  154. }
  155. EXPORT_SYMBOL_GPL(phy_pm_runtime_put);
  156. int phy_pm_runtime_put_sync(struct phy *phy)
  157. {
  158. if (!phy)
  159. return 0;
  160. if (!pm_runtime_enabled(&phy->dev))
  161. return -ENOTSUPP;
  162. return pm_runtime_put_sync(&phy->dev);
  163. }
  164. EXPORT_SYMBOL_GPL(phy_pm_runtime_put_sync);
  165. void phy_pm_runtime_allow(struct phy *phy)
  166. {
  167. if (!phy)
  168. return;
  169. if (!pm_runtime_enabled(&phy->dev))
  170. return;
  171. pm_runtime_allow(&phy->dev);
  172. }
  173. EXPORT_SYMBOL_GPL(phy_pm_runtime_allow);
  174. void phy_pm_runtime_forbid(struct phy *phy)
  175. {
  176. if (!phy)
  177. return;
  178. if (!pm_runtime_enabled(&phy->dev))
  179. return;
  180. pm_runtime_forbid(&phy->dev);
  181. }
  182. EXPORT_SYMBOL_GPL(phy_pm_runtime_forbid);
  183. int phy_init(struct phy *phy)
  184. {
  185. int ret;
  186. if (!phy)
  187. return 0;
  188. ret = phy_pm_runtime_get_sync(phy);
  189. if (ret < 0 && ret != -ENOTSUPP)
  190. return ret;
  191. ret = 0; /* Override possible ret == -ENOTSUPP */
  192. mutex_lock(&phy->mutex);
  193. if (phy->init_count == 0 && phy->ops->init) {
  194. ret = phy->ops->init(phy);
  195. if (ret < 0) {
  196. dev_err(&phy->dev, "phy init failed --> %d\n", ret);
  197. goto out;
  198. }
  199. }
  200. ++phy->init_count;
  201. out:
  202. mutex_unlock(&phy->mutex);
  203. phy_pm_runtime_put(phy);
  204. return ret;
  205. }
  206. EXPORT_SYMBOL_GPL(phy_init);
  207. int phy_exit(struct phy *phy)
  208. {
  209. int ret;
  210. if (!phy)
  211. return 0;
  212. ret = phy_pm_runtime_get_sync(phy);
  213. if (ret < 0 && ret != -ENOTSUPP)
  214. return ret;
  215. ret = 0; /* Override possible ret == -ENOTSUPP */
  216. mutex_lock(&phy->mutex);
  217. if (phy->init_count == 1 && phy->ops->exit) {
  218. ret = phy->ops->exit(phy);
  219. if (ret < 0) {
  220. dev_err(&phy->dev, "phy exit failed --> %d\n", ret);
  221. goto out;
  222. }
  223. }
  224. --phy->init_count;
  225. out:
  226. mutex_unlock(&phy->mutex);
  227. phy_pm_runtime_put(phy);
  228. return ret;
  229. }
  230. EXPORT_SYMBOL_GPL(phy_exit);
  231. int phy_power_on(struct phy *phy)
  232. {
  233. int ret = 0;
  234. if (!phy)
  235. goto out;
  236. if (phy->pwr) {
  237. ret = regulator_enable(phy->pwr);
  238. if (ret)
  239. goto out;
  240. }
  241. ret = phy_pm_runtime_get_sync(phy);
  242. if (ret < 0 && ret != -ENOTSUPP)
  243. goto err_pm_sync;
  244. ret = 0; /* Override possible ret == -ENOTSUPP */
  245. mutex_lock(&phy->mutex);
  246. if (phy->power_count == 0 && phy->ops->power_on) {
  247. ret = phy->ops->power_on(phy);
  248. if (ret < 0) {
  249. dev_err(&phy->dev, "phy poweron failed --> %d\n", ret);
  250. goto err_pwr_on;
  251. }
  252. }
  253. ++phy->power_count;
  254. mutex_unlock(&phy->mutex);
  255. return 0;
  256. err_pwr_on:
  257. mutex_unlock(&phy->mutex);
  258. phy_pm_runtime_put_sync(phy);
  259. err_pm_sync:
  260. if (phy->pwr)
  261. regulator_disable(phy->pwr);
  262. out:
  263. return ret;
  264. }
  265. EXPORT_SYMBOL_GPL(phy_power_on);
  266. int phy_power_off(struct phy *phy)
  267. {
  268. int ret;
  269. if (!phy)
  270. return 0;
  271. mutex_lock(&phy->mutex);
  272. if (phy->power_count == 1 && phy->ops->power_off) {
  273. ret = phy->ops->power_off(phy);
  274. if (ret < 0) {
  275. dev_err(&phy->dev, "phy poweroff failed --> %d\n", ret);
  276. mutex_unlock(&phy->mutex);
  277. return ret;
  278. }
  279. }
  280. --phy->power_count;
  281. mutex_unlock(&phy->mutex);
  282. phy_pm_runtime_put(phy);
  283. if (phy->pwr)
  284. regulator_disable(phy->pwr);
  285. return 0;
  286. }
  287. EXPORT_SYMBOL_GPL(phy_power_off);
  288. int phy_set_mode_ext(struct phy *phy, enum phy_mode mode, int submode)
  289. {
  290. int ret;
  291. if (!phy || !phy->ops->set_mode)
  292. return 0;
  293. mutex_lock(&phy->mutex);
  294. ret = phy->ops->set_mode(phy, mode, submode);
  295. if (!ret)
  296. phy->attrs.mode = mode;
  297. mutex_unlock(&phy->mutex);
  298. return ret;
  299. }
  300. EXPORT_SYMBOL_GPL(phy_set_mode_ext);
  301. int phy_reset(struct phy *phy)
  302. {
  303. int ret;
  304. if (!phy || !phy->ops->reset)
  305. return 0;
  306. ret = phy_pm_runtime_get_sync(phy);
  307. if (ret < 0 && ret != -ENOTSUPP)
  308. return ret;
  309. mutex_lock(&phy->mutex);
  310. ret = phy->ops->reset(phy);
  311. mutex_unlock(&phy->mutex);
  312. phy_pm_runtime_put(phy);
  313. return ret;
  314. }
  315. EXPORT_SYMBOL_GPL(phy_reset);
  316. /**
  317. * phy_calibrate() - Tunes the phy hw parameters for current configuration
  318. * @phy: the phy returned by phy_get()
  319. *
  320. * Used to calibrate phy hardware, typically by adjusting some parameters in
  321. * runtime, which are otherwise lost after host controller reset and cannot
  322. * be applied in phy_init() or phy_power_on().
  323. *
  324. * Returns: 0 if successful, an negative error code otherwise
  325. */
  326. int phy_calibrate(struct phy *phy)
  327. {
  328. int ret;
  329. if (!phy || !phy->ops->calibrate)
  330. return 0;
  331. mutex_lock(&phy->mutex);
  332. ret = phy->ops->calibrate(phy);
  333. mutex_unlock(&phy->mutex);
  334. return ret;
  335. }
  336. EXPORT_SYMBOL_GPL(phy_calibrate);
  337. /**
  338. * phy_configure() - Changes the phy parameters
  339. * @phy: the phy returned by phy_get()
  340. * @opts: New configuration to apply
  341. *
  342. * Used to change the PHY parameters. phy_init() must have been called
  343. * on the phy. The configuration will be applied on the current phy
  344. * mode, that can be changed using phy_set_mode().
  345. *
  346. * Returns: 0 if successful, an negative error code otherwise
  347. */
  348. int phy_configure(struct phy *phy, union phy_configure_opts *opts)
  349. {
  350. int ret;
  351. if (!phy)
  352. return -EINVAL;
  353. if (!phy->ops->configure)
  354. return -EOPNOTSUPP;
  355. mutex_lock(&phy->mutex);
  356. ret = phy->ops->configure(phy, opts);
  357. mutex_unlock(&phy->mutex);
  358. return ret;
  359. }
  360. EXPORT_SYMBOL_GPL(phy_configure);
  361. /**
  362. * phy_validate() - Checks the phy parameters
  363. * @phy: the phy returned by phy_get()
  364. * @mode: phy_mode the configuration is applicable to.
  365. * @submode: PHY submode the configuration is applicable to.
  366. * @opts: Configuration to check
  367. *
  368. * Used to check that the current set of parameters can be handled by
  369. * the phy. Implementations are free to tune the parameters passed as
  370. * arguments if needed by some implementation detail or
  371. * constraints. It will not change any actual configuration of the
  372. * PHY, so calling it as many times as deemed fit will have no side
  373. * effect.
  374. *
  375. * Returns: 0 if successful, an negative error code otherwise
  376. */
  377. int phy_validate(struct phy *phy, enum phy_mode mode, int submode,
  378. union phy_configure_opts *opts)
  379. {
  380. int ret;
  381. if (!phy)
  382. return -EINVAL;
  383. if (!phy->ops->validate)
  384. return -EOPNOTSUPP;
  385. mutex_lock(&phy->mutex);
  386. ret = phy->ops->validate(phy, mode, submode, opts);
  387. mutex_unlock(&phy->mutex);
  388. return ret;
  389. }
  390. EXPORT_SYMBOL_GPL(phy_validate);
  391. /**
  392. * _of_phy_get() - lookup and obtain a reference to a phy by phandle
  393. * @np: device_node for which to get the phy
  394. * @index: the index of the phy
  395. *
  396. * Returns the phy associated with the given phandle value,
  397. * after getting a refcount to it or -ENODEV if there is no such phy or
  398. * -EPROBE_DEFER if there is a phandle to the phy, but the device is
  399. * not yet loaded. This function uses of_xlate call back function provided
  400. * while registering the phy_provider to find the phy instance.
  401. */
  402. static struct phy *_of_phy_get(struct device_node *np, int index)
  403. {
  404. int ret;
  405. struct phy_provider *phy_provider;
  406. struct phy *phy = NULL;
  407. struct of_phandle_args args;
  408. ret = of_parse_phandle_with_args(np, "phys", "#phy-cells",
  409. index, &args);
  410. if (ret)
  411. return ERR_PTR(-ENODEV);
  412. /* This phy type handled by the usb-phy subsystem for now */
  413. if (of_device_is_compatible(args.np, "usb-nop-xceiv"))
  414. return ERR_PTR(-ENODEV);
  415. mutex_lock(&phy_provider_mutex);
  416. phy_provider = of_phy_provider_lookup(args.np);
  417. if (IS_ERR(phy_provider) || !try_module_get(phy_provider->owner)) {
  418. phy = ERR_PTR(-EPROBE_DEFER);
  419. goto out_unlock;
  420. }
  421. if (!of_device_is_available(args.np)) {
  422. dev_warn(phy_provider->dev, "Requested PHY is disabled\n");
  423. phy = ERR_PTR(-ENODEV);
  424. goto out_put_module;
  425. }
  426. phy = phy_provider->of_xlate(phy_provider->dev, &args);
  427. out_put_module:
  428. module_put(phy_provider->owner);
  429. out_unlock:
  430. mutex_unlock(&phy_provider_mutex);
  431. of_node_put(args.np);
  432. return phy;
  433. }
  434. /**
  435. * of_phy_get() - lookup and obtain a reference to a phy using a device_node.
  436. * @np: device_node for which to get the phy
  437. * @con_id: name of the phy from device's point of view
  438. *
  439. * Returns the phy driver, after getting a refcount to it; or
  440. * -ENODEV if there is no such phy. The caller is responsible for
  441. * calling phy_put() to release that count.
  442. */
  443. struct phy *of_phy_get(struct device_node *np, const char *con_id)
  444. {
  445. struct phy *phy = NULL;
  446. int index = 0;
  447. if (con_id)
  448. index = of_property_match_string(np, "phy-names", con_id);
  449. phy = _of_phy_get(np, index);
  450. if (IS_ERR(phy))
  451. return phy;
  452. if (!try_module_get(phy->ops->owner))
  453. return ERR_PTR(-EPROBE_DEFER);
  454. get_device(&phy->dev);
  455. return phy;
  456. }
  457. EXPORT_SYMBOL_GPL(of_phy_get);
  458. /**
  459. * of_phy_put() - release the PHY
  460. * @phy: the phy returned by of_phy_get()
  461. *
  462. * Releases a refcount the caller received from of_phy_get().
  463. */
  464. void of_phy_put(struct phy *phy)
  465. {
  466. if (!phy || IS_ERR(phy))
  467. return;
  468. mutex_lock(&phy->mutex);
  469. if (phy->ops->release)
  470. phy->ops->release(phy);
  471. mutex_unlock(&phy->mutex);
  472. module_put(phy->ops->owner);
  473. put_device(&phy->dev);
  474. }
  475. EXPORT_SYMBOL_GPL(of_phy_put);
  476. /**
  477. * phy_put() - release the PHY
  478. * @dev: device that wants to release this phy
  479. * @phy: the phy returned by phy_get()
  480. *
  481. * Releases a refcount the caller received from phy_get().
  482. */
  483. void phy_put(struct device *dev, struct phy *phy)
  484. {
  485. device_link_remove(dev, &phy->dev);
  486. of_phy_put(phy);
  487. }
  488. EXPORT_SYMBOL_GPL(phy_put);
  489. /**
  490. * devm_phy_put() - release the PHY
  491. * @dev: device that wants to release this phy
  492. * @phy: the phy returned by devm_phy_get()
  493. *
  494. * destroys the devres associated with this phy and invokes phy_put
  495. * to release the phy.
  496. */
  497. void devm_phy_put(struct device *dev, struct phy *phy)
  498. {
  499. int r;
  500. if (!phy)
  501. return;
  502. r = devres_destroy(dev, devm_phy_release, devm_phy_match, phy);
  503. dev_WARN_ONCE(dev, r, "couldn't find PHY resource\n");
  504. }
  505. EXPORT_SYMBOL_GPL(devm_phy_put);
  506. /**
  507. * of_phy_simple_xlate() - returns the phy instance from phy provider
  508. * @dev: the PHY provider device
  509. * @args: of_phandle_args (not used here)
  510. *
  511. * Intended to be used by phy provider for the common case where #phy-cells is
  512. * 0. For other cases where #phy-cells is greater than '0', the phy provider
  513. * should provide a custom of_xlate function that reads the *args* and returns
  514. * the appropriate phy.
  515. */
  516. struct phy *of_phy_simple_xlate(struct device *dev, struct of_phandle_args
  517. *args)
  518. {
  519. struct phy *phy;
  520. struct class_dev_iter iter;
  521. class_dev_iter_init(&iter, phy_class, NULL, NULL);
  522. while ((dev = class_dev_iter_next(&iter))) {
  523. phy = to_phy(dev);
  524. if (args->np != phy->dev.of_node)
  525. continue;
  526. class_dev_iter_exit(&iter);
  527. return phy;
  528. }
  529. class_dev_iter_exit(&iter);
  530. return ERR_PTR(-ENODEV);
  531. }
  532. EXPORT_SYMBOL_GPL(of_phy_simple_xlate);
  533. /**
  534. * phy_get() - lookup and obtain a reference to a phy.
  535. * @dev: device that requests this phy
  536. * @string: the phy name as given in the dt data or the name of the controller
  537. * port for non-dt case
  538. *
  539. * Returns the phy driver, after getting a refcount to it; or
  540. * -ENODEV if there is no such phy. The caller is responsible for
  541. * calling phy_put() to release that count.
  542. */
  543. struct phy *phy_get(struct device *dev, const char *string)
  544. {
  545. int index = 0;
  546. struct phy *phy;
  547. struct device_link *link;
  548. if (string == NULL) {
  549. dev_WARN(dev, "missing string\n");
  550. return ERR_PTR(-EINVAL);
  551. }
  552. if (dev->of_node) {
  553. index = of_property_match_string(dev->of_node, "phy-names",
  554. string);
  555. phy = _of_phy_get(dev->of_node, index);
  556. } else {
  557. phy = phy_find(dev, string);
  558. }
  559. if (IS_ERR(phy))
  560. return phy;
  561. if (!try_module_get(phy->ops->owner))
  562. return ERR_PTR(-EPROBE_DEFER);
  563. get_device(&phy->dev);
  564. link = device_link_add(dev, &phy->dev, DL_FLAG_STATELESS);
  565. if (!link)
  566. dev_dbg(dev, "failed to create device link to %s\n",
  567. dev_name(phy->dev.parent));
  568. return phy;
  569. }
  570. EXPORT_SYMBOL_GPL(phy_get);
  571. /**
  572. * phy_optional_get() - lookup and obtain a reference to an optional phy.
  573. * @dev: device that requests this phy
  574. * @string: the phy name as given in the dt data or the name of the controller
  575. * port for non-dt case
  576. *
  577. * Returns the phy driver, after getting a refcount to it; or
  578. * NULL if there is no such phy. The caller is responsible for
  579. * calling phy_put() to release that count.
  580. */
  581. struct phy *phy_optional_get(struct device *dev, const char *string)
  582. {
  583. struct phy *phy = phy_get(dev, string);
  584. if (PTR_ERR(phy) == -ENODEV)
  585. phy = NULL;
  586. return phy;
  587. }
  588. EXPORT_SYMBOL_GPL(phy_optional_get);
  589. /**
  590. * devm_phy_get() - lookup and obtain a reference to a phy.
  591. * @dev: device that requests this phy
  592. * @string: the phy name as given in the dt data or phy device name
  593. * for non-dt case
  594. *
  595. * Gets the phy using phy_get(), and associates a device with it using
  596. * devres. On driver detach, release function is invoked on the devres data,
  597. * then, devres data is freed.
  598. */
  599. struct phy *devm_phy_get(struct device *dev, const char *string)
  600. {
  601. struct phy **ptr, *phy;
  602. ptr = devres_alloc(devm_phy_release, sizeof(*ptr), GFP_KERNEL);
  603. if (!ptr)
  604. return ERR_PTR(-ENOMEM);
  605. phy = phy_get(dev, string);
  606. if (!IS_ERR(phy)) {
  607. *ptr = phy;
  608. devres_add(dev, ptr);
  609. } else {
  610. devres_free(ptr);
  611. }
  612. return phy;
  613. }
  614. EXPORT_SYMBOL_GPL(devm_phy_get);
  615. /**
  616. * devm_phy_optional_get() - lookup and obtain a reference to an optional phy.
  617. * @dev: device that requests this phy
  618. * @string: the phy name as given in the dt data or phy device name
  619. * for non-dt case
  620. *
  621. * Gets the phy using phy_get(), and associates a device with it using
  622. * devres. On driver detach, release function is invoked on the devres
  623. * data, then, devres data is freed. This differs to devm_phy_get() in
  624. * that if the phy does not exist, it is not considered an error and
  625. * -ENODEV will not be returned. Instead the NULL phy is returned,
  626. * which can be passed to all other phy consumer calls.
  627. */
  628. struct phy *devm_phy_optional_get(struct device *dev, const char *string)
  629. {
  630. struct phy *phy = devm_phy_get(dev, string);
  631. if (PTR_ERR(phy) == -ENODEV)
  632. phy = NULL;
  633. return phy;
  634. }
  635. EXPORT_SYMBOL_GPL(devm_phy_optional_get);
  636. /**
  637. * devm_of_phy_get() - lookup and obtain a reference to a phy.
  638. * @dev: device that requests this phy
  639. * @np: node containing the phy
  640. * @con_id: name of the phy from device's point of view
  641. *
  642. * Gets the phy using of_phy_get(), and associates a device with it using
  643. * devres. On driver detach, release function is invoked on the devres data,
  644. * then, devres data is freed.
  645. */
  646. struct phy *devm_of_phy_get(struct device *dev, struct device_node *np,
  647. const char *con_id)
  648. {
  649. struct phy **ptr, *phy;
  650. struct device_link *link;
  651. ptr = devres_alloc(devm_phy_release, sizeof(*ptr), GFP_KERNEL);
  652. if (!ptr)
  653. return ERR_PTR(-ENOMEM);
  654. phy = of_phy_get(np, con_id);
  655. if (!IS_ERR(phy)) {
  656. *ptr = phy;
  657. devres_add(dev, ptr);
  658. } else {
  659. devres_free(ptr);
  660. return phy;
  661. }
  662. link = device_link_add(dev, &phy->dev, DL_FLAG_STATELESS);
  663. if (!link)
  664. dev_dbg(dev, "failed to create device link to %s\n",
  665. dev_name(phy->dev.parent));
  666. return phy;
  667. }
  668. EXPORT_SYMBOL_GPL(devm_of_phy_get);
  669. /**
  670. * devm_of_phy_get_by_index() - lookup and obtain a reference to a phy by index.
  671. * @dev: device that requests this phy
  672. * @np: node containing the phy
  673. * @index: index of the phy
  674. *
  675. * Gets the phy using _of_phy_get(), then gets a refcount to it,
  676. * and associates a device with it using devres. On driver detach,
  677. * release function is invoked on the devres data,
  678. * then, devres data is freed.
  679. *
  680. */
  681. struct phy *devm_of_phy_get_by_index(struct device *dev, struct device_node *np,
  682. int index)
  683. {
  684. struct phy **ptr, *phy;
  685. struct device_link *link;
  686. ptr = devres_alloc(devm_phy_release, sizeof(*ptr), GFP_KERNEL);
  687. if (!ptr)
  688. return ERR_PTR(-ENOMEM);
  689. phy = _of_phy_get(np, index);
  690. if (IS_ERR(phy)) {
  691. devres_free(ptr);
  692. return phy;
  693. }
  694. if (!try_module_get(phy->ops->owner)) {
  695. devres_free(ptr);
  696. return ERR_PTR(-EPROBE_DEFER);
  697. }
  698. get_device(&phy->dev);
  699. *ptr = phy;
  700. devres_add(dev, ptr);
  701. link = device_link_add(dev, &phy->dev, DL_FLAG_STATELESS);
  702. if (!link)
  703. dev_dbg(dev, "failed to create device link to %s\n",
  704. dev_name(phy->dev.parent));
  705. return phy;
  706. }
  707. EXPORT_SYMBOL_GPL(devm_of_phy_get_by_index);
  708. /**
  709. * phy_create() - create a new phy
  710. * @dev: device that is creating the new phy
  711. * @node: device node of the phy
  712. * @ops: function pointers for performing phy operations
  713. *
  714. * Called to create a phy using phy framework.
  715. */
  716. struct phy *phy_create(struct device *dev, struct device_node *node,
  717. const struct phy_ops *ops)
  718. {
  719. int ret;
  720. int id;
  721. struct phy *phy;
  722. if (WARN_ON(!dev))
  723. return ERR_PTR(-EINVAL);
  724. phy = kzalloc(sizeof(*phy), GFP_KERNEL);
  725. if (!phy)
  726. return ERR_PTR(-ENOMEM);
  727. id = ida_simple_get(&phy_ida, 0, 0, GFP_KERNEL);
  728. if (id < 0) {
  729. dev_err(dev, "unable to get id\n");
  730. ret = id;
  731. goto free_phy;
  732. }
  733. device_initialize(&phy->dev);
  734. mutex_init(&phy->mutex);
  735. phy->dev.class = phy_class;
  736. phy->dev.parent = dev;
  737. phy->dev.of_node = node ?: dev->of_node;
  738. phy->id = id;
  739. phy->ops = ops;
  740. ret = dev_set_name(&phy->dev, "phy-%s.%d", dev_name(dev), id);
  741. if (ret)
  742. goto put_dev;
  743. /* phy-supply */
  744. phy->pwr = regulator_get_optional(&phy->dev, "phy");
  745. if (IS_ERR(phy->pwr)) {
  746. ret = PTR_ERR(phy->pwr);
  747. if (ret == -EPROBE_DEFER)
  748. goto put_dev;
  749. phy->pwr = NULL;
  750. }
  751. ret = device_add(&phy->dev);
  752. if (ret)
  753. goto put_dev;
  754. if (pm_runtime_enabled(dev)) {
  755. pm_runtime_enable(&phy->dev);
  756. pm_runtime_no_callbacks(&phy->dev);
  757. }
  758. return phy;
  759. put_dev:
  760. put_device(&phy->dev); /* calls phy_release() which frees resources */
  761. return ERR_PTR(ret);
  762. free_phy:
  763. kfree(phy);
  764. return ERR_PTR(ret);
  765. }
  766. EXPORT_SYMBOL_GPL(phy_create);
  767. /**
  768. * devm_phy_create() - create a new phy
  769. * @dev: device that is creating the new phy
  770. * @node: device node of the phy
  771. * @ops: function pointers for performing phy operations
  772. *
  773. * Creates a new PHY device adding it to the PHY class.
  774. * While at that, it also associates the device with the phy using devres.
  775. * On driver detach, release function is invoked on the devres data,
  776. * then, devres data is freed.
  777. */
  778. struct phy *devm_phy_create(struct device *dev, struct device_node *node,
  779. const struct phy_ops *ops)
  780. {
  781. struct phy **ptr, *phy;
  782. ptr = devres_alloc(devm_phy_consume, sizeof(*ptr), GFP_KERNEL);
  783. if (!ptr)
  784. return ERR_PTR(-ENOMEM);
  785. phy = phy_create(dev, node, ops);
  786. if (!IS_ERR(phy)) {
  787. *ptr = phy;
  788. devres_add(dev, ptr);
  789. } else {
  790. devres_free(ptr);
  791. }
  792. return phy;
  793. }
  794. EXPORT_SYMBOL_GPL(devm_phy_create);
  795. /**
  796. * phy_destroy() - destroy the phy
  797. * @phy: the phy to be destroyed
  798. *
  799. * Called to destroy the phy.
  800. */
  801. void phy_destroy(struct phy *phy)
  802. {
  803. pm_runtime_disable(&phy->dev);
  804. device_unregister(&phy->dev);
  805. }
  806. EXPORT_SYMBOL_GPL(phy_destroy);
  807. /**
  808. * devm_phy_destroy() - destroy the PHY
  809. * @dev: device that wants to release this phy
  810. * @phy: the phy returned by devm_phy_get()
  811. *
  812. * destroys the devres associated with this phy and invokes phy_destroy
  813. * to destroy the phy.
  814. */
  815. void devm_phy_destroy(struct device *dev, struct phy *phy)
  816. {
  817. int r;
  818. r = devres_destroy(dev, devm_phy_consume, devm_phy_match, phy);
  819. dev_WARN_ONCE(dev, r, "couldn't find PHY resource\n");
  820. }
  821. EXPORT_SYMBOL_GPL(devm_phy_destroy);
  822. /**
  823. * __of_phy_provider_register() - create/register phy provider with the framework
  824. * @dev: struct device of the phy provider
  825. * @children: device node containing children (if different from dev->of_node)
  826. * @owner: the module owner containing of_xlate
  827. * @of_xlate: function pointer to obtain phy instance from phy provider
  828. *
  829. * Creates struct phy_provider from dev and of_xlate function pointer.
  830. * This is used in the case of dt boot for finding the phy instance from
  831. * phy provider.
  832. *
  833. * If the PHY provider doesn't nest children directly but uses a separate
  834. * child node to contain the individual children, the @children parameter
  835. * can be used to override the default. If NULL, the default (dev->of_node)
  836. * will be used. If non-NULL, the device node must be a child (or further
  837. * descendant) of dev->of_node. Otherwise an ERR_PTR()-encoded -EINVAL
  838. * error code is returned.
  839. */
  840. struct phy_provider *__of_phy_provider_register(struct device *dev,
  841. struct device_node *children, struct module *owner,
  842. struct phy * (*of_xlate)(struct device *dev,
  843. struct of_phandle_args *args))
  844. {
  845. struct phy_provider *phy_provider;
  846. /*
  847. * If specified, the device node containing the children must itself
  848. * be the provider's device node or a child (or further descendant)
  849. * thereof.
  850. */
  851. if (children) {
  852. struct device_node *parent = of_node_get(children), *next;
  853. while (parent) {
  854. if (parent == dev->of_node)
  855. break;
  856. next = of_get_parent(parent);
  857. of_node_put(parent);
  858. parent = next;
  859. }
  860. if (!parent)
  861. return ERR_PTR(-EINVAL);
  862. of_node_put(parent);
  863. } else {
  864. children = dev->of_node;
  865. }
  866. phy_provider = kzalloc(sizeof(*phy_provider), GFP_KERNEL);
  867. if (!phy_provider)
  868. return ERR_PTR(-ENOMEM);
  869. phy_provider->dev = dev;
  870. phy_provider->children = of_node_get(children);
  871. phy_provider->owner = owner;
  872. phy_provider->of_xlate = of_xlate;
  873. mutex_lock(&phy_provider_mutex);
  874. list_add_tail(&phy_provider->list, &phy_provider_list);
  875. mutex_unlock(&phy_provider_mutex);
  876. return phy_provider;
  877. }
  878. EXPORT_SYMBOL_GPL(__of_phy_provider_register);
  879. /**
  880. * __devm_of_phy_provider_register() - create/register phy provider with the
  881. * framework
  882. * @dev: struct device of the phy provider
  883. * @children: device node containing children (if different from dev->of_node)
  884. * @owner: the module owner containing of_xlate
  885. * @of_xlate: function pointer to obtain phy instance from phy provider
  886. *
  887. * Creates struct phy_provider from dev and of_xlate function pointer.
  888. * This is used in the case of dt boot for finding the phy instance from
  889. * phy provider. While at that, it also associates the device with the
  890. * phy provider using devres. On driver detach, release function is invoked
  891. * on the devres data, then, devres data is freed.
  892. */
  893. struct phy_provider *__devm_of_phy_provider_register(struct device *dev,
  894. struct device_node *children, struct module *owner,
  895. struct phy * (*of_xlate)(struct device *dev,
  896. struct of_phandle_args *args))
  897. {
  898. struct phy_provider **ptr, *phy_provider;
  899. ptr = devres_alloc(devm_phy_provider_release, sizeof(*ptr), GFP_KERNEL);
  900. if (!ptr)
  901. return ERR_PTR(-ENOMEM);
  902. phy_provider = __of_phy_provider_register(dev, children, owner,
  903. of_xlate);
  904. if (!IS_ERR(phy_provider)) {
  905. *ptr = phy_provider;
  906. devres_add(dev, ptr);
  907. } else {
  908. devres_free(ptr);
  909. }
  910. return phy_provider;
  911. }
  912. EXPORT_SYMBOL_GPL(__devm_of_phy_provider_register);
  913. /**
  914. * of_phy_provider_unregister() - unregister phy provider from the framework
  915. * @phy_provider: phy provider returned by of_phy_provider_register()
  916. *
  917. * Removes the phy_provider created using of_phy_provider_register().
  918. */
  919. void of_phy_provider_unregister(struct phy_provider *phy_provider)
  920. {
  921. if (IS_ERR(phy_provider))
  922. return;
  923. mutex_lock(&phy_provider_mutex);
  924. list_del(&phy_provider->list);
  925. of_node_put(phy_provider->children);
  926. kfree(phy_provider);
  927. mutex_unlock(&phy_provider_mutex);
  928. }
  929. EXPORT_SYMBOL_GPL(of_phy_provider_unregister);
  930. /**
  931. * devm_of_phy_provider_unregister() - remove phy provider from the framework
  932. * @dev: struct device of the phy provider
  933. * @phy_provider: phy provider returned by of_phy_provider_register()
  934. *
  935. * destroys the devres associated with this phy provider and invokes
  936. * of_phy_provider_unregister to unregister the phy provider.
  937. */
  938. void devm_of_phy_provider_unregister(struct device *dev,
  939. struct phy_provider *phy_provider)
  940. {
  941. int r;
  942. r = devres_destroy(dev, devm_phy_provider_release, devm_phy_match,
  943. phy_provider);
  944. dev_WARN_ONCE(dev, r, "couldn't find PHY provider device resource\n");
  945. }
  946. EXPORT_SYMBOL_GPL(devm_of_phy_provider_unregister);
  947. /**
  948. * phy_release() - release the phy
  949. * @dev: the dev member within phy
  950. *
  951. * When the last reference to the device is removed, it is called
  952. * from the embedded kobject as release method.
  953. */
  954. static void phy_release(struct device *dev)
  955. {
  956. struct phy *phy;
  957. phy = to_phy(dev);
  958. dev_vdbg(dev, "releasing '%s'\n", dev_name(dev));
  959. regulator_put(phy->pwr);
  960. ida_simple_remove(&phy_ida, phy->id);
  961. kfree(phy);
  962. }
  963. static int __init phy_core_init(void)
  964. {
  965. phy_class = class_create(THIS_MODULE, "phy");
  966. if (IS_ERR(phy_class)) {
  967. pr_err("failed to create phy class --> %ld\n",
  968. PTR_ERR(phy_class));
  969. return PTR_ERR(phy_class);
  970. }
  971. phy_class->dev_release = phy_release;
  972. return 0;
  973. }
  974. device_initcall(phy_core_init);