blob: 0e86840eb603186728d6af5d14a3c277f3f4382b [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,
40 CM_EVENT_EXT_PWR_IN_OUT,
41 CM_EVENT_CHG_START_STOP,
42 CM_EVENT_OTHERS,
43};
44
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +090045/**
46 * struct charger_global_desc
47 * @rtc_name: the name of RTC used to wake up the system from suspend.
48 * @rtc_only_wakeup:
49 * If the system is woken up by waekup-sources other than the RTC or
50 * callbacks, Charger Manager should recognize with
51 * rtc_only_wakeup() returning false.
52 * If the RTC given to CM is the only wakeup reason,
53 * rtc_only_wakeup should return true.
Chanwoo Choid829dc72012-05-05 06:24:10 -070054 * @assume_timer_stops_in_suspend:
55 * Assume that the jiffy timer stops in suspend-to-RAM.
56 * When enabled, CM does not rely on jiffies value in
57 * suspend_again and assumes that jiffies value does not
58 * change during suspend.
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +090059 */
60struct charger_global_desc {
61 char *rtc_name;
62
63 bool (*rtc_only_wakeup)(void);
Chanwoo Choid829dc72012-05-05 06:24:10 -070064
65 bool assume_timer_stops_in_suspend;
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +090066};
67
68/**
Chanwoo Choibee737b2012-07-12 15:03:25 +090069 * struct charger_cable
70 * @extcon_name: the name of extcon device.
71 * @name: the name of charger cable(external connector).
72 * @extcon_dev: the extcon device.
73 * @wq: the workqueue to control charger according to the state of
74 * charger cable. If charger cable is attached, enable charger.
75 * But if charger cable is detached, disable charger.
76 * @nb: the notifier block to receive changed state from EXTCON
77 * (External Connector) when charger cable is attached/detached.
78 * @attached: the state of charger cable.
79 * true: the charger cable is attached
80 * false: the charger cable is detached
81 * @charger: the instance of struct charger_regulator.
82 * @cm: the Charger Manager representing the battery.
83 */
84struct charger_cable {
85 const char *extcon_name;
86 const char *name;
87
88 /* The charger-manager use Exton framework*/
89 struct extcon_specific_cable_nb extcon_dev;
90 struct work_struct wq;
91 struct notifier_block nb;
92
93 /* The state of charger cable */
94 bool attached;
95
96 struct charger_regulator *charger;
Chanwoo Choi45cd4fb2012-07-12 15:03:29 +090097
98 /*
99 * Set min/max current of regulator to protect over-current issue
100 * according to a kind of charger cable when cable is attached.
101 */
102 int min_uA;
103 int max_uA;
104
Chanwoo Choibee737b2012-07-12 15:03:25 +0900105 struct charger_manager *cm;
106};
107
108/**
109 * struct charger_regulator
110 * @regulator_name: the name of regulator for using charger.
111 * @consumer: the regulator consumer for the charger.
Chanwoo Choi3950c782012-09-21 18:49:37 +0900112 * @externally_control:
113 * Set if the charger-manager cannot control charger,
114 * the charger will be maintained with disabled state.
Chanwoo Choibee737b2012-07-12 15:03:25 +0900115 * @cables:
116 * the array of charger cables to enable/disable charger
117 * and set current limit according to constratint data of
118 * struct charger_cable if only charger cable included
119 * in the array of charger cables is attached/detached.
120 * @num_cables: the number of charger cables.
Chanwoo Choi3950c782012-09-21 18:49:37 +0900121 * @attr_g: Attribute group for the charger(regulator)
122 * @attr_name: "name" sysfs entry
123 * @attr_state: "state" sysfs entry
124 * @attr_externally_control: "externally_control" sysfs entry
125 * @attrs: Arrays pointing to attr_name/state/externally_control for attr_g
Chanwoo Choibee737b2012-07-12 15:03:25 +0900126 */
127struct charger_regulator {
128 /* The name of regulator for charging */
129 const char *regulator_name;
130 struct regulator *consumer;
131
Chanwoo Choi3950c782012-09-21 18:49:37 +0900132 /* charger never on when system is on */
133 int externally_control;
134
Chanwoo Choibee737b2012-07-12 15:03:25 +0900135 /*
136 * Store constraint information related to current limit,
137 * each cable have different condition for charging.
138 */
139 struct charger_cable *cables;
140 int num_cables;
Chanwoo Choi3950c782012-09-21 18:49:37 +0900141
142 struct attribute_group attr_g;
143 struct device_attribute attr_name;
144 struct device_attribute attr_state;
145 struct device_attribute attr_externally_control;
146 struct attribute *attrs[4];
147
148 struct charger_manager *cm;
Chanwoo Choibee737b2012-07-12 15:03:25 +0900149};
150
151/**
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +0900152 * struct charger_desc
Donggeun Kimad3d13ee2011-12-27 18:47:49 +0900153 * @psy_name: the name of power-supply-class for charger manager
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +0900154 * @polling_mode:
155 * Determine which polling mode will be used
Chanwoo Choid829dc72012-05-05 06:24:10 -0700156 * @fullbatt_vchkdrop_ms:
157 * @fullbatt_vchkdrop_uV:
158 * Check voltage drop after the battery is fully charged.
159 * If it has dropped more than fullbatt_vchkdrop_uV after
160 * fullbatt_vchkdrop_ms, CM will restart charging.
Donggeun Kimad3d13ee2011-12-27 18:47:49 +0900161 * @fullbatt_uV: voltage in microvolt
Chanwoo Choi2ed9e9b2012-08-21 17:06:52 +0900162 * If VBATT >= fullbatt_uV, it is assumed to be full.
163 * @fullbatt_soc: state of Charge in %
164 * If state of Charge >= fullbatt_soc, it is assumed to be full.
165 * @fullbatt_full_capacity: full capacity measure
166 * If full capacity of battery >= fullbatt_full_capacity,
Donggeun Kimad3d13ee2011-12-27 18:47:49 +0900167 * it is assumed to be full.
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +0900168 * @polling_interval_ms: interval in millisecond at which
169 * charger manager will monitor battery health
170 * @battery_present:
171 * Specify where information for existance of battery can be obtained
172 * @psy_charger_stat: the names of power-supply for chargers
173 * @num_charger_regulator: the number of entries in charger_regulators
Anton Vorontsovc6b27442012-08-22 21:36:05 -0700174 * @charger_regulators: array of charger regulators
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +0900175 * @psy_fuel_gauge: the name of power-supply for fuel gauge
176 * @temperature_out_of_range:
177 * Determine whether the status is overheat or cold or normal.
178 * return_value > 0: overheat
179 * return_value == 0: normal
180 * return_value < 0: cold
Donggeun Kimad3d13ee2011-12-27 18:47:49 +0900181 * @measure_battery_temp:
182 * true: measure battery temperature
183 * false: measure ambient temperature
Chanwoo Choi8fcfe082012-09-20 21:20:05 -0700184 * @charging_max_duration_ms: Maximum possible duration for charging
185 * If whole charging duration exceed 'charging_max_duration_ms',
186 * cm stop charging.
187 * @discharging_max_duration_ms:
188 * Maximum possible duration for discharging with charger cable
189 * after full-batt. If discharging duration exceed 'discharging
190 * max_duration_ms', cm start charging.
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +0900191 */
192struct charger_desc {
Donggeun Kimad3d13ee2011-12-27 18:47:49 +0900193 char *psy_name;
194
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +0900195 enum polling_modes polling_mode;
196 unsigned int polling_interval_ms;
197
Chanwoo Choid829dc72012-05-05 06:24:10 -0700198 unsigned int fullbatt_vchkdrop_ms;
199 unsigned int fullbatt_vchkdrop_uV;
Donggeun Kimad3d13ee2011-12-27 18:47:49 +0900200 unsigned int fullbatt_uV;
Chanwoo Choi2ed9e9b2012-08-21 17:06:52 +0900201 unsigned int fullbatt_soc;
202 unsigned int fullbatt_full_capacity;
Donggeun Kimad3d13ee2011-12-27 18:47:49 +0900203
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +0900204 enum data_source battery_present;
205
206 char **psy_charger_stat;
207
208 int num_charger_regulators;
Chanwoo Choibee737b2012-07-12 15:03:25 +0900209 struct charger_regulator *charger_regulators;
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +0900210
211 char *psy_fuel_gauge;
212
213 int (*temperature_out_of_range)(int *mC);
Donggeun Kimad3d13ee2011-12-27 18:47:49 +0900214 bool measure_battery_temp;
Chanwoo Choi8fcfe082012-09-20 21:20:05 -0700215
216 u64 charging_max_duration_ms;
217 u64 discharging_max_duration_ms;
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +0900218};
219
220#define PSY_NAME_MAX 30
221
222/**
223 * struct charger_manager
224 * @entry: entry for list
225 * @dev: device pointer
226 * @desc: instance of charger_desc
227 * @fuel_gauge: power_supply for fuel gauge
228 * @charger_stat: array of power_supply for chargers
229 * @charger_enabled: the state of charger
Chanwoo Choid829dc72012-05-05 06:24:10 -0700230 * @fullbatt_vchk_jiffies_at:
231 * jiffies at the time full battery check will occur.
Chanwoo Choid829dc72012-05-05 06:24:10 -0700232 * @fullbatt_vchk_work: work queue for full battery check
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +0900233 * @emergency_stop:
234 * When setting true, stop charging
235 * @last_temp_mC: the measured temperature in milli-Celsius
Donggeun Kimad3d13ee2011-12-27 18:47:49 +0900236 * @psy_name_buf: the name of power-supply-class for charger manager
237 * @charger_psy: power_supply for charger manager
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +0900238 * @status_save_ext_pwr_inserted:
239 * saved status of external power before entering suspend-to-RAM
240 * @status_save_batt:
241 * saved status of battery before entering suspend-to-RAM
Chanwoo Choi8fcfe082012-09-20 21:20:05 -0700242 * @charging_start_time: saved start time of enabling charging
243 * @charging_end_time: saved end time of disabling charging
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +0900244 */
245struct charger_manager {
246 struct list_head entry;
247 struct device *dev;
248 struct charger_desc *desc;
249
250 struct power_supply *fuel_gauge;
251 struct power_supply **charger_stat;
252
253 bool charger_enabled;
254
Chanwoo Choid829dc72012-05-05 06:24:10 -0700255 unsigned long fullbatt_vchk_jiffies_at;
Chanwoo Choid829dc72012-05-05 06:24:10 -0700256 struct delayed_work fullbatt_vchk_work;
257
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +0900258 int emergency_stop;
259 int last_temp_mC;
260
Donggeun Kimad3d13ee2011-12-27 18:47:49 +0900261 char psy_name_buf[PSY_NAME_MAX + 1];
262 struct power_supply charger_psy;
263
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +0900264 bool status_save_ext_pwr_inserted;
265 bool status_save_batt;
Chanwoo Choi8fcfe082012-09-20 21:20:05 -0700266
267 u64 charging_start_time;
268 u64 charging_end_time;
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +0900269};
270
271#ifdef CONFIG_CHARGER_MANAGER
272extern int setup_charger_manager(struct charger_global_desc *gd);
273extern bool cm_suspend_again(void);
Chanwoo Choidfeccb12012-05-05 06:26:47 -0700274extern void cm_notify_event(struct power_supply *psy,
275 enum cm_event_types type, char *msg);
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +0900276#else
Chanwoo Choidfeccb12012-05-05 06:26:47 -0700277static inline int setup_charger_manager(struct charger_global_desc *gd)
278{ return 0; }
279static inline bool cm_suspend_again(void) { return false; }
280static inline void cm_notify_event(struct power_supply *psy,
281 enum cm_event_types type, char *msg) { }
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +0900282#endif
Donggeun Kim3bb3dbb2011-12-27 18:47:48 +0900283#endif /* _CHARGER_MANAGER_H */