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