ab8500_chargalg.c 56.2 KB
Newer Older
1
// SPDX-License-Identifier: GPL-2.0-only
2 3
/*
 * Copyright (C) ST-Ericsson SA 2012
L
Lee Jones 已提交
4
 * Copyright (c) 2012 Sony Mobile Communications AB
5
 *
6
 * Charging algorithm driver for AB8500
7 8 9 10 11
 *
 * Authors:
 *	Johan Palsson <johan.palsson@stericsson.com>
 *	Karl Komierowski <karl.komierowski@stericsson.com>
 *	Arun R Murthy <arun.murthy@stericsson.com>
L
Lee Jones 已提交
12
 *	Author: Imre Sunyi <imre.sunyi@sonymobile.com>
13 14 15 16 17
 */

#include <linux/init.h>
#include <linux/module.h>
#include <linux/device.h>
18
#include <linux/component.h>
L
Lee Jones 已提交
19
#include <linux/hrtimer.h>
20 21 22 23 24 25 26 27
#include <linux/interrupt.h>
#include <linux/delay.h>
#include <linux/slab.h>
#include <linux/platform_device.h>
#include <linux/power_supply.h>
#include <linux/completion.h>
#include <linux/workqueue.h>
#include <linux/kobject.h>
28 29
#include <linux/of.h>
#include <linux/mfd/core.h>
30
#include <linux/mfd/abx500.h>
L
Lee Jones 已提交
31
#include <linux/mfd/abx500/ab8500.h>
32
#include <linux/notifier.h>
33

34
#include "ab8500-bm.h"
35
#include "ab8500-chargalg.h"
36

37 38 39 40 41 42
/* Watchdog kick interval */
#define CHG_WD_INTERVAL			(6 * HZ)

/* End-of-charge criteria counter */
#define EOC_COND_CNT			10

L
Lee Jones 已提交
43 44 45 46 47 48
/* One hour expressed in seconds */
#define ONE_HOUR_IN_SECONDS            3600

/* Five minutes expressed in seconds */
#define FIVE_MINUTES_IN_SECONDS        300

49 50 51 52 53 54 55
/*
 * This is the battery capacity limit that will trigger a new
 * full charging cycle in the case where maintenance charging
 * has been disabled
 */
#define AB8500_RECHARGE_CAP		95

56
enum ab8500_chargers {
57 58 59 60 61
	NO_CHG,
	AC_CHG,
	USB_CHG,
};

62 63 64 65 66 67
struct ab8500_chargalg_charger_info {
	enum ab8500_chargers conn_chg;
	enum ab8500_chargers prev_conn_chg;
	enum ab8500_chargers online_chg;
	enum ab8500_chargers prev_online_chg;
	enum ab8500_chargers charger_type;
68 69
	bool usb_chg_ok;
	bool ac_chg_ok;
70
	int usb_volt_uv;
71
	int usb_curr_ua;
72
	int ac_volt_uv;
73
	int ac_curr_ua;
74
	int usb_vset_uv;
75
	int usb_iset_ua;
76
	int ac_vset_uv;
77
	int ac_iset_ua;
78 79
};

80
struct ab8500_chargalg_suspension_status {
81 82 83 84 85
	bool suspended_change;
	bool ac_suspended;
	bool usb_suspended;
};

86
struct ab8500_chargalg_battery_data {
87
	int temp;
88
	int volt_uv;
89
	int avg_curr_ua;
90
	int inst_curr_ua;
91 92 93
	int percent;
};

94
enum ab8500_chargalg_states {
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
	STATE_HANDHELD_INIT,
	STATE_HANDHELD,
	STATE_CHG_NOT_OK_INIT,
	STATE_CHG_NOT_OK,
	STATE_HW_TEMP_PROTECT_INIT,
	STATE_HW_TEMP_PROTECT,
	STATE_NORMAL_INIT,
	STATE_NORMAL,
	STATE_WAIT_FOR_RECHARGE_INIT,
	STATE_WAIT_FOR_RECHARGE,
	STATE_MAINTENANCE_A_INIT,
	STATE_MAINTENANCE_A,
	STATE_MAINTENANCE_B_INIT,
	STATE_MAINTENANCE_B,
	STATE_TEMP_UNDEROVER_INIT,
	STATE_TEMP_UNDEROVER,
	STATE_TEMP_LOWHIGH_INIT,
	STATE_TEMP_LOWHIGH,
	STATE_SUSPENDED_INIT,
	STATE_SUSPENDED,
	STATE_OVV_PROTECT_INIT,
	STATE_OVV_PROTECT,
	STATE_SAFETY_TIMER_EXPIRED_INIT,
	STATE_SAFETY_TIMER_EXPIRED,
	STATE_BATT_REMOVED_INIT,
	STATE_BATT_REMOVED,
	STATE_WD_EXPIRED_INIT,
	STATE_WD_EXPIRED,
};

125
static const char * const states[] = {
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155
	"HANDHELD_INIT",
	"HANDHELD",
	"CHG_NOT_OK_INIT",
	"CHG_NOT_OK",
	"HW_TEMP_PROTECT_INIT",
	"HW_TEMP_PROTECT",
	"NORMAL_INIT",
	"NORMAL",
	"WAIT_FOR_RECHARGE_INIT",
	"WAIT_FOR_RECHARGE",
	"MAINTENANCE_A_INIT",
	"MAINTENANCE_A",
	"MAINTENANCE_B_INIT",
	"MAINTENANCE_B",
	"TEMP_UNDEROVER_INIT",
	"TEMP_UNDEROVER",
	"TEMP_LOWHIGH_INIT",
	"TEMP_LOWHIGH",
	"SUSPENDED_INIT",
	"SUSPENDED",
	"OVV_PROTECT_INIT",
	"OVV_PROTECT",
	"SAFETY_TIMER_EXPIRED_INIT",
	"SAFETY_TIMER_EXPIRED",
	"BATT_REMOVED_INIT",
	"BATT_REMOVED",
	"WD_EXPIRED_INIT",
	"WD_EXPIRED",
};

156
struct ab8500_chargalg_events {
157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177
	bool batt_unknown;
	bool mainextchnotok;
	bool batt_ovv;
	bool batt_rem;
	bool btemp_underover;
	bool btemp_lowhigh;
	bool main_thermal_prot;
	bool usb_thermal_prot;
	bool main_ovv;
	bool vbus_ovv;
	bool usbchargernotok;
	bool safety_timer_expired;
	bool maintenance_timer_expired;
	bool ac_wd_expired;
	bool usb_wd_expired;
	bool ac_cv_active;
	bool usb_cv_active;
	bool vbus_collapsed;
};

/**
178
 * struct ab8500_charge_curr_maximization - Charger maximization parameters
179 180
 * @original_iset_ua:	the non optimized/maximised charger current
 * @current_iset_ua:	the charging current used at this moment
181 182
 * @condition_cnt:	number of iterations needed before a new charger current
			is set
183
 * @max_current_ua:	maximum charger current
184 185 186 187 188 189
 * @wait_cnt:		to avoid too fast current step down in case of charger
 *			voltage collapse, we insert this delay between step
 *			down
 * @level:		tells in how many steps the charging current has been
			increased
 */
190
struct ab8500_charge_curr_maximization {
191 192
	int original_iset_ua;
	int current_iset_ua;
193
	int condition_cnt;
194
	int max_current_ua;
195 196 197 198 199 200 201 202 203 204 205
	int wait_cnt;
	u8 level;
};

enum maxim_ret {
	MAXIM_RET_NOACTION,
	MAXIM_RET_CHANGE,
	MAXIM_RET_IBAT_TOO_HIGH,
};

/**
206
 * struct ab8500_chargalg - ab8500 Charging algorithm device information
207 208 209 210 211 212 213 214 215 216 217 218 219
 * @dev:		pointer to the structure device
 * @charge_status:	battery operating status
 * @eoc_cnt:		counter used to determine end-of_charge
 * @maintenance_chg:	indicate if maintenance charge is active
 * @t_hyst_norm		temperature hysteresis when the temperature has been
 *			over or under normal limits
 * @t_hyst_lowhigh	temperature hysteresis when the temperature has been
 *			over or under the high or low limits
 * @charge_state:	current state of the charging algorithm
 * @ccm			charging current maximization parameters
 * @chg_info:		information about connected charger types
 * @batt_data:		data of the battery
 * @susp_status:	current charger suspension status
220
 * @bm:           	Platform specific battery management information
221
 * @parent:		pointer to the struct ab8500
222 223 224 225 226 227 228 229 230 231 232
 * @chargalg_psy:	structure that holds the battery properties exposed by
 *			the charging algorithm
 * @events:		structure for information about events triggered
 * @chargalg_wq:		work queue for running the charging algorithm
 * @chargalg_periodic_work:	work to run the charging algorithm periodically
 * @chargalg_wd_work:		work to kick the charger watchdog periodically
 * @chargalg_work:		work to run the charging algorithm instantly
 * @safety_timer:		charging safety timer
 * @maintenance_timer:		maintenance charging timer
 * @chargalg_kobject:		structure of type kobject
 */
233
struct ab8500_chargalg {
234 235 236 237 238 239
	struct device *dev;
	int charge_status;
	int eoc_cnt;
	bool maintenance_chg;
	int t_hyst_norm;
	int t_hyst_lowhigh;
240 241 242 243 244
	enum ab8500_chargalg_states charge_state;
	struct ab8500_charge_curr_maximization ccm;
	struct ab8500_chargalg_charger_info chg_info;
	struct ab8500_chargalg_battery_data batt_data;
	struct ab8500_chargalg_suspension_status susp_status;
245
	struct ab8500 *parent;
246
	struct ab8500_bm_data *bm;
247
	struct power_supply *chargalg_psy;
248 249
	struct ux500_charger *ac_chg;
	struct ux500_charger *usb_chg;
250
	struct ab8500_chargalg_events events;
251 252 253 254
	struct workqueue_struct *chargalg_wq;
	struct delayed_work chargalg_periodic_work;
	struct delayed_work chargalg_wd_work;
	struct work_struct chargalg_work;
L
Lee Jones 已提交
255 256
	struct hrtimer safety_timer;
	struct hrtimer maintenance_timer;
257 258 259
	struct kobject chargalg_kobject;
};

260 261 262
/*External charger prepare notifier*/
BLOCKING_NOTIFIER_HEAD(charger_notifier_list);

