1 // SPDX-License-Identifier: GPL-2.0-only
3 * ACPI INT3403 thermal driver
4 * Copyright (c) 2013, Intel Corporation.
7 #include <linux/kernel.h>
8 #include <linux/module.h>
9 #include <linux/init.h>
10 #include <linux/types.h>
11 #include <linux/acpi.h>
12 #include <linux/thermal.h>
13 #include <linux/platform_device.h>
14 #include "int340x_thermal_zone.h"
16 #define INT3403_TYPE_SENSOR 0x03
17 #define INT3403_TYPE_CHARGER 0x0B
18 #define INT3403_TYPE_BATTERY 0x0C
19 #define INT3403_PERF_CHANGED_EVENT 0x80
20 #define INT3403_PERF_TRIP_POINT_CHANGED 0x81
21 #define INT3403_THERMAL_EVENT 0x90
23 /* Preserved structure for future expandbility */
24 struct int3403_sensor {
25 struct int34x_thermal_zone *int340x_zone;
28 struct int3403_performance_state {
40 struct thermal_cooling_device *cdev;
41 unsigned long max_state;
45 struct platform_device *pdev;
46 struct acpi_device *adev;
47 unsigned long long type;
51 static void int3403_notify(acpi_handle handle,
52 u32 event, void *data)
54 struct int3403_priv *priv = data;
55 struct int3403_sensor *obj;
61 if (priv->type != INT3403_TYPE_SENSOR || !obj)
65 case INT3403_PERF_CHANGED_EVENT:
67 case INT3403_THERMAL_EVENT:
68 int340x_thermal_zone_device_update(obj->int340x_zone,
69 THERMAL_TRIP_VIOLATED);
71 case INT3403_PERF_TRIP_POINT_CHANGED:
72 int340x_thermal_read_trips(obj->int340x_zone);
73 int340x_thermal_zone_device_update(obj->int340x_zone,
74 THERMAL_TRIP_CHANGED);
77 dev_err(&priv->pdev->dev, "Unsupported event [0x%x]\n", event);
82 static int int3403_sensor_add(struct int3403_priv *priv)
85 struct int3403_sensor *obj;
87 obj = devm_kzalloc(&priv->pdev->dev, sizeof(*obj), GFP_KERNEL);
93 obj->int340x_zone = int340x_thermal_zone_add(priv->adev, NULL);
94 if (IS_ERR(obj->int340x_zone))
95 return PTR_ERR(obj->int340x_zone);
97 result = acpi_install_notify_handler(priv->adev->handle,
98 ACPI_DEVICE_NOTIFY, int3403_notify,
106 int340x_thermal_zone_remove(obj->int340x_zone);
110 static int int3403_sensor_remove(struct int3403_priv *priv)
112 struct int3403_sensor *obj = priv->priv;
114 acpi_remove_notify_handler(priv->adev->handle,
115 ACPI_DEVICE_NOTIFY, int3403_notify);
116 int340x_thermal_zone_remove(obj->int340x_zone);
121 /* INT3403 Cooling devices */
122 static int int3403_get_max_state(struct thermal_cooling_device *cdev,
123 unsigned long *state)
125 struct int3403_priv *priv = cdev->devdata;
126 struct int3403_cdev *obj = priv->priv;
128 *state = obj->max_state;
132 static int int3403_get_cur_state(struct thermal_cooling_device *cdev,
133 unsigned long *state)
135 struct int3403_priv *priv = cdev->devdata;
136 unsigned long long level;
139 status = acpi_evaluate_integer(priv->adev->handle, "PPPC", NULL, &level);
140 if (ACPI_SUCCESS(status)) {
148 int3403_set_cur_state(struct thermal_cooling_device *cdev, unsigned long state)
150 struct int3403_priv *priv = cdev->devdata;
153 status = acpi_execute_simple_method(priv->adev->handle, "SPPC", state);
154 if (ACPI_SUCCESS(status))
160 static const struct thermal_cooling_device_ops int3403_cooling_ops = {
161 .get_max_state = int3403_get_max_state,
162 .get_cur_state = int3403_get_cur_state,
163 .set_cur_state = int3403_set_cur_state,
166 static int int3403_cdev_add(struct int3403_priv *priv)
170 struct int3403_cdev *obj;
171 struct acpi_buffer buf = { ACPI_ALLOCATE_BUFFER, NULL };
172 union acpi_object *p;
174 obj = devm_kzalloc(&priv->pdev->dev, sizeof(*obj), GFP_KERNEL);
178 status = acpi_evaluate_object(priv->adev->handle, "PPSS", NULL, &buf);
179 if (ACPI_FAILURE(status))
183 if (!p || (p->type != ACPI_TYPE_PACKAGE)) {
184 printk(KERN_WARNING "Invalid PPSS data\n");
190 obj->max_state = p->package.count - 1;
192 thermal_cooling_device_register(acpi_device_bid(priv->adev),
193 priv, &int3403_cooling_ops);
194 if (IS_ERR(obj->cdev))
195 result = PTR_ERR(obj->cdev);
198 /* TODO: add ACPI notification support */
203 static int int3403_cdev_remove(struct int3403_priv *priv)
205 struct int3403_cdev *obj = priv->priv;
207 thermal_cooling_device_unregister(obj->cdev);
211 static int int3403_add(struct platform_device *pdev)
213 struct int3403_priv *priv;
215 unsigned long long tmp;
218 priv = devm_kzalloc(&pdev->dev, sizeof(struct int3403_priv),
224 priv->adev = ACPI_COMPANION(&(pdev->dev));
231 status = acpi_evaluate_integer(priv->adev->handle, "_TMP",
233 if (ACPI_FAILURE(status)) {
234 status = acpi_evaluate_integer(priv->adev->handle, "PTYP",
236 if (ACPI_FAILURE(status)) {
241 priv->type = INT3403_TYPE_SENSOR;
244 platform_set_drvdata(pdev, priv);
245 switch (priv->type) {
246 case INT3403_TYPE_SENSOR:
247 result = int3403_sensor_add(priv);
249 case INT3403_TYPE_CHARGER:
250 case INT3403_TYPE_BATTERY:
251 result = int3403_cdev_add(priv);
265 static int int3403_remove(struct platform_device *pdev)
267 struct int3403_priv *priv = platform_get_drvdata(pdev);
269 switch (priv->type) {
270 case INT3403_TYPE_SENSOR:
271 int3403_sensor_remove(priv);
273 case INT3403_TYPE_CHARGER:
274 case INT3403_TYPE_BATTERY:
275 int3403_cdev_remove(priv);
284 static const struct acpi_device_id int3403_device_ids[] = {
288 MODULE_DEVICE_TABLE(acpi, int3403_device_ids);
290 static struct platform_driver int3403_driver = {
291 .probe = int3403_add,
292 .remove = int3403_remove,
294 .name = "int3403 thermal",
295 .acpi_match_table = int3403_device_ids,
299 module_platform_driver(int3403_driver);
301 MODULE_AUTHOR("Srinivas Pandruvada <srinivas.pandruvada@linux.intel.com>");
302 MODULE_LICENSE("GPL v2");
303 MODULE_DESCRIPTION("ACPI INT3403 thermal driver");