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