dm thin metadata: fix __udivdi3 undefined on 32-bit
[linux/fpc-iii.git] / drivers / power / abx500_chargalg.c
blob541f702e0451c732b7d00efed27fd53f05b03b79
1 /*
2 * Copyright (C) ST-Ericsson SA 2012
3 * Copyright (c) 2012 Sony Mobile Communications AB
5 * Charging algorithm driver for abx500 variants
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>
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>
35 /* Watchdog kick interval */
36 #define CHG_WD_INTERVAL (6 * HZ)
38 /* End-of-charge criteria counter */
39 #define EOC_COND_CNT 10
41 /* One hour expressed in seconds */
42 #define ONE_HOUR_IN_SECONDS 3600
44 /* Five minutes expressed in seconds */
45 #define FIVE_MINUTES_IN_SECONDS 300
47 /* Plus margin for the low battery threshold */
48 #define BAT_PLUS_MARGIN (100)
50 #define CHARGALG_CURR_STEP_LOW 0
51 #define CHARGALG_CURR_STEP_HIGH 100
53 enum abx500_chargers {
54 NO_CHG,
55 AC_CHG,
56 USB_CHG,
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;
77 struct abx500_chargalg_suspension_status {
78 bool suspended_change;
79 bool ac_suspended;
80 bool usb_suspended;
83 struct abx500_chargalg_current_step_status {
84 bool curr_step_change;
85 int curr_step;
88 struct abx500_chargalg_battery_data {
89 int temp;
90 int volt;
91 int avg_curr;
92 int inst_curr;
93 int percent;
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,
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",
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;
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
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;
206 enum maxim_ret {
207 MAXIM_RET_NOACTION,
208 MAXIM_RET_CHANGE,
209 MAXIM_RET_IBAT_TOO_HIGH,
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
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;
269 /*External charger prepare notifier*/
270 BLOCKING_NOTIFIER_HEAD(charger_notifier_list);
272 /* Main battery properties */
273 static enum power_supply_property abx500_chargalg_props[] = {
274 POWER_SUPPLY_PROP_STATUS,
275 POWER_SUPPLY_PROP_HEALTH,
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);
285 * abx500_chargalg_safety_timer_expired() - Expiration of the safety timer
286 * @timer: pointer to the hrtimer structure
288 * This function gets called when the safety timer for the charger
289 * expires
291 static enum hrtimer_restart
292 abx500_chargalg_safety_timer_expired(struct hrtimer *timer)
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;
299 /* Trigger execution of the algorithm instantly */
300 queue_work(di->chargalg_wq, &di->chargalg_work);
302 return HRTIMER_NORESTART;
306 * abx500_chargalg_maintenance_timer_expired() - Expiration of
307 * the maintenance timer
308 * @timer: pointer to the timer structure
310 * This function gets called when the maintenence timer
311 * expires
313 static enum hrtimer_restart
314 abx500_chargalg_maintenance_timer_expired(struct hrtimer *timer)
317 struct abx500_chargalg *di = container_of(timer, struct abx500_chargalg,
318 maintenance_timer);
320 dev_dbg(di->dev, "Maintenance timer expired\n");
321 di->events.maintenance_timer_expired = true;
323 /* Trigger execution of the algorithm instantly */
324 queue_work(di->chargalg_wq, &di->chargalg_work);
326 return HRTIMER_NORESTART;
330 * abx500_chargalg_state_to() - Change charge state
331 * @di: pointer to the abx500_chargalg structure
333 * This function gets called when a charge state change should occur
335 static void abx500_chargalg_state_to(struct abx500_chargalg *di,
336 enum abx500_chargalg_states state)
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]);
346 di->charge_state = state;
349 static int abx500_chargalg_check_charger_enable(struct abx500_chargalg *di)
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;
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);
370 return 0;
374 * abx500_chargalg_check_charger_connection() - Check charger connection change
375 * @di: pointer to the abx500_chargalg structure
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.
380 static int abx500_chargalg_check_charger_connection(struct abx500_chargalg *di)
382 if (di->chg_info.conn_chg != di->chg_info.prev_conn_chg ||
383 di->susp_status.suspended_change) {
385 * Charger state changed or suspension
386 * has changed since last update
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);
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);
411 di->chg_info.prev_conn_chg = di->chg_info.conn_chg;
412 di->susp_status.suspended_change = false;
414 return di->chg_info.conn_chg;
418 * abx500_chargalg_check_current_step_status() - Check charging current
419 * step status.
420 * @di: pointer to the abx500_chargalg structure
422 * This function will check if there is a change in the charging current step
423 * and change charge state accordingly.
425 static void abx500_chargalg_check_current_step_status
426 (struct abx500_chargalg *di)
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;
434 * abx500_chargalg_start_safety_timer() - Start charging safety timer
435 * @di: pointer to the abx500_chargalg structure
437 * The safety timer is used to avoid overcharging of old or bad batteries.
438 * There are different timers for AC and USB
440 static void abx500_chargalg_start_safety_timer(struct abx500_chargalg *di)
442 /* Charger-dependent expiration time in hours*/
443 int timer_expiration = 0;
445 switch (di->chg_info.charger_type) {
446 case AC_CHG:
447 timer_expiration = di->bm->main_safety_tmr_h;
448 break;
450 case USB_CHG:
451 timer_expiration = di->bm->usb_safety_tmr_h;
452 break;
454 default:
455 dev_err(di->dev, "Unknown charger to charge from\n");
456 break;
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);
467 * abx500_chargalg_stop_safety_timer() - Stop charging safety timer
468 * @di: pointer to the abx500_chargalg structure
470 * The safety timer is stopped whenever the NORMAL state is exited
472 static void abx500_chargalg_stop_safety_timer(struct abx500_chargalg *di)
474 if (hrtimer_try_to_cancel(&di->safety_timer) >= 0)
475 di->events.safety_timer_expired = false;
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
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
487 static void abx500_chargalg_start_maintenance_timer(struct abx500_chargalg *di,
488 int duration)
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);
498 * abx500_chargalg_stop_maintenance_timer() - Stop maintenance timer
499 * @di: pointer to the abx500_chargalg structure
501 * The maintenance timer is stopped whenever maintenance ends or when another
502 * state is entered
504 static void abx500_chargalg_stop_maintenance_timer(struct abx500_chargalg *di)
506 if (hrtimer_try_to_cancel(&di->maintenance_timer) >= 0)
507 di->events.maintenance_timer_expired = false;
511 * abx500_chargalg_kick_watchdog() - Kick charger watchdog
512 * @di: pointer to the abx500_chargalg structure
514 * The charger watchdog have to be kicked periodically whenever the charger is
515 * on, else the ABB will reset the system
517 static int abx500_chargalg_kick_watchdog(struct abx500_chargalg *di)
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) {
523 * If AB charger watchdog expired, pm2xxx charging
524 * gets disabled. To be safe, kick both AB charger watchdog
525 * and pm2xxx watchdog.
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);
531 return di->ac_chg->ops.kick_wd(di->ac_chg);
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);
537 return -ENXIO;
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
547 * The AC charger will be turned on/off with the requested charge voltage and
548 * current
550 static int abx500_chargalg_ac_en(struct abx500_chargalg *di, int enable,
551 int vset, int iset)
553 static int abx500_chargalg_ex_ac_enable_toggle;
555 if (!di->ac_chg || !di->ac_chg->ops.enable)
556 return -ENXIO;
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);
564 di->chg_info.ac_iset = iset;
565 di->chg_info.ac_vset = vset;
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++;
575 return di->ac_chg->ops.enable(di->ac_chg, enable, vset, iset);
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
585 * The USB charger will be turned on/off with the requested charge voltage and
586 * current
588 static int abx500_chargalg_usb_en(struct abx500_chargalg *di, int enable,
589 int vset, int iset)
591 if (!di->usb_chg || !di->usb_chg->ops.enable)
592 return -ENXIO;
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);
600 di->chg_info.usb_iset = iset;
601 di->chg_info.usb_vset = vset;
603 return di->usb_chg->ops.enable(di->usb_chg, enable, vset, iset);
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
611 * The USB power path will be enable/ disable
613 static int ab8540_chargalg_usb_pp_en(struct abx500_chargalg *di, bool enable)
615 if (!di->usb_chg || !di->usb_chg->ops.pp_enable)
616 return -ENXIO;
618 return di->usb_chg->ops.pp_enable(di->usb_chg, enable);
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
626 * The USB USB pre-charge will be enable/ disable
628 static int ab8540_chargalg_usb_pre_chg_en(struct abx500_chargalg *di,
629 bool enable)
631 if (!di->usb_chg || !di->usb_chg->ops.pre_chg_enable)
632 return -ENXIO;
634 return di->usb_chg->ops.pre_chg_enable(di->usb_chg, enable);
638 * abx500_chargalg_update_chg_curr() - Update charger current
639 * @di: pointer to the abx500_chargalg structure
640 * @iset: requested charger output current
642 * The charger output current will be updated for the charger
643 * that is currently in use
645 static int abx500_chargalg_update_chg_curr(struct abx500_chargalg *di,
646 int iset)
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) {
652 * Select maximum of what both the charger
653 * and the battery supports
655 if (di->ac_chg->max_out_curr)
656 iset = min(iset, di->ac_chg->max_out_curr);
658 di->chg_info.ac_iset = iset;
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) {
664 * Select maximum of what both the charger
665 * and the battery supports
667 if (di->usb_chg->max_out_curr)
668 iset = min(iset, di->usb_chg->max_out_curr);
670 di->chg_info.usb_iset = iset;
672 return di->usb_chg->ops.update_curr(di->usb_chg, iset);
675 return -ENXIO;
679 * abx500_chargalg_stop_charging() - Stop charging
680 * @di: pointer to the abx500_chargalg structure
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
686 static void abx500_chargalg_stop_charging(struct abx500_chargalg *di)
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);
699 * abx500_chargalg_hold_charging() - Pauses charging
700 * @di: pointer to the abx500_chargalg structure
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
706 static void abx500_chargalg_hold_charging(struct abx500_chargalg *di)
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);
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
724 * A charger will be enabled depending on the requested charger type that was
725 * detected previously.
727 static void abx500_chargalg_start_charging(struct abx500_chargalg *di,
728 int vset, int iset)
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;
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;
745 default:
746 dev_err(di->dev, "Unknown charger to charge from\n");
747 break;
752 * abx500_chargalg_check_temp() - Check battery temperature ranges
753 * @di: pointer to the abx500_chargalg structure
755 * The battery temperature is checked against the predefined limits and the
756 * charge state is changed accordingly
758 static void abx500_chargalg_check_temp(struct abx500_chargalg *di)
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);
797 * abx500_chargalg_check_charger_voltage() - Check charger voltage
798 * @di: pointer to the abx500_chargalg structure
800 * Charger voltage is checked against maximum limit
802 static void abx500_chargalg_check_charger_voltage(struct abx500_chargalg *di)
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;
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;
817 * abx500_chargalg_end_of_charge() - Check if end-of-charge criteria is fulfilled
818 * @di: pointer to the abx500_chargalg structure
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
824 static void abx500_chargalg_end_of_charge(struct abx500_chargalg *di)
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);
850 } else {
851 di->eoc_cnt = 0;
855 static void init_maxim_chg_curr(struct abx500_chargalg *di)
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;
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
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
876 static enum maxim_ret abx500_chargalg_chg_curr_maxim(struct abx500_chargalg *di)
878 int delta_i;
880 if (!di->bm->maxi->ena_maxi)
881 return MAXIM_RET_NOACTION;
883 delta_i = di->ccm.original_iset - di->batt_data.inst_curr;
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;
905 di->ccm.wait_cnt = 0;
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);
913 if (di->ccm.current_iset == di->ccm.original_iset)
914 return MAXIM_RET_NOACTION;
916 di->ccm.condition_cnt = di->bm->maxi->wait_cycles;
917 di->ccm.current_iset = di->ccm.original_iset;
918 di->ccm.level = 0;
920 return MAXIM_RET_IBAT_TOO_HIGH;
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;
942 } else {
943 di->ccm.condition_cnt = di->bm->maxi->wait_cycles;
944 return MAXIM_RET_NOACTION;
948 static void handle_maxim_chg_curr(struct abx500_chargalg *di)
950 enum maxim_ret ret;
951 int result;
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;
968 case MAXIM_RET_NOACTION:
969 default:
970 /* Do nothing..*/
971 break;
975 static int abx500_chargalg_get_ext_psy_data(struct device *dev, void *data)
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;
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;
993 if (!psy_found)
994 return 0;
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.
1001 if (!power_supply_get_property(ext, POWER_SUPPLY_PROP_CAPACITY, &ret)) {
1002 di->batt_data.percent = ret.intval;
1003 capacity_updated = true;
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];
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);
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;
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;
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;
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;
1065 break;
1066 default:
1067 break;
1069 break;
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;
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);
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;
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);
1110 break;
1111 default:
1112 break;
1114 break;
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;
1156 break;
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;
1194 default:
1195 break;
1197 break;
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;
1213 break;
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;
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;
1234 break;
1235 default:
1236 break;
1238 break;
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;
1248 break;
1249 default:
1250 break;
1252 break;
1254 case POWER_SUPPLY_PROP_TEMP:
1255 di->batt_data.temp = ret.intval / 10;
1256 break;
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;
1274 break;
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;
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;
1299 return 0;
1303 * abx500_chargalg_external_power_changed() - callback for power supply changes
1304 * @psy: pointer to the structure power_supply
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.
1311 static void abx500_chargalg_external_power_changed(struct power_supply *psy)
1313 struct abx500_chargalg *di = power_supply_get_drvdata(psy);
1316 * Trigger execution of the algorithm instantly and read
1317 * all power_supply properties there instead
1319 queue_work(di->chargalg_wq, &di->chargalg_work);
1323 * abx500_chargalg_algorithm() - Main function for the algorithm
1324 * @di: pointer to the abx500_chargalg structure
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
1330 static void abx500_chargalg_algorithm(struct abx500_chargalg *di)
1332 int charger_status;
1333 int ret;
1334 int curr_step_lvl;
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);
1340 abx500_chargalg_end_of_charge(di);
1341 abx500_chargalg_check_temp(di);
1342 abx500_chargalg_check_charger_voltage(di);
1344 charger_status = abx500_chargalg_check_charger_connection(di);
1345 abx500_chargalg_check_current_step_status(di);
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);
1355 * First check if we have a charger connected.
1356 * Also we don't allow charging of unknown batteries if configured
1357 * this way
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);
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 */
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);
1380 * Check if any interrupts has occured
1381 * that will prevent us from charging
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);
1389 /* Main or USB charger not ok. */
1390 else if (di->events.mainextchnotok || di->events.usbchargernotok) {
1392 * If vbus_collapsed is set, we have to lower the charger
1393 * current, which is done in the normal state below
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);
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);
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);
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);
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);
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);
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);
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 */
1467 case STATE_HANDHELD:
1468 break;
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 */
1483 case STATE_SUSPENDED:
1484 /* CHARGING is suspended */
1485 break;
1487 case STATE_BATT_REMOVED_INIT:
1488 abx500_chargalg_stop_charging(di);
1489 abx500_chargalg_state_to(di, STATE_BATT_REMOVED);
1490 /* Intentional fallthrough */
1492 case STATE_BATT_REMOVED:
1493 if (!di->events.batt_rem)
1494 abx500_chargalg_state_to(di, STATE_NORMAL_INIT);
1495 break;
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 */
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;
1508 case STATE_OVV_PROTECT_INIT:
1509 abx500_chargalg_stop_charging(di);
1510 abx500_chargalg_state_to(di, STATE_OVV_PROTECT);
1511 /* Intentional fallthrough */
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;
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 */
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;
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 */
1538 case STATE_SAFETY_TIMER_EXPIRED:
1539 /* We exit this state when charger is removed */
1540 break;
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;
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);
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);
1580 break;
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;
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);
1600 break;
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 */
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;
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*/
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);
1634 break;
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*/
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);
1654 break;
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 */
1668 case STATE_TEMP_LOWHIGH:
1669 if (!di->events.btemp_lowhigh)
1670 abx500_chargalg_state_to(di, STATE_NORMAL_INIT);
1671 break;
1673 case STATE_WD_EXPIRED_INIT:
1674 abx500_chargalg_stop_charging(di);
1675 abx500_chargalg_state_to(di, STATE_WD_EXPIRED);
1676 /* Intentional fallthrough */
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;
1684 case STATE_TEMP_UNDEROVER_INIT:
1685 abx500_chargalg_stop_charging(di);
1686 abx500_chargalg_state_to(di, STATE_TEMP_UNDEROVER);
1687 /* Intentional fallthrough */
1689 case STATE_TEMP_UNDEROVER:
1690 if (!di->events.btemp_underover)
1691 abx500_chargalg_state_to(di, STATE_NORMAL_INIT);
1692 break;
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);
1703 * abx500_chargalg_periodic_work() - Periodic work for the algorithm
1704 * @work: pointer to the work_struct structure
1706 * Work queue function for the charging algorithm
1708 static void abx500_chargalg_periodic_work(struct work_struct *work)
1710 struct abx500_chargalg *di = container_of(work,
1711 struct abx500_chargalg, chargalg_periodic_work.work);
1713 abx500_chargalg_algorithm(di);
1716 * If a charger is connected then the battery has to be monitored
1717 * frequently, else the work can be delayed.
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);
1730 * abx500_chargalg_wd_work() - periodic work to kick the charger watchdog
1731 * @work: pointer to the work_struct structure
1733 * Work queue function for kicking the charger watchdog
1735 static void abx500_chargalg_wd_work(struct work_struct *work)
1737 int ret;
1738 struct abx500_chargalg *di = container_of(work,
1739 struct abx500_chargalg, chargalg_wd_work.work);
1741 dev_dbg(di->dev, "abx500_chargalg_wd_work\n");
1743 ret = abx500_chargalg_kick_watchdog(di);
1744 if (ret < 0)
1745 dev_err(di->dev, "failed to kick watchdog\n");
1747 queue_delayed_work(di->chargalg_wq,
1748 &di->chargalg_wd_work, CHG_WD_INTERVAL);
1752 * abx500_chargalg_work() - Work to run the charging algorithm instantly
1753 * @work: pointer to the work_struct structure
1755 * Work queue function for calling the charging algorithm
1757 static void abx500_chargalg_work(struct work_struct *work)
1759 struct abx500_chargalg *di = container_of(work,
1760 struct abx500_chargalg, chargalg_work);
1762 abx500_chargalg_algorithm(di);
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
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
1777 static int abx500_chargalg_get_property(struct power_supply *psy,
1778 enum power_supply_property psp,
1779 union power_supply_propval *val)
1781 struct abx500_chargalg *di = power_supply_get_drvdata(psy);
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;
1801 break;
1802 default:
1803 return -EINVAL;
1805 return 0;
1808 /* Exposure to the sysfs interface */
1810 static ssize_t abx500_chargalg_curr_step_show(struct abx500_chargalg *di,
1811 char *buf)
1813 return sprintf(buf, "%d\n", di->curr_status.curr_step);
1816 static ssize_t abx500_chargalg_curr_step_store(struct abx500_chargalg *di,
1817 const char *buf, size_t length)
1819 long int param;
1820 int ret;
1822 ret = kstrtol(buf, 10, &param);
1823 if (ret < 0)
1824 return ret;
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");
1837 return strlen(buf);
1841 static ssize_t abx500_chargalg_en_show(struct abx500_chargalg *di,
1842 char *buf)
1844 return sprintf(buf, "%d\n",
1845 di->susp_status.ac_suspended &&
1846 di->susp_status.usb_suspended);
1849 static ssize_t abx500_chargalg_en_store(struct abx500_chargalg *di,
1850 const char *buf, size_t length)
1852 long int param;
1853 int ac_usb;
1854 int ret;
1856 ret = kstrtol(buf, 10, &param);
1857 if (ret < 0)
1858 return ret;
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");
1893 return strlen(buf);
1896 static struct abx500_chargalg_sysfs_entry abx500_chargalg_en_charger =
1897 __ATTR(chargalg, 0644, abx500_chargalg_en_show,
1898 abx500_chargalg_en_store);
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);
1904 static ssize_t abx500_chargalg_sysfs_show(struct kobject *kobj,
1905 struct attribute *attr, char *buf)
1907 struct abx500_chargalg_sysfs_entry *entry = container_of(attr,
1908 struct abx500_chargalg_sysfs_entry, attr);
1910 struct abx500_chargalg *di = container_of(kobj,
1911 struct abx500_chargalg, chargalg_kobject);
1913 if (!entry->show)
1914 return -EIO;
1916 return entry->show(di, buf);
1919 static ssize_t abx500_chargalg_sysfs_charger(struct kobject *kobj,
1920 struct attribute *attr, const char *buf, size_t length)
1922 struct abx500_chargalg_sysfs_entry *entry = container_of(attr,
1923 struct abx500_chargalg_sysfs_entry, attr);
1925 struct abx500_chargalg *di = container_of(kobj,
1926 struct abx500_chargalg, chargalg_kobject);
1928 if (!entry->store)
1929 return -EIO;
1931 return entry->store(di, buf, length);
1934 static struct attribute *abx500_chargalg_chg[] = {
1935 &abx500_chargalg_en_charger.attr,
1936 &abx500_chargalg_curr_step.attr,
1937 NULL,
1940 static const struct sysfs_ops abx500_chargalg_sysfs_ops = {
1941 .show = abx500_chargalg_sysfs_show,
1942 .store = abx500_chargalg_sysfs_charger,
1945 static struct kobj_type abx500_chargalg_ktype = {
1946 .sysfs_ops = &abx500_chargalg_sysfs_ops,
1947 .default_attrs = abx500_chargalg_chg,
1951 * abx500_chargalg_sysfs_exit() - de-init of sysfs entry
1952 * @di: pointer to the struct abx500_chargalg
1954 * This function removes the entry in sysfs.
1956 static void abx500_chargalg_sysfs_exit(struct abx500_chargalg *di)
1958 kobject_del(&di->chargalg_kobject);
1962 * abx500_chargalg_sysfs_init() - init of sysfs entry
1963 * @di: pointer to the struct abx500_chargalg
1965 * This function adds an entry in sysfs.
1966 * Returns error code in case of failure else 0(on success)
1968 static int abx500_chargalg_sysfs_init(struct abx500_chargalg *di)
1970 int ret = 0;
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");
1978 return ret;
1980 /* Exposure to the sysfs interface <<END>> */
1982 #if defined(CONFIG_PM)
1983 static int abx500_chargalg_resume(struct platform_device *pdev)
1985 struct abx500_chargalg *di = platform_get_drvdata(pdev);
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);
1992 * Run the charging algorithm directly to be sure we don't
1993 * do it too seldom
1995 queue_delayed_work(di->chargalg_wq, &di->chargalg_periodic_work, 0);
1997 return 0;
2000 static int abx500_chargalg_suspend(struct platform_device *pdev,
2001 pm_message_t state)
2003 struct abx500_chargalg *di = platform_get_drvdata(pdev);
2005 if (di->chg_info.online_chg)
2006 cancel_delayed_work_sync(&di->chargalg_wd_work);
2008 cancel_delayed_work_sync(&di->chargalg_periodic_work);
2010 return 0;
2012 #else
2013 #define abx500_chargalg_suspend NULL
2014 #define abx500_chargalg_resume NULL
2015 #endif
2017 static int abx500_chargalg_remove(struct platform_device *pdev)
2019 struct abx500_chargalg *di = platform_get_drvdata(pdev);
2021 /* sysfs interface to enable/disbale charging from user space */
2022 abx500_chargalg_sysfs_exit(di);
2024 hrtimer_cancel(&di->safety_timer);
2025 hrtimer_cancel(&di->maintenance_timer);
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);
2031 /* Delete the work queue */
2032 destroy_workqueue(di->chargalg_wq);
2034 power_supply_unregister(di->chargalg_psy);
2036 return 0;
2039 static char *supply_interface[] = {
2040 "ab8500_fg",
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,
2052 static int abx500_chargalg_probe(struct platform_device *pdev)
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;
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;
2066 if (!plat) {
2067 dev_err(&pdev->dev, "no battery management data supplied\n");
2068 return -EINVAL;
2070 di->bm = plat;
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;
2080 /* get device struct and parent */
2081 di->dev = &pdev->dev;
2082 di->parent = dev_get_drvdata(pdev->dev.parent);
2084 psy_cfg.supplied_to = supply_interface;
2085 psy_cfg.num_supplicants = ARRAY_SIZE(supply_interface);
2086 psy_cfg.drv_data = di;
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;
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;
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;
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);
2111 /* Init work for chargalg */
2112 INIT_WORK(&di->chargalg_work, abx500_chargalg_work);
2114 /* To detect charger at startup */
2115 di->chg_info.prev_conn_chg = -1;
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;
2126 platform_set_drvdata(pdev, di);
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;
2134 di->curr_status.curr_step = CHARGALG_CURR_STEP_HIGH;
2136 /* Run the charging algorithm */
2137 queue_delayed_work(di->chargalg_wq, &di->chargalg_periodic_work, 0);
2139 dev_info(di->dev, "probe success\n");
2140 return ret;
2142 free_psy:
2143 power_supply_unregister(di->chargalg_psy);
2144 free_chargalg_wq:
2145 destroy_workqueue(di->chargalg_wq);
2146 return ret;
2149 static const struct of_device_id ab8500_chargalg_match[] = {
2150 { .compatible = "stericsson,ab8500-chargalg", },
2151 { },
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,
2165 module_platform_driver(abx500_chargalg_driver);
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");