263
/* Main battery properties */
264
static enum power_supply_property ab8500_chargalg_props[] = {
265 266 267 268
	POWER_SUPPLY_PROP_STATUS,
	POWER_SUPPLY_PROP_HEALTH,
};

269
struct ab8500_chargalg_sysfs_entry {
270
	struct attribute attr;
271 272
	ssize_t (*show)(struct ab8500_chargalg *di, char *buf);
	ssize_t (*store)(struct ab8500_chargalg *di, const char *buf, size_t length);
273 274
};

275
/**
276
 * ab8500_chargalg_safety_timer_expired() - Expiration of the safety timer
L
Lee Jones 已提交
277
 * @timer:     pointer to the hrtimer structure
278 279 280 281
 *
 * This function gets called when the safety timer for the charger
 * expires
 */
L
Lee Jones 已提交
282
static enum hrtimer_restart
283
ab8500_chargalg_safety_timer_expired(struct hrtimer *timer)
284
{
285
	struct ab8500_chargalg *di = container_of(timer, struct ab8500_chargalg,
L
Lee Jones 已提交
286
						  safety_timer);
287 288 289 290 291
	dev_err(di->dev, "Safety timer expired\n");
	di->events.safety_timer_expired = true;

	/* Trigger execution of the algorithm instantly */
	queue_work(di->chargalg_wq, &di->chargalg_work);
L
Lee Jones 已提交
292 293

	return HRTIMER_NORESTART;
294 295 296
}

/**
297
 * ab8500_chargalg_maintenance_timer_expired() - Expiration of
298
 * the maintenance timer
L
Lee Jones 已提交
299
 * @timer:     pointer to the timer structure
300 301 302 303
 *
 * This function gets called when the maintenence timer
 * expires
 */
L
Lee Jones 已提交
304
static enum hrtimer_restart
305
ab8500_chargalg_maintenance_timer_expired(struct hrtimer *timer)
306 307
{

308
	struct ab8500_chargalg *di = container_of(timer, struct ab8500_chargalg,
L
Lee Jones 已提交
309 310
						  maintenance_timer);

311 312 313 314 315
	dev_dbg(di->dev, "Maintenance timer expired\n");
	di->events.maintenance_timer_expired = true;

	/* Trigger execution of the algorithm instantly */
	queue_work(di->chargalg_wq, &di->chargalg_work);
L
Lee Jones 已提交
316 317

	return HRTIMER_NORESTART;
318 319 320
}

/**
321 322
 * ab8500_chargalg_state_to() - Change charge state
 * @di:		pointer to the ab8500_chargalg structure
323 324 325
 *
 * This function gets called when a charge state change should occur
 */
326 327
static void ab8500_chargalg_state_to(struct ab8500_chargalg *di,
	enum ab8500_chargalg_states state)
328 329 330 331 332 333 334 335 336 337 338 339
{
	dev_dbg(di->dev,
		"State changed: %s (From state: [%d] %s =to=> [%d] %s )\n",
		di->charge_state == state ? "NO" : "YES",
		di->charge_state,
		states[di->charge_state],
		state,
		states[state]);

	di->charge_state = state;
}

340
static int ab8500_chargalg_check_charger_enable(struct ab8500_chargalg *di)
341
{
342
	struct power_supply_battery_info *bi = di->bm->bi;
343

344 345 346 347 348 349 350 351 352 353 354
	switch (di->charge_state) {
	case STATE_NORMAL:
	case STATE_MAINTENANCE_A:
	case STATE_MAINTENANCE_B:
		break;
	default:
		return 0;
	}

	if (di->chg_info.charger_type & USB_CHG) {
		return di->usb_chg->ops.check_enable(di->usb_chg,
355
			bi->constant_charge_voltage_max_uv,
356
			bi->constant_charge_current_max_ua);
357 358 359
	} else if ((di->chg_info.charger_type & AC_CHG) &&
		   !(di->ac_chg->external)) {
		return di->ac_chg->ops.check_enable(di->ac_chg,
360
			bi->constant_charge_voltage_max_uv,
361
			bi->constant_charge_current_max_ua);
362 363 364 365
	}
	return 0;
}

366
/**
367 368
 * ab8500_chargalg_check_charger_connection() - Check charger connection change
 * @di:		pointer to the ab8500_chargalg structure
369 370 371 372
 *
 * This function will check if there is a change in the charger connection
 * and change charge state accordingly. AC has precedence over USB.
 */
373
static int ab8500_chargalg_check_charger_connection(struct ab8500_chargalg *di)
374 375 376 377 378 379 380 381 382 383 384 385
{
	if (di->chg_info.conn_chg != di->chg_info.prev_conn_chg ||
		di->susp_status.suspended_change) {
		/*
		 * Charger state changed or suspension
		 * has changed since last update
		 */
		if ((di->chg_info.conn_chg & AC_CHG) &&
			!di->susp_status.ac_suspended) {
			dev_dbg(di->dev, "Charging source is AC\n");
			if (di->chg_info.charger_type != AC_CHG) {
				di->chg_info.charger_type = AC_CHG;
386
				ab8500_chargalg_state_to(di, STATE_NORMAL_INIT);
387 388 389 390 391
			}
		} else if ((di->chg_info.conn_chg & USB_CHG) &&
			!di->susp_status.usb_suspended) {
			dev_dbg(di->dev, "Charging source is USB\n");
			di->chg_info.charger_type = USB_CHG;
392
			ab8500_chargalg_state_to(di, STATE_NORMAL_INIT);
393 394 395 396 397
		} else if (di->chg_info.conn_chg &&
			(di->susp_status.ac_suspended ||
			di->susp_status.usb_suspended)) {
			dev_dbg(di->dev, "Charging is suspended\n");
			di->chg_info.charger_type = NO_CHG;
398
			ab8500_chargalg_state_to(di, STATE_SUSPENDED_INIT);
399 400 401
		} else {
			dev_dbg(di->dev, "Charging source is OFF\n");
			di->chg_info.charger_type = NO_CHG;
402
			ab8500_chargalg_state_to(di, STATE_HANDHELD_INIT);
403 404 405 406 407 408 409 410
		}
		di->chg_info.prev_conn_chg = di->chg_info.conn_chg;
		di->susp_status.suspended_change = false;
	}
	return di->chg_info.conn_chg;
}

/**
411 412
 * ab8500_chargalg_start_safety_timer() - Start charging safety timer
 * @di:		pointer to the ab8500_chargalg structure
413 414 415 416
 *
 * The safety timer is used to avoid overcharging of old or bad batteries.
 * There are different timers for AC and USB
 */
417
static void ab8500_chargalg_start_safety_timer(struct ab8500_chargalg *di)
418
{
L
Lee Jones 已提交
419 420
	/* Charger-dependent expiration time in hours*/
	int timer_expiration = 0;
421 422 423

	switch (di->chg_info.charger_type) {
	case AC_CHG:
L
Lee Jones 已提交
424
		timer_expiration = di->bm->main_safety_tmr_h;
425 426 427
		break;

	case USB_CHG:
L
Lee Jones 已提交
428
		timer_expiration = di->bm->usb_safety_tmr_h;
429 430 431 432 433 434 435 436
		break;

	default:
		dev_err(di->dev, "Unknown charger to charge from\n");
		break;
	}

	di->events.safety_timer_expired = false;
L
Lee Jones 已提交
437 438 439 440
	hrtimer_set_expires_range(&di->safety_timer,
		ktime_set(timer_expiration * ONE_HOUR_IN_SECONDS, 0),
		ktime_set(FIVE_MINUTES_IN_SECONDS, 0));
	hrtimer_start_expires(&di->safety_timer, HRTIMER_MODE_REL);
441 442 443
}

/**
444 445
 * ab8500_chargalg_stop_safety_timer() - Stop charging safety timer
 * @di:		pointer to the ab8500_chargalg structure
446 447 448
 *
 * The safety timer is stopped whenever the NORMAL state is exited
 */
449
static void ab8500_chargalg_stop_safety_timer(struct ab8500_chargalg *di)
450
{
L
Lee Jones 已提交
451 452
	if (hrtimer_try_to_cancel(&di->safety_timer) >= 0)
		di->events.safety_timer_expired = false;
453 454 455
}

/**
456 457
 * ab8500_chargalg_start_maintenance_timer() - Start charging maintenance timer
 * @di:		pointer to the ab8500_chargalg structure
458 459 460 461 462 463
 * @duration:	duration of ther maintenance timer in hours
 *
 * The maintenance timer is used to maintain the charge in the battery once
 * the battery is considered full. These timers are chosen to match the
 * discharge curve of the battery
 */
464
static void ab8500_chargalg_start_maintenance_timer(struct ab8500_chargalg *di,
465 466
	int duration)
{
L
Lee Jones 已提交
467 468 469
	hrtimer_set_expires_range(&di->maintenance_timer,
		ktime_set(duration * ONE_HOUR_IN_SECONDS, 0),
		ktime_set(FIVE_MINUTES_IN_SECONDS, 0));
470
	di->events.maintenance_timer_expired = false;
L
Lee Jones 已提交
471
	hrtimer_start_expires(&di->maintenance_timer, HRTIMER_MODE_REL);
472 473 474
}

/**
475 476
 * ab8500_chargalg_stop_maintenance_timer() - Stop maintenance timer
 * @di:		pointer to the ab8500_chargalg structure
477 478 479 480
 *
 * The maintenance timer is stopped whenever maintenance ends or when another
 * state is entered
 */
481
static void ab8500_chargalg_stop_maintenance_timer(struct ab8500_chargalg *di)
482
{
L
Lee Jones 已提交
483 484
	if (hrtimer_try_to_cancel(&di->maintenance_timer) >= 0)
		di->events.maintenance_timer_expired = false;
485 486 487
}

/**
488 489
 * ab8500_chargalg_kick_watchdog() - Kick charger watchdog
 * @di:		pointer to the ab8500_chargalg structure
490 491 492 493
 *
 * The charger watchdog have to be kicked periodically whenever the charger is
 * on, else the ABB will reset the system
 */
