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