| David Woodhouse | fb97287 | 2007-05-04 00:51:18 +0400 | [diff] [blame] | 1 | /* | 
 | 2 |  * Battery driver for One Laptop Per Child board. | 
 | 3 |  * | 
| David Woodhouse | 690e85a | 2010-08-09 18:13:09 +0100 | [diff] [blame] | 4 |  *	Copyright © 2006-2010  David Woodhouse <dwmw2@infradead.org> | 
| David Woodhouse | fb97287 | 2007-05-04 00:51:18 +0400 | [diff] [blame] | 5 |  * | 
 | 6 |  * This program is free software; you can redistribute it and/or modify | 
 | 7 |  * it under the terms of the GNU General Public License version 2 as | 
 | 8 |  * published by the Free Software Foundation. | 
 | 9 |  */ | 
 | 10 |  | 
| Andres Salomon | 04a820e | 2009-06-30 02:14:00 -0400 | [diff] [blame] | 11 | #include <linux/kernel.h> | 
| David Woodhouse | fb97287 | 2007-05-04 00:51:18 +0400 | [diff] [blame] | 12 | #include <linux/module.h> | 
| Andres Salomon | 144bbea | 2009-06-30 02:15:26 -0400 | [diff] [blame] | 13 | #include <linux/types.h> | 
| David Woodhouse | fb97287 | 2007-05-04 00:51:18 +0400 | [diff] [blame] | 14 | #include <linux/err.h> | 
| Andres Salomon | 144bbea | 2009-06-30 02:15:26 -0400 | [diff] [blame] | 15 | #include <linux/device.h> | 
| David Woodhouse | fb97287 | 2007-05-04 00:51:18 +0400 | [diff] [blame] | 16 | #include <linux/platform_device.h> | 
 | 17 | #include <linux/power_supply.h> | 
 | 18 | #include <linux/jiffies.h> | 
 | 19 | #include <linux/sched.h> | 
 | 20 | #include <asm/olpc.h> | 
 | 21 |  | 
 | 22 |  | 
 | 23 | #define EC_BAT_VOLTAGE	0x10	/* uint16_t,	*9.76/32,    mV   */ | 
 | 24 | #define EC_BAT_CURRENT	0x11	/* int16_t,	*15.625/120, mA   */ | 
| Andres Salomon | 75d8807 | 2008-05-14 16:20:38 -0700 | [diff] [blame] | 25 | #define EC_BAT_ACR	0x12	/* int16_t,	*6250/15,    µAh  */ | 
| David Woodhouse | fb97287 | 2007-05-04 00:51:18 +0400 | [diff] [blame] | 26 | #define EC_BAT_TEMP	0x13	/* uint16_t,	*100/256,   °C  */ | 
 | 27 | #define EC_AMB_TEMP	0x14	/* uint16_t,	*100/256,   °C  */ | 
 | 28 | #define EC_BAT_STATUS	0x15	/* uint8_t,	bitmask */ | 
 | 29 | #define EC_BAT_SOC	0x16	/* uint8_t,	percentage */ | 
 | 30 | #define EC_BAT_SERIAL	0x17	/* uint8_t[6] */ | 
 | 31 | #define EC_BAT_EEPROM	0x18	/* uint8_t adr as input, uint8_t output */ | 
 | 32 | #define EC_BAT_ERRCODE	0x1f	/* uint8_t,	bitmask */ | 
 | 33 |  | 
 | 34 | #define BAT_STAT_PRESENT	0x01 | 
 | 35 | #define BAT_STAT_FULL		0x02 | 
 | 36 | #define BAT_STAT_LOW		0x04 | 
 | 37 | #define BAT_STAT_DESTROY	0x08 | 
 | 38 | #define BAT_STAT_AC		0x10 | 
 | 39 | #define BAT_STAT_CHARGING	0x20 | 
 | 40 | #define BAT_STAT_DISCHARGING	0x40 | 
| Andres Salomon | 8f7e579 | 2009-06-30 02:16:17 -0400 | [diff] [blame] | 41 | #define BAT_STAT_TRICKLE	0x80 | 
| David Woodhouse | fb97287 | 2007-05-04 00:51:18 +0400 | [diff] [blame] | 42 |  | 
 | 43 | #define BAT_ERR_INFOFAIL	0x02 | 
 | 44 | #define BAT_ERR_OVERVOLTAGE	0x04 | 
 | 45 | #define BAT_ERR_OVERTEMP	0x05 | 
 | 46 | #define BAT_ERR_GAUGESTOP	0x06 | 
 | 47 | #define BAT_ERR_OUT_OF_CONTROL	0x07 | 
 | 48 | #define BAT_ERR_ID_FAIL		0x09 | 
 | 49 | #define BAT_ERR_ACR_FAIL	0x10 | 
 | 50 |  | 
 | 51 | #define BAT_ADDR_MFR_TYPE	0x5F | 
 | 52 |  | 
 | 53 | /********************************************************************* | 
 | 54 |  *		Power | 
 | 55 |  *********************************************************************/ | 
 | 56 |  | 
 | 57 | static int olpc_ac_get_prop(struct power_supply *psy, | 
 | 58 | 			    enum power_supply_property psp, | 
 | 59 | 			    union power_supply_propval *val) | 
 | 60 | { | 
 | 61 | 	int ret = 0; | 
 | 62 | 	uint8_t status; | 
 | 63 |  | 
 | 64 | 	switch (psp) { | 
 | 65 | 	case POWER_SUPPLY_PROP_ONLINE: | 
 | 66 | 		ret = olpc_ec_cmd(EC_BAT_STATUS, NULL, 0, &status, 1); | 
 | 67 | 		if (ret) | 
 | 68 | 			return ret; | 
 | 69 |  | 
 | 70 | 		val->intval = !!(status & BAT_STAT_AC); | 
 | 71 | 		break; | 
 | 72 | 	default: | 
 | 73 | 		ret = -EINVAL; | 
 | 74 | 		break; | 
 | 75 | 	} | 
 | 76 | 	return ret; | 
 | 77 | } | 
 | 78 |  | 
 | 79 | static enum power_supply_property olpc_ac_props[] = { | 
 | 80 | 	POWER_SUPPLY_PROP_ONLINE, | 
 | 81 | }; | 
 | 82 |  | 
 | 83 | static struct power_supply olpc_ac = { | 
 | 84 | 	.name = "olpc-ac", | 
 | 85 | 	.type = POWER_SUPPLY_TYPE_MAINS, | 
 | 86 | 	.properties = olpc_ac_props, | 
 | 87 | 	.num_properties = ARRAY_SIZE(olpc_ac_props), | 
 | 88 | 	.get_property = olpc_ac_get_prop, | 
 | 89 | }; | 
 | 90 |  | 
