phy-core.c 29 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * phy-core.c -- Generic Phy framework.
  4. *
  5. * Copyright (C) 2013 Texas Instruments Incorporated - http://www.ti.com
  6. *
  7. * Author: Kishon Vijay Abraham I <[email protected]>
  8. */
  9. #include <linux/kernel.h>
  10. #include <linux/export.h>
  11. #include <linux/module.h>
  12. #include <linux/err.h>
  13. #include <linux/device.h>
  14. #include <linux/slab.h>
  15. #include <linux/of.h>
  16. #include <linux/phy/phy.h>
  17. #include <linux/idr.h>
  18. #include <linux/pm_runtime.h>
  19. #include <linux/regulator/consumer.h>
  20. static struct class *phy_class;
  21. static DEFINE_MUTEX(phy_provider_mutex);
  22. static LIST_HEAD(phy_provider_list);
  23. static LIST_HEAD(phys);
  24. static DEFINE_IDA(phy_ida);
  25. static void devm_phy_release(struct device *dev, void *res)
  26. {
  27. struct phy *phy = *(struct phy **)res;
  28. phy_put(dev, phy);
  29. }
  30. static void devm_phy_provider_release(struct device *dev, void *res)
  31. {
  32. struct phy_provider *phy_provider = *(struct phy_provider **)res;
  33. of_phy_provider_unregister(phy_provider);
  34. }
  35. static void devm_phy_consume(struct device *dev, void *res)
  36. {
  37. struct phy *phy = *(struct phy **)res;
  38. phy_destroy(phy);
  39. }
  40. static int devm_phy_match(struct device *dev, void *res, void *match_data)
  41. {
  42. struct phy **phy = res;
  43. return *phy == match_data;
  44. }
  45. /**
  46. * phy_create_lookup() - allocate and register PHY/device association
  47. * @phy: the phy of the association
  48. * @con_id: connection ID string on device
  49. * @dev_id: the device of the association
  50. *
  51. * Creates and registers phy_lookup entry.
  52. */
  53. int phy_create_lookup(struct phy *phy, const char *con_id, const char *dev_id)
  54. {
  55. struct phy_lookup *pl;
  56. if (!phy || !dev_id || !con_id)
  57. return -EINVAL;
  58. pl = kzalloc(sizeof(*pl), GFP_KERNEL);
  59. if (!pl)
  60. return -ENOMEM;
  61. pl->dev_id = dev_id;
  62. pl->con_id = con_id;
  63. pl->phy = phy;
  64. mutex_lock(&phy_provider_mutex);
  65. list_add_tail(&pl->node, &phys);
  66. mutex_unlock(&phy_provider_mutex);
  67. return 0;
  68. }
  69. EXPORT_SYMBOL_GPL(phy_create_lookup);
  70. /**
  71. * phy_remove_lookup() - find and remove PHY/device association
  72. * @phy: the phy of the association
  73. * @con_id: connection ID string on device
  74. * @dev_id: the device of the association
  75. *
  76. * Finds and unregisters phy_lookup entry that was created with
  77. * phy_create_lookup().
  78. */
  79. void phy_remove_lookup(struct phy *phy, const char *con_id, const char *dev_id)
  80. {
  81. struct phy_lookup *pl;
  82. if (!phy || !dev_id || !con_id)
  83. return;
  84. mutex_lock(&phy_provider_mutex);
  85. list_for_each_entry(pl, &phys, node)
  86. if (pl->phy == phy && !strcmp(pl->dev_id, dev_id) &&
  87. !strcmp(pl->con_id, con_id)) {
  88. list_del(&pl->node);
  89. kfree(pl);
  90. break;
  91. }
  92. mutex_unlock(&phy_provider_mutex);
  93. }
  94. EXPORT_SYMBOL_GPL(phy_remove_lookup);
  95. static struct phy *phy_find(struct device *dev, const char *con_id)
  96. {
  97. const char *dev_id = dev_name(dev);
  98. struct phy_lookup *p, *pl = NULL;
  99. mutex_lock(&phy_provider_mutex);
  100. list_for_each_entry(p, &phys, node)
  101. if (!strcmp(p->dev_id, dev_id) && !strcmp(p->con_id, con_id)) {
  102. pl = p;
  103. break;
  104. }
  105. mutex_unlock(&phy_provider_mutex);
  106. return pl ? pl->phy : ERR_PTR(-ENODEV);
  107. }
  108. static struct phy_provider *of_phy_provider_lookup(struct device_node *node)
  109. {
  110. struct phy_provider *phy_provider;
  111. struct device_node *child;
  112. list_for_each_entry(phy_provider, &phy_provider_list, list) {
  113. if (phy_provider->dev->of_node == node)
  114. return phy_provider;
  115. for_each_child_of_node(phy_provider->children, child)
  116. if (child == node)
  117. return phy_provider;
  118. }
  119. return ERR_PTR(-EPROBE_DEFER);
  120. }
  121. int phy_pm_runtime_get(struct phy *phy)
  122. {
  123. int ret;
  124. if (!phy)
  125. return 0;
  126. if (!pm_runtime_enabled(&phy->dev))
  127. return -ENOTSUPP;
  128. ret = pm_runtime_get(&phy->dev);
  129. if (ret < 0 && ret != -EINPROGRESS)
  130. pm_runtime_put_noidle(&phy->dev);
  131. return ret;
  132. }
  133. EXPORT_SYMBOL_GPL(phy_pm_runtime_get);
  134. int phy_pm_runtime_get_sync(struct phy *phy)
  135. {
  136. int ret;
  137. if (!phy)
  138. return 0;
  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 (!phy)
  150. return 0;
  151. if (!pm_runtime_enabled(&phy->dev))
  152. return -ENOTSUPP;
  153. return pm_runtime_put(&phy->dev);
  154. }
  155. EXPORT_SYMBOL_GPL(phy_pm_runtime_put);
  156. int phy_pm_runtime_put_sync(struct phy *phy)
  157. {
  158. if (!phy)
  159. return 0;
  160. if (!pm_runtime_enabled(&phy->dev))
  161. return -ENOTSUPP;
  162. return pm_runtime_put_sync(&phy->dev);
  163. }
  164. EXPORT_SYMBOL_GPL(phy_pm_runtime_put_sync);
  165. void phy_pm_runtime_allow(struct phy *phy)
  166. {
  167. if (!phy)
  168. return;
  169. if (!pm_runtime_enabled(&phy->dev))
  170. return;
  171. pm_runtime_allow(&phy->dev);
  172. }
  173. EXPORT_SYMBOL_GPL(phy_pm_runtime_allow);
  174. void phy_pm_runtime_forbid(struct phy *phy)
  175. {
  176. if (!phy)
  177. return;
  178. if (!pm_runtime_enabled(&phy->dev))
  179. return;
  180. pm_runtime_forbid(&phy->dev);
  181. }
  182. EXPORT_SYMBOL_GPL(phy_pm_runtime_forbid);
  183. /**
  184. * phy_init - phy internal initialization before phy operation
  185. * @phy: the phy returned by phy_get()
  186. *
  187. * Used to allow phy's driver to perform phy internal initialization,
  188. * such as PLL block powering, clock initialization or anything that's
  189. * is required by the phy to perform the start of operation.
  190. * Must be called before phy_power_on().
  191. *
  192. * Return: %0 if successful, a negative error code otherwise
  193. */
  194. int phy_init(struct phy *phy)
  195. {
  196. int ret;
  197. if (!phy)
  198. return 0;
  199. ret = phy_pm_runtime_get_sync(phy);
  200. if (ret < 0 && ret != -ENOTSUPP)
  201. return ret;
  202. ret = 0; /* Override possible ret == -ENOTSUPP */
  203. mutex_lock(&phy->mutex);
  204. if (phy->power_count > phy->init_count)
  205. dev_warn(&phy->dev, "phy_power_on was called before phy_init\n");
  206. if (phy->init_count == 0 && phy->ops->init) {
  207. ret = phy->ops->init(phy);
  208. if (ret < 0) {
  209. dev_err(&phy->dev, "phy init failed --> %d\n", ret);
  210. goto out;
  211. }
  212. }
  213. ++phy->init_count;
  214. out:
  215. mutex_unlock(&phy->mutex);
  216. phy_pm_runtime_put(phy);
  217. return ret;
  218. }
  219. EXPORT_SYMBOL_GPL(phy_init);
  220. /**
  221. * phy_exit - Phy internal un-initialization
  222. * @phy: the phy returned by phy_get()
  223. *
  224. * Must be called after phy_power_off().
  225. *
  226. * Return: %0 if successful, a negative error code otherwise
  227. */
  228. int phy_exit(struct phy *phy)
  229. {
  230. int ret;
  231. if (!phy)
  232. return 0;
  233. ret = phy_pm_runtime_get_sync(phy);
  234. if (ret < 0 && ret != -ENOTSUPP)
  235. return ret;
  236. ret = 0; /* Override possible ret == -ENOTSUPP */
  237. mutex_lock(&phy->mutex);
  238. if (phy->init_count == 1 && phy->ops->exit) {
  239. ret = phy->ops->exit(phy);
  240. if (ret < 0) {
  241. dev_err(&phy->dev, "phy exit failed --> %d\n", ret);
  242. goto out;
  243. }
  244. }
  245. --phy->init_count;
  246. out:
  247. mutex_unlock(&phy->mutex);
  248. phy_pm_runtime_put(phy);
  249. return ret;
  250. }
  251. EXPORT_SYMBOL_GPL(phy_exit);
  252. /**
  253. * phy_power_on - Enable the phy and enter proper operation
  254. * @phy: the phy returned by phy_get()
  255. *
  256. * Must be called after phy_init().
  257. *
  258. * Return: %0 if successful, a negative error code otherwise
  259. */
  260. int phy_power_on(struct phy *phy)
  261. {
  262. int ret = 0;
  263. if (!phy)
  264. goto out;
  265. if (phy->pwr) {
  266. ret = regulator_enable(phy->pwr);
  267. if (ret)
  268. goto out;
  269. }
  270. ret = phy_pm_runtime_get_sync(phy);
  271. if (ret < 0 && ret != -ENOTSUPP)
  272. goto err_pm_sync;
  273. ret = 0; /* Override possible ret == -ENOTSUPP */
  274. mutex_lock(&phy->mutex);
  275. if (phy->power_count == 0 && phy->ops->power_on) {
  276. ret = phy->ops->power_on(phy);
  277. if (ret < 0) {
  278. dev_err(&phy->dev, "phy poweron failed --> %d\n", ret);
  279. goto err_pwr_on;
  280. }
  281. }
  282. ++phy->power_count;
  283. mutex_unlock(&phy->mutex);
  284. return 0;
  285. err_pwr_on:
  286. mutex_unlock(&phy->mutex);
  287. phy_pm_runtime_put_sync(phy);
  288. err_pm_sync:
  289. if (phy->pwr)
  290. regulator_disable(phy->pwr);
  291. out:
  292. return ret;
  293. }
  294. EXPORT_SYMBOL_GPL(phy_power_on);
  295. /**
  296. * phy_power_off - Disable the phy.
  297. * @phy: the phy returned by phy_get()
  298. *
  299. * Must be called before phy_exit().
  300. *
  301. * Return: %0 if successful, a negative error code otherwise
  302. */
  303. int phy_power_off(struct phy *phy)
  304. {
  305. int ret;
  306. if (!phy)
  307. return 0;
  308. mutex_lock(&phy->mutex);
  309. if (phy->power_count == 1 && phy->ops->power_off) {
  310. ret = phy->ops->power_off(phy);
  311. if (ret < 0) {
  312. dev_err(&phy->dev, "phy poweroff failed --> %d\n", ret);
  313. mutex_unlock(&phy->mutex);
  314. return ret;
  315. }
  316. }
  317. --phy->power_count;
  318. mutex_unlock(&phy->mutex);
  319. phy_pm_runtime_put(phy);
  320. if (phy->pwr)
  321. regulator_disable(phy->pwr);
  322. return 0;
  323. }
  324. EXPORT_SYMBOL_GPL(phy_power_off);
  325. int phy_set_mode_ext(struct phy *phy, enum phy_mode mode, int submode)
  326. {
  327. int ret;
  328. if (!phy || !phy->ops->set_mode)
  329. return 0;
  330. mutex_lock(&phy->mutex);
  331. ret = phy->ops->set_mode(phy, mode, submode);
  332. if (!ret)
  333. phy->attrs.mode = mode;
  334. mutex_unlock(&phy->mutex);
  335. return ret;
  336. }
  337. EXPORT_SYMBOL_GPL(phy_set_mode_ext);
  338. int phy_set_media(struct phy *phy, enum phy_media media)
  339. {
  340. int ret;
  341. if (!phy || !phy->ops->set_media)
  342. return 0;
  343. mutex_lock(&phy->mutex);
  344. ret = phy->ops->set_media(phy, media);
  345. mutex_unlock(&phy->mutex);
  346. return ret;
  347. }
  348. EXPORT_SYMBOL_GPL(phy_set_media);
  349. int phy_set_speed(struct phy *phy, int speed)
  350. {
  351. int ret;
  352. if (!phy || !phy->ops->set_speed)
  353. return 0;
  354. mutex_lock(&phy->mutex);
  355. ret = phy->ops->set_speed(phy, speed);
  356. mutex_unlock(&phy->mutex);
  357. return ret;
  358. }
  359. EXPORT_SYMBOL_GPL(phy_set_speed);
  360. int phy_reset(struct phy *phy)
  361. {
  362. int ret;
  363. if (!phy || !phy->ops->reset)
  364. return 0;
  365. ret = phy_pm_runtime_get_sync(phy);
  366. if (ret < 0 && ret != -ENOTSUPP)
  367. return ret;
  368. mutex_lock(&phy->mutex);
  369. ret = phy->ops->reset(phy);
  370. mutex_unlock(&phy->mutex);
  371. phy_pm_runtime_put(phy);
  372. return ret;
  373. }
  374. EXPORT_SYMBOL_GPL(phy_reset);
  375. /**
  376. * phy_calibrate() - Tunes the phy hw parameters for current configuration
  377. * @phy: the phy returned by phy_get()
  378. *
  379. * Used to calibrate phy hardware, typically by adjusting some parameters in
  380. * runtime, which are otherwise lost after host controller reset and cannot
  381. * be applied in phy_init() or phy_power_on().
  382. *
  383. * Return: %0 if successful, a negative error code otherwise
  384. */
  385. int phy_calibrate(struct phy *phy)
  386. {
  387. int ret;
  388. if (!phy || !phy->ops->calibrate)
  389. return 0;
  390. mutex_lock(&phy->mutex);
  391. ret = phy->ops->calibrate(phy);
  392. mutex_unlock(&phy->mutex);
  393. return ret;
  394. }
  395. EXPORT_SYMBOL_GPL(phy_calibrate);
  396. /**
  397. * phy_configure() - Changes the phy parameters
  398. * @phy: the phy returned by phy_get()
  399. * @opts: New configuration to apply
  400. *
  401. * Used to change the PHY parameters. phy_init() must have been called
  402. * on the phy. The configuration will be applied on the current phy
  403. * mode, that can be changed using phy_set_mode().
  404. *
  405. * Return: %0 if successful, a negative error code otherwise
  406. */
  407. int phy_configure(struct phy *phy, union phy_configure_opts *opts)
  408. {
  409. int ret;
  410. if (!phy)
  411. return -EINVAL;
  412. if (!phy->ops->configure)
  413. return -EOPNOTSUPP;
  414. mutex_lock(&phy->mutex);
  415. ret = phy->ops->configure(phy, opts);
  416. mutex_unlock(&phy->mutex);
  417. return ret;
  418. }
  419. EXPORT_SYMBOL_GPL(phy_configure);
  420. /**
  421. * phy_validate() - Checks the phy parameters
  422. * @phy: the phy returned by phy_get()
  423. * @mode: phy_mode the configuration is applicable to.
  424. * @submode: PHY submode the configuration is applicable to.
  425. * @opts: Configuration to check
  426. *
  427. * Used to check that the current set of parameters can be handled by
  428. * the phy. Implementations are free to tune the parameters passed as
  429. * arguments if needed by some implementation detail or
  430. * constraints. It will not change any actual configuration of the
  431. * PHY, so calling it as many times as deemed fit will have no side
  432. * effect.
  433. *
  434. * Return: %0 if successful, a negative error code otherwise
  435. */
  436. int phy_validate(struct phy *phy, enum phy_mode mode, int submode,
  437. union phy_configure_opts *opts)
  438. {
  439. int ret;
  440. if (!phy)
  441. return -EINVAL;
  442. if (!phy->ops->validate)
  443. return -EOPNOTSUPP;
  444. mutex_lock(&phy->mutex);
  445. ret = phy->ops->validate(phy, mode, submode, opts);
  446. mutex_unlock(&phy->mutex);
  447. return ret;
  448. }
  449. EXPORT_SYMBOL_GPL(phy_validate);
  450. /**
  451. * _of_phy_get() - lookup and obtain a reference to a phy by phandle
  452. * @np: device_node for which to get the phy
  453. * @index: the index of the phy
  454. *
  455. * Returns the phy associated with the given phandle value,
  456. * after getting a refcount to it or -ENODEV if there is no such phy or
  457. * -EPROBE_DEFER if there is a phandle to the phy, but the device is
  458. * not yet loaded. This function uses of_xlate call back function provided
  459. * while registering the phy_provider to find the phy instance.
  460. */
  461. static struct phy *_of_phy_get(struct device_node *np, int index)
  462. {
  463. int ret;
  464. struct phy_provider *phy_provider;
  465. struct phy *phy = NULL;
  466. struct of_phandle_args args;
  467. ret = of_parse_phandle_with_args(np, "phys", "#phy-cells",
  468. index, &args);
  469. if (ret)
  470. return ERR_PTR(-ENODEV);
  471. /* This phy type handled by the usb-phy subsystem for now */
  472. if (of_device_is_compatible(args.np, "usb-nop-xceiv"))
  473. return ERR_PTR(-ENODEV);
  474. mutex_lock(&phy_provider_mutex);
  475. phy_provider = of_phy_provider_lookup(args.np);
  476. if (IS_ERR(phy_provider) || !try_module_get(phy_provider->owner)) {
  477. phy = ERR_PTR(-EPROBE_DEFER);
  478. goto out_unlock;
  479. }
  480. if (!of_device_is_available(args.np)) {
  481. dev_warn(phy_provider->dev, "Requested PHY is disabled\n");
  482. phy = ERR_PTR(-ENODEV);
  483. goto out_put_module;
  484. }
  485. phy = phy_provider->of_xlate(phy_provider->dev, &args);
  486. out_put_module:
  487. module_put(phy_provider->owner);
  488. out_unlock:
  489. mutex_unlock(&phy_provider_mutex);
  490. of_node_put(args.np);
  491. return phy;
  492. }
  493. /**
  494. * of_phy_get() - lookup and obtain a reference to a phy using a device_node.
  495. * @np: device_node for which to get the phy
  496. * @con_id: name of the phy from device's point of view
  497. *
  498. * Returns the phy driver, after getting a refcount to it; or
  499. * -ENODEV if there is no such phy. The caller is responsible for
  500. * calling phy_put() to release that count.
  501. */
  502. struct phy *of_phy_get(struct device_node *np, const char *con_id)
  503. {
  504. struct phy *phy = NULL;
  505. int index = 0;
  506. if (con_id)
  507. index = of_property_match_string(np, "phy-names", con_id);
  508. phy = _of_phy_get(np, index);
  509. if (IS_ERR(phy))
  510. return phy;
  511. if (!try_module_get(phy->ops->owner))
  512. return ERR_PTR(-EPROBE_DEFER);
  513. get_device(&phy->dev);
  514. return phy;
  515. }
  516. EXPORT_SYMBOL_GPL(of_phy_get);
  517. /**
  518. * of_phy_put() - release the PHY
  519. * @phy: the phy returned by of_phy_get()
  520. *
  521. * Releases a refcount the caller received from of_phy_get().
  522. */
  523. void of_phy_put(struct phy *phy)
  524. {
  525. if (!phy || IS_ERR(phy))
  526. return;
  527. mutex_lock(&phy->mutex);
  528. if (phy->ops->release)
  529. phy->ops->release(phy);
  530. mutex_unlock(&phy->mutex);
  531. module_put(phy->ops->owner);
  532. put_device(&phy->dev);
  533. }
  534. EXPORT_SYMBOL_GPL(of_phy_put);
  535. /**
  536. * phy_put() - release the PHY
  537. * @dev: device that wants to release this phy
  538. * @phy: the phy returned by phy_get()
  539. *
  540. * Releases a refcount the caller received from phy_get().
  541. */
  542. void phy_put(struct device *dev, struct phy *phy)
  543. {
  544. device_link_remove(dev, &phy->dev);
  545. of_phy_put(phy);
  546. }
  547. EXPORT_SYMBOL_GPL(phy_put);
  548. /**
  549. * devm_phy_put() - release the PHY
  550. * @dev: device that wants to release this phy
  551. * @phy: the phy returned by devm_phy_get()
  552. *
  553. * destroys the devres associated with this phy and invokes phy_put
  554. * to release the phy.
  555. */
  556. void devm_phy_put(struct device *dev, struct phy *phy)
  557. {
  558. int r;
  559. if (!phy)
  560. return;
  561. r = devres_destroy(dev, devm_phy_release, devm_phy_match, phy);
  562. dev_WARN_ONCE(dev, r, "couldn't find PHY resource\n");
  563. }
  564. EXPORT_SYMBOL_GPL(devm_phy_put);
  565. /**
  566. * of_phy_simple_xlate() - returns the phy instance from phy provider
  567. * @dev: the PHY provider device
  568. * @args: of_phandle_args (not used here)
  569. *
  570. * Intended to be used by phy provider for the common case where #phy-cells is
  571. * 0. For other cases where #phy-cells is greater than '0', the phy provider
  572. * should provide a custom of_xlate function that reads the *args* and returns
  573. * the appropriate phy.
  574. */
  575. struct phy *of_phy_simple_xlate(struct device *dev, struct of_phandle_args
  576. *args)
  577. {
  578. struct phy *phy;
  579. struct class_dev_iter iter;
  580. class_dev_iter_init(&iter, phy_class, NULL, NULL);
  581. while ((dev = class_dev_iter_next(&iter))) {
  582. phy = to_phy(dev);
  583. if (args->np != phy->dev.of_node)
  584. continue;
  585. class_dev_iter_exit(&iter);
  586. return phy;
  587. }
  588. class_dev_iter_exit(&iter);
  589. return ERR_PTR(-ENODEV);
  590. }
  591. EXPORT_SYMBOL_GPL(of_phy_simple_xlate);
  592. /**
  593. * phy_get() - lookup and obtain a reference to a phy.
  594. * @dev: device that requests this phy
  595. * @string: the phy name as given in the dt data or the name of the controller
  596. * port for non-dt case
  597. *
  598. * Returns the phy driver, after getting a refcount to it; or
  599. * -ENODEV if there is no such phy. The caller is responsible for
  600. * calling phy_put() to release that count.
  601. */
  602. struct phy *phy_get(struct device *dev, const char *string)
  603. {
  604. int index = 0;
  605. struct phy *phy;
  606. struct device_link *link;
  607. if (dev->of_node) {
  608. if (string)
  609. index = of_property_match_string(dev->of_node, "phy-names",
  610. string);
  611. else
  612. index = 0;
  613. phy = _of_phy_get(dev->of_node, index);
  614. } else {
  615. if (string == NULL) {
  616. dev_WARN(dev, "missing string\n");
  617. return ERR_PTR(-EINVAL);
  618. }
  619. phy = phy_find(dev, string);
  620. }
  621. if (IS_ERR(phy))
  622. return phy;
  623. if (!try_module_get(phy->ops->owner))
  624. return ERR_PTR(-EPROBE_DEFER);
  625. get_device(&phy->dev);
  626. link = device_link_add(dev, &phy->dev, DL_FLAG_STATELESS);
  627. if (!link)
  628. dev_dbg(dev, "failed to create device link to %s\n",
  629. dev_name(phy->dev.parent));
  630. return phy;
  631. }
  632. EXPORT_SYMBOL_GPL(phy_get);
  633. /**
  634. * phy_optional_get() - lookup and obtain a reference to an optional phy.
  635. * @dev: device that requests this phy
  636. * @string: the phy name as given in the dt data or the name of the controller
  637. * port for non-dt case
  638. *
  639. * Returns the phy driver, after getting a refcount to it; or
  640. * NULL if there is no such phy. The caller is responsible for
  641. * calling phy_put() to release that count.
  642. */
  643. struct phy *phy_optional_get(struct device *dev, const char *string)
  644. {
  645. struct phy *phy = phy_get(dev, string);
  646. if (PTR_ERR(phy) == -ENODEV)
  647. phy = NULL;
  648. return phy;
  649. }
  650. EXPORT_SYMBOL_GPL(phy_optional_get);
  651. /**
  652. * devm_phy_get() - lookup and obtain a reference to a phy.
  653. * @dev: device that requests this phy
  654. * @string: the phy name as given in the dt data or phy device name
  655. * for non-dt case
  656. *
  657. * Gets the phy using phy_get(), and associates a device with it using
  658. * devres. On driver detach, release function is invoked on the devres data,
  659. * then, devres data is freed.
  660. */
  661. struct phy *devm_phy_get(struct device *dev, const char *string)
  662. {
  663. struct phy **ptr, *phy;
  664. ptr = devres_alloc(devm_phy_release, sizeof(*ptr), GFP_KERNEL);
  665. if (!ptr)
  666. return ERR_PTR(-ENOMEM);
  667. phy = phy_get(dev, string);
  668. if (!IS_ERR(phy)) {
  669. *ptr = phy;
  670. devres_add(dev, ptr);
  671. } else {
  672. devres_free(ptr);
  673. }
  674. return phy;
  675. }
  676. EXPORT_SYMBOL_GPL(devm_phy_get);
  677. /**
  678. * devm_phy_optional_get() - lookup and obtain a reference to an optional phy.
  679. * @dev: device that requests this phy
  680. * @string: the phy name as given in the dt data or phy device name
  681. * for non-dt case
  682. *
  683. * Gets the phy using phy_get(), and associates a device with it using
  684. * devres. On driver detach, release function is invoked on the devres
  685. * data, then, devres data is freed. This differs to devm_phy_get() in
  686. * that if the phy does not exist, it is not considered an error and
  687. * -ENODEV will not be returned. Instead the NULL phy is returned,
  688. * which can be passed to all other phy consumer calls.
  689. */
  690. struct phy *devm_phy_optional_get(struct device *dev, const char *string)
  691. {
  692. struct phy *phy = devm_phy_get(dev, string);
  693. if (PTR_ERR(phy) == -ENODEV)
  694. phy = NULL;
  695. return phy;
  696. }
  697. EXPORT_SYMBOL_GPL(devm_phy_optional_get);
  698. /**
  699. * devm_of_phy_get() - lookup and obtain a reference to a phy.
  700. * @dev: device that requests this phy
  701. * @np: node containing the phy
  702. * @con_id: name of the phy from device's point of view
  703. *
  704. * Gets the phy using of_phy_get(), and associates a device with it using
  705. * devres. On driver detach, release function is invoked on the devres data,
  706. * then, devres data is freed.
  707. */
  708. struct phy *devm_of_phy_get(struct device *dev, struct device_node *np,
  709. const char *con_id)
  710. {
  711. struct phy **ptr, *phy;
  712. struct device_link *link;
  713. ptr = devres_alloc(devm_phy_release, sizeof(*ptr), GFP_KERNEL);
  714. if (!ptr)
  715. return ERR_PTR(-ENOMEM);
  716. phy = of_phy_get(np, con_id);
  717. if (!IS_ERR(phy)) {
  718. *ptr = phy;
  719. devres_add(dev, ptr);
  720. } else {
  721. devres_free(ptr);
  722. return phy;
  723. }
  724. link = device_link_add(dev, &phy->dev, DL_FLAG_STATELESS);
  725. if (!link)
  726. dev_dbg(dev, "failed to create device link to %s\n",
  727. dev_name(phy->dev.parent));
  728. return phy;
  729. }
  730. EXPORT_SYMBOL_GPL(devm_of_phy_get);
  731. /**
  732. * devm_of_phy_get_by_index() - lookup and obtain a reference to a phy by index.
  733. * @dev: device that requests this phy
  734. * @np: node containing the phy
  735. * @index: index of the phy
  736. *
  737. * Gets the phy using _of_phy_get(), then gets a refcount to it,
  738. * and associates a device with it using devres. On driver detach,
  739. * release function is invoked on the devres data,
  740. * then, devres data is freed.
  741. *
  742. */
  743. struct phy *devm_of_phy_get_by_index(struct device *dev, struct device_node *np,
  744. int index)
  745. {
  746. struct phy **ptr, *phy;
  747. struct device_link *link;
  748. ptr = devres_alloc(devm_phy_release, sizeof(*ptr), GFP_KERNEL);
  749. if (!ptr)
  750. return ERR_PTR(-ENOMEM);
  751. phy = _of_phy_get(np, index);
  752. if (IS_ERR(phy)) {
  753. devres_free(ptr);
  754. return phy;
  755. }
  756. if (!try_module_get(phy->ops->owner)) {
  757. devres_free(ptr);
  758. return ERR_PTR(-EPROBE_DEFER);
  759. }
  760. get_device(&phy->dev);
  761. *ptr = phy;
  762. devres_add(dev, ptr);
  763. link = device_link_add(dev, &phy->dev, DL_FLAG_STATELESS);
  764. if (!link)
  765. dev_dbg(dev, "failed to create device link to %s\n",
  766. dev_name(phy->dev.parent));
  767. return phy;
  768. }
  769. EXPORT_SYMBOL_GPL(devm_of_phy_get_by_index);
  770. /**
  771. * phy_create() - create a new phy
  772. * @dev: device that is creating the new phy
  773. * @node: device node of the phy
  774. * @ops: function pointers for performing phy operations
  775. *
  776. * Called to create a phy using phy framework.
  777. */
  778. struct phy *phy_create(struct device *dev, struct device_node *node,
  779. const struct phy_ops *ops)
  780. {
  781. int ret;
  782. int id;
  783. struct phy *phy;
  784. if (WARN_ON(!dev))
  785. return ERR_PTR(-EINVAL);
  786. phy = kzalloc(sizeof(*phy), GFP_KERNEL);
  787. if (!phy)
  788. return ERR_PTR(-ENOMEM);
  789. id = ida_simple_get(&phy_ida, 0, 0, GFP_KERNEL);
  790. if (id < 0) {
  791. dev_err(dev, "unable to get id\n");
  792. ret = id;
  793. goto free_phy;
  794. }
  795. device_initialize(&phy->dev);
  796. mutex_init(&phy->mutex);
  797. phy->dev.class = phy_class;
  798. phy->dev.parent = dev;
  799. phy->dev.of_node = node ?: dev->of_node;
  800. phy->id = id;
  801. phy->ops = ops;
  802. ret = dev_set_name(&phy->dev, "phy-%s.%d", dev_name(dev), id);
  803. if (ret)
  804. goto put_dev;
  805. /* phy-supply */
  806. phy->pwr = regulator_get_optional(&phy->dev, "phy");
  807. if (IS_ERR(phy->pwr)) {
  808. ret = PTR_ERR(phy->pwr);
  809. if (ret == -EPROBE_DEFER)
  810. goto put_dev;
  811. phy->pwr = NULL;
  812. }
  813. ret = device_add(&phy->dev);
  814. if (ret)
  815. goto put_dev;
  816. if (pm_runtime_enabled(dev)) {
  817. pm_runtime_enable(&phy->dev);
  818. pm_runtime_no_callbacks(&phy->dev);
  819. }
  820. return phy;
  821. put_dev:
  822. put_device(&phy->dev); /* calls phy_release() which frees resources */
  823. return ERR_PTR(ret);
  824. free_phy:
  825. kfree(phy);
  826. return ERR_PTR(ret);
  827. }
  828. EXPORT_SYMBOL_GPL(phy_create);
  829. /**
  830. * devm_phy_create() - create a new phy
  831. * @dev: device that is creating the new phy
  832. * @node: device node of the phy
  833. * @ops: function pointers for performing phy operations
  834. *
  835. * Creates a new PHY device adding it to the PHY class.
  836. * While at that, it also associates the device with the phy using devres.
  837. * On driver detach, release function is invoked on the devres data,
  838. * then, devres data is freed.
  839. */
  840. struct phy *devm_phy_create(struct device *dev, struct device_node *node,
  841. const struct phy_ops *ops)
  842. {
  843. struct phy **ptr, *phy;
  844. ptr = devres_alloc(devm_phy_consume, sizeof(*ptr), GFP_KERNEL);
  845. if (!ptr)
  846. return ERR_PTR(-ENOMEM);
  847. phy = phy_create(dev, node, ops);
  848. if (!IS_ERR(phy)) {
  849. *ptr = phy;
  850. devres_add(dev, ptr);
  851. } else {
  852. devres_free(ptr);
  853. }
  854. return phy;
  855. }
  856. EXPORT_SYMBOL_GPL(devm_phy_create);
  857. /**
  858. * phy_destroy() - destroy the phy
  859. * @phy: the phy to be destroyed
  860. *
  861. * Called to destroy the phy.
  862. */
  863. void phy_destroy(struct phy *phy)
  864. {
  865. pm_runtime_disable(&phy->dev);
  866. device_unregister(&phy->dev);
  867. }
  868. EXPORT_SYMBOL_GPL(phy_destroy);
  869. /**
  870. * devm_phy_destroy() - destroy the PHY
  871. * @dev: device that wants to release this phy
  872. * @phy: the phy returned by devm_phy_get()
  873. *
  874. * destroys the devres associated with this phy and invokes phy_destroy
  875. * to destroy the phy.
  876. */
  877. void devm_phy_destroy(struct device *dev, struct phy *phy)
  878. {
  879. int r;
  880. r = devres_destroy(dev, devm_phy_consume, devm_phy_match, phy);
  881. dev_WARN_ONCE(dev, r, "couldn't find PHY resource\n");
  882. }
  883. EXPORT_SYMBOL_GPL(devm_phy_destroy);
  884. /**
  885. * __of_phy_provider_register() - create/register phy provider with the framework
  886. * @dev: struct device of the phy provider
  887. * @children: device node containing children (if different from dev->of_node)
  888. * @owner: the module owner containing of_xlate
  889. * @of_xlate: function pointer to obtain phy instance from phy provider
  890. *
  891. * Creates struct phy_provider from dev and of_xlate function pointer.
  892. * This is used in the case of dt boot for finding the phy instance from
  893. * phy provider.
  894. *
  895. * If the PHY provider doesn't nest children directly but uses a separate
  896. * child node to contain the individual children, the @children parameter
  897. * can be used to override the default. If NULL, the default (dev->of_node)
  898. * will be used. If non-NULL, the device node must be a child (or further
  899. * descendant) of dev->of_node. Otherwise an ERR_PTR()-encoded -EINVAL
  900. * error code is returned.
  901. */
  902. struct phy_provider *__of_phy_provider_register(struct device *dev,
  903. struct device_node *children, struct module *owner,
  904. struct phy * (*of_xlate)(struct device *dev,
  905. struct of_phandle_args *args))
  906. {
  907. struct phy_provider *phy_provider;
  908. /*
  909. * If specified, the device node containing the children must itself
  910. * be the provider's device node or a child (or further descendant)
  911. * thereof.
  912. */
  913. if (children) {
  914. struct device_node *parent = of_node_get(children), *next;
  915. while (parent) {
  916. if (parent == dev->of_node)
  917. break;
  918. next = of_get_parent(parent);
  919. of_node_put(parent);
  920. parent = next;
  921. }
  922. if (!parent)
  923. return ERR_PTR(-EINVAL);
  924. of_node_put(parent);
  925. } else {
  926. children = dev->of_node;
  927. }
  928. phy_provider = kzalloc(sizeof(*phy_provider), GFP_KERNEL);
  929. if (!phy_provider)
  930. return ERR_PTR(-ENOMEM);
  931. phy_provider->dev = dev;
  932. phy_provider->children = of_node_get(children);
  933. phy_provider->owner = owner;
  934. phy_provider->of_xlate = of_xlate;
  935. mutex_lock(&phy_provider_mutex);
  936. list_add_tail(&phy_provider->list, &phy_provider_list);
  937. mutex_unlock(&phy_provider_mutex);
  938. return phy_provider;
  939. }
  940. EXPORT_SYMBOL_GPL(__of_phy_provider_register);
  941. /**
  942. * __devm_of_phy_provider_register() - create/register phy provider with the
  943. * framework
  944. * @dev: struct device of the phy provider
  945. * @children: device node containing children (if different from dev->of_node)
  946. * @owner: the module owner containing of_xlate
  947. * @of_xlate: function pointer to obtain phy instance from phy provider
  948. *
  949. * Creates struct phy_provider from dev and of_xlate function pointer.
  950. * This is used in the case of dt boot for finding the phy instance from
  951. * phy provider. While at that, it also associates the device with the
  952. * phy provider using devres. On driver detach, release function is invoked
  953. * on the devres data, then, devres data is freed.
  954. */
  955. struct phy_provider *__devm_of_phy_provider_register(struct device *dev,
  956. struct device_node *children, struct module *owner,
  957. struct phy * (*of_xlate)(struct device *dev,
  958. struct of_phandle_args *args))
  959. {
  960. struct phy_provider **ptr, *phy_provider;
  961. ptr = devres_alloc(devm_phy_provider_release, sizeof(*ptr), GFP_KERNEL);
  962. if (!ptr)
  963. return ERR_PTR(-ENOMEM);
  964. phy_provider = __of_phy_provider_register(dev, children, owner,
  965. of_xlate);
  966. if (!IS_ERR(phy_provider)) {
  967. *ptr = phy_provider;
  968. devres_add(dev, ptr);
  969. } else {
  970. devres_free(ptr);
  971. }
  972. return phy_provider;
  973. }
  974. EXPORT_SYMBOL_GPL(__devm_of_phy_provider_register);
  975. /**
  976. * of_phy_provider_unregister() - unregister phy provider from the framework
  977. * @phy_provider: phy provider returned by of_phy_provider_register()
  978. *
  979. * Removes the phy_provider created using of_phy_provider_register().
  980. */
  981. void of_phy_provider_unregister(struct phy_provider *phy_provider)
  982. {
  983. if (IS_ERR(phy_provider))
  984. return;
  985. mutex_lock(&phy_provider_mutex);
  986. list_del(&phy_provider->list);
  987. of_node_put(phy_provider->children);
  988. kfree(phy_provider);
  989. mutex_unlock(&phy_provider_mutex);
  990. }
  991. EXPORT_SYMBOL_GPL(of_phy_provider_unregister);
  992. /**
  993. * devm_of_phy_provider_unregister() - remove phy provider from the framework
  994. * @dev: struct device of the phy provider
  995. * @phy_provider: phy provider returned by of_phy_provider_register()
  996. *
  997. * destroys the devres associated with this phy provider and invokes
  998. * of_phy_provider_unregister to unregister the phy provider.
  999. */
  1000. void devm_of_phy_provider_unregister(struct device *dev,
  1001. struct phy_provider *phy_provider)
  1002. {
  1003. int r;
  1004. r = devres_destroy(dev, devm_phy_provider_release, devm_phy_match,
  1005. phy_provider);
  1006. dev_WARN_ONCE(dev, r, "couldn't find PHY provider device resource\n");
  1007. }
  1008. EXPORT_SYMBOL_GPL(devm_of_phy_provider_unregister);
  1009. /**
  1010. * phy_release() - release the phy
  1011. * @dev: the dev member within phy
  1012. *
  1013. * When the last reference to the device is removed, it is called
  1014. * from the embedded kobject as release method.
  1015. */
  1016. static void phy_release(struct device *dev)
  1017. {
  1018. struct phy *phy;
  1019. phy = to_phy(dev);
  1020. dev_vdbg(dev, "releasing '%s'\n", dev_name(dev));
  1021. regulator_put(phy->pwr);
  1022. ida_simple_remove(&phy_ida, phy->id);
  1023. kfree(phy);
  1024. }
  1025. static int __init phy_core_init(void)
  1026. {
  1027. phy_class = class_create(THIS_MODULE, "phy");
  1028. if (IS_ERR(phy_class)) {
  1029. pr_err("failed to create phy class --> %ld\n",
  1030. PTR_ERR(phy_class));
  1031. return PTR_ERR(phy_class);
  1032. }
  1033. phy_class->dev_release = phy_release;
  1034. return 0;
  1035. }
  1036. device_initcall(phy_core_init);