blob: 8ab65a3a81900431fa74441850c8ccec663549a7 [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
Lee Jones4dcdf572013-02-14 09:24:10 +0000308static int abx500_chargalg_check_charger_enable(struct abx500_chargalg *di)
309{
310 switch (di->charge_state) {
311 case STATE_NORMAL:
312 case STATE_MAINTENANCE_A:
313 case STATE_MAINTENANCE_B:
314 break;
315 default:
316 return 0;
317 }
318
319 if (di->chg_info.charger_type & USB_CHG) {
320 return di->usb_chg->ops.check_enable(di->usb_chg,
321 di->bm->bat_type[di->bm->batt_id].normal_vol_lvl,
322 di->bm->bat_type[di->bm->batt_id].normal_cur_lvl);
323 } else if ((di->chg_info.charger_type & AC_CHG) &&
324 !(di->ac_chg->external)) {
325 return di->ac_chg->ops.check_enable(di->ac_chg,
326 di->bm->bat_type[di->bm->batt_id].normal_vol_lvl,
327 di->bm->bat_type[di->bm->batt_id].normal_cur_lvl);
328 }
329 return 0;
330}
331
Arun Murthy1668f812012-02-29 21:54:25 +0530332/**
333 * abx500_chargalg_check_charger_connection() - Check charger connection change
334 * @di: pointer to the abx500_chargalg structure
335 *
336 * This function will check if there is a change in the charger connection
337 * and change charge state accordingly. AC has precedence over USB.
338 */
339static int abx500_chargalg_check_charger_connection(struct abx500_chargalg *di)
340{
341 if (di->chg_info.conn_chg != di->chg_info.prev_conn_chg ||
342 di->susp_status.suspended_change) {
343 /*
344 * Charger state changed or suspension
345 * has changed since last update
346 */
347 if ((di->chg_info.conn_chg & AC_CHG) &&
348 !di->susp_status.ac_suspended) {
349 dev_dbg(di->dev, "Charging source is AC\n");
350 if (di->chg_info.charger_type != AC_CHG) {
351 di->chg_info.charger_type = AC_CHG;
352 abx500_chargalg_state_to(di, STATE_NORMAL_INIT);
353 }
354 } else if ((di->chg_info.conn_chg & USB_CHG) &&
355 !di->susp_status.usb_suspended) {
356 dev_dbg(di->dev, "Charging source is USB\n");
357 di->chg_info.charger_type = USB_CHG;
358 abx500_chargalg_state_to(di, STATE_NORMAL_INIT);
359 } else if (di->chg_info.conn_chg &&
360 (di->susp_status.ac_suspended ||
361 di->susp_status.usb_suspended)) {
362 dev_dbg(di->dev, "Charging is suspended\n");
363 di->chg_info.charger_type = NO_CHG;
364 abx500_chargalg_state_to(di, STATE_SUSPENDED_INIT);
365 } else {
366 dev_dbg(di->dev, "Charging source is OFF\n");
367 di->chg_info.charger_type = NO_CHG;
368 abx500_chargalg_state_to(di, STATE_HANDHELD_INIT);
369 }
370 di->chg_info.prev_conn_chg = di->chg_info.conn_chg;
371 di->susp_status.suspended_change = false;
372 }
373 return di->chg_info.conn_chg;
374}
375
376/**
377 * abx500_chargalg_start_safety_timer() - Start charging safety timer
378 * @di: pointer to the abx500_chargalg structure
379 *
380 * The safety timer is used to avoid overcharging of old or bad batteries.
381 * There are different timers for AC and USB
382 */
383static void abx500_chargalg_start_safety_timer(struct abx500_chargalg *di)
384{
385 unsigned long timer_expiration = 0;
386
387 switch (di->chg_info.charger_type) {
388 case AC_CHG:
389 timer_expiration =
390 round_jiffies(jiffies +
Lee Jonesb0284de2012-11-30 10:09:42 +0000391 (di->bm->main_safety_tmr_h * 3600 * HZ));
Arun Murthy1668f812012-02-29 21:54:25 +0530392 break;
393
394 case USB_CHG:
395 timer_expiration =
396 round_jiffies(jiffies +
Lee Jonesb0284de2012-11-30 10:09:42 +0000397 (di->bm->usb_safety_tmr_h * 3600 * HZ));
Arun Murthy1668f812012-02-29 21:54:25 +0530398 break;
399
400 default:
401 dev_err(di->dev, "Unknown charger to charge from\n");
402 break;
403 }
404
405 di->events.safety_timer_expired = false;
406 di->safety_timer.expires = timer_expiration;
407 if (!timer_pending(&di->safety_timer))
408 add_timer(&di->safety_timer);
409 else
410 mod_timer(&di->safety_timer, timer_expiration);
411}
412
413/**
414 * abx500_chargalg_stop_safety_timer() - Stop charging safety timer
415 * @di: pointer to the abx500_chargalg structure
416 *
417 * The safety timer is stopped whenever the NORMAL state is exited
418 */
419static void abx500_chargalg_stop_safety_timer(struct abx500_chargalg *di)
420{
421 di->events.safety_timer_expired = false;
422 del_timer(&di->safety_timer);
423}
424
425/**
426 * abx500_chargalg_start_maintenance_timer() - Start charging maintenance timer
427 * @di: pointer to the abx500_chargalg structure
428 * @duration: duration of ther maintenance timer in hours
429 *
430 * The maintenance timer is used to maintain the charge in the battery once
431 * the battery is considered full. These timers are chosen to match the
432 * discharge curve of the battery
433 */
434static void abx500_chargalg_start_maintenance_timer(struct abx500_chargalg *di,
435 int duration)
436{
437 unsigned long timer_expiration;
438
439 /* Convert from hours to jiffies */
440 timer_expiration = round_jiffies(jiffies + (duration * 3600 * HZ));
441
442 di->events.maintenance_timer_expired = false;
443 di->maintenance_timer.expires = timer_expiration;
444 if (!timer_pending(&di->maintenance_timer))
445 add_timer(&di->maintenance_timer);
446 else
447 mod_timer(&di->maintenance_timer, timer_expiration);
448}
449
450/**
451 * abx500_chargalg_stop_maintenance_timer() - Stop maintenance timer
452 * @di: pointer to the abx500_chargalg structure
453 *
454 * The maintenance timer is stopped whenever maintenance ends or when another
455 * state is entered
456 */
457static void abx500_chargalg_stop_maintenance_timer(struct abx500_chargalg *di)
458{
459 di->events.maintenance_timer_expired = false;
460 del_timer(&di->maintenance_timer);
461}
462
463/**
464 * abx500_chargalg_kick_watchdog() - Kick charger watchdog
465 * @di: pointer to the abx500_chargalg structure
466 *
467 * The charger watchdog have to be kicked periodically whenever the charger is
468 * on, else the ABB will reset the system
469 */
470static int abx500_chargalg_kick_watchdog(struct abx500_chargalg *di)
471{
472 /* Check if charger exists and kick watchdog if charging */
473 if (di->ac_chg && di->ac_chg->ops.kick_wd &&
Loic Pallardye07a5642012-05-10 15:33:56 +0200474 di->chg_info.online_chg & AC_CHG) {
475 /*
476 * If AB charger watchdog expired, pm2xxx charging
477 * gets disabled. To be safe, kick both AB charger watchdog
478 * and pm2xxx watchdog.
479 */
480 if (di->ac_chg->external &&
481 di->usb_chg && di->usb_chg->ops.kick_wd)
482 di->usb_chg->ops.kick_wd(di->usb_chg);
483
Arun Murthy1668f812012-02-29 21:54:25 +0530484 return di->ac_chg->ops.kick_wd(di->ac_chg);
Loic Pallardye07a5642012-05-10 15:33:56 +0200485 }
Arun Murthy1668f812012-02-29 21:54:25 +0530486 else if (di->usb_chg && di->usb_chg->ops.kick_wd &&
487 di->chg_info.online_chg & USB_CHG)
488 return di->usb_chg->ops.kick_wd(di->usb_chg);
489
490 return -ENXIO;
491}
492
493/**
494 * abx500_chargalg_ac_en() - Turn on/off the AC charger
495 * @di: pointer to the abx500_chargalg structure
496 * @enable: charger on/off
497 * @vset: requested charger output voltage
498 * @iset: requested charger output current
499 *
500 * The AC charger will be turned on/off with the requested charge voltage and
501 * current
502 */
503static int abx500_chargalg_ac_en(struct abx500_chargalg *di, int enable,
504 int vset, int iset)
505{
506 if (!di->ac_chg || !di->ac_chg->ops.enable)
507 return -ENXIO;
508
509 /* Select maximum of what both the charger and the battery supports */
510 if (di->ac_chg->max_out_volt)
511 vset = min(vset, di->ac_chg->max_out_volt);
512 if (di->ac_chg->max_out_curr)
513 iset = min(iset, di->ac_chg->max_out_curr);
514
515 di->chg_info.ac_iset = iset;
516 di->chg_info.ac_vset = vset;
517
518 return di->ac_chg->ops.enable(di->ac_chg, enable, vset, iset);
519}
520
521/**
522 * abx500_chargalg_usb_en() - Turn on/off the USB charger
523 * @di: pointer to the abx500_chargalg structure
524 * @enable: charger on/off
525 * @vset: requested charger output voltage
526 * @iset: requested charger output current
527 *
528 * The USB charger will be turned on/off with the requested charge voltage and
529 * current
530 */
531static int abx500_chargalg_usb_en(struct abx500_chargalg *di, int enable,
532 int vset, int iset)
533{
534 if (!di->usb_chg || !di->usb_chg->ops.enable)
535 return -ENXIO;
536
537 /* Select maximum of what both the charger and the battery supports */
538 if (di->usb_chg->max_out_volt)
539 vset = min(vset, di->usb_chg->max_out_volt);
540 if (di->usb_chg->max_out_curr)
541 iset = min(iset, di->usb_chg->max_out_curr);
542
543 di->chg_info.usb_iset = iset;
544 di->chg_info.usb_vset = vset;
545
546 return di->usb_chg->ops.enable(di->usb_chg, enable, vset, iset);
547}
548
549/**
550 * abx500_chargalg_update_chg_curr() - Update charger current
551 * @di: pointer to the abx500_chargalg structure
552 * @iset: requested charger output current
553 *
554 * The charger output current will be updated for the charger
555 * that is currently in use
556 */
557static int abx500_chargalg_update_chg_curr(struct abx500_chargalg *di,
558 int iset)
559{
560 /* Check if charger exists and update current if charging */
561 if (di->ac_chg && di->ac_chg->ops.update_curr &&
562 di->chg_info.charger_type & AC_CHG) {
563 /*
564 * Select maximum of what both the charger
565 * and the battery supports
566 */
567 if (di->ac_chg->max_out_curr)
568 iset = min(iset, di->ac_chg->max_out_curr);
569
570 di->chg_info.ac_iset = iset;
571
572 return di->ac_chg->ops.update_curr(di->ac_chg, iset);
573 } else if (di->usb_chg && di->usb_chg->ops.update_curr &&
574 di->chg_info.charger_type & USB_CHG) {
575 /*
576 * Select maximum of what both the charger
577 * and the battery supports
578 */
579 if (di->usb_chg->max_out_curr)
580 iset = min(iset, di->usb_chg->max_out_curr);
581
582 di->chg_info.usb_iset = iset;
583
584 return di->usb_chg->ops.update_curr(di->usb_chg, iset);
585 }
586
587 return -ENXIO;
588}
589
590/**
591 * abx500_chargalg_stop_charging() - Stop charging
592 * @di: pointer to the abx500_chargalg structure
593 *
594 * This function is called from any state where charging should be stopped.
595 * All charging is disabled and all status parameters and timers are changed
596 * accordingly
597 */
598static void abx500_chargalg_stop_charging(struct abx500_chargalg *di)
599{
600 abx500_chargalg_ac_en(di, false, 0, 0);
601 abx500_chargalg_usb_en(di, false, 0, 0);
602 abx500_chargalg_stop_safety_timer(di);
603 abx500_chargalg_stop_maintenance_timer(di);
604 di->charge_status = POWER_SUPPLY_STATUS_NOT_CHARGING;
605 di->maintenance_chg = false;
606 cancel_delayed_work(&di->chargalg_wd_work);
607 power_supply_changed(&di->chargalg_psy);
608}
609
610/**
611 * abx500_chargalg_hold_charging() - Pauses charging
612 * @di: pointer to the abx500_chargalg structure
613 *
614 * This function is called in the case where maintenance charging has been
615 * disabled and instead a battery voltage mode is entered to check when the
616 * battery voltage has reached a certain recharge voltage
617 */
618static void abx500_chargalg_hold_charging(struct abx500_chargalg *di)
619{
620 abx500_chargalg_ac_en(di, false, 0, 0);
621 abx500_chargalg_usb_en(di, false, 0, 0);
622 abx500_chargalg_stop_safety_timer(di);
623 abx500_chargalg_stop_maintenance_timer(di);
624 di->charge_status = POWER_SUPPLY_STATUS_CHARGING;
625 di->maintenance_chg = false;
626 cancel_delayed_work(&di->chargalg_wd_work);
627 power_supply_changed(&di->chargalg_psy);
628}
629
630/**
631 * abx500_chargalg_start_charging() - Start the charger
632 * @di: pointer to the abx500_chargalg structure
633 * @vset: requested charger output voltage
634 * @iset: requested charger output current
635 *
636 * A charger will be enabled depending on the requested charger type that was
637 * detected previously.
638 */
639static void abx500_chargalg_start_charging(struct abx500_chargalg *di,
640 int vset, int iset)
641{
Lee Jones3edebfc2013-01-17 13:40:32 +0000642 bool start_chargalg_wd = true;
643
Arun Murthy1668f812012-02-29 21:54:25 +0530644 switch (di->chg_info.charger_type) {
645 case AC_CHG:
646 dev_dbg(di->dev,
647 "AC parameters: Vset %d, Ich %d\n", vset, iset);
648 abx500_chargalg_usb_en(di, false, 0, 0);
649 abx500_chargalg_ac_en(di, true, vset, iset);
650 break;
651
652 case USB_CHG:
653 dev_dbg(di->dev,
654 "USB parameters: Vset %d, Ich %d\n", vset, iset);
655 abx500_chargalg_ac_en(di, false, 0, 0);
656 abx500_chargalg_usb_en(di, true, vset, iset);
657 break;
658
659 default:
660 dev_err(di->dev, "Unknown charger to charge from\n");
Lee Jones3edebfc2013-01-17 13:40:32 +0000661 start_chargalg_wd = false;
Arun Murthy1668f812012-02-29 21:54:25 +0530662 break;
663 }
Lee Jones3edebfc2013-01-17 13:40:32 +0000664
665 if (start_chargalg_wd && !delayed_work_pending(&di->chargalg_wd_work))
666 queue_delayed_work(di->chargalg_wq, &di->chargalg_wd_work, 0);
Arun Murthy1668f812012-02-29 21:54:25 +0530667}
668
669/**
670 * abx500_chargalg_check_temp() - Check battery temperature ranges
671 * @di: pointer to the abx500_chargalg structure
672 *
673 * The battery temperature is checked against the predefined limits and the
674 * charge state is changed accordingly
675 */
676static void abx500_chargalg_check_temp(struct abx500_chargalg *di)
677{
Lee Jonesb0284de2012-11-30 10:09:42 +0000678 if (di->batt_data.temp > (di->bm->temp_low + di->t_hyst_norm) &&
679 di->batt_data.temp < (di->bm->temp_high - di->t_hyst_norm)) {
Arun Murthy1668f812012-02-29 21:54:25 +0530680 /* Temp OK! */
681 di->events.btemp_underover = false;
682 di->events.btemp_lowhigh = false;
683 di->t_hyst_norm = 0;
684 di->t_hyst_lowhigh = 0;
685 } else {
Lee Jonesb0284de2012-11-30 10:09:42 +0000686 if (((di->batt_data.temp >= di->bm->temp_high) &&
Arun Murthy1668f812012-02-29 21:54:25 +0530687 (di->batt_data.temp <
Lee Jonesb0284de2012-11-30 10:09:42 +0000688 (di->bm->temp_over - di->t_hyst_lowhigh))) ||
Arun Murthy1668f812012-02-29 21:54:25 +0530689 ((di->batt_data.temp >
Lee Jonesb0284de2012-11-30 10:09:42 +0000690 (di->bm->temp_under + di->t_hyst_lowhigh)) &&
691 (di->batt_data.temp <= di->bm->temp_low))) {
Arun Murthy1668f812012-02-29 21:54:25 +0530692 /* TEMP minor!!!!! */
693 di->events.btemp_underover = false;
694 di->events.btemp_lowhigh = true;
Lee Jonesb0284de2012-11-30 10:09:42 +0000695 di->t_hyst_norm = di->bm->temp_hysteresis;
Arun Murthy1668f812012-02-29 21:54:25 +0530696 di->t_hyst_lowhigh = 0;
Lee Jonesb0284de2012-11-30 10:09:42 +0000697 } else if (di->batt_data.temp <= di->bm->temp_under ||
698 di->batt_data.temp >= di->bm->temp_over) {
Arun Murthy1668f812012-02-29 21:54:25 +0530699 /* TEMP major!!!!! */
700 di->events.btemp_underover = true;
701 di->events.btemp_lowhigh = false;
702 di->t_hyst_norm = 0;
Lee Jonesb0284de2012-11-30 10:09:42 +0000703 di->t_hyst_lowhigh = di->bm->temp_hysteresis;
Arun Murthy1668f812012-02-29 21:54:25 +0530704 } else {
705 /* Within hysteresis */
706 dev_dbg(di->dev, "Within hysteresis limit temp: %d "
707 "hyst_lowhigh %d, hyst normal %d\n",
708 di->batt_data.temp, di->t_hyst_lowhigh,
709 di->t_hyst_norm);
710 }
711 }
712}
713
714/**
715 * abx500_chargalg_check_charger_voltage() - Check charger voltage
716 * @di: pointer to the abx500_chargalg structure
717 *
718 * Charger voltage is checked against maximum limit
719 */
720static void abx500_chargalg_check_charger_voltage(struct abx500_chargalg *di)
721{
Lee Jonesb0284de2012-11-30 10:09:42 +0000722 if (di->chg_info.usb_volt > di->bm->chg_params->usb_volt_max)
Arun Murthy1668f812012-02-29 21:54:25 +0530723 di->chg_info.usb_chg_ok = false;
724 else
725 di->chg_info.usb_chg_ok = true;
726
Lee Jonesb0284de2012-11-30 10:09:42 +0000727 if (di->chg_info.ac_volt > di->bm->chg_params->ac_volt_max)
Arun Murthy1668f812012-02-29 21:54:25 +0530728 di->chg_info.ac_chg_ok = false;
729 else
730 di->chg_info.ac_chg_ok = true;
731
732}
733
734/**
735 * abx500_chargalg_end_of_charge() - Check if end-of-charge criteria is fulfilled
736 * @di: pointer to the abx500_chargalg structure
737 *
738 * End-of-charge criteria is fulfilled when the battery voltage is above a
739 * certain limit and the battery current is below a certain limit for a
740 * predefined number of consecutive seconds. If true, the battery is full
741 */
742static void abx500_chargalg_end_of_charge(struct abx500_chargalg *di)
743{
744 if (di->charge_status == POWER_SUPPLY_STATUS_CHARGING &&
745 di->charge_state == STATE_NORMAL &&
746 !di->maintenance_chg && (di->batt_data.volt >=
Lee Jonesb0284de2012-11-30 10:09:42 +0000747 di->bm->bat_type[di->bm->batt_id].termination_vol ||
Arun Murthy1668f812012-02-29 21:54:25 +0530748 di->events.usb_cv_active || di->events.ac_cv_active) &&
749 di->batt_data.avg_curr <
Lee Jonesb0284de2012-11-30 10:09:42 +0000750 di->bm->bat_type[di->bm->batt_id].termination_curr &&
Arun Murthy1668f812012-02-29 21:54:25 +0530751 di->batt_data.avg_curr > 0) {
752 if (++di->eoc_cnt >= EOC_COND_CNT) {
753 di->eoc_cnt = 0;
754 di->charge_status = POWER_SUPPLY_STATUS_FULL;
755 di->maintenance_chg = true;
756 dev_dbg(di->dev, "EOC reached!\n");
757 power_supply_changed(&di->chargalg_psy);
758 } else {
759 dev_dbg(di->dev,
760 " EOC limit reached for the %d"
761 " time, out of %d before EOC\n",
762 di->eoc_cnt,
763 EOC_COND_CNT);
764 }
765 } else {
766 di->eoc_cnt = 0;
767 }
768}
769
770static void init_maxim_chg_curr(struct abx500_chargalg *di)
771{
772 di->ccm.original_iset =
Lee Jonesb0284de2012-11-30 10:09:42 +0000773 di->bm->bat_type[di->bm->batt_id].normal_cur_lvl;
Arun Murthy1668f812012-02-29 21:54:25 +0530774 di->ccm.current_iset =
Lee Jonesb0284de2012-11-30 10:09:42 +0000775 di->bm->bat_type[di->bm->batt_id].normal_cur_lvl;
776 di->ccm.test_delta_i = di->bm->maxi->charger_curr_step;
777 di->ccm.max_current = di->bm->maxi->chg_curr;
778 di->ccm.condition_cnt = di->bm->maxi->wait_cycles;
Arun Murthy1668f812012-02-29 21:54:25 +0530779 di->ccm.level = 0;
780}
781
782/**
783 * abx500_chargalg_chg_curr_maxim - increases the charger current to
784 * compensate for the system load
785 * @di pointer to the abx500_chargalg structure
786 *
787 * This maximization function is used to raise the charger current to get the
788 * battery current as close to the optimal value as possible. The battery
789 * current during charging is affected by the system load
790 */
791static enum maxim_ret abx500_chargalg_chg_curr_maxim(struct abx500_chargalg *di)
792{
793 int delta_i;
794
Lee Jonesb0284de2012-11-30 10:09:42 +0000795 if (!di->bm->maxi->ena_maxi)
Arun Murthy1668f812012-02-29 21:54:25 +0530796 return MAXIM_RET_NOACTION;
797
798 delta_i = di->ccm.original_iset - di->batt_data.inst_curr;
799
800 if (di->events.vbus_collapsed) {
801 dev_dbg(di->dev, "Charger voltage has collapsed %d\n",
802 di->ccm.wait_cnt);
803 if (di->ccm.wait_cnt == 0) {
804 dev_dbg(di->dev, "lowering current\n");
805 di->ccm.wait_cnt++;
Lee Jonesb0284de2012-11-30 10:09:42 +0000806 di->ccm.condition_cnt = di->bm->maxi->wait_cycles;
Arun Murthy1668f812012-02-29 21:54:25 +0530807 di->ccm.max_current =
808 di->ccm.current_iset - di->ccm.test_delta_i;
809 di->ccm.current_iset = di->ccm.max_current;
810 di->ccm.level--;
811 return MAXIM_RET_CHANGE;
812 } else {
813 dev_dbg(di->dev, "waiting\n");
814 /* Let's go in here twice before lowering curr again */
815 di->ccm.wait_cnt = (di->ccm.wait_cnt + 1) % 3;
816 return MAXIM_RET_NOACTION;
817 }
818 }
819
820 di->ccm.wait_cnt = 0;
821
822 if ((di->batt_data.inst_curr > di->ccm.original_iset)) {
823 dev_dbg(di->dev, " Maximization Ibat (%dmA) too high"
824 " (limit %dmA) (current iset: %dmA)!\n",
825 di->batt_data.inst_curr, di->ccm.original_iset,
826 di->ccm.current_iset);
827
828 if (di->ccm.current_iset == di->ccm.original_iset)
829 return MAXIM_RET_NOACTION;
830
Lee Jonesb0284de2012-11-30 10:09:42 +0000831 di->ccm.condition_cnt = di->bm->maxi->wait_cycles;
Arun Murthy1668f812012-02-29 21:54:25 +0530832 di->ccm.current_iset = di->ccm.original_iset;
833 di->ccm.level = 0;
834
835 return MAXIM_RET_IBAT_TOO_HIGH;
836 }
837
838 if (delta_i > di->ccm.test_delta_i &&
839 (di->ccm.current_iset + di->ccm.test_delta_i) <
840 di->ccm.max_current) {
841 if (di->ccm.condition_cnt-- == 0) {
842 /* Increse the iset with cco.test_delta_i */
Lee Jonesb0284de2012-11-30 10:09:42 +0000843 di->ccm.condition_cnt = di->bm->maxi->wait_cycles;
Arun Murthy1668f812012-02-29 21:54:25 +0530844 di->ccm.current_iset += di->ccm.test_delta_i;
845 di->ccm.level++;
846 dev_dbg(di->dev, " Maximization needed, increase"
847 " with %d mA to %dmA (Optimal ibat: %d)"
848 " Level %d\n",
849 di->ccm.test_delta_i,
850 di->ccm.current_iset,
851 di->ccm.original_iset,
852 di->ccm.level);
853 return MAXIM_RET_CHANGE;
854 } else {
855 return MAXIM_RET_NOACTION;
856 }
857 } else {
Lee Jonesb0284de2012-11-30 10:09:42 +0000858 di->ccm.condition_cnt = di->bm->maxi->wait_cycles;
Arun Murthy1668f812012-02-29 21:54:25 +0530859 return MAXIM_RET_NOACTION;
860 }
861}
862
863static void handle_maxim_chg_curr(struct abx500_chargalg *di)
864{
865 enum maxim_ret ret;
866 int result;
867
868 ret = abx500_chargalg_chg_curr_maxim(di);
869 switch (ret) {
870 case MAXIM_RET_CHANGE:
871 result = abx500_chargalg_update_chg_curr(di,
872 di->ccm.current_iset);
873 if (result)
874 dev_err(di->dev, "failed to set chg curr\n");
875 break;
876 case MAXIM_RET_IBAT_TOO_HIGH:
877 result = abx500_chargalg_update_chg_curr(di,
Lee Jonesb0284de2012-11-30 10:09:42 +0000878 di->bm->bat_type[di->bm->batt_id].normal_cur_lvl);
Arun Murthy1668f812012-02-29 21:54:25 +0530879 if (result)
880 dev_err(di->dev, "failed to set chg curr\n");
881 break;
882
883 case MAXIM_RET_NOACTION:
884 default:
885 /* Do nothing..*/
886 break;
887 }
888}
889
890static int abx500_chargalg_get_ext_psy_data(struct device *dev, void *data)
891{
892 struct power_supply *psy;
893 struct power_supply *ext;
894 struct abx500_chargalg *di;
895 union power_supply_propval ret;
896 int i, j;
897 bool psy_found = false;
Marcus Cooperea402402013-01-11 13:12:54 +0000898 bool capacity_updated = false;
Arun Murthy1668f812012-02-29 21:54:25 +0530899
900 psy = (struct power_supply *)data;
901 ext = dev_get_drvdata(dev);
902 di = to_abx500_chargalg_device_info(psy);
903 /* For all psy where the driver name appears in any supplied_to */
904 for (i = 0; i < ext->num_supplicants; i++) {
905 if (!strcmp(ext->supplied_to[i], psy->name))
906 psy_found = true;
907 }
908 if (!psy_found)
909 return 0;
910
Marcus Cooperea402402013-01-11 13:12:54 +0000911 /*
912 * If external is not registering 'POWER_SUPPLY_PROP_CAPACITY' to its
913 * property because of handling that sysfs entry on its own, this is
914 * the place to get the battery capacity.
915 */
916 if (!ext->get_property(ext, POWER_SUPPLY_PROP_CAPACITY, &ret)) {
917 di->batt_data.percent = ret.intval;
918 capacity_updated = true;
919 }
920
Arun Murthy1668f812012-02-29 21:54:25 +0530921 /* Go through all properties for the psy */
922 for (j = 0; j < ext->num_properties; j++) {
923 enum power_supply_property prop;
924 prop = ext->properties[j];
925
926 /* Initialize chargers if not already done */
927 if (!di->ac_chg &&
928 ext->type == POWER_SUPPLY_TYPE_MAINS)
929 di->ac_chg = psy_to_ux500_charger(ext);
930 else if (!di->usb_chg &&
931 ext->type == POWER_SUPPLY_TYPE_USB)
932 di->usb_chg = psy_to_ux500_charger(ext);
933
934 if (ext->get_property(ext, prop, &ret))
935 continue;
936 switch (prop) {
937 case POWER_SUPPLY_PROP_PRESENT:
938 switch (ext->type) {
939 case POWER_SUPPLY_TYPE_BATTERY:
940 /* Battery present */
941 if (ret.intval)
942 di->events.batt_rem = false;
943 /* Battery removed */
944 else
945 di->events.batt_rem = true;
946 break;
947 case POWER_SUPPLY_TYPE_MAINS:
948 /* AC disconnected */
949 if (!ret.intval &&
950 (di->chg_info.conn_chg & AC_CHG)) {
951 di->chg_info.prev_conn_chg =
952 di->chg_info.conn_chg;
953 di->chg_info.conn_chg &= ~AC_CHG;
954 }
955 /* AC connected */
956 else if (ret.intval &&
957 !(di->chg_info.conn_chg & AC_CHG)) {
958 di->chg_info.prev_conn_chg =
959 di->chg_info.conn_chg;
960 di->chg_info.conn_chg |= AC_CHG;
961 }
962 break;
963 case POWER_SUPPLY_TYPE_USB:
964 /* USB disconnected */
965 if (!ret.intval &&
966 (di->chg_info.conn_chg & USB_CHG)) {
967 di->chg_info.prev_conn_chg =
968 di->chg_info.conn_chg;
969 di->chg_info.conn_chg &= ~USB_CHG;
970 }
971 /* USB connected */
972 else if (ret.intval &&
973 !(di->chg_info.conn_chg & USB_CHG)) {
974 di->chg_info.prev_conn_chg =
975 di->chg_info.conn_chg;
976 di->chg_info.conn_chg |= USB_CHG;
977 }
978 break;
979 default:
980 break;
981 }
982 break;
983
984 case POWER_SUPPLY_PROP_ONLINE:
985 switch (ext->type) {
986 case POWER_SUPPLY_TYPE_BATTERY:
987 break;
988 case POWER_SUPPLY_TYPE_MAINS:
989 /* AC offline */
990 if (!ret.intval &&
991 (di->chg_info.online_chg & AC_CHG)) {
992 di->chg_info.prev_online_chg =
993 di->chg_info.online_chg;
994 di->chg_info.online_chg &= ~AC_CHG;
995 }
996 /* AC online */
997 else if (ret.intval &&
998 !(di->chg_info.online_chg & AC_CHG)) {
999 di->chg_info.prev_online_chg =
1000 di->chg_info.online_chg;
1001 di->chg_info.online_chg |= AC_CHG;
1002 queue_delayed_work(di->chargalg_wq,
1003 &di->chargalg_wd_work, 0);
1004 }
1005 break;
1006 case POWER_SUPPLY_TYPE_USB:
1007 /* USB offline */
1008 if (!ret.intval &&
1009 (di->chg_info.online_chg & USB_CHG)) {
1010 di->chg_info.prev_online_chg =
1011 di->chg_info.online_chg;
1012 di->chg_info.online_chg &= ~USB_CHG;
1013 }
1014 /* USB online */
1015 else if (ret.intval &&
1016 !(di->chg_info.online_chg & USB_CHG)) {
1017 di->chg_info.prev_online_chg =
1018 di->chg_info.online_chg;
1019 di->chg_info.online_chg |= USB_CHG;
1020 queue_delayed_work(di->chargalg_wq,
1021 &di->chargalg_wd_work, 0);
1022 }
1023 break;
1024 default:
1025 break;
1026 }
1027 break;
1028
1029 case POWER_SUPPLY_PROP_HEALTH:
1030 switch (ext->type) {
1031 case POWER_SUPPLY_TYPE_BATTERY:
1032 break;
1033 case POWER_SUPPLY_TYPE_MAINS:
1034 switch (ret.intval) {
1035 case POWER_SUPPLY_HEALTH_UNSPEC_FAILURE:
1036 di->events.mainextchnotok = true;
1037 di->events.main_thermal_prot = false;
1038 di->events.main_ovv = false;
1039 di->events.ac_wd_expired = false;
1040 break;
1041 case POWER_SUPPLY_HEALTH_DEAD:
1042 di->events.ac_wd_expired = true;
1043 di->events.mainextchnotok = false;
1044 di->events.main_ovv = false;
1045 di->events.main_thermal_prot = false;
1046 break;
1047 case POWER_SUPPLY_HEALTH_COLD:
1048 case POWER_SUPPLY_HEALTH_OVERHEAT:
1049 di->events.main_thermal_prot = true;
1050 di->events.mainextchnotok = false;
1051 di->events.main_ovv = false;
1052 di->events.ac_wd_expired = false;
1053 break;
1054 case POWER_SUPPLY_HEALTH_OVERVOLTAGE:
1055 di->events.main_ovv = true;
1056 di->events.mainextchnotok = false;
1057 di->events.main_thermal_prot = false;
1058 di->events.ac_wd_expired = false;
1059 break;
1060 case POWER_SUPPLY_HEALTH_GOOD:
1061 di->events.main_thermal_prot = false;
1062 di->events.mainextchnotok = false;
1063 di->events.main_ovv = false;
1064 di->events.ac_wd_expired = false;
1065 break;
1066 default:
1067 break;
1068 }
1069 break;
1070
1071 case POWER_SUPPLY_TYPE_USB:
1072 switch (ret.intval) {
1073 case POWER_SUPPLY_HEALTH_UNSPEC_FAILURE:
1074 di->events.usbchargernotok = true;
1075 di->events.usb_thermal_prot = false;
1076 di->events.vbus_ovv = false;
1077 di->events.usb_wd_expired = false;
1078 break;
1079 case POWER_SUPPLY_HEALTH_DEAD:
1080 di->events.usb_wd_expired = true;
1081 di->events.usbchargernotok = false;
1082 di->events.usb_thermal_prot = false;
1083 di->events.vbus_ovv = false;
1084 break;
1085 case POWER_SUPPLY_HEALTH_COLD:
1086 case POWER_SUPPLY_HEALTH_OVERHEAT:
1087 di->events.usb_thermal_prot = true;
1088 di->events.usbchargernotok = false;
1089 di->events.vbus_ovv = false;
1090 di->events.usb_wd_expired = false;
1091 break;
1092 case POWER_SUPPLY_HEALTH_OVERVOLTAGE:
1093 di->events.vbus_ovv = true;
1094 di->events.usbchargernotok = false;
1095 di->events.usb_thermal_prot = false;
1096 di->events.usb_wd_expired = false;
1097 break;
1098 case POWER_SUPPLY_HEALTH_GOOD:
1099 di->events.usbchargernotok = false;
1100 di->events.usb_thermal_prot = false;
1101 di->events.vbus_ovv = false;
1102 di->events.usb_wd_expired = false;
1103 break;
1104 default:
1105 break;
1106 }
1107 default:
1108 break;
1109 }
1110 break;
1111
1112 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
1113 switch (ext->type) {
1114 case POWER_SUPPLY_TYPE_BATTERY:
1115 di->batt_data.volt = ret.intval / 1000;
1116 break;
1117 case POWER_SUPPLY_TYPE_MAINS:
1118 di->chg_info.ac_volt = ret.intval / 1000;
1119 break;
1120 case POWER_SUPPLY_TYPE_USB:
1121 di->chg_info.usb_volt = ret.intval / 1000;
1122 break;
1123 default:
1124 break;
1125 }
1126 break;
1127
1128 case POWER_SUPPLY_PROP_VOLTAGE_AVG:
1129 switch (ext->type) {
1130 case POWER_SUPPLY_TYPE_MAINS:
1131 /* AVG is used to indicate when we are
1132 * in CV mode */
1133 if (ret.intval)
1134 di->events.ac_cv_active = true;
1135 else
1136 di->events.ac_cv_active = false;
1137
1138 break;
1139 case POWER_SUPPLY_TYPE_USB:
1140 /* AVG is used to indicate when we are
1141 * in CV mode */
1142 if (ret.intval)
1143 di->events.usb_cv_active = true;
1144 else
1145 di->events.usb_cv_active = false;
1146
1147 break;
1148 default:
1149 break;
1150 }
1151 break;
1152
1153 case POWER_SUPPLY_PROP_TECHNOLOGY:
1154 switch (ext->type) {
1155 case POWER_SUPPLY_TYPE_BATTERY:
1156 if (ret.intval)
1157 di->events.batt_unknown = false;
1158 else
1159 di->events.batt_unknown = true;
1160
1161 break;
1162 default:
1163 break;
1164 }
1165 break;
1166
1167 case POWER_SUPPLY_PROP_TEMP:
1168 di->batt_data.temp = ret.intval / 10;
1169 break;
1170
1171 case POWER_SUPPLY_PROP_CURRENT_NOW:
1172 switch (ext->type) {
1173 case POWER_SUPPLY_TYPE_MAINS:
1174 di->chg_info.ac_curr =
1175 ret.intval / 1000;
1176 break;
1177 case POWER_SUPPLY_TYPE_USB:
1178 di->chg_info.usb_curr =
1179 ret.intval / 1000;
1180 break;
1181 case POWER_SUPPLY_TYPE_BATTERY:
1182 di->batt_data.inst_curr = ret.intval / 1000;
1183 break;
1184 default:
1185 break;
1186 }
1187 break;
1188
1189 case POWER_SUPPLY_PROP_CURRENT_AVG:
1190 switch (ext->type) {
1191 case POWER_SUPPLY_TYPE_BATTERY:
1192 di->batt_data.avg_curr = ret.intval / 1000;
1193 break;
1194 case POWER_SUPPLY_TYPE_USB:
1195 if (ret.intval)
1196 di->events.vbus_collapsed = true;
1197 else
1198 di->events.vbus_collapsed = false;
1199 break;
1200 default:
1201 break;
1202 }
1203 break;
1204 case POWER_SUPPLY_PROP_CAPACITY:
Marcus Cooperea402402013-01-11 13:12:54 +00001205 if (!capacity_updated)
1206 di->batt_data.percent = ret.intval;
Arun Murthy1668f812012-02-29 21:54:25 +05301207 break;
1208 default:
1209 break;
1210 }
1211 }
1212 return 0;
1213}
1214
1215/**
1216 * abx500_chargalg_external_power_changed() - callback for power supply changes
1217 * @psy: pointer to the structure power_supply
1218 *
1219 * This function is the entry point of the pointer external_power_changed
1220 * of the structure power_supply.
1221 * This function gets executed when there is a change in any external power
1222 * supply that this driver needs to be notified of.
1223 */
1224static void abx500_chargalg_external_power_changed(struct power_supply *psy)
1225{
1226 struct abx500_chargalg *di = to_abx500_chargalg_device_info(psy);
1227
1228 /*
1229 * Trigger execution of the algorithm instantly and read
1230 * all power_supply properties there instead
1231 */
1232 queue_work(di->chargalg_wq, &di->chargalg_work);
1233}
1234
1235/**
1236 * abx500_chargalg_algorithm() - Main function for the algorithm
1237 * @di: pointer to the abx500_chargalg structure
1238 *
1239 * This is the main control function for the charging algorithm.
1240 * It is called periodically or when something happens that will
1241 * trigger a state change
1242 */
1243static void abx500_chargalg_algorithm(struct abx500_chargalg *di)
1244{
1245 int charger_status;
Lee Jones4dcdf572013-02-14 09:24:10 +00001246 int ret;
Arun Murthy1668f812012-02-29 21:54:25 +05301247
1248 /* Collect data from all power_supply class devices */
1249 class_for_each_device(power_supply_class, NULL,
1250 &di->chargalg_psy, abx500_chargalg_get_ext_psy_data);
1251
1252 abx500_chargalg_end_of_charge(di);
1253 abx500_chargalg_check_temp(di);
1254 abx500_chargalg_check_charger_voltage(di);
1255
1256 charger_status = abx500_chargalg_check_charger_connection(di);
Lee Jones4dcdf572013-02-14 09:24:10 +00001257
1258 if (is_ab8500(di->parent)) {
1259 ret = abx500_chargalg_check_charger_enable(di);
1260 if (ret < 0)
1261 dev_err(di->dev, "Checking charger is enabled error"
1262 ": Returned Value %d\n", ret);
1263 }
1264
Arun Murthy1668f812012-02-29 21:54:25 +05301265 /*
1266 * First check if we have a charger connected.
1267 * Also we don't allow charging of unknown batteries if configured
1268 * this way
1269 */
1270 if (!charger_status ||
Lee Jonesb0284de2012-11-30 10:09:42 +00001271 (di->events.batt_unknown && !di->bm->chg_unknown_bat)) {
Arun Murthy1668f812012-02-29 21:54:25 +05301272 if (di->charge_state != STATE_HANDHELD) {
1273 di->events.safety_timer_expired = false;
1274 abx500_chargalg_state_to(di, STATE_HANDHELD_INIT);
1275 }
1276 }
1277
1278 /* If suspended, we should not continue checking the flags */
1279 else if (di->charge_state == STATE_SUSPENDED_INIT ||
1280 di->charge_state == STATE_SUSPENDED) {
1281 /* We don't do anything here, just don,t continue */
1282 }
1283
1284 /* Safety timer expiration */
1285 else if (di->events.safety_timer_expired) {
1286 if (di->charge_state != STATE_SAFETY_TIMER_EXPIRED)
1287 abx500_chargalg_state_to(di,
1288 STATE_SAFETY_TIMER_EXPIRED_INIT);
1289 }
1290 /*
1291 * Check if any interrupts has occured
1292 * that will prevent us from charging
1293 */
1294
1295 /* Battery removed */
1296 else if (di->events.batt_rem) {
1297 if (di->charge_state != STATE_BATT_REMOVED)
1298 abx500_chargalg_state_to(di, STATE_BATT_REMOVED_INIT);
1299 }
1300 /* Main or USB charger not ok. */
1301 else if (di->events.mainextchnotok || di->events.usbchargernotok) {
1302 /*
1303 * If vbus_collapsed is set, we have to lower the charger
1304 * current, which is done in the normal state below
1305 */
1306 if (di->charge_state != STATE_CHG_NOT_OK &&
1307 !di->events.vbus_collapsed)
1308 abx500_chargalg_state_to(di, STATE_CHG_NOT_OK_INIT);
1309 }
1310 /* VBUS, Main or VBAT OVV. */
1311 else if (di->events.vbus_ovv ||
1312 di->events.main_ovv ||
1313 di->events.batt_ovv ||
1314 !di->chg_info.usb_chg_ok ||
1315 !di->chg_info.ac_chg_ok) {
1316 if (di->charge_state != STATE_OVV_PROTECT)
1317 abx500_chargalg_state_to(di, STATE_OVV_PROTECT_INIT);
1318 }
1319 /* USB Thermal, stop charging */
1320 else if (di->events.main_thermal_prot ||
1321 di->events.usb_thermal_prot) {
1322 if (di->charge_state != STATE_HW_TEMP_PROTECT)
1323 abx500_chargalg_state_to(di,
1324 STATE_HW_TEMP_PROTECT_INIT);
1325 }
1326 /* Battery temp over/under */
1327 else if (di->events.btemp_underover) {
1328 if (di->charge_state != STATE_TEMP_UNDEROVER)
1329 abx500_chargalg_state_to(di,
1330 STATE_TEMP_UNDEROVER_INIT);
1331 }
1332 /* Watchdog expired */
1333 else if (di->events.ac_wd_expired ||
1334 di->events.usb_wd_expired) {
1335 if (di->charge_state != STATE_WD_EXPIRED)
1336 abx500_chargalg_state_to(di, STATE_WD_EXPIRED_INIT);
1337 }
1338 /* Battery temp high/low */
1339 else if (di->events.btemp_lowhigh) {
1340 if (di->charge_state != STATE_TEMP_LOWHIGH)
1341 abx500_chargalg_state_to(di, STATE_TEMP_LOWHIGH_INIT);
1342 }
1343
1344 dev_dbg(di->dev,
1345 "[CHARGALG] Vb %d Ib_avg %d Ib_inst %d Tb %d Cap %d Maint %d "
1346 "State %s Active_chg %d Chg_status %d AC %d USB %d "
1347 "AC_online %d USB_online %d AC_CV %d USB_CV %d AC_I %d "
1348 "USB_I %d AC_Vset %d AC_Iset %d USB_Vset %d USB_Iset %d\n",
1349 di->batt_data.volt,
1350 di->batt_data.avg_curr,
1351 di->batt_data.inst_curr,
1352 di->batt_data.temp,
1353 di->batt_data.percent,
1354 di->maintenance_chg,
1355 states[di->charge_state],
1356 di->chg_info.charger_type,
1357 di->charge_status,
1358 di->chg_info.conn_chg & AC_CHG,
1359 di->chg_info.conn_chg & USB_CHG,
1360 di->chg_info.online_chg & AC_CHG,
1361 di->chg_info.online_chg & USB_CHG,
1362 di->events.ac_cv_active,
1363 di->events.usb_cv_active,
1364 di->chg_info.ac_curr,
1365 di->chg_info.usb_curr,
1366 di->chg_info.ac_vset,
1367 di->chg_info.ac_iset,
1368 di->chg_info.usb_vset,
1369 di->chg_info.usb_iset);
1370
1371 switch (di->charge_state) {
1372 case STATE_HANDHELD_INIT:
1373 abx500_chargalg_stop_charging(di);
1374 di->charge_status = POWER_SUPPLY_STATUS_DISCHARGING;
1375 abx500_chargalg_state_to(di, STATE_HANDHELD);
1376 /* Intentional fallthrough */
1377
1378 case STATE_HANDHELD:
1379 break;
1380
1381 case STATE_SUSPENDED_INIT:
1382 if (di->susp_status.ac_suspended)
1383 abx500_chargalg_ac_en(di, false, 0, 0);
1384 if (di->susp_status.usb_suspended)
1385 abx500_chargalg_usb_en(di, false, 0, 0);
1386 abx500_chargalg_stop_safety_timer(di);
1387 abx500_chargalg_stop_maintenance_timer(di);
1388 di->charge_status = POWER_SUPPLY_STATUS_NOT_CHARGING;
1389 di->maintenance_chg = false;
1390 abx500_chargalg_state_to(di, STATE_SUSPENDED);
1391 power_supply_changed(&di->chargalg_psy);
1392 /* Intentional fallthrough */
1393
1394 case STATE_SUSPENDED:
1395 /* CHARGING is suspended */
1396 break;
1397
1398 case STATE_BATT_REMOVED_INIT:
1399 abx500_chargalg_stop_charging(di);
1400 abx500_chargalg_state_to(di, STATE_BATT_REMOVED);
1401 /* Intentional fallthrough */
1402
1403 case STATE_BATT_REMOVED:
1404 if (!di->events.batt_rem)
1405 abx500_chargalg_state_to(di, STATE_NORMAL_INIT);
1406 break;
1407
1408 case STATE_HW_TEMP_PROTECT_INIT:
1409 abx500_chargalg_stop_charging(di);
1410 abx500_chargalg_state_to(di, STATE_HW_TEMP_PROTECT);
1411 /* Intentional fallthrough */
1412
1413 case STATE_HW_TEMP_PROTECT:
1414 if (!di->events.main_thermal_prot &&
1415 !di->events.usb_thermal_prot)
1416 abx500_chargalg_state_to(di, STATE_NORMAL_INIT);
1417 break;
1418
1419 case STATE_OVV_PROTECT_INIT:
1420 abx500_chargalg_stop_charging(di);
1421 abx500_chargalg_state_to(di, STATE_OVV_PROTECT);
1422 /* Intentional fallthrough */
1423
1424 case STATE_OVV_PROTECT:
1425 if (!di->events.vbus_ovv &&
1426 !di->events.main_ovv &&
1427 !di->events.batt_ovv &&
1428 di->chg_info.usb_chg_ok &&
1429 di->chg_info.ac_chg_ok)
1430 abx500_chargalg_state_to(di, STATE_NORMAL_INIT);
1431 break;
1432
1433 case STATE_CHG_NOT_OK_INIT:
1434 abx500_chargalg_stop_charging(di);
1435 abx500_chargalg_state_to(di, STATE_CHG_NOT_OK);
1436 /* Intentional fallthrough */
1437
1438 case STATE_CHG_NOT_OK:
1439 if (!di->events.mainextchnotok &&
1440 !di->events.usbchargernotok)
1441 abx500_chargalg_state_to(di, STATE_NORMAL_INIT);
1442 break;
1443
1444 case STATE_SAFETY_TIMER_EXPIRED_INIT:
1445 abx500_chargalg_stop_charging(di);
1446 abx500_chargalg_state_to(di, STATE_SAFETY_TIMER_EXPIRED);
1447 /* Intentional fallthrough */
1448
1449 case STATE_SAFETY_TIMER_EXPIRED:
1450 /* We exit this state when charger is removed */
1451 break;
1452
1453 case STATE_NORMAL_INIT:
1454 abx500_chargalg_start_charging(di,
Lee Jonesb0284de2012-11-30 10:09:42 +00001455 di->bm->bat_type[di->bm->batt_id].normal_vol_lvl,
1456 di->bm->bat_type[di->bm->batt_id].normal_cur_lvl);
Arun Murthy1668f812012-02-29 21:54:25 +05301457 abx500_chargalg_state_to(di, STATE_NORMAL);
1458 abx500_chargalg_start_safety_timer(di);
1459 abx500_chargalg_stop_maintenance_timer(di);
1460 init_maxim_chg_curr(di);
1461 di->charge_status = POWER_SUPPLY_STATUS_CHARGING;
1462 di->eoc_cnt = 0;
1463 di->maintenance_chg = false;
1464 power_supply_changed(&di->chargalg_psy);
1465
1466 break;
1467
1468 case STATE_NORMAL:
1469 handle_maxim_chg_curr(di);
1470 if (di->charge_status == POWER_SUPPLY_STATUS_FULL &&
1471 di->maintenance_chg) {
Lee Jonesb0284de2012-11-30 10:09:42 +00001472 if (di->bm->no_maintenance)
Arun Murthy1668f812012-02-29 21:54:25 +05301473 abx500_chargalg_state_to(di,
1474 STATE_WAIT_FOR_RECHARGE_INIT);
1475 else
1476 abx500_chargalg_state_to(di,
1477 STATE_MAINTENANCE_A_INIT);
1478 }
1479 break;
1480
1481 /* This state will be used when the maintenance state is disabled */
1482 case STATE_WAIT_FOR_RECHARGE_INIT:
1483 abx500_chargalg_hold_charging(di);
1484 abx500_chargalg_state_to(di, STATE_WAIT_FOR_RECHARGE);
Arun Murthy1668f812012-02-29 21:54:25 +05301485 /* Intentional fallthrough */
1486
1487 case STATE_WAIT_FOR_RECHARGE:
Marcus Cooperea402402013-01-11 13:12:54 +00001488 if (di->batt_data.percent <=
1489 di->bm->bat_type[di->bm->batt_id].
1490 recharge_cap)
1491 abx500_chargalg_state_to(di, STATE_NORMAL_INIT);
Arun Murthy1668f812012-02-29 21:54:25 +05301492 break;
1493
1494 case STATE_MAINTENANCE_A_INIT:
1495 abx500_chargalg_stop_safety_timer(di);
1496 abx500_chargalg_start_maintenance_timer(di,
Lee Jonesb0284de2012-11-30 10:09:42 +00001497 di->bm->bat_type[
1498 di->bm->batt_id].maint_a_chg_timer_h);
Arun Murthy1668f812012-02-29 21:54:25 +05301499 abx500_chargalg_start_charging(di,
Lee Jonesb0284de2012-11-30 10:09:42 +00001500 di->bm->bat_type[
1501 di->bm->batt_id].maint_a_vol_lvl,
1502 di->bm->bat_type[
1503 di->bm->batt_id].maint_a_cur_lvl);
Arun Murthy1668f812012-02-29 21:54:25 +05301504 abx500_chargalg_state_to(di, STATE_MAINTENANCE_A);
1505 power_supply_changed(&di->chargalg_psy);
1506 /* Intentional fallthrough*/
1507
1508 case STATE_MAINTENANCE_A:
1509 if (di->events.maintenance_timer_expired) {
1510 abx500_chargalg_stop_maintenance_timer(di);
1511 abx500_chargalg_state_to(di, STATE_MAINTENANCE_B_INIT);
1512 }
1513 break;
1514
1515 case STATE_MAINTENANCE_B_INIT:
1516 abx500_chargalg_start_maintenance_timer(di,
Lee Jonesb0284de2012-11-30 10:09:42 +00001517 di->bm->bat_type[
1518 di->bm->batt_id].maint_b_chg_timer_h);
Arun Murthy1668f812012-02-29 21:54:25 +05301519 abx500_chargalg_start_charging(di,
Lee Jonesb0284de2012-11-30 10:09:42 +00001520 di->bm->bat_type[
1521 di->bm->batt_id].maint_b_vol_lvl,
1522 di->bm->bat_type[
1523 di->bm->batt_id].maint_b_cur_lvl);
Arun Murthy1668f812012-02-29 21:54:25 +05301524 abx500_chargalg_state_to(di, STATE_MAINTENANCE_B);
1525 power_supply_changed(&di->chargalg_psy);
1526 /* Intentional fallthrough*/
1527
1528 case STATE_MAINTENANCE_B:
1529 if (di->events.maintenance_timer_expired) {
1530 abx500_chargalg_stop_maintenance_timer(di);
1531 abx500_chargalg_state_to(di, STATE_NORMAL_INIT);
1532 }
1533 break;
1534
1535 case STATE_TEMP_LOWHIGH_INIT:
1536 abx500_chargalg_start_charging(di,
Lee Jonesb0284de2012-11-30 10:09:42 +00001537 di->bm->bat_type[
1538 di->bm->batt_id].low_high_vol_lvl,
1539 di->bm->bat_type[
1540 di->bm->batt_id].low_high_cur_lvl);
Arun Murthy1668f812012-02-29 21:54:25 +05301541 abx500_chargalg_stop_maintenance_timer(di);
1542 di->charge_status = POWER_SUPPLY_STATUS_CHARGING;
1543 abx500_chargalg_state_to(di, STATE_TEMP_LOWHIGH);
1544 power_supply_changed(&di->chargalg_psy);
1545 /* Intentional fallthrough */
1546
1547 case STATE_TEMP_LOWHIGH:
1548 if (!di->events.btemp_lowhigh)
1549 abx500_chargalg_state_to(di, STATE_NORMAL_INIT);
1550 break;
1551
1552 case STATE_WD_EXPIRED_INIT:
1553 abx500_chargalg_stop_charging(di);
1554 abx500_chargalg_state_to(di, STATE_WD_EXPIRED);
1555 /* Intentional fallthrough */
1556
1557 case STATE_WD_EXPIRED:
1558 if (!di->events.ac_wd_expired &&
1559 !di->events.usb_wd_expired)
1560 abx500_chargalg_state_to(di, STATE_NORMAL_INIT);
1561 break;
1562
1563 case STATE_TEMP_UNDEROVER_INIT:
1564 abx500_chargalg_stop_charging(di);
1565 abx500_chargalg_state_to(di, STATE_TEMP_UNDEROVER);
1566 /* Intentional fallthrough */
1567
1568 case STATE_TEMP_UNDEROVER:
1569 if (!di->events.btemp_underover)
1570 abx500_chargalg_state_to(di, STATE_NORMAL_INIT);
1571 break;
1572 }
1573
1574 /* Start charging directly if the new state is a charge state */
1575 if (di->charge_state == STATE_NORMAL_INIT ||
1576 di->charge_state == STATE_MAINTENANCE_A_INIT ||
1577 di->charge_state == STATE_MAINTENANCE_B_INIT)
1578 queue_work(di->chargalg_wq, &di->chargalg_work);
1579}
1580
1581/**
1582 * abx500_chargalg_periodic_work() - Periodic work for the algorithm
1583 * @work: pointer to the work_struct structure
1584 *
1585 * Work queue function for the charging algorithm
1586 */
1587static void abx500_chargalg_periodic_work(struct work_struct *work)
1588{
1589 struct abx500_chargalg *di = container_of(work,
1590 struct abx500_chargalg, chargalg_periodic_work.work);
1591
1592 abx500_chargalg_algorithm(di);
1593
1594 /*
1595 * If a charger is connected then the battery has to be monitored
1596 * frequently, else the work can be delayed.
1597 */
1598 if (di->chg_info.conn_chg)
1599 queue_delayed_work(di->chargalg_wq,
1600 &di->chargalg_periodic_work,
Lee Jonesb0284de2012-11-30 10:09:42 +00001601 di->bm->interval_charging * HZ);
Arun Murthy1668f812012-02-29 21:54:25 +05301602 else
1603 queue_delayed_work(di->chargalg_wq,
1604 &di->chargalg_periodic_work,
Lee Jonesb0284de2012-11-30 10:09:42 +00001605 di->bm->interval_not_charging * HZ);
Arun Murthy1668f812012-02-29 21:54:25 +05301606}
1607
1608/**
1609 * abx500_chargalg_wd_work() - periodic work to kick the charger watchdog
1610 * @work: pointer to the work_struct structure
1611 *
1612 * Work queue function for kicking the charger watchdog
1613 */
1614static void abx500_chargalg_wd_work(struct work_struct *work)
1615{
1616 int ret;
1617 struct abx500_chargalg *di = container_of(work,
1618 struct abx500_chargalg, chargalg_wd_work.work);
1619
1620 dev_dbg(di->dev, "abx500_chargalg_wd_work\n");
1621
1622 ret = abx500_chargalg_kick_watchdog(di);
1623 if (ret < 0)
1624 dev_err(di->dev, "failed to kick watchdog\n");
1625
1626 queue_delayed_work(di->chargalg_wq,
1627 &di->chargalg_wd_work, CHG_WD_INTERVAL);
1628}
1629
1630/**
1631 * abx500_chargalg_work() - Work to run the charging algorithm instantly
1632 * @work: pointer to the work_struct structure
1633 *
1634 * Work queue function for calling the charging algorithm
1635 */
1636static void abx500_chargalg_work(struct work_struct *work)
1637{
1638 struct abx500_chargalg *di = container_of(work,
1639 struct abx500_chargalg, chargalg_work);
1640
1641 abx500_chargalg_algorithm(di);
1642}
1643
1644/**
1645 * abx500_chargalg_get_property() - get the chargalg properties
1646 * @psy: pointer to the power_supply structure
1647 * @psp: pointer to the power_supply_property structure
1648 * @val: pointer to the power_supply_propval union
1649 *
1650 * This function gets called when an application tries to get the
1651 * chargalg properties by reading the sysfs files.
1652 * status: charging/discharging/full/unknown
1653 * health: health of the battery
1654 * Returns error code in case of failure else 0 on success
1655 */
1656static int abx500_chargalg_get_property(struct power_supply *psy,
1657 enum power_supply_property psp,
1658 union power_supply_propval *val)
1659{
1660 struct abx500_chargalg *di;
1661
1662 di = to_abx500_chargalg_device_info(psy);
1663
1664 switch (psp) {
1665 case POWER_SUPPLY_PROP_STATUS:
1666 val->intval = di->charge_status;
1667 break;
1668 case POWER_SUPPLY_PROP_HEALTH:
1669 if (di->events.batt_ovv) {
1670 val->intval = POWER_SUPPLY_HEALTH_OVERVOLTAGE;
1671 } else if (di->events.btemp_underover) {
Lee Jonesb0284de2012-11-30 10:09:42 +00001672 if (di->batt_data.temp <= di->bm->temp_under)
Arun Murthy1668f812012-02-29 21:54:25 +05301673 val->intval = POWER_SUPPLY_HEALTH_COLD;
1674 else
1675 val->intval = POWER_SUPPLY_HEALTH_OVERHEAT;
Lee Jonesd80108f2013-01-17 13:49:45 +00001676 } else if (di->charge_state == STATE_SAFETY_TIMER_EXPIRED ||
1677 di->charge_state == STATE_SAFETY_TIMER_EXPIRED_INIT) {
1678 val->intval = POWER_SUPPLY_HEALTH_UNSPEC_FAILURE;
Arun Murthy1668f812012-02-29 21:54:25 +05301679 } else {
1680 val->intval = POWER_SUPPLY_HEALTH_GOOD;
1681 }
1682 break;
1683 default:
1684 return -EINVAL;
1685 }
1686 return 0;
1687}
1688
1689/* Exposure to the sysfs interface */
1690
1691/**
Lee Jonesc9ade0f2013-01-21 11:22:56 +00001692 * abx500_chargalg_sysfs_show() - sysfs show operations
1693 * @kobj: pointer to the struct kobject
1694 * @attr: pointer to the struct attribute
1695 * @buf: buffer that holds the parameter to send to userspace
1696 *
1697 * Returns a buffer to be displayed in user space
1698 */
1699static ssize_t abx500_chargalg_sysfs_show(struct kobject *kobj,
1700 struct attribute *attr, char *buf)
1701{
1702 struct abx500_chargalg *di = container_of(kobj,
1703 struct abx500_chargalg, chargalg_kobject);
1704
1705 return sprintf(buf, "%d\n",
1706 di->susp_status.ac_suspended &&
1707 di->susp_status.usb_suspended);
1708}
1709
1710/**
Arun Murthy1668f812012-02-29 21:54:25 +05301711 * abx500_chargalg_sysfs_charger() - sysfs store operations
1712 * @kobj: pointer to the struct kobject
1713 * @attr: pointer to the struct attribute
1714 * @buf: buffer that holds the parameter passed from userspace
1715 * @length: length of the parameter passed
1716 *
1717 * Returns length of the buffer(input taken from user space) on success
1718 * else error code on failure
1719 * The operation to be performed on passing the parameters from the user space.
1720 */
1721static ssize_t abx500_chargalg_sysfs_charger(struct kobject *kobj,
1722 struct attribute *attr, const char *buf, size_t length)
1723{
1724 struct abx500_chargalg *di = container_of(kobj,
1725 struct abx500_chargalg, chargalg_kobject);
1726 long int param;
1727 int ac_usb;
1728 int ret;
1729 char entry = *attr->name;
1730
1731 switch (entry) {
1732 case 'c':
1733 ret = strict_strtol(buf, 10, &param);
1734 if (ret < 0)
1735 return ret;
1736
1737 ac_usb = param;
1738 switch (ac_usb) {
1739 case 0:
1740 /* Disable charging */
1741 di->susp_status.ac_suspended = true;
1742 di->susp_status.usb_suspended = true;
1743 di->susp_status.suspended_change = true;
1744 /* Trigger a state change */
1745 queue_work(di->chargalg_wq,
1746 &di->chargalg_work);
1747 break;
1748 case 1:
1749 /* Enable AC Charging */
1750 di->susp_status.ac_suspended = false;
1751 di->susp_status.suspended_change = true;
1752 /* Trigger a state change */
1753 queue_work(di->chargalg_wq,
1754 &di->chargalg_work);
1755 break;
1756 case 2:
1757 /* Enable USB charging */
1758 di->susp_status.usb_suspended = false;
1759 di->susp_status.suspended_change = true;
1760 /* Trigger a state change */
1761 queue_work(di->chargalg_wq,
1762 &di->chargalg_work);
1763 break;
1764 default:
1765 dev_info(di->dev, "Wrong input\n"
1766 "Enter 0. Disable AC/USB Charging\n"
1767 "1. Enable AC charging\n"
1768 "2. Enable USB Charging\n");
1769 };
1770 break;
1771 };
1772 return strlen(buf);
1773}
1774
1775static struct attribute abx500_chargalg_en_charger = \
1776{
1777 .name = "chargalg",
Lee Jonesc9ade0f2013-01-21 11:22:56 +00001778 .mode = S_IRUGO | S_IWUSR,
Arun Murthy1668f812012-02-29 21:54:25 +05301779};
1780
1781static struct attribute *abx500_chargalg_chg[] = {
1782 &abx500_chargalg_en_charger,
1783 NULL
1784};
1785
Anton Vorontsov64eb9b02012-03-14 04:43:11 +04001786static const struct sysfs_ops abx500_chargalg_sysfs_ops = {
Lee Jonesc9ade0f2013-01-21 11:22:56 +00001787 .show = abx500_chargalg_sysfs_show,
Arun Murthy1668f812012-02-29 21:54:25 +05301788 .store = abx500_chargalg_sysfs_charger,
1789};
1790
1791static struct kobj_type abx500_chargalg_ktype = {
1792 .sysfs_ops = &abx500_chargalg_sysfs_ops,
1793 .default_attrs = abx500_chargalg_chg,
1794};
1795
1796/**
1797 * abx500_chargalg_sysfs_exit() - de-init of sysfs entry
1798 * @di: pointer to the struct abx500_chargalg
1799 *
1800 * This function removes the entry in sysfs.
1801 */
1802static void abx500_chargalg_sysfs_exit(struct abx500_chargalg *di)
1803{
1804 kobject_del(&di->chargalg_kobject);
1805}
1806
1807/**
1808 * abx500_chargalg_sysfs_init() - init of sysfs entry
1809 * @di: pointer to the struct abx500_chargalg
1810 *
1811 * This function adds an entry in sysfs.
1812 * Returns error code in case of failure else 0(on success)
1813 */
1814static int abx500_chargalg_sysfs_init(struct abx500_chargalg *di)
1815{
1816 int ret = 0;
1817
1818 ret = kobject_init_and_add(&di->chargalg_kobject,
1819 &abx500_chargalg_ktype,
1820 NULL, "abx500_chargalg");
1821 if (ret < 0)
1822 dev_err(di->dev, "failed to create sysfs entry\n");
1823
1824 return ret;
1825}
1826/* Exposure to the sysfs interface <<END>> */
1827
1828#if defined(CONFIG_PM)
1829static int abx500_chargalg_resume(struct platform_device *pdev)
1830{
1831 struct abx500_chargalg *di = platform_get_drvdata(pdev);
1832
1833 /* Kick charger watchdog if charging (any charger online) */
1834 if (di->chg_info.online_chg)
1835 queue_delayed_work(di->chargalg_wq, &di->chargalg_wd_work, 0);
1836
1837 /*
1838 * Run the charging algorithm directly to be sure we don't
1839 * do it too seldom
1840 */
1841 queue_delayed_work(di->chargalg_wq, &di->chargalg_periodic_work, 0);
1842
1843 return 0;
1844}
1845
1846static int abx500_chargalg_suspend(struct platform_device *pdev,
1847 pm_message_t state)
1848{
1849 struct abx500_chargalg *di = platform_get_drvdata(pdev);
1850
1851 if (di->chg_info.online_chg)
1852 cancel_delayed_work_sync(&di->chargalg_wd_work);
1853
1854 cancel_delayed_work_sync(&di->chargalg_periodic_work);
1855
1856 return 0;
1857}
1858#else
1859#define abx500_chargalg_suspend NULL
1860#define abx500_chargalg_resume NULL
1861#endif
1862
Bill Pemberton415ec692012-11-19 13:26:07 -05001863static int abx500_chargalg_remove(struct platform_device *pdev)
Arun Murthy1668f812012-02-29 21:54:25 +05301864{
1865 struct abx500_chargalg *di = platform_get_drvdata(pdev);
1866
1867 /* sysfs interface to enable/disbale charging from user space */
1868 abx500_chargalg_sysfs_exit(di);
1869
1870 /* Delete the work queue */
1871 destroy_workqueue(di->chargalg_wq);
1872
1873 flush_scheduled_work();
1874 power_supply_unregister(&di->chargalg_psy);
1875 platform_set_drvdata(pdev, NULL);
Arun Murthy1668f812012-02-29 21:54:25 +05301876
1877 return 0;
1878}
1879
Rajanikanth H.Va12810a2012-10-31 15:40:33 +00001880static char *supply_interface[] = {
1881 "ab8500_fg",
1882};
1883
Bill Pembertonc8afa642012-11-19 13:22:23 -05001884static int abx500_chargalg_probe(struct platform_device *pdev)
Arun Murthy1668f812012-02-29 21:54:25 +05301885{
Rajanikanth H.Va12810a2012-10-31 15:40:33 +00001886 struct device_node *np = pdev->dev.of_node;
Lee Jonesbdc56b42012-11-30 10:57:14 +00001887 struct abx500_bm_data *plat = pdev->dev.platform_data;
Rajanikanth H.Va12810a2012-10-31 15:40:33 +00001888 struct abx500_chargalg *di;
Arun Murthy1668f812012-02-29 21:54:25 +05301889 int ret = 0;
1890
Rajanikanth H.Va12810a2012-10-31 15:40:33 +00001891 di = devm_kzalloc(&pdev->dev, sizeof(*di), GFP_KERNEL);
1892 if (!di) {
1893 dev_err(&pdev->dev, "%s no mem for ab8500_chargalg\n", __func__);
Arun Murthy1668f812012-02-29 21:54:25 +05301894 return -ENOMEM;
Rajanikanth H.Va12810a2012-10-31 15:40:33 +00001895 }
Lee Jonesbdc56b42012-11-30 10:57:14 +00001896
1897 if (!plat) {
1898 dev_err(&pdev->dev, "no battery management data supplied\n");
1899 return -EINVAL;
1900 }
1901 di->bm = plat;
1902
1903 if (np) {
1904 ret = ab8500_bm_of_probe(&pdev->dev, np, di->bm);
1905 if (ret) {
1906 dev_err(&pdev->dev, "failed to get battery information\n");
1907 return ret;
Rajanikanth H.Va12810a2012-10-31 15:40:33 +00001908 }
Rajanikanth H.Va12810a2012-10-31 15:40:33 +00001909 }
Arun Murthy1668f812012-02-29 21:54:25 +05301910
Lee Jones330b7eb2013-02-15 10:53:57 +00001911 /* get device struct and parent */
Arun Murthy1668f812012-02-29 21:54:25 +05301912 di->dev = &pdev->dev;
Lee Jones330b7eb2013-02-15 10:53:57 +00001913 di->parent = dev_get_drvdata(pdev->dev.parent);
Arun Murthy1668f812012-02-29 21:54:25 +05301914
1915 /* chargalg supply */
1916 di->chargalg_psy.name = "abx500_chargalg";
1917 di->chargalg_psy.type = POWER_SUPPLY_TYPE_BATTERY;
1918 di->chargalg_psy.properties = abx500_chargalg_props;
1919 di->chargalg_psy.num_properties = ARRAY_SIZE(abx500_chargalg_props);
1920 di->chargalg_psy.get_property = abx500_chargalg_get_property;
Rajanikanth H.Va12810a2012-10-31 15:40:33 +00001921 di->chargalg_psy.supplied_to = supply_interface;
1922 di->chargalg_psy.num_supplicants = ARRAY_SIZE(supply_interface),
Arun Murthy1668f812012-02-29 21:54:25 +05301923 di->chargalg_psy.external_power_changed =
1924 abx500_chargalg_external_power_changed;
1925
1926 /* Initilialize safety timer */
1927 init_timer(&di->safety_timer);
1928 di->safety_timer.function = abx500_chargalg_safety_timer_expired;
1929 di->safety_timer.data = (unsigned long) di;
1930
1931 /* Initilialize maintenance timer */
1932 init_timer(&di->maintenance_timer);
1933 di->maintenance_timer.function =
1934 abx500_chargalg_maintenance_timer_expired;
1935 di->maintenance_timer.data = (unsigned long) di;
1936
1937 /* Create a work queue for the chargalg */
1938 di->chargalg_wq =
1939 create_singlethread_workqueue("abx500_chargalg_wq");
1940 if (di->chargalg_wq == NULL) {
1941 dev_err(di->dev, "failed to create work queue\n");
Rajanikanth H.Va12810a2012-10-31 15:40:33 +00001942 return -ENOMEM;
Arun Murthy1668f812012-02-29 21:54:25 +05301943 }
1944
1945 /* Init work for chargalg */
Tejun Heo203b42f2012-08-21 13:18:23 -07001946 INIT_DEFERRABLE_WORK(&di->chargalg_periodic_work,
Arun Murthy1668f812012-02-29 21:54:25 +05301947 abx500_chargalg_periodic_work);
Tejun Heo203b42f2012-08-21 13:18:23 -07001948 INIT_DEFERRABLE_WORK(&di->chargalg_wd_work,
Arun Murthy1668f812012-02-29 21:54:25 +05301949 abx500_chargalg_wd_work);
1950
1951 /* Init work for chargalg */
1952 INIT_WORK(&di->chargalg_work, abx500_chargalg_work);
1953
1954 /* To detect charger at startup */
1955 di->chg_info.prev_conn_chg = -1;
1956
1957 /* Register chargalg power supply class */
1958 ret = power_supply_register(di->dev, &di->chargalg_psy);
1959 if (ret) {
1960 dev_err(di->dev, "failed to register chargalg psy\n");
1961 goto free_chargalg_wq;
1962 }
1963
1964 platform_set_drvdata(pdev, di);
1965
1966 /* sysfs interface to enable/disable charging from user space */
1967 ret = abx500_chargalg_sysfs_init(di);
1968 if (ret) {
1969 dev_err(di->dev, "failed to create sysfs entry\n");
1970 goto free_psy;
1971 }
1972
1973 /* Run the charging algorithm */
1974 queue_delayed_work(di->chargalg_wq, &di->chargalg_periodic_work, 0);
1975
1976 dev_info(di->dev, "probe success\n");
1977 return ret;
1978
1979free_psy:
1980 power_supply_unregister(&di->chargalg_psy);
1981free_chargalg_wq:
1982 destroy_workqueue(di->chargalg_wq);
Arun Murthy1668f812012-02-29 21:54:25 +05301983 return ret;
1984}
1985
Rajanikanth H.Va12810a2012-10-31 15:40:33 +00001986static const struct of_device_id ab8500_chargalg_match[] = {
1987 { .compatible = "stericsson,ab8500-chargalg", },
1988 { },
1989};
1990
Arun Murthy1668f812012-02-29 21:54:25 +05301991static struct platform_driver abx500_chargalg_driver = {
1992 .probe = abx500_chargalg_probe,
Bill Pemberton28ea73f2012-11-19 13:20:40 -05001993 .remove = abx500_chargalg_remove,
Arun Murthy1668f812012-02-29 21:54:25 +05301994 .suspend = abx500_chargalg_suspend,
1995 .resume = abx500_chargalg_resume,
1996 .driver = {
Rajanikanth H.Va12810a2012-10-31 15:40:33 +00001997 .name = "ab8500-chargalg",
Arun Murthy1668f812012-02-29 21:54:25 +05301998 .owner = THIS_MODULE,
Rajanikanth H.Va12810a2012-10-31 15:40:33 +00001999 .of_match_table = ab8500_chargalg_match,
Arun Murthy1668f812012-02-29 21:54:25 +05302000 },
2001};
2002
2003static int __init abx500_chargalg_init(void)
2004{
2005 return platform_driver_register(&abx500_chargalg_driver);
2006}
2007
2008static void __exit abx500_chargalg_exit(void)
2009{
2010 platform_driver_unregister(&abx500_chargalg_driver);
2011}
2012
2013module_init(abx500_chargalg_init);
2014module_exit(abx500_chargalg_exit);
2015
2016MODULE_LICENSE("GPL v2");
2017MODULE_AUTHOR("Johan Palsson, Karl Komierowski");
2018MODULE_ALIAS("platform:abx500-chargalg");
2019MODULE_DESCRIPTION("abx500 battery charging algorithm");