494
static int ab8500_chargalg_kick_watchdog(struct ab8500_chargalg *di)
495 496 497
{
	/* Check if charger exists and kick watchdog if charging */
	if (di->ac_chg && di->ac_chg->ops.kick_wd &&
498 499 500 501 502 503 504 505 506 507
	    di->chg_info.online_chg & AC_CHG) {
		/*
		 * If AB charger watchdog expired, pm2xxx charging
		 * gets disabled. To be safe, kick both AB charger watchdog
		 * and pm2xxx watchdog.
		 */
		if (di->ac_chg->external &&
		    di->usb_chg && di->usb_chg->ops.kick_wd)
			di->usb_chg->ops.kick_wd(di->usb_chg);

508
		return di->ac_chg->ops.kick_wd(di->ac_chg);
509
	} else if (di->usb_chg && di->usb_chg->ops.kick_wd &&
510 511 512 513 514 515 516
			di->chg_info.online_chg & USB_CHG)
		return di->usb_chg->ops.kick_wd(di->usb_chg);

	return -ENXIO;
}

/**
517 518
 * ab8500_chargalg_ac_en() - Turn on/off the AC charger
 * @di:		pointer to the ab8500_chargalg structure
519
 * @enable:	charger on/off
520
 * @vset_uv:	requested charger output voltage in microvolt
521
 * @iset_ua:	requested charger output current in microampere
522 523 524 525
 *
 * The AC charger will be turned on/off with the requested charge voltage and
 * current
 */
526
static int ab8500_chargalg_ac_en(struct ab8500_chargalg *di, int enable,
527
	int vset_uv, int iset_ua)
528
{
529
	static int ab8500_chargalg_ex_ac_enable_toggle;
530

531 532 533 534
	if (!di->ac_chg || !di->ac_chg->ops.enable)
		return -ENXIO;

	/* Select maximum of what both the charger and the battery supports */
535 536
	if (di->ac_chg->max_out_volt_uv)
		vset_uv = min(vset_uv, di->ac_chg->max_out_volt_uv);
537 538
	if (di->ac_chg->max_out_curr_ua)
		iset_ua = min(iset_ua, di->ac_chg->max_out_curr_ua);
539

540
	di->chg_info.ac_iset_ua = iset_ua;
541
	di->chg_info.ac_vset_uv = vset_uv;
542

543 544
	/* Enable external charger */
	if (enable && di->ac_chg->external &&
545
	    !ab8500_chargalg_ex_ac_enable_toggle) {
546 547
		blocking_notifier_call_chain(&charger_notifier_list,
					     0, di->dev);
548
		ab8500_chargalg_ex_ac_enable_toggle++;
549 550
	}

551
	return di->ac_chg->ops.enable(di->ac_chg, enable, vset_uv, iset_ua);
552 553 554
}

/**
555 556
 * ab8500_chargalg_usb_en() - Turn on/off the USB charger
 * @di:		pointer to the ab8500_chargalg structure
557
 * @enable:	charger on/off
558
 * @vset_uv:	requested charger output voltage in microvolt
559
 * @iset_ua:	requested charger output current in microampere
560 561 562 563
 *
 * The USB charger will be turned on/off with the requested charge voltage and
 * current
 */
564
static int ab8500_chargalg_usb_en(struct ab8500_chargalg *di, int enable,
565
	int vset_uv, int iset_ua)
566 567 568 569 570
{
	if (!di->usb_chg || !di->usb_chg->ops.enable)
		return -ENXIO;

	/* Select maximum of what both the charger and the battery supports */
571 572
	if (di->usb_chg->max_out_volt_uv)
		vset_uv = min(vset_uv, di->usb_chg->max_out_volt_uv);
573 574
	if (di->usb_chg->max_out_curr_ua)
		iset_ua = min(iset_ua, di->usb_chg->max_out_curr_ua);
575

576
	di->chg_info.usb_iset_ua = iset_ua;
577
	di->chg_info.usb_vset_uv = vset_uv;
578

579
	return di->usb_chg->ops.enable(di->usb_chg, enable, vset_uv, iset_ua);
580 581 582
}

/**
583 584
 * ab8500_chargalg_update_chg_curr() - Update charger current
 * @di:		pointer to the ab8500_chargalg structure
585
 * @iset_ua:	requested charger output current in microampere
586 587 588 589
 *
 * The charger output current will be updated for the charger
 * that is currently in use
 */
590
static int ab8500_chargalg_update_chg_curr(struct ab8500_chargalg *di,
591
		int iset_ua)
592 593 594 595 596 597 598 599
{
	/* Check if charger exists and update current if charging */
	if (di->ac_chg && di->ac_chg->ops.update_curr &&
			di->chg_info.charger_type & AC_CHG) {
		/*
		 * Select maximum of what both the charger
		 * and the battery supports
		 */
600 601
		if (di->ac_chg->max_out_curr_ua)
			iset_ua = min(iset_ua, di->ac_chg->max_out_curr_ua);
602

603
		di->chg_info.ac_iset_ua = iset_ua;
604

605
		return di->ac_chg->ops.update_curr(di->ac_chg, iset_ua);
606 607 608 609 610 611
	} else if (di->usb_chg && di->usb_chg->ops.update_curr &&
			di->chg_info.charger_type & USB_CHG) {
		/*
		 * Select maximum of what both the charger
		 * and the battery supports
		 */
612 613
		if (di->usb_chg->max_out_curr_ua)
			iset_ua = min(iset_ua, di->usb_chg->max_out_curr_ua);
614

615
		di->chg_info.usb_iset_ua = iset_ua;
616

617
		return di->usb_chg->ops.update_curr(di->usb_chg, iset_ua);
618 619 620 621 622 623
	}

	return -ENXIO;
}

/**
624 625
 * ab8500_chargalg_stop_charging() - Stop charging
 * @di:		pointer to the ab8500_chargalg structure
626 627 628 629 630
 *
 * This function is called from any state where charging should be stopped.
 * All charging is disabled and all status parameters and timers are changed
 * accordingly
 */
631
static void ab8500_chargalg_stop_charging(struct ab8500_chargalg *di)
632
{
633 634 635 636
	ab8500_chargalg_ac_en(di, false, 0, 0);
	ab8500_chargalg_usb_en(di, false, 0, 0);
	ab8500_chargalg_stop_safety_timer(di);
	ab8500_chargalg_stop_maintenance_timer(di);
637 638 639
	di->charge_status = POWER_SUPPLY_STATUS_NOT_CHARGING;
	di->maintenance_chg = false;
	cancel_delayed_work(&di->chargalg_wd_work);
640
	power_supply_changed(di->chargalg_psy);
641 642 643
}

/**
644 645
 * ab8500_chargalg_hold_charging() - Pauses charging
 * @di:		pointer to the ab8500_chargalg structure
646 647 648 649 650
 *
 * This function is called in the case where maintenance charging has been
 * disabled and instead a battery voltage mode is entered to check when the
 * battery voltage has reached a certain recharge voltage
 */
651
static void ab8500_chargalg_hold_charging(struct ab8500_chargalg *di)
652
{
653 654 655 656
	ab8500_chargalg_ac_en(di, false, 0, 0);
	ab8500_chargalg_usb_en(di, false, 0, 0);
	ab8500_chargalg_stop_safety_timer(di);
	ab8500_chargalg_stop_maintenance_timer(di);
657 658 659
	di->charge_status = POWER_SUPPLY_STATUS_CHARGING;
	di->maintenance_chg = false;
	cancel_delayed_work(&di->chargalg_wd_work);
660
	power_supply_changed(di->chargalg_psy);
661 662 663
}

/**
664 665
 * ab8500_chargalg_start_charging() - Start the charger
 * @di:		pointer to the ab8500_chargalg structure
666
 * @vset_uv:	requested charger output voltage in microvolt
667
 * @iset_ua:	requested charger output current in microampere
668 669 670 671
 *
 * A charger will be enabled depending on the requested charger type that was
 * detected previously.
 */
672
static void ab8500_chargalg_start_charging(struct ab8500_chargalg *di,
673
	int vset_uv, int iset_ua)
674 675 676 677
{
	switch (di->chg_info.charger_type) {
	case AC_CHG:
		dev_dbg(di->dev,
678
			"AC parameters: Vset %d, Ich %d\n", vset_uv, iset_ua);
679
		ab8500_chargalg_usb_en(di, false, 0, 0);
680
		ab8500_chargalg_ac_en(di, true, vset_uv, iset_ua);
681 682 683 684
		break;

	case USB_CHG:
		dev_dbg(di->dev,
685
			"USB parameters: Vset %d, Ich %d\n", vset_uv, iset_ua);
686
		ab8500_chargalg_ac_en(di, false, 0, 0);
687
		ab8500_chargalg_usb_en(di, true, vset_uv, iset_ua);
688 689 690 691 692 693 694 695 696
		break;

	default:
		dev_err(di->dev, "Unknown charger to charge from\n");
		break;
	}
}

/**
697 698
 * ab8500_chargalg_check_temp() - Check battery temperature ranges
 * @di:		pointer to the ab8500_chargalg structure
699 700 701 702
 *
 * The battery temperature is checked against the predefined limits and the
 * charge state is changed accordingly
 */
703
static void ab8500_chargalg_check_temp(struct ab8500_chargalg *di)
704
{
705
	struct power_supply_battery_info *bi = di->bm->bi;
706 707 708

	if (di->batt_data.temp > (bi->temp_alert_min + di->t_hyst_norm) &&
		di->batt_data.temp < (bi->temp_alert_max - di->t_hyst_norm)) {
709 710 711 712 713 714
		/* Temp OK! */
		di->events.btemp_underover = false;
		di->events.btemp_lowhigh = false;
		di->t_hyst_norm = 0;
		di->t_hyst_lowhigh = 0;
	} else {
715
		if (((di->batt_data.temp >= bi->temp_alert_max) &&
716
			(di->batt_data.temp <
717
				(bi->temp_max - di->t_hyst_lowhigh))) ||
718
			((di->batt_data.temp >
719 720
				(bi->temp_min + di->t_hyst_lowhigh)) &&
			(di->batt_data.temp <= bi->temp_alert_min))) {
721 722 723
			/* TEMP minor!!!!! */
			di->events.btemp_underover = false;
			di->events.btemp_lowhigh = true;
724
			di->t_hyst_norm = di->bm->temp_hysteresis;
725
			di->t_hyst_lowhigh = 0;
726 727
		} else if (di->batt_data.temp <= bi->temp_min ||
			di->batt_data.temp >= bi->temp_max) {
728 729 730 731
			/* TEMP major!!!!! */
			di->events.btemp_underover = true;
			di->events.btemp_lowhigh = false;
			di->t_hyst_norm = 0;
732
			di->t_hyst_lowhigh = di->bm->temp_hysteresis;
733
		} else {
734 735
			/* Within hysteresis */
			dev_dbg(di->dev, "Within hysteresis limit temp: %d "
736 737 738 739 740 741 742 743
				"hyst_lowhigh %d, hyst normal %d\n",
				di->batt_data.temp, di->t_hyst_lowhigh,
				di->t_hyst_norm);
		}
	}
}

