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