1/*/* SPDX-License-Identifier: GPL-2.0 */
2/*
3 * Copyright (C) 2011 The Chromium OS Authors. All rights reserved.
4 */
5
6// Scope (EC0)
7
8/* Mutex for EC battery index interface */
9Mutex (BATM, 0)
10
11// Wait for desired battery index to be presented in shared memory
12//   Arg0 = battery index
13//   Returns Zero on success, One on error.
14Method (BTSW, 1)
15{
16#ifdef EC_ENABLE_SECOND_BATTERY_DEVICE
17	If (LEqual (BTIX, Arg0)) {
18		Return (Zero)
19	}
20	If (LGreaterEqual (Arg0, BTCN)) {
21		Return (One)
22	}
23	Store (Arg0, \_SB.PCI0.LPCB.EC0.BTID)
24	Store (5, Local0)      // Timeout 5 msec
25	While (LNotEqual (BTIX, Arg0))
26	{
27		Sleep (1)
28		Decrement (Local0)
29		If (LEqual (Local0, Zero))
30		{
31			Return (One)
32		}
33	}
34#else
35	If (LNotEqual (0, Arg0)) {
36		Return (One)
37	}
38#endif
39	Return (Zero)
40}
41
42// _STA implementation.
43//   Arg0 = battery index
44Method (BSTA, 1, Serialized)
45{
46	If (Acquire (^BATM, 1000)) {
47		Return (Zero)
48	}
49
50	If (And(Not(BTSW (Arg0)), BTEX)) {
51		Store (0x1F, Local0)
52	} Else {
53		Store (0x0F, Local0)
54	}
55
56	Release (^BATM)
57	Return (Local0)
58}
59
60// _BIF implementation.
61//   Arg0 = battery index
62//   Arg1 = PBIF
63Method (BBIF, 2, Serialized)
64{
65	If (Acquire (^BATM, 1000)) {
66		Return (Arg1)
67	}
68
69	If (BTSW (Arg0)) {
70		Release (^BATM)
71		Return (Arg1)
72	}
73	// Last Full Charge Capacity
74	Store (BTDF, Index (Arg1, 2))
75
76	// Design Voltage
77	Store (BTDV, Index (Arg1, 4))
78
79	// Design Capacity
80	Store (BTDA, Local0)
81	Store (Local0, Index (Arg1, 1))
82
83	// Design Capacity of Warning
84	Divide (Multiply (Local0, DWRN), 100, , Local2)
85	Store (Local2, Index (Arg1, 5))
86
87	// Design Capacity of Low
88	Divide (Multiply (Local0, DLOW), 100, , Local2)
89	Store (Local2, Index (Arg1, 6))
90
91	// Get battery info from mainboard
92	Store (ToString(Concatenate(BMOD, 0x00)), Index (Arg1, 9))
93	Store (ToString(Concatenate(BSER, 0x00)), Index (Arg1, 10))
94	Store (ToString(Concatenate(BMFG, 0x00)), Index (Arg1, 12))
95
96	Release (^BATM)
97	Return (Arg1)
98}
99
100// _BIX implementation.
101//   Arg0 = battery index
102//   Arg1 = PBIX
103Method (BBIX, 2, Serialized)
104{
105	If (Acquire (^BATM, 1000)) {
106		Return (Arg1)
107	}
108
109	If (BTSW (Arg0)) {
110		Release (^BATM)
111		Return (Arg1)
112	}
113	// Last Full Charge Capacity
114	Store (BTDF, Index (Arg1, 3))
115
116	// Design Voltage
117	Store (BTDV, Index (Arg1, 5))
118
119	// Design Capacity
120	Store (BTDA, Local0)
121	Store (Local0, Index (Arg1, 2))
122
123	// Design Capacity of Warning
124	Divide (Multiply (Local0, DWRN), 100, , Local2)
125	Store (Local2, Index (Arg1, 6))
126
127	// Design Capacity of Low
128	Divide (Multiply (Local0, DLOW), 100, , Local2)
129	Store (Local2, Index (Arg1, 7))
130
131	// Cycle Count
132	Store (BTCC, Index (Arg1, 8))
133
134	// Get battery info from mainboard
135	Store (ToString(Concatenate(BMOD, 0x00)), Index (Arg1, 16))
136	Store (ToString(Concatenate(BSER, 0x00)), Index (Arg1, 17))
137	Store (ToString(Concatenate(BMFG, 0x00)), Index (Arg1, 19))
138
139	Release (^BATM)
140	Return (Arg1)
141}
142
143// _BST implementation.
144//   Arg0 = battery index
145//   Arg1 = PBST
146//   Arg2 = BSTP
147//   Arg3 = BFWK
148Method (BBST, 4, Serialized)
149{
150	If (Acquire (^BATM, 1000)) {
151		Return (Arg1)
152	}
153
154	If (BTSW (Arg0)) {
155		Release (^BATM)
156		Return (Arg1)
157	}
158	//
159	// 0: BATTERY STATE
160	//
161	// bit 0 = discharging
162	// bit 1 = charging
163	// bit 2 = critical level
164	//
165	Store (Zero, Local1)
166
167	// Check if AC is present
168	If (ACEX) {
169		If (BFCG) {
170			Store (0x02, Local1)
171		} ElseIf (BFDC) {
172			Store (0x01, Local1)
173		}
174	} Else {
175		// Always discharging when on battery power
176		Store (0x01, Local1)
177	}
178
179	// Check for critical battery level
180	If (BFCR) {
181		Or (Local1, 0x04, Local1)
182	}
183	Store (Local1, Index (Arg1, 0))
184
185	// Notify if battery state has changed since last time
186	If (LNotEqual (Local1, DeRefOf (Arg2))) {
187		Store (Local1, Arg2)
188		If (LEqual(Arg0, 0)) {
189			Notify (BAT0, 0x80)
190		}
191#ifdef EC_ENABLE_SECOND_BATTERY_DEVICE
192		Else {
193			Notify (BAT1, 0x80)
194		}
195#endif
196	}
197
198	//
199	// 1: BATTERY PRESENT RATE
200	//
201	Store (BTPR, Index (Arg1, 1))
202
203	//
204	// 2: BATTERY REMAINING CAPACITY
205	//
206	Store (BTRA, Local1)
207	If (LAnd (Arg3, LAnd (ACEX, LNot (LAnd (BFDC, BFCG))))) {
208		// On AC power and battery is neither charging
209		// nor discharging.  Linux expects a full battery
210		// to report same capacity as last full charge.
211		// https://bugzilla.kernel.org/show_bug.cgi?id=12632
212		Store (BTDF, Local2)
213
214		// See if within ~6% of full
215		ShiftRight (Local2, 4, Local3)
216		If (LAnd (LGreater (Local1, Subtract (Local2, Local3)),
217		          LLess (Local1, Add (Local2, Local3))))
218		{
219			Store (Local2, Local1)
220		}
221	}
222	Store (Local1, Index (Arg1, 2))
223
224	//
225	// 3: BATTERY PRESENT VOLTAGE
226	//
227	Store (BTVO, Index (Arg1, 3))
228
229	Release (^BATM)
230	Return (Arg1)
231}
232
233Device (BAT0)
234{
235	Name (_HID, EISAID ("PNP0C0A"))
236	Name (_UID, 1)
237	Name (_PCL, Package () { \_SB })
238
239	Name (PBIF, Package () {
240		0x00000001,  // 0x00: Power Unit: mAh
241		0xFFFFFFFF,  // 0x01: Design Capacity
242		0xFFFFFFFF,  // 0x02: Last Full Charge Capacity
243		0x00000001,  // 0x03: Battery Technology: Rechargeable
244		0xFFFFFFFF,  // 0x04: Design Voltage
245		0x00000003,  // 0x05: Design Capacity of Warning
246		0xFFFFFFFF,  // 0x06: Design Capacity of Low
247		0x00000001,  // 0x07: Capacity Granularity 1
248		0x00000001,  // 0x08: Capacity Granularity 2
249		"",          // 0x09: Model Number
250		"",          // 0x0a: Serial Number
251		"LION",      // 0x0b: Battery Type
252		""           // 0x0c: OEM Information
253	})
254
255	Name (PBIX, Package () {
256		0x00000000,  // 0x00: Revision
257		0x00000001,  // 0x01: Power Unit: mAh
258		0xFFFFFFFF,  // 0x02: Design Capacity
259		0xFFFFFFFF,  // 0x03: Last Full Charge Capacity
260		0x00000001,  // 0x04: Battery Technology: Rechargeable
261		0xFFFFFFFF,  // 0x05: Design Voltage
262		0x00000003,  // 0x06: Design Capacity of Warning
263		0xFFFFFFFF,  // 0x07: Design Capacity of Low
264		0x00000000,  // 0x08: Cycle Count
265		0x00018000,  // 0x09: Measurement Accuracy (98.3%?)
266		0x000001F4,  // 0x0a: Max Sampling Time (500ms)
267		0x0000000a,  // 0x0b: Min Sampling Time (10ms)
268		0xFFFFFFFF,  // 0x0c: Max Averaging Interval
269		0xFFFFFFFF,  // 0x0d: Min Averaging Interval
270		0x00000001,  // 0x0e: Capacity Granularity 1
271		0x00000001,  // 0x0f: Capacity Granularity 2
272		"",          // 0x10 Model Number
273		"",          // 0x11: Serial Number
274		"LION",      // 0x12: Battery Type
275		""           // 0x13: OEM Information
276	})
277
278	Name (PBST, Package () {
279		0x00000000,  // 0x00: Battery State
280		0xFFFFFFFF,  // 0x01: Battery Present Rate
281		0xFFFFFFFF,  // 0x02: Battery Remaining Capacity
282		0xFFFFFFFF,  // 0x03: Battery Present Voltage
283	})
284	Name (BSTP, Zero)
285
286	// Workaround for full battery status, disabled by default
287	Name (BFWK, Zero)
288
289	// Method to enable full battery workaround
290	Method (BFWE)
291	{
292		Store (One, BFWK)
293	}
294
295	// Method to disable full battery workaround
296	Method (BFWD)
297	{
298		Store (Zero, BFWK)
299	}
300
301	Method (_STA, 0, Serialized)
302	{
303		Return (BSTA (0))
304	}
305
306	Method (_BIF, 0, Serialized)
307	{
308		Return (BBIF (0, PBIF))
309	}
310
311	Method (_BIX, 0, Serialized)
312	{
313		Return (BBIX (0, PBIX))
314	}
315
316	Method (_BST, 0, Serialized)
317	{
318		Return (BBST (0, PBST, RefOf (BSTP), BFWK))
319	}
320}
321
322#ifdef EC_ENABLE_SECOND_BATTERY_DEVICE
323Device (BAT1)
324{
325	Name (_HID, EISAID ("PNP0C0A"))
326	Name (_UID, 1)
327	Name (_PCL, Package () { \_SB })
328
329	Name (PBIF, Package () {
330		0x00000001,  // 0x00: Power Unit: mAh
331		0xFFFFFFFF,  // 0x01: Design Capacity
332		0xFFFFFFFF,  // 0x02: Last Full Charge Capacity
333		0x00000001,  // 0x03: Battery Technology: Rechargeable
334		0xFFFFFFFF,  // 0x04: Design Voltage
335		0x00000003,  // 0x05: Design Capacity of Warning
336		0xFFFFFFFF,  // 0x06: Design Capacity of Low
337		0x00000001,  // 0x07: Capacity Granularity 1
338		0x00000001,  // 0x08: Capacity Granularity 2
339		"",          // 0x09: Model Number
340		"",          // 0x0a: Serial Number
341		"LION",      // 0x0b: Battery Type
342		""           // 0x0c: OEM Information
343	})
344
345	Name (PBIX, Package () {
346		0x00000000,  // 0x00: Revision
347		0x00000001,  // 0x01: Power Unit: mAh
348		0xFFFFFFFF,  // 0x02: Design Capacity
349		0xFFFFFFFF,  // 0x03: Last Full Charge Capacity
350		0x00000001,  // 0x04: Battery Technology: Rechargeable
351		0xFFFFFFFF,  // 0x05: Design Voltage
352		0x00000003,  // 0x06: Design Capacity of Warning
353		0xFFFFFFFF,  // 0x07: Design Capacity of Low
354		0x00000000,  // 0x08: Cycle Count
355		0x00018000,  // 0x09: Measurement Accuracy (98.3%?)
356		0x000001F4,  // 0x0a: Max Sampling Time (500ms)
357		0x0000000a,  // 0x0b: Min Sampling Time (10ms)
358		0xFFFFFFFF,  // 0x0c: Max Averaging Interval
359		0xFFFFFFFF,  // 0x0d: Min Averaging Interval
360		0x00000001,  // 0x0e: Capacity Granularity 1
361		0x00000001,  // 0x0f: Capacity Granularity 2
362		"",          // 0x10 Model Number
363		"",          // 0x11: Serial Number
364		"LION",      // 0x12: Battery Type
365		""           // 0x13: OEM Information
366	})
367
368	Name (PBST, Package () {
369		0x00000000,  // 0x00: Battery State
370		0xFFFFFFFF,  // 0x01: Battery Present Rate
371		0xFFFFFFFF,  // 0x02: Battery Remaining Capacity
372		0xFFFFFFFF,  // 0x03: Battery Present Voltage
373	})
374	Name (BSTP, Zero)
375
376	// Workaround for full battery status, disabled by default
377	Name (BFWK, Zero)
378
379	// Method to enable full battery workaround
380	Method (BFWE)
381	{
382		Store (One, BFWK)
383	}
384
385	// Method to disable full battery workaround
386	Method (BFWD)
387	{
388		Store (Zero, BFWK)
389	}
390
391	Method (_STA, 0, Serialized)
392	{
393		Return (BSTA (1))
394	}
395
396	Method (_BIF, 0, Serialized)
397	{
398		Return (BBIF (1, PBIF))
399	}
400
401	Method (_BIX, 0, Serialized)
402	{
403		Return (BBIX (1, PBIX))
404	}
405
406	Method (_BST, 0, Serialized)
407	{
408		Return (BBST (1, PBST, RefOf (BSTP), BFWK))
409	}
410}
411#endif
412