Anton Vorontsov | fe0e315 | 2007-05-04 00:45:39 +0400 | [diff] [blame] | 1 | /* |
| 2 | * Driver for batteries with DS2760 chips inside. |
| 3 | * |
| 4 | * Copyright © 2007 Anton Vorontsov |
| 5 | * 2004-2007 Matt Reimer |
| 6 | * 2004 Szabolcs Gyurko |
| 7 | * |
| 8 | * Use consistent with the GNU GPL is permitted, |
| 9 | * provided that this copyright notice is |
| 10 | * preserved in its entirety in all copies and derived works. |
| 11 | * |
| 12 | * Author: Anton Vorontsov <cbou@mail.ru> |
| 13 | * February 2007 |
| 14 | * |
| 15 | * Matt Reimer <mreimer@vpop.net> |
| 16 | * April 2004, 2005, 2007 |
| 17 | * |
| 18 | * Szabolcs Gyurko <szabolcs.gyurko@tlt.hu> |
| 19 | * September 2004 |
| 20 | */ |
| 21 | |
| 22 | #include <linux/module.h> |
| 23 | #include <linux/param.h> |
| 24 | #include <linux/jiffies.h> |
| 25 | #include <linux/workqueue.h> |
| 26 | #include <linux/pm.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 27 | #include <linux/slab.h> |
Anton Vorontsov | fe0e315 | 2007-05-04 00:45:39 +0400 | [diff] [blame] | 28 | #include <linux/platform_device.h> |
| 29 | #include <linux/power_supply.h> |
| 30 | |
| 31 | #include "../w1/w1.h" |
| 32 | #include "../w1/slaves/w1_ds2760.h" |
| 33 | |
| 34 | struct ds2760_device_info { |
| 35 | struct device *dev; |
| 36 | |
| 37 | /* DS2760 data, valid after calling ds2760_battery_read_status() */ |
| 38 | unsigned long update_time; /* jiffies when data read */ |
| 39 | char raw[DS2760_DATA_SIZE]; /* raw DS2760 data */ |
| 40 | int voltage_raw; /* units of 4.88 mV */ |
| 41 | int voltage_uV; /* units of µV */ |
| 42 | int current_raw; /* units of 0.625 mA */ |
| 43 | int current_uA; /* units of µA */ |
| 44 | int accum_current_raw; /* units of 0.25 mAh */ |
| 45 | int accum_current_uAh; /* units of µAh */ |
| 46 | int temp_raw; /* units of 0.125 °C */ |
| 47 | int temp_C; /* units of 0.1 °C */ |
| 48 | int rated_capacity; /* units of µAh */ |
| 49 | int rem_capacity; /* percentage */ |
| 50 | int full_active_uAh; /* units of µAh */ |
| 51 | int empty_uAh; /* units of µAh */ |
| 52 | int life_sec; /* units of seconds */ |
| 53 | int charge_status; /* POWER_SUPPLY_STATUS_* */ |
| 54 | |
| 55 | int full_counter; |
| 56 | struct power_supply bat; |
| 57 | struct device *w1_dev; |
| 58 | struct workqueue_struct *monitor_wqueue; |
| 59 | struct delayed_work monitor_work; |
Daniel Mack | 8d631cc | 2009-07-23 20:35:54 +0200 | [diff] [blame] | 60 | struct delayed_work set_charged_work; |
Anton Vorontsov | fe0e315 | 2007-05-04 00:45:39 +0400 | [diff] [blame] | 61 | }; |
| 62 | |
| 63 | static unsigned int cache_time = 1000; |
| 64 | module_param(cache_time, uint, 0644); |
| 65 | MODULE_PARM_DESC(cache_time, "cache time in milliseconds"); |
| 66 | |
Rusty Russell | 90ab5ee | 2012-01-13 09:32:20 +1030 | [diff] [blame] | 67 | static bool pmod_enabled; |
Daniel Mack | cef437e | 2009-04-28 10:55:02 +0200 | [diff] [blame] | 68 | module_param(pmod_enabled, bool, 0644); |
| 69 | MODULE_PARM_DESC(pmod_enabled, "PMOD enable bit"); |
| 70 | |
Daniel Mack | c1e7219 | 2009-07-15 18:20:40 +0200 | [diff] [blame] | 71 | static unsigned int rated_capacity; |
| 72 | module_param(rated_capacity, uint, 0644); |
| 73 | MODULE_PARM_DESC(rated_capacity, "rated battery capacity, 10*mAh or index"); |
| 74 | |
Daniel Mack | 02d0d27 | 2009-07-15 22:57:16 +0200 | [diff] [blame] | 75 | static unsigned int current_accum; |
| 76 | module_param(current_accum, uint, 0644); |
| 77 | MODULE_PARM_DESC(current_accum, "current accumulator value"); |
| 78 | |
Anton Vorontsov | fe0e315 | 2007-05-04 00:45:39 +0400 | [diff] [blame] | 79 | /* Some batteries have their rated capacity stored a N * 10 mAh, while |
| 80 | * others use an index into this table. */ |
| 81 | static int rated_capacities[] = { |
| 82 | 0, |
| 83 | 920, /* Samsung */ |
| 84 | 920, /* BYD */ |
| 85 | 920, /* Lishen */ |
| 86 | 920, /* NEC */ |
| 87 | 1440, /* Samsung */ |
| 88 | 1440, /* BYD */ |
Paul Parsons | 9c6f874 | 2011-04-25 13:45:40 +0000 | [diff] [blame] | 89 | #ifdef CONFIG_MACH_H4700 |
| 90 | 1800, /* HP iPAQ hx4700 3.7V 1800mAh (359113-001) */ |
| 91 | #else |
Anton Vorontsov | fe0e315 | 2007-05-04 00:45:39 +0400 | [diff] [blame] | 92 | 1440, /* Lishen */ |
Paul Parsons | 9c6f874 | 2011-04-25 13:45:40 +0000 | [diff] [blame] | 93 | #endif |
Anton Vorontsov | fe0e315 | 2007-05-04 00:45:39 +0400 | [diff] [blame] | 94 | 1440, /* NEC */ |
| 95 | 2880, /* Samsung */ |
| 96 | 2880, /* BYD */ |
| 97 | 2880, /* Lishen */ |
Paul Parsons | 86e6c6b | 2011-11-23 22:37:15 +0000 | [diff] [blame] | 98 | 2880, /* NEC */ |
| 99 | #ifdef CONFIG_MACH_H4700 |
| 100 | 0, |
| 101 | 3600, /* HP iPAQ hx4700 3.7V 3600mAh (359114-001) */ |
| 102 | #endif |
Anton Vorontsov | fe0e315 | 2007-05-04 00:45:39 +0400 | [diff] [blame] | 103 | }; |
| 104 | |
| 105 | /* array is level at temps 0°C, 10°C, 20°C, 30°C, 40°C |
| 106 | * temp is in Celsius */ |
| 107 | static int battery_interpolate(int array[], int temp) |
| 108 | { |
| 109 | int index, dt; |
| 110 | |
| 111 | if (temp <= 0) |
| 112 | return array[0]; |
| 113 | if (temp >= 40) |
| 114 | return array[4]; |
| 115 | |
| 116 | index = temp / 10; |
| 117 | dt = temp % 10; |
| 118 | |
| 119 | return array[index] + (((array[index + 1] - array[index]) * dt) / 10); |
| 120 | } |
| 121 | |
| 122 | static int ds2760_battery_read_status(struct ds2760_device_info *di) |
| 123 | { |
| 124 | int ret, i, start, count, scale[5]; |
| 125 | |
| 126 | if (di->update_time && time_before(jiffies, di->update_time + |
| 127 | msecs_to_jiffies(cache_time))) |
| 128 | return 0; |
| 129 | |
| 130 | /* The first time we read the entire contents of SRAM/EEPROM, |
| 131 | * but after that we just read the interesting bits that change. */ |
| 132 | if (di->update_time == 0) { |
| 133 | start = 0; |
| 134 | count = DS2760_DATA_SIZE; |
| 135 | } else { |
| 136 | start = DS2760_VOLTAGE_MSB; |
| 137 | count = DS2760_TEMP_LSB - start + 1; |
| 138 | } |
| 139 | |
| 140 | ret = w1_ds2760_read(di->w1_dev, di->raw + start, start, count); |
| 141 | if (ret != count) { |
| 142 | dev_warn(di->dev, "call to w1_ds2760_read failed (0x%p)\n", |
| 143 | di->w1_dev); |
| 144 | return 1; |
| 145 | } |
| 146 | |
| 147 | di->update_time = jiffies; |
| 148 | |
| 149 | /* DS2760 reports voltage in units of 4.88mV, but the battery class |
| 150 | * reports in units of uV, so convert by multiplying by 4880. */ |
| 151 | di->voltage_raw = (di->raw[DS2760_VOLTAGE_MSB] << 3) | |
| 152 | (di->raw[DS2760_VOLTAGE_LSB] >> 5); |
| 153 | di->voltage_uV = di->voltage_raw * 4880; |
| 154 | |
| 155 | /* DS2760 reports current in signed units of 0.625mA, but the battery |
| 156 | * class reports in units of µA, so convert by multiplying by 625. */ |
| 157 | di->current_raw = |
| 158 | (((signed char)di->raw[DS2760_CURRENT_MSB]) << 5) | |
| 159 | (di->raw[DS2760_CURRENT_LSB] >> 3); |
| 160 | di->current_uA = di->current_raw * 625; |
| 161 | |
| 162 | /* DS2760 reports accumulated current in signed units of 0.25mAh. */ |
| 163 | di->accum_current_raw = |
| 164 | (((signed char)di->raw[DS2760_CURRENT_ACCUM_MSB]) << 8) | |
| 165 | di->raw[DS2760_CURRENT_ACCUM_LSB]; |
| 166 | di->accum_current_uAh = di->accum_current_raw * 250; |
| 167 | |
| 168 | /* DS2760 reports temperature in signed units of 0.125°C, but the |
| 169 | * battery class reports in units of 1/10 °C, so we convert by |
| 170 | * multiplying by .125 * 10 = 1.25. */ |
| 171 | di->temp_raw = (((signed char)di->raw[DS2760_TEMP_MSB]) << 3) | |
| 172 | (di->raw[DS2760_TEMP_LSB] >> 5); |
| 173 | di->temp_C = di->temp_raw + (di->temp_raw / 4); |
| 174 | |
| 175 | /* At least some battery monitors (e.g. HP iPAQ) store the battery's |
| 176 | * maximum rated capacity. */ |
| 177 | if (di->raw[DS2760_RATED_CAPACITY] < ARRAY_SIZE(rated_capacities)) |
| 178 | di->rated_capacity = rated_capacities[ |
| 179 | (unsigned int)di->raw[DS2760_RATED_CAPACITY]]; |
| 180 | else |
| 181 | di->rated_capacity = di->raw[DS2760_RATED_CAPACITY] * 10; |
| 182 | |
| 183 | di->rated_capacity *= 1000; /* convert to µAh */ |
| 184 | |
| 185 | /* Calculate the full level at the present temperature. */ |
| 186 | di->full_active_uAh = di->raw[DS2760_ACTIVE_FULL] << 8 | |
| 187 | di->raw[DS2760_ACTIVE_FULL + 1]; |
| 188 | |
Daniel Mack | 25f2bfa | 2009-07-15 18:20:41 +0200 | [diff] [blame] | 189 | /* If the full_active_uAh value is not given, fall back to the rated |
| 190 | * capacity. This is likely to happen when chips are not part of the |
| 191 | * battery pack and is therefore not bootstrapped. */ |
| 192 | if (di->full_active_uAh == 0) |
| 193 | di->full_active_uAh = di->rated_capacity / 1000L; |
| 194 | |
| 195 | scale[0] = di->full_active_uAh; |
Anton Vorontsov | fe0e315 | 2007-05-04 00:45:39 +0400 | [diff] [blame] | 196 | for (i = 1; i < 5; i++) |
Paul Parsons | 3af98a8 | 2011-04-25 14:43:30 +0000 | [diff] [blame] | 197 | scale[i] = scale[i - 1] + di->raw[DS2760_ACTIVE_FULL + 1 + i]; |
Anton Vorontsov | fe0e315 | 2007-05-04 00:45:39 +0400 | [diff] [blame] | 198 | |
| 199 | di->full_active_uAh = battery_interpolate(scale, di->temp_C / 10); |
| 200 | di->full_active_uAh *= 1000; /* convert to µAh */ |
| 201 | |
| 202 | /* Calculate the empty level at the present temperature. */ |
| 203 | scale[4] = di->raw[DS2760_ACTIVE_EMPTY + 4]; |
| 204 | for (i = 3; i >= 0; i--) |
| 205 | scale[i] = scale[i + 1] + di->raw[DS2760_ACTIVE_EMPTY + i]; |
| 206 | |
| 207 | di->empty_uAh = battery_interpolate(scale, di->temp_C / 10); |
| 208 | di->empty_uAh *= 1000; /* convert to µAh */ |
| 209 | |
Daniel Mack | a4e3f91 | 2009-03-12 14:31:30 -0700 | [diff] [blame] | 210 | if (di->full_active_uAh == di->empty_uAh) |
| 211 | di->rem_capacity = 0; |
| 212 | else |
| 213 | /* From Maxim Application Note 131: remaining capacity = |
| 214 | * ((ICA - Empty Value) / (Full Value - Empty Value)) x 100% */ |
| 215 | di->rem_capacity = ((di->accum_current_uAh - di->empty_uAh) * 100L) / |
| 216 | (di->full_active_uAh - di->empty_uAh); |
Anton Vorontsov | fe0e315 | 2007-05-04 00:45:39 +0400 | [diff] [blame] | 217 | |
| 218 | if (di->rem_capacity < 0) |
| 219 | di->rem_capacity = 0; |
| 220 | if (di->rem_capacity > 100) |
| 221 | di->rem_capacity = 100; |
| 222 | |
Sven Neumann | 86af950 | 2010-11-12 11:36:22 +0100 | [diff] [blame] | 223 | if (di->current_uA < -100L) |
Daniel Mack | b0525b4 | 2009-07-24 15:08:11 +0200 | [diff] [blame] | 224 | di->life_sec = -((di->accum_current_uAh - di->empty_uAh) * 36L) |
| 225 | / (di->current_uA / 100L); |
Anton Vorontsov | fe0e315 | 2007-05-04 00:45:39 +0400 | [diff] [blame] | 226 | else |
| 227 | di->life_sec = 0; |
| 228 | |
| 229 | return 0; |
| 230 | } |
| 231 | |
Daniel Mack | 02d0d27 | 2009-07-15 22:57:16 +0200 | [diff] [blame] | 232 | static void ds2760_battery_set_current_accum(struct ds2760_device_info *di, |
| 233 | unsigned int acr_val) |
| 234 | { |
| 235 | unsigned char acr[2]; |
| 236 | |
| 237 | /* acr is in units of 0.25 mAh */ |
| 238 | acr_val *= 4L; |
| 239 | acr_val /= 1000; |
| 240 | |
| 241 | acr[0] = acr_val >> 8; |
| 242 | acr[1] = acr_val & 0xff; |
| 243 | |
| 244 | if (w1_ds2760_write(di->w1_dev, acr, DS2760_CURRENT_ACCUM_MSB, 2) < 2) |
| 245 | dev_warn(di->dev, "ACR write failed\n"); |
| 246 | } |
| 247 | |
Anton Vorontsov | fe0e315 | 2007-05-04 00:45:39 +0400 | [diff] [blame] | 248 | static void ds2760_battery_update_status(struct ds2760_device_info *di) |
| 249 | { |
| 250 | int old_charge_status = di->charge_status; |
| 251 | |
| 252 | ds2760_battery_read_status(di); |
| 253 | |
| 254 | if (di->charge_status == POWER_SUPPLY_STATUS_UNKNOWN) |
| 255 | di->full_counter = 0; |
| 256 | |
| 257 | if (power_supply_am_i_supplied(&di->bat)) { |
| 258 | if (di->current_uA > 10000) { |
| 259 | di->charge_status = POWER_SUPPLY_STATUS_CHARGING; |
| 260 | di->full_counter = 0; |
| 261 | } else if (di->current_uA < -5000) { |
| 262 | if (di->charge_status != POWER_SUPPLY_STATUS_NOT_CHARGING) |
| 263 | dev_notice(di->dev, "not enough power to " |
| 264 | "charge\n"); |
| 265 | di->charge_status = POWER_SUPPLY_STATUS_NOT_CHARGING; |
| 266 | di->full_counter = 0; |
| 267 | } else if (di->current_uA < 10000 && |
| 268 | di->charge_status != POWER_SUPPLY_STATUS_FULL) { |
| 269 | |
| 270 | /* Don't consider the battery to be full unless |
| 271 | * we've seen the current < 10 mA at least two |
| 272 | * consecutive times. */ |
| 273 | |
| 274 | di->full_counter++; |
| 275 | |
| 276 | if (di->full_counter < 2) { |
| 277 | di->charge_status = POWER_SUPPLY_STATUS_CHARGING; |
| 278 | } else { |
Anton Vorontsov | fe0e315 | 2007-05-04 00:45:39 +0400 | [diff] [blame] | 279 | di->charge_status = POWER_SUPPLY_STATUS_FULL; |
Daniel Mack | 02d0d27 | 2009-07-15 22:57:16 +0200 | [diff] [blame] | 280 | ds2760_battery_set_current_accum(di, |
| 281 | di->full_active_uAh); |
Anton Vorontsov | fe0e315 | 2007-05-04 00:45:39 +0400 | [diff] [blame] | 282 | } |
| 283 | } |
| 284 | } else { |
| 285 | di->charge_status = POWER_SUPPLY_STATUS_DISCHARGING; |
| 286 | di->full_counter = 0; |
| 287 | } |
| 288 | |
| 289 | if (di->charge_status != old_charge_status) |
| 290 | power_supply_changed(&di->bat); |
Anton Vorontsov | fe0e315 | 2007-05-04 00:45:39 +0400 | [diff] [blame] | 291 | } |
| 292 | |
Daniel Mack | cef437e | 2009-04-28 10:55:02 +0200 | [diff] [blame] | 293 | static void ds2760_battery_write_status(struct ds2760_device_info *di, |
| 294 | char status) |
| 295 | { |
| 296 | if (status == di->raw[DS2760_STATUS_REG]) |
| 297 | return; |
| 298 | |
| 299 | w1_ds2760_write(di->w1_dev, &status, DS2760_STATUS_WRITE_REG, 1); |
| 300 | w1_ds2760_store_eeprom(di->w1_dev, DS2760_EEPROM_BLOCK1); |
| 301 | w1_ds2760_recall_eeprom(di->w1_dev, DS2760_EEPROM_BLOCK1); |
| 302 | } |
| 303 | |
Daniel Mack | c1e7219 | 2009-07-15 18:20:40 +0200 | [diff] [blame] | 304 | static void ds2760_battery_write_rated_capacity(struct ds2760_device_info *di, |
| 305 | unsigned char rated_capacity) |
| 306 | { |
| 307 | if (rated_capacity == di->raw[DS2760_RATED_CAPACITY]) |
| 308 | return; |
| 309 | |
| 310 | w1_ds2760_write(di->w1_dev, &rated_capacity, DS2760_RATED_CAPACITY, 1); |
| 311 | w1_ds2760_store_eeprom(di->w1_dev, DS2760_EEPROM_BLOCK1); |
| 312 | w1_ds2760_recall_eeprom(di->w1_dev, DS2760_EEPROM_BLOCK1); |
| 313 | } |
| 314 | |
Daniel Mack | bd52ca5 | 2010-05-18 21:49:53 +0200 | [diff] [blame] | 315 | static void ds2760_battery_write_active_full(struct ds2760_device_info *di, |
| 316 | int active_full) |
| 317 | { |
| 318 | unsigned char tmp[2] = { |
| 319 | active_full >> 8, |
| 320 | active_full & 0xff |
| 321 | }; |
| 322 | |
| 323 | if (tmp[0] == di->raw[DS2760_ACTIVE_FULL] && |
| 324 | tmp[1] == di->raw[DS2760_ACTIVE_FULL + 1]) |
| 325 | return; |
| 326 | |
| 327 | w1_ds2760_write(di->w1_dev, tmp, DS2760_ACTIVE_FULL, sizeof(tmp)); |
| 328 | w1_ds2760_store_eeprom(di->w1_dev, DS2760_EEPROM_BLOCK0); |
| 329 | w1_ds2760_recall_eeprom(di->w1_dev, DS2760_EEPROM_BLOCK0); |
| 330 | |
| 331 | /* Write to the di->raw[] buffer directly - the DS2760_ACTIVE_FULL |
| 332 | * values won't be read back by ds2760_battery_read_status() */ |
| 333 | di->raw[DS2760_ACTIVE_FULL] = tmp[0]; |
| 334 | di->raw[DS2760_ACTIVE_FULL + 1] = tmp[1]; |
| 335 | } |
| 336 | |
Anton Vorontsov | fe0e315 | 2007-05-04 00:45:39 +0400 | [diff] [blame] | 337 | static void ds2760_battery_work(struct work_struct *work) |
| 338 | { |
| 339 | struct ds2760_device_info *di = container_of(work, |
| 340 | struct ds2760_device_info, monitor_work.work); |
| 341 | const int interval = HZ * 60; |
| 342 | |
Harvey Harrison | 0cddc0a | 2008-04-29 00:58:29 -0700 | [diff] [blame] | 343 | dev_dbg(di->dev, "%s\n", __func__); |
Anton Vorontsov | fe0e315 | 2007-05-04 00:45:39 +0400 | [diff] [blame] | 344 | |
| 345 | ds2760_battery_update_status(di); |
| 346 | queue_delayed_work(di->monitor_wqueue, &di->monitor_work, interval); |
Anton Vorontsov | fe0e315 | 2007-05-04 00:45:39 +0400 | [diff] [blame] | 347 | } |
| 348 | |
| 349 | #define to_ds2760_device_info(x) container_of((x), struct ds2760_device_info, \ |
| 350 | bat); |
| 351 | |
| 352 | static void ds2760_battery_external_power_changed(struct power_supply *psy) |
| 353 | { |
| 354 | struct ds2760_device_info *di = to_ds2760_device_info(psy); |
| 355 | |
Harvey Harrison | 0cddc0a | 2008-04-29 00:58:29 -0700 | [diff] [blame] | 356 | dev_dbg(di->dev, "%s\n", __func__); |
Anton Vorontsov | fe0e315 | 2007-05-04 00:45:39 +0400 | [diff] [blame] | 357 | |
Tejun Heo | 41f63c5 | 2012-08-03 10:30:47 -0700 | [diff] [blame] | 358 | mod_delayed_work(di->monitor_wqueue, &di->monitor_work, HZ/10); |
Anton Vorontsov | fe0e315 | 2007-05-04 00:45:39 +0400 | [diff] [blame] | 359 | } |
| 360 | |
Daniel Mack | 8d631cc | 2009-07-23 20:35:54 +0200 | [diff] [blame] | 361 | |
| 362 | static void ds2760_battery_set_charged_work(struct work_struct *work) |
| 363 | { |
| 364 | char bias; |
| 365 | struct ds2760_device_info *di = container_of(work, |
| 366 | struct ds2760_device_info, set_charged_work.work); |
| 367 | |
| 368 | dev_dbg(di->dev, "%s\n", __func__); |
| 369 | |
| 370 | ds2760_battery_read_status(di); |
| 371 | |
| 372 | /* When we get notified by external circuitry that the battery is |
| 373 | * considered fully charged now, we know that there is no current |
| 374 | * flow any more. However, the ds2760's internal current meter is |
| 375 | * too inaccurate to rely on - spec say something ~15% failure. |
| 376 | * Hence, we use the current offset bias register to compensate |
| 377 | * that error. |
| 378 | */ |
| 379 | |
| 380 | if (!power_supply_am_i_supplied(&di->bat)) |
| 381 | return; |
| 382 | |
| 383 | bias = (signed char) di->current_raw + |
| 384 | (signed char) di->raw[DS2760_CURRENT_OFFSET_BIAS]; |
| 385 | |
| 386 | dev_dbg(di->dev, "%s: bias = %d\n", __func__, bias); |
| 387 | |
| 388 | w1_ds2760_write(di->w1_dev, &bias, DS2760_CURRENT_OFFSET_BIAS, 1); |
| 389 | w1_ds2760_store_eeprom(di->w1_dev, DS2760_EEPROM_BLOCK1); |
| 390 | w1_ds2760_recall_eeprom(di->w1_dev, DS2760_EEPROM_BLOCK1); |
| 391 | |
| 392 | /* Write to the di->raw[] buffer directly - the CURRENT_OFFSET_BIAS |
| 393 | * value won't be read back by ds2760_battery_read_status() */ |
| 394 | di->raw[DS2760_CURRENT_OFFSET_BIAS] = bias; |
| 395 | } |
| 396 | |
| 397 | static void ds2760_battery_set_charged(struct power_supply *psy) |
| 398 | { |
| 399 | struct ds2760_device_info *di = to_ds2760_device_info(psy); |
| 400 | |
| 401 | /* postpone the actual work by 20 secs. This is for debouncing GPIO |
| 402 | * signals and to let the current value settle. See AN4188. */ |
Tejun Heo | 41f63c5 | 2012-08-03 10:30:47 -0700 | [diff] [blame] | 403 | mod_delayed_work(di->monitor_wqueue, &di->set_charged_work, HZ * 20); |
Daniel Mack | 8d631cc | 2009-07-23 20:35:54 +0200 | [diff] [blame] | 404 | } |
| 405 | |
Anton Vorontsov | fe0e315 | 2007-05-04 00:45:39 +0400 | [diff] [blame] | 406 | static int ds2760_battery_get_property(struct power_supply *psy, |
| 407 | enum power_supply_property psp, |
| 408 | union power_supply_propval *val) |
| 409 | { |
| 410 | struct ds2760_device_info *di = to_ds2760_device_info(psy); |
| 411 | |
| 412 | switch (psp) { |
| 413 | case POWER_SUPPLY_PROP_STATUS: |
| 414 | val->intval = di->charge_status; |
| 415 | return 0; |
| 416 | default: |
| 417 | break; |
| 418 | } |
| 419 | |
| 420 | ds2760_battery_read_status(di); |
| 421 | |
| 422 | switch (psp) { |
| 423 | case POWER_SUPPLY_PROP_VOLTAGE_NOW: |
| 424 | val->intval = di->voltage_uV; |
| 425 | break; |
| 426 | case POWER_SUPPLY_PROP_CURRENT_NOW: |
| 427 | val->intval = di->current_uA; |
| 428 | break; |
| 429 | case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN: |
| 430 | val->intval = di->rated_capacity; |
| 431 | break; |
| 432 | case POWER_SUPPLY_PROP_CHARGE_FULL: |
| 433 | val->intval = di->full_active_uAh; |
| 434 | break; |
| 435 | case POWER_SUPPLY_PROP_CHARGE_EMPTY: |
| 436 | val->intval = di->empty_uAh; |
| 437 | break; |
| 438 | case POWER_SUPPLY_PROP_CHARGE_NOW: |
| 439 | val->intval = di->accum_current_uAh; |
| 440 | break; |
| 441 | case POWER_SUPPLY_PROP_TEMP: |
| 442 | val->intval = di->temp_C; |
| 443 | break; |
Daniel Mack | 5c6e9bf2 | 2009-07-15 18:20:39 +0200 | [diff] [blame] | 444 | case POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW: |
| 445 | val->intval = di->life_sec; |
| 446 | break; |
| 447 | case POWER_SUPPLY_PROP_CAPACITY: |
| 448 | val->intval = di->rem_capacity; |
| 449 | break; |
Anton Vorontsov | fe0e315 | 2007-05-04 00:45:39 +0400 | [diff] [blame] | 450 | default: |
| 451 | return -EINVAL; |
| 452 | } |
| 453 | |
| 454 | return 0; |
| 455 | } |
| 456 | |
Daniel Mack | bd52ca5 | 2010-05-18 21:49:53 +0200 | [diff] [blame] | 457 | static int ds2760_battery_set_property(struct power_supply *psy, |
| 458 | enum power_supply_property psp, |
| 459 | const union power_supply_propval *val) |
| 460 | { |
| 461 | struct ds2760_device_info *di = to_ds2760_device_info(psy); |
| 462 | |
| 463 | switch (psp) { |
| 464 | case POWER_SUPPLY_PROP_CHARGE_FULL: |
| 465 | /* the interface counts in uAh, convert the value */ |
| 466 | ds2760_battery_write_active_full(di, val->intval / 1000L); |
| 467 | break; |
| 468 | |
| 469 | case POWER_SUPPLY_PROP_CHARGE_NOW: |
| 470 | /* ds2760_battery_set_current_accum() does the conversion */ |
| 471 | ds2760_battery_set_current_accum(di, val->intval); |
| 472 | break; |
| 473 | |
| 474 | default: |
| 475 | return -EPERM; |
| 476 | } |
| 477 | |
| 478 | return 0; |
| 479 | } |
| 480 | |
| 481 | static int ds2760_battery_property_is_writeable(struct power_supply *psy, |
| 482 | enum power_supply_property psp) |
| 483 | { |
| 484 | switch (psp) { |
| 485 | case POWER_SUPPLY_PROP_CHARGE_FULL: |
| 486 | case POWER_SUPPLY_PROP_CHARGE_NOW: |
| 487 | return 1; |
| 488 | |
| 489 | default: |
| 490 | break; |
| 491 | } |
| 492 | |
| 493 | return 0; |
| 494 | } |
| 495 | |
Anton Vorontsov | fe0e315 | 2007-05-04 00:45:39 +0400 | [diff] [blame] | 496 | static enum power_supply_property ds2760_battery_props[] = { |
| 497 | POWER_SUPPLY_PROP_STATUS, |
| 498 | POWER_SUPPLY_PROP_VOLTAGE_NOW, |
| 499 | POWER_SUPPLY_PROP_CURRENT_NOW, |
| 500 | POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN, |
| 501 | POWER_SUPPLY_PROP_CHARGE_FULL, |
| 502 | POWER_SUPPLY_PROP_CHARGE_EMPTY, |
| 503 | POWER_SUPPLY_PROP_CHARGE_NOW, |
| 504 | POWER_SUPPLY_PROP_TEMP, |
Daniel Mack | 5c6e9bf2 | 2009-07-15 18:20:39 +0200 | [diff] [blame] | 505 | POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW, |
| 506 | POWER_SUPPLY_PROP_CAPACITY, |
Anton Vorontsov | fe0e315 | 2007-05-04 00:45:39 +0400 | [diff] [blame] | 507 | }; |
| 508 | |
| 509 | static int ds2760_battery_probe(struct platform_device *pdev) |
| 510 | { |
Daniel Mack | cef437e | 2009-04-28 10:55:02 +0200 | [diff] [blame] | 511 | char status; |
Anton Vorontsov | fe0e315 | 2007-05-04 00:45:39 +0400 | [diff] [blame] | 512 | int retval = 0; |
| 513 | struct ds2760_device_info *di; |
Anton Vorontsov | fe0e315 | 2007-05-04 00:45:39 +0400 | [diff] [blame] | 514 | |
| 515 | di = kzalloc(sizeof(*di), GFP_KERNEL); |
| 516 | if (!di) { |
| 517 | retval = -ENOMEM; |
| 518 | goto di_alloc_failed; |
| 519 | } |
| 520 | |
| 521 | platform_set_drvdata(pdev, di); |
| 522 | |
Daniel Mack | ae9fb6e | 2009-04-28 10:55:00 +0200 | [diff] [blame] | 523 | di->dev = &pdev->dev; |
| 524 | di->w1_dev = pdev->dev.parent; |
| 525 | di->bat.name = dev_name(&pdev->dev); |
| 526 | di->bat.type = POWER_SUPPLY_TYPE_BATTERY; |
| 527 | di->bat.properties = ds2760_battery_props; |
| 528 | di->bat.num_properties = ARRAY_SIZE(ds2760_battery_props); |
| 529 | di->bat.get_property = ds2760_battery_get_property; |
Daniel Mack | bd52ca5 | 2010-05-18 21:49:53 +0200 | [diff] [blame] | 530 | di->bat.set_property = ds2760_battery_set_property; |
| 531 | di->bat.property_is_writeable = |
| 532 | ds2760_battery_property_is_writeable; |
Daniel Mack | 8d631cc | 2009-07-23 20:35:54 +0200 | [diff] [blame] | 533 | di->bat.set_charged = ds2760_battery_set_charged; |
Anton Vorontsov | fe0e315 | 2007-05-04 00:45:39 +0400 | [diff] [blame] | 534 | di->bat.external_power_changed = |
| 535 | ds2760_battery_external_power_changed; |
| 536 | |
| 537 | di->charge_status = POWER_SUPPLY_STATUS_UNKNOWN; |
| 538 | |
Daniel Mack | cef437e | 2009-04-28 10:55:02 +0200 | [diff] [blame] | 539 | /* enable sleep mode feature */ |
| 540 | ds2760_battery_read_status(di); |
| 541 | status = di->raw[DS2760_STATUS_REG]; |
| 542 | if (pmod_enabled) |
| 543 | status |= DS2760_STATUS_PMOD; |
| 544 | else |
| 545 | status &= ~DS2760_STATUS_PMOD; |
| 546 | |
| 547 | ds2760_battery_write_status(di, status); |
| 548 | |
Daniel Mack | c1e7219 | 2009-07-15 18:20:40 +0200 | [diff] [blame] | 549 | /* set rated capacity from module param */ |
| 550 | if (rated_capacity) |
| 551 | ds2760_battery_write_rated_capacity(di, rated_capacity); |
| 552 | |
Daniel Mack | 02d0d27 | 2009-07-15 22:57:16 +0200 | [diff] [blame] | 553 | /* set current accumulator if given as parameter. |
| 554 | * this should only be done for bootstrapping the value */ |
| 555 | if (current_accum) |
| 556 | ds2760_battery_set_current_accum(di, current_accum); |
| 557 | |
Daniel Mack | 2e83a5c | 2009-07-15 18:20:38 +0200 | [diff] [blame] | 558 | retval = power_supply_register(&pdev->dev, &di->bat); |
| 559 | if (retval) { |
| 560 | dev_err(di->dev, "failed to register battery\n"); |
| 561 | goto batt_failed; |
| 562 | } |
| 563 | |
Anton Vorontsov | fe0e315 | 2007-05-04 00:45:39 +0400 | [diff] [blame] | 564 | INIT_DELAYED_WORK(&di->monitor_work, ds2760_battery_work); |
Daniel Mack | 8d631cc | 2009-07-23 20:35:54 +0200 | [diff] [blame] | 565 | INIT_DELAYED_WORK(&di->set_charged_work, |
| 566 | ds2760_battery_set_charged_work); |
Kay Sievers | ba88b00 | 2009-01-06 10:44:38 -0800 | [diff] [blame] | 567 | di->monitor_wqueue = create_singlethread_workqueue(dev_name(&pdev->dev)); |
Anton Vorontsov | fe0e315 | 2007-05-04 00:45:39 +0400 | [diff] [blame] | 568 | if (!di->monitor_wqueue) { |
| 569 | retval = -ESRCH; |
| 570 | goto workqueue_failed; |
| 571 | } |
| 572 | queue_delayed_work(di->monitor_wqueue, &di->monitor_work, HZ * 1); |
| 573 | |
| 574 | goto success; |
| 575 | |
| 576 | workqueue_failed: |
| 577 | power_supply_unregister(&di->bat); |
| 578 | batt_failed: |
| 579 | kfree(di); |
| 580 | di_alloc_failed: |
| 581 | success: |
| 582 | return retval; |
| 583 | } |
| 584 | |
| 585 | static int ds2760_battery_remove(struct platform_device *pdev) |
| 586 | { |
| 587 | struct ds2760_device_info *di = platform_get_drvdata(pdev); |
| 588 | |
Tejun Heo | afe2c51 | 2010-12-14 16:21:17 +0100 | [diff] [blame] | 589 | cancel_delayed_work_sync(&di->monitor_work); |
| 590 | cancel_delayed_work_sync(&di->set_charged_work); |
Anton Vorontsov | fe0e315 | 2007-05-04 00:45:39 +0400 | [diff] [blame] | 591 | destroy_workqueue(di->monitor_wqueue); |
| 592 | power_supply_unregister(&di->bat); |
Axel Lin | a01bce6 | 2010-09-27 15:07:12 +0800 | [diff] [blame] | 593 | kfree(di); |
Anton Vorontsov | fe0e315 | 2007-05-04 00:45:39 +0400 | [diff] [blame] | 594 | |
| 595 | return 0; |
| 596 | } |
| 597 | |
| 598 | #ifdef CONFIG_PM |
| 599 | |
| 600 | static int ds2760_battery_suspend(struct platform_device *pdev, |
| 601 | pm_message_t state) |
| 602 | { |
| 603 | struct ds2760_device_info *di = platform_get_drvdata(pdev); |
| 604 | |
| 605 | di->charge_status = POWER_SUPPLY_STATUS_UNKNOWN; |
| 606 | |
| 607 | return 0; |
| 608 | } |
| 609 | |
| 610 | static int ds2760_battery_resume(struct platform_device *pdev) |
| 611 | { |
| 612 | struct ds2760_device_info *di = platform_get_drvdata(pdev); |
| 613 | |
| 614 | di->charge_status = POWER_SUPPLY_STATUS_UNKNOWN; |
| 615 | power_supply_changed(&di->bat); |
| 616 | |
Tejun Heo | 41f63c5 | 2012-08-03 10:30:47 -0700 | [diff] [blame] | 617 | mod_delayed_work(di->monitor_wqueue, &di->monitor_work, HZ); |
Anton Vorontsov | fe0e315 | 2007-05-04 00:45:39 +0400 | [diff] [blame] | 618 | |
| 619 | return 0; |
| 620 | } |
| 621 | |
| 622 | #else |
| 623 | |
| 624 | #define ds2760_battery_suspend NULL |
| 625 | #define ds2760_battery_resume NULL |
| 626 | |
| 627 | #endif /* CONFIG_PM */ |
| 628 | |
Kay Sievers | 2f5a5cf | 2008-07-25 01:45:46 -0700 | [diff] [blame] | 629 | MODULE_ALIAS("platform:ds2760-battery"); |
| 630 | |
Anton Vorontsov | fe0e315 | 2007-05-04 00:45:39 +0400 | [diff] [blame] | 631 | static struct platform_driver ds2760_battery_driver = { |
| 632 | .driver = { |
| 633 | .name = "ds2760-battery", |
| 634 | }, |
| 635 | .probe = ds2760_battery_probe, |
| 636 | .remove = ds2760_battery_remove, |
| 637 | .suspend = ds2760_battery_suspend, |
| 638 | .resume = ds2760_battery_resume, |
| 639 | }; |
| 640 | |
Axel Lin | 300bac7 | 2011-11-26 12:01:10 +0800 | [diff] [blame] | 641 | module_platform_driver(ds2760_battery_driver); |
Anton Vorontsov | fe0e315 | 2007-05-04 00:45:39 +0400 | [diff] [blame] | 642 | |
| 643 | MODULE_LICENSE("GPL"); |
| 644 | MODULE_AUTHOR("Szabolcs Gyurko <szabolcs.gyurko@tlt.hu>, " |
| 645 | "Matt Reimer <mreimer@vpop.net>, " |
| 646 | "Anton Vorontsov <cbou@mail.ru>"); |
| 647 | MODULE_DESCRIPTION("ds2760 battery driver"); |