phy-core.c 27 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136
  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 (!phy)
  129. return 0;
  130. if (!pm_runtime_enabled(&phy->dev))
  131. return -ENOTSUPP;
  132. ret = pm_runtime_get(&phy->dev);
  133. if (ret < 0 && ret != -EINPROGRESS)
  134. pm_runtime_put_noidle(&phy->dev);
  135. return ret;
  136. }
  137. EXPORT_SYMBOL_GPL(phy_pm_runtime_get);
  138. int phy_pm_runtime_get_sync(struct phy *phy)
  139. {
  140. int ret;
  141. if (!phy)
  142. return 0;
  143. if (!pm_runtime_enabled(&phy->dev))
  144. return -ENOTSUPP;
  145. ret = pm_runtime_get_sync(&phy->dev);
  146. if (ret < 0)
  147. pm_runtime_put_sync(&phy->dev);
  148. return ret;
  149. }
  150. EXPORT_SYMBOL_GPL(phy_pm_runtime_get_sync);
  151. int phy_pm_runtime_put(struct phy *phy)
  152. {
  153. if (!phy)
  154. return 0;
  155. if (!pm_runtime_enabled(&phy->dev))
  156. return -ENOTSUPP;
  157. return pm_runtime_put(&phy->dev);
  158. }
  159. EXPORT_SYMBOL_GPL(phy_pm_runtime_put);
  160. int phy_pm_runtime_put_sync(struct phy *phy)
  161. {
  162. if (!phy)
  163. return 0;
  164. if (!pm_runtime_enabled(&phy->dev))
  165. return -ENOTSUPP;
  166. return pm_runtime_put_sync(&phy->dev);
  167. }
  168. EXPORT_SYMBOL_GPL(phy_pm_runtime_put_sync);
  169. void phy_pm_runtime_allow(struct phy *phy)
  170. {
  171. if (!phy)
  172. return;
  173. if (!pm_runtime_enabled(&phy->dev))
  174. return;
  175. pm_runtime_allow(&phy->dev);
  176. }
  177. EXPORT_SYMBOL_GPL(phy_pm_runtime_allow);
  178. void phy_pm_runtime_forbid(struct phy *phy)
  179. {
  180. if (!phy)
  181. return;
  182. if (!pm_runtime_enabled(&phy->dev))
  183. return;
  184. pm_runtime_forbid(&phy->dev);
  185. }
  186. EXPORT_SYMBOL_GPL(phy_pm_runtime_forbid);
  187. int phy_init(struct phy *phy)
  188. {
  189. int ret;
  190. if (!phy)
  191. return 0;
  192. ret = phy_pm_runtime_get_sync(phy);
  193. if (ret < 0 && ret != -ENOTSUPP)
  194. return ret;
  195. ret = 0; /* Override possible ret == -ENOTSUPP */
  196. mutex_lock(&phy->mutex);
  197. if (phy->init_count == 0 && phy->ops->init) {
  198. ret = phy->ops->init(phy);
  199. if (ret < 0) {
  200. dev_err(&phy->dev, "phy init failed --> %d\n", ret);
  201. goto out;
  202. }
  203. }
  204. ++phy->init_count;
  205. out:
  206. mutex_unlock(&phy->mutex);
  207. phy_pm_runtime_put(phy);
  208. return ret;
  209. }
  210. EXPORT_SYMBOL_GPL(phy_init);
  211. int phy_exit(struct phy *phy)
  212. {
  213. int ret;
  214. if (!phy)
  215. return 0;
  216. ret = phy_pm_runtime_get_sync(phy);
  217. if (ret < 0 && ret != -ENOTSUPP)
  218. return ret;
  219. ret = 0; /* Override possible ret == -ENOTSUPP */
  220. mutex_lock(&phy->mutex);
  221. if (phy->init_count == 1 && phy->ops->exit) {
  222. ret = phy->ops->exit(phy);
  223. if (ret < 0) {
  224. dev_err(&phy->dev, "phy exit failed --> %d\n", ret);
  225. goto out;
  226. }
  227. }
  228. --phy->init_count;
  229. out:
  230. mutex_unlock(&phy->mutex);
  231. phy_pm_runtime_put(phy);
  232. return ret;
  233. }
  234. EXPORT_SYMBOL_GPL(phy_exit);
  235. int phy_power_on(struct phy *phy)
  236. {
  237. int ret = 0;
  238. if (!phy)
  239. goto out;
  240. if (phy->pwr) {
  241. ret = regulator_enable(phy->pwr);
  242. if (ret)
  243. goto out;
  244. }
  245. ret = phy_pm_runtime_get_sync(phy);
  246. if (ret < 0 && ret != -ENOTSUPP)
  247. goto err_pm_sync;
  248. ret = 0; /* Override possible ret == -ENOTSUPP */
  249. mutex_lock(&phy->mutex);
  250. if (phy->power_count == 0 && phy->ops->power_on) {
  251. ret = phy->ops->power_on(phy);
  252. if (ret < 0) {
  253. dev_err(&phy->dev, "phy poweron failed --> %d\n", ret);
  254. goto err_pwr_on;
  255. }
  256. }
  257. ++phy->power_count;
  258. mutex_unlock(&phy->mutex);
  259. return 0;
  260. err_pwr_on:
  261. mutex_unlock(&phy->mutex);
  262. phy_pm_runtime_put_sync(phy);
  263. err_pm_sync:
  264. if (phy->pwr)
  265. regulator_disable(phy->pwr);
  266. out:
  267. return ret;
  268. }
  269. EXPORT_SYMBOL_GPL(phy_power_on);
  270. int phy_power_off(struct phy *phy)
  271. {
  272. int ret;
  273. if (!phy)
  274. return 0;
  275. mutex_lock(&phy->mutex);
  276. if (phy->power_count == 1 && phy->ops->power_off) {
  277. ret = phy->ops->power_off(phy);
  278. if (ret < 0) {
  279. dev_err(&phy->dev, "phy poweroff failed --> %d\n", ret);
  280. mutex_unlock(&phy->mutex);
  281. return ret;
  282. }
  283. }
  284. --phy->power_count;
  285. mutex_unlock(&phy->mutex);
  286. phy_pm_runtime_put(phy);
  287. if (phy->pwr)
  288. regulator_disable(phy->pwr);
  289. return 0;
  290. }
  291. EXPORT_SYMBOL_GPL(phy_power_off);
  292. int phy_set_mode_ext(struct phy *phy, enum phy_mode mode, int submode)
  293. {
  294. int ret;
  295. if (!phy || !phy->ops->set_mode)
  296. return 0;
  297. mutex_lock(&phy->mutex);
  298. ret = phy->ops->set_mode(phy, mode, submode);
  299. if (!ret)
  300. phy->attrs.mode = mode;
  301. mutex_unlock(&phy->mutex);
  302. return ret;
  303. }
  304. EXPORT_SYMBOL_GPL(phy_set_mode_ext);
  305. int phy_reset(struct phy *phy)
  306. {
  307. int ret;
  308. if (!phy || !phy->ops->reset)
  309. return 0;
  310. ret = phy_pm_runtime_get_sync(phy);
  311. if (ret < 0 && ret != -ENOTSUPP)
  312. return ret;
  313. mutex_lock(&phy->mutex);
  314. ret = phy->ops->reset(phy);
  315. mutex_unlock(&phy->mutex);
  316. phy_pm_runtime_put(phy);
  317. return ret;
  318. }
  319. EXPORT_SYMBOL_GPL(phy_reset);
  320. int phy_calibrate(struct phy *phy)
  321. {
  322. int ret;
  323. if (!phy || !phy->ops->calibrate)
  324. return 0;
  325. mutex_lock(&phy->mutex);
  326. ret = phy->ops->calibrate(phy);
  327. mutex_unlock(&phy->mutex);
  328. return ret;
  329. }
  330. EXPORT_SYMBOL_GPL(phy_calibrate);
  331. /**
  332. * phy_configure() - Changes the phy parameters
  333. * @phy: the phy returned by phy_get()
  334. * @opts: New configuration to apply
  335. *
  336. * Used to change the PHY parameters. phy_init() must have been called
  337. * on the phy. The configuration will be applied on the current phy
  338. * mode, that can be changed using phy_set_mode().
  339. *
  340. * Returns: 0 if successful, an negative error code otherwise
  341. */
  342. int phy_configure(struct phy *phy, union phy_configure_opts *opts)
  343. {
  344. int ret;
  345. if (!phy)
  346. return -EINVAL;
  347. if (!phy->ops->configure)
  348. return -EOPNOTSUPP;
  349. mutex_lock(&phy->mutex);
  350. ret = phy->ops->configure(phy, opts);
  351. mutex_unlock(&phy->mutex);
  352. return ret;
  353. }
  354. EXPORT_SYMBOL_GPL(phy_configure);
  355. /**
  356. * phy_validate() - Checks the phy parameters
  357. * @phy: the phy returned by phy_get()
  358. * @mode: phy_mode the configuration is applicable to.
  359. * @submode: PHY submode the configuration is applicable to.
  360. * @opts: Configuration to check
  361. *
  362. * Used to check that the current set of parameters can be handled by
  363. * the phy. Implementations are free to tune the parameters passed as
  364. * arguments if needed by some implementation detail or
  365. * constraints. It will not change any actual configuration of the
  366. * PHY, so calling it as many times as deemed fit will have no side
  367. * effect.
  368. *
  369. * Returns: 0 if successful, an negative error code otherwise
  370. */
  371. int phy_validate(struct phy *phy, enum phy_mode mode, int submode,
  372. union phy_configure_opts *opts)
  373. {
  374. int ret;
  375. if (!phy)
  376. return -EINVAL;
  377. if (!phy->ops->validate)
  378. return -EOPNOTSUPP;
  379. mutex_lock(&phy->mutex);
  380. ret = phy->ops->validate(phy, mode, submode, opts);
  381. mutex_unlock(&phy->mutex);
  382. return ret;
  383. }
  384. EXPORT_SYMBOL_GPL(phy_validate);
  385. /**
  386. * _of_phy_get() - lookup and obtain a reference to a phy by phandle
  387. * @np: device_node for which to get the phy
  388. * @index: the index of the phy
  389. *
  390. * Returns the phy associated with the given phandle value,
  391. * after getting a refcount to it or -ENODEV if there is no such phy or
  392. * -EPROBE_DEFER if there is a phandle to the phy, but the device is
  393. * not yet loaded. This function uses of_xlate call back function provided
  394. * while registering the phy_provider to find the phy instance.
  395. */
  396. static struct phy *_of_phy_get(struct device_node *np, int index)
  397. {
  398. int ret;
  399. struct phy_provider *phy_provider;
  400. struct phy *phy = NULL;
  401. struct of_phandle_args args;
  402. ret = of_parse_phandle_with_args(np, "phys", "#phy-cells",
  403. index, &args);
  404. if (ret)
  405. return ERR_PTR(-ENODEV);
  406. /* This phy type handled by the usb-phy subsystem for now */
  407. if (of_device_is_compatible(args.np, "usb-nop-xceiv"))
  408. return ERR_PTR(-ENODEV);
  409. mutex_lock(&phy_provider_mutex);
  410. phy_provider = of_phy_provider_lookup(args.np);
  411. if (IS_ERR(phy_provider) || !try_module_get(phy_provider->owner)) {
  412. phy = ERR_PTR(-EPROBE_DEFER);
  413. goto out_unlock;
  414. }
  415. if (!of_device_is_available(args.np)) {
  416. dev_warn(phy_provider->dev, "Requested PHY is disabled\n");
  417. phy = ERR_PTR(-ENODEV);
  418. goto out_put_module;
  419. }
  420. phy = phy_provider->of_xlate(phy_provider->dev, &args);
  421. out_put_module:
  422. module_put(phy_provider->owner);
  423. out_unlock:
  424. mutex_unlock(&phy_provider_mutex);
  425. of_node_put(args.np);
  426. return phy;
  427. }
  428. /**
  429. * of_phy_get() - lookup and obtain a reference to a phy using a device_node.
  430. * @np: device_node for which to get the phy
  431. * @con_id: name of the phy from device's point of view
  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 *of_phy_get(struct device_node *np, const char *con_id)
  438. {
  439. struct phy *phy = NULL;
  440. int index = 0;
  441. if (con_id)
  442. index = of_property_match_string(np, "phy-names", con_id);
  443. phy = _of_phy_get(np, index);
  444. if (IS_ERR(phy))
  445. return phy;
  446. if (!try_module_get(phy->ops->owner))
  447. return ERR_PTR(-EPROBE_DEFER);
  448. get_device(&phy->dev);
  449. return phy;
  450. }
  451. EXPORT_SYMBOL_GPL(of_phy_get);
  452. /**
  453. * phy_put() - release the PHY
  454. * @phy: the phy returned by phy_get()
  455. *
  456. * Releases a refcount the caller received from phy_get().
  457. */
  458. void phy_put(struct phy *phy)
  459. {
  460. if (!phy || IS_ERR(phy))
  461. return;
  462. mutex_lock(&phy->mutex);
  463. if (phy->ops->release)
  464. phy->ops->release(phy);
  465. mutex_unlock(&phy->mutex);
  466. module_put(phy->ops->owner);
  467. put_device(&phy->dev);
  468. }
  469. EXPORT_SYMBOL_GPL(phy_put);
  470. /**
  471. * devm_phy_put() - release the PHY
  472. * @dev: device that wants to release this phy
  473. * @phy: the phy returned by devm_phy_get()
  474. *
  475. * destroys the devres associated with this phy and invokes phy_put
  476. * to release the phy.
  477. */
  478. void devm_phy_put(struct device *dev, struct phy *phy)
  479. {
  480. int r;
  481. if (!phy)
  482. return;
  483. r = devres_destroy(dev, devm_phy_release, devm_phy_match, phy);
  484. dev_WARN_ONCE(dev, r, "couldn't find PHY resource\n");
  485. }
  486. EXPORT_SYMBOL_GPL(devm_phy_put);
  487. /**
  488. * of_phy_simple_xlate() - returns the phy instance from phy provider
  489. * @dev: the PHY provider device
  490. * @args: of_phandle_args (not used here)
  491. *
  492. * Intended to be used by phy provider for the common case where #phy-cells is
  493. * 0. For other cases where #phy-cells is greater than '0', the phy provider
  494. * should provide a custom of_xlate function that reads the *args* and returns
  495. * the appropriate phy.
  496. */
  497. struct phy *of_phy_simple_xlate(struct device *dev, struct of_phandle_args
  498. *args)
  499. {
  500. struct phy *phy;
  501. struct class_dev_iter iter;
  502. class_dev_iter_init(&iter, phy_class, NULL, NULL);
  503. while ((dev = class_dev_iter_next(&iter))) {
  504. phy = to_phy(dev);
  505. if (args->np != phy->dev.of_node)
  506. continue;
  507. class_dev_iter_exit(&iter);
  508. return phy;
  509. }
  510. class_dev_iter_exit(&iter);
  511. return ERR_PTR(-ENODEV);
  512. }
  513. EXPORT_SYMBOL_GPL(of_phy_simple_xlate);
  514. /**
  515. * phy_get() - lookup and obtain a reference to a phy.
  516. * @dev: device that requests this phy
  517. * @string: the phy name as given in the dt data or the name of the controller
  518. * port for non-dt case
  519. *
  520. * Returns the phy driver, after getting a refcount to it; or
  521. * -ENODEV if there is no such phy. The caller is responsible for
  522. * calling phy_put() to release that count.
  523. */
  524. struct phy *phy_get(struct device *dev, const char *string)
  525. {
  526. int index = 0;
  527. struct phy *phy;
  528. if (string == NULL) {
  529. dev_WARN(dev, "missing string\n");
  530. return ERR_PTR(-EINVAL);
  531. }
  532. if (dev->of_node) {
  533. index = of_property_match_string(dev->of_node, "phy-names",
  534. string);
  535. phy = _of_phy_get(dev->of_node, index);
  536. } else {
  537. phy = phy_find(dev, string);
  538. }
  539. if (IS_ERR(phy))
  540. return phy;
  541. if (!try_module_get(phy->ops->owner))
  542. return ERR_PTR(-EPROBE_DEFER);
  543. get_device(&phy->dev);
  544. return phy;
  545. }
  546. EXPORT_SYMBOL_GPL(phy_get);
  547. /**
  548. * phy_optional_get() - lookup and obtain a reference to an optional phy.
  549. * @dev: device that requests this phy
  550. * @string: the phy name as given in the dt data or the name of the controller
  551. * port for non-dt case
  552. *
  553. * Returns the phy driver, after getting a refcount to it; or
  554. * NULL if there is no such phy. The caller is responsible for
  555. * calling phy_put() to release that count.
  556. */
  557. struct phy *phy_optional_get(struct device *dev, const char *string)
  558. {
  559. struct phy *phy = phy_get(dev, string);
  560. if (IS_ERR(phy) && (PTR_ERR(phy) == -ENODEV))
  561. phy = NULL;
  562. return phy;
  563. }
  564. EXPORT_SYMBOL_GPL(phy_optional_get);
  565. /**
  566. * devm_phy_get() - lookup and obtain a reference to a phy.
  567. * @dev: device that requests this phy
  568. * @string: the phy name as given in the dt data or phy device name
  569. * for non-dt case
  570. *
  571. * Gets the phy using phy_get(), and associates a device with it using
  572. * devres. On driver detach, release function is invoked on the devres data,
  573. * then, devres data is freed.
  574. */
  575. struct phy *devm_phy_get(struct device *dev, const char *string)
  576. {
  577. struct phy **ptr, *phy;
  578. ptr = devres_alloc(devm_phy_release, sizeof(*ptr), GFP_KERNEL);
  579. if (!ptr)
  580. return ERR_PTR(-ENOMEM);
  581. phy = phy_get(dev, string);
  582. if (!IS_ERR(phy)) {
  583. *ptr = phy;
  584. devres_add(dev, ptr);
  585. } else {
  586. devres_free(ptr);
  587. }
  588. return phy;
  589. }
  590. EXPORT_SYMBOL_GPL(devm_phy_get);
  591. /**
  592. * devm_phy_optional_get() - lookup and obtain a reference to an optional phy.
  593. * @dev: device that requests this phy
  594. * @string: the phy name as given in the dt data or phy device name
  595. * for non-dt case
  596. *
  597. * Gets the phy using phy_get(), and associates a device with it using
  598. * devres. On driver detach, release function is invoked on the devres
  599. * data, then, devres data is freed. This differs to devm_phy_get() in
  600. * that if the phy does not exist, it is not considered an error and
  601. * -ENODEV will not be returned. Instead the NULL phy is returned,
  602. * which can be passed to all other phy consumer calls.
  603. */
  604. struct phy *devm_phy_optional_get(struct device *dev, const char *string)
  605. {
  606. struct phy *phy = devm_phy_get(dev, string);
  607. if (IS_ERR(phy) && (PTR_ERR(phy) == -ENODEV))
  608. phy = NULL;
  609. return phy;
  610. }
  611. EXPORT_SYMBOL_GPL(devm_phy_optional_get);
  612. /**
  613. * devm_of_phy_get() - lookup and obtain a reference to a phy.
  614. * @dev: device that requests this phy
  615. * @np: node containing the phy
  616. * @con_id: name of the phy from device's point of view
  617. *
  618. * Gets the phy using of_phy_get(), and associates a device with it using
  619. * devres. On driver detach, release function is invoked on the devres data,
  620. * then, devres data is freed.
  621. */
  622. struct phy *devm_of_phy_get(struct device *dev, struct device_node *np,
  623. const char *con_id)
  624. {
  625. struct phy **ptr, *phy;
  626. ptr = devres_alloc(devm_phy_release, sizeof(*ptr), GFP_KERNEL);
  627. if (!ptr)
  628. return ERR_PTR(-ENOMEM);
  629. phy = of_phy_get(np, con_id);
  630. if (!IS_ERR(phy)) {
  631. *ptr = phy;
  632. devres_add(dev, ptr);
  633. } else {
  634. devres_free(ptr);
  635. }
  636. return phy;
  637. }
  638. EXPORT_SYMBOL_GPL(devm_of_phy_get);
  639. /**
  640. * devm_of_phy_get_by_index() - lookup and obtain a reference to a phy by index.
  641. * @dev: device that requests this phy
  642. * @np: node containing the phy
  643. * @index: index of the phy
  644. *
  645. * Gets the phy using _of_phy_get(), then gets a refcount to it,
  646. * and associates a device with it using devres. On driver detach,
  647. * release function is invoked on the devres data,
  648. * then, devres data is freed.
  649. *
  650. */
  651. struct phy *devm_of_phy_get_by_index(struct device *dev, struct device_node *np,
  652. int index)
  653. {
  654. struct phy **ptr, *phy;
  655. ptr = devres_alloc(devm_phy_release, sizeof(*ptr), GFP_KERNEL);
  656. if (!ptr)
  657. return ERR_PTR(-ENOMEM);
  658. phy = _of_phy_get(np, index);
  659. if (IS_ERR(phy)) {
  660. devres_free(ptr);
  661. return phy;
  662. }
  663. if (!try_module_get(phy->ops->owner)) {
  664. devres_free(ptr);
  665. return ERR_PTR(-EPROBE_DEFER);
  666. }
  667. get_device(&phy->dev);
  668. *ptr = phy;
  669. devres_add(dev, ptr);
  670. return phy;
  671. }
  672. EXPORT_SYMBOL_GPL(devm_of_phy_get_by_index);
  673. /**
  674. * phy_create() - create a new phy
  675. * @dev: device that is creating the new phy
  676. * @node: device node of the phy
  677. * @ops: function pointers for performing phy operations
  678. *
  679. * Called to create a phy using phy framework.
  680. */
  681. struct phy *phy_create(struct device *dev, struct device_node *node,
  682. const struct phy_ops *ops)
  683. {
  684. int ret;
  685. int id;
  686. struct phy *phy;
  687. if (WARN_ON(!dev))
  688. return ERR_PTR(-EINVAL);
  689. phy = kzalloc(sizeof(*phy), GFP_KERNEL);
  690. if (!phy)
  691. return ERR_PTR(-ENOMEM);
  692. id = ida_simple_get(&phy_ida, 0, 0, GFP_KERNEL);
  693. if (id < 0) {
  694. dev_err(dev, "unable to get id\n");
  695. ret = id;
  696. goto free_phy;
  697. }
  698. device_initialize(&phy->dev);
  699. mutex_init(&phy->mutex);
  700. phy->dev.class = phy_class;
  701. phy->dev.parent = dev;
  702. phy->dev.of_node = node ?: dev->of_node;
  703. phy->id = id;
  704. phy->ops = ops;
  705. ret = dev_set_name(&phy->dev, "phy-%s.%d", dev_name(dev), id);
  706. if (ret)
  707. goto put_dev;
  708. /* phy-supply */
  709. phy->pwr = regulator_get_optional(&phy->dev, "phy");
  710. if (IS_ERR(phy->pwr)) {
  711. ret = PTR_ERR(phy->pwr);
  712. if (ret == -EPROBE_DEFER)
  713. goto put_dev;
  714. phy->pwr = NULL;
  715. }
  716. ret = device_add(&phy->dev);
  717. if (ret)
  718. goto put_dev;
  719. if (pm_runtime_enabled(dev)) {
  720. pm_runtime_enable(&phy->dev);
  721. pm_runtime_no_callbacks(&phy->dev);
  722. }
  723. return phy;
  724. put_dev:
  725. put_device(&phy->dev); /* calls phy_release() which frees resources */
  726. return ERR_PTR(ret);
  727. free_phy:
  728. kfree(phy);
  729. return ERR_PTR(ret);
  730. }
  731. EXPORT_SYMBOL_GPL(phy_create);
  732. /**
  733. * devm_phy_create() - create a new phy
  734. * @dev: device that is creating the new phy
  735. * @node: device node of the phy
  736. * @ops: function pointers for performing phy operations
  737. *
  738. * Creates a new PHY device adding it to the PHY class.
  739. * While at that, it also associates the device with the phy using devres.
  740. * On driver detach, release function is invoked on the devres data,
  741. * then, devres data is freed.
  742. */
  743. struct phy *devm_phy_create(struct device *dev, struct device_node *node,
  744. const struct phy_ops *ops)
  745. {
  746. struct phy **ptr, *phy;
  747. ptr = devres_alloc(devm_phy_consume, sizeof(*ptr), GFP_KERNEL);
  748. if (!ptr)
  749. return ERR_PTR(-ENOMEM);
  750. phy = phy_create(dev, node, ops);
  751. if (!IS_ERR(phy)) {
  752. *ptr = phy;
  753. devres_add(dev, ptr);
  754. } else {
  755. devres_free(ptr);
  756. }
  757. return phy;
  758. }
  759. EXPORT_SYMBOL_GPL(devm_phy_create);
  760. /**
  761. * phy_destroy() - destroy the phy
  762. * @phy: the phy to be destroyed
  763. *
  764. * Called to destroy the phy.
  765. */
  766. void phy_destroy(struct phy *phy)
  767. {
  768. pm_runtime_disable(&phy->dev);
  769. device_unregister(&phy->dev);
  770. }
  771. EXPORT_SYMBOL_GPL(phy_destroy);
  772. /**
  773. * devm_phy_destroy() - destroy the PHY
  774. * @dev: device that wants to release this phy
  775. * @phy: the phy returned by devm_phy_get()
  776. *
  777. * destroys the devres associated with this phy and invokes phy_destroy
  778. * to destroy the phy.
  779. */
  780. void devm_phy_destroy(struct device *dev, struct phy *phy)
  781. {
  782. int r;
  783. r = devres_destroy(dev, devm_phy_consume, devm_phy_match, phy);
  784. dev_WARN_ONCE(dev, r, "couldn't find PHY resource\n");
  785. }
  786. EXPORT_SYMBOL_GPL(devm_phy_destroy);
  787. /**
  788. * __of_phy_provider_register() - create/register phy provider with the framework
  789. * @dev: struct device of the phy provider
  790. * @children: device node containing children (if different from dev->of_node)
  791. * @owner: the module owner containing of_xlate
  792. * @of_xlate: function pointer to obtain phy instance from phy provider
  793. *
  794. * Creates struct phy_provider from dev and of_xlate function pointer.
  795. * This is used in the case of dt boot for finding the phy instance from
  796. * phy provider.
  797. *
  798. * If the PHY provider doesn't nest children directly but uses a separate
  799. * child node to contain the individual children, the @children parameter
  800. * can be used to override the default. If NULL, the default (dev->of_node)
  801. * will be used. If non-NULL, the device node must be a child (or further
  802. * descendant) of dev->of_node. Otherwise an ERR_PTR()-encoded -EINVAL
  803. * error code is returned.
  804. */
  805. struct phy_provider *__of_phy_provider_register(struct device *dev,
  806. struct device_node *children, struct module *owner,
  807. struct phy * (*of_xlate)(struct device *dev,
  808. struct of_phandle_args *args))
  809. {
  810. struct phy_provider *phy_provider;
  811. /*
  812. * If specified, the device node containing the children must itself
  813. * be the provider's device node or a child (or further descendant)
  814. * thereof.
  815. */
  816. if (children) {
  817. struct device_node *parent = of_node_get(children), *next;
  818. while (parent) {
  819. if (parent == dev->of_node)
  820. break;
  821. next = of_get_parent(parent);
  822. of_node_put(parent);
  823. parent = next;
  824. }
  825. if (!parent)
  826. return ERR_PTR(-EINVAL);
  827. of_node_put(parent);
  828. } else {
  829. children = dev->of_node;
  830. }
  831. phy_provider = kzalloc(sizeof(*phy_provider), GFP_KERNEL);
  832. if (!phy_provider)
  833. return ERR_PTR(-ENOMEM);
  834. phy_provider->dev = dev;
  835. phy_provider->children = of_node_get(children);
  836. phy_provider->owner = owner;
  837. phy_provider->of_xlate = of_xlate;
  838. mutex_lock(&phy_provider_mutex);
  839. list_add_tail(&phy_provider->list, &phy_provider_list);
  840. mutex_unlock(&phy_provider_mutex);
  841. return phy_provider;
  842. }
  843. EXPORT_SYMBOL_GPL(__of_phy_provider_register);
  844. /**
  845. * __devm_of_phy_provider_register() - create/register phy provider with the
  846. * framework
  847. * @dev: struct device of the phy provider
  848. * @owner: the module owner containing of_xlate
  849. * @of_xlate: function pointer to obtain phy instance from phy provider
  850. *
  851. * Creates struct phy_provider from dev and of_xlate function pointer.
  852. * This is used in the case of dt boot for finding the phy instance from
  853. * phy provider. While at that, it also associates the device with the
  854. * phy provider using devres. On driver detach, release function is invoked
  855. * on the devres data, then, devres data is freed.
  856. */
  857. struct phy_provider *__devm_of_phy_provider_register(struct device *dev,
  858. struct device_node *children, struct module *owner,
  859. struct phy * (*of_xlate)(struct device *dev,
  860. struct of_phandle_args *args))
  861. {
  862. struct phy_provider **ptr, *phy_provider;
  863. ptr = devres_alloc(devm_phy_provider_release, sizeof(*ptr), GFP_KERNEL);
  864. if (!ptr)
  865. return ERR_PTR(-ENOMEM);
  866. phy_provider = __of_phy_provider_register(dev, children, owner,
  867. of_xlate);
  868. if (!IS_ERR(phy_provider)) {
  869. *ptr = phy_provider;
  870. devres_add(dev, ptr);
  871. } else {
  872. devres_free(ptr);
  873. }
  874. return phy_provider;
  875. }
  876. EXPORT_SYMBOL_GPL(__devm_of_phy_provider_register);
  877. /**
  878. * of_phy_provider_unregister() - unregister phy provider from the framework
  879. * @phy_provider: phy provider returned by of_phy_provider_register()
  880. *
  881. * Removes the phy_provider created using of_phy_provider_register().
  882. */
  883. void of_phy_provider_unregister(struct phy_provider *phy_provider)
  884. {
  885. if (IS_ERR(phy_provider))
  886. return;
  887. mutex_lock(&phy_provider_mutex);
  888. list_del(&phy_provider->list);
  889. of_node_put(phy_provider->children);
  890. kfree(phy_provider);
  891. mutex_unlock(&phy_provider_mutex);
  892. }
  893. EXPORT_SYMBOL_GPL(of_phy_provider_unregister);
  894. /**
  895. * devm_of_phy_provider_unregister() - remove phy provider from the framework
  896. * @dev: struct device of the phy provider
  897. *
  898. * destroys the devres associated with this phy provider and invokes
  899. * of_phy_provider_unregister to unregister the phy provider.
  900. */
  901. void devm_of_phy_provider_unregister(struct device *dev,
  902. struct phy_provider *phy_provider) {
  903. int r;
  904. r = devres_destroy(dev, devm_phy_provider_release, devm_phy_match,
  905. phy_provider);
  906. dev_WARN_ONCE(dev, r, "couldn't find PHY provider device resource\n");
  907. }
  908. EXPORT_SYMBOL_GPL(devm_of_phy_provider_unregister);
  909. /**
  910. * phy_release() - release the phy
  911. * @dev: the dev member within phy
  912. *
  913. * When the last reference to the device is removed, it is called
  914. * from the embedded kobject as release method.
  915. */
  916. static void phy_release(struct device *dev)
  917. {
  918. struct phy *phy;
  919. phy = to_phy(dev);
  920. dev_vdbg(dev, "releasing '%s'\n", dev_name(dev));
  921. regulator_put(phy->pwr);
  922. ida_simple_remove(&phy_ida, phy->id);
  923. kfree(phy);
  924. }
  925. static int __init phy_core_init(void)
  926. {
  927. phy_class = class_create(THIS_MODULE, "phy");
  928. if (IS_ERR(phy_class)) {
  929. pr_err("failed to create phy class --> %ld\n",
  930. PTR_ERR(phy_class));
  931. return PTR_ERR(phy_class);
  932. }
  933. phy_class->dev_release = phy_release;
  934. return 0;
  935. }
  936. module_init(phy_core_init);
  937. static void __exit phy_core_exit(void)
  938. {
  939. class_destroy(phy_class);
  940. }
  941. module_exit(phy_core_exit);
  942. MODULE_DESCRIPTION("Generic PHY Framework");
  943. MODULE_AUTHOR("Kishon Vijay Abraham I <kishon@ti.com>");
  944. MODULE_LICENSE("GPL v2");