1d2912cb1SThomas Gleixner /* SPDX-License-Identifier: GPL-2.0-only */
24c1184e8SLiam Girdwood /*
34c1184e8SLiam Girdwood  * machine.h -- SoC Regulator support, machine/board driver API.
44c1184e8SLiam Girdwood  *
54c1184e8SLiam Girdwood  * Copyright (C) 2007, 2008 Wolfson Microelectronics PLC.
64c1184e8SLiam Girdwood  *
71dd68f01SLiam Girdwood  * Author: Liam Girdwood <[email protected]>
84c1184e8SLiam Girdwood  *
94c1184e8SLiam Girdwood  * Regulator Machine/Board Interface.
104c1184e8SLiam Girdwood  */
114c1184e8SLiam Girdwood 
124c1184e8SLiam Girdwood #ifndef __LINUX_REGULATOR_MACHINE_H_
134c1184e8SLiam Girdwood #define __LINUX_REGULATOR_MACHINE_H_
144c1184e8SLiam Girdwood 
154c1184e8SLiam Girdwood #include <linux/regulator/consumer.h>
164c1184e8SLiam Girdwood #include <linux/suspend.h>
174c1184e8SLiam Girdwood 
184c1184e8SLiam Girdwood struct regulator;
194c1184e8SLiam Girdwood 
204c1184e8SLiam Girdwood /*
214c1184e8SLiam Girdwood  * Regulator operation constraint flags. These flags are used to enable
224c1184e8SLiam Girdwood  * certain regulator operations and can be OR'ed together.
234c1184e8SLiam Girdwood  *
244c1184e8SLiam Girdwood  * VOLTAGE:  Regulator output voltage can be changed by software on this
254c1184e8SLiam Girdwood  *           board/machine.
264c1184e8SLiam Girdwood  * CURRENT:  Regulator output current can be changed by software on this
274c1184e8SLiam Girdwood  *           board/machine.
284c1184e8SLiam Girdwood  * MODE:     Regulator operating mode can be changed by software on this
294c1184e8SLiam Girdwood  *           board/machine.
304c1184e8SLiam Girdwood  * STATUS:   Regulator can be enabled and disabled.
314c1184e8SLiam Girdwood  * DRMS:     Dynamic Regulator Mode Switching is enabled for this regulator.
32f59c8f9fSMark Brown  * BYPASS:   Regulator can be put into bypass mode
334c1184e8SLiam Girdwood  */
344c1184e8SLiam Girdwood 
354c1184e8SLiam Girdwood #define REGULATOR_CHANGE_VOLTAGE	0x1
364c1184e8SLiam Girdwood #define REGULATOR_CHANGE_CURRENT	0x2
374c1184e8SLiam Girdwood #define REGULATOR_CHANGE_MODE		0x4
384c1184e8SLiam Girdwood #define REGULATOR_CHANGE_STATUS		0x8
394c1184e8SLiam Girdwood #define REGULATOR_CHANGE_DRMS		0x10
40f59c8f9fSMark Brown #define REGULATOR_CHANGE_BYPASS		0x20
414c1184e8SLiam Girdwood 
4272069f99SChunyan Zhang /*
4372069f99SChunyan Zhang  * operations in suspend mode
4472069f99SChunyan Zhang  * DO_NOTHING_IN_SUSPEND - the default value
4572069f99SChunyan Zhang  * DISABLE_IN_SUSPEND	- turn off regulator in suspend states
4672069f99SChunyan Zhang  * ENABLE_IN_SUSPEND	- keep regulator on in suspend states
4772069f99SChunyan Zhang  */
483edd79cfSMarek Szyprowski #define DO_NOTHING_IN_SUSPEND	0
493edd79cfSMarek Szyprowski #define DISABLE_IN_SUSPEND	1
503edd79cfSMarek Szyprowski #define ENABLE_IN_SUSPEND	2
5172069f99SChunyan Zhang 
528156c7ddSOleksij Rempel /*
538156c7ddSOleksij Rempel  * Default time window (in milliseconds) following a critical under-voltage
548156c7ddSOleksij Rempel  * event during which less critical actions can be safely carried out by the
558156c7ddSOleksij Rempel  * system.
568156c7ddSOleksij Rempel  */
578156c7ddSOleksij Rempel #define REGULATOR_DEF_UV_LESS_CRITICAL_WINDOW_MS	10
588156c7ddSOleksij Rempel 
59670666b9SLaxman Dewangan /* Regulator active discharge flags */
60670666b9SLaxman Dewangan enum regulator_active_discharge {
61670666b9SLaxman Dewangan 	REGULATOR_ACTIVE_DISCHARGE_DEFAULT,
62670666b9SLaxman Dewangan 	REGULATOR_ACTIVE_DISCHARGE_DISABLE,
63670666b9SLaxman Dewangan 	REGULATOR_ACTIVE_DISCHARGE_ENABLE,
64670666b9SLaxman Dewangan };
65670666b9SLaxman Dewangan 
664c1184e8SLiam Girdwood /**
672e7e65ceSWolfram Sang  * struct regulator_state - regulator state during low power system states
684c1184e8SLiam Girdwood  *
69638f85c5SMark Brown  * This describes a regulators state during a system wide low power
70638f85c5SMark Brown  * state.  One of enabled or disabled must be set for the
71638f85c5SMark Brown  * configuration to be applied.
72c8e7e464SMark Brown  *
73f7efad10SChunyan Zhang  * @uV: Default operating voltage during suspend, it can be adjusted
74f7efad10SChunyan Zhang  *	among <min_uV, max_uV>.
75f7efad10SChunyan Zhang  * @min_uV: Minimum suspend voltage may be set.
76f7efad10SChunyan Zhang  * @max_uV: Maximum suspend voltage may be set.
77c8e7e464SMark Brown  * @mode: Operating mode during suspend.
7872069f99SChunyan Zhang  * @enabled: operations during suspend.
7972069f99SChunyan Zhang  *	     - DO_NOTHING_IN_SUSPEND
8072069f99SChunyan Zhang  *	     - DISABLE_IN_SUSPEND
8172069f99SChunyan Zhang  *	     - ENABLE_IN_SUSPEND
82f7efad10SChunyan Zhang  * @changeable: Is this state can be switched between enabled/disabled,
834c1184e8SLiam Girdwood  */
844c1184e8SLiam Girdwood struct regulator_state {
85f7efad10SChunyan Zhang 	int uV;
86f7efad10SChunyan Zhang 	int min_uV;
87f7efad10SChunyan Zhang 	int max_uV;
88f7efad10SChunyan Zhang 	unsigned int mode;
8972069f99SChunyan Zhang 	int enabled;
90f7efad10SChunyan Zhang 	bool changeable;
914c1184e8SLiam Girdwood };
924c1184e8SLiam Girdwood 
9389a6a5e5SMatti Vaittinen #define REGULATOR_NOTIF_LIMIT_DISABLE -1
9489a6a5e5SMatti Vaittinen #define REGULATOR_NOTIF_LIMIT_ENABLE -2
9589a6a5e5SMatti Vaittinen struct notification_limit {
9689a6a5e5SMatti Vaittinen 	int prot;
9789a6a5e5SMatti Vaittinen 	int err;
9889a6a5e5SMatti Vaittinen 	int warn;
9989a6a5e5SMatti Vaittinen };
10089a6a5e5SMatti Vaittinen 
1014c1184e8SLiam Girdwood /**
1024c1184e8SLiam Girdwood  * struct regulation_constraints - regulator operating constraints.
1034c1184e8SLiam Girdwood  *
1044c1184e8SLiam Girdwood  * This struct describes regulator and board/machine specific constraints.
105c8e7e464SMark Brown  *
106c8e7e464SMark Brown  * @name: Descriptive name for the constraints, used for display purposes.
107c8e7e464SMark Brown  *
108c8e7e464SMark Brown  * @min_uV: Smallest voltage consumers may set.
109c8e7e464SMark Brown  * @max_uV: Largest voltage consumers may set.
110bf5892a8SMark Brown  * @uV_offset: Offset applied to voltages from consumer to compensate for
111bf5892a8SMark Brown  *             voltage drops.
112c8e7e464SMark Brown  *
11315c08f66SAxel Lin  * @min_uA: Smallest current consumers may set.
114c8e7e464SMark Brown  * @max_uA: Largest current consumers may set.
11536e4f839SStephen Boyd  * @ilim_uA: Maximum input current.
116*42d7c87bSKory Maincent  * @pw_budget_mW: Power budget for the regulator in mW.
11722a10bcaSStephen Boyd  * @system_load: Load that isn't captured by any consumer requests.
118c8e7e464SMark Brown  *
11989a6a5e5SMatti Vaittinen  * @over_curr_limits:		Limits for acting on over current.
12089a6a5e5SMatti Vaittinen  * @over_voltage_limits:	Limits for acting on over voltage.
12189a6a5e5SMatti Vaittinen  * @under_voltage_limits:	Limits for acting on under voltage.
12289a6a5e5SMatti Vaittinen  * @temp_limits:		Limits for acting on over temperature.
12378bbb7c3SRandy Dunlap  *
124a085a31aSMaciej Purski  * @max_spread: Max possible spread between coupled regulators
125a98bcaa9SColton Lewis  * @max_uV_step: Max possible step change in voltage
126c8e7e464SMark Brown  * @valid_modes_mask: Mask of modes which may be configured by consumers.
127c8e7e464SMark Brown  * @valid_ops_mask: Operations which may be performed by consumers.
128c8e7e464SMark Brown  *
129c8e7e464SMark Brown  * @always_on: Set if the regulator should never be disabled.
130c8e7e464SMark Brown  * @boot_on: Set if the regulator is enabled when the system is initially
131cacf90f2SMark Brown  *           started.  If the regulator is not enabled by the hardware or
132cacf90f2SMark Brown  *           bootloader then it will be enabled when the constraints are
133cacf90f2SMark Brown  *           applied.
134c8e7e464SMark Brown  * @apply_uV: Apply the voltage constraint when initialising.
135a8dbfeedSRandy Dunlap  * @ramp_disable: Disable ramp delay when initialising or when setting voltage.
136c751ad0dSStephen Boyd  * @soft_start: Enable soft start so that voltage ramps slowly.
13723c779b9SStephen Boyd  * @pull_down: Enable pull down when regulator is disabled.
1388156c7ddSOleksij Rempel  * @system_critical: Set if the regulator is critical to system stability or
1398156c7ddSOleksij Rempel  *                   functionality.
140abf2f825SLuis de Bethencourt  * @over_current_protection: Auto disable on over current event.
141c8e7e464SMark Brown  *
14289a6a5e5SMatti Vaittinen  * @over_current_detection: Configure over current limits.
14389a6a5e5SMatti Vaittinen  * @over_voltage_detection: Configure over voltage limits.
14489a6a5e5SMatti Vaittinen  * @under_voltage_detection: Configure under voltage limits.
14589a6a5e5SMatti Vaittinen  * @over_temp_detection: Configure over temperature limits.
14689a6a5e5SMatti Vaittinen  *
147c8e7e464SMark Brown  * @input_uV: Input voltage for regulator when supplied by another regulator.
148c8e7e464SMark Brown  *
149c8e7e464SMark Brown  * @state_disk: State for regulator when system is suspended in disk mode.
150c8e7e464SMark Brown  * @state_mem: State for regulator when system is suspended in mem mode.
151c8e7e464SMark Brown  * @state_standby: State for regulator when system is suspended in standby
152c8e7e464SMark Brown  *                 mode.
153c8e7e464SMark Brown  * @initial_state: Suspend state to set by default.
154a308466cSMark Brown  * @initial_mode: Mode to set at startup.
155ea38d13fSAxel Lin  * @ramp_delay: Time to settle down after voltage change (unit: uV/us)
156d6c1dc3fSLaxman Dewangan  * @settling_time: Time to settle down after voltage change when voltage
157d6c1dc3fSLaxman Dewangan  *		   change is non-linear (unit: microseconds).
1583ffad468SMatthias Kaehlcke  * @settling_time_up: Time to settle down after voltage increase when voltage
1593ffad468SMatthias Kaehlcke  *		      change is non-linear (unit: microseconds).
1603ffad468SMatthias Kaehlcke  * @settling_time_down : Time to settle down after voltage decrease when
1613ffad468SMatthias Kaehlcke  *			 voltage change is non-linear (unit: microseconds).
162670666b9SLaxman Dewangan  * @active_discharge: Enable/disable active discharge. The enum
163670666b9SLaxman Dewangan  *		      regulator_active_discharge values are used for
164670666b9SLaxman Dewangan  *		      initialisation.
16500c877c6SLaxman Dewangan  * @enable_time: Turn-on time of the rails (unit: microseconds)
1661e22152aSOleksij Rempel  * @uv_less_critical_window_ms: Specifies the time window (in milliseconds)
1671e22152aSOleksij Rempel  *                              following a critical under-voltage (UV) event
1681e22152aSOleksij Rempel  *                              during which less critical actions can be
1691e22152aSOleksij Rempel  *                              safely carried out by the system (for example
1701e22152aSOleksij Rempel  *                              logging). After this time window more critical
1711e22152aSOleksij Rempel  *                              actions should be done (for example prevent
1721e22152aSOleksij Rempel  *                              HW damage).
1734c1184e8SLiam Girdwood  */
1744c1184e8SLiam Girdwood struct regulation_constraints {
1754c1184e8SLiam Girdwood 
1760151546fSMark Brown 	const char *name;
1774c1184e8SLiam Girdwood 
1784c1184e8SLiam Girdwood 	/* voltage output range (inclusive) - for voltage control */
1794c1184e8SLiam Girdwood 	int min_uV;
1804c1184e8SLiam Girdwood 	int max_uV;
1814c1184e8SLiam Girdwood 
182bf5892a8SMark Brown 	int uV_offset;
183bf5892a8SMark Brown 
1844c1184e8SLiam Girdwood 	/* current output range (inclusive) - for current control */
1854c1184e8SLiam Girdwood 	int min_uA;
1864c1184e8SLiam Girdwood 	int max_uA;
18736e4f839SStephen Boyd 	int ilim_uA;
1884c1184e8SLiam Girdwood 
189*42d7c87bSKory Maincent 	int pw_budget_mW;
19022a10bcaSStephen Boyd 	int system_load;
19122a10bcaSStephen Boyd 
192a085a31aSMaciej Purski 	/* used for coupled regulators */
193d8ca7d18SDmitry Osipenko 	u32 *max_spread;
194a085a31aSMaciej Purski 
19585254bcfSDmitry Osipenko 	/* used for changing voltage in steps */
19685254bcfSDmitry Osipenko 	int max_uV_step;
19785254bcfSDmitry Osipenko 
1984c1184e8SLiam Girdwood 	/* valid regulator operating modes for this machine */
1994c1184e8SLiam Girdwood 	unsigned int valid_modes_mask;
2004c1184e8SLiam Girdwood 
2014c1184e8SLiam Girdwood 	/* valid operations for regulator on this machine */
2024c1184e8SLiam Girdwood 	unsigned int valid_ops_mask;
2034c1184e8SLiam Girdwood 
2044c1184e8SLiam Girdwood 	/* regulator input voltage - only if supply is another regulator */
2054c1184e8SLiam Girdwood 	int input_uV;
2064c1184e8SLiam Girdwood 
2074c1184e8SLiam Girdwood 	/* regulator suspend states for global PMIC STANDBY/HIBERNATE */
2084c1184e8SLiam Girdwood 	struct regulator_state state_disk;
2094c1184e8SLiam Girdwood 	struct regulator_state state_mem;
2104c1184e8SLiam Girdwood 	struct regulator_state state_standby;
21189a6a5e5SMatti Vaittinen 	struct notification_limit over_curr_limits;
21289a6a5e5SMatti Vaittinen 	struct notification_limit over_voltage_limits;
21389a6a5e5SMatti Vaittinen 	struct notification_limit under_voltage_limits;
21489a6a5e5SMatti Vaittinen 	struct notification_limit temp_limits;
2154c1184e8SLiam Girdwood 	suspend_state_t initial_state; /* suspend state to set at init */
2164c1184e8SLiam Girdwood 
217a308466cSMark Brown 	/* mode to set on startup */
218a308466cSMark Brown 	unsigned int initial_mode;
219a308466cSMark Brown 
2206f0b2c69SYadwinder Singh Brar 	unsigned int ramp_delay;
221d6c1dc3fSLaxman Dewangan 	unsigned int settling_time;
2223ffad468SMatthias Kaehlcke 	unsigned int settling_time_up;
2233ffad468SMatthias Kaehlcke 	unsigned int settling_time_down;
22400c877c6SLaxman Dewangan 	unsigned int enable_time;
2251e22152aSOleksij Rempel 	unsigned int uv_less_critical_window_ms;
2266f0b2c69SYadwinder Singh Brar 
227670666b9SLaxman Dewangan 	unsigned int active_discharge;
228670666b9SLaxman Dewangan 
2292e7e65ceSWolfram Sang 	/* constraint flags */
2304c1184e8SLiam Girdwood 	unsigned always_on:1;	/* regulator never off when system is on */
2314c1184e8SLiam Girdwood 	unsigned boot_on:1;	/* bootloader/firmware enabled regulator */
2322e7e65ceSWolfram Sang 	unsigned apply_uV:1;	/* apply uV constraint if min == max */
2331653ccf4SYadwinder Singh Brar 	unsigned ramp_disable:1; /* disable ramp delay */
23457f66b78SStephen Boyd 	unsigned soft_start:1;	/* ramp voltage slowly */
23523c779b9SStephen Boyd 	unsigned pull_down:1;	/* pull down resistor when regulator off */
2368156c7ddSOleksij Rempel 	unsigned system_critical:1;	/* critical to system stability */
2373a003baeSStephen Boyd 	unsigned over_current_protection:1; /* auto disable on over current */
23889a6a5e5SMatti Vaittinen 	unsigned over_current_detection:1; /* notify on over current */
23989a6a5e5SMatti Vaittinen 	unsigned over_voltage_detection:1; /* notify on over voltage */
24089a6a5e5SMatti Vaittinen 	unsigned under_voltage_detection:1; /* notify on under voltage */
24189a6a5e5SMatti Vaittinen 	unsigned over_temp_detection:1; /* notify on over temperature */
2424c1184e8SLiam Girdwood };
2434c1184e8SLiam Girdwood 
244a5766f11SLiam Girdwood /**
245a5766f11SLiam Girdwood  * struct regulator_consumer_supply - supply -> device mapping
246a5766f11SLiam Girdwood  *
24715c08f66SAxel Lin  * This maps a supply name to a device. Use of dev_name allows support for
24815c08f66SAxel Lin  * buses which make struct device available late such as I2C.
249c8e7e464SMark Brown  *
25040f9244fSMark Brown  * @dev_name: Result of dev_name() for the consumer.
251c8e7e464SMark Brown  * @supply: Name for the supply.
252a5766f11SLiam Girdwood  */
253a5766f11SLiam Girdwood struct regulator_consumer_supply {
25440f9244fSMark Brown 	const char *dev_name;   /* dev_name() for consumer */
255a5766f11SLiam Girdwood 	const char *supply;	/* consumer supply - e.g. "vcc" */
256a5766f11SLiam Girdwood };
2574c1184e8SLiam Girdwood 
258ed654324Sroald /* Initialize struct regulator_consumer_supply */
259ed654324Sroald #define REGULATOR_SUPPLY(_name, _dev_name)			\
260ed654324Sroald {								\
261ed654324Sroald 	.supply		= _name,				\
262ed654324Sroald 	.dev_name	= _dev_name,				\
263ed654324Sroald }
264ed654324Sroald 
265a5766f11SLiam Girdwood /**
266a5766f11SLiam Girdwood  * struct regulator_init_data - regulator platform initialisation data.
267a5766f11SLiam Girdwood  *
268a5766f11SLiam Girdwood  * Initialisation constraints, our supply and consumers supplies.
269c8e7e464SMark Brown  *
2700178f3e2SMark Brown  * @supply_regulator: Parent regulator.  Specified using the regulator name
2710178f3e2SMark Brown  *                    as it appears in the name field in sysfs, which can
2720178f3e2SMark Brown  *                    be explicitly set using the constraints field 'name'.
273c8e7e464SMark Brown  *
274c8e7e464SMark Brown  * @constraints: Constraints.  These must be specified for the regulator to
275c8e7e464SMark Brown  *               be usable.
276c8e7e464SMark Brown  * @num_consumer_supplies: Number of consumer device supplies.
277c8e7e464SMark Brown  * @consumer_supplies: Consumer device supply configuration.
278c8e7e464SMark Brown  * @driver_data: Data passed to regulator_init.
279a5766f11SLiam Girdwood  */
280a5766f11SLiam Girdwood struct regulator_init_data {
2810178f3e2SMark Brown 	const char *supply_regulator;        /* or NULL for system supply */
2824c1184e8SLiam Girdwood 
283a5766f11SLiam Girdwood 	struct regulation_constraints constraints;
2844c1184e8SLiam Girdwood 
285a5766f11SLiam Girdwood 	int num_consumer_supplies;
286a5766f11SLiam Girdwood 	struct regulator_consumer_supply *consumer_supplies;
287a5766f11SLiam Girdwood 
288602ff58aSJerome Brunet 	/* optional regulator machine specific data */
289a5766f11SLiam Girdwood 	void *driver_data;	/* core does not touch this */
290a5766f11SLiam Girdwood };
2914c1184e8SLiam Girdwood 
2929c19bc04SMark Brown #ifdef CONFIG_REGULATOR
293ca725561SMark Brown void regulator_has_full_constraints(void);
2949c19bc04SMark Brown #else
regulator_has_full_constraints(void)2959c19bc04SMark Brown static inline void regulator_has_full_constraints(void)
2969c19bc04SMark Brown {
2979c19bc04SMark Brown }
298aa27bbc6SChunyan Zhang #endif
299aa27bbc6SChunyan Zhang 
regulator_suspend_prepare(suspend_state_t state)30050910276SKrzysztof Kozlowski static inline int regulator_suspend_prepare(suspend_state_t state)
30150910276SKrzysztof Kozlowski {
30250910276SKrzysztof Kozlowski 	return 0;
30350910276SKrzysztof Kozlowski }
regulator_suspend_finish(void)30450910276SKrzysztof Kozlowski static inline int regulator_suspend_finish(void)
30550910276SKrzysztof Kozlowski {
30650910276SKrzysztof Kozlowski 	return 0;
30750910276SKrzysztof Kozlowski }
308ca725561SMark Brown 
3094c1184e8SLiam Girdwood #endif
310