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