mm: Don't pin ZERO_PAGE in pin_user_pages()
[linux-block.git] / include / linux / regulator / driver.h
CommitLineData
d2912cb1 1/* SPDX-License-Identifier: GPL-2.0-only */
571a354b
LG
2/*
3 * driver.h -- SoC Regulator driver support.
4 *
5 * Copyright (C) 2007, 2008 Wolfson Microelectronics PLC.
6 *
1dd68f01 7 * Author: Liam Girdwood <lrg@slimlogic.co.uk>
571a354b 8 *
571a354b
LG
9 * Regulator Driver Interface.
10 */
11
12#ifndef __LINUX_REGULATOR_DRIVER_H_
13#define __LINUX_REGULATOR_DRIVER_H_
14
15#include <linux/device.h>
60ab7f41 16#include <linux/linear_range.h>
ced55d4e 17#include <linux/notifier.h>
571a354b 18#include <linux/regulator/consumer.h>
f8702f9e 19#include <linux/ww_mutex.h>
571a354b 20
e45e290a 21struct gpio_desc;
65b19ce6 22struct regmap;
571a354b 23struct regulator_dev;
bfa21a0d 24struct regulator_config;
a5766f11 25struct regulator_init_data;
f19b00da 26struct regulator_enable_gpio;
571a354b 27
853116a1
DB
28enum regulator_status {
29 REGULATOR_STATUS_OFF,
30 REGULATOR_STATUS_ON,
31 REGULATOR_STATUS_ERROR,
32 /* fast/normal/idle/standby are flavors of "on" */
33 REGULATOR_STATUS_FAST,
34 REGULATOR_STATUS_NORMAL,
35 REGULATOR_STATUS_IDLE,
36 REGULATOR_STATUS_STANDBY,
f59c8f9f
MB
37 /* The regulator is enabled but not regulating */
38 REGULATOR_STATUS_BYPASS,
1beaf762
KG
39 /* in case that any other status doesn't apply */
40 REGULATOR_STATUS_UNDEFINED,
853116a1
DB
41};
42
89a6a5e5
MV
43enum regulator_detection_severity {
44 /* Hardware shut down voltage outputs if condition is detected */
45 REGULATOR_SEVERITY_PROT,
46 /* Hardware is probably damaged/inoperable */
47 REGULATOR_SEVERITY_ERR,
48 /* Hardware is still recoverable but recovery action must be taken */
49 REGULATOR_SEVERITY_WARN,
50};
51
60ab7f41 52/* Initialize struct linear_range for regulators */
8828bae4
AL
53#define REGULATOR_LINEAR_RANGE(_min_uV, _min_sel, _max_sel, _step_uV) \
54{ \
60ab7f41 55 .min = _min_uV, \
8828bae4
AL
56 .min_sel = _min_sel, \
57 .max_sel = _max_sel, \
60ab7f41 58 .step = _step_uV, \
8828bae4
AL
59}
60
571a354b
LG
61/**
62 * struct regulator_ops - regulator operations.
63 *
3b2a6061
DB
64 * @enable: Configure the regulator as enabled.
65 * @disable: Configure the regulator as disabled.
d87b969d
WS
66 * @is_enabled: Return 1 if the regulator is enabled, 0 if not.
67 * May also return negative errno.
c8e7e464
MB
68 *
69 * @set_voltage: Set the voltage for the regulator within the range specified.
70 * The driver should select the voltage closest to min_uV.
e8eef82b
MB
71 * @set_voltage_sel: Set the voltage for the regulator using the specified
72 * selector.
e843fc46 73 * @map_voltage: Convert a voltage into a selector
84b3a7c9
DA
74 * @get_voltage: Return the currently configured voltage for the regulator;
75 * return -ENOTRECOVERABLE if regulator can't be read at
76 * bootup and hasn't been set yet.
476c2d83 77 * @get_voltage_sel: Return the currently configured voltage selector for the
84b3a7c9
DA
78 * regulator; return -ENOTRECOVERABLE if regulator can't
79 * be read at bootup and hasn't been set yet.
4367cfdc
DB
80 * @list_voltage: Return one of the supported voltages, in microvolts; zero
81 * if the selector indicates a voltage that is unusable on this system;
82 * or negative errno. Selectors range from zero to one less than
83 * regulator_desc.n_voltages. Voltages may be reported in any order.
c8e7e464 84 *
c8e7e464 85 * @set_current_limit: Configure a limit for a current-limited regulator.
89009e18 86 * The driver should select the current closest to max_uA.
3b2a6061 87 * @get_current_limit: Get the configured limit for a current-limited regulator.
36e4f839 88 * @set_input_current_limit: Configure an input limit.
c8e7e464 89 *
89a6a5e5
MV
90 * @set_over_current_protection: Support enabling of and setting limits for over
91 * current situation detection. Detection can be configured for three
92 * levels of severity.
8d9f738f
YS
93 *
94 * - REGULATOR_SEVERITY_PROT should automatically shut down the regulator(s).
95 *
96 * - REGULATOR_SEVERITY_ERR should indicate that over-current situation is
97 * caused by an unrecoverable error but HW does not perform
98 * automatic shut down.
99 *
100 * - REGULATOR_SEVERITY_WARN should indicate situation where hardware is
101 * still believed to not be damaged but that a board sepcific
102 * recovery action is needed. If lim_uA is 0 the limit should not
103 * be changed but the detection should just be enabled/disabled as
104 * is requested.
105 *
89a6a5e5
MV
106 * @set_over_voltage_protection: Support enabling of and setting limits for over
107 * voltage situation detection. Detection can be configured for same
92b13482 108 * severities as over current protection. Units of uV.
89a6a5e5 109 * @set_under_voltage_protection: Support enabling of and setting limits for
92b13482
MV
110 * under voltage situation detection. Detection can be configured for same
111 * severities as over current protection. Units of uV.
89a6a5e5 112 * @set_thermal_protection: Support enabling of and setting limits for over
92b13482
MV
113 * temperature situation detection.Detection can be configured for same
114 * severities as over current protection. Units of degree Kelvin.
abf2f825 115 *
670666b9
LD
116 * @set_active_discharge: Set active discharge enable/disable of regulators.
117 *
9f653251 118 * @set_mode: Set the configured operating mode for the regulator.
3b2a6061 119 * @get_mode: Get the configured operating mode for the regulator.
1b5b4221 120 * @get_error_flags: Get the current error(s) for the regulator.
3b2a6061
DB
121 * @get_status: Return actual (not as-configured) status of regulator, as a
122 * REGULATOR_STATUS value (or negative errno)
c8e7e464
MB
123 * @get_optimum_mode: Get the most efficient operating mode for the regulator
124 * when running with the specified parameters.
8f4490e0 125 * @set_load: Set the load for the regulator.
c8e7e464 126 *
f59c8f9f
MB
127 * @set_bypass: Set the regulator in bypass mode.
128 * @get_bypass: Get the regulator bypass mode state.
129 *
31aae2be 130 * @enable_time: Time taken for the regulator voltage output voltage to
77af1b26 131 * stabilise after being enabled, in microseconds.
6f0b2c69
YSB
132 * @set_ramp_delay: Set the ramp delay for the regulator. The driver should
133 * select ramp delay equal to or less than(closest) ramp_delay.
73e705bf
MK
134 * @set_voltage_time: Time taken for the regulator voltage output voltage
135 * to stabilise after being set to a new value, in microseconds.
136 * The function receives the from and to voltage as input, it
137 * should return the worst case.
77af1b26
LW
138 * @set_voltage_time_sel: Time taken for the regulator voltage output voltage
139 * to stabilise after being set to a new value, in microseconds.
73e705bf
MK
140 * The function receives the from and to voltage selector as
141 * input, it should return the worst case.
c751ad0d 142 * @set_soft_start: Enable soft start for the regulator.
31aae2be 143 *
c8e7e464
MB
144 * @set_suspend_voltage: Set the voltage for the regulator when the system
145 * is suspended.
146 * @set_suspend_enable: Mark the regulator as enabled when the system is
147 * suspended.
148 * @set_suspend_disable: Mark the regulator as disabled when the system is
149 * suspended.
150 * @set_suspend_mode: Set the operating mode for the regulator when the
151 * system is suspended.
a98bcaa9 152 * @resume: Resume operation of suspended regulator.
23c779b9
SB
153 * @set_pull_down: Configure the regulator to pull down when the regulator
154 * is disabled.
155 *
3b2a6061
DB
156 * This struct describes regulator operations which can be implemented by
157 * regulator chip drivers.
571a354b
LG
158 */
159struct regulator_ops {
160
4367cfdc
DB
161 /* enumerate supported voltages */
162 int (*list_voltage) (struct regulator_dev *, unsigned selector);
163
571a354b 164 /* get/set regulator voltage */
3a93f2a9
MB
165 int (*set_voltage) (struct regulator_dev *, int min_uV, int max_uV,
166 unsigned *selector);
e843fc46 167 int (*map_voltage)(struct regulator_dev *, int min_uV, int max_uV);
e8eef82b 168 int (*set_voltage_sel) (struct regulator_dev *, unsigned selector);
571a354b 169 int (*get_voltage) (struct regulator_dev *);
476c2d83 170 int (*get_voltage_sel) (struct regulator_dev *);
571a354b
LG
171
172 /* get/set regulator current */
173 int (*set_current_limit) (struct regulator_dev *,
174 int min_uA, int max_uA);
175 int (*get_current_limit) (struct regulator_dev *);
176
36e4f839 177 int (*set_input_current_limit) (struct regulator_dev *, int lim_uA);
89a6a5e5
MV
178 int (*set_over_current_protection)(struct regulator_dev *, int lim_uA,
179 int severity, bool enable);
180 int (*set_over_voltage_protection)(struct regulator_dev *, int lim_uV,
181 int severity, bool enable);
182 int (*set_under_voltage_protection)(struct regulator_dev *, int lim_uV,
183 int severity, bool enable);
184 int (*set_thermal_protection)(struct regulator_dev *, int lim,
185 int severity, bool enable);
186 int (*set_active_discharge)(struct regulator_dev *, bool enable);
36e4f839 187
571a354b
LG
188 /* enable/disable regulator */
189 int (*enable) (struct regulator_dev *);
190 int (*disable) (struct regulator_dev *);
191 int (*is_enabled) (struct regulator_dev *);
192
fde297bb 193 /* get/set regulator operating mode (defined in consumer.h) */
571a354b
LG
194 int (*set_mode) (struct regulator_dev *, unsigned int mode);
195 unsigned int (*get_mode) (struct regulator_dev *);
196
1b5b4221
AH
197 /* retrieve current error flags on the regulator */
198 int (*get_error_flags)(struct regulator_dev *, unsigned int *flags);
199
77af1b26 200 /* Time taken to enable or set voltage on the regulator */
31aae2be 201 int (*enable_time) (struct regulator_dev *);
6f0b2c69 202 int (*set_ramp_delay) (struct regulator_dev *, int ramp_delay);
73e705bf
MK
203 int (*set_voltage_time) (struct regulator_dev *, int old_uV,
204 int new_uV);
77af1b26
LW
205 int (*set_voltage_time_sel) (struct regulator_dev *,
206 unsigned int old_selector,
207 unsigned int new_selector);
31aae2be 208
57f66b78
SB
209 int (*set_soft_start) (struct regulator_dev *);
210
853116a1
DB
211 /* report regulator status ... most other accessors report
212 * control inputs, this reports results of combining inputs
213 * from Linux (and other sources) with the actual load.
3b2a6061 214 * returns REGULATOR_STATUS_* or negative errno.
853116a1
DB
215 */
216 int (*get_status)(struct regulator_dev *);
217
571a354b
LG
218 /* get most efficient regulator operating mode for load */
219 unsigned int (*get_optimum_mode) (struct regulator_dev *, int input_uV,
220 int output_uV, int load_uA);
8f4490e0
BA
221 /* set the load on the regulator */
222 int (*set_load)(struct regulator_dev *, int load_uA);
571a354b 223
f59c8f9f
MB
224 /* control and report on bypass mode */
225 int (*set_bypass)(struct regulator_dev *dev, bool enable);
226 int (*get_bypass)(struct regulator_dev *dev, bool *enable);
227
571a354b 228 /* the operations below are for configuration of regulator state when
3de89609 229 * its parent PMIC enters a global STANDBY/HIBERNATE state */
571a354b
LG
230
231 /* set regulator suspend voltage */
232 int (*set_suspend_voltage) (struct regulator_dev *, int uV);
233
234 /* enable/disable regulator in suspend state */
235 int (*set_suspend_enable) (struct regulator_dev *);
236 int (*set_suspend_disable) (struct regulator_dev *);
237
fde297bb 238 /* set regulator suspend operating mode (defined in consumer.h) */
571a354b 239 int (*set_suspend_mode) (struct regulator_dev *, unsigned int mode);
23c779b9 240
0380cf7d 241 int (*resume)(struct regulator_dev *rdev);
f7efad10 242
23c779b9 243 int (*set_pull_down) (struct regulator_dev *);
571a354b
LG
244};
245
246/*
247 * Regulators can either control voltage or current.
248 */
249enum regulator_type {
250 REGULATOR_VOLTAGE,
251 REGULATOR_CURRENT,
252};
253
254/**
c172708d 255 * struct regulator_desc - Static regulator descriptor
571a354b 256 *
c172708d
MB
257 * Each regulator registered with the core is described with a
258 * structure of this type and a struct regulator_config. This
259 * structure contains the non-varying parts of the regulator
260 * description.
c8e7e464
MB
261 *
262 * @name: Identifying name for the regulator.
69511a45 263 * @supply_name: Identifying the regulator supply
a0c7b164 264 * @of_match: Name used to identify regulator in DT.
e7095c35
CM
265 * @of_match_full_name: A flag to indicate that the of_match string, if
266 * present, should be matched against the node full_name.
a0c7b164 267 * @regulators_node: Name of node containing regulator definitions in DT.
bfa21a0d
KK
268 * @of_parse_cb: Optional callback called only if of_match is present.
269 * Will be called for each regulator parsed from DT, during
270 * init_data parsing.
271 * The regulator_config passed as argument to the callback will
272 * be a copy of config passed to regulator_register, valid only
273 * for this particular call. Callback may freely change the
274 * config but it cannot store it for later usage.
275 * Callback should return 0 on success or negative ERRNO
276 * indicating failure.
c8e7e464
MB
277 * @id: Numerical identifier for the regulator.
278 * @ops: Regulator operations table.
0ba4887c 279 * @irq: Interrupt number for the regulator.
c8e7e464
MB
280 * @type: Indicates if the regulator is a voltage or current regulator.
281 * @owner: Module providing the regulator, used for refcounting.
bca7bbff 282 *
bd7a2b60
PM
283 * @continuous_voltage_range: Indicates if the regulator can set any
284 * voltage within constrains range.
bca7bbff 285 * @n_voltages: Number of selectors available for ops.list_voltage().
a32e0c77 286 * @n_current_limits: Number of selectors available for current limits
bca7bbff
MB
287 *
288 * @min_uV: Voltage given by the lowest selector (if linear mapping)
289 * @uV_step: Voltage increase with each selector (if linear mapping)
33234e79 290 * @linear_min_sel: Minimal selector for starting linear mapping
5a523605 291 * @fixed_uV: Fixed voltage of rails.
ea38d13f 292 * @ramp_delay: Time to settle down after voltage change (unit: uV/us)
5abe4f22 293 * @min_dropout_uV: The minimum dropout voltage this regulator can handle
a8dbfeed 294 * @linear_ranges: A constant table of possible voltage ranges.
18e4b55f
MV
295 * @linear_range_selectors: A constant table of voltage range selectors.
296 * If pickable ranges are used each range must
297 * have corresponding selector here.
298 * @n_linear_ranges: Number of entries in the @linear_ranges (and in
299 * linear_range_selectors if used) table(s).
cffc9592 300 * @volt_table: Voltage mapping table (if table based mapping)
a32e0c77 301 * @curr_table: Current limit mapping table (if table based mapping)
bca7bbff 302 *
18e4b55f 303 * @vsel_range_reg: Register for range selector when using pickable ranges
bd3ebed9 304 * and ``regulator_map_*_voltage_*_pickable`` functions.
18e4b55f 305 * @vsel_range_mask: Mask for register bitfield used for range selector
bd3ebed9 306 * @vsel_reg: Register for selector when using ``regulator_map_*_voltage_*``
4ab5b3d9 307 * @vsel_mask: Mask for register bitfield used for selector
2da8d947
BG
308 * @vsel_step: Specify the resolution of selector stepping when setting
309 * voltage. If 0, then no stepping is done (requested selector is
310 * set directly), if >0 then the regulator API will ramp the
311 * voltage up/down gradually each time increasing/decreasing the
312 * selector by the specified step value.
35d838ff
AL
313 * @csel_reg: Register for current limit selector using regmap set_current_limit
314 * @csel_mask: Mask for register bitfield used for current limit selector
c8520b4c
AL
315 * @apply_reg: Register for initiate voltage change on the output when
316 * using regulator_set_voltage_sel_regmap
317 * @apply_bit: Register bitfield used for initiate voltage change on the
318 * output when using regulator_set_voltage_sel_regmap
cd6dffb4
MB
319 * @enable_reg: Register for control when using regmap enable/disable ops
320 * @enable_mask: Mask for control when using regmap enable/disable ops
ca5d1b35
CC
321 * @enable_val: Enabling value for control when using regmap enable/disable ops
322 * @disable_val: Disabling value for control when using regmap enable/disable ops
51dcdafc
AL
323 * @enable_is_inverted: A flag to indicate set enable_mask bits to disable
324 * when using regulator_enable_regmap and friends APIs.
5838b032
NM
325 * @bypass_reg: Register for control when using regmap set_bypass
326 * @bypass_mask: Mask for control when using regmap set_bypass
ca5d1b35
CC
327 * @bypass_val_on: Enabling value for control when using regmap set_bypass
328 * @bypass_val_off: Disabling value for control when using regmap set_bypass
354794da
LD
329 * @active_discharge_off: Enabling value for control when using regmap
330 * set_active_discharge
331 * @active_discharge_on: Disabling value for control when using regmap
332 * set_active_discharge
333 * @active_discharge_mask: Mask for control when using regmap
334 * set_active_discharge
335 * @active_discharge_reg: Register for control when using regmap
336 * set_active_discharge
a7a453f5
CK
337 * @soft_start_reg: Register for control when using regmap set_soft_start
338 * @soft_start_mask: Mask for control when using regmap set_soft_start
339 * @soft_start_val_on: Enabling value for control when using regmap
340 * set_soft_start
f7d37bc3
CK
341 * @pull_down_reg: Register for control when using regmap set_pull_down
342 * @pull_down_mask: Mask for control when using regmap set_pull_down
343 * @pull_down_val_on: Enabling value for control when using regmap
344 * set_pull_down
79511ed3 345 *
c049742f
MV
346 * @ramp_reg: Register for controlling the regulator ramp-rate.
347 * @ramp_mask: Bitmask for the ramp-rate control register.
348 * @ramp_delay_table: Table for mapping the regulator ramp-rate values. Values
349 * should be given in units of V/S (uV/uS). See the
350 * regulator_set_ramp_delay_regmap().
0e584d46 351 * @n_ramp_values: number of elements at @ramp_delay_table.
c049742f 352 *
79511ed3 353 * @enable_time: Time taken for initial enable of regulator (in uS).
871f5650 354 * @off_on_delay: guard time (in uS), before re-enabling a regulator
87e1e0f2 355 *
f7d7ad42
SS
356 * @poll_enabled_time: The polling interval (in uS) to use while checking that
357 * the regulator was actually enabled. Max upto enable_time.
358 *
87e1e0f2 359 * @of_map_mode: Maps a hardware mode defined in a DeviceTree to a standard mode
571a354b
LG
360 */
361struct regulator_desc {
362 const char *name;
69511a45 363 const char *supply_name;
a0c7b164 364 const char *of_match;
e7095c35 365 bool of_match_full_name;
a0c7b164 366 const char *regulators_node;
bfa21a0d
KK
367 int (*of_parse_cb)(struct device_node *,
368 const struct regulator_desc *,
369 struct regulator_config *);
571a354b 370 int id;
de4a54c4 371 unsigned int continuous_voltage_range:1;
4367cfdc 372 unsigned n_voltages;
a32e0c77 373 unsigned int n_current_limits;
df11e506 374 const struct regulator_ops *ops;
571a354b
LG
375 int irq;
376 enum regulator_type type;
377 struct module *owner;
4ab5b3d9 378
bca7bbff
MB
379 unsigned int min_uV;
380 unsigned int uV_step;
33234e79 381 unsigned int linear_min_sel;
5a523605 382 int fixed_uV;
98a175b6 383 unsigned int ramp_delay;
5abe4f22 384 int min_dropout_uV;
bca7bbff 385
60ab7f41 386 const struct linear_range *linear_ranges;
18e4b55f
MV
387 const unsigned int *linear_range_selectors;
388
94d33c02
MB
389 int n_linear_ranges;
390
cffc9592 391 const unsigned int *volt_table;
a32e0c77 392 const unsigned int *curr_table;
cffc9592 393
18e4b55f
MV
394 unsigned int vsel_range_reg;
395 unsigned int vsel_range_mask;
4ab5b3d9
MB
396 unsigned int vsel_reg;
397 unsigned int vsel_mask;
2da8d947 398 unsigned int vsel_step;
c0ea88b8
NK
399 unsigned int csel_reg;
400 unsigned int csel_mask;
c8520b4c
AL
401 unsigned int apply_reg;
402 unsigned int apply_bit;
cd6dffb4
MB
403 unsigned int enable_reg;
404 unsigned int enable_mask;
ca5d1b35
CC
405 unsigned int enable_val;
406 unsigned int disable_val;
51dcdafc 407 bool enable_is_inverted;
df367931
MB
408 unsigned int bypass_reg;
409 unsigned int bypass_mask;
ca5d1b35
CC
410 unsigned int bypass_val_on;
411 unsigned int bypass_val_off;
354794da
LD
412 unsigned int active_discharge_on;
413 unsigned int active_discharge_off;
414 unsigned int active_discharge_mask;
415 unsigned int active_discharge_reg;
a7a453f5
CK
416 unsigned int soft_start_reg;
417 unsigned int soft_start_mask;
418 unsigned int soft_start_val_on;
f7d37bc3
CK
419 unsigned int pull_down_reg;
420 unsigned int pull_down_mask;
421 unsigned int pull_down_val_on;
fb8fee9e
MV
422 unsigned int ramp_reg;
423 unsigned int ramp_mask;
424 const unsigned int *ramp_delay_table;
425 unsigned int n_ramp_values;
79511ed3
MB
426
427 unsigned int enable_time;
871f5650
GX
428
429 unsigned int off_on_delay;
87e1e0f2 430
f7d7ad42
SS
431 unsigned int poll_enabled_time;
432
87e1e0f2 433 unsigned int (*of_map_mode)(unsigned int mode);
571a354b
LG
434};
435
c172708d
MB
436/**
437 * struct regulator_config - Dynamic regulator descriptor
438 *
439 * Each regulator registered with the core is described with a
440 * structure of this type and a struct regulator_desc. This structure
441 * contains the runtime variable parts of the regulator description.
442 *
443 * @dev: struct device for the regulator
444 * @init_data: platform provided init data, passed through by driver
445 * @driver_data: private regulator data
446 * @of_node: OpenFirmware node to parse for device tree bindings (may be
447 * NULL).
cf39284d 448 * @regmap: regmap to use for core regmap helpers if dev_get_regmap() is
380a0e6f 449 * insufficient.
541d052d 450 * @ena_gpiod: GPIO controlling regulator enable.
c172708d
MB
451 */
452struct regulator_config {
453 struct device *dev;
454 const struct regulator_init_data *init_data;
455 void *driver_data;
456 struct device_node *of_node;
65b19ce6 457 struct regmap *regmap;
65f73508 458
e45e290a 459 struct gpio_desc *ena_gpiod;
c172708d
MB
460};
461
7111c6d1
MV
462/**
463 * struct regulator_err_state - regulator error/notification status
464 *
465 * @rdev: Regulator which status the struct indicates.
466 * @notifs: Events which have occurred on the regulator.
467 * @errors: Errors which are active on the regulator.
468 * @possible_errs: Errors which can be signaled (by given IRQ).
469 */
470struct regulator_err_state {
471 struct regulator_dev *rdev;
472 unsigned long notifs;
473 unsigned long errors;
474 int possible_errs;
475};
476
477/**
c049742f 478 * struct regulator_irq_data - regulator error/notification status data
7111c6d1
MV
479 *
480 * @states: Status structs for each of the associated regulators.
481 * @num_states: Amount of associated regulators.
482 * @data: Driver data pointer given at regulator_irq_desc.
483 * @opaque: Value storage for IC driver. Core does not update this. ICs
484 * may want to store status register value here at map_event and
485 * compare contents at 'renable' callback to see if new problems
486 * have been added to status. If that is the case it may be
487 * desirable to return REGULATOR_ERROR_CLEARED and not
488 * REGULATOR_ERROR_ON to allow IRQ fire again and to generate
489 * notifications also for the new issues.
490 *
491 * This structure is passed to 'map_event' and 'renable' callbacks for
492 * reporting regulator status to core.
493 */
494struct regulator_irq_data {
495 struct regulator_err_state *states;
496 int num_states;
497 void *data;
498 long opaque;
499};
500
501/**
502 * struct regulator_irq_desc - notification sender for IRQ based events.
503 *
504 * @name: The visible name for the IRQ
505 * @fatal_cnt: If this IRQ is used to signal HW damaging condition it may be
506 * best to shut-down regulator(s) or reboot the SOC if error
507 * handling is repeatedly failing. If fatal_cnt is given the IRQ
508 * handling is aborted if it fails for fatal_cnt times and die()
6966df48
MV
509 * callback (if populated) is called. If die() is not populated
510 * poweroff for the system is attempted in order to prevent any
7111c6d1
MV
511 * further damage.
512 * @reread_ms: The time which is waited before attempting to re-read status
513 * at the worker if IC reading fails. Immediate re-read is done
514 * if time is not specified.
515 * @irq_off_ms: The time which IRQ is kept disabled before re-evaluating the
516 * status for devices which keep IRQ disabled for duration of the
517 * error. If this is not given the IRQ is left enabled and renable
518 * is not called.
519 * @skip_off: If set to true the IRQ handler will attempt to check if any of
520 * the associated regulators are enabled prior to taking other
521 * actions. If no regulators are enabled and this is set to true
522 * a spurious IRQ is assumed and IRQ_NONE is returned.
523 * @high_prio: Boolean to indicate that high priority WQ should be used.
524 * @data: Driver private data pointer which will be passed as such to
525 * the renable, map_event and die callbacks in regulator_irq_data.
526 * @die: Protection callback. If IC status reading or recovery actions
6966df48
MV
527 * fail fatal_cnt times this callback is called or system is
528 * powered off. This callback should implement a final protection
529 * attempt like disabling the regulator. If protection succeeded
530 * die() may return 0. If anything else is returned the core
531 * assumes final protection failed and attempts to perform a
532 * poweroff as a last resort.
7111c6d1
MV
533 * @map_event: Driver callback to map IRQ status into regulator devices with
534 * events / errors. NOTE: callback MUST initialize both the
535 * errors and notifs for all rdevs which it signals having
536 * active events as core does not clean the map data.
537 * REGULATOR_FAILED_RETRY can be returned to indicate that the
538 * status reading from IC failed. If this is repeated for
ad3ead1e
MV
539 * fatal_cnt times the core will call die() callback or power-off
540 * the system as a last resort to protect the HW.
7111c6d1
MV
541 * @renable: Optional callback to check status (if HW supports that) before
542 * re-enabling IRQ. If implemented this should clear the error
543 * flags so that errors fetched by regulator_get_error_flags()
544 * are updated. If callback is not implemented then errors are
545 * assumed to be cleared and IRQ is re-enabled.
546 * REGULATOR_FAILED_RETRY can be returned to
547 * indicate that the status reading from IC failed. If this is
548 * repeated for 'fatal_cnt' times the core will call die()
ad3ead1e
MV
549 * callback or if die() is not populated then attempt to power-off
550 * the system as a last resort to protect the HW.
7111c6d1
MV
551 * Returning zero indicates that the problem in HW has been solved
552 * and IRQ will be re-enabled. Returning REGULATOR_ERROR_ON
553 * indicates the error condition is still active and keeps IRQ
554 * disabled. Please note that returning REGULATOR_ERROR_ON does
555 * not retrigger evaluating what events are active or resending
556 * notifications. If this is needed you probably want to return
557 * zero and allow IRQ to retrigger causing events to be
558 * re-evaluated and re-sent.
559 *
560 * This structure is used for registering regulator IRQ notification helper.
561 */
562struct regulator_irq_desc {
563 const char *name;
7111c6d1
MV
564 int fatal_cnt;
565 int reread_ms;
566 int irq_off_ms;
567 bool skip_off;
568 bool high_prio;
569 void *data;
570
571 int (*die)(struct regulator_irq_data *rid);
572 int (*map_event)(int irq, struct regulator_irq_data *rid,
573 unsigned long *dev_mask);
574 int (*renable)(struct regulator_irq_data *rid);
575};
576
577/*
578 * Return values for regulator IRQ helpers.
579 */
580enum {
581 REGULATOR_ERROR_CLEARED,
582 REGULATOR_FAILED_RETRY,
583 REGULATOR_ERROR_ON,
584};
585
a085a31a
MP
586/*
587 * struct coupling_desc
588 *
589 * Describes coupling of regulators. Each regulator should have
590 * at least a pointer to itself in coupled_rdevs array.
591 * When a new coupled regulator is resolved, n_resolved is
592 * incremented.
593 */
594struct coupling_desc {
d8ca7d18
DO
595 struct regulator_dev **coupled_rdevs;
596 struct regulator_coupler *coupler;
a085a31a
MP
597 int n_resolved;
598 int n_coupled;
599};
600
1fa9ad52
MB
601/*
602 * struct regulator_dev
603 *
604 * Voltage / Current regulator class device. One for each
605 * regulator.
606 *
607 * This should *not* be used directly by anything except the regulator
608 * core and notification injection (which should take the mutex and do
609 * no other direct access).
610 */
611struct regulator_dev {
65f26846 612 const struct regulator_desc *desc;
5ffbd136 613 int exclusive;
1130e5b3
MB
614 u32 use_count;
615 u32 open_count;
f59c8f9f 616 u32 bypass_count;
1fa9ad52
MB
617
618 /* lists we belong to */
619 struct list_head list; /* list of all regulators */
1fa9ad52
MB
620
621 /* lists we own */
622 struct list_head consumer_list; /* consumers we supply */
1fa9ad52 623
a085a31a
MP
624 struct coupling_desc coupling_desc;
625
1fa9ad52 626 struct blocking_notifier_head notifier;
f8702f9e 627 struct ww_mutex mutex; /* consumer lock */
66cf9a7e
MP
628 struct task_struct *mutex_owner;
629 int ref_cnt;
1fa9ad52
MB
630 struct module *owner;
631 struct device dev;
632 struct regulation_constraints *constraints;
3801b86a 633 struct regulator *supply; /* for tree */
6261b06d 634 const char *supply_name;
65b19ce6 635 struct regmap *regmap;
1fa9ad52 636
da07ecd9 637 struct delayed_work disable_work;
da07ecd9 638
1fa9ad52 639 void *reg_data; /* regulator_dev data */
1130e5b3 640
1130e5b3 641 struct dentry *debugfs;
65f73508 642
f19b00da 643 struct regulator_enable_gpio *ena_pin;
65f73508 644 unsigned int ena_gpio_state:1;
871f5650 645
fd086045
MK
646 unsigned int is_switch:1;
647
871f5650 648 /* time when this regulator was disabled last time */
a8ce7bd8 649 ktime_t last_off;
7111c6d1
MV
650 int cached_err;
651 bool use_cached_err;
652 spinlock_t err_lock;
1fa9ad52
MB
653};
654
6fadec4c
MV
655/*
656 * Convert error flags to corresponding notifications.
657 *
658 * Can be used by drivers which use the notification helpers to
659 * find out correct notification flags based on the error flags. Drivers
660 * can avoid storing both supported notification and error flags which
661 * may save few bytes.
662 */
663static inline int regulator_err2notif(int err)
664{
665 switch (err) {
666 case REGULATOR_ERROR_UNDER_VOLTAGE:
667 return REGULATOR_EVENT_UNDER_VOLTAGE;
668 case REGULATOR_ERROR_OVER_CURRENT:
669 return REGULATOR_EVENT_OVER_CURRENT;
670 case REGULATOR_ERROR_REGULATION_OUT:
671 return REGULATOR_EVENT_REGULATION_OUT;
672 case REGULATOR_ERROR_FAIL:
673 return REGULATOR_EVENT_FAIL;
674 case REGULATOR_ERROR_OVER_TEMP:
675 return REGULATOR_EVENT_OVER_TEMP;
676 case REGULATOR_ERROR_UNDER_VOLTAGE_WARN:
677 return REGULATOR_EVENT_UNDER_VOLTAGE_WARN;
678 case REGULATOR_ERROR_OVER_CURRENT_WARN:
679 return REGULATOR_EVENT_OVER_CURRENT_WARN;
680 case REGULATOR_ERROR_OVER_VOLTAGE_WARN:
681 return REGULATOR_EVENT_OVER_VOLTAGE_WARN;
682 case REGULATOR_ERROR_OVER_TEMP_WARN:
683 return REGULATOR_EVENT_OVER_TEMP_WARN;
684 }
685 return 0;
686}
687
688
65f26846 689struct regulator_dev *
8f3cbcd6
CH
690regulator_register(struct device *dev,
691 const struct regulator_desc *regulator_desc,
c172708d 692 const struct regulator_config *config);
b33e46bc
MB
693struct regulator_dev *
694devm_regulator_register(struct device *dev,
695 const struct regulator_desc *regulator_desc,
696 const struct regulator_config *config);
571a354b
LG
697void regulator_unregister(struct regulator_dev *rdev);
698
699int regulator_notifier_call_chain(struct regulator_dev *rdev,
700 unsigned long event, void *data);
7111c6d1
MV
701void *devm_regulator_irq_helper(struct device *dev,
702 const struct regulator_irq_desc *d, int irq,
703 int irq_flags, int common_errs,
704 int *per_rdev_errs, struct regulator_dev **rdev,
705 int rdev_amount);
706void *regulator_irq_helper(struct device *dev,
707 const struct regulator_irq_desc *d, int irq,
708 int irq_flags, int common_errs, int *per_rdev_errs,
709 struct regulator_dev **rdev, int rdev_amount);
710void regulator_irq_helper_cancel(void **handle);
a764ff77
MV
711int regulator_irq_map_event_simple(int irq, struct regulator_irq_data *rid,
712 unsigned long *dev_mask);
571a354b
LG
713
714void *rdev_get_drvdata(struct regulator_dev *rdev);
a5766f11 715struct device *rdev_get_dev(struct regulator_dev *rdev);
03c87b95 716struct regmap *rdev_get_regmap(struct regulator_dev *rdev);
571a354b
LG
717int rdev_get_id(struct regulator_dev *rdev);
718
be721979
MB
719int regulator_mode_to_status(unsigned int);
720
bca7bbff
MB
721int regulator_list_voltage_linear(struct regulator_dev *rdev,
722 unsigned int selector);
18e4b55f
MV
723int regulator_list_voltage_pickable_linear_range(struct regulator_dev *rdev,
724 unsigned int selector);
94d33c02
MB
725int regulator_list_voltage_linear_range(struct regulator_dev *rdev,
726 unsigned int selector);
cffc9592
AL
727int regulator_list_voltage_table(struct regulator_dev *rdev,
728 unsigned int selector);
bca7bbff
MB
729int regulator_map_voltage_linear(struct regulator_dev *rdev,
730 int min_uV, int max_uV);
18e4b55f
MV
731int regulator_map_voltage_pickable_linear_range(struct regulator_dev *rdev,
732 int min_uV, int max_uV);
94d33c02
MB
733int regulator_map_voltage_linear_range(struct regulator_dev *rdev,
734 int min_uV, int max_uV);
e843fc46
MB
735int regulator_map_voltage_iterate(struct regulator_dev *rdev,
736 int min_uV, int max_uV);
fcf371ee
AL
737int regulator_map_voltage_ascend(struct regulator_dev *rdev,
738 int min_uV, int max_uV);
18e4b55f
MV
739int regulator_get_voltage_sel_pickable_regmap(struct regulator_dev *rdev);
740int regulator_set_voltage_sel_pickable_regmap(struct regulator_dev *rdev,
741 unsigned int sel);
4ab5b3d9
MB
742int regulator_get_voltage_sel_regmap(struct regulator_dev *rdev);
743int regulator_set_voltage_sel_regmap(struct regulator_dev *rdev, unsigned sel);
cd6dffb4
MB
744int regulator_is_enabled_regmap(struct regulator_dev *rdev);
745int regulator_enable_regmap(struct regulator_dev *rdev);
746int regulator_disable_regmap(struct regulator_dev *rdev);
98a175b6
YSB
747int regulator_set_voltage_time_sel(struct regulator_dev *rdev,
748 unsigned int old_selector,
749 unsigned int new_selector);
df367931
MB
750int regulator_set_bypass_regmap(struct regulator_dev *rdev, bool enable);
751int regulator_get_bypass_regmap(struct regulator_dev *rdev, bool *enable);
a7a453f5 752int regulator_set_soft_start_regmap(struct regulator_dev *rdev);
f7d37bc3 753int regulator_set_pull_down_regmap(struct regulator_dev *rdev);
4ab5b3d9 754
354794da
LD
755int regulator_set_active_discharge_regmap(struct regulator_dev *rdev,
756 bool enable);
a32e0c77
AL
757int regulator_set_current_limit_regmap(struct regulator_dev *rdev,
758 int min_uA, int max_uA);
759int regulator_get_current_limit_regmap(struct regulator_dev *rdev);
a5766f11 760void *regulator_get_init_drvdata(struct regulator_init_data *reg_init_data);
fb8fee9e 761int regulator_set_ramp_delay_regmap(struct regulator_dev *rdev, int ramp_delay);
380d2b2d 762int regulator_sync_voltage_rdev(struct regulator_dev *rdev);
a5766f11 763
6a47b4da
MV
764/*
765 * Helper functions intended to be used by regulator drivers prior registering
766 * their regulators.
767 */
768int regulator_desc_list_voltage_linear_range(const struct regulator_desc *desc,
769 unsigned int selector);
d8ca7d18 770
e3baacf5
MV
771int regulator_desc_list_voltage_linear(const struct regulator_desc *desc,
772 unsigned int selector);
157d2230
MV
773
774#ifdef CONFIG_REGULATOR
775const char *rdev_get_name(struct regulator_dev *rdev);
776#else
777static inline const char *rdev_get_name(struct regulator_dev *rdev)
778{
779 return NULL;
780}
781#endif
782
571a354b 783#endif