phy-core.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768
  1. /*
  2. * phy-core.c -- Generic Phy framework.
  3. *
  4. * Copyright (C) 2013 Texas Instruments Incorporated - http://www.ti.com
  5. *
  6. * Author: Kishon Vijay Abraham I <kishon@ti.com>
  7. *
  8. * This program is free software; you can redistribute it and/or modify it
  9. * under the terms of the GNU General Public License as published by the
  10. * Free Software Foundation; either version 2 of the License, or (at your
  11. * option) any later version.
  12. */
  13. #include <linux/kernel.h>
  14. #include <linux/export.h>
  15. #include <linux/module.h>
  16. #include <linux/err.h>
  17. #include <linux/device.h>
  18. #include <linux/slab.h>
  19. #include <linux/of.h>
  20. #include <linux/phy/phy.h>
  21. #include <linux/idr.h>
  22. #include <linux/pm_runtime.h>
  23. static struct class *phy_class;
  24. static DEFINE_MUTEX(phy_provider_mutex);
  25. static LIST_HEAD(phy_provider_list);
  26. static DEFINE_IDA(phy_ida);
  27. static void devm_phy_release(struct device *dev, void *res)
  28. {
  29. struct phy *phy = *(struct phy **)res;
  30. phy_put(phy);
  31. }
  32. static void devm_phy_provider_release(struct device *dev, void *res)
  33. {
  34. struct phy_provider *phy_provider = *(struct phy_provider **)res;
  35. of_phy_provider_unregister(phy_provider);
  36. }
  37. static void devm_phy_consume(struct device *dev, void *res)
  38. {
  39. struct phy *phy = *(struct phy **)res;
  40. phy_destroy(phy);
  41. }
  42. static int devm_phy_match(struct device *dev, void *res, void *match_data)
  43. {
  44. return res == match_data;
  45. }
  46. static struct phy *phy_lookup(struct device *device, const char *port)
  47. {
  48. unsigned int count;
  49. struct phy *phy;
  50. struct device *dev;
  51. struct phy_consumer *consumers;
  52. struct class_dev_iter iter;
  53. class_dev_iter_init(&iter, phy_class, NULL, NULL);
  54. while ((dev = class_dev_iter_next(&iter))) {
  55. phy = to_phy(dev);
  56. count = phy->init_data->num_consumers;
  57. consumers = phy->init_data->consumers;
  58. while (count--) {
  59. if (!strcmp(consumers->dev_name, dev_name(device)) &&
  60. !strcmp(consumers->port, port)) {
  61. class_dev_iter_exit(&iter);
  62. return phy;
  63. }
  64. consumers++;
  65. }
  66. }
  67. class_dev_iter_exit(&iter);
  68. return ERR_PTR(-ENODEV);
  69. }
  70. static struct phy_provider *of_phy_provider_lookup(struct device_node *node)
  71. {
  72. struct phy_provider *phy_provider;
  73. list_for_each_entry(phy_provider, &phy_provider_list, list) {
  74. if (phy_provider->dev->of_node == node)
  75. return phy_provider;
  76. }
  77. return ERR_PTR(-EPROBE_DEFER);
  78. }
  79. int phy_pm_runtime_get(struct phy *phy)
  80. {
  81. int ret;
  82. if (!pm_runtime_enabled(&phy->dev))
  83. return -ENOTSUPP;
  84. ret = pm_runtime_get(&phy->dev);
  85. if (ret < 0 && ret != -EINPROGRESS)
  86. pm_runtime_put_noidle(&phy->dev);
  87. return ret;
  88. }
  89. EXPORT_SYMBOL_GPL(phy_pm_runtime_get);
  90. int phy_pm_runtime_get_sync(struct phy *phy)
  91. {
  92. int ret;
  93. if (!pm_runtime_enabled(&phy->dev))
  94. return -ENOTSUPP;
  95. ret = pm_runtime_get_sync(&phy->dev);
  96. if (ret < 0)
  97. pm_runtime_put_sync(&phy->dev);
  98. return ret;
  99. }
  100. EXPORT_SYMBOL_GPL(phy_pm_runtime_get_sync);
  101. int phy_pm_runtime_put(struct phy *phy)
  102. {
  103. if (!pm_runtime_enabled(&phy->dev))
  104. return -ENOTSUPP;
  105. return pm_runtime_put(&phy->dev);
  106. }
  107. EXPORT_SYMBOL_GPL(phy_pm_runtime_put);
  108. int phy_pm_runtime_put_sync(struct phy *phy)
  109. {
  110. if (!pm_runtime_enabled(&phy->dev))
  111. return -ENOTSUPP;
  112. return pm_runtime_put_sync(&phy->dev);
  113. }
  114. EXPORT_SYMBOL_GPL(phy_pm_runtime_put_sync);
  115. void phy_pm_runtime_allow(struct phy *phy)
  116. {
  117. if (!pm_runtime_enabled(&phy->dev))
  118. return;
  119. pm_runtime_allow(&phy->dev);
  120. }
  121. EXPORT_SYMBOL_GPL(phy_pm_runtime_allow);
  122. void phy_pm_runtime_forbid(struct phy *phy)
  123. {
  124. if (!pm_runtime_enabled(&phy->dev))
  125. return;
  126. pm_runtime_forbid(&phy->dev);
  127. }
  128. EXPORT_SYMBOL_GPL(phy_pm_runtime_forbid);
  129. int phy_init(struct phy *phy)
  130. {
  131. int ret;
  132. if (!phy)
  133. return 0;
  134. ret = phy_pm_runtime_get_sync(phy);
  135. if (ret < 0 && ret != -ENOTSUPP)
  136. return ret;
  137. mutex_lock(&phy->mutex);
  138. if (phy->init_count == 0 && phy->ops->init) {
  139. ret = phy->ops->init(phy);
  140. if (ret < 0) {
  141. dev_err(&phy->dev, "phy init failed --> %d\n", ret);
  142. goto out;
  143. }
  144. } else {
  145. ret = 0; /* Override possible ret == -ENOTSUPP */
  146. }
  147. ++phy->init_count;
  148. out:
  149. mutex_unlock(&phy->mutex);
  150. phy_pm_runtime_put(phy);
  151. return ret;
  152. }
  153. EXPORT_SYMBOL_GPL(phy_init);
  154. int phy_exit(struct phy *phy)
  155. {
  156. int ret;
  157. if (!phy)
  158. return 0;
  159. ret = phy_pm_runtime_get_sync(phy);
  160. if (ret < 0 && ret != -ENOTSUPP)
  161. return ret;
  162. mutex_lock(&phy->mutex);
  163. if (phy->init_count == 1 && phy->ops->exit) {
  164. ret = phy->ops->exit(phy);
  165. if (ret < 0) {
  166. dev_err(&phy->dev, "phy exit failed --> %d\n", ret);
  167. goto out;
  168. }
  169. }
  170. --phy->init_count;
  171. out:
  172. mutex_unlock(&phy->mutex);
  173. phy_pm_runtime_put(phy);
  174. return ret;
  175. }
  176. EXPORT_SYMBOL_GPL(phy_exit);
  177. int phy_power_on(struct phy *phy)
  178. {
  179. int ret;
  180. if (!phy)
  181. return 0;
  182. ret = phy_pm_runtime_get_sync(phy);
  183. if (ret < 0 && ret != -ENOTSUPP)
  184. return ret;
  185. mutex_lock(&phy->mutex);
  186. if (phy->power_count == 0 && phy->ops->power_on) {
  187. ret = phy->ops->power_on(phy);
  188. if (ret < 0) {
  189. dev_err(&phy->dev, "phy poweron failed --> %d\n", ret);
  190. goto out;
  191. }
  192. } else {
  193. ret = 0; /* Override possible ret == -ENOTSUPP */
  194. }
  195. ++phy->power_count;
  196. mutex_unlock(&phy->mutex);
  197. return 0;
  198. out:
  199. mutex_unlock(&phy->mutex);
  200. phy_pm_runtime_put_sync(phy);
  201. return ret;
  202. }
  203. EXPORT_SYMBOL_GPL(phy_power_on);
  204. int phy_power_off(struct phy *phy)
  205. {
  206. int ret;
  207. if (!phy)
  208. return 0;
  209. mutex_lock(&phy->mutex);
  210. if (phy->power_count == 1 && phy->ops->power_off) {
  211. ret = phy->ops->power_off(phy);
  212. if (ret < 0) {
  213. dev_err(&phy->dev, "phy poweroff failed --> %d\n", ret);
  214. mutex_unlock(&phy->mutex);
  215. return ret;
  216. }
  217. }
  218. --phy->power_count;
  219. mutex_unlock(&phy->mutex);
  220. phy_pm_runtime_put(phy);
  221. return 0;
  222. }
  223. EXPORT_SYMBOL_GPL(phy_power_off);
  224. /**
  225. * of_phy_get() - lookup and obtain a reference to a phy by phandle
  226. * @dev: device that requests this phy
  227. * @index: the index of the phy
  228. *
  229. * Returns the phy associated with the given phandle value,
  230. * after getting a refcount to it or -ENODEV if there is no such phy or
  231. * -EPROBE_DEFER if there is a phandle to the phy, but the device is
  232. * not yet loaded. This function uses of_xlate call back function provided
  233. * while registering the phy_provider to find the phy instance.
  234. */
  235. static struct phy *of_phy_get(struct device *dev, int index)
  236. {
  237. int ret;
  238. struct phy_provider *phy_provider;
  239. struct phy *phy = NULL;
  240. struct of_phandle_args args;
  241. ret = of_parse_phandle_with_args(dev->of_node, "phys", "#phy-cells",
  242. index, &args);
  243. if (ret) {
  244. dev_dbg(dev, "failed to get phy in %s node\n",
  245. dev->of_node->full_name);
  246. return ERR_PTR(-ENODEV);
  247. }
  248. mutex_lock(&phy_provider_mutex);
  249. phy_provider = of_phy_provider_lookup(args.np);
  250. if (IS_ERR(phy_provider) || !try_module_get(phy_provider->owner)) {
  251. phy = ERR_PTR(-EPROBE_DEFER);
  252. goto err0;
  253. }
  254. phy = phy_provider->of_xlate(phy_provider->dev, &args);
  255. module_put(phy_provider->owner);
  256. err0:
  257. mutex_unlock(&phy_provider_mutex);
  258. of_node_put(args.np);
  259. return phy;
  260. }
  261. /**
  262. * phy_put() - release the PHY
  263. * @phy: the phy returned by phy_get()
  264. *
  265. * Releases a refcount the caller received from phy_get().
  266. */
  267. void phy_put(struct phy *phy)
  268. {
  269. if (!phy || IS_ERR(phy))
  270. return;
  271. module_put(phy->ops->owner);
  272. put_device(&phy->dev);
  273. }
  274. EXPORT_SYMBOL_GPL(phy_put);
  275. /**
  276. * devm_phy_put() - release the PHY
  277. * @dev: device that wants to release this phy
  278. * @phy: the phy returned by devm_phy_get()
  279. *
  280. * destroys the devres associated with this phy and invokes phy_put
  281. * to release the phy.
  282. */
  283. void devm_phy_put(struct device *dev, struct phy *phy)
  284. {
  285. int r;
  286. if (!phy)
  287. return;
  288. r = devres_destroy(dev, devm_phy_release, devm_phy_match, phy);
  289. dev_WARN_ONCE(dev, r, "couldn't find PHY resource\n");
  290. }
  291. EXPORT_SYMBOL_GPL(devm_phy_put);
  292. /**
  293. * of_phy_simple_xlate() - returns the phy instance from phy provider
  294. * @dev: the PHY provider device
  295. * @args: of_phandle_args (not used here)
  296. *
  297. * Intended to be used by phy provider for the common case where #phy-cells is
  298. * 0. For other cases where #phy-cells is greater than '0', the phy provider
  299. * should provide a custom of_xlate function that reads the *args* and returns
  300. * the appropriate phy.
  301. */
  302. struct phy *of_phy_simple_xlate(struct device *dev, struct of_phandle_args
  303. *args)
  304. {
  305. struct phy *phy;
  306. struct class_dev_iter iter;
  307. struct device_node *node = dev->of_node;
  308. class_dev_iter_init(&iter, phy_class, NULL, NULL);
  309. while ((dev = class_dev_iter_next(&iter))) {
  310. phy = to_phy(dev);
  311. if (node != phy->dev.of_node)
  312. continue;
  313. class_dev_iter_exit(&iter);
  314. return phy;
  315. }
  316. class_dev_iter_exit(&iter);
  317. return ERR_PTR(-ENODEV);
  318. }
  319. EXPORT_SYMBOL_GPL(of_phy_simple_xlate);
  320. /**
  321. * phy_get() - lookup and obtain a reference to a phy.
  322. * @dev: device that requests this phy
  323. * @string: the phy name as given in the dt data or the name of the controller
  324. * port for non-dt case
  325. *
  326. * Returns the phy driver, after getting a refcount to it; or
  327. * -ENODEV if there is no such phy. The caller is responsible for
  328. * calling phy_put() to release that count.
  329. */
  330. struct phy *phy_get(struct device *dev, const char *string)
  331. {
  332. int index = 0;
  333. struct phy *phy;
  334. if (string == NULL) {
  335. dev_WARN(dev, "missing string\n");
  336. return ERR_PTR(-EINVAL);
  337. }
  338. if (dev->of_node) {
  339. index = of_property_match_string(dev->of_node, "phy-names",
  340. string);
  341. phy = of_phy_get(dev, index);
  342. } else {
  343. phy = phy_lookup(dev, string);
  344. }
  345. if (IS_ERR(phy))
  346. return phy;
  347. if (!try_module_get(phy->ops->owner))
  348. return ERR_PTR(-EPROBE_DEFER);
  349. get_device(&phy->dev);
  350. return phy;
  351. }
  352. EXPORT_SYMBOL_GPL(phy_get);
  353. /**
  354. * phy_optional_get() - lookup and obtain a reference to an optional phy.
  355. * @dev: device that requests this phy
  356. * @string: the phy name as given in the dt data or the name of the controller
  357. * port for non-dt case
  358. *
  359. * Returns the phy driver, after getting a refcount to it; or
  360. * NULL if there is no such phy. The caller is responsible for
  361. * calling phy_put() to release that count.
  362. */
  363. struct phy *phy_optional_get(struct device *dev, const char *string)
  364. {
  365. struct phy *phy = phy_get(dev, string);
  366. if (PTR_ERR(phy) == -ENODEV)
  367. phy = NULL;
  368. return phy;
  369. }
  370. EXPORT_SYMBOL_GPL(phy_optional_get);
  371. /**
  372. * devm_phy_get() - lookup and obtain a reference to a phy.
  373. * @dev: device that requests this phy
  374. * @string: the phy name as given in the dt data or phy device name
  375. * for non-dt case
  376. *
  377. * Gets the phy using phy_get(), and associates a device with it using
  378. * devres. On driver detach, release function is invoked on the devres data,
  379. * then, devres data is freed.
  380. */
  381. struct phy *devm_phy_get(struct device *dev, const char *string)
  382. {
  383. struct phy **ptr, *phy;
  384. ptr = devres_alloc(devm_phy_release, sizeof(*ptr), GFP_KERNEL);
  385. if (!ptr)
  386. return ERR_PTR(-ENOMEM);
  387. phy = phy_get(dev, string);
  388. if (!IS_ERR(phy)) {
  389. *ptr = phy;
  390. devres_add(dev, ptr);
  391. } else {
  392. devres_free(ptr);
  393. }
  394. return phy;
  395. }
  396. EXPORT_SYMBOL_GPL(devm_phy_get);
  397. /**
  398. * devm_phy_optional_get() - lookup and obtain a reference to an optional phy.
  399. * @dev: device that requests this phy
  400. * @string: the phy name as given in the dt data or phy device name
  401. * for non-dt case
  402. *
  403. * Gets the phy using phy_get(), and associates a device with it using
  404. * devres. On driver detach, release function is invoked on the devres
  405. * data, then, devres data is freed. This differs to devm_phy_get() in
  406. * that if the phy does not exist, it is not considered an error and
  407. * -ENODEV will not be returned. Instead the NULL phy is returned,
  408. * which can be passed to all other phy consumer calls.
  409. */
  410. struct phy *devm_phy_optional_get(struct device *dev, const char *string)
  411. {
  412. struct phy *phy = devm_phy_get(dev, string);
  413. if (PTR_ERR(phy) == -ENODEV)
  414. phy = NULL;
  415. return phy;
  416. }
  417. EXPORT_SYMBOL_GPL(devm_phy_optional_get);
  418. /**
  419. * phy_create() - create a new phy
  420. * @dev: device that is creating the new phy
  421. * @ops: function pointers for performing phy operations
  422. * @init_data: contains the list of PHY consumers or NULL
  423. *
  424. * Called to create a phy using phy framework.
  425. */
  426. struct phy *phy_create(struct device *dev, const struct phy_ops *ops,
  427. struct phy_init_data *init_data)
  428. {
  429. int ret;
  430. int id;
  431. struct phy *phy;
  432. if (WARN_ON(!dev))
  433. return ERR_PTR(-EINVAL);
  434. phy = kzalloc(sizeof(*phy), GFP_KERNEL);
  435. if (!phy)
  436. return ERR_PTR(-ENOMEM);
  437. id = ida_simple_get(&phy_ida, 0, 0, GFP_KERNEL);
  438. if (id < 0) {
  439. dev_err(dev, "unable to get id\n");
  440. ret = id;
  441. goto free_phy;
  442. }
  443. device_initialize(&phy->dev);
  444. mutex_init(&phy->mutex);
  445. phy->dev.class = phy_class;
  446. phy->dev.parent = dev;
  447. phy->dev.of_node = dev->of_node;
  448. phy->id = id;
  449. phy->ops = ops;
  450. phy->init_data = init_data;
  451. ret = dev_set_name(&phy->dev, "phy-%s.%d", dev_name(dev), id);
  452. if (ret)
  453. goto put_dev;
  454. ret = device_add(&phy->dev);
  455. if (ret)
  456. goto put_dev;
  457. if (pm_runtime_enabled(dev)) {
  458. pm_runtime_enable(&phy->dev);
  459. pm_runtime_no_callbacks(&phy->dev);
  460. }
  461. return phy;
  462. put_dev:
  463. put_device(&phy->dev);
  464. ida_remove(&phy_ida, phy->id);
  465. free_phy:
  466. kfree(phy);
  467. return ERR_PTR(ret);
  468. }
  469. EXPORT_SYMBOL_GPL(phy_create);
  470. /**
  471. * devm_phy_create() - create a new phy
  472. * @dev: device that is creating the new phy
  473. * @ops: function pointers for performing phy operations
  474. * @init_data: contains the list of PHY consumers or NULL
  475. *
  476. * Creates a new PHY device adding it to the PHY class.
  477. * While at that, it also associates the device with the phy using devres.
  478. * On driver detach, release function is invoked on the devres data,
  479. * then, devres data is freed.
  480. */
  481. struct phy *devm_phy_create(struct device *dev, const struct phy_ops *ops,
  482. struct phy_init_data *init_data)
  483. {
  484. struct phy **ptr, *phy;
  485. ptr = devres_alloc(devm_phy_consume, sizeof(*ptr), GFP_KERNEL);
  486. if (!ptr)
  487. return ERR_PTR(-ENOMEM);
  488. phy = phy_create(dev, ops, init_data);
  489. if (!IS_ERR(phy)) {
  490. *ptr = phy;
  491. devres_add(dev, ptr);
  492. } else {
  493. devres_free(ptr);
  494. }
  495. return phy;
  496. }
  497. EXPORT_SYMBOL_GPL(devm_phy_create);
  498. /**
  499. * phy_destroy() - destroy the phy
  500. * @phy: the phy to be destroyed
  501. *
  502. * Called to destroy the phy.
  503. */
  504. void phy_destroy(struct phy *phy)
  505. {
  506. pm_runtime_disable(&phy->dev);
  507. device_unregister(&phy->dev);
  508. }
  509. EXPORT_SYMBOL_GPL(phy_destroy);
  510. /**
  511. * devm_phy_destroy() - destroy the PHY
  512. * @dev: device that wants to release this phy
  513. * @phy: the phy returned by devm_phy_get()
  514. *
  515. * destroys the devres associated with this phy and invokes phy_destroy
  516. * to destroy the phy.
  517. */
  518. void devm_phy_destroy(struct device *dev, struct phy *phy)
  519. {
  520. int r;
  521. r = devres_destroy(dev, devm_phy_consume, devm_phy_match, phy);
  522. dev_WARN_ONCE(dev, r, "couldn't find PHY resource\n");
  523. }
  524. EXPORT_SYMBOL_GPL(devm_phy_destroy);
  525. /**
  526. * __of_phy_provider_register() - create/register phy provider with the framework
  527. * @dev: struct device of the phy provider
  528. * @owner: the module owner containing of_xlate
  529. * @of_xlate: function pointer to obtain phy instance from phy provider
  530. *
  531. * Creates struct phy_provider from dev and of_xlate function pointer.
  532. * This is used in the case of dt boot for finding the phy instance from
  533. * phy provider.
  534. */
  535. struct phy_provider *__of_phy_provider_register(struct device *dev,
  536. struct module *owner, struct phy * (*of_xlate)(struct device *dev,
  537. struct of_phandle_args *args))
  538. {
  539. struct phy_provider *phy_provider;
  540. phy_provider = kzalloc(sizeof(*phy_provider), GFP_KERNEL);
  541. if (!phy_provider)
  542. return ERR_PTR(-ENOMEM);
  543. phy_provider->dev = dev;
  544. phy_provider->owner = owner;
  545. phy_provider->of_xlate = of_xlate;
  546. mutex_lock(&phy_provider_mutex);
  547. list_add_tail(&phy_provider->list, &phy_provider_list);
  548. mutex_unlock(&phy_provider_mutex);
  549. return phy_provider;
  550. }
  551. EXPORT_SYMBOL_GPL(__of_phy_provider_register);
  552. /**
  553. * __devm_of_phy_provider_register() - create/register phy provider with the
  554. * framework
  555. * @dev: struct device of the phy provider
  556. * @owner: the module owner containing of_xlate
  557. * @of_xlate: function pointer to obtain phy instance from phy provider
  558. *
  559. * Creates struct phy_provider from dev and of_xlate function pointer.
  560. * This is used in the case of dt boot for finding the phy instance from
  561. * phy provider. While at that, it also associates the device with the
  562. * phy provider using devres. On driver detach, release function is invoked
  563. * on the devres data, then, devres data is freed.
  564. */
  565. struct phy_provider *__devm_of_phy_provider_register(struct device *dev,
  566. struct module *owner, struct phy * (*of_xlate)(struct device *dev,
  567. struct of_phandle_args *args))
  568. {
  569. struct phy_provider **ptr, *phy_provider;
  570. ptr = devres_alloc(devm_phy_provider_release, sizeof(*ptr), GFP_KERNEL);
  571. if (!ptr)
  572. return ERR_PTR(-ENOMEM);
  573. phy_provider = __of_phy_provider_register(dev, owner, of_xlate);
  574. if (!IS_ERR(phy_provider)) {
  575. *ptr = phy_provider;
  576. devres_add(dev, ptr);
  577. } else {
  578. devres_free(ptr);
  579. }
  580. return phy_provider;
  581. }
  582. EXPORT_SYMBOL_GPL(__devm_of_phy_provider_register);
  583. /**
  584. * of_phy_provider_unregister() - unregister phy provider from the framework
  585. * @phy_provider: phy provider returned by of_phy_provider_register()
  586. *
  587. * Removes the phy_provider created using of_phy_provider_register().
  588. */
  589. void of_phy_provider_unregister(struct phy_provider *phy_provider)
  590. {
  591. if (IS_ERR(phy_provider))
  592. return;
  593. mutex_lock(&phy_provider_mutex);
  594. list_del(&phy_provider->list);
  595. kfree(phy_provider);
  596. mutex_unlock(&phy_provider_mutex);
  597. }
  598. EXPORT_SYMBOL_GPL(of_phy_provider_unregister);
  599. /**
  600. * devm_of_phy_provider_unregister() - remove phy provider from the framework
  601. * @dev: struct device of the phy provider
  602. *
  603. * destroys the devres associated with this phy provider and invokes
  604. * of_phy_provider_unregister to unregister the phy provider.
  605. */
  606. void devm_of_phy_provider_unregister(struct device *dev,
  607. struct phy_provider *phy_provider) {
  608. int r;
  609. r = devres_destroy(dev, devm_phy_provider_release, devm_phy_match,
  610. phy_provider);
  611. dev_WARN_ONCE(dev, r, "couldn't find PHY provider device resource\n");
  612. }
  613. EXPORT_SYMBOL_GPL(devm_of_phy_provider_unregister);
  614. /**
  615. * phy_release() - release the phy
  616. * @dev: the dev member within phy
  617. *
  618. * When the last reference to the device is removed, it is called
  619. * from the embedded kobject as release method.
  620. */
  621. static void phy_release(struct device *dev)
  622. {
  623. struct phy *phy;
  624. phy = to_phy(dev);
  625. dev_vdbg(dev, "releasing '%s'\n", dev_name(dev));
  626. ida_remove(&phy_ida, phy->id);
  627. kfree(phy);
  628. }
  629. static int __init phy_core_init(void)
  630. {
  631. phy_class = class_create(THIS_MODULE, "phy");
  632. if (IS_ERR(phy_class)) {
  633. pr_err("failed to create phy class --> %ld\n",
  634. PTR_ERR(phy_class));
  635. return PTR_ERR(phy_class);
  636. }
  637. phy_class->dev_release = phy_release;
  638. return 0;
  639. }
  640. module_init(phy_core_init);
  641. static void __exit phy_core_exit(void)
  642. {
  643. class_destroy(phy_class);
  644. }
  645. module_exit(phy_core_exit);
  646. MODULE_DESCRIPTION("Generic PHY Framework");
  647. MODULE_AUTHOR("Kishon Vijay Abraham I <kishon@ti.com>");
  648. MODULE_LICENSE("GPL v2");