summaryrefslogtreecommitdiff
path: root/zephyr/subsys/ap_pwrseq/signal_gpio.c
blob: 9f8c3adb482c8f7a9431f41ce8309778c4163e32 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
/* Copyright 2022 The Chromium OS Authors. All rights reserved.
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 */

#include <power_signals.h>
#include <signal_gpio.h>
#include <zephyr/drivers/gpio.h>
#include "system.h"

#define MY_COMPAT	intel_ap_pwrseq_gpio

#if HAS_GPIO_SIGNALS

#define INIT_GPIO_SPEC(id)	\
	GPIO_DT_SPEC_GET(id, gpios),

const static struct gpio_dt_spec spec[] = {
DT_FOREACH_STATUS_OKAY(MY_COMPAT, INIT_GPIO_SPEC)
};

/*
 * Configuration for GPIO inputs.
 */
struct ps_gpio_int {
	gpio_flags_t flags;
	uint8_t signal;
	unsigned output : 1;
	unsigned no_enable : 1;
};

#define INIT_GPIO_CONFIG(id)					\
	{							\
		.flags = DT_PROP_OR(id, interrupt_flags, 0),	\
		.signal = PWR_SIGNAL_ENUM(id),			\
		.no_enable = DT_PROP(id, no_enable),		\
		.output = DT_PROP(id, output),			\
	 },

const static struct ps_gpio_int gpio_config[] = {
DT_FOREACH_STATUS_OKAY(MY_COMPAT, INIT_GPIO_CONFIG)
};

static struct gpio_callback int_cb[ARRAY_SIZE(gpio_config)];

int power_signal_gpio_enable(enum pwr_sig_gpio index)
{
	gpio_flags_t flags;

	if (index < 0 || index >= ARRAY_SIZE(gpio_config)) {
		return -EINVAL;
	}
	/*
	 * Do not allow interrupts on an output GPIO.
	 */
	if (gpio_config[index].output) {
		return -EINVAL;
	}
	flags = gpio_config[index].flags;

	/* Only enable if flags are present. */
	if (flags) {
		return gpio_pin_interrupt_configure_dt(&spec[index], flags);
	}
	return -EINVAL;
}

int power_signal_gpio_disable(enum pwr_sig_gpio index)
{
	gpio_flags_t flags;

	if (index < 0 || index >= ARRAY_SIZE(gpio_config)) {
		return -EINVAL;
	}
	/*
	 * Do not allow interrupts on an output GPIO.
	 */
	if (gpio_config[index].output) {
		return -EINVAL;
	}
	flags = gpio_config[index].flags;

	/* Disable if flags are present. */
	if (flags) {
		return gpio_pin_interrupt_configure_dt(&spec[index],
						       GPIO_INT_DISABLE);
	}
	return -EINVAL;
}

void power_signal_gpio_interrupt(const struct device *port,
				 struct gpio_callback *cb,
				 gpio_port_pins_t pins)
{
	int index = cb - int_cb;

	power_signal_interrupt(gpio_config[index].signal,
			       gpio_pin_get_dt(&spec[index]));
}

int power_signal_gpio_get(enum pwr_sig_gpio index)
{
	if (index < 0 || index >= ARRAY_SIZE(gpio_config)) {
		return -EINVAL;
	}
	return gpio_pin_get_dt(&spec[index]);
}

int power_signal_gpio_set(enum pwr_sig_gpio index, int value)
{
	if (index < 0 || index >= ARRAY_SIZE(gpio_config)) {
		return -EINVAL;
	}
	if (!gpio_config[index].output) {
		return -EINVAL;
	}
	return gpio_pin_set_dt(&spec[index], value);
}
void power_signal_gpio_init(void)
{
	/*
	 * If there has been a sysjump, do not set the output
	 * to the deasserted state.
	 */
	gpio_flags_t out_flags = system_jumped_to_this_image() ?
				 GPIO_OUTPUT : GPIO_OUTPUT_INACTIVE;

	for (int i = 0; i < ARRAY_SIZE(gpio_config); i++) {
		if (gpio_config[i].output) {
			gpio_pin_configure_dt(&spec[i], out_flags);
		} else {
			gpio_pin_configure_dt(&spec[i], GPIO_INPUT);
			/* If interrupt, initialise it */
			if (gpio_config[i].flags) {
				gpio_init_callback(&int_cb[i],
					   power_signal_gpio_interrupt,
					   BIT(spec[i].pin));
				gpio_add_callback(spec[i].port, &int_cb[i]);
				/*
				 * If the interrupt is to be enabled at
				 * startup, enable the interrupt.
				 */
				if (!gpio_config[i].no_enable) {
					power_signal_gpio_enable(i);
				}
			}
		}
	}
}

#endif /*  HAS_GPIO_SIGNALS */