treewide: remove redundant IS_ERR() before error code check
[linux/fpc-iii.git] / drivers / thermal / intel / intel_pch_thermal.c
blob56401fd4708d9904585bd9c4a37df798b91cafd9
1 // SPDX-License-Identifier: GPL-2.0-only
2 /* intel_pch_thermal.c - Intel PCH Thermal driver
4 * Copyright (c) 2015, Intel Corporation.
6 * Authors:
7 * Tushar Dave <tushar.n.dave@intel.com>
8 */
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>
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;
62 struct pci_dev *pdev;
63 struct thermal_zone_device *tzd;
64 int crt_trip_id;
65 unsigned long crt_temp;
66 int hot_trip_id;
67 unsigned long hot_temp;
68 int psv_trip_id;
69 unsigned long psv_temp;
70 bool bios_enabled;
73 #ifdef CONFIG_ACPI
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,
81 int *nr_trips)
83 struct acpi_device *adev;
85 ptd->psv_trip_id = -1;
87 adev = ACPI_COMPANION(&ptd->pdev->dev);
88 if (adev) {
89 unsigned long long r;
90 acpi_status status;
92 status = acpi_evaluate_integer(adev->handle, "_PSV", NULL,
93 &r);
94 if (ACPI_SUCCESS(status)) {
95 unsigned long trip_temp;
97 trip_temp = deci_kelvin_to_millicelsius(r);
98 if (trip_temp) {
99 ptd->psv_temp = trip_temp;
100 ptd->psv_trip_id = *nr_trips;
101 ++(*nr_trips);
106 #else
107 static void pch_wpt_add_acpi_psv_trip(struct pch_thermal_device *ptd,
108 int *nr_trips)
110 ptd->psv_trip_id = -1;
113 #endif
115 static int pch_wpt_init(struct pch_thermal_device *ptd, int *nr_trips)
117 u8 tsel;
118 u16 trip_temp;
120 *nr_trips = 0;
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;
125 goto read_trips;
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");
135 return -ENODEV;
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");
141 return -ENODEV;
144 read_trips:
145 ptd->crt_trip_id = -1;
146 trip_temp = readw(ptd->hw_base + WPT_CTT);
147 trip_temp &= 0x1FF;
148 if (trip_temp) {
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;
152 ++(*nr_trips);
155 ptd->hot_trip_id = -1;
156 trip_temp = readw(ptd->hw_base + WPT_PHL);
157 trip_temp &= 0x1FF;
158 if (trip_temp) {
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;
162 ++(*nr_trips);
165 pch_wpt_add_acpi_psv_trip(ptd, nr_trips);
167 return 0;
170 static int pch_wpt_get_temp(struct pch_thermal_device *ptd, int *temp)
172 u16 wpt_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);
179 return 0;
182 static int pch_wpt_suspend(struct pch_thermal_device *ptd)
184 u8 tsel;
186 if (ptd->bios_enabled)
187 return 0;
189 tsel = readb(ptd->hw_base + WPT_TSEL);
191 writeb(tsel & 0xFE, ptd->hw_base + WPT_TSEL);
193 return 0;
196 static int pch_wpt_resume(struct pch_thermal_device *ptd)
198 u8 tsel;
200 if (ptd->bios_enabled)
201 return 0;
203 tsel = readb(ptd->hw_base + WPT_TSEL);
205 writeb(tsel | WPT_TSEL_ETS, ptd->hw_base + WPT_TSEL);
207 return 0;
210 struct pch_dev_ops {
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;
244 else
245 return -EINVAL;
247 return 0;
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;
260 else
261 return -EINVAL;
263 return 0;
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,
272 enum board_ids {
273 board_hsw,
274 board_wpt,
275 board_skl,
276 board_cnl,
277 board_cml,
280 static const struct board_info {
281 const char *name;
282 const struct pch_dev_ops *ops;
283 } board_info[] = {
284 [board_hsw] = {
285 .name = "pch_haswell",
286 .ops = &pch_dev_ops_wpt,
288 [board_wpt] = {
289 .name = "pch_wildcat_point",
290 .ops = &pch_dev_ops_wpt,
292 [board_skl] = {
293 .name = "pch_skylake",
294 .ops = &pch_dev_ops_wpt,
296 [board_cnl] = {
297 .name = "pch_cannonlake",
298 .ops = &pch_dev_ops_wpt,
300 [board_cml] = {
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;
312 int err;
313 int nr_trips;
315 ptd = devm_kzalloc(&pdev->dev, sizeof(*ptd), GFP_KERNEL);
316 if (!ptd)
317 return -ENOMEM;
319 ptd->ops = bi->ops;
321 pci_set_drvdata(pdev, ptd);
322 ptd->pdev = pdev;
324 err = pci_enable_device(pdev);
325 if (err) {
326 dev_err(&pdev->dev, "failed to enable pci device\n");
327 return err;
330 err = pci_request_regions(pdev, driver_name);
331 if (err) {
332 dev_err(&pdev->dev, "failed to request pci region\n");
333 goto error_disable;
336 ptd->hw_base = pci_ioremap_bar(pdev, 0);
337 if (!ptd->hw_base) {
338 err = -ENOMEM;
339 dev_err(&pdev->dev, "failed to map mem base\n");
340 goto error_release;
343 err = ptd->ops->hw_init(ptd, &nr_trips);
344 if (err)
345 goto error_cleanup;
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",
351 bi->name);
352 err = PTR_ERR(ptd->tzd);
353 goto error_cleanup;
356 return 0;
358 error_cleanup:
359 iounmap(ptd->hw_base);
360 error_release:
361 pci_release_regions(pdev);
362 error_disable:
363 pci_disable_device(pdev);
364 dev_err(&pdev->dev, "pci device failed to probe\n");
365 return err;
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, },
410 { 0, },
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");