phy-core.c 21 KB

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