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

#include "adc.h"
#include "ap_power/ap_power.h"
#include "base_state.h"
#include "chipset.h"
#include "console.h"
#include "hooks.h"
#include "tablet_mode.h"

#include <zephyr/drivers/gpio.h>

#define CPRINTS(format, args...) cprints(CC_SYSTEM, format, ##args)
#define CPRINTF(format, args...) cprintf(CC_SYSTEM, format, ##args)

#define BASE_DETECT_INTERVAL (200 * MSEC)
#define ATTACH_MAX_THRESHOLD_MV 300
#define DETACH_MIN_THRESHOLD_MV 3000

static void base_update(bool attached)
{
	const struct gpio_dt_spec *en_cc_lid_base_pu =
		GPIO_DT_FROM_NODELABEL(en_cc_lid_base_pu);

	base_set_state(attached);
	tablet_set_mode(!attached, TABLET_TRIGGER_BASE);

	gpio_pin_set_dt(GPIO_DT_FROM_NODELABEL(en_ppvar_base_x), attached);
	gpio_pin_configure(en_cc_lid_base_pu->port, en_cc_lid_base_pu->pin,
			   attached ? GPIO_OUTPUT_HIGH : GPIO_INPUT);
}

static void base_detect_tick(void);
DECLARE_DEFERRED(base_detect_tick);

static void base_detect_tick(void)
{
	static bool debouncing;
	int mv = adc_read_channel(ADC_BASE_DET);

	if (mv >= DETACH_MIN_THRESHOLD_MV && base_get_state()) {
		if (!debouncing) {
			debouncing = true;
		} else {
			debouncing = false;
			base_update(false);
		}
	} else if (mv <= ATTACH_MAX_THRESHOLD_MV && !base_get_state()) {
		if (!debouncing) {
			debouncing = true;
		} else {
			debouncing = false;
			base_update(true);
		}
	} else {
		debouncing = false;
	}
	hook_call_deferred(&base_detect_tick_data, BASE_DETECT_INTERVAL);
}

static void base_detect_enable(bool enable)
{
	if (enable) {
		hook_call_deferred(&base_detect_tick_data,
				   BASE_DETECT_INTERVAL);
	} else {
		hook_call_deferred(&base_detect_tick_data, -1);
		base_update(false);
	}
}

static void base_startup_hook(struct ap_power_ev_callback *cb,
			      struct ap_power_ev_data data)
{
	switch (data.event) {
	case AP_POWER_STARTUP:
		base_detect_enable(true);
		break;
	case AP_POWER_SHUTDOWN:
		base_detect_enable(false);
		break;
	default:
		return;
	}
}

static int base_init(const struct device *unused)
{
	static struct ap_power_ev_callback cb;

	base_update(false);

	ap_power_ev_init_callback(&cb, base_startup_hook,
				  AP_POWER_STARTUP | AP_POWER_SHUTDOWN);
	ap_power_ev_add_callback(&cb);

	if (!chipset_in_state(CHIPSET_STATE_ANY_OFF)) {
		base_detect_enable(true);
	}

	return 0;
}

SYS_INIT(base_init, APPLICATION, 1);

void base_force_state(enum ec_set_base_state_cmd state)
{
	switch (state) {
	case EC_SET_BASE_STATE_ATTACH:
		base_update(true);
		base_detect_enable(false);
		break;
	case EC_SET_BASE_STATE_DETACH:
		base_update(false);
		base_detect_enable(false);
		break;
	case EC_SET_BASE_STATE_RESET:
		base_detect_enable(true);
		break;
	}
}