/**
744 745
 * ab8500_chargalg_check_charger_voltage() - Check charger voltage
 * @di:		pointer to the ab8500_chargalg structure
746 747 748
 *
 * Charger voltage is checked against maximum limit
 */
749
static void ab8500_chargalg_check_charger_voltage(struct ab8500_chargalg *di)
750
{
751
	if (di->chg_info.usb_volt_uv > di->bm->chg_params->usb_volt_max_uv)
752 753 754 755
		di->chg_info.usb_chg_ok = false;
	else
		di->chg_info.usb_chg_ok = true;

756
	if (di->chg_info.ac_volt_uv > di->bm->chg_params->ac_volt_max_uv)
757 758 759 760 761 762 763
		di->chg_info.ac_chg_ok = false;
	else
		di->chg_info.ac_chg_ok = true;

}

/**
764 765
 * ab8500_chargalg_end_of_charge() - Check if end-of-charge criteria is fulfilled
 * @di:		pointer to the ab8500_chargalg structure
766 767 768 769 770
 *
 * End-of-charge criteria is fulfilled when the battery voltage is above a
 * certain limit and the battery current is below a certain limit for a
 * predefined number of consecutive seconds. If true, the battery is full
 */
771
static void ab8500_chargalg_end_of_charge(struct ab8500_chargalg *di)
772 773 774
{
	if (di->charge_status == POWER_SUPPLY_STATUS_CHARGING &&
		di->charge_state == STATE_NORMAL &&
775
		!di->maintenance_chg && (di->batt_data.volt_uv >=
776
		di->bm->bi->voltage_max_design_uv ||
777
		di->events.usb_cv_active || di->events.ac_cv_active) &&
778
		di->batt_data.avg_curr_ua <
779
		di->bm->bi->charge_term_current_ua &&
780
		di->batt_data.avg_curr_ua > 0) {
781 782 783 784 785
		if (++di->eoc_cnt >= EOC_COND_CNT) {
			di->eoc_cnt = 0;
			di->charge_status = POWER_SUPPLY_STATUS_FULL;
			di->maintenance_chg = true;
			dev_dbg(di->dev, "EOC reached!\n");
786
			power_supply_changed(di->chargalg_psy);
787 788 789 790 791 792 793 794 795 796 797 798
		} else {
			dev_dbg(di->dev,
				" EOC limit reached for the %d"
				" time, out of %d before EOC\n",
				di->eoc_cnt,
				EOC_COND_CNT);
		}
	} else {
		di->eoc_cnt = 0;
	}
}

799
static void init_maxim_chg_curr(struct ab8500_chargalg *di)
800
{
801
	struct power_supply_battery_info *bi = di->bm->bi;
802 803 804 805

	di->ccm.original_iset_ua = bi->constant_charge_current_max_ua;
	di->ccm.current_iset_ua = bi->constant_charge_current_max_ua;
	di->ccm.max_current_ua = di->bm->maxi->chg_curr_ua;
806
	di->ccm.condition_cnt = di->bm->maxi->wait_cycles;
807 808 809 810
	di->ccm.level = 0;
}

/**
811
 * ab8500_chargalg_chg_curr_maxim - increases the charger current to
812
 *			compensate for the system load
813
 * @di		pointer to the ab8500_chargalg structure
814 815 816 817 818
 *
 * This maximization function is used to raise the charger current to get the
 * battery current as close to the optimal value as possible. The battery
 * current during charging is affected by the system load
 */
819
static enum maxim_ret ab8500_chargalg_chg_curr_maxim(struct ab8500_chargalg *di)
820
{
821
	int delta_i_ua;
822

823
	if (!di->bm->maxi->ena_maxi)
824 825
		return MAXIM_RET_NOACTION;

826
	delta_i_ua = di->ccm.original_iset_ua - di->batt_data.inst_curr_ua;
827 828 829 830 831 832 833

	if (di->events.vbus_collapsed) {
		dev_dbg(di->dev, "Charger voltage has collapsed %d\n",
				di->ccm.wait_cnt);
		if (di->ccm.wait_cnt == 0) {
			dev_dbg(di->dev, "lowering current\n");
			di->ccm.wait_cnt++;
834
			di->ccm.condition_cnt = di->bm->maxi->wait_cycles;
835
			di->ccm.max_current_ua = di->ccm.current_iset_ua;
836
			di->ccm.current_iset_ua = di->ccm.max_current_ua;
837 838 839 840 841 842 843 844 845 846 847 848
			di->ccm.level--;
			return MAXIM_RET_CHANGE;
		} else {
			dev_dbg(di->dev, "waiting\n");
			/* Let's go in here twice before lowering curr again */
			di->ccm.wait_cnt = (di->ccm.wait_cnt + 1) % 3;
			return MAXIM_RET_NOACTION;
		}
	}

	di->ccm.wait_cnt = 0;

849 850 851 852 853
	if (di->batt_data.inst_curr_ua > di->ccm.original_iset_ua) {
		dev_dbg(di->dev, " Maximization Ibat (%duA) too high"
			" (limit %duA) (current iset: %duA)!\n",
			di->batt_data.inst_curr_ua, di->ccm.original_iset_ua,
			di->ccm.current_iset_ua);
854

855
		if (di->ccm.current_iset_ua == di->ccm.original_iset_ua)
856 857
			return MAXIM_RET_NOACTION;

858
		di->ccm.condition_cnt = di->bm->maxi->wait_cycles;
859
		di->ccm.current_iset_ua = di->ccm.original_iset_ua;
860 861 862 863 864
		di->ccm.level = 0;

		return MAXIM_RET_IBAT_TOO_HIGH;
	}

865 866
	di->ccm.condition_cnt = di->bm->maxi->wait_cycles;
	return MAXIM_RET_NOACTION;
867 868
}

869
static void handle_maxim_chg_curr(struct ab8500_chargalg *di)
870
{
871
	struct power_supply_battery_info *bi = di->bm->bi;
872 873 874
	enum maxim_ret ret;
	int result;

875
	ret = ab8500_chargalg_chg_curr_maxim(di);
876 877
	switch (ret) {
	case MAXIM_RET_CHANGE:
878
		result = ab8500_chargalg_update_chg_curr(di,
879
			di->ccm.current_iset_ua);
880 881 882 883
		if (result)
			dev_err(di->dev, "failed to set chg curr\n");
		break;
	case MAXIM_RET_IBAT_TOO_HIGH:
884
		result = ab8500_chargalg_update_chg_curr(di,
885
			bi->constant_charge_current_max_ua);
886 887 888 889 890 891 892 893 894 895 896
		if (result)
			dev_err(di->dev, "failed to set chg curr\n");
		break;

	case MAXIM_RET_NOACTION:
	default:
		/* Do nothing..*/
		break;
	}
}

