Thermal: introduce INT3402 thermal driver
authorAaron Lu <aaron.lu@intel.com>
Wed, 3 Sep 2014 07:13:02 +0000 (15:13 +0800)
committerZhang Rui <rui.zhang@intel.com>
Fri, 10 Oct 2014 06:02:25 +0000 (14:02 +0800)
ACPI INT3402 device object could report temperature for the memory module.
To expose such information to user space, a thermal zone device is registered
for it so that the thermal sysfs interface can expose such information for
userspace to use.

Signed-off-by: Aaron Lu <aaron.lu@intel.com>
Signed-off-by: Zhang Rui <rui.zhang@intel.com>
drivers/thermal/int340x_thermal/Makefile
drivers/thermal/int340x_thermal/int3402_thermal.c [new file with mode: 0644]
include/linux/thermal.h

index e10a53b..67c98fd 100644 (file)
@@ -1 +1,2 @@
 obj-$(CONFIG_INT340X_THERMAL)  += int3400_thermal.o
+obj-$(CONFIG_INT340X_THERMAL)  += int3402_thermal.o
diff --git a/drivers/thermal/int340x_thermal/int3402_thermal.c b/drivers/thermal/int340x_thermal/int3402_thermal.c
new file mode 100644 (file)
index 0000000..a5d08c1
--- /dev/null
@@ -0,0 +1,242 @@
+/*
+ * INT3402 thermal driver for memory temperature reporting
+ *
+ * Copyright (C) 2014, Intel Corporation
+ * Authors: Aaron Lu <aaron.lu@intel.com>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2 as
+ * published by the Free Software Foundation.
+ *
+ */
+
+#include <linux/module.h>
+#include <linux/platform_device.h>
+#include <linux/acpi.h>
+#include <linux/thermal.h>
+
+#define ACPI_ACTIVE_COOLING_MAX_NR 10
+
+struct active_trip {
+       unsigned long temp;
+       int id;
+       bool valid;
+};
+
+struct int3402_thermal_data {
+       unsigned long *aux_trips;
+       int aux_trip_nr;
+       unsigned long psv_temp;
+       int psv_trip_id;
+       unsigned long crt_temp;
+       int crt_trip_id;
+       unsigned long hot_temp;
+       int hot_trip_id;
+       struct active_trip act_trips[ACPI_ACTIVE_COOLING_MAX_NR];
+       acpi_handle *handle;
+};
+
+static int int3402_thermal_get_zone_temp(struct thermal_zone_device *zone,
+                                        unsigned long *temp)
+{
+       struct int3402_thermal_data *d = zone->devdata;
+       unsigned long long tmp;
+       acpi_status status;
+
+       status = acpi_evaluate_integer(d->handle, "_TMP", NULL, &tmp);
+       if (ACPI_FAILURE(status))
+               return -ENODEV;
+
+       /* _TMP returns the temperature in tenths of degrees Kelvin */
+       *temp = DECI_KELVIN_TO_MILLICELSIUS(tmp);
+
+       return 0;
+}
+
+static int int3402_thermal_get_trip_temp(struct thermal_zone_device *zone,
+                                        int trip, unsigned long *temp)
+{
+       struct int3402_thermal_data *d = zone->devdata;
+       int i;
+
+       if (trip < d->aux_trip_nr)
+               *temp = d->aux_trips[trip];
+       else if (trip == d->crt_trip_id)
+               *temp = d->crt_temp;
+       else if (trip == d->psv_trip_id)
+               *temp = d->psv_temp;
+       else if (trip == d->hot_trip_id)
+               *temp = d->hot_temp;
+       else {
+               for (i = 0; i < ACPI_ACTIVE_COOLING_MAX_NR; i++) {
+                       if (d->act_trips[i].valid &&
+                           d->act_trips[i].id == trip) {
+                               *temp = d->act_trips[i].temp;
+                               break;
+                       }
+               }
+               if (i == ACPI_ACTIVE_COOLING_MAX_NR)
+                       return -EINVAL;
+       }
+       return 0;
+}
+
+static int int3402_thermal_get_trip_type(struct thermal_zone_device *zone,
+                                        int trip, enum thermal_trip_type *type)
+{
+       struct int3402_thermal_data *d = zone->devdata;
+       int i;
+
+       if (trip < d->aux_trip_nr)
+               *type = THERMAL_TRIP_PASSIVE;
+       else if (trip == d->crt_trip_id)
+               *type = THERMAL_TRIP_CRITICAL;
+       else if (trip == d->hot_trip_id)
+               *type = THERMAL_TRIP_HOT;
+       else if (trip == d->psv_trip_id)
+               *type = THERMAL_TRIP_PASSIVE;
+       else {
+               for (i = 0; i < ACPI_ACTIVE_COOLING_MAX_NR; i++) {
+                       if (d->act_trips[i].valid &&
+                           d->act_trips[i].id == trip) {
+                               *type = THERMAL_TRIP_ACTIVE;
+                               break;
+                       }
+               }
+               if (i == ACPI_ACTIVE_COOLING_MAX_NR)
+                       return -EINVAL;
+       }
+       return 0;
+}
+
+static int int3402_thermal_set_trip_temp(struct thermal_zone_device *zone, int trip,
+                                 unsigned long temp)
+{
+       struct int3402_thermal_data *d = zone->devdata;
+       acpi_status status;
+       char name[10];
+
+       snprintf(name, sizeof(name), "PAT%d", trip);
+       status = acpi_execute_simple_method(d->handle, name,
+                       MILLICELSIUS_TO_DECI_KELVIN(temp));
+       if (ACPI_FAILURE(status))
+               return -EIO;
+
+       d->aux_trips[trip] = temp;
+       return 0;
+}
+
+static struct thermal_zone_device_ops int3402_thermal_zone_ops = {
+       .get_temp       = int3402_thermal_get_zone_temp,
+       .get_trip_temp  = int3402_thermal_get_trip_temp,
+       .get_trip_type  = int3402_thermal_get_trip_type,
+       .set_trip_temp  = int3402_thermal_set_trip_temp,
+};
+
+static struct thermal_zone_params int3402_thermal_params = {
+       .governor_name = "user_space",
+       .no_hwmon = true,
+};
+
+static int int3402_thermal_get_temp(acpi_handle handle, char *name,
+                                   unsigned long *temp)
+{
+       unsigned long long r;
+       acpi_status status;
+
+       status = acpi_evaluate_integer(handle, name, NULL, &r);
+       if (ACPI_FAILURE(status))
+               return -EIO;
+
+       *temp = DECI_KELVIN_TO_MILLICELSIUS(r);
+       return 0;
+}
+
+static int int3402_thermal_probe(struct platform_device *pdev)
+{
+       struct acpi_device *adev = ACPI_COMPANION(&pdev->dev);
+       struct int3402_thermal_data *d;
+       struct thermal_zone_device *zone;
+       acpi_status status;
+       unsigned long long trip_cnt;
+       int trip_mask = 0, i;
+
+       if (!acpi_has_method(adev->handle, "_TMP"))
+               return -ENODEV;
+
+       d = devm_kzalloc(&pdev->dev, sizeof(*d), GFP_KERNEL);
+       if (!d)
+               return -ENOMEM;
+
+       status = acpi_evaluate_integer(adev->handle, "PATC", NULL, &trip_cnt);
+       if (ACPI_FAILURE(status))
+               trip_cnt = 0;
+       else {
+               d->aux_trips = devm_kzalloc(&pdev->dev,
+                               sizeof(*d->aux_trips) * trip_cnt, GFP_KERNEL);
+               if (!d->aux_trips)
+                       return -ENOMEM;
+               trip_mask = trip_cnt - 1;
+               d->handle = adev->handle;
+               d->aux_trip_nr = trip_cnt;
+       }
+
+       d->crt_trip_id = -1;
+       if (!int3402_thermal_get_temp(adev->handle, "_CRT", &d->crt_temp))
+               d->crt_trip_id = trip_cnt++;
+       d->hot_trip_id = -1;
+       if (!int3402_thermal_get_temp(adev->handle, "_HOT", &d->hot_temp))
+               d->hot_trip_id = trip_cnt++;
+       d->psv_trip_id = -1;
+       if (!int3402_thermal_get_temp(adev->handle, "_PSV", &d->psv_temp))
+               d->psv_trip_id = trip_cnt++;
+       for (i = 0; i < ACPI_ACTIVE_COOLING_MAX_NR; i++) {
+               char name[5] = { '_', 'A', 'C', '0' + i, '\0' };
+               if (int3402_thermal_get_temp(adev->handle, name,
+                                            &d->act_trips[i].temp))
+                       break;
+               d->act_trips[i].id = trip_cnt++;
+               d->act_trips[i].valid = true;
+       }
+
+       zone = thermal_zone_device_register(acpi_device_bid(adev), trip_cnt,
+                                           trip_mask, d,
+                                           &int3402_thermal_zone_ops,
+                                           &int3402_thermal_params,
+                                           0, 0);
+       if (IS_ERR(zone))
+               return PTR_ERR(zone);
+       platform_set_drvdata(pdev, zone);
+
+       return 0;
+}
+
+static int int3402_thermal_remove(struct platform_device *pdev)
+{
+       struct thermal_zone_device *zone = platform_get_drvdata(pdev);
+
+       thermal_zone_device_unregister(zone);
+       return 0;
+}
+
+static const struct acpi_device_id int3402_thermal_match[] = {
+       {"INT3402", 0},
+       {}
+};
+
+MODULE_DEVICE_TABLE(acpi, int3402_thermal_match);
+
+static struct platform_driver int3402_thermal_driver = {
+       .probe = int3402_thermal_probe,
+       .remove = int3402_thermal_remove,
+       .driver = {
+                  .name = "int3402 thermal",
+                  .owner = THIS_MODULE,
+                  .acpi_match_table = int3402_thermal_match,
+                  },
+};
+
+module_platform_driver(int3402_thermal_driver);
+
+MODULE_DESCRIPTION("INT3402 Thermal driver");
+MODULE_LICENSE("GPL");
index 79ce6b9..ef90838 100644 (file)
@@ -46,6 +46,8 @@
 #define CELSIUS_TO_KELVIN(t)   ((t)*10+2732)
 #define DECI_KELVIN_TO_MILLICELSIUS_WITH_OFFSET(t, off) (((t) - (off)) * 100)
 #define DECI_KELVIN_TO_MILLICELSIUS(t) DECI_KELVIN_TO_MILLICELSIUS_WITH_OFFSET(t, 2732)
+#define MILLICELSIUS_TO_DECI_KELVIN_WITH_OFFSET(t, off) (((t) / 100) + (off))
+#define MILLICELSIUS_TO_DECI_KELVIN(t) MILLICELSIUS_TO_DECI_KELVIN_WITH_OFFSET(t, 2732)
 
 /* Adding event notification support elements */
 #define THERMAL_GENL_FAMILY_NAME                "thermal_event"