blob: 07e7945a1ff292048da586ac9709d17691a29fca [file] [log] [blame]
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +09001/*
2 * Copyright (C) 2011 Samsung Electronics Co., Ltd.
3 * MyungJoo.Ham <myungjoo.ham@samsung.com>
4 *
5 * Charger Manager.
6 * This framework enables to control and multiple chargers and to
7 * monitor charging even in the context of suspend-to-RAM with
8 * an interface combining the chargers.
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License version 2 as
12 * published by the Free Software Foundation.
13**/
14
15#ifndef _CHARGER_MANAGER_H
16#define _CHARGER_MANAGER_H
17
18#include <linux/power_supply.h>
Chanwoo Choibee737b2012-07-12 15:03:25 +090019#include <linux/extcon.h>
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +090020
21enum data_source {
Chanwoo Choid829dc72012-05-05 06:24:10 -070022 CM_BATTERY_PRESENT,
23 CM_NO_BATTERY,
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +090024 CM_FUEL_GAUGE,
25 CM_CHARGER_STAT,
26};
27
28enum polling_modes {
29 CM_POLL_DISABLE = 0,
30 CM_POLL_ALWAYS,
31 CM_POLL_EXTERNAL_POWER_ONLY,
32 CM_POLL_CHARGING_ONLY,
33};
34
Chanwoo Choidfeccb12012-05-05 06:26:47 -070035enum cm_event_types {
36 CM_EVENT_UNKNOWN = 0,
37 CM_EVENT_BATT_FULL,
38 CM_EVENT_BATT_IN,
39 CM_EVENT_BATT_OUT,
Jonghwa Lee5c49a622013-12-18 15:42:34 +090040 CM_EVENT_BATT_OVERHEAT,
41 CM_EVENT_BATT_COLD,
Chanwoo Choidfeccb12012-05-05 06:26:47 -070042 CM_EVENT_EXT_PWR_IN_OUT,
43 CM_EVENT_CHG_START_STOP,
44 CM_EVENT_OTHERS,
45};
46
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +090047/**
48 * struct charger_global_desc
49 * @rtc_name: the name of RTC used to wake up the system from suspend.
50 * @rtc_only_wakeup:
51 * If the system is woken up by waekup-sources other than the RTC or
52 * callbacks, Charger Manager should recognize with
53 * rtc_only_wakeup() returning false.
54 * If the RTC given to CM is the only wakeup reason,
55 * rtc_only_wakeup should return true.
Chanwoo Choid829dc72012-05-05 06:24:10 -070056 * @assume_timer_stops_in_suspend:
57 * Assume that the jiffy timer stops in suspend-to-RAM.
58 * When enabled, CM does not rely on jiffies value in
59 * suspend_again and assumes that jiffies value does not
60 * change during suspend.
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +090061 */
62struct charger_global_desc {
63 char *rtc_name;
64
65 bool (*rtc_only_wakeup)(void);
Chanwoo Choid829dc72012-05-05 06:24:10 -070066
67 bool assume_timer_stops_in_suspend;
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +090068};
69
70/**
Chanwoo Choibee737b2012-07-12 15:03:25 +090071 * struct charger_cable
72 * @extcon_name: the name of extcon device.
73 * @name: the name of charger cable(external connector).
74 * @extcon_dev: the extcon device.
75 * @wq: the workqueue to control charger according to the state of
76 * charger cable. If charger cable is attached, enable charger.
77 * But if charger cable is detached, disable charger.
78 * @nb: the notifier block to receive changed state from EXTCON
79 * (External Connector) when charger cable is attached/detached.
80 * @attached: the state of charger cable.
81 * true: the charger cable is attached
82 * false: the charger cable is detached
83 * @charger: the instance of struct charger_regulator.
84 * @cm: the Charger Manager representing the battery.
85 */
86struct charger_cable {
87 const char *extcon_name;
88 const char *name;
89
90 /* The charger-manager use Exton framework*/
91 struct extcon_specific_cable_nb extcon_dev;
92 struct work_struct wq;
93 struct notifier_block nb;
94
95 /* The state of charger cable */
96 bool attached;
97
98 struct charger_regulator *charger;
Chanwoo Choi45cd4fb2012-07-12 15:03:29 +090099
100 /*
101 * Set min/max current of regulator to protect over-current issue
102 * according to a kind of charger cable when cable is attached.
103 */
104 int min_uA;
105 int max_uA;
106
Chanwoo Choibee737b2012-07-12 15:03:25 +0900107 struct charger_manager *cm;
108};
109
110/**
111 * struct charger_regulator
112 * @regulator_name: the name of regulator for using charger.
113 * @consumer: the regulator consumer for the charger.
Chanwoo Choi3950c782012-09-21 18:49:37 +0900114 * @externally_control:
115 * Set if the charger-manager cannot control charger,
116 * the charger will be maintained with disabled state.
Chanwoo Choibee737b2012-07-12 15:03:25 +0900117 * @cables:
118 * the array of charger cables to enable/disable charger
119 * and set current limit according to constratint data of
120 * struct charger_cable if only charger cable included
121 * in the array of charger cables is attached/detached.
122 * @num_cables: the number of charger cables.
Chanwoo Choi3950c782012-09-21 18:49:37 +0900123 * @attr_g: Attribute group for the charger(regulator)
124 * @attr_name: "name" sysfs entry
125 * @attr_state: "state" sysfs entry
126 * @attr_externally_control: "externally_control" sysfs entry
127 * @attrs: Arrays pointing to attr_name/state/externally_control for attr_g
Chanwoo Choibee737b2012-07-12 15:03:25 +0900128 */
129struct charger_regulator {
130 /* The name of regulator for charging */
131 const char *regulator_name;
132 struct regulator *consumer;
133
Chanwoo Choi3950c782012-09-21 18:49:37 +0900134 /* charger never on when system is on */
135 int externally_control;
136
Chanwoo Choibee737b2012-07-12 15:03:25 +0900137 /*
138 * Store constraint information related to current limit,
139 * each cable have different condition for charging.
140 */
141 struct charger_cable *cables;
142 int num_cables;
Chanwoo Choi3950c782012-09-21 18:49:37 +0900143
144 struct attribute_group attr_g;
145 struct device_attribute attr_name;
146 struct device_attribute attr_state;
147 struct device_attribute attr_externally_control;
148 struct attribute *attrs[4];
149
150 struct charger_manager *cm;
Chanwoo Choibee737b2012-07-12 15:03:25 +0900151};
152
153/**
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +0900154 * struct charger_desc
Donggeun Kimad3d13ee2011-12-27 18:47:49 +0900155 * @psy_name: the name of power-supply-class for charger manager
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +0900156 * @polling_mode:
157 * Determine which polling mode will be used
Chanwoo Choid829dc72012-05-05 06:24:10 -0700158 * @fullbatt_vchkdrop_ms:
159 * @fullbatt_vchkdrop_uV:
160 * Check voltage drop after the battery is fully charged.
161 * If it has dropped more than fullbatt_vchkdrop_uV after
162 * fullbatt_vchkdrop_ms, CM will restart charging.
Donggeun Kimad3d13ee2011-12-27 18:47:49 +0900163 * @fullbatt_uV: voltage in microvolt
Chanwoo Choi2ed9e9b2012-08-21 17:06:52 +0900164 * If VBATT >= fullbatt_uV, it is assumed to be full.
165 * @fullbatt_soc: state of Charge in %
166 * If state of Charge >= fullbatt_soc, it is assumed to be full.
167 * @fullbatt_full_capacity: full capacity measure
168 * If full capacity of battery >= fullbatt_full_capacity,
Donggeun Kimad3d13ee2011-12-27 18:47:49 +0900169 * it is assumed to be full.
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +0900170 * @polling_interval_ms: interval in millisecond at which
171 * charger manager will monitor battery health
172 * @battery_present:
173 * Specify where information for existance of battery can be obtained
174 * @psy_charger_stat: the names of power-supply for chargers
175 * @num_charger_regulator: the number of entries in charger_regulators
Anton Vorontsovc6b27442012-08-22 21:36:05 -0700176 * @charger_regulators: array of charger regulators
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +0900177 * @psy_fuel_gauge: the name of power-supply for fuel gauge
Jonghwa Lee5c49a622013-12-18 15:42:34 +0900178 * @thermal_zone : the name of thermal zone for battery
179 * @temp_min : Minimum battery temperature for charging.
180 * @temp_max : Maximum battery temperature for charging.
181 * @temp_diff : Temperature diffential to restart charging.
Donggeun Kimad3d13ee2011-12-27 18:47:49 +0900182 * @measure_battery_temp:
183 * true: measure battery temperature
184 * false: measure ambient temperature
Chanwoo Choi8fcfe082012-09-20 21:20:05 -0700185 * @charging_max_duration_ms: Maximum possible duration for charging
186 * If whole charging duration exceed 'charging_max_duration_ms',
187 * cm stop charging.
188 * @discharging_max_duration_ms:
189 * Maximum possible duration for discharging with charger cable
190 * after full-batt. If discharging duration exceed 'discharging
191 * max_duration_ms', cm start charging.
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +0900192 */
193struct charger_desc {
Jonghwa Lee856ee612013-12-18 15:42:35 +0900194 const char *psy_name;
Donggeun Kimad3d13ee2011-12-27 18:47:49 +0900195
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +0900196 enum polling_modes polling_mode;
197 unsigned int polling_interval_ms;
198
Chanwoo Choid829dc72012-05-05 06:24:10 -0700199 unsigned int fullbatt_vchkdrop_ms;
200 unsigned int fullbatt_vchkdrop_uV;
Donggeun Kimad3d13ee2011-12-27 18:47:49 +0900201 unsigned int fullbatt_uV;
Chanwoo Choi2ed9e9b2012-08-21 17:06:52 +0900202 unsigned int fullbatt_soc;
203 unsigned int fullbatt_full_capacity;
Donggeun Kimad3d13ee2011-12-27 18:47:49 +0900204
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +0900205 enum data_source battery_present;
206
Jonghwa Lee856ee612013-12-18 15:42:35 +0900207 const char **psy_charger_stat;
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +0900208
209 int num_charger_regulators;
Chanwoo Choibee737b2012-07-12 15:03:25 +0900210 struct charger_regulator *charger_regulators;
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +0900211
Jonghwa Lee856ee612013-12-18 15:42:35 +0900212 const char *psy_fuel_gauge;
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +0900213
Jonghwa Lee856ee612013-12-18 15:42:35 +0900214 const char *thermal_zone;
Jonghwa Lee5c49a622013-12-18 15:42:34 +0900215
216 int temp_min;
217 int temp_max;
218 int temp_diff;
219
Donggeun Kimad3d13ee2011-12-27 18:47:49 +0900220 bool measure_battery_temp;
Chanwoo Choi8fcfe082012-09-20 21:20:05 -0700221
Jonghwa Lee856ee612013-12-18 15:42:35 +0900222 u32 charging_max_duration_ms;
223 u32 discharging_max_duration_ms;
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +0900224};
225
226#define PSY_NAME_MAX 30
227
228/**
229 * struct charger_manager
230 * @entry: entry for list
231 * @dev: device pointer
232 * @desc: instance of charger_desc
233 * @fuel_gauge: power_supply for fuel gauge
234 * @charger_stat: array of power_supply for chargers
Jonghwa Lee5c49a622013-12-18 15:42:34 +0900235 * @tzd_batt : thermal zone device for battery
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +0900236 * @charger_enabled: the state of charger
Chanwoo Choid829dc72012-05-05 06:24:10 -0700237 * @fullbatt_vchk_jiffies_at:
238 * jiffies at the time full battery check will occur.
Chanwoo Choid829dc72012-05-05 06:24:10 -0700239 * @fullbatt_vchk_work: work queue for full battery check
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +0900240 * @emergency_stop:
241 * When setting true, stop charging
Donggeun Kimad3d13ee2011-12-27 18:47:49 +0900242 * @psy_name_buf: the name of power-supply-class for charger manager
243 * @charger_psy: power_supply for charger manager
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +0900244 * @status_save_ext_pwr_inserted:
245 * saved status of external power before entering suspend-to-RAM
246 * @status_save_batt:
247 * saved status of battery before entering suspend-to-RAM
Chanwoo Choi8fcfe082012-09-20 21:20:05 -0700248 * @charging_start_time: saved start time of enabling charging
249 * @charging_end_time: saved end time of disabling charging
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +0900250 */
251struct charger_manager {
252 struct list_head entry;
253 struct device *dev;
254 struct charger_desc *desc;
255
256 struct power_supply *fuel_gauge;
257 struct power_supply **charger_stat;
258
Jonghwa Lee5c49a622013-12-18 15:42:34 +0900259#ifdef CONFIG_THERMAL
260 struct thermal_zone_device *tzd_batt;
261#endif
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +0900262 bool charger_enabled;
263
Chanwoo Choid829dc72012-05-05 06:24:10 -0700264 unsigned long fullbatt_vchk_jiffies_at;
Chanwoo Choid829dc72012-05-05 06:24:10 -0700265 struct delayed_work fullbatt_vchk_work;
266
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +0900267 int emergency_stop;
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +0900268
Donggeun Kimad3d13ee2011-12-27 18:47:49 +0900269 char psy_name_buf[PSY_NAME_MAX + 1];
270 struct power_supply charger_psy;
271
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +0900272 bool status_save_ext_pwr_inserted;
273 bool status_save_batt;
Chanwoo Choi8fcfe082012-09-20 21:20:05 -0700274
275 u64 charging_start_time;
276 u64 charging_end_time;
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +0900277};
278
279#ifdef CONFIG_CHARGER_MANAGER
280extern int setup_charger_manager(struct charger_global_desc *gd);
281extern bool cm_suspend_again(void);
Chanwoo Choidfeccb12012-05-05 06:26:47 -0700282extern void cm_notify_event(struct power_supply *psy,
283 enum cm_event_types type, char *msg);
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +0900284#else
Chanwoo Choidfeccb12012-05-05 06:26:47 -0700285static inline int setup_charger_manager(struct charger_global_desc *gd)
286{ return 0; }
287static inline bool cm_suspend_again(void) { return false; }
288static inline void cm_notify_event(struct power_supply *psy,
289 enum cm_event_types type, char *msg) { }
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +0900290#endif
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +0900291#endif /* _CHARGER_MANAGER_H */