2 * I2C client/driver for the Maxim/Dallas DS2782 Stand-Alone Fuel Gauge IC
4 * Copyright (C) 2009 Bluewater Systems Ltd
8 * DS2786 added by Yulia Vilensky <vilensky@compulab.co.il>
10 * UEvent sending added by Evgeny Romanov <romanov@neurosoft.ru>
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License version 2 as
14 * published by the Free Software Foundation.
18 #include <linux/kernel.h>
19 #include <linux/module.h>
20 #include <linux/types.h>
21 #include <linux/errno.h>
22 #include <linux/swab.h>
23 #include <linux/i2c.h>
24 #include <linux/delay.h>
25 #include <linux/idr.h>
26 #include <linux/power_supply.h>
27 #include <linux/slab.h>
28 #include <linux/ds2782_battery.h>
30 #define DS2782_REG_RARC 0x06 /* Remaining active relative capacity */
32 #define DS278x_REG_VOLT_MSB 0x0c
33 #define DS278x_REG_TEMP_MSB 0x0a
34 #define DS278x_REG_CURRENT_MSB 0x0e
37 #define DS2782_REG_RSNSP 0x69 /* Sense resistor value */
39 /* Current unit measurement in uA for a 1 milli-ohm sense resistor */
40 #define DS2782_CURRENT_UNITS 1563
42 #define DS2786_REG_RARC 0x02 /* Remaining active relative capacity */
44 #define DS2786_CURRENT_UNITS 25
46 #define DS278x_DELAY 1000
50 struct ds278x_battery_ops {
51 int (*get_battery_current)(struct ds278x_info *info, int *current_uA);
52 int (*get_battery_voltage)(struct ds278x_info *info, int *voltage_uV);
53 int (*get_battery_capacity)(struct ds278x_info *info, int *capacity);
56 #define to_ds278x_info(x) power_supply_get_drvdata(x)
59 struct i2c_client *client;
60 struct power_supply *battery;
61 struct power_supply_desc battery_desc;
62 struct ds278x_battery_ops *ops;
63 struct delayed_work bat_work;
67 int status; /* State Of Charge */
70 static DEFINE_IDR(battery_id);
71 static DEFINE_MUTEX(battery_lock);
73 static inline int ds278x_read_reg(struct ds278x_info *info, int reg, u8 *val)
77 ret = i2c_smbus_read_byte_data(info->client, reg);
79 dev_err(&info->client->dev, "register read failed\n");
87 static inline int ds278x_read_reg16(struct ds278x_info *info, int reg_msb,
92 ret = i2c_smbus_read_word_data(info->client, reg_msb);
94 dev_err(&info->client->dev, "register read failed\n");
102 static int ds278x_get_temp(struct ds278x_info *info, int *temp)
108 * Temperature is measured in units of 0.125 degrees celcius, the
109 * power_supply class measures temperature in tenths of degrees
110 * celsius. The temperature value is stored as a 10 bit number, plus
111 * sign in the upper bits of a 16 bit register.
113 err = ds278x_read_reg16(info, DS278x_REG_TEMP_MSB, &raw);
116 *temp = ((raw / 32) * 125) / 100;
120 static int ds2782_get_current(struct ds278x_info *info, int *current_uA)
128 * The units of measurement for current are dependent on the value of
129 * the sense resistor.
131 err = ds278x_read_reg(info, DS2782_REG_RSNSP, &sense_res_raw);
134 if (sense_res_raw == 0) {
135 dev_err(&info->client->dev, "sense resistor value is 0\n");
138 sense_res = 1000 / sense_res_raw;
140 dev_dbg(&info->client->dev, "sense resistor = %d milli-ohms\n",
142 err = ds278x_read_reg16(info, DS278x_REG_CURRENT_MSB, &raw);
145 *current_uA = raw * (DS2782_CURRENT_UNITS / sense_res);
149 static int ds2782_get_voltage(struct ds278x_info *info, int *voltage_uV)
155 * Voltage is measured in units of 4.88mV. The voltage is stored as
156 * a 10-bit number plus sign, in the upper bits of a 16-bit register
158 err = ds278x_read_reg16(info, DS278x_REG_VOLT_MSB, &raw);
161 *voltage_uV = (raw / 32) * 4800;
165 static int ds2782_get_capacity(struct ds278x_info *info, int *capacity)
170 err = ds278x_read_reg(info, DS2782_REG_RARC, &raw);
177 static int ds2786_get_current(struct ds278x_info *info, int *current_uA)
182 err = ds278x_read_reg16(info, DS278x_REG_CURRENT_MSB, &raw);
185 *current_uA = (raw / 16) * (DS2786_CURRENT_UNITS / info->rsns);
189 static int ds2786_get_voltage(struct ds278x_info *info, int *voltage_uV)
195 * Voltage is measured in units of 1.22mV. The voltage is stored as
196 * a 12-bit number plus sign, in the upper bits of a 16-bit register
198 err = ds278x_read_reg16(info, DS278x_REG_VOLT_MSB, &raw);
201 *voltage_uV = (raw / 8) * 1220;
205 static int ds2786_get_capacity(struct ds278x_info *info, int *capacity)
210 err = ds278x_read_reg(info, DS2786_REG_RARC, &raw);
213 /* Relative capacity is displayed with resolution 0.5 % */
218 static int ds278x_get_status(struct ds278x_info *info, int *status)
224 err = info->ops->get_battery_current(info, ¤t_uA);
228 err = info->ops->get_battery_capacity(info, &capacity);
232 info->capacity = capacity;
235 *status = POWER_SUPPLY_STATUS_FULL;
236 else if (current_uA == 0)
237 *status = POWER_SUPPLY_STATUS_NOT_CHARGING;
238 else if (current_uA < 0)
239 *status = POWER_SUPPLY_STATUS_DISCHARGING;
241 *status = POWER_SUPPLY_STATUS_CHARGING;
246 static int ds278x_battery_get_property(struct power_supply *psy,
247 enum power_supply_property prop,
248 union power_supply_propval *val)
250 struct ds278x_info *info = to_ds278x_info(psy);
254 case POWER_SUPPLY_PROP_STATUS:
255 ret = ds278x_get_status(info, &val->intval);
258 case POWER_SUPPLY_PROP_CAPACITY:
259 ret = info->ops->get_battery_capacity(info, &val->intval);
262 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
263 ret = info->ops->get_battery_voltage(info, &val->intval);
266 case POWER_SUPPLY_PROP_CURRENT_NOW:
267 ret = info->ops->get_battery_current(info, &val->intval);
270 case POWER_SUPPLY_PROP_TEMP:
271 ret = ds278x_get_temp(info, &val->intval);
281 static void ds278x_bat_update(struct ds278x_info *info)
283 int old_status = info->status;
284 int old_capacity = info->capacity;
286 ds278x_get_status(info, &info->status);
288 if ((old_status != info->status) || (old_capacity != info->capacity))
289 power_supply_changed(info->battery);
292 static void ds278x_bat_work(struct work_struct *work)
294 struct ds278x_info *info;
296 info = container_of(work, struct ds278x_info, bat_work.work);
297 ds278x_bat_update(info);
299 schedule_delayed_work(&info->bat_work, DS278x_DELAY);
302 static enum power_supply_property ds278x_battery_props[] = {
303 POWER_SUPPLY_PROP_STATUS,
304 POWER_SUPPLY_PROP_CAPACITY,
305 POWER_SUPPLY_PROP_VOLTAGE_NOW,
306 POWER_SUPPLY_PROP_CURRENT_NOW,
307 POWER_SUPPLY_PROP_TEMP,
310 static void ds278x_power_supply_init(struct power_supply_desc *battery)
312 battery->type = POWER_SUPPLY_TYPE_BATTERY;
313 battery->properties = ds278x_battery_props;
314 battery->num_properties = ARRAY_SIZE(ds278x_battery_props);
315 battery->get_property = ds278x_battery_get_property;
316 battery->external_power_changed = NULL;
319 static int ds278x_battery_remove(struct i2c_client *client)
321 struct ds278x_info *info = i2c_get_clientdata(client);
323 power_supply_unregister(info->battery);
324 kfree(info->battery_desc.name);
326 mutex_lock(&battery_lock);
327 idr_remove(&battery_id, info->id);
328 mutex_unlock(&battery_lock);
330 cancel_delayed_work(&info->bat_work);
336 #ifdef CONFIG_PM_SLEEP
338 static int ds278x_suspend(struct device *dev)
340 struct i2c_client *client = to_i2c_client(dev);
341 struct ds278x_info *info = i2c_get_clientdata(client);
343 cancel_delayed_work(&info->bat_work);
347 static int ds278x_resume(struct device *dev)
349 struct i2c_client *client = to_i2c_client(dev);
350 struct ds278x_info *info = i2c_get_clientdata(client);
352 schedule_delayed_work(&info->bat_work, DS278x_DELAY);
355 #endif /* CONFIG_PM_SLEEP */
357 static SIMPLE_DEV_PM_OPS(ds278x_battery_pm_ops, ds278x_suspend, ds278x_resume);
364 static struct ds278x_battery_ops ds278x_ops[] = {
366 .get_battery_current = ds2782_get_current,
367 .get_battery_voltage = ds2782_get_voltage,
368 .get_battery_capacity = ds2782_get_capacity,
371 .get_battery_current = ds2786_get_current,
372 .get_battery_voltage = ds2786_get_voltage,
373 .get_battery_capacity = ds2786_get_capacity,
377 static int ds278x_battery_probe(struct i2c_client *client,
378 const struct i2c_device_id *id)
380 struct ds278x_platform_data *pdata = client->dev.platform_data;
381 struct power_supply_config psy_cfg = {};
382 struct ds278x_info *info;
387 * ds2786 should have the sense resistor value set
388 * in the platform data
390 if (id->driver_data == DS2786 && !pdata) {
391 dev_err(&client->dev, "missing platform data for ds2786\n");
395 /* Get an ID for this battery */
396 mutex_lock(&battery_lock);
397 ret = idr_alloc(&battery_id, client, 0, 0, GFP_KERNEL);
398 mutex_unlock(&battery_lock);
403 info = kzalloc(sizeof(*info), GFP_KERNEL);
409 info->battery_desc.name = kasprintf(GFP_KERNEL, "%s-%d",
411 if (!info->battery_desc.name) {
416 if (id->driver_data == DS2786)
417 info->rsns = pdata->rsns;
419 i2c_set_clientdata(client, info);
420 info->client = client;
422 info->ops = &ds278x_ops[id->driver_data];
423 ds278x_power_supply_init(&info->battery_desc);
424 psy_cfg.drv_data = info;
426 info->capacity = 100;
427 info->status = POWER_SUPPLY_STATUS_FULL;
429 INIT_DELAYED_WORK(&info->bat_work, ds278x_bat_work);
431 info->battery = power_supply_register(&client->dev,
432 &info->battery_desc, &psy_cfg);
433 if (IS_ERR(info->battery)) {
434 dev_err(&client->dev, "failed to register battery\n");
435 ret = PTR_ERR(info->battery);
438 schedule_delayed_work(&info->bat_work, DS278x_DELAY);
444 kfree(info->battery_desc.name);
448 mutex_lock(&battery_lock);
449 idr_remove(&battery_id, num);
450 mutex_unlock(&battery_lock);
455 static const struct i2c_device_id ds278x_id[] = {
460 MODULE_DEVICE_TABLE(i2c, ds278x_id);
462 static struct i2c_driver ds278x_battery_driver = {
464 .name = "ds2782-battery",
465 .pm = &ds278x_battery_pm_ops,
467 .probe = ds278x_battery_probe,
468 .remove = ds278x_battery_remove,
469 .id_table = ds278x_id,
471 module_i2c_driver(ds278x_battery_driver);
473 MODULE_AUTHOR("Ryan Mallon");
474 MODULE_DESCRIPTION("Maxim/Dallas DS2782 Stand-Alone Fuel Gauage IC driver");
475 MODULE_LICENSE("GPL");