1d2912cb1SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
28c0984e5SSebastian Reichel /*
38c0984e5SSebastian Reichel  * Battery driver for Maxim MAX8925
48c0984e5SSebastian Reichel  *
58c0984e5SSebastian Reichel  * Copyright (c) 2009-2010 Marvell International Ltd.
68c0984e5SSebastian Reichel  *	Haojian Zhuang <haojian.zhuang@marvell.com>
78c0984e5SSebastian Reichel  */
88c0984e5SSebastian Reichel 
98c0984e5SSebastian Reichel #include <linux/module.h>
108c0984e5SSebastian Reichel #include <linux/err.h>
118c0984e5SSebastian Reichel #include <linux/slab.h>
128c0984e5SSebastian Reichel #include <linux/of.h>
138c0984e5SSebastian Reichel #include <linux/i2c.h>
148c0984e5SSebastian Reichel #include <linux/interrupt.h>
158c0984e5SSebastian Reichel #include <linux/platform_device.h>
168c0984e5SSebastian Reichel #include <linux/power_supply.h>
178c0984e5SSebastian Reichel #include <linux/mfd/max8925.h>
188c0984e5SSebastian Reichel 
198c0984e5SSebastian Reichel /* registers in GPM */
208c0984e5SSebastian Reichel #define MAX8925_OUT5VEN			0x54
218c0984e5SSebastian Reichel #define MAX8925_OUT3VEN			0x58
228c0984e5SSebastian Reichel #define MAX8925_CHG_CNTL1		0x7c
238c0984e5SSebastian Reichel 
248c0984e5SSebastian Reichel /* bits definition */
258c0984e5SSebastian Reichel #define MAX8925_CHG_STAT_VSYSLOW	(1 << 0)
268c0984e5SSebastian Reichel #define MAX8925_CHG_STAT_MODE_MASK	(3 << 2)
278c0984e5SSebastian Reichel #define MAX8925_CHG_STAT_EN_MASK	(1 << 4)
288c0984e5SSebastian Reichel #define MAX8925_CHG_MBDET		(1 << 1)
298c0984e5SSebastian Reichel #define MAX8925_CHG_AC_RANGE_MASK	(3 << 6)
308c0984e5SSebastian Reichel 
318c0984e5SSebastian Reichel /* registers in ADC */
328c0984e5SSebastian Reichel #define MAX8925_ADC_RES_CNFG1		0x06
338c0984e5SSebastian Reichel #define MAX8925_ADC_AVG_CNFG1		0x07
348c0984e5SSebastian Reichel #define MAX8925_ADC_ACQ_CNFG1		0x08
358c0984e5SSebastian Reichel #define MAX8925_ADC_ACQ_CNFG2		0x09
368c0984e5SSebastian Reichel /* 2 bytes registers in below. MSB is 1st, LSB is 2nd. */
378c0984e5SSebastian Reichel #define MAX8925_ADC_AUX2		0x62
388c0984e5SSebastian Reichel #define MAX8925_ADC_VCHG		0x64
398c0984e5SSebastian Reichel #define MAX8925_ADC_VBBATT		0x66
408c0984e5SSebastian Reichel #define MAX8925_ADC_VMBATT		0x68
418c0984e5SSebastian Reichel #define MAX8925_ADC_ISNS		0x6a
428c0984e5SSebastian Reichel #define MAX8925_ADC_THM			0x6c
438c0984e5SSebastian Reichel #define MAX8925_ADC_TDIE		0x6e
448c0984e5SSebastian Reichel #define MAX8925_CMD_AUX2		0xc8
458c0984e5SSebastian Reichel #define MAX8925_CMD_VCHG		0xd0
468c0984e5SSebastian Reichel #define MAX8925_CMD_VBBATT		0xd8
478c0984e5SSebastian Reichel #define MAX8925_CMD_VMBATT		0xe0
488c0984e5SSebastian Reichel #define MAX8925_CMD_ISNS		0xe8
498c0984e5SSebastian Reichel #define MAX8925_CMD_THM			0xf0
508c0984e5SSebastian Reichel #define MAX8925_CMD_TDIE		0xf8
518c0984e5SSebastian Reichel 
528c0984e5SSebastian Reichel enum {
538c0984e5SSebastian Reichel 	MEASURE_AUX2,
548c0984e5SSebastian Reichel 	MEASURE_VCHG,
558c0984e5SSebastian Reichel 	MEASURE_VBBATT,
568c0984e5SSebastian Reichel 	MEASURE_VMBATT,
578c0984e5SSebastian Reichel 	MEASURE_ISNS,
588c0984e5SSebastian Reichel 	MEASURE_THM,
598c0984e5SSebastian Reichel 	MEASURE_TDIE,
608c0984e5SSebastian Reichel 	MEASURE_MAX,
618c0984e5SSebastian Reichel };
628c0984e5SSebastian Reichel 
638c0984e5SSebastian Reichel struct max8925_power_info {
648c0984e5SSebastian Reichel 	struct max8925_chip	*chip;
658c0984e5SSebastian Reichel 	struct i2c_client	*gpm;
668c0984e5SSebastian Reichel 	struct i2c_client	*adc;
678c0984e5SSebastian Reichel 
688c0984e5SSebastian Reichel 	struct power_supply	*ac;
698c0984e5SSebastian Reichel 	struct power_supply	*usb;
708c0984e5SSebastian Reichel 	struct power_supply	*battery;
718c0984e5SSebastian Reichel 	int			irq_base;
728c0984e5SSebastian Reichel 	unsigned		ac_online:1;
738c0984e5SSebastian Reichel 	unsigned		usb_online:1;
748c0984e5SSebastian Reichel 	unsigned		bat_online:1;
758c0984e5SSebastian Reichel 	unsigned		chg_mode:2;
768c0984e5SSebastian Reichel 	unsigned		batt_detect:1;	/* detecing MB by ID pin */
778c0984e5SSebastian Reichel 	unsigned		topoff_threshold:2;
788c0984e5SSebastian Reichel 	unsigned		fast_charge:3;
798c0984e5SSebastian Reichel 	unsigned		no_temp_support:1;
808c0984e5SSebastian Reichel 	unsigned		no_insert_detect:1;
818c0984e5SSebastian Reichel 
828c0984e5SSebastian Reichel 	int (*set_charger) (int);
838c0984e5SSebastian Reichel };
848c0984e5SSebastian Reichel 
__set_charger(struct max8925_power_info * info,int enable)858c0984e5SSebastian Reichel static int __set_charger(struct max8925_power_info *info, int enable)
868c0984e5SSebastian Reichel {
878c0984e5SSebastian Reichel 	struct max8925_chip *chip = info->chip;
888c0984e5SSebastian Reichel 	if (enable) {
898c0984e5SSebastian Reichel 		/* enable charger in platform */
908c0984e5SSebastian Reichel 		if (info->set_charger)
918c0984e5SSebastian Reichel 			info->set_charger(1);
928c0984e5SSebastian Reichel 		/* enable charger */
938c0984e5SSebastian Reichel 		max8925_set_bits(info->gpm, MAX8925_CHG_CNTL1, 1 << 7, 0);
948c0984e5SSebastian Reichel 	} else {
958c0984e5SSebastian Reichel 		/* disable charge */
968c0984e5SSebastian Reichel 		max8925_set_bits(info->gpm, MAX8925_CHG_CNTL1, 1 << 7, 1 << 7);
978c0984e5SSebastian Reichel 		if (info->set_charger)
988c0984e5SSebastian Reichel 			info->set_charger(0);
998c0984e5SSebastian Reichel 	}
1008c0984e5SSebastian Reichel 	dev_dbg(chip->dev, "%s\n", (enable) ? "Enable charger"
1018c0984e5SSebastian Reichel 		: "Disable charger");
1028c0984e5SSebastian Reichel 	return 0;
1038c0984e5SSebastian Reichel }
1048c0984e5SSebastian Reichel 
max8925_charger_handler(int irq,void * data)1058c0984e5SSebastian Reichel static irqreturn_t max8925_charger_handler(int irq, void *data)
1068c0984e5SSebastian Reichel {
1078c0984e5SSebastian Reichel 	struct max8925_power_info *info = (struct max8925_power_info *)data;
1088c0984e5SSebastian Reichel 	struct max8925_chip *chip = info->chip;
1098c0984e5SSebastian Reichel 
1108c0984e5SSebastian Reichel 	switch (irq - chip->irq_base) {
1118c0984e5SSebastian Reichel 	case MAX8925_IRQ_VCHG_DC_R:
1128c0984e5SSebastian Reichel 		info->ac_online = 1;
1138c0984e5SSebastian Reichel 		__set_charger(info, 1);
1148c0984e5SSebastian Reichel 		dev_dbg(chip->dev, "Adapter inserted\n");
1158c0984e5SSebastian Reichel 		break;
1168c0984e5SSebastian Reichel 	case MAX8925_IRQ_VCHG_DC_F:
1178c0984e5SSebastian Reichel 		info->ac_online = 0;
1188c0984e5SSebastian Reichel 		__set_charger(info, 0);
1198c0984e5SSebastian Reichel 		dev_dbg(chip->dev, "Adapter removed\n");
1208c0984e5SSebastian Reichel 		break;
1218c0984e5SSebastian Reichel 	case MAX8925_IRQ_VCHG_THM_OK_F:
1228c0984e5SSebastian Reichel 		/* Battery is not ready yet */
1238c0984e5SSebastian Reichel 		dev_dbg(chip->dev, "Battery temperature is out of range\n");
124df561f66SGustavo A. R. Silva 		fallthrough;
1258c0984e5SSebastian Reichel 	case MAX8925_IRQ_VCHG_DC_OVP:
1268c0984e5SSebastian Reichel 		dev_dbg(chip->dev, "Error detection\n");
1278c0984e5SSebastian Reichel 		__set_charger(info, 0);
1288c0984e5SSebastian Reichel 		break;
1298c0984e5SSebastian Reichel 	case MAX8925_IRQ_VCHG_THM_OK_R:
1308c0984e5SSebastian Reichel 		/* Battery is ready now */
1318c0984e5SSebastian Reichel 		dev_dbg(chip->dev, "Battery temperature is in range\n");
1328c0984e5SSebastian Reichel 		break;
1338c0984e5SSebastian Reichel 	case MAX8925_IRQ_VCHG_SYSLOW_R:
1348c0984e5SSebastian Reichel 		/* VSYS is low */
1358c0984e5SSebastian Reichel 		dev_info(chip->dev, "Sys power is too low\n");
1368c0984e5SSebastian Reichel 		break;
1378c0984e5SSebastian Reichel 	case MAX8925_IRQ_VCHG_SYSLOW_F:
1388c0984e5SSebastian Reichel 		dev_dbg(chip->dev, "Sys power is above low threshold\n");
1398c0984e5SSebastian Reichel 		break;
1408c0984e5SSebastian Reichel 	case MAX8925_IRQ_VCHG_DONE:
1418c0984e5SSebastian Reichel 		__set_charger(info, 0);
1428c0984e5SSebastian Reichel 		dev_dbg(chip->dev, "Charging is done\n");
1438c0984e5SSebastian Reichel 		break;
1448c0984e5SSebastian Reichel 	case MAX8925_IRQ_VCHG_TOPOFF:
1458c0984e5SSebastian Reichel 		dev_dbg(chip->dev, "Charging in top-off mode\n");
1468c0984e5SSebastian Reichel 		break;
1478c0984e5SSebastian Reichel 	case MAX8925_IRQ_VCHG_TMR_FAULT:
1488c0984e5SSebastian Reichel 		__set_charger(info, 0);
1498c0984e5SSebastian Reichel 		dev_dbg(chip->dev, "Safe timer is expired\n");
1508c0984e5SSebastian Reichel 		break;
1518c0984e5SSebastian Reichel 	case MAX8925_IRQ_VCHG_RST:
1528c0984e5SSebastian Reichel 		__set_charger(info, 0);
1538c0984e5SSebastian Reichel 		dev_dbg(chip->dev, "Charger is reset\n");
1548c0984e5SSebastian Reichel 		break;
1558c0984e5SSebastian Reichel 	}
1568c0984e5SSebastian Reichel 	return IRQ_HANDLED;
1578c0984e5SSebastian Reichel }
1588c0984e5SSebastian Reichel 
start_measure(struct max8925_power_info * info,int type)1598c0984e5SSebastian Reichel static int start_measure(struct max8925_power_info *info, int type)
1608c0984e5SSebastian Reichel {
1618c0984e5SSebastian Reichel 	unsigned char buf[2] = {0, 0};
1628c0984e5SSebastian Reichel 	int meas_cmd;
1638c0984e5SSebastian Reichel 	int meas_reg = 0, ret;
1648c0984e5SSebastian Reichel 
1658c0984e5SSebastian Reichel 	switch (type) {
1668c0984e5SSebastian Reichel 	case MEASURE_VCHG:
1678c0984e5SSebastian Reichel 		meas_cmd = MAX8925_CMD_VCHG;
1688c0984e5SSebastian Reichel 		meas_reg = MAX8925_ADC_VCHG;
1698c0984e5SSebastian Reichel 		break;
1708c0984e5SSebastian Reichel 	case MEASURE_VBBATT:
1718c0984e5SSebastian Reichel 		meas_cmd = MAX8925_CMD_VBBATT;
1728c0984e5SSebastian Reichel 		meas_reg = MAX8925_ADC_VBBATT;
1738c0984e5SSebastian Reichel 		break;
1748c0984e5SSebastian Reichel 	case MEASURE_VMBATT:
1758c0984e5SSebastian Reichel 		meas_cmd = MAX8925_CMD_VMBATT;
1768c0984e5SSebastian Reichel 		meas_reg = MAX8925_ADC_VMBATT;
1778c0984e5SSebastian Reichel 		break;
1788c0984e5SSebastian Reichel 	case MEASURE_ISNS:
1798c0984e5SSebastian Reichel 		meas_cmd = MAX8925_CMD_ISNS;
1808c0984e5SSebastian Reichel 		meas_reg = MAX8925_ADC_ISNS;
1818c0984e5SSebastian Reichel 		break;
1828c0984e5SSebastian Reichel 	default:
1838c0984e5SSebastian Reichel 		return -EINVAL;
1848c0984e5SSebastian Reichel 	}
1858c0984e5SSebastian Reichel 
1868c0984e5SSebastian Reichel 	max8925_reg_write(info->adc, meas_cmd, 0);
1878c0984e5SSebastian Reichel 	max8925_bulk_read(info->adc, meas_reg, 2, buf);
1888c0984e5SSebastian Reichel 	ret = ((buf[0]<<8) | buf[1]) >> 4;
1898c0984e5SSebastian Reichel 
1908c0984e5SSebastian Reichel 	return ret;
1918c0984e5SSebastian Reichel }
1928c0984e5SSebastian Reichel 
max8925_ac_get_prop(struct power_supply * psy,enum power_supply_property psp,union power_supply_propval * val)1938c0984e5SSebastian Reichel static int max8925_ac_get_prop(struct power_supply *psy,
1948c0984e5SSebastian Reichel 			       enum power_supply_property psp,
1958c0984e5SSebastian Reichel 			       union power_supply_propval *val)
1968c0984e5SSebastian Reichel {
1978c0984e5SSebastian Reichel 	struct max8925_power_info *info = dev_get_drvdata(psy->dev.parent);
1988c0984e5SSebastian Reichel 	int ret = 0;
1998c0984e5SSebastian Reichel 
2008c0984e5SSebastian Reichel 	switch (psp) {
2018c0984e5SSebastian Reichel 	case POWER_SUPPLY_PROP_ONLINE:
2028c0984e5SSebastian Reichel 		val->intval = info->ac_online;
2038c0984e5SSebastian Reichel 		break;
2048c0984e5SSebastian Reichel 	case POWER_SUPPLY_PROP_VOLTAGE_NOW:
2058c0984e5SSebastian Reichel 		if (info->ac_online) {
2068c0984e5SSebastian Reichel 			ret = start_measure(info, MEASURE_VCHG);
2078c0984e5SSebastian Reichel 			if (ret >= 0) {
2088c0984e5SSebastian Reichel 				val->intval = ret * 2000;	/* unit is uV */
2098c0984e5SSebastian Reichel 				goto out;
2108c0984e5SSebastian Reichel 			}
2118c0984e5SSebastian Reichel 		}
2128c0984e5SSebastian Reichel 		ret = -ENODATA;
2138c0984e5SSebastian Reichel 		break;
2148c0984e5SSebastian Reichel 	default:
2158c0984e5SSebastian Reichel 		ret = -ENODEV;
2168c0984e5SSebastian Reichel 		break;
2178c0984e5SSebastian Reichel 	}
2188c0984e5SSebastian Reichel out:
2198c0984e5SSebastian Reichel 	return ret;
2208c0984e5SSebastian Reichel }
2218c0984e5SSebastian Reichel 
2228c0984e5SSebastian Reichel static enum power_supply_property max8925_ac_props[] = {
2238c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_ONLINE,
2248c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_VOLTAGE_NOW,
2258c0984e5SSebastian Reichel };
2268c0984e5SSebastian Reichel 
max8925_usb_get_prop(struct power_supply * psy,enum power_supply_property psp,union power_supply_propval * val)2278c0984e5SSebastian Reichel static int max8925_usb_get_prop(struct power_supply *psy,
2288c0984e5SSebastian Reichel 				enum power_supply_property psp,
2298c0984e5SSebastian Reichel 				union power_supply_propval *val)
2308c0984e5SSebastian Reichel {
2318c0984e5SSebastian Reichel 	struct max8925_power_info *info = dev_get_drvdata(psy->dev.parent);
2328c0984e5SSebastian Reichel 	int ret = 0;
2338c0984e5SSebastian Reichel 
2348c0984e5SSebastian Reichel 	switch (psp) {
2358c0984e5SSebastian Reichel 	case POWER_SUPPLY_PROP_ONLINE:
2368c0984e5SSebastian Reichel 		val->intval = info->usb_online;
2378c0984e5SSebastian Reichel 		break;
2388c0984e5SSebastian Reichel 	case POWER_SUPPLY_PROP_VOLTAGE_NOW:
2398c0984e5SSebastian Reichel 		if (info->usb_online) {
2408c0984e5SSebastian Reichel 			ret = start_measure(info, MEASURE_VCHG);
2418c0984e5SSebastian Reichel 			if (ret >= 0) {
2428c0984e5SSebastian Reichel 				val->intval = ret * 2000;	/* unit is uV */
2438c0984e5SSebastian Reichel 				goto out;
2448c0984e5SSebastian Reichel 			}
2458c0984e5SSebastian Reichel 		}
2468c0984e5SSebastian Reichel 		ret = -ENODATA;
2478c0984e5SSebastian Reichel 		break;
2488c0984e5SSebastian Reichel 	default:
2498c0984e5SSebastian Reichel 		ret = -ENODEV;
2508c0984e5SSebastian Reichel 		break;
2518c0984e5SSebastian Reichel 	}
2528c0984e5SSebastian Reichel out:
2538c0984e5SSebastian Reichel 	return ret;
2548c0984e5SSebastian Reichel }
2558c0984e5SSebastian Reichel 
2568c0984e5SSebastian Reichel static enum power_supply_property max8925_usb_props[] = {
2578c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_ONLINE,
2588c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_VOLTAGE_NOW,
2598c0984e5SSebastian Reichel };
2608c0984e5SSebastian Reichel 
max8925_bat_get_prop(struct power_supply * psy,enum power_supply_property psp,union power_supply_propval * val)2618c0984e5SSebastian Reichel static int max8925_bat_get_prop(struct power_supply *psy,
2628c0984e5SSebastian Reichel 				enum power_supply_property psp,
2638c0984e5SSebastian Reichel 				union power_supply_propval *val)
2648c0984e5SSebastian Reichel {
2658c0984e5SSebastian Reichel 	struct max8925_power_info *info = dev_get_drvdata(psy->dev.parent);
2668c0984e5SSebastian Reichel 	int ret = 0;
2678c0984e5SSebastian Reichel 
2688c0984e5SSebastian Reichel 	switch (psp) {
2698c0984e5SSebastian Reichel 	case POWER_SUPPLY_PROP_ONLINE:
2708c0984e5SSebastian Reichel 		val->intval = info->bat_online;
2718c0984e5SSebastian Reichel 		break;
2728c0984e5SSebastian Reichel 	case POWER_SUPPLY_PROP_VOLTAGE_NOW:
2738c0984e5SSebastian Reichel 		if (info->bat_online) {
2748c0984e5SSebastian Reichel 			ret = start_measure(info, MEASURE_VMBATT);
2758c0984e5SSebastian Reichel 			if (ret >= 0) {
2768c0984e5SSebastian Reichel 				val->intval = ret * 2000;	/* unit is uV */
2778c0984e5SSebastian Reichel 				ret = 0;
2788c0984e5SSebastian Reichel 				break;
2798c0984e5SSebastian Reichel 			}
2808c0984e5SSebastian Reichel 		}
2818c0984e5SSebastian Reichel 		ret = -ENODATA;
2828c0984e5SSebastian Reichel 		break;
2838c0984e5SSebastian Reichel 	case POWER_SUPPLY_PROP_CURRENT_NOW:
2848c0984e5SSebastian Reichel 		if (info->bat_online) {
2858c0984e5SSebastian Reichel 			ret = start_measure(info, MEASURE_ISNS);
2868c0984e5SSebastian Reichel 			if (ret >= 0) {
2878c0984e5SSebastian Reichel 				/* assume r_sns is 0.02 */
2888c0984e5SSebastian Reichel 				ret = ((ret * 6250) - 3125) /* uA */;
2898c0984e5SSebastian Reichel 				val->intval = 0;
2908c0984e5SSebastian Reichel 				if (ret > 0)
2918c0984e5SSebastian Reichel 					val->intval = ret; /* unit is mA */
2928c0984e5SSebastian Reichel 				ret = 0;
2938c0984e5SSebastian Reichel 				break;
2948c0984e5SSebastian Reichel 			}
2958c0984e5SSebastian Reichel 		}
2968c0984e5SSebastian Reichel 		ret = -ENODATA;
2978c0984e5SSebastian Reichel 		break;
2988c0984e5SSebastian Reichel 	case POWER_SUPPLY_PROP_CHARGE_TYPE:
2998c0984e5SSebastian Reichel 		if (!info->bat_online) {
3008c0984e5SSebastian Reichel 			ret = -ENODATA;
3018c0984e5SSebastian Reichel 			break;
3028c0984e5SSebastian Reichel 		}
3038c0984e5SSebastian Reichel 		ret = max8925_reg_read(info->gpm, MAX8925_CHG_STATUS);
3048c0984e5SSebastian Reichel 		ret = (ret & MAX8925_CHG_STAT_MODE_MASK) >> 2;
3058c0984e5SSebastian Reichel 		switch (ret) {
3068c0984e5SSebastian Reichel 		case 1:
3078c0984e5SSebastian Reichel 			val->intval = POWER_SUPPLY_CHARGE_TYPE_FAST;
3088c0984e5SSebastian Reichel 			break;
3098c0984e5SSebastian Reichel 		case 0:
3108c0984e5SSebastian Reichel 		case 2:
3118c0984e5SSebastian Reichel 			val->intval = POWER_SUPPLY_CHARGE_TYPE_TRICKLE;
3128c0984e5SSebastian Reichel 			break;
3138c0984e5SSebastian Reichel 		case 3:
3148c0984e5SSebastian Reichel 			val->intval = POWER_SUPPLY_CHARGE_TYPE_NONE;
3158c0984e5SSebastian Reichel 			break;
3168c0984e5SSebastian Reichel 		}
3178c0984e5SSebastian Reichel 		ret = 0;
3188c0984e5SSebastian Reichel 		break;
3198c0984e5SSebastian Reichel 	case POWER_SUPPLY_PROP_STATUS:
3208c0984e5SSebastian Reichel 		if (!info->bat_online) {
3218c0984e5SSebastian Reichel 			ret = -ENODATA;
3228c0984e5SSebastian Reichel 			break;
3238c0984e5SSebastian Reichel 		}
3248c0984e5SSebastian Reichel 		ret = max8925_reg_read(info->gpm, MAX8925_CHG_STATUS);
3258c0984e5SSebastian Reichel 		if (info->usb_online || info->ac_online) {
3268c0984e5SSebastian Reichel 			val->intval = POWER_SUPPLY_STATUS_NOT_CHARGING;
3278c0984e5SSebastian Reichel 			if (ret & MAX8925_CHG_STAT_EN_MASK)
3288c0984e5SSebastian Reichel 				val->intval = POWER_SUPPLY_STATUS_CHARGING;
3298c0984e5SSebastian Reichel 		} else
3308c0984e5SSebastian Reichel 			val->intval = POWER_SUPPLY_STATUS_DISCHARGING;
3318c0984e5SSebastian Reichel 		ret = 0;
3328c0984e5SSebastian Reichel 		break;
3338c0984e5SSebastian Reichel 	default:
3348c0984e5SSebastian Reichel 		ret = -ENODEV;
3358c0984e5SSebastian Reichel 		break;
3368c0984e5SSebastian Reichel 	}
3378c0984e5SSebastian Reichel 	return ret;
3388c0984e5SSebastian Reichel }
3398c0984e5SSebastian Reichel 
3408c0984e5SSebastian Reichel static enum power_supply_property max8925_battery_props[] = {
3418c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_ONLINE,
3428c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_VOLTAGE_NOW,
3438c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_CURRENT_NOW,
3448c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_CHARGE_TYPE,
3458c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_STATUS,
3468c0984e5SSebastian Reichel };
3478c0984e5SSebastian Reichel 
3488c0984e5SSebastian Reichel static const struct power_supply_desc ac_desc = {
3498c0984e5SSebastian Reichel 	.name		= "max8925-ac",
3508c0984e5SSebastian Reichel 	.type		= POWER_SUPPLY_TYPE_MAINS,
3518c0984e5SSebastian Reichel 	.properties	= max8925_ac_props,
3528c0984e5SSebastian Reichel 	.num_properties	= ARRAY_SIZE(max8925_ac_props),
3538c0984e5SSebastian Reichel 	.get_property	= max8925_ac_get_prop,
3548c0984e5SSebastian Reichel };
3558c0984e5SSebastian Reichel 
3568c0984e5SSebastian Reichel static const struct power_supply_desc usb_desc = {
3578c0984e5SSebastian Reichel 	.name		= "max8925-usb",
3588c0984e5SSebastian Reichel 	.type		= POWER_SUPPLY_TYPE_USB,
3598c0984e5SSebastian Reichel 	.properties	= max8925_usb_props,
3608c0984e5SSebastian Reichel 	.num_properties	= ARRAY_SIZE(max8925_usb_props),
3618c0984e5SSebastian Reichel 	.get_property	= max8925_usb_get_prop,
3628c0984e5SSebastian Reichel };
3638c0984e5SSebastian Reichel 
3648c0984e5SSebastian Reichel static const struct power_supply_desc battery_desc = {
3658c0984e5SSebastian Reichel 	.name		= "max8925-battery",
3668c0984e5SSebastian Reichel 	.type		= POWER_SUPPLY_TYPE_BATTERY,
3678c0984e5SSebastian Reichel 	.properties	= max8925_battery_props,
3688c0984e5SSebastian Reichel 	.num_properties	= ARRAY_SIZE(max8925_battery_props),
3698c0984e5SSebastian Reichel 	.get_property	= max8925_bat_get_prop,
3708c0984e5SSebastian Reichel };
3718c0984e5SSebastian Reichel 
3728c0984e5SSebastian Reichel #define REQUEST_IRQ(_irq, _name)					\
3738c0984e5SSebastian Reichel do {									\
3748c0984e5SSebastian Reichel 	ret = request_threaded_irq(chip->irq_base + _irq, NULL,		\
3758c0984e5SSebastian Reichel 				    max8925_charger_handler,		\
3768c0984e5SSebastian Reichel 				    IRQF_ONESHOT, _name, info);		\
3778c0984e5SSebastian Reichel 	if (ret)							\
3788c0984e5SSebastian Reichel 		dev_err(chip->dev, "Failed to request IRQ #%d: %d\n",	\
3798c0984e5SSebastian Reichel 			_irq, ret);					\
3808c0984e5SSebastian Reichel } while (0)
3818c0984e5SSebastian Reichel 
max8925_init_charger(struct max8925_chip * chip,struct max8925_power_info * info)3828c0984e5SSebastian Reichel static int max8925_init_charger(struct max8925_chip *chip,
3838c0984e5SSebastian Reichel 					  struct max8925_power_info *info)
3848c0984e5SSebastian Reichel {
3858c0984e5SSebastian Reichel 	int ret;
3868c0984e5SSebastian Reichel 
3878c0984e5SSebastian Reichel 	REQUEST_IRQ(MAX8925_IRQ_VCHG_DC_OVP, "ac-ovp");
3888c0984e5SSebastian Reichel 	if (!info->no_insert_detect) {
3898c0984e5SSebastian Reichel 		REQUEST_IRQ(MAX8925_IRQ_VCHG_DC_F, "ac-remove");
3908c0984e5SSebastian Reichel 		REQUEST_IRQ(MAX8925_IRQ_VCHG_DC_R, "ac-insert");
3918c0984e5SSebastian Reichel 	}
3928c0984e5SSebastian Reichel 	if (!info->no_temp_support) {
3938c0984e5SSebastian Reichel 		REQUEST_IRQ(MAX8925_IRQ_VCHG_THM_OK_R, "batt-temp-in-range");
3948c0984e5SSebastian Reichel 		REQUEST_IRQ(MAX8925_IRQ_VCHG_THM_OK_F, "batt-temp-out-range");
3958c0984e5SSebastian Reichel 	}
3968c0984e5SSebastian Reichel 	REQUEST_IRQ(MAX8925_IRQ_VCHG_SYSLOW_F, "vsys-high");
3978c0984e5SSebastian Reichel 	REQUEST_IRQ(MAX8925_IRQ_VCHG_SYSLOW_R, "vsys-low");
3988c0984e5SSebastian Reichel 	REQUEST_IRQ(MAX8925_IRQ_VCHG_RST, "charger-reset");
3998c0984e5SSebastian Reichel 	REQUEST_IRQ(MAX8925_IRQ_VCHG_DONE, "charger-done");
4008c0984e5SSebastian Reichel 	REQUEST_IRQ(MAX8925_IRQ_VCHG_TOPOFF, "charger-topoff");
4018c0984e5SSebastian Reichel 	REQUEST_IRQ(MAX8925_IRQ_VCHG_TMR_FAULT, "charger-timer-expire");
4028c0984e5SSebastian Reichel 
4038c0984e5SSebastian Reichel 	info->usb_online = 0;
4048c0984e5SSebastian Reichel 	info->bat_online = 0;
4058c0984e5SSebastian Reichel 
4068c0984e5SSebastian Reichel 	/* check for power - can miss interrupt at boot time */
4078c0984e5SSebastian Reichel 	if (start_measure(info, MEASURE_VCHG) * 2000 > 500000)
4088c0984e5SSebastian Reichel 		info->ac_online = 1;
4098c0984e5SSebastian Reichel 	else
4108c0984e5SSebastian Reichel 		info->ac_online = 0;
4118c0984e5SSebastian Reichel 
4128c0984e5SSebastian Reichel 	ret = max8925_reg_read(info->gpm, MAX8925_CHG_STATUS);
4138c0984e5SSebastian Reichel 	if (ret >= 0) {
4148c0984e5SSebastian Reichel 		/*
4158c0984e5SSebastian Reichel 		 * If battery detection is enabled, ID pin of battery is
4168c0984e5SSebastian Reichel 		 * connected to MBDET pin of MAX8925. It could be used to
4178c0984e5SSebastian Reichel 		 * detect battery presence.
4188c0984e5SSebastian Reichel 		 * Otherwise, we have to assume that battery is always on.
4198c0984e5SSebastian Reichel 		 */
4208c0984e5SSebastian Reichel 		if (info->batt_detect)
4218c0984e5SSebastian Reichel 			info->bat_online = (ret & MAX8925_CHG_MBDET) ? 0 : 1;
4228c0984e5SSebastian Reichel 		else
4238c0984e5SSebastian Reichel 			info->bat_online = 1;
4248c0984e5SSebastian Reichel 		if (ret & MAX8925_CHG_AC_RANGE_MASK)
4258c0984e5SSebastian Reichel 			info->ac_online = 1;
4268c0984e5SSebastian Reichel 		else
4278c0984e5SSebastian Reichel 			info->ac_online = 0;
4288c0984e5SSebastian Reichel 	}
4298c0984e5SSebastian Reichel 	/* disable charge */
4308c0984e5SSebastian Reichel 	max8925_set_bits(info->gpm, MAX8925_CHG_CNTL1, 1 << 7, 1 << 7);
4318c0984e5SSebastian Reichel 	/* set charging current in charge topoff mode */
4328c0984e5SSebastian Reichel 	max8925_set_bits(info->gpm, MAX8925_CHG_CNTL1, 3 << 5,
4338c0984e5SSebastian Reichel 			 info->topoff_threshold << 5);
4348c0984e5SSebastian Reichel 	/* set charing current in fast charge mode */
4358c0984e5SSebastian Reichel 	max8925_set_bits(info->gpm, MAX8925_CHG_CNTL1, 7, info->fast_charge);
4368c0984e5SSebastian Reichel 
4378c0984e5SSebastian Reichel 	return 0;
4388c0984e5SSebastian Reichel }
4398c0984e5SSebastian Reichel 
max8925_deinit_charger(struct max8925_power_info * info)4408c0984e5SSebastian Reichel static int max8925_deinit_charger(struct max8925_power_info *info)
4418c0984e5SSebastian Reichel {
4428c0984e5SSebastian Reichel 	struct max8925_chip *chip = info->chip;
4438c0984e5SSebastian Reichel 	int irq;
4448c0984e5SSebastian Reichel 
4458c0984e5SSebastian Reichel 	irq = chip->irq_base + MAX8925_IRQ_VCHG_DC_OVP;
4468c0984e5SSebastian Reichel 	for (; irq <= chip->irq_base + MAX8925_IRQ_VCHG_TMR_FAULT; irq++)
4478c0984e5SSebastian Reichel 		free_irq(irq, info);
4488c0984e5SSebastian Reichel 
4498c0984e5SSebastian Reichel 	return 0;
4508c0984e5SSebastian Reichel }
4518c0984e5SSebastian Reichel 
4528c0984e5SSebastian Reichel #ifdef CONFIG_OF
4538c0984e5SSebastian Reichel static struct max8925_power_pdata *
max8925_power_dt_init(struct platform_device * pdev)4548c0984e5SSebastian Reichel max8925_power_dt_init(struct platform_device *pdev)
4558c0984e5SSebastian Reichel {
4568c0984e5SSebastian Reichel 	struct device_node *nproot = pdev->dev.parent->of_node;
4578c0984e5SSebastian Reichel 	struct device_node *np;
4588c0984e5SSebastian Reichel 	int batt_detect;
4598c0984e5SSebastian Reichel 	int topoff_threshold;
4608c0984e5SSebastian Reichel 	int fast_charge;
4618c0984e5SSebastian Reichel 	int no_temp_support;
4628c0984e5SSebastian Reichel 	int no_insert_detect;
4638c0984e5SSebastian Reichel 	struct max8925_power_pdata *pdata;
4648c0984e5SSebastian Reichel 
4658c0984e5SSebastian Reichel 	if (!nproot)
4668c0984e5SSebastian Reichel 		return pdev->dev.platform_data;
4678c0984e5SSebastian Reichel 
4688c0984e5SSebastian Reichel 	np = of_get_child_by_name(nproot, "charger");
4698c0984e5SSebastian Reichel 	if (!np) {
4708c0984e5SSebastian Reichel 		dev_err(&pdev->dev, "failed to find charger node\n");
4718c0984e5SSebastian Reichel 		return NULL;
4728c0984e5SSebastian Reichel 	}
4738c0984e5SSebastian Reichel 
4748c0984e5SSebastian Reichel 	pdata = devm_kzalloc(&pdev->dev,
4758c0984e5SSebastian Reichel 			sizeof(struct max8925_power_pdata),
4768c0984e5SSebastian Reichel 			GFP_KERNEL);
4778c0984e5SSebastian Reichel 	if (!pdata)
4788c0984e5SSebastian Reichel 		goto ret;
4798c0984e5SSebastian Reichel 
4808c0984e5SSebastian Reichel 	of_property_read_u32(np, "topoff-threshold", &topoff_threshold);
4818c0984e5SSebastian Reichel 	of_property_read_u32(np, "batt-detect", &batt_detect);
4828c0984e5SSebastian Reichel 	of_property_read_u32(np, "fast-charge", &fast_charge);
4838c0984e5SSebastian Reichel 	of_property_read_u32(np, "no-insert-detect", &no_insert_detect);
4848c0984e5SSebastian Reichel 	of_property_read_u32(np, "no-temp-support", &no_temp_support);
4858c0984e5SSebastian Reichel 
4868c0984e5SSebastian Reichel 	pdata->batt_detect = batt_detect;
4878c0984e5SSebastian Reichel 	pdata->fast_charge = fast_charge;
4888c0984e5SSebastian Reichel 	pdata->topoff_threshold = topoff_threshold;
4898c0984e5SSebastian Reichel 	pdata->no_insert_detect = no_insert_detect;
4908c0984e5SSebastian Reichel 	pdata->no_temp_support = no_temp_support;
4918c0984e5SSebastian Reichel 
4928c0984e5SSebastian Reichel ret:
4938c0984e5SSebastian Reichel 	of_node_put(np);
4948c0984e5SSebastian Reichel 	return pdata;
4958c0984e5SSebastian Reichel }
4968c0984e5SSebastian Reichel #else
4978c0984e5SSebastian Reichel static struct max8925_power_pdata *
max8925_power_dt_init(struct platform_device * pdev)4988c0984e5SSebastian Reichel max8925_power_dt_init(struct platform_device *pdev)
4998c0984e5SSebastian Reichel {
5008c0984e5SSebastian Reichel 	return pdev->dev.platform_data;
5018c0984e5SSebastian Reichel }
5028c0984e5SSebastian Reichel #endif
5038c0984e5SSebastian Reichel 
max8925_power_probe(struct platform_device * pdev)5048c0984e5SSebastian Reichel static int max8925_power_probe(struct platform_device *pdev)
5058c0984e5SSebastian Reichel {
5068c0984e5SSebastian Reichel 	struct max8925_chip *chip = dev_get_drvdata(pdev->dev.parent);
5078c0984e5SSebastian Reichel 	struct power_supply_config psy_cfg = {}; /* Only for ac and usb */
5088c0984e5SSebastian Reichel 	struct max8925_power_pdata *pdata = NULL;
5098c0984e5SSebastian Reichel 	struct max8925_power_info *info;
5108c0984e5SSebastian Reichel 	int ret;
5118c0984e5SSebastian Reichel 
5128c0984e5SSebastian Reichel 	pdata = max8925_power_dt_init(pdev);
5138c0984e5SSebastian Reichel 	if (!pdata) {
5148c0984e5SSebastian Reichel 		dev_err(&pdev->dev, "platform data isn't assigned to "
5158c0984e5SSebastian Reichel 			"power supply\n");
5168c0984e5SSebastian Reichel 		return -EINVAL;
5178c0984e5SSebastian Reichel 	}
5188c0984e5SSebastian Reichel 
5198c0984e5SSebastian Reichel 	info = devm_kzalloc(&pdev->dev, sizeof(struct max8925_power_info),
5208c0984e5SSebastian Reichel 				GFP_KERNEL);
5218c0984e5SSebastian Reichel 	if (!info)
5228c0984e5SSebastian Reichel 		return -ENOMEM;
5238c0984e5SSebastian Reichel 	info->chip = chip;
5248c0984e5SSebastian Reichel 	info->gpm = chip->i2c;
5258c0984e5SSebastian Reichel 	info->adc = chip->adc;
5268c0984e5SSebastian Reichel 	platform_set_drvdata(pdev, info);
5278c0984e5SSebastian Reichel 
5288c0984e5SSebastian Reichel 	psy_cfg.supplied_to = pdata->supplied_to;
5298c0984e5SSebastian Reichel 	psy_cfg.num_supplicants = pdata->num_supplicants;
5308c0984e5SSebastian Reichel 
5318c0984e5SSebastian Reichel 	info->ac = power_supply_register(&pdev->dev, &ac_desc, &psy_cfg);
5328c0984e5SSebastian Reichel 	if (IS_ERR(info->ac)) {
5338c0984e5SSebastian Reichel 		ret = PTR_ERR(info->ac);
5348c0984e5SSebastian Reichel 		goto out;
5358c0984e5SSebastian Reichel 	}
5368c0984e5SSebastian Reichel 	info->ac->dev.parent = &pdev->dev;
5378c0984e5SSebastian Reichel 
5388c0984e5SSebastian Reichel 	info->usb = power_supply_register(&pdev->dev, &usb_desc, &psy_cfg);
5398c0984e5SSebastian Reichel 	if (IS_ERR(info->usb)) {
5408c0984e5SSebastian Reichel 		ret = PTR_ERR(info->usb);
5418c0984e5SSebastian Reichel 		goto out_unregister_ac;
5428c0984e5SSebastian Reichel 	}
5438c0984e5SSebastian Reichel 	info->usb->dev.parent = &pdev->dev;
5448c0984e5SSebastian Reichel 
5458c0984e5SSebastian Reichel 	info->battery = power_supply_register(&pdev->dev, &battery_desc, NULL);
5468c0984e5SSebastian Reichel 	if (IS_ERR(info->battery)) {
5478c0984e5SSebastian Reichel 		ret = PTR_ERR(info->battery);
5488c0984e5SSebastian Reichel 		goto out_unregister_usb;
5498c0984e5SSebastian Reichel 	}
5508c0984e5SSebastian Reichel 	info->battery->dev.parent = &pdev->dev;
5518c0984e5SSebastian Reichel 
5528c0984e5SSebastian Reichel 	info->batt_detect = pdata->batt_detect;
5538c0984e5SSebastian Reichel 	info->topoff_threshold = pdata->topoff_threshold;
5548c0984e5SSebastian Reichel 	info->fast_charge = pdata->fast_charge;
5558c0984e5SSebastian Reichel 	info->set_charger = pdata->set_charger;
5568c0984e5SSebastian Reichel 	info->no_temp_support = pdata->no_temp_support;
5578c0984e5SSebastian Reichel 	info->no_insert_detect = pdata->no_insert_detect;
5588c0984e5SSebastian Reichel 
5598c0984e5SSebastian Reichel 	max8925_init_charger(chip, info);
5608c0984e5SSebastian Reichel 	return 0;
5618c0984e5SSebastian Reichel out_unregister_usb:
5628c0984e5SSebastian Reichel 	power_supply_unregister(info->usb);
5638c0984e5SSebastian Reichel out_unregister_ac:
5648c0984e5SSebastian Reichel 	power_supply_unregister(info->ac);
5658c0984e5SSebastian Reichel out:
5668c0984e5SSebastian Reichel 	return ret;
5678c0984e5SSebastian Reichel }
5688c0984e5SSebastian Reichel 
max8925_power_remove(struct platform_device * pdev)5698c0984e5SSebastian Reichel static int max8925_power_remove(struct platform_device *pdev)
5708c0984e5SSebastian Reichel {
5718c0984e5SSebastian Reichel 	struct max8925_power_info *info = platform_get_drvdata(pdev);
5728c0984e5SSebastian Reichel 
5738c0984e5SSebastian Reichel 	if (info) {
5748c0984e5SSebastian Reichel 		power_supply_unregister(info->ac);
5758c0984e5SSebastian Reichel 		power_supply_unregister(info->usb);
5768c0984e5SSebastian Reichel 		power_supply_unregister(info->battery);
5778c0984e5SSebastian Reichel 		max8925_deinit_charger(info);
5788c0984e5SSebastian Reichel 	}
5798c0984e5SSebastian Reichel 	return 0;
5808c0984e5SSebastian Reichel }
5818c0984e5SSebastian Reichel 
5828c0984e5SSebastian Reichel static struct platform_driver max8925_power_driver = {
5838c0984e5SSebastian Reichel 	.probe	= max8925_power_probe,
5848c0984e5SSebastian Reichel 	.remove	= max8925_power_remove,
5858c0984e5SSebastian Reichel 	.driver	= {
5868c0984e5SSebastian Reichel 		.name	= "max8925-power",
5878c0984e5SSebastian Reichel 	},
5888c0984e5SSebastian Reichel };
5898c0984e5SSebastian Reichel 
5908c0984e5SSebastian Reichel module_platform_driver(max8925_power_driver);
5918c0984e5SSebastian Reichel 
5928c0984e5SSebastian Reichel MODULE_LICENSE("GPL");
5938c0984e5SSebastian Reichel MODULE_DESCRIPTION("Power supply driver for MAX8925");
5948c0984e5SSebastian Reichel MODULE_ALIAS("platform:max8925-power");
595