1d2912cb1SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
28c0984e5SSebastian Reichel /*
38c0984e5SSebastian Reichel  * Battery driver for Marvell 88PM860x PMIC
48c0984e5SSebastian Reichel  *
58c0984e5SSebastian Reichel  * Copyright (c) 2012 Marvell International Ltd.
68c0984e5SSebastian Reichel  * Author:	Jett Zhou <jtzhou@marvell.com>
78c0984e5SSebastian Reichel  *		Haojian Zhuang <haojian.zhuang@marvell.com>
88c0984e5SSebastian Reichel  */
98c0984e5SSebastian Reichel 
108c0984e5SSebastian Reichel #include <linux/kernel.h>
118c0984e5SSebastian Reichel #include <linux/module.h>
128c0984e5SSebastian Reichel #include <linux/platform_device.h>
138c0984e5SSebastian Reichel #include <linux/slab.h>
148c0984e5SSebastian Reichel #include <linux/power_supply.h>
158c0984e5SSebastian Reichel #include <linux/mfd/88pm860x.h>
168c0984e5SSebastian Reichel #include <linux/delay.h>
178c0984e5SSebastian Reichel #include <linux/uaccess.h>
188c0984e5SSebastian Reichel #include <asm/div64.h>
198c0984e5SSebastian Reichel 
208c0984e5SSebastian Reichel /* bit definitions of Status Query Interface 2 */
218c0984e5SSebastian Reichel #define STATUS2_CHG		(1 << 2)
228c0984e5SSebastian Reichel 
238c0984e5SSebastian Reichel /* bit definitions of Reset Out Register */
248c0984e5SSebastian Reichel #define RESET_SW_PD		(1 << 7)
258c0984e5SSebastian Reichel 
268c0984e5SSebastian Reichel /* bit definitions of PreReg 1 */
278c0984e5SSebastian Reichel #define PREREG1_90MA		(0x0)
288c0984e5SSebastian Reichel #define PREREG1_180MA		(0x1)
298c0984e5SSebastian Reichel #define PREREG1_450MA		(0x4)
308c0984e5SSebastian Reichel #define PREREG1_540MA		(0x5)
318c0984e5SSebastian Reichel #define PREREG1_1350MA		(0xE)
328c0984e5SSebastian Reichel #define PREREG1_VSYS_4_5V	(3 << 4)
338c0984e5SSebastian Reichel 
348c0984e5SSebastian Reichel /* bit definitions of Charger Control 1 Register */
358c0984e5SSebastian Reichel #define CC1_MODE_OFF		(0)
368c0984e5SSebastian Reichel #define CC1_MODE_PRECHARGE	(1)
378c0984e5SSebastian Reichel #define CC1_MODE_FASTCHARGE	(2)
388c0984e5SSebastian Reichel #define CC1_MODE_PULSECHARGE	(3)
398c0984e5SSebastian Reichel #define CC1_ITERM_20MA		(0 << 2)
408c0984e5SSebastian Reichel #define CC1_ITERM_60MA		(2 << 2)
418c0984e5SSebastian Reichel #define CC1_VFCHG_4_2V		(9 << 4)
428c0984e5SSebastian Reichel 
438c0984e5SSebastian Reichel /* bit definitions of Charger Control 2 Register */
448c0984e5SSebastian Reichel #define CC2_ICHG_100MA		(0x1)
458c0984e5SSebastian Reichel #define CC2_ICHG_500MA		(0x9)
468c0984e5SSebastian Reichel #define CC2_ICHG_1000MA		(0x13)
478c0984e5SSebastian Reichel 
488c0984e5SSebastian Reichel /* bit definitions of Charger Control 3 Register */
498c0984e5SSebastian Reichel #define CC3_180MIN_TIMEOUT	(0x6 << 4)
508c0984e5SSebastian Reichel #define CC3_270MIN_TIMEOUT	(0x7 << 4)
518c0984e5SSebastian Reichel #define CC3_360MIN_TIMEOUT	(0xA << 4)
528c0984e5SSebastian Reichel #define CC3_DISABLE_TIMEOUT	(0xF << 4)
538c0984e5SSebastian Reichel 
548c0984e5SSebastian Reichel /* bit definitions of Charger Control 4 Register */
558c0984e5SSebastian Reichel #define CC4_IPRE_40MA		(7)
568c0984e5SSebastian Reichel #define CC4_VPCHG_3_2V		(3 << 4)
578c0984e5SSebastian Reichel #define CC4_IFCHG_MON_EN	(1 << 6)
588c0984e5SSebastian Reichel #define CC4_BTEMP_MON_EN	(1 << 7)
598c0984e5SSebastian Reichel 
608c0984e5SSebastian Reichel /* bit definitions of Charger Control 6 Register */
618c0984e5SSebastian Reichel #define CC6_BAT_OV_EN		(1 << 2)
628c0984e5SSebastian Reichel #define CC6_BAT_UV_EN		(1 << 3)
638c0984e5SSebastian Reichel #define CC6_UV_VBAT_SET		(0x3 << 6)	/* 2.8v */
648c0984e5SSebastian Reichel 
658c0984e5SSebastian Reichel /* bit definitions of Charger Control 7 Register */
668c0984e5SSebastian Reichel #define CC7_BAT_REM_EN		(1 << 3)
678c0984e5SSebastian Reichel #define CC7_IFSM_EN		(1 << 7)
688c0984e5SSebastian Reichel 
698c0984e5SSebastian Reichel /* bit definitions of Measurement Enable 1 Register */
708c0984e5SSebastian Reichel #define MEAS1_VBAT		(1 << 0)
718c0984e5SSebastian Reichel 
728c0984e5SSebastian Reichel /* bit definitions of Measurement Enable 3 Register */
738c0984e5SSebastian Reichel #define MEAS3_IBAT_EN		(1 << 0)
748c0984e5SSebastian Reichel #define MEAS3_CC_EN		(1 << 2)
758c0984e5SSebastian Reichel 
768c0984e5SSebastian Reichel #define FSM_INIT		0
778c0984e5SSebastian Reichel #define FSM_DISCHARGE		1
788c0984e5SSebastian Reichel #define FSM_PRECHARGE		2
798c0984e5SSebastian Reichel #define FSM_FASTCHARGE		3
808c0984e5SSebastian Reichel 
818c0984e5SSebastian Reichel #define PRECHARGE_THRESHOLD	3100
828c0984e5SSebastian Reichel #define POWEROFF_THRESHOLD	3400
838c0984e5SSebastian Reichel #define CHARGE_THRESHOLD	4000
848c0984e5SSebastian Reichel #define DISCHARGE_THRESHOLD	4180
858c0984e5SSebastian Reichel 
868c0984e5SSebastian Reichel /* over-temperature on PM8606 setting */
878c0984e5SSebastian Reichel #define OVER_TEMP_FLAG		(1 << 6)
888c0984e5SSebastian Reichel #define OVTEMP_AUTORECOVER	(1 << 3)
898c0984e5SSebastian Reichel 
908c0984e5SSebastian Reichel /* over-voltage protect on vchg setting mv */
918c0984e5SSebastian Reichel #define VCHG_NORMAL_LOW		4200
928c0984e5SSebastian Reichel #define VCHG_NORMAL_CHECK	5800
938c0984e5SSebastian Reichel #define VCHG_NORMAL_HIGH	6000
948c0984e5SSebastian Reichel #define VCHG_OVP_LOW		5500
958c0984e5SSebastian Reichel 
968c0984e5SSebastian Reichel struct pm860x_charger_info {
978c0984e5SSebastian Reichel 	struct pm860x_chip *chip;
988c0984e5SSebastian Reichel 	struct i2c_client *i2c;
998c0984e5SSebastian Reichel 	struct i2c_client *i2c_8606;
1008c0984e5SSebastian Reichel 	struct device *dev;
1018c0984e5SSebastian Reichel 
1028c0984e5SSebastian Reichel 	struct power_supply *usb;
1038c0984e5SSebastian Reichel 	struct mutex lock;
1048c0984e5SSebastian Reichel 	int irq_nums;
1058c0984e5SSebastian Reichel 	int irq[7];
1068c0984e5SSebastian Reichel 	unsigned state:3;	/* fsm state */
1078c0984e5SSebastian Reichel 	unsigned online:1;	/* usb charger */
1088c0984e5SSebastian Reichel 	unsigned present:1;	/* battery present */
1098c0984e5SSebastian Reichel 	unsigned allowed:1;
1108c0984e5SSebastian Reichel };
1118c0984e5SSebastian Reichel 
1128c0984e5SSebastian Reichel static char *pm860x_supplied_to[] = {
1138c0984e5SSebastian Reichel 	"battery-monitor",
1148c0984e5SSebastian Reichel };
1158c0984e5SSebastian Reichel 
measure_vchg(struct pm860x_charger_info * info,int * data)1168c0984e5SSebastian Reichel static int measure_vchg(struct pm860x_charger_info *info, int *data)
1178c0984e5SSebastian Reichel {
1188c0984e5SSebastian Reichel 	unsigned char buf[2];
1198c0984e5SSebastian Reichel 	int ret = 0;
1208c0984e5SSebastian Reichel 
1218c0984e5SSebastian Reichel 	ret = pm860x_bulk_read(info->i2c, PM8607_VCHG_MEAS1, 2, buf);
1228c0984e5SSebastian Reichel 	if (ret < 0)
1238c0984e5SSebastian Reichel 		return ret;
1248c0984e5SSebastian Reichel 
1258c0984e5SSebastian Reichel 	*data = ((buf[0] & 0xff) << 4) | (buf[1] & 0x0f);
1268c0984e5SSebastian Reichel 	/* V_BATT_MEAS(mV) = value * 5 * 1.8 * 1000 / (2^12) */
1278c0984e5SSebastian Reichel 	*data = ((*data & 0xfff) * 9 * 125) >> 9;
1288c0984e5SSebastian Reichel 
1298c0984e5SSebastian Reichel 	dev_dbg(info->dev, "%s, vchg: %d mv\n", __func__, *data);
1308c0984e5SSebastian Reichel 
1318c0984e5SSebastian Reichel 	return ret;
1328c0984e5SSebastian Reichel }
1338c0984e5SSebastian Reichel 
set_vchg_threshold(struct pm860x_charger_info * info,int min,int max)1348c0984e5SSebastian Reichel static void set_vchg_threshold(struct pm860x_charger_info *info,
1358c0984e5SSebastian Reichel 			       int min, int max)
1368c0984e5SSebastian Reichel {
1378c0984e5SSebastian Reichel 	int data;
1388c0984e5SSebastian Reichel 
1398c0984e5SSebastian Reichel 	/* (tmp << 8) * / 5 / 1800 */
1408c0984e5SSebastian Reichel 	if (min <= 0)
1418c0984e5SSebastian Reichel 		data = 0;
1428c0984e5SSebastian Reichel 	else
1438c0984e5SSebastian Reichel 		data = (min << 5) / 1125;
1448c0984e5SSebastian Reichel 	pm860x_reg_write(info->i2c, PM8607_VCHG_LOWTH, data);
1458c0984e5SSebastian Reichel 	dev_dbg(info->dev, "VCHG_LOWTH:%dmv, 0x%x\n", min, data);
1468c0984e5SSebastian Reichel 
1478c0984e5SSebastian Reichel 	if (max <= 0)
1488c0984e5SSebastian Reichel 		data = 0xff;
1498c0984e5SSebastian Reichel 	else
1508c0984e5SSebastian Reichel 		data = (max << 5) / 1125;
1518c0984e5SSebastian Reichel 	pm860x_reg_write(info->i2c, PM8607_VCHG_HIGHTH, data);
1528c0984e5SSebastian Reichel 	dev_dbg(info->dev, "VCHG_HIGHTH:%dmv, 0x%x\n", max, data);
1538c0984e5SSebastian Reichel 
1548c0984e5SSebastian Reichel }
1558c0984e5SSebastian Reichel 
set_vbatt_threshold(struct pm860x_charger_info * info,int min,int max)1568c0984e5SSebastian Reichel static void set_vbatt_threshold(struct pm860x_charger_info *info,
1578c0984e5SSebastian Reichel 				int min, int max)
1588c0984e5SSebastian Reichel {
1598c0984e5SSebastian Reichel 	int data;
1608c0984e5SSebastian Reichel 
1618c0984e5SSebastian Reichel 	/* (tmp << 8) * 3 / 1800 */
1628c0984e5SSebastian Reichel 	if (min <= 0)
1638c0984e5SSebastian Reichel 		data = 0;
1648c0984e5SSebastian Reichel 	else
1658c0984e5SSebastian Reichel 		data = (min << 5) / 675;
1668c0984e5SSebastian Reichel 	pm860x_reg_write(info->i2c, PM8607_VBAT_LOWTH, data);
1678c0984e5SSebastian Reichel 	dev_dbg(info->dev, "VBAT Min:%dmv, LOWTH:0x%x\n", min, data);
1688c0984e5SSebastian Reichel 
1698c0984e5SSebastian Reichel 	if (max <= 0)
1708c0984e5SSebastian Reichel 		data = 0xff;
1718c0984e5SSebastian Reichel 	else
1728c0984e5SSebastian Reichel 		data = (max << 5) / 675;
1738c0984e5SSebastian Reichel 	pm860x_reg_write(info->i2c, PM8607_VBAT_HIGHTH, data);
1748c0984e5SSebastian Reichel 	dev_dbg(info->dev, "VBAT Max:%dmv, HIGHTH:0x%x\n", max, data);
1758c0984e5SSebastian Reichel 
1768c0984e5SSebastian Reichel 	return;
1778c0984e5SSebastian Reichel }
1788c0984e5SSebastian Reichel 
start_precharge(struct pm860x_charger_info * info)1798c0984e5SSebastian Reichel static int start_precharge(struct pm860x_charger_info *info)
1808c0984e5SSebastian Reichel {
1818c0984e5SSebastian Reichel 	int ret;
1828c0984e5SSebastian Reichel 
1838c0984e5SSebastian Reichel 	dev_dbg(info->dev, "Start Pre-charging!\n");
1848c0984e5SSebastian Reichel 	set_vbatt_threshold(info, 0, 0);
1858c0984e5SSebastian Reichel 
1868c0984e5SSebastian Reichel 	ret = pm860x_reg_write(info->i2c_8606, PM8606_PREREGULATORA,
1878c0984e5SSebastian Reichel 			       PREREG1_1350MA | PREREG1_VSYS_4_5V);
1888c0984e5SSebastian Reichel 	if (ret < 0)
1898c0984e5SSebastian Reichel 		goto out;
1908c0984e5SSebastian Reichel 	/* stop charging */
1918c0984e5SSebastian Reichel 	ret = pm860x_set_bits(info->i2c, PM8607_CHG_CTRL1, 3,
1928c0984e5SSebastian Reichel 			      CC1_MODE_OFF);
1938c0984e5SSebastian Reichel 	if (ret < 0)
1948c0984e5SSebastian Reichel 		goto out;
1958c0984e5SSebastian Reichel 	/* set 270 minutes timeout */
1968c0984e5SSebastian Reichel 	ret = pm860x_set_bits(info->i2c, PM8607_CHG_CTRL3, (0xf << 4),
1978c0984e5SSebastian Reichel 			      CC3_270MIN_TIMEOUT);
1988c0984e5SSebastian Reichel 	if (ret < 0)
1998c0984e5SSebastian Reichel 		goto out;
2008c0984e5SSebastian Reichel 	/* set precharge current, termination voltage, IBAT & TBAT monitor */
2018c0984e5SSebastian Reichel 	ret = pm860x_reg_write(info->i2c, PM8607_CHG_CTRL4,
2028c0984e5SSebastian Reichel 			       CC4_IPRE_40MA | CC4_VPCHG_3_2V |
2038c0984e5SSebastian Reichel 			       CC4_IFCHG_MON_EN | CC4_BTEMP_MON_EN);
2048c0984e5SSebastian Reichel 	if (ret < 0)
2058c0984e5SSebastian Reichel 		goto out;
2068c0984e5SSebastian Reichel 	ret = pm860x_set_bits(info->i2c, PM8607_CHG_CTRL7,
2078c0984e5SSebastian Reichel 			      CC7_BAT_REM_EN | CC7_IFSM_EN,
2088c0984e5SSebastian Reichel 			      CC7_BAT_REM_EN | CC7_IFSM_EN);
2098c0984e5SSebastian Reichel 	if (ret < 0)
2108c0984e5SSebastian Reichel 		goto out;
2118c0984e5SSebastian Reichel 	/* trigger precharge */
2128c0984e5SSebastian Reichel 	ret = pm860x_set_bits(info->i2c, PM8607_CHG_CTRL1, 3,
2138c0984e5SSebastian Reichel 			      CC1_MODE_PRECHARGE);
2148c0984e5SSebastian Reichel out:
2158c0984e5SSebastian Reichel 	return ret;
2168c0984e5SSebastian Reichel }
2178c0984e5SSebastian Reichel 
start_fastcharge(struct pm860x_charger_info * info)2188c0984e5SSebastian Reichel static int start_fastcharge(struct pm860x_charger_info *info)
2198c0984e5SSebastian Reichel {
2208c0984e5SSebastian Reichel 	int ret;
2218c0984e5SSebastian Reichel 
2228c0984e5SSebastian Reichel 	dev_dbg(info->dev, "Start Fast-charging!\n");
2238c0984e5SSebastian Reichel 
2248c0984e5SSebastian Reichel 	/* set fastcharge termination current & voltage, disable charging */
2258c0984e5SSebastian Reichel 	ret = pm860x_reg_write(info->i2c, PM8607_CHG_CTRL1,
2268c0984e5SSebastian Reichel 			       CC1_MODE_OFF | CC1_ITERM_60MA |
2278c0984e5SSebastian Reichel 			       CC1_VFCHG_4_2V);
2288c0984e5SSebastian Reichel 	if (ret < 0)
2298c0984e5SSebastian Reichel 		goto out;
2308c0984e5SSebastian Reichel 	ret = pm860x_reg_write(info->i2c_8606, PM8606_PREREGULATORA,
2318c0984e5SSebastian Reichel 			       PREREG1_540MA | PREREG1_VSYS_4_5V);
2328c0984e5SSebastian Reichel 	if (ret < 0)
2338c0984e5SSebastian Reichel 		goto out;
2348c0984e5SSebastian Reichel 	ret = pm860x_set_bits(info->i2c, PM8607_CHG_CTRL2, 0x1f,
2358c0984e5SSebastian Reichel 			      CC2_ICHG_500MA);
2368c0984e5SSebastian Reichel 	if (ret < 0)
2378c0984e5SSebastian Reichel 		goto out;
2388c0984e5SSebastian Reichel 	/* set 270 minutes timeout */
2398c0984e5SSebastian Reichel 	ret = pm860x_set_bits(info->i2c, PM8607_CHG_CTRL3, (0xf << 4),
2408c0984e5SSebastian Reichel 			      CC3_270MIN_TIMEOUT);
2418c0984e5SSebastian Reichel 	if (ret < 0)
2428c0984e5SSebastian Reichel 		goto out;
2438c0984e5SSebastian Reichel 	/* set IBAT & TBAT monitor */
2448c0984e5SSebastian Reichel 	ret = pm860x_set_bits(info->i2c, PM8607_CHG_CTRL4,
2458c0984e5SSebastian Reichel 			      CC4_IFCHG_MON_EN | CC4_BTEMP_MON_EN,
2468c0984e5SSebastian Reichel 			      CC4_IFCHG_MON_EN | CC4_BTEMP_MON_EN);
2478c0984e5SSebastian Reichel 	if (ret < 0)
2488c0984e5SSebastian Reichel 		goto out;
2498c0984e5SSebastian Reichel 	ret = pm860x_set_bits(info->i2c, PM8607_CHG_CTRL6,
2508c0984e5SSebastian Reichel 			      CC6_BAT_OV_EN | CC6_BAT_UV_EN |
2518c0984e5SSebastian Reichel 			      CC6_UV_VBAT_SET,
2528c0984e5SSebastian Reichel 			      CC6_BAT_OV_EN | CC6_BAT_UV_EN |
2538c0984e5SSebastian Reichel 			      CC6_UV_VBAT_SET);
2548c0984e5SSebastian Reichel 	if (ret < 0)
2558c0984e5SSebastian Reichel 		goto out;
2568c0984e5SSebastian Reichel 	ret = pm860x_set_bits(info->i2c, PM8607_CHG_CTRL7,
2578c0984e5SSebastian Reichel 			      CC7_BAT_REM_EN | CC7_IFSM_EN,
2588c0984e5SSebastian Reichel 			      CC7_BAT_REM_EN | CC7_IFSM_EN);
2598c0984e5SSebastian Reichel 	if (ret < 0)
2608c0984e5SSebastian Reichel 		goto out;
2618c0984e5SSebastian Reichel 	/* launch fast-charge */
2628c0984e5SSebastian Reichel 	ret = pm860x_set_bits(info->i2c, PM8607_CHG_CTRL1, 3,
2638c0984e5SSebastian Reichel 			      CC1_MODE_FASTCHARGE);
2648c0984e5SSebastian Reichel 	/* vchg threshold setting */
2658c0984e5SSebastian Reichel 	set_vchg_threshold(info, VCHG_NORMAL_LOW, VCHG_NORMAL_HIGH);
2668c0984e5SSebastian Reichel out:
2678c0984e5SSebastian Reichel 	return ret;
2688c0984e5SSebastian Reichel }
2698c0984e5SSebastian Reichel 
stop_charge(struct pm860x_charger_info * info,int vbatt)2708c0984e5SSebastian Reichel static void stop_charge(struct pm860x_charger_info *info, int vbatt)
2718c0984e5SSebastian Reichel {
2728c0984e5SSebastian Reichel 	dev_dbg(info->dev, "Stop charging!\n");
2738c0984e5SSebastian Reichel 	pm860x_set_bits(info->i2c, PM8607_CHG_CTRL1, 3, CC1_MODE_OFF);
2748c0984e5SSebastian Reichel 	if (vbatt > CHARGE_THRESHOLD && info->online)
2758c0984e5SSebastian Reichel 		set_vbatt_threshold(info, CHARGE_THRESHOLD, 0);
2768c0984e5SSebastian Reichel }
2778c0984e5SSebastian Reichel 
power_off_notification(struct pm860x_charger_info * info)2788c0984e5SSebastian Reichel static void power_off_notification(struct pm860x_charger_info *info)
2798c0984e5SSebastian Reichel {
2808c0984e5SSebastian Reichel 	dev_dbg(info->dev, "Power-off notification!\n");
2818c0984e5SSebastian Reichel }
2828c0984e5SSebastian Reichel 
set_charging_fsm(struct pm860x_charger_info * info)2838c0984e5SSebastian Reichel static int set_charging_fsm(struct pm860x_charger_info *info)
2848c0984e5SSebastian Reichel {
2858c0984e5SSebastian Reichel 	struct power_supply *psy;
2868c0984e5SSebastian Reichel 	union power_supply_propval data;
2878c0984e5SSebastian Reichel 	unsigned char fsm_state[][16] = { "init", "discharge", "precharge",
2888c0984e5SSebastian Reichel 		"fastcharge",
2898c0984e5SSebastian Reichel 	};
2908c0984e5SSebastian Reichel 	int ret;
2918c0984e5SSebastian Reichel 	int vbatt;
2928c0984e5SSebastian Reichel 
2938c0984e5SSebastian Reichel 	psy = power_supply_get_by_name(pm860x_supplied_to[0]);
2948c0984e5SSebastian Reichel 	if (!psy)
2958c0984e5SSebastian Reichel 		return -EINVAL;
2968c0984e5SSebastian Reichel 	ret = power_supply_get_property(psy, POWER_SUPPLY_PROP_VOLTAGE_NOW,
2978c0984e5SSebastian Reichel 			&data);
2988c0984e5SSebastian Reichel 	if (ret) {
2998c0984e5SSebastian Reichel 		power_supply_put(psy);
3008c0984e5SSebastian Reichel 		return ret;
3018c0984e5SSebastian Reichel 	}
3028c0984e5SSebastian Reichel 	vbatt = data.intval / 1000;
3038c0984e5SSebastian Reichel 
3048c0984e5SSebastian Reichel 	ret = power_supply_get_property(psy, POWER_SUPPLY_PROP_PRESENT, &data);
3058c0984e5SSebastian Reichel 	if (ret) {
3068c0984e5SSebastian Reichel 		power_supply_put(psy);
3078c0984e5SSebastian Reichel 		return ret;
3088c0984e5SSebastian Reichel 	}
3098c0984e5SSebastian Reichel 	power_supply_put(psy);
3108c0984e5SSebastian Reichel 
3118c0984e5SSebastian Reichel 	mutex_lock(&info->lock);
3128c0984e5SSebastian Reichel 	info->present = data.intval;
3138c0984e5SSebastian Reichel 
3148c0984e5SSebastian Reichel 	dev_dbg(info->dev, "Entering FSM:%s, Charger:%s, Battery:%s, "
3158c0984e5SSebastian Reichel 		"Allowed:%d\n",
3168c0984e5SSebastian Reichel 		&fsm_state[info->state][0],
3178c0984e5SSebastian Reichel 		(info->online) ? "online" : "N/A",
3188c0984e5SSebastian Reichel 		(info->present) ? "present" : "N/A", info->allowed);
3198c0984e5SSebastian Reichel 	dev_dbg(info->dev, "set_charging_fsm:vbatt:%d(mV)\n", vbatt);
3208c0984e5SSebastian Reichel 
3218c0984e5SSebastian Reichel 	switch (info->state) {
3228c0984e5SSebastian Reichel 	case FSM_INIT:
3238c0984e5SSebastian Reichel 		if (info->online && info->present && info->allowed) {
3248c0984e5SSebastian Reichel 			if (vbatt < PRECHARGE_THRESHOLD) {
3258c0984e5SSebastian Reichel 				info->state = FSM_PRECHARGE;
3268c0984e5SSebastian Reichel 				start_precharge(info);
3278c0984e5SSebastian Reichel 			} else if (vbatt > DISCHARGE_THRESHOLD) {
3288c0984e5SSebastian Reichel 				info->state = FSM_DISCHARGE;
3298c0984e5SSebastian Reichel 				stop_charge(info, vbatt);
3308c0984e5SSebastian Reichel 			} else if (vbatt < DISCHARGE_THRESHOLD) {
3318c0984e5SSebastian Reichel 				info->state = FSM_FASTCHARGE;
3328c0984e5SSebastian Reichel 				start_fastcharge(info);
3338c0984e5SSebastian Reichel 			}
3348c0984e5SSebastian Reichel 		} else {
3358c0984e5SSebastian Reichel 			if (vbatt < POWEROFF_THRESHOLD) {
3368c0984e5SSebastian Reichel 				power_off_notification(info);
3378c0984e5SSebastian Reichel 			} else {
3388c0984e5SSebastian Reichel 				info->state = FSM_DISCHARGE;
3398c0984e5SSebastian Reichel 				stop_charge(info, vbatt);
3408c0984e5SSebastian Reichel 			}
3418c0984e5SSebastian Reichel 		}
3428c0984e5SSebastian Reichel 		break;
3438c0984e5SSebastian Reichel 	case FSM_PRECHARGE:
3448c0984e5SSebastian Reichel 		if (info->online && info->present && info->allowed) {
3458c0984e5SSebastian Reichel 			if (vbatt > PRECHARGE_THRESHOLD) {
3468c0984e5SSebastian Reichel 				info->state = FSM_FASTCHARGE;
3478c0984e5SSebastian Reichel 				start_fastcharge(info);
3488c0984e5SSebastian Reichel 			}
3498c0984e5SSebastian Reichel 		} else {
3508c0984e5SSebastian Reichel 			info->state = FSM_DISCHARGE;
3518c0984e5SSebastian Reichel 			stop_charge(info, vbatt);
3528c0984e5SSebastian Reichel 		}
3538c0984e5SSebastian Reichel 		break;
3548c0984e5SSebastian Reichel 	case FSM_FASTCHARGE:
3558c0984e5SSebastian Reichel 		if (info->online && info->present && info->allowed) {
3568c0984e5SSebastian Reichel 			if (vbatt < PRECHARGE_THRESHOLD) {
3578c0984e5SSebastian Reichel 				info->state = FSM_PRECHARGE;
3588c0984e5SSebastian Reichel 				start_precharge(info);
3598c0984e5SSebastian Reichel 			}
3608c0984e5SSebastian Reichel 		} else {
3618c0984e5SSebastian Reichel 			info->state = FSM_DISCHARGE;
3628c0984e5SSebastian Reichel 			stop_charge(info, vbatt);
3638c0984e5SSebastian Reichel 		}
3648c0984e5SSebastian Reichel 		break;
3658c0984e5SSebastian Reichel 	case FSM_DISCHARGE:
3668c0984e5SSebastian Reichel 		if (info->online && info->present && info->allowed) {
3678c0984e5SSebastian Reichel 			if (vbatt < PRECHARGE_THRESHOLD) {
3688c0984e5SSebastian Reichel 				info->state = FSM_PRECHARGE;
3698c0984e5SSebastian Reichel 				start_precharge(info);
3708c0984e5SSebastian Reichel 			} else if (vbatt < DISCHARGE_THRESHOLD) {
3718c0984e5SSebastian Reichel 				info->state = FSM_FASTCHARGE;
3728c0984e5SSebastian Reichel 				start_fastcharge(info);
3738c0984e5SSebastian Reichel 			}
3748c0984e5SSebastian Reichel 		} else {
3758c0984e5SSebastian Reichel 			if (vbatt < POWEROFF_THRESHOLD)
3768c0984e5SSebastian Reichel 				power_off_notification(info);
3778c0984e5SSebastian Reichel 			else if (vbatt > CHARGE_THRESHOLD && info->online)
3788c0984e5SSebastian Reichel 				set_vbatt_threshold(info, CHARGE_THRESHOLD, 0);
3798c0984e5SSebastian Reichel 		}
3808c0984e5SSebastian Reichel 		break;
3818c0984e5SSebastian Reichel 	default:
3828c0984e5SSebastian Reichel 		dev_warn(info->dev, "FSM meets wrong state:%d\n",
3838c0984e5SSebastian Reichel 			 info->state);
3848c0984e5SSebastian Reichel 		break;
3858c0984e5SSebastian Reichel 	}
3868c0984e5SSebastian Reichel 	dev_dbg(info->dev,
3878c0984e5SSebastian Reichel 		"Out FSM:%s, Charger:%s, Battery:%s, Allowed:%d\n",
3888c0984e5SSebastian Reichel 		&fsm_state[info->state][0],
3898c0984e5SSebastian Reichel 		(info->online) ? "online" : "N/A",
3908c0984e5SSebastian Reichel 		(info->present) ? "present" : "N/A", info->allowed);
3918c0984e5SSebastian Reichel 	mutex_unlock(&info->lock);
3928c0984e5SSebastian Reichel 
3938c0984e5SSebastian Reichel 	return 0;
3948c0984e5SSebastian Reichel }
3958c0984e5SSebastian Reichel 
pm860x_charger_handler(int irq,void * data)3968c0984e5SSebastian Reichel static irqreturn_t pm860x_charger_handler(int irq, void *data)
3978c0984e5SSebastian Reichel {
3988c0984e5SSebastian Reichel 	struct pm860x_charger_info *info = data;
3998c0984e5SSebastian Reichel 	int ret;
4008c0984e5SSebastian Reichel 
4018c0984e5SSebastian Reichel 	mutex_lock(&info->lock);
4028c0984e5SSebastian Reichel 	ret = pm860x_reg_read(info->i2c, PM8607_STATUS_2);
4038c0984e5SSebastian Reichel 	if (ret < 0) {
4048c0984e5SSebastian Reichel 		mutex_unlock(&info->lock);
4058c0984e5SSebastian Reichel 		goto out;
4068c0984e5SSebastian Reichel 	}
4078c0984e5SSebastian Reichel 	if (ret & STATUS2_CHG) {
4088c0984e5SSebastian Reichel 		info->online = 1;
4098c0984e5SSebastian Reichel 		info->allowed = 1;
4108c0984e5SSebastian Reichel 	} else {
4118c0984e5SSebastian Reichel 		info->online = 0;
4128c0984e5SSebastian Reichel 		info->allowed = 0;
4138c0984e5SSebastian Reichel 	}
4148c0984e5SSebastian Reichel 	mutex_unlock(&info->lock);
4158c0984e5SSebastian Reichel 	dev_dbg(info->dev, "%s, Charger:%s, Allowed:%d\n", __func__,
4168c0984e5SSebastian Reichel 		(info->online) ? "online" : "N/A", info->allowed);
4178c0984e5SSebastian Reichel 
4188c0984e5SSebastian Reichel 	set_charging_fsm(info);
4198c0984e5SSebastian Reichel 
4208c0984e5SSebastian Reichel 	power_supply_changed(info->usb);
4218c0984e5SSebastian Reichel out:
4228c0984e5SSebastian Reichel 	return IRQ_HANDLED;
4238c0984e5SSebastian Reichel }
4248c0984e5SSebastian Reichel 
pm860x_temp_handler(int irq,void * data)4258c0984e5SSebastian Reichel static irqreturn_t pm860x_temp_handler(int irq, void *data)
4268c0984e5SSebastian Reichel {
4278c0984e5SSebastian Reichel 	struct power_supply *psy;
4288c0984e5SSebastian Reichel 	struct pm860x_charger_info *info = data;
4298c0984e5SSebastian Reichel 	union power_supply_propval temp;
4308c0984e5SSebastian Reichel 	int value;
4318c0984e5SSebastian Reichel 	int ret;
4328c0984e5SSebastian Reichel 
4338c0984e5SSebastian Reichel 	psy = power_supply_get_by_name(pm860x_supplied_to[0]);
4348c0984e5SSebastian Reichel 	if (!psy)
4358c0984e5SSebastian Reichel 		return IRQ_HANDLED;
4368c0984e5SSebastian Reichel 	ret = power_supply_get_property(psy, POWER_SUPPLY_PROP_TEMP, &temp);
4378c0984e5SSebastian Reichel 	if (ret)
4388c0984e5SSebastian Reichel 		goto out;
4398c0984e5SSebastian Reichel 	value = temp.intval / 10;
4408c0984e5SSebastian Reichel 
4418c0984e5SSebastian Reichel 	mutex_lock(&info->lock);
4428c0984e5SSebastian Reichel 	/* Temperature < -10 C or >40 C, Will not allow charge */
4438c0984e5SSebastian Reichel 	if (value < -10 || value > 40)
4448c0984e5SSebastian Reichel 		info->allowed = 0;
4458c0984e5SSebastian Reichel 	else
4468c0984e5SSebastian Reichel 		info->allowed = 1;
4478c0984e5SSebastian Reichel 	dev_dbg(info->dev, "%s, Allowed: %d\n", __func__, info->allowed);
4488c0984e5SSebastian Reichel 	mutex_unlock(&info->lock);
4498c0984e5SSebastian Reichel 
4508c0984e5SSebastian Reichel 	set_charging_fsm(info);
4518c0984e5SSebastian Reichel out:
4528c0984e5SSebastian Reichel 	power_supply_put(psy);
4538c0984e5SSebastian Reichel 	return IRQ_HANDLED;
4548c0984e5SSebastian Reichel }
4558c0984e5SSebastian Reichel 
pm860x_exception_handler(int irq,void * data)4568c0984e5SSebastian Reichel static irqreturn_t pm860x_exception_handler(int irq, void *data)
4578c0984e5SSebastian Reichel {
4588c0984e5SSebastian Reichel 	struct pm860x_charger_info *info = data;
4598c0984e5SSebastian Reichel 
4608c0984e5SSebastian Reichel 	mutex_lock(&info->lock);
4618c0984e5SSebastian Reichel 	info->allowed = 0;
4628c0984e5SSebastian Reichel 	mutex_unlock(&info->lock);
4638c0984e5SSebastian Reichel 	dev_dbg(info->dev, "%s, irq: %d\n", __func__, irq);
4648c0984e5SSebastian Reichel 
4658c0984e5SSebastian Reichel 	set_charging_fsm(info);
4668c0984e5SSebastian Reichel 	return IRQ_HANDLED;
4678c0984e5SSebastian Reichel }
4688c0984e5SSebastian Reichel 
pm860x_done_handler(int irq,void * data)4698c0984e5SSebastian Reichel static irqreturn_t pm860x_done_handler(int irq, void *data)
4708c0984e5SSebastian Reichel {
4718c0984e5SSebastian Reichel 	struct pm860x_charger_info *info = data;
4728c0984e5SSebastian Reichel 	struct power_supply *psy;
4738c0984e5SSebastian Reichel 	union power_supply_propval val;
4748c0984e5SSebastian Reichel 	int ret;
4758c0984e5SSebastian Reichel 	int vbatt;
4768c0984e5SSebastian Reichel 
4778c0984e5SSebastian Reichel 	mutex_lock(&info->lock);
4788c0984e5SSebastian Reichel 	/* pre-charge done, will transimit to fast-charge stage */
4798c0984e5SSebastian Reichel 	if (info->state == FSM_PRECHARGE) {
4808c0984e5SSebastian Reichel 		info->allowed = 1;
4818c0984e5SSebastian Reichel 		goto out;
4828c0984e5SSebastian Reichel 	}
4838c0984e5SSebastian Reichel 	/*
4848c0984e5SSebastian Reichel 	 * Fast charge done, delay to read
4858c0984e5SSebastian Reichel 	 * the correct status of CHG_DET.
4868c0984e5SSebastian Reichel 	 */
4878c0984e5SSebastian Reichel 	mdelay(5);
4888c0984e5SSebastian Reichel 	info->allowed = 0;
4898c0984e5SSebastian Reichel 	psy = power_supply_get_by_name(pm860x_supplied_to[0]);
4908c0984e5SSebastian Reichel 	if (!psy)
4918c0984e5SSebastian Reichel 		goto out;
4928c0984e5SSebastian Reichel 	ret = power_supply_get_property(psy, POWER_SUPPLY_PROP_VOLTAGE_NOW,
4938c0984e5SSebastian Reichel 			&val);
4948c0984e5SSebastian Reichel 	if (ret)
4958c0984e5SSebastian Reichel 		goto out_psy_put;
4968c0984e5SSebastian Reichel 	vbatt = val.intval / 1000;
4978c0984e5SSebastian Reichel 	/*
4988c0984e5SSebastian Reichel 	 * CHG_DONE interrupt is faster than CHG_DET interrupt when
4998c0984e5SSebastian Reichel 	 * plug in/out usb, So we can not rely on info->online, we
5008c0984e5SSebastian Reichel 	 * need check pm8607 status register to check usb is online
5018c0984e5SSebastian Reichel 	 * or not, then we can decide it is real charge done
5028c0984e5SSebastian Reichel 	 * automatically or it is triggered by usb plug out;
5038c0984e5SSebastian Reichel 	 */
5048c0984e5SSebastian Reichel 	ret = pm860x_reg_read(info->i2c, PM8607_STATUS_2);
5058c0984e5SSebastian Reichel 	if (ret < 0)
5068c0984e5SSebastian Reichel 		goto out_psy_put;
5078c0984e5SSebastian Reichel 	if (vbatt > CHARGE_THRESHOLD && ret & STATUS2_CHG)
5088c0984e5SSebastian Reichel 		power_supply_set_property(psy, POWER_SUPPLY_PROP_CHARGE_FULL,
5098c0984e5SSebastian Reichel 				&val);
5108c0984e5SSebastian Reichel 
5118c0984e5SSebastian Reichel out_psy_put:
5128c0984e5SSebastian Reichel 	power_supply_put(psy);
5138c0984e5SSebastian Reichel out:
5148c0984e5SSebastian Reichel 	mutex_unlock(&info->lock);
5158c0984e5SSebastian Reichel 	dev_dbg(info->dev, "%s, Allowed: %d\n", __func__, info->allowed);
5168c0984e5SSebastian Reichel 	set_charging_fsm(info);
5178c0984e5SSebastian Reichel 
5188c0984e5SSebastian Reichel 	return IRQ_HANDLED;
5198c0984e5SSebastian Reichel }
5208c0984e5SSebastian Reichel 
pm860x_vbattery_handler(int irq,void * data)5218c0984e5SSebastian Reichel static irqreturn_t pm860x_vbattery_handler(int irq, void *data)
5228c0984e5SSebastian Reichel {
5238c0984e5SSebastian Reichel 	struct pm860x_charger_info *info = data;
5248c0984e5SSebastian Reichel 
5258c0984e5SSebastian Reichel 	mutex_lock(&info->lock);
5268c0984e5SSebastian Reichel 
5278c0984e5SSebastian Reichel 	set_vbatt_threshold(info, 0, 0);
5288c0984e5SSebastian Reichel 
5298c0984e5SSebastian Reichel 	if (info->present && info->online)
5308c0984e5SSebastian Reichel 		info->allowed = 1;
5318c0984e5SSebastian Reichel 	else
5328c0984e5SSebastian Reichel 		info->allowed = 0;
5338c0984e5SSebastian Reichel 	mutex_unlock(&info->lock);
5348c0984e5SSebastian Reichel 	dev_dbg(info->dev, "%s, Allowed: %d\n", __func__, info->allowed);
5358c0984e5SSebastian Reichel 
5368c0984e5SSebastian Reichel 	set_charging_fsm(info);
5378c0984e5SSebastian Reichel 
5388c0984e5SSebastian Reichel 	return IRQ_HANDLED;
5398c0984e5SSebastian Reichel }
5408c0984e5SSebastian Reichel 
pm860x_vchg_handler(int irq,void * data)5418c0984e5SSebastian Reichel static irqreturn_t pm860x_vchg_handler(int irq, void *data)
5428c0984e5SSebastian Reichel {
5438c0984e5SSebastian Reichel 	struct pm860x_charger_info *info = data;
5448c0984e5SSebastian Reichel 	int vchg = 0;
5458c0984e5SSebastian Reichel 
5468c0984e5SSebastian Reichel 	if (info->present)
5478c0984e5SSebastian Reichel 		goto out;
5488c0984e5SSebastian Reichel 
5498c0984e5SSebastian Reichel 	measure_vchg(info, &vchg);
5508c0984e5SSebastian Reichel 
5518c0984e5SSebastian Reichel 	mutex_lock(&info->lock);
5528c0984e5SSebastian Reichel 	if (!info->online) {
5538c0984e5SSebastian Reichel 		int status;
5548c0984e5SSebastian Reichel 		/* check if over-temp on pm8606 or not */
5558c0984e5SSebastian Reichel 		status = pm860x_reg_read(info->i2c_8606, PM8606_FLAGS);
5568c0984e5SSebastian Reichel 		if (status & OVER_TEMP_FLAG) {
5578c0984e5SSebastian Reichel 			/* clear over temp flag and set auto recover */
5588c0984e5SSebastian Reichel 			pm860x_set_bits(info->i2c_8606, PM8606_FLAGS,
5598c0984e5SSebastian Reichel 					OVER_TEMP_FLAG, OVER_TEMP_FLAG);
5608c0984e5SSebastian Reichel 			pm860x_set_bits(info->i2c_8606,
5618c0984e5SSebastian Reichel 					PM8606_VSYS,
5628c0984e5SSebastian Reichel 					OVTEMP_AUTORECOVER,
5638c0984e5SSebastian Reichel 					OVTEMP_AUTORECOVER);
5648c0984e5SSebastian Reichel 			dev_dbg(info->dev,
5658c0984e5SSebastian Reichel 				"%s, pm8606 over-temp occurred\n", __func__);
5668c0984e5SSebastian Reichel 		}
5678c0984e5SSebastian Reichel 	}
5688c0984e5SSebastian Reichel 
5698c0984e5SSebastian Reichel 	if (vchg > VCHG_NORMAL_CHECK) {
5708c0984e5SSebastian Reichel 		set_vchg_threshold(info, VCHG_OVP_LOW, 0);
5718c0984e5SSebastian Reichel 		info->allowed = 0;
5728c0984e5SSebastian Reichel 		dev_dbg(info->dev,
5738c0984e5SSebastian Reichel 			"%s,pm8607 over-vchg occurred,vchg = %dmv\n",
5748c0984e5SSebastian Reichel 			__func__, vchg);
5758c0984e5SSebastian Reichel 	} else if (vchg < VCHG_OVP_LOW) {
5768c0984e5SSebastian Reichel 		set_vchg_threshold(info, VCHG_NORMAL_LOW,
5778c0984e5SSebastian Reichel 				   VCHG_NORMAL_HIGH);
5788c0984e5SSebastian Reichel 		info->allowed = 1;
5798c0984e5SSebastian Reichel 		dev_dbg(info->dev,
5808c0984e5SSebastian Reichel 			"%s,pm8607 over-vchg recover,vchg = %dmv\n",
5818c0984e5SSebastian Reichel 			__func__, vchg);
5828c0984e5SSebastian Reichel 	}
5838c0984e5SSebastian Reichel 	mutex_unlock(&info->lock);
5848c0984e5SSebastian Reichel 
5858c0984e5SSebastian Reichel 	dev_dbg(info->dev, "%s, Allowed: %d\n", __func__, info->allowed);
5868c0984e5SSebastian Reichel 	set_charging_fsm(info);
5878c0984e5SSebastian Reichel out:
5888c0984e5SSebastian Reichel 	return IRQ_HANDLED;
5898c0984e5SSebastian Reichel }
5908c0984e5SSebastian Reichel 
pm860x_usb_get_prop(struct power_supply * psy,enum power_supply_property psp,union power_supply_propval * val)5918c0984e5SSebastian Reichel static int pm860x_usb_get_prop(struct power_supply *psy,
5928c0984e5SSebastian Reichel 			       enum power_supply_property psp,
5938c0984e5SSebastian Reichel 			       union power_supply_propval *val)
5948c0984e5SSebastian Reichel {
5958c0984e5SSebastian Reichel 	struct pm860x_charger_info *info = power_supply_get_drvdata(psy);
5968c0984e5SSebastian Reichel 
5978c0984e5SSebastian Reichel 	switch (psp) {
5988c0984e5SSebastian Reichel 	case POWER_SUPPLY_PROP_STATUS:
5998c0984e5SSebastian Reichel 		if (info->state == FSM_FASTCHARGE ||
6008c0984e5SSebastian Reichel 				info->state == FSM_PRECHARGE)
6018c0984e5SSebastian Reichel 			val->intval = POWER_SUPPLY_STATUS_CHARGING;
6028c0984e5SSebastian Reichel 		else
6038c0984e5SSebastian Reichel 			val->intval = POWER_SUPPLY_STATUS_DISCHARGING;
6048c0984e5SSebastian Reichel 		break;
6058c0984e5SSebastian Reichel 	case POWER_SUPPLY_PROP_ONLINE:
6068c0984e5SSebastian Reichel 		val->intval = info->online;
6078c0984e5SSebastian Reichel 		break;
6088c0984e5SSebastian Reichel 	default:
6098c0984e5SSebastian Reichel 		return -ENODEV;
6108c0984e5SSebastian Reichel 	}
6118c0984e5SSebastian Reichel 	return 0;
6128c0984e5SSebastian Reichel }
6138c0984e5SSebastian Reichel 
6148c0984e5SSebastian Reichel static enum power_supply_property pm860x_usb_props[] = {
6158c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_STATUS,
6168c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_ONLINE,
6178c0984e5SSebastian Reichel };
6188c0984e5SSebastian Reichel 
pm860x_init_charger(struct pm860x_charger_info * info)6198c0984e5SSebastian Reichel static int pm860x_init_charger(struct pm860x_charger_info *info)
6208c0984e5SSebastian Reichel {
6218c0984e5SSebastian Reichel 	int ret;
6228c0984e5SSebastian Reichel 
6238c0984e5SSebastian Reichel 	ret = pm860x_reg_read(info->i2c, PM8607_STATUS_2);
6248c0984e5SSebastian Reichel 	if (ret < 0)
6258c0984e5SSebastian Reichel 		return ret;
6268c0984e5SSebastian Reichel 
6278c0984e5SSebastian Reichel 	mutex_lock(&info->lock);
6288c0984e5SSebastian Reichel 	info->state = FSM_INIT;
6298c0984e5SSebastian Reichel 	if (ret & STATUS2_CHG) {
6308c0984e5SSebastian Reichel 		info->online = 1;
6318c0984e5SSebastian Reichel 		info->allowed = 1;
6328c0984e5SSebastian Reichel 	} else {
6338c0984e5SSebastian Reichel 		info->online = 0;
6348c0984e5SSebastian Reichel 		info->allowed = 0;
6358c0984e5SSebastian Reichel 	}
6368c0984e5SSebastian Reichel 	mutex_unlock(&info->lock);
6378c0984e5SSebastian Reichel 
6388c0984e5SSebastian Reichel 	set_charging_fsm(info);
6398c0984e5SSebastian Reichel 	return 0;
6408c0984e5SSebastian Reichel }
6418c0984e5SSebastian Reichel 
6428c0984e5SSebastian Reichel static struct pm860x_irq_desc {
6438c0984e5SSebastian Reichel 	const char *name;
6448c0984e5SSebastian Reichel 	irqreturn_t (*handler)(int irq, void *data);
6458c0984e5SSebastian Reichel } pm860x_irq_descs[] = {
6468c0984e5SSebastian Reichel 	{ "usb supply detect", pm860x_charger_handler },
6478c0984e5SSebastian Reichel 	{ "charge done", pm860x_done_handler },
6488c0984e5SSebastian Reichel 	{ "charge timeout", pm860x_exception_handler },
6498c0984e5SSebastian Reichel 	{ "charge fault", pm860x_exception_handler },
6508c0984e5SSebastian Reichel 	{ "temperature", pm860x_temp_handler },
6518c0984e5SSebastian Reichel 	{ "vbatt", pm860x_vbattery_handler },
6528c0984e5SSebastian Reichel 	{ "vchg", pm860x_vchg_handler },
6538c0984e5SSebastian Reichel };
6548c0984e5SSebastian Reichel 
6558c0984e5SSebastian Reichel static const struct power_supply_desc pm860x_charger_desc = {
6568c0984e5SSebastian Reichel 	.name		= "usb",
6578c0984e5SSebastian Reichel 	.type		= POWER_SUPPLY_TYPE_USB,
6588c0984e5SSebastian Reichel 	.properties	= pm860x_usb_props,
6598c0984e5SSebastian Reichel 	.num_properties	= ARRAY_SIZE(pm860x_usb_props),
6608c0984e5SSebastian Reichel 	.get_property	= pm860x_usb_get_prop,
6618c0984e5SSebastian Reichel };
6628c0984e5SSebastian Reichel 
pm860x_charger_probe(struct platform_device * pdev)6638c0984e5SSebastian Reichel static int pm860x_charger_probe(struct platform_device *pdev)
6648c0984e5SSebastian Reichel {
6658c0984e5SSebastian Reichel 	struct pm860x_chip *chip = dev_get_drvdata(pdev->dev.parent);
6668c0984e5SSebastian Reichel 	struct power_supply_config psy_cfg = {};
6678c0984e5SSebastian Reichel 	struct pm860x_charger_info *info;
6688c0984e5SSebastian Reichel 	int ret;
6698c0984e5SSebastian Reichel 	int count;
6708c0984e5SSebastian Reichel 	int i;
6718c0984e5SSebastian Reichel 	int j;
6728c0984e5SSebastian Reichel 
6738c0984e5SSebastian Reichel 	info = devm_kzalloc(&pdev->dev, sizeof(*info), GFP_KERNEL);
6748c0984e5SSebastian Reichel 	if (!info)
6758c0984e5SSebastian Reichel 		return -ENOMEM;
6768c0984e5SSebastian Reichel 
6778c0984e5SSebastian Reichel 	count = pdev->num_resources;
6788c0984e5SSebastian Reichel 	for (i = 0, j = 0; i < count; i++) {
6798c0984e5SSebastian Reichel 		info->irq[j] = platform_get_irq(pdev, i);
6808c0984e5SSebastian Reichel 		if (info->irq[j] < 0)
6818c0984e5SSebastian Reichel 			continue;
6828c0984e5SSebastian Reichel 		j++;
6838c0984e5SSebastian Reichel 	}
6848c0984e5SSebastian Reichel 	info->irq_nums = j;
6858c0984e5SSebastian Reichel 
6868c0984e5SSebastian Reichel 	info->chip = chip;
6878c0984e5SSebastian Reichel 	info->i2c =
6888c0984e5SSebastian Reichel 	    (chip->id == CHIP_PM8607) ? chip->client : chip->companion;
6898c0984e5SSebastian Reichel 	info->i2c_8606 =
6908c0984e5SSebastian Reichel 	    (chip->id == CHIP_PM8607) ? chip->companion : chip->client;
6918c0984e5SSebastian Reichel 	if (!info->i2c_8606) {
6928c0984e5SSebastian Reichel 		dev_err(&pdev->dev, "Missed I2C address of 88PM8606!\n");
693*332d7d0cSMatti Vaittinen 		return -EINVAL;
6948c0984e5SSebastian Reichel 	}
6958c0984e5SSebastian Reichel 	info->dev = &pdev->dev;
6968c0984e5SSebastian Reichel 
6978c0984e5SSebastian Reichel 	/* set init value for the case we are not using battery */
6988c0984e5SSebastian Reichel 	set_vchg_threshold(info, VCHG_NORMAL_LOW, VCHG_OVP_LOW);
6998c0984e5SSebastian Reichel 
7008c0984e5SSebastian Reichel 	mutex_init(&info->lock);
7018c0984e5SSebastian Reichel 	platform_set_drvdata(pdev, info);
7028c0984e5SSebastian Reichel 
7038c0984e5SSebastian Reichel 	psy_cfg.drv_data = info;
7048c0984e5SSebastian Reichel 	psy_cfg.supplied_to = pm860x_supplied_to;
7058c0984e5SSebastian Reichel 	psy_cfg.num_supplicants = ARRAY_SIZE(pm860x_supplied_to);
706*332d7d0cSMatti Vaittinen 	info->usb = devm_power_supply_register(&pdev->dev, &pm860x_charger_desc,
7078c0984e5SSebastian Reichel 					       &psy_cfg);
7088c0984e5SSebastian Reichel 	if (IS_ERR(info->usb)) {
709*332d7d0cSMatti Vaittinen 		return PTR_ERR(info->usb);
7108c0984e5SSebastian Reichel 	}
7118c0984e5SSebastian Reichel 
7128c0984e5SSebastian Reichel 	pm860x_init_charger(info);
7138c0984e5SSebastian Reichel 
7148c0984e5SSebastian Reichel 	for (i = 0; i < ARRAY_SIZE(info->irq); i++) {
715*332d7d0cSMatti Vaittinen 		ret = devm_request_threaded_irq(&pdev->dev, info->irq[i], NULL,
7168c0984e5SSebastian Reichel 						pm860x_irq_descs[i].handler,
717*332d7d0cSMatti Vaittinen 						IRQF_ONESHOT,
718*332d7d0cSMatti Vaittinen 						pm860x_irq_descs[i].name, info);
7198c0984e5SSebastian Reichel 		if (ret < 0) {
7208c0984e5SSebastian Reichel 			dev_err(chip->dev, "Failed to request IRQ: #%d: %d\n",
7218c0984e5SSebastian Reichel 				info->irq[i], ret);
7228c0984e5SSebastian Reichel 			return ret;
7238c0984e5SSebastian Reichel 		}
724*332d7d0cSMatti Vaittinen 	}
7258c0984e5SSebastian Reichel 	return 0;
7268c0984e5SSebastian Reichel }
7278c0984e5SSebastian Reichel 
7288c0984e5SSebastian Reichel static struct platform_driver pm860x_charger_driver = {
7298c0984e5SSebastian Reichel 	.driver = {
7308c0984e5SSebastian Reichel 		   .name = "88pm860x-charger",
7318c0984e5SSebastian Reichel 	},
7328c0984e5SSebastian Reichel 	.probe = pm860x_charger_probe,
7338c0984e5SSebastian Reichel };
7348c0984e5SSebastian Reichel module_platform_driver(pm860x_charger_driver);
7358c0984e5SSebastian Reichel 
7368c0984e5SSebastian Reichel MODULE_DESCRIPTION("Marvell 88PM860x Charger driver");
7378c0984e5SSebastian Reichel MODULE_LICENSE("GPL");
738