ubi/upd: Always flush after prepared for an update
[linux/fpc-iii.git] / drivers / thermal / int340x_thermal / processor_thermal_device.c
blobccc0ad02d06698108ec486d11f7b0dc69c7c734a
1 /*
2 * processor_thermal_device.c
3 * Copyright (c) 2014, Intel Corporation.
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms and conditions of the GNU General Public License,
7 * version 2, as published by the Free Software Foundation.
9 * This program is distributed in the hope it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12 * more details.
15 #include <linux/kernel.h>
16 #include <linux/module.h>
17 #include <linux/init.h>
18 #include <linux/pci.h>
19 #include <linux/interrupt.h>
20 #include <linux/platform_device.h>
21 #include <linux/acpi.h>
22 #include <linux/thermal.h>
23 #include "int340x_thermal_zone.h"
24 #include "../intel_soc_dts_iosf.h"
26 /* Broadwell-U/HSB thermal reporting device */
27 #define PCI_DEVICE_ID_PROC_BDW_THERMAL 0x1603
28 #define PCI_DEVICE_ID_PROC_HSB_THERMAL 0x0A03
30 /* Skylake thermal reporting device */
31 #define PCI_DEVICE_ID_PROC_SKL_THERMAL 0x1903
33 /* Braswell thermal reporting device */
34 #define PCI_DEVICE_ID_PROC_BSW_THERMAL 0x22DC
36 struct power_config {
37 u32 index;
38 u32 min_uw;
39 u32 max_uw;
40 u32 tmin_us;
41 u32 tmax_us;
42 u32 step_uw;
45 struct proc_thermal_device {
46 struct device *dev;
47 struct acpi_device *adev;
48 struct power_config power_limits[2];
49 struct int34x_thermal_zone *int340x_zone;
50 struct intel_soc_dts_sensors *soc_dts;
53 enum proc_thermal_emum_mode_type {
54 PROC_THERMAL_NONE,
55 PROC_THERMAL_PCI,
56 PROC_THERMAL_PLATFORM_DEV
60 * We can have only one type of enumeration, PCI or Platform,
61 * not both. So we don't need instance specific data.
63 static enum proc_thermal_emum_mode_type proc_thermal_emum_mode =
64 PROC_THERMAL_NONE;
66 #define POWER_LIMIT_SHOW(index, suffix) \
67 static ssize_t power_limit_##index##_##suffix##_show(struct device *dev, \
68 struct device_attribute *attr, \
69 char *buf) \
70 { \
71 struct pci_dev *pci_dev; \
72 struct platform_device *pdev; \
73 struct proc_thermal_device *proc_dev; \
75 if (proc_thermal_emum_mode == PROC_THERMAL_PLATFORM_DEV) { \
76 pdev = to_platform_device(dev); \
77 proc_dev = platform_get_drvdata(pdev); \
78 } else { \
79 pci_dev = to_pci_dev(dev); \
80 proc_dev = pci_get_drvdata(pci_dev); \
81 } \
82 return sprintf(buf, "%lu\n",\
83 (unsigned long)proc_dev->power_limits[index].suffix * 1000); \
86 POWER_LIMIT_SHOW(0, min_uw)
87 POWER_LIMIT_SHOW(0, max_uw)
88 POWER_LIMIT_SHOW(0, step_uw)
89 POWER_LIMIT_SHOW(0, tmin_us)
90 POWER_LIMIT_SHOW(0, tmax_us)
92 POWER_LIMIT_SHOW(1, min_uw)
93 POWER_LIMIT_SHOW(1, max_uw)
94 POWER_LIMIT_SHOW(1, step_uw)
95 POWER_LIMIT_SHOW(1, tmin_us)
96 POWER_LIMIT_SHOW(1, tmax_us)
98 static DEVICE_ATTR_RO(power_limit_0_min_uw);
99 static DEVICE_ATTR_RO(power_limit_0_max_uw);
100 static DEVICE_ATTR_RO(power_limit_0_step_uw);
101 static DEVICE_ATTR_RO(power_limit_0_tmin_us);
102 static DEVICE_ATTR_RO(power_limit_0_tmax_us);
104 static DEVICE_ATTR_RO(power_limit_1_min_uw);
105 static DEVICE_ATTR_RO(power_limit_1_max_uw);
106 static DEVICE_ATTR_RO(power_limit_1_step_uw);
107 static DEVICE_ATTR_RO(power_limit_1_tmin_us);
108 static DEVICE_ATTR_RO(power_limit_1_tmax_us);
110 static struct attribute *power_limit_attrs[] = {
111 &dev_attr_power_limit_0_min_uw.attr,
112 &dev_attr_power_limit_1_min_uw.attr,
113 &dev_attr_power_limit_0_max_uw.attr,
114 &dev_attr_power_limit_1_max_uw.attr,
115 &dev_attr_power_limit_0_step_uw.attr,
116 &dev_attr_power_limit_1_step_uw.attr,
117 &dev_attr_power_limit_0_tmin_us.attr,
118 &dev_attr_power_limit_1_tmin_us.attr,
119 &dev_attr_power_limit_0_tmax_us.attr,
120 &dev_attr_power_limit_1_tmax_us.attr,
121 NULL
124 static struct attribute_group power_limit_attribute_group = {
125 .attrs = power_limit_attrs,
126 .name = "power_limits"
129 static int stored_tjmax; /* since it is fixed, we can have local storage */
131 static int get_tjmax(void)
133 u32 eax, edx;
134 u32 val;
135 int err;
137 err = rdmsr_safe(MSR_IA32_TEMPERATURE_TARGET, &eax, &edx);
138 if (err)
139 return err;
141 val = (eax >> 16) & 0xff;
142 if (val)
143 return val;
145 return -EINVAL;
148 static int read_temp_msr(int *temp)
150 int cpu;
151 u32 eax, edx;
152 int err;
153 unsigned long curr_temp_off = 0;
155 *temp = 0;
157 for_each_online_cpu(cpu) {
158 err = rdmsr_safe_on_cpu(cpu, MSR_IA32_THERM_STATUS, &eax,
159 &edx);
160 if (err)
161 goto err_ret;
162 else {
163 if (eax & 0x80000000) {
164 curr_temp_off = (eax >> 16) & 0x7f;
165 if (!*temp || curr_temp_off < *temp)
166 *temp = curr_temp_off;
167 } else {
168 err = -EINVAL;
169 goto err_ret;
174 return 0;
175 err_ret:
176 return err;
179 static int proc_thermal_get_zone_temp(struct thermal_zone_device *zone,
180 int *temp)
182 int ret;
184 ret = read_temp_msr(temp);
185 if (!ret)
186 *temp = (stored_tjmax - *temp) * 1000;
188 return ret;
191 static struct thermal_zone_device_ops proc_thermal_local_ops = {
192 .get_temp = proc_thermal_get_zone_temp,
195 static int proc_thermal_add(struct device *dev,
196 struct proc_thermal_device **priv)
198 struct proc_thermal_device *proc_priv;
199 struct acpi_device *adev;
200 acpi_status status;
201 struct acpi_buffer buf = { ACPI_ALLOCATE_BUFFER, NULL };
202 union acpi_object *elements, *ppcc;
203 union acpi_object *p;
204 unsigned long long tmp;
205 struct thermal_zone_device_ops *ops = NULL;
206 int i;
207 int ret;
209 adev = ACPI_COMPANION(dev);
210 if (!adev)
211 return -ENODEV;
213 status = acpi_evaluate_object(adev->handle, "PPCC", NULL, &buf);
214 if (ACPI_FAILURE(status))
215 return -ENODEV;
217 p = buf.pointer;
218 if (!p || (p->type != ACPI_TYPE_PACKAGE)) {
219 dev_err(dev, "Invalid PPCC data\n");
220 ret = -EFAULT;
221 goto free_buffer;
223 if (!p->package.count) {
224 dev_err(dev, "Invalid PPCC package size\n");
225 ret = -EFAULT;
226 goto free_buffer;
229 proc_priv = devm_kzalloc(dev, sizeof(*proc_priv), GFP_KERNEL);
230 if (!proc_priv) {
231 ret = -ENOMEM;
232 goto free_buffer;
235 proc_priv->dev = dev;
236 proc_priv->adev = adev;
238 for (i = 0; i < min((int)p->package.count - 1, 2); ++i) {
239 elements = &(p->package.elements[i+1]);
240 if (elements->type != ACPI_TYPE_PACKAGE ||
241 elements->package.count != 6) {
242 ret = -EFAULT;
243 goto free_buffer;
245 ppcc = elements->package.elements;
246 proc_priv->power_limits[i].index = ppcc[0].integer.value;
247 proc_priv->power_limits[i].min_uw = ppcc[1].integer.value;
248 proc_priv->power_limits[i].max_uw = ppcc[2].integer.value;
249 proc_priv->power_limits[i].tmin_us = ppcc[3].integer.value;
250 proc_priv->power_limits[i].tmax_us = ppcc[4].integer.value;
251 proc_priv->power_limits[i].step_uw = ppcc[5].integer.value;
254 *priv = proc_priv;
256 ret = sysfs_create_group(&dev->kobj,
257 &power_limit_attribute_group);
258 if (ret)
259 goto free_buffer;
261 status = acpi_evaluate_integer(adev->handle, "_TMP", NULL, &tmp);
262 if (ACPI_FAILURE(status)) {
263 /* there is no _TMP method, add local method */
264 stored_tjmax = get_tjmax();
265 if (stored_tjmax > 0)
266 ops = &proc_thermal_local_ops;
269 proc_priv->int340x_zone = int340x_thermal_zone_add(adev, ops);
270 if (IS_ERR(proc_priv->int340x_zone)) {
271 sysfs_remove_group(&proc_priv->dev->kobj,
272 &power_limit_attribute_group);
273 ret = PTR_ERR(proc_priv->int340x_zone);
274 } else
275 ret = 0;
277 free_buffer:
278 kfree(buf.pointer);
280 return ret;
283 static void proc_thermal_remove(struct proc_thermal_device *proc_priv)
285 int340x_thermal_zone_remove(proc_priv->int340x_zone);
286 sysfs_remove_group(&proc_priv->dev->kobj,
287 &power_limit_attribute_group);
290 static int int3401_add(struct platform_device *pdev)
292 struct proc_thermal_device *proc_priv;
293 int ret;
295 if (proc_thermal_emum_mode == PROC_THERMAL_PCI) {
296 dev_err(&pdev->dev, "error: enumerated as PCI dev\n");
297 return -ENODEV;
300 ret = proc_thermal_add(&pdev->dev, &proc_priv);
301 if (ret)
302 return ret;
304 platform_set_drvdata(pdev, proc_priv);
305 proc_thermal_emum_mode = PROC_THERMAL_PLATFORM_DEV;
307 return 0;
310 static int int3401_remove(struct platform_device *pdev)
312 proc_thermal_remove(platform_get_drvdata(pdev));
314 return 0;
317 static irqreturn_t proc_thermal_pci_msi_irq(int irq, void *devid)
319 struct proc_thermal_device *proc_priv;
320 struct pci_dev *pdev = devid;
322 proc_priv = pci_get_drvdata(pdev);
324 intel_soc_dts_iosf_interrupt_handler(proc_priv->soc_dts);
326 return IRQ_HANDLED;
329 static int proc_thermal_pci_probe(struct pci_dev *pdev,
330 const struct pci_device_id *unused)
332 struct proc_thermal_device *proc_priv;
333 int ret;
335 if (proc_thermal_emum_mode == PROC_THERMAL_PLATFORM_DEV) {
336 dev_err(&pdev->dev, "error: enumerated as platform dev\n");
337 return -ENODEV;
340 ret = pci_enable_device(pdev);
341 if (ret < 0) {
342 dev_err(&pdev->dev, "error: could not enable device\n");
343 return ret;
346 ret = proc_thermal_add(&pdev->dev, &proc_priv);
347 if (ret) {
348 pci_disable_device(pdev);
349 return ret;
352 pci_set_drvdata(pdev, proc_priv);
353 proc_thermal_emum_mode = PROC_THERMAL_PCI;
355 if (pdev->device == PCI_DEVICE_ID_PROC_BSW_THERMAL) {
357 * Enumerate additional DTS sensors available via IOSF.
358 * But we are not treating as a failure condition, if
359 * there are no aux DTSs enabled or fails. This driver
360 * already exposes sensors, which can be accessed via
361 * ACPI/MSR. So we don't want to fail for auxiliary DTSs.
363 proc_priv->soc_dts = intel_soc_dts_iosf_init(
364 INTEL_SOC_DTS_INTERRUPT_MSI, 2, 0);
366 if (proc_priv->soc_dts && pdev->irq) {
367 ret = pci_enable_msi(pdev);
368 if (!ret) {
369 ret = request_threaded_irq(pdev->irq, NULL,
370 proc_thermal_pci_msi_irq,
371 IRQF_ONESHOT, "proc_thermal",
372 pdev);
373 if (ret) {
374 intel_soc_dts_iosf_exit(
375 proc_priv->soc_dts);
376 pci_disable_msi(pdev);
377 proc_priv->soc_dts = NULL;
380 } else
381 dev_err(&pdev->dev, "No auxiliary DTSs enabled\n");
384 return 0;
387 static void proc_thermal_pci_remove(struct pci_dev *pdev)
389 struct proc_thermal_device *proc_priv = pci_get_drvdata(pdev);
391 if (proc_priv->soc_dts) {
392 intel_soc_dts_iosf_exit(proc_priv->soc_dts);
393 if (pdev->irq) {
394 free_irq(pdev->irq, pdev);
395 pci_disable_msi(pdev);
398 proc_thermal_remove(proc_priv);
399 pci_disable_device(pdev);
402 static const struct pci_device_id proc_thermal_pci_ids[] = {
403 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_PROC_BDW_THERMAL)},
404 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_PROC_HSB_THERMAL)},
405 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_PROC_SKL_THERMAL)},
406 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_PROC_BSW_THERMAL)},
407 { 0, },
410 MODULE_DEVICE_TABLE(pci, proc_thermal_pci_ids);
412 static struct pci_driver proc_thermal_pci_driver = {
413 .name = "proc_thermal",
414 .probe = proc_thermal_pci_probe,
415 .remove = proc_thermal_pci_remove,
416 .id_table = proc_thermal_pci_ids,
419 static const struct acpi_device_id int3401_device_ids[] = {
420 {"INT3401", 0},
421 {"", 0},
423 MODULE_DEVICE_TABLE(acpi, int3401_device_ids);
425 static struct platform_driver int3401_driver = {
426 .probe = int3401_add,
427 .remove = int3401_remove,
428 .driver = {
429 .name = "int3401 thermal",
430 .acpi_match_table = int3401_device_ids,
434 static int __init proc_thermal_init(void)
436 int ret;
438 ret = platform_driver_register(&int3401_driver);
439 if (ret)
440 return ret;
442 ret = pci_register_driver(&proc_thermal_pci_driver);
444 return ret;
447 static void __exit proc_thermal_exit(void)
449 platform_driver_unregister(&int3401_driver);
450 pci_unregister_driver(&proc_thermal_pci_driver);
453 module_init(proc_thermal_init);
454 module_exit(proc_thermal_exit);
456 MODULE_AUTHOR("Srinivas Pandruvada <srinivas.pandruvada@linux.intel.com>");
457 MODULE_DESCRIPTION("Processor Thermal Reporting Device Driver");
458 MODULE_LICENSE("GPL v2");