1 // SPDX-License-Identifier: GPL-2.0-only
2 /* intel_pch_thermal.c - Intel PCH Thermal driver
4 * Copyright (c) 2015, Intel Corporation.
7 * Tushar Dave <tushar.n.dave@intel.com>
10 #include <linux/module.h>
11 #include <linux/types.h>
12 #include <linux/init.h>
13 #include <linux/pci.h>
14 #include <linux/acpi.h>
15 #include <linux/thermal.h>
16 #include <linux/units.h>
19 /* Intel PCH thermal Device IDs */
20 #define PCH_THERMAL_DID_HSW_1 0x9C24 /* Haswell PCH */
21 #define PCH_THERMAL_DID_HSW_2 0x8C24 /* Haswell PCH */
22 #define PCH_THERMAL_DID_WPT 0x9CA4 /* Wildcat Point */
23 #define PCH_THERMAL_DID_SKL 0x9D31 /* Skylake PCH */
24 #define PCH_THERMAL_DID_SKL_H 0xA131 /* Skylake PCH 100 series */
25 #define PCH_THERMAL_DID_CNL 0x9Df9 /* CNL PCH */
26 #define PCH_THERMAL_DID_CNL_H 0xA379 /* CNL-H PCH */
27 #define PCH_THERMAL_DID_CML_H 0X06F9 /* CML-H PCH */
29 /* Wildcat Point-LP PCH Thermal registers */
30 #define WPT_TEMP 0x0000 /* Temperature */
31 #define WPT_TSC 0x04 /* Thermal Sensor Control */
32 #define WPT_TSS 0x06 /* Thermal Sensor Status */
33 #define WPT_TSEL 0x08 /* Thermal Sensor Enable and Lock */
34 #define WPT_TSREL 0x0A /* Thermal Sensor Report Enable and Lock */
35 #define WPT_TSMIC 0x0C /* Thermal Sensor SMI Control */
36 #define WPT_CTT 0x0010 /* Catastrophic Trip Point */
37 #define WPT_TAHV 0x0014 /* Thermal Alert High Value */
38 #define WPT_TALV 0x0018 /* Thermal Alert Low Value */
39 #define WPT_TL 0x00000040 /* Throttle Value */
40 #define WPT_PHL 0x0060 /* PCH Hot Level */
41 #define WPT_PHLC 0x62 /* PHL Control */
42 #define WPT_TAS 0x80 /* Thermal Alert Status */
43 #define WPT_TSPIEN 0x82 /* PCI Interrupt Event Enables */
44 #define WPT_TSGPEN 0x84 /* General Purpose Event Enables */
46 /* Wildcat Point-LP PCH Thermal Register bit definitions */
47 #define WPT_TEMP_TSR 0x01ff /* Temp TS Reading */
48 #define WPT_TSC_CPDE 0x01 /* Catastrophic Power-Down Enable */
49 #define WPT_TSS_TSDSS 0x10 /* Thermal Sensor Dynamic Shutdown Status */
50 #define WPT_TSS_GPES 0x08 /* GPE status */
51 #define WPT_TSEL_ETS 0x01 /* Enable TS */
52 #define WPT_TSEL_PLDB 0x80 /* TSEL Policy Lock-Down Bit */
53 #define WPT_TL_TOL 0x000001FF /* T0 Level */
54 #define WPT_TL_T1L 0x1ff00000 /* T1 Level */
55 #define WPT_TL_TTEN 0x20000000 /* TT Enable */
57 static char driver_name
[] = "Intel PCH thermal driver";
59 struct pch_thermal_device
{
60 void __iomem
*hw_base
;
61 const struct pch_dev_ops
*ops
;
63 struct thermal_zone_device
*tzd
;
65 unsigned long crt_temp
;
67 unsigned long hot_temp
;
69 unsigned long psv_temp
;
76 * On some platforms, there is a companion ACPI device, which adds
77 * passive trip temperature using _PSV method. There is no specific
78 * passive temperature setting in MMIO interface of this PCI device.
80 static void pch_wpt_add_acpi_psv_trip(struct pch_thermal_device
*ptd
,
83 struct acpi_device
*adev
;
85 ptd
->psv_trip_id
= -1;
87 adev
= ACPI_COMPANION(&ptd
->pdev
->dev
);
92 status
= acpi_evaluate_integer(adev
->handle
, "_PSV", NULL
,
94 if (ACPI_SUCCESS(status
)) {
95 unsigned long trip_temp
;
97 trip_temp
= deci_kelvin_to_millicelsius(r
);
99 ptd
->psv_temp
= trip_temp
;
100 ptd
->psv_trip_id
= *nr_trips
;
107 static void pch_wpt_add_acpi_psv_trip(struct pch_thermal_device
*ptd
,
110 ptd
->psv_trip_id
= -1;
115 static int pch_wpt_init(struct pch_thermal_device
*ptd
, int *nr_trips
)
122 /* Check if BIOS has already enabled thermal sensor */
123 if (WPT_TSEL_ETS
& readb(ptd
->hw_base
+ WPT_TSEL
)) {
124 ptd
->bios_enabled
= true;
128 tsel
= readb(ptd
->hw_base
+ WPT_TSEL
);
130 * When TSEL's Policy Lock-Down bit is 1, TSEL become RO.
131 * If so, thermal sensor cannot enable. Bail out.
133 if (tsel
& WPT_TSEL_PLDB
) {
134 dev_err(&ptd
->pdev
->dev
, "Sensor can't be enabled\n");
138 writeb(tsel
|WPT_TSEL_ETS
, ptd
->hw_base
+ WPT_TSEL
);
139 if (!(WPT_TSEL_ETS
& readb(ptd
->hw_base
+ WPT_TSEL
))) {
140 dev_err(&ptd
->pdev
->dev
, "Sensor can't be enabled\n");
145 ptd
->crt_trip_id
= -1;
146 trip_temp
= readw(ptd
->hw_base
+ WPT_CTT
);
149 /* Resolution of 1/2 degree C and an offset of -50C */
150 ptd
->crt_temp
= trip_temp
* 1000 / 2 - 50000;
151 ptd
->crt_trip_id
= 0;
155 ptd
->hot_trip_id
= -1;
156 trip_temp
= readw(ptd
->hw_base
+ WPT_PHL
);
159 /* Resolution of 1/2 degree C and an offset of -50C */
160 ptd
->hot_temp
= trip_temp
* 1000 / 2 - 50000;
161 ptd
->hot_trip_id
= *nr_trips
;
165 pch_wpt_add_acpi_psv_trip(ptd
, nr_trips
);
170 static int pch_wpt_get_temp(struct pch_thermal_device
*ptd
, int *temp
)
174 wpt_temp
= WPT_TEMP_TSR
& readw(ptd
->hw_base
+ WPT_TEMP
);
176 /* Resolution of 1/2 degree C and an offset of -50C */
177 *temp
= (wpt_temp
* 1000 / 2 - 50000);
182 static int pch_wpt_suspend(struct pch_thermal_device
*ptd
)
186 if (ptd
->bios_enabled
)
189 tsel
= readb(ptd
->hw_base
+ WPT_TSEL
);
191 writeb(tsel
& 0xFE, ptd
->hw_base
+ WPT_TSEL
);
196 static int pch_wpt_resume(struct pch_thermal_device
*ptd
)
200 if (ptd
->bios_enabled
)
203 tsel
= readb(ptd
->hw_base
+ WPT_TSEL
);
205 writeb(tsel
| WPT_TSEL_ETS
, ptd
->hw_base
+ WPT_TSEL
);
211 int (*hw_init
)(struct pch_thermal_device
*ptd
, int *nr_trips
);
212 int (*get_temp
)(struct pch_thermal_device
*ptd
, int *temp
);
213 int (*suspend
)(struct pch_thermal_device
*ptd
);
214 int (*resume
)(struct pch_thermal_device
*ptd
);
218 /* dev ops for Wildcat Point */
219 static const struct pch_dev_ops pch_dev_ops_wpt
= {
220 .hw_init
= pch_wpt_init
,
221 .get_temp
= pch_wpt_get_temp
,
222 .suspend
= pch_wpt_suspend
,
223 .resume
= pch_wpt_resume
,
226 static int pch_thermal_get_temp(struct thermal_zone_device
*tzd
, int *temp
)
228 struct pch_thermal_device
*ptd
= tzd
->devdata
;
230 return ptd
->ops
->get_temp(ptd
, temp
);
233 static int pch_get_trip_type(struct thermal_zone_device
*tzd
, int trip
,
234 enum thermal_trip_type
*type
)
236 struct pch_thermal_device
*ptd
= tzd
->devdata
;
238 if (ptd
->crt_trip_id
== trip
)
239 *type
= THERMAL_TRIP_CRITICAL
;
240 else if (ptd
->hot_trip_id
== trip
)
241 *type
= THERMAL_TRIP_HOT
;
242 else if (ptd
->psv_trip_id
== trip
)
243 *type
= THERMAL_TRIP_PASSIVE
;
250 static int pch_get_trip_temp(struct thermal_zone_device
*tzd
, int trip
, int *temp
)
252 struct pch_thermal_device
*ptd
= tzd
->devdata
;
254 if (ptd
->crt_trip_id
== trip
)
255 *temp
= ptd
->crt_temp
;
256 else if (ptd
->hot_trip_id
== trip
)
257 *temp
= ptd
->hot_temp
;
258 else if (ptd
->psv_trip_id
== trip
)
259 *temp
= ptd
->psv_temp
;
266 static struct thermal_zone_device_ops tzd_ops
= {
267 .get_temp
= pch_thermal_get_temp
,
268 .get_trip_type
= pch_get_trip_type
,
269 .get_trip_temp
= pch_get_trip_temp
,
280 static const struct board_info
{
282 const struct pch_dev_ops
*ops
;
285 .name
= "pch_haswell",
286 .ops
= &pch_dev_ops_wpt
,
289 .name
= "pch_wildcat_point",
290 .ops
= &pch_dev_ops_wpt
,
293 .name
= "pch_skylake",
294 .ops
= &pch_dev_ops_wpt
,
297 .name
= "pch_cannonlake",
298 .ops
= &pch_dev_ops_wpt
,
301 .name
= "pch_cometlake",
302 .ops
= &pch_dev_ops_wpt
,
306 static int intel_pch_thermal_probe(struct pci_dev
*pdev
,
307 const struct pci_device_id
*id
)
309 enum board_ids board_id
= id
->driver_data
;
310 const struct board_info
*bi
= &board_info
[board_id
];
311 struct pch_thermal_device
*ptd
;
315 ptd
= devm_kzalloc(&pdev
->dev
, sizeof(*ptd
), GFP_KERNEL
);
321 pci_set_drvdata(pdev
, ptd
);
324 err
= pci_enable_device(pdev
);
326 dev_err(&pdev
->dev
, "failed to enable pci device\n");
330 err
= pci_request_regions(pdev
, driver_name
);
332 dev_err(&pdev
->dev
, "failed to request pci region\n");
336 ptd
->hw_base
= pci_ioremap_bar(pdev
, 0);
339 dev_err(&pdev
->dev
, "failed to map mem base\n");
343 err
= ptd
->ops
->hw_init(ptd
, &nr_trips
);
347 ptd
->tzd
= thermal_zone_device_register(bi
->name
, nr_trips
, 0, ptd
,
348 &tzd_ops
, NULL
, 0, 0);
349 if (IS_ERR(ptd
->tzd
)) {
350 dev_err(&pdev
->dev
, "Failed to register thermal zone %s\n",
352 err
= PTR_ERR(ptd
->tzd
);
359 iounmap(ptd
->hw_base
);
361 pci_release_regions(pdev
);
363 pci_disable_device(pdev
);
364 dev_err(&pdev
->dev
, "pci device failed to probe\n");
368 static void intel_pch_thermal_remove(struct pci_dev
*pdev
)
370 struct pch_thermal_device
*ptd
= pci_get_drvdata(pdev
);
372 thermal_zone_device_unregister(ptd
->tzd
);
373 iounmap(ptd
->hw_base
);
374 pci_set_drvdata(pdev
, NULL
);
375 pci_release_regions(pdev
);
376 pci_disable_device(pdev
);
379 static int intel_pch_thermal_suspend(struct device
*device
)
381 struct pch_thermal_device
*ptd
= dev_get_drvdata(device
);
383 return ptd
->ops
->suspend(ptd
);
386 static int intel_pch_thermal_resume(struct device
*device
)
388 struct pch_thermal_device
*ptd
= dev_get_drvdata(device
);
390 return ptd
->ops
->resume(ptd
);
393 static const struct pci_device_id intel_pch_thermal_id
[] = {
394 { PCI_DEVICE(PCI_VENDOR_ID_INTEL
, PCH_THERMAL_DID_HSW_1
),
395 .driver_data
= board_hsw
, },
396 { PCI_DEVICE(PCI_VENDOR_ID_INTEL
, PCH_THERMAL_DID_HSW_2
),
397 .driver_data
= board_hsw
, },
398 { PCI_DEVICE(PCI_VENDOR_ID_INTEL
, PCH_THERMAL_DID_WPT
),
399 .driver_data
= board_wpt
, },
400 { PCI_DEVICE(PCI_VENDOR_ID_INTEL
, PCH_THERMAL_DID_SKL
),
401 .driver_data
= board_skl
, },
402 { PCI_DEVICE(PCI_VENDOR_ID_INTEL
, PCH_THERMAL_DID_SKL_H
),
403 .driver_data
= board_skl
, },
404 { PCI_DEVICE(PCI_VENDOR_ID_INTEL
, PCH_THERMAL_DID_CNL
),
405 .driver_data
= board_cnl
, },
406 { PCI_DEVICE(PCI_VENDOR_ID_INTEL
, PCH_THERMAL_DID_CNL_H
),
407 .driver_data
= board_cnl
, },
408 { PCI_DEVICE(PCI_VENDOR_ID_INTEL
, PCH_THERMAL_DID_CML_H
),
409 .driver_data
= board_cml
, },
412 MODULE_DEVICE_TABLE(pci
, intel_pch_thermal_id
);
414 static const struct dev_pm_ops intel_pch_pm_ops
= {
415 .suspend
= intel_pch_thermal_suspend
,
416 .resume
= intel_pch_thermal_resume
,
419 static struct pci_driver intel_pch_thermal_driver
= {
420 .name
= "intel_pch_thermal",
421 .id_table
= intel_pch_thermal_id
,
422 .probe
= intel_pch_thermal_probe
,
423 .remove
= intel_pch_thermal_remove
,
424 .driver
.pm
= &intel_pch_pm_ops
,
427 module_pci_driver(intel_pch_thermal_driver
);
429 MODULE_LICENSE("GPL v2");
430 MODULE_DESCRIPTION("Intel PCH Thermal driver");