xref: /openbmc/linux/drivers/power/supply/olpc_battery.c (revision 9a87ffc99ec8eb8d35eed7c4f816d75f5cc9662e)
1d2912cb1SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
28c0984e5SSebastian Reichel /*
38c0984e5SSebastian Reichel  * Battery driver for One Laptop Per Child board.
48c0984e5SSebastian Reichel  *
58c0984e5SSebastian Reichel  *	Copyright © 2006-2010  David Woodhouse <dwmw2@infradead.org>
68c0984e5SSebastian Reichel  */
78c0984e5SSebastian Reichel 
88c0984e5SSebastian Reichel #include <linux/kernel.h>
98c0984e5SSebastian Reichel #include <linux/module.h>
10ac316725SRandy Dunlap #include <linux/mod_devicetable.h>
118c0984e5SSebastian Reichel #include <linux/types.h>
128c0984e5SSebastian Reichel #include <linux/err.h>
138c0984e5SSebastian Reichel #include <linux/device.h>
14f7a228eaSLubomir Rintel #include <linux/of.h>
158c0984e5SSebastian Reichel #include <linux/platform_device.h>
168c0984e5SSebastian Reichel #include <linux/power_supply.h>
178c0984e5SSebastian Reichel #include <linux/jiffies.h>
188c0984e5SSebastian Reichel #include <linux/sched.h>
198c0984e5SSebastian Reichel #include <linux/olpc-ec.h>
208c0984e5SSebastian Reichel 
218c0984e5SSebastian Reichel 
228c0984e5SSebastian Reichel #define EC_BAT_VOLTAGE	0x10	/* uint16_t,	*9.76/32,    mV   */
238c0984e5SSebastian Reichel #define EC_BAT_CURRENT	0x11	/* int16_t,	*15.625/120, mA   */
248c0984e5SSebastian Reichel #define EC_BAT_ACR	0x12	/* int16_t,	*6250/15,    µAh  */
258c0984e5SSebastian Reichel #define EC_BAT_TEMP	0x13	/* uint16_t,	*100/256,   °C  */
268c0984e5SSebastian Reichel #define EC_AMB_TEMP	0x14	/* uint16_t,	*100/256,   °C  */
278c0984e5SSebastian Reichel #define EC_BAT_STATUS	0x15	/* uint8_t,	bitmask */
288c0984e5SSebastian Reichel #define EC_BAT_SOC	0x16	/* uint8_t,	percentage */
298c0984e5SSebastian Reichel #define EC_BAT_SERIAL	0x17	/* uint8_t[6] */
308c0984e5SSebastian Reichel #define EC_BAT_EEPROM	0x18	/* uint8_t adr as input, uint8_t output */
318c0984e5SSebastian Reichel #define EC_BAT_ERRCODE	0x1f	/* uint8_t,	bitmask */
328c0984e5SSebastian Reichel 
338c0984e5SSebastian Reichel #define BAT_STAT_PRESENT	0x01
348c0984e5SSebastian Reichel #define BAT_STAT_FULL		0x02
358c0984e5SSebastian Reichel #define BAT_STAT_LOW		0x04
368c0984e5SSebastian Reichel #define BAT_STAT_DESTROY	0x08
378c0984e5SSebastian Reichel #define BAT_STAT_AC		0x10
388c0984e5SSebastian Reichel #define BAT_STAT_CHARGING	0x20
398c0984e5SSebastian Reichel #define BAT_STAT_DISCHARGING	0x40
408c0984e5SSebastian Reichel #define BAT_STAT_TRICKLE	0x80
418c0984e5SSebastian Reichel 
428c0984e5SSebastian Reichel #define BAT_ERR_INFOFAIL	0x02
438c0984e5SSebastian Reichel #define BAT_ERR_OVERVOLTAGE	0x04
448c0984e5SSebastian Reichel #define BAT_ERR_OVERTEMP	0x05
458c0984e5SSebastian Reichel #define BAT_ERR_GAUGESTOP	0x06
468c0984e5SSebastian Reichel #define BAT_ERR_OUT_OF_CONTROL	0x07
478c0984e5SSebastian Reichel #define BAT_ERR_ID_FAIL		0x09
488c0984e5SSebastian Reichel #define BAT_ERR_ACR_FAIL	0x10
498c0984e5SSebastian Reichel 
508c0984e5SSebastian Reichel #define BAT_ADDR_MFR_TYPE	0x5F
518c0984e5SSebastian Reichel 
5233554d81SLubomir Rintel struct olpc_battery_data {
5333554d81SLubomir Rintel 	struct power_supply *olpc_ac;
5433554d81SLubomir Rintel 	struct power_supply *olpc_bat;
5533554d81SLubomir Rintel 	char bat_serial[17];
568ecefda2SLubomir Rintel 	bool new_proto;
5776311b9aSLubomir Rintel 	bool little_endian;
5833554d81SLubomir Rintel };
5933554d81SLubomir Rintel 
608c0984e5SSebastian Reichel /*********************************************************************
618c0984e5SSebastian Reichel  *		Power
628c0984e5SSebastian Reichel  *********************************************************************/
638c0984e5SSebastian Reichel 
olpc_ac_get_prop(struct power_supply * psy,enum power_supply_property psp,union power_supply_propval * val)648c0984e5SSebastian Reichel static int olpc_ac_get_prop(struct power_supply *psy,
658c0984e5SSebastian Reichel 			    enum power_supply_property psp,
668c0984e5SSebastian Reichel 			    union power_supply_propval *val)
678c0984e5SSebastian Reichel {
688c0984e5SSebastian Reichel 	int ret = 0;
698c0984e5SSebastian Reichel 	uint8_t status;
708c0984e5SSebastian Reichel 
718c0984e5SSebastian Reichel 	switch (psp) {
728c0984e5SSebastian Reichel 	case POWER_SUPPLY_PROP_ONLINE:
738c0984e5SSebastian Reichel 		ret = olpc_ec_cmd(EC_BAT_STATUS, NULL, 0, &status, 1);
748c0984e5SSebastian Reichel 		if (ret)
758c0984e5SSebastian Reichel 			return ret;
768c0984e5SSebastian Reichel 
778c0984e5SSebastian Reichel 		val->intval = !!(status & BAT_STAT_AC);
788c0984e5SSebastian Reichel 		break;
798c0984e5SSebastian Reichel 	default:
808c0984e5SSebastian Reichel 		ret = -EINVAL;
818c0984e5SSebastian Reichel 		break;
828c0984e5SSebastian Reichel 	}
838c0984e5SSebastian Reichel 	return ret;
848c0984e5SSebastian Reichel }
858c0984e5SSebastian Reichel 
868c0984e5SSebastian Reichel static enum power_supply_property olpc_ac_props[] = {
878c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_ONLINE,
888c0984e5SSebastian Reichel };
898c0984e5SSebastian Reichel 
908c0984e5SSebastian Reichel static const struct power_supply_desc olpc_ac_desc = {
9129e9eff4SLubomir Rintel 	.name = "olpc_ac",
928c0984e5SSebastian Reichel 	.type = POWER_SUPPLY_TYPE_MAINS,
938c0984e5SSebastian Reichel 	.properties = olpc_ac_props,
948c0984e5SSebastian Reichel 	.num_properties = ARRAY_SIZE(olpc_ac_props),
958c0984e5SSebastian Reichel 	.get_property = olpc_ac_get_prop,
968c0984e5SSebastian Reichel };
978c0984e5SSebastian Reichel 
olpc_bat_get_status(struct olpc_battery_data * data,union power_supply_propval * val,uint8_t ec_byte)9833554d81SLubomir Rintel static int olpc_bat_get_status(struct olpc_battery_data *data,
9933554d81SLubomir Rintel 		union power_supply_propval *val, uint8_t ec_byte)
1008c0984e5SSebastian Reichel {
1018ecefda2SLubomir Rintel 	if (data->new_proto) {
1028c0984e5SSebastian Reichel 		if (ec_byte & (BAT_STAT_CHARGING | BAT_STAT_TRICKLE))
1038c0984e5SSebastian Reichel 			val->intval = POWER_SUPPLY_STATUS_CHARGING;
1048c0984e5SSebastian Reichel 		else if (ec_byte & BAT_STAT_DISCHARGING)
1058c0984e5SSebastian Reichel 			val->intval = POWER_SUPPLY_STATUS_DISCHARGING;
1068c0984e5SSebastian Reichel 		else if (ec_byte & BAT_STAT_FULL)
1078c0984e5SSebastian Reichel 			val->intval = POWER_SUPPLY_STATUS_FULL;
1088c0984e5SSebastian Reichel 		else /* er,... */
1098c0984e5SSebastian Reichel 			val->intval = POWER_SUPPLY_STATUS_NOT_CHARGING;
1108c0984e5SSebastian Reichel 	} else {
1118c0984e5SSebastian Reichel 		/* Older EC didn't report charge/discharge bits */
1128c0984e5SSebastian Reichel 		if (!(ec_byte & BAT_STAT_AC)) /* No AC means discharging */
1138c0984e5SSebastian Reichel 			val->intval = POWER_SUPPLY_STATUS_DISCHARGING;
1148c0984e5SSebastian Reichel 		else if (ec_byte & BAT_STAT_FULL)
1158c0984e5SSebastian Reichel 			val->intval = POWER_SUPPLY_STATUS_FULL;
1168c0984e5SSebastian Reichel 		else /* Not _necessarily_ true but EC doesn't tell all yet */
1178c0984e5SSebastian Reichel 			val->intval = POWER_SUPPLY_STATUS_CHARGING;
1188c0984e5SSebastian Reichel 	}
1198c0984e5SSebastian Reichel 
1208c0984e5SSebastian Reichel 	return 0;
1218c0984e5SSebastian Reichel }
1228c0984e5SSebastian Reichel 
olpc_bat_get_health(union power_supply_propval * val)1238c0984e5SSebastian Reichel static int olpc_bat_get_health(union power_supply_propval *val)
1248c0984e5SSebastian Reichel {
1258c0984e5SSebastian Reichel 	uint8_t ec_byte;
1268c0984e5SSebastian Reichel 	int ret;
1278c0984e5SSebastian Reichel 
1288c0984e5SSebastian Reichel 	ret = olpc_ec_cmd(EC_BAT_ERRCODE, NULL, 0, &ec_byte, 1);
1298c0984e5SSebastian Reichel 	if (ret)
1308c0984e5SSebastian Reichel 		return ret;
1318c0984e5SSebastian Reichel 
1328c0984e5SSebastian Reichel 	switch (ec_byte) {
1338c0984e5SSebastian Reichel 	case 0:
1348c0984e5SSebastian Reichel 		val->intval = POWER_SUPPLY_HEALTH_GOOD;
1358c0984e5SSebastian Reichel 		break;
1368c0984e5SSebastian Reichel 
1378c0984e5SSebastian Reichel 	case BAT_ERR_OVERTEMP:
1388c0984e5SSebastian Reichel 		val->intval = POWER_SUPPLY_HEALTH_OVERHEAT;
1398c0984e5SSebastian Reichel 		break;
1408c0984e5SSebastian Reichel 
1418c0984e5SSebastian Reichel 	case BAT_ERR_OVERVOLTAGE:
1428c0984e5SSebastian Reichel 		val->intval = POWER_SUPPLY_HEALTH_OVERVOLTAGE;
1438c0984e5SSebastian Reichel 		break;
1448c0984e5SSebastian Reichel 
1458c0984e5SSebastian Reichel 	case BAT_ERR_INFOFAIL:
1468c0984e5SSebastian Reichel 	case BAT_ERR_OUT_OF_CONTROL:
1478c0984e5SSebastian Reichel 	case BAT_ERR_ID_FAIL:
1488c0984e5SSebastian Reichel 	case BAT_ERR_ACR_FAIL:
1498c0984e5SSebastian Reichel 		val->intval = POWER_SUPPLY_HEALTH_UNSPEC_FAILURE;
1508c0984e5SSebastian Reichel 		break;
1518c0984e5SSebastian Reichel 
1528c0984e5SSebastian Reichel 	default:
1538c0984e5SSebastian Reichel 		/* Eep. We don't know this failure code */
1548c0984e5SSebastian Reichel 		ret = -EIO;
1558c0984e5SSebastian Reichel 	}
1568c0984e5SSebastian Reichel 
1578c0984e5SSebastian Reichel 	return ret;
1588c0984e5SSebastian Reichel }
1598c0984e5SSebastian Reichel 
olpc_bat_get_mfr(union power_supply_propval * val)1608c0984e5SSebastian Reichel static int olpc_bat_get_mfr(union power_supply_propval *val)
1618c0984e5SSebastian Reichel {
1628c0984e5SSebastian Reichel 	uint8_t ec_byte;
1638c0984e5SSebastian Reichel 	int ret;
1648c0984e5SSebastian Reichel 
1658c0984e5SSebastian Reichel 	ec_byte = BAT_ADDR_MFR_TYPE;
1668c0984e5SSebastian Reichel 	ret = olpc_ec_cmd(EC_BAT_EEPROM, &ec_byte, 1, &ec_byte, 1);
1678c0984e5SSebastian Reichel 	if (ret)
1688c0984e5SSebastian Reichel 		return ret;
1698c0984e5SSebastian Reichel 
1708c0984e5SSebastian Reichel 	switch (ec_byte >> 4) {
1718c0984e5SSebastian Reichel 	case 1:
1728c0984e5SSebastian Reichel 		val->strval = "Gold Peak";
1738c0984e5SSebastian Reichel 		break;
1748c0984e5SSebastian Reichel 	case 2:
1758c0984e5SSebastian Reichel 		val->strval = "BYD";
1768c0984e5SSebastian Reichel 		break;
1778c0984e5SSebastian Reichel 	default:
1788c0984e5SSebastian Reichel 		val->strval = "Unknown";
1798c0984e5SSebastian Reichel 		break;
1808c0984e5SSebastian Reichel 	}
1818c0984e5SSebastian Reichel 
1828c0984e5SSebastian Reichel 	return ret;
1838c0984e5SSebastian Reichel }
1848c0984e5SSebastian Reichel 
olpc_bat_get_tech(union power_supply_propval * val)1858c0984e5SSebastian Reichel static int olpc_bat_get_tech(union power_supply_propval *val)
1868c0984e5SSebastian Reichel {
1878c0984e5SSebastian Reichel 	uint8_t ec_byte;
1888c0984e5SSebastian Reichel 	int ret;
1898c0984e5SSebastian Reichel 
1908c0984e5SSebastian Reichel 	ec_byte = BAT_ADDR_MFR_TYPE;
1918c0984e5SSebastian Reichel 	ret = olpc_ec_cmd(EC_BAT_EEPROM, &ec_byte, 1, &ec_byte, 1);
1928c0984e5SSebastian Reichel 	if (ret)
1938c0984e5SSebastian Reichel 		return ret;
1948c0984e5SSebastian Reichel 
1958c0984e5SSebastian Reichel 	switch (ec_byte & 0xf) {
1968c0984e5SSebastian Reichel 	case 1:
1978c0984e5SSebastian Reichel 		val->intval = POWER_SUPPLY_TECHNOLOGY_NiMH;
1988c0984e5SSebastian Reichel 		break;
1998c0984e5SSebastian Reichel 	case 2:
2008c0984e5SSebastian Reichel 		val->intval = POWER_SUPPLY_TECHNOLOGY_LiFe;
2018c0984e5SSebastian Reichel 		break;
2028c0984e5SSebastian Reichel 	default:
2038c0984e5SSebastian Reichel 		val->intval = POWER_SUPPLY_TECHNOLOGY_UNKNOWN;
2048c0984e5SSebastian Reichel 		break;
2058c0984e5SSebastian Reichel 	}
2068c0984e5SSebastian Reichel 
2078c0984e5SSebastian Reichel 	return ret;
2088c0984e5SSebastian Reichel }
2098c0984e5SSebastian Reichel 
olpc_bat_get_charge_full_design(union power_supply_propval * val)2108c0984e5SSebastian Reichel static int olpc_bat_get_charge_full_design(union power_supply_propval *val)
2118c0984e5SSebastian Reichel {
2128c0984e5SSebastian Reichel 	uint8_t ec_byte;
2138c0984e5SSebastian Reichel 	union power_supply_propval tech;
2148c0984e5SSebastian Reichel 	int ret, mfr;
2158c0984e5SSebastian Reichel 
2168c0984e5SSebastian Reichel 	ret = olpc_bat_get_tech(&tech);
2178c0984e5SSebastian Reichel 	if (ret)
2188c0984e5SSebastian Reichel 		return ret;
2198c0984e5SSebastian Reichel 
2208c0984e5SSebastian Reichel 	ec_byte = BAT_ADDR_MFR_TYPE;
2218c0984e5SSebastian Reichel 	ret = olpc_ec_cmd(EC_BAT_EEPROM, &ec_byte, 1, &ec_byte, 1);
2228c0984e5SSebastian Reichel 	if (ret)
2238c0984e5SSebastian Reichel 		return ret;
2248c0984e5SSebastian Reichel 
2258c0984e5SSebastian Reichel 	mfr = ec_byte >> 4;
2268c0984e5SSebastian Reichel 
2278c0984e5SSebastian Reichel 	switch (tech.intval) {
2288c0984e5SSebastian Reichel 	case POWER_SUPPLY_TECHNOLOGY_NiMH:
2298c0984e5SSebastian Reichel 		switch (mfr) {
2308c0984e5SSebastian Reichel 		case 1: /* Gold Peak */
2318c0984e5SSebastian Reichel 			val->intval = 3000000*.8;
2328c0984e5SSebastian Reichel 			break;
2338c0984e5SSebastian Reichel 		default:
2348c0984e5SSebastian Reichel 			return -EIO;
2358c0984e5SSebastian Reichel 		}
2368c0984e5SSebastian Reichel 		break;
2378c0984e5SSebastian Reichel 
2388c0984e5SSebastian Reichel 	case POWER_SUPPLY_TECHNOLOGY_LiFe:
2398c0984e5SSebastian Reichel 		switch (mfr) {
2408c0984e5SSebastian Reichel 		case 1: /* Gold Peak, fall through */
2418c0984e5SSebastian Reichel 		case 2: /* BYD */
2428c0984e5SSebastian Reichel 			val->intval = 2800000;
2438c0984e5SSebastian Reichel 			break;
2448c0984e5SSebastian Reichel 		default:
2458c0984e5SSebastian Reichel 			return -EIO;
2468c0984e5SSebastian Reichel 		}
2478c0984e5SSebastian Reichel 		break;
2488c0984e5SSebastian Reichel 
2498c0984e5SSebastian Reichel 	default:
2508c0984e5SSebastian Reichel 		return -EIO;
2518c0984e5SSebastian Reichel 	}
2528c0984e5SSebastian Reichel 
2538c0984e5SSebastian Reichel 	return ret;
2548c0984e5SSebastian Reichel }
2558c0984e5SSebastian Reichel 
olpc_bat_get_charge_now(union power_supply_propval * val)2568c0984e5SSebastian Reichel static int olpc_bat_get_charge_now(union power_supply_propval *val)
2578c0984e5SSebastian Reichel {
2588c0984e5SSebastian Reichel 	uint8_t soc;
2598c0984e5SSebastian Reichel 	union power_supply_propval full;
2608c0984e5SSebastian Reichel 	int ret;
2618c0984e5SSebastian Reichel 
2628c0984e5SSebastian Reichel 	ret = olpc_ec_cmd(EC_BAT_SOC, NULL, 0, &soc, 1);
2638c0984e5SSebastian Reichel 	if (ret)
2648c0984e5SSebastian Reichel 		return ret;
2658c0984e5SSebastian Reichel 
2668c0984e5SSebastian Reichel 	ret = olpc_bat_get_charge_full_design(&full);
2678c0984e5SSebastian Reichel 	if (ret)
2688c0984e5SSebastian Reichel 		return ret;
2698c0984e5SSebastian Reichel 
2708c0984e5SSebastian Reichel 	val->intval = soc * (full.intval / 100);
2718c0984e5SSebastian Reichel 	return 0;
2728c0984e5SSebastian Reichel }
2738c0984e5SSebastian Reichel 
olpc_bat_get_voltage_max_design(union power_supply_propval * val)2748c0984e5SSebastian Reichel static int olpc_bat_get_voltage_max_design(union power_supply_propval *val)
2758c0984e5SSebastian Reichel {
2768c0984e5SSebastian Reichel 	uint8_t ec_byte;
2778c0984e5SSebastian Reichel 	union power_supply_propval tech;
2788c0984e5SSebastian Reichel 	int mfr;
2798c0984e5SSebastian Reichel 	int ret;
2808c0984e5SSebastian Reichel 
2818c0984e5SSebastian Reichel 	ret = olpc_bat_get_tech(&tech);
2828c0984e5SSebastian Reichel 	if (ret)
2838c0984e5SSebastian Reichel 		return ret;
2848c0984e5SSebastian Reichel 
2858c0984e5SSebastian Reichel 	ec_byte = BAT_ADDR_MFR_TYPE;
2868c0984e5SSebastian Reichel 	ret = olpc_ec_cmd(EC_BAT_EEPROM, &ec_byte, 1, &ec_byte, 1);
2878c0984e5SSebastian Reichel 	if (ret)
2888c0984e5SSebastian Reichel 		return ret;
2898c0984e5SSebastian Reichel 
2908c0984e5SSebastian Reichel 	mfr = ec_byte >> 4;
2918c0984e5SSebastian Reichel 
2928c0984e5SSebastian Reichel 	switch (tech.intval) {
2938c0984e5SSebastian Reichel 	case POWER_SUPPLY_TECHNOLOGY_NiMH:
2948c0984e5SSebastian Reichel 		switch (mfr) {
2958c0984e5SSebastian Reichel 		case 1: /* Gold Peak */
2968c0984e5SSebastian Reichel 			val->intval = 6000000;
2978c0984e5SSebastian Reichel 			break;
2988c0984e5SSebastian Reichel 		default:
2998c0984e5SSebastian Reichel 			return -EIO;
3008c0984e5SSebastian Reichel 		}
3018c0984e5SSebastian Reichel 		break;
3028c0984e5SSebastian Reichel 
3038c0984e5SSebastian Reichel 	case POWER_SUPPLY_TECHNOLOGY_LiFe:
3048c0984e5SSebastian Reichel 		switch (mfr) {
3058c0984e5SSebastian Reichel 		case 1: /* Gold Peak */
3068c0984e5SSebastian Reichel 			val->intval = 6400000;
3078c0984e5SSebastian Reichel 			break;
3088c0984e5SSebastian Reichel 		case 2: /* BYD */
3098c0984e5SSebastian Reichel 			val->intval = 6500000;
3108c0984e5SSebastian Reichel 			break;
3118c0984e5SSebastian Reichel 		default:
3128c0984e5SSebastian Reichel 			return -EIO;
3138c0984e5SSebastian Reichel 		}
3148c0984e5SSebastian Reichel 		break;
3158c0984e5SSebastian Reichel 
3168c0984e5SSebastian Reichel 	default:
3178c0984e5SSebastian Reichel 		return -EIO;
3188c0984e5SSebastian Reichel 	}
3198c0984e5SSebastian Reichel 
3208c0984e5SSebastian Reichel 	return ret;
3218c0984e5SSebastian Reichel }
3228c0984e5SSebastian Reichel 
ecword_to_cpu(struct olpc_battery_data * data,u16 ec_word)32376311b9aSLubomir Rintel static u16 ecword_to_cpu(struct olpc_battery_data *data, u16 ec_word)
32476311b9aSLubomir Rintel {
32576311b9aSLubomir Rintel 	if (data->little_endian)
326baf5964eSLubomir Rintel 		return le16_to_cpu((__force __le16)ec_word);
32776311b9aSLubomir Rintel 	else
328baf5964eSLubomir Rintel 		return be16_to_cpu((__force __be16)ec_word);
32976311b9aSLubomir Rintel }
33076311b9aSLubomir Rintel 
3318c0984e5SSebastian Reichel /*********************************************************************
3328c0984e5SSebastian Reichel  *		Battery properties
3338c0984e5SSebastian Reichel  *********************************************************************/
olpc_bat_get_property(struct power_supply * psy,enum power_supply_property psp,union power_supply_propval * val)3348c0984e5SSebastian Reichel static int olpc_bat_get_property(struct power_supply *psy,
3358c0984e5SSebastian Reichel 				 enum power_supply_property psp,
3368c0984e5SSebastian Reichel 				 union power_supply_propval *val)
3378c0984e5SSebastian Reichel {
33833554d81SLubomir Rintel 	struct olpc_battery_data *data = power_supply_get_drvdata(psy);
3398c0984e5SSebastian Reichel 	int ret = 0;
340baf5964eSLubomir Rintel 	u16 ec_word;
3418c0984e5SSebastian Reichel 	uint8_t ec_byte;
3428c0984e5SSebastian Reichel 	__be64 ser_buf;
3438c0984e5SSebastian Reichel 
3448c0984e5SSebastian Reichel 	ret = olpc_ec_cmd(EC_BAT_STATUS, NULL, 0, &ec_byte, 1);
3458c0984e5SSebastian Reichel 	if (ret)
3468c0984e5SSebastian Reichel 		return ret;
3478c0984e5SSebastian Reichel 
3488c0984e5SSebastian Reichel 	/* Theoretically there's a race here -- the battery could be
3498c0984e5SSebastian Reichel 	   removed immediately after we check whether it's present, and
3508c0984e5SSebastian Reichel 	   then we query for some other property of the now-absent battery.
3518c0984e5SSebastian Reichel 	   It doesn't matter though -- the EC will return the last-known
3528c0984e5SSebastian Reichel 	   information, and it's as if we just ran that _little_ bit faster
3538c0984e5SSebastian Reichel 	   and managed to read it out before the battery went away. */
3548c0984e5SSebastian Reichel 	if (!(ec_byte & (BAT_STAT_PRESENT | BAT_STAT_TRICKLE)) &&
3558c0984e5SSebastian Reichel 			psp != POWER_SUPPLY_PROP_PRESENT)
3568c0984e5SSebastian Reichel 		return -ENODEV;
3578c0984e5SSebastian Reichel 
3588c0984e5SSebastian Reichel 	switch (psp) {
3598c0984e5SSebastian Reichel 	case POWER_SUPPLY_PROP_STATUS:
36033554d81SLubomir Rintel 		ret = olpc_bat_get_status(data, val, ec_byte);
3618c0984e5SSebastian Reichel 		if (ret)
3628c0984e5SSebastian Reichel 			return ret;
3638c0984e5SSebastian Reichel 		break;
3648c0984e5SSebastian Reichel 	case POWER_SUPPLY_PROP_CHARGE_TYPE:
3658c0984e5SSebastian Reichel 		if (ec_byte & BAT_STAT_TRICKLE)
3668c0984e5SSebastian Reichel 			val->intval = POWER_SUPPLY_CHARGE_TYPE_TRICKLE;
3678c0984e5SSebastian Reichel 		else if (ec_byte & BAT_STAT_CHARGING)
3688c0984e5SSebastian Reichel 			val->intval = POWER_SUPPLY_CHARGE_TYPE_FAST;
3698c0984e5SSebastian Reichel 		else
3708c0984e5SSebastian Reichel 			val->intval = POWER_SUPPLY_CHARGE_TYPE_NONE;
3718c0984e5SSebastian Reichel 		break;
3728c0984e5SSebastian Reichel 	case POWER_SUPPLY_PROP_PRESENT:
3738c0984e5SSebastian Reichel 		val->intval = !!(ec_byte & (BAT_STAT_PRESENT |
3748c0984e5SSebastian Reichel 					    BAT_STAT_TRICKLE));
3758c0984e5SSebastian Reichel 		break;
3768c0984e5SSebastian Reichel 
3778c0984e5SSebastian Reichel 	case POWER_SUPPLY_PROP_HEALTH:
3788c0984e5SSebastian Reichel 		if (ec_byte & BAT_STAT_DESTROY)
3798c0984e5SSebastian Reichel 			val->intval = POWER_SUPPLY_HEALTH_DEAD;
3808c0984e5SSebastian Reichel 		else {
3818c0984e5SSebastian Reichel 			ret = olpc_bat_get_health(val);
3828c0984e5SSebastian Reichel 			if (ret)
3838c0984e5SSebastian Reichel 				return ret;
3848c0984e5SSebastian Reichel 		}
3858c0984e5SSebastian Reichel 		break;
3868c0984e5SSebastian Reichel 
3878c0984e5SSebastian Reichel 	case POWER_SUPPLY_PROP_MANUFACTURER:
3888c0984e5SSebastian Reichel 		ret = olpc_bat_get_mfr(val);
3898c0984e5SSebastian Reichel 		if (ret)
3908c0984e5SSebastian Reichel 			return ret;
3918c0984e5SSebastian Reichel 		break;
3928c0984e5SSebastian Reichel 	case POWER_SUPPLY_PROP_TECHNOLOGY:
3938c0984e5SSebastian Reichel 		ret = olpc_bat_get_tech(val);
3948c0984e5SSebastian Reichel 		if (ret)
3958c0984e5SSebastian Reichel 			return ret;
3968c0984e5SSebastian Reichel 		break;
3978c0984e5SSebastian Reichel 	case POWER_SUPPLY_PROP_VOLTAGE_AVG:
3988c0984e5SSebastian Reichel 	case POWER_SUPPLY_PROP_VOLTAGE_NOW:
3998c0984e5SSebastian Reichel 		ret = olpc_ec_cmd(EC_BAT_VOLTAGE, NULL, 0, (void *)&ec_word, 2);
4008c0984e5SSebastian Reichel 		if (ret)
4018c0984e5SSebastian Reichel 			return ret;
4028c0984e5SSebastian Reichel 
40376311b9aSLubomir Rintel 		val->intval = ecword_to_cpu(data, ec_word) * 9760L / 32;
4048c0984e5SSebastian Reichel 		break;
4058c0984e5SSebastian Reichel 	case POWER_SUPPLY_PROP_CURRENT_AVG:
4068c0984e5SSebastian Reichel 	case POWER_SUPPLY_PROP_CURRENT_NOW:
4078c0984e5SSebastian Reichel 		ret = olpc_ec_cmd(EC_BAT_CURRENT, NULL, 0, (void *)&ec_word, 2);
4088c0984e5SSebastian Reichel 		if (ret)
4098c0984e5SSebastian Reichel 			return ret;
4108c0984e5SSebastian Reichel 
41176311b9aSLubomir Rintel 		val->intval = ecword_to_cpu(data, ec_word) * 15625L / 120;
4128c0984e5SSebastian Reichel 		break;
4138c0984e5SSebastian Reichel 	case POWER_SUPPLY_PROP_CAPACITY:
4148c0984e5SSebastian Reichel 		ret = olpc_ec_cmd(EC_BAT_SOC, NULL, 0, &ec_byte, 1);
4158c0984e5SSebastian Reichel 		if (ret)
4168c0984e5SSebastian Reichel 			return ret;
4178c0984e5SSebastian Reichel 		val->intval = ec_byte;
4188c0984e5SSebastian Reichel 		break;
4198c0984e5SSebastian Reichel 	case POWER_SUPPLY_PROP_CAPACITY_LEVEL:
4208c0984e5SSebastian Reichel 		if (ec_byte & BAT_STAT_FULL)
4218c0984e5SSebastian Reichel 			val->intval = POWER_SUPPLY_CAPACITY_LEVEL_FULL;
4228c0984e5SSebastian Reichel 		else if (ec_byte & BAT_STAT_LOW)
4238c0984e5SSebastian Reichel 			val->intval = POWER_SUPPLY_CAPACITY_LEVEL_LOW;
4248c0984e5SSebastian Reichel 		else
4258c0984e5SSebastian Reichel 			val->intval = POWER_SUPPLY_CAPACITY_LEVEL_NORMAL;
4268c0984e5SSebastian Reichel 		break;
4278c0984e5SSebastian Reichel 	case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN:
4288c0984e5SSebastian Reichel 		ret = olpc_bat_get_charge_full_design(val);
4298c0984e5SSebastian Reichel 		if (ret)
4308c0984e5SSebastian Reichel 			return ret;
4318c0984e5SSebastian Reichel 		break;
4328c0984e5SSebastian Reichel 	case POWER_SUPPLY_PROP_CHARGE_NOW:
4338c0984e5SSebastian Reichel 		ret = olpc_bat_get_charge_now(val);
4348c0984e5SSebastian Reichel 		if (ret)
4358c0984e5SSebastian Reichel 			return ret;
4368c0984e5SSebastian Reichel 		break;
4378c0984e5SSebastian Reichel 	case POWER_SUPPLY_PROP_TEMP:
4388c0984e5SSebastian Reichel 		ret = olpc_ec_cmd(EC_BAT_TEMP, NULL, 0, (void *)&ec_word, 2);
4398c0984e5SSebastian Reichel 		if (ret)
4408c0984e5SSebastian Reichel 			return ret;
4418c0984e5SSebastian Reichel 
44276311b9aSLubomir Rintel 		val->intval = ecword_to_cpu(data, ec_word) * 10 / 256;
4438c0984e5SSebastian Reichel 		break;
4448c0984e5SSebastian Reichel 	case POWER_SUPPLY_PROP_TEMP_AMBIENT:
4458c0984e5SSebastian Reichel 		ret = olpc_ec_cmd(EC_AMB_TEMP, NULL, 0, (void *)&ec_word, 2);
4468c0984e5SSebastian Reichel 		if (ret)
4478c0984e5SSebastian Reichel 			return ret;
4488c0984e5SSebastian Reichel 
44976311b9aSLubomir Rintel 		val->intval = (int)ecword_to_cpu(data, ec_word) * 10 / 256;
4508c0984e5SSebastian Reichel 		break;
4518c0984e5SSebastian Reichel 	case POWER_SUPPLY_PROP_CHARGE_COUNTER:
4528c0984e5SSebastian Reichel 		ret = olpc_ec_cmd(EC_BAT_ACR, NULL, 0, (void *)&ec_word, 2);
4538c0984e5SSebastian Reichel 		if (ret)
4548c0984e5SSebastian Reichel 			return ret;
4558c0984e5SSebastian Reichel 
45676311b9aSLubomir Rintel 		val->intval = ecword_to_cpu(data, ec_word) * 6250 / 15;
4578c0984e5SSebastian Reichel 		break;
4588c0984e5SSebastian Reichel 	case POWER_SUPPLY_PROP_SERIAL_NUMBER:
4598c0984e5SSebastian Reichel 		ret = olpc_ec_cmd(EC_BAT_SERIAL, NULL, 0, (void *)&ser_buf, 8);
4608c0984e5SSebastian Reichel 		if (ret)
4618c0984e5SSebastian Reichel 			return ret;
4628c0984e5SSebastian Reichel 
46333554d81SLubomir Rintel 		sprintf(data->bat_serial, "%016llx", (long long)be64_to_cpu(ser_buf));
46433554d81SLubomir Rintel 		val->strval = data->bat_serial;
4658c0984e5SSebastian Reichel 		break;
4668c0984e5SSebastian Reichel 	case POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN:
4678c0984e5SSebastian Reichel 		ret = olpc_bat_get_voltage_max_design(val);
4688c0984e5SSebastian Reichel 		if (ret)
4698c0984e5SSebastian Reichel 			return ret;
4708c0984e5SSebastian Reichel 		break;
4718c0984e5SSebastian Reichel 	default:
4728c0984e5SSebastian Reichel 		ret = -EINVAL;
4738c0984e5SSebastian Reichel 		break;
4748c0984e5SSebastian Reichel 	}
4758c0984e5SSebastian Reichel 
4768c0984e5SSebastian Reichel 	return ret;
4778c0984e5SSebastian Reichel }
4788c0984e5SSebastian Reichel 
4798c0984e5SSebastian Reichel static enum power_supply_property olpc_xo1_bat_props[] = {
4808c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_STATUS,
4818c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_CHARGE_TYPE,
4828c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_PRESENT,
4838c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_HEALTH,
4848c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_TECHNOLOGY,
4858c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_VOLTAGE_AVG,
4868c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_VOLTAGE_NOW,
4878c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_CURRENT_AVG,
4888c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_CURRENT_NOW,
4898c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_CAPACITY,
4908c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_CAPACITY_LEVEL,
4918c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN,
4928c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_CHARGE_NOW,
4938c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_TEMP,
4948c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_TEMP_AMBIENT,
4958c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_MANUFACTURER,
4968c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_SERIAL_NUMBER,
4978c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_CHARGE_COUNTER,
4988c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN,
4998c0984e5SSebastian Reichel };
5008c0984e5SSebastian Reichel 
5018c0984e5SSebastian Reichel /* XO-1.5 does not have ambient temperature property */
5028c0984e5SSebastian Reichel static enum power_supply_property olpc_xo15_bat_props[] = {
5038c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_STATUS,
5048c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_CHARGE_TYPE,
5058c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_PRESENT,
5068c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_HEALTH,
5078c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_TECHNOLOGY,
5088c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_VOLTAGE_AVG,
5098c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_VOLTAGE_NOW,
5108c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_CURRENT_AVG,
5118c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_CURRENT_NOW,
5128c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_CAPACITY,
5138c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_CAPACITY_LEVEL,
5148c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN,
5158c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_CHARGE_NOW,
5168c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_TEMP,
5178c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_MANUFACTURER,
5188c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_SERIAL_NUMBER,
5198c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_CHARGE_COUNTER,
5208c0984e5SSebastian Reichel 	POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN,
5218c0984e5SSebastian Reichel };
5228c0984e5SSebastian Reichel 
5238c0984e5SSebastian Reichel /* EEPROM reading goes completely around the power_supply API, sadly */
5248c0984e5SSebastian Reichel 
5258c0984e5SSebastian Reichel #define EEPROM_START	0x20
5268c0984e5SSebastian Reichel #define EEPROM_END	0x80
5278c0984e5SSebastian Reichel #define EEPROM_SIZE	(EEPROM_END - EEPROM_START)
5288c0984e5SSebastian Reichel 
olpc_bat_eeprom_read(struct file * filp,struct kobject * kobj,struct bin_attribute * attr,char * buf,loff_t off,size_t count)5298c0984e5SSebastian Reichel static ssize_t olpc_bat_eeprom_read(struct file *filp, struct kobject *kobj,
5308c0984e5SSebastian Reichel 		struct bin_attribute *attr, char *buf, loff_t off, size_t count)
5318c0984e5SSebastian Reichel {
5328c0984e5SSebastian Reichel 	uint8_t ec_byte;
5338c0984e5SSebastian Reichel 	int ret;
5348c0984e5SSebastian Reichel 	int i;
5358c0984e5SSebastian Reichel 
5368c0984e5SSebastian Reichel 	for (i = 0; i < count; i++) {
5378c0984e5SSebastian Reichel 		ec_byte = EEPROM_START + off + i;
5388c0984e5SSebastian Reichel 		ret = olpc_ec_cmd(EC_BAT_EEPROM, &ec_byte, 1, &buf[i], 1);
5398c0984e5SSebastian Reichel 		if (ret) {
5408c0984e5SSebastian Reichel 			pr_err("olpc-battery: "
5418c0984e5SSebastian Reichel 			       "EC_BAT_EEPROM cmd @ 0x%x failed - %d!\n",
5428c0984e5SSebastian Reichel 			       ec_byte, ret);
5438c0984e5SSebastian Reichel 			return -EIO;
5448c0984e5SSebastian Reichel 		}
5458c0984e5SSebastian Reichel 	}
5468c0984e5SSebastian Reichel 
5478c0984e5SSebastian Reichel 	return count;
5488c0984e5SSebastian Reichel }
5498c0984e5SSebastian Reichel 
55031e22087SLubomir Rintel static struct bin_attribute olpc_bat_eeprom = {
5518c0984e5SSebastian Reichel 	.attr = {
5528c0984e5SSebastian Reichel 		.name = "eeprom",
5538c0984e5SSebastian Reichel 		.mode = S_IRUGO,
5548c0984e5SSebastian Reichel 	},
5558c0984e5SSebastian Reichel 	.size = EEPROM_SIZE,
5568c0984e5SSebastian Reichel 	.read = olpc_bat_eeprom_read,
5578c0984e5SSebastian Reichel };
5588c0984e5SSebastian Reichel 
5598c0984e5SSebastian Reichel /* Allow userspace to see the specific error value pulled from the EC */
5608c0984e5SSebastian Reichel 
olpc_bat_error_read(struct device * dev,struct device_attribute * attr,char * buf)5618c0984e5SSebastian Reichel static ssize_t olpc_bat_error_read(struct device *dev,
5628c0984e5SSebastian Reichel 		struct device_attribute *attr, char *buf)
5638c0984e5SSebastian Reichel {
5648c0984e5SSebastian Reichel 	uint8_t ec_byte;
5658c0984e5SSebastian Reichel 	ssize_t ret;
5668c0984e5SSebastian Reichel 
5678c0984e5SSebastian Reichel 	ret = olpc_ec_cmd(EC_BAT_ERRCODE, NULL, 0, &ec_byte, 1);
5688c0984e5SSebastian Reichel 	if (ret < 0)
5698c0984e5SSebastian Reichel 		return ret;
5708c0984e5SSebastian Reichel 
571*a441f3b9Sye xingchen 	return sysfs_emit(buf, "%d\n", ec_byte);
5728c0984e5SSebastian Reichel }
5738c0984e5SSebastian Reichel 
57431e22087SLubomir Rintel static struct device_attribute olpc_bat_error = {
5758c0984e5SSebastian Reichel 	.attr = {
5768c0984e5SSebastian Reichel 		.name = "error",
5778c0984e5SSebastian Reichel 		.mode = S_IRUGO,
5788c0984e5SSebastian Reichel 	},
5798c0984e5SSebastian Reichel 	.show = olpc_bat_error_read,
5808c0984e5SSebastian Reichel };
5818c0984e5SSebastian Reichel 
58231e22087SLubomir Rintel static struct attribute *olpc_bat_sysfs_attrs[] = {
58331e22087SLubomir Rintel 	&olpc_bat_error.attr,
58431e22087SLubomir Rintel 	NULL
58531e22087SLubomir Rintel };
58631e22087SLubomir Rintel 
58731e22087SLubomir Rintel static struct bin_attribute *olpc_bat_sysfs_bin_attrs[] = {
58831e22087SLubomir Rintel 	&olpc_bat_eeprom,
58931e22087SLubomir Rintel 	NULL
59031e22087SLubomir Rintel };
59131e22087SLubomir Rintel 
59231e22087SLubomir Rintel static const struct attribute_group olpc_bat_sysfs_group = {
59331e22087SLubomir Rintel 	.attrs = olpc_bat_sysfs_attrs,
59431e22087SLubomir Rintel 	.bin_attrs = olpc_bat_sysfs_bin_attrs,
59531e22087SLubomir Rintel 
59631e22087SLubomir Rintel };
59731e22087SLubomir Rintel 
59831e22087SLubomir Rintel static const struct attribute_group *olpc_bat_sysfs_groups[] = {
59931e22087SLubomir Rintel 	&olpc_bat_sysfs_group,
60031e22087SLubomir Rintel 	NULL
60131e22087SLubomir Rintel };
60231e22087SLubomir Rintel 
6038c0984e5SSebastian Reichel /*********************************************************************
6048c0984e5SSebastian Reichel  *		Initialisation
6058c0984e5SSebastian Reichel  *********************************************************************/
6068c0984e5SSebastian Reichel 
6078c0984e5SSebastian Reichel static struct power_supply_desc olpc_bat_desc = {
60829e9eff4SLubomir Rintel 	.name = "olpc_battery",
6098c0984e5SSebastian Reichel 	.get_property = olpc_bat_get_property,
6108c0984e5SSebastian Reichel 	.use_for_apm = 1,
6118c0984e5SSebastian Reichel };
6128c0984e5SSebastian Reichel 
olpc_battery_suspend(struct platform_device * pdev,pm_message_t state)6138c0984e5SSebastian Reichel static int olpc_battery_suspend(struct platform_device *pdev,
6148c0984e5SSebastian Reichel 				pm_message_t state)
6158c0984e5SSebastian Reichel {
61633554d81SLubomir Rintel 	struct olpc_battery_data *data = platform_get_drvdata(pdev);
61733554d81SLubomir Rintel 
61833554d81SLubomir Rintel 	if (device_may_wakeup(&data->olpc_ac->dev))
6198c0984e5SSebastian Reichel 		olpc_ec_wakeup_set(EC_SCI_SRC_ACPWR);
6208c0984e5SSebastian Reichel 	else
6218c0984e5SSebastian Reichel 		olpc_ec_wakeup_clear(EC_SCI_SRC_ACPWR);
6228c0984e5SSebastian Reichel 
62333554d81SLubomir Rintel 	if (device_may_wakeup(&data->olpc_bat->dev))
6248c0984e5SSebastian Reichel 		olpc_ec_wakeup_set(EC_SCI_SRC_BATTERY | EC_SCI_SRC_BATSOC
6258c0984e5SSebastian Reichel 				   | EC_SCI_SRC_BATERR);
6268c0984e5SSebastian Reichel 	else
6278c0984e5SSebastian Reichel 		olpc_ec_wakeup_clear(EC_SCI_SRC_BATTERY | EC_SCI_SRC_BATSOC
6288c0984e5SSebastian Reichel 				     | EC_SCI_SRC_BATERR);
6298c0984e5SSebastian Reichel 
6308c0984e5SSebastian Reichel 	return 0;
6318c0984e5SSebastian Reichel }
6328c0984e5SSebastian Reichel 
olpc_battery_probe(struct platform_device * pdev)6338c0984e5SSebastian Reichel static int olpc_battery_probe(struct platform_device *pdev)
6348c0984e5SSebastian Reichel {
63531e22087SLubomir Rintel 	struct power_supply_config bat_psy_cfg = {};
63631e22087SLubomir Rintel 	struct power_supply_config ac_psy_cfg = {};
63733554d81SLubomir Rintel 	struct olpc_battery_data *data;
638c9d84681SLiang He 	struct device_node *np;
6398c0984e5SSebastian Reichel 	uint8_t status;
6408ecefda2SLubomir Rintel 	uint8_t ecver;
64133554d81SLubomir Rintel 	int ret;
64233554d81SLubomir Rintel 
64333554d81SLubomir Rintel 	data = devm_kzalloc(&pdev->dev, sizeof(*data), GFP_KERNEL);
64433554d81SLubomir Rintel 	if (!data)
64533554d81SLubomir Rintel 		return -ENOMEM;
64633554d81SLubomir Rintel 	platform_set_drvdata(pdev, data);
6478c0984e5SSebastian Reichel 
6488ecefda2SLubomir Rintel 	/* See if the EC is already there and get the EC revision */
6498ecefda2SLubomir Rintel 	ret = olpc_ec_cmd(EC_FIRMWARE_REV, NULL, 0, &ecver, 1);
6508ecefda2SLubomir Rintel 	if (ret)
6518ecefda2SLubomir Rintel 		return ret;
6528ecefda2SLubomir Rintel 
653c9d84681SLiang He 	np = of_find_compatible_node(NULL, NULL, "olpc,xo1.75-ec");
654c9d84681SLiang He 	if (np) {
655c9d84681SLiang He 		of_node_put(np);
65676311b9aSLubomir Rintel 		/* XO 1.75 */
65776311b9aSLubomir Rintel 		data->new_proto = true;
65876311b9aSLubomir Rintel 		data->little_endian = true;
65976311b9aSLubomir Rintel 	} else if (ecver > 0x44) {
6608ecefda2SLubomir Rintel 		/* XO 1 or 1.5 with a new EC firmware. */
6618ecefda2SLubomir Rintel 		data->new_proto = true;
6628ecefda2SLubomir Rintel 	} else if (ecver < 0x44) {
6638c0984e5SSebastian Reichel 		/*
6648ecefda2SLubomir Rintel 		 * We've seen a number of EC protocol changes; this driver
6658ecefda2SLubomir Rintel 		 * requires the latest EC protocol, supported by 0x44 and above.
6668c0984e5SSebastian Reichel 		 */
6678c0984e5SSebastian Reichel 		printk(KERN_NOTICE "OLPC EC version 0x%02x too old for "
6688ecefda2SLubomir Rintel 			"battery driver.\n", ecver);
6698c0984e5SSebastian Reichel 		return -ENXIO;
6708c0984e5SSebastian Reichel 	}
6718c0984e5SSebastian Reichel 
6728c0984e5SSebastian Reichel 	ret = olpc_ec_cmd(EC_BAT_STATUS, NULL, 0, &status, 1);
6738c0984e5SSebastian Reichel 	if (ret)
6748c0984e5SSebastian Reichel 		return ret;
6758c0984e5SSebastian Reichel 
6768c0984e5SSebastian Reichel 	/* Ignore the status. It doesn't actually matter */
6778c0984e5SSebastian Reichel 
67831e22087SLubomir Rintel 	ac_psy_cfg.of_node = pdev->dev.of_node;
67931e22087SLubomir Rintel 	ac_psy_cfg.drv_data = data;
68033554d81SLubomir Rintel 
68131e22087SLubomir Rintel 	data->olpc_ac = devm_power_supply_register(&pdev->dev, &olpc_ac_desc,
68231e22087SLubomir Rintel 								&ac_psy_cfg);
68333554d81SLubomir Rintel 	if (IS_ERR(data->olpc_ac))
68433554d81SLubomir Rintel 		return PTR_ERR(data->olpc_ac);
68533554d81SLubomir Rintel 
686f7a228eaSLubomir Rintel 	if (of_device_is_compatible(pdev->dev.of_node, "olpc,xo1.5-battery")) {
687f7a228eaSLubomir Rintel 		/* XO-1.5 */
6888c0984e5SSebastian Reichel 		olpc_bat_desc.properties = olpc_xo15_bat_props;
6898c0984e5SSebastian Reichel 		olpc_bat_desc.num_properties = ARRAY_SIZE(olpc_xo15_bat_props);
690f7a228eaSLubomir Rintel 	} else {
691f7a228eaSLubomir Rintel 		/* XO-1 */
6928c0984e5SSebastian Reichel 		olpc_bat_desc.properties = olpc_xo1_bat_props;
6938c0984e5SSebastian Reichel 		olpc_bat_desc.num_properties = ARRAY_SIZE(olpc_xo1_bat_props);
6948c0984e5SSebastian Reichel 	}
6958c0984e5SSebastian Reichel 
69631e22087SLubomir Rintel 	bat_psy_cfg.of_node = pdev->dev.of_node;
69731e22087SLubomir Rintel 	bat_psy_cfg.drv_data = data;
69831e22087SLubomir Rintel 	bat_psy_cfg.attr_grp = olpc_bat_sysfs_groups;
69931e22087SLubomir Rintel 
70031e22087SLubomir Rintel 	data->olpc_bat = devm_power_supply_register(&pdev->dev, &olpc_bat_desc,
70131e22087SLubomir Rintel 								&bat_psy_cfg);
702b0280d05SLubomir Rintel 	if (IS_ERR(data->olpc_bat))
703b0280d05SLubomir Rintel 		return PTR_ERR(data->olpc_bat);
7048c0984e5SSebastian Reichel 
7058c0984e5SSebastian Reichel 	if (olpc_ec_wakeup_available()) {
70633554d81SLubomir Rintel 		device_set_wakeup_capable(&data->olpc_ac->dev, true);
70733554d81SLubomir Rintel 		device_set_wakeup_capable(&data->olpc_bat->dev, true);
7088c0984e5SSebastian Reichel 	}
7098c0984e5SSebastian Reichel 
7108c0984e5SSebastian Reichel 	return 0;
7118c0984e5SSebastian Reichel }
7128c0984e5SSebastian Reichel 
7138c0984e5SSebastian Reichel static const struct of_device_id olpc_battery_ids[] = {
7148c0984e5SSebastian Reichel 	{ .compatible = "olpc,xo1-battery" },
715f7a228eaSLubomir Rintel 	{ .compatible = "olpc,xo1.5-battery" },
7168c0984e5SSebastian Reichel 	{}
7178c0984e5SSebastian Reichel };
7188c0984e5SSebastian Reichel MODULE_DEVICE_TABLE(of, olpc_battery_ids);
7198c0984e5SSebastian Reichel 
7208c0984e5SSebastian Reichel static struct platform_driver olpc_battery_driver = {
7218c0984e5SSebastian Reichel 	.driver = {
7228c0984e5SSebastian Reichel 		.name = "olpc-battery",
7238c0984e5SSebastian Reichel 		.of_match_table = olpc_battery_ids,
7248c0984e5SSebastian Reichel 	},
7258c0984e5SSebastian Reichel 	.probe = olpc_battery_probe,
7268c0984e5SSebastian Reichel 	.suspend = olpc_battery_suspend,
7278c0984e5SSebastian Reichel };
7288c0984e5SSebastian Reichel 
7298c0984e5SSebastian Reichel module_platform_driver(olpc_battery_driver);
7308c0984e5SSebastian Reichel 
7318c0984e5SSebastian Reichel MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org>");
7328c0984e5SSebastian Reichel MODULE_LICENSE("GPL");
7338c0984e5SSebastian Reichel MODULE_DESCRIPTION("Battery driver for One Laptop Per Child 'XO' machine");
734