Commit 2aac3de19b72608f474c90034185c2be4908728f

Authored by Lee Jones
Committed by Anton Vorontsov
1 parent 5f98eb393c

ab8500: Clean up probe routines

These patches clean up some ugliness and brings the variable
initialisation formatting more into line with other drivers.

Signed-off-by: Lee Jones <lee.jones@linaro.org>
Signed-off-by: Anton Vorontsov <anton.vorontsov@linaro.org>

Showing 3 changed files with 30 additions and 18 deletions Inline Diff

drivers/power/ab8500_btemp.c
1 /* 1 /*
2 * Copyright (C) ST-Ericsson SA 2012 2 * Copyright (C) ST-Ericsson SA 2012
3 * 3 *
4 * Battery temperature driver for AB8500 4 * Battery temperature driver for AB8500
5 * 5 *
6 * License Terms: GNU General Public License v2 6 * License Terms: GNU General Public License v2
7 * Author: 7 * Author:
8 * Johan Palsson <johan.palsson@stericsson.com> 8 * Johan Palsson <johan.palsson@stericsson.com>
9 * Karl Komierowski <karl.komierowski@stericsson.com> 9 * Karl Komierowski <karl.komierowski@stericsson.com>
10 * Arun R Murthy <arun.murthy@stericsson.com> 10 * Arun R Murthy <arun.murthy@stericsson.com>
11 */ 11 */
12 12
13 #include <linux/init.h> 13 #include <linux/init.h>
14 #include <linux/module.h> 14 #include <linux/module.h>
15 #include <linux/device.h> 15 #include <linux/device.h>
16 #include <linux/interrupt.h> 16 #include <linux/interrupt.h>
17 #include <linux/delay.h> 17 #include <linux/delay.h>
18 #include <linux/slab.h> 18 #include <linux/slab.h>
19 #include <linux/platform_device.h> 19 #include <linux/platform_device.h>
20 #include <linux/power_supply.h> 20 #include <linux/power_supply.h>
21 #include <linux/completion.h> 21 #include <linux/completion.h>
22 #include <linux/workqueue.h> 22 #include <linux/workqueue.h>
23 #include <linux/mfd/abx500/ab8500.h> 23 #include <linux/mfd/abx500/ab8500.h>
24 #include <linux/mfd/abx500.h> 24 #include <linux/mfd/abx500.h>
25 #include <linux/mfd/abx500/ab8500-bm.h> 25 #include <linux/mfd/abx500/ab8500-bm.h>
26 #include <linux/mfd/abx500/ab8500-gpadc.h> 26 #include <linux/mfd/abx500/ab8500-gpadc.h>
27 #include <linux/jiffies.h> 27 #include <linux/jiffies.h>
28 28
29 #define VTVOUT_V 1800 29 #define VTVOUT_V 1800
30 30
31 #define BTEMP_THERMAL_LOW_LIMIT -10 31 #define BTEMP_THERMAL_LOW_LIMIT -10
32 #define BTEMP_THERMAL_MED_LIMIT 0 32 #define BTEMP_THERMAL_MED_LIMIT 0
33 #define BTEMP_THERMAL_HIGH_LIMIT_52 52 33 #define BTEMP_THERMAL_HIGH_LIMIT_52 52
34 #define BTEMP_THERMAL_HIGH_LIMIT_57 57 34 #define BTEMP_THERMAL_HIGH_LIMIT_57 57
35 #define BTEMP_THERMAL_HIGH_LIMIT_62 62 35 #define BTEMP_THERMAL_HIGH_LIMIT_62 62
36 36
37 #define BTEMP_BATCTRL_CURR_SRC_7UA 7 37 #define BTEMP_BATCTRL_CURR_SRC_7UA 7
38 #define BTEMP_BATCTRL_CURR_SRC_20UA 20 38 #define BTEMP_BATCTRL_CURR_SRC_20UA 20
39 39
40 #define to_ab8500_btemp_device_info(x) container_of((x), \ 40 #define to_ab8500_btemp_device_info(x) container_of((x), \
41 struct ab8500_btemp, btemp_psy); 41 struct ab8500_btemp, btemp_psy);
42 42
43 /** 43 /**
44 * struct ab8500_btemp_interrupts - ab8500 interrupts 44 * struct ab8500_btemp_interrupts - ab8500 interrupts
45 * @name: name of the interrupt 45 * @name: name of the interrupt
46 * @isr function pointer to the isr 46 * @isr function pointer to the isr
47 */ 47 */
48 struct ab8500_btemp_interrupts { 48 struct ab8500_btemp_interrupts {
49 char *name; 49 char *name;
50 irqreturn_t (*isr)(int irq, void *data); 50 irqreturn_t (*isr)(int irq, void *data);
51 }; 51 };
52 52
53 struct ab8500_btemp_events { 53 struct ab8500_btemp_events {
54 bool batt_rem; 54 bool batt_rem;
55 bool btemp_high; 55 bool btemp_high;
56 bool btemp_medhigh; 56 bool btemp_medhigh;
57 bool btemp_lowmed; 57 bool btemp_lowmed;
58 bool btemp_low; 58 bool btemp_low;
59 bool ac_conn; 59 bool ac_conn;
60 bool usb_conn; 60 bool usb_conn;
61 }; 61 };
62 62
63 struct ab8500_btemp_ranges { 63 struct ab8500_btemp_ranges {
64 int btemp_high_limit; 64 int btemp_high_limit;
65 int btemp_med_limit; 65 int btemp_med_limit;
66 int btemp_low_limit; 66 int btemp_low_limit;
67 }; 67 };
68 68
69 /** 69 /**
70 * struct ab8500_btemp - ab8500 BTEMP device information 70 * struct ab8500_btemp - ab8500 BTEMP device information
71 * @dev: Pointer to the structure device 71 * @dev: Pointer to the structure device
72 * @node: List of AB8500 BTEMPs, hence prepared for reentrance 72 * @node: List of AB8500 BTEMPs, hence prepared for reentrance
73 * @curr_source: What current source we use, in uA 73 * @curr_source: What current source we use, in uA
74 * @bat_temp: Battery temperature in degree Celcius 74 * @bat_temp: Battery temperature in degree Celcius
75 * @prev_bat_temp Last dispatched battery temperature 75 * @prev_bat_temp Last dispatched battery temperature
76 * @parent: Pointer to the struct ab8500 76 * @parent: Pointer to the struct ab8500
77 * @gpadc: Pointer to the struct gpadc 77 * @gpadc: Pointer to the struct gpadc
78 * @fg: Pointer to the struct fg 78 * @fg: Pointer to the struct fg
79 * @pdata: Pointer to the abx500_btemp platform data 79 * @pdata: Pointer to the abx500_btemp platform data
80 * @bat: Pointer to the abx500_bm platform data 80 * @bat: Pointer to the abx500_bm platform data
81 * @btemp_psy: Structure for BTEMP specific battery properties 81 * @btemp_psy: Structure for BTEMP specific battery properties
82 * @events: Structure for information about events triggered 82 * @events: Structure for information about events triggered
83 * @btemp_ranges: Battery temperature range structure 83 * @btemp_ranges: Battery temperature range structure
84 * @btemp_wq: Work queue for measuring the temperature periodically 84 * @btemp_wq: Work queue for measuring the temperature periodically
85 * @btemp_periodic_work: Work for measuring the temperature periodically 85 * @btemp_periodic_work: Work for measuring the temperature periodically
86 */ 86 */
87 struct ab8500_btemp { 87 struct ab8500_btemp {
88 struct device *dev; 88 struct device *dev;
89 struct list_head node; 89 struct list_head node;
90 int curr_source; 90 int curr_source;
91 int bat_temp; 91 int bat_temp;
92 int prev_bat_temp; 92 int prev_bat_temp;
93 struct ab8500 *parent; 93 struct ab8500 *parent;
94 struct ab8500_gpadc *gpadc; 94 struct ab8500_gpadc *gpadc;
95 struct ab8500_fg *fg; 95 struct ab8500_fg *fg;
96 struct abx500_btemp_platform_data *pdata; 96 struct abx500_btemp_platform_data *pdata;
97 struct abx500_bm_data *bat; 97 struct abx500_bm_data *bat;
98 struct power_supply btemp_psy; 98 struct power_supply btemp_psy;
99 struct ab8500_btemp_events events; 99 struct ab8500_btemp_events events;
100 struct ab8500_btemp_ranges btemp_ranges; 100 struct ab8500_btemp_ranges btemp_ranges;
101 struct workqueue_struct *btemp_wq; 101 struct workqueue_struct *btemp_wq;
102 struct delayed_work btemp_periodic_work; 102 struct delayed_work btemp_periodic_work;
103 }; 103 };
104 104
105 /* BTEMP power supply properties */ 105 /* BTEMP power supply properties */
106 static enum power_supply_property ab8500_btemp_props[] = { 106 static enum power_supply_property ab8500_btemp_props[] = {
107 POWER_SUPPLY_PROP_PRESENT, 107 POWER_SUPPLY_PROP_PRESENT,
108 POWER_SUPPLY_PROP_ONLINE, 108 POWER_SUPPLY_PROP_ONLINE,
109 POWER_SUPPLY_PROP_TECHNOLOGY, 109 POWER_SUPPLY_PROP_TECHNOLOGY,
110 POWER_SUPPLY_PROP_TEMP, 110 POWER_SUPPLY_PROP_TEMP,
111 }; 111 };
112 112
113 static LIST_HEAD(ab8500_btemp_list); 113 static LIST_HEAD(ab8500_btemp_list);
114 114
115 /** 115 /**
116 * ab8500_btemp_get() - returns a reference to the primary AB8500 BTEMP 116 * ab8500_btemp_get() - returns a reference to the primary AB8500 BTEMP
117 * (i.e. the first BTEMP in the instance list) 117 * (i.e. the first BTEMP in the instance list)
118 */ 118 */
119 struct ab8500_btemp *ab8500_btemp_get(void) 119 struct ab8500_btemp *ab8500_btemp_get(void)
120 { 120 {
121 struct ab8500_btemp *btemp; 121 struct ab8500_btemp *btemp;
122 btemp = list_first_entry(&ab8500_btemp_list, struct ab8500_btemp, node); 122 btemp = list_first_entry(&ab8500_btemp_list, struct ab8500_btemp, node);
123 123
124 return btemp; 124 return btemp;
125 } 125 }
126 126
127 /** 127 /**
128 * ab8500_btemp_batctrl_volt_to_res() - convert batctrl voltage to resistance 128 * ab8500_btemp_batctrl_volt_to_res() - convert batctrl voltage to resistance
129 * @di: pointer to the ab8500_btemp structure 129 * @di: pointer to the ab8500_btemp structure
130 * @v_batctrl: measured batctrl voltage 130 * @v_batctrl: measured batctrl voltage
131 * @inst_curr: measured instant current 131 * @inst_curr: measured instant current
132 * 132 *
133 * This function returns the battery resistance that is 133 * This function returns the battery resistance that is
134 * derived from the BATCTRL voltage. 134 * derived from the BATCTRL voltage.
135 * Returns value in Ohms. 135 * Returns value in Ohms.
136 */ 136 */
137 static int ab8500_btemp_batctrl_volt_to_res(struct ab8500_btemp *di, 137 static int ab8500_btemp_batctrl_volt_to_res(struct ab8500_btemp *di,
138 int v_batctrl, int inst_curr) 138 int v_batctrl, int inst_curr)
139 { 139 {
140 int rbs; 140 int rbs;
141 141
142 if (is_ab8500_1p1_or_earlier(di->parent)) { 142 if (is_ab8500_1p1_or_earlier(di->parent)) {
143 /* 143 /*
144 * For ABB cut1.0 and 1.1 BAT_CTRL is internally 144 * For ABB cut1.0 and 1.1 BAT_CTRL is internally
145 * connected to 1.8V through a 450k resistor 145 * connected to 1.8V through a 450k resistor
146 */ 146 */
147 return (450000 * (v_batctrl)) / (1800 - v_batctrl); 147 return (450000 * (v_batctrl)) / (1800 - v_batctrl);
148 } 148 }
149 149
150 if (di->bat->adc_therm == ABx500_ADC_THERM_BATCTRL) { 150 if (di->bat->adc_therm == ABx500_ADC_THERM_BATCTRL) {
151 /* 151 /*
152 * If the battery has internal NTC, we use the current 152 * If the battery has internal NTC, we use the current
153 * source to calculate the resistance, 7uA or 20uA 153 * source to calculate the resistance, 7uA or 20uA
154 */ 154 */
155 rbs = (v_batctrl * 1000 155 rbs = (v_batctrl * 1000
156 - di->bat->gnd_lift_resistance * inst_curr) 156 - di->bat->gnd_lift_resistance * inst_curr)
157 / di->curr_source; 157 / di->curr_source;
158 } else { 158 } else {
159 /* 159 /*
160 * BAT_CTRL is internally 160 * BAT_CTRL is internally
161 * connected to 1.8V through a 80k resistor 161 * connected to 1.8V through a 80k resistor
162 */ 162 */
163 rbs = (80000 * (v_batctrl)) / (1800 - v_batctrl); 163 rbs = (80000 * (v_batctrl)) / (1800 - v_batctrl);
164 } 164 }
165 165
166 return rbs; 166 return rbs;
167 } 167 }
168 168
169 /** 169 /**
170 * ab8500_btemp_read_batctrl_voltage() - measure batctrl voltage 170 * ab8500_btemp_read_batctrl_voltage() - measure batctrl voltage
171 * @di: pointer to the ab8500_btemp structure 171 * @di: pointer to the ab8500_btemp structure
172 * 172 *
173 * This function returns the voltage on BATCTRL. Returns value in mV. 173 * This function returns the voltage on BATCTRL. Returns value in mV.
174 */ 174 */
175 static int ab8500_btemp_read_batctrl_voltage(struct ab8500_btemp *di) 175 static int ab8500_btemp_read_batctrl_voltage(struct ab8500_btemp *di)
176 { 176 {
177 int vbtemp; 177 int vbtemp;
178 static int prev; 178 static int prev;
179 179
180 vbtemp = ab8500_gpadc_convert(di->gpadc, BAT_CTRL); 180 vbtemp = ab8500_gpadc_convert(di->gpadc, BAT_CTRL);
181 if (vbtemp < 0) { 181 if (vbtemp < 0) {
182 dev_err(di->dev, 182 dev_err(di->dev,
183 "%s gpadc conversion failed, using previous value", 183 "%s gpadc conversion failed, using previous value",
184 __func__); 184 __func__);
185 return prev; 185 return prev;
186 } 186 }
187 prev = vbtemp; 187 prev = vbtemp;
188 return vbtemp; 188 return vbtemp;
189 } 189 }
190 190
191 /** 191 /**
192 * ab8500_btemp_curr_source_enable() - enable/disable batctrl current source 192 * ab8500_btemp_curr_source_enable() - enable/disable batctrl current source
193 * @di: pointer to the ab8500_btemp structure 193 * @di: pointer to the ab8500_btemp structure
194 * @enable: enable or disable the current source 194 * @enable: enable or disable the current source
195 * 195 *
196 * Enable or disable the current sources for the BatCtrl AD channel 196 * Enable or disable the current sources for the BatCtrl AD channel
197 */ 197 */
198 static int ab8500_btemp_curr_source_enable(struct ab8500_btemp *di, 198 static int ab8500_btemp_curr_source_enable(struct ab8500_btemp *di,
199 bool enable) 199 bool enable)
200 { 200 {
201 int curr; 201 int curr;
202 int ret = 0; 202 int ret = 0;
203 203
204 /* 204 /*
205 * BATCTRL current sources are included on AB8500 cut2.0 205 * BATCTRL current sources are included on AB8500 cut2.0
206 * and future versions 206 * and future versions
207 */ 207 */
208 if (is_ab8500_1p1_or_earlier(di->parent)) 208 if (is_ab8500_1p1_or_earlier(di->parent))
209 return 0; 209 return 0;
210 210
211 /* Only do this for batteries with internal NTC */ 211 /* Only do this for batteries with internal NTC */
212 if (di->bat->adc_therm == ABx500_ADC_THERM_BATCTRL && enable) { 212 if (di->bat->adc_therm == ABx500_ADC_THERM_BATCTRL && enable) {
213 if (di->curr_source == BTEMP_BATCTRL_CURR_SRC_7UA) 213 if (di->curr_source == BTEMP_BATCTRL_CURR_SRC_7UA)
214 curr = BAT_CTRL_7U_ENA; 214 curr = BAT_CTRL_7U_ENA;
215 else 215 else
216 curr = BAT_CTRL_20U_ENA; 216 curr = BAT_CTRL_20U_ENA;
217 217
218 dev_dbg(di->dev, "Set BATCTRL %duA\n", di->curr_source); 218 dev_dbg(di->dev, "Set BATCTRL %duA\n", di->curr_source);
219 219
220 ret = abx500_mask_and_set_register_interruptible(di->dev, 220 ret = abx500_mask_and_set_register_interruptible(di->dev,
221 AB8500_CHARGER, AB8500_BAT_CTRL_CURRENT_SOURCE, 221 AB8500_CHARGER, AB8500_BAT_CTRL_CURRENT_SOURCE,
222 FORCE_BAT_CTRL_CMP_HIGH, FORCE_BAT_CTRL_CMP_HIGH); 222 FORCE_BAT_CTRL_CMP_HIGH, FORCE_BAT_CTRL_CMP_HIGH);
223 if (ret) { 223 if (ret) {
224 dev_err(di->dev, "%s failed setting cmp_force\n", 224 dev_err(di->dev, "%s failed setting cmp_force\n",
225 __func__); 225 __func__);
226 return ret; 226 return ret;
227 } 227 }
228 228
229 /* 229 /*
230 * We have to wait one 32kHz cycle before enabling 230 * We have to wait one 32kHz cycle before enabling
231 * the current source, since ForceBatCtrlCmpHigh needs 231 * the current source, since ForceBatCtrlCmpHigh needs
232 * to be written in a separate cycle 232 * to be written in a separate cycle
233 */ 233 */
234 udelay(32); 234 udelay(32);
235 235
236 ret = abx500_set_register_interruptible(di->dev, 236 ret = abx500_set_register_interruptible(di->dev,
237 AB8500_CHARGER, AB8500_BAT_CTRL_CURRENT_SOURCE, 237 AB8500_CHARGER, AB8500_BAT_CTRL_CURRENT_SOURCE,
238 FORCE_BAT_CTRL_CMP_HIGH | curr); 238 FORCE_BAT_CTRL_CMP_HIGH | curr);
239 if (ret) { 239 if (ret) {
240 dev_err(di->dev, "%s failed enabling current source\n", 240 dev_err(di->dev, "%s failed enabling current source\n",
241 __func__); 241 __func__);
242 goto disable_curr_source; 242 goto disable_curr_source;
243 } 243 }
244 } else if (di->bat->adc_therm == ABx500_ADC_THERM_BATCTRL && !enable) { 244 } else if (di->bat->adc_therm == ABx500_ADC_THERM_BATCTRL && !enable) {
245 dev_dbg(di->dev, "Disable BATCTRL curr source\n"); 245 dev_dbg(di->dev, "Disable BATCTRL curr source\n");
246 246
247 /* Write 0 to the curr bits */ 247 /* Write 0 to the curr bits */
248 ret = abx500_mask_and_set_register_interruptible(di->dev, 248 ret = abx500_mask_and_set_register_interruptible(di->dev,
249 AB8500_CHARGER, AB8500_BAT_CTRL_CURRENT_SOURCE, 249 AB8500_CHARGER, AB8500_BAT_CTRL_CURRENT_SOURCE,
250 BAT_CTRL_7U_ENA | BAT_CTRL_20U_ENA, 250 BAT_CTRL_7U_ENA | BAT_CTRL_20U_ENA,
251 ~(BAT_CTRL_7U_ENA | BAT_CTRL_20U_ENA)); 251 ~(BAT_CTRL_7U_ENA | BAT_CTRL_20U_ENA));
252 if (ret) { 252 if (ret) {
253 dev_err(di->dev, "%s failed disabling current source\n", 253 dev_err(di->dev, "%s failed disabling current source\n",
254 __func__); 254 __func__);
255 goto disable_curr_source; 255 goto disable_curr_source;
256 } 256 }
257 257
258 /* Enable Pull-Up and comparator */ 258 /* Enable Pull-Up and comparator */
259 ret = abx500_mask_and_set_register_interruptible(di->dev, 259 ret = abx500_mask_and_set_register_interruptible(di->dev,
260 AB8500_CHARGER, AB8500_BAT_CTRL_CURRENT_SOURCE, 260 AB8500_CHARGER, AB8500_BAT_CTRL_CURRENT_SOURCE,
261 BAT_CTRL_PULL_UP_ENA | BAT_CTRL_CMP_ENA, 261 BAT_CTRL_PULL_UP_ENA | BAT_CTRL_CMP_ENA,
262 BAT_CTRL_PULL_UP_ENA | BAT_CTRL_CMP_ENA); 262 BAT_CTRL_PULL_UP_ENA | BAT_CTRL_CMP_ENA);
263 if (ret) { 263 if (ret) {
264 dev_err(di->dev, "%s failed enabling PU and comp\n", 264 dev_err(di->dev, "%s failed enabling PU and comp\n",
265 __func__); 265 __func__);
266 goto enable_pu_comp; 266 goto enable_pu_comp;
267 } 267 }
268 268
269 /* 269 /*
270 * We have to wait one 32kHz cycle before disabling 270 * We have to wait one 32kHz cycle before disabling
271 * ForceBatCtrlCmpHigh since this needs to be written 271 * ForceBatCtrlCmpHigh since this needs to be written
272 * in a separate cycle 272 * in a separate cycle
273 */ 273 */
274 udelay(32); 274 udelay(32);
275 275
276 /* Disable 'force comparator' */ 276 /* Disable 'force comparator' */
277 ret = abx500_mask_and_set_register_interruptible(di->dev, 277 ret = abx500_mask_and_set_register_interruptible(di->dev,
278 AB8500_CHARGER, AB8500_BAT_CTRL_CURRENT_SOURCE, 278 AB8500_CHARGER, AB8500_BAT_CTRL_CURRENT_SOURCE,
279 FORCE_BAT_CTRL_CMP_HIGH, ~FORCE_BAT_CTRL_CMP_HIGH); 279 FORCE_BAT_CTRL_CMP_HIGH, ~FORCE_BAT_CTRL_CMP_HIGH);
280 if (ret) { 280 if (ret) {
281 dev_err(di->dev, "%s failed disabling force comp\n", 281 dev_err(di->dev, "%s failed disabling force comp\n",
282 __func__); 282 __func__);
283 goto disable_force_comp; 283 goto disable_force_comp;
284 } 284 }
285 } 285 }
286 return ret; 286 return ret;
287 287
288 /* 288 /*
289 * We have to try unsetting FORCE_BAT_CTRL_CMP_HIGH one more time 289 * We have to try unsetting FORCE_BAT_CTRL_CMP_HIGH one more time
290 * if we got an error above 290 * if we got an error above
291 */ 291 */
292 disable_curr_source: 292 disable_curr_source:
293 /* Write 0 to the curr bits */ 293 /* Write 0 to the curr bits */
294 ret = abx500_mask_and_set_register_interruptible(di->dev, 294 ret = abx500_mask_and_set_register_interruptible(di->dev,
295 AB8500_CHARGER, AB8500_BAT_CTRL_CURRENT_SOURCE, 295 AB8500_CHARGER, AB8500_BAT_CTRL_CURRENT_SOURCE,
296 BAT_CTRL_7U_ENA | BAT_CTRL_20U_ENA, 296 BAT_CTRL_7U_ENA | BAT_CTRL_20U_ENA,
297 ~(BAT_CTRL_7U_ENA | BAT_CTRL_20U_ENA)); 297 ~(BAT_CTRL_7U_ENA | BAT_CTRL_20U_ENA));
298 if (ret) { 298 if (ret) {
299 dev_err(di->dev, "%s failed disabling current source\n", 299 dev_err(di->dev, "%s failed disabling current source\n",
300 __func__); 300 __func__);
301 return ret; 301 return ret;
302 } 302 }
303 enable_pu_comp: 303 enable_pu_comp:
304 /* Enable Pull-Up and comparator */ 304 /* Enable Pull-Up and comparator */
305 ret = abx500_mask_and_set_register_interruptible(di->dev, 305 ret = abx500_mask_and_set_register_interruptible(di->dev,
306 AB8500_CHARGER, AB8500_BAT_CTRL_CURRENT_SOURCE, 306 AB8500_CHARGER, AB8500_BAT_CTRL_CURRENT_SOURCE,
307 BAT_CTRL_PULL_UP_ENA | BAT_CTRL_CMP_ENA, 307 BAT_CTRL_PULL_UP_ENA | BAT_CTRL_CMP_ENA,
308 BAT_CTRL_PULL_UP_ENA | BAT_CTRL_CMP_ENA); 308 BAT_CTRL_PULL_UP_ENA | BAT_CTRL_CMP_ENA);
309 if (ret) { 309 if (ret) {
310 dev_err(di->dev, "%s failed enabling PU and comp\n", 310 dev_err(di->dev, "%s failed enabling PU and comp\n",
311 __func__); 311 __func__);
312 return ret; 312 return ret;
313 } 313 }
314 314
315 disable_force_comp: 315 disable_force_comp:
316 /* 316 /*
317 * We have to wait one 32kHz cycle before disabling 317 * We have to wait one 32kHz cycle before disabling
318 * ForceBatCtrlCmpHigh since this needs to be written 318 * ForceBatCtrlCmpHigh since this needs to be written
319 * in a separate cycle 319 * in a separate cycle
320 */ 320 */
321 udelay(32); 321 udelay(32);
322 322
323 /* Disable 'force comparator' */ 323 /* Disable 'force comparator' */
324 ret = abx500_mask_and_set_register_interruptible(di->dev, 324 ret = abx500_mask_and_set_register_interruptible(di->dev,
325 AB8500_CHARGER, AB8500_BAT_CTRL_CURRENT_SOURCE, 325 AB8500_CHARGER, AB8500_BAT_CTRL_CURRENT_SOURCE,
326 FORCE_BAT_CTRL_CMP_HIGH, ~FORCE_BAT_CTRL_CMP_HIGH); 326 FORCE_BAT_CTRL_CMP_HIGH, ~FORCE_BAT_CTRL_CMP_HIGH);
327 if (ret) { 327 if (ret) {
328 dev_err(di->dev, "%s failed disabling force comp\n", 328 dev_err(di->dev, "%s failed disabling force comp\n",
329 __func__); 329 __func__);
330 return ret; 330 return ret;
331 } 331 }
332 332
333 return ret; 333 return ret;
334 } 334 }
335 335
336 /** 336 /**
337 * ab8500_btemp_get_batctrl_res() - get battery resistance 337 * ab8500_btemp_get_batctrl_res() - get battery resistance
338 * @di: pointer to the ab8500_btemp structure 338 * @di: pointer to the ab8500_btemp structure
339 * 339 *
340 * This function returns the battery pack identification resistance. 340 * This function returns the battery pack identification resistance.
341 * Returns value in Ohms. 341 * Returns value in Ohms.
342 */ 342 */
343 static int ab8500_btemp_get_batctrl_res(struct ab8500_btemp *di) 343 static int ab8500_btemp_get_batctrl_res(struct ab8500_btemp *di)
344 { 344 {
345 int ret; 345 int ret;
346 int batctrl = 0; 346 int batctrl = 0;
347 int res; 347 int res;
348 int inst_curr; 348 int inst_curr;
349 int i; 349 int i;
350 350
351 /* 351 /*
352 * BATCTRL current sources are included on AB8500 cut2.0 352 * BATCTRL current sources are included on AB8500 cut2.0
353 * and future versions 353 * and future versions
354 */ 354 */
355 ret = ab8500_btemp_curr_source_enable(di, true); 355 ret = ab8500_btemp_curr_source_enable(di, true);
356 if (ret) { 356 if (ret) {
357 dev_err(di->dev, "%s curr source enabled failed\n", __func__); 357 dev_err(di->dev, "%s curr source enabled failed\n", __func__);
358 return ret; 358 return ret;
359 } 359 }
360 360
361 if (!di->fg) 361 if (!di->fg)
362 di->fg = ab8500_fg_get(); 362 di->fg = ab8500_fg_get();
363 if (!di->fg) { 363 if (!di->fg) {
364 dev_err(di->dev, "No fg found\n"); 364 dev_err(di->dev, "No fg found\n");
365 return -EINVAL; 365 return -EINVAL;
366 } 366 }
367 367
368 ret = ab8500_fg_inst_curr_start(di->fg); 368 ret = ab8500_fg_inst_curr_start(di->fg);
369 369
370 if (ret) { 370 if (ret) {
371 dev_err(di->dev, "Failed to start current measurement\n"); 371 dev_err(di->dev, "Failed to start current measurement\n");
372 return ret; 372 return ret;
373 } 373 }
374 374
375 /* 375 /*
376 * Since there is no interrupt when current measurement is done, 376 * Since there is no interrupt when current measurement is done,
377 * loop for over 250ms (250ms is one sample conversion time 377 * loop for over 250ms (250ms is one sample conversion time
378 * with 32.768 Khz RTC clock). Note that a stop time must be set 378 * with 32.768 Khz RTC clock). Note that a stop time must be set
379 * since the ab8500_btemp_read_batctrl_voltage call can block and 379 * since the ab8500_btemp_read_batctrl_voltage call can block and
380 * take an unknown amount of time to complete. 380 * take an unknown amount of time to complete.
381 */ 381 */
382 i = 0; 382 i = 0;
383 383
384 do { 384 do {
385 batctrl += ab8500_btemp_read_batctrl_voltage(di); 385 batctrl += ab8500_btemp_read_batctrl_voltage(di);
386 i++; 386 i++;
387 msleep(20); 387 msleep(20);
388 } while (!ab8500_fg_inst_curr_done(di->fg)); 388 } while (!ab8500_fg_inst_curr_done(di->fg));
389 batctrl /= i; 389 batctrl /= i;
390 390
391 ret = ab8500_fg_inst_curr_finalize(di->fg, &inst_curr); 391 ret = ab8500_fg_inst_curr_finalize(di->fg, &inst_curr);
392 if (ret) { 392 if (ret) {
393 dev_err(di->dev, "Failed to finalize current measurement\n"); 393 dev_err(di->dev, "Failed to finalize current measurement\n");
394 return ret; 394 return ret;
395 } 395 }
396 396
397 res = ab8500_btemp_batctrl_volt_to_res(di, batctrl, inst_curr); 397 res = ab8500_btemp_batctrl_volt_to_res(di, batctrl, inst_curr);
398 398
399 ret = ab8500_btemp_curr_source_enable(di, false); 399 ret = ab8500_btemp_curr_source_enable(di, false);
400 if (ret) { 400 if (ret) {
401 dev_err(di->dev, "%s curr source disable failed\n", __func__); 401 dev_err(di->dev, "%s curr source disable failed\n", __func__);
402 return ret; 402 return ret;
403 } 403 }
404 404
405 dev_dbg(di->dev, "%s batctrl: %d res: %d inst_curr: %d samples: %d\n", 405 dev_dbg(di->dev, "%s batctrl: %d res: %d inst_curr: %d samples: %d\n",
406 __func__, batctrl, res, inst_curr, i); 406 __func__, batctrl, res, inst_curr, i);
407 407
408 return res; 408 return res;
409 } 409 }
410 410
411 /** 411 /**
412 * ab8500_btemp_res_to_temp() - resistance to temperature 412 * ab8500_btemp_res_to_temp() - resistance to temperature
413 * @di: pointer to the ab8500_btemp structure 413 * @di: pointer to the ab8500_btemp structure
414 * @tbl: pointer to the resiatance to temperature table 414 * @tbl: pointer to the resiatance to temperature table
415 * @tbl_size: size of the resistance to temperature table 415 * @tbl_size: size of the resistance to temperature table
416 * @res: resistance to calculate the temperature from 416 * @res: resistance to calculate the temperature from
417 * 417 *
418 * This function returns the battery temperature in degrees Celcius 418 * This function returns the battery temperature in degrees Celcius
419 * based on the NTC resistance. 419 * based on the NTC resistance.
420 */ 420 */
421 static int ab8500_btemp_res_to_temp(struct ab8500_btemp *di, 421 static int ab8500_btemp_res_to_temp(struct ab8500_btemp *di,
422 const struct abx500_res_to_temp *tbl, int tbl_size, int res) 422 const struct abx500_res_to_temp *tbl, int tbl_size, int res)
423 { 423 {
424 int i, temp; 424 int i, temp;
425 /* 425 /*
426 * Calculate the formula for the straight line 426 * Calculate the formula for the straight line
427 * Simple interpolation if we are within 427 * Simple interpolation if we are within
428 * the resistance table limits, extrapolate 428 * the resistance table limits, extrapolate
429 * if resistance is outside the limits. 429 * if resistance is outside the limits.
430 */ 430 */
431 if (res > tbl[0].resist) 431 if (res > tbl[0].resist)
432 i = 0; 432 i = 0;
433 else if (res <= tbl[tbl_size - 1].resist) 433 else if (res <= tbl[tbl_size - 1].resist)
434 i = tbl_size - 2; 434 i = tbl_size - 2;
435 else { 435 else {
436 i = 0; 436 i = 0;
437 while (!(res <= tbl[i].resist && 437 while (!(res <= tbl[i].resist &&
438 res > tbl[i + 1].resist)) 438 res > tbl[i + 1].resist))
439 i++; 439 i++;
440 } 440 }
441 441
442 temp = tbl[i].temp + ((tbl[i + 1].temp - tbl[i].temp) * 442 temp = tbl[i].temp + ((tbl[i + 1].temp - tbl[i].temp) *
443 (res - tbl[i].resist)) / (tbl[i + 1].resist - tbl[i].resist); 443 (res - tbl[i].resist)) / (tbl[i + 1].resist - tbl[i].resist);
444 return temp; 444 return temp;
445 } 445 }
446 446
447 /** 447 /**
448 * ab8500_btemp_measure_temp() - measure battery temperature 448 * ab8500_btemp_measure_temp() - measure battery temperature
449 * @di: pointer to the ab8500_btemp structure 449 * @di: pointer to the ab8500_btemp structure
450 * 450 *
451 * Returns battery temperature (on success) else the previous temperature 451 * Returns battery temperature (on success) else the previous temperature
452 */ 452 */
453 static int ab8500_btemp_measure_temp(struct ab8500_btemp *di) 453 static int ab8500_btemp_measure_temp(struct ab8500_btemp *di)
454 { 454 {
455 int temp; 455 int temp;
456 static int prev; 456 static int prev;
457 int rbat, rntc, vntc; 457 int rbat, rntc, vntc;
458 u8 id; 458 u8 id;
459 459
460 id = di->bat->batt_id; 460 id = di->bat->batt_id;
461 461
462 if (di->bat->adc_therm == ABx500_ADC_THERM_BATCTRL && 462 if (di->bat->adc_therm == ABx500_ADC_THERM_BATCTRL &&
463 id != BATTERY_UNKNOWN) { 463 id != BATTERY_UNKNOWN) {
464 464
465 rbat = ab8500_btemp_get_batctrl_res(di); 465 rbat = ab8500_btemp_get_batctrl_res(di);
466 if (rbat < 0) { 466 if (rbat < 0) {
467 dev_err(di->dev, "%s get batctrl res failed\n", 467 dev_err(di->dev, "%s get batctrl res failed\n",
468 __func__); 468 __func__);
469 /* 469 /*
470 * Return out-of-range temperature so that 470 * Return out-of-range temperature so that
471 * charging is stopped 471 * charging is stopped
472 */ 472 */
473 return BTEMP_THERMAL_LOW_LIMIT; 473 return BTEMP_THERMAL_LOW_LIMIT;
474 } 474 }
475 475
476 temp = ab8500_btemp_res_to_temp(di, 476 temp = ab8500_btemp_res_to_temp(di,
477 di->bat->bat_type[id].r_to_t_tbl, 477 di->bat->bat_type[id].r_to_t_tbl,
478 di->bat->bat_type[id].n_temp_tbl_elements, rbat); 478 di->bat->bat_type[id].n_temp_tbl_elements, rbat);
479 } else { 479 } else {
480 vntc = ab8500_gpadc_convert(di->gpadc, BTEMP_BALL); 480 vntc = ab8500_gpadc_convert(di->gpadc, BTEMP_BALL);
481 if (vntc < 0) { 481 if (vntc < 0) {
482 dev_err(di->dev, 482 dev_err(di->dev,
483 "%s gpadc conversion failed," 483 "%s gpadc conversion failed,"
484 " using previous value\n", __func__); 484 " using previous value\n", __func__);
485 return prev; 485 return prev;
486 } 486 }
487 /* 487 /*
488 * The PCB NTC is sourced from VTVOUT via a 230kOhm 488 * The PCB NTC is sourced from VTVOUT via a 230kOhm
489 * resistor. 489 * resistor.
490 */ 490 */
491 rntc = 230000 * vntc / (VTVOUT_V - vntc); 491 rntc = 230000 * vntc / (VTVOUT_V - vntc);
492 492
493 temp = ab8500_btemp_res_to_temp(di, 493 temp = ab8500_btemp_res_to_temp(di,
494 di->bat->bat_type[id].r_to_t_tbl, 494 di->bat->bat_type[id].r_to_t_tbl,
495 di->bat->bat_type[id].n_temp_tbl_elements, rntc); 495 di->bat->bat_type[id].n_temp_tbl_elements, rntc);
496 prev = temp; 496 prev = temp;
497 } 497 }
498 dev_dbg(di->dev, "Battery temperature is %d\n", temp); 498 dev_dbg(di->dev, "Battery temperature is %d\n", temp);
499 return temp; 499 return temp;
500 } 500 }
501 501
502 /** 502 /**
503 * ab8500_btemp_id() - Identify the connected battery 503 * ab8500_btemp_id() - Identify the connected battery
504 * @di: pointer to the ab8500_btemp structure 504 * @di: pointer to the ab8500_btemp structure
505 * 505 *
506 * This function will try to identify the battery by reading the ID 506 * This function will try to identify the battery by reading the ID
507 * resistor. Some brands use a combined ID resistor with a NTC resistor to 507 * resistor. Some brands use a combined ID resistor with a NTC resistor to
508 * both be able to identify and to read the temperature of it. 508 * both be able to identify and to read the temperature of it.
509 */ 509 */
510 static int ab8500_btemp_id(struct ab8500_btemp *di) 510 static int ab8500_btemp_id(struct ab8500_btemp *di)
511 { 511 {
512 int res; 512 int res;
513 u8 i; 513 u8 i;
514 514
515 di->curr_source = BTEMP_BATCTRL_CURR_SRC_7UA; 515 di->curr_source = BTEMP_BATCTRL_CURR_SRC_7UA;
516 di->bat->batt_id = BATTERY_UNKNOWN; 516 di->bat->batt_id = BATTERY_UNKNOWN;
517 517
518 res = ab8500_btemp_get_batctrl_res(di); 518 res = ab8500_btemp_get_batctrl_res(di);
519 if (res < 0) { 519 if (res < 0) {
520 dev_err(di->dev, "%s get batctrl res failed\n", __func__); 520 dev_err(di->dev, "%s get batctrl res failed\n", __func__);
521 return -ENXIO; 521 return -ENXIO;
522 } 522 }
523 523
524 /* BATTERY_UNKNOWN is defined on position 0, skip it! */ 524 /* BATTERY_UNKNOWN is defined on position 0, skip it! */
525 for (i = BATTERY_UNKNOWN + 1; i < di->bat->n_btypes; i++) { 525 for (i = BATTERY_UNKNOWN + 1; i < di->bat->n_btypes; i++) {
526 if ((res <= di->bat->bat_type[i].resis_high) && 526 if ((res <= di->bat->bat_type[i].resis_high) &&
527 (res >= di->bat->bat_type[i].resis_low)) { 527 (res >= di->bat->bat_type[i].resis_low)) {
528 dev_dbg(di->dev, "Battery detected on %s" 528 dev_dbg(di->dev, "Battery detected on %s"
529 " low %d < res %d < high: %d" 529 " low %d < res %d < high: %d"
530 " index: %d\n", 530 " index: %d\n",
531 di->bat->adc_therm == ABx500_ADC_THERM_BATCTRL ? 531 di->bat->adc_therm == ABx500_ADC_THERM_BATCTRL ?
532 "BATCTRL" : "BATTEMP", 532 "BATCTRL" : "BATTEMP",
533 di->bat->bat_type[i].resis_low, res, 533 di->bat->bat_type[i].resis_low, res,
534 di->bat->bat_type[i].resis_high, i); 534 di->bat->bat_type[i].resis_high, i);
535 535
536 di->bat->batt_id = i; 536 di->bat->batt_id = i;
537 break; 537 break;
538 } 538 }
539 } 539 }
540 540
541 if (di->bat->batt_id == BATTERY_UNKNOWN) { 541 if (di->bat->batt_id == BATTERY_UNKNOWN) {
542 dev_warn(di->dev, "Battery identified as unknown" 542 dev_warn(di->dev, "Battery identified as unknown"
543 ", resistance %d Ohm\n", res); 543 ", resistance %d Ohm\n", res);
544 return -ENXIO; 544 return -ENXIO;
545 } 545 }
546 546
547 /* 547 /*
548 * We only have to change current source if the 548 * We only have to change current source if the
549 * detected type is Type 1, else we use the 7uA source 549 * detected type is Type 1, else we use the 7uA source
550 */ 550 */
551 if (di->bat->adc_therm == ABx500_ADC_THERM_BATCTRL && 551 if (di->bat->adc_therm == ABx500_ADC_THERM_BATCTRL &&
552 di->bat->batt_id == 1) { 552 di->bat->batt_id == 1) {
553 dev_dbg(di->dev, "Set BATCTRL current source to 20uA\n"); 553 dev_dbg(di->dev, "Set BATCTRL current source to 20uA\n");
554 di->curr_source = BTEMP_BATCTRL_CURR_SRC_20UA; 554 di->curr_source = BTEMP_BATCTRL_CURR_SRC_20UA;
555 } 555 }
556 556
557 return di->bat->batt_id; 557 return di->bat->batt_id;
558 } 558 }
559 559
560 /** 560 /**
561 * ab8500_btemp_periodic_work() - Measuring the temperature periodically 561 * ab8500_btemp_periodic_work() - Measuring the temperature periodically
562 * @work: pointer to the work_struct structure 562 * @work: pointer to the work_struct structure
563 * 563 *
564 * Work function for measuring the temperature periodically 564 * Work function for measuring the temperature periodically
565 */ 565 */
566 static void ab8500_btemp_periodic_work(struct work_struct *work) 566 static void ab8500_btemp_periodic_work(struct work_struct *work)
567 { 567 {
568 int interval; 568 int interval;
569 struct ab8500_btemp *di = container_of(work, 569 struct ab8500_btemp *di = container_of(work,
570 struct ab8500_btemp, btemp_periodic_work.work); 570 struct ab8500_btemp, btemp_periodic_work.work);
571 571
572 di->bat_temp = ab8500_btemp_measure_temp(di); 572 di->bat_temp = ab8500_btemp_measure_temp(di);
573 573
574 if (di->bat_temp != di->prev_bat_temp) { 574 if (di->bat_temp != di->prev_bat_temp) {
575 di->prev_bat_temp = di->bat_temp; 575 di->prev_bat_temp = di->bat_temp;
576 power_supply_changed(&di->btemp_psy); 576 power_supply_changed(&di->btemp_psy);
577 } 577 }
578 578
579 if (di->events.ac_conn || di->events.usb_conn) 579 if (di->events.ac_conn || di->events.usb_conn)
580 interval = di->bat->temp_interval_chg; 580 interval = di->bat->temp_interval_chg;
581 else 581 else
582 interval = di->bat->temp_interval_nochg; 582 interval = di->bat->temp_interval_nochg;
583 583
584 /* Schedule a new measurement */ 584 /* Schedule a new measurement */
585 queue_delayed_work(di->btemp_wq, 585 queue_delayed_work(di->btemp_wq,
586 &di->btemp_periodic_work, 586 &di->btemp_periodic_work,
587 round_jiffies(interval * HZ)); 587 round_jiffies(interval * HZ));
588 } 588 }
589 589
590 /** 590 /**
591 * ab8500_btemp_batctrlindb_handler() - battery removal detected 591 * ab8500_btemp_batctrlindb_handler() - battery removal detected
592 * @irq: interrupt number 592 * @irq: interrupt number
593 * @_di: void pointer that has to address of ab8500_btemp 593 * @_di: void pointer that has to address of ab8500_btemp
594 * 594 *
595 * Returns IRQ status(IRQ_HANDLED) 595 * Returns IRQ status(IRQ_HANDLED)
596 */ 596 */
597 static irqreturn_t ab8500_btemp_batctrlindb_handler(int irq, void *_di) 597 static irqreturn_t ab8500_btemp_batctrlindb_handler(int irq, void *_di)
598 { 598 {
599 struct ab8500_btemp *di = _di; 599 struct ab8500_btemp *di = _di;
600 dev_err(di->dev, "Battery removal detected!\n"); 600 dev_err(di->dev, "Battery removal detected!\n");
601 601
602 di->events.batt_rem = true; 602 di->events.batt_rem = true;
603 power_supply_changed(&di->btemp_psy); 603 power_supply_changed(&di->btemp_psy);
604 604
605 return IRQ_HANDLED; 605 return IRQ_HANDLED;
606 } 606 }
607 607
608 /** 608 /**
609 * ab8500_btemp_templow_handler() - battery temp lower than 10 degrees 609 * ab8500_btemp_templow_handler() - battery temp lower than 10 degrees
610 * @irq: interrupt number 610 * @irq: interrupt number
611 * @_di: void pointer that has to address of ab8500_btemp 611 * @_di: void pointer that has to address of ab8500_btemp
612 * 612 *
613 * Returns IRQ status(IRQ_HANDLED) 613 * Returns IRQ status(IRQ_HANDLED)
614 */ 614 */
615 static irqreturn_t ab8500_btemp_templow_handler(int irq, void *_di) 615 static irqreturn_t ab8500_btemp_templow_handler(int irq, void *_di)
616 { 616 {
617 struct ab8500_btemp *di = _di; 617 struct ab8500_btemp *di = _di;
618 618
619 if (is_ab8500_2p0_or_earlier(di->parent)) { 619 if (is_ab8500_2p0_or_earlier(di->parent)) {
620 dev_dbg(di->dev, "Ignore false btemp low irq" 620 dev_dbg(di->dev, "Ignore false btemp low irq"
621 " for ABB cut 1.0, 1.1 and 2.0\n"); 621 " for ABB cut 1.0, 1.1 and 2.0\n");
622 } else { 622 } else {
623 dev_crit(di->dev, "Battery temperature lower than -10deg c\n"); 623 dev_crit(di->dev, "Battery temperature lower than -10deg c\n");
624 624
625 di->events.btemp_low = true; 625 di->events.btemp_low = true;
626 di->events.btemp_high = false; 626 di->events.btemp_high = false;
627 di->events.btemp_medhigh = false; 627 di->events.btemp_medhigh = false;
628 di->events.btemp_lowmed = false; 628 di->events.btemp_lowmed = false;
629 power_supply_changed(&di->btemp_psy); 629 power_supply_changed(&di->btemp_psy);
630 } 630 }
631 631
632 return IRQ_HANDLED; 632 return IRQ_HANDLED;
633 } 633 }
634 634
635 /** 635 /**
636 * ab8500_btemp_temphigh_handler() - battery temp higher than max temp 636 * ab8500_btemp_temphigh_handler() - battery temp higher than max temp
637 * @irq: interrupt number 637 * @irq: interrupt number
638 * @_di: void pointer that has to address of ab8500_btemp 638 * @_di: void pointer that has to address of ab8500_btemp
639 * 639 *
640 * Returns IRQ status(IRQ_HANDLED) 640 * Returns IRQ status(IRQ_HANDLED)
641 */ 641 */
642 static irqreturn_t ab8500_btemp_temphigh_handler(int irq, void *_di) 642 static irqreturn_t ab8500_btemp_temphigh_handler(int irq, void *_di)
643 { 643 {
644 struct ab8500_btemp *di = _di; 644 struct ab8500_btemp *di = _di;
645 645
646 dev_crit(di->dev, "Battery temperature is higher than MAX temp\n"); 646 dev_crit(di->dev, "Battery temperature is higher than MAX temp\n");
647 647
648 di->events.btemp_high = true; 648 di->events.btemp_high = true;
649 di->events.btemp_medhigh = false; 649 di->events.btemp_medhigh = false;
650 di->events.btemp_lowmed = false; 650 di->events.btemp_lowmed = false;
651 di->events.btemp_low = false; 651 di->events.btemp_low = false;
652 power_supply_changed(&di->btemp_psy); 652 power_supply_changed(&di->btemp_psy);
653 653
654 return IRQ_HANDLED; 654 return IRQ_HANDLED;
655 } 655 }
656 656
657 /** 657 /**
658 * ab8500_btemp_lowmed_handler() - battery temp between low and medium 658 * ab8500_btemp_lowmed_handler() - battery temp between low and medium
659 * @irq: interrupt number 659 * @irq: interrupt number
660 * @_di: void pointer that has to address of ab8500_btemp 660 * @_di: void pointer that has to address of ab8500_btemp
661 * 661 *
662 * Returns IRQ status(IRQ_HANDLED) 662 * Returns IRQ status(IRQ_HANDLED)
663 */ 663 */
664 static irqreturn_t ab8500_btemp_lowmed_handler(int irq, void *_di) 664 static irqreturn_t ab8500_btemp_lowmed_handler(int irq, void *_di)
665 { 665 {
666 struct ab8500_btemp *di = _di; 666 struct ab8500_btemp *di = _di;
667 667
668 dev_dbg(di->dev, "Battery temperature is between low and medium\n"); 668 dev_dbg(di->dev, "Battery temperature is between low and medium\n");
669 669
670 di->events.btemp_lowmed = true; 670 di->events.btemp_lowmed = true;
671 di->events.btemp_medhigh = false; 671 di->events.btemp_medhigh = false;
672 di->events.btemp_high = false; 672 di->events.btemp_high = false;
673 di->events.btemp_low = false; 673 di->events.btemp_low = false;
674 power_supply_changed(&di->btemp_psy); 674 power_supply_changed(&di->btemp_psy);
675 675
676 return IRQ_HANDLED; 676 return IRQ_HANDLED;
677 } 677 }
678 678
679 /** 679 /**
680 * ab8500_btemp_medhigh_handler() - battery temp between medium and high 680 * ab8500_btemp_medhigh_handler() - battery temp between medium and high
681 * @irq: interrupt number 681 * @irq: interrupt number
682 * @_di: void pointer that has to address of ab8500_btemp 682 * @_di: void pointer that has to address of ab8500_btemp
683 * 683 *
684 * Returns IRQ status(IRQ_HANDLED) 684 * Returns IRQ status(IRQ_HANDLED)
685 */ 685 */
686 static irqreturn_t ab8500_btemp_medhigh_handler(int irq, void *_di) 686 static irqreturn_t ab8500_btemp_medhigh_handler(int irq, void *_di)
687 { 687 {
688 struct ab8500_btemp *di = _di; 688 struct ab8500_btemp *di = _di;
689 689
690 dev_dbg(di->dev, "Battery temperature is between medium and high\n"); 690 dev_dbg(di->dev, "Battery temperature is between medium and high\n");
691 691
692 di->events.btemp_medhigh = true; 692 di->events.btemp_medhigh = true;
693 di->events.btemp_lowmed = false; 693 di->events.btemp_lowmed = false;
694 di->events.btemp_high = false; 694 di->events.btemp_high = false;
695 di->events.btemp_low = false; 695 di->events.btemp_low = false;
696 power_supply_changed(&di->btemp_psy); 696 power_supply_changed(&di->btemp_psy);
697 697
698 return IRQ_HANDLED; 698 return IRQ_HANDLED;
699 } 699 }
700 700
701 /** 701 /**
702 * ab8500_btemp_periodic() - Periodic temperature measurements 702 * ab8500_btemp_periodic() - Periodic temperature measurements
703 * @di: pointer to the ab8500_btemp structure 703 * @di: pointer to the ab8500_btemp structure
704 * @enable: enable or disable periodic temperature measurements 704 * @enable: enable or disable periodic temperature measurements
705 * 705 *
706 * Starts of stops periodic temperature measurements. Periodic measurements 706 * Starts of stops periodic temperature measurements. Periodic measurements
707 * should only be done when a charger is connected. 707 * should only be done when a charger is connected.
708 */ 708 */
709 static void ab8500_btemp_periodic(struct ab8500_btemp *di, 709 static void ab8500_btemp_periodic(struct ab8500_btemp *di,
710 bool enable) 710 bool enable)
711 { 711 {
712 dev_dbg(di->dev, "Enable periodic temperature measurements: %d\n", 712 dev_dbg(di->dev, "Enable periodic temperature measurements: %d\n",
713 enable); 713 enable);
714 /* 714 /*
715 * Make sure a new measurement is done directly by cancelling 715 * Make sure a new measurement is done directly by cancelling
716 * any pending work 716 * any pending work
717 */ 717 */
718 cancel_delayed_work_sync(&di->btemp_periodic_work); 718 cancel_delayed_work_sync(&di->btemp_periodic_work);
719 719
720 if (enable) 720 if (enable)
721 queue_delayed_work(di->btemp_wq, &di->btemp_periodic_work, 0); 721 queue_delayed_work(di->btemp_wq, &di->btemp_periodic_work, 0);
722 } 722 }
723 723
724 /** 724 /**
725 * ab8500_btemp_get_temp() - get battery temperature 725 * ab8500_btemp_get_temp() - get battery temperature
726 * @di: pointer to the ab8500_btemp structure 726 * @di: pointer to the ab8500_btemp structure
727 * 727 *
728 * Returns battery temperature 728 * Returns battery temperature
729 */ 729 */
730 static int ab8500_btemp_get_temp(struct ab8500_btemp *di) 730 static int ab8500_btemp_get_temp(struct ab8500_btemp *di)
731 { 731 {
732 int temp = 0; 732 int temp = 0;
733 733
734 /* 734 /*
735 * The BTEMP events are not reliabe on AB8500 cut2.0 735 * The BTEMP events are not reliabe on AB8500 cut2.0
736 * and prior versions 736 * and prior versions
737 */ 737 */
738 if (is_ab8500_2p0_or_earlier(di->parent)) { 738 if (is_ab8500_2p0_or_earlier(di->parent)) {
739 temp = di->bat_temp * 10; 739 temp = di->bat_temp * 10;
740 } else { 740 } else {
741 if (di->events.btemp_low) { 741 if (di->events.btemp_low) {
742 if (temp > di->btemp_ranges.btemp_low_limit) 742 if (temp > di->btemp_ranges.btemp_low_limit)
743 temp = di->btemp_ranges.btemp_low_limit; 743 temp = di->btemp_ranges.btemp_low_limit;
744 else 744 else
745 temp = di->bat_temp * 10; 745 temp = di->bat_temp * 10;
746 } else if (di->events.btemp_high) { 746 } else if (di->events.btemp_high) {
747 if (temp < di->btemp_ranges.btemp_high_limit) 747 if (temp < di->btemp_ranges.btemp_high_limit)
748 temp = di->btemp_ranges.btemp_high_limit; 748 temp = di->btemp_ranges.btemp_high_limit;
749 else 749 else
750 temp = di->bat_temp * 10; 750 temp = di->bat_temp * 10;
751 } else if (di->events.btemp_lowmed) { 751 } else if (di->events.btemp_lowmed) {
752 if (temp > di->btemp_ranges.btemp_med_limit) 752 if (temp > di->btemp_ranges.btemp_med_limit)
753 temp = di->btemp_ranges.btemp_med_limit; 753 temp = di->btemp_ranges.btemp_med_limit;
754 else 754 else
755 temp = di->bat_temp * 10; 755 temp = di->bat_temp * 10;
756 } else if (di->events.btemp_medhigh) { 756 } else if (di->events.btemp_medhigh) {
757 if (temp < di->btemp_ranges.btemp_med_limit) 757 if (temp < di->btemp_ranges.btemp_med_limit)
758 temp = di->btemp_ranges.btemp_med_limit; 758 temp = di->btemp_ranges.btemp_med_limit;
759 else 759 else
760 temp = di->bat_temp * 10; 760 temp = di->bat_temp * 10;
761 } else 761 } else
762 temp = di->bat_temp * 10; 762 temp = di->bat_temp * 10;
763 } 763 }
764 return temp; 764 return temp;
765 } 765 }
766 766
767 /** 767 /**
768 * ab8500_btemp_get_batctrl_temp() - get the temperature 768 * ab8500_btemp_get_batctrl_temp() - get the temperature
769 * @btemp: pointer to the btemp structure 769 * @btemp: pointer to the btemp structure
770 * 770 *
771 * Returns the batctrl temperature in millidegrees 771 * Returns the batctrl temperature in millidegrees
772 */ 772 */
773 int ab8500_btemp_get_batctrl_temp(struct ab8500_btemp *btemp) 773 int ab8500_btemp_get_batctrl_temp(struct ab8500_btemp *btemp)
774 { 774 {
775 return btemp->bat_temp * 1000; 775 return btemp->bat_temp * 1000;
776 } 776 }
777 777
778 /** 778 /**
779 * ab8500_btemp_get_property() - get the btemp properties 779 * ab8500_btemp_get_property() - get the btemp properties
780 * @psy: pointer to the power_supply structure 780 * @psy: pointer to the power_supply structure
781 * @psp: pointer to the power_supply_property structure 781 * @psp: pointer to the power_supply_property structure
782 * @val: pointer to the power_supply_propval union 782 * @val: pointer to the power_supply_propval union
783 * 783 *
784 * This function gets called when an application tries to get the btemp 784 * This function gets called when an application tries to get the btemp
785 * properties by reading the sysfs files. 785 * properties by reading the sysfs files.
786 * online: presence of the battery 786 * online: presence of the battery
787 * present: presence of the battery 787 * present: presence of the battery
788 * technology: battery technology 788 * technology: battery technology
789 * temp: battery temperature 789 * temp: battery temperature
790 * Returns error code in case of failure else 0(on success) 790 * Returns error code in case of failure else 0(on success)
791 */ 791 */
792 static int ab8500_btemp_get_property(struct power_supply *psy, 792 static int ab8500_btemp_get_property(struct power_supply *psy,
793 enum power_supply_property psp, 793 enum power_supply_property psp,
794 union power_supply_propval *val) 794 union power_supply_propval *val)
795 { 795 {
796 struct ab8500_btemp *di; 796 struct ab8500_btemp *di;
797 797
798 di = to_ab8500_btemp_device_info(psy); 798 di = to_ab8500_btemp_device_info(psy);
799 799
800 switch (psp) { 800 switch (psp) {
801 case POWER_SUPPLY_PROP_PRESENT: 801 case POWER_SUPPLY_PROP_PRESENT:
802 case POWER_SUPPLY_PROP_ONLINE: 802 case POWER_SUPPLY_PROP_ONLINE:
803 if (di->events.batt_rem) 803 if (di->events.batt_rem)
804 val->intval = 0; 804 val->intval = 0;
805 else 805 else
806 val->intval = 1; 806 val->intval = 1;
807 break; 807 break;
808 case POWER_SUPPLY_PROP_TECHNOLOGY: 808 case POWER_SUPPLY_PROP_TECHNOLOGY:
809 val->intval = di->bat->bat_type[di->bat->batt_id].name; 809 val->intval = di->bat->bat_type[di->bat->batt_id].name;
810 break; 810 break;
811 case POWER_SUPPLY_PROP_TEMP: 811 case POWER_SUPPLY_PROP_TEMP:
812 val->intval = ab8500_btemp_get_temp(di); 812 val->intval = ab8500_btemp_get_temp(di);
813 break; 813 break;
814 default: 814 default:
815 return -EINVAL; 815 return -EINVAL;
816 } 816 }
817 return 0; 817 return 0;
818 } 818 }
819 819
820 static int ab8500_btemp_get_ext_psy_data(struct device *dev, void *data) 820 static int ab8500_btemp_get_ext_psy_data(struct device *dev, void *data)
821 { 821 {
822 struct power_supply *psy; 822 struct power_supply *psy;
823 struct power_supply *ext; 823 struct power_supply *ext;
824 struct ab8500_btemp *di; 824 struct ab8500_btemp *di;
825 union power_supply_propval ret; 825 union power_supply_propval ret;
826 int i, j; 826 int i, j;
827 bool psy_found = false; 827 bool psy_found = false;
828 828
829 psy = (struct power_supply *)data; 829 psy = (struct power_supply *)data;
830 ext = dev_get_drvdata(dev); 830 ext = dev_get_drvdata(dev);
831 di = to_ab8500_btemp_device_info(psy); 831 di = to_ab8500_btemp_device_info(psy);
832 832
833 /* 833 /*
834 * For all psy where the name of your driver 834 * For all psy where the name of your driver
835 * appears in any supplied_to 835 * appears in any supplied_to
836 */ 836 */
837 for (i = 0; i < ext->num_supplicants; i++) { 837 for (i = 0; i < ext->num_supplicants; i++) {
838 if (!strcmp(ext->supplied_to[i], psy->name)) 838 if (!strcmp(ext->supplied_to[i], psy->name))
839 psy_found = true; 839 psy_found = true;
840 } 840 }
841 841
842 if (!psy_found) 842 if (!psy_found)
843 return 0; 843 return 0;
844 844
845 /* Go through all properties for the psy */ 845 /* Go through all properties for the psy */
846 for (j = 0; j < ext->num_properties; j++) { 846 for (j = 0; j < ext->num_properties; j++) {
847 enum power_supply_property prop; 847 enum power_supply_property prop;
848 prop = ext->properties[j]; 848 prop = ext->properties[j];
849 849
850 if (ext->get_property(ext, prop, &ret)) 850 if (ext->get_property(ext, prop, &ret))
851 continue; 851 continue;
852 852
853 switch (prop) { 853 switch (prop) {
854 case POWER_SUPPLY_PROP_PRESENT: 854 case POWER_SUPPLY_PROP_PRESENT:
855 switch (ext->type) { 855 switch (ext->type) {
856 case POWER_SUPPLY_TYPE_MAINS: 856 case POWER_SUPPLY_TYPE_MAINS:
857 /* AC disconnected */ 857 /* AC disconnected */
858 if (!ret.intval && di->events.ac_conn) { 858 if (!ret.intval && di->events.ac_conn) {
859 di->events.ac_conn = false; 859 di->events.ac_conn = false;
860 } 860 }
861 /* AC connected */ 861 /* AC connected */
862 else if (ret.intval && !di->events.ac_conn) { 862 else if (ret.intval && !di->events.ac_conn) {
863 di->events.ac_conn = true; 863 di->events.ac_conn = true;
864 if (!di->events.usb_conn) 864 if (!di->events.usb_conn)
865 ab8500_btemp_periodic(di, true); 865 ab8500_btemp_periodic(di, true);
866 } 866 }
867 break; 867 break;
868 case POWER_SUPPLY_TYPE_USB: 868 case POWER_SUPPLY_TYPE_USB:
869 /* USB disconnected */ 869 /* USB disconnected */
870 if (!ret.intval && di->events.usb_conn) { 870 if (!ret.intval && di->events.usb_conn) {
871 di->events.usb_conn = false; 871 di->events.usb_conn = false;
872 } 872 }
873 /* USB connected */ 873 /* USB connected */
874 else if (ret.intval && !di->events.usb_conn) { 874 else if (ret.intval && !di->events.usb_conn) {
875 di->events.usb_conn = true; 875 di->events.usb_conn = true;
876 if (!di->events.ac_conn) 876 if (!di->events.ac_conn)
877 ab8500_btemp_periodic(di, true); 877 ab8500_btemp_periodic(di, true);
878 } 878 }
879 break; 879 break;
880 default: 880 default:
881 break; 881 break;
882 } 882 }
883 break; 883 break;
884 default: 884 default:
885 break; 885 break;
886 } 886 }
887 } 887 }
888 return 0; 888 return 0;
889 } 889 }
890 890
891 /** 891 /**
892 * ab8500_btemp_external_power_changed() - callback for power supply changes 892 * ab8500_btemp_external_power_changed() - callback for power supply changes
893 * @psy: pointer to the structure power_supply 893 * @psy: pointer to the structure power_supply
894 * 894 *
895 * This function is pointing to the function pointer external_power_changed 895 * This function is pointing to the function pointer external_power_changed
896 * of the structure power_supply. 896 * of the structure power_supply.
897 * This function gets executed when there is a change in the external power 897 * This function gets executed when there is a change in the external power
898 * supply to the btemp. 898 * supply to the btemp.
899 */ 899 */
900 static void ab8500_btemp_external_power_changed(struct power_supply *psy) 900 static void ab8500_btemp_external_power_changed(struct power_supply *psy)
901 { 901 {
902 struct ab8500_btemp *di = to_ab8500_btemp_device_info(psy); 902 struct ab8500_btemp *di = to_ab8500_btemp_device_info(psy);
903 903
904 class_for_each_device(power_supply_class, NULL, 904 class_for_each_device(power_supply_class, NULL,
905 &di->btemp_psy, ab8500_btemp_get_ext_psy_data); 905 &di->btemp_psy, ab8500_btemp_get_ext_psy_data);
906 } 906 }
907 907
908 /* ab8500 btemp driver interrupts and their respective isr */ 908 /* ab8500 btemp driver interrupts and their respective isr */
909 static struct ab8500_btemp_interrupts ab8500_btemp_irq[] = { 909 static struct ab8500_btemp_interrupts ab8500_btemp_irq[] = {
910 {"BAT_CTRL_INDB", ab8500_btemp_batctrlindb_handler}, 910 {"BAT_CTRL_INDB", ab8500_btemp_batctrlindb_handler},
911 {"BTEMP_LOW", ab8500_btemp_templow_handler}, 911 {"BTEMP_LOW", ab8500_btemp_templow_handler},
912 {"BTEMP_HIGH", ab8500_btemp_temphigh_handler}, 912 {"BTEMP_HIGH", ab8500_btemp_temphigh_handler},
913 {"BTEMP_LOW_MEDIUM", ab8500_btemp_lowmed_handler}, 913 {"BTEMP_LOW_MEDIUM", ab8500_btemp_lowmed_handler},
914 {"BTEMP_MEDIUM_HIGH", ab8500_btemp_medhigh_handler}, 914 {"BTEMP_MEDIUM_HIGH", ab8500_btemp_medhigh_handler},
915 }; 915 };
916 916
917 #if defined(CONFIG_PM) 917 #if defined(CONFIG_PM)
918 static int ab8500_btemp_resume(struct platform_device *pdev) 918 static int ab8500_btemp_resume(struct platform_device *pdev)
919 { 919 {
920 struct ab8500_btemp *di = platform_get_drvdata(pdev); 920 struct ab8500_btemp *di = platform_get_drvdata(pdev);
921 921
922 ab8500_btemp_periodic(di, true); 922 ab8500_btemp_periodic(di, true);
923 923
924 return 0; 924 return 0;
925 } 925 }
926 926
927 static int ab8500_btemp_suspend(struct platform_device *pdev, 927 static int ab8500_btemp_suspend(struct platform_device *pdev,
928 pm_message_t state) 928 pm_message_t state)
929 { 929 {
930 struct ab8500_btemp *di = platform_get_drvdata(pdev); 930 struct ab8500_btemp *di = platform_get_drvdata(pdev);
931 931
932 ab8500_btemp_periodic(di, false); 932 ab8500_btemp_periodic(di, false);
933 933
934 return 0; 934 return 0;
935 } 935 }
936 #else 936 #else
937 #define ab8500_btemp_suspend NULL 937 #define ab8500_btemp_suspend NULL
938 #define ab8500_btemp_resume NULL 938 #define ab8500_btemp_resume NULL
939 #endif 939 #endif
940 940
941 static int __devexit ab8500_btemp_remove(struct platform_device *pdev) 941 static int __devexit ab8500_btemp_remove(struct platform_device *pdev)
942 { 942 {
943 struct ab8500_btemp *di = platform_get_drvdata(pdev); 943 struct ab8500_btemp *di = platform_get_drvdata(pdev);
944 int i, irq; 944 int i, irq;
945 945
946 /* Disable interrupts */ 946 /* Disable interrupts */
947 for (i = 0; i < ARRAY_SIZE(ab8500_btemp_irq); i++) { 947 for (i = 0; i < ARRAY_SIZE(ab8500_btemp_irq); i++) {
948 irq = platform_get_irq_byname(pdev, ab8500_btemp_irq[i].name); 948 irq = platform_get_irq_byname(pdev, ab8500_btemp_irq[i].name);
949 free_irq(irq, di); 949 free_irq(irq, di);
950 } 950 }
951 951
952 /* Delete the work queue */ 952 /* Delete the work queue */
953 destroy_workqueue(di->btemp_wq); 953 destroy_workqueue(di->btemp_wq);
954 954
955 flush_scheduled_work(); 955 flush_scheduled_work();
956 power_supply_unregister(&di->btemp_psy); 956 power_supply_unregister(&di->btemp_psy);
957 platform_set_drvdata(pdev, NULL); 957 platform_set_drvdata(pdev, NULL);
958 kfree(di); 958 kfree(di);
959 959
960 return 0; 960 return 0;
961 } 961 }
962 962
963 static int __devinit ab8500_btemp_probe(struct platform_device *pdev) 963 static int __devinit ab8500_btemp_probe(struct platform_device *pdev)
964 { 964 {
965 int irq, i, ret = 0; 965 int irq, i, ret = 0;
966 u8 val; 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 if (!plat_data) {
970 kzalloc(sizeof(struct ab8500_btemp), GFP_KERNEL); 971 dev_err(&pdev->dev, "No platform data\n");
972 return -EINVAL;
973 }
974
975 di = kzalloc(sizeof(*di), GFP_KERNEL);
971 if (!di) 976 if (!di)
972 return -ENOMEM; 977 return -ENOMEM;
973 978
974 /* get parent data */ 979 /* get parent data */
975 di->dev = &pdev->dev; 980 di->dev = &pdev->dev;
976 di->parent = dev_get_drvdata(pdev->dev.parent); 981 di->parent = dev_get_drvdata(pdev->dev.parent);
977 di->gpadc = ab8500_gpadc_get("ab8500-gpadc.0"); 982 di->gpadc = ab8500_gpadc_get("ab8500-gpadc.0");
978 983
979 /* get btemp specific platform data */ 984 /* get btemp specific platform data */
980 plat_data = pdev->dev.platform_data; 985 di->pdata = plat_data->btemp;
981 if (!plat_data || !plat_data->btemp) { 986 if (!di->pdata) {
982 dev_err(di->dev, "no btemp platform data supplied\n"); 987 dev_err(di->dev, "no btemp platform data supplied\n");
983 ret = -EINVAL; 988 ret = -EINVAL;
984 goto free_device_info; 989 goto free_device_info;
985 } 990 }
986 di->pdata = plat_data->btemp;
987 991
988 /* get battery specific platform data */ 992 /* get battery specific platform data */
989 di->bat = plat_data->battery; 993 di->bat = plat_data->battery;
990 if (!di->bat) { 994 if (!di->bat) {
991 dev_err(di->dev, "no battery platform data supplied\n"); 995 dev_err(di->dev, "no battery platform data supplied\n");
992 ret = -EINVAL; 996 ret = -EINVAL;
993 goto free_device_info; 997 goto free_device_info;
994 } 998 }
995 999
996 /* BTEMP supply */ 1000 /* BTEMP supply */
997 di->btemp_psy.name = "ab8500_btemp"; 1001 di->btemp_psy.name = "ab8500_btemp";
998 di->btemp_psy.type = POWER_SUPPLY_TYPE_BATTERY; 1002 di->btemp_psy.type = POWER_SUPPLY_TYPE_BATTERY;
999 di->btemp_psy.properties = ab8500_btemp_props; 1003 di->btemp_psy.properties = ab8500_btemp_props;
1000 di->btemp_psy.num_properties = ARRAY_SIZE(ab8500_btemp_props); 1004 di->btemp_psy.num_properties = ARRAY_SIZE(ab8500_btemp_props);
1001 di->btemp_psy.get_property = ab8500_btemp_get_property; 1005 di->btemp_psy.get_property = ab8500_btemp_get_property;
1002 di->btemp_psy.supplied_to = di->pdata->supplied_to; 1006 di->btemp_psy.supplied_to = di->pdata->supplied_to;
1003 di->btemp_psy.num_supplicants = di->pdata->num_supplicants; 1007 di->btemp_psy.num_supplicants = di->pdata->num_supplicants;
1004 di->btemp_psy.external_power_changed = 1008 di->btemp_psy.external_power_changed =
1005 ab8500_btemp_external_power_changed; 1009 ab8500_btemp_external_power_changed;
1006 1010
1007 1011
1008 /* Create a work queue for the btemp */ 1012 /* Create a work queue for the btemp */
1009 di->btemp_wq = 1013 di->btemp_wq =
1010 create_singlethread_workqueue("ab8500_btemp_wq"); 1014 create_singlethread_workqueue("ab8500_btemp_wq");
1011 if (di->btemp_wq == NULL) { 1015 if (di->btemp_wq == NULL) {
1012 dev_err(di->dev, "failed to create work queue\n"); 1016 dev_err(di->dev, "failed to create work queue\n");
1013 goto free_device_info; 1017 goto free_device_info;
1014 } 1018 }
1015 1019
1016 /* Init work for measuring temperature periodically */ 1020 /* Init work for measuring temperature periodically */
1017 INIT_DELAYED_WORK_DEFERRABLE(&di->btemp_periodic_work, 1021 INIT_DELAYED_WORK_DEFERRABLE(&di->btemp_periodic_work,
1018 ab8500_btemp_periodic_work); 1022 ab8500_btemp_periodic_work);
1019 1023
1020 /* Identify the battery */ 1024 /* Identify the battery */
1021 if (ab8500_btemp_id(di) < 0) 1025 if (ab8500_btemp_id(di) < 0)
1022 dev_warn(di->dev, "failed to identify the battery\n"); 1026 dev_warn(di->dev, "failed to identify the battery\n");
1023 1027
1024 /* Set BTEMP thermal limits. Low and Med are fixed */ 1028 /* Set BTEMP thermal limits. Low and Med are fixed */
1025 di->btemp_ranges.btemp_low_limit = BTEMP_THERMAL_LOW_LIMIT; 1029 di->btemp_ranges.btemp_low_limit = BTEMP_THERMAL_LOW_LIMIT;
1026 di->btemp_ranges.btemp_med_limit = BTEMP_THERMAL_MED_LIMIT; 1030 di->btemp_ranges.btemp_med_limit = BTEMP_THERMAL_MED_LIMIT;
1027 1031
1028 ret = abx500_get_register_interruptible(di->dev, AB8500_CHARGER, 1032 ret = abx500_get_register_interruptible(di->dev, AB8500_CHARGER,
1029 AB8500_BTEMP_HIGH_TH, &val); 1033 AB8500_BTEMP_HIGH_TH, &val);
1030 if (ret < 0) { 1034 if (ret < 0) {
1031 dev_err(di->dev, "%s ab8500 read failed\n", __func__); 1035 dev_err(di->dev, "%s ab8500 read failed\n", __func__);
1032 goto free_btemp_wq; 1036 goto free_btemp_wq;
1033 } 1037 }
1034 switch (val) { 1038 switch (val) {
1035 case BTEMP_HIGH_TH_57_0: 1039 case BTEMP_HIGH_TH_57_0:
1036 case BTEMP_HIGH_TH_57_1: 1040 case BTEMP_HIGH_TH_57_1:
1037 di->btemp_ranges.btemp_high_limit = 1041 di->btemp_ranges.btemp_high_limit =
1038 BTEMP_THERMAL_HIGH_LIMIT_57; 1042 BTEMP_THERMAL_HIGH_LIMIT_57;
1039 break; 1043 break;
1040 case BTEMP_HIGH_TH_52: 1044 case BTEMP_HIGH_TH_52:
1041 di->btemp_ranges.btemp_high_limit = 1045 di->btemp_ranges.btemp_high_limit =
1042 BTEMP_THERMAL_HIGH_LIMIT_52; 1046 BTEMP_THERMAL_HIGH_LIMIT_52;
1043 break; 1047 break;
1044 case BTEMP_HIGH_TH_62: 1048 case BTEMP_HIGH_TH_62:
1045 di->btemp_ranges.btemp_high_limit = 1049 di->btemp_ranges.btemp_high_limit =
1046 BTEMP_THERMAL_HIGH_LIMIT_62; 1050 BTEMP_THERMAL_HIGH_LIMIT_62;
1047 break; 1051 break;
1048 } 1052 }
1049 1053
1050 /* Register BTEMP power supply class */ 1054 /* Register BTEMP power supply class */
1051 ret = power_supply_register(di->dev, &di->btemp_psy); 1055 ret = power_supply_register(di->dev, &di->btemp_psy);
1052 if (ret) { 1056 if (ret) {
1053 dev_err(di->dev, "failed to register BTEMP psy\n"); 1057 dev_err(di->dev, "failed to register BTEMP psy\n");
1054 goto free_btemp_wq; 1058 goto free_btemp_wq;
1055 } 1059 }
1056 1060
1057 /* Register interrupts */ 1061 /* Register interrupts */
1058 for (i = 0; i < ARRAY_SIZE(ab8500_btemp_irq); i++) { 1062 for (i = 0; i < ARRAY_SIZE(ab8500_btemp_irq); i++) {
1059 irq = platform_get_irq_byname(pdev, ab8500_btemp_irq[i].name); 1063 irq = platform_get_irq_byname(pdev, ab8500_btemp_irq[i].name);
1060 ret = request_threaded_irq(irq, NULL, ab8500_btemp_irq[i].isr, 1064 ret = request_threaded_irq(irq, NULL, ab8500_btemp_irq[i].isr,
1061 IRQF_SHARED | IRQF_NO_SUSPEND, 1065 IRQF_SHARED | IRQF_NO_SUSPEND,
1062 ab8500_btemp_irq[i].name, di); 1066 ab8500_btemp_irq[i].name, di);
1063 1067
1064 if (ret) { 1068 if (ret) {
1065 dev_err(di->dev, "failed to request %s IRQ %d: %d\n" 1069 dev_err(di->dev, "failed to request %s IRQ %d: %d\n"
1066 , ab8500_btemp_irq[i].name, irq, ret); 1070 , ab8500_btemp_irq[i].name, irq, ret);
1067 goto free_irq; 1071 goto free_irq;
1068 } 1072 }
1069 dev_dbg(di->dev, "Requested %s IRQ %d: %d\n", 1073 dev_dbg(di->dev, "Requested %s IRQ %d: %d\n",
1070 ab8500_btemp_irq[i].name, irq, ret); 1074 ab8500_btemp_irq[i].name, irq, ret);
1071 } 1075 }
1072 1076
1073 platform_set_drvdata(pdev, di); 1077 platform_set_drvdata(pdev, di);
1074 1078
1075 /* Kick off periodic temperature measurements */ 1079 /* Kick off periodic temperature measurements */
1076 ab8500_btemp_periodic(di, true); 1080 ab8500_btemp_periodic(di, true);
1077 list_add_tail(&di->node, &ab8500_btemp_list); 1081 list_add_tail(&di->node, &ab8500_btemp_list);
1078 1082
1079 return ret; 1083 return ret;
1080 1084
1081 free_irq: 1085 free_irq:
1082 power_supply_unregister(&di->btemp_psy); 1086 power_supply_unregister(&di->btemp_psy);
1083 1087
1084 /* We also have to free all successfully registered irqs */ 1088 /* We also have to free all successfully registered irqs */
1085 for (i = i - 1; i >= 0; i--) { 1089 for (i = i - 1; i >= 0; i--) {
1086 irq = platform_get_irq_byname(pdev, ab8500_btemp_irq[i].name); 1090 irq = platform_get_irq_byname(pdev, ab8500_btemp_irq[i].name);
1087 free_irq(irq, di); 1091 free_irq(irq, di);
1088 } 1092 }
1089 free_btemp_wq: 1093 free_btemp_wq:
1090 destroy_workqueue(di->btemp_wq); 1094 destroy_workqueue(di->btemp_wq);
1091 free_device_info: 1095 free_device_info:
1092 kfree(di); 1096 kfree(di);
1093 1097
1094 return ret; 1098 return ret;
1095 } 1099 }
1096 1100
1097 static struct platform_driver ab8500_btemp_driver = { 1101 static struct platform_driver ab8500_btemp_driver = {
1098 .probe = ab8500_btemp_probe, 1102 .probe = ab8500_btemp_probe,
1099 .remove = __devexit_p(ab8500_btemp_remove), 1103 .remove = __devexit_p(ab8500_btemp_remove),
1100 .suspend = ab8500_btemp_suspend, 1104 .suspend = ab8500_btemp_suspend,
1101 .resume = ab8500_btemp_resume, 1105 .resume = ab8500_btemp_resume,
1102 .driver = { 1106 .driver = {
1103 .name = "ab8500-btemp", 1107 .name = "ab8500-btemp",
1104 .owner = THIS_MODULE, 1108 .owner = THIS_MODULE,
1105 }, 1109 },
1106 }; 1110 };
1107 1111
1108 static int __init ab8500_btemp_init(void) 1112 static int __init ab8500_btemp_init(void)
1109 { 1113 {
1110 return platform_driver_register(&ab8500_btemp_driver); 1114 return platform_driver_register(&ab8500_btemp_driver);
1111 } 1115 }
1112 1116
1113 static void __exit ab8500_btemp_exit(void) 1117 static void __exit ab8500_btemp_exit(void)
1114 { 1118 {
1115 platform_driver_unregister(&ab8500_btemp_driver); 1119 platform_driver_unregister(&ab8500_btemp_driver);
1116 } 1120 }
1117 1121
1118 subsys_initcall_sync(ab8500_btemp_init); 1122 subsys_initcall_sync(ab8500_btemp_init);
1119 module_exit(ab8500_btemp_exit); 1123 module_exit(ab8500_btemp_exit);
1120 1124
1121 MODULE_LICENSE("GPL v2"); 1125 MODULE_LICENSE("GPL v2");
1122 MODULE_AUTHOR("Johan Palsson, Karl Komierowski, Arun R Murthy"); 1126 MODULE_AUTHOR("Johan Palsson, Karl Komierowski, Arun R Murthy");
1123 MODULE_ALIAS("platform:ab8500-btemp"); 1127 MODULE_ALIAS("platform:ab8500-btemp");
1124 MODULE_DESCRIPTION("AB8500 battery temperature driver"); 1128 MODULE_DESCRIPTION("AB8500 battery temperature driver");
drivers/power/ab8500_charger.c
1 /* 1 /*
2 * Copyright (C) ST-Ericsson SA 2012 2 * Copyright (C) ST-Ericsson SA 2012
3 * 3 *
4 * Charger driver for AB8500 4 * Charger driver for AB8500
5 * 5 *
6 * License Terms: GNU General Public License v2 6 * License Terms: GNU General Public License v2
7 * Author: 7 * Author:
8 * Johan Palsson <johan.palsson@stericsson.com> 8 * Johan Palsson <johan.palsson@stericsson.com>
9 * Karl Komierowski <karl.komierowski@stericsson.com> 9 * Karl Komierowski <karl.komierowski@stericsson.com>
10 * Arun R Murthy <arun.murthy@stericsson.com> 10 * Arun R Murthy <arun.murthy@stericsson.com>
11 */ 11 */
12 12
13 #include <linux/init.h> 13 #include <linux/init.h>
14 #include <linux/module.h> 14 #include <linux/module.h>
15 #include <linux/device.h> 15 #include <linux/device.h>
16 #include <linux/interrupt.h> 16 #include <linux/interrupt.h>
17 #include <linux/delay.h> 17 #include <linux/delay.h>
18 #include <linux/slab.h> 18 #include <linux/slab.h>
19 #include <linux/platform_device.h> 19 #include <linux/platform_device.h>
20 #include <linux/power_supply.h> 20 #include <linux/power_supply.h>
21 #include <linux/completion.h> 21 #include <linux/completion.h>
22 #include <linux/regulator/consumer.h> 22 #include <linux/regulator/consumer.h>
23 #include <linux/err.h> 23 #include <linux/err.h>
24 #include <linux/workqueue.h> 24 #include <linux/workqueue.h>
25 #include <linux/kobject.h> 25 #include <linux/kobject.h>
26 #include <linux/mfd/abx500/ab8500.h> 26 #include <linux/mfd/abx500/ab8500.h>
27 #include <linux/mfd/abx500.h> 27 #include <linux/mfd/abx500.h>
28 #include <linux/mfd/abx500/ab8500-bm.h> 28 #include <linux/mfd/abx500/ab8500-bm.h>
29 #include <linux/mfd/abx500/ab8500-gpadc.h> 29 #include <linux/mfd/abx500/ab8500-gpadc.h>
30 #include <linux/mfd/abx500/ux500_chargalg.h> 30 #include <linux/mfd/abx500/ux500_chargalg.h>
31 #include <linux/usb/otg.h> 31 #include <linux/usb/otg.h>
32 32
33 /* Charger constants */ 33 /* Charger constants */
34 #define NO_PW_CONN 0 34 #define NO_PW_CONN 0
35 #define AC_PW_CONN 1 35 #define AC_PW_CONN 1
36 #define USB_PW_CONN 2 36 #define USB_PW_CONN 2
37 37
38 #define MAIN_WDOG_ENA 0x01 38 #define MAIN_WDOG_ENA 0x01
39 #define MAIN_WDOG_KICK 0x02 39 #define MAIN_WDOG_KICK 0x02
40 #define MAIN_WDOG_DIS 0x00 40 #define MAIN_WDOG_DIS 0x00
41 #define CHARG_WD_KICK 0x01 41 #define CHARG_WD_KICK 0x01
42 #define MAIN_CH_ENA 0x01 42 #define MAIN_CH_ENA 0x01
43 #define MAIN_CH_NO_OVERSHOOT_ENA_N 0x02 43 #define MAIN_CH_NO_OVERSHOOT_ENA_N 0x02
44 #define USB_CH_ENA 0x01 44 #define USB_CH_ENA 0x01
45 #define USB_CHG_NO_OVERSHOOT_ENA_N 0x02 45 #define USB_CHG_NO_OVERSHOOT_ENA_N 0x02
46 #define MAIN_CH_DET 0x01 46 #define MAIN_CH_DET 0x01
47 #define MAIN_CH_CV_ON 0x04 47 #define MAIN_CH_CV_ON 0x04
48 #define USB_CH_CV_ON 0x08 48 #define USB_CH_CV_ON 0x08
49 #define VBUS_DET_DBNC100 0x02 49 #define VBUS_DET_DBNC100 0x02
50 #define VBUS_DET_DBNC1 0x01 50 #define VBUS_DET_DBNC1 0x01
51 #define OTP_ENABLE_WD 0x01 51 #define OTP_ENABLE_WD 0x01
52 52
53 #define MAIN_CH_INPUT_CURR_SHIFT 4 53 #define MAIN_CH_INPUT_CURR_SHIFT 4
54 #define VBUS_IN_CURR_LIM_SHIFT 4 54 #define VBUS_IN_CURR_LIM_SHIFT 4
55 55
56 #define LED_INDICATOR_PWM_ENA 0x01 56 #define LED_INDICATOR_PWM_ENA 0x01
57 #define LED_INDICATOR_PWM_DIS 0x00 57 #define LED_INDICATOR_PWM_DIS 0x00
58 #define LED_IND_CUR_5MA 0x04 58 #define LED_IND_CUR_5MA 0x04
59 #define LED_INDICATOR_PWM_DUTY_252_256 0xBF 59 #define LED_INDICATOR_PWM_DUTY_252_256 0xBF
60 60
61 /* HW failure constants */ 61 /* HW failure constants */
62 #define MAIN_CH_TH_PROT 0x02 62 #define MAIN_CH_TH_PROT 0x02
63 #define VBUS_CH_NOK 0x08 63 #define VBUS_CH_NOK 0x08
64 #define USB_CH_TH_PROT 0x02 64 #define USB_CH_TH_PROT 0x02
65 #define VBUS_OVV_TH 0x01 65 #define VBUS_OVV_TH 0x01
66 #define MAIN_CH_NOK 0x01 66 #define MAIN_CH_NOK 0x01
67 #define VBUS_DET 0x80 67 #define VBUS_DET 0x80
68 68
69 /* UsbLineStatus register bit masks */ 69 /* UsbLineStatus register bit masks */
70 #define AB8500_USB_LINK_STATUS 0x78 70 #define AB8500_USB_LINK_STATUS 0x78
71 #define AB8500_STD_HOST_SUSP 0x18 71 #define AB8500_STD_HOST_SUSP 0x18
72 72
73 /* Watchdog timeout constant */ 73 /* Watchdog timeout constant */
74 #define WD_TIMER 0x30 /* 4min */ 74 #define WD_TIMER 0x30 /* 4min */
75 #define WD_KICK_INTERVAL (60 * HZ) 75 #define WD_KICK_INTERVAL (60 * HZ)
76 76
77 /* Lowest charger voltage is 3.39V -> 0x4E */ 77 /* Lowest charger voltage is 3.39V -> 0x4E */
78 #define LOW_VOLT_REG 0x4E 78 #define LOW_VOLT_REG 0x4E
79 79
80 /* UsbLineStatus register - usb types */ 80 /* UsbLineStatus register - usb types */
81 enum ab8500_charger_link_status { 81 enum ab8500_charger_link_status {
82 USB_STAT_NOT_CONFIGURED, 82 USB_STAT_NOT_CONFIGURED,
83 USB_STAT_STD_HOST_NC, 83 USB_STAT_STD_HOST_NC,
84 USB_STAT_STD_HOST_C_NS, 84 USB_STAT_STD_HOST_C_NS,
85 USB_STAT_STD_HOST_C_S, 85 USB_STAT_STD_HOST_C_S,
86 USB_STAT_HOST_CHG_NM, 86 USB_STAT_HOST_CHG_NM,
87 USB_STAT_HOST_CHG_HS, 87 USB_STAT_HOST_CHG_HS,
88 USB_STAT_HOST_CHG_HS_CHIRP, 88 USB_STAT_HOST_CHG_HS_CHIRP,
89 USB_STAT_DEDICATED_CHG, 89 USB_STAT_DEDICATED_CHG,
90 USB_STAT_ACA_RID_A, 90 USB_STAT_ACA_RID_A,
91 USB_STAT_ACA_RID_B, 91 USB_STAT_ACA_RID_B,
92 USB_STAT_ACA_RID_C_NM, 92 USB_STAT_ACA_RID_C_NM,
93 USB_STAT_ACA_RID_C_HS, 93 USB_STAT_ACA_RID_C_HS,
94 USB_STAT_ACA_RID_C_HS_CHIRP, 94 USB_STAT_ACA_RID_C_HS_CHIRP,
95 USB_STAT_HM_IDGND, 95 USB_STAT_HM_IDGND,
96 USB_STAT_RESERVED, 96 USB_STAT_RESERVED,
97 USB_STAT_NOT_VALID_LINK, 97 USB_STAT_NOT_VALID_LINK,
98 }; 98 };
99 99
100 enum ab8500_usb_state { 100 enum ab8500_usb_state {
101 AB8500_BM_USB_STATE_RESET_HS, /* HighSpeed Reset */ 101 AB8500_BM_USB_STATE_RESET_HS, /* HighSpeed Reset */
102 AB8500_BM_USB_STATE_RESET_FS, /* FullSpeed/LowSpeed Reset */ 102 AB8500_BM_USB_STATE_RESET_FS, /* FullSpeed/LowSpeed Reset */
103 AB8500_BM_USB_STATE_CONFIGURED, 103 AB8500_BM_USB_STATE_CONFIGURED,
104 AB8500_BM_USB_STATE_SUSPEND, 104 AB8500_BM_USB_STATE_SUSPEND,
105 AB8500_BM_USB_STATE_RESUME, 105 AB8500_BM_USB_STATE_RESUME,
106 AB8500_BM_USB_STATE_MAX, 106 AB8500_BM_USB_STATE_MAX,
107 }; 107 };
108 108
109 /* VBUS input current limits supported in AB8500 in mA */ 109 /* VBUS input current limits supported in AB8500 in mA */
110 #define USB_CH_IP_CUR_LVL_0P05 50 110 #define USB_CH_IP_CUR_LVL_0P05 50
111 #define USB_CH_IP_CUR_LVL_0P09 98 111 #define USB_CH_IP_CUR_LVL_0P09 98
112 #define USB_CH_IP_CUR_LVL_0P19 193 112 #define USB_CH_IP_CUR_LVL_0P19 193
113 #define USB_CH_IP_CUR_LVL_0P29 290 113 #define USB_CH_IP_CUR_LVL_0P29 290
114 #define USB_CH_IP_CUR_LVL_0P38 380 114 #define USB_CH_IP_CUR_LVL_0P38 380
115 #define USB_CH_IP_CUR_LVL_0P45 450 115 #define USB_CH_IP_CUR_LVL_0P45 450
116 #define USB_CH_IP_CUR_LVL_0P5 500 116 #define USB_CH_IP_CUR_LVL_0P5 500
117 #define USB_CH_IP_CUR_LVL_0P6 600 117 #define USB_CH_IP_CUR_LVL_0P6 600
118 #define USB_CH_IP_CUR_LVL_0P7 700 118 #define USB_CH_IP_CUR_LVL_0P7 700
119 #define USB_CH_IP_CUR_LVL_0P8 800 119 #define USB_CH_IP_CUR_LVL_0P8 800
120 #define USB_CH_IP_CUR_LVL_0P9 900 120 #define USB_CH_IP_CUR_LVL_0P9 900
121 #define USB_CH_IP_CUR_LVL_1P0 1000 121 #define USB_CH_IP_CUR_LVL_1P0 1000
122 #define USB_CH_IP_CUR_LVL_1P1 1100 122 #define USB_CH_IP_CUR_LVL_1P1 1100
123 #define USB_CH_IP_CUR_LVL_1P3 1300 123 #define USB_CH_IP_CUR_LVL_1P3 1300
124 #define USB_CH_IP_CUR_LVL_1P4 1400 124 #define USB_CH_IP_CUR_LVL_1P4 1400
125 #define USB_CH_IP_CUR_LVL_1P5 1500 125 #define USB_CH_IP_CUR_LVL_1P5 1500
126 126
127 #define VBAT_TRESH_IP_CUR_RED 3800 127 #define VBAT_TRESH_IP_CUR_RED 3800
128 128
129 #define to_ab8500_charger_usb_device_info(x) container_of((x), \ 129 #define to_ab8500_charger_usb_device_info(x) container_of((x), \
130 struct ab8500_charger, usb_chg) 130 struct ab8500_charger, usb_chg)
131 #define to_ab8500_charger_ac_device_info(x) container_of((x), \ 131 #define to_ab8500_charger_ac_device_info(x) container_of((x), \
132 struct ab8500_charger, ac_chg) 132 struct ab8500_charger, ac_chg)
133 133
134 /** 134 /**
135 * struct ab8500_charger_interrupts - ab8500 interupts 135 * struct ab8500_charger_interrupts - ab8500 interupts
136 * @name: name of the interrupt 136 * @name: name of the interrupt
137 * @isr function pointer to the isr 137 * @isr function pointer to the isr
138 */ 138 */
139 struct ab8500_charger_interrupts { 139 struct ab8500_charger_interrupts {
140 char *name; 140 char *name;
141 irqreturn_t (*isr)(int irq, void *data); 141 irqreturn_t (*isr)(int irq, void *data);
142 }; 142 };
143 143
144 struct ab8500_charger_info { 144 struct ab8500_charger_info {
145 int charger_connected; 145 int charger_connected;
146 int charger_online; 146 int charger_online;
147 int charger_voltage; 147 int charger_voltage;
148 int cv_active; 148 int cv_active;
149 bool wd_expired; 149 bool wd_expired;
150 }; 150 };
151 151
152 struct ab8500_charger_event_flags { 152 struct ab8500_charger_event_flags {
153 bool mainextchnotok; 153 bool mainextchnotok;
154 bool main_thermal_prot; 154 bool main_thermal_prot;
155 bool usb_thermal_prot; 155 bool usb_thermal_prot;
156 bool vbus_ovv; 156 bool vbus_ovv;
157 bool usbchargernotok; 157 bool usbchargernotok;
158 bool chgwdexp; 158 bool chgwdexp;
159 bool vbus_collapse; 159 bool vbus_collapse;
160 }; 160 };
161 161
162 struct ab8500_charger_usb_state { 162 struct ab8500_charger_usb_state {
163 bool usb_changed; 163 bool usb_changed;
164 int usb_current; 164 int usb_current;
165 enum ab8500_usb_state state; 165 enum ab8500_usb_state state;
166 spinlock_t usb_lock; 166 spinlock_t usb_lock;
167 }; 167 };
168 168
169 /** 169 /**
170 * struct ab8500_charger - ab8500 Charger device information 170 * struct ab8500_charger - ab8500 Charger device information
171 * @dev: Pointer to the structure device 171 * @dev: Pointer to the structure device
172 * @max_usb_in_curr: Max USB charger input current 172 * @max_usb_in_curr: Max USB charger input current
173 * @vbus_detected: VBUS detected 173 * @vbus_detected: VBUS detected
174 * @vbus_detected_start: 174 * @vbus_detected_start:
175 * VBUS detected during startup 175 * VBUS detected during startup
176 * @ac_conn: This will be true when the AC charger has been plugged 176 * @ac_conn: This will be true when the AC charger has been plugged
177 * @vddadc_en_ac: Indicate if VDD ADC supply is enabled because AC 177 * @vddadc_en_ac: Indicate if VDD ADC supply is enabled because AC
178 * charger is enabled 178 * charger is enabled
179 * @vddadc_en_usb: Indicate if VDD ADC supply is enabled because USB 179 * @vddadc_en_usb: Indicate if VDD ADC supply is enabled because USB
180 * charger is enabled 180 * charger is enabled
181 * @vbat Battery voltage 181 * @vbat Battery voltage
182 * @old_vbat Previously measured battery voltage 182 * @old_vbat Previously measured battery voltage
183 * @autopower Indicate if we should have automatic pwron after pwrloss 183 * @autopower Indicate if we should have automatic pwron after pwrloss
184 * @parent: Pointer to the struct ab8500 184 * @parent: Pointer to the struct ab8500
185 * @gpadc: Pointer to the struct gpadc 185 * @gpadc: Pointer to the struct gpadc
186 * @pdata: Pointer to the abx500_charger platform data 186 * @pdata: Pointer to the abx500_charger platform data
187 * @bat: Pointer to the abx500_bm platform data 187 * @bat: Pointer to the abx500_bm platform data
188 * @flags: Structure for information about events triggered 188 * @flags: Structure for information about events triggered
189 * @usb_state: Structure for usb stack information 189 * @usb_state: Structure for usb stack information
190 * @ac_chg: AC charger power supply 190 * @ac_chg: AC charger power supply
191 * @usb_chg: USB charger power supply 191 * @usb_chg: USB charger power supply
192 * @ac: Structure that holds the AC charger properties 192 * @ac: Structure that holds the AC charger properties
193 * @usb: Structure that holds the USB charger properties 193 * @usb: Structure that holds the USB charger properties
194 * @regu: Pointer to the struct regulator 194 * @regu: Pointer to the struct regulator
195 * @charger_wq: Work queue for the IRQs and checking HW state 195 * @charger_wq: Work queue for the IRQs and checking HW state
196 * @check_vbat_work Work for checking vbat threshold to adjust vbus current 196 * @check_vbat_work Work for checking vbat threshold to adjust vbus current
197 * @check_hw_failure_work: Work for checking HW state 197 * @check_hw_failure_work: Work for checking HW state
198 * @check_usbchgnotok_work: Work for checking USB charger not ok status 198 * @check_usbchgnotok_work: Work for checking USB charger not ok status
199 * @kick_wd_work: Work for kicking the charger watchdog in case 199 * @kick_wd_work: Work for kicking the charger watchdog in case
200 * of ABB rev 1.* due to the watchog logic bug 200 * of ABB rev 1.* due to the watchog logic bug
201 * @ac_work: Work for checking AC charger connection 201 * @ac_work: Work for checking AC charger connection
202 * @detect_usb_type_work: Work for detecting the USB type connected 202 * @detect_usb_type_work: Work for detecting the USB type connected
203 * @usb_link_status_work: Work for checking the new USB link status 203 * @usb_link_status_work: Work for checking the new USB link status
204 * @usb_state_changed_work: Work for checking USB state 204 * @usb_state_changed_work: Work for checking USB state
205 * @check_main_thermal_prot_work: 205 * @check_main_thermal_prot_work:
206 * Work for checking Main thermal status 206 * Work for checking Main thermal status
207 * @check_usb_thermal_prot_work: 207 * @check_usb_thermal_prot_work:
208 * Work for checking USB thermal status 208 * Work for checking USB thermal status
209 */ 209 */
210 struct ab8500_charger { 210 struct ab8500_charger {
211 struct device *dev; 211 struct device *dev;
212 int max_usb_in_curr; 212 int max_usb_in_curr;
213 bool vbus_detected; 213 bool vbus_detected;
214 bool vbus_detected_start; 214 bool vbus_detected_start;
215 bool ac_conn; 215 bool ac_conn;
216 bool vddadc_en_ac; 216 bool vddadc_en_ac;
217 bool vddadc_en_usb; 217 bool vddadc_en_usb;
218 int vbat; 218 int vbat;
219 int old_vbat; 219 int old_vbat;
220 bool autopower; 220 bool autopower;
221 struct ab8500 *parent; 221 struct ab8500 *parent;
222 struct ab8500_gpadc *gpadc; 222 struct ab8500_gpadc *gpadc;
223 struct abx500_charger_platform_data *pdata; 223 struct abx500_charger_platform_data *pdata;
224 struct abx500_bm_data *bat; 224 struct abx500_bm_data *bat;
225 struct ab8500_charger_event_flags flags; 225 struct ab8500_charger_event_flags flags;
226 struct ab8500_charger_usb_state usb_state; 226 struct ab8500_charger_usb_state usb_state;
227 struct ux500_charger ac_chg; 227 struct ux500_charger ac_chg;
228 struct ux500_charger usb_chg; 228 struct ux500_charger usb_chg;
229 struct ab8500_charger_info ac; 229 struct ab8500_charger_info ac;
230 struct ab8500_charger_info usb; 230 struct ab8500_charger_info usb;
231 struct regulator *regu; 231 struct regulator *regu;
232 struct workqueue_struct *charger_wq; 232 struct workqueue_struct *charger_wq;
233 struct delayed_work check_vbat_work; 233 struct delayed_work check_vbat_work;
234 struct delayed_work check_hw_failure_work; 234 struct delayed_work check_hw_failure_work;
235 struct delayed_work check_usbchgnotok_work; 235 struct delayed_work check_usbchgnotok_work;
236 struct delayed_work kick_wd_work; 236 struct delayed_work kick_wd_work;
237 struct work_struct ac_work; 237 struct work_struct ac_work;
238 struct work_struct detect_usb_type_work; 238 struct work_struct detect_usb_type_work;
239 struct work_struct usb_link_status_work; 239 struct work_struct usb_link_status_work;
240 struct work_struct usb_state_changed_work; 240 struct work_struct usb_state_changed_work;
241 struct work_struct check_main_thermal_prot_work; 241 struct work_struct check_main_thermal_prot_work;
242 struct work_struct check_usb_thermal_prot_work; 242 struct work_struct check_usb_thermal_prot_work;
243 struct usb_phy *usb_phy; 243 struct usb_phy *usb_phy;
244 struct notifier_block nb; 244 struct notifier_block nb;
245 }; 245 };
246 246
247 /* AC properties */ 247 /* AC properties */
248 static enum power_supply_property ab8500_charger_ac_props[] = { 248 static enum power_supply_property ab8500_charger_ac_props[] = {
249 POWER_SUPPLY_PROP_HEALTH, 249 POWER_SUPPLY_PROP_HEALTH,
250 POWER_SUPPLY_PROP_PRESENT, 250 POWER_SUPPLY_PROP_PRESENT,
251 POWER_SUPPLY_PROP_ONLINE, 251 POWER_SUPPLY_PROP_ONLINE,
252 POWER_SUPPLY_PROP_VOLTAGE_NOW, 252 POWER_SUPPLY_PROP_VOLTAGE_NOW,
253 POWER_SUPPLY_PROP_VOLTAGE_AVG, 253 POWER_SUPPLY_PROP_VOLTAGE_AVG,
254 POWER_SUPPLY_PROP_CURRENT_NOW, 254 POWER_SUPPLY_PROP_CURRENT_NOW,
255 }; 255 };
256 256
257 /* USB properties */ 257 /* USB properties */
258 static enum power_supply_property ab8500_charger_usb_props[] = { 258 static enum power_supply_property ab8500_charger_usb_props[] = {
259 POWER_SUPPLY_PROP_HEALTH, 259 POWER_SUPPLY_PROP_HEALTH,
260 POWER_SUPPLY_PROP_CURRENT_AVG, 260 POWER_SUPPLY_PROP_CURRENT_AVG,
261 POWER_SUPPLY_PROP_PRESENT, 261 POWER_SUPPLY_PROP_PRESENT,
262 POWER_SUPPLY_PROP_ONLINE, 262 POWER_SUPPLY_PROP_ONLINE,
263 POWER_SUPPLY_PROP_VOLTAGE_NOW, 263 POWER_SUPPLY_PROP_VOLTAGE_NOW,
264 POWER_SUPPLY_PROP_VOLTAGE_AVG, 264 POWER_SUPPLY_PROP_VOLTAGE_AVG,
265 POWER_SUPPLY_PROP_CURRENT_NOW, 265 POWER_SUPPLY_PROP_CURRENT_NOW,
266 }; 266 };
267 267
268 /** 268 /**
269 * ab8500_power_loss_handling - set how we handle powerloss. 269 * ab8500_power_loss_handling - set how we handle powerloss.
270 * @di: pointer to the ab8500_charger structure 270 * @di: pointer to the ab8500_charger structure
271 * 271 *
272 * Magic nummbers are from STE HW department. 272 * Magic nummbers are from STE HW department.
273 */ 273 */
274 static void ab8500_power_loss_handling(struct ab8500_charger *di) 274 static void ab8500_power_loss_handling(struct ab8500_charger *di)
275 { 275 {
276 u8 reg; 276 u8 reg;
277 int ret; 277 int ret;
278 278
279 dev_dbg(di->dev, "Autopower : %d\n", di->autopower); 279 dev_dbg(di->dev, "Autopower : %d\n", di->autopower);
280 280
281 /* read the autopower register */ 281 /* read the autopower register */
282 ret = abx500_get_register_interruptible(di->dev, 0x15, 0x00, &reg); 282 ret = abx500_get_register_interruptible(di->dev, 0x15, 0x00, &reg);
283 if (ret) { 283 if (ret) {
284 dev_err(di->dev, "%d write failed\n", __LINE__); 284 dev_err(di->dev, "%d write failed\n", __LINE__);
285 return; 285 return;
286 } 286 }
287 287
288 /* enable the OPT emulation registers */ 288 /* enable the OPT emulation registers */
289 ret = abx500_set_register_interruptible(di->dev, 0x11, 0x00, 0x2); 289 ret = abx500_set_register_interruptible(di->dev, 0x11, 0x00, 0x2);
290 if (ret) { 290 if (ret) {
291 dev_err(di->dev, "%d write failed\n", __LINE__); 291 dev_err(di->dev, "%d write failed\n", __LINE__);
292 return; 292 return;
293 } 293 }
294 294
295 if (di->autopower) 295 if (di->autopower)
296 reg |= 0x8; 296 reg |= 0x8;
297 else 297 else
298 reg &= ~0x8; 298 reg &= ~0x8;
299 299
300 /* write back the changed value to autopower reg */ 300 /* write back the changed value to autopower reg */
301 ret = abx500_set_register_interruptible(di->dev, 0x15, 0x00, reg); 301 ret = abx500_set_register_interruptible(di->dev, 0x15, 0x00, reg);
302 if (ret) { 302 if (ret) {
303 dev_err(di->dev, "%d write failed\n", __LINE__); 303 dev_err(di->dev, "%d write failed\n", __LINE__);
304 return; 304 return;
305 } 305 }
306 306
307 /* disable the set OTP registers again */ 307 /* disable the set OTP registers again */
308 ret = abx500_set_register_interruptible(di->dev, 0x11, 0x00, 0x0); 308 ret = abx500_set_register_interruptible(di->dev, 0x11, 0x00, 0x0);
309 if (ret) { 309 if (ret) {
310 dev_err(di->dev, "%d write failed\n", __LINE__); 310 dev_err(di->dev, "%d write failed\n", __LINE__);
311 return; 311 return;
312 } 312 }
313 } 313 }
314 314
315 /** 315 /**
316 * ab8500_power_supply_changed - a wrapper with local extentions for 316 * ab8500_power_supply_changed - a wrapper with local extentions for
317 * power_supply_changed 317 * power_supply_changed
318 * @di: pointer to the ab8500_charger structure 318 * @di: pointer to the ab8500_charger structure
319 * @psy: pointer to power_supply_that have changed. 319 * @psy: pointer to power_supply_that have changed.
320 * 320 *
321 */ 321 */
322 static void ab8500_power_supply_changed(struct ab8500_charger *di, 322 static void ab8500_power_supply_changed(struct ab8500_charger *di,
323 struct power_supply *psy) 323 struct power_supply *psy)
324 { 324 {
325 if (di->pdata->autopower_cfg) { 325 if (di->pdata->autopower_cfg) {
326 if (!di->usb.charger_connected && 326 if (!di->usb.charger_connected &&
327 !di->ac.charger_connected && 327 !di->ac.charger_connected &&
328 di->autopower) { 328 di->autopower) {
329 di->autopower = false; 329 di->autopower = false;
330 ab8500_power_loss_handling(di); 330 ab8500_power_loss_handling(di);
331 } else if (!di->autopower && 331 } else if (!di->autopower &&
332 (di->ac.charger_connected || 332 (di->ac.charger_connected ||
333 di->usb.charger_connected)) { 333 di->usb.charger_connected)) {
334 di->autopower = true; 334 di->autopower = true;
335 ab8500_power_loss_handling(di); 335 ab8500_power_loss_handling(di);
336 } 336 }
337 } 337 }
338 power_supply_changed(psy); 338 power_supply_changed(psy);
339 } 339 }
340 340
341 static void ab8500_charger_set_usb_connected(struct ab8500_charger *di, 341 static void ab8500_charger_set_usb_connected(struct ab8500_charger *di,
342 bool connected) 342 bool connected)
343 { 343 {
344 if (connected != di->usb.charger_connected) { 344 if (connected != di->usb.charger_connected) {
345 dev_dbg(di->dev, "USB connected:%i\n", connected); 345 dev_dbg(di->dev, "USB connected:%i\n", connected);
346 di->usb.charger_connected = connected; 346 di->usb.charger_connected = connected;
347 sysfs_notify(&di->usb_chg.psy.dev->kobj, NULL, "present"); 347 sysfs_notify(&di->usb_chg.psy.dev->kobj, NULL, "present");
348 } 348 }
349 } 349 }
350 350
351 /** 351 /**
352 * ab8500_charger_get_ac_voltage() - get ac charger voltage 352 * ab8500_charger_get_ac_voltage() - get ac charger voltage
353 * @di: pointer to the ab8500_charger structure 353 * @di: pointer to the ab8500_charger structure
354 * 354 *
355 * Returns ac charger voltage (on success) 355 * Returns ac charger voltage (on success)
356 */ 356 */
357 static int ab8500_charger_get_ac_voltage(struct ab8500_charger *di) 357 static int ab8500_charger_get_ac_voltage(struct ab8500_charger *di)
358 { 358 {
359 int vch; 359 int vch;
360 360
361 /* Only measure voltage if the charger is connected */ 361 /* Only measure voltage if the charger is connected */
362 if (di->ac.charger_connected) { 362 if (di->ac.charger_connected) {
363 vch = ab8500_gpadc_convert(di->gpadc, MAIN_CHARGER_V); 363 vch = ab8500_gpadc_convert(di->gpadc, MAIN_CHARGER_V);
364 if (vch < 0) 364 if (vch < 0)
365 dev_err(di->dev, "%s gpadc conv failed,\n", __func__); 365 dev_err(di->dev, "%s gpadc conv failed,\n", __func__);
366 } else { 366 } else {
367 vch = 0; 367 vch = 0;
368 } 368 }
369 return vch; 369 return vch;
370 } 370 }
371 371
372 /** 372 /**
373 * ab8500_charger_ac_cv() - check if the main charger is in CV mode 373 * ab8500_charger_ac_cv() - check if the main charger is in CV mode
374 * @di: pointer to the ab8500_charger structure 374 * @di: pointer to the ab8500_charger structure
375 * 375 *
376 * Returns ac charger CV mode (on success) else error code 376 * Returns ac charger CV mode (on success) else error code
377 */ 377 */
378 static int ab8500_charger_ac_cv(struct ab8500_charger *di) 378 static int ab8500_charger_ac_cv(struct ab8500_charger *di)
379 { 379 {
380 u8 val; 380 u8 val;
381 int ret = 0; 381 int ret = 0;
382 382
383 /* Only check CV mode if the charger is online */ 383 /* Only check CV mode if the charger is online */
384 if (di->ac.charger_online) { 384 if (di->ac.charger_online) {
385 ret = abx500_get_register_interruptible(di->dev, AB8500_CHARGER, 385 ret = abx500_get_register_interruptible(di->dev, AB8500_CHARGER,
386 AB8500_CH_STATUS1_REG, &val); 386 AB8500_CH_STATUS1_REG, &val);
387 if (ret < 0) { 387 if (ret < 0) {
388 dev_err(di->dev, "%s ab8500 read failed\n", __func__); 388 dev_err(di->dev, "%s ab8500 read failed\n", __func__);
389 return 0; 389 return 0;
390 } 390 }
391 391
392 if (val & MAIN_CH_CV_ON) 392 if (val & MAIN_CH_CV_ON)
393 ret = 1; 393 ret = 1;
394 else 394 else
395 ret = 0; 395 ret = 0;
396 } 396 }
397 397
398 return ret; 398 return ret;
399 } 399 }
400 400
401 /** 401 /**
402 * ab8500_charger_get_vbus_voltage() - get vbus voltage 402 * ab8500_charger_get_vbus_voltage() - get vbus voltage
403 * @di: pointer to the ab8500_charger structure 403 * @di: pointer to the ab8500_charger structure
404 * 404 *
405 * This function returns the vbus voltage. 405 * This function returns the vbus voltage.
406 * Returns vbus voltage (on success) 406 * Returns vbus voltage (on success)
407 */ 407 */
408 static int ab8500_charger_get_vbus_voltage(struct ab8500_charger *di) 408 static int ab8500_charger_get_vbus_voltage(struct ab8500_charger *di)
409 { 409 {
410 int vch; 410 int vch;
411 411
412 /* Only measure voltage if the charger is connected */ 412 /* Only measure voltage if the charger is connected */
413 if (di->usb.charger_connected) { 413 if (di->usb.charger_connected) {
414 vch = ab8500_gpadc_convert(di->gpadc, VBUS_V); 414 vch = ab8500_gpadc_convert(di->gpadc, VBUS_V);
415 if (vch < 0) 415 if (vch < 0)
416 dev_err(di->dev, "%s gpadc conv failed\n", __func__); 416 dev_err(di->dev, "%s gpadc conv failed\n", __func__);
417 } else { 417 } else {
418 vch = 0; 418 vch = 0;
419 } 419 }
420 return vch; 420 return vch;
421 } 421 }
422 422
423 /** 423 /**
424 * ab8500_charger_get_usb_current() - get usb charger current 424 * ab8500_charger_get_usb_current() - get usb charger current
425 * @di: pointer to the ab8500_charger structure 425 * @di: pointer to the ab8500_charger structure
426 * 426 *
427 * This function returns the usb charger current. 427 * This function returns the usb charger current.
428 * Returns usb current (on success) and error code on failure 428 * Returns usb current (on success) and error code on failure
429 */ 429 */
430 static int ab8500_charger_get_usb_current(struct ab8500_charger *di) 430 static int ab8500_charger_get_usb_current(struct ab8500_charger *di)
431 { 431 {
432 int ich; 432 int ich;
433 433
434 /* Only measure current if the charger is online */ 434 /* Only measure current if the charger is online */
435 if (di->usb.charger_online) { 435 if (di->usb.charger_online) {
436 ich = ab8500_gpadc_convert(di->gpadc, USB_CHARGER_C); 436 ich = ab8500_gpadc_convert(di->gpadc, USB_CHARGER_C);
437 if (ich < 0) 437 if (ich < 0)
438 dev_err(di->dev, "%s gpadc conv failed\n", __func__); 438 dev_err(di->dev, "%s gpadc conv failed\n", __func__);
439 } else { 439 } else {
440 ich = 0; 440 ich = 0;
441 } 441 }
442 return ich; 442 return ich;
443 } 443 }
444 444
445 /** 445 /**
446 * ab8500_charger_get_ac_current() - get ac charger current 446 * ab8500_charger_get_ac_current() - get ac charger current
447 * @di: pointer to the ab8500_charger structure 447 * @di: pointer to the ab8500_charger structure
448 * 448 *
449 * This function returns the ac charger current. 449 * This function returns the ac charger current.
450 * Returns ac current (on success) and error code on failure. 450 * Returns ac current (on success) and error code on failure.
451 */ 451 */
452 static int ab8500_charger_get_ac_current(struct ab8500_charger *di) 452 static int ab8500_charger_get_ac_current(struct ab8500_charger *di)
453 { 453 {
454 int ich; 454 int ich;
455 455
456 /* Only measure current if the charger is online */ 456 /* Only measure current if the charger is online */
457 if (di->ac.charger_online) { 457 if (di->ac.charger_online) {
458 ich = ab8500_gpadc_convert(di->gpadc, MAIN_CHARGER_C); 458 ich = ab8500_gpadc_convert(di->gpadc, MAIN_CHARGER_C);
459 if (ich < 0) 459 if (ich < 0)
460 dev_err(di->dev, "%s gpadc conv failed\n", __func__); 460 dev_err(di->dev, "%s gpadc conv failed\n", __func__);
461 } else { 461 } else {
462 ich = 0; 462 ich = 0;
463 } 463 }
464 return ich; 464 return ich;
465 } 465 }
466 466
467 /** 467 /**
468 * ab8500_charger_usb_cv() - check if the usb charger is in CV mode 468 * ab8500_charger_usb_cv() - check if the usb charger is in CV mode
469 * @di: pointer to the ab8500_charger structure 469 * @di: pointer to the ab8500_charger structure
470 * 470 *
471 * Returns ac charger CV mode (on success) else error code 471 * Returns ac charger CV mode (on success) else error code
472 */ 472 */
473 static int ab8500_charger_usb_cv(struct ab8500_charger *di) 473 static int ab8500_charger_usb_cv(struct ab8500_charger *di)
474 { 474 {
475 int ret; 475 int ret;
476 u8 val; 476 u8 val;
477 477
478 /* Only check CV mode if the charger is online */ 478 /* Only check CV mode if the charger is online */
479 if (di->usb.charger_online) { 479 if (di->usb.charger_online) {
480 ret = abx500_get_register_interruptible(di->dev, AB8500_CHARGER, 480 ret = abx500_get_register_interruptible(di->dev, AB8500_CHARGER,
481 AB8500_CH_USBCH_STAT1_REG, &val); 481 AB8500_CH_USBCH_STAT1_REG, &val);
482 if (ret < 0) { 482 if (ret < 0) {
483 dev_err(di->dev, "%s ab8500 read failed\n", __func__); 483 dev_err(di->dev, "%s ab8500 read failed\n", __func__);
484 return 0; 484 return 0;
485 } 485 }
486 486
487 if (val & USB_CH_CV_ON) 487 if (val & USB_CH_CV_ON)
488 ret = 1; 488 ret = 1;
489 else 489 else
490 ret = 0; 490 ret = 0;
491 } else { 491 } else {
492 ret = 0; 492 ret = 0;
493 } 493 }
494 494
495 return ret; 495 return ret;
496 } 496 }
497 497
498 /** 498 /**
499 * ab8500_charger_detect_chargers() - Detect the connected chargers 499 * ab8500_charger_detect_chargers() - Detect the connected chargers
500 * @di: pointer to the ab8500_charger structure 500 * @di: pointer to the ab8500_charger structure
501 * 501 *
502 * Returns the type of charger connected. 502 * Returns the type of charger connected.
503 * For USB it will not mean we can actually charge from it 503 * For USB it will not mean we can actually charge from it
504 * but that there is a USB cable connected that we have to 504 * but that there is a USB cable connected that we have to
505 * identify. This is used during startup when we don't get 505 * identify. This is used during startup when we don't get
506 * interrupts of the charger detection 506 * interrupts of the charger detection
507 * 507 *
508 * Returns an integer value, that means, 508 * Returns an integer value, that means,
509 * NO_PW_CONN no power supply is connected 509 * NO_PW_CONN no power supply is connected
510 * AC_PW_CONN if the AC power supply is connected 510 * AC_PW_CONN if the AC power supply is connected
511 * USB_PW_CONN if the USB power supply is connected 511 * USB_PW_CONN if the USB power supply is connected
512 * AC_PW_CONN + USB_PW_CONN if USB and AC power supplies are both connected 512 * AC_PW_CONN + USB_PW_CONN if USB and AC power supplies are both connected
513 */ 513 */
514 static int ab8500_charger_detect_chargers(struct ab8500_charger *di) 514 static int ab8500_charger_detect_chargers(struct ab8500_charger *di)
515 { 515 {
516 int result = NO_PW_CONN; 516 int result = NO_PW_CONN;
517 int ret; 517 int ret;
518 u8 val; 518 u8 val;
519 519
520 /* Check for AC charger */ 520 /* Check for AC charger */
521 ret = abx500_get_register_interruptible(di->dev, AB8500_CHARGER, 521 ret = abx500_get_register_interruptible(di->dev, AB8500_CHARGER,
522 AB8500_CH_STATUS1_REG, &val); 522 AB8500_CH_STATUS1_REG, &val);
523 if (ret < 0) { 523 if (ret < 0) {
524 dev_err(di->dev, "%s ab8500 read failed\n", __func__); 524 dev_err(di->dev, "%s ab8500 read failed\n", __func__);
525 return ret; 525 return ret;
526 } 526 }
527 527
528 if (val & MAIN_CH_DET) 528 if (val & MAIN_CH_DET)
529 result = AC_PW_CONN; 529 result = AC_PW_CONN;
530 530
531 /* Check for USB charger */ 531 /* Check for USB charger */
532 ret = abx500_get_register_interruptible(di->dev, AB8500_CHARGER, 532 ret = abx500_get_register_interruptible(di->dev, AB8500_CHARGER,
533 AB8500_CH_USBCH_STAT1_REG, &val); 533 AB8500_CH_USBCH_STAT1_REG, &val);
534 if (ret < 0) { 534 if (ret < 0) {
535 dev_err(di->dev, "%s ab8500 read failed\n", __func__); 535 dev_err(di->dev, "%s ab8500 read failed\n", __func__);
536 return ret; 536 return ret;
537 } 537 }
538 538
539 if ((val & VBUS_DET_DBNC1) && (val & VBUS_DET_DBNC100)) 539 if ((val & VBUS_DET_DBNC1) && (val & VBUS_DET_DBNC100))
540 result |= USB_PW_CONN; 540 result |= USB_PW_CONN;
541 541
542 return result; 542 return result;
543 } 543 }
544 544
545 /** 545 /**
546 * ab8500_charger_max_usb_curr() - get the max curr for the USB type 546 * ab8500_charger_max_usb_curr() - get the max curr for the USB type
547 * @di: pointer to the ab8500_charger structure 547 * @di: pointer to the ab8500_charger structure
548 * @link_status: the identified USB type 548 * @link_status: the identified USB type
549 * 549 *
550 * Get the maximum current that is allowed to be drawn from the host 550 * Get the maximum current that is allowed to be drawn from the host
551 * based on the USB type. 551 * based on the USB type.
552 * Returns error code in case of failure else 0 on success 552 * Returns error code in case of failure else 0 on success
553 */ 553 */
554 static int ab8500_charger_max_usb_curr(struct ab8500_charger *di, 554 static int ab8500_charger_max_usb_curr(struct ab8500_charger *di,
555 enum ab8500_charger_link_status link_status) 555 enum ab8500_charger_link_status link_status)
556 { 556 {
557 int ret = 0; 557 int ret = 0;
558 558
559 switch (link_status) { 559 switch (link_status) {
560 case USB_STAT_STD_HOST_NC: 560 case USB_STAT_STD_HOST_NC:
561 case USB_STAT_STD_HOST_C_NS: 561 case USB_STAT_STD_HOST_C_NS:
562 case USB_STAT_STD_HOST_C_S: 562 case USB_STAT_STD_HOST_C_S:
563 dev_dbg(di->dev, "USB Type - Standard host is " 563 dev_dbg(di->dev, "USB Type - Standard host is "
564 "detected through USB driver\n"); 564 "detected through USB driver\n");
565 di->max_usb_in_curr = USB_CH_IP_CUR_LVL_0P09; 565 di->max_usb_in_curr = USB_CH_IP_CUR_LVL_0P09;
566 break; 566 break;
567 case USB_STAT_HOST_CHG_HS_CHIRP: 567 case USB_STAT_HOST_CHG_HS_CHIRP:
568 di->max_usb_in_curr = USB_CH_IP_CUR_LVL_0P5; 568 di->max_usb_in_curr = USB_CH_IP_CUR_LVL_0P5;
569 break; 569 break;
570 case USB_STAT_HOST_CHG_HS: 570 case USB_STAT_HOST_CHG_HS:
571 case USB_STAT_ACA_RID_C_HS: 571 case USB_STAT_ACA_RID_C_HS:
572 di->max_usb_in_curr = USB_CH_IP_CUR_LVL_0P9; 572 di->max_usb_in_curr = USB_CH_IP_CUR_LVL_0P9;
573 break; 573 break;
574 case USB_STAT_ACA_RID_A: 574 case USB_STAT_ACA_RID_A:
575 /* 575 /*
576 * Dedicated charger level minus maximum current accessory 576 * Dedicated charger level minus maximum current accessory
577 * can consume (300mA). Closest level is 1100mA 577 * can consume (300mA). Closest level is 1100mA
578 */ 578 */
579 di->max_usb_in_curr = USB_CH_IP_CUR_LVL_1P1; 579 di->max_usb_in_curr = USB_CH_IP_CUR_LVL_1P1;
580 break; 580 break;
581 case USB_STAT_ACA_RID_B: 581 case USB_STAT_ACA_RID_B:
582 /* 582 /*
583 * Dedicated charger level minus 120mA (20mA for ACA and 583 * Dedicated charger level minus 120mA (20mA for ACA and
584 * 100mA for potential accessory). Closest level is 1300mA 584 * 100mA for potential accessory). Closest level is 1300mA
585 */ 585 */
586 di->max_usb_in_curr = USB_CH_IP_CUR_LVL_1P3; 586 di->max_usb_in_curr = USB_CH_IP_CUR_LVL_1P3;
587 break; 587 break;
588 case USB_STAT_DEDICATED_CHG: 588 case USB_STAT_DEDICATED_CHG:
589 case USB_STAT_HOST_CHG_NM: 589 case USB_STAT_HOST_CHG_NM:
590 case USB_STAT_ACA_RID_C_HS_CHIRP: 590 case USB_STAT_ACA_RID_C_HS_CHIRP:
591 case USB_STAT_ACA_RID_C_NM: 591 case USB_STAT_ACA_RID_C_NM:
592 di->max_usb_in_curr = USB_CH_IP_CUR_LVL_1P5; 592 di->max_usb_in_curr = USB_CH_IP_CUR_LVL_1P5;
593 break; 593 break;
594 case USB_STAT_RESERVED: 594 case USB_STAT_RESERVED:
595 /* 595 /*
596 * This state is used to indicate that VBUS has dropped below 596 * This state is used to indicate that VBUS has dropped below
597 * the detection level 4 times in a row. This is due to the 597 * the detection level 4 times in a row. This is due to the
598 * charger output current is set to high making the charger 598 * charger output current is set to high making the charger
599 * voltage collapse. This have to be propagated through to 599 * voltage collapse. This have to be propagated through to
600 * chargalg. This is done using the property 600 * chargalg. This is done using the property
601 * POWER_SUPPLY_PROP_CURRENT_AVG = 1 601 * POWER_SUPPLY_PROP_CURRENT_AVG = 1
602 */ 602 */
603 di->flags.vbus_collapse = true; 603 di->flags.vbus_collapse = true;
604 dev_dbg(di->dev, "USB Type - USB_STAT_RESERVED " 604 dev_dbg(di->dev, "USB Type - USB_STAT_RESERVED "
605 "VBUS has collapsed\n"); 605 "VBUS has collapsed\n");
606 ret = -1; 606 ret = -1;
607 break; 607 break;
608 case USB_STAT_HM_IDGND: 608 case USB_STAT_HM_IDGND:
609 case USB_STAT_NOT_CONFIGURED: 609 case USB_STAT_NOT_CONFIGURED:
610 case USB_STAT_NOT_VALID_LINK: 610 case USB_STAT_NOT_VALID_LINK:
611 dev_err(di->dev, "USB Type - Charging not allowed\n"); 611 dev_err(di->dev, "USB Type - Charging not allowed\n");
612 di->max_usb_in_curr = USB_CH_IP_CUR_LVL_0P05; 612 di->max_usb_in_curr = USB_CH_IP_CUR_LVL_0P05;
613 ret = -ENXIO; 613 ret = -ENXIO;
614 break; 614 break;
615 default: 615 default:
616 dev_err(di->dev, "USB Type - Unknown\n"); 616 dev_err(di->dev, "USB Type - Unknown\n");
617 di->max_usb_in_curr = USB_CH_IP_CUR_LVL_0P05; 617 di->max_usb_in_curr = USB_CH_IP_CUR_LVL_0P05;
618 ret = -ENXIO; 618 ret = -ENXIO;
619 break; 619 break;
620 }; 620 };
621 621
622 dev_dbg(di->dev, "USB Type - 0x%02x MaxCurr: %d", 622 dev_dbg(di->dev, "USB Type - 0x%02x MaxCurr: %d",
623 link_status, di->max_usb_in_curr); 623 link_status, di->max_usb_in_curr);
624 624
625 return ret; 625 return ret;
626 } 626 }
627 627
628 /** 628 /**
629 * ab8500_charger_read_usb_type() - read the type of usb connected 629 * ab8500_charger_read_usb_type() - read the type of usb connected
630 * @di: pointer to the ab8500_charger structure 630 * @di: pointer to the ab8500_charger structure
631 * 631 *
632 * Detect the type of the plugged USB 632 * Detect the type of the plugged USB
633 * Returns error code in case of failure else 0 on success 633 * Returns error code in case of failure else 0 on success
634 */ 634 */
635 static int ab8500_charger_read_usb_type(struct ab8500_charger *di) 635 static int ab8500_charger_read_usb_type(struct ab8500_charger *di)
636 { 636 {
637 int ret; 637 int ret;
638 u8 val; 638 u8 val;
639 639
640 ret = abx500_get_register_interruptible(di->dev, 640 ret = abx500_get_register_interruptible(di->dev,
641 AB8500_INTERRUPT, AB8500_IT_SOURCE21_REG, &val); 641 AB8500_INTERRUPT, AB8500_IT_SOURCE21_REG, &val);
642 if (ret < 0) { 642 if (ret < 0) {
643 dev_err(di->dev, "%s ab8500 read failed\n", __func__); 643 dev_err(di->dev, "%s ab8500 read failed\n", __func__);
644 return ret; 644 return ret;
645 } 645 }
646 ret = abx500_get_register_interruptible(di->dev, AB8500_USB, 646 ret = abx500_get_register_interruptible(di->dev, AB8500_USB,
647 AB8500_USB_LINE_STAT_REG, &val); 647 AB8500_USB_LINE_STAT_REG, &val);
648 if (ret < 0) { 648 if (ret < 0) {
649 dev_err(di->dev, "%s ab8500 read failed\n", __func__); 649 dev_err(di->dev, "%s ab8500 read failed\n", __func__);
650 return ret; 650 return ret;
651 } 651 }
652 652
653 /* get the USB type */ 653 /* get the USB type */
654 val = (val & AB8500_USB_LINK_STATUS) >> 3; 654 val = (val & AB8500_USB_LINK_STATUS) >> 3;
655 ret = ab8500_charger_max_usb_curr(di, 655 ret = ab8500_charger_max_usb_curr(di,
656 (enum ab8500_charger_link_status) val); 656 (enum ab8500_charger_link_status) val);
657 657
658 return ret; 658 return ret;
659 } 659 }
660 660
661 /** 661 /**
662 * ab8500_charger_detect_usb_type() - get the type of usb connected 662 * ab8500_charger_detect_usb_type() - get the type of usb connected
663 * @di: pointer to the ab8500_charger structure 663 * @di: pointer to the ab8500_charger structure
664 * 664 *
665 * Detect the type of the plugged USB 665 * Detect the type of the plugged USB
666 * Returns error code in case of failure else 0 on success 666 * Returns error code in case of failure else 0 on success
667 */ 667 */
668 static int ab8500_charger_detect_usb_type(struct ab8500_charger *di) 668 static int ab8500_charger_detect_usb_type(struct ab8500_charger *di)
669 { 669 {
670 int i, ret; 670 int i, ret;
671 u8 val; 671 u8 val;
672 672
673 /* 673 /*
674 * On getting the VBUS rising edge detect interrupt there 674 * On getting the VBUS rising edge detect interrupt there
675 * is a 250ms delay after which the register UsbLineStatus 675 * is a 250ms delay after which the register UsbLineStatus
676 * is filled with valid data. 676 * is filled with valid data.
677 */ 677 */
678 for (i = 0; i < 10; i++) { 678 for (i = 0; i < 10; i++) {
679 msleep(250); 679 msleep(250);
680 ret = abx500_get_register_interruptible(di->dev, 680 ret = abx500_get_register_interruptible(di->dev,
681 AB8500_INTERRUPT, AB8500_IT_SOURCE21_REG, 681 AB8500_INTERRUPT, AB8500_IT_SOURCE21_REG,
682 &val); 682 &val);
683 if (ret < 0) { 683 if (ret < 0) {
684 dev_err(di->dev, "%s ab8500 read failed\n", __func__); 684 dev_err(di->dev, "%s ab8500 read failed\n", __func__);
685 return ret; 685 return ret;
686 } 686 }
687 ret = abx500_get_register_interruptible(di->dev, AB8500_USB, 687 ret = abx500_get_register_interruptible(di->dev, AB8500_USB,
688 AB8500_USB_LINE_STAT_REG, &val); 688 AB8500_USB_LINE_STAT_REG, &val);
689 if (ret < 0) { 689 if (ret < 0) {
690 dev_err(di->dev, "%s ab8500 read failed\n", __func__); 690 dev_err(di->dev, "%s ab8500 read failed\n", __func__);
691 return ret; 691 return ret;
692 } 692 }
693 /* 693 /*
694 * Until the IT source register is read the UsbLineStatus 694 * Until the IT source register is read the UsbLineStatus
695 * register is not updated, hence doing the same 695 * register is not updated, hence doing the same
696 * Revisit this: 696 * Revisit this:
697 */ 697 */
698 698
699 /* get the USB type */ 699 /* get the USB type */
700 val = (val & AB8500_USB_LINK_STATUS) >> 3; 700 val = (val & AB8500_USB_LINK_STATUS) >> 3;
701 if (val) 701 if (val)
702 break; 702 break;
703 } 703 }
704 ret = ab8500_charger_max_usb_curr(di, 704 ret = ab8500_charger_max_usb_curr(di,
705 (enum ab8500_charger_link_status) val); 705 (enum ab8500_charger_link_status) val);
706 706
707 return ret; 707 return ret;
708 } 708 }
709 709
710 /* 710 /*
711 * This array maps the raw hex value to charger voltage used by the AB8500 711 * This array maps the raw hex value to charger voltage used by the AB8500
712 * Values taken from the UM0836 712 * Values taken from the UM0836
713 */ 713 */
714 static int ab8500_charger_voltage_map[] = { 714 static int ab8500_charger_voltage_map[] = {
715 3500 , 715 3500 ,
716 3525 , 716 3525 ,
717 3550 , 717 3550 ,
718 3575 , 718 3575 ,
719 3600 , 719 3600 ,
720 3625 , 720 3625 ,
721 3650 , 721 3650 ,
722 3675 , 722 3675 ,
723 3700 , 723 3700 ,
724 3725 , 724 3725 ,
725 3750 , 725 3750 ,
726 3775 , 726 3775 ,
727 3800 , 727 3800 ,
728 3825 , 728 3825 ,
729 3850 , 729 3850 ,
730 3875 , 730 3875 ,
731 3900 , 731 3900 ,
732 3925 , 732 3925 ,
733 3950 , 733 3950 ,
734 3975 , 734 3975 ,
735 4000 , 735 4000 ,
736 4025 , 736 4025 ,
737 4050 , 737 4050 ,
738 4060 , 738 4060 ,
739 4070 , 739 4070 ,
740 4080 , 740 4080 ,
741 4090 , 741 4090 ,
742 4100 , 742 4100 ,
743 4110 , 743 4110 ,
744 4120 , 744 4120 ,
745 4130 , 745 4130 ,
746 4140 , 746 4140 ,
747 4150 , 747 4150 ,
748 4160 , 748 4160 ,
749 4170 , 749 4170 ,
750 4180 , 750 4180 ,
751 4190 , 751 4190 ,
752 4200 , 752 4200 ,
753 4210 , 753 4210 ,
754 4220 , 754 4220 ,
755 4230 , 755 4230 ,
756 4240 , 756 4240 ,
757 4250 , 757 4250 ,
758 4260 , 758 4260 ,
759 4270 , 759 4270 ,
760 4280 , 760 4280 ,
761 4290 , 761 4290 ,
762 4300 , 762 4300 ,
763 4310 , 763 4310 ,
764 4320 , 764 4320 ,
765 4330 , 765 4330 ,
766 4340 , 766 4340 ,
767 4350 , 767 4350 ,
768 4360 , 768 4360 ,
769 4370 , 769 4370 ,
770 4380 , 770 4380 ,
771 4390 , 771 4390 ,
772 4400 , 772 4400 ,
773 4410 , 773 4410 ,
774 4420 , 774 4420 ,
775 4430 , 775 4430 ,
776 4440 , 776 4440 ,
777 4450 , 777 4450 ,
778 4460 , 778 4460 ,
779 4470 , 779 4470 ,
780 4480 , 780 4480 ,
781 4490 , 781 4490 ,
782 4500 , 782 4500 ,
783 4510 , 783 4510 ,
784 4520 , 784 4520 ,
785 4530 , 785 4530 ,
786 4540 , 786 4540 ,
787 4550 , 787 4550 ,
788 4560 , 788 4560 ,
789 4570 , 789 4570 ,
790 4580 , 790 4580 ,
791 4590 , 791 4590 ,
792 4600 , 792 4600 ,
793 }; 793 };
794 794
795 /* 795 /*
796 * This array maps the raw hex value to charger current used by the AB8500 796 * This array maps the raw hex value to charger current used by the AB8500
797 * Values taken from the UM0836 797 * Values taken from the UM0836
798 */ 798 */
799 static int ab8500_charger_current_map[] = { 799 static int ab8500_charger_current_map[] = {
800 100 , 800 100 ,
801 200 , 801 200 ,
802 300 , 802 300 ,
803 400 , 803 400 ,
804 500 , 804 500 ,
805 600 , 805 600 ,
806 700 , 806 700 ,
807 800 , 807 800 ,
808 900 , 808 900 ,
809 1000 , 809 1000 ,
810 1100 , 810 1100 ,
811 1200 , 811 1200 ,
812 1300 , 812 1300 ,
813 1400 , 813 1400 ,
814 1500 , 814 1500 ,
815 }; 815 };
816 816
817 /* 817 /*
818 * This array maps the raw hex value to VBUS input current used by the AB8500 818 * This array maps the raw hex value to VBUS input current used by the AB8500
819 * Values taken from the UM0836 819 * Values taken from the UM0836
820 */ 820 */
821 static int ab8500_charger_vbus_in_curr_map[] = { 821 static int ab8500_charger_vbus_in_curr_map[] = {
822 USB_CH_IP_CUR_LVL_0P05, 822 USB_CH_IP_CUR_LVL_0P05,
823 USB_CH_IP_CUR_LVL_0P09, 823 USB_CH_IP_CUR_LVL_0P09,
824 USB_CH_IP_CUR_LVL_0P19, 824 USB_CH_IP_CUR_LVL_0P19,
825 USB_CH_IP_CUR_LVL_0P29, 825 USB_CH_IP_CUR_LVL_0P29,
826 USB_CH_IP_CUR_LVL_0P38, 826 USB_CH_IP_CUR_LVL_0P38,
827 USB_CH_IP_CUR_LVL_0P45, 827 USB_CH_IP_CUR_LVL_0P45,
828 USB_CH_IP_CUR_LVL_0P5, 828 USB_CH_IP_CUR_LVL_0P5,
829 USB_CH_IP_CUR_LVL_0P6, 829 USB_CH_IP_CUR_LVL_0P6,
830 USB_CH_IP_CUR_LVL_0P7, 830 USB_CH_IP_CUR_LVL_0P7,
831 USB_CH_IP_CUR_LVL_0P8, 831 USB_CH_IP_CUR_LVL_0P8,
832 USB_CH_IP_CUR_LVL_0P9, 832 USB_CH_IP_CUR_LVL_0P9,
833 USB_CH_IP_CUR_LVL_1P0, 833 USB_CH_IP_CUR_LVL_1P0,
834 USB_CH_IP_CUR_LVL_1P1, 834 USB_CH_IP_CUR_LVL_1P1,
835 USB_CH_IP_CUR_LVL_1P3, 835 USB_CH_IP_CUR_LVL_1P3,
836 USB_CH_IP_CUR_LVL_1P4, 836 USB_CH_IP_CUR_LVL_1P4,
837 USB_CH_IP_CUR_LVL_1P5, 837 USB_CH_IP_CUR_LVL_1P5,
838 }; 838 };
839 839
840 static int ab8500_voltage_to_regval(int voltage) 840 static int ab8500_voltage_to_regval(int voltage)
841 { 841 {
842 int i; 842 int i;
843 843
844 /* Special case for voltage below 3.5V */ 844 /* Special case for voltage below 3.5V */
845 if (voltage < ab8500_charger_voltage_map[0]) 845 if (voltage < ab8500_charger_voltage_map[0])
846 return LOW_VOLT_REG; 846 return LOW_VOLT_REG;
847 847
848 for (i = 1; i < ARRAY_SIZE(ab8500_charger_voltage_map); i++) { 848 for (i = 1; i < ARRAY_SIZE(ab8500_charger_voltage_map); i++) {
849 if (voltage < ab8500_charger_voltage_map[i]) 849 if (voltage < ab8500_charger_voltage_map[i])
850 return i - 1; 850 return i - 1;
851 } 851 }
852 852
853 /* If not last element, return error */ 853 /* If not last element, return error */
854 i = ARRAY_SIZE(ab8500_charger_voltage_map) - 1; 854 i = ARRAY_SIZE(ab8500_charger_voltage_map) - 1;
855 if (voltage == ab8500_charger_voltage_map[i]) 855 if (voltage == ab8500_charger_voltage_map[i])
856 return i; 856 return i;
857 else 857 else
858 return -1; 858 return -1;
859 } 859 }
860 860
861 static int ab8500_current_to_regval(int curr) 861 static int ab8500_current_to_regval(int curr)
862 { 862 {
863 int i; 863 int i;
864 864
865 if (curr < ab8500_charger_current_map[0]) 865 if (curr < ab8500_charger_current_map[0])
866 return 0; 866 return 0;
867 867
868 for (i = 0; i < ARRAY_SIZE(ab8500_charger_current_map); i++) { 868 for (i = 0; i < ARRAY_SIZE(ab8500_charger_current_map); i++) {
869 if (curr < ab8500_charger_current_map[i]) 869 if (curr < ab8500_charger_current_map[i])
870 return i - 1; 870 return i - 1;
871 } 871 }
872 872
873 /* If not last element, return error */ 873 /* If not last element, return error */
874 i = ARRAY_SIZE(ab8500_charger_current_map) - 1; 874 i = ARRAY_SIZE(ab8500_charger_current_map) - 1;
875 if (curr == ab8500_charger_current_map[i]) 875 if (curr == ab8500_charger_current_map[i])
876 return i; 876 return i;
877 else 877 else
878 return -1; 878 return -1;
879 } 879 }
880 880
881 static int ab8500_vbus_in_curr_to_regval(int curr) 881 static int ab8500_vbus_in_curr_to_regval(int curr)
882 { 882 {
883 int i; 883 int i;
884 884
885 if (curr < ab8500_charger_vbus_in_curr_map[0]) 885 if (curr < ab8500_charger_vbus_in_curr_map[0])
886 return 0; 886 return 0;
887 887
888 for (i = 0; i < ARRAY_SIZE(ab8500_charger_vbus_in_curr_map); i++) { 888 for (i = 0; i < ARRAY_SIZE(ab8500_charger_vbus_in_curr_map); i++) {
889 if (curr < ab8500_charger_vbus_in_curr_map[i]) 889 if (curr < ab8500_charger_vbus_in_curr_map[i])
890 return i - 1; 890 return i - 1;
891 } 891 }
892 892
893 /* If not last element, return error */ 893 /* If not last element, return error */
894 i = ARRAY_SIZE(ab8500_charger_vbus_in_curr_map) - 1; 894 i = ARRAY_SIZE(ab8500_charger_vbus_in_curr_map) - 1;
895 if (curr == ab8500_charger_vbus_in_curr_map[i]) 895 if (curr == ab8500_charger_vbus_in_curr_map[i])
896 return i; 896 return i;
897 else 897 else
898 return -1; 898 return -1;
899 } 899 }
900 900
901 /** 901 /**
902 * ab8500_charger_get_usb_cur() - get usb current 902 * ab8500_charger_get_usb_cur() - get usb current
903 * @di: pointer to the ab8500_charger structre 903 * @di: pointer to the ab8500_charger structre
904 * 904 *
905 * The usb stack provides the maximum current that can be drawn from 905 * The usb stack provides the maximum current that can be drawn from
906 * the standard usb host. This will be in mA. 906 * the standard usb host. This will be in mA.
907 * This function converts current in mA to a value that can be written 907 * This function converts current in mA to a value that can be written
908 * to the register. Returns -1 if charging is not allowed 908 * to the register. Returns -1 if charging is not allowed
909 */ 909 */
910 static int ab8500_charger_get_usb_cur(struct ab8500_charger *di) 910 static int ab8500_charger_get_usb_cur(struct ab8500_charger *di)
911 { 911 {
912 switch (di->usb_state.usb_current) { 912 switch (di->usb_state.usb_current) {
913 case 100: 913 case 100:
914 di->max_usb_in_curr = USB_CH_IP_CUR_LVL_0P09; 914 di->max_usb_in_curr = USB_CH_IP_CUR_LVL_0P09;
915 break; 915 break;
916 case 200: 916 case 200:
917 di->max_usb_in_curr = USB_CH_IP_CUR_LVL_0P19; 917 di->max_usb_in_curr = USB_CH_IP_CUR_LVL_0P19;
918 break; 918 break;
919 case 300: 919 case 300:
920 di->max_usb_in_curr = USB_CH_IP_CUR_LVL_0P29; 920 di->max_usb_in_curr = USB_CH_IP_CUR_LVL_0P29;
921 break; 921 break;
922 case 400: 922 case 400:
923 di->max_usb_in_curr = USB_CH_IP_CUR_LVL_0P38; 923 di->max_usb_in_curr = USB_CH_IP_CUR_LVL_0P38;
924 break; 924 break;
925 case 500: 925 case 500:
926 di->max_usb_in_curr = USB_CH_IP_CUR_LVL_0P5; 926 di->max_usb_in_curr = USB_CH_IP_CUR_LVL_0P5;
927 break; 927 break;
928 default: 928 default:
929 di->max_usb_in_curr = USB_CH_IP_CUR_LVL_0P05; 929 di->max_usb_in_curr = USB_CH_IP_CUR_LVL_0P05;
930 return -1; 930 return -1;
931 break; 931 break;
932 }; 932 };
933 return 0; 933 return 0;
934 } 934 }
935 935
936 /** 936 /**
937 * ab8500_charger_set_vbus_in_curr() - set VBUS input current limit 937 * ab8500_charger_set_vbus_in_curr() - set VBUS input current limit
938 * @di: pointer to the ab8500_charger structure 938 * @di: pointer to the ab8500_charger structure
939 * @ich_in: charger input current limit 939 * @ich_in: charger input current limit
940 * 940 *
941 * Sets the current that can be drawn from the USB host 941 * Sets the current that can be drawn from the USB host
942 * Returns error code in case of failure else 0(on success) 942 * Returns error code in case of failure else 0(on success)
943 */ 943 */
944 static int ab8500_charger_set_vbus_in_curr(struct ab8500_charger *di, 944 static int ab8500_charger_set_vbus_in_curr(struct ab8500_charger *di,
945 int ich_in) 945 int ich_in)
946 { 946 {
947 int ret; 947 int ret;
948 int input_curr_index; 948 int input_curr_index;
949 int min_value; 949 int min_value;
950 950
951 /* We should always use to lowest current limit */ 951 /* We should always use to lowest current limit */
952 min_value = min(di->bat->chg_params->usb_curr_max, ich_in); 952 min_value = min(di->bat->chg_params->usb_curr_max, ich_in);
953 953
954 switch (min_value) { 954 switch (min_value) {
955 case 100: 955 case 100:
956 if (di->vbat < VBAT_TRESH_IP_CUR_RED) 956 if (di->vbat < VBAT_TRESH_IP_CUR_RED)
957 min_value = USB_CH_IP_CUR_LVL_0P05; 957 min_value = USB_CH_IP_CUR_LVL_0P05;
958 break; 958 break;
959 case 500: 959 case 500:
960 if (di->vbat < VBAT_TRESH_IP_CUR_RED) 960 if (di->vbat < VBAT_TRESH_IP_CUR_RED)
961 min_value = USB_CH_IP_CUR_LVL_0P45; 961 min_value = USB_CH_IP_CUR_LVL_0P45;
962 break; 962 break;
963 default: 963 default:
964 break; 964 break;
965 } 965 }
966 966
967 input_curr_index = ab8500_vbus_in_curr_to_regval(min_value); 967 input_curr_index = ab8500_vbus_in_curr_to_regval(min_value);
968 if (input_curr_index < 0) { 968 if (input_curr_index < 0) {
969 dev_err(di->dev, "VBUS input current limit too high\n"); 969 dev_err(di->dev, "VBUS input current limit too high\n");
970 return -ENXIO; 970 return -ENXIO;
971 } 971 }
972 972
973 ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER, 973 ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
974 AB8500_USBCH_IPT_CRNTLVL_REG, 974 AB8500_USBCH_IPT_CRNTLVL_REG,
975 input_curr_index << VBUS_IN_CURR_LIM_SHIFT); 975 input_curr_index << VBUS_IN_CURR_LIM_SHIFT);
976 if (ret) 976 if (ret)
977 dev_err(di->dev, "%s write failed\n", __func__); 977 dev_err(di->dev, "%s write failed\n", __func__);
978 978
979 return ret; 979 return ret;
980 } 980 }
981 981
982 /** 982 /**
983 * ab8500_charger_led_en() - turn on/off chargign led 983 * ab8500_charger_led_en() - turn on/off chargign led
984 * @di: pointer to the ab8500_charger structure 984 * @di: pointer to the ab8500_charger structure
985 * @on: flag to turn on/off the chargign led 985 * @on: flag to turn on/off the chargign led
986 * 986 *
987 * Power ON/OFF charging LED indication 987 * Power ON/OFF charging LED indication
988 * Returns error code in case of failure else 0(on success) 988 * Returns error code in case of failure else 0(on success)
989 */ 989 */
990 static int ab8500_charger_led_en(struct ab8500_charger *di, int on) 990 static int ab8500_charger_led_en(struct ab8500_charger *di, int on)
991 { 991 {
992 int ret; 992 int ret;
993 993
994 if (on) { 994 if (on) {
995 /* Power ON charging LED indicator, set LED current to 5mA */ 995 /* Power ON charging LED indicator, set LED current to 5mA */
996 ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER, 996 ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
997 AB8500_LED_INDICATOR_PWM_CTRL, 997 AB8500_LED_INDICATOR_PWM_CTRL,
998 (LED_IND_CUR_5MA | LED_INDICATOR_PWM_ENA)); 998 (LED_IND_CUR_5MA | LED_INDICATOR_PWM_ENA));
999 if (ret) { 999 if (ret) {
1000 dev_err(di->dev, "Power ON LED failed\n"); 1000 dev_err(di->dev, "Power ON LED failed\n");
1001 return ret; 1001 return ret;
1002 } 1002 }
1003 /* LED indicator PWM duty cycle 252/256 */ 1003 /* LED indicator PWM duty cycle 252/256 */
1004 ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER, 1004 ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
1005 AB8500_LED_INDICATOR_PWM_DUTY, 1005 AB8500_LED_INDICATOR_PWM_DUTY,
1006 LED_INDICATOR_PWM_DUTY_252_256); 1006 LED_INDICATOR_PWM_DUTY_252_256);
1007 if (ret) { 1007 if (ret) {
1008 dev_err(di->dev, "Set LED PWM duty cycle failed\n"); 1008 dev_err(di->dev, "Set LED PWM duty cycle failed\n");
1009 return ret; 1009 return ret;
1010 } 1010 }
1011 } else { 1011 } else {
1012 /* Power off charging LED indicator */ 1012 /* Power off charging LED indicator */
1013 ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER, 1013 ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
1014 AB8500_LED_INDICATOR_PWM_CTRL, 1014 AB8500_LED_INDICATOR_PWM_CTRL,
1015 LED_INDICATOR_PWM_DIS); 1015 LED_INDICATOR_PWM_DIS);
1016 if (ret) { 1016 if (ret) {
1017 dev_err(di->dev, "Power-off LED failed\n"); 1017 dev_err(di->dev, "Power-off LED failed\n");
1018 return ret; 1018 return ret;
1019 } 1019 }
1020 } 1020 }
1021 1021
1022 return ret; 1022 return ret;
1023 } 1023 }
1024 1024
1025 /** 1025 /**
1026 * ab8500_charger_ac_en() - enable or disable ac charging 1026 * ab8500_charger_ac_en() - enable or disable ac charging
1027 * @di: pointer to the ab8500_charger structure 1027 * @di: pointer to the ab8500_charger structure
1028 * @enable: enable/disable flag 1028 * @enable: enable/disable flag
1029 * @vset: charging voltage 1029 * @vset: charging voltage
1030 * @iset: charging current 1030 * @iset: charging current
1031 * 1031 *
1032 * Enable/Disable AC/Mains charging and turns on/off the charging led 1032 * Enable/Disable AC/Mains charging and turns on/off the charging led
1033 * respectively. 1033 * respectively.
1034 **/ 1034 **/
1035 static int ab8500_charger_ac_en(struct ux500_charger *charger, 1035 static int ab8500_charger_ac_en(struct ux500_charger *charger,
1036 int enable, int vset, int iset) 1036 int enable, int vset, int iset)
1037 { 1037 {
1038 int ret; 1038 int ret;
1039 int volt_index; 1039 int volt_index;
1040 int curr_index; 1040 int curr_index;
1041 int input_curr_index; 1041 int input_curr_index;
1042 u8 overshoot = 0; 1042 u8 overshoot = 0;
1043 1043
1044 struct ab8500_charger *di = to_ab8500_charger_ac_device_info(charger); 1044 struct ab8500_charger *di = to_ab8500_charger_ac_device_info(charger);
1045 1045
1046 if (enable) { 1046 if (enable) {
1047 /* Check if AC is connected */ 1047 /* Check if AC is connected */
1048 if (!di->ac.charger_connected) { 1048 if (!di->ac.charger_connected) {
1049 dev_err(di->dev, "AC charger not connected\n"); 1049 dev_err(di->dev, "AC charger not connected\n");
1050 return -ENXIO; 1050 return -ENXIO;
1051 } 1051 }
1052 1052
1053 /* Enable AC charging */ 1053 /* Enable AC charging */
1054 dev_dbg(di->dev, "Enable AC: %dmV %dmA\n", vset, iset); 1054 dev_dbg(di->dev, "Enable AC: %dmV %dmA\n", vset, iset);
1055 1055
1056 /* 1056 /*
1057 * Due to a bug in AB8500, BTEMP_HIGH/LOW interrupts 1057 * Due to a bug in AB8500, BTEMP_HIGH/LOW interrupts
1058 * will be triggered everytime we enable the VDD ADC supply. 1058 * will be triggered everytime we enable the VDD ADC supply.
1059 * This will turn off charging for a short while. 1059 * This will turn off charging for a short while.
1060 * It can be avoided by having the supply on when 1060 * It can be avoided by having the supply on when
1061 * there is a charger enabled. Normally the VDD ADC supply 1061 * there is a charger enabled. Normally the VDD ADC supply
1062 * is enabled everytime a GPADC conversion is triggered. We will 1062 * is enabled everytime a GPADC conversion is triggered. We will
1063 * force it to be enabled from this driver to have 1063 * force it to be enabled from this driver to have
1064 * the GPADC module independant of the AB8500 chargers 1064 * the GPADC module independant of the AB8500 chargers
1065 */ 1065 */
1066 if (!di->vddadc_en_ac) { 1066 if (!di->vddadc_en_ac) {
1067 regulator_enable(di->regu); 1067 regulator_enable(di->regu);
1068 di->vddadc_en_ac = true; 1068 di->vddadc_en_ac = true;
1069 } 1069 }
1070 1070
1071 /* Check if the requested voltage or current is valid */ 1071 /* Check if the requested voltage or current is valid */
1072 volt_index = ab8500_voltage_to_regval(vset); 1072 volt_index = ab8500_voltage_to_regval(vset);
1073 curr_index = ab8500_current_to_regval(iset); 1073 curr_index = ab8500_current_to_regval(iset);
1074 input_curr_index = ab8500_current_to_regval( 1074 input_curr_index = ab8500_current_to_regval(
1075 di->bat->chg_params->ac_curr_max); 1075 di->bat->chg_params->ac_curr_max);
1076 if (volt_index < 0 || curr_index < 0 || input_curr_index < 0) { 1076 if (volt_index < 0 || curr_index < 0 || input_curr_index < 0) {
1077 dev_err(di->dev, 1077 dev_err(di->dev,
1078 "Charger voltage or current too high, " 1078 "Charger voltage or current too high, "
1079 "charging not started\n"); 1079 "charging not started\n");
1080 return -ENXIO; 1080 return -ENXIO;
1081 } 1081 }
1082 1082
1083 /* ChVoltLevel: maximum battery charging voltage */ 1083 /* ChVoltLevel: maximum battery charging voltage */
1084 ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER, 1084 ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
1085 AB8500_CH_VOLT_LVL_REG, (u8) volt_index); 1085 AB8500_CH_VOLT_LVL_REG, (u8) volt_index);
1086 if (ret) { 1086 if (ret) {
1087 dev_err(di->dev, "%s write failed\n", __func__); 1087 dev_err(di->dev, "%s write failed\n", __func__);
1088 return ret; 1088 return ret;
1089 } 1089 }
1090 /* MainChInputCurr: current that can be drawn from the charger*/ 1090 /* MainChInputCurr: current that can be drawn from the charger*/
1091 ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER, 1091 ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
1092 AB8500_MCH_IPT_CURLVL_REG, 1092 AB8500_MCH_IPT_CURLVL_REG,
1093 input_curr_index << MAIN_CH_INPUT_CURR_SHIFT); 1093 input_curr_index << MAIN_CH_INPUT_CURR_SHIFT);
1094 if (ret) { 1094 if (ret) {
1095 dev_err(di->dev, "%s write failed\n", __func__); 1095 dev_err(di->dev, "%s write failed\n", __func__);
1096 return ret; 1096 return ret;
1097 } 1097 }
1098 /* ChOutputCurentLevel: protected output current */ 1098 /* ChOutputCurentLevel: protected output current */
1099 ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER, 1099 ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
1100 AB8500_CH_OPT_CRNTLVL_REG, (u8) curr_index); 1100 AB8500_CH_OPT_CRNTLVL_REG, (u8) curr_index);
1101 if (ret) { 1101 if (ret) {
1102 dev_err(di->dev, "%s write failed\n", __func__); 1102 dev_err(di->dev, "%s write failed\n", __func__);
1103 return ret; 1103 return ret;
1104 } 1104 }
1105 1105
1106 /* Check if VBAT overshoot control should be enabled */ 1106 /* Check if VBAT overshoot control should be enabled */
1107 if (!di->bat->enable_overshoot) 1107 if (!di->bat->enable_overshoot)
1108 overshoot = MAIN_CH_NO_OVERSHOOT_ENA_N; 1108 overshoot = MAIN_CH_NO_OVERSHOOT_ENA_N;
1109 1109
1110 /* Enable Main Charger */ 1110 /* Enable Main Charger */
1111 ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER, 1111 ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
1112 AB8500_MCH_CTRL1, MAIN_CH_ENA | overshoot); 1112 AB8500_MCH_CTRL1, MAIN_CH_ENA | overshoot);
1113 if (ret) { 1113 if (ret) {
1114 dev_err(di->dev, "%s write failed\n", __func__); 1114 dev_err(di->dev, "%s write failed\n", __func__);
1115 return ret; 1115 return ret;
1116 } 1116 }
1117 1117
1118 /* Power on charging LED indication */ 1118 /* Power on charging LED indication */
1119 ret = ab8500_charger_led_en(di, true); 1119 ret = ab8500_charger_led_en(di, true);
1120 if (ret < 0) 1120 if (ret < 0)
1121 dev_err(di->dev, "failed to enable LED\n"); 1121 dev_err(di->dev, "failed to enable LED\n");
1122 1122
1123 di->ac.charger_online = 1; 1123 di->ac.charger_online = 1;
1124 } else { 1124 } else {
1125 /* Disable AC charging */ 1125 /* Disable AC charging */
1126 if (is_ab8500_1p1_or_earlier(di->parent)) { 1126 if (is_ab8500_1p1_or_earlier(di->parent)) {
1127 /* 1127 /*
1128 * For ABB revision 1.0 and 1.1 there is a bug in the 1128 * For ABB revision 1.0 and 1.1 there is a bug in the
1129 * watchdog logic. That means we have to continously 1129 * watchdog logic. That means we have to continously
1130 * kick the charger watchdog even when no charger is 1130 * kick the charger watchdog even when no charger is
1131 * connected. This is only valid once the AC charger 1131 * connected. This is only valid once the AC charger
1132 * has been enabled. This is a bug that is not handled 1132 * has been enabled. This is a bug that is not handled
1133 * by the algorithm and the watchdog have to be kicked 1133 * by the algorithm and the watchdog have to be kicked
1134 * by the charger driver when the AC charger 1134 * by the charger driver when the AC charger
1135 * is disabled 1135 * is disabled
1136 */ 1136 */
1137 if (di->ac_conn) { 1137 if (di->ac_conn) {
1138 queue_delayed_work(di->charger_wq, 1138 queue_delayed_work(di->charger_wq,
1139 &di->kick_wd_work, 1139 &di->kick_wd_work,
1140 round_jiffies(WD_KICK_INTERVAL)); 1140 round_jiffies(WD_KICK_INTERVAL));
1141 } 1141 }
1142 1142
1143 /* 1143 /*
1144 * We can't turn off charging completely 1144 * We can't turn off charging completely
1145 * due to a bug in AB8500 cut1. 1145 * due to a bug in AB8500 cut1.
1146 * If we do, charging will not start again. 1146 * If we do, charging will not start again.
1147 * That is why we set the lowest voltage 1147 * That is why we set the lowest voltage
1148 * and current possible 1148 * and current possible
1149 */ 1149 */
1150 ret = abx500_set_register_interruptible(di->dev, 1150 ret = abx500_set_register_interruptible(di->dev,
1151 AB8500_CHARGER, 1151 AB8500_CHARGER,
1152 AB8500_CH_VOLT_LVL_REG, CH_VOL_LVL_3P5); 1152 AB8500_CH_VOLT_LVL_REG, CH_VOL_LVL_3P5);
1153 if (ret) { 1153 if (ret) {
1154 dev_err(di->dev, 1154 dev_err(di->dev,
1155 "%s write failed\n", __func__); 1155 "%s write failed\n", __func__);
1156 return ret; 1156 return ret;
1157 } 1157 }
1158 1158
1159 ret = abx500_set_register_interruptible(di->dev, 1159 ret = abx500_set_register_interruptible(di->dev,
1160 AB8500_CHARGER, 1160 AB8500_CHARGER,
1161 AB8500_CH_OPT_CRNTLVL_REG, CH_OP_CUR_LVL_0P1); 1161 AB8500_CH_OPT_CRNTLVL_REG, CH_OP_CUR_LVL_0P1);
1162 if (ret) { 1162 if (ret) {
1163 dev_err(di->dev, 1163 dev_err(di->dev,
1164 "%s write failed\n", __func__); 1164 "%s write failed\n", __func__);
1165 return ret; 1165 return ret;
1166 } 1166 }
1167 } else { 1167 } else {
1168 ret = abx500_set_register_interruptible(di->dev, 1168 ret = abx500_set_register_interruptible(di->dev,
1169 AB8500_CHARGER, 1169 AB8500_CHARGER,
1170 AB8500_MCH_CTRL1, 0); 1170 AB8500_MCH_CTRL1, 0);
1171 if (ret) { 1171 if (ret) {
1172 dev_err(di->dev, 1172 dev_err(di->dev,
1173 "%s write failed\n", __func__); 1173 "%s write failed\n", __func__);
1174 return ret; 1174 return ret;
1175 } 1175 }
1176 } 1176 }
1177 1177
1178 ret = ab8500_charger_led_en(di, false); 1178 ret = ab8500_charger_led_en(di, false);
1179 if (ret < 0) 1179 if (ret < 0)
1180 dev_err(di->dev, "failed to disable LED\n"); 1180 dev_err(di->dev, "failed to disable LED\n");
1181 1181
1182 di->ac.charger_online = 0; 1182 di->ac.charger_online = 0;
1183 di->ac.wd_expired = false; 1183 di->ac.wd_expired = false;
1184 1184
1185 /* Disable regulator if enabled */ 1185 /* Disable regulator if enabled */
1186 if (di->vddadc_en_ac) { 1186 if (di->vddadc_en_ac) {
1187 regulator_disable(di->regu); 1187 regulator_disable(di->regu);
1188 di->vddadc_en_ac = false; 1188 di->vddadc_en_ac = false;
1189 } 1189 }
1190 1190
1191 dev_dbg(di->dev, "%s Disabled AC charging\n", __func__); 1191 dev_dbg(di->dev, "%s Disabled AC charging\n", __func__);
1192 } 1192 }
1193 ab8500_power_supply_changed(di, &di->ac_chg.psy); 1193 ab8500_power_supply_changed(di, &di->ac_chg.psy);
1194 1194
1195 return ret; 1195 return ret;
1196 } 1196 }
1197 1197
1198 /** 1198 /**
1199 * ab8500_charger_usb_en() - enable usb charging 1199 * ab8500_charger_usb_en() - enable usb charging
1200 * @di: pointer to the ab8500_charger structure 1200 * @di: pointer to the ab8500_charger structure
1201 * @enable: enable/disable flag 1201 * @enable: enable/disable flag
1202 * @vset: charging voltage 1202 * @vset: charging voltage
1203 * @ich_out: charger output current 1203 * @ich_out: charger output current
1204 * 1204 *
1205 * Enable/Disable USB charging and turns on/off the charging led respectively. 1205 * Enable/Disable USB charging and turns on/off the charging led respectively.
1206 * Returns error code in case of failure else 0(on success) 1206 * Returns error code in case of failure else 0(on success)
1207 */ 1207 */
1208 static int ab8500_charger_usb_en(struct ux500_charger *charger, 1208 static int ab8500_charger_usb_en(struct ux500_charger *charger,
1209 int enable, int vset, int ich_out) 1209 int enable, int vset, int ich_out)
1210 { 1210 {
1211 int ret; 1211 int ret;
1212 int volt_index; 1212 int volt_index;
1213 int curr_index; 1213 int curr_index;
1214 u8 overshoot = 0; 1214 u8 overshoot = 0;
1215 1215
1216 struct ab8500_charger *di = to_ab8500_charger_usb_device_info(charger); 1216 struct ab8500_charger *di = to_ab8500_charger_usb_device_info(charger);
1217 1217
1218 if (enable) { 1218 if (enable) {
1219 /* Check if USB is connected */ 1219 /* Check if USB is connected */
1220 if (!di->usb.charger_connected) { 1220 if (!di->usb.charger_connected) {
1221 dev_err(di->dev, "USB charger not connected\n"); 1221 dev_err(di->dev, "USB charger not connected\n");
1222 return -ENXIO; 1222 return -ENXIO;
1223 } 1223 }
1224 1224
1225 /* 1225 /*
1226 * Due to a bug in AB8500, BTEMP_HIGH/LOW interrupts 1226 * Due to a bug in AB8500, BTEMP_HIGH/LOW interrupts
1227 * will be triggered everytime we enable the VDD ADC supply. 1227 * will be triggered everytime we enable the VDD ADC supply.
1228 * This will turn off charging for a short while. 1228 * This will turn off charging for a short while.
1229 * It can be avoided by having the supply on when 1229 * It can be avoided by having the supply on when
1230 * there is a charger enabled. Normally the VDD ADC supply 1230 * there is a charger enabled. Normally the VDD ADC supply
1231 * is enabled everytime a GPADC conversion is triggered. We will 1231 * is enabled everytime a GPADC conversion is triggered. We will
1232 * force it to be enabled from this driver to have 1232 * force it to be enabled from this driver to have
1233 * the GPADC module independant of the AB8500 chargers 1233 * the GPADC module independant of the AB8500 chargers
1234 */ 1234 */
1235 if (!di->vddadc_en_usb) { 1235 if (!di->vddadc_en_usb) {
1236 regulator_enable(di->regu); 1236 regulator_enable(di->regu);
1237 di->vddadc_en_usb = true; 1237 di->vddadc_en_usb = true;
1238 } 1238 }
1239 1239
1240 /* Enable USB charging */ 1240 /* Enable USB charging */
1241 dev_dbg(di->dev, "Enable USB: %dmV %dmA\n", vset, ich_out); 1241 dev_dbg(di->dev, "Enable USB: %dmV %dmA\n", vset, ich_out);
1242 1242
1243 /* Check if the requested voltage or current is valid */ 1243 /* Check if the requested voltage or current is valid */
1244 volt_index = ab8500_voltage_to_regval(vset); 1244 volt_index = ab8500_voltage_to_regval(vset);
1245 curr_index = ab8500_current_to_regval(ich_out); 1245 curr_index = ab8500_current_to_regval(ich_out);
1246 if (volt_index < 0 || curr_index < 0) { 1246 if (volt_index < 0 || curr_index < 0) {
1247 dev_err(di->dev, 1247 dev_err(di->dev,
1248 "Charger voltage or current too high, " 1248 "Charger voltage or current too high, "
1249 "charging not started\n"); 1249 "charging not started\n");
1250 return -ENXIO; 1250 return -ENXIO;
1251 } 1251 }
1252 1252
1253 /* ChVoltLevel: max voltage upto which battery can be charged */ 1253 /* ChVoltLevel: max voltage upto which battery can be charged */
1254 ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER, 1254 ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
1255 AB8500_CH_VOLT_LVL_REG, (u8) volt_index); 1255 AB8500_CH_VOLT_LVL_REG, (u8) volt_index);
1256 if (ret) { 1256 if (ret) {
1257 dev_err(di->dev, "%s write failed\n", __func__); 1257 dev_err(di->dev, "%s write failed\n", __func__);
1258 return ret; 1258 return ret;
1259 } 1259 }
1260 /* USBChInputCurr: current that can be drawn from the usb */ 1260 /* USBChInputCurr: current that can be drawn from the usb */
1261 ret = ab8500_charger_set_vbus_in_curr(di, di->max_usb_in_curr); 1261 ret = ab8500_charger_set_vbus_in_curr(di, di->max_usb_in_curr);
1262 if (ret) { 1262 if (ret) {
1263 dev_err(di->dev, "setting USBChInputCurr failed\n"); 1263 dev_err(di->dev, "setting USBChInputCurr failed\n");
1264 return ret; 1264 return ret;
1265 } 1265 }
1266 /* ChOutputCurentLevel: protected output current */ 1266 /* ChOutputCurentLevel: protected output current */
1267 ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER, 1267 ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
1268 AB8500_CH_OPT_CRNTLVL_REG, (u8) curr_index); 1268 AB8500_CH_OPT_CRNTLVL_REG, (u8) curr_index);
1269 if (ret) { 1269 if (ret) {
1270 dev_err(di->dev, "%s write failed\n", __func__); 1270 dev_err(di->dev, "%s write failed\n", __func__);
1271 return ret; 1271 return ret;
1272 } 1272 }
1273 /* Check if VBAT overshoot control should be enabled */ 1273 /* Check if VBAT overshoot control should be enabled */
1274 if (!di->bat->enable_overshoot) 1274 if (!di->bat->enable_overshoot)
1275 overshoot = USB_CHG_NO_OVERSHOOT_ENA_N; 1275 overshoot = USB_CHG_NO_OVERSHOOT_ENA_N;
1276 1276
1277 /* Enable USB Charger */ 1277 /* Enable USB Charger */
1278 ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER, 1278 ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
1279 AB8500_USBCH_CTRL1_REG, USB_CH_ENA | overshoot); 1279 AB8500_USBCH_CTRL1_REG, USB_CH_ENA | overshoot);
1280 if (ret) { 1280 if (ret) {
1281 dev_err(di->dev, "%s write failed\n", __func__); 1281 dev_err(di->dev, "%s write failed\n", __func__);
1282 return ret; 1282 return ret;
1283 } 1283 }
1284 1284
1285 /* If success power on charging LED indication */ 1285 /* If success power on charging LED indication */
1286 ret = ab8500_charger_led_en(di, true); 1286 ret = ab8500_charger_led_en(di, true);
1287 if (ret < 0) 1287 if (ret < 0)
1288 dev_err(di->dev, "failed to enable LED\n"); 1288 dev_err(di->dev, "failed to enable LED\n");
1289 1289
1290 queue_delayed_work(di->charger_wq, &di->check_vbat_work, HZ); 1290 queue_delayed_work(di->charger_wq, &di->check_vbat_work, HZ);
1291 1291
1292 di->usb.charger_online = 1; 1292 di->usb.charger_online = 1;
1293 } else { 1293 } else {
1294 /* Disable USB charging */ 1294 /* Disable USB charging */
1295 ret = abx500_set_register_interruptible(di->dev, 1295 ret = abx500_set_register_interruptible(di->dev,
1296 AB8500_CHARGER, 1296 AB8500_CHARGER,
1297 AB8500_USBCH_CTRL1_REG, 0); 1297 AB8500_USBCH_CTRL1_REG, 0);
1298 if (ret) { 1298 if (ret) {
1299 dev_err(di->dev, 1299 dev_err(di->dev,
1300 "%s write failed\n", __func__); 1300 "%s write failed\n", __func__);
1301 return ret; 1301 return ret;
1302 } 1302 }
1303 1303
1304 ret = ab8500_charger_led_en(di, false); 1304 ret = ab8500_charger_led_en(di, false);
1305 if (ret < 0) 1305 if (ret < 0)
1306 dev_err(di->dev, "failed to disable LED\n"); 1306 dev_err(di->dev, "failed to disable LED\n");
1307 1307
1308 di->usb.charger_online = 0; 1308 di->usb.charger_online = 0;
1309 di->usb.wd_expired = false; 1309 di->usb.wd_expired = false;
1310 1310
1311 /* Disable regulator if enabled */ 1311 /* Disable regulator if enabled */
1312 if (di->vddadc_en_usb) { 1312 if (di->vddadc_en_usb) {
1313 regulator_disable(di->regu); 1313 regulator_disable(di->regu);
1314 di->vddadc_en_usb = false; 1314 di->vddadc_en_usb = false;
1315 } 1315 }
1316 1316
1317 dev_dbg(di->dev, "%s Disabled USB charging\n", __func__); 1317 dev_dbg(di->dev, "%s Disabled USB charging\n", __func__);
1318 1318
1319 /* Cancel any pending Vbat check work */ 1319 /* Cancel any pending Vbat check work */
1320 if (delayed_work_pending(&di->check_vbat_work)) 1320 if (delayed_work_pending(&di->check_vbat_work))
1321 cancel_delayed_work(&di->check_vbat_work); 1321 cancel_delayed_work(&di->check_vbat_work);
1322 1322
1323 } 1323 }
1324 ab8500_power_supply_changed(di, &di->usb_chg.psy); 1324 ab8500_power_supply_changed(di, &di->usb_chg.psy);
1325 1325
1326 return ret; 1326 return ret;
1327 } 1327 }
1328 1328
1329 /** 1329 /**
1330 * ab8500_charger_watchdog_kick() - kick charger watchdog 1330 * ab8500_charger_watchdog_kick() - kick charger watchdog
1331 * @di: pointer to the ab8500_charger structure 1331 * @di: pointer to the ab8500_charger structure
1332 * 1332 *
1333 * Kick charger watchdog 1333 * Kick charger watchdog
1334 * Returns error code in case of failure else 0(on success) 1334 * Returns error code in case of failure else 0(on success)
1335 */ 1335 */
1336 static int ab8500_charger_watchdog_kick(struct ux500_charger *charger) 1336 static int ab8500_charger_watchdog_kick(struct ux500_charger *charger)
1337 { 1337 {
1338 int ret; 1338 int ret;
1339 struct ab8500_charger *di; 1339 struct ab8500_charger *di;
1340 1340
1341 if (charger->psy.type == POWER_SUPPLY_TYPE_MAINS) 1341 if (charger->psy.type == POWER_SUPPLY_TYPE_MAINS)
1342 di = to_ab8500_charger_ac_device_info(charger); 1342 di = to_ab8500_charger_ac_device_info(charger);
1343 else if (charger->psy.type == POWER_SUPPLY_TYPE_USB) 1343 else if (charger->psy.type == POWER_SUPPLY_TYPE_USB)
1344 di = to_ab8500_charger_usb_device_info(charger); 1344 di = to_ab8500_charger_usb_device_info(charger);
1345 else 1345 else
1346 return -ENXIO; 1346 return -ENXIO;
1347 1347
1348 ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER, 1348 ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
1349 AB8500_CHARG_WD_CTRL, CHARG_WD_KICK); 1349 AB8500_CHARG_WD_CTRL, CHARG_WD_KICK);
1350 if (ret) 1350 if (ret)
1351 dev_err(di->dev, "Failed to kick WD!\n"); 1351 dev_err(di->dev, "Failed to kick WD!\n");
1352 1352
1353 return ret; 1353 return ret;
1354 } 1354 }
1355 1355
1356 /** 1356 /**
1357 * ab8500_charger_update_charger_current() - update charger current 1357 * ab8500_charger_update_charger_current() - update charger current
1358 * @di: pointer to the ab8500_charger structure 1358 * @di: pointer to the ab8500_charger structure
1359 * 1359 *
1360 * Update the charger output current for the specified charger 1360 * Update the charger output current for the specified charger
1361 * Returns error code in case of failure else 0(on success) 1361 * Returns error code in case of failure else 0(on success)
1362 */ 1362 */
1363 static int ab8500_charger_update_charger_current(struct ux500_charger *charger, 1363 static int ab8500_charger_update_charger_current(struct ux500_charger *charger,
1364 int ich_out) 1364 int ich_out)
1365 { 1365 {
1366 int ret; 1366 int ret;
1367 int curr_index; 1367 int curr_index;
1368 struct ab8500_charger *di; 1368 struct ab8500_charger *di;
1369 1369
1370 if (charger->psy.type == POWER_SUPPLY_TYPE_MAINS) 1370 if (charger->psy.type == POWER_SUPPLY_TYPE_MAINS)
1371 di = to_ab8500_charger_ac_device_info(charger); 1371 di = to_ab8500_charger_ac_device_info(charger);
1372 else if (charger->psy.type == POWER_SUPPLY_TYPE_USB) 1372 else if (charger->psy.type == POWER_SUPPLY_TYPE_USB)
1373 di = to_ab8500_charger_usb_device_info(charger); 1373 di = to_ab8500_charger_usb_device_info(charger);
1374 else 1374 else
1375 return -ENXIO; 1375 return -ENXIO;
1376 1376
1377 curr_index = ab8500_current_to_regval(ich_out); 1377 curr_index = ab8500_current_to_regval(ich_out);
1378 if (curr_index < 0) { 1378 if (curr_index < 0) {
1379 dev_err(di->dev, 1379 dev_err(di->dev,
1380 "Charger current too high, " 1380 "Charger current too high, "
1381 "charging not started\n"); 1381 "charging not started\n");
1382 return -ENXIO; 1382 return -ENXIO;
1383 } 1383 }
1384 1384
1385 ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER, 1385 ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
1386 AB8500_CH_OPT_CRNTLVL_REG, (u8) curr_index); 1386 AB8500_CH_OPT_CRNTLVL_REG, (u8) curr_index);
1387 if (ret) { 1387 if (ret) {
1388 dev_err(di->dev, "%s write failed\n", __func__); 1388 dev_err(di->dev, "%s write failed\n", __func__);
1389 return ret; 1389 return ret;
1390 } 1390 }
1391 1391
1392 /* Reset the main and usb drop input current measurement counter */ 1392 /* Reset the main and usb drop input current measurement counter */
1393 ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER, 1393 ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
1394 AB8500_CHARGER_CTRL, 1394 AB8500_CHARGER_CTRL,
1395 0x1); 1395 0x1);
1396 if (ret) { 1396 if (ret) {
1397 dev_err(di->dev, "%s write failed\n", __func__); 1397 dev_err(di->dev, "%s write failed\n", __func__);
1398 return ret; 1398 return ret;
1399 } 1399 }
1400 1400
1401 return ret; 1401 return ret;
1402 } 1402 }
1403 1403
1404 static int ab8500_charger_get_ext_psy_data(struct device *dev, void *data) 1404 static int ab8500_charger_get_ext_psy_data(struct device *dev, void *data)
1405 { 1405 {
1406 struct power_supply *psy; 1406 struct power_supply *psy;
1407 struct power_supply *ext; 1407 struct power_supply *ext;
1408 struct ab8500_charger *di; 1408 struct ab8500_charger *di;
1409 union power_supply_propval ret; 1409 union power_supply_propval ret;
1410 int i, j; 1410 int i, j;
1411 bool psy_found = false; 1411 bool psy_found = false;
1412 struct ux500_charger *usb_chg; 1412 struct ux500_charger *usb_chg;
1413 1413
1414 usb_chg = (struct ux500_charger *)data; 1414 usb_chg = (struct ux500_charger *)data;
1415 psy = &usb_chg->psy; 1415 psy = &usb_chg->psy;
1416 1416
1417 di = to_ab8500_charger_usb_device_info(usb_chg); 1417 di = to_ab8500_charger_usb_device_info(usb_chg);
1418 1418
1419 ext = dev_get_drvdata(dev); 1419 ext = dev_get_drvdata(dev);
1420 1420
1421 /* For all psy where the driver name appears in any supplied_to */ 1421 /* For all psy where the driver name appears in any supplied_to */
1422 for (i = 0; i < ext->num_supplicants; i++) { 1422 for (i = 0; i < ext->num_supplicants; i++) {
1423 if (!strcmp(ext->supplied_to[i], psy->name)) 1423 if (!strcmp(ext->supplied_to[i], psy->name))
1424 psy_found = true; 1424 psy_found = true;
1425 } 1425 }
1426 1426
1427 if (!psy_found) 1427 if (!psy_found)
1428 return 0; 1428 return 0;
1429 1429
1430 /* Go through all properties for the psy */ 1430 /* Go through all properties for the psy */
1431 for (j = 0; j < ext->num_properties; j++) { 1431 for (j = 0; j < ext->num_properties; j++) {
1432 enum power_supply_property prop; 1432 enum power_supply_property prop;
1433 prop = ext->properties[j]; 1433 prop = ext->properties[j];
1434 1434
1435 if (ext->get_property(ext, prop, &ret)) 1435 if (ext->get_property(ext, prop, &ret))
1436 continue; 1436 continue;
1437 1437
1438 switch (prop) { 1438 switch (prop) {
1439 case POWER_SUPPLY_PROP_VOLTAGE_NOW: 1439 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
1440 switch (ext->type) { 1440 switch (ext->type) {
1441 case POWER_SUPPLY_TYPE_BATTERY: 1441 case POWER_SUPPLY_TYPE_BATTERY:
1442 di->vbat = ret.intval / 1000; 1442 di->vbat = ret.intval / 1000;
1443 break; 1443 break;
1444 default: 1444 default:
1445 break; 1445 break;
1446 } 1446 }
1447 break; 1447 break;
1448 default: 1448 default:
1449 break; 1449 break;
1450 } 1450 }
1451 } 1451 }
1452 return 0; 1452 return 0;
1453 } 1453 }
1454 1454
1455 /** 1455 /**
1456 * ab8500_charger_check_vbat_work() - keep vbus current within spec 1456 * ab8500_charger_check_vbat_work() - keep vbus current within spec
1457 * @work pointer to the work_struct structure 1457 * @work pointer to the work_struct structure
1458 * 1458 *
1459 * Due to a asic bug it is necessary to lower the input current to the vbus 1459 * Due to a asic bug it is necessary to lower the input current to the vbus
1460 * charger when charging with at some specific levels. This issue is only valid 1460 * charger when charging with at some specific levels. This issue is only valid
1461 * for below a certain battery voltage. This function makes sure that the 1461 * for below a certain battery voltage. This function makes sure that the
1462 * the allowed current limit isn't exceeded. 1462 * the allowed current limit isn't exceeded.
1463 */ 1463 */
1464 static void ab8500_charger_check_vbat_work(struct work_struct *work) 1464 static void ab8500_charger_check_vbat_work(struct work_struct *work)
1465 { 1465 {
1466 int t = 10; 1466 int t = 10;
1467 struct ab8500_charger *di = container_of(work, 1467 struct ab8500_charger *di = container_of(work,
1468 struct ab8500_charger, check_vbat_work.work); 1468 struct ab8500_charger, check_vbat_work.work);
1469 1469
1470 class_for_each_device(power_supply_class, NULL, 1470 class_for_each_device(power_supply_class, NULL,
1471 &di->usb_chg.psy, ab8500_charger_get_ext_psy_data); 1471 &di->usb_chg.psy, ab8500_charger_get_ext_psy_data);
1472 1472
1473 /* First run old_vbat is 0. */ 1473 /* First run old_vbat is 0. */
1474 if (di->old_vbat == 0) 1474 if (di->old_vbat == 0)
1475 di->old_vbat = di->vbat; 1475 di->old_vbat = di->vbat;
1476 1476
1477 if (!((di->old_vbat <= VBAT_TRESH_IP_CUR_RED && 1477 if (!((di->old_vbat <= VBAT_TRESH_IP_CUR_RED &&
1478 di->vbat <= VBAT_TRESH_IP_CUR_RED) || 1478 di->vbat <= VBAT_TRESH_IP_CUR_RED) ||
1479 (di->old_vbat > VBAT_TRESH_IP_CUR_RED && 1479 (di->old_vbat > VBAT_TRESH_IP_CUR_RED &&
1480 di->vbat > VBAT_TRESH_IP_CUR_RED))) { 1480 di->vbat > VBAT_TRESH_IP_CUR_RED))) {
1481 1481
1482 dev_dbg(di->dev, "Vbat did cross threshold, curr: %d, new: %d," 1482 dev_dbg(di->dev, "Vbat did cross threshold, curr: %d, new: %d,"
1483 " old: %d\n", di->max_usb_in_curr, di->vbat, 1483 " old: %d\n", di->max_usb_in_curr, di->vbat,
1484 di->old_vbat); 1484 di->old_vbat);
1485 ab8500_charger_set_vbus_in_curr(di, di->max_usb_in_curr); 1485 ab8500_charger_set_vbus_in_curr(di, di->max_usb_in_curr);
1486 power_supply_changed(&di->usb_chg.psy); 1486 power_supply_changed(&di->usb_chg.psy);
1487 } 1487 }
1488 1488
1489 di->old_vbat = di->vbat; 1489 di->old_vbat = di->vbat;
1490 1490
1491 /* 1491 /*
1492 * No need to check the battery voltage every second when not close to 1492 * No need to check the battery voltage every second when not close to
1493 * the threshold. 1493 * the threshold.
1494 */ 1494 */
1495 if (di->vbat < (VBAT_TRESH_IP_CUR_RED + 100) && 1495 if (di->vbat < (VBAT_TRESH_IP_CUR_RED + 100) &&
1496 (di->vbat > (VBAT_TRESH_IP_CUR_RED - 100))) 1496 (di->vbat > (VBAT_TRESH_IP_CUR_RED - 100)))
1497 t = 1; 1497 t = 1;
1498 1498
1499 queue_delayed_work(di->charger_wq, &di->check_vbat_work, t * HZ); 1499 queue_delayed_work(di->charger_wq, &di->check_vbat_work, t * HZ);
1500 } 1500 }
1501 1501
1502 /** 1502 /**
1503 * ab8500_charger_check_hw_failure_work() - check main charger failure 1503 * ab8500_charger_check_hw_failure_work() - check main charger failure
1504 * @work: pointer to the work_struct structure 1504 * @work: pointer to the work_struct structure
1505 * 1505 *
1506 * Work queue function for checking the main charger status 1506 * Work queue function for checking the main charger status
1507 */ 1507 */
1508 static void ab8500_charger_check_hw_failure_work(struct work_struct *work) 1508 static void ab8500_charger_check_hw_failure_work(struct work_struct *work)
1509 { 1509 {
1510 int ret; 1510 int ret;
1511 u8 reg_value; 1511 u8 reg_value;
1512 1512
1513 struct ab8500_charger *di = container_of(work, 1513 struct ab8500_charger *di = container_of(work,
1514 struct ab8500_charger, check_hw_failure_work.work); 1514 struct ab8500_charger, check_hw_failure_work.work);
1515 1515
1516 /* Check if the status bits for HW failure is still active */ 1516 /* Check if the status bits for HW failure is still active */
1517 if (di->flags.mainextchnotok) { 1517 if (di->flags.mainextchnotok) {
1518 ret = abx500_get_register_interruptible(di->dev, 1518 ret = abx500_get_register_interruptible(di->dev,
1519 AB8500_CHARGER, AB8500_CH_STATUS2_REG, &reg_value); 1519 AB8500_CHARGER, AB8500_CH_STATUS2_REG, &reg_value);
1520 if (ret < 0) { 1520 if (ret < 0) {
1521 dev_err(di->dev, "%s ab8500 read failed\n", __func__); 1521 dev_err(di->dev, "%s ab8500 read failed\n", __func__);
1522 return; 1522 return;
1523 } 1523 }
1524 if (!(reg_value & MAIN_CH_NOK)) { 1524 if (!(reg_value & MAIN_CH_NOK)) {
1525 di->flags.mainextchnotok = false; 1525 di->flags.mainextchnotok = false;
1526 ab8500_power_supply_changed(di, &di->ac_chg.psy); 1526 ab8500_power_supply_changed(di, &di->ac_chg.psy);
1527 } 1527 }
1528 } 1528 }
1529 if (di->flags.vbus_ovv) { 1529 if (di->flags.vbus_ovv) {
1530 ret = abx500_get_register_interruptible(di->dev, 1530 ret = abx500_get_register_interruptible(di->dev,
1531 AB8500_CHARGER, AB8500_CH_USBCH_STAT2_REG, 1531 AB8500_CHARGER, AB8500_CH_USBCH_STAT2_REG,
1532 &reg_value); 1532 &reg_value);
1533 if (ret < 0) { 1533 if (ret < 0) {
1534 dev_err(di->dev, "%s ab8500 read failed\n", __func__); 1534 dev_err(di->dev, "%s ab8500 read failed\n", __func__);
1535 return; 1535 return;
1536 } 1536 }
1537 if (!(reg_value & VBUS_OVV_TH)) { 1537 if (!(reg_value & VBUS_OVV_TH)) {
1538 di->flags.vbus_ovv = false; 1538 di->flags.vbus_ovv = false;
1539 ab8500_power_supply_changed(di, &di->usb_chg.psy); 1539 ab8500_power_supply_changed(di, &di->usb_chg.psy);
1540 } 1540 }
1541 } 1541 }
1542 /* If we still have a failure, schedule a new check */ 1542 /* If we still have a failure, schedule a new check */
1543 if (di->flags.mainextchnotok || di->flags.vbus_ovv) { 1543 if (di->flags.mainextchnotok || di->flags.vbus_ovv) {
1544 queue_delayed_work(di->charger_wq, 1544 queue_delayed_work(di->charger_wq,
1545 &di->check_hw_failure_work, round_jiffies(HZ)); 1545 &di->check_hw_failure_work, round_jiffies(HZ));
1546 } 1546 }
1547 } 1547 }
1548 1548
1549 /** 1549 /**
1550 * ab8500_charger_kick_watchdog_work() - kick the watchdog 1550 * ab8500_charger_kick_watchdog_work() - kick the watchdog
1551 * @work: pointer to the work_struct structure 1551 * @work: pointer to the work_struct structure
1552 * 1552 *
1553 * Work queue function for kicking the charger watchdog. 1553 * Work queue function for kicking the charger watchdog.
1554 * 1554 *
1555 * For ABB revision 1.0 and 1.1 there is a bug in the watchdog 1555 * For ABB revision 1.0 and 1.1 there is a bug in the watchdog
1556 * logic. That means we have to continously kick the charger 1556 * logic. That means we have to continously kick the charger
1557 * watchdog even when no charger is connected. This is only 1557 * watchdog even when no charger is connected. This is only
1558 * valid once the AC charger has been enabled. This is 1558 * valid once the AC charger has been enabled. This is
1559 * a bug that is not handled by the algorithm and the 1559 * a bug that is not handled by the algorithm and the
1560 * watchdog have to be kicked by the charger driver 1560 * watchdog have to be kicked by the charger driver
1561 * when the AC charger is disabled 1561 * when the AC charger is disabled
1562 */ 1562 */
1563 static void ab8500_charger_kick_watchdog_work(struct work_struct *work) 1563 static void ab8500_charger_kick_watchdog_work(struct work_struct *work)
1564 { 1564 {
1565 int ret; 1565 int ret;
1566 1566
1567 struct ab8500_charger *di = container_of(work, 1567 struct ab8500_charger *di = container_of(work,
1568 struct ab8500_charger, kick_wd_work.work); 1568 struct ab8500_charger, kick_wd_work.work);
1569 1569
1570 ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER, 1570 ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
1571 AB8500_CHARG_WD_CTRL, CHARG_WD_KICK); 1571 AB8500_CHARG_WD_CTRL, CHARG_WD_KICK);
1572 if (ret) 1572 if (ret)
1573 dev_err(di->dev, "Failed to kick WD!\n"); 1573 dev_err(di->dev, "Failed to kick WD!\n");
1574 1574
1575 /* Schedule a new watchdog kick */ 1575 /* Schedule a new watchdog kick */
1576 queue_delayed_work(di->charger_wq, 1576 queue_delayed_work(di->charger_wq,
1577 &di->kick_wd_work, round_jiffies(WD_KICK_INTERVAL)); 1577 &di->kick_wd_work, round_jiffies(WD_KICK_INTERVAL));
1578 } 1578 }
1579 1579
1580 /** 1580 /**
1581 * ab8500_charger_ac_work() - work to get and set main charger status 1581 * ab8500_charger_ac_work() - work to get and set main charger status
1582 * @work: pointer to the work_struct structure 1582 * @work: pointer to the work_struct structure
1583 * 1583 *
1584 * Work queue function for checking the main charger status 1584 * Work queue function for checking the main charger status
1585 */ 1585 */
1586 static void ab8500_charger_ac_work(struct work_struct *work) 1586 static void ab8500_charger_ac_work(struct work_struct *work)
1587 { 1587 {
1588 int ret; 1588 int ret;
1589 1589
1590 struct ab8500_charger *di = container_of(work, 1590 struct ab8500_charger *di = container_of(work,
1591 struct ab8500_charger, ac_work); 1591 struct ab8500_charger, ac_work);
1592 1592
1593 /* 1593 /*
1594 * Since we can't be sure that the events are received 1594 * Since we can't be sure that the events are received
1595 * synchronously, we have the check if the main charger is 1595 * synchronously, we have the check if the main charger is
1596 * connected by reading the status register 1596 * connected by reading the status register
1597 */ 1597 */
1598 ret = ab8500_charger_detect_chargers(di); 1598 ret = ab8500_charger_detect_chargers(di);
1599 if (ret < 0) 1599 if (ret < 0)
1600 return; 1600 return;
1601 1601
1602 if (ret & AC_PW_CONN) { 1602 if (ret & AC_PW_CONN) {
1603 di->ac.charger_connected = 1; 1603 di->ac.charger_connected = 1;
1604 di->ac_conn = true; 1604 di->ac_conn = true;
1605 } else { 1605 } else {
1606 di->ac.charger_connected = 0; 1606 di->ac.charger_connected = 0;
1607 } 1607 }
1608 1608
1609 ab8500_power_supply_changed(di, &di->ac_chg.psy); 1609 ab8500_power_supply_changed(di, &di->ac_chg.psy);
1610 sysfs_notify(&di->ac_chg.psy.dev->kobj, NULL, "present"); 1610 sysfs_notify(&di->ac_chg.psy.dev->kobj, NULL, "present");
1611 } 1611 }
1612 1612
1613 /** 1613 /**
1614 * ab8500_charger_detect_usb_type_work() - work to detect USB type 1614 * ab8500_charger_detect_usb_type_work() - work to detect USB type
1615 * @work: Pointer to the work_struct structure 1615 * @work: Pointer to the work_struct structure
1616 * 1616 *
1617 * Detect the type of USB plugged 1617 * Detect the type of USB plugged
1618 */ 1618 */
1619 static void ab8500_charger_detect_usb_type_work(struct work_struct *work) 1619 static void ab8500_charger_detect_usb_type_work(struct work_struct *work)
1620 { 1620 {
1621 int ret; 1621 int ret;
1622 1622
1623 struct ab8500_charger *di = container_of(work, 1623 struct ab8500_charger *di = container_of(work,
1624 struct ab8500_charger, detect_usb_type_work); 1624 struct ab8500_charger, detect_usb_type_work);
1625 1625
1626 /* 1626 /*
1627 * Since we can't be sure that the events are received 1627 * Since we can't be sure that the events are received
1628 * synchronously, we have the check if is 1628 * synchronously, we have the check if is
1629 * connected by reading the status register 1629 * connected by reading the status register
1630 */ 1630 */
1631 ret = ab8500_charger_detect_chargers(di); 1631 ret = ab8500_charger_detect_chargers(di);
1632 if (ret < 0) 1632 if (ret < 0)
1633 return; 1633 return;
1634 1634
1635 if (!(ret & USB_PW_CONN)) { 1635 if (!(ret & USB_PW_CONN)) {
1636 di->vbus_detected = 0; 1636 di->vbus_detected = 0;
1637 ab8500_charger_set_usb_connected(di, false); 1637 ab8500_charger_set_usb_connected(di, false);
1638 ab8500_power_supply_changed(di, &di->usb_chg.psy); 1638 ab8500_power_supply_changed(di, &di->usb_chg.psy);
1639 } else { 1639 } else {
1640 di->vbus_detected = 1; 1640 di->vbus_detected = 1;
1641 1641
1642 if (is_ab8500_1p1_or_earlier(di->parent)) { 1642 if (is_ab8500_1p1_or_earlier(di->parent)) {
1643 ret = ab8500_charger_detect_usb_type(di); 1643 ret = ab8500_charger_detect_usb_type(di);
1644 if (!ret) { 1644 if (!ret) {
1645 ab8500_charger_set_usb_connected(di, true); 1645 ab8500_charger_set_usb_connected(di, true);
1646 ab8500_power_supply_changed(di, 1646 ab8500_power_supply_changed(di,
1647 &di->usb_chg.psy); 1647 &di->usb_chg.psy);
1648 } 1648 }
1649 } else { 1649 } else {
1650 /* For ABB cut2.0 and onwards we have an IRQ, 1650 /* For ABB cut2.0 and onwards we have an IRQ,
1651 * USB_LINK_STATUS that will be triggered when the USB 1651 * USB_LINK_STATUS that will be triggered when the USB
1652 * link status changes. The exception is USB connected 1652 * link status changes. The exception is USB connected
1653 * during startup. Then we don't get a 1653 * during startup. Then we don't get a
1654 * USB_LINK_STATUS IRQ 1654 * USB_LINK_STATUS IRQ
1655 */ 1655 */
1656 if (di->vbus_detected_start) { 1656 if (di->vbus_detected_start) {
1657 di->vbus_detected_start = false; 1657 di->vbus_detected_start = false;
1658 ret = ab8500_charger_detect_usb_type(di); 1658 ret = ab8500_charger_detect_usb_type(di);
1659 if (!ret) { 1659 if (!ret) {
1660 ab8500_charger_set_usb_connected(di, 1660 ab8500_charger_set_usb_connected(di,
1661 true); 1661 true);
1662 ab8500_power_supply_changed(di, 1662 ab8500_power_supply_changed(di,
1663 &di->usb_chg.psy); 1663 &di->usb_chg.psy);
1664 } 1664 }
1665 } 1665 }
1666 } 1666 }
1667 } 1667 }
1668 } 1668 }
1669 1669
1670 /** 1670 /**
1671 * ab8500_charger_usb_link_status_work() - work to detect USB type 1671 * ab8500_charger_usb_link_status_work() - work to detect USB type
1672 * @work: pointer to the work_struct structure 1672 * @work: pointer to the work_struct structure
1673 * 1673 *
1674 * Detect the type of USB plugged 1674 * Detect the type of USB plugged
1675 */ 1675 */
1676 static void ab8500_charger_usb_link_status_work(struct work_struct *work) 1676 static void ab8500_charger_usb_link_status_work(struct work_struct *work)
1677 { 1677 {
1678 int ret; 1678 int ret;
1679 1679
1680 struct ab8500_charger *di = container_of(work, 1680 struct ab8500_charger *di = container_of(work,
1681 struct ab8500_charger, usb_link_status_work); 1681 struct ab8500_charger, usb_link_status_work);
1682 1682
1683 /* 1683 /*
1684 * Since we can't be sure that the events are received 1684 * Since we can't be sure that the events are received
1685 * synchronously, we have the check if is 1685 * synchronously, we have the check if is
1686 * connected by reading the status register 1686 * connected by reading the status register
1687 */ 1687 */
1688 ret = ab8500_charger_detect_chargers(di); 1688 ret = ab8500_charger_detect_chargers(di);
1689 if (ret < 0) 1689 if (ret < 0)
1690 return; 1690 return;
1691 1691
1692 if (!(ret & USB_PW_CONN)) { 1692 if (!(ret & USB_PW_CONN)) {
1693 di->vbus_detected = 0; 1693 di->vbus_detected = 0;
1694 ab8500_charger_set_usb_connected(di, false); 1694 ab8500_charger_set_usb_connected(di, false);
1695 ab8500_power_supply_changed(di, &di->usb_chg.psy); 1695 ab8500_power_supply_changed(di, &di->usb_chg.psy);
1696 } else { 1696 } else {
1697 di->vbus_detected = 1; 1697 di->vbus_detected = 1;
1698 ret = ab8500_charger_read_usb_type(di); 1698 ret = ab8500_charger_read_usb_type(di);
1699 if (!ret) { 1699 if (!ret) {
1700 /* Update maximum input current */ 1700 /* Update maximum input current */
1701 ret = ab8500_charger_set_vbus_in_curr(di, 1701 ret = ab8500_charger_set_vbus_in_curr(di,
1702 di->max_usb_in_curr); 1702 di->max_usb_in_curr);
1703 if (ret) 1703 if (ret)
1704 return; 1704 return;
1705 1705
1706 ab8500_charger_set_usb_connected(di, true); 1706 ab8500_charger_set_usb_connected(di, true);
1707 ab8500_power_supply_changed(di, &di->usb_chg.psy); 1707 ab8500_power_supply_changed(di, &di->usb_chg.psy);
1708 } else if (ret == -ENXIO) { 1708 } else if (ret == -ENXIO) {
1709 /* No valid charger type detected */ 1709 /* No valid charger type detected */
1710 ab8500_charger_set_usb_connected(di, false); 1710 ab8500_charger_set_usb_connected(di, false);
1711 ab8500_power_supply_changed(di, &di->usb_chg.psy); 1711 ab8500_power_supply_changed(di, &di->usb_chg.psy);
1712 } 1712 }
1713 } 1713 }
1714 } 1714 }
1715 1715
1716 static void ab8500_charger_usb_state_changed_work(struct work_struct *work) 1716 static void ab8500_charger_usb_state_changed_work(struct work_struct *work)
1717 { 1717 {
1718 int ret; 1718 int ret;
1719 unsigned long flags; 1719 unsigned long flags;
1720 1720
1721 struct ab8500_charger *di = container_of(work, 1721 struct ab8500_charger *di = container_of(work,
1722 struct ab8500_charger, usb_state_changed_work); 1722 struct ab8500_charger, usb_state_changed_work);
1723 1723
1724 if (!di->vbus_detected) 1724 if (!di->vbus_detected)
1725 return; 1725 return;
1726 1726
1727 spin_lock_irqsave(&di->usb_state.usb_lock, flags); 1727 spin_lock_irqsave(&di->usb_state.usb_lock, flags);
1728 di->usb_state.usb_changed = false; 1728 di->usb_state.usb_changed = false;
1729 spin_unlock_irqrestore(&di->usb_state.usb_lock, flags); 1729 spin_unlock_irqrestore(&di->usb_state.usb_lock, flags);
1730 1730
1731 /* 1731 /*
1732 * wait for some time until you get updates from the usb stack 1732 * wait for some time until you get updates from the usb stack
1733 * and negotiations are completed 1733 * and negotiations are completed
1734 */ 1734 */
1735 msleep(250); 1735 msleep(250);
1736 1736
1737 if (di->usb_state.usb_changed) 1737 if (di->usb_state.usb_changed)
1738 return; 1738 return;
1739 1739
1740 dev_dbg(di->dev, "%s USB state: 0x%02x mA: %d\n", 1740 dev_dbg(di->dev, "%s USB state: 0x%02x mA: %d\n",
1741 __func__, di->usb_state.state, di->usb_state.usb_current); 1741 __func__, di->usb_state.state, di->usb_state.usb_current);
1742 1742
1743 switch (di->usb_state.state) { 1743 switch (di->usb_state.state) {
1744 case AB8500_BM_USB_STATE_RESET_HS: 1744 case AB8500_BM_USB_STATE_RESET_HS:
1745 case AB8500_BM_USB_STATE_RESET_FS: 1745 case AB8500_BM_USB_STATE_RESET_FS:
1746 case AB8500_BM_USB_STATE_SUSPEND: 1746 case AB8500_BM_USB_STATE_SUSPEND:
1747 case AB8500_BM_USB_STATE_MAX: 1747 case AB8500_BM_USB_STATE_MAX:
1748 ab8500_charger_set_usb_connected(di, false); 1748 ab8500_charger_set_usb_connected(di, false);
1749 ab8500_power_supply_changed(di, &di->usb_chg.psy); 1749 ab8500_power_supply_changed(di, &di->usb_chg.psy);
1750 break; 1750 break;
1751 1751
1752 case AB8500_BM_USB_STATE_RESUME: 1752 case AB8500_BM_USB_STATE_RESUME:
1753 /* 1753 /*
1754 * when suspend->resume there should be delay 1754 * when suspend->resume there should be delay
1755 * of 1sec for enabling charging 1755 * of 1sec for enabling charging
1756 */ 1756 */
1757 msleep(1000); 1757 msleep(1000);
1758 /* Intentional fall through */ 1758 /* Intentional fall through */
1759 case AB8500_BM_USB_STATE_CONFIGURED: 1759 case AB8500_BM_USB_STATE_CONFIGURED:
1760 /* 1760 /*
1761 * USB is configured, enable charging with the charging 1761 * USB is configured, enable charging with the charging
1762 * input current obtained from USB driver 1762 * input current obtained from USB driver
1763 */ 1763 */
1764 if (!ab8500_charger_get_usb_cur(di)) { 1764 if (!ab8500_charger_get_usb_cur(di)) {
1765 /* Update maximum input current */ 1765 /* Update maximum input current */
1766 ret = ab8500_charger_set_vbus_in_curr(di, 1766 ret = ab8500_charger_set_vbus_in_curr(di,
1767 di->max_usb_in_curr); 1767 di->max_usb_in_curr);
1768 if (ret) 1768 if (ret)
1769 return; 1769 return;
1770 1770
1771 ab8500_charger_set_usb_connected(di, true); 1771 ab8500_charger_set_usb_connected(di, true);
1772 ab8500_power_supply_changed(di, &di->usb_chg.psy); 1772 ab8500_power_supply_changed(di, &di->usb_chg.psy);
1773 } 1773 }
1774 break; 1774 break;
1775 1775
1776 default: 1776 default:
1777 break; 1777 break;
1778 }; 1778 };
1779 } 1779 }
1780 1780
1781 /** 1781 /**
1782 * ab8500_charger_check_usbchargernotok_work() - check USB chg not ok status 1782 * ab8500_charger_check_usbchargernotok_work() - check USB chg not ok status
1783 * @work: pointer to the work_struct structure 1783 * @work: pointer to the work_struct structure
1784 * 1784 *
1785 * Work queue function for checking the USB charger Not OK status 1785 * Work queue function for checking the USB charger Not OK status
1786 */ 1786 */
1787 static void ab8500_charger_check_usbchargernotok_work(struct work_struct *work) 1787 static void ab8500_charger_check_usbchargernotok_work(struct work_struct *work)
1788 { 1788 {
1789 int ret; 1789 int ret;
1790 u8 reg_value; 1790 u8 reg_value;
1791 bool prev_status; 1791 bool prev_status;
1792 1792
1793 struct ab8500_charger *di = container_of(work, 1793 struct ab8500_charger *di = container_of(work,
1794 struct ab8500_charger, check_usbchgnotok_work.work); 1794 struct ab8500_charger, check_usbchgnotok_work.work);
1795 1795
1796 /* Check if the status bit for usbchargernotok is still active */ 1796 /* Check if the status bit for usbchargernotok is still active */
1797 ret = abx500_get_register_interruptible(di->dev, 1797 ret = abx500_get_register_interruptible(di->dev,
1798 AB8500_CHARGER, AB8500_CH_USBCH_STAT2_REG, &reg_value); 1798 AB8500_CHARGER, AB8500_CH_USBCH_STAT2_REG, &reg_value);
1799 if (ret < 0) { 1799 if (ret < 0) {
1800 dev_err(di->dev, "%s ab8500 read failed\n", __func__); 1800 dev_err(di->dev, "%s ab8500 read failed\n", __func__);
1801 return; 1801 return;
1802 } 1802 }
1803 prev_status = di->flags.usbchargernotok; 1803 prev_status = di->flags.usbchargernotok;
1804 1804
1805 if (reg_value & VBUS_CH_NOK) { 1805 if (reg_value & VBUS_CH_NOK) {
1806 di->flags.usbchargernotok = true; 1806 di->flags.usbchargernotok = true;
1807 /* Check again in 1sec */ 1807 /* Check again in 1sec */
1808 queue_delayed_work(di->charger_wq, 1808 queue_delayed_work(di->charger_wq,
1809 &di->check_usbchgnotok_work, HZ); 1809 &di->check_usbchgnotok_work, HZ);
1810 } else { 1810 } else {
1811 di->flags.usbchargernotok = false; 1811 di->flags.usbchargernotok = false;
1812 di->flags.vbus_collapse = false; 1812 di->flags.vbus_collapse = false;
1813 } 1813 }
1814 1814
1815 if (prev_status != di->flags.usbchargernotok) 1815 if (prev_status != di->flags.usbchargernotok)
1816 ab8500_power_supply_changed(di, &di->usb_chg.psy); 1816 ab8500_power_supply_changed(di, &di->usb_chg.psy);
1817 } 1817 }
1818 1818
1819 /** 1819 /**
1820 * ab8500_charger_check_main_thermal_prot_work() - check main thermal status 1820 * ab8500_charger_check_main_thermal_prot_work() - check main thermal status
1821 * @work: pointer to the work_struct structure 1821 * @work: pointer to the work_struct structure
1822 * 1822 *
1823 * Work queue function for checking the Main thermal prot status 1823 * Work queue function for checking the Main thermal prot status
1824 */ 1824 */
1825 static void ab8500_charger_check_main_thermal_prot_work( 1825 static void ab8500_charger_check_main_thermal_prot_work(
1826 struct work_struct *work) 1826 struct work_struct *work)
1827 { 1827 {
1828 int ret; 1828 int ret;
1829 u8 reg_value; 1829 u8 reg_value;
1830 1830
1831 struct ab8500_charger *di = container_of(work, 1831 struct ab8500_charger *di = container_of(work,
1832 struct ab8500_charger, check_main_thermal_prot_work); 1832 struct ab8500_charger, check_main_thermal_prot_work);
1833 1833
1834 /* Check if the status bit for main_thermal_prot is still active */ 1834 /* Check if the status bit for main_thermal_prot is still active */
1835 ret = abx500_get_register_interruptible(di->dev, 1835 ret = abx500_get_register_interruptible(di->dev,
1836 AB8500_CHARGER, AB8500_CH_STATUS2_REG, &reg_value); 1836 AB8500_CHARGER, AB8500_CH_STATUS2_REG, &reg_value);
1837 if (ret < 0) { 1837 if (ret < 0) {
1838 dev_err(di->dev, "%s ab8500 read failed\n", __func__); 1838 dev_err(di->dev, "%s ab8500 read failed\n", __func__);
1839 return; 1839 return;
1840 } 1840 }
1841 if (reg_value & MAIN_CH_TH_PROT) 1841 if (reg_value & MAIN_CH_TH_PROT)
1842 di->flags.main_thermal_prot = true; 1842 di->flags.main_thermal_prot = true;
1843 else 1843 else
1844 di->flags.main_thermal_prot = false; 1844 di->flags.main_thermal_prot = false;
1845 1845
1846 ab8500_power_supply_changed(di, &di->ac_chg.psy); 1846 ab8500_power_supply_changed(di, &di->ac_chg.psy);
1847 } 1847 }
1848 1848
1849 /** 1849 /**
1850 * ab8500_charger_check_usb_thermal_prot_work() - check usb thermal status 1850 * ab8500_charger_check_usb_thermal_prot_work() - check usb thermal status
1851 * @work: pointer to the work_struct structure 1851 * @work: pointer to the work_struct structure
1852 * 1852 *
1853 * Work queue function for checking the USB thermal prot status 1853 * Work queue function for checking the USB thermal prot status
1854 */ 1854 */
1855 static void ab8500_charger_check_usb_thermal_prot_work( 1855 static void ab8500_charger_check_usb_thermal_prot_work(
1856 struct work_struct *work) 1856 struct work_struct *work)
1857 { 1857 {
1858 int ret; 1858 int ret;
1859 u8 reg_value; 1859 u8 reg_value;
1860 1860
1861 struct ab8500_charger *di = container_of(work, 1861 struct ab8500_charger *di = container_of(work,
1862 struct ab8500_charger, check_usb_thermal_prot_work); 1862 struct ab8500_charger, check_usb_thermal_prot_work);
1863 1863
1864 /* Check if the status bit for usb_thermal_prot is still active */ 1864 /* Check if the status bit for usb_thermal_prot is still active */
1865 ret = abx500_get_register_interruptible(di->dev, 1865 ret = abx500_get_register_interruptible(di->dev,
1866 AB8500_CHARGER, AB8500_CH_USBCH_STAT2_REG, &reg_value); 1866 AB8500_CHARGER, AB8500_CH_USBCH_STAT2_REG, &reg_value);
1867 if (ret < 0) { 1867 if (ret < 0) {
1868 dev_err(di->dev, "%s ab8500 read failed\n", __func__); 1868 dev_err(di->dev, "%s ab8500 read failed\n", __func__);
1869 return; 1869 return;
1870 } 1870 }
1871 if (reg_value & USB_CH_TH_PROT) 1871 if (reg_value & USB_CH_TH_PROT)
1872 di->flags.usb_thermal_prot = true; 1872 di->flags.usb_thermal_prot = true;
1873 else 1873 else
1874 di->flags.usb_thermal_prot = false; 1874 di->flags.usb_thermal_prot = false;
1875 1875
1876 ab8500_power_supply_changed(di, &di->usb_chg.psy); 1876 ab8500_power_supply_changed(di, &di->usb_chg.psy);
1877 } 1877 }
1878 1878
1879 /** 1879 /**
1880 * ab8500_charger_mainchunplugdet_handler() - main charger unplugged 1880 * ab8500_charger_mainchunplugdet_handler() - main charger unplugged
1881 * @irq: interrupt number 1881 * @irq: interrupt number
1882 * @_di: pointer to the ab8500_charger structure 1882 * @_di: pointer to the ab8500_charger structure
1883 * 1883 *
1884 * Returns IRQ status(IRQ_HANDLED) 1884 * Returns IRQ status(IRQ_HANDLED)
1885 */ 1885 */
1886 static irqreturn_t ab8500_charger_mainchunplugdet_handler(int irq, void *_di) 1886 static irqreturn_t ab8500_charger_mainchunplugdet_handler(int irq, void *_di)
1887 { 1887 {
1888 struct ab8500_charger *di = _di; 1888 struct ab8500_charger *di = _di;
1889 1889
1890 dev_dbg(di->dev, "Main charger unplugged\n"); 1890 dev_dbg(di->dev, "Main charger unplugged\n");
1891 queue_work(di->charger_wq, &di->ac_work); 1891 queue_work(di->charger_wq, &di->ac_work);
1892 1892
1893 return IRQ_HANDLED; 1893 return IRQ_HANDLED;
1894 } 1894 }
1895 1895
1896 /** 1896 /**
1897 * ab8500_charger_mainchplugdet_handler() - main charger plugged 1897 * ab8500_charger_mainchplugdet_handler() - main charger plugged
1898 * @irq: interrupt number 1898 * @irq: interrupt number
1899 * @_di: pointer to the ab8500_charger structure 1899 * @_di: pointer to the ab8500_charger structure
1900 * 1900 *
1901 * Returns IRQ status(IRQ_HANDLED) 1901 * Returns IRQ status(IRQ_HANDLED)
1902 */ 1902 */
1903 static irqreturn_t ab8500_charger_mainchplugdet_handler(int irq, void *_di) 1903 static irqreturn_t ab8500_charger_mainchplugdet_handler(int irq, void *_di)
1904 { 1904 {
1905 struct ab8500_charger *di = _di; 1905 struct ab8500_charger *di = _di;
1906 1906
1907 dev_dbg(di->dev, "Main charger plugged\n"); 1907 dev_dbg(di->dev, "Main charger plugged\n");
1908 queue_work(di->charger_wq, &di->ac_work); 1908 queue_work(di->charger_wq, &di->ac_work);
1909 1909
1910 return IRQ_HANDLED; 1910 return IRQ_HANDLED;
1911 } 1911 }
1912 1912
1913 /** 1913 /**
1914 * ab8500_charger_mainextchnotok_handler() - main charger not ok 1914 * ab8500_charger_mainextchnotok_handler() - main charger not ok
1915 * @irq: interrupt number 1915 * @irq: interrupt number
1916 * @_di: pointer to the ab8500_charger structure 1916 * @_di: pointer to the ab8500_charger structure
1917 * 1917 *
1918 * Returns IRQ status(IRQ_HANDLED) 1918 * Returns IRQ status(IRQ_HANDLED)
1919 */ 1919 */
1920 static irqreturn_t ab8500_charger_mainextchnotok_handler(int irq, void *_di) 1920 static irqreturn_t ab8500_charger_mainextchnotok_handler(int irq, void *_di)
1921 { 1921 {
1922 struct ab8500_charger *di = _di; 1922 struct ab8500_charger *di = _di;
1923 1923
1924 dev_dbg(di->dev, "Main charger not ok\n"); 1924 dev_dbg(di->dev, "Main charger not ok\n");
1925 di->flags.mainextchnotok = true; 1925 di->flags.mainextchnotok = true;
1926 ab8500_power_supply_changed(di, &di->ac_chg.psy); 1926 ab8500_power_supply_changed(di, &di->ac_chg.psy);
1927 1927
1928 /* Schedule a new HW failure check */ 1928 /* Schedule a new HW failure check */
1929 queue_delayed_work(di->charger_wq, &di->check_hw_failure_work, 0); 1929 queue_delayed_work(di->charger_wq, &di->check_hw_failure_work, 0);
1930 1930
1931 return IRQ_HANDLED; 1931 return IRQ_HANDLED;
1932 } 1932 }
1933 1933
1934 /** 1934 /**
1935 * ab8500_charger_mainchthprotr_handler() - Die temp is above main charger 1935 * ab8500_charger_mainchthprotr_handler() - Die temp is above main charger
1936 * thermal protection threshold 1936 * thermal protection threshold
1937 * @irq: interrupt number 1937 * @irq: interrupt number
1938 * @_di: pointer to the ab8500_charger structure 1938 * @_di: pointer to the ab8500_charger structure
1939 * 1939 *
1940 * Returns IRQ status(IRQ_HANDLED) 1940 * Returns IRQ status(IRQ_HANDLED)
1941 */ 1941 */
1942 static irqreturn_t ab8500_charger_mainchthprotr_handler(int irq, void *_di) 1942 static irqreturn_t ab8500_charger_mainchthprotr_handler(int irq, void *_di)
1943 { 1943 {
1944 struct ab8500_charger *di = _di; 1944 struct ab8500_charger *di = _di;
1945 1945
1946 dev_dbg(di->dev, 1946 dev_dbg(di->dev,
1947 "Die temp above Main charger thermal protection threshold\n"); 1947 "Die temp above Main charger thermal protection threshold\n");
1948 queue_work(di->charger_wq, &di->check_main_thermal_prot_work); 1948 queue_work(di->charger_wq, &di->check_main_thermal_prot_work);
1949 1949
1950 return IRQ_HANDLED; 1950 return IRQ_HANDLED;
1951 } 1951 }
1952 1952
1953 /** 1953 /**
1954 * ab8500_charger_mainchthprotf_handler() - Die temp is below main charger 1954 * ab8500_charger_mainchthprotf_handler() - Die temp is below main charger
1955 * thermal protection threshold 1955 * thermal protection threshold
1956 * @irq: interrupt number 1956 * @irq: interrupt number
1957 * @_di: pointer to the ab8500_charger structure 1957 * @_di: pointer to the ab8500_charger structure
1958 * 1958 *
1959 * Returns IRQ status(IRQ_HANDLED) 1959 * Returns IRQ status(IRQ_HANDLED)
1960 */ 1960 */
1961 static irqreturn_t ab8500_charger_mainchthprotf_handler(int irq, void *_di) 1961 static irqreturn_t ab8500_charger_mainchthprotf_handler(int irq, void *_di)
1962 { 1962 {
1963 struct ab8500_charger *di = _di; 1963 struct ab8500_charger *di = _di;
1964 1964
1965 dev_dbg(di->dev, 1965 dev_dbg(di->dev,
1966 "Die temp ok for Main charger thermal protection threshold\n"); 1966 "Die temp ok for Main charger thermal protection threshold\n");
1967 queue_work(di->charger_wq, &di->check_main_thermal_prot_work); 1967 queue_work(di->charger_wq, &di->check_main_thermal_prot_work);
1968 1968
1969 return IRQ_HANDLED; 1969 return IRQ_HANDLED;
1970 } 1970 }
1971 1971
1972 /** 1972 /**
1973 * ab8500_charger_vbusdetf_handler() - VBUS falling detected 1973 * ab8500_charger_vbusdetf_handler() - VBUS falling detected
1974 * @irq: interrupt number 1974 * @irq: interrupt number
1975 * @_di: pointer to the ab8500_charger structure 1975 * @_di: pointer to the ab8500_charger structure
1976 * 1976 *
1977 * Returns IRQ status(IRQ_HANDLED) 1977 * Returns IRQ status(IRQ_HANDLED)
1978 */ 1978 */
1979 static irqreturn_t ab8500_charger_vbusdetf_handler(int irq, void *_di) 1979 static irqreturn_t ab8500_charger_vbusdetf_handler(int irq, void *_di)
1980 { 1980 {
1981 struct ab8500_charger *di = _di; 1981 struct ab8500_charger *di = _di;
1982 1982
1983 dev_dbg(di->dev, "VBUS falling detected\n"); 1983 dev_dbg(di->dev, "VBUS falling detected\n");
1984 queue_work(di->charger_wq, &di->detect_usb_type_work); 1984 queue_work(di->charger_wq, &di->detect_usb_type_work);
1985 1985
1986 return IRQ_HANDLED; 1986 return IRQ_HANDLED;
1987 } 1987 }
1988 1988
1989 /** 1989 /**
1990 * ab8500_charger_vbusdetr_handler() - VBUS rising detected 1990 * ab8500_charger_vbusdetr_handler() - VBUS rising detected
1991 * @irq: interrupt number 1991 * @irq: interrupt number
1992 * @_di: pointer to the ab8500_charger structure 1992 * @_di: pointer to the ab8500_charger structure
1993 * 1993 *
1994 * Returns IRQ status(IRQ_HANDLED) 1994 * Returns IRQ status(IRQ_HANDLED)
1995 */ 1995 */
1996 static irqreturn_t ab8500_charger_vbusdetr_handler(int irq, void *_di) 1996 static irqreturn_t ab8500_charger_vbusdetr_handler(int irq, void *_di)
1997 { 1997 {
1998 struct ab8500_charger *di = _di; 1998 struct ab8500_charger *di = _di;
1999 1999
2000 di->vbus_detected = true; 2000 di->vbus_detected = true;
2001 dev_dbg(di->dev, "VBUS rising detected\n"); 2001 dev_dbg(di->dev, "VBUS rising detected\n");
2002 queue_work(di->charger_wq, &di->detect_usb_type_work); 2002 queue_work(di->charger_wq, &di->detect_usb_type_work);
2003 2003
2004 return IRQ_HANDLED; 2004 return IRQ_HANDLED;
2005 } 2005 }
2006 2006
2007 /** 2007 /**
2008 * ab8500_charger_usblinkstatus_handler() - USB link status has changed 2008 * ab8500_charger_usblinkstatus_handler() - USB link status has changed
2009 * @irq: interrupt number 2009 * @irq: interrupt number
2010 * @_di: pointer to the ab8500_charger structure 2010 * @_di: pointer to the ab8500_charger structure
2011 * 2011 *
2012 * Returns IRQ status(IRQ_HANDLED) 2012 * Returns IRQ status(IRQ_HANDLED)
2013 */ 2013 */
2014 static irqreturn_t ab8500_charger_usblinkstatus_handler(int irq, void *_di) 2014 static irqreturn_t ab8500_charger_usblinkstatus_handler(int irq, void *_di)
2015 { 2015 {
2016 struct ab8500_charger *di = _di; 2016 struct ab8500_charger *di = _di;
2017 2017
2018 dev_dbg(di->dev, "USB link status changed\n"); 2018 dev_dbg(di->dev, "USB link status changed\n");
2019 2019
2020 queue_work(di->charger_wq, &di->usb_link_status_work); 2020 queue_work(di->charger_wq, &di->usb_link_status_work);
2021 2021
2022 return IRQ_HANDLED; 2022 return IRQ_HANDLED;
2023 } 2023 }
2024 2024
2025 /** 2025 /**
2026 * ab8500_charger_usbchthprotr_handler() - Die temp is above usb charger 2026 * ab8500_charger_usbchthprotr_handler() - Die temp is above usb charger
2027 * thermal protection threshold 2027 * thermal protection threshold
2028 * @irq: interrupt number 2028 * @irq: interrupt number
2029 * @_di: pointer to the ab8500_charger structure 2029 * @_di: pointer to the ab8500_charger structure
2030 * 2030 *
2031 * Returns IRQ status(IRQ_HANDLED) 2031 * Returns IRQ status(IRQ_HANDLED)
2032 */ 2032 */
2033 static irqreturn_t ab8500_charger_usbchthprotr_handler(int irq, void *_di) 2033 static irqreturn_t ab8500_charger_usbchthprotr_handler(int irq, void *_di)
2034 { 2034 {
2035 struct ab8500_charger *di = _di; 2035 struct ab8500_charger *di = _di;
2036 2036
2037 dev_dbg(di->dev, 2037 dev_dbg(di->dev,
2038 "Die temp above USB charger thermal protection threshold\n"); 2038 "Die temp above USB charger thermal protection threshold\n");
2039 queue_work(di->charger_wq, &di->check_usb_thermal_prot_work); 2039 queue_work(di->charger_wq, &di->check_usb_thermal_prot_work);
2040 2040
2041 return IRQ_HANDLED; 2041 return IRQ_HANDLED;
2042 } 2042 }
2043 2043
2044 /** 2044 /**
2045 * ab8500_charger_usbchthprotf_handler() - Die temp is below usb charger 2045 * ab8500_charger_usbchthprotf_handler() - Die temp is below usb charger
2046 * thermal protection threshold 2046 * thermal protection threshold
2047 * @irq: interrupt number 2047 * @irq: interrupt number
2048 * @_di: pointer to the ab8500_charger structure 2048 * @_di: pointer to the ab8500_charger structure
2049 * 2049 *
2050 * Returns IRQ status(IRQ_HANDLED) 2050 * Returns IRQ status(IRQ_HANDLED)
2051 */ 2051 */
2052 static irqreturn_t ab8500_charger_usbchthprotf_handler(int irq, void *_di) 2052 static irqreturn_t ab8500_charger_usbchthprotf_handler(int irq, void *_di)
2053 { 2053 {
2054 struct ab8500_charger *di = _di; 2054 struct ab8500_charger *di = _di;
2055 2055
2056 dev_dbg(di->dev, 2056 dev_dbg(di->dev,
2057 "Die temp ok for USB charger thermal protection threshold\n"); 2057 "Die temp ok for USB charger thermal protection threshold\n");
2058 queue_work(di->charger_wq, &di->check_usb_thermal_prot_work); 2058 queue_work(di->charger_wq, &di->check_usb_thermal_prot_work);
2059 2059
2060 return IRQ_HANDLED; 2060 return IRQ_HANDLED;
2061 } 2061 }
2062 2062
2063 /** 2063 /**
2064 * ab8500_charger_usbchargernotokr_handler() - USB charger not ok detected 2064 * ab8500_charger_usbchargernotokr_handler() - USB charger not ok detected
2065 * @irq: interrupt number 2065 * @irq: interrupt number
2066 * @_di: pointer to the ab8500_charger structure 2066 * @_di: pointer to the ab8500_charger structure
2067 * 2067 *
2068 * Returns IRQ status(IRQ_HANDLED) 2068 * Returns IRQ status(IRQ_HANDLED)
2069 */ 2069 */
2070 static irqreturn_t ab8500_charger_usbchargernotokr_handler(int irq, void *_di) 2070 static irqreturn_t ab8500_charger_usbchargernotokr_handler(int irq, void *_di)
2071 { 2071 {
2072 struct ab8500_charger *di = _di; 2072 struct ab8500_charger *di = _di;
2073 2073
2074 dev_dbg(di->dev, "Not allowed USB charger detected\n"); 2074 dev_dbg(di->dev, "Not allowed USB charger detected\n");
2075 queue_delayed_work(di->charger_wq, &di->check_usbchgnotok_work, 0); 2075 queue_delayed_work(di->charger_wq, &di->check_usbchgnotok_work, 0);
2076 2076
2077 return IRQ_HANDLED; 2077 return IRQ_HANDLED;
2078 } 2078 }
2079 2079
2080 /** 2080 /**
2081 * ab8500_charger_chwdexp_handler() - Charger watchdog expired 2081 * ab8500_charger_chwdexp_handler() - Charger watchdog expired
2082 * @irq: interrupt number 2082 * @irq: interrupt number
2083 * @_di: pointer to the ab8500_charger structure 2083 * @_di: pointer to the ab8500_charger structure
2084 * 2084 *
2085 * Returns IRQ status(IRQ_HANDLED) 2085 * Returns IRQ status(IRQ_HANDLED)
2086 */ 2086 */
2087 static irqreturn_t ab8500_charger_chwdexp_handler(int irq, void *_di) 2087 static irqreturn_t ab8500_charger_chwdexp_handler(int irq, void *_di)
2088 { 2088 {
2089 struct ab8500_charger *di = _di; 2089 struct ab8500_charger *di = _di;
2090 2090
2091 dev_dbg(di->dev, "Charger watchdog expired\n"); 2091 dev_dbg(di->dev, "Charger watchdog expired\n");
2092 2092
2093 /* 2093 /*
2094 * The charger that was online when the watchdog expired 2094 * The charger that was online when the watchdog expired
2095 * needs to be restarted for charging to start again 2095 * needs to be restarted for charging to start again
2096 */ 2096 */
2097 if (di->ac.charger_online) { 2097 if (di->ac.charger_online) {
2098 di->ac.wd_expired = true; 2098 di->ac.wd_expired = true;
2099 ab8500_power_supply_changed(di, &di->ac_chg.psy); 2099 ab8500_power_supply_changed(di, &di->ac_chg.psy);
2100 } 2100 }
2101 if (di->usb.charger_online) { 2101 if (di->usb.charger_online) {
2102 di->usb.wd_expired = true; 2102 di->usb.wd_expired = true;
2103 ab8500_power_supply_changed(di, &di->usb_chg.psy); 2103 ab8500_power_supply_changed(di, &di->usb_chg.psy);
2104 } 2104 }
2105 2105
2106 return IRQ_HANDLED; 2106 return IRQ_HANDLED;
2107 } 2107 }
2108 2108
2109 /** 2109 /**
2110 * ab8500_charger_vbusovv_handler() - VBUS overvoltage detected 2110 * ab8500_charger_vbusovv_handler() - VBUS overvoltage detected
2111 * @irq: interrupt number 2111 * @irq: interrupt number
2112 * @_di: pointer to the ab8500_charger structure 2112 * @_di: pointer to the ab8500_charger structure
2113 * 2113 *
2114 * Returns IRQ status(IRQ_HANDLED) 2114 * Returns IRQ status(IRQ_HANDLED)
2115 */ 2115 */
2116 static irqreturn_t ab8500_charger_vbusovv_handler(int irq, void *_di) 2116 static irqreturn_t ab8500_charger_vbusovv_handler(int irq, void *_di)
2117 { 2117 {
2118 struct ab8500_charger *di = _di; 2118 struct ab8500_charger *di = _di;
2119 2119
2120 dev_dbg(di->dev, "VBUS overvoltage detected\n"); 2120 dev_dbg(di->dev, "VBUS overvoltage detected\n");
2121 di->flags.vbus_ovv = true; 2121 di->flags.vbus_ovv = true;
2122 ab8500_power_supply_changed(di, &di->usb_chg.psy); 2122 ab8500_power_supply_changed(di, &di->usb_chg.psy);
2123 2123
2124 /* Schedule a new HW failure check */ 2124 /* Schedule a new HW failure check */
2125 queue_delayed_work(di->charger_wq, &di->check_hw_failure_work, 0); 2125 queue_delayed_work(di->charger_wq, &di->check_hw_failure_work, 0);
2126 2126
2127 return IRQ_HANDLED; 2127 return IRQ_HANDLED;
2128 } 2128 }
2129 2129
2130 /** 2130 /**
2131 * ab8500_charger_ac_get_property() - get the ac/mains properties 2131 * ab8500_charger_ac_get_property() - get the ac/mains properties
2132 * @psy: pointer to the power_supply structure 2132 * @psy: pointer to the power_supply structure
2133 * @psp: pointer to the power_supply_property structure 2133 * @psp: pointer to the power_supply_property structure
2134 * @val: pointer to the power_supply_propval union 2134 * @val: pointer to the power_supply_propval union
2135 * 2135 *
2136 * This function gets called when an application tries to get the ac/mains 2136 * This function gets called when an application tries to get the ac/mains
2137 * properties by reading the sysfs files. 2137 * properties by reading the sysfs files.
2138 * AC/Mains properties are online, present and voltage. 2138 * AC/Mains properties are online, present and voltage.
2139 * online: ac/mains charging is in progress or not 2139 * online: ac/mains charging is in progress or not
2140 * present: presence of the ac/mains 2140 * present: presence of the ac/mains
2141 * voltage: AC/Mains voltage 2141 * voltage: AC/Mains voltage
2142 * Returns error code in case of failure else 0(on success) 2142 * Returns error code in case of failure else 0(on success)
2143 */ 2143 */
2144 static int ab8500_charger_ac_get_property(struct power_supply *psy, 2144 static int ab8500_charger_ac_get_property(struct power_supply *psy,
2145 enum power_supply_property psp, 2145 enum power_supply_property psp,
2146 union power_supply_propval *val) 2146 union power_supply_propval *val)
2147 { 2147 {
2148 struct ab8500_charger *di; 2148 struct ab8500_charger *di;
2149 2149
2150 di = to_ab8500_charger_ac_device_info(psy_to_ux500_charger(psy)); 2150 di = to_ab8500_charger_ac_device_info(psy_to_ux500_charger(psy));
2151 2151
2152 switch (psp) { 2152 switch (psp) {
2153 case POWER_SUPPLY_PROP_HEALTH: 2153 case POWER_SUPPLY_PROP_HEALTH:
2154 if (di->flags.mainextchnotok) 2154 if (di->flags.mainextchnotok)
2155 val->intval = POWER_SUPPLY_HEALTH_UNSPEC_FAILURE; 2155 val->intval = POWER_SUPPLY_HEALTH_UNSPEC_FAILURE;
2156 else if (di->ac.wd_expired || di->usb.wd_expired) 2156 else if (di->ac.wd_expired || di->usb.wd_expired)
2157 val->intval = POWER_SUPPLY_HEALTH_DEAD; 2157 val->intval = POWER_SUPPLY_HEALTH_DEAD;
2158 else if (di->flags.main_thermal_prot) 2158 else if (di->flags.main_thermal_prot)
2159 val->intval = POWER_SUPPLY_HEALTH_OVERHEAT; 2159 val->intval = POWER_SUPPLY_HEALTH_OVERHEAT;
2160 else 2160 else
2161 val->intval = POWER_SUPPLY_HEALTH_GOOD; 2161 val->intval = POWER_SUPPLY_HEALTH_GOOD;
2162 break; 2162 break;
2163 case POWER_SUPPLY_PROP_ONLINE: 2163 case POWER_SUPPLY_PROP_ONLINE:
2164 val->intval = di->ac.charger_online; 2164 val->intval = di->ac.charger_online;
2165 break; 2165 break;
2166 case POWER_SUPPLY_PROP_PRESENT: 2166 case POWER_SUPPLY_PROP_PRESENT:
2167 val->intval = di->ac.charger_connected; 2167 val->intval = di->ac.charger_connected;
2168 break; 2168 break;
2169 case POWER_SUPPLY_PROP_VOLTAGE_NOW: 2169 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
2170 di->ac.charger_voltage = ab8500_charger_get_ac_voltage(di); 2170 di->ac.charger_voltage = ab8500_charger_get_ac_voltage(di);
2171 val->intval = di->ac.charger_voltage * 1000; 2171 val->intval = di->ac.charger_voltage * 1000;
2172 break; 2172 break;
2173 case POWER_SUPPLY_PROP_VOLTAGE_AVG: 2173 case POWER_SUPPLY_PROP_VOLTAGE_AVG:
2174 /* 2174 /*
2175 * This property is used to indicate when CV mode is entered 2175 * This property is used to indicate when CV mode is entered
2176 * for the AC charger 2176 * for the AC charger
2177 */ 2177 */
2178 di->ac.cv_active = ab8500_charger_ac_cv(di); 2178 di->ac.cv_active = ab8500_charger_ac_cv(di);
2179 val->intval = di->ac.cv_active; 2179 val->intval = di->ac.cv_active;
2180 break; 2180 break;
2181 case POWER_SUPPLY_PROP_CURRENT_NOW: 2181 case POWER_SUPPLY_PROP_CURRENT_NOW:
2182 val->intval = ab8500_charger_get_ac_current(di) * 1000; 2182 val->intval = ab8500_charger_get_ac_current(di) * 1000;
2183 break; 2183 break;
2184 default: 2184 default:
2185 return -EINVAL; 2185 return -EINVAL;
2186 } 2186 }
2187 return 0; 2187 return 0;
2188 } 2188 }
2189 2189
2190 /** 2190 /**
2191 * ab8500_charger_usb_get_property() - get the usb properties 2191 * ab8500_charger_usb_get_property() - get the usb properties
2192 * @psy: pointer to the power_supply structure 2192 * @psy: pointer to the power_supply structure
2193 * @psp: pointer to the power_supply_property structure 2193 * @psp: pointer to the power_supply_property structure
2194 * @val: pointer to the power_supply_propval union 2194 * @val: pointer to the power_supply_propval union
2195 * 2195 *
2196 * This function gets called when an application tries to get the usb 2196 * This function gets called when an application tries to get the usb
2197 * properties by reading the sysfs files. 2197 * properties by reading the sysfs files.
2198 * USB properties are online, present and voltage. 2198 * USB properties are online, present and voltage.
2199 * online: usb charging is in progress or not 2199 * online: usb charging is in progress or not
2200 * present: presence of the usb 2200 * present: presence of the usb
2201 * voltage: vbus voltage 2201 * voltage: vbus voltage
2202 * Returns error code in case of failure else 0(on success) 2202 * Returns error code in case of failure else 0(on success)
2203 */ 2203 */
2204 static int ab8500_charger_usb_get_property(struct power_supply *psy, 2204 static int ab8500_charger_usb_get_property(struct power_supply *psy,
2205 enum power_supply_property psp, 2205 enum power_supply_property psp,
2206 union power_supply_propval *val) 2206 union power_supply_propval *val)
2207 { 2207 {
2208 struct ab8500_charger *di; 2208 struct ab8500_charger *di;
2209 2209
2210 di = to_ab8500_charger_usb_device_info(psy_to_ux500_charger(psy)); 2210 di = to_ab8500_charger_usb_device_info(psy_to_ux500_charger(psy));
2211 2211
2212 switch (psp) { 2212 switch (psp) {
2213 case POWER_SUPPLY_PROP_HEALTH: 2213 case POWER_SUPPLY_PROP_HEALTH:
2214 if (di->flags.usbchargernotok) 2214 if (di->flags.usbchargernotok)
2215 val->intval = POWER_SUPPLY_HEALTH_UNSPEC_FAILURE; 2215 val->intval = POWER_SUPPLY_HEALTH_UNSPEC_FAILURE;
2216 else if (di->ac.wd_expired || di->usb.wd_expired) 2216 else if (di->ac.wd_expired || di->usb.wd_expired)
2217 val->intval = POWER_SUPPLY_HEALTH_DEAD; 2217 val->intval = POWER_SUPPLY_HEALTH_DEAD;
2218 else if (di->flags.usb_thermal_prot) 2218 else if (di->flags.usb_thermal_prot)
2219 val->intval = POWER_SUPPLY_HEALTH_OVERHEAT; 2219 val->intval = POWER_SUPPLY_HEALTH_OVERHEAT;
2220 else if (di->flags.vbus_ovv) 2220 else if (di->flags.vbus_ovv)
2221 val->intval = POWER_SUPPLY_HEALTH_OVERVOLTAGE; 2221 val->intval = POWER_SUPPLY_HEALTH_OVERVOLTAGE;
2222 else 2222 else
2223 val->intval = POWER_SUPPLY_HEALTH_GOOD; 2223 val->intval = POWER_SUPPLY_HEALTH_GOOD;
2224 break; 2224 break;
2225 case POWER_SUPPLY_PROP_ONLINE: 2225 case POWER_SUPPLY_PROP_ONLINE:
2226 val->intval = di->usb.charger_online; 2226 val->intval = di->usb.charger_online;
2227 break; 2227 break;
2228 case POWER_SUPPLY_PROP_PRESENT: 2228 case POWER_SUPPLY_PROP_PRESENT:
2229 val->intval = di->usb.charger_connected; 2229 val->intval = di->usb.charger_connected;
2230 break; 2230 break;
2231 case POWER_SUPPLY_PROP_VOLTAGE_NOW: 2231 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
2232 di->usb.charger_voltage = ab8500_charger_get_vbus_voltage(di); 2232 di->usb.charger_voltage = ab8500_charger_get_vbus_voltage(di);
2233 val->intval = di->usb.charger_voltage * 1000; 2233 val->intval = di->usb.charger_voltage * 1000;
2234 break; 2234 break;
2235 case POWER_SUPPLY_PROP_VOLTAGE_AVG: 2235 case POWER_SUPPLY_PROP_VOLTAGE_AVG:
2236 /* 2236 /*
2237 * This property is used to indicate when CV mode is entered 2237 * This property is used to indicate when CV mode is entered
2238 * for the USB charger 2238 * for the USB charger
2239 */ 2239 */
2240 di->usb.cv_active = ab8500_charger_usb_cv(di); 2240 di->usb.cv_active = ab8500_charger_usb_cv(di);
2241 val->intval = di->usb.cv_active; 2241 val->intval = di->usb.cv_active;
2242 break; 2242 break;
2243 case POWER_SUPPLY_PROP_CURRENT_NOW: 2243 case POWER_SUPPLY_PROP_CURRENT_NOW:
2244 val->intval = ab8500_charger_get_usb_current(di) * 1000; 2244 val->intval = ab8500_charger_get_usb_current(di) * 1000;
2245 break; 2245 break;
2246 case POWER_SUPPLY_PROP_CURRENT_AVG: 2246 case POWER_SUPPLY_PROP_CURRENT_AVG:
2247 /* 2247 /*
2248 * This property is used to indicate when VBUS has collapsed 2248 * This property is used to indicate when VBUS has collapsed
2249 * due to too high output current from the USB charger 2249 * due to too high output current from the USB charger
2250 */ 2250 */
2251 if (di->flags.vbus_collapse) 2251 if (di->flags.vbus_collapse)
2252 val->intval = 1; 2252 val->intval = 1;
2253 else 2253 else
2254 val->intval = 0; 2254 val->intval = 0;
2255 break; 2255 break;
2256 default: 2256 default:
2257 return -EINVAL; 2257 return -EINVAL;
2258 } 2258 }
2259 return 0; 2259 return 0;
2260 } 2260 }
2261 2261
2262 /** 2262 /**
2263 * ab8500_charger_init_hw_registers() - Set up charger related registers 2263 * ab8500_charger_init_hw_registers() - Set up charger related registers
2264 * @di: pointer to the ab8500_charger structure 2264 * @di: pointer to the ab8500_charger structure
2265 * 2265 *
2266 * Set up charger OVV, watchdog and maximum voltage registers as well as 2266 * Set up charger OVV, watchdog and maximum voltage registers as well as
2267 * charging of the backup battery 2267 * charging of the backup battery
2268 */ 2268 */
2269 static int ab8500_charger_init_hw_registers(struct ab8500_charger *di) 2269 static int ab8500_charger_init_hw_registers(struct ab8500_charger *di)
2270 { 2270 {
2271 int ret = 0; 2271 int ret = 0;
2272 2272
2273 /* Setup maximum charger current and voltage for ABB cut2.0 */ 2273 /* Setup maximum charger current and voltage for ABB cut2.0 */
2274 if (!is_ab8500_1p1_or_earlier(di->parent)) { 2274 if (!is_ab8500_1p1_or_earlier(di->parent)) {
2275 ret = abx500_set_register_interruptible(di->dev, 2275 ret = abx500_set_register_interruptible(di->dev,
2276 AB8500_CHARGER, 2276 AB8500_CHARGER,
2277 AB8500_CH_VOLT_LVL_MAX_REG, CH_VOL_LVL_4P6); 2277 AB8500_CH_VOLT_LVL_MAX_REG, CH_VOL_LVL_4P6);
2278 if (ret) { 2278 if (ret) {
2279 dev_err(di->dev, 2279 dev_err(di->dev,
2280 "failed to set CH_VOLT_LVL_MAX_REG\n"); 2280 "failed to set CH_VOLT_LVL_MAX_REG\n");
2281 goto out; 2281 goto out;
2282 } 2282 }
2283 2283
2284 ret = abx500_set_register_interruptible(di->dev, 2284 ret = abx500_set_register_interruptible(di->dev,
2285 AB8500_CHARGER, 2285 AB8500_CHARGER,
2286 AB8500_CH_OPT_CRNTLVL_MAX_REG, CH_OP_CUR_LVL_1P6); 2286 AB8500_CH_OPT_CRNTLVL_MAX_REG, CH_OP_CUR_LVL_1P6);
2287 if (ret) { 2287 if (ret) {
2288 dev_err(di->dev, 2288 dev_err(di->dev,
2289 "failed to set CH_OPT_CRNTLVL_MAX_REG\n"); 2289 "failed to set CH_OPT_CRNTLVL_MAX_REG\n");
2290 goto out; 2290 goto out;
2291 } 2291 }
2292 } 2292 }
2293 2293
2294 /* VBUS OVV set to 6.3V and enable automatic current limitiation */ 2294 /* VBUS OVV set to 6.3V and enable automatic current limitiation */
2295 ret = abx500_set_register_interruptible(di->dev, 2295 ret = abx500_set_register_interruptible(di->dev,
2296 AB8500_CHARGER, 2296 AB8500_CHARGER,
2297 AB8500_USBCH_CTRL2_REG, 2297 AB8500_USBCH_CTRL2_REG,
2298 VBUS_OVV_SELECT_6P3V | VBUS_AUTO_IN_CURR_LIM_ENA); 2298 VBUS_OVV_SELECT_6P3V | VBUS_AUTO_IN_CURR_LIM_ENA);
2299 if (ret) { 2299 if (ret) {
2300 dev_err(di->dev, "failed to set VBUS OVV\n"); 2300 dev_err(di->dev, "failed to set VBUS OVV\n");
2301 goto out; 2301 goto out;
2302 } 2302 }
2303 2303
2304 /* Enable main watchdog in OTP */ 2304 /* Enable main watchdog in OTP */
2305 ret = abx500_set_register_interruptible(di->dev, 2305 ret = abx500_set_register_interruptible(di->dev,
2306 AB8500_OTP_EMUL, AB8500_OTP_CONF_15, OTP_ENABLE_WD); 2306 AB8500_OTP_EMUL, AB8500_OTP_CONF_15, OTP_ENABLE_WD);
2307 if (ret) { 2307 if (ret) {
2308 dev_err(di->dev, "failed to enable main WD in OTP\n"); 2308 dev_err(di->dev, "failed to enable main WD in OTP\n");
2309 goto out; 2309 goto out;
2310 } 2310 }
2311 2311
2312 /* Enable main watchdog */ 2312 /* Enable main watchdog */
2313 ret = abx500_set_register_interruptible(di->dev, 2313 ret = abx500_set_register_interruptible(di->dev,
2314 AB8500_SYS_CTRL2_BLOCK, 2314 AB8500_SYS_CTRL2_BLOCK,
2315 AB8500_MAIN_WDOG_CTRL_REG, MAIN_WDOG_ENA); 2315 AB8500_MAIN_WDOG_CTRL_REG, MAIN_WDOG_ENA);
2316 if (ret) { 2316 if (ret) {
2317 dev_err(di->dev, "faile to enable main watchdog\n"); 2317 dev_err(di->dev, "faile to enable main watchdog\n");
2318 goto out; 2318 goto out;
2319 } 2319 }
2320 2320
2321 /* 2321 /*
2322 * Due to internal synchronisation, Enable and Kick watchdog bits 2322 * Due to internal synchronisation, Enable and Kick watchdog bits
2323 * cannot be enabled in a single write. 2323 * cannot be enabled in a single write.
2324 * A minimum delay of 2*32 kHz period (62.5ยตs) must be inserted 2324 * A minimum delay of 2*32 kHz period (62.5ยตs) must be inserted
2325 * between writing Enable then Kick bits. 2325 * between writing Enable then Kick bits.
2326 */ 2326 */
2327 udelay(63); 2327 udelay(63);
2328 2328
2329 /* Kick main watchdog */ 2329 /* Kick main watchdog */
2330 ret = abx500_set_register_interruptible(di->dev, 2330 ret = abx500_set_register_interruptible(di->dev,
2331 AB8500_SYS_CTRL2_BLOCK, 2331 AB8500_SYS_CTRL2_BLOCK,
2332 AB8500_MAIN_WDOG_CTRL_REG, 2332 AB8500_MAIN_WDOG_CTRL_REG,
2333 (MAIN_WDOG_ENA | MAIN_WDOG_KICK)); 2333 (MAIN_WDOG_ENA | MAIN_WDOG_KICK));
2334 if (ret) { 2334 if (ret) {
2335 dev_err(di->dev, "failed to kick main watchdog\n"); 2335 dev_err(di->dev, "failed to kick main watchdog\n");
2336 goto out; 2336 goto out;
2337 } 2337 }
2338 2338
2339 /* Disable main watchdog */ 2339 /* Disable main watchdog */
2340 ret = abx500_set_register_interruptible(di->dev, 2340 ret = abx500_set_register_interruptible(di->dev,
2341 AB8500_SYS_CTRL2_BLOCK, 2341 AB8500_SYS_CTRL2_BLOCK,
2342 AB8500_MAIN_WDOG_CTRL_REG, MAIN_WDOG_DIS); 2342 AB8500_MAIN_WDOG_CTRL_REG, MAIN_WDOG_DIS);
2343 if (ret) { 2343 if (ret) {
2344 dev_err(di->dev, "failed to disable main watchdog\n"); 2344 dev_err(di->dev, "failed to disable main watchdog\n");
2345 goto out; 2345 goto out;
2346 } 2346 }
2347 2347
2348 /* Set watchdog timeout */ 2348 /* Set watchdog timeout */
2349 ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER, 2349 ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
2350 AB8500_CH_WD_TIMER_REG, WD_TIMER); 2350 AB8500_CH_WD_TIMER_REG, WD_TIMER);
2351 if (ret) { 2351 if (ret) {
2352 dev_err(di->dev, "failed to set charger watchdog timeout\n"); 2352 dev_err(di->dev, "failed to set charger watchdog timeout\n");
2353 goto out; 2353 goto out;
2354 } 2354 }
2355 2355
2356 /* Backup battery voltage and current */ 2356 /* Backup battery voltage and current */
2357 ret = abx500_set_register_interruptible(di->dev, 2357 ret = abx500_set_register_interruptible(di->dev,
2358 AB8500_RTC, 2358 AB8500_RTC,
2359 AB8500_RTC_BACKUP_CHG_REG, 2359 AB8500_RTC_BACKUP_CHG_REG,
2360 di->bat->bkup_bat_v | 2360 di->bat->bkup_bat_v |
2361 di->bat->bkup_bat_i); 2361 di->bat->bkup_bat_i);
2362 if (ret) { 2362 if (ret) {
2363 dev_err(di->dev, "failed to setup backup battery charging\n"); 2363 dev_err(di->dev, "failed to setup backup battery charging\n");
2364 goto out; 2364 goto out;
2365 } 2365 }
2366 2366
2367 /* Enable backup battery charging */ 2367 /* Enable backup battery charging */
2368 abx500_mask_and_set_register_interruptible(di->dev, 2368 abx500_mask_and_set_register_interruptible(di->dev,
2369 AB8500_RTC, AB8500_RTC_CTRL_REG, 2369 AB8500_RTC, AB8500_RTC_CTRL_REG,
2370 RTC_BUP_CH_ENA, RTC_BUP_CH_ENA); 2370 RTC_BUP_CH_ENA, RTC_BUP_CH_ENA);
2371 if (ret < 0) 2371 if (ret < 0)
2372 dev_err(di->dev, "%s mask and set failed\n", __func__); 2372 dev_err(di->dev, "%s mask and set failed\n", __func__);
2373 2373
2374 out: 2374 out:
2375 return ret; 2375 return ret;
2376 } 2376 }
2377 2377
2378 /* 2378 /*
2379 * ab8500 charger driver interrupts and their respective isr 2379 * ab8500 charger driver interrupts and their respective isr
2380 */ 2380 */
2381 static struct ab8500_charger_interrupts ab8500_charger_irq[] = { 2381 static struct ab8500_charger_interrupts ab8500_charger_irq[] = {
2382 {"MAIN_CH_UNPLUG_DET", ab8500_charger_mainchunplugdet_handler}, 2382 {"MAIN_CH_UNPLUG_DET", ab8500_charger_mainchunplugdet_handler},
2383 {"MAIN_CHARGE_PLUG_DET", ab8500_charger_mainchplugdet_handler}, 2383 {"MAIN_CHARGE_PLUG_DET", ab8500_charger_mainchplugdet_handler},
2384 {"MAIN_EXT_CH_NOT_OK", ab8500_charger_mainextchnotok_handler}, 2384 {"MAIN_EXT_CH_NOT_OK", ab8500_charger_mainextchnotok_handler},
2385 {"MAIN_CH_TH_PROT_R", ab8500_charger_mainchthprotr_handler}, 2385 {"MAIN_CH_TH_PROT_R", ab8500_charger_mainchthprotr_handler},
2386 {"MAIN_CH_TH_PROT_F", ab8500_charger_mainchthprotf_handler}, 2386 {"MAIN_CH_TH_PROT_F", ab8500_charger_mainchthprotf_handler},
2387 {"VBUS_DET_F", ab8500_charger_vbusdetf_handler}, 2387 {"VBUS_DET_F", ab8500_charger_vbusdetf_handler},
2388 {"VBUS_DET_R", ab8500_charger_vbusdetr_handler}, 2388 {"VBUS_DET_R", ab8500_charger_vbusdetr_handler},
2389 {"USB_LINK_STATUS", ab8500_charger_usblinkstatus_handler}, 2389 {"USB_LINK_STATUS", ab8500_charger_usblinkstatus_handler},
2390 {"USB_CH_TH_PROT_R", ab8500_charger_usbchthprotr_handler}, 2390 {"USB_CH_TH_PROT_R", ab8500_charger_usbchthprotr_handler},
2391 {"USB_CH_TH_PROT_F", ab8500_charger_usbchthprotf_handler}, 2391 {"USB_CH_TH_PROT_F", ab8500_charger_usbchthprotf_handler},
2392 {"USB_CHARGER_NOT_OKR", ab8500_charger_usbchargernotokr_handler}, 2392 {"USB_CHARGER_NOT_OKR", ab8500_charger_usbchargernotokr_handler},
2393 {"VBUS_OVV", ab8500_charger_vbusovv_handler}, 2393 {"VBUS_OVV", ab8500_charger_vbusovv_handler},
2394 {"CH_WD_EXP", ab8500_charger_chwdexp_handler}, 2394 {"CH_WD_EXP", ab8500_charger_chwdexp_handler},
2395 }; 2395 };
2396 2396
2397 static int ab8500_charger_usb_notifier_call(struct notifier_block *nb, 2397 static int ab8500_charger_usb_notifier_call(struct notifier_block *nb,
2398 unsigned long event, void *power) 2398 unsigned long event, void *power)
2399 { 2399 {
2400 struct ab8500_charger *di = 2400 struct ab8500_charger *di =
2401 container_of(nb, struct ab8500_charger, nb); 2401 container_of(nb, struct ab8500_charger, nb);
2402 enum ab8500_usb_state bm_usb_state; 2402 enum ab8500_usb_state bm_usb_state;
2403 unsigned mA = *((unsigned *)power); 2403 unsigned mA = *((unsigned *)power);
2404 2404
2405 if (event != USB_EVENT_VBUS) { 2405 if (event != USB_EVENT_VBUS) {
2406 dev_dbg(di->dev, "not a standard host, returning\n"); 2406 dev_dbg(di->dev, "not a standard host, returning\n");
2407 return NOTIFY_DONE; 2407 return NOTIFY_DONE;
2408 } 2408 }
2409 2409
2410 /* TODO: State is fabricate here. See if charger really needs USB 2410 /* TODO: State is fabricate here. See if charger really needs USB
2411 * state or if mA is enough 2411 * state or if mA is enough
2412 */ 2412 */
2413 if ((di->usb_state.usb_current == 2) && (mA > 2)) 2413 if ((di->usb_state.usb_current == 2) && (mA > 2))
2414 bm_usb_state = AB8500_BM_USB_STATE_RESUME; 2414 bm_usb_state = AB8500_BM_USB_STATE_RESUME;
2415 else if (mA == 0) 2415 else if (mA == 0)
2416 bm_usb_state = AB8500_BM_USB_STATE_RESET_HS; 2416 bm_usb_state = AB8500_BM_USB_STATE_RESET_HS;
2417 else if (mA == 2) 2417 else if (mA == 2)
2418 bm_usb_state = AB8500_BM_USB_STATE_SUSPEND; 2418 bm_usb_state = AB8500_BM_USB_STATE_SUSPEND;
2419 else if (mA >= 8) /* 8, 100, 500 */ 2419 else if (mA >= 8) /* 8, 100, 500 */
2420 bm_usb_state = AB8500_BM_USB_STATE_CONFIGURED; 2420 bm_usb_state = AB8500_BM_USB_STATE_CONFIGURED;
2421 else /* Should never occur */ 2421 else /* Should never occur */
2422 bm_usb_state = AB8500_BM_USB_STATE_RESET_FS; 2422 bm_usb_state = AB8500_BM_USB_STATE_RESET_FS;
2423 2423
2424 dev_dbg(di->dev, "%s usb_state: 0x%02x mA: %d\n", 2424 dev_dbg(di->dev, "%s usb_state: 0x%02x mA: %d\n",
2425 __func__, bm_usb_state, mA); 2425 __func__, bm_usb_state, mA);
2426 2426
2427 spin_lock(&di->usb_state.usb_lock); 2427 spin_lock(&di->usb_state.usb_lock);
2428 di->usb_state.usb_changed = true; 2428 di->usb_state.usb_changed = true;
2429 spin_unlock(&di->usb_state.usb_lock); 2429 spin_unlock(&di->usb_state.usb_lock);
2430 2430
2431 di->usb_state.state = bm_usb_state; 2431 di->usb_state.state = bm_usb_state;
2432 di->usb_state.usb_current = mA; 2432 di->usb_state.usb_current = mA;
2433 2433
2434 queue_work(di->charger_wq, &di->usb_state_changed_work); 2434 queue_work(di->charger_wq, &di->usb_state_changed_work);
2435 2435
2436 return NOTIFY_OK; 2436 return NOTIFY_OK;
2437 } 2437 }
2438 2438
2439 #if defined(CONFIG_PM) 2439 #if defined(CONFIG_PM)
2440 static int ab8500_charger_resume(struct platform_device *pdev) 2440 static int ab8500_charger_resume(struct platform_device *pdev)
2441 { 2441 {
2442 int ret; 2442 int ret;
2443 struct ab8500_charger *di = platform_get_drvdata(pdev); 2443 struct ab8500_charger *di = platform_get_drvdata(pdev);
2444 2444
2445 /* 2445 /*
2446 * For ABB revision 1.0 and 1.1 there is a bug in the watchdog 2446 * For ABB revision 1.0 and 1.1 there is a bug in the watchdog
2447 * logic. That means we have to continously kick the charger 2447 * logic. That means we have to continously kick the charger
2448 * watchdog even when no charger is connected. This is only 2448 * watchdog even when no charger is connected. This is only
2449 * valid once the AC charger has been enabled. This is 2449 * valid once the AC charger has been enabled. This is
2450 * a bug that is not handled by the algorithm and the 2450 * a bug that is not handled by the algorithm and the
2451 * watchdog have to be kicked by the charger driver 2451 * watchdog have to be kicked by the charger driver
2452 * when the AC charger is disabled 2452 * when the AC charger is disabled
2453 */ 2453 */
2454 if (di->ac_conn && is_ab8500_1p1_or_earlier(di->parent)) { 2454 if (di->ac_conn && is_ab8500_1p1_or_earlier(di->parent)) {
2455 ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER, 2455 ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
2456 AB8500_CHARG_WD_CTRL, CHARG_WD_KICK); 2456 AB8500_CHARG_WD_CTRL, CHARG_WD_KICK);
2457 if (ret) 2457 if (ret)
2458 dev_err(di->dev, "Failed to kick WD!\n"); 2458 dev_err(di->dev, "Failed to kick WD!\n");
2459 2459
2460 /* If not already pending start a new timer */ 2460 /* If not already pending start a new timer */
2461 if (!delayed_work_pending( 2461 if (!delayed_work_pending(
2462 &di->kick_wd_work)) { 2462 &di->kick_wd_work)) {
2463 queue_delayed_work(di->charger_wq, &di->kick_wd_work, 2463 queue_delayed_work(di->charger_wq, &di->kick_wd_work,
2464 round_jiffies(WD_KICK_INTERVAL)); 2464 round_jiffies(WD_KICK_INTERVAL));
2465 } 2465 }
2466 } 2466 }
2467 2467
2468 /* If we still have a HW failure, schedule a new check */ 2468 /* If we still have a HW failure, schedule a new check */
2469 if (di->flags.mainextchnotok || di->flags.vbus_ovv) { 2469 if (di->flags.mainextchnotok || di->flags.vbus_ovv) {
2470 queue_delayed_work(di->charger_wq, 2470 queue_delayed_work(di->charger_wq,
2471 &di->check_hw_failure_work, 0); 2471 &di->check_hw_failure_work, 0);
2472 } 2472 }
2473 2473
2474 return 0; 2474 return 0;
2475 } 2475 }
2476 2476
2477 static int ab8500_charger_suspend(struct platform_device *pdev, 2477 static int ab8500_charger_suspend(struct platform_device *pdev,
2478 pm_message_t state) 2478 pm_message_t state)
2479 { 2479 {
2480 struct ab8500_charger *di = platform_get_drvdata(pdev); 2480 struct ab8500_charger *di = platform_get_drvdata(pdev);
2481 2481
2482 /* Cancel any pending HW failure check */ 2482 /* Cancel any pending HW failure check */
2483 if (delayed_work_pending(&di->check_hw_failure_work)) 2483 if (delayed_work_pending(&di->check_hw_failure_work))
2484 cancel_delayed_work(&di->check_hw_failure_work); 2484 cancel_delayed_work(&di->check_hw_failure_work);
2485 2485
2486 return 0; 2486 return 0;
2487 } 2487 }
2488 #else 2488 #else
2489 #define ab8500_charger_suspend NULL 2489 #define ab8500_charger_suspend NULL
2490 #define ab8500_charger_resume NULL 2490 #define ab8500_charger_resume NULL
2491 #endif 2491 #endif
2492 2492
2493 static int __devexit ab8500_charger_remove(struct platform_device *pdev) 2493 static int __devexit ab8500_charger_remove(struct platform_device *pdev)
2494 { 2494 {
2495 struct ab8500_charger *di = platform_get_drvdata(pdev); 2495 struct ab8500_charger *di = platform_get_drvdata(pdev);
2496 int i, irq, ret; 2496 int i, irq, ret;
2497 2497
2498 /* Disable AC charging */ 2498 /* Disable AC charging */
2499 ab8500_charger_ac_en(&di->ac_chg, false, 0, 0); 2499 ab8500_charger_ac_en(&di->ac_chg, false, 0, 0);
2500 2500
2501 /* Disable USB charging */ 2501 /* Disable USB charging */
2502 ab8500_charger_usb_en(&di->usb_chg, false, 0, 0); 2502 ab8500_charger_usb_en(&di->usb_chg, false, 0, 0);
2503 2503
2504 /* Disable interrupts */ 2504 /* Disable interrupts */
2505 for (i = 0; i < ARRAY_SIZE(ab8500_charger_irq); i++) { 2505 for (i = 0; i < ARRAY_SIZE(ab8500_charger_irq); i++) {
2506 irq = platform_get_irq_byname(pdev, ab8500_charger_irq[i].name); 2506 irq = platform_get_irq_byname(pdev, ab8500_charger_irq[i].name);
2507 free_irq(irq, di); 2507 free_irq(irq, di);
2508 } 2508 }
2509 2509
2510 /* disable the regulator */ 2510 /* disable the regulator */
2511 regulator_put(di->regu); 2511 regulator_put(di->regu);
2512 2512
2513 /* Backup battery voltage and current disable */ 2513 /* Backup battery voltage and current disable */
2514 ret = abx500_mask_and_set_register_interruptible(di->dev, 2514 ret = abx500_mask_and_set_register_interruptible(di->dev,
2515 AB8500_RTC, AB8500_RTC_CTRL_REG, RTC_BUP_CH_ENA, 0); 2515 AB8500_RTC, AB8500_RTC_CTRL_REG, RTC_BUP_CH_ENA, 0);
2516 if (ret < 0) 2516 if (ret < 0)
2517 dev_err(di->dev, "%s mask and set failed\n", __func__); 2517 dev_err(di->dev, "%s mask and set failed\n", __func__);
2518 2518
2519 usb_unregister_notifier(di->usb_phy, &di->nb); 2519 usb_unregister_notifier(di->usb_phy, &di->nb);
2520 usb_put_transceiver(di->usb_phy); 2520 usb_put_transceiver(di->usb_phy);
2521 2521
2522 /* Delete the work queue */ 2522 /* Delete the work queue */
2523 destroy_workqueue(di->charger_wq); 2523 destroy_workqueue(di->charger_wq);
2524 2524
2525 flush_scheduled_work(); 2525 flush_scheduled_work();
2526 power_supply_unregister(&di->usb_chg.psy); 2526 power_supply_unregister(&di->usb_chg.psy);
2527 power_supply_unregister(&di->ac_chg.psy); 2527 power_supply_unregister(&di->ac_chg.psy);
2528 platform_set_drvdata(pdev, NULL); 2528 platform_set_drvdata(pdev, NULL);
2529 kfree(di); 2529 kfree(di);
2530 2530
2531 return 0; 2531 return 0;
2532 } 2532 }
2533 2533
2534 static int __devinit ab8500_charger_probe(struct platform_device *pdev) 2534 static int __devinit ab8500_charger_probe(struct platform_device *pdev)
2535 { 2535 {
2536 int irq, i, charger_status, ret = 0; 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 if (!plat_data) {
2540 kzalloc(sizeof(struct ab8500_charger), GFP_KERNEL); 2541 dev_err(&pdev->dev, "No platform data\n");
2542 return -EINVAL;
2543 }
2544
2545 di = kzalloc(sizeof(*di), GFP_KERNEL);
2541 if (!di) 2546 if (!di)
2542 return -ENOMEM; 2547 return -ENOMEM;
2543 2548
2544 /* get parent data */ 2549 /* get parent data */
2545 di->dev = &pdev->dev; 2550 di->dev = &pdev->dev;
2546 di->parent = dev_get_drvdata(pdev->dev.parent); 2551 di->parent = dev_get_drvdata(pdev->dev.parent);
2547 di->gpadc = ab8500_gpadc_get("ab8500-gpadc.0"); 2552 di->gpadc = ab8500_gpadc_get("ab8500-gpadc.0");
2548 2553
2549 /* initialize lock */ 2554 /* initialize lock */
2550 spin_lock_init(&di->usb_state.usb_lock); 2555 spin_lock_init(&di->usb_state.usb_lock);
2551 2556
2552 /* get charger specific platform data */ 2557 /* get charger specific platform data */
2553 plat_data = pdev->dev.platform_data; 2558 di->pdata = plat_data->charger;
2554 if (!plat_data || !plat_data->charger) { 2559 if (!di->pdata) {
2555 dev_err(di->dev, "no charger platform data supplied\n"); 2560 dev_err(di->dev, "no charger platform data supplied\n");
2556 ret = -EINVAL; 2561 ret = -EINVAL;
2557 goto free_device_info; 2562 goto free_device_info;
2558 } 2563 }
2559 di->pdata = plat_data->charger;
2560 2564
2561 /* get battery specific platform data */ 2565 /* get battery specific platform data */
2562 di->bat = plat_data->battery; 2566 di->bat = plat_data->battery;
2563 if (!di->bat) { 2567 if (!di->bat) {
2564 dev_err(di->dev, "no battery platform data supplied\n"); 2568 dev_err(di->dev, "no battery platform data supplied\n");
2565 ret = -EINVAL; 2569 ret = -EINVAL;
2566 goto free_device_info; 2570 goto free_device_info;
2567 } 2571 }
2568 2572
2569 di->autopower = false; 2573 di->autopower = false;
2570 2574
2571 /* AC supply */ 2575 /* AC supply */
2572 /* power_supply base class */ 2576 /* power_supply base class */
2573 di->ac_chg.psy.name = "ab8500_ac"; 2577 di->ac_chg.psy.name = "ab8500_ac";
2574 di->ac_chg.psy.type = POWER_SUPPLY_TYPE_MAINS; 2578 di->ac_chg.psy.type = POWER_SUPPLY_TYPE_MAINS;
2575 di->ac_chg.psy.properties = ab8500_charger_ac_props; 2579 di->ac_chg.psy.properties = ab8500_charger_ac_props;
2576 di->ac_chg.psy.num_properties = ARRAY_SIZE(ab8500_charger_ac_props); 2580 di->ac_chg.psy.num_properties = ARRAY_SIZE(ab8500_charger_ac_props);
2577 di->ac_chg.psy.get_property = ab8500_charger_ac_get_property; 2581 di->ac_chg.psy.get_property = ab8500_charger_ac_get_property;
2578 di->ac_chg.psy.supplied_to = di->pdata->supplied_to; 2582 di->ac_chg.psy.supplied_to = di->pdata->supplied_to;
2579 di->ac_chg.psy.num_supplicants = di->pdata->num_supplicants; 2583 di->ac_chg.psy.num_supplicants = di->pdata->num_supplicants;
2580 /* ux500_charger sub-class */ 2584 /* ux500_charger sub-class */
2581 di->ac_chg.ops.enable = &ab8500_charger_ac_en; 2585 di->ac_chg.ops.enable = &ab8500_charger_ac_en;
2582 di->ac_chg.ops.kick_wd = &ab8500_charger_watchdog_kick; 2586 di->ac_chg.ops.kick_wd = &ab8500_charger_watchdog_kick;
2583 di->ac_chg.ops.update_curr = &ab8500_charger_update_charger_current; 2587 di->ac_chg.ops.update_curr = &ab8500_charger_update_charger_current;
2584 di->ac_chg.max_out_volt = ab8500_charger_voltage_map[ 2588 di->ac_chg.max_out_volt = ab8500_charger_voltage_map[
2585 ARRAY_SIZE(ab8500_charger_voltage_map) - 1]; 2589 ARRAY_SIZE(ab8500_charger_voltage_map) - 1];
2586 di->ac_chg.max_out_curr = ab8500_charger_current_map[ 2590 di->ac_chg.max_out_curr = ab8500_charger_current_map[
2587 ARRAY_SIZE(ab8500_charger_current_map) - 1]; 2591 ARRAY_SIZE(ab8500_charger_current_map) - 1];
2588 2592
2589 /* USB supply */ 2593 /* USB supply */
2590 /* power_supply base class */ 2594 /* power_supply base class */
2591 di->usb_chg.psy.name = "ab8500_usb"; 2595 di->usb_chg.psy.name = "ab8500_usb";
2592 di->usb_chg.psy.type = POWER_SUPPLY_TYPE_USB; 2596 di->usb_chg.psy.type = POWER_SUPPLY_TYPE_USB;
2593 di->usb_chg.psy.properties = ab8500_charger_usb_props; 2597 di->usb_chg.psy.properties = ab8500_charger_usb_props;
2594 di->usb_chg.psy.num_properties = ARRAY_SIZE(ab8500_charger_usb_props); 2598 di->usb_chg.psy.num_properties = ARRAY_SIZE(ab8500_charger_usb_props);
2595 di->usb_chg.psy.get_property = ab8500_charger_usb_get_property; 2599 di->usb_chg.psy.get_property = ab8500_charger_usb_get_property;
2596 di->usb_chg.psy.supplied_to = di->pdata->supplied_to; 2600 di->usb_chg.psy.supplied_to = di->pdata->supplied_to;
2597 di->usb_chg.psy.num_supplicants = di->pdata->num_supplicants; 2601 di->usb_chg.psy.num_supplicants = di->pdata->num_supplicants;
2598 /* ux500_charger sub-class */ 2602 /* ux500_charger sub-class */
2599 di->usb_chg.ops.enable = &ab8500_charger_usb_en; 2603 di->usb_chg.ops.enable = &ab8500_charger_usb_en;
2600 di->usb_chg.ops.kick_wd = &ab8500_charger_watchdog_kick; 2604 di->usb_chg.ops.kick_wd = &ab8500_charger_watchdog_kick;
2601 di->usb_chg.ops.update_curr = &ab8500_charger_update_charger_current; 2605 di->usb_chg.ops.update_curr = &ab8500_charger_update_charger_current;
2602 di->usb_chg.max_out_volt = ab8500_charger_voltage_map[ 2606 di->usb_chg.max_out_volt = ab8500_charger_voltage_map[
2603 ARRAY_SIZE(ab8500_charger_voltage_map) - 1]; 2607 ARRAY_SIZE(ab8500_charger_voltage_map) - 1];
2604 di->usb_chg.max_out_curr = ab8500_charger_current_map[ 2608 di->usb_chg.max_out_curr = ab8500_charger_current_map[
2605 ARRAY_SIZE(ab8500_charger_current_map) - 1]; 2609 ARRAY_SIZE(ab8500_charger_current_map) - 1];
2606 2610
2607 2611
2608 /* Create a work queue for the charger */ 2612 /* Create a work queue for the charger */
2609 di->charger_wq = 2613 di->charger_wq =
2610 create_singlethread_workqueue("ab8500_charger_wq"); 2614 create_singlethread_workqueue("ab8500_charger_wq");
2611 if (di->charger_wq == NULL) { 2615 if (di->charger_wq == NULL) {
2612 dev_err(di->dev, "failed to create work queue\n"); 2616 dev_err(di->dev, "failed to create work queue\n");
2613 goto free_device_info; 2617 goto free_device_info;
2614 } 2618 }
2615 2619
2616 /* Init work for HW failure check */ 2620 /* Init work for HW failure check */
2617 INIT_DELAYED_WORK_DEFERRABLE(&di->check_hw_failure_work, 2621 INIT_DELAYED_WORK_DEFERRABLE(&di->check_hw_failure_work,
2618 ab8500_charger_check_hw_failure_work); 2622 ab8500_charger_check_hw_failure_work);
2619 INIT_DELAYED_WORK_DEFERRABLE(&di->check_usbchgnotok_work, 2623 INIT_DELAYED_WORK_DEFERRABLE(&di->check_usbchgnotok_work,
2620 ab8500_charger_check_usbchargernotok_work); 2624 ab8500_charger_check_usbchargernotok_work);
2621 2625
2622 /* 2626 /*
2623 * For ABB revision 1.0 and 1.1 there is a bug in the watchdog 2627 * For ABB revision 1.0 and 1.1 there is a bug in the watchdog
2624 * logic. That means we have to continously kick the charger 2628 * logic. That means we have to continously kick the charger
2625 * watchdog even when no charger is connected. This is only 2629 * watchdog even when no charger is connected. This is only
2626 * valid once the AC charger has been enabled. This is 2630 * valid once the AC charger has been enabled. This is
2627 * a bug that is not handled by the algorithm and the 2631 * a bug that is not handled by the algorithm and the
2628 * watchdog have to be kicked by the charger driver 2632 * watchdog have to be kicked by the charger driver
2629 * when the AC charger is disabled 2633 * when the AC charger is disabled
2630 */ 2634 */
2631 INIT_DELAYED_WORK_DEFERRABLE(&di->kick_wd_work, 2635 INIT_DELAYED_WORK_DEFERRABLE(&di->kick_wd_work,
2632 ab8500_charger_kick_watchdog_work); 2636 ab8500_charger_kick_watchdog_work);
2633 2637
2634 INIT_DELAYED_WORK_DEFERRABLE(&di->check_vbat_work, 2638 INIT_DELAYED_WORK_DEFERRABLE(&di->check_vbat_work,
2635 ab8500_charger_check_vbat_work); 2639 ab8500_charger_check_vbat_work);
2636 2640
2637 /* Init work for charger detection */ 2641 /* Init work for charger detection */
2638 INIT_WORK(&di->usb_link_status_work, 2642 INIT_WORK(&di->usb_link_status_work,
2639 ab8500_charger_usb_link_status_work); 2643 ab8500_charger_usb_link_status_work);
2640 INIT_WORK(&di->ac_work, ab8500_charger_ac_work); 2644 INIT_WORK(&di->ac_work, ab8500_charger_ac_work);
2641 INIT_WORK(&di->detect_usb_type_work, 2645 INIT_WORK(&di->detect_usb_type_work,
2642 ab8500_charger_detect_usb_type_work); 2646 ab8500_charger_detect_usb_type_work);
2643 2647
2644 INIT_WORK(&di->usb_state_changed_work, 2648 INIT_WORK(&di->usb_state_changed_work,
2645 ab8500_charger_usb_state_changed_work); 2649 ab8500_charger_usb_state_changed_work);
2646 2650
2647 /* Init work for checking HW status */ 2651 /* Init work for checking HW status */
2648 INIT_WORK(&di->check_main_thermal_prot_work, 2652 INIT_WORK(&di->check_main_thermal_prot_work,
2649 ab8500_charger_check_main_thermal_prot_work); 2653 ab8500_charger_check_main_thermal_prot_work);
2650 INIT_WORK(&di->check_usb_thermal_prot_work, 2654 INIT_WORK(&di->check_usb_thermal_prot_work,
2651 ab8500_charger_check_usb_thermal_prot_work); 2655 ab8500_charger_check_usb_thermal_prot_work);
2652 2656
2653 /* 2657 /*
2654 * VDD ADC supply needs to be enabled from this driver when there 2658 * VDD ADC supply needs to be enabled from this driver when there
2655 * is a charger connected to avoid erroneous BTEMP_HIGH/LOW 2659 * is a charger connected to avoid erroneous BTEMP_HIGH/LOW
2656 * interrupts during charging 2660 * interrupts during charging
2657 */ 2661 */
2658 di->regu = regulator_get(di->dev, "vddadc"); 2662 di->regu = regulator_get(di->dev, "vddadc");
2659 if (IS_ERR(di->regu)) { 2663 if (IS_ERR(di->regu)) {
2660 ret = PTR_ERR(di->regu); 2664 ret = PTR_ERR(di->regu);
2661 dev_err(di->dev, "failed to get vddadc regulator\n"); 2665 dev_err(di->dev, "failed to get vddadc regulator\n");
2662 goto free_charger_wq; 2666 goto free_charger_wq;
2663 } 2667 }
2664 2668
2665 2669
2666 /* Initialize OVV, and other registers */ 2670 /* Initialize OVV, and other registers */
2667 ret = ab8500_charger_init_hw_registers(di); 2671 ret = ab8500_charger_init_hw_registers(di);
2668 if (ret) { 2672 if (ret) {
2669 dev_err(di->dev, "failed to initialize ABB registers\n"); 2673 dev_err(di->dev, "failed to initialize ABB registers\n");
2670 goto free_regulator; 2674 goto free_regulator;
2671 } 2675 }
2672 2676
2673 /* Register AC charger class */ 2677 /* Register AC charger class */
2674 ret = power_supply_register(di->dev, &di->ac_chg.psy); 2678 ret = power_supply_register(di->dev, &di->ac_chg.psy);
2675 if (ret) { 2679 if (ret) {
2676 dev_err(di->dev, "failed to register AC charger\n"); 2680 dev_err(di->dev, "failed to register AC charger\n");
2677 goto free_regulator; 2681 goto free_regulator;
2678 } 2682 }
2679 2683
2680 /* Register USB charger class */ 2684 /* Register USB charger class */
2681 ret = power_supply_register(di->dev, &di->usb_chg.psy); 2685 ret = power_supply_register(di->dev, &di->usb_chg.psy);
2682 if (ret) { 2686 if (ret) {
2683 dev_err(di->dev, "failed to register USB charger\n"); 2687 dev_err(di->dev, "failed to register USB charger\n");
2684 goto free_ac; 2688 goto free_ac;
2685 } 2689 }
2686 2690
2687 di->usb_phy = usb_get_transceiver(); 2691 di->usb_phy = usb_get_transceiver();
2688 if (!di->usb_phy) { 2692 if (!di->usb_phy) {
2689 dev_err(di->dev, "failed to get usb transceiver\n"); 2693 dev_err(di->dev, "failed to get usb transceiver\n");
2690 ret = -EINVAL; 2694 ret = -EINVAL;
2691 goto free_usb; 2695 goto free_usb;
2692 } 2696 }
2693 di->nb.notifier_call = ab8500_charger_usb_notifier_call; 2697 di->nb.notifier_call = ab8500_charger_usb_notifier_call;
2694 ret = usb_register_notifier(di->usb_phy, &di->nb); 2698 ret = usb_register_notifier(di->usb_phy, &di->nb);
2695 if (ret) { 2699 if (ret) {
2696 dev_err(di->dev, "failed to register usb notifier\n"); 2700 dev_err(di->dev, "failed to register usb notifier\n");
2697 goto put_usb_phy; 2701 goto put_usb_phy;
2698 } 2702 }
2699 2703
2700 /* Identify the connected charger types during startup */ 2704 /* Identify the connected charger types during startup */
2701 charger_status = ab8500_charger_detect_chargers(di); 2705 charger_status = ab8500_charger_detect_chargers(di);
2702 if (charger_status & AC_PW_CONN) { 2706 if (charger_status & AC_PW_CONN) {
2703 di->ac.charger_connected = 1; 2707 di->ac.charger_connected = 1;
2704 di->ac_conn = true; 2708 di->ac_conn = true;
2705 ab8500_power_supply_changed(di, &di->ac_chg.psy); 2709 ab8500_power_supply_changed(di, &di->ac_chg.psy);
2706 sysfs_notify(&di->ac_chg.psy.dev->kobj, NULL, "present"); 2710 sysfs_notify(&di->ac_chg.psy.dev->kobj, NULL, "present");
2707 } 2711 }
2708 2712
2709 if (charger_status & USB_PW_CONN) { 2713 if (charger_status & USB_PW_CONN) {
2710 dev_dbg(di->dev, "VBUS Detect during startup\n"); 2714 dev_dbg(di->dev, "VBUS Detect during startup\n");
2711 di->vbus_detected = true; 2715 di->vbus_detected = true;
2712 di->vbus_detected_start = true; 2716 di->vbus_detected_start = true;
2713 queue_work(di->charger_wq, 2717 queue_work(di->charger_wq,
2714 &di->detect_usb_type_work); 2718 &di->detect_usb_type_work);
2715 } 2719 }
2716 2720
2717 /* Register interrupts */ 2721 /* Register interrupts */
2718 for (i = 0; i < ARRAY_SIZE(ab8500_charger_irq); i++) { 2722 for (i = 0; i < ARRAY_SIZE(ab8500_charger_irq); i++) {
2719 irq = platform_get_irq_byname(pdev, ab8500_charger_irq[i].name); 2723 irq = platform_get_irq_byname(pdev, ab8500_charger_irq[i].name);
2720 ret = request_threaded_irq(irq, NULL, ab8500_charger_irq[i].isr, 2724 ret = request_threaded_irq(irq, NULL, ab8500_charger_irq[i].isr,
2721 IRQF_SHARED | IRQF_NO_SUSPEND, 2725 IRQF_SHARED | IRQF_NO_SUSPEND,
2722 ab8500_charger_irq[i].name, di); 2726 ab8500_charger_irq[i].name, di);
2723 2727
2724 if (ret != 0) { 2728 if (ret != 0) {
2725 dev_err(di->dev, "failed to request %s IRQ %d: %d\n" 2729 dev_err(di->dev, "failed to request %s IRQ %d: %d\n"
2726 , ab8500_charger_irq[i].name, irq, ret); 2730 , ab8500_charger_irq[i].name, irq, ret);
2727 goto free_irq; 2731 goto free_irq;
2728 } 2732 }
2729 dev_dbg(di->dev, "Requested %s IRQ %d: %d\n", 2733 dev_dbg(di->dev, "Requested %s IRQ %d: %d\n",
2730 ab8500_charger_irq[i].name, irq, ret); 2734 ab8500_charger_irq[i].name, irq, ret);
2731 } 2735 }
2732 2736
2733 platform_set_drvdata(pdev, di); 2737 platform_set_drvdata(pdev, di);
2734 2738
2735 return ret; 2739 return ret;
2736 2740
2737 free_irq: 2741 free_irq:
2738 usb_unregister_notifier(di->usb_phy, &di->nb); 2742 usb_unregister_notifier(di->usb_phy, &di->nb);
2739 2743
2740 /* We also have to free all successfully registered irqs */ 2744 /* We also have to free all successfully registered irqs */
2741 for (i = i - 1; i >= 0; i--) { 2745 for (i = i - 1; i >= 0; i--) {
2742 irq = platform_get_irq_byname(pdev, ab8500_charger_irq[i].name); 2746 irq = platform_get_irq_byname(pdev, ab8500_charger_irq[i].name);
2743 free_irq(irq, di); 2747 free_irq(irq, di);
2744 } 2748 }
2745 put_usb_phy: 2749 put_usb_phy:
2746 usb_put_transceiver(di->usb_phy); 2750 usb_put_transceiver(di->usb_phy);
2747 free_usb: 2751 free_usb:
2748 power_supply_unregister(&di->usb_chg.psy); 2752 power_supply_unregister(&di->usb_chg.psy);
2749 free_ac: 2753 free_ac:
2750 power_supply_unregister(&di->ac_chg.psy); 2754 power_supply_unregister(&di->ac_chg.psy);
2751 free_regulator: 2755 free_regulator:
2752 regulator_put(di->regu); 2756 regulator_put(di->regu);
2753 free_charger_wq: 2757 free_charger_wq:
2754 destroy_workqueue(di->charger_wq); 2758 destroy_workqueue(di->charger_wq);
2755 free_device_info: 2759 free_device_info:
2756 kfree(di); 2760 kfree(di);
2757 2761
2758 return ret; 2762 return ret;
2759 } 2763 }
2760 2764
2761 static struct platform_driver ab8500_charger_driver = { 2765 static struct platform_driver ab8500_charger_driver = {
2762 .probe = ab8500_charger_probe, 2766 .probe = ab8500_charger_probe,
2763 .remove = __devexit_p(ab8500_charger_remove), 2767 .remove = __devexit_p(ab8500_charger_remove),
2764 .suspend = ab8500_charger_suspend, 2768 .suspend = ab8500_charger_suspend,
2765 .resume = ab8500_charger_resume, 2769 .resume = ab8500_charger_resume,
2766 .driver = { 2770 .driver = {
2767 .name = "ab8500-charger", 2771 .name = "ab8500-charger",
2768 .owner = THIS_MODULE, 2772 .owner = THIS_MODULE,
2769 }, 2773 },
2770 }; 2774 };
2771 2775
2772 static int __init ab8500_charger_init(void) 2776 static int __init ab8500_charger_init(void)
2773 { 2777 {
2774 return platform_driver_register(&ab8500_charger_driver); 2778 return platform_driver_register(&ab8500_charger_driver);
2775 } 2779 }
2776 2780
2777 static void __exit ab8500_charger_exit(void) 2781 static void __exit ab8500_charger_exit(void)
2778 { 2782 {
2779 platform_driver_unregister(&ab8500_charger_driver); 2783 platform_driver_unregister(&ab8500_charger_driver);
2780 } 2784 }
2781 2785
2782 subsys_initcall_sync(ab8500_charger_init); 2786 subsys_initcall_sync(ab8500_charger_init);
2783 module_exit(ab8500_charger_exit); 2787 module_exit(ab8500_charger_exit);
2784 2788
2785 MODULE_LICENSE("GPL v2"); 2789 MODULE_LICENSE("GPL v2");
2786 MODULE_AUTHOR("Johan Palsson, Karl Komierowski, Arun R Murthy"); 2790 MODULE_AUTHOR("Johan Palsson, Karl Komierowski, Arun R Murthy");
2787 MODULE_ALIAS("platform:ab8500-charger"); 2791 MODULE_ALIAS("platform:ab8500-charger");
2788 MODULE_DESCRIPTION("AB8500 charger management driver"); 2792 MODULE_DESCRIPTION("AB8500 charger management driver");
drivers/power/ab8500_fg.c
1 /* 1 /*
2 * Copyright (C) ST-Ericsson AB 2012 2 * Copyright (C) ST-Ericsson AB 2012
3 * 3 *
4 * Main and Back-up battery management driver. 4 * Main and Back-up battery management driver.
5 * 5 *
6 * Note: Backup battery management is required in case of Li-Ion battery and not 6 * Note: Backup battery management is required in case of Li-Ion battery and not
7 * for capacitive battery. HREF boards have capacitive battery and hence backup 7 * for capacitive battery. HREF boards have capacitive battery and hence backup
8 * battery management is not used and the supported code is available in this 8 * battery management is not used and the supported code is available in this
9 * driver. 9 * driver.
10 * 10 *
11 * License Terms: GNU General Public License v2 11 * License Terms: GNU General Public License v2
12 * Author: 12 * Author:
13 * Johan Palsson <johan.palsson@stericsson.com> 13 * Johan Palsson <johan.palsson@stericsson.com>
14 * Karl Komierowski <karl.komierowski@stericsson.com> 14 * Karl Komierowski <karl.komierowski@stericsson.com>
15 * Arun R Murthy <arun.murthy@stericsson.com> 15 * Arun R Murthy <arun.murthy@stericsson.com>
16 */ 16 */
17 17
18 #include <linux/init.h> 18 #include <linux/init.h>
19 #include <linux/module.h> 19 #include <linux/module.h>
20 #include <linux/device.h> 20 #include <linux/device.h>
21 #include <linux/interrupt.h> 21 #include <linux/interrupt.h>
22 #include <linux/platform_device.h> 22 #include <linux/platform_device.h>
23 #include <linux/power_supply.h> 23 #include <linux/power_supply.h>
24 #include <linux/kobject.h> 24 #include <linux/kobject.h>
25 #include <linux/mfd/abx500/ab8500.h> 25 #include <linux/mfd/abx500/ab8500.h>
26 #include <linux/mfd/abx500.h> 26 #include <linux/mfd/abx500.h>
27 #include <linux/slab.h> 27 #include <linux/slab.h>
28 #include <linux/mfd/abx500/ab8500-bm.h> 28 #include <linux/mfd/abx500/ab8500-bm.h>
29 #include <linux/delay.h> 29 #include <linux/delay.h>
30 #include <linux/mfd/abx500/ab8500-gpadc.h> 30 #include <linux/mfd/abx500/ab8500-gpadc.h>
31 #include <linux/mfd/abx500.h> 31 #include <linux/mfd/abx500.h>
32 #include <linux/time.h> 32 #include <linux/time.h>
33 #include <linux/completion.h> 33 #include <linux/completion.h>
34 34
35 #define MILLI_TO_MICRO 1000 35 #define MILLI_TO_MICRO 1000
36 #define FG_LSB_IN_MA 1627 36 #define FG_LSB_IN_MA 1627
37 #define QLSB_NANO_AMP_HOURS_X10 1129 37 #define QLSB_NANO_AMP_HOURS_X10 1129
38 #define INS_CURR_TIMEOUT (3 * HZ) 38 #define INS_CURR_TIMEOUT (3 * HZ)
39 39
40 #define SEC_TO_SAMPLE(S) (S * 4) 40 #define SEC_TO_SAMPLE(S) (S * 4)
41 41
42 #define NBR_AVG_SAMPLES 20 42 #define NBR_AVG_SAMPLES 20
43 43
44 #define LOW_BAT_CHECK_INTERVAL (2 * HZ) 44 #define LOW_BAT_CHECK_INTERVAL (2 * HZ)
45 45
46 #define VALID_CAPACITY_SEC (45 * 60) /* 45 minutes */ 46 #define VALID_CAPACITY_SEC (45 * 60) /* 45 minutes */
47 #define BATT_OK_MIN 2360 /* mV */ 47 #define BATT_OK_MIN 2360 /* mV */
48 #define BATT_OK_INCREMENT 50 /* mV */ 48 #define BATT_OK_INCREMENT 50 /* mV */
49 #define BATT_OK_MAX_NR_INCREMENTS 0xE 49 #define BATT_OK_MAX_NR_INCREMENTS 0xE
50 50
51 /* FG constants */ 51 /* FG constants */
52 #define BATT_OVV 0x01 52 #define BATT_OVV 0x01
53 53
54 #define interpolate(x, x1, y1, x2, y2) \ 54 #define interpolate(x, x1, y1, x2, y2) \
55 ((y1) + ((((y2) - (y1)) * ((x) - (x1))) / ((x2) - (x1)))); 55 ((y1) + ((((y2) - (y1)) * ((x) - (x1))) / ((x2) - (x1))));
56 56
57 #define to_ab8500_fg_device_info(x) container_of((x), \ 57 #define to_ab8500_fg_device_info(x) container_of((x), \
58 struct ab8500_fg, fg_psy); 58 struct ab8500_fg, fg_psy);
59 59
60 /** 60 /**
61 * struct ab8500_fg_interrupts - ab8500 fg interupts 61 * struct ab8500_fg_interrupts - ab8500 fg interupts
62 * @name: name of the interrupt 62 * @name: name of the interrupt
63 * @isr function pointer to the isr 63 * @isr function pointer to the isr
64 */ 64 */
65 struct ab8500_fg_interrupts { 65 struct ab8500_fg_interrupts {
66 char *name; 66 char *name;
67 irqreturn_t (*isr)(int irq, void *data); 67 irqreturn_t (*isr)(int irq, void *data);
68 }; 68 };
69 69
70 enum ab8500_fg_discharge_state { 70 enum ab8500_fg_discharge_state {
71 AB8500_FG_DISCHARGE_INIT, 71 AB8500_FG_DISCHARGE_INIT,
72 AB8500_FG_DISCHARGE_INITMEASURING, 72 AB8500_FG_DISCHARGE_INITMEASURING,
73 AB8500_FG_DISCHARGE_INIT_RECOVERY, 73 AB8500_FG_DISCHARGE_INIT_RECOVERY,
74 AB8500_FG_DISCHARGE_RECOVERY, 74 AB8500_FG_DISCHARGE_RECOVERY,
75 AB8500_FG_DISCHARGE_READOUT_INIT, 75 AB8500_FG_DISCHARGE_READOUT_INIT,
76 AB8500_FG_DISCHARGE_READOUT, 76 AB8500_FG_DISCHARGE_READOUT,
77 AB8500_FG_DISCHARGE_WAKEUP, 77 AB8500_FG_DISCHARGE_WAKEUP,
78 }; 78 };
79 79
80 static char *discharge_state[] = { 80 static char *discharge_state[] = {
81 "DISCHARGE_INIT", 81 "DISCHARGE_INIT",
82 "DISCHARGE_INITMEASURING", 82 "DISCHARGE_INITMEASURING",
83 "DISCHARGE_INIT_RECOVERY", 83 "DISCHARGE_INIT_RECOVERY",
84 "DISCHARGE_RECOVERY", 84 "DISCHARGE_RECOVERY",
85 "DISCHARGE_READOUT_INIT", 85 "DISCHARGE_READOUT_INIT",
86 "DISCHARGE_READOUT", 86 "DISCHARGE_READOUT",
87 "DISCHARGE_WAKEUP", 87 "DISCHARGE_WAKEUP",
88 }; 88 };
89 89
90 enum ab8500_fg_charge_state { 90 enum ab8500_fg_charge_state {
91 AB8500_FG_CHARGE_INIT, 91 AB8500_FG_CHARGE_INIT,
92 AB8500_FG_CHARGE_READOUT, 92 AB8500_FG_CHARGE_READOUT,
93 }; 93 };
94 94
95 static char *charge_state[] = { 95 static char *charge_state[] = {
96 "CHARGE_INIT", 96 "CHARGE_INIT",
97 "CHARGE_READOUT", 97 "CHARGE_READOUT",
98 }; 98 };
99 99
100 enum ab8500_fg_calibration_state { 100 enum ab8500_fg_calibration_state {
101 AB8500_FG_CALIB_INIT, 101 AB8500_FG_CALIB_INIT,
102 AB8500_FG_CALIB_WAIT, 102 AB8500_FG_CALIB_WAIT,
103 AB8500_FG_CALIB_END, 103 AB8500_FG_CALIB_END,
104 }; 104 };
105 105
106 struct ab8500_fg_avg_cap { 106 struct ab8500_fg_avg_cap {
107 int avg; 107 int avg;
108 int samples[NBR_AVG_SAMPLES]; 108 int samples[NBR_AVG_SAMPLES];
109 __kernel_time_t time_stamps[NBR_AVG_SAMPLES]; 109 __kernel_time_t time_stamps[NBR_AVG_SAMPLES];
110 int pos; 110 int pos;
111 int nbr_samples; 111 int nbr_samples;
112 int sum; 112 int sum;
113 }; 113 };
114 114
115 struct ab8500_fg_battery_capacity { 115 struct ab8500_fg_battery_capacity {
116 int max_mah_design; 116 int max_mah_design;
117 int max_mah; 117 int max_mah;
118 int mah; 118 int mah;
119 int permille; 119 int permille;
120 int level; 120 int level;
121 int prev_mah; 121 int prev_mah;
122 int prev_percent; 122 int prev_percent;
123 int prev_level; 123 int prev_level;
124 int user_mah; 124 int user_mah;
125 }; 125 };
126 126
127 struct ab8500_fg_flags { 127 struct ab8500_fg_flags {
128 bool fg_enabled; 128 bool fg_enabled;
129 bool conv_done; 129 bool conv_done;
130 bool charging; 130 bool charging;
131 bool fully_charged; 131 bool fully_charged;
132 bool force_full; 132 bool force_full;
133 bool low_bat_delay; 133 bool low_bat_delay;
134 bool low_bat; 134 bool low_bat;
135 bool bat_ovv; 135 bool bat_ovv;
136 bool batt_unknown; 136 bool batt_unknown;
137 bool calibrate; 137 bool calibrate;
138 bool user_cap; 138 bool user_cap;
139 bool batt_id_received; 139 bool batt_id_received;
140 }; 140 };
141 141
142 struct inst_curr_result_list { 142 struct inst_curr_result_list {
143 struct list_head list; 143 struct list_head list;
144 int *result; 144 int *result;
145 }; 145 };
146 146
147 /** 147 /**
148 * struct ab8500_fg - ab8500 FG device information 148 * struct ab8500_fg - ab8500 FG device information
149 * @dev: Pointer to the structure device 149 * @dev: Pointer to the structure device
150 * @node: a list of AB8500 FGs, hence prepared for reentrance 150 * @node: a list of AB8500 FGs, hence prepared for reentrance
151 * @irq holds the CCEOC interrupt number 151 * @irq holds the CCEOC interrupt number
152 * @vbat: Battery voltage in mV 152 * @vbat: Battery voltage in mV
153 * @vbat_nom: Nominal battery voltage in mV 153 * @vbat_nom: Nominal battery voltage in mV
154 * @inst_curr: Instantenous battery current in mA 154 * @inst_curr: Instantenous battery current in mA
155 * @avg_curr: Average battery current in mA 155 * @avg_curr: Average battery current in mA
156 * @bat_temp battery temperature 156 * @bat_temp battery temperature
157 * @fg_samples: Number of samples used in the FG accumulation 157 * @fg_samples: Number of samples used in the FG accumulation
158 * @accu_charge: Accumulated charge from the last conversion 158 * @accu_charge: Accumulated charge from the last conversion
159 * @recovery_cnt: Counter for recovery mode 159 * @recovery_cnt: Counter for recovery mode
160 * @high_curr_cnt: Counter for high current mode 160 * @high_curr_cnt: Counter for high current mode
161 * @init_cnt: Counter for init mode 161 * @init_cnt: Counter for init mode
162 * @recovery_needed: Indicate if recovery is needed 162 * @recovery_needed: Indicate if recovery is needed
163 * @high_curr_mode: Indicate if we're in high current mode 163 * @high_curr_mode: Indicate if we're in high current mode
164 * @init_capacity: Indicate if initial capacity measuring should be done 164 * @init_capacity: Indicate if initial capacity measuring should be done
165 * @turn_off_fg: True if fg was off before current measurement 165 * @turn_off_fg: True if fg was off before current measurement
166 * @calib_state State during offset calibration 166 * @calib_state State during offset calibration
167 * @discharge_state: Current discharge state 167 * @discharge_state: Current discharge state
168 * @charge_state: Current charge state 168 * @charge_state: Current charge state
169 * @ab8500_fg_complete Completion struct used for the instant current reading 169 * @ab8500_fg_complete Completion struct used for the instant current reading
170 * @flags: Structure for information about events triggered 170 * @flags: Structure for information about events triggered
171 * @bat_cap: Structure for battery capacity specific parameters 171 * @bat_cap: Structure for battery capacity specific parameters
172 * @avg_cap: Average capacity filter 172 * @avg_cap: Average capacity filter
173 * @parent: Pointer to the struct ab8500 173 * @parent: Pointer to the struct ab8500
174 * @gpadc: Pointer to the struct gpadc 174 * @gpadc: Pointer to the struct gpadc
175 * @pdata: Pointer to the abx500_fg platform data 175 * @pdata: Pointer to the abx500_fg platform data
176 * @bat: Pointer to the abx500_bm platform data 176 * @bat: Pointer to the abx500_bm platform data
177 * @fg_psy: Structure that holds the FG specific battery properties 177 * @fg_psy: Structure that holds the FG specific battery properties
178 * @fg_wq: Work queue for running the FG algorithm 178 * @fg_wq: Work queue for running the FG algorithm
179 * @fg_periodic_work: Work to run the FG algorithm periodically 179 * @fg_periodic_work: Work to run the FG algorithm periodically
180 * @fg_low_bat_work: Work to check low bat condition 180 * @fg_low_bat_work: Work to check low bat condition
181 * @fg_reinit_work Work used to reset and reinitialise the FG algorithm 181 * @fg_reinit_work Work used to reset and reinitialise the FG algorithm
182 * @fg_work: Work to run the FG algorithm instantly 182 * @fg_work: Work to run the FG algorithm instantly
183 * @fg_acc_cur_work: Work to read the FG accumulator 183 * @fg_acc_cur_work: Work to read the FG accumulator
184 * @fg_check_hw_failure_work: Work for checking HW state 184 * @fg_check_hw_failure_work: Work for checking HW state
185 * @cc_lock: Mutex for locking the CC 185 * @cc_lock: Mutex for locking the CC
186 * @fg_kobject: Structure of type kobject 186 * @fg_kobject: Structure of type kobject
187 */ 187 */
188 struct ab8500_fg { 188 struct ab8500_fg {
189 struct device *dev; 189 struct device *dev;
190 struct list_head node; 190 struct list_head node;
191 int irq; 191 int irq;
192 int vbat; 192 int vbat;
193 int vbat_nom; 193 int vbat_nom;
194 int inst_curr; 194 int inst_curr;
195 int avg_curr; 195 int avg_curr;
196 int bat_temp; 196 int bat_temp;
197 int fg_samples; 197 int fg_samples;
198 int accu_charge; 198 int accu_charge;
199 int recovery_cnt; 199 int recovery_cnt;
200 int high_curr_cnt; 200 int high_curr_cnt;
201 int init_cnt; 201 int init_cnt;
202 bool recovery_needed; 202 bool recovery_needed;
203 bool high_curr_mode; 203 bool high_curr_mode;
204 bool init_capacity; 204 bool init_capacity;
205 bool turn_off_fg; 205 bool turn_off_fg;
206 enum ab8500_fg_calibration_state calib_state; 206 enum ab8500_fg_calibration_state calib_state;
207 enum ab8500_fg_discharge_state discharge_state; 207 enum ab8500_fg_discharge_state discharge_state;
208 enum ab8500_fg_charge_state charge_state; 208 enum ab8500_fg_charge_state charge_state;
209 struct completion ab8500_fg_complete; 209 struct completion ab8500_fg_complete;
210 struct ab8500_fg_flags flags; 210 struct ab8500_fg_flags flags;
211 struct ab8500_fg_battery_capacity bat_cap; 211 struct ab8500_fg_battery_capacity bat_cap;
212 struct ab8500_fg_avg_cap avg_cap; 212 struct ab8500_fg_avg_cap avg_cap;
213 struct ab8500 *parent; 213 struct ab8500 *parent;
214 struct ab8500_gpadc *gpadc; 214 struct ab8500_gpadc *gpadc;
215 struct abx500_fg_platform_data *pdata; 215 struct abx500_fg_platform_data *pdata;
216 struct abx500_bm_data *bat; 216 struct abx500_bm_data *bat;
217 struct power_supply fg_psy; 217 struct power_supply fg_psy;
218 struct workqueue_struct *fg_wq; 218 struct workqueue_struct *fg_wq;
219 struct delayed_work fg_periodic_work; 219 struct delayed_work fg_periodic_work;
220 struct delayed_work fg_low_bat_work; 220 struct delayed_work fg_low_bat_work;
221 struct delayed_work fg_reinit_work; 221 struct delayed_work fg_reinit_work;
222 struct work_struct fg_work; 222 struct work_struct fg_work;
223 struct work_struct fg_acc_cur_work; 223 struct work_struct fg_acc_cur_work;
224 struct delayed_work fg_check_hw_failure_work; 224 struct delayed_work fg_check_hw_failure_work;
225 struct mutex cc_lock; 225 struct mutex cc_lock;
226 struct kobject fg_kobject; 226 struct kobject fg_kobject;
227 }; 227 };
228 static LIST_HEAD(ab8500_fg_list); 228 static LIST_HEAD(ab8500_fg_list);
229 229
230 /** 230 /**
231 * ab8500_fg_get() - returns a reference to the primary AB8500 fuel gauge 231 * ab8500_fg_get() - returns a reference to the primary AB8500 fuel gauge
232 * (i.e. the first fuel gauge in the instance list) 232 * (i.e. the first fuel gauge in the instance list)
233 */ 233 */
234 struct ab8500_fg *ab8500_fg_get(void) 234 struct ab8500_fg *ab8500_fg_get(void)
235 { 235 {
236 struct ab8500_fg *fg; 236 struct ab8500_fg *fg;
237 237
238 if (list_empty(&ab8500_fg_list)) 238 if (list_empty(&ab8500_fg_list))
239 return NULL; 239 return NULL;
240 240
241 fg = list_first_entry(&ab8500_fg_list, struct ab8500_fg, node); 241 fg = list_first_entry(&ab8500_fg_list, struct ab8500_fg, node);
242 return fg; 242 return fg;
243 } 243 }
244 244
245 /* Main battery properties */ 245 /* Main battery properties */
246 static enum power_supply_property ab8500_fg_props[] = { 246 static enum power_supply_property ab8500_fg_props[] = {
247 POWER_SUPPLY_PROP_VOLTAGE_NOW, 247 POWER_SUPPLY_PROP_VOLTAGE_NOW,
248 POWER_SUPPLY_PROP_CURRENT_NOW, 248 POWER_SUPPLY_PROP_CURRENT_NOW,
249 POWER_SUPPLY_PROP_CURRENT_AVG, 249 POWER_SUPPLY_PROP_CURRENT_AVG,
250 POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN, 250 POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN,
251 POWER_SUPPLY_PROP_ENERGY_FULL, 251 POWER_SUPPLY_PROP_ENERGY_FULL,
252 POWER_SUPPLY_PROP_ENERGY_NOW, 252 POWER_SUPPLY_PROP_ENERGY_NOW,
253 POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN, 253 POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN,
254 POWER_SUPPLY_PROP_CHARGE_FULL, 254 POWER_SUPPLY_PROP_CHARGE_FULL,
255 POWER_SUPPLY_PROP_CHARGE_NOW, 255 POWER_SUPPLY_PROP_CHARGE_NOW,
256 POWER_SUPPLY_PROP_CAPACITY, 256 POWER_SUPPLY_PROP_CAPACITY,
257 POWER_SUPPLY_PROP_CAPACITY_LEVEL, 257 POWER_SUPPLY_PROP_CAPACITY_LEVEL,
258 }; 258 };
259 259
260 /* 260 /*
261 * This array maps the raw hex value to lowbat voltage used by the AB8500 261 * This array maps the raw hex value to lowbat voltage used by the AB8500
262 * Values taken from the UM0836 262 * Values taken from the UM0836
263 */ 263 */
264 static int ab8500_fg_lowbat_voltage_map[] = { 264 static int ab8500_fg_lowbat_voltage_map[] = {
265 2300 , 265 2300 ,
266 2325 , 266 2325 ,
267 2350 , 267 2350 ,
268 2375 , 268 2375 ,
269 2400 , 269 2400 ,
270 2425 , 270 2425 ,
271 2450 , 271 2450 ,
272 2475 , 272 2475 ,
273 2500 , 273 2500 ,
274 2525 , 274 2525 ,
275 2550 , 275 2550 ,
276 2575 , 276 2575 ,
277 2600 , 277 2600 ,
278 2625 , 278 2625 ,
279 2650 , 279 2650 ,
280 2675 , 280 2675 ,
281 2700 , 281 2700 ,
282 2725 , 282 2725 ,
283 2750 , 283 2750 ,
284 2775 , 284 2775 ,
285 2800 , 285 2800 ,
286 2825 , 286 2825 ,
287 2850 , 287 2850 ,
288 2875 , 288 2875 ,
289 2900 , 289 2900 ,
290 2925 , 290 2925 ,
291 2950 , 291 2950 ,
292 2975 , 292 2975 ,
293 3000 , 293 3000 ,
294 3025 , 294 3025 ,
295 3050 , 295 3050 ,
296 3075 , 296 3075 ,
297 3100 , 297 3100 ,
298 3125 , 298 3125 ,
299 3150 , 299 3150 ,
300 3175 , 300 3175 ,
301 3200 , 301 3200 ,
302 3225 , 302 3225 ,
303 3250 , 303 3250 ,
304 3275 , 304 3275 ,
305 3300 , 305 3300 ,
306 3325 , 306 3325 ,
307 3350 , 307 3350 ,
308 3375 , 308 3375 ,
309 3400 , 309 3400 ,
310 3425 , 310 3425 ,
311 3450 , 311 3450 ,
312 3475 , 312 3475 ,
313 3500 , 313 3500 ,
314 3525 , 314 3525 ,
315 3550 , 315 3550 ,
316 3575 , 316 3575 ,
317 3600 , 317 3600 ,
318 3625 , 318 3625 ,
319 3650 , 319 3650 ,
320 3675 , 320 3675 ,
321 3700 , 321 3700 ,
322 3725 , 322 3725 ,
323 3750 , 323 3750 ,
324 3775 , 324 3775 ,
325 3800 , 325 3800 ,
326 3825 , 326 3825 ,
327 3850 , 327 3850 ,
328 3850 , 328 3850 ,
329 }; 329 };
330 330
331 static u8 ab8500_volt_to_regval(int voltage) 331 static u8 ab8500_volt_to_regval(int voltage)
332 { 332 {
333 int i; 333 int i;
334 334
335 if (voltage < ab8500_fg_lowbat_voltage_map[0]) 335 if (voltage < ab8500_fg_lowbat_voltage_map[0])
336 return 0; 336 return 0;
337 337
338 for (i = 0; i < ARRAY_SIZE(ab8500_fg_lowbat_voltage_map); i++) { 338 for (i = 0; i < ARRAY_SIZE(ab8500_fg_lowbat_voltage_map); i++) {
339 if (voltage < ab8500_fg_lowbat_voltage_map[i]) 339 if (voltage < ab8500_fg_lowbat_voltage_map[i])
340 return (u8) i - 1; 340 return (u8) i - 1;
341 } 341 }
342 342
343 /* If not captured above, return index of last element */ 343 /* If not captured above, return index of last element */
344 return (u8) ARRAY_SIZE(ab8500_fg_lowbat_voltage_map) - 1; 344 return (u8) ARRAY_SIZE(ab8500_fg_lowbat_voltage_map) - 1;
345 } 345 }
346 346
347 /** 347 /**
348 * ab8500_fg_is_low_curr() - Low or high current mode 348 * ab8500_fg_is_low_curr() - Low or high current mode
349 * @di: pointer to the ab8500_fg structure 349 * @di: pointer to the ab8500_fg structure
350 * @curr: the current to base or our decision on 350 * @curr: the current to base or our decision on
351 * 351 *
352 * Low current mode if the current consumption is below a certain threshold 352 * Low current mode if the current consumption is below a certain threshold
353 */ 353 */
354 static int ab8500_fg_is_low_curr(struct ab8500_fg *di, int curr) 354 static int ab8500_fg_is_low_curr(struct ab8500_fg *di, int curr)
355 { 355 {
356 /* 356 /*
357 * We want to know if we're in low current mode 357 * We want to know if we're in low current mode
358 */ 358 */
359 if (curr > -di->bat->fg_params->high_curr_threshold) 359 if (curr > -di->bat->fg_params->high_curr_threshold)
360 return true; 360 return true;
361 else 361 else
362 return false; 362 return false;
363 } 363 }
364 364
365 /** 365 /**
366 * ab8500_fg_add_cap_sample() - Add capacity to average filter 366 * ab8500_fg_add_cap_sample() - Add capacity to average filter
367 * @di: pointer to the ab8500_fg structure 367 * @di: pointer to the ab8500_fg structure
368 * @sample: the capacity in mAh to add to the filter 368 * @sample: the capacity in mAh to add to the filter
369 * 369 *
370 * A capacity is added to the filter and a new mean capacity is calculated and 370 * A capacity is added to the filter and a new mean capacity is calculated and
371 * returned 371 * returned
372 */ 372 */
373 static int ab8500_fg_add_cap_sample(struct ab8500_fg *di, int sample) 373 static int ab8500_fg_add_cap_sample(struct ab8500_fg *di, int sample)
374 { 374 {
375 struct timespec ts; 375 struct timespec ts;
376 struct ab8500_fg_avg_cap *avg = &di->avg_cap; 376 struct ab8500_fg_avg_cap *avg = &di->avg_cap;
377 377
378 getnstimeofday(&ts); 378 getnstimeofday(&ts);
379 379
380 do { 380 do {
381 avg->sum += sample - avg->samples[avg->pos]; 381 avg->sum += sample - avg->samples[avg->pos];
382 avg->samples[avg->pos] = sample; 382 avg->samples[avg->pos] = sample;
383 avg->time_stamps[avg->pos] = ts.tv_sec; 383 avg->time_stamps[avg->pos] = ts.tv_sec;
384 avg->pos++; 384 avg->pos++;
385 385
386 if (avg->pos == NBR_AVG_SAMPLES) 386 if (avg->pos == NBR_AVG_SAMPLES)
387 avg->pos = 0; 387 avg->pos = 0;
388 388
389 if (avg->nbr_samples < NBR_AVG_SAMPLES) 389 if (avg->nbr_samples < NBR_AVG_SAMPLES)
390 avg->nbr_samples++; 390 avg->nbr_samples++;
391 391
392 /* 392 /*
393 * Check the time stamp for each sample. If too old, 393 * Check the time stamp for each sample. If too old,
394 * replace with latest sample 394 * replace with latest sample
395 */ 395 */
396 } while (ts.tv_sec - VALID_CAPACITY_SEC > avg->time_stamps[avg->pos]); 396 } while (ts.tv_sec - VALID_CAPACITY_SEC > avg->time_stamps[avg->pos]);
397 397
398 avg->avg = avg->sum / avg->nbr_samples; 398 avg->avg = avg->sum / avg->nbr_samples;
399 399
400 return avg->avg; 400 return avg->avg;
401 } 401 }
402 402
403 /** 403 /**
404 * ab8500_fg_clear_cap_samples() - Clear average filter 404 * ab8500_fg_clear_cap_samples() - Clear average filter
405 * @di: pointer to the ab8500_fg structure 405 * @di: pointer to the ab8500_fg structure
406 * 406 *
407 * The capacity filter is is reset to zero. 407 * The capacity filter is is reset to zero.
408 */ 408 */
409 static void ab8500_fg_clear_cap_samples(struct ab8500_fg *di) 409 static void ab8500_fg_clear_cap_samples(struct ab8500_fg *di)
410 { 410 {
411 int i; 411 int i;
412 struct ab8500_fg_avg_cap *avg = &di->avg_cap; 412 struct ab8500_fg_avg_cap *avg = &di->avg_cap;
413 413
414 avg->pos = 0; 414 avg->pos = 0;
415 avg->nbr_samples = 0; 415 avg->nbr_samples = 0;
416 avg->sum = 0; 416 avg->sum = 0;
417 avg->avg = 0; 417 avg->avg = 0;
418 418
419 for (i = 0; i < NBR_AVG_SAMPLES; i++) { 419 for (i = 0; i < NBR_AVG_SAMPLES; i++) {
420 avg->samples[i] = 0; 420 avg->samples[i] = 0;
421 avg->time_stamps[i] = 0; 421 avg->time_stamps[i] = 0;
422 } 422 }
423 } 423 }
424 424
425 /** 425 /**
426 * ab8500_fg_fill_cap_sample() - Fill average filter 426 * ab8500_fg_fill_cap_sample() - Fill average filter
427 * @di: pointer to the ab8500_fg structure 427 * @di: pointer to the ab8500_fg structure
428 * @sample: the capacity in mAh to fill the filter with 428 * @sample: the capacity in mAh to fill the filter with
429 * 429 *
430 * The capacity filter is filled with a capacity in mAh 430 * The capacity filter is filled with a capacity in mAh
431 */ 431 */
432 static void ab8500_fg_fill_cap_sample(struct ab8500_fg *di, int sample) 432 static void ab8500_fg_fill_cap_sample(struct ab8500_fg *di, int sample)
433 { 433 {
434 int i; 434 int i;
435 struct timespec ts; 435 struct timespec ts;
436 struct ab8500_fg_avg_cap *avg = &di->avg_cap; 436 struct ab8500_fg_avg_cap *avg = &di->avg_cap;
437 437
438 getnstimeofday(&ts); 438 getnstimeofday(&ts);
439 439
440 for (i = 0; i < NBR_AVG_SAMPLES; i++) { 440 for (i = 0; i < NBR_AVG_SAMPLES; i++) {
441 avg->samples[i] = sample; 441 avg->samples[i] = sample;
442 avg->time_stamps[i] = ts.tv_sec; 442 avg->time_stamps[i] = ts.tv_sec;
443 } 443 }
444 444
445 avg->pos = 0; 445 avg->pos = 0;
446 avg->nbr_samples = NBR_AVG_SAMPLES; 446 avg->nbr_samples = NBR_AVG_SAMPLES;
447 avg->sum = sample * NBR_AVG_SAMPLES; 447 avg->sum = sample * NBR_AVG_SAMPLES;
448 avg->avg = sample; 448 avg->avg = sample;
449 } 449 }
450 450
451 /** 451 /**
452 * ab8500_fg_coulomb_counter() - enable coulomb counter 452 * ab8500_fg_coulomb_counter() - enable coulomb counter
453 * @di: pointer to the ab8500_fg structure 453 * @di: pointer to the ab8500_fg structure
454 * @enable: enable/disable 454 * @enable: enable/disable
455 * 455 *
456 * Enable/Disable coulomb counter. 456 * Enable/Disable coulomb counter.
457 * On failure returns negative value. 457 * On failure returns negative value.
458 */ 458 */
459 static int ab8500_fg_coulomb_counter(struct ab8500_fg *di, bool enable) 459 static int ab8500_fg_coulomb_counter(struct ab8500_fg *di, bool enable)
460 { 460 {
461 int ret = 0; 461 int ret = 0;
462 mutex_lock(&di->cc_lock); 462 mutex_lock(&di->cc_lock);
463 if (enable) { 463 if (enable) {
464 /* To be able to reprogram the number of samples, we have to 464 /* To be able to reprogram the number of samples, we have to
465 * first stop the CC and then enable it again */ 465 * first stop the CC and then enable it again */
466 ret = abx500_set_register_interruptible(di->dev, AB8500_RTC, 466 ret = abx500_set_register_interruptible(di->dev, AB8500_RTC,
467 AB8500_RTC_CC_CONF_REG, 0x00); 467 AB8500_RTC_CC_CONF_REG, 0x00);
468 if (ret) 468 if (ret)
469 goto cc_err; 469 goto cc_err;
470 470
471 /* Program the samples */ 471 /* Program the samples */
472 ret = abx500_set_register_interruptible(di->dev, 472 ret = abx500_set_register_interruptible(di->dev,
473 AB8500_GAS_GAUGE, AB8500_GASG_CC_NCOV_ACCU, 473 AB8500_GAS_GAUGE, AB8500_GASG_CC_NCOV_ACCU,
474 di->fg_samples); 474 di->fg_samples);
475 if (ret) 475 if (ret)
476 goto cc_err; 476 goto cc_err;
477 477
478 /* Start the CC */ 478 /* Start the CC */
479 ret = abx500_set_register_interruptible(di->dev, AB8500_RTC, 479 ret = abx500_set_register_interruptible(di->dev, AB8500_RTC,
480 AB8500_RTC_CC_CONF_REG, 480 AB8500_RTC_CC_CONF_REG,
481 (CC_DEEP_SLEEP_ENA | CC_PWR_UP_ENA)); 481 (CC_DEEP_SLEEP_ENA | CC_PWR_UP_ENA));
482 if (ret) 482 if (ret)
483 goto cc_err; 483 goto cc_err;
484 484
485 di->flags.fg_enabled = true; 485 di->flags.fg_enabled = true;
486 } else { 486 } else {
487 /* Clear any pending read requests */ 487 /* Clear any pending read requests */
488 ret = abx500_set_register_interruptible(di->dev, 488 ret = abx500_set_register_interruptible(di->dev,
489 AB8500_GAS_GAUGE, AB8500_GASG_CC_CTRL_REG, 0); 489 AB8500_GAS_GAUGE, AB8500_GASG_CC_CTRL_REG, 0);
490 if (ret) 490 if (ret)
491 goto cc_err; 491 goto cc_err;
492 492
493 ret = abx500_set_register_interruptible(di->dev, 493 ret = abx500_set_register_interruptible(di->dev,
494 AB8500_GAS_GAUGE, AB8500_GASG_CC_NCOV_ACCU_CTRL, 0); 494 AB8500_GAS_GAUGE, AB8500_GASG_CC_NCOV_ACCU_CTRL, 0);
495 if (ret) 495 if (ret)
496 goto cc_err; 496 goto cc_err;
497 497
498 /* Stop the CC */ 498 /* Stop the CC */
499 ret = abx500_set_register_interruptible(di->dev, AB8500_RTC, 499 ret = abx500_set_register_interruptible(di->dev, AB8500_RTC,
500 AB8500_RTC_CC_CONF_REG, 0); 500 AB8500_RTC_CC_CONF_REG, 0);
501 if (ret) 501 if (ret)
502 goto cc_err; 502 goto cc_err;
503 503
504 di->flags.fg_enabled = false; 504 di->flags.fg_enabled = false;
505 505
506 } 506 }
507 dev_dbg(di->dev, " CC enabled: %d Samples: %d\n", 507 dev_dbg(di->dev, " CC enabled: %d Samples: %d\n",
508 enable, di->fg_samples); 508 enable, di->fg_samples);
509 509
510 mutex_unlock(&di->cc_lock); 510 mutex_unlock(&di->cc_lock);
511 511
512 return ret; 512 return ret;
513 cc_err: 513 cc_err:
514 dev_err(di->dev, "%s Enabling coulomb counter failed\n", __func__); 514 dev_err(di->dev, "%s Enabling coulomb counter failed\n", __func__);
515 mutex_unlock(&di->cc_lock); 515 mutex_unlock(&di->cc_lock);
516 return ret; 516 return ret;
517 } 517 }
518 518
519 /** 519 /**
520 * ab8500_fg_inst_curr_start() - start battery instantaneous current 520 * ab8500_fg_inst_curr_start() - start battery instantaneous current
521 * @di: pointer to the ab8500_fg structure 521 * @di: pointer to the ab8500_fg structure
522 * 522 *
523 * Returns 0 or error code 523 * Returns 0 or error code
524 * Note: This is part "one" and has to be called before 524 * Note: This is part "one" and has to be called before
525 * ab8500_fg_inst_curr_finalize() 525 * ab8500_fg_inst_curr_finalize()
526 */ 526 */
527 int ab8500_fg_inst_curr_start(struct ab8500_fg *di) 527 int ab8500_fg_inst_curr_start(struct ab8500_fg *di)
528 { 528 {
529 u8 reg_val; 529 u8 reg_val;
530 int ret; 530 int ret;
531 531
532 mutex_lock(&di->cc_lock); 532 mutex_lock(&di->cc_lock);
533 533
534 ret = abx500_get_register_interruptible(di->dev, AB8500_RTC, 534 ret = abx500_get_register_interruptible(di->dev, AB8500_RTC,
535 AB8500_RTC_CC_CONF_REG, &reg_val); 535 AB8500_RTC_CC_CONF_REG, &reg_val);
536 if (ret < 0) 536 if (ret < 0)
537 goto fail; 537 goto fail;
538 538
539 if (!(reg_val & CC_PWR_UP_ENA)) { 539 if (!(reg_val & CC_PWR_UP_ENA)) {
540 dev_dbg(di->dev, "%s Enable FG\n", __func__); 540 dev_dbg(di->dev, "%s Enable FG\n", __func__);
541 di->turn_off_fg = true; 541 di->turn_off_fg = true;
542 542
543 /* Program the samples */ 543 /* Program the samples */
544 ret = abx500_set_register_interruptible(di->dev, 544 ret = abx500_set_register_interruptible(di->dev,
545 AB8500_GAS_GAUGE, AB8500_GASG_CC_NCOV_ACCU, 545 AB8500_GAS_GAUGE, AB8500_GASG_CC_NCOV_ACCU,
546 SEC_TO_SAMPLE(10)); 546 SEC_TO_SAMPLE(10));
547 if (ret) 547 if (ret)
548 goto fail; 548 goto fail;
549 549
550 /* Start the CC */ 550 /* Start the CC */
551 ret = abx500_set_register_interruptible(di->dev, AB8500_RTC, 551 ret = abx500_set_register_interruptible(di->dev, AB8500_RTC,
552 AB8500_RTC_CC_CONF_REG, 552 AB8500_RTC_CC_CONF_REG,
553 (CC_DEEP_SLEEP_ENA | CC_PWR_UP_ENA)); 553 (CC_DEEP_SLEEP_ENA | CC_PWR_UP_ENA));
554 if (ret) 554 if (ret)
555 goto fail; 555 goto fail;
556 } else { 556 } else {
557 di->turn_off_fg = false; 557 di->turn_off_fg = false;
558 } 558 }
559 559
560 /* Return and WFI */ 560 /* Return and WFI */
561 INIT_COMPLETION(di->ab8500_fg_complete); 561 INIT_COMPLETION(di->ab8500_fg_complete);
562 enable_irq(di->irq); 562 enable_irq(di->irq);
563 563
564 /* Note: cc_lock is still locked */ 564 /* Note: cc_lock is still locked */
565 return 0; 565 return 0;
566 fail: 566 fail:
567 mutex_unlock(&di->cc_lock); 567 mutex_unlock(&di->cc_lock);
568 return ret; 568 return ret;
569 } 569 }
570 570
571 /** 571 /**
572 * ab8500_fg_inst_curr_done() - check if fg conversion is done 572 * ab8500_fg_inst_curr_done() - check if fg conversion is done
573 * @di: pointer to the ab8500_fg structure 573 * @di: pointer to the ab8500_fg structure
574 * 574 *
575 * Returns 1 if conversion done, 0 if still waiting 575 * Returns 1 if conversion done, 0 if still waiting
576 */ 576 */
577 int ab8500_fg_inst_curr_done(struct ab8500_fg *di) 577 int ab8500_fg_inst_curr_done(struct ab8500_fg *di)
578 { 578 {
579 return completion_done(&di->ab8500_fg_complete); 579 return completion_done(&di->ab8500_fg_complete);
580 } 580 }
581 581
582 /** 582 /**
583 * ab8500_fg_inst_curr_finalize() - battery instantaneous current 583 * ab8500_fg_inst_curr_finalize() - battery instantaneous current
584 * @di: pointer to the ab8500_fg structure 584 * @di: pointer to the ab8500_fg structure
585 * @res: battery instantenous current(on success) 585 * @res: battery instantenous current(on success)
586 * 586 *
587 * Returns 0 or an error code 587 * Returns 0 or an error code
588 * Note: This is part "two" and has to be called at earliest 250 ms 588 * Note: This is part "two" and has to be called at earliest 250 ms
589 * after ab8500_fg_inst_curr_start() 589 * after ab8500_fg_inst_curr_start()
590 */ 590 */
591 int ab8500_fg_inst_curr_finalize(struct ab8500_fg *di, int *res) 591 int ab8500_fg_inst_curr_finalize(struct ab8500_fg *di, int *res)
592 { 592 {
593 u8 low, high; 593 u8 low, high;
594 int val; 594 int val;
595 int ret; 595 int ret;
596 int timeout; 596 int timeout;
597 597
598 if (!completion_done(&di->ab8500_fg_complete)) { 598 if (!completion_done(&di->ab8500_fg_complete)) {
599 timeout = wait_for_completion_timeout(&di->ab8500_fg_complete, 599 timeout = wait_for_completion_timeout(&di->ab8500_fg_complete,
600 INS_CURR_TIMEOUT); 600 INS_CURR_TIMEOUT);
601 dev_dbg(di->dev, "Finalize time: %d ms\n", 601 dev_dbg(di->dev, "Finalize time: %d ms\n",
602 ((INS_CURR_TIMEOUT - timeout) * 1000) / HZ); 602 ((INS_CURR_TIMEOUT - timeout) * 1000) / HZ);
603 if (!timeout) { 603 if (!timeout) {
604 ret = -ETIME; 604 ret = -ETIME;
605 disable_irq(di->irq); 605 disable_irq(di->irq);
606 dev_err(di->dev, "completion timed out [%d]\n", 606 dev_err(di->dev, "completion timed out [%d]\n",
607 __LINE__); 607 __LINE__);
608 goto fail; 608 goto fail;
609 } 609 }
610 } 610 }
611 611
612 disable_irq(di->irq); 612 disable_irq(di->irq);
613 613
614 ret = abx500_mask_and_set_register_interruptible(di->dev, 614 ret = abx500_mask_and_set_register_interruptible(di->dev,
615 AB8500_GAS_GAUGE, AB8500_GASG_CC_CTRL_REG, 615 AB8500_GAS_GAUGE, AB8500_GASG_CC_CTRL_REG,
616 READ_REQ, READ_REQ); 616 READ_REQ, READ_REQ);
617 617
618 /* 100uS between read request and read is needed */ 618 /* 100uS between read request and read is needed */
619 usleep_range(100, 100); 619 usleep_range(100, 100);
620 620
621 /* Read CC Sample conversion value Low and high */ 621 /* Read CC Sample conversion value Low and high */
622 ret = abx500_get_register_interruptible(di->dev, AB8500_GAS_GAUGE, 622 ret = abx500_get_register_interruptible(di->dev, AB8500_GAS_GAUGE,
623 AB8500_GASG_CC_SMPL_CNVL_REG, &low); 623 AB8500_GASG_CC_SMPL_CNVL_REG, &low);
624 if (ret < 0) 624 if (ret < 0)
625 goto fail; 625 goto fail;
626 626
627 ret = abx500_get_register_interruptible(di->dev, AB8500_GAS_GAUGE, 627 ret = abx500_get_register_interruptible(di->dev, AB8500_GAS_GAUGE,
628 AB8500_GASG_CC_SMPL_CNVH_REG, &high); 628 AB8500_GASG_CC_SMPL_CNVH_REG, &high);
629 if (ret < 0) 629 if (ret < 0)
630 goto fail; 630 goto fail;
631 631
632 /* 632 /*
633 * negative value for Discharging 633 * negative value for Discharging
634 * convert 2's compliment into decimal 634 * convert 2's compliment into decimal
635 */ 635 */
636 if (high & 0x10) 636 if (high & 0x10)
637 val = (low | (high << 8) | 0xFFFFE000); 637 val = (low | (high << 8) | 0xFFFFE000);
638 else 638 else
639 val = (low | (high << 8)); 639 val = (low | (high << 8));
640 640
641 /* 641 /*
642 * Convert to unit value in mA 642 * Convert to unit value in mA
643 * Full scale input voltage is 643 * Full scale input voltage is
644 * 66.660mV => LSB = 66.660mV/(4096*res) = 1.627mA 644 * 66.660mV => LSB = 66.660mV/(4096*res) = 1.627mA
645 * Given a 250ms conversion cycle time the LSB corresponds 645 * Given a 250ms conversion cycle time the LSB corresponds
646 * to 112.9 nAh. Convert to current by dividing by the conversion 646 * to 112.9 nAh. Convert to current by dividing by the conversion
647 * time in hours (250ms = 1 / (3600 * 4)h) 647 * time in hours (250ms = 1 / (3600 * 4)h)
648 * 112.9nAh assumes 10mOhm, but fg_res is in 0.1mOhm 648 * 112.9nAh assumes 10mOhm, but fg_res is in 0.1mOhm
649 */ 649 */
650 val = (val * QLSB_NANO_AMP_HOURS_X10 * 36 * 4) / 650 val = (val * QLSB_NANO_AMP_HOURS_X10 * 36 * 4) /
651 (1000 * di->bat->fg_res); 651 (1000 * di->bat->fg_res);
652 652
653 if (di->turn_off_fg) { 653 if (di->turn_off_fg) {
654 dev_dbg(di->dev, "%s Disable FG\n", __func__); 654 dev_dbg(di->dev, "%s Disable FG\n", __func__);
655 655
656 /* Clear any pending read requests */ 656 /* Clear any pending read requests */
657 ret = abx500_set_register_interruptible(di->dev, 657 ret = abx500_set_register_interruptible(di->dev,
658 AB8500_GAS_GAUGE, AB8500_GASG_CC_CTRL_REG, 0); 658 AB8500_GAS_GAUGE, AB8500_GASG_CC_CTRL_REG, 0);
659 if (ret) 659 if (ret)
660 goto fail; 660 goto fail;
661 661
662 /* Stop the CC */ 662 /* Stop the CC */
663 ret = abx500_set_register_interruptible(di->dev, AB8500_RTC, 663 ret = abx500_set_register_interruptible(di->dev, AB8500_RTC,
664 AB8500_RTC_CC_CONF_REG, 0); 664 AB8500_RTC_CC_CONF_REG, 0);
665 if (ret) 665 if (ret)
666 goto fail; 666 goto fail;
667 } 667 }
668 mutex_unlock(&di->cc_lock); 668 mutex_unlock(&di->cc_lock);
669 (*res) = val; 669 (*res) = val;
670 670
671 return 0; 671 return 0;
672 fail: 672 fail:
673 mutex_unlock(&di->cc_lock); 673 mutex_unlock(&di->cc_lock);
674 return ret; 674 return ret;
675 } 675 }
676 676
677 /** 677 /**
678 * ab8500_fg_inst_curr_blocking() - battery instantaneous current 678 * ab8500_fg_inst_curr_blocking() - battery instantaneous current
679 * @di: pointer to the ab8500_fg structure 679 * @di: pointer to the ab8500_fg structure
680 * @res: battery instantenous current(on success) 680 * @res: battery instantenous current(on success)
681 * 681 *
682 * Returns 0 else error code 682 * Returns 0 else error code
683 */ 683 */
684 int ab8500_fg_inst_curr_blocking(struct ab8500_fg *di) 684 int ab8500_fg_inst_curr_blocking(struct ab8500_fg *di)
685 { 685 {
686 int ret; 686 int ret;
687 int res = 0; 687 int res = 0;
688 688
689 ret = ab8500_fg_inst_curr_start(di); 689 ret = ab8500_fg_inst_curr_start(di);
690 if (ret) { 690 if (ret) {
691 dev_err(di->dev, "Failed to initialize fg_inst\n"); 691 dev_err(di->dev, "Failed to initialize fg_inst\n");
692 return 0; 692 return 0;
693 } 693 }
694 694
695 ret = ab8500_fg_inst_curr_finalize(di, &res); 695 ret = ab8500_fg_inst_curr_finalize(di, &res);
696 if (ret) { 696 if (ret) {
697 dev_err(di->dev, "Failed to finalize fg_inst\n"); 697 dev_err(di->dev, "Failed to finalize fg_inst\n");
698 return 0; 698 return 0;
699 } 699 }
700 700
701 return res; 701 return res;
702 } 702 }
703 703
704 /** 704 /**
705 * ab8500_fg_acc_cur_work() - average battery current 705 * ab8500_fg_acc_cur_work() - average battery current
706 * @work: pointer to the work_struct structure 706 * @work: pointer to the work_struct structure
707 * 707 *
708 * Updated the average battery current obtained from the 708 * Updated the average battery current obtained from the
709 * coulomb counter. 709 * coulomb counter.
710 */ 710 */
711 static void ab8500_fg_acc_cur_work(struct work_struct *work) 711 static void ab8500_fg_acc_cur_work(struct work_struct *work)
712 { 712 {
713 int val; 713 int val;
714 int ret; 714 int ret;
715 u8 low, med, high; 715 u8 low, med, high;
716 716
717 struct ab8500_fg *di = container_of(work, 717 struct ab8500_fg *di = container_of(work,
718 struct ab8500_fg, fg_acc_cur_work); 718 struct ab8500_fg, fg_acc_cur_work);
719 719
720 mutex_lock(&di->cc_lock); 720 mutex_lock(&di->cc_lock);
721 ret = abx500_set_register_interruptible(di->dev, AB8500_GAS_GAUGE, 721 ret = abx500_set_register_interruptible(di->dev, AB8500_GAS_GAUGE,
722 AB8500_GASG_CC_NCOV_ACCU_CTRL, RD_NCONV_ACCU_REQ); 722 AB8500_GASG_CC_NCOV_ACCU_CTRL, RD_NCONV_ACCU_REQ);
723 if (ret) 723 if (ret)
724 goto exit; 724 goto exit;
725 725
726 ret = abx500_get_register_interruptible(di->dev, AB8500_GAS_GAUGE, 726 ret = abx500_get_register_interruptible(di->dev, AB8500_GAS_GAUGE,
727 AB8500_GASG_CC_NCOV_ACCU_LOW, &low); 727 AB8500_GASG_CC_NCOV_ACCU_LOW, &low);
728 if (ret < 0) 728 if (ret < 0)
729 goto exit; 729 goto exit;
730 730
731 ret = abx500_get_register_interruptible(di->dev, AB8500_GAS_GAUGE, 731 ret = abx500_get_register_interruptible(di->dev, AB8500_GAS_GAUGE,
732 AB8500_GASG_CC_NCOV_ACCU_MED, &med); 732 AB8500_GASG_CC_NCOV_ACCU_MED, &med);
733 if (ret < 0) 733 if (ret < 0)
734 goto exit; 734 goto exit;
735 735
736 ret = abx500_get_register_interruptible(di->dev, AB8500_GAS_GAUGE, 736 ret = abx500_get_register_interruptible(di->dev, AB8500_GAS_GAUGE,
737 AB8500_GASG_CC_NCOV_ACCU_HIGH, &high); 737 AB8500_GASG_CC_NCOV_ACCU_HIGH, &high);
738 if (ret < 0) 738 if (ret < 0)
739 goto exit; 739 goto exit;
740 740
741 /* Check for sign bit in case of negative value, 2's compliment */ 741 /* Check for sign bit in case of negative value, 2's compliment */
742 if (high & 0x10) 742 if (high & 0x10)
743 val = (low | (med << 8) | (high << 16) | 0xFFE00000); 743 val = (low | (med << 8) | (high << 16) | 0xFFE00000);
744 else 744 else
745 val = (low | (med << 8) | (high << 16)); 745 val = (low | (med << 8) | (high << 16));
746 746
747 /* 747 /*
748 * Convert to uAh 748 * Convert to uAh
749 * Given a 250ms conversion cycle time the LSB corresponds 749 * Given a 250ms conversion cycle time the LSB corresponds
750 * to 112.9 nAh. 750 * to 112.9 nAh.
751 * 112.9nAh assumes 10mOhm, but fg_res is in 0.1mOhm 751 * 112.9nAh assumes 10mOhm, but fg_res is in 0.1mOhm
752 */ 752 */
753 di->accu_charge = (val * QLSB_NANO_AMP_HOURS_X10) / 753 di->accu_charge = (val * QLSB_NANO_AMP_HOURS_X10) /
754 (100 * di->bat->fg_res); 754 (100 * di->bat->fg_res);
755 755
756 /* 756 /*
757 * Convert to unit value in mA 757 * Convert to unit value in mA
758 * Full scale input voltage is 758 * Full scale input voltage is
759 * 66.660mV => LSB = 66.660mV/(4096*res) = 1.627mA 759 * 66.660mV => LSB = 66.660mV/(4096*res) = 1.627mA
760 * Given a 250ms conversion cycle time the LSB corresponds 760 * Given a 250ms conversion cycle time the LSB corresponds
761 * to 112.9 nAh. Convert to current by dividing by the conversion 761 * to 112.9 nAh. Convert to current by dividing by the conversion
762 * time in hours (= samples / (3600 * 4)h) 762 * time in hours (= samples / (3600 * 4)h)
763 * 112.9nAh assumes 10mOhm, but fg_res is in 0.1mOhm 763 * 112.9nAh assumes 10mOhm, but fg_res is in 0.1mOhm
764 */ 764 */
765 di->avg_curr = (val * QLSB_NANO_AMP_HOURS_X10 * 36) / 765 di->avg_curr = (val * QLSB_NANO_AMP_HOURS_X10 * 36) /
766 (1000 * di->bat->fg_res * (di->fg_samples / 4)); 766 (1000 * di->bat->fg_res * (di->fg_samples / 4));
767 767
768 di->flags.conv_done = true; 768 di->flags.conv_done = true;
769 769
770 mutex_unlock(&di->cc_lock); 770 mutex_unlock(&di->cc_lock);
771 771
772 queue_work(di->fg_wq, &di->fg_work); 772 queue_work(di->fg_wq, &di->fg_work);
773 773
774 return; 774 return;
775 exit: 775 exit:
776 dev_err(di->dev, 776 dev_err(di->dev,
777 "Failed to read or write gas gauge registers\n"); 777 "Failed to read or write gas gauge registers\n");
778 mutex_unlock(&di->cc_lock); 778 mutex_unlock(&di->cc_lock);
779 queue_work(di->fg_wq, &di->fg_work); 779 queue_work(di->fg_wq, &di->fg_work);
780 } 780 }
781 781
782 /** 782 /**
783 * ab8500_fg_bat_voltage() - get battery voltage 783 * ab8500_fg_bat_voltage() - get battery voltage
784 * @di: pointer to the ab8500_fg structure 784 * @di: pointer to the ab8500_fg structure
785 * 785 *
786 * Returns battery voltage(on success) else error code 786 * Returns battery voltage(on success) else error code
787 */ 787 */
788 static int ab8500_fg_bat_voltage(struct ab8500_fg *di) 788 static int ab8500_fg_bat_voltage(struct ab8500_fg *di)
789 { 789 {
790 int vbat; 790 int vbat;
791 static int prev; 791 static int prev;
792 792
793 vbat = ab8500_gpadc_convert(di->gpadc, MAIN_BAT_V); 793 vbat = ab8500_gpadc_convert(di->gpadc, MAIN_BAT_V);
794 if (vbat < 0) { 794 if (vbat < 0) {
795 dev_err(di->dev, 795 dev_err(di->dev,
796 "%s gpadc conversion failed, using previous value\n", 796 "%s gpadc conversion failed, using previous value\n",
797 __func__); 797 __func__);
798 return prev; 798 return prev;
799 } 799 }
800 800
801 prev = vbat; 801 prev = vbat;
802 return vbat; 802 return vbat;
803 } 803 }
804 804
805 /** 805 /**
806 * ab8500_fg_volt_to_capacity() - Voltage based capacity 806 * ab8500_fg_volt_to_capacity() - Voltage based capacity
807 * @di: pointer to the ab8500_fg structure 807 * @di: pointer to the ab8500_fg structure
808 * @voltage: The voltage to convert to a capacity 808 * @voltage: The voltage to convert to a capacity
809 * 809 *
810 * Returns battery capacity in per mille based on voltage 810 * Returns battery capacity in per mille based on voltage
811 */ 811 */
812 static int ab8500_fg_volt_to_capacity(struct ab8500_fg *di, int voltage) 812 static int ab8500_fg_volt_to_capacity(struct ab8500_fg *di, int voltage)
813 { 813 {
814 int i, tbl_size; 814 int i, tbl_size;
815 struct abx500_v_to_cap *tbl; 815 struct abx500_v_to_cap *tbl;
816 int cap = 0; 816 int cap = 0;
817 817
818 tbl = di->bat->bat_type[di->bat->batt_id].v_to_cap_tbl, 818 tbl = di->bat->bat_type[di->bat->batt_id].v_to_cap_tbl,
819 tbl_size = di->bat->bat_type[di->bat->batt_id].n_v_cap_tbl_elements; 819 tbl_size = di->bat->bat_type[di->bat->batt_id].n_v_cap_tbl_elements;
820 820
821 for (i = 0; i < tbl_size; ++i) { 821 for (i = 0; i < tbl_size; ++i) {
822 if (voltage > tbl[i].voltage) 822 if (voltage > tbl[i].voltage)
823 break; 823 break;
824 } 824 }
825 825
826 if ((i > 0) && (i < tbl_size)) { 826 if ((i > 0) && (i < tbl_size)) {
827 cap = interpolate(voltage, 827 cap = interpolate(voltage,
828 tbl[i].voltage, 828 tbl[i].voltage,
829 tbl[i].capacity * 10, 829 tbl[i].capacity * 10,
830 tbl[i-1].voltage, 830 tbl[i-1].voltage,
831 tbl[i-1].capacity * 10); 831 tbl[i-1].capacity * 10);
832 } else if (i == 0) { 832 } else if (i == 0) {
833 cap = 1000; 833 cap = 1000;
834 } else { 834 } else {
835 cap = 0; 835 cap = 0;
836 } 836 }
837 837
838 dev_dbg(di->dev, "%s Vbat: %d, Cap: %d per mille", 838 dev_dbg(di->dev, "%s Vbat: %d, Cap: %d per mille",
839 __func__, voltage, cap); 839 __func__, voltage, cap);
840 840
841 return cap; 841 return cap;
842 } 842 }
843 843
844 /** 844 /**
845 * ab8500_fg_uncomp_volt_to_capacity() - Uncompensated voltage based capacity 845 * ab8500_fg_uncomp_volt_to_capacity() - Uncompensated voltage based capacity
846 * @di: pointer to the ab8500_fg structure 846 * @di: pointer to the ab8500_fg structure
847 * 847 *
848 * Returns battery capacity based on battery voltage that is not compensated 848 * Returns battery capacity based on battery voltage that is not compensated
849 * for the voltage drop due to the load 849 * for the voltage drop due to the load
850 */ 850 */
851 static int ab8500_fg_uncomp_volt_to_capacity(struct ab8500_fg *di) 851 static int ab8500_fg_uncomp_volt_to_capacity(struct ab8500_fg *di)
852 { 852 {
853 di->vbat = ab8500_fg_bat_voltage(di); 853 di->vbat = ab8500_fg_bat_voltage(di);
854 return ab8500_fg_volt_to_capacity(di, di->vbat); 854 return ab8500_fg_volt_to_capacity(di, di->vbat);
855 } 855 }
856 856
857 /** 857 /**
858 * ab8500_fg_battery_resistance() - Returns the battery inner resistance 858 * ab8500_fg_battery_resistance() - Returns the battery inner resistance
859 * @di: pointer to the ab8500_fg structure 859 * @di: pointer to the ab8500_fg structure
860 * 860 *
861 * Returns battery inner resistance added with the fuel gauge resistor value 861 * Returns battery inner resistance added with the fuel gauge resistor value
862 * to get the total resistance in the whole link from gnd to bat+ node. 862 * to get the total resistance in the whole link from gnd to bat+ node.
863 */ 863 */
864 static int ab8500_fg_battery_resistance(struct ab8500_fg *di) 864 static int ab8500_fg_battery_resistance(struct ab8500_fg *di)
865 { 865 {
866 int i, tbl_size; 866 int i, tbl_size;
867 struct batres_vs_temp *tbl; 867 struct batres_vs_temp *tbl;
868 int resist = 0; 868 int resist = 0;
869 869
870 tbl = di->bat->bat_type[di->bat->batt_id].batres_tbl; 870 tbl = di->bat->bat_type[di->bat->batt_id].batres_tbl;
871 tbl_size = di->bat->bat_type[di->bat->batt_id].n_batres_tbl_elements; 871 tbl_size = di->bat->bat_type[di->bat->batt_id].n_batres_tbl_elements;
872 872
873 for (i = 0; i < tbl_size; ++i) { 873 for (i = 0; i < tbl_size; ++i) {
874 if (di->bat_temp / 10 > tbl[i].temp) 874 if (di->bat_temp / 10 > tbl[i].temp)
875 break; 875 break;
876 } 876 }
877 877
878 if ((i > 0) && (i < tbl_size)) { 878 if ((i > 0) && (i < tbl_size)) {
879 resist = interpolate(di->bat_temp / 10, 879 resist = interpolate(di->bat_temp / 10,
880 tbl[i].temp, 880 tbl[i].temp,
881 tbl[i].resist, 881 tbl[i].resist,
882 tbl[i-1].temp, 882 tbl[i-1].temp,
883 tbl[i-1].resist); 883 tbl[i-1].resist);
884 } else if (i == 0) { 884 } else if (i == 0) {
885 resist = tbl[0].resist; 885 resist = tbl[0].resist;
886 } else { 886 } else {
887 resist = tbl[tbl_size - 1].resist; 887 resist = tbl[tbl_size - 1].resist;
888 } 888 }
889 889
890 dev_dbg(di->dev, "%s Temp: %d battery internal resistance: %d" 890 dev_dbg(di->dev, "%s Temp: %d battery internal resistance: %d"
891 " fg resistance %d, total: %d (mOhm)\n", 891 " fg resistance %d, total: %d (mOhm)\n",
892 __func__, di->bat_temp, resist, di->bat->fg_res / 10, 892 __func__, di->bat_temp, resist, di->bat->fg_res / 10,
893 (di->bat->fg_res / 10) + resist); 893 (di->bat->fg_res / 10) + resist);
894 894
895 /* fg_res variable is in 0.1mOhm */ 895 /* fg_res variable is in 0.1mOhm */
896 resist += di->bat->fg_res / 10; 896 resist += di->bat->fg_res / 10;
897 897
898 return resist; 898 return resist;
899 } 899 }
900 900
901 /** 901 /**
902 * ab8500_fg_load_comp_volt_to_capacity() - Load compensated voltage based capacity 902 * ab8500_fg_load_comp_volt_to_capacity() - Load compensated voltage based capacity
903 * @di: pointer to the ab8500_fg structure 903 * @di: pointer to the ab8500_fg structure
904 * 904 *
905 * Returns battery capacity based on battery voltage that is load compensated 905 * Returns battery capacity based on battery voltage that is load compensated
906 * for the voltage drop 906 * for the voltage drop
907 */ 907 */
908 static int ab8500_fg_load_comp_volt_to_capacity(struct ab8500_fg *di) 908 static int ab8500_fg_load_comp_volt_to_capacity(struct ab8500_fg *di)
909 { 909 {
910 int vbat_comp, res; 910 int vbat_comp, res;
911 int i = 0; 911 int i = 0;
912 int vbat = 0; 912 int vbat = 0;
913 913
914 ab8500_fg_inst_curr_start(di); 914 ab8500_fg_inst_curr_start(di);
915 915
916 do { 916 do {
917 vbat += ab8500_fg_bat_voltage(di); 917 vbat += ab8500_fg_bat_voltage(di);
918 i++; 918 i++;
919 msleep(5); 919 msleep(5);
920 } while (!ab8500_fg_inst_curr_done(di)); 920 } while (!ab8500_fg_inst_curr_done(di));
921 921
922 ab8500_fg_inst_curr_finalize(di, &di->inst_curr); 922 ab8500_fg_inst_curr_finalize(di, &di->inst_curr);
923 923
924 di->vbat = vbat / i; 924 di->vbat = vbat / i;
925 res = ab8500_fg_battery_resistance(di); 925 res = ab8500_fg_battery_resistance(di);
926 926
927 /* Use Ohms law to get the load compensated voltage */ 927 /* Use Ohms law to get the load compensated voltage */
928 vbat_comp = di->vbat - (di->inst_curr * res) / 1000; 928 vbat_comp = di->vbat - (di->inst_curr * res) / 1000;
929 929
930 dev_dbg(di->dev, "%s Measured Vbat: %dmV,Compensated Vbat %dmV, " 930 dev_dbg(di->dev, "%s Measured Vbat: %dmV,Compensated Vbat %dmV, "
931 "R: %dmOhm, Current: %dmA Vbat Samples: %d\n", 931 "R: %dmOhm, Current: %dmA Vbat Samples: %d\n",
932 __func__, di->vbat, vbat_comp, res, di->inst_curr, i); 932 __func__, di->vbat, vbat_comp, res, di->inst_curr, i);
933 933
934 return ab8500_fg_volt_to_capacity(di, vbat_comp); 934 return ab8500_fg_volt_to_capacity(di, vbat_comp);
935 } 935 }
936 936
937 /** 937 /**
938 * ab8500_fg_convert_mah_to_permille() - Capacity in mAh to permille 938 * ab8500_fg_convert_mah_to_permille() - Capacity in mAh to permille
939 * @di: pointer to the ab8500_fg structure 939 * @di: pointer to the ab8500_fg structure
940 * @cap_mah: capacity in mAh 940 * @cap_mah: capacity in mAh
941 * 941 *
942 * Converts capacity in mAh to capacity in permille 942 * Converts capacity in mAh to capacity in permille
943 */ 943 */
944 static int ab8500_fg_convert_mah_to_permille(struct ab8500_fg *di, int cap_mah) 944 static int ab8500_fg_convert_mah_to_permille(struct ab8500_fg *di, int cap_mah)
945 { 945 {
946 return (cap_mah * 1000) / di->bat_cap.max_mah_design; 946 return (cap_mah * 1000) / di->bat_cap.max_mah_design;
947 } 947 }
948 948
949 /** 949 /**
950 * ab8500_fg_convert_permille_to_mah() - Capacity in permille to mAh 950 * ab8500_fg_convert_permille_to_mah() - Capacity in permille to mAh
951 * @di: pointer to the ab8500_fg structure 951 * @di: pointer to the ab8500_fg structure
952 * @cap_pm: capacity in permille 952 * @cap_pm: capacity in permille
953 * 953 *
954 * Converts capacity in permille to capacity in mAh 954 * Converts capacity in permille to capacity in mAh
955 */ 955 */
956 static int ab8500_fg_convert_permille_to_mah(struct ab8500_fg *di, int cap_pm) 956 static int ab8500_fg_convert_permille_to_mah(struct ab8500_fg *di, int cap_pm)
957 { 957 {
958 return cap_pm * di->bat_cap.max_mah_design / 1000; 958 return cap_pm * di->bat_cap.max_mah_design / 1000;
959 } 959 }
960 960
961 /** 961 /**
962 * ab8500_fg_convert_mah_to_uwh() - Capacity in mAh to uWh 962 * ab8500_fg_convert_mah_to_uwh() - Capacity in mAh to uWh
963 * @di: pointer to the ab8500_fg structure 963 * @di: pointer to the ab8500_fg structure
964 * @cap_mah: capacity in mAh 964 * @cap_mah: capacity in mAh
965 * 965 *
966 * Converts capacity in mAh to capacity in uWh 966 * Converts capacity in mAh to capacity in uWh
967 */ 967 */
968 static int ab8500_fg_convert_mah_to_uwh(struct ab8500_fg *di, int cap_mah) 968 static int ab8500_fg_convert_mah_to_uwh(struct ab8500_fg *di, int cap_mah)
969 { 969 {
970 u64 div_res; 970 u64 div_res;
971 u32 div_rem; 971 u32 div_rem;
972 972
973 div_res = ((u64) cap_mah) * ((u64) di->vbat_nom); 973 div_res = ((u64) cap_mah) * ((u64) di->vbat_nom);
974 div_rem = do_div(div_res, 1000); 974 div_rem = do_div(div_res, 1000);
975 975
976 /* Make sure to round upwards if necessary */ 976 /* Make sure to round upwards if necessary */
977 if (div_rem >= 1000 / 2) 977 if (div_rem >= 1000 / 2)
978 div_res++; 978 div_res++;
979 979
980 return (int) div_res; 980 return (int) div_res;
981 } 981 }
982 982
983 /** 983 /**
984 * ab8500_fg_calc_cap_charging() - Calculate remaining capacity while charging 984 * ab8500_fg_calc_cap_charging() - Calculate remaining capacity while charging
985 * @di: pointer to the ab8500_fg structure 985 * @di: pointer to the ab8500_fg structure
986 * 986 *
987 * Return the capacity in mAh based on previous calculated capcity and the FG 987 * Return the capacity in mAh based on previous calculated capcity and the FG
988 * accumulator register value. The filter is filled with this capacity 988 * accumulator register value. The filter is filled with this capacity
989 */ 989 */
990 static int ab8500_fg_calc_cap_charging(struct ab8500_fg *di) 990 static int ab8500_fg_calc_cap_charging(struct ab8500_fg *di)
991 { 991 {
992 dev_dbg(di->dev, "%s cap_mah %d accu_charge %d\n", 992 dev_dbg(di->dev, "%s cap_mah %d accu_charge %d\n",
993 __func__, 993 __func__,
994 di->bat_cap.mah, 994 di->bat_cap.mah,
995 di->accu_charge); 995 di->accu_charge);
996 996
997 /* Capacity should not be less than 0 */ 997 /* Capacity should not be less than 0 */
998 if (di->bat_cap.mah + di->accu_charge > 0) 998 if (di->bat_cap.mah + di->accu_charge > 0)
999 di->bat_cap.mah += di->accu_charge; 999 di->bat_cap.mah += di->accu_charge;
1000 else 1000 else
1001 di->bat_cap.mah = 0; 1001 di->bat_cap.mah = 0;
1002 /* 1002 /*
1003 * We force capacity to 100% once when the algorithm 1003 * We force capacity to 100% once when the algorithm
1004 * reports that it's full. 1004 * reports that it's full.
1005 */ 1005 */
1006 if (di->bat_cap.mah >= di->bat_cap.max_mah_design || 1006 if (di->bat_cap.mah >= di->bat_cap.max_mah_design ||
1007 di->flags.force_full) { 1007 di->flags.force_full) {
1008 di->bat_cap.mah = di->bat_cap.max_mah_design; 1008 di->bat_cap.mah = di->bat_cap.max_mah_design;
1009 } 1009 }
1010 1010
1011 ab8500_fg_fill_cap_sample(di, di->bat_cap.mah); 1011 ab8500_fg_fill_cap_sample(di, di->bat_cap.mah);
1012 di->bat_cap.permille = 1012 di->bat_cap.permille =
1013 ab8500_fg_convert_mah_to_permille(di, di->bat_cap.mah); 1013 ab8500_fg_convert_mah_to_permille(di, di->bat_cap.mah);
1014 1014
1015 /* We need to update battery voltage and inst current when charging */ 1015 /* We need to update battery voltage and inst current when charging */
1016 di->vbat = ab8500_fg_bat_voltage(di); 1016 di->vbat = ab8500_fg_bat_voltage(di);
1017 di->inst_curr = ab8500_fg_inst_curr_blocking(di); 1017 di->inst_curr = ab8500_fg_inst_curr_blocking(di);
1018 1018
1019 return di->bat_cap.mah; 1019 return di->bat_cap.mah;
1020 } 1020 }
1021 1021
1022 /** 1022 /**
1023 * ab8500_fg_calc_cap_discharge_voltage() - Capacity in discharge with voltage 1023 * ab8500_fg_calc_cap_discharge_voltage() - Capacity in discharge with voltage
1024 * @di: pointer to the ab8500_fg structure 1024 * @di: pointer to the ab8500_fg structure
1025 * @comp: if voltage should be load compensated before capacity calc 1025 * @comp: if voltage should be load compensated before capacity calc
1026 * 1026 *
1027 * Return the capacity in mAh based on the battery voltage. The voltage can 1027 * Return the capacity in mAh based on the battery voltage. The voltage can
1028 * either be load compensated or not. This value is added to the filter and a 1028 * either be load compensated or not. This value is added to the filter and a
1029 * new mean value is calculated and returned. 1029 * new mean value is calculated and returned.
1030 */ 1030 */
1031 static int ab8500_fg_calc_cap_discharge_voltage(struct ab8500_fg *di, bool comp) 1031 static int ab8500_fg_calc_cap_discharge_voltage(struct ab8500_fg *di, bool comp)
1032 { 1032 {
1033 int permille, mah; 1033 int permille, mah;
1034 1034
1035 if (comp) 1035 if (comp)
1036 permille = ab8500_fg_load_comp_volt_to_capacity(di); 1036 permille = ab8500_fg_load_comp_volt_to_capacity(di);
1037 else 1037 else
1038 permille = ab8500_fg_uncomp_volt_to_capacity(di); 1038 permille = ab8500_fg_uncomp_volt_to_capacity(di);
1039 1039
1040 mah = ab8500_fg_convert_permille_to_mah(di, permille); 1040 mah = ab8500_fg_convert_permille_to_mah(di, permille);
1041 1041
1042 di->bat_cap.mah = ab8500_fg_add_cap_sample(di, mah); 1042 di->bat_cap.mah = ab8500_fg_add_cap_sample(di, mah);
1043 di->bat_cap.permille = 1043 di->bat_cap.permille =
1044 ab8500_fg_convert_mah_to_permille(di, di->bat_cap.mah); 1044 ab8500_fg_convert_mah_to_permille(di, di->bat_cap.mah);
1045 1045
1046 return di->bat_cap.mah; 1046 return di->bat_cap.mah;
1047 } 1047 }
1048 1048
1049 /** 1049 /**
1050 * ab8500_fg_calc_cap_discharge_fg() - Capacity in discharge with FG 1050 * ab8500_fg_calc_cap_discharge_fg() - Capacity in discharge with FG
1051 * @di: pointer to the ab8500_fg structure 1051 * @di: pointer to the ab8500_fg structure
1052 * 1052 *
1053 * Return the capacity in mAh based on previous calculated capcity and the FG 1053 * Return the capacity in mAh based on previous calculated capcity and the FG
1054 * accumulator register value. This value is added to the filter and a 1054 * accumulator register value. This value is added to the filter and a
1055 * new mean value is calculated and returned. 1055 * new mean value is calculated and returned.
1056 */ 1056 */
1057 static int ab8500_fg_calc_cap_discharge_fg(struct ab8500_fg *di) 1057 static int ab8500_fg_calc_cap_discharge_fg(struct ab8500_fg *di)
1058 { 1058 {
1059 int permille_volt, permille; 1059 int permille_volt, permille;
1060 1060
1061 dev_dbg(di->dev, "%s cap_mah %d accu_charge %d\n", 1061 dev_dbg(di->dev, "%s cap_mah %d accu_charge %d\n",
1062 __func__, 1062 __func__,
1063 di->bat_cap.mah, 1063 di->bat_cap.mah,
1064 di->accu_charge); 1064 di->accu_charge);
1065 1065
1066 /* Capacity should not be less than 0 */ 1066 /* Capacity should not be less than 0 */
1067 if (di->bat_cap.mah + di->accu_charge > 0) 1067 if (di->bat_cap.mah + di->accu_charge > 0)
1068 di->bat_cap.mah += di->accu_charge; 1068 di->bat_cap.mah += di->accu_charge;
1069 else 1069 else
1070 di->bat_cap.mah = 0; 1070 di->bat_cap.mah = 0;
1071 1071
1072 if (di->bat_cap.mah >= di->bat_cap.max_mah_design) 1072 if (di->bat_cap.mah >= di->bat_cap.max_mah_design)
1073 di->bat_cap.mah = di->bat_cap.max_mah_design; 1073 di->bat_cap.mah = di->bat_cap.max_mah_design;
1074 1074
1075 /* 1075 /*
1076 * Check against voltage based capacity. It can not be lower 1076 * Check against voltage based capacity. It can not be lower
1077 * than what the uncompensated voltage says 1077 * than what the uncompensated voltage says
1078 */ 1078 */
1079 permille = ab8500_fg_convert_mah_to_permille(di, di->bat_cap.mah); 1079 permille = ab8500_fg_convert_mah_to_permille(di, di->bat_cap.mah);
1080 permille_volt = ab8500_fg_uncomp_volt_to_capacity(di); 1080 permille_volt = ab8500_fg_uncomp_volt_to_capacity(di);
1081 1081
1082 if (permille < permille_volt) { 1082 if (permille < permille_volt) {
1083 di->bat_cap.permille = permille_volt; 1083 di->bat_cap.permille = permille_volt;
1084 di->bat_cap.mah = ab8500_fg_convert_permille_to_mah(di, 1084 di->bat_cap.mah = ab8500_fg_convert_permille_to_mah(di,
1085 di->bat_cap.permille); 1085 di->bat_cap.permille);
1086 1086
1087 dev_dbg(di->dev, "%s voltage based: perm %d perm_volt %d\n", 1087 dev_dbg(di->dev, "%s voltage based: perm %d perm_volt %d\n",
1088 __func__, 1088 __func__,
1089 permille, 1089 permille,
1090 permille_volt); 1090 permille_volt);
1091 1091
1092 ab8500_fg_fill_cap_sample(di, di->bat_cap.mah); 1092 ab8500_fg_fill_cap_sample(di, di->bat_cap.mah);
1093 } else { 1093 } else {
1094 ab8500_fg_fill_cap_sample(di, di->bat_cap.mah); 1094 ab8500_fg_fill_cap_sample(di, di->bat_cap.mah);
1095 di->bat_cap.permille = 1095 di->bat_cap.permille =
1096 ab8500_fg_convert_mah_to_permille(di, di->bat_cap.mah); 1096 ab8500_fg_convert_mah_to_permille(di, di->bat_cap.mah);
1097 } 1097 }
1098 1098
1099 return di->bat_cap.mah; 1099 return di->bat_cap.mah;
1100 } 1100 }
1101 1101
1102 /** 1102 /**
1103 * ab8500_fg_capacity_level() - Get the battery capacity level 1103 * ab8500_fg_capacity_level() - Get the battery capacity level
1104 * @di: pointer to the ab8500_fg structure 1104 * @di: pointer to the ab8500_fg structure
1105 * 1105 *
1106 * Get the battery capacity level based on the capacity in percent 1106 * Get the battery capacity level based on the capacity in percent
1107 */ 1107 */
1108 static int ab8500_fg_capacity_level(struct ab8500_fg *di) 1108 static int ab8500_fg_capacity_level(struct ab8500_fg *di)
1109 { 1109 {
1110 int ret, percent; 1110 int ret, percent;
1111 1111
1112 percent = di->bat_cap.permille / 10; 1112 percent = di->bat_cap.permille / 10;
1113 1113
1114 if (percent <= di->bat->cap_levels->critical || 1114 if (percent <= di->bat->cap_levels->critical ||
1115 di->flags.low_bat) 1115 di->flags.low_bat)
1116 ret = POWER_SUPPLY_CAPACITY_LEVEL_CRITICAL; 1116 ret = POWER_SUPPLY_CAPACITY_LEVEL_CRITICAL;
1117 else if (percent <= di->bat->cap_levels->low) 1117 else if (percent <= di->bat->cap_levels->low)
1118 ret = POWER_SUPPLY_CAPACITY_LEVEL_LOW; 1118 ret = POWER_SUPPLY_CAPACITY_LEVEL_LOW;
1119 else if (percent <= di->bat->cap_levels->normal) 1119 else if (percent <= di->bat->cap_levels->normal)
1120 ret = POWER_SUPPLY_CAPACITY_LEVEL_NORMAL; 1120 ret = POWER_SUPPLY_CAPACITY_LEVEL_NORMAL;
1121 else if (percent <= di->bat->cap_levels->high) 1121 else if (percent <= di->bat->cap_levels->high)
1122 ret = POWER_SUPPLY_CAPACITY_LEVEL_HIGH; 1122 ret = POWER_SUPPLY_CAPACITY_LEVEL_HIGH;
1123 else 1123 else
1124 ret = POWER_SUPPLY_CAPACITY_LEVEL_FULL; 1124 ret = POWER_SUPPLY_CAPACITY_LEVEL_FULL;
1125 1125
1126 return ret; 1126 return ret;
1127 } 1127 }
1128 1128
1129 /** 1129 /**
1130 * ab8500_fg_check_capacity_limits() - Check if capacity has changed 1130 * ab8500_fg_check_capacity_limits() - Check if capacity has changed
1131 * @di: pointer to the ab8500_fg structure 1131 * @di: pointer to the ab8500_fg structure
1132 * @init: capacity is allowed to go up in init mode 1132 * @init: capacity is allowed to go up in init mode
1133 * 1133 *
1134 * Check if capacity or capacity limit has changed and notify the system 1134 * Check if capacity or capacity limit has changed and notify the system
1135 * about it using the power_supply framework 1135 * about it using the power_supply framework
1136 */ 1136 */
1137 static void ab8500_fg_check_capacity_limits(struct ab8500_fg *di, bool init) 1137 static void ab8500_fg_check_capacity_limits(struct ab8500_fg *di, bool init)
1138 { 1138 {
1139 bool changed = false; 1139 bool changed = false;
1140 1140
1141 di->bat_cap.level = ab8500_fg_capacity_level(di); 1141 di->bat_cap.level = ab8500_fg_capacity_level(di);
1142 1142
1143 if (di->bat_cap.level != di->bat_cap.prev_level) { 1143 if (di->bat_cap.level != di->bat_cap.prev_level) {
1144 /* 1144 /*
1145 * We do not allow reported capacity level to go up 1145 * We do not allow reported capacity level to go up
1146 * unless we're charging or if we're in init 1146 * unless we're charging or if we're in init
1147 */ 1147 */
1148 if (!(!di->flags.charging && di->bat_cap.level > 1148 if (!(!di->flags.charging && di->bat_cap.level >
1149 di->bat_cap.prev_level) || init) { 1149 di->bat_cap.prev_level) || init) {
1150 dev_dbg(di->dev, "level changed from %d to %d\n", 1150 dev_dbg(di->dev, "level changed from %d to %d\n",
1151 di->bat_cap.prev_level, 1151 di->bat_cap.prev_level,
1152 di->bat_cap.level); 1152 di->bat_cap.level);
1153 di->bat_cap.prev_level = di->bat_cap.level; 1153 di->bat_cap.prev_level = di->bat_cap.level;
1154 changed = true; 1154 changed = true;
1155 } else { 1155 } else {
1156 dev_dbg(di->dev, "level not allowed to go up " 1156 dev_dbg(di->dev, "level not allowed to go up "
1157 "since no charger is connected: %d to %d\n", 1157 "since no charger is connected: %d to %d\n",
1158 di->bat_cap.prev_level, 1158 di->bat_cap.prev_level,
1159 di->bat_cap.level); 1159 di->bat_cap.level);
1160 } 1160 }
1161 } 1161 }
1162 1162
1163 /* 1163 /*
1164 * If we have received the LOW_BAT IRQ, set capacity to 0 to initiate 1164 * If we have received the LOW_BAT IRQ, set capacity to 0 to initiate
1165 * shutdown 1165 * shutdown
1166 */ 1166 */
1167 if (di->flags.low_bat) { 1167 if (di->flags.low_bat) {
1168 dev_dbg(di->dev, "Battery low, set capacity to 0\n"); 1168 dev_dbg(di->dev, "Battery low, set capacity to 0\n");
1169 di->bat_cap.prev_percent = 0; 1169 di->bat_cap.prev_percent = 0;
1170 di->bat_cap.permille = 0; 1170 di->bat_cap.permille = 0;
1171 di->bat_cap.prev_mah = 0; 1171 di->bat_cap.prev_mah = 0;
1172 di->bat_cap.mah = 0; 1172 di->bat_cap.mah = 0;
1173 changed = true; 1173 changed = true;
1174 } else if (di->flags.fully_charged) { 1174 } else if (di->flags.fully_charged) {
1175 /* 1175 /*
1176 * We report 100% if algorithm reported fully charged 1176 * We report 100% if algorithm reported fully charged
1177 * unless capacity drops too much 1177 * unless capacity drops too much
1178 */ 1178 */
1179 if (di->flags.force_full) { 1179 if (di->flags.force_full) {
1180 di->bat_cap.prev_percent = di->bat_cap.permille / 10; 1180 di->bat_cap.prev_percent = di->bat_cap.permille / 10;
1181 di->bat_cap.prev_mah = di->bat_cap.mah; 1181 di->bat_cap.prev_mah = di->bat_cap.mah;
1182 } else if (!di->flags.force_full && 1182 } else if (!di->flags.force_full &&
1183 di->bat_cap.prev_percent != 1183 di->bat_cap.prev_percent !=
1184 (di->bat_cap.permille) / 10 && 1184 (di->bat_cap.permille) / 10 &&
1185 (di->bat_cap.permille / 10) < 1185 (di->bat_cap.permille / 10) <
1186 di->bat->fg_params->maint_thres) { 1186 di->bat->fg_params->maint_thres) {
1187 dev_dbg(di->dev, 1187 dev_dbg(di->dev,
1188 "battery reported full " 1188 "battery reported full "
1189 "but capacity dropping: %d\n", 1189 "but capacity dropping: %d\n",
1190 di->bat_cap.permille / 10); 1190 di->bat_cap.permille / 10);
1191 di->bat_cap.prev_percent = di->bat_cap.permille / 10; 1191 di->bat_cap.prev_percent = di->bat_cap.permille / 10;
1192 di->bat_cap.prev_mah = di->bat_cap.mah; 1192 di->bat_cap.prev_mah = di->bat_cap.mah;
1193 1193
1194 changed = true; 1194 changed = true;
1195 } 1195 }
1196 } else if (di->bat_cap.prev_percent != di->bat_cap.permille / 10) { 1196 } else if (di->bat_cap.prev_percent != di->bat_cap.permille / 10) {
1197 if (di->bat_cap.permille / 10 == 0) { 1197 if (di->bat_cap.permille / 10 == 0) {
1198 /* 1198 /*
1199 * We will not report 0% unless we've got 1199 * We will not report 0% unless we've got
1200 * the LOW_BAT IRQ, no matter what the FG 1200 * the LOW_BAT IRQ, no matter what the FG
1201 * algorithm says. 1201 * algorithm says.
1202 */ 1202 */
1203 di->bat_cap.prev_percent = 1; 1203 di->bat_cap.prev_percent = 1;
1204 di->bat_cap.permille = 1; 1204 di->bat_cap.permille = 1;
1205 di->bat_cap.prev_mah = 1; 1205 di->bat_cap.prev_mah = 1;
1206 di->bat_cap.mah = 1; 1206 di->bat_cap.mah = 1;
1207 1207
1208 changed = true; 1208 changed = true;
1209 } else if (!(!di->flags.charging && 1209 } else if (!(!di->flags.charging &&
1210 (di->bat_cap.permille / 10) > 1210 (di->bat_cap.permille / 10) >
1211 di->bat_cap.prev_percent) || init) { 1211 di->bat_cap.prev_percent) || init) {
1212 /* 1212 /*
1213 * We do not allow reported capacity to go up 1213 * We do not allow reported capacity to go up
1214 * unless we're charging or if we're in init 1214 * unless we're charging or if we're in init
1215 */ 1215 */
1216 dev_dbg(di->dev, 1216 dev_dbg(di->dev,
1217 "capacity changed from %d to %d (%d)\n", 1217 "capacity changed from %d to %d (%d)\n",
1218 di->bat_cap.prev_percent, 1218 di->bat_cap.prev_percent,
1219 di->bat_cap.permille / 10, 1219 di->bat_cap.permille / 10,
1220 di->bat_cap.permille); 1220 di->bat_cap.permille);
1221 di->bat_cap.prev_percent = di->bat_cap.permille / 10; 1221 di->bat_cap.prev_percent = di->bat_cap.permille / 10;
1222 di->bat_cap.prev_mah = di->bat_cap.mah; 1222 di->bat_cap.prev_mah = di->bat_cap.mah;
1223 1223
1224 changed = true; 1224 changed = true;
1225 } else { 1225 } else {
1226 dev_dbg(di->dev, "capacity not allowed to go up since " 1226 dev_dbg(di->dev, "capacity not allowed to go up since "
1227 "no charger is connected: %d to %d (%d)\n", 1227 "no charger is connected: %d to %d (%d)\n",
1228 di->bat_cap.prev_percent, 1228 di->bat_cap.prev_percent,
1229 di->bat_cap.permille / 10, 1229 di->bat_cap.permille / 10,
1230 di->bat_cap.permille); 1230 di->bat_cap.permille);
1231 } 1231 }
1232 } 1232 }
1233 1233
1234 if (changed) { 1234 if (changed) {
1235 power_supply_changed(&di->fg_psy); 1235 power_supply_changed(&di->fg_psy);
1236 if (di->flags.fully_charged && di->flags.force_full) { 1236 if (di->flags.fully_charged && di->flags.force_full) {
1237 dev_dbg(di->dev, "Battery full, notifying.\n"); 1237 dev_dbg(di->dev, "Battery full, notifying.\n");
1238 di->flags.force_full = false; 1238 di->flags.force_full = false;
1239 sysfs_notify(&di->fg_kobject, NULL, "charge_full"); 1239 sysfs_notify(&di->fg_kobject, NULL, "charge_full");
1240 } 1240 }
1241 sysfs_notify(&di->fg_kobject, NULL, "charge_now"); 1241 sysfs_notify(&di->fg_kobject, NULL, "charge_now");
1242 } 1242 }
1243 } 1243 }
1244 1244
1245 static void ab8500_fg_charge_state_to(struct ab8500_fg *di, 1245 static void ab8500_fg_charge_state_to(struct ab8500_fg *di,
1246 enum ab8500_fg_charge_state new_state) 1246 enum ab8500_fg_charge_state new_state)
1247 { 1247 {
1248 dev_dbg(di->dev, "Charge state from %d [%s] to %d [%s]\n", 1248 dev_dbg(di->dev, "Charge state from %d [%s] to %d [%s]\n",
1249 di->charge_state, 1249 di->charge_state,
1250 charge_state[di->charge_state], 1250 charge_state[di->charge_state],
1251 new_state, 1251 new_state,
1252 charge_state[new_state]); 1252 charge_state[new_state]);
1253 1253
1254 di->charge_state = new_state; 1254 di->charge_state = new_state;
1255 } 1255 }
1256 1256
1257 static void ab8500_fg_discharge_state_to(struct ab8500_fg *di, 1257 static void ab8500_fg_discharge_state_to(struct ab8500_fg *di,
1258 enum ab8500_fg_discharge_state new_state) 1258 enum ab8500_fg_discharge_state new_state)
1259 { 1259 {
1260 dev_dbg(di->dev, "Disharge state from %d [%s] to %d [%s]\n", 1260 dev_dbg(di->dev, "Disharge state from %d [%s] to %d [%s]\n",
1261 di->discharge_state, 1261 di->discharge_state,
1262 discharge_state[di->discharge_state], 1262 discharge_state[di->discharge_state],
1263 new_state, 1263 new_state,
1264 discharge_state[new_state]); 1264 discharge_state[new_state]);
1265 1265
1266 di->discharge_state = new_state; 1266 di->discharge_state = new_state;
1267 } 1267 }
1268 1268
1269 /** 1269 /**
1270 * ab8500_fg_algorithm_charging() - FG algorithm for when charging 1270 * ab8500_fg_algorithm_charging() - FG algorithm for when charging
1271 * @di: pointer to the ab8500_fg structure 1271 * @di: pointer to the ab8500_fg structure
1272 * 1272 *
1273 * Battery capacity calculation state machine for when we're charging 1273 * Battery capacity calculation state machine for when we're charging
1274 */ 1274 */
1275 static void ab8500_fg_algorithm_charging(struct ab8500_fg *di) 1275 static void ab8500_fg_algorithm_charging(struct ab8500_fg *di)
1276 { 1276 {
1277 /* 1277 /*
1278 * If we change to discharge mode 1278 * If we change to discharge mode
1279 * we should start with recovery 1279 * we should start with recovery
1280 */ 1280 */
1281 if (di->discharge_state != AB8500_FG_DISCHARGE_INIT_RECOVERY) 1281 if (di->discharge_state != AB8500_FG_DISCHARGE_INIT_RECOVERY)
1282 ab8500_fg_discharge_state_to(di, 1282 ab8500_fg_discharge_state_to(di,
1283 AB8500_FG_DISCHARGE_INIT_RECOVERY); 1283 AB8500_FG_DISCHARGE_INIT_RECOVERY);
1284 1284
1285 switch (di->charge_state) { 1285 switch (di->charge_state) {
1286 case AB8500_FG_CHARGE_INIT: 1286 case AB8500_FG_CHARGE_INIT:
1287 di->fg_samples = SEC_TO_SAMPLE( 1287 di->fg_samples = SEC_TO_SAMPLE(
1288 di->bat->fg_params->accu_charging); 1288 di->bat->fg_params->accu_charging);
1289 1289
1290 ab8500_fg_coulomb_counter(di, true); 1290 ab8500_fg_coulomb_counter(di, true);
1291 ab8500_fg_charge_state_to(di, AB8500_FG_CHARGE_READOUT); 1291 ab8500_fg_charge_state_to(di, AB8500_FG_CHARGE_READOUT);
1292 1292
1293 break; 1293 break;
1294 1294
1295 case AB8500_FG_CHARGE_READOUT: 1295 case AB8500_FG_CHARGE_READOUT:
1296 /* 1296 /*
1297 * Read the FG and calculate the new capacity 1297 * Read the FG and calculate the new capacity
1298 */ 1298 */
1299 mutex_lock(&di->cc_lock); 1299 mutex_lock(&di->cc_lock);
1300 if (!di->flags.conv_done) { 1300 if (!di->flags.conv_done) {
1301 /* Wasn't the CC IRQ that got us here */ 1301 /* Wasn't the CC IRQ that got us here */
1302 mutex_unlock(&di->cc_lock); 1302 mutex_unlock(&di->cc_lock);
1303 dev_dbg(di->dev, "%s CC conv not done\n", 1303 dev_dbg(di->dev, "%s CC conv not done\n",
1304 __func__); 1304 __func__);
1305 1305
1306 break; 1306 break;
1307 } 1307 }
1308 di->flags.conv_done = false; 1308 di->flags.conv_done = false;
1309 mutex_unlock(&di->cc_lock); 1309 mutex_unlock(&di->cc_lock);
1310 1310
1311 ab8500_fg_calc_cap_charging(di); 1311 ab8500_fg_calc_cap_charging(di);
1312 1312
1313 break; 1313 break;
1314 1314
1315 default: 1315 default:
1316 break; 1316 break;
1317 } 1317 }
1318 1318
1319 /* Check capacity limits */ 1319 /* Check capacity limits */
1320 ab8500_fg_check_capacity_limits(di, false); 1320 ab8500_fg_check_capacity_limits(di, false);
1321 } 1321 }
1322 1322
1323 static void force_capacity(struct ab8500_fg *di) 1323 static void force_capacity(struct ab8500_fg *di)
1324 { 1324 {
1325 int cap; 1325 int cap;
1326 1326
1327 ab8500_fg_clear_cap_samples(di); 1327 ab8500_fg_clear_cap_samples(di);
1328 cap = di->bat_cap.user_mah; 1328 cap = di->bat_cap.user_mah;
1329 if (cap > di->bat_cap.max_mah_design) { 1329 if (cap > di->bat_cap.max_mah_design) {
1330 dev_dbg(di->dev, "Remaining cap %d can't be bigger than total" 1330 dev_dbg(di->dev, "Remaining cap %d can't be bigger than total"
1331 " %d\n", cap, di->bat_cap.max_mah_design); 1331 " %d\n", cap, di->bat_cap.max_mah_design);
1332 cap = di->bat_cap.max_mah_design; 1332 cap = di->bat_cap.max_mah_design;
1333 } 1333 }
1334 ab8500_fg_fill_cap_sample(di, di->bat_cap.user_mah); 1334 ab8500_fg_fill_cap_sample(di, di->bat_cap.user_mah);
1335 di->bat_cap.permille = ab8500_fg_convert_mah_to_permille(di, cap); 1335 di->bat_cap.permille = ab8500_fg_convert_mah_to_permille(di, cap);
1336 di->bat_cap.mah = cap; 1336 di->bat_cap.mah = cap;
1337 ab8500_fg_check_capacity_limits(di, true); 1337 ab8500_fg_check_capacity_limits(di, true);
1338 } 1338 }
1339 1339
1340 static bool check_sysfs_capacity(struct ab8500_fg *di) 1340 static bool check_sysfs_capacity(struct ab8500_fg *di)
1341 { 1341 {
1342 int cap, lower, upper; 1342 int cap, lower, upper;
1343 int cap_permille; 1343 int cap_permille;
1344 1344
1345 cap = di->bat_cap.user_mah; 1345 cap = di->bat_cap.user_mah;
1346 1346
1347 cap_permille = ab8500_fg_convert_mah_to_permille(di, 1347 cap_permille = ab8500_fg_convert_mah_to_permille(di,
1348 di->bat_cap.user_mah); 1348 di->bat_cap.user_mah);
1349 1349
1350 lower = di->bat_cap.permille - di->bat->fg_params->user_cap_limit * 10; 1350 lower = di->bat_cap.permille - di->bat->fg_params->user_cap_limit * 10;
1351 upper = di->bat_cap.permille + di->bat->fg_params->user_cap_limit * 10; 1351 upper = di->bat_cap.permille + di->bat->fg_params->user_cap_limit * 10;
1352 1352
1353 if (lower < 0) 1353 if (lower < 0)
1354 lower = 0; 1354 lower = 0;
1355 /* 1000 is permille, -> 100 percent */ 1355 /* 1000 is permille, -> 100 percent */
1356 if (upper > 1000) 1356 if (upper > 1000)
1357 upper = 1000; 1357 upper = 1000;
1358 1358
1359 dev_dbg(di->dev, "Capacity limits:" 1359 dev_dbg(di->dev, "Capacity limits:"
1360 " (Lower: %d User: %d Upper: %d) [user: %d, was: %d]\n", 1360 " (Lower: %d User: %d Upper: %d) [user: %d, was: %d]\n",
1361 lower, cap_permille, upper, cap, di->bat_cap.mah); 1361 lower, cap_permille, upper, cap, di->bat_cap.mah);
1362 1362
1363 /* If within limits, use the saved capacity and exit estimation...*/ 1363 /* If within limits, use the saved capacity and exit estimation...*/
1364 if (cap_permille > lower && cap_permille < upper) { 1364 if (cap_permille > lower && cap_permille < upper) {
1365 dev_dbg(di->dev, "OK! Using users cap %d uAh now\n", cap); 1365 dev_dbg(di->dev, "OK! Using users cap %d uAh now\n", cap);
1366 force_capacity(di); 1366 force_capacity(di);
1367 return true; 1367 return true;
1368 } 1368 }
1369 dev_dbg(di->dev, "Capacity from user out of limits, ignoring"); 1369 dev_dbg(di->dev, "Capacity from user out of limits, ignoring");
1370 return false; 1370 return false;
1371 } 1371 }
1372 1372
1373 /** 1373 /**
1374 * ab8500_fg_algorithm_discharging() - FG algorithm for when discharging 1374 * ab8500_fg_algorithm_discharging() - FG algorithm for when discharging
1375 * @di: pointer to the ab8500_fg structure 1375 * @di: pointer to the ab8500_fg structure
1376 * 1376 *
1377 * Battery capacity calculation state machine for when we're discharging 1377 * Battery capacity calculation state machine for when we're discharging
1378 */ 1378 */
1379 static void ab8500_fg_algorithm_discharging(struct ab8500_fg *di) 1379 static void ab8500_fg_algorithm_discharging(struct ab8500_fg *di)
1380 { 1380 {
1381 int sleep_time; 1381 int sleep_time;
1382 1382
1383 /* If we change to charge mode we should start with init */ 1383 /* If we change to charge mode we should start with init */
1384 if (di->charge_state != AB8500_FG_CHARGE_INIT) 1384 if (di->charge_state != AB8500_FG_CHARGE_INIT)
1385 ab8500_fg_charge_state_to(di, AB8500_FG_CHARGE_INIT); 1385 ab8500_fg_charge_state_to(di, AB8500_FG_CHARGE_INIT);
1386 1386
1387 switch (di->discharge_state) { 1387 switch (di->discharge_state) {
1388 case AB8500_FG_DISCHARGE_INIT: 1388 case AB8500_FG_DISCHARGE_INIT:
1389 /* We use the FG IRQ to work on */ 1389 /* We use the FG IRQ to work on */
1390 di->init_cnt = 0; 1390 di->init_cnt = 0;
1391 di->fg_samples = SEC_TO_SAMPLE(di->bat->fg_params->init_timer); 1391 di->fg_samples = SEC_TO_SAMPLE(di->bat->fg_params->init_timer);
1392 ab8500_fg_coulomb_counter(di, true); 1392 ab8500_fg_coulomb_counter(di, true);
1393 ab8500_fg_discharge_state_to(di, 1393 ab8500_fg_discharge_state_to(di,
1394 AB8500_FG_DISCHARGE_INITMEASURING); 1394 AB8500_FG_DISCHARGE_INITMEASURING);
1395 1395
1396 /* Intentional fallthrough */ 1396 /* Intentional fallthrough */
1397 case AB8500_FG_DISCHARGE_INITMEASURING: 1397 case AB8500_FG_DISCHARGE_INITMEASURING:
1398 /* 1398 /*
1399 * Discard a number of samples during startup. 1399 * Discard a number of samples during startup.
1400 * After that, use compensated voltage for a few 1400 * After that, use compensated voltage for a few
1401 * samples to get an initial capacity. 1401 * samples to get an initial capacity.
1402 * Then go to READOUT 1402 * Then go to READOUT
1403 */ 1403 */
1404 sleep_time = di->bat->fg_params->init_timer; 1404 sleep_time = di->bat->fg_params->init_timer;
1405 1405
1406 /* Discard the first [x] seconds */ 1406 /* Discard the first [x] seconds */
1407 if (di->init_cnt > 1407 if (di->init_cnt >
1408 di->bat->fg_params->init_discard_time) { 1408 di->bat->fg_params->init_discard_time) {
1409 ab8500_fg_calc_cap_discharge_voltage(di, true); 1409 ab8500_fg_calc_cap_discharge_voltage(di, true);
1410 1410
1411 ab8500_fg_check_capacity_limits(di, true); 1411 ab8500_fg_check_capacity_limits(di, true);
1412 } 1412 }
1413 1413
1414 di->init_cnt += sleep_time; 1414 di->init_cnt += sleep_time;
1415 if (di->init_cnt > di->bat->fg_params->init_total_time) 1415 if (di->init_cnt > di->bat->fg_params->init_total_time)
1416 ab8500_fg_discharge_state_to(di, 1416 ab8500_fg_discharge_state_to(di,
1417 AB8500_FG_DISCHARGE_READOUT_INIT); 1417 AB8500_FG_DISCHARGE_READOUT_INIT);
1418 1418
1419 break; 1419 break;
1420 1420
1421 case AB8500_FG_DISCHARGE_INIT_RECOVERY: 1421 case AB8500_FG_DISCHARGE_INIT_RECOVERY:
1422 di->recovery_cnt = 0; 1422 di->recovery_cnt = 0;
1423 di->recovery_needed = true; 1423 di->recovery_needed = true;
1424 ab8500_fg_discharge_state_to(di, 1424 ab8500_fg_discharge_state_to(di,
1425 AB8500_FG_DISCHARGE_RECOVERY); 1425 AB8500_FG_DISCHARGE_RECOVERY);
1426 1426
1427 /* Intentional fallthrough */ 1427 /* Intentional fallthrough */
1428 1428
1429 case AB8500_FG_DISCHARGE_RECOVERY: 1429 case AB8500_FG_DISCHARGE_RECOVERY:
1430 sleep_time = di->bat->fg_params->recovery_sleep_timer; 1430 sleep_time = di->bat->fg_params->recovery_sleep_timer;
1431 1431
1432 /* 1432 /*
1433 * We should check the power consumption 1433 * We should check the power consumption
1434 * If low, go to READOUT (after x min) or 1434 * If low, go to READOUT (after x min) or
1435 * RECOVERY_SLEEP if time left. 1435 * RECOVERY_SLEEP if time left.
1436 * If high, go to READOUT 1436 * If high, go to READOUT
1437 */ 1437 */
1438 di->inst_curr = ab8500_fg_inst_curr_blocking(di); 1438 di->inst_curr = ab8500_fg_inst_curr_blocking(di);
1439 1439
1440 if (ab8500_fg_is_low_curr(di, di->inst_curr)) { 1440 if (ab8500_fg_is_low_curr(di, di->inst_curr)) {
1441 if (di->recovery_cnt > 1441 if (di->recovery_cnt >
1442 di->bat->fg_params->recovery_total_time) { 1442 di->bat->fg_params->recovery_total_time) {
1443 di->fg_samples = SEC_TO_SAMPLE( 1443 di->fg_samples = SEC_TO_SAMPLE(
1444 di->bat->fg_params->accu_high_curr); 1444 di->bat->fg_params->accu_high_curr);
1445 ab8500_fg_coulomb_counter(di, true); 1445 ab8500_fg_coulomb_counter(di, true);
1446 ab8500_fg_discharge_state_to(di, 1446 ab8500_fg_discharge_state_to(di,
1447 AB8500_FG_DISCHARGE_READOUT); 1447 AB8500_FG_DISCHARGE_READOUT);
1448 di->recovery_needed = false; 1448 di->recovery_needed = false;
1449 } else { 1449 } else {
1450 queue_delayed_work(di->fg_wq, 1450 queue_delayed_work(di->fg_wq,
1451 &di->fg_periodic_work, 1451 &di->fg_periodic_work,
1452 sleep_time * HZ); 1452 sleep_time * HZ);
1453 } 1453 }
1454 di->recovery_cnt += sleep_time; 1454 di->recovery_cnt += sleep_time;
1455 } else { 1455 } else {
1456 di->fg_samples = SEC_TO_SAMPLE( 1456 di->fg_samples = SEC_TO_SAMPLE(
1457 di->bat->fg_params->accu_high_curr); 1457 di->bat->fg_params->accu_high_curr);
1458 ab8500_fg_coulomb_counter(di, true); 1458 ab8500_fg_coulomb_counter(di, true);
1459 ab8500_fg_discharge_state_to(di, 1459 ab8500_fg_discharge_state_to(di,
1460 AB8500_FG_DISCHARGE_READOUT); 1460 AB8500_FG_DISCHARGE_READOUT);
1461 } 1461 }
1462 break; 1462 break;
1463 1463
1464 case AB8500_FG_DISCHARGE_READOUT_INIT: 1464 case AB8500_FG_DISCHARGE_READOUT_INIT:
1465 di->fg_samples = SEC_TO_SAMPLE( 1465 di->fg_samples = SEC_TO_SAMPLE(
1466 di->bat->fg_params->accu_high_curr); 1466 di->bat->fg_params->accu_high_curr);
1467 ab8500_fg_coulomb_counter(di, true); 1467 ab8500_fg_coulomb_counter(di, true);
1468 ab8500_fg_discharge_state_to(di, 1468 ab8500_fg_discharge_state_to(di,
1469 AB8500_FG_DISCHARGE_READOUT); 1469 AB8500_FG_DISCHARGE_READOUT);
1470 break; 1470 break;
1471 1471
1472 case AB8500_FG_DISCHARGE_READOUT: 1472 case AB8500_FG_DISCHARGE_READOUT:
1473 di->inst_curr = ab8500_fg_inst_curr_blocking(di); 1473 di->inst_curr = ab8500_fg_inst_curr_blocking(di);
1474 1474
1475 if (ab8500_fg_is_low_curr(di, di->inst_curr)) { 1475 if (ab8500_fg_is_low_curr(di, di->inst_curr)) {
1476 /* Detect mode change */ 1476 /* Detect mode change */
1477 if (di->high_curr_mode) { 1477 if (di->high_curr_mode) {
1478 di->high_curr_mode = false; 1478 di->high_curr_mode = false;
1479 di->high_curr_cnt = 0; 1479 di->high_curr_cnt = 0;
1480 } 1480 }
1481 1481
1482 if (di->recovery_needed) { 1482 if (di->recovery_needed) {
1483 ab8500_fg_discharge_state_to(di, 1483 ab8500_fg_discharge_state_to(di,
1484 AB8500_FG_DISCHARGE_RECOVERY); 1484 AB8500_FG_DISCHARGE_RECOVERY);
1485 1485
1486 queue_delayed_work(di->fg_wq, 1486 queue_delayed_work(di->fg_wq,
1487 &di->fg_periodic_work, 0); 1487 &di->fg_periodic_work, 0);
1488 1488
1489 break; 1489 break;
1490 } 1490 }
1491 1491
1492 ab8500_fg_calc_cap_discharge_voltage(di, true); 1492 ab8500_fg_calc_cap_discharge_voltage(di, true);
1493 } else { 1493 } else {
1494 mutex_lock(&di->cc_lock); 1494 mutex_lock(&di->cc_lock);
1495 if (!di->flags.conv_done) { 1495 if (!di->flags.conv_done) {
1496 /* Wasn't the CC IRQ that got us here */ 1496 /* Wasn't the CC IRQ that got us here */
1497 mutex_unlock(&di->cc_lock); 1497 mutex_unlock(&di->cc_lock);
1498 dev_dbg(di->dev, "%s CC conv not done\n", 1498 dev_dbg(di->dev, "%s CC conv not done\n",
1499 __func__); 1499 __func__);
1500 1500
1501 break; 1501 break;
1502 } 1502 }
1503 di->flags.conv_done = false; 1503 di->flags.conv_done = false;
1504 mutex_unlock(&di->cc_lock); 1504 mutex_unlock(&di->cc_lock);
1505 1505
1506 /* Detect mode change */ 1506 /* Detect mode change */
1507 if (!di->high_curr_mode) { 1507 if (!di->high_curr_mode) {
1508 di->high_curr_mode = true; 1508 di->high_curr_mode = true;
1509 di->high_curr_cnt = 0; 1509 di->high_curr_cnt = 0;
1510 } 1510 }
1511 1511
1512 di->high_curr_cnt += 1512 di->high_curr_cnt +=
1513 di->bat->fg_params->accu_high_curr; 1513 di->bat->fg_params->accu_high_curr;
1514 if (di->high_curr_cnt > 1514 if (di->high_curr_cnt >
1515 di->bat->fg_params->high_curr_time) 1515 di->bat->fg_params->high_curr_time)
1516 di->recovery_needed = true; 1516 di->recovery_needed = true;
1517 1517
1518 ab8500_fg_calc_cap_discharge_fg(di); 1518 ab8500_fg_calc_cap_discharge_fg(di);
1519 } 1519 }
1520 1520
1521 ab8500_fg_check_capacity_limits(di, false); 1521 ab8500_fg_check_capacity_limits(di, false);
1522 1522
1523 break; 1523 break;
1524 1524
1525 case AB8500_FG_DISCHARGE_WAKEUP: 1525 case AB8500_FG_DISCHARGE_WAKEUP:
1526 ab8500_fg_coulomb_counter(di, true); 1526 ab8500_fg_coulomb_counter(di, true);
1527 di->inst_curr = ab8500_fg_inst_curr_blocking(di); 1527 di->inst_curr = ab8500_fg_inst_curr_blocking(di);
1528 1528
1529 ab8500_fg_calc_cap_discharge_voltage(di, true); 1529 ab8500_fg_calc_cap_discharge_voltage(di, true);
1530 1530
1531 di->fg_samples = SEC_TO_SAMPLE( 1531 di->fg_samples = SEC_TO_SAMPLE(
1532 di->bat->fg_params->accu_high_curr); 1532 di->bat->fg_params->accu_high_curr);
1533 ab8500_fg_coulomb_counter(di, true); 1533 ab8500_fg_coulomb_counter(di, true);
1534 ab8500_fg_discharge_state_to(di, 1534 ab8500_fg_discharge_state_to(di,
1535 AB8500_FG_DISCHARGE_READOUT); 1535 AB8500_FG_DISCHARGE_READOUT);
1536 1536
1537 ab8500_fg_check_capacity_limits(di, false); 1537 ab8500_fg_check_capacity_limits(di, false);
1538 1538
1539 break; 1539 break;
1540 1540
1541 default: 1541 default:
1542 break; 1542 break;
1543 } 1543 }
1544 } 1544 }
1545 1545
1546 /** 1546 /**
1547 * ab8500_fg_algorithm_calibrate() - Internal columb counter offset calibration 1547 * ab8500_fg_algorithm_calibrate() - Internal columb counter offset calibration
1548 * @di: pointer to the ab8500_fg structure 1548 * @di: pointer to the ab8500_fg structure
1549 * 1549 *
1550 */ 1550 */
1551 static void ab8500_fg_algorithm_calibrate(struct ab8500_fg *di) 1551 static void ab8500_fg_algorithm_calibrate(struct ab8500_fg *di)
1552 { 1552 {
1553 int ret; 1553 int ret;
1554 1554
1555 switch (di->calib_state) { 1555 switch (di->calib_state) {
1556 case AB8500_FG_CALIB_INIT: 1556 case AB8500_FG_CALIB_INIT:
1557 dev_dbg(di->dev, "Calibration ongoing...\n"); 1557 dev_dbg(di->dev, "Calibration ongoing...\n");
1558 1558
1559 ret = abx500_mask_and_set_register_interruptible(di->dev, 1559 ret = abx500_mask_and_set_register_interruptible(di->dev,
1560 AB8500_GAS_GAUGE, AB8500_GASG_CC_CTRL_REG, 1560 AB8500_GAS_GAUGE, AB8500_GASG_CC_CTRL_REG,
1561 CC_INT_CAL_N_AVG_MASK, CC_INT_CAL_SAMPLES_8); 1561 CC_INT_CAL_N_AVG_MASK, CC_INT_CAL_SAMPLES_8);
1562 if (ret < 0) 1562 if (ret < 0)
1563 goto err; 1563 goto err;
1564 1564
1565 ret = abx500_mask_and_set_register_interruptible(di->dev, 1565 ret = abx500_mask_and_set_register_interruptible(di->dev,
1566 AB8500_GAS_GAUGE, AB8500_GASG_CC_CTRL_REG, 1566 AB8500_GAS_GAUGE, AB8500_GASG_CC_CTRL_REG,
1567 CC_INTAVGOFFSET_ENA, CC_INTAVGOFFSET_ENA); 1567 CC_INTAVGOFFSET_ENA, CC_INTAVGOFFSET_ENA);
1568 if (ret < 0) 1568 if (ret < 0)
1569 goto err; 1569 goto err;
1570 di->calib_state = AB8500_FG_CALIB_WAIT; 1570 di->calib_state = AB8500_FG_CALIB_WAIT;
1571 break; 1571 break;
1572 case AB8500_FG_CALIB_END: 1572 case AB8500_FG_CALIB_END:
1573 ret = abx500_mask_and_set_register_interruptible(di->dev, 1573 ret = abx500_mask_and_set_register_interruptible(di->dev,
1574 AB8500_GAS_GAUGE, AB8500_GASG_CC_CTRL_REG, 1574 AB8500_GAS_GAUGE, AB8500_GASG_CC_CTRL_REG,
1575 CC_MUXOFFSET, CC_MUXOFFSET); 1575 CC_MUXOFFSET, CC_MUXOFFSET);
1576 if (ret < 0) 1576 if (ret < 0)
1577 goto err; 1577 goto err;
1578 di->flags.calibrate = false; 1578 di->flags.calibrate = false;
1579 dev_dbg(di->dev, "Calibration done...\n"); 1579 dev_dbg(di->dev, "Calibration done...\n");
1580 queue_delayed_work(di->fg_wq, &di->fg_periodic_work, 0); 1580 queue_delayed_work(di->fg_wq, &di->fg_periodic_work, 0);
1581 break; 1581 break;
1582 case AB8500_FG_CALIB_WAIT: 1582 case AB8500_FG_CALIB_WAIT:
1583 dev_dbg(di->dev, "Calibration WFI\n"); 1583 dev_dbg(di->dev, "Calibration WFI\n");
1584 default: 1584 default:
1585 break; 1585 break;
1586 } 1586 }
1587 return; 1587 return;
1588 err: 1588 err:
1589 /* Something went wrong, don't calibrate then */ 1589 /* Something went wrong, don't calibrate then */
1590 dev_err(di->dev, "failed to calibrate the CC\n"); 1590 dev_err(di->dev, "failed to calibrate the CC\n");
1591 di->flags.calibrate = false; 1591 di->flags.calibrate = false;
1592 di->calib_state = AB8500_FG_CALIB_INIT; 1592 di->calib_state = AB8500_FG_CALIB_INIT;
1593 queue_delayed_work(di->fg_wq, &di->fg_periodic_work, 0); 1593 queue_delayed_work(di->fg_wq, &di->fg_periodic_work, 0);
1594 } 1594 }
1595 1595
1596 /** 1596 /**
1597 * ab8500_fg_algorithm() - Entry point for the FG algorithm 1597 * ab8500_fg_algorithm() - Entry point for the FG algorithm
1598 * @di: pointer to the ab8500_fg structure 1598 * @di: pointer to the ab8500_fg structure
1599 * 1599 *
1600 * Entry point for the battery capacity calculation state machine 1600 * Entry point for the battery capacity calculation state machine
1601 */ 1601 */
1602 static void ab8500_fg_algorithm(struct ab8500_fg *di) 1602 static void ab8500_fg_algorithm(struct ab8500_fg *di)
1603 { 1603 {
1604 if (di->flags.calibrate) 1604 if (di->flags.calibrate)
1605 ab8500_fg_algorithm_calibrate(di); 1605 ab8500_fg_algorithm_calibrate(di);
1606 else { 1606 else {
1607 if (di->flags.charging) 1607 if (di->flags.charging)
1608 ab8500_fg_algorithm_charging(di); 1608 ab8500_fg_algorithm_charging(di);
1609 else 1609 else
1610 ab8500_fg_algorithm_discharging(di); 1610 ab8500_fg_algorithm_discharging(di);
1611 } 1611 }
1612 1612
1613 dev_dbg(di->dev, "[FG_DATA] %d %d %d %d %d %d %d %d %d " 1613 dev_dbg(di->dev, "[FG_DATA] %d %d %d %d %d %d %d %d %d "
1614 "%d %d %d %d %d %d %d\n", 1614 "%d %d %d %d %d %d %d\n",
1615 di->bat_cap.max_mah_design, 1615 di->bat_cap.max_mah_design,
1616 di->bat_cap.mah, 1616 di->bat_cap.mah,
1617 di->bat_cap.permille, 1617 di->bat_cap.permille,
1618 di->bat_cap.level, 1618 di->bat_cap.level,
1619 di->bat_cap.prev_mah, 1619 di->bat_cap.prev_mah,
1620 di->bat_cap.prev_percent, 1620 di->bat_cap.prev_percent,
1621 di->bat_cap.prev_level, 1621 di->bat_cap.prev_level,
1622 di->vbat, 1622 di->vbat,
1623 di->inst_curr, 1623 di->inst_curr,
1624 di->avg_curr, 1624 di->avg_curr,
1625 di->accu_charge, 1625 di->accu_charge,
1626 di->flags.charging, 1626 di->flags.charging,
1627 di->charge_state, 1627 di->charge_state,
1628 di->discharge_state, 1628 di->discharge_state,
1629 di->high_curr_mode, 1629 di->high_curr_mode,
1630 di->recovery_needed); 1630 di->recovery_needed);
1631 } 1631 }
1632 1632
1633 /** 1633 /**
1634 * ab8500_fg_periodic_work() - Run the FG state machine periodically 1634 * ab8500_fg_periodic_work() - Run the FG state machine periodically
1635 * @work: pointer to the work_struct structure 1635 * @work: pointer to the work_struct structure
1636 * 1636 *
1637 * Work queue function for periodic work 1637 * Work queue function for periodic work
1638 */ 1638 */
1639 static void ab8500_fg_periodic_work(struct work_struct *work) 1639 static void ab8500_fg_periodic_work(struct work_struct *work)
1640 { 1640 {
1641 struct ab8500_fg *di = container_of(work, struct ab8500_fg, 1641 struct ab8500_fg *di = container_of(work, struct ab8500_fg,
1642 fg_periodic_work.work); 1642 fg_periodic_work.work);
1643 1643
1644 if (di->init_capacity) { 1644 if (di->init_capacity) {
1645 /* A dummy read that will return 0 */ 1645 /* A dummy read that will return 0 */
1646 di->inst_curr = ab8500_fg_inst_curr_blocking(di); 1646 di->inst_curr = ab8500_fg_inst_curr_blocking(di);
1647 /* Get an initial capacity calculation */ 1647 /* Get an initial capacity calculation */
1648 ab8500_fg_calc_cap_discharge_voltage(di, true); 1648 ab8500_fg_calc_cap_discharge_voltage(di, true);
1649 ab8500_fg_check_capacity_limits(di, true); 1649 ab8500_fg_check_capacity_limits(di, true);
1650 di->init_capacity = false; 1650 di->init_capacity = false;
1651 1651
1652 queue_delayed_work(di->fg_wq, &di->fg_periodic_work, 0); 1652 queue_delayed_work(di->fg_wq, &di->fg_periodic_work, 0);
1653 } else if (di->flags.user_cap) { 1653 } else if (di->flags.user_cap) {
1654 if (check_sysfs_capacity(di)) { 1654 if (check_sysfs_capacity(di)) {
1655 ab8500_fg_check_capacity_limits(di, true); 1655 ab8500_fg_check_capacity_limits(di, true);
1656 if (di->flags.charging) 1656 if (di->flags.charging)
1657 ab8500_fg_charge_state_to(di, 1657 ab8500_fg_charge_state_to(di,
1658 AB8500_FG_CHARGE_INIT); 1658 AB8500_FG_CHARGE_INIT);
1659 else 1659 else
1660 ab8500_fg_discharge_state_to(di, 1660 ab8500_fg_discharge_state_to(di,
1661 AB8500_FG_DISCHARGE_READOUT_INIT); 1661 AB8500_FG_DISCHARGE_READOUT_INIT);
1662 } 1662 }
1663 di->flags.user_cap = false; 1663 di->flags.user_cap = false;
1664 queue_delayed_work(di->fg_wq, &di->fg_periodic_work, 0); 1664 queue_delayed_work(di->fg_wq, &di->fg_periodic_work, 0);
1665 } else 1665 } else
1666 ab8500_fg_algorithm(di); 1666 ab8500_fg_algorithm(di);
1667 1667
1668 } 1668 }
1669 1669
1670 /** 1670 /**
1671 * ab8500_fg_check_hw_failure_work() - Check OVV_BAT condition 1671 * ab8500_fg_check_hw_failure_work() - Check OVV_BAT condition
1672 * @work: pointer to the work_struct structure 1672 * @work: pointer to the work_struct structure
1673 * 1673 *
1674 * Work queue function for checking the OVV_BAT condition 1674 * Work queue function for checking the OVV_BAT condition
1675 */ 1675 */
1676 static void ab8500_fg_check_hw_failure_work(struct work_struct *work) 1676 static void ab8500_fg_check_hw_failure_work(struct work_struct *work)
1677 { 1677 {
1678 int ret; 1678 int ret;
1679 u8 reg_value; 1679 u8 reg_value;
1680 1680
1681 struct ab8500_fg *di = container_of(work, struct ab8500_fg, 1681 struct ab8500_fg *di = container_of(work, struct ab8500_fg,
1682 fg_check_hw_failure_work.work); 1682 fg_check_hw_failure_work.work);
1683 1683
1684 /* 1684 /*
1685 * If we have had a battery over-voltage situation, 1685 * If we have had a battery over-voltage situation,
1686 * check ovv-bit to see if it should be reset. 1686 * check ovv-bit to see if it should be reset.
1687 */ 1687 */
1688 if (di->flags.bat_ovv) { 1688 if (di->flags.bat_ovv) {
1689 ret = abx500_get_register_interruptible(di->dev, 1689 ret = abx500_get_register_interruptible(di->dev,
1690 AB8500_CHARGER, AB8500_CH_STAT_REG, 1690 AB8500_CHARGER, AB8500_CH_STAT_REG,
1691 &reg_value); 1691 &reg_value);
1692 if (ret < 0) { 1692 if (ret < 0) {
1693 dev_err(di->dev, "%s ab8500 read failed\n", __func__); 1693 dev_err(di->dev, "%s ab8500 read failed\n", __func__);
1694 return; 1694 return;
1695 } 1695 }
1696 if ((reg_value & BATT_OVV) != BATT_OVV) { 1696 if ((reg_value & BATT_OVV) != BATT_OVV) {
1697 dev_dbg(di->dev, "Battery recovered from OVV\n"); 1697 dev_dbg(di->dev, "Battery recovered from OVV\n");
1698 di->flags.bat_ovv = false; 1698 di->flags.bat_ovv = false;
1699 power_supply_changed(&di->fg_psy); 1699 power_supply_changed(&di->fg_psy);
1700 return; 1700 return;
1701 } 1701 }
1702 1702
1703 /* Not yet recovered from ovv, reschedule this test */ 1703 /* Not yet recovered from ovv, reschedule this test */
1704 queue_delayed_work(di->fg_wq, &di->fg_check_hw_failure_work, 1704 queue_delayed_work(di->fg_wq, &di->fg_check_hw_failure_work,
1705 round_jiffies(HZ)); 1705 round_jiffies(HZ));
1706 } 1706 }
1707 } 1707 }
1708 1708
1709 /** 1709 /**
1710 * ab8500_fg_low_bat_work() - Check LOW_BAT condition 1710 * ab8500_fg_low_bat_work() - Check LOW_BAT condition
1711 * @work: pointer to the work_struct structure 1711 * @work: pointer to the work_struct structure
1712 * 1712 *
1713 * Work queue function for checking the LOW_BAT condition 1713 * Work queue function for checking the LOW_BAT condition
1714 */ 1714 */
1715 static void ab8500_fg_low_bat_work(struct work_struct *work) 1715 static void ab8500_fg_low_bat_work(struct work_struct *work)
1716 { 1716 {
1717 int vbat; 1717 int vbat;
1718 1718
1719 struct ab8500_fg *di = container_of(work, struct ab8500_fg, 1719 struct ab8500_fg *di = container_of(work, struct ab8500_fg,
1720 fg_low_bat_work.work); 1720 fg_low_bat_work.work);
1721 1721
1722 vbat = ab8500_fg_bat_voltage(di); 1722 vbat = ab8500_fg_bat_voltage(di);
1723 1723
1724 /* Check if LOW_BAT still fulfilled */ 1724 /* Check if LOW_BAT still fulfilled */
1725 if (vbat < di->bat->fg_params->lowbat_threshold) { 1725 if (vbat < di->bat->fg_params->lowbat_threshold) {
1726 di->flags.low_bat = true; 1726 di->flags.low_bat = true;
1727 dev_warn(di->dev, "Battery voltage still LOW\n"); 1727 dev_warn(di->dev, "Battery voltage still LOW\n");
1728 1728
1729 /* 1729 /*
1730 * We need to re-schedule this check to be able to detect 1730 * We need to re-schedule this check to be able to detect
1731 * if the voltage increases again during charging 1731 * if the voltage increases again during charging
1732 */ 1732 */
1733 queue_delayed_work(di->fg_wq, &di->fg_low_bat_work, 1733 queue_delayed_work(di->fg_wq, &di->fg_low_bat_work,
1734 round_jiffies(LOW_BAT_CHECK_INTERVAL)); 1734 round_jiffies(LOW_BAT_CHECK_INTERVAL));
1735 } else { 1735 } else {
1736 di->flags.low_bat = false; 1736 di->flags.low_bat = false;
1737 dev_warn(di->dev, "Battery voltage OK again\n"); 1737 dev_warn(di->dev, "Battery voltage OK again\n");
1738 } 1738 }
1739 1739
1740 /* This is needed to dispatch LOW_BAT */ 1740 /* This is needed to dispatch LOW_BAT */
1741 ab8500_fg_check_capacity_limits(di, false); 1741 ab8500_fg_check_capacity_limits(di, false);
1742 1742
1743 /* Set this flag to check if LOW_BAT IRQ still occurs */ 1743 /* Set this flag to check if LOW_BAT IRQ still occurs */
1744 di->flags.low_bat_delay = false; 1744 di->flags.low_bat_delay = false;
1745 } 1745 }
1746 1746
1747 /** 1747 /**
1748 * ab8500_fg_battok_calc - calculate the bit pattern corresponding 1748 * ab8500_fg_battok_calc - calculate the bit pattern corresponding
1749 * to the target voltage. 1749 * to the target voltage.
1750 * @di: pointer to the ab8500_fg structure 1750 * @di: pointer to the ab8500_fg structure
1751 * @target target voltage 1751 * @target target voltage
1752 * 1752 *
1753 * Returns bit pattern closest to the target voltage 1753 * Returns bit pattern closest to the target voltage
1754 * valid return values are 0-14. (0-BATT_OK_MAX_NR_INCREMENTS) 1754 * valid return values are 0-14. (0-BATT_OK_MAX_NR_INCREMENTS)
1755 */ 1755 */
1756 1756
1757 static int ab8500_fg_battok_calc(struct ab8500_fg *di, int target) 1757 static int ab8500_fg_battok_calc(struct ab8500_fg *di, int target)
1758 { 1758 {
1759 if (target > BATT_OK_MIN + 1759 if (target > BATT_OK_MIN +
1760 (BATT_OK_INCREMENT * BATT_OK_MAX_NR_INCREMENTS)) 1760 (BATT_OK_INCREMENT * BATT_OK_MAX_NR_INCREMENTS))
1761 return BATT_OK_MAX_NR_INCREMENTS; 1761 return BATT_OK_MAX_NR_INCREMENTS;
1762 if (target < BATT_OK_MIN) 1762 if (target < BATT_OK_MIN)
1763 return 0; 1763 return 0;
1764 return (target - BATT_OK_MIN) / BATT_OK_INCREMENT; 1764 return (target - BATT_OK_MIN) / BATT_OK_INCREMENT;
1765 } 1765 }
1766 1766
1767 /** 1767 /**
1768 * ab8500_fg_battok_init_hw_register - init battok levels 1768 * ab8500_fg_battok_init_hw_register - init battok levels
1769 * @di: pointer to the ab8500_fg structure 1769 * @di: pointer to the ab8500_fg structure
1770 * 1770 *
1771 */ 1771 */
1772 1772
1773 static int ab8500_fg_battok_init_hw_register(struct ab8500_fg *di) 1773 static int ab8500_fg_battok_init_hw_register(struct ab8500_fg *di)
1774 { 1774 {
1775 int selected; 1775 int selected;
1776 int sel0; 1776 int sel0;
1777 int sel1; 1777 int sel1;
1778 int cbp_sel0; 1778 int cbp_sel0;
1779 int cbp_sel1; 1779 int cbp_sel1;
1780 int ret; 1780 int ret;
1781 int new_val; 1781 int new_val;
1782 1782
1783 sel0 = di->bat->fg_params->battok_falling_th_sel0; 1783 sel0 = di->bat->fg_params->battok_falling_th_sel0;
1784 sel1 = di->bat->fg_params->battok_raising_th_sel1; 1784 sel1 = di->bat->fg_params->battok_raising_th_sel1;
1785 1785
1786 cbp_sel0 = ab8500_fg_battok_calc(di, sel0); 1786 cbp_sel0 = ab8500_fg_battok_calc(di, sel0);
1787 cbp_sel1 = ab8500_fg_battok_calc(di, sel1); 1787 cbp_sel1 = ab8500_fg_battok_calc(di, sel1);
1788 1788
1789 selected = BATT_OK_MIN + cbp_sel0 * BATT_OK_INCREMENT; 1789 selected = BATT_OK_MIN + cbp_sel0 * BATT_OK_INCREMENT;
1790 1790
1791 if (selected != sel0) 1791 if (selected != sel0)
1792 dev_warn(di->dev, "Invalid voltage step:%d, using %d %d\n", 1792 dev_warn(di->dev, "Invalid voltage step:%d, using %d %d\n",
1793 sel0, selected, cbp_sel0); 1793 sel0, selected, cbp_sel0);
1794 1794
1795 selected = BATT_OK_MIN + cbp_sel1 * BATT_OK_INCREMENT; 1795 selected = BATT_OK_MIN + cbp_sel1 * BATT_OK_INCREMENT;
1796 1796
1797 if (selected != sel1) 1797 if (selected != sel1)
1798 dev_warn(di->dev, "Invalid voltage step:%d, using %d %d\n", 1798 dev_warn(di->dev, "Invalid voltage step:%d, using %d %d\n",
1799 sel1, selected, cbp_sel1); 1799 sel1, selected, cbp_sel1);
1800 1800
1801 new_val = cbp_sel0 | (cbp_sel1 << 4); 1801 new_val = cbp_sel0 | (cbp_sel1 << 4);
1802 1802
1803 dev_dbg(di->dev, "using: %x %d %d\n", new_val, cbp_sel0, cbp_sel1); 1803 dev_dbg(di->dev, "using: %x %d %d\n", new_val, cbp_sel0, cbp_sel1);
1804 ret = abx500_set_register_interruptible(di->dev, AB8500_SYS_CTRL2_BLOCK, 1804 ret = abx500_set_register_interruptible(di->dev, AB8500_SYS_CTRL2_BLOCK,
1805 AB8500_BATT_OK_REG, new_val); 1805 AB8500_BATT_OK_REG, new_val);
1806 return ret; 1806 return ret;
1807 } 1807 }
1808 1808
1809 /** 1809 /**
1810 * ab8500_fg_instant_work() - Run the FG state machine instantly 1810 * ab8500_fg_instant_work() - Run the FG state machine instantly
1811 * @work: pointer to the work_struct structure 1811 * @work: pointer to the work_struct structure
1812 * 1812 *
1813 * Work queue function for instant work 1813 * Work queue function for instant work
1814 */ 1814 */
1815 static void ab8500_fg_instant_work(struct work_struct *work) 1815 static void ab8500_fg_instant_work(struct work_struct *work)
1816 { 1816 {
1817 struct ab8500_fg *di = container_of(work, struct ab8500_fg, fg_work); 1817 struct ab8500_fg *di = container_of(work, struct ab8500_fg, fg_work);
1818 1818
1819 ab8500_fg_algorithm(di); 1819 ab8500_fg_algorithm(di);
1820 } 1820 }
1821 1821
1822 /** 1822 /**
1823 * ab8500_fg_cc_data_end_handler() - isr to get battery avg current. 1823 * ab8500_fg_cc_data_end_handler() - isr to get battery avg current.
1824 * @irq: interrupt number 1824 * @irq: interrupt number
1825 * @_di: pointer to the ab8500_fg structure 1825 * @_di: pointer to the ab8500_fg structure
1826 * 1826 *
1827 * Returns IRQ status(IRQ_HANDLED) 1827 * Returns IRQ status(IRQ_HANDLED)
1828 */ 1828 */
1829 static irqreturn_t ab8500_fg_cc_data_end_handler(int irq, void *_di) 1829 static irqreturn_t ab8500_fg_cc_data_end_handler(int irq, void *_di)
1830 { 1830 {
1831 struct ab8500_fg *di = _di; 1831 struct ab8500_fg *di = _di;
1832 complete(&di->ab8500_fg_complete); 1832 complete(&di->ab8500_fg_complete);
1833 return IRQ_HANDLED; 1833 return IRQ_HANDLED;
1834 } 1834 }
1835 1835
1836 /** 1836 /**
1837 * ab8500_fg_cc_convend_handler() - isr to get battery avg current. 1837 * ab8500_fg_cc_convend_handler() - isr to get battery avg current.
1838 * @irq: interrupt number 1838 * @irq: interrupt number
1839 * @_di: pointer to the ab8500_fg structure 1839 * @_di: pointer to the ab8500_fg structure
1840 * 1840 *
1841 * Returns IRQ status(IRQ_HANDLED) 1841 * Returns IRQ status(IRQ_HANDLED)
1842 */ 1842 */
1843 static irqreturn_t ab8500_fg_cc_int_calib_handler(int irq, void *_di) 1843 static irqreturn_t ab8500_fg_cc_int_calib_handler(int irq, void *_di)
1844 { 1844 {
1845 struct ab8500_fg *di = _di; 1845 struct ab8500_fg *di = _di;
1846 di->calib_state = AB8500_FG_CALIB_END; 1846 di->calib_state = AB8500_FG_CALIB_END;
1847 queue_delayed_work(di->fg_wq, &di->fg_periodic_work, 0); 1847 queue_delayed_work(di->fg_wq, &di->fg_periodic_work, 0);
1848 return IRQ_HANDLED; 1848 return IRQ_HANDLED;
1849 } 1849 }
1850 1850
1851 /** 1851 /**
1852 * ab8500_fg_cc_convend_handler() - isr to get battery avg current. 1852 * ab8500_fg_cc_convend_handler() - isr to get battery avg current.
1853 * @irq: interrupt number 1853 * @irq: interrupt number
1854 * @_di: pointer to the ab8500_fg structure 1854 * @_di: pointer to the ab8500_fg structure
1855 * 1855 *
1856 * Returns IRQ status(IRQ_HANDLED) 1856 * Returns IRQ status(IRQ_HANDLED)
1857 */ 1857 */
1858 static irqreturn_t ab8500_fg_cc_convend_handler(int irq, void *_di) 1858 static irqreturn_t ab8500_fg_cc_convend_handler(int irq, void *_di)
1859 { 1859 {
1860 struct ab8500_fg *di = _di; 1860 struct ab8500_fg *di = _di;
1861 1861
1862 queue_work(di->fg_wq, &di->fg_acc_cur_work); 1862 queue_work(di->fg_wq, &di->fg_acc_cur_work);
1863 1863
1864 return IRQ_HANDLED; 1864 return IRQ_HANDLED;
1865 } 1865 }
1866 1866
1867 /** 1867 /**
1868 * ab8500_fg_batt_ovv_handler() - Battery OVV occured 1868 * ab8500_fg_batt_ovv_handler() - Battery OVV occured
1869 * @irq: interrupt number 1869 * @irq: interrupt number
1870 * @_di: pointer to the ab8500_fg structure 1870 * @_di: pointer to the ab8500_fg structure
1871 * 1871 *
1872 * Returns IRQ status(IRQ_HANDLED) 1872 * Returns IRQ status(IRQ_HANDLED)
1873 */ 1873 */
1874 static irqreturn_t ab8500_fg_batt_ovv_handler(int irq, void *_di) 1874 static irqreturn_t ab8500_fg_batt_ovv_handler(int irq, void *_di)
1875 { 1875 {
1876 struct ab8500_fg *di = _di; 1876 struct ab8500_fg *di = _di;
1877 1877
1878 dev_dbg(di->dev, "Battery OVV\n"); 1878 dev_dbg(di->dev, "Battery OVV\n");
1879 di->flags.bat_ovv = true; 1879 di->flags.bat_ovv = true;
1880 power_supply_changed(&di->fg_psy); 1880 power_supply_changed(&di->fg_psy);
1881 1881
1882 /* Schedule a new HW failure check */ 1882 /* Schedule a new HW failure check */
1883 queue_delayed_work(di->fg_wq, &di->fg_check_hw_failure_work, 0); 1883 queue_delayed_work(di->fg_wq, &di->fg_check_hw_failure_work, 0);
1884 1884
1885 return IRQ_HANDLED; 1885 return IRQ_HANDLED;
1886 } 1886 }
1887 1887
1888 /** 1888 /**
1889 * ab8500_fg_lowbatf_handler() - Battery voltage is below LOW threshold 1889 * ab8500_fg_lowbatf_handler() - Battery voltage is below LOW threshold
1890 * @irq: interrupt number 1890 * @irq: interrupt number
1891 * @_di: pointer to the ab8500_fg structure 1891 * @_di: pointer to the ab8500_fg structure
1892 * 1892 *
1893 * Returns IRQ status(IRQ_HANDLED) 1893 * Returns IRQ status(IRQ_HANDLED)
1894 */ 1894 */
1895 static irqreturn_t ab8500_fg_lowbatf_handler(int irq, void *_di) 1895 static irqreturn_t ab8500_fg_lowbatf_handler(int irq, void *_di)
1896 { 1896 {
1897 struct ab8500_fg *di = _di; 1897 struct ab8500_fg *di = _di;
1898 1898
1899 if (!di->flags.low_bat_delay) { 1899 if (!di->flags.low_bat_delay) {
1900 dev_warn(di->dev, "Battery voltage is below LOW threshold\n"); 1900 dev_warn(di->dev, "Battery voltage is below LOW threshold\n");
1901 di->flags.low_bat_delay = true; 1901 di->flags.low_bat_delay = true;
1902 /* 1902 /*
1903 * Start a timer to check LOW_BAT again after some time 1903 * Start a timer to check LOW_BAT again after some time
1904 * This is done to avoid shutdown on single voltage dips 1904 * This is done to avoid shutdown on single voltage dips
1905 */ 1905 */
1906 queue_delayed_work(di->fg_wq, &di->fg_low_bat_work, 1906 queue_delayed_work(di->fg_wq, &di->fg_low_bat_work,
1907 round_jiffies(LOW_BAT_CHECK_INTERVAL)); 1907 round_jiffies(LOW_BAT_CHECK_INTERVAL));
1908 } 1908 }
1909 return IRQ_HANDLED; 1909 return IRQ_HANDLED;
1910 } 1910 }
1911 1911
1912 /** 1912 /**
1913 * ab8500_fg_get_property() - get the fg properties 1913 * ab8500_fg_get_property() - get the fg properties
1914 * @psy: pointer to the power_supply structure 1914 * @psy: pointer to the power_supply structure
1915 * @psp: pointer to the power_supply_property structure 1915 * @psp: pointer to the power_supply_property structure
1916 * @val: pointer to the power_supply_propval union 1916 * @val: pointer to the power_supply_propval union
1917 * 1917 *
1918 * This function gets called when an application tries to get the 1918 * This function gets called when an application tries to get the
1919 * fg properties by reading the sysfs files. 1919 * fg properties by reading the sysfs files.
1920 * voltage_now: battery voltage 1920 * voltage_now: battery voltage
1921 * current_now: battery instant current 1921 * current_now: battery instant current
1922 * current_avg: battery average current 1922 * current_avg: battery average current
1923 * charge_full_design: capacity where battery is considered full 1923 * charge_full_design: capacity where battery is considered full
1924 * charge_now: battery capacity in nAh 1924 * charge_now: battery capacity in nAh
1925 * capacity: capacity in percent 1925 * capacity: capacity in percent
1926 * capacity_level: capacity level 1926 * capacity_level: capacity level
1927 * 1927 *
1928 * Returns error code in case of failure else 0 on success 1928 * Returns error code in case of failure else 0 on success
1929 */ 1929 */
1930 static int ab8500_fg_get_property(struct power_supply *psy, 1930 static int ab8500_fg_get_property(struct power_supply *psy,
1931 enum power_supply_property psp, 1931 enum power_supply_property psp,
1932 union power_supply_propval *val) 1932 union power_supply_propval *val)
1933 { 1933 {
1934 struct ab8500_fg *di; 1934 struct ab8500_fg *di;
1935 1935
1936 di = to_ab8500_fg_device_info(psy); 1936 di = to_ab8500_fg_device_info(psy);
1937 1937
1938 /* 1938 /*
1939 * If battery is identified as unknown and charging of unknown 1939 * If battery is identified as unknown and charging of unknown
1940 * batteries is disabled, we always report 100% capacity and 1940 * batteries is disabled, we always report 100% capacity and
1941 * capacity level UNKNOWN, since we can't calculate 1941 * capacity level UNKNOWN, since we can't calculate
1942 * remaining capacity 1942 * remaining capacity
1943 */ 1943 */
1944 1944
1945 switch (psp) { 1945 switch (psp) {
1946 case POWER_SUPPLY_PROP_VOLTAGE_NOW: 1946 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
1947 if (di->flags.bat_ovv) 1947 if (di->flags.bat_ovv)
1948 val->intval = BATT_OVV_VALUE * 1000; 1948 val->intval = BATT_OVV_VALUE * 1000;
1949 else 1949 else
1950 val->intval = di->vbat * 1000; 1950 val->intval = di->vbat * 1000;
1951 break; 1951 break;
1952 case POWER_SUPPLY_PROP_CURRENT_NOW: 1952 case POWER_SUPPLY_PROP_CURRENT_NOW:
1953 val->intval = di->inst_curr * 1000; 1953 val->intval = di->inst_curr * 1000;
1954 break; 1954 break;
1955 case POWER_SUPPLY_PROP_CURRENT_AVG: 1955 case POWER_SUPPLY_PROP_CURRENT_AVG:
1956 val->intval = di->avg_curr * 1000; 1956 val->intval = di->avg_curr * 1000;
1957 break; 1957 break;
1958 case POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN: 1958 case POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN:
1959 val->intval = ab8500_fg_convert_mah_to_uwh(di, 1959 val->intval = ab8500_fg_convert_mah_to_uwh(di,
1960 di->bat_cap.max_mah_design); 1960 di->bat_cap.max_mah_design);
1961 break; 1961 break;
1962 case POWER_SUPPLY_PROP_ENERGY_FULL: 1962 case POWER_SUPPLY_PROP_ENERGY_FULL:
1963 val->intval = ab8500_fg_convert_mah_to_uwh(di, 1963 val->intval = ab8500_fg_convert_mah_to_uwh(di,
1964 di->bat_cap.max_mah); 1964 di->bat_cap.max_mah);
1965 break; 1965 break;
1966 case POWER_SUPPLY_PROP_ENERGY_NOW: 1966 case POWER_SUPPLY_PROP_ENERGY_NOW:
1967 if (di->flags.batt_unknown && !di->bat->chg_unknown_bat && 1967 if (di->flags.batt_unknown && !di->bat->chg_unknown_bat &&
1968 di->flags.batt_id_received) 1968 di->flags.batt_id_received)
1969 val->intval = ab8500_fg_convert_mah_to_uwh(di, 1969 val->intval = ab8500_fg_convert_mah_to_uwh(di,
1970 di->bat_cap.max_mah); 1970 di->bat_cap.max_mah);
1971 else 1971 else
1972 val->intval = ab8500_fg_convert_mah_to_uwh(di, 1972 val->intval = ab8500_fg_convert_mah_to_uwh(di,
1973 di->bat_cap.prev_mah); 1973 di->bat_cap.prev_mah);
1974 break; 1974 break;
1975 case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN: 1975 case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN:
1976 val->intval = di->bat_cap.max_mah_design; 1976 val->intval = di->bat_cap.max_mah_design;
1977 break; 1977 break;
1978 case POWER_SUPPLY_PROP_CHARGE_FULL: 1978 case POWER_SUPPLY_PROP_CHARGE_FULL:
1979 val->intval = di->bat_cap.max_mah; 1979 val->intval = di->bat_cap.max_mah;
1980 break; 1980 break;
1981 case POWER_SUPPLY_PROP_CHARGE_NOW: 1981 case POWER_SUPPLY_PROP_CHARGE_NOW:
1982 if (di->flags.batt_unknown && !di->bat->chg_unknown_bat && 1982 if (di->flags.batt_unknown && !di->bat->chg_unknown_bat &&
1983 di->flags.batt_id_received) 1983 di->flags.batt_id_received)
1984 val->intval = di->bat_cap.max_mah; 1984 val->intval = di->bat_cap.max_mah;
1985 else 1985 else
1986 val->intval = di->bat_cap.prev_mah; 1986 val->intval = di->bat_cap.prev_mah;
1987 break; 1987 break;
1988 case POWER_SUPPLY_PROP_CAPACITY: 1988 case POWER_SUPPLY_PROP_CAPACITY:
1989 if (di->flags.batt_unknown && !di->bat->chg_unknown_bat && 1989 if (di->flags.batt_unknown && !di->bat->chg_unknown_bat &&
1990 di->flags.batt_id_received) 1990 di->flags.batt_id_received)
1991 val->intval = 100; 1991 val->intval = 100;
1992 else 1992 else
1993 val->intval = di->bat_cap.prev_percent; 1993 val->intval = di->bat_cap.prev_percent;
1994 break; 1994 break;
1995 case POWER_SUPPLY_PROP_CAPACITY_LEVEL: 1995 case POWER_SUPPLY_PROP_CAPACITY_LEVEL:
1996 if (di->flags.batt_unknown && !di->bat->chg_unknown_bat && 1996 if (di->flags.batt_unknown && !di->bat->chg_unknown_bat &&
1997 di->flags.batt_id_received) 1997 di->flags.batt_id_received)
1998 val->intval = POWER_SUPPLY_CAPACITY_LEVEL_UNKNOWN; 1998 val->intval = POWER_SUPPLY_CAPACITY_LEVEL_UNKNOWN;
1999 else 1999 else
2000 val->intval = di->bat_cap.prev_level; 2000 val->intval = di->bat_cap.prev_level;
2001 break; 2001 break;
2002 default: 2002 default:
2003 return -EINVAL; 2003 return -EINVAL;
2004 } 2004 }
2005 return 0; 2005 return 0;
2006 } 2006 }
2007 2007
2008 static int ab8500_fg_get_ext_psy_data(struct device *dev, void *data) 2008 static int ab8500_fg_get_ext_psy_data(struct device *dev, void *data)
2009 { 2009 {
2010 struct power_supply *psy; 2010 struct power_supply *psy;
2011 struct power_supply *ext; 2011 struct power_supply *ext;
2012 struct ab8500_fg *di; 2012 struct ab8500_fg *di;
2013 union power_supply_propval ret; 2013 union power_supply_propval ret;
2014 int i, j; 2014 int i, j;
2015 bool psy_found = false; 2015 bool psy_found = false;
2016 2016
2017 psy = (struct power_supply *)data; 2017 psy = (struct power_supply *)data;
2018 ext = dev_get_drvdata(dev); 2018 ext = dev_get_drvdata(dev);
2019 di = to_ab8500_fg_device_info(psy); 2019 di = to_ab8500_fg_device_info(psy);
2020 2020
2021 /* 2021 /*
2022 * For all psy where the name of your driver 2022 * For all psy where the name of your driver
2023 * appears in any supplied_to 2023 * appears in any supplied_to
2024 */ 2024 */
2025 for (i = 0; i < ext->num_supplicants; i++) { 2025 for (i = 0; i < ext->num_supplicants; i++) {
2026 if (!strcmp(ext->supplied_to[i], psy->name)) 2026 if (!strcmp(ext->supplied_to[i], psy->name))
2027 psy_found = true; 2027 psy_found = true;
2028 } 2028 }
2029 2029
2030 if (!psy_found) 2030 if (!psy_found)
2031 return 0; 2031 return 0;
2032 2032
2033 /* Go through all properties for the psy */ 2033 /* Go through all properties for the psy */
2034 for (j = 0; j < ext->num_properties; j++) { 2034 for (j = 0; j < ext->num_properties; j++) {
2035 enum power_supply_property prop; 2035 enum power_supply_property prop;
2036 prop = ext->properties[j]; 2036 prop = ext->properties[j];
2037 2037
2038 if (ext->get_property(ext, prop, &ret)) 2038 if (ext->get_property(ext, prop, &ret))
2039 continue; 2039 continue;
2040 2040
2041 switch (prop) { 2041 switch (prop) {
2042 case POWER_SUPPLY_PROP_STATUS: 2042 case POWER_SUPPLY_PROP_STATUS:
2043 switch (ext->type) { 2043 switch (ext->type) {
2044 case POWER_SUPPLY_TYPE_BATTERY: 2044 case POWER_SUPPLY_TYPE_BATTERY:
2045 switch (ret.intval) { 2045 switch (ret.intval) {
2046 case POWER_SUPPLY_STATUS_UNKNOWN: 2046 case POWER_SUPPLY_STATUS_UNKNOWN:
2047 case POWER_SUPPLY_STATUS_DISCHARGING: 2047 case POWER_SUPPLY_STATUS_DISCHARGING:
2048 case POWER_SUPPLY_STATUS_NOT_CHARGING: 2048 case POWER_SUPPLY_STATUS_NOT_CHARGING:
2049 if (!di->flags.charging) 2049 if (!di->flags.charging)
2050 break; 2050 break;
2051 di->flags.charging = false; 2051 di->flags.charging = false;
2052 di->flags.fully_charged = false; 2052 di->flags.fully_charged = false;
2053 queue_work(di->fg_wq, &di->fg_work); 2053 queue_work(di->fg_wq, &di->fg_work);
2054 break; 2054 break;
2055 case POWER_SUPPLY_STATUS_FULL: 2055 case POWER_SUPPLY_STATUS_FULL:
2056 if (di->flags.fully_charged) 2056 if (di->flags.fully_charged)
2057 break; 2057 break;
2058 di->flags.fully_charged = true; 2058 di->flags.fully_charged = true;
2059 di->flags.force_full = true; 2059 di->flags.force_full = true;
2060 /* Save current capacity as maximum */ 2060 /* Save current capacity as maximum */
2061 di->bat_cap.max_mah = di->bat_cap.mah; 2061 di->bat_cap.max_mah = di->bat_cap.mah;
2062 queue_work(di->fg_wq, &di->fg_work); 2062 queue_work(di->fg_wq, &di->fg_work);
2063 break; 2063 break;
2064 case POWER_SUPPLY_STATUS_CHARGING: 2064 case POWER_SUPPLY_STATUS_CHARGING:
2065 if (di->flags.charging) 2065 if (di->flags.charging)
2066 break; 2066 break;
2067 di->flags.charging = true; 2067 di->flags.charging = true;
2068 di->flags.fully_charged = false; 2068 di->flags.fully_charged = false;
2069 queue_work(di->fg_wq, &di->fg_work); 2069 queue_work(di->fg_wq, &di->fg_work);
2070 break; 2070 break;
2071 }; 2071 };
2072 default: 2072 default:
2073 break; 2073 break;
2074 }; 2074 };
2075 break; 2075 break;
2076 case POWER_SUPPLY_PROP_TECHNOLOGY: 2076 case POWER_SUPPLY_PROP_TECHNOLOGY:
2077 switch (ext->type) { 2077 switch (ext->type) {
2078 case POWER_SUPPLY_TYPE_BATTERY: 2078 case POWER_SUPPLY_TYPE_BATTERY:
2079 if (!di->flags.batt_id_received) { 2079 if (!di->flags.batt_id_received) {
2080 const struct abx500_battery_type *b; 2080 const struct abx500_battery_type *b;
2081 2081
2082 b = &(di->bat->bat_type[di->bat->batt_id]); 2082 b = &(di->bat->bat_type[di->bat->batt_id]);
2083 2083
2084 di->flags.batt_id_received = true; 2084 di->flags.batt_id_received = true;
2085 2085
2086 di->bat_cap.max_mah_design = 2086 di->bat_cap.max_mah_design =
2087 MILLI_TO_MICRO * 2087 MILLI_TO_MICRO *
2088 b->charge_full_design; 2088 b->charge_full_design;
2089 2089
2090 di->bat_cap.max_mah = 2090 di->bat_cap.max_mah =
2091 di->bat_cap.max_mah_design; 2091 di->bat_cap.max_mah_design;
2092 2092
2093 di->vbat_nom = b->nominal_voltage; 2093 di->vbat_nom = b->nominal_voltage;
2094 } 2094 }
2095 2095
2096 if (ret.intval) 2096 if (ret.intval)
2097 di->flags.batt_unknown = false; 2097 di->flags.batt_unknown = false;
2098 else 2098 else
2099 di->flags.batt_unknown = true; 2099 di->flags.batt_unknown = true;
2100 break; 2100 break;
2101 default: 2101 default:
2102 break; 2102 break;
2103 } 2103 }
2104 break; 2104 break;
2105 case POWER_SUPPLY_PROP_TEMP: 2105 case POWER_SUPPLY_PROP_TEMP:
2106 switch (ext->type) { 2106 switch (ext->type) {
2107 case POWER_SUPPLY_TYPE_BATTERY: 2107 case POWER_SUPPLY_TYPE_BATTERY:
2108 if (di->flags.batt_id_received) 2108 if (di->flags.batt_id_received)
2109 di->bat_temp = ret.intval; 2109 di->bat_temp = ret.intval;
2110 break; 2110 break;
2111 default: 2111 default:
2112 break; 2112 break;
2113 } 2113 }
2114 break; 2114 break;
2115 default: 2115 default:
2116 break; 2116 break;
2117 } 2117 }
2118 } 2118 }
2119 return 0; 2119 return 0;
2120 } 2120 }
2121 2121
2122 /** 2122 /**
2123 * ab8500_fg_init_hw_registers() - Set up FG related registers 2123 * ab8500_fg_init_hw_registers() - Set up FG related registers
2124 * @di: pointer to the ab8500_fg structure 2124 * @di: pointer to the ab8500_fg structure
2125 * 2125 *
2126 * Set up battery OVV, low battery voltage registers 2126 * Set up battery OVV, low battery voltage registers
2127 */ 2127 */
2128 static int ab8500_fg_init_hw_registers(struct ab8500_fg *di) 2128 static int ab8500_fg_init_hw_registers(struct ab8500_fg *di)
2129 { 2129 {
2130 int ret; 2130 int ret;
2131 2131
2132 /* Set VBAT OVV threshold */ 2132 /* Set VBAT OVV threshold */
2133 ret = abx500_mask_and_set_register_interruptible(di->dev, 2133 ret = abx500_mask_and_set_register_interruptible(di->dev,
2134 AB8500_CHARGER, 2134 AB8500_CHARGER,
2135 AB8500_BATT_OVV, 2135 AB8500_BATT_OVV,
2136 BATT_OVV_TH_4P75, 2136 BATT_OVV_TH_4P75,
2137 BATT_OVV_TH_4P75); 2137 BATT_OVV_TH_4P75);
2138 if (ret) { 2138 if (ret) {
2139 dev_err(di->dev, "failed to set BATT_OVV\n"); 2139 dev_err(di->dev, "failed to set BATT_OVV\n");
2140 goto out; 2140 goto out;
2141 } 2141 }
2142 2142
2143 /* Enable VBAT OVV detection */ 2143 /* Enable VBAT OVV detection */
2144 ret = abx500_mask_and_set_register_interruptible(di->dev, 2144 ret = abx500_mask_and_set_register_interruptible(di->dev,
2145 AB8500_CHARGER, 2145 AB8500_CHARGER,
2146 AB8500_BATT_OVV, 2146 AB8500_BATT_OVV,
2147 BATT_OVV_ENA, 2147 BATT_OVV_ENA,
2148 BATT_OVV_ENA); 2148 BATT_OVV_ENA);
2149 if (ret) { 2149 if (ret) {
2150 dev_err(di->dev, "failed to enable BATT_OVV\n"); 2150 dev_err(di->dev, "failed to enable BATT_OVV\n");
2151 goto out; 2151 goto out;
2152 } 2152 }
2153 2153
2154 /* Low Battery Voltage */ 2154 /* Low Battery Voltage */
2155 ret = abx500_set_register_interruptible(di->dev, 2155 ret = abx500_set_register_interruptible(di->dev,
2156 AB8500_SYS_CTRL2_BLOCK, 2156 AB8500_SYS_CTRL2_BLOCK,
2157 AB8500_LOW_BAT_REG, 2157 AB8500_LOW_BAT_REG,
2158 ab8500_volt_to_regval( 2158 ab8500_volt_to_regval(
2159 di->bat->fg_params->lowbat_threshold) << 1 | 2159 di->bat->fg_params->lowbat_threshold) << 1 |
2160 LOW_BAT_ENABLE); 2160 LOW_BAT_ENABLE);
2161 if (ret) { 2161 if (ret) {
2162 dev_err(di->dev, "%s write failed\n", __func__); 2162 dev_err(di->dev, "%s write failed\n", __func__);
2163 goto out; 2163 goto out;
2164 } 2164 }
2165 2165
2166 /* Battery OK threshold */ 2166 /* Battery OK threshold */
2167 ret = ab8500_fg_battok_init_hw_register(di); 2167 ret = ab8500_fg_battok_init_hw_register(di);
2168 if (ret) { 2168 if (ret) {
2169 dev_err(di->dev, "BattOk init write failed.\n"); 2169 dev_err(di->dev, "BattOk init write failed.\n");
2170 goto out; 2170 goto out;
2171 } 2171 }
2172 out: 2172 out:
2173 return ret; 2173 return ret;
2174 } 2174 }
2175 2175
2176 /** 2176 /**
2177 * ab8500_fg_external_power_changed() - callback for power supply changes 2177 * ab8500_fg_external_power_changed() - callback for power supply changes
2178 * @psy: pointer to the structure power_supply 2178 * @psy: pointer to the structure power_supply
2179 * 2179 *
2180 * This function is the entry point of the pointer external_power_changed 2180 * This function is the entry point of the pointer external_power_changed
2181 * of the structure power_supply. 2181 * of the structure power_supply.
2182 * This function gets executed when there is a change in any external power 2182 * This function gets executed when there is a change in any external power
2183 * supply that this driver needs to be notified of. 2183 * supply that this driver needs to be notified of.
2184 */ 2184 */
2185 static void ab8500_fg_external_power_changed(struct power_supply *psy) 2185 static void ab8500_fg_external_power_changed(struct power_supply *psy)
2186 { 2186 {
2187 struct ab8500_fg *di = to_ab8500_fg_device_info(psy); 2187 struct ab8500_fg *di = to_ab8500_fg_device_info(psy);
2188 2188
2189 class_for_each_device(power_supply_class, NULL, 2189 class_for_each_device(power_supply_class, NULL,
2190 &di->fg_psy, ab8500_fg_get_ext_psy_data); 2190 &di->fg_psy, ab8500_fg_get_ext_psy_data);
2191 } 2191 }
2192 2192
2193 /** 2193 /**
2194 * abab8500_fg_reinit_work() - work to reset the FG algorithm 2194 * abab8500_fg_reinit_work() - work to reset the FG algorithm
2195 * @work: pointer to the work_struct structure 2195 * @work: pointer to the work_struct structure
2196 * 2196 *
2197 * Used to reset the current battery capacity to be able to 2197 * Used to reset the current battery capacity to be able to
2198 * retrigger a new voltage base capacity calculation. For 2198 * retrigger a new voltage base capacity calculation. For
2199 * test and verification purpose. 2199 * test and verification purpose.
2200 */ 2200 */
2201 static void ab8500_fg_reinit_work(struct work_struct *work) 2201 static void ab8500_fg_reinit_work(struct work_struct *work)
2202 { 2202 {
2203 struct ab8500_fg *di = container_of(work, struct ab8500_fg, 2203 struct ab8500_fg *di = container_of(work, struct ab8500_fg,
2204 fg_reinit_work.work); 2204 fg_reinit_work.work);
2205 2205
2206 if (di->flags.calibrate == false) { 2206 if (di->flags.calibrate == false) {
2207 dev_dbg(di->dev, "Resetting FG state machine to init.\n"); 2207 dev_dbg(di->dev, "Resetting FG state machine to init.\n");
2208 ab8500_fg_clear_cap_samples(di); 2208 ab8500_fg_clear_cap_samples(di);
2209 ab8500_fg_calc_cap_discharge_voltage(di, true); 2209 ab8500_fg_calc_cap_discharge_voltage(di, true);
2210 ab8500_fg_charge_state_to(di, AB8500_FG_CHARGE_INIT); 2210 ab8500_fg_charge_state_to(di, AB8500_FG_CHARGE_INIT);
2211 ab8500_fg_discharge_state_to(di, AB8500_FG_DISCHARGE_INIT); 2211 ab8500_fg_discharge_state_to(di, AB8500_FG_DISCHARGE_INIT);
2212 queue_delayed_work(di->fg_wq, &di->fg_periodic_work, 0); 2212 queue_delayed_work(di->fg_wq, &di->fg_periodic_work, 0);
2213 2213
2214 } else { 2214 } else {
2215 dev_err(di->dev, "Residual offset calibration ongoing " 2215 dev_err(di->dev, "Residual offset calibration ongoing "
2216 "retrying..\n"); 2216 "retrying..\n");
2217 /* Wait one second until next try*/ 2217 /* Wait one second until next try*/
2218 queue_delayed_work(di->fg_wq, &di->fg_reinit_work, 2218 queue_delayed_work(di->fg_wq, &di->fg_reinit_work,
2219 round_jiffies(1)); 2219 round_jiffies(1));
2220 } 2220 }
2221 } 2221 }
2222 2222
2223 /** 2223 /**
2224 * ab8500_fg_reinit() - forces FG algorithm to reinitialize with current values 2224 * ab8500_fg_reinit() - forces FG algorithm to reinitialize with current values
2225 * 2225 *
2226 * This function can be used to force the FG algorithm to recalculate a new 2226 * This function can be used to force the FG algorithm to recalculate a new
2227 * voltage based battery capacity. 2227 * voltage based battery capacity.
2228 */ 2228 */
2229 void ab8500_fg_reinit(void) 2229 void ab8500_fg_reinit(void)
2230 { 2230 {
2231 struct ab8500_fg *di = ab8500_fg_get(); 2231 struct ab8500_fg *di = ab8500_fg_get();
2232 /* User won't be notified if a null pointer returned. */ 2232 /* User won't be notified if a null pointer returned. */
2233 if (di != NULL) 2233 if (di != NULL)
2234 queue_delayed_work(di->fg_wq, &di->fg_reinit_work, 0); 2234 queue_delayed_work(di->fg_wq, &di->fg_reinit_work, 0);
2235 } 2235 }
2236 2236
2237 /* Exposure to the sysfs interface */ 2237 /* Exposure to the sysfs interface */
2238 2238
2239 struct ab8500_fg_sysfs_entry { 2239 struct ab8500_fg_sysfs_entry {
2240 struct attribute attr; 2240 struct attribute attr;
2241 ssize_t (*show)(struct ab8500_fg *, char *); 2241 ssize_t (*show)(struct ab8500_fg *, char *);
2242 ssize_t (*store)(struct ab8500_fg *, const char *, size_t); 2242 ssize_t (*store)(struct ab8500_fg *, const char *, size_t);
2243 }; 2243 };
2244 2244
2245 static ssize_t charge_full_show(struct ab8500_fg *di, char *buf) 2245 static ssize_t charge_full_show(struct ab8500_fg *di, char *buf)
2246 { 2246 {
2247 return sprintf(buf, "%d\n", di->bat_cap.max_mah); 2247 return sprintf(buf, "%d\n", di->bat_cap.max_mah);
2248 } 2248 }
2249 2249
2250 static ssize_t charge_full_store(struct ab8500_fg *di, const char *buf, 2250 static ssize_t charge_full_store(struct ab8500_fg *di, const char *buf,
2251 size_t count) 2251 size_t count)
2252 { 2252 {
2253 unsigned long charge_full; 2253 unsigned long charge_full;
2254 ssize_t ret = -EINVAL; 2254 ssize_t ret = -EINVAL;
2255 2255
2256 ret = strict_strtoul(buf, 10, &charge_full); 2256 ret = strict_strtoul(buf, 10, &charge_full);
2257 2257
2258 dev_dbg(di->dev, "Ret %zd charge_full %lu", ret, charge_full); 2258 dev_dbg(di->dev, "Ret %zd charge_full %lu", ret, charge_full);
2259 2259
2260 if (!ret) { 2260 if (!ret) {
2261 di->bat_cap.max_mah = (int) charge_full; 2261 di->bat_cap.max_mah = (int) charge_full;
2262 ret = count; 2262 ret = count;
2263 } 2263 }
2264 return ret; 2264 return ret;
2265 } 2265 }
2266 2266
2267 static ssize_t charge_now_show(struct ab8500_fg *di, char *buf) 2267 static ssize_t charge_now_show(struct ab8500_fg *di, char *buf)
2268 { 2268 {
2269 return sprintf(buf, "%d\n", di->bat_cap.prev_mah); 2269 return sprintf(buf, "%d\n", di->bat_cap.prev_mah);
2270 } 2270 }
2271 2271
2272 static ssize_t charge_now_store(struct ab8500_fg *di, const char *buf, 2272 static ssize_t charge_now_store(struct ab8500_fg *di, const char *buf,
2273 size_t count) 2273 size_t count)
2274 { 2274 {
2275 unsigned long charge_now; 2275 unsigned long charge_now;
2276 ssize_t ret; 2276 ssize_t ret;
2277 2277
2278 ret = strict_strtoul(buf, 10, &charge_now); 2278 ret = strict_strtoul(buf, 10, &charge_now);
2279 2279
2280 dev_dbg(di->dev, "Ret %zd charge_now %lu was %d", 2280 dev_dbg(di->dev, "Ret %zd charge_now %lu was %d",
2281 ret, charge_now, di->bat_cap.prev_mah); 2281 ret, charge_now, di->bat_cap.prev_mah);
2282 2282
2283 if (!ret) { 2283 if (!ret) {
2284 di->bat_cap.user_mah = (int) charge_now; 2284 di->bat_cap.user_mah = (int) charge_now;
2285 di->flags.user_cap = true; 2285 di->flags.user_cap = true;
2286 ret = count; 2286 ret = count;
2287 queue_delayed_work(di->fg_wq, &di->fg_periodic_work, 0); 2287 queue_delayed_work(di->fg_wq, &di->fg_periodic_work, 0);
2288 } 2288 }
2289 return ret; 2289 return ret;
2290 } 2290 }
2291 2291
2292 static struct ab8500_fg_sysfs_entry charge_full_attr = 2292 static struct ab8500_fg_sysfs_entry charge_full_attr =
2293 __ATTR(charge_full, 0644, charge_full_show, charge_full_store); 2293 __ATTR(charge_full, 0644, charge_full_show, charge_full_store);
2294 2294
2295 static struct ab8500_fg_sysfs_entry charge_now_attr = 2295 static struct ab8500_fg_sysfs_entry charge_now_attr =
2296 __ATTR(charge_now, 0644, charge_now_show, charge_now_store); 2296 __ATTR(charge_now, 0644, charge_now_show, charge_now_store);
2297 2297
2298 static ssize_t 2298 static ssize_t
2299 ab8500_fg_show(struct kobject *kobj, struct attribute *attr, char *buf) 2299 ab8500_fg_show(struct kobject *kobj, struct attribute *attr, char *buf)
2300 { 2300 {
2301 struct ab8500_fg_sysfs_entry *entry; 2301 struct ab8500_fg_sysfs_entry *entry;
2302 struct ab8500_fg *di; 2302 struct ab8500_fg *di;
2303 2303
2304 entry = container_of(attr, struct ab8500_fg_sysfs_entry, attr); 2304 entry = container_of(attr, struct ab8500_fg_sysfs_entry, attr);
2305 di = container_of(kobj, struct ab8500_fg, fg_kobject); 2305 di = container_of(kobj, struct ab8500_fg, fg_kobject);
2306 2306
2307 if (!entry->show) 2307 if (!entry->show)
2308 return -EIO; 2308 return -EIO;
2309 2309
2310 return entry->show(di, buf); 2310 return entry->show(di, buf);
2311 } 2311 }
2312 static ssize_t 2312 static ssize_t
2313 ab8500_fg_store(struct kobject *kobj, struct attribute *attr, const char *buf, 2313 ab8500_fg_store(struct kobject *kobj, struct attribute *attr, const char *buf,
2314 size_t count) 2314 size_t count)
2315 { 2315 {
2316 struct ab8500_fg_sysfs_entry *entry; 2316 struct ab8500_fg_sysfs_entry *entry;
2317 struct ab8500_fg *di; 2317 struct ab8500_fg *di;
2318 2318
2319 entry = container_of(attr, struct ab8500_fg_sysfs_entry, attr); 2319 entry = container_of(attr, struct ab8500_fg_sysfs_entry, attr);
2320 di = container_of(kobj, struct ab8500_fg, fg_kobject); 2320 di = container_of(kobj, struct ab8500_fg, fg_kobject);
2321 2321
2322 if (!entry->store) 2322 if (!entry->store)
2323 return -EIO; 2323 return -EIO;
2324 2324
2325 return entry->store(di, buf, count); 2325 return entry->store(di, buf, count);
2326 } 2326 }
2327 2327
2328 static const struct sysfs_ops ab8500_fg_sysfs_ops = { 2328 static const struct sysfs_ops ab8500_fg_sysfs_ops = {
2329 .show = ab8500_fg_show, 2329 .show = ab8500_fg_show,
2330 .store = ab8500_fg_store, 2330 .store = ab8500_fg_store,
2331 }; 2331 };
2332 2332
2333 static struct attribute *ab8500_fg_attrs[] = { 2333 static struct attribute *ab8500_fg_attrs[] = {
2334 &charge_full_attr.attr, 2334 &charge_full_attr.attr,
2335 &charge_now_attr.attr, 2335 &charge_now_attr.attr,
2336 NULL, 2336 NULL,
2337 }; 2337 };
2338 2338
2339 static struct kobj_type ab8500_fg_ktype = { 2339 static struct kobj_type ab8500_fg_ktype = {
2340 .sysfs_ops = &ab8500_fg_sysfs_ops, 2340 .sysfs_ops = &ab8500_fg_sysfs_ops,
2341 .default_attrs = ab8500_fg_attrs, 2341 .default_attrs = ab8500_fg_attrs,
2342 }; 2342 };
2343 2343
2344 /** 2344 /**
2345 * ab8500_chargalg_sysfs_exit() - de-init of sysfs entry 2345 * ab8500_chargalg_sysfs_exit() - de-init of sysfs entry
2346 * @di: pointer to the struct ab8500_chargalg 2346 * @di: pointer to the struct ab8500_chargalg
2347 * 2347 *
2348 * This function removes the entry in sysfs. 2348 * This function removes the entry in sysfs.
2349 */ 2349 */
2350 static void ab8500_fg_sysfs_exit(struct ab8500_fg *di) 2350 static void ab8500_fg_sysfs_exit(struct ab8500_fg *di)
2351 { 2351 {
2352 kobject_del(&di->fg_kobject); 2352 kobject_del(&di->fg_kobject);
2353 } 2353 }
2354 2354
2355 /** 2355 /**
2356 * ab8500_chargalg_sysfs_init() - init of sysfs entry 2356 * ab8500_chargalg_sysfs_init() - init of sysfs entry
2357 * @di: pointer to the struct ab8500_chargalg 2357 * @di: pointer to the struct ab8500_chargalg
2358 * 2358 *
2359 * This function adds an entry in sysfs. 2359 * This function adds an entry in sysfs.
2360 * Returns error code in case of failure else 0(on success) 2360 * Returns error code in case of failure else 0(on success)
2361 */ 2361 */
2362 static int ab8500_fg_sysfs_init(struct ab8500_fg *di) 2362 static int ab8500_fg_sysfs_init(struct ab8500_fg *di)
2363 { 2363 {
2364 int ret = 0; 2364 int ret = 0;
2365 2365
2366 ret = kobject_init_and_add(&di->fg_kobject, 2366 ret = kobject_init_and_add(&di->fg_kobject,
2367 &ab8500_fg_ktype, 2367 &ab8500_fg_ktype,
2368 NULL, "battery"); 2368 NULL, "battery");
2369 if (ret < 0) 2369 if (ret < 0)
2370 dev_err(di->dev, "failed to create sysfs entry\n"); 2370 dev_err(di->dev, "failed to create sysfs entry\n");
2371 2371
2372 return ret; 2372 return ret;
2373 } 2373 }
2374 /* Exposure to the sysfs interface <<END>> */ 2374 /* Exposure to the sysfs interface <<END>> */
2375 2375
2376 #if defined(CONFIG_PM) 2376 #if defined(CONFIG_PM)
2377 static int ab8500_fg_resume(struct platform_device *pdev) 2377 static int ab8500_fg_resume(struct platform_device *pdev)
2378 { 2378 {
2379 struct ab8500_fg *di = platform_get_drvdata(pdev); 2379 struct ab8500_fg *di = platform_get_drvdata(pdev);
2380 2380
2381 /* 2381 /*
2382 * Change state if we're not charging. If we're charging we will wake 2382 * Change state if we're not charging. If we're charging we will wake
2383 * up on the FG IRQ 2383 * up on the FG IRQ
2384 */ 2384 */
2385 if (!di->flags.charging) { 2385 if (!di->flags.charging) {
2386 ab8500_fg_discharge_state_to(di, AB8500_FG_DISCHARGE_WAKEUP); 2386 ab8500_fg_discharge_state_to(di, AB8500_FG_DISCHARGE_WAKEUP);
2387 queue_work(di->fg_wq, &di->fg_work); 2387 queue_work(di->fg_wq, &di->fg_work);
2388 } 2388 }
2389 2389
2390 return 0; 2390 return 0;
2391 } 2391 }
2392 2392
2393 static int ab8500_fg_suspend(struct platform_device *pdev, 2393 static int ab8500_fg_suspend(struct platform_device *pdev,
2394 pm_message_t state) 2394 pm_message_t state)
2395 { 2395 {
2396 struct ab8500_fg *di = platform_get_drvdata(pdev); 2396 struct ab8500_fg *di = platform_get_drvdata(pdev);
2397 2397
2398 flush_delayed_work(&di->fg_periodic_work); 2398 flush_delayed_work(&di->fg_periodic_work);
2399 2399
2400 /* 2400 /*
2401 * If the FG is enabled we will disable it before going to suspend 2401 * If the FG is enabled we will disable it before going to suspend
2402 * only if we're not charging 2402 * only if we're not charging
2403 */ 2403 */
2404 if (di->flags.fg_enabled && !di->flags.charging) 2404 if (di->flags.fg_enabled && !di->flags.charging)
2405 ab8500_fg_coulomb_counter(di, false); 2405 ab8500_fg_coulomb_counter(di, false);
2406 2406
2407 return 0; 2407 return 0;
2408 } 2408 }
2409 #else 2409 #else
2410 #define ab8500_fg_suspend NULL 2410 #define ab8500_fg_suspend NULL
2411 #define ab8500_fg_resume NULL 2411 #define ab8500_fg_resume NULL
2412 #endif 2412 #endif
2413 2413
2414 static int __devexit ab8500_fg_remove(struct platform_device *pdev) 2414 static int __devexit ab8500_fg_remove(struct platform_device *pdev)
2415 { 2415 {
2416 int ret = 0; 2416 int ret = 0;
2417 struct ab8500_fg *di = platform_get_drvdata(pdev); 2417 struct ab8500_fg *di = platform_get_drvdata(pdev);
2418 2418
2419 list_del(&di->node); 2419 list_del(&di->node);
2420 2420
2421 /* Disable coulomb counter */ 2421 /* Disable coulomb counter */
2422 ret = ab8500_fg_coulomb_counter(di, false); 2422 ret = ab8500_fg_coulomb_counter(di, false);
2423 if (ret) 2423 if (ret)
2424 dev_err(di->dev, "failed to disable coulomb counter\n"); 2424 dev_err(di->dev, "failed to disable coulomb counter\n");
2425 2425
2426 destroy_workqueue(di->fg_wq); 2426 destroy_workqueue(di->fg_wq);
2427 ab8500_fg_sysfs_exit(di); 2427 ab8500_fg_sysfs_exit(di);
2428 2428
2429 flush_scheduled_work(); 2429 flush_scheduled_work();
2430 power_supply_unregister(&di->fg_psy); 2430 power_supply_unregister(&di->fg_psy);
2431 platform_set_drvdata(pdev, NULL); 2431 platform_set_drvdata(pdev, NULL);
2432 kfree(di); 2432 kfree(di);
2433 return ret; 2433 return ret;
2434 } 2434 }
2435 2435
2436 /* ab8500 fg driver interrupts and their respective isr */ 2436 /* ab8500 fg driver interrupts and their respective isr */
2437 static struct ab8500_fg_interrupts ab8500_fg_irq[] = { 2437 static struct ab8500_fg_interrupts ab8500_fg_irq[] = {
2438 {"NCONV_ACCU", ab8500_fg_cc_convend_handler}, 2438 {"NCONV_ACCU", ab8500_fg_cc_convend_handler},
2439 {"BATT_OVV", ab8500_fg_batt_ovv_handler}, 2439 {"BATT_OVV", ab8500_fg_batt_ovv_handler},
2440 {"LOW_BAT_F", ab8500_fg_lowbatf_handler}, 2440 {"LOW_BAT_F", ab8500_fg_lowbatf_handler},
2441 {"CC_INT_CALIB", ab8500_fg_cc_int_calib_handler}, 2441 {"CC_INT_CALIB", ab8500_fg_cc_int_calib_handler},
2442 {"CCEOC", ab8500_fg_cc_data_end_handler}, 2442 {"CCEOC", ab8500_fg_cc_data_end_handler},
2443 }; 2443 };
2444 2444
2445 static int __devinit ab8500_fg_probe(struct platform_device *pdev) 2445 static int __devinit ab8500_fg_probe(struct platform_device *pdev)
2446 { 2446 {
2447 int i, irq; 2447 int i, irq;
2448 int ret = 0; 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 if (!plat_data) {
2452 kzalloc(sizeof(struct ab8500_fg), GFP_KERNEL); 2453 dev_err(&pdev->dev, "No platform data\n");
2454 return -EINVAL;
2455 }
2456
2457 di = kzalloc(sizeof(*di), GFP_KERNEL);
2453 if (!di) 2458 if (!di)
2454 return -ENOMEM; 2459 return -ENOMEM;
2455 2460
2456 mutex_init(&di->cc_lock); 2461 mutex_init(&di->cc_lock);
2457 2462
2458 /* get parent data */ 2463 /* get parent data */
2459 di->dev = &pdev->dev; 2464 di->dev = &pdev->dev;
2460 di->parent = dev_get_drvdata(pdev->dev.parent); 2465 di->parent = dev_get_drvdata(pdev->dev.parent);
2461 di->gpadc = ab8500_gpadc_get("ab8500-gpadc.0"); 2466 di->gpadc = ab8500_gpadc_get("ab8500-gpadc.0");
2462 2467
2463 /* get fg specific platform data */ 2468 /* get fg specific platform data */
2464 plat_data = pdev->dev.platform_data; 2469 di->pdata = plat_data->fg;
2465 if (!plat_data || !plat_data->fg) { 2470 if (!di->pdata) {
2466 dev_err(di->dev, "no fg platform data supplied\n"); 2471 dev_err(di->dev, "no fg platform data supplied\n");
2467 ret = -EINVAL; 2472 ret = -EINVAL;
2468 goto free_device_info; 2473 goto free_device_info;
2469 } 2474 }
2470 di->pdata = plat_data->fg;
2471 2475
2472 /* get battery specific platform data */ 2476 /* get battery specific platform data */
2473 di->bat = plat_data->battery; 2477 di->bat = plat_data->battery;
2474 if (!di->bat) { 2478 if (!di->bat) {
2475 dev_err(di->dev, "no battery platform data supplied\n"); 2479 dev_err(di->dev, "no battery platform data supplied\n");
2476 ret = -EINVAL; 2480 ret = -EINVAL;
2477 goto free_device_info; 2481 goto free_device_info;
2478 } 2482 }
2479 2483
2480 di->fg_psy.name = "ab8500_fg"; 2484 di->fg_psy.name = "ab8500_fg";
2481 di->fg_psy.type = POWER_SUPPLY_TYPE_BATTERY; 2485 di->fg_psy.type = POWER_SUPPLY_TYPE_BATTERY;
2482 di->fg_psy.properties = ab8500_fg_props; 2486 di->fg_psy.properties = ab8500_fg_props;
2483 di->fg_psy.num_properties = ARRAY_SIZE(ab8500_fg_props); 2487 di->fg_psy.num_properties = ARRAY_SIZE(ab8500_fg_props);
2484 di->fg_psy.get_property = ab8500_fg_get_property; 2488 di->fg_psy.get_property = ab8500_fg_get_property;
2485 di->fg_psy.supplied_to = di->pdata->supplied_to; 2489 di->fg_psy.supplied_to = di->pdata->supplied_to;
2486 di->fg_psy.num_supplicants = di->pdata->num_supplicants; 2490 di->fg_psy.num_supplicants = di->pdata->num_supplicants;
2487 di->fg_psy.external_power_changed = ab8500_fg_external_power_changed; 2491 di->fg_psy.external_power_changed = ab8500_fg_external_power_changed;
2488 2492
2489 di->bat_cap.max_mah_design = MILLI_TO_MICRO * 2493 di->bat_cap.max_mah_design = MILLI_TO_MICRO *
2490 di->bat->bat_type[di->bat->batt_id].charge_full_design; 2494 di->bat->bat_type[di->bat->batt_id].charge_full_design;
2491 2495
2492 di->bat_cap.max_mah = di->bat_cap.max_mah_design; 2496 di->bat_cap.max_mah = di->bat_cap.max_mah_design;
2493 2497
2494 di->vbat_nom = di->bat->bat_type[di->bat->batt_id].nominal_voltage; 2498 di->vbat_nom = di->bat->bat_type[di->bat->batt_id].nominal_voltage;
2495 2499
2496 di->init_capacity = true; 2500 di->init_capacity = true;
2497 2501
2498 ab8500_fg_charge_state_to(di, AB8500_FG_CHARGE_INIT); 2502 ab8500_fg_charge_state_to(di, AB8500_FG_CHARGE_INIT);
2499 ab8500_fg_discharge_state_to(di, AB8500_FG_DISCHARGE_INIT); 2503 ab8500_fg_discharge_state_to(di, AB8500_FG_DISCHARGE_INIT);
2500 2504
2501 /* Create a work queue for running the FG algorithm */ 2505 /* Create a work queue for running the FG algorithm */
2502 di->fg_wq = create_singlethread_workqueue("ab8500_fg_wq"); 2506 di->fg_wq = create_singlethread_workqueue("ab8500_fg_wq");
2503 if (di->fg_wq == NULL) { 2507 if (di->fg_wq == NULL) {
2504 dev_err(di->dev, "failed to create work queue\n"); 2508 dev_err(di->dev, "failed to create work queue\n");
2505 goto free_device_info; 2509 goto free_device_info;
2506 } 2510 }
2507 2511
2508 /* Init work for running the fg algorithm instantly */ 2512 /* Init work for running the fg algorithm instantly */
2509 INIT_WORK(&di->fg_work, ab8500_fg_instant_work); 2513 INIT_WORK(&di->fg_work, ab8500_fg_instant_work);
2510 2514
2511 /* Init work for getting the battery accumulated current */ 2515 /* Init work for getting the battery accumulated current */
2512 INIT_WORK(&di->fg_acc_cur_work, ab8500_fg_acc_cur_work); 2516 INIT_WORK(&di->fg_acc_cur_work, ab8500_fg_acc_cur_work);
2513 2517
2514 /* Init work for reinitialising the fg algorithm */ 2518 /* Init work for reinitialising the fg algorithm */
2515 INIT_DELAYED_WORK_DEFERRABLE(&di->fg_reinit_work, 2519 INIT_DELAYED_WORK_DEFERRABLE(&di->fg_reinit_work,
2516 ab8500_fg_reinit_work); 2520 ab8500_fg_reinit_work);
2517 2521
2518 /* Work delayed Queue to run the state machine */ 2522 /* Work delayed Queue to run the state machine */
2519 INIT_DELAYED_WORK_DEFERRABLE(&di->fg_periodic_work, 2523 INIT_DELAYED_WORK_DEFERRABLE(&di->fg_periodic_work,
2520 ab8500_fg_periodic_work); 2524 ab8500_fg_periodic_work);
2521 2525
2522 /* Work to check low battery condition */ 2526 /* Work to check low battery condition */
2523 INIT_DELAYED_WORK_DEFERRABLE(&di->fg_low_bat_work, 2527 INIT_DELAYED_WORK_DEFERRABLE(&di->fg_low_bat_work,
2524 ab8500_fg_low_bat_work); 2528 ab8500_fg_low_bat_work);
2525 2529
2526 /* Init work for HW failure check */ 2530 /* Init work for HW failure check */
2527 INIT_DELAYED_WORK_DEFERRABLE(&di->fg_check_hw_failure_work, 2531 INIT_DELAYED_WORK_DEFERRABLE(&di->fg_check_hw_failure_work,
2528 ab8500_fg_check_hw_failure_work); 2532 ab8500_fg_check_hw_failure_work);
2529 2533
2530 /* Initialize OVV, and other registers */ 2534 /* Initialize OVV, and other registers */
2531 ret = ab8500_fg_init_hw_registers(di); 2535 ret = ab8500_fg_init_hw_registers(di);
2532 if (ret) { 2536 if (ret) {
2533 dev_err(di->dev, "failed to initialize registers\n"); 2537 dev_err(di->dev, "failed to initialize registers\n");
2534 goto free_inst_curr_wq; 2538 goto free_inst_curr_wq;
2535 } 2539 }
2536 2540
2537 /* Consider battery unknown until we're informed otherwise */ 2541 /* Consider battery unknown until we're informed otherwise */
2538 di->flags.batt_unknown = true; 2542 di->flags.batt_unknown = true;
2539 di->flags.batt_id_received = false; 2543 di->flags.batt_id_received = false;
2540 2544
2541 /* Register FG power supply class */ 2545 /* Register FG power supply class */
2542 ret = power_supply_register(di->dev, &di->fg_psy); 2546 ret = power_supply_register(di->dev, &di->fg_psy);
2543 if (ret) { 2547 if (ret) {
2544 dev_err(di->dev, "failed to register FG psy\n"); 2548 dev_err(di->dev, "failed to register FG psy\n");
2545 goto free_inst_curr_wq; 2549 goto free_inst_curr_wq;
2546 } 2550 }
2547 2551
2548 di->fg_samples = SEC_TO_SAMPLE(di->bat->fg_params->init_timer); 2552 di->fg_samples = SEC_TO_SAMPLE(di->bat->fg_params->init_timer);
2549 ab8500_fg_coulomb_counter(di, true); 2553 ab8500_fg_coulomb_counter(di, true);
2550 2554
2551 /* Initialize completion used to notify completion of inst current */ 2555 /* Initialize completion used to notify completion of inst current */
2552 init_completion(&di->ab8500_fg_complete); 2556 init_completion(&di->ab8500_fg_complete);
2553 2557
2554 /* Register interrupts */ 2558 /* Register interrupts */
2555 for (i = 0; i < ARRAY_SIZE(ab8500_fg_irq); i++) { 2559 for (i = 0; i < ARRAY_SIZE(ab8500_fg_irq); i++) {
2556 irq = platform_get_irq_byname(pdev, ab8500_fg_irq[i].name); 2560 irq = platform_get_irq_byname(pdev, ab8500_fg_irq[i].name);
2557 ret = request_threaded_irq(irq, NULL, ab8500_fg_irq[i].isr, 2561 ret = request_threaded_irq(irq, NULL, ab8500_fg_irq[i].isr,
2558 IRQF_SHARED | IRQF_NO_SUSPEND, 2562 IRQF_SHARED | IRQF_NO_SUSPEND,
2559 ab8500_fg_irq[i].name, di); 2563 ab8500_fg_irq[i].name, di);
2560 2564
2561 if (ret != 0) { 2565 if (ret != 0) {
2562 dev_err(di->dev, "failed to request %s IRQ %d: %d\n" 2566 dev_err(di->dev, "failed to request %s IRQ %d: %d\n"
2563 , ab8500_fg_irq[i].name, irq, ret); 2567 , ab8500_fg_irq[i].name, irq, ret);
2564 goto free_irq; 2568 goto free_irq;
2565 } 2569 }
2566 dev_dbg(di->dev, "Requested %s IRQ %d: %d\n", 2570 dev_dbg(di->dev, "Requested %s IRQ %d: %d\n",
2567 ab8500_fg_irq[i].name, irq, ret); 2571 ab8500_fg_irq[i].name, irq, ret);
2568 } 2572 }
2569 di->irq = platform_get_irq_byname(pdev, "CCEOC"); 2573 di->irq = platform_get_irq_byname(pdev, "CCEOC");
2570 disable_irq(di->irq); 2574 disable_irq(di->irq);
2571 2575
2572 platform_set_drvdata(pdev, di); 2576 platform_set_drvdata(pdev, di);
2573 2577
2574 ret = ab8500_fg_sysfs_init(di); 2578 ret = ab8500_fg_sysfs_init(di);
2575 if (ret) { 2579 if (ret) {
2576 dev_err(di->dev, "failed to create sysfs entry\n"); 2580 dev_err(di->dev, "failed to create sysfs entry\n");
2577 goto free_irq; 2581 goto free_irq;
2578 } 2582 }
2579 2583
2580 /* Calibrate the fg first time */ 2584 /* Calibrate the fg first time */
2581 di->flags.calibrate = true; 2585 di->flags.calibrate = true;
2582 di->calib_state = AB8500_FG_CALIB_INIT; 2586 di->calib_state = AB8500_FG_CALIB_INIT;
2583 2587
2584 /* Use room temp as default value until we get an update from driver. */ 2588 /* Use room temp as default value until we get an update from driver. */
2585 di->bat_temp = 210; 2589 di->bat_temp = 210;
2586 2590
2587 /* Run the FG algorithm */ 2591 /* Run the FG algorithm */
2588 queue_delayed_work(di->fg_wq, &di->fg_periodic_work, 0); 2592 queue_delayed_work(di->fg_wq, &di->fg_periodic_work, 0);
2589 2593
2590 list_add_tail(&di->node, &ab8500_fg_list); 2594 list_add_tail(&di->node, &ab8500_fg_list);
2591 2595
2592 return ret; 2596 return ret;
2593 2597
2594 free_irq: 2598 free_irq:
2595 power_supply_unregister(&di->fg_psy); 2599 power_supply_unregister(&di->fg_psy);
2596 2600
2597 /* We also have to free all successfully registered irqs */ 2601 /* We also have to free all successfully registered irqs */
2598 for (i = i - 1; i >= 0; i--) { 2602 for (i = i - 1; i >= 0; i--) {
2599 irq = platform_get_irq_byname(pdev, ab8500_fg_irq[i].name); 2603 irq = platform_get_irq_byname(pdev, ab8500_fg_irq[i].name);
2600 free_irq(irq, di); 2604 free_irq(irq, di);
2601 } 2605 }
2602 free_inst_curr_wq: 2606 free_inst_curr_wq:
2603 destroy_workqueue(di->fg_wq); 2607 destroy_workqueue(di->fg_wq);
2604 free_device_info: 2608 free_device_info:
2605 kfree(di); 2609 kfree(di);
2606 2610
2607 return ret; 2611 return ret;
2608 } 2612 }
2609 2613
2610 static struct platform_driver ab8500_fg_driver = { 2614 static struct platform_driver ab8500_fg_driver = {
2611 .probe = ab8500_fg_probe, 2615 .probe = ab8500_fg_probe,
2612 .remove = __devexit_p(ab8500_fg_remove), 2616 .remove = __devexit_p(ab8500_fg_remove),
2613 .suspend = ab8500_fg_suspend, 2617 .suspend = ab8500_fg_suspend,
2614 .resume = ab8500_fg_resume, 2618 .resume = ab8500_fg_resume,
2615 .driver = { 2619 .driver = {
2616 .name = "ab8500-fg", 2620 .name = "ab8500-fg",
2617 .owner = THIS_MODULE, 2621 .owner = THIS_MODULE,
2618 }, 2622 },
2619 }; 2623 };
2620 2624
2621 static int __init ab8500_fg_init(void) 2625 static int __init ab8500_fg_init(void)
2622 { 2626 {
2623 return platform_driver_register(&ab8500_fg_driver); 2627 return platform_driver_register(&ab8500_fg_driver);
2624 } 2628 }
2625 2629
2626 static void __exit ab8500_fg_exit(void) 2630 static void __exit ab8500_fg_exit(void)
2627 { 2631 {
2628 platform_driver_unregister(&ab8500_fg_driver); 2632 platform_driver_unregister(&ab8500_fg_driver);
2629 } 2633 }
2630 2634
2631 subsys_initcall_sync(ab8500_fg_init); 2635 subsys_initcall_sync(ab8500_fg_init);
2632 module_exit(ab8500_fg_exit); 2636 module_exit(ab8500_fg_exit);
2633 2637
2634 MODULE_LICENSE("GPL v2"); 2638 MODULE_LICENSE("GPL v2");
2635 MODULE_AUTHOR("Johan Palsson, Karl Komierowski"); 2639 MODULE_AUTHOR("Johan Palsson, Karl Komierowski");
2636 MODULE_ALIAS("platform:ab8500-fg"); 2640 MODULE_ALIAS("platform:ab8500-fg");
2637 MODULE_DESCRIPTION("AB8500 Fuel Gauge driver"); 2641 MODULE_DESCRIPTION("AB8500 Fuel Gauge driver");