phy-core.c 22 KB

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