| David Woodhouse | 1ca5b9d | 2008-05-04 01:31:42 -0400 | [diff] [blame] | 91 | static char bat_serial[17]; /* Ick */ | 
 | 92 |  | 
| Andres Salomon | b2bd8a3 | 2008-05-02 13:41:59 -0700 | [diff] [blame] | 93 | static int olpc_bat_get_status(union power_supply_propval *val, uint8_t ec_byte) | 
 | 94 | { | 
 | 95 | 	if (olpc_platform_info.ecver > 0x44) { | 
| Andres Salomon | 8f7e579 | 2009-06-30 02:16:17 -0400 | [diff] [blame] | 96 | 		if (ec_byte & (BAT_STAT_CHARGING | BAT_STAT_TRICKLE)) | 
| Andres Salomon | b2bd8a3 | 2008-05-02 13:41:59 -0700 | [diff] [blame] | 97 | 			val->intval = POWER_SUPPLY_STATUS_CHARGING; | 
 | 98 | 		else if (ec_byte & BAT_STAT_DISCHARGING) | 
 | 99 | 			val->intval = POWER_SUPPLY_STATUS_DISCHARGING; | 
 | 100 | 		else if (ec_byte & BAT_STAT_FULL) | 
 | 101 | 			val->intval = POWER_SUPPLY_STATUS_FULL; | 
 | 102 | 		else /* er,... */ | 
 | 103 | 			val->intval = POWER_SUPPLY_STATUS_NOT_CHARGING; | 
 | 104 | 	} else { | 
 | 105 | 		/* Older EC didn't report charge/discharge bits */ | 
 | 106 | 		if (!(ec_byte & BAT_STAT_AC)) /* No AC means discharging */ | 
 | 107 | 			val->intval = POWER_SUPPLY_STATUS_DISCHARGING; | 
 | 108 | 		else if (ec_byte & BAT_STAT_FULL) | 
 | 109 | 			val->intval = POWER_SUPPLY_STATUS_FULL; | 
 | 110 | 		else /* Not _necessarily_ true but EC doesn't tell all yet */ | 
 | 111 | 			val->intval = POWER_SUPPLY_STATUS_CHARGING; | 
 | 112 | 	} | 
 | 113 |  | 
 | 114 | 	return 0; | 
 | 115 | } | 
 | 116 |  | 
 | 117 | static int olpc_bat_get_health(union power_supply_propval *val) | 
 | 118 | { | 
 | 119 | 	uint8_t ec_byte; | 
 | 120 | 	int ret; | 
 | 121 |  | 
 | 122 | 	ret = olpc_ec_cmd(EC_BAT_ERRCODE, NULL, 0, &ec_byte, 1); | 
 | 123 | 	if (ret) | 
 | 124 | 		return ret; | 
 | 125 |  | 
 | 126 | 	switch (ec_byte) { | 
 | 127 | 	case 0: | 
 | 128 | 		val->intval = POWER_SUPPLY_HEALTH_GOOD; | 
 | 129 | 		break; | 
 | 130 |  | 
 | 131 | 	case BAT_ERR_OVERTEMP: | 
 | 132 | 		val->intval = POWER_SUPPLY_HEALTH_OVERHEAT; | 
 | 133 | 		break; | 
 | 134 |  | 
 | 135 | 	case BAT_ERR_OVERVOLTAGE: | 
 | 136 | 		val->intval = POWER_SUPPLY_HEALTH_OVERVOLTAGE; | 
 | 137 | 		break; | 
 | 138 |  | 
 | 139 | 	case BAT_ERR_INFOFAIL: | 
 | 140 | 	case BAT_ERR_OUT_OF_CONTROL: | 
 | 141 | 	case BAT_ERR_ID_FAIL: | 
 | 142 | 	case BAT_ERR_ACR_FAIL: | 
 | 143 | 		val->intval = POWER_SUPPLY_HEALTH_UNSPEC_FAILURE; | 
 | 144 | 		break; | 
 | 145 |  | 
 | 146 | 	default: | 
 | 147 | 		/* Eep. We don't know this failure code */ | 
 | 148 | 		ret = -EIO; | 
 | 149 | 	} | 
 | 150 |  | 
 | 151 | 	return ret; | 
 | 152 | } | 
 | 153 |  | 
 | 154 | static int olpc_bat_get_mfr(union power_supply_propval *val) | 
 | 155 | { | 
 | 156 | 	uint8_t ec_byte; | 
 | 157 | 	int ret; | 
 | 158 |  | 
 | 159 | 	ec_byte = BAT_ADDR_MFR_TYPE; | 
 | 160 | 	ret = olpc_ec_cmd(EC_BAT_EEPROM, &ec_byte, 1, &ec_byte, 1); | 
 | 161 | 	if (ret) | 
 | 162 | 		return ret; | 
 | 163 |  | 
 | 164 | 	switch (ec_byte >> 4) { | 
 | 165 | 	case 1: | 
 | 166 | 		val->strval = "Gold Peak"; | 
 | 167 | 		break; | 
 | 168 | 	case 2: | 
 | 169 | 		val->strval = "BYD"; | 
 | 170 | 		break; | 
 | 171 | 	default: | 
 | 172 | 		val->strval = "Unknown"; | 
 | 173 | 		break; | 
 | 174 | 	} | 
 | 175 |  | 
 | 176 | 	return ret; | 
 | 177 | } | 
 | 178 |  | 
 | 179 | static int olpc_bat_get_tech(union power_supply_propval *val) | 
 | 180 | { | 
 | 181 | 	uint8_t ec_byte; | 
 | 182 | 	int ret; | 
 | 183 |  | 
 | 184 | 	ec_byte = BAT_ADDR_MFR_TYPE; | 
 | 185 | 	ret = olpc_ec_cmd(EC_BAT_EEPROM, &ec_byte, 1, &ec_byte, 1); | 
 | 186 | 	if (ret) | 
 | 187 | 		return ret; | 
 | 188 |  | 
 | 189 | 	switch (ec_byte & 0xf) { | 
 | 190 | 	case 1: | 
 | 191 | 		val->intval = POWER_SUPPLY_TECHNOLOGY_NiMH; | 
 | 192 | 		break; | 
 | 193 | 	case 2: | 
 | 194 | 		val->intval = POWER_SUPPLY_TECHNOLOGY_LiFe; | 
 | 195 | 		break; | 
 | 196 | 	default: | 
 | 197 | 		val->intval = POWER_SUPPLY_TECHNOLOGY_UNKNOWN; | 
 | 198 | 		break; | 
 | 199 | 	} | 
 | 200 |  | 
 | 201 | 	return ret; | 
 | 202 | } | 
 | 203 |  | 
