Ryan Mallon | bfdb46c | 2009-06-18 11:26:26 +1200 | [diff] [blame] | 1 | /* |
| 2 | * I2C client/driver for the Maxim/Dallas DS2782 Stand-Alone Fuel Gauge IC |
| 3 | * |
| 4 | * Copyright (C) 2009 Bluewater Systems Ltd |
| 5 | * |
Ryan Mallon | 1c5454e | 2011-06-15 14:45:36 +1000 | [diff] [blame] | 6 | * Author: Ryan Mallon |
Ryan Mallon | bfdb46c | 2009-06-18 11:26:26 +1200 | [diff] [blame] | 7 | * |
Yulia Vilensky | 9b9ade6 | 2010-04-26 14:05:25 +0300 | [diff] [blame] | 8 | * DS2786 added by Yulia Vilensky <vilensky@compulab.co.il> |
| 9 | * |
Evgeny Romanov | 7cee9ae | 2013-01-10 12:24:48 +0300 | [diff] [blame] | 10 | * UEvent sending added by Evgeny Romanov <romanov@neurosoft.ru> |
| 11 | * |
Ryan Mallon | bfdb46c | 2009-06-18 11:26:26 +1200 | [diff] [blame] | 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. |
| 15 | * |
| 16 | */ |
| 17 | |
| 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> |
Evgeny Romanov | 7cee9ae | 2013-01-10 12:24:48 +0300 | [diff] [blame] | 24 | #include <linux/delay.h> |
Ryan Mallon | bfdb46c | 2009-06-18 11:26:26 +1200 | [diff] [blame] | 25 | #include <linux/idr.h> |
| 26 | #include <linux/power_supply.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 27 | #include <linux/slab.h> |
Yulia Vilensky | 9b9ade6 | 2010-04-26 14:05:25 +0300 | [diff] [blame] | 28 | #include <linux/ds2782_battery.h> |
Ryan Mallon | bfdb46c | 2009-06-18 11:26:26 +1200 | [diff] [blame] | 29 | |
| 30 | #define DS2782_REG_RARC 0x06 /* Remaining active relative capacity */ |
| 31 | |
Yulia Vilensky | 9b9ade6 | 2010-04-26 14:05:25 +0300 | [diff] [blame] | 32 | #define DS278x_REG_VOLT_MSB 0x0c |
| 33 | #define DS278x_REG_TEMP_MSB 0x0a |
| 34 | #define DS278x_REG_CURRENT_MSB 0x0e |
Ryan Mallon | bfdb46c | 2009-06-18 11:26:26 +1200 | [diff] [blame] | 35 | |
| 36 | /* EEPROM Block */ |
| 37 | #define DS2782_REG_RSNSP 0x69 /* Sense resistor value */ |
| 38 | |
| 39 | /* Current unit measurement in uA for a 1 milli-ohm sense resistor */ |
| 40 | #define DS2782_CURRENT_UNITS 1563 |
| 41 | |
Yulia Vilensky | 9b9ade6 | 2010-04-26 14:05:25 +0300 | [diff] [blame] | 42 | #define DS2786_REG_RARC 0x02 /* Remaining active relative capacity */ |
Ryan Mallon | bfdb46c | 2009-06-18 11:26:26 +1200 | [diff] [blame] | 43 | |
Yulia Vilensky | 9b9ade6 | 2010-04-26 14:05:25 +0300 | [diff] [blame] | 44 | #define DS2786_CURRENT_UNITS 25 |
| 45 | |
Evgeny Romanov | 7cee9ae | 2013-01-10 12:24:48 +0300 | [diff] [blame] | 46 | #define DS278x_DELAY 1000 |
| 47 | |
Yulia Vilensky | 9b9ade6 | 2010-04-26 14:05:25 +0300 | [diff] [blame] | 48 | struct ds278x_info; |
| 49 | |
| 50 | struct ds278x_battery_ops { |
Peter Huewe | eb9650d | 2010-05-13 01:54:57 +0200 | [diff] [blame] | 51 | int (*get_battery_current)(struct ds278x_info *info, int *current_uA); |
Ryan Mallon | 353f867 | 2010-10-01 14:17:42 -0700 | [diff] [blame] | 52 | int (*get_battery_voltage)(struct ds278x_info *info, int *voltage_uV); |
| 53 | int (*get_battery_capacity)(struct ds278x_info *info, int *capacity); |
Yulia Vilensky | 9b9ade6 | 2010-04-26 14:05:25 +0300 | [diff] [blame] | 54 | }; |
| 55 | |
Krzysztof Kozlowski | 297d716 | 2015-03-12 08:44:11 +0100 | [diff] [blame] | 56 | #define to_ds278x_info(x) power_supply_get_drvdata(x) |
Yulia Vilensky | 9b9ade6 | 2010-04-26 14:05:25 +0300 | [diff] [blame] | 57 | |
| 58 | struct ds278x_info { |
Ryan Mallon | bfdb46c | 2009-06-18 11:26:26 +1200 | [diff] [blame] | 59 | struct i2c_client *client; |
Krzysztof Kozlowski | 297d716 | 2015-03-12 08:44:11 +0100 | [diff] [blame] | 60 | struct power_supply *battery; |
| 61 | struct power_supply_desc battery_desc; |
Yulia Vilensky | 9b9ade6 | 2010-04-26 14:05:25 +0300 | [diff] [blame] | 62 | struct ds278x_battery_ops *ops; |
Evgeny Romanov | 7cee9ae | 2013-01-10 12:24:48 +0300 | [diff] [blame] | 63 | struct delayed_work bat_work; |
Ryan Mallon | bfdb46c | 2009-06-18 11:26:26 +1200 | [diff] [blame] | 64 | int id; |
Yulia Vilensky | 9b9ade6 | 2010-04-26 14:05:25 +0300 | [diff] [blame] | 65 | int rsns; |
Evgeny Romanov | 7cee9ae | 2013-01-10 12:24:48 +0300 | [diff] [blame] | 66 | int capacity; |
| 67 | int status; /* State Of Charge */ |
Ryan Mallon | bfdb46c | 2009-06-18 11:26:26 +1200 | [diff] [blame] | 68 | }; |
| 69 | |
| 70 | static DEFINE_IDR(battery_id); |
| 71 | static DEFINE_MUTEX(battery_lock); |
| 72 | |
Yulia Vilensky | 9b9ade6 | 2010-04-26 14:05:25 +0300 | [diff] [blame] | 73 | static inline int ds278x_read_reg(struct ds278x_info *info, int reg, u8 *val) |
Ryan Mallon | bfdb46c | 2009-06-18 11:26:26 +1200 | [diff] [blame] | 74 | { |
| 75 | int ret; |
| 76 | |
| 77 | ret = i2c_smbus_read_byte_data(info->client, reg); |
| 78 | if (ret < 0) { |
| 79 | dev_err(&info->client->dev, "register read failed\n"); |
| 80 | return ret; |
| 81 | } |
| 82 | |
| 83 | *val = ret; |
| 84 | return 0; |
| 85 | } |
| 86 | |
Yulia Vilensky | 9b9ade6 | 2010-04-26 14:05:25 +0300 | [diff] [blame] | 87 | static inline int ds278x_read_reg16(struct ds278x_info *info, int reg_msb, |
Ryan Mallon | bfdb46c | 2009-06-18 11:26:26 +1200 | [diff] [blame] | 88 | s16 *val) |
| 89 | { |
| 90 | int ret; |
| 91 | |
Dan Carpenter | 8511748 | 2012-10-24 10:12:29 +0300 | [diff] [blame] | 92 | ret = i2c_smbus_read_word_data(info->client, reg_msb); |
Ryan Mallon | bfdb46c | 2009-06-18 11:26:26 +1200 | [diff] [blame] | 93 | if (ret < 0) { |
| 94 | dev_err(&info->client->dev, "register read failed\n"); |
| 95 | return ret; |
| 96 | } |
| 97 | |
Dan Carpenter | 8511748 | 2012-10-24 10:12:29 +0300 | [diff] [blame] | 98 | *val = swab16(ret); |
Ryan Mallon | bfdb46c | 2009-06-18 11:26:26 +1200 | [diff] [blame] | 99 | return 0; |
| 100 | } |
| 101 | |
Yulia Vilensky | 9b9ade6 | 2010-04-26 14:05:25 +0300 | [diff] [blame] | 102 | static int ds278x_get_temp(struct ds278x_info *info, int *temp) |
Ryan Mallon | bfdb46c | 2009-06-18 11:26:26 +1200 | [diff] [blame] | 103 | { |
| 104 | s16 raw; |
| 105 | int err; |
| 106 | |
| 107 | /* |
| 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. |
| 112 | */ |
Yulia Vilensky | 9b9ade6 | 2010-04-26 14:05:25 +0300 | [diff] [blame] | 113 | err = ds278x_read_reg16(info, DS278x_REG_TEMP_MSB, &raw); |
Ryan Mallon | bfdb46c | 2009-06-18 11:26:26 +1200 | [diff] [blame] | 114 | if (err) |
| 115 | return err; |
| 116 | *temp = ((raw / 32) * 125) / 100; |
| 117 | return 0; |
| 118 | } |
| 119 | |
Yulia Vilensky | 9b9ade6 | 2010-04-26 14:05:25 +0300 | [diff] [blame] | 120 | static int ds2782_get_current(struct ds278x_info *info, int *current_uA) |
Ryan Mallon | bfdb46c | 2009-06-18 11:26:26 +1200 | [diff] [blame] | 121 | { |
| 122 | int sense_res; |
| 123 | int err; |
| 124 | u8 sense_res_raw; |
| 125 | s16 raw; |
| 126 | |
| 127 | /* |
| 128 | * The units of measurement for current are dependent on the value of |
| 129 | * the sense resistor. |
| 130 | */ |
Yulia Vilensky | 9b9ade6 | 2010-04-26 14:05:25 +0300 | [diff] [blame] | 131 | err = ds278x_read_reg(info, DS2782_REG_RSNSP, &sense_res_raw); |
Ryan Mallon | bfdb46c | 2009-06-18 11:26:26 +1200 | [diff] [blame] | 132 | if (err) |
| 133 | return err; |
| 134 | if (sense_res_raw == 0) { |
| 135 | dev_err(&info->client->dev, "sense resistor value is 0\n"); |
| 136 | return -ENXIO; |
| 137 | } |
| 138 | sense_res = 1000 / sense_res_raw; |
| 139 | |
| 140 | dev_dbg(&info->client->dev, "sense resistor = %d milli-ohms\n", |
| 141 | sense_res); |
Yulia Vilensky | 9b9ade6 | 2010-04-26 14:05:25 +0300 | [diff] [blame] | 142 | err = ds278x_read_reg16(info, DS278x_REG_CURRENT_MSB, &raw); |
Ryan Mallon | bfdb46c | 2009-06-18 11:26:26 +1200 | [diff] [blame] | 143 | if (err) |
| 144 | return err; |
| 145 | *current_uA = raw * (DS2782_CURRENT_UNITS / sense_res); |
| 146 | return 0; |
| 147 | } |
| 148 | |
Ryan Mallon | 353f867 | 2010-10-01 14:17:42 -0700 | [diff] [blame] | 149 | static int ds2782_get_voltage(struct ds278x_info *info, int *voltage_uV) |
Ryan Mallon | bfdb46c | 2009-06-18 11:26:26 +1200 | [diff] [blame] | 150 | { |
| 151 | s16 raw; |
| 152 | int err; |
| 153 | |
| 154 | /* |
| 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 |
| 157 | */ |
Yulia Vilensky | 9b9ade6 | 2010-04-26 14:05:25 +0300 | [diff] [blame] | 158 | err = ds278x_read_reg16(info, DS278x_REG_VOLT_MSB, &raw); |
Ryan Mallon | bfdb46c | 2009-06-18 11:26:26 +1200 | [diff] [blame] | 159 | if (err) |
| 160 | return err; |
Ryan Mallon | 353f867 | 2010-10-01 14:17:42 -0700 | [diff] [blame] | 161 | *voltage_uV = (raw / 32) * 4800; |
Ryan Mallon | bfdb46c | 2009-06-18 11:26:26 +1200 | [diff] [blame] | 162 | return 0; |
| 163 | } |
| 164 | |
Yulia Vilensky | 9b9ade6 | 2010-04-26 14:05:25 +0300 | [diff] [blame] | 165 | static int ds2782_get_capacity(struct ds278x_info *info, int *capacity) |
Ryan Mallon | bfdb46c | 2009-06-18 11:26:26 +1200 | [diff] [blame] | 166 | { |
| 167 | int err; |
| 168 | u8 raw; |
| 169 | |
Yulia Vilensky | 9b9ade6 | 2010-04-26 14:05:25 +0300 | [diff] [blame] | 170 | err = ds278x_read_reg(info, DS2782_REG_RARC, &raw); |
Ryan Mallon | bfdb46c | 2009-06-18 11:26:26 +1200 | [diff] [blame] | 171 | if (err) |
| 172 | return err; |
| 173 | *capacity = raw; |
Ryan Mallon | 2d31757 | 2010-06-15 12:44:59 +1200 | [diff] [blame] | 174 | return 0; |
Ryan Mallon | bfdb46c | 2009-06-18 11:26:26 +1200 | [diff] [blame] | 175 | } |
| 176 | |
Yulia Vilensky | 9b9ade6 | 2010-04-26 14:05:25 +0300 | [diff] [blame] | 177 | static int ds2786_get_current(struct ds278x_info *info, int *current_uA) |
| 178 | { |
| 179 | int err; |
| 180 | s16 raw; |
| 181 | |
| 182 | err = ds278x_read_reg16(info, DS278x_REG_CURRENT_MSB, &raw); |
| 183 | if (err) |
| 184 | return err; |
| 185 | *current_uA = (raw / 16) * (DS2786_CURRENT_UNITS / info->rsns); |
| 186 | return 0; |
| 187 | } |
| 188 | |
Ryan Mallon | 353f867 | 2010-10-01 14:17:42 -0700 | [diff] [blame] | 189 | static int ds2786_get_voltage(struct ds278x_info *info, int *voltage_uV) |
Yulia Vilensky | 9b9ade6 | 2010-04-26 14:05:25 +0300 | [diff] [blame] | 190 | { |
| 191 | s16 raw; |
| 192 | int err; |
| 193 | |
| 194 | /* |
| 195 | * Voltage is measured in units of 1.22mV. The voltage is stored as |
Matthias Brugger | 1b5e1c6 | 2014-01-17 18:02:38 +0100 | [diff] [blame] | 196 | * a 12-bit number plus sign, in the upper bits of a 16-bit register |
Yulia Vilensky | 9b9ade6 | 2010-04-26 14:05:25 +0300 | [diff] [blame] | 197 | */ |
| 198 | err = ds278x_read_reg16(info, DS278x_REG_VOLT_MSB, &raw); |
| 199 | if (err) |
| 200 | return err; |
Ryan Mallon | 353f867 | 2010-10-01 14:17:42 -0700 | [diff] [blame] | 201 | *voltage_uV = (raw / 8) * 1220; |
Yulia Vilensky | 9b9ade6 | 2010-04-26 14:05:25 +0300 | [diff] [blame] | 202 | return 0; |
| 203 | } |
| 204 | |
| 205 | static int ds2786_get_capacity(struct ds278x_info *info, int *capacity) |
| 206 | { |
| 207 | int err; |
| 208 | u8 raw; |
| 209 | |
| 210 | err = ds278x_read_reg(info, DS2786_REG_RARC, &raw); |
| 211 | if (err) |
| 212 | return err; |
| 213 | /* Relative capacity is displayed with resolution 0.5 % */ |
| 214 | *capacity = raw/2 ; |
| 215 | return 0; |
| 216 | } |
| 217 | |
| 218 | static int ds278x_get_status(struct ds278x_info *info, int *status) |
Ryan Mallon | bfdb46c | 2009-06-18 11:26:26 +1200 | [diff] [blame] | 219 | { |
| 220 | int err; |
| 221 | int current_uA; |
| 222 | int capacity; |
| 223 | |
Peter Huewe | eb9650d | 2010-05-13 01:54:57 +0200 | [diff] [blame] | 224 | err = info->ops->get_battery_current(info, ¤t_uA); |
Ryan Mallon | bfdb46c | 2009-06-18 11:26:26 +1200 | [diff] [blame] | 225 | if (err) |
| 226 | return err; |
| 227 | |
Peter Huewe | eb9650d | 2010-05-13 01:54:57 +0200 | [diff] [blame] | 228 | err = info->ops->get_battery_capacity(info, &capacity); |
Ryan Mallon | bfdb46c | 2009-06-18 11:26:26 +1200 | [diff] [blame] | 229 | if (err) |
| 230 | return err; |
| 231 | |
Evgeny Romanov | 7cee9ae | 2013-01-10 12:24:48 +0300 | [diff] [blame] | 232 | info->capacity = capacity; |
| 233 | |
Ryan Mallon | bfdb46c | 2009-06-18 11:26:26 +1200 | [diff] [blame] | 234 | if (capacity == 100) |
| 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; |
| 240 | else |
| 241 | *status = POWER_SUPPLY_STATUS_CHARGING; |
| 242 | |
| 243 | return 0; |
| 244 | } |
| 245 | |
Yulia Vilensky | 9b9ade6 | 2010-04-26 14:05:25 +0300 | [diff] [blame] | 246 | static int ds278x_battery_get_property(struct power_supply *psy, |
Ryan Mallon | bfdb46c | 2009-06-18 11:26:26 +1200 | [diff] [blame] | 247 | enum power_supply_property prop, |
| 248 | union power_supply_propval *val) |
| 249 | { |
Yulia Vilensky | 9b9ade6 | 2010-04-26 14:05:25 +0300 | [diff] [blame] | 250 | struct ds278x_info *info = to_ds278x_info(psy); |
Ryan Mallon | bfdb46c | 2009-06-18 11:26:26 +1200 | [diff] [blame] | 251 | int ret; |
| 252 | |
| 253 | switch (prop) { |
| 254 | case POWER_SUPPLY_PROP_STATUS: |
Yulia Vilensky | 9b9ade6 | 2010-04-26 14:05:25 +0300 | [diff] [blame] | 255 | ret = ds278x_get_status(info, &val->intval); |
Ryan Mallon | bfdb46c | 2009-06-18 11:26:26 +1200 | [diff] [blame] | 256 | break; |
| 257 | |
| 258 | case POWER_SUPPLY_PROP_CAPACITY: |
Peter Huewe | eb9650d | 2010-05-13 01:54:57 +0200 | [diff] [blame] | 259 | ret = info->ops->get_battery_capacity(info, &val->intval); |
Ryan Mallon | bfdb46c | 2009-06-18 11:26:26 +1200 | [diff] [blame] | 260 | break; |
| 261 | |
| 262 | case POWER_SUPPLY_PROP_VOLTAGE_NOW: |
Peter Huewe | eb9650d | 2010-05-13 01:54:57 +0200 | [diff] [blame] | 263 | ret = info->ops->get_battery_voltage(info, &val->intval); |
Ryan Mallon | bfdb46c | 2009-06-18 11:26:26 +1200 | [diff] [blame] | 264 | break; |
| 265 | |
| 266 | case POWER_SUPPLY_PROP_CURRENT_NOW: |
Peter Huewe | eb9650d | 2010-05-13 01:54:57 +0200 | [diff] [blame] | 267 | ret = info->ops->get_battery_current(info, &val->intval); |
Ryan Mallon | bfdb46c | 2009-06-18 11:26:26 +1200 | [diff] [blame] | 268 | break; |
| 269 | |
| 270 | case POWER_SUPPLY_PROP_TEMP: |
Yulia Vilensky | 9b9ade6 | 2010-04-26 14:05:25 +0300 | [diff] [blame] | 271 | ret = ds278x_get_temp(info, &val->intval); |
Ryan Mallon | bfdb46c | 2009-06-18 11:26:26 +1200 | [diff] [blame] | 272 | break; |
| 273 | |
| 274 | default: |
| 275 | ret = -EINVAL; |
| 276 | } |
| 277 | |
| 278 | return ret; |
| 279 | } |
| 280 | |
Evgeny Romanov | 7cee9ae | 2013-01-10 12:24:48 +0300 | [diff] [blame] | 281 | static void ds278x_bat_update(struct ds278x_info *info) |
| 282 | { |
| 283 | int old_status = info->status; |
| 284 | int old_capacity = info->capacity; |
| 285 | |
| 286 | ds278x_get_status(info, &info->status); |
| 287 | |
| 288 | if ((old_status != info->status) || (old_capacity != info->capacity)) |
Krzysztof Kozlowski | 297d716 | 2015-03-12 08:44:11 +0100 | [diff] [blame] | 289 | power_supply_changed(info->battery); |
Evgeny Romanov | 7cee9ae | 2013-01-10 12:24:48 +0300 | [diff] [blame] | 290 | } |
| 291 | |
| 292 | static void ds278x_bat_work(struct work_struct *work) |
| 293 | { |
| 294 | struct ds278x_info *info; |
| 295 | |
| 296 | info = container_of(work, struct ds278x_info, bat_work.work); |
| 297 | ds278x_bat_update(info); |
| 298 | |
| 299 | schedule_delayed_work(&info->bat_work, DS278x_DELAY); |
| 300 | } |
| 301 | |
Yulia Vilensky | 9b9ade6 | 2010-04-26 14:05:25 +0300 | [diff] [blame] | 302 | static enum power_supply_property ds278x_battery_props[] = { |
Ryan Mallon | bfdb46c | 2009-06-18 11:26:26 +1200 | [diff] [blame] | 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, |
| 308 | }; |
| 309 | |
Krzysztof Kozlowski | 297d716 | 2015-03-12 08:44:11 +0100 | [diff] [blame] | 310 | static void ds278x_power_supply_init(struct power_supply_desc *battery) |
Ryan Mallon | bfdb46c | 2009-06-18 11:26:26 +1200 | [diff] [blame] | 311 | { |
| 312 | battery->type = POWER_SUPPLY_TYPE_BATTERY; |
Yulia Vilensky | 9b9ade6 | 2010-04-26 14:05:25 +0300 | [diff] [blame] | 313 | battery->properties = ds278x_battery_props; |
| 314 | battery->num_properties = ARRAY_SIZE(ds278x_battery_props); |
| 315 | battery->get_property = ds278x_battery_get_property; |
Ryan Mallon | bfdb46c | 2009-06-18 11:26:26 +1200 | [diff] [blame] | 316 | battery->external_power_changed = NULL; |
| 317 | } |
| 318 | |
Yulia Vilensky | 9b9ade6 | 2010-04-26 14:05:25 +0300 | [diff] [blame] | 319 | static int ds278x_battery_remove(struct i2c_client *client) |
Ryan Mallon | bfdb46c | 2009-06-18 11:26:26 +1200 | [diff] [blame] | 320 | { |
Yulia Vilensky | 9b9ade6 | 2010-04-26 14:05:25 +0300 | [diff] [blame] | 321 | struct ds278x_info *info = i2c_get_clientdata(client); |
Ryan Mallon | bfdb46c | 2009-06-18 11:26:26 +1200 | [diff] [blame] | 322 | |
Krzysztof Kozlowski | 297d716 | 2015-03-12 08:44:11 +0100 | [diff] [blame] | 323 | power_supply_unregister(info->battery); |
| 324 | kfree(info->battery_desc.name); |
Ryan Mallon | bfdb46c | 2009-06-18 11:26:26 +1200 | [diff] [blame] | 325 | |
| 326 | mutex_lock(&battery_lock); |
| 327 | idr_remove(&battery_id, info->id); |
| 328 | mutex_unlock(&battery_lock); |
| 329 | |
Evgeny Romanov | 7cee9ae | 2013-01-10 12:24:48 +0300 | [diff] [blame] | 330 | cancel_delayed_work(&info->bat_work); |
| 331 | |
Ryan Mallon | bfdb46c | 2009-06-18 11:26:26 +1200 | [diff] [blame] | 332 | kfree(info); |
| 333 | return 0; |
| 334 | } |
| 335 | |
Lars-Peter Clausen | cc6616f | 2013-03-10 14:34:05 +0100 | [diff] [blame] | 336 | #ifdef CONFIG_PM_SLEEP |
Evgeny Romanov | 7cee9ae | 2013-01-10 12:24:48 +0300 | [diff] [blame] | 337 | |
Lars-Peter Clausen | cc6616f | 2013-03-10 14:34:05 +0100 | [diff] [blame] | 338 | static int ds278x_suspend(struct device *dev) |
Evgeny Romanov | 7cee9ae | 2013-01-10 12:24:48 +0300 | [diff] [blame] | 339 | { |
Lars-Peter Clausen | cc6616f | 2013-03-10 14:34:05 +0100 | [diff] [blame] | 340 | struct i2c_client *client = to_i2c_client(dev); |
Evgeny Romanov | 7cee9ae | 2013-01-10 12:24:48 +0300 | [diff] [blame] | 341 | struct ds278x_info *info = i2c_get_clientdata(client); |
| 342 | |
| 343 | cancel_delayed_work(&info->bat_work); |
| 344 | return 0; |
| 345 | } |
| 346 | |
Lars-Peter Clausen | cc6616f | 2013-03-10 14:34:05 +0100 | [diff] [blame] | 347 | static int ds278x_resume(struct device *dev) |
Evgeny Romanov | 7cee9ae | 2013-01-10 12:24:48 +0300 | [diff] [blame] | 348 | { |
Lars-Peter Clausen | cc6616f | 2013-03-10 14:34:05 +0100 | [diff] [blame] | 349 | struct i2c_client *client = to_i2c_client(dev); |
Evgeny Romanov | 7cee9ae | 2013-01-10 12:24:48 +0300 | [diff] [blame] | 350 | struct ds278x_info *info = i2c_get_clientdata(client); |
| 351 | |
| 352 | schedule_delayed_work(&info->bat_work, DS278x_DELAY); |
| 353 | return 0; |
| 354 | } |
Fabio Estevam | 99a7956 | 2014-10-09 19:50:26 -0300 | [diff] [blame] | 355 | #endif /* CONFIG_PM_SLEEP */ |
Evgeny Romanov | 7cee9ae | 2013-01-10 12:24:48 +0300 | [diff] [blame] | 356 | |
Lars-Peter Clausen | cc6616f | 2013-03-10 14:34:05 +0100 | [diff] [blame] | 357 | static SIMPLE_DEV_PM_OPS(ds278x_battery_pm_ops, ds278x_suspend, ds278x_resume); |
Evgeny Romanov | 7cee9ae | 2013-01-10 12:24:48 +0300 | [diff] [blame] | 358 | |
Anton Vorontsov | ab6cc8f | 2010-04-26 22:10:52 +0400 | [diff] [blame] | 359 | enum ds278x_num_id { |
| 360 | DS2782 = 0, |
| 361 | DS2786, |
| 362 | }; |
| 363 | |
Yulia Vilensky | 9b9ade6 | 2010-04-26 14:05:25 +0300 | [diff] [blame] | 364 | static struct ds278x_battery_ops ds278x_ops[] = { |
Anton Vorontsov | ab6cc8f | 2010-04-26 22:10:52 +0400 | [diff] [blame] | 365 | [DS2782] = { |
Peter Huewe | eb9650d | 2010-05-13 01:54:57 +0200 | [diff] [blame] | 366 | .get_battery_current = ds2782_get_current, |
| 367 | .get_battery_voltage = ds2782_get_voltage, |
| 368 | .get_battery_capacity = ds2782_get_capacity, |
Yulia Vilensky | 9b9ade6 | 2010-04-26 14:05:25 +0300 | [diff] [blame] | 369 | }, |
Anton Vorontsov | ab6cc8f | 2010-04-26 22:10:52 +0400 | [diff] [blame] | 370 | [DS2786] = { |
Peter Huewe | eb9650d | 2010-05-13 01:54:57 +0200 | [diff] [blame] | 371 | .get_battery_current = ds2786_get_current, |
| 372 | .get_battery_voltage = ds2786_get_voltage, |
| 373 | .get_battery_capacity = ds2786_get_capacity, |
Yulia Vilensky | 9b9ade6 | 2010-04-26 14:05:25 +0300 | [diff] [blame] | 374 | } |
| 375 | }; |
| 376 | |
| 377 | static int ds278x_battery_probe(struct i2c_client *client, |
Ryan Mallon | bfdb46c | 2009-06-18 11:26:26 +1200 | [diff] [blame] | 378 | const struct i2c_device_id *id) |
| 379 | { |
Yulia Vilensky | 9b9ade6 | 2010-04-26 14:05:25 +0300 | [diff] [blame] | 380 | struct ds278x_platform_data *pdata = client->dev.platform_data; |
Krzysztof Kozlowski | 297d716 | 2015-03-12 08:44:11 +0100 | [diff] [blame] | 381 | struct power_supply_config psy_cfg = {}; |
Yulia Vilensky | 9b9ade6 | 2010-04-26 14:05:25 +0300 | [diff] [blame] | 382 | struct ds278x_info *info; |
Ryan Mallon | bfdb46c | 2009-06-18 11:26:26 +1200 | [diff] [blame] | 383 | int ret; |
| 384 | int num; |
| 385 | |
Yulia Vilensky | 9b9ade6 | 2010-04-26 14:05:25 +0300 | [diff] [blame] | 386 | /* |
| 387 | * ds2786 should have the sense resistor value set |
| 388 | * in the platform data |
| 389 | */ |
Anton Vorontsov | ab6cc8f | 2010-04-26 22:10:52 +0400 | [diff] [blame] | 390 | if (id->driver_data == DS2786 && !pdata) { |
Yulia Vilensky | 9b9ade6 | 2010-04-26 14:05:25 +0300 | [diff] [blame] | 391 | dev_err(&client->dev, "missing platform data for ds2786\n"); |
| 392 | return -EINVAL; |
| 393 | } |
| 394 | |
Ryan Mallon | bfdb46c | 2009-06-18 11:26:26 +1200 | [diff] [blame] | 395 | /* Get an ID for this battery */ |
Ryan Mallon | bfdb46c | 2009-06-18 11:26:26 +1200 | [diff] [blame] | 396 | mutex_lock(&battery_lock); |
Tejun Heo | 05e2cef | 2013-02-27 17:04:37 -0800 | [diff] [blame] | 397 | ret = idr_alloc(&battery_id, client, 0, 0, GFP_KERNEL); |
Ryan Mallon | bfdb46c | 2009-06-18 11:26:26 +1200 | [diff] [blame] | 398 | mutex_unlock(&battery_lock); |
| 399 | if (ret < 0) |
| 400 | goto fail_id; |
Tejun Heo | 05e2cef | 2013-02-27 17:04:37 -0800 | [diff] [blame] | 401 | num = ret; |
Ryan Mallon | bfdb46c | 2009-06-18 11:26:26 +1200 | [diff] [blame] | 402 | |
| 403 | info = kzalloc(sizeof(*info), GFP_KERNEL); |
| 404 | if (!info) { |
| 405 | ret = -ENOMEM; |
| 406 | goto fail_info; |
| 407 | } |
| 408 | |
Krzysztof Kozlowski | 297d716 | 2015-03-12 08:44:11 +0100 | [diff] [blame] | 409 | info->battery_desc.name = kasprintf(GFP_KERNEL, "%s-%d", |
| 410 | client->name, num); |
| 411 | if (!info->battery_desc.name) { |
Ryan Mallon | bfdb46c | 2009-06-18 11:26:26 +1200 | [diff] [blame] | 412 | ret = -ENOMEM; |
| 413 | goto fail_name; |
| 414 | } |
| 415 | |
Anton Vorontsov | ab6cc8f | 2010-04-26 22:10:52 +0400 | [diff] [blame] | 416 | if (id->driver_data == DS2786) |
Yulia Vilensky | 9b9ade6 | 2010-04-26 14:05:25 +0300 | [diff] [blame] | 417 | info->rsns = pdata->rsns; |
| 418 | |
Ryan Mallon | bfdb46c | 2009-06-18 11:26:26 +1200 | [diff] [blame] | 419 | i2c_set_clientdata(client, info); |
| 420 | info->client = client; |
Yulia Vilensky | 9b9ade6 | 2010-04-26 14:05:25 +0300 | [diff] [blame] | 421 | info->id = num; |
| 422 | info->ops = &ds278x_ops[id->driver_data]; |
Krzysztof Kozlowski | 297d716 | 2015-03-12 08:44:11 +0100 | [diff] [blame] | 423 | ds278x_power_supply_init(&info->battery_desc); |
| 424 | psy_cfg.drv_data = info; |
Ryan Mallon | bfdb46c | 2009-06-18 11:26:26 +1200 | [diff] [blame] | 425 | |
Evgeny Romanov | 7cee9ae | 2013-01-10 12:24:48 +0300 | [diff] [blame] | 426 | info->capacity = 100; |
| 427 | info->status = POWER_SUPPLY_STATUS_FULL; |
| 428 | |
| 429 | INIT_DELAYED_WORK(&info->bat_work, ds278x_bat_work); |
| 430 | |
Krzysztof Kozlowski | 297d716 | 2015-03-12 08:44:11 +0100 | [diff] [blame] | 431 | info->battery = power_supply_register(&client->dev, |
| 432 | &info->battery_desc, &psy_cfg); |
| 433 | if (IS_ERR(info->battery)) { |
Ryan Mallon | bfdb46c | 2009-06-18 11:26:26 +1200 | [diff] [blame] | 434 | dev_err(&client->dev, "failed to register battery\n"); |
Krzysztof Kozlowski | 297d716 | 2015-03-12 08:44:11 +0100 | [diff] [blame] | 435 | ret = PTR_ERR(info->battery); |
Ryan Mallon | bfdb46c | 2009-06-18 11:26:26 +1200 | [diff] [blame] | 436 | goto fail_register; |
Evgeny Romanov | 7cee9ae | 2013-01-10 12:24:48 +0300 | [diff] [blame] | 437 | } else { |
| 438 | schedule_delayed_work(&info->bat_work, DS278x_DELAY); |
Ryan Mallon | bfdb46c | 2009-06-18 11:26:26 +1200 | [diff] [blame] | 439 | } |
| 440 | |
| 441 | return 0; |
| 442 | |
| 443 | fail_register: |
Krzysztof Kozlowski | 297d716 | 2015-03-12 08:44:11 +0100 | [diff] [blame] | 444 | kfree(info->battery_desc.name); |
Ryan Mallon | bfdb46c | 2009-06-18 11:26:26 +1200 | [diff] [blame] | 445 | fail_name: |
Ryan Mallon | bfdb46c | 2009-06-18 11:26:26 +1200 | [diff] [blame] | 446 | kfree(info); |
| 447 | fail_info: |
| 448 | mutex_lock(&battery_lock); |
| 449 | idr_remove(&battery_id, num); |
| 450 | mutex_unlock(&battery_lock); |
| 451 | fail_id: |
| 452 | return ret; |
| 453 | } |
| 454 | |
Yulia Vilensky | 9b9ade6 | 2010-04-26 14:05:25 +0300 | [diff] [blame] | 455 | static const struct i2c_device_id ds278x_id[] = { |
Anton Vorontsov | ab6cc8f | 2010-04-26 22:10:52 +0400 | [diff] [blame] | 456 | {"ds2782", DS2782}, |
| 457 | {"ds2786", DS2786}, |
Ryan Mallon | bfdb46c | 2009-06-18 11:26:26 +1200 | [diff] [blame] | 458 | {}, |
| 459 | }; |
Axel Lin | 84ab16f | 2011-03-01 17:23:34 +0800 | [diff] [blame] | 460 | MODULE_DEVICE_TABLE(i2c, ds278x_id); |
Ryan Mallon | bfdb46c | 2009-06-18 11:26:26 +1200 | [diff] [blame] | 461 | |
Yulia Vilensky | 9b9ade6 | 2010-04-26 14:05:25 +0300 | [diff] [blame] | 462 | static struct i2c_driver ds278x_battery_driver = { |
Ryan Mallon | bfdb46c | 2009-06-18 11:26:26 +1200 | [diff] [blame] | 463 | .driver = { |
| 464 | .name = "ds2782-battery", |
Fabio Estevam | 99a7956 | 2014-10-09 19:50:26 -0300 | [diff] [blame] | 465 | .pm = &ds278x_battery_pm_ops, |
Ryan Mallon | bfdb46c | 2009-06-18 11:26:26 +1200 | [diff] [blame] | 466 | }, |
Yulia Vilensky | 9b9ade6 | 2010-04-26 14:05:25 +0300 | [diff] [blame] | 467 | .probe = ds278x_battery_probe, |
| 468 | .remove = ds278x_battery_remove, |
| 469 | .id_table = ds278x_id, |
Ryan Mallon | bfdb46c | 2009-06-18 11:26:26 +1200 | [diff] [blame] | 470 | }; |
Axel Lin | 5ff92e7 | 2012-01-21 14:42:54 +0800 | [diff] [blame] | 471 | module_i2c_driver(ds278x_battery_driver); |
Ryan Mallon | bfdb46c | 2009-06-18 11:26:26 +1200 | [diff] [blame] | 472 | |
Ryan Mallon | 1c5454e | 2011-06-15 14:45:36 +1000 | [diff] [blame] | 473 | MODULE_AUTHOR("Ryan Mallon"); |
Ryan Mallon | bfdb46c | 2009-06-18 11:26:26 +1200 | [diff] [blame] | 474 | MODULE_DESCRIPTION("Maxim/Dallas DS2782 Stand-Alone Fuel Gauage IC driver"); |
| 475 | MODULE_LICENSE("GPL"); |