phy-core.c 25 KB

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