1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Battery driver for One Laptop Per Child board.
4  *
5  *	Copyright © 2006-2010  David Woodhouse <dwmw2@infradead.org>
6  */
7 
8 #include <linux/kernel.h>
9 #include <linux/module.h>
10 #include <linux/mod_devicetable.h>
11 #include <linux/types.h>
12 #include <linux/err.h>
13 #include <linux/device.h>
14 #include <linux/of.h>
15 #include <linux/platform_device.h>
16 #include <linux/power_supply.h>
17 #include <linux/jiffies.h>
18 #include <linux/sched.h>
19 #include <linux/olpc-ec.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   */
25 #define EC_BAT_ACR	0x12	/* int16_t,	*6250/15,    µAh  */
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
41 #define BAT_STAT_TRICKLE	0x80
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 struct olpc_battery_data {
54 	struct power_supply *olpc_ac;
55 	struct power_supply *olpc_bat;
56 	char bat_serial[17];
57 	bool new_proto;
58 	bool little_endian;
59 };
60 
61 /*********************************************************************
62  *		Power
63  *********************************************************************/
64 
65 static int olpc_ac_get_prop(struct power_supply *psy,
66 			    enum power_supply_property psp,
67 			    union power_supply_propval *val)
68 {
69 	int ret = 0;
70 	uint8_t status;
71 
72 	switch (psp) {
73 	case POWER_SUPPLY_PROP_ONLINE:
74 		ret = olpc_ec_cmd(EC_BAT_STATUS, NULL, 0, &status, 1);
75 		if (ret)
76 			return ret;
77 
78 		val->intval = !!(status & BAT_STAT_AC);
79 		break;
80 	default:
81 		ret = -EINVAL;
82 		break;
83 	}
84 	return ret;
85 }
86 
87 static enum power_supply_property olpc_ac_props[] = {
88 	POWER_SUPPLY_PROP_ONLINE,
89 };
90 
91 static const struct power_supply_desc olpc_ac_desc = {
92 	.name = "olpc-ac",
93 	.type = POWER_SUPPLY_TYPE_MAINS,
94 	.properties = olpc_ac_props,
95 	.num_properties = ARRAY_SIZE(olpc_ac_props),
96 	.get_property = olpc_ac_get_prop,
97 };
98 
99 static int olpc_bat_get_status(struct olpc_battery_data *data,
100 		union power_supply_propval *val, uint8_t ec_byte)
101 {
102 	if (data->new_proto) {
103 		if (ec_byte & (BAT_STAT_CHARGING | BAT_STAT_TRICKLE))
104 			val->intval = POWER_SUPPLY_STATUS_CHARGING;
105 		else if (ec_byte & BAT_STAT_DISCHARGING)
106 			val->intval = POWER_SUPPLY_STATUS_DISCHARGING;
107 		else if (ec_byte & BAT_STAT_FULL)
108 			val->intval = POWER_SUPPLY_STATUS_FULL;
109 		else /* er,... */
110 			val->intval = POWER_SUPPLY_STATUS_NOT_CHARGING;
111 	} else {
112 		/* Older EC didn't report charge/discharge bits */
113 		if (!(ec_byte & BAT_STAT_AC)) /* No AC means discharging */
114 			val->intval = POWER_SUPPLY_STATUS_DISCHARGING;
115 		else if (ec_byte & BAT_STAT_FULL)
116 			val->intval = POWER_SUPPLY_STATUS_FULL;
117 		else /* Not _necessarily_ true but EC doesn't tell all yet */
118 			val->intval = POWER_SUPPLY_STATUS_CHARGING;
119 	}
120 
121 	return 0;
122 }
123 
124 static int olpc_bat_get_health(union power_supply_propval *val)
125 {
126 	uint8_t ec_byte;
127 	int ret;
128 
129 	ret = olpc_ec_cmd(EC_BAT_ERRCODE, NULL, 0, &ec_byte, 1);
130 	if (ret)
131 		return ret;
132 
133 	switch (ec_byte) {
134 	case 0:
135 		val->intval = POWER_SUPPLY_HEALTH_GOOD;
136 		break;
137 
138 	case BAT_ERR_OVERTEMP:
139 		val->intval = POWER_SUPPLY_HEALTH_OVERHEAT;
140 		break;
141 
142 	case BAT_ERR_OVERVOLTAGE:
143 		val->intval = POWER_SUPPLY_HEALTH_OVERVOLTAGE;
144 		break;
145 
146 	case BAT_ERR_INFOFAIL:
147 	case BAT_ERR_OUT_OF_CONTROL:
148 	case BAT_ERR_ID_FAIL:
149 	case BAT_ERR_ACR_FAIL:
150 		val->intval = POWER_SUPPLY_HEALTH_UNSPEC_FAILURE;
151 		break;
152 
153 	default:
154 		/* Eep. We don't know this failure code */
155 		ret = -EIO;
156 	}
157 
158 	return ret;
159 }
160 
161 static int olpc_bat_get_mfr(union power_supply_propval *val)
162 {
163 	uint8_t ec_byte;
164 	int ret;
165 
166 	ec_byte = BAT_ADDR_MFR_TYPE;
167 	ret = olpc_ec_cmd(EC_BAT_EEPROM, &ec_byte, 1, &ec_byte, 1);
168 	if (ret)
169 		return ret;
170 
171 	switch (ec_byte >> 4) {
172 	case 1:
173 		val->strval = "Gold Peak";
174 		break;
175 	case 2:
176 		val->strval = "BYD";
177 		break;
178 	default:
179 		val->strval = "Unknown";
180 		break;
181 	}
182 
183 	return ret;
184 }
185 
186 static int olpc_bat_get_tech(union power_supply_propval *val)
187 {
188 	uint8_t ec_byte;
189 	int ret;
190 
191 	ec_byte = BAT_ADDR_MFR_TYPE;
192 	ret = olpc_ec_cmd(EC_BAT_EEPROM, &ec_byte, 1, &ec_byte, 1);
193 	if (ret)
194 		return ret;
195 
196 	switch (ec_byte & 0xf) {
197 	case 1:
198 		val->intval = POWER_SUPPLY_TECHNOLOGY_NiMH;
199 		break;
200 	case 2:
201 		val->intval = POWER_SUPPLY_TECHNOLOGY_LiFe;
202 		break;
203 	default:
204 		val->intval = POWER_SUPPLY_TECHNOLOGY_UNKNOWN;
205 		break;
206 	}
207 
208 	return ret;
209 }
210 
211 static int olpc_bat_get_charge_full_design(union power_supply_propval *val)
212 {
213 	uint8_t ec_byte;
214 	union power_supply_propval tech;
215 	int ret, mfr;
216 
217 	ret = olpc_bat_get_tech(&tech);
218 	if (ret)
219 		return ret;
220 
221 	ec_byte = BAT_ADDR_MFR_TYPE;
222 	ret = olpc_ec_cmd(EC_BAT_EEPROM, &ec_byte, 1, &ec_byte, 1);
223 	if (ret)
224 		return ret;
225 
226 	mfr = ec_byte >> 4;
227 
228 	switch (tech.intval) {
229 	case POWER_SUPPLY_TECHNOLOGY_NiMH:
230 		switch (mfr) {
231 		case 1: /* Gold Peak */
232 			val->intval = 3000000*.8;
233 			break;
234 		default:
235 			return -EIO;
236 		}
237 		break;
238 
239 	case POWER_SUPPLY_TECHNOLOGY_LiFe:
240 		switch (mfr) {
241 		case 1: /* Gold Peak, fall through */
242 		case 2: /* BYD */
243 			val->intval = 2800000;
244 			break;
245 		default:
246 			return -EIO;
247 		}
248 		break;
249 
250 	default:
251 		return -EIO;
252 	}
253 
254 	return ret;
255 }
256 
257 static int olpc_bat_get_charge_now(union power_supply_propval *val)
258 {
259 	uint8_t soc;
260 	union power_supply_propval full;
261 	int ret;
262 
263 	ret = olpc_ec_cmd(EC_BAT_SOC, NULL, 0, &soc, 1);
264 	if (ret)
265 		return ret;
266 
267 	ret = olpc_bat_get_charge_full_design(&full);
268 	if (ret)
269 		return ret;
270 
271 	val->intval = soc * (full.intval / 100);
272 	return 0;
273 }
274 
275 static int olpc_bat_get_voltage_max_design(union power_supply_propval *val)
276 {
277 	uint8_t ec_byte;
278 	union power_supply_propval tech;
279 	int mfr;
280 	int ret;
281 
282 	ret = olpc_bat_get_tech(&tech);
283 	if (ret)
284 		return ret;
285 
286 	ec_byte = BAT_ADDR_MFR_TYPE;
287 	ret = olpc_ec_cmd(EC_BAT_EEPROM, &ec_byte, 1, &ec_byte, 1);
288 	if (ret)
289 		return ret;
290 
291 	mfr = ec_byte >> 4;
292 
293 	switch (tech.intval) {
294 	case POWER_SUPPLY_TECHNOLOGY_NiMH:
295 		switch (mfr) {
296 		case 1: /* Gold Peak */
297 			val->intval = 6000000;
298 			break;
299 		default:
300 			return -EIO;
301 		}
302 		break;
303 
304 	case POWER_SUPPLY_TECHNOLOGY_LiFe:
305 		switch (mfr) {
306 		case 1: /* Gold Peak */
307 			val->intval = 6400000;
308 			break;
309 		case 2: /* BYD */
310 			val->intval = 6500000;
311 			break;
312 		default:
313 			return -EIO;
314 		}
315 		break;
316 
317 	default:
318 		return -EIO;
319 	}
320 
321 	return ret;
322 }
323 
324 static u16 ecword_to_cpu(struct olpc_battery_data *data, u16 ec_word)
325 {
326 	if (data->little_endian)
327 		return le16_to_cpu((__force __le16)ec_word);
328 	else
329 		return be16_to_cpu((__force __be16)ec_word);
330 }
331 
332 /*********************************************************************
333  *		Battery properties
334  *********************************************************************/
335 static int olpc_bat_get_property(struct power_supply *psy,
336 				 enum power_supply_property psp,
337 				 union power_supply_propval *val)
338 {
339 	struct olpc_battery_data *data = power_supply_get_drvdata(psy);
340 	int ret = 0;
341 	u16 ec_word;
342 	uint8_t ec_byte;
343 	__be64 ser_buf;
344 
345 	ret = olpc_ec_cmd(EC_BAT_STATUS, NULL, 0, &ec_byte, 1);
346 	if (ret)
347 		return ret;
348 
349 	/* Theoretically there's a race here -- the battery could be
350 	   removed immediately after we check whether it's present, and
351 	   then we query for some other property of the now-absent battery.
352 	   It doesn't matter though -- the EC will return the last-known
353 	   information, and it's as if we just ran that _little_ bit faster
354 	   and managed to read it out before the battery went away. */
355 	if (!(ec_byte & (BAT_STAT_PRESENT | BAT_STAT_TRICKLE)) &&
356 			psp != POWER_SUPPLY_PROP_PRESENT)
357 		return -ENODEV;
358 
359 	switch (psp) {
360 	case POWER_SUPPLY_PROP_STATUS:
361 		ret = olpc_bat_get_status(data, val, ec_byte);
362 		if (ret)
363 			return ret;
364 		break;
365 	case POWER_SUPPLY_PROP_CHARGE_TYPE:
366 		if (ec_byte & BAT_STAT_TRICKLE)
367 			val->intval = POWER_SUPPLY_CHARGE_TYPE_TRICKLE;
368 		else if (ec_byte & BAT_STAT_CHARGING)
369 			val->intval = POWER_SUPPLY_CHARGE_TYPE_FAST;
370 		else
371 			val->intval = POWER_SUPPLY_CHARGE_TYPE_NONE;
372 		break;
373 	case POWER_SUPPLY_PROP_PRESENT:
374 		val->intval = !!(ec_byte & (BAT_STAT_PRESENT |
375 					    BAT_STAT_TRICKLE));
376 		break;
377 
378 	case POWER_SUPPLY_PROP_HEALTH:
379 		if (ec_byte & BAT_STAT_DESTROY)
380 			val->intval = POWER_SUPPLY_HEALTH_DEAD;
381 		else {
382 			ret = olpc_bat_get_health(val);
383 			if (ret)
384 				return ret;
385 		}
386 		break;
387 
388 	case POWER_SUPPLY_PROP_MANUFACTURER:
389 		ret = olpc_bat_get_mfr(val);
390 		if (ret)
391 			return ret;
392 		break;
393 	case POWER_SUPPLY_PROP_TECHNOLOGY:
394 		ret = olpc_bat_get_tech(val);
395 		if (ret)
396 			return ret;
397 		break;
398 	case POWER_SUPPLY_PROP_VOLTAGE_AVG:
399 	case POWER_SUPPLY_PROP_VOLTAGE_NOW:
400 		ret = olpc_ec_cmd(EC_BAT_VOLTAGE, NULL, 0, (void *)&ec_word, 2);
401 		if (ret)
402 			return ret;
403 
404 		val->intval = ecword_to_cpu(data, ec_word) * 9760L / 32;
405 		break;
406 	case POWER_SUPPLY_PROP_CURRENT_AVG:
407 	case POWER_SUPPLY_PROP_CURRENT_NOW:
408 		ret = olpc_ec_cmd(EC_BAT_CURRENT, NULL, 0, (void *)&ec_word, 2);
409 		if (ret)
410 			return ret;
411 
412 		val->intval = ecword_to_cpu(data, ec_word) * 15625L / 120;
413 		break;
414 	case POWER_SUPPLY_PROP_CAPACITY:
415 		ret = olpc_ec_cmd(EC_BAT_SOC, NULL, 0, &ec_byte, 1);
416 		if (ret)
417 			return ret;
418 		val->intval = ec_byte;
419 		break;
420 	case POWER_SUPPLY_PROP_CAPACITY_LEVEL:
421 		if (ec_byte & BAT_STAT_FULL)
422 			val->intval = POWER_SUPPLY_CAPACITY_LEVEL_FULL;
423 		else if (ec_byte & BAT_STAT_LOW)
424 			val->intval = POWER_SUPPLY_CAPACITY_LEVEL_LOW;
425 		else
426 			val->intval = POWER_SUPPLY_CAPACITY_LEVEL_NORMAL;
427 		break;
428 	case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN:
429 		ret = olpc_bat_get_charge_full_design(val);
430 		if (ret)
431 			return ret;
432 		break;
433 	case POWER_SUPPLY_PROP_CHARGE_NOW:
434 		ret = olpc_bat_get_charge_now(val);
435 		if (ret)
436 			return ret;
437 		break;
438 	case POWER_SUPPLY_PROP_TEMP:
439 		ret = olpc_ec_cmd(EC_BAT_TEMP, NULL, 0, (void *)&ec_word, 2);
440 		if (ret)
441 			return ret;
442 
443 		val->intval = ecword_to_cpu(data, ec_word) * 10 / 256;
444 		break;
445 	case POWER_SUPPLY_PROP_TEMP_AMBIENT:
446 		ret = olpc_ec_cmd(EC_AMB_TEMP, NULL, 0, (void *)&ec_word, 2);
447 		if (ret)
448 			return ret;
449 
450 		val->intval = (int)ecword_to_cpu(data, ec_word) * 10 / 256;
451 		break;
452 	case POWER_SUPPLY_PROP_CHARGE_COUNTER:
453 		ret = olpc_ec_cmd(EC_BAT_ACR, NULL, 0, (void *)&ec_word, 2);
454 		if (ret)
455 			return ret;
456 
457 		val->intval = ecword_to_cpu(data, ec_word) * 6250 / 15;
458 		break;
459 	case POWER_SUPPLY_PROP_SERIAL_NUMBER:
460 		ret = olpc_ec_cmd(EC_BAT_SERIAL, NULL, 0, (void *)&ser_buf, 8);
461 		if (ret)
462 			return ret;
463 
464 		sprintf(data->bat_serial, "%016llx", (long long)be64_to_cpu(ser_buf));
465 		val->strval = data->bat_serial;
466 		break;
467 	case POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN:
468 		ret = olpc_bat_get_voltage_max_design(val);
469 		if (ret)
470 			return ret;
471 		break;
472 	default:
473 		ret = -EINVAL;
474 		break;
475 	}
476 
477 	return ret;
478 }
479 
480 static enum power_supply_property olpc_xo1_bat_props[] = {
481 	POWER_SUPPLY_PROP_STATUS,
482 	POWER_SUPPLY_PROP_CHARGE_TYPE,
483 	POWER_SUPPLY_PROP_PRESENT,
484 	POWER_SUPPLY_PROP_HEALTH,
485 	POWER_SUPPLY_PROP_TECHNOLOGY,
486 	POWER_SUPPLY_PROP_VOLTAGE_AVG,
487 	POWER_SUPPLY_PROP_VOLTAGE_NOW,
488 	POWER_SUPPLY_PROP_CURRENT_AVG,
489 	POWER_SUPPLY_PROP_CURRENT_NOW,
490 	POWER_SUPPLY_PROP_CAPACITY,
491 	POWER_SUPPLY_PROP_CAPACITY_LEVEL,
492 	POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN,
493 	POWER_SUPPLY_PROP_CHARGE_NOW,
494 	POWER_SUPPLY_PROP_TEMP,
495 	POWER_SUPPLY_PROP_TEMP_AMBIENT,
496 	POWER_SUPPLY_PROP_MANUFACTURER,
497 	POWER_SUPPLY_PROP_SERIAL_NUMBER,
498 	POWER_SUPPLY_PROP_CHARGE_COUNTER,
499 	POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN,
500 };
501 
502 /* XO-1.5 does not have ambient temperature property */
503 static enum power_supply_property olpc_xo15_bat_props[] = {
504 	POWER_SUPPLY_PROP_STATUS,
505 	POWER_SUPPLY_PROP_CHARGE_TYPE,
506 	POWER_SUPPLY_PROP_PRESENT,
507 	POWER_SUPPLY_PROP_HEALTH,
508 	POWER_SUPPLY_PROP_TECHNOLOGY,
509 	POWER_SUPPLY_PROP_VOLTAGE_AVG,
510 	POWER_SUPPLY_PROP_VOLTAGE_NOW,
511 	POWER_SUPPLY_PROP_CURRENT_AVG,
512 	POWER_SUPPLY_PROP_CURRENT_NOW,
513 	POWER_SUPPLY_PROP_CAPACITY,
514 	POWER_SUPPLY_PROP_CAPACITY_LEVEL,
515 	POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN,
516 	POWER_SUPPLY_PROP_CHARGE_NOW,
517 	POWER_SUPPLY_PROP_TEMP,
518 	POWER_SUPPLY_PROP_MANUFACTURER,
519 	POWER_SUPPLY_PROP_SERIAL_NUMBER,
520 	POWER_SUPPLY_PROP_CHARGE_COUNTER,
521 	POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN,
522 };
523 
524 /* EEPROM reading goes completely around the power_supply API, sadly */
525 
526 #define EEPROM_START	0x20
527 #define EEPROM_END	0x80
528 #define EEPROM_SIZE	(EEPROM_END - EEPROM_START)
529 
530 static ssize_t olpc_bat_eeprom_read(struct file *filp, struct kobject *kobj,
531 		struct bin_attribute *attr, char *buf, loff_t off, size_t count)
532 {
533 	uint8_t ec_byte;
534 	int ret;
535 	int i;
536 
537 	for (i = 0; i < count; i++) {
538 		ec_byte = EEPROM_START + off + i;
539 		ret = olpc_ec_cmd(EC_BAT_EEPROM, &ec_byte, 1, &buf[i], 1);
540 		if (ret) {
541 			pr_err("olpc-battery: "
542 			       "EC_BAT_EEPROM cmd @ 0x%x failed - %d!\n",
543 			       ec_byte, ret);
544 			return -EIO;
545 		}
546 	}
547 
548 	return count;
549 }
550 
551 static struct bin_attribute olpc_bat_eeprom = {
552 	.attr = {
553 		.name = "eeprom",
554 		.mode = S_IRUGO,
555 	},
556 	.size = EEPROM_SIZE,
557 	.read = olpc_bat_eeprom_read,
558 };
559 
560 /* Allow userspace to see the specific error value pulled from the EC */
561 
562 static ssize_t olpc_bat_error_read(struct device *dev,
563 		struct device_attribute *attr, char *buf)
564 {
565 	uint8_t ec_byte;
566 	ssize_t ret;
567 
568 	ret = olpc_ec_cmd(EC_BAT_ERRCODE, NULL, 0, &ec_byte, 1);
569 	if (ret < 0)
570 		return ret;
571 
572 	return sprintf(buf, "%d\n", ec_byte);
573 }
574 
575 static struct device_attribute olpc_bat_error = {
576 	.attr = {
577 		.name = "error",
578 		.mode = S_IRUGO,
579 	},
580 	.show = olpc_bat_error_read,
581 };
582 
583 static struct attribute *olpc_bat_sysfs_attrs[] = {
584 	&olpc_bat_error.attr,
585 	NULL
586 };
587 
588 static struct bin_attribute *olpc_bat_sysfs_bin_attrs[] = {
589 	&olpc_bat_eeprom,
590 	NULL
591 };
592 
593 static const struct attribute_group olpc_bat_sysfs_group = {
594 	.attrs = olpc_bat_sysfs_attrs,
595 	.bin_attrs = olpc_bat_sysfs_bin_attrs,
596 
597 };
598 
599 static const struct attribute_group *olpc_bat_sysfs_groups[] = {
600 	&olpc_bat_sysfs_group,
601 	NULL
602 };
603 
604 /*********************************************************************
605  *		Initialisation
606  *********************************************************************/
607 
608 static struct power_supply_desc olpc_bat_desc = {
609 	.name = "olpc-battery",
610 	.get_property = olpc_bat_get_property,
611 	.use_for_apm = 1,
612 };
613 
614 static int olpc_battery_suspend(struct platform_device *pdev,
615 				pm_message_t state)
616 {
617 	struct olpc_battery_data *data = platform_get_drvdata(pdev);
618 
619 	if (device_may_wakeup(&data->olpc_ac->dev))
620 		olpc_ec_wakeup_set(EC_SCI_SRC_ACPWR);
621 	else
622 		olpc_ec_wakeup_clear(EC_SCI_SRC_ACPWR);
623 
624 	if (device_may_wakeup(&data->olpc_bat->dev))
625 		olpc_ec_wakeup_set(EC_SCI_SRC_BATTERY | EC_SCI_SRC_BATSOC
626 				   | EC_SCI_SRC_BATERR);
627 	else
628 		olpc_ec_wakeup_clear(EC_SCI_SRC_BATTERY | EC_SCI_SRC_BATSOC
629 				     | EC_SCI_SRC_BATERR);
630 
631 	return 0;
632 }
633 
634 static int olpc_battery_probe(struct platform_device *pdev)
635 {
636 	struct power_supply_config bat_psy_cfg = {};
637 	struct power_supply_config ac_psy_cfg = {};
638 	struct olpc_battery_data *data;
639 	uint8_t status;
640 	uint8_t ecver;
641 	int ret;
642 
643 	data = devm_kzalloc(&pdev->dev, sizeof(*data), GFP_KERNEL);
644 	if (!data)
645 		return -ENOMEM;
646 	platform_set_drvdata(pdev, data);
647 
648 	/* See if the EC is already there and get the EC revision */
649 	ret = olpc_ec_cmd(EC_FIRMWARE_REV, NULL, 0, &ecver, 1);
650 	if (ret)
651 		return ret;
652 
653 	if (of_find_compatible_node(NULL, NULL, "olpc,xo1.75-ec")) {
654 		/* XO 1.75 */
655 		data->new_proto = true;
656 		data->little_endian = true;
657 	} else if (ecver > 0x44) {
658 		/* XO 1 or 1.5 with a new EC firmware. */
659 		data->new_proto = true;
660 	} else if (ecver < 0x44) {
661 		/*
662 		 * We've seen a number of EC protocol changes; this driver
663 		 * requires the latest EC protocol, supported by 0x44 and above.
664 		 */
665 		printk(KERN_NOTICE "OLPC EC version 0x%02x too old for "
666 			"battery driver.\n", ecver);
667 		return -ENXIO;
668 	}
669 
670 	ret = olpc_ec_cmd(EC_BAT_STATUS, NULL, 0, &status, 1);
671 	if (ret)
672 		return ret;
673 
674 	/* Ignore the status. It doesn't actually matter */
675 
676 	ac_psy_cfg.of_node = pdev->dev.of_node;
677 	ac_psy_cfg.drv_data = data;
678 
679 	data->olpc_ac = devm_power_supply_register(&pdev->dev, &olpc_ac_desc,
680 								&ac_psy_cfg);
681 	if (IS_ERR(data->olpc_ac))
682 		return PTR_ERR(data->olpc_ac);
683 
684 	if (of_device_is_compatible(pdev->dev.of_node, "olpc,xo1.5-battery")) {
685 		/* XO-1.5 */
686 		olpc_bat_desc.properties = olpc_xo15_bat_props;
687 		olpc_bat_desc.num_properties = ARRAY_SIZE(olpc_xo15_bat_props);
688 	} else {
689 		/* XO-1 */
690 		olpc_bat_desc.properties = olpc_xo1_bat_props;
691 		olpc_bat_desc.num_properties = ARRAY_SIZE(olpc_xo1_bat_props);
692 	}
693 
694 	bat_psy_cfg.of_node = pdev->dev.of_node;
695 	bat_psy_cfg.drv_data = data;
696 	bat_psy_cfg.attr_grp = olpc_bat_sysfs_groups;
697 
698 	data->olpc_bat = devm_power_supply_register(&pdev->dev, &olpc_bat_desc,
699 								&bat_psy_cfg);
700 	if (IS_ERR(data->olpc_bat))
701 		return PTR_ERR(data->olpc_bat);
702 
703 	if (olpc_ec_wakeup_available()) {
704 		device_set_wakeup_capable(&data->olpc_ac->dev, true);
705 		device_set_wakeup_capable(&data->olpc_bat->dev, true);
706 	}
707 
708 	return 0;
709 }
710 
711 static const struct of_device_id olpc_battery_ids[] = {
712 	{ .compatible = "olpc,xo1-battery" },
713 	{ .compatible = "olpc,xo1.5-battery" },
714 	{}
715 };
716 MODULE_DEVICE_TABLE(of, olpc_battery_ids);
717 
718 static struct platform_driver olpc_battery_driver = {
719 	.driver = {
720 		.name = "olpc-battery",
721 		.of_match_table = olpc_battery_ids,
722 	},
723 	.probe = olpc_battery_probe,
724 	.suspend = olpc_battery_suspend,
725 };
726 
727 module_platform_driver(olpc_battery_driver);
728 
729 MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org>");
730 MODULE_LICENSE("GPL");
731 MODULE_DESCRIPTION("Battery driver for One Laptop Per Child 'XO' machine");
732