2 * I2C client/driver for the Maxim/Dallas DS2782 Stand-Alone Fuel Gauge IC
4 * Copyright (C) 2009 Bluewater Systems Ltd
6 * Author: Ryan Mallon <ryan@bluewatersys.com>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation.
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/idr.h>
21 #include <linux/power_supply.h>
22 #include <linux/slab.h>
24 #define DS2782_REG_RARC 0x06 /* Remaining active relative capacity */
26 #define DS2782_REG_VOLT_MSB 0x0c
27 #define DS2782_REG_TEMP_MSB 0x0a
28 #define DS2782_REG_CURRENT_MSB 0x0e
31 #define DS2782_REG_RSNSP 0x69 /* Sense resistor value */
33 /* Current unit measurement in uA for a 1 milli-ohm sense resistor */
34 #define DS2782_CURRENT_UNITS 1563
36 #define to_ds2782_info(x) container_of(x, struct ds2782_info, battery)
39 struct i2c_client
*client
;
40 struct power_supply battery
;
44 static DEFINE_IDR(battery_id
);
45 static DEFINE_MUTEX(battery_lock
);
47 static inline int ds2782_read_reg(struct ds2782_info
*info
, int reg
, u8
*val
)
51 ret
= i2c_smbus_read_byte_data(info
->client
, reg
);
53 dev_err(&info
->client
->dev
, "register read failed\n");
61 static inline int ds2782_read_reg16(struct ds2782_info
*info
, int reg_msb
,
66 ret
= swab16(i2c_smbus_read_word_data(info
->client
, reg_msb
));
68 dev_err(&info
->client
->dev
, "register read failed\n");
76 static int ds2782_get_temp(struct ds2782_info
*info
, int *temp
)
82 * Temperature is measured in units of 0.125 degrees celcius, the
83 * power_supply class measures temperature in tenths of degrees
84 * celsius. The temperature value is stored as a 10 bit number, plus
85 * sign in the upper bits of a 16 bit register.
87 err
= ds2782_read_reg16(info
, DS2782_REG_TEMP_MSB
, &raw
);
90 *temp
= ((raw
/ 32) * 125) / 100;
94 static int ds2782_get_current(struct ds2782_info
*info
, int *current_uA
)
102 * The units of measurement for current are dependent on the value of
103 * the sense resistor.
105 err
= ds2782_read_reg(info
, DS2782_REG_RSNSP
, &sense_res_raw
);
108 if (sense_res_raw
== 0) {
109 dev_err(&info
->client
->dev
, "sense resistor value is 0\n");
112 sense_res
= 1000 / sense_res_raw
;
114 dev_dbg(&info
->client
->dev
, "sense resistor = %d milli-ohms\n",
116 err
= ds2782_read_reg16(info
, DS2782_REG_CURRENT_MSB
, &raw
);
119 *current_uA
= raw
* (DS2782_CURRENT_UNITS
/ sense_res
);
123 static int ds2782_get_voltage(struct ds2782_info
*info
, int *voltage_uA
)
129 * Voltage is measured in units of 4.88mV. The voltage is stored as
130 * a 10-bit number plus sign, in the upper bits of a 16-bit register
132 err
= ds2782_read_reg16(info
, DS2782_REG_VOLT_MSB
, &raw
);
135 *voltage_uA
= (raw
/ 32) * 4800;
139 static int ds2782_get_capacity(struct ds2782_info
*info
, int *capacity
)
144 err
= ds2782_read_reg(info
, DS2782_REG_RARC
, &raw
);
151 static int ds2782_get_status(struct ds2782_info
*info
, int *status
)
157 err
= ds2782_get_current(info
, ¤t_uA
);
161 err
= ds2782_get_capacity(info
, &capacity
);
166 *status
= POWER_SUPPLY_STATUS_FULL
;
167 else if (current_uA
== 0)
168 *status
= POWER_SUPPLY_STATUS_NOT_CHARGING
;
169 else if (current_uA
< 0)
170 *status
= POWER_SUPPLY_STATUS_DISCHARGING
;
172 *status
= POWER_SUPPLY_STATUS_CHARGING
;
177 static int ds2782_battery_get_property(struct power_supply
*psy
,
178 enum power_supply_property prop
,
179 union power_supply_propval
*val
)
181 struct ds2782_info
*info
= to_ds2782_info(psy
);
185 case POWER_SUPPLY_PROP_STATUS
:
186 ret
= ds2782_get_status(info
, &val
->intval
);
189 case POWER_SUPPLY_PROP_CAPACITY
:
190 ret
= ds2782_get_capacity(info
, &val
->intval
);
193 case POWER_SUPPLY_PROP_VOLTAGE_NOW
:
194 ret
= ds2782_get_voltage(info
, &val
->intval
);
197 case POWER_SUPPLY_PROP_CURRENT_NOW
:
198 ret
= ds2782_get_current(info
, &val
->intval
);
201 case POWER_SUPPLY_PROP_TEMP
:
202 ret
= ds2782_get_temp(info
, &val
->intval
);
212 static enum power_supply_property ds2782_battery_props
[] = {
213 POWER_SUPPLY_PROP_STATUS
,
214 POWER_SUPPLY_PROP_CAPACITY
,
215 POWER_SUPPLY_PROP_VOLTAGE_NOW
,
216 POWER_SUPPLY_PROP_CURRENT_NOW
,
217 POWER_SUPPLY_PROP_TEMP
,
220 static void ds2782_power_supply_init(struct power_supply
*battery
)
222 battery
->type
= POWER_SUPPLY_TYPE_BATTERY
;
223 battery
->properties
= ds2782_battery_props
;
224 battery
->num_properties
= ARRAY_SIZE(ds2782_battery_props
);
225 battery
->get_property
= ds2782_battery_get_property
;
226 battery
->external_power_changed
= NULL
;
229 static int ds2782_battery_remove(struct i2c_client
*client
)
231 struct ds2782_info
*info
= i2c_get_clientdata(client
);
233 power_supply_unregister(&info
->battery
);
234 kfree(info
->battery
.name
);
236 mutex_lock(&battery_lock
);
237 idr_remove(&battery_id
, info
->id
);
238 mutex_unlock(&battery_lock
);
240 i2c_set_clientdata(client
, info
);
246 static int ds2782_battery_probe(struct i2c_client
*client
,
247 const struct i2c_device_id
*id
)
249 struct ds2782_info
*info
;
253 /* Get an ID for this battery */
254 ret
= idr_pre_get(&battery_id
, GFP_KERNEL
);
260 mutex_lock(&battery_lock
);
261 ret
= idr_get_new(&battery_id
, client
, &num
);
262 mutex_unlock(&battery_lock
);
266 info
= kzalloc(sizeof(*info
), GFP_KERNEL
);
272 info
->battery
.name
= kasprintf(GFP_KERNEL
, "ds2782-%d", num
);
273 if (!info
->battery
.name
) {
278 i2c_set_clientdata(client
, info
);
279 info
->client
= client
;
280 ds2782_power_supply_init(&info
->battery
);
282 ret
= power_supply_register(&client
->dev
, &info
->battery
);
284 dev_err(&client
->dev
, "failed to register battery\n");
291 kfree(info
->battery
.name
);
293 i2c_set_clientdata(client
, info
);
296 mutex_lock(&battery_lock
);
297 idr_remove(&battery_id
, num
);
298 mutex_unlock(&battery_lock
);
303 static const struct i2c_device_id ds2782_id
[] = {
308 static struct i2c_driver ds2782_battery_driver
= {
310 .name
= "ds2782-battery",
312 .probe
= ds2782_battery_probe
,
313 .remove
= ds2782_battery_remove
,
314 .id_table
= ds2782_id
,
317 static int __init
ds2782_init(void)
319 return i2c_add_driver(&ds2782_battery_driver
);
321 module_init(ds2782_init
);
323 static void __exit
ds2782_exit(void)
325 i2c_del_driver(&ds2782_battery_driver
);
327 module_exit(ds2782_exit
);
329 MODULE_AUTHOR("Ryan Mallon <ryan@bluewatersys.com>");
330 MODULE_DESCRIPTION("Maxim/Dallas DS2782 Stand-Alone Fuel Gauage IC driver");
331 MODULE_LICENSE("GPL");