consumer.h 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609
  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_FAST 0x1
  79. #define REGULATOR_MODE_NORMAL 0x2
  80. #define REGULATOR_MODE_IDLE 0x4
  81. #define REGULATOR_MODE_STANDBY 0x8
  82. /*
  83. * Regulator notifier events.
  84. *
  85. * UNDER_VOLTAGE Regulator output is under voltage.
  86. * OVER_CURRENT Regulator output current is too high.
  87. * REGULATION_OUT Regulator output is out of regulation.
  88. * FAIL Regulator output has failed.
  89. * OVER_TEMP Regulator over temp.
  90. * FORCE_DISABLE Regulator forcibly shut down by software.
  91. * VOLTAGE_CHANGE Regulator voltage changed.
  92. * Data passed is old voltage cast to (void *).
  93. * DISABLE Regulator was disabled.
  94. * PRE_VOLTAGE_CHANGE Regulator is about to have voltage changed.
  95. * Data passed is "struct pre_voltage_change_data"
  96. * ABORT_VOLTAGE_CHANGE Regulator voltage change failed for some reason.
  97. * Data passed is old voltage cast to (void *).
  98. * PRE_DISABLE Regulator is about to be disabled
  99. * ABORT_DISABLE Regulator disable failed for some reason
  100. *
  101. * NOTE: These events can be OR'ed together when passed into handler.
  102. */
  103. #define REGULATOR_EVENT_UNDER_VOLTAGE 0x01
  104. #define REGULATOR_EVENT_OVER_CURRENT 0x02
  105. #define REGULATOR_EVENT_REGULATION_OUT 0x04
  106. #define REGULATOR_EVENT_FAIL 0x08
  107. #define REGULATOR_EVENT_OVER_TEMP 0x10
  108. #define REGULATOR_EVENT_FORCE_DISABLE 0x20
  109. #define REGULATOR_EVENT_VOLTAGE_CHANGE 0x40
  110. #define REGULATOR_EVENT_DISABLE 0x80
  111. #define REGULATOR_EVENT_PRE_VOLTAGE_CHANGE 0x100
  112. #define REGULATOR_EVENT_ABORT_VOLTAGE_CHANGE 0x200
  113. #define REGULATOR_EVENT_PRE_DISABLE 0x400
  114. #define REGULATOR_EVENT_ABORT_DISABLE 0x800
  115. /*
  116. * Regulator errors that can be queried using regulator_get_error_flags
  117. *
  118. * UNDER_VOLTAGE Regulator output is under voltage.
  119. * OVER_CURRENT Regulator output current is too high.
  120. * REGULATION_OUT Regulator output is out of regulation.
  121. * FAIL Regulator output has failed.
  122. * OVER_TEMP Regulator over temp.
  123. *
  124. * NOTE: These errors can be OR'ed together.
  125. */
  126. #define REGULATOR_ERROR_UNDER_VOLTAGE BIT(1)
  127. #define REGULATOR_ERROR_OVER_CURRENT BIT(2)
  128. #define REGULATOR_ERROR_REGULATION_OUT BIT(3)
  129. #define REGULATOR_ERROR_FAIL BIT(4)
  130. #define REGULATOR_ERROR_OVER_TEMP BIT(5)
  131. /**
  132. * struct pre_voltage_change_data - Data sent with PRE_VOLTAGE_CHANGE event
  133. *
  134. * @old_uV: Current voltage before change.
  135. * @min_uV: Min voltage we'll change to.
  136. * @max_uV: Max voltage we'll change to.
  137. */
  138. struct pre_voltage_change_data {
  139. unsigned long old_uV;
  140. unsigned long min_uV;
  141. unsigned long max_uV;
  142. };
  143. struct regulator;
  144. /**
  145. * struct regulator_bulk_data - Data used for bulk regulator operations.
  146. *
  147. * @supply: The name of the supply. Initialised by the user before
  148. * using the bulk regulator APIs.
  149. * @consumer: The regulator consumer for the supply. This will be managed
  150. * by the bulk API.
  151. *
  152. * The regulator APIs provide a series of regulator_bulk_() API calls as
  153. * a convenience to consumers which require multiple supplies. This
  154. * structure is used to manage data for these calls.
  155. */
  156. struct regulator_bulk_data {
  157. const char *supply;
  158. struct regulator *consumer;
  159. /* private: Internal use */
  160. int ret;
  161. };
  162. #if defined(CONFIG_REGULATOR)
  163. /* regulator get and put */
  164. struct regulator *__must_check regulator_get(struct device *dev,
  165. const char *id);
  166. struct regulator *__must_check devm_regulator_get(struct device *dev,
  167. const char *id);
  168. struct regulator *__must_check regulator_get_exclusive(struct device *dev,
  169. const char *id);
  170. struct regulator *__must_check devm_regulator_get_exclusive(struct device *dev,
  171. const char *id);
  172. struct regulator *__must_check regulator_get_optional(struct device *dev,
  173. const char *id);
  174. struct regulator *__must_check devm_regulator_get_optional(struct device *dev,
  175. const char *id);
  176. void regulator_put(struct regulator *regulator);
  177. void devm_regulator_put(struct regulator *regulator);
  178. int regulator_register_supply_alias(struct device *dev, const char *id,
  179. struct device *alias_dev,
  180. const char *alias_id);
  181. void regulator_unregister_supply_alias(struct device *dev, const char *id);
  182. int regulator_bulk_register_supply_alias(struct device *dev,
  183. const char *const *id,
  184. struct device *alias_dev,
  185. const char *const *alias_id,
  186. int num_id);
  187. void regulator_bulk_unregister_supply_alias(struct device *dev,
  188. const char * const *id, int num_id);
  189. int devm_regulator_register_supply_alias(struct device *dev, const char *id,
  190. struct device *alias_dev,
  191. const char *alias_id);
  192. void devm_regulator_unregister_supply_alias(struct device *dev,
  193. const char *id);
  194. int devm_regulator_bulk_register_supply_alias(struct device *dev,
  195. const char *const *id,
  196. struct device *alias_dev,
  197. const char *const *alias_id,
  198. int num_id);
  199. void devm_regulator_bulk_unregister_supply_alias(struct device *dev,
  200. const char *const *id,
  201. int num_id);
  202. /* regulator output control and status */
  203. int __must_check regulator_enable(struct regulator *regulator);
  204. int regulator_disable(struct regulator *regulator);
  205. int regulator_force_disable(struct regulator *regulator);
  206. int regulator_is_enabled(struct regulator *regulator);
  207. int regulator_disable_deferred(struct regulator *regulator, int ms);
  208. int __must_check regulator_bulk_get(struct device *dev, int num_consumers,
  209. struct regulator_bulk_data *consumers);
  210. int __must_check devm_regulator_bulk_get(struct device *dev, int num_consumers,
  211. struct regulator_bulk_data *consumers);
  212. int __must_check regulator_bulk_enable(int num_consumers,
  213. struct regulator_bulk_data *consumers);
  214. int regulator_bulk_disable(int num_consumers,
  215. struct regulator_bulk_data *consumers);
  216. int regulator_bulk_force_disable(int num_consumers,
  217. struct regulator_bulk_data *consumers);
  218. void regulator_bulk_free(int num_consumers,
  219. struct regulator_bulk_data *consumers);
  220. int regulator_count_voltages(struct regulator *regulator);
  221. int regulator_list_voltage(struct regulator *regulator, unsigned selector);
  222. int regulator_is_supported_voltage(struct regulator *regulator,
  223. int min_uV, int max_uV);
  224. unsigned int regulator_get_linear_step(struct regulator *regulator);
  225. int regulator_set_voltage(struct regulator *regulator, int min_uV, int max_uV);
  226. int regulator_set_voltage_time(struct regulator *regulator,
  227. int old_uV, int new_uV);
  228. int regulator_get_voltage(struct regulator *regulator);
  229. int regulator_sync_voltage(struct regulator *regulator);
  230. int regulator_set_current_limit(struct regulator *regulator,
  231. int min_uA, int max_uA);
  232. int regulator_get_current_limit(struct regulator *regulator);
  233. int regulator_set_mode(struct regulator *regulator, unsigned int mode);
  234. unsigned int regulator_get_mode(struct regulator *regulator);
  235. int regulator_get_error_flags(struct regulator *regulator,
  236. unsigned int *flags);
  237. int regulator_set_load(struct regulator *regulator, int load_uA);
  238. int regulator_allow_bypass(struct regulator *regulator, bool allow);
  239. struct regmap *regulator_get_regmap(struct regulator *regulator);
  240. int regulator_get_hardware_vsel_register(struct regulator *regulator,
  241. unsigned *vsel_reg,
  242. unsigned *vsel_mask);
  243. int regulator_list_hardware_vsel(struct regulator *regulator,
  244. unsigned selector);
  245. /* regulator notifier block */
  246. int regulator_register_notifier(struct regulator *regulator,
  247. struct notifier_block *nb);
  248. int devm_regulator_register_notifier(struct regulator *regulator,
  249. struct notifier_block *nb);
  250. int regulator_unregister_notifier(struct regulator *regulator,
  251. struct notifier_block *nb);
  252. void devm_regulator_unregister_notifier(struct regulator *regulator,
  253. struct notifier_block *nb);
  254. /* driver data - core doesn't touch */
  255. void *regulator_get_drvdata(struct regulator *regulator);
  256. void regulator_set_drvdata(struct regulator *regulator, void *data);
  257. #else
  258. /*
  259. * Make sure client drivers will still build on systems with no software
  260. * controllable voltage or current regulators.
  261. */
  262. static inline struct regulator *__must_check regulator_get(struct device *dev,
  263. const char *id)
  264. {
  265. /* Nothing except the stubbed out regulator API should be
  266. * looking at the value except to check if it is an error
  267. * value. Drivers are free to handle NULL specifically by
  268. * skipping all regulator API calls, but they don't have to.
  269. * Drivers which don't, should make sure they properly handle
  270. * corner cases of the API, such as regulator_get_voltage()
  271. * returning 0.
  272. */
  273. return NULL;
  274. }
  275. static inline struct regulator *__must_check
  276. devm_regulator_get(struct device *dev, const char *id)
  277. {
  278. return NULL;
  279. }
  280. static inline struct regulator *__must_check
  281. regulator_get_exclusive(struct device *dev, const char *id)
  282. {
  283. return ERR_PTR(-ENODEV);
  284. }
  285. static inline struct regulator *__must_check
  286. regulator_get_optional(struct device *dev, const char *id)
  287. {
  288. return ERR_PTR(-ENODEV);
  289. }
  290. static inline struct regulator *__must_check
  291. devm_regulator_get_optional(struct device *dev, const char *id)
  292. {
  293. return ERR_PTR(-ENODEV);
  294. }
  295. static inline void regulator_put(struct regulator *regulator)
  296. {
  297. }
  298. static inline void devm_regulator_put(struct regulator *regulator)
  299. {
  300. }
  301. static inline int regulator_register_supply_alias(struct device *dev,
  302. const char *id,
  303. struct device *alias_dev,
  304. const char *alias_id)
  305. {
  306. return 0;
  307. }
  308. static inline void regulator_unregister_supply_alias(struct device *dev,
  309. const char *id)
  310. {
  311. }
  312. static inline int regulator_bulk_register_supply_alias(struct device *dev,
  313. const char *const *id,
  314. struct device *alias_dev,
  315. const char * const *alias_id,
  316. int num_id)
  317. {
  318. return 0;
  319. }
  320. static inline void regulator_bulk_unregister_supply_alias(struct device *dev,
  321. const char * const *id,
  322. int num_id)
  323. {
  324. }
  325. static inline int devm_regulator_register_supply_alias(struct device *dev,
  326. const char *id,
  327. struct device *alias_dev,
  328. const char *alias_id)
  329. {
  330. return 0;
  331. }
  332. static inline void devm_regulator_unregister_supply_alias(struct device *dev,
  333. const char *id)
  334. {
  335. }
  336. static inline int devm_regulator_bulk_register_supply_alias(struct device *dev,
  337. const char *const *id,
  338. struct device *alias_dev,
  339. const char *const *alias_id,
  340. int num_id)
  341. {
  342. return 0;
  343. }
  344. static inline void devm_regulator_bulk_unregister_supply_alias(
  345. struct device *dev, const char *const *id, int num_id)
  346. {
  347. }
  348. static inline int regulator_enable(struct regulator *regulator)
  349. {
  350. return 0;
  351. }
  352. static inline int regulator_disable(struct regulator *regulator)
  353. {
  354. return 0;
  355. }
  356. static inline int regulator_force_disable(struct regulator *regulator)
  357. {
  358. return 0;
  359. }
  360. static inline int regulator_disable_deferred(struct regulator *regulator,
  361. int ms)
  362. {
  363. return 0;
  364. }
  365. static inline int regulator_is_enabled(struct regulator *regulator)
  366. {
  367. return 1;
  368. }
  369. static inline int regulator_bulk_get(struct device *dev,
  370. int num_consumers,
  371. struct regulator_bulk_data *consumers)
  372. {
  373. return 0;
  374. }
  375. static inline int devm_regulator_bulk_get(struct device *dev, int num_consumers,
  376. struct regulator_bulk_data *consumers)
  377. {
  378. return 0;
  379. }
  380. static inline int regulator_bulk_enable(int num_consumers,
  381. struct regulator_bulk_data *consumers)
  382. {
  383. return 0;
  384. }
  385. static inline int regulator_bulk_disable(int num_consumers,
  386. struct regulator_bulk_data *consumers)
  387. {
  388. return 0;
  389. }
  390. static inline int regulator_bulk_force_disable(int num_consumers,
  391. struct regulator_bulk_data *consumers)
  392. {
  393. return 0;
  394. }
  395. static inline void regulator_bulk_free(int num_consumers,
  396. struct regulator_bulk_data *consumers)
  397. {
  398. }
  399. static inline int regulator_set_voltage(struct regulator *regulator,
  400. int min_uV, int max_uV)
  401. {
  402. return 0;
  403. }
  404. static inline int regulator_set_voltage_time(struct regulator *regulator,
  405. int old_uV, int new_uV)
  406. {
  407. return 0;
  408. }
  409. static inline int regulator_get_voltage(struct regulator *regulator)
  410. {
  411. return -EINVAL;
  412. }
  413. static inline int regulator_is_supported_voltage(struct regulator *regulator,
  414. int min_uV, int max_uV)
  415. {
  416. return 0;
  417. }
  418. static inline int regulator_set_current_limit(struct regulator *regulator,
  419. int min_uA, int max_uA)
  420. {
  421. return 0;
  422. }
  423. static inline int regulator_get_current_limit(struct regulator *regulator)
  424. {
  425. return 0;
  426. }
  427. static inline int regulator_set_mode(struct regulator *regulator,
  428. unsigned int mode)
  429. {
  430. return 0;
  431. }
  432. static inline unsigned int regulator_get_mode(struct regulator *regulator)
  433. {
  434. return REGULATOR_MODE_NORMAL;
  435. }
  436. static inline int regulator_get_error_flags(struct regulator *regulator)
  437. {
  438. return -EINVAL;
  439. }
  440. static inline int regulator_set_load(struct regulator *regulator, int load_uA)
  441. {
  442. return REGULATOR_MODE_NORMAL;
  443. }
  444. static inline int regulator_allow_bypass(struct regulator *regulator,
  445. bool allow)
  446. {
  447. return 0;
  448. }
  449. static inline struct regmap *regulator_get_regmap(struct regulator *regulator)
  450. {
  451. return ERR_PTR(-EOPNOTSUPP);
  452. }
  453. static inline int regulator_get_hardware_vsel_register(struct regulator *regulator,
  454. unsigned *vsel_reg,
  455. unsigned *vsel_mask)
  456. {
  457. return -EOPNOTSUPP;
  458. }
  459. static inline int regulator_list_hardware_vsel(struct regulator *regulator,
  460. unsigned selector)
  461. {
  462. return -EOPNOTSUPP;
  463. }
  464. static inline int regulator_register_notifier(struct regulator *regulator,
  465. struct notifier_block *nb)
  466. {
  467. return 0;
  468. }
  469. static inline int devm_regulator_register_notifier(struct regulator *regulator,
  470. struct notifier_block *nb)
  471. {
  472. return 0;
  473. }
  474. static inline int regulator_unregister_notifier(struct regulator *regulator,
  475. struct notifier_block *nb)
  476. {
  477. return 0;
  478. }
  479. static inline int devm_regulator_unregister_notifier(struct regulator *regulator,
  480. struct notifier_block *nb)
  481. {
  482. return 0;
  483. }
  484. static inline void *regulator_get_drvdata(struct regulator *regulator)
  485. {
  486. return NULL;
  487. }
  488. static inline void regulator_set_drvdata(struct regulator *regulator,
  489. void *data)
  490. {
  491. }
  492. static inline int regulator_count_voltages(struct regulator *regulator)
  493. {
  494. return 0;
  495. }
  496. static inline int regulator_list_voltage(struct regulator *regulator, unsigned selector)
  497. {
  498. return -EINVAL;
  499. }
  500. #endif
  501. static inline int regulator_set_voltage_triplet(struct regulator *regulator,
  502. int min_uV, int target_uV,
  503. int max_uV)
  504. {
  505. if (regulator_set_voltage(regulator, target_uV, max_uV) == 0)
  506. return 0;
  507. return regulator_set_voltage(regulator, min_uV, max_uV);
  508. }
  509. static inline int regulator_set_voltage_tol(struct regulator *regulator,
  510. int new_uV, int tol_uV)
  511. {
  512. if (regulator_set_voltage(regulator, new_uV, new_uV + tol_uV) == 0)
  513. return 0;
  514. else
  515. return regulator_set_voltage(regulator,
  516. new_uV - tol_uV, new_uV + tol_uV);
  517. }
  518. static inline int regulator_is_supported_voltage_tol(struct regulator *regulator,
  519. int target_uV, int tol_uV)
  520. {
  521. return regulator_is_supported_voltage(regulator,
  522. target_uV - tol_uV,
  523. target_uV + tol_uV);
  524. }
  525. #endif