16 #include <linux/kernel.h>
17 #include <linux/module.h>
18 #include <linux/types.h>
19 #include <linux/errno.h>
20 #include <linux/swab.h>
21 #include <linux/i2c.h>
24 #include <linux/slab.h>
27 #define DS2782_REG_RARC 0x06
29 #define DS278x_REG_VOLT_MSB 0x0c
30 #define DS278x_REG_TEMP_MSB 0x0a
31 #define DS278x_REG_CURRENT_MSB 0x0e
34 #define DS2782_REG_RSNSP 0x69
37 #define DS2782_CURRENT_UNITS 1563
39 #define DS2786_REG_RARC 0x02
41 #define DS2786_CURRENT_UNITS 25
51 #define to_ds278x_info(x) container_of(x, struct ds278x_info, battery)
78 static inline int ds278x_read_reg16(
struct ds278x_info *info,
int reg_msb,
107 *temp = ((raw / 32) * 125) / 100;
111 static int ds2782_get_current(
struct ds278x_info *info,
int *current_uA)
125 if (sense_res_raw == 0) {
129 sense_res = 1000 / sense_res_raw;
131 dev_dbg(&info->
client->dev,
"sense resistor = %d milli-ohms\n",
140 static int ds2782_get_voltage(
struct ds278x_info *info,
int *voltage_uV)
152 *voltage_uV = (raw / 32) * 4800;
168 static int ds2786_get_current(
struct ds278x_info *info,
int *current_uA)
180 static int ds2786_get_voltage(
struct ds278x_info *info,
int *voltage_uV)
192 *voltage_uV = (raw / 8) * 1220;
196 static int ds2786_get_capacity(
struct ds278x_info *info,
int *capacity)
215 err = info->
ops->get_battery_current(info, ¤t_uA);
219 err = info->
ops->get_battery_capacity(info, &capacity);
225 else if (current_uA == 0)
227 else if (current_uA < 0)
235 static int ds278x_battery_get_property(
struct power_supply *psy,
244 ret = ds278x_get_status(info, &val->
intval);
248 ret = info->
ops->get_battery_capacity(info, &val->
intval);
252 ret = info->
ops->get_battery_voltage(info, &val->
intval);
256 ret = info->
ops->get_battery_current(info, &val->
intval);
260 ret = ds278x_get_temp(info, &val->
intval);
289 struct ds278x_info *info = i2c_get_clientdata(client);
309 .get_battery_current = ds2782_get_current,
310 .get_battery_voltage = ds2782_get_voltage,
311 .get_battery_capacity = ds2782_get_capacity,
314 .get_battery_current = ds2786_get_current,
315 .get_battery_voltage = ds2786_get_voltage,
316 .get_battery_capacity = ds2786_get_capacity,
332 if (id->driver_data ==
DS2786 && !pdata) {
333 dev_err(&client->
dev,
"missing platform data for ds2786\n");
362 if (id->driver_data ==
DS2786)
365 i2c_set_clientdata(client, info);
368 info->
ops = &ds278x_ops[
id->driver_data];
369 ds278x_power_supply_init(&info->
battery);
373 dev_err(&client->
dev,
"failed to register battery\n");
398 static struct i2c_driver ds278x_battery_driver = {
400 .name =
"ds2782-battery",
402 .probe = ds278x_battery_probe,
403 .remove = ds278x_battery_remove,
404 .id_table = ds278x_id,