|
| 1 | +/* |
| 2 | + * Copyright 2024 The Chromium OS Authors. All rights reserved. |
| 3 | + * Use of this source code is governed by a BSD-style license that can be |
| 4 | + * found in the LICENSE file. |
| 5 | + */ |
| 6 | + |
| 7 | +#include <zephyr/drivers/gpio.h> |
| 8 | + |
| 9 | +#include "adc.h" |
| 10 | +#include "battery.h" |
| 11 | +#include "battery_smart.h" |
| 12 | +#include "battery_fuel_gauge.h" |
| 13 | +#include "board_adc.h" |
| 14 | +#include "board_host_command.h" |
| 15 | +#include "board_function.h" |
| 16 | +#include "charger.h" |
| 17 | +#include "charge_state.h" |
| 18 | +#include "console.h" |
| 19 | +#include "customized_shared_memory.h" |
| 20 | +#include "hooks.h" |
| 21 | +#include "host_command.h" |
| 22 | +#include "system.h" |
| 23 | +#include "util.h" |
| 24 | + |
| 25 | +#define CPRINTS(format, args...) cprints(CC_CHARGER, format, ##args) |
| 26 | +#define CPRINTF(format, args...) cprintf(CC_CHARGER, format, ##args) |
| 27 | + |
| 28 | +static uint8_t charging_maximum_level = EC_CHARGE_LIMIT_RESTORE; |
| 29 | + |
| 30 | +static void battery_percentage_control(void) |
| 31 | +{ |
| 32 | + enum ec_charge_control_mode new_mode; |
| 33 | + static int in_percentage_control; |
| 34 | + uint32_t batt_os_percentage = get_system_percentage(); |
| 35 | + int rv; |
| 36 | + |
| 37 | + /** |
| 38 | + * If the host command EC_CMD_CHARGE_CONTROL set control mode to CHARGE_CONTROL_DISCHARGE |
| 39 | + * or CHARGE_CONTROL_IDLE, ignore the battery_percentage_control(); |
| 40 | + */ |
| 41 | + if (!in_percentage_control && get_chg_ctrl_mode() != CHARGE_CONTROL_NORMAL) |
| 42 | + return; |
| 43 | + |
| 44 | + if (charging_maximum_level == EC_CHARGE_LIMIT_RESTORE) |
| 45 | + system_get_bbram(SYSTEM_BBRAM_IDX_CHARGE_LIMIT_MAX, &charging_maximum_level); |
| 46 | + |
| 47 | + if (charging_maximum_level & CHG_LIMIT_OVERRIDE) { |
| 48 | + new_mode = CHARGE_CONTROL_NORMAL; |
| 49 | + if (batt_os_percentage == 1000) |
| 50 | + charging_maximum_level = charging_maximum_level | 0x64; |
| 51 | + } else if (charging_maximum_level < 20) |
| 52 | + new_mode = CHARGE_CONTROL_NORMAL; |
| 53 | + else if (batt_os_percentage > charging_maximum_level * 10) { |
| 54 | + new_mode = CHARGE_CONTROL_DISCHARGE; |
| 55 | + in_percentage_control = 1; |
| 56 | + } else if (batt_os_percentage == charging_maximum_level * 10) { |
| 57 | + new_mode = CHARGE_CONTROL_IDLE; |
| 58 | + in_percentage_control = 1; |
| 59 | + } else { |
| 60 | + new_mode = CHARGE_CONTROL_NORMAL; |
| 61 | + in_percentage_control = 0; |
| 62 | + } |
| 63 | + |
| 64 | + set_chg_ctrl_mode(new_mode); |
| 65 | +#ifdef CONFIG_PLATFORM_EC_CHARGER_DISCHARGE_ON_AC |
| 66 | + rv = charger_discharge_on_ac(new_mode == CHARGE_CONTROL_DISCHARGE); |
| 67 | +#endif |
| 68 | + if (rv != EC_SUCCESS) |
| 69 | + CPRINTS("Failed to discharge."); |
| 70 | +} |
| 71 | +DECLARE_HOOK(HOOK_AC_CHANGE, battery_percentage_control, HOOK_PRIO_DEFAULT); |
| 72 | +DECLARE_HOOK(HOOK_BATTERY_SOC_CHANGE, battery_percentage_control, HOOK_PRIO_DEFAULT); |
| 73 | + |
| 74 | +/*****************************************************************************/ |
| 75 | +/* Host command */ |
| 76 | + |
| 77 | +static enum ec_status cmd_charging_limit_control(struct host_cmd_handler_args *args) |
| 78 | +{ |
| 79 | + |
| 80 | + const struct ec_params_ec_chg_limit_control *p = args->params; |
| 81 | + struct ec_response_chg_limit_control *r = args->response; |
| 82 | + |
| 83 | + if (p->modes & CHG_LIMIT_DISABLE) { |
| 84 | + charging_maximum_level = 0; |
| 85 | + system_set_bbram(SYSTEM_BBRAM_IDX_CHARGE_LIMIT_MAX, 0); |
| 86 | + } |
| 87 | + |
| 88 | + if (p->modes & CHG_LIMIT_SET_LIMIT) { |
| 89 | + if (p->max_percentage < 20) |
| 90 | + return EC_RES_ERROR; |
| 91 | + |
| 92 | + charging_maximum_level = p->max_percentage; |
| 93 | + system_set_bbram(SYSTEM_BBRAM_IDX_CHARGE_LIMIT_MAX, charging_maximum_level); |
| 94 | + } |
| 95 | + |
| 96 | + if (p->modes & CHG_LIMIT_OVERRIDE) |
| 97 | + charging_maximum_level = charging_maximum_level | CHG_LIMIT_OVERRIDE; |
| 98 | + |
| 99 | + if (p->modes & CHG_LIMIT_GET_LIMIT) { |
| 100 | + system_get_bbram(SYSTEM_BBRAM_IDX_CHARGE_LIMIT_MAX, &charging_maximum_level); |
| 101 | + r->max_percentage = charging_maximum_level; |
| 102 | + args->response_size = sizeof(*r); |
| 103 | + } |
| 104 | + |
| 105 | + battery_percentage_control(); |
| 106 | + |
| 107 | + return EC_RES_SUCCESS; |
| 108 | +} |
| 109 | +DECLARE_HOST_COMMAND(EC_CMD_CHARGE_LIMIT_CONTROL, cmd_charging_limit_control, |
| 110 | + EC_VER_MASK(0)); |
0 commit comments