/drivers/regulator/core.c

https://bitbucket.org/wisechild/galaxy-nexus · C · 2961 lines · 1949 code · 426 blank · 586 comment · 401 complexity · 2b3d507c313a74f8f6669437141af36c MD5 · raw file

Large files are truncated click here to view the full file

  1. /*
  2. * core.c -- Voltage/Current Regulator framework.
  3. *
  4. * Copyright 2007, 2008 Wolfson Microelectronics PLC.
  5. * Copyright 2008 SlimLogic Ltd.
  6. *
  7. * Author: Liam Girdwood <lrg@slimlogic.co.uk>
  8. *
  9. * This program is free software; you can redistribute it and/or modify it
  10. * under the terms of the GNU General Public License as published by the
  11. * Free Software Foundation; either version 2 of the License, or (at your
  12. * option) any later version.
  13. *
  14. */
  15. #define pr_fmt(fmt) "%s: " fmt, __func__
  16. #include <linux/kernel.h>
  17. #include <linux/init.h>
  18. #include <linux/debugfs.h>
  19. #include <linux/device.h>
  20. #include <linux/slab.h>
  21. #include <linux/err.h>
  22. #include <linux/mutex.h>
  23. #include <linux/suspend.h>
  24. #include <linux/delay.h>
  25. #include <linux/regulator/consumer.h>
  26. #include <linux/regulator/driver.h>
  27. #include <linux/regulator/machine.h>
  28. #define CREATE_TRACE_POINTS
  29. #include <trace/events/regulator.h>
  30. #include "dummy.h"
  31. #define rdev_err(rdev, fmt, ...) \
  32. pr_err("%s: " fmt, rdev_get_name(rdev), ##__VA_ARGS__)
  33. #define rdev_warn(rdev, fmt, ...) \
  34. pr_warn("%s: " fmt, rdev_get_name(rdev), ##__VA_ARGS__)
  35. #define rdev_info(rdev, fmt, ...) \
  36. pr_info("%s: " fmt, rdev_get_name(rdev), ##__VA_ARGS__)
  37. #define rdev_dbg(rdev, fmt, ...) \
  38. pr_debug("%s: " fmt, rdev_get_name(rdev), ##__VA_ARGS__)
  39. static DEFINE_MUTEX(regulator_list_mutex);
  40. static LIST_HEAD(regulator_list);
  41. static LIST_HEAD(regulator_map_list);
  42. static bool has_full_constraints;
  43. static bool board_wants_dummy_regulator;
  44. #ifdef CONFIG_DEBUG_FS
  45. static struct dentry *debugfs_root;
  46. #endif
  47. /*
  48. * struct regulator_map
  49. *
  50. * Used to provide symbolic supply names to devices.
  51. */
  52. struct regulator_map {
  53. struct list_head list;
  54. const char *dev_name; /* The dev_name() for the consumer */
  55. const char *supply;
  56. struct regulator_dev *regulator;
  57. };
  58. /*
  59. * struct regulator
  60. *
  61. * One for each consumer device.
  62. */
  63. struct regulator {
  64. struct device *dev;
  65. struct list_head list;
  66. int uA_load;
  67. int min_uV;
  68. int max_uV;
  69. char *supply_name;
  70. struct device_attribute dev_attr;
  71. struct regulator_dev *rdev;
  72. };
  73. static int _regulator_is_enabled(struct regulator_dev *rdev);
  74. static int _regulator_disable(struct regulator_dev *rdev,
  75. struct regulator_dev **supply_rdev_ptr);
  76. static int _regulator_get_voltage(struct regulator_dev *rdev);
  77. static int _regulator_get_current_limit(struct regulator_dev *rdev);
  78. static unsigned int _regulator_get_mode(struct regulator_dev *rdev);
  79. static void _notifier_call_chain(struct regulator_dev *rdev,
  80. unsigned long event, void *data);
  81. static int _regulator_do_set_voltage(struct regulator_dev *rdev,
  82. int min_uV, int max_uV);
  83. static const char *rdev_get_name(struct regulator_dev *rdev)
  84. {
  85. if (rdev->constraints && rdev->constraints->name)
  86. return rdev->constraints->name;
  87. else if (rdev->desc->name)
  88. return rdev->desc->name;
  89. else
  90. return "";
  91. }
  92. /* gets the regulator for a given consumer device */
  93. static struct regulator *get_device_regulator(struct device *dev)
  94. {
  95. struct regulator *regulator = NULL;
  96. struct regulator_dev *rdev;
  97. mutex_lock(&regulator_list_mutex);
  98. list_for_each_entry(rdev, &regulator_list, list) {
  99. mutex_lock(&rdev->mutex);
  100. list_for_each_entry(regulator, &rdev->consumer_list, list) {
  101. if (regulator->dev == dev) {
  102. mutex_unlock(&rdev->mutex);
  103. mutex_unlock(&regulator_list_mutex);
  104. return regulator;
  105. }
  106. }
  107. mutex_unlock(&rdev->mutex);
  108. }
  109. mutex_unlock(&regulator_list_mutex);
  110. return NULL;
  111. }
  112. /* Platform voltage constraint check */
  113. static int regulator_check_voltage(struct regulator_dev *rdev,
  114. int *min_uV, int *max_uV)
  115. {
  116. BUG_ON(*min_uV > *max_uV);
  117. if (!rdev->constraints) {
  118. rdev_err(rdev, "no constraints\n");
  119. return -ENODEV;
  120. }
  121. if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_VOLTAGE)) {
  122. rdev_err(rdev, "operation not allowed\n");
  123. return -EPERM;
  124. }
  125. if (*max_uV > rdev->constraints->max_uV)
  126. *max_uV = rdev->constraints->max_uV;
  127. if (*min_uV < rdev->constraints->min_uV)
  128. *min_uV = rdev->constraints->min_uV;
  129. if (*min_uV > *max_uV)
  130. return -EINVAL;
  131. return 0;
  132. }
  133. /* Make sure we select a voltage that suits the needs of all
  134. * regulator consumers
  135. */
  136. static int regulator_check_consumers(struct regulator_dev *rdev,
  137. int *min_uV, int *max_uV)
  138. {
  139. struct regulator *regulator;
  140. list_for_each_entry(regulator, &rdev->consumer_list, list) {
  141. /*
  142. * Assume consumers that didn't say anything are OK
  143. * with anything in the constraint range.
  144. */
  145. if (!regulator->min_uV && !regulator->max_uV)
  146. continue;
  147. if (*max_uV > regulator->max_uV)
  148. *max_uV = regulator->max_uV;
  149. if (*min_uV < regulator->min_uV)
  150. *min_uV = regulator->min_uV;
  151. }
  152. if (*min_uV > *max_uV)
  153. return -EINVAL;
  154. return 0;
  155. }
  156. /* current constraint check */
  157. static int regulator_check_current_limit(struct regulator_dev *rdev,
  158. int *min_uA, int *max_uA)
  159. {
  160. BUG_ON(*min_uA > *max_uA);
  161. if (!rdev->constraints) {
  162. rdev_err(rdev, "no constraints\n");
  163. return -ENODEV;
  164. }
  165. if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_CURRENT)) {
  166. rdev_err(rdev, "operation not allowed\n");
  167. return -EPERM;
  168. }
  169. if (*max_uA > rdev->constraints->max_uA)
  170. *max_uA = rdev->constraints->max_uA;
  171. if (*min_uA < rdev->constraints->min_uA)
  172. *min_uA = rdev->constraints->min_uA;
  173. if (*min_uA > *max_uA)
  174. return -EINVAL;
  175. return 0;
  176. }
  177. /* operating mode constraint check */
  178. static int regulator_mode_constrain(struct regulator_dev *rdev, int *mode)
  179. {
  180. switch (*mode) {
  181. case REGULATOR_MODE_FAST:
  182. case REGULATOR_MODE_NORMAL:
  183. case REGULATOR_MODE_IDLE:
  184. case REGULATOR_MODE_STANDBY:
  185. break;
  186. default:
  187. return -EINVAL;
  188. }
  189. if (!rdev->constraints) {
  190. rdev_err(rdev, "no constraints\n");
  191. return -ENODEV;
  192. }
  193. if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_MODE)) {
  194. rdev_err(rdev, "operation not allowed\n");
  195. return -EPERM;
  196. }
  197. /* The modes are bitmasks, the most power hungry modes having
  198. * the lowest values. If the requested mode isn't supported
  199. * try higher modes. */
  200. while (*mode) {
  201. if (rdev->constraints->valid_modes_mask & *mode)
  202. return 0;
  203. *mode /= 2;
  204. }
  205. return -EINVAL;
  206. }
  207. /* dynamic regulator mode switching constraint check */
  208. static int regulator_check_drms(struct regulator_dev *rdev)
  209. {
  210. if (!rdev->constraints) {
  211. rdev_err(rdev, "no constraints\n");
  212. return -ENODEV;
  213. }
  214. if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_DRMS)) {
  215. rdev_err(rdev, "operation not allowed\n");
  216. return -EPERM;
  217. }
  218. return 0;
  219. }
  220. static ssize_t device_requested_uA_show(struct device *dev,
  221. struct device_attribute *attr, char *buf)
  222. {
  223. struct regulator *regulator;
  224. regulator = get_device_regulator(dev);
  225. if (regulator == NULL)
  226. return 0;
  227. return sprintf(buf, "%d\n", regulator->uA_load);
  228. }
  229. static ssize_t regulator_uV_show(struct device *dev,
  230. struct device_attribute *attr, char *buf)
  231. {
  232. struct regulator_dev *rdev = dev_get_drvdata(dev);
  233. ssize_t ret;
  234. mutex_lock(&rdev->mutex);
  235. ret = sprintf(buf, "%d\n", _regulator_get_voltage(rdev));
  236. mutex_unlock(&rdev->mutex);
  237. return ret;
  238. }
  239. static DEVICE_ATTR(microvolts, 0444, regulator_uV_show, NULL);
  240. static ssize_t regulator_uA_show(struct device *dev,
  241. struct device_attribute *attr, char *buf)
  242. {
  243. struct regulator_dev *rdev = dev_get_drvdata(dev);
  244. return sprintf(buf, "%d\n", _regulator_get_current_limit(rdev));
  245. }
  246. static DEVICE_ATTR(microamps, 0444, regulator_uA_show, NULL);
  247. static ssize_t regulator_name_show(struct device *dev,
  248. struct device_attribute *attr, char *buf)
  249. {
  250. struct regulator_dev *rdev = dev_get_drvdata(dev);
  251. return sprintf(buf, "%s\n", rdev_get_name(rdev));
  252. }
  253. static ssize_t regulator_print_opmode(char *buf, int mode)
  254. {
  255. switch (mode) {
  256. case REGULATOR_MODE_FAST:
  257. return sprintf(buf, "fast\n");
  258. case REGULATOR_MODE_NORMAL:
  259. return sprintf(buf, "normal\n");
  260. case REGULATOR_MODE_IDLE:
  261. return sprintf(buf, "idle\n");
  262. case REGULATOR_MODE_STANDBY:
  263. return sprintf(buf, "standby\n");
  264. }
  265. return sprintf(buf, "unknown\n");
  266. }
  267. static ssize_t regulator_opmode_show(struct device *dev,
  268. struct device_attribute *attr, char *buf)
  269. {
  270. struct regulator_dev *rdev = dev_get_drvdata(dev);
  271. return regulator_print_opmode(buf, _regulator_get_mode(rdev));
  272. }
  273. static DEVICE_ATTR(opmode, 0444, regulator_opmode_show, NULL);
  274. static ssize_t regulator_print_state(char *buf, int state)
  275. {
  276. if (state > 0)
  277. return sprintf(buf, "enabled\n");
  278. else if (state == 0)
  279. return sprintf(buf, "disabled\n");
  280. else
  281. return sprintf(buf, "unknown\n");
  282. }
  283. static ssize_t regulator_state_show(struct device *dev,
  284. struct device_attribute *attr, char *buf)
  285. {
  286. struct regulator_dev *rdev = dev_get_drvdata(dev);
  287. ssize_t ret;
  288. mutex_lock(&rdev->mutex);
  289. ret = regulator_print_state(buf, _regulator_is_enabled(rdev));
  290. mutex_unlock(&rdev->mutex);
  291. return ret;
  292. }
  293. static DEVICE_ATTR(state, 0444, regulator_state_show, NULL);
  294. static ssize_t regulator_status_show(struct device *dev,
  295. struct device_attribute *attr, char *buf)
  296. {
  297. struct regulator_dev *rdev = dev_get_drvdata(dev);
  298. int status;
  299. char *label;
  300. status = rdev->desc->ops->get_status(rdev);
  301. if (status < 0)
  302. return status;
  303. switch (status) {
  304. case REGULATOR_STATUS_OFF:
  305. label = "off";
  306. break;
  307. case REGULATOR_STATUS_ON:
  308. label = "on";
  309. break;
  310. case REGULATOR_STATUS_ERROR:
  311. label = "error";
  312. break;
  313. case REGULATOR_STATUS_FAST:
  314. label = "fast";
  315. break;
  316. case REGULATOR_STATUS_NORMAL:
  317. label = "normal";
  318. break;
  319. case REGULATOR_STATUS_IDLE:
  320. label = "idle";
  321. break;
  322. case REGULATOR_STATUS_STANDBY:
  323. label = "standby";
  324. break;
  325. default:
  326. return -ERANGE;
  327. }
  328. return sprintf(buf, "%s\n", label);
  329. }
  330. static DEVICE_ATTR(status, 0444, regulator_status_show, NULL);
  331. static ssize_t regulator_min_uA_show(struct device *dev,
  332. struct device_attribute *attr, char *buf)
  333. {
  334. struct regulator_dev *rdev = dev_get_drvdata(dev);
  335. if (!rdev->constraints)
  336. return sprintf(buf, "constraint not defined\n");
  337. return sprintf(buf, "%d\n", rdev->constraints->min_uA);
  338. }
  339. static DEVICE_ATTR(min_microamps, 0444, regulator_min_uA_show, NULL);
  340. static ssize_t regulator_max_uA_show(struct device *dev,
  341. struct device_attribute *attr, char *buf)
  342. {
  343. struct regulator_dev *rdev = dev_get_drvdata(dev);
  344. if (!rdev->constraints)
  345. return sprintf(buf, "constraint not defined\n");
  346. return sprintf(buf, "%d\n", rdev->constraints->max_uA);
  347. }
  348. static DEVICE_ATTR(max_microamps, 0444, regulator_max_uA_show, NULL);
  349. static ssize_t regulator_min_uV_show(struct device *dev,
  350. struct device_attribute *attr, char *buf)
  351. {
  352. struct regulator_dev *rdev = dev_get_drvdata(dev);
  353. if (!rdev->constraints)
  354. return sprintf(buf, "constraint not defined\n");
  355. return sprintf(buf, "%d\n", rdev->constraints->min_uV);
  356. }
  357. static DEVICE_ATTR(min_microvolts, 0444, regulator_min_uV_show, NULL);
  358. static ssize_t regulator_max_uV_show(struct device *dev,
  359. struct device_attribute *attr, char *buf)
  360. {
  361. struct regulator_dev *rdev = dev_get_drvdata(dev);
  362. if (!rdev->constraints)
  363. return sprintf(buf, "constraint not defined\n");
  364. return sprintf(buf, "%d\n", rdev->constraints->max_uV);
  365. }
  366. static DEVICE_ATTR(max_microvolts, 0444, regulator_max_uV_show, NULL);
  367. static ssize_t regulator_total_uA_show(struct device *dev,
  368. struct device_attribute *attr, char *buf)
  369. {
  370. struct regulator_dev *rdev = dev_get_drvdata(dev);
  371. struct regulator *regulator;
  372. int uA = 0;
  373. mutex_lock(&rdev->mutex);
  374. list_for_each_entry(regulator, &rdev->consumer_list, list)
  375. uA += regulator->uA_load;
  376. mutex_unlock(&rdev->mutex);
  377. return sprintf(buf, "%d\n", uA);
  378. }
  379. static DEVICE_ATTR(requested_microamps, 0444, regulator_total_uA_show, NULL);
  380. static ssize_t regulator_num_users_show(struct device *dev,
  381. struct device_attribute *attr, char *buf)
  382. {
  383. struct regulator_dev *rdev = dev_get_drvdata(dev);
  384. return sprintf(buf, "%d\n", rdev->use_count);
  385. }
  386. static ssize_t regulator_type_show(struct device *dev,
  387. struct device_attribute *attr, char *buf)
  388. {
  389. struct regulator_dev *rdev = dev_get_drvdata(dev);
  390. switch (rdev->desc->type) {
  391. case REGULATOR_VOLTAGE:
  392. return sprintf(buf, "voltage\n");
  393. case REGULATOR_CURRENT:
  394. return sprintf(buf, "current\n");
  395. }
  396. return sprintf(buf, "unknown\n");
  397. }
  398. static ssize_t regulator_suspend_mem_uV_show(struct device *dev,
  399. struct device_attribute *attr, char *buf)
  400. {
  401. struct regulator_dev *rdev = dev_get_drvdata(dev);
  402. return sprintf(buf, "%d\n", rdev->constraints->state_mem.uV);
  403. }
  404. static DEVICE_ATTR(suspend_mem_microvolts, 0444,
  405. regulator_suspend_mem_uV_show, NULL);
  406. static ssize_t regulator_suspend_disk_uV_show(struct device *dev,
  407. struct device_attribute *attr, char *buf)
  408. {
  409. struct regulator_dev *rdev = dev_get_drvdata(dev);
  410. return sprintf(buf, "%d\n", rdev->constraints->state_disk.uV);
  411. }
  412. static DEVICE_ATTR(suspend_disk_microvolts, 0444,
  413. regulator_suspend_disk_uV_show, NULL);
  414. static ssize_t regulator_suspend_standby_uV_show(struct device *dev,
  415. struct device_attribute *attr, char *buf)
  416. {
  417. struct regulator_dev *rdev = dev_get_drvdata(dev);
  418. return sprintf(buf, "%d\n", rdev->constraints->state_standby.uV);
  419. }
  420. static DEVICE_ATTR(suspend_standby_microvolts, 0444,
  421. regulator_suspend_standby_uV_show, NULL);
  422. static ssize_t regulator_suspend_mem_mode_show(struct device *dev,
  423. struct device_attribute *attr, char *buf)
  424. {
  425. struct regulator_dev *rdev = dev_get_drvdata(dev);
  426. return regulator_print_opmode(buf,
  427. rdev->constraints->state_mem.mode);
  428. }
  429. static DEVICE_ATTR(suspend_mem_mode, 0444,
  430. regulator_suspend_mem_mode_show, NULL);
  431. static ssize_t regulator_suspend_disk_mode_show(struct device *dev,
  432. struct device_attribute *attr, char *buf)
  433. {
  434. struct regulator_dev *rdev = dev_get_drvdata(dev);
  435. return regulator_print_opmode(buf,
  436. rdev->constraints->state_disk.mode);
  437. }
  438. static DEVICE_ATTR(suspend_disk_mode, 0444,
  439. regulator_suspend_disk_mode_show, NULL);
  440. static ssize_t regulator_suspend_standby_mode_show(struct device *dev,
  441. struct device_attribute *attr, char *buf)
  442. {
  443. struct regulator_dev *rdev = dev_get_drvdata(dev);
  444. return regulator_print_opmode(buf,
  445. rdev->constraints->state_standby.mode);
  446. }
  447. static DEVICE_ATTR(suspend_standby_mode, 0444,
  448. regulator_suspend_standby_mode_show, NULL);
  449. static ssize_t regulator_suspend_mem_state_show(struct device *dev,
  450. struct device_attribute *attr, char *buf)
  451. {
  452. struct regulator_dev *rdev = dev_get_drvdata(dev);
  453. return regulator_print_state(buf,
  454. rdev->constraints->state_mem.enabled);
  455. }
  456. static DEVICE_ATTR(suspend_mem_state, 0444,
  457. regulator_suspend_mem_state_show, NULL);
  458. static ssize_t regulator_suspend_disk_state_show(struct device *dev,
  459. struct device_attribute *attr, char *buf)
  460. {
  461. struct regulator_dev *rdev = dev_get_drvdata(dev);
  462. return regulator_print_state(buf,
  463. rdev->constraints->state_disk.enabled);
  464. }
  465. static DEVICE_ATTR(suspend_disk_state, 0444,
  466. regulator_suspend_disk_state_show, NULL);
  467. static ssize_t regulator_suspend_standby_state_show(struct device *dev,
  468. struct device_attribute *attr, char *buf)
  469. {
  470. struct regulator_dev *rdev = dev_get_drvdata(dev);
  471. return regulator_print_state(buf,
  472. rdev->constraints->state_standby.enabled);
  473. }
  474. static DEVICE_ATTR(suspend_standby_state, 0444,
  475. regulator_suspend_standby_state_show, NULL);
  476. /*
  477. * These are the only attributes are present for all regulators.
  478. * Other attributes are a function of regulator functionality.
  479. */
  480. static struct device_attribute regulator_dev_attrs[] = {
  481. __ATTR(name, 0444, regulator_name_show, NULL),
  482. __ATTR(num_users, 0444, regulator_num_users_show, NULL),
  483. __ATTR(type, 0444, regulator_type_show, NULL),
  484. __ATTR_NULL,
  485. };
  486. static void regulator_dev_release(struct device *dev)
  487. {
  488. struct regulator_dev *rdev = dev_get_drvdata(dev);
  489. kfree(rdev);
  490. }
  491. static struct class regulator_class = {
  492. .name = "regulator",
  493. .dev_release = regulator_dev_release,
  494. .dev_attrs = regulator_dev_attrs,
  495. };
  496. /* Calculate the new optimum regulator operating mode based on the new total
  497. * consumer load. All locks held by caller */
  498. static void drms_uA_update(struct regulator_dev *rdev)
  499. {
  500. struct regulator *sibling;
  501. int current_uA = 0, output_uV, input_uV, err;
  502. unsigned int mode;
  503. err = regulator_check_drms(rdev);
  504. if (err < 0 || !rdev->desc->ops->get_optimum_mode ||
  505. (!rdev->desc->ops->get_voltage &&
  506. !rdev->desc->ops->get_voltage_sel) ||
  507. !rdev->desc->ops->set_mode)
  508. return;
  509. /* get output voltage */
  510. output_uV = _regulator_get_voltage(rdev);
  511. if (output_uV <= 0)
  512. return;
  513. /* get input voltage */
  514. input_uV = 0;
  515. if (rdev->supply)
  516. input_uV = _regulator_get_voltage(rdev);
  517. if (input_uV <= 0)
  518. input_uV = rdev->constraints->input_uV;
  519. if (input_uV <= 0)
  520. return;
  521. /* calc total requested load */
  522. list_for_each_entry(sibling, &rdev->consumer_list, list)
  523. current_uA += sibling->uA_load;
  524. /* now get the optimum mode for our new total regulator load */
  525. mode = rdev->desc->ops->get_optimum_mode(rdev, input_uV,
  526. output_uV, current_uA);
  527. /* check the new mode is allowed */
  528. err = regulator_mode_constrain(rdev, &mode);
  529. if (err == 0)
  530. rdev->desc->ops->set_mode(rdev, mode);
  531. }
  532. static int suspend_set_state(struct regulator_dev *rdev,
  533. struct regulator_state *rstate)
  534. {
  535. int ret = 0;
  536. bool can_set_state;
  537. can_set_state = rdev->desc->ops->set_suspend_enable &&
  538. rdev->desc->ops->set_suspend_disable;
  539. /* If we have no suspend mode configration don't set anything;
  540. * only warn if the driver actually makes the suspend mode
  541. * configurable.
  542. */
  543. if (!rstate->enabled && !rstate->disabled) {
  544. if (can_set_state)
  545. rdev_warn(rdev, "No configuration\n");
  546. return 0;
  547. }
  548. if (rstate->enabled && rstate->disabled) {
  549. rdev_err(rdev, "invalid configuration\n");
  550. return -EINVAL;
  551. }
  552. if (!can_set_state) {
  553. rdev_err(rdev, "no way to set suspend state\n");
  554. return -EINVAL;
  555. }
  556. if (rstate->enabled)
  557. ret = rdev->desc->ops->set_suspend_enable(rdev);
  558. else
  559. ret = rdev->desc->ops->set_suspend_disable(rdev);
  560. if (ret < 0) {
  561. rdev_err(rdev, "failed to enabled/disable\n");
  562. return ret;
  563. }
  564. if (rdev->desc->ops->set_suspend_voltage && rstate->uV > 0) {
  565. ret = rdev->desc->ops->set_suspend_voltage(rdev, rstate->uV);
  566. if (ret < 0) {
  567. rdev_err(rdev, "failed to set voltage\n");
  568. return ret;
  569. }
  570. }
  571. if (rdev->desc->ops->set_suspend_mode && rstate->mode > 0) {
  572. ret = rdev->desc->ops->set_suspend_mode(rdev, rstate->mode);
  573. if (ret < 0) {
  574. rdev_err(rdev, "failed to set mode\n");
  575. return ret;
  576. }
  577. }
  578. return ret;
  579. }
  580. /* locks held by caller */
  581. static int suspend_prepare(struct regulator_dev *rdev, suspend_state_t state)
  582. {
  583. if (!rdev->constraints)
  584. return -EINVAL;
  585. switch (state) {
  586. case PM_SUSPEND_STANDBY:
  587. return suspend_set_state(rdev,
  588. &rdev->constraints->state_standby);
  589. case PM_SUSPEND_MEM:
  590. return suspend_set_state(rdev,
  591. &rdev->constraints->state_mem);
  592. case PM_SUSPEND_MAX:
  593. return suspend_set_state(rdev,
  594. &rdev->constraints->state_disk);
  595. default:
  596. return -EINVAL;
  597. }
  598. }
  599. static void print_constraints(struct regulator_dev *rdev)
  600. {
  601. struct regulation_constraints *constraints = rdev->constraints;
  602. char buf[80] = "";
  603. int count = 0;
  604. int ret;
  605. if (constraints->min_uV && constraints->max_uV) {
  606. if (constraints->min_uV == constraints->max_uV)
  607. count += sprintf(buf + count, "%d mV ",
  608. constraints->min_uV / 1000);
  609. else
  610. count += sprintf(buf + count, "%d <--> %d mV ",
  611. constraints->min_uV / 1000,
  612. constraints->max_uV / 1000);
  613. }
  614. if (!constraints->min_uV ||
  615. constraints->min_uV != constraints->max_uV) {
  616. ret = _regulator_get_voltage(rdev);
  617. if (ret > 0)
  618. count += sprintf(buf + count, "at %d mV ", ret / 1000);
  619. }
  620. if (constraints->uV_offset)
  621. count += sprintf(buf, "%dmV offset ",
  622. constraints->uV_offset / 1000);
  623. if (constraints->min_uA && constraints->max_uA) {
  624. if (constraints->min_uA == constraints->max_uA)
  625. count += sprintf(buf + count, "%d mA ",
  626. constraints->min_uA / 1000);
  627. else
  628. count += sprintf(buf + count, "%d <--> %d mA ",
  629. constraints->min_uA / 1000,
  630. constraints->max_uA / 1000);
  631. }
  632. if (!constraints->min_uA ||
  633. constraints->min_uA != constraints->max_uA) {
  634. ret = _regulator_get_current_limit(rdev);
  635. if (ret > 0)
  636. count += sprintf(buf + count, "at %d mA ", ret / 1000);
  637. }
  638. if (constraints->valid_modes_mask & REGULATOR_MODE_FAST)
  639. count += sprintf(buf + count, "fast ");
  640. if (constraints->valid_modes_mask & REGULATOR_MODE_NORMAL)
  641. count += sprintf(buf + count, "normal ");
  642. if (constraints->valid_modes_mask & REGULATOR_MODE_IDLE)
  643. count += sprintf(buf + count, "idle ");
  644. if (constraints->valid_modes_mask & REGULATOR_MODE_STANDBY)
  645. count += sprintf(buf + count, "standby");
  646. rdev_info(rdev, "%s\n", buf);
  647. }
  648. static int machine_constraints_voltage(struct regulator_dev *rdev,
  649. struct regulation_constraints *constraints)
  650. {
  651. struct regulator_ops *ops = rdev->desc->ops;
  652. int ret;
  653. /* do we need to apply the constraint voltage */
  654. if (rdev->constraints->apply_uV &&
  655. rdev->constraints->min_uV == rdev->constraints->max_uV) {
  656. ret = _regulator_do_set_voltage(rdev,
  657. rdev->constraints->min_uV,
  658. rdev->constraints->max_uV);
  659. if (ret < 0) {
  660. rdev_err(rdev, "failed to apply %duV constraint\n",
  661. rdev->constraints->min_uV);
  662. rdev->constraints = NULL;
  663. return ret;
  664. }
  665. }
  666. /* constrain machine-level voltage specs to fit
  667. * the actual range supported by this regulator.
  668. */
  669. if (ops->list_voltage && rdev->desc->n_voltages) {
  670. int count = rdev->desc->n_voltages;
  671. int i;
  672. int min_uV = INT_MAX;
  673. int max_uV = INT_MIN;
  674. int cmin = constraints->min_uV;
  675. int cmax = constraints->max_uV;
  676. /* it's safe to autoconfigure fixed-voltage supplies
  677. and the constraints are used by list_voltage. */
  678. if (count == 1 && !cmin) {
  679. cmin = 1;
  680. cmax = INT_MAX;
  681. constraints->min_uV = cmin;
  682. constraints->max_uV = cmax;
  683. }
  684. /* voltage constraints are optional */
  685. if ((cmin == 0) && (cmax == 0))
  686. return 0;
  687. /* else require explicit machine-level constraints */
  688. if (cmin <= 0 || cmax <= 0 || cmax < cmin) {
  689. rdev_err(rdev, "invalid voltage constraints\n");
  690. return -EINVAL;
  691. }
  692. /* initial: [cmin..cmax] valid, [min_uV..max_uV] not */
  693. for (i = 0; i < count; i++) {
  694. int value;
  695. value = ops->list_voltage(rdev, i);
  696. if (value <= 0)
  697. continue;
  698. /* maybe adjust [min_uV..max_uV] */
  699. if (value >= cmin && value < min_uV)
  700. min_uV = value;
  701. if (value <= cmax && value > max_uV)
  702. max_uV = value;
  703. }
  704. /* final: [min_uV..max_uV] valid iff constraints valid */
  705. if (max_uV < min_uV) {
  706. rdev_err(rdev, "unsupportable voltage constraints\n");
  707. return -EINVAL;
  708. }
  709. /* use regulator's subset of machine constraints */
  710. if (constraints->min_uV < min_uV) {
  711. rdev_dbg(rdev, "override min_uV, %d -> %d\n",
  712. constraints->min_uV, min_uV);
  713. constraints->min_uV = min_uV;
  714. }
  715. if (constraints->max_uV > max_uV) {
  716. rdev_dbg(rdev, "override max_uV, %d -> %d\n",
  717. constraints->max_uV, max_uV);
  718. constraints->max_uV = max_uV;
  719. }
  720. }
  721. return 0;
  722. }
  723. /**
  724. * set_machine_constraints - sets regulator constraints
  725. * @rdev: regulator source
  726. * @constraints: constraints to apply
  727. *
  728. * Allows platform initialisation code to define and constrain
  729. * regulator circuits e.g. valid voltage/current ranges, etc. NOTE:
  730. * Constraints *must* be set by platform code in order for some
  731. * regulator operations to proceed i.e. set_voltage, set_current_limit,
  732. * set_mode.
  733. */
  734. static int set_machine_constraints(struct regulator_dev *rdev,
  735. const struct regulation_constraints *constraints)
  736. {
  737. int ret = 0;
  738. struct regulator_ops *ops = rdev->desc->ops;
  739. rdev->constraints = kmemdup(constraints, sizeof(*constraints),
  740. GFP_KERNEL);
  741. if (!rdev->constraints)
  742. return -ENOMEM;
  743. ret = machine_constraints_voltage(rdev, rdev->constraints);
  744. if (ret != 0)
  745. goto out;
  746. /* do we need to setup our suspend state */
  747. if (constraints->initial_state) {
  748. ret = suspend_prepare(rdev, rdev->constraints->initial_state);
  749. if (ret < 0) {
  750. rdev_err(rdev, "failed to set suspend state\n");
  751. rdev->constraints = NULL;
  752. goto out;
  753. }
  754. }
  755. if (constraints->initial_mode) {
  756. if (!ops->set_mode) {
  757. rdev_err(rdev, "no set_mode operation\n");
  758. ret = -EINVAL;
  759. goto out;
  760. }
  761. ret = ops->set_mode(rdev, rdev->constraints->initial_mode);
  762. if (ret < 0) {
  763. rdev_err(rdev, "failed to set initial mode: %d\n", ret);
  764. goto out;
  765. }
  766. }
  767. /* If the constraints say the regulator should be on at this point
  768. * and we have control then make sure it is enabled.
  769. */
  770. if ((rdev->constraints->always_on || rdev->constraints->boot_on) &&
  771. ops->enable) {
  772. ret = ops->enable(rdev);
  773. if (ret < 0) {
  774. rdev_err(rdev, "failed to enable\n");
  775. rdev->constraints = NULL;
  776. goto out;
  777. }
  778. }
  779. print_constraints(rdev);
  780. out:
  781. return ret;
  782. }
  783. /**
  784. * set_supply - set regulator supply regulator
  785. * @rdev: regulator name
  786. * @supply_rdev: supply regulator name
  787. *
  788. * Called by platform initialisation code to set the supply regulator for this
  789. * regulator. This ensures that a regulators supply will also be enabled by the
  790. * core if it's child is enabled.
  791. */
  792. static int set_supply(struct regulator_dev *rdev,
  793. struct regulator_dev *supply_rdev)
  794. {
  795. int err;
  796. err = sysfs_create_link(&rdev->dev.kobj, &supply_rdev->dev.kobj,
  797. "supply");
  798. if (err) {
  799. rdev_err(rdev, "could not add device link %s err %d\n",
  800. supply_rdev->dev.kobj.name, err);
  801. goto out;
  802. }
  803. rdev->supply = supply_rdev;
  804. list_add(&rdev->slist, &supply_rdev->supply_list);
  805. out:
  806. return err;
  807. }
  808. /**
  809. * set_consumer_device_supply - Bind a regulator to a symbolic supply
  810. * @rdev: regulator source
  811. * @consumer_dev: device the supply applies to
  812. * @consumer_dev_name: dev_name() string for device supply applies to
  813. * @supply: symbolic name for supply
  814. *
  815. * Allows platform initialisation code to map physical regulator
  816. * sources to symbolic names for supplies for use by devices. Devices
  817. * should use these symbolic names to request regulators, avoiding the
  818. * need to provide board-specific regulator names as platform data.
  819. *
  820. * Only one of consumer_dev and consumer_dev_name may be specified.
  821. */
  822. static int set_consumer_device_supply(struct regulator_dev *rdev,
  823. struct device *consumer_dev, const char *consumer_dev_name,
  824. const char *supply)
  825. {
  826. struct regulator_map *node;
  827. int has_dev;
  828. if (consumer_dev && consumer_dev_name)
  829. return -EINVAL;
  830. if (!consumer_dev_name && consumer_dev)
  831. consumer_dev_name = dev_name(consumer_dev);
  832. if (supply == NULL)
  833. return -EINVAL;
  834. if (consumer_dev_name != NULL)
  835. has_dev = 1;
  836. else
  837. has_dev = 0;
  838. list_for_each_entry(node, &regulator_map_list, list) {
  839. if (node->dev_name && consumer_dev_name) {
  840. if (strcmp(node->dev_name, consumer_dev_name) != 0)
  841. continue;
  842. } else if (node->dev_name || consumer_dev_name) {
  843. continue;
  844. }
  845. if (strcmp(node->supply, supply) != 0)
  846. continue;
  847. dev_dbg(consumer_dev, "%s/%s is '%s' supply; fail %s/%s\n",
  848. dev_name(&node->regulator->dev),
  849. node->regulator->desc->name,
  850. supply,
  851. dev_name(&rdev->dev), rdev_get_name(rdev));
  852. return -EBUSY;
  853. }
  854. node = kzalloc(sizeof(struct regulator_map), GFP_KERNEL);
  855. if (node == NULL)
  856. return -ENOMEM;
  857. node->regulator = rdev;
  858. node->supply = supply;
  859. if (has_dev) {
  860. node->dev_name = kstrdup(consumer_dev_name, GFP_KERNEL);
  861. if (node->dev_name == NULL) {
  862. kfree(node);
  863. return -ENOMEM;
  864. }
  865. }
  866. list_add(&node->list, &regulator_map_list);
  867. return 0;
  868. }
  869. static void unset_regulator_supplies(struct regulator_dev *rdev)
  870. {
  871. struct regulator_map *node, *n;
  872. list_for_each_entry_safe(node, n, &regulator_map_list, list) {
  873. if (rdev == node->regulator) {
  874. list_del(&node->list);
  875. kfree(node->dev_name);
  876. kfree(node);
  877. }
  878. }
  879. }
  880. #define REG_STR_SIZE 32
  881. static struct regulator *create_regulator(struct regulator_dev *rdev,
  882. struct device *dev,
  883. const char *supply_name)
  884. {
  885. struct regulator *regulator;
  886. char buf[REG_STR_SIZE];
  887. int err, size;
  888. regulator = kzalloc(sizeof(*regulator), GFP_KERNEL);
  889. if (regulator == NULL)
  890. return NULL;
  891. mutex_lock(&rdev->mutex);
  892. regulator->rdev = rdev;
  893. list_add(&regulator->list, &rdev->consumer_list);
  894. if (dev) {
  895. /* create a 'requested_microamps_name' sysfs entry */
  896. size = scnprintf(buf, REG_STR_SIZE, "microamps_requested_%s",
  897. supply_name);
  898. if (size >= REG_STR_SIZE)
  899. goto overflow_err;
  900. regulator->dev = dev;
  901. sysfs_attr_init(&regulator->dev_attr.attr);
  902. regulator->dev_attr.attr.name = kstrdup(buf, GFP_KERNEL);
  903. if (regulator->dev_attr.attr.name == NULL)
  904. goto attr_name_err;
  905. regulator->dev_attr.attr.mode = 0444;
  906. regulator->dev_attr.show = device_requested_uA_show;
  907. err = device_create_file(dev, &regulator->dev_attr);
  908. if (err < 0) {
  909. rdev_warn(rdev, "could not add regulator_dev requested microamps sysfs entry\n");
  910. goto attr_name_err;
  911. }
  912. /* also add a link to the device sysfs entry */
  913. size = scnprintf(buf, REG_STR_SIZE, "%s-%s",
  914. dev->kobj.name, supply_name);
  915. if (size >= REG_STR_SIZE)
  916. goto attr_err;
  917. regulator->supply_name = kstrdup(buf, GFP_KERNEL);
  918. if (regulator->supply_name == NULL)
  919. goto attr_err;
  920. err = sysfs_create_link(&rdev->dev.kobj, &dev->kobj,
  921. buf);
  922. if (err) {
  923. rdev_warn(rdev, "could not add device link %s err %d\n",
  924. dev->kobj.name, err);
  925. goto link_name_err;
  926. }
  927. }
  928. mutex_unlock(&rdev->mutex);
  929. return regulator;
  930. link_name_err:
  931. kfree(regulator->supply_name);
  932. attr_err:
  933. device_remove_file(regulator->dev, &regulator->dev_attr);
  934. attr_name_err:
  935. kfree(regulator->dev_attr.attr.name);
  936. overflow_err:
  937. list_del(&regulator->list);
  938. kfree(regulator);
  939. mutex_unlock(&rdev->mutex);
  940. return NULL;
  941. }
  942. static int _regulator_get_enable_time(struct regulator_dev *rdev)
  943. {
  944. if (!rdev->desc->ops->enable_time)
  945. return 0;
  946. return rdev->desc->ops->enable_time(rdev);
  947. }
  948. /* Internal regulator request function */
  949. static struct regulator *_regulator_get(struct device *dev, const char *id,
  950. int exclusive)
  951. {
  952. struct regulator_dev *rdev;
  953. struct regulator_map *map;
  954. struct regulator *regulator = ERR_PTR(-ENODEV);
  955. const char *devname = NULL;
  956. int ret;
  957. if (id == NULL) {
  958. pr_err("get() with no identifier\n");
  959. return regulator;
  960. }
  961. if (dev)
  962. devname = dev_name(dev);
  963. mutex_lock(&regulator_list_mutex);
  964. list_for_each_entry(map, &regulator_map_list, list) {
  965. /* If the mapping has a device set up it must match */
  966. if (map->dev_name &&
  967. (!devname || strcmp(map->dev_name, devname)))
  968. continue;
  969. if (strcmp(map->supply, id) == 0) {
  970. rdev = map->regulator;
  971. goto found;
  972. }
  973. }
  974. if (board_wants_dummy_regulator) {
  975. rdev = dummy_regulator_rdev;
  976. goto found;
  977. }
  978. #ifdef CONFIG_REGULATOR_DUMMY
  979. if (!devname)
  980. devname = "deviceless";
  981. /* If the board didn't flag that it was fully constrained then
  982. * substitute in a dummy regulator so consumers can continue.
  983. */
  984. if (!has_full_constraints) {
  985. pr_warn("%s supply %s not found, using dummy regulator\n",
  986. devname, id);
  987. rdev = dummy_regulator_rdev;
  988. goto found;
  989. }
  990. #endif
  991. mutex_unlock(&regulator_list_mutex);
  992. return regulator;
  993. found:
  994. if (rdev->exclusive) {
  995. regulator = ERR_PTR(-EPERM);
  996. goto out;
  997. }
  998. if (exclusive && rdev->open_count) {
  999. regulator = ERR_PTR(-EBUSY);
  1000. goto out;
  1001. }
  1002. if (!try_module_get(rdev->owner))
  1003. goto out;
  1004. regulator = create_regulator(rdev, dev, id);
  1005. if (regulator == NULL) {
  1006. regulator = ERR_PTR(-ENOMEM);
  1007. module_put(rdev->owner);
  1008. }
  1009. rdev->open_count++;
  1010. if (exclusive) {
  1011. rdev->exclusive = 1;
  1012. ret = _regulator_is_enabled(rdev);
  1013. if (ret > 0)
  1014. rdev->use_count = 1;
  1015. else
  1016. rdev->use_count = 0;
  1017. }
  1018. out:
  1019. mutex_unlock(&regulator_list_mutex);
  1020. return regulator;
  1021. }
  1022. /**
  1023. * regulator_get - lookup and obtain a reference to a regulator.
  1024. * @dev: device for regulator "consumer"
  1025. * @id: Supply name or regulator ID.
  1026. *
  1027. * Returns a struct regulator corresponding to the regulator producer,
  1028. * or IS_ERR() condition containing errno.
  1029. *
  1030. * Use of supply names configured via regulator_set_device_supply() is
  1031. * strongly encouraged. It is recommended that the supply name used
  1032. * should match the name used for the supply and/or the relevant
  1033. * device pins in the datasheet.
  1034. */
  1035. struct regulator *regulator_get(struct device *dev, const char *id)
  1036. {
  1037. return _regulator_get(dev, id, 0);
  1038. }
  1039. EXPORT_SYMBOL_GPL(regulator_get);
  1040. /**
  1041. * regulator_get_exclusive - obtain exclusive access to a regulator.
  1042. * @dev: device for regulator "consumer"
  1043. * @id: Supply name or regulator ID.
  1044. *
  1045. * Returns a struct regulator corresponding to the regulator producer,
  1046. * or IS_ERR() condition containing errno. Other consumers will be
  1047. * unable to obtain this reference is held and the use count for the
  1048. * regulator will be initialised to reflect the current state of the
  1049. * regulator.
  1050. *
  1051. * This is intended for use by consumers which cannot tolerate shared
  1052. * use of the regulator such as those which need to force the
  1053. * regulator off for correct operation of the hardware they are
  1054. * controlling.
  1055. *
  1056. * Use of supply names configured via regulator_set_device_supply() is
  1057. * strongly encouraged. It is recommended that the supply name used
  1058. * should match the name used for the supply and/or the relevant
  1059. * device pins in the datasheet.
  1060. */
  1061. struct regulator *regulator_get_exclusive(struct device *dev, const char *id)
  1062. {
  1063. return _regulator_get(dev, id, 1);
  1064. }
  1065. EXPORT_SYMBOL_GPL(regulator_get_exclusive);
  1066. /**
  1067. * regulator_put - "free" the regulator source
  1068. * @regulator: regulator source
  1069. *
  1070. * Note: drivers must ensure that all regulator_enable calls made on this
  1071. * regulator source are balanced by regulator_disable calls prior to calling
  1072. * this function.
  1073. */
  1074. void regulator_put(struct regulator *regulator)
  1075. {
  1076. struct regulator_dev *rdev;
  1077. if (regulator == NULL || IS_ERR(regulator))
  1078. return;
  1079. mutex_lock(&regulator_list_mutex);
  1080. rdev = regulator->rdev;
  1081. /* remove any sysfs entries */
  1082. if (regulator->dev) {
  1083. sysfs_remove_link(&rdev->dev.kobj, regulator->supply_name);
  1084. kfree(regulator->supply_name);
  1085. device_remove_file(regulator->dev, &regulator->dev_attr);
  1086. kfree(regulator->dev_attr.attr.name);
  1087. }
  1088. list_del(&regulator->list);
  1089. kfree(regulator);
  1090. rdev->open_count--;
  1091. rdev->exclusive = 0;
  1092. module_put(rdev->owner);
  1093. mutex_unlock(&regulator_list_mutex);
  1094. }
  1095. EXPORT_SYMBOL_GPL(regulator_put);
  1096. static int _regulator_can_change_status(struct regulator_dev *rdev)
  1097. {
  1098. if (!rdev->constraints)
  1099. return 0;
  1100. if (rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_STATUS)
  1101. return 1;
  1102. else
  1103. return 0;
  1104. }
  1105. /* locks held by regulator_enable() */
  1106. static int _regulator_enable(struct regulator_dev *rdev)
  1107. {
  1108. int ret, delay;
  1109. if (rdev->use_count == 0) {
  1110. /* do we need to enable the supply regulator first */
  1111. if (rdev->supply) {
  1112. mutex_lock(&rdev->supply->mutex);
  1113. ret = _regulator_enable(rdev->supply);
  1114. mutex_unlock(&rdev->supply->mutex);
  1115. if (ret < 0) {
  1116. rdev_err(rdev, "failed to enable: %d\n", ret);
  1117. return ret;
  1118. }
  1119. }
  1120. }
  1121. /* check voltage and requested load before enabling */
  1122. if (rdev->constraints &&
  1123. (rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_DRMS))
  1124. drms_uA_update(rdev);
  1125. if (rdev->use_count == 0) {
  1126. /* The regulator may on if it's not switchable or left on */
  1127. ret = _regulator_is_enabled(rdev);
  1128. if (ret == -EINVAL || ret == 0) {
  1129. if (!_regulator_can_change_status(rdev))
  1130. return -EPERM;
  1131. if (!rdev->desc->ops->enable)
  1132. return -EINVAL;
  1133. /* Query before enabling in case configuration
  1134. * dependent. */
  1135. ret = _regulator_get_enable_time(rdev);
  1136. if (ret >= 0) {
  1137. delay = ret;
  1138. } else {
  1139. rdev_warn(rdev, "enable_time() failed: %d\n",
  1140. ret);
  1141. delay = 0;
  1142. }
  1143. trace_regulator_enable(rdev_get_name(rdev));
  1144. /* Allow the regulator to ramp; it would be useful
  1145. * to extend this for bulk operations so that the
  1146. * regulators can ramp together. */
  1147. ret = rdev->desc->ops->enable(rdev);
  1148. if (ret < 0)
  1149. return ret;
  1150. trace_regulator_enable_delay(rdev_get_name(rdev));
  1151. if (delay >= 1000) {
  1152. mdelay(delay / 1000);
  1153. udelay(delay % 1000);
  1154. } else if (delay) {
  1155. udelay(delay);
  1156. }
  1157. trace_regulator_enable_complete(rdev_get_name(rdev));
  1158. } else if (ret < 0) {
  1159. rdev_err(rdev, "is_enabled() failed: %d\n", ret);
  1160. return ret;
  1161. }
  1162. /* Fallthrough on positive return values - already enabled */
  1163. }
  1164. rdev->use_count++;
  1165. return 0;
  1166. }
  1167. /**
  1168. * regulator_enable - enable regulator output
  1169. * @regulator: regulator source
  1170. *
  1171. * Request that the regulator be enabled with the regulator output at
  1172. * the predefined voltage or current value. Calls to regulator_enable()
  1173. * must be balanced with calls to regulator_disable().
  1174. *
  1175. * NOTE: the output value can be set by other drivers, boot loader or may be
  1176. * hardwired in the regulator.
  1177. */
  1178. int regulator_enable(struct regulator *regulator)
  1179. {
  1180. struct regulator_dev *rdev = regulator->rdev;
  1181. int ret = 0;
  1182. mutex_lock(&rdev->mutex);
  1183. ret = _regulator_enable(rdev);
  1184. mutex_unlock(&rdev->mutex);
  1185. return ret;
  1186. }
  1187. EXPORT_SYMBOL_GPL(regulator_enable);
  1188. /* locks held by regulator_disable() */
  1189. static int _regulator_disable(struct regulator_dev *rdev,
  1190. struct regulator_dev **supply_rdev_ptr)
  1191. {
  1192. int ret = 0;
  1193. *supply_rdev_ptr = NULL;
  1194. if (WARN(rdev->use_count <= 0,
  1195. "unbalanced disables for %s\n", rdev_get_name(rdev)))
  1196. return -EIO;
  1197. /* are we the last user and permitted to disable ? */
  1198. if (rdev->use_count == 1 &&
  1199. (rdev->constraints && !rdev->constraints->always_on)) {
  1200. /* we are last user */
  1201. if (_regulator_can_change_status(rdev) &&
  1202. rdev->desc->ops->disable) {
  1203. trace_regulator_disable(rdev_get_name(rdev));
  1204. ret = rdev->desc->ops->disable(rdev);
  1205. if (ret < 0) {
  1206. rdev_err(rdev, "failed to disable\n");
  1207. return ret;
  1208. }
  1209. trace_regulator_disable_complete(rdev_get_name(rdev));
  1210. _notifier_call_chain(rdev, REGULATOR_EVENT_DISABLE,
  1211. NULL);
  1212. }
  1213. /* decrease our supplies ref count and disable if required */
  1214. *supply_rdev_ptr = rdev->supply;
  1215. rdev->use_count = 0;
  1216. } else if (rdev->use_count > 1) {
  1217. if (rdev->constraints &&
  1218. (rdev->constraints->valid_ops_mask &
  1219. REGULATOR_CHANGE_DRMS))
  1220. drms_uA_update(rdev);
  1221. rdev->use_count--;
  1222. }
  1223. return ret;
  1224. }
  1225. /**
  1226. * regulator_disable - disable regulator output
  1227. * @regulator: regulator source
  1228. *
  1229. * Disable the regulator output voltage or current. Calls to
  1230. * regulator_enable() must be balanced with calls to
  1231. * regulator_disable().
  1232. *
  1233. * NOTE: this will only disable the regulator output if no other consumer
  1234. * devices have it enabled, the regulator device supports disabling and
  1235. * machine constraints permit this operation.
  1236. */
  1237. int regulator_disable(struct regulator *regulator)
  1238. {
  1239. struct regulator_dev *rdev = regulator->rdev;
  1240. struct regulator_dev *supply_rdev = NULL;
  1241. int ret = 0;
  1242. mutex_lock(&rdev->mutex);
  1243. ret = _regulator_disable(rdev, &supply_rdev);
  1244. mutex_unlock(&rdev->mutex);
  1245. /* decrease our supplies ref count and disable if required */
  1246. while (supply_rdev != NULL) {
  1247. rdev = supply_rdev;
  1248. mutex_lock(&rdev->mutex);
  1249. _regulator_disable(rdev, &supply_rdev);
  1250. mutex_unlock(&rdev->mutex);
  1251. }
  1252. return ret;
  1253. }
  1254. EXPORT_SYMBOL_GPL(regulator_disable);
  1255. /* locks held by regulator_force_disable() */
  1256. static int _regulator_force_disable(struct regulator_dev *rdev,
  1257. struct regulator_dev **supply_rdev_ptr)
  1258. {
  1259. int ret = 0;
  1260. /* force disable */
  1261. if (rdev->desc->ops->disable) {
  1262. /* ah well, who wants to live forever... */
  1263. ret = rdev->desc->ops->disable(rdev);
  1264. if (ret < 0) {
  1265. rdev_err(rdev, "failed to force disable\n");
  1266. return ret;
  1267. }
  1268. /* notify other consumers that power has been forced off */
  1269. _notifier_call_chain(rdev, REGULATOR_EVENT_FORCE_DISABLE |
  1270. REGULATOR_EVENT_DISABLE, NULL);
  1271. }
  1272. /* decrease our supplies ref count and disable if required */
  1273. *supply_rdev_ptr = rdev->supply;
  1274. rdev->use_count = 0;
  1275. return ret;
  1276. }
  1277. /**
  1278. * regulator_force_disable - force disable regulator output
  1279. * @regulator: regulator source
  1280. *
  1281. * Forcibly disable the regulator output voltage or current.
  1282. * NOTE: this *will* disable the regulator output even if other consumer
  1283. * devices have it enabled. This should be used for situations when device
  1284. * damage will likely occur if the regulator is not disabled (e.g. over temp).
  1285. */
  1286. int regulator_force_disable(struct regulator *regulator)
  1287. {
  1288. struct regulator_dev *rdev = regulator->rdev;
  1289. struct regulator_dev *supply_rdev = NULL;
  1290. int ret;
  1291. mutex_lock(&rdev->mutex);
  1292. regulator->uA_load = 0;
  1293. ret = _regulator_force_disable(rdev, &supply_rdev);
  1294. mutex_unlock(&rdev->mutex);
  1295. if (supply_rdev)
  1296. regulator_disable(get_device_regulator(rdev_get_dev(supply_rdev)));
  1297. return ret;
  1298. }
  1299. EXPORT_SYMBOL_GPL(regulator_force_disable);
  1300. static int _regulator_is_enabled(struct regulator_dev *rdev)
  1301. {
  1302. /* If we don't know then assume that the regulator is always on */
  1303. if (!rdev->desc->ops->is_enabled)
  1304. return 1;
  1305. return rdev->desc->ops->is_enabled(rdev);
  1306. }
  1307. /**
  1308. * regulator_is_enabled - is the regulator output enabled
  1309. * @regulator: regulator source
  1310. *
  1311. * Returns positive if the regulator driver backing the source/client
  1312. * has requested that the device be enabled, zero if it hasn't, else a
  1313. * negative errno code.
  1314. *
  1315. * Note that the device backing this regulator handle can have multiple
  1316. * users, so it might be enabled even if regulator_enable() was never
  1317. * called for this particular source.
  1318. */
  1319. int regulator_is_enabled(struct regulator *regulator)
  1320. {
  1321. int ret;
  1322. mutex_lock(&regulator->rdev->mutex);
  1323. ret = _regulator_is_enabled(regulator->rdev);
  1324. mutex_unlock(&regulator->rdev->mutex);
  1325. return ret;
  1326. }
  1327. EXPORT_SYMBOL_GPL(regulator_is_enabled);
  1328. /**
  1329. * regulator_count_voltages - count regulator_list_voltage() selectors
  1330. * @regulator: regulator source
  1331. *
  1332. * Returns number of selectors, or negative errno. Selectors are
  1333. * numbered starting at zero, and typically correspond to bitfields
  1334. * in hardware registers.
  1335. */
  1336. int regulator_count_voltages(struct regulator *regulator)
  1337. {
  1338. struct regulator_dev *rdev = regulator->rdev;
  1339. return rdev->desc->n_voltages ? : -EINVAL;
  1340. }
  1341. EXPORT_SYMBOL_GPL(regulator_count_voltages);
  1342. /**
  1343. * regulator_list_voltage - enumerate supported voltages
  1344. * @regulator: regulator source
  1345. * @selector: identify voltage to list
  1346. * Context: can sleep
  1347. *
  1348. * Returns a voltage that can be passed to @regulator_set_voltage(),
  1349. * zero if this selector code can't be used on this system, or a
  1350. * negative errno.
  1351. */
  1352. int regulator_list_voltage(struct regulator *regulator, unsigned selector)
  1353. {
  1354. struct regulator_dev *rdev = regulator->rdev;
  1355. struct regulator_ops *ops = rdev->desc->ops;
  1356. int ret;
  1357. if (!ops->list_voltage || selector >= rdev->desc->n_voltages)
  1358. return -EINVAL;
  1359. mutex_lock(&rdev->mutex);
  1360. ret = ops->list_voltage(rdev, selector);
  1361. mutex_unlock(&rdev->mutex);
  1362. if (ret > 0) {
  1363. if (ret < rdev->constraints->min_uV)
  1364. ret = 0;
  1365. else if (ret > rdev->constraints->max_uV)
  1366. ret = 0;
  1367. }
  1368. return ret;
  1369. }
  1370. EXPORT_SYMBOL_GPL(regulator_list_voltage);
  1371. /**
  1372. * regulator_is_supported_voltage - check if a voltage range can be supported
  1373. *
  1374. * @regulator: Regulator to check.
  1375. * @min_uV: Minimum required voltage in uV.
  1376. * @max_uV: Maximum required voltage in uV.
  1377. *
  1378. * Returns a boolean or a negative error code.
  1379. */
  1380. int regulator_is_supported_voltage(struct regulator *regulator,
  1381. int min_uV, int max_uV)
  1382. {
  1383. int i, voltages, ret;
  1384. ret = regulator_count_voltages(regulator);
  1385. if (ret < 0)
  1386. return ret;
  1387. voltages = ret;
  1388. for (i = 0; i < voltages; i++) {
  1389. ret = regulator_list_voltage(regulator, i);
  1390. if (ret >= min_uV && ret <= max_uV)
  1391. return 1;
  1392. }
  1393. return 0;
  1394. }
  1395. static int _regulator_do_set_voltage(struct regulator_dev *rdev,
  1396. int min_uV, int max_uV)
  1397. {
  1398. int ret;
  1399. int delay = 0;
  1400. unsigned int selector;
  1401. trace_regulator_set_voltage(rdev_get_name(rdev), min_uV, max_uV);
  1402. min_uV += rdev->constraints->uV_offset;
  1403. max_uV += rdev->constraints->uV_offset;
  1404. if (rdev->desc->ops->set_voltage) {
  1405. ret = rdev->desc->ops->set_voltage(rdev, min_uV, max_uV,
  1406. &selector);
  1407. if (rdev->desc->ops->list_voltage)
  1408. selector = rdev->desc->ops->list_voltage(rdev,
  1409. selector);
  1410. else
  1411. selector = -1;
  1412. } else if (rdev->desc->ops->set_voltage_sel) {
  1413. int best_val = INT_MAX;
  1414. int i;
  1415. selector = 0;
  1416. /* Find the smallest voltage that falls within the specified
  1417. * range.
  1418. */
  1419. for (i = 0; i < rdev->desc->n_voltages; i++) {
  1420. ret = rdev->desc->ops->list_voltage(rdev, i);
  1421. if (ret < 0)
  1422. continue;
  1423. if (ret < best_val && ret >= min_uV && ret <= max_uV) {
  1424. best_val = ret;
  1425. selector = i;
  1426. }
  1427. }
  1428. /*
  1429. * If we can't obtain the old selector there is not enough
  1430. * info to call set_voltage_time_sel().
  1431. */
  1432. if (rdev->desc->ops->set_voltage_time_sel &&
  1433. rdev->desc->ops->get_voltage_sel) {
  1434. unsigned int old_selector = 0;
  1435. ret = rdev->desc->ops->get_voltage_sel(rdev);
  1436. if (ret < 0)
  1437. return ret;
  1438. old_selector = ret;
  1439. delay = rdev->desc->ops->set_voltage_time_sel(rdev,
  1440. old_selector, selector);
  1441. }
  1442. if (best_val != INT_MAX) {
  1443. ret = rdev->desc->ops->set_voltage_sel(rdev, selector);
  1444. selector = best_val;
  1445. } else {
  1446. ret = -EINVAL;
  1447. }
  1448. } else {
  1449. ret = -EINVAL;
  1450. }
  1451. /* Insert any necessary delays */
  1452. if (delay >= 1000) {
  1453. mdelay(delay / 1000);
  1454. udelay(delay % 1000);
  1455. } else if (delay) {
  1456. udelay(delay);
  1457. }
  1458. if (ret == 0)
  1459. _notifier_call_chain(rdev, REGULATOR_EVENT_VOLTAGE_CHANGE,
  1460. NULL);
  1461. trace_regulator_set_voltage_complete(rdev_get_name(rdev), selector);
  1462. return ret;
  1463. }
  1464. /**
  1465. * regulator_set_voltage - set regulator output voltage
  1466. * @regulator: regulator source
  1467. * @min_uV: Minimum required voltage in uV
  1468. * @max_uV: Maximum acceptable voltage in uV
  1469. *
  1470. * Sets a voltage regulator to the desired output voltage. This can be set
  1471. * during any regulator state. IOW, regulator can be disabled or enabled.
  1472. *
  1473. * If the regulator is enabled then the voltage will change to the new value
  1474. * immediately otherwise if the regulator is disabled the regulator will
  1475. * output at the new voltage when enabled.
  1476. *
  1477. * NOTE: If the regulator is shared between several devices then the lowest
  1478. * request voltage that meets the system constraints will be used.
  1479. * Regulator system constraints must be set for this regulator before
  1480. * calling this function otherwise this call will fail.
  1481. */
  1482. int regulator_set_voltage(struct regulator *regulator, int min_uV, int max_uV)
  1483. {
  1484. struct regulator_dev *rdev = regulator->rdev;
  1485. int ret = 0;
  1486. mutex_lock(&rdev->mutex);
  1487. /* If we're setting the same range as last time the change
  1488. * should be a noop (some cpufreq implementations use the same
  1489. * voltage for multiple frequencies, for example).
  1490. */
  1491. if (regulator->min_uV == min_uV && regulator->max_uV == max_uV)
  1492. goto out;
  1493. /* sanity check */
  1494. if (!rdev->desc->ops->set_voltage &&
  1495. !rdev->desc->ops->set_voltage_sel) {
  1496. ret = -EINVAL;
  1497. goto out;
  1498. }
  1499. /* constraints check */
  1500. ret = regulator_check_voltage(rdev, &min_uV, &max_uV);
  1501. if (ret < 0)
  1502. goto out;
  1503. regulator->min_uV = min_uV;
  1504. regulator->max_uV = max_uV;
  1505. ret = regulator_check_consumers(rdev, &min_uV, &max_uV);
  1506. if (ret < 0)
  1507. goto out;
  1508. ret = _regulator_do_set_voltage(rdev, min_uV, max_uV);
  1509. out:
  1510. mutex_unlock(&rdev->mutex);
  1511. return ret;
  1512. }
  1513. EXPORT_SYMBOL_GPL(regulator_set_voltage);
  1514. /**
  1515. * regulator_set_voltage_time - get raise/fall time
  1516. * @regulator: regulator source
  1517. * @old_uV: starting voltage in microvolts
  1518. * @new_uV: target voltage in microvolts
  1519. *
  1520. * Provided with the starting and ending voltage, this function attempts to
  1521. * calculate the time in microseconds required to rise or fall to this new
  1522. * voltage.
  1523. */
  1524. int regulator_set_voltage_time(struct regulator *regulator,
  1525. int old_uV, int new_uV)
  1526. {
  1527. struct regulator_dev *rdev = regulator->rdev;
  1528. struct regulator_ops *ops = rdev->desc->ops;
  1529. int old_sel = -1;
  1530. int new_sel = -1;
  1531. int voltage;
  1532. int i;
  1533. /* Currently requires operations to do this */
  1534. if (!ops->list_voltage || !ops->set_voltage_time_sel
  1535. || !rdev->desc->n_voltages)
  1536. return -EINVAL;
  1537. for (i = 0; i < rdev->desc->n_voltages; i++) {
  1538. /* We only look for exact voltage matches here */
  1539. voltage = regulator_list_voltage(regulator, i);
  1540. if (voltage < 0)
  1541. return -EINVAL;
  1542. if (voltage == 0)
  1543. continue;
  1544. if (voltage == old_uV)
  1545. old_sel = i;
  1546. if (voltage == new_uV)
  1547. new_sel = i;
  1548. }
  1549. if (old_sel < 0 || new_sel < 0)
  1550. return -EINVAL;
  1551. return ops->set_voltage_time_sel(rdev, old_sel, new_sel);
  1552. }
  1553. EXPORT_SYMBOL_GPL(regulator_set_voltage_time);
  1554. /**
  1555. * regulator_sync_voltage - re-apply last regulator output voltage
  1556. * @regulator: regulator source
  1557. *
  1558. * Re-apply the last configured voltage. This is intended to be used
  1559. * where some external control source the consumer is cooperating with
  1560. * has caused the configured voltage to change.
  1561. */
  1562. int regulator_sync_voltage(struct regulator *regulator)
  1563. {
  1564. struct regulator_dev *rdev = regulator->rdev;
  1565. int ret, min_uV, max_uV;
  1566. mutex_lock(&rdev->mutex);
  1567. if (!rdev->desc->ops->set_voltage &&
  1568. !rdev->desc->ops->set_voltage_sel) {
  1569. ret = -EINVAL;
  1570. goto out;
  1571. }
  1572. /* This is only going to work if we've had a voltage configured. */
  1573. if (!regulator->min_uV && !regulator->max_uV) {
  1574. ret = -EINVAL;
  1575. goto out;
  1576. }
  1577. min_uV = regulator->min_uV;
  1578. max_uV = regulator->max_uV;
  1579. /* This should be a paranoia check... */
  1580. ret = regulator_check_voltage(rdev, &min_uV, &max_uV);
  1581. if (ret < 0)
  1582. goto out;
  1583. ret = regulator_check_consumers(rdev, &min_uV, &max_uV);
  1584. if (ret < 0)
  1585. goto out;
  1586. ret = _regulator_do_set_voltage(rdev, min_uV, max_uV);
  1587. out:
  1588. mutex_unlock(&rdev->mutex);
  1589. return ret;
  1590. }
  1591. EXPORT_SYMBOL_GPL(regulator_sync_voltage);
  1592. static int _regulator_get_voltage(struct regulator_dev *rdev)
  1593. {
  1594. int sel, ret;
  1595. if (rdev->desc->ops->get_voltage_sel) {
  1596. sel = rdev->desc->ops->get_voltage_sel(rdev);
  1597. if (sel < 0)
  1598. return sel;
  1599. ret = rdev->desc->ops->list_voltage(rdev, sel);
  1600. } else if (rdev->desc->ops->get_voltage) {
  1601. ret = rdev->desc->ops->get_voltage(rdev);
  1602. } else {
  1603. return -EINVAL;
  1604. }
  1605. if (ret < 0)
  1606. return ret;
  1607. return ret - rdev->constraints->uV_offset;
  1608. }
  1609. /**
  1610. * regulator_get_voltage - get regulator output voltage
  1611. * @regulator: regulator source
  1612. *
  1613. * This r…