summaryrefslogtreecommitdiff
path: root/zephyr/shim/src/switchcap_gpio.c
blob: 91d9de942f0d4f79927a8a2b0c5431c3462a534f (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
/* Copyright 2021 The ChromiumOS Authors
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 */

#include <zephyr/devicetree.h>
#include <zephyr/drivers/gpio.h>
#include "common.h"

/* TODO(b/218600962): Consolidate switchcap code. */

#if DT_NODE_EXISTS(DT_PATH(switchcap))

#if !DT_NODE_HAS_COMPAT(DT_PATH(switchcap), switchcap_gpio)
#error "Invalid /switchcap node in device tree"
#endif

#define SC_PIN_ENABLE_PHANDLE \
	DT_PHANDLE_BY_IDX(DT_PATH(switchcap), enable_pin, 0)
#define SC_PIN_ENABLE GPIO_DT_FROM_NODE(SC_PIN_ENABLE_PHANDLE)

#define SC_PIN_POWER_GOOD_PHANDLE \
	DT_PHANDLE_BY_IDX(DT_PATH(switchcap), power_good_pin, 0)
#define SC_PIN_POWER_GOOD_EXISTS DT_NODE_EXISTS(SC_PIN_POWER_GOOD_PHANDLE)
#define SC_PIN_POWER_GOOD GPIO_DT_FROM_NODE(SC_PIN_POWER_GOOD_PHANDLE)

#if DT_NODE_HAS_PROP(DT_PATH(switchcap), poff_delay_ms)
static const int32_t poff_delay_ms = DT_PROP(DT_PATH(switchcap), poff_delay_ms);
#else
static const int32_t poff_delay_ms;
#endif

void board_set_switchcap_power(int enable)
{
	gpio_pin_set_dt(SC_PIN_ENABLE, enable);
	if (!enable && poff_delay_ms > 0)
		k_msleep(poff_delay_ms);
}

int board_is_switchcap_enabled(void)
{
	return gpio_pin_get_dt(SC_PIN_ENABLE);
}

int board_is_switchcap_power_good(void)
{
#if SC_PIN_POWER_GOOD_EXISTS
	return gpio_pin_get_dt(SC_PIN_POWER_GOOD);
#else
	return 1;
#endif
}

#endif