1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright (C) ST-Ericsson SA 2012
4  * Copyright (c) 2012 Sony Mobile Communications AB
5  *
6  * Charging algorithm driver for AB8500
7  *
8  * Authors:
9  *	Johan Palsson <johan.palsson@stericsson.com>
10  *	Karl Komierowski <karl.komierowski@stericsson.com>
11  *	Arun R Murthy <arun.murthy@stericsson.com>
12  *	Author: Imre Sunyi <imre.sunyi@sonymobile.com>
13  */
14 
15 #include <linux/init.h>
16 #include <linux/module.h>
17 #include <linux/device.h>
18 #include <linux/component.h>
19 #include <linux/hrtimer.h>
20 #include <linux/interrupt.h>
21 #include <linux/delay.h>
22 #include <linux/slab.h>
23 #include <linux/platform_device.h>
24 #include <linux/power_supply.h>
25 #include <linux/completion.h>
26 #include <linux/workqueue.h>
27 #include <linux/kobject.h>
28 #include <linux/of.h>
29 #include <linux/mfd/core.h>
30 #include <linux/mfd/abx500.h>
31 #include <linux/mfd/abx500/ab8500.h>
32 #include <linux/notifier.h>
33 
34 #include "ab8500-bm.h"
35 #include "ab8500-chargalg.h"
36 
37 /* Watchdog kick interval */
38 #define CHG_WD_INTERVAL			(6 * HZ)
39 
40 /* End-of-charge criteria counter */
41 #define EOC_COND_CNT			10
42 
43 /* One hour expressed in seconds */
44 #define ONE_HOUR_IN_SECONDS            3600
45 
46 /* Five minutes expressed in seconds */
47 #define FIVE_MINUTES_IN_SECONDS        300
48 
49 #define CHARGALG_CURR_STEP_LOW		0
50 #define CHARGALG_CURR_STEP_HIGH	100
51 
52 enum ab8500_chargers {
53 	NO_CHG,
54 	AC_CHG,
55 	USB_CHG,
56 };
57 
58 struct ab8500_chargalg_charger_info {
59 	enum ab8500_chargers conn_chg;
60 	enum ab8500_chargers prev_conn_chg;
61 	enum ab8500_chargers online_chg;
62 	enum ab8500_chargers prev_online_chg;
63 	enum ab8500_chargers charger_type;
64 	bool usb_chg_ok;
65 	bool ac_chg_ok;
66 	int usb_volt;
67 	int usb_curr;
68 	int ac_volt;
69 	int ac_curr;
70 	int usb_vset;
71 	int usb_iset;
72 	int ac_vset;
73 	int ac_iset;
74 };
75 
76 struct ab8500_chargalg_suspension_status {
77 	bool suspended_change;
78 	bool ac_suspended;
79 	bool usb_suspended;
80 };
81 
82 struct ab8500_chargalg_current_step_status {
83 	bool curr_step_change;
84 	int curr_step;
85 };
86 
87 struct ab8500_chargalg_battery_data {
88 	int temp;
89 	int volt;
90 	int avg_curr;
91 	int inst_curr;
92 	int percent;
93 };
94 
95 enum ab8500_chargalg_states {
96 	STATE_HANDHELD_INIT,
97 	STATE_HANDHELD,
98 	STATE_CHG_NOT_OK_INIT,
99 	STATE_CHG_NOT_OK,
100 	STATE_HW_TEMP_PROTECT_INIT,
101 	STATE_HW_TEMP_PROTECT,
102 	STATE_NORMAL_INIT,
103 	STATE_NORMAL,
104 	STATE_WAIT_FOR_RECHARGE_INIT,
105 	STATE_WAIT_FOR_RECHARGE,
106 	STATE_MAINTENANCE_A_INIT,
107 	STATE_MAINTENANCE_A,
108 	STATE_MAINTENANCE_B_INIT,
109 	STATE_MAINTENANCE_B,
110 	STATE_TEMP_UNDEROVER_INIT,
111 	STATE_TEMP_UNDEROVER,
112 	STATE_TEMP_LOWHIGH_INIT,
113 	STATE_TEMP_LOWHIGH,
114 	STATE_SUSPENDED_INIT,
115 	STATE_SUSPENDED,
116 	STATE_OVV_PROTECT_INIT,
117 	STATE_OVV_PROTECT,
118 	STATE_SAFETY_TIMER_EXPIRED_INIT,
119 	STATE_SAFETY_TIMER_EXPIRED,
120 	STATE_BATT_REMOVED_INIT,
121 	STATE_BATT_REMOVED,
122 	STATE_WD_EXPIRED_INIT,
123 	STATE_WD_EXPIRED,
124 };
125 
126 static const char * const states[] = {
127 	"HANDHELD_INIT",
128 	"HANDHELD",
129 	"CHG_NOT_OK_INIT",
130 	"CHG_NOT_OK",
131 	"HW_TEMP_PROTECT_INIT",
132 	"HW_TEMP_PROTECT",
133 	"NORMAL_INIT",
134 	"NORMAL",
135 	"WAIT_FOR_RECHARGE_INIT",
136 	"WAIT_FOR_RECHARGE",
137 	"MAINTENANCE_A_INIT",
138 	"MAINTENANCE_A",
139 	"MAINTENANCE_B_INIT",
140 	"MAINTENANCE_B",
141 	"TEMP_UNDEROVER_INIT",
142 	"TEMP_UNDEROVER",
143 	"TEMP_LOWHIGH_INIT",
144 	"TEMP_LOWHIGH",
145 	"SUSPENDED_INIT",
146 	"SUSPENDED",
147 	"OVV_PROTECT_INIT",
148 	"OVV_PROTECT",
149 	"SAFETY_TIMER_EXPIRED_INIT",
150 	"SAFETY_TIMER_EXPIRED",
151 	"BATT_REMOVED_INIT",
152 	"BATT_REMOVED",
153 	"WD_EXPIRED_INIT",
154 	"WD_EXPIRED",
155 };
156 
157 struct ab8500_chargalg_events {
158 	bool batt_unknown;
159 	bool mainextchnotok;
160 	bool batt_ovv;
161 	bool batt_rem;
162 	bool btemp_underover;
163 	bool btemp_lowhigh;
164 	bool main_thermal_prot;
165 	bool usb_thermal_prot;
166 	bool main_ovv;
167 	bool vbus_ovv;
168 	bool usbchargernotok;
169 	bool safety_timer_expired;
170 	bool maintenance_timer_expired;
171 	bool ac_wd_expired;
172 	bool usb_wd_expired;
173 	bool ac_cv_active;
174 	bool usb_cv_active;
175 	bool vbus_collapsed;
176 };
177 
178 /**
179  * struct ab8500_charge_curr_maximization - Charger maximization parameters
180  * @original_iset:	the non optimized/maximised charger current
181  * @current_iset:	the charging current used at this moment
182  * @test_delta_i:	the delta between the current we want to charge and the
183 			current that is really going into the battery
184  * @condition_cnt:	number of iterations needed before a new charger current
185 			is set
186  * @max_current:	maximum charger current
187  * @wait_cnt:		to avoid too fast current step down in case of charger
188  *			voltage collapse, we insert this delay between step
189  *			down
190  * @level:		tells in how many steps the charging current has been
191 			increased
192  */
193 struct ab8500_charge_curr_maximization {
194 	int original_iset;
195 	int current_iset;
196 	int test_delta_i;
197 	int condition_cnt;
198 	int max_current;
199 	int wait_cnt;
200 	u8 level;
201 };
202 
203 enum maxim_ret {
204 	MAXIM_RET_NOACTION,
205 	MAXIM_RET_CHANGE,
206 	MAXIM_RET_IBAT_TOO_HIGH,
207 };
208 
209 /**
210  * struct ab8500_chargalg - ab8500 Charging algorithm device information
211  * @dev:		pointer to the structure device
212  * @charge_status:	battery operating status
213  * @eoc_cnt:		counter used to determine end-of_charge
214  * @maintenance_chg:	indicate if maintenance charge is active
215  * @t_hyst_norm		temperature hysteresis when the temperature has been
216  *			over or under normal limits
217  * @t_hyst_lowhigh	temperature hysteresis when the temperature has been
218  *			over or under the high or low limits
219  * @charge_state:	current state of the charging algorithm
220  * @ccm			charging current maximization parameters
221  * @chg_info:		information about connected charger types
222  * @batt_data:		data of the battery
223  * @susp_status:	current charger suspension status
224  * @bm:           	Platform specific battery management information
225  * @curr_status:	Current step status for over-current protection
226  * @parent:		pointer to the struct ab8500
227  * @chargalg_psy:	structure that holds the battery properties exposed by
228  *			the charging algorithm
229  * @events:		structure for information about events triggered
230  * @chargalg_wq:		work queue for running the charging algorithm
231  * @chargalg_periodic_work:	work to run the charging algorithm periodically
232  * @chargalg_wd_work:		work to kick the charger watchdog periodically
233  * @chargalg_work:		work to run the charging algorithm instantly
234  * @safety_timer:		charging safety timer
235  * @maintenance_timer:		maintenance charging timer
236  * @chargalg_kobject:		structure of type kobject
237  */
238 struct ab8500_chargalg {
239 	struct device *dev;
240 	int charge_status;
241 	int eoc_cnt;
242 	bool maintenance_chg;
243 	int t_hyst_norm;
244 	int t_hyst_lowhigh;
245 	enum ab8500_chargalg_states charge_state;
246 	struct ab8500_charge_curr_maximization ccm;
247 	struct ab8500_chargalg_charger_info chg_info;
248 	struct ab8500_chargalg_battery_data batt_data;
249 	struct ab8500_chargalg_suspension_status susp_status;
250 	struct ab8500 *parent;
251 	struct ab8500_chargalg_current_step_status curr_status;
252 	struct ab8500_bm_data *bm;
253 	struct power_supply *chargalg_psy;
254 	struct ux500_charger *ac_chg;
255 	struct ux500_charger *usb_chg;
256 	struct ab8500_chargalg_events events;
257 	struct workqueue_struct *chargalg_wq;
258 	struct delayed_work chargalg_periodic_work;
259 	struct delayed_work chargalg_wd_work;
260 	struct work_struct chargalg_work;
261 	struct hrtimer safety_timer;
262 	struct hrtimer maintenance_timer;
263 	struct kobject chargalg_kobject;
264 };
265 
266 /*External charger prepare notifier*/
267 BLOCKING_NOTIFIER_HEAD(charger_notifier_list);
268 
269 /* Main battery properties */
270 static enum power_supply_property ab8500_chargalg_props[] = {
271 	POWER_SUPPLY_PROP_STATUS,
272 	POWER_SUPPLY_PROP_HEALTH,
273 };
274 
275 struct ab8500_chargalg_sysfs_entry {
276 	struct attribute attr;
277 	ssize_t (*show)(struct ab8500_chargalg *di, char *buf);
278 	ssize_t (*store)(struct ab8500_chargalg *di, const char *buf, size_t length);
279 };
280 
281 /**
282  * ab8500_chargalg_safety_timer_expired() - Expiration of the safety timer
283  * @timer:     pointer to the hrtimer structure
284  *
285  * This function gets called when the safety timer for the charger
286  * expires
287  */
288 static enum hrtimer_restart
289 ab8500_chargalg_safety_timer_expired(struct hrtimer *timer)
290 {
291 	struct ab8500_chargalg *di = container_of(timer, struct ab8500_chargalg,
292 						  safety_timer);
293 	dev_err(di->dev, "Safety timer expired\n");
294 	di->events.safety_timer_expired = true;
295 
296 	/* Trigger execution of the algorithm instantly */
297 	queue_work(di->chargalg_wq, &di->chargalg_work);
298 
299 	return HRTIMER_NORESTART;
300 }
301 
302 /**
303  * ab8500_chargalg_maintenance_timer_expired() - Expiration of
304  * the maintenance timer
305  * @timer:     pointer to the timer structure
306  *
307  * This function gets called when the maintenence timer
308  * expires
309  */
310 static enum hrtimer_restart
311 ab8500_chargalg_maintenance_timer_expired(struct hrtimer *timer)
312 {
313 
314 	struct ab8500_chargalg *di = container_of(timer, struct ab8500_chargalg,
315 						  maintenance_timer);
316 
317 	dev_dbg(di->dev, "Maintenance timer expired\n");
318 	di->events.maintenance_timer_expired = true;
319 
320 	/* Trigger execution of the algorithm instantly */
321 	queue_work(di->chargalg_wq, &di->chargalg_work);
322 
323 	return HRTIMER_NORESTART;
324 }
325 
326 /**
327  * ab8500_chargalg_state_to() - Change charge state
328  * @di:		pointer to the ab8500_chargalg structure
329  *
330  * This function gets called when a charge state change should occur
331  */
332 static void ab8500_chargalg_state_to(struct ab8500_chargalg *di,
333 	enum ab8500_chargalg_states state)
334 {
335 	dev_dbg(di->dev,
336 		"State changed: %s (From state: [%d] %s =to=> [%d] %s )\n",
337 		di->charge_state == state ? "NO" : "YES",
338 		di->charge_state,
339 		states[di->charge_state],
340 		state,
341 		states[state]);
342 
343 	di->charge_state = state;
344 }
345 
346 static int ab8500_chargalg_check_charger_enable(struct ab8500_chargalg *di)
347 {
348 	switch (di->charge_state) {
349 	case STATE_NORMAL:
350 	case STATE_MAINTENANCE_A:
351 	case STATE_MAINTENANCE_B:
352 		break;
353 	default:
354 		return 0;
355 	}
356 
357 	if (di->chg_info.charger_type & USB_CHG) {
358 		return di->usb_chg->ops.check_enable(di->usb_chg,
359 			di->bm->bat_type[di->bm->batt_id].normal_vol_lvl,
360 			di->bm->bat_type[di->bm->batt_id].normal_cur_lvl);
361 	} else if ((di->chg_info.charger_type & AC_CHG) &&
362 		   !(di->ac_chg->external)) {
363 		return di->ac_chg->ops.check_enable(di->ac_chg,
364 			di->bm->bat_type[di->bm->batt_id].normal_vol_lvl,
365 			di->bm->bat_type[di->bm->batt_id].normal_cur_lvl);
366 	}
367 	return 0;
368 }
369 
370 /**
371  * ab8500_chargalg_check_charger_connection() - Check charger connection change
372  * @di:		pointer to the ab8500_chargalg structure
373  *
374  * This function will check if there is a change in the charger connection
375  * and change charge state accordingly. AC has precedence over USB.
376  */
377 static int ab8500_chargalg_check_charger_connection(struct ab8500_chargalg *di)
378 {
379 	if (di->chg_info.conn_chg != di->chg_info.prev_conn_chg ||
380 		di->susp_status.suspended_change) {
381 		/*
382 		 * Charger state changed or suspension
383 		 * has changed since last update
384 		 */
385 		if ((di->chg_info.conn_chg & AC_CHG) &&
386 			!di->susp_status.ac_suspended) {
387 			dev_dbg(di->dev, "Charging source is AC\n");
388 			if (di->chg_info.charger_type != AC_CHG) {
389 				di->chg_info.charger_type = AC_CHG;
390 				ab8500_chargalg_state_to(di, STATE_NORMAL_INIT);
391 			}
392 		} else if ((di->chg_info.conn_chg & USB_CHG) &&
393 			!di->susp_status.usb_suspended) {
394 			dev_dbg(di->dev, "Charging source is USB\n");
395 			di->chg_info.charger_type = USB_CHG;
396 			ab8500_chargalg_state_to(di, STATE_NORMAL_INIT);
397 		} else if (di->chg_info.conn_chg &&
398 			(di->susp_status.ac_suspended ||
399 			di->susp_status.usb_suspended)) {
400 			dev_dbg(di->dev, "Charging is suspended\n");
401 			di->chg_info.charger_type = NO_CHG;
402 			ab8500_chargalg_state_to(di, STATE_SUSPENDED_INIT);
403 		} else {
404 			dev_dbg(di->dev, "Charging source is OFF\n");
405 			di->chg_info.charger_type = NO_CHG;
406 			ab8500_chargalg_state_to(di, STATE_HANDHELD_INIT);
407 		}
408 		di->chg_info.prev_conn_chg = di->chg_info.conn_chg;
409 		di->susp_status.suspended_change = false;
410 	}
411 	return di->chg_info.conn_chg;
412 }
413 
414 /**
415  * ab8500_chargalg_check_current_step_status() - Check charging current
416  * step status.
417  * @di:		pointer to the ab8500_chargalg structure
418  *
419  * This function will check if there is a change in the charging current step
420  * and change charge state accordingly.
421  */
422 static void ab8500_chargalg_check_current_step_status
423 	(struct ab8500_chargalg *di)
424 {
425 	if (di->curr_status.curr_step_change)
426 		ab8500_chargalg_state_to(di, STATE_NORMAL_INIT);
427 	di->curr_status.curr_step_change = false;
428 }
429 
430 /**
431  * ab8500_chargalg_start_safety_timer() - Start charging safety timer
432  * @di:		pointer to the ab8500_chargalg structure
433  *
434  * The safety timer is used to avoid overcharging of old or bad batteries.
435  * There are different timers for AC and USB
436  */
437 static void ab8500_chargalg_start_safety_timer(struct ab8500_chargalg *di)
438 {
439 	/* Charger-dependent expiration time in hours*/
440 	int timer_expiration = 0;
441 
442 	switch (di->chg_info.charger_type) {
443 	case AC_CHG:
444 		timer_expiration = di->bm->main_safety_tmr_h;
445 		break;
446 
447 	case USB_CHG:
448 		timer_expiration = di->bm->usb_safety_tmr_h;
449 		break;
450 
451 	default:
452 		dev_err(di->dev, "Unknown charger to charge from\n");
453 		break;
454 	}
455 
456 	di->events.safety_timer_expired = false;
457 	hrtimer_set_expires_range(&di->safety_timer,
458 		ktime_set(timer_expiration * ONE_HOUR_IN_SECONDS, 0),
459 		ktime_set(FIVE_MINUTES_IN_SECONDS, 0));
460 	hrtimer_start_expires(&di->safety_timer, HRTIMER_MODE_REL);
461 }
462 
463 /**
464  * ab8500_chargalg_stop_safety_timer() - Stop charging safety timer
465  * @di:		pointer to the ab8500_chargalg structure
466  *
467  * The safety timer is stopped whenever the NORMAL state is exited
468  */
469 static void ab8500_chargalg_stop_safety_timer(struct ab8500_chargalg *di)
470 {
471 	if (hrtimer_try_to_cancel(&di->safety_timer) >= 0)
472 		di->events.safety_timer_expired = false;
473 }
474 
475 /**
476  * ab8500_chargalg_start_maintenance_timer() - Start charging maintenance timer
477  * @di:		pointer to the ab8500_chargalg structure
478  * @duration:	duration of ther maintenance timer in hours
479  *
480  * The maintenance timer is used to maintain the charge in the battery once
481  * the battery is considered full. These timers are chosen to match the
482  * discharge curve of the battery
483  */
484 static void ab8500_chargalg_start_maintenance_timer(struct ab8500_chargalg *di,
485 	int duration)
486 {
487 	hrtimer_set_expires_range(&di->maintenance_timer,
488 		ktime_set(duration * ONE_HOUR_IN_SECONDS, 0),
489 		ktime_set(FIVE_MINUTES_IN_SECONDS, 0));
490 	di->events.maintenance_timer_expired = false;
491 	hrtimer_start_expires(&di->maintenance_timer, HRTIMER_MODE_REL);
492 }
493 
494 /**
495  * ab8500_chargalg_stop_maintenance_timer() - Stop maintenance timer
496  * @di:		pointer to the ab8500_chargalg structure
497  *
498  * The maintenance timer is stopped whenever maintenance ends or when another
499  * state is entered
500  */
501 static void ab8500_chargalg_stop_maintenance_timer(struct ab8500_chargalg *di)
502 {
503 	if (hrtimer_try_to_cancel(&di->maintenance_timer) >= 0)
504 		di->events.maintenance_timer_expired = false;
505 }
506 
507 /**
508  * ab8500_chargalg_kick_watchdog() - Kick charger watchdog
509  * @di:		pointer to the ab8500_chargalg structure
510  *
511  * The charger watchdog have to be kicked periodically whenever the charger is
512  * on, else the ABB will reset the system
513  */
514 static int ab8500_chargalg_kick_watchdog(struct ab8500_chargalg *di)
515 {
516 	/* Check if charger exists and kick watchdog if charging */
517 	if (di->ac_chg && di->ac_chg->ops.kick_wd &&
518 	    di->chg_info.online_chg & AC_CHG) {
519 		/*
520 		 * If AB charger watchdog expired, pm2xxx charging
521 		 * gets disabled. To be safe, kick both AB charger watchdog
522 		 * and pm2xxx watchdog.
523 		 */
524 		if (di->ac_chg->external &&
525 		    di->usb_chg && di->usb_chg->ops.kick_wd)
526 			di->usb_chg->ops.kick_wd(di->usb_chg);
527 
528 		return di->ac_chg->ops.kick_wd(di->ac_chg);
529 	} else if (di->usb_chg && di->usb_chg->ops.kick_wd &&
530 			di->chg_info.online_chg & USB_CHG)
531 		return di->usb_chg->ops.kick_wd(di->usb_chg);
532 
533 	return -ENXIO;
534 }
535 
536 /**
537  * ab8500_chargalg_ac_en() - Turn on/off the AC charger
538  * @di:		pointer to the ab8500_chargalg structure
539  * @enable:	charger on/off
540  * @vset:	requested charger output voltage
541  * @iset:	requested charger output current
542  *
543  * The AC charger will be turned on/off with the requested charge voltage and
544  * current
545  */
546 static int ab8500_chargalg_ac_en(struct ab8500_chargalg *di, int enable,
547 	int vset, int iset)
548 {
549 	static int ab8500_chargalg_ex_ac_enable_toggle;
550 
551 	if (!di->ac_chg || !di->ac_chg->ops.enable)
552 		return -ENXIO;
553 
554 	/* Select maximum of what both the charger and the battery supports */
555 	if (di->ac_chg->max_out_volt)
556 		vset = min(vset, di->ac_chg->max_out_volt);
557 	if (di->ac_chg->max_out_curr)
558 		iset = min(iset, di->ac_chg->max_out_curr);
559 
560 	di->chg_info.ac_iset = iset;
561 	di->chg_info.ac_vset = vset;
562 
563 	/* Enable external charger */
564 	if (enable && di->ac_chg->external &&
565 	    !ab8500_chargalg_ex_ac_enable_toggle) {
566 		blocking_notifier_call_chain(&charger_notifier_list,
567 					     0, di->dev);
568 		ab8500_chargalg_ex_ac_enable_toggle++;
569 	}
570 
571 	return di->ac_chg->ops.enable(di->ac_chg, enable, vset, iset);
572 }
573 
574 /**
575  * ab8500_chargalg_usb_en() - Turn on/off the USB charger
576  * @di:		pointer to the ab8500_chargalg structure
577  * @enable:	charger on/off
578  * @vset:	requested charger output voltage
579  * @iset:	requested charger output current
580  *
581  * The USB charger will be turned on/off with the requested charge voltage and
582  * current
583  */
584 static int ab8500_chargalg_usb_en(struct ab8500_chargalg *di, int enable,
585 	int vset, int iset)
586 {
587 	if (!di->usb_chg || !di->usb_chg->ops.enable)
588 		return -ENXIO;
589 
590 	/* Select maximum of what both the charger and the battery supports */
591 	if (di->usb_chg->max_out_volt)
592 		vset = min(vset, di->usb_chg->max_out_volt);
593 	if (di->usb_chg->max_out_curr)
594 		iset = min(iset, di->usb_chg->max_out_curr);
595 
596 	di->chg_info.usb_iset = iset;
597 	di->chg_info.usb_vset = vset;
598 
599 	return di->usb_chg->ops.enable(di->usb_chg, enable, vset, iset);
600 }
601 
602 /**
603  * ab8500_chargalg_update_chg_curr() - Update charger current
604  * @di:		pointer to the ab8500_chargalg structure
605  * @iset:	requested charger output current
606  *
607  * The charger output current will be updated for the charger
608  * that is currently in use
609  */
610 static int ab8500_chargalg_update_chg_curr(struct ab8500_chargalg *di,
611 		int iset)
612 {
613 	/* Check if charger exists and update current if charging */
614 	if (di->ac_chg && di->ac_chg->ops.update_curr &&
615 			di->chg_info.charger_type & AC_CHG) {
616 		/*
617 		 * Select maximum of what both the charger
618 		 * and the battery supports
619 		 */
620 		if (di->ac_chg->max_out_curr)
621 			iset = min(iset, di->ac_chg->max_out_curr);
622 
623 		di->chg_info.ac_iset = iset;
624 
625 		return di->ac_chg->ops.update_curr(di->ac_chg, iset);
626 	} else if (di->usb_chg && di->usb_chg->ops.update_curr &&
627 			di->chg_info.charger_type & USB_CHG) {
628 		/*
629 		 * Select maximum of what both the charger
630 		 * and the battery supports
631 		 */
632 		if (di->usb_chg->max_out_curr)
633 			iset = min(iset, di->usb_chg->max_out_curr);
634 
635 		di->chg_info.usb_iset = iset;
636 
637 		return di->usb_chg->ops.update_curr(di->usb_chg, iset);
638 	}
639 
640 	return -ENXIO;
641 }
642 
643 /**
644  * ab8500_chargalg_stop_charging() - Stop charging
645  * @di:		pointer to the ab8500_chargalg structure
646  *
647  * This function is called from any state where charging should be stopped.
648  * All charging is disabled and all status parameters and timers are changed
649  * accordingly
650  */
651 static void ab8500_chargalg_stop_charging(struct ab8500_chargalg *di)
652 {
653 	ab8500_chargalg_ac_en(di, false, 0, 0);
654 	ab8500_chargalg_usb_en(di, false, 0, 0);
655 	ab8500_chargalg_stop_safety_timer(di);
656 	ab8500_chargalg_stop_maintenance_timer(di);
657 	di->charge_status = POWER_SUPPLY_STATUS_NOT_CHARGING;
658 	di->maintenance_chg = false;
659 	cancel_delayed_work(&di->chargalg_wd_work);
660 	power_supply_changed(di->chargalg_psy);
661 }
662 
663 /**
664  * ab8500_chargalg_hold_charging() - Pauses charging
665  * @di:		pointer to the ab8500_chargalg structure
666  *
667  * This function is called in the case where maintenance charging has been
668  * disabled and instead a battery voltage mode is entered to check when the
669  * battery voltage has reached a certain recharge voltage
670  */
671 static void ab8500_chargalg_hold_charging(struct ab8500_chargalg *di)
672 {
673 	ab8500_chargalg_ac_en(di, false, 0, 0);
674 	ab8500_chargalg_usb_en(di, false, 0, 0);
675 	ab8500_chargalg_stop_safety_timer(di);
676 	ab8500_chargalg_stop_maintenance_timer(di);
677 	di->charge_status = POWER_SUPPLY_STATUS_CHARGING;
678 	di->maintenance_chg = false;
679 	cancel_delayed_work(&di->chargalg_wd_work);
680 	power_supply_changed(di->chargalg_psy);
681 }
682 
683 /**
684  * ab8500_chargalg_start_charging() - Start the charger
685  * @di:		pointer to the ab8500_chargalg structure
686  * @vset:	requested charger output voltage
687  * @iset:	requested charger output current
688  *
689  * A charger will be enabled depending on the requested charger type that was
690  * detected previously.
691  */
692 static void ab8500_chargalg_start_charging(struct ab8500_chargalg *di,
693 	int vset, int iset)
694 {
695 	switch (di->chg_info.charger_type) {
696 	case AC_CHG:
697 		dev_dbg(di->dev,
698 			"AC parameters: Vset %d, Ich %d\n", vset, iset);
699 		ab8500_chargalg_usb_en(di, false, 0, 0);
700 		ab8500_chargalg_ac_en(di, true, vset, iset);
701 		break;
702 
703 	case USB_CHG:
704 		dev_dbg(di->dev,
705 			"USB parameters: Vset %d, Ich %d\n", vset, iset);
706 		ab8500_chargalg_ac_en(di, false, 0, 0);
707 		ab8500_chargalg_usb_en(di, true, vset, iset);
708 		break;
709 
710 	default:
711 		dev_err(di->dev, "Unknown charger to charge from\n");
712 		break;
713 	}
714 }
715 
716 /**
717  * ab8500_chargalg_check_temp() - Check battery temperature ranges
718  * @di:		pointer to the ab8500_chargalg structure
719  *
720  * The battery temperature is checked against the predefined limits and the
721  * charge state is changed accordingly
722  */
723 static void ab8500_chargalg_check_temp(struct ab8500_chargalg *di)
724 {
725 	struct power_supply_battery_info *bi = &di->bm->bi;
726 
727 	if (di->batt_data.temp > (bi->temp_alert_min + di->t_hyst_norm) &&
728 		di->batt_data.temp < (bi->temp_alert_max - di->t_hyst_norm)) {
729 		/* Temp OK! */
730 		di->events.btemp_underover = false;
731 		di->events.btemp_lowhigh = false;
732 		di->t_hyst_norm = 0;
733 		di->t_hyst_lowhigh = 0;
734 	} else {
735 		if (((di->batt_data.temp >= bi->temp_alert_max) &&
736 			(di->batt_data.temp <
737 				(bi->temp_max - di->t_hyst_lowhigh))) ||
738 			((di->batt_data.temp >
739 				(bi->temp_min + di->t_hyst_lowhigh)) &&
740 			(di->batt_data.temp <= bi->temp_alert_min))) {
741 			/* TEMP minor!!!!! */
742 			di->events.btemp_underover = false;
743 			di->events.btemp_lowhigh = true;
744 			di->t_hyst_norm = di->bm->temp_hysteresis;
745 			di->t_hyst_lowhigh = 0;
746 		} else if (di->batt_data.temp <= bi->temp_min ||
747 			di->batt_data.temp >= bi->temp_max) {
748 			/* TEMP major!!!!! */
749 			di->events.btemp_underover = true;
750 			di->events.btemp_lowhigh = false;
751 			di->t_hyst_norm = 0;
752 			di->t_hyst_lowhigh = di->bm->temp_hysteresis;
753 		} else {
754 			/* Within hysteresis */
755 			dev_dbg(di->dev, "Within hysteresis limit temp: %d "
756 				"hyst_lowhigh %d, hyst normal %d\n",
757 				di->batt_data.temp, di->t_hyst_lowhigh,
758 				di->t_hyst_norm);
759 		}
760 	}
761 }
762 
763 /**
764  * ab8500_chargalg_check_charger_voltage() - Check charger voltage
765  * @di:		pointer to the ab8500_chargalg structure
766  *
767  * Charger voltage is checked against maximum limit
768  */
769 static void ab8500_chargalg_check_charger_voltage(struct ab8500_chargalg *di)
770 {
771 	if (di->chg_info.usb_volt > di->bm->chg_params->usb_volt_max)
772 		di->chg_info.usb_chg_ok = false;
773 	else
774 		di->chg_info.usb_chg_ok = true;
775 
776 	if (di->chg_info.ac_volt > di->bm->chg_params->ac_volt_max)
777 		di->chg_info.ac_chg_ok = false;
778 	else
779 		di->chg_info.ac_chg_ok = true;
780 
781 }
782 
783 /**
784  * ab8500_chargalg_end_of_charge() - Check if end-of-charge criteria is fulfilled
785  * @di:		pointer to the ab8500_chargalg structure
786  *
787  * End-of-charge criteria is fulfilled when the battery voltage is above a
788  * certain limit and the battery current is below a certain limit for a
789  * predefined number of consecutive seconds. If true, the battery is full
790  */
791 static void ab8500_chargalg_end_of_charge(struct ab8500_chargalg *di)
792 {
793 	if (di->charge_status == POWER_SUPPLY_STATUS_CHARGING &&
794 		di->charge_state == STATE_NORMAL &&
795 		!di->maintenance_chg && (di->batt_data.volt >=
796 		di->bm->bat_type[di->bm->batt_id].termination_vol ||
797 		di->events.usb_cv_active || di->events.ac_cv_active) &&
798 		di->batt_data.avg_curr <
799 		di->bm->bat_type[di->bm->batt_id].termination_curr &&
800 		di->batt_data.avg_curr > 0) {
801 		if (++di->eoc_cnt >= EOC_COND_CNT) {
802 			di->eoc_cnt = 0;
803 			di->charge_status = POWER_SUPPLY_STATUS_FULL;
804 			di->maintenance_chg = true;
805 			dev_dbg(di->dev, "EOC reached!\n");
806 			power_supply_changed(di->chargalg_psy);
807 		} else {
808 			dev_dbg(di->dev,
809 				" EOC limit reached for the %d"
810 				" time, out of %d before EOC\n",
811 				di->eoc_cnt,
812 				EOC_COND_CNT);
813 		}
814 	} else {
815 		di->eoc_cnt = 0;
816 	}
817 }
818 
819 static void init_maxim_chg_curr(struct ab8500_chargalg *di)
820 {
821 	di->ccm.original_iset =
822 		di->bm->bat_type[di->bm->batt_id].normal_cur_lvl;
823 	di->ccm.current_iset =
824 		di->bm->bat_type[di->bm->batt_id].normal_cur_lvl;
825 	di->ccm.test_delta_i = di->bm->maxi->charger_curr_step;
826 	di->ccm.max_current = di->bm->maxi->chg_curr;
827 	di->ccm.condition_cnt = di->bm->maxi->wait_cycles;
828 	di->ccm.level = 0;
829 }
830 
831 /**
832  * ab8500_chargalg_chg_curr_maxim - increases the charger current to
833  *			compensate for the system load
834  * @di		pointer to the ab8500_chargalg structure
835  *
836  * This maximization function is used to raise the charger current to get the
837  * battery current as close to the optimal value as possible. The battery
838  * current during charging is affected by the system load
839  */
840 static enum maxim_ret ab8500_chargalg_chg_curr_maxim(struct ab8500_chargalg *di)
841 {
842 	int delta_i;
843 
844 	if (!di->bm->maxi->ena_maxi)
845 		return MAXIM_RET_NOACTION;
846 
847 	delta_i = di->ccm.original_iset - di->batt_data.inst_curr;
848 
849 	if (di->events.vbus_collapsed) {
850 		dev_dbg(di->dev, "Charger voltage has collapsed %d\n",
851 				di->ccm.wait_cnt);
852 		if (di->ccm.wait_cnt == 0) {
853 			dev_dbg(di->dev, "lowering current\n");
854 			di->ccm.wait_cnt++;
855 			di->ccm.condition_cnt = di->bm->maxi->wait_cycles;
856 			di->ccm.max_current =
857 				di->ccm.current_iset - di->ccm.test_delta_i;
858 			di->ccm.current_iset = di->ccm.max_current;
859 			di->ccm.level--;
860 			return MAXIM_RET_CHANGE;
861 		} else {
862 			dev_dbg(di->dev, "waiting\n");
863 			/* Let's go in here twice before lowering curr again */
864 			di->ccm.wait_cnt = (di->ccm.wait_cnt + 1) % 3;
865 			return MAXIM_RET_NOACTION;
866 		}
867 	}
868 
869 	di->ccm.wait_cnt = 0;
870 
871 	if (di->batt_data.inst_curr > di->ccm.original_iset) {
872 		dev_dbg(di->dev, " Maximization Ibat (%dmA) too high"
873 			" (limit %dmA) (current iset: %dmA)!\n",
874 			di->batt_data.inst_curr, di->ccm.original_iset,
875 			di->ccm.current_iset);
876 
877 		if (di->ccm.current_iset == di->ccm.original_iset)
878 			return MAXIM_RET_NOACTION;
879 
880 		di->ccm.condition_cnt = di->bm->maxi->wait_cycles;
881 		di->ccm.current_iset = di->ccm.original_iset;
882 		di->ccm.level = 0;
883 
884 		return MAXIM_RET_IBAT_TOO_HIGH;
885 	}
886 
887 	if (delta_i > di->ccm.test_delta_i &&
888 		(di->ccm.current_iset + di->ccm.test_delta_i) <
889 		di->ccm.max_current) {
890 		if (di->ccm.condition_cnt-- == 0) {
891 			/* Increse the iset with cco.test_delta_i */
892 			di->ccm.condition_cnt = di->bm->maxi->wait_cycles;
893 			di->ccm.current_iset += di->ccm.test_delta_i;
894 			di->ccm.level++;
895 			dev_dbg(di->dev, " Maximization needed, increase"
896 				" with %d mA to %dmA (Optimal ibat: %d)"
897 				" Level %d\n",
898 				di->ccm.test_delta_i,
899 				di->ccm.current_iset,
900 				di->ccm.original_iset,
901 				di->ccm.level);
902 			return MAXIM_RET_CHANGE;
903 		} else {
904 			return MAXIM_RET_NOACTION;
905 		}
906 	}  else {
907 		di->ccm.condition_cnt = di->bm->maxi->wait_cycles;
908 		return MAXIM_RET_NOACTION;
909 	}
910 }
911 
912 static void handle_maxim_chg_curr(struct ab8500_chargalg *di)
913 {
914 	enum maxim_ret ret;
915 	int result;
916 
917 	ret = ab8500_chargalg_chg_curr_maxim(di);
918 	switch (ret) {
919 	case MAXIM_RET_CHANGE:
920 		result = ab8500_chargalg_update_chg_curr(di,
921 			di->ccm.current_iset);
922 		if (result)
923 			dev_err(di->dev, "failed to set chg curr\n");
924 		break;
925 	case MAXIM_RET_IBAT_TOO_HIGH:
926 		result = ab8500_chargalg_update_chg_curr(di,
927 			di->bm->bat_type[di->bm->batt_id].normal_cur_lvl);
928 		if (result)
929 			dev_err(di->dev, "failed to set chg curr\n");
930 		break;
931 
932 	case MAXIM_RET_NOACTION:
933 	default:
934 		/* Do nothing..*/
935 		break;
936 	}
937 }
938 
939 static int ab8500_chargalg_get_ext_psy_data(struct device *dev, void *data)
940 {
941 	struct power_supply *psy;
942 	struct power_supply *ext = dev_get_drvdata(dev);
943 	const char **supplicants = (const char **)ext->supplied_to;
944 	struct ab8500_chargalg *di;
945 	union power_supply_propval ret;
946 	int j;
947 	bool capacity_updated = false;
948 
949 	psy = (struct power_supply *)data;
950 	di = power_supply_get_drvdata(psy);
951 	/* For all psy where the driver name appears in any supplied_to */
952 	j = match_string(supplicants, ext->num_supplicants, psy->desc->name);
953 	if (j < 0)
954 		return 0;
955 
956 	/*
957 	 *  If external is not registering 'POWER_SUPPLY_PROP_CAPACITY' to its
958 	 * property because of handling that sysfs entry on its own, this is
959 	 * the place to get the battery capacity.
960 	 */
961 	if (!power_supply_get_property(ext, POWER_SUPPLY_PROP_CAPACITY, &ret)) {
962 		di->batt_data.percent = ret.intval;
963 		capacity_updated = true;
964 	}
965 
966 	/* Go through all properties for the psy */
967 	for (j = 0; j < ext->desc->num_properties; j++) {
968 		enum power_supply_property prop;
969 		prop = ext->desc->properties[j];
970 
971 		/*
972 		 * Initialize chargers if not already done.
973 		 * The ab8500_charger*/
974 		if (!di->ac_chg &&
975 			ext->desc->type == POWER_SUPPLY_TYPE_MAINS)
976 			di->ac_chg = psy_to_ux500_charger(ext);
977 		else if (!di->usb_chg &&
978 			ext->desc->type == POWER_SUPPLY_TYPE_USB)
979 			di->usb_chg = psy_to_ux500_charger(ext);
980 
981 		if (power_supply_get_property(ext, prop, &ret))
982 			continue;
983 		switch (prop) {
984 		case POWER_SUPPLY_PROP_PRESENT:
985 			switch (ext->desc->type) {
986 			case POWER_SUPPLY_TYPE_BATTERY:
987 				/* Battery present */
988 				if (ret.intval)
989 					di->events.batt_rem = false;
990 				/* Battery removed */
991 				else
992 					di->events.batt_rem = true;
993 				break;
994 			case POWER_SUPPLY_TYPE_MAINS:
995 				/* AC disconnected */
996 				if (!ret.intval &&
997 					(di->chg_info.conn_chg & AC_CHG)) {
998 					di->chg_info.prev_conn_chg =
999 						di->chg_info.conn_chg;
1000 					di->chg_info.conn_chg &= ~AC_CHG;
1001 				}
1002 				/* AC connected */
1003 				else if (ret.intval &&
1004 					!(di->chg_info.conn_chg & AC_CHG)) {
1005 					di->chg_info.prev_conn_chg =
1006 						di->chg_info.conn_chg;
1007 					di->chg_info.conn_chg |= AC_CHG;
1008 				}
1009 				break;
1010 			case POWER_SUPPLY_TYPE_USB:
1011 				/* USB disconnected */
1012 				if (!ret.intval &&
1013 					(di->chg_info.conn_chg & USB_CHG)) {
1014 					di->chg_info.prev_conn_chg =
1015 						di->chg_info.conn_chg;
1016 					di->chg_info.conn_chg &= ~USB_CHG;
1017 				}
1018 				/* USB connected */
1019 				else if (ret.intval &&
1020 					!(di->chg_info.conn_chg & USB_CHG)) {
1021 					di->chg_info.prev_conn_chg =
1022 						di->chg_info.conn_chg;
1023 					di->chg_info.conn_chg |= USB_CHG;
1024 				}
1025 				break;
1026 			default:
1027 				break;
1028 			}
1029 			break;
1030 
1031 		case POWER_SUPPLY_PROP_ONLINE:
1032 			switch (ext->desc->type) {
1033 			case POWER_SUPPLY_TYPE_BATTERY:
1034 				break;
1035 			case POWER_SUPPLY_TYPE_MAINS:
1036 				/* AC offline */
1037 				if (!ret.intval &&
1038 					(di->chg_info.online_chg & AC_CHG)) {
1039 					di->chg_info.prev_online_chg =
1040 						di->chg_info.online_chg;
1041 					di->chg_info.online_chg &= ~AC_CHG;
1042 				}
1043 				/* AC online */
1044 				else if (ret.intval &&
1045 					!(di->chg_info.online_chg & AC_CHG)) {
1046 					di->chg_info.prev_online_chg =
1047 						di->chg_info.online_chg;
1048 					di->chg_info.online_chg |= AC_CHG;
1049 					queue_delayed_work(di->chargalg_wq,
1050 						&di->chargalg_wd_work, 0);
1051 				}
1052 				break;
1053 			case POWER_SUPPLY_TYPE_USB:
1054 				/* USB offline */
1055 				if (!ret.intval &&
1056 					(di->chg_info.online_chg & USB_CHG)) {
1057 					di->chg_info.prev_online_chg =
1058 						di->chg_info.online_chg;
1059 					di->chg_info.online_chg &= ~USB_CHG;
1060 				}
1061 				/* USB online */
1062 				else if (ret.intval &&
1063 					!(di->chg_info.online_chg & USB_CHG)) {
1064 					di->chg_info.prev_online_chg =
1065 						di->chg_info.online_chg;
1066 					di->chg_info.online_chg |= USB_CHG;
1067 					queue_delayed_work(di->chargalg_wq,
1068 						&di->chargalg_wd_work, 0);
1069 				}
1070 				break;
1071 			default:
1072 				break;
1073 			}
1074 			break;
1075 
1076 		case POWER_SUPPLY_PROP_HEALTH:
1077 			switch (ext->desc->type) {
1078 			case POWER_SUPPLY_TYPE_BATTERY:
1079 				break;
1080 			case POWER_SUPPLY_TYPE_MAINS:
1081 				switch (ret.intval) {
1082 				case POWER_SUPPLY_HEALTH_UNSPEC_FAILURE:
1083 					di->events.mainextchnotok = true;
1084 					di->events.main_thermal_prot = false;
1085 					di->events.main_ovv = false;
1086 					di->events.ac_wd_expired = false;
1087 					break;
1088 				case POWER_SUPPLY_HEALTH_DEAD:
1089 					di->events.ac_wd_expired = true;
1090 					di->events.mainextchnotok = false;
1091 					di->events.main_ovv = false;
1092 					di->events.main_thermal_prot = false;
1093 					break;
1094 				case POWER_SUPPLY_HEALTH_COLD:
1095 				case POWER_SUPPLY_HEALTH_OVERHEAT:
1096 					di->events.main_thermal_prot = true;
1097 					di->events.mainextchnotok = false;
1098 					di->events.main_ovv = false;
1099 					di->events.ac_wd_expired = false;
1100 					break;
1101 				case POWER_SUPPLY_HEALTH_OVERVOLTAGE:
1102 					di->events.main_ovv = true;
1103 					di->events.mainextchnotok = false;
1104 					di->events.main_thermal_prot = false;
1105 					di->events.ac_wd_expired = false;
1106 					break;
1107 				case POWER_SUPPLY_HEALTH_GOOD:
1108 					di->events.main_thermal_prot = false;
1109 					di->events.mainextchnotok = false;
1110 					di->events.main_ovv = false;
1111 					di->events.ac_wd_expired = false;
1112 					break;
1113 				default:
1114 					break;
1115 				}
1116 				break;
1117 
1118 			case POWER_SUPPLY_TYPE_USB:
1119 				switch (ret.intval) {
1120 				case POWER_SUPPLY_HEALTH_UNSPEC_FAILURE:
1121 					di->events.usbchargernotok = true;
1122 					di->events.usb_thermal_prot = false;
1123 					di->events.vbus_ovv = false;
1124 					di->events.usb_wd_expired = false;
1125 					break;
1126 				case POWER_SUPPLY_HEALTH_DEAD:
1127 					di->events.usb_wd_expired = true;
1128 					di->events.usbchargernotok = false;
1129 					di->events.usb_thermal_prot = false;
1130 					di->events.vbus_ovv = false;
1131 					break;
1132 				case POWER_SUPPLY_HEALTH_COLD:
1133 				case POWER_SUPPLY_HEALTH_OVERHEAT:
1134 					di->events.usb_thermal_prot = true;
1135 					di->events.usbchargernotok = false;
1136 					di->events.vbus_ovv = false;
1137 					di->events.usb_wd_expired = false;
1138 					break;
1139 				case POWER_SUPPLY_HEALTH_OVERVOLTAGE:
1140 					di->events.vbus_ovv = true;
1141 					di->events.usbchargernotok = false;
1142 					di->events.usb_thermal_prot = false;
1143 					di->events.usb_wd_expired = false;
1144 					break;
1145 				case POWER_SUPPLY_HEALTH_GOOD:
1146 					di->events.usbchargernotok = false;
1147 					di->events.usb_thermal_prot = false;
1148 					di->events.vbus_ovv = false;
1149 					di->events.usb_wd_expired = false;
1150 					break;
1151 				default:
1152 					break;
1153 				}
1154 				break;
1155 			default:
1156 				break;
1157 			}
1158 			break;
1159 
1160 		case POWER_SUPPLY_PROP_VOLTAGE_NOW:
1161 			switch (ext->desc->type) {
1162 			case POWER_SUPPLY_TYPE_BATTERY:
1163 				di->batt_data.volt = ret.intval / 1000;
1164 				break;
1165 			case POWER_SUPPLY_TYPE_MAINS:
1166 				di->chg_info.ac_volt = ret.intval / 1000;
1167 				break;
1168 			case POWER_SUPPLY_TYPE_USB:
1169 				di->chg_info.usb_volt = ret.intval / 1000;
1170 				break;
1171 			default:
1172 				break;
1173 			}
1174 			break;
1175 
1176 		case POWER_SUPPLY_PROP_VOLTAGE_AVG:
1177 			switch (ext->desc->type) {
1178 			case POWER_SUPPLY_TYPE_MAINS:
1179 				/* AVG is used to indicate when we are
1180 				 * in CV mode */
1181 				if (ret.intval)
1182 					di->events.ac_cv_active = true;
1183 				else
1184 					di->events.ac_cv_active = false;
1185 
1186 				break;
1187 			case POWER_SUPPLY_TYPE_USB:
1188 				/* AVG is used to indicate when we are
1189 				 * in CV mode */
1190 				if (ret.intval)
1191 					di->events.usb_cv_active = true;
1192 				else
1193 					di->events.usb_cv_active = false;
1194 
1195 				break;
1196 			default:
1197 				break;
1198 			}
1199 			break;
1200 
1201 		case POWER_SUPPLY_PROP_TECHNOLOGY:
1202 			switch (ext->desc->type) {
1203 			case POWER_SUPPLY_TYPE_BATTERY:
1204 				if (ret.intval)
1205 					di->events.batt_unknown = false;
1206 				else
1207 					di->events.batt_unknown = true;
1208 
1209 				break;
1210 			default:
1211 				break;
1212 			}
1213 			break;
1214 
1215 		case POWER_SUPPLY_PROP_TEMP:
1216 			di->batt_data.temp = ret.intval / 10;
1217 			break;
1218 
1219 		case POWER_SUPPLY_PROP_CURRENT_NOW:
1220 			switch (ext->desc->type) {
1221 			case POWER_SUPPLY_TYPE_MAINS:
1222 					di->chg_info.ac_curr =
1223 						ret.intval / 1000;
1224 					break;
1225 			case POWER_SUPPLY_TYPE_USB:
1226 					di->chg_info.usb_curr =
1227 						ret.intval / 1000;
1228 				break;
1229 			case POWER_SUPPLY_TYPE_BATTERY:
1230 				di->batt_data.inst_curr = ret.intval / 1000;
1231 				break;
1232 			default:
1233 				break;
1234 			}
1235 			break;
1236 
1237 		case POWER_SUPPLY_PROP_CURRENT_AVG:
1238 			switch (ext->desc->type) {
1239 			case POWER_SUPPLY_TYPE_BATTERY:
1240 				di->batt_data.avg_curr = ret.intval / 1000;
1241 				break;
1242 			case POWER_SUPPLY_TYPE_USB:
1243 				if (ret.intval)
1244 					di->events.vbus_collapsed = true;
1245 				else
1246 					di->events.vbus_collapsed = false;
1247 				break;
1248 			default:
1249 				break;
1250 			}
1251 			break;
1252 		case POWER_SUPPLY_PROP_CAPACITY:
1253 			if (!capacity_updated)
1254 				di->batt_data.percent = ret.intval;
1255 			break;
1256 		default:
1257 			break;
1258 		}
1259 	}
1260 	return 0;
1261 }
1262 
1263 /**
1264  * ab8500_chargalg_external_power_changed() - callback for power supply changes
1265  * @psy:       pointer to the structure power_supply
1266  *
1267  * This function is the entry point of the pointer external_power_changed
1268  * of the structure power_supply.
1269  * This function gets executed when there is a change in any external power
1270  * supply that this driver needs to be notified of.
1271  */
1272 static void ab8500_chargalg_external_power_changed(struct power_supply *psy)
1273 {
1274 	struct ab8500_chargalg *di = power_supply_get_drvdata(psy);
1275 
1276 	/*
1277 	 * Trigger execution of the algorithm instantly and read
1278 	 * all power_supply properties there instead
1279 	 */
1280 	if (di->chargalg_wq)
1281 		queue_work(di->chargalg_wq, &di->chargalg_work);
1282 }
1283 
1284 /**
1285  * ab8500_chargalg_algorithm() - Main function for the algorithm
1286  * @di:		pointer to the ab8500_chargalg structure
1287  *
1288  * This is the main control function for the charging algorithm.
1289  * It is called periodically or when something happens that will
1290  * trigger a state change
1291  */
1292 static void ab8500_chargalg_algorithm(struct ab8500_chargalg *di)
1293 {
1294 	int charger_status;
1295 	int ret;
1296 	int curr_step_lvl;
1297 
1298 	/* Collect data from all power_supply class devices */
1299 	class_for_each_device(power_supply_class, NULL,
1300 		di->chargalg_psy, ab8500_chargalg_get_ext_psy_data);
1301 
1302 	ab8500_chargalg_end_of_charge(di);
1303 	ab8500_chargalg_check_temp(di);
1304 	ab8500_chargalg_check_charger_voltage(di);
1305 
1306 	charger_status = ab8500_chargalg_check_charger_connection(di);
1307 	ab8500_chargalg_check_current_step_status(di);
1308 
1309 	if (is_ab8500(di->parent)) {
1310 		ret = ab8500_chargalg_check_charger_enable(di);
1311 		if (ret < 0)
1312 			dev_err(di->dev, "Checking charger is enabled error"
1313 					": Returned Value %d\n", ret);
1314 	}
1315 
1316 	/*
1317 	 * First check if we have a charger connected.
1318 	 * Also we don't allow charging of unknown batteries if configured
1319 	 * this way
1320 	 */
1321 	if (!charger_status ||
1322 		(di->events.batt_unknown && !di->bm->chg_unknown_bat)) {
1323 		if (di->charge_state != STATE_HANDHELD) {
1324 			di->events.safety_timer_expired = false;
1325 			ab8500_chargalg_state_to(di, STATE_HANDHELD_INIT);
1326 		}
1327 	}
1328 
1329 	/* If suspended, we should not continue checking the flags */
1330 	else if (di->charge_state == STATE_SUSPENDED_INIT ||
1331 		di->charge_state == STATE_SUSPENDED) {
1332 		/* We don't do anything here, just don,t continue */
1333 	}
1334 
1335 	/* Safety timer expiration */
1336 	else if (di->events.safety_timer_expired) {
1337 		if (di->charge_state != STATE_SAFETY_TIMER_EXPIRED)
1338 			ab8500_chargalg_state_to(di,
1339 				STATE_SAFETY_TIMER_EXPIRED_INIT);
1340 	}
1341 	/*
1342 	 * Check if any interrupts has occured
1343 	 * that will prevent us from charging
1344 	 */
1345 
1346 	/* Battery removed */
1347 	else if (di->events.batt_rem) {
1348 		if (di->charge_state != STATE_BATT_REMOVED)
1349 			ab8500_chargalg_state_to(di, STATE_BATT_REMOVED_INIT);
1350 	}
1351 	/* Main or USB charger not ok. */
1352 	else if (di->events.mainextchnotok || di->events.usbchargernotok) {
1353 		/*
1354 		 * If vbus_collapsed is set, we have to lower the charger
1355 		 * current, which is done in the normal state below
1356 		 */
1357 		if (di->charge_state != STATE_CHG_NOT_OK &&
1358 				!di->events.vbus_collapsed)
1359 			ab8500_chargalg_state_to(di, STATE_CHG_NOT_OK_INIT);
1360 	}
1361 	/* VBUS, Main or VBAT OVV. */
1362 	else if (di->events.vbus_ovv ||
1363 			di->events.main_ovv ||
1364 			di->events.batt_ovv ||
1365 			!di->chg_info.usb_chg_ok ||
1366 			!di->chg_info.ac_chg_ok) {
1367 		if (di->charge_state != STATE_OVV_PROTECT)
1368 			ab8500_chargalg_state_to(di, STATE_OVV_PROTECT_INIT);
1369 	}
1370 	/* USB Thermal, stop charging */
1371 	else if (di->events.main_thermal_prot ||
1372 		di->events.usb_thermal_prot) {
1373 		if (di->charge_state != STATE_HW_TEMP_PROTECT)
1374 			ab8500_chargalg_state_to(di,
1375 				STATE_HW_TEMP_PROTECT_INIT);
1376 	}
1377 	/* Battery temp over/under */
1378 	else if (di->events.btemp_underover) {
1379 		if (di->charge_state != STATE_TEMP_UNDEROVER)
1380 			ab8500_chargalg_state_to(di,
1381 				STATE_TEMP_UNDEROVER_INIT);
1382 	}
1383 	/* Watchdog expired */
1384 	else if (di->events.ac_wd_expired ||
1385 		di->events.usb_wd_expired) {
1386 		if (di->charge_state != STATE_WD_EXPIRED)
1387 			ab8500_chargalg_state_to(di, STATE_WD_EXPIRED_INIT);
1388 	}
1389 	/* Battery temp high/low */
1390 	else if (di->events.btemp_lowhigh) {
1391 		if (di->charge_state != STATE_TEMP_LOWHIGH)
1392 			ab8500_chargalg_state_to(di, STATE_TEMP_LOWHIGH_INIT);
1393 	}
1394 
1395 	dev_dbg(di->dev,
1396 		"[CHARGALG] Vb %d Ib_avg %d Ib_inst %d Tb %d Cap %d Maint %d "
1397 		"State %s Active_chg %d Chg_status %d AC %d USB %d "
1398 		"AC_online %d USB_online %d AC_CV %d USB_CV %d AC_I %d "
1399 		"USB_I %d AC_Vset %d AC_Iset %d USB_Vset %d USB_Iset %d\n",
1400 		di->batt_data.volt,
1401 		di->batt_data.avg_curr,
1402 		di->batt_data.inst_curr,
1403 		di->batt_data.temp,
1404 		di->batt_data.percent,
1405 		di->maintenance_chg,
1406 		states[di->charge_state],
1407 		di->chg_info.charger_type,
1408 		di->charge_status,
1409 		di->chg_info.conn_chg & AC_CHG,
1410 		di->chg_info.conn_chg & USB_CHG,
1411 		di->chg_info.online_chg & AC_CHG,
1412 		di->chg_info.online_chg & USB_CHG,
1413 		di->events.ac_cv_active,
1414 		di->events.usb_cv_active,
1415 		di->chg_info.ac_curr,
1416 		di->chg_info.usb_curr,
1417 		di->chg_info.ac_vset,
1418 		di->chg_info.ac_iset,
1419 		di->chg_info.usb_vset,
1420 		di->chg_info.usb_iset);
1421 
1422 	switch (di->charge_state) {
1423 	case STATE_HANDHELD_INIT:
1424 		ab8500_chargalg_stop_charging(di);
1425 		di->charge_status = POWER_SUPPLY_STATUS_DISCHARGING;
1426 		ab8500_chargalg_state_to(di, STATE_HANDHELD);
1427 		fallthrough;
1428 
1429 	case STATE_HANDHELD:
1430 		break;
1431 
1432 	case STATE_SUSPENDED_INIT:
1433 		if (di->susp_status.ac_suspended)
1434 			ab8500_chargalg_ac_en(di, false, 0, 0);
1435 		if (di->susp_status.usb_suspended)
1436 			ab8500_chargalg_usb_en(di, false, 0, 0);
1437 		ab8500_chargalg_stop_safety_timer(di);
1438 		ab8500_chargalg_stop_maintenance_timer(di);
1439 		di->charge_status = POWER_SUPPLY_STATUS_NOT_CHARGING;
1440 		di->maintenance_chg = false;
1441 		ab8500_chargalg_state_to(di, STATE_SUSPENDED);
1442 		power_supply_changed(di->chargalg_psy);
1443 		fallthrough;
1444 
1445 	case STATE_SUSPENDED:
1446 		/* CHARGING is suspended */
1447 		break;
1448 
1449 	case STATE_BATT_REMOVED_INIT:
1450 		ab8500_chargalg_stop_charging(di);
1451 		ab8500_chargalg_state_to(di, STATE_BATT_REMOVED);
1452 		fallthrough;
1453 
1454 	case STATE_BATT_REMOVED:
1455 		if (!di->events.batt_rem)
1456 			ab8500_chargalg_state_to(di, STATE_NORMAL_INIT);
1457 		break;
1458 
1459 	case STATE_HW_TEMP_PROTECT_INIT:
1460 		ab8500_chargalg_stop_charging(di);
1461 		ab8500_chargalg_state_to(di, STATE_HW_TEMP_PROTECT);
1462 		fallthrough;
1463 
1464 	case STATE_HW_TEMP_PROTECT:
1465 		if (!di->events.main_thermal_prot &&
1466 				!di->events.usb_thermal_prot)
1467 			ab8500_chargalg_state_to(di, STATE_NORMAL_INIT);
1468 		break;
1469 
1470 	case STATE_OVV_PROTECT_INIT:
1471 		ab8500_chargalg_stop_charging(di);
1472 		ab8500_chargalg_state_to(di, STATE_OVV_PROTECT);
1473 		fallthrough;
1474 
1475 	case STATE_OVV_PROTECT:
1476 		if (!di->events.vbus_ovv &&
1477 				!di->events.main_ovv &&
1478 				!di->events.batt_ovv &&
1479 				di->chg_info.usb_chg_ok &&
1480 				di->chg_info.ac_chg_ok)
1481 			ab8500_chargalg_state_to(di, STATE_NORMAL_INIT);
1482 		break;
1483 
1484 	case STATE_CHG_NOT_OK_INIT:
1485 		ab8500_chargalg_stop_charging(di);
1486 		ab8500_chargalg_state_to(di, STATE_CHG_NOT_OK);
1487 		fallthrough;
1488 
1489 	case STATE_CHG_NOT_OK:
1490 		if (!di->events.mainextchnotok &&
1491 				!di->events.usbchargernotok)
1492 			ab8500_chargalg_state_to(di, STATE_NORMAL_INIT);
1493 		break;
1494 
1495 	case STATE_SAFETY_TIMER_EXPIRED_INIT:
1496 		ab8500_chargalg_stop_charging(di);
1497 		ab8500_chargalg_state_to(di, STATE_SAFETY_TIMER_EXPIRED);
1498 		fallthrough;
1499 
1500 	case STATE_SAFETY_TIMER_EXPIRED:
1501 		/* We exit this state when charger is removed */
1502 		break;
1503 
1504 	case STATE_NORMAL_INIT:
1505 		if (di->curr_status.curr_step == CHARGALG_CURR_STEP_LOW)
1506 			ab8500_chargalg_stop_charging(di);
1507 		else {
1508 			curr_step_lvl = di->bm->bat_type[
1509 				di->bm->batt_id].normal_cur_lvl
1510 				* di->curr_status.curr_step
1511 				/ CHARGALG_CURR_STEP_HIGH;
1512 			ab8500_chargalg_start_charging(di,
1513 				di->bm->bat_type[di->bm->batt_id]
1514 				.normal_vol_lvl, curr_step_lvl);
1515 		}
1516 
1517 		ab8500_chargalg_state_to(di, STATE_NORMAL);
1518 		ab8500_chargalg_start_safety_timer(di);
1519 		ab8500_chargalg_stop_maintenance_timer(di);
1520 		init_maxim_chg_curr(di);
1521 		di->charge_status = POWER_SUPPLY_STATUS_CHARGING;
1522 		di->eoc_cnt = 0;
1523 		di->maintenance_chg = false;
1524 		power_supply_changed(di->chargalg_psy);
1525 
1526 		break;
1527 
1528 	case STATE_NORMAL:
1529 		handle_maxim_chg_curr(di);
1530 		if (di->charge_status == POWER_SUPPLY_STATUS_FULL &&
1531 			di->maintenance_chg) {
1532 			if (di->bm->no_maintenance)
1533 				ab8500_chargalg_state_to(di,
1534 					STATE_WAIT_FOR_RECHARGE_INIT);
1535 			else
1536 				ab8500_chargalg_state_to(di,
1537 					STATE_MAINTENANCE_A_INIT);
1538 		}
1539 		break;
1540 
1541 	/* This state will be used when the maintenance state is disabled */
1542 	case STATE_WAIT_FOR_RECHARGE_INIT:
1543 		ab8500_chargalg_hold_charging(di);
1544 		ab8500_chargalg_state_to(di, STATE_WAIT_FOR_RECHARGE);
1545 		fallthrough;
1546 
1547 	case STATE_WAIT_FOR_RECHARGE:
1548 		if (di->batt_data.percent <=
1549 		    di->bm->bat_type[di->bm->batt_id].recharge_cap)
1550 			ab8500_chargalg_state_to(di, STATE_NORMAL_INIT);
1551 		break;
1552 
1553 	case STATE_MAINTENANCE_A_INIT:
1554 		ab8500_chargalg_stop_safety_timer(di);
1555 		ab8500_chargalg_start_maintenance_timer(di,
1556 			di->bm->bat_type[
1557 				di->bm->batt_id].maint_a_chg_timer_h);
1558 		ab8500_chargalg_start_charging(di,
1559 			di->bm->bat_type[
1560 				di->bm->batt_id].maint_a_vol_lvl,
1561 			di->bm->bat_type[
1562 				di->bm->batt_id].maint_a_cur_lvl);
1563 		ab8500_chargalg_state_to(di, STATE_MAINTENANCE_A);
1564 		power_supply_changed(di->chargalg_psy);
1565 		fallthrough;
1566 
1567 	case STATE_MAINTENANCE_A:
1568 		if (di->events.maintenance_timer_expired) {
1569 			ab8500_chargalg_stop_maintenance_timer(di);
1570 			ab8500_chargalg_state_to(di, STATE_MAINTENANCE_B_INIT);
1571 		}
1572 		break;
1573 
1574 	case STATE_MAINTENANCE_B_INIT:
1575 		ab8500_chargalg_start_maintenance_timer(di,
1576 			di->bm->bat_type[
1577 				di->bm->batt_id].maint_b_chg_timer_h);
1578 		ab8500_chargalg_start_charging(di,
1579 			di->bm->bat_type[
1580 				di->bm->batt_id].maint_b_vol_lvl,
1581 			di->bm->bat_type[
1582 				di->bm->batt_id].maint_b_cur_lvl);
1583 		ab8500_chargalg_state_to(di, STATE_MAINTENANCE_B);
1584 		power_supply_changed(di->chargalg_psy);
1585 		fallthrough;
1586 
1587 	case STATE_MAINTENANCE_B:
1588 		if (di->events.maintenance_timer_expired) {
1589 			ab8500_chargalg_stop_maintenance_timer(di);
1590 			ab8500_chargalg_state_to(di, STATE_NORMAL_INIT);
1591 		}
1592 		break;
1593 
1594 	case STATE_TEMP_LOWHIGH_INIT:
1595 		ab8500_chargalg_start_charging(di,
1596 			di->bm->bat_type[
1597 				di->bm->batt_id].low_high_vol_lvl,
1598 			di->bm->bat_type[
1599 				di->bm->batt_id].low_high_cur_lvl);
1600 		ab8500_chargalg_stop_maintenance_timer(di);
1601 		di->charge_status = POWER_SUPPLY_STATUS_CHARGING;
1602 		ab8500_chargalg_state_to(di, STATE_TEMP_LOWHIGH);
1603 		power_supply_changed(di->chargalg_psy);
1604 		fallthrough;
1605 
1606 	case STATE_TEMP_LOWHIGH:
1607 		if (!di->events.btemp_lowhigh)
1608 			ab8500_chargalg_state_to(di, STATE_NORMAL_INIT);
1609 		break;
1610 
1611 	case STATE_WD_EXPIRED_INIT:
1612 		ab8500_chargalg_stop_charging(di);
1613 		ab8500_chargalg_state_to(di, STATE_WD_EXPIRED);
1614 		fallthrough;
1615 
1616 	case STATE_WD_EXPIRED:
1617 		if (!di->events.ac_wd_expired &&
1618 				!di->events.usb_wd_expired)
1619 			ab8500_chargalg_state_to(di, STATE_NORMAL_INIT);
1620 		break;
1621 
1622 	case STATE_TEMP_UNDEROVER_INIT:
1623 		ab8500_chargalg_stop_charging(di);
1624 		ab8500_chargalg_state_to(di, STATE_TEMP_UNDEROVER);
1625 		fallthrough;
1626 
1627 	case STATE_TEMP_UNDEROVER:
1628 		if (!di->events.btemp_underover)
1629 			ab8500_chargalg_state_to(di, STATE_NORMAL_INIT);
1630 		break;
1631 	}
1632 
1633 	/* Start charging directly if the new state is a charge state */
1634 	if (di->charge_state == STATE_NORMAL_INIT ||
1635 			di->charge_state == STATE_MAINTENANCE_A_INIT ||
1636 			di->charge_state == STATE_MAINTENANCE_B_INIT)
1637 		queue_work(di->chargalg_wq, &di->chargalg_work);
1638 }
1639 
1640 /**
1641  * ab8500_chargalg_periodic_work() - Periodic work for the algorithm
1642  * @work:	pointer to the work_struct structure
1643  *
1644  * Work queue function for the charging algorithm
1645  */
1646 static void ab8500_chargalg_periodic_work(struct work_struct *work)
1647 {
1648 	struct ab8500_chargalg *di = container_of(work,
1649 		struct ab8500_chargalg, chargalg_periodic_work.work);
1650 
1651 	ab8500_chargalg_algorithm(di);
1652 
1653 	/*
1654 	 * If a charger is connected then the battery has to be monitored
1655 	 * frequently, else the work can be delayed.
1656 	 */
1657 	if (di->chg_info.conn_chg)
1658 		queue_delayed_work(di->chargalg_wq,
1659 			&di->chargalg_periodic_work,
1660 			di->bm->interval_charging * HZ);
1661 	else
1662 		queue_delayed_work(di->chargalg_wq,
1663 			&di->chargalg_periodic_work,
1664 			di->bm->interval_not_charging * HZ);
1665 }
1666 
1667 /**
1668  * ab8500_chargalg_wd_work() - periodic work to kick the charger watchdog
1669  * @work:	pointer to the work_struct structure
1670  *
1671  * Work queue function for kicking the charger watchdog
1672  */
1673 static void ab8500_chargalg_wd_work(struct work_struct *work)
1674 {
1675 	int ret;
1676 	struct ab8500_chargalg *di = container_of(work,
1677 		struct ab8500_chargalg, chargalg_wd_work.work);
1678 
1679 	ret = ab8500_chargalg_kick_watchdog(di);
1680 	if (ret < 0)
1681 		dev_err(di->dev, "failed to kick watchdog\n");
1682 
1683 	queue_delayed_work(di->chargalg_wq,
1684 		&di->chargalg_wd_work, CHG_WD_INTERVAL);
1685 }
1686 
1687 /**
1688  * ab8500_chargalg_work() - Work to run the charging algorithm instantly
1689  * @work:	pointer to the work_struct structure
1690  *
1691  * Work queue function for calling the charging algorithm
1692  */
1693 static void ab8500_chargalg_work(struct work_struct *work)
1694 {
1695 	struct ab8500_chargalg *di = container_of(work,
1696 		struct ab8500_chargalg, chargalg_work);
1697 
1698 	ab8500_chargalg_algorithm(di);
1699 }
1700 
1701 /**
1702  * ab8500_chargalg_get_property() - get the chargalg properties
1703  * @psy:	pointer to the power_supply structure
1704  * @psp:	pointer to the power_supply_property structure
1705  * @val:	pointer to the power_supply_propval union
1706  *
1707  * This function gets called when an application tries to get the
1708  * chargalg properties by reading the sysfs files.
1709  * status:     charging/discharging/full/unknown
1710  * health:     health of the battery
1711  * Returns error code in case of failure else 0 on success
1712  */
1713 static int ab8500_chargalg_get_property(struct power_supply *psy,
1714 	enum power_supply_property psp,
1715 	union power_supply_propval *val)
1716 {
1717 	struct ab8500_chargalg *di = power_supply_get_drvdata(psy);
1718 
1719 	switch (psp) {
1720 	case POWER_SUPPLY_PROP_STATUS:
1721 		val->intval = di->charge_status;
1722 		break;
1723 	case POWER_SUPPLY_PROP_HEALTH:
1724 		if (di->events.batt_ovv) {
1725 			val->intval = POWER_SUPPLY_HEALTH_OVERVOLTAGE;
1726 		} else if (di->events.btemp_underover) {
1727 			if (di->batt_data.temp <= di->bm->bi.temp_min)
1728 				val->intval = POWER_SUPPLY_HEALTH_COLD;
1729 			else
1730 				val->intval = POWER_SUPPLY_HEALTH_OVERHEAT;
1731 		} else if (di->charge_state == STATE_SAFETY_TIMER_EXPIRED ||
1732 			   di->charge_state == STATE_SAFETY_TIMER_EXPIRED_INIT) {
1733 			val->intval = POWER_SUPPLY_HEALTH_UNSPEC_FAILURE;
1734 		} else {
1735 			val->intval = POWER_SUPPLY_HEALTH_GOOD;
1736 		}
1737 		break;
1738 	default:
1739 		return -EINVAL;
1740 	}
1741 	return 0;
1742 }
1743 
1744 /* Exposure to the sysfs interface */
1745 
1746 static ssize_t ab8500_chargalg_curr_step_show(struct ab8500_chargalg *di,
1747 					      char *buf)
1748 {
1749 	return sprintf(buf, "%d\n", di->curr_status.curr_step);
1750 }
1751 
1752 static ssize_t ab8500_chargalg_curr_step_store(struct ab8500_chargalg *di,
1753 					       const char *buf, size_t length)
1754 {
1755 	long param;
1756 	int ret;
1757 
1758 	ret = kstrtol(buf, 10, &param);
1759 	if (ret < 0)
1760 		return ret;
1761 
1762 	di->curr_status.curr_step = param;
1763 	if (di->curr_status.curr_step >= CHARGALG_CURR_STEP_LOW &&
1764 		di->curr_status.curr_step <= CHARGALG_CURR_STEP_HIGH) {
1765 		di->curr_status.curr_step_change = true;
1766 		queue_work(di->chargalg_wq, &di->chargalg_work);
1767 	} else
1768 		dev_info(di->dev, "Wrong current step\n"
1769 			"Enter 0. Disable AC/USB Charging\n"
1770 			"1--100. Set AC/USB charging current step\n"
1771 			"100. Enable AC/USB Charging\n");
1772 
1773 	return strlen(buf);
1774 }
1775 
1776 
1777 static ssize_t ab8500_chargalg_en_show(struct ab8500_chargalg *di,
1778 				       char *buf)
1779 {
1780 	return sprintf(buf, "%d\n",
1781 		       di->susp_status.ac_suspended &&
1782 		       di->susp_status.usb_suspended);
1783 }
1784 
1785 static ssize_t ab8500_chargalg_en_store(struct ab8500_chargalg *di,
1786 	const char *buf, size_t length)
1787 {
1788 	long param;
1789 	int ac_usb;
1790 	int ret;
1791 
1792 	ret = kstrtol(buf, 10, &param);
1793 	if (ret < 0)
1794 		return ret;
1795 
1796 	ac_usb = param;
1797 	switch (ac_usb) {
1798 	case 0:
1799 		/* Disable charging */
1800 		di->susp_status.ac_suspended = true;
1801 		di->susp_status.usb_suspended = true;
1802 		di->susp_status.suspended_change = true;
1803 		/* Trigger a state change */
1804 		queue_work(di->chargalg_wq,
1805 			&di->chargalg_work);
1806 		break;
1807 	case 1:
1808 		/* Enable AC Charging */
1809 		di->susp_status.ac_suspended = false;
1810 		di->susp_status.suspended_change = true;
1811 		/* Trigger a state change */
1812 		queue_work(di->chargalg_wq,
1813 			&di->chargalg_work);
1814 		break;
1815 	case 2:
1816 		/* Enable USB charging */
1817 		di->susp_status.usb_suspended = false;
1818 		di->susp_status.suspended_change = true;
1819 		/* Trigger a state change */
1820 		queue_work(di->chargalg_wq,
1821 			&di->chargalg_work);
1822 		break;
1823 	default:
1824 		dev_info(di->dev, "Wrong input\n"
1825 			"Enter 0. Disable AC/USB Charging\n"
1826 			"1. Enable AC charging\n"
1827 			"2. Enable USB Charging\n");
1828 	}
1829 	return strlen(buf);
1830 }
1831 
1832 static struct ab8500_chargalg_sysfs_entry ab8500_chargalg_en_charger =
1833 	__ATTR(chargalg, 0644, ab8500_chargalg_en_show,
1834 				ab8500_chargalg_en_store);
1835 
1836 static struct ab8500_chargalg_sysfs_entry ab8500_chargalg_curr_step =
1837 	__ATTR(chargalg_curr_step, 0644, ab8500_chargalg_curr_step_show,
1838 					ab8500_chargalg_curr_step_store);
1839 
1840 static ssize_t ab8500_chargalg_sysfs_show(struct kobject *kobj,
1841 	struct attribute *attr, char *buf)
1842 {
1843 	struct ab8500_chargalg_sysfs_entry *entry = container_of(attr,
1844 		struct ab8500_chargalg_sysfs_entry, attr);
1845 
1846 	struct ab8500_chargalg *di = container_of(kobj,
1847 		struct ab8500_chargalg, chargalg_kobject);
1848 
1849 	if (!entry->show)
1850 		return -EIO;
1851 
1852 	return entry->show(di, buf);
1853 }
1854 
1855 static ssize_t ab8500_chargalg_sysfs_charger(struct kobject *kobj,
1856 	struct attribute *attr, const char *buf, size_t length)
1857 {
1858 	struct ab8500_chargalg_sysfs_entry *entry = container_of(attr,
1859 		struct ab8500_chargalg_sysfs_entry, attr);
1860 
1861 	struct ab8500_chargalg *di = container_of(kobj,
1862 		struct ab8500_chargalg, chargalg_kobject);
1863 
1864 	if (!entry->store)
1865 		return -EIO;
1866 
1867 	return entry->store(di, buf, length);
1868 }
1869 
1870 static struct attribute *ab8500_chargalg_chg[] = {
1871 	&ab8500_chargalg_en_charger.attr,
1872 	&ab8500_chargalg_curr_step.attr,
1873 	NULL,
1874 };
1875 
1876 static const struct sysfs_ops ab8500_chargalg_sysfs_ops = {
1877 	.show = ab8500_chargalg_sysfs_show,
1878 	.store = ab8500_chargalg_sysfs_charger,
1879 };
1880 
1881 static struct kobj_type ab8500_chargalg_ktype = {
1882 	.sysfs_ops = &ab8500_chargalg_sysfs_ops,
1883 	.default_attrs = ab8500_chargalg_chg,
1884 };
1885 
1886 /**
1887  * ab8500_chargalg_sysfs_exit() - de-init of sysfs entry
1888  * @di:                pointer to the struct ab8500_chargalg
1889  *
1890  * This function removes the entry in sysfs.
1891  */
1892 static void ab8500_chargalg_sysfs_exit(struct ab8500_chargalg *di)
1893 {
1894 	kobject_del(&di->chargalg_kobject);
1895 }
1896 
1897 /**
1898  * ab8500_chargalg_sysfs_init() - init of sysfs entry
1899  * @di:                pointer to the struct ab8500_chargalg
1900  *
1901  * This function adds an entry in sysfs.
1902  * Returns error code in case of failure else 0(on success)
1903  */
1904 static int ab8500_chargalg_sysfs_init(struct ab8500_chargalg *di)
1905 {
1906 	int ret = 0;
1907 
1908 	ret = kobject_init_and_add(&di->chargalg_kobject,
1909 		&ab8500_chargalg_ktype,
1910 		NULL, "ab8500_chargalg");
1911 	if (ret < 0)
1912 		dev_err(di->dev, "failed to create sysfs entry\n");
1913 
1914 	return ret;
1915 }
1916 /* Exposure to the sysfs interface <<END>> */
1917 
1918 static int __maybe_unused ab8500_chargalg_resume(struct device *dev)
1919 {
1920 	struct ab8500_chargalg *di = dev_get_drvdata(dev);
1921 
1922 	/* Kick charger watchdog if charging (any charger online) */
1923 	if (di->chg_info.online_chg)
1924 		queue_delayed_work(di->chargalg_wq, &di->chargalg_wd_work, 0);
1925 
1926 	/*
1927 	 * Run the charging algorithm directly to be sure we don't
1928 	 * do it too seldom
1929 	 */
1930 	queue_delayed_work(di->chargalg_wq, &di->chargalg_periodic_work, 0);
1931 
1932 	return 0;
1933 }
1934 
1935 static int __maybe_unused ab8500_chargalg_suspend(struct device *dev)
1936 {
1937 	struct ab8500_chargalg *di = dev_get_drvdata(dev);
1938 
1939 	if (di->chg_info.online_chg)
1940 		cancel_delayed_work_sync(&di->chargalg_wd_work);
1941 
1942 	cancel_delayed_work_sync(&di->chargalg_periodic_work);
1943 
1944 	return 0;
1945 }
1946 
1947 static char *supply_interface[] = {
1948 	"ab8500_fg",
1949 };
1950 
1951 static const struct power_supply_desc ab8500_chargalg_desc = {
1952 	.name			= "ab8500_chargalg",
1953 	.type			= POWER_SUPPLY_TYPE_BATTERY,
1954 	.properties		= ab8500_chargalg_props,
1955 	.num_properties		= ARRAY_SIZE(ab8500_chargalg_props),
1956 	.get_property		= ab8500_chargalg_get_property,
1957 	.external_power_changed	= ab8500_chargalg_external_power_changed,
1958 };
1959 
1960 static int ab8500_chargalg_bind(struct device *dev, struct device *master,
1961 				void *data)
1962 {
1963 	struct ab8500_chargalg *di = dev_get_drvdata(dev);
1964 
1965 	/* Create a work queue for the chargalg */
1966 	di->chargalg_wq = alloc_ordered_workqueue("ab8500_chargalg_wq",
1967 						  WQ_MEM_RECLAIM);
1968 	if (di->chargalg_wq == NULL) {
1969 		dev_err(di->dev, "failed to create work queue\n");
1970 		return -ENOMEM;
1971 	}
1972 
1973 	/* Run the charging algorithm */
1974 	queue_delayed_work(di->chargalg_wq, &di->chargalg_periodic_work, 0);
1975 
1976 	return 0;
1977 }
1978 
1979 static void ab8500_chargalg_unbind(struct device *dev, struct device *master,
1980 				   void *data)
1981 {
1982 	struct ab8500_chargalg *di = dev_get_drvdata(dev);
1983 
1984 	/* Stop all timers and work */
1985 	hrtimer_cancel(&di->safety_timer);
1986 	hrtimer_cancel(&di->maintenance_timer);
1987 
1988 	cancel_delayed_work_sync(&di->chargalg_periodic_work);
1989 	cancel_delayed_work_sync(&di->chargalg_wd_work);
1990 	cancel_work_sync(&di->chargalg_work);
1991 
1992 	/* Delete the work queue */
1993 	destroy_workqueue(di->chargalg_wq);
1994 	flush_scheduled_work();
1995 }
1996 
1997 static const struct component_ops ab8500_chargalg_component_ops = {
1998 	.bind = ab8500_chargalg_bind,
1999 	.unbind = ab8500_chargalg_unbind,
2000 };
2001 
2002 static int ab8500_chargalg_probe(struct platform_device *pdev)
2003 {
2004 	struct device *dev = &pdev->dev;
2005 	struct power_supply_config psy_cfg = {};
2006 	struct ab8500_chargalg *di;
2007 	int ret = 0;
2008 
2009 	di = devm_kzalloc(dev, sizeof(*di), GFP_KERNEL);
2010 	if (!di)
2011 		return -ENOMEM;
2012 
2013 	di->bm = &ab8500_bm_data;
2014 
2015 	/* get device struct and parent */
2016 	di->dev = dev;
2017 	di->parent = dev_get_drvdata(pdev->dev.parent);
2018 
2019 	psy_cfg.supplied_to = supply_interface;
2020 	psy_cfg.num_supplicants = ARRAY_SIZE(supply_interface);
2021 	psy_cfg.drv_data = di;
2022 
2023 	/* Initilialize safety timer */
2024 	hrtimer_init(&di->safety_timer, CLOCK_REALTIME, HRTIMER_MODE_ABS);
2025 	di->safety_timer.function = ab8500_chargalg_safety_timer_expired;
2026 
2027 	/* Initilialize maintenance timer */
2028 	hrtimer_init(&di->maintenance_timer, CLOCK_REALTIME, HRTIMER_MODE_ABS);
2029 	di->maintenance_timer.function =
2030 		ab8500_chargalg_maintenance_timer_expired;
2031 
2032 	/* Init work for chargalg */
2033 	INIT_DEFERRABLE_WORK(&di->chargalg_periodic_work,
2034 		ab8500_chargalg_periodic_work);
2035 	INIT_DEFERRABLE_WORK(&di->chargalg_wd_work,
2036 		ab8500_chargalg_wd_work);
2037 
2038 	/* Init work for chargalg */
2039 	INIT_WORK(&di->chargalg_work, ab8500_chargalg_work);
2040 
2041 	/* To detect charger at startup */
2042 	di->chg_info.prev_conn_chg = -1;
2043 
2044 	/* Register chargalg power supply class */
2045 	di->chargalg_psy = devm_power_supply_register(di->dev,
2046 						 &ab8500_chargalg_desc,
2047 						 &psy_cfg);
2048 	if (IS_ERR(di->chargalg_psy)) {
2049 		dev_err(di->dev, "failed to register chargalg psy\n");
2050 		return PTR_ERR(di->chargalg_psy);
2051 	}
2052 
2053 	platform_set_drvdata(pdev, di);
2054 
2055 	/* sysfs interface to enable/disable charging from user space */
2056 	ret = ab8500_chargalg_sysfs_init(di);
2057 	if (ret) {
2058 		dev_err(di->dev, "failed to create sysfs entry\n");
2059 		return ret;
2060 	}
2061 	di->curr_status.curr_step = CHARGALG_CURR_STEP_HIGH;
2062 
2063 	dev_info(di->dev, "probe success\n");
2064 	return component_add(dev, &ab8500_chargalg_component_ops);
2065 }
2066 
2067 static int ab8500_chargalg_remove(struct platform_device *pdev)
2068 {
2069 	struct ab8500_chargalg *di = platform_get_drvdata(pdev);
2070 
2071 	component_del(&pdev->dev, &ab8500_chargalg_component_ops);
2072 
2073 	/* sysfs interface to enable/disable charging from user space */
2074 	ab8500_chargalg_sysfs_exit(di);
2075 
2076 	return 0;
2077 }
2078 
2079 static SIMPLE_DEV_PM_OPS(ab8500_chargalg_pm_ops, ab8500_chargalg_suspend, ab8500_chargalg_resume);
2080 
2081 static const struct of_device_id ab8500_chargalg_match[] = {
2082 	{ .compatible = "stericsson,ab8500-chargalg", },
2083 	{ },
2084 };
2085 
2086 struct platform_driver ab8500_chargalg_driver = {
2087 	.probe = ab8500_chargalg_probe,
2088 	.remove = ab8500_chargalg_remove,
2089 	.driver = {
2090 		.name = "ab8500_chargalg",
2091 		.of_match_table = ab8500_chargalg_match,
2092 		.pm = &ab8500_chargalg_pm_ops,
2093 	},
2094 };
2095 MODULE_LICENSE("GPL v2");
2096 MODULE_AUTHOR("Johan Palsson, Karl Komierowski");
2097 MODULE_ALIAS("platform:ab8500-chargalg");
2098 MODULE_DESCRIPTION("ab8500 battery charging algorithm");
2099