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
{
57 struct power_dom_info
*dom_info
;
60 static int scmi_power_attributes_get(const struct scmi_handle
*handle
,
61 struct scmi_power_info
*pi
)
65 struct scmi_msg_resp_power_attributes
*attr
;
67 ret
= scmi_xfer_get_init(handle
, PROTOCOL_ATTRIBUTES
,
68 SCMI_PROTOCOL_POWER
, 0, sizeof(*attr
), &t
);
74 ret
= scmi_do_xfer(handle
, t
);
76 pi
->num_domains
= le16_to_cpu(attr
->num_domains
);
77 pi
->stats_addr
= le32_to_cpu(attr
->stats_addr_low
) |
78 (u64
)le32_to_cpu(attr
->stats_addr_high
) << 32;
79 pi
->stats_size
= le32_to_cpu(attr
->stats_size
);
82 scmi_xfer_put(handle
, t
);
87 scmi_power_domain_attributes_get(const struct scmi_handle
*handle
, u32 domain
,
88 struct power_dom_info
*dom_info
)
92 struct scmi_msg_resp_power_domain_attributes
*attr
;
94 ret
= scmi_xfer_get_init(handle
, POWER_DOMAIN_ATTRIBUTES
,
95 SCMI_PROTOCOL_POWER
, sizeof(domain
),
100 put_unaligned_le32(domain
, t
->tx
.buf
);
103 ret
= scmi_do_xfer(handle
, t
);
105 u32 flags
= le32_to_cpu(attr
->flags
);
107 dom_info
->state_set_notify
= SUPPORTS_STATE_SET_NOTIFY(flags
);
108 dom_info
->state_set_async
= SUPPORTS_STATE_SET_ASYNC(flags
);
109 dom_info
->state_set_sync
= SUPPORTS_STATE_SET_SYNC(flags
);
110 strlcpy(dom_info
->name
, attr
->name
, SCMI_MAX_STR_SIZE
);
113 scmi_xfer_put(handle
, t
);
118 scmi_power_state_set(const struct scmi_handle
*handle
, u32 domain
, u32 state
)
122 struct scmi_power_set_state
*st
;
124 ret
= scmi_xfer_get_init(handle
, POWER_STATE_SET
, SCMI_PROTOCOL_POWER
,
130 st
->flags
= cpu_to_le32(0);
131 st
->domain
= cpu_to_le32(domain
);
132 st
->state
= cpu_to_le32(state
);
134 ret
= scmi_do_xfer(handle
, t
);
136 scmi_xfer_put(handle
, t
);
141 scmi_power_state_get(const struct scmi_handle
*handle
, u32 domain
, u32
*state
)
146 ret
= scmi_xfer_get_init(handle
, POWER_STATE_GET
, SCMI_PROTOCOL_POWER
,
147 sizeof(u32
), sizeof(u32
), &t
);
151 put_unaligned_le32(domain
, t
->tx
.buf
);
153 ret
= scmi_do_xfer(handle
, t
);
155 *state
= get_unaligned_le32(t
->rx
.buf
);
157 scmi_xfer_put(handle
, t
);
161 static int scmi_power_num_domains_get(const struct scmi_handle
*handle
)
163 struct scmi_power_info
*pi
= handle
->power_priv
;
165 return pi
->num_domains
;
168 static char *scmi_power_name_get(const struct scmi_handle
*handle
, u32 domain
)
170 struct scmi_power_info
*pi
= handle
->power_priv
;
171 struct power_dom_info
*dom
= pi
->dom_info
+ domain
;
176 static struct scmi_power_ops power_ops
= {
177 .num_domains_get
= scmi_power_num_domains_get
,
178 .name_get
= scmi_power_name_get
,
179 .state_set
= scmi_power_state_set
,
180 .state_get
= scmi_power_state_get
,
183 static int scmi_power_protocol_init(struct scmi_handle
*handle
)
187 struct scmi_power_info
*pinfo
;
189 scmi_version_get(handle
, SCMI_PROTOCOL_POWER
, &version
);
191 dev_dbg(handle
->dev
, "Power Version %d.%d\n",
192 PROTOCOL_REV_MAJOR(version
), PROTOCOL_REV_MINOR(version
));
194 pinfo
= devm_kzalloc(handle
->dev
, sizeof(*pinfo
), GFP_KERNEL
);
198 scmi_power_attributes_get(handle
, pinfo
);
200 pinfo
->dom_info
= devm_kcalloc(handle
->dev
, pinfo
->num_domains
,
201 sizeof(*pinfo
->dom_info
), GFP_KERNEL
);
202 if (!pinfo
->dom_info
)
205 for (domain
= 0; domain
< pinfo
->num_domains
; domain
++) {
206 struct power_dom_info
*dom
= pinfo
->dom_info
+ domain
;
208 scmi_power_domain_attributes_get(handle
, domain
, dom
);
211 pinfo
->version
= version
;
212 handle
->power_ops
= &power_ops
;
213 handle
->power_priv
= pinfo
;
218 static int __init
scmi_power_init(void)
220 return scmi_protocol_register(SCMI_PROTOCOL_POWER
,
221 &scmi_power_protocol_init
);
223 subsys_initcall(scmi_power_init
);