RT-AC66 3.0.0.4.374.130 core
[tomato.git] / release / src-rt-6.x / linux / linux-2.6 / drivers / hwmon / applesmc.c
blob55397a16ed693aeb4ebcb22a49c2c2fe39ec4757
1 /*
2 * drivers/hwmon/applesmc.c - driver for Apple's SMC (accelerometer, temperature
3 * sensors, fan control, keyboard backlight control) used in Intel-based Apple
4 * computers.
6 * Copyright (C) 2007 Nicolas Boichat <nicolas@boichat.ch>
8 * Based on hdaps.c driver:
9 * Copyright (C) 2005 Robert Love <rml@novell.com>
10 * Copyright (C) 2005 Jesper Juhl <jesper.juhl@gmail.com>
12 * Fan control based on smcFanControl:
13 * Copyright (C) 2006 Hendrik Holtmann <holtmann@mac.com>
15 * This program is free software; you can redistribute it and/or modify it
16 * under the terms of the GNU General Public License v2 as published by the
17 * Free Software Foundation.
19 * This program is distributed in the hope that it will be useful, but WITHOUT
20 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
21 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
22 * more details.
24 * You should have received a copy of the GNU General Public License along with
25 * this program; if not, write to the Free Software Foundation, Inc.,
26 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
29 #include <linux/delay.h>
30 #include <linux/platform_device.h>
31 #include <linux/input.h>
32 #include <linux/kernel.h>
33 #include <linux/module.h>
34 #include <linux/timer.h>
35 #include <linux/dmi.h>
36 #include <linux/mutex.h>
37 #include <linux/hwmon-sysfs.h>
38 #include <asm/io.h>
39 #include <linux/leds.h>
40 #include <linux/hwmon.h>
41 #include <linux/workqueue.h>
43 /* data port used by Apple SMC */
44 #define APPLESMC_DATA_PORT 0x300
45 /* command/status port used by Apple SMC */
46 #define APPLESMC_CMD_PORT 0x304
48 #define APPLESMC_NR_PORTS 32 /* 0x300-0x31f */
50 #define APPLESMC_MAX_DATA_LENGTH 32
52 #define APPLESMC_STATUS_MASK 0x0f
53 #define APPLESMC_READ_CMD 0x10
54 #define APPLESMC_WRITE_CMD 0x11
55 #define APPLESMC_GET_KEY_BY_INDEX_CMD 0x12
56 #define APPLESMC_GET_KEY_TYPE_CMD 0x13
58 #define KEY_COUNT_KEY "#KEY" /* r-o ui32 */
60 #define LIGHT_SENSOR_LEFT_KEY "ALV0" /* r-o {alv (6 bytes) */
61 #define LIGHT_SENSOR_RIGHT_KEY "ALV1" /* r-o {alv (6 bytes) */
62 #define BACKLIGHT_KEY "LKSB" /* w-o {lkb (2 bytes) */
64 #define CLAMSHELL_KEY "MSLD" /* r-o ui8 (unused) */
66 #define MOTION_SENSOR_X_KEY "MO_X" /* r-o sp78 (2 bytes) */
67 #define MOTION_SENSOR_Y_KEY "MO_Y" /* r-o sp78 (2 bytes) */
68 #define MOTION_SENSOR_Z_KEY "MO_Z" /* r-o sp78 (2 bytes) */
69 #define MOTION_SENSOR_KEY "MOCN" /* r/w ui16 */
71 #define FANS_COUNT "FNum" /* r-o ui8 */
72 #define FANS_MANUAL "FS! " /* r-w ui16 */
73 #define FAN_ACTUAL_SPEED "F0Ac" /* r-o fpe2 (2 bytes) */
74 #define FAN_MIN_SPEED "F0Mn" /* r-o fpe2 (2 bytes) */
75 #define FAN_MAX_SPEED "F0Mx" /* r-o fpe2 (2 bytes) */
76 #define FAN_SAFE_SPEED "F0Sf" /* r-o fpe2 (2 bytes) */
77 #define FAN_TARGET_SPEED "F0Tg" /* r-w fpe2 (2 bytes) */
78 #define FAN_POSITION "F0ID" /* r-o char[16] */
81 * Temperature sensors keys (sp78 - 2 bytes).
82 * First set for Macbook(Pro), second for Macmini.
84 static const char* temperature_sensors_sets[][13] = {
85 { "TA0P", "TB0T", "TC0D", "TC0P", "TG0H", "TG0P", "TG0T", "Th0H",
86 "Th1H", "Tm0P", "Ts0P", "Ts1P", NULL },
87 { "TC0D", "TC0P", NULL }
90 /* List of keys used to read/write fan speeds */
91 static const char* fan_speed_keys[] = {
92 FAN_ACTUAL_SPEED,
93 FAN_MIN_SPEED,
94 FAN_MAX_SPEED,
95 FAN_SAFE_SPEED,
96 FAN_TARGET_SPEED
99 #define INIT_TIMEOUT_MSECS 5000 /* wait up to 5s for device init ... */
100 #define INIT_WAIT_MSECS 50 /* ... in 50ms increments */
102 #define APPLESMC_POLL_PERIOD (HZ/20) /* poll for input every 1/20s */
103 #define APPLESMC_INPUT_FUZZ 4 /* input event threshold */
104 #define APPLESMC_INPUT_FLAT 4
106 #define SENSOR_X 0
107 #define SENSOR_Y 1
108 #define SENSOR_Z 2
110 /* Structure to be passed to DMI_MATCH function */
111 struct dmi_match_data {
112 /* Indicates whether this computer has an accelerometer. */
113 int accelerometer;
114 /* Indicates whether this computer has light sensors and keyboard backlight. */
115 int light;
116 /* Indicates which temperature sensors set to use. */
117 int temperature_set;
120 static const int debug;
121 static struct platform_device *pdev;
122 static s16 rest_x;
123 static s16 rest_y;
124 static struct timer_list applesmc_timer;
125 static struct input_dev *applesmc_idev;
126 static struct class_device *hwmon_class_dev;
128 /* Indicates whether this computer has an accelerometer. */
129 static unsigned int applesmc_accelerometer;
131 /* Indicates whether this computer has light sensors and keyboard backlight. */
132 static unsigned int applesmc_light;
134 /* Indicates which temperature sensors set to use. */
135 static unsigned int applesmc_temperature_set;
137 static struct mutex applesmc_lock;
140 * Last index written to key_at_index sysfs file, and value to use for all other
141 * key_at_index_* sysfs files.
143 static unsigned int key_at_index;
145 static struct workqueue_struct *applesmc_led_wq;
148 * __wait_status - Wait up to 2ms for the status port to get a certain value
149 * (masked with 0x0f), returning zero if the value is obtained. Callers must
150 * hold applesmc_lock.
152 static int __wait_status(u8 val)
154 unsigned int i;
156 val = val & APPLESMC_STATUS_MASK;
158 for (i = 0; i < 200; i++) {
159 if ((inb(APPLESMC_CMD_PORT) & APPLESMC_STATUS_MASK) == val) {
160 if (debug)
161 printk(KERN_DEBUG
162 "Waited %d us for status %x\n",
163 i*10, val);
164 return 0;
166 udelay(10);
169 printk(KERN_WARNING "applesmc: wait status failed: %x != %x\n",
170 val, inb(APPLESMC_CMD_PORT));
172 return -EIO;
176 * applesmc_read_key - reads len bytes from a given key, and put them in buffer.
177 * Returns zero on success or a negative error on failure. Callers must
178 * hold applesmc_lock.
180 static int applesmc_read_key(const char* key, u8* buffer, u8 len)
182 int i;
184 if (len > APPLESMC_MAX_DATA_LENGTH) {
185 printk(KERN_ERR "applesmc_read_key: cannot read more than "
186 "%d bytes\n", APPLESMC_MAX_DATA_LENGTH);
187 return -EINVAL;
190 outb(APPLESMC_READ_CMD, APPLESMC_CMD_PORT);
191 if (__wait_status(0x0c))
192 return -EIO;
194 for (i = 0; i < 4; i++) {
195 outb(key[i], APPLESMC_DATA_PORT);
196 if (__wait_status(0x04))
197 return -EIO;
199 if (debug)
200 printk(KERN_DEBUG "<%s", key);
202 outb(len, APPLESMC_DATA_PORT);
203 if (debug)
204 printk(KERN_DEBUG ">%x", len);
206 for (i = 0; i < len; i++) {
207 if (__wait_status(0x05))
208 return -EIO;
209 buffer[i] = inb(APPLESMC_DATA_PORT);
210 if (debug)
211 printk(KERN_DEBUG "<%x", buffer[i]);
213 if (debug)
214 printk(KERN_DEBUG "\n");
216 return 0;
220 * applesmc_write_key - writes len bytes from buffer to a given key.
221 * Returns zero on success or a negative error on failure. Callers must
222 * hold applesmc_lock.
224 static int applesmc_write_key(const char* key, u8* buffer, u8 len)
226 int i;
228 if (len > APPLESMC_MAX_DATA_LENGTH) {
229 printk(KERN_ERR "applesmc_write_key: cannot write more than "
230 "%d bytes\n", APPLESMC_MAX_DATA_LENGTH);
231 return -EINVAL;
234 outb(APPLESMC_WRITE_CMD, APPLESMC_CMD_PORT);
235 if (__wait_status(0x0c))
236 return -EIO;
238 for (i = 0; i < 4; i++) {
239 outb(key[i], APPLESMC_DATA_PORT);
240 if (__wait_status(0x04))
241 return -EIO;
244 outb(len, APPLESMC_DATA_PORT);
246 for (i = 0; i < len; i++) {
247 if (__wait_status(0x04))
248 return -EIO;
249 outb(buffer[i], APPLESMC_DATA_PORT);
252 return 0;
256 * applesmc_get_key_at_index - get key at index, and put the result in key
257 * (char[6]). Returns zero on success or a negative error on failure. Callers
258 * must hold applesmc_lock.
260 static int applesmc_get_key_at_index(int index, char* key)
262 int i;
263 u8 readkey[4];
264 readkey[0] = index >> 24;
265 readkey[1] = index >> 16;
266 readkey[2] = index >> 8;
267 readkey[3] = index;
269 outb(APPLESMC_GET_KEY_BY_INDEX_CMD, APPLESMC_CMD_PORT);
270 if (__wait_status(0x0c))
271 return -EIO;
273 for (i = 0; i < 4; i++) {
274 outb(readkey[i], APPLESMC_DATA_PORT);
275 if (__wait_status(0x04))
276 return -EIO;
279 outb(4, APPLESMC_DATA_PORT);
281 for (i = 0; i < 4; i++) {
282 if (__wait_status(0x05))
283 return -EIO;
284 key[i] = inb(APPLESMC_DATA_PORT);
286 key[4] = 0;
288 return 0;
292 * applesmc_get_key_type - get key type, and put the result in type (char[6]).
293 * Returns zero on success or a negative error on failure. Callers must
294 * hold applesmc_lock.
296 static int applesmc_get_key_type(char* key, char* type)
298 int i;
300 outb(APPLESMC_GET_KEY_TYPE_CMD, APPLESMC_CMD_PORT);
301 if (__wait_status(0x0c))
302 return -EIO;
304 for (i = 0; i < 4; i++) {
305 outb(key[i], APPLESMC_DATA_PORT);
306 if (__wait_status(0x04))
307 return -EIO;
310 outb(5, APPLESMC_DATA_PORT);
312 for (i = 0; i < 6; i++) {
313 if (__wait_status(0x05))
314 return -EIO;
315 type[i] = inb(APPLESMC_DATA_PORT);
317 type[5] = 0;
319 return 0;
323 * applesmc_read_motion_sensor - Read motion sensor (X, Y or Z). Callers must
324 * hold applesmc_lock.
326 static int applesmc_read_motion_sensor(int index, s16* value)
328 u8 buffer[2];
329 int ret;
331 switch (index) {
332 case SENSOR_X:
333 ret = applesmc_read_key(MOTION_SENSOR_X_KEY, buffer, 2);
334 break;
335 case SENSOR_Y:
336 ret = applesmc_read_key(MOTION_SENSOR_Y_KEY, buffer, 2);
337 break;
338 case SENSOR_Z:
339 ret = applesmc_read_key(MOTION_SENSOR_Z_KEY, buffer, 2);
340 break;
341 default:
342 ret = -EINVAL;
345 *value = ((s16)buffer[0] << 8) | buffer[1];
347 return ret;
351 * applesmc_device_init - initialize the accelerometer. Returns zero on success
352 * and negative error code on failure. Can sleep.
354 static int applesmc_device_init(void)
356 int total, ret = -ENXIO;
357 u8 buffer[2];
359 if (!applesmc_accelerometer)
360 return 0;
362 mutex_lock(&applesmc_lock);
364 for (total = INIT_TIMEOUT_MSECS; total > 0; total -= INIT_WAIT_MSECS) {
365 if (debug)
366 printk(KERN_DEBUG "applesmc try %d\n", total);
367 if (!applesmc_read_key(MOTION_SENSOR_KEY, buffer, 2) &&
368 (buffer[0] != 0x00 || buffer[1] != 0x00)) {
369 if (total == INIT_TIMEOUT_MSECS) {
370 printk(KERN_DEBUG "applesmc: device has"
371 " already been initialized"
372 " (0x%02x, 0x%02x).\n",
373 buffer[0], buffer[1]);
374 } else {
375 printk(KERN_DEBUG "applesmc: device"
376 " successfully initialized"
377 " (0x%02x, 0x%02x).\n",
378 buffer[0], buffer[1]);
380 ret = 0;
381 goto out;
383 buffer[0] = 0xe0;
384 buffer[1] = 0x00;
385 applesmc_write_key(MOTION_SENSOR_KEY, buffer, 2);
386 msleep(INIT_WAIT_MSECS);
389 printk(KERN_WARNING "applesmc: failed to init the device\n");
391 out:
392 mutex_unlock(&applesmc_lock);
393 return ret;
397 * applesmc_get_fan_count - get the number of fans. Callers must NOT hold
398 * applesmc_lock.
400 static int applesmc_get_fan_count(void)
402 int ret;
403 u8 buffer[1];
405 mutex_lock(&applesmc_lock);
407 ret = applesmc_read_key(FANS_COUNT, buffer, 1);
409 mutex_unlock(&applesmc_lock);
410 if (ret)
411 return ret;
412 else
413 return buffer[0];
416 /* Device model stuff */
417 static int applesmc_probe(struct platform_device *dev)
419 int ret;
421 ret = applesmc_device_init();
422 if (ret)
423 return ret;
425 printk(KERN_INFO "applesmc: device successfully initialized.\n");
426 return 0;
429 static int applesmc_resume(struct platform_device *dev)
431 return applesmc_device_init();
434 static struct platform_driver applesmc_driver = {
435 .probe = applesmc_probe,
436 .resume = applesmc_resume,
437 .driver = {
438 .name = "applesmc",
439 .owner = THIS_MODULE,
444 * applesmc_calibrate - Set our "resting" values. Callers must
445 * hold applesmc_lock.
447 static void applesmc_calibrate(void)
449 applesmc_read_motion_sensor(SENSOR_X, &rest_x);
450 applesmc_read_motion_sensor(SENSOR_Y, &rest_y);
451 rest_x = -rest_x;
454 static int applesmc_idev_open(struct input_dev *dev)
456 add_timer(&applesmc_timer);
458 return 0;
461 static void applesmc_idev_close(struct input_dev *dev)
463 del_timer_sync(&applesmc_timer);
466 static void applesmc_idev_poll(unsigned long unused)
468 s16 x, y;
470 /* Cannot sleep. Try nonblockingly. If we fail, try again later. */
471 if (!mutex_trylock(&applesmc_lock)) {
472 mod_timer(&applesmc_timer, jiffies + APPLESMC_POLL_PERIOD);
473 return;
476 if (applesmc_read_motion_sensor(SENSOR_X, &x))
477 goto out;
478 if (applesmc_read_motion_sensor(SENSOR_Y, &y))
479 goto out;
481 x = -x;
482 input_report_abs(applesmc_idev, ABS_X, x - rest_x);
483 input_report_abs(applesmc_idev, ABS_Y, y - rest_y);
484 input_sync(applesmc_idev);
486 out:
487 mod_timer(&applesmc_timer, jiffies + APPLESMC_POLL_PERIOD);
489 mutex_unlock(&applesmc_lock);
492 /* Sysfs Files */
494 static ssize_t applesmc_name_show(struct device *dev,
495 struct device_attribute *attr, char *buf)
497 return snprintf(buf, PAGE_SIZE, "applesmc\n");
500 static ssize_t applesmc_position_show(struct device *dev,
501 struct device_attribute *attr, char *buf)
503 int ret;
504 s16 x, y, z;
506 mutex_lock(&applesmc_lock);
508 ret = applesmc_read_motion_sensor(SENSOR_X, &x);
509 if (ret)
510 goto out;
511 ret = applesmc_read_motion_sensor(SENSOR_Y, &y);
512 if (ret)
513 goto out;
514 ret = applesmc_read_motion_sensor(SENSOR_Z, &z);
515 if (ret)
516 goto out;
518 out:
519 mutex_unlock(&applesmc_lock);
520 if (ret)
521 return ret;
522 else
523 return snprintf(buf, PAGE_SIZE, "(%d,%d,%d)\n", x, y, z);
526 static ssize_t applesmc_light_show(struct device *dev,
527 struct device_attribute *attr, char *sysfsbuf)
529 int ret;
530 u8 left = 0, right = 0;
531 u8 buffer[6];
533 mutex_lock(&applesmc_lock);
535 ret = applesmc_read_key(LIGHT_SENSOR_LEFT_KEY, buffer, 6);
536 left = buffer[2];
537 if (ret)
538 goto out;
539 ret = applesmc_read_key(LIGHT_SENSOR_RIGHT_KEY, buffer, 6);
540 right = buffer[2];
542 out:
543 mutex_unlock(&applesmc_lock);
544 if (ret)
545 return ret;
546 else
547 return snprintf(sysfsbuf, PAGE_SIZE, "(%d,%d)\n", left, right);
550 /* Displays degree Celsius * 1000 */
551 static ssize_t applesmc_show_temperature(struct device *dev,
552 struct device_attribute *devattr, char *sysfsbuf)
554 int ret;
555 u8 buffer[2];
556 unsigned int temp;
557 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
558 const char* key =
559 temperature_sensors_sets[applesmc_temperature_set][attr->index];
561 mutex_lock(&applesmc_lock);
563 ret = applesmc_read_key(key, buffer, 2);
564 temp = buffer[0]*1000;
565 temp += (buffer[1] >> 6) * 250;
567 mutex_unlock(&applesmc_lock);
569 if (ret)
570 return ret;
571 else
572 return snprintf(sysfsbuf, PAGE_SIZE, "%u\n", temp);
575 static ssize_t applesmc_show_fan_speed(struct device *dev,
576 struct device_attribute *attr, char *sysfsbuf)
578 int ret;
579 unsigned int speed = 0;
580 char newkey[5];
581 u8 buffer[2];
582 struct sensor_device_attribute_2 *sensor_attr =
583 to_sensor_dev_attr_2(attr);
585 newkey[0] = fan_speed_keys[sensor_attr->nr][0];
586 newkey[1] = '0' + sensor_attr->index;
587 newkey[2] = fan_speed_keys[sensor_attr->nr][2];
588 newkey[3] = fan_speed_keys[sensor_attr->nr][3];
589 newkey[4] = 0;
591 mutex_lock(&applesmc_lock);
593 ret = applesmc_read_key(newkey, buffer, 2);
594 speed = ((buffer[0] << 8 | buffer[1]) >> 2);
596 mutex_unlock(&applesmc_lock);
597 if (ret)
598 return ret;
599 else
600 return snprintf(sysfsbuf, PAGE_SIZE, "%u\n", speed);
603 static ssize_t applesmc_store_fan_speed(struct device *dev,
604 struct device_attribute *attr,
605 const char *sysfsbuf, size_t count)
607 int ret;
608 u32 speed;
609 char newkey[5];
610 u8 buffer[2];
611 struct sensor_device_attribute_2 *sensor_attr =
612 to_sensor_dev_attr_2(attr);
614 speed = simple_strtoul(sysfsbuf, NULL, 10);
616 if (speed > 0x4000) /* Bigger than a 14-bit value */
617 return -EINVAL;
619 newkey[0] = fan_speed_keys[sensor_attr->nr][0];
620 newkey[1] = '0' + sensor_attr->index;
621 newkey[2] = fan_speed_keys[sensor_attr->nr][2];
622 newkey[3] = fan_speed_keys[sensor_attr->nr][3];
623 newkey[4] = 0;
625 mutex_lock(&applesmc_lock);
627 buffer[0] = (speed >> 6) & 0xff;
628 buffer[1] = (speed << 2) & 0xff;
629 ret = applesmc_write_key(newkey, buffer, 2);
631 mutex_unlock(&applesmc_lock);
632 if (ret)
633 return ret;
634 else
635 return count;
638 static ssize_t applesmc_show_fan_manual(struct device *dev,
639 struct device_attribute *devattr, char *sysfsbuf)
641 int ret;
642 u16 manual = 0;
643 u8 buffer[2];
644 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
646 mutex_lock(&applesmc_lock);
648 ret = applesmc_read_key(FANS_MANUAL, buffer, 2);
649 manual = ((buffer[0] << 8 | buffer[1]) >> attr->index) & 0x01;
651 mutex_unlock(&applesmc_lock);
652 if (ret)
653 return ret;
654 else
655 return snprintf(sysfsbuf, PAGE_SIZE, "%d\n", manual);
658 static ssize_t applesmc_store_fan_manual(struct device *dev,
659 struct device_attribute *devattr,
660 const char *sysfsbuf, size_t count)
662 int ret;
663 u8 buffer[2];
664 u32 input;
665 u16 val;
666 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
668 input = simple_strtoul(sysfsbuf, NULL, 10);
670 mutex_lock(&applesmc_lock);
672 ret = applesmc_read_key(FANS_MANUAL, buffer, 2);
673 val = (buffer[0] << 8 | buffer[1]);
674 if (ret)
675 goto out;
677 if (input)
678 val = val | (0x01 << attr->index);
679 else
680 val = val & ~(0x01 << attr->index);
682 buffer[0] = (val >> 8) & 0xFF;
683 buffer[1] = val & 0xFF;
685 ret = applesmc_write_key(FANS_MANUAL, buffer, 2);
687 out:
688 mutex_unlock(&applesmc_lock);
689 if (ret)
690 return ret;
691 else
692 return count;
695 static ssize_t applesmc_show_fan_position(struct device *dev,
696 struct device_attribute *attr, char *sysfsbuf)
698 int ret;
699 char newkey[5];
700 u8 buffer[17];
701 struct sensor_device_attribute_2 *sensor_attr =
702 to_sensor_dev_attr_2(attr);
704 newkey[0] = FAN_POSITION[0];
705 newkey[1] = '0' + sensor_attr->index;
706 newkey[2] = FAN_POSITION[2];
707 newkey[3] = FAN_POSITION[3];
708 newkey[4] = 0;
710 mutex_lock(&applesmc_lock);
712 ret = applesmc_read_key(newkey, buffer, 16);
713 buffer[16] = 0;
715 mutex_unlock(&applesmc_lock);
716 if (ret)
717 return ret;
718 else
719 return snprintf(sysfsbuf, PAGE_SIZE, "%s\n", buffer+4);
722 static ssize_t applesmc_calibrate_show(struct device *dev,
723 struct device_attribute *attr, char *sysfsbuf)
725 return snprintf(sysfsbuf, PAGE_SIZE, "(%d,%d)\n", rest_x, rest_y);
728 static ssize_t applesmc_calibrate_store(struct device *dev,
729 struct device_attribute *attr, const char *sysfsbuf, size_t count)
731 mutex_lock(&applesmc_lock);
732 applesmc_calibrate();
733 mutex_unlock(&applesmc_lock);
735 return count;
738 /* Store the next backlight value to be written by the work */
739 static unsigned int backlight_value;
741 static void applesmc_backlight_set(struct work_struct *work)
743 u8 buffer[2];
745 mutex_lock(&applesmc_lock);
746 buffer[0] = backlight_value;
747 buffer[1] = 0x00;
748 applesmc_write_key(BACKLIGHT_KEY, buffer, 2);
749 mutex_unlock(&applesmc_lock);
751 static DECLARE_WORK(backlight_work, &applesmc_backlight_set);
753 static void applesmc_brightness_set(struct led_classdev *led_cdev,
754 enum led_brightness value)
756 int ret;
758 backlight_value = value;
759 ret = queue_work(applesmc_led_wq, &backlight_work);
761 if (debug && (!ret))
762 printk(KERN_DEBUG "applesmc: work was already on the queue.\n");
765 static ssize_t applesmc_key_count_show(struct device *dev,
766 struct device_attribute *attr, char *sysfsbuf)
768 int ret;
769 u8 buffer[4];
770 u32 count;
772 mutex_lock(&applesmc_lock);
774 ret = applesmc_read_key(KEY_COUNT_KEY, buffer, 4);
775 count = ((u32)buffer[0]<<24) + ((u32)buffer[1]<<16) +
776 ((u32)buffer[2]<<8) + buffer[3];
778 mutex_unlock(&applesmc_lock);
779 if (ret)
780 return ret;
781 else
782 return snprintf(sysfsbuf, PAGE_SIZE, "%d\n", count);
785 static ssize_t applesmc_key_at_index_read_show(struct device *dev,
786 struct device_attribute *attr, char *sysfsbuf)
788 char key[5];
789 char info[6];
790 int ret;
792 mutex_lock(&applesmc_lock);
794 ret = applesmc_get_key_at_index(key_at_index, key);
796 if (ret || !key[0]) {
797 mutex_unlock(&applesmc_lock);
799 return -EINVAL;
802 ret = applesmc_get_key_type(key, info);
804 if (ret) {
805 mutex_unlock(&applesmc_lock);
807 return ret;
811 * info[0] maximum value (APPLESMC_MAX_DATA_LENGTH) is much lower than
812 * PAGE_SIZE, so we don't need any checks before writing to sysfsbuf.
814 ret = applesmc_read_key(key, sysfsbuf, info[0]);
816 mutex_unlock(&applesmc_lock);
818 if (!ret) {
819 return info[0];
821 else {
822 return ret;
826 static ssize_t applesmc_key_at_index_data_length_show(struct device *dev,
827 struct device_attribute *attr, char *sysfsbuf)
829 char key[5];
830 char info[6];
831 int ret;
833 mutex_lock(&applesmc_lock);
835 ret = applesmc_get_key_at_index(key_at_index, key);
837 if (ret || !key[0]) {
838 mutex_unlock(&applesmc_lock);
840 return -EINVAL;
843 ret = applesmc_get_key_type(key, info);
845 mutex_unlock(&applesmc_lock);
847 if (!ret)
848 return snprintf(sysfsbuf, PAGE_SIZE, "%d\n", info[0]);
849 else
850 return ret;
853 static ssize_t applesmc_key_at_index_type_show(struct device *dev,
854 struct device_attribute *attr, char *sysfsbuf)
856 char key[5];
857 char info[6];
858 int ret;
860 mutex_lock(&applesmc_lock);
862 ret = applesmc_get_key_at_index(key_at_index, key);
864 if (ret || !key[0]) {
865 mutex_unlock(&applesmc_lock);
867 return -EINVAL;
870 ret = applesmc_get_key_type(key, info);
872 mutex_unlock(&applesmc_lock);
874 if (!ret)
875 return snprintf(sysfsbuf, PAGE_SIZE, "%s\n", info+1);
876 else
877 return ret;
880 static ssize_t applesmc_key_at_index_name_show(struct device *dev,
881 struct device_attribute *attr, char *sysfsbuf)
883 char key[5];
884 int ret;
886 mutex_lock(&applesmc_lock);
888 ret = applesmc_get_key_at_index(key_at_index, key);
890 mutex_unlock(&applesmc_lock);
892 if (!ret && key[0])
893 return snprintf(sysfsbuf, PAGE_SIZE, "%s\n", key);
894 else
895 return -EINVAL;
898 static ssize_t applesmc_key_at_index_show(struct device *dev,
899 struct device_attribute *attr, char *sysfsbuf)
901 return snprintf(sysfsbuf, PAGE_SIZE, "%d\n", key_at_index);
904 static ssize_t applesmc_key_at_index_store(struct device *dev,
905 struct device_attribute *attr, const char *sysfsbuf, size_t count)
907 mutex_lock(&applesmc_lock);
909 key_at_index = simple_strtoul(sysfsbuf, NULL, 10);
911 mutex_unlock(&applesmc_lock);
913 return count;
916 static struct led_classdev applesmc_backlight = {
917 .name = "smc:kbd_backlight",
918 .default_trigger = "nand-disk",
919 .brightness_set = applesmc_brightness_set,
922 static DEVICE_ATTR(name, 0444, applesmc_name_show, NULL);
924 static DEVICE_ATTR(position, 0444, applesmc_position_show, NULL);
925 static DEVICE_ATTR(calibrate, 0644,
926 applesmc_calibrate_show, applesmc_calibrate_store);
928 static struct attribute *accelerometer_attributes[] = {
929 &dev_attr_position.attr,
930 &dev_attr_calibrate.attr,
931 NULL
934 static const struct attribute_group accelerometer_attributes_group =
935 { .attrs = accelerometer_attributes };
937 static DEVICE_ATTR(light, 0444, applesmc_light_show, NULL);
939 static DEVICE_ATTR(key_count, 0444, applesmc_key_count_show, NULL);
940 static DEVICE_ATTR(key_at_index, 0644,
941 applesmc_key_at_index_show, applesmc_key_at_index_store);
942 static DEVICE_ATTR(key_at_index_name, 0444,
943 applesmc_key_at_index_name_show, NULL);
944 static DEVICE_ATTR(key_at_index_type, 0444,
945 applesmc_key_at_index_type_show, NULL);
946 static DEVICE_ATTR(key_at_index_data_length, 0444,
947 applesmc_key_at_index_data_length_show, NULL);
948 static DEVICE_ATTR(key_at_index_data, 0444,
949 applesmc_key_at_index_read_show, NULL);
951 static struct attribute *key_enumeration_attributes[] = {
952 &dev_attr_key_count.attr,
953 &dev_attr_key_at_index.attr,
954 &dev_attr_key_at_index_name.attr,
955 &dev_attr_key_at_index_type.attr,
956 &dev_attr_key_at_index_data_length.attr,
957 &dev_attr_key_at_index_data.attr,
958 NULL
961 static const struct attribute_group key_enumeration_group =
962 { .attrs = key_enumeration_attributes };
965 * Macro defining SENSOR_DEVICE_ATTR for a fan sysfs entries.
966 * - show actual speed
967 * - show/store minimum speed
968 * - show maximum speed
969 * - show safe speed
970 * - show/store target speed
971 * - show/store manual mode
973 #define sysfs_fan_speeds_offset(offset) \
974 static SENSOR_DEVICE_ATTR_2(fan##offset##_input, S_IRUGO, \
975 applesmc_show_fan_speed, NULL, 0, offset-1); \
977 static SENSOR_DEVICE_ATTR_2(fan##offset##_min, S_IRUGO | S_IWUSR, \
978 applesmc_show_fan_speed, applesmc_store_fan_speed, 1, offset-1); \
980 static SENSOR_DEVICE_ATTR_2(fan##offset##_max, S_IRUGO, \
981 applesmc_show_fan_speed, NULL, 2, offset-1); \
983 static SENSOR_DEVICE_ATTR_2(fan##offset##_safe, S_IRUGO, \
984 applesmc_show_fan_speed, NULL, 3, offset-1); \
986 static SENSOR_DEVICE_ATTR_2(fan##offset##_output, S_IRUGO | S_IWUSR, \
987 applesmc_show_fan_speed, applesmc_store_fan_speed, 4, offset-1); \
989 static SENSOR_DEVICE_ATTR(fan##offset##_manual, S_IRUGO | S_IWUSR, \
990 applesmc_show_fan_manual, applesmc_store_fan_manual, offset-1); \
992 static SENSOR_DEVICE_ATTR(fan##offset##_label, S_IRUGO, \
993 applesmc_show_fan_position, NULL, offset-1); \
995 static struct attribute *fan##offset##_attributes[] = { \
996 &sensor_dev_attr_fan##offset##_input.dev_attr.attr, \
997 &sensor_dev_attr_fan##offset##_min.dev_attr.attr, \
998 &sensor_dev_attr_fan##offset##_max.dev_attr.attr, \
999 &sensor_dev_attr_fan##offset##_safe.dev_attr.attr, \
1000 &sensor_dev_attr_fan##offset##_output.dev_attr.attr, \
1001 &sensor_dev_attr_fan##offset##_manual.dev_attr.attr, \
1002 &sensor_dev_attr_fan##offset##_label.dev_attr.attr, \
1003 NULL \
1007 * Create the needed functions for each fan using the macro defined above
1008 * (2 fans are supported)
1010 sysfs_fan_speeds_offset(1);
1011 sysfs_fan_speeds_offset(2);
1013 static const struct attribute_group fan_attribute_groups[] = {
1014 { .attrs = fan1_attributes },
1015 { .attrs = fan2_attributes }
1019 * Temperature sensors sysfs entries.
1021 static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO,
1022 applesmc_show_temperature, NULL, 0);
1023 static SENSOR_DEVICE_ATTR(temp2_input, S_IRUGO,
1024 applesmc_show_temperature, NULL, 1);
1025 static SENSOR_DEVICE_ATTR(temp3_input, S_IRUGO,
1026 applesmc_show_temperature, NULL, 2);
1027 static SENSOR_DEVICE_ATTR(temp4_input, S_IRUGO,
1028 applesmc_show_temperature, NULL, 3);
1029 static SENSOR_DEVICE_ATTR(temp5_input, S_IRUGO,
1030 applesmc_show_temperature, NULL, 4);
1031 static SENSOR_DEVICE_ATTR(temp6_input, S_IRUGO,
1032 applesmc_show_temperature, NULL, 5);
1033 static SENSOR_DEVICE_ATTR(temp7_input, S_IRUGO,
1034 applesmc_show_temperature, NULL, 6);
1035 static SENSOR_DEVICE_ATTR(temp8_input, S_IRUGO,
1036 applesmc_show_temperature, NULL, 7);
1037 static SENSOR_DEVICE_ATTR(temp9_input, S_IRUGO,
1038 applesmc_show_temperature, NULL, 8);
1039 static SENSOR_DEVICE_ATTR(temp10_input, S_IRUGO,
1040 applesmc_show_temperature, NULL, 9);
1041 static SENSOR_DEVICE_ATTR(temp11_input, S_IRUGO,
1042 applesmc_show_temperature, NULL, 10);
1043 static SENSOR_DEVICE_ATTR(temp12_input, S_IRUGO,
1044 applesmc_show_temperature, NULL, 11);
1046 static struct attribute *temperature_attributes[] = {
1047 &sensor_dev_attr_temp1_input.dev_attr.attr,
1048 &sensor_dev_attr_temp2_input.dev_attr.attr,
1049 &sensor_dev_attr_temp3_input.dev_attr.attr,
1050 &sensor_dev_attr_temp4_input.dev_attr.attr,
1051 &sensor_dev_attr_temp5_input.dev_attr.attr,
1052 &sensor_dev_attr_temp6_input.dev_attr.attr,
1053 &sensor_dev_attr_temp7_input.dev_attr.attr,
1054 &sensor_dev_attr_temp8_input.dev_attr.attr,
1055 &sensor_dev_attr_temp9_input.dev_attr.attr,
1056 &sensor_dev_attr_temp10_input.dev_attr.attr,
1057 &sensor_dev_attr_temp11_input.dev_attr.attr,
1058 &sensor_dev_attr_temp12_input.dev_attr.attr,
1059 NULL
1062 static const struct attribute_group temperature_attributes_group =
1063 { .attrs = temperature_attributes };
1065 /* Module stuff */
1068 * applesmc_dmi_match - found a match. return one, short-circuiting the hunt.
1070 static int applesmc_dmi_match(struct dmi_system_id *id)
1072 int i = 0;
1073 struct dmi_match_data* dmi_data = id->driver_data;
1074 printk(KERN_INFO "applesmc: %s detected:\n", id->ident);
1075 applesmc_accelerometer = dmi_data->accelerometer;
1076 printk(KERN_INFO "applesmc: - Model %s accelerometer\n",
1077 applesmc_accelerometer ? "with" : "without");
1078 applesmc_light = dmi_data->light;
1079 printk(KERN_INFO "applesmc: - Model %s light sensors and backlight\n",
1080 applesmc_light ? "with" : "without");
1082 applesmc_temperature_set = dmi_data->temperature_set;
1083 while (temperature_sensors_sets[applesmc_temperature_set][i] != NULL)
1084 i++;
1085 printk(KERN_INFO "applesmc: - Model with %d temperature sensors\n", i);
1086 return 1;
1089 /* Create accelerometer ressources */
1090 static int applesmc_create_accelerometer(void)
1092 int ret;
1094 ret = sysfs_create_group(&pdev->dev.kobj,
1095 &accelerometer_attributes_group);
1096 if (ret)
1097 goto out;
1099 applesmc_idev = input_allocate_device();
1100 if (!applesmc_idev) {
1101 ret = -ENOMEM;
1102 goto out_sysfs;
1105 /* initial calibrate for the input device */
1106 applesmc_calibrate();
1108 /* initialize the input class */
1109 applesmc_idev->name = "applesmc";
1110 applesmc_idev->id.bustype = BUS_HOST;
1111 applesmc_idev->dev.parent = &pdev->dev;
1112 applesmc_idev->evbit[0] = BIT_MASK(EV_ABS);
1113 applesmc_idev->open = applesmc_idev_open;
1114 applesmc_idev->close = applesmc_idev_close;
1115 input_set_abs_params(applesmc_idev, ABS_X,
1116 -256, 256, APPLESMC_INPUT_FUZZ, APPLESMC_INPUT_FLAT);
1117 input_set_abs_params(applesmc_idev, ABS_Y,
1118 -256, 256, APPLESMC_INPUT_FUZZ, APPLESMC_INPUT_FLAT);
1120 ret = input_register_device(applesmc_idev);
1121 if (ret)
1122 goto out_idev;
1124 /* start up our timer for the input device */
1125 init_timer(&applesmc_timer);
1126 applesmc_timer.function = applesmc_idev_poll;
1127 applesmc_timer.expires = jiffies + APPLESMC_POLL_PERIOD;
1129 return 0;
1131 out_idev:
1132 input_free_device(applesmc_idev);
1134 out_sysfs:
1135 sysfs_remove_group(&pdev->dev.kobj, &accelerometer_attributes_group);
1137 out:
1138 printk(KERN_WARNING "applesmc: driver init failed (ret=%d)!\n", ret);
1139 return ret;
1142 /* Release all ressources used by the accelerometer */
1143 static void applesmc_release_accelerometer(void)
1145 del_timer_sync(&applesmc_timer);
1146 input_unregister_device(applesmc_idev);
1147 sysfs_remove_group(&pdev->dev.kobj, &accelerometer_attributes_group);
1150 static __initdata struct dmi_match_data applesmc_dmi_data[] = {
1151 /* MacBook Pro: accelerometer, backlight and temperature set 0 */
1152 { .accelerometer = 1, .light = 1, .temperature_set = 0 },
1153 /* MacBook: accelerometer and temperature set 0 */
1154 { .accelerometer = 1, .light = 0, .temperature_set = 0 },
1155 /* MacBook: temperature set 1 */
1156 { .accelerometer = 0, .light = 0, .temperature_set = 1 }
1159 /* Note that DMI_MATCH(...,"MacBook") will match "MacBookPro1,1".
1160 * So we need to put "Apple MacBook Pro" before "Apple MacBook". */
1161 static __initdata struct dmi_system_id applesmc_whitelist[] = {
1162 { applesmc_dmi_match, "Apple MacBook Pro", {
1163 DMI_MATCH(DMI_BOARD_VENDOR,"Apple"),
1164 DMI_MATCH(DMI_PRODUCT_NAME,"MacBookPro") },
1165 (void*)&applesmc_dmi_data[0]},
1166 { applesmc_dmi_match, "Apple MacBook", {
1167 DMI_MATCH(DMI_BOARD_VENDOR,"Apple"),
1168 DMI_MATCH(DMI_PRODUCT_NAME,"MacBook") },
1169 (void*)&applesmc_dmi_data[1]},
1170 { applesmc_dmi_match, "Apple Macmini", {
1171 DMI_MATCH(DMI_BOARD_VENDOR,"Apple"),
1172 DMI_MATCH(DMI_PRODUCT_NAME,"Macmini") },
1173 (void*)&applesmc_dmi_data[2]},
1174 { .ident = NULL }
1177 static int __init applesmc_init(void)
1179 int ret;
1180 int count;
1181 int i;
1183 mutex_init(&applesmc_lock);
1185 if (!dmi_check_system(applesmc_whitelist)) {
1186 printk(KERN_WARNING "applesmc: supported laptop not found!\n");
1187 ret = -ENODEV;
1188 goto out;
1191 if (!request_region(APPLESMC_DATA_PORT, APPLESMC_NR_PORTS,
1192 "applesmc")) {
1193 ret = -ENXIO;
1194 goto out;
1197 ret = platform_driver_register(&applesmc_driver);
1198 if (ret)
1199 goto out_region;
1201 pdev = platform_device_register_simple("applesmc", APPLESMC_DATA_PORT,
1202 NULL, 0);
1203 if (IS_ERR(pdev)) {
1204 ret = PTR_ERR(pdev);
1205 goto out_driver;
1208 ret = sysfs_create_file(&pdev->dev.kobj, &dev_attr_name.attr);
1209 if (ret)
1210 goto out_device;
1212 /* Create key enumeration sysfs files */
1213 ret = sysfs_create_group(&pdev->dev.kobj, &key_enumeration_group);
1214 if (ret)
1215 goto out_name;
1217 /* create fan files */
1218 count = applesmc_get_fan_count();
1219 if (count < 0) {
1220 printk(KERN_ERR "applesmc: Cannot get the number of fans.\n");
1221 } else {
1222 printk(KERN_INFO "applesmc: %d fans found.\n", count);
1224 switch (count) {
1225 default:
1226 printk(KERN_WARNING "applesmc: More than 2 fans found,"
1227 " but at most 2 fans are supported"
1228 " by the driver.\n");
1229 case 2:
1230 ret = sysfs_create_group(&pdev->dev.kobj,
1231 &fan_attribute_groups[1]);
1232 if (ret)
1233 goto out_key_enumeration;
1234 case 1:
1235 ret = sysfs_create_group(&pdev->dev.kobj,
1236 &fan_attribute_groups[0]);
1237 if (ret)
1238 goto out_fan_1;
1239 case 0:
1244 for (i = 0;
1245 temperature_sensors_sets[applesmc_temperature_set][i] != NULL;
1246 i++) {
1247 if (temperature_attributes[i] == NULL) {
1248 printk(KERN_ERR "applesmc: More temperature sensors "
1249 "in temperature_sensors_sets (at least %i)"
1250 "than available sysfs files in "
1251 "temperature_attributes (%i), please report "
1252 "this bug.\n", i, i-1);
1253 goto out_temperature;
1255 ret = sysfs_create_file(&pdev->dev.kobj,
1256 temperature_attributes[i]);
1257 if (ret)
1258 goto out_temperature;
1261 if (applesmc_accelerometer) {
1262 ret = applesmc_create_accelerometer();
1263 if (ret)
1264 goto out_temperature;
1267 if (applesmc_light) {
1268 /* Add light sensor file */
1269 ret = sysfs_create_file(&pdev->dev.kobj, &dev_attr_light.attr);
1270 if (ret)
1271 goto out_accelerometer;
1273 /* Create the workqueue */
1274 applesmc_led_wq = create_singlethread_workqueue("applesmc-led");
1275 if (!applesmc_led_wq) {
1276 ret = -ENOMEM;
1277 goto out_light_sysfs;
1280 /* register as a led device */
1281 ret = led_classdev_register(&pdev->dev, &applesmc_backlight);
1282 if (ret < 0)
1283 goto out_light_wq;
1286 hwmon_class_dev = hwmon_device_register(&pdev->dev);
1287 if (IS_ERR(hwmon_class_dev)) {
1288 ret = PTR_ERR(hwmon_class_dev);
1289 goto out_light_ledclass;
1292 printk(KERN_INFO "applesmc: driver successfully loaded.\n");
1294 return 0;
1296 out_light_ledclass:
1297 if (applesmc_light)
1298 led_classdev_unregister(&applesmc_backlight);
1299 out_light_wq:
1300 if (applesmc_light)
1301 destroy_workqueue(applesmc_led_wq);
1302 out_light_sysfs:
1303 if (applesmc_light)
1304 sysfs_remove_file(&pdev->dev.kobj, &dev_attr_light.attr);
1305 out_accelerometer:
1306 if (applesmc_accelerometer)
1307 applesmc_release_accelerometer();
1308 out_temperature:
1309 sysfs_remove_group(&pdev->dev.kobj, &temperature_attributes_group);
1310 sysfs_remove_group(&pdev->dev.kobj, &fan_attribute_groups[0]);
1311 out_fan_1:
1312 sysfs_remove_group(&pdev->dev.kobj, &fan_attribute_groups[1]);
1313 out_key_enumeration:
1314 sysfs_remove_group(&pdev->dev.kobj, &key_enumeration_group);
1315 out_name:
1316 sysfs_remove_file(&pdev->dev.kobj, &dev_attr_name.attr);
1317 out_device:
1318 platform_device_unregister(pdev);
1319 out_driver:
1320 platform_driver_unregister(&applesmc_driver);
1321 out_region:
1322 release_region(APPLESMC_DATA_PORT, APPLESMC_NR_PORTS);
1323 out:
1324 printk(KERN_WARNING "applesmc: driver init failed (ret=%d)!\n", ret);
1325 return ret;
1328 static void __exit applesmc_exit(void)
1330 hwmon_device_unregister(hwmon_class_dev);
1331 if (applesmc_light) {
1332 led_classdev_unregister(&applesmc_backlight);
1333 destroy_workqueue(applesmc_led_wq);
1334 sysfs_remove_file(&pdev->dev.kobj, &dev_attr_light.attr);
1336 if (applesmc_accelerometer)
1337 applesmc_release_accelerometer();
1338 sysfs_remove_group(&pdev->dev.kobj, &temperature_attributes_group);
1339 sysfs_remove_group(&pdev->dev.kobj, &fan_attribute_groups[0]);
1340 sysfs_remove_group(&pdev->dev.kobj, &fan_attribute_groups[1]);
1341 sysfs_remove_group(&pdev->dev.kobj, &key_enumeration_group);
1342 sysfs_remove_file(&pdev->dev.kobj, &dev_attr_name.attr);
1343 platform_device_unregister(pdev);
1344 platform_driver_unregister(&applesmc_driver);
1345 release_region(APPLESMC_DATA_PORT, APPLESMC_NR_PORTS);
1347 printk(KERN_INFO "applesmc: driver unloaded.\n");
1350 module_init(applesmc_init);
1351 module_exit(applesmc_exit);
1353 MODULE_AUTHOR("Nicolas Boichat");
1354 MODULE_DESCRIPTION("Apple SMC");
1355 MODULE_LICENSE("GPL v2");