phy-core.c 31 KB

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