897
static int ab8500_chargalg_get_ext_psy_data(struct device *dev, void *data)
898 899
{
	struct power_supply *psy;
900 901
	struct power_supply *ext = dev_get_drvdata(dev);
	const char **supplicants = (const char **)ext->supplied_to;
902
	struct ab8500_chargalg *di;
903
	union power_supply_propval ret;
904
	int j;
905
	bool capacity_updated = false;
906 907

	psy = (struct power_supply *)data;
908
	di = power_supply_get_drvdata(psy);
909
	/* For all psy where the driver name appears in any supplied_to */
910 911
	j = match_string(supplicants, ext->num_supplicants, psy->desc->name);
	if (j < 0)
912 913
		return 0;

914 915 916 917 918
	/*
	 *  If external is not registering 'POWER_SUPPLY_PROP_CAPACITY' to its
	 * property because of handling that sysfs entry on its own, this is
	 * the place to get the battery capacity.
	 */
919
	if (!power_supply_get_property(ext, POWER_SUPPLY_PROP_CAPACITY, &ret)) {
920 921 922 923
		di->batt_data.percent = ret.intval;
		capacity_updated = true;
	}

924
	/* Go through all properties for the psy */
925
	for (j = 0; j < ext->desc->num_properties; j++) {
926
		enum power_supply_property prop;
927
		prop = ext->desc->properties[j];
928

929 930 931
		/*
		 * Initialize chargers if not already done.
		 * The ab8500_charger*/
932
		if (!di->ac_chg &&
933
			ext->desc->type == POWER_SUPPLY_TYPE_MAINS)
934 935
			di->ac_chg = psy_to_ux500_charger(ext);
		else if (!di->usb_chg &&
936
			ext->desc->type == POWER_SUPPLY_TYPE_USB)
937 938
			di->usb_chg = psy_to_ux500_charger(ext);

939
		if (power_supply_get_property(ext, prop, &ret))
940 941 942
			continue;
		switch (prop) {
		case POWER_SUPPLY_PROP_PRESENT:
943
			switch (ext->desc->type) {
944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989
			case POWER_SUPPLY_TYPE_BATTERY:
				/* Battery present */
				if (ret.intval)
					di->events.batt_rem = false;
				/* Battery removed */
				else
					di->events.batt_rem = true;
				break;
			case POWER_SUPPLY_TYPE_MAINS:
				/* AC disconnected */
				if (!ret.intval &&
					(di->chg_info.conn_chg & AC_CHG)) {
					di->chg_info.prev_conn_chg =
						di->chg_info.conn_chg;
					di->chg_info.conn_chg &= ~AC_CHG;
				}
				/* AC connected */
				else if (ret.intval &&
					!(di->chg_info.conn_chg & AC_CHG)) {
					di->chg_info.prev_conn_chg =
						di->chg_info.conn_chg;
					di->chg_info.conn_chg |= AC_CHG;
				}
				break;
			case POWER_SUPPLY_TYPE_USB:
				/* USB disconnected */
				if (!ret.intval &&
					(di->chg_info.conn_chg & USB_CHG)) {
					di->chg_info.prev_conn_chg =
						di->chg_info.conn_chg;
					di->chg_info.conn_chg &= ~USB_CHG;
				}
				/* USB connected */
				else if (ret.intval &&
					!(di->chg_info.conn_chg & USB_CHG)) {
					di->chg_info.prev_conn_chg =
						di->chg_info.conn_chg;
					di->chg_info.conn_chg |= USB_CHG;
				}
				break;
			default:
				break;
			}
			break;

		case POWER_SUPPLY_PROP_ONLINE:
990
			switch (ext->desc->type) {
991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034
			case POWER_SUPPLY_TYPE_BATTERY:
				break;
			case POWER_SUPPLY_TYPE_MAINS:
				/* AC offline */
				if (!ret.intval &&
					(di->chg_info.online_chg & AC_CHG)) {
					di->chg_info.prev_online_chg =
						di->chg_info.online_chg;
					di->chg_info.online_chg &= ~AC_CHG;
				}
				/* AC online */
				else if (ret.intval &&
					!(di->chg_info.online_chg & AC_CHG)) {
					di->chg_info.prev_online_chg =
						di->chg_info.online_chg;
					di->chg_info.online_chg |= AC_CHG;
					queue_delayed_work(di->chargalg_wq,
						&di->chargalg_wd_work, 0);
				}
				break;
			case POWER_SUPPLY_TYPE_USB:
				/* USB offline */
				if (!ret.intval &&
					(di->chg_info.online_chg & USB_CHG)) {
					di->chg_info.prev_online_chg =
						di->chg_info.online_chg;
					di->chg_info.online_chg &= ~USB_CHG;
				}
				/* USB online */
				else if (ret.intval &&
					!(di->chg_info.online_chg & USB_CHG)) {
					di->chg_info.prev_online_chg =
						di->chg_info.online_chg;
					di->chg_info.online_chg |= USB_CHG;
					queue_delayed_work(di->chargalg_wq,
						&di->chargalg_wd_work, 0);
				}
				break;
			default:
				break;
			}
			break;

		case POWER_SUPPLY_PROP_HEALTH:
1035
			switch (ext->desc->type) {
1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111
			case POWER_SUPPLY_TYPE_BATTERY:
				break;
			case POWER_SUPPLY_TYPE_MAINS:
				switch (ret.intval) {
				case POWER_SUPPLY_HEALTH_UNSPEC_FAILURE:
					di->events.mainextchnotok = true;
					di->events.main_thermal_prot = false;
					di->events.main_ovv = false;
					di->events.ac_wd_expired = false;
					break;
				case POWER_SUPPLY_HEALTH_DEAD:
					di->events.ac_wd_expired = true;
					di->events.mainextchnotok = false;
					di->events.main_ovv = false;
					di->events.main_thermal_prot = false;
					break;
				case POWER_SUPPLY_HEALTH_COLD:
				case POWER_SUPPLY_HEALTH_OVERHEAT:
					di->events.main_thermal_prot = true;
					di->events.mainextchnotok = false;
					di->events.main_ovv = false;
					di->events.ac_wd_expired = false;
					break;
				case POWER_SUPPLY_HEALTH_OVERVOLTAGE:
					di->events.main_ovv = true;
					di->events.mainextchnotok = false;
					di->events.main_thermal_prot = false;
					di->events.ac_wd_expired = false;
					break;
				case POWER_SUPPLY_HEALTH_GOOD:
					di->events.main_thermal_prot = false;
					di->events.mainextchnotok = false;
					di->events.main_ovv = false;
					di->events.ac_wd_expired = false;
					break;
				default:
					break;
				}
				break;

			case POWER_SUPPLY_TYPE_USB:
				switch (ret.intval) {
				case POWER_SUPPLY_HEALTH_UNSPEC_FAILURE:
					di->events.usbchargernotok = true;
					di->events.usb_thermal_prot = false;
					di->events.vbus_ovv = false;
					di->events.usb_wd_expired = false;
					break;
				case POWER_SUPPLY_HEALTH_DEAD:
					di->events.usb_wd_expired = true;
					di->events.usbchargernotok = false;
					di->events.usb_thermal_prot = false;
					di->events.vbus_ovv = false;
					break;
				case POWER_SUPPLY_HEALTH_COLD:
				case POWER_SUPPLY_HEALTH_OVERHEAT:
					di->events.usb_thermal_prot = true;
					di->events.usbchargernotok = false;
					di->events.vbus_ovv = false;
					di->events.usb_wd_expired = false;
					break;
				case POWER_SUPPLY_HEALTH_OVERVOLTAGE:
					di->events.vbus_ovv = true;
					di->events.usbchargernotok = false;
					di->events.usb_thermal_prot = false;
					di->events.usb_wd_expired = false;
					break;
				case POWER_SUPPLY_HEALTH_GOOD:
					di->events.usbchargernotok = false;
					di->events.usb_thermal_prot = false;
					di->events.vbus_ovv = false;
					di->events.usb_wd_expired = false;
					break;
				default:
					break;
				}
1112
				break;
1113 1114 1115 1116 1117 1118
			default:
				break;
			}
			break;

		case POWER_SUPPLY_PROP_VOLTAGE_NOW:
1119
			switch (ext->desc->type) {
1120
			case POWER_SUPPLY_TYPE_BATTERY:
1121
				di->batt_data.volt_uv = ret.intval;
1122 1123
				break;
			case POWER_SUPPLY_TYPE_MAINS:
1124
				di->chg_info.ac_volt_uv = ret.intval;
1125 1126
				break;
			case POWER_SUPPLY_TYPE_USB:
1127
				di->chg_info.usb_volt_uv = ret.intval;
1128 1129 1130 1131 1132 1133 1134
				break;
			default:
				break;
			}
			break;

		case POWER_SUPPLY_PROP_VOLTAGE_AVG:
1135
			switch (ext->desc->type) {
1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159
			case POWER_SUPPLY_TYPE_MAINS:
				/* AVG is used to indicate when we are
				 * in CV mode */
				if (ret.intval)
					di->events.ac_cv_active = true;
				else
					di->events.ac_cv_active = false;

				break;
			case POWER_SUPPLY_TYPE_USB:
				/* AVG is used to indicate when we are
				 * in CV mode */
				if (ret.intval)
					di->events.usb_cv_active = true;
				else
					di->events.usb_cv_active = false;

				break;
			default:
				break;
			}
			break;

		case POWER_SUPPLY_PROP_TECHNOLOGY:
1160
			switch (ext->desc->type) {
1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177
			case POWER_SUPPLY_TYPE_BATTERY:
				if (ret.intval)
					di->events.batt_unknown = false;
				else
					di->events.batt_unknown = true;

				break;
			default:
				break;
			}
			break;

		case POWER_SUPPLY_PROP_TEMP:
			di->batt_data.temp = ret.intval / 10;
			break;

		case POWER_SUPPLY_PROP_CURRENT_NOW:
1178
			switch (ext->desc->type) {
1179
			case POWER_SUPPLY_TYPE_MAINS:
1180 1181
				di->chg_info.ac_curr_ua = ret.intval;
				break;
1182
			case POWER_SUPPLY_TYPE_USB:
1183
				di->chg_info.usb_curr_ua = ret.intval;
1184 1185
				break;
			case POWER_SUPPLY_TYPE_BATTERY:
1186
				di->batt_data.inst_curr_ua = ret.intval;
1187 1188 1189 1190 1191 1192 1193
				break;
			default:
				break;
			}
			break;

		case POWER_SUPPLY_PROP_CURRENT_AVG:
1194
			switch (ext->desc->type) {
1195
			case POWER_SUPPLY_TYPE_BATTERY:
1196
				di->batt_data.avg_curr_ua = ret.intval;
1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208
				break;
			case POWER_SUPPLY_TYPE_USB:
				if (ret.intval)
					di->events.vbus_collapsed = true;
				else
					di->events.vbus_collapsed = false;
				break;
			default:
				break;
			}
			break;
		case POWER_SUPPLY_PROP_CAPACITY:
1209 1210
			if (!capacity_updated)
				di->batt_data.percent = ret.intval;
1211 1212 1213 1214 1215 1216 1217 1218 1219
			break;
		default:
			break;
		}
	}
	return 0;
}

/**
1220
 * ab8500_chargalg_external_power_changed() - callback for power supply changes
1221 1222 1223 1224 1225 1226 1227
 * @psy:       pointer to the structure power_supply
 *
 * This function is the entry point of the pointer external_power_changed
 * of the structure power_supply.
 * This function gets executed when there is a change in any external power
 * supply that this driver needs to be notified of.
 */
1228
static void ab8500_chargalg_external_power_changed(struct power_supply *psy)
1229
{
1230
	struct ab8500_chargalg *di = power_supply_get_drvdata(psy);
1231 1232 1233 1234 1235

	/*
	 * Trigger execution of the algorithm instantly and read
	 * all power_supply properties there instead
	 */
1236 1237
	if (di->chargalg_wq)
		queue_work(di->chargalg_wq, &di->chargalg_work);
1238 1239 1240
}

/**
1241 1242
 * ab8500_chargalg_algorithm() - Main function for the algorithm
 * @di:		pointer to the ab8500_chargalg structure
1243 1244 1245 1246 1247
 *
 * This is the main control function for the charging algorithm.
 * It is called periodically or when something happens that will
 * trigger a state change
 */
