phy-core.c 31 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318
  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(sizeof(*pl), GFP_KERNEL);
  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. struct device_node *child;
  118. list_for_each_entry(phy_provider, &phy_provider_list, list) {
  119. if (phy_provider->dev->of_node == node)
  120. return phy_provider;
  121. for_each_child_of_node(phy_provider->children, child)
  122. if (child == node) {
  123. of_node_put(child);
  124. return phy_provider;
  125. }
  126. }
  127. return ERR_PTR(-EPROBE_DEFER);
  128. }
  129. int phy_pm_runtime_get(struct phy *phy)
  130. {
  131. int ret;
  132. if (!phy)
  133. return 0;
  134. if (!pm_runtime_enabled(&phy->dev))
  135. return -ENOTSUPP;
  136. ret = pm_runtime_get(&phy->dev);
  137. if (ret < 0 && ret != -EINPROGRESS)
  138. pm_runtime_put_noidle(&phy->dev);
  139. return ret;
  140. }
  141. EXPORT_SYMBOL_GPL(phy_pm_runtime_get);
  142. int phy_pm_runtime_get_sync(struct phy *phy)
  143. {
  144. int ret;
  145. if (!phy)
  146. return 0;
  147. if (!pm_runtime_enabled(&phy->dev))
  148. return -ENOTSUPP;
  149. ret = pm_runtime_get_sync(&phy->dev);
  150. if (ret < 0)
  151. pm_runtime_put_sync(&phy->dev);
  152. return ret;
  153. }
  154. EXPORT_SYMBOL_GPL(phy_pm_runtime_get_sync);
  155. int phy_pm_runtime_put(struct phy *phy)
  156. {
  157. if (!phy)
  158. return 0;
  159. if (!pm_runtime_enabled(&phy->dev))
  160. return -ENOTSUPP;
  161. return pm_runtime_put(&phy->dev);
  162. }
  163. EXPORT_SYMBOL_GPL(phy_pm_runtime_put);
  164. int phy_pm_runtime_put_sync(struct phy *phy)
  165. {
  166. if (!phy)
  167. return 0;
  168. if (!pm_runtime_enabled(&phy->dev))
  169. return -ENOTSUPP;
  170. return pm_runtime_put_sync(&phy->dev);
  171. }
  172. EXPORT_SYMBOL_GPL(phy_pm_runtime_put_sync);
  173. void phy_pm_runtime_allow(struct phy *phy)
  174. {
  175. if (!phy)
  176. return;
  177. if (!pm_runtime_enabled(&phy->dev))
  178. return;
  179. pm_runtime_allow(&phy->dev);
  180. }
  181. EXPORT_SYMBOL_GPL(phy_pm_runtime_allow);
  182. void phy_pm_runtime_forbid(struct phy *phy)
  183. {
  184. if (!phy)
  185. return;
  186. if (!pm_runtime_enabled(&phy->dev))
  187. return;
  188. pm_runtime_forbid(&phy->dev);
  189. }
  190. EXPORT_SYMBOL_GPL(phy_pm_runtime_forbid);
  191. /**
  192. * phy_init - phy internal initialization before phy operation
  193. * @phy: the phy returned by phy_get()
  194. *
  195. * Used to allow phy's driver to perform phy internal initialization,
  196. * such as PLL block powering, clock initialization or anything that's
  197. * is required by the phy to perform the start of operation.
  198. * Must be called before phy_power_on().
  199. *
  200. * Return: %0 if successful, a negative error code otherwise
  201. */
  202. int phy_init(struct phy *phy)
  203. {
  204. int ret;
  205. if (!phy)
  206. return 0;
  207. ret = phy_pm_runtime_get_sync(phy);
  208. if (ret < 0 && ret != -ENOTSUPP)
  209. return ret;
  210. ret = 0; /* Override possible ret == -ENOTSUPP */
  211. mutex_lock(&phy->mutex);
  212. if (phy->power_count > phy->init_count)
  213. dev_warn(&phy->dev, "phy_power_on was called before phy_init\n");
  214. if (phy->init_count == 0 && phy->ops->init) {
  215. ret = phy->ops->init(phy);
  216. if (ret < 0) {
  217. dev_err(&phy->dev, "phy init failed --> %d\n", ret);
  218. goto out;
  219. }
  220. }
  221. ++phy->init_count;
  222. out:
  223. mutex_unlock(&phy->mutex);
  224. phy_pm_runtime_put(phy);
  225. return ret;
  226. }
  227. EXPORT_SYMBOL_GPL(phy_init);
  228. /**
  229. * phy_exit - Phy internal un-initialization
  230. * @phy: the phy returned by phy_get()
  231. *
  232. * Must be called after phy_power_off().
  233. *
  234. * Return: %0 if successful, a negative error code otherwise
  235. */
  236. int phy_exit(struct phy *phy)
  237. {
  238. int ret;
  239. if (!phy)
  240. return 0;
  241. ret = phy_pm_runtime_get_sync(phy);
  242. if (ret < 0 && ret != -ENOTSUPP)
  243. return ret;
  244. ret = 0; /* Override possible ret == -ENOTSUPP */
  245. mutex_lock(&phy->mutex);
  246. if (phy->init_count == 1 && phy->ops->exit) {
  247. ret = phy->ops->exit(phy);
  248. if (ret < 0) {
  249. dev_err(&phy->dev, "phy exit failed --> %d\n", ret);
  250. goto out;
  251. }
  252. }
  253. --phy->init_count;
  254. out:
  255. mutex_unlock(&phy->mutex);
  256. phy_pm_runtime_put(phy);
  257. return ret;
  258. }
  259. EXPORT_SYMBOL_GPL(phy_exit);
  260. /**
  261. * phy_power_on - Enable the phy and enter proper operation
  262. * @phy: the phy returned by phy_get()
  263. *
  264. * Must be called after phy_init().
  265. *
  266. * Return: %0 if successful, a negative error code otherwise
  267. */
  268. int phy_power_on(struct phy *phy)
  269. {
  270. int ret = 0;
  271. if (!phy)
  272. goto out;
  273. if (phy->pwr) {
  274. ret = regulator_enable(phy->pwr);
  275. if (ret)
  276. goto out;
  277. }
  278. ret = phy_pm_runtime_get_sync(phy);
  279. if (ret < 0 && ret != -ENOTSUPP)
  280. goto err_pm_sync;
  281. ret = 0; /* Override possible ret == -ENOTSUPP */
  282. mutex_lock(&phy->mutex);
  283. if (phy->power_count == 0 && phy->ops->power_on) {
  284. ret = phy->ops->power_on(phy);
  285. if (ret < 0) {
  286. dev_err(&phy->dev, "phy poweron failed --> %d\n", ret);
  287. goto err_pwr_on;
  288. }
  289. }
  290. ++phy->power_count;
  291. mutex_unlock(&phy->mutex);
  292. return 0;
  293. err_pwr_on:
  294. mutex_unlock(&phy->mutex);
  295. phy_pm_runtime_put_sync(phy);
  296. err_pm_sync:
  297. if (phy->pwr)
  298. regulator_disable(phy->pwr);
  299. out:
  300. return ret;
  301. }
  302. EXPORT_SYMBOL_GPL(phy_power_on);
  303. /**
  304. * phy_power_off - Disable the phy.
  305. * @phy: the phy returned by phy_get()
  306. *
  307. * Must be called before phy_exit().
  308. *
  309. * Return: %0 if successful, a negative error code otherwise
  310. */
  311. int phy_power_off(struct phy *phy)
  312. {
  313. int ret;
  314. if (!phy)
  315. return 0;
  316. mutex_lock(&phy->mutex);
  317. if (phy->power_count == 1 && phy->ops->power_off) {
  318. ret = phy->ops->power_off(phy);
  319. if (ret < 0) {
  320. dev_err(&phy->dev, "phy poweroff failed --> %d\n", ret);
  321. mutex_unlock(&phy->mutex);
  322. return ret;
  323. }
  324. }
  325. --phy->power_count;
  326. mutex_unlock(&phy->mutex);
  327. phy_pm_runtime_put(phy);
  328. if (phy->pwr)
  329. regulator_disable(phy->pwr);
  330. return 0;
  331. }
  332. EXPORT_SYMBOL_GPL(phy_power_off);
  333. int phy_set_mode_ext(struct phy *phy, enum phy_mode mode, int submode)
  334. {
  335. int ret = 0;
  336. if (!phy)
  337. return 0;
  338. mutex_lock(&phy->mutex);
  339. if (phy->ops->set_mode)
  340. ret = phy->ops->set_mode(phy, mode, submode);
  341. if (!ret)
  342. phy->attrs.mode = mode;
  343. mutex_unlock(&phy->mutex);
  344. return ret;
  345. }
  346. EXPORT_SYMBOL_GPL(phy_set_mode_ext);
  347. int phy_set_media(struct phy *phy, enum phy_media media)
  348. {
  349. int ret;
  350. if (!phy || !phy->ops->set_media)
  351. return 0;
  352. mutex_lock(&phy->mutex);
  353. ret = phy->ops->set_media(phy, media);
  354. mutex_unlock(&phy->mutex);
  355. return ret;
  356. }
  357. EXPORT_SYMBOL_GPL(phy_set_media);
  358. int phy_set_speed(struct phy *phy, int speed)
  359. {
  360. int ret;
  361. if (!phy || !phy->ops->set_speed)
  362. return 0;
  363. mutex_lock(&phy->mutex);
  364. ret = phy->ops->set_speed(phy, speed);
  365. mutex_unlock(&phy->mutex);
  366. return ret;
  367. }
  368. EXPORT_SYMBOL_GPL(phy_set_speed);
  369. int phy_reset(struct phy *phy)
  370. {
  371. int ret;
  372. if (!phy || !phy->ops->reset)
  373. return 0;
  374. ret = phy_pm_runtime_get_sync(phy);
  375. if (ret < 0 && ret != -ENOTSUPP)
  376. return ret;
  377. mutex_lock(&phy->mutex);
  378. ret = phy->ops->reset(phy);
  379. mutex_unlock(&phy->mutex);
  380. phy_pm_runtime_put(phy);
  381. return ret;
  382. }
  383. EXPORT_SYMBOL_GPL(phy_reset);
  384. /**
  385. * phy_calibrate() - Tunes the phy hw parameters for current configuration
  386. * @phy: the phy returned by phy_get()
  387. *
  388. * Used to calibrate phy hardware, typically by adjusting some parameters in
  389. * runtime, which are otherwise lost after host controller reset and cannot
  390. * be applied in phy_init() or phy_power_on().
  391. *
  392. * Return: %0 if successful, a negative error code otherwise
  393. */
  394. int phy_calibrate(struct phy *phy)
  395. {
  396. int ret;
  397. if (!phy || !phy->ops->calibrate)
  398. return 0;
  399. mutex_lock(&phy->mutex);
  400. ret = phy->ops->calibrate(phy);
  401. mutex_unlock(&phy->mutex);
  402. return ret;
  403. }
  404. EXPORT_SYMBOL_GPL(phy_calibrate);
  405. /**
  406. * phy_notify_connect() - phy connect notification
  407. * @phy: the phy returned by phy_get()
  408. * @port: the port index for connect
  409. *
  410. * If the phy needs to get connection status, the callback can be used.
  411. * Returns: %0 if successful, a negative error code otherwise
  412. */
  413. int phy_notify_connect(struct phy *phy, int port)
  414. {
  415. int ret;
  416. if (!phy || !phy->ops->connect)
  417. return 0;
  418. mutex_lock(&phy->mutex);
  419. ret = phy->ops->connect(phy, port);
  420. mutex_unlock(&phy->mutex);
  421. return ret;
  422. }
  423. EXPORT_SYMBOL_GPL(phy_notify_connect);
  424. /**
  425. * phy_notify_disconnect() - phy disconnect notification
  426. * @phy: the phy returned by phy_get()
  427. * @port: the port index for disconnect
  428. *
  429. * If the phy needs to get connection status, the callback can be used.
  430. *
  431. * Returns: %0 if successful, a negative error code otherwise
  432. */
  433. int phy_notify_disconnect(struct phy *phy, int port)
  434. {
  435. int ret;
  436. if (!phy || !phy->ops->disconnect)
  437. return 0;
  438. mutex_lock(&phy->mutex);
  439. ret = phy->ops->disconnect(phy, port);
  440. mutex_unlock(&phy->mutex);
  441. return ret;
  442. }
  443. EXPORT_SYMBOL_GPL(phy_notify_disconnect);
  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
  619. * @args: of_phandle_args (not used here)
  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 phy *phy;
  630. struct class_dev_iter iter;
  631. class_dev_iter_init(&iter, &phy_class, NULL, NULL);
  632. while ((dev = class_dev_iter_next(&iter))) {
  633. phy = to_phy(dev);
  634. if (args->np != phy->dev.of_node)
  635. continue;
  636. class_dev_iter_exit(&iter);
  637. return phy;
  638. }
  639. class_dev_iter_exit(&iter);
  640. return ERR_PTR(-ENODEV);
  641. }
  642. EXPORT_SYMBOL_GPL(of_phy_simple_xlate);
  643. /**
  644. * phy_get() - lookup and obtain a reference to a phy.
  645. * @dev: device that requests this phy
  646. * @string: the phy name as given in the dt data or the name of the controller
  647. * port for non-dt case
  648. *
  649. * Returns the phy driver, after getting a refcount to it; or
  650. * -ENODEV if there is no such phy. The caller is responsible for
  651. * calling phy_put() to release that count.
  652. */
  653. struct phy *phy_get(struct device *dev, const char *string)
  654. {
  655. int index = 0;
  656. struct phy *phy;
  657. struct device_link *link;
  658. if (dev->of_node) {
  659. if (string)
  660. index = of_property_match_string(dev->of_node, "phy-names",
  661. string);
  662. else
  663. index = 0;
  664. phy = _of_phy_get(dev->of_node, index);
  665. } else {
  666. if (string == NULL) {
  667. dev_WARN(dev, "missing string\n");
  668. return ERR_PTR(-EINVAL);
  669. }
  670. phy = phy_find(dev, string);
  671. }
  672. if (IS_ERR(phy))
  673. return phy;
  674. if (!try_module_get(phy->ops->owner))
  675. return ERR_PTR(-EPROBE_DEFER);
  676. get_device(&phy->dev);
  677. link = device_link_add(dev, &phy->dev, DL_FLAG_STATELESS);
  678. if (!link)
  679. dev_dbg(dev, "failed to create device link to %s\n",
  680. dev_name(phy->dev.parent));
  681. return phy;
  682. }
  683. EXPORT_SYMBOL_GPL(phy_get);
  684. /**
  685. * devm_phy_get() - lookup and obtain a reference to a phy.
  686. * @dev: device that requests this phy
  687. * @string: the phy name as given in the dt data or phy device name
  688. * for non-dt case
  689. *
  690. * Gets the phy using phy_get(), and associates a device with it using
  691. * devres. On driver detach, release function is invoked on the devres data,
  692. * then, devres data is freed.
  693. */
  694. struct phy *devm_phy_get(struct device *dev, const char *string)
  695. {
  696. struct phy **ptr, *phy;
  697. ptr = devres_alloc(devm_phy_release, sizeof(*ptr), GFP_KERNEL);
  698. if (!ptr)
  699. return ERR_PTR(-ENOMEM);
  700. phy = phy_get(dev, string);
  701. if (!IS_ERR(phy)) {
  702. *ptr = phy;
  703. devres_add(dev, ptr);
  704. } else {
  705. devres_free(ptr);
  706. }
  707. return phy;
  708. }
  709. EXPORT_SYMBOL_GPL(devm_phy_get);
  710. /**
  711. * devm_phy_optional_get() - lookup and obtain a reference to an optional phy.
  712. * @dev: device that requests this phy
  713. * @string: the phy name as given in the dt data or phy device name
  714. * for non-dt case
  715. *
  716. * Gets the phy using phy_get(), and associates a device with it using
  717. * devres. On driver detach, release function is invoked on the devres
  718. * data, then, devres data is freed. This differs to devm_phy_get() in
  719. * that if the phy does not exist, it is not considered an error and
  720. * -ENODEV will not be returned. Instead the NULL phy is returned,
  721. * which can be passed to all other phy consumer calls.
  722. */
  723. struct phy *devm_phy_optional_get(struct device *dev, const char *string)
  724. {
  725. struct phy *phy = devm_phy_get(dev, string);
  726. if (PTR_ERR(phy) == -ENODEV)
  727. phy = NULL;
  728. return phy;
  729. }
  730. EXPORT_SYMBOL_GPL(devm_phy_optional_get);
  731. /**
  732. * devm_of_phy_get() - lookup and obtain a reference to a phy.
  733. * @dev: device that requests this phy
  734. * @np: node containing the phy
  735. * @con_id: name of the phy from device's point of view
  736. *
  737. * Gets the phy using of_phy_get(), and associates a device with it using
  738. * devres. On driver detach, release function is invoked on the devres data,
  739. * then, devres data is freed.
  740. */
  741. struct phy *devm_of_phy_get(struct device *dev, struct device_node *np,
  742. const char *con_id)
  743. {
  744. struct phy **ptr, *phy;
  745. struct device_link *link;
  746. ptr = devres_alloc(devm_phy_release, sizeof(*ptr), GFP_KERNEL);
  747. if (!ptr)
  748. return ERR_PTR(-ENOMEM);
  749. phy = of_phy_get(np, con_id);
  750. if (!IS_ERR(phy)) {
  751. *ptr = phy;
  752. devres_add(dev, ptr);
  753. } else {
  754. devres_free(ptr);
  755. return phy;
  756. }
  757. link = device_link_add(dev, &phy->dev, DL_FLAG_STATELESS);
  758. if (!link)
  759. dev_dbg(dev, "failed to create device link to %s\n",
  760. dev_name(phy->dev.parent));
  761. return phy;
  762. }
  763. EXPORT_SYMBOL_GPL(devm_of_phy_get);
  764. /**
  765. * devm_of_phy_optional_get() - lookup and obtain a reference to an optional
  766. * phy.
  767. * @dev: device that requests this phy
  768. * @np: node containing the phy
  769. * @con_id: name of the phy from device's point of view
  770. *
  771. * Gets the phy using of_phy_get(), and associates a device with it using
  772. * devres. On driver detach, release function is invoked on the devres data,
  773. * then, devres data is freed. This differs to devm_of_phy_get() in
  774. * that if the phy does not exist, it is not considered an error and
  775. * -ENODEV will not be returned. Instead the NULL phy is returned,
  776. * which can be passed to all other phy consumer calls.
  777. */
  778. struct phy *devm_of_phy_optional_get(struct device *dev, struct device_node *np,
  779. const char *con_id)
  780. {
  781. struct phy *phy = devm_of_phy_get(dev, np, con_id);
  782. if (PTR_ERR(phy) == -ENODEV)
  783. phy = NULL;
  784. if (IS_ERR(phy))
  785. dev_err_probe(dev, PTR_ERR(phy), "failed to get PHY %pOF:%s",
  786. np, con_id);
  787. return phy;
  788. }
  789. EXPORT_SYMBOL_GPL(devm_of_phy_optional_get);
  790. /**
  791. * devm_of_phy_get_by_index() - lookup and obtain a reference to a phy by index.
  792. * @dev: device that requests this phy
  793. * @np: node containing the phy
  794. * @index: index of the phy
  795. *
  796. * Gets the phy using _of_phy_get(), then gets a refcount to it,
  797. * and associates a device with it using devres. On driver detach,
  798. * release function is invoked on the devres data,
  799. * then, devres data is freed.
  800. *
  801. */
  802. struct phy *devm_of_phy_get_by_index(struct device *dev, struct device_node *np,
  803. int index)
  804. {
  805. struct phy **ptr, *phy;
  806. struct device_link *link;
  807. ptr = devres_alloc(devm_phy_release, sizeof(*ptr), GFP_KERNEL);
  808. if (!ptr)
  809. return ERR_PTR(-ENOMEM);
  810. phy = _of_phy_get(np, index);
  811. if (IS_ERR(phy)) {
  812. devres_free(ptr);
  813. return phy;
  814. }
  815. if (!try_module_get(phy->ops->owner)) {
  816. devres_free(ptr);
  817. return ERR_PTR(-EPROBE_DEFER);
  818. }
  819. get_device(&phy->dev);
  820. *ptr = phy;
  821. devres_add(dev, ptr);
  822. link = device_link_add(dev, &phy->dev, DL_FLAG_STATELESS);
  823. if (!link)
  824. dev_dbg(dev, "failed to create device link to %s\n",
  825. dev_name(phy->dev.parent));
  826. return phy;
  827. }
  828. EXPORT_SYMBOL_GPL(devm_of_phy_get_by_index);
  829. /**
  830. * phy_create() - create a new phy
  831. * @dev: device that is creating the new phy
  832. * @node: device node of the phy
  833. * @ops: function pointers for performing phy operations
  834. *
  835. * Called to create a phy using phy framework.
  836. */
  837. struct phy *phy_create(struct device *dev, struct device_node *node,
  838. const struct phy_ops *ops)
  839. {
  840. int ret;
  841. int id;
  842. struct phy *phy;
  843. if (WARN_ON(!dev))
  844. return ERR_PTR(-EINVAL);
  845. phy = kzalloc(sizeof(*phy), GFP_KERNEL);
  846. if (!phy)
  847. return ERR_PTR(-ENOMEM);
  848. id = ida_alloc(&phy_ida, GFP_KERNEL);
  849. if (id < 0) {
  850. dev_err(dev, "unable to get id\n");
  851. ret = id;
  852. goto free_phy;
  853. }
  854. device_initialize(&phy->dev);
  855. mutex_init(&phy->mutex);
  856. phy->dev.class = &phy_class;
  857. phy->dev.parent = dev;
  858. phy->dev.of_node = node ?: dev->of_node;
  859. phy->id = id;
  860. phy->ops = ops;
  861. ret = dev_set_name(&phy->dev, "phy-%s.%d", dev_name(dev), id);
  862. if (ret)
  863. goto put_dev;
  864. /* phy-supply */
  865. phy->pwr = regulator_get_optional(&phy->dev, "phy");
  866. if (IS_ERR(phy->pwr)) {
  867. ret = PTR_ERR(phy->pwr);
  868. if (ret == -EPROBE_DEFER)
  869. goto put_dev;
  870. phy->pwr = NULL;
  871. }
  872. ret = device_add(&phy->dev);
  873. if (ret)
  874. goto put_dev;
  875. if (pm_runtime_enabled(dev)) {
  876. pm_runtime_enable(&phy->dev);
  877. pm_runtime_no_callbacks(&phy->dev);
  878. }
  879. phy->debugfs = debugfs_create_dir(dev_name(&phy->dev), phy_debugfs_root);
  880. return phy;
  881. put_dev:
  882. put_device(&phy->dev); /* calls phy_release() which frees resources */
  883. return ERR_PTR(ret);
  884. free_phy:
  885. kfree(phy);
  886. return ERR_PTR(ret);
  887. }
  888. EXPORT_SYMBOL_GPL(phy_create);
  889. /**
  890. * devm_phy_create() - create a new phy
  891. * @dev: device that is creating the new phy
  892. * @node: device node of the phy
  893. * @ops: function pointers for performing phy operations
  894. *
  895. * Creates a new PHY device adding it to the PHY class.
  896. * While at that, it also associates the device with the phy using devres.
  897. * On driver detach, release function is invoked on the devres data,
  898. * then, devres data is freed.
  899. */
  900. struct phy *devm_phy_create(struct device *dev, struct device_node *node,
  901. const struct phy_ops *ops)
  902. {
  903. struct phy **ptr, *phy;
  904. ptr = devres_alloc(devm_phy_consume, sizeof(*ptr), GFP_KERNEL);
  905. if (!ptr)
  906. return ERR_PTR(-ENOMEM);
  907. phy = phy_create(dev, node, ops);
  908. if (!IS_ERR(phy)) {
  909. *ptr = phy;
  910. devres_add(dev, ptr);
  911. } else {
  912. devres_free(ptr);
  913. }
  914. return phy;
  915. }
  916. EXPORT_SYMBOL_GPL(devm_phy_create);
  917. /**
  918. * phy_destroy() - destroy the phy
  919. * @phy: the phy to be destroyed
  920. *
  921. * Called to destroy the phy.
  922. */
  923. void phy_destroy(struct phy *phy)
  924. {
  925. pm_runtime_disable(&phy->dev);
  926. device_unregister(&phy->dev);
  927. }
  928. EXPORT_SYMBOL_GPL(phy_destroy);
  929. /**
  930. * devm_phy_destroy() - destroy the PHY
  931. * @dev: device that wants to release this phy
  932. * @phy: the phy returned by devm_phy_get()
  933. *
  934. * destroys the devres associated with this phy and invokes phy_destroy
  935. * to destroy the phy.
  936. */
  937. void devm_phy_destroy(struct device *dev, struct phy *phy)
  938. {
  939. int r;
  940. r = devres_release(dev, devm_phy_consume, devm_phy_match, phy);
  941. dev_WARN_ONCE(dev, r, "couldn't find PHY resource\n");
  942. }
  943. EXPORT_SYMBOL_GPL(devm_phy_destroy);
  944. /**
  945. * __of_phy_provider_register() - create/register phy provider with the framework
  946. * @dev: struct device of the phy provider
  947. * @children: device node containing children (if different from dev->of_node)
  948. * @owner: the module owner containing of_xlate
  949. * @of_xlate: function pointer to obtain phy instance from phy provider
  950. *
  951. * Creates struct phy_provider from dev and of_xlate function pointer.
  952. * This is used in the case of dt boot for finding the phy instance from
  953. * phy provider.
  954. *
  955. * If the PHY provider doesn't nest children directly but uses a separate
  956. * child node to contain the individual children, the @children parameter
  957. * can be used to override the default. If NULL, the default (dev->of_node)
  958. * will be used. If non-NULL, the device node must be a child (or further
  959. * descendant) of dev->of_node. Otherwise an ERR_PTR()-encoded -EINVAL
  960. * error code is returned.
  961. */
  962. struct phy_provider *__of_phy_provider_register(struct device *dev,
  963. struct device_node *children, struct module *owner,
  964. struct phy * (*of_xlate)(struct device *dev,
  965. const struct of_phandle_args *args))
  966. {
  967. struct phy_provider *phy_provider;
  968. /*
  969. * If specified, the device node containing the children must itself
  970. * be the provider's device node or a child (or further descendant)
  971. * thereof.
  972. */
  973. if (children) {
  974. struct device_node *parent = of_node_get(children), *next;
  975. while (parent) {
  976. if (parent == dev->of_node)
  977. break;
  978. next = of_get_parent(parent);
  979. of_node_put(parent);
  980. parent = next;
  981. }
  982. if (!parent)
  983. return ERR_PTR(-EINVAL);
  984. of_node_put(parent);
  985. } else {
  986. children = dev->of_node;
  987. }
  988. phy_provider = kzalloc(sizeof(*phy_provider), GFP_KERNEL);
  989. if (!phy_provider)
  990. return ERR_PTR(-ENOMEM);
  991. phy_provider->dev = dev;
  992. phy_provider->children = of_node_get(children);
  993. phy_provider->owner = owner;
  994. phy_provider->of_xlate = of_xlate;
  995. mutex_lock(&phy_provider_mutex);
  996. list_add_tail(&phy_provider->list, &phy_provider_list);
  997. mutex_unlock(&phy_provider_mutex);
  998. return phy_provider;
  999. }
  1000. EXPORT_SYMBOL_GPL(__of_phy_provider_register);
  1001. /**
  1002. * __devm_of_phy_provider_register() - create/register phy provider with the
  1003. * framework
  1004. * @dev: struct device of the phy provider
  1005. * @children: device node containing children (if different from dev->of_node)
  1006. * @owner: the module owner containing of_xlate
  1007. * @of_xlate: function pointer to obtain phy instance from phy provider
  1008. *
  1009. * Creates struct phy_provider from dev and of_xlate function pointer.
  1010. * This is used in the case of dt boot for finding the phy instance from
  1011. * phy provider. While at that, it also associates the device with the
  1012. * phy provider using devres. On driver detach, release function is invoked
  1013. * on the devres data, then, devres data is freed.
  1014. */
  1015. struct phy_provider *__devm_of_phy_provider_register(struct device *dev,
  1016. struct device_node *children, struct module *owner,
  1017. struct phy * (*of_xlate)(struct device *dev,
  1018. const struct of_phandle_args *args))
  1019. {
  1020. struct phy_provider **ptr, *phy_provider;
  1021. ptr = devres_alloc(devm_phy_provider_release, sizeof(*ptr), GFP_KERNEL);
  1022. if (!ptr)
  1023. return ERR_PTR(-ENOMEM);
  1024. phy_provider = __of_phy_provider_register(dev, children, owner,
  1025. of_xlate);
  1026. if (!IS_ERR(phy_provider)) {
  1027. *ptr = phy_provider;
  1028. devres_add(dev, ptr);
  1029. } else {
  1030. devres_free(ptr);
  1031. }
  1032. return phy_provider;
  1033. }
  1034. EXPORT_SYMBOL_GPL(__devm_of_phy_provider_register);
  1035. /**
  1036. * of_phy_provider_unregister() - unregister phy provider from the framework
  1037. * @phy_provider: phy provider returned by of_phy_provider_register()
  1038. *
  1039. * Removes the phy_provider created using of_phy_provider_register().
  1040. */
  1041. void of_phy_provider_unregister(struct phy_provider *phy_provider)
  1042. {
  1043. if (IS_ERR(phy_provider))
  1044. return;
  1045. mutex_lock(&phy_provider_mutex);
  1046. list_del(&phy_provider->list);
  1047. of_node_put(phy_provider->children);
  1048. kfree(phy_provider);
  1049. mutex_unlock(&phy_provider_mutex);
  1050. }
  1051. EXPORT_SYMBOL_GPL(of_phy_provider_unregister);
  1052. /**
  1053. * devm_of_phy_provider_unregister() - remove phy provider from the framework
  1054. * @dev: struct device of the phy provider
  1055. * @phy_provider: phy provider returned by of_phy_provider_register()
  1056. *
  1057. * destroys the devres associated with this phy provider and invokes
  1058. * of_phy_provider_unregister to unregister the phy provider.
  1059. */
  1060. void devm_of_phy_provider_unregister(struct device *dev,
  1061. struct phy_provider *phy_provider)
  1062. {
  1063. int r;
  1064. r = devres_release(dev, devm_phy_provider_release, devm_phy_match,
  1065. phy_provider);
  1066. dev_WARN_ONCE(dev, r, "couldn't find PHY provider device resource\n");
  1067. }
  1068. EXPORT_SYMBOL_GPL(devm_of_phy_provider_unregister);
  1069. /**
  1070. * phy_release() - release the phy
  1071. * @dev: the dev member within phy
  1072. *
  1073. * When the last reference to the device is removed, it is called
  1074. * from the embedded kobject as release method.
  1075. */
  1076. static void phy_release(struct device *dev)
  1077. {
  1078. struct phy *phy;
  1079. phy = to_phy(dev);
  1080. dev_vdbg(dev, "releasing '%s'\n", dev_name(dev));
  1081. debugfs_remove_recursive(phy->debugfs);
  1082. regulator_put(phy->pwr);
  1083. ida_free(&phy_ida, phy->id);
  1084. kfree(phy);
  1085. }
  1086. static int __init phy_core_init(void)
  1087. {
  1088. int err;
  1089. err = class_register(&phy_class);
  1090. if (err) {
  1091. pr_err("failed to register phy class");
  1092. return err;
  1093. }
  1094. phy_debugfs_root = debugfs_create_dir("phy", NULL);
  1095. return 0;
  1096. }
  1097. device_initcall(phy_core_init);
  1098. static void __exit phy_core_exit(void)
  1099. {
  1100. debugfs_remove_recursive(phy_debugfs_root);
  1101. class_unregister(&phy_class);
  1102. }
  1103. module_exit(phy_core_exit);