blob: 541f702e0451c732b7d00efed27fd53f05b03b79 [file] [log] [blame]
Arun Murthy1668f812012-02-29 21:54:25 +05301/*
2 * Copyright (C) ST-Ericsson SA 2012
Lee Jones257107a2013-02-13 15:15:03 +00003 * Copyright (c) 2012 Sony Mobile Communications AB
Arun Murthy1668f812012-02-29 21:54:25 +05304 *
5 * Charging algorithm driver for abx500 variants
6 *
7 * License Terms: GNU General Public License v2
8 * Authors:
9 * Johan Palsson <johan.palsson@stericsson.com>
10 * Karl Komierowski <karl.komierowski@stericsson.com>
11 * Arun R Murthy <arun.murthy@stericsson.com>
Lee Jones257107a2013-02-13 15:15:03 +000012 * Author: Imre Sunyi <imre.sunyi@sonymobile.com>
Arun Murthy1668f812012-02-29 21:54:25 +053013 */
14
15#include <linux/init.h>
16#include <linux/module.h>
17#include <linux/device.h>
Lee Jones257107a2013-02-13 15:15:03 +000018#include <linux/hrtimer.h>
Arun Murthy1668f812012-02-29 21:54:25 +053019#include <linux/interrupt.h>
20#include <linux/delay.h>
21#include <linux/slab.h>
22#include <linux/platform_device.h>
23#include <linux/power_supply.h>
24#include <linux/completion.h>
25#include <linux/workqueue.h>
26#include <linux/kobject.h>
Rajanikanth H.Va12810a2012-10-31 15:40:33 +000027#include <linux/of.h>
28#include <linux/mfd/core.h>
Arun Murthy1668f812012-02-29 21:54:25 +053029#include <linux/mfd/abx500.h>
Lee Jones257107a2013-02-13 15:15:03 +000030#include <linux/mfd/abx500/ab8500.h>
Arun Murthy1668f812012-02-29 21:54:25 +053031#include <linux/mfd/abx500/ux500_chargalg.h>
32#include <linux/mfd/abx500/ab8500-bm.h>
Lee Jones88917162013-02-13 11:39:19 +000033#include <linux/notifier.h>
Arun Murthy1668f812012-02-29 21:54:25 +053034
35/* Watchdog kick interval */
36#define CHG_WD_INTERVAL (6 * HZ)
37
38/* End-of-charge criteria counter */
39#define EOC_COND_CNT 10
40
Lee Jones257107a2013-02-13 15:15:03 +000041/* One hour expressed in seconds */
42#define ONE_HOUR_IN_SECONDS 3600
43
44/* Five minutes expressed in seconds */
45#define FIVE_MINUTES_IN_SECONDS 300
46
Lee Jonesdb43e6c2013-02-14 12:39:15 +000047/* Plus margin for the low battery threshold */
48#define BAT_PLUS_MARGIN (100)
49
Lee Jones4d3b4aa2013-02-15 10:58:58 +000050#define CHARGALG_CURR_STEP_LOW 0
51#define CHARGALG_CURR_STEP_HIGH 100
52
Arun Murthy1668f812012-02-29 21:54:25 +053053enum abx500_chargers {
54 NO_CHG,
55 AC_CHG,
56 USB_CHG,
57};
58
59struct abx500_chargalg_charger_info {
60 enum abx500_chargers conn_chg;
61 enum abx500_chargers prev_conn_chg;
62 enum abx500_chargers online_chg;
63 enum abx500_chargers prev_online_chg;
64 enum abx500_chargers charger_type;
65 bool usb_chg_ok;
66 bool ac_chg_ok;
67 int usb_volt;
68 int usb_curr;
69 int ac_volt;
70 int ac_curr;
71 int usb_vset;
72 int usb_iset;
73 int ac_vset;
74 int ac_iset;
75};
76
77struct abx500_chargalg_suspension_status {
78 bool suspended_change;
79 bool ac_suspended;
80 bool usb_suspended;
81};
82
Lee Jones4d3b4aa2013-02-15 10:58:58 +000083struct abx500_chargalg_current_step_status {
84 bool curr_step_change;
85 int curr_step;
86};
87
Arun Murthy1668f812012-02-29 21:54:25 +053088struct abx500_chargalg_battery_data {
89 int temp;
90 int volt;
91 int avg_curr;
92 int inst_curr;
93 int percent;
94};
95
96enum abx500_chargalg_states {
97 STATE_HANDHELD_INIT,
98 STATE_HANDHELD,
99 STATE_CHG_NOT_OK_INIT,
100 STATE_CHG_NOT_OK,
101 STATE_HW_TEMP_PROTECT_INIT,
102 STATE_HW_TEMP_PROTECT,
103 STATE_NORMAL_INIT,
Lee Jonesdb43e6c2013-02-14 12:39:15 +0000104 STATE_USB_PP_PRE_CHARGE,
Arun Murthy1668f812012-02-29 21:54:25 +0530105 STATE_NORMAL,
106 STATE_WAIT_FOR_RECHARGE_INIT,
107 STATE_WAIT_FOR_RECHARGE,
108 STATE_MAINTENANCE_A_INIT,
109 STATE_MAINTENANCE_A,
110 STATE_MAINTENANCE_B_INIT,
111 STATE_MAINTENANCE_B,
112 STATE_TEMP_UNDEROVER_INIT,
113 STATE_TEMP_UNDEROVER,
114 STATE_TEMP_LOWHIGH_INIT,
115 STATE_TEMP_LOWHIGH,
116 STATE_SUSPENDED_INIT,
117 STATE_SUSPENDED,
118 STATE_OVV_PROTECT_INIT,
119 STATE_OVV_PROTECT,
120 STATE_SAFETY_TIMER_EXPIRED_INIT,
121 STATE_SAFETY_TIMER_EXPIRED,
122 STATE_BATT_REMOVED_INIT,
123 STATE_BATT_REMOVED,
124 STATE_WD_EXPIRED_INIT,
125 STATE_WD_EXPIRED,
126};
127
128static const char *states[] = {
129 "HANDHELD_INIT",
130 "HANDHELD",
131 "CHG_NOT_OK_INIT",
132 "CHG_NOT_OK",
133 "HW_TEMP_PROTECT_INIT",
134 "HW_TEMP_PROTECT",
135 "NORMAL_INIT",
Lee Jonesdb43e6c2013-02-14 12:39:15 +0000136 "USB_PP_PRE_CHARGE",
Arun Murthy1668f812012-02-29 21:54:25 +0530137 "NORMAL",
138 "WAIT_FOR_RECHARGE_INIT",
139 "WAIT_FOR_RECHARGE",
140 "MAINTENANCE_A_INIT",
141 "MAINTENANCE_A",
142 "MAINTENANCE_B_INIT",
143 "MAINTENANCE_B",
144 "TEMP_UNDEROVER_INIT",
145 "TEMP_UNDEROVER",
146 "TEMP_LOWHIGH_INIT",
147 "TEMP_LOWHIGH",
148 "SUSPENDED_INIT",
149 "SUSPENDED",
150 "OVV_PROTECT_INIT",
151 "OVV_PROTECT",
152 "SAFETY_TIMER_EXPIRED_INIT",
153 "SAFETY_TIMER_EXPIRED",
154 "BATT_REMOVED_INIT",
155 "BATT_REMOVED",
156 "WD_EXPIRED_INIT",
157 "WD_EXPIRED",
158};
159
160struct abx500_chargalg_events {
161 bool batt_unknown;
162 bool mainextchnotok;
163 bool batt_ovv;
164 bool batt_rem;
165 bool btemp_underover;
166 bool btemp_lowhigh;
167 bool main_thermal_prot;
168 bool usb_thermal_prot;
169 bool main_ovv;
170 bool vbus_ovv;
171 bool usbchargernotok;
172 bool safety_timer_expired;
173 bool maintenance_timer_expired;
174 bool ac_wd_expired;
175 bool usb_wd_expired;
176 bool ac_cv_active;
177 bool usb_cv_active;
178 bool vbus_collapsed;
179};
180
181/**
182 * struct abx500_charge_curr_maximization - Charger maximization parameters
183 * @original_iset: the non optimized/maximised charger current
184 * @current_iset: the charging current used at this moment
185 * @test_delta_i: the delta between the current we want to charge and the
186 current that is really going into the battery
187 * @condition_cnt: number of iterations needed before a new charger current
188 is set
189 * @max_current: maximum charger current
190 * @wait_cnt: to avoid too fast current step down in case of charger
191 * voltage collapse, we insert this delay between step
192 * down
193 * @level: tells in how many steps the charging current has been
194 increased
195 */
196struct abx500_charge_curr_maximization {
197 int original_iset;
198 int current_iset;
199 int test_delta_i;
200 int condition_cnt;
201 int max_current;
202 int wait_cnt;
203 u8 level;
204};
205
206enum maxim_ret {
207 MAXIM_RET_NOACTION,
208 MAXIM_RET_CHANGE,
209 MAXIM_RET_IBAT_TOO_HIGH,
210};
211
212/**
213 * struct abx500_chargalg - abx500 Charging algorithm device information
214 * @dev: pointer to the structure device
215 * @charge_status: battery operating status
216 * @eoc_cnt: counter used to determine end-of_charge
Arun Murthy1668f812012-02-29 21:54:25 +0530217 * @maintenance_chg: indicate if maintenance charge is active
218 * @t_hyst_norm temperature hysteresis when the temperature has been
219 * over or under normal limits
220 * @t_hyst_lowhigh temperature hysteresis when the temperature has been
221 * over or under the high or low limits
222 * @charge_state: current state of the charging algorithm
223 * @ccm charging current maximization parameters
224 * @chg_info: information about connected charger types
225 * @batt_data: data of the battery
226 * @susp_status: current charger suspension status
Lee Jonesb0284de2012-11-30 10:09:42 +0000227 * @bm: Platform specific battery management information
Lee Jones4d3b4aa2013-02-15 10:58:58 +0000228 * @curr_status: Current step status for over-current protection
Lee Jones330b7eb2013-02-15 10:53:57 +0000229 * @parent: pointer to the struct abx500
Arun Murthy1668f812012-02-29 21:54:25 +0530230 * @chargalg_psy: structure that holds the battery properties exposed by
231 * the charging algorithm
232 * @events: structure for information about events triggered
233 * @chargalg_wq: work queue for running the charging algorithm
234 * @chargalg_periodic_work: work to run the charging algorithm periodically
235 * @chargalg_wd_work: work to kick the charger watchdog periodically
236 * @chargalg_work: work to run the charging algorithm instantly
237 * @safety_timer: charging safety timer
238 * @maintenance_timer: maintenance charging timer
239 * @chargalg_kobject: structure of type kobject
240 */
241struct abx500_chargalg {
242 struct device *dev;
243 int charge_status;
244 int eoc_cnt;
Arun Murthy1668f812012-02-29 21:54:25 +0530245 bool maintenance_chg;
246 int t_hyst_norm;
247 int t_hyst_lowhigh;
248 enum abx500_chargalg_states charge_state;
249 struct abx500_charge_curr_maximization ccm;
250 struct abx500_chargalg_charger_info chg_info;
251 struct abx500_chargalg_battery_data batt_data;
252 struct abx500_chargalg_suspension_status susp_status;
Lee Jones330b7eb2013-02-15 10:53:57 +0000253 struct ab8500 *parent;
Lee Jones4d3b4aa2013-02-15 10:58:58 +0000254 struct abx500_chargalg_current_step_status curr_status;
Lee Jonesb0284de2012-11-30 10:09:42 +0000255 struct abx500_bm_data *bm;
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100256 struct power_supply *chargalg_psy;
Arun Murthy1668f812012-02-29 21:54:25 +0530257 struct ux500_charger *ac_chg;
258 struct ux500_charger *usb_chg;
259 struct abx500_chargalg_events events;
260 struct workqueue_struct *chargalg_wq;
261 struct delayed_work chargalg_periodic_work;
262 struct delayed_work chargalg_wd_work;
263 struct work_struct chargalg_work;
Lee Jones257107a2013-02-13 15:15:03 +0000264 struct hrtimer safety_timer;
265 struct hrtimer maintenance_timer;
Arun Murthy1668f812012-02-29 21:54:25 +0530266 struct kobject chargalg_kobject;
267};
268
Lee Jones88917162013-02-13 11:39:19 +0000269/*External charger prepare notifier*/
270BLOCKING_NOTIFIER_HEAD(charger_notifier_list);
271
Arun Murthy1668f812012-02-29 21:54:25 +0530272/* Main battery properties */
273static enum power_supply_property abx500_chargalg_props[] = {
274 POWER_SUPPLY_PROP_STATUS,
275 POWER_SUPPLY_PROP_HEALTH,
276};
277
Lee Jones4d3b4aa2013-02-15 10:58:58 +0000278struct abx500_chargalg_sysfs_entry {
279 struct attribute attr;
280 ssize_t (*show)(struct abx500_chargalg *, char *);
281 ssize_t (*store)(struct abx500_chargalg *, const char *, size_t);
282};
283
Arun Murthy1668f812012-02-29 21:54:25 +0530284/**
285 * abx500_chargalg_safety_timer_expired() - Expiration of the safety timer
Lee Jones257107a2013-02-13 15:15:03 +0000286 * @timer: pointer to the hrtimer structure
Arun Murthy1668f812012-02-29 21:54:25 +0530287 *
288 * This function gets called when the safety timer for the charger
289 * expires
290 */
Lee Jones257107a2013-02-13 15:15:03 +0000291static enum hrtimer_restart
292abx500_chargalg_safety_timer_expired(struct hrtimer *timer)
Arun Murthy1668f812012-02-29 21:54:25 +0530293{
Lee Jones257107a2013-02-13 15:15:03 +0000294 struct abx500_chargalg *di = container_of(timer, struct abx500_chargalg,
295 safety_timer);
Arun Murthy1668f812012-02-29 21:54:25 +0530296 dev_err(di->dev, "Safety timer expired\n");
297 di->events.safety_timer_expired = true;
298
299 /* Trigger execution of the algorithm instantly */
300 queue_work(di->chargalg_wq, &di->chargalg_work);
Lee Jones257107a2013-02-13 15:15:03 +0000301
302 return HRTIMER_NORESTART;
Arun Murthy1668f812012-02-29 21:54:25 +0530303}
304
305/**
306 * abx500_chargalg_maintenance_timer_expired() - Expiration of
307 * the maintenance timer
Lee Jones257107a2013-02-13 15:15:03 +0000308 * @timer: pointer to the timer structure
Arun Murthy1668f812012-02-29 21:54:25 +0530309 *
310 * This function gets called when the maintenence timer
311 * expires
312 */
Lee Jones257107a2013-02-13 15:15:03 +0000313static enum hrtimer_restart
314abx500_chargalg_maintenance_timer_expired(struct hrtimer *timer)
Arun Murthy1668f812012-02-29 21:54:25 +0530315{
316
Lee Jones257107a2013-02-13 15:15:03 +0000317 struct abx500_chargalg *di = container_of(timer, struct abx500_chargalg,
318 maintenance_timer);
319
Arun Murthy1668f812012-02-29 21:54:25 +0530320 dev_dbg(di->dev, "Maintenance timer expired\n");
321 di->events.maintenance_timer_expired = true;
322
323 /* Trigger execution of the algorithm instantly */
324 queue_work(di->chargalg_wq, &di->chargalg_work);
Lee Jones257107a2013-02-13 15:15:03 +0000325
326 return HRTIMER_NORESTART;
Arun Murthy1668f812012-02-29 21:54:25 +0530327}
328
329/**
330 * abx500_chargalg_state_to() - Change charge state
331 * @di: pointer to the abx500_chargalg structure
332 *
333 * This function gets called when a charge state change should occur
334 */
335static void abx500_chargalg_state_to(struct abx500_chargalg *di,
336 enum abx500_chargalg_states state)
337{
338 dev_dbg(di->dev,
339 "State changed: %s (From state: [%d] %s =to=> [%d] %s )\n",
340 di->charge_state == state ? "NO" : "YES",
341 di->charge_state,
342 states[di->charge_state],
343 state,
344 states[state]);
345
346 di->charge_state = state;
347}
348
Lee Jones4dcdf572013-02-14 09:24:10 +0000349static int abx500_chargalg_check_charger_enable(struct abx500_chargalg *di)
350{
351 switch (di->charge_state) {
352 case STATE_NORMAL:
353 case STATE_MAINTENANCE_A:
354 case STATE_MAINTENANCE_B:
355 break;
356 default:
357 return 0;
358 }
359
360 if (di->chg_info.charger_type & USB_CHG) {
361 return di->usb_chg->ops.check_enable(di->usb_chg,
362 di->bm->bat_type[di->bm->batt_id].normal_vol_lvl,
363 di->bm->bat_type[di->bm->batt_id].normal_cur_lvl);
364 } else if ((di->chg_info.charger_type & AC_CHG) &&
365 !(di->ac_chg->external)) {
366 return di->ac_chg->ops.check_enable(di->ac_chg,
367 di->bm->bat_type[di->bm->batt_id].normal_vol_lvl,
368 di->bm->bat_type[di->bm->batt_id].normal_cur_lvl);
369 }
370 return 0;
371}
372
Arun Murthy1668f812012-02-29 21:54:25 +0530373/**
374 * abx500_chargalg_check_charger_connection() - Check charger connection change
375 * @di: pointer to the abx500_chargalg structure
376 *
377 * This function will check if there is a change in the charger connection
378 * and change charge state accordingly. AC has precedence over USB.
379 */
380static int abx500_chargalg_check_charger_connection(struct abx500_chargalg *di)
381{
382 if (di->chg_info.conn_chg != di->chg_info.prev_conn_chg ||
383 di->susp_status.suspended_change) {
384 /*
385 * Charger state changed or suspension
386 * has changed since last update
387 */
388 if ((di->chg_info.conn_chg & AC_CHG) &&
389 !di->susp_status.ac_suspended) {
390 dev_dbg(di->dev, "Charging source is AC\n");
391 if (di->chg_info.charger_type != AC_CHG) {
392 di->chg_info.charger_type = AC_CHG;
393 abx500_chargalg_state_to(di, STATE_NORMAL_INIT);
394 }
395 } else if ((di->chg_info.conn_chg & USB_CHG) &&
396 !di->susp_status.usb_suspended) {
397 dev_dbg(di->dev, "Charging source is USB\n");
398 di->chg_info.charger_type = USB_CHG;
399 abx500_chargalg_state_to(di, STATE_NORMAL_INIT);
400 } else if (di->chg_info.conn_chg &&
401 (di->susp_status.ac_suspended ||
402 di->susp_status.usb_suspended)) {
403 dev_dbg(di->dev, "Charging is suspended\n");
404 di->chg_info.charger_type = NO_CHG;
405 abx500_chargalg_state_to(di, STATE_SUSPENDED_INIT);
406 } else {
407 dev_dbg(di->dev, "Charging source is OFF\n");
408 di->chg_info.charger_type = NO_CHG;
409 abx500_chargalg_state_to(di, STATE_HANDHELD_INIT);
410 }
411 di->chg_info.prev_conn_chg = di->chg_info.conn_chg;
412 di->susp_status.suspended_change = false;
413 }
414 return di->chg_info.conn_chg;
415}
416
417/**
Lee Jones4d3b4aa2013-02-15 10:58:58 +0000418 * abx500_chargalg_check_current_step_status() - Check charging current
419 * step status.
420 * @di: pointer to the abx500_chargalg structure
421 *
422 * This function will check if there is a change in the charging current step
423 * and change charge state accordingly.
424 */
425static void abx500_chargalg_check_current_step_status
426 (struct abx500_chargalg *di)
427{
428 if (di->curr_status.curr_step_change)
429 abx500_chargalg_state_to(di, STATE_NORMAL_INIT);
430 di->curr_status.curr_step_change = false;
431}
432
433/**
Arun Murthy1668f812012-02-29 21:54:25 +0530434 * abx500_chargalg_start_safety_timer() - Start charging safety timer
435 * @di: pointer to the abx500_chargalg structure
436 *
437 * The safety timer is used to avoid overcharging of old or bad batteries.
438 * There are different timers for AC and USB
439 */
440static void abx500_chargalg_start_safety_timer(struct abx500_chargalg *di)
441{
Lee Jones257107a2013-02-13 15:15:03 +0000442 /* Charger-dependent expiration time in hours*/
443 int timer_expiration = 0;
Arun Murthy1668f812012-02-29 21:54:25 +0530444
445 switch (di->chg_info.charger_type) {
446 case AC_CHG:
Lee Jones257107a2013-02-13 15:15:03 +0000447 timer_expiration = di->bm->main_safety_tmr_h;
Arun Murthy1668f812012-02-29 21:54:25 +0530448 break;
449
450 case USB_CHG:
Lee Jones257107a2013-02-13 15:15:03 +0000451 timer_expiration = di->bm->usb_safety_tmr_h;
Arun Murthy1668f812012-02-29 21:54:25 +0530452 break;
453
454 default:
455 dev_err(di->dev, "Unknown charger to charge from\n");
456 break;
457 }
458
459 di->events.safety_timer_expired = false;
Lee Jones257107a2013-02-13 15:15:03 +0000460 hrtimer_set_expires_range(&di->safety_timer,
461 ktime_set(timer_expiration * ONE_HOUR_IN_SECONDS, 0),
462 ktime_set(FIVE_MINUTES_IN_SECONDS, 0));
463 hrtimer_start_expires(&di->safety_timer, HRTIMER_MODE_REL);
Arun Murthy1668f812012-02-29 21:54:25 +0530464}
465
466/**
467 * abx500_chargalg_stop_safety_timer() - Stop charging safety timer
468 * @di: pointer to the abx500_chargalg structure
469 *
470 * The safety timer is stopped whenever the NORMAL state is exited
471 */
472static void abx500_chargalg_stop_safety_timer(struct abx500_chargalg *di)
473{
Lee Jones257107a2013-02-13 15:15:03 +0000474 if (hrtimer_try_to_cancel(&di->safety_timer) >= 0)
475 di->events.safety_timer_expired = false;
Arun Murthy1668f812012-02-29 21:54:25 +0530476}
477
478/**
479 * abx500_chargalg_start_maintenance_timer() - Start charging maintenance timer
480 * @di: pointer to the abx500_chargalg structure
481 * @duration: duration of ther maintenance timer in hours
482 *
483 * The maintenance timer is used to maintain the charge in the battery once
484 * the battery is considered full. These timers are chosen to match the
485 * discharge curve of the battery
486 */
487static void abx500_chargalg_start_maintenance_timer(struct abx500_chargalg *di,
488 int duration)
489{
Lee Jones257107a2013-02-13 15:15:03 +0000490 hrtimer_set_expires_range(&di->maintenance_timer,
491 ktime_set(duration * ONE_HOUR_IN_SECONDS, 0),
492 ktime_set(FIVE_MINUTES_IN_SECONDS, 0));
Arun Murthy1668f812012-02-29 21:54:25 +0530493 di->events.maintenance_timer_expired = false;
Lee Jones257107a2013-02-13 15:15:03 +0000494 hrtimer_start_expires(&di->maintenance_timer, HRTIMER_MODE_REL);
Arun Murthy1668f812012-02-29 21:54:25 +0530495}
496
497/**
498 * abx500_chargalg_stop_maintenance_timer() - Stop maintenance timer
499 * @di: pointer to the abx500_chargalg structure
500 *
501 * The maintenance timer is stopped whenever maintenance ends or when another
502 * state is entered
503 */
504static void abx500_chargalg_stop_maintenance_timer(struct abx500_chargalg *di)
505{
Lee Jones257107a2013-02-13 15:15:03 +0000506 if (hrtimer_try_to_cancel(&di->maintenance_timer) >= 0)
507 di->events.maintenance_timer_expired = false;
Arun Murthy1668f812012-02-29 21:54:25 +0530508}
509
510/**
511 * abx500_chargalg_kick_watchdog() - Kick charger watchdog
512 * @di: pointer to the abx500_chargalg structure
513 *
514 * The charger watchdog have to be kicked periodically whenever the charger is
515 * on, else the ABB will reset the system
516 */
517static int abx500_chargalg_kick_watchdog(struct abx500_chargalg *di)
518{
519 /* Check if charger exists and kick watchdog if charging */
520 if (di->ac_chg && di->ac_chg->ops.kick_wd &&
Loic Pallardye07a5642012-05-10 15:33:56 +0200521 di->chg_info.online_chg & AC_CHG) {
522 /*
523 * If AB charger watchdog expired, pm2xxx charging
524 * gets disabled. To be safe, kick both AB charger watchdog
525 * and pm2xxx watchdog.
526 */
527 if (di->ac_chg->external &&
528 di->usb_chg && di->usb_chg->ops.kick_wd)
529 di->usb_chg->ops.kick_wd(di->usb_chg);
530
Arun Murthy1668f812012-02-29 21:54:25 +0530531 return di->ac_chg->ops.kick_wd(di->ac_chg);
Loic Pallardye07a5642012-05-10 15:33:56 +0200532 }
Arun Murthy1668f812012-02-29 21:54:25 +0530533 else if (di->usb_chg && di->usb_chg->ops.kick_wd &&
534 di->chg_info.online_chg & USB_CHG)
535 return di->usb_chg->ops.kick_wd(di->usb_chg);
536
537 return -ENXIO;
538}
539
540/**
541 * abx500_chargalg_ac_en() - Turn on/off the AC charger
542 * @di: pointer to the abx500_chargalg structure
543 * @enable: charger on/off
544 * @vset: requested charger output voltage
545 * @iset: requested charger output current
546 *
547 * The AC charger will be turned on/off with the requested charge voltage and
548 * current
549 */
550static int abx500_chargalg_ac_en(struct abx500_chargalg *di, int enable,
551 int vset, int iset)
552{
Lee Jones88917162013-02-13 11:39:19 +0000553 static int abx500_chargalg_ex_ac_enable_toggle;
554
Arun Murthy1668f812012-02-29 21:54:25 +0530555 if (!di->ac_chg || !di->ac_chg->ops.enable)
556 return -ENXIO;
557
558 /* Select maximum of what both the charger and the battery supports */
559 if (di->ac_chg->max_out_volt)
560 vset = min(vset, di->ac_chg->max_out_volt);
561 if (di->ac_chg->max_out_curr)
562 iset = min(iset, di->ac_chg->max_out_curr);
563
564 di->chg_info.ac_iset = iset;
565 di->chg_info.ac_vset = vset;
566
Lee Jones88917162013-02-13 11:39:19 +0000567 /* Enable external charger */
568 if (enable && di->ac_chg->external &&
569 !abx500_chargalg_ex_ac_enable_toggle) {
570 blocking_notifier_call_chain(&charger_notifier_list,
571 0, di->dev);
572 abx500_chargalg_ex_ac_enable_toggle++;
573 }
574
Arun Murthy1668f812012-02-29 21:54:25 +0530575 return di->ac_chg->ops.enable(di->ac_chg, enable, vset, iset);
576}
577
578/**
579 * abx500_chargalg_usb_en() - Turn on/off the USB charger
580 * @di: pointer to the abx500_chargalg structure
581 * @enable: charger on/off
582 * @vset: requested charger output voltage
583 * @iset: requested charger output current
584 *
585 * The USB charger will be turned on/off with the requested charge voltage and
586 * current
587 */
588static int abx500_chargalg_usb_en(struct abx500_chargalg *di, int enable,
589 int vset, int iset)
590{
591 if (!di->usb_chg || !di->usb_chg->ops.enable)
592 return -ENXIO;
593
594 /* Select maximum of what both the charger and the battery supports */
595 if (di->usb_chg->max_out_volt)
596 vset = min(vset, di->usb_chg->max_out_volt);
597 if (di->usb_chg->max_out_curr)
598 iset = min(iset, di->usb_chg->max_out_curr);
599
600 di->chg_info.usb_iset = iset;
601 di->chg_info.usb_vset = vset;
602
603 return di->usb_chg->ops.enable(di->usb_chg, enable, vset, iset);
604}
605
Lee Jonesdb43e6c2013-02-14 12:39:15 +0000606 /**
607 * ab8540_chargalg_usb_pp_en() - Enable/ disable USB power path
608 * @di: pointer to the abx500_chargalg structure
609 * @enable: power path enable/disable
610 *
611 * The USB power path will be enable/ disable
612 */
613static int ab8540_chargalg_usb_pp_en(struct abx500_chargalg *di, bool enable)
614{
615 if (!di->usb_chg || !di->usb_chg->ops.pp_enable)
616 return -ENXIO;
617
618 return di->usb_chg->ops.pp_enable(di->usb_chg, enable);
619}
620
621/**
622 * ab8540_chargalg_usb_pre_chg_en() - Enable/ disable USB pre-charge
623 * @di: pointer to the abx500_chargalg structure
624 * @enable: USB pre-charge enable/disable
625 *
626 * The USB USB pre-charge will be enable/ disable
627 */
628static int ab8540_chargalg_usb_pre_chg_en(struct abx500_chargalg *di,
629 bool enable)
630{
631 if (!di->usb_chg || !di->usb_chg->ops.pre_chg_enable)
632 return -ENXIO;
633
634 return di->usb_chg->ops.pre_chg_enable(di->usb_chg, enable);
635}
636
Arun Murthy1668f812012-02-29 21:54:25 +0530637/**
638 * abx500_chargalg_update_chg_curr() - Update charger current
639 * @di: pointer to the abx500_chargalg structure
640 * @iset: requested charger output current
641 *
642 * The charger output current will be updated for the charger
643 * that is currently in use
644 */
645static int abx500_chargalg_update_chg_curr(struct abx500_chargalg *di,
646 int iset)
647{
648 /* Check if charger exists and update current if charging */
649 if (di->ac_chg && di->ac_chg->ops.update_curr &&
650 di->chg_info.charger_type & AC_CHG) {
651 /*
652 * Select maximum of what both the charger
653 * and the battery supports
654 */
655 if (di->ac_chg->max_out_curr)
656 iset = min(iset, di->ac_chg->max_out_curr);
657
658 di->chg_info.ac_iset = iset;
659
660 return di->ac_chg->ops.update_curr(di->ac_chg, iset);
661 } else if (di->usb_chg && di->usb_chg->ops.update_curr &&
662 di->chg_info.charger_type & USB_CHG) {
663 /*
664 * Select maximum of what both the charger
665 * and the battery supports
666 */
667 if (di->usb_chg->max_out_curr)
668 iset = min(iset, di->usb_chg->max_out_curr);
669
670 di->chg_info.usb_iset = iset;
671
672 return di->usb_chg->ops.update_curr(di->usb_chg, iset);
673 }
674
675 return -ENXIO;
676}
677
678/**
679 * abx500_chargalg_stop_charging() - Stop charging
680 * @di: pointer to the abx500_chargalg structure
681 *
682 * This function is called from any state where charging should be stopped.
683 * All charging is disabled and all status parameters and timers are changed
684 * accordingly
685 */
686static void abx500_chargalg_stop_charging(struct abx500_chargalg *di)
687{
688 abx500_chargalg_ac_en(di, false, 0, 0);
689 abx500_chargalg_usb_en(di, false, 0, 0);
690 abx500_chargalg_stop_safety_timer(di);
691 abx500_chargalg_stop_maintenance_timer(di);
692 di->charge_status = POWER_SUPPLY_STATUS_NOT_CHARGING;
693 di->maintenance_chg = false;
694 cancel_delayed_work(&di->chargalg_wd_work);
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100695 power_supply_changed(di->chargalg_psy);
Arun Murthy1668f812012-02-29 21:54:25 +0530696}
697
698/**
699 * abx500_chargalg_hold_charging() - Pauses charging
700 * @di: pointer to the abx500_chargalg structure
701 *
702 * This function is called in the case where maintenance charging has been
703 * disabled and instead a battery voltage mode is entered to check when the
704 * battery voltage has reached a certain recharge voltage
705 */
706static void abx500_chargalg_hold_charging(struct abx500_chargalg *di)
707{
708 abx500_chargalg_ac_en(di, false, 0, 0);
709 abx500_chargalg_usb_en(di, false, 0, 0);
710 abx500_chargalg_stop_safety_timer(di);
711 abx500_chargalg_stop_maintenance_timer(di);
712 di->charge_status = POWER_SUPPLY_STATUS_CHARGING;
713 di->maintenance_chg = false;
714 cancel_delayed_work(&di->chargalg_wd_work);
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100715 power_supply_changed(di->chargalg_psy);
Arun Murthy1668f812012-02-29 21:54:25 +0530716}
717
718/**
719 * abx500_chargalg_start_charging() - Start the charger
720 * @di: pointer to the abx500_chargalg structure
721 * @vset: requested charger output voltage
722 * @iset: requested charger output current
723 *
724 * A charger will be enabled depending on the requested charger type that was
725 * detected previously.
726 */
727static void abx500_chargalg_start_charging(struct abx500_chargalg *di,
728 int vset, int iset)
729{
730 switch (di->chg_info.charger_type) {
731 case AC_CHG:
732 dev_dbg(di->dev,
733 "AC parameters: Vset %d, Ich %d\n", vset, iset);
734 abx500_chargalg_usb_en(di, false, 0, 0);
735 abx500_chargalg_ac_en(di, true, vset, iset);
736 break;
737
738 case USB_CHG:
739 dev_dbg(di->dev,
740 "USB parameters: Vset %d, Ich %d\n", vset, iset);
741 abx500_chargalg_ac_en(di, false, 0, 0);
742 abx500_chargalg_usb_en(di, true, vset, iset);
743 break;
744
745 default:
746 dev_err(di->dev, "Unknown charger to charge from\n");
747 break;
748 }
749}
750
751/**
752 * abx500_chargalg_check_temp() - Check battery temperature ranges
753 * @di: pointer to the abx500_chargalg structure
754 *
755 * The battery temperature is checked against the predefined limits and the
756 * charge state is changed accordingly
757 */
758static void abx500_chargalg_check_temp(struct abx500_chargalg *di)
759{
Lee Jonesb0284de2012-11-30 10:09:42 +0000760 if (di->batt_data.temp > (di->bm->temp_low + di->t_hyst_norm) &&
761 di->batt_data.temp < (di->bm->temp_high - di->t_hyst_norm)) {
Arun Murthy1668f812012-02-29 21:54:25 +0530762 /* Temp OK! */
763 di->events.btemp_underover = false;
764 di->events.btemp_lowhigh = false;
765 di->t_hyst_norm = 0;
766 di->t_hyst_lowhigh = 0;
767 } else {
Lee Jonesb0284de2012-11-30 10:09:42 +0000768 if (((di->batt_data.temp >= di->bm->temp_high) &&
Arun Murthy1668f812012-02-29 21:54:25 +0530769 (di->batt_data.temp <
Lee Jonesb0284de2012-11-30 10:09:42 +0000770 (di->bm->temp_over - di->t_hyst_lowhigh))) ||
Arun Murthy1668f812012-02-29 21:54:25 +0530771 ((di->batt_data.temp >
Lee Jonesb0284de2012-11-30 10:09:42 +0000772 (di->bm->temp_under + di->t_hyst_lowhigh)) &&
773 (di->batt_data.temp <= di->bm->temp_low))) {
Arun Murthy1668f812012-02-29 21:54:25 +0530774 /* TEMP minor!!!!! */
775 di->events.btemp_underover = false;
776 di->events.btemp_lowhigh = true;
Lee Jonesb0284de2012-11-30 10:09:42 +0000777 di->t_hyst_norm = di->bm->temp_hysteresis;
Arun Murthy1668f812012-02-29 21:54:25 +0530778 di->t_hyst_lowhigh = 0;
Lee Jonesb0284de2012-11-30 10:09:42 +0000779 } else if (di->batt_data.temp <= di->bm->temp_under ||
780 di->batt_data.temp >= di->bm->temp_over) {
Arun Murthy1668f812012-02-29 21:54:25 +0530781 /* TEMP major!!!!! */
782 di->events.btemp_underover = true;
783 di->events.btemp_lowhigh = false;
784 di->t_hyst_norm = 0;
Lee Jonesb0284de2012-11-30 10:09:42 +0000785 di->t_hyst_lowhigh = di->bm->temp_hysteresis;
Arun Murthy1668f812012-02-29 21:54:25 +0530786 } else {
787 /* Within hysteresis */
788 dev_dbg(di->dev, "Within hysteresis limit temp: %d "
789 "hyst_lowhigh %d, hyst normal %d\n",
790 di->batt_data.temp, di->t_hyst_lowhigh,
791 di->t_hyst_norm);
792 }
793 }
794}
795
796/**
797 * abx500_chargalg_check_charger_voltage() - Check charger voltage
798 * @di: pointer to the abx500_chargalg structure
799 *
800 * Charger voltage is checked against maximum limit
801 */
802static void abx500_chargalg_check_charger_voltage(struct abx500_chargalg *di)
803{
Lee Jonesb0284de2012-11-30 10:09:42 +0000804 if (di->chg_info.usb_volt > di->bm->chg_params->usb_volt_max)
Arun Murthy1668f812012-02-29 21:54:25 +0530805 di->chg_info.usb_chg_ok = false;
806 else
807 di->chg_info.usb_chg_ok = true;
808
Lee Jonesb0284de2012-11-30 10:09:42 +0000809 if (di->chg_info.ac_volt > di->bm->chg_params->ac_volt_max)
Arun Murthy1668f812012-02-29 21:54:25 +0530810 di->chg_info.ac_chg_ok = false;
811 else
812 di->chg_info.ac_chg_ok = true;
813
814}
815
816/**
817 * abx500_chargalg_end_of_charge() - Check if end-of-charge criteria is fulfilled
818 * @di: pointer to the abx500_chargalg structure
819 *
820 * End-of-charge criteria is fulfilled when the battery voltage is above a
821 * certain limit and the battery current is below a certain limit for a
822 * predefined number of consecutive seconds. If true, the battery is full
823 */
824static void abx500_chargalg_end_of_charge(struct abx500_chargalg *di)
825{
826 if (di->charge_status == POWER_SUPPLY_STATUS_CHARGING &&
827 di->charge_state == STATE_NORMAL &&
828 !di->maintenance_chg && (di->batt_data.volt >=
Lee Jonesb0284de2012-11-30 10:09:42 +0000829 di->bm->bat_type[di->bm->batt_id].termination_vol ||
Arun Murthy1668f812012-02-29 21:54:25 +0530830 di->events.usb_cv_active || di->events.ac_cv_active) &&
831 di->batt_data.avg_curr <
Lee Jonesb0284de2012-11-30 10:09:42 +0000832 di->bm->bat_type[di->bm->batt_id].termination_curr &&
Arun Murthy1668f812012-02-29 21:54:25 +0530833 di->batt_data.avg_curr > 0) {
834 if (++di->eoc_cnt >= EOC_COND_CNT) {
835 di->eoc_cnt = 0;
Lee Jonesdb43e6c2013-02-14 12:39:15 +0000836 if ((di->chg_info.charger_type & USB_CHG) &&
837 (di->usb_chg->power_path))
838 ab8540_chargalg_usb_pp_en(di, true);
Arun Murthy1668f812012-02-29 21:54:25 +0530839 di->charge_status = POWER_SUPPLY_STATUS_FULL;
840 di->maintenance_chg = true;
841 dev_dbg(di->dev, "EOC reached!\n");
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100842 power_supply_changed(di->chargalg_psy);
Arun Murthy1668f812012-02-29 21:54:25 +0530843 } else {
844 dev_dbg(di->dev,
845 " EOC limit reached for the %d"
846 " time, out of %d before EOC\n",
847 di->eoc_cnt,
848 EOC_COND_CNT);
849 }
850 } else {
851 di->eoc_cnt = 0;
852 }
853}
854
855static void init_maxim_chg_curr(struct abx500_chargalg *di)
856{
857 di->ccm.original_iset =
Lee Jonesb0284de2012-11-30 10:09:42 +0000858 di->bm->bat_type[di->bm->batt_id].normal_cur_lvl;
Arun Murthy1668f812012-02-29 21:54:25 +0530859 di->ccm.current_iset =
Lee Jonesb0284de2012-11-30 10:09:42 +0000860 di->bm->bat_type[di->bm->batt_id].normal_cur_lvl;
861 di->ccm.test_delta_i = di->bm->maxi->charger_curr_step;
862 di->ccm.max_current = di->bm->maxi->chg_curr;
863 di->ccm.condition_cnt = di->bm->maxi->wait_cycles;
Arun Murthy1668f812012-02-29 21:54:25 +0530864 di->ccm.level = 0;
865}
866
867/**
868 * abx500_chargalg_chg_curr_maxim - increases the charger current to
869 * compensate for the system load
870 * @di pointer to the abx500_chargalg structure
871 *
872 * This maximization function is used to raise the charger current to get the
873 * battery current as close to the optimal value as possible. The battery
874 * current during charging is affected by the system load
875 */
876static enum maxim_ret abx500_chargalg_chg_curr_maxim(struct abx500_chargalg *di)
877{
878 int delta_i;
879
Lee Jonesb0284de2012-11-30 10:09:42 +0000880 if (!di->bm->maxi->ena_maxi)
Arun Murthy1668f812012-02-29 21:54:25 +0530881 return MAXIM_RET_NOACTION;
882
883 delta_i = di->ccm.original_iset - di->batt_data.inst_curr;
884
885 if (di->events.vbus_collapsed) {
886 dev_dbg(di->dev, "Charger voltage has collapsed %d\n",
887 di->ccm.wait_cnt);
888 if (di->ccm.wait_cnt == 0) {
889 dev_dbg(di->dev, "lowering current\n");
890 di->ccm.wait_cnt++;
Lee Jonesb0284de2012-11-30 10:09:42 +0000891 di->ccm.condition_cnt = di->bm->maxi->wait_cycles;
Arun Murthy1668f812012-02-29 21:54:25 +0530892 di->ccm.max_current =
893 di->ccm.current_iset - di->ccm.test_delta_i;
894 di->ccm.current_iset = di->ccm.max_current;
895 di->ccm.level--;
896 return MAXIM_RET_CHANGE;
897 } else {
898 dev_dbg(di->dev, "waiting\n");
899 /* Let's go in here twice before lowering curr again */
900 di->ccm.wait_cnt = (di->ccm.wait_cnt + 1) % 3;
901 return MAXIM_RET_NOACTION;
902 }
903 }
904
905 di->ccm.wait_cnt = 0;
906
907 if ((di->batt_data.inst_curr > di->ccm.original_iset)) {
908 dev_dbg(di->dev, " Maximization Ibat (%dmA) too high"
909 " (limit %dmA) (current iset: %dmA)!\n",
910 di->batt_data.inst_curr, di->ccm.original_iset,
911 di->ccm.current_iset);
912
913 if (di->ccm.current_iset == di->ccm.original_iset)
914 return MAXIM_RET_NOACTION;
915
Lee Jonesb0284de2012-11-30 10:09:42 +0000916 di->ccm.condition_cnt = di->bm->maxi->wait_cycles;
Arun Murthy1668f812012-02-29 21:54:25 +0530917 di->ccm.current_iset = di->ccm.original_iset;
918 di->ccm.level = 0;
919
920 return MAXIM_RET_IBAT_TOO_HIGH;
921 }
922
923 if (delta_i > di->ccm.test_delta_i &&
924 (di->ccm.current_iset + di->ccm.test_delta_i) <
925 di->ccm.max_current) {
926 if (di->ccm.condition_cnt-- == 0) {
927 /* Increse the iset with cco.test_delta_i */
Lee Jonesb0284de2012-11-30 10:09:42 +0000928 di->ccm.condition_cnt = di->bm->maxi->wait_cycles;
Arun Murthy1668f812012-02-29 21:54:25 +0530929 di->ccm.current_iset += di->ccm.test_delta_i;
930 di->ccm.level++;
931 dev_dbg(di->dev, " Maximization needed, increase"
932 " with %d mA to %dmA (Optimal ibat: %d)"
933 " Level %d\n",
934 di->ccm.test_delta_i,
935 di->ccm.current_iset,
936 di->ccm.original_iset,
937 di->ccm.level);
938 return MAXIM_RET_CHANGE;
939 } else {
940 return MAXIM_RET_NOACTION;
941 }
942 } else {
Lee Jonesb0284de2012-11-30 10:09:42 +0000943 di->ccm.condition_cnt = di->bm->maxi->wait_cycles;
Arun Murthy1668f812012-02-29 21:54:25 +0530944 return MAXIM_RET_NOACTION;
945 }
946}
947
948static void handle_maxim_chg_curr(struct abx500_chargalg *di)
949{
950 enum maxim_ret ret;
951 int result;
952
953 ret = abx500_chargalg_chg_curr_maxim(di);
954 switch (ret) {
955 case MAXIM_RET_CHANGE:
956 result = abx500_chargalg_update_chg_curr(di,
957 di->ccm.current_iset);
958 if (result)
959 dev_err(di->dev, "failed to set chg curr\n");
960 break;
961 case MAXIM_RET_IBAT_TOO_HIGH:
962 result = abx500_chargalg_update_chg_curr(di,
Lee Jonesb0284de2012-11-30 10:09:42 +0000963 di->bm->bat_type[di->bm->batt_id].normal_cur_lvl);
Arun Murthy1668f812012-02-29 21:54:25 +0530964 if (result)
965 dev_err(di->dev, "failed to set chg curr\n");
966 break;
967
968 case MAXIM_RET_NOACTION:
969 default:
970 /* Do nothing..*/
971 break;
972 }
973}
974
975static int abx500_chargalg_get_ext_psy_data(struct device *dev, void *data)
976{
977 struct power_supply *psy;
978 struct power_supply *ext;
979 struct abx500_chargalg *di;
980 union power_supply_propval ret;
981 int i, j;
982 bool psy_found = false;
Marcus Cooperea402402013-01-11 13:12:54 +0000983 bool capacity_updated = false;
Arun Murthy1668f812012-02-29 21:54:25 +0530984
985 psy = (struct power_supply *)data;
986 ext = dev_get_drvdata(dev);
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100987 di = power_supply_get_drvdata(psy);
Arun Murthy1668f812012-02-29 21:54:25 +0530988 /* For all psy where the driver name appears in any supplied_to */
989 for (i = 0; i < ext->num_supplicants; i++) {
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100990 if (!strcmp(ext->supplied_to[i], psy->desc->name))
Arun Murthy1668f812012-02-29 21:54:25 +0530991 psy_found = true;
992 }
993 if (!psy_found)
994 return 0;
995
Marcus Cooperea402402013-01-11 13:12:54 +0000996 /*
997 * If external is not registering 'POWER_SUPPLY_PROP_CAPACITY' to its
998 * property because of handling that sysfs entry on its own, this is
999 * the place to get the battery capacity.
1000 */
Krzysztof Kozlowski15077fc2015-03-12 08:44:06 +01001001 if (!power_supply_get_property(ext, POWER_SUPPLY_PROP_CAPACITY, &ret)) {
Marcus Cooperea402402013-01-11 13:12:54 +00001002 di->batt_data.percent = ret.intval;
1003 capacity_updated = true;
1004 }
1005
Arun Murthy1668f812012-02-29 21:54:25 +05301006 /* Go through all properties for the psy */
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +01001007 for (j = 0; j < ext->desc->num_properties; j++) {
Arun Murthy1668f812012-02-29 21:54:25 +05301008 enum power_supply_property prop;
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +01001009 prop = ext->desc->properties[j];
Arun Murthy1668f812012-02-29 21:54:25 +05301010
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +01001011 /*
1012 * Initialize chargers if not already done.
1013 * The ab8500_charger*/
Arun Murthy1668f812012-02-29 21:54:25 +05301014 if (!di->ac_chg &&
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +01001015 ext->desc->type == POWER_SUPPLY_TYPE_MAINS)
Arun Murthy1668f812012-02-29 21:54:25 +05301016 di->ac_chg = psy_to_ux500_charger(ext);
1017 else if (!di->usb_chg &&
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +01001018 ext->desc->type == POWER_SUPPLY_TYPE_USB)
Arun Murthy1668f812012-02-29 21:54:25 +05301019 di->usb_chg = psy_to_ux500_charger(ext);
1020
Krzysztof Kozlowski15077fc2015-03-12 08:44:06 +01001021 if (power_supply_get_property(ext, prop, &ret))
Arun Murthy1668f812012-02-29 21:54:25 +05301022 continue;
1023 switch (prop) {
1024 case POWER_SUPPLY_PROP_PRESENT:
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +01001025 switch (ext->desc->type) {
Arun Murthy1668f812012-02-29 21:54:25 +05301026 case POWER_SUPPLY_TYPE_BATTERY:
1027 /* Battery present */
1028 if (ret.intval)
1029 di->events.batt_rem = false;
1030 /* Battery removed */
1031 else
1032 di->events.batt_rem = true;
1033 break;
1034 case POWER_SUPPLY_TYPE_MAINS:
1035 /* AC disconnected */
1036 if (!ret.intval &&
1037 (di->chg_info.conn_chg & AC_CHG)) {
1038 di->chg_info.prev_conn_chg =
1039 di->chg_info.conn_chg;
1040 di->chg_info.conn_chg &= ~AC_CHG;
1041 }
1042 /* AC connected */
1043 else if (ret.intval &&
1044 !(di->chg_info.conn_chg & AC_CHG)) {
1045 di->chg_info.prev_conn_chg =
1046 di->chg_info.conn_chg;
1047 di->chg_info.conn_chg |= AC_CHG;
1048 }
1049 break;
1050 case POWER_SUPPLY_TYPE_USB:
1051 /* USB disconnected */
1052 if (!ret.intval &&
1053 (di->chg_info.conn_chg & USB_CHG)) {
1054 di->chg_info.prev_conn_chg =
1055 di->chg_info.conn_chg;
1056 di->chg_info.conn_chg &= ~USB_CHG;
1057 }
1058 /* USB connected */
1059 else if (ret.intval &&
1060 !(di->chg_info.conn_chg & USB_CHG)) {
1061 di->chg_info.prev_conn_chg =
1062 di->chg_info.conn_chg;
1063 di->chg_info.conn_chg |= USB_CHG;
1064 }
1065 break;
1066 default:
1067 break;
1068 }
1069 break;
1070
1071 case POWER_SUPPLY_PROP_ONLINE:
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +01001072 switch (ext->desc->type) {
Arun Murthy1668f812012-02-29 21:54:25 +05301073 case POWER_SUPPLY_TYPE_BATTERY:
1074 break;
1075 case POWER_SUPPLY_TYPE_MAINS:
1076 /* AC offline */
1077 if (!ret.intval &&
1078 (di->chg_info.online_chg & AC_CHG)) {
1079 di->chg_info.prev_online_chg =
1080 di->chg_info.online_chg;
1081 di->chg_info.online_chg &= ~AC_CHG;
1082 }
1083 /* AC online */
1084 else if (ret.intval &&
1085 !(di->chg_info.online_chg & AC_CHG)) {
1086 di->chg_info.prev_online_chg =
1087 di->chg_info.online_chg;
1088 di->chg_info.online_chg |= AC_CHG;
1089 queue_delayed_work(di->chargalg_wq,
1090 &di->chargalg_wd_work, 0);
1091 }
1092 break;
1093 case POWER_SUPPLY_TYPE_USB:
1094 /* USB offline */
1095 if (!ret.intval &&
1096 (di->chg_info.online_chg & USB_CHG)) {
1097 di->chg_info.prev_online_chg =
1098 di->chg_info.online_chg;
1099 di->chg_info.online_chg &= ~USB_CHG;
1100 }
1101 /* USB online */
1102 else if (ret.intval &&
1103 !(di->chg_info.online_chg & USB_CHG)) {
1104 di->chg_info.prev_online_chg =
1105 di->chg_info.online_chg;
1106 di->chg_info.online_chg |= USB_CHG;
1107 queue_delayed_work(di->chargalg_wq,
1108 &di->chargalg_wd_work, 0);
1109 }
1110 break;
1111 default:
1112 break;
1113 }
1114 break;
1115
1116 case POWER_SUPPLY_PROP_HEALTH:
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +01001117 switch (ext->desc->type) {
Arun Murthy1668f812012-02-29 21:54:25 +05301118 case POWER_SUPPLY_TYPE_BATTERY:
1119 break;
1120 case POWER_SUPPLY_TYPE_MAINS:
1121 switch (ret.intval) {
1122 case POWER_SUPPLY_HEALTH_UNSPEC_FAILURE:
1123 di->events.mainextchnotok = true;
1124 di->events.main_thermal_prot = false;
1125 di->events.main_ovv = false;
1126 di->events.ac_wd_expired = false;
1127 break;
1128 case POWER_SUPPLY_HEALTH_DEAD:
1129 di->events.ac_wd_expired = true;
1130 di->events.mainextchnotok = false;
1131 di->events.main_ovv = false;
1132 di->events.main_thermal_prot = false;
1133 break;
1134 case POWER_SUPPLY_HEALTH_COLD:
1135 case POWER_SUPPLY_HEALTH_OVERHEAT:
1136 di->events.main_thermal_prot = true;
1137 di->events.mainextchnotok = false;
1138 di->events.main_ovv = false;
1139 di->events.ac_wd_expired = false;
1140 break;
1141 case POWER_SUPPLY_HEALTH_OVERVOLTAGE:
1142 di->events.main_ovv = true;
1143 di->events.mainextchnotok = false;
1144 di->events.main_thermal_prot = false;
1145 di->events.ac_wd_expired = false;
1146 break;
1147 case POWER_SUPPLY_HEALTH_GOOD:
1148 di->events.main_thermal_prot = false;
1149 di->events.mainextchnotok = false;
1150 di->events.main_ovv = false;
1151 di->events.ac_wd_expired = false;
1152 break;
1153 default:
1154 break;
1155 }
1156 break;
1157
1158 case POWER_SUPPLY_TYPE_USB:
1159 switch (ret.intval) {
1160 case POWER_SUPPLY_HEALTH_UNSPEC_FAILURE:
1161 di->events.usbchargernotok = true;
1162 di->events.usb_thermal_prot = false;
1163 di->events.vbus_ovv = false;
1164 di->events.usb_wd_expired = false;
1165 break;
1166 case POWER_SUPPLY_HEALTH_DEAD:
1167 di->events.usb_wd_expired = true;
1168 di->events.usbchargernotok = false;
1169 di->events.usb_thermal_prot = false;
1170 di->events.vbus_ovv = false;
1171 break;
1172 case POWER_SUPPLY_HEALTH_COLD:
1173 case POWER_SUPPLY_HEALTH_OVERHEAT:
1174 di->events.usb_thermal_prot = true;
1175 di->events.usbchargernotok = false;
1176 di->events.vbus_ovv = false;
1177 di->events.usb_wd_expired = false;
1178 break;
1179 case POWER_SUPPLY_HEALTH_OVERVOLTAGE:
1180 di->events.vbus_ovv = true;
1181 di->events.usbchargernotok = false;
1182 di->events.usb_thermal_prot = false;
1183 di->events.usb_wd_expired = false;
1184 break;
1185 case POWER_SUPPLY_HEALTH_GOOD:
1186 di->events.usbchargernotok = false;
1187 di->events.usb_thermal_prot = false;
1188 di->events.vbus_ovv = false;
1189 di->events.usb_wd_expired = false;
1190 break;
1191 default:
1192 break;
1193 }
1194 default:
1195 break;
1196 }
1197 break;
1198
1199 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +01001200 switch (ext->desc->type) {
Arun Murthy1668f812012-02-29 21:54:25 +05301201 case POWER_SUPPLY_TYPE_BATTERY:
1202 di->batt_data.volt = ret.intval / 1000;
1203 break;
1204 case POWER_SUPPLY_TYPE_MAINS:
1205 di->chg_info.ac_volt = ret.intval / 1000;
1206 break;
1207 case POWER_SUPPLY_TYPE_USB:
1208 di->chg_info.usb_volt = ret.intval / 1000;
1209 break;
1210 default:
1211 break;
1212 }
1213 break;
1214
1215 case POWER_SUPPLY_PROP_VOLTAGE_AVG:
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +01001216 switch (ext->desc->type) {
Arun Murthy1668f812012-02-29 21:54:25 +05301217 case POWER_SUPPLY_TYPE_MAINS:
1218 /* AVG is used to indicate when we are
1219 * in CV mode */
1220 if (ret.intval)
1221 di->events.ac_cv_active = true;
1222 else
1223 di->events.ac_cv_active = false;
1224
1225 break;
1226 case POWER_SUPPLY_TYPE_USB:
1227 /* AVG is used to indicate when we are
1228 * in CV mode */
1229 if (ret.intval)
1230 di->events.usb_cv_active = true;
1231 else
1232 di->events.usb_cv_active = false;
1233
1234 break;
1235 default:
1236 break;
1237 }
1238 break;
1239
1240 case POWER_SUPPLY_PROP_TECHNOLOGY:
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +01001241 switch (ext->desc->type) {
Arun Murthy1668f812012-02-29 21:54:25 +05301242 case POWER_SUPPLY_TYPE_BATTERY:
1243 if (ret.intval)
1244 di->events.batt_unknown = false;
1245 else
1246 di->events.batt_unknown = true;
1247
1248 break;
1249 default:
1250 break;
1251 }
1252 break;
1253
1254 case POWER_SUPPLY_PROP_TEMP:
1255 di->batt_data.temp = ret.intval / 10;
1256 break;
1257
1258 case POWER_SUPPLY_PROP_CURRENT_NOW:
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +01001259 switch (ext->desc->type) {
Arun Murthy1668f812012-02-29 21:54:25 +05301260 case POWER_SUPPLY_TYPE_MAINS:
1261 di->chg_info.ac_curr =
1262 ret.intval / 1000;
1263 break;
1264 case POWER_SUPPLY_TYPE_USB:
1265 di->chg_info.usb_curr =
1266 ret.intval / 1000;
1267 break;
1268 case POWER_SUPPLY_TYPE_BATTERY:
1269 di->batt_data.inst_curr = ret.intval / 1000;
1270 break;
1271 default:
1272 break;
1273 }
1274 break;
1275
1276 case POWER_SUPPLY_PROP_CURRENT_AVG:
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +01001277 switch (ext->desc->type) {
Arun Murthy1668f812012-02-29 21:54:25 +05301278 case POWER_SUPPLY_TYPE_BATTERY:
1279 di->batt_data.avg_curr = ret.intval / 1000;
1280 break;
1281 case POWER_SUPPLY_TYPE_USB:
1282 if (ret.intval)
1283 di->events.vbus_collapsed = true;
1284 else
1285 di->events.vbus_collapsed = false;
1286 break;
1287 default:
1288 break;
1289 }
1290 break;
1291 case POWER_SUPPLY_PROP_CAPACITY:
Marcus Cooperea402402013-01-11 13:12:54 +00001292 if (!capacity_updated)
1293 di->batt_data.percent = ret.intval;
Arun Murthy1668f812012-02-29 21:54:25 +05301294 break;
1295 default:
1296 break;
1297 }
1298 }
1299 return 0;
1300}
1301
1302/**
1303 * abx500_chargalg_external_power_changed() - callback for power supply changes
1304 * @psy: pointer to the structure power_supply
1305 *
1306 * This function is the entry point of the pointer external_power_changed
1307 * of the structure power_supply.
1308 * This function gets executed when there is a change in any external power
1309 * supply that this driver needs to be notified of.
1310 */
1311static void abx500_chargalg_external_power_changed(struct power_supply *psy)
1312{
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +01001313 struct abx500_chargalg *di = power_supply_get_drvdata(psy);
Arun Murthy1668f812012-02-29 21:54:25 +05301314
1315 /*
1316 * Trigger execution of the algorithm instantly and read
1317 * all power_supply properties there instead
1318 */
1319 queue_work(di->chargalg_wq, &di->chargalg_work);
1320}
1321
1322/**
1323 * abx500_chargalg_algorithm() - Main function for the algorithm
1324 * @di: pointer to the abx500_chargalg structure
1325 *
1326 * This is the main control function for the charging algorithm.
1327 * It is called periodically or when something happens that will
1328 * trigger a state change
1329 */
1330static void abx500_chargalg_algorithm(struct abx500_chargalg *di)
1331{
1332 int charger_status;
Lee Jones4dcdf572013-02-14 09:24:10 +00001333 int ret;
Lee Jones4d3b4aa2013-02-15 10:58:58 +00001334 int curr_step_lvl;
Arun Murthy1668f812012-02-29 21:54:25 +05301335
1336 /* Collect data from all power_supply class devices */
1337 class_for_each_device(power_supply_class, NULL,
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +01001338 di->chargalg_psy, abx500_chargalg_get_ext_psy_data);
Arun Murthy1668f812012-02-29 21:54:25 +05301339
1340 abx500_chargalg_end_of_charge(di);
1341 abx500_chargalg_check_temp(di);
1342 abx500_chargalg_check_charger_voltage(di);
1343
1344 charger_status = abx500_chargalg_check_charger_connection(di);
Lee Jones4d3b4aa2013-02-15 10:58:58 +00001345 abx500_chargalg_check_current_step_status(di);
Lee Jones4dcdf572013-02-14 09:24:10 +00001346
1347 if (is_ab8500(di->parent)) {
1348 ret = abx500_chargalg_check_charger_enable(di);
1349 if (ret < 0)
1350 dev_err(di->dev, "Checking charger is enabled error"
1351 ": Returned Value %d\n", ret);
1352 }
1353
Arun Murthy1668f812012-02-29 21:54:25 +05301354 /*
1355 * First check if we have a charger connected.
1356 * Also we don't allow charging of unknown batteries if configured
1357 * this way
1358 */
1359 if (!charger_status ||
Lee Jonesb0284de2012-11-30 10:09:42 +00001360 (di->events.batt_unknown && !di->bm->chg_unknown_bat)) {
Arun Murthy1668f812012-02-29 21:54:25 +05301361 if (di->charge_state != STATE_HANDHELD) {
1362 di->events.safety_timer_expired = false;
1363 abx500_chargalg_state_to(di, STATE_HANDHELD_INIT);
1364 }
1365 }
1366
1367 /* If suspended, we should not continue checking the flags */
1368 else if (di->charge_state == STATE_SUSPENDED_INIT ||
1369 di->charge_state == STATE_SUSPENDED) {
1370 /* We don't do anything here, just don,t continue */
1371 }
1372
1373 /* Safety timer expiration */
1374 else if (di->events.safety_timer_expired) {
1375 if (di->charge_state != STATE_SAFETY_TIMER_EXPIRED)
1376 abx500_chargalg_state_to(di,
1377 STATE_SAFETY_TIMER_EXPIRED_INIT);
1378 }
1379 /*
1380 * Check if any interrupts has occured
1381 * that will prevent us from charging
1382 */
1383
1384 /* Battery removed */
1385 else if (di->events.batt_rem) {
1386 if (di->charge_state != STATE_BATT_REMOVED)
1387 abx500_chargalg_state_to(di, STATE_BATT_REMOVED_INIT);
1388 }
1389 /* Main or USB charger not ok. */
1390 else if (di->events.mainextchnotok || di->events.usbchargernotok) {
1391 /*
1392 * If vbus_collapsed is set, we have to lower the charger
1393 * current, which is done in the normal state below
1394 */
1395 if (di->charge_state != STATE_CHG_NOT_OK &&
1396 !di->events.vbus_collapsed)
1397 abx500_chargalg_state_to(di, STATE_CHG_NOT_OK_INIT);
1398 }
1399 /* VBUS, Main or VBAT OVV. */
1400 else if (di->events.vbus_ovv ||
1401 di->events.main_ovv ||
1402 di->events.batt_ovv ||
1403 !di->chg_info.usb_chg_ok ||
1404 !di->chg_info.ac_chg_ok) {
1405 if (di->charge_state != STATE_OVV_PROTECT)
1406 abx500_chargalg_state_to(di, STATE_OVV_PROTECT_INIT);
1407 }
1408 /* USB Thermal, stop charging */
1409 else if (di->events.main_thermal_prot ||
1410 di->events.usb_thermal_prot) {
1411 if (di->charge_state != STATE_HW_TEMP_PROTECT)
1412 abx500_chargalg_state_to(di,
1413 STATE_HW_TEMP_PROTECT_INIT);
1414 }
1415 /* Battery temp over/under */
1416 else if (di->events.btemp_underover) {
1417 if (di->charge_state != STATE_TEMP_UNDEROVER)
1418 abx500_chargalg_state_to(di,
1419 STATE_TEMP_UNDEROVER_INIT);
1420 }
1421 /* Watchdog expired */
1422 else if (di->events.ac_wd_expired ||
1423 di->events.usb_wd_expired) {
1424 if (di->charge_state != STATE_WD_EXPIRED)
1425 abx500_chargalg_state_to(di, STATE_WD_EXPIRED_INIT);
1426 }
1427 /* Battery temp high/low */
1428 else if (di->events.btemp_lowhigh) {
1429 if (di->charge_state != STATE_TEMP_LOWHIGH)
1430 abx500_chargalg_state_to(di, STATE_TEMP_LOWHIGH_INIT);
1431 }
1432
1433 dev_dbg(di->dev,
1434 "[CHARGALG] Vb %d Ib_avg %d Ib_inst %d Tb %d Cap %d Maint %d "
1435 "State %s Active_chg %d Chg_status %d AC %d USB %d "
1436 "AC_online %d USB_online %d AC_CV %d USB_CV %d AC_I %d "
1437 "USB_I %d AC_Vset %d AC_Iset %d USB_Vset %d USB_Iset %d\n",
1438 di->batt_data.volt,
1439 di->batt_data.avg_curr,
1440 di->batt_data.inst_curr,
1441 di->batt_data.temp,
1442 di->batt_data.percent,
1443 di->maintenance_chg,
1444 states[di->charge_state],
1445 di->chg_info.charger_type,
1446 di->charge_status,
1447 di->chg_info.conn_chg & AC_CHG,
1448 di->chg_info.conn_chg & USB_CHG,
1449 di->chg_info.online_chg & AC_CHG,
1450 di->chg_info.online_chg & USB_CHG,
1451 di->events.ac_cv_active,
1452 di->events.usb_cv_active,
1453 di->chg_info.ac_curr,
1454 di->chg_info.usb_curr,
1455 di->chg_info.ac_vset,
1456 di->chg_info.ac_iset,
1457 di->chg_info.usb_vset,
1458 di->chg_info.usb_iset);
1459
1460 switch (di->charge_state) {
1461 case STATE_HANDHELD_INIT:
1462 abx500_chargalg_stop_charging(di);
1463 di->charge_status = POWER_SUPPLY_STATUS_DISCHARGING;
1464 abx500_chargalg_state_to(di, STATE_HANDHELD);
1465 /* Intentional fallthrough */
1466
1467 case STATE_HANDHELD:
1468 break;
1469
1470 case STATE_SUSPENDED_INIT:
1471 if (di->susp_status.ac_suspended)
1472 abx500_chargalg_ac_en(di, false, 0, 0);
1473 if (di->susp_status.usb_suspended)
1474 abx500_chargalg_usb_en(di, false, 0, 0);
1475 abx500_chargalg_stop_safety_timer(di);
1476 abx500_chargalg_stop_maintenance_timer(di);
1477 di->charge_status = POWER_SUPPLY_STATUS_NOT_CHARGING;
1478 di->maintenance_chg = false;
1479 abx500_chargalg_state_to(di, STATE_SUSPENDED);
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +01001480 power_supply_changed(di->chargalg_psy);
Arun Murthy1668f812012-02-29 21:54:25 +05301481 /* Intentional fallthrough */
1482
1483 case STATE_SUSPENDED:
1484 /* CHARGING is suspended */
1485 break;
1486
1487 case STATE_BATT_REMOVED_INIT:
1488 abx500_chargalg_stop_charging(di);
1489 abx500_chargalg_state_to(di, STATE_BATT_REMOVED);
1490 /* Intentional fallthrough */
1491
1492 case STATE_BATT_REMOVED:
1493 if (!di->events.batt_rem)
1494 abx500_chargalg_state_to(di, STATE_NORMAL_INIT);
1495 break;
1496
1497 case STATE_HW_TEMP_PROTECT_INIT:
1498 abx500_chargalg_stop_charging(di);
1499 abx500_chargalg_state_to(di, STATE_HW_TEMP_PROTECT);
1500 /* Intentional fallthrough */
1501
1502 case STATE_HW_TEMP_PROTECT:
1503 if (!di->events.main_thermal_prot &&
1504 !di->events.usb_thermal_prot)
1505 abx500_chargalg_state_to(di, STATE_NORMAL_INIT);
1506 break;
1507
1508 case STATE_OVV_PROTECT_INIT:
1509 abx500_chargalg_stop_charging(di);
1510 abx500_chargalg_state_to(di, STATE_OVV_PROTECT);
1511 /* Intentional fallthrough */
1512
1513 case STATE_OVV_PROTECT:
1514 if (!di->events.vbus_ovv &&
1515 !di->events.main_ovv &&
1516 !di->events.batt_ovv &&
1517 di->chg_info.usb_chg_ok &&
1518 di->chg_info.ac_chg_ok)
1519 abx500_chargalg_state_to(di, STATE_NORMAL_INIT);
1520 break;
1521
1522 case STATE_CHG_NOT_OK_INIT:
1523 abx500_chargalg_stop_charging(di);
1524 abx500_chargalg_state_to(di, STATE_CHG_NOT_OK);
1525 /* Intentional fallthrough */
1526
1527 case STATE_CHG_NOT_OK:
1528 if (!di->events.mainextchnotok &&
1529 !di->events.usbchargernotok)
1530 abx500_chargalg_state_to(di, STATE_NORMAL_INIT);
1531 break;
1532
1533 case STATE_SAFETY_TIMER_EXPIRED_INIT:
1534 abx500_chargalg_stop_charging(di);
1535 abx500_chargalg_state_to(di, STATE_SAFETY_TIMER_EXPIRED);
1536 /* Intentional fallthrough */
1537
1538 case STATE_SAFETY_TIMER_EXPIRED:
1539 /* We exit this state when charger is removed */
1540 break;
1541
1542 case STATE_NORMAL_INIT:
Lee Jonesdb43e6c2013-02-14 12:39:15 +00001543 if ((di->chg_info.charger_type & USB_CHG) &&
1544 di->usb_chg->power_path) {
1545 if (di->batt_data.volt >
1546 (di->bm->fg_params->lowbat_threshold +
1547 BAT_PLUS_MARGIN)) {
1548 ab8540_chargalg_usb_pre_chg_en(di, false);
1549 ab8540_chargalg_usb_pp_en(di, false);
1550 } else {
1551 ab8540_chargalg_usb_pp_en(di, true);
1552 ab8540_chargalg_usb_pre_chg_en(di, true);
1553 abx500_chargalg_state_to(di,
1554 STATE_USB_PP_PRE_CHARGE);
1555 break;
1556 }
1557 }
1558
Lee Jones4d3b4aa2013-02-15 10:58:58 +00001559 if (di->curr_status.curr_step == CHARGALG_CURR_STEP_LOW)
1560 abx500_chargalg_stop_charging(di);
1561 else {
1562 curr_step_lvl = di->bm->bat_type[
1563 di->bm->batt_id].normal_cur_lvl
1564 * di->curr_status.curr_step
1565 / CHARGALG_CURR_STEP_HIGH;
1566 abx500_chargalg_start_charging(di,
1567 di->bm->bat_type[di->bm->batt_id]
1568 .normal_vol_lvl, curr_step_lvl);
1569 }
1570
Arun Murthy1668f812012-02-29 21:54:25 +05301571 abx500_chargalg_state_to(di, STATE_NORMAL);
1572 abx500_chargalg_start_safety_timer(di);
1573 abx500_chargalg_stop_maintenance_timer(di);
1574 init_maxim_chg_curr(di);
1575 di->charge_status = POWER_SUPPLY_STATUS_CHARGING;
1576 di->eoc_cnt = 0;
1577 di->maintenance_chg = false;
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +01001578 power_supply_changed(di->chargalg_psy);
Arun Murthy1668f812012-02-29 21:54:25 +05301579
1580 break;
1581
Lee Jonesdb43e6c2013-02-14 12:39:15 +00001582 case STATE_USB_PP_PRE_CHARGE:
1583 if (di->batt_data.volt >
1584 (di->bm->fg_params->lowbat_threshold +
1585 BAT_PLUS_MARGIN))
1586 abx500_chargalg_state_to(di, STATE_NORMAL_INIT);
1587 break;
1588
Arun Murthy1668f812012-02-29 21:54:25 +05301589 case STATE_NORMAL:
1590 handle_maxim_chg_curr(di);
1591 if (di->charge_status == POWER_SUPPLY_STATUS_FULL &&
1592 di->maintenance_chg) {
Lee Jonesb0284de2012-11-30 10:09:42 +00001593 if (di->bm->no_maintenance)
Arun Murthy1668f812012-02-29 21:54:25 +05301594 abx500_chargalg_state_to(di,
1595 STATE_WAIT_FOR_RECHARGE_INIT);
1596 else
1597 abx500_chargalg_state_to(di,
1598 STATE_MAINTENANCE_A_INIT);
1599 }
1600 break;
1601
1602 /* This state will be used when the maintenance state is disabled */
1603 case STATE_WAIT_FOR_RECHARGE_INIT:
1604 abx500_chargalg_hold_charging(di);
1605 abx500_chargalg_state_to(di, STATE_WAIT_FOR_RECHARGE);
Arun Murthy1668f812012-02-29 21:54:25 +05301606 /* Intentional fallthrough */
1607
1608 case STATE_WAIT_FOR_RECHARGE:
Marcus Cooperea402402013-01-11 13:12:54 +00001609 if (di->batt_data.percent <=
1610 di->bm->bat_type[di->bm->batt_id].
1611 recharge_cap)
1612 abx500_chargalg_state_to(di, STATE_NORMAL_INIT);
Arun Murthy1668f812012-02-29 21:54:25 +05301613 break;
1614
1615 case STATE_MAINTENANCE_A_INIT:
1616 abx500_chargalg_stop_safety_timer(di);
1617 abx500_chargalg_start_maintenance_timer(di,
Lee Jonesb0284de2012-11-30 10:09:42 +00001618 di->bm->bat_type[
1619 di->bm->batt_id].maint_a_chg_timer_h);
Arun Murthy1668f812012-02-29 21:54:25 +05301620 abx500_chargalg_start_charging(di,
Lee Jonesb0284de2012-11-30 10:09:42 +00001621 di->bm->bat_type[
1622 di->bm->batt_id].maint_a_vol_lvl,
1623 di->bm->bat_type[
1624 di->bm->batt_id].maint_a_cur_lvl);
Arun Murthy1668f812012-02-29 21:54:25 +05301625 abx500_chargalg_state_to(di, STATE_MAINTENANCE_A);
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +01001626 power_supply_changed(di->chargalg_psy);
Arun Murthy1668f812012-02-29 21:54:25 +05301627 /* Intentional fallthrough*/
1628
1629 case STATE_MAINTENANCE_A:
1630 if (di->events.maintenance_timer_expired) {
1631 abx500_chargalg_stop_maintenance_timer(di);
1632 abx500_chargalg_state_to(di, STATE_MAINTENANCE_B_INIT);
1633 }
1634 break;
1635
1636 case STATE_MAINTENANCE_B_INIT:
1637 abx500_chargalg_start_maintenance_timer(di,
Lee Jonesb0284de2012-11-30 10:09:42 +00001638 di->bm->bat_type[
1639 di->bm->batt_id].maint_b_chg_timer_h);
Arun Murthy1668f812012-02-29 21:54:25 +05301640 abx500_chargalg_start_charging(di,
Lee Jonesb0284de2012-11-30 10:09:42 +00001641 di->bm->bat_type[
1642 di->bm->batt_id].maint_b_vol_lvl,
1643 di->bm->bat_type[
1644 di->bm->batt_id].maint_b_cur_lvl);
Arun Murthy1668f812012-02-29 21:54:25 +05301645 abx500_chargalg_state_to(di, STATE_MAINTENANCE_B);
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +01001646 power_supply_changed(di->chargalg_psy);
Arun Murthy1668f812012-02-29 21:54:25 +05301647 /* Intentional fallthrough*/
1648
1649 case STATE_MAINTENANCE_B:
1650 if (di->events.maintenance_timer_expired) {
1651 abx500_chargalg_stop_maintenance_timer(di);
1652 abx500_chargalg_state_to(di, STATE_NORMAL_INIT);
1653 }
1654 break;
1655
1656 case STATE_TEMP_LOWHIGH_INIT:
1657 abx500_chargalg_start_charging(di,
Lee Jonesb0284de2012-11-30 10:09:42 +00001658 di->bm->bat_type[
1659 di->bm->batt_id].low_high_vol_lvl,
1660 di->bm->bat_type[
1661 di->bm->batt_id].low_high_cur_lvl);
Arun Murthy1668f812012-02-29 21:54:25 +05301662 abx500_chargalg_stop_maintenance_timer(di);
1663 di->charge_status = POWER_SUPPLY_STATUS_CHARGING;
1664 abx500_chargalg_state_to(di, STATE_TEMP_LOWHIGH);
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +01001665 power_supply_changed(di->chargalg_psy);
Arun Murthy1668f812012-02-29 21:54:25 +05301666 /* Intentional fallthrough */
1667
1668 case STATE_TEMP_LOWHIGH:
1669 if (!di->events.btemp_lowhigh)
1670 abx500_chargalg_state_to(di, STATE_NORMAL_INIT);
1671 break;
1672
1673 case STATE_WD_EXPIRED_INIT:
1674 abx500_chargalg_stop_charging(di);
1675 abx500_chargalg_state_to(di, STATE_WD_EXPIRED);
1676 /* Intentional fallthrough */
1677
1678 case STATE_WD_EXPIRED:
1679 if (!di->events.ac_wd_expired &&
1680 !di->events.usb_wd_expired)
1681 abx500_chargalg_state_to(di, STATE_NORMAL_INIT);
1682 break;
1683
1684 case STATE_TEMP_UNDEROVER_INIT:
1685 abx500_chargalg_stop_charging(di);
1686 abx500_chargalg_state_to(di, STATE_TEMP_UNDEROVER);
1687 /* Intentional fallthrough */
1688
1689 case STATE_TEMP_UNDEROVER:
1690 if (!di->events.btemp_underover)
1691 abx500_chargalg_state_to(di, STATE_NORMAL_INIT);
1692 break;
1693 }
1694
1695 /* Start charging directly if the new state is a charge state */
1696 if (di->charge_state == STATE_NORMAL_INIT ||
1697 di->charge_state == STATE_MAINTENANCE_A_INIT ||
1698 di->charge_state == STATE_MAINTENANCE_B_INIT)
1699 queue_work(di->chargalg_wq, &di->chargalg_work);
1700}
1701
1702/**
1703 * abx500_chargalg_periodic_work() - Periodic work for the algorithm
1704 * @work: pointer to the work_struct structure
1705 *
1706 * Work queue function for the charging algorithm
1707 */
1708static void abx500_chargalg_periodic_work(struct work_struct *work)
1709{
1710 struct abx500_chargalg *di = container_of(work,
1711 struct abx500_chargalg, chargalg_periodic_work.work);
1712
1713 abx500_chargalg_algorithm(di);
1714
1715 /*
1716 * If a charger is connected then the battery has to be monitored
1717 * frequently, else the work can be delayed.
1718 */
1719 if (di->chg_info.conn_chg)
1720 queue_delayed_work(di->chargalg_wq,
1721 &di->chargalg_periodic_work,
Lee Jonesb0284de2012-11-30 10:09:42 +00001722 di->bm->interval_charging * HZ);
Arun Murthy1668f812012-02-29 21:54:25 +05301723 else
1724 queue_delayed_work(di->chargalg_wq,
1725 &di->chargalg_periodic_work,
Lee Jonesb0284de2012-11-30 10:09:42 +00001726 di->bm->interval_not_charging * HZ);
Arun Murthy1668f812012-02-29 21:54:25 +05301727}
1728
1729/**
1730 * abx500_chargalg_wd_work() - periodic work to kick the charger watchdog
1731 * @work: pointer to the work_struct structure
1732 *
1733 * Work queue function for kicking the charger watchdog
1734 */
1735static void abx500_chargalg_wd_work(struct work_struct *work)
1736{
1737 int ret;
1738 struct abx500_chargalg *di = container_of(work,
1739 struct abx500_chargalg, chargalg_wd_work.work);
1740
1741 dev_dbg(di->dev, "abx500_chargalg_wd_work\n");
1742
1743 ret = abx500_chargalg_kick_watchdog(di);
1744 if (ret < 0)
1745 dev_err(di->dev, "failed to kick watchdog\n");
1746
1747 queue_delayed_work(di->chargalg_wq,
1748 &di->chargalg_wd_work, CHG_WD_INTERVAL);
1749}
1750
1751/**
1752 * abx500_chargalg_work() - Work to run the charging algorithm instantly
1753 * @work: pointer to the work_struct structure
1754 *
1755 * Work queue function for calling the charging algorithm
1756 */
1757static void abx500_chargalg_work(struct work_struct *work)
1758{
1759 struct abx500_chargalg *di = container_of(work,
1760 struct abx500_chargalg, chargalg_work);
1761
1762 abx500_chargalg_algorithm(di);
1763}
1764
1765/**
1766 * abx500_chargalg_get_property() - get the chargalg properties
1767 * @psy: pointer to the power_supply structure
1768 * @psp: pointer to the power_supply_property structure
1769 * @val: pointer to the power_supply_propval union
1770 *
1771 * This function gets called when an application tries to get the
1772 * chargalg properties by reading the sysfs files.
1773 * status: charging/discharging/full/unknown
1774 * health: health of the battery
1775 * Returns error code in case of failure else 0 on success
1776 */
1777static int abx500_chargalg_get_property(struct power_supply *psy,
1778 enum power_supply_property psp,
1779 union power_supply_propval *val)
1780{
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +01001781 struct abx500_chargalg *di = power_supply_get_drvdata(psy);
Arun Murthy1668f812012-02-29 21:54:25 +05301782
1783 switch (psp) {
1784 case POWER_SUPPLY_PROP_STATUS:
1785 val->intval = di->charge_status;
1786 break;
1787 case POWER_SUPPLY_PROP_HEALTH:
1788 if (di->events.batt_ovv) {
1789 val->intval = POWER_SUPPLY_HEALTH_OVERVOLTAGE;
1790 } else if (di->events.btemp_underover) {
Lee Jonesb0284de2012-11-30 10:09:42 +00001791 if (di->batt_data.temp <= di->bm->temp_under)
Arun Murthy1668f812012-02-29 21:54:25 +05301792 val->intval = POWER_SUPPLY_HEALTH_COLD;
1793 else
1794 val->intval = POWER_SUPPLY_HEALTH_OVERHEAT;
Lee Jonesd80108f2013-01-17 13:49:45 +00001795 } else if (di->charge_state == STATE_SAFETY_TIMER_EXPIRED ||
1796 di->charge_state == STATE_SAFETY_TIMER_EXPIRED_INIT) {
1797 val->intval = POWER_SUPPLY_HEALTH_UNSPEC_FAILURE;
Arun Murthy1668f812012-02-29 21:54:25 +05301798 } else {
1799 val->intval = POWER_SUPPLY_HEALTH_GOOD;
1800 }
1801 break;
1802 default:
1803 return -EINVAL;
1804 }
1805 return 0;
1806}
1807
1808/* Exposure to the sysfs interface */
1809
Lee Jones4d3b4aa2013-02-15 10:58:58 +00001810static ssize_t abx500_chargalg_curr_step_show(struct abx500_chargalg *di,
1811 char *buf)
Lee Jonesc9ade0f2013-01-21 11:22:56 +00001812{
Lee Jones4d3b4aa2013-02-15 10:58:58 +00001813 return sprintf(buf, "%d\n", di->curr_status.curr_step);
1814}
Lee Jonesc9ade0f2013-01-21 11:22:56 +00001815
Lee Jones4d3b4aa2013-02-15 10:58:58 +00001816static ssize_t abx500_chargalg_curr_step_store(struct abx500_chargalg *di,
1817 const char *buf, size_t length)
1818{
1819 long int param;
1820 int ret;
1821
1822 ret = kstrtol(buf, 10, &param);
1823 if (ret < 0)
1824 return ret;
1825
1826 di->curr_status.curr_step = param;
1827 if (di->curr_status.curr_step >= CHARGALG_CURR_STEP_LOW &&
1828 di->curr_status.curr_step <= CHARGALG_CURR_STEP_HIGH) {
1829 di->curr_status.curr_step_change = true;
1830 queue_work(di->chargalg_wq, &di->chargalg_work);
1831 } else
1832 dev_info(di->dev, "Wrong current step\n"
1833 "Enter 0. Disable AC/USB Charging\n"
1834 "1--100. Set AC/USB charging current step\n"
1835 "100. Enable AC/USB Charging\n");
1836
1837 return strlen(buf);
1838}
1839
1840
1841static ssize_t abx500_chargalg_en_show(struct abx500_chargalg *di,
1842 char *buf)
1843{
Lee Jonesc9ade0f2013-01-21 11:22:56 +00001844 return sprintf(buf, "%d\n",
1845 di->susp_status.ac_suspended &&
1846 di->susp_status.usb_suspended);
1847}
1848
Lee Jones4d3b4aa2013-02-15 10:58:58 +00001849static ssize_t abx500_chargalg_en_store(struct abx500_chargalg *di,
1850 const char *buf, size_t length)
Arun Murthy1668f812012-02-29 21:54:25 +05301851{
Arun Murthy1668f812012-02-29 21:54:25 +05301852 long int param;
1853 int ac_usb;
1854 int ret;
Arun Murthy1668f812012-02-29 21:54:25 +05301855
Lee Jones4d3b4aa2013-02-15 10:58:58 +00001856 ret = kstrtol(buf, 10, &param);
1857 if (ret < 0)
1858 return ret;
Arun Murthy1668f812012-02-29 21:54:25 +05301859
Lee Jones4d3b4aa2013-02-15 10:58:58 +00001860 ac_usb = param;
1861 switch (ac_usb) {
1862 case 0:
1863 /* Disable charging */
1864 di->susp_status.ac_suspended = true;
1865 di->susp_status.usb_suspended = true;
1866 di->susp_status.suspended_change = true;
1867 /* Trigger a state change */
1868 queue_work(di->chargalg_wq,
1869 &di->chargalg_work);
Arun Murthy1668f812012-02-29 21:54:25 +05301870 break;
Lee Jones4d3b4aa2013-02-15 10:58:58 +00001871 case 1:
1872 /* Enable AC Charging */
1873 di->susp_status.ac_suspended = false;
1874 di->susp_status.suspended_change = true;
1875 /* Trigger a state change */
1876 queue_work(di->chargalg_wq,
1877 &di->chargalg_work);
1878 break;
1879 case 2:
1880 /* Enable USB charging */
1881 di->susp_status.usb_suspended = false;
1882 di->susp_status.suspended_change = true;
1883 /* Trigger a state change */
1884 queue_work(di->chargalg_wq,
1885 &di->chargalg_work);
1886 break;
1887 default:
1888 dev_info(di->dev, "Wrong input\n"
1889 "Enter 0. Disable AC/USB Charging\n"
1890 "1. Enable AC charging\n"
1891 "2. Enable USB Charging\n");
Arun Murthy1668f812012-02-29 21:54:25 +05301892 };
1893 return strlen(buf);
1894}
1895
Lee Jones4d3b4aa2013-02-15 10:58:58 +00001896static struct abx500_chargalg_sysfs_entry abx500_chargalg_en_charger =
1897 __ATTR(chargalg, 0644, abx500_chargalg_en_show,
1898 abx500_chargalg_en_store);
1899
1900static struct abx500_chargalg_sysfs_entry abx500_chargalg_curr_step =
1901 __ATTR(chargalg_curr_step, 0644, abx500_chargalg_curr_step_show,
1902 abx500_chargalg_curr_step_store);
1903
1904static ssize_t abx500_chargalg_sysfs_show(struct kobject *kobj,
1905 struct attribute *attr, char *buf)
Arun Murthy1668f812012-02-29 21:54:25 +05301906{
Lee Jones4d3b4aa2013-02-15 10:58:58 +00001907 struct abx500_chargalg_sysfs_entry *entry = container_of(attr,
1908 struct abx500_chargalg_sysfs_entry, attr);
1909
1910 struct abx500_chargalg *di = container_of(kobj,
1911 struct abx500_chargalg, chargalg_kobject);
1912
1913 if (!entry->show)
1914 return -EIO;
1915
1916 return entry->show(di, buf);
1917}
1918
1919static ssize_t abx500_chargalg_sysfs_charger(struct kobject *kobj,
1920 struct attribute *attr, const char *buf, size_t length)
1921{
1922 struct abx500_chargalg_sysfs_entry *entry = container_of(attr,
1923 struct abx500_chargalg_sysfs_entry, attr);
1924
1925 struct abx500_chargalg *di = container_of(kobj,
1926 struct abx500_chargalg, chargalg_kobject);
1927
1928 if (!entry->store)
1929 return -EIO;
1930
1931 return entry->store(di, buf, length);
1932}
Arun Murthy1668f812012-02-29 21:54:25 +05301933
1934static struct attribute *abx500_chargalg_chg[] = {
Lee Jones4d3b4aa2013-02-15 10:58:58 +00001935 &abx500_chargalg_en_charger.attr,
1936 &abx500_chargalg_curr_step.attr,
1937 NULL,
Arun Murthy1668f812012-02-29 21:54:25 +05301938};
1939
Anton Vorontsov64eb9b02012-03-14 04:43:11 +04001940static const struct sysfs_ops abx500_chargalg_sysfs_ops = {
Lee Jonesc9ade0f2013-01-21 11:22:56 +00001941 .show = abx500_chargalg_sysfs_show,
Arun Murthy1668f812012-02-29 21:54:25 +05301942 .store = abx500_chargalg_sysfs_charger,
1943};
1944
1945static struct kobj_type abx500_chargalg_ktype = {
1946 .sysfs_ops = &abx500_chargalg_sysfs_ops,
1947 .default_attrs = abx500_chargalg_chg,
1948};
1949
1950/**
1951 * abx500_chargalg_sysfs_exit() - de-init of sysfs entry
1952 * @di: pointer to the struct abx500_chargalg
1953 *
1954 * This function removes the entry in sysfs.
1955 */
1956static void abx500_chargalg_sysfs_exit(struct abx500_chargalg *di)
1957{
1958 kobject_del(&di->chargalg_kobject);
1959}
1960
1961/**
1962 * abx500_chargalg_sysfs_init() - init of sysfs entry
1963 * @di: pointer to the struct abx500_chargalg
1964 *
1965 * This function adds an entry in sysfs.
1966 * Returns error code in case of failure else 0(on success)
1967 */
1968static int abx500_chargalg_sysfs_init(struct abx500_chargalg *di)
1969{
1970 int ret = 0;
1971
1972 ret = kobject_init_and_add(&di->chargalg_kobject,
1973 &abx500_chargalg_ktype,
1974 NULL, "abx500_chargalg");
1975 if (ret < 0)
1976 dev_err(di->dev, "failed to create sysfs entry\n");
1977
1978 return ret;
1979}
1980/* Exposure to the sysfs interface <<END>> */
1981
1982#if defined(CONFIG_PM)
1983static int abx500_chargalg_resume(struct platform_device *pdev)
1984{
1985 struct abx500_chargalg *di = platform_get_drvdata(pdev);
1986
1987 /* Kick charger watchdog if charging (any charger online) */
1988 if (di->chg_info.online_chg)
1989 queue_delayed_work(di->chargalg_wq, &di->chargalg_wd_work, 0);
1990
1991 /*
1992 * Run the charging algorithm directly to be sure we don't
1993 * do it too seldom
1994 */
1995 queue_delayed_work(di->chargalg_wq, &di->chargalg_periodic_work, 0);
1996
1997 return 0;
1998}
1999
2000static int abx500_chargalg_suspend(struct platform_device *pdev,
2001 pm_message_t state)
2002{
2003 struct abx500_chargalg *di = platform_get_drvdata(pdev);
2004
2005 if (di->chg_info.online_chg)
2006 cancel_delayed_work_sync(&di->chargalg_wd_work);
2007
2008 cancel_delayed_work_sync(&di->chargalg_periodic_work);
2009
2010 return 0;
2011}
2012#else
2013#define abx500_chargalg_suspend NULL
2014#define abx500_chargalg_resume NULL
2015#endif
2016
Bill Pemberton415ec692012-11-19 13:26:07 -05002017static int abx500_chargalg_remove(struct platform_device *pdev)
Arun Murthy1668f812012-02-29 21:54:25 +05302018{
2019 struct abx500_chargalg *di = platform_get_drvdata(pdev);
2020
2021 /* sysfs interface to enable/disbale charging from user space */
2022 abx500_chargalg_sysfs_exit(di);
2023
Lee Jones257107a2013-02-13 15:15:03 +00002024 hrtimer_cancel(&di->safety_timer);
2025 hrtimer_cancel(&di->maintenance_timer);
2026
2027 cancel_delayed_work_sync(&di->chargalg_periodic_work);
2028 cancel_delayed_work_sync(&di->chargalg_wd_work);
2029 cancel_work_sync(&di->chargalg_work);
2030
Arun Murthy1668f812012-02-29 21:54:25 +05302031 /* Delete the work queue */
2032 destroy_workqueue(di->chargalg_wq);
2033
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +01002034 power_supply_unregister(di->chargalg_psy);
Arun Murthy1668f812012-02-29 21:54:25 +05302035
2036 return 0;
2037}
2038
Rajanikanth H.Va12810a2012-10-31 15:40:33 +00002039static char *supply_interface[] = {
2040 "ab8500_fg",
2041};
2042
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +01002043static const struct power_supply_desc abx500_chargalg_desc = {
2044 .name = "abx500_chargalg",
2045 .type = POWER_SUPPLY_TYPE_BATTERY,
2046 .properties = abx500_chargalg_props,
2047 .num_properties = ARRAY_SIZE(abx500_chargalg_props),
2048 .get_property = abx500_chargalg_get_property,
2049 .external_power_changed = abx500_chargalg_external_power_changed,
2050};
2051
Bill Pembertonc8afa642012-11-19 13:22:23 -05002052static int abx500_chargalg_probe(struct platform_device *pdev)
Arun Murthy1668f812012-02-29 21:54:25 +05302053{
Rajanikanth H.Va12810a2012-10-31 15:40:33 +00002054 struct device_node *np = pdev->dev.of_node;
Lee Jonesbdc56b42012-11-30 10:57:14 +00002055 struct abx500_bm_data *plat = pdev->dev.platform_data;
Krzysztof Kozlowski2dc92152015-03-12 08:44:02 +01002056 struct power_supply_config psy_cfg = {};
Rajanikanth H.Va12810a2012-10-31 15:40:33 +00002057 struct abx500_chargalg *di;
Arun Murthy1668f812012-02-29 21:54:25 +05302058 int ret = 0;
2059
Rajanikanth H.Va12810a2012-10-31 15:40:33 +00002060 di = devm_kzalloc(&pdev->dev, sizeof(*di), GFP_KERNEL);
2061 if (!di) {
2062 dev_err(&pdev->dev, "%s no mem for ab8500_chargalg\n", __func__);
Arun Murthy1668f812012-02-29 21:54:25 +05302063 return -ENOMEM;
Rajanikanth H.Va12810a2012-10-31 15:40:33 +00002064 }
Lee Jonesbdc56b42012-11-30 10:57:14 +00002065
2066 if (!plat) {
2067 dev_err(&pdev->dev, "no battery management data supplied\n");
2068 return -EINVAL;
2069 }
2070 di->bm = plat;
2071
2072 if (np) {
2073 ret = ab8500_bm_of_probe(&pdev->dev, np, di->bm);
2074 if (ret) {
2075 dev_err(&pdev->dev, "failed to get battery information\n");
2076 return ret;
Rajanikanth H.Va12810a2012-10-31 15:40:33 +00002077 }
Rajanikanth H.Va12810a2012-10-31 15:40:33 +00002078 }
Arun Murthy1668f812012-02-29 21:54:25 +05302079
Lee Jones330b7eb2013-02-15 10:53:57 +00002080 /* get device struct and parent */
Arun Murthy1668f812012-02-29 21:54:25 +05302081 di->dev = &pdev->dev;
Lee Jones330b7eb2013-02-15 10:53:57 +00002082 di->parent = dev_get_drvdata(pdev->dev.parent);
Arun Murthy1668f812012-02-29 21:54:25 +05302083
Krzysztof Kozlowski2dc92152015-03-12 08:44:02 +01002084 psy_cfg.supplied_to = supply_interface;
2085 psy_cfg.num_supplicants = ARRAY_SIZE(supply_interface);
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +01002086 psy_cfg.drv_data = di;
Krzysztof Kozlowski2dc92152015-03-12 08:44:02 +01002087
Arun Murthy1668f812012-02-29 21:54:25 +05302088 /* Initilialize safety timer */
Lee Jones257107a2013-02-13 15:15:03 +00002089 hrtimer_init(&di->safety_timer, CLOCK_REALTIME, HRTIMER_MODE_ABS);
Arun Murthy1668f812012-02-29 21:54:25 +05302090 di->safety_timer.function = abx500_chargalg_safety_timer_expired;
Arun Murthy1668f812012-02-29 21:54:25 +05302091
2092 /* Initilialize maintenance timer */
Lee Jones257107a2013-02-13 15:15:03 +00002093 hrtimer_init(&di->maintenance_timer, CLOCK_REALTIME, HRTIMER_MODE_ABS);
Arun Murthy1668f812012-02-29 21:54:25 +05302094 di->maintenance_timer.function =
2095 abx500_chargalg_maintenance_timer_expired;
Arun Murthy1668f812012-02-29 21:54:25 +05302096
2097 /* Create a work queue for the chargalg */
2098 di->chargalg_wq =
2099 create_singlethread_workqueue("abx500_chargalg_wq");
2100 if (di->chargalg_wq == NULL) {
2101 dev_err(di->dev, "failed to create work queue\n");
Rajanikanth H.Va12810a2012-10-31 15:40:33 +00002102 return -ENOMEM;
Arun Murthy1668f812012-02-29 21:54:25 +05302103 }
2104
2105 /* Init work for chargalg */
Tejun Heo203b42f2012-08-21 13:18:23 -07002106 INIT_DEFERRABLE_WORK(&di->chargalg_periodic_work,
Arun Murthy1668f812012-02-29 21:54:25 +05302107 abx500_chargalg_periodic_work);
Tejun Heo203b42f2012-08-21 13:18:23 -07002108 INIT_DEFERRABLE_WORK(&di->chargalg_wd_work,
Arun Murthy1668f812012-02-29 21:54:25 +05302109 abx500_chargalg_wd_work);
2110
2111 /* Init work for chargalg */
2112 INIT_WORK(&di->chargalg_work, abx500_chargalg_work);
2113
2114 /* To detect charger at startup */
2115 di->chg_info.prev_conn_chg = -1;
2116
2117 /* Register chargalg power supply class */
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +01002118 di->chargalg_psy = power_supply_register(di->dev, &abx500_chargalg_desc,
2119 &psy_cfg);
2120 if (IS_ERR(di->chargalg_psy)) {
Arun Murthy1668f812012-02-29 21:54:25 +05302121 dev_err(di->dev, "failed to register chargalg psy\n");
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +01002122 ret = PTR_ERR(di->chargalg_psy);
Arun Murthy1668f812012-02-29 21:54:25 +05302123 goto free_chargalg_wq;
2124 }
2125
2126 platform_set_drvdata(pdev, di);
2127
2128 /* sysfs interface to enable/disable charging from user space */
2129 ret = abx500_chargalg_sysfs_init(di);
2130 if (ret) {
2131 dev_err(di->dev, "failed to create sysfs entry\n");
2132 goto free_psy;
2133 }
Lee Jones4d3b4aa2013-02-15 10:58:58 +00002134 di->curr_status.curr_step = CHARGALG_CURR_STEP_HIGH;
Arun Murthy1668f812012-02-29 21:54:25 +05302135
2136 /* Run the charging algorithm */
2137 queue_delayed_work(di->chargalg_wq, &di->chargalg_periodic_work, 0);
2138
2139 dev_info(di->dev, "probe success\n");
2140 return ret;
2141
2142free_psy:
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +01002143 power_supply_unregister(di->chargalg_psy);
Arun Murthy1668f812012-02-29 21:54:25 +05302144free_chargalg_wq:
2145 destroy_workqueue(di->chargalg_wq);
Arun Murthy1668f812012-02-29 21:54:25 +05302146 return ret;
2147}
2148
Rajanikanth H.Va12810a2012-10-31 15:40:33 +00002149static const struct of_device_id ab8500_chargalg_match[] = {
2150 { .compatible = "stericsson,ab8500-chargalg", },
2151 { },
2152};
2153
Arun Murthy1668f812012-02-29 21:54:25 +05302154static struct platform_driver abx500_chargalg_driver = {
2155 .probe = abx500_chargalg_probe,
Bill Pemberton28ea73f2012-11-19 13:20:40 -05002156 .remove = abx500_chargalg_remove,
Arun Murthy1668f812012-02-29 21:54:25 +05302157 .suspend = abx500_chargalg_suspend,
2158 .resume = abx500_chargalg_resume,
2159 .driver = {
Rajanikanth H.Va12810a2012-10-31 15:40:33 +00002160 .name = "ab8500-chargalg",
Rajanikanth H.Va12810a2012-10-31 15:40:33 +00002161 .of_match_table = ab8500_chargalg_match,
Arun Murthy1668f812012-02-29 21:54:25 +05302162 },
2163};
2164
srinidhi kasagar1ee26af2012-10-11 14:38:28 +05302165module_platform_driver(abx500_chargalg_driver);
Arun Murthy1668f812012-02-29 21:54:25 +05302166
2167MODULE_LICENSE("GPL v2");
2168MODULE_AUTHOR("Johan Palsson, Karl Komierowski");
2169MODULE_ALIAS("platform:abx500-chargalg");
2170MODULE_DESCRIPTION("abx500 battery charging algorithm");