]> Git Repo - linux.git/blob - drivers/acpi/fan.c
Merge branch 'int340x-thermal' of .git into next
[linux.git] / drivers / acpi / fan.c
1 /*
2  *  acpi_fan.c - ACPI Fan Driver ($Revision: 29 $)
3  *
4  *  Copyright (C) 2001, 2002 Andy Grover <[email protected]>
5  *  Copyright (C) 2001, 2002 Paul Diefenbaugh <[email protected]>
6  *
7  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
8  *
9  *  This program is free software; you can redistribute it and/or modify
10  *  it under the terms of the GNU General Public License as published by
11  *  the Free Software Foundation; either version 2 of the License, or (at
12  *  your option) any later version.
13  *
14  *  This program is distributed in the hope that it will be useful, but
15  *  WITHOUT ANY WARRANTY; without even the implied warranty of
16  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17  *  General Public License for more details.
18  *
19  *  You should have received a copy of the GNU General Public License along
20  *  with this program; if not, write to the Free Software Foundation, Inc.,
21  *  59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
22  *
23  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
24  */
25
26 #include <linux/kernel.h>
27 #include <linux/module.h>
28 #include <linux/init.h>
29 #include <linux/types.h>
30 #include <asm/uaccess.h>
31 #include <linux/thermal.h>
32 #include <linux/acpi.h>
33 #include <linux/platform_device.h>
34 #include <linux/sort.h>
35
36 MODULE_AUTHOR("Paul Diefenbaugh");
37 MODULE_DESCRIPTION("ACPI Fan Driver");
38 MODULE_LICENSE("GPL");
39
40 static int acpi_fan_probe(struct platform_device *pdev);
41 static int acpi_fan_remove(struct platform_device *pdev);
42
43 static const struct acpi_device_id fan_device_ids[] = {
44         {"PNP0C0B", 0},
45         {"INT3404", 0},
46         {"", 0},
47 };
48 MODULE_DEVICE_TABLE(acpi, fan_device_ids);
49
50 #ifdef CONFIG_PM_SLEEP
51 static int acpi_fan_suspend(struct device *dev);
52 static int acpi_fan_resume(struct device *dev);
53 static struct dev_pm_ops acpi_fan_pm = {
54         .resume = acpi_fan_resume,
55         .freeze = acpi_fan_suspend,
56         .thaw = acpi_fan_resume,
57         .restore = acpi_fan_resume,
58 };
59 #define FAN_PM_OPS_PTR (&acpi_fan_pm)
60 #else
61 #define FAN_PM_OPS_PTR NULL
62 #endif
63
64 struct acpi_fan_fps {
65         u64 control;
66         u64 trip_point;
67         u64 speed;
68         u64 noise_level;
69         u64 power;
70 };
71
72 struct acpi_fan_fif {
73         u64 revision;
74         u64 fine_grain_ctrl;
75         u64 step_size;
76         u64 low_speed_notification;
77 };
78
79 struct acpi_fan {
80         bool acpi4;
81         struct acpi_fan_fif fif;
82         struct acpi_fan_fps *fps;
83         int fps_count;
84         struct thermal_cooling_device *cdev;
85 };
86
87 static struct platform_driver acpi_fan_driver = {
88         .probe = acpi_fan_probe,
89         .remove = acpi_fan_remove,
90         .driver = {
91                 .name = "acpi-fan",
92                 .acpi_match_table = fan_device_ids,
93                 .pm = FAN_PM_OPS_PTR,
94         },
95 };
96
97 /* thermal cooling device callbacks */
98 static int fan_get_max_state(struct thermal_cooling_device *cdev, unsigned long
99                              *state)
100 {
101         struct acpi_device *device = cdev->devdata;
102         struct acpi_fan *fan = acpi_driver_data(device);
103
104         if (fan->acpi4)
105                 *state = fan->fps_count - 1;
106         else
107                 *state = 1;
108         return 0;
109 }
110
111 static int fan_get_state_acpi4(struct acpi_device *device, unsigned long *state)
112 {
113         struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
114         struct acpi_fan *fan = acpi_driver_data(device);
115         union acpi_object *obj;
116         acpi_status status;
117         int control, i;
118
119         status = acpi_evaluate_object(device->handle, "_FST", NULL, &buffer);
120         if (ACPI_FAILURE(status)) {
121                 dev_err(&device->dev, "Get fan state failed\n");
122                 return status;
123         }
124
125         obj = buffer.pointer;
126         if (!obj || obj->type != ACPI_TYPE_PACKAGE ||
127             obj->package.count != 3 ||
128             obj->package.elements[1].type != ACPI_TYPE_INTEGER) {
129                 dev_err(&device->dev, "Invalid _FST data\n");
130                 status = -EINVAL;
131                 goto err;
132         }
133
134         control = obj->package.elements[1].integer.value;
135         for (i = 0; i < fan->fps_count; i++) {
136                 if (control == fan->fps[i].control)
137                         break;
138         }
139         if (i == fan->fps_count) {
140                 dev_dbg(&device->dev, "Invalid control value returned\n");
141                 status = -EINVAL;
142                 goto err;
143         }
144
145         *state = i;
146
147 err:
148         kfree(obj);
149         return status;
150 }
151
152 static int fan_get_state(struct acpi_device *device, unsigned long *state)
153 {
154         int result;
155         int acpi_state = ACPI_STATE_D0;
156
157         result = acpi_device_update_power(device, &acpi_state);
158         if (result)
159                 return result;
160
161         *state = (acpi_state == ACPI_STATE_D3_COLD ? 0 :
162                  (acpi_state == ACPI_STATE_D0 ? 1 : -1));
163         return 0;
164 }
165
166 static int fan_get_cur_state(struct thermal_cooling_device *cdev, unsigned long
167                              *state)
168 {
169         struct acpi_device *device = cdev->devdata;
170         struct acpi_fan *fan = acpi_driver_data(device);
171
172         if (fan->acpi4)
173                 return fan_get_state_acpi4(device, state);
174         else
175                 return fan_get_state(device, state);
176 }
177
178 static int fan_set_state(struct acpi_device *device, unsigned long state)
179 {
180         if (state != 0 && state != 1)
181                 return -EINVAL;
182
183         return acpi_device_set_power(device,
184                                      state ? ACPI_STATE_D0 : ACPI_STATE_D3_COLD);
185 }
186
187 static int fan_set_state_acpi4(struct acpi_device *device, unsigned long state)
188 {
189         struct acpi_fan *fan = acpi_driver_data(device);
190         acpi_status status;
191
192         if (state >= fan->fps_count)
193                 return -EINVAL;
194
195         status = acpi_execute_simple_method(device->handle, "_FSL",
196                                             fan->fps[state].control);
197         if (ACPI_FAILURE(status)) {
198                 dev_dbg(&device->dev, "Failed to set state by _FSL\n");
199                 return status;
200         }
201
202         return 0;
203 }
204
205 static int
206 fan_set_cur_state(struct thermal_cooling_device *cdev, unsigned long state)
207 {
208         struct acpi_device *device = cdev->devdata;
209         struct acpi_fan *fan = acpi_driver_data(device);
210
211         if (fan->acpi4)
212                 return fan_set_state_acpi4(device, state);
213         else
214                 return fan_set_state(device, state);
215  }
216
217 static const struct thermal_cooling_device_ops fan_cooling_ops = {
218         .get_max_state = fan_get_max_state,
219         .get_cur_state = fan_get_cur_state,
220         .set_cur_state = fan_set_cur_state,
221 };
222
223 /* --------------------------------------------------------------------------
224                                  Driver Interface
225    -------------------------------------------------------------------------- */
226
227 static bool acpi_fan_is_acpi4(struct acpi_device *device)
228 {
229         return acpi_has_method(device->handle, "_FIF") &&
230                acpi_has_method(device->handle, "_FPS") &&
231                acpi_has_method(device->handle, "_FSL") &&
232                acpi_has_method(device->handle, "_FST");
233 }
234
235 static int acpi_fan_get_fif(struct acpi_device *device)
236 {
237         struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
238         struct acpi_fan *fan = acpi_driver_data(device);
239         struct acpi_buffer format = { sizeof("NNNN"), "NNNN" };
240         struct acpi_buffer fif = { sizeof(fan->fif), &fan->fif };
241         union acpi_object *obj;
242         acpi_status status;
243
244         status = acpi_evaluate_object(device->handle, "_FIF", NULL, &buffer);
245         if (ACPI_FAILURE(status))
246                 return status;
247
248         obj = buffer.pointer;
249         if (!obj || obj->type != ACPI_TYPE_PACKAGE) {
250                 dev_err(&device->dev, "Invalid _FIF data\n");
251                 status = -EINVAL;
252                 goto err;
253         }
254
255         status = acpi_extract_package(obj, &format, &fif);
256         if (ACPI_FAILURE(status)) {
257                 dev_err(&device->dev, "Invalid _FIF element\n");
258                 status = -EINVAL;
259         }
260
261 err:
262         kfree(obj);
263         return status;
264 }
265
266 static int acpi_fan_speed_cmp(const void *a, const void *b)
267 {
268         const struct acpi_fan_fps *fps1 = a;
269         const struct acpi_fan_fps *fps2 = b;
270         return fps1->speed - fps2->speed;
271 }
272
273 static int acpi_fan_get_fps(struct acpi_device *device)
274 {
275         struct acpi_fan *fan = acpi_driver_data(device);
276         struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
277         union acpi_object *obj;
278         acpi_status status;
279         int i;
280
281         status = acpi_evaluate_object(device->handle, "_FPS", NULL, &buffer);
282         if (ACPI_FAILURE(status))
283                 return status;
284
285         obj = buffer.pointer;
286         if (!obj || obj->type != ACPI_TYPE_PACKAGE || obj->package.count < 2) {
287                 dev_err(&device->dev, "Invalid _FPS data\n");
288                 status = -EINVAL;
289                 goto err;
290         }
291
292         fan->fps_count = obj->package.count - 1; /* minus revision field */
293         fan->fps = devm_kzalloc(&device->dev,
294                                 fan->fps_count * sizeof(struct acpi_fan_fps),
295                                 GFP_KERNEL);
296         if (!fan->fps) {
297                 dev_err(&device->dev, "Not enough memory\n");
298                 status = -ENOMEM;
299                 goto err;
300         }
301         for (i = 0; i < fan->fps_count; i++) {
302                 struct acpi_buffer format = { sizeof("NNNNN"), "NNNNN" };
303                 struct acpi_buffer fps = { sizeof(fan->fps[i]), &fan->fps[i] };
304                 status = acpi_extract_package(&obj->package.elements[i + 1],
305                                               &format, &fps);
306                 if (ACPI_FAILURE(status)) {
307                         dev_err(&device->dev, "Invalid _FPS element\n");
308                         break;
309                 }
310         }
311
312         /* sort the state array according to fan speed in increase order */
313         sort(fan->fps, fan->fps_count, sizeof(*fan->fps),
314              acpi_fan_speed_cmp, NULL);
315
316 err:
317         kfree(obj);
318         return status;
319 }
320
321 static int acpi_fan_probe(struct platform_device *pdev)
322 {
323         int result = 0;
324         struct thermal_cooling_device *cdev;
325         struct acpi_fan *fan;
326         struct acpi_device *device = ACPI_COMPANION(&pdev->dev);
327
328         fan = devm_kzalloc(&pdev->dev, sizeof(*fan), GFP_KERNEL);
329         if (!fan) {
330                 dev_err(&device->dev, "No memory for fan\n");
331                 return -ENOMEM;
332         }
333         device->driver_data = fan;
334         platform_set_drvdata(pdev, fan);
335
336         if (acpi_fan_is_acpi4(device)) {
337                 if (acpi_fan_get_fif(device) || acpi_fan_get_fps(device))
338                         goto end;
339                 fan->acpi4 = true;
340         } else {
341                 result = acpi_device_update_power(device, NULL);
342                 if (result) {
343                         dev_err(&device->dev, "Setting initial power state\n");
344                         goto end;
345                 }
346         }
347
348         cdev = thermal_cooling_device_register("Fan", device,
349                                                 &fan_cooling_ops);
350         if (IS_ERR(cdev)) {
351                 result = PTR_ERR(cdev);
352                 goto end;
353         }
354
355         dev_dbg(&pdev->dev, "registered as cooling_device%d\n", cdev->id);
356
357         fan->cdev = cdev;
358         result = sysfs_create_link(&pdev->dev.kobj,
359                                    &cdev->device.kobj,
360                                    "thermal_cooling");
361         if (result)
362                 dev_err(&pdev->dev, "Failed to create sysfs link "
363                         "'thermal_cooling'\n");
364
365         result = sysfs_create_link(&cdev->device.kobj,
366                                    &pdev->dev.kobj,
367                                    "device");
368         if (result)
369                 dev_err(&pdev->dev, "Failed to create sysfs link "
370                         "'device'\n");
371
372 end:
373         return result;
374 }
375
376 static int acpi_fan_remove(struct platform_device *pdev)
377 {
378         struct acpi_fan *fan = platform_get_drvdata(pdev);
379
380         sysfs_remove_link(&pdev->dev.kobj, "thermal_cooling");
381         sysfs_remove_link(&fan->cdev->device.kobj, "device");
382         thermal_cooling_device_unregister(fan->cdev);
383
384         return 0;
385 }
386
387 #ifdef CONFIG_PM_SLEEP
388 static int acpi_fan_suspend(struct device *dev)
389 {
390         struct acpi_fan *fan = dev_get_drvdata(dev);
391         if (fan->acpi4)
392                 return 0;
393
394         acpi_device_set_power(ACPI_COMPANION(dev), ACPI_STATE_D0);
395
396         return AE_OK;
397 }
398
399 static int acpi_fan_resume(struct device *dev)
400 {
401         int result;
402         struct acpi_fan *fan = dev_get_drvdata(dev);
403
404         if (fan->acpi4)
405                 return 0;
406
407         result = acpi_device_update_power(ACPI_COMPANION(dev), NULL);
408         if (result)
409                 dev_err(dev, "Error updating fan power state\n");
410
411         return result;
412 }
413 #endif
414
415 module_platform_driver(acpi_fan_driver);
This page took 0.060705 seconds and 4 git commands to generate.