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