xref: /OK3568_Linux_fs/kernel/drivers/thermal/intel/int340x_thermal/int3406_thermal.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0-only
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun  * INT3406 thermal driver for display participant device
4*4882a593Smuzhiyun  *
5*4882a593Smuzhiyun  * Copyright (C) 2016, Intel Corporation
6*4882a593Smuzhiyun  * Authors: Aaron Lu <aaron.lu@intel.com>
7*4882a593Smuzhiyun  */
8*4882a593Smuzhiyun 
9*4882a593Smuzhiyun #include <linux/module.h>
10*4882a593Smuzhiyun #include <linux/platform_device.h>
11*4882a593Smuzhiyun #include <linux/acpi.h>
12*4882a593Smuzhiyun #include <linux/backlight.h>
13*4882a593Smuzhiyun #include <linux/thermal.h>
14*4882a593Smuzhiyun #include <acpi/video.h>
15*4882a593Smuzhiyun 
16*4882a593Smuzhiyun #define INT3406_BRIGHTNESS_LIMITS_CHANGED	0x80
17*4882a593Smuzhiyun 
18*4882a593Smuzhiyun struct int3406_thermal_data {
19*4882a593Smuzhiyun 	int upper_limit;
20*4882a593Smuzhiyun 	int lower_limit;
21*4882a593Smuzhiyun 	acpi_handle handle;
22*4882a593Smuzhiyun 	struct acpi_video_device_brightness *br;
23*4882a593Smuzhiyun 	struct backlight_device *raw_bd;
24*4882a593Smuzhiyun 	struct thermal_cooling_device *cooling_dev;
25*4882a593Smuzhiyun };
26*4882a593Smuzhiyun 
27*4882a593Smuzhiyun /*
28*4882a593Smuzhiyun  * According to the ACPI spec,
29*4882a593Smuzhiyun  * "Each brightness level is represented by a number between 0 and 100,
30*4882a593Smuzhiyun  * and can be thought of as a percentage. For example, 50 can be 50%
31*4882a593Smuzhiyun  * power consumption or 50% brightness, as defined by the OEM."
32*4882a593Smuzhiyun  *
33*4882a593Smuzhiyun  * As int3406 device uses this value to communicate with the native
34*4882a593Smuzhiyun  * graphics driver, we make the assumption that it represents
35*4882a593Smuzhiyun  * the percentage of brightness only
36*4882a593Smuzhiyun  */
37*4882a593Smuzhiyun #define ACPI_TO_RAW(v, d) (d->raw_bd->props.max_brightness * v / 100)
38*4882a593Smuzhiyun #define RAW_TO_ACPI(v, d) (v * 100 / d->raw_bd->props.max_brightness)
39*4882a593Smuzhiyun 
40*4882a593Smuzhiyun static int
int3406_thermal_get_max_state(struct thermal_cooling_device * cooling_dev,unsigned long * state)41*4882a593Smuzhiyun int3406_thermal_get_max_state(struct thermal_cooling_device *cooling_dev,
42*4882a593Smuzhiyun 			      unsigned long *state)
43*4882a593Smuzhiyun {
44*4882a593Smuzhiyun 	struct int3406_thermal_data *d = cooling_dev->devdata;
45*4882a593Smuzhiyun 
46*4882a593Smuzhiyun 	*state = d->upper_limit - d->lower_limit;
47*4882a593Smuzhiyun 	return 0;
48*4882a593Smuzhiyun }
49*4882a593Smuzhiyun 
50*4882a593Smuzhiyun static int
int3406_thermal_set_cur_state(struct thermal_cooling_device * cooling_dev,unsigned long state)51*4882a593Smuzhiyun int3406_thermal_set_cur_state(struct thermal_cooling_device *cooling_dev,
52*4882a593Smuzhiyun 			      unsigned long state)
53*4882a593Smuzhiyun {
54*4882a593Smuzhiyun 	struct int3406_thermal_data *d = cooling_dev->devdata;
55*4882a593Smuzhiyun 	int acpi_level, raw_level;
56*4882a593Smuzhiyun 
57*4882a593Smuzhiyun 	if (state > d->upper_limit - d->lower_limit)
58*4882a593Smuzhiyun 		return -EINVAL;
59*4882a593Smuzhiyun 
60*4882a593Smuzhiyun 	acpi_level = d->br->levels[d->upper_limit - state];
61*4882a593Smuzhiyun 
62*4882a593Smuzhiyun 	raw_level = ACPI_TO_RAW(acpi_level, d);
63*4882a593Smuzhiyun 
64*4882a593Smuzhiyun 	return backlight_device_set_brightness(d->raw_bd, raw_level);
65*4882a593Smuzhiyun }
66*4882a593Smuzhiyun 
67*4882a593Smuzhiyun static int
int3406_thermal_get_cur_state(struct thermal_cooling_device * cooling_dev,unsigned long * state)68*4882a593Smuzhiyun int3406_thermal_get_cur_state(struct thermal_cooling_device *cooling_dev,
69*4882a593Smuzhiyun 			      unsigned long *state)
70*4882a593Smuzhiyun {
71*4882a593Smuzhiyun 	struct int3406_thermal_data *d = cooling_dev->devdata;
72*4882a593Smuzhiyun 	int acpi_level;
73*4882a593Smuzhiyun 	int index;
74*4882a593Smuzhiyun 
75*4882a593Smuzhiyun 	acpi_level = RAW_TO_ACPI(d->raw_bd->props.brightness, d);
76*4882a593Smuzhiyun 
77*4882a593Smuzhiyun 	/*
78*4882a593Smuzhiyun 	 * There is no 1:1 mapping between the firmware interface level
79*4882a593Smuzhiyun 	 * with the raw interface level, we will have to find one that is
80*4882a593Smuzhiyun 	 * right above it.
81*4882a593Smuzhiyun 	 */
82*4882a593Smuzhiyun 	for (index = d->lower_limit; index < d->upper_limit; index++) {
83*4882a593Smuzhiyun 		if (acpi_level <= d->br->levels[index])
84*4882a593Smuzhiyun 			break;
85*4882a593Smuzhiyun 	}
86*4882a593Smuzhiyun 
87*4882a593Smuzhiyun 	*state = d->upper_limit - index;
88*4882a593Smuzhiyun 	return 0;
89*4882a593Smuzhiyun }
90*4882a593Smuzhiyun 
91*4882a593Smuzhiyun static const struct thermal_cooling_device_ops video_cooling_ops = {
92*4882a593Smuzhiyun 	.get_max_state = int3406_thermal_get_max_state,
93*4882a593Smuzhiyun 	.get_cur_state = int3406_thermal_get_cur_state,
94*4882a593Smuzhiyun 	.set_cur_state = int3406_thermal_set_cur_state,
95*4882a593Smuzhiyun };
96*4882a593Smuzhiyun 
int3406_thermal_get_index(int * array,int nr,int value)97*4882a593Smuzhiyun static int int3406_thermal_get_index(int *array, int nr, int value)
98*4882a593Smuzhiyun {
99*4882a593Smuzhiyun 	int i;
100*4882a593Smuzhiyun 
101*4882a593Smuzhiyun 	for (i = 2; i < nr; i++) {
102*4882a593Smuzhiyun 		if (array[i] == value)
103*4882a593Smuzhiyun 			break;
104*4882a593Smuzhiyun 	}
105*4882a593Smuzhiyun 	return i == nr ? -ENOENT : i;
106*4882a593Smuzhiyun }
107*4882a593Smuzhiyun 
int3406_thermal_get_limit(struct int3406_thermal_data * d)108*4882a593Smuzhiyun static void int3406_thermal_get_limit(struct int3406_thermal_data *d)
109*4882a593Smuzhiyun {
110*4882a593Smuzhiyun 	acpi_status status;
111*4882a593Smuzhiyun 	unsigned long long lower_limit, upper_limit;
112*4882a593Smuzhiyun 
113*4882a593Smuzhiyun 	status = acpi_evaluate_integer(d->handle, "DDDL", NULL, &lower_limit);
114*4882a593Smuzhiyun 	if (ACPI_SUCCESS(status))
115*4882a593Smuzhiyun 		d->lower_limit = int3406_thermal_get_index(d->br->levels,
116*4882a593Smuzhiyun 					d->br->count, lower_limit);
117*4882a593Smuzhiyun 
118*4882a593Smuzhiyun 	status = acpi_evaluate_integer(d->handle, "DDPC", NULL, &upper_limit);
119*4882a593Smuzhiyun 	if (ACPI_SUCCESS(status))
120*4882a593Smuzhiyun 		d->upper_limit = int3406_thermal_get_index(d->br->levels,
121*4882a593Smuzhiyun 					d->br->count, upper_limit);
122*4882a593Smuzhiyun 
123*4882a593Smuzhiyun 	/* lower_limit and upper_limit should be always set */
124*4882a593Smuzhiyun 	d->lower_limit = d->lower_limit > 0 ? d->lower_limit : 2;
125*4882a593Smuzhiyun 	d->upper_limit = d->upper_limit > 0 ? d->upper_limit : d->br->count - 1;
126*4882a593Smuzhiyun }
127*4882a593Smuzhiyun 
int3406_notify(acpi_handle handle,u32 event,void * data)128*4882a593Smuzhiyun static void int3406_notify(acpi_handle handle, u32 event, void *data)
129*4882a593Smuzhiyun {
130*4882a593Smuzhiyun 	if (event == INT3406_BRIGHTNESS_LIMITS_CHANGED)
131*4882a593Smuzhiyun 		int3406_thermal_get_limit(data);
132*4882a593Smuzhiyun }
133*4882a593Smuzhiyun 
int3406_thermal_probe(struct platform_device * pdev)134*4882a593Smuzhiyun static int int3406_thermal_probe(struct platform_device *pdev)
135*4882a593Smuzhiyun {
136*4882a593Smuzhiyun 	struct acpi_device *adev = ACPI_COMPANION(&pdev->dev);
137*4882a593Smuzhiyun 	struct int3406_thermal_data *d;
138*4882a593Smuzhiyun 	struct backlight_device *bd;
139*4882a593Smuzhiyun 	int ret;
140*4882a593Smuzhiyun 
141*4882a593Smuzhiyun 	if (!ACPI_HANDLE(&pdev->dev))
142*4882a593Smuzhiyun 		return -ENODEV;
143*4882a593Smuzhiyun 
144*4882a593Smuzhiyun 	d = devm_kzalloc(&pdev->dev, sizeof(*d), GFP_KERNEL);
145*4882a593Smuzhiyun 	if (!d)
146*4882a593Smuzhiyun 		return -ENOMEM;
147*4882a593Smuzhiyun 	d->handle = ACPI_HANDLE(&pdev->dev);
148*4882a593Smuzhiyun 
149*4882a593Smuzhiyun 	bd = backlight_device_get_by_type(BACKLIGHT_RAW);
150*4882a593Smuzhiyun 	if (!bd)
151*4882a593Smuzhiyun 		return -ENODEV;
152*4882a593Smuzhiyun 	d->raw_bd = bd;
153*4882a593Smuzhiyun 
154*4882a593Smuzhiyun 	ret = acpi_video_get_levels(ACPI_COMPANION(&pdev->dev), &d->br, NULL);
155*4882a593Smuzhiyun 	if (ret)
156*4882a593Smuzhiyun 		return ret;
157*4882a593Smuzhiyun 
158*4882a593Smuzhiyun 	int3406_thermal_get_limit(d);
159*4882a593Smuzhiyun 
160*4882a593Smuzhiyun 	d->cooling_dev = thermal_cooling_device_register(acpi_device_bid(adev),
161*4882a593Smuzhiyun 							 d, &video_cooling_ops);
162*4882a593Smuzhiyun 	if (IS_ERR(d->cooling_dev))
163*4882a593Smuzhiyun 		goto err;
164*4882a593Smuzhiyun 
165*4882a593Smuzhiyun 	ret = acpi_install_notify_handler(adev->handle, ACPI_DEVICE_NOTIFY,
166*4882a593Smuzhiyun 					  int3406_notify, d);
167*4882a593Smuzhiyun 	if (ret)
168*4882a593Smuzhiyun 		goto err_cdev;
169*4882a593Smuzhiyun 
170*4882a593Smuzhiyun 	platform_set_drvdata(pdev, d);
171*4882a593Smuzhiyun 
172*4882a593Smuzhiyun 	return 0;
173*4882a593Smuzhiyun 
174*4882a593Smuzhiyun err_cdev:
175*4882a593Smuzhiyun 	thermal_cooling_device_unregister(d->cooling_dev);
176*4882a593Smuzhiyun err:
177*4882a593Smuzhiyun 	kfree(d->br);
178*4882a593Smuzhiyun 	return -ENODEV;
179*4882a593Smuzhiyun }
180*4882a593Smuzhiyun 
int3406_thermal_remove(struct platform_device * pdev)181*4882a593Smuzhiyun static int int3406_thermal_remove(struct platform_device *pdev)
182*4882a593Smuzhiyun {
183*4882a593Smuzhiyun 	struct int3406_thermal_data *d = platform_get_drvdata(pdev);
184*4882a593Smuzhiyun 
185*4882a593Smuzhiyun 	thermal_cooling_device_unregister(d->cooling_dev);
186*4882a593Smuzhiyun 	kfree(d->br);
187*4882a593Smuzhiyun 	return 0;
188*4882a593Smuzhiyun }
189*4882a593Smuzhiyun 
190*4882a593Smuzhiyun static const struct acpi_device_id int3406_thermal_match[] = {
191*4882a593Smuzhiyun 	{"INT3406", 0},
192*4882a593Smuzhiyun 	{}
193*4882a593Smuzhiyun };
194*4882a593Smuzhiyun 
195*4882a593Smuzhiyun MODULE_DEVICE_TABLE(acpi, int3406_thermal_match);
196*4882a593Smuzhiyun 
197*4882a593Smuzhiyun static struct platform_driver int3406_thermal_driver = {
198*4882a593Smuzhiyun 	.probe = int3406_thermal_probe,
199*4882a593Smuzhiyun 	.remove = int3406_thermal_remove,
200*4882a593Smuzhiyun 	.driver = {
201*4882a593Smuzhiyun 		   .name = "int3406 thermal",
202*4882a593Smuzhiyun 		   .acpi_match_table = int3406_thermal_match,
203*4882a593Smuzhiyun 		   },
204*4882a593Smuzhiyun };
205*4882a593Smuzhiyun 
206*4882a593Smuzhiyun module_platform_driver(int3406_thermal_driver);
207*4882a593Smuzhiyun 
208*4882a593Smuzhiyun MODULE_DESCRIPTION("INT3406 Thermal driver");
209*4882a593Smuzhiyun MODULE_LICENSE("GPL v2");
210