consumer.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558
  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. * struct pre_voltage_change_data - Data sent with PRE_VOLTAGE_CHANGE event
  117. *
  118. * @old_uV: Current voltage before change.
  119. * @min_uV: Min voltage we'll change to.
  120. * @max_uV: Max voltage we'll change to.
  121. */
  122. struct pre_voltage_change_data {
  123. unsigned long old_uV;
  124. unsigned long min_uV;
  125. unsigned long max_uV;
  126. };
  127. struct regulator;
  128. /**
  129. * struct regulator_bulk_data - Data used for bulk regulator operations.
  130. *
  131. * @supply: The name of the supply. Initialised by the user before
  132. * using the bulk regulator APIs.
  133. * @consumer: The regulator consumer for the supply. This will be managed
  134. * by the bulk API.
  135. *
  136. * The regulator APIs provide a series of regulator_bulk_() API calls as
  137. * a convenience to consumers which require multiple supplies. This
  138. * structure is used to manage data for these calls.
  139. */
  140. struct regulator_bulk_data {
  141. const char *supply;
  142. struct regulator *consumer;
  143. /* private: Internal use */
  144. int ret;
  145. };
  146. #if defined(CONFIG_REGULATOR)
  147. /* regulator get and put */
  148. struct regulator *__must_check regulator_get(struct device *dev,
  149. const char *id);
  150. struct regulator *__must_check devm_regulator_get(struct device *dev,
  151. const char *id);
  152. struct regulator *__must_check regulator_get_exclusive(struct device *dev,
  153. const char *id);
  154. struct regulator *__must_check devm_regulator_get_exclusive(struct device *dev,
  155. const char *id);
  156. struct regulator *__must_check regulator_get_optional(struct device *dev,
  157. const char *id);
  158. struct regulator *__must_check devm_regulator_get_optional(struct device *dev,
  159. const char *id);
  160. void regulator_put(struct regulator *regulator);
  161. void devm_regulator_put(struct regulator *regulator);
  162. int regulator_register_supply_alias(struct device *dev, const char *id,
  163. struct device *alias_dev,
  164. const char *alias_id);
  165. void regulator_unregister_supply_alias(struct device *dev, const char *id);
  166. int regulator_bulk_register_supply_alias(struct device *dev,
  167. const char *const *id,
  168. struct device *alias_dev,
  169. const char *const *alias_id,
  170. int num_id);
  171. void regulator_bulk_unregister_supply_alias(struct device *dev,
  172. const char * const *id, int num_id);
  173. int devm_regulator_register_supply_alias(struct device *dev, const char *id,
  174. struct device *alias_dev,
  175. const char *alias_id);
  176. void devm_regulator_unregister_supply_alias(struct device *dev,
  177. const char *id);
  178. int devm_regulator_bulk_register_supply_alias(struct device *dev,
  179. const char *const *id,
  180. struct device *alias_dev,
  181. const char *const *alias_id,
  182. int num_id);
  183. void devm_regulator_bulk_unregister_supply_alias(struct device *dev,
  184. const char *const *id,
  185. int num_id);
  186. /* regulator output control and status */
  187. int __must_check regulator_enable(struct regulator *regulator);
  188. int regulator_disable(struct regulator *regulator);
  189. int regulator_force_disable(struct regulator *regulator);
  190. int regulator_is_enabled(struct regulator *regulator);
  191. int regulator_disable_deferred(struct regulator *regulator, int ms);
  192. int __must_check regulator_bulk_get(struct device *dev, int num_consumers,
  193. struct regulator_bulk_data *consumers);
  194. int __must_check devm_regulator_bulk_get(struct device *dev, int num_consumers,
  195. struct regulator_bulk_data *consumers);
  196. int __must_check regulator_bulk_enable(int num_consumers,
  197. struct regulator_bulk_data *consumers);
  198. int regulator_bulk_disable(int num_consumers,
  199. struct regulator_bulk_data *consumers);
  200. int regulator_bulk_force_disable(int num_consumers,
  201. struct regulator_bulk_data *consumers);
  202. void regulator_bulk_free(int num_consumers,
  203. struct regulator_bulk_data *consumers);
  204. int regulator_can_change_voltage(struct regulator *regulator);
  205. int regulator_count_voltages(struct regulator *regulator);
  206. int regulator_list_voltage(struct regulator *regulator, unsigned selector);
  207. int regulator_is_supported_voltage(struct regulator *regulator,
  208. int min_uV, int max_uV);
  209. unsigned int regulator_get_linear_step(struct regulator *regulator);
  210. int regulator_set_voltage(struct regulator *regulator, int min_uV, int max_uV);
  211. int regulator_set_voltage_time(struct regulator *regulator,
  212. int old_uV, int new_uV);
  213. int regulator_get_voltage(struct regulator *regulator);
  214. int regulator_sync_voltage(struct regulator *regulator);
  215. int regulator_set_current_limit(struct regulator *regulator,
  216. int min_uA, int max_uA);
  217. int regulator_get_current_limit(struct regulator *regulator);
  218. int regulator_set_mode(struct regulator *regulator, unsigned int mode);
  219. unsigned int regulator_get_mode(struct regulator *regulator);
  220. int regulator_set_optimum_mode(struct regulator *regulator, int load_uA);
  221. int regulator_allow_bypass(struct regulator *regulator, bool allow);
  222. struct regmap *regulator_get_regmap(struct regulator *regulator);
  223. int regulator_get_hardware_vsel_register(struct regulator *regulator,
  224. unsigned *vsel_reg,
  225. unsigned *vsel_mask);
  226. int regulator_list_hardware_vsel(struct regulator *regulator,
  227. unsigned selector);
  228. /* regulator notifier block */
  229. int regulator_register_notifier(struct regulator *regulator,
  230. struct notifier_block *nb);
  231. int regulator_unregister_notifier(struct regulator *regulator,
  232. struct notifier_block *nb);
  233. /* driver data - core doesn't touch */
  234. void *regulator_get_drvdata(struct regulator *regulator);
  235. void regulator_set_drvdata(struct regulator *regulator, void *data);
  236. #else
  237. /*
  238. * Make sure client drivers will still build on systems with no software
  239. * controllable voltage or current regulators.
  240. */
  241. static inline struct regulator *__must_check regulator_get(struct device *dev,
  242. const char *id)
  243. {
  244. /* Nothing except the stubbed out regulator API should be
  245. * looking at the value except to check if it is an error
  246. * value. Drivers are free to handle NULL specifically by
  247. * skipping all regulator API calls, but they don't have to.
  248. * Drivers which don't, should make sure they properly handle
  249. * corner cases of the API, such as regulator_get_voltage()
  250. * returning 0.
  251. */
  252. return NULL;
  253. }
  254. static inline struct regulator *__must_check
  255. devm_regulator_get(struct device *dev, const char *id)
  256. {
  257. return NULL;
  258. }
  259. static inline struct regulator *__must_check
  260. regulator_get_exclusive(struct device *dev, const char *id)
  261. {
  262. return ERR_PTR(-ENODEV);
  263. }
  264. static inline struct regulator *__must_check
  265. regulator_get_optional(struct device *dev, const char *id)
  266. {
  267. return ERR_PTR(-ENODEV);
  268. }
  269. static inline struct regulator *__must_check
  270. devm_regulator_get_optional(struct device *dev, const char *id)
  271. {
  272. return ERR_PTR(-ENODEV);
  273. }
  274. static inline void regulator_put(struct regulator *regulator)
  275. {
  276. }
  277. static inline void devm_regulator_put(struct regulator *regulator)
  278. {
  279. }
  280. static inline int regulator_register_supply_alias(struct device *dev,
  281. const char *id,
  282. struct device *alias_dev,
  283. const char *alias_id)
  284. {
  285. return 0;
  286. }
  287. static inline void regulator_unregister_supply_alias(struct device *dev,
  288. const char *id)
  289. {
  290. }
  291. static inline int regulator_bulk_register_supply_alias(struct device *dev,
  292. const char *const *id,
  293. struct device *alias_dev,
  294. const char * const *alias_id,
  295. int num_id)
  296. {
  297. return 0;
  298. }
  299. static inline void regulator_bulk_unregister_supply_alias(struct device *dev,
  300. const char * const *id,
  301. int num_id)
  302. {
  303. }
  304. static inline int devm_regulator_register_supply_alias(struct device *dev,
  305. const char *id,
  306. struct device *alias_dev,
  307. const char *alias_id)
  308. {
  309. return 0;
  310. }
  311. static inline void devm_regulator_unregister_supply_alias(struct device *dev,
  312. const char *id)
  313. {
  314. }
  315. static inline int devm_regulator_bulk_register_supply_alias(struct device *dev,
  316. const char *const *id,
  317. struct device *alias_dev,
  318. const char *const *alias_id,
  319. int num_id)
  320. {
  321. return 0;
  322. }
  323. static inline void devm_regulator_bulk_unregister_supply_alias(
  324. struct device *dev, const char *const *id, int num_id)
  325. {
  326. }
  327. static inline int regulator_enable(struct regulator *regulator)
  328. {
  329. return 0;
  330. }
  331. static inline int regulator_disable(struct regulator *regulator)
  332. {
  333. return 0;
  334. }
  335. static inline int regulator_force_disable(struct regulator *regulator)
  336. {
  337. return 0;
  338. }
  339. static inline int regulator_disable_deferred(struct regulator *regulator,
  340. int ms)
  341. {
  342. return 0;
  343. }
  344. static inline int regulator_is_enabled(struct regulator *regulator)
  345. {
  346. return 1;
  347. }
  348. static inline int regulator_bulk_get(struct device *dev,
  349. int num_consumers,
  350. struct regulator_bulk_data *consumers)
  351. {
  352. return 0;
  353. }
  354. static inline int devm_regulator_bulk_get(struct device *dev, int num_consumers,
  355. struct regulator_bulk_data *consumers)
  356. {
  357. return 0;
  358. }
  359. static inline int regulator_bulk_enable(int num_consumers,
  360. struct regulator_bulk_data *consumers)
  361. {
  362. return 0;
  363. }
  364. static inline int regulator_bulk_disable(int num_consumers,
  365. struct regulator_bulk_data *consumers)
  366. {
  367. return 0;
  368. }
  369. static inline int regulator_bulk_force_disable(int num_consumers,
  370. struct regulator_bulk_data *consumers)
  371. {
  372. return 0;
  373. }
  374. static inline void regulator_bulk_free(int num_consumers,
  375. struct regulator_bulk_data *consumers)
  376. {
  377. }
  378. static inline int regulator_can_change_voltage(struct regulator *regulator)
  379. {
  380. return 0;
  381. }
  382. static inline int regulator_set_voltage(struct regulator *regulator,
  383. int min_uV, int max_uV)
  384. {
  385. return 0;
  386. }
  387. static inline int regulator_set_voltage_time(struct regulator *regulator,
  388. int old_uV, int new_uV)
  389. {
  390. return 0;
  391. }
  392. static inline int regulator_get_voltage(struct regulator *regulator)
  393. {
  394. return -EINVAL;
  395. }
  396. static inline int regulator_is_supported_voltage(struct regulator *regulator,
  397. int min_uV, int max_uV)
  398. {
  399. return 0;
  400. }
  401. static inline int regulator_set_current_limit(struct regulator *regulator,
  402. int min_uA, int max_uA)
  403. {
  404. return 0;
  405. }
  406. static inline int regulator_get_current_limit(struct regulator *regulator)
  407. {
  408. return 0;
  409. }
  410. static inline int regulator_set_mode(struct regulator *regulator,
  411. unsigned int mode)
  412. {
  413. return 0;
  414. }
  415. static inline unsigned int regulator_get_mode(struct regulator *regulator)
  416. {
  417. return REGULATOR_MODE_NORMAL;
  418. }
  419. static inline int regulator_set_optimum_mode(struct regulator *regulator,
  420. int load_uA)
  421. {
  422. return REGULATOR_MODE_NORMAL;
  423. }
  424. static inline int regulator_allow_bypass(struct regulator *regulator,
  425. bool allow)
  426. {
  427. return 0;
  428. }
  429. static inline struct regmap *regulator_get_regmap(struct regulator *regulator)
  430. {
  431. return ERR_PTR(-EOPNOTSUPP);
  432. }
  433. static inline int regulator_get_hardware_vsel_register(struct regulator *regulator,
  434. unsigned *vsel_reg,
  435. unsigned *vsel_mask)
  436. {
  437. return -EOPNOTSUPP;
  438. }
  439. static inline int regulator_list_hardware_vsel(struct regulator *regulator,
  440. unsigned selector)
  441. {
  442. return -EOPNOTSUPP;
  443. }
  444. static inline int regulator_register_notifier(struct regulator *regulator,
  445. struct notifier_block *nb)
  446. {
  447. return 0;
  448. }
  449. static inline int regulator_unregister_notifier(struct regulator *regulator,
  450. struct notifier_block *nb)
  451. {
  452. return 0;
  453. }
  454. static inline void *regulator_get_drvdata(struct regulator *regulator)
  455. {
  456. return NULL;
  457. }
  458. static inline void regulator_set_drvdata(struct regulator *regulator,
  459. void *data)
  460. {
  461. }
  462. static inline int regulator_count_voltages(struct regulator *regulator)
  463. {
  464. return 0;
  465. }
  466. #endif
  467. static inline int regulator_set_voltage_tol(struct regulator *regulator,
  468. int new_uV, int tol_uV)
  469. {
  470. if (regulator_set_voltage(regulator, new_uV, new_uV + tol_uV) == 0)
  471. return 0;
  472. else
  473. return regulator_set_voltage(regulator,
  474. new_uV - tol_uV, new_uV + tol_uV);
  475. }
  476. static inline int regulator_is_supported_voltage_tol(struct regulator *regulator,
  477. int target_uV, int tol_uV)
  478. {
  479. return regulator_is_supported_voltage(regulator,
  480. target_uV - tol_uV,
  481. target_uV + tol_uV);
  482. }
  483. #endif