1 /*
2  * Copyright (c) 2022 Intel Corporation.
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #include <zephyr/kernel.h>
8 #include <zephyr/pm/device.h>
9 #include <zephyr/pm/device_runtime.h>
10 #include <adsp_shim.h>
11 
12 #include <zephyr/logging/log.h>
13 LOG_MODULE_REGISTER(power_domain_intel_adsp, LOG_LEVEL_INF);
14 
15 struct pg_bits {
16 	uint32_t SPA_bit;
17 	uint32_t CPA_bit;
18 };
19 
pd_intel_adsp_set_power_enable(struct pg_bits * bits,bool power_enable)20 static int pd_intel_adsp_set_power_enable(struct pg_bits *bits, bool power_enable)
21 {
22 	uint16_t SPA_bit_mask = BIT(bits->SPA_bit);
23 
24 	if (power_enable) {
25 		sys_write16(sys_read16((mem_addr_t)&ACE_DfPMCCU.dfpwrctl) | SPA_bit_mask,
26 			    (mem_addr_t)&ACE_DfPMCCU.dfpwrctl);
27 
28 		if (!WAIT_FOR(sys_read16((mem_addr_t)&ACE_DfPMCCU.dfpwrsts) & BIT(bits->CPA_bit),
29 		    10000, k_busy_wait(1))) {
30 			return -1;
31 		}
32 	} else {
33 		sys_write16(sys_read16((mem_addr_t)&ACE_DfPMCCU.dfpwrctl) & ~(SPA_bit_mask),
34 			    (mem_addr_t)&ACE_DfPMCCU.dfpwrctl);
35 	}
36 
37 	return 0;
38 }
39 
pd_intel_adsp_pm_action(const struct device * dev,enum pm_device_action action)40 static int pd_intel_adsp_pm_action(const struct device *dev, enum pm_device_action action)
41 {
42 	struct pg_bits *reg_bits = (struct pg_bits *)dev->data;
43 	int ret = 0;
44 
45 	switch (action) {
46 	case PM_DEVICE_ACTION_RESUME:
47 		ret = pd_intel_adsp_set_power_enable(reg_bits, true);
48 
49 		if (ret == 0) {
50 			pm_device_children_action_run(dev, PM_DEVICE_ACTION_TURN_ON, NULL);
51 		}
52 
53 		break;
54 	case PM_DEVICE_ACTION_SUSPEND:
55 		pm_device_children_action_run(dev, PM_DEVICE_ACTION_TURN_OFF, NULL);
56 		ret = pd_intel_adsp_set_power_enable(reg_bits, false);
57 		break;
58 	case PM_DEVICE_ACTION_TURN_ON:
59 		break;
60 	case PM_DEVICE_ACTION_TURN_OFF:
61 		break;
62 	default:
63 		return -ENOTSUP;
64 	}
65 
66 	return ret;
67 }
pd_intel_adsp_init(const struct device * dev)68 static int pd_intel_adsp_init(const struct device *dev)
69 {
70 	pm_device_init_suspended(dev);
71 	return pm_device_runtime_enable(dev);
72 }
73 
74 #define DT_DRV_COMPAT intel_adsp_power_domain
75 
76 #define POWER_DOMAIN_DEVICE(id)							\
77 	static struct pg_bits pd_pg_reg##id = {					\
78 		.SPA_bit = DT_INST_PROP(id, bit_position),			\
79 		.CPA_bit = DT_INST_PROP(id, bit_position),			\
80 	};									\
81 	PM_DEVICE_DT_INST_DEFINE(id, pd_intel_adsp_pm_action);			\
82 	DEVICE_DT_INST_DEFINE(id, pd_intel_adsp_init, PM_DEVICE_DT_INST_GET(id),\
83 			      &pd_pg_reg##id, NULL, POST_KERNEL,                \
84 			      CONFIG_KERNEL_INIT_PRIORITY_DEFAULT, NULL);
85 
86 DT_INST_FOREACH_STATUS_OKAY(POWER_DOMAIN_DEVICE)
87