| Sascha Silbe | b202a5e | 2010-12-10 23:05:19 +0100 | [diff] [blame] | 204 | static int olpc_bat_get_charge_full_design(union power_supply_propval *val) | 
 | 205 | { | 
 | 206 | 	uint8_t ec_byte; | 
 | 207 | 	union power_supply_propval tech; | 
 | 208 | 	int ret, mfr; | 
 | 209 |  | 
 | 210 | 	ret = olpc_bat_get_tech(&tech); | 
 | 211 | 	if (ret) | 
 | 212 | 		return ret; | 
 | 213 |  | 
 | 214 | 	ec_byte = BAT_ADDR_MFR_TYPE; | 
 | 215 | 	ret = olpc_ec_cmd(EC_BAT_EEPROM, &ec_byte, 1, &ec_byte, 1); | 
 | 216 | 	if (ret) | 
 | 217 | 		return ret; | 
 | 218 |  | 
 | 219 | 	mfr = ec_byte >> 4; | 
 | 220 |  | 
 | 221 | 	switch (tech.intval) { | 
 | 222 | 	case POWER_SUPPLY_TECHNOLOGY_NiMH: | 
 | 223 | 		switch (mfr) { | 
 | 224 | 		case 1: /* Gold Peak */ | 
 | 225 | 			val->intval = 3000000*.8; | 
 | 226 | 			break; | 
 | 227 | 		default: | 
 | 228 | 			return -EIO; | 
 | 229 | 		} | 
 | 230 | 		break; | 
 | 231 |  | 
 | 232 | 	case POWER_SUPPLY_TECHNOLOGY_LiFe: | 
 | 233 | 		switch (mfr) { | 
 | 234 | 		case 1: /* Gold Peak */ | 
 | 235 | 			val->intval = 2800000; | 
 | 236 | 			break; | 
 | 237 | 		case 2: /* BYD */ | 
 | 238 | 			val->intval = 3100000; | 
 | 239 | 			break; | 
 | 240 | 		default: | 
 | 241 | 			return -EIO; | 
 | 242 | 		} | 
 | 243 | 		break; | 
 | 244 |  | 
 | 245 | 	default: | 
 | 246 | 		return -EIO; | 
 | 247 | 	} | 
 | 248 |  | 
 | 249 | 	return ret; | 
 | 250 | } | 
 | 251 |  | 
| Sascha Silbe | 20fd983 | 2010-12-10 23:05:20 +0100 | [diff] [blame] | 252 | static int olpc_bat_get_charge_now(union power_supply_propval *val) | 
 | 253 | { | 
 | 254 | 	uint8_t soc; | 
 | 255 | 	union power_supply_propval full; | 
 | 256 | 	int ret; | 
 | 257 |  | 
 | 258 | 	ret = olpc_ec_cmd(EC_BAT_SOC, NULL, 0, &soc, 1); | 
 | 259 | 	if (ret) | 
 | 260 | 		return ret; | 
 | 261 |  | 
 | 262 | 	ret = olpc_bat_get_charge_full_design(&full); | 
 | 263 | 	if (ret) | 
 | 264 | 		return ret; | 
 | 265 |  | 
 | 266 | 	val->intval = soc * (full.intval / 100); | 
 | 267 | 	return 0; | 
 | 268 | } | 
 | 269 |  | 
