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