1248
static void ab8500_chargalg_algorithm(struct ab8500_chargalg *di)
1249
{
1250
	struct power_supply_battery_info *bi = di->bm->bi;
1251
	int charger_status;
1252
	int ret;
1253 1254 1255

	/* Collect data from all power_supply class devices */
	class_for_each_device(power_supply_class, NULL,
1256
		di->chargalg_psy, ab8500_chargalg_get_ext_psy_data);
1257

1258 1259 1260
	ab8500_chargalg_end_of_charge(di);
	ab8500_chargalg_check_temp(di);
	ab8500_chargalg_check_charger_voltage(di);
1261

1262
	charger_status = ab8500_chargalg_check_charger_connection(di);
1263 1264

	if (is_ab8500(di->parent)) {
1265
		ret = ab8500_chargalg_check_charger_enable(di);
1266 1267 1268 1269 1270
		if (ret < 0)
			dev_err(di->dev, "Checking charger is enabled error"
					": Returned Value %d\n", ret);
	}

1271 1272 1273 1274 1275 1276
	/*
	 * First check if we have a charger connected.
	 * Also we don't allow charging of unknown batteries if configured
	 * this way
	 */
	if (!charger_status ||
1277
		(di->events.batt_unknown && !di->bm->chg_unknown_bat)) {
1278 1279
		if (di->charge_state != STATE_HANDHELD) {
			di->events.safety_timer_expired = false;
1280
			ab8500_chargalg_state_to(di, STATE_HANDHELD_INIT);
1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292
		}
	}

	/* If suspended, we should not continue checking the flags */
	else if (di->charge_state == STATE_SUSPENDED_INIT ||
		di->charge_state == STATE_SUSPENDED) {
		/* We don't do anything here, just don,t continue */
	}

	/* Safety timer expiration */
	else if (di->events.safety_timer_expired) {
		if (di->charge_state != STATE_SAFETY_TIMER_EXPIRED)
1293
			ab8500_chargalg_state_to(di,
1294 1295 1296 1297 1298 1299 1300 1301 1302 1303
				STATE_SAFETY_TIMER_EXPIRED_INIT);
	}
	/*
	 * Check if any interrupts has occured
	 * that will prevent us from charging
	 */

	/* Battery removed */
	else if (di->events.batt_rem) {
		if (di->charge_state != STATE_BATT_REMOVED)
1304
			ab8500_chargalg_state_to(di, STATE_BATT_REMOVED_INIT);
1305 1306 1307 1308 1309 1310 1311 1312 1313
	}
	/* Main or USB charger not ok. */
	else if (di->events.mainextchnotok || di->events.usbchargernotok) {
		/*
		 * If vbus_collapsed is set, we have to lower the charger
		 * current, which is done in the normal state below
		 */
		if (di->charge_state != STATE_CHG_NOT_OK &&
				!di->events.vbus_collapsed)
1314
			ab8500_chargalg_state_to(di, STATE_CHG_NOT_OK_INIT);
1315 1316 1317 1318 1319 1320 1321 1322
	}
	/* VBUS, Main or VBAT OVV. */
	else if (di->events.vbus_ovv ||
			di->events.main_ovv ||
			di->events.batt_ovv ||
			!di->chg_info.usb_chg_ok ||
			!di->chg_info.ac_chg_ok) {
		if (di->charge_state != STATE_OVV_PROTECT)
1323
			ab8500_chargalg_state_to(di, STATE_OVV_PROTECT_INIT);
1324 1325 1326 1327 1328
	}
	/* USB Thermal, stop charging */
	else if (di->events.main_thermal_prot ||
		di->events.usb_thermal_prot) {
		if (di->charge_state != STATE_HW_TEMP_PROTECT)
1329
			ab8500_chargalg_state_to(di,
1330 1331 1332 1333 1334
				STATE_HW_TEMP_PROTECT_INIT);
	}
	/* Battery temp over/under */
	else if (di->events.btemp_underover) {
		if (di->charge_state != STATE_TEMP_UNDEROVER)
1335
			ab8500_chargalg_state_to(di,
1336 1337 1338 1339 1340 1341
				STATE_TEMP_UNDEROVER_INIT);
	}
	/* Watchdog expired */
	else if (di->events.ac_wd_expired ||
		di->events.usb_wd_expired) {
		if (di->charge_state != STATE_WD_EXPIRED)
1342
			ab8500_chargalg_state_to(di, STATE_WD_EXPIRED_INIT);
1343 1344 1345 1346
	}
	/* Battery temp high/low */
	else if (di->events.btemp_lowhigh) {
		if (di->charge_state != STATE_TEMP_LOWHIGH)
1347
			ab8500_chargalg_state_to(di, STATE_TEMP_LOWHIGH_INIT);
1348 1349 1350 1351 1352 1353 1354
	}

	dev_dbg(di->dev,
		"[CHARGALG] Vb %d Ib_avg %d Ib_inst %d Tb %d Cap %d Maint %d "
		"State %s Active_chg %d Chg_status %d AC %d USB %d "
		"AC_online %d USB_online %d AC_CV %d USB_CV %d AC_I %d "
		"USB_I %d AC_Vset %d AC_Iset %d USB_Vset %d USB_Iset %d\n",
1355
		di->batt_data.volt_uv,
1356
		di->batt_data.avg_curr_ua,
1357
		di->batt_data.inst_curr_ua,
1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369
		di->batt_data.temp,
		di->batt_data.percent,
		di->maintenance_chg,
		states[di->charge_state],
		di->chg_info.charger_type,
		di->charge_status,
		di->chg_info.conn_chg & AC_CHG,
		di->chg_info.conn_chg & USB_CHG,
		di->chg_info.online_chg & AC_CHG,
		di->chg_info.online_chg & USB_CHG,
		di->events.ac_cv_active,
		di->events.usb_cv_active,
1370 1371
		di->chg_info.ac_curr_ua,
		di->chg_info.usb_curr_ua,
1372
		di->chg_info.ac_vset_uv,
1373
		di->chg_info.ac_iset_ua,
1374
		di->chg_info.usb_vset_uv,
1375
		di->chg_info.usb_iset_ua);
1376 1377 1378

	switch (di->charge_state) {
	case STATE_HANDHELD_INIT:
1379
		ab8500_chargalg_stop_charging(di);
1380
		di->charge_status = POWER_SUPPLY_STATUS_DISCHARGING;
1381
		ab8500_chargalg_state_to(di, STATE_HANDHELD);
1382
		fallthrough;
1383 1384 1385 1386 1387 1388

	case STATE_HANDHELD:
		break;

	case STATE_SUSPENDED_INIT:
		if (di->susp_status.ac_suspended)
1389
			ab8500_chargalg_ac_en(di, false, 0, 0);
1390
		if (di->susp_status.usb_suspended)
1391 1392 1393
			ab8500_chargalg_usb_en(di, false, 0, 0);
		ab8500_chargalg_stop_safety_timer(di);
		ab8500_chargalg_stop_maintenance_timer(di);
1394 1395
		di->charge_status = POWER_SUPPLY_STATUS_NOT_CHARGING;
		di->maintenance_chg = false;
1396
		ab8500_chargalg_state_to(di, STATE_SUSPENDED);
1397
		power_supply_changed(di->chargalg_psy);
1398
		fallthrough;
1399 1400 1401 1402 1403 1404

	case STATE_SUSPENDED:
		/* CHARGING is suspended */
		break;

	case STATE_BATT_REMOVED_INIT:
1405 1406
		ab8500_chargalg_stop_charging(di);
		ab8500_chargalg_state_to(di, STATE_BATT_REMOVED);
1407
		fallthrough;
1408 1409 1410

	case STATE_BATT_REMOVED:
		if (!di->events.batt_rem)
1411
			ab8500_chargalg_state_to(di, STATE_NORMAL_INIT);
1412 1413 1414
		break;

	case STATE_HW_TEMP_PROTECT_INIT:
1415 1416
		ab8500_chargalg_stop_charging(di);
		ab8500_chargalg_state_to(di, STATE_HW_TEMP_PROTECT);
1417
		fallthrough;
1418 1419 1420 1421

	case STATE_HW_TEMP_PROTECT:
		if (!di->events.main_thermal_prot &&
				!di->events.usb_thermal_prot)
1422
			ab8500_chargalg_state_to(di, STATE_NORMAL_INIT);
1423 1424 1425
		break;

	case STATE_OVV_PROTECT_INIT:
1426 1427
		ab8500_chargalg_stop_charging(di);
		ab8500_chargalg_state_to(di, STATE_OVV_PROTECT);
1428
		fallthrough;
1429 1430 1431 1432 1433 1434 1435

	case STATE_OVV_PROTECT:
		if (!di->events.vbus_ovv &&
				!di->events.main_ovv &&
				!di->events.batt_ovv &&
				di->chg_info.usb_chg_ok &&
				di->chg_info.ac_chg_ok)
1436
			ab8500_chargalg_state_to(di, STATE_NORMAL_INIT);
1437 1438 1439
		break;

	case STATE_CHG_NOT_OK_INIT:
1440 1441
		ab8500_chargalg_stop_charging(di);
		ab8500_chargalg_state_to(di, STATE_CHG_NOT_OK);
1442
		fallthrough;
1443 1444 1445 1446

	case STATE_CHG_NOT_OK:
		if (!di->events.mainextchnotok &&
				!di->events.usbchargernotok)
1447
			ab8500_chargalg_state_to(di, STATE_NORMAL_INIT);
1448 1449 1450
		break;

	case STATE_SAFETY_TIMER_EXPIRED_INIT:
1451 1452
		ab8500_chargalg_stop_charging(di);
		ab8500_chargalg_state_to(di, STATE_SAFETY_TIMER_EXPIRED);
1453
		fallthrough;
1454 1455 1456 1457 1458 1459

	case STATE_SAFETY_TIMER_EXPIRED:
		/* We exit this state when charger is removed */
		break;

	case STATE_NORMAL_INIT:
1460 1461
		if (bi->constant_charge_current_max_ua == 0)
			/* "charging" with 0 uA */
1462
			ab8500_chargalg_stop_charging(di);
1463
		else {
1464
			ab8500_chargalg_start_charging(di,
1465
				bi->constant_charge_voltage_max_uv,
1466
				bi->constant_charge_current_max_ua);
1467 1468
		}

1469 1470 1471
		ab8500_chargalg_state_to(di, STATE_NORMAL);
		ab8500_chargalg_start_safety_timer(di);
		ab8500_chargalg_stop_maintenance_timer(di);
1472 1473 1474 1475
		init_maxim_chg_curr(di);
		di->charge_status = POWER_SUPPLY_STATUS_CHARGING;
		di->eoc_cnt = 0;
		di->maintenance_chg = false;
1476
		power_supply_changed(di->chargalg_psy);
1477 1478 1479 1480 1481 1482 1483

		break;

	case STATE_NORMAL:
		handle_maxim_chg_curr(di);
		if (di->charge_status == POWER_SUPPLY_STATUS_FULL &&
			di->maintenance_chg) {
1484
			if (di->bm->no_maintenance)
1485
				ab8500_chargalg_state_to(di,
1486 1487
					STATE_WAIT_FOR_RECHARGE_INIT);
			else
1488
				ab8500_chargalg_state_to(di,
1489 1490 1491 1492 1493 1494
					STATE_MAINTENANCE_A_INIT);
		}
		break;

	/* This state will be used when the maintenance state is disabled */
	case STATE_WAIT_FOR_RECHARGE_INIT:
1495 1496
		ab8500_chargalg_hold_charging(di);
		ab8500_chargalg_state_to(di, STATE_WAIT_FOR_RECHARGE);
1497
		fallthrough;
1498 1499

	case STATE_WAIT_FOR_RECHARGE:
1500
		if (di->batt_data.percent <= AB8500_RECHARGE_CAP)
1501
			ab8500_chargalg_state_to(di, STATE_NORMAL_INIT);
1502 1503 1504
		break;

	case STATE_MAINTENANCE_A_INIT:
1505 1506
		ab8500_chargalg_stop_safety_timer(di);
		ab8500_chargalg_start_maintenance_timer(di,
1507
			di->bm->bat_type->maint_a_chg_timer_h);
1508
		ab8500_chargalg_start_charging(di,
1509 1510
			di->bm->bat_type->maint_a_vol_lvl,
			di->bm->bat_type->maint_a_cur_lvl);
1511
		ab8500_chargalg_state_to(di, STATE_MAINTENANCE_A);
1512
		power_supply_changed(di->chargalg_psy);
1513
		fallthrough;
1514 1515 1516

	case STATE_MAINTENANCE_A:
		if (di->events.maintenance_timer_expired) {
1517 1518
			ab8500_chargalg_stop_maintenance_timer(di);
			ab8500_chargalg_state_to(di, STATE_MAINTENANCE_B_INIT);
1519 1520 1521 1522
		}
		break;

	case STATE_MAINTENANCE_B_INIT:
1523
		ab8500_chargalg_start_maintenance_timer(di,
1524
			di->bm->bat_type->maint_b_chg_timer_h);
1525
		ab8500_chargalg_start_charging(di,
1526 1527
			di->bm->bat_type->maint_b_vol_lvl,
			di->bm->bat_type->maint_b_cur_lvl);
1528
		ab8500_chargalg_state_to(di, STATE_MAINTENANCE_B);
1529
		power_supply_changed(di->chargalg_psy);
1530
		fallthrough;
1531 1532 1533

	case STATE_MAINTENANCE_B:
		if (di->events.maintenance_timer_expired) {
1534 1535
			ab8500_chargalg_stop_maintenance_timer(di);
			ab8500_chargalg_state_to(di, STATE_NORMAL_INIT);
1536 1537 1538 1539
		}
		break;

	case STATE_TEMP_LOWHIGH_INIT:
1540
		ab8500_chargalg_start_charging(di,
1541 1542
			di->bm->bat_type->low_high_vol_lvl,
			di->bm->bat_type->low_high_cur_lvl);
1543
		ab8500_chargalg_stop_maintenance_timer(di);
1544
		di->charge_status = POWER_SUPPLY_STATUS_CHARGING;
1545
		ab8500_chargalg_state_to(di, STATE_TEMP_LOWHIGH);
1546
		power_supply_changed(di->chargalg_psy);
1547
		fallthrough;
1548 1549 1550

	case STATE_TEMP_LOWHIGH:
		if (!di->events.btemp_lowhigh)
1551
			ab8500_chargalg_state_to(di, STATE_NORMAL_INIT);
1552 1553 1554
		break;

	case STATE_WD_EXPIRED_INIT:
1555 1556
		ab8500_chargalg_stop_charging(di);
		ab8500_chargalg_state_to(di, STATE_WD_EXPIRED);
1557
		fallthrough;
1558 1559 1560 1561

	case STATE_WD_EXPIRED:
		if (!di->events.ac_wd_expired &&
				!di->events.usb_wd_expired)
1562
			ab8500_chargalg_state_to(di, STATE_NORMAL_INIT);
1563 1564 1565
		break;

	case STATE_TEMP_UNDEROVER_INIT:
1566 1567
		ab8500_chargalg_stop_charging(di);
		ab8500_chargalg_state_to(di, STATE_TEMP_UNDEROVER);
1568
		fallthrough;
1569 1570 1571

	case STATE_TEMP_UNDEROVER:
		if (!di->events.btemp_underover)
1572
			ab8500_chargalg_state_to(di, STATE_NORMAL_INIT);
1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583
		break;
	}

	/* Start charging directly if the new state is a charge state */
	if (di->charge_state == STATE_NORMAL_INIT ||
			di->charge_state == STATE_MAINTENANCE_A_INIT ||
			di->charge_state == STATE_MAINTENANCE_B_INIT)
		queue_work(di->chargalg_wq, &di->chargalg_work);
}

