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