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>
23 #define DS2782_REG_RARC 0x06 /* Remaining active relative capacity */
25 #define DS2782_REG_VOLT_MSB 0x0c
26 #define DS2782_REG_TEMP_MSB 0x0a
27 #define DS2782_REG_CURRENT_MSB 0x0e
30 #define DS2782_REG_RSNSP 0x69 /* Sense resistor value */
32 /* Current unit measurement in uA for a 1 milli-ohm sense resistor */
33 #define DS2782_CURRENT_UNITS 1563
35 #define to_ds2782_info(x) container_of(x, struct ds2782_info, battery)
38 struct i2c_client
*client
;
39 struct power_supply battery
;
43 static DEFINE_IDR(battery_id
);
44 static DEFINE_MUTEX(battery_lock
);
46 static inline int ds2782_read_reg(struct ds2782_info
*info
, int reg
, u8
*val
)
50 ret
= i2c_smbus_read_byte_data(info
->client
, reg
);
52 dev_err(&info
->client
->dev
, "register read failed\n");
60 static inline int ds2782_read_reg16(struct ds2782_info
*info
, int reg_msb
,
65 ret
= swab16(i2c_smbus_read_word_data(info
->client
, reg_msb
));
67 dev_err(&info
->client
->dev
, "register read failed\n");
75 static int ds2782_get_temp(struct ds2782_info
*info
, int *temp
)
81 * Temperature is measured in units of 0.125 degrees celcius, the
82 * power_supply class measures temperature in tenths of degrees
83 * celsius. The temperature value is stored as a 10 bit number, plus
84 * sign in the upper bits of a 16 bit register.
86 err
= ds2782_read_reg16(info
, DS2782_REG_TEMP_MSB
, &raw
);
89 *temp
= ((raw
/ 32) * 125) / 100;
93 static int ds2782_get_current(struct ds2782_info
*info
, int *current_uA
)
101 * The units of measurement for current are dependent on the value of
102 * the sense resistor.
104 err
= ds2782_read_reg(info
, DS2782_REG_RSNSP
, &sense_res_raw
);
107 if (sense_res_raw
== 0) {
108 dev_err(&info
->client
->dev
, "sense resistor value is 0\n");
111 sense_res
= 1000 / sense_res_raw
;
113 dev_dbg(&info
->client
->dev
, "sense resistor = %d milli-ohms\n",
115 err
= ds2782_read_reg16(info
, DS2782_REG_CURRENT_MSB
, &raw
);
118 *current_uA
= raw
* (DS2782_CURRENT_UNITS
/ sense_res
);
122 static int ds2782_get_voltage(struct ds2782_info
*info
, int *voltage_uA
)
128 * Voltage is measured in units of 4.88mV. The voltage is stored as
129 * a 10-bit number plus sign, in the upper bits of a 16-bit register
131 err
= ds2782_read_reg16(info
, DS2782_REG_VOLT_MSB
, &raw
);
134 *voltage_uA
= (raw
/ 32) * 4800;
138 static int ds2782_get_capacity(struct ds2782_info
*info
, int *capacity
)
143 err
= ds2782_read_reg(info
, DS2782_REG_RARC
, &raw
);
150 static int ds2782_get_status(struct ds2782_info
*info
, int *status
)
156 err
= ds2782_get_current(info
, ¤t_uA
);
160 err
= ds2782_get_capacity(info
, &capacity
);
165 *status
= POWER_SUPPLY_STATUS_FULL
;
166 else if (current_uA
== 0)
167 *status
= POWER_SUPPLY_STATUS_NOT_CHARGING
;
168 else if (current_uA
< 0)
169 *status
= POWER_SUPPLY_STATUS_DISCHARGING
;
171 *status
= POWER_SUPPLY_STATUS_CHARGING
;
176 static int ds2782_battery_get_property(struct power_supply
*psy
,
177 enum power_supply_property prop
,
178 union power_supply_propval
*val
)
180 struct ds2782_info
*info
= to_ds2782_info(psy
);
184 case POWER_SUPPLY_PROP_STATUS
:
185 ret
= ds2782_get_status(info
, &val
->intval
);
188 case POWER_SUPPLY_PROP_CAPACITY
:
189 ret
= ds2782_get_capacity(info
, &val
->intval
);
192 case POWER_SUPPLY_PROP_VOLTAGE_NOW
:
193 ret
= ds2782_get_voltage(info
, &val
->intval
);
196 case POWER_SUPPLY_PROP_CURRENT_NOW
:
197 ret
= ds2782_get_current(info
, &val
->intval
);
200 case POWER_SUPPLY_PROP_TEMP
:
201 ret
= ds2782_get_temp(info
, &val
->intval
);
211 static enum power_supply_property ds2782_battery_props
[] = {
212 POWER_SUPPLY_PROP_STATUS
,
213 POWER_SUPPLY_PROP_CAPACITY
,
214 POWER_SUPPLY_PROP_VOLTAGE_NOW
,
215 POWER_SUPPLY_PROP_CURRENT_NOW
,
216 POWER_SUPPLY_PROP_TEMP
,
219 static void ds2782_power_supply_init(struct power_supply
*battery
)
221 battery
->type
= POWER_SUPPLY_TYPE_BATTERY
;
222 battery
->properties
= ds2782_battery_props
;
223 battery
->num_properties
= ARRAY_SIZE(ds2782_battery_props
);
224 battery
->get_property
= ds2782_battery_get_property
;
225 battery
->external_power_changed
= NULL
;
228 static int ds2782_battery_remove(struct i2c_client
*client
)
230 struct ds2782_info
*info
= i2c_get_clientdata(client
);
232 power_supply_unregister(&info
->battery
);
233 kfree(info
->battery
.name
);
235 mutex_lock(&battery_lock
);
236 idr_remove(&battery_id
, info
->id
);
237 mutex_unlock(&battery_lock
);
239 i2c_set_clientdata(client
, info
);
245 static int ds2782_battery_probe(struct i2c_client
*client
,
246 const struct i2c_device_id
*id
)
248 struct ds2782_info
*info
;
252 /* Get an ID for this battery */
253 ret
= idr_pre_get(&battery_id
, GFP_KERNEL
);
259 mutex_lock(&battery_lock
);
260 ret
= idr_get_new(&battery_id
, client
, &num
);
261 mutex_unlock(&battery_lock
);
265 info
= kzalloc(sizeof(*info
), GFP_KERNEL
);
271 info
->battery
.name
= kasprintf(GFP_KERNEL
, "ds2782-%d", num
);
272 if (!info
->battery
.name
) {
277 i2c_set_clientdata(client
, info
);
278 info
->client
= client
;
279 ds2782_power_supply_init(&info
->battery
);
281 ret
= power_supply_register(&client
->dev
, &info
->battery
);
283 dev_err(&client
->dev
, "failed to register battery\n");
290 kfree(info
->battery
.name
);
292 i2c_set_clientdata(client
, info
);
295 mutex_lock(&battery_lock
);
296 idr_remove(&battery_id
, num
);
297 mutex_unlock(&battery_lock
);
302 static const struct i2c_device_id ds2782_id
[] = {
307 static struct i2c_driver ds2782_battery_driver
= {
309 .name
= "ds2782-battery",
311 .probe
= ds2782_battery_probe
,
312 .remove
= ds2782_battery_remove
,
313 .id_table
= ds2782_id
,
316 static int __init
ds2782_init(void)
318 return i2c_add_driver(&ds2782_battery_driver
);
320 module_init(ds2782_init
);
322 static void __exit
ds2782_exit(void)
324 i2c_del_driver(&ds2782_battery_driver
);
326 module_exit(ds2782_exit
);
328 MODULE_AUTHOR("Ryan Mallon <ryan@bluewatersys.com>");
329 MODULE_DESCRIPTION("Maxim/Dallas DS2782 Stand-Alone Fuel Gauage IC driver");
330 MODULE_LICENSE("GPL");