xref: /openbmc/linux/drivers/power/supply/max77693_charger.c (revision 9a87ffc99ec8eb8d35eed7c4f816d75f5cc9662e)
17b38ebdfSKrzysztof Kozlowski // SPDX-License-Identifier: GPL-2.0+
27b38ebdfSKrzysztof Kozlowski //
37b38ebdfSKrzysztof Kozlowski // max77693_charger.c - Battery charger driver for the Maxim 77693
47b38ebdfSKrzysztof Kozlowski //
57b38ebdfSKrzysztof Kozlowski // Copyright (C) 2014 Samsung Electronics
67b38ebdfSKrzysztof Kozlowski // Krzysztof Kozlowski <krzk@kernel.org>
78c0984e5SSebastian Reichel 
88c0984e5SSebastian Reichel #include <linux/module.h>
98c0984e5SSebastian Reichel #include <linux/platform_device.h>
108c0984e5SSebastian Reichel #include <linux/power_supply.h>
118c0984e5SSebastian Reichel #include <linux/regmap.h>
128c0984e5SSebastian Reichel #include <linux/mfd/max77693.h>
138c0984e5SSebastian Reichel #include <linux/mfd/max77693-common.h>
148c0984e5SSebastian Reichel #include <linux/mfd/max77693-private.h>
158c0984e5SSebastian Reichel 
168c0984e5SSebastian Reichel #define MAX77693_CHARGER_NAME				"max77693-charger"
178c0984e5SSebastian Reichel static const char *max77693_charger_model		= "MAX77693";
188c0984e5SSebastian Reichel static const char *max77693_charger_manufacturer	= "Maxim Integrated";
198c0984e5SSebastian Reichel 
208c0984e5SSebastian Reichel struct max77693_charger {
218c0984e5SSebastian Reichel 	struct device		*dev;
228c0984e5SSebastian Reichel 	struct max77693_dev	*max77693;
238c0984e5SSebastian Reichel 	struct power_supply	*charger;
248c0984e5SSebastian Reichel 
258c0984e5SSebastian Reichel 	u32 constant_volt;
268c0984e5SSebastian Reichel 	u32 min_system_volt;
278c0984e5SSebastian Reichel 	u32 thermal_regulation_temp;
288c0984e5SSebastian Reichel 	u32 batttery_overcurrent;
298c0984e5SSebastian Reichel 	u32 charge_input_threshold_volt;
308c0984e5SSebastian Reichel };
318c0984e5SSebastian Reichel 
max77693_get_charger_state(struct regmap * regmap,int * val)328c0984e5SSebastian Reichel static int max77693_get_charger_state(struct regmap *regmap, int *val)
338c0984e5SSebastian Reichel {
348c0984e5SSebastian Reichel 	int ret;
358c0984e5SSebastian Reichel 	unsigned int data;
368c0984e5SSebastian Reichel 
378c0984e5SSebastian Reichel 	ret = regmap_read(regmap, MAX77693_CHG_REG_CHG_DETAILS_01, &data);
388c0984e5SSebastian Reichel 	if (ret < 0)
398c0984e5SSebastian Reichel 		return ret;
408c0984e5SSebastian Reichel 
418c0984e5SSebastian Reichel 	data &= CHG_DETAILS_01_CHG_MASK;
428c0984e5SSebastian Reichel 	data >>= CHG_DETAILS_01_CHG_SHIFT;
438c0984e5SSebastian Reichel 
448c0984e5SSebastian Reichel 	switch (data) {
458c0984e5SSebastian Reichel 	case MAX77693_CHARGING_PREQUALIFICATION:
468c0984e5SSebastian Reichel 	case MAX77693_CHARGING_FAST_CONST_CURRENT:
478c0984e5SSebastian Reichel 	case MAX77693_CHARGING_FAST_CONST_VOLTAGE:
488c0984e5SSebastian Reichel 	case MAX77693_CHARGING_TOP_OFF:
498c0984e5SSebastian Reichel 	/* In high temp the charging current is reduced, but still charging */
508c0984e5SSebastian Reichel 	case MAX77693_CHARGING_HIGH_TEMP:
518c0984e5SSebastian Reichel 		*val = POWER_SUPPLY_STATUS_CHARGING;
528c0984e5SSebastian Reichel 		break;
538c0984e5SSebastian Reichel 	case MAX77693_CHARGING_DONE:
548c0984e5SSebastian Reichel 		*val = POWER_SUPPLY_STATUS_FULL;
558c0984e5SSebastian Reichel 		break;
568c0984e5SSebastian Reichel 	case MAX77693_CHARGING_TIMER_EXPIRED:
578c0984e5SSebastian Reichel 	case MAX77693_CHARGING_THERMISTOR_SUSPEND:
588c0984e5SSebastian Reichel 		*val = POWER_SUPPLY_STATUS_NOT_CHARGING;
598c0984e5SSebastian Reichel 		break;
608c0984e5SSebastian Reichel 	case MAX77693_CHARGING_OFF:
618c0984e5SSebastian Reichel 	case MAX77693_CHARGING_OVER_TEMP:
628c0984e5SSebastian Reichel 	case MAX77693_CHARGING_WATCHDOG_EXPIRED:
638c0984e5SSebastian Reichel 		*val = POWER_SUPPLY_STATUS_DISCHARGING;
648c0984e5SSebastian Reichel 		break;
658c0984e5SSebastian Reichel 	case MAX77693_CHARGING_RESERVED:
668c0984e5SSebastian Reichel 	default:
678c0984e5SSebastian Reichel 		*val = POWER_SUPPLY_STATUS_UNKNOWN;
688c0984e5SSebastian Reichel 	}
698c0984e5SSebastian Reichel 
708c0984e5SSebastian Reichel 	return 0;
718c0984e5SSebastian Reichel }
728c0984e5SSebastian Reichel 
max77693_get_charge_type(struct regmap * regmap,int * val)738c0984e5SSebastian Reichel static int max77693_get_charge_type(struct regmap *regmap, int *val)
748c0984e5SSebastian Reichel {
758c0984e5SSebastian Reichel 	int ret;
768c0984e5SSebastian Reichel 	unsigned int data;
778c0984e5SSebastian Reichel 
788c0984e5SSebastian Reichel 	ret = regmap_read(regmap, MAX77693_CHG_REG_CHG_DETAILS_01, &data);
798c0984e5SSebastian Reichel 	if (ret < 0)
808c0984e5SSebastian Reichel 		return ret;
818c0984e5SSebastian Reichel 
828c0984e5SSebastian Reichel 	data &= CHG_DETAILS_01_CHG_MASK;
838c0984e5SSebastian Reichel 	data >>= CHG_DETAILS_01_CHG_SHIFT;
848c0984e5SSebastian Reichel 
858c0984e5SSebastian Reichel 	switch (data) {
868c0984e5SSebastian Reichel 	case MAX77693_CHARGING_PREQUALIFICATION:
878c0984e5SSebastian Reichel 	/*
888c0984e5SSebastian Reichel 	 * Top-off: trickle or fast? In top-off the current varies between
898c0984e5SSebastian Reichel 	 * 100 and 250 mA. It is higher than prequalification current.
908c0984e5SSebastian Reichel 	 */
918c0984e5SSebastian Reichel 	case MAX77693_CHARGING_TOP_OFF:
928c0984e5SSebastian Reichel 		*val = POWER_SUPPLY_CHARGE_TYPE_TRICKLE;
938c0984e5SSebastian Reichel 		break;
948c0984e5SSebastian Reichel 	case MAX77693_CHARGING_FAST_CONST_CURRENT:
958c0984e5SSebastian Reichel 	case MAX77693_CHARGING_FAST_CONST_VOLTAGE:
968c0984e5SSebastian Reichel 	/* In high temp the charging current is reduced, but still charging */
978c0984e5SSebastian Reichel 	case MAX77693_CHARGING_HIGH_TEMP:
988c0984e5SSebastian Reichel 		*val = POWER_SUPPLY_CHARGE_TYPE_FAST;
998c0984e5SSebastian Reichel 		break;
1008c0984e5SSebastian Reichel 	case MAX77693_CHARGING_DONE:
1018c0984e5SSebastian Reichel 	case MAX77693_CHARGING_TIMER_EXPIRED:
1028c0984e5SSebastian Reichel 	case MAX77693_CHARGING_THERMISTOR_SUSPEND:
1038c0984e5SSebastian Reichel 	case MAX77693_CHARGING_OFF:
1048c0984e5SSebastian Reichel 	case MAX77693_CHARGING_OVER_TEMP:
1058c0984e5SSebastian Reichel 	case MAX77693_CHARGING_WATCHDOG_EXPIRED:
1068c0984e5SSebastian Reichel 		*val = POWER_SUPPLY_CHARGE_TYPE_NONE;
1078c0984e5SSebastian Reichel 		break;
1088c0984e5SSebastian Reichel 	case MAX77693_CHARGING_RESERVED:
1098c0984e5SSebastian Reichel 	default:
1108c0984e5SSebastian Reichel 		*val = POWER_SUPPLY_CHARGE_TYPE_UNKNOWN;
1118c0984e5SSebastian Reichel 	}
1128c0984e5SSebastian Reichel 
1138c0984e5SSebastian Reichel 	return 0;
1148c0984e5SSebastian Reichel }
1158c0984e5SSebastian Reichel 
1168c0984e5SSebastian Reichel /*
1178c0984e5SSebastian Reichel  * Supported health statuses:
1188c0984e5SSebastian Reichel  *  - POWER_SUPPLY_HEALTH_DEAD
1198c0984e5SSebastian Reichel  *  - POWER_SUPPLY_HEALTH_GOOD
1208c0984e5SSebastian Reichel  *  - POWER_SUPPLY_HEALTH_OVERVOLTAGE
1218c0984e5SSebastian Reichel  *  - POWER_SUPPLY_HEALTH_SAFETY_TIMER_EXPIRE
1228c0984e5SSebastian Reichel  *  - POWER_SUPPLY_HEALTH_UNKNOWN
1238c0984e5SSebastian Reichel  *  - POWER_SUPPLY_HEALTH_UNSPEC_FAILURE
1248c0984e5SSebastian Reichel  */
max77693_get_battery_health(struct regmap * regmap,int * val)1258c0984e5SSebastian Reichel static int max77693_get_battery_health(struct regmap *regmap, int *val)
1268c0984e5SSebastian Reichel {
1278c0984e5SSebastian Reichel 	int ret;
1288c0984e5SSebastian Reichel 	unsigned int data;
1298c0984e5SSebastian Reichel 
1308c0984e5SSebastian Reichel 	ret = regmap_read(regmap, MAX77693_CHG_REG_CHG_DETAILS_01, &data);
1318c0984e5SSebastian Reichel 	if (ret < 0)
1328c0984e5SSebastian Reichel 		return ret;
1338c0984e5SSebastian Reichel 
1348c0984e5SSebastian Reichel 	data &= CHG_DETAILS_01_BAT_MASK;
1358c0984e5SSebastian Reichel 	data >>= CHG_DETAILS_01_BAT_SHIFT;
1368c0984e5SSebastian Reichel 
1378c0984e5SSebastian Reichel 	switch (data) {
1388c0984e5SSebastian Reichel 	case MAX77693_BATTERY_NOBAT:
1398c0984e5SSebastian Reichel 		*val = POWER_SUPPLY_HEALTH_DEAD;
1408c0984e5SSebastian Reichel 		break;
1418c0984e5SSebastian Reichel 	case MAX77693_BATTERY_PREQUALIFICATION:
1428c0984e5SSebastian Reichel 	case MAX77693_BATTERY_GOOD:
1438c0984e5SSebastian Reichel 	case MAX77693_BATTERY_LOWVOLTAGE:
1448c0984e5SSebastian Reichel 		*val = POWER_SUPPLY_HEALTH_GOOD;
1458c0984e5SSebastian Reichel 		break;
1468c0984e5SSebastian Reichel 	case MAX77693_BATTERY_TIMER_EXPIRED:
1478c0984e5SSebastian Reichel 		/*
1488c0984e5SSebastian Reichel 		 * Took longer to charge than expected, charging suspended.
1498c0984e5SSebastian Reichel 		 * Damaged battery?
1508c0984e5SSebastian Reichel 		 */
1518c0984e5SSebastian Reichel 		*val = POWER_SUPPLY_HEALTH_SAFETY_TIMER_EXPIRE;
1528c0984e5SSebastian Reichel 		break;
1538c0984e5SSebastian Reichel 	case MAX77693_BATTERY_OVERVOLTAGE:
1548c0984e5SSebastian Reichel 		*val = POWER_SUPPLY_HEALTH_OVERVOLTAGE;
1558c0984e5SSebastian Reichel 		break;
1568c0984e5SSebastian Reichel 	case MAX77693_BATTERY_OVERCURRENT:
1578c0984e5SSebastian Reichel 		*val = POWER_SUPPLY_HEALTH_UNSPEC_FAILURE;
1588c0984e5SSebastian Reichel 		break;
1598c0984e5SSebastian Reichel 	case MAX77693_BATTERY_RESERVED:
1608c0984e5SSebastian Reichel 	default:
1618c0984e5SSebastian Reichel 		*val = POWER_SUPPLY_HEALTH_UNKNOWN;
1628c0984e5SSebastian Reichel 		break;
1638c0984e5SSebastian Reichel 	}
1648c0984e5SSebastian Reichel 
1658c0984e5SSebastian Reichel 	return 0;
1668c0984e5SSebastian Reichel }
1678c0984e5SSebastian Reichel 
max77693_get_present(struct regmap * regmap,int * val)1688c0984e5SSebastian Reichel static int max77693_get_present(struct regmap *regmap, int *val)
1698c0984e5SSebastian Reichel {
1708c0984e5SSebastian Reichel 	unsigned int data;
1718c0984e5SSebastian Reichel 	int ret;
1728c0984e5SSebastian Reichel 
1738c0984e5SSebastian Reichel 	/*
1748c0984e5SSebastian Reichel 	 * Read CHG_INT_OK register. High DETBAT bit here should be
1758c0984e5SSebastian Reichel 	 * equal to value 0x0 in CHG_DETAILS_01/BAT field.
1768c0984e5SSebastian Reichel 	 */
1778c0984e5SSebastian Reichel 	ret = regmap_read(regmap, MAX77693_CHG_REG_CHG_INT_OK, &data);
1788c0984e5SSebastian Reichel 	if (ret < 0)
1798c0984e5SSebastian Reichel 		return ret;
1808c0984e5SSebastian Reichel 
1818c0984e5SSebastian Reichel 	*val = (data & CHG_INT_OK_DETBAT_MASK) ? 0 : 1;
1828c0984e5SSebastian Reichel 
1838c0984e5SSebastian Reichel 	return 0;
1848c0984e5SSebastian Reichel }
1858c0984e5SSebastian Reichel 
max77693_get_online(struct regmap * regmap,int * val)1868c0984e5SSebastian Reichel static int max77693_get_online(struct regmap *regmap, int *val)
1878c0984e5SSebastian Reichel {
1888c0984e5SSebastian Reichel 	unsigned int data;
1898c0984e5SSebastian Reichel 	int ret;
1908c0984e5SSebastian Reichel 
1918c0984e5SSebastian Reichel 	ret = regmap_read(regmap, MAX77693_CHG_REG_CHG_INT_OK, &data);
1928c0984e5SSebastian Reichel 	if (ret < 0)
1938c0984e5SSebastian Reichel 		return ret;
1948c0984e5SSebastian Reichel 
1958c0984e5SSebastian Reichel 	*val = (data & CHG_INT_OK_CHGIN_MASK) ? 1 : 0;
1968c0984e5SSebastian Reichel 
1978c0984e5SSebastian Reichel 	return 0;
1988c0984e5SSebastian Reichel }
1998c0984e5SSebastian Reichel 
2008c0984e5SSebastian Reichel static enum power_supply_property max77693_charger_props[] = {
2018c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_STATUS,
2028c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_CHARGE_TYPE,
2038c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_HEALTH,
2048c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_PRESENT,
2058c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_ONLINE,
2068c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_MODEL_NAME,
2078c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_MANUFACTURER,
2088c0984e5SSebastian Reichel };
2098c0984e5SSebastian Reichel 
max77693_charger_get_property(struct power_supply * psy,enum power_supply_property psp,union power_supply_propval * val)2108c0984e5SSebastian Reichel static int max77693_charger_get_property(struct power_supply *psy,
2118c0984e5SSebastian Reichel 			    enum power_supply_property psp,
2128c0984e5SSebastian Reichel 			    union power_supply_propval *val)
2138c0984e5SSebastian Reichel {
2148c0984e5SSebastian Reichel 	struct max77693_charger *chg = power_supply_get_drvdata(psy);
2158c0984e5SSebastian Reichel 	struct regmap *regmap = chg->max77693->regmap;
2168c0984e5SSebastian Reichel 	int ret = 0;
2178c0984e5SSebastian Reichel 
2188c0984e5SSebastian Reichel 	switch (psp) {
2198c0984e5SSebastian Reichel 	case POWER_SUPPLY_PROP_STATUS:
2208c0984e5SSebastian Reichel 		ret = max77693_get_charger_state(regmap, &val->intval);
2218c0984e5SSebastian Reichel 		break;
2228c0984e5SSebastian Reichel 	case POWER_SUPPLY_PROP_CHARGE_TYPE:
2238c0984e5SSebastian Reichel 		ret = max77693_get_charge_type(regmap, &val->intval);
2248c0984e5SSebastian Reichel 		break;
2258c0984e5SSebastian Reichel 	case POWER_SUPPLY_PROP_HEALTH:
2268c0984e5SSebastian Reichel 		ret = max77693_get_battery_health(regmap, &val->intval);
2278c0984e5SSebastian Reichel 		break;
2288c0984e5SSebastian Reichel 	case POWER_SUPPLY_PROP_PRESENT:
2298c0984e5SSebastian Reichel 		ret = max77693_get_present(regmap, &val->intval);
2308c0984e5SSebastian Reichel 		break;
2318c0984e5SSebastian Reichel 	case POWER_SUPPLY_PROP_ONLINE:
2328c0984e5SSebastian Reichel 		ret = max77693_get_online(regmap, &val->intval);
2338c0984e5SSebastian Reichel 		break;
2348c0984e5SSebastian Reichel 	case POWER_SUPPLY_PROP_MODEL_NAME:
2358c0984e5SSebastian Reichel 		val->strval = max77693_charger_model;
2368c0984e5SSebastian Reichel 		break;
2378c0984e5SSebastian Reichel 	case POWER_SUPPLY_PROP_MANUFACTURER:
2388c0984e5SSebastian Reichel 		val->strval = max77693_charger_manufacturer;
2398c0984e5SSebastian Reichel 		break;
2408c0984e5SSebastian Reichel 	default:
2418c0984e5SSebastian Reichel 		return -EINVAL;
2428c0984e5SSebastian Reichel 	}
2438c0984e5SSebastian Reichel 
2448c0984e5SSebastian Reichel 	return ret;
2458c0984e5SSebastian Reichel }
2468c0984e5SSebastian Reichel 
2478c0984e5SSebastian Reichel static const struct power_supply_desc max77693_charger_desc = {
2488c0984e5SSebastian Reichel 	.name		= MAX77693_CHARGER_NAME,
2498c0984e5SSebastian Reichel 	.type		= POWER_SUPPLY_TYPE_BATTERY,
2508c0984e5SSebastian Reichel 	.properties	= max77693_charger_props,
2518c0984e5SSebastian Reichel 	.num_properties	= ARRAY_SIZE(max77693_charger_props),
2528c0984e5SSebastian Reichel 	.get_property	= max77693_charger_get_property,
2538c0984e5SSebastian Reichel };
2548c0984e5SSebastian Reichel 
device_attr_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count,int (* fn)(struct max77693_charger *,unsigned long))2558c0984e5SSebastian Reichel static ssize_t device_attr_store(struct device *dev,
2568c0984e5SSebastian Reichel 		struct device_attribute *attr, const char *buf, size_t count,
2578c0984e5SSebastian Reichel 		int (*fn)(struct max77693_charger *, unsigned long))
2588c0984e5SSebastian Reichel {
2598c0984e5SSebastian Reichel 	struct max77693_charger *chg = dev_get_drvdata(dev);
2608c0984e5SSebastian Reichel 	unsigned long val;
2618c0984e5SSebastian Reichel 	int ret;
2628c0984e5SSebastian Reichel 
2638c0984e5SSebastian Reichel 	ret = kstrtoul(buf, 10, &val);
2648c0984e5SSebastian Reichel 	if (ret)
2658c0984e5SSebastian Reichel 		return ret;
2668c0984e5SSebastian Reichel 
2678c0984e5SSebastian Reichel 	ret = fn(chg, val);
2688c0984e5SSebastian Reichel 	if (ret)
2698c0984e5SSebastian Reichel 		return ret;
2708c0984e5SSebastian Reichel 
2718c0984e5SSebastian Reichel 	return count;
2728c0984e5SSebastian Reichel }
2738c0984e5SSebastian Reichel 
fast_charge_timer_show(struct device * dev,struct device_attribute * attr,char * buf)2748c0984e5SSebastian Reichel static ssize_t fast_charge_timer_show(struct device *dev,
2758c0984e5SSebastian Reichel 		struct device_attribute *attr, char *buf)
2768c0984e5SSebastian Reichel {
2778c0984e5SSebastian Reichel 	struct max77693_charger *chg = dev_get_drvdata(dev);
2788c0984e5SSebastian Reichel 	unsigned int data, val;
2798c0984e5SSebastian Reichel 	int ret;
2808c0984e5SSebastian Reichel 
2818c0984e5SSebastian Reichel 	ret = regmap_read(chg->max77693->regmap, MAX77693_CHG_REG_CHG_CNFG_01,
2828c0984e5SSebastian Reichel 			&data);
2838c0984e5SSebastian Reichel 	if (ret < 0)
2848c0984e5SSebastian Reichel 		return ret;
2858c0984e5SSebastian Reichel 
2868c0984e5SSebastian Reichel 	data &= CHG_CNFG_01_FCHGTIME_MASK;
2878c0984e5SSebastian Reichel 	data >>= CHG_CNFG_01_FCHGTIME_SHIFT;
2888c0984e5SSebastian Reichel 	switch (data) {
2898c0984e5SSebastian Reichel 	case 0x1 ... 0x7:
2908c0984e5SSebastian Reichel 		/* Starting from 4 hours, step by 2 hours */
2918c0984e5SSebastian Reichel 		val = 4 + (data - 1) * 2;
2928c0984e5SSebastian Reichel 		break;
2938c0984e5SSebastian Reichel 	case 0x0:
2948c0984e5SSebastian Reichel 	default:
2958c0984e5SSebastian Reichel 		val = 0;
2968c0984e5SSebastian Reichel 		break;
2978c0984e5SSebastian Reichel 	}
2988c0984e5SSebastian Reichel 
299*35aa0628Sye xingchen 	return sysfs_emit(buf, "%u\n", val);
3008c0984e5SSebastian Reichel }
3018c0984e5SSebastian Reichel 
max77693_set_fast_charge_timer(struct max77693_charger * chg,unsigned long hours)3028c0984e5SSebastian Reichel static int max77693_set_fast_charge_timer(struct max77693_charger *chg,
3038c0984e5SSebastian Reichel 		unsigned long hours)
3048c0984e5SSebastian Reichel {
3058c0984e5SSebastian Reichel 	unsigned int data;
3068c0984e5SSebastian Reichel 
3078c0984e5SSebastian Reichel 	/*
3088c0984e5SSebastian Reichel 	 * 0x00 - disable
3098c0984e5SSebastian Reichel 	 * 0x01 - 4h
3108c0984e5SSebastian Reichel 	 * 0x02 - 6h
3118c0984e5SSebastian Reichel 	 * ...
3128c0984e5SSebastian Reichel 	 * 0x07 - 16h
3138c0984e5SSebastian Reichel 	 * Round down odd values.
3148c0984e5SSebastian Reichel 	 */
3158c0984e5SSebastian Reichel 	switch (hours) {
3168c0984e5SSebastian Reichel 	case 4 ... 16:
3178c0984e5SSebastian Reichel 		data = (hours - 4) / 2 + 1;
3188c0984e5SSebastian Reichel 		break;
3198c0984e5SSebastian Reichel 	case 0:
3208c0984e5SSebastian Reichel 		/* Disable */
3218c0984e5SSebastian Reichel 		data = 0;
3228c0984e5SSebastian Reichel 		break;
3238c0984e5SSebastian Reichel 	default:
3248c0984e5SSebastian Reichel 		return -EINVAL;
3258c0984e5SSebastian Reichel 	}
3268c0984e5SSebastian Reichel 	data <<= CHG_CNFG_01_FCHGTIME_SHIFT;
3278c0984e5SSebastian Reichel 
3288c0984e5SSebastian Reichel 	return regmap_update_bits(chg->max77693->regmap,
3298c0984e5SSebastian Reichel 			MAX77693_CHG_REG_CHG_CNFG_01,
3308c0984e5SSebastian Reichel 			CHG_CNFG_01_FCHGTIME_MASK, data);
3318c0984e5SSebastian Reichel }
3328c0984e5SSebastian Reichel 
fast_charge_timer_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)3338c0984e5SSebastian Reichel static ssize_t fast_charge_timer_store(struct device *dev,
3348c0984e5SSebastian Reichel 		struct device_attribute *attr, const char *buf, size_t count)
3358c0984e5SSebastian Reichel {
3368c0984e5SSebastian Reichel 	return device_attr_store(dev, attr, buf, count,
3378c0984e5SSebastian Reichel 			max77693_set_fast_charge_timer);
3388c0984e5SSebastian Reichel }
3398c0984e5SSebastian Reichel 
top_off_threshold_current_show(struct device * dev,struct device_attribute * attr,char * buf)3408c0984e5SSebastian Reichel static ssize_t top_off_threshold_current_show(struct device *dev,
3418c0984e5SSebastian Reichel 		struct device_attribute *attr, char *buf)
3428c0984e5SSebastian Reichel {
3438c0984e5SSebastian Reichel 	struct max77693_charger *chg = dev_get_drvdata(dev);
3448c0984e5SSebastian Reichel 	unsigned int data, val;
3458c0984e5SSebastian Reichel 	int ret;
3468c0984e5SSebastian Reichel 
3478c0984e5SSebastian Reichel 	ret = regmap_read(chg->max77693->regmap, MAX77693_CHG_REG_CHG_CNFG_03,
3488c0984e5SSebastian Reichel 			&data);
3498c0984e5SSebastian Reichel 	if (ret < 0)
3508c0984e5SSebastian Reichel 		return ret;
3518c0984e5SSebastian Reichel 
3528c0984e5SSebastian Reichel 	data &= CHG_CNFG_03_TOITH_MASK;
3538c0984e5SSebastian Reichel 	data >>= CHG_CNFG_03_TOITH_SHIFT;
3548c0984e5SSebastian Reichel 
3558c0984e5SSebastian Reichel 	if (data <= 0x04)
3568c0984e5SSebastian Reichel 		val = 100000 + data * 25000;
3578c0984e5SSebastian Reichel 	else
3588c0984e5SSebastian Reichel 		val = data * 50000;
3598c0984e5SSebastian Reichel 
360*35aa0628Sye xingchen 	return sysfs_emit(buf, "%u\n", val);
3618c0984e5SSebastian Reichel }
3628c0984e5SSebastian Reichel 
max77693_set_top_off_threshold_current(struct max77693_charger * chg,unsigned long uamp)3638c0984e5SSebastian Reichel static int max77693_set_top_off_threshold_current(struct max77693_charger *chg,
3648c0984e5SSebastian Reichel 		unsigned long uamp)
3658c0984e5SSebastian Reichel {
3668c0984e5SSebastian Reichel 	unsigned int data;
3678c0984e5SSebastian Reichel 
3688c0984e5SSebastian Reichel 	if (uamp < 100000 || uamp > 350000)
3698c0984e5SSebastian Reichel 		return -EINVAL;
3708c0984e5SSebastian Reichel 
3718c0984e5SSebastian Reichel 	if (uamp <= 200000)
3728c0984e5SSebastian Reichel 		data = (uamp - 100000) / 25000;
3738c0984e5SSebastian Reichel 	else
3748c0984e5SSebastian Reichel 		/* (200000, 350000> */
3758c0984e5SSebastian Reichel 		data = uamp / 50000;
3768c0984e5SSebastian Reichel 
3778c0984e5SSebastian Reichel 	data <<= CHG_CNFG_03_TOITH_SHIFT;
3788c0984e5SSebastian Reichel 
3798c0984e5SSebastian Reichel 	return regmap_update_bits(chg->max77693->regmap,
3808c0984e5SSebastian Reichel 			MAX77693_CHG_REG_CHG_CNFG_03,
3818c0984e5SSebastian Reichel 			CHG_CNFG_03_TOITH_MASK, data);
3828c0984e5SSebastian Reichel }
3838c0984e5SSebastian Reichel 
top_off_threshold_current_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)3848c0984e5SSebastian Reichel static ssize_t top_off_threshold_current_store(struct device *dev,
3858c0984e5SSebastian Reichel 		struct device_attribute *attr, const char *buf, size_t count)
3868c0984e5SSebastian Reichel {
3878c0984e5SSebastian Reichel 	return device_attr_store(dev, attr, buf, count,
3888c0984e5SSebastian Reichel 			max77693_set_top_off_threshold_current);
3898c0984e5SSebastian Reichel }
3908c0984e5SSebastian Reichel 
top_off_timer_show(struct device * dev,struct device_attribute * attr,char * buf)3918c0984e5SSebastian Reichel static ssize_t top_off_timer_show(struct device *dev,
3928c0984e5SSebastian Reichel 		struct device_attribute *attr, char *buf)
3938c0984e5SSebastian Reichel {
3948c0984e5SSebastian Reichel 	struct max77693_charger *chg = dev_get_drvdata(dev);
3958c0984e5SSebastian Reichel 	unsigned int data, val;
3968c0984e5SSebastian Reichel 	int ret;
3978c0984e5SSebastian Reichel 
3988c0984e5SSebastian Reichel 	ret = regmap_read(chg->max77693->regmap, MAX77693_CHG_REG_CHG_CNFG_03,
3998c0984e5SSebastian Reichel 			&data);
4008c0984e5SSebastian Reichel 	if (ret < 0)
4018c0984e5SSebastian Reichel 		return ret;
4028c0984e5SSebastian Reichel 
4038c0984e5SSebastian Reichel 	data &= CHG_CNFG_03_TOTIME_MASK;
4048c0984e5SSebastian Reichel 	data >>= CHG_CNFG_03_TOTIME_SHIFT;
4058c0984e5SSebastian Reichel 
4068c0984e5SSebastian Reichel 	val = data * 10;
4078c0984e5SSebastian Reichel 
408*35aa0628Sye xingchen 	return sysfs_emit(buf, "%u\n", val);
4098c0984e5SSebastian Reichel }
4108c0984e5SSebastian Reichel 
max77693_set_top_off_timer(struct max77693_charger * chg,unsigned long minutes)4118c0984e5SSebastian Reichel static int max77693_set_top_off_timer(struct max77693_charger *chg,
4128c0984e5SSebastian Reichel 		unsigned long minutes)
4138c0984e5SSebastian Reichel {
4148c0984e5SSebastian Reichel 	unsigned int data;
4158c0984e5SSebastian Reichel 
4168c0984e5SSebastian Reichel 	if (minutes > 70)
4178c0984e5SSebastian Reichel 		return -EINVAL;
4188c0984e5SSebastian Reichel 
4198c0984e5SSebastian Reichel 	data = minutes / 10;
4208c0984e5SSebastian Reichel 	data <<= CHG_CNFG_03_TOTIME_SHIFT;
4218c0984e5SSebastian Reichel 
4228c0984e5SSebastian Reichel 	return regmap_update_bits(chg->max77693->regmap,
4238c0984e5SSebastian Reichel 			MAX77693_CHG_REG_CHG_CNFG_03,
4248c0984e5SSebastian Reichel 			CHG_CNFG_03_TOTIME_MASK, data);
4258c0984e5SSebastian Reichel }
4268c0984e5SSebastian Reichel 
top_off_timer_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)4278c0984e5SSebastian Reichel static ssize_t top_off_timer_store(struct device *dev,
4288c0984e5SSebastian Reichel 		struct device_attribute *attr, const char *buf, size_t count)
4298c0984e5SSebastian Reichel {
4308c0984e5SSebastian Reichel 	return device_attr_store(dev, attr, buf, count,
4318c0984e5SSebastian Reichel 			max77693_set_top_off_timer);
4328c0984e5SSebastian Reichel }
4338c0984e5SSebastian Reichel 
4348c0984e5SSebastian Reichel static DEVICE_ATTR_RW(fast_charge_timer);
4358c0984e5SSebastian Reichel static DEVICE_ATTR_RW(top_off_threshold_current);
4368c0984e5SSebastian Reichel static DEVICE_ATTR_RW(top_off_timer);
4378c0984e5SSebastian Reichel 
max77693_set_constant_volt(struct max77693_charger * chg,unsigned int uvolt)4388c0984e5SSebastian Reichel static int max77693_set_constant_volt(struct max77693_charger *chg,
4398c0984e5SSebastian Reichel 		unsigned int uvolt)
4408c0984e5SSebastian Reichel {
4418c0984e5SSebastian Reichel 	unsigned int data;
4428c0984e5SSebastian Reichel 
4438c0984e5SSebastian Reichel 	/*
4448c0984e5SSebastian Reichel 	 * 0x00 - 3.650 V
4458c0984e5SSebastian Reichel 	 * 0x01 - 3.675 V
4468c0984e5SSebastian Reichel 	 * ...
4478c0984e5SSebastian Reichel 	 * 0x1b - 4.325 V
4488c0984e5SSebastian Reichel 	 * 0x1c - 4.340 V
4498c0984e5SSebastian Reichel 	 * 0x1d - 4.350 V
4508c0984e5SSebastian Reichel 	 * 0x1e - 4.375 V
4518c0984e5SSebastian Reichel 	 * 0x1f - 4.400 V
4528c0984e5SSebastian Reichel 	 */
4538c0984e5SSebastian Reichel 	if (uvolt >= 3650000 && uvolt < 4340000)
4548c0984e5SSebastian Reichel 		data = (uvolt - 3650000) / 25000;
4558c0984e5SSebastian Reichel 	else if (uvolt >= 4340000 && uvolt < 4350000)
4568c0984e5SSebastian Reichel 		data = 0x1c;
4578c0984e5SSebastian Reichel 	else if (uvolt >= 4350000 && uvolt <= 4400000)
4588c0984e5SSebastian Reichel 		data = 0x1d + (uvolt - 4350000) / 25000;
4598c0984e5SSebastian Reichel 	else {
4608c0984e5SSebastian Reichel 		dev_err(chg->dev, "Wrong value for charging constant voltage\n");
4618c0984e5SSebastian Reichel 		return -EINVAL;
4628c0984e5SSebastian Reichel 	}
4638c0984e5SSebastian Reichel 
4648c0984e5SSebastian Reichel 	data <<= CHG_CNFG_04_CHGCVPRM_SHIFT;
4658c0984e5SSebastian Reichel 
4668c0984e5SSebastian Reichel 	dev_dbg(chg->dev, "Charging constant voltage: %u (0x%x)\n", uvolt,
4678c0984e5SSebastian Reichel 			data);
4688c0984e5SSebastian Reichel 
4698c0984e5SSebastian Reichel 	return regmap_update_bits(chg->max77693->regmap,
4708c0984e5SSebastian Reichel 			MAX77693_CHG_REG_CHG_CNFG_04,
4718c0984e5SSebastian Reichel 			CHG_CNFG_04_CHGCVPRM_MASK, data);
4728c0984e5SSebastian Reichel }
4738c0984e5SSebastian Reichel 
max77693_set_min_system_volt(struct max77693_charger * chg,unsigned int uvolt)4748c0984e5SSebastian Reichel static int max77693_set_min_system_volt(struct max77693_charger *chg,
4758c0984e5SSebastian Reichel 		unsigned int uvolt)
4768c0984e5SSebastian Reichel {
4778c0984e5SSebastian Reichel 	unsigned int data;
4788c0984e5SSebastian Reichel 
4798c0984e5SSebastian Reichel 	if (uvolt < 3000000 || uvolt > 3700000) {
4808c0984e5SSebastian Reichel 		dev_err(chg->dev, "Wrong value for minimum system regulation voltage\n");
4818c0984e5SSebastian Reichel 		return -EINVAL;
4828c0984e5SSebastian Reichel 	}
4838c0984e5SSebastian Reichel 
4848c0984e5SSebastian Reichel 	data = (uvolt - 3000000) / 100000;
4858c0984e5SSebastian Reichel 
4868c0984e5SSebastian Reichel 	data <<= CHG_CNFG_04_MINVSYS_SHIFT;
4878c0984e5SSebastian Reichel 
4888c0984e5SSebastian Reichel 	dev_dbg(chg->dev, "Minimum system regulation voltage: %u (0x%x)\n",
4898c0984e5SSebastian Reichel 			uvolt, data);
4908c0984e5SSebastian Reichel 
4918c0984e5SSebastian Reichel 	return regmap_update_bits(chg->max77693->regmap,
4928c0984e5SSebastian Reichel 			MAX77693_CHG_REG_CHG_CNFG_04,
4938c0984e5SSebastian Reichel 			CHG_CNFG_04_MINVSYS_MASK, data);
4948c0984e5SSebastian Reichel }
4958c0984e5SSebastian Reichel 
max77693_set_thermal_regulation_temp(struct max77693_charger * chg,unsigned int cels)4968c0984e5SSebastian Reichel static int max77693_set_thermal_regulation_temp(struct max77693_charger *chg,
4978c0984e5SSebastian Reichel 		unsigned int cels)
4988c0984e5SSebastian Reichel {
4998c0984e5SSebastian Reichel 	unsigned int data;
5008c0984e5SSebastian Reichel 
5018c0984e5SSebastian Reichel 	switch (cels) {
5028c0984e5SSebastian Reichel 	case 70:
5038c0984e5SSebastian Reichel 	case 85:
5048c0984e5SSebastian Reichel 	case 100:
5058c0984e5SSebastian Reichel 	case 115:
5068c0984e5SSebastian Reichel 		data = (cels - 70) / 15;
5078c0984e5SSebastian Reichel 		break;
5088c0984e5SSebastian Reichel 	default:
5098c0984e5SSebastian Reichel 		dev_err(chg->dev, "Wrong value for thermal regulation loop temperature\n");
5108c0984e5SSebastian Reichel 		return -EINVAL;
5118c0984e5SSebastian Reichel 	}
5128c0984e5SSebastian Reichel 
5138c0984e5SSebastian Reichel 	data <<= CHG_CNFG_07_REGTEMP_SHIFT;
5148c0984e5SSebastian Reichel 
5158c0984e5SSebastian Reichel 	dev_dbg(chg->dev, "Thermal regulation loop temperature: %u (0x%x)\n",
5168c0984e5SSebastian Reichel 			cels, data);
5178c0984e5SSebastian Reichel 
5188c0984e5SSebastian Reichel 	return regmap_update_bits(chg->max77693->regmap,
5198c0984e5SSebastian Reichel 			MAX77693_CHG_REG_CHG_CNFG_07,
5208c0984e5SSebastian Reichel 			CHG_CNFG_07_REGTEMP_MASK, data);
5218c0984e5SSebastian Reichel }
5228c0984e5SSebastian Reichel 
max77693_set_batttery_overcurrent(struct max77693_charger * chg,unsigned int uamp)5238c0984e5SSebastian Reichel static int max77693_set_batttery_overcurrent(struct max77693_charger *chg,
5248c0984e5SSebastian Reichel 		unsigned int uamp)
5258c0984e5SSebastian Reichel {
5268c0984e5SSebastian Reichel 	unsigned int data;
5278c0984e5SSebastian Reichel 
5288c0984e5SSebastian Reichel 	if (uamp && (uamp < 2000000 || uamp > 3500000)) {
5298c0984e5SSebastian Reichel 		dev_err(chg->dev, "Wrong value for battery overcurrent\n");
5308c0984e5SSebastian Reichel 		return -EINVAL;
5318c0984e5SSebastian Reichel 	}
5328c0984e5SSebastian Reichel 
5338c0984e5SSebastian Reichel 	if (uamp)
5348c0984e5SSebastian Reichel 		data = ((uamp - 2000000) / 250000) + 1;
5358c0984e5SSebastian Reichel 	else
5368c0984e5SSebastian Reichel 		data = 0; /* disable */
5378c0984e5SSebastian Reichel 
5388c0984e5SSebastian Reichel 	data <<= CHG_CNFG_12_B2SOVRC_SHIFT;
5398c0984e5SSebastian Reichel 
5408c0984e5SSebastian Reichel 	dev_dbg(chg->dev, "Battery overcurrent: %u (0x%x)\n", uamp, data);
5418c0984e5SSebastian Reichel 
5428c0984e5SSebastian Reichel 	return regmap_update_bits(chg->max77693->regmap,
5438c0984e5SSebastian Reichel 			MAX77693_CHG_REG_CHG_CNFG_12,
5448c0984e5SSebastian Reichel 			CHG_CNFG_12_B2SOVRC_MASK, data);
5458c0984e5SSebastian Reichel }
5468c0984e5SSebastian Reichel 
max77693_set_charge_input_threshold_volt(struct max77693_charger * chg,unsigned int uvolt)5478c0984e5SSebastian Reichel static int max77693_set_charge_input_threshold_volt(struct max77693_charger *chg,
5488c0984e5SSebastian Reichel 		unsigned int uvolt)
5498c0984e5SSebastian Reichel {
5508c0984e5SSebastian Reichel 	unsigned int data;
5518c0984e5SSebastian Reichel 
5528c0984e5SSebastian Reichel 	switch (uvolt) {
5538c0984e5SSebastian Reichel 	case 4300000:
5548c0984e5SSebastian Reichel 		data = 0x0;
5558c0984e5SSebastian Reichel 		break;
5568c0984e5SSebastian Reichel 	case 4700000:
5578c0984e5SSebastian Reichel 	case 4800000:
5588c0984e5SSebastian Reichel 	case 4900000:
5598c0984e5SSebastian Reichel 		data = (uvolt - 4700000) / 100000;
560cc44ba91SGustavo A. R. Silva 		break;
5618c0984e5SSebastian Reichel 	default:
5628c0984e5SSebastian Reichel 		dev_err(chg->dev, "Wrong value for charge input voltage regulation threshold\n");
5638c0984e5SSebastian Reichel 		return -EINVAL;
5648c0984e5SSebastian Reichel 	}
5658c0984e5SSebastian Reichel 
5668c0984e5SSebastian Reichel 	data <<= CHG_CNFG_12_VCHGINREG_SHIFT;
5678c0984e5SSebastian Reichel 
5688c0984e5SSebastian Reichel 	dev_dbg(chg->dev, "Charge input voltage regulation threshold: %u (0x%x)\n",
5698c0984e5SSebastian Reichel 			uvolt, data);
5708c0984e5SSebastian Reichel 
5718c0984e5SSebastian Reichel 	return regmap_update_bits(chg->max77693->regmap,
5728c0984e5SSebastian Reichel 			MAX77693_CHG_REG_CHG_CNFG_12,
5738c0984e5SSebastian Reichel 			CHG_CNFG_12_VCHGINREG_MASK, data);
5748c0984e5SSebastian Reichel }
5758c0984e5SSebastian Reichel 
5768c0984e5SSebastian Reichel /*
5778c0984e5SSebastian Reichel  * Sets charger registers to proper and safe default values.
5788c0984e5SSebastian Reichel  */
max77693_reg_init(struct max77693_charger * chg)5798c0984e5SSebastian Reichel static int max77693_reg_init(struct max77693_charger *chg)
5808c0984e5SSebastian Reichel {
5818c0984e5SSebastian Reichel 	int ret;
5828c0984e5SSebastian Reichel 	unsigned int data;
5838c0984e5SSebastian Reichel 
5848c0984e5SSebastian Reichel 	/* Unlock charger register protection */
5858c0984e5SSebastian Reichel 	data = (0x3 << CHG_CNFG_06_CHGPROT_SHIFT);
5868c0984e5SSebastian Reichel 	ret = regmap_update_bits(chg->max77693->regmap,
5878c0984e5SSebastian Reichel 				MAX77693_CHG_REG_CHG_CNFG_06,
5888c0984e5SSebastian Reichel 				CHG_CNFG_06_CHGPROT_MASK, data);
5898c0984e5SSebastian Reichel 	if (ret) {
5908c0984e5SSebastian Reichel 		dev_err(chg->dev, "Error unlocking registers: %d\n", ret);
5918c0984e5SSebastian Reichel 		return ret;
5928c0984e5SSebastian Reichel 	}
5938c0984e5SSebastian Reichel 
5948c0984e5SSebastian Reichel 	ret = max77693_set_fast_charge_timer(chg, DEFAULT_FAST_CHARGE_TIMER);
5958c0984e5SSebastian Reichel 	if (ret)
5968c0984e5SSebastian Reichel 		return ret;
5978c0984e5SSebastian Reichel 
5988c0984e5SSebastian Reichel 	ret = max77693_set_top_off_threshold_current(chg,
5998c0984e5SSebastian Reichel 			DEFAULT_TOP_OFF_THRESHOLD_CURRENT);
6008c0984e5SSebastian Reichel 	if (ret)
6018c0984e5SSebastian Reichel 		return ret;
6028c0984e5SSebastian Reichel 
6038c0984e5SSebastian Reichel 	ret = max77693_set_top_off_timer(chg, DEFAULT_TOP_OFF_TIMER);
6048c0984e5SSebastian Reichel 	if (ret)
6058c0984e5SSebastian Reichel 		return ret;
6068c0984e5SSebastian Reichel 
6078c0984e5SSebastian Reichel 	ret = max77693_set_constant_volt(chg, chg->constant_volt);
6088c0984e5SSebastian Reichel 	if (ret)
6098c0984e5SSebastian Reichel 		return ret;
6108c0984e5SSebastian Reichel 
6118c0984e5SSebastian Reichel 	ret = max77693_set_min_system_volt(chg, chg->min_system_volt);
6128c0984e5SSebastian Reichel 	if (ret)
6138c0984e5SSebastian Reichel 		return ret;
6148c0984e5SSebastian Reichel 
6158c0984e5SSebastian Reichel 	ret = max77693_set_thermal_regulation_temp(chg,
6168c0984e5SSebastian Reichel 			chg->thermal_regulation_temp);
6178c0984e5SSebastian Reichel 	if (ret)
6188c0984e5SSebastian Reichel 		return ret;
6198c0984e5SSebastian Reichel 
6208c0984e5SSebastian Reichel 	ret = max77693_set_batttery_overcurrent(chg, chg->batttery_overcurrent);
6218c0984e5SSebastian Reichel 	if (ret)
6228c0984e5SSebastian Reichel 		return ret;
6238c0984e5SSebastian Reichel 
6248c0984e5SSebastian Reichel 	return max77693_set_charge_input_threshold_volt(chg,
6258c0984e5SSebastian Reichel 			chg->charge_input_threshold_volt);
6268c0984e5SSebastian Reichel }
6278c0984e5SSebastian Reichel 
6288c0984e5SSebastian Reichel #ifdef CONFIG_OF
max77693_dt_init(struct device * dev,struct max77693_charger * chg)6298c0984e5SSebastian Reichel static int max77693_dt_init(struct device *dev, struct max77693_charger *chg)
6308c0984e5SSebastian Reichel {
6318c0984e5SSebastian Reichel 	struct device_node *np = dev->of_node;
6328c0984e5SSebastian Reichel 
6338c0984e5SSebastian Reichel 	if (!np) {
6348c0984e5SSebastian Reichel 		dev_err(dev, "no charger OF node\n");
6358c0984e5SSebastian Reichel 		return -EINVAL;
6368c0984e5SSebastian Reichel 	}
6378c0984e5SSebastian Reichel 
6388c0984e5SSebastian Reichel 	if (of_property_read_u32(np, "maxim,constant-microvolt",
6398c0984e5SSebastian Reichel 			&chg->constant_volt))
6408c0984e5SSebastian Reichel 		chg->constant_volt = DEFAULT_CONSTANT_VOLT;
6418c0984e5SSebastian Reichel 
6428c0984e5SSebastian Reichel 	if (of_property_read_u32(np, "maxim,min-system-microvolt",
6438c0984e5SSebastian Reichel 			&chg->min_system_volt))
6448c0984e5SSebastian Reichel 		chg->min_system_volt = DEFAULT_MIN_SYSTEM_VOLT;
6458c0984e5SSebastian Reichel 
6468c0984e5SSebastian Reichel 	if (of_property_read_u32(np, "maxim,thermal-regulation-celsius",
6478c0984e5SSebastian Reichel 			&chg->thermal_regulation_temp))
6488c0984e5SSebastian Reichel 		chg->thermal_regulation_temp = DEFAULT_THERMAL_REGULATION_TEMP;
6498c0984e5SSebastian Reichel 
6508c0984e5SSebastian Reichel 	if (of_property_read_u32(np, "maxim,battery-overcurrent-microamp",
6518c0984e5SSebastian Reichel 			&chg->batttery_overcurrent))
6528c0984e5SSebastian Reichel 		chg->batttery_overcurrent = DEFAULT_BATTERY_OVERCURRENT;
6538c0984e5SSebastian Reichel 
6548c0984e5SSebastian Reichel 	if (of_property_read_u32(np, "maxim,charge-input-threshold-microvolt",
6558c0984e5SSebastian Reichel 			&chg->charge_input_threshold_volt))
6568c0984e5SSebastian Reichel 		chg->charge_input_threshold_volt =
6578c0984e5SSebastian Reichel 			DEFAULT_CHARGER_INPUT_THRESHOLD_VOLT;
6588c0984e5SSebastian Reichel 
6598c0984e5SSebastian Reichel 	return 0;
6608c0984e5SSebastian Reichel }
6618c0984e5SSebastian Reichel #else /* CONFIG_OF */
max77693_dt_init(struct device * dev,struct max77693_charger * chg)6628c0984e5SSebastian Reichel static int max77693_dt_init(struct device *dev, struct max77693_charger *chg)
6638c0984e5SSebastian Reichel {
6648c0984e5SSebastian Reichel 	return 0;
6658c0984e5SSebastian Reichel }
6668c0984e5SSebastian Reichel #endif /* CONFIG_OF */
6678c0984e5SSebastian Reichel 
max77693_charger_probe(struct platform_device * pdev)6688c0984e5SSebastian Reichel static int max77693_charger_probe(struct platform_device *pdev)
6698c0984e5SSebastian Reichel {
6708c0984e5SSebastian Reichel 	struct max77693_charger *chg;
6718c0984e5SSebastian Reichel 	struct power_supply_config psy_cfg = {};
6728c0984e5SSebastian Reichel 	struct max77693_dev *max77693 = dev_get_drvdata(pdev->dev.parent);
6738c0984e5SSebastian Reichel 	int ret;
6748c0984e5SSebastian Reichel 
6758c0984e5SSebastian Reichel 	chg = devm_kzalloc(&pdev->dev, sizeof(*chg), GFP_KERNEL);
6768c0984e5SSebastian Reichel 	if (!chg)
6778c0984e5SSebastian Reichel 		return -ENOMEM;
6788c0984e5SSebastian Reichel 
6798c0984e5SSebastian Reichel 	platform_set_drvdata(pdev, chg);
6808c0984e5SSebastian Reichel 	chg->dev = &pdev->dev;
6818c0984e5SSebastian Reichel 	chg->max77693 = max77693;
6828c0984e5SSebastian Reichel 
6838c0984e5SSebastian Reichel 	ret = max77693_dt_init(&pdev->dev, chg);
6848c0984e5SSebastian Reichel 	if (ret)
6858c0984e5SSebastian Reichel 		return ret;
6868c0984e5SSebastian Reichel 
6878c0984e5SSebastian Reichel 	ret = max77693_reg_init(chg);
6888c0984e5SSebastian Reichel 	if (ret)
6898c0984e5SSebastian Reichel 		return ret;
6908c0984e5SSebastian Reichel 
6918c0984e5SSebastian Reichel 	psy_cfg.drv_data = chg;
6928c0984e5SSebastian Reichel 
6938c0984e5SSebastian Reichel 	ret = device_create_file(&pdev->dev, &dev_attr_fast_charge_timer);
6948c0984e5SSebastian Reichel 	if (ret) {
6958c0984e5SSebastian Reichel 		dev_err(&pdev->dev, "failed: create fast charge timer sysfs entry\n");
6968c0984e5SSebastian Reichel 		goto err;
6978c0984e5SSebastian Reichel 	}
6988c0984e5SSebastian Reichel 
6998c0984e5SSebastian Reichel 	ret = device_create_file(&pdev->dev,
7008c0984e5SSebastian Reichel 			&dev_attr_top_off_threshold_current);
7018c0984e5SSebastian Reichel 	if (ret) {
7028c0984e5SSebastian Reichel 		dev_err(&pdev->dev, "failed: create top off current sysfs entry\n");
7038c0984e5SSebastian Reichel 		goto err;
7048c0984e5SSebastian Reichel 	}
7058c0984e5SSebastian Reichel 
7068c0984e5SSebastian Reichel 	ret = device_create_file(&pdev->dev, &dev_attr_top_off_timer);
7078c0984e5SSebastian Reichel 	if (ret) {
7088c0984e5SSebastian Reichel 		dev_err(&pdev->dev, "failed: create top off timer sysfs entry\n");
7098c0984e5SSebastian Reichel 		goto err;
7108c0984e5SSebastian Reichel 	}
7118c0984e5SSebastian Reichel 
7128c0984e5SSebastian Reichel 	chg->charger = power_supply_register(&pdev->dev,
7138c0984e5SSebastian Reichel 						&max77693_charger_desc,
7148c0984e5SSebastian Reichel 						&psy_cfg);
7158c0984e5SSebastian Reichel 	if (IS_ERR(chg->charger)) {
7168c0984e5SSebastian Reichel 		dev_err(&pdev->dev, "failed: power supply register\n");
7178c0984e5SSebastian Reichel 		ret = PTR_ERR(chg->charger);
7188c0984e5SSebastian Reichel 		goto err;
7198c0984e5SSebastian Reichel 	}
7208c0984e5SSebastian Reichel 
7218c0984e5SSebastian Reichel 	return 0;
7228c0984e5SSebastian Reichel 
7238c0984e5SSebastian Reichel err:
7248c0984e5SSebastian Reichel 	device_remove_file(&pdev->dev, &dev_attr_top_off_timer);
7258c0984e5SSebastian Reichel 	device_remove_file(&pdev->dev, &dev_attr_top_off_threshold_current);
7268c0984e5SSebastian Reichel 	device_remove_file(&pdev->dev, &dev_attr_fast_charge_timer);
7278c0984e5SSebastian Reichel 
7288c0984e5SSebastian Reichel 	return ret;
7298c0984e5SSebastian Reichel }
7308c0984e5SSebastian Reichel 
max77693_charger_remove(struct platform_device * pdev)7318c0984e5SSebastian Reichel static int max77693_charger_remove(struct platform_device *pdev)
7328c0984e5SSebastian Reichel {
7338c0984e5SSebastian Reichel 	struct max77693_charger *chg = platform_get_drvdata(pdev);
7348c0984e5SSebastian Reichel 
7358c0984e5SSebastian Reichel 	device_remove_file(&pdev->dev, &dev_attr_top_off_timer);
7368c0984e5SSebastian Reichel 	device_remove_file(&pdev->dev, &dev_attr_top_off_threshold_current);
7378c0984e5SSebastian Reichel 	device_remove_file(&pdev->dev, &dev_attr_fast_charge_timer);
7388c0984e5SSebastian Reichel 
7398c0984e5SSebastian Reichel 	power_supply_unregister(chg->charger);
7408c0984e5SSebastian Reichel 
7418c0984e5SSebastian Reichel 	return 0;
7428c0984e5SSebastian Reichel }
7438c0984e5SSebastian Reichel 
7448c0984e5SSebastian Reichel static const struct platform_device_id max77693_charger_id[] = {
7458c0984e5SSebastian Reichel 	{ "max77693-charger", 0, },
7468c0984e5SSebastian Reichel 	{ }
7478c0984e5SSebastian Reichel };
7488c0984e5SSebastian Reichel MODULE_DEVICE_TABLE(platform, max77693_charger_id);
7498c0984e5SSebastian Reichel 
7508c0984e5SSebastian Reichel static struct platform_driver max77693_charger_driver = {
7518c0984e5SSebastian Reichel 	.driver = {
7528c0984e5SSebastian Reichel 		.name	= "max77693-charger",
7538c0984e5SSebastian Reichel 	},
7548c0984e5SSebastian Reichel 	.probe		= max77693_charger_probe,
7558c0984e5SSebastian Reichel 	.remove		= max77693_charger_remove,
7568c0984e5SSebastian Reichel 	.id_table	= max77693_charger_id,
7578c0984e5SSebastian Reichel };
7588c0984e5SSebastian Reichel module_platform_driver(max77693_charger_driver);
7598c0984e5SSebastian Reichel 
7601c4593edSKrzysztof Kozlowski MODULE_AUTHOR("Krzysztof Kozlowski <krzk@kernel.org>");
7618c0984e5SSebastian Reichel MODULE_DESCRIPTION("Maxim 77693 charger driver");
7628c0984e5SSebastian Reichel MODULE_LICENSE("GPL");
763