phy-uclass.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright (C) 2017 Texas Instruments Incorporated - http://www.ti.com/
  4. * Written by Jean-Jacques Hiblot <jjhiblot@ti.com>
  5. */
  6. #define LOG_CATEGORY UCLASS_PHY
  7. #include <common.h>
  8. #include <dm.h>
  9. #include <dm/device_compat.h>
  10. #include <dm/devres.h>
  11. #include <generic-phy.h>
  12. #include <linux/list.h>
  13. #include <power/regulator.h>
  14. /**
  15. * struct phy_counts - Init and power-on counts of a single PHY port
  16. *
  17. * This structure is used to keep track of PHY initialization and power
  18. * state change requests, so that we don't power off and deinitialize a
  19. * PHY instance until all of its users want it done. Otherwise, multiple
  20. * consumers using the same PHY port can cause problems (e.g. one might
  21. * call power_off() after another's exit() and hang indefinitely).
  22. *
  23. * @id: The PHY ID within a PHY provider
  24. * @power_on_count: Times generic_phy_power_on() was called for this ID
  25. * without a matching generic_phy_power_off() afterwards
  26. * @init_count: Times generic_phy_init() was called for this ID
  27. * without a matching generic_phy_exit() afterwards
  28. * @list: Handle for a linked list of these structures corresponding to
  29. * ports of the same PHY provider
  30. * @supply: Handle to a phy-supply device
  31. */
  32. struct phy_counts {
  33. unsigned long id;
  34. int power_on_count;
  35. int init_count;
  36. struct list_head list;
  37. struct udevice *supply;
  38. };
  39. static inline struct phy_ops *phy_dev_ops(struct udevice *dev)
  40. {
  41. return (struct phy_ops *)dev->driver->ops;
  42. }
  43. static struct phy_counts *phy_get_counts(struct phy *phy)
  44. {
  45. struct list_head *uc_priv;
  46. struct phy_counts *counts;
  47. if (!generic_phy_valid(phy))
  48. return NULL;
  49. uc_priv = dev_get_uclass_priv(phy->dev);
  50. list_for_each_entry(counts, uc_priv, list)
  51. if (counts->id == phy->id)
  52. return counts;
  53. return NULL;
  54. }
  55. static int phy_alloc_counts(struct phy *phy, struct udevice *supply)
  56. {
  57. struct list_head *uc_priv;
  58. struct phy_counts *counts;
  59. if (!generic_phy_valid(phy))
  60. return 0;
  61. if (phy_get_counts(phy))
  62. return 0;
  63. uc_priv = dev_get_uclass_priv(phy->dev);
  64. counts = kzalloc(sizeof(*counts), GFP_KERNEL);
  65. if (!counts)
  66. return -ENOMEM;
  67. counts->id = phy->id;
  68. counts->power_on_count = 0;
  69. counts->init_count = 0;
  70. counts->supply = supply;
  71. list_add(&counts->list, uc_priv);
  72. return 0;
  73. }
  74. static int phy_uclass_pre_probe(struct udevice *dev)
  75. {
  76. struct list_head *uc_priv = dev_get_uclass_priv(dev);
  77. INIT_LIST_HEAD(uc_priv);
  78. return 0;
  79. }
  80. static int phy_uclass_pre_remove(struct udevice *dev)
  81. {
  82. struct list_head *uc_priv = dev_get_uclass_priv(dev);
  83. struct phy_counts *counts, *next;
  84. list_for_each_entry_safe(counts, next, uc_priv, list)
  85. kfree(counts);
  86. return 0;
  87. }
  88. static int generic_phy_xlate_offs_flags(struct phy *phy,
  89. struct ofnode_phandle_args *args)
  90. {
  91. debug("%s(phy=%p)\n", __func__, phy);
  92. if (args->args_count > 1) {
  93. debug("Invalid args_count: %d\n", args->args_count);
  94. return -EINVAL;
  95. }
  96. if (args->args_count)
  97. phy->id = args->args[0];
  98. else
  99. phy->id = 0;
  100. return 0;
  101. }
  102. int generic_phy_get_by_index_nodev(ofnode node, int index, struct phy *phy)
  103. {
  104. struct ofnode_phandle_args args;
  105. struct phy_ops *ops;
  106. struct udevice *phydev, *supply = NULL;
  107. int i, ret;
  108. debug("%s(node=%s, index=%d, phy=%p)\n",
  109. __func__, ofnode_get_name(node), index, phy);
  110. assert(phy);
  111. phy->dev = NULL;
  112. ret = ofnode_parse_phandle_with_args(node, "phys", "#phy-cells", 0,
  113. index, &args);
  114. if (ret) {
  115. debug("%s: dev_read_phandle_with_args failed: err=%d\n",
  116. __func__, ret);
  117. return ret;
  118. }
  119. ret = uclass_get_device_by_ofnode(UCLASS_PHY, args.node, &phydev);
  120. if (ret) {
  121. debug("%s: uclass_get_device_by_ofnode failed: err=%d\n",
  122. __func__, ret);
  123. /* Check if args.node's parent is a PHY provider */
  124. ret = uclass_get_device_by_ofnode(UCLASS_PHY,
  125. ofnode_get_parent(args.node),
  126. &phydev);
  127. if (ret)
  128. return ret;
  129. /* insert phy idx at first position into args array */
  130. for (i = args.args_count; i >= 1 ; i--)
  131. args.args[i] = args.args[i - 1];
  132. args.args_count++;
  133. args.args[0] = ofnode_read_u32_default(args.node, "reg", -1);
  134. }
  135. phy->dev = phydev;
  136. ops = phy_dev_ops(phydev);
  137. if (ops->of_xlate)
  138. ret = ops->of_xlate(phy, &args);
  139. else
  140. ret = generic_phy_xlate_offs_flags(phy, &args);
  141. if (ret) {
  142. debug("of_xlate() failed: %d\n", ret);
  143. goto err;
  144. }
  145. if (CONFIG_IS_ENABLED(DM_REGULATOR)) {
  146. ret = device_get_supply_regulator(phydev, "phy-supply",
  147. &supply);
  148. if (ret && ret != -ENOENT) {
  149. debug("%s: device_get_supply_regulator failed: %d\n",
  150. __func__, ret);
  151. goto err;
  152. }
  153. }
  154. ret = phy_alloc_counts(phy, supply);
  155. if (ret) {
  156. debug("phy_alloc_counts() failed: %d\n", ret);
  157. goto err;
  158. }
  159. return 0;
  160. err:
  161. return ret;
  162. }
  163. int generic_phy_get_by_index(struct udevice *dev, int index,
  164. struct phy *phy)
  165. {
  166. return generic_phy_get_by_index_nodev(dev_ofnode(dev), index, phy);
  167. }
  168. int generic_phy_get_by_name(struct udevice *dev, const char *phy_name,
  169. struct phy *phy)
  170. {
  171. int index;
  172. debug("%s(dev=%p, name=%s, phy=%p)\n", __func__, dev, phy_name, phy);
  173. index = dev_read_stringlist_search(dev, "phy-names", phy_name);
  174. if (index < 0) {
  175. debug("dev_read_stringlist_search() failed: %d\n", index);
  176. return index;
  177. }
  178. return generic_phy_get_by_index(dev, index, phy);
  179. }
  180. int generic_phy_init(struct phy *phy)
  181. {
  182. struct phy_counts *counts;
  183. struct phy_ops const *ops;
  184. int ret;
  185. if (!generic_phy_valid(phy))
  186. return 0;
  187. counts = phy_get_counts(phy);
  188. if (counts->init_count > 0) {
  189. counts->init_count++;
  190. return 0;
  191. }
  192. ops = phy_dev_ops(phy->dev);
  193. if (ops->init) {
  194. ret = ops->init(phy);
  195. if (ret) {
  196. dev_err(phy->dev, "PHY: Failed to init %s: %d.\n",
  197. phy->dev->name, ret);
  198. return ret;
  199. }
  200. }
  201. counts->init_count = 1;
  202. return 0;
  203. }
  204. int generic_phy_reset(struct phy *phy)
  205. {
  206. struct phy_ops const *ops;
  207. int ret;
  208. if (!generic_phy_valid(phy))
  209. return 0;
  210. ops = phy_dev_ops(phy->dev);
  211. if (!ops->reset)
  212. return 0;
  213. ret = ops->reset(phy);
  214. if (ret)
  215. dev_err(phy->dev, "PHY: Failed to reset %s: %d.\n",
  216. phy->dev->name, ret);
  217. return ret;
  218. }
  219. int generic_phy_exit(struct phy *phy)
  220. {
  221. struct phy_counts *counts;
  222. struct phy_ops const *ops;
  223. int ret;
  224. if (!generic_phy_valid(phy))
  225. return 0;
  226. counts = phy_get_counts(phy);
  227. if (counts->init_count == 0)
  228. return 0;
  229. if (counts->init_count > 1) {
  230. counts->init_count--;
  231. return 0;
  232. }
  233. ops = phy_dev_ops(phy->dev);
  234. if (ops->exit) {
  235. ret = ops->exit(phy);
  236. if (ret) {
  237. dev_err(phy->dev, "PHY: Failed to exit %s: %d.\n",
  238. phy->dev->name, ret);
  239. return ret;
  240. }
  241. }
  242. counts->init_count = 0;
  243. return 0;
  244. }
  245. int generic_phy_power_on(struct phy *phy)
  246. {
  247. struct phy_counts *counts;
  248. struct phy_ops const *ops;
  249. int ret;
  250. if (!generic_phy_valid(phy))
  251. return 0;
  252. counts = phy_get_counts(phy);
  253. if (counts->power_on_count > 0) {
  254. counts->power_on_count++;
  255. return 0;
  256. }
  257. ret = regulator_set_enable_if_allowed(counts->supply, true);
  258. if (ret && ret != -ENOSYS) {
  259. dev_err(phy->dev, "PHY: Failed to enable regulator %s: %d.\n",
  260. counts->supply->name, ret);
  261. return ret;
  262. }
  263. ops = phy_dev_ops(phy->dev);
  264. if (ops->power_on) {
  265. ret = ops->power_on(phy);
  266. if (ret) {
  267. dev_err(phy->dev, "PHY: Failed to power on %s: %d.\n",
  268. phy->dev->name, ret);
  269. regulator_set_enable_if_allowed(counts->supply, false);
  270. return ret;
  271. }
  272. }
  273. counts->power_on_count = 1;
  274. return 0;
  275. }
  276. int generic_phy_power_off(struct phy *phy)
  277. {
  278. struct phy_counts *counts;
  279. struct phy_ops const *ops;
  280. int ret;
  281. if (!generic_phy_valid(phy))
  282. return 0;
  283. counts = phy_get_counts(phy);
  284. if (counts->power_on_count == 0)
  285. return 0;
  286. if (counts->power_on_count > 1) {
  287. counts->power_on_count--;
  288. return 0;
  289. }
  290. ops = phy_dev_ops(phy->dev);
  291. if (ops->power_off) {
  292. ret = ops->power_off(phy);
  293. if (ret) {
  294. dev_err(phy->dev, "PHY: Failed to power off %s: %d.\n",
  295. phy->dev->name, ret);
  296. return ret;
  297. }
  298. }
  299. counts->power_on_count = 0;
  300. ret = regulator_set_enable_if_allowed(counts->supply, false);
  301. if (ret && ret != -ENOSYS)
  302. dev_err(phy->dev, "PHY: Failed to disable regulator %s: %d.\n",
  303. counts->supply->name, ret);
  304. return 0;
  305. }
  306. int generic_phy_configure(struct phy *phy, void *params)
  307. {
  308. struct phy_ops const *ops;
  309. if (!generic_phy_valid(phy))
  310. return 0;
  311. ops = phy_dev_ops(phy->dev);
  312. return ops->configure ? ops->configure(phy, params) : 0;
  313. }
  314. int generic_phy_set_mode(struct phy *phy, enum phy_mode mode, int submode)
  315. {
  316. struct phy_ops const *ops;
  317. if (!generic_phy_valid(phy))
  318. return 0;
  319. ops = phy_dev_ops(phy->dev);
  320. return ops->set_mode ? ops->set_mode(phy, mode, submode) : 0;
  321. }
  322. int generic_phy_set_speed(struct phy *phy, int speed)
  323. {
  324. struct phy_ops const *ops;
  325. if (!generic_phy_valid(phy))
  326. return 0;
  327. ops = phy_dev_ops(phy->dev);
  328. return ops->set_speed ? ops->set_speed(phy, speed) : 0;
  329. }
  330. int generic_phy_get_bulk(struct udevice *dev, struct phy_bulk *bulk)
  331. {
  332. int i, ret, count;
  333. struct udevice *phydev = dev;
  334. bulk->count = 0;
  335. /* Return if no phy declared */
  336. if (!dev_read_prop(dev, "phys", NULL)) {
  337. phydev = dev->parent;
  338. if (!dev_read_prop(phydev, "phys", NULL)) {
  339. pr_err("%s : no phys property\n", __func__);
  340. return 0;
  341. }
  342. }
  343. count = dev_count_phandle_with_args(phydev, "phys", "#phy-cells", 0);
  344. if (count < 1) {
  345. pr_err("%s : no phys found %d\n", __func__, count);
  346. return count;
  347. }
  348. bulk->phys = devm_kcalloc(phydev, count, sizeof(struct phy), GFP_KERNEL);
  349. if (!bulk->phys)
  350. return -ENOMEM;
  351. for (i = 0; i < count; i++) {
  352. ret = generic_phy_get_by_index(phydev, i, &bulk->phys[i]);
  353. if (ret) {
  354. pr_err("Failed to get PHY%d for %s\n", i, dev->name);
  355. return ret;
  356. }
  357. bulk->count++;
  358. }
  359. return 0;
  360. }
  361. int generic_phy_init_bulk(struct phy_bulk *bulk)
  362. {
  363. struct phy *phys = bulk->phys;
  364. int i, ret;
  365. for (i = 0; i < bulk->count; i++) {
  366. ret = generic_phy_init(&phys[i]);
  367. if (ret) {
  368. pr_err("Can't init PHY%d\n", i);
  369. goto phys_init_err;
  370. }
  371. }
  372. return 0;
  373. phys_init_err:
  374. for (; i > 0; i--)
  375. generic_phy_exit(&phys[i - 1]);
  376. return ret;
  377. }
  378. int generic_phy_exit_bulk(struct phy_bulk *bulk)
  379. {
  380. struct phy *phys = bulk->phys;
  381. int i, ret = 0;
  382. for (i = 0; i < bulk->count; i++)
  383. ret |= generic_phy_exit(&phys[i]);
  384. return ret;
  385. }
  386. int generic_phy_power_on_bulk(struct phy_bulk *bulk)
  387. {
  388. struct phy *phys = bulk->phys;
  389. int i, ret;
  390. for (i = 0; i < bulk->count; i++) {
  391. ret = generic_phy_power_on(&phys[i]);
  392. if (ret) {
  393. pr_err("Can't power on PHY%d\n", i);
  394. goto phys_poweron_err;
  395. }
  396. }
  397. return 0;
  398. phys_poweron_err:
  399. for (; i > 0; i--)
  400. generic_phy_power_off(&phys[i - 1]);
  401. return ret;
  402. }
  403. int generic_phy_power_off_bulk(struct phy_bulk *bulk)
  404. {
  405. struct phy *phys = bulk->phys;
  406. int i, ret = 0;
  407. for (i = 0; i < bulk->count; i++)
  408. ret |= generic_phy_power_off(&phys[i]);
  409. return ret;
  410. }
  411. int generic_setup_phy(struct udevice *dev, struct phy *phy, int index)
  412. {
  413. int ret = 0;
  414. if (!phy)
  415. return 0;
  416. ret = generic_phy_get_by_index(dev, index, phy);
  417. if (ret) {
  418. if (ret != -ENOENT)
  419. return ret;
  420. } else {
  421. ret = generic_phy_init(phy);
  422. if (ret)
  423. return ret;
  424. ret = generic_phy_power_on(phy);
  425. if (ret)
  426. ret = generic_phy_exit(phy);
  427. }
  428. return ret;
  429. }
  430. int generic_shutdown_phy(struct phy *phy)
  431. {
  432. int ret = 0;
  433. if (!phy)
  434. return 0;
  435. if (generic_phy_valid(phy)) {
  436. ret = generic_phy_power_off(phy);
  437. if (ret)
  438. return ret;
  439. ret = generic_phy_exit(phy);
  440. }
  441. return ret;
  442. }
  443. UCLASS_DRIVER(phy) = {
  444. .id = UCLASS_PHY,
  445. .name = "phy",
  446. .pre_probe = phy_uclass_pre_probe,
  447. .pre_remove = phy_uclass_pre_remove,
  448. .per_device_auto = sizeof(struct list_head),
  449. };