blob: 9f9430ac88877542c34947b3e2b37946ade51e90 [file] [log] [blame]
Thomas Gleixnerd2912cb2019-06-04 10:11:33 +02001// SPDX-License-Identifier: GPL-2.0-only
David Woodhousefb972872007-05-04 00:51:18 +04002/*
3 * Battery driver for One Laptop Per Child board.
4 *
David Woodhouse690e85a2010-08-09 18:13:09 +01005 * Copyright © 2006-2010 David Woodhouse <dwmw2@infradead.org>
David Woodhousefb972872007-05-04 00:51:18 +04006 */
7
Andres Salomon04a820e2009-06-30 02:14:00 -04008#include <linux/kernel.h>
David Woodhousefb972872007-05-04 00:51:18 +04009#include <linux/module.h>
Randy Dunlapac316722018-06-19 22:47:28 -070010#include <linux/mod_devicetable.h>
Andres Salomon144bbea2009-06-30 02:15:26 -040011#include <linux/types.h>
David Woodhousefb972872007-05-04 00:51:18 +040012#include <linux/err.h>
Andres Salomon144bbea2009-06-30 02:15:26 -040013#include <linux/device.h>
Lubomir Rintelf7a228e2019-04-18 16:46:50 +020014#include <linux/of.h>
David Woodhousefb972872007-05-04 00:51:18 +040015#include <linux/platform_device.h>
16#include <linux/power_supply.h>
17#include <linux/jiffies.h>
18#include <linux/sched.h>
Andres Salomon3bf94282012-07-11 01:16:29 -070019#include <linux/olpc-ec.h>
David Woodhousefb972872007-05-04 00:51:18 +040020#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 Salomon75d88072008-05-14 16:20:38 -070025#define EC_BAT_ACR 0x12 /* int16_t, *6250/15, µAh */
David Woodhousefb972872007-05-04 00:51:18 +040026#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 Salomon8f7e5792009-06-30 02:16:17 -040041#define BAT_STAT_TRICKLE 0x80
David Woodhousefb972872007-05-04 00:51:18 +040042
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
Lubomir Rintel33554d82019-04-18 16:46:51 +020053struct olpc_battery_data {
54 struct power_supply *olpc_ac;
55 struct power_supply *olpc_bat;
56 char bat_serial[17];
Lubomir Rintel8ecefda2019-04-18 16:46:53 +020057 bool new_proto;
Lubomir Rintel76311b92019-04-18 16:46:54 +020058 bool little_endian;
Lubomir Rintel33554d82019-04-18 16:46:51 +020059};
60
David Woodhousefb972872007-05-04 00:51:18 +040061/*********************************************************************
62 * Power
63 *********************************************************************/
64
65static 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
87static enum power_supply_property olpc_ac_props[] = {
88 POWER_SUPPLY_PROP_ONLINE,
89};
90
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +010091static const struct power_supply_desc olpc_ac_desc = {
David Woodhousefb972872007-05-04 00:51:18 +040092 .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
Lubomir Rintel33554d82019-04-18 16:46:51 +020099static int olpc_bat_get_status(struct olpc_battery_data *data,
100 union power_supply_propval *val, uint8_t ec_byte)
Andres Salomonb2bd8a32008-05-02 13:41:59 -0700101{
Lubomir Rintel8ecefda2019-04-18 16:46:53 +0200102 if (data->new_proto) {
Andres Salomon8f7e5792009-06-30 02:16:17 -0400103 if (ec_byte & (BAT_STAT_CHARGING | BAT_STAT_TRICKLE))
Andres Salomonb2bd8a32008-05-02 13:41:59 -0700104 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
124static 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
161static 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
186static 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
Sascha Silbeb202a5e2010-12-10 23:05:19 +0100211static 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) {
Richard A. Smithecc2edd2012-07-15 22:43:51 +0100241 case 1: /* Gold Peak, fall through */
Sascha Silbeb202a5e2010-12-10 23:05:19 +0100242 case 2: /* BYD */
Richard A. Smithecc2edd2012-07-15 22:43:51 +0100243 val->intval = 2800000;
Sascha Silbeb202a5e2010-12-10 23:05:19 +0100244 break;
245 default:
246 return -EIO;
247 }
248 break;
249
250 default:
251 return -EIO;
252 }
253
254 return ret;
255}
256
Sascha Silbe20fd9832010-12-10 23:05:20 +0100257static 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
Richard A. Smith5619d0b2012-07-15 22:43:25 +0100275static 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
Lubomir Rintel76311b92019-04-18 16:46:54 +0200324static u16 ecword_to_cpu(struct olpc_battery_data *data, u16 ec_word)
325{
326 if (data->little_endian)
Lubomir Rintelbaf59642019-05-11 10:56:14 +0200327 return le16_to_cpu((__force __le16)ec_word);
Lubomir Rintel76311b92019-04-18 16:46:54 +0200328 else
Lubomir Rintelbaf59642019-05-11 10:56:14 +0200329 return be16_to_cpu((__force __be16)ec_word);
Lubomir Rintel76311b92019-04-18 16:46:54 +0200330}
331
David Woodhousefb972872007-05-04 00:51:18 +0400332/*********************************************************************
333 * Battery properties
334 *********************************************************************/
335static int olpc_bat_get_property(struct power_supply *psy,
336 enum power_supply_property psp,
337 union power_supply_propval *val)
338{
Lubomir Rintel33554d82019-04-18 16:46:51 +0200339 struct olpc_battery_data *data = power_supply_get_drvdata(psy);
David Woodhousefb972872007-05-04 00:51:18 +0400340 int ret = 0;
Lubomir Rintelbaf59642019-05-11 10:56:14 +0200341 u16 ec_word;
David Woodhousefb972872007-05-04 00:51:18 +0400342 uint8_t ec_byte;
Harvey Harrison8e9c7712008-10-15 22:01:23 -0700343 __be64 ser_buf;
David Woodhousefb972872007-05-04 00:51:18 +0400344
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. */
Andres Salomon8f7e5792009-06-30 02:16:17 -0400355 if (!(ec_byte & (BAT_STAT_PRESENT | BAT_STAT_TRICKLE)) &&
356 psp != POWER_SUPPLY_PROP_PRESENT)
David Woodhousefb972872007-05-04 00:51:18 +0400357 return -ENODEV;
358
359 switch (psp) {
360 case POWER_SUPPLY_PROP_STATUS:
Lubomir Rintel33554d82019-04-18 16:46:51 +0200361 ret = olpc_bat_get_status(data, val, ec_byte);
Andres Salomonb2bd8a32008-05-02 13:41:59 -0700362 if (ret)
363 return ret;
364 break;
Andres Salomonee8076e2009-07-02 09:45:18 -0400365 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;
David Woodhousefb972872007-05-04 00:51:18 +0400373 case POWER_SUPPLY_PROP_PRESENT:
Andres Salomon8f7e5792009-06-30 02:16:17 -0400374 val->intval = !!(ec_byte & (BAT_STAT_PRESENT |
375 BAT_STAT_TRICKLE));
David Woodhousefb972872007-05-04 00:51:18 +0400376 break;
377
378 case POWER_SUPPLY_PROP_HEALTH:
379 if (ec_byte & BAT_STAT_DESTROY)
380 val->intval = POWER_SUPPLY_HEALTH_DEAD;
381 else {
Andres Salomonb2bd8a32008-05-02 13:41:59 -0700382 ret = olpc_bat_get_health(val);
David Woodhousefb972872007-05-04 00:51:18 +0400383 if (ret)
384 return ret;
David Woodhousefb972872007-05-04 00:51:18 +0400385 }
386 break;
387
388 case POWER_SUPPLY_PROP_MANUFACTURER:
Andres Salomonb2bd8a32008-05-02 13:41:59 -0700389 ret = olpc_bat_get_mfr(val);
David Woodhousefb972872007-05-04 00:51:18 +0400390 if (ret)
391 return ret;
David Woodhousefb972872007-05-04 00:51:18 +0400392 break;
393 case POWER_SUPPLY_PROP_TECHNOLOGY:
Andres Salomonb2bd8a32008-05-02 13:41:59 -0700394 ret = olpc_bat_get_tech(val);
David Woodhousefb972872007-05-04 00:51:18 +0400395 if (ret)
396 return ret;
David Woodhousefb972872007-05-04 00:51:18 +0400397 break;
398 case POWER_SUPPLY_PROP_VOLTAGE_AVG:
Sascha Silbe22fadd72010-12-10 23:05:21 +0100399 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
David Woodhousefb972872007-05-04 00:51:18 +0400400 ret = olpc_ec_cmd(EC_BAT_VOLTAGE, NULL, 0, (void *)&ec_word, 2);
401 if (ret)
402 return ret;
403
Lubomir Rintel76311b92019-04-18 16:46:54 +0200404 val->intval = ecword_to_cpu(data, ec_word) * 9760L / 32;
David Woodhousefb972872007-05-04 00:51:18 +0400405 break;
406 case POWER_SUPPLY_PROP_CURRENT_AVG:
Sascha Silbe22fadd72010-12-10 23:05:21 +0100407 case POWER_SUPPLY_PROP_CURRENT_NOW:
David Woodhousefb972872007-05-04 00:51:18 +0400408 ret = olpc_ec_cmd(EC_BAT_CURRENT, NULL, 0, (void *)&ec_word, 2);
409 if (ret)
410 return ret;
411
Lubomir Rintel76311b92019-04-18 16:46:54 +0200412 val->intval = ecword_to_cpu(data, ec_word) * 15625L / 120;
David Woodhousefb972872007-05-04 00:51:18 +0400413 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;
Andres Salomonb294a292009-06-30 02:13:01 -0400420 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;
Sascha Silbeb202a5e2010-12-10 23:05:19 +0100428 case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN:
429 ret = olpc_bat_get_charge_full_design(val);
430 if (ret)
431 return ret;
432 break;
Sascha Silbe20fd9832010-12-10 23:05:20 +0100433 case POWER_SUPPLY_PROP_CHARGE_NOW:
434 ret = olpc_bat_get_charge_now(val);
435 if (ret)
436 return ret;
437 break;
David Woodhousefb972872007-05-04 00:51:18 +0400438 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;
Harvey Harrison8e9c7712008-10-15 22:01:23 -0700442
Lubomir Rintel76311b92019-04-18 16:46:54 +0200443 val->intval = ecword_to_cpu(data, ec_word) * 10 / 256;
David Woodhousefb972872007-05-04 00:51:18 +0400444 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
Lubomir Rintel76311b92019-04-18 16:46:54 +0200450 val->intval = (int)ecword_to_cpu(data, ec_word) * 10 / 256;
David Woodhousefb972872007-05-04 00:51:18 +0400451 break;
Andres Salomon8e552c32008-05-12 21:46:29 -0400452 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
Lubomir Rintel76311b92019-04-18 16:46:54 +0200457 val->intval = ecword_to_cpu(data, ec_word) * 6250 / 15;
Andres Salomon8e552c32008-05-12 21:46:29 -0400458 break;
David Woodhouse1ca5b9d2008-05-04 01:31:42 -0400459 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
Lubomir Rintel33554d82019-04-18 16:46:51 +0200464 sprintf(data->bat_serial, "%016llx", (long long)be64_to_cpu(ser_buf));
465 val->strval = data->bat_serial;
David Woodhouse1ca5b9d2008-05-04 01:31:42 -0400466 break;
Richard A. Smith5619d0b2012-07-15 22:43:25 +0100467 case POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN:
468 ret = olpc_bat_get_voltage_max_design(val);
469 if (ret)
470 return ret;
471 break;
David Woodhousefb972872007-05-04 00:51:18 +0400472 default:
473 ret = -EINVAL;
474 break;
475 }
476
477 return ret;
478}
479
Daniel Drakec566d292010-12-29 19:12:01 +0000480static enum power_supply_property olpc_xo1_bat_props[] = {
David Woodhousefb972872007-05-04 00:51:18 +0400481 POWER_SUPPLY_PROP_STATUS,
Andres Salomonee8076e2009-07-02 09:45:18 -0400482 POWER_SUPPLY_PROP_CHARGE_TYPE,
David Woodhousefb972872007-05-04 00:51:18 +0400483 POWER_SUPPLY_PROP_PRESENT,
484 POWER_SUPPLY_PROP_HEALTH,
485 POWER_SUPPLY_PROP_TECHNOLOGY,
486 POWER_SUPPLY_PROP_VOLTAGE_AVG,
Sascha Silbe22fadd72010-12-10 23:05:21 +0100487 POWER_SUPPLY_PROP_VOLTAGE_NOW,
David Woodhousefb972872007-05-04 00:51:18 +0400488 POWER_SUPPLY_PROP_CURRENT_AVG,
Sascha Silbe22fadd72010-12-10 23:05:21 +0100489 POWER_SUPPLY_PROP_CURRENT_NOW,
David Woodhousefb972872007-05-04 00:51:18 +0400490 POWER_SUPPLY_PROP_CAPACITY,
Andres Salomonb294a292009-06-30 02:13:01 -0400491 POWER_SUPPLY_PROP_CAPACITY_LEVEL,
Sascha Silbeb202a5e2010-12-10 23:05:19 +0100492 POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN,
Sascha Silbe20fd9832010-12-10 23:05:20 +0100493 POWER_SUPPLY_PROP_CHARGE_NOW,
David Woodhousefb972872007-05-04 00:51:18 +0400494 POWER_SUPPLY_PROP_TEMP,
495 POWER_SUPPLY_PROP_TEMP_AMBIENT,
496 POWER_SUPPLY_PROP_MANUFACTURER,
David Woodhouse1ca5b9d2008-05-04 01:31:42 -0400497 POWER_SUPPLY_PROP_SERIAL_NUMBER,
Andres Salomon8e552c32008-05-12 21:46:29 -0400498 POWER_SUPPLY_PROP_CHARGE_COUNTER,
Richard A. Smith5619d0b2012-07-15 22:43:25 +0100499 POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN,
David Woodhousefb972872007-05-04 00:51:18 +0400500};
501
Daniel Drakec566d292010-12-29 19:12:01 +0000502/* XO-1.5 does not have ambient temperature property */
503static 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,
Sascha Silbebf542a42011-01-12 23:23:23 +0100510 POWER_SUPPLY_PROP_VOLTAGE_NOW,
Daniel Drakec566d292010-12-29 19:12:01 +0000511 POWER_SUPPLY_PROP_CURRENT_AVG,
Sascha Silbebf542a42011-01-12 23:23:23 +0100512 POWER_SUPPLY_PROP_CURRENT_NOW,
Daniel Drakec566d292010-12-29 19:12:01 +0000513 POWER_SUPPLY_PROP_CAPACITY,
514 POWER_SUPPLY_PROP_CAPACITY_LEVEL,
Sascha Silbebf542a42011-01-12 23:23:23 +0100515 POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN,
516 POWER_SUPPLY_PROP_CHARGE_NOW,
Daniel Drakec566d292010-12-29 19:12:01 +0000517 POWER_SUPPLY_PROP_TEMP,
518 POWER_SUPPLY_PROP_MANUFACTURER,
519 POWER_SUPPLY_PROP_SERIAL_NUMBER,
520 POWER_SUPPLY_PROP_CHARGE_COUNTER,
Richard A. Smith5619d0b2012-07-15 22:43:25 +0100521 POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN,
Daniel Drakec566d292010-12-29 19:12:01 +0000522};
523
Andres Salomond7eb9e32008-05-02 13:41:58 -0700524/* 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
Chris Wright2c3c8be2010-05-12 18:28:57 -0700530static ssize_t olpc_bat_eeprom_read(struct file *filp, struct kobject *kobj,
Andres Salomond7eb9e32008-05-02 13:41:58 -0700531 struct bin_attribute *attr, char *buf, loff_t off, size_t count)
532{
533 uint8_t ec_byte;
Andres Salomon04a820e2009-06-30 02:14:00 -0400534 int ret;
535 int i;
Andres Salomond7eb9e32008-05-02 13:41:58 -0700536
Andres Salomon04a820e2009-06-30 02:14:00 -0400537 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);
Andres Salomond7eb9e32008-05-02 13:41:58 -0700540 if (ret) {
Andres Salomon04a820e2009-06-30 02:14:00 -0400541 pr_err("olpc-battery: "
542 "EC_BAT_EEPROM cmd @ 0x%x failed - %d!\n",
543 ec_byte, ret);
Andres Salomond7eb9e32008-05-02 13:41:58 -0700544 return -EIO;
545 }
546 }
547
548 return count;
549}
550
Lubomir Rintel31e22082019-04-18 16:46:55 +0200551static struct bin_attribute olpc_bat_eeprom = {
Andres Salomond7eb9e32008-05-02 13:41:58 -0700552 .attr = {
553 .name = "eeprom",
554 .mode = S_IRUGO,
Andres Salomond7eb9e32008-05-02 13:41:58 -0700555 },
Vladimir Zapolskiy3e1d9c62015-07-27 00:26:49 +0300556 .size = EEPROM_SIZE,
Andres Salomond7eb9e32008-05-02 13:41:58 -0700557 .read = olpc_bat_eeprom_read,
558};
559
Andres Salomon144bbea2009-06-30 02:15:26 -0400560/* Allow userspace to see the specific error value pulled from the EC */
561
562static 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
Lubomir Rintel31e22082019-04-18 16:46:55 +0200575static struct device_attribute olpc_bat_error = {
Andres Salomon144bbea2009-06-30 02:15:26 -0400576 .attr = {
577 .name = "error",
578 .mode = S_IRUGO,
579 },
580 .show = olpc_bat_error_read,
581};
582
Lubomir Rintel31e22082019-04-18 16:46:55 +0200583static struct attribute *olpc_bat_sysfs_attrs[] = {
584 &olpc_bat_error.attr,
585 NULL
586};
587
588static struct bin_attribute *olpc_bat_sysfs_bin_attrs[] = {
589 &olpc_bat_eeprom,
590 NULL
591};
592
593static 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
599static const struct attribute_group *olpc_bat_sysfs_groups[] = {
600 &olpc_bat_sysfs_group,
601 NULL
602};
603
David Woodhousefb972872007-05-04 00:51:18 +0400604/*********************************************************************
605 * Initialisation
606 *********************************************************************/
607
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100608static struct power_supply_desc olpc_bat_desc = {
Daniel Drakec3503fd2011-08-10 21:45:36 +0100609 .name = "olpc-battery",
David Woodhousefb972872007-05-04 00:51:18 +0400610 .get_property = olpc_bat_get_property,
611 .use_for_apm = 1,
612};
613
Daniel Drakecae659a2011-08-10 21:46:02 +0100614static int olpc_battery_suspend(struct platform_device *pdev,
615 pm_message_t state)
616{
Lubomir Rintel33554d82019-04-18 16:46:51 +0200617 struct olpc_battery_data *data = platform_get_drvdata(pdev);
618
619 if (device_may_wakeup(&data->olpc_ac->dev))
Daniel Drakecae659a2011-08-10 21:46:02 +0100620 olpc_ec_wakeup_set(EC_SCI_SRC_ACPWR);
621 else
622 olpc_ec_wakeup_clear(EC_SCI_SRC_ACPWR);
623
Lubomir Rintel33554d82019-04-18 16:46:51 +0200624 if (device_may_wakeup(&data->olpc_bat->dev))
Daniel Drakecae659a2011-08-10 21:46:02 +0100625 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
Bill Pembertonc8afa642012-11-19 13:22:23 -0500634static int olpc_battery_probe(struct platform_device *pdev)
David Woodhousefb972872007-05-04 00:51:18 +0400635{
Lubomir Rintel31e22082019-04-18 16:46:55 +0200636 struct power_supply_config bat_psy_cfg = {};
637 struct power_supply_config ac_psy_cfg = {};
Lubomir Rintel33554d82019-04-18 16:46:51 +0200638 struct olpc_battery_data *data;
David Woodhousefb972872007-05-04 00:51:18 +0400639 uint8_t status;
Lubomir Rintel8ecefda2019-04-18 16:46:53 +0200640 uint8_t ecver;
Lubomir Rintel33554d82019-04-18 16:46:51 +0200641 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);
David Woodhousefb972872007-05-04 00:51:18 +0400647
Lubomir Rintel8ecefda2019-04-18 16:46:53 +0200648 /* 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
Lubomir Rintel76311b92019-04-18 16:46:54 +0200653 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) {
Lubomir Rintel8ecefda2019-04-18 16:46:53 +0200658 /* 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 */
Andres Salomon484d6d52008-05-02 13:41:59 -0700665 printk(KERN_NOTICE "OLPC EC version 0x%02x too old for "
Lubomir Rintel8ecefda2019-04-18 16:46:53 +0200666 "battery driver.\n", ecver);
David Woodhousefb972872007-05-04 00:51:18 +0400667 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
Lubomir Rintel31e22082019-04-18 16:46:55 +0200676 ac_psy_cfg.of_node = pdev->dev.of_node;
677 ac_psy_cfg.drv_data = data;
Lubomir Rintel33554d82019-04-18 16:46:51 +0200678
Lubomir Rintel31e22082019-04-18 16:46:55 +0200679 data->olpc_ac = devm_power_supply_register(&pdev->dev, &olpc_ac_desc,
680 &ac_psy_cfg);
Lubomir Rintel33554d82019-04-18 16:46:51 +0200681 if (IS_ERR(data->olpc_ac))
682 return PTR_ERR(data->olpc_ac);
683
Lubomir Rintelf7a228e2019-04-18 16:46:50 +0200684 if (of_device_is_compatible(pdev->dev.of_node, "olpc,xo1.5-battery")) {
685 /* XO-1.5 */
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100686 olpc_bat_desc.properties = olpc_xo15_bat_props;
687 olpc_bat_desc.num_properties = ARRAY_SIZE(olpc_xo15_bat_props);
Lubomir Rintelf7a228e2019-04-18 16:46:50 +0200688 } else {
689 /* XO-1 */
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100690 olpc_bat_desc.properties = olpc_xo1_bat_props;
691 olpc_bat_desc.num_properties = ARRAY_SIZE(olpc_xo1_bat_props);
Daniel Drakec566d292010-12-29 19:12:01 +0000692 }
David Woodhousefb972872007-05-04 00:51:18 +0400693
Lubomir Rintel31e22082019-04-18 16:46:55 +0200694 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);
Lubomir Rintelb0280d02019-04-18 16:46:52 +0200700 if (IS_ERR(data->olpc_bat))
701 return PTR_ERR(data->olpc_bat);
David Woodhousefb972872007-05-04 00:51:18 +0400702
Daniel Drakecae659a2011-08-10 21:46:02 +0100703 if (olpc_ec_wakeup_available()) {
Lubomir Rintel33554d82019-04-18 16:46:51 +0200704 device_set_wakeup_capable(&data->olpc_ac->dev, true);
705 device_set_wakeup_capable(&data->olpc_bat->dev, true);
Daniel Drakecae659a2011-08-10 21:46:02 +0100706 }
707
Daniel Drakec3503fd2011-08-10 21:45:36 +0100708 return 0;
David Woodhousefb972872007-05-04 00:51:18 +0400709}
710
Greg Kroah-Hartman6d2cea42012-12-21 15:04:54 -0800711static const struct of_device_id olpc_battery_ids[] = {
Daniel Drakec3503fd2011-08-10 21:45:36 +0100712 { .compatible = "olpc,xo1-battery" },
Lubomir Rintelf7a228e2019-04-18 16:46:50 +0200713 { .compatible = "olpc,xo1.5-battery" },
Daniel Drakec3503fd2011-08-10 21:45:36 +0100714 {}
715};
716MODULE_DEVICE_TABLE(of, olpc_battery_ids);
717
Anton Vorontsov5519d002011-11-24 22:49:07 +0400718static struct platform_driver olpc_battery_driver = {
Daniel Drakec3503fd2011-08-10 21:45:36 +0100719 .driver = {
720 .name = "olpc-battery",
Daniel Drakec3503fd2011-08-10 21:45:36 +0100721 .of_match_table = olpc_battery_ids,
722 },
723 .probe = olpc_battery_probe,
Daniel Drakecae659a2011-08-10 21:46:02 +0100724 .suspend = olpc_battery_suspend,
Daniel Drakec3503fd2011-08-10 21:45:36 +0100725};
726
Axel Lin300bac72011-11-26 12:01:10 +0800727module_platform_driver(olpc_battery_driver);
David Woodhousefb972872007-05-04 00:51:18 +0400728
729MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org>");
730MODULE_LICENSE("GPL");
731MODULE_DESCRIPTION("Battery driver for One Laptop Per Child 'XO' machine");