ds2760_battery.c revision 25f2bfa62ae77820a8185734c4a2ab7f3971a2fc
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> 27#include <linux/platform_device.h> 28#include <linux/power_supply.h> 29 30#include "../w1/w1.h" 31#include "../w1/slaves/w1_ds2760.h" 32 33struct ds2760_device_info { 34 struct device *dev; 35 36 /* DS2760 data, valid after calling ds2760_battery_read_status() */ 37 unsigned long update_time; /* jiffies when data read */ 38 char raw[DS2760_DATA_SIZE]; /* raw DS2760 data */ 39 int voltage_raw; /* units of 4.88 mV */ 40 int voltage_uV; /* units of µV */ 41 int current_raw; /* units of 0.625 mA */ 42 int current_uA; /* units of µA */ 43 int accum_current_raw; /* units of 0.25 mAh */ 44 int accum_current_uAh; /* units of µAh */ 45 int temp_raw; /* units of 0.125 °C */ 46 int temp_C; /* units of 0.1 °C */ 47 int rated_capacity; /* units of µAh */ 48 int rem_capacity; /* percentage */ 49 int full_active_uAh; /* units of µAh */ 50 int empty_uAh; /* units of µAh */ 51 int life_sec; /* units of seconds */ 52 int charge_status; /* POWER_SUPPLY_STATUS_* */ 53 54 int full_counter; 55 struct power_supply bat; 56 struct device *w1_dev; 57 struct workqueue_struct *monitor_wqueue; 58 struct delayed_work monitor_work; 59}; 60 61static unsigned int cache_time = 1000; 62module_param(cache_time, uint, 0644); 63MODULE_PARM_DESC(cache_time, "cache time in milliseconds"); 64 65static unsigned int pmod_enabled; 66module_param(pmod_enabled, bool, 0644); 67MODULE_PARM_DESC(pmod_enabled, "PMOD enable bit"); 68 69static unsigned int rated_capacity; 70module_param(rated_capacity, uint, 0644); 71MODULE_PARM_DESC(rated_capacity, "rated battery capacity, 10*mAh or index"); 72 73/* Some batteries have their rated capacity stored a N * 10 mAh, while 74 * others use an index into this table. */ 75static int rated_capacities[] = { 76 0, 77 920, /* Samsung */ 78 920, /* BYD */ 79 920, /* Lishen */ 80 920, /* NEC */ 81 1440, /* Samsung */ 82 1440, /* BYD */ 83 1440, /* Lishen */ 84 1440, /* NEC */ 85 2880, /* Samsung */ 86 2880, /* BYD */ 87 2880, /* Lishen */ 88 2880 /* NEC */ 89}; 90 91/* array is level at temps 0°C, 10°C, 20°C, 30°C, 40°C 92 * temp is in Celsius */ 93static int battery_interpolate(int array[], int temp) 94{ 95 int index, dt; 96 97 if (temp <= 0) 98 return array[0]; 99 if (temp >= 40) 100 return array[4]; 101 102 index = temp / 10; 103 dt = temp % 10; 104 105 return array[index] + (((array[index + 1] - array[index]) * dt) / 10); 106} 107 108static int ds2760_battery_read_status(struct ds2760_device_info *di) 109{ 110 int ret, i, start, count, scale[5]; 111 112 if (di->update_time && time_before(jiffies, di->update_time + 113 msecs_to_jiffies(cache_time))) 114 return 0; 115 116 /* The first time we read the entire contents of SRAM/EEPROM, 117 * but after that we just read the interesting bits that change. */ 118 if (di->update_time == 0) { 119 start = 0; 120 count = DS2760_DATA_SIZE; 121 } else { 122 start = DS2760_VOLTAGE_MSB; 123 count = DS2760_TEMP_LSB - start + 1; 124 } 125 126 ret = w1_ds2760_read(di->w1_dev, di->raw + start, start, count); 127 if (ret != count) { 128 dev_warn(di->dev, "call to w1_ds2760_read failed (0x%p)\n", 129 di->w1_dev); 130 return 1; 131 } 132 133 di->update_time = jiffies; 134 135 /* DS2760 reports voltage in units of 4.88mV, but the battery class 136 * reports in units of uV, so convert by multiplying by 4880. */ 137 di->voltage_raw = (di->raw[DS2760_VOLTAGE_MSB] << 3) | 138 (di->raw[DS2760_VOLTAGE_LSB] >> 5); 139 di->voltage_uV = di->voltage_raw * 4880; 140 141 /* DS2760 reports current in signed units of 0.625mA, but the battery 142 * class reports in units of µA, so convert by multiplying by 625. */ 143 di->current_raw = 144 (((signed char)di->raw[DS2760_CURRENT_MSB]) << 5) | 145 (di->raw[DS2760_CURRENT_LSB] >> 3); 146 di->current_uA = di->current_raw * 625; 147 148 /* DS2760 reports accumulated current in signed units of 0.25mAh. */ 149 di->accum_current_raw = 150 (((signed char)di->raw[DS2760_CURRENT_ACCUM_MSB]) << 8) | 151 di->raw[DS2760_CURRENT_ACCUM_LSB]; 152 di->accum_current_uAh = di->accum_current_raw * 250; 153 154 /* DS2760 reports temperature in signed units of 0.125°C, but the 155 * battery class reports in units of 1/10 °C, so we convert by 156 * multiplying by .125 * 10 = 1.25. */ 157 di->temp_raw = (((signed char)di->raw[DS2760_TEMP_MSB]) << 3) | 158 (di->raw[DS2760_TEMP_LSB] >> 5); 159 di->temp_C = di->temp_raw + (di->temp_raw / 4); 160 161 /* At least some battery monitors (e.g. HP iPAQ) store the battery's 162 * maximum rated capacity. */ 163 if (di->raw[DS2760_RATED_CAPACITY] < ARRAY_SIZE(rated_capacities)) 164 di->rated_capacity = rated_capacities[ 165 (unsigned int)di->raw[DS2760_RATED_CAPACITY]]; 166 else 167 di->rated_capacity = di->raw[DS2760_RATED_CAPACITY] * 10; 168 169 di->rated_capacity *= 1000; /* convert to µAh */ 170 171 /* Calculate the full level at the present temperature. */ 172 di->full_active_uAh = di->raw[DS2760_ACTIVE_FULL] << 8 | 173 di->raw[DS2760_ACTIVE_FULL + 1]; 174 175 /* If the full_active_uAh value is not given, fall back to the rated 176 * capacity. This is likely to happen when chips are not part of the 177 * battery pack and is therefore not bootstrapped. */ 178 if (di->full_active_uAh == 0) 179 di->full_active_uAh = di->rated_capacity / 1000L; 180 181 scale[0] = di->full_active_uAh; 182 for (i = 1; i < 5; i++) 183 scale[i] = scale[i - 1] + di->raw[DS2760_ACTIVE_FULL + 2 + i]; 184 185 di->full_active_uAh = battery_interpolate(scale, di->temp_C / 10); 186 di->full_active_uAh *= 1000; /* convert to µAh */ 187 188 /* Calculate the empty level at the present temperature. */ 189 scale[4] = di->raw[DS2760_ACTIVE_EMPTY + 4]; 190 for (i = 3; i >= 0; i--) 191 scale[i] = scale[i + 1] + di->raw[DS2760_ACTIVE_EMPTY + i]; 192 193 di->empty_uAh = battery_interpolate(scale, di->temp_C / 10); 194 di->empty_uAh *= 1000; /* convert to µAh */ 195 196 if (di->full_active_uAh == di->empty_uAh) 197 di->rem_capacity = 0; 198 else 199 /* From Maxim Application Note 131: remaining capacity = 200 * ((ICA - Empty Value) / (Full Value - Empty Value)) x 100% */ 201 di->rem_capacity = ((di->accum_current_uAh - di->empty_uAh) * 100L) / 202 (di->full_active_uAh - di->empty_uAh); 203 204 if (di->rem_capacity < 0) 205 di->rem_capacity = 0; 206 if (di->rem_capacity > 100) 207 di->rem_capacity = 100; 208 209 if (di->current_uA) 210 di->life_sec = -((di->accum_current_uAh - di->empty_uAh) * 211 3600L) / di->current_uA; 212 else 213 di->life_sec = 0; 214 215 return 0; 216} 217 218static void ds2760_battery_update_status(struct ds2760_device_info *di) 219{ 220 int old_charge_status = di->charge_status; 221 222 ds2760_battery_read_status(di); 223 224 if (di->charge_status == POWER_SUPPLY_STATUS_UNKNOWN) 225 di->full_counter = 0; 226 227 if (power_supply_am_i_supplied(&di->bat)) { 228 if (di->current_uA > 10000) { 229 di->charge_status = POWER_SUPPLY_STATUS_CHARGING; 230 di->full_counter = 0; 231 } else if (di->current_uA < -5000) { 232 if (di->charge_status != POWER_SUPPLY_STATUS_NOT_CHARGING) 233 dev_notice(di->dev, "not enough power to " 234 "charge\n"); 235 di->charge_status = POWER_SUPPLY_STATUS_NOT_CHARGING; 236 di->full_counter = 0; 237 } else if (di->current_uA < 10000 && 238 di->charge_status != POWER_SUPPLY_STATUS_FULL) { 239 240 /* Don't consider the battery to be full unless 241 * we've seen the current < 10 mA at least two 242 * consecutive times. */ 243 244 di->full_counter++; 245 246 if (di->full_counter < 2) { 247 di->charge_status = POWER_SUPPLY_STATUS_CHARGING; 248 } else { 249 unsigned char acr[2]; 250 int acr_val; 251 252 /* acr is in units of 0.25 mAh */ 253 acr_val = di->full_active_uAh * 4L / 1000; 254 255 acr[0] = acr_val >> 8; 256 acr[1] = acr_val & 0xff; 257 258 if (w1_ds2760_write(di->w1_dev, acr, 259 DS2760_CURRENT_ACCUM_MSB, 2) < 2) 260 dev_warn(di->dev, 261 "ACR reset failed\n"); 262 263 di->charge_status = POWER_SUPPLY_STATUS_FULL; 264 } 265 } 266 } else { 267 di->charge_status = POWER_SUPPLY_STATUS_DISCHARGING; 268 di->full_counter = 0; 269 } 270 271 if (di->charge_status != old_charge_status) 272 power_supply_changed(&di->bat); 273} 274 275static void ds2760_battery_write_status(struct ds2760_device_info *di, 276 char status) 277{ 278 if (status == di->raw[DS2760_STATUS_REG]) 279 return; 280 281 w1_ds2760_write(di->w1_dev, &status, DS2760_STATUS_WRITE_REG, 1); 282 w1_ds2760_store_eeprom(di->w1_dev, DS2760_EEPROM_BLOCK1); 283 w1_ds2760_recall_eeprom(di->w1_dev, DS2760_EEPROM_BLOCK1); 284} 285 286static void ds2760_battery_write_rated_capacity(struct ds2760_device_info *di, 287 unsigned char rated_capacity) 288{ 289 if (rated_capacity == di->raw[DS2760_RATED_CAPACITY]) 290 return; 291 292 w1_ds2760_write(di->w1_dev, &rated_capacity, DS2760_RATED_CAPACITY, 1); 293 w1_ds2760_store_eeprom(di->w1_dev, DS2760_EEPROM_BLOCK1); 294 w1_ds2760_recall_eeprom(di->w1_dev, DS2760_EEPROM_BLOCK1); 295} 296 297static void ds2760_battery_work(struct work_struct *work) 298{ 299 struct ds2760_device_info *di = container_of(work, 300 struct ds2760_device_info, monitor_work.work); 301 const int interval = HZ * 60; 302 303 dev_dbg(di->dev, "%s\n", __func__); 304 305 ds2760_battery_update_status(di); 306 queue_delayed_work(di->monitor_wqueue, &di->monitor_work, interval); 307} 308 309#define to_ds2760_device_info(x) container_of((x), struct ds2760_device_info, \ 310 bat); 311 312static void ds2760_battery_external_power_changed(struct power_supply *psy) 313{ 314 struct ds2760_device_info *di = to_ds2760_device_info(psy); 315 316 dev_dbg(di->dev, "%s\n", __func__); 317 318 cancel_delayed_work(&di->monitor_work); 319 queue_delayed_work(di->monitor_wqueue, &di->monitor_work, HZ/10); 320} 321 322static int ds2760_battery_get_property(struct power_supply *psy, 323 enum power_supply_property psp, 324 union power_supply_propval *val) 325{ 326 struct ds2760_device_info *di = to_ds2760_device_info(psy); 327 328 switch (psp) { 329 case POWER_SUPPLY_PROP_STATUS: 330 val->intval = di->charge_status; 331 return 0; 332 default: 333 break; 334 } 335 336 ds2760_battery_read_status(di); 337 338 switch (psp) { 339 case POWER_SUPPLY_PROP_VOLTAGE_NOW: 340 val->intval = di->voltage_uV; 341 break; 342 case POWER_SUPPLY_PROP_CURRENT_NOW: 343 val->intval = di->current_uA; 344 break; 345 case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN: 346 val->intval = di->rated_capacity; 347 break; 348 case POWER_SUPPLY_PROP_CHARGE_FULL: 349 val->intval = di->full_active_uAh; 350 break; 351 case POWER_SUPPLY_PROP_CHARGE_EMPTY: 352 val->intval = di->empty_uAh; 353 break; 354 case POWER_SUPPLY_PROP_CHARGE_NOW: 355 val->intval = di->accum_current_uAh; 356 break; 357 case POWER_SUPPLY_PROP_TEMP: 358 val->intval = di->temp_C; 359 break; 360 case POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW: 361 val->intval = di->life_sec; 362 break; 363 case POWER_SUPPLY_PROP_CAPACITY: 364 val->intval = di->rem_capacity; 365 break; 366 default: 367 return -EINVAL; 368 } 369 370 return 0; 371} 372 373static enum power_supply_property ds2760_battery_props[] = { 374 POWER_SUPPLY_PROP_STATUS, 375 POWER_SUPPLY_PROP_VOLTAGE_NOW, 376 POWER_SUPPLY_PROP_CURRENT_NOW, 377 POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN, 378 POWER_SUPPLY_PROP_CHARGE_FULL, 379 POWER_SUPPLY_PROP_CHARGE_EMPTY, 380 POWER_SUPPLY_PROP_CHARGE_NOW, 381 POWER_SUPPLY_PROP_TEMP, 382 POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW, 383 POWER_SUPPLY_PROP_CAPACITY, 384}; 385 386static int ds2760_battery_probe(struct platform_device *pdev) 387{ 388 char status; 389 int retval = 0; 390 struct ds2760_device_info *di; 391 392 di = kzalloc(sizeof(*di), GFP_KERNEL); 393 if (!di) { 394 retval = -ENOMEM; 395 goto di_alloc_failed; 396 } 397 398 platform_set_drvdata(pdev, di); 399 400 di->dev = &pdev->dev; 401 di->w1_dev = pdev->dev.parent; 402 di->bat.name = dev_name(&pdev->dev); 403 di->bat.type = POWER_SUPPLY_TYPE_BATTERY; 404 di->bat.properties = ds2760_battery_props; 405 di->bat.num_properties = ARRAY_SIZE(ds2760_battery_props); 406 di->bat.get_property = ds2760_battery_get_property; 407 di->bat.external_power_changed = 408 ds2760_battery_external_power_changed; 409 410 di->charge_status = POWER_SUPPLY_STATUS_UNKNOWN; 411 412 /* enable sleep mode feature */ 413 ds2760_battery_read_status(di); 414 status = di->raw[DS2760_STATUS_REG]; 415 if (pmod_enabled) 416 status |= DS2760_STATUS_PMOD; 417 else 418 status &= ~DS2760_STATUS_PMOD; 419 420 ds2760_battery_write_status(di, status); 421 422 /* set rated capacity from module param */ 423 if (rated_capacity) 424 ds2760_battery_write_rated_capacity(di, rated_capacity); 425 426 retval = power_supply_register(&pdev->dev, &di->bat); 427 if (retval) { 428 dev_err(di->dev, "failed to register battery\n"); 429 goto batt_failed; 430 } 431 432 INIT_DELAYED_WORK(&di->monitor_work, ds2760_battery_work); 433 di->monitor_wqueue = create_singlethread_workqueue(dev_name(&pdev->dev)); 434 if (!di->monitor_wqueue) { 435 retval = -ESRCH; 436 goto workqueue_failed; 437 } 438 queue_delayed_work(di->monitor_wqueue, &di->monitor_work, HZ * 1); 439 440 goto success; 441 442workqueue_failed: 443 power_supply_unregister(&di->bat); 444batt_failed: 445 kfree(di); 446di_alloc_failed: 447success: 448 return retval; 449} 450 451static int ds2760_battery_remove(struct platform_device *pdev) 452{ 453 struct ds2760_device_info *di = platform_get_drvdata(pdev); 454 455 cancel_rearming_delayed_workqueue(di->monitor_wqueue, 456 &di->monitor_work); 457 destroy_workqueue(di->monitor_wqueue); 458 power_supply_unregister(&di->bat); 459 460 return 0; 461} 462 463#ifdef CONFIG_PM 464 465static int ds2760_battery_suspend(struct platform_device *pdev, 466 pm_message_t state) 467{ 468 struct ds2760_device_info *di = platform_get_drvdata(pdev); 469 470 di->charge_status = POWER_SUPPLY_STATUS_UNKNOWN; 471 472 return 0; 473} 474 475static int ds2760_battery_resume(struct platform_device *pdev) 476{ 477 struct ds2760_device_info *di = platform_get_drvdata(pdev); 478 479 di->charge_status = POWER_SUPPLY_STATUS_UNKNOWN; 480 power_supply_changed(&di->bat); 481 482 cancel_delayed_work(&di->monitor_work); 483 queue_delayed_work(di->monitor_wqueue, &di->monitor_work, HZ); 484 485 return 0; 486} 487 488#else 489 490#define ds2760_battery_suspend NULL 491#define ds2760_battery_resume NULL 492 493#endif /* CONFIG_PM */ 494 495MODULE_ALIAS("platform:ds2760-battery"); 496 497static struct platform_driver ds2760_battery_driver = { 498 .driver = { 499 .name = "ds2760-battery", 500 }, 501 .probe = ds2760_battery_probe, 502 .remove = ds2760_battery_remove, 503 .suspend = ds2760_battery_suspend, 504 .resume = ds2760_battery_resume, 505}; 506 507static int __init ds2760_battery_init(void) 508{ 509 return platform_driver_register(&ds2760_battery_driver); 510} 511 512static void __exit ds2760_battery_exit(void) 513{ 514 platform_driver_unregister(&ds2760_battery_driver); 515} 516 517module_init(ds2760_battery_init); 518module_exit(ds2760_battery_exit); 519 520MODULE_LICENSE("GPL"); 521MODULE_AUTHOR("Szabolcs Gyurko <szabolcs.gyurko@tlt.hu>, " 522 "Matt Reimer <mreimer@vpop.net>, " 523 "Anton Vorontsov <cbou@mail.ru>"); 524MODULE_DESCRIPTION("ds2760 battery driver"); 525