phy-core.c 22 KB

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