1 // SPDX-License-Identifier: GPL-2.0-only
2 /* intel_pch_thermal.c - Intel PCH Thermal driver
3  *
4  * Copyright (c) 2015, Intel Corporation.
5  *
6  * Authors:
7  *     Tushar Dave <tushar.n.dave@intel.com>
8  */
9 
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>
17 #include <linux/pm.h>
18 
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_CNL_LP	0x02F9 /* CNL-LP PCH */
28 #define PCH_THERMAL_DID_CML_H	0X06F9 /* CML-H PCH */
29 
30 /* Wildcat Point-LP  PCH Thermal registers */
31 #define WPT_TEMP	0x0000	/* Temperature */
32 #define WPT_TSC	0x04	/* Thermal Sensor Control */
33 #define WPT_TSS	0x06	/* Thermal Sensor Status */
34 #define WPT_TSEL	0x08	/* Thermal Sensor Enable and Lock */
35 #define WPT_TSREL	0x0A	/* Thermal Sensor Report Enable and Lock */
36 #define WPT_TSMIC	0x0C	/* Thermal Sensor SMI Control */
37 #define WPT_CTT	0x0010	/* Catastrophic Trip Point */
38 #define WPT_TAHV	0x0014	/* Thermal Alert High Value */
39 #define WPT_TALV	0x0018	/* Thermal Alert Low Value */
40 #define WPT_TL		0x00000040	/* Throttle Value */
41 #define WPT_PHL	0x0060	/* PCH Hot Level */
42 #define WPT_PHLC	0x62	/* PHL Control */
43 #define WPT_TAS	0x80	/* Thermal Alert Status */
44 #define WPT_TSPIEN	0x82	/* PCI Interrupt Event Enables */
45 #define WPT_TSGPEN	0x84	/* General Purpose Event Enables */
46 
47 /*  Wildcat Point-LP  PCH Thermal Register bit definitions */
48 #define WPT_TEMP_TSR	0x01ff	/* Temp TS Reading */
49 #define WPT_TSC_CPDE	0x01	/* Catastrophic Power-Down Enable */
50 #define WPT_TSS_TSDSS	0x10	/* Thermal Sensor Dynamic Shutdown Status */
51 #define WPT_TSS_GPES	0x08	/* GPE status */
52 #define WPT_TSEL_ETS	0x01    /* Enable TS */
53 #define WPT_TSEL_PLDB	0x80	/* TSEL Policy Lock-Down Bit */
54 #define WPT_TL_TOL	0x000001FF	/* T0 Level */
55 #define WPT_TL_T1L	0x1ff00000	/* T1 Level */
56 #define WPT_TL_TTEN	0x20000000	/* TT Enable */
57 
58 static char driver_name[] = "Intel PCH thermal driver";
59 
60 struct pch_thermal_device {
61 	void __iomem *hw_base;
62 	const struct pch_dev_ops *ops;
63 	struct pci_dev *pdev;
64 	struct thermal_zone_device *tzd;
65 	int crt_trip_id;
66 	unsigned long crt_temp;
67 	int hot_trip_id;
68 	unsigned long hot_temp;
69 	int psv_trip_id;
70 	unsigned long psv_temp;
71 	bool bios_enabled;
72 };
73 
74 #ifdef CONFIG_ACPI
75 
76 /*
77  * On some platforms, there is a companion ACPI device, which adds
78  * passive trip temperature using _PSV method. There is no specific
79  * passive temperature setting in MMIO interface of this PCI device.
80  */
81 static void pch_wpt_add_acpi_psv_trip(struct pch_thermal_device *ptd,
82 				      int *nr_trips)
83 {
84 	struct acpi_device *adev;
85 
86 	ptd->psv_trip_id = -1;
87 
88 	adev = ACPI_COMPANION(&ptd->pdev->dev);
89 	if (adev) {
90 		unsigned long long r;
91 		acpi_status status;
92 
93 		status = acpi_evaluate_integer(adev->handle, "_PSV", NULL,
94 					       &r);
95 		if (ACPI_SUCCESS(status)) {
96 			unsigned long trip_temp;
97 
98 			trip_temp = deci_kelvin_to_millicelsius(r);
99 			if (trip_temp) {
100 				ptd->psv_temp = trip_temp;
101 				ptd->psv_trip_id = *nr_trips;
102 				++(*nr_trips);
103 			}
104 		}
105 	}
106 }
107 #else
108 static void pch_wpt_add_acpi_psv_trip(struct pch_thermal_device *ptd,
109 				      int *nr_trips)
110 {
111 	ptd->psv_trip_id = -1;
112 
113 }
114 #endif
115 
116 static int pch_wpt_init(struct pch_thermal_device *ptd, int *nr_trips)
117 {
118 	u8 tsel;
119 	u16 trip_temp;
120 
121 	*nr_trips = 0;
122 
123 	/* Check if BIOS has already enabled thermal sensor */
124 	if (WPT_TSEL_ETS & readb(ptd->hw_base + WPT_TSEL)) {
125 		ptd->bios_enabled = true;
126 		goto read_trips;
127 	}
128 
129 	tsel = readb(ptd->hw_base + WPT_TSEL);
130 	/*
131 	 * When TSEL's Policy Lock-Down bit is 1, TSEL become RO.
132 	 * If so, thermal sensor cannot enable. Bail out.
133 	 */
134 	if (tsel & WPT_TSEL_PLDB) {
135 		dev_err(&ptd->pdev->dev, "Sensor can't be enabled\n");
136 		return -ENODEV;
137 	}
138 
139 	writeb(tsel|WPT_TSEL_ETS, ptd->hw_base + WPT_TSEL);
140 	if (!(WPT_TSEL_ETS & readb(ptd->hw_base + WPT_TSEL))) {
141 		dev_err(&ptd->pdev->dev, "Sensor can't be enabled\n");
142 		return -ENODEV;
143 	}
144 
145 read_trips:
146 	ptd->crt_trip_id = -1;
147 	trip_temp = readw(ptd->hw_base + WPT_CTT);
148 	trip_temp &= 0x1FF;
149 	if (trip_temp) {
150 		/* Resolution of 1/2 degree C and an offset of -50C */
151 		ptd->crt_temp = trip_temp * 1000 / 2 - 50000;
152 		ptd->crt_trip_id = 0;
153 		++(*nr_trips);
154 	}
155 
156 	ptd->hot_trip_id = -1;
157 	trip_temp = readw(ptd->hw_base + WPT_PHL);
158 	trip_temp &= 0x1FF;
159 	if (trip_temp) {
160 		/* Resolution of 1/2 degree C and an offset of -50C */
161 		ptd->hot_temp = trip_temp * 1000 / 2 - 50000;
162 		ptd->hot_trip_id = *nr_trips;
163 		++(*nr_trips);
164 	}
165 
166 	pch_wpt_add_acpi_psv_trip(ptd, nr_trips);
167 
168 	return 0;
169 }
170 
171 static int pch_wpt_get_temp(struct pch_thermal_device *ptd, int *temp)
172 {
173 	u16 wpt_temp;
174 
175 	wpt_temp = WPT_TEMP_TSR & readw(ptd->hw_base + WPT_TEMP);
176 
177 	/* Resolution of 1/2 degree C and an offset of -50C */
178 	*temp = (wpt_temp * 1000 / 2 - 50000);
179 
180 	return 0;
181 }
182 
183 static int pch_wpt_suspend(struct pch_thermal_device *ptd)
184 {
185 	u8 tsel;
186 
187 	if (ptd->bios_enabled)
188 		return 0;
189 
190 	tsel = readb(ptd->hw_base + WPT_TSEL);
191 
192 	writeb(tsel & 0xFE, ptd->hw_base + WPT_TSEL);
193 
194 	return 0;
195 }
196 
197 static int pch_wpt_resume(struct pch_thermal_device *ptd)
198 {
199 	u8 tsel;
200 
201 	if (ptd->bios_enabled)
202 		return 0;
203 
204 	tsel = readb(ptd->hw_base + WPT_TSEL);
205 
206 	writeb(tsel | WPT_TSEL_ETS, ptd->hw_base + WPT_TSEL);
207 
208 	return 0;
209 }
210 
211 struct pch_dev_ops {
212 	int (*hw_init)(struct pch_thermal_device *ptd, int *nr_trips);
213 	int (*get_temp)(struct pch_thermal_device *ptd, int *temp);
214 	int (*suspend)(struct pch_thermal_device *ptd);
215 	int (*resume)(struct pch_thermal_device *ptd);
216 };
217 
218 
219 /* dev ops for Wildcat Point */
220 static const struct pch_dev_ops pch_dev_ops_wpt = {
221 	.hw_init = pch_wpt_init,
222 	.get_temp = pch_wpt_get_temp,
223 	.suspend = pch_wpt_suspend,
224 	.resume = pch_wpt_resume,
225 };
226 
227 static int pch_thermal_get_temp(struct thermal_zone_device *tzd, int *temp)
228 {
229 	struct pch_thermal_device *ptd = tzd->devdata;
230 
231 	return	ptd->ops->get_temp(ptd, temp);
232 }
233 
234 static int pch_get_trip_type(struct thermal_zone_device *tzd, int trip,
235 			     enum thermal_trip_type *type)
236 {
237 	struct pch_thermal_device *ptd = tzd->devdata;
238 
239 	if (ptd->crt_trip_id == trip)
240 		*type = THERMAL_TRIP_CRITICAL;
241 	else if (ptd->hot_trip_id == trip)
242 		*type = THERMAL_TRIP_HOT;
243 	else if (ptd->psv_trip_id == trip)
244 		*type = THERMAL_TRIP_PASSIVE;
245 	else
246 		return -EINVAL;
247 
248 	return 0;
249 }
250 
251 static int pch_get_trip_temp(struct thermal_zone_device *tzd, int trip, int *temp)
252 {
253 	struct pch_thermal_device *ptd = tzd->devdata;
254 
255 	if (ptd->crt_trip_id == trip)
256 		*temp = ptd->crt_temp;
257 	else if (ptd->hot_trip_id == trip)
258 		*temp = ptd->hot_temp;
259 	else if (ptd->psv_trip_id == trip)
260 		*temp = ptd->psv_temp;
261 	else
262 		return -EINVAL;
263 
264 	return 0;
265 }
266 
267 static struct thermal_zone_device_ops tzd_ops = {
268 	.get_temp = pch_thermal_get_temp,
269 	.get_trip_type = pch_get_trip_type,
270 	.get_trip_temp = pch_get_trip_temp,
271 };
272 
273 enum board_ids {
274 	board_hsw,
275 	board_wpt,
276 	board_skl,
277 	board_cnl,
278 	board_cml,
279 };
280 
281 static const struct board_info {
282 	const char *name;
283 	const struct pch_dev_ops *ops;
284 } board_info[] = {
285 	[board_hsw] = {
286 		.name = "pch_haswell",
287 		.ops = &pch_dev_ops_wpt,
288 	},
289 	[board_wpt] = {
290 		.name = "pch_wildcat_point",
291 		.ops = &pch_dev_ops_wpt,
292 	},
293 	[board_skl] = {
294 		.name = "pch_skylake",
295 		.ops = &pch_dev_ops_wpt,
296 	},
297 	[board_cnl] = {
298 		.name = "pch_cannonlake",
299 		.ops = &pch_dev_ops_wpt,
300 	},
301 	[board_cml] = {
302 		.name = "pch_cometlake",
303 		.ops = &pch_dev_ops_wpt,
304 	}
305 };
306 
307 static int intel_pch_thermal_probe(struct pci_dev *pdev,
308 				   const struct pci_device_id *id)
309 {
310 	enum board_ids board_id = id->driver_data;
311 	const struct board_info *bi = &board_info[board_id];
312 	struct pch_thermal_device *ptd;
313 	int err;
314 	int nr_trips;
315 
316 	ptd = devm_kzalloc(&pdev->dev, sizeof(*ptd), GFP_KERNEL);
317 	if (!ptd)
318 		return -ENOMEM;
319 
320 	ptd->ops = bi->ops;
321 
322 	pci_set_drvdata(pdev, ptd);
323 	ptd->pdev = pdev;
324 
325 	err = pci_enable_device(pdev);
326 	if (err) {
327 		dev_err(&pdev->dev, "failed to enable pci device\n");
328 		return err;
329 	}
330 
331 	err = pci_request_regions(pdev, driver_name);
332 	if (err) {
333 		dev_err(&pdev->dev, "failed to request pci region\n");
334 		goto error_disable;
335 	}
336 
337 	ptd->hw_base = pci_ioremap_bar(pdev, 0);
338 	if (!ptd->hw_base) {
339 		err = -ENOMEM;
340 		dev_err(&pdev->dev, "failed to map mem base\n");
341 		goto error_release;
342 	}
343 
344 	err = ptd->ops->hw_init(ptd, &nr_trips);
345 	if (err)
346 		goto error_cleanup;
347 
348 	ptd->tzd = thermal_zone_device_register(bi->name, nr_trips, 0, ptd,
349 						&tzd_ops, NULL, 0, 0);
350 	if (IS_ERR(ptd->tzd)) {
351 		dev_err(&pdev->dev, "Failed to register thermal zone %s\n",
352 			bi->name);
353 		err = PTR_ERR(ptd->tzd);
354 		goto error_cleanup;
355 	}
356 	err = thermal_zone_device_enable(ptd->tzd);
357 	if (err)
358 		goto err_unregister;
359 
360 	return 0;
361 
362 err_unregister:
363 	thermal_zone_device_unregister(ptd->tzd);
364 error_cleanup:
365 	iounmap(ptd->hw_base);
366 error_release:
367 	pci_release_regions(pdev);
368 error_disable:
369 	pci_disable_device(pdev);
370 	dev_err(&pdev->dev, "pci device failed to probe\n");
371 	return err;
372 }
373 
374 static void intel_pch_thermal_remove(struct pci_dev *pdev)
375 {
376 	struct pch_thermal_device *ptd = pci_get_drvdata(pdev);
377 
378 	thermal_zone_device_unregister(ptd->tzd);
379 	iounmap(ptd->hw_base);
380 	pci_set_drvdata(pdev, NULL);
381 	pci_release_regions(pdev);
382 	pci_disable_device(pdev);
383 }
384 
385 static int intel_pch_thermal_suspend(struct device *device)
386 {
387 	struct pch_thermal_device *ptd = dev_get_drvdata(device);
388 
389 	return ptd->ops->suspend(ptd);
390 }
391 
392 static int intel_pch_thermal_resume(struct device *device)
393 {
394 	struct pch_thermal_device *ptd = dev_get_drvdata(device);
395 
396 	return ptd->ops->resume(ptd);
397 }
398 
399 static const struct pci_device_id intel_pch_thermal_id[] = {
400 	{ PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCH_THERMAL_DID_HSW_1),
401 		.driver_data = board_hsw, },
402 	{ PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCH_THERMAL_DID_HSW_2),
403 		.driver_data = board_hsw, },
404 	{ PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCH_THERMAL_DID_WPT),
405 		.driver_data = board_wpt, },
406 	{ PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCH_THERMAL_DID_SKL),
407 		.driver_data = board_skl, },
408 	{ PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCH_THERMAL_DID_SKL_H),
409 		.driver_data = board_skl, },
410 	{ PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCH_THERMAL_DID_CNL),
411 		.driver_data = board_cnl, },
412 	{ PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCH_THERMAL_DID_CNL_H),
413 		.driver_data = board_cnl, },
414 	{ PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCH_THERMAL_DID_CNL_LP),
415 		.driver_data = board_cnl, },
416 	{ PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCH_THERMAL_DID_CML_H),
417 		.driver_data = board_cml, },
418 	{ 0, },
419 };
420 MODULE_DEVICE_TABLE(pci, intel_pch_thermal_id);
421 
422 static const struct dev_pm_ops intel_pch_pm_ops = {
423 	.suspend = intel_pch_thermal_suspend,
424 	.resume = intel_pch_thermal_resume,
425 };
426 
427 static struct pci_driver intel_pch_thermal_driver = {
428 	.name		= "intel_pch_thermal",
429 	.id_table	= intel_pch_thermal_id,
430 	.probe		= intel_pch_thermal_probe,
431 	.remove		= intel_pch_thermal_remove,
432 	.driver.pm	= &intel_pch_pm_ops,
433 };
434 
435 module_pci_driver(intel_pch_thermal_driver);
436 
437 MODULE_LICENSE("GPL v2");
438 MODULE_DESCRIPTION("Intel PCH Thermal driver");
439