blob: 3db83a2dc3a1fc76f72bd87b26c77cb2493a0cdb [file] [log] [blame]
Thomas Sujithcc0573b2008-01-25 11:45:44 +08001/*
2 * intel_menlow.c - Intel menlow Driver for thermal management extension
3 *
4 * Copyright (C) 2008 Intel Corp
5 * Copyright (C) 2008 Sujith Thomas <sujith.thomas@intel.com>
6 * Copyright (C) 2008 Zhang Rui <rui.zhang@intel.com>
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; version 2 of the License.
12 *
13 * This program is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
21 *
22 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
23 *
24 * This driver creates the sys I/F for programming the sensors.
25 * It also implements the driver for intel menlow memory controller (hardware
26 * id is INT0002) which makes use of the platform specific ACPI methods
27 * to get/set bandwidth.
28 */
29
30#include <linux/kernel.h>
31#include <linux/module.h>
32#include <linux/init.h>
33#include <linux/types.h>
34#include <linux/pci.h>
35#include <linux/pm.h>
36
37#include <linux/thermal.h>
38#include <acpi/acpi_bus.h>
39#include <acpi/acpi_drivers.h>
40
41MODULE_AUTHOR("Thomas Sujith");
42MODULE_AUTHOR("Zhang Rui");
43MODULE_DESCRIPTION("Intel Menlow platform specific driver");
44MODULE_LICENSE("GPL");
45
46/*
47 * Memory controller device control
48 */
49
50#define MEMORY_GET_BANDWIDTH "GTHS"
51#define MEMORY_SET_BANDWIDTH "STHS"
52#define MEMORY_ARG_CUR_BANDWIDTH 1
53#define MEMORY_ARG_MAX_BANDWIDTH 0
54
55static int memory_get_int_max_bandwidth(struct thermal_cooling_device *cdev,
56 unsigned long *max_state)
57{
58 struct acpi_device *device = cdev->devdata;
59 acpi_handle handle = device->handle;
60 unsigned long value;
61 struct acpi_object_list arg_list;
62 union acpi_object arg;
63 acpi_status status = AE_OK;
64
65 arg_list.count = 1;
66 arg_list.pointer = &arg;
67 arg.type = ACPI_TYPE_INTEGER;
68 arg.integer.value = MEMORY_ARG_MAX_BANDWIDTH;
69 status = acpi_evaluate_integer(handle, MEMORY_GET_BANDWIDTH,
70 &arg_list, &value);
71 if (ACPI_FAILURE(status))
72 return -EFAULT;
73
74 *max_state = value - 1;
75 return 0;
76}
77
78static int memory_get_max_bandwidth(struct thermal_cooling_device *cdev,
79 char *buf)
80{
81 unsigned long value;
82 if (memory_get_int_max_bandwidth(cdev, &value))
83 return -EINVAL;
84
85 return sprintf(buf, "%ld\n", value);
86}
87
88static int memory_get_cur_bandwidth(struct thermal_cooling_device *cdev,
89 char *buf)
90{
91 struct acpi_device *device = cdev->devdata;
92 acpi_handle handle = device->handle;
93 unsigned long value;
94 struct acpi_object_list arg_list;
95 union acpi_object arg;
96 acpi_status status = AE_OK;
97
98 arg_list.count = 1;
99 arg_list.pointer = &arg;
100 arg.type = ACPI_TYPE_INTEGER;
101 arg.integer.value = MEMORY_ARG_CUR_BANDWIDTH;
102 status = acpi_evaluate_integer(handle, MEMORY_GET_BANDWIDTH,
103 &arg_list, &value);
104 if (ACPI_FAILURE(status))
105 return -EFAULT;
106
107 return sprintf(buf, "%ld\n", value);
108}
109
110static int memory_set_cur_bandwidth(struct thermal_cooling_device *cdev,
111 unsigned int state)
112{
113 struct acpi_device *device = cdev->devdata;
114 acpi_handle handle = device->handle;
115 struct acpi_object_list arg_list;
116 union acpi_object arg;
117 acpi_status status;
118 int temp;
119 unsigned long max_state;
120
121 if (memory_get_int_max_bandwidth(cdev, &max_state))
122 return -EFAULT;
123
124 if (max_state < 0 || state > max_state)
125 return -EINVAL;
126
127 arg_list.count = 1;
128 arg_list.pointer = &arg;
129 arg.type = ACPI_TYPE_INTEGER;
130 arg.integer.value = state;
131
132 status =
133 acpi_evaluate_integer(handle, MEMORY_SET_BANDWIDTH, &arg_list,
134 (unsigned long *)&temp);
135
136 printk(KERN_INFO
137 "Bandwidth value was %d: status is %d\n", state, status);
138 if (ACPI_FAILURE(status))
139 return -EFAULT;
140
141 return 0;
142}
143
144static struct thermal_cooling_device_ops memory_cooling_ops = {
145 .get_max_state = memory_get_max_bandwidth,
146 .get_cur_state = memory_get_cur_bandwidth,
147 .set_cur_state = memory_set_cur_bandwidth,
148};
149
150/*
151 * Memory Device Management
152 */
153static int intel_menlow_memory_add(struct acpi_device *device)
154{
155 int result = -ENODEV;
156 acpi_status status = AE_OK;
157 acpi_handle dummy;
158 struct thermal_cooling_device *cdev;
159
160 if (!device)
161 return -EINVAL;
162
163 status = acpi_get_handle(device->handle, MEMORY_GET_BANDWIDTH, &dummy);
164 if (ACPI_FAILURE(status))
165 goto end;
166
167 status = acpi_get_handle(device->handle, MEMORY_SET_BANDWIDTH, &dummy);
168 if (ACPI_FAILURE(status))
169 goto end;
170
171 cdev = thermal_cooling_device_register("Memory controller", device,
172 &memory_cooling_ops);
Thomas Sujith69f6b8d2008-02-15 01:05:23 -0500173 if (IS_ERR(cdev)) {
174 result = PTR_ERR(cdev);
175 goto end;
176 }
177
Julia Lawall90300622008-04-11 10:09:24 +0800178 acpi_driver_data(device) = cdev;
179 result = sysfs_create_link(&device->dev.kobj,
180 &cdev->device.kobj, "thermal_cooling");
181 if (result)
182 printk(KERN_ERR PREFIX "Create sysfs link\n");
183 result = sysfs_create_link(&cdev->device.kobj,
184 &device->dev.kobj, "device");
185 if (result)
186 printk(KERN_ERR PREFIX "Create sysfs link\n");
Thomas Sujithcc0573b2008-01-25 11:45:44 +0800187
188 end:
189 return result;
190
191 unregister:
192 thermal_cooling_device_unregister(cdev);
193 return result;
194
195}
196
197static int intel_menlow_memory_remove(struct acpi_device *device, int type)
198{
199 struct thermal_cooling_device *cdev = acpi_driver_data(device);
200
201 if (!device || !cdev)
202 return -EINVAL;
203
204 sysfs_remove_link(&device->dev.kobj, "thermal_cooling");
205 sysfs_remove_link(&cdev->device.kobj, "device");
206 thermal_cooling_device_unregister(cdev);
207
208 return 0;
209}
210
Tobias Klauserf7e8dd52008-04-21 22:28:49 +0000211static const struct acpi_device_id intel_menlow_memory_ids[] = {
Thomas Sujithcc0573b2008-01-25 11:45:44 +0800212 {"INT0002", 0},
213 {"", 0},
214};
215
216static struct acpi_driver intel_menlow_memory_driver = {
217 .name = "intel_menlow_thermal_control",
218 .ids = intel_menlow_memory_ids,
219 .ops = {
220 .add = intel_menlow_memory_add,
221 .remove = intel_menlow_memory_remove,
222 },
223};
224
225/*
226 * Sensor control on menlow platform
227 */
228
229#define THERMAL_AUX0 0
230#define THERMAL_AUX1 1
231#define GET_AUX0 "GAX0"
232#define GET_AUX1 "GAX1"
233#define SET_AUX0 "SAX0"
234#define SET_AUX1 "SAX1"
235
236struct intel_menlow_attribute {
237 struct device_attribute attr;
238 struct device *device;
239 acpi_handle handle;
240 struct list_head node;
241};
242
243static LIST_HEAD(intel_menlow_attr_list);
244static DEFINE_MUTEX(intel_menlow_attr_lock);
245
246/*
247 * sensor_get_auxtrip - get the current auxtrip value from sensor
248 * @name: Thermalzone name
249 * @auxtype : AUX0/AUX1
250 * @buf: syfs buffer
251 */
252static int sensor_get_auxtrip(acpi_handle handle, int index, int *value)
253{
254 acpi_status status;
255
256 if ((index != 0 && index != 1) || !value)
257 return -EINVAL;
258
259 status = acpi_evaluate_integer(handle, index ? GET_AUX1 : GET_AUX0,
260 NULL, (unsigned long *)value);
261 if (ACPI_FAILURE(status))
262 return -EIO;
263
264 return 0;
265}
266
267/*
268 * sensor_set_auxtrip - set the new auxtrip value to sensor
269 * @name: Thermalzone name
270 * @auxtype : AUX0/AUX1
271 * @buf: syfs buffer
272 */
273static int sensor_set_auxtrip(acpi_handle handle, int index, int value)
274{
275 acpi_status status;
276 union acpi_object arg = {
277 ACPI_TYPE_INTEGER
278 };
279 struct acpi_object_list args = {
280 1, &arg
281 };
282 int temp;
283
284 if (index != 0 && index != 1)
285 return -EINVAL;
286
287 status = acpi_evaluate_integer(handle, index ? GET_AUX0 : GET_AUX1,
288 NULL, (unsigned long *)&temp);
289 if (ACPI_FAILURE(status))
290 return -EIO;
291 if ((index && value < temp) || (!index && value > temp))
292 return -EINVAL;
293
294 arg.integer.value = value;
295 status = acpi_evaluate_integer(handle, index ? SET_AUX1 : SET_AUX0,
296 &args, (unsigned long *)&temp);
297 if (ACPI_FAILURE(status))
298 return -EIO;
299
300 /* do we need to check the return value of SAX0/SAX1 ? */
301
302 return 0;
303}
304
305#define to_intel_menlow_attr(_attr) \
306 container_of(_attr, struct intel_menlow_attribute, attr)
307
308static ssize_t aux0_show(struct device *dev,
309 struct device_attribute *dev_attr, char *buf)
310{
311 struct intel_menlow_attribute *attr = to_intel_menlow_attr(dev_attr);
312 int value;
313 int result;
314
315 result = sensor_get_auxtrip(attr->handle, 0, &value);
316
317 return result ? result : sprintf(buf, "%lu", KELVIN_TO_CELSIUS(value));
318}
319
320static ssize_t aux1_show(struct device *dev,
321 struct device_attribute *dev_attr, char *buf)
322{
323 struct intel_menlow_attribute *attr = to_intel_menlow_attr(dev_attr);
324 int value;
325 int result;
326
327 result = sensor_get_auxtrip(attr->handle, 1, &value);
328
329 return result ? result : sprintf(buf, "%lu", KELVIN_TO_CELSIUS(value));
330}
331
332static ssize_t aux0_store(struct device *dev,
333 struct device_attribute *dev_attr,
334 const char *buf, size_t count)
335{
336 struct intel_menlow_attribute *attr = to_intel_menlow_attr(dev_attr);
337 int value;
338 int result;
339
340 /*Sanity check; should be a positive integer */
341 if (!sscanf(buf, "%d", &value))
342 return -EINVAL;
343
344 if (value < 0)
345 return -EINVAL;
346
347 result = sensor_set_auxtrip(attr->handle, 0, CELSIUS_TO_KELVIN(value));
348 return result ? result : count;
349}
350
351static ssize_t aux1_store(struct device *dev,
352 struct device_attribute *dev_attr,
353 const char *buf, size_t count)
354{
355 struct intel_menlow_attribute *attr = to_intel_menlow_attr(dev_attr);
356 int value;
357 int result;
358
359 /*Sanity check; should be a positive integer */
360 if (!sscanf(buf, "%d", &value))
361 return -EINVAL;
362
363 if (value < 0)
364 return -EINVAL;
365
366 result = sensor_set_auxtrip(attr->handle, 1, CELSIUS_TO_KELVIN(value));
367 return result ? result : count;
368}
369
370/* BIOS can enable/disable the thermal user application in dabney platform */
371#define BIOS_ENABLED "\\_TZ.GSTS"
372static ssize_t bios_enabled_show(struct device *dev,
373 struct device_attribute *attr, char *buf)
374{
375 acpi_status status;
376 unsigned long bios_enabled;
377
378 status = acpi_evaluate_integer(NULL, BIOS_ENABLED, NULL, &bios_enabled);
379 if (ACPI_FAILURE(status))
380 return -ENODEV;
381
382 return sprintf(buf, "%s\n", bios_enabled ? "enabled" : "disabled");
383}
384
385static int intel_menlow_add_one_attribute(char *name, int mode, void *show,
386 void *store, struct device *dev,
387 acpi_handle handle)
388{
389 struct intel_menlow_attribute *attr;
390 int result;
391
392 attr = kzalloc(sizeof(struct intel_menlow_attribute), GFP_KERNEL);
393 if (!attr)
394 return -ENOMEM;
395
396 attr->attr.attr.name = name;
397 attr->attr.attr.mode = mode;
398 attr->attr.show = show;
399 attr->attr.store = store;
400 attr->device = dev;
401 attr->handle = handle;
402
403 result = device_create_file(dev, &attr->attr);
404 if (result)
405 return result;
406
407 mutex_lock(&intel_menlow_attr_lock);
408 list_add_tail(&attr->node, &intel_menlow_attr_list);
409 mutex_unlock(&intel_menlow_attr_lock);
410
411 return 0;
412}
413
414static acpi_status intel_menlow_register_sensor(acpi_handle handle, u32 lvl,
415 void *context, void **rv)
416{
417 acpi_status status;
418 acpi_handle dummy;
419 struct thermal_zone_device *thermal;
420 int result;
421
422 result = acpi_bus_get_private_data(handle, (void **)&thermal);
423 if (result)
424 return 0;
425
426 /* _TZ must have the AUX0/1 methods */
427 status = acpi_get_handle(handle, GET_AUX0, &dummy);
428 if (ACPI_FAILURE(status))
429 goto not_found;
430
431 status = acpi_get_handle(handle, SET_AUX0, &dummy);
432 if (ACPI_FAILURE(status))
433 goto not_found;
434
435 result = intel_menlow_add_one_attribute("aux0", 0644,
436 aux0_show, aux0_store,
437 &thermal->device, handle);
438 if (result)
439 return AE_ERROR;
440
441 status = acpi_get_handle(handle, GET_AUX1, &dummy);
442 if (ACPI_FAILURE(status))
443 goto not_found;
444
445 status = acpi_get_handle(handle, SET_AUX1, &dummy);
446 if (ACPI_FAILURE(status))
447 goto not_found;
448
449 result = intel_menlow_add_one_attribute("aux1", 0644,
450 aux1_show, aux1_store,
451 &thermal->device, handle);
452 if (result)
453 return AE_ERROR;
454
455 /*
456 * create the "dabney_enabled" attribute which means the user app
457 * should be loaded or not
458 */
459
460 result = intel_menlow_add_one_attribute("bios_enabled", 0444,
461 bios_enabled_show, NULL,
462 &thermal->device, handle);
463 if (result)
464 return AE_ERROR;
465
466 not_found:
467 if (status == AE_NOT_FOUND)
468 return AE_OK;
469 else
470 return status;
471}
472
473static void intel_menlow_unregister_sensor(void)
474{
475 struct intel_menlow_attribute *pos, *next;
476
477 mutex_lock(&intel_menlow_attr_lock);
478 list_for_each_entry_safe(pos, next, &intel_menlow_attr_list, node) {
479 list_del(&pos->node);
480 device_remove_file(pos->device, &pos->attr);
481 kfree(pos);
482 }
483 mutex_unlock(&intel_menlow_attr_lock);
484
485 return;
486}
487
488static int __init intel_menlow_module_init(void)
489{
490 int result = -ENODEV;
491 acpi_status status;
492 unsigned long enable;
493
494 if (acpi_disabled)
495 return result;
496
497 /* Looking for the \_TZ.GSTS method */
498 status = acpi_evaluate_integer(NULL, BIOS_ENABLED, NULL, &enable);
499 if (ACPI_FAILURE(status) || !enable)
500 return -ENODEV;
501
502 /* Looking for ACPI device MEM0 with hardware id INT0002 */
503 result = acpi_bus_register_driver(&intel_menlow_memory_driver);
504 if (result)
505 return result;
506
507 /* Looking for sensors in each ACPI thermal zone */
508 status = acpi_walk_namespace(ACPI_TYPE_THERMAL, ACPI_ROOT_OBJECT,
509 ACPI_UINT32_MAX,
510 intel_menlow_register_sensor, NULL, NULL);
511 if (ACPI_FAILURE(status))
512 return -ENODEV;
513
514 return 0;
515}
516
517static void __exit intel_menlow_module_exit(void)
518{
519 acpi_bus_unregister_driver(&intel_menlow_memory_driver);
520 intel_menlow_unregister_sensor();
521}
522
523module_init(intel_menlow_module_init);
524module_exit(intel_menlow_module_exit);