xref: /linux/drivers/thermal/intel/intel_pch_thermal.c (revision a1ff5a7d78a036d6c2178ee5acd6ba4946243800)
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;
873e8c4d31SAmit Kucheria 	bool bios_enabled;
883e8c4d31SAmit Kucheria };
893e8c4d31SAmit Kucheria 
903e8c4d31SAmit Kucheria #ifdef CONFIG_ACPI
913e8c4d31SAmit Kucheria /*
923e8c4d31SAmit Kucheria  * On some platforms, there is a companion ACPI device, which adds
933e8c4d31SAmit Kucheria  * passive trip temperature using _PSV method. There is no specific
943e8c4d31SAmit Kucheria  * passive temperature setting in MMIO interface of this PCI device.
953e8c4d31SAmit Kucheria  */
pch_wpt_add_acpi_psv_trip(struct pch_thermal_device * ptd,struct thermal_trip * trip)96fcbf8780SRafael J. Wysocki static int pch_wpt_add_acpi_psv_trip(struct pch_thermal_device *ptd,
97fcbf8780SRafael J. Wysocki 				     struct thermal_trip *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 
109fcbf8780SRafael J. Wysocki 	trip->type = THERMAL_TRIP_PASSIVE;
110fcbf8780SRafael J. Wysocki 	trip->temperature = temp;
1112cee7356SRafael J. Wysocki 	return 1;
1123e8c4d31SAmit Kucheria }
1133e8c4d31SAmit Kucheria #else
pch_wpt_add_acpi_psv_trip(struct pch_thermal_device * ptd,struct thermal_trip * trip)114fcbf8780SRafael J. Wysocki static int pch_wpt_add_acpi_psv_trip(struct pch_thermal_device *ptd,
115fcbf8780SRafael J. Wysocki 				     struct thermal_trip *trip)
1163e8c4d31SAmit Kucheria {
1172cee7356SRafael J. Wysocki 	return 0;
1183e8c4d31SAmit Kucheria }
1193e8c4d31SAmit Kucheria #endif
1203e8c4d31SAmit Kucheria 
pch_thermal_get_temp(struct thermal_zone_device * tzd,int * temp)1213e8c4d31SAmit Kucheria static int pch_thermal_get_temp(struct thermal_zone_device *tzd, int *temp)
1223e8c4d31SAmit Kucheria {
1235f68d078SDaniel Lezcano 	struct pch_thermal_device *ptd = thermal_zone_device_priv(tzd);
1243e8c4d31SAmit Kucheria 
12535c87f94SRafael J. Wysocki 	*temp = GET_WPT_TEMP(WPT_TEMP_TSR & readw(ptd->hw_base + WPT_TEMP));
126558718f4SRafael J. Wysocki 	return 0;
1273e8c4d31SAmit Kucheria }
1283e8c4d31SAmit Kucheria 
pch_critical(struct thermal_zone_device * tzd)12903671968SKai-Heng Feng static void pch_critical(struct thermal_zone_device *tzd)
13003671968SKai-Heng Feng {
131ba7894beSDaniel Lezcano 	dev_dbg(thermal_zone_device(tzd), "%s: critical temperature reached\n",
132ba7894beSDaniel Lezcano 		thermal_zone_device_type(tzd));
13303671968SKai-Heng Feng }
13403671968SKai-Heng Feng 
13562dd1784SRafael J. Wysocki static const struct thermal_zone_device_ops tzd_ops = {
1363e8c4d31SAmit Kucheria 	.get_temp = pch_thermal_get_temp,
13703671968SKai-Heng Feng 	.critical = pch_critical,
1383e8c4d31SAmit Kucheria };
1393e8c4d31SAmit Kucheria 
140ae98e57aSRafael J. Wysocki enum pch_board_ids {
141ae98e57aSRafael J. Wysocki 	PCH_BOARD_HSW = 0,
142ae98e57aSRafael J. Wysocki 	PCH_BOARD_WPT,
143ae98e57aSRafael J. Wysocki 	PCH_BOARD_SKL,
144ae98e57aSRafael J. Wysocki 	PCH_BOARD_CNL,
145ae98e57aSRafael J. Wysocki 	PCH_BOARD_CML,
146ae98e57aSRafael J. Wysocki 	PCH_BOARD_LWB,
147ae98e57aSRafael J. Wysocki 	PCH_BOARD_WBG,
1483e8c4d31SAmit Kucheria };
1493e8c4d31SAmit Kucheria 
1502153a87fSRafael J. Wysocki static const char *board_names[] = {
1512153a87fSRafael J. Wysocki 	[PCH_BOARD_HSW] = "pch_haswell",
1522153a87fSRafael J. Wysocki 	[PCH_BOARD_WPT] = "pch_wildcat_point",
1532153a87fSRafael J. Wysocki 	[PCH_BOARD_SKL] = "pch_skylake",
1542153a87fSRafael J. Wysocki 	[PCH_BOARD_CNL] = "pch_cannonlake",
1552153a87fSRafael J. Wysocki 	[PCH_BOARD_CML] = "pch_cometlake",
1562153a87fSRafael J. Wysocki 	[PCH_BOARD_LWB] = "pch_lewisburg",
1572153a87fSRafael J. Wysocki 	[PCH_BOARD_WBG] = "pch_wellsburg",
1583e8c4d31SAmit Kucheria };
1593e8c4d31SAmit Kucheria 
intel_pch_thermal_probe(struct pci_dev * pdev,const struct pci_device_id * id)1603e8c4d31SAmit Kucheria static int intel_pch_thermal_probe(struct pci_dev *pdev,
1613e8c4d31SAmit Kucheria 				   const struct pci_device_id *id)
1623e8c4d31SAmit Kucheria {
163fcbf8780SRafael J. Wysocki 	struct thermal_trip ptd_trips[PCH_MAX_TRIPS] = { 0 };
164ae98e57aSRafael J. Wysocki 	enum pch_board_ids board_id = id->driver_data;
1653e8c4d31SAmit Kucheria 	struct pch_thermal_device *ptd;
16635c87f94SRafael J. Wysocki 	int nr_trips = 0;
16735c87f94SRafael J. Wysocki 	u16 trip_temp;
16835c87f94SRafael J. Wysocki 	u8 tsel;
1693e8c4d31SAmit Kucheria 	int err;
1703e8c4d31SAmit Kucheria 
1713e8c4d31SAmit Kucheria 	ptd = devm_kzalloc(&pdev->dev, sizeof(*ptd), GFP_KERNEL);
1723e8c4d31SAmit Kucheria 	if (!ptd)
1733e8c4d31SAmit Kucheria 		return -ENOMEM;
1743e8c4d31SAmit Kucheria 
1753e8c4d31SAmit Kucheria 	pci_set_drvdata(pdev, ptd);
1763e8c4d31SAmit Kucheria 	ptd->pdev = pdev;
1773e8c4d31SAmit Kucheria 
1783e8c4d31SAmit Kucheria 	err = pci_enable_device(pdev);
1793e8c4d31SAmit Kucheria 	if (err) {
1803e8c4d31SAmit Kucheria 		dev_err(&pdev->dev, "failed to enable pci device\n");
1813e8c4d31SAmit Kucheria 		return err;
1823e8c4d31SAmit Kucheria 	}
1833e8c4d31SAmit Kucheria 
1843e8c4d31SAmit Kucheria 	err = pci_request_regions(pdev, driver_name);
1853e8c4d31SAmit Kucheria 	if (err) {
1863e8c4d31SAmit Kucheria 		dev_err(&pdev->dev, "failed to request pci region\n");
1873e8c4d31SAmit Kucheria 		goto error_disable;
1883e8c4d31SAmit Kucheria 	}
1893e8c4d31SAmit Kucheria 
1903e8c4d31SAmit Kucheria 	ptd->hw_base = pci_ioremap_bar(pdev, 0);
1913e8c4d31SAmit Kucheria 	if (!ptd->hw_base) {
1923e8c4d31SAmit Kucheria 		err = -ENOMEM;
1933e8c4d31SAmit Kucheria 		dev_err(&pdev->dev, "failed to map mem base\n");
1943e8c4d31SAmit Kucheria 		goto error_release;
1953e8c4d31SAmit Kucheria 	}
1963e8c4d31SAmit Kucheria 
19735c87f94SRafael J. Wysocki 	/* Check if BIOS has already enabled thermal sensor */
19835c87f94SRafael J. Wysocki 	if (WPT_TSEL_ETS & readb(ptd->hw_base + WPT_TSEL)) {
19935c87f94SRafael J. Wysocki 		ptd->bios_enabled = true;
20035c87f94SRafael J. Wysocki 		goto read_trips;
20135c87f94SRafael J. Wysocki 	}
20235c87f94SRafael J. Wysocki 
20335c87f94SRafael J. Wysocki 	tsel = readb(ptd->hw_base + WPT_TSEL);
20435c87f94SRafael J. Wysocki 	/*
20535c87f94SRafael J. Wysocki 	 * When TSEL's Policy Lock-Down bit is 1, TSEL become RO.
20635c87f94SRafael J. Wysocki 	 * If so, thermal sensor cannot enable. Bail out.
20735c87f94SRafael J. Wysocki 	 */
20835c87f94SRafael J. Wysocki 	if (tsel & WPT_TSEL_PLDB) {
20935c87f94SRafael J. Wysocki 		dev_err(&ptd->pdev->dev, "Sensor can't be enabled\n");
21035c87f94SRafael J. Wysocki 		err = -ENODEV;
2113e8c4d31SAmit Kucheria 		goto error_cleanup;
212558718f4SRafael J. Wysocki 	}
2133e8c4d31SAmit Kucheria 
21435c87f94SRafael J. Wysocki 	writeb(tsel|WPT_TSEL_ETS, ptd->hw_base + WPT_TSEL);
21535c87f94SRafael J. Wysocki 	if (!(WPT_TSEL_ETS & readb(ptd->hw_base + WPT_TSEL))) {
21635c87f94SRafael J. Wysocki 		dev_err(&ptd->pdev->dev, "Sensor can't be enabled\n");
21735c87f94SRafael J. Wysocki 		err = -ENODEV;
21835c87f94SRafael J. Wysocki 		goto error_cleanup;
21935c87f94SRafael J. Wysocki 	}
22035c87f94SRafael J. Wysocki 
22135c87f94SRafael J. Wysocki read_trips:
22235c87f94SRafael J. Wysocki 	trip_temp = readw(ptd->hw_base + WPT_CTT);
22335c87f94SRafael J. Wysocki 	trip_temp &= 0x1FF;
22435c87f94SRafael J. Wysocki 	if (trip_temp) {
225fcbf8780SRafael J. Wysocki 		ptd_trips[nr_trips].temperature = GET_WPT_TEMP(trip_temp);
226fcbf8780SRafael J. Wysocki 		ptd_trips[nr_trips++].type = THERMAL_TRIP_CRITICAL;
22735c87f94SRafael J. Wysocki 	}
22835c87f94SRafael J. Wysocki 
22935c87f94SRafael J. Wysocki 	trip_temp = readw(ptd->hw_base + WPT_PHL);
23035c87f94SRafael J. Wysocki 	trip_temp &= 0x1FF;
23135c87f94SRafael J. Wysocki 	if (trip_temp) {
232fcbf8780SRafael J. Wysocki 		ptd_trips[nr_trips].temperature = GET_WPT_TEMP(trip_temp);
233fcbf8780SRafael J. Wysocki 		ptd_trips[nr_trips++].type = THERMAL_TRIP_HOT;
23435c87f94SRafael J. Wysocki 	}
23535c87f94SRafael J. Wysocki 
236fcbf8780SRafael J. Wysocki 	nr_trips += pch_wpt_add_acpi_psv_trip(ptd, &ptd_trips[nr_trips]);
23735c87f94SRafael J. Wysocki 
2382153a87fSRafael J. Wysocki 	ptd->tzd = thermal_zone_device_register_with_trips(board_names[board_id],
239fcbf8780SRafael J. Wysocki 							   ptd_trips, nr_trips,
2404a62d588SRafael J. Wysocki 							   ptd, &tzd_ops,
2412153a87fSRafael J. Wysocki 							   NULL, 0, 0);
2423e8c4d31SAmit Kucheria 	if (IS_ERR(ptd->tzd)) {
2433e8c4d31SAmit Kucheria 		dev_err(&pdev->dev, "Failed to register thermal zone %s\n",
2442153a87fSRafael J. Wysocki 			board_names[board_id]);
2453e8c4d31SAmit Kucheria 		err = PTR_ERR(ptd->tzd);
2463e8c4d31SAmit Kucheria 		goto error_cleanup;
2473e8c4d31SAmit Kucheria 	}
248bbcf90c0SAndrzej Pietrasiewicz 	err = thermal_zone_device_enable(ptd->tzd);
249bbcf90c0SAndrzej Pietrasiewicz 	if (err)
250bbcf90c0SAndrzej Pietrasiewicz 		goto err_unregister;
2513e8c4d31SAmit Kucheria 
2523e8c4d31SAmit Kucheria 	return 0;
2533e8c4d31SAmit Kucheria 
254bbcf90c0SAndrzej Pietrasiewicz err_unregister:
255bbcf90c0SAndrzej Pietrasiewicz 	thermal_zone_device_unregister(ptd->tzd);
2563e8c4d31SAmit Kucheria error_cleanup:
2573e8c4d31SAmit Kucheria 	iounmap(ptd->hw_base);
2583e8c4d31SAmit Kucheria error_release:
2593e8c4d31SAmit Kucheria 	pci_release_regions(pdev);
2603e8c4d31SAmit Kucheria error_disable:
2613e8c4d31SAmit Kucheria 	pci_disable_device(pdev);
2623e8c4d31SAmit Kucheria 	dev_err(&pdev->dev, "pci device failed to probe\n");
2633e8c4d31SAmit Kucheria 	return err;
2643e8c4d31SAmit Kucheria }
2653e8c4d31SAmit Kucheria 
intel_pch_thermal_remove(struct pci_dev * pdev)2663e8c4d31SAmit Kucheria static void intel_pch_thermal_remove(struct pci_dev *pdev)
2673e8c4d31SAmit Kucheria {
2683e8c4d31SAmit Kucheria 	struct pch_thermal_device *ptd = pci_get_drvdata(pdev);
2693e8c4d31SAmit Kucheria 
2703e8c4d31SAmit Kucheria 	thermal_zone_device_unregister(ptd->tzd);
2713e8c4d31SAmit Kucheria 	iounmap(ptd->hw_base);
2723e8c4d31SAmit Kucheria 	pci_set_drvdata(pdev, NULL);
27366dd8b80SChuhong Yuan 	pci_release_regions(pdev);
2743e8c4d31SAmit Kucheria 	pci_disable_device(pdev);
2753e8c4d31SAmit Kucheria }
2763e8c4d31SAmit Kucheria 
intel_pch_thermal_suspend_noirq(struct device * device)27728708e19SZhang Rui static int intel_pch_thermal_suspend_noirq(struct device *device)
2783e8c4d31SAmit Kucheria {
27997e9cafeSChuhong Yuan 	struct pch_thermal_device *ptd = dev_get_drvdata(device);
280c5f43242SRafael J. Wysocki 	u16 pch_thr_temp, pch_cur_temp;
281c5f43242SRafael J. Wysocki 	int pch_delay_cnt = 0;
282c5f43242SRafael J. Wysocki 	u8 tsel;
2833e8c4d31SAmit Kucheria 
284c5f43242SRafael J. Wysocki 	/* Shutdown the thermal sensor if it is not enabled by BIOS */
285c5f43242SRafael J. Wysocki 	if (!ptd->bios_enabled) {
286c5f43242SRafael J. Wysocki 		tsel = readb(ptd->hw_base + WPT_TSEL);
287c5f43242SRafael J. Wysocki 		writeb(tsel & 0xFE, ptd->hw_base + WPT_TSEL);
288c5f43242SRafael J. Wysocki 		return 0;
289c5f43242SRafael J. Wysocki 	}
290c5f43242SRafael J. Wysocki 
291c5f43242SRafael J. Wysocki 	/* Do not check temperature if it is not s2idle */
292c5f43242SRafael J. Wysocki 	if (pm_suspend_via_firmware())
293c5f43242SRafael J. Wysocki 		return 0;
294c5f43242SRafael J. Wysocki 
295c5f43242SRafael J. Wysocki 	/* Get the PCH temperature threshold value */
296c5f43242SRafael J. Wysocki 	pch_thr_temp = GET_PCH_TEMP(WPT_TEMP_TSR & readw(ptd->hw_base + WPT_TSPM));
297c5f43242SRafael J. Wysocki 
298c5f43242SRafael J. Wysocki 	/* Get the PCH current temperature value */
299c5f43242SRafael J. Wysocki 	pch_cur_temp = GET_PCH_TEMP(WPT_TEMP_TSR & readw(ptd->hw_base + WPT_TEMP));
300c5f43242SRafael J. Wysocki 
301*68de0ae4SZhang Rui 	if (pch_cur_temp >= pch_thr_temp)
302*68de0ae4SZhang Rui 		dev_warn(&ptd->pdev->dev,
303*68de0ae4SZhang Rui 			"CPU-PCH current temp [%dC] higher than the threshold temp [%dC], S0ix might fail. Start cooling...\n",
304*68de0ae4SZhang Rui 			pch_cur_temp, pch_thr_temp);
305*68de0ae4SZhang Rui 
306c5f43242SRafael J. Wysocki 	/*
307c5f43242SRafael J. Wysocki 	 * If current PCH temperature is higher than configured PCH threshold
308c5f43242SRafael J. Wysocki 	 * value, run some delay loop with sleep to let the current temperature
309c5f43242SRafael J. Wysocki 	 * go down below the threshold value which helps to allow system enter
310c5f43242SRafael J. Wysocki 	 * lower power S0ix suspend state. Even after delay loop if PCH current
311c5f43242SRafael J. Wysocki 	 * temperature stays above threshold, notify the warning message
312c5f43242SRafael J. Wysocki 	 * which helps to indentify the reason why S0ix entry was rejected.
313c5f43242SRafael J. Wysocki 	 */
314c5f43242SRafael J. Wysocki 	while (pch_delay_cnt < delay_cnt) {
315c5f43242SRafael J. Wysocki 		if (pch_cur_temp < pch_thr_temp)
316c5f43242SRafael J. Wysocki 			break;
317c5f43242SRafael J. Wysocki 
318c5f43242SRafael J. Wysocki 		if (pm_wakeup_pending()) {
319c5f43242SRafael J. Wysocki 			dev_warn(&ptd->pdev->dev, "Wakeup event detected, abort cooling\n");
320c5f43242SRafael J. Wysocki 			return 0;
321c5f43242SRafael J. Wysocki 		}
322c5f43242SRafael J. Wysocki 
323c5f43242SRafael J. Wysocki 		pch_delay_cnt++;
324c5f43242SRafael J. Wysocki 		dev_dbg(&ptd->pdev->dev,
325c5f43242SRafael J. Wysocki 			"CPU-PCH current temp [%dC] higher than the threshold temp [%dC], sleep %d times for %d ms duration\n",
326c5f43242SRafael J. Wysocki 			pch_cur_temp, pch_thr_temp, pch_delay_cnt, delay_timeout);
327c5f43242SRafael J. Wysocki 		msleep(delay_timeout);
328c5f43242SRafael J. Wysocki 		/* Read the PCH current temperature for next cycle. */
329c5f43242SRafael J. Wysocki 		pch_cur_temp = GET_PCH_TEMP(WPT_TEMP_TSR & readw(ptd->hw_base + WPT_TEMP));
330c5f43242SRafael J. Wysocki 	}
331c5f43242SRafael J. Wysocki 
332c5f43242SRafael J. Wysocki 	if (pch_cur_temp >= pch_thr_temp)
333c5f43242SRafael J. Wysocki 		dev_warn(&ptd->pdev->dev,
334c5f43242SRafael J. Wysocki 			"CPU-PCH is hot [%dC] after %d ms delay. S0ix might fail\n",
335c5f43242SRafael J. Wysocki 			pch_cur_temp, pch_delay_cnt * delay_timeout);
336c5f43242SRafael J. Wysocki 	else {
337c5f43242SRafael J. Wysocki 		if (pch_delay_cnt)
338c5f43242SRafael J. Wysocki 			dev_info(&ptd->pdev->dev,
339c5f43242SRafael J. Wysocki 				"CPU-PCH is cool [%dC] after %d ms delay\n",
340c5f43242SRafael J. Wysocki 				pch_cur_temp, pch_delay_cnt * delay_timeout);
341c5f43242SRafael J. Wysocki 		else
342c5f43242SRafael J. Wysocki 			dev_info(&ptd->pdev->dev,
343c5f43242SRafael J. Wysocki 				"CPU-PCH is cool [%dC]\n",
344c5f43242SRafael J. Wysocki 				pch_cur_temp);
345c5f43242SRafael J. Wysocki 	}
346c5f43242SRafael J. Wysocki 
347c5f43242SRafael J. Wysocki 	return 0;
3483e8c4d31SAmit Kucheria }
3493e8c4d31SAmit Kucheria 
intel_pch_thermal_resume(struct device * device)3503e8c4d31SAmit Kucheria static int intel_pch_thermal_resume(struct device *device)
3513e8c4d31SAmit Kucheria {
35297e9cafeSChuhong Yuan 	struct pch_thermal_device *ptd = dev_get_drvdata(device);
353c5f43242SRafael J. Wysocki 	u8 tsel;
3543e8c4d31SAmit Kucheria 
355c5f43242SRafael J. Wysocki 	if (ptd->bios_enabled)
356c5f43242SRafael J. Wysocki 		return 0;
357c5f43242SRafael J. Wysocki 
358c5f43242SRafael J. Wysocki 	tsel = readb(ptd->hw_base + WPT_TSEL);
359c5f43242SRafael J. Wysocki 
360c5f43242SRafael J. Wysocki 	writeb(tsel | WPT_TSEL_ETS, ptd->hw_base + WPT_TSEL);
361c5f43242SRafael J. Wysocki 
362c5f43242SRafael J. Wysocki 	return 0;
3633e8c4d31SAmit Kucheria }
3643e8c4d31SAmit Kucheria 
3653e8c4d31SAmit Kucheria static const struct pci_device_id intel_pch_thermal_id[] = {
3663e8c4d31SAmit Kucheria 	{ PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCH_THERMAL_DID_HSW_1),
367ae98e57aSRafael J. Wysocki 		.driver_data = PCH_BOARD_HSW, },
3683e8c4d31SAmit Kucheria 	{ PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCH_THERMAL_DID_HSW_2),
369ae98e57aSRafael J. Wysocki 		.driver_data = PCH_BOARD_HSW, },
3703e8c4d31SAmit Kucheria 	{ PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCH_THERMAL_DID_WPT),
371ae98e57aSRafael J. Wysocki 		.driver_data = PCH_BOARD_WPT, },
3723e8c4d31SAmit Kucheria 	{ PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCH_THERMAL_DID_SKL),
373ae98e57aSRafael J. Wysocki 		.driver_data = PCH_BOARD_SKL, },
3743e8c4d31SAmit Kucheria 	{ PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCH_THERMAL_DID_SKL_H),
375ae98e57aSRafael J. Wysocki 		.driver_data = PCH_BOARD_SKL, },
3763e8c4d31SAmit Kucheria 	{ PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCH_THERMAL_DID_CNL),
377ae98e57aSRafael J. Wysocki 		.driver_data = PCH_BOARD_CNL, },
3783e8c4d31SAmit Kucheria 	{ PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCH_THERMAL_DID_CNL_H),
379ae98e57aSRafael J. Wysocki 		.driver_data = PCH_BOARD_CNL, },
380c569e805SSumeet Pawnikar 	{ PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCH_THERMAL_DID_CNL_LP),
381ae98e57aSRafael J. Wysocki 		.driver_data = PCH_BOARD_CNL, },
38235709c4eSGayatri Kammela 	{ PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCH_THERMAL_DID_CML_H),
383ae98e57aSRafael J. Wysocki 		.driver_data = PCH_BOARD_CML, },
384e78acf7eSAndres Freund 	{ PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCH_THERMAL_DID_LWB),
385ae98e57aSRafael J. Wysocki 		.driver_data = PCH_BOARD_LWB, },
38640dc1929STim Zimmermann 	{ PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCH_THERMAL_DID_WBG),
387ae98e57aSRafael J. Wysocki 		.driver_data = PCH_BOARD_WBG, },
3883e8c4d31SAmit Kucheria 	{ 0, },
3893e8c4d31SAmit Kucheria };
3903e8c4d31SAmit Kucheria MODULE_DEVICE_TABLE(pci, intel_pch_thermal_id);
3913e8c4d31SAmit Kucheria 
3923e8c4d31SAmit Kucheria static const struct dev_pm_ops intel_pch_pm_ops = {
39328708e19SZhang Rui 	.suspend_noirq = intel_pch_thermal_suspend_noirq,
3943e8c4d31SAmit Kucheria 	.resume = intel_pch_thermal_resume,
3953e8c4d31SAmit Kucheria };
3963e8c4d31SAmit Kucheria 
3973e8c4d31SAmit Kucheria static struct pci_driver intel_pch_thermal_driver = {
3983e8c4d31SAmit Kucheria 	.name		= "intel_pch_thermal",
3993e8c4d31SAmit Kucheria 	.id_table	= intel_pch_thermal_id,
4003e8c4d31SAmit Kucheria 	.probe		= intel_pch_thermal_probe,
4013e8c4d31SAmit Kucheria 	.remove		= intel_pch_thermal_remove,
4023e8c4d31SAmit Kucheria 	.driver.pm	= &intel_pch_pm_ops,
4033e8c4d31SAmit Kucheria };
4043e8c4d31SAmit Kucheria 
4053e8c4d31SAmit Kucheria module_pci_driver(intel_pch_thermal_driver);
4063e8c4d31SAmit Kucheria 
4073e8c4d31SAmit Kucheria MODULE_LICENSE("GPL v2");
4083e8c4d31SAmit Kucheria MODULE_DESCRIPTION("Intel PCH Thermal driver");
409