summaryrefslogtreecommitdiff
path: root/common/lid_switch.c
diff options
context:
space:
mode:
authorRandall Spangler <rspangler@chromium.org>2013-04-01 10:56:33 -0700
committerChromeBot <chrome-bot@google.com>2013-04-02 14:12:57 -0700
commitbdd16f82069f9e07e9a9e3008c318d9581ce0664 (patch)
tree16287a843f975c9505636dcc13fbaef50cb08f9a /common/lid_switch.c
parent50c53c0d542d6fc3184db09a35196b4c702536c5 (diff)
downloadchrome-ec-bdd16f82069f9e07e9a9e3008c318d9581ce0664.tar.gz
Split lid switch code out of switch.c to its own file
This will allow ARM code to use the same lid switch code (in a subsequent CL). BUG=chrome-os-partner:18343 BRANCH=none TEST=open lid; system boots. close lid; system suspends. open lid; resumes. Change-Id: I83536a3ad24c4446dccf8a6b6e296756659070a8 Signed-off-by: Randall Spangler <rspangler@chromium.org> Reviewed-on: https://gerrit.chromium.org/gerrit/47043 Reviewed-by: Bill Richardson <wfrichar@chromium.org>
Diffstat (limited to 'common/lid_switch.c')
-rw-r--r--common/lid_switch.c126
1 files changed, 126 insertions, 0 deletions
diff --git a/common/lid_switch.c b/common/lid_switch.c
new file mode 100644
index 0000000000..68c049e52c
--- /dev/null
+++ b/common/lid_switch.c
@@ -0,0 +1,126 @@
+/* Copyright (c) 2013 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.
+ */
+
+/* Lid switch module for Chrome EC */
+
+#include "common.h"
+#include "console.h"
+#include "gpio.h"
+#include "hooks.h"
+#include "host_command.h"
+#include "timer.h"
+#include "util.h"
+
+/* Console output macros */
+#define CPUTS(outstr) cputs(CC_SWITCH, outstr)
+#define CPRINTF(format, args...) cprintf(CC_SWITCH, format, ## args)
+
+#define LID_DEBOUNCE_US (30 * MSEC) /* Debounce time for lid switch */
+
+static int debounced_lid_open; /* Debounced lid state */
+
+/**
+ * Get raw lid switch state.
+ *
+ * @return 1 if lid is open, 0 if closed.
+ */
+static int raw_lid_open(void)
+{
+ return gpio_get_level(GPIO_LID_SWITCHn) ? 1 : 0;
+}
+
+/**
+ * Handle lid open.
+ */
+static void lid_switch_open(void)
+{
+ if (debounced_lid_open) {
+ CPRINTF("[%T lid already open]\n");
+ return;
+ }
+
+ CPRINTF("[%T lid open]\n");
+ debounced_lid_open = 1;
+ hook_notify(HOOK_LID_CHANGE);
+ host_set_single_event(EC_HOST_EVENT_LID_OPEN);
+}
+
+/**
+ * Handle lid close.
+ */
+static void lid_switch_close(void)
+{
+ if (!debounced_lid_open) {
+ CPRINTF("[%T lid already closed]\n");
+ return;
+ }
+
+ CPRINTF("[%T lid close]\n");
+ debounced_lid_open = 0;
+ hook_notify(HOOK_LID_CHANGE);
+ host_set_single_event(EC_HOST_EVENT_LID_CLOSED);
+}
+
+int lid_is_open(void)
+{
+ return debounced_lid_open;
+}
+
+/**
+ * Lid switch initialization code
+ */
+static void lid_init(void)
+{
+ if (raw_lid_open())
+ debounced_lid_open = 1;
+
+ /* Enable interrupts, now that we've initialized */
+ gpio_enable_interrupt(GPIO_LID_SWITCHn);
+}
+DECLARE_HOOK(HOOK_INIT, lid_init, HOOK_PRIO_INIT_LID);
+
+/**
+ * Handle debounced lid switch changing state.
+ */
+static void lid_change_deferred(void)
+{
+ const int new_open = raw_lid_open();
+
+ /* If lid hasn't changed state, nothing to do */
+ if (new_open == debounced_lid_open)
+ return;
+
+ if (new_open)
+ lid_switch_open();
+ else
+ lid_switch_close();
+}
+DECLARE_DEFERRED(lid_change_deferred);
+
+void lid_interrupt(enum gpio_signal signal)
+{
+ /* Reset lid debounce time */
+ hook_call_deferred(lid_change_deferred, LID_DEBOUNCE_US);
+}
+
+static int command_lidopen(int argc, char **argv)
+{
+ lid_switch_open();
+ return EC_SUCCESS;
+}
+DECLARE_CONSOLE_COMMAND(lidopen, command_lidopen,
+ NULL,
+ "Simulate lid open",
+ NULL);
+
+static int command_lidclose(int argc, char **argv)
+{
+ lid_switch_close();
+ return EC_SUCCESS;
+}
+DECLARE_CONSOLE_COMMAND(lidclose, command_lidclose,
+ NULL,
+ "Simulate lid close",
+ NULL);