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