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