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