]> git.ipfire.org Git - people/arne_f/kernel.git/blame - drivers/thermal/int340x_thermal/int3400_thermal.c
thermal/int340x_thermal: Add additional UUIDs
[people/arne_f/kernel.git] / drivers / thermal / int340x_thermal / int3400_thermal.c
CommitLineData
816cab93
ZR
1/*
2 * INT3400 thermal driver
3 *
4 * Copyright (C) 2014, Intel Corporation
5 * Authors: Zhang Rui <rui.zhang@intel.com>
6 *
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.
10 *
11 */
12
13#include <linux/module.h>
14#include <linux/platform_device.h>
15#include <linux/acpi.h>
0ab15365 16#include <linux/thermal.h>
6306e68a 17#include "acpi_thermal_rel.h"
816cab93 18
c5738ddd
ZR
19enum int3400_thermal_uuid {
20 INT3400_THERMAL_PASSIVE_1,
c5738ddd
ZR
21 INT3400_THERMAL_ACTIVE,
22 INT3400_THERMAL_CRITICAL,
14216f18
MG
23 INT3400_THERMAL_ADAPTIVE_PERFORMANCE,
24 INT3400_THERMAL_EMERGENCY_CALL_MODE,
25 INT3400_THERMAL_PASSIVE_2,
26 INT3400_THERMAL_POWER_BOSS,
27 INT3400_THERMAL_VIRTUAL_SENSOR,
28 INT3400_THERMAL_COOLING_MODE,
29 INT3400_THERMAL_HARDWARE_DUTY_CYCLING,
c5738ddd
ZR
30 INT3400_THERMAL_MAXIMUM_UUID,
31};
32
33static u8 *int3400_thermal_uuids[INT3400_THERMAL_MAXIMUM_UUID] = {
34 "42A441D6-AE6A-462b-A84B-4A8CE79027D3",
c5738ddd
ZR
35 "3A95C389-E4B8-4629-A526-C52C88626BAE",
36 "97C68AE7-15FA-499c-B8C9-5DA81D606E0A",
14216f18
MG
37 "63BE270F-1C11-48FD-A6F7-3AF253FF3E2D",
38 "5349962F-71E6-431D-9AE8-0A635B710AEE",
39 "9E04115A-AE87-4D1C-9500-0F3E340BFE75",
40 "F5A35014-C209-46A4-993A-EB56DE7530A1",
41 "6ED722A7-9240-48A5-B479-31EEF723D7CF",
42 "16CAF1B7-DD38-40ED-B1C1-1B8A1913D531",
43 "BE84BABF-C4D4-403D-B495-3128FD44dAC1",
c5738ddd
ZR
44};
45
816cab93
ZR
46struct int3400_thermal_priv {
47 struct acpi_device *adev;
0ab15365
ZR
48 struct thermal_zone_device *thermal;
49 int mode;
816cab93
ZR
50 int art_count;
51 struct art *arts;
52 int trt_count;
53 struct trt *trts;
c5738ddd 54 u8 uuid_bitmap;
6306e68a 55 int rel_misc_dev_res;
19ecaea2
SP
56 int current_uuid_index;
57};
58
59static ssize_t available_uuids_show(struct device *dev,
60 struct device_attribute *attr,
61 char *buf)
62{
63 struct platform_device *pdev = to_platform_device(dev);
64 struct int3400_thermal_priv *priv = platform_get_drvdata(pdev);
65 int i;
66 int length = 0;
67
68 for (i = 0; i < INT3400_THERMAL_MAXIMUM_UUID; i++) {
69 if (priv->uuid_bitmap & (1 << i))
70 if (PAGE_SIZE - length > 0)
71 length += snprintf(&buf[length],
72 PAGE_SIZE - length,
73 "%s\n",
74 int3400_thermal_uuids[i]);
75 }
76
77 return length;
78}
79
80static ssize_t current_uuid_show(struct device *dev,
81 struct device_attribute *devattr, char *buf)
82{
83 struct platform_device *pdev = to_platform_device(dev);
84 struct int3400_thermal_priv *priv = platform_get_drvdata(pdev);
85
86 if (priv->uuid_bitmap & (1 << priv->current_uuid_index))
87 return sprintf(buf, "%s\n",
88 int3400_thermal_uuids[priv->current_uuid_index]);
89 else
90 return sprintf(buf, "INVALID\n");
91}
92
93static ssize_t current_uuid_store(struct device *dev,
94 struct device_attribute *attr,
95 const char *buf, size_t count)
96{
97 struct platform_device *pdev = to_platform_device(dev);
98 struct int3400_thermal_priv *priv = platform_get_drvdata(pdev);
99 int i;
100
101 for (i = 0; i < INT3400_THERMAL_MAXIMUM_UUID; ++i) {
102 if ((priv->uuid_bitmap & (1 << i)) &&
103 !(strncmp(buf, int3400_thermal_uuids[i],
104 sizeof(int3400_thermal_uuids[i]) - 1))) {
105 priv->current_uuid_index = i;
106 return count;
107 }
108 }
109
110 return -EINVAL;
111}
112
113static DEVICE_ATTR(current_uuid, 0644, current_uuid_show, current_uuid_store);
114static DEVICE_ATTR_RO(available_uuids);
115static struct attribute *uuid_attrs[] = {
116 &dev_attr_available_uuids.attr,
117 &dev_attr_current_uuid.attr,
118 NULL
119};
120
121static struct attribute_group uuid_attribute_group = {
122 .attrs = uuid_attrs,
123 .name = "uuids"
816cab93
ZR
124};
125
c5738ddd
ZR
126static int int3400_thermal_get_uuids(struct int3400_thermal_priv *priv)
127{
128 struct acpi_buffer buf = { ACPI_ALLOCATE_BUFFER, NULL};
129 union acpi_object *obja, *objb;
130 int i, j;
131 int result = 0;
132 acpi_status status;
133
134 status = acpi_evaluate_object(priv->adev->handle, "IDSP", NULL, &buf);
135 if (ACPI_FAILURE(status))
136 return -ENODEV;
137
138 obja = (union acpi_object *)buf.pointer;
139 if (obja->type != ACPI_TYPE_PACKAGE) {
140 result = -EINVAL;
141 goto end;
142 }
143
144 for (i = 0; i < obja->package.count; i++) {
145 objb = &obja->package.elements[i];
146 if (objb->type != ACPI_TYPE_BUFFER) {
147 result = -EINVAL;
148 goto end;
149 }
150
151 /* UUID must be 16 bytes */
152 if (objb->buffer.length != 16) {
153 result = -EINVAL;
154 goto end;
155 }
156
157 for (j = 0; j < INT3400_THERMAL_MAXIMUM_UUID; j++) {
158 u8 uuid[16];
159
160 acpi_str_to_uuid(int3400_thermal_uuids[j], uuid);
161 if (!strncmp(uuid, objb->buffer.pointer, 16)) {
162 priv->uuid_bitmap |= (1 << j);
163 break;
164 }
165 }
166 }
167
168end:
169 kfree(buf.pointer);
170 return result;
171}
172
0ab15365
ZR
173static int int3400_thermal_run_osc(acpi_handle handle,
174 enum int3400_thermal_uuid uuid, bool enable)
175{
176 u32 ret, buf[2];
177 acpi_status status;
178 int result = 0;
179 struct acpi_osc_context context = {
180 .uuid_str = int3400_thermal_uuids[uuid],
181 .rev = 1,
182 .cap.length = 8,
183 };
184
185 buf[OSC_QUERY_DWORD] = 0;
186 buf[OSC_SUPPORT_DWORD] = enable;
187
188 context.cap.pointer = buf;
189
190 status = acpi_run_osc(handle, &context);
191 if (ACPI_SUCCESS(status)) {
192 ret = *((u32 *)(context.ret.pointer + 4));
193 if (ret != enable)
194 result = -EPERM;
195 } else
196 result = -EPERM;
197
198 kfree(context.ret.pointer);
199 return result;
200}
201
0ab15365 202static int int3400_thermal_get_temp(struct thermal_zone_device *thermal,
17e8351a 203 int *temp)
0ab15365
ZR
204{
205 *temp = 20 * 1000; /* faked temp sensor with 20C */
206 return 0;
207}
208
209static int int3400_thermal_get_mode(struct thermal_zone_device *thermal,
210 enum thermal_device_mode *mode)
211{
212 struct int3400_thermal_priv *priv = thermal->devdata;
213
214 if (!priv)
215 return -EINVAL;
216
217 *mode = priv->mode;
218
219 return 0;
220}
221
222static int int3400_thermal_set_mode(struct thermal_zone_device *thermal,
223 enum thermal_device_mode mode)
224{
225 struct int3400_thermal_priv *priv = thermal->devdata;
226 bool enable;
227 int result = 0;
228
229 if (!priv)
230 return -EINVAL;
231
232 if (mode == THERMAL_DEVICE_ENABLED)
233 enable = true;
234 else if (mode == THERMAL_DEVICE_DISABLED)
235 enable = false;
236 else
237 return -EINVAL;
238
239 if (enable != priv->mode) {
240 priv->mode = enable;
0ab15365 241 result = int3400_thermal_run_osc(priv->adev->handle,
19ecaea2
SP
242 priv->current_uuid_index,
243 enable);
0ab15365
ZR
244 }
245 return result;
246}
247
248static struct thermal_zone_device_ops int3400_thermal_ops = {
249 .get_temp = int3400_thermal_get_temp,
250};
251
252static struct thermal_zone_params int3400_thermal_params = {
253 .governor_name = "user_space",
254 .no_hwmon = true,
255};
256
816cab93
ZR
257static int int3400_thermal_probe(struct platform_device *pdev)
258{
259 struct acpi_device *adev = ACPI_COMPANION(&pdev->dev);
260 struct int3400_thermal_priv *priv;
261 int result;
262
263 if (!adev)
264 return -ENODEV;
265
266 priv = kzalloc(sizeof(struct int3400_thermal_priv), GFP_KERNEL);
267 if (!priv)
268 return -ENOMEM;
269
270 priv->adev = adev;
271
c5738ddd
ZR
272 result = int3400_thermal_get_uuids(priv);
273 if (result)
274 goto free_priv;
275
6306e68a
JP
276 result = acpi_parse_art(priv->adev->handle, &priv->art_count,
277 &priv->arts, true);
816cab93 278 if (result)
f1260791 279 dev_dbg(&pdev->dev, "_ART table parsing error\n");
6306e68a
JP
280
281 result = acpi_parse_trt(priv->adev->handle, &priv->trt_count,
282 &priv->trts, true);
816cab93 283 if (result)
f1260791 284 dev_dbg(&pdev->dev, "_TRT table parsing error\n");
816cab93
ZR
285
286 platform_set_drvdata(pdev, priv);
287
0ab15365
ZR
288 if (priv->uuid_bitmap & 1 << INT3400_THERMAL_PASSIVE_1) {
289 int3400_thermal_ops.get_mode = int3400_thermal_get_mode;
290 int3400_thermal_ops.set_mode = int3400_thermal_set_mode;
291 }
292 priv->thermal = thermal_zone_device_register("INT3400 Thermal", 0, 0,
293 priv, &int3400_thermal_ops,
294 &int3400_thermal_params, 0, 0);
295 if (IS_ERR(priv->thermal)) {
296 result = PTR_ERR(priv->thermal);
f1260791 297 goto free_art_trt;
0ab15365
ZR
298 }
299
6306e68a
JP
300 priv->rel_misc_dev_res = acpi_thermal_rel_misc_device_add(
301 priv->adev->handle);
302
19ecaea2
SP
303 result = sysfs_create_group(&pdev->dev.kobj, &uuid_attribute_group);
304 if (result)
305 goto free_zone;
306
816cab93 307 return 0;
19ecaea2
SP
308
309free_zone:
310 thermal_zone_device_unregister(priv->thermal);
f1260791 311free_art_trt:
0ab15365 312 kfree(priv->trts);
816cab93
ZR
313 kfree(priv->arts);
314free_priv:
315 kfree(priv);
316 return result;
317}
318
319static int int3400_thermal_remove(struct platform_device *pdev)
320{
321 struct int3400_thermal_priv *priv = platform_get_drvdata(pdev);
322
6306e68a
JP
323 if (!priv->rel_misc_dev_res)
324 acpi_thermal_rel_misc_device_remove(priv->adev->handle);
325
19ecaea2 326 sysfs_remove_group(&pdev->dev.kobj, &uuid_attribute_group);
0ab15365 327 thermal_zone_device_unregister(priv->thermal);
816cab93
ZR
328 kfree(priv->trts);
329 kfree(priv->arts);
330 kfree(priv);
331 return 0;
332}
333
334static const struct acpi_device_id int3400_thermal_match[] = {
335 {"INT3400", 0},
336 {}
337};
338
339MODULE_DEVICE_TABLE(acpi, int3400_thermal_match);
340
341static struct platform_driver int3400_thermal_driver = {
342 .probe = int3400_thermal_probe,
343 .remove = int3400_thermal_remove,
344 .driver = {
345 .name = "int3400 thermal",
816cab93
ZR
346 .acpi_match_table = ACPI_PTR(int3400_thermal_match),
347 },
348};
349
350module_platform_driver(int3400_thermal_driver);
351
352MODULE_DESCRIPTION("INT3400 Thermal driver");
353MODULE_AUTHOR("Zhang Rui <rui.zhang@intel.com>");
354MODULE_LICENSE("GPL");