2 * INT3402 thermal driver for memory temperature reporting
4 * Copyright (C) 2014, Intel Corporation
5 * Authors: Aaron Lu <aaron.lu@intel.com>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
13 #include <linux/module.h>
14 #include <linux/platform_device.h>
15 #include <linux/acpi.h>
16 #include <linux/thermal.h>
18 #define ACPI_ACTIVE_COOLING_MAX_NR 10
26 struct int3402_thermal_data {
27 unsigned long *aux_trips;
29 unsigned long psv_temp;
31 unsigned long crt_temp;
33 unsigned long hot_temp;
35 struct active_trip act_trips[ACPI_ACTIVE_COOLING_MAX_NR];
39 static int int3402_thermal_get_zone_temp(struct thermal_zone_device *zone,
42 struct int3402_thermal_data *d = zone->devdata;
43 unsigned long long tmp;
46 status = acpi_evaluate_integer(d->handle, "_TMP", NULL, &tmp);
47 if (ACPI_FAILURE(status))
50 /* _TMP returns the temperature in tenths of degrees Kelvin */
51 *temp = DECI_KELVIN_TO_MILLICELSIUS(tmp);
56 static int int3402_thermal_get_trip_temp(struct thermal_zone_device *zone,
57 int trip, unsigned long *temp)
59 struct int3402_thermal_data *d = zone->devdata;
62 if (trip < d->aux_trip_nr)
63 *temp = d->aux_trips[trip];
64 else if (trip == d->crt_trip_id)
66 else if (trip == d->psv_trip_id)
68 else if (trip == d->hot_trip_id)
71 for (i = 0; i < ACPI_ACTIVE_COOLING_MAX_NR; i++) {
72 if (d->act_trips[i].valid &&
73 d->act_trips[i].id == trip) {
74 *temp = d->act_trips[i].temp;
78 if (i == ACPI_ACTIVE_COOLING_MAX_NR)
84 static int int3402_thermal_get_trip_type(struct thermal_zone_device *zone,
85 int trip, enum thermal_trip_type *type)
87 struct int3402_thermal_data *d = zone->devdata;
90 if (trip < d->aux_trip_nr)
91 *type = THERMAL_TRIP_PASSIVE;
92 else if (trip == d->crt_trip_id)
93 *type = THERMAL_TRIP_CRITICAL;
94 else if (trip == d->hot_trip_id)
95 *type = THERMAL_TRIP_HOT;
96 else if (trip == d->psv_trip_id)
97 *type = THERMAL_TRIP_PASSIVE;
99 for (i = 0; i < ACPI_ACTIVE_COOLING_MAX_NR; i++) {
100 if (d->act_trips[i].valid &&
101 d->act_trips[i].id == trip) {
102 *type = THERMAL_TRIP_ACTIVE;
106 if (i == ACPI_ACTIVE_COOLING_MAX_NR)
112 static int int3402_thermal_set_trip_temp(struct thermal_zone_device *zone, int trip,
115 struct int3402_thermal_data *d = zone->devdata;
119 snprintf(name, sizeof(name), "PAT%d", trip);
120 status = acpi_execute_simple_method(d->handle, name,
121 MILLICELSIUS_TO_DECI_KELVIN(temp));
122 if (ACPI_FAILURE(status))
125 d->aux_trips[trip] = temp;
129 static struct thermal_zone_device_ops int3402_thermal_zone_ops = {
130 .get_temp = int3402_thermal_get_zone_temp,
131 .get_trip_temp = int3402_thermal_get_trip_temp,
132 .get_trip_type = int3402_thermal_get_trip_type,
133 .set_trip_temp = int3402_thermal_set_trip_temp,
136 static struct thermal_zone_params int3402_thermal_params = {
137 .governor_name = "user_space",
141 static int int3402_thermal_get_temp(acpi_handle handle, char *name,
144 unsigned long long r;
147 status = acpi_evaluate_integer(handle, name, NULL, &r);
148 if (ACPI_FAILURE(status))
151 *temp = DECI_KELVIN_TO_MILLICELSIUS(r);
155 static int int3402_thermal_probe(struct platform_device *pdev)
157 struct acpi_device *adev = ACPI_COMPANION(&pdev->dev);
158 struct int3402_thermal_data *d;
159 struct thermal_zone_device *zone;
161 unsigned long long trip_cnt;
162 int trip_mask = 0, i;
164 if (!acpi_has_method(adev->handle, "_TMP"))
167 d = devm_kzalloc(&pdev->dev, sizeof(*d), GFP_KERNEL);
171 status = acpi_evaluate_integer(adev->handle, "PATC", NULL, &trip_cnt);
172 if (ACPI_FAILURE(status))
175 d->aux_trips = devm_kzalloc(&pdev->dev,
176 sizeof(*d->aux_trips) * trip_cnt, GFP_KERNEL);
179 trip_mask = trip_cnt - 1;
180 d->handle = adev->handle;
181 d->aux_trip_nr = trip_cnt;
185 if (!int3402_thermal_get_temp(adev->handle, "_CRT", &d->crt_temp))
186 d->crt_trip_id = trip_cnt++;
188 if (!int3402_thermal_get_temp(adev->handle, "_HOT", &d->hot_temp))
189 d->hot_trip_id = trip_cnt++;
191 if (!int3402_thermal_get_temp(adev->handle, "_PSV", &d->psv_temp))
192 d->psv_trip_id = trip_cnt++;
193 for (i = 0; i < ACPI_ACTIVE_COOLING_MAX_NR; i++) {
194 char name[5] = { '_', 'A', 'C', '0' + i, '\0' };
195 if (int3402_thermal_get_temp(adev->handle, name,
196 &d->act_trips[i].temp))
198 d->act_trips[i].id = trip_cnt++;
199 d->act_trips[i].valid = true;
202 zone = thermal_zone_device_register(acpi_device_bid(adev), trip_cnt,
204 &int3402_thermal_zone_ops,
205 &int3402_thermal_params,
208 return PTR_ERR(zone);
209 platform_set_drvdata(pdev, zone);
214 static int int3402_thermal_remove(struct platform_device *pdev)
216 struct thermal_zone_device *zone = platform_get_drvdata(pdev);
218 thermal_zone_device_unregister(zone);
222 static const struct acpi_device_id int3402_thermal_match[] = {
227 MODULE_DEVICE_TABLE(acpi, int3402_thermal_match);
229 static struct platform_driver int3402_thermal_driver = {
230 .probe = int3402_thermal_probe,
231 .remove = int3402_thermal_remove,
233 .name = "int3402 thermal",
234 .acpi_match_table = int3402_thermal_match,
238 module_platform_driver(int3402_thermal_driver);
240 MODULE_DESCRIPTION("INT3402 Thermal driver");
241 MODULE_LICENSE("GPL");