xref: /linux/drivers/thermal/intel/intel_pch_thermal.c (revision 2153a87ff9ef5537b8a96e7c94a9d79a78c7a30c)
12025cf9eSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
23e8c4d31SAmit Kucheria /* intel_pch_thermal.c - Intel PCH Thermal driver
33e8c4d31SAmit Kucheria  *
43e8c4d31SAmit Kucheria  * Copyright (c) 2015, Intel Corporation.
53e8c4d31SAmit Kucheria  *
63e8c4d31SAmit Kucheria  * Authors:
73e8c4d31SAmit Kucheria  *     Tushar Dave <tushar.n.dave@intel.com>
83e8c4d31SAmit Kucheria  */
93e8c4d31SAmit Kucheria 
10ef63b043SSumeet Pawnikar #include <linux/acpi.h>
11ef63b043SSumeet Pawnikar #include <linux/delay.h>
123e8c4d31SAmit Kucheria #include <linux/module.h>
133e8c4d31SAmit Kucheria #include <linux/init.h>
143e8c4d31SAmit Kucheria #include <linux/pci.h>
153e8c4d31SAmit Kucheria #include <linux/pm.h>
16ef63b043SSumeet Pawnikar #include <linux/suspend.h>
17ef63b043SSumeet Pawnikar #include <linux/thermal.h>
18ef63b043SSumeet Pawnikar #include <linux/types.h>
19ef63b043SSumeet Pawnikar #include <linux/units.h>
203e8c4d31SAmit Kucheria 
213e8c4d31SAmit Kucheria /* Intel PCH thermal Device IDs */
223e8c4d31SAmit Kucheria #define PCH_THERMAL_DID_HSW_1	0x9C24 /* Haswell PCH */
233e8c4d31SAmit Kucheria #define PCH_THERMAL_DID_HSW_2	0x8C24 /* Haswell PCH */
243e8c4d31SAmit Kucheria #define PCH_THERMAL_DID_WPT	0x9CA4 /* Wildcat Point */
253e8c4d31SAmit Kucheria #define PCH_THERMAL_DID_SKL	0x9D31 /* Skylake PCH */
263e8c4d31SAmit Kucheria #define PCH_THERMAL_DID_SKL_H	0xA131 /* Skylake PCH 100 series */
273e8c4d31SAmit Kucheria #define PCH_THERMAL_DID_CNL	0x9Df9 /* CNL PCH */
283e8c4d31SAmit Kucheria #define PCH_THERMAL_DID_CNL_H	0xA379 /* CNL-H PCH */
29c569e805SSumeet Pawnikar #define PCH_THERMAL_DID_CNL_LP	0x02F9 /* CNL-LP PCH */
3035709c4eSGayatri Kammela #define PCH_THERMAL_DID_CML_H	0X06F9 /* CML-H PCH */
31e78acf7eSAndres Freund #define PCH_THERMAL_DID_LWB	0xA1B1 /* Lewisburg PCH */
3240dc1929STim Zimmermann #define PCH_THERMAL_DID_WBG	0x8D24 /* Wellsburg PCH */
333e8c4d31SAmit Kucheria 
343e8c4d31SAmit Kucheria /* Wildcat Point-LP  PCH Thermal registers */
353e8c4d31SAmit Kucheria #define WPT_TEMP	0x0000	/* Temperature */
363e8c4d31SAmit Kucheria #define WPT_TSC	0x04	/* Thermal Sensor Control */
373e8c4d31SAmit Kucheria #define WPT_TSS	0x06	/* Thermal Sensor Status */
383e8c4d31SAmit Kucheria #define WPT_TSEL	0x08	/* Thermal Sensor Enable and Lock */
393e8c4d31SAmit Kucheria #define WPT_TSREL	0x0A	/* Thermal Sensor Report Enable and Lock */
403e8c4d31SAmit Kucheria #define WPT_TSMIC	0x0C	/* Thermal Sensor SMI Control */
413e8c4d31SAmit Kucheria #define WPT_CTT	0x0010	/* Catastrophic Trip Point */
42ef63b043SSumeet Pawnikar #define WPT_TSPM	0x001C	/* Thermal Sensor Power Management */
433e8c4d31SAmit Kucheria #define WPT_TAHV	0x0014	/* Thermal Alert High Value */
443e8c4d31SAmit Kucheria #define WPT_TALV	0x0018	/* Thermal Alert Low Value */
453e8c4d31SAmit Kucheria #define WPT_TL		0x00000040	/* Throttle Value */
463e8c4d31SAmit Kucheria #define WPT_PHL	0x0060	/* PCH Hot Level */
473e8c4d31SAmit Kucheria #define WPT_PHLC	0x62	/* PHL Control */
483e8c4d31SAmit Kucheria #define WPT_TAS	0x80	/* Thermal Alert Status */
493e8c4d31SAmit Kucheria #define WPT_TSPIEN	0x82	/* PCI Interrupt Event Enables */
503e8c4d31SAmit Kucheria #define WPT_TSGPEN	0x84	/* General Purpose Event Enables */
513e8c4d31SAmit Kucheria 
523e8c4d31SAmit Kucheria /*  Wildcat Point-LP  PCH Thermal Register bit definitions */
533e8c4d31SAmit Kucheria #define WPT_TEMP_TSR	0x01ff	/* Temp TS Reading */
543e8c4d31SAmit Kucheria #define WPT_TSC_CPDE	0x01	/* Catastrophic Power-Down Enable */
553e8c4d31SAmit Kucheria #define WPT_TSS_TSDSS	0x10	/* Thermal Sensor Dynamic Shutdown Status */
563e8c4d31SAmit Kucheria #define WPT_TSS_GPES	0x08	/* GPE status */
573e8c4d31SAmit Kucheria #define WPT_TSEL_ETS	0x01    /* Enable TS */
583e8c4d31SAmit Kucheria #define WPT_TSEL_PLDB	0x80	/* TSEL Policy Lock-Down Bit */
593e8c4d31SAmit Kucheria #define WPT_TL_TOL	0x000001FF	/* T0 Level */
603e8c4d31SAmit Kucheria #define WPT_TL_T1L	0x1ff00000	/* T1 Level */
613e8c4d31SAmit Kucheria #define WPT_TL_TTEN	0x20000000	/* TT Enable */
623e8c4d31SAmit Kucheria 
63ef63b043SSumeet Pawnikar /* Resolution of 1/2 degree C and an offset of -50C */
64ef63b043SSumeet Pawnikar #define PCH_TEMP_OFFSET	(-50)
65ef63b043SSumeet Pawnikar #define GET_WPT_TEMP(x)	((x) * MILLIDEGREE_PER_DEGREE / 2 + WPT_TEMP_OFFSET)
66ef63b043SSumeet Pawnikar #define WPT_TEMP_OFFSET	(PCH_TEMP_OFFSET * MILLIDEGREE_PER_DEGREE)
67ef63b043SSumeet Pawnikar #define GET_PCH_TEMP(x)	(((x) / 2) + PCH_TEMP_OFFSET)
68ef63b043SSumeet Pawnikar 
69fee19c69SDaniel Lezcano #define PCH_MAX_TRIPS 3 /* critical, hot, passive */
70fee19c69SDaniel Lezcano 
71ef63b043SSumeet Pawnikar /* Amount of time for each cooling delay, 100ms by default for now */
72ef63b043SSumeet Pawnikar static unsigned int delay_timeout = 100;
73ef63b043SSumeet Pawnikar module_param(delay_timeout, int, 0644);
74ef63b043SSumeet Pawnikar MODULE_PARM_DESC(delay_timeout, "amount of time delay for each iteration.");
75ef63b043SSumeet Pawnikar 
7692923028SZhang Rui /* Number of iterations for cooling delay, 600 counts by default for now */
7792923028SZhang Rui static unsigned int delay_cnt = 600;
78ef63b043SSumeet Pawnikar module_param(delay_cnt, int, 0644);
79ef63b043SSumeet Pawnikar MODULE_PARM_DESC(delay_cnt, "total number of iterations for time delay.");
80ef63b043SSumeet Pawnikar 
813e8c4d31SAmit Kucheria static char driver_name[] = "Intel PCH thermal driver";
823e8c4d31SAmit Kucheria 
833e8c4d31SAmit Kucheria struct pch_thermal_device {
843e8c4d31SAmit Kucheria 	void __iomem *hw_base;
853e8c4d31SAmit Kucheria 	struct pci_dev *pdev;
863e8c4d31SAmit Kucheria 	struct thermal_zone_device *tzd;
87fee19c69SDaniel Lezcano 	struct thermal_trip trips[PCH_MAX_TRIPS];
883e8c4d31SAmit Kucheria 	bool bios_enabled;
893e8c4d31SAmit Kucheria };
903e8c4d31SAmit Kucheria 
913e8c4d31SAmit Kucheria #ifdef CONFIG_ACPI
923e8c4d31SAmit Kucheria /*
933e8c4d31SAmit Kucheria  * On some platforms, there is a companion ACPI device, which adds
943e8c4d31SAmit Kucheria  * passive trip temperature using _PSV method. There is no specific
953e8c4d31SAmit Kucheria  * passive temperature setting in MMIO interface of this PCI device.
963e8c4d31SAmit Kucheria  */
972cee7356SRafael J. Wysocki static int pch_wpt_add_acpi_psv_trip(struct pch_thermal_device *ptd, int trip)
983e8c4d31SAmit Kucheria {
993e8c4d31SAmit Kucheria 	struct acpi_device *adev;
100dd3b3d16SRafael J. Wysocki 	int temp;
1013e8c4d31SAmit Kucheria 
1023e8c4d31SAmit Kucheria 	adev = ACPI_COMPANION(&ptd->pdev->dev);
103fee19c69SDaniel Lezcano 	if (!adev)
1042cee7356SRafael J. Wysocki 		return 0;
1053e8c4d31SAmit Kucheria 
106dd3b3d16SRafael J. Wysocki 	if (thermal_acpi_passive_trip_temp(adev, &temp) || temp <= 0)
1072cee7356SRafael J. Wysocki 		return 0;
1083e8c4d31SAmit Kucheria 
1092cee7356SRafael J. Wysocki 	ptd->trips[trip].type = THERMAL_TRIP_PASSIVE;
1102cee7356SRafael J. Wysocki 	ptd->trips[trip].temperature = temp;
1112cee7356SRafael J. Wysocki 	return 1;
1123e8c4d31SAmit Kucheria }
1133e8c4d31SAmit Kucheria #else
1142cee7356SRafael J. Wysocki static int pch_wpt_add_acpi_psv_trip(struct pch_thermal_device *ptd, int trip)
1153e8c4d31SAmit Kucheria {
1162cee7356SRafael J. Wysocki 	return 0;
1173e8c4d31SAmit Kucheria }
1183e8c4d31SAmit Kucheria #endif
1193e8c4d31SAmit Kucheria 
1203e8c4d31SAmit Kucheria static int pch_thermal_get_temp(struct thermal_zone_device *tzd, int *temp)
1213e8c4d31SAmit Kucheria {
1223e8c4d31SAmit Kucheria 	struct pch_thermal_device *ptd = tzd->devdata;
1233e8c4d31SAmit Kucheria 
12435c87f94SRafael J. Wysocki 	*temp = GET_WPT_TEMP(WPT_TEMP_TSR & readw(ptd->hw_base + WPT_TEMP));
125558718f4SRafael J. Wysocki 	return 0;
1263e8c4d31SAmit Kucheria }
1273e8c4d31SAmit Kucheria 
12803671968SKai-Heng Feng static void pch_critical(struct thermal_zone_device *tzd)
12903671968SKai-Heng Feng {
13003671968SKai-Heng Feng 	dev_dbg(&tzd->device, "%s: critical temperature reached\n", tzd->type);
13103671968SKai-Heng Feng }
13203671968SKai-Heng Feng 
1333e8c4d31SAmit Kucheria static struct thermal_zone_device_ops tzd_ops = {
1343e8c4d31SAmit Kucheria 	.get_temp = pch_thermal_get_temp,
13503671968SKai-Heng Feng 	.critical = pch_critical,
1363e8c4d31SAmit Kucheria };
1373e8c4d31SAmit Kucheria 
138ae98e57aSRafael J. Wysocki enum pch_board_ids {
139ae98e57aSRafael J. Wysocki 	PCH_BOARD_HSW = 0,
140ae98e57aSRafael J. Wysocki 	PCH_BOARD_WPT,
141ae98e57aSRafael J. Wysocki 	PCH_BOARD_SKL,
142ae98e57aSRafael J. Wysocki 	PCH_BOARD_CNL,
143ae98e57aSRafael J. Wysocki 	PCH_BOARD_CML,
144ae98e57aSRafael J. Wysocki 	PCH_BOARD_LWB,
145ae98e57aSRafael J. Wysocki 	PCH_BOARD_WBG,
1463e8c4d31SAmit Kucheria };
1473e8c4d31SAmit Kucheria 
148*2153a87fSRafael J. Wysocki static const char *board_names[] = {
149*2153a87fSRafael J. Wysocki 	[PCH_BOARD_HSW] = "pch_haswell",
150*2153a87fSRafael J. Wysocki 	[PCH_BOARD_WPT] = "pch_wildcat_point",
151*2153a87fSRafael J. Wysocki 	[PCH_BOARD_SKL] = "pch_skylake",
152*2153a87fSRafael J. Wysocki 	[PCH_BOARD_CNL] = "pch_cannonlake",
153*2153a87fSRafael J. Wysocki 	[PCH_BOARD_CML] = "pch_cometlake",
154*2153a87fSRafael J. Wysocki 	[PCH_BOARD_LWB] = "pch_lewisburg",
155*2153a87fSRafael J. Wysocki 	[PCH_BOARD_WBG] = "pch_wellsburg",
1563e8c4d31SAmit Kucheria };
1573e8c4d31SAmit Kucheria 
1583e8c4d31SAmit Kucheria static int intel_pch_thermal_probe(struct pci_dev *pdev,
1593e8c4d31SAmit Kucheria 				   const struct pci_device_id *id)
1603e8c4d31SAmit Kucheria {
161ae98e57aSRafael J. Wysocki 	enum pch_board_ids board_id = id->driver_data;
1623e8c4d31SAmit Kucheria 	struct pch_thermal_device *ptd;
16335c87f94SRafael J. Wysocki 	int nr_trips = 0;
16435c87f94SRafael J. Wysocki 	u16 trip_temp;
16535c87f94SRafael J. Wysocki 	u8 tsel;
1663e8c4d31SAmit Kucheria 	int err;
1673e8c4d31SAmit Kucheria 
1683e8c4d31SAmit Kucheria 	ptd = devm_kzalloc(&pdev->dev, sizeof(*ptd), GFP_KERNEL);
1693e8c4d31SAmit Kucheria 	if (!ptd)
1703e8c4d31SAmit Kucheria 		return -ENOMEM;
1713e8c4d31SAmit Kucheria 
1723e8c4d31SAmit Kucheria 	pci_set_drvdata(pdev, ptd);
1733e8c4d31SAmit Kucheria 	ptd->pdev = pdev;
1743e8c4d31SAmit Kucheria 
1753e8c4d31SAmit Kucheria 	err = pci_enable_device(pdev);
1763e8c4d31SAmit Kucheria 	if (err) {
1773e8c4d31SAmit Kucheria 		dev_err(&pdev->dev, "failed to enable pci device\n");
1783e8c4d31SAmit Kucheria 		return err;
1793e8c4d31SAmit Kucheria 	}
1803e8c4d31SAmit Kucheria 
1813e8c4d31SAmit Kucheria 	err = pci_request_regions(pdev, driver_name);
1823e8c4d31SAmit Kucheria 	if (err) {
1833e8c4d31SAmit Kucheria 		dev_err(&pdev->dev, "failed to request pci region\n");
1843e8c4d31SAmit Kucheria 		goto error_disable;
1853e8c4d31SAmit Kucheria 	}
1863e8c4d31SAmit Kucheria 
1873e8c4d31SAmit Kucheria 	ptd->hw_base = pci_ioremap_bar(pdev, 0);
1883e8c4d31SAmit Kucheria 	if (!ptd->hw_base) {
1893e8c4d31SAmit Kucheria 		err = -ENOMEM;
1903e8c4d31SAmit Kucheria 		dev_err(&pdev->dev, "failed to map mem base\n");
1913e8c4d31SAmit Kucheria 		goto error_release;
1923e8c4d31SAmit Kucheria 	}
1933e8c4d31SAmit Kucheria 
19435c87f94SRafael J. Wysocki 	/* Check if BIOS has already enabled thermal sensor */
19535c87f94SRafael J. Wysocki 	if (WPT_TSEL_ETS & readb(ptd->hw_base + WPT_TSEL)) {
19635c87f94SRafael J. Wysocki 		ptd->bios_enabled = true;
19735c87f94SRafael J. Wysocki 		goto read_trips;
19835c87f94SRafael J. Wysocki 	}
19935c87f94SRafael J. Wysocki 
20035c87f94SRafael J. Wysocki 	tsel = readb(ptd->hw_base + WPT_TSEL);
20135c87f94SRafael J. Wysocki 	/*
20235c87f94SRafael J. Wysocki 	 * When TSEL's Policy Lock-Down bit is 1, TSEL become RO.
20335c87f94SRafael J. Wysocki 	 * If so, thermal sensor cannot enable. Bail out.
20435c87f94SRafael J. Wysocki 	 */
20535c87f94SRafael J. Wysocki 	if (tsel & WPT_TSEL_PLDB) {
20635c87f94SRafael J. Wysocki 		dev_err(&ptd->pdev->dev, "Sensor can't be enabled\n");
20735c87f94SRafael J. Wysocki 		err = -ENODEV;
2083e8c4d31SAmit Kucheria 		goto error_cleanup;
209558718f4SRafael J. Wysocki 	}
2103e8c4d31SAmit Kucheria 
21135c87f94SRafael J. Wysocki 	writeb(tsel|WPT_TSEL_ETS, ptd->hw_base + WPT_TSEL);
21235c87f94SRafael J. Wysocki 	if (!(WPT_TSEL_ETS & readb(ptd->hw_base + WPT_TSEL))) {
21335c87f94SRafael J. Wysocki 		dev_err(&ptd->pdev->dev, "Sensor can't be enabled\n");
21435c87f94SRafael J. Wysocki 		err = -ENODEV;
21535c87f94SRafael J. Wysocki 		goto error_cleanup;
21635c87f94SRafael J. Wysocki 	}
21735c87f94SRafael J. Wysocki 
21835c87f94SRafael J. Wysocki read_trips:
21935c87f94SRafael J. Wysocki 	trip_temp = readw(ptd->hw_base + WPT_CTT);
22035c87f94SRafael J. Wysocki 	trip_temp &= 0x1FF;
22135c87f94SRafael J. Wysocki 	if (trip_temp) {
22235c87f94SRafael J. Wysocki 		ptd->trips[nr_trips].temperature = GET_WPT_TEMP(trip_temp);
22335c87f94SRafael J. Wysocki 		ptd->trips[nr_trips++].type = THERMAL_TRIP_CRITICAL;
22435c87f94SRafael J. Wysocki 	}
22535c87f94SRafael J. Wysocki 
22635c87f94SRafael J. Wysocki 	trip_temp = readw(ptd->hw_base + WPT_PHL);
22735c87f94SRafael J. Wysocki 	trip_temp &= 0x1FF;
22835c87f94SRafael J. Wysocki 	if (trip_temp) {
22935c87f94SRafael J. Wysocki 		ptd->trips[nr_trips].temperature = GET_WPT_TEMP(trip_temp);
23035c87f94SRafael J. Wysocki 		ptd->trips[nr_trips++].type = THERMAL_TRIP_HOT;
23135c87f94SRafael J. Wysocki 	}
23235c87f94SRafael J. Wysocki 
23335c87f94SRafael J. Wysocki 	nr_trips += pch_wpt_add_acpi_psv_trip(ptd, nr_trips);
23435c87f94SRafael J. Wysocki 
235*2153a87fSRafael J. Wysocki 	ptd->tzd = thermal_zone_device_register_with_trips(board_names[board_id],
236*2153a87fSRafael J. Wysocki 							   ptd->trips, nr_trips,
237*2153a87fSRafael J. Wysocki 							   0, ptd, &tzd_ops,
238*2153a87fSRafael J. Wysocki 							   NULL, 0, 0);
2393e8c4d31SAmit Kucheria 	if (IS_ERR(ptd->tzd)) {
2403e8c4d31SAmit Kucheria 		dev_err(&pdev->dev, "Failed to register thermal zone %s\n",
241*2153a87fSRafael J. Wysocki 			board_names[board_id]);
2423e8c4d31SAmit Kucheria 		err = PTR_ERR(ptd->tzd);
2433e8c4d31SAmit Kucheria 		goto error_cleanup;
2443e8c4d31SAmit Kucheria 	}
245bbcf90c0SAndrzej Pietrasiewicz 	err = thermal_zone_device_enable(ptd->tzd);
246bbcf90c0SAndrzej Pietrasiewicz 	if (err)
247bbcf90c0SAndrzej Pietrasiewicz 		goto err_unregister;
2483e8c4d31SAmit Kucheria 
2493e8c4d31SAmit Kucheria 	return 0;
2503e8c4d31SAmit Kucheria 
251bbcf90c0SAndrzej Pietrasiewicz err_unregister:
252bbcf90c0SAndrzej Pietrasiewicz 	thermal_zone_device_unregister(ptd->tzd);
2533e8c4d31SAmit Kucheria error_cleanup:
2543e8c4d31SAmit Kucheria 	iounmap(ptd->hw_base);
2553e8c4d31SAmit Kucheria error_release:
2563e8c4d31SAmit Kucheria 	pci_release_regions(pdev);
2573e8c4d31SAmit Kucheria error_disable:
2583e8c4d31SAmit Kucheria 	pci_disable_device(pdev);
2593e8c4d31SAmit Kucheria 	dev_err(&pdev->dev, "pci device failed to probe\n");
2603e8c4d31SAmit Kucheria 	return err;
2613e8c4d31SAmit Kucheria }
2623e8c4d31SAmit Kucheria 
2633e8c4d31SAmit Kucheria static void intel_pch_thermal_remove(struct pci_dev *pdev)
2643e8c4d31SAmit Kucheria {
2653e8c4d31SAmit Kucheria 	struct pch_thermal_device *ptd = pci_get_drvdata(pdev);
2663e8c4d31SAmit Kucheria 
2673e8c4d31SAmit Kucheria 	thermal_zone_device_unregister(ptd->tzd);
2683e8c4d31SAmit Kucheria 	iounmap(ptd->hw_base);
2693e8c4d31SAmit Kucheria 	pci_set_drvdata(pdev, NULL);
27066dd8b80SChuhong Yuan 	pci_release_regions(pdev);
2713e8c4d31SAmit Kucheria 	pci_disable_device(pdev);
2723e8c4d31SAmit Kucheria }
2733e8c4d31SAmit Kucheria 
27428708e19SZhang Rui static int intel_pch_thermal_suspend_noirq(struct device *device)
2753e8c4d31SAmit Kucheria {
27697e9cafeSChuhong Yuan 	struct pch_thermal_device *ptd = dev_get_drvdata(device);
277c5f43242SRafael J. Wysocki 	u16 pch_thr_temp, pch_cur_temp;
278c5f43242SRafael J. Wysocki 	int pch_delay_cnt = 0;
279c5f43242SRafael J. Wysocki 	u8 tsel;
2803e8c4d31SAmit Kucheria 
281c5f43242SRafael J. Wysocki 	/* Shutdown the thermal sensor if it is not enabled by BIOS */
282c5f43242SRafael J. Wysocki 	if (!ptd->bios_enabled) {
283c5f43242SRafael J. Wysocki 		tsel = readb(ptd->hw_base + WPT_TSEL);
284c5f43242SRafael J. Wysocki 		writeb(tsel & 0xFE, ptd->hw_base + WPT_TSEL);
285c5f43242SRafael J. Wysocki 		return 0;
286c5f43242SRafael J. Wysocki 	}
287c5f43242SRafael J. Wysocki 
288c5f43242SRafael J. Wysocki 	/* Do not check temperature if it is not s2idle */
289c5f43242SRafael J. Wysocki 	if (pm_suspend_via_firmware())
290c5f43242SRafael J. Wysocki 		return 0;
291c5f43242SRafael J. Wysocki 
292c5f43242SRafael J. Wysocki 	/* Get the PCH temperature threshold value */
293c5f43242SRafael J. Wysocki 	pch_thr_temp = GET_PCH_TEMP(WPT_TEMP_TSR & readw(ptd->hw_base + WPT_TSPM));
294c5f43242SRafael J. Wysocki 
295c5f43242SRafael J. Wysocki 	/* Get the PCH current temperature value */
296c5f43242SRafael J. Wysocki 	pch_cur_temp = GET_PCH_TEMP(WPT_TEMP_TSR & readw(ptd->hw_base + WPT_TEMP));
297c5f43242SRafael J. Wysocki 
298c5f43242SRafael J. Wysocki 	/*
299c5f43242SRafael J. Wysocki 	 * If current PCH temperature is higher than configured PCH threshold
300c5f43242SRafael J. Wysocki 	 * value, run some delay loop with sleep to let the current temperature
301c5f43242SRafael J. Wysocki 	 * go down below the threshold value which helps to allow system enter
302c5f43242SRafael J. Wysocki 	 * lower power S0ix suspend state. Even after delay loop if PCH current
303c5f43242SRafael J. Wysocki 	 * temperature stays above threshold, notify the warning message
304c5f43242SRafael J. Wysocki 	 * which helps to indentify the reason why S0ix entry was rejected.
305c5f43242SRafael J. Wysocki 	 */
306c5f43242SRafael J. Wysocki 	while (pch_delay_cnt < delay_cnt) {
307c5f43242SRafael J. Wysocki 		if (pch_cur_temp < pch_thr_temp)
308c5f43242SRafael J. Wysocki 			break;
309c5f43242SRafael J. Wysocki 
310c5f43242SRafael J. Wysocki 		if (pm_wakeup_pending()) {
311c5f43242SRafael J. Wysocki 			dev_warn(&ptd->pdev->dev, "Wakeup event detected, abort cooling\n");
312c5f43242SRafael J. Wysocki 			return 0;
313c5f43242SRafael J. Wysocki 		}
314c5f43242SRafael J. Wysocki 
315c5f43242SRafael J. Wysocki 		pch_delay_cnt++;
316c5f43242SRafael J. Wysocki 		dev_dbg(&ptd->pdev->dev,
317c5f43242SRafael J. Wysocki 			"CPU-PCH current temp [%dC] higher than the threshold temp [%dC], sleep %d times for %d ms duration\n",
318c5f43242SRafael J. Wysocki 			pch_cur_temp, pch_thr_temp, pch_delay_cnt, delay_timeout);
319c5f43242SRafael J. Wysocki 		msleep(delay_timeout);
320c5f43242SRafael J. Wysocki 		/* Read the PCH current temperature for next cycle. */
321c5f43242SRafael J. Wysocki 		pch_cur_temp = GET_PCH_TEMP(WPT_TEMP_TSR & readw(ptd->hw_base + WPT_TEMP));
322c5f43242SRafael J. Wysocki 	}
323c5f43242SRafael J. Wysocki 
324c5f43242SRafael J. Wysocki 	if (pch_cur_temp >= pch_thr_temp)
325c5f43242SRafael J. Wysocki 		dev_warn(&ptd->pdev->dev,
326c5f43242SRafael J. Wysocki 			"CPU-PCH is hot [%dC] after %d ms delay. S0ix might fail\n",
327c5f43242SRafael J. Wysocki 			pch_cur_temp, pch_delay_cnt * delay_timeout);
328c5f43242SRafael J. Wysocki 	else {
329c5f43242SRafael J. Wysocki 		if (pch_delay_cnt)
330c5f43242SRafael J. Wysocki 			dev_info(&ptd->pdev->dev,
331c5f43242SRafael J. Wysocki 				"CPU-PCH is cool [%dC] after %d ms delay\n",
332c5f43242SRafael J. Wysocki 				pch_cur_temp, pch_delay_cnt * delay_timeout);
333c5f43242SRafael J. Wysocki 		else
334c5f43242SRafael J. Wysocki 			dev_info(&ptd->pdev->dev,
335c5f43242SRafael J. Wysocki 				"CPU-PCH is cool [%dC]\n",
336c5f43242SRafael J. Wysocki 				pch_cur_temp);
337c5f43242SRafael J. Wysocki 	}
338c5f43242SRafael J. Wysocki 
339c5f43242SRafael J. Wysocki 	return 0;
3403e8c4d31SAmit Kucheria }
3413e8c4d31SAmit Kucheria 
3423e8c4d31SAmit Kucheria static int intel_pch_thermal_resume(struct device *device)
3433e8c4d31SAmit Kucheria {
34497e9cafeSChuhong Yuan 	struct pch_thermal_device *ptd = dev_get_drvdata(device);
345c5f43242SRafael J. Wysocki 	u8 tsel;
3463e8c4d31SAmit Kucheria 
347c5f43242SRafael J. Wysocki 	if (ptd->bios_enabled)
348c5f43242SRafael J. Wysocki 		return 0;
349c5f43242SRafael J. Wysocki 
350c5f43242SRafael J. Wysocki 	tsel = readb(ptd->hw_base + WPT_TSEL);
351c5f43242SRafael J. Wysocki 
352c5f43242SRafael J. Wysocki 	writeb(tsel | WPT_TSEL_ETS, ptd->hw_base + WPT_TSEL);
353c5f43242SRafael J. Wysocki 
354c5f43242SRafael J. Wysocki 	return 0;
3553e8c4d31SAmit Kucheria }
3563e8c4d31SAmit Kucheria 
3573e8c4d31SAmit Kucheria static const struct pci_device_id intel_pch_thermal_id[] = {
3583e8c4d31SAmit Kucheria 	{ PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCH_THERMAL_DID_HSW_1),
359ae98e57aSRafael J. Wysocki 		.driver_data = PCH_BOARD_HSW, },
3603e8c4d31SAmit Kucheria 	{ PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCH_THERMAL_DID_HSW_2),
361ae98e57aSRafael J. Wysocki 		.driver_data = PCH_BOARD_HSW, },
3623e8c4d31SAmit Kucheria 	{ PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCH_THERMAL_DID_WPT),
363ae98e57aSRafael J. Wysocki 		.driver_data = PCH_BOARD_WPT, },
3643e8c4d31SAmit Kucheria 	{ PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCH_THERMAL_DID_SKL),
365ae98e57aSRafael J. Wysocki 		.driver_data = PCH_BOARD_SKL, },
3663e8c4d31SAmit Kucheria 	{ PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCH_THERMAL_DID_SKL_H),
367ae98e57aSRafael J. Wysocki 		.driver_data = PCH_BOARD_SKL, },
3683e8c4d31SAmit Kucheria 	{ PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCH_THERMAL_DID_CNL),
369ae98e57aSRafael J. Wysocki 		.driver_data = PCH_BOARD_CNL, },
3703e8c4d31SAmit Kucheria 	{ PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCH_THERMAL_DID_CNL_H),
371ae98e57aSRafael J. Wysocki 		.driver_data = PCH_BOARD_CNL, },
372c569e805SSumeet Pawnikar 	{ PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCH_THERMAL_DID_CNL_LP),
373ae98e57aSRafael J. Wysocki 		.driver_data = PCH_BOARD_CNL, },
37435709c4eSGayatri Kammela 	{ PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCH_THERMAL_DID_CML_H),
375ae98e57aSRafael J. Wysocki 		.driver_data = PCH_BOARD_CML, },
376e78acf7eSAndres Freund 	{ PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCH_THERMAL_DID_LWB),
377ae98e57aSRafael J. Wysocki 		.driver_data = PCH_BOARD_LWB, },
37840dc1929STim Zimmermann 	{ PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCH_THERMAL_DID_WBG),
379ae98e57aSRafael J. Wysocki 		.driver_data = PCH_BOARD_WBG, },
3803e8c4d31SAmit Kucheria 	{ 0, },
3813e8c4d31SAmit Kucheria };
3823e8c4d31SAmit Kucheria MODULE_DEVICE_TABLE(pci, intel_pch_thermal_id);
3833e8c4d31SAmit Kucheria 
3843e8c4d31SAmit Kucheria static const struct dev_pm_ops intel_pch_pm_ops = {
38528708e19SZhang Rui 	.suspend_noirq = intel_pch_thermal_suspend_noirq,
3863e8c4d31SAmit Kucheria 	.resume = intel_pch_thermal_resume,
3873e8c4d31SAmit Kucheria };
3883e8c4d31SAmit Kucheria 
3893e8c4d31SAmit Kucheria static struct pci_driver intel_pch_thermal_driver = {
3903e8c4d31SAmit Kucheria 	.name		= "intel_pch_thermal",
3913e8c4d31SAmit Kucheria 	.id_table	= intel_pch_thermal_id,
3923e8c4d31SAmit Kucheria 	.probe		= intel_pch_thermal_probe,
3933e8c4d31SAmit Kucheria 	.remove		= intel_pch_thermal_remove,
3943e8c4d31SAmit Kucheria 	.driver.pm	= &intel_pch_pm_ops,
3953e8c4d31SAmit Kucheria };
3963e8c4d31SAmit Kucheria 
3973e8c4d31SAmit Kucheria module_pci_driver(intel_pch_thermal_driver);
3983e8c4d31SAmit Kucheria 
3993e8c4d31SAmit Kucheria MODULE_LICENSE("GPL v2");
4003e8c4d31SAmit Kucheria MODULE_DESCRIPTION("Intel PCH Thermal driver");
401