blob: e5175ef73b40e0d0375466f647203dd91299f3eb [file] [log] [blame]
Cristian Marussia8803052020-09-07 18:46:55 +01001// SPDX-License-Identifier: GPL-2.0
2/*
3 * System Control and Management Interface (SCMI) System Power Protocol
4 *
Cristian Marussi48dc16e2021-03-16 12:48:26 +00005 * Copyright (C) 2020-2021 ARM Ltd.
Cristian Marussia8803052020-09-07 18:46:55 +01006 */
7
8#define pr_fmt(fmt) "SCMI Notifications SYSTEM - " fmt
9
Cristian Marussif5800e02021-03-16 12:49:02 +000010#include <linux/module.h>
Cristian Marussia8803052020-09-07 18:46:55 +010011#include <linux/scmi_protocol.h>
12
13#include "common.h"
14#include "notify.h"
15
16#define SCMI_SYSTEM_NUM_SOURCES 1
17
18enum scmi_system_protocol_cmd {
19 SYSTEM_POWER_STATE_NOTIFY = 0x5,
20};
21
22struct scmi_system_power_state_notify {
23 __le32 notify_enable;
24};
25
26struct scmi_system_power_state_notifier_payld {
27 __le32 agent_id;
28 __le32 flags;
29 __le32 system_state;
30};
31
32struct scmi_system_info {
33 u32 version;
34};
35
Cristian Marussib46d8522021-03-16 12:48:52 +000036static int scmi_system_request_notify(const struct scmi_protocol_handle *ph,
Cristian Marussia8803052020-09-07 18:46:55 +010037 bool enable)
38{
39 int ret;
40 struct scmi_xfer *t;
41 struct scmi_system_power_state_notify *notify;
42
Cristian Marussib46d8522021-03-16 12:48:52 +000043 ret = ph->xops->xfer_get_init(ph, SYSTEM_POWER_STATE_NOTIFY,
44 sizeof(*notify), 0, &t);
Cristian Marussia8803052020-09-07 18:46:55 +010045 if (ret)
46 return ret;
47
48 notify = t->tx.buf;
49 notify->notify_enable = enable ? cpu_to_le32(BIT(0)) : 0;
50
Cristian Marussib46d8522021-03-16 12:48:52 +000051 ret = ph->xops->do_xfer(ph, t);
Cristian Marussia8803052020-09-07 18:46:55 +010052
Cristian Marussib46d8522021-03-16 12:48:52 +000053 ph->xops->xfer_put(ph, t);
Cristian Marussia8803052020-09-07 18:46:55 +010054 return ret;
55}
56
Cristian Marussi3cb8c952021-03-16 12:48:59 +000057static int scmi_system_set_notify_enabled(const struct scmi_protocol_handle *ph,
Cristian Marussia8803052020-09-07 18:46:55 +010058 u8 evt_id, u32 src_id, bool enable)
59{
60 int ret;
61
Cristian Marussib46d8522021-03-16 12:48:52 +000062 ret = scmi_system_request_notify(ph, enable);
Cristian Marussia8803052020-09-07 18:46:55 +010063 if (ret)
64 pr_debug("FAIL_ENABLE - evt[%X] - ret:%d\n", evt_id, ret);
65
66 return ret;
67}
68
Cristian Marussi3cb8c952021-03-16 12:48:59 +000069static void *
70scmi_system_fill_custom_report(const struct scmi_protocol_handle *ph,
71 u8 evt_id, ktime_t timestamp,
72 const void *payld, size_t payld_sz,
73 void *report, u32 *src_id)
Cristian Marussia8803052020-09-07 18:46:55 +010074{
75 const struct scmi_system_power_state_notifier_payld *p = payld;
76 struct scmi_system_power_state_notifier_report *r = report;
77
78 if (evt_id != SCMI_EVENT_SYSTEM_POWER_STATE_NOTIFIER ||
79 sizeof(*p) != payld_sz)
80 return NULL;
81
82 r->timestamp = timestamp;
83 r->agent_id = le32_to_cpu(p->agent_id);
84 r->flags = le32_to_cpu(p->flags);
85 r->system_state = le32_to_cpu(p->system_state);
86 *src_id = 0;
87
88 return r;
89}
90
91static const struct scmi_event system_events[] = {
92 {
93 .id = SCMI_EVENT_SYSTEM_POWER_STATE_NOTIFIER,
94 .max_payld_sz =
95 sizeof(struct scmi_system_power_state_notifier_payld),
96 .max_report_sz =
97 sizeof(struct scmi_system_power_state_notifier_report),
98 },
99};
100
101static const struct scmi_event_ops system_event_ops = {
102 .set_notify_enabled = scmi_system_set_notify_enabled,
103 .fill_custom_report = scmi_system_fill_custom_report,
104};
105
Cristian Marussi533c7092021-03-16 12:48:31 +0000106static const struct scmi_protocol_events system_protocol_events = {
107 .queue_sz = SCMI_PROTO_QUEUE_SZ,
108 .ops = &system_event_ops,
109 .evts = system_events,
110 .num_events = ARRAY_SIZE(system_events),
111 .num_sources = SCMI_SYSTEM_NUM_SOURCES,
112};
113
Cristian Marussib46d8522021-03-16 12:48:52 +0000114static int scmi_system_protocol_init(const struct scmi_protocol_handle *ph)
Cristian Marussia8803052020-09-07 18:46:55 +0100115{
116 u32 version;
117 struct scmi_system_info *pinfo;
118
Cristian Marussib46d8522021-03-16 12:48:52 +0000119 ph->xops->version_get(ph, &version);
Cristian Marussia8803052020-09-07 18:46:55 +0100120
Cristian Marussib46d8522021-03-16 12:48:52 +0000121 dev_dbg(ph->dev, "System Power Version %d.%d\n",
Cristian Marussia8803052020-09-07 18:46:55 +0100122 PROTOCOL_REV_MAJOR(version), PROTOCOL_REV_MINOR(version));
123
Cristian Marussib46d8522021-03-16 12:48:52 +0000124 pinfo = devm_kzalloc(ph->dev, sizeof(*pinfo), GFP_KERNEL);
Cristian Marussia8803052020-09-07 18:46:55 +0100125 if (!pinfo)
126 return -ENOMEM;
127
Cristian Marussia8803052020-09-07 18:46:55 +0100128 pinfo->version = version;
Cristian Marussib46d8522021-03-16 12:48:52 +0000129 return ph->set_priv(ph, pinfo);
Cristian Marussia8803052020-09-07 18:46:55 +0100130}
131
Cristian Marussi48dc16e2021-03-16 12:48:26 +0000132static const struct scmi_protocol scmi_system = {
133 .id = SCMI_PROTOCOL_SYSTEM,
Cristian Marussif5800e02021-03-16 12:49:02 +0000134 .owner = THIS_MODULE,
Cristian Marussib46d8522021-03-16 12:48:52 +0000135 .instance_init = &scmi_system_protocol_init,
Cristian Marussi48dc16e2021-03-16 12:48:26 +0000136 .ops = NULL,
Cristian Marussi533c7092021-03-16 12:48:31 +0000137 .events = &system_protocol_events,
Cristian Marussi48dc16e2021-03-16 12:48:26 +0000138};
139
140DEFINE_SCMI_PROTOCOL_REGISTER_UNREGISTER(system, scmi_system)