/**
1584
 * ab8500_chargalg_periodic_work() - Periodic work for the algorithm
1585 1586 1587 1588
 * @work:	pointer to the work_struct structure
 *
 * Work queue function for the charging algorithm
 */
1589
static void ab8500_chargalg_periodic_work(struct work_struct *work)
1590
{
1591 1592
	struct ab8500_chargalg *di = container_of(work,
		struct ab8500_chargalg, chargalg_periodic_work.work);
1593

1594
	ab8500_chargalg_algorithm(di);
1595 1596 1597 1598 1599 1600 1601 1602

	/*
	 * If a charger is connected then the battery has to be monitored
	 * frequently, else the work can be delayed.
	 */
	if (di->chg_info.conn_chg)
		queue_delayed_work(di->chargalg_wq,
			&di->chargalg_periodic_work,
1603
			di->bm->interval_charging * HZ);
1604 1605 1606
	else
		queue_delayed_work(di->chargalg_wq,
			&di->chargalg_periodic_work,
1607
			di->bm->interval_not_charging * HZ);
1608 1609 1610
}

/**
1611
 * ab8500_chargalg_wd_work() - periodic work to kick the charger watchdog
1612 1613 1614 1615
 * @work:	pointer to the work_struct structure
 *
 * Work queue function for kicking the charger watchdog
 */
1616
static void ab8500_chargalg_wd_work(struct work_struct *work)
1617 1618
{
	int ret;
1619 1620
	struct ab8500_chargalg *di = container_of(work,
		struct ab8500_chargalg, chargalg_wd_work.work);
1621

1622
	ret = ab8500_chargalg_kick_watchdog(di);
1623 1624 1625 1626 1627 1628 1629 1630
	if (ret < 0)
		dev_err(di->dev, "failed to kick watchdog\n");

	queue_delayed_work(di->chargalg_wq,
		&di->chargalg_wd_work, CHG_WD_INTERVAL);
}

/**
1631
 * ab8500_chargalg_work() - Work to run the charging algorithm instantly
1632 1633 1634 1635
 * @work:	pointer to the work_struct structure
 *
 * Work queue function for calling the charging algorithm
 */
1636
static void ab8500_chargalg_work(struct work_struct *work)
1637
{
1638 1639
	struct ab8500_chargalg *di = container_of(work,
		struct ab8500_chargalg, chargalg_work);
1640

1641
	ab8500_chargalg_algorithm(di);
1642 1643 1644
}

/**
1645
 * ab8500_chargalg_get_property() - get the chargalg properties
1646 1647 1648 1649 1650 1651 1652 1653 1654 1655
 * @psy:	pointer to the power_supply structure
 * @psp:	pointer to the power_supply_property structure
 * @val:	pointer to the power_supply_propval union
 *
 * This function gets called when an application tries to get the
 * chargalg properties by reading the sysfs files.
 * status:     charging/discharging/full/unknown
 * health:     health of the battery
 * Returns error code in case of failure else 0 on success
 */
1656
static int ab8500_chargalg_get_property(struct power_supply *psy,
1657 1658 1659
	enum power_supply_property psp,
	union power_supply_propval *val)
{
1660
	struct ab8500_chargalg *di = power_supply_get_drvdata(psy);
1661 1662 1663 1664 1665 1666 1667 1668 1669

	switch (psp) {
	case POWER_SUPPLY_PROP_STATUS:
		val->intval = di->charge_status;
		break;
	case POWER_SUPPLY_PROP_HEALTH:
		if (di->events.batt_ovv) {
			val->intval = POWER_SUPPLY_HEALTH_OVERVOLTAGE;
		} else if (di->events.btemp_underover) {
1670
			if (di->batt_data.temp <= di->bm->bi->temp_min)
1671 1672 1673
				val->intval = POWER_SUPPLY_HEALTH_COLD;
			else
				val->intval = POWER_SUPPLY_HEALTH_OVERHEAT;
1674 1675 1676
		} else if (di->charge_state == STATE_SAFETY_TIMER_EXPIRED ||
			   di->charge_state == STATE_SAFETY_TIMER_EXPIRED_INIT) {
			val->intval = POWER_SUPPLY_HEALTH_UNSPEC_FAILURE;
1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688
		} else {
			val->intval = POWER_SUPPLY_HEALTH_GOOD;
		}
		break;
	default:
		return -EINVAL;
	}
	return 0;
}

/* Exposure to the sysfs interface */

1689
static ssize_t ab8500_chargalg_en_show(struct ab8500_chargalg *di,
1690 1691
				       char *buf)
{
1692 1693 1694 1695 1696
	return sprintf(buf, "%d\n",
		       di->susp_status.ac_suspended &&
		       di->susp_status.usb_suspended);
}

1697
static ssize_t ab8500_chargalg_en_store(struct ab8500_chargalg *di,
1698
	const char *buf, size_t length)
1699
{
1700
	long param;
1701 1702 1703
	int ac_usb;
	int ret;

1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725
	ret = kstrtol(buf, 10, &param);
	if (ret < 0)
		return ret;

	ac_usb = param;
	switch (ac_usb) {
	case 0:
		/* Disable charging */
		di->susp_status.ac_suspended = true;
		di->susp_status.usb_suspended = true;
		di->susp_status.suspended_change = true;
		/* Trigger a state change */
		queue_work(di->chargalg_wq,
			&di->chargalg_work);
		break;
	case 1:
		/* Enable AC Charging */
		di->susp_status.ac_suspended = false;
		di->susp_status.suspended_change = true;
		/* Trigger a state change */
		queue_work(di->chargalg_wq,
			&di->chargalg_work);
1726
		break;
1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739
	case 2:
		/* Enable USB charging */
		di->susp_status.usb_suspended = false;
		di->susp_status.suspended_change = true;
		/* Trigger a state change */
		queue_work(di->chargalg_wq,
			&di->chargalg_work);
		break;
	default:
		dev_info(di->dev, "Wrong input\n"
			"Enter 0. Disable AC/USB Charging\n"
			"1. Enable AC charging\n"
			"2. Enable USB Charging\n");
1740
	}
1741 1742 1743
	return strlen(buf);
}

1744 1745 1746
static struct ab8500_chargalg_sysfs_entry ab8500_chargalg_en_charger =
	__ATTR(chargalg, 0644, ab8500_chargalg_en_show,
				ab8500_chargalg_en_store);
1747

1748
static ssize_t ab8500_chargalg_sysfs_show(struct kobject *kobj,
1749 1750
	struct attribute *attr, char *buf)
{
1751 1752
	struct ab8500_chargalg_sysfs_entry *entry = container_of(attr,
		struct ab8500_chargalg_sysfs_entry, attr);
1753

1754 1755
	struct ab8500_chargalg *di = container_of(kobj,
		struct ab8500_chargalg, chargalg_kobject);
1756 1757 1758 1759 1760 1761 1762

