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