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