1/* SPDX-License-Identifier: GPL-2.0 */
2/*
3 * Copyright (C) 2012 The Chromium OS Authors. All rights reserved.
4 */
5
6/*
7 * The mainboard must define a PNOT method to handle power
8 * state notifications and Notify CPU device objects to
9 * re-evaluate their _PPC and _CST tables.
10 */
11
12// Mainboard specific throttle handler
13#ifdef DPTF_ENABLE_CHARGER
14External (\_SB.DPTF.TCHG, DeviceObj)
15#endif
16
17
18Device (EC0)
19{
20	Name (_HID, EISAID ("PNP0C09"))
21	Name (_UID, 1)
22	Name (_GPE, EC_SCI_GPI)
23	Name (TOFS, EC_TEMP_SENSOR_OFFSET)
24	Name (TNCA, EC_TEMP_SENSOR_NOT_CALIBRATED)
25	Name (TNOP, EC_TEMP_SENSOR_NOT_POWERED)
26	Name (TBAD, EC_TEMP_SENSOR_ERROR)
27	Name (TNPR, EC_TEMP_SENSOR_NOT_PRESENT)
28	Name (DWRN, 15)		// Battery capacity warning at 15%
29	Name (DLOW, 10)		// Battery capacity low at 10%
30
31	OperationRegion (ERAM, EmbeddedControl, 0x00, EC_ACPI_MEM_MAPPED_BEGIN)
32	Field (ERAM, ByteAcc, Lock, Preserve)
33	{
34		Offset (0x00),
35		RAMV, 8,	// EC RAM Version
36		TSTB, 8,	// Test Byte
37		TSTC, 8,	// Complement of Test Byte
38		KBLV, 8,	// Keyboard Backlight value
39		FAND, 8,	// Set Fan Duty Cycle
40		PATI, 8,	// Programmable Auxiliary Trip Sensor ID
41		PATT, 8,	// Programmable Auxiliary Trip Threshold
42		PATC, 8,	// Programmable Auxiliary Trip Commit
43		CHGL, 8,	// Charger Current Limit
44		TBMD, 1,	// Tablet mode
45		DDPN, 3,	// Device DPTF Profile Number
46		// DFUD must be 0 for the other 31 values to be valid
47		Offset (0x0a),
48		DFUD, 1,	// Device Features Undefined
49		FLSH, 1,	// Flash commands present
50		PFAN, 1,	// PWM Fan control present
51		KBLE, 1,	// Keyboard Backlight present
52		LTBR, 1,	// Lightbar present
53		LEDC, 1,	// LED control
54		MTNS, 1,	// Motion sensors present
55		KEYB, 1,	// EC is keyboard controller
56		PSTR, 1,	// Persistent storage
57		P80P, 1,	// EC serves I/O Port 80h
58		THRM, 1,	// EC supports thermal management
59		SBKL, 1,	// Screen backlight switch present
60		WIFI, 1,	// WIFI switch present
61		HOST, 1,	// EC monitors host events (eg SCI, SMI)
62		GPIO, 1,	// EC provides GPIO commands
63		I2CB, 1,	// EC provides I2C controller access
64		CHRG, 1,	// EC provides commands for charger control
65		BATT, 1,	// Simply Battery support
66		SBAT, 1,	// Smart Battery support
67		HANG, 1,	// EC can detect host hang
68		PMUI, 1,	// Power Information
69		DSEC, 1,	// another EC exists downstream
70		UPDC, 1,	// supports USB Power Delivery
71		UMUX, 1,	// supports USB Mux
72		MSFF, 1,	// Motion Sense has FIFO
73		TVST, 1,	// supports temporary secure vstore
74		TCMV, 1,	// USB Type C Muxing is virtual (host assisted)
75		RTCD, 1,	// EC provides an RTC device
76		FPRD, 1,	// EC provides a fingerprint reader device
77		TPAD, 1,	// EC provides a touchpad device
78		RWSG, 1,	// EC has RWSIG task enabled
79		DEVE, 1,	// EC supports device events
80		// make sure we're within our space envelope
81		Offset (0x0e),
82		Offset (0x12),
83		BTID, 8,	// Battery index that host wants to read
84		USPP, 8,	// USB Port Power
85}
86
87#if IS_ENABLED(CONFIG_EC_GOOGLE_CHROMEEC_ACPI_MEMMAP)
88	OperationRegion (EMEM, EmbeddedControl,
89			 EC_ACPI_MEM_MAPPED_BEGIN, EC_ACPI_MEM_MAPPED_SIZE)
90	Field (EMEM, ByteAcc, Lock, Preserve)
91#else
92	OperationRegion (EMEM, SystemIO, EC_LPC_ADDR_MEMMAP, EC_MEMMAP_SIZE)
93	Field (EMEM, ByteAcc, NoLock, Preserve)
94#endif
95	{
96		#include "emem.asl"
97	}
98
99#ifdef EC_ENABLE_LID_SWITCH
100	/* LID Switch */
101	Device (LID0)
102	{
103		Name (_HID, EisaId ("PNP0C0D"))
104		Method (_LID, 0)
105		{
106			Return (^^LIDS)
107		}
108
109#ifdef EC_ENABLE_WAKE_PIN
110		Name (_PRW, Package () { EC_ENABLE_WAKE_PIN, 0x5 })
111#endif
112	}
113#endif
114
115	Method (TINS, 1, Serialized)
116	{
117		Switch (ToInteger (Arg0))
118		{
119			Case (0) { Return (TIN0) }
120			Case (1) { Return (TIN1) }
121			Case (2) { Return (TIN2) }
122			Case (3) { Return (TIN3) }
123			Case (4) { Return (TIN4) }
124			Case (5) { Return (TIN5) }
125			Case (6) { Return (TIN6) }
126			Case (7) { Return (TIN7) }
127			Case (8) { Return (TIN8) }
128			Case (9) { Return (TIN9) }
129			Default  { Return (TIN0) }
130		}
131	}
132
133	Method (_CRS, 0, Serialized)
134	{
135		Name (ECMD, ResourceTemplate()
136		{
137			IO (Decode16,
138			    EC_LPC_ADDR_ACPI_DATA,
139			    EC_LPC_ADDR_ACPI_DATA,
140			    0, 1)
141			IO (Decode16,
142			    EC_LPC_ADDR_ACPI_CMD,
143			    EC_LPC_ADDR_ACPI_CMD,
144			    0, 1)
145		})
146		Return (ECMD)
147	}
148
149	Method (_REG, 2, NotSerialized)
150	{
151		// Initialize AC power state
152		Store (ACEX, \PWRS)
153
154		// Initialize LID switch state
155		Store (LIDS, \LIDS)
156	}
157
158	/* Read requested temperature and check against EC error values */
159	Method (TSRD, 1, Serialized)
160	{
161		Store (\_SB.PCI0.LPCB.EC0.TINS (Arg0), Local0)
162
163		/* Check for sensor not calibrated */
164		If (LEqual (Local0, \_SB.PCI0.LPCB.EC0.TNCA)) {
165			Return (Zero)
166		}
167
168		/* Check for sensor not present */
169		If (LEqual (Local0, \_SB.PCI0.LPCB.EC0.TNPR)) {
170			Return (Zero)
171		}
172
173		/* Check for sensor not powered */
174		If (LEqual (Local0, \_SB.PCI0.LPCB.EC0.TNOP)) {
175			Return (Zero)
176		}
177
178		/* Check for sensor bad reading */
179		If (LEqual (Local0, \_SB.PCI0.LPCB.EC0.TBAD)) {
180			Return (Zero)
181		}
182
183		/* Adjust by offset to get Kelvin */
184		Add (\_SB.PCI0.LPCB.EC0.TOFS, Local0, Local0)
185
186		/* Convert to 1/10 Kelvin */
187		Multiply (Local0, 10, Local0)
188
189		Return (Local0)
190	}
191
192	// Lid Closed Event
193	Method (_Q01, 0, NotSerialized)
194	{
195		Store ("EC: LID CLOSE", Debug)
196		Store (LIDS, \LIDS)
197#ifdef EC_ENABLE_LID_SWITCH
198		Notify (LID0, 0x80)
199#endif
200	}
201
202	// Lid Open Event
203	Method (_Q02, 0, NotSerialized)
204	{
205		Store ("EC: LID OPEN", Debug)
206		Store (LIDS, \LIDS)
207		Notify (CREC, 0x2)
208#ifdef EC_ENABLE_LID_SWITCH
209		Notify (LID0, 0x80)
210#endif
211	}
212
213	// Power Button
214	Method (_Q03, 0, NotSerialized)
215	{
216		Store ("EC: POWER BUTTON", Debug)
217	}
218
219	// AC Connected
220	Method (_Q04, 0, NotSerialized)
221	{
222		Store ("EC: AC CONNECTED", Debug)
223		Store (ACEX, \PWRS)
224		Notify (AC, 0x80)
225#ifdef DPTF_ENABLE_CHARGER
226		If (CondRefOf (\_SB.DPTF.TCHG)) {
227			Notify (\_SB.DPTF.TCHG, 0x80)
228		}
229#endif
230		\PNOT ()
231	}
232
233	// AC Disconnected
234	Method (_Q05, 0, NotSerialized)
235	{
236		Store ("EC: AC DISCONNECTED", Debug)
237		Store (ACEX, \PWRS)
238		Notify (AC, 0x80)
239#ifdef DPTF_ENABLE_CHARGER
240		If (CondRefOf (\_SB.DPTF.TCHG)) {
241			Notify (\_SB.DPTF.TCHG, 0x80)
242		}
243#endif
244		\PNOT ()
245	}
246
247	// Battery Low Event
248	Method (_Q06, 0, NotSerialized)
249	{
250		Store ("EC: BATTERY LOW", Debug)
251		Notify (BAT0, 0x80)
252	}
253
254	// Battery Critical Event
255	Method (_Q07, 0, NotSerialized)
256	{
257		Store ("EC: BATTERY CRITICAL", Debug)
258		Notify (BAT0, 0x80)
259	}
260
261	// Battery Info Event
262	Method (_Q08, 0, NotSerialized)
263	{
264		Store ("EC: BATTERY INFO", Debug)
265		Notify (BAT0, 0x81)
266#ifdef EC_ENABLE_SECOND_BATTERY_DEVICE
267		If (CondRefOf (BAT1)) {
268			Notify (BAT1, 0x81)
269		}
270#endif
271	}
272
273	// Thermal Overload Event
274	Method (_Q0A, 0, NotSerialized)
275	{
276		Store ("EC: THERMAL OVERLOAD", Debug)
277		Notify (\_TZ, 0x80)
278	}
279
280	// Thermal Event
281	Method (_Q0B, 0, NotSerialized)
282	{
283		Store ("EC: THERMAL", Debug)
284		Notify (\_TZ, 0x80)
285	}
286
287	// USB Charger
288	Method (_Q0C, 0, NotSerialized)
289	{
290		Store ("EC: USB CHARGER", Debug)
291	}
292
293	// Key Pressed
294	Method (_Q0D, 0, NotSerialized)
295	{
296		Store ("EC: KEY PRESSED", Debug)
297		Notify (CREC, 0x2)
298	}
299
300	// Thermal Shutdown Imminent
301	Method (_Q10, 0, NotSerialized)
302	{
303		Store ("EC: THERMAL SHUTDOWN", Debug)
304		Notify (\_TZ, 0x80)
305	}
306
307	// Battery Shutdown Imminent
308	Method (_Q11, 0, NotSerialized)
309	{
310		Store ("EC: BATTERY SHUTDOWN", Debug)
311		Notify (BAT0, 0x80)
312	}
313
314	// Throttle Start
315	Method (_Q12, 0, NotSerialized)
316	{
317#ifdef EC_ENABLE_THROTTLING_HANDLER
318		Store ("EC: THROTTLE START", Debug)
319		\_TZ.THRT (1)
320#endif
321	}
322
323	// Throttle Stop
324	Method (_Q13, 0, NotSerialized)
325	{
326#ifdef EC_ENABLE_THROTTLING_HANDLER
327		Store ("EC: THROTTLE STOP", Debug)
328		\_TZ.THRT (0)
329#endif
330	}
331
332#ifdef EC_ENABLE_PD_MCU_DEVICE
333	// PD event
334	Method (_Q16, 0, NotSerialized)
335	{
336		Store ("EC: GOT PD EVENT", Debug)
337		Notify (ECPD, 0x80)
338	}
339#endif
340
341	// Battery Status
342	Method (_Q17, 0, NotSerialized)
343	{
344		Store ("EC: BATTERY STATUS", Debug)
345		Notify (BAT0, 0x80)
346#ifdef EC_ENABLE_SECOND_BATTERY_DEVICE
347		If (CondRefOf (BAT1)) {
348			Notify (BAT1, 0x80)
349		}
350#endif
351	}
352
353	// MKBP interrupt.
354	Method (_Q1B, 0, NotSerialized)
355	{
356		Store ("EC: MKBP", Debug)
357		Notify (CREC, 0x80)
358	}
359
360	// TABLET mode switch Event
361	Method (_Q1D, 0, NotSerialized)
362	{
363		Store ("EC: TABLET mode switch Event", Debug)
364		Notify (CREC, 0x2)
365#ifdef EC_ENABLE_MULTIPLE_DPTF_PROFILES
366		\_SB.DPTF.TPET()
367#endif
368#ifdef EC_ENABLE_TBMC_DEVICE
369		Notify (TBMC, 0x80)
370#endif
371	}
372
373	/*
374	 * Dynamic Platform Thermal Framework support
375	 */
376
377	/* Mutex for EC PAT interface */
378	Mutex (PATM, 1)
379
380	/*
381	 * Set Aux Trip Point 0
382	 *   Arg0 = Temp Sensor ID
383	 *   Arg1 = Value to set
384	 */
385	Method (PAT0, 2, Serialized)
386	{
387		If (Acquire (^PATM, 1000)) {
388			Return (0)
389		}
390
391		/* Set sensor ID */
392		Store (ToInteger (Arg0), ^PATI)
393
394		/* Temperature is passed in 1/10 Kelvin */
395		Divide (ToInteger (Arg1), 10, , Local1)
396
397		/* Adjust by EC temperature offset */
398		Subtract (Local1, ^TOFS, ^PATT)
399
400		/* Set commit value with SELECT=0 and ENABLE=1 */
401		Store (0x02, ^PATC)
402
403		Release (^PATM)
404		Return (1)
405	}
406
407	/*
408	 * Set Aux Trip Point 1
409	 *   Arg0 = Temp Sensor ID
410	 *   Arg1 = Value to set
411	 */
412	Method (PAT1, 2, Serialized)
413	{
414		If (Acquire (^PATM, 1000)) {
415			Return (0)
416		}
417
418		/* Set sensor ID */
419		Store (ToInteger (Arg0), ^PATI)
420
421		/* Temperature is passed in 1/10 Kelvin */
422		Divide (ToInteger (Arg1), 10, , Local1)
423
424		/* Adjust by EC temperature offset */
425		Subtract (Local1, ^TOFS, ^PATT)
426
427		/* Set commit value with SELECT=1 and ENABLE=1 */
428		Store (0x03, ^PATC)
429
430		Release (^PATM)
431		Return (1)
432	}
433
434	/* Disable Aux Trip Points
435	 *   Arg0 = Temp Sensor ID
436	 */
437	Method (PATD, 1, Serialized)
438	{
439		If (Acquire (^PATM, 1000)) {
440			Return (0)
441		}
442
443		Store (ToInteger (Arg0), ^PATI)
444		Store (0x00, ^PATT)
445
446		/* Disable PAT0 */
447		Store (0x00, ^PATC)
448
449		/* Disable PAT1 */
450		Store (0x01, ^PATC)
451
452		Release (^PATM)
453		Return (1)
454	}
455
456	/*
457	 * Thermal Threshold Event
458	 */
459	Method (_Q09, 0, NotSerialized)
460	{
461		If (LNot(Acquire (^PATM, 1000))) {
462			/* Read sensor ID for event */
463			Store (^PATI, Local0)
464
465			/* When sensor ID returns 0xFF then no more events */
466			While (LNotEqual (Local0, EC_TEMP_SENSOR_NOT_PRESENT))
467			{
468#ifdef HAVE_THERM_EVENT_HANDLER
469				\_SB.DPTF.TEVT (Local0)
470#endif
471
472				/* Keep reaading sensor ID for event */
473				Store (^PATI, Local0)
474			}
475
476			Release (^PATM)
477		}
478	}
479
480	/*
481	 * Set Charger Current Limit
482	 *   Arg0 = Current Limit in 64mA steps
483	 */
484	Method (CHGS, 1, Serialized)
485	{
486		Store (ToInteger (Arg0), ^CHGL)
487	}
488
489	/*
490	 * Disable Charger Current Limit
491	 */
492	Method (CHGD, 0, Serialized)
493	{
494		Store (0xFF, ^CHGL)
495	}
496
497	/* Read current Tablet mode */
498	Method (RCTM, 0, NotSerialized)
499	{
500		Return (^TBMD)
501	}
502
503	/* Read current Device DPTF Profile Number */
504	Method (RCDP, 0, NotSerialized)
505	{
506		/*
507		 * DDPN = 0 is reserved for backwards compatibility.
508		 * If DDPN == 0 use TBMD to load appropriate DPTF table.
509		 */
510		If (LEqual (^DDPN, 0)) {
511			Return (^TBMD)
512		} Else {
513			Subtract (^DDPN, 1, Local0)
514			Return (Local0)
515		}
516	}
517
518#if IS_ENABLED(CONFIG_EC_GOOGLE_CHROMEEC_ACPI_USB_PORT_POWER)
519	/*
520	 * Enable USB Port Power
521	 *   Arg0 = USB port ID
522	 */
523	Method (UPPS, 1, Serialized)
524	{
525		Or (USPP, ShiftLeft (1, Arg0), USPP)
526	}
527
528	/*
529	 * Disable USB Port Power
530	 *   Arg0 = USB port ID
531	 */
532	Method (UPPC, 1, Serialized)
533	{
534		And (USPP, Not (ShiftLeft (1, Arg0)), USPP)
535	}
536#endif
537
538	#include "ac.asl"
539	#include "battery.asl"
540	#include "cros_ec.asl"
541
542#ifdef EC_ENABLE_ALS_DEVICE
543	#include "als.asl"
544#endif
545
546#ifdef EC_ENABLE_KEYBOARD_BACKLIGHT
547	#include "keyboard_backlight.asl"
548#endif
549
550#ifdef EC_ENABLE_PD_MCU_DEVICE
551	#include "pd.asl"
552#endif
553
554#ifdef EC_ENABLE_TBMC_DEVICE
555	#include "tbmc.asl"
556#endif
557}
558