blob: 7fd2c5e365b54889a156d0f0b969fae490ac41a7 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001/* SPDX-License-Identifier: GPL-2.0 */
Stephen Warren92c67fa2016-07-13 13:45:31 -06002/*
3 * Copyright (c) 2016, NVIDIA CORPORATION.
Stephen Warren92c67fa2016-07-13 13:45:31 -06004 */
5
6#ifndef _POWER_DOMAIN_H
7#define _POWER_DOMAIN_H
8
Max Krummenacher0e226eb2024-01-18 19:10:47 +01009#include <linux/errno.h>
10
Stephen Warren92c67fa2016-07-13 13:45:31 -060011/**
12 * A power domain is a portion of an SoC or chip that is powered by a
13 * switchable source of power. In many cases, software has control over the
14 * power domain, and can turn the power source on or off. This is typically
15 * done to save power by powering off unused devices, or to enable software
16 * sequencing of initial powerup at boot. This API provides a means for
17 * drivers to turn power domains on and off.
18 *
19 * A driver that implements UCLASS_POWER_DOMAIN is a power domain controller or
20 * provider. A controller will often implement multiple separate power domains,
21 * since the hardware it manages often has this capability.
22 * power-domain-uclass.h describes the interface which power domain controllers
23 * must implement.
24 *
25 * Depending on the power domain controller hardware, changing the state of a
26 * power domain may require performing related operations on other resources.
27 * For example, some power domains may require certain clocks to be enabled
28 * whenever the power domain is powered on, or during the time when the power
29 * domain is transitioning state. These details are implementation-specific
30 * and should ideally be encapsulated entirely within the provider driver, or
31 * configured through mechanisms (e.g. device tree) that do not require client
32 * drivers to provide extra configuration information.
33 *
34 * Power domain consumers/clients are the drivers for HW modules within the
35 * power domain. This header file describes the API used by those drivers.
36 *
37 * In many cases, a single complex IO controller (e.g. a PCIe controller) will
38 * be the sole logic contained within a power domain. In such cases, it is
39 * logical for the relevant device driver to directly control that power
40 * domain. In other cases, multiple controllers, each with their own driver,
41 * may be contained in a single power domain. Any logic require to co-ordinate
42 * between drivers for these multiple controllers is beyond the scope of this
43 * API at present. Equally, this API does not define or implement any policy
44 * by which power domains are managed.
45 */
46
47struct udevice;
48
49/**
50 * struct power_domain - A handle to (allowing control of) a single power domain.
51 *
52 * Clients provide storage for power domain handles. The content of the
53 * structure is managed solely by the power domain API and power domain
54 * drivers. A power domain struct is initialized by "get"ing the power domain
55 * struct. The power domain struct is passed to all other power domain APIs to
56 * identify which power domain to operate upon.
57 *
58 * @dev: The device which implements the power domain.
59 * @id: The power domain ID within the provider.
Lokesh Vutla987ae662019-06-07 19:24:44 +053060 * @priv: Private data corresponding to each power domain.
Stephen Warren92c67fa2016-07-13 13:45:31 -060061 */
62struct power_domain {
63 struct udevice *dev;
Stephen Warren92c67fa2016-07-13 13:45:31 -060064 unsigned long id;
Lokesh Vutla987ae662019-06-07 19:24:44 +053065 void *priv;
Stephen Warren92c67fa2016-07-13 13:45:31 -060066};
67
68/**
Miquel Raynalc1f527e2025-04-25 08:49:32 +020069 * struct power_domain_plat - Per device accessible structure
70 * @subdomains: Number of subdomains covered by this device, required
71 * for refcounting
72 */
73struct power_domain_plat {
74 int subdomains;
75};
76
77/**
Stephen Warren92c67fa2016-07-13 13:45:31 -060078 * power_domain_get - Get/request the power domain for a device.
79 *
80 * This looks up and requests a power domain. Each device is assumed to have
81 * a single (or, at least one) power domain associated with it somehow, and
82 * that domain, or the first/default domain. The mapping of client device to
83 * provider power domain may be via device-tree properties, board-provided
84 * mapping tables, or some other mechanism.
85 *
86 * @dev: The client device.
87 * @power_domain A pointer to a power domain struct to initialize.
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +010088 * Return: 0 if OK, or a negative error code.
Stephen Warren92c67fa2016-07-13 13:45:31 -060089 */
Peng Fan57965702018-07-27 10:20:36 +080090#if CONFIG_IS_ENABLED(POWER_DOMAIN)
Stephen Warren92c67fa2016-07-13 13:45:31 -060091int power_domain_get(struct udevice *dev, struct power_domain *power_domain);
Peng Fan57965702018-07-27 10:20:36 +080092#else
93static inline
94int power_domain_get(struct udevice *dev, struct power_domain *power_domain)
95{
96 return -ENOSYS;
97}
98#endif
Stephen Warren92c67fa2016-07-13 13:45:31 -060099
100/**
Lokesh Vutlac411c672018-08-27 15:57:44 +0530101 * power_domain_get_by_index - Get the indexed power domain for a device.
102 *
103 * @dev: The client device.
104 * @power_domain: A pointer to a power domain struct to initialize.
105 * @index: Power domain index to be powered on.
106 *
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100107 * Return: 0 if OK, or a negative error code.
Lokesh Vutlac411c672018-08-27 15:57:44 +0530108 */
109#if CONFIG_IS_ENABLED(POWER_DOMAIN)
110int power_domain_get_by_index(struct udevice *dev,
111 struct power_domain *power_domain, int index);
112#else
113static inline
114int power_domain_get_by_index(struct udevice *dev,
115 struct power_domain *power_domain, int index)
116{
117 return -ENOSYS;
118}
119#endif
120
121/**
Marek Vasutfb4e8e92022-04-13 00:42:52 +0200122 * power_domain_get_by_name - Get the named power domain for a device.
123 *
124 * @dev: The client device.
125 * @power_domain: A pointer to a power domain struct to initialize.
126 * @name: Power domain name to be powered on.
127 *
128 * Return: 0 if OK, or a negative error code.
129 */
130#if CONFIG_IS_ENABLED(POWER_DOMAIN)
131int power_domain_get_by_name(struct udevice *dev,
132 struct power_domain *power_domain, const char *name);
133#else
134static inline
135int power_domain_get_by_name(struct udevice *dev,
136 struct power_domain *power_domain, const char *name)
137{
138 return -ENOSYS;
139}
140#endif
141
142/**
Stephen Warren92c67fa2016-07-13 13:45:31 -0600143 * power_domain_free - Free a previously requested power domain.
144 *
145 * @power_domain: A power domain struct that was previously successfully
146 * requested by power_domain_get().
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100147 * Return: 0 if OK, or a negative error code.
Stephen Warren92c67fa2016-07-13 13:45:31 -0600148 */
Peng Fan57965702018-07-27 10:20:36 +0800149#if CONFIG_IS_ENABLED(POWER_DOMAIN)
Stephen Warren92c67fa2016-07-13 13:45:31 -0600150int power_domain_free(struct power_domain *power_domain);
Peng Fan57965702018-07-27 10:20:36 +0800151#else
152static inline int power_domain_free(struct power_domain *power_domain)
153{
154 return -ENOSYS;
155}
156#endif
Stephen Warren92c67fa2016-07-13 13:45:31 -0600157
158/**
Miquel Raynalc1f527e2025-04-25 08:49:32 +0200159 * power_domain_on_lowlevel - Enable power to a power domain (with refcounting)
Stephen Warren92c67fa2016-07-13 13:45:31 -0600160 *
161 * @power_domain: A power domain struct that was previously successfully
162 * requested by power_domain_get().
Miquel Raynalc1f527e2025-04-25 08:49:32 +0200163 * Return: 0 if the transition has been performed correctly,
164 * -EALREADY if the domain is already on,
165 * a negative error code otherwise.
Stephen Warren92c67fa2016-07-13 13:45:31 -0600166 */
Peng Fan57965702018-07-27 10:20:36 +0800167#if CONFIG_IS_ENABLED(POWER_DOMAIN)
Miquel Raynalc1f527e2025-04-25 08:49:32 +0200168int power_domain_on_lowlevel(struct power_domain *power_domain);
Peng Fan57965702018-07-27 10:20:36 +0800169#else
Miquel Raynalc1f527e2025-04-25 08:49:32 +0200170static inline int power_domain_on_lowlevel(struct power_domain *power_domain)
Peng Fan57965702018-07-27 10:20:36 +0800171{
172 return -ENOSYS;
173}
174#endif
Stephen Warren92c67fa2016-07-13 13:45:31 -0600175
176/**
Miquel Raynalc1f527e2025-04-25 08:49:32 +0200177 * power_domain_on - Enable power to a power domain (ignores the actual state
178 * of the power domain)
Stephen Warren92c67fa2016-07-13 13:45:31 -0600179 *
180 * @power_domain: A power domain struct that was previously successfully
181 * requested by power_domain_get().
Miquel Raynalc1f527e2025-04-25 08:49:32 +0200182 * Return: a negative error code upon error during the transition, 0 otherwise.
183 */
184static inline int power_domain_on(struct power_domain *power_domain)
185{
186 int ret;
187
188 ret = power_domain_on_lowlevel(power_domain);
189 if (ret == -EALREADY)
190 ret = 0;
191
192 return ret;
193}
194
195/**
196 * power_domain_off_lowlevel - Disable power to a power domain (with refcounting)
197 *
198 * @power_domain: A power domain struct that was previously successfully
199 * requested by power_domain_get().
200 * Return: 0 if the transition has been performed correctly,
201 * -EALREADY if the domain is already off,
202 * -EBUSY if another device is keeping the domain on (but the refcounter
203 * is decremented),
204 * a negative error code otherwise.
Stephen Warren92c67fa2016-07-13 13:45:31 -0600205 */
Peng Fan57965702018-07-27 10:20:36 +0800206#if CONFIG_IS_ENABLED(POWER_DOMAIN)
Miquel Raynalc1f527e2025-04-25 08:49:32 +0200207int power_domain_off_lowlevel(struct power_domain *power_domain);
Peng Fan57965702018-07-27 10:20:36 +0800208#else
Miquel Raynalc1f527e2025-04-25 08:49:32 +0200209static inline int power_domain_off_lowlevel(struct power_domain *power_domain)
Peng Fan57965702018-07-27 10:20:36 +0800210{
211 return -ENOSYS;
212}
213#endif
Stephen Warren92c67fa2016-07-13 13:45:31 -0600214
Peng Fandb513e92019-09-17 09:29:19 +0000215/**
Miquel Raynalc1f527e2025-04-25 08:49:32 +0200216 * power_domain_off - Disable power to a power domain (ignores the actual state
217 * of the power domain)
218 *
219 * @power_domain: A power domain struct that was previously successfully
220 * requested by power_domain_get().
221 * Return: a negative error code upon error during the transition, 0 otherwise.
222 */
223static inline int power_domain_off(struct power_domain *power_domain)
224{
225 int ret;
226
227 ret = power_domain_off_lowlevel(power_domain);
228 if (ret == -EALREADY || ret == -EBUSY)
229 ret = 0;
230
231 return ret;
232}
233
234/**
Peng Fandb513e92019-09-17 09:29:19 +0000235 * dev_power_domain_on - Enable power domains for a device .
236 *
237 * @dev: The client device.
238 *
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100239 * Return: 0 if OK, or a negative error code.
Peng Fandb513e92019-09-17 09:29:19 +0000240 */
Simon Glass3580f6d2021-08-07 07:24:03 -0600241#if CONFIG_IS_ENABLED(OF_REAL) && CONFIG_IS_ENABLED(POWER_DOMAIN)
Peng Fandb513e92019-09-17 09:29:19 +0000242int dev_power_domain_on(struct udevice *dev);
243#else
244static inline int dev_power_domain_on(struct udevice *dev)
245{
246 return 0;
247}
248#endif
249
Lokesh Vutla589eeb82019-09-27 13:48:14 +0530250/**
251 * dev_power_domain_off - Disable power domains for a device .
252 *
253 * @dev: The client device.
254 *
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100255 * Return: 0 if OK, or a negative error code.
Lokesh Vutla589eeb82019-09-27 13:48:14 +0530256 */
Simon Glass3580f6d2021-08-07 07:24:03 -0600257#if CONFIG_IS_ENABLED(OF_REAL) && CONFIG_IS_ENABLED(POWER_DOMAIN)
Lokesh Vutla589eeb82019-09-27 13:48:14 +0530258int dev_power_domain_off(struct udevice *dev);
259#else
260static inline int dev_power_domain_off(struct udevice *dev)
261{
262 return 0;
263}
264#endif
265
Stephen Warren92c67fa2016-07-13 13:45:31 -0600266#endif