	if (!entry->show)
		return -EIO;

	return entry->show(di, buf);
}

1763
static ssize_t ab8500_chargalg_sysfs_charger(struct kobject *kobj,
1764
	struct attribute *attr, const char *buf, size_t length)
1765
{
1766 1767
	struct ab8500_chargalg_sysfs_entry *entry = container_of(attr,
		struct ab8500_chargalg_sysfs_entry, attr);
1768

1769 1770
	struct ab8500_chargalg *di = container_of(kobj,
		struct ab8500_chargalg, chargalg_kobject);
1771 1772 1773 1774 1775 1776

	if (!entry->store)
		return -EIO;

	return entry->store(di, buf, length);
}
1777

1778 1779
static struct attribute *ab8500_chargalg_chg[] = {
	&ab8500_chargalg_en_charger.attr,
1780
	NULL,
1781 1782
};

1783 1784 1785
static const struct sysfs_ops ab8500_chargalg_sysfs_ops = {
	.show = ab8500_chargalg_sysfs_show,
	.store = ab8500_chargalg_sysfs_charger,
1786 1787
};

1788 1789 1790
static struct kobj_type ab8500_chargalg_ktype = {
	.sysfs_ops = &ab8500_chargalg_sysfs_ops,
	.default_attrs = ab8500_chargalg_chg,
1791 1792 1793
};

/**
1794 1795
 * ab8500_chargalg_sysfs_exit() - de-init of sysfs entry
 * @di:                pointer to the struct ab8500_chargalg
1796 1797 1798
 *
 * This function removes the entry in sysfs.
 */
1799
static void ab8500_chargalg_sysfs_exit(struct ab8500_chargalg *di)
1800 1801 1802 1803 1804
{
	kobject_del(&di->chargalg_kobject);
}

/**
1805 1806
 * ab8500_chargalg_sysfs_init() - init of sysfs entry
 * @di:                pointer to the struct ab8500_chargalg
1807 1808 1809 1810
 *
 * This function adds an entry in sysfs.
 * Returns error code in case of failure else 0(on success)
 */
1811
static int ab8500_chargalg_sysfs_init(struct ab8500_chargalg *di)
1812 1813 1814 1815
{
	int ret = 0;

	ret = kobject_init_and_add(&di->chargalg_kobject,
1816 1817
		&ab8500_chargalg_ktype,
		NULL, "ab8500_chargalg");
1818 1819 1820 1821 1822 1823 1824
	if (ret < 0)
		dev_err(di->dev, "failed to create sysfs entry\n");

	return ret;
}
/* Exposure to the sysfs interface <<END>> */

1825
static int __maybe_unused ab8500_chargalg_resume(struct device *dev)
1826
{
1827
	struct ab8500_chargalg *di = dev_get_drvdata(dev);
1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841

	/* Kick charger watchdog if charging (any charger online) */
	if (di->chg_info.online_chg)
		queue_delayed_work(di->chargalg_wq, &di->chargalg_wd_work, 0);

	/*
	 * Run the charging algorithm directly to be sure we don't
	 * do it too seldom
	 */
	queue_delayed_work(di->chargalg_wq, &di->chargalg_periodic_work, 0);

	return 0;
}

1842
static int __maybe_unused ab8500_chargalg_suspend(struct device *dev)
1843
{
1844
	struct ab8500_chargalg *di = dev_get_drvdata(dev);
1845 1846 1847 1848 1849 1850 1851 1852 1853

	if (di->chg_info.online_chg)
		cancel_delayed_work_sync(&di->chargalg_wd_work);

	cancel_delayed_work_sync(&di->chargalg_periodic_work);

	return 0;
}

1854 1855 1856 1857
static char *supply_interface[] = {
	"ab8500_fg",
};

1858
static const struct power_supply_desc ab8500_chargalg_desc = {
1859
	.name			= "ab8500_chargalg",
1860
	.type			= POWER_SUPPLY_TYPE_BATTERY,
1861 1862 1863 1864
	.properties		= ab8500_chargalg_props,
	.num_properties		= ARRAY_SIZE(ab8500_chargalg_props),
	.get_property		= ab8500_chargalg_get_property,
	.external_power_changed	= ab8500_chargalg_external_power_changed,
1865 1866
};

1867
static int ab8500_chargalg_bind(struct device *dev, struct device *master,
1868
				void *data)
1869
{
1870
	struct ab8500_chargalg *di = dev_get_drvdata(dev);
1871

1872
	/* Create a work queue for the chargalg */
1873
	di->chargalg_wq = alloc_ordered_workqueue("ab8500_chargalg_wq",
1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884
						  WQ_MEM_RECLAIM);
	if (di->chargalg_wq == NULL) {
		dev_err(di->dev, "failed to create work queue\n");
		return -ENOMEM;
	}

	/* Run the charging algorithm */
	queue_delayed_work(di->chargalg_wq, &di->chargalg_periodic_work, 0);

	return 0;
}
1885

1886
static void ab8500_chargalg_unbind(struct device *dev, struct device *master,
1887 1888
				   void *data)
{
1889
	struct ab8500_chargalg *di = dev_get_drvdata(dev);
1890 1891

	/* Stop all timers and work */
L
Lee Jones 已提交
1892 1893 1894 1895 1896 1897 1898
	hrtimer_cancel(&di->safety_timer);
	hrtimer_cancel(&di->maintenance_timer);

	cancel_delayed_work_sync(&di->chargalg_periodic_work);
	cancel_delayed_work_sync(&di->chargalg_wd_work);
	cancel_work_sync(&di->chargalg_work);

1899 1900
	/* Delete the work queue */
	destroy_workqueue(di->chargalg_wq);
1901
	flush_scheduled_work();
1902 1903
}

1904 1905 1906
static const struct component_ops ab8500_chargalg_component_ops = {
	.bind = ab8500_chargalg_bind,
	.unbind = ab8500_chargalg_unbind,
1907 1908
};

1909
static int ab8500_chargalg_probe(struct platform_device *pdev)
1910
{
1911
	struct device *dev = &pdev->dev;
1912
	struct power_supply_config psy_cfg = {};
1913
	struct ab8500_chargalg *di;
1914 1915
	int ret = 0;

1916 1917
	di = devm_kzalloc(dev, sizeof(*di), GFP_KERNEL);
	if (!di)
1918
		return -ENOMEM;
1919

1920
	di->bm = &ab8500_bm_data;
1921

1922
	/* get device struct and parent */
1923
	di->dev = dev;
1924
	di->parent = dev_get_drvdata(pdev->dev.parent);
1925

1926 1927
	psy_cfg.supplied_to = supply_interface;
	psy_cfg.num_supplicants = ARRAY_SIZE(supply_interface);
1928
	psy_cfg.drv_data = di;
1929

1930
	/* Initilialize safety timer */
L
Lee Jones 已提交
1931
	hrtimer_init(&di->safety_timer, CLOCK_REALTIME, HRTIMER_MODE_ABS);
1932
	di->safety_timer.function = ab8500_chargalg_safety_timer_expired;
1933 1934

	/* Initilialize maintenance timer */
L
Lee Jones 已提交
1935
	hrtimer_init(&di->maintenance_timer, CLOCK_REALTIME, HRTIMER_MODE_ABS);
1936
	di->maintenance_timer.function =
1937
		ab8500_chargalg_maintenance_timer_expired;
1938 1939

	/* Init work for chargalg */
1940
	INIT_DEFERRABLE_WORK(&di->chargalg_periodic_work,
1941
		ab8500_chargalg_periodic_work);
1942
	INIT_DEFERRABLE_WORK(&di->chargalg_wd_work,
1943
		ab8500_chargalg_wd_work);
1944 1945

	/* Init work for chargalg */
1946
	INIT_WORK(&di->chargalg_work, ab8500_chargalg_work);
1947 1948 1949 1950 1951

	/* To detect charger at startup */
	di->chg_info.prev_conn_chg = -1;

	/* Register chargalg power supply class */
1952
	di->chargalg_psy = devm_power_supply_register(di->dev,
1953
						 &ab8500_chargalg_desc,
1954 1955
						 &psy_cfg);
	if (IS_ERR(di->chargalg_psy)) {
1956
		dev_err(di->dev, "failed to register chargalg psy\n");
1957
		return PTR_ERR(di->chargalg_psy);
1958 1959 1960 1961 1962
	}

	platform_set_drvdata(pdev, di);

	/* sysfs interface to enable/disable charging from user space */
1963
	ret = ab8500_chargalg_sysfs_init(di);
1964 1965
	if (ret) {
		dev_err(di->dev, "failed to create sysfs entry\n");
1966
		return ret;
1967 1968 1969
	}

	dev_info(di->dev, "probe success\n");
1970
	return component_add(dev, &ab8500_chargalg_component_ops);
1971
}
1972

1973
static int ab8500_chargalg_remove(struct platform_device *pdev)
1974
{
1975
	struct ab8500_chargalg *di = platform_get_drvdata(pdev);
1976

1977
	component_del(&pdev->dev, &ab8500_chargalg_component_ops);
1978 1979

	/* sysfs interface to enable/disable charging from user space */
1980
	ab8500_chargalg_sysfs_exit(di);
1981 1982

	return 0;
1983 1984
}

1985
static SIMPLE_DEV_PM_OPS(ab8500_chargalg_pm_ops, ab8500_chargalg_suspend, ab8500_chargalg_resume);
1986

1987 1988 1989 1990 1991
static const struct of_device_id ab8500_chargalg_match[] = {
	{ .compatible = "stericsson,ab8500-chargalg", },
	{ },
};

1992 1993 1994
struct platform_driver ab8500_chargalg_driver = {
	.probe = ab8500_chargalg_probe,
	.remove = ab8500_chargalg_remove,
1995
	.driver = {
1996
		.name = "ab8500_chargalg",
1997
		.of_match_table = ab8500_chargalg_match,
1998
		.pm = &ab8500_chargalg_pm_ops,
1999 2000 2001 2002
	},
};
MODULE_LICENSE("GPL v2");
MODULE_AUTHOR("Johan Palsson, Karl Komierowski");
2003 2004
MODULE_ALIAS("platform:ab8500-chargalg");
MODULE_DESCRIPTION("ab8500 battery charging algorithm");