blob: d9104b1ab7cfd765ae59bd71220f68f0df3e9adb [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;
Andy Shevchenkoea32cea2016-03-17 14:22:29 -0700978 struct power_supply *ext = dev_get_drvdata(dev);
979 const char **supplicants = (const char **)ext->supplied_to;
Arun Murthy1668f812012-02-29 21:54:25 +0530980 struct abx500_chargalg *di;
981 union power_supply_propval ret;
Andy Shevchenkoea32cea2016-03-17 14:22:29 -0700982 int j;
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;
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100986 di = power_supply_get_drvdata(psy);
Arun Murthy1668f812012-02-29 21:54:25 +0530987 /* For all psy where the driver name appears in any supplied_to */
Andy Shevchenkoea32cea2016-03-17 14:22:29 -0700988 j = match_string(supplicants, ext->num_supplicants, psy->desc->name);
989 if (j < 0)
Arun Murthy1668f812012-02-29 21:54:25 +0530990 return 0;
991
Marcus Cooperea402402013-01-11 13:12:54 +0000992 /*
993 * If external is not registering 'POWER_SUPPLY_PROP_CAPACITY' to its
994 * property because of handling that sysfs entry on its own, this is
995 * the place to get the battery capacity.
996 */
Krzysztof Kozlowski15077fc2015-03-12 08:44:06 +0100997 if (!power_supply_get_property(ext, POWER_SUPPLY_PROP_CAPACITY, &ret)) {
Marcus Cooperea402402013-01-11 13:12:54 +0000998 di->batt_data.percent = ret.intval;
999 capacity_updated = true;
1000 }
1001
Arun Murthy1668f812012-02-29 21:54:25 +05301002 /* Go through all properties for the psy */
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +01001003 for (j = 0; j < ext->desc->num_properties; j++) {
Arun Murthy1668f812012-02-29 21:54:25 +05301004 enum power_supply_property prop;
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +01001005 prop = ext->desc->properties[j];
Arun Murthy1668f812012-02-29 21:54:25 +05301006
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +01001007 /*
1008 * Initialize chargers if not already done.
1009 * The ab8500_charger*/
Arun Murthy1668f812012-02-29 21:54:25 +05301010 if (!di->ac_chg &&
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +01001011 ext->desc->type == POWER_SUPPLY_TYPE_MAINS)
Arun Murthy1668f812012-02-29 21:54:25 +05301012 di->ac_chg = psy_to_ux500_charger(ext);
1013 else if (!di->usb_chg &&
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +01001014 ext->desc->type == POWER_SUPPLY_TYPE_USB)
Arun Murthy1668f812012-02-29 21:54:25 +05301015 di->usb_chg = psy_to_ux500_charger(ext);
1016
Krzysztof Kozlowski15077fc2015-03-12 08:44:06 +01001017 if (power_supply_get_property(ext, prop, &ret))
Arun Murthy1668f812012-02-29 21:54:25 +05301018 continue;
1019 switch (prop) {
1020 case POWER_SUPPLY_PROP_PRESENT:
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +01001021 switch (ext->desc->type) {
Arun Murthy1668f812012-02-29 21:54:25 +05301022 case POWER_SUPPLY_TYPE_BATTERY:
1023 /* Battery present */
1024 if (ret.intval)
1025 di->events.batt_rem = false;
1026 /* Battery removed */
1027 else
1028 di->events.batt_rem = true;
1029 break;
1030 case POWER_SUPPLY_TYPE_MAINS:
1031 /* AC disconnected */
1032 if (!ret.intval &&
1033 (di->chg_info.conn_chg & AC_CHG)) {
1034 di->chg_info.prev_conn_chg =
1035 di->chg_info.conn_chg;
1036 di->chg_info.conn_chg &= ~AC_CHG;
1037 }
1038 /* AC connected */
1039 else if (ret.intval &&
1040 !(di->chg_info.conn_chg & AC_CHG)) {
1041 di->chg_info.prev_conn_chg =
1042 di->chg_info.conn_chg;
1043 di->chg_info.conn_chg |= AC_CHG;
1044 }
1045 break;
1046 case POWER_SUPPLY_TYPE_USB:
1047 /* USB disconnected */
1048 if (!ret.intval &&
1049 (di->chg_info.conn_chg & USB_CHG)) {
1050 di->chg_info.prev_conn_chg =
1051 di->chg_info.conn_chg;
1052 di->chg_info.conn_chg &= ~USB_CHG;
1053 }
1054 /* USB connected */
1055 else if (ret.intval &&
1056 !(di->chg_info.conn_chg & USB_CHG)) {
1057 di->chg_info.prev_conn_chg =
1058 di->chg_info.conn_chg;
1059 di->chg_info.conn_chg |= USB_CHG;
1060 }
1061 break;
1062 default:
1063 break;
1064 }
1065 break;
1066
1067 case POWER_SUPPLY_PROP_ONLINE:
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +01001068 switch (ext->desc->type) {
Arun Murthy1668f812012-02-29 21:54:25 +05301069 case POWER_SUPPLY_TYPE_BATTERY:
1070 break;
1071 case POWER_SUPPLY_TYPE_MAINS:
1072 /* AC offline */
1073 if (!ret.intval &&
1074 (di->chg_info.online_chg & AC_CHG)) {
1075 di->chg_info.prev_online_chg =
1076 di->chg_info.online_chg;
1077 di->chg_info.online_chg &= ~AC_CHG;
1078 }
1079 /* AC online */
1080 else if (ret.intval &&
1081 !(di->chg_info.online_chg & AC_CHG)) {
1082 di->chg_info.prev_online_chg =
1083 di->chg_info.online_chg;
1084 di->chg_info.online_chg |= AC_CHG;
1085 queue_delayed_work(di->chargalg_wq,
1086 &di->chargalg_wd_work, 0);
1087 }
1088 break;
1089 case POWER_SUPPLY_TYPE_USB:
1090 /* USB offline */
1091 if (!ret.intval &&
1092 (di->chg_info.online_chg & USB_CHG)) {
1093 di->chg_info.prev_online_chg =
1094 di->chg_info.online_chg;
1095 di->chg_info.online_chg &= ~USB_CHG;
1096 }
1097 /* USB online */
1098 else if (ret.intval &&
1099 !(di->chg_info.online_chg & USB_CHG)) {
1100 di->chg_info.prev_online_chg =
1101 di->chg_info.online_chg;
1102 di->chg_info.online_chg |= USB_CHG;
1103 queue_delayed_work(di->chargalg_wq,
1104 &di->chargalg_wd_work, 0);
1105 }
1106 break;
1107 default:
1108 break;
1109 }
1110 break;
1111
1112 case POWER_SUPPLY_PROP_HEALTH:
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +01001113 switch (ext->desc->type) {
Arun Murthy1668f812012-02-29 21:54:25 +05301114 case POWER_SUPPLY_TYPE_BATTERY:
1115 break;
1116 case POWER_SUPPLY_TYPE_MAINS:
1117 switch (ret.intval) {
1118 case POWER_SUPPLY_HEALTH_UNSPEC_FAILURE:
1119 di->events.mainextchnotok = true;
1120 di->events.main_thermal_prot = false;
1121 di->events.main_ovv = false;
1122 di->events.ac_wd_expired = false;
1123 break;
1124 case POWER_SUPPLY_HEALTH_DEAD:
1125 di->events.ac_wd_expired = true;
1126 di->events.mainextchnotok = false;
1127 di->events.main_ovv = false;
1128 di->events.main_thermal_prot = false;
1129 break;
1130 case POWER_SUPPLY_HEALTH_COLD:
1131 case POWER_SUPPLY_HEALTH_OVERHEAT:
1132 di->events.main_thermal_prot = true;
1133 di->events.mainextchnotok = false;
1134 di->events.main_ovv = false;
1135 di->events.ac_wd_expired = false;
1136 break;
1137 case POWER_SUPPLY_HEALTH_OVERVOLTAGE:
1138 di->events.main_ovv = true;
1139 di->events.mainextchnotok = false;
1140 di->events.main_thermal_prot = false;
1141 di->events.ac_wd_expired = false;
1142 break;
1143 case POWER_SUPPLY_HEALTH_GOOD:
1144 di->events.main_thermal_prot = false;
1145 di->events.mainextchnotok = false;
1146 di->events.main_ovv = false;
1147 di->events.ac_wd_expired = false;
1148 break;
1149 default:
1150 break;
1151 }
1152 break;
1153
1154 case POWER_SUPPLY_TYPE_USB:
1155 switch (ret.intval) {
1156 case POWER_SUPPLY_HEALTH_UNSPEC_FAILURE:
1157 di->events.usbchargernotok = true;
1158 di->events.usb_thermal_prot = false;
1159 di->events.vbus_ovv = false;
1160 di->events.usb_wd_expired = false;
1161 break;
1162 case POWER_SUPPLY_HEALTH_DEAD:
1163 di->events.usb_wd_expired = true;
1164 di->events.usbchargernotok = false;
1165 di->events.usb_thermal_prot = false;
1166 di->events.vbus_ovv = false;
1167 break;
1168 case POWER_SUPPLY_HEALTH_COLD:
1169 case POWER_SUPPLY_HEALTH_OVERHEAT:
1170 di->events.usb_thermal_prot = true;
1171 di->events.usbchargernotok = false;
1172 di->events.vbus_ovv = false;
1173 di->events.usb_wd_expired = false;
1174 break;
1175 case POWER_SUPPLY_HEALTH_OVERVOLTAGE:
1176 di->events.vbus_ovv = true;
1177 di->events.usbchargernotok = false;
1178 di->events.usb_thermal_prot = false;
1179 di->events.usb_wd_expired = false;
1180 break;
1181 case POWER_SUPPLY_HEALTH_GOOD:
1182 di->events.usbchargernotok = false;
1183 di->events.usb_thermal_prot = false;
1184 di->events.vbus_ovv = false;
1185 di->events.usb_wd_expired = false;
1186 break;
1187 default:
1188 break;
1189 }
1190 default:
1191 break;
1192 }
1193 break;
1194
1195 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +01001196 switch (ext->desc->type) {
Arun Murthy1668f812012-02-29 21:54:25 +05301197 case POWER_SUPPLY_TYPE_BATTERY:
1198 di->batt_data.volt = ret.intval / 1000;
1199 break;
1200 case POWER_SUPPLY_TYPE_MAINS:
1201 di->chg_info.ac_volt = ret.intval / 1000;
1202 break;
1203 case POWER_SUPPLY_TYPE_USB:
1204 di->chg_info.usb_volt = ret.intval / 1000;
1205 break;
1206 default:
1207 break;
1208 }
1209 break;
1210
1211 case POWER_SUPPLY_PROP_VOLTAGE_AVG:
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +01001212 switch (ext->desc->type) {
Arun Murthy1668f812012-02-29 21:54:25 +05301213 case POWER_SUPPLY_TYPE_MAINS:
1214 /* AVG is used to indicate when we are
1215 * in CV mode */
1216 if (ret.intval)
1217 di->events.ac_cv_active = true;
1218 else
1219 di->events.ac_cv_active = false;
1220
1221 break;
1222 case POWER_SUPPLY_TYPE_USB:
1223 /* AVG is used to indicate when we are
1224 * in CV mode */
1225 if (ret.intval)
1226 di->events.usb_cv_active = true;
1227 else
1228 di->events.usb_cv_active = false;
1229
1230 break;
1231 default:
1232 break;
1233 }
1234 break;
1235
1236 case POWER_SUPPLY_PROP_TECHNOLOGY:
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +01001237 switch (ext->desc->type) {
Arun Murthy1668f812012-02-29 21:54:25 +05301238 case POWER_SUPPLY_TYPE_BATTERY:
1239 if (ret.intval)
1240 di->events.batt_unknown = false;
1241 else
1242 di->events.batt_unknown = true;
1243
1244 break;
1245 default:
1246 break;
1247 }
1248 break;
1249
1250 case POWER_SUPPLY_PROP_TEMP:
1251 di->batt_data.temp = ret.intval / 10;
1252 break;
1253
1254 case POWER_SUPPLY_PROP_CURRENT_NOW:
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +01001255 switch (ext->desc->type) {
Arun Murthy1668f812012-02-29 21:54:25 +05301256 case POWER_SUPPLY_TYPE_MAINS:
1257 di->chg_info.ac_curr =
1258 ret.intval / 1000;
1259 break;
1260 case POWER_SUPPLY_TYPE_USB:
1261 di->chg_info.usb_curr =
1262 ret.intval / 1000;
1263 break;
1264 case POWER_SUPPLY_TYPE_BATTERY:
1265 di->batt_data.inst_curr = ret.intval / 1000;
1266 break;
1267 default:
1268 break;
1269 }
1270 break;
1271
1272 case POWER_SUPPLY_PROP_CURRENT_AVG:
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +01001273 switch (ext->desc->type) {
Arun Murthy1668f812012-02-29 21:54:25 +05301274 case POWER_SUPPLY_TYPE_BATTERY:
1275 di->batt_data.avg_curr = ret.intval / 1000;
1276 break;
1277 case POWER_SUPPLY_TYPE_USB:
1278 if (ret.intval)
1279 di->events.vbus_collapsed = true;
1280 else
1281 di->events.vbus_collapsed = false;
1282 break;
1283 default:
1284 break;
1285 }
1286 break;
1287 case POWER_SUPPLY_PROP_CAPACITY:
Marcus Cooperea402402013-01-11 13:12:54 +00001288 if (!capacity_updated)
1289 di->batt_data.percent = ret.intval;
Arun Murthy1668f812012-02-29 21:54:25 +05301290 break;
1291 default:
1292 break;
1293 }
1294 }
1295 return 0;
1296}
1297
1298/**
1299 * abx500_chargalg_external_power_changed() - callback for power supply changes
1300 * @psy: pointer to the structure power_supply
1301 *
1302 * This function is the entry point of the pointer external_power_changed
1303 * of the structure power_supply.
1304 * This function gets executed when there is a change in any external power
1305 * supply that this driver needs to be notified of.
1306 */
1307static void abx500_chargalg_external_power_changed(struct power_supply *psy)
1308{
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +01001309 struct abx500_chargalg *di = power_supply_get_drvdata(psy);
Arun Murthy1668f812012-02-29 21:54:25 +05301310
1311 /*
1312 * Trigger execution of the algorithm instantly and read
1313 * all power_supply properties there instead
1314 */
1315 queue_work(di->chargalg_wq, &di->chargalg_work);
1316}
1317
1318/**
1319 * abx500_chargalg_algorithm() - Main function for the algorithm
1320 * @di: pointer to the abx500_chargalg structure
1321 *
1322 * This is the main control function for the charging algorithm.
1323 * It is called periodically or when something happens that will
1324 * trigger a state change
1325 */
1326static void abx500_chargalg_algorithm(struct abx500_chargalg *di)
1327{
1328 int charger_status;
Lee Jones4dcdf572013-02-14 09:24:10 +00001329 int ret;
Lee Jones4d3b4aa2013-02-15 10:58:58 +00001330 int curr_step_lvl;
Arun Murthy1668f812012-02-29 21:54:25 +05301331
1332 /* Collect data from all power_supply class devices */
1333 class_for_each_device(power_supply_class, NULL,
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +01001334 di->chargalg_psy, abx500_chargalg_get_ext_psy_data);
Arun Murthy1668f812012-02-29 21:54:25 +05301335
1336 abx500_chargalg_end_of_charge(di);
1337 abx500_chargalg_check_temp(di);
1338 abx500_chargalg_check_charger_voltage(di);
1339
1340 charger_status = abx500_chargalg_check_charger_connection(di);
Lee Jones4d3b4aa2013-02-15 10:58:58 +00001341 abx500_chargalg_check_current_step_status(di);
Lee Jones4dcdf572013-02-14 09:24:10 +00001342
1343 if (is_ab8500(di->parent)) {
1344 ret = abx500_chargalg_check_charger_enable(di);
1345 if (ret < 0)
1346 dev_err(di->dev, "Checking charger is enabled error"
1347 ": Returned Value %d\n", ret);
1348 }
1349
Arun Murthy1668f812012-02-29 21:54:25 +05301350 /*
1351 * First check if we have a charger connected.
1352 * Also we don't allow charging of unknown batteries if configured
1353 * this way
1354 */
1355 if (!charger_status ||
Lee Jonesb0284de2012-11-30 10:09:42 +00001356 (di->events.batt_unknown && !di->bm->chg_unknown_bat)) {
Arun Murthy1668f812012-02-29 21:54:25 +05301357 if (di->charge_state != STATE_HANDHELD) {
1358 di->events.safety_timer_expired = false;
1359 abx500_chargalg_state_to(di, STATE_HANDHELD_INIT);
1360 }
1361 }
1362
1363 /* If suspended, we should not continue checking the flags */
1364 else if (di->charge_state == STATE_SUSPENDED_INIT ||
1365 di->charge_state == STATE_SUSPENDED) {
1366 /* We don't do anything here, just don,t continue */
1367 }
1368
1369 /* Safety timer expiration */
1370 else if (di->events.safety_timer_expired) {
1371 if (di->charge_state != STATE_SAFETY_TIMER_EXPIRED)
1372 abx500_chargalg_state_to(di,
1373 STATE_SAFETY_TIMER_EXPIRED_INIT);
1374 }
1375 /*
1376 * Check if any interrupts has occured
1377 * that will prevent us from charging
1378 */
1379
1380 /* Battery removed */
1381 else if (di->events.batt_rem) {
1382 if (di->charge_state != STATE_BATT_REMOVED)
1383 abx500_chargalg_state_to(di, STATE_BATT_REMOVED_INIT);
1384 }
1385 /* Main or USB charger not ok. */
1386 else if (di->events.mainextchnotok || di->events.usbchargernotok) {
1387 /*
1388 * If vbus_collapsed is set, we have to lower the charger
1389 * current, which is done in the normal state below
1390 */
1391 if (di->charge_state != STATE_CHG_NOT_OK &&
1392 !di->events.vbus_collapsed)
1393 abx500_chargalg_state_to(di, STATE_CHG_NOT_OK_INIT);
1394 }
1395 /* VBUS, Main or VBAT OVV. */
1396 else if (di->events.vbus_ovv ||
1397 di->events.main_ovv ||
1398 di->events.batt_ovv ||
1399 !di->chg_info.usb_chg_ok ||
1400 !di->chg_info.ac_chg_ok) {
1401 if (di->charge_state != STATE_OVV_PROTECT)
1402 abx500_chargalg_state_to(di, STATE_OVV_PROTECT_INIT);
1403 }
1404 /* USB Thermal, stop charging */
1405 else if (di->events.main_thermal_prot ||
1406 di->events.usb_thermal_prot) {
1407 if (di->charge_state != STATE_HW_TEMP_PROTECT)
1408 abx500_chargalg_state_to(di,
1409 STATE_HW_TEMP_PROTECT_INIT);
1410 }
1411 /* Battery temp over/under */
1412 else if (di->events.btemp_underover) {
1413 if (di->charge_state != STATE_TEMP_UNDEROVER)
1414 abx500_chargalg_state_to(di,
1415 STATE_TEMP_UNDEROVER_INIT);
1416 }
1417 /* Watchdog expired */
1418 else if (di->events.ac_wd_expired ||
1419 di->events.usb_wd_expired) {
1420 if (di->charge_state != STATE_WD_EXPIRED)
1421 abx500_chargalg_state_to(di, STATE_WD_EXPIRED_INIT);
1422 }
1423 /* Battery temp high/low */
1424 else if (di->events.btemp_lowhigh) {
1425 if (di->charge_state != STATE_TEMP_LOWHIGH)
1426 abx500_chargalg_state_to(di, STATE_TEMP_LOWHIGH_INIT);
1427 }
1428
1429 dev_dbg(di->dev,
1430 "[CHARGALG] Vb %d Ib_avg %d Ib_inst %d Tb %d Cap %d Maint %d "
1431 "State %s Active_chg %d Chg_status %d AC %d USB %d "
1432 "AC_online %d USB_online %d AC_CV %d USB_CV %d AC_I %d "
1433 "USB_I %d AC_Vset %d AC_Iset %d USB_Vset %d USB_Iset %d\n",
1434 di->batt_data.volt,
1435 di->batt_data.avg_curr,
1436 di->batt_data.inst_curr,
1437 di->batt_data.temp,
1438 di->batt_data.percent,
1439 di->maintenance_chg,
1440 states[di->charge_state],
1441 di->chg_info.charger_type,
1442 di->charge_status,
1443 di->chg_info.conn_chg & AC_CHG,
1444 di->chg_info.conn_chg & USB_CHG,
1445 di->chg_info.online_chg & AC_CHG,
1446 di->chg_info.online_chg & USB_CHG,
1447 di->events.ac_cv_active,
1448 di->events.usb_cv_active,
1449 di->chg_info.ac_curr,
1450 di->chg_info.usb_curr,
1451 di->chg_info.ac_vset,
1452 di->chg_info.ac_iset,
1453 di->chg_info.usb_vset,
1454 di->chg_info.usb_iset);
1455
1456 switch (di->charge_state) {
1457 case STATE_HANDHELD_INIT:
1458 abx500_chargalg_stop_charging(di);
1459 di->charge_status = POWER_SUPPLY_STATUS_DISCHARGING;
1460 abx500_chargalg_state_to(di, STATE_HANDHELD);
1461 /* Intentional fallthrough */
1462
1463 case STATE_HANDHELD:
1464 break;
1465
1466 case STATE_SUSPENDED_INIT:
1467 if (di->susp_status.ac_suspended)
1468 abx500_chargalg_ac_en(di, false, 0, 0);
1469 if (di->susp_status.usb_suspended)
1470 abx500_chargalg_usb_en(di, false, 0, 0);
1471 abx500_chargalg_stop_safety_timer(di);
1472 abx500_chargalg_stop_maintenance_timer(di);
1473 di->charge_status = POWER_SUPPLY_STATUS_NOT_CHARGING;
1474 di->maintenance_chg = false;
1475 abx500_chargalg_state_to(di, STATE_SUSPENDED);
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +01001476 power_supply_changed(di->chargalg_psy);
Arun Murthy1668f812012-02-29 21:54:25 +05301477 /* Intentional fallthrough */
1478
1479 case STATE_SUSPENDED:
1480 /* CHARGING is suspended */
1481 break;
1482
1483 case STATE_BATT_REMOVED_INIT:
1484 abx500_chargalg_stop_charging(di);
1485 abx500_chargalg_state_to(di, STATE_BATT_REMOVED);
1486 /* Intentional fallthrough */
1487
1488 case STATE_BATT_REMOVED:
1489 if (!di->events.batt_rem)
1490 abx500_chargalg_state_to(di, STATE_NORMAL_INIT);
1491 break;
1492
1493 case STATE_HW_TEMP_PROTECT_INIT:
1494 abx500_chargalg_stop_charging(di);
1495 abx500_chargalg_state_to(di, STATE_HW_TEMP_PROTECT);
1496 /* Intentional fallthrough */
1497
1498 case STATE_HW_TEMP_PROTECT:
1499 if (!di->events.main_thermal_prot &&
1500 !di->events.usb_thermal_prot)
1501 abx500_chargalg_state_to(di, STATE_NORMAL_INIT);
1502 break;
1503
1504 case STATE_OVV_PROTECT_INIT:
1505 abx500_chargalg_stop_charging(di);
1506 abx500_chargalg_state_to(di, STATE_OVV_PROTECT);
1507 /* Intentional fallthrough */
1508
1509 case STATE_OVV_PROTECT:
1510 if (!di->events.vbus_ovv &&
1511 !di->events.main_ovv &&
1512 !di->events.batt_ovv &&
1513 di->chg_info.usb_chg_ok &&
1514 di->chg_info.ac_chg_ok)
1515 abx500_chargalg_state_to(di, STATE_NORMAL_INIT);
1516 break;
1517
1518 case STATE_CHG_NOT_OK_INIT:
1519 abx500_chargalg_stop_charging(di);
1520 abx500_chargalg_state_to(di, STATE_CHG_NOT_OK);
1521 /* Intentional fallthrough */
1522
1523 case STATE_CHG_NOT_OK:
1524 if (!di->events.mainextchnotok &&
1525 !di->events.usbchargernotok)
1526 abx500_chargalg_state_to(di, STATE_NORMAL_INIT);
1527 break;
1528
1529 case STATE_SAFETY_TIMER_EXPIRED_INIT:
1530 abx500_chargalg_stop_charging(di);
1531 abx500_chargalg_state_to(di, STATE_SAFETY_TIMER_EXPIRED);
1532 /* Intentional fallthrough */
1533
1534 case STATE_SAFETY_TIMER_EXPIRED:
1535 /* We exit this state when charger is removed */
1536 break;
1537
1538 case STATE_NORMAL_INIT:
Lee Jonesdb43e6c2013-02-14 12:39:15 +00001539 if ((di->chg_info.charger_type & USB_CHG) &&
1540 di->usb_chg->power_path) {
1541 if (di->batt_data.volt >
1542 (di->bm->fg_params->lowbat_threshold +
1543 BAT_PLUS_MARGIN)) {
1544 ab8540_chargalg_usb_pre_chg_en(di, false);
1545 ab8540_chargalg_usb_pp_en(di, false);
1546 } else {
1547 ab8540_chargalg_usb_pp_en(di, true);
1548 ab8540_chargalg_usb_pre_chg_en(di, true);
1549 abx500_chargalg_state_to(di,
1550 STATE_USB_PP_PRE_CHARGE);
1551 break;
1552 }
1553 }
1554
Lee Jones4d3b4aa2013-02-15 10:58:58 +00001555 if (di->curr_status.curr_step == CHARGALG_CURR_STEP_LOW)
1556 abx500_chargalg_stop_charging(di);
1557 else {
1558 curr_step_lvl = di->bm->bat_type[
1559 di->bm->batt_id].normal_cur_lvl
1560 * di->curr_status.curr_step
1561 / CHARGALG_CURR_STEP_HIGH;
1562 abx500_chargalg_start_charging(di,
1563 di->bm->bat_type[di->bm->batt_id]
1564 .normal_vol_lvl, curr_step_lvl);
1565 }
1566
Arun Murthy1668f812012-02-29 21:54:25 +05301567 abx500_chargalg_state_to(di, STATE_NORMAL);
1568 abx500_chargalg_start_safety_timer(di);
1569 abx500_chargalg_stop_maintenance_timer(di);
1570 init_maxim_chg_curr(di);
1571 di->charge_status = POWER_SUPPLY_STATUS_CHARGING;
1572 di->eoc_cnt = 0;
1573 di->maintenance_chg = false;
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +01001574 power_supply_changed(di->chargalg_psy);
Arun Murthy1668f812012-02-29 21:54:25 +05301575
1576 break;
1577
Lee Jonesdb43e6c2013-02-14 12:39:15 +00001578 case STATE_USB_PP_PRE_CHARGE:
1579 if (di->batt_data.volt >
1580 (di->bm->fg_params->lowbat_threshold +
1581 BAT_PLUS_MARGIN))
1582 abx500_chargalg_state_to(di, STATE_NORMAL_INIT);
1583 break;
1584
Arun Murthy1668f812012-02-29 21:54:25 +05301585 case STATE_NORMAL:
1586 handle_maxim_chg_curr(di);
1587 if (di->charge_status == POWER_SUPPLY_STATUS_FULL &&
1588 di->maintenance_chg) {
Lee Jonesb0284de2012-11-30 10:09:42 +00001589 if (di->bm->no_maintenance)
Arun Murthy1668f812012-02-29 21:54:25 +05301590 abx500_chargalg_state_to(di,
1591 STATE_WAIT_FOR_RECHARGE_INIT);
1592 else
1593 abx500_chargalg_state_to(di,
1594 STATE_MAINTENANCE_A_INIT);
1595 }
1596 break;
1597
1598 /* This state will be used when the maintenance state is disabled */
1599 case STATE_WAIT_FOR_RECHARGE_INIT:
1600 abx500_chargalg_hold_charging(di);
1601 abx500_chargalg_state_to(di, STATE_WAIT_FOR_RECHARGE);
Arun Murthy1668f812012-02-29 21:54:25 +05301602 /* Intentional fallthrough */
1603
1604 case STATE_WAIT_FOR_RECHARGE:
Marcus Cooperea402402013-01-11 13:12:54 +00001605 if (di->batt_data.percent <=
1606 di->bm->bat_type[di->bm->batt_id].
1607 recharge_cap)
1608 abx500_chargalg_state_to(di, STATE_NORMAL_INIT);
Arun Murthy1668f812012-02-29 21:54:25 +05301609 break;
1610
1611 case STATE_MAINTENANCE_A_INIT:
1612 abx500_chargalg_stop_safety_timer(di);
1613 abx500_chargalg_start_maintenance_timer(di,
Lee Jonesb0284de2012-11-30 10:09:42 +00001614 di->bm->bat_type[
1615 di->bm->batt_id].maint_a_chg_timer_h);
Arun Murthy1668f812012-02-29 21:54:25 +05301616 abx500_chargalg_start_charging(di,
Lee Jonesb0284de2012-11-30 10:09:42 +00001617 di->bm->bat_type[
1618 di->bm->batt_id].maint_a_vol_lvl,
1619 di->bm->bat_type[
1620 di->bm->batt_id].maint_a_cur_lvl);
Arun Murthy1668f812012-02-29 21:54:25 +05301621 abx500_chargalg_state_to(di, STATE_MAINTENANCE_A);
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +01001622 power_supply_changed(di->chargalg_psy);
Arun Murthy1668f812012-02-29 21:54:25 +05301623 /* Intentional fallthrough*/
1624
1625 case STATE_MAINTENANCE_A:
1626 if (di->events.maintenance_timer_expired) {
1627 abx500_chargalg_stop_maintenance_timer(di);
1628 abx500_chargalg_state_to(di, STATE_MAINTENANCE_B_INIT);
1629 }
1630 break;
1631
1632 case STATE_MAINTENANCE_B_INIT:
1633 abx500_chargalg_start_maintenance_timer(di,
Lee Jonesb0284de2012-11-30 10:09:42 +00001634 di->bm->bat_type[
1635 di->bm->batt_id].maint_b_chg_timer_h);
Arun Murthy1668f812012-02-29 21:54:25 +05301636 abx500_chargalg_start_charging(di,
Lee Jonesb0284de2012-11-30 10:09:42 +00001637 di->bm->bat_type[
1638 di->bm->batt_id].maint_b_vol_lvl,
1639 di->bm->bat_type[
1640 di->bm->batt_id].maint_b_cur_lvl);
Arun Murthy1668f812012-02-29 21:54:25 +05301641 abx500_chargalg_state_to(di, STATE_MAINTENANCE_B);
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +01001642 power_supply_changed(di->chargalg_psy);
Arun Murthy1668f812012-02-29 21:54:25 +05301643 /* Intentional fallthrough*/
1644
1645 case STATE_MAINTENANCE_B:
1646 if (di->events.maintenance_timer_expired) {
1647 abx500_chargalg_stop_maintenance_timer(di);
1648 abx500_chargalg_state_to(di, STATE_NORMAL_INIT);
1649 }
1650 break;
1651
1652 case STATE_TEMP_LOWHIGH_INIT:
1653 abx500_chargalg_start_charging(di,
Lee Jonesb0284de2012-11-30 10:09:42 +00001654 di->bm->bat_type[
1655 di->bm->batt_id].low_high_vol_lvl,
1656 di->bm->bat_type[
1657 di->bm->batt_id].low_high_cur_lvl);
Arun Murthy1668f812012-02-29 21:54:25 +05301658 abx500_chargalg_stop_maintenance_timer(di);
1659 di->charge_status = POWER_SUPPLY_STATUS_CHARGING;
1660 abx500_chargalg_state_to(di, STATE_TEMP_LOWHIGH);
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +01001661 power_supply_changed(di->chargalg_psy);
Arun Murthy1668f812012-02-29 21:54:25 +05301662 /* Intentional fallthrough */
1663
1664 case STATE_TEMP_LOWHIGH:
1665 if (!di->events.btemp_lowhigh)
1666 abx500_chargalg_state_to(di, STATE_NORMAL_INIT);
1667 break;
1668
1669 case STATE_WD_EXPIRED_INIT:
1670 abx500_chargalg_stop_charging(di);
1671 abx500_chargalg_state_to(di, STATE_WD_EXPIRED);
1672 /* Intentional fallthrough */
1673
1674 case STATE_WD_EXPIRED:
1675 if (!di->events.ac_wd_expired &&
1676 !di->events.usb_wd_expired)
1677 abx500_chargalg_state_to(di, STATE_NORMAL_INIT);
1678 break;
1679
1680 case STATE_TEMP_UNDEROVER_INIT:
1681 abx500_chargalg_stop_charging(di);
1682 abx500_chargalg_state_to(di, STATE_TEMP_UNDEROVER);
1683 /* Intentional fallthrough */
1684
1685 case STATE_TEMP_UNDEROVER:
1686 if (!di->events.btemp_underover)
1687 abx500_chargalg_state_to(di, STATE_NORMAL_INIT);
1688 break;
1689 }
1690
1691 /* Start charging directly if the new state is a charge state */
1692 if (di->charge_state == STATE_NORMAL_INIT ||
1693 di->charge_state == STATE_MAINTENANCE_A_INIT ||
1694 di->charge_state == STATE_MAINTENANCE_B_INIT)
1695 queue_work(di->chargalg_wq, &di->chargalg_work);
1696}
1697
1698/**
1699 * abx500_chargalg_periodic_work() - Periodic work for the algorithm
1700 * @work: pointer to the work_struct structure
1701 *
1702 * Work queue function for the charging algorithm
1703 */
1704static void abx500_chargalg_periodic_work(struct work_struct *work)
1705{
1706 struct abx500_chargalg *di = container_of(work,
1707 struct abx500_chargalg, chargalg_periodic_work.work);
1708
1709 abx500_chargalg_algorithm(di);
1710
1711 /*
1712 * If a charger is connected then the battery has to be monitored
1713 * frequently, else the work can be delayed.
1714 */
1715 if (di->chg_info.conn_chg)
1716 queue_delayed_work(di->chargalg_wq,
1717 &di->chargalg_periodic_work,
Lee Jonesb0284de2012-11-30 10:09:42 +00001718 di->bm->interval_charging * HZ);
Arun Murthy1668f812012-02-29 21:54:25 +05301719 else
1720 queue_delayed_work(di->chargalg_wq,
1721 &di->chargalg_periodic_work,
Lee Jonesb0284de2012-11-30 10:09:42 +00001722 di->bm->interval_not_charging * HZ);
Arun Murthy1668f812012-02-29 21:54:25 +05301723}
1724
1725/**
1726 * abx500_chargalg_wd_work() - periodic work to kick the charger watchdog
1727 * @work: pointer to the work_struct structure
1728 *
1729 * Work queue function for kicking the charger watchdog
1730 */
1731static void abx500_chargalg_wd_work(struct work_struct *work)
1732{
1733 int ret;
1734 struct abx500_chargalg *di = container_of(work,
1735 struct abx500_chargalg, chargalg_wd_work.work);
1736
1737 dev_dbg(di->dev, "abx500_chargalg_wd_work\n");
1738
1739 ret = abx500_chargalg_kick_watchdog(di);
1740 if (ret < 0)
1741 dev_err(di->dev, "failed to kick watchdog\n");
1742
1743 queue_delayed_work(di->chargalg_wq,
1744 &di->chargalg_wd_work, CHG_WD_INTERVAL);
1745}
1746
1747/**
1748 * abx500_chargalg_work() - Work to run the charging algorithm instantly
1749 * @work: pointer to the work_struct structure
1750 *
1751 * Work queue function for calling the charging algorithm
1752 */
1753static void abx500_chargalg_work(struct work_struct *work)
1754{
1755 struct abx500_chargalg *di = container_of(work,
1756 struct abx500_chargalg, chargalg_work);
1757
1758 abx500_chargalg_algorithm(di);
1759}
1760
1761/**
1762 * abx500_chargalg_get_property() - get the chargalg properties
1763 * @psy: pointer to the power_supply structure
1764 * @psp: pointer to the power_supply_property structure
1765 * @val: pointer to the power_supply_propval union
1766 *
1767 * This function gets called when an application tries to get the
1768 * chargalg properties by reading the sysfs files.
1769 * status: charging/discharging/full/unknown
1770 * health: health of the battery
1771 * Returns error code in case of failure else 0 on success
1772 */
1773static int abx500_chargalg_get_property(struct power_supply *psy,
1774 enum power_supply_property psp,
1775 union power_supply_propval *val)
1776{
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +01001777 struct abx500_chargalg *di = power_supply_get_drvdata(psy);
Arun Murthy1668f812012-02-29 21:54:25 +05301778
1779 switch (psp) {
1780 case POWER_SUPPLY_PROP_STATUS:
1781 val->intval = di->charge_status;
1782 break;
1783 case POWER_SUPPLY_PROP_HEALTH:
1784 if (di->events.batt_ovv) {
1785 val->intval = POWER_SUPPLY_HEALTH_OVERVOLTAGE;
1786 } else if (di->events.btemp_underover) {
Lee Jonesb0284de2012-11-30 10:09:42 +00001787 if (di->batt_data.temp <= di->bm->temp_under)
Arun Murthy1668f812012-02-29 21:54:25 +05301788 val->intval = POWER_SUPPLY_HEALTH_COLD;
1789 else
1790 val->intval = POWER_SUPPLY_HEALTH_OVERHEAT;
Lee Jonesd80108f2013-01-17 13:49:45 +00001791 } else if (di->charge_state == STATE_SAFETY_TIMER_EXPIRED ||
1792 di->charge_state == STATE_SAFETY_TIMER_EXPIRED_INIT) {
1793 val->intval = POWER_SUPPLY_HEALTH_UNSPEC_FAILURE;
Arun Murthy1668f812012-02-29 21:54:25 +05301794 } else {
1795 val->intval = POWER_SUPPLY_HEALTH_GOOD;
1796 }
1797 break;
1798 default:
1799 return -EINVAL;
1800 }
1801 return 0;
1802}
1803
1804/* Exposure to the sysfs interface */
1805
Lee Jones4d3b4aa2013-02-15 10:58:58 +00001806static ssize_t abx500_chargalg_curr_step_show(struct abx500_chargalg *di,
1807 char *buf)
Lee Jonesc9ade0f2013-01-21 11:22:56 +00001808{
Lee Jones4d3b4aa2013-02-15 10:58:58 +00001809 return sprintf(buf, "%d\n", di->curr_status.curr_step);
1810}
Lee Jonesc9ade0f2013-01-21 11:22:56 +00001811
Lee Jones4d3b4aa2013-02-15 10:58:58 +00001812static ssize_t abx500_chargalg_curr_step_store(struct abx500_chargalg *di,
1813 const char *buf, size_t length)
1814{
1815 long int param;
1816 int ret;
1817
1818 ret = kstrtol(buf, 10, &param);
1819 if (ret < 0)
1820 return ret;
1821
1822 di->curr_status.curr_step = param;
1823 if (di->curr_status.curr_step >= CHARGALG_CURR_STEP_LOW &&
1824 di->curr_status.curr_step <= CHARGALG_CURR_STEP_HIGH) {
1825 di->curr_status.curr_step_change = true;
1826 queue_work(di->chargalg_wq, &di->chargalg_work);
1827 } else
1828 dev_info(di->dev, "Wrong current step\n"
1829 "Enter 0. Disable AC/USB Charging\n"
1830 "1--100. Set AC/USB charging current step\n"
1831 "100. Enable AC/USB Charging\n");
1832
1833 return strlen(buf);
1834}
1835
1836
1837static ssize_t abx500_chargalg_en_show(struct abx500_chargalg *di,
1838 char *buf)
1839{
Lee Jonesc9ade0f2013-01-21 11:22:56 +00001840 return sprintf(buf, "%d\n",
1841 di->susp_status.ac_suspended &&
1842 di->susp_status.usb_suspended);
1843}
1844
Lee Jones4d3b4aa2013-02-15 10:58:58 +00001845static ssize_t abx500_chargalg_en_store(struct abx500_chargalg *di,
1846 const char *buf, size_t length)
Arun Murthy1668f812012-02-29 21:54:25 +05301847{
Arun Murthy1668f812012-02-29 21:54:25 +05301848 long int param;
1849 int ac_usb;
1850 int ret;
Arun Murthy1668f812012-02-29 21:54:25 +05301851
Lee Jones4d3b4aa2013-02-15 10:58:58 +00001852 ret = kstrtol(buf, 10, &param);
1853 if (ret < 0)
1854 return ret;
Arun Murthy1668f812012-02-29 21:54:25 +05301855
Lee Jones4d3b4aa2013-02-15 10:58:58 +00001856 ac_usb = param;
1857 switch (ac_usb) {
1858 case 0:
1859 /* Disable charging */
1860 di->susp_status.ac_suspended = true;
1861 di->susp_status.usb_suspended = true;
1862 di->susp_status.suspended_change = true;
1863 /* Trigger a state change */
1864 queue_work(di->chargalg_wq,
1865 &di->chargalg_work);
Arun Murthy1668f812012-02-29 21:54:25 +05301866 break;
Lee Jones4d3b4aa2013-02-15 10:58:58 +00001867 case 1:
1868 /* Enable AC Charging */
1869 di->susp_status.ac_suspended = false;
1870 di->susp_status.suspended_change = true;
1871 /* Trigger a state change */
1872 queue_work(di->chargalg_wq,
1873 &di->chargalg_work);
1874 break;
1875 case 2:
1876 /* Enable USB charging */
1877 di->susp_status.usb_suspended = false;
1878 di->susp_status.suspended_change = true;
1879 /* Trigger a state change */
1880 queue_work(di->chargalg_wq,
1881 &di->chargalg_work);
1882 break;
1883 default:
1884 dev_info(di->dev, "Wrong input\n"
1885 "Enter 0. Disable AC/USB Charging\n"
1886 "1. Enable AC charging\n"
1887 "2. Enable USB Charging\n");
Arun Murthy1668f812012-02-29 21:54:25 +05301888 };
1889 return strlen(buf);
1890}
1891
Lee Jones4d3b4aa2013-02-15 10:58:58 +00001892static struct abx500_chargalg_sysfs_entry abx500_chargalg_en_charger =
1893 __ATTR(chargalg, 0644, abx500_chargalg_en_show,
1894 abx500_chargalg_en_store);
1895
1896static struct abx500_chargalg_sysfs_entry abx500_chargalg_curr_step =
1897 __ATTR(chargalg_curr_step, 0644, abx500_chargalg_curr_step_show,
1898 abx500_chargalg_curr_step_store);
1899
1900static ssize_t abx500_chargalg_sysfs_show(struct kobject *kobj,
1901 struct attribute *attr, char *buf)
Arun Murthy1668f812012-02-29 21:54:25 +05301902{
Lee Jones4d3b4aa2013-02-15 10:58:58 +00001903 struct abx500_chargalg_sysfs_entry *entry = container_of(attr,
1904 struct abx500_chargalg_sysfs_entry, attr);
1905
1906 struct abx500_chargalg *di = container_of(kobj,
1907 struct abx500_chargalg, chargalg_kobject);
1908
1909 if (!entry->show)
1910 return -EIO;
1911
1912 return entry->show(di, buf);
1913}
1914
1915static ssize_t abx500_chargalg_sysfs_charger(struct kobject *kobj,
1916 struct attribute *attr, const char *buf, size_t length)
1917{
1918 struct abx500_chargalg_sysfs_entry *entry = container_of(attr,
1919 struct abx500_chargalg_sysfs_entry, attr);
1920
1921 struct abx500_chargalg *di = container_of(kobj,
1922 struct abx500_chargalg, chargalg_kobject);
1923
1924 if (!entry->store)
1925 return -EIO;
1926
1927 return entry->store(di, buf, length);
1928}
Arun Murthy1668f812012-02-29 21:54:25 +05301929
1930static struct attribute *abx500_chargalg_chg[] = {
Lee Jones4d3b4aa2013-02-15 10:58:58 +00001931 &abx500_chargalg_en_charger.attr,
1932 &abx500_chargalg_curr_step.attr,
1933 NULL,
Arun Murthy1668f812012-02-29 21:54:25 +05301934};
1935
Anton Vorontsov64eb9b02012-03-14 04:43:11 +04001936static const struct sysfs_ops abx500_chargalg_sysfs_ops = {
Lee Jonesc9ade0f2013-01-21 11:22:56 +00001937 .show = abx500_chargalg_sysfs_show,
Arun Murthy1668f812012-02-29 21:54:25 +05301938 .store = abx500_chargalg_sysfs_charger,
1939};
1940
1941static struct kobj_type abx500_chargalg_ktype = {
1942 .sysfs_ops = &abx500_chargalg_sysfs_ops,
1943 .default_attrs = abx500_chargalg_chg,
1944};
1945
1946/**
1947 * abx500_chargalg_sysfs_exit() - de-init of sysfs entry
1948 * @di: pointer to the struct abx500_chargalg
1949 *
1950 * This function removes the entry in sysfs.
1951 */
1952static void abx500_chargalg_sysfs_exit(struct abx500_chargalg *di)
1953{
1954 kobject_del(&di->chargalg_kobject);
1955}
1956
1957/**
1958 * abx500_chargalg_sysfs_init() - init of sysfs entry
1959 * @di: pointer to the struct abx500_chargalg
1960 *
1961 * This function adds an entry in sysfs.
1962 * Returns error code in case of failure else 0(on success)
1963 */
1964static int abx500_chargalg_sysfs_init(struct abx500_chargalg *di)
1965{
1966 int ret = 0;
1967
1968 ret = kobject_init_and_add(&di->chargalg_kobject,
1969 &abx500_chargalg_ktype,
1970 NULL, "abx500_chargalg");
1971 if (ret < 0)
1972 dev_err(di->dev, "failed to create sysfs entry\n");
1973
1974 return ret;
1975}
1976/* Exposure to the sysfs interface <<END>> */
1977
1978#if defined(CONFIG_PM)
1979static int abx500_chargalg_resume(struct platform_device *pdev)
1980{
1981 struct abx500_chargalg *di = platform_get_drvdata(pdev);
1982
1983 /* Kick charger watchdog if charging (any charger online) */
1984 if (di->chg_info.online_chg)
1985 queue_delayed_work(di->chargalg_wq, &di->chargalg_wd_work, 0);
1986
1987 /*
1988 * Run the charging algorithm directly to be sure we don't
1989 * do it too seldom
1990 */
1991 queue_delayed_work(di->chargalg_wq, &di->chargalg_periodic_work, 0);
1992
1993 return 0;
1994}
1995
1996static int abx500_chargalg_suspend(struct platform_device *pdev,
1997 pm_message_t state)
1998{
1999 struct abx500_chargalg *di = platform_get_drvdata(pdev);
2000
2001 if (di->chg_info.online_chg)
2002 cancel_delayed_work_sync(&di->chargalg_wd_work);
2003
2004 cancel_delayed_work_sync(&di->chargalg_periodic_work);
2005
2006 return 0;
2007}
2008#else
2009#define abx500_chargalg_suspend NULL
2010#define abx500_chargalg_resume NULL
2011#endif
2012
Bill Pemberton415ec692012-11-19 13:26:07 -05002013static int abx500_chargalg_remove(struct platform_device *pdev)
Arun Murthy1668f812012-02-29 21:54:25 +05302014{
2015 struct abx500_chargalg *di = platform_get_drvdata(pdev);
2016
2017 /* sysfs interface to enable/disbale charging from user space */
2018 abx500_chargalg_sysfs_exit(di);
2019
Lee Jones257107a2013-02-13 15:15:03 +00002020 hrtimer_cancel(&di->safety_timer);
2021 hrtimer_cancel(&di->maintenance_timer);
2022
2023 cancel_delayed_work_sync(&di->chargalg_periodic_work);
2024 cancel_delayed_work_sync(&di->chargalg_wd_work);
2025 cancel_work_sync(&di->chargalg_work);
2026
Arun Murthy1668f812012-02-29 21:54:25 +05302027 /* Delete the work queue */
2028 destroy_workqueue(di->chargalg_wq);
2029
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +01002030 power_supply_unregister(di->chargalg_psy);
Arun Murthy1668f812012-02-29 21:54:25 +05302031
2032 return 0;
2033}
2034
Rajanikanth H.Va12810a2012-10-31 15:40:33 +00002035static char *supply_interface[] = {
2036 "ab8500_fg",
2037};
2038
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +01002039static const struct power_supply_desc abx500_chargalg_desc = {
2040 .name = "abx500_chargalg",
2041 .type = POWER_SUPPLY_TYPE_BATTERY,
2042 .properties = abx500_chargalg_props,
2043 .num_properties = ARRAY_SIZE(abx500_chargalg_props),
2044 .get_property = abx500_chargalg_get_property,
2045 .external_power_changed = abx500_chargalg_external_power_changed,
2046};
2047
Bill Pembertonc8afa642012-11-19 13:22:23 -05002048static int abx500_chargalg_probe(struct platform_device *pdev)
Arun Murthy1668f812012-02-29 21:54:25 +05302049{
Rajanikanth H.Va12810a2012-10-31 15:40:33 +00002050 struct device_node *np = pdev->dev.of_node;
Lee Jonesbdc56b42012-11-30 10:57:14 +00002051 struct abx500_bm_data *plat = pdev->dev.platform_data;
Krzysztof Kozlowski2dc92152015-03-12 08:44:02 +01002052 struct power_supply_config psy_cfg = {};
Rajanikanth H.Va12810a2012-10-31 15:40:33 +00002053 struct abx500_chargalg *di;
Arun Murthy1668f812012-02-29 21:54:25 +05302054 int ret = 0;
2055
Rajanikanth H.Va12810a2012-10-31 15:40:33 +00002056 di = devm_kzalloc(&pdev->dev, sizeof(*di), GFP_KERNEL);
2057 if (!di) {
2058 dev_err(&pdev->dev, "%s no mem for ab8500_chargalg\n", __func__);
Arun Murthy1668f812012-02-29 21:54:25 +05302059 return -ENOMEM;
Rajanikanth H.Va12810a2012-10-31 15:40:33 +00002060 }
Lee Jonesbdc56b42012-11-30 10:57:14 +00002061
2062 if (!plat) {
2063 dev_err(&pdev->dev, "no battery management data supplied\n");
2064 return -EINVAL;
2065 }
2066 di->bm = plat;
2067
2068 if (np) {
2069 ret = ab8500_bm_of_probe(&pdev->dev, np, di->bm);
2070 if (ret) {
2071 dev_err(&pdev->dev, "failed to get battery information\n");
2072 return ret;
Rajanikanth H.Va12810a2012-10-31 15:40:33 +00002073 }
Rajanikanth H.Va12810a2012-10-31 15:40:33 +00002074 }
Arun Murthy1668f812012-02-29 21:54:25 +05302075
Lee Jones330b7eb2013-02-15 10:53:57 +00002076 /* get device struct and parent */
Arun Murthy1668f812012-02-29 21:54:25 +05302077 di->dev = &pdev->dev;
Lee Jones330b7eb2013-02-15 10:53:57 +00002078 di->parent = dev_get_drvdata(pdev->dev.parent);
Arun Murthy1668f812012-02-29 21:54:25 +05302079
Krzysztof Kozlowski2dc92152015-03-12 08:44:02 +01002080 psy_cfg.supplied_to = supply_interface;
2081 psy_cfg.num_supplicants = ARRAY_SIZE(supply_interface);
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +01002082 psy_cfg.drv_data = di;
Krzysztof Kozlowski2dc92152015-03-12 08:44:02 +01002083
Arun Murthy1668f812012-02-29 21:54:25 +05302084 /* Initilialize safety timer */
Lee Jones257107a2013-02-13 15:15:03 +00002085 hrtimer_init(&di->safety_timer, CLOCK_REALTIME, HRTIMER_MODE_ABS);
Arun Murthy1668f812012-02-29 21:54:25 +05302086 di->safety_timer.function = abx500_chargalg_safety_timer_expired;
Arun Murthy1668f812012-02-29 21:54:25 +05302087
2088 /* Initilialize maintenance timer */
Lee Jones257107a2013-02-13 15:15:03 +00002089 hrtimer_init(&di->maintenance_timer, CLOCK_REALTIME, HRTIMER_MODE_ABS);
Arun Murthy1668f812012-02-29 21:54:25 +05302090 di->maintenance_timer.function =
2091 abx500_chargalg_maintenance_timer_expired;
Arun Murthy1668f812012-02-29 21:54:25 +05302092
2093 /* Create a work queue for the chargalg */
2094 di->chargalg_wq =
2095 create_singlethread_workqueue("abx500_chargalg_wq");
2096 if (di->chargalg_wq == NULL) {
2097 dev_err(di->dev, "failed to create work queue\n");
Rajanikanth H.Va12810a2012-10-31 15:40:33 +00002098 return -ENOMEM;
Arun Murthy1668f812012-02-29 21:54:25 +05302099 }
2100
2101 /* Init work for chargalg */
Tejun Heo203b42f2012-08-21 13:18:23 -07002102 INIT_DEFERRABLE_WORK(&di->chargalg_periodic_work,
Arun Murthy1668f812012-02-29 21:54:25 +05302103 abx500_chargalg_periodic_work);
Tejun Heo203b42f2012-08-21 13:18:23 -07002104 INIT_DEFERRABLE_WORK(&di->chargalg_wd_work,
Arun Murthy1668f812012-02-29 21:54:25 +05302105 abx500_chargalg_wd_work);
2106
2107 /* Init work for chargalg */
2108 INIT_WORK(&di->chargalg_work, abx500_chargalg_work);
2109
2110 /* To detect charger at startup */
2111 di->chg_info.prev_conn_chg = -1;
2112
2113 /* Register chargalg power supply class */
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +01002114 di->chargalg_psy = power_supply_register(di->dev, &abx500_chargalg_desc,
2115 &psy_cfg);
2116 if (IS_ERR(di->chargalg_psy)) {
Arun Murthy1668f812012-02-29 21:54:25 +05302117 dev_err(di->dev, "failed to register chargalg psy\n");
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +01002118 ret = PTR_ERR(di->chargalg_psy);
Arun Murthy1668f812012-02-29 21:54:25 +05302119 goto free_chargalg_wq;
2120 }
2121
2122 platform_set_drvdata(pdev, di);
2123
2124 /* sysfs interface to enable/disable charging from user space */
2125 ret = abx500_chargalg_sysfs_init(di);
2126 if (ret) {
2127 dev_err(di->dev, "failed to create sysfs entry\n");
2128 goto free_psy;
2129 }
Lee Jones4d3b4aa2013-02-15 10:58:58 +00002130 di->curr_status.curr_step = CHARGALG_CURR_STEP_HIGH;
Arun Murthy1668f812012-02-29 21:54:25 +05302131
2132 /* Run the charging algorithm */
2133 queue_delayed_work(di->chargalg_wq, &di->chargalg_periodic_work, 0);
2134
2135 dev_info(di->dev, "probe success\n");
2136 return ret;
2137
2138free_psy:
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +01002139 power_supply_unregister(di->chargalg_psy);
Arun Murthy1668f812012-02-29 21:54:25 +05302140free_chargalg_wq:
2141 destroy_workqueue(di->chargalg_wq);
Arun Murthy1668f812012-02-29 21:54:25 +05302142 return ret;
2143}
2144
Rajanikanth H.Va12810a2012-10-31 15:40:33 +00002145static const struct of_device_id ab8500_chargalg_match[] = {
2146 { .compatible = "stericsson,ab8500-chargalg", },
2147 { },
2148};
2149
Arun Murthy1668f812012-02-29 21:54:25 +05302150static struct platform_driver abx500_chargalg_driver = {
2151 .probe = abx500_chargalg_probe,
Bill Pemberton28ea73f2012-11-19 13:20:40 -05002152 .remove = abx500_chargalg_remove,
Arun Murthy1668f812012-02-29 21:54:25 +05302153 .suspend = abx500_chargalg_suspend,
2154 .resume = abx500_chargalg_resume,
2155 .driver = {
Rajanikanth H.Va12810a2012-10-31 15:40:33 +00002156 .name = "ab8500-chargalg",
Rajanikanth H.Va12810a2012-10-31 15:40:33 +00002157 .of_match_table = ab8500_chargalg_match,
Arun Murthy1668f812012-02-29 21:54:25 +05302158 },
2159};
2160
srinidhi kasagar1ee26af2012-10-11 14:38:28 +05302161module_platform_driver(abx500_chargalg_driver);
Arun Murthy1668f812012-02-29 21:54:25 +05302162
2163MODULE_LICENSE("GPL v2");
2164MODULE_AUTHOR("Johan Palsson, Karl Komierowski");
2165MODULE_ALIAS("platform:abx500-chargalg");
2166MODULE_DESCRIPTION("abx500 battery charging algorithm");