Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

Merge tag 'for-v3.5' of git://git.infradead.org/battery-2.6

Pull battery updates from Anton Vorontsov:
"A bunch of fixes for v3.5, nothing extraordinary."

* tag 'for-v3.5' of git://git.infradead.org/battery-2.6: (27 commits)
smb347-charger: Include missing <linux/err.h>
smb347-charger: Clean up battery attributes
max17042_battery: Add support for max17047/50 chip
sbs-battery.c: Capacity attr = remaining relative capacity
isp1704_charger: Use after free on probe error
ds2781_battery: Use DS2781_PARAM_EEPROM_SIZE and DS2781_USER_EEPROM_SIZE
power_supply: Fix a typo in BATTERY_DS2781 Kconfig entry
charger-manager: Provide cm_notify_event function for in-kernel use
charger-manager: Poll battery health in normal state
smb347-charger: Convert to regmap API
smb347-charger: Move IRQ enabling to the end of probe
smb347-charger: Rename few functions to match better what they are doing
smb347-charger: Convert to use module_i2c_driver()
smb347_charger: Cleanup power supply registration code in probe
ab8500: Clean up probe routines
ab8500_fg: Harden platform data check
ab8500_btemp: Harden platform data check
ab8500_charger: Harden platform data check
MAINTAINERS: Fix 'F' entry for the power supply class
max17042_battery: Handle irq request failure case
...

