driver core: Make Kconfig text for DEBUG_TEST_DRIVER_REMOVE stronger
[cascardo/linux.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/thermal.h>
24 #include <linux/pm.h>
25
26 /* Intel PCH thermal Device IDs */
27 #define PCH_THERMAL_DID_WPT     0x9CA4 /* Wildcat Point */
28 #define PCH_THERMAL_DID_SKL     0x9D31 /* Skylake 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    0x00ff  /* 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         bool bios_enabled;
70 };
71
72 static int pch_wpt_init(struct pch_thermal_device *ptd, int *nr_trips)
73 {
74         u8 tsel;
75         u16 trip_temp;
76
77         *nr_trips = 0;
78
79         /* Check if BIOS has already enabled thermal sensor */
80         if (WPT_TSS_TSDSS & readb(ptd->hw_base + WPT_TSS)) {
81                 ptd->bios_enabled = true;
82                 goto read_trips;
83         }
84
85         tsel = readb(ptd->hw_base + WPT_TSEL);
86         /*
87          * When TSEL's Policy Lock-Down bit is 1, TSEL become RO.
88          * If so, thermal sensor cannot enable. Bail out.
89          */
90         if (tsel & WPT_TSEL_PLDB) {
91                 dev_err(&ptd->pdev->dev, "Sensor can't be enabled\n");
92                 return -ENODEV;
93         }
94
95         writeb(tsel|WPT_TSEL_ETS, ptd->hw_base + WPT_TSEL);
96         if (!(WPT_TSS_TSDSS & readb(ptd->hw_base + WPT_TSS))) {
97                 dev_err(&ptd->pdev->dev, "Sensor can't be enabled\n");
98                 return -ENODEV;
99         }
100
101 read_trips:
102         ptd->crt_trip_id = -1;
103         trip_temp = readw(ptd->hw_base + WPT_CTT);
104         trip_temp &= 0x1FF;
105         if (trip_temp) {
106                 /* Resolution of 1/2 degree C and an offset of -50C */
107                 ptd->crt_temp = trip_temp * 1000 / 2 - 50000;
108                 ptd->crt_trip_id = 0;
109                 ++(*nr_trips);
110         }
111
112         ptd->hot_trip_id = -1;
113         trip_temp = readw(ptd->hw_base + WPT_PHL);
114         trip_temp &= 0x1FF;
115         if (trip_temp) {
116                 /* Resolution of 1/2 degree C and an offset of -50C */
117                 ptd->hot_temp = trip_temp * 1000 / 2 - 50000;
118                 ptd->hot_trip_id = *nr_trips;
119                 ++(*nr_trips);
120         }
121
122         return 0;
123 }
124
125 static int pch_wpt_get_temp(struct pch_thermal_device *ptd, int *temp)
126 {
127         u8 wpt_temp;
128
129         wpt_temp = WPT_TEMP_TSR & readl(ptd->hw_base + WPT_TEMP);
130
131         /* Resolution of 1/2 degree C and an offset of -50C */
132         *temp = (wpt_temp * 1000 / 2 - 50000);
133
134         return 0;
135 }
136
137 static int pch_wpt_suspend(struct pch_thermal_device *ptd)
138 {
139         u8 tsel;
140
141         if (ptd->bios_enabled)
142                 return 0;
143
144         tsel = readb(ptd->hw_base + WPT_TSEL);
145
146         writeb(tsel & 0xFE, ptd->hw_base + WPT_TSEL);
147
148         return 0;
149 }
150
151 static int pch_wpt_resume(struct pch_thermal_device *ptd)
152 {
153         u8 tsel;
154
155         if (ptd->bios_enabled)
156                 return 0;
157
158         tsel = readb(ptd->hw_base + WPT_TSEL);
159
160         writeb(tsel | WPT_TSEL_ETS, ptd->hw_base + WPT_TSEL);
161
162         return 0;
163 }
164
165 struct pch_dev_ops {
166         int (*hw_init)(struct pch_thermal_device *ptd, int *nr_trips);
167         int (*get_temp)(struct pch_thermal_device *ptd, int *temp);
168         int (*suspend)(struct pch_thermal_device *ptd);
169         int (*resume)(struct pch_thermal_device *ptd);
170 };
171
172
173 /* dev ops for Wildcat Point */
174 static const struct pch_dev_ops pch_dev_ops_wpt = {
175         .hw_init = pch_wpt_init,
176         .get_temp = pch_wpt_get_temp,
177         .suspend = pch_wpt_suspend,
178         .resume = pch_wpt_resume,
179 };
180
181 static int pch_thermal_get_temp(struct thermal_zone_device *tzd, int *temp)
182 {
183         struct pch_thermal_device *ptd = tzd->devdata;
184
185         return  ptd->ops->get_temp(ptd, temp);
186 }
187
188 static int pch_get_trip_type(struct thermal_zone_device *tzd, int trip,
189                              enum thermal_trip_type *type)
190 {
191         struct pch_thermal_device *ptd = tzd->devdata;
192
193         if (ptd->crt_trip_id == trip)
194                 *type = THERMAL_TRIP_CRITICAL;
195         else if (ptd->hot_trip_id == trip)
196                 *type = THERMAL_TRIP_HOT;
197         else
198                 return -EINVAL;
199
200         return 0;
201 }
202
203 static int pch_get_trip_temp(struct thermal_zone_device *tzd, int trip, int *temp)
204 {
205         struct pch_thermal_device *ptd = tzd->devdata;
206
207         if (ptd->crt_trip_id == trip)
208                 *temp = ptd->crt_temp;
209         else if (ptd->hot_trip_id == trip)
210                 *temp = ptd->hot_temp;
211         else
212                 return -EINVAL;
213
214         return 0;
215 }
216
217 static struct thermal_zone_device_ops tzd_ops = {
218         .get_temp = pch_thermal_get_temp,
219         .get_trip_type = pch_get_trip_type,
220         .get_trip_temp = pch_get_trip_temp,
221 };
222
223
224 static int intel_pch_thermal_probe(struct pci_dev *pdev,
225                                    const struct pci_device_id *id)
226 {
227         struct pch_thermal_device *ptd;
228         int err;
229         int nr_trips;
230         char *dev_name;
231
232         ptd = devm_kzalloc(&pdev->dev, sizeof(*ptd), GFP_KERNEL);
233         if (!ptd)
234                 return -ENOMEM;
235
236         switch (pdev->device) {
237         case PCH_THERMAL_DID_WPT:
238                 ptd->ops = &pch_dev_ops_wpt;
239                 dev_name = "pch_wildcat_point";
240                 break;
241         case PCH_THERMAL_DID_SKL:
242                 ptd->ops = &pch_dev_ops_wpt;
243                 dev_name = "pch_skylake";
244                 break;
245         default:
246                 dev_err(&pdev->dev, "unknown pch thermal device\n");
247                 return -ENODEV;
248         }
249
250         pci_set_drvdata(pdev, ptd);
251         ptd->pdev = pdev;
252
253         err = pci_enable_device(pdev);
254         if (err) {
255                 dev_err(&pdev->dev, "failed to enable pci device\n");
256                 return err;
257         }
258
259         err = pci_request_regions(pdev, driver_name);
260         if (err) {
261                 dev_err(&pdev->dev, "failed to request pci region\n");
262                 goto error_disable;
263         }
264
265         ptd->hw_base = pci_ioremap_bar(pdev, 0);
266         if (!ptd->hw_base) {
267                 err = -ENOMEM;
268                 dev_err(&pdev->dev, "failed to map mem base\n");
269                 goto error_release;
270         }
271
272         err = ptd->ops->hw_init(ptd, &nr_trips);
273         if (err)
274                 goto error_cleanup;
275
276         ptd->tzd = thermal_zone_device_register(dev_name, nr_trips, 0, ptd,
277                                                 &tzd_ops, NULL, 0, 0);
278         if (IS_ERR(ptd->tzd)) {
279                 dev_err(&pdev->dev, "Failed to register thermal zone %s\n",
280                         dev_name);
281                 err = PTR_ERR(ptd->tzd);
282                 goto error_cleanup;
283         }
284
285         return 0;
286
287 error_cleanup:
288         iounmap(ptd->hw_base);
289 error_release:
290         pci_release_regions(pdev);
291 error_disable:
292         pci_disable_device(pdev);
293         dev_err(&pdev->dev, "pci device failed to probe\n");
294         return err;
295 }
296
297 static void intel_pch_thermal_remove(struct pci_dev *pdev)
298 {
299         struct pch_thermal_device *ptd = pci_get_drvdata(pdev);
300
301         thermal_zone_device_unregister(ptd->tzd);
302         iounmap(ptd->hw_base);
303         pci_set_drvdata(pdev, NULL);
304         pci_release_region(pdev, 0);
305         pci_disable_device(pdev);
306 }
307
308 static int intel_pch_thermal_suspend(struct device *device)
309 {
310         struct pci_dev *pdev = to_pci_dev(device);
311         struct pch_thermal_device *ptd = pci_get_drvdata(pdev);
312
313         return ptd->ops->suspend(ptd);
314 }
315
316 static int intel_pch_thermal_resume(struct device *device)
317 {
318         struct pci_dev *pdev = to_pci_dev(device);
319         struct pch_thermal_device *ptd = pci_get_drvdata(pdev);
320
321         return ptd->ops->resume(ptd);
322 }
323
324 static struct pci_device_id intel_pch_thermal_id[] = {
325         { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCH_THERMAL_DID_WPT) },
326         { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCH_THERMAL_DID_SKL) },
327         { 0, },
328 };
329 MODULE_DEVICE_TABLE(pci, intel_pch_thermal_id);
330
331 static const struct dev_pm_ops intel_pch_pm_ops = {
332         .suspend = intel_pch_thermal_suspend,
333         .resume = intel_pch_thermal_resume,
334 };
335
336 static struct pci_driver intel_pch_thermal_driver = {
337         .name           = "intel_pch_thermal",
338         .id_table       = intel_pch_thermal_id,
339         .probe          = intel_pch_thermal_probe,
340         .remove         = intel_pch_thermal_remove,
341         .driver.pm      = &intel_pch_pm_ops,
342 };
343
344 module_pci_driver(intel_pch_thermal_driver);
345
346 MODULE_LICENSE("GPL v2");
347 MODULE_DESCRIPTION("Intel PCH Thermal driver");