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