Files
OpenCellular/common/lid_switch.c
Randall Spangler 45bc5c1a21 Split out power button code from switch.c
The power button code is platform-independent.  This change splits the
code out of the LM4 switch.c module so that a subseqent change to
STM32 platforms can start using it.

BUG=chrome-os-partner:18945
BRANCH=none
TEST=manual

1. Power+refresh+esc goes to recovery mode,
2. Press power button at recovery screen turns off.
3. With system off, power button turns system on.
4. Press power button for a second; screen locks.
5. Press power button while typing; blocks keystrokes while it's pressed.
6. Hold power button down for 8 sec; system forced to shutdown.
7. From EC console, with system on:
   hostevent clear
   hostevent -> event 0x04 is clear
   press power button
   hostevent -> event 0x04 is set
8. From EC console, with system off:
   powerbtn -> system turns on
   powerbtn 5000 -> system turns off, just like power button was held for 5 sec

Change-Id: If2a9b02514a201e1d03c857d128e2ccab51a16ef
Signed-off-by: Randall Spangler <rspangler@chromium.org>
Reviewed-on: https://gerrit.chromium.org/gerrit/49217
Reviewed-by: Bill Richardson <wfrichar@chromium.org>
2013-04-25 17:03:18 -07:00

128 lines
2.5 KiB
C

/* 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 "lid_switch.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_OPEN) ? 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_OPEN);
}
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);