| David Woodhouse | fb97287 | 2007-05-04 00:51:18 +0400 | [diff] [blame] | 270 | /********************************************************************* | 
 | 271 |  *		Battery properties | 
 | 272 |  *********************************************************************/ | 
 | 273 | static int olpc_bat_get_property(struct power_supply *psy, | 
 | 274 | 				 enum power_supply_property psp, | 
 | 275 | 				 union power_supply_propval *val) | 
 | 276 | { | 
 | 277 | 	int ret = 0; | 
| Harvey Harrison | 8e9c771 | 2008-10-15 22:01:23 -0700 | [diff] [blame] | 278 | 	__be16 ec_word; | 
| David Woodhouse | fb97287 | 2007-05-04 00:51:18 +0400 | [diff] [blame] | 279 | 	uint8_t ec_byte; | 
| Harvey Harrison | 8e9c771 | 2008-10-15 22:01:23 -0700 | [diff] [blame] | 280 | 	__be64 ser_buf; | 
| David Woodhouse | fb97287 | 2007-05-04 00:51:18 +0400 | [diff] [blame] | 281 |  | 
 | 282 | 	ret = olpc_ec_cmd(EC_BAT_STATUS, NULL, 0, &ec_byte, 1); | 
 | 283 | 	if (ret) | 
 | 284 | 		return ret; | 
 | 285 |  | 
 | 286 | 	/* Theoretically there's a race here -- the battery could be | 
 | 287 | 	   removed immediately after we check whether it's present, and | 
 | 288 | 	   then we query for some other property of the now-absent battery. | 
 | 289 | 	   It doesn't matter though -- the EC will return the last-known | 
 | 290 | 	   information, and it's as if we just ran that _little_ bit faster | 
 | 291 | 	   and managed to read it out before the battery went away. */ | 
| Andres Salomon | 8f7e579 | 2009-06-30 02:16:17 -0400 | [diff] [blame] | 292 | 	if (!(ec_byte & (BAT_STAT_PRESENT | BAT_STAT_TRICKLE)) && | 
 | 293 | 			psp != POWER_SUPPLY_PROP_PRESENT) | 
| David Woodhouse | fb97287 | 2007-05-04 00:51:18 +0400 | [diff] [blame] | 294 | 		return -ENODEV; | 
 | 295 |  | 
 | 296 | 	switch (psp) { | 
 | 297 | 	case POWER_SUPPLY_PROP_STATUS: | 
| Andres Salomon | b2bd8a3 | 2008-05-02 13:41:59 -0700 | [diff] [blame] | 298 | 		ret = olpc_bat_get_status(val, ec_byte); | 
 | 299 | 		if (ret) | 
 | 300 | 			return ret; | 
 | 301 | 		break; | 
| Andres Salomon | ee8076e | 2009-07-02 09:45:18 -0400 | [diff] [blame] | 302 | 	case POWER_SUPPLY_PROP_CHARGE_TYPE: | 
 | 303 | 		if (ec_byte & BAT_STAT_TRICKLE) | 
 | 304 | 			val->intval = POWER_SUPPLY_CHARGE_TYPE_TRICKLE; | 
 | 305 | 		else if (ec_byte & BAT_STAT_CHARGING) | 
 | 306 | 			val->intval = POWER_SUPPLY_CHARGE_TYPE_FAST; | 
 | 307 | 		else | 
 | 308 | 			val->intval = POWER_SUPPLY_CHARGE_TYPE_NONE; | 
 | 309 | 		break; | 
| David Woodhouse | fb97287 | 2007-05-04 00:51:18 +0400 | [diff] [blame] | 310 | 	case POWER_SUPPLY_PROP_PRESENT: | 
| Andres Salomon | 8f7e579 | 2009-06-30 02:16:17 -0400 | [diff] [blame] | 311 | 		val->intval = !!(ec_byte & (BAT_STAT_PRESENT | | 
 | 312 | 					    BAT_STAT_TRICKLE)); | 
| David Woodhouse | fb97287 | 2007-05-04 00:51:18 +0400 | [diff] [blame] | 313 | 		break; | 
 | 314 |  | 
 | 315 | 	case POWER_SUPPLY_PROP_HEALTH: | 
 | 316 | 		if (ec_byte & BAT_STAT_DESTROY) | 
 | 317 | 			val->intval = POWER_SUPPLY_HEALTH_DEAD; | 
 | 318 | 		else { | 
| Andres Salomon | b2bd8a3 | 2008-05-02 13:41:59 -0700 | [diff] [blame] | 319 | 			ret = olpc_bat_get_health(val); | 
| David Woodhouse | fb97287 | 2007-05-04 00:51:18 +0400 | [diff] [blame] | 320 | 			if (ret) | 
 | 321 | 				return ret; | 
| David Woodhouse | fb97287 | 2007-05-04 00:51:18 +0400 | [diff] [blame] | 322 | 		} | 
 | 323 | 		break; | 
 | 324 |  | 
 | 325 | 	case POWER_SUPPLY_PROP_MANUFACTURER: | 
| Andres Salomon | b2bd8a3 | 2008-05-02 13:41:59 -0700 | [diff] [blame] | 326 | 		ret = olpc_bat_get_mfr(val); | 
| David Woodhouse | fb97287 | 2007-05-04 00:51:18 +0400 | [diff] [blame] | 327 | 		if (ret) | 
 | 328 | 			return ret; | 
| David Woodhouse | fb97287 | 2007-05-04 00:51:18 +0400 | [diff] [blame] | 329 | 		break; | 
 | 330 | 	case POWER_SUPPLY_PROP_TECHNOLOGY: | 
| Andres Salomon | b2bd8a3 | 2008-05-02 13:41:59 -0700 | [diff] [blame] | 331 | 		ret = olpc_bat_get_tech(val); | 
| David Woodhouse | fb97287 | 2007-05-04 00:51:18 +0400 | [diff] [blame] | 332 | 		if (ret) | 
 | 333 | 			return ret; | 
| David Woodhouse | fb97287 | 2007-05-04 00:51:18 +0400 | [diff] [blame] | 334 | 		break; | 
 | 335 | 	case POWER_SUPPLY_PROP_VOLTAGE_AVG: | 
| Sascha Silbe | 22fadd7 | 2010-12-10 23:05:21 +0100 | [diff] [blame] | 336 | 	case POWER_SUPPLY_PROP_VOLTAGE_NOW: | 
| David Woodhouse | fb97287 | 2007-05-04 00:51:18 +0400 | [diff] [blame] | 337 | 		ret = olpc_ec_cmd(EC_BAT_VOLTAGE, NULL, 0, (void *)&ec_word, 2); | 
 | 338 | 		if (ret) | 
 | 339 | 			return ret; | 
 | 340 |  | 
| Richard A. Smith | 7cfbb29 | 2010-09-25 19:19:26 +0100 | [diff] [blame] | 341 | 		val->intval = (s16)be16_to_cpu(ec_word) * 9760L / 32; | 
| David Woodhouse | fb97287 | 2007-05-04 00:51:18 +0400 | [diff] [blame] | 342 | 		break; | 
 | 343 | 	case POWER_SUPPLY_PROP_CURRENT_AVG: | 
| Sascha Silbe | 22fadd7 | 2010-12-10 23:05:21 +0100 | [diff] [blame] | 344 | 	case POWER_SUPPLY_PROP_CURRENT_NOW: | 
| David Woodhouse | fb97287 | 2007-05-04 00:51:18 +0400 | [diff] [blame] | 345 | 		ret = olpc_ec_cmd(EC_BAT_CURRENT, NULL, 0, (void *)&ec_word, 2); | 
 | 346 | 		if (ret) | 
 | 347 | 			return ret; | 
 | 348 |  | 
| Richard A. Smith | 7cfbb29 | 2010-09-25 19:19:26 +0100 | [diff] [blame] | 349 | 		val->intval = (s16)be16_to_cpu(ec_word) * 15625L / 120; | 
| David Woodhouse | fb97287 | 2007-05-04 00:51:18 +0400 | [diff] [blame] | 350 | 		break; | 
 | 351 | 	case POWER_SUPPLY_PROP_CAPACITY: | 
 | 352 | 		ret = olpc_ec_cmd(EC_BAT_SOC, NULL, 0, &ec_byte, 1); | 
 | 353 | 		if (ret) | 
 | 354 | 			return ret; | 
 | 355 | 		val->intval = ec_byte; | 
 | 356 | 		break; | 
| Andres Salomon | b294a29 | 2009-06-30 02:13:01 -0400 | [diff] [blame] | 357 | 	case POWER_SUPPLY_PROP_CAPACITY_LEVEL: | 
 | 358 | 		if (ec_byte & BAT_STAT_FULL) | 
 | 359 | 			val->intval = POWER_SUPPLY_CAPACITY_LEVEL_FULL; | 
 | 360 | 		else if (ec_byte & BAT_STAT_LOW) | 
 | 361 | 			val->intval = POWER_SUPPLY_CAPACITY_LEVEL_LOW; | 
 | 362 | 		else | 
 | 363 | 			val->intval = POWER_SUPPLY_CAPACITY_LEVEL_NORMAL; | 
 | 364 | 		break; | 
| Sascha Silbe | b202a5e | 2010-12-10 23:05:19 +0100 | [diff] [blame] | 365 | 	case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN: | 
 | 366 | 		ret = olpc_bat_get_charge_full_design(val); | 
 | 367 | 		if (ret) | 
 | 368 | 			return ret; | 
 | 369 | 		break; | 
| Sascha Silbe | 20fd983 | 2010-12-10 23:05:20 +0100 | [diff] [blame] | 370 | 	case POWER_SUPPLY_PROP_CHARGE_NOW: | 
 | 371 | 		ret = olpc_bat_get_charge_now(val); | 
 | 372 | 		if (ret) | 
 | 373 | 			return ret; | 
 | 374 | 		break; | 
| David Woodhouse | fb97287 | 2007-05-04 00:51:18 +0400 | [diff] [blame] | 375 | 	case POWER_SUPPLY_PROP_TEMP: | 
 | 376 | 		ret = olpc_ec_cmd(EC_BAT_TEMP, NULL, 0, (void *)&ec_word, 2); | 
 | 377 | 		if (ret) | 
 | 378 | 			return ret; | 
| Harvey Harrison | 8e9c771 | 2008-10-15 22:01:23 -0700 | [diff] [blame] | 379 |  | 
| Richard A. Smith | 7cfbb29 | 2010-09-25 19:19:26 +0100 | [diff] [blame] | 380 | 		val->intval = (s16)be16_to_cpu(ec_word) * 100 / 256; | 
| David Woodhouse | fb97287 | 2007-05-04 00:51:18 +0400 | [diff] [blame] | 381 | 		break; | 
 | 382 | 	case POWER_SUPPLY_PROP_TEMP_AMBIENT: | 
 | 383 | 		ret = olpc_ec_cmd(EC_AMB_TEMP, NULL, 0, (void *)&ec_word, 2); | 
 | 384 | 		if (ret) | 
 | 385 | 			return ret; | 
 | 386 |  | 
| Harvey Harrison | 8e9c771 | 2008-10-15 22:01:23 -0700 | [diff] [blame] | 387 | 		val->intval = (int)be16_to_cpu(ec_word) * 100 / 256; | 
| David Woodhouse | fb97287 | 2007-05-04 00:51:18 +0400 | [diff] [blame] | 388 | 		break; | 
| Andres Salomon | 8e552c3 | 2008-05-12 21:46:29 -0400 | [diff] [blame] | 389 | 	case POWER_SUPPLY_PROP_CHARGE_COUNTER: | 
 | 390 | 		ret = olpc_ec_cmd(EC_BAT_ACR, NULL, 0, (void *)&ec_word, 2); | 
 | 391 | 		if (ret) | 
 | 392 | 			return ret; | 
 | 393 |  | 
| Richard A. Smith | 7cfbb29 | 2010-09-25 19:19:26 +0100 | [diff] [blame] | 394 | 		val->intval = (s16)be16_to_cpu(ec_word) * 6250 / 15; | 
| Andres Salomon | 8e552c3 | 2008-05-12 21:46:29 -0400 | [diff] [blame] | 395 | 		break; | 
| David Woodhouse | 1ca5b9d | 2008-05-04 01:31:42 -0400 | [diff] [blame] | 396 | 	case POWER_SUPPLY_PROP_SERIAL_NUMBER: | 
 | 397 | 		ret = olpc_ec_cmd(EC_BAT_SERIAL, NULL, 0, (void *)&ser_buf, 8); | 
 | 398 | 		if (ret) | 
 | 399 | 			return ret; | 
 | 400 |  | 
 | 401 | 		sprintf(bat_serial, "%016llx", (long long)be64_to_cpu(ser_buf)); | 
 | 402 | 		val->strval = bat_serial; | 
 | 403 | 		break; | 
| David Woodhouse | fb97287 | 2007-05-04 00:51:18 +0400 | [diff] [blame] | 404 | 	default: | 
 | 405 | 		ret = -EINVAL; | 
 | 406 | 		break; | 
 | 407 | 	} | 
 | 408 |  | 
 | 409 | 	return ret; | 
 | 410 | } | 
 | 411 |  | 
