consumer.h 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612
  1. /*
  2. * consumer.h -- SoC Regulator consumer support.
  3. *
  4. * Copyright (C) 2007, 2008 Wolfson Microelectronics PLC.
  5. *
  6. * Author: Liam Girdwood <lrg@slimlogic.co.uk>
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License version 2 as
  10. * published by the Free Software Foundation.
  11. *
  12. * Regulator Consumer Interface.
  13. *
  14. * A Power Management Regulator framework for SoC based devices.
  15. * Features:-
  16. * o Voltage and current level control.
  17. * o Operating mode control.
  18. * o Regulator status.
  19. * o sysfs entries for showing client devices and status
  20. *
  21. * EXPERIMENTAL FEATURES:
  22. * Dynamic Regulator operating Mode Switching (DRMS) - allows regulators
  23. * to use most efficient operating mode depending upon voltage and load and
  24. * is transparent to client drivers.
  25. *
  26. * e.g. Devices x,y,z share regulator r. Device x and y draw 20mA each during
  27. * IO and 1mA at idle. Device z draws 100mA when under load and 5mA when
  28. * idling. Regulator r has > 90% efficiency in NORMAL mode at loads > 100mA
  29. * but this drops rapidly to 60% when below 100mA. Regulator r has > 90%
  30. * efficiency in IDLE mode at loads < 10mA. Thus regulator r will operate
  31. * in normal mode for loads > 10mA and in IDLE mode for load <= 10mA.
  32. *
  33. */
  34. #ifndef __LINUX_REGULATOR_CONSUMER_H_
  35. #define __LINUX_REGULATOR_CONSUMER_H_
  36. #include <linux/err.h>
  37. struct device;
  38. struct notifier_block;
  39. struct regmap;
  40. /*
  41. * Regulator operating modes.
  42. *
  43. * Regulators can run in a variety of different operating modes depending on
  44. * output load. This allows further system power savings by selecting the
  45. * best (and most efficient) regulator mode for a desired load.
  46. *
  47. * Most drivers will only care about NORMAL. The modes below are generic and
  48. * will probably not match the naming convention of your regulator data sheet
  49. * but should match the use cases in the datasheet.
  50. *
  51. * In order of power efficiency (least efficient at top).
  52. *
  53. * Mode Description
  54. * FAST Regulator can handle fast changes in it's load.
  55. * e.g. useful in CPU voltage & frequency scaling where
  56. * load can quickly increase with CPU frequency increases.
  57. *
  58. * NORMAL Normal regulator power supply mode. Most drivers will
  59. * use this mode.
  60. *
  61. * IDLE Regulator runs in a more efficient mode for light
  62. * loads. Can be used for devices that have a low power
  63. * requirement during periods of inactivity. This mode
  64. * may be more noisy than NORMAL and may not be able
  65. * to handle fast load switching.
  66. *
  67. * STANDBY Regulator runs in the most efficient mode for very
  68. * light loads. Can be used by devices when they are
  69. * in a sleep/standby state. This mode is likely to be
  70. * the most noisy and may not be able to handle fast load
  71. * switching.
  72. *
  73. * NOTE: Most regulators will only support a subset of these modes. Some
  74. * will only just support NORMAL.
  75. *
  76. * These modes can be OR'ed together to make up a mask of valid register modes.
  77. */
  78. #define REGULATOR_MODE_INVALID 0x0
  79. #define REGULATOR_MODE_FAST 0x1
  80. #define REGULATOR_MODE_NORMAL 0x2
  81. #define REGULATOR_MODE_IDLE 0x4
  82. #define REGULATOR_MODE_STANDBY 0x8
  83. /*
  84. * Regulator notifier events.
  85. *
  86. * UNDER_VOLTAGE Regulator output is under voltage.
  87. * OVER_CURRENT Regulator output current is too high.
  88. * REGULATION_OUT Regulator output is out of regulation.
  89. * FAIL Regulator output has failed.
  90. * OVER_TEMP Regulator over temp.
  91. * FORCE_DISABLE Regulator forcibly shut down by software.
  92. * VOLTAGE_CHANGE Regulator voltage changed.
  93. * Data passed is old voltage cast to (void *).
  94. * DISABLE Regulator was disabled.
  95. * PRE_VOLTAGE_CHANGE Regulator is about to have voltage changed.
  96. * Data passed is "struct pre_voltage_change_data"
  97. * ABORT_VOLTAGE_CHANGE Regulator voltage change failed for some reason.
  98. * Data passed is old voltage cast to (void *).
  99. * PRE_DISABLE Regulator is about to be disabled
  100. * ABORT_DISABLE Regulator disable failed for some reason
  101. *
  102. * NOTE: These events can be OR'ed together when passed into handler.
  103. */
  104. #define REGULATOR_EVENT_UNDER_VOLTAGE 0x01
  105. #define REGULATOR_EVENT_OVER_CURRENT 0x02
  106. #define REGULATOR_EVENT_REGULATION_OUT 0x04
  107. #define REGULATOR_EVENT_FAIL 0x08
  108. #define REGULATOR_EVENT_OVER_TEMP 0x10
  109. #define REGULATOR_EVENT_FORCE_DISABLE 0x20
  110. #define REGULATOR_EVENT_VOLTAGE_CHANGE 0x40
  111. #define REGULATOR_EVENT_DISABLE 0x80
  112. #define REGULATOR_EVENT_PRE_VOLTAGE_CHANGE 0x100
  113. #define REGULATOR_EVENT_ABORT_VOLTAGE_CHANGE 0x200
  114. #define REGULATOR_EVENT_PRE_DISABLE 0x400
  115. #define REGULATOR_EVENT_ABORT_DISABLE 0x800
  116. #define REGULATOR_EVENT_ENABLE 0x1000
  117. /*
  118. * Regulator errors that can be queried using regulator_get_error_flags
  119. *
  120. * UNDER_VOLTAGE Regulator output is under voltage.
  121. * OVER_CURRENT Regulator output current is too high.
  122. * REGULATION_OUT Regulator output is out of regulation.
  123. * FAIL Regulator output has failed.
  124. * OVER_TEMP Regulator over temp.
  125. *
  126. * NOTE: These errors can be OR'ed together.
  127. */
  128. #define REGULATOR_ERROR_UNDER_VOLTAGE BIT(1)
  129. #define REGULATOR_ERROR_OVER_CURRENT BIT(2)
  130. #define REGULATOR_ERROR_REGULATION_OUT BIT(3)
  131. #define REGULATOR_ERROR_FAIL BIT(4)
  132. #define REGULATOR_ERROR_OVER_TEMP BIT(5)
  133. /**
  134. * struct pre_voltage_change_data - Data sent with PRE_VOLTAGE_CHANGE event
  135. *
  136. * @old_uV: Current voltage before change.
  137. * @min_uV: Min voltage we'll change to.
  138. * @max_uV: Max voltage we'll change to.
  139. */
  140. struct pre_voltage_change_data {
  141. unsigned long old_uV;
  142. unsigned long min_uV;
  143. unsigned long max_uV;
  144. };
  145. struct regulator;
  146. /**
  147. * struct regulator_bulk_data - Data used for bulk regulator operations.
  148. *
  149. * @supply: The name of the supply. Initialised by the user before
  150. * using the bulk regulator APIs.
  151. * @consumer: The regulator consumer for the supply. This will be managed
  152. * by the bulk API.
  153. *
  154. * The regulator APIs provide a series of regulator_bulk_() API calls as
  155. * a convenience to consumers which require multiple supplies. This
  156. * structure is used to manage data for these calls.
  157. */
  158. struct regulator_bulk_data {
  159. const char *supply;
  160. struct regulator *consumer;
  161. /* private: Internal use */
  162. int ret;
  163. };
  164. #if defined(CONFIG_REGULATOR)
  165. /* regulator get and put */
  166. struct regulator *__must_check regulator_get(struct device *dev,
  167. const char *id);
  168. struct regulator *__must_check devm_regulator_get(struct device *dev,
  169. const char *id);
  170. struct regulator *__must_check regulator_get_exclusive(struct device *dev,
  171. const char *id);
  172. struct regulator *__must_check devm_regulator_get_exclusive(struct device *dev,
  173. const char *id);
  174. struct regulator *__must_check regulator_get_optional(struct device *dev,
  175. const char *id);
  176. struct regulator *__must_check devm_regulator_get_optional(struct device *dev,
  177. const char *id);
  178. void regulator_put(struct regulator *regulator);
  179. void devm_regulator_put(struct regulator *regulator);
  180. int regulator_register_supply_alias(struct device *dev, const char *id,
  181. struct device *alias_dev,
  182. const char *alias_id);
  183. void regulator_unregister_supply_alias(struct device *dev, const char *id);
  184. int regulator_bulk_register_supply_alias(struct device *dev,
  185. const char *const *id,
  186. struct device *alias_dev,
  187. const char *const *alias_id,
  188. int num_id);
  189. void regulator_bulk_unregister_supply_alias(struct device *dev,
  190. const char * const *id, int num_id);
  191. int devm_regulator_register_supply_alias(struct device *dev, const char *id,
  192. struct device *alias_dev,
  193. const char *alias_id);
  194. void devm_regulator_unregister_supply_alias(struct device *dev,
  195. const char *id);
  196. int devm_regulator_bulk_register_supply_alias(struct device *dev,
  197. const char *const *id,
  198. struct device *alias_dev,
  199. const char *const *alias_id,
  200. int num_id);
  201. void devm_regulator_bulk_unregister_supply_alias(struct device *dev,
  202. const char *const *id,
  203. int num_id);
  204. /* regulator output control and status */
  205. int __must_check regulator_enable(struct regulator *regulator);
  206. int regulator_disable(struct regulator *regulator);
  207. int regulator_force_disable(struct regulator *regulator);
  208. int regulator_is_enabled(struct regulator *regulator);
  209. int regulator_disable_deferred(struct regulator *regulator, int ms);
  210. int __must_check regulator_bulk_get(struct device *dev, int num_consumers,
  211. struct regulator_bulk_data *consumers);
  212. int __must_check devm_regulator_bulk_get(struct device *dev, int num_consumers,
  213. struct regulator_bulk_data *consumers);
  214. int __must_check regulator_bulk_enable(int num_consumers,
  215. struct regulator_bulk_data *consumers);
  216. int regulator_bulk_disable(int num_consumers,
  217. struct regulator_bulk_data *consumers);
  218. int regulator_bulk_force_disable(int num_consumers,
  219. struct regulator_bulk_data *consumers);
  220. void regulator_bulk_free(int num_consumers,
  221. struct regulator_bulk_data *consumers);
  222. int regulator_count_voltages(struct regulator *regulator);
  223. int regulator_list_voltage(struct regulator *regulator, unsigned selector);
  224. int regulator_is_supported_voltage(struct regulator *regulator,
  225. int min_uV, int max_uV);
  226. unsigned int regulator_get_linear_step(struct regulator *regulator);
  227. int regulator_set_voltage(struct regulator *regulator, int min_uV, int max_uV);
  228. int regulator_set_voltage_time(struct regulator *regulator,
  229. int old_uV, int new_uV);
  230. int regulator_get_voltage(struct regulator *regulator);
  231. int regulator_sync_voltage(struct regulator *regulator);
  232. int regulator_set_current_limit(struct regulator *regulator,
  233. int min_uA, int max_uA);
  234. int regulator_get_current_limit(struct regulator *regulator);
  235. int regulator_set_mode(struct regulator *regulator, unsigned int mode);
  236. unsigned int regulator_get_mode(struct regulator *regulator);
  237. int regulator_get_error_flags(struct regulator *regulator,
  238. unsigned int *flags);
  239. int regulator_set_load(struct regulator *regulator, int load_uA);
  240. int regulator_allow_bypass(struct regulator *regulator, bool allow);
  241. struct regmap *regulator_get_regmap(struct regulator *regulator);
  242. int regulator_get_hardware_vsel_register(struct regulator *regulator,
  243. unsigned *vsel_reg,
  244. unsigned *vsel_mask);
  245. int regulator_list_hardware_vsel(struct regulator *regulator,
  246. unsigned selector);
  247. /* regulator notifier block */
  248. int regulator_register_notifier(struct regulator *regulator,
  249. struct notifier_block *nb);
  250. int devm_regulator_register_notifier(struct regulator *regulator,
  251. struct notifier_block *nb);
  252. int regulator_unregister_notifier(struct regulator *regulator,
  253. struct notifier_block *nb);
  254. void devm_regulator_unregister_notifier(struct regulator *regulator,
  255. struct notifier_block *nb);
  256. /* driver data - core doesn't touch */
  257. void *regulator_get_drvdata(struct regulator *regulator);
  258. void regulator_set_drvdata(struct regulator *regulator, void *data);
  259. #else
  260. /*
  261. * Make sure client drivers will still build on systems with no software
  262. * controllable voltage or current regulators.
  263. */
  264. static inline struct regulator *__must_check regulator_get(struct device *dev,
  265. const char *id)
  266. {
  267. /* Nothing except the stubbed out regulator API should be
  268. * looking at the value except to check if it is an error
  269. * value. Drivers are free to handle NULL specifically by
  270. * skipping all regulator API calls, but they don't have to.
  271. * Drivers which don't, should make sure they properly handle
  272. * corner cases of the API, such as regulator_get_voltage()
  273. * returning 0.
  274. */
  275. return NULL;
  276. }
  277. static inline struct regulator *__must_check
  278. devm_regulator_get(struct device *dev, const char *id)
  279. {
  280. return NULL;
  281. }
  282. static inline struct regulator *__must_check
  283. regulator_get_exclusive(struct device *dev, const char *id)
  284. {
  285. return ERR_PTR(-ENODEV);
  286. }
  287. static inline struct regulator *__must_check
  288. regulator_get_optional(struct device *dev, const char *id)
  289. {
  290. return ERR_PTR(-ENODEV);
  291. }
  292. static inline struct regulator *__must_check
  293. devm_regulator_get_optional(struct device *dev, const char *id)
  294. {
  295. return ERR_PTR(-ENODEV);
  296. }
  297. static inline void regulator_put(struct regulator *regulator)
  298. {
  299. }
  300. static inline void devm_regulator_put(struct regulator *regulator)
  301. {
  302. }
  303. static inline int regulator_register_supply_alias(struct device *dev,
  304. const char *id,
  305. struct device *alias_dev,
  306. const char *alias_id)
  307. {
  308. return 0;
  309. }
  310. static inline void regulator_unregister_supply_alias(struct device *dev,
  311. const char *id)
  312. {
  313. }
  314. static inline int regulator_bulk_register_supply_alias(struct device *dev,
  315. const char *const *id,
  316. struct device *alias_dev,
  317. const char * const *alias_id,
  318. int num_id)
  319. {
  320. return 0;
  321. }
  322. static inline void regulator_bulk_unregister_supply_alias(struct device *dev,
  323. const char * const *id,
  324. int num_id)
  325. {
  326. }
  327. static inline int devm_regulator_register_supply_alias(struct device *dev,
  328. const char *id,
  329. struct device *alias_dev,
  330. const char *alias_id)
  331. {
  332. return 0;
  333. }
  334. static inline void devm_regulator_unregister_supply_alias(struct device *dev,
  335. const char *id)
  336. {
  337. }
  338. static inline int devm_regulator_bulk_register_supply_alias(struct device *dev,
  339. const char *const *id,
  340. struct device *alias_dev,
  341. const char *const *alias_id,
  342. int num_id)
  343. {
  344. return 0;
  345. }
  346. static inline void devm_regulator_bulk_unregister_supply_alias(
  347. struct device *dev, const char *const *id, int num_id)
  348. {
  349. }
  350. static inline int regulator_enable(struct regulator *regulator)
  351. {
  352. return 0;
  353. }
  354. static inline int regulator_disable(struct regulator *regulator)
  355. {
  356. return 0;
  357. }
  358. static inline int regulator_force_disable(struct regulator *regulator)
  359. {
  360. return 0;
  361. }
  362. static inline int regulator_disable_deferred(struct regulator *regulator,
  363. int ms)
  364. {
  365. return 0;
  366. }
  367. static inline int regulator_is_enabled(struct regulator *regulator)
  368. {
  369. return 1;
  370. }
  371. static inline int regulator_bulk_get(struct device *dev,
  372. int num_consumers,
  373. struct regulator_bulk_data *consumers)
  374. {
  375. return 0;
  376. }
  377. static inline int devm_regulator_bulk_get(struct device *dev, int num_consumers,
  378. struct regulator_bulk_data *consumers)
  379. {
  380. return 0;
  381. }
  382. static inline int regulator_bulk_enable(int num_consumers,
  383. struct regulator_bulk_data *consumers)
  384. {
  385. return 0;
  386. }
  387. static inline int regulator_bulk_disable(int num_consumers,
  388. struct regulator_bulk_data *consumers)
  389. {
  390. return 0;
  391. }
  392. static inline int regulator_bulk_force_disable(int num_consumers,
  393. struct regulator_bulk_data *consumers)
  394. {
  395. return 0;
  396. }
  397. static inline void regulator_bulk_free(int num_consumers,
  398. struct regulator_bulk_data *consumers)
  399. {
  400. }
  401. static inline int regulator_set_voltage(struct regulator *regulator,
  402. int min_uV, int max_uV)
  403. {
  404. return 0;
  405. }
  406. static inline int regulator_set_voltage_time(struct regulator *regulator,
  407. int old_uV, int new_uV)
  408. {
  409. return 0;
  410. }
  411. static inline int regulator_get_voltage(struct regulator *regulator)
  412. {
  413. return -EINVAL;
  414. }
  415. static inline int regulator_is_supported_voltage(struct regulator *regulator,
  416. int min_uV, int max_uV)
  417. {
  418. return 0;
  419. }
  420. static inline int regulator_set_current_limit(struct regulator *regulator,
  421. int min_uA, int max_uA)
  422. {
  423. return 0;
  424. }
  425. static inline int regulator_get_current_limit(struct regulator *regulator)
  426. {
  427. return 0;
  428. }
  429. static inline int regulator_set_mode(struct regulator *regulator,
  430. unsigned int mode)
  431. {
  432. return 0;
  433. }
  434. static inline unsigned int regulator_get_mode(struct regulator *regulator)
  435. {
  436. return REGULATOR_MODE_NORMAL;
  437. }
  438. static inline int regulator_get_error_flags(struct regulator *regulator,
  439. unsigned int *flags)
  440. {
  441. return -EINVAL;
  442. }
  443. static inline int regulator_set_load(struct regulator *regulator, int load_uA)
  444. {
  445. return REGULATOR_MODE_NORMAL;
  446. }
  447. static inline int regulator_allow_bypass(struct regulator *regulator,
  448. bool allow)
  449. {
  450. return 0;
  451. }
  452. static inline struct regmap *regulator_get_regmap(struct regulator *regulator)
  453. {
  454. return ERR_PTR(-EOPNOTSUPP);
  455. }
  456. static inline int regulator_get_hardware_vsel_register(struct regulator *regulator,
  457. unsigned *vsel_reg,
  458. unsigned *vsel_mask)
  459. {
  460. return -EOPNOTSUPP;
  461. }
  462. static inline int regulator_list_hardware_vsel(struct regulator *regulator,
  463. unsigned selector)
  464. {
  465. return -EOPNOTSUPP;
  466. }
  467. static inline int regulator_register_notifier(struct regulator *regulator,
  468. struct notifier_block *nb)
  469. {
  470. return 0;
  471. }
  472. static inline int devm_regulator_register_notifier(struct regulator *regulator,
  473. struct notifier_block *nb)
  474. {
  475. return 0;
  476. }
  477. static inline int regulator_unregister_notifier(struct regulator *regulator,
  478. struct notifier_block *nb)
  479. {
  480. return 0;
  481. }
  482. static inline int devm_regulator_unregister_notifier(struct regulator *regulator,
  483. struct notifier_block *nb)
  484. {
  485. return 0;
  486. }
  487. static inline void *regulator_get_drvdata(struct regulator *regulator)
  488. {
  489. return NULL;
  490. }
  491. static inline void regulator_set_drvdata(struct regulator *regulator,
  492. void *data)
  493. {
  494. }
  495. static inline int regulator_count_voltages(struct regulator *regulator)
  496. {
  497. return 0;
  498. }
  499. static inline int regulator_list_voltage(struct regulator *regulator, unsigned selector)
  500. {
  501. return -EINVAL;
  502. }
  503. #endif
  504. static inline int regulator_set_voltage_triplet(struct regulator *regulator,
  505. int min_uV, int target_uV,
  506. int max_uV)
  507. {
  508. if (regulator_set_voltage(regulator, target_uV, max_uV) == 0)
  509. return 0;
  510. return regulator_set_voltage(regulator, min_uV, max_uV);
  511. }
  512. static inline int regulator_set_voltage_tol(struct regulator *regulator,
  513. int new_uV, int tol_uV)
  514. {
  515. if (regulator_set_voltage(regulator, new_uV, new_uV + tol_uV) == 0)
  516. return 0;
  517. else
  518. return regulator_set_voltage(regulator,
  519. new_uV - tol_uV, new_uV + tol_uV);
  520. }
  521. static inline int regulator_is_supported_voltage_tol(struct regulator *regulator,
  522. int target_uV, int tol_uV)
  523. {
  524. return regulator_is_supported_voltage(regulator,
  525. target_uV - tol_uV,
  526. target_uV + tol_uV);
  527. }
  528. #endif