phy-core.c 20 KB

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