+942 -496
+39 -2
Documentation/power/charger-manager.txt
··· 44 44 Normally, the platform will need to resume and suspend some devices 45 45 that are used by Charger Manager. 46 46 47 + * Support for premature full-battery event handling 48 + If the battery voltage drops by "fullbatt_vchkdrop_uV" after 49 + "fullbatt_vchkdrop_ms" from the full-battery event, the framework 50 + restarts charging. This check is also performed while suspended by 51 + setting wakeup time accordingly and using suspend_again. 52 + 53 + * Support for uevent-notify 54 + With the charger-related events, the device sends 55 + notification to users with UEVENT. 56 + 47 57 2. Global Charger-Manager Data related with suspend_again 48 58 ======================================================== 49 59 In order to setup Charger Manager with suspend-again feature ··· 65 55 multiple instances of Charger Manager share the same charger_global_desc 66 56 and it will manage in-suspend monitoring for all instances of Charger Manager. 67 57 68 - The user needs to provide all the two entries properly in order to activate 58 + The user needs to provide all the three entries properly in order to activate 69 59 in-suspend monitoring: 70 60 71 61 struct charger_global_desc { ··· 84 74 same struct. If there is any other wakeup source triggered the 85 75 wakeup, it should return false. If the "rtc" is the only wakeup 86 76 reason, it should return true. 77 + 78 + bool assume_timer_stops_in_suspend; 79 + : if true, Charger Manager assumes that 80 + the timer (CM uses jiffies as timer) stops during suspend. Then, CM 81 + assumes that the suspend-duration is same as the alarm length. 87 82 }; 88 83 89 84 3. How to setup suspend_again ··· 126 111 CM_POLL_CHARGING_ONLY: poll this battery if and only if the 127 112 battery is being charged. 128 113 114 + unsigned int fullbatt_vchkdrop_ms; 115 + unsigned int fullbatt_vchkdrop_uV; 116 + : If both have non-zero values, Charger Manager will check the 117 + battery voltage drop fullbatt_vchkdrop_ms after the battery is fully 118 + charged. If the voltage drop is over fullbatt_vchkdrop_uV, Charger 119 + Manager will try to recharge the battery by disabling and enabling 120 + chargers. Recharge with voltage drop condition only (without delay 121 + condition) is needed to be implemented with hardware interrupts from 122 + fuel gauges or charger devices/chips. 123 + 129 124 unsigned int fullbatt_uV; 130 125 : If specified with a non-zero value, Charger Manager assumes 131 126 that the battery is full (capacity = 100) if the battery is not being ··· 147 122 this battery every polling_interval_ms or more frequently. 148 123 149 124 enum data_source battery_present; 125 + : CM_BATTERY_PRESENT: assume that the battery exists. 126 + CM_NO_BATTERY: assume that the battery does not exists. 150 127 CM_FUEL_GAUGE: get battery presence information from fuel gauge. 151 128 CM_CHARGER_STAT: get battery presence from chargers. 152 129 ··· 178 151 the value of measure_battery_temp. 179 152 }; 180 153 181 - 5. Other Considerations 154 + 5. Notify Charger-Manager of charger events: cm_notify_event() 155 + ========================================================= 156 + If there is an charger event is required to notify 157 + Charger Manager, a charger device driver that triggers the event can call 158 + cm_notify_event(psy, type, msg) to notify the corresponding Charger Manager. 159 + In the function, psy is the charger driver's power_supply pointer, which is 160 + associated with Charger-Manager. The parameter "type" 161 + is the same as irq's type (enum cm_event_types). The event message "msg" is 162 + optional and is effective only if the event type is "UNDESCRIBED" or "OTHERS". 163 + 164 + 6. Other Considerations 182 165 ======================= 183 166 184 167 At the charger/battery-related events such as battery-pulled-out,
+2
Documentation/power/power_supply_class.txt
··· 84 84 HEALTH - represents health of the battery, values corresponds to 85 85 POWER_SUPPLY_HEALTH_*, defined in battery.h. 86 86 87 + VOLTAGE_OCV - open circuit voltage of the battery. 88 + 87 89 VOLTAGE_MAX_DESIGN, VOLTAGE_MIN_DESIGN - design values for maximal and 88 90 minimal power supply voltages. Maximal/minimal means values of voltages 89 91 when battery considered "full"/"empty" at normal conditions. Yes, there is
+1 -1
MAINTAINERS
··· 5337 5337 T: git git://git.infradead.org/battery-2.6.git 5338 5338 S: Maintained 5339 5339 F: include/linux/power_supply.h 5340 - F: drivers/power/power_supply* 5340 + F: drivers/power/ 5341 5341 5342 5342 PNP SUPPORT 5343 5343 M: Adam Belay <abelay@mit.edu>
+6 -4
drivers/power/Kconfig
··· 1 1 menuconfig POWER_SUPPLY 2 - tristate "Power supply class support" 2 + bool "Power supply class support" 3 3 help 4 4 Say Y here to enable power supply class support. This allows 5 5 power supply (batteries, AC, USB) monitoring by userspace ··· 77 77 Say Y here to enable support for batteries with ds2780 chip. 78 78 79 79 config BATTERY_DS2781 80 - tristate "2781 battery driver" 80 + tristate "DS2781 battery driver" 81 81 depends on HAS_IOMEM 82 82 select W1 83 83 select W1_SLAVE_DS2781 ··· 181 181 to operate with a single lithium cell 182 182 183 183 config BATTERY_MAX17042 184 - tristate "Maxim MAX17042/8997/8966 Fuel Gauge" 184 + tristate "Maxim MAX17042/17047/17050/8997/8966 Fuel Gauge" 185 185 depends on I2C 186 186 help 187 187 MAX17042 is fuel-gauge systems for lithium-ion (Li+) batteries 188 188 in handheld and portable equipment. The MAX17042 is configured 189 189 to operate with a single lithium cell. MAX8997 and MAX8966 are 190 190 multi-function devices that include fuel gauages that are compatible 191 - with MAX17042. 191 + with MAX17042. This driver also supports max17047/50 chips which are 192 + improved version of max17042. 192 193 193 194 config BATTERY_Z2 194 195 tristate "Z2 battery driver" ··· 292 291 config CHARGER_SMB347 293 292 tristate "Summit Microelectronics SMB347 Battery Charger" 294 293 depends on I2C 294 + select REGMAP_I2C 295 295 help 296 296 Say Y to include support for Summit Microelectronics SMB347 297 297 Battery Charger.
+8 -4
drivers/power/ab8500_btemp.c
··· 964 964 { 965 965 int irq, i, ret = 0; 966 966 u8 val; 967 - struct abx500_bm_plat_data *plat_data; 967 + struct abx500_bm_plat_data *plat_data = pdev->dev.platform_data; 968 + struct ab8500_btemp *di; 968 969 969 - struct ab8500_btemp *di = 970 - kzalloc(sizeof(struct ab8500_btemp), GFP_KERNEL); 970 + if (!plat_data) { 971 + dev_err(&pdev->dev, "No platform data\n"); 972 + return -EINVAL; 973 + } 974 + 975 + di = kzalloc(sizeof(*di), GFP_KERNEL); 971 976 if (!di) 972 977 return -ENOMEM; 973 978 ··· 982 977 di->gpadc = ab8500_gpadc_get("ab8500-gpadc.0"); 983 978 984 979 /* get btemp specific platform data */ 985 - plat_data = pdev->dev.platform_data; 986 980 di->pdata = plat_data->btemp; 987 981 if (!di->pdata) { 988 982 dev_err(di->dev, "no btemp platform data supplied\n");
+8 -5
drivers/power/ab8500_charger.c
··· 2534 2534 static int __devinit ab8500_charger_probe(struct platform_device *pdev) 2535 2535 { 2536 2536 int irq, i, charger_status, ret = 0; 2537 - struct abx500_bm_plat_data *plat_data; 2537 + struct abx500_bm_plat_data *plat_data = pdev->dev.platform_data; 2538 + struct ab8500_charger *di; 2538 2539 2539 - struct ab8500_charger *di = 2540 - kzalloc(sizeof(struct ab8500_charger), GFP_KERNEL); 2540 + if (!plat_data) { 2541 + dev_err(&pdev->dev, "No platform data\n"); 2542 + return -EINVAL; 2543 + } 2544 + 2545 + di = kzalloc(sizeof(*di), GFP_KERNEL); 2541 2546 if (!di) 2542 2547 return -ENOMEM; 2543 2548 ··· 2555 2550 spin_lock_init(&di->usb_state.usb_lock); 2556 2551 2557 2552 /* get charger specific platform data */ 2558 - plat_data = pdev->dev.platform_data; 2559 2553 di->pdata = plat_data->charger; 2560 - 2561 2554 if (!di->pdata) { 2562 2555 dev_err(di->dev, "no charger platform data supplied\n"); 2563 2556 ret = -EINVAL;
+8 -4
drivers/power/ab8500_fg.c
··· 2446 2446 { 2447 2447 int i, irq; 2448 2448 int ret = 0; 2449 - struct abx500_bm_plat_data *plat_data; 2449 + struct abx500_bm_plat_data *plat_data = pdev->dev.platform_data; 2450 + struct ab8500_fg *di; 2450 2451 2451 - struct ab8500_fg *di = 2452 - kzalloc(sizeof(struct ab8500_fg), GFP_KERNEL); 2452 + if (!plat_data) { 2453 + dev_err(&pdev->dev, "No platform data\n"); 2454 + return -EINVAL; 2455 + } 2456 + 2457 + di = kzalloc(sizeof(*di), GFP_KERNEL); 2453 2458 if (!di) 2454 2459 return -ENOMEM; 2455 2460 ··· 2466 2461 di->gpadc = ab8500_gpadc_get("ab8500-gpadc.0"); 2467 2462 2468 2463 /* get fg specific platform data */ 2469 - plat_data = pdev->dev.platform_data; 2470 2464 di->pdata = plat_data->fg; 2471 2465 if (!di->pdata) { 2472 2466 dev_err(di->dev, "no fg platform data supplied\n");
+392
drivers/power/charger-manager.c
··· 23 23 #include <linux/power/charger-manager.h> 24 24 #include <linux/regulator/consumer.h> 25 25 26 + static const char * const default_event_names[] = { 27 + [CM_EVENT_UNKNOWN] = "Unknown", 28 + [CM_EVENT_BATT_FULL] = "Battery Full", 29 + [CM_EVENT_BATT_IN] = "Battery Inserted", 30 + [CM_EVENT_BATT_OUT] = "Battery Pulled Out", 31 + [CM_EVENT_EXT_PWR_IN_OUT] = "External Power Attach/Detach", 32 + [CM_EVENT_CHG_START_STOP] = "Charging Start/Stop", 33 + [CM_EVENT_OTHERS] = "Other battery events" 34 + }; 35 + 26 36 /* 27 37 * Regard CM_JIFFIES_SMALL jiffies is small enough to ignore for 28 38 * delayed works so that we can run delayed works with CM_JIFFIES_SMALL ··· 67 57 static bool cm_rtc_set; 68 58 static unsigned long cm_suspend_duration_ms; 69 59 60 + /* About normal (not suspended) monitoring */ 61 + static unsigned long polling_jiffy = ULONG_MAX; /* ULONG_MAX: no polling */ 62 + static unsigned long next_polling; /* Next appointed polling time */ 63 + static struct workqueue_struct *cm_wq; /* init at driver add */ 64 + static struct delayed_work cm_monitor_work; /* init at driver add */ 65 + 70 66 /* Global charger-manager description */ 71 67 static struct charger_global_desc *g_desc; /* init with setup_charger_manager */ 72 68 ··· 87 71 int i, ret; 88 72 89 73 switch (cm->desc->battery_present) { 74 + case CM_BATTERY_PRESENT: 75 + present = true; 76 + break; 77 + case CM_NO_BATTERY: 78 + break; 90 79 case CM_FUEL_GAUGE: 91 80 ret = cm->fuel_gauge->get_property(cm->fuel_gauge, 92 81 POWER_SUPPLY_PROP_PRESENT, &val); ··· 300 279 } 301 280 302 281 /** 282 + * try_charger_restart - Restart charging. 283 + * @cm: the Charger Manager representing the battery. 284 + * 285 + * Restart charging by turning off and on the charger. 286 + */ 287 + static int try_charger_restart(struct charger_manager *cm) 288 + { 289 + int err; 290 + 291 + if (cm->emergency_stop) 292 + return -EAGAIN; 293 + 294 + err = try_charger_enable(cm, false); 295 + if (err) 296 + return err; 297 + 298 + return try_charger_enable(cm, true); 299 + } 300 + 301 + /** 303 302 * uevent_notify - Let users know something has changed. 304 303 * @cm: the Charger Manager representing the battery. 305 304 * @event: the event string. ··· 372 331 kobject_uevent(&cm->dev->kobj, KOBJ_CHANGE); 373 332 374 333 dev_info(cm->dev, event); 334 + } 335 + 336 + /** 337 + * fullbatt_vchk - Check voltage drop some times after "FULL" event. 338 + * @work: the work_struct appointing the function 339 + * 340 + * If a user has designated "fullbatt_vchkdrop_ms/uV" values with 341 + * charger_desc, Charger Manager checks voltage drop after the battery 342 + * "FULL" event. It checks whether the voltage has dropped more than 343 + * fullbatt_vchkdrop_uV by calling this function after fullbatt_vchkrop_ms. 344 + */ 345 + static void fullbatt_vchk(struct work_struct *work) 346 + { 347 + struct delayed_work *dwork = to_delayed_work(work); 348 + struct charger_manager *cm = container_of(dwork, 349 + struct charger_manager, fullbatt_vchk_work); 350 + struct charger_desc *desc = cm->desc; 351 + int batt_uV, err, diff; 352 + 353 + /* remove the appointment for fullbatt_vchk */ 354 + cm->fullbatt_vchk_jiffies_at = 0; 355 + 356 + if (!desc->fullbatt_vchkdrop_uV || !desc->fullbatt_vchkdrop_ms) 357 + return; 358 + 359 + err = get_batt_uV(cm, &batt_uV); 360 + if (err) { 361 + dev_err(cm->dev, "%s: get_batt_uV error(%d).\n", __func__, err); 362 + return; 363 + } 364 + 365 + diff = cm->fullbatt_vchk_uV; 366 + diff -= batt_uV; 367 + 368 + dev_dbg(cm->dev, "VBATT dropped %duV after full-batt.\n", diff); 369 + 370 + if (diff > desc->fullbatt_vchkdrop_uV) { 371 + try_charger_restart(cm); 372 + uevent_notify(cm, "Recharge"); 373 + } 375 374 } 376 375 377 376 /** ··· 471 390 mutex_unlock(&cm_list_mtx); 472 391 473 392 return stop; 393 + } 394 + 395 + /** 396 + * _setup_polling - Setup the next instance of polling. 397 + * @work: work_struct of the function _setup_polling. 398 + */ 399 + static void _setup_polling(struct work_struct *work) 400 + { 401 + unsigned long min = ULONG_MAX; 402 + struct charger_manager *cm; 403 + bool keep_polling = false; 404 + unsigned long _next_polling; 405 + 406 + mutex_lock(&cm_list_mtx); 407 + 408 + list_for_each_entry(cm, &cm_list, entry) { 409 + if (is_polling_required(cm) && cm->desc->polling_interval_ms) { 410 + keep_polling = true; 411 + 412 + if (min > cm->desc->polling_interval_ms) 413 + min = cm->desc->polling_interval_ms; 414 + } 415 + } 416 + 417 + polling_jiffy = msecs_to_jiffies(min); 418 + if (polling_jiffy <= CM_JIFFIES_SMALL) 419 + polling_jiffy = CM_JIFFIES_SMALL + 1; 420 + 421 + if (!keep_polling) 422 + polling_jiffy = ULONG_MAX; 423 + if (polling_jiffy == ULONG_MAX) 424 + goto out; 425 + 426 + WARN(cm_wq == NULL, "charger-manager: workqueue not initialized" 427 + ". try it later. %s\n", __func__); 428 + 429 + _next_polling = jiffies + polling_jiffy; 430 + 431 + if (!delayed_work_pending(&cm_monitor_work) || 432 + (delayed_work_pending(&cm_monitor_work) && 433 + time_after(next_polling, _next_polling))) { 434 + cancel_delayed_work_sync(&cm_monitor_work); 435 + next_polling = jiffies + polling_jiffy; 436 + queue_delayed_work(cm_wq, &cm_monitor_work, polling_jiffy); 437 + } 438 + 439 + out: 440 + mutex_unlock(&cm_list_mtx); 441 + } 442 + static DECLARE_WORK(setup_polling, _setup_polling); 443 + 444 + /** 445 + * cm_monitor_poller - The Monitor / Poller. 446 + * @work: work_struct of the function cm_monitor_poller 447 + * 448 + * During non-suspended state, cm_monitor_poller is used to poll and monitor 449 + * the batteries. 450 + */ 451 + static void cm_monitor_poller(struct work_struct *work) 452 + { 453 + cm_monitor(); 454 + schedule_work(&setup_polling); 455 + } 456 + 457 + /** 458 + * fullbatt_handler - Event handler for CM_EVENT_BATT_FULL 459 + * @cm: the Charger Manager representing the battery. 460 + */ 461 + static void fullbatt_handler(struct charger_manager *cm) 462 + { 463 + struct charger_desc *desc = cm->desc; 464 + 465 + if (!desc->fullbatt_vchkdrop_uV || !desc->fullbatt_vchkdrop_ms) 466 + goto out; 467 + 468 + if (cm_suspended) 469 + device_set_wakeup_capable(cm->dev, true); 470 + 471 + if (delayed_work_pending(&cm->fullbatt_vchk_work)) 472 + cancel_delayed_work(&cm->fullbatt_vchk_work); 473 + queue_delayed_work(cm_wq, &cm->fullbatt_vchk_work, 474 + msecs_to_jiffies(desc->fullbatt_vchkdrop_ms)); 475 + cm->fullbatt_vchk_jiffies_at = jiffies + msecs_to_jiffies( 476 + desc->fullbatt_vchkdrop_ms); 477 + 478 + if (cm->fullbatt_vchk_jiffies_at == 0) 479 + cm->fullbatt_vchk_jiffies_at = 1; 480 + 481 + out: 482 + dev_info(cm->dev, "EVENT_HANDLE: Battery Fully Charged.\n"); 483 + uevent_notify(cm, default_event_names[CM_EVENT_BATT_FULL]); 484 + } 485 + 486 + /** 487 + * battout_handler - Event handler for CM_EVENT_BATT_OUT 488 + * @cm: the Charger Manager representing the battery. 489 + */ 490 + static void battout_handler(struct charger_manager *cm) 491 + { 492 + if (cm_suspended) 493 + device_set_wakeup_capable(cm->dev, true); 494 + 495 + if (!is_batt_present(cm)) { 496 + dev_emerg(cm->dev, "Battery Pulled Out!\n"); 497 + uevent_notify(cm, default_event_names[CM_EVENT_BATT_OUT]); 498 + } else { 499 + uevent_notify(cm, "Battery Reinserted?"); 500 + } 501 + } 502 + 503 + /** 504 + * misc_event_handler - Handler for other evnets 505 + * @cm: the Charger Manager representing the battery. 506 + * @type: the Charger Manager representing the battery. 507 + */ 508 + static void misc_event_handler(struct charger_manager *cm, 509 + enum cm_event_types type) 510 + { 511 + if (cm_suspended) 512 + device_set_wakeup_capable(cm->dev, true); 513 + 514 + if (!delayed_work_pending(&cm_monitor_work) && 515 + is_polling_required(cm) && cm->desc->polling_interval_ms) 516 + schedule_work(&setup_polling); 517 + uevent_notify(cm, default_event_names[type]); 474 518 } 475 519 476 520 static int charger_get_property(struct power_supply *psy, ··· 819 613 mutex_lock(&cm_list_mtx); 820 614 821 615 list_for_each_entry(cm, &cm_list, entry) { 616 + unsigned int fbchk_ms = 0; 617 + 618 + /* fullbatt_vchk is required. setup timer for that */ 619 + if (cm->fullbatt_vchk_jiffies_at) { 620 + fbchk_ms = jiffies_to_msecs(cm->fullbatt_vchk_jiffies_at 621 + - jiffies); 622 + if (time_is_before_eq_jiffies( 623 + cm->fullbatt_vchk_jiffies_at) || 624 + msecs_to_jiffies(fbchk_ms) < CM_JIFFIES_SMALL) { 625 + fullbatt_vchk(&cm->fullbatt_vchk_work.work); 626 + fbchk_ms = 0; 627 + } 628 + } 629 + CM_MIN_VALID(wakeup_ms, fbchk_ms); 630 + 822 631 /* Skip if polling is not required for this CM */ 823 632 if (!is_polling_required(cm) && !cm->emergency_stop) 824 633 continue; ··· 893 672 return false; 894 673 } 895 674 675 + static void _cm_fbchk_in_suspend(struct charger_manager *cm) 676 + { 677 + unsigned long jiffy_now = jiffies; 678 + 679 + if (!cm->fullbatt_vchk_jiffies_at) 680 + return; 681 + 682 + if (g_desc && g_desc->assume_timer_stops_in_suspend) 683 + jiffy_now += msecs_to_jiffies(cm_suspend_duration_ms); 684 + 685 + /* Execute now if it's going to be executed not too long after */ 686 + jiffy_now += CM_JIFFIES_SMALL; 687 + 688 + if (time_after_eq(jiffy_now, cm->fullbatt_vchk_jiffies_at)) 689 + fullbatt_vchk(&cm->fullbatt_vchk_work.work); 690 + } 691 + 896 692 /** 897 693 * cm_suspend_again - Determine whether suspend again or not 898 694 * ··· 931 693 ret = true; 932 694 mutex_lock(&cm_list_mtx); 933 695 list_for_each_entry(cm, &cm_list, entry) { 696 + _cm_fbchk_in_suspend(cm); 697 + 934 698 if (cm->status_save_ext_pwr_inserted != is_ext_pwr_online(cm) || 935 699 cm->status_save_batt != is_batt_present(cm)) { 936 700 ret = false; ··· 1035 795 } 1036 796 memcpy(cm->desc, desc, sizeof(struct charger_desc)); 1037 797 cm->last_temp_mC = INT_MIN; /* denotes "unmeasured, yet" */ 798 + 799 + /* 800 + * The following two do not need to be errors. 801 + * Users may intentionally ignore those two features. 802 + */ 803 + if (desc->fullbatt_uV == 0) { 804 + dev_info(&pdev->dev, "Ignoring full-battery voltage threshold" 805 + " as it is not supplied."); 806 + } 807 + if (!desc->fullbatt_vchkdrop_ms || !desc->fullbatt_vchkdrop_uV) { 808 + dev_info(&pdev->dev, "Disabling full-battery voltage drop " 809 + "checking mechanism as it is not supplied."); 810 + desc->fullbatt_vchkdrop_ms = 0; 811 + desc->fullbatt_vchkdrop_uV = 0; 812 + } 1038 813 1039 814 if (!desc->charger_regulators || desc->num_charger_regulators < 1) { 1040 815 ret = -EINVAL; ··· 1158 903 cm->charger_psy.num_properties++; 1159 904 } 1160 905 906 + INIT_DELAYED_WORK(&cm->fullbatt_vchk_work, fullbatt_vchk); 907 + 1161 908 ret = power_supply_register(NULL, &cm->charger_psy); 1162 909 if (ret) { 1163 910 dev_err(&pdev->dev, "Cannot register charger-manager with" ··· 1184 927 mutex_lock(&cm_list_mtx); 1185 928 list_add(&cm->entry, &cm_list); 1186 929 mutex_unlock(&cm_list_mtx); 930 + 931 + /* 932 + * Charger-manager is capable of waking up the systme from sleep 933 + * when event is happend through cm_notify_event() 934 + */ 935 + device_init_wakeup(&pdev->dev, true); 936 + device_set_wakeup_capable(&pdev->dev, false); 937 + 938 + schedule_work(&setup_polling); 1187 939 1188 940 return 0; 1189 941 ··· 1224 958 list_del(&cm->entry); 1225 959 mutex_unlock(&cm_list_mtx); 1226 960 961 + if (work_pending(&setup_polling)) 962 + cancel_work_sync(&setup_polling); 963 + if (delayed_work_pending(&cm_monitor_work)) 964 + cancel_delayed_work_sync(&cm_monitor_work); 965 + 1227 966 regulator_bulk_free(desc->num_charger_regulators, 1228 967 desc->charger_regulators); 1229 968 power_supply_unregister(&cm->charger_psy); 969 + 970 + try_charger_enable(cm, false); 971 + 1230 972 kfree(cm->charger_psy.properties); 1231 973 kfree(cm->charger_stat); 1232 974 kfree(cm->desc); ··· 1248 974 { }, 1249 975 }; 1250 976 MODULE_DEVICE_TABLE(platform, charger_manager_id); 977 + 978 + static int cm_suspend_noirq(struct device *dev) 979 + { 980 + int ret = 0; 981 + 982 + if (device_may_wakeup(dev)) { 983 + device_set_wakeup_capable(dev, false); 984 + ret = -EAGAIN; 985 + } 986 + 987 + return ret; 988 + } 1251 989 1252 990 static int cm_suspend_prepare(struct device *dev) 1253 991 { ··· 1286 1000 cm_suspended = true; 1287 1001 } 1288 1002 1003 + if (delayed_work_pending(&cm->fullbatt_vchk_work)) 1004 + cancel_delayed_work(&cm->fullbatt_vchk_work); 1289 1005 cm->status_save_ext_pwr_inserted = is_ext_pwr_online(cm); 1290 1006 cm->status_save_batt = is_batt_present(cm); 1291 1007 ··· 1315 1027 cm_rtc_set = false; 1316 1028 } 1317 1029 1030 + /* Re-enqueue delayed work (fullbatt_vchk_work) */ 1031 + if (cm->fullbatt_vchk_jiffies_at) { 1032 + unsigned long delay = 0; 1033 + unsigned long now = jiffies + CM_JIFFIES_SMALL; 1034 + 1035 + if (time_after_eq(now, cm->fullbatt_vchk_jiffies_at)) { 1036 + delay = (unsigned long)((long)now 1037 + - (long)(cm->fullbatt_vchk_jiffies_at)); 1038 + delay = jiffies_to_msecs(delay); 1039 + } else { 1040 + delay = 0; 1041 + } 1042 + 1043 + /* 1044 + * Account for cm_suspend_duration_ms if 1045 + * assume_timer_stops_in_suspend is active 1046 + */ 1047 + if (g_desc && g_desc->assume_timer_stops_in_suspend) { 1048 + if (delay > cm_suspend_duration_ms) 1049 + delay -= cm_suspend_duration_ms; 1050 + else 1051 + delay = 0; 1052 + } 1053 + 1054 + queue_delayed_work(cm_wq, &cm->fullbatt_vchk_work, 1055 + msecs_to_jiffies(delay)); 1056 + } 1057 + device_set_wakeup_capable(cm->dev, false); 1318 1058 uevent_notify(cm, NULL); 1319 1059 } 1320 1060 1321 1061 static const struct dev_pm_ops charger_manager_pm = { 1322 1062 .prepare = cm_suspend_prepare, 1063 + .suspend_noirq = cm_suspend_noirq, 1323 1064 .complete = cm_suspend_complete, 1324 1065 }; 1325 1066 ··· 1365 1048 1366 1049 static int __init charger_manager_init(void) 1367 1050 { 1051 + cm_wq = create_freezable_workqueue("charger_manager"); 1052 + INIT_DELAYED_WORK(&cm_monitor_work, cm_monitor_poller); 1053 + 1368 1054 return platform_driver_register(&charger_manager_driver); 1369 1055 } 1370 1056 late_initcall(charger_manager_init); 1371 1057 1372 1058 static void __exit charger_manager_cleanup(void) 1373 1059 { 1060 + destroy_workqueue(cm_wq); 1061 + cm_wq = NULL; 1062 + 1374 1063 platform_driver_unregister(&charger_manager_driver); 1375 1064 } 1376 1065 module_exit(charger_manager_cleanup); 1066 + 1067 + /** 1068 + * find_power_supply - find the associated power_supply of charger 1069 + * @cm: the Charger Manager representing the battery 1070 + * @psy: pointer to instance of charger's power_supply 1071 + */ 1072 + static bool find_power_supply(struct charger_manager *cm, 1073 + struct power_supply *psy) 1074 + { 1075 + int i; 1076 + bool found = false; 1077 + 1078 + for (i = 0; cm->charger_stat[i]; i++) { 1079 + if (psy == cm->charger_stat[i]) { 1080 + found = true; 1081 + break; 1082 + } 1083 + } 1084 + 1085 + return found; 1086 + } 1087 + 1088 + /** 1089 + * cm_notify_event - charger driver notify Charger Manager of charger event 1090 + * @psy: pointer to instance of charger's power_supply 1091 + * @type: type of charger event 1092 + * @msg: optional message passed to uevent_notify fuction 1093 + */ 1094 + void cm_notify_event(struct power_supply *psy, enum cm_event_types type, 1095 + char *msg) 1096 + { 1097 + struct charger_manager *cm; 1098 + bool found_power_supply = false; 1099 + 1100 + if (psy == NULL) 1101 + return; 1102 + 1103 + mutex_lock(&cm_list_mtx); 1104 + list_for_each_entry(cm, &cm_list, entry) { 1105 + found_power_supply = find_power_supply(cm, psy); 1106 + if (found_power_supply) 1107 + break; 1108 + } 1109 + mutex_unlock(&cm_list_mtx); 1110 + 1111 + if (!found_power_supply) 1112 + return; 1113 + 1114 + switch (type) { 1115 + case CM_EVENT_BATT_FULL: 1116 + fullbatt_handler(cm); 1117 + break; 1118 + case CM_EVENT_BATT_OUT: 1119 + battout_handler(cm); 1120 + break; 1121 + case CM_EVENT_BATT_IN: 1122 + case CM_EVENT_EXT_PWR_IN_OUT ... CM_EVENT_CHG_START_STOP: 1123 + misc_event_handler(cm, type); 1124 + break; 1125 + case CM_EVENT_UNKNOWN: 1126 + case CM_EVENT_OTHERS: 1127 + uevent_notify(cm, msg ? msg : default_event_names[type]); 1128 + break; 1129 + default: 1130 + dev_err(cm->dev, "%s type not specified.\n", __func__); 1131 + break; 1132 + } 1133 + } 1134 + EXPORT_SYMBOL_GPL(cm_notify_event); 1377 1135 1378 1136 MODULE_AUTHOR("MyungJoo Ham <myungjoo.ham@samsung.com>"); 1379 1137 MODULE_DESCRIPTION("Charger Manager");
+6 -14
drivers/power/ds2781_battery.c
··· 643 643 struct power_supply *psy = to_power_supply(dev); 644 644 struct ds2781_device_info *dev_info = to_ds2781_device_info(psy); 645 645 646 - count = min_t(loff_t, count, 647 - DS2781_EEPROM_BLOCK1_END - 648 - DS2781_EEPROM_BLOCK1_START + 1 - off); 646 + count = min_t(loff_t, count, DS2781_PARAM_EEPROM_SIZE - off); 649 647 650 648 return ds2781_read_block(dev_info, buf, 651 649 DS2781_EEPROM_BLOCK1_START + off, count); ··· 659 661 struct ds2781_device_info *dev_info = to_ds2781_device_info(psy); 660 662 int ret; 661 663 662 - count = min_t(loff_t, count, 663 - DS2781_EEPROM_BLOCK1_END - 664 - DS2781_EEPROM_BLOCK1_START + 1 - off); 664 + count = min_t(loff_t, count, DS2781_PARAM_EEPROM_SIZE - off); 665 665 666 666 ret = ds2781_write(dev_info, buf, 667 667 DS2781_EEPROM_BLOCK1_START + off, count); ··· 678 682 .name = "param_eeprom", 679 683 .mode = S_IRUGO | S_IWUSR, 680 684 }, 681 - .size = DS2781_EEPROM_BLOCK1_END - DS2781_EEPROM_BLOCK1_START + 1, 685 + .size = DS2781_PARAM_EEPROM_SIZE, 682 686 .read = ds2781_read_param_eeprom_bin, 683 687 .write = ds2781_write_param_eeprom_bin, 684 688 }; ··· 692 696 struct power_supply *psy = to_power_supply(dev); 693 697 struct ds2781_device_info *dev_info = to_ds2781_device_info(psy); 694 698 695 - count = min_t(loff_t, count, 696 - DS2781_EEPROM_BLOCK0_END - 697 - DS2781_EEPROM_BLOCK0_START + 1 - off); 699 + count = min_t(loff_t, count, DS2781_USER_EEPROM_SIZE - off); 698 700 699 701 return ds2781_read_block(dev_info, buf, 700 702 DS2781_EEPROM_BLOCK0_START + off, count); ··· 709 715 struct ds2781_device_info *dev_info = to_ds2781_device_info(psy); 710 716 int ret; 711 717 712 - count = min_t(loff_t, count, 713 - DS2781_EEPROM_BLOCK0_END - 714 - DS2781_EEPROM_BLOCK0_START + 1 - off); 718 + count = min_t(loff_t, count, DS2781_USER_EEPROM_SIZE - off); 715 719 716 720 ret = ds2781_write(dev_info, buf, 717 721 DS2781_EEPROM_BLOCK0_START + off, count); ··· 728 736 .name = "user_eeprom", 729 737 .mode = S_IRUGO | S_IWUSR, 730 738 }, 731 - .size = DS2781_EEPROM_BLOCK0_END - DS2781_EEPROM_BLOCK0_START + 1, 739 + .size = DS2781_USER_EEPROM_SIZE, 732 740 .read = ds2781_read_user_eeprom_bin, 733 741 .write = ds2781_write_user_eeprom_bin, 734 742 };
+1 -1
drivers/power/isp1704_charger.c
··· 474 474 fail2: 475 475 power_supply_unregister(&isp->psy); 476 476 fail1: 477 + isp1704_charger_set_power(isp, 0); 477 478 usb_put_transceiver(isp->phy); 478 479 fail0: 479 480 kfree(isp); 480 481 481 482 dev_err(&pdev->dev, "failed to register isp1704 with error %d\n", ret); 482 483 483 - isp1704_charger_set_power(isp, 0); 484 484 return ret; 485 485 } 486 486
+125 -23
drivers/power/max17042_battery.c
··· 28 28 #include <linux/i2c.h> 29 29 #include <linux/delay.h> 30 30 #include <linux/interrupt.h> 31 + #include <linux/pm.h> 31 32 #include <linux/mod_devicetable.h> 32 33 #include <linux/power_supply.h> 33 34 #include <linux/power/max17042_battery.h> ··· 62 61 #define dP_ACC_100 0x1900 63 62 #define dP_ACC_200 0x3200 64 63 64 + #define MAX17042_IC_VERSION 0x0092 65 + #define MAX17047_IC_VERSION 0x00AC /* same for max17050 */ 66 + 65 67 struct max17042_chip { 66 68 struct i2c_client *client; 67 69 struct power_supply battery; 70 + enum max170xx_chip_type chip_type; 68 71 struct max17042_platform_data *pdata; 69 72 struct work_struct work; 70 73 int init_complete; ··· 110 105 POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN, 111 106 POWER_SUPPLY_PROP_VOLTAGE_NOW, 112 107 POWER_SUPPLY_PROP_VOLTAGE_AVG, 108 + POWER_SUPPLY_PROP_VOLTAGE_OCV, 113 109 POWER_SUPPLY_PROP_CAPACITY, 114 110 POWER_SUPPLY_PROP_CHARGE_FULL, 115 111 POWER_SUPPLY_PROP_TEMP, ··· 156 150 val->intval *= 20000; /* Units of LSB = 20mV */ 157 151 break; 158 152 case POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN: 159 - ret = max17042_read_reg(chip->client, MAX17042_V_empty); 153 + if (chip->chip_type == MAX17042) 154 + ret = max17042_read_reg(chip->client, MAX17042_V_empty); 155 + else 156 + ret = max17042_read_reg(chip->client, MAX17047_V_empty); 160 157 if (ret < 0) 161 158 return ret; 162 159 ··· 175 166 break; 176 167 case POWER_SUPPLY_PROP_VOLTAGE_AVG: 177 168 ret = max17042_read_reg(chip->client, MAX17042_AvgVCELL); 169 + if (ret < 0) 170 + return ret; 171 + 172 + val->intval = ret * 625 / 8; 173 + break; 174 + case POWER_SUPPLY_PROP_VOLTAGE_OCV: 175 + ret = max17042_read_reg(chip->client, MAX17042_OCVInternal); 178 176 if (ret < 0) 179 177 return ret; 180 178 ··· 341 325 static int max17042_init_model(struct max17042_chip *chip) 342 326 { 343 327 int ret; 344 - int table_size = 345 - sizeof(chip->pdata->config_data->cell_char_tbl)/sizeof(u16); 328 + int table_size = ARRAY_SIZE(chip->pdata->config_data->cell_char_tbl); 346 329 u16 *temp_data; 347 330 348 - temp_data = kzalloc(table_size, GFP_KERNEL); 331 + temp_data = kcalloc(table_size, sizeof(*temp_data), GFP_KERNEL); 349 332 if (!temp_data) 350 333 return -ENOMEM; 351 334 ··· 369 354 static int max17042_verify_model_lock(struct max17042_chip *chip) 370 355 { 371 356 int i; 372 - int table_size = 373 - sizeof(chip->pdata->config_data->cell_char_tbl); 357 + int table_size = ARRAY_SIZE(chip->pdata->config_data->cell_char_tbl); 374 358 u16 *temp_data; 375 359 int ret = 0; 376 360 377 - temp_data = kzalloc(table_size, GFP_KERNEL); 361 + temp_data = kcalloc(table_size, sizeof(*temp_data), GFP_KERNEL); 378 362 if (!temp_data) 379 363 return -ENOMEM; 380 364 ··· 396 382 max17042_write_reg(chip->client, MAX17042_FilterCFG, 397 383 config->filter_cfg); 398 384 max17042_write_reg(chip->client, MAX17042_RelaxCFG, config->relax_cfg); 385 + if (chip->chip_type == MAX17047) 386 + max17042_write_reg(chip->client, MAX17047_FullSOCThr, 387 + config->full_soc_thresh); 399 388 } 400 389 401 390 static void max17042_write_custom_regs(struct max17042_chip *chip) ··· 409 392 config->rcomp0); 410 393 max17042_write_verify_reg(chip->client, MAX17042_TempCo, 411 394 config->tcompc0); 412 - max17042_write_reg(chip->client, MAX17042_EmptyTempCo, 413 - config->empty_tempco); 414 - max17042_write_verify_reg(chip->client, MAX17042_K_empty0, 415 - config->kempty0); 416 395 max17042_write_verify_reg(chip->client, MAX17042_ICHGTerm, 417 396 config->ichgt_term); 397 + if (chip->chip_type == MAX17042) { 398 + max17042_write_reg(chip->client, MAX17042_EmptyTempCo, 399 + config->empty_tempco); 400 + max17042_write_verify_reg(chip->client, MAX17042_K_empty0, 401 + config->kempty0); 402 + } else { 403 + max17042_write_verify_reg(chip->client, MAX17047_QRTbl00, 404 + config->qrtbl00); 405 + max17042_write_verify_reg(chip->client, MAX17047_QRTbl10, 406 + config->qrtbl10); 407 + max17042_write_verify_reg(chip->client, MAX17047_QRTbl20, 408 + config->qrtbl20); 409 + max17042_write_verify_reg(chip->client, MAX17047_QRTbl30, 410 + config->qrtbl30); 411 + } 418 412 } 419 413 420 414 static void max17042_update_capacity_regs(struct max17042_chip *chip) ··· 481 453 config->design_cap); 482 454 max17042_write_verify_reg(chip->client, MAX17042_FullCAPNom, 483 455 config->fullcapnom); 456 + /* Update SOC register with new SOC */ 457 + max17042_write_reg(chip->client, MAX17042_RepSOC, vfSoc); 484 458 } 485 459 486 460 /* ··· 519 489 520 490 max17042_override_por(client, MAX17042_FullCAP, config->fullcap); 521 491 max17042_override_por(client, MAX17042_FullCAPNom, config->fullcapnom); 522 - max17042_override_por(client, MAX17042_SOC_empty, config->socempty); 492 + if (chip->chip_type == MAX17042) 493 + max17042_override_por(client, MAX17042_SOC_empty, 494 + config->socempty); 523 495 max17042_override_por(client, MAX17042_LAvg_empty, config->lavg_empty); 524 496 max17042_override_por(client, MAX17042_dQacc, config->dqacc); 525 497 max17042_override_por(client, MAX17042_dPacc, config->dpacc); 526 498 527 - max17042_override_por(client, MAX17042_V_empty, config->vempty); 499 + if (chip->chip_type == MAX17042) 500 + max17042_override_por(client, MAX17042_V_empty, config->vempty); 501 + else 502 + max17042_override_por(client, MAX17047_V_empty, config->vempty); 528 503 max17042_override_por(client, MAX17042_TempNom, config->temp_nom); 529 504 max17042_override_por(client, MAX17042_TempLim, config->temp_lim); 530 505 max17042_override_por(client, MAX17042_FCTC, config->fctc); 531 506 max17042_override_por(client, MAX17042_RCOMP0, config->rcomp0); 532 507 max17042_override_por(client, MAX17042_TempCo, config->tcompc0); 533 - max17042_override_por(client, MAX17042_EmptyTempCo, 534 - config->empty_tempco); 535 - max17042_override_por(client, MAX17042_K_empty0, config->kempty0); 508 + if (chip->chip_type) { 509 + max17042_override_por(client, MAX17042_EmptyTempCo, 510 + config->empty_tempco); 511 + max17042_override_por(client, MAX17042_K_empty0, 512 + config->kempty0); 513 + } 536 514 } 537 515 538 516 static int max17042_init_chip(struct max17042_chip *chip) ··· 697 659 698 660 i2c_set_clientdata(client, chip); 699 661 700 - chip->battery.name = "max17042_battery"; 662 + ret = max17042_read_reg(chip->client, MAX17042_DevName); 663 + if (ret == MAX17042_IC_VERSION) { 664 + dev_dbg(&client->dev, "chip type max17042 detected\n"); 665 + chip->chip_type = MAX17042; 666 + } else if (ret == MAX17047_IC_VERSION) { 667 + dev_dbg(&client->dev, "chip type max17047/50 detected\n"); 668 + chip->chip_type = MAX17047; 669 + } else { 670 + dev_err(&client->dev, "device version mismatch: %x\n", ret); 671 + return -EIO; 672 + } 673 + 674 + chip->battery.name = "max170xx_battery"; 701 675 chip->battery.type = POWER_SUPPLY_TYPE_BATTERY; 702 676 chip->battery.get_property = max17042_get_property; 703 677 chip->battery.properties = max17042_battery_props; ··· 733 683 max17042_write_reg(client, MAX17042_LearnCFG, 0x0007); 734 684 } 735 685 686 + ret = power_supply_register(&client->dev, &chip->battery); 687 + if (ret) { 688 + dev_err(&client->dev, "failed: power supply register\n"); 689 + return ret; 690 + } 691 + 736 692 if (client->irq) { 737 693 ret = request_threaded_irq(client->irq, NULL, 738 694 max17042_thread_handler, ··· 749 693 reg |= CONFIG_ALRT_BIT_ENBL; 750 694 max17042_write_reg(client, MAX17042_CONFIG, reg); 751 695 max17042_set_soc_threshold(chip, 1); 752 - } else 696 + } else { 697 + client->irq = 0; 753 698 dev_err(&client->dev, "%s(): cannot get IRQ\n", 754 699 __func__); 700 + } 755 701 } 756 702 757 703 reg = max17042_read_reg(chip->client, MAX17042_STATUS); 758 - 759 704 if (reg & STATUS_POR_BIT) { 760 705 INIT_WORK(&chip->work, max17042_init_worker); 761 706 schedule_work(&chip->work); ··· 764 707 chip->init_complete = 1; 765 708 } 766 709 767 - ret = power_supply_register(&client->dev, &chip->battery); 768 - if (ret) 769 - dev_err(&client->dev, "failed: power supply register\n"); 770 - return ret; 710 + return 0; 771 711 } 772 712 773 713 static int __devexit max17042_remove(struct i2c_client *client) 774 714 { 775 715 struct max17042_chip *chip = i2c_get_clientdata(client); 776 716 717 + if (client->irq) 718 + free_irq(client->irq, chip); 777 719 power_supply_unregister(&chip->battery); 778 720 return 0; 779 721 } 780 722 723 + #ifdef CONFIG_PM 724 + static int max17042_suspend(struct device *dev) 725 + { 726 + struct max17042_chip *chip = dev_get_drvdata(dev); 727 + 728 + /* 729 + * disable the irq and enable irq_wake 730 + * capability to the interrupt line. 731 + */ 732 + if (chip->client->irq) { 733 + disable_irq(chip->client->irq); 734 + enable_irq_wake(chip->client->irq); 735 + } 736 + 737 + return 0; 738 + } 739 + 740 + static int max17042_resume(struct device *dev) 741 + { 742 + struct max17042_chip *chip = dev_get_drvdata(dev); 743 + 744 + if (chip->client->irq) { 745 + disable_irq_wake(chip->client->irq); 746 + enable_irq(chip->client->irq); 747 + /* re-program the SOC thresholds to 1% change */ 748 + max17042_set_soc_threshold(chip, 1); 749 + } 750 + 751 + return 0; 752 + } 753 + 754 + static const struct dev_pm_ops max17042_pm_ops = { 755 + .suspend = max17042_suspend, 756 + .resume = max17042_resume, 757 + }; 758 + 759 + #define MAX17042_PM_OPS (&max17042_pm_ops) 760 + #else 761 + #define MAX17042_PM_OPS NULL 762 + #endif 763 + 781 764 #ifdef CONFIG_OF 782 765 static const struct of_device_id max17042_dt_match[] = { 783 766 { .compatible = "maxim,max17042" }, 767 + { .compatible = "maxim,max17047" }, 768 + { .compatible = "maxim,max17050" }, 784 769 { }, 785 770 }; 786 771 MODULE_DEVICE_TABLE(of, max17042_dt_match); ··· 830 731 831 732 static const struct i2c_device_id max17042_id[] = { 832 733 { "max17042", 0 }, 734 + { "max17047", 1 }, 735 + { "max17050", 2 }, 833 736 { } 834 737 }; 835 738 MODULE_DEVICE_TABLE(i2c, max17042_id); ··· 840 739 .driver = { 841 740 .name = "max17042", 842 741 .of_match_table = of_match_ptr(max17042_dt_match), 742 + .pm = MAX17042_PM_OPS, 843 743 }, 844 744 .probe = max17042_probe, 845 745 .remove = __devexit_p(max17042_remove),
+1
drivers/power/power_supply_sysfs.c
··· 146 146 POWER_SUPPLY_ATTR(voltage_min_design), 147 147 POWER_SUPPLY_ATTR(voltage_now), 148 148 POWER_SUPPLY_ATTR(voltage_avg), 149 + POWER_SUPPLY_ATTR(voltage_ocv), 149 150 POWER_SUPPLY_ATTR(current_max), 150 151 POWER_SUPPLY_ATTR(current_now), 151 152 POWER_SUPPLY_ATTR(current_avg),
+1 -1
drivers/power/sbs-battery.c
··· 89 89 [REG_CURRENT] = 90 90 SBS_DATA(POWER_SUPPLY_PROP_CURRENT_NOW, 0x0A, -32768, 32767), 91 91 [REG_CAPACITY] = 92 - SBS_DATA(POWER_SUPPLY_PROP_CAPACITY, 0x0E, 0, 100), 92 + SBS_DATA(POWER_SUPPLY_PROP_CAPACITY, 0x0D, 0, 100), 93 93 [REG_REMAINING_CAPACITY] = 94 94 SBS_DATA(POWER_SUPPLY_PROP_ENERGY_NOW, 0x0F, 0, 65535), 95 95 [REG_REMAINING_CAPACITY_CHARGE] =
+282 -428
drivers/power/smb347-charger.c
··· 11 11 * published by the Free Software Foundation. 12 12 */ 13 13 14 - #include <linux/debugfs.h> 14 + #include <linux/err.h> 15 15 #include <linux/gpio.h> 16 16 #include <linux/kernel.h> 17 17 #include <linux/module.h> ··· 21 21 #include <linux/mutex.h> 22 22 #include <linux/power_supply.h> 23 23 #include <linux/power/smb347-charger.h> 24 - #include <linux/seq_file.h> 24 + #include <linux/regmap.h> 25 25 26 26 /* 27 27 * Configuration registers. These are mirrored to volatile RAM and can be ··· 39 39 #define CFG_CURRENT_LIMIT_DC_SHIFT 4 40 40 #define CFG_CURRENT_LIMIT_USB_MASK 0x0f 41 41 #define CFG_FLOAT_VOLTAGE 0x03 42 + #define CFG_FLOAT_VOLTAGE_FLOAT_MASK 0x3f 42 43 #define CFG_FLOAT_VOLTAGE_THRESHOLD_MASK 0xc0 43 44 #define CFG_FLOAT_VOLTAGE_THRESHOLD_SHIFT 6 44 45 #define CFG_STAT 0x05 ··· 114 113 #define STAT_C_CHARGER_ERROR BIT(6) 115 114 #define STAT_E 0x3f 116 115 116 + #define SMB347_MAX_REGISTER 0x3f 117 + 117 118 /** 118 119 * struct smb347_charger - smb347 charger instance 119 120 * @lock: protects concurrent access to online variables 120 - * @client: pointer to i2c client 121 + * @dev: pointer to device 122 + * @regmap: pointer to driver regmap 121 123 * @mains: power_supply instance for AC/DC power 122 124 * @usb: power_supply instance for USB power 123 125 * @battery: power_supply instance for battery 124 126 * @mains_online: is AC/DC input connected 125 127 * @usb_online: is USB input connected 126 128 * @charging_enabled: is charging enabled 127 - * @dentry: for debugfs 128 129 * @pdata: pointer to platform data 129 130 */ 130 131 struct smb347_charger { 131 132 struct mutex lock; 132 - struct i2c_client *client; 133 + struct device *dev; 134 + struct regmap *regmap; 133 135 struct power_supply mains; 134 136 struct power_supply usb; 135 137 struct power_supply battery; 136 138 bool mains_online; 137 139 bool usb_online; 138 140 bool charging_enabled; 139 - struct dentry *dentry; 140 141 const struct smb347_charger_platform_data *pdata; 141 142 }; 142 143 ··· 196 193 1200000, 197 194 }; 198 195 199 - /* Convert register value to current using lookup table */ 200 - static int hw_to_current(const unsigned int *tbl, size_t size, unsigned int val) 201 - { 202 - if (val >= size) 203 - return -EINVAL; 204 - return tbl[val]; 205 - } 206 - 207 196 /* Convert current to register value using lookup table */ 208 197 static int current_to_hw(const unsigned int *tbl, size_t size, unsigned int val) 209 198 { ··· 207 212 return i > 0 ? i - 1 : -EINVAL; 208 213 } 209 214 210 - static int smb347_read(struct smb347_charger *smb, u8 reg) 211 - { 212 - int ret; 213 - 214 - ret = i2c_smbus_read_byte_data(smb->client, reg); 215 - if (ret < 0) 216 - dev_warn(&smb->client->dev, "failed to read reg 0x%x: %d\n", 217 - reg, ret); 218 - return ret; 219 - } 220 - 221 - static int smb347_write(struct smb347_charger *smb, u8 reg, u8 val) 222 - { 223 - int ret; 224 - 225 - ret = i2c_smbus_write_byte_data(smb->client, reg, val); 226 - if (ret < 0) 227 - dev_warn(&smb->client->dev, "failed to write reg 0x%x: %d\n", 228 - reg, ret); 229 - return ret; 230 - } 231 - 232 215 /** 233 - * smb347_update_status - updates the charging status 216 + * smb347_update_ps_status - refreshes the power source status 234 217 * @smb: pointer to smb347 charger instance 235 218 * 236 - * Function checks status of the charging and updates internal state 237 - * accordingly. Returns %0 if there is no change in status, %1 if the 238 - * status has changed and negative errno in case of failure. 219 + * Function checks whether any power source is connected to the charger and 220 + * updates internal state accordingly. If there is a change to previous state 221 + * function returns %1, otherwise %0 and negative errno in case of errror. 239 222 */ 240 - static int smb347_update_status(struct smb347_charger *smb) 223 + static int smb347_update_ps_status(struct smb347_charger *smb) 241 224 { 242 225 bool usb = false; 243 226 bool dc = false; 227 + unsigned int val; 244 228 int ret; 245 229 246 - ret = smb347_read(smb, IRQSTAT_E); 230 + ret = regmap_read(smb->regmap, IRQSTAT_E, &val); 247 231 if (ret < 0) 248 232 return ret; 249 233 ··· 231 257 * platform data _and_ whether corresponding undervoltage is set. 232 258 */ 233 259 if (smb->pdata->use_mains) 234 - dc = !(ret & IRQSTAT_E_DCIN_UV_STAT); 260 + dc = !(val & IRQSTAT_E_DCIN_UV_STAT); 235 261 if (smb->pdata->use_usb) 236 - usb = !(ret & IRQSTAT_E_USBIN_UV_STAT); 262 + usb = !(val & IRQSTAT_E_USBIN_UV_STAT); 237 263 238 264 mutex_lock(&smb->lock); 239 265 ret = smb->mains_online != dc || smb->usb_online != usb; ··· 245 271 } 246 272 247 273 /* 248 - * smb347_is_online - returns whether input power source is connected 274 + * smb347_is_ps_online - returns whether input power source is connected 249 275 * @smb: pointer to smb347 charger instance 250 276 * 251 277 * Returns %true if input power source is connected. Note that this is 252 278 * dependent on what platform has configured for usable power sources. For 253 - * example if USB is disabled, this will return %false even if the USB 254 - * cable is connected. 279 + * example if USB is disabled, this will return %false even if the USB cable 280 + * is connected. 255 281 */ 256 - static bool smb347_is_online(struct smb347_charger *smb) 282 + static bool smb347_is_ps_online(struct smb347_charger *smb) 257 283 { 258 284 bool ret; 259 285 ··· 273 299 */ 274 300 static int smb347_charging_status(struct smb347_charger *smb) 275 301 { 302 + unsigned int val; 276 303 int ret; 277 304 278 - if (!smb347_is_online(smb)) 305 + if (!smb347_is_ps_online(smb)) 279 306 return 0; 280 307 281 - ret = smb347_read(smb, STAT_C); 308 + ret = regmap_read(smb->regmap, STAT_C, &val); 282 309 if (ret < 0) 283 310 return 0; 284 311 285 - return (ret & STAT_C_CHG_MASK) >> STAT_C_CHG_SHIFT; 312 + return (val & STAT_C_CHG_MASK) >> STAT_C_CHG_SHIFT; 286 313 } 287 314 288 315 static int smb347_charging_set(struct smb347_charger *smb, bool enable) ··· 291 316 int ret = 0; 292 317 293 318 if (smb->pdata->enable_control != SMB347_CHG_ENABLE_SW) { 294 - dev_dbg(&smb->client->dev, 295 - "charging enable/disable in SW disabled\n"); 319 + dev_dbg(smb->dev, "charging enable/disable in SW disabled\n"); 296 320 return 0; 297 321 } 298 322 299 323 mutex_lock(&smb->lock); 300 324 if (smb->charging_enabled != enable) { 301 - ret = smb347_read(smb, CMD_A); 302 - if (ret < 0) 303 - goto out; 304 - 305 - smb->charging_enabled = enable; 306 - 307 - if (enable) 308 - ret |= CMD_A_CHG_ENABLED; 309 - else 310 - ret &= ~CMD_A_CHG_ENABLED; 311 - 312 - ret = smb347_write(smb, CMD_A, ret); 325 + ret = regmap_update_bits(smb->regmap, CMD_A, CMD_A_CHG_ENABLED, 326 + enable ? CMD_A_CHG_ENABLED : 0); 327 + if (!ret) 328 + smb->charging_enabled = enable; 313 329 } 314 - out: 315 330 mutex_unlock(&smb->lock); 316 331 return ret; 317 332 } ··· 316 351 return smb347_charging_set(smb, false); 317 352 } 318 353 319 - static int smb347_update_online(struct smb347_charger *smb) 354 + static int smb347_start_stop_charging(struct smb347_charger *smb) 320 355 { 321 356 int ret; 322 357 ··· 325 360 * disable or enable the charging. We do it manually because it 326 361 * depends on how the platform has configured the valid inputs. 327 362 */ 328 - if (smb347_is_online(smb)) { 363 + if (smb347_is_ps_online(smb)) { 329 364 ret = smb347_charging_enable(smb); 330 365 if (ret < 0) 331 - dev_err(&smb->client->dev, 332 - "failed to enable charging\n"); 366 + dev_err(smb->dev, "failed to enable charging\n"); 333 367 } else { 334 368 ret = smb347_charging_disable(smb); 335 369 if (ret < 0) 336 - dev_err(&smb->client->dev, 337 - "failed to disable charging\n"); 370 + dev_err(smb->dev, "failed to disable charging\n"); 338 371 } 339 372 340 373 return ret; ··· 340 377 341 378 static int smb347_set_charge_current(struct smb347_charger *smb) 342 379 { 343 - int ret, val; 344 - 345 - ret = smb347_read(smb, CFG_CHARGE_CURRENT); 346 - if (ret < 0) 347 - return ret; 380 + int ret; 348 381 349 382 if (smb->pdata->max_charge_current) { 350 - val = current_to_hw(fcc_tbl, ARRAY_SIZE(fcc_tbl), 383 + ret = current_to_hw(fcc_tbl, ARRAY_SIZE(fcc_tbl), 351 384 smb->pdata->max_charge_current); 352 - if (val < 0) 353 - return val; 385 + if (ret < 0) 386 + return ret; 354 387 355 - ret &= ~CFG_CHARGE_CURRENT_FCC_MASK; 356 - ret |= val << CFG_CHARGE_CURRENT_FCC_SHIFT; 388 + ret = regmap_update_bits(smb->regmap, CFG_CHARGE_CURRENT, 389 + CFG_CHARGE_CURRENT_FCC_MASK, 390 + ret << CFG_CHARGE_CURRENT_FCC_SHIFT); 391 + if (ret < 0) 392 + return ret; 357 393 } 358 394 359 395 if (smb->pdata->pre_charge_current) { 360 - val = current_to_hw(pcc_tbl, ARRAY_SIZE(pcc_tbl), 396 + ret = current_to_hw(pcc_tbl, ARRAY_SIZE(pcc_tbl), 361 397 smb->pdata->pre_charge_current); 362 - if (val < 0) 363 - return val; 398 + if (ret < 0) 399 + return ret; 364 400 365 - ret &= ~CFG_CHARGE_CURRENT_PCC_MASK; 366 - ret |= val << CFG_CHARGE_CURRENT_PCC_SHIFT; 401 + ret = regmap_update_bits(smb->regmap, CFG_CHARGE_CURRENT, 402 + CFG_CHARGE_CURRENT_PCC_MASK, 403 + ret << CFG_CHARGE_CURRENT_PCC_SHIFT); 404 + if (ret < 0) 405 + return ret; 367 406 } 368 407 369 408 if (smb->pdata->termination_current) { 370 - val = current_to_hw(tc_tbl, ARRAY_SIZE(tc_tbl), 409 + ret = current_to_hw(tc_tbl, ARRAY_SIZE(tc_tbl), 371 410 smb->pdata->termination_current); 372 - if (val < 0) 373 - return val; 411 + if (ret < 0) 412 + return ret; 374 413 375 - ret &= ~CFG_CHARGE_CURRENT_TC_MASK; 376 - ret |= val; 414 + ret = regmap_update_bits(smb->regmap, CFG_CHARGE_CURRENT, 415 + CFG_CHARGE_CURRENT_TC_MASK, ret); 416 + if (ret < 0) 417 + return ret; 377 418 } 378 419 379 - return smb347_write(smb, CFG_CHARGE_CURRENT, ret); 420 + return 0; 380 421 } 381 422 382 423 static int smb347_set_current_limits(struct smb347_charger *smb) 383 424 { 384 - int ret, val; 385 - 386 - ret = smb347_read(smb, CFG_CURRENT_LIMIT); 387 - if (ret < 0) 388 - return ret; 425 + int ret; 389 426 390 427 if (smb->pdata->mains_current_limit) { 391 - val = current_to_hw(icl_tbl, ARRAY_SIZE(icl_tbl), 428 + ret = current_to_hw(icl_tbl, ARRAY_SIZE(icl_tbl), 392 429 smb->pdata->mains_current_limit); 393 - if (val < 0) 394 - return val; 430 + if (ret < 0) 431 + return ret; 395 432 396 - ret &= ~CFG_CURRENT_LIMIT_DC_MASK; 397 - ret |= val << CFG_CURRENT_LIMIT_DC_SHIFT; 433 + ret = regmap_update_bits(smb->regmap, CFG_CURRENT_LIMIT, 434 + CFG_CURRENT_LIMIT_DC_MASK, 435 + ret << CFG_CURRENT_LIMIT_DC_SHIFT); 436 + if (ret < 0) 437 + return ret; 398 438 } 399 439 400 440 if (smb->pdata->usb_hc_current_limit) { 401 - val = current_to_hw(icl_tbl, ARRAY_SIZE(icl_tbl), 441 + ret = current_to_hw(icl_tbl, ARRAY_SIZE(icl_tbl), 402 442 smb->pdata->usb_hc_current_limit); 403 - if (val < 0) 404 - return val; 443 + if (ret < 0) 444 + return ret; 405 445 406 - ret &= ~CFG_CURRENT_LIMIT_USB_MASK; 407 - ret |= val; 446 + ret = regmap_update_bits(smb->regmap, CFG_CURRENT_LIMIT, 447 + CFG_CURRENT_LIMIT_USB_MASK, ret); 448 + if (ret < 0) 449 + return ret; 408 450 } 409 451 410 - return smb347_write(smb, CFG_CURRENT_LIMIT, ret); 452 + return 0; 411 453 } 412 454 413 455 static int smb347_set_voltage_limits(struct smb347_charger *smb) 414 456 { 415 - int ret, val; 416 - 417 - ret = smb347_read(smb, CFG_FLOAT_VOLTAGE); 418 - if (ret < 0) 419 - return ret; 457 + int ret; 420 458 421 459 if (smb->pdata->pre_to_fast_voltage) { 422 - val = smb->pdata->pre_to_fast_voltage; 460 + ret = smb->pdata->pre_to_fast_voltage; 423 461 424 462 /* uV */ 425 - val = clamp_val(val, 2400000, 3000000) - 2400000; 426 - val /= 200000; 463 + ret = clamp_val(ret, 2400000, 3000000) - 2400000; 464 + ret /= 200000; 427 465 428 - ret &= ~CFG_FLOAT_VOLTAGE_THRESHOLD_MASK; 429 - ret |= val << CFG_FLOAT_VOLTAGE_THRESHOLD_SHIFT; 466 + ret = regmap_update_bits(smb->regmap, CFG_FLOAT_VOLTAGE, 467 + CFG_FLOAT_VOLTAGE_THRESHOLD_MASK, 468 + ret << CFG_FLOAT_VOLTAGE_THRESHOLD_SHIFT); 469 + if (ret < 0) 470 + return ret; 430 471 } 431 472 432 473 if (smb->pdata->max_charge_voltage) { 433 - val = smb->pdata->max_charge_voltage; 474 + ret = smb->pdata->max_charge_voltage; 434 475 435 476 /* uV */ 436 - val = clamp_val(val, 3500000, 4500000) - 3500000; 437 - val /= 20000; 477 + ret = clamp_val(ret, 3500000, 4500000) - 3500000; 478 + ret /= 20000; 438 479 439 - ret |= val; 480 + ret = regmap_update_bits(smb->regmap, CFG_FLOAT_VOLTAGE, 481 + CFG_FLOAT_VOLTAGE_FLOAT_MASK, ret); 482 + if (ret < 0) 483 + return ret; 440 484 } 441 485 442 - return smb347_write(smb, CFG_FLOAT_VOLTAGE, ret); 486 + return 0; 443 487 } 444 488 445 489 static int smb347_set_temp_limits(struct smb347_charger *smb) 446 490 { 447 491 bool enable_therm_monitor = false; 448 - int ret, val; 492 + int ret = 0; 493 + int val; 449 494 450 495 if (smb->pdata->chip_temp_threshold) { 451 496 val = smb->pdata->chip_temp_threshold; ··· 462 491 val = clamp_val(val, 100, 130) - 100; 463 492 val /= 10; 464 493 465 - ret = smb347_read(smb, CFG_OTG); 466 - if (ret < 0) 467 - return ret; 468 - 469 - ret &= ~CFG_OTG_TEMP_THRESHOLD_MASK; 470 - ret |= val << CFG_OTG_TEMP_THRESHOLD_SHIFT; 471 - 472 - ret = smb347_write(smb, CFG_OTG, ret); 494 + ret = regmap_update_bits(smb->regmap, CFG_OTG, 495 + CFG_OTG_TEMP_THRESHOLD_MASK, 496 + val << CFG_OTG_TEMP_THRESHOLD_SHIFT); 473 497 if (ret < 0) 474 498 return ret; 475 499 } 476 - 477 - ret = smb347_read(smb, CFG_TEMP_LIMIT); 478 - if (ret < 0) 479 - return ret; 480 500 481 501 if (smb->pdata->soft_cold_temp_limit != SMB347_TEMP_USE_DEFAULT) { 482 502 val = smb->pdata->soft_cold_temp_limit; ··· 477 515 /* this goes from higher to lower so invert the value */ 478 516 val = ~val & 0x3; 479 517 480 - ret &= ~CFG_TEMP_LIMIT_SOFT_COLD_MASK; 481 - ret |= val << CFG_TEMP_LIMIT_SOFT_COLD_SHIFT; 518 + ret = regmap_update_bits(smb->regmap, CFG_TEMP_LIMIT, 519 + CFG_TEMP_LIMIT_SOFT_COLD_MASK, 520 + val << CFG_TEMP_LIMIT_SOFT_COLD_SHIFT); 521 + if (ret < 0) 522 + return ret; 482 523 483 524 enable_therm_monitor = true; 484 525 } ··· 492 527 val = clamp_val(val, 40, 55) - 40; 493 528 val /= 5; 494 529 495 - ret &= ~CFG_TEMP_LIMIT_SOFT_HOT_MASK; 496 - ret |= val << CFG_TEMP_LIMIT_SOFT_HOT_SHIFT; 530 + ret = regmap_update_bits(smb->regmap, CFG_TEMP_LIMIT, 531 + CFG_TEMP_LIMIT_SOFT_HOT_MASK, 532 + val << CFG_TEMP_LIMIT_SOFT_HOT_SHIFT); 533 + if (ret < 0) 534 + return ret; 497 535 498 536 enable_therm_monitor = true; 499 537 } ··· 509 541 /* this goes from higher to lower so invert the value */ 510 542 val = ~val & 0x3; 511 543 512 - ret &= ~CFG_TEMP_LIMIT_HARD_COLD_MASK; 513 - ret |= val << CFG_TEMP_LIMIT_HARD_COLD_SHIFT; 544 + ret = regmap_update_bits(smb->regmap, CFG_TEMP_LIMIT, 545 + CFG_TEMP_LIMIT_HARD_COLD_MASK, 546 + val << CFG_TEMP_LIMIT_HARD_COLD_SHIFT); 547 + if (ret < 0) 548 + return ret; 514 549 515 550 enable_therm_monitor = true; 516 551 } ··· 524 553 val = clamp_val(val, 50, 65) - 50; 525 554 val /= 5; 526 555 527 - ret &= ~CFG_TEMP_LIMIT_HARD_HOT_MASK; 528 - ret |= val << CFG_TEMP_LIMIT_HARD_HOT_SHIFT; 556 + ret = regmap_update_bits(smb->regmap, CFG_TEMP_LIMIT, 557 + CFG_TEMP_LIMIT_HARD_HOT_MASK, 558 + val << CFG_TEMP_LIMIT_HARD_HOT_SHIFT); 559 + if (ret < 0) 560 + return ret; 529 561 530 562 enable_therm_monitor = true; 531 563 } 532 - 533 - ret = smb347_write(smb, CFG_TEMP_LIMIT, ret); 534 - if (ret < 0) 535 - return ret; 536 564 537 565 /* 538 566 * If any of the temperature limits are set, we also enable the ··· 544 574 * depending on the configuration. 545 575 */ 546 576 if (enable_therm_monitor) { 547 - ret = smb347_read(smb, CFG_THERM); 548 - if (ret < 0) 549 - return ret; 550 - 551 - ret &= ~CFG_THERM_MONITOR_DISABLED; 552 - 553 - ret = smb347_write(smb, CFG_THERM, ret); 577 + ret = regmap_update_bits(smb->regmap, CFG_THERM, 578 + CFG_THERM_MONITOR_DISABLED, 0); 554 579 if (ret < 0) 555 580 return ret; 556 581 } 557 582 558 583 if (smb->pdata->suspend_on_hard_temp_limit) { 559 - ret = smb347_read(smb, CFG_SYSOK); 560 - if (ret < 0) 561 - return ret; 562 - 563 - ret &= ~CFG_SYSOK_SUSPEND_HARD_LIMIT_DISABLED; 564 - 565 - ret = smb347_write(smb, CFG_SYSOK, ret); 584 + ret = regmap_update_bits(smb->regmap, CFG_SYSOK, 585 + CFG_SYSOK_SUSPEND_HARD_LIMIT_DISABLED, 0); 566 586 if (ret < 0) 567 587 return ret; 568 588 } ··· 561 601 SMB347_SOFT_TEMP_COMPENSATE_DEFAULT) { 562 602 val = smb->pdata->soft_temp_limit_compensation & 0x3; 563 603 564 - ret = smb347_read(smb, CFG_THERM); 604 + ret = regmap_update_bits(smb->regmap, CFG_THERM, 605 + CFG_THERM_SOFT_HOT_COMPENSATION_MASK, 606 + val << CFG_THERM_SOFT_HOT_COMPENSATION_SHIFT); 565 607 if (ret < 0) 566 608 return ret; 567 609 568 - ret &= ~CFG_THERM_SOFT_HOT_COMPENSATION_MASK; 569 - ret |= val << CFG_THERM_SOFT_HOT_COMPENSATION_SHIFT; 570 - 571 - ret &= ~CFG_THERM_SOFT_COLD_COMPENSATION_MASK; 572 - ret |= val << CFG_THERM_SOFT_COLD_COMPENSATION_SHIFT; 573 - 574 - ret = smb347_write(smb, CFG_THERM, ret); 610 + ret = regmap_update_bits(smb->regmap, CFG_THERM, 611 + CFG_THERM_SOFT_COLD_COMPENSATION_MASK, 612 + val << CFG_THERM_SOFT_COLD_COMPENSATION_SHIFT); 575 613 if (ret < 0) 576 614 return ret; 577 615 } ··· 580 622 if (val < 0) 581 623 return val; 582 624 583 - ret = smb347_read(smb, CFG_OTG); 584 - if (ret < 0) 585 - return ret; 586 - 587 - ret &= ~CFG_OTG_CC_COMPENSATION_MASK; 588 - ret |= (val & 0x3) << CFG_OTG_CC_COMPENSATION_SHIFT; 589 - 590 - ret = smb347_write(smb, CFG_OTG, ret); 625 + ret = regmap_update_bits(smb->regmap, CFG_OTG, 626 + CFG_OTG_CC_COMPENSATION_MASK, 627 + (val & 0x3) << CFG_OTG_CC_COMPENSATION_SHIFT); 591 628 if (ret < 0) 592 629 return ret; 593 630 } ··· 601 648 */ 602 649 static int smb347_set_writable(struct smb347_charger *smb, bool writable) 603 650 { 604 - int ret; 605 - 606 - ret = smb347_read(smb, CMD_A); 607 - if (ret < 0) 608 - return ret; 609 - 610 - if (writable) 611 - ret |= CMD_A_ALLOW_WRITE; 612 - else 613 - ret &= ~CMD_A_ALLOW_WRITE; 614 - 615 - return smb347_write(smb, CMD_A, ret); 651 + return regmap_update_bits(smb->regmap, CMD_A, CMD_A_ALLOW_WRITE, 652 + writable ? CMD_A_ALLOW_WRITE : 0); 616 653 } 617 654 618 655 static int smb347_hw_init(struct smb347_charger *smb) 619 656 { 657 + unsigned int val; 620 658 int ret; 621 659 622 660 ret = smb347_set_writable(smb, true); ··· 636 692 637 693 /* If USB charging is disabled we put the USB in suspend mode */ 638 694 if (!smb->pdata->use_usb) { 639 - ret = smb347_read(smb, CMD_A); 640 - if (ret < 0) 641 - goto fail; 642 - 643 - ret |= CMD_A_SUSPEND_ENABLED; 644 - 645 - ret = smb347_write(smb, CMD_A, ret); 695 + ret = regmap_update_bits(smb->regmap, CMD_A, 696 + CMD_A_SUSPEND_ENABLED, 697 + CMD_A_SUSPEND_ENABLED); 646 698 if (ret < 0) 647 699 goto fail; 648 700 } 649 - 650 - ret = smb347_read(smb, CFG_OTHER); 651 - if (ret < 0) 652 - goto fail; 653 701 654 702 /* 655 703 * If configured by platform data, we enable hardware Auto-OTG 656 704 * support for driving VBUS. Otherwise we disable it. 657 705 */ 658 - ret &= ~CFG_OTHER_RID_MASK; 659 - if (smb->pdata->use_usb_otg) 660 - ret |= CFG_OTHER_RID_ENABLED_AUTO_OTG; 661 - 662 - ret = smb347_write(smb, CFG_OTHER, ret); 663 - if (ret < 0) 664 - goto fail; 665 - 666 - ret = smb347_read(smb, CFG_PIN); 706 + ret = regmap_update_bits(smb->regmap, CFG_OTHER, CFG_OTHER_RID_MASK, 707 + smb->pdata->use_usb_otg ? CFG_OTHER_RID_ENABLED_AUTO_OTG : 0); 667 708 if (ret < 0) 668 709 goto fail; 669 710 ··· 657 728 * command register unless pin control is specified in the platform 658 729 * data. 659 730 */ 660 - ret &= ~CFG_PIN_EN_CTRL_MASK; 661 - 662 731 switch (smb->pdata->enable_control) { 663 - case SMB347_CHG_ENABLE_SW: 664 - /* Do nothing, 0 means i2c control */ 665 - break; 666 732 case SMB347_CHG_ENABLE_PIN_ACTIVE_LOW: 667 - ret |= CFG_PIN_EN_CTRL_ACTIVE_LOW; 733 + val = CFG_PIN_EN_CTRL_ACTIVE_LOW; 668 734 break; 669 735 case SMB347_CHG_ENABLE_PIN_ACTIVE_HIGH: 670 - ret |= CFG_PIN_EN_CTRL_ACTIVE_HIGH; 736 + val = CFG_PIN_EN_CTRL_ACTIVE_HIGH; 737 + break; 738 + default: 739 + val = 0; 671 740 break; 672 741 } 673 742 743 + ret = regmap_update_bits(smb->regmap, CFG_PIN, CFG_PIN_EN_CTRL_MASK, 744 + val); 745 + if (ret < 0) 746 + goto fail; 747 + 674 748 /* Disable Automatic Power Source Detection (APSD) interrupt. */ 675 - ret &= ~CFG_PIN_EN_APSD_IRQ; 676 - 677 - ret = smb347_write(smb, CFG_PIN, ret); 749 + ret = regmap_update_bits(smb->regmap, CFG_PIN, CFG_PIN_EN_APSD_IRQ, 0); 678 750 if (ret < 0) 679 751 goto fail; 680 752 681 - ret = smb347_update_status(smb); 753 + ret = smb347_update_ps_status(smb); 682 754 if (ret < 0) 683 755 goto fail; 684 756 685 - ret = smb347_update_online(smb); 757 + ret = smb347_start_stop_charging(smb); 686 758 687 759 fail: 688 760 smb347_set_writable(smb, false); ··· 693 763 static irqreturn_t smb347_interrupt(int irq, void *data) 694 764 { 695 765 struct smb347_charger *smb = data; 696 - int stat_c, irqstat_e, irqstat_c; 697 - irqreturn_t ret = IRQ_NONE; 766 + unsigned int stat_c, irqstat_e, irqstat_c; 767 + bool handled = false; 768 + int ret; 698 769 699 - stat_c = smb347_read(smb, STAT_C); 700 - if (stat_c < 0) { 701 - dev_warn(&smb->client->dev, "reading STAT_C failed\n"); 770 + ret = regmap_read(smb->regmap, STAT_C, &stat_c); 771 + if (ret < 0) { 772 + dev_warn(smb->dev, "reading STAT_C failed\n"); 702 773 return IRQ_NONE; 703 774 } 704 775 705 - irqstat_c = smb347_read(smb, IRQSTAT_C); 706 - if (irqstat_c < 0) { 707 - dev_warn(&smb->client->dev, "reading IRQSTAT_C failed\n"); 776 + ret = regmap_read(smb->regmap, IRQSTAT_C, &irqstat_c); 777 + if (ret < 0) { 778 + dev_warn(smb->dev, "reading IRQSTAT_C failed\n"); 708 779 return IRQ_NONE; 709 780 } 710 781 711 - irqstat_e = smb347_read(smb, IRQSTAT_E); 712 - if (irqstat_e < 0) { 713 - dev_warn(&smb->client->dev, "reading IRQSTAT_E failed\n"); 782 + ret = regmap_read(smb->regmap, IRQSTAT_E, &irqstat_e); 783 + if (ret < 0) { 784 + dev_warn(smb->dev, "reading IRQSTAT_E failed\n"); 714 785 return IRQ_NONE; 715 786 } 716 787 ··· 720 789 * disable charging. 721 790 */ 722 791 if (stat_c & STAT_C_CHARGER_ERROR) { 723 - dev_err(&smb->client->dev, 724 - "error in charger, disabling charging\n"); 792 + dev_err(smb->dev, "error in charger, disabling charging\n"); 725 793 726 794 smb347_charging_disable(smb); 727 795 power_supply_changed(&smb->battery); 728 - 729 - ret = IRQ_HANDLED; 796 + handled = true; 730 797 } 731 798 732 799 /* ··· 735 806 if (irqstat_c & (IRQSTAT_C_TERMINATION_IRQ | IRQSTAT_C_TAPER_IRQ)) { 736 807 if (irqstat_c & IRQSTAT_C_TERMINATION_STAT) 737 808 power_supply_changed(&smb->battery); 738 - ret = IRQ_HANDLED; 809 + handled = true; 739 810 } 740 811 741 812 /* ··· 743 814 * was connected or disconnected. 744 815 */ 745 816 if (irqstat_e & (IRQSTAT_E_USBIN_UV_IRQ | IRQSTAT_E_DCIN_UV_IRQ)) { 746 - if (smb347_update_status(smb) > 0) { 747 - smb347_update_online(smb); 748 - power_supply_changed(&smb->mains); 749 - power_supply_changed(&smb->usb); 817 + if (smb347_update_ps_status(smb) > 0) { 818 + smb347_start_stop_charging(smb); 819 + if (smb->pdata->use_mains) 820 + power_supply_changed(&smb->mains); 821 + if (smb->pdata->use_usb) 822 + power_supply_changed(&smb->usb); 750 823 } 751 - ret = IRQ_HANDLED; 824 + handled = true; 752 825 } 753 826 754 - return ret; 827 + return handled ? IRQ_HANDLED : IRQ_NONE; 755 828 } 756 829 757 830 static int smb347_irq_set(struct smb347_charger *smb, bool enable) ··· 770 839 * - termination current reached 771 840 * - charger error 772 841 */ 773 - if (enable) { 774 - ret = smb347_write(smb, CFG_FAULT_IRQ, CFG_FAULT_IRQ_DCIN_UV); 775 - if (ret < 0) 776 - goto fail; 842 + ret = regmap_update_bits(smb->regmap, CFG_FAULT_IRQ, 0xff, 843 + enable ? CFG_FAULT_IRQ_DCIN_UV : 0); 844 + if (ret < 0) 845 + goto fail; 777 846 778 - ret = smb347_write(smb, CFG_STATUS_IRQ, 779 - CFG_STATUS_IRQ_TERMINATION_OR_TAPER); 780 - if (ret < 0) 781 - goto fail; 847 + ret = regmap_update_bits(smb->regmap, CFG_STATUS_IRQ, 0xff, 848 + enable ? CFG_STATUS_IRQ_TERMINATION_OR_TAPER : 0); 849 + if (ret < 0) 850 + goto fail; 782 851 783 - ret = smb347_read(smb, CFG_PIN); 784 - if (ret < 0) 785 - goto fail; 786 - 787 - ret |= CFG_PIN_EN_CHARGER_ERROR; 788 - 789 - ret = smb347_write(smb, CFG_PIN, ret); 790 - } else { 791 - ret = smb347_write(smb, CFG_FAULT_IRQ, 0); 792 - if (ret < 0) 793 - goto fail; 794 - 795 - ret = smb347_write(smb, CFG_STATUS_IRQ, 0); 796 - if (ret < 0) 797 - goto fail; 798 - 799 - ret = smb347_read(smb, CFG_PIN); 800 - if (ret < 0) 801 - goto fail; 802 - 803 - ret &= ~CFG_PIN_EN_CHARGER_ERROR; 804 - 805 - ret = smb347_write(smb, CFG_PIN, ret); 806 - } 807 - 852 + ret = regmap_update_bits(smb->regmap, CFG_PIN, CFG_PIN_EN_CHARGER_ERROR, 853 + enable ? CFG_PIN_EN_CHARGER_ERROR : 0); 808 854 fail: 809 855 smb347_set_writable(smb, false); 810 856 return ret; ··· 797 889 return smb347_irq_set(smb, false); 798 890 } 799 891 800 - static int smb347_irq_init(struct smb347_charger *smb) 892 + static int smb347_irq_init(struct smb347_charger *smb, 893 + struct i2c_client *client) 801 894 { 802 895 const struct smb347_charger_platform_data *pdata = smb->pdata; 803 896 int ret, irq = gpio_to_irq(pdata->irq_gpio); 804 897 805 - ret = gpio_request_one(pdata->irq_gpio, GPIOF_IN, smb->client->name); 898 + ret = gpio_request_one(pdata->irq_gpio, GPIOF_IN, client->name); 806 899 if (ret < 0) 807 900 goto fail; 808 901 809 902 ret = request_threaded_irq(irq, NULL, smb347_interrupt, 810 - IRQF_TRIGGER_FALLING, smb->client->name, 811 - smb); 903 + IRQF_TRIGGER_FALLING, client->name, smb); 812 904 if (ret < 0) 813 905 goto fail_gpio; 814 906 ··· 820 912 * Configure the STAT output to be suitable for interrupts: disable 821 913 * all other output (except interrupts) and make it active low. 822 914 */ 823 - ret = smb347_read(smb, CFG_STAT); 824 - if (ret < 0) 825 - goto fail_readonly; 826 - 827 - ret &= ~CFG_STAT_ACTIVE_HIGH; 828 - ret |= CFG_STAT_DISABLED; 829 - 830 - ret = smb347_write(smb, CFG_STAT, ret); 831 - if (ret < 0) 832 - goto fail_readonly; 833 - 834 - ret = smb347_irq_enable(smb); 915 + ret = regmap_update_bits(smb->regmap, CFG_STAT, 916 + CFG_STAT_ACTIVE_HIGH | CFG_STAT_DISABLED, 917 + CFG_STAT_DISABLED); 835 918 if (ret < 0) 836 919 goto fail_readonly; 837 920 838 921 smb347_set_writable(smb, false); 839 - smb->client->irq = irq; 922 + client->irq = irq; 840 923 return 0; 841 924 842 925 fail_readonly: ··· 837 938 fail_gpio: 838 939 gpio_free(pdata->irq_gpio); 839 940 fail: 840 - smb->client->irq = 0; 941 + client->irq = 0; 841 942 return ret; 842 943 } 843 944 ··· 886 987 const struct smb347_charger_platform_data *pdata = smb->pdata; 887 988 int ret; 888 989 889 - ret = smb347_update_status(smb); 990 + ret = smb347_update_ps_status(smb); 890 991 if (ret < 0) 891 992 return ret; 892 993 893 994 switch (prop) { 894 995 case POWER_SUPPLY_PROP_STATUS: 895 - if (!smb347_is_online(smb)) { 996 + if (!smb347_is_ps_online(smb)) { 896 997 val->intval = POWER_SUPPLY_STATUS_DISCHARGING; 897 998 break; 898 999 } ··· 903 1004 break; 904 1005 905 1006 case POWER_SUPPLY_PROP_CHARGE_TYPE: 906 - if (!smb347_is_online(smb)) 1007 + if (!smb347_is_ps_online(smb)) 907 1008 return -ENODATA; 908 1009 909 1010 /* ··· 935 1036 val->intval = pdata->battery_info.voltage_max_design; 936 1037 break; 937 1038 938 - case POWER_SUPPLY_PROP_VOLTAGE_NOW: 939 - if (!smb347_is_online(smb)) 940 - return -ENODATA; 941 - ret = smb347_read(smb, STAT_A); 942 - if (ret < 0) 943 - return ret; 944 - 945 - ret &= STAT_A_FLOAT_VOLTAGE_MASK; 946 - if (ret > 0x3d) 947 - ret = 0x3d; 948 - 949 - val->intval = 3500000 + ret * 20000; 950 - break; 951 - 952 - case POWER_SUPPLY_PROP_CURRENT_NOW: 953 - if (!smb347_is_online(smb)) 954 - return -ENODATA; 955 - 956 - ret = smb347_read(smb, STAT_B); 957 - if (ret < 0) 958 - return ret; 959 - 960 - /* 961 - * The current value is composition of FCC and PCC values 962 - * and we can detect which table to use from bit 5. 963 - */ 964 - if (ret & 0x20) { 965 - val->intval = hw_to_current(fcc_tbl, 966 - ARRAY_SIZE(fcc_tbl), 967 - ret & 7); 968 - } else { 969 - ret >>= 3; 970 - val->intval = hw_to_current(pcc_tbl, 971 - ARRAY_SIZE(pcc_tbl), 972 - ret & 7); 973 - } 974 - break; 975 - 976 1039 case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN: 977 1040 val->intval = pdata->battery_info.charge_full_design; 978 1041 break; ··· 956 1095 POWER_SUPPLY_PROP_TECHNOLOGY, 957 1096 POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN, 958 1097 POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN, 959 - POWER_SUPPLY_PROP_VOLTAGE_NOW, 960 - POWER_SUPPLY_PROP_CURRENT_NOW, 961 1098 POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN, 962 1099 POWER_SUPPLY_PROP_MODEL_NAME, 963 1100 }; 964 1101 965 - static int smb347_debugfs_show(struct seq_file *s, void *data) 1102 + static bool smb347_volatile_reg(struct device *dev, unsigned int reg) 966 1103 { 967 - struct smb347_charger *smb = s->private; 968 - int ret; 969 - u8 reg; 970 - 971 - seq_printf(s, "Control registers:\n"); 972 - seq_printf(s, "==================\n"); 973 - for (reg = CFG_CHARGE_CURRENT; reg <= CFG_ADDRESS; reg++) { 974 - ret = smb347_read(smb, reg); 975 - seq_printf(s, "0x%02x:\t0x%02x\n", reg, ret); 976 - } 977 - seq_printf(s, "\n"); 978 - 979 - seq_printf(s, "Command registers:\n"); 980 - seq_printf(s, "==================\n"); 981 - ret = smb347_read(smb, CMD_A); 982 - seq_printf(s, "0x%02x:\t0x%02x\n", CMD_A, ret); 983 - ret = smb347_read(smb, CMD_B); 984 - seq_printf(s, "0x%02x:\t0x%02x\n", CMD_B, ret); 985 - ret = smb347_read(smb, CMD_C); 986 - seq_printf(s, "0x%02x:\t0x%02x\n", CMD_C, ret); 987 - seq_printf(s, "\n"); 988 - 989 - seq_printf(s, "Interrupt status registers:\n"); 990 - seq_printf(s, "===========================\n"); 991 - for (reg = IRQSTAT_A; reg <= IRQSTAT_F; reg++) { 992 - ret = smb347_read(smb, reg); 993 - seq_printf(s, "0x%02x:\t0x%02x\n", reg, ret); 994 - } 995 - seq_printf(s, "\n"); 996 - 997 - seq_printf(s, "Status registers:\n"); 998 - seq_printf(s, "=================\n"); 999 - for (reg = STAT_A; reg <= STAT_E; reg++) { 1000 - ret = smb347_read(smb, reg); 1001 - seq_printf(s, "0x%02x:\t0x%02x\n", reg, ret); 1104 + switch (reg) { 1105 + case IRQSTAT_A: 1106 + case IRQSTAT_C: 1107 + case IRQSTAT_E: 1108 + case IRQSTAT_F: 1109 + case STAT_A: 1110 + case STAT_B: 1111 + case STAT_C: 1112 + case STAT_E: 1113 + return true; 1002 1114 } 1003 1115 1004 - return 0; 1116 + return false; 1005 1117 } 1006 1118 1007 - static int smb347_debugfs_open(struct inode *inode, struct file *file) 1119 + static bool smb347_readable_reg(struct device *dev, unsigned int reg) 1008 1120 { 1009 - return single_open(file, smb347_debugfs_show, inode->i_private); 1121 + switch (reg) { 1122 + case CFG_CHARGE_CURRENT: 1123 + case CFG_CURRENT_LIMIT: 1124 + case CFG_FLOAT_VOLTAGE: 1125 + case CFG_STAT: 1126 + case CFG_PIN: 1127 + case CFG_THERM: 1128 + case CFG_SYSOK: 1129 + case CFG_OTHER: 1130 + case CFG_OTG: 1131 + case CFG_TEMP_LIMIT: 1132 + case CFG_FAULT_IRQ: 1133 + case CFG_STATUS_IRQ: 1134 + case CFG_ADDRESS: 1135 + case CMD_A: 1136 + case CMD_B: 1137 + case CMD_C: 1138 + return true; 1139 + } 1140 + 1141 + return smb347_volatile_reg(dev, reg); 1010 1142 } 1011 1143 1012 - static const struct file_operations smb347_debugfs_fops = { 1013 - .open = smb347_debugfs_open, 1014 - .read = seq_read, 1015 - .llseek = seq_lseek, 1016 - .release = single_release, 1144 + static const struct regmap_config smb347_regmap = { 1145 + .reg_bits = 8, 1146 + .val_bits = 8, 1147 + .max_register = SMB347_MAX_REGISTER, 1148 + .volatile_reg = smb347_volatile_reg, 1149 + .readable_reg = smb347_readable_reg, 1017 1150 }; 1018 1151 1019 1152 static int smb347_probe(struct i2c_client *client, ··· 1033 1178 i2c_set_clientdata(client, smb); 1034 1179 1035 1180 mutex_init(&smb->lock); 1036 - smb->client = client; 1181 + smb->dev = &client->dev; 1037 1182 smb->pdata = pdata; 1183 + 1184 + smb->regmap = devm_regmap_init_i2c(client, &smb347_regmap); 1185 + if (IS_ERR(smb->regmap)) 1186 + return PTR_ERR(smb->regmap); 1038 1187 1039 1188 ret = smb347_hw_init(smb); 1040 1189 if (ret < 0) 1041 1190 return ret; 1042 1191 1043 - smb->mains.name = "smb347-mains"; 1044 - smb->mains.type = POWER_SUPPLY_TYPE_MAINS; 1045 - smb->mains.get_property = smb347_mains_get_property; 1046 - smb->mains.properties = smb347_mains_properties; 1047 - smb->mains.num_properties = ARRAY_SIZE(smb347_mains_properties); 1048 - smb->mains.supplied_to = battery; 1049 - smb->mains.num_supplicants = ARRAY_SIZE(battery); 1192 + if (smb->pdata->use_mains) { 1193 + smb->mains.name = "smb347-mains"; 1194 + smb->mains.type = POWER_SUPPLY_TYPE_MAINS; 1195 + smb->mains.get_property = smb347_mains_get_property; 1196 + smb->mains.properties = smb347_mains_properties; 1197 + smb->mains.num_properties = ARRAY_SIZE(smb347_mains_properties); 1198 + smb->mains.supplied_to = battery; 1199 + smb->mains.num_supplicants = ARRAY_SIZE(battery); 1200 + ret = power_supply_register(dev, &smb->mains); 1201 + if (ret < 0) 1202 + return ret; 1203 + } 1050 1204 1051 - smb->usb.name = "smb347-usb"; 1052 - smb->usb.type = POWER_SUPPLY_TYPE_USB; 1053 - smb->usb.get_property = smb347_usb_get_property; 1054 - smb->usb.properties = smb347_usb_properties; 1055 - smb->usb.num_properties = ARRAY_SIZE(smb347_usb_properties); 1056 - smb->usb.supplied_to = battery; 1057 - smb->usb.num_supplicants = ARRAY_SIZE(battery); 1205 + if (smb->pdata->use_usb) { 1206 + smb->usb.name = "smb347-usb"; 1207 + smb->usb.type = POWER_SUPPLY_TYPE_USB; 1208 + smb->usb.get_property = smb347_usb_get_property; 1209 + smb->usb.properties = smb347_usb_properties; 1210 + smb->usb.num_properties = ARRAY_SIZE(smb347_usb_properties); 1211 + smb->usb.supplied_to = battery; 1212 + smb->usb.num_supplicants = ARRAY_SIZE(battery); 1213 + ret = power_supply_register(dev, &smb->usb); 1214 + if (ret < 0) { 1215 + if (smb->pdata->use_mains) 1216 + power_supply_unregister(&smb->mains); 1217 + return ret; 1218 + } 1219 + } 1058 1220 1059 1221 smb->battery.name = "smb347-battery"; 1060 1222 smb->battery.type = POWER_SUPPLY_TYPE_BATTERY; ··· 1079 1207 smb->battery.properties = smb347_battery_properties; 1080 1208 smb->battery.num_properties = ARRAY_SIZE(smb347_battery_properties); 1081 1209 1082 - ret = power_supply_register(dev, &smb->mains); 1083 - if (ret < 0) 1084 - return ret; 1085 - 1086 - ret = power_supply_register(dev, &smb->usb); 1087 - if (ret < 0) { 1088 - power_supply_unregister(&smb->mains); 1089 - return ret; 1090 - } 1091 1210 1092 1211 ret = power_supply_register(dev, &smb->battery); 1093 1212 if (ret < 0) { 1094 - power_supply_unregister(&smb->usb); 1095 - power_supply_unregister(&smb->mains); 1213 + if (smb->pdata->use_usb) 1214 + power_supply_unregister(&smb->usb); 1215 + if (smb->pdata->use_mains) 1216 + power_supply_unregister(&smb->mains); 1096 1217 return ret; 1097 1218 } 1098 1219 ··· 1094 1229 * interrupt support here. 1095 1230 */ 1096 1231 if (pdata->irq_gpio >= 0) { 1097 - ret = smb347_irq_init(smb); 1232 + ret = smb347_irq_init(smb, client); 1098 1233 if (ret < 0) { 1099 1234 dev_warn(dev, "failed to initialize IRQ: %d\n", ret); 1100 1235 dev_warn(dev, "disabling IRQ support\n"); 1236 + } else { 1237 + smb347_irq_enable(smb); 1101 1238 } 1102 1239 } 1103 1240 1104 - smb->dentry = debugfs_create_file("smb347-regs", S_IRUSR, NULL, smb, 1105 - &smb347_debugfs_fops); 1106 1241 return 0; 1107 1242 } 1108 1243 1109 1244 static int smb347_remove(struct i2c_client *client) 1110 1245 { 1111 1246 struct smb347_charger *smb = i2c_get_clientdata(client); 1112 - 1113 - if (!IS_ERR_OR_NULL(smb->dentry)) 1114 - debugfs_remove(smb->dentry); 1115 1247 1116 1248 if (client->irq) { 1117 1249 smb347_irq_disable(smb); ··· 1117 1255 } 1118 1256 1119 1257 power_supply_unregister(&smb->battery); 1120 - power_supply_unregister(&smb->usb); 1121 - power_supply_unregister(&smb->mains); 1258 + if (smb->pdata->use_usb) 1259 + power_supply_unregister(&smb->usb); 1260 + if (smb->pdata->use_mains) 1261 + power_supply_unregister(&smb->mains); 1122 1262 return 0; 1123 1263 } 1124 1264 ··· 1139 1275 .id_table = smb347_id, 1140 1276 }; 1141 1277 1142 - static int __init smb347_init(void) 1143 - { 1144 - return i2c_add_driver(&smb347_driver); 1145 - } 1146 - module_init(smb347_init); 1147 - 1148 - static void __exit smb347_exit(void) 1149 - { 1150 - i2c_del_driver(&smb347_driver); 1151 - } 1152 - module_exit(smb347_exit); 1278 + module_i2c_driver(smb347_driver); 1153 1279 1154 1280 MODULE_AUTHOR("Bruce E. Robertson <bruce.e.robertson@intel.com>"); 1155 1281 MODULE_AUTHOR("Mika Westerberg <mika.westerberg@linux.intel.com>");
+42 -8
include/linux/power/charger-manager.h
··· 18 18 #include <linux/power_supply.h> 19 19 20 20 enum data_source { 21 + CM_BATTERY_PRESENT, 22 + CM_NO_BATTERY, 21 23 CM_FUEL_GAUGE, 22 24 CM_CHARGER_STAT, 23 25 }; ··· 31 29 CM_POLL_CHARGING_ONLY, 32 30 }; 33 31 32 + enum cm_event_types { 33 + CM_EVENT_UNKNOWN = 0, 34 + CM_EVENT_BATT_FULL, 35 + CM_EVENT_BATT_IN, 36 + CM_EVENT_BATT_OUT, 37 + CM_EVENT_EXT_PWR_IN_OUT, 38 + CM_EVENT_CHG_START_STOP, 39 + CM_EVENT_OTHERS, 40 + }; 41 + 34 42 /** 35 43 * struct charger_global_desc 36 44 * @rtc_name: the name of RTC used to wake up the system from suspend. ··· 50 38 * rtc_only_wakeup() returning false. 51 39 * If the RTC given to CM is the only wakeup reason, 52 40 * rtc_only_wakeup should return true. 41 + * @assume_timer_stops_in_suspend: 42 + * Assume that the jiffy timer stops in suspend-to-RAM. 43 + * When enabled, CM does not rely on jiffies value in 44 + * suspend_again and assumes that jiffies value does not 45 + * change during suspend. 53 46 */ 54 47 struct charger_global_desc { 55 48 char *rtc_name; 56 49 57 50 bool (*rtc_only_wakeup)(void); 51 + 52 + bool assume_timer_stops_in_suspend; 58 53 }; 59 54 60 55 /** ··· 69 50 * @psy_name: the name of power-supply-class for charger manager 70 51 * @polling_mode: 71 52 * Determine which polling mode will be used 53 + * @fullbatt_vchkdrop_ms: 54 + * @fullbatt_vchkdrop_uV: 55 + * Check voltage drop after the battery is fully charged. 56 + * If it has dropped more than fullbatt_vchkdrop_uV after 57 + * fullbatt_vchkdrop_ms, CM will restart charging. 72 58 * @fullbatt_uV: voltage in microvolt 73 59 * If it is not being charged and VBATT >= fullbatt_uV, 74 60 * it is assumed to be full. ··· 100 76 enum polling_modes polling_mode; 101 77 unsigned int polling_interval_ms; 102 78 79 + unsigned int fullbatt_vchkdrop_ms; 80 + unsigned int fullbatt_vchkdrop_uV; 103 81 unsigned int fullbatt_uV; 104 82 105 83 enum data_source battery_present; ··· 127 101 * @fuel_gauge: power_supply for fuel gauge 128 102 * @charger_stat: array of power_supply for chargers 129 103 * @charger_enabled: the state of charger 104 + * @fullbatt_vchk_jiffies_at: 105 + * jiffies at the time full battery check will occur. 106 + * @fullbatt_vchk_uV: voltage in microvolt 107 + * criteria for full battery 108 + * @fullbatt_vchk_work: work queue for full battery check 130 109 * @emergency_stop: 131 110 * When setting true, stop charging 132 111 * @last_temp_mC: the measured temperature in milli-Celsius ··· 152 121 153 122 bool charger_enabled; 154 123 124 + unsigned long fullbatt_vchk_jiffies_at; 125 + unsigned int fullbatt_vchk_uV; 126 + struct delayed_work fullbatt_vchk_work; 127 + 155 128 int emergency_stop; 156 129 int last_temp_mC; 157 130 ··· 169 134 #ifdef CONFIG_CHARGER_MANAGER 170 135 extern int setup_charger_manager(struct charger_global_desc *gd); 171 136 extern bool cm_suspend_again(void); 137 + extern void cm_notify_event(struct power_supply *psy, 138 + enum cm_event_types type, char *msg); 172 139 #else 173 - static void __maybe_unused setup_charger_manager(struct charger_global_desc *gd) 174 - { } 175 - 176 - static bool __maybe_unused cm_suspend_again(void) 177 - { 178 - return false; 179 - } 140 + static inline int setup_charger_manager(struct charger_global_desc *gd) 141 + { return 0; } 142 + static inline bool cm_suspend_again(void) { return false; } 143 + static inline void cm_notify_event(struct power_supply *psy, 144 + enum cm_event_types type, char *msg) { } 180 145 #endif 181 - 182 146 #endif /* _CHARGER_MANAGER_H */
+17
include/linux/power/max17042_battery.h
··· 116 116 MAX17042_VFSOC = 0xFF, 117 117 }; 118 118 119 + /* Registers specific to max17047/50 */ 120 + enum max17047_register { 121 + MAX17047_QRTbl00 = 0x12, 122 + MAX17047_FullSOCThr = 0x13, 123 + MAX17047_QRTbl10 = 0x22, 124 + MAX17047_QRTbl20 = 0x32, 125 + MAX17047_V_empty = 0x3A, 126 + MAX17047_QRTbl30 = 0x42, 127 + }; 128 + 129 + enum max170xx_chip_type {MAX17042, MAX17047}; 130 + 119 131 /* 120 132 * used for setting a register to a desired value 121 133 * addr : address for a register ··· 156 144 u16 shdntimer; /* 0x03F */ 157 145 158 146 /* App data */ 147 + u16 full_soc_thresh; /* 0x13 */ 159 148 u16 design_cap; /* 0x18 */ 160 149 u16 ichgt_term; /* 0x1E */ 161 150 ··· 175 162 u16 lavg_empty; /* 0x36 */ 176 163 u16 dqacc; /* 0x45 */ 177 164 u16 dpacc; /* 0x46 */ 165 + u16 qrtbl00; /* 0x12 */ 166 + u16 qrtbl10; /* 0x22 */ 167 + u16 qrtbl20; /* 0x32 */ 168 + u16 qrtbl30; /* 0x42 */ 178 169 179 170 /* Cell technology from power_supply.h */ 180 171 u16 cell_technology;
+3 -1
include/linux/power_supply.h
··· 96 96 POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN, 97 97 POWER_SUPPLY_PROP_VOLTAGE_NOW, 98 98 POWER_SUPPLY_PROP_VOLTAGE_AVG, 99 + POWER_SUPPLY_PROP_VOLTAGE_OCV, 99 100 POWER_SUPPLY_PROP_CURRENT_MAX, 100 101 POWER_SUPPLY_PROP_CURRENT_NOW, 101 102 POWER_SUPPLY_PROP_CURRENT_AVG, ··· 212 211 extern int power_supply_am_i_supplied(struct power_supply *psy); 213 212 extern int power_supply_set_battery_charged(struct power_supply *psy); 214 213 215 - #if defined(CONFIG_POWER_SUPPLY) || defined(CONFIG_POWER_SUPPLY_MODULE) 214 + #ifdef CONFIG_POWER_SUPPLY 216 215 extern int power_supply_is_system_supplied(void); 217 216 #else 218 217 static inline int power_supply_is_system_supplied(void) { return -ENOSYS; } ··· 262 261 case POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN: 263 262 case POWER_SUPPLY_PROP_VOLTAGE_NOW: 264 263 case POWER_SUPPLY_PROP_VOLTAGE_AVG: 264 + case POWER_SUPPLY_PROP_VOLTAGE_OCV: 265 265 case POWER_SUPPLY_PROP_POWER_NOW: 266 266 return 1; 267 267 default: