2 * ACPI INT3403 thermal driver
3 * Copyright (c) 2013, 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
15 #include <linux/kernel.h>
16 #include <linux/module.h>
17 #include <linux/init.h>
18 #include <linux/types.h>
19 #include <linux/acpi.h>
20 #include <linux/thermal.h>
21 #include <linux/platform_device.h>
23 #define INT3403_TYPE_SENSOR 0x03
24 #define INT3403_TYPE_CHARGER 0x0B
25 #define INT3403_TYPE_BATTERY 0x0C
26 #define INT3403_PERF_CHANGED_EVENT 0x80
27 #define INT3403_THERMAL_EVENT 0x90
29 #define DECI_KELVIN_TO_MILLI_CELSIUS(t, off) (((t) - (off)) * 100)
30 #define KELVIN_OFFSET 2732
31 #define MILLI_CELSIUS_TO_DECI_KELVIN(t, off) (((t) / 100) + (off))
33 struct int3403_sensor {
34 struct thermal_zone_device *tzone;
35 unsigned long *thresholds;
36 unsigned long crit_temp;
38 unsigned long psv_temp;
43 struct int3403_performance_state {
55 struct thermal_cooling_device *cdev;
56 unsigned long max_state;
60 struct platform_device *pdev;
61 struct acpi_device *adev;
62 unsigned long long type;
66 static int sys_get_curr_temp(struct thermal_zone_device *tzone,
69 struct int3403_priv *priv = tzone->devdata;
70 struct acpi_device *device = priv->adev;
71 unsigned long long tmp;
74 status = acpi_evaluate_integer(device->handle, "_TMP", NULL, &tmp);
75 if (ACPI_FAILURE(status))
78 *temp = DECI_KELVIN_TO_MILLI_CELSIUS(tmp, KELVIN_OFFSET);
83 static int sys_get_trip_hyst(struct thermal_zone_device *tzone,
84 int trip, unsigned long *temp)
86 struct int3403_priv *priv = tzone->devdata;
87 struct acpi_device *device = priv->adev;
88 unsigned long long hyst;
91 status = acpi_evaluate_integer(device->handle, "GTSH", NULL, &hyst);
92 if (ACPI_FAILURE(status))
95 *temp = DECI_KELVIN_TO_MILLI_CELSIUS(hyst, KELVIN_OFFSET);
100 static int sys_get_trip_temp(struct thermal_zone_device *tzone,
101 int trip, unsigned long *temp)
103 struct int3403_priv *priv = tzone->devdata;
104 struct int3403_sensor *obj = priv->priv;
106 if (priv->type != INT3403_TYPE_SENSOR || !obj)
109 if (trip == obj->crit_trip_id)
110 *temp = obj->crit_temp;
111 else if (trip == obj->psv_trip_id)
112 *temp = obj->psv_temp;
115 * get_trip_temp is a mandatory callback but
116 * PATx method doesn't return any value, so return
117 * cached value, which was last set from user space
119 *temp = obj->thresholds[trip];
125 static int sys_get_trip_type(struct thermal_zone_device *thermal,
126 int trip, enum thermal_trip_type *type)
128 struct int3403_priv *priv = thermal->devdata;
129 struct int3403_sensor *obj = priv->priv;
131 /* Mandatory callback, may not mean much here */
132 if (trip == obj->crit_trip_id)
133 *type = THERMAL_TRIP_CRITICAL;
135 *type = THERMAL_TRIP_PASSIVE;
140 int sys_set_trip_temp(struct thermal_zone_device *tzone, int trip,
143 struct int3403_priv *priv = tzone->devdata;
144 struct acpi_device *device = priv->adev;
145 struct int3403_sensor *obj = priv->priv;
150 snprintf(name, sizeof(name), "PAT%d", trip);
151 if (acpi_has_method(device->handle, name)) {
152 status = acpi_execute_simple_method(device->handle, name,
153 MILLI_CELSIUS_TO_DECI_KELVIN(temp,
155 if (ACPI_FAILURE(status))
158 obj->thresholds[trip] = temp;
161 dev_err(&device->dev, "sys_set_trip_temp: method not found\n");
167 static struct thermal_zone_device_ops tzone_ops = {
168 .get_temp = sys_get_curr_temp,
169 .get_trip_temp = sys_get_trip_temp,
170 .get_trip_type = sys_get_trip_type,
171 .set_trip_temp = sys_set_trip_temp,
172 .get_trip_hyst = sys_get_trip_hyst,
175 static struct thermal_zone_params int3403_thermal_params = {
176 .governor_name = "user_space",
180 static void int3403_notify(acpi_handle handle,
181 u32 event, void *data)
183 struct int3403_priv *priv = data;
184 struct int3403_sensor *obj;
190 if (priv->type != INT3403_TYPE_SENSOR || !obj)
194 case INT3403_PERF_CHANGED_EVENT:
196 case INT3403_THERMAL_EVENT:
197 thermal_zone_device_update(obj->tzone);
200 dev_err(&priv->pdev->dev, "Unsupported event [0x%x]\n", event);
205 static int sys_get_trip_crt(struct acpi_device *device, unsigned long *temp)
207 unsigned long long crt;
210 status = acpi_evaluate_integer(device->handle, "_CRT", NULL, &crt);
211 if (ACPI_FAILURE(status))
214 *temp = DECI_KELVIN_TO_MILLI_CELSIUS(crt, KELVIN_OFFSET);
219 static int sys_get_trip_psv(struct acpi_device *device, unsigned long *temp)
221 unsigned long long psv;
224 status = acpi_evaluate_integer(device->handle, "_PSV", NULL, &psv);
225 if (ACPI_FAILURE(status))
228 *temp = DECI_KELVIN_TO_MILLI_CELSIUS(psv, KELVIN_OFFSET);
233 static int int3403_sensor_add(struct int3403_priv *priv)
237 struct int3403_sensor *obj;
238 unsigned long long trip_cnt;
241 obj = devm_kzalloc(&priv->pdev->dev, sizeof(*obj), GFP_KERNEL);
247 status = acpi_evaluate_integer(priv->adev->handle, "PATC", NULL,
249 if (ACPI_FAILURE(status))
253 /* We have to cache, thresholds can't be readback */
254 obj->thresholds = devm_kzalloc(&priv->pdev->dev,
255 sizeof(*obj->thresholds) * trip_cnt,
257 if (!obj->thresholds) {
261 trip_mask = BIT(trip_cnt) - 1;
264 obj->psv_trip_id = -1;
265 if (!sys_get_trip_psv(priv->adev, &obj->psv_temp))
266 obj->psv_trip_id = trip_cnt++;
268 obj->crit_trip_id = -1;
269 if (!sys_get_trip_crt(priv->adev, &obj->crit_temp))
270 obj->crit_trip_id = trip_cnt++;
272 obj->tzone = thermal_zone_device_register(acpi_device_bid(priv->adev),
273 trip_cnt, trip_mask, priv, &tzone_ops,
274 &int3403_thermal_params, 0, 0);
275 if (IS_ERR(obj->tzone)) {
276 result = PTR_ERR(obj->tzone);
281 result = acpi_install_notify_handler(priv->adev->handle,
282 ACPI_DEVICE_NOTIFY, int3403_notify,
291 thermal_zone_device_unregister(obj->tzone);
295 static int int3403_sensor_remove(struct int3403_priv *priv)
297 struct int3403_sensor *obj = priv->priv;
299 thermal_zone_device_unregister(obj->tzone);
303 /* INT3403 Cooling devices */
304 static int int3403_get_max_state(struct thermal_cooling_device *cdev,
305 unsigned long *state)
307 struct int3403_priv *priv = cdev->devdata;
308 struct int3403_cdev *obj = priv->priv;
310 *state = obj->max_state;
314 static int int3403_get_cur_state(struct thermal_cooling_device *cdev,
315 unsigned long *state)
317 struct int3403_priv *priv = cdev->devdata;
318 unsigned long long level;
321 status = acpi_evaluate_integer(priv->adev->handle, "PPPC", NULL, &level);
322 if (ACPI_SUCCESS(status)) {
330 int3403_set_cur_state(struct thermal_cooling_device *cdev, unsigned long state)
332 struct int3403_priv *priv = cdev->devdata;
335 status = acpi_execute_simple_method(priv->adev->handle, "SPPC", state);
336 if (ACPI_SUCCESS(status))
342 static const struct thermal_cooling_device_ops int3403_cooling_ops = {
343 .get_max_state = int3403_get_max_state,
344 .get_cur_state = int3403_get_cur_state,
345 .set_cur_state = int3403_set_cur_state,
348 static int int3403_cdev_add(struct int3403_priv *priv)
352 struct int3403_cdev *obj;
353 struct acpi_buffer buf = { ACPI_ALLOCATE_BUFFER, NULL };
354 union acpi_object *p;
356 obj = devm_kzalloc(&priv->pdev->dev, sizeof(*obj), GFP_KERNEL);
360 status = acpi_evaluate_object(priv->adev->handle, "PPSS", NULL, &buf);
361 if (ACPI_FAILURE(status))
365 if (!p || (p->type != ACPI_TYPE_PACKAGE)) {
366 printk(KERN_WARNING "Invalid PPSS data\n");
370 obj->max_state = p->package.count - 1;
372 thermal_cooling_device_register(acpi_device_bid(priv->adev),
373 priv, &int3403_cooling_ops);
374 if (IS_ERR(obj->cdev))
375 result = PTR_ERR(obj->cdev);
379 /* TODO: add ACPI notification support */
384 static int int3403_cdev_remove(struct int3403_priv *priv)
386 struct int3403_cdev *obj = priv->priv;
388 thermal_cooling_device_unregister(obj->cdev);
392 static int int3403_add(struct platform_device *pdev)
394 struct int3403_priv *priv;
398 priv = devm_kzalloc(&pdev->dev, sizeof(struct int3403_priv),
404 priv->adev = ACPI_COMPANION(&(pdev->dev));
410 status = acpi_evaluate_integer(priv->adev->handle, "PTYP",
412 if (ACPI_FAILURE(status)) {
417 platform_set_drvdata(pdev, priv);
418 switch (priv->type) {
419 case INT3403_TYPE_SENSOR:
420 result = int3403_sensor_add(priv);
422 case INT3403_TYPE_CHARGER:
423 case INT3403_TYPE_BATTERY:
424 result = int3403_cdev_add(priv);
438 static int int3403_remove(struct platform_device *pdev)
440 struct int3403_priv *priv = platform_get_drvdata(pdev);
442 switch (priv->type) {
443 case INT3403_TYPE_SENSOR:
444 int3403_sensor_remove(priv);
446 case INT3403_TYPE_CHARGER:
447 case INT3403_TYPE_BATTERY:
448 int3403_cdev_remove(priv);
457 static const struct acpi_device_id int3403_device_ids[] = {
461 MODULE_DEVICE_TABLE(acpi, int3403_device_ids);
463 static struct platform_driver int3403_driver = {
464 .probe = int3403_add,
465 .remove = int3403_remove,
467 .name = "int3403 thermal",
468 .owner = THIS_MODULE,
469 .acpi_match_table = int3403_device_ids,
473 module_platform_driver(int3403_driver);
475 MODULE_AUTHOR("Srinivas Pandruvada <srinivas.pandruvada@linux.intel.com>");
476 MODULE_LICENSE("GPL v2");
477 MODULE_DESCRIPTION("ACPI INT3403 thermal driver");