2 * INT3406 thermal driver for display participant device
4 * Copyright (C) 2016, 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/backlight.h>
17 #include <linux/thermal.h>
18 #include <acpi/video.h>
20 #define INT3406_BRIGHTNESS_LIMITS_CHANGED 0x80
22 struct int3406_thermal_data
{
24 int upper_limit_index
;
26 int lower_limit_index
;
28 struct acpi_video_device_brightness
*br
;
29 struct backlight_device
*raw_bd
;
30 struct thermal_cooling_device
*cooling_dev
;
33 static int int3406_thermal_to_raw(int level
, struct int3406_thermal_data
*d
)
35 int max_level
= d
->br
->levels
[d
->br
->count
- 1];
36 int raw_max
= d
->raw_bd
->props
.max_brightness
;
38 return level
* raw_max
/ max_level
;
41 static int int3406_thermal_to_acpi(int level
, struct int3406_thermal_data
*d
)
43 int raw_max
= d
->raw_bd
->props
.max_brightness
;
44 int max_level
= d
->br
->levels
[d
->br
->count
- 1];
46 return level
* max_level
/ raw_max
;
50 int3406_thermal_get_max_state(struct thermal_cooling_device
*cooling_dev
,
53 struct int3406_thermal_data
*d
= cooling_dev
->devdata
;
54 int index
= d
->lower_limit_index
? d
->lower_limit_index
: 2;
56 *state
= d
->br
->count
- 1 - index
;
61 int3406_thermal_set_cur_state(struct thermal_cooling_device
*cooling_dev
,
64 struct int3406_thermal_data
*d
= cooling_dev
->devdata
;
67 if (state
> d
->br
->count
- 3)
70 state
= d
->br
->count
- 1 - state
;
71 level
= d
->br
->levels
[state
];
73 if ((d
->upper_limit
&& level
> d
->upper_limit
) ||
74 (d
->lower_limit
&& level
< d
->lower_limit
))
77 raw_level
= int3406_thermal_to_raw(level
, d
);
78 return backlight_device_set_brightness(d
->raw_bd
, raw_level
);
82 int3406_thermal_get_cur_state(struct thermal_cooling_device
*cooling_dev
,
85 struct int3406_thermal_data
*d
= cooling_dev
->devdata
;
86 int raw_level
, level
, i
;
87 int *levels
= d
->br
->levels
;
89 raw_level
= d
->raw_bd
->props
.brightness
;
90 level
= int3406_thermal_to_acpi(raw_level
, d
);
93 * There is no 1:1 mapping between the firmware interface level with the
94 * raw interface level, we will have to find one that is close enough.
96 for (i
= 2; i
< d
->br
->count
; i
++) {
97 if (level
< levels
[i
]) {
100 if ((level
- levels
[i
- 1]) < (levels
[i
] - level
))
106 *state
= d
->br
->count
- 1 - i
;
110 static const struct thermal_cooling_device_ops video_cooling_ops
= {
111 .get_max_state
= int3406_thermal_get_max_state
,
112 .get_cur_state
= int3406_thermal_get_cur_state
,
113 .set_cur_state
= int3406_thermal_set_cur_state
,
116 static int int3406_thermal_get_index(int *array
, int nr
, int value
)
120 for (i
= 0; i
< nr
; i
++) {
121 if (array
[i
] == value
)
124 return i
== nr
? -ENOENT
: i
;
127 static void int3406_thermal_get_limit(struct int3406_thermal_data
*d
)
130 unsigned long long lower_limit
, upper_limit
;
133 status
= acpi_evaluate_integer(d
->handle
, "DDDL", NULL
, &lower_limit
);
134 if (ACPI_SUCCESS(status
)) {
135 index
= int3406_thermal_get_index(d
->br
->levels
, d
->br
->count
,
138 d
->lower_limit
= (int)lower_limit
;
139 d
->lower_limit_index
= index
;
143 status
= acpi_evaluate_integer(d
->handle
, "DDPC", NULL
, &upper_limit
);
144 if (ACPI_SUCCESS(status
)) {
145 index
= int3406_thermal_get_index(d
->br
->levels
, d
->br
->count
,
148 d
->upper_limit
= (int)upper_limit
;
149 d
->upper_limit_index
= index
;
154 static void int3406_notify(acpi_handle handle
, u32 event
, void *data
)
156 if (event
== INT3406_BRIGHTNESS_LIMITS_CHANGED
)
157 int3406_thermal_get_limit(data
);
160 static int int3406_thermal_probe(struct platform_device
*pdev
)
162 struct acpi_device
*adev
= ACPI_COMPANION(&pdev
->dev
);
163 struct int3406_thermal_data
*d
;
164 struct backlight_device
*bd
;
167 if (!ACPI_HANDLE(&pdev
->dev
))
170 d
= devm_kzalloc(&pdev
->dev
, sizeof(*d
), GFP_KERNEL
);
173 d
->handle
= ACPI_HANDLE(&pdev
->dev
);
175 bd
= backlight_device_get_by_type(BACKLIGHT_RAW
);
180 ret
= acpi_video_get_levels(ACPI_COMPANION(&pdev
->dev
), &d
->br
, NULL
);
184 int3406_thermal_get_limit(d
);
186 d
->cooling_dev
= thermal_cooling_device_register(acpi_device_bid(adev
),
187 d
, &video_cooling_ops
);
188 if (IS_ERR(d
->cooling_dev
))
191 ret
= acpi_install_notify_handler(adev
->handle
, ACPI_DEVICE_NOTIFY
,
196 platform_set_drvdata(pdev
, d
);
201 thermal_cooling_device_unregister(d
->cooling_dev
);
207 static int int3406_thermal_remove(struct platform_device
*pdev
)
209 struct int3406_thermal_data
*d
= platform_get_drvdata(pdev
);
211 thermal_cooling_device_unregister(d
->cooling_dev
);
216 static const struct acpi_device_id int3406_thermal_match
[] = {
221 MODULE_DEVICE_TABLE(acpi
, int3406_thermal_match
);
223 static struct platform_driver int3406_thermal_driver
= {
224 .probe
= int3406_thermal_probe
,
225 .remove
= int3406_thermal_remove
,
227 .name
= "int3406 thermal",
228 .acpi_match_table
= int3406_thermal_match
,
232 module_platform_driver(int3406_thermal_driver
);
234 MODULE_DESCRIPTION("INT3406 Thermal driver");
235 MODULE_LICENSE("GPL v2");