2 * drivers/hwmon/applesmc.c - driver for Apple's SMC (accelerometer, temperature
3 * sensors, fan control, keyboard backlight control) used in Intel-based Apple
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
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>
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[] = {
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
110 /* Structure to be passed to DMI_MATCH function */
111 struct dmi_match_data {
112 /* Indicates whether this computer has an accelerometer. */
114 /* Indicates whether this computer has light sensors and keyboard backlight. */
116 /* Indicates which temperature sensors set to use. */
120 static const int debug;
121 static struct platform_device *pdev;
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)
156 val = val & APPLESMC_STATUS_MASK;
158 for (i = 0; i < 200; i++) {
159 if ((inb(APPLESMC_CMD_PORT) & APPLESMC_STATUS_MASK) == val) {
162 "Waited %d us for status %x\n",
169 printk(KERN_WARNING "applesmc: wait status failed: %x != %x\n",
170 val, inb(APPLESMC_CMD_PORT));
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)
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);
190 outb(APPLESMC_READ_CMD, APPLESMC_CMD_PORT);
191 if (__wait_status(0x0c))
194 for (i = 0; i < 4; i++) {
195 outb(key[i], APPLESMC_DATA_PORT);
196 if (__wait_status(0x04))
200 printk(KERN_DEBUG "<%s", key);
202 outb(len, APPLESMC_DATA_PORT);
204 printk(KERN_DEBUG ">%x", len);
206 for (i = 0; i < len; i++) {
207 if (__wait_status(0x05))
209 buffer[i] = inb(APPLESMC_DATA_PORT);
211 printk(KERN_DEBUG "<%x", buffer[i]);
214 printk(KERN_DEBUG "\n");
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)
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);
234 outb(APPLESMC_WRITE_CMD, APPLESMC_CMD_PORT);
235 if (__wait_status(0x0c))
238 for (i = 0; i < 4; i++) {
239 outb(key[i], APPLESMC_DATA_PORT);
240 if (__wait_status(0x04))
244 outb(len, APPLESMC_DATA_PORT);
246 for (i = 0; i < len; i++) {
247 if (__wait_status(0x04))
249 outb(buffer[i], APPLESMC_DATA_PORT);
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)
264 readkey[0] = index >> 24;
265 readkey[1] = index >> 16;
266 readkey[2] = index >> 8;
269 outb(APPLESMC_GET_KEY_BY_INDEX_CMD, APPLESMC_CMD_PORT);
270 if (__wait_status(0x0c))
273 for (i = 0; i < 4; i++) {
274 outb(readkey[i], APPLESMC_DATA_PORT);
275 if (__wait_status(0x04))
279 outb(4, APPLESMC_DATA_PORT);
281 for (i = 0; i < 4; i++) {
282 if (__wait_status(0x05))
284 key[i] = inb(APPLESMC_DATA_PORT);
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)
300 outb(APPLESMC_GET_KEY_TYPE_CMD, APPLESMC_CMD_PORT);
301 if (__wait_status(0x0c))
304 for (i = 0; i < 4; i++) {
305 outb(key[i], APPLESMC_DATA_PORT);
306 if (__wait_status(0x04))
310 outb(5, APPLESMC_DATA_PORT);
312 for (i = 0; i < 6; i++) {
313 if (__wait_status(0x05))
315 type[i] = inb(APPLESMC_DATA_PORT);
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)
333 ret = applesmc_read_key(MOTION_SENSOR_X_KEY, buffer, 2);
336 ret = applesmc_read_key(MOTION_SENSOR_Y_KEY, buffer, 2);
339 ret = applesmc_read_key(MOTION_SENSOR_Z_KEY, buffer, 2);
345 *value = ((s16)buffer[0] << 8) | buffer[1];
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;
359 if (!applesmc_accelerometer)
362 mutex_lock(&applesmc_lock);
364 for (total = INIT_TIMEOUT_MSECS; total > 0; total -= INIT_WAIT_MSECS) {
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]);
375 printk(KERN_DEBUG "applesmc: device"
376 " successfully initialized"
377 " (0x%02x, 0x%02x).\n",
378 buffer[0], buffer[1]);
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");
392 mutex_unlock(&applesmc_lock);
397 * applesmc_get_fan_count - get the number of fans. Callers must NOT hold
400 static int applesmc_get_fan_count(void)
405 mutex_lock(&applesmc_lock);
407 ret = applesmc_read_key(FANS_COUNT, buffer, 1);
409 mutex_unlock(&applesmc_lock);
416 /* Device model stuff */
417 static int applesmc_probe(struct platform_device *dev)
421 ret = applesmc_device_init();
425 printk(KERN_INFO "applesmc: device successfully initialized.\n");
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,
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);
454 static int applesmc_idev_open(struct input_dev *dev)
456 add_timer(&applesmc_timer);
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)
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);
476 if (applesmc_read_motion_sensor(SENSOR_X, &x))
478 if (applesmc_read_motion_sensor(SENSOR_Y, &y))
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);
487 mod_timer(&applesmc_timer, jiffies + APPLESMC_POLL_PERIOD);
489 mutex_unlock(&applesmc_lock);
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)
506 mutex_lock(&applesmc_lock);
508 ret = applesmc_read_motion_sensor(SENSOR_X, &x);
511 ret = applesmc_read_motion_sensor(SENSOR_Y, &y);
514 ret = applesmc_read_motion_sensor(SENSOR_Z, &z);
519 mutex_unlock(&applesmc_lock);
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)
530 u8 left = 0, right = 0;
533 mutex_lock(&applesmc_lock);
535 ret = applesmc_read_key(LIGHT_SENSOR_LEFT_KEY, buffer, 6);
539 ret = applesmc_read_key(LIGHT_SENSOR_RIGHT_KEY, buffer, 6);
543 mutex_unlock(&applesmc_lock);
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)
557 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
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);
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)
579 unsigned int speed = 0;
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];
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);
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)
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 */
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];
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);
638 static ssize_t applesmc_show_fan_manual(struct device *dev,
639 struct device_attribute *devattr, char *sysfsbuf)
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);
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)
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]);
678 val = val | (0x01 << attr->index);
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);
688 mutex_unlock(&applesmc_lock);
695 static ssize_t applesmc_show_fan_position(struct device *dev,
696 struct device_attribute *attr, char *sysfsbuf)
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];
710 mutex_lock(&applesmc_lock);
712 ret = applesmc_read_key(newkey, buffer, 16);
715 mutex_unlock(&applesmc_lock);
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);
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)
745 mutex_lock(&applesmc_lock);
746 buffer[0] = backlight_value;
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)
758 backlight_value = value;
759 ret = queue_work(applesmc_led_wq, &backlight_work);
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)
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);
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)
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);
802 ret = applesmc_get_key_type(key, info);
805 mutex_unlock(&applesmc_lock);
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);
826 static ssize_t applesmc_key_at_index_data_length_show(struct device *dev,
827 struct device_attribute *attr, char *sysfsbuf)
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);
843 ret = applesmc_get_key_type(key, info);
845 mutex_unlock(&applesmc_lock);
848 return snprintf(sysfsbuf, PAGE_SIZE, "%d\n", info[0]);
853 static ssize_t applesmc_key_at_index_type_show(struct device *dev,
854 struct device_attribute *attr, char *sysfsbuf)
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);
870 ret = applesmc_get_key_type(key, info);
872 mutex_unlock(&applesmc_lock);
875 return snprintf(sysfsbuf, PAGE_SIZE, "%s\n", info+1);
880 static ssize_t applesmc_key_at_index_name_show(struct device *dev,
881 struct device_attribute *attr, char *sysfsbuf)
886 mutex_lock(&applesmc_lock);
888 ret = applesmc_get_key_at_index(key_at_index, key);
890 mutex_unlock(&applesmc_lock);
893 return snprintf(sysfsbuf, PAGE_SIZE, "%s\n", key);
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);
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,
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,
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
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, \
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,
1062 static const struct attribute_group temperature_attributes_group =
1063 { .attrs = temperature_attributes };
1068 * applesmc_dmi_match - found a match. return one, short-circuiting the hunt.
1070 static int applesmc_dmi_match(struct dmi_system_id *id)
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)
1085 printk(KERN_INFO "applesmc: - Model with %d temperature sensors\n", i);
1089 /* Create accelerometer ressources */
1090 static int applesmc_create_accelerometer(void)
1094 ret = sysfs_create_group(&pdev->dev.kobj,
1095 &accelerometer_attributes_group);
1099 applesmc_idev = input_allocate_device();
1100 if (!applesmc_idev) {
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(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);
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;
1132 input_free_device(applesmc_idev);
1135 sysfs_remove_group(&pdev->dev.kobj, &accelerometer_attributes_group);
1138 printk(KERN_WARNING "applesmc: driver init failed (ret=%d)!\n", 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]},
1177 static int __init applesmc_init(void)
1183 mutex_init(&applesmc_lock);
1185 if (!dmi_check_system(applesmc_whitelist)) {
1186 printk(KERN_WARNING "applesmc: supported laptop not found!\n");
1191 if (!request_region(APPLESMC_DATA_PORT, APPLESMC_NR_PORTS,
1197 ret = platform_driver_register(&applesmc_driver);
1201 pdev = platform_device_register_simple("applesmc", APPLESMC_DATA_PORT,
1204 ret = PTR_ERR(pdev);
1208 ret = sysfs_create_file(&pdev->dev.kobj, &dev_attr_name.attr);
1210 /* Create key enumeration sysfs files */
1211 ret = sysfs_create_group(&pdev->dev.kobj, &key_enumeration_group);
1215 /* create fan files */
1216 count = applesmc_get_fan_count();
1218 printk(KERN_ERR "applesmc: Cannot get the number of fans.\n");
1220 printk(KERN_INFO "applesmc: %d fans found.\n", count);
1224 printk(KERN_WARNING "applesmc: More than 2 fans found,"
1225 " but at most 2 fans are supported"
1226 " by the driver.\n");
1228 ret = sysfs_create_group(&pdev->dev.kobj,
1229 &fan_attribute_groups[1]);
1231 goto out_key_enumeration;
1233 ret = sysfs_create_group(&pdev->dev.kobj,
1234 &fan_attribute_groups[0]);
1243 temperature_sensors_sets[applesmc_temperature_set][i] != NULL;
1245 if (temperature_attributes[i] == NULL) {
1246 printk(KERN_ERR "applesmc: More temperature sensors "
1247 "in temperature_sensors_sets (at least %i)"
1248 "than available sysfs files in "
1249 "temperature_attributes (%i), please report "
1250 "this bug.\n", i, i-1);
1251 goto out_temperature;
1253 ret = sysfs_create_file(&pdev->dev.kobj,
1254 temperature_attributes[i]);
1256 goto out_temperature;
1259 if (applesmc_accelerometer) {
1260 ret = applesmc_create_accelerometer();
1262 goto out_temperature;
1265 if (applesmc_light) {
1266 /* Add light sensor file */
1267 ret = sysfs_create_file(&pdev->dev.kobj, &dev_attr_light.attr);
1269 goto out_accelerometer;
1271 /* Create the workqueue */
1272 applesmc_led_wq = create_singlethread_workqueue("applesmc-led");
1273 if (!applesmc_led_wq) {
1275 goto out_light_sysfs;
1278 /* register as a led device */
1279 ret = led_classdev_register(&pdev->dev, &applesmc_backlight);
1284 hwmon_class_dev = hwmon_device_register(&pdev->dev);
1285 if (IS_ERR(hwmon_class_dev)) {
1286 ret = PTR_ERR(hwmon_class_dev);
1287 goto out_light_ledclass;
1290 printk(KERN_INFO "applesmc: driver successfully loaded.\n");
1296 led_classdev_unregister(&applesmc_backlight);
1299 destroy_workqueue(applesmc_led_wq);
1302 sysfs_remove_file(&pdev->dev.kobj, &dev_attr_light.attr);
1304 if (applesmc_accelerometer)
1305 applesmc_release_accelerometer();
1307 sysfs_remove_group(&pdev->dev.kobj, &temperature_attributes_group);
1308 sysfs_remove_group(&pdev->dev.kobj, &fan_attribute_groups[0]);
1310 sysfs_remove_group(&pdev->dev.kobj, &fan_attribute_groups[1]);
1311 out_key_enumeration:
1312 sysfs_remove_group(&pdev->dev.kobj, &key_enumeration_group);
1314 platform_device_unregister(pdev);
1316 platform_driver_unregister(&applesmc_driver);
1318 release_region(APPLESMC_DATA_PORT, APPLESMC_NR_PORTS);
1320 printk(KERN_WARNING "applesmc: driver init failed (ret=%d)!\n", ret);
1324 static void __exit applesmc_exit(void)
1326 hwmon_device_unregister(hwmon_class_dev);
1327 if (applesmc_light) {
1328 led_classdev_unregister(&applesmc_backlight);
1329 destroy_workqueue(applesmc_led_wq);
1330 sysfs_remove_file(&pdev->dev.kobj, &dev_attr_light.attr);
1332 if (applesmc_accelerometer)
1333 applesmc_release_accelerometer();
1334 sysfs_remove_group(&pdev->dev.kobj, &temperature_attributes_group);
1335 sysfs_remove_group(&pdev->dev.kobj, &fan_attribute_groups[0]);
1336 sysfs_remove_group(&pdev->dev.kobj, &fan_attribute_groups[1]);
1337 sysfs_remove_group(&pdev->dev.kobj, &key_enumeration_group);
1338 platform_device_unregister(pdev);
1339 platform_driver_unregister(&applesmc_driver);
1340 release_region(APPLESMC_DATA_PORT, APPLESMC_NR_PORTS);
1342 printk(KERN_INFO "applesmc: driver unloaded.\n");
1345 module_init(applesmc_init);
1346 module_exit(applesmc_exit);
1348 MODULE_AUTHOR("Nicolas Boichat");
1349 MODULE_DESCRIPTION("Apple SMC");
1350 MODULE_LICENSE("GPL v2");