| Daniel Drake | c566d29 | 2010-12-29 19:12:01 +0000 | [diff] [blame] | 412 | static enum power_supply_property olpc_xo1_bat_props[] = { | 
| David Woodhouse | fb97287 | 2007-05-04 00:51:18 +0400 | [diff] [blame] | 413 | 	POWER_SUPPLY_PROP_STATUS, | 
| Andres Salomon | ee8076e | 2009-07-02 09:45:18 -0400 | [diff] [blame] | 414 | 	POWER_SUPPLY_PROP_CHARGE_TYPE, | 
| David Woodhouse | fb97287 | 2007-05-04 00:51:18 +0400 | [diff] [blame] | 415 | 	POWER_SUPPLY_PROP_PRESENT, | 
 | 416 | 	POWER_SUPPLY_PROP_HEALTH, | 
 | 417 | 	POWER_SUPPLY_PROP_TECHNOLOGY, | 
 | 418 | 	POWER_SUPPLY_PROP_VOLTAGE_AVG, | 
| Sascha Silbe | 22fadd7 | 2010-12-10 23:05:21 +0100 | [diff] [blame] | 419 | 	POWER_SUPPLY_PROP_VOLTAGE_NOW, | 
| David Woodhouse | fb97287 | 2007-05-04 00:51:18 +0400 | [diff] [blame] | 420 | 	POWER_SUPPLY_PROP_CURRENT_AVG, | 
| Sascha Silbe | 22fadd7 | 2010-12-10 23:05:21 +0100 | [diff] [blame] | 421 | 	POWER_SUPPLY_PROP_CURRENT_NOW, | 
| David Woodhouse | fb97287 | 2007-05-04 00:51:18 +0400 | [diff] [blame] | 422 | 	POWER_SUPPLY_PROP_CAPACITY, | 
| Andres Salomon | b294a29 | 2009-06-30 02:13:01 -0400 | [diff] [blame] | 423 | 	POWER_SUPPLY_PROP_CAPACITY_LEVEL, | 
| Sascha Silbe | b202a5e | 2010-12-10 23:05:19 +0100 | [diff] [blame] | 424 | 	POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN, | 
| Sascha Silbe | 20fd983 | 2010-12-10 23:05:20 +0100 | [diff] [blame] | 425 | 	POWER_SUPPLY_PROP_CHARGE_NOW, | 
| David Woodhouse | fb97287 | 2007-05-04 00:51:18 +0400 | [diff] [blame] | 426 | 	POWER_SUPPLY_PROP_TEMP, | 
 | 427 | 	POWER_SUPPLY_PROP_TEMP_AMBIENT, | 
 | 428 | 	POWER_SUPPLY_PROP_MANUFACTURER, | 
| David Woodhouse | 1ca5b9d | 2008-05-04 01:31:42 -0400 | [diff] [blame] | 429 | 	POWER_SUPPLY_PROP_SERIAL_NUMBER, | 
| Andres Salomon | 8e552c3 | 2008-05-12 21:46:29 -0400 | [diff] [blame] | 430 | 	POWER_SUPPLY_PROP_CHARGE_COUNTER, | 
| David Woodhouse | fb97287 | 2007-05-04 00:51:18 +0400 | [diff] [blame] | 431 | }; | 
 | 432 |  | 
