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