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