1 /* 2 * 1-wire client/driver for the Maxim/Dallas DS2780 Stand-Alone Fuel Gauge IC 3 * 4 * Copyright (C) 2010 Indesign, LLC 5 * 6 * Author: Clifton Barnes <cabarnes@indesign-llc.com> 7 * 8 * Based on ds2760_battery and ds2782_battery drivers 9 * 10 * This program is free software; you can redistribute it and/or modify 11 * it under the terms of the GNU General Public License version 2 as 12 * published by the Free Software Foundation. 13 * 14 */ 15 16 #include <linux/module.h> 17 #include <linux/slab.h> 18 #include <linux/param.h> 19 #include <linux/pm.h> 20 #include <linux/platform_device.h> 21 #include <linux/power_supply.h> 22 #include <linux/idr.h> 23 24 #include <linux/w1.h> 25 #include "../../w1/slaves/w1_ds2780.h" 26 27 /* Current unit measurement in uA for a 1 milli-ohm sense resistor */ 28 #define DS2780_CURRENT_UNITS 1563 29 /* Charge unit measurement in uAh for a 1 milli-ohm sense resistor */ 30 #define DS2780_CHARGE_UNITS 6250 31 /* Number of bytes in user EEPROM space */ 32 #define DS2780_USER_EEPROM_SIZE (DS2780_EEPROM_BLOCK0_END - \ 33 DS2780_EEPROM_BLOCK0_START + 1) 34 /* Number of bytes in parameter EEPROM space */ 35 #define DS2780_PARAM_EEPROM_SIZE (DS2780_EEPROM_BLOCK1_END - \ 36 DS2780_EEPROM_BLOCK1_START + 1) 37 38 struct ds2780_device_info { 39 struct device *dev; 40 struct power_supply *bat; 41 struct power_supply_desc bat_desc; 42 struct device *w1_dev; 43 }; 44 45 enum current_types { 46 CURRENT_NOW, 47 CURRENT_AVG, 48 }; 49 50 static const char model[] = "DS2780"; 51 static const char manufacturer[] = "Maxim/Dallas"; 52 53 static inline struct ds2780_device_info * 54 to_ds2780_device_info(struct power_supply *psy) 55 { 56 return power_supply_get_drvdata(psy); 57 } 58 59 static inline struct power_supply *to_power_supply(struct device *dev) 60 { 61 return dev_get_drvdata(dev); 62 } 63 64 static inline int ds2780_battery_io(struct ds2780_device_info *dev_info, 65 char *buf, int addr, size_t count, int io) 66 { 67 return w1_ds2780_io(dev_info->w1_dev, buf, addr, count, io); 68 } 69 70 static inline int ds2780_read8(struct ds2780_device_info *dev_info, u8 *val, 71 int addr) 72 { 73 return ds2780_battery_io(dev_info, val, addr, sizeof(u8), 0); 74 } 75 76 static int ds2780_read16(struct ds2780_device_info *dev_info, s16 *val, 77 int addr) 78 { 79 int ret; 80 u8 raw[2]; 81 82 ret = ds2780_battery_io(dev_info, raw, addr, sizeof(raw), 0); 83 if (ret < 0) 84 return ret; 85 86 *val = (raw[0] << 8) | raw[1]; 87 88 return 0; 89 } 90 91 static inline int ds2780_read_block(struct ds2780_device_info *dev_info, 92 u8 *val, int addr, size_t count) 93 { 94 return ds2780_battery_io(dev_info, val, addr, count, 0); 95 } 96 97 static inline int ds2780_write(struct ds2780_device_info *dev_info, u8 *val, 98 int addr, size_t count) 99 { 100 return ds2780_battery_io(dev_info, val, addr, count, 1); 101 } 102 103 static inline int ds2780_store_eeprom(struct device *dev, int addr) 104 { 105 return w1_ds2780_eeprom_cmd(dev, addr, W1_DS2780_COPY_DATA); 106 } 107 108 static inline int ds2780_recall_eeprom(struct device *dev, int addr) 109 { 110 return w1_ds2780_eeprom_cmd(dev, addr, W1_DS2780_RECALL_DATA); 111 } 112 113 static int ds2780_save_eeprom(struct ds2780_device_info *dev_info, int reg) 114 { 115 int ret; 116 117 ret = ds2780_store_eeprom(dev_info->w1_dev, reg); 118 if (ret < 0) 119 return ret; 120 121 ret = ds2780_recall_eeprom(dev_info->w1_dev, reg); 122 if (ret < 0) 123 return ret; 124 125 return 0; 126 } 127 128 /* Set sense resistor value in mhos */ 129 static int ds2780_set_sense_register(struct ds2780_device_info *dev_info, 130 u8 conductance) 131 { 132 int ret; 133 134 ret = ds2780_write(dev_info, &conductance, 135 DS2780_RSNSP_REG, sizeof(u8)); 136 if (ret < 0) 137 return ret; 138 139 return ds2780_save_eeprom(dev_info, DS2780_RSNSP_REG); 140 } 141 142 /* Get RSGAIN value from 0 to 1.999 in steps of 0.001 */ 143 static int ds2780_get_rsgain_register(struct ds2780_device_info *dev_info, 144 u16 *rsgain) 145 { 146 return ds2780_read16(dev_info, rsgain, DS2780_RSGAIN_MSB_REG); 147 } 148 149 /* Set RSGAIN value from 0 to 1.999 in steps of 0.001 */ 150 static int ds2780_set_rsgain_register(struct ds2780_device_info *dev_info, 151 u16 rsgain) 152 { 153 int ret; 154 u8 raw[] = {rsgain >> 8, rsgain & 0xFF}; 155 156 ret = ds2780_write(dev_info, raw, 157 DS2780_RSGAIN_MSB_REG, sizeof(raw)); 158 if (ret < 0) 159 return ret; 160 161 return ds2780_save_eeprom(dev_info, DS2780_RSGAIN_MSB_REG); 162 } 163 164 static int ds2780_get_voltage(struct ds2780_device_info *dev_info, 165 int *voltage_uV) 166 { 167 int ret; 168 s16 voltage_raw; 169 170 /* 171 * The voltage value is located in 10 bits across the voltage MSB 172 * and LSB registers in two's compliment form 173 * Sign bit of the voltage value is in bit 7 of the voltage MSB register 174 * Bits 9 - 3 of the voltage value are in bits 6 - 0 of the 175 * voltage MSB register 176 * Bits 2 - 0 of the voltage value are in bits 7 - 5 of the 177 * voltage LSB register 178 */ 179 ret = ds2780_read16(dev_info, &voltage_raw, 180 DS2780_VOLT_MSB_REG); 181 if (ret < 0) 182 return ret; 183 184 /* 185 * DS2780 reports voltage in units of 4.88mV, but the battery class 186 * reports in units of uV, so convert by multiplying by 4880. 187 */ 188 *voltage_uV = (voltage_raw / 32) * 4880; 189 return 0; 190 } 191 192 static int ds2780_get_temperature(struct ds2780_device_info *dev_info, 193 int *temperature) 194 { 195 int ret; 196 s16 temperature_raw; 197 198 /* 199 * The temperature value is located in 10 bits across the temperature 200 * MSB and LSB registers in two's compliment form 201 * Sign bit of the temperature value is in bit 7 of the temperature 202 * MSB register 203 * Bits 9 - 3 of the temperature value are in bits 6 - 0 of the 204 * temperature MSB register 205 * Bits 2 - 0 of the temperature value are in bits 7 - 5 of the 206 * temperature LSB register 207 */ 208 ret = ds2780_read16(dev_info, &temperature_raw, 209 DS2780_TEMP_MSB_REG); 210 if (ret < 0) 211 return ret; 212 213 /* 214 * Temperature is measured in units of 0.125 degrees celcius, the 215 * power_supply class measures temperature in tenths of degrees 216 * celsius. The temperature value is stored as a 10 bit number, plus 217 * sign in the upper bits of a 16 bit register. 218 */ 219 *temperature = ((temperature_raw / 32) * 125) / 100; 220 return 0; 221 } 222 223 static int ds2780_get_current(struct ds2780_device_info *dev_info, 224 enum current_types type, int *current_uA) 225 { 226 int ret, sense_res; 227 s16 current_raw; 228 u8 sense_res_raw, reg_msb; 229 230 /* 231 * The units of measurement for current are dependent on the value of 232 * the sense resistor. 233 */ 234 ret = ds2780_read8(dev_info, &sense_res_raw, DS2780_RSNSP_REG); 235 if (ret < 0) 236 return ret; 237 238 if (sense_res_raw == 0) { 239 dev_err(dev_info->dev, "sense resistor value is 0\n"); 240 return -EINVAL; 241 } 242 sense_res = 1000 / sense_res_raw; 243 244 if (type == CURRENT_NOW) 245 reg_msb = DS2780_CURRENT_MSB_REG; 246 else if (type == CURRENT_AVG) 247 reg_msb = DS2780_IAVG_MSB_REG; 248 else 249 return -EINVAL; 250 251 /* 252 * The current value is located in 16 bits across the current MSB 253 * and LSB registers in two's compliment form 254 * Sign bit of the current value is in bit 7 of the current MSB register 255 * Bits 14 - 8 of the current value are in bits 6 - 0 of the current 256 * MSB register 257 * Bits 7 - 0 of the current value are in bits 7 - 0 of the current 258 * LSB register 259 */ 260 ret = ds2780_read16(dev_info, ¤t_raw, reg_msb); 261 if (ret < 0) 262 return ret; 263 264 *current_uA = current_raw * (DS2780_CURRENT_UNITS / sense_res); 265 return 0; 266 } 267 268 static int ds2780_get_accumulated_current(struct ds2780_device_info *dev_info, 269 int *accumulated_current) 270 { 271 int ret, sense_res; 272 s16 current_raw; 273 u8 sense_res_raw; 274 275 /* 276 * The units of measurement for accumulated current are dependent on 277 * the value of the sense resistor. 278 */ 279 ret = ds2780_read8(dev_info, &sense_res_raw, DS2780_RSNSP_REG); 280 if (ret < 0) 281 return ret; 282 283 if (sense_res_raw == 0) { 284 dev_err(dev_info->dev, "sense resistor value is 0\n"); 285 return -ENXIO; 286 } 287 sense_res = 1000 / sense_res_raw; 288 289 /* 290 * The ACR value is located in 16 bits across the ACR MSB and 291 * LSB registers 292 * Bits 15 - 8 of the ACR value are in bits 7 - 0 of the ACR 293 * MSB register 294 * Bits 7 - 0 of the ACR value are in bits 7 - 0 of the ACR 295 * LSB register 296 */ 297 ret = ds2780_read16(dev_info, ¤t_raw, DS2780_ACR_MSB_REG); 298 if (ret < 0) 299 return ret; 300 301 *accumulated_current = current_raw * (DS2780_CHARGE_UNITS / sense_res); 302 return 0; 303 } 304 305 static int ds2780_get_capacity(struct ds2780_device_info *dev_info, 306 int *capacity) 307 { 308 int ret; 309 u8 raw; 310 311 ret = ds2780_read8(dev_info, &raw, DS2780_RARC_REG); 312 if (ret < 0) 313 return ret; 314 315 *capacity = raw; 316 return raw; 317 } 318 319 static int ds2780_get_status(struct ds2780_device_info *dev_info, int *status) 320 { 321 int ret, current_uA, capacity; 322 323 ret = ds2780_get_current(dev_info, CURRENT_NOW, ¤t_uA); 324 if (ret < 0) 325 return ret; 326 327 ret = ds2780_get_capacity(dev_info, &capacity); 328 if (ret < 0) 329 return ret; 330 331 if (capacity == 100) 332 *status = POWER_SUPPLY_STATUS_FULL; 333 else if (current_uA == 0) 334 *status = POWER_SUPPLY_STATUS_NOT_CHARGING; 335 else if (current_uA < 0) 336 *status = POWER_SUPPLY_STATUS_DISCHARGING; 337 else 338 *status = POWER_SUPPLY_STATUS_CHARGING; 339 340 return 0; 341 } 342 343 static int ds2780_get_charge_now(struct ds2780_device_info *dev_info, 344 int *charge_now) 345 { 346 int ret; 347 u16 charge_raw; 348 349 /* 350 * The RAAC value is located in 16 bits across the RAAC MSB and 351 * LSB registers 352 * Bits 15 - 8 of the RAAC value are in bits 7 - 0 of the RAAC 353 * MSB register 354 * Bits 7 - 0 of the RAAC value are in bits 7 - 0 of the RAAC 355 * LSB register 356 */ 357 ret = ds2780_read16(dev_info, &charge_raw, DS2780_RAAC_MSB_REG); 358 if (ret < 0) 359 return ret; 360 361 *charge_now = charge_raw * 1600; 362 return 0; 363 } 364 365 static int ds2780_get_control_register(struct ds2780_device_info *dev_info, 366 u8 *control_reg) 367 { 368 return ds2780_read8(dev_info, control_reg, DS2780_CONTROL_REG); 369 } 370 371 static int ds2780_set_control_register(struct ds2780_device_info *dev_info, 372 u8 control_reg) 373 { 374 int ret; 375 376 ret = ds2780_write(dev_info, &control_reg, 377 DS2780_CONTROL_REG, sizeof(u8)); 378 if (ret < 0) 379 return ret; 380 381 return ds2780_save_eeprom(dev_info, DS2780_CONTROL_REG); 382 } 383 384 static int ds2780_battery_get_property(struct power_supply *psy, 385 enum power_supply_property psp, 386 union power_supply_propval *val) 387 { 388 int ret = 0; 389 struct ds2780_device_info *dev_info = to_ds2780_device_info(psy); 390 391 switch (psp) { 392 case POWER_SUPPLY_PROP_VOLTAGE_NOW: 393 ret = ds2780_get_voltage(dev_info, &val->intval); 394 break; 395 396 case POWER_SUPPLY_PROP_TEMP: 397 ret = ds2780_get_temperature(dev_info, &val->intval); 398 break; 399 400 case POWER_SUPPLY_PROP_MODEL_NAME: 401 val->strval = model; 402 break; 403 404 case POWER_SUPPLY_PROP_MANUFACTURER: 405 val->strval = manufacturer; 406 break; 407 408 case POWER_SUPPLY_PROP_CURRENT_NOW: 409 ret = ds2780_get_current(dev_info, CURRENT_NOW, &val->intval); 410 break; 411 412 case POWER_SUPPLY_PROP_CURRENT_AVG: 413 ret = ds2780_get_current(dev_info, CURRENT_AVG, &val->intval); 414 break; 415 416 case POWER_SUPPLY_PROP_STATUS: 417 ret = ds2780_get_status(dev_info, &val->intval); 418 break; 419 420 case POWER_SUPPLY_PROP_CAPACITY: 421 ret = ds2780_get_capacity(dev_info, &val->intval); 422 break; 423 424 case POWER_SUPPLY_PROP_CHARGE_COUNTER: 425 ret = ds2780_get_accumulated_current(dev_info, &val->intval); 426 break; 427 428 case POWER_SUPPLY_PROP_CHARGE_NOW: 429 ret = ds2780_get_charge_now(dev_info, &val->intval); 430 break; 431 432 default: 433 ret = -EINVAL; 434 } 435 436 return ret; 437 } 438 439 static enum power_supply_property ds2780_battery_props[] = { 440 POWER_SUPPLY_PROP_STATUS, 441 POWER_SUPPLY_PROP_VOLTAGE_NOW, 442 POWER_SUPPLY_PROP_TEMP, 443 POWER_SUPPLY_PROP_MODEL_NAME, 444 POWER_SUPPLY_PROP_MANUFACTURER, 445 POWER_SUPPLY_PROP_CURRENT_NOW, 446 POWER_SUPPLY_PROP_CURRENT_AVG, 447 POWER_SUPPLY_PROP_CAPACITY, 448 POWER_SUPPLY_PROP_CHARGE_COUNTER, 449 POWER_SUPPLY_PROP_CHARGE_NOW, 450 }; 451 452 static ssize_t ds2780_get_pmod_enabled(struct device *dev, 453 struct device_attribute *attr, 454 char *buf) 455 { 456 int ret; 457 u8 control_reg; 458 struct power_supply *psy = to_power_supply(dev); 459 struct ds2780_device_info *dev_info = to_ds2780_device_info(psy); 460 461 /* Get power mode */ 462 ret = ds2780_get_control_register(dev_info, &control_reg); 463 if (ret < 0) 464 return ret; 465 466 return sprintf(buf, "%d\n", 467 !!(control_reg & DS2780_CONTROL_REG_PMOD)); 468 } 469 470 static ssize_t ds2780_set_pmod_enabled(struct device *dev, 471 struct device_attribute *attr, 472 const char *buf, 473 size_t count) 474 { 475 int ret; 476 u8 control_reg, new_setting; 477 struct power_supply *psy = to_power_supply(dev); 478 struct ds2780_device_info *dev_info = to_ds2780_device_info(psy); 479 480 /* Set power mode */ 481 ret = ds2780_get_control_register(dev_info, &control_reg); 482 if (ret < 0) 483 return ret; 484 485 ret = kstrtou8(buf, 0, &new_setting); 486 if (ret < 0) 487 return ret; 488 489 if ((new_setting != 0) && (new_setting != 1)) { 490 dev_err(dev_info->dev, "Invalid pmod setting (0 or 1)\n"); 491 return -EINVAL; 492 } 493 494 if (new_setting) 495 control_reg |= DS2780_CONTROL_REG_PMOD; 496 else 497 control_reg &= ~DS2780_CONTROL_REG_PMOD; 498 499 ret = ds2780_set_control_register(dev_info, control_reg); 500 if (ret < 0) 501 return ret; 502 503 return count; 504 } 505 506 static ssize_t ds2780_get_sense_resistor_value(struct device *dev, 507 struct device_attribute *attr, 508 char *buf) 509 { 510 int ret; 511 u8 sense_resistor; 512 struct power_supply *psy = to_power_supply(dev); 513 struct ds2780_device_info *dev_info = to_ds2780_device_info(psy); 514 515 ret = ds2780_read8(dev_info, &sense_resistor, DS2780_RSNSP_REG); 516 if (ret < 0) 517 return ret; 518 519 ret = sprintf(buf, "%d\n", sense_resistor); 520 return ret; 521 } 522 523 static ssize_t ds2780_set_sense_resistor_value(struct device *dev, 524 struct device_attribute *attr, 525 const char *buf, 526 size_t count) 527 { 528 int ret; 529 u8 new_setting; 530 struct power_supply *psy = to_power_supply(dev); 531 struct ds2780_device_info *dev_info = to_ds2780_device_info(psy); 532 533 ret = kstrtou8(buf, 0, &new_setting); 534 if (ret < 0) 535 return ret; 536 537 ret = ds2780_set_sense_register(dev_info, new_setting); 538 if (ret < 0) 539 return ret; 540 541 return count; 542 } 543 544 static ssize_t ds2780_get_rsgain_setting(struct device *dev, 545 struct device_attribute *attr, 546 char *buf) 547 { 548 int ret; 549 u16 rsgain; 550 struct power_supply *psy = to_power_supply(dev); 551 struct ds2780_device_info *dev_info = to_ds2780_device_info(psy); 552 553 ret = ds2780_get_rsgain_register(dev_info, &rsgain); 554 if (ret < 0) 555 return ret; 556 557 return sprintf(buf, "%d\n", rsgain); 558 } 559 560 static ssize_t ds2780_set_rsgain_setting(struct device *dev, 561 struct device_attribute *attr, 562 const char *buf, 563 size_t count) 564 { 565 int ret; 566 u16 new_setting; 567 struct power_supply *psy = to_power_supply(dev); 568 struct ds2780_device_info *dev_info = to_ds2780_device_info(psy); 569 570 ret = kstrtou16(buf, 0, &new_setting); 571 if (ret < 0) 572 return ret; 573 574 /* Gain can only be from 0 to 1.999 in steps of .001 */ 575 if (new_setting > 1999) { 576 dev_err(dev_info->dev, "Invalid rsgain setting (0 - 1999)\n"); 577 return -EINVAL; 578 } 579 580 ret = ds2780_set_rsgain_register(dev_info, new_setting); 581 if (ret < 0) 582 return ret; 583 584 return count; 585 } 586 587 static ssize_t ds2780_get_pio_pin(struct device *dev, 588 struct device_attribute *attr, 589 char *buf) 590 { 591 int ret; 592 u8 sfr; 593 struct power_supply *psy = to_power_supply(dev); 594 struct ds2780_device_info *dev_info = to_ds2780_device_info(psy); 595 596 ret = ds2780_read8(dev_info, &sfr, DS2780_SFR_REG); 597 if (ret < 0) 598 return ret; 599 600 ret = sprintf(buf, "%d\n", sfr & DS2780_SFR_REG_PIOSC); 601 return ret; 602 } 603 604 static ssize_t ds2780_set_pio_pin(struct device *dev, 605 struct device_attribute *attr, 606 const char *buf, 607 size_t count) 608 { 609 int ret; 610 u8 new_setting; 611 struct power_supply *psy = to_power_supply(dev); 612 struct ds2780_device_info *dev_info = to_ds2780_device_info(psy); 613 614 ret = kstrtou8(buf, 0, &new_setting); 615 if (ret < 0) 616 return ret; 617 618 if ((new_setting != 0) && (new_setting != 1)) { 619 dev_err(dev_info->dev, "Invalid pio_pin setting (0 or 1)\n"); 620 return -EINVAL; 621 } 622 623 ret = ds2780_write(dev_info, &new_setting, 624 DS2780_SFR_REG, sizeof(u8)); 625 if (ret < 0) 626 return ret; 627 628 return count; 629 } 630 631 static ssize_t ds2780_read_param_eeprom_bin(struct file *filp, 632 struct kobject *kobj, 633 struct bin_attribute *bin_attr, 634 char *buf, loff_t off, size_t count) 635 { 636 struct device *dev = container_of(kobj, struct device, kobj); 637 struct power_supply *psy = to_power_supply(dev); 638 struct ds2780_device_info *dev_info = to_ds2780_device_info(psy); 639 640 return ds2780_read_block(dev_info, buf, 641 DS2780_EEPROM_BLOCK1_START + off, count); 642 } 643 644 static ssize_t ds2780_write_param_eeprom_bin(struct file *filp, 645 struct kobject *kobj, 646 struct bin_attribute *bin_attr, 647 char *buf, loff_t off, size_t count) 648 { 649 struct device *dev = container_of(kobj, struct device, kobj); 650 struct power_supply *psy = to_power_supply(dev); 651 struct ds2780_device_info *dev_info = to_ds2780_device_info(psy); 652 int ret; 653 654 ret = ds2780_write(dev_info, buf, 655 DS2780_EEPROM_BLOCK1_START + off, count); 656 if (ret < 0) 657 return ret; 658 659 ret = ds2780_save_eeprom(dev_info, DS2780_EEPROM_BLOCK1_START); 660 if (ret < 0) 661 return ret; 662 663 return count; 664 } 665 666 static const struct bin_attribute ds2780_param_eeprom_bin_attr = { 667 .attr = { 668 .name = "param_eeprom", 669 .mode = S_IRUGO | S_IWUSR, 670 }, 671 .size = DS2780_PARAM_EEPROM_SIZE, 672 .read = ds2780_read_param_eeprom_bin, 673 .write = ds2780_write_param_eeprom_bin, 674 }; 675 676 static ssize_t ds2780_read_user_eeprom_bin(struct file *filp, 677 struct kobject *kobj, 678 struct bin_attribute *bin_attr, 679 char *buf, loff_t off, size_t count) 680 { 681 struct device *dev = container_of(kobj, struct device, kobj); 682 struct power_supply *psy = to_power_supply(dev); 683 struct ds2780_device_info *dev_info = to_ds2780_device_info(psy); 684 685 return ds2780_read_block(dev_info, buf, 686 DS2780_EEPROM_BLOCK0_START + off, count); 687 } 688 689 static ssize_t ds2780_write_user_eeprom_bin(struct file *filp, 690 struct kobject *kobj, 691 struct bin_attribute *bin_attr, 692 char *buf, loff_t off, size_t count) 693 { 694 struct device *dev = container_of(kobj, struct device, kobj); 695 struct power_supply *psy = to_power_supply(dev); 696 struct ds2780_device_info *dev_info = to_ds2780_device_info(psy); 697 int ret; 698 699 ret = ds2780_write(dev_info, buf, 700 DS2780_EEPROM_BLOCK0_START + off, count); 701 if (ret < 0) 702 return ret; 703 704 ret = ds2780_save_eeprom(dev_info, DS2780_EEPROM_BLOCK0_START); 705 if (ret < 0) 706 return ret; 707 708 return count; 709 } 710 711 static const struct bin_attribute ds2780_user_eeprom_bin_attr = { 712 .attr = { 713 .name = "user_eeprom", 714 .mode = S_IRUGO | S_IWUSR, 715 }, 716 .size = DS2780_USER_EEPROM_SIZE, 717 .read = ds2780_read_user_eeprom_bin, 718 .write = ds2780_write_user_eeprom_bin, 719 }; 720 721 static DEVICE_ATTR(pmod_enabled, S_IRUGO | S_IWUSR, ds2780_get_pmod_enabled, 722 ds2780_set_pmod_enabled); 723 static DEVICE_ATTR(sense_resistor_value, S_IRUGO | S_IWUSR, 724 ds2780_get_sense_resistor_value, ds2780_set_sense_resistor_value); 725 static DEVICE_ATTR(rsgain_setting, S_IRUGO | S_IWUSR, ds2780_get_rsgain_setting, 726 ds2780_set_rsgain_setting); 727 static DEVICE_ATTR(pio_pin, S_IRUGO | S_IWUSR, ds2780_get_pio_pin, 728 ds2780_set_pio_pin); 729 730 731 static struct attribute *ds2780_attributes[] = { 732 &dev_attr_pmod_enabled.attr, 733 &dev_attr_sense_resistor_value.attr, 734 &dev_attr_rsgain_setting.attr, 735 &dev_attr_pio_pin.attr, 736 NULL 737 }; 738 739 static const struct attribute_group ds2780_attr_group = { 740 .attrs = ds2780_attributes, 741 }; 742 743 static int ds2780_battery_probe(struct platform_device *pdev) 744 { 745 struct power_supply_config psy_cfg = {}; 746 int ret = 0; 747 struct ds2780_device_info *dev_info; 748 749 dev_info = devm_kzalloc(&pdev->dev, sizeof(*dev_info), GFP_KERNEL); 750 if (!dev_info) { 751 ret = -ENOMEM; 752 goto fail; 753 } 754 755 platform_set_drvdata(pdev, dev_info); 756 757 dev_info->dev = &pdev->dev; 758 dev_info->w1_dev = pdev->dev.parent; 759 dev_info->bat_desc.name = dev_name(&pdev->dev); 760 dev_info->bat_desc.type = POWER_SUPPLY_TYPE_BATTERY; 761 dev_info->bat_desc.properties = ds2780_battery_props; 762 dev_info->bat_desc.num_properties = ARRAY_SIZE(ds2780_battery_props); 763 dev_info->bat_desc.get_property = ds2780_battery_get_property; 764 765 psy_cfg.drv_data = dev_info; 766 767 dev_info->bat = power_supply_register(&pdev->dev, &dev_info->bat_desc, 768 &psy_cfg); 769 if (IS_ERR(dev_info->bat)) { 770 dev_err(dev_info->dev, "failed to register battery\n"); 771 ret = PTR_ERR(dev_info->bat); 772 goto fail; 773 } 774 775 ret = sysfs_create_group(&dev_info->bat->dev.kobj, &ds2780_attr_group); 776 if (ret) { 777 dev_err(dev_info->dev, "failed to create sysfs group\n"); 778 goto fail_unregister; 779 } 780 781 ret = sysfs_create_bin_file(&dev_info->bat->dev.kobj, 782 &ds2780_param_eeprom_bin_attr); 783 if (ret) { 784 dev_err(dev_info->dev, 785 "failed to create param eeprom bin file"); 786 goto fail_remove_group; 787 } 788 789 ret = sysfs_create_bin_file(&dev_info->bat->dev.kobj, 790 &ds2780_user_eeprom_bin_attr); 791 if (ret) { 792 dev_err(dev_info->dev, 793 "failed to create user eeprom bin file"); 794 goto fail_remove_bin_file; 795 } 796 797 return 0; 798 799 fail_remove_bin_file: 800 sysfs_remove_bin_file(&dev_info->bat->dev.kobj, 801 &ds2780_param_eeprom_bin_attr); 802 fail_remove_group: 803 sysfs_remove_group(&dev_info->bat->dev.kobj, &ds2780_attr_group); 804 fail_unregister: 805 power_supply_unregister(dev_info->bat); 806 fail: 807 return ret; 808 } 809 810 static int ds2780_battery_remove(struct platform_device *pdev) 811 { 812 struct ds2780_device_info *dev_info = platform_get_drvdata(pdev); 813 814 /* 815 * Remove attributes before unregistering power supply 816 * because 'bat' will be freed on power_supply_unregister() call. 817 */ 818 sysfs_remove_group(&dev_info->bat->dev.kobj, &ds2780_attr_group); 819 820 power_supply_unregister(dev_info->bat); 821 822 return 0; 823 } 824 825 static struct platform_driver ds2780_battery_driver = { 826 .driver = { 827 .name = "ds2780-battery", 828 }, 829 .probe = ds2780_battery_probe, 830 .remove = ds2780_battery_remove, 831 }; 832 833 module_platform_driver(ds2780_battery_driver); 834 835 MODULE_LICENSE("GPL"); 836 MODULE_AUTHOR("Clifton Barnes <cabarnes@indesign-llc.com>"); 837 MODULE_DESCRIPTION("Maxim/Dallas DS2780 Stand-Alone Fuel Gauage IC driver"); 838 MODULE_ALIAS("platform:ds2780-battery"); 839