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