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