1*4882a593Smuzhiyun=============== 2*4882a593SmuzhiyunCharger Manager 3*4882a593Smuzhiyun=============== 4*4882a593Smuzhiyun 5*4882a593Smuzhiyun (C) 2011 MyungJoo Ham <myungjoo.ham@samsung.com>, GPL 6*4882a593Smuzhiyun 7*4882a593SmuzhiyunCharger Manager provides in-kernel battery charger management that 8*4882a593Smuzhiyunrequires temperature monitoring during suspend-to-RAM state 9*4882a593Smuzhiyunand where each battery may have multiple chargers attached and the userland 10*4882a593Smuzhiyunwants to look at the aggregated information of the multiple chargers. 11*4882a593Smuzhiyun 12*4882a593SmuzhiyunCharger Manager is a platform_driver with power-supply-class entries. 13*4882a593SmuzhiyunAn instance of Charger Manager (a platform-device created with Charger-Manager) 14*4882a593Smuzhiyunrepresents an independent battery with chargers. If there are multiple 15*4882a593Smuzhiyunbatteries with their own chargers acting independently in a system, 16*4882a593Smuzhiyunthe system may need multiple instances of Charger Manager. 17*4882a593Smuzhiyun 18*4882a593Smuzhiyun1. Introduction 19*4882a593Smuzhiyun=============== 20*4882a593Smuzhiyun 21*4882a593SmuzhiyunCharger Manager supports the following: 22*4882a593Smuzhiyun 23*4882a593Smuzhiyun* Support for multiple chargers (e.g., a device with USB, AC, and solar panels) 24*4882a593Smuzhiyun A system may have multiple chargers (or power sources) and some of 25*4882a593Smuzhiyun they may be activated at the same time. Each charger may have its 26*4882a593Smuzhiyun own power-supply-class and each power-supply-class can provide 27*4882a593Smuzhiyun different information about the battery status. This framework 28*4882a593Smuzhiyun aggregates charger-related information from multiple sources and 29*4882a593Smuzhiyun shows combined information as a single power-supply-class. 30*4882a593Smuzhiyun 31*4882a593Smuzhiyun* Support for in suspend-to-RAM polling (with suspend_again callback) 32*4882a593Smuzhiyun While the battery is being charged and the system is in suspend-to-RAM, 33*4882a593Smuzhiyun we may need to monitor the battery health by looking at the ambient or 34*4882a593Smuzhiyun battery temperature. We can accomplish this by waking up the system 35*4882a593Smuzhiyun periodically. However, such a method wakes up devices unnecessarily for 36*4882a593Smuzhiyun monitoring the battery health and tasks, and user processes that are 37*4882a593Smuzhiyun supposed to be kept suspended. That, in turn, incurs unnecessary power 38*4882a593Smuzhiyun consumption and slow down charging process. Or even, such peak power 39*4882a593Smuzhiyun consumption can stop chargers in the middle of charging 40*4882a593Smuzhiyun (external power input < device power consumption), which not 41*4882a593Smuzhiyun only affects the charging time, but the lifespan of the battery. 42*4882a593Smuzhiyun 43*4882a593Smuzhiyun Charger Manager provides a function "cm_suspend_again" that can be 44*4882a593Smuzhiyun used as suspend_again callback of platform_suspend_ops. If the platform 45*4882a593Smuzhiyun requires tasks other than cm_suspend_again, it may implement its own 46*4882a593Smuzhiyun suspend_again callback that calls cm_suspend_again in the middle. 47*4882a593Smuzhiyun Normally, the platform will need to resume and suspend some devices 48*4882a593Smuzhiyun that are used by Charger Manager. 49*4882a593Smuzhiyun 50*4882a593Smuzhiyun* Support for premature full-battery event handling 51*4882a593Smuzhiyun If the battery voltage drops by "fullbatt_vchkdrop_uV" after 52*4882a593Smuzhiyun "fullbatt_vchkdrop_ms" from the full-battery event, the framework 53*4882a593Smuzhiyun restarts charging. This check is also performed while suspended by 54*4882a593Smuzhiyun setting wakeup time accordingly and using suspend_again. 55*4882a593Smuzhiyun 56*4882a593Smuzhiyun* Support for uevent-notify 57*4882a593Smuzhiyun With the charger-related events, the device sends 58*4882a593Smuzhiyun notification to users with UEVENT. 59*4882a593Smuzhiyun 60*4882a593Smuzhiyun2. Global Charger-Manager Data related with suspend_again 61*4882a593Smuzhiyun========================================================= 62*4882a593SmuzhiyunIn order to setup Charger Manager with suspend-again feature 63*4882a593Smuzhiyun(in-suspend monitoring), the user should provide charger_global_desc 64*4882a593Smuzhiyunwith setup_charger_manager(`struct charger_global_desc *`). 65*4882a593SmuzhiyunThis charger_global_desc data for in-suspend monitoring is global 66*4882a593Smuzhiyunas the name suggests. Thus, the user needs to provide only once even 67*4882a593Smuzhiyunif there are multiple batteries. If there are multiple batteries, the 68*4882a593Smuzhiyunmultiple instances of Charger Manager share the same charger_global_desc 69*4882a593Smuzhiyunand it will manage in-suspend monitoring for all instances of Charger Manager. 70*4882a593Smuzhiyun 71*4882a593SmuzhiyunThe user needs to provide all the three entries to `struct charger_global_desc` 72*4882a593Smuzhiyunproperly in order to activate in-suspend monitoring: 73*4882a593Smuzhiyun 74*4882a593Smuzhiyun`char *rtc_name;` 75*4882a593Smuzhiyun The name of rtc (e.g., "rtc0") used to wakeup the system from 76*4882a593Smuzhiyun suspend for Charger Manager. The alarm interrupt (AIE) of the rtc 77*4882a593Smuzhiyun should be able to wake up the system from suspend. Charger Manager 78*4882a593Smuzhiyun saves and restores the alarm value and use the previously-defined 79*4882a593Smuzhiyun alarm if it is going to go off earlier than Charger Manager so that 80*4882a593Smuzhiyun Charger Manager does not interfere with previously-defined alarms. 81*4882a593Smuzhiyun 82*4882a593Smuzhiyun`bool (*rtc_only_wakeup)(void);` 83*4882a593Smuzhiyun This callback should let CM know whether 84*4882a593Smuzhiyun the wakeup-from-suspend is caused only by the alarm of "rtc" in the 85*4882a593Smuzhiyun same struct. If there is any other wakeup source triggered the 86*4882a593Smuzhiyun wakeup, it should return false. If the "rtc" is the only wakeup 87*4882a593Smuzhiyun reason, it should return true. 88*4882a593Smuzhiyun 89*4882a593Smuzhiyun`bool assume_timer_stops_in_suspend;` 90*4882a593Smuzhiyun if true, Charger Manager assumes that 91*4882a593Smuzhiyun the timer (CM uses jiffies as timer) stops during suspend. Then, CM 92*4882a593Smuzhiyun assumes that the suspend-duration is same as the alarm length. 93*4882a593Smuzhiyun 94*4882a593Smuzhiyun 95*4882a593Smuzhiyun3. How to setup suspend_again 96*4882a593Smuzhiyun============================= 97*4882a593SmuzhiyunCharger Manager provides a function "extern bool cm_suspend_again(void)". 98*4882a593SmuzhiyunWhen cm_suspend_again is called, it monitors every battery. The suspend_ops 99*4882a593Smuzhiyuncallback of the system's platform_suspend_ops can call cm_suspend_again 100*4882a593Smuzhiyunfunction to know whether Charger Manager wants to suspend again or not. 101*4882a593SmuzhiyunIf there are no other devices or tasks that want to use suspend_again 102*4882a593Smuzhiyunfeature, the platform_suspend_ops may directly refer to cm_suspend_again 103*4882a593Smuzhiyunfor its suspend_again callback. 104*4882a593Smuzhiyun 105*4882a593SmuzhiyunThe cm_suspend_again() returns true (meaning "I want to suspend again") 106*4882a593Smuzhiyunif the system was woken up by Charger Manager and the polling 107*4882a593Smuzhiyun(in-suspend monitoring) results in "normal". 108*4882a593Smuzhiyun 109*4882a593Smuzhiyun4. Charger-Manager Data (struct charger_desc) 110*4882a593Smuzhiyun============================================= 111*4882a593SmuzhiyunFor each battery charged independently from other batteries (if a series of 112*4882a593Smuzhiyunbatteries are charged by a single charger, they are counted as one independent 113*4882a593Smuzhiyunbattery), an instance of Charger Manager is attached to it. The following 114*4882a593Smuzhiyun 115*4882a593Smuzhiyunstruct charger_desc elements: 116*4882a593Smuzhiyun 117*4882a593Smuzhiyun`char *psy_name;` 118*4882a593Smuzhiyun The power-supply-class name of the battery. Default is 119*4882a593Smuzhiyun "battery" if psy_name is NULL. Users can access the psy entries 120*4882a593Smuzhiyun at "/sys/class/power_supply/[psy_name]/". 121*4882a593Smuzhiyun 122*4882a593Smuzhiyun`enum polling_modes polling_mode;` 123*4882a593Smuzhiyun CM_POLL_DISABLE: 124*4882a593Smuzhiyun do not poll this battery. 125*4882a593Smuzhiyun CM_POLL_ALWAYS: 126*4882a593Smuzhiyun always poll this battery. 127*4882a593Smuzhiyun CM_POLL_EXTERNAL_POWER_ONLY: 128*4882a593Smuzhiyun poll this battery if and only if an external power 129*4882a593Smuzhiyun source is attached. 130*4882a593Smuzhiyun CM_POLL_CHARGING_ONLY: 131*4882a593Smuzhiyun poll this battery if and only if the battery is being charged. 132*4882a593Smuzhiyun 133*4882a593Smuzhiyun`unsigned int fullbatt_vchkdrop_ms; / unsigned int fullbatt_vchkdrop_uV;` 134*4882a593Smuzhiyun If both have non-zero values, Charger Manager will check the 135*4882a593Smuzhiyun battery voltage drop fullbatt_vchkdrop_ms after the battery is fully 136*4882a593Smuzhiyun charged. If the voltage drop is over fullbatt_vchkdrop_uV, Charger 137*4882a593Smuzhiyun Manager will try to recharge the battery by disabling and enabling 138*4882a593Smuzhiyun chargers. Recharge with voltage drop condition only (without delay 139*4882a593Smuzhiyun condition) is needed to be implemented with hardware interrupts from 140*4882a593Smuzhiyun fuel gauges or charger devices/chips. 141*4882a593Smuzhiyun 142*4882a593Smuzhiyun`unsigned int fullbatt_uV;` 143*4882a593Smuzhiyun If specified with a non-zero value, Charger Manager assumes 144*4882a593Smuzhiyun that the battery is full (capacity = 100) if the battery is not being 145*4882a593Smuzhiyun charged and the battery voltage is equal to or greater than 146*4882a593Smuzhiyun fullbatt_uV. 147*4882a593Smuzhiyun 148*4882a593Smuzhiyun`unsigned int polling_interval_ms;` 149*4882a593Smuzhiyun Required polling interval in ms. Charger Manager will poll 150*4882a593Smuzhiyun this battery every polling_interval_ms or more frequently. 151*4882a593Smuzhiyun 152*4882a593Smuzhiyun`enum data_source battery_present;` 153*4882a593Smuzhiyun CM_BATTERY_PRESENT: 154*4882a593Smuzhiyun assume that the battery exists. 155*4882a593Smuzhiyun CM_NO_BATTERY: 156*4882a593Smuzhiyun assume that the battery does not exists. 157*4882a593Smuzhiyun CM_FUEL_GAUGE: 158*4882a593Smuzhiyun get battery presence information from fuel gauge. 159*4882a593Smuzhiyun CM_CHARGER_STAT: 160*4882a593Smuzhiyun get battery presence from chargers. 161*4882a593Smuzhiyun 162*4882a593Smuzhiyun`char **psy_charger_stat;` 163*4882a593Smuzhiyun An array ending with NULL that has power-supply-class names of 164*4882a593Smuzhiyun chargers. Each power-supply-class should provide "PRESENT" (if 165*4882a593Smuzhiyun battery_present is "CM_CHARGER_STAT"), "ONLINE" (shows whether an 166*4882a593Smuzhiyun external power source is attached or not), and "STATUS" (shows whether 167*4882a593Smuzhiyun the battery is {"FULL" or not FULL} or {"FULL", "Charging", 168*4882a593Smuzhiyun "Discharging", "NotCharging"}). 169*4882a593Smuzhiyun 170*4882a593Smuzhiyun`int num_charger_regulators; / struct regulator_bulk_data *charger_regulators;` 171*4882a593Smuzhiyun Regulators representing the chargers in the form for 172*4882a593Smuzhiyun regulator framework's bulk functions. 173*4882a593Smuzhiyun 174*4882a593Smuzhiyun`char *psy_fuel_gauge;` 175*4882a593Smuzhiyun Power-supply-class name of the fuel gauge. 176*4882a593Smuzhiyun 177*4882a593Smuzhiyun`int (*temperature_out_of_range)(int *mC); / bool measure_battery_temp;` 178*4882a593Smuzhiyun This callback returns 0 if the temperature is safe for charging, 179*4882a593Smuzhiyun a positive number if it is too hot to charge, and a negative number 180*4882a593Smuzhiyun if it is too cold to charge. With the variable mC, the callback returns 181*4882a593Smuzhiyun the temperature in 1/1000 of centigrade. 182*4882a593Smuzhiyun The source of temperature can be battery or ambient one according to 183*4882a593Smuzhiyun the value of measure_battery_temp. 184*4882a593Smuzhiyun 185*4882a593Smuzhiyun 186*4882a593Smuzhiyun5. Notify Charger-Manager of charger events: cm_notify_event() 187*4882a593Smuzhiyun============================================================== 188*4882a593SmuzhiyunIf there is an charger event is required to notify 189*4882a593SmuzhiyunCharger Manager, a charger device driver that triggers the event can call 190*4882a593Smuzhiyuncm_notify_event(psy, type, msg) to notify the corresponding Charger Manager. 191*4882a593SmuzhiyunIn the function, psy is the charger driver's power_supply pointer, which is 192*4882a593Smuzhiyunassociated with Charger-Manager. The parameter "type" 193*4882a593Smuzhiyunis the same as irq's type (enum cm_event_types). The event message "msg" is 194*4882a593Smuzhiyunoptional and is effective only if the event type is "UNDESCRIBED" or "OTHERS". 195*4882a593Smuzhiyun 196*4882a593Smuzhiyun6. Other Considerations 197*4882a593Smuzhiyun======================= 198*4882a593Smuzhiyun 199*4882a593SmuzhiyunAt the charger/battery-related events such as battery-pulled-out, 200*4882a593Smuzhiyuncharger-pulled-out, charger-inserted, DCIN-over/under-voltage, charger-stopped, 201*4882a593Smuzhiyunand others critical to chargers, the system should be configured to wake up. 202*4882a593SmuzhiyunAt least the following should wake up the system from a suspend: 203*4882a593Smuzhiyuna) charger-on/off b) external-power-in/out c) battery-in/out (while charging) 204*4882a593Smuzhiyun 205*4882a593SmuzhiyunIt is usually accomplished by configuring the PMIC as a wakeup source. 206