|
| 1 | +// SPDX-License-Identifier: GPL-2.0 |
| 2 | +/* |
| 3 | + * Battery Fuel Gauge Driver for Samsung S2MU005 PMIC. |
| 4 | + * |
| 5 | + * Copyright (C) 2015 Samsung Electronics |
| 6 | + * Copyright (C) 2023 Yassine Oudjana <y.oudjana@protonmail.com> |
| 7 | + * Copyright (C) 2025 Kaustabh Chakraborty <kauschluss@disroot.org> |
| 8 | + */ |
| 9 | + |
| 10 | +#include <linux/delay.h> |
| 11 | +#include <linux/i2c.h> |
| 12 | +#include <linux/interrupt.h> |
| 13 | +#include <linux/mod_devicetable.h> |
| 14 | +#include <linux/mutex.h> |
| 15 | +#include <linux/power_supply.h> |
| 16 | +#include <linux/property.h> |
| 17 | +#include <linux/regmap.h> |
| 18 | +#include <linux/units.h> |
| 19 | + |
| 20 | +#define S2MU005_FG_REG_STATUS 0x00 |
| 21 | +#define S2MU005_FG_REG_IRQ 0x02 |
| 22 | +#define S2MU005_FG_REG_RVBAT 0x04 |
| 23 | +#define S2MU005_FG_REG_RCURCC 0x06 |
| 24 | +#define S2MU005_FG_REG_RSOC 0x08 |
| 25 | +#define S2MU005_FG_REG_MONOUT 0x0a |
| 26 | +#define S2MU005_FG_REG_MONOUTSEL 0x0c |
| 27 | +#define S2MU005_FG_REG_RBATCAP 0x0e |
| 28 | +#define S2MU005_FG_REG_RZADJ 0x12 |
| 29 | +#define S2MU005_FG_REG_RBATZ0 0x16 |
| 30 | +#define S2MU005_FG_REG_RBATZ1 0x18 |
| 31 | +#define S2MU005_FG_REG_IRQLVL 0x1a |
| 32 | +#define S2MU005_FG_REG_START 0x1e |
| 33 | + |
| 34 | +#define S2MU005_FG_MONOUTSEL_AVGCURRENT 0x26 |
| 35 | +#define S2MU005_FG_MONOUTSEL_AVGVOLTAGE 0x27 |
| 36 | + |
| 37 | +struct s2mu005_fg { |
| 38 | + struct device *dev; |
| 39 | + struct regmap *regmap; |
| 40 | + struct power_supply *psy; |
| 41 | + struct mutex monout_mutex; |
| 42 | +}; |
| 43 | + |
| 44 | +static const struct regmap_config s2mu005_fg_regmap_config = { |
| 45 | + .reg_bits = 8, |
| 46 | + .val_bits = 16, |
| 47 | + .val_format_endian = REGMAP_ENDIAN_LITTLE, |
| 48 | +}; |
| 49 | + |
| 50 | +static irqreturn_t s2mu005_handle_irq(int irq, void *data) |
| 51 | +{ |
| 52 | + struct s2mu005_fg *priv = data; |
| 53 | + |
| 54 | + msleep(100); |
| 55 | + power_supply_changed(priv->psy); |
| 56 | + |
| 57 | + return IRQ_HANDLED; |
| 58 | +} |
| 59 | + |
| 60 | +static int s2mu005_fg_get_voltage_now(struct s2mu005_fg *priv, int *value) |
| 61 | +{ |
| 62 | + struct regmap *regmap = priv->regmap; |
| 63 | + u32 val; |
| 64 | + int ret; |
| 65 | + |
| 66 | + ret = regmap_read(regmap, S2MU005_FG_REG_RVBAT, &val); |
| 67 | + if (ret < 0) { |
| 68 | + dev_err(priv->dev, "failed to read voltage register (%d)\n", ret); |
| 69 | + return ret; |
| 70 | + } |
| 71 | + |
| 72 | + *value = (val * MICRO) >> 13; |
| 73 | + |
| 74 | + return 0; |
| 75 | +} |
| 76 | + |
| 77 | +static int s2mu005_fg_get_voltage_avg(struct s2mu005_fg *priv, int *value) |
| 78 | +{ |
| 79 | + struct regmap *regmap = priv->regmap; |
| 80 | + u32 val; |
| 81 | + int ret; |
| 82 | + |
| 83 | + mutex_lock(&priv->monout_mutex); |
| 84 | + |
| 85 | + ret = regmap_write(regmap, S2MU005_FG_REG_MONOUTSEL, |
| 86 | + S2MU005_FG_MONOUTSEL_AVGVOLTAGE); |
| 87 | + if (ret < 0) { |
| 88 | + dev_err(priv->dev, "failed to enable average voltage monitoring (%d)\n", |
| 89 | + ret); |
| 90 | + goto unlock; |
| 91 | + } |
| 92 | + |
| 93 | + ret = regmap_read(regmap, S2MU005_FG_REG_MONOUT, &val); |
| 94 | + if (ret < 0) { |
| 95 | + dev_err(priv->dev, "failed to read current register (%d)\n", ret); |
| 96 | + goto unlock; |
| 97 | + } |
| 98 | + |
| 99 | + *value = (val * MICRO) >> 12; |
| 100 | + |
| 101 | +unlock: |
| 102 | + mutex_unlock(&priv->monout_mutex); |
| 103 | + |
| 104 | + return ret; |
| 105 | +} |
| 106 | +static int s2mu005_fg_get_current_now(struct s2mu005_fg *priv, int *value) |
| 107 | +{ |
| 108 | + struct regmap *regmap = priv->regmap; |
| 109 | + u32 val; |
| 110 | + int ret; |
| 111 | + |
| 112 | + ret = regmap_read(regmap, S2MU005_FG_REG_RCURCC, &val); |
| 113 | + if (ret < 0) { |
| 114 | + dev_err(priv->dev, "failed to read current register (%d)\n", ret); |
| 115 | + return ret; |
| 116 | + } |
| 117 | + |
| 118 | + *value = -((s16)val * MICRO) >> 12; |
| 119 | + |
| 120 | + return 0; |
| 121 | +} |
| 122 | + |
| 123 | +static int s2mu005_fg_get_current_avg(struct s2mu005_fg *priv, int *value) |
| 124 | +{ |
| 125 | + struct regmap *regmap = priv->regmap; |
| 126 | + u32 val; |
| 127 | + int ret; |
| 128 | + |
| 129 | + mutex_lock(&priv->monout_mutex); |
| 130 | + |
| 131 | + ret = regmap_write(regmap, S2MU005_FG_REG_MONOUTSEL, |
| 132 | + S2MU005_FG_MONOUTSEL_AVGCURRENT); |
| 133 | + if (ret < 0) { |
| 134 | + dev_err(priv->dev, "failed to enable average current monitoring (%d)\n", |
| 135 | + ret); |
| 136 | + goto unlock; |
| 137 | + } |
| 138 | + |
| 139 | + ret = regmap_read(regmap, S2MU005_FG_REG_MONOUT, &val); |
| 140 | + if (ret < 0) { |
| 141 | + dev_err(priv->dev, "failed to read current register (%d)\n", ret); |
| 142 | + goto unlock; |
| 143 | + } |
| 144 | + |
| 145 | + *value = -((s16)val * MICRO) >> 12; |
| 146 | + |
| 147 | +unlock: |
| 148 | + mutex_unlock(&priv->monout_mutex); |
| 149 | + |
| 150 | + return ret; |
| 151 | +} |
| 152 | + |
| 153 | +static int s2mu005_fg_get_capacity(struct s2mu005_fg *priv, int *value) |
| 154 | +{ |
| 155 | + struct regmap *regmap = priv->regmap; |
| 156 | + u32 val; |
| 157 | + int ret; |
| 158 | + |
| 159 | + ret = regmap_read(regmap, S2MU005_FG_REG_RSOC, &val); |
| 160 | + if (ret < 0) { |
| 161 | + dev_err(priv->dev, "failed to read capacity register (%d)\n", ret); |
| 162 | + return ret; |
| 163 | + } |
| 164 | + |
| 165 | + *value = (val * CENTI) >> 14; |
| 166 | + |
| 167 | + return 0; |
| 168 | +} |
| 169 | + |
| 170 | +static int s2mu005_fg_get_status(struct s2mu005_fg *priv, int *value) |
| 171 | +{ |
| 172 | + int current_now, current_avg, capacity; |
| 173 | + int ret; |
| 174 | + |
| 175 | + ret = s2mu005_fg_get_current_now(priv, ¤t_now); |
| 176 | + if (ret < 0) |
| 177 | + return ret; |
| 178 | + |
| 179 | + ret = s2mu005_fg_get_current_avg(priv, ¤t_avg); |
| 180 | + if (ret < 0) |
| 181 | + return ret; |
| 182 | + |
| 183 | + /* |
| 184 | + * Verify both current values reported to reduce inaccuracies due to |
| 185 | + * internal hysteresis. |
| 186 | + */ |
| 187 | + if (current_now < 0 && current_avg < 0) { |
| 188 | + *value = POWER_SUPPLY_STATUS_DISCHARGING; |
| 189 | + } else if (current_now == 0) { |
| 190 | + *value = POWER_SUPPLY_STATUS_NOT_CHARGING; |
| 191 | + } else { |
| 192 | + *value = POWER_SUPPLY_STATUS_CHARGING; |
| 193 | + |
| 194 | + ret = s2mu005_fg_get_capacity(priv, &capacity); |
| 195 | + if (!ret && capacity > 98) |
| 196 | + *value = POWER_SUPPLY_STATUS_FULL; |
| 197 | + return ret; |
| 198 | + } |
| 199 | + |
| 200 | + return 0; |
| 201 | +} |
| 202 | + |
| 203 | +static const enum power_supply_property s2mu005_fg_properties[] = { |
| 204 | + POWER_SUPPLY_PROP_VOLTAGE_NOW, |
| 205 | + POWER_SUPPLY_PROP_VOLTAGE_AVG, |
| 206 | + POWER_SUPPLY_PROP_CURRENT_NOW, |
| 207 | + POWER_SUPPLY_PROP_CURRENT_AVG, |
| 208 | + POWER_SUPPLY_PROP_CAPACITY, |
| 209 | + POWER_SUPPLY_PROP_STATUS, |
| 210 | +}; |
| 211 | + |
| 212 | +static int s2mu005_fg_get_property(struct power_supply *psy, |
| 213 | + enum power_supply_property psp, |
| 214 | + union power_supply_propval *val) |
| 215 | +{ |
| 216 | + struct s2mu005_fg *priv = power_supply_get_drvdata(psy); |
| 217 | + |
| 218 | + switch (psp) { |
| 219 | + case POWER_SUPPLY_PROP_VOLTAGE_NOW: |
| 220 | + return s2mu005_fg_get_voltage_now(priv, &val->intval); |
| 221 | + case POWER_SUPPLY_PROP_VOLTAGE_AVG: |
| 222 | + return s2mu005_fg_get_voltage_avg(priv, &val->intval); |
| 223 | + case POWER_SUPPLY_PROP_CURRENT_NOW: |
| 224 | + return s2mu005_fg_get_current_now(priv, &val->intval); |
| 225 | + case POWER_SUPPLY_PROP_CURRENT_AVG: |
| 226 | + return s2mu005_fg_get_current_avg(priv, &val->intval); |
| 227 | + case POWER_SUPPLY_PROP_CAPACITY: |
| 228 | + return s2mu005_fg_get_capacity(priv, &val->intval); |
| 229 | + case POWER_SUPPLY_PROP_STATUS: |
| 230 | + return s2mu005_fg_get_status(priv, &val->intval); |
| 231 | + default: |
| 232 | + return -EINVAL; |
| 233 | + } |
| 234 | +} |
| 235 | + |
| 236 | +static const struct power_supply_desc s2mu005_fg_desc = { |
| 237 | + .name = "s2mu005-fuel-gauge", |
| 238 | + .type = POWER_SUPPLY_TYPE_BATTERY, |
| 239 | + .properties = s2mu005_fg_properties, |
| 240 | + .num_properties = ARRAY_SIZE(s2mu005_fg_properties), |
| 241 | + .get_property = s2mu005_fg_get_property, |
| 242 | +}; |
| 243 | + |
| 244 | +static int s2mu005_fg_i2c_probe(struct i2c_client *client) |
| 245 | +{ |
| 246 | + struct device *dev = &client->dev; |
| 247 | + struct s2mu005_fg *priv; |
| 248 | + struct power_supply_config psy_cfg = {}; |
| 249 | + const struct power_supply_desc *psy_desc; |
| 250 | + int ret; |
| 251 | + |
| 252 | + priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL); |
| 253 | + if (!priv) |
| 254 | + return -ENOMEM; |
| 255 | + |
| 256 | + dev_set_drvdata(dev, priv); |
| 257 | + priv->dev = dev; |
| 258 | + |
| 259 | + priv->regmap = devm_regmap_init_i2c(client, &s2mu005_fg_regmap_config); |
| 260 | + if (IS_ERR(priv->regmap)) |
| 261 | + return dev_err_probe(dev, PTR_ERR(priv->regmap), |
| 262 | + "failed to initialize regmap\n"); |
| 263 | + |
| 264 | + ret = devm_mutex_init(dev, &priv->monout_mutex); |
| 265 | + if (ret) |
| 266 | + dev_err_probe(dev, ret, "failed to initialize MONOUT mutex\n"); |
| 267 | + |
| 268 | + psy_desc = device_get_match_data(dev); |
| 269 | + |
| 270 | + psy_cfg.drv_data = priv; |
| 271 | + psy_cfg.fwnode = dev_fwnode(dev); |
| 272 | + priv->psy = devm_power_supply_register(priv->dev, psy_desc, &psy_cfg); |
| 273 | + if (IS_ERR(priv->psy)) |
| 274 | + return dev_err_probe(dev, PTR_ERR(priv->psy), |
| 275 | + "failed to register power supply subsystem\n"); |
| 276 | + |
| 277 | + ret = devm_request_threaded_irq(priv->dev, client->irq, NULL, |
| 278 | + s2mu005_handle_irq, IRQF_ONESHOT, |
| 279 | + psy_desc->name, priv); |
| 280 | + if (ret) |
| 281 | + dev_err_probe(dev, ret, "failed to request IRQ\n"); |
| 282 | + |
| 283 | + return 0; |
| 284 | +} |
| 285 | + |
| 286 | +static const struct of_device_id s2mu005_fg_of_match_table[] = { |
| 287 | + { |
| 288 | + .compatible = "samsung,s2mu005-fuel-gauge", |
| 289 | + .data = &s2mu005_fg_desc, |
| 290 | + }, |
| 291 | + { } |
| 292 | +}; |
| 293 | +MODULE_DEVICE_TABLE(of, s2mu005_fg_of_match_table); |
| 294 | + |
| 295 | +static struct i2c_driver s2mu005_fg_i2c_driver = { |
| 296 | + .probe = s2mu005_fg_i2c_probe, |
| 297 | + .driver = { |
| 298 | + .name = "s2mu005-fuel-gauge", |
| 299 | + .of_match_table = s2mu005_fg_of_match_table, |
| 300 | + }, |
| 301 | +}; |
| 302 | +module_i2c_driver(s2mu005_fg_i2c_driver); |
| 303 | + |
| 304 | +MODULE_DESCRIPTION("Samsung S2MU005 PMIC Battery Fuel Gauge Driver"); |
| 305 | +MODULE_AUTHOR("Yassine Oudjana <y.oudjana@protonmail.com>"); |
| 306 | +MODULE_AUTHOR("Kaustabh Chakraborty <kauschluss@disroot.org>"); |
| 307 | +MODULE_LICENSE("GPL"); |
0 commit comments