| 1 |
/* SPDX-License-Identifier: GPL-2.0+ */ |
| 2 |
/* |
| 3 |
* Helper for batteries with accurate current and voltage measurement, but |
| 4 |
* without temperature measurement or without a "resistance-temp-table". |
| 5 |
* Copyright (c) 2021-2025 Hans de Goede <[email protected]> |
| 6 |
*/ |
| 7 |
|
| 8 |
#include <linux/mutex.h> |
| 9 |
#include <linux/workqueue.h> |
| 10 |
|
| 11 |
#define ADC_BAT_HELPER_MOV_AVG_WINDOW_SIZE 8 |
| 12 |
|
| 13 |
struct power_supply; |
| 14 |
struct gpio_desc; |
| 15 |
|
| 16 |
/* |
| 17 |
* The adc battery helper code needs voltage- and current-now to be sampled as |
| 18 |
* close to each other (in sample-time) as possible. A single getter function is |
| 19 |
* used to allow the battery driver to handle this in the best way possible. |
| 20 |
*/ |
| 21 |
typedef int (*adc_battery_helper_get_func)(struct power_supply *psy, int *volt, int *curr); |
| 22 |
|
| 23 |
struct adc_battery_helper { |
| 24 |
struct power_supply *psy; |
| 25 |
struct gpio_desc *charge_finished; |
| 26 |
struct delayed_work work; |
| 27 |
struct mutex lock; |
| 28 |
adc_battery_helper_get_func get_voltage_and_current_now; |
| 29 |
int ocv_uv[ADC_BAT_HELPER_MOV_AVG_WINDOW_SIZE]; /* micro-volt */ |
| 30 |
int intern_res_mohm[ADC_BAT_HELPER_MOV_AVG_WINDOW_SIZE]; /* milli-ohm */ |
| 31 |
int poll_count; |
| 32 |
int ocv_avg_index; |
| 33 |
int ocv_avg_uv; /* micro-volt */ |
| 34 |
int intern_res_poll_count; |
| 35 |
int intern_res_avg_index; |
| 36 |
int intern_res_avg_mohm; /* milli-ohm */ |
| 37 |
int volt_uv; /* micro-volt */ |
| 38 |
int curr_ua; /* micro-ampere */ |
| 39 |
int capacity; /* percent */ |
| 40 |
int status; |
| 41 |
bool supplied; |
| 42 |
}; |
| 43 |
|
| 44 |
extern const enum power_supply_property adc_battery_helper_properties[]; |
| 45 |
/* Must be const cannot be an external. Asserted in adc-battery-helper.c */ |
| 46 |
#define ADC_HELPER_NUM_PROPERTIES 7 |
| 47 |
|
| 48 |
int adc_battery_helper_init(struct adc_battery_helper *help, struct power_supply *psy, |
| 49 |
adc_battery_helper_get_func get_voltage_and_current_now, |
| 50 |
struct gpio_desc *charge_finished_gpio); |
| 51 |
/* |
| 52 |
* The below functions can be directly used as power-supply / suspend-resume |
| 53 |
* callbacks. They cast the power_supply_get_drvdata() / dev_get_drvdata() data |
| 54 |
* directly to struct adc_battery_helper. Therefor struct adc_battery_helper |
| 55 |
* MUST be the first member of the battery driver's data struct. |
| 56 |
*/ |
| 57 |
int adc_battery_helper_get_property(struct power_supply *psy, |
| 58 |
enum power_supply_property psp, |
| 59 |
union power_supply_propval *val); |
| 60 |
void adc_battery_helper_external_power_changed(struct power_supply *psy); |
| 61 |
int adc_battery_helper_suspend(struct device *dev); |
| 62 |
int adc_battery_helper_resume(struct device *dev); |