consumer.h 16 KB

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