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