PageRenderTime 70ms CodeModel.GetById 29ms RepoModel.GetById 1ms app.codeStats 0ms

/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
Possible License(s): GPL-2.0, LGPL-2.0, AGPL-1.0
  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 returns the current regulator voltage in uV.
  1614. *
  1615. * NOTE: If the regulator is disabled it will return the voltage value. This
  1616. * function should not be used to determine regulator state.
  1617. */
  1618. int regulator_get_voltage(struct regulator *regulator)
  1619. {
  1620. int ret;
  1621. mutex_lock(&regulator->rdev->mutex);
  1622. ret = _regulator_get_voltage(regulator->rdev);
  1623. mutex_unlock(&regulator->rdev->mutex);
  1624. return ret;
  1625. }
  1626. EXPORT_SYMBOL_GPL(regulator_get_voltage);
  1627. /**
  1628. * regulator_set_current_limit - set regulator output current limit
  1629. * @regulator: regulator source
  1630. * @min_uA: Minimuum supported current in uA
  1631. * @max_uA: Maximum supported current in uA
  1632. *
  1633. * Sets current sink to the desired output current. This can be set during
  1634. * any regulator state. IOW, regulator can be disabled or enabled.
  1635. *
  1636. * If the regulator is enabled then the current will change to the new value
  1637. * immediately otherwise if the regulator is disabled the regulator will
  1638. * output at the new current when enabled.
  1639. *
  1640. * NOTE: Regulator system constraints must be set for this regulator before
  1641. * calling this function otherwise this call will fail.
  1642. */
  1643. int regulator_set_current_limit(struct regulator *regulator,
  1644. int min_uA, int max_uA)
  1645. {
  1646. struct regulator_dev *rdev = regulator->rdev;
  1647. int ret;
  1648. mutex_lock(&rdev->mutex);
  1649. /* sanity check */
  1650. if (!rdev->desc->ops->set_current_limit) {
  1651. ret = -EINVAL;
  1652. goto out;
  1653. }
  1654. /* constraints check */
  1655. ret = regulator_check_current_limit(rdev, &min_uA, &max_uA);
  1656. if (ret < 0)
  1657. goto out;
  1658. ret = rdev->desc->ops->set_current_limit(rdev, min_uA, max_uA);
  1659. out:
  1660. mutex_unlock(&rdev->mutex);
  1661. return ret;
  1662. }
  1663. EXPORT_SYMBOL_GPL(regulator_set_current_limit);
  1664. static int _regulator_get_current_limit(struct regulator_dev *rdev)
  1665. {
  1666. int ret;
  1667. mutex_lock(&rdev->mutex);
  1668. /* sanity check */
  1669. if (!rdev->desc->ops->get_current_limit) {
  1670. ret = -EINVAL;
  1671. goto out;
  1672. }
  1673. ret = rdev->desc->ops->get_current_limit(rdev);
  1674. out:
  1675. mutex_unlock(&rdev->mutex);
  1676. return ret;
  1677. }
  1678. /**
  1679. * regulator_get_current_limit - get regulator output current
  1680. * @regulator: regulator source
  1681. *
  1682. * This returns the current supplied by the specified current sink in uA.
  1683. *
  1684. * NOTE: If the regulator is disabled it will return the current value. This
  1685. * function should not be used to determine regulator state.
  1686. */
  1687. int regulator_get_current_limit(struct regulator *regulator)
  1688. {
  1689. return _regulator_get_current_limit(regulator->rdev);
  1690. }
  1691. EXPORT_SYMBOL_GPL(regulator_get_current_limit);
  1692. /**
  1693. * regulator_set_mode - set regulator operating mode
  1694. * @regulator: regulator source
  1695. * @mode: operating mode - one of the REGULATOR_MODE constants
  1696. *
  1697. * Set regulator operating mode to increase regulator efficiency or improve
  1698. * regulation performance.
  1699. *
  1700. * NOTE: Regulator system constraints must be set for this regulator before
  1701. * calling this function otherwise this call will fail.
  1702. */
  1703. int regulator_set_mode(struct regulator *regulator, unsigned int mode)
  1704. {
  1705. struct regulator_dev *rdev = regulator->rdev;
  1706. int ret;
  1707. int regulator_curr_mode;
  1708. mutex_lock(&rdev->mutex);
  1709. /* sanity check */
  1710. if (!rdev->desc->ops->set_mode) {
  1711. ret = -EINVAL;
  1712. goto out;
  1713. }
  1714. /* return if the same mode is requested */
  1715. if (rdev->desc->ops->get_mode) {
  1716. regulator_curr_mode = rdev->desc->ops->get_mode(rdev);
  1717. if (regulator_curr_mode == mode) {
  1718. ret = 0;
  1719. goto out;
  1720. }
  1721. }
  1722. /* constraints check */
  1723. ret = regulator_mode_constrain(rdev, &mode);
  1724. if (ret < 0)
  1725. goto out;
  1726. ret = rdev->desc->ops->set_mode(rdev, mode);
  1727. out:
  1728. mutex_unlock(&rdev->mutex);
  1729. return ret;
  1730. }
  1731. EXPORT_SYMBOL_GPL(regulator_set_mode);
  1732. static unsigned int _regulator_get_mode(struct regulator_dev *rdev)
  1733. {
  1734. int ret;
  1735. mutex_lock(&rdev->mutex);
  1736. /* sanity check */
  1737. if (!rdev->desc->ops->get_mode) {
  1738. ret = -EINVAL;
  1739. goto out;
  1740. }
  1741. ret = rdev->desc->ops->get_mode(rdev);
  1742. out:
  1743. mutex_unlock(&rdev->mutex);
  1744. return ret;
  1745. }
  1746. /**
  1747. * regulator_get_mode - get regulator operating mode
  1748. * @regulator: regulator source
  1749. *
  1750. * Get the current regulator operating mode.
  1751. */
  1752. unsigned int regulator_get_mode(struct regulator *regulator)
  1753. {
  1754. return _regulator_get_mode(regulator->rdev);
  1755. }
  1756. EXPORT_SYMBOL_GPL(regulator_get_mode);
  1757. /**
  1758. * regulator_set_optimum_mode - set regulator optimum operating mode
  1759. * @regulator: regulator source
  1760. * @uA_load: load current
  1761. *
  1762. * Notifies the regulator core of a new device load. This is then used by
  1763. * DRMS (if enabled by constraints) to set the most efficient regulator
  1764. * operating mode for the new regulator loading.
  1765. *
  1766. * Consumer devices notify their supply regulator of the maximum power
  1767. * they will require (can be taken from device datasheet in the power
  1768. * consumption tables) when they change operational status and hence power
  1769. * state. Examples of operational state changes that can affect power
  1770. * consumption are :-
  1771. *
  1772. * o Device is opened / closed.
  1773. * o Device I/O is about to begin or has just finished.
  1774. * o Device is idling in between work.
  1775. *
  1776. * This information is also exported via sysfs to userspace.
  1777. *
  1778. * DRMS will sum the total requested load on the regulator and change
  1779. * to the most efficient operating mode if platform constraints allow.
  1780. *
  1781. * Returns the new regulator mode or error.
  1782. */
  1783. int regulator_set_optimum_mode(struct regulator *regulator, int uA_load)
  1784. {
  1785. struct regulator_dev *rdev = regulator->rdev;
  1786. struct regulator *consumer;
  1787. int ret, output_uV, input_uV, total_uA_load = 0;
  1788. unsigned int mode;
  1789. mutex_lock(&rdev->mutex);
  1790. /*
  1791. * first check to see if we can set modes at all, otherwise just
  1792. * tell the consumer everything is OK.
  1793. */
  1794. regulator->uA_load = uA_load;
  1795. ret = regulator_check_drms(rdev);
  1796. if (ret < 0) {
  1797. ret = 0;
  1798. goto out;
  1799. }
  1800. if (!rdev->desc->ops->get_optimum_mode)
  1801. goto out;
  1802. /*
  1803. * we can actually do this so any errors are indicators of
  1804. * potential real failure.
  1805. */
  1806. ret = -EINVAL;
  1807. /* get output voltage */
  1808. output_uV = _regulator_get_voltage(rdev);
  1809. if (output_uV <= 0) {
  1810. rdev_err(rdev, "invalid output voltage found\n");
  1811. goto out;
  1812. }
  1813. /* get input voltage */
  1814. input_uV = 0;
  1815. if (rdev->supply)
  1816. input_uV = _regulator_get_voltage(rdev->supply);
  1817. if (input_uV <= 0)
  1818. input_uV = rdev->constraints->input_uV;
  1819. if (input_uV <= 0) {
  1820. rdev_err(rdev, "invalid input voltage found\n");
  1821. goto out;
  1822. }
  1823. /* calc total requested load for this regulator */
  1824. list_for_each_entry(consumer, &rdev->consumer_list, list)
  1825. total_uA_load += consumer->uA_load;
  1826. mode = rdev->desc->ops->get_optimum_mode(rdev,
  1827. input_uV, output_uV,
  1828. total_uA_load);
  1829. ret = regulator_mode_constrain(rdev, &mode);
  1830. if (ret < 0) {
  1831. rdev_err(rdev, "failed to get optimum mode @ %d uA %d -> %d uV\n",
  1832. total_uA_load, input_uV, output_uV);
  1833. goto out;
  1834. }
  1835. ret = rdev->desc->ops->set_mode(rdev, mode);
  1836. if (ret < 0) {
  1837. rdev_err(rdev, "failed to set optimum mode %x\n", mode);
  1838. goto out;
  1839. }
  1840. ret = mode;
  1841. out:
  1842. mutex_unlock(&rdev->mutex);
  1843. return ret;
  1844. }
  1845. EXPORT_SYMBOL_GPL(regulator_set_optimum_mode);
  1846. /**
  1847. * regulator_register_notifier - register regulator event notifier
  1848. * @regulator: regulator source
  1849. * @nb: notifier block
  1850. *
  1851. * Register notifier block to receive regulator events.
  1852. */
  1853. int regulator_register_notifier(struct regulator *regulator,
  1854. struct notifier_block *nb)
  1855. {
  1856. return blocking_notifier_chain_register(&regulator->rdev->notifier,
  1857. nb);
  1858. }
  1859. EXPORT_SYMBOL_GPL(regulator_register_notifier);
  1860. /**
  1861. * regulator_unregister_notifier - unregister regulator event notifier
  1862. * @regulator: regulator source
  1863. * @nb: notifier block
  1864. *
  1865. * Unregister regulator event notifier block.
  1866. */
  1867. int regulator_unregister_notifier(struct regulator *regulator,
  1868. struct notifier_block *nb)
  1869. {
  1870. return blocking_notifier_chain_unregister(&regulator->rdev->notifier,
  1871. nb);
  1872. }
  1873. EXPORT_SYMBOL_GPL(regulator_unregister_notifier);
  1874. /* notify regulator consumers and downstream regulator consumers.
  1875. * Note mutex must be held by caller.
  1876. */
  1877. static void _notifier_call_chain(struct regulator_dev *rdev,
  1878. unsigned long event, void *data)
  1879. {
  1880. struct regulator_dev *_rdev;
  1881. /* call rdev chain first */
  1882. blocking_notifier_call_chain(&rdev->notifier, event, NULL);
  1883. /* now notify regulator we supply */
  1884. list_for_each_entry(_rdev, &rdev->supply_list, slist) {
  1885. mutex_lock(&_rdev->mutex);
  1886. _notifier_call_chain(_rdev, event, data);
  1887. mutex_unlock(&_rdev->mutex);
  1888. }
  1889. }
  1890. /**
  1891. * regulator_bulk_get - get multiple regulator consumers
  1892. *
  1893. * @dev: Device to supply
  1894. * @num_consumers: Number of consumers to register
  1895. * @consumers: Configuration of consumers; clients are stored here.
  1896. *
  1897. * @return 0 on success, an errno on failure.
  1898. *
  1899. * This helper function allows drivers to get several regulator
  1900. * consumers in one operation. If any of the regulators cannot be
  1901. * acquired then any regulators that were allocated will be freed
  1902. * before returning to the caller.
  1903. */
  1904. int regulator_bulk_get(struct device *dev, int num_consumers,
  1905. struct regulator_bulk_data *consumers)
  1906. {
  1907. int i;
  1908. int ret;
  1909. for (i = 0; i < num_consumers; i++)
  1910. consumers[i].consumer = NULL;
  1911. for (i = 0; i < num_consumers; i++) {
  1912. consumers[i].consumer = regulator_get(dev,
  1913. consumers[i].supply);
  1914. if (IS_ERR(consumers[i].consumer)) {
  1915. ret = PTR_ERR(consumers[i].consumer);
  1916. dev_err(dev, "Failed to get supply '%s': %d\n",
  1917. consumers[i].supply, ret);
  1918. consumers[i].consumer = NULL;
  1919. goto err;
  1920. }
  1921. }
  1922. return 0;
  1923. err:
  1924. for (i = 0; i < num_consumers && consumers[i].consumer; i++)
  1925. regulator_put(consumers[i].consumer);
  1926. return ret;
  1927. }
  1928. EXPORT_SYMBOL_GPL(regulator_bulk_get);
  1929. /**
  1930. * regulator_bulk_enable - enable multiple regulator consumers
  1931. *
  1932. * @num_consumers: Number of consumers
  1933. * @consumers: Consumer data; clients are stored here.
  1934. * @return 0 on success, an errno on failure
  1935. *
  1936. * This convenience API allows consumers to enable multiple regulator
  1937. * clients in a single API call. If any consumers cannot be enabled
  1938. * then any others that were enabled will be disabled again prior to
  1939. * return.
  1940. */
  1941. int regulator_bulk_enable(int num_consumers,
  1942. struct regulator_bulk_data *consumers)
  1943. {
  1944. int i;
  1945. int ret;
  1946. for (i = 0; i < num_consumers; i++) {
  1947. ret = regulator_enable(consumers[i].consumer);
  1948. if (ret != 0)
  1949. goto err;
  1950. }
  1951. return 0;
  1952. err:
  1953. pr_err("Failed to enable %s: %d\n", consumers[i].supply, ret);
  1954. for (--i; i >= 0; --i)
  1955. regulator_disable(consumers[i].consumer);
  1956. return ret;
  1957. }
  1958. EXPORT_SYMBOL_GPL(regulator_bulk_enable);
  1959. /**
  1960. * regulator_bulk_disable - disable multiple regulator consumers
  1961. *
  1962. * @num_consumers: Number of consumers
  1963. * @consumers: Consumer data; clients are stored here.
  1964. * @return 0 on success, an errno on failure
  1965. *
  1966. * This convenience API allows consumers to disable multiple regulator
  1967. * clients in a single API call. If any consumers cannot be enabled
  1968. * then any others that were disabled will be disabled again prior to
  1969. * return.
  1970. */
  1971. int regulator_bulk_disable(int num_consumers,
  1972. struct regulator_bulk_data *consumers)
  1973. {
  1974. int i;
  1975. int ret;
  1976. for (i = 0; i < num_consumers; i++) {
  1977. ret = regulator_disable(consumers[i].consumer);
  1978. if (ret != 0)
  1979. goto err;
  1980. }
  1981. return 0;
  1982. err:
  1983. pr_err("Failed to disable %s: %d\n", consumers[i].supply, ret);
  1984. for (--i; i >= 0; --i)
  1985. regulator_enable(consumers[i].consumer);
  1986. return ret;
  1987. }
  1988. EXPORT_SYMBOL_GPL(regulator_bulk_disable);
  1989. /**
  1990. * regulator_bulk_free - free multiple regulator consumers
  1991. *
  1992. * @num_consumers: Number of consumers
  1993. * @consumers: Consumer data; clients are stored here.
  1994. *
  1995. * This convenience API allows consumers to free multiple regulator
  1996. * clients in a single API call.
  1997. */
  1998. void regulator_bulk_free(int num_consumers,
  1999. struct regulator_bulk_data *consumers)
  2000. {
  2001. int i;
  2002. for (i = 0; i < num_consumers; i++) {
  2003. regulator_put(consumers[i].consumer);
  2004. consumers[i].consumer = NULL;
  2005. }
  2006. }
  2007. EXPORT_SYMBOL_GPL(regulator_bulk_free);
  2008. /**
  2009. * regulator_notifier_call_chain - call regulator event notifier
  2010. * @rdev: regulator source
  2011. * @event: notifier block
  2012. * @data: callback-specific data.
  2013. *
  2014. * Called by regulator drivers to notify clients a regulator event has
  2015. * occurred. We also notify regulator clients downstream.
  2016. * Note lock must be held by caller.
  2017. */
  2018. int regulator_notifier_call_chain(struct regulator_dev *rdev,
  2019. unsigned long event, void *data)
  2020. {
  2021. _notifier_call_chain(rdev, event, data);
  2022. return NOTIFY_DONE;
  2023. }
  2024. EXPORT_SYMBOL_GPL(regulator_notifier_call_chain);
  2025. /**
  2026. * regulator_mode_to_status - convert a regulator mode into a status
  2027. *
  2028. * @mode: Mode to convert
  2029. *
  2030. * Convert a regulator mode into a status.
  2031. */
  2032. int regulator_mode_to_status(unsigned int mode)
  2033. {
  2034. switch (mode) {
  2035. case REGULATOR_MODE_FAST:
  2036. return REGULATOR_STATUS_FAST;
  2037. case REGULATOR_MODE_NORMAL:
  2038. return REGULATOR_STATUS_NORMAL;
  2039. case REGULATOR_MODE_IDLE:
  2040. return REGULATOR_STATUS_IDLE;
  2041. case REGULATOR_STATUS_STANDBY:
  2042. return REGULATOR_STATUS_STANDBY;
  2043. default:
  2044. return 0;
  2045. }
  2046. }
  2047. EXPORT_SYMBOL_GPL(regulator_mode_to_status);
  2048. /*
  2049. * To avoid cluttering sysfs (and memory) with useless state, only
  2050. * create attributes that can be meaningfully displayed.
  2051. */
  2052. static int add_regulator_attributes(struct regulator_dev *rdev)
  2053. {
  2054. struct device *dev = &rdev->dev;
  2055. struct regulator_ops *ops = rdev->desc->ops;
  2056. int status = 0;
  2057. /* some attributes need specific methods to be displayed */
  2058. if (ops->get_voltage || ops->get_voltage_sel) {
  2059. status = device_create_file(dev, &dev_attr_microvolts);
  2060. if (status < 0)
  2061. return status;
  2062. }
  2063. if (ops->get_current_limit) {
  2064. status = device_create_file(dev, &dev_attr_microamps);
  2065. if (status < 0)
  2066. return status;
  2067. }
  2068. if (ops->get_mode) {
  2069. status = device_create_file(dev, &dev_attr_opmode);
  2070. if (status < 0)
  2071. return status;
  2072. }
  2073. if (ops->is_enabled) {
  2074. status = device_create_file(dev, &dev_attr_state);
  2075. if (status < 0)
  2076. return status;
  2077. }
  2078. if (ops->get_status) {
  2079. status = device_create_file(dev, &dev_attr_status);
  2080. if (status < 0)
  2081. return status;
  2082. }
  2083. /* some attributes are type-specific */
  2084. if (rdev->desc->type == REGULATOR_CURRENT) {
  2085. status = device_create_file(dev, &dev_attr_requested_microamps);
  2086. if (status < 0)
  2087. return status;
  2088. }
  2089. /* all the other attributes exist to support constraints;
  2090. * don't show them if there are no constraints, or if the
  2091. * relevant supporting methods are missing.
  2092. */
  2093. if (!rdev->constraints)
  2094. return status;
  2095. /* constraints need specific supporting methods */
  2096. if (ops->set_voltage || ops->set_voltage_sel) {
  2097. status = device_create_file(dev, &dev_attr_min_microvolts);
  2098. if (status < 0)
  2099. return status;
  2100. status = device_create_file(dev, &dev_attr_max_microvolts);
  2101. if (status < 0)
  2102. return status;
  2103. }
  2104. if (ops->set_current_limit) {
  2105. status = device_create_file(dev, &dev_attr_min_microamps);
  2106. if (status < 0)
  2107. return status;
  2108. status = device_create_file(dev, &dev_attr_max_microamps);
  2109. if (status < 0)
  2110. return status;
  2111. }
  2112. /* suspend mode constraints need multiple supporting methods */
  2113. if (!(ops->set_suspend_enable && ops->set_suspend_disable))
  2114. return status;
  2115. status = device_create_file(dev, &dev_attr_suspend_standby_state);
  2116. if (status < 0)
  2117. return status;
  2118. status = device_create_file(dev, &dev_attr_suspend_mem_state);
  2119. if (status < 0)
  2120. return status;
  2121. status = device_create_file(dev, &dev_attr_suspend_disk_state);
  2122. if (status < 0)
  2123. return status;
  2124. if (ops->set_suspend_voltage) {
  2125. status = device_create_file(dev,
  2126. &dev_attr_suspend_standby_microvolts);
  2127. if (status < 0)
  2128. return status;
  2129. status = device_create_file(dev,
  2130. &dev_attr_suspend_mem_microvolts);
  2131. if (status < 0)
  2132. return status;
  2133. status = device_create_file(dev,
  2134. &dev_attr_suspend_disk_microvolts);
  2135. if (status < 0)
  2136. return status;
  2137. }
  2138. if (ops->set_suspend_mode) {
  2139. status = device_create_file(dev,
  2140. &dev_attr_suspend_standby_mode);
  2141. if (status < 0)
  2142. return status;
  2143. status = device_create_file(dev,
  2144. &dev_attr_suspend_mem_mode);
  2145. if (status < 0)
  2146. return status;
  2147. status = device_create_file(dev,
  2148. &dev_attr_suspend_disk_mode);
  2149. if (status < 0)
  2150. return status;
  2151. }
  2152. return status;
  2153. }
  2154. static void rdev_init_debugfs(struct regulator_dev *rdev)
  2155. {
  2156. #ifdef CONFIG_DEBUG_FS
  2157. rdev->debugfs = debugfs_create_dir(rdev_get_name(rdev), debugfs_root);
  2158. if (IS_ERR(rdev->debugfs) || !rdev->debugfs) {
  2159. rdev_warn(rdev, "Failed to create debugfs directory\n");
  2160. rdev->debugfs = NULL;
  2161. return;
  2162. }
  2163. debugfs_create_u32("use_count", 0444, rdev->debugfs,
  2164. &rdev->use_count);
  2165. debugfs_create_u32("open_count", 0444, rdev->debugfs,
  2166. &rdev->open_count);
  2167. #endif
  2168. }
  2169. /**
  2170. * regulator_register - register regulator
  2171. * @regulator_desc: regulator to register
  2172. * @dev: struct device for the regulator
  2173. * @init_data: platform provided init data, passed through by driver
  2174. * @driver_data: private regulator data
  2175. *
  2176. * Called by regulator drivers to register a regulator.
  2177. * Returns 0 on success.
  2178. */
  2179. struct regulator_dev *regulator_register(struct regulator_desc *regulator_desc,
  2180. struct device *dev, const struct regulator_init_data *init_data,
  2181. void *driver_data)
  2182. {
  2183. static atomic_t regulator_no = ATOMIC_INIT(0);
  2184. struct regulator_dev *rdev;
  2185. int ret, i;
  2186. if (regulator_desc == NULL)
  2187. return ERR_PTR(-EINVAL);
  2188. if (regulator_desc->name == NULL || regulator_desc->ops == NULL)
  2189. return ERR_PTR(-EINVAL);
  2190. if (regulator_desc->type != REGULATOR_VOLTAGE &&
  2191. regulator_desc->type != REGULATOR_CURRENT)
  2192. return ERR_PTR(-EINVAL);
  2193. if (!init_data)
  2194. return ERR_PTR(-EINVAL);
  2195. /* Only one of each should be implemented */
  2196. WARN_ON(regulator_desc->ops->get_voltage &&
  2197. regulator_desc->ops->get_voltage_sel);
  2198. WARN_ON(regulator_desc->ops->set_voltage &&
  2199. regulator_desc->ops->set_voltage_sel);
  2200. /* If we're using selectors we must implement list_voltage. */
  2201. if (regulator_desc->ops->get_voltage_sel &&
  2202. !regulator_desc->ops->list_voltage) {
  2203. return ERR_PTR(-EINVAL);
  2204. }
  2205. if (regulator_desc->ops->set_voltage_sel &&
  2206. !regulator_desc->ops->list_voltage) {
  2207. return ERR_PTR(-EINVAL);
  2208. }
  2209. rdev = kzalloc(sizeof(struct regulator_dev), GFP_KERNEL);
  2210. if (rdev == NULL)
  2211. return ERR_PTR(-ENOMEM);
  2212. mutex_lock(&regulator_list_mutex);
  2213. mutex_init(&rdev->mutex);
  2214. rdev->reg_data = driver_data;
  2215. rdev->owner = regulator_desc->owner;
  2216. rdev->desc = regulator_desc;
  2217. INIT_LIST_HEAD(&rdev->consumer_list);
  2218. INIT_LIST_HEAD(&rdev->supply_list);
  2219. INIT_LIST_HEAD(&rdev->list);
  2220. INIT_LIST_HEAD(&rdev->slist);
  2221. BLOCKING_INIT_NOTIFIER_HEAD(&rdev->notifier);
  2222. /* preform any regulator specific init */
  2223. if (init_data->regulator_init) {
  2224. ret = init_data->regulator_init(rdev->reg_data);
  2225. if (ret < 0)
  2226. goto clean;
  2227. }
  2228. /* register with sysfs */
  2229. rdev->dev.class = &regulator_class;
  2230. rdev->dev.parent = dev;
  2231. dev_set_name(&rdev->dev, "regulator.%d",
  2232. atomic_inc_return(&regulator_no) - 1);
  2233. ret = device_register(&rdev->dev);
  2234. if (ret != 0) {
  2235. put_device(&rdev->dev);
  2236. goto clean;
  2237. }
  2238. dev_set_drvdata(&rdev->dev, rdev);
  2239. /* set regulator constraints */
  2240. ret = set_machine_constraints(rdev, &init_data->constraints);
  2241. if (ret < 0)
  2242. goto scrub;
  2243. /* add attributes supported by this regulator */
  2244. ret = add_regulator_attributes(rdev);
  2245. if (ret < 0)
  2246. goto scrub;
  2247. if (init_data->supply_regulator) {
  2248. struct regulator_dev *r;
  2249. int found = 0;
  2250. list_for_each_entry(r, &regulator_list, list) {
  2251. if (strcmp(rdev_get_name(r),
  2252. init_data->supply_regulator) == 0) {
  2253. found = 1;
  2254. break;
  2255. }
  2256. }
  2257. if (!found) {
  2258. dev_err(dev, "Failed to find supply %s\n",
  2259. init_data->supply_regulator);
  2260. ret = -ENODEV;
  2261. goto scrub;
  2262. }
  2263. ret = set_supply(rdev, r);
  2264. if (ret < 0)
  2265. goto scrub;
  2266. }
  2267. /* add consumers devices */
  2268. for (i = 0; i < init_data->num_consumer_supplies; i++) {
  2269. ret = set_consumer_device_supply(rdev,
  2270. init_data->consumer_supplies[i].dev,
  2271. init_data->consumer_supplies[i].dev_name,
  2272. init_data->consumer_supplies[i].supply);
  2273. if (ret < 0) {
  2274. dev_err(dev, "Failed to set supply %s\n",
  2275. init_data->consumer_supplies[i].supply);
  2276. goto unset_supplies;
  2277. }
  2278. }
  2279. list_add(&rdev->list, &regulator_list);
  2280. rdev_init_debugfs(rdev);
  2281. out:
  2282. mutex_unlock(&regulator_list_mutex);
  2283. return rdev;
  2284. unset_supplies:
  2285. unset_regulator_supplies(rdev);
  2286. scrub:
  2287. device_unregister(&rdev->dev);
  2288. /* device core frees rdev */
  2289. rdev = ERR_PTR(ret);
  2290. goto out;
  2291. clean:
  2292. kfree(rdev);
  2293. rdev = ERR_PTR(ret);
  2294. goto out;
  2295. }
  2296. EXPORT_SYMBOL_GPL(regulator_register);
  2297. /**
  2298. * regulator_unregister - unregister regulator
  2299. * @rdev: regulator to unregister
  2300. *
  2301. * Called by regulator drivers to unregister a regulator.
  2302. */
  2303. void regulator_unregister(struct regulator_dev *rdev)
  2304. {
  2305. if (rdev == NULL)
  2306. return;
  2307. mutex_lock(&regulator_list_mutex);
  2308. #ifdef CONFIG_DEBUG_FS
  2309. debugfs_remove_recursive(rdev->debugfs);
  2310. #endif
  2311. WARN_ON(rdev->open_count);
  2312. unset_regulator_supplies(rdev);
  2313. list_del(&rdev->list);
  2314. if (rdev->supply)
  2315. sysfs_remove_link(&rdev->dev.kobj, "supply");
  2316. device_unregister(&rdev->dev);
  2317. kfree(rdev->constraints);
  2318. mutex_unlock(&regulator_list_mutex);
  2319. }
  2320. EXPORT_SYMBOL_GPL(regulator_unregister);
  2321. /**
  2322. * regulator_suspend_prepare - prepare regulators for system wide suspend
  2323. * @state: system suspend state
  2324. *
  2325. * Configure each regulator with it's suspend operating parameters for state.
  2326. * This will usually be called by machine suspend code prior to supending.
  2327. */
  2328. int regulator_suspend_prepare(suspend_state_t state)
  2329. {
  2330. struct regulator_dev *rdev;
  2331. int ret = 0;
  2332. /* ON is handled by regulator active state */
  2333. if (state == PM_SUSPEND_ON)
  2334. return -EINVAL;
  2335. mutex_lock(&regulator_list_mutex);
  2336. list_for_each_entry(rdev, &regulator_list, list) {
  2337. mutex_lock(&rdev->mutex);
  2338. ret = suspend_prepare(rdev, state);
  2339. mutex_unlock(&rdev->mutex);
  2340. if (ret < 0) {
  2341. rdev_err(rdev, "failed to prepare\n");
  2342. goto out;
  2343. }
  2344. }
  2345. out:
  2346. mutex_unlock(&regulator_list_mutex);
  2347. return ret;
  2348. }
  2349. EXPORT_SYMBOL_GPL(regulator_suspend_prepare);
  2350. /**
  2351. * regulator_suspend_finish - resume regulators from system wide suspend
  2352. *
  2353. * Turn on regulators that might be turned off by regulator_suspend_prepare
  2354. * and that should be turned on according to the regulators properties.
  2355. */
  2356. int regulator_suspend_finish(void)
  2357. {
  2358. struct regulator_dev *rdev;
  2359. int ret = 0, error;
  2360. mutex_lock(&regulator_list_mutex);
  2361. list_for_each_entry(rdev, &regulator_list, list) {
  2362. struct regulator_ops *ops = rdev->desc->ops;
  2363. mutex_lock(&rdev->mutex);
  2364. if ((rdev->use_count > 0 || rdev->constraints->always_on) &&
  2365. ops->enable) {
  2366. error = ops->enable(rdev);
  2367. if (error)
  2368. ret = error;
  2369. } else {
  2370. if (!has_full_constraints)
  2371. goto unlock;
  2372. if (!ops->disable)
  2373. goto unlock;
  2374. if (ops->is_enabled && !ops->is_enabled(rdev))
  2375. goto unlock;
  2376. error = ops->disable(rdev);
  2377. if (error)
  2378. ret = error;
  2379. }
  2380. unlock:
  2381. mutex_unlock(&rdev->mutex);
  2382. }
  2383. mutex_unlock(&regulator_list_mutex);
  2384. return ret;
  2385. }
  2386. EXPORT_SYMBOL_GPL(regulator_suspend_finish);
  2387. /**
  2388. * regulator_has_full_constraints - the system has fully specified constraints
  2389. *
  2390. * Calling this function will cause the regulator API to disable all
  2391. * regulators which have a zero use count and don't have an always_on
  2392. * constraint in a late_initcall.
  2393. *
  2394. * The intention is that this will become the default behaviour in a
  2395. * future kernel release so users are encouraged to use this facility
  2396. * now.
  2397. */
  2398. void regulator_has_full_constraints(void)
  2399. {
  2400. has_full_constraints = 1;
  2401. }
  2402. EXPORT_SYMBOL_GPL(regulator_has_full_constraints);
  2403. /**
  2404. * regulator_use_dummy_regulator - Provide a dummy regulator when none is found
  2405. *
  2406. * Calling this function will cause the regulator API to provide a
  2407. * dummy regulator to consumers if no physical regulator is found,
  2408. * allowing most consumers to proceed as though a regulator were
  2409. * configured. This allows systems such as those with software
  2410. * controllable regulators for the CPU core only to be brought up more
  2411. * readily.
  2412. */
  2413. void regulator_use_dummy_regulator(void)
  2414. {
  2415. board_wants_dummy_regulator = true;
  2416. }
  2417. EXPORT_SYMBOL_GPL(regulator_use_dummy_regulator);
  2418. /**
  2419. * rdev_get_drvdata - get rdev regulator driver data
  2420. * @rdev: regulator
  2421. *
  2422. * Get rdev regulator driver private data. This call can be used in the
  2423. * regulator driver context.
  2424. */
  2425. void *rdev_get_drvdata(struct regulator_dev *rdev)
  2426. {
  2427. return rdev->reg_data;
  2428. }
  2429. EXPORT_SYMBOL_GPL(rdev_get_drvdata);
  2430. /**
  2431. * regulator_get_drvdata - get regulator driver data
  2432. * @regulator: regulator
  2433. *
  2434. * Get regulator driver private data. This call can be used in the consumer
  2435. * driver context when non API regulator specific functions need to be called.
  2436. */
  2437. void *regulator_get_drvdata(struct regulator *regulator)
  2438. {
  2439. return regulator->rdev->reg_data;
  2440. }
  2441. EXPORT_SYMBOL_GPL(regulator_get_drvdata);
  2442. /**
  2443. * regulator_set_drvdata - set regulator driver data
  2444. * @regulator: regulator
  2445. * @data: data
  2446. */
  2447. void regulator_set_drvdata(struct regulator *regulator, void *data)
  2448. {
  2449. regulator->rdev->reg_data = data;
  2450. }
  2451. EXPORT_SYMBOL_GPL(regulator_set_drvdata);
  2452. /**
  2453. * regulator_get_id - get regulator ID
  2454. * @rdev: regulator
  2455. */
  2456. int rdev_get_id(struct regulator_dev *rdev)
  2457. {
  2458. return rdev->desc->id;
  2459. }
  2460. EXPORT_SYMBOL_GPL(rdev_get_id);
  2461. struct device *rdev_get_dev(struct regulator_dev *rdev)
  2462. {
  2463. return &rdev->dev;
  2464. }
  2465. EXPORT_SYMBOL_GPL(rdev_get_dev);
  2466. void *regulator_get_init_drvdata(struct regulator_init_data *reg_init_data)
  2467. {
  2468. return reg_init_data->driver_data;
  2469. }
  2470. EXPORT_SYMBOL_GPL(regulator_get_init_drvdata);
  2471. static int __init regulator_init(void)
  2472. {
  2473. int ret;
  2474. ret = class_register(&regulator_class);
  2475. #ifdef CONFIG_DEBUG_FS
  2476. debugfs_root = debugfs_create_dir("regulator", NULL);
  2477. if (IS_ERR(debugfs_root) || !debugfs_root) {
  2478. pr_warn("regulator: Failed to create debugfs directory\n");
  2479. debugfs_root = NULL;
  2480. }
  2481. #endif
  2482. regulator_dummy_init();
  2483. return ret;
  2484. }
  2485. /* init early to allow our consumers to complete system booting */
  2486. core_initcall(regulator_init);
  2487. static int __init regulator_init_complete(void)
  2488. {
  2489. struct regulator_dev *rdev;
  2490. struct regulator_ops *ops;
  2491. struct regulation_constraints *c;
  2492. int enabled, ret;
  2493. mutex_lock(&regulator_list_mutex);
  2494. /* If we have a full configuration then disable any regulators
  2495. * which are not in use or always_on. This will become the
  2496. * default behaviour in the future.
  2497. */
  2498. list_for_each_entry(rdev, &regulator_list, list) {
  2499. ops = rdev->desc->ops;
  2500. c = rdev->constraints;
  2501. if (!ops->disable || (c && c->always_on))
  2502. continue;
  2503. mutex_lock(&rdev->mutex);
  2504. if (rdev->use_count)
  2505. goto unlock;
  2506. /* If we can't read the status assume it's on. */
  2507. if (ops->is_enabled)
  2508. enabled = ops->is_enabled(rdev);
  2509. else
  2510. enabled = 1;
  2511. if (!enabled)
  2512. goto unlock;
  2513. if (has_full_constraints) {
  2514. /* We log since this may kill the system if it
  2515. * goes wrong. */
  2516. rdev_info(rdev, "disabling\n");
  2517. ret = ops->disable(rdev);
  2518. if (ret != 0) {
  2519. rdev_err(rdev, "couldn't disable: %d\n", ret);
  2520. }
  2521. } else {
  2522. /* The intention is that in future we will
  2523. * assume that full constraints are provided
  2524. * so warn even if we aren't going to do
  2525. * anything here.
  2526. */
  2527. rdev_warn(rdev, "incomplete constraints, leaving on\n");
  2528. }
  2529. unlock:
  2530. mutex_unlock(&rdev->mutex);
  2531. }
  2532. mutex_unlock(&regulator_list_mutex);
  2533. return 0;
  2534. }
  2535. late_initcall(regulator_init_complete);