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