phy-core.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026
  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 <[email protected]>
  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->children, 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 = 0;
  226. if (!phy)
  227. goto out;
  228. if (phy->pwr) {
  229. ret = regulator_enable(phy->pwr);
  230. if (ret)
  231. goto out;
  232. }
  233. ret = phy_pm_runtime_get_sync(phy);
  234. if (ret < 0 && ret != -ENOTSUPP)
  235. goto err_pm_sync;
  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 err_pwr_on;
  243. }
  244. }
  245. ++phy->power_count;
  246. mutex_unlock(&phy->mutex);
  247. return 0;
  248. err_pwr_on:
  249. mutex_unlock(&phy->mutex);
  250. phy_pm_runtime_put_sync(phy);
  251. err_pm_sync:
  252. if (phy->pwr)
  253. regulator_disable(phy->pwr);
  254. out:
  255. return ret;
  256. }
  257. EXPORT_SYMBOL_GPL(phy_power_on);
  258. int phy_power_off(struct phy *phy)
  259. {
  260. int ret;
  261. if (!phy)
  262. return 0;
  263. mutex_lock(&phy->mutex);
  264. if (phy->power_count == 1 && phy->ops->power_off) {
  265. ret = phy->ops->power_off(phy);
  266. if (ret < 0) {
  267. dev_err(&phy->dev, "phy poweroff failed --> %d\n", ret);
  268. mutex_unlock(&phy->mutex);
  269. return ret;
  270. }
  271. }
  272. --phy->power_count;
  273. mutex_unlock(&phy->mutex);
  274. phy_pm_runtime_put(phy);
  275. if (phy->pwr)
  276. regulator_disable(phy->pwr);
  277. return 0;
  278. }
  279. EXPORT_SYMBOL_GPL(phy_power_off);
  280. int phy_set_mode(struct phy *phy, enum phy_mode mode)
  281. {
  282. int ret;
  283. if (!phy || !phy->ops->set_mode)
  284. return 0;
  285. mutex_lock(&phy->mutex);
  286. ret = phy->ops->set_mode(phy, mode);
  287. mutex_unlock(&phy->mutex);
  288. return ret;
  289. }
  290. EXPORT_SYMBOL_GPL(phy_set_mode);
  291. int phy_reset(struct phy *phy)
  292. {
  293. int ret;
  294. if (!phy || !phy->ops->reset)
  295. return 0;
  296. mutex_lock(&phy->mutex);
  297. ret = phy->ops->reset(phy);
  298. mutex_unlock(&phy->mutex);
  299. return ret;
  300. }
  301. EXPORT_SYMBOL_GPL(phy_reset);
  302. /**
  303. * _of_phy_get() - lookup and obtain a reference to a phy by phandle
  304. * @np: device_node for which to get the phy
  305. * @index: the index of the phy
  306. *
  307. * Returns the phy associated with the given phandle value,
  308. * after getting a refcount to it or -ENODEV if there is no such phy or
  309. * -EPROBE_DEFER if there is a phandle to the phy, but the device is
  310. * not yet loaded. This function uses of_xlate call back function provided
  311. * while registering the phy_provider to find the phy instance.
  312. */
  313. static struct phy *_of_phy_get(struct device_node *np, int index)
  314. {
  315. int ret;
  316. struct phy_provider *phy_provider;
  317. struct phy *phy = NULL;
  318. struct of_phandle_args args;
  319. ret = of_parse_phandle_with_args(np, "phys", "#phy-cells",
  320. index, &args);
  321. if (ret)
  322. return ERR_PTR(-ENODEV);
  323. /* This phy type handled by the usb-phy subsystem for now */
  324. if (of_device_is_compatible(args.np, "usb-nop-xceiv"))
  325. return ERR_PTR(-ENODEV);
  326. mutex_lock(&phy_provider_mutex);
  327. phy_provider = of_phy_provider_lookup(args.np);
  328. if (IS_ERR(phy_provider) || !try_module_get(phy_provider->owner)) {
  329. phy = ERR_PTR(-EPROBE_DEFER);
  330. goto out_unlock;
  331. }
  332. if (!of_device_is_available(args.np)) {
  333. dev_warn(phy_provider->dev, "Requested PHY is disabled\n");
  334. phy = ERR_PTR(-ENODEV);
  335. goto out_put_module;
  336. }
  337. phy = phy_provider->of_xlate(phy_provider->dev, &args);
  338. out_put_module:
  339. module_put(phy_provider->owner);
  340. out_unlock:
  341. mutex_unlock(&phy_provider_mutex);
  342. of_node_put(args.np);
  343. return phy;
  344. }
  345. /**
  346. * of_phy_get() - lookup and obtain a reference to a phy using a device_node.
  347. * @np: device_node for which to get the phy
  348. * @con_id: name of the phy from device's point of view
  349. *
  350. * Returns the phy driver, after getting a refcount to it; or
  351. * -ENODEV if there is no such phy. The caller is responsible for
  352. * calling phy_put() to release that count.
  353. */
  354. struct phy *of_phy_get(struct device_node *np, const char *con_id)
  355. {
  356. struct phy *phy = NULL;
  357. int index = 0;
  358. if (con_id)
  359. index = of_property_match_string(np, "phy-names", con_id);
  360. phy = _of_phy_get(np, index);
  361. if (IS_ERR(phy))
  362. return phy;
  363. if (!try_module_get(phy->ops->owner))
  364. return ERR_PTR(-EPROBE_DEFER);
  365. get_device(&phy->dev);
  366. return phy;
  367. }
  368. EXPORT_SYMBOL_GPL(of_phy_get);
  369. /**
  370. * phy_put() - release the PHY
  371. * @phy: the phy returned by phy_get()
  372. *
  373. * Releases a refcount the caller received from phy_get().
  374. */
  375. void phy_put(struct phy *phy)
  376. {
  377. if (!phy || IS_ERR(phy))
  378. return;
  379. module_put(phy->ops->owner);
  380. put_device(&phy->dev);
  381. }
  382. EXPORT_SYMBOL_GPL(phy_put);
  383. /**
  384. * devm_phy_put() - release the PHY
  385. * @dev: device that wants to release this phy
  386. * @phy: the phy returned by devm_phy_get()
  387. *
  388. * destroys the devres associated with this phy and invokes phy_put
  389. * to release the phy.
  390. */
  391. void devm_phy_put(struct device *dev, struct phy *phy)
  392. {
  393. int r;
  394. if (!phy)
  395. return;
  396. r = devres_destroy(dev, devm_phy_release, devm_phy_match, phy);
  397. dev_WARN_ONCE(dev, r, "couldn't find PHY resource\n");
  398. }
  399. EXPORT_SYMBOL_GPL(devm_phy_put);
  400. /**
  401. * of_phy_simple_xlate() - returns the phy instance from phy provider
  402. * @dev: the PHY provider device
  403. * @args: of_phandle_args (not used here)
  404. *
  405. * Intended to be used by phy provider for the common case where #phy-cells is
  406. * 0. For other cases where #phy-cells is greater than '0', the phy provider
  407. * should provide a custom of_xlate function that reads the *args* and returns
  408. * the appropriate phy.
  409. */
  410. struct phy *of_phy_simple_xlate(struct device *dev, struct of_phandle_args
  411. *args)
  412. {
  413. struct phy *phy;
  414. struct class_dev_iter iter;
  415. class_dev_iter_init(&iter, phy_class, NULL, NULL);
  416. while ((dev = class_dev_iter_next(&iter))) {
  417. phy = to_phy(dev);
  418. if (args->np != phy->dev.of_node)
  419. continue;
  420. class_dev_iter_exit(&iter);
  421. return phy;
  422. }
  423. class_dev_iter_exit(&iter);
  424. return ERR_PTR(-ENODEV);
  425. }
  426. EXPORT_SYMBOL_GPL(of_phy_simple_xlate);
  427. /**
  428. * phy_get() - lookup and obtain a reference to a 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. * -ENODEV if there is no such phy. The caller is responsible for
  435. * calling phy_put() to release that count.
  436. */
  437. struct phy *phy_get(struct device *dev, const char *string)
  438. {
  439. int index = 0;
  440. struct phy *phy;
  441. if (string == NULL) {
  442. dev_WARN(dev, "missing string\n");
  443. return ERR_PTR(-EINVAL);
  444. }
  445. if (dev->of_node) {
  446. index = of_property_match_string(dev->of_node, "phy-names",
  447. string);
  448. phy = _of_phy_get(dev->of_node, index);
  449. } else {
  450. phy = phy_find(dev, string);
  451. }
  452. if (IS_ERR(phy))
  453. return phy;
  454. if (!try_module_get(phy->ops->owner))
  455. return ERR_PTR(-EPROBE_DEFER);
  456. get_device(&phy->dev);
  457. return phy;
  458. }
  459. EXPORT_SYMBOL_GPL(phy_get);
  460. /**
  461. * phy_optional_get() - lookup and obtain a reference to an optional phy.
  462. * @dev: device that requests this phy
  463. * @string: the phy name as given in the dt data or the name of the controller
  464. * port for non-dt case
  465. *
  466. * Returns the phy driver, after getting a refcount to it; or
  467. * NULL if there is no such phy. The caller is responsible for
  468. * calling phy_put() to release that count.
  469. */
  470. struct phy *phy_optional_get(struct device *dev, const char *string)
  471. {
  472. struct phy *phy = phy_get(dev, string);
  473. if (IS_ERR(phy) && (PTR_ERR(phy) == -ENODEV))
  474. phy = NULL;
  475. return phy;
  476. }
  477. EXPORT_SYMBOL_GPL(phy_optional_get);
  478. /**
  479. * devm_phy_get() - lookup and obtain a reference to a phy.
  480. * @dev: device that requests this phy
  481. * @string: the phy name as given in the dt data or phy device name
  482. * for non-dt case
  483. *
  484. * Gets the phy using phy_get(), and associates a device with it using
  485. * devres. On driver detach, release function is invoked on the devres data,
  486. * then, devres data is freed.
  487. */
  488. struct phy *devm_phy_get(struct device *dev, const char *string)
  489. {
  490. struct phy **ptr, *phy;
  491. ptr = devres_alloc(devm_phy_release, sizeof(*ptr), GFP_KERNEL);
  492. if (!ptr)
  493. return ERR_PTR(-ENOMEM);
  494. phy = phy_get(dev, string);
  495. if (!IS_ERR(phy)) {
  496. *ptr = phy;
  497. devres_add(dev, ptr);
  498. } else {
  499. devres_free(ptr);
  500. }
  501. return phy;
  502. }
  503. EXPORT_SYMBOL_GPL(devm_phy_get);
  504. /**
  505. * devm_phy_optional_get() - lookup and obtain a reference to an optional phy.
  506. * @dev: device that requests this phy
  507. * @string: the phy name as given in the dt data or phy device name
  508. * for non-dt case
  509. *
  510. * Gets the phy using phy_get(), and associates a device with it using
  511. * devres. On driver detach, release function is invoked on the devres
  512. * data, then, devres data is freed. This differs to devm_phy_get() in
  513. * that if the phy does not exist, it is not considered an error and
  514. * -ENODEV will not be returned. Instead the NULL phy is returned,
  515. * which can be passed to all other phy consumer calls.
  516. */
  517. struct phy *devm_phy_optional_get(struct device *dev, const char *string)
  518. {
  519. struct phy *phy = devm_phy_get(dev, string);
  520. if (IS_ERR(phy) && (PTR_ERR(phy) == -ENODEV))
  521. phy = NULL;
  522. return phy;
  523. }
  524. EXPORT_SYMBOL_GPL(devm_phy_optional_get);
  525. /**
  526. * devm_of_phy_get() - lookup and obtain a reference to a phy.
  527. * @dev: device that requests this phy
  528. * @np: node containing the phy
  529. * @con_id: name of the phy from device's point of view
  530. *
  531. * Gets the phy using of_phy_get(), and associates a device with it using
  532. * devres. On driver detach, release function is invoked on the devres data,
  533. * then, devres data is freed.
  534. */
  535. struct phy *devm_of_phy_get(struct device *dev, struct device_node *np,
  536. const char *con_id)
  537. {
  538. struct phy **ptr, *phy;
  539. ptr = devres_alloc(devm_phy_release, sizeof(*ptr), GFP_KERNEL);
  540. if (!ptr)
  541. return ERR_PTR(-ENOMEM);
  542. phy = of_phy_get(np, con_id);
  543. if (!IS_ERR(phy)) {
  544. *ptr = phy;
  545. devres_add(dev, ptr);
  546. } else {
  547. devres_free(ptr);
  548. }
  549. return phy;
  550. }
  551. EXPORT_SYMBOL_GPL(devm_of_phy_get);
  552. /**
  553. * devm_of_phy_get_by_index() - lookup and obtain a reference to a phy by index.
  554. * @dev: device that requests this phy
  555. * @np: node containing the phy
  556. * @index: index of the phy
  557. *
  558. * Gets the phy using _of_phy_get(), then gets a refcount to it,
  559. * and associates a device with it using devres. On driver detach,
  560. * release function is invoked on the devres data,
  561. * then, devres data is freed.
  562. *
  563. */
  564. struct phy *devm_of_phy_get_by_index(struct device *dev, struct device_node *np,
  565. int index)
  566. {
  567. struct phy **ptr, *phy;
  568. ptr = devres_alloc(devm_phy_release, sizeof(*ptr), GFP_KERNEL);
  569. if (!ptr)
  570. return ERR_PTR(-ENOMEM);
  571. phy = _of_phy_get(np, index);
  572. if (IS_ERR(phy)) {
  573. devres_free(ptr);
  574. return phy;
  575. }
  576. if (!try_module_get(phy->ops->owner)) {
  577. devres_free(ptr);
  578. return ERR_PTR(-EPROBE_DEFER);
  579. }
  580. get_device(&phy->dev);
  581. *ptr = phy;
  582. devres_add(dev, ptr);
  583. return phy;
  584. }
  585. EXPORT_SYMBOL_GPL(devm_of_phy_get_by_index);
  586. /**
  587. * phy_create() - create a new phy
  588. * @dev: device that is creating the new phy
  589. * @node: device node of the phy
  590. * @ops: function pointers for performing phy operations
  591. *
  592. * Called to create a phy using phy framework.
  593. */
  594. struct phy *phy_create(struct device *dev, struct device_node *node,
  595. const struct phy_ops *ops)
  596. {
  597. int ret;
  598. int id;
  599. struct phy *phy;
  600. if (WARN_ON(!dev))
  601. return ERR_PTR(-EINVAL);
  602. phy = kzalloc(sizeof(*phy), GFP_KERNEL);
  603. if (!phy)
  604. return ERR_PTR(-ENOMEM);
  605. id = ida_simple_get(&phy_ida, 0, 0, GFP_KERNEL);
  606. if (id < 0) {
  607. dev_err(dev, "unable to get id\n");
  608. ret = id;
  609. goto free_phy;
  610. }
  611. device_initialize(&phy->dev);
  612. mutex_init(&phy->mutex);
  613. phy->dev.class = phy_class;
  614. phy->dev.parent = dev;
  615. phy->dev.of_node = node ?: dev->of_node;
  616. phy->id = id;
  617. phy->ops = ops;
  618. ret = dev_set_name(&phy->dev, "phy-%s.%d", dev_name(dev), id);
  619. if (ret)
  620. goto put_dev;
  621. /* phy-supply */
  622. phy->pwr = regulator_get_optional(&phy->dev, "phy");
  623. if (IS_ERR(phy->pwr)) {
  624. ret = PTR_ERR(phy->pwr);
  625. if (ret == -EPROBE_DEFER)
  626. goto put_dev;
  627. phy->pwr = NULL;
  628. }
  629. ret = device_add(&phy->dev);
  630. if (ret)
  631. goto put_dev;
  632. if (pm_runtime_enabled(dev)) {
  633. pm_runtime_enable(&phy->dev);
  634. pm_runtime_no_callbacks(&phy->dev);
  635. }
  636. return phy;
  637. put_dev:
  638. put_device(&phy->dev); /* calls phy_release() which frees resources */
  639. return ERR_PTR(ret);
  640. free_phy:
  641. kfree(phy);
  642. return ERR_PTR(ret);
  643. }
  644. EXPORT_SYMBOL_GPL(phy_create);
  645. /**
  646. * devm_phy_create() - create a new phy
  647. * @dev: device that is creating the new phy
  648. * @node: device node of the phy
  649. * @ops: function pointers for performing phy operations
  650. *
  651. * Creates a new PHY device adding it to the PHY class.
  652. * While at that, it also associates the device with the phy using devres.
  653. * On driver detach, release function is invoked on the devres data,
  654. * then, devres data is freed.
  655. */
  656. struct phy *devm_phy_create(struct device *dev, struct device_node *node,
  657. const struct phy_ops *ops)
  658. {
  659. struct phy **ptr, *phy;
  660. ptr = devres_alloc(devm_phy_consume, sizeof(*ptr), GFP_KERNEL);
  661. if (!ptr)
  662. return ERR_PTR(-ENOMEM);
  663. phy = phy_create(dev, node, ops);
  664. if (!IS_ERR(phy)) {
  665. *ptr = phy;
  666. devres_add(dev, ptr);
  667. } else {
  668. devres_free(ptr);
  669. }
  670. return phy;
  671. }
  672. EXPORT_SYMBOL_GPL(devm_phy_create);
  673. /**
  674. * phy_destroy() - destroy the phy
  675. * @phy: the phy to be destroyed
  676. *
  677. * Called to destroy the phy.
  678. */
  679. void phy_destroy(struct phy *phy)
  680. {
  681. pm_runtime_disable(&phy->dev);
  682. device_unregister(&phy->dev);
  683. }
  684. EXPORT_SYMBOL_GPL(phy_destroy);
  685. /**
  686. * devm_phy_destroy() - destroy the PHY
  687. * @dev: device that wants to release this phy
  688. * @phy: the phy returned by devm_phy_get()
  689. *
  690. * destroys the devres associated with this phy and invokes phy_destroy
  691. * to destroy the phy.
  692. */
  693. void devm_phy_destroy(struct device *dev, struct phy *phy)
  694. {
  695. int r;
  696. r = devres_destroy(dev, devm_phy_consume, devm_phy_match, phy);
  697. dev_WARN_ONCE(dev, r, "couldn't find PHY resource\n");
  698. }
  699. EXPORT_SYMBOL_GPL(devm_phy_destroy);
  700. /**
  701. * __of_phy_provider_register() - create/register phy provider with the framework
  702. * @dev: struct device of the phy provider
  703. * @children: device node containing children (if different from dev->of_node)
  704. * @owner: the module owner containing of_xlate
  705. * @of_xlate: function pointer to obtain phy instance from phy provider
  706. *
  707. * Creates struct phy_provider from dev and of_xlate function pointer.
  708. * This is used in the case of dt boot for finding the phy instance from
  709. * phy provider.
  710. *
  711. * If the PHY provider doesn't nest children directly but uses a separate
  712. * child node to contain the individual children, the @children parameter
  713. * can be used to override the default. If NULL, the default (dev->of_node)
  714. * will be used. If non-NULL, the device node must be a child (or further
  715. * descendant) of dev->of_node. Otherwise an ERR_PTR()-encoded -EINVAL
  716. * error code is returned.
  717. */
  718. struct phy_provider *__of_phy_provider_register(struct device *dev,
  719. struct device_node *children, struct module *owner,
  720. struct phy * (*of_xlate)(struct device *dev,
  721. struct of_phandle_args *args))
  722. {
  723. struct phy_provider *phy_provider;
  724. /*
  725. * If specified, the device node containing the children must itself
  726. * be the provider's device node or a child (or further descendant)
  727. * thereof.
  728. */
  729. if (children) {
  730. struct device_node *parent = of_node_get(children), *next;
  731. while (parent) {
  732. if (parent == dev->of_node)
  733. break;
  734. next = of_get_parent(parent);
  735. of_node_put(parent);
  736. parent = next;
  737. }
  738. if (!parent)
  739. return ERR_PTR(-EINVAL);
  740. of_node_put(parent);
  741. } else {
  742. children = dev->of_node;
  743. }
  744. phy_provider = kzalloc(sizeof(*phy_provider), GFP_KERNEL);
  745. if (!phy_provider)
  746. return ERR_PTR(-ENOMEM);
  747. phy_provider->dev = dev;
  748. phy_provider->children = of_node_get(children);
  749. phy_provider->owner = owner;
  750. phy_provider->of_xlate = of_xlate;
  751. mutex_lock(&phy_provider_mutex);
  752. list_add_tail(&phy_provider->list, &phy_provider_list);
  753. mutex_unlock(&phy_provider_mutex);
  754. return phy_provider;
  755. }
  756. EXPORT_SYMBOL_GPL(__of_phy_provider_register);
  757. /**
  758. * __devm_of_phy_provider_register() - create/register phy provider with the
  759. * framework
  760. * @dev: struct device of the phy provider
  761. * @owner: the module owner containing of_xlate
  762. * @of_xlate: function pointer to obtain phy instance from phy provider
  763. *
  764. * Creates struct phy_provider from dev and of_xlate function pointer.
  765. * This is used in the case of dt boot for finding the phy instance from
  766. * phy provider. While at that, it also associates the device with the
  767. * phy provider using devres. On driver detach, release function is invoked
  768. * on the devres data, then, devres data is freed.
  769. */
  770. struct phy_provider *__devm_of_phy_provider_register(struct device *dev,
  771. struct device_node *children, struct module *owner,
  772. struct phy * (*of_xlate)(struct device *dev,
  773. struct of_phandle_args *args))
  774. {
  775. struct phy_provider **ptr, *phy_provider;
  776. ptr = devres_alloc(devm_phy_provider_release, sizeof(*ptr), GFP_KERNEL);
  777. if (!ptr)
  778. return ERR_PTR(-ENOMEM);
  779. phy_provider = __of_phy_provider_register(dev, children, owner,
  780. of_xlate);
  781. if (!IS_ERR(phy_provider)) {
  782. *ptr = phy_provider;
  783. devres_add(dev, ptr);
  784. } else {
  785. devres_free(ptr);
  786. }
  787. return phy_provider;
  788. }
  789. EXPORT_SYMBOL_GPL(__devm_of_phy_provider_register);
  790. /**
  791. * of_phy_provider_unregister() - unregister phy provider from the framework
  792. * @phy_provider: phy provider returned by of_phy_provider_register()
  793. *
  794. * Removes the phy_provider created using of_phy_provider_register().
  795. */
  796. void of_phy_provider_unregister(struct phy_provider *phy_provider)
  797. {
  798. if (IS_ERR(phy_provider))
  799. return;
  800. mutex_lock(&phy_provider_mutex);
  801. list_del(&phy_provider->list);
  802. of_node_put(phy_provider->children);
  803. kfree(phy_provider);
  804. mutex_unlock(&phy_provider_mutex);
  805. }
  806. EXPORT_SYMBOL_GPL(of_phy_provider_unregister);
  807. /**
  808. * devm_of_phy_provider_unregister() - remove phy provider from the framework
  809. * @dev: struct device of the phy provider
  810. *
  811. * destroys the devres associated with this phy provider and invokes
  812. * of_phy_provider_unregister to unregister the phy provider.
  813. */
  814. void devm_of_phy_provider_unregister(struct device *dev,
  815. struct phy_provider *phy_provider) {
  816. int r;
  817. r = devres_destroy(dev, devm_phy_provider_release, devm_phy_match,
  818. phy_provider);
  819. dev_WARN_ONCE(dev, r, "couldn't find PHY provider device resource\n");
  820. }
  821. EXPORT_SYMBOL_GPL(devm_of_phy_provider_unregister);
  822. /**
  823. * phy_release() - release the phy
  824. * @dev: the dev member within phy
  825. *
  826. * When the last reference to the device is removed, it is called
  827. * from the embedded kobject as release method.
  828. */
  829. static void phy_release(struct device *dev)
  830. {
  831. struct phy *phy;
  832. phy = to_phy(dev);
  833. dev_vdbg(dev, "releasing '%s'\n", dev_name(dev));
  834. regulator_put(phy->pwr);
  835. ida_simple_remove(&phy_ida, phy->id);
  836. kfree(phy);
  837. }
  838. static int __init phy_core_init(void)
  839. {
  840. phy_class = class_create(THIS_MODULE, "phy");
  841. if (IS_ERR(phy_class)) {
  842. pr_err("failed to create phy class --> %ld\n",
  843. PTR_ERR(phy_class));
  844. return PTR_ERR(phy_class);
  845. }
  846. phy_class->dev_release = phy_release;
  847. return 0;
  848. }
  849. module_init(phy_core_init);
  850. static void __exit phy_core_exit(void)
  851. {
  852. class_destroy(phy_class);
  853. }
  854. module_exit(phy_core_exit);
  855. MODULE_DESCRIPTION("Generic PHY Framework");
  856. MODULE_AUTHOR("Kishon Vijay Abraham I <[email protected]>");
  857. MODULE_LICENSE("GPL v2");