driver.h 18 KB

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