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/slab.h> 28 #include <linux/platform_device.h> 29 #include <linux/power_supply.h> 30 #include <linux/suspend.h> 31 #include <linux/w1.h> 32 #include <linux/of.h> 33 34 static unsigned int cache_time = 1000; 35 module_param(cache_time, uint, 0644); 36 MODULE_PARM_DESC(cache_time, "cache time in milliseconds"); 37 38 static bool pmod_enabled; 39 module_param(pmod_enabled, bool, 0644); 40 MODULE_PARM_DESC(pmod_enabled, "PMOD enable bit"); 41 42 static unsigned int rated_capacity; 43 module_param(rated_capacity, uint, 0644); 44 MODULE_PARM_DESC(rated_capacity, "rated battery capacity, 10*mAh or index"); 45 46 static unsigned int current_accum; 47 module_param(current_accum, uint, 0644); 48 MODULE_PARM_DESC(current_accum, "current accumulator value"); 49 50 #define W1_FAMILY_DS2760 0x30 51 52 /* Known commands to the DS2760 chip */ 53 #define W1_DS2760_SWAP 0xAA 54 #define W1_DS2760_READ_DATA 0x69 55 #define W1_DS2760_WRITE_DATA 0x6C 56 #define W1_DS2760_COPY_DATA 0x48 57 #define W1_DS2760_RECALL_DATA 0xB8 58 #define W1_DS2760_LOCK 0x6A 59 60 /* Number of valid register addresses */ 61 #define DS2760_DATA_SIZE 0x40 62 63 #define DS2760_PROTECTION_REG 0x00 64 65 #define DS2760_STATUS_REG 0x01 66 #define DS2760_STATUS_IE (1 << 2) 67 #define DS2760_STATUS_SWEN (1 << 3) 68 #define DS2760_STATUS_RNAOP (1 << 4) 69 #define DS2760_STATUS_PMOD (1 << 5) 70 71 #define DS2760_EEPROM_REG 0x07 72 #define DS2760_SPECIAL_FEATURE_REG 0x08 73 #define DS2760_VOLTAGE_MSB 0x0c 74 #define DS2760_VOLTAGE_LSB 0x0d 75 #define DS2760_CURRENT_MSB 0x0e 76 #define DS2760_CURRENT_LSB 0x0f 77 #define DS2760_CURRENT_ACCUM_MSB 0x10 78 #define DS2760_CURRENT_ACCUM_LSB 0x11 79 #define DS2760_TEMP_MSB 0x18 80 #define DS2760_TEMP_LSB 0x19 81 #define DS2760_EEPROM_BLOCK0 0x20 82 #define DS2760_ACTIVE_FULL 0x20 83 #define DS2760_EEPROM_BLOCK1 0x30 84 #define DS2760_STATUS_WRITE_REG 0x31 85 #define DS2760_RATED_CAPACITY 0x32 86 #define DS2760_CURRENT_OFFSET_BIAS 0x33 87 #define DS2760_ACTIVE_EMPTY 0x3b 88 89 struct ds2760_device_info { 90 struct device *dev; 91 92 /* DS2760 data, valid after calling ds2760_battery_read_status() */ 93 unsigned long update_time; /* jiffies when data read */ 94 char raw[DS2760_DATA_SIZE]; /* raw DS2760 data */ 95 int voltage_raw; /* units of 4.88 mV */ 96 int voltage_uV; /* units of µV */ 97 int current_raw; /* units of 0.625 mA */ 98 int current_uA; /* units of µA */ 99 int accum_current_raw; /* units of 0.25 mAh */ 100 int accum_current_uAh; /* units of µAh */ 101 int temp_raw; /* units of 0.125 °C */ 102 int temp_C; /* units of 0.1 °C */ 103 int rated_capacity; /* units of µAh */ 104 int rem_capacity; /* percentage */ 105 int full_active_uAh; /* units of µAh */ 106 int empty_uAh; /* units of µAh */ 107 int life_sec; /* units of seconds */ 108 int charge_status; /* POWER_SUPPLY_STATUS_* */ 109 110 int full_counter; 111 struct power_supply *bat; 112 struct power_supply_desc bat_desc; 113 struct workqueue_struct *monitor_wqueue; 114 struct delayed_work monitor_work; 115 struct delayed_work set_charged_work; 116 struct notifier_block pm_notifier; 117 }; 118 119 static int w1_ds2760_io(struct device *dev, char *buf, int addr, size_t count, 120 int io) 121 { 122 struct w1_slave *sl = container_of(dev, struct w1_slave, dev); 123 124 if (!dev) 125 return 0; 126 127 mutex_lock(&sl->master->bus_mutex); 128 129 if (addr > DS2760_DATA_SIZE || addr < 0) { 130 count = 0; 131 goto out; 132 } 133 if (addr + count > DS2760_DATA_SIZE) 134 count = DS2760_DATA_SIZE - addr; 135 136 if (!w1_reset_select_slave(sl)) { 137 if (!io) { 138 w1_write_8(sl->master, W1_DS2760_READ_DATA); 139 w1_write_8(sl->master, addr); 140 count = w1_read_block(sl->master, buf, count); 141 } else { 142 w1_write_8(sl->master, W1_DS2760_WRITE_DATA); 143 w1_write_8(sl->master, addr); 144 w1_write_block(sl->master, buf, count); 145 /* XXX w1_write_block returns void, not n_written */ 146 } 147 } 148 149 out: 150 mutex_unlock(&sl->master->bus_mutex); 151 152 return count; 153 } 154 155 static int w1_ds2760_read(struct device *dev, 156 char *buf, int addr, 157 size_t count) 158 { 159 return w1_ds2760_io(dev, buf, addr, count, 0); 160 } 161 162 static int w1_ds2760_write(struct device *dev, 163 char *buf, 164 int addr, size_t count) 165 { 166 return w1_ds2760_io(dev, buf, addr, count, 1); 167 } 168 169 static int w1_ds2760_eeprom_cmd(struct device *dev, int addr, int cmd) 170 { 171 struct w1_slave *sl = container_of(dev, struct w1_slave, dev); 172 173 if (!dev) 174 return -EINVAL; 175 176 mutex_lock(&sl->master->bus_mutex); 177 178 if (w1_reset_select_slave(sl) == 0) { 179 w1_write_8(sl->master, cmd); 180 w1_write_8(sl->master, addr); 181 } 182 183 mutex_unlock(&sl->master->bus_mutex); 184 return 0; 185 } 186 187 static int w1_ds2760_store_eeprom(struct device *dev, int addr) 188 { 189 return w1_ds2760_eeprom_cmd(dev, addr, W1_DS2760_COPY_DATA); 190 } 191 192 static int w1_ds2760_recall_eeprom(struct device *dev, int addr) 193 { 194 return w1_ds2760_eeprom_cmd(dev, addr, W1_DS2760_RECALL_DATA); 195 } 196 197 static ssize_t w1_slave_read(struct file *filp, struct kobject *kobj, 198 struct bin_attribute *bin_attr, char *buf, 199 loff_t off, size_t count) 200 { 201 struct device *dev = kobj_to_dev(kobj); 202 return w1_ds2760_read(dev, buf, off, count); 203 } 204 205 static BIN_ATTR_RO(w1_slave, DS2760_DATA_SIZE); 206 207 static struct bin_attribute *w1_ds2760_bin_attrs[] = { 208 &bin_attr_w1_slave, 209 NULL, 210 }; 211 212 static const struct attribute_group w1_ds2760_group = { 213 .bin_attrs = w1_ds2760_bin_attrs, 214 }; 215 216 static const struct attribute_group *w1_ds2760_groups[] = { 217 &w1_ds2760_group, 218 NULL, 219 }; 220 /* Some batteries have their rated capacity stored a N * 10 mAh, while 221 * others use an index into this table. */ 222 static int rated_capacities[] = { 223 0, 224 920, /* Samsung */ 225 920, /* BYD */ 226 920, /* Lishen */ 227 920, /* NEC */ 228 1440, /* Samsung */ 229 1440, /* BYD */ 230 1440, /* Lishen */ 231 1440, /* NEC */ 232 2880, /* Samsung */ 233 2880, /* BYD */ 234 2880, /* Lishen */ 235 2880, /* NEC */ 236 }; 237 238 /* array is level at temps 0°C, 10°C, 20°C, 30°C, 40°C 239 * temp is in Celsius */ 240 static int battery_interpolate(int array[], int temp) 241 { 242 int index, dt; 243 244 if (temp <= 0) 245 return array[0]; 246 if (temp >= 40) 247 return array[4]; 248 249 index = temp / 10; 250 dt = temp % 10; 251 252 return array[index] + (((array[index + 1] - array[index]) * dt) / 10); 253 } 254 255 static int ds2760_battery_read_status(struct ds2760_device_info *di) 256 { 257 int ret, i, start, count, scale[5]; 258 259 if (di->update_time && time_before(jiffies, di->update_time + 260 msecs_to_jiffies(cache_time))) 261 return 0; 262 263 /* The first time we read the entire contents of SRAM/EEPROM, 264 * but after that we just read the interesting bits that change. */ 265 if (di->update_time == 0) { 266 start = 0; 267 count = DS2760_DATA_SIZE; 268 } else { 269 start = DS2760_VOLTAGE_MSB; 270 count = DS2760_TEMP_LSB - start + 1; 271 } 272 273 ret = w1_ds2760_read(di->dev, di->raw + start, start, count); 274 if (ret != count) { 275 dev_warn(di->dev, "call to w1_ds2760_read failed (0x%p)\n", 276 di->dev); 277 return 1; 278 } 279 280 di->update_time = jiffies; 281 282 /* DS2760 reports voltage in units of 4.88mV, but the battery class 283 * reports in units of uV, so convert by multiplying by 4880. */ 284 di->voltage_raw = (di->raw[DS2760_VOLTAGE_MSB] << 3) | 285 (di->raw[DS2760_VOLTAGE_LSB] >> 5); 286 di->voltage_uV = di->voltage_raw * 4880; 287 288 /* DS2760 reports current in signed units of 0.625mA, but the battery 289 * class reports in units of µA, so convert by multiplying by 625. */ 290 di->current_raw = 291 (((signed char)di->raw[DS2760_CURRENT_MSB]) << 5) | 292 (di->raw[DS2760_CURRENT_LSB] >> 3); 293 di->current_uA = di->current_raw * 625; 294 295 /* DS2760 reports accumulated current in signed units of 0.25mAh. */ 296 di->accum_current_raw = 297 (((signed char)di->raw[DS2760_CURRENT_ACCUM_MSB]) << 8) | 298 di->raw[DS2760_CURRENT_ACCUM_LSB]; 299 di->accum_current_uAh = di->accum_current_raw * 250; 300 301 /* DS2760 reports temperature in signed units of 0.125°C, but the 302 * battery class reports in units of 1/10 °C, so we convert by 303 * multiplying by .125 * 10 = 1.25. */ 304 di->temp_raw = (((signed char)di->raw[DS2760_TEMP_MSB]) << 3) | 305 (di->raw[DS2760_TEMP_LSB] >> 5); 306 di->temp_C = di->temp_raw + (di->temp_raw / 4); 307 308 /* At least some battery monitors (e.g. HP iPAQ) store the battery's 309 * maximum rated capacity. */ 310 if (di->raw[DS2760_RATED_CAPACITY] < ARRAY_SIZE(rated_capacities)) 311 di->rated_capacity = rated_capacities[ 312 (unsigned int)di->raw[DS2760_RATED_CAPACITY]]; 313 else 314 di->rated_capacity = di->raw[DS2760_RATED_CAPACITY] * 10; 315 316 di->rated_capacity *= 1000; /* convert to µAh */ 317 318 /* Calculate the full level at the present temperature. */ 319 di->full_active_uAh = di->raw[DS2760_ACTIVE_FULL] << 8 | 320 di->raw[DS2760_ACTIVE_FULL + 1]; 321 322 /* If the full_active_uAh value is not given, fall back to the rated 323 * capacity. This is likely to happen when chips are not part of the 324 * battery pack and is therefore not bootstrapped. */ 325 if (di->full_active_uAh == 0) 326 di->full_active_uAh = di->rated_capacity / 1000L; 327 328 scale[0] = di->full_active_uAh; 329 for (i = 1; i < 5; i++) 330 scale[i] = scale[i - 1] + di->raw[DS2760_ACTIVE_FULL + 1 + i]; 331 332 di->full_active_uAh = battery_interpolate(scale, di->temp_C / 10); 333 di->full_active_uAh *= 1000; /* convert to µAh */ 334 335 /* Calculate the empty level at the present temperature. */ 336 scale[4] = di->raw[DS2760_ACTIVE_EMPTY + 4]; 337 for (i = 3; i >= 0; i--) 338 scale[i] = scale[i + 1] + di->raw[DS2760_ACTIVE_EMPTY + i]; 339 340 di->empty_uAh = battery_interpolate(scale, di->temp_C / 10); 341 di->empty_uAh *= 1000; /* convert to µAh */ 342 343 if (di->full_active_uAh == di->empty_uAh) 344 di->rem_capacity = 0; 345 else 346 /* From Maxim Application Note 131: remaining capacity = 347 * ((ICA - Empty Value) / (Full Value - Empty Value)) x 100% */ 348 di->rem_capacity = ((di->accum_current_uAh - di->empty_uAh) * 100L) / 349 (di->full_active_uAh - di->empty_uAh); 350 351 if (di->rem_capacity < 0) 352 di->rem_capacity = 0; 353 if (di->rem_capacity > 100) 354 di->rem_capacity = 100; 355 356 if (di->current_uA < -100L) 357 di->life_sec = -((di->accum_current_uAh - di->empty_uAh) * 36L) 358 / (di->current_uA / 100L); 359 else 360 di->life_sec = 0; 361 362 return 0; 363 } 364 365 static void ds2760_battery_set_current_accum(struct ds2760_device_info *di, 366 unsigned int acr_val) 367 { 368 unsigned char acr[2]; 369 370 /* acr is in units of 0.25 mAh */ 371 acr_val *= 4L; 372 acr_val /= 1000; 373 374 acr[0] = acr_val >> 8; 375 acr[1] = acr_val & 0xff; 376 377 if (w1_ds2760_write(di->dev, acr, DS2760_CURRENT_ACCUM_MSB, 2) < 2) 378 dev_warn(di->dev, "ACR write failed\n"); 379 } 380 381 static void ds2760_battery_update_status(struct ds2760_device_info *di) 382 { 383 int old_charge_status = di->charge_status; 384 385 ds2760_battery_read_status(di); 386 387 if (di->charge_status == POWER_SUPPLY_STATUS_UNKNOWN) 388 di->full_counter = 0; 389 390 if (power_supply_am_i_supplied(di->bat)) { 391 if (di->current_uA > 10000) { 392 di->charge_status = POWER_SUPPLY_STATUS_CHARGING; 393 di->full_counter = 0; 394 } else if (di->current_uA < -5000) { 395 if (di->charge_status != POWER_SUPPLY_STATUS_NOT_CHARGING) 396 dev_notice(di->dev, "not enough power to " 397 "charge\n"); 398 di->charge_status = POWER_SUPPLY_STATUS_NOT_CHARGING; 399 di->full_counter = 0; 400 } else if (di->current_uA < 10000 && 401 di->charge_status != POWER_SUPPLY_STATUS_FULL) { 402 403 /* Don't consider the battery to be full unless 404 * we've seen the current < 10 mA at least two 405 * consecutive times. */ 406 407 di->full_counter++; 408 409 if (di->full_counter < 2) { 410 di->charge_status = POWER_SUPPLY_STATUS_CHARGING; 411 } else { 412 di->charge_status = POWER_SUPPLY_STATUS_FULL; 413 ds2760_battery_set_current_accum(di, 414 di->full_active_uAh); 415 } 416 } 417 } else { 418 di->charge_status = POWER_SUPPLY_STATUS_DISCHARGING; 419 di->full_counter = 0; 420 } 421 422 if (di->charge_status != old_charge_status) 423 power_supply_changed(di->bat); 424 } 425 426 static void ds2760_battery_write_status(struct ds2760_device_info *di, 427 char status) 428 { 429 if (status == di->raw[DS2760_STATUS_REG]) 430 return; 431 432 w1_ds2760_write(di->dev, &status, DS2760_STATUS_WRITE_REG, 1); 433 w1_ds2760_store_eeprom(di->dev, DS2760_EEPROM_BLOCK1); 434 w1_ds2760_recall_eeprom(di->dev, DS2760_EEPROM_BLOCK1); 435 } 436 437 static void ds2760_battery_write_rated_capacity(struct ds2760_device_info *di, 438 unsigned char rated_capacity) 439 { 440 if (rated_capacity == di->raw[DS2760_RATED_CAPACITY]) 441 return; 442 443 w1_ds2760_write(di->dev, &rated_capacity, DS2760_RATED_CAPACITY, 1); 444 w1_ds2760_store_eeprom(di->dev, DS2760_EEPROM_BLOCK1); 445 w1_ds2760_recall_eeprom(di->dev, DS2760_EEPROM_BLOCK1); 446 } 447 448 static void ds2760_battery_write_active_full(struct ds2760_device_info *di, 449 int active_full) 450 { 451 unsigned char tmp[2] = { 452 active_full >> 8, 453 active_full & 0xff 454 }; 455 456 if (tmp[0] == di->raw[DS2760_ACTIVE_FULL] && 457 tmp[1] == di->raw[DS2760_ACTIVE_FULL + 1]) 458 return; 459 460 w1_ds2760_write(di->dev, tmp, DS2760_ACTIVE_FULL, sizeof(tmp)); 461 w1_ds2760_store_eeprom(di->dev, DS2760_EEPROM_BLOCK0); 462 w1_ds2760_recall_eeprom(di->dev, DS2760_EEPROM_BLOCK0); 463 464 /* Write to the di->raw[] buffer directly - the DS2760_ACTIVE_FULL 465 * values won't be read back by ds2760_battery_read_status() */ 466 di->raw[DS2760_ACTIVE_FULL] = tmp[0]; 467 di->raw[DS2760_ACTIVE_FULL + 1] = tmp[1]; 468 } 469 470 static void ds2760_battery_work(struct work_struct *work) 471 { 472 struct ds2760_device_info *di = container_of(work, 473 struct ds2760_device_info, monitor_work.work); 474 const int interval = HZ * 60; 475 476 dev_dbg(di->dev, "%s\n", __func__); 477 478 ds2760_battery_update_status(di); 479 queue_delayed_work(di->monitor_wqueue, &di->monitor_work, interval); 480 } 481 482 static void ds2760_battery_external_power_changed(struct power_supply *psy) 483 { 484 struct ds2760_device_info *di = power_supply_get_drvdata(psy); 485 486 dev_dbg(di->dev, "%s\n", __func__); 487 488 mod_delayed_work(di->monitor_wqueue, &di->monitor_work, HZ/10); 489 } 490 491 492 static void ds2760_battery_set_charged_work(struct work_struct *work) 493 { 494 char bias; 495 struct ds2760_device_info *di = container_of(work, 496 struct ds2760_device_info, set_charged_work.work); 497 498 dev_dbg(di->dev, "%s\n", __func__); 499 500 ds2760_battery_read_status(di); 501 502 /* When we get notified by external circuitry that the battery is 503 * considered fully charged now, we know that there is no current 504 * flow any more. However, the ds2760's internal current meter is 505 * too inaccurate to rely on - spec say something ~15% failure. 506 * Hence, we use the current offset bias register to compensate 507 * that error. 508 */ 509 510 if (!power_supply_am_i_supplied(di->bat)) 511 return; 512 513 bias = (signed char) di->current_raw + 514 (signed char) di->raw[DS2760_CURRENT_OFFSET_BIAS]; 515 516 dev_dbg(di->dev, "%s: bias = %d\n", __func__, bias); 517 518 w1_ds2760_write(di->dev, &bias, DS2760_CURRENT_OFFSET_BIAS, 1); 519 w1_ds2760_store_eeprom(di->dev, DS2760_EEPROM_BLOCK1); 520 w1_ds2760_recall_eeprom(di->dev, DS2760_EEPROM_BLOCK1); 521 522 /* Write to the di->raw[] buffer directly - the CURRENT_OFFSET_BIAS 523 * value won't be read back by ds2760_battery_read_status() */ 524 di->raw[DS2760_CURRENT_OFFSET_BIAS] = bias; 525 } 526 527 static void ds2760_battery_set_charged(struct power_supply *psy) 528 { 529 struct ds2760_device_info *di = power_supply_get_drvdata(psy); 530 531 /* postpone the actual work by 20 secs. This is for debouncing GPIO 532 * signals and to let the current value settle. See AN4188. */ 533 mod_delayed_work(di->monitor_wqueue, &di->set_charged_work, HZ * 20); 534 } 535 536 static int ds2760_battery_get_property(struct power_supply *psy, 537 enum power_supply_property psp, 538 union power_supply_propval *val) 539 { 540 struct ds2760_device_info *di = power_supply_get_drvdata(psy); 541 542 switch (psp) { 543 case POWER_SUPPLY_PROP_STATUS: 544 val->intval = di->charge_status; 545 return 0; 546 default: 547 break; 548 } 549 550 ds2760_battery_read_status(di); 551 552 switch (psp) { 553 case POWER_SUPPLY_PROP_VOLTAGE_NOW: 554 val->intval = di->voltage_uV; 555 break; 556 case POWER_SUPPLY_PROP_CURRENT_NOW: 557 val->intval = di->current_uA; 558 break; 559 case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN: 560 val->intval = di->rated_capacity; 561 break; 562 case POWER_SUPPLY_PROP_CHARGE_FULL: 563 val->intval = di->full_active_uAh; 564 break; 565 case POWER_SUPPLY_PROP_CHARGE_EMPTY: 566 val->intval = di->empty_uAh; 567 break; 568 case POWER_SUPPLY_PROP_CHARGE_NOW: 569 val->intval = di->accum_current_uAh; 570 break; 571 case POWER_SUPPLY_PROP_TEMP: 572 val->intval = di->temp_C; 573 break; 574 case POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW: 575 val->intval = di->life_sec; 576 break; 577 case POWER_SUPPLY_PROP_CAPACITY: 578 val->intval = di->rem_capacity; 579 break; 580 default: 581 return -EINVAL; 582 } 583 584 return 0; 585 } 586 587 static int ds2760_battery_set_property(struct power_supply *psy, 588 enum power_supply_property psp, 589 const union power_supply_propval *val) 590 { 591 struct ds2760_device_info *di = power_supply_get_drvdata(psy); 592 593 switch (psp) { 594 case POWER_SUPPLY_PROP_CHARGE_FULL: 595 /* the interface counts in uAh, convert the value */ 596 ds2760_battery_write_active_full(di, val->intval / 1000L); 597 break; 598 599 case POWER_SUPPLY_PROP_CHARGE_NOW: 600 /* ds2760_battery_set_current_accum() does the conversion */ 601 ds2760_battery_set_current_accum(di, val->intval); 602 break; 603 604 default: 605 return -EPERM; 606 } 607 608 return 0; 609 } 610 611 static int ds2760_battery_property_is_writeable(struct power_supply *psy, 612 enum power_supply_property psp) 613 { 614 switch (psp) { 615 case POWER_SUPPLY_PROP_CHARGE_FULL: 616 case POWER_SUPPLY_PROP_CHARGE_NOW: 617 return 1; 618 619 default: 620 break; 621 } 622 623 return 0; 624 } 625 626 static enum power_supply_property ds2760_battery_props[] = { 627 POWER_SUPPLY_PROP_STATUS, 628 POWER_SUPPLY_PROP_VOLTAGE_NOW, 629 POWER_SUPPLY_PROP_CURRENT_NOW, 630 POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN, 631 POWER_SUPPLY_PROP_CHARGE_FULL, 632 POWER_SUPPLY_PROP_CHARGE_EMPTY, 633 POWER_SUPPLY_PROP_CHARGE_NOW, 634 POWER_SUPPLY_PROP_TEMP, 635 POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW, 636 POWER_SUPPLY_PROP_CAPACITY, 637 }; 638 639 static int ds2760_pm_notifier(struct notifier_block *notifier, 640 unsigned long pm_event, 641 void *unused) 642 { 643 struct ds2760_device_info *di = 644 container_of(notifier, struct ds2760_device_info, pm_notifier); 645 646 switch (pm_event) { 647 case PM_HIBERNATION_PREPARE: 648 case PM_SUSPEND_PREPARE: 649 di->charge_status = POWER_SUPPLY_STATUS_UNKNOWN; 650 break; 651 652 case PM_POST_RESTORE: 653 case PM_POST_HIBERNATION: 654 case PM_POST_SUSPEND: 655 di->charge_status = POWER_SUPPLY_STATUS_UNKNOWN; 656 power_supply_changed(di->bat); 657 mod_delayed_work(di->monitor_wqueue, &di->monitor_work, HZ); 658 659 break; 660 661 case PM_RESTORE_PREPARE: 662 default: 663 break; 664 } 665 666 return NOTIFY_DONE; 667 } 668 669 static int w1_ds2760_add_slave(struct w1_slave *sl) 670 { 671 struct power_supply_config psy_cfg = {}; 672 struct ds2760_device_info *di; 673 struct device *dev = &sl->dev; 674 int retval = 0; 675 char name[32]; 676 char status; 677 678 di = devm_kzalloc(dev, sizeof(*di), GFP_KERNEL); 679 if (!di) { 680 retval = -ENOMEM; 681 goto di_alloc_failed; 682 } 683 684 snprintf(name, sizeof(name), "ds2760-battery.%d", dev->id); 685 686 di->dev = dev; 687 di->bat_desc.name = name; 688 di->bat_desc.type = POWER_SUPPLY_TYPE_BATTERY; 689 di->bat_desc.properties = ds2760_battery_props; 690 di->bat_desc.num_properties = ARRAY_SIZE(ds2760_battery_props); 691 di->bat_desc.get_property = ds2760_battery_get_property; 692 di->bat_desc.set_property = ds2760_battery_set_property; 693 di->bat_desc.property_is_writeable = 694 ds2760_battery_property_is_writeable; 695 di->bat_desc.set_charged = ds2760_battery_set_charged; 696 di->bat_desc.external_power_changed = 697 ds2760_battery_external_power_changed; 698 699 psy_cfg.drv_data = di; 700 701 if (dev->of_node) { 702 u32 tmp; 703 704 psy_cfg.of_node = dev->of_node; 705 706 if (!of_property_read_bool(dev->of_node, "maxim,pmod-enabled")) 707 pmod_enabled = true; 708 709 if (!of_property_read_u32(dev->of_node, 710 "maxim,cache-time-ms", &tmp)) 711 cache_time = tmp; 712 713 if (!of_property_read_u32(dev->of_node, 714 "rated-capacity-microamp-hours", 715 &tmp)) 716 rated_capacity = tmp / 10; /* property is in mAh */ 717 } 718 719 di->charge_status = POWER_SUPPLY_STATUS_UNKNOWN; 720 721 sl->family_data = di; 722 723 /* enable sleep mode feature */ 724 ds2760_battery_read_status(di); 725 status = di->raw[DS2760_STATUS_REG]; 726 if (pmod_enabled) 727 status |= DS2760_STATUS_PMOD; 728 else 729 status &= ~DS2760_STATUS_PMOD; 730 731 ds2760_battery_write_status(di, status); 732 733 /* set rated capacity from module param or device tree */ 734 if (rated_capacity) 735 ds2760_battery_write_rated_capacity(di, rated_capacity); 736 737 /* set current accumulator if given as parameter. 738 * this should only be done for bootstrapping the value */ 739 if (current_accum) 740 ds2760_battery_set_current_accum(di, current_accum); 741 742 di->bat = power_supply_register(dev, &di->bat_desc, &psy_cfg); 743 if (IS_ERR(di->bat)) { 744 dev_err(di->dev, "failed to register battery\n"); 745 retval = PTR_ERR(di->bat); 746 goto batt_failed; 747 } 748 749 INIT_DELAYED_WORK(&di->monitor_work, ds2760_battery_work); 750 INIT_DELAYED_WORK(&di->set_charged_work, 751 ds2760_battery_set_charged_work); 752 di->monitor_wqueue = alloc_ordered_workqueue(name, WQ_MEM_RECLAIM); 753 if (!di->monitor_wqueue) { 754 retval = -ESRCH; 755 goto workqueue_failed; 756 } 757 queue_delayed_work(di->monitor_wqueue, &di->monitor_work, HZ * 1); 758 759 di->pm_notifier.notifier_call = ds2760_pm_notifier; 760 register_pm_notifier(&di->pm_notifier); 761 762 goto success; 763 764 workqueue_failed: 765 power_supply_unregister(di->bat); 766 batt_failed: 767 di_alloc_failed: 768 success: 769 return retval; 770 } 771 772 static void w1_ds2760_remove_slave(struct w1_slave *sl) 773 { 774 struct ds2760_device_info *di = sl->family_data; 775 776 unregister_pm_notifier(&di->pm_notifier); 777 cancel_delayed_work_sync(&di->monitor_work); 778 cancel_delayed_work_sync(&di->set_charged_work); 779 destroy_workqueue(di->monitor_wqueue); 780 power_supply_unregister(di->bat); 781 } 782 783 #ifdef CONFIG_OF 784 static const struct of_device_id w1_ds2760_of_ids[] = { 785 { .compatible = "maxim,ds2760" }, 786 {} 787 }; 788 #endif 789 790 static const struct w1_family_ops w1_ds2760_fops = { 791 .add_slave = w1_ds2760_add_slave, 792 .remove_slave = w1_ds2760_remove_slave, 793 .groups = w1_ds2760_groups, 794 }; 795 796 static struct w1_family w1_ds2760_family = { 797 .fid = W1_FAMILY_DS2760, 798 .fops = &w1_ds2760_fops, 799 .of_match_table = of_match_ptr(w1_ds2760_of_ids), 800 }; 801 module_w1_family(w1_ds2760_family); 802 803 MODULE_AUTHOR("Szabolcs Gyurko <szabolcs.gyurko@tlt.hu>, " 804 "Matt Reimer <mreimer@vpop.net>, " 805 "Anton Vorontsov <cbou@mail.ru>"); 806 MODULE_DESCRIPTION("1-wire Driver Dallas 2760 battery monitor chip"); 807 MODULE_LICENSE("GPL"); 808 MODULE_ALIAS("w1-family-" __stringify(W1_FAMILY_DS2760)); 809