| Daniel Drake | c566d29 | 2010-12-29 19:12:01 +0000 | [diff] [blame] | 433 | /* XO-1.5 does not have ambient temperature property */ | 
 | 434 | static enum power_supply_property olpc_xo15_bat_props[] = { | 
 | 435 | 	POWER_SUPPLY_PROP_STATUS, | 
 | 436 | 	POWER_SUPPLY_PROP_CHARGE_TYPE, | 
 | 437 | 	POWER_SUPPLY_PROP_PRESENT, | 
 | 438 | 	POWER_SUPPLY_PROP_HEALTH, | 
 | 439 | 	POWER_SUPPLY_PROP_TECHNOLOGY, | 
 | 440 | 	POWER_SUPPLY_PROP_VOLTAGE_AVG, | 
| Sascha Silbe | bf542a4 | 2011-01-12 23:23:23 +0100 | [diff] [blame] | 441 | 	POWER_SUPPLY_PROP_VOLTAGE_NOW, | 
| Daniel Drake | c566d29 | 2010-12-29 19:12:01 +0000 | [diff] [blame] | 442 | 	POWER_SUPPLY_PROP_CURRENT_AVG, | 
| Sascha Silbe | bf542a4 | 2011-01-12 23:23:23 +0100 | [diff] [blame] | 443 | 	POWER_SUPPLY_PROP_CURRENT_NOW, | 
| Daniel Drake | c566d29 | 2010-12-29 19:12:01 +0000 | [diff] [blame] | 444 | 	POWER_SUPPLY_PROP_CAPACITY, | 
 | 445 | 	POWER_SUPPLY_PROP_CAPACITY_LEVEL, | 
| Sascha Silbe | bf542a4 | 2011-01-12 23:23:23 +0100 | [diff] [blame] | 446 | 	POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN, | 
 | 447 | 	POWER_SUPPLY_PROP_CHARGE_NOW, | 
| Daniel Drake | c566d29 | 2010-12-29 19:12:01 +0000 | [diff] [blame] | 448 | 	POWER_SUPPLY_PROP_TEMP, | 
 | 449 | 	POWER_SUPPLY_PROP_MANUFACTURER, | 
 | 450 | 	POWER_SUPPLY_PROP_SERIAL_NUMBER, | 
 | 451 | 	POWER_SUPPLY_PROP_CHARGE_COUNTER, | 
 | 452 | }; | 
 | 453 |  | 
