driver.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415
  1. /*
  2. * driver.h -- SoC Regulator driver 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 Driver Interface.
  13. */
  14. #ifndef __LINUX_REGULATOR_DRIVER_H_
  15. #define __LINUX_REGULATOR_DRIVER_H_
  16. #include <linux/device.h>
  17. #include <linux/notifier.h>
  18. #include <linux/regulator/consumer.h>
  19. struct regmap;
  20. struct regulator_dev;
  21. struct regulator_init_data;
  22. struct regulator_enable_gpio;
  23. enum regulator_status {
  24. REGULATOR_STATUS_OFF,
  25. REGULATOR_STATUS_ON,
  26. REGULATOR_STATUS_ERROR,
  27. /* fast/normal/idle/standby are flavors of "on" */
  28. REGULATOR_STATUS_FAST,
  29. REGULATOR_STATUS_NORMAL,
  30. REGULATOR_STATUS_IDLE,
  31. REGULATOR_STATUS_STANDBY,
  32. /* The regulator is enabled but not regulating */
  33. REGULATOR_STATUS_BYPASS,
  34. /* in case that any other status doesn't apply */
  35. REGULATOR_STATUS_UNDEFINED,
  36. };
  37. /**
  38. * struct regulator_linear_range - specify linear voltage ranges
  39. *
  40. * Specify a range of voltages for regulator_map_linar_range() and
  41. * regulator_list_linear_range().
  42. *
  43. * @min_uV: Lowest voltage in range
  44. * @min_sel: Lowest selector for range
  45. * @max_sel: Highest selector for range
  46. * @uV_step: Step size
  47. */
  48. struct regulator_linear_range {
  49. unsigned int min_uV;
  50. unsigned int min_sel;
  51. unsigned int max_sel;
  52. unsigned int uV_step;
  53. };
  54. /* Initialize struct regulator_linear_range */
  55. #define REGULATOR_LINEAR_RANGE(_min_uV, _min_sel, _max_sel, _step_uV) \
  56. { \
  57. .min_uV = _min_uV, \
  58. .min_sel = _min_sel, \
  59. .max_sel = _max_sel, \
  60. .uV_step = _step_uV, \
  61. }
  62. /**
  63. * struct regulator_ops - regulator operations.
  64. *
  65. * @enable: Configure the regulator as enabled.
  66. * @disable: Configure the regulator as disabled.
  67. * @is_enabled: Return 1 if the regulator is enabled, 0 if not.
  68. * May also return negative errno.
  69. *
  70. * @set_voltage: Set the voltage for the regulator within the range specified.
  71. * The driver should select the voltage closest to min_uV.
  72. * @set_voltage_sel: Set the voltage for the regulator using the specified
  73. * selector.
  74. * @map_voltage: Convert a voltage into a selector
  75. * @get_voltage: Return the currently configured voltage for the regulator.
  76. * @get_voltage_sel: Return the currently configured voltage selector for the
  77. * regulator.
  78. * @list_voltage: Return one of the supported voltages, in microvolts; zero
  79. * if the selector indicates a voltage that is unusable on this system;
  80. * or negative errno. Selectors range from zero to one less than
  81. * regulator_desc.n_voltages. Voltages may be reported in any order.
  82. *
  83. * @set_current_limit: Configure a limit for a current-limited regulator.
  84. * The driver should select the current closest to max_uA.
  85. * @get_current_limit: Get the configured limit for a current-limited regulator.
  86. *
  87. * @set_mode: Set the configured operating mode for the regulator.
  88. * @get_mode: Get the configured operating mode for the regulator.
  89. * @get_status: Return actual (not as-configured) status of regulator, as a
  90. * REGULATOR_STATUS value (or negative errno)
  91. * @get_optimum_mode: Get the most efficient operating mode for the regulator
  92. * when running with the specified parameters.
  93. *
  94. * @set_bypass: Set the regulator in bypass mode.
  95. * @get_bypass: Get the regulator bypass mode state.
  96. *
  97. * @enable_time: Time taken for the regulator voltage output voltage to
  98. * stabilise after being enabled, in microseconds.
  99. * @set_ramp_delay: Set the ramp delay for the regulator. The driver should
  100. * select ramp delay equal to or less than(closest) ramp_delay.
  101. * @set_voltage_time_sel: Time taken for the regulator voltage output voltage
  102. * to stabilise after being set to a new value, in microseconds.
  103. * The function provides the from and to voltage selector, the
  104. * function should return the worst case.
  105. *
  106. * @set_suspend_voltage: Set the voltage for the regulator when the system
  107. * is suspended.
  108. * @set_suspend_enable: Mark the regulator as enabled when the system is
  109. * suspended.
  110. * @set_suspend_disable: Mark the regulator as disabled when the system is
  111. * suspended.
  112. * @set_suspend_mode: Set the operating mode for the regulator when the
  113. * system is suspended.
  114. *
  115. * This struct describes regulator operations which can be implemented by
  116. * regulator chip drivers.
  117. */
  118. struct regulator_ops {
  119. /* enumerate supported voltages */
  120. int (*list_voltage) (struct regulator_dev *, unsigned selector);
  121. /* get/set regulator voltage */
  122. int (*set_voltage) (struct regulator_dev *, int min_uV, int max_uV,
  123. unsigned *selector);
  124. int (*map_voltage)(struct regulator_dev *, int min_uV, int max_uV);
  125. int (*set_voltage_sel) (struct regulator_dev *, unsigned selector);
  126. int (*get_voltage) (struct regulator_dev *);
  127. int (*get_voltage_sel) (struct regulator_dev *);
  128. /* get/set regulator current */
  129. int (*set_current_limit) (struct regulator_dev *,
  130. int min_uA, int max_uA);
  131. int (*get_current_limit) (struct regulator_dev *);
  132. /* enable/disable regulator */
  133. int (*enable) (struct regulator_dev *);
  134. int (*disable) (struct regulator_dev *);
  135. int (*is_enabled) (struct regulator_dev *);
  136. /* get/set regulator operating mode (defined in consumer.h) */
  137. int (*set_mode) (struct regulator_dev *, unsigned int mode);
  138. unsigned int (*get_mode) (struct regulator_dev *);
  139. /* Time taken to enable or set voltage on the regulator */
  140. int (*enable_time) (struct regulator_dev *);
  141. int (*set_ramp_delay) (struct regulator_dev *, int ramp_delay);
  142. int (*set_voltage_time_sel) (struct regulator_dev *,
  143. unsigned int old_selector,
  144. unsigned int new_selector);
  145. /* report regulator status ... most other accessors report
  146. * control inputs, this reports results of combining inputs
  147. * from Linux (and other sources) with the actual load.
  148. * returns REGULATOR_STATUS_* or negative errno.
  149. */
  150. int (*get_status)(struct regulator_dev *);
  151. /* get most efficient regulator operating mode for load */
  152. unsigned int (*get_optimum_mode) (struct regulator_dev *, int input_uV,
  153. int output_uV, int load_uA);
  154. /* control and report on bypass mode */
  155. int (*set_bypass)(struct regulator_dev *dev, bool enable);
  156. int (*get_bypass)(struct regulator_dev *dev, bool *enable);
  157. /* the operations below are for configuration of regulator state when
  158. * its parent PMIC enters a global STANDBY/HIBERNATE state */
  159. /* set regulator suspend voltage */
  160. int (*set_suspend_voltage) (struct regulator_dev *, int uV);
  161. /* enable/disable regulator in suspend state */
  162. int (*set_suspend_enable) (struct regulator_dev *);
  163. int (*set_suspend_disable) (struct regulator_dev *);
  164. /* set regulator suspend operating mode (defined in consumer.h) */
  165. int (*set_suspend_mode) (struct regulator_dev *, unsigned int mode);
  166. };
  167. /*
  168. * Regulators can either control voltage or current.
  169. */
  170. enum regulator_type {
  171. REGULATOR_VOLTAGE,
  172. REGULATOR_CURRENT,
  173. };
  174. /**
  175. * struct regulator_desc - Static regulator descriptor
  176. *
  177. * Each regulator registered with the core is described with a
  178. * structure of this type and a struct regulator_config. This
  179. * structure contains the non-varying parts of the regulator
  180. * description.
  181. *
  182. * @name: Identifying name for the regulator.
  183. * @supply_name: Identifying the regulator supply
  184. * @of_match: Name used to identify regulator in DT.
  185. * @regulators_node: Name of node containing regulator definitions in DT.
  186. * @id: Numerical identifier for the regulator.
  187. * @ops: Regulator operations table.
  188. * @irq: Interrupt number for the regulator.
  189. * @type: Indicates if the regulator is a voltage or current regulator.
  190. * @owner: Module providing the regulator, used for refcounting.
  191. *
  192. * @continuous_voltage_range: Indicates if the regulator can set any
  193. * voltage within constrains range.
  194. * @n_voltages: Number of selectors available for ops.list_voltage().
  195. *
  196. * @min_uV: Voltage given by the lowest selector (if linear mapping)
  197. * @uV_step: Voltage increase with each selector (if linear mapping)
  198. * @linear_min_sel: Minimal selector for starting linear mapping
  199. * @fixed_uV: Fixed voltage of rails.
  200. * @ramp_delay: Time to settle down after voltage change (unit: uV/us)
  201. * @linear_ranges: A constant table of possible voltage ranges.
  202. * @n_linear_ranges: Number of entries in the @linear_ranges table.
  203. * @volt_table: Voltage mapping table (if table based mapping)
  204. *
  205. * @vsel_reg: Register for selector when using regulator_regmap_X_voltage_
  206. * @vsel_mask: Mask for register bitfield used for selector
  207. * @apply_reg: Register for initiate voltage change on the output when
  208. * using regulator_set_voltage_sel_regmap
  209. * @apply_bit: Register bitfield used for initiate voltage change on the
  210. * output when using regulator_set_voltage_sel_regmap
  211. * @enable_reg: Register for control when using regmap enable/disable ops
  212. * @enable_mask: Mask for control when using regmap enable/disable ops
  213. * @enable_val: Enabling value for control when using regmap enable/disable ops
  214. * @disable_val: Disabling value for control when using regmap enable/disable ops
  215. * @enable_is_inverted: A flag to indicate set enable_mask bits to disable
  216. * when using regulator_enable_regmap and friends APIs.
  217. * @bypass_reg: Register for control when using regmap set_bypass
  218. * @bypass_mask: Mask for control when using regmap set_bypass
  219. * @bypass_val_on: Enabling value for control when using regmap set_bypass
  220. * @bypass_val_off: Disabling value for control when using regmap set_bypass
  221. *
  222. * @enable_time: Time taken for initial enable of regulator (in uS).
  223. * @off_on_delay: guard time (in uS), before re-enabling a regulator
  224. */
  225. struct regulator_desc {
  226. const char *name;
  227. const char *supply_name;
  228. const char *of_match;
  229. const char *regulators_node;
  230. int id;
  231. bool continuous_voltage_range;
  232. unsigned n_voltages;
  233. const struct regulator_ops *ops;
  234. int irq;
  235. enum regulator_type type;
  236. struct module *owner;
  237. unsigned int min_uV;
  238. unsigned int uV_step;
  239. unsigned int linear_min_sel;
  240. int fixed_uV;
  241. unsigned int ramp_delay;
  242. const struct regulator_linear_range *linear_ranges;
  243. int n_linear_ranges;
  244. const unsigned int *volt_table;
  245. unsigned int vsel_reg;
  246. unsigned int vsel_mask;
  247. unsigned int apply_reg;
  248. unsigned int apply_bit;
  249. unsigned int enable_reg;
  250. unsigned int enable_mask;
  251. unsigned int enable_val;
  252. unsigned int disable_val;
  253. bool enable_is_inverted;
  254. unsigned int bypass_reg;
  255. unsigned int bypass_mask;
  256. unsigned int bypass_val_on;
  257. unsigned int bypass_val_off;
  258. unsigned int enable_time;
  259. unsigned int off_on_delay;
  260. };
  261. /**
  262. * struct regulator_config - Dynamic regulator descriptor
  263. *
  264. * Each regulator registered with the core is described with a
  265. * structure of this type and a struct regulator_desc. This structure
  266. * contains the runtime variable parts of the regulator description.
  267. *
  268. * @dev: struct device for the regulator
  269. * @init_data: platform provided init data, passed through by driver
  270. * @driver_data: private regulator data
  271. * @of_node: OpenFirmware node to parse for device tree bindings (may be
  272. * NULL).
  273. * @regmap: regmap to use for core regmap helpers if dev_get_regulator() is
  274. * insufficient.
  275. * @ena_gpio_initialized: GPIO controlling regulator enable was properly
  276. * initialized, meaning that >= 0 is a valid gpio
  277. * identifier and < 0 is a non existent gpio.
  278. * @ena_gpio: GPIO controlling regulator enable.
  279. * @ena_gpio_invert: Sense for GPIO enable control.
  280. * @ena_gpio_flags: Flags to use when calling gpio_request_one()
  281. */
  282. struct regulator_config {
  283. struct device *dev;
  284. const struct regulator_init_data *init_data;
  285. void *driver_data;
  286. struct device_node *of_node;
  287. struct regmap *regmap;
  288. bool ena_gpio_initialized;
  289. int ena_gpio;
  290. unsigned int ena_gpio_invert:1;
  291. unsigned int ena_gpio_flags;
  292. };
  293. /*
  294. * struct regulator_dev
  295. *
  296. * Voltage / Current regulator class device. One for each
  297. * regulator.
  298. *
  299. * This should *not* be used directly by anything except the regulator
  300. * core and notification injection (which should take the mutex and do
  301. * no other direct access).
  302. */
  303. struct regulator_dev {
  304. const struct regulator_desc *desc;
  305. int exclusive;
  306. u32 use_count;
  307. u32 open_count;
  308. u32 bypass_count;
  309. /* lists we belong to */
  310. struct list_head list; /* list of all regulators */
  311. /* lists we own */
  312. struct list_head consumer_list; /* consumers we supply */
  313. struct blocking_notifier_head notifier;
  314. struct mutex mutex; /* consumer lock */
  315. struct module *owner;
  316. struct device dev;
  317. struct regulation_constraints *constraints;
  318. struct regulator *supply; /* for tree */
  319. struct regmap *regmap;
  320. struct delayed_work disable_work;
  321. int deferred_disables;
  322. void *reg_data; /* regulator_dev data */
  323. struct dentry *debugfs;
  324. struct regulator_enable_gpio *ena_pin;
  325. unsigned int ena_gpio_state:1;
  326. /* time when this regulator was disabled last time */
  327. unsigned long last_off_jiffy;
  328. };
  329. struct regulator_dev *
  330. regulator_register(const struct regulator_desc *regulator_desc,
  331. const struct regulator_config *config);
  332. struct regulator_dev *
  333. devm_regulator_register(struct device *dev,
  334. const struct regulator_desc *regulator_desc,
  335. const struct regulator_config *config);
  336. void regulator_unregister(struct regulator_dev *rdev);
  337. void devm_regulator_unregister(struct device *dev, struct regulator_dev *rdev);
  338. int regulator_notifier_call_chain(struct regulator_dev *rdev,
  339. unsigned long event, void *data);
  340. void *rdev_get_drvdata(struct regulator_dev *rdev);
  341. struct device *rdev_get_dev(struct regulator_dev *rdev);
  342. int rdev_get_id(struct regulator_dev *rdev);
  343. int regulator_mode_to_status(unsigned int);
  344. int regulator_list_voltage_linear(struct regulator_dev *rdev,
  345. unsigned int selector);
  346. int regulator_list_voltage_linear_range(struct regulator_dev *rdev,
  347. unsigned int selector);
  348. int regulator_list_voltage_table(struct regulator_dev *rdev,
  349. unsigned int selector);
  350. int regulator_map_voltage_linear(struct regulator_dev *rdev,
  351. int min_uV, int max_uV);
  352. int regulator_map_voltage_linear_range(struct regulator_dev *rdev,
  353. int min_uV, int max_uV);
  354. int regulator_map_voltage_iterate(struct regulator_dev *rdev,
  355. int min_uV, int max_uV);
  356. int regulator_map_voltage_ascend(struct regulator_dev *rdev,
  357. int min_uV, int max_uV);
  358. int regulator_get_voltage_sel_regmap(struct regulator_dev *rdev);
  359. int regulator_set_voltage_sel_regmap(struct regulator_dev *rdev, unsigned sel);
  360. int regulator_is_enabled_regmap(struct regulator_dev *rdev);
  361. int regulator_enable_regmap(struct regulator_dev *rdev);
  362. int regulator_disable_regmap(struct regulator_dev *rdev);
  363. int regulator_set_voltage_time_sel(struct regulator_dev *rdev,
  364. unsigned int old_selector,
  365. unsigned int new_selector);
  366. int regulator_set_bypass_regmap(struct regulator_dev *rdev, bool enable);
  367. int regulator_get_bypass_regmap(struct regulator_dev *rdev, bool *enable);
  368. void *regulator_get_init_drvdata(struct regulator_init_data *reg_init_data);
  369. #endif