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
;
29 struct thermal_cooling_device
*cdev
;
30 unsigned long max_state
;
34 struct platform_device
*pdev
;
35 struct acpi_device
*adev
;
36 unsigned long long type
;
40 static void int3403_notify(acpi_handle handle
,
41 u32 event
, void *data
)
43 struct int3403_priv
*priv
= data
;
44 struct int3403_sensor
*obj
;
50 if (priv
->type
!= INT3403_TYPE_SENSOR
|| !obj
)
54 case INT3403_PERF_CHANGED_EVENT
:
56 case INT3403_THERMAL_EVENT
:
57 int340x_thermal_zone_device_update(obj
->int340x_zone
,
58 THERMAL_TRIP_VIOLATED
);
60 case INT3403_PERF_TRIP_POINT_CHANGED
:
61 int340x_thermal_update_trips(obj
->int340x_zone
);
62 int340x_thermal_zone_device_update(obj
->int340x_zone
,
63 THERMAL_TRIP_CHANGED
);
66 dev_dbg(&priv
->pdev
->dev
, "Unsupported event [0x%x]\n", event
);
71 static int int3403_sensor_add(struct int3403_priv
*priv
)
74 struct int3403_sensor
*obj
;
76 obj
= devm_kzalloc(&priv
->pdev
->dev
, sizeof(*obj
), GFP_KERNEL
);
82 obj
->int340x_zone
= int340x_thermal_zone_add(priv
->adev
, NULL
);
83 if (IS_ERR(obj
->int340x_zone
))
84 return PTR_ERR(obj
->int340x_zone
);
86 result
= acpi_install_notify_handler(priv
->adev
->handle
,
87 ACPI_DEVICE_NOTIFY
, int3403_notify
,
95 int340x_thermal_zone_remove(obj
->int340x_zone
);
99 static int int3403_sensor_remove(struct int3403_priv
*priv
)
101 struct int3403_sensor
*obj
= priv
->priv
;
103 acpi_remove_notify_handler(priv
->adev
->handle
,
104 ACPI_DEVICE_NOTIFY
, int3403_notify
);
105 int340x_thermal_zone_remove(obj
->int340x_zone
);
110 /* INT3403 Cooling devices */
111 static int int3403_get_max_state(struct thermal_cooling_device
*cdev
,
112 unsigned long *state
)
114 struct int3403_priv
*priv
= cdev
->devdata
;
115 struct int3403_cdev
*obj
= priv
->priv
;
117 *state
= obj
->max_state
;
121 static int int3403_get_cur_state(struct thermal_cooling_device
*cdev
,
122 unsigned long *state
)
124 struct int3403_priv
*priv
= cdev
->devdata
;
125 unsigned long long level
;
128 status
= acpi_evaluate_integer(priv
->adev
->handle
, "PPPC", NULL
, &level
);
129 if (ACPI_SUCCESS(status
)) {
137 int3403_set_cur_state(struct thermal_cooling_device
*cdev
, unsigned long state
)
139 struct int3403_priv
*priv
= cdev
->devdata
;
142 status
= acpi_execute_simple_method(priv
->adev
->handle
, "SPPC", state
);
143 if (ACPI_SUCCESS(status
))
149 static const struct thermal_cooling_device_ops int3403_cooling_ops
= {
150 .get_max_state
= int3403_get_max_state
,
151 .get_cur_state
= int3403_get_cur_state
,
152 .set_cur_state
= int3403_set_cur_state
,
155 static int int3403_cdev_add(struct int3403_priv
*priv
)
159 struct int3403_cdev
*obj
;
160 struct acpi_buffer buf
= { ACPI_ALLOCATE_BUFFER
, NULL
};
161 union acpi_object
*p
;
163 obj
= devm_kzalloc(&priv
->pdev
->dev
, sizeof(*obj
), GFP_KERNEL
);
167 status
= acpi_evaluate_object(priv
->adev
->handle
, "PPSS", NULL
, &buf
);
168 if (ACPI_FAILURE(status
))
172 if (!p
|| (p
->type
!= ACPI_TYPE_PACKAGE
)) {
173 pr_warn("Invalid PPSS data\n");
179 obj
->max_state
= p
->package
.count
- 1;
181 thermal_cooling_device_register(acpi_device_bid(priv
->adev
),
182 priv
, &int3403_cooling_ops
);
183 if (IS_ERR(obj
->cdev
))
184 result
= PTR_ERR(obj
->cdev
);
187 /* TODO: add ACPI notification support */
192 static int int3403_cdev_remove(struct int3403_priv
*priv
)
194 struct int3403_cdev
*obj
= priv
->priv
;
196 thermal_cooling_device_unregister(obj
->cdev
);
200 static int int3403_add(struct platform_device
*pdev
)
202 struct int3403_priv
*priv
;
204 unsigned long long tmp
;
207 priv
= devm_kzalloc(&pdev
->dev
, sizeof(struct int3403_priv
),
213 priv
->adev
= ACPI_COMPANION(&(pdev
->dev
));
220 status
= acpi_evaluate_integer(priv
->adev
->handle
, "_TMP",
222 if (ACPI_FAILURE(status
)) {
223 status
= acpi_evaluate_integer(priv
->adev
->handle
, "PTYP",
225 if (ACPI_FAILURE(status
)) {
230 priv
->type
= INT3403_TYPE_SENSOR
;
233 platform_set_drvdata(pdev
, priv
);
234 switch (priv
->type
) {
235 case INT3403_TYPE_SENSOR
:
236 result
= int3403_sensor_add(priv
);
238 case INT3403_TYPE_CHARGER
:
239 case INT3403_TYPE_BATTERY
:
240 result
= int3403_cdev_add(priv
);
254 static void int3403_remove(struct platform_device
*pdev
)
256 struct int3403_priv
*priv
= platform_get_drvdata(pdev
);
258 switch (priv
->type
) {
259 case INT3403_TYPE_SENSOR
:
260 int3403_sensor_remove(priv
);
262 case INT3403_TYPE_CHARGER
:
263 case INT3403_TYPE_BATTERY
:
264 int3403_cdev_remove(priv
);
271 static const struct acpi_device_id int3403_device_ids
[] = {
280 MODULE_DEVICE_TABLE(acpi
, int3403_device_ids
);
282 static struct platform_driver int3403_driver
= {
283 .probe
= int3403_add
,
284 .remove
= int3403_remove
,
286 .name
= "int3403 thermal",
287 .acpi_match_table
= int3403_device_ids
,
291 module_platform_driver(int3403_driver
);
293 MODULE_AUTHOR("Srinivas Pandruvada <srinivas.pandruvada@linux.intel.com>");
294 MODULE_LICENSE("GPL v2");
295 MODULE_DESCRIPTION("ACPI INT3403 thermal driver");