thermal: rockchip: add temperature dump when panic
[firefly-linux-kernel-4.4.55.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
25 /* Intel PCH thermal Device IDs */
26 #define PCH_THERMAL_DID_WPT     0x9CA4 /* Wildcat Point */
27
28 /* Wildcat Point-LP  PCH Thermal registers */
29 #define WPT_TEMP        0x0000  /* Temperature */
30 #define WPT_TSC 0x04    /* Thermal Sensor Control */
31 #define WPT_TSS 0x06    /* Thermal Sensor Status */
32 #define WPT_TSEL        0x08    /* Thermal Sensor Enable and Lock */
33 #define WPT_TSREL       0x0A    /* Thermal Sensor Report Enable and Lock */
34 #define WPT_TSMIC       0x0C    /* Thermal Sensor SMI Control */
35 #define WPT_CTT 0x0010  /* Catastrophic Trip Point */
36 #define WPT_TAHV        0x0014  /* Thermal Alert High Value */
37 #define WPT_TALV        0x0018  /* Thermal Alert Low Value */
38 #define WPT_TL          0x00000040      /* Throttle Value */
39 #define WPT_PHL 0x0060  /* PCH Hot Level */
40 #define WPT_PHLC        0x62    /* PHL Control */
41 #define WPT_TAS 0x80    /* Thermal Alert Status */
42 #define WPT_TSPIEN      0x82    /* PCI Interrupt Event Enables */
43 #define WPT_TSGPEN      0x84    /* General Purpose Event Enables */
44
45 /*  Wildcat Point-LP  PCH Thermal Register bit definitions */
46 #define WPT_TEMP_TSR    0x00ff  /* Temp TS Reading */
47 #define WPT_TSC_CPDE    0x01    /* Catastrophic Power-Down Enable */
48 #define WPT_TSS_TSDSS   0x10    /* Thermal Sensor Dynamic Shutdown Status */
49 #define WPT_TSS_GPES    0x08    /* GPE status */
50 #define WPT_TSEL_ETS    0x01    /* Enable TS */
51 #define WPT_TSEL_PLDB   0x80    /* TSEL Policy Lock-Down Bit */
52 #define WPT_TL_TOL      0x000001FF      /* T0 Level */
53 #define WPT_TL_T1L      0x1ff00000      /* T1 Level */
54 #define WPT_TL_TTEN     0x20000000      /* TT Enable */
55
56 static char driver_name[] = "Intel PCH thermal driver";
57
58 struct pch_thermal_device {
59         void __iomem *hw_base;
60         const struct pch_dev_ops *ops;
61         struct pci_dev *pdev;
62         struct thermal_zone_device *tzd;
63         int crt_trip_id;
64         unsigned long crt_temp;
65         int hot_trip_id;
66         unsigned long hot_temp;
67 };
68
69 static int pch_wpt_init(struct pch_thermal_device *ptd, int *nr_trips)
70 {
71         u8 tsel;
72         u16 trip_temp;
73
74         *nr_trips = 0;
75
76         /* Check if BIOS has already enabled thermal sensor */
77         if (WPT_TSS_TSDSS & readb(ptd->hw_base + WPT_TSS))
78                 goto read_trips;
79
80         tsel = readb(ptd->hw_base + WPT_TSEL);
81         /*
82          * When TSEL's Policy Lock-Down bit is 1, TSEL become RO.
83          * If so, thermal sensor cannot enable. Bail out.
84          */
85         if (tsel & WPT_TSEL_PLDB) {
86                 dev_err(&ptd->pdev->dev, "Sensor can't be enabled\n");
87                 return -ENODEV;
88         }
89
90         writeb(tsel|WPT_TSEL_ETS, ptd->hw_base + WPT_TSEL);
91         if (!(WPT_TSS_TSDSS & readb(ptd->hw_base + WPT_TSS))) {
92                 dev_err(&ptd->pdev->dev, "Sensor can't be enabled\n");
93                 return -ENODEV;
94         }
95
96 read_trips:
97         ptd->crt_trip_id = -1;
98         trip_temp = readw(ptd->hw_base + WPT_CTT);
99         trip_temp &= 0x1FF;
100         if (trip_temp) {
101                 /* Resolution of 1/2 degree C and an offset of -50C */
102                 ptd->crt_temp = trip_temp * 1000 / 2 - 50000;
103                 ptd->crt_trip_id = 0;
104                 ++(*nr_trips);
105         }
106
107         ptd->hot_trip_id = -1;
108         trip_temp = readw(ptd->hw_base + WPT_PHL);
109         trip_temp &= 0x1FF;
110         if (trip_temp) {
111                 /* Resolution of 1/2 degree C and an offset of -50C */
112                 ptd->hot_temp = trip_temp * 1000 / 2 - 50000;
113                 ptd->hot_trip_id = *nr_trips;
114                 ++(*nr_trips);
115         }
116
117         return 0;
118 }
119
120 static int pch_wpt_get_temp(struct pch_thermal_device *ptd, int *temp)
121 {
122         u8 wpt_temp;
123
124         wpt_temp = WPT_TEMP_TSR & readl(ptd->hw_base + WPT_TEMP);
125
126         /* Resolution of 1/2 degree C and an offset of -50C */
127         *temp = (wpt_temp * 1000 / 2 - 50000);
128
129         return 0;
130 }
131
132 struct pch_dev_ops {
133         int (*hw_init)(struct pch_thermal_device *ptd, int *nr_trips);
134         int (*get_temp)(struct pch_thermal_device *ptd, int *temp);
135 };
136
137
138 /* dev ops for Wildcat Point */
139 static struct pch_dev_ops pch_dev_ops_wpt = {
140         .hw_init = pch_wpt_init,
141         .get_temp = pch_wpt_get_temp,
142 };
143
144 static int pch_thermal_get_temp(struct thermal_zone_device *tzd, int *temp)
145 {
146         struct pch_thermal_device *ptd = tzd->devdata;
147
148         return  ptd->ops->get_temp(ptd, temp);
149 }
150
151 static int pch_get_trip_type(struct thermal_zone_device *tzd, int trip,
152                              enum thermal_trip_type *type)
153 {
154         struct pch_thermal_device *ptd = tzd->devdata;
155
156         if (ptd->crt_trip_id == trip)
157                 *type = THERMAL_TRIP_CRITICAL;
158         else if (ptd->hot_trip_id == trip)
159                 *type = THERMAL_TRIP_HOT;
160         else
161                 return -EINVAL;
162
163         return 0;
164 }
165
166 static int pch_get_trip_temp(struct thermal_zone_device *tzd, int trip, int *temp)
167 {
168         struct pch_thermal_device *ptd = tzd->devdata;
169
170         if (ptd->crt_trip_id == trip)
171                 *temp = ptd->crt_temp;
172         else if (ptd->hot_trip_id == trip)
173                 *temp = ptd->hot_temp;
174         else
175                 return -EINVAL;
176
177         return 0;
178 }
179
180 static struct thermal_zone_device_ops tzd_ops = {
181         .get_temp = pch_thermal_get_temp,
182         .get_trip_type = pch_get_trip_type,
183         .get_trip_temp = pch_get_trip_temp,
184 };
185
186
187 static int intel_pch_thermal_probe(struct pci_dev *pdev,
188                                    const struct pci_device_id *id)
189 {
190         struct pch_thermal_device *ptd;
191         int err;
192         int nr_trips;
193         char *dev_name;
194
195         ptd = devm_kzalloc(&pdev->dev, sizeof(*ptd), GFP_KERNEL);
196         if (!ptd)
197                 return -ENOMEM;
198
199         switch (pdev->device) {
200         case PCH_THERMAL_DID_WPT:
201                 ptd->ops = &pch_dev_ops_wpt;
202                 dev_name = "pch_wildcat_point";
203                 break;
204         default:
205                 dev_err(&pdev->dev, "unknown pch thermal device\n");
206                 return -ENODEV;
207         }
208
209         pci_set_drvdata(pdev, ptd);
210         ptd->pdev = pdev;
211
212         err = pci_enable_device(pdev);
213         if (err) {
214                 dev_err(&pdev->dev, "failed to enable pci device\n");
215                 return err;
216         }
217
218         err = pci_request_regions(pdev, driver_name);
219         if (err) {
220                 dev_err(&pdev->dev, "failed to request pci region\n");
221                 goto error_disable;
222         }
223
224         ptd->hw_base = pci_ioremap_bar(pdev, 0);
225         if (!ptd->hw_base) {
226                 err = -ENOMEM;
227                 dev_err(&pdev->dev, "failed to map mem base\n");
228                 goto error_release;
229         }
230
231         err = ptd->ops->hw_init(ptd, &nr_trips);
232         if (err)
233                 goto error_cleanup;
234
235         ptd->tzd = thermal_zone_device_register(dev_name, nr_trips, 0, ptd,
236                                                 &tzd_ops, NULL, 0, 0);
237         if (IS_ERR(ptd->tzd)) {
238                 dev_err(&pdev->dev, "Failed to register thermal zone %s\n",
239                         dev_name);
240                 err = PTR_ERR(ptd->tzd);
241                 goto error_cleanup;
242         }
243
244         return 0;
245
246 error_cleanup:
247         iounmap(ptd->hw_base);
248 error_release:
249         pci_release_regions(pdev);
250 error_disable:
251         pci_disable_device(pdev);
252         dev_err(&pdev->dev, "pci device failed to probe\n");
253         return err;
254 }
255
256 static void intel_pch_thermal_remove(struct pci_dev *pdev)
257 {
258         struct pch_thermal_device *ptd = pci_get_drvdata(pdev);
259
260         thermal_zone_device_unregister(ptd->tzd);
261         iounmap(ptd->hw_base);
262         pci_set_drvdata(pdev, NULL);
263         pci_release_region(pdev, 0);
264         pci_disable_device(pdev);
265 }
266
267 static struct pci_device_id intel_pch_thermal_id[] = {
268         { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCH_THERMAL_DID_WPT) },
269         { 0, },
270 };
271 MODULE_DEVICE_TABLE(pci, intel_pch_thermal_id);
272
273 static struct pci_driver intel_pch_thermal_driver = {
274         .name           = "intel_pch_thermal",
275         .id_table       = intel_pch_thermal_id,
276         .probe          = intel_pch_thermal_probe,
277         .remove         = intel_pch_thermal_remove,
278 };
279
280 module_pci_driver(intel_pch_thermal_driver);
281
282 MODULE_LICENSE("GPL v2");
283 MODULE_DESCRIPTION("Intel PCH Thermal driver");