| 1 |
/* SPDX-License-Identifier: GPL-2.0-only */ |
| 2 |
|
| 3 |
// Scope (EC0) |
| 4 |
|
| 5 |
/* Mutex for EC battery index interface */ |
| 6 |
Mutex (BATM, 0) |
| 7 |
|
| 8 |
// Wait for desired battery index to be presented in shared memory |
| 9 |
// Arg0 = battery index |
| 10 |
// Returns 0 on success, 1 on error. |
| 11 |
Method (BTSW, 1) |
| 12 |
{ |
| 13 |
#ifdef EC_ENABLE_SECOND_BATTERY_DEVICE |
| 14 |
If (BTIX == Arg0) { |
| 15 |
Return (0) |
| 16 |
} |
| 17 |
If (Arg0 >= BTCN) { |
| 18 |
Return (1) |
| 19 |
} |
| 20 |
\_SB.PCI0.LPCB.EC0.BTID = Arg0 |
| 21 |
Local0 = 5 // Timeout 5 msec |
| 22 |
While (BTIX != Arg0) |
| 23 |
{ |
| 24 |
Sleep (1) |
| 25 |
Local0-- |
| 26 |
If (Local0 == 0) |
| 27 |
{ |
| 28 |
Return (1) |
| 29 |
} |
| 30 |
} |
| 31 |
#else |
| 32 |
If (Arg0 != 0) { |
| 33 |
Return (1) |
| 34 |
} |
| 35 |
#endif |
| 36 |
Return (0) |
| 37 |
} |
| 38 |
|
| 39 |
// _STA implementation. |
| 40 |
// Arg0 = battery index |
| 41 |
Method (BSTA, 1, Serialized) |
| 42 |
{ |
| 43 |
If (Acquire (^BATM, 1000)) { |
| 44 |
Return (0) |
| 45 |
} |
| 46 |
|
| 47 |
If (~BTSW (Arg0) & BTEX) { |
| 48 |
Local0 = 0x1F |
| 49 |
} Else { |
| 50 |
Local0 = 0x0F |
| 51 |
} |
| 52 |
|
| 53 |
Release (^BATM) |
| 54 |
Return (Local0) |
| 55 |
} |
| 56 |
|
| 57 |
#if CONFIG(EC_GOOGLE_CHROMEEC_READ_BATTERY_LONG_STRING) |
| 58 |
// Cached flag for BSRF FIFO readout support from EC. |
| 59 |
Name(BRSS, 0xff) |
| 60 |
#else |
| 61 |
Name(BRSS, 0x0) |
| 62 |
#endif |
| 63 |
// Cached battery string response indicator |
| 64 |
Name(BRI1, 0) |
| 65 |
Name(BRI2, 0) |
| 66 |
Name(BRI3, 0) |
| 67 |
// Cached battery string response data to save suspend-resume time |
| 68 |
Name(BRS1, Buffer(32) {0}) |
| 69 |
Name(BRS2, Buffer(32) {0}) |
| 70 |
Name(BRS3, Buffer(32) {0}) |
| 71 |
// Read extended battery strings from the selected battery. |
| 72 |
// Arg0 = string index |
| 73 |
// |
| 74 |
// If supported by the EC, strings of arbitrary length are read using the |
| 75 |
// FIFO readout method. Otherwise short (no more than 8 bytes) strings are |
| 76 |
// read from the EC shared memory map. The desired battery index should be |
| 77 |
// selected with BTSW before calling this method. |
| 78 |
// |
| 79 |
// Currently supported string indexes: |
| 80 |
// * 1 = EC_ACPI_MEM_STRINGS_FIFO_ID_BATTERY_MODEL: battery model name, |
| 81 |
// equivalent to BMOD. |
| 82 |
// * 2 = EC_ACPI_MEM_STRINGS_FIFO_ID_BATTERY_SERIAL: battery serial number, |
| 83 |
// equivalent to BSER. |
| 84 |
// * 3 = EC_ACPI_MEM_STRINGS_FIFO_ID_BATTERY_MANUFACTURER: battery |
| 85 |
// manufacturer's name, equivalent to BMFG. |
| 86 |
// |
| 87 |
// These are assumed to be supported if the EC reports at least version 1 of |
| 88 |
// string readout (it returns an integer greater than 0 and less than 255 when |
| 89 |
// reading FIFO index 0). Future strings may require higher FIFO versions. |
| 90 |
Method(BRSX, 1, Serialized) |
| 91 |
{ |
| 92 |
// It doesn't make sense to read the FIFO support indicator. |
| 93 |
if (Arg0 == 0 || Arg0 > 3) |
| 94 |
{ |
| 95 |
Return ("") |
| 96 |
} |
| 97 |
|
| 98 |
// Check if response is already cached |
| 99 |
If (Arg0 == 1 && BRI1 == 1) |
| 100 |
{ |
| 101 |
Return (BRS1) /* battery model name */ |
| 102 |
} |
| 103 |
|
| 104 |
If (Arg0 == 2 && BRI2 == 1) |
| 105 |
{ |
| 106 |
Return (BRS2) /* battery serial number */ |
| 107 |
} |
| 108 |
|
| 109 |
If (Arg0 == 3 && BRI3 == 1) |
| 110 |
{ |
| 111 |
Return (BRS3) /* battery manufacturer's name */ |
| 112 |
} |
| 113 |
|
| 114 |
If (BRSS == 0xff) |
| 115 |
{ |
| 116 |
// Write 0 to BSRF to read back a support indicator; nonzero and |
| 117 |
// non-0xFF if FIFO readout is supported, assuming minimum v1 support |
| 118 |
// for strings 1 through 3. |
| 119 |
BSRF = 0 |
| 120 |
BRSS = BSRF |
| 121 |
|
| 122 |
// 0xff readback also means no support for FIFO readout, when the EC |
| 123 |
// doesn't even know what this command is. |
| 124 |
if (BRSS == 0xff) |
| 125 |
{ |
| 126 |
BRSS = 0 |
| 127 |
} |
| 128 |
} |
| 129 |
|
| 130 |
// If FIFO readout through BSRF is not supported, fall back to reading |
| 131 |
// the short strings in EMEM. |
| 132 |
If (BRSS == 0) |
| 133 |
{ |
| 134 |
If (Arg0 == 1) |
| 135 |
{ |
| 136 |
Local0 = ToString (Concatenate (BMOD, 0)) |
| 137 |
} |
| 138 |
ElseIf (Arg0 == 2) |
| 139 |
{ |
| 140 |
Local0 = ToString (Concatenate (BSER, 0)) |
| 141 |
} |
| 142 |
ElseIf (Arg0 == 3) |
| 143 |
{ |
| 144 |
Local0 = ToString (Concatenate (BMFG, 0)) |
| 145 |
} |
| 146 |
} |
| 147 |
Else |
| 148 |
{ |
| 149 |
// Select requested parameter to read |
| 150 |
BSRF = Arg0 |
| 151 |
|
| 152 |
// Read to end of string, or up to a reasonable maximum length. Reads of |
| 153 |
// BSRF consume bytes from the FIFO, so take care to read it only once |
| 154 |
// per byte of data. |
| 155 |
Local0 = "" |
| 156 |
Local1 = BSRF |
| 157 |
While (Local1 != 0 && SizeOf (Local0) < 32) |
| 158 |
{ |
| 159 |
Local0 = Concatenate (Local0, ToString (Local1)) |
| 160 |
Local1 = BSRF |
| 161 |
} |
| 162 |
} |
| 163 |
|
| 164 |
// Store the result in the cache |
| 165 |
If (Arg0 == 1) |
| 166 |
{ |
| 167 |
BRS1 = Local0 |
| 168 |
BRI1 = 1 |
| 169 |
} |
| 170 |
|
| 171 |
If (Arg0 == 2) |
| 172 |
{ |
| 173 |
BRS2 = Local0 |
| 174 |
BRI2 = 1 |
| 175 |
} |
| 176 |
|
| 177 |
If (Arg0 == 3) |
| 178 |
{ |
| 179 |
BRS3 = Local0 |
| 180 |
BRI3 = 1 |
| 181 |
} |
| 182 |
|
| 183 |
Return (Local0) |
| 184 |
} |
| 185 |
|
| 186 |
// _BIF implementation. |
| 187 |
// Arg0 = battery index |
| 188 |
// Arg1 = PBIF |
| 189 |
Method (BBIF, 2, Serialized) |
| 190 |
{ |
| 191 |
If (Acquire (^BATM, 1000)) { |
| 192 |
Return (Arg1) |
| 193 |
} |
| 194 |
|
| 195 |
If (BTSW (Arg0)) { |
| 196 |
Release (^BATM) |
| 197 |
Return (Arg1) |
| 198 |
} |
| 199 |
// Last Full Charge Capacity |
| 200 |
Arg1[2] = BTDF |
| 201 |
|
| 202 |
// Design Voltage |
| 203 |
Arg1[4] = BTDV |
| 204 |
|
| 205 |
// Design Capacity |
| 206 |
Local0 = BTDA |
| 207 |
Arg1[1] = Local0 |
| 208 |
|
| 209 |
// Design Capacity of Warning |
| 210 |
Local2 = Local0 * DWRN / 100 |
| 211 |
Arg1[5] = Local2 |
| 212 |
|
| 213 |
// Design Capacity of Low |
| 214 |
Local2 = Local0 * DLOW / 100 |
| 215 |
Arg1[6] = Local2 |
| 216 |
|
| 217 |
// Get battery info from mainboard |
| 218 |
Arg1[9] = BRSX (1) |
| 219 |
Arg1[10] = BRSX (2) |
| 220 |
Arg1[12] = BRSX (3) |
| 221 |
|
| 222 |
Release (^BATM) |
| 223 |
Return (Arg1) |
| 224 |
} |
| 225 |
|
| 226 |
// _BIX implementation. |
| 227 |
// Arg0 = battery index |
| 228 |
// Arg1 = PBIX |
| 229 |
Method (BBIX, 2, Serialized) |
| 230 |
{ |
| 231 |
If (Acquire (^BATM, 1000)) { |
| 232 |
Return (Arg1) |
| 233 |
} |
| 234 |
|
| 235 |
If (BTSW (Arg0)) { |
| 236 |
Release (^BATM) |
| 237 |
Return (Arg1) |
| 238 |
} |
| 239 |
// Last Full Charge Capacity |
| 240 |
Arg1[3] = BTDF |
| 241 |
|
| 242 |
// Design Voltage |
| 243 |
Arg1[5] = BTDV |
| 244 |
|
| 245 |
// Design Capacity |
| 246 |
Local0 = BTDA |
| 247 |
Arg1[2] = Local0 |
| 248 |
|
| 249 |
// Design Capacity of Warning |
| 250 |
Local2 = Local0 * DWRN / 100 |
| 251 |
Arg1[6] = Local2 |
| 252 |
|
| 253 |
// Design Capacity of Low |
| 254 |
Local2 = Local0 * DLOW / 100 |
| 255 |
Arg1[7] = Local2 |
| 256 |
|
| 257 |
// Cycle Count |
| 258 |
Arg1[8] = BTCC |
| 259 |
|
| 260 |
// Get battery info from mainboard |
| 261 |
Arg1[16] = BRSX (1) |
| 262 |
Arg1[17] = BRSX (2) |
| 263 |
Arg1[19] = BRSX (3) |
| 264 |
|
| 265 |
Release (^BATM) |
| 266 |
Return (Arg1) |
| 267 |
} |
| 268 |
|
| 269 |
// _BST implementation. |
| 270 |
// Arg0 = battery index |
| 271 |
// Arg1 = PBST |
| 272 |
// Arg2 = BSTP |
| 273 |
// Arg3 = BFWK |
| 274 |
Method (BBST, 4, Serialized) |
| 275 |
{ |
| 276 |
If (Acquire (^BATM, 1000)) { |
| 277 |
Return (Arg1) |
| 278 |
} |
| 279 |
|
| 280 |
If (BTSW (Arg0)) { |
| 281 |
Release (^BATM) |
| 282 |
Return (Arg1) |
| 283 |
} |
| 284 |
// |
| 285 |
// 0: BATTERY STATE |
| 286 |
// |
| 287 |
// bit 0 = discharging |
| 288 |
// bit 1 = charging |
| 289 |
// bit 2 = critical level |
| 290 |
// |
| 291 |
Local1 = 0 |
| 292 |
|
| 293 |
// Check if AC is present |
| 294 |
If (ACEX) { |
| 295 |
If (BFCG) { |
| 296 |
Local1 = 0x02 |
| 297 |
} ElseIf (BFDC) { |
| 298 |
Local1 = 0x01 |
| 299 |
} |
| 300 |
} Else { |
| 301 |
// Always discharging when on battery power |
| 302 |
Local1 = 0x01 |
| 303 |
} |
| 304 |
|
| 305 |
// Check for critical battery level |
| 306 |
If (BFCR) { |
| 307 |
Local1 |= 4 |
| 308 |
} |
| 309 |
Arg1[0] = Local1 |
| 310 |
|
| 311 |
// Notify if battery state has changed since last time |
| 312 |
If (Local1 != DeRefOf (Arg2)) { |
| 313 |
Arg2 = Local1 |
| 314 |
If (Arg0 == 0) { |
| 315 |
Notify (BAT0, 0x80) |
| 316 |
} |
| 317 |
#ifdef EC_ENABLE_SECOND_BATTERY_DEVICE |
| 318 |
Else { |
| 319 |
Notify (BAT1, 0x80) |
| 320 |
} |
| 321 |
#endif |
| 322 |
} |
| 323 |
|
| 324 |
// |
| 325 |
// 1: BATTERY PRESENT RATE |
| 326 |
// |
| 327 |
Arg1[1] = BTPR |
| 328 |
|
| 329 |
// |
| 330 |
// 2: BATTERY REMAINING CAPACITY |
| 331 |
// |
| 332 |
Local1 = BTRA |
| 333 |
If (Arg3 && ACEX && !(BFDC && BFCG)) { |
| 334 |
// On AC power and battery is neither charging |
| 335 |
// nor discharging. Linux expects a full battery |
| 336 |
// to report same capacity as last full charge. |
| 337 |
// https://bugzilla.kernel.org/show_bug.cgi?id=12632 |
| 338 |
Local2 = BTDF |
| 339 |
|
| 340 |
// See if within ~6% of full |
| 341 |
Local3 = Local2 >> 4 |
| 342 |
If (Local1 > Local2 - Local3 && Local1 < Local2 + Local3) |
| 343 |
{ |
| 344 |
Local1 = Local2 |
| 345 |
} |
| 346 |
} |
| 347 |
Arg1[2] = Local1 |
| 348 |
|
| 349 |
// |
| 350 |
// 3: BATTERY PRESENT VOLTAGE |
| 351 |
// |
| 352 |
Arg1[3] = BTVO |
| 353 |
|
| 354 |
Release (^BATM) |
| 355 |
Return (Arg1) |
| 356 |
} |
| 357 |
|
| 358 |
Device (BAT0) |
| 359 |
{ |
| 360 |
Name (_HID, EISAID ("PNP0C0A")) |
| 361 |
Name (_UID, 1) |
| 362 |
Name (_PCL, Package () { \_SB }) |
| 363 |
|
| 364 |
Name (PBIF, Package () { |
| 365 |
0x00000001, // 0x00: Power Unit: mAh |
| 366 |
0xFFFFFFFF, // 0x01: Design Capacity |
| 367 |
0xFFFFFFFF, // 0x02: Last Full Charge Capacity |
| 368 |
0x00000001, // 0x03: Battery Technology: Rechargeable |
| 369 |
0xFFFFFFFF, // 0x04: Design Voltage |
| 370 |
0x00000003, // 0x05: Design Capacity of Warning |
| 371 |
0xFFFFFFFF, // 0x06: Design Capacity of Low |
| 372 |
0x00000001, // 0x07: Capacity Granularity 1 |
| 373 |
0x00000001, // 0x08: Capacity Granularity 2 |
| 374 |
"", // 0x09: Model Number |
| 375 |
"", // 0x0a: Serial Number |
| 376 |
"LION", // 0x0b: Battery Type |
| 377 |
"" // 0x0c: OEM Information |
| 378 |
}) |
| 379 |
|
| 380 |
Name (PBIX, Package () { |
| 381 |
0x00000000, // 0x00: Revision |
| 382 |
0x00000001, // 0x01: Power Unit: mAh |
| 383 |
0xFFFFFFFF, // 0x02: Design Capacity |
| 384 |
0xFFFFFFFF, // 0x03: Last Full Charge Capacity |
| 385 |
0x00000001, // 0x04: Battery Technology: Rechargeable |
| 386 |
0xFFFFFFFF, // 0x05: Design Voltage |
| 387 |
0x00000003, // 0x06: Design Capacity of Warning |
| 388 |
0xFFFFFFFF, // 0x07: Design Capacity of Low |
| 389 |
0x00000000, // 0x08: Cycle Count |
| 390 |
0x00018000, // 0x09: Measurement Accuracy (98.3%?) |
| 391 |
0x000001F4, // 0x0a: Max Sampling Time (500ms) |
| 392 |
0x0000000a, // 0x0b: Min Sampling Time (10ms) |
| 393 |
0xFFFFFFFF, // 0x0c: Max Averaging Interval |
| 394 |
0xFFFFFFFF, // 0x0d: Min Averaging Interval |
| 395 |
0x00000001, // 0x0e: Capacity Granularity 1 |
| 396 |
0x00000001, // 0x0f: Capacity Granularity 2 |
| 397 |
"", // 0x10 Model Number |
| 398 |
"", // 0x11: Serial Number |
| 399 |
"LION", // 0x12: Battery Type |
| 400 |
"" // 0x13: OEM Information |
| 401 |
}) |
| 402 |
|
| 403 |
Name (PBST, Package () { |
| 404 |
0x00000000, // 0x00: Battery State |
| 405 |
0xFFFFFFFF, // 0x01: Battery Present Rate |
| 406 |
0xFFFFFFFF, // 0x02: Battery Remaining Capacity |
| 407 |
0xFFFFFFFF, // 0x03: Battery Present Voltage |
| 408 |
}) |
| 409 |
Name (BSTP, 0) |
| 410 |
|
| 411 |
// Workaround for full battery status, disabled by default |
| 412 |
Name (BFWK, 0) |
| 413 |
|
| 414 |
// Method to enable full battery workaround |
| 415 |
Method (BFWE) |
| 416 |
{ |
| 417 |
BFWK = 1 |
| 418 |
} |
| 419 |
|
| 420 |
// Method to disable full battery workaround |
| 421 |
Method (BFWD) |
| 422 |
{ |
| 423 |
BFWK = 0 |
| 424 |
} |
| 425 |
|
| 426 |
Method (_STA, 0, Serialized) |
| 427 |
{ |
| 428 |
Return (BSTA (0)) |
| 429 |
} |
| 430 |
|
| 431 |
Method (_BIF, 0, Serialized) |
| 432 |
{ |
| 433 |
Return (BBIF (0, PBIF)) |
| 434 |
} |
| 435 |
|
| 436 |
Method (_BIX, 0, Serialized) |
| 437 |
{ |
| 438 |
Return (BBIX (0, PBIX)) |
| 439 |
} |
| 440 |
|
| 441 |
Method (_BST, 0, Serialized) |
| 442 |
{ |
| 443 |
Return (BBST (0, PBST, RefOf (BSTP), BFWK)) |
| 444 |
} |
| 445 |
} |
| 446 |
|
| 447 |
#ifdef EC_ENABLE_SECOND_BATTERY_DEVICE |
| 448 |
Device (BAT1) |
| 449 |
{ |
| 450 |
Name (_HID, EISAID ("PNP0C0A")) |
| 451 |
Name (_UID, 1) |
| 452 |
Name (_PCL, Package () { \_SB }) |
| 453 |
|
| 454 |
Name (PBIF, Package () { |
| 455 |
0x00000001, // 0x00: Power Unit: mAh |
| 456 |
0xFFFFFFFF, // 0x01: Design Capacity |
| 457 |
0xFFFFFFFF, // 0x02: Last Full Charge Capacity |
| 458 |
0x00000001, // 0x03: Battery Technology: Rechargeable |
| 459 |
0xFFFFFFFF, // 0x04: Design Voltage |
| 460 |
0x00000003, // 0x05: Design Capacity of Warning |
| 461 |
0xFFFFFFFF, // 0x06: Design Capacity of Low |
| 462 |
0x00000001, // 0x07: Capacity Granularity 1 |
| 463 |
0x00000001, // 0x08: Capacity Granularity 2 |
| 464 |
"", // 0x09: Model Number |
| 465 |
"", // 0x0a: Serial Number |
| 466 |
"LION", // 0x0b: Battery Type |
| 467 |
"" // 0x0c: OEM Information |
| 468 |
}) |
| 469 |
|
| 470 |
Name (PBIX, Package () { |
| 471 |
0x00000000, // 0x00: Revision |
| 472 |
0x00000001, // 0x01: Power Unit: mAh |
| 473 |
0xFFFFFFFF, // 0x02: Design Capacity |
| 474 |
0xFFFFFFFF, // 0x03: Last Full Charge Capacity |
| 475 |
0x00000001, // 0x04: Battery Technology: Rechargeable |
| 476 |
0xFFFFFFFF, // 0x05: Design Voltage |
| 477 |
0x00000003, // 0x06: Design Capacity of Warning |
| 478 |
0xFFFFFFFF, // 0x07: Design Capacity of Low |
| 479 |
0x00000000, // 0x08: Cycle Count |
| 480 |
0x00018000, // 0x09: Measurement Accuracy (98.3%?) |
| 481 |
0x000001F4, // 0x0a: Max Sampling Time (500ms) |
| 482 |
0x0000000a, // 0x0b: Min Sampling Time (10ms) |
| 483 |
0xFFFFFFFF, // 0x0c: Max Averaging Interval |
| 484 |
0xFFFFFFFF, // 0x0d: Min Averaging Interval |
| 485 |
0x00000001, // 0x0e: Capacity Granularity 1 |
| 486 |
0x00000001, // 0x0f: Capacity Granularity 2 |
| 487 |
"", // 0x10 Model Number |
| 488 |
"", // 0x11: Serial Number |
| 489 |
"LION", // 0x12: Battery Type |
| 490 |
"" // 0x13: OEM Information |
| 491 |
}) |
| 492 |
|
| 493 |
Name (PBST, Package () { |
| 494 |
0x00000000, // 0x00: Battery State |
| 495 |
0xFFFFFFFF, // 0x01: Battery Present Rate |
| 496 |
0xFFFFFFFF, // 0x02: Battery Remaining Capacity |
| 497 |
0xFFFFFFFF, // 0x03: Battery Present Voltage |
| 498 |
}) |
| 499 |
Name (BSTP, 0) |
| 500 |
|
| 501 |
// Workaround for full battery status, disabled by default |
| 502 |
Name (BFWK, 0) |
| 503 |
|
| 504 |
// Method to enable full battery workaround |
| 505 |
Method (BFWE) |
| 506 |
{ |
| 507 |
BFWK = 1 |
| 508 |
} |
| 509 |
|
| 510 |
// Method to disable full battery workaround |
| 511 |
Method (BFWD) |
| 512 |
{ |
| 513 |
BFWK = 0 |
| 514 |
} |
| 515 |
|
| 516 |
Method (_STA, 0, Serialized) |
| 517 |
{ |
| 518 |
Return (BSTA (1)) |
| 519 |
} |
| 520 |
|
| 521 |
Method (_BIF, 0, Serialized) |
| 522 |
{ |
| 523 |
Return (BBIF (1, PBIF)) |
| 524 |
} |
| 525 |
|
| 526 |
Method (_BIX, 0, Serialized) |
| 527 |
{ |
| 528 |
Return (BBIX (1, PBIX)) |
| 529 |
} |
| 530 |
|
| 531 |
Method (_BST, 0, Serialized) |
| 532 |
{ |
| 533 |
Return (BBST (1, PBST, RefOf (BSTP), BFWK)) |
| 534 |
} |
| 535 |
} |
| 536 |
#endif |