mirror of
https://github.com/Telecominfraproject/OpenCellular.git
synced 2025-12-29 10:00:51 +00:00
Fix bug on samus that the battery status doesn't always show on lightbar when AC is plugged/unplugged. It doesn't show when the battery is full is S3 or S5 because in these states we turn off CHARGE_EN so that ACOK to the EC never toggles. Instead, what we want to do is display battery status whenever the active charge port changes. This will happen when AC is plugged or unplugged OR if a user has AC on both ports and toggles between them using the charge override hot-keys. BUG=chrome-os-partner:36317 BRANCH=samus TEST=test plugging and unplugging AC on both sides when battery is full and unit is in S0, and when unit is in S5. also tested lightbar flashes battery percentage when two zingers are plugged in and you switch between them using Ctrl+Search+0|1|2. Change-Id: I5cd7fff4f466adf857f1e63f07f3b0c7ae8422c7 Signed-off-by: Alec Berg <alecaberg@chromium.org> Reviewed-on: https://chromium-review.googlesource.com/245922 Reviewed-by: Bill Richardson <wfrichar@chromium.org>
112 lines
3.0 KiB
C
112 lines
3.0 KiB
C
/* Copyright (c) 2014 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.
|
|
*/
|
|
|
|
/* Host command module for PD MCU */
|
|
|
|
#include "charge_state.h"
|
|
#include "common.h"
|
|
#include "console.h"
|
|
#include "host_command.h"
|
|
#include "lightbar.h"
|
|
#include "task.h"
|
|
#include "timer.h"
|
|
#include "util.h"
|
|
|
|
#define CPRINTS(format, args...) cprints(CC_PD_HOST_CMD, format, ## args)
|
|
|
|
#define TASK_EVENT_EXCHANGE_PD_STATUS TASK_EVENT_CUSTOM(1)
|
|
|
|
/* By default allow 5V charging only for the dead battery case */
|
|
static enum pd_charge_state charge_state = PD_CHARGE_5V;
|
|
|
|
#define CHARGE_PORT_UNINITIALIZED -2
|
|
static int charge_port = CHARGE_PORT_UNINITIALIZED;
|
|
|
|
void host_command_pd_send_status(enum pd_charge_state new_chg_state)
|
|
{
|
|
/* Update PD MCU charge state if necessary */
|
|
if (new_chg_state != PD_CHARGE_NO_CHANGE)
|
|
charge_state = new_chg_state;
|
|
/* Wake PD HC task to send status */
|
|
task_set_event(TASK_ID_PDCMD, TASK_EVENT_EXCHANGE_PD_STATUS, 0);
|
|
}
|
|
|
|
int pd_get_active_charge_port(void)
|
|
{
|
|
return charge_port;
|
|
}
|
|
|
|
static void pd_exchange_status(void)
|
|
{
|
|
struct ec_params_pd_status ec_status;
|
|
struct ec_response_pd_status pd_status;
|
|
int rv = 0;
|
|
|
|
/* Send PD charge state and battery state of charge */
|
|
ec_status.charge_state = charge_state;
|
|
if (charge_get_flags() & CHARGE_FLAG_BATT_RESPONSIVE)
|
|
ec_status.batt_soc = charge_get_percent();
|
|
else
|
|
ec_status.batt_soc = -1;
|
|
|
|
rv = pd_host_command(EC_CMD_PD_EXCHANGE_STATUS, 1, &ec_status,
|
|
sizeof(struct ec_params_pd_status), &pd_status,
|
|
sizeof(struct ec_response_pd_status));
|
|
|
|
/* If PD doesn't support new command version, try old version */
|
|
if (rv == -EC_RES_INVALID_VERSION)
|
|
rv = pd_host_command(EC_CMD_PD_EXCHANGE_STATUS, 0, &ec_status,
|
|
sizeof(struct ec_params_pd_status), &pd_status,
|
|
sizeof(struct ec_response_pd_status));
|
|
|
|
if (rv < 0) {
|
|
CPRINTS("Host command to PD MCU failed");
|
|
return;
|
|
}
|
|
|
|
#ifdef HAS_TASK_LIGHTBAR
|
|
/*
|
|
* If charge port has changed, and it was initialized, then show
|
|
* battery status on lightbar.
|
|
*/
|
|
if (pd_status.active_charge_port != charge_port) {
|
|
if (charge_port != CHARGE_PORT_UNINITIALIZED) {
|
|
charge_port = pd_status.active_charge_port;
|
|
lightbar_sequence(LIGHTBAR_TAP);
|
|
} else {
|
|
charge_port = pd_status.active_charge_port;
|
|
}
|
|
}
|
|
#else
|
|
/* Store the active charge port */
|
|
charge_port = pd_status.active_charge_port;
|
|
#endif
|
|
|
|
/* Set input current limit */
|
|
rv = charge_set_input_current_limit(MAX(pd_status.curr_lim_ma,
|
|
CONFIG_CHARGER_INPUT_CURRENT));
|
|
if (rv < 0)
|
|
CPRINTS("Failed to set input current limit from PD MCU");
|
|
|
|
/* If PD is signalling host event, then pass it up to AP */
|
|
if (pd_status.status & PD_STATUS_HOST_EVENT)
|
|
host_set_single_event(EC_HOST_EVENT_PD_MCU);
|
|
}
|
|
|
|
void pd_command_task(void)
|
|
{
|
|
/* On startup exchange status with the PD */
|
|
pd_exchange_status();
|
|
|
|
while (1) {
|
|
/* Wait for the next command event */
|
|
int evt = task_wait_event(-1);
|
|
|
|
/* Process event to send status to PD */
|
|
if (evt & TASK_EVENT_EXCHANGE_PD_STATUS)
|
|
pd_exchange_status();
|
|
}
|
|
}
|