13bb3dbbdSDonggeun Kim /* 23bb3dbbdSDonggeun Kim * Copyright (C) 2011 Samsung Electronics Co., Ltd. 33bb3dbbdSDonggeun Kim * MyungJoo.Ham <myungjoo.ham@samsung.com> 43bb3dbbdSDonggeun Kim * 53bb3dbbdSDonggeun Kim * Charger Manager. 63bb3dbbdSDonggeun Kim * This framework enables to control and multiple chargers and to 73bb3dbbdSDonggeun Kim * monitor charging even in the context of suspend-to-RAM with 83bb3dbbdSDonggeun Kim * an interface combining the chargers. 93bb3dbbdSDonggeun Kim * 103bb3dbbdSDonggeun Kim * This program is free software; you can redistribute it and/or modify 113bb3dbbdSDonggeun Kim * it under the terms of the GNU General Public License version 2 as 123bb3dbbdSDonggeun Kim * published by the Free Software Foundation. 133bb3dbbdSDonggeun Kim **/ 143bb3dbbdSDonggeun Kim 153bb3dbbdSDonggeun Kim #ifndef _CHARGER_MANAGER_H 163bb3dbbdSDonggeun Kim #define _CHARGER_MANAGER_H 173bb3dbbdSDonggeun Kim 183bb3dbbdSDonggeun Kim #include <linux/power_supply.h> 19bee737bcSChanwoo Choi #include <linux/extcon.h> 203bb3dbbdSDonggeun Kim 213bb3dbbdSDonggeun Kim enum data_source { 22d829dc75SChanwoo Choi CM_BATTERY_PRESENT, 23d829dc75SChanwoo Choi CM_NO_BATTERY, 243bb3dbbdSDonggeun Kim CM_FUEL_GAUGE, 253bb3dbbdSDonggeun Kim CM_CHARGER_STAT, 263bb3dbbdSDonggeun Kim }; 273bb3dbbdSDonggeun Kim 283bb3dbbdSDonggeun Kim enum polling_modes { 293bb3dbbdSDonggeun Kim CM_POLL_DISABLE = 0, 303bb3dbbdSDonggeun Kim CM_POLL_ALWAYS, 313bb3dbbdSDonggeun Kim CM_POLL_EXTERNAL_POWER_ONLY, 323bb3dbbdSDonggeun Kim CM_POLL_CHARGING_ONLY, 333bb3dbbdSDonggeun Kim }; 343bb3dbbdSDonggeun Kim 35dfeccb12SChanwoo Choi enum cm_event_types { 36dfeccb12SChanwoo Choi CM_EVENT_UNKNOWN = 0, 37dfeccb12SChanwoo Choi CM_EVENT_BATT_FULL, 38dfeccb12SChanwoo Choi CM_EVENT_BATT_IN, 39dfeccb12SChanwoo Choi CM_EVENT_BATT_OUT, 40dfeccb12SChanwoo Choi CM_EVENT_EXT_PWR_IN_OUT, 41dfeccb12SChanwoo Choi CM_EVENT_CHG_START_STOP, 42dfeccb12SChanwoo Choi CM_EVENT_OTHERS, 43dfeccb12SChanwoo Choi }; 44dfeccb12SChanwoo Choi 453bb3dbbdSDonggeun Kim /** 463bb3dbbdSDonggeun Kim * struct charger_global_desc 473bb3dbbdSDonggeun Kim * @rtc_name: the name of RTC used to wake up the system from suspend. 483bb3dbbdSDonggeun Kim * @rtc_only_wakeup: 493bb3dbbdSDonggeun Kim * If the system is woken up by waekup-sources other than the RTC or 503bb3dbbdSDonggeun Kim * callbacks, Charger Manager should recognize with 513bb3dbbdSDonggeun Kim * rtc_only_wakeup() returning false. 523bb3dbbdSDonggeun Kim * If the RTC given to CM is the only wakeup reason, 533bb3dbbdSDonggeun Kim * rtc_only_wakeup should return true. 54d829dc75SChanwoo Choi * @assume_timer_stops_in_suspend: 55d829dc75SChanwoo Choi * Assume that the jiffy timer stops in suspend-to-RAM. 56d829dc75SChanwoo Choi * When enabled, CM does not rely on jiffies value in 57d829dc75SChanwoo Choi * suspend_again and assumes that jiffies value does not 58d829dc75SChanwoo Choi * change during suspend. 593bb3dbbdSDonggeun Kim */ 603bb3dbbdSDonggeun Kim struct charger_global_desc { 613bb3dbbdSDonggeun Kim char *rtc_name; 623bb3dbbdSDonggeun Kim 633bb3dbbdSDonggeun Kim bool (*rtc_only_wakeup)(void); 64d829dc75SChanwoo Choi 65d829dc75SChanwoo Choi bool assume_timer_stops_in_suspend; 663bb3dbbdSDonggeun Kim }; 673bb3dbbdSDonggeun Kim 683bb3dbbdSDonggeun Kim /** 69bee737bcSChanwoo Choi * struct charger_cable 70bee737bcSChanwoo Choi * @extcon_name: the name of extcon device. 71bee737bcSChanwoo Choi * @name: the name of charger cable(external connector). 72bee737bcSChanwoo Choi * @extcon_dev: the extcon device. 73bee737bcSChanwoo Choi * @wq: the workqueue to control charger according to the state of 74bee737bcSChanwoo Choi * charger cable. If charger cable is attached, enable charger. 75bee737bcSChanwoo Choi * But if charger cable is detached, disable charger. 76bee737bcSChanwoo Choi * @nb: the notifier block to receive changed state from EXTCON 77bee737bcSChanwoo Choi * (External Connector) when charger cable is attached/detached. 78bee737bcSChanwoo Choi * @attached: the state of charger cable. 79bee737bcSChanwoo Choi * true: the charger cable is attached 80bee737bcSChanwoo Choi * false: the charger cable is detached 81bee737bcSChanwoo Choi * @charger: the instance of struct charger_regulator. 82bee737bcSChanwoo Choi * @cm: the Charger Manager representing the battery. 83bee737bcSChanwoo Choi */ 84bee737bcSChanwoo Choi struct charger_cable { 85bee737bcSChanwoo Choi const char *extcon_name; 86bee737bcSChanwoo Choi const char *name; 87bee737bcSChanwoo Choi 88bee737bcSChanwoo Choi /* The charger-manager use Exton framework*/ 89bee737bcSChanwoo Choi struct extcon_specific_cable_nb extcon_dev; 90bee737bcSChanwoo Choi struct work_struct wq; 91bee737bcSChanwoo Choi struct notifier_block nb; 92bee737bcSChanwoo Choi 93bee737bcSChanwoo Choi /* The state of charger cable */ 94bee737bcSChanwoo Choi bool attached; 95bee737bcSChanwoo Choi 96bee737bcSChanwoo Choi struct charger_regulator *charger; 9745cd4fb2SChanwoo Choi 9845cd4fb2SChanwoo Choi /* 9945cd4fb2SChanwoo Choi * Set min/max current of regulator to protect over-current issue 10045cd4fb2SChanwoo Choi * according to a kind of charger cable when cable is attached. 10145cd4fb2SChanwoo Choi */ 10245cd4fb2SChanwoo Choi int min_uA; 10345cd4fb2SChanwoo Choi int max_uA; 10445cd4fb2SChanwoo Choi 105bee737bcSChanwoo Choi struct charger_manager *cm; 106bee737bcSChanwoo Choi }; 107bee737bcSChanwoo Choi 108bee737bcSChanwoo Choi /** 109bee737bcSChanwoo Choi * struct charger_regulator 110bee737bcSChanwoo Choi * @regulator_name: the name of regulator for using charger. 111bee737bcSChanwoo Choi * @consumer: the regulator consumer for the charger. 112bee737bcSChanwoo Choi * @cables: 113bee737bcSChanwoo Choi * the array of charger cables to enable/disable charger 114bee737bcSChanwoo Choi * and set current limit according to constratint data of 115bee737bcSChanwoo Choi * struct charger_cable if only charger cable included 116bee737bcSChanwoo Choi * in the array of charger cables is attached/detached. 117bee737bcSChanwoo Choi * @num_cables: the number of charger cables. 118bee737bcSChanwoo Choi */ 119bee737bcSChanwoo Choi struct charger_regulator { 120bee737bcSChanwoo Choi /* The name of regulator for charging */ 121bee737bcSChanwoo Choi const char *regulator_name; 122bee737bcSChanwoo Choi struct regulator *consumer; 123bee737bcSChanwoo Choi 124bee737bcSChanwoo Choi /* 125bee737bcSChanwoo Choi * Store constraint information related to current limit, 126bee737bcSChanwoo Choi * each cable have different condition for charging. 127bee737bcSChanwoo Choi */ 128bee737bcSChanwoo Choi struct charger_cable *cables; 129bee737bcSChanwoo Choi int num_cables; 130bee737bcSChanwoo Choi }; 131bee737bcSChanwoo Choi 132bee737bcSChanwoo Choi /** 1333bb3dbbdSDonggeun Kim * struct charger_desc 134ad3d13eeSDonggeun Kim * @psy_name: the name of power-supply-class for charger manager 1353bb3dbbdSDonggeun Kim * @polling_mode: 1363bb3dbbdSDonggeun Kim * Determine which polling mode will be used 137d829dc75SChanwoo Choi * @fullbatt_vchkdrop_ms: 138d829dc75SChanwoo Choi * @fullbatt_vchkdrop_uV: 139d829dc75SChanwoo Choi * Check voltage drop after the battery is fully charged. 140d829dc75SChanwoo Choi * If it has dropped more than fullbatt_vchkdrop_uV after 141d829dc75SChanwoo Choi * fullbatt_vchkdrop_ms, CM will restart charging. 142ad3d13eeSDonggeun Kim * @fullbatt_uV: voltage in microvolt 1432ed9e9b6SChanwoo Choi * If VBATT >= fullbatt_uV, it is assumed to be full. 1442ed9e9b6SChanwoo Choi * @fullbatt_soc: state of Charge in % 1452ed9e9b6SChanwoo Choi * If state of Charge >= fullbatt_soc, it is assumed to be full. 1462ed9e9b6SChanwoo Choi * @fullbatt_full_capacity: full capacity measure 1472ed9e9b6SChanwoo Choi * If full capacity of battery >= fullbatt_full_capacity, 148ad3d13eeSDonggeun Kim * it is assumed to be full. 1493bb3dbbdSDonggeun Kim * @polling_interval_ms: interval in millisecond at which 1503bb3dbbdSDonggeun Kim * charger manager will monitor battery health 1513bb3dbbdSDonggeun Kim * @battery_present: 1523bb3dbbdSDonggeun Kim * Specify where information for existance of battery can be obtained 1533bb3dbbdSDonggeun Kim * @psy_charger_stat: the names of power-supply for chargers 1543bb3dbbdSDonggeun Kim * @num_charger_regulator: the number of entries in charger_regulators 155c6b2744cSAnton Vorontsov * @charger_regulators: array of charger regulators 1563bb3dbbdSDonggeun Kim * @psy_fuel_gauge: the name of power-supply for fuel gauge 1573bb3dbbdSDonggeun Kim * @temperature_out_of_range: 1583bb3dbbdSDonggeun Kim * Determine whether the status is overheat or cold or normal. 1593bb3dbbdSDonggeun Kim * return_value > 0: overheat 1603bb3dbbdSDonggeun Kim * return_value == 0: normal 1613bb3dbbdSDonggeun Kim * return_value < 0: cold 162ad3d13eeSDonggeun Kim * @measure_battery_temp: 163ad3d13eeSDonggeun Kim * true: measure battery temperature 164ad3d13eeSDonggeun Kim * false: measure ambient temperature 165*8fcfe088SChanwoo Choi * @charging_max_duration_ms: Maximum possible duration for charging 166*8fcfe088SChanwoo Choi * If whole charging duration exceed 'charging_max_duration_ms', 167*8fcfe088SChanwoo Choi * cm stop charging. 168*8fcfe088SChanwoo Choi * @discharging_max_duration_ms: 169*8fcfe088SChanwoo Choi * Maximum possible duration for discharging with charger cable 170*8fcfe088SChanwoo Choi * after full-batt. If discharging duration exceed 'discharging 171*8fcfe088SChanwoo Choi * max_duration_ms', cm start charging. 1723bb3dbbdSDonggeun Kim */ 1733bb3dbbdSDonggeun Kim struct charger_desc { 174ad3d13eeSDonggeun Kim char *psy_name; 175ad3d13eeSDonggeun Kim 1763bb3dbbdSDonggeun Kim enum polling_modes polling_mode; 1773bb3dbbdSDonggeun Kim unsigned int polling_interval_ms; 1783bb3dbbdSDonggeun Kim 179d829dc75SChanwoo Choi unsigned int fullbatt_vchkdrop_ms; 180d829dc75SChanwoo Choi unsigned int fullbatt_vchkdrop_uV; 181ad3d13eeSDonggeun Kim unsigned int fullbatt_uV; 1822ed9e9b6SChanwoo Choi unsigned int fullbatt_soc; 1832ed9e9b6SChanwoo Choi unsigned int fullbatt_full_capacity; 184ad3d13eeSDonggeun Kim 1853bb3dbbdSDonggeun Kim enum data_source battery_present; 1863bb3dbbdSDonggeun Kim 1873bb3dbbdSDonggeun Kim char **psy_charger_stat; 1883bb3dbbdSDonggeun Kim 1893bb3dbbdSDonggeun Kim int num_charger_regulators; 190bee737bcSChanwoo Choi struct charger_regulator *charger_regulators; 1913bb3dbbdSDonggeun Kim 1923bb3dbbdSDonggeun Kim char *psy_fuel_gauge; 1933bb3dbbdSDonggeun Kim 1943bb3dbbdSDonggeun Kim int (*temperature_out_of_range)(int *mC); 195ad3d13eeSDonggeun Kim bool measure_battery_temp; 196*8fcfe088SChanwoo Choi 197*8fcfe088SChanwoo Choi u64 charging_max_duration_ms; 198*8fcfe088SChanwoo Choi u64 discharging_max_duration_ms; 1993bb3dbbdSDonggeun Kim }; 2003bb3dbbdSDonggeun Kim 2013bb3dbbdSDonggeun Kim #define PSY_NAME_MAX 30 2023bb3dbbdSDonggeun Kim 2033bb3dbbdSDonggeun Kim /** 2043bb3dbbdSDonggeun Kim * struct charger_manager 2053bb3dbbdSDonggeun Kim * @entry: entry for list 2063bb3dbbdSDonggeun Kim * @dev: device pointer 2073bb3dbbdSDonggeun Kim * @desc: instance of charger_desc 2083bb3dbbdSDonggeun Kim * @fuel_gauge: power_supply for fuel gauge 2093bb3dbbdSDonggeun Kim * @charger_stat: array of power_supply for chargers 2103bb3dbbdSDonggeun Kim * @charger_enabled: the state of charger 211d829dc75SChanwoo Choi * @fullbatt_vchk_jiffies_at: 212d829dc75SChanwoo Choi * jiffies at the time full battery check will occur. 213d829dc75SChanwoo Choi * @fullbatt_vchk_work: work queue for full battery check 2143bb3dbbdSDonggeun Kim * @emergency_stop: 2153bb3dbbdSDonggeun Kim * When setting true, stop charging 2163bb3dbbdSDonggeun Kim * @last_temp_mC: the measured temperature in milli-Celsius 217ad3d13eeSDonggeun Kim * @psy_name_buf: the name of power-supply-class for charger manager 218ad3d13eeSDonggeun Kim * @charger_psy: power_supply for charger manager 2193bb3dbbdSDonggeun Kim * @status_save_ext_pwr_inserted: 2203bb3dbbdSDonggeun Kim * saved status of external power before entering suspend-to-RAM 2213bb3dbbdSDonggeun Kim * @status_save_batt: 2223bb3dbbdSDonggeun Kim * saved status of battery before entering suspend-to-RAM 223*8fcfe088SChanwoo Choi * @charging_start_time: saved start time of enabling charging 224*8fcfe088SChanwoo Choi * @charging_end_time: saved end time of disabling charging 2253bb3dbbdSDonggeun Kim */ 2263bb3dbbdSDonggeun Kim struct charger_manager { 2273bb3dbbdSDonggeun Kim struct list_head entry; 2283bb3dbbdSDonggeun Kim struct device *dev; 2293bb3dbbdSDonggeun Kim struct charger_desc *desc; 2303bb3dbbdSDonggeun Kim 2313bb3dbbdSDonggeun Kim struct power_supply *fuel_gauge; 2323bb3dbbdSDonggeun Kim struct power_supply **charger_stat; 2333bb3dbbdSDonggeun Kim 2343bb3dbbdSDonggeun Kim bool charger_enabled; 2353bb3dbbdSDonggeun Kim 236d829dc75SChanwoo Choi unsigned long fullbatt_vchk_jiffies_at; 237d829dc75SChanwoo Choi struct delayed_work fullbatt_vchk_work; 238d829dc75SChanwoo Choi 2393bb3dbbdSDonggeun Kim int emergency_stop; 2403bb3dbbdSDonggeun Kim int last_temp_mC; 2413bb3dbbdSDonggeun Kim 242ad3d13eeSDonggeun Kim char psy_name_buf[PSY_NAME_MAX + 1]; 243ad3d13eeSDonggeun Kim struct power_supply charger_psy; 244ad3d13eeSDonggeun Kim 2453bb3dbbdSDonggeun Kim bool status_save_ext_pwr_inserted; 2463bb3dbbdSDonggeun Kim bool status_save_batt; 247*8fcfe088SChanwoo Choi 248*8fcfe088SChanwoo Choi u64 charging_start_time; 249*8fcfe088SChanwoo Choi u64 charging_end_time; 2503bb3dbbdSDonggeun Kim }; 2513bb3dbbdSDonggeun Kim 2523bb3dbbdSDonggeun Kim #ifdef CONFIG_CHARGER_MANAGER 2533bb3dbbdSDonggeun Kim extern int setup_charger_manager(struct charger_global_desc *gd); 2543bb3dbbdSDonggeun Kim extern bool cm_suspend_again(void); 255dfeccb12SChanwoo Choi extern void cm_notify_event(struct power_supply *psy, 256dfeccb12SChanwoo Choi enum cm_event_types type, char *msg); 2573bb3dbbdSDonggeun Kim #else 258dfeccb12SChanwoo Choi static inline int setup_charger_manager(struct charger_global_desc *gd) 259dfeccb12SChanwoo Choi { return 0; } 260dfeccb12SChanwoo Choi static inline bool cm_suspend_again(void) { return false; } 261dfeccb12SChanwoo Choi static inline void cm_notify_event(struct power_supply *psy, 262dfeccb12SChanwoo Choi enum cm_event_types type, char *msg) { } 2633bb3dbbdSDonggeun Kim #endif 2643bb3dbbdSDonggeun Kim #endif /* _CHARGER_MANAGER_H */ 265