1 // SPDX-License-Identifier: GPL-2.0-only
3 * I2C client/driver for the Maxim/Dallas DS2782 Stand-Alone Fuel Gauge IC
5 * Copyright (C) 2009 Bluewater Systems Ltd
9 * DS2786 added by Yulia Vilensky <vilensky@compulab.co.il>
11 * UEvent sending added by Evgeny Romanov <romanov@neurosoft.ru>
14 #include <linux/kernel.h>
15 #include <linux/module.h>
16 #include <linux/types.h>
17 #include <linux/errno.h>
18 #include <linux/swab.h>
19 #include <linux/i2c.h>
20 #include <linux/delay.h>
21 #include <linux/idr.h>
22 #include <linux/power_supply.h>
23 #include <linux/slab.h>
24 #include <linux/ds2782_battery.h>
26 #define DS2782_REG_RARC 0x06 /* Remaining active relative capacity */
28 #define DS278x_REG_VOLT_MSB 0x0c
29 #define DS278x_REG_TEMP_MSB 0x0a
30 #define DS278x_REG_CURRENT_MSB 0x0e
33 #define DS2782_REG_RSNSP 0x69 /* Sense resistor value */
35 /* Current unit measurement in uA for a 1 milli-ohm sense resistor */
36 #define DS2782_CURRENT_UNITS 1563
38 #define DS2786_REG_RARC 0x02 /* Remaining active relative capacity */
40 #define DS2786_CURRENT_UNITS 25
42 #define DS278x_DELAY 1000
46 struct ds278x_battery_ops {
47 int (*get_battery_current)(struct ds278x_info *info, int *current_uA);
48 int (*get_battery_voltage)(struct ds278x_info *info, int *voltage_uV);
49 int (*get_battery_capacity)(struct ds278x_info *info, int *capacity);
52 #define to_ds278x_info(x) power_supply_get_drvdata(x)
55 struct i2c_client *client;
56 struct power_supply *battery;
57 struct power_supply_desc battery_desc;
58 const struct ds278x_battery_ops *ops;
59 struct delayed_work bat_work;
63 int status; /* State Of Charge */
66 static DEFINE_IDR(battery_id);
67 static DEFINE_MUTEX(battery_lock);
69 static inline int ds278x_read_reg(struct ds278x_info *info, int reg, u8 *val)
73 ret = i2c_smbus_read_byte_data(info->client, reg);
75 dev_err(&info->client->dev, "register read failed\n");
83 static inline int ds278x_read_reg16(struct ds278x_info *info, int reg_msb,
88 ret = i2c_smbus_read_word_data(info->client, reg_msb);
90 dev_err(&info->client->dev, "register read failed\n");
98 static int ds278x_get_temp(struct ds278x_info *info, int *temp)
104 * Temperature is measured in units of 0.125 degrees celcius, the
105 * power_supply class measures temperature in tenths of degrees
106 * celsius. The temperature value is stored as a 10 bit number, plus
107 * sign in the upper bits of a 16 bit register.
109 err = ds278x_read_reg16(info, DS278x_REG_TEMP_MSB, &raw);
112 *temp = ((raw / 32) * 125) / 100;
116 static int ds2782_get_current(struct ds278x_info *info, int *current_uA)
124 * The units of measurement for current are dependent on the value of
125 * the sense resistor.
127 err = ds278x_read_reg(info, DS2782_REG_RSNSP, &sense_res_raw);
130 if (sense_res_raw == 0) {
131 dev_err(&info->client->dev, "sense resistor value is 0\n");
134 sense_res = 1000 / sense_res_raw;
136 dev_dbg(&info->client->dev, "sense resistor = %d milli-ohms\n",
138 err = ds278x_read_reg16(info, DS278x_REG_CURRENT_MSB, &raw);
141 *current_uA = raw * (DS2782_CURRENT_UNITS / sense_res);
145 static int ds2782_get_voltage(struct ds278x_info *info, int *voltage_uV)
151 * Voltage is measured in units of 4.88mV. The voltage is stored as
152 * a 10-bit number plus sign, in the upper bits of a 16-bit register
154 err = ds278x_read_reg16(info, DS278x_REG_VOLT_MSB, &raw);
157 *voltage_uV = (raw / 32) * 4800;
161 static int ds2782_get_capacity(struct ds278x_info *info, int *capacity)
166 err = ds278x_read_reg(info, DS2782_REG_RARC, &raw);
173 static int ds2786_get_current(struct ds278x_info *info, int *current_uA)
178 err = ds278x_read_reg16(info, DS278x_REG_CURRENT_MSB, &raw);
181 *current_uA = (raw / 16) * (DS2786_CURRENT_UNITS / info->rsns);
185 static int ds2786_get_voltage(struct ds278x_info *info, int *voltage_uV)
191 * Voltage is measured in units of 1.22mV. The voltage is stored as
192 * a 12-bit number plus sign, in the upper bits of a 16-bit register
194 err = ds278x_read_reg16(info, DS278x_REG_VOLT_MSB, &raw);
197 *voltage_uV = (raw / 8) * 1220;
201 static int ds2786_get_capacity(struct ds278x_info *info, int *capacity)
206 err = ds278x_read_reg(info, DS2786_REG_RARC, &raw);
209 /* Relative capacity is displayed with resolution 0.5 % */
214 static int ds278x_get_status(struct ds278x_info *info, int *status)
220 err = info->ops->get_battery_current(info, ¤t_uA);
224 err = info->ops->get_battery_capacity(info, &capacity);
228 info->capacity = capacity;
231 *status = POWER_SUPPLY_STATUS_FULL;
232 else if (current_uA == 0)
233 *status = POWER_SUPPLY_STATUS_NOT_CHARGING;
234 else if (current_uA < 0)
235 *status = POWER_SUPPLY_STATUS_DISCHARGING;
237 *status = POWER_SUPPLY_STATUS_CHARGING;
242 static int ds278x_battery_get_property(struct power_supply *psy,
243 enum power_supply_property prop,
244 union power_supply_propval *val)
246 struct ds278x_info *info = to_ds278x_info(psy);
250 case POWER_SUPPLY_PROP_STATUS:
251 ret = ds278x_get_status(info, &val->intval);
254 case POWER_SUPPLY_PROP_CAPACITY:
255 ret = info->ops->get_battery_capacity(info, &val->intval);
258 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
259 ret = info->ops->get_battery_voltage(info, &val->intval);
262 case POWER_SUPPLY_PROP_CURRENT_NOW:
263 ret = info->ops->get_battery_current(info, &val->intval);
266 case POWER_SUPPLY_PROP_TEMP:
267 ret = ds278x_get_temp(info, &val->intval);
277 static void ds278x_bat_update(struct ds278x_info *info)
279 int old_status = info->status;
280 int old_capacity = info->capacity;
282 ds278x_get_status(info, &info->status);
284 if ((old_status != info->status) || (old_capacity != info->capacity))
285 power_supply_changed(info->battery);
288 static void ds278x_bat_work(struct work_struct *work)
290 struct ds278x_info *info;
292 info = container_of(work, struct ds278x_info, bat_work.work);
293 ds278x_bat_update(info);
295 schedule_delayed_work(&info->bat_work, DS278x_DELAY);
298 static enum power_supply_property ds278x_battery_props[] = {
299 POWER_SUPPLY_PROP_STATUS,
300 POWER_SUPPLY_PROP_CAPACITY,
301 POWER_SUPPLY_PROP_VOLTAGE_NOW,
302 POWER_SUPPLY_PROP_CURRENT_NOW,
303 POWER_SUPPLY_PROP_TEMP,
306 static void ds278x_power_supply_init(struct power_supply_desc *battery)
308 battery->type = POWER_SUPPLY_TYPE_BATTERY;
309 battery->properties = ds278x_battery_props;
310 battery->num_properties = ARRAY_SIZE(ds278x_battery_props);
311 battery->get_property = ds278x_battery_get_property;
312 battery->external_power_changed = NULL;
315 static void ds278x_battery_remove(struct i2c_client *client)
317 struct ds278x_info *info = i2c_get_clientdata(client);
320 power_supply_unregister(info->battery);
321 cancel_delayed_work_sync(&info->bat_work);
322 kfree(info->battery_desc.name);
325 mutex_lock(&battery_lock);
326 idr_remove(&battery_id, id);
327 mutex_unlock(&battery_lock);
330 #ifdef CONFIG_PM_SLEEP
332 static int ds278x_suspend(struct device *dev)
334 struct i2c_client *client = to_i2c_client(dev);
335 struct ds278x_info *info = i2c_get_clientdata(client);
337 cancel_delayed_work(&info->bat_work);
341 static int ds278x_resume(struct device *dev)
343 struct i2c_client *client = to_i2c_client(dev);
344 struct ds278x_info *info = i2c_get_clientdata(client);
346 schedule_delayed_work(&info->bat_work, DS278x_DELAY);
349 #endif /* CONFIG_PM_SLEEP */
351 static SIMPLE_DEV_PM_OPS(ds278x_battery_pm_ops, ds278x_suspend, ds278x_resume);
358 static const struct ds278x_battery_ops ds278x_ops[] = {
360 .get_battery_current = ds2782_get_current,
361 .get_battery_voltage = ds2782_get_voltage,
362 .get_battery_capacity = ds2782_get_capacity,
365 .get_battery_current = ds2786_get_current,
366 .get_battery_voltage = ds2786_get_voltage,
367 .get_battery_capacity = ds2786_get_capacity,
371 static int ds278x_battery_probe(struct i2c_client *client)
373 const struct i2c_device_id *id = i2c_client_get_device_id(client);
374 struct ds278x_platform_data *pdata = client->dev.platform_data;
375 struct power_supply_config psy_cfg = {};
376 struct ds278x_info *info;
381 * ds2786 should have the sense resistor value set
382 * in the platform data
384 if (id->driver_data == DS2786 && !pdata) {
385 dev_err(&client->dev, "missing platform data for ds2786\n");
389 /* Get an ID for this battery */
390 mutex_lock(&battery_lock);
391 ret = idr_alloc(&battery_id, client, 0, 0, GFP_KERNEL);
392 mutex_unlock(&battery_lock);
397 info = kzalloc(sizeof(*info), GFP_KERNEL);
403 info->battery_desc.name = kasprintf(GFP_KERNEL, "%s-%d",
405 if (!info->battery_desc.name) {
410 if (id->driver_data == DS2786)
411 info->rsns = pdata->rsns;
413 i2c_set_clientdata(client, info);
414 info->client = client;
416 info->ops = &ds278x_ops[id->driver_data];
417 ds278x_power_supply_init(&info->battery_desc);
418 psy_cfg.drv_data = info;
420 info->capacity = 100;
421 info->status = POWER_SUPPLY_STATUS_FULL;
423 INIT_DELAYED_WORK(&info->bat_work, ds278x_bat_work);
425 info->battery = power_supply_register(&client->dev,
426 &info->battery_desc, &psy_cfg);
427 if (IS_ERR(info->battery)) {
428 dev_err(&client->dev, "failed to register battery\n");
429 ret = PTR_ERR(info->battery);
432 schedule_delayed_work(&info->bat_work, DS278x_DELAY);
438 kfree(info->battery_desc.name);
442 mutex_lock(&battery_lock);
443 idr_remove(&battery_id, num);
444 mutex_unlock(&battery_lock);
449 static const struct i2c_device_id ds278x_id[] = {
454 MODULE_DEVICE_TABLE(i2c, ds278x_id);
456 static struct i2c_driver ds278x_battery_driver = {
458 .name = "ds2782-battery",
459 .pm = &ds278x_battery_pm_ops,
461 .probe = ds278x_battery_probe,
462 .remove = ds278x_battery_remove,
463 .id_table = ds278x_id,
465 module_i2c_driver(ds278x_battery_driver);
467 MODULE_AUTHOR("Ryan Mallon");
468 MODULE_DESCRIPTION("Maxim/Dallas DS2782 Stand-Alone Fuel Gauge IC driver");
469 MODULE_LICENSE("GPL");