pse_core.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. //
  3. // Framework for Ethernet Power Sourcing Equipment
  4. //
  5. // Copyright (c) 2022 Pengutronix, Oleksij Rempel <kernel@pengutronix.de>
  6. //
  7. #include <linux/device.h>
  8. #include <linux/of.h>
  9. #include <linux/pse-pd/pse.h>
  10. #include <linux/regulator/driver.h>
  11. #include <linux/regulator/machine.h>
  12. static DEFINE_MUTEX(pse_list_mutex);
  13. static LIST_HEAD(pse_controller_list);
  14. /**
  15. * struct pse_control - a PSE control
  16. * @pcdev: a pointer to the PSE controller device
  17. * this PSE control belongs to
  18. * @ps: PSE PI supply of the PSE control
  19. * @list: list entry for the pcdev's PSE controller list
  20. * @id: ID of the PSE line in the PSE controller device
  21. * @refcnt: Number of gets of this pse_control
  22. */
  23. struct pse_control {
  24. struct pse_controller_dev *pcdev;
  25. struct regulator *ps;
  26. struct list_head list;
  27. unsigned int id;
  28. struct kref refcnt;
  29. };
  30. static int of_load_single_pse_pi_pairset(struct device_node *node,
  31. struct pse_pi *pi,
  32. int pairset_num)
  33. {
  34. struct device_node *pairset_np;
  35. const char *name;
  36. int ret;
  37. ret = of_property_read_string_index(node, "pairset-names",
  38. pairset_num, &name);
  39. if (ret)
  40. return ret;
  41. if (!strcmp(name, "alternative-a")) {
  42. pi->pairset[pairset_num].pinout = ALTERNATIVE_A;
  43. } else if (!strcmp(name, "alternative-b")) {
  44. pi->pairset[pairset_num].pinout = ALTERNATIVE_B;
  45. } else {
  46. pr_err("pse: wrong pairset-names value %s (%pOF)\n",
  47. name, node);
  48. return -EINVAL;
  49. }
  50. pairset_np = of_parse_phandle(node, "pairsets", pairset_num);
  51. if (!pairset_np)
  52. return -ENODEV;
  53. pi->pairset[pairset_num].np = pairset_np;
  54. return 0;
  55. }
  56. /**
  57. * of_load_pse_pi_pairsets - load PSE PI pairsets pinout and polarity
  58. * @node: a pointer of the device node
  59. * @pi: a pointer of the PSE PI to fill
  60. * @npairsets: the number of pairsets (1 or 2) used by the PI
  61. *
  62. * Return: 0 on success and failure value on error
  63. */
  64. static int of_load_pse_pi_pairsets(struct device_node *node,
  65. struct pse_pi *pi,
  66. int npairsets)
  67. {
  68. int i, ret;
  69. ret = of_property_count_strings(node, "pairset-names");
  70. if (ret != npairsets) {
  71. pr_err("pse: amount of pairsets and pairset-names is not equal %d != %d (%pOF)\n",
  72. npairsets, ret, node);
  73. return -EINVAL;
  74. }
  75. for (i = 0; i < npairsets; i++) {
  76. ret = of_load_single_pse_pi_pairset(node, pi, i);
  77. if (ret)
  78. goto out;
  79. }
  80. if (npairsets == 2 &&
  81. pi->pairset[0].pinout == pi->pairset[1].pinout) {
  82. pr_err("pse: two PI pairsets can not have identical pinout (%pOF)",
  83. node);
  84. ret = -EINVAL;
  85. }
  86. out:
  87. /* If an error appears, release all the pairset device node kref */
  88. if (ret) {
  89. of_node_put(pi->pairset[0].np);
  90. pi->pairset[0].np = NULL;
  91. of_node_put(pi->pairset[1].np);
  92. pi->pairset[1].np = NULL;
  93. }
  94. return ret;
  95. }
  96. static void pse_release_pis(struct pse_controller_dev *pcdev)
  97. {
  98. int i;
  99. for (i = 0; i < pcdev->nr_lines; i++) {
  100. of_node_put(pcdev->pi[i].pairset[0].np);
  101. of_node_put(pcdev->pi[i].pairset[1].np);
  102. of_node_put(pcdev->pi[i].np);
  103. }
  104. kfree(pcdev->pi);
  105. }
  106. /**
  107. * of_load_pse_pis - load all the PSE PIs
  108. * @pcdev: a pointer to the PSE controller device
  109. *
  110. * Return: 0 on success and failure value on error
  111. */
  112. static int of_load_pse_pis(struct pse_controller_dev *pcdev)
  113. {
  114. struct device_node *np = pcdev->dev->of_node;
  115. struct device_node *node, *pis;
  116. int ret;
  117. if (!np)
  118. return -ENODEV;
  119. pcdev->pi = kcalloc(pcdev->nr_lines, sizeof(*pcdev->pi), GFP_KERNEL);
  120. if (!pcdev->pi)
  121. return -ENOMEM;
  122. pis = of_get_child_by_name(np, "pse-pis");
  123. if (!pis) {
  124. /* no description of PSE PIs */
  125. pcdev->no_of_pse_pi = true;
  126. return 0;
  127. }
  128. for_each_child_of_node(pis, node) {
  129. struct pse_pi pi = {0};
  130. u32 id;
  131. if (!of_node_name_eq(node, "pse-pi"))
  132. continue;
  133. ret = of_property_read_u32(node, "reg", &id);
  134. if (ret) {
  135. dev_err(pcdev->dev,
  136. "can't get reg property for node '%pOF'",
  137. node);
  138. goto out;
  139. }
  140. if (id >= pcdev->nr_lines) {
  141. dev_err(pcdev->dev,
  142. "reg value (%u) is out of range (%u) (%pOF)\n",
  143. id, pcdev->nr_lines, node);
  144. ret = -EINVAL;
  145. goto out;
  146. }
  147. if (pcdev->pi[id].np) {
  148. dev_err(pcdev->dev,
  149. "other node with same reg value was already registered. %pOF : %pOF\n",
  150. pcdev->pi[id].np, node);
  151. ret = -EINVAL;
  152. goto out;
  153. }
  154. ret = of_count_phandle_with_args(node, "pairsets", NULL);
  155. /* npairsets is limited to value one or two */
  156. if (ret == 1 || ret == 2) {
  157. ret = of_load_pse_pi_pairsets(node, &pi, ret);
  158. if (ret)
  159. goto out;
  160. } else if (ret != ENOENT) {
  161. dev_err(pcdev->dev,
  162. "error: wrong number of pairsets. Should be 1 or 2, got %d (%pOF)\n",
  163. ret, node);
  164. ret = -EINVAL;
  165. goto out;
  166. }
  167. of_node_get(node);
  168. pi.np = node;
  169. memcpy(&pcdev->pi[id], &pi, sizeof(pi));
  170. }
  171. of_node_put(pis);
  172. return 0;
  173. out:
  174. pse_release_pis(pcdev);
  175. of_node_put(node);
  176. of_node_put(pis);
  177. return ret;
  178. }
  179. static int pse_pi_is_enabled(struct regulator_dev *rdev)
  180. {
  181. struct pse_controller_dev *pcdev = rdev_get_drvdata(rdev);
  182. const struct pse_controller_ops *ops;
  183. int id, ret;
  184. ops = pcdev->ops;
  185. if (!ops->pi_is_enabled)
  186. return -EOPNOTSUPP;
  187. id = rdev_get_id(rdev);
  188. mutex_lock(&pcdev->lock);
  189. ret = ops->pi_is_enabled(pcdev, id);
  190. mutex_unlock(&pcdev->lock);
  191. return ret;
  192. }
  193. static int pse_pi_enable(struct regulator_dev *rdev)
  194. {
  195. struct pse_controller_dev *pcdev = rdev_get_drvdata(rdev);
  196. const struct pse_controller_ops *ops;
  197. int id, ret;
  198. ops = pcdev->ops;
  199. if (!ops->pi_enable)
  200. return -EOPNOTSUPP;
  201. id = rdev_get_id(rdev);
  202. mutex_lock(&pcdev->lock);
  203. ret = ops->pi_enable(pcdev, id);
  204. if (!ret)
  205. pcdev->pi[id].admin_state_enabled = 1;
  206. mutex_unlock(&pcdev->lock);
  207. return ret;
  208. }
  209. static int pse_pi_disable(struct regulator_dev *rdev)
  210. {
  211. struct pse_controller_dev *pcdev = rdev_get_drvdata(rdev);
  212. const struct pse_controller_ops *ops;
  213. int id, ret;
  214. ops = pcdev->ops;
  215. if (!ops->pi_disable)
  216. return -EOPNOTSUPP;
  217. id = rdev_get_id(rdev);
  218. mutex_lock(&pcdev->lock);
  219. ret = ops->pi_disable(pcdev, id);
  220. if (!ret)
  221. pcdev->pi[id].admin_state_enabled = 0;
  222. mutex_unlock(&pcdev->lock);
  223. return ret;
  224. }
  225. static int _pse_pi_get_voltage(struct regulator_dev *rdev)
  226. {
  227. struct pse_controller_dev *pcdev = rdev_get_drvdata(rdev);
  228. const struct pse_controller_ops *ops;
  229. int id;
  230. ops = pcdev->ops;
  231. if (!ops->pi_get_voltage)
  232. return -EOPNOTSUPP;
  233. id = rdev_get_id(rdev);
  234. return ops->pi_get_voltage(pcdev, id);
  235. }
  236. static int pse_pi_get_voltage(struct regulator_dev *rdev)
  237. {
  238. struct pse_controller_dev *pcdev = rdev_get_drvdata(rdev);
  239. int ret;
  240. mutex_lock(&pcdev->lock);
  241. ret = _pse_pi_get_voltage(rdev);
  242. mutex_unlock(&pcdev->lock);
  243. return ret;
  244. }
  245. static int pse_pi_get_current_limit(struct regulator_dev *rdev)
  246. {
  247. struct pse_controller_dev *pcdev = rdev_get_drvdata(rdev);
  248. const struct pse_controller_ops *ops;
  249. int id, uV, mW, ret;
  250. s64 tmp_64;
  251. ops = pcdev->ops;
  252. id = rdev_get_id(rdev);
  253. if (!ops->pi_get_pw_limit || !ops->pi_get_voltage)
  254. return -EOPNOTSUPP;
  255. mutex_lock(&pcdev->lock);
  256. ret = ops->pi_get_pw_limit(pcdev, id);
  257. if (ret < 0)
  258. goto out;
  259. mW = ret;
  260. ret = _pse_pi_get_voltage(rdev);
  261. if (!ret) {
  262. dev_err(pcdev->dev, "Voltage null\n");
  263. ret = -ERANGE;
  264. goto out;
  265. }
  266. if (ret < 0)
  267. goto out;
  268. uV = ret;
  269. tmp_64 = mW;
  270. tmp_64 *= 1000000000ull;
  271. /* uA = mW * 1000000000 / uV */
  272. ret = DIV_ROUND_CLOSEST_ULL(tmp_64, uV);
  273. out:
  274. mutex_unlock(&pcdev->lock);
  275. return ret;
  276. }
  277. static int pse_pi_set_current_limit(struct regulator_dev *rdev, int min_uA,
  278. int max_uA)
  279. {
  280. struct pse_controller_dev *pcdev = rdev_get_drvdata(rdev);
  281. const struct pse_controller_ops *ops;
  282. int id, mW, ret;
  283. s64 tmp_64;
  284. ops = pcdev->ops;
  285. if (!ops->pi_set_pw_limit || !ops->pi_get_voltage)
  286. return -EOPNOTSUPP;
  287. if (max_uA > MAX_PI_CURRENT)
  288. return -ERANGE;
  289. id = rdev_get_id(rdev);
  290. mutex_lock(&pcdev->lock);
  291. ret = _pse_pi_get_voltage(rdev);
  292. if (!ret) {
  293. dev_err(pcdev->dev, "Voltage null\n");
  294. ret = -ERANGE;
  295. goto out;
  296. }
  297. if (ret < 0)
  298. goto out;
  299. tmp_64 = ret;
  300. tmp_64 *= max_uA;
  301. /* mW = uA * uV / 1000000000 */
  302. mW = DIV_ROUND_CLOSEST_ULL(tmp_64, 1000000000);
  303. ret = ops->pi_set_pw_limit(pcdev, id, mW);
  304. out:
  305. mutex_unlock(&pcdev->lock);
  306. return ret;
  307. }
  308. static const struct regulator_ops pse_pi_ops = {
  309. .is_enabled = pse_pi_is_enabled,
  310. .enable = pse_pi_enable,
  311. .disable = pse_pi_disable,
  312. .get_voltage = pse_pi_get_voltage,
  313. .get_current_limit = pse_pi_get_current_limit,
  314. .set_current_limit = pse_pi_set_current_limit,
  315. };
  316. static int
  317. devm_pse_pi_regulator_register(struct pse_controller_dev *pcdev,
  318. char *name, int id)
  319. {
  320. struct regulator_init_data *rinit_data;
  321. struct regulator_config rconfig = {0};
  322. struct regulator_desc *rdesc;
  323. struct regulator_dev *rdev;
  324. rinit_data = devm_kzalloc(pcdev->dev, sizeof(*rinit_data),
  325. GFP_KERNEL);
  326. if (!rinit_data)
  327. return -ENOMEM;
  328. rdesc = devm_kzalloc(pcdev->dev, sizeof(*rdesc), GFP_KERNEL);
  329. if (!rdesc)
  330. return -ENOMEM;
  331. /* Regulator descriptor id have to be the same as its associated
  332. * PSE PI id for the well functioning of the PSE controls.
  333. */
  334. rdesc->id = id;
  335. rdesc->name = name;
  336. rdesc->type = REGULATOR_VOLTAGE;
  337. rdesc->ops = &pse_pi_ops;
  338. rdesc->owner = pcdev->owner;
  339. rinit_data->constraints.valid_ops_mask = REGULATOR_CHANGE_STATUS;
  340. if (pcdev->ops->pi_set_pw_limit)
  341. rinit_data->constraints.valid_ops_mask |=
  342. REGULATOR_CHANGE_CURRENT;
  343. rinit_data->supply_regulator = "vpwr";
  344. rconfig.dev = pcdev->dev;
  345. rconfig.driver_data = pcdev;
  346. rconfig.init_data = rinit_data;
  347. rdev = devm_regulator_register(pcdev->dev, rdesc, &rconfig);
  348. if (IS_ERR(rdev)) {
  349. dev_err_probe(pcdev->dev, PTR_ERR(rdev),
  350. "Failed to register regulator\n");
  351. return PTR_ERR(rdev);
  352. }
  353. pcdev->pi[id].rdev = rdev;
  354. return 0;
  355. }
  356. /**
  357. * pse_controller_register - register a PSE controller device
  358. * @pcdev: a pointer to the initialized PSE controller device
  359. *
  360. * Return: 0 on success and failure value on error
  361. */
  362. int pse_controller_register(struct pse_controller_dev *pcdev)
  363. {
  364. size_t reg_name_len;
  365. int ret, i;
  366. mutex_init(&pcdev->lock);
  367. INIT_LIST_HEAD(&pcdev->pse_control_head);
  368. if (!pcdev->nr_lines)
  369. pcdev->nr_lines = 1;
  370. ret = of_load_pse_pis(pcdev);
  371. if (ret)
  372. return ret;
  373. if (pcdev->ops->setup_pi_matrix) {
  374. ret = pcdev->ops->setup_pi_matrix(pcdev);
  375. if (ret)
  376. return ret;
  377. }
  378. /* Each regulator name len is pcdev dev name + 7 char +
  379. * int max digit number (10) + 1
  380. */
  381. reg_name_len = strlen(dev_name(pcdev->dev)) + 18;
  382. /* Register PI regulators */
  383. for (i = 0; i < pcdev->nr_lines; i++) {
  384. char *reg_name;
  385. /* Do not register regulator for PIs not described */
  386. if (!pcdev->no_of_pse_pi && !pcdev->pi[i].np)
  387. continue;
  388. reg_name = devm_kzalloc(pcdev->dev, reg_name_len, GFP_KERNEL);
  389. if (!reg_name)
  390. return -ENOMEM;
  391. snprintf(reg_name, reg_name_len, "pse-%s_pi%d",
  392. dev_name(pcdev->dev), i);
  393. ret = devm_pse_pi_regulator_register(pcdev, reg_name, i);
  394. if (ret)
  395. return ret;
  396. }
  397. mutex_lock(&pse_list_mutex);
  398. list_add(&pcdev->list, &pse_controller_list);
  399. mutex_unlock(&pse_list_mutex);
  400. return 0;
  401. }
  402. EXPORT_SYMBOL_GPL(pse_controller_register);
  403. /**
  404. * pse_controller_unregister - unregister a PSE controller device
  405. * @pcdev: a pointer to the PSE controller device
  406. */
  407. void pse_controller_unregister(struct pse_controller_dev *pcdev)
  408. {
  409. pse_release_pis(pcdev);
  410. mutex_lock(&pse_list_mutex);
  411. list_del(&pcdev->list);
  412. mutex_unlock(&pse_list_mutex);
  413. }
  414. EXPORT_SYMBOL_GPL(pse_controller_unregister);
  415. static void devm_pse_controller_release(struct device *dev, void *res)
  416. {
  417. pse_controller_unregister(*(struct pse_controller_dev **)res);
  418. }
  419. /**
  420. * devm_pse_controller_register - resource managed pse_controller_register()
  421. * @dev: device that is registering this PSE controller
  422. * @pcdev: a pointer to the initialized PSE controller device
  423. *
  424. * Managed pse_controller_register(). For PSE controllers registered by
  425. * this function, pse_controller_unregister() is automatically called on
  426. * driver detach. See pse_controller_register() for more information.
  427. *
  428. * Return: 0 on success and failure value on error
  429. */
  430. int devm_pse_controller_register(struct device *dev,
  431. struct pse_controller_dev *pcdev)
  432. {
  433. struct pse_controller_dev **pcdevp;
  434. int ret;
  435. pcdevp = devres_alloc(devm_pse_controller_release, sizeof(*pcdevp),
  436. GFP_KERNEL);
  437. if (!pcdevp)
  438. return -ENOMEM;
  439. ret = pse_controller_register(pcdev);
  440. if (ret) {
  441. devres_free(pcdevp);
  442. return ret;
  443. }
  444. *pcdevp = pcdev;
  445. devres_add(dev, pcdevp);
  446. return 0;
  447. }
  448. EXPORT_SYMBOL_GPL(devm_pse_controller_register);
  449. /* PSE control section */
  450. static void __pse_control_release(struct kref *kref)
  451. {
  452. struct pse_control *psec = container_of(kref, struct pse_control,
  453. refcnt);
  454. lockdep_assert_held(&pse_list_mutex);
  455. if (psec->pcdev->pi[psec->id].admin_state_enabled)
  456. regulator_disable(psec->ps);
  457. devm_regulator_put(psec->ps);
  458. module_put(psec->pcdev->owner);
  459. list_del(&psec->list);
  460. kfree(psec);
  461. }
  462. static void __pse_control_put_internal(struct pse_control *psec)
  463. {
  464. lockdep_assert_held(&pse_list_mutex);
  465. kref_put(&psec->refcnt, __pse_control_release);
  466. }
  467. /**
  468. * pse_control_put - free the PSE control
  469. * @psec: PSE control pointer
  470. */
  471. void pse_control_put(struct pse_control *psec)
  472. {
  473. if (IS_ERR_OR_NULL(psec))
  474. return;
  475. mutex_lock(&pse_list_mutex);
  476. __pse_control_put_internal(psec);
  477. mutex_unlock(&pse_list_mutex);
  478. }
  479. EXPORT_SYMBOL_GPL(pse_control_put);
  480. static struct pse_control *
  481. pse_control_get_internal(struct pse_controller_dev *pcdev, unsigned int index)
  482. {
  483. struct pse_control *psec;
  484. int ret;
  485. lockdep_assert_held(&pse_list_mutex);
  486. list_for_each_entry(psec, &pcdev->pse_control_head, list) {
  487. if (psec->id == index) {
  488. kref_get(&psec->refcnt);
  489. return psec;
  490. }
  491. }
  492. psec = kzalloc(sizeof(*psec), GFP_KERNEL);
  493. if (!psec)
  494. return ERR_PTR(-ENOMEM);
  495. if (!try_module_get(pcdev->owner)) {
  496. ret = -ENODEV;
  497. goto free_psec;
  498. }
  499. psec->ps = devm_regulator_get_exclusive(pcdev->dev,
  500. rdev_get_name(pcdev->pi[index].rdev));
  501. if (IS_ERR(psec->ps)) {
  502. ret = PTR_ERR(psec->ps);
  503. goto put_module;
  504. }
  505. ret = regulator_is_enabled(psec->ps);
  506. if (ret < 0)
  507. goto regulator_put;
  508. pcdev->pi[index].admin_state_enabled = ret;
  509. psec->pcdev = pcdev;
  510. list_add(&psec->list, &pcdev->pse_control_head);
  511. psec->id = index;
  512. kref_init(&psec->refcnt);
  513. return psec;
  514. regulator_put:
  515. devm_regulator_put(psec->ps);
  516. put_module:
  517. module_put(pcdev->owner);
  518. free_psec:
  519. kfree(psec);
  520. return ERR_PTR(ret);
  521. }
  522. /**
  523. * of_pse_match_pi - Find the PSE PI id matching the device node phandle
  524. * @pcdev: a pointer to the PSE controller device
  525. * @np: a pointer to the device node
  526. *
  527. * Return: id of the PSE PI, -EINVAL if not found
  528. */
  529. static int of_pse_match_pi(struct pse_controller_dev *pcdev,
  530. struct device_node *np)
  531. {
  532. int i;
  533. for (i = 0; i < pcdev->nr_lines; i++) {
  534. if (pcdev->pi[i].np == np)
  535. return i;
  536. }
  537. return -EINVAL;
  538. }
  539. /**
  540. * psec_id_xlate - translate pse_spec to the PSE line number according
  541. * to the number of pse-cells in case of no pse_pi node
  542. * @pcdev: a pointer to the PSE controller device
  543. * @pse_spec: PSE line specifier as found in the device tree
  544. *
  545. * Return: 0 if #pse-cells = <0>. Return PSE line number otherwise.
  546. */
  547. static int psec_id_xlate(struct pse_controller_dev *pcdev,
  548. const struct of_phandle_args *pse_spec)
  549. {
  550. if (!pcdev->of_pse_n_cells)
  551. return 0;
  552. if (pcdev->of_pse_n_cells > 1 ||
  553. pse_spec->args[0] >= pcdev->nr_lines)
  554. return -EINVAL;
  555. return pse_spec->args[0];
  556. }
  557. struct pse_control *of_pse_control_get(struct device_node *node)
  558. {
  559. struct pse_controller_dev *r, *pcdev;
  560. struct of_phandle_args args;
  561. struct pse_control *psec;
  562. int psec_id;
  563. int ret;
  564. if (!node)
  565. return ERR_PTR(-EINVAL);
  566. ret = of_parse_phandle_with_args(node, "pses", "#pse-cells", 0, &args);
  567. if (ret)
  568. return ERR_PTR(ret);
  569. mutex_lock(&pse_list_mutex);
  570. pcdev = NULL;
  571. list_for_each_entry(r, &pse_controller_list, list) {
  572. if (!r->no_of_pse_pi) {
  573. ret = of_pse_match_pi(r, args.np);
  574. if (ret >= 0) {
  575. pcdev = r;
  576. psec_id = ret;
  577. break;
  578. }
  579. } else if (args.np == r->dev->of_node) {
  580. pcdev = r;
  581. break;
  582. }
  583. }
  584. if (!pcdev) {
  585. psec = ERR_PTR(-EPROBE_DEFER);
  586. goto out;
  587. }
  588. if (WARN_ON(args.args_count != pcdev->of_pse_n_cells)) {
  589. psec = ERR_PTR(-EINVAL);
  590. goto out;
  591. }
  592. if (pcdev->no_of_pse_pi) {
  593. psec_id = psec_id_xlate(pcdev, &args);
  594. if (psec_id < 0) {
  595. psec = ERR_PTR(psec_id);
  596. goto out;
  597. }
  598. }
  599. /* pse_list_mutex also protects the pcdev's pse_control list */
  600. psec = pse_control_get_internal(pcdev, psec_id);
  601. out:
  602. mutex_unlock(&pse_list_mutex);
  603. of_node_put(args.np);
  604. return psec;
  605. }
  606. EXPORT_SYMBOL_GPL(of_pse_control_get);
  607. /**
  608. * pse_ethtool_get_status - get status of PSE control
  609. * @psec: PSE control pointer
  610. * @extack: extack for reporting useful error messages
  611. * @status: struct to store PSE status
  612. *
  613. * Return: 0 on success and failure value on error
  614. */
  615. int pse_ethtool_get_status(struct pse_control *psec,
  616. struct netlink_ext_ack *extack,
  617. struct pse_control_status *status)
  618. {
  619. const struct pse_controller_ops *ops;
  620. struct pse_controller_dev *pcdev;
  621. int err;
  622. pcdev = psec->pcdev;
  623. ops = pcdev->ops;
  624. if (!ops->ethtool_get_status) {
  625. NL_SET_ERR_MSG(extack,
  626. "PSE driver does not support status report");
  627. return -EOPNOTSUPP;
  628. }
  629. mutex_lock(&pcdev->lock);
  630. err = ops->ethtool_get_status(pcdev, psec->id, extack, status);
  631. mutex_unlock(&pcdev->lock);
  632. return err;
  633. }
  634. EXPORT_SYMBOL_GPL(pse_ethtool_get_status);
  635. static int pse_ethtool_c33_set_config(struct pse_control *psec,
  636. const struct pse_control_config *config)
  637. {
  638. int err = 0;
  639. /* Look at admin_state_enabled status to not call regulator_enable
  640. * or regulator_disable twice creating a regulator counter mismatch
  641. */
  642. switch (config->c33_admin_control) {
  643. case ETHTOOL_C33_PSE_ADMIN_STATE_ENABLED:
  644. /* We could have mismatch between admin_state_enabled and
  645. * state reported by regulator_is_enabled. This can occur when
  646. * the PI is forcibly turn off by the controller. Call
  647. * regulator_disable on that case to fix the counters state.
  648. */
  649. if (psec->pcdev->pi[psec->id].admin_state_enabled &&
  650. !regulator_is_enabled(psec->ps)) {
  651. err = regulator_disable(psec->ps);
  652. if (err)
  653. break;
  654. }
  655. if (!psec->pcdev->pi[psec->id].admin_state_enabled)
  656. err = regulator_enable(psec->ps);
  657. break;
  658. case ETHTOOL_C33_PSE_ADMIN_STATE_DISABLED:
  659. if (psec->pcdev->pi[psec->id].admin_state_enabled)
  660. err = regulator_disable(psec->ps);
  661. break;
  662. default:
  663. err = -EOPNOTSUPP;
  664. }
  665. return err;
  666. }
  667. static int pse_ethtool_podl_set_config(struct pse_control *psec,
  668. const struct pse_control_config *config)
  669. {
  670. int err = 0;
  671. /* Look at admin_state_enabled status to not call regulator_enable
  672. * or regulator_disable twice creating a regulator counter mismatch
  673. */
  674. switch (config->podl_admin_control) {
  675. case ETHTOOL_PODL_PSE_ADMIN_STATE_ENABLED:
  676. if (!psec->pcdev->pi[psec->id].admin_state_enabled)
  677. err = regulator_enable(psec->ps);
  678. break;
  679. case ETHTOOL_PODL_PSE_ADMIN_STATE_DISABLED:
  680. if (psec->pcdev->pi[psec->id].admin_state_enabled)
  681. err = regulator_disable(psec->ps);
  682. break;
  683. default:
  684. err = -EOPNOTSUPP;
  685. }
  686. return err;
  687. }
  688. /**
  689. * pse_ethtool_set_config - set PSE control configuration
  690. * @psec: PSE control pointer
  691. * @extack: extack for reporting useful error messages
  692. * @config: Configuration of the test to run
  693. *
  694. * Return: 0 on success and failure value on error
  695. */
  696. int pse_ethtool_set_config(struct pse_control *psec,
  697. struct netlink_ext_ack *extack,
  698. const struct pse_control_config *config)
  699. {
  700. int err = 0;
  701. if (pse_has_c33(psec) && config->c33_admin_control) {
  702. err = pse_ethtool_c33_set_config(psec, config);
  703. if (err)
  704. return err;
  705. }
  706. if (pse_has_podl(psec) && config->podl_admin_control)
  707. err = pse_ethtool_podl_set_config(psec, config);
  708. return err;
  709. }
  710. EXPORT_SYMBOL_GPL(pse_ethtool_set_config);
  711. /**
  712. * pse_ethtool_set_pw_limit - set PSE control power limit
  713. * @psec: PSE control pointer
  714. * @extack: extack for reporting useful error messages
  715. * @pw_limit: power limit value in mW
  716. *
  717. * Return: 0 on success and failure value on error
  718. */
  719. int pse_ethtool_set_pw_limit(struct pse_control *psec,
  720. struct netlink_ext_ack *extack,
  721. const unsigned int pw_limit)
  722. {
  723. int uV, uA, ret;
  724. s64 tmp_64;
  725. ret = regulator_get_voltage(psec->ps);
  726. if (!ret) {
  727. NL_SET_ERR_MSG(extack,
  728. "Can't calculate the current, PSE voltage read is 0");
  729. return -ERANGE;
  730. }
  731. if (ret < 0) {
  732. NL_SET_ERR_MSG(extack,
  733. "Error reading PSE voltage");
  734. return ret;
  735. }
  736. uV = ret;
  737. tmp_64 = pw_limit;
  738. tmp_64 *= 1000000000ull;
  739. /* uA = mW * 1000000000 / uV */
  740. uA = DIV_ROUND_CLOSEST_ULL(tmp_64, uV);
  741. return regulator_set_current_limit(psec->ps, 0, uA);
  742. }
  743. EXPORT_SYMBOL_GPL(pse_ethtool_set_pw_limit);
  744. bool pse_has_podl(struct pse_control *psec)
  745. {
  746. return psec->pcdev->types & ETHTOOL_PSE_PODL;
  747. }
  748. EXPORT_SYMBOL_GPL(pse_has_podl);
  749. bool pse_has_c33(struct pse_control *psec)
  750. {
  751. return psec->pcdev->types & ETHTOOL_PSE_C33;
  752. }
  753. EXPORT_SYMBOL_GPL(pse_has_c33);