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