| Andres Salomon | d7eb9e3 | 2008-05-02 13:41:58 -0700 | [diff] [blame] | 454 | /* EEPROM reading goes completely around the power_supply API, sadly */ | 
 | 455 |  | 
 | 456 | #define EEPROM_START	0x20 | 
 | 457 | #define EEPROM_END	0x80 | 
 | 458 | #define EEPROM_SIZE	(EEPROM_END - EEPROM_START) | 
 | 459 |  | 
| Chris Wright | 2c3c8be | 2010-05-12 18:28:57 -0700 | [diff] [blame] | 460 | static ssize_t olpc_bat_eeprom_read(struct file *filp, struct kobject *kobj, | 
| Andres Salomon | d7eb9e3 | 2008-05-02 13:41:58 -0700 | [diff] [blame] | 461 | 		struct bin_attribute *attr, char *buf, loff_t off, size_t count) | 
 | 462 | { | 
 | 463 | 	uint8_t ec_byte; | 
| Andres Salomon | 04a820e | 2009-06-30 02:14:00 -0400 | [diff] [blame] | 464 | 	int ret; | 
 | 465 | 	int i; | 
| Andres Salomon | d7eb9e3 | 2008-05-02 13:41:58 -0700 | [diff] [blame] | 466 |  | 
 | 467 | 	if (off >= EEPROM_SIZE) | 
 | 468 | 		return 0; | 
 | 469 | 	if (off + count > EEPROM_SIZE) | 
 | 470 | 		count = EEPROM_SIZE - off; | 
 | 471 |  | 
| Andres Salomon | 04a820e | 2009-06-30 02:14:00 -0400 | [diff] [blame] | 472 | 	for (i = 0; i < count; i++) { | 
 | 473 | 		ec_byte = EEPROM_START + off + i; | 
 | 474 | 		ret = olpc_ec_cmd(EC_BAT_EEPROM, &ec_byte, 1, &buf[i], 1); | 
| Andres Salomon | d7eb9e3 | 2008-05-02 13:41:58 -0700 | [diff] [blame] | 475 | 		if (ret) { | 
| Andres Salomon | 04a820e | 2009-06-30 02:14:00 -0400 | [diff] [blame] | 476 | 			pr_err("olpc-battery: " | 
 | 477 | 			       "EC_BAT_EEPROM cmd @ 0x%x failed - %d!\n", | 
 | 478 | 			       ec_byte, ret); | 
| Andres Salomon | d7eb9e3 | 2008-05-02 13:41:58 -0700 | [diff] [blame] | 479 | 			return -EIO; | 
 | 480 | 		} | 
 | 481 | 	} | 
 | 482 |  | 
 | 483 | 	return count; | 
 | 484 | } | 
 | 485 |  | 
 | 486 | static struct bin_attribute olpc_bat_eeprom = { | 
 | 487 | 	.attr = { | 
 | 488 | 		.name = "eeprom", | 
 | 489 | 		.mode = S_IRUGO, | 
| Andres Salomon | d7eb9e3 | 2008-05-02 13:41:58 -0700 | [diff] [blame] | 490 | 	}, | 
 | 491 | 	.size = 0, | 
 | 492 | 	.read = olpc_bat_eeprom_read, | 
 | 493 | }; | 
 | 494 |  | 
| Andres Salomon | 144bbea | 2009-06-30 02:15:26 -0400 | [diff] [blame] | 495 | /* Allow userspace to see the specific error value pulled from the EC */ | 
 | 496 |  | 
 | 497 | static ssize_t olpc_bat_error_read(struct device *dev, | 
 | 498 | 		struct device_attribute *attr, char *buf) | 
 | 499 | { | 
 | 500 | 	uint8_t ec_byte; | 
 | 501 | 	ssize_t ret; | 
 | 502 |  | 
 | 503 | 	ret = olpc_ec_cmd(EC_BAT_ERRCODE, NULL, 0, &ec_byte, 1); | 
 | 504 | 	if (ret < 0) | 
 | 505 | 		return ret; | 
 | 506 |  | 
 | 507 | 	return sprintf(buf, "%d\n", ec_byte); | 
 | 508 | } | 
 | 509 |  | 
 | 510 | static struct device_attribute olpc_bat_error = { | 
 | 511 | 	.attr = { | 
 | 512 | 		.name = "error", | 
 | 513 | 		.mode = S_IRUGO, | 
 | 514 | 	}, | 
 | 515 | 	.show = olpc_bat_error_read, | 
 | 516 | }; | 
 | 517 |  | 
