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