Version:  2.0.40 2.2.26 2.4.37 3.5 3.6 3.7 3.8 3.9 3.10 3.11 3.12 3.13 3.14 3.15 3.16 3.17 3.18 3.19 4.0 4.1

Linux/drivers/power/abx500_chargalg.c

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

This page was automatically generated by LXR 0.3.1 (source).  •  Linux is a registered trademark of Linus Torvalds  •  Contact us