| David Woodhouse | fb97287 | 2007-05-04 00:51:18 +0400 | [diff] [blame] | 518 | /********************************************************************* | 
 | 519 |  *		Initialisation | 
 | 520 |  *********************************************************************/ | 
 | 521 |  | 
 | 522 | static struct platform_device *bat_pdev; | 
 | 523 |  | 
 | 524 | static struct power_supply olpc_bat = { | 
| David Woodhouse | fb97287 | 2007-05-04 00:51:18 +0400 | [diff] [blame] | 525 | 	.get_property = olpc_bat_get_property, | 
 | 526 | 	.use_for_apm = 1, | 
 | 527 | }; | 
 | 528 |  | 
 | 529 | void olpc_battery_trigger_uevent(unsigned long cause) | 
 | 530 | { | 
 | 531 | 	if (cause & EC_SCI_SRC_ACPWR) | 
 | 532 | 		kobject_uevent(&olpc_ac.dev->kobj, KOBJ_CHANGE); | 
 | 533 | 	if (cause & (EC_SCI_SRC_BATERR|EC_SCI_SRC_BATSOC|EC_SCI_SRC_BATTERY)) | 
 | 534 | 		kobject_uevent(&olpc_bat.dev->kobj, KOBJ_CHANGE); | 
 | 535 | } | 
 | 536 |  | 
 | 537 | static int __init olpc_bat_init(void) | 
 | 538 | { | 
 | 539 | 	int ret = 0; | 
 | 540 | 	uint8_t status; | 
 | 541 |  | 
 | 542 | 	if (!olpc_platform_info.ecver) | 
 | 543 | 		return -ENXIO; | 
| Andres Salomon | 484d6d5 | 2008-05-02 13:41:59 -0700 | [diff] [blame] | 544 |  | 
 | 545 | 	/* | 
 | 546 | 	 * We've seen a number of EC protocol changes; this driver requires | 
 | 547 | 	 * the latest EC protocol, supported by 0x44 and above. | 
 | 548 | 	 */ | 
 | 549 | 	if (olpc_platform_info.ecver < 0x44) { | 
 | 550 | 		printk(KERN_NOTICE "OLPC EC version 0x%02x too old for " | 
 | 551 | 			"battery driver.\n", olpc_platform_info.ecver); | 
| David Woodhouse | fb97287 | 2007-05-04 00:51:18 +0400 | [diff] [blame] | 552 | 		return -ENXIO; | 
 | 553 | 	} | 
 | 554 |  | 
 | 555 | 	ret = olpc_ec_cmd(EC_BAT_STATUS, NULL, 0, &status, 1); | 
 | 556 | 	if (ret) | 
 | 557 | 		return ret; | 
 | 558 |  | 
 | 559 | 	/* Ignore the status. It doesn't actually matter */ | 
 | 560 |  | 
 | 561 | 	bat_pdev = platform_device_register_simple("olpc-battery", 0, NULL, 0); | 
 | 562 | 	if (IS_ERR(bat_pdev)) | 
 | 563 | 		return PTR_ERR(bat_pdev); | 
 | 564 |  | 
 | 565 | 	ret = power_supply_register(&bat_pdev->dev, &olpc_ac); | 
 | 566 | 	if (ret) | 
 | 567 | 		goto ac_failed; | 
 | 568 |  | 
 | 569 | 	olpc_bat.name = bat_pdev->name; | 
| Daniel Drake | c566d29 | 2010-12-29 19:12:01 +0000 | [diff] [blame] | 570 | 	if (olpc_board_at_least(olpc_board_pre(0xd0))) { /* XO-1.5 */ | 
 | 571 | 		olpc_bat.properties = olpc_xo15_bat_props; | 
 | 572 | 		olpc_bat.num_properties = ARRAY_SIZE(olpc_xo15_bat_props); | 
 | 573 | 	} else { /* XO-1 */ | 
 | 574 | 		olpc_bat.properties = olpc_xo1_bat_props; | 
 | 575 | 		olpc_bat.num_properties = ARRAY_SIZE(olpc_xo1_bat_props); | 
 | 576 | 	} | 
| David Woodhouse | fb97287 | 2007-05-04 00:51:18 +0400 | [diff] [blame] | 577 |  | 
 | 578 | 	ret = power_supply_register(&bat_pdev->dev, &olpc_bat); | 
 | 579 | 	if (ret) | 
 | 580 | 		goto battery_failed; | 
 | 581 |  | 
| Andres Salomon | d7eb9e3 | 2008-05-02 13:41:58 -0700 | [diff] [blame] | 582 | 	ret = device_create_bin_file(olpc_bat.dev, &olpc_bat_eeprom); | 
 | 583 | 	if (ret) | 
 | 584 | 		goto eeprom_failed; | 
 | 585 |  | 
| Andres Salomon | 144bbea | 2009-06-30 02:15:26 -0400 | [diff] [blame] | 586 | 	ret = device_create_file(olpc_bat.dev, &olpc_bat_error); | 
 | 587 | 	if (ret) | 
 | 588 | 		goto error_failed; | 
 | 589 |  | 
| David Woodhouse | fb97287 | 2007-05-04 00:51:18 +0400 | [diff] [blame] | 590 | 	goto success; | 
 | 591 |  | 
| Andres Salomon | 144bbea | 2009-06-30 02:15:26 -0400 | [diff] [blame] | 592 | error_failed: | 
 | 593 | 	device_remove_bin_file(olpc_bat.dev, &olpc_bat_eeprom); | 
| Andres Salomon | d7eb9e3 | 2008-05-02 13:41:58 -0700 | [diff] [blame] | 594 | eeprom_failed: | 
 | 595 | 	power_supply_unregister(&olpc_bat); | 
| David Woodhouse | fb97287 | 2007-05-04 00:51:18 +0400 | [diff] [blame] | 596 | battery_failed: | 
 | 597 | 	power_supply_unregister(&olpc_ac); | 
 | 598 | ac_failed: | 
 | 599 | 	platform_device_unregister(bat_pdev); | 
 | 600 | success: | 
 | 601 | 	return ret; | 
 | 602 | } | 
 | 603 |  | 
 | 604 | static void __exit olpc_bat_exit(void) | 
 | 605 | { | 
| Andres Salomon | 144bbea | 2009-06-30 02:15:26 -0400 | [diff] [blame] | 606 | 	device_remove_file(olpc_bat.dev, &olpc_bat_error); | 
| Andres Salomon | d7eb9e3 | 2008-05-02 13:41:58 -0700 | [diff] [blame] | 607 | 	device_remove_bin_file(olpc_bat.dev, &olpc_bat_eeprom); | 
| David Woodhouse | fb97287 | 2007-05-04 00:51:18 +0400 | [diff] [blame] | 608 | 	power_supply_unregister(&olpc_bat); | 
 | 609 | 	power_supply_unregister(&olpc_ac); | 
 | 610 | 	platform_device_unregister(bat_pdev); | 
| David Woodhouse | fb97287 | 2007-05-04 00:51:18 +0400 | [diff] [blame] | 611 | } | 
 | 612 |  | 
 | 613 | module_init(olpc_bat_init); | 
 | 614 | module_exit(olpc_bat_exit); | 
 | 615 |  | 
 | 616 | MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org>"); | 
 | 617 | MODULE_LICENSE("GPL"); | 
 | 618 | MODULE_DESCRIPTION("Battery driver for One Laptop Per Child 'XO' machine"); |