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