// SPDX-License-Identifier: GPL-2.0+ /* * Battery monitor driver for the uPI uG3105 battery monitor * * Note the uG3105 is not a full-featured autonomous fuel-gauge. Instead it is * expected to be use in combination with some always on microcontroller reading * its coulomb-counter before it can wrap (must be read every 400 seconds!). * * Since Linux does not monitor coulomb-counter changes while the device * is off or suspended, the coulomb counter is not used atm. * * Possible improvements: * 1. Activate commented out total_coulomb_count code * 2. Reset total_coulomb_count val to 0 when the battery is as good as empty * and remember that we did this (and clear the flag for this on susp/resume) * 3. When the battery is full check if the flag that we set total_coulomb_count * to when the battery was empty is set. If so we now know the capacity, * not the design, but actual capacity, of the battery * 4. Add some mechanism (needs userspace help, or maybe use efivar?) to remember * the actual capacity of the battery over reboots * 5. When we know the actual capacity at probe time, add energy_now and * energy_full attributes. Guess boot + resume energy_now value based on ocv * and then use total_coulomb_count to report energy_now over time, resetting * things to adjust for drift when empty/full. This should give more accurate * readings, esp. in the 30-70% range and allow userspace to estimate time * remaining till empty/full * 6. Maybe unregister + reregister the psy device when we learn the actual * capacity during run-time ? * * The above will also require some sort of mwh_per_unit calculation. Testing * has shown that an estimated 7404mWh increase of the battery's energy results * in a total_coulomb_count increase of 3277 units with a 5 milli-ohm sense R. * * Copyright (C) 2021 Hans de Goede <hdegoede@redhat.com>
*/
struct ug3105_chip { struct i2c_client *client; struct power_supply *psy; struct delayed_work work; struct mutex lock; int ocv[UG3105_MOV_AVG_WINDOW]; /* micro-volt */ int intern_res[UG3105_MOV_AVG_WINDOW]; /* milli-ohm */ int poll_count; int ocv_avg_index; int ocv_avg; /* micro-volt */ int intern_res_poll_count; int intern_res_avg_index; int intern_res_avg; /* milli-ohm */ int volt; /* micro-volt */ int curr; /* micro-ampere */ int total_coulomb_count; int uv_per_unit; int ua_per_unit; int status; int capacity; bool supplied;
};
staticint ug3105_read_word(struct i2c_client *client, u8 reg)
{ int val;
val = i2c_smbus_read_word_data(client, reg); if (val < 0)
dev_err(&client->dev, "Error reading reg 0x%02x\n", reg);
return val;
}
staticint ug3105_get_status(struct ug3105_chip *chip)
{ int full = chip->psy->battery_info->constant_charge_voltage_max_uv -
UG3105_FULL_BAT_HYST_UV;
if (chip->curr > UG3105_CURR_HYST_UA) return POWER_SUPPLY_STATUS_CHARGING;
if (chip->curr < -UG3105_CURR_HYST_UA) return POWER_SUPPLY_STATUS_DISCHARGING;
if (chip->supplied && chip->ocv_avg > full) return POWER_SUPPLY_STATUS_FULL;
return POWER_SUPPLY_STATUS_NOT_CHARGING;
}
staticvoid ug3105_work(struct work_struct *work)
{ struct ug3105_chip *chip = container_of(work, struct ug3105_chip,
work.work); int i, val, curr_diff, volt_diff, res, win_size; bool prev_supplied = chip->supplied; int prev_status = chip->status; int prev_volt = chip->volt; int prev_curr = chip->curr; struct power_supply *psy;
mutex_lock(&chip->lock);
psy = chip->psy; if (!psy) goto out;
val = ug3105_read_word(chip->client, UG3105_REG_BAT_VOLT); if (val < 0) goto out;
chip->volt = val * chip->uv_per_unit;
val = ug3105_read_word(chip->client, UG3105_REG_BAT_CURR); if (val < 0) goto out;
chip->curr = (s16)val * chip->ua_per_unit;
/* * Skip internal resistance calc on charger [un]plug and * when the battery is almost empty (voltage low).
*/ if (chip->supplied != prev_supplied ||
chip->volt < UG3105_LOW_BAT_UV ||
chip->poll_count < 2) goto out;
/* * Assuming that the OCV voltage does not change significantly * between 2 polls, then we can calculate the internal resistance * on a significant current change by attributing all voltage * change between the 2 readings to the internal resistance.
*/
curr_diff = abs(chip->curr - prev_curr); if (curr_diff < UG3105_CURR_HYST_UA) goto out;
/* * DAC maximum is 4.5V divided by 65536 steps + an unknown factor of 10 * coming from somewhere for some reason (verified with a volt-meter).
*/
chip->uv_per_unit = 45000000/65536; /* Datasheet says 8.1 uV per unit for the current ADC */
chip->ua_per_unit = 8100000 / curr_sense_res_uohm;
/* Use provided internal resistance as start point (in milli-ohm) */
chip->intern_res_avg = psy->battery_info->factory_internal_resistance_uohm / 1000; /* Also add it to the internal resistance moving average window */
chip->intern_res[0] = chip->intern_res_avg;
chip->intern_res_avg_index = 1;
chip->intern_res_poll_count = 1;
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.