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