Add EC component for SMSC MEC1308/1310
[coreboot.git] / src / ec / smsc / mec1308 / acpi / battery.asl
1 /*
2  * This file is part of the coreboot project.
3  *
4  * Copyright (C) 2011 The Chromium OS Authors. All rights reserved.
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License as
8  * published by the Free Software Foundation; version 2 of
9  * the License.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
19  * MA 02110-1301 USA
20  */
21
22 /*
23  * The mainboard must define strings in the root scope to
24  * report device-specific battery information to the OS.
25  *
26  *  BATM: Model
27  *  BATS: Serial
28  *  BATV: Vendor
29  */
30
31 // Scope (EC0)
32
33 Device (BAT0)
34 {
35         Name (_HID, EISAID ("PNP0C0A"))
36         Name (_UID, 1)
37         Name (_PCL, Package () { \_SB })
38
39         Name (PBIF, Package () {
40                 0x00000001,  // Power Unit: mAh
41                 0xFFFFFFFF,  // Design Capacity
42                 0xFFFFFFFF,  // Last Full Charge Capacity
43                 0x00000001,  // Battery Technology: Rechargeable
44                 0xFFFFFFFF,  // Design Voltage
45                 0x00000003,  // Design Capacity of Warning
46                 0xFFFFFFFF,  // Design Capacity of Low
47                 0x00000001,  // Capacity Granularity 1
48                 0x00000001,  // Capacity Granularity 2
49                 "",          // Model Number
50                 "",          // Serial Number
51                 "LION",      // Battery Type
52                 ""           // OEM Information
53         })
54
55         Name (PBST, Package () {
56                 0x00000000,  // Battery State
57                 0xFFFFFFFF,  // Battery Present Rate
58                 0xFFFFFFFF,  // Battery Remaining Capacity
59                 0xFFFFFFFF,  // Battery Present Voltage
60         })
61         Name (BSTP, Zero)
62
63         // Workaround for full battery status, enabled by default
64         Name (BFWK, One)
65
66         // Method to enable full battery workaround
67         Method (BFWE)
68         {
69                 Store (One, BFWK)
70         }
71
72         // Method to disable full battery workaround
73         Method (BFWD)
74         {
75                 Store (Zero, BFWK)
76         }
77
78         // Swap bytes in a word
79         Method (SWAB, 1, NotSerialized)
80         {
81                 ShiftRight (Arg0, 8, Local0)
82                 ShiftLeft (Arg0, 8, Local1)
83                 And (Local1, 0xFF00, Local1)
84                 Or (Local0, Local1, Local0)
85                 If (LEqual (Local0, 0xFFFF)) {
86                         Store (0xFFFFFFFF, Local0)
87                 }
88                 Return (Local0)
89         }
90
91         Method (_STA, 0, Serialized)
92         {
93                 If (BTEX) {
94                         Return (0x1F)
95                 } Else {
96                         Return (0x0F)
97                 }
98         }
99
100         Method (_BIF, 0, Serialized)
101         {
102                 // Update fields from EC
103                 Store (SWAB (BTDA), Index (PBIF, 1))
104                 Store (SWAB (BTDF), Index (PBIF, 2))
105                 Store (SWAB (BTDV), Index (PBIF, 4))
106                 Store (SWAB (BTDL), Index (PBIF, 6))
107
108                 // Get battery info from mainboard
109                 Store (\BATM, Index (PBIF, 9))
110                 Store (\BATS, Index (PBIF, 10))
111                 Store (\BATV, Index (PBIF, 12))
112
113                 Return (PBIF)
114         }
115
116         Method (_BST, 0, Serialized)
117         {
118                 //
119                 // 0: BATTERY STATE
120                 //
121                 // bit 0 = discharging
122                 // bit 1 = charging
123                 // bit 2 = critical level
124                 //
125
126                 // Get battery state from EC
127                 Store (BTST, Local0)
128                 Store (Zero, Local1)
129
130                 // Check if AC is present
131                 If (ACEX) {
132                         // Set only charging/discharging bits
133                         And (Local0, 0x03, Local1)
134                 } Else {
135                         // Always discharging when on battery power
136                         Store (0x01, Local1)
137                 }
138
139                 // Flag if the battery level is critical
140                 And (Local0, 0x04, Local4)
141                 Or (Local1, Local4, Local1)
142                 Store (Local1, Index (PBST, 0))
143
144                 // Notify if battery state has changed since last time
145                 If (LNotEqual (Local1, BSTP)) {
146                         Store (Local1, BSTP)
147                         Notify (BAT0, 0x80)
148                 }
149
150                 //
151                 // 1: BATTERY PRESENT RATE
152                 //
153
154                 Store (SWAB (BTPR), Local1)
155                 If (LAnd (LNotEqual (Local1, 0xFFFFFFFF),
156                           LGreaterEqual (Local1, 0x8000))) {
157                         Xor (Local1, 0xFFFF, Local1)
158                         Increment (Local1)
159                 }
160                 Store (Local1, Index (PBST, 1))
161
162                 //
163                 // 2: BATTERY REMAINING CAPACITY
164                 //
165                 Store (SWAB (BTRA), Local1)
166                 If (LAnd (LNotEqual (Local1, 0xFFFFFFFF),
167                           LGreaterEqual (Local1, 0x8000))) {
168                         Xor (Local1, 0xFFFF, Local1)
169                         Increment (Local1)
170                 }
171
172                 If (LAnd (BFWK, LAnd (ACEX, LNot (Local0)))) {
173                         // On AC power and battery is neither charging
174                         // nor discharging.  Linux expects a full battery
175                         // to report same capacity as last full charge.
176                         // https://bugzilla.kernel.org/show_bug.cgi?id=12632
177                         Store (SWAB (BTDF), Local2)
178
179                         // See if within ~3% of full
180                         ShiftRight (Local2, 5, Local3)
181                         If (LAnd (LGreater (Local1, Subtract (Local2, Local3)),
182                                   LLess (Local1, Add (Local2, Local3))))
183                         {
184                                 Store (Local2, Local1)
185                         }
186                 }
187                 Store (Local1, Index (PBST, 2))
188
189                 //
190                 // 3: BATTERY PRESENT VOLTAGE
191                 //
192                 Store (SWAB (BTVO), Index (PBST, 3))
193
194                 Return (PBST)
195         }
196 }