summaryrefslogtreecommitdiff
path: root/zephyr/subsys/ap_pwrseq/signal_gpio.c
blob: 1dbd430bef059cc6c6880a55d79e7d809d5de114 (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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
/* Copyright 2022 The ChromiumOS Authors
 * 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;
	}
	/*
	 * Getting the current value of an output is
	 * done by retrieving the config and checking what the
	 * output state has been set to, not by reading the
	 * physical level of the pin (open drain outputs
	 * may have a low voltage).
	 */
	if (IS_ENABLED(CONFIG_GPIO_GET_CONFIG) && gpio_config[index].output) {
		int rv;
		gpio_flags_t flags;

		rv = gpio_pin_get_config_dt(&spec[index], &flags);
		if (rv == 0) {
			int pin = (flags & GPIO_OUTPUT_INIT_HIGH) ? 1 : 0;
			/* If active low signal, invert it */
			if (spec[index].dt_flags & GPIO_ACTIVE_LOW) {
				pin = !pin;
			}
			return pin;
		}
		/*
		 * -ENOSYS is returned when this API call is not supported,
		 *  so drop into the default method of returning the pin value.
		 */
		if (rv != -ENOSYS) {
			return rv;
		}
	}
	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 */