1 // SPDX-License-Identifier: GPL-2.0
3 * System Control and Management Interface (SCMI) Power Protocol
5 * Copyright (C) 2018 ARM Ltd.
10 enum scmi_power_protocol_cmd
{
11 POWER_DOMAIN_ATTRIBUTES
= 0x3,
12 POWER_STATE_SET
= 0x4,
13 POWER_STATE_GET
= 0x5,
14 POWER_STATE_NOTIFY
= 0x6,
17 struct scmi_msg_resp_power_attributes
{
20 __le32 stats_addr_low
;
21 __le32 stats_addr_high
;
25 struct scmi_msg_resp_power_domain_attributes
{
27 #define SUPPORTS_STATE_SET_NOTIFY(x) ((x) & BIT(31))
28 #define SUPPORTS_STATE_SET_ASYNC(x) ((x) & BIT(30))
29 #define SUPPORTS_STATE_SET_SYNC(x) ((x) & BIT(29))
30 u8 name
[SCMI_MAX_STR_SIZE
];
33 struct scmi_power_set_state
{
35 #define STATE_SET_ASYNC BIT(0)
40 struct scmi_power_state_notify
{
45 struct power_dom_info
{
48 bool state_set_notify
;
49 char name
[SCMI_MAX_STR_SIZE
];
52 struct scmi_power_info
{
56 struct power_dom_info
*dom_info
;
59 static int scmi_power_attributes_get(const struct scmi_handle
*handle
,
60 struct scmi_power_info
*pi
)
64 struct scmi_msg_resp_power_attributes
*attr
;
66 ret
= scmi_xfer_get_init(handle
, PROTOCOL_ATTRIBUTES
,
67 SCMI_PROTOCOL_POWER
, 0, sizeof(*attr
), &t
);
73 ret
= scmi_do_xfer(handle
, t
);
75 pi
->num_domains
= le16_to_cpu(attr
->num_domains
);
76 pi
->stats_addr
= le32_to_cpu(attr
->stats_addr_low
) |
77 (u64
)le32_to_cpu(attr
->stats_addr_high
) << 32;
78 pi
->stats_size
= le32_to_cpu(attr
->stats_size
);
81 scmi_xfer_put(handle
, t
);
86 scmi_power_domain_attributes_get(const struct scmi_handle
*handle
, u32 domain
,
87 struct power_dom_info
*dom_info
)
91 struct scmi_msg_resp_power_domain_attributes
*attr
;
93 ret
= scmi_xfer_get_init(handle
, POWER_DOMAIN_ATTRIBUTES
,
94 SCMI_PROTOCOL_POWER
, sizeof(domain
),
99 *(__le32
*)t
->tx
.buf
= cpu_to_le32(domain
);
102 ret
= scmi_do_xfer(handle
, t
);
104 u32 flags
= le32_to_cpu(attr
->flags
);
106 dom_info
->state_set_notify
= SUPPORTS_STATE_SET_NOTIFY(flags
);
107 dom_info
->state_set_async
= SUPPORTS_STATE_SET_ASYNC(flags
);
108 dom_info
->state_set_sync
= SUPPORTS_STATE_SET_SYNC(flags
);
109 memcpy(dom_info
->name
, attr
->name
, SCMI_MAX_STR_SIZE
);
112 scmi_xfer_put(handle
, t
);
117 scmi_power_state_set(const struct scmi_handle
*handle
, u32 domain
, u32 state
)
121 struct scmi_power_set_state
*st
;
123 ret
= scmi_xfer_get_init(handle
, POWER_STATE_SET
, SCMI_PROTOCOL_POWER
,
129 st
->flags
= cpu_to_le32(0);
130 st
->domain
= cpu_to_le32(domain
);
131 st
->state
= cpu_to_le32(state
);
133 ret
= scmi_do_xfer(handle
, t
);
135 scmi_xfer_put(handle
, t
);
140 scmi_power_state_get(const struct scmi_handle
*handle
, u32 domain
, u32
*state
)
145 ret
= scmi_xfer_get_init(handle
, POWER_STATE_GET
, SCMI_PROTOCOL_POWER
,
146 sizeof(u32
), sizeof(u32
), &t
);
150 *(__le32
*)t
->tx
.buf
= cpu_to_le32(domain
);
152 ret
= scmi_do_xfer(handle
, t
);
154 *state
= le32_to_cpu(*(__le32
*)t
->rx
.buf
);
156 scmi_xfer_put(handle
, t
);
160 static int scmi_power_num_domains_get(const struct scmi_handle
*handle
)
162 struct scmi_power_info
*pi
= handle
->power_priv
;
164 return pi
->num_domains
;
167 static char *scmi_power_name_get(const struct scmi_handle
*handle
, u32 domain
)
169 struct scmi_power_info
*pi
= handle
->power_priv
;
170 struct power_dom_info
*dom
= pi
->dom_info
+ domain
;
175 static struct scmi_power_ops power_ops
= {
176 .num_domains_get
= scmi_power_num_domains_get
,
177 .name_get
= scmi_power_name_get
,
178 .state_set
= scmi_power_state_set
,
179 .state_get
= scmi_power_state_get
,
182 static int scmi_power_protocol_init(struct scmi_handle
*handle
)
186 struct scmi_power_info
*pinfo
;
188 scmi_version_get(handle
, SCMI_PROTOCOL_POWER
, &version
);
190 dev_dbg(handle
->dev
, "Power Version %d.%d\n",
191 PROTOCOL_REV_MAJOR(version
), PROTOCOL_REV_MINOR(version
));
193 pinfo
= devm_kzalloc(handle
->dev
, sizeof(*pinfo
), GFP_KERNEL
);
197 scmi_power_attributes_get(handle
, pinfo
);
199 pinfo
->dom_info
= devm_kcalloc(handle
->dev
, pinfo
->num_domains
,
200 sizeof(*pinfo
->dom_info
), GFP_KERNEL
);
201 if (!pinfo
->dom_info
)
204 for (domain
= 0; domain
< pinfo
->num_domains
; domain
++) {
205 struct power_dom_info
*dom
= pinfo
->dom_info
+ domain
;
207 scmi_power_domain_attributes_get(handle
, domain
, dom
);
210 handle
->power_ops
= &power_ops
;
211 handle
->power_priv
= pinfo
;
216 static int __init
scmi_power_init(void)
218 return scmi_protocol_register(SCMI_PROTOCOL_POWER
,
219 &scmi_power_protocol_init
);
221 subsys_initcall(scmi_power_init
);