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