Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   1) // SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3)  * System Control and Management Interface (SCMI) System Power Protocol
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5)  * Copyright (C) 2020 ARM Ltd.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8) #define pr_fmt(fmt) "SCMI Notifications SYSTEM - " fmt
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11) #include <linux/scmi_protocol.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13) #include "common.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14) #include "notify.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16) #define SCMI_SYSTEM_NUM_SOURCES		1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) enum scmi_system_protocol_cmd {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19) 	SYSTEM_POWER_STATE_NOTIFY = 0x5,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22) struct scmi_system_power_state_notify {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23) 	__le32 notify_enable;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) struct scmi_system_power_state_notifier_payld {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) 	__le32 agent_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) 	__le32 flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) 	__le32 system_state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) struct scmi_system_info {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) 	u32 version;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) static int scmi_system_request_notify(const struct scmi_protocol_handle *ph,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) 				      bool enable)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) 	struct scmi_xfer *t;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) 	struct scmi_system_power_state_notify *notify;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) 	ret = ph->xops->xfer_get_init(ph, SYSTEM_POWER_STATE_NOTIFY,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) 				      sizeof(*notify), 0, &t);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) 	notify = t->tx.buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) 	notify->notify_enable = enable ? cpu_to_le32(BIT(0)) : 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) 	ret = ph->xops->do_xfer(ph, t);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) 	ph->xops->xfer_put(ph, t);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) static int scmi_system_set_notify_enabled(const struct scmi_protocol_handle *ph,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) 					  u8 evt_id, u32 src_id, bool enable)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) 	ret = scmi_system_request_notify(ph, enable);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) 		pr_debug("FAIL_ENABLE - evt[%X] - ret:%d\n", evt_id, ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) static void *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) scmi_system_fill_custom_report(const struct scmi_protocol_handle *ph,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) 			       u8 evt_id, ktime_t timestamp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) 			       const void *payld, size_t payld_sz,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) 			       void *report, u32 *src_id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) 	const struct scmi_system_power_state_notifier_payld *p = payld;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) 	struct scmi_system_power_state_notifier_report *r = report;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) 	if (evt_id != SCMI_EVENT_SYSTEM_POWER_STATE_NOTIFIER ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) 	    sizeof(*p) != payld_sz)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) 		return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) 	r->timestamp = timestamp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) 	r->agent_id = le32_to_cpu(p->agent_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) 	r->flags = le32_to_cpu(p->flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) 	r->system_state = le32_to_cpu(p->system_state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) 	*src_id = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) 	return r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) static const struct scmi_event system_events[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) 	{
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) 		.id = SCMI_EVENT_SYSTEM_POWER_STATE_NOTIFIER,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) 		.max_payld_sz =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) 			sizeof(struct scmi_system_power_state_notifier_payld),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) 		.max_report_sz =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) 			sizeof(struct scmi_system_power_state_notifier_report),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 	},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) static const struct scmi_event_ops system_event_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) 	.set_notify_enabled = scmi_system_set_notify_enabled,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) 	.fill_custom_report = scmi_system_fill_custom_report,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) static const struct scmi_protocol_events system_protocol_events = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) 	.queue_sz = SCMI_PROTO_QUEUE_SZ,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) 	.ops = &system_event_ops,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) 	.evts = system_events,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) 	.num_events = ARRAY_SIZE(system_events),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) 	.num_sources = SCMI_SYSTEM_NUM_SOURCES,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) static int scmi_system_protocol_init(const struct scmi_protocol_handle *ph)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) 	u32 version;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) 	struct scmi_system_info *pinfo;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) 	ph->xops->version_get(ph, &version);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) 	dev_dbg(ph->dev, "System Power Version %d.%d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) 		PROTOCOL_REV_MAJOR(version), PROTOCOL_REV_MINOR(version));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) 	pinfo = devm_kzalloc(ph->dev, sizeof(*pinfo), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) 	if (!pinfo)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) 	pinfo->version = version;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) 	return ph->set_priv(ph, pinfo);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) static const struct scmi_protocol scmi_system = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) 	.id = SCMI_PROTOCOL_SYSTEM,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) 	.owner = THIS_MODULE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) 	.init_instance = &scmi_system_protocol_init,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) 	.ops = NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) 	.events = &system_protocol_events,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) DEFINE_SCMI_PROTOCOL_REGISTER_UNREGISTER(system, scmi_system)