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