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-polldev.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_INTERVAL 50 /* msecs */
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 input_polled_dev *applesmc_idev;
125 static struct class_device *hwmon_class_dev;
127 /* Indicates whether this computer has an accelerometer. */
128 static unsigned int applesmc_accelerometer;
130 /* Indicates whether this computer has light sensors and keyboard backlight. */
131 static unsigned int applesmc_light;
133 /* Indicates which temperature sensors set to use. */
134 static unsigned int applesmc_temperature_set;
136 static DEFINE_MUTEX(applesmc_lock);
139 * Last index written to key_at_index sysfs file, and value to use for all other
140 * key_at_index_* sysfs files.
142 static unsigned int key_at_index;
144 static struct workqueue_struct *applesmc_led_wq;
147 * __wait_status - Wait up to 2ms for the status port to get a certain value
148 * (masked with 0x0f), returning zero if the value is obtained. Callers must
149 * hold applesmc_lock.
151 static int __wait_status(u8 val)
155 val = val & APPLESMC_STATUS_MASK;
157 for (i = 0; i < 200; i++) {
158 if ((inb(APPLESMC_CMD_PORT) & APPLESMC_STATUS_MASK) == val) {
161 "Waited %d us for status %x\n",
168 printk(KERN_WARNING "applesmc: wait status failed: %x != %x\n",
169 val, inb(APPLESMC_CMD_PORT));
175 * applesmc_read_key - reads len bytes from a given key, and put them in buffer.
176 * Returns zero on success or a negative error on failure. Callers must
177 * hold applesmc_lock.
179 static int applesmc_read_key(const char* key, u8* buffer, u8 len)
183 if (len > APPLESMC_MAX_DATA_LENGTH) {
184 printk(KERN_ERR "applesmc_read_key: cannot read more than "
185 "%d bytes\n", APPLESMC_MAX_DATA_LENGTH);
189 outb(APPLESMC_READ_CMD, APPLESMC_CMD_PORT);
190 if (__wait_status(0x0c))
193 for (i = 0; i < 4; i++) {
194 outb(key[i], APPLESMC_DATA_PORT);
195 if (__wait_status(0x04))
199 printk(KERN_DEBUG "<%s", key);
201 outb(len, APPLESMC_DATA_PORT);
203 printk(KERN_DEBUG ">%x", len);
205 for (i = 0; i < len; i++) {
206 if (__wait_status(0x05))
208 buffer[i] = inb(APPLESMC_DATA_PORT);
210 printk(KERN_DEBUG "<%x", buffer[i]);
213 printk(KERN_DEBUG "\n");
219 * applesmc_write_key - writes len bytes from buffer to a given key.
220 * Returns zero on success or a negative error on failure. Callers must
221 * hold applesmc_lock.
223 static int applesmc_write_key(const char* key, u8* buffer, u8 len)
227 if (len > APPLESMC_MAX_DATA_LENGTH) {
228 printk(KERN_ERR "applesmc_write_key: cannot write more than "
229 "%d bytes\n", APPLESMC_MAX_DATA_LENGTH);
233 outb(APPLESMC_WRITE_CMD, APPLESMC_CMD_PORT);
234 if (__wait_status(0x0c))
237 for (i = 0; i < 4; i++) {
238 outb(key[i], APPLESMC_DATA_PORT);
239 if (__wait_status(0x04))
243 outb(len, APPLESMC_DATA_PORT);
245 for (i = 0; i < len; i++) {
246 if (__wait_status(0x04))
248 outb(buffer[i], APPLESMC_DATA_PORT);
255 * applesmc_get_key_at_index - get key at index, and put the result in key
256 * (char[6]). Returns zero on success or a negative error on failure. Callers
257 * must hold applesmc_lock.
259 static int applesmc_get_key_at_index(int index, char* key)
263 readkey[0] = index >> 24;
264 readkey[1] = index >> 16;
265 readkey[2] = index >> 8;
268 outb(APPLESMC_GET_KEY_BY_INDEX_CMD, APPLESMC_CMD_PORT);
269 if (__wait_status(0x0c))
272 for (i = 0; i < 4; i++) {
273 outb(readkey[i], APPLESMC_DATA_PORT);
274 if (__wait_status(0x04))
278 outb(4, APPLESMC_DATA_PORT);
280 for (i = 0; i < 4; i++) {
281 if (__wait_status(0x05))
283 key[i] = inb(APPLESMC_DATA_PORT);
291 * applesmc_get_key_type - get key type, and put the result in type (char[6]).
292 * Returns zero on success or a negative error on failure. Callers must
293 * hold applesmc_lock.
295 static int applesmc_get_key_type(char* key, char* type)
299 outb(APPLESMC_GET_KEY_TYPE_CMD, APPLESMC_CMD_PORT);
300 if (__wait_status(0x0c))
303 for (i = 0; i < 4; i++) {
304 outb(key[i], APPLESMC_DATA_PORT);
305 if (__wait_status(0x04))
309 outb(5, APPLESMC_DATA_PORT);
311 for (i = 0; i < 6; i++) {
312 if (__wait_status(0x05))
314 type[i] = inb(APPLESMC_DATA_PORT);
322 * applesmc_read_motion_sensor - Read motion sensor (X, Y or Z). Callers must
323 * hold applesmc_lock.
325 static int applesmc_read_motion_sensor(int index, s16* value)
332 ret = applesmc_read_key(MOTION_SENSOR_X_KEY, buffer, 2);
335 ret = applesmc_read_key(MOTION_SENSOR_Y_KEY, buffer, 2);
338 ret = applesmc_read_key(MOTION_SENSOR_Z_KEY, buffer, 2);
344 *value = ((s16)buffer[0] << 8) | buffer[1];
350 * applesmc_device_init - initialize the accelerometer. Returns zero on success
351 * and negative error code on failure. Can sleep.
353 static int applesmc_device_init(void)
355 int total, ret = -ENXIO;
358 if (!applesmc_accelerometer)
361 mutex_lock(&applesmc_lock);
363 for (total = INIT_TIMEOUT_MSECS; total > 0; total -= INIT_WAIT_MSECS) {
365 printk(KERN_DEBUG "applesmc try %d\n", total);
366 if (!applesmc_read_key(MOTION_SENSOR_KEY, buffer, 2) &&
367 (buffer[0] != 0x00 || buffer[1] != 0x00)) {
368 if (total == INIT_TIMEOUT_MSECS) {
369 printk(KERN_DEBUG "applesmc: device has"
370 " already been initialized"
371 " (0x%02x, 0x%02x).\n",
372 buffer[0], buffer[1]);
374 printk(KERN_DEBUG "applesmc: device"
375 " successfully initialized"
376 " (0x%02x, 0x%02x).\n",
377 buffer[0], buffer[1]);
384 applesmc_write_key(MOTION_SENSOR_KEY, buffer, 2);
385 msleep(INIT_WAIT_MSECS);
388 printk(KERN_WARNING "applesmc: failed to init the device\n");
391 mutex_unlock(&applesmc_lock);
396 * applesmc_get_fan_count - get the number of fans. Callers must NOT hold
399 static int applesmc_get_fan_count(void)
404 mutex_lock(&applesmc_lock);
406 ret = applesmc_read_key(FANS_COUNT, buffer, 1);
408 mutex_unlock(&applesmc_lock);
415 /* Device model stuff */
416 static int applesmc_probe(struct platform_device *dev)
420 ret = applesmc_device_init();
424 printk(KERN_INFO "applesmc: device successfully initialized.\n");
428 static int applesmc_resume(struct platform_device *dev)
430 return applesmc_device_init();
433 static struct platform_driver applesmc_driver = {
434 .probe = applesmc_probe,
435 .resume = applesmc_resume,
438 .owner = THIS_MODULE,
443 * applesmc_calibrate - Set our "resting" values. Callers must
444 * hold applesmc_lock.
446 static void applesmc_calibrate(void)
448 applesmc_read_motion_sensor(SENSOR_X, &rest_x);
449 applesmc_read_motion_sensor(SENSOR_Y, &rest_y);
453 static void applesmc_idev_poll(struct input_polled_dev *dev)
455 struct input_dev *idev = dev->input;
458 mutex_lock(&applesmc_lock);
460 if (applesmc_read_motion_sensor(SENSOR_X, &x))
462 if (applesmc_read_motion_sensor(SENSOR_Y, &y))
466 input_report_abs(idev, ABS_X, x - rest_x);
467 input_report_abs(idev, ABS_Y, y - rest_y);
471 mutex_unlock(&applesmc_lock);
476 static ssize_t applesmc_name_show(struct device *dev,
477 struct device_attribute *attr, char *buf)
479 return snprintf(buf, PAGE_SIZE, "applesmc\n");
482 static ssize_t applesmc_position_show(struct device *dev,
483 struct device_attribute *attr, char *buf)
488 mutex_lock(&applesmc_lock);
490 ret = applesmc_read_motion_sensor(SENSOR_X, &x);
493 ret = applesmc_read_motion_sensor(SENSOR_Y, &y);
496 ret = applesmc_read_motion_sensor(SENSOR_Z, &z);
501 mutex_unlock(&applesmc_lock);
505 return snprintf(buf, PAGE_SIZE, "(%d,%d,%d)\n", x, y, z);
508 static ssize_t applesmc_light_show(struct device *dev,
509 struct device_attribute *attr, char *sysfsbuf)
512 u8 left = 0, right = 0;
515 mutex_lock(&applesmc_lock);
517 ret = applesmc_read_key(LIGHT_SENSOR_LEFT_KEY, buffer, 6);
521 ret = applesmc_read_key(LIGHT_SENSOR_RIGHT_KEY, buffer, 6);
525 mutex_unlock(&applesmc_lock);
529 return snprintf(sysfsbuf, PAGE_SIZE, "(%d,%d)\n", left, right);
532 /* Displays degree Celsius * 1000 */
533 static ssize_t applesmc_show_temperature(struct device *dev,
534 struct device_attribute *devattr, char *sysfsbuf)
539 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
541 temperature_sensors_sets[applesmc_temperature_set][attr->index];
543 mutex_lock(&applesmc_lock);
545 ret = applesmc_read_key(key, buffer, 2);
546 temp = buffer[0]*1000;
547 temp += (buffer[1] >> 6) * 250;
549 mutex_unlock(&applesmc_lock);
554 return snprintf(sysfsbuf, PAGE_SIZE, "%u\n", temp);
557 static ssize_t applesmc_show_fan_speed(struct device *dev,
558 struct device_attribute *attr, char *sysfsbuf)
561 unsigned int speed = 0;
564 struct sensor_device_attribute_2 *sensor_attr =
565 to_sensor_dev_attr_2(attr);
567 newkey[0] = fan_speed_keys[sensor_attr->nr][0];
568 newkey[1] = '0' + sensor_attr->index;
569 newkey[2] = fan_speed_keys[sensor_attr->nr][2];
570 newkey[3] = fan_speed_keys[sensor_attr->nr][3];
573 mutex_lock(&applesmc_lock);
575 ret = applesmc_read_key(newkey, buffer, 2);
576 speed = ((buffer[0] << 8 | buffer[1]) >> 2);
578 mutex_unlock(&applesmc_lock);
582 return snprintf(sysfsbuf, PAGE_SIZE, "%u\n", speed);
585 static ssize_t applesmc_store_fan_speed(struct device *dev,
586 struct device_attribute *attr,
587 const char *sysfsbuf, size_t count)
593 struct sensor_device_attribute_2 *sensor_attr =
594 to_sensor_dev_attr_2(attr);
596 speed = simple_strtoul(sysfsbuf, NULL, 10);
598 if (speed > 0x4000) /* Bigger than a 14-bit value */
601 newkey[0] = fan_speed_keys[sensor_attr->nr][0];
602 newkey[1] = '0' + sensor_attr->index;
603 newkey[2] = fan_speed_keys[sensor_attr->nr][2];
604 newkey[3] = fan_speed_keys[sensor_attr->nr][3];
607 mutex_lock(&applesmc_lock);
609 buffer[0] = (speed >> 6) & 0xff;
610 buffer[1] = (speed << 2) & 0xff;
611 ret = applesmc_write_key(newkey, buffer, 2);
613 mutex_unlock(&applesmc_lock);
620 static ssize_t applesmc_show_fan_manual(struct device *dev,
621 struct device_attribute *devattr, char *sysfsbuf)
626 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
628 mutex_lock(&applesmc_lock);
630 ret = applesmc_read_key(FANS_MANUAL, buffer, 2);
631 manual = ((buffer[0] << 8 | buffer[1]) >> attr->index) & 0x01;
633 mutex_unlock(&applesmc_lock);
637 return snprintf(sysfsbuf, PAGE_SIZE, "%d\n", manual);
640 static ssize_t applesmc_store_fan_manual(struct device *dev,
641 struct device_attribute *devattr,
642 const char *sysfsbuf, size_t count)
648 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
650 input = simple_strtoul(sysfsbuf, NULL, 10);
652 mutex_lock(&applesmc_lock);
654 ret = applesmc_read_key(FANS_MANUAL, buffer, 2);
655 val = (buffer[0] << 8 | buffer[1]);
660 val = val | (0x01 << attr->index);
662 val = val & ~(0x01 << attr->index);
664 buffer[0] = (val >> 8) & 0xFF;
665 buffer[1] = val & 0xFF;
667 ret = applesmc_write_key(FANS_MANUAL, buffer, 2);
670 mutex_unlock(&applesmc_lock);
677 static ssize_t applesmc_show_fan_position(struct device *dev,
678 struct device_attribute *attr, char *sysfsbuf)
683 struct sensor_device_attribute_2 *sensor_attr =
684 to_sensor_dev_attr_2(attr);
686 newkey[0] = FAN_POSITION[0];
687 newkey[1] = '0' + sensor_attr->index;
688 newkey[2] = FAN_POSITION[2];
689 newkey[3] = FAN_POSITION[3];
692 mutex_lock(&applesmc_lock);
694 ret = applesmc_read_key(newkey, buffer, 16);
697 mutex_unlock(&applesmc_lock);
701 return snprintf(sysfsbuf, PAGE_SIZE, "%s\n", buffer+4);
704 static ssize_t applesmc_calibrate_show(struct device *dev,
705 struct device_attribute *attr, char *sysfsbuf)
707 return snprintf(sysfsbuf, PAGE_SIZE, "(%d,%d)\n", rest_x, rest_y);
710 static ssize_t applesmc_calibrate_store(struct device *dev,
711 struct device_attribute *attr, const char *sysfsbuf, size_t count)
713 mutex_lock(&applesmc_lock);
714 applesmc_calibrate();
715 mutex_unlock(&applesmc_lock);
720 /* Store the next backlight value to be written by the work */
721 static unsigned int backlight_value;
723 static void applesmc_backlight_set(struct work_struct *work)
727 mutex_lock(&applesmc_lock);
728 buffer[0] = backlight_value;
730 applesmc_write_key(BACKLIGHT_KEY, buffer, 2);
731 mutex_unlock(&applesmc_lock);
733 static DECLARE_WORK(backlight_work, &applesmc_backlight_set);
735 static void applesmc_brightness_set(struct led_classdev *led_cdev,
736 enum led_brightness value)
740 backlight_value = value;
741 ret = queue_work(applesmc_led_wq, &backlight_work);
744 printk(KERN_DEBUG "applesmc: work was already on the queue.\n");
747 static ssize_t applesmc_key_count_show(struct device *dev,
748 struct device_attribute *attr, char *sysfsbuf)
754 mutex_lock(&applesmc_lock);
756 ret = applesmc_read_key(KEY_COUNT_KEY, buffer, 4);
757 count = ((u32)buffer[0]<<24) + ((u32)buffer[1]<<16) +
758 ((u32)buffer[2]<<8) + buffer[3];
760 mutex_unlock(&applesmc_lock);
764 return snprintf(sysfsbuf, PAGE_SIZE, "%d\n", count);
767 static ssize_t applesmc_key_at_index_read_show(struct device *dev,
768 struct device_attribute *attr, char *sysfsbuf)
774 mutex_lock(&applesmc_lock);
776 ret = applesmc_get_key_at_index(key_at_index, key);
778 if (ret || !key[0]) {
779 mutex_unlock(&applesmc_lock);
784 ret = applesmc_get_key_type(key, info);
787 mutex_unlock(&applesmc_lock);
793 * info[0] maximum value (APPLESMC_MAX_DATA_LENGTH) is much lower than
794 * PAGE_SIZE, so we don't need any checks before writing to sysfsbuf.
796 ret = applesmc_read_key(key, sysfsbuf, info[0]);
798 mutex_unlock(&applesmc_lock);
807 static ssize_t applesmc_key_at_index_data_length_show(struct device *dev,
808 struct device_attribute *attr, char *sysfsbuf)
814 mutex_lock(&applesmc_lock);
816 ret = applesmc_get_key_at_index(key_at_index, key);
818 if (ret || !key[0]) {
819 mutex_unlock(&applesmc_lock);
824 ret = applesmc_get_key_type(key, info);
826 mutex_unlock(&applesmc_lock);
829 return snprintf(sysfsbuf, PAGE_SIZE, "%d\n", info[0]);
834 static ssize_t applesmc_key_at_index_type_show(struct device *dev,
835 struct device_attribute *attr, char *sysfsbuf)
841 mutex_lock(&applesmc_lock);
843 ret = applesmc_get_key_at_index(key_at_index, key);
845 if (ret || !key[0]) {
846 mutex_unlock(&applesmc_lock);
851 ret = applesmc_get_key_type(key, info);
853 mutex_unlock(&applesmc_lock);
856 return snprintf(sysfsbuf, PAGE_SIZE, "%s\n", info+1);
861 static ssize_t applesmc_key_at_index_name_show(struct device *dev,
862 struct device_attribute *attr, char *sysfsbuf)
867 mutex_lock(&applesmc_lock);
869 ret = applesmc_get_key_at_index(key_at_index, key);
871 mutex_unlock(&applesmc_lock);
874 return snprintf(sysfsbuf, PAGE_SIZE, "%s\n", key);
879 static ssize_t applesmc_key_at_index_show(struct device *dev,
880 struct device_attribute *attr, char *sysfsbuf)
882 return snprintf(sysfsbuf, PAGE_SIZE, "%d\n", key_at_index);
885 static ssize_t applesmc_key_at_index_store(struct device *dev,
886 struct device_attribute *attr, const char *sysfsbuf, size_t count)
888 mutex_lock(&applesmc_lock);
890 key_at_index = simple_strtoul(sysfsbuf, NULL, 10);
892 mutex_unlock(&applesmc_lock);
897 static struct led_classdev applesmc_backlight = {
898 .name = "smc:kbd_backlight",
899 .default_trigger = "nand-disk",
900 .brightness_set = applesmc_brightness_set,
903 static DEVICE_ATTR(name, 0444, applesmc_name_show, NULL);
905 static DEVICE_ATTR(position, 0444, applesmc_position_show, NULL);
906 static DEVICE_ATTR(calibrate, 0644,
907 applesmc_calibrate_show, applesmc_calibrate_store);
909 static struct attribute *accelerometer_attributes[] = {
910 &dev_attr_position.attr,
911 &dev_attr_calibrate.attr,
915 static const struct attribute_group accelerometer_attributes_group =
916 { .attrs = accelerometer_attributes };
918 static DEVICE_ATTR(light, 0444, applesmc_light_show, NULL);
920 static DEVICE_ATTR(key_count, 0444, applesmc_key_count_show, NULL);
921 static DEVICE_ATTR(key_at_index, 0644,
922 applesmc_key_at_index_show, applesmc_key_at_index_store);
923 static DEVICE_ATTR(key_at_index_name, 0444,
924 applesmc_key_at_index_name_show, NULL);
925 static DEVICE_ATTR(key_at_index_type, 0444,
926 applesmc_key_at_index_type_show, NULL);
927 static DEVICE_ATTR(key_at_index_data_length, 0444,
928 applesmc_key_at_index_data_length_show, NULL);
929 static DEVICE_ATTR(key_at_index_data, 0444,
930 applesmc_key_at_index_read_show, NULL);
932 static struct attribute *key_enumeration_attributes[] = {
933 &dev_attr_key_count.attr,
934 &dev_attr_key_at_index.attr,
935 &dev_attr_key_at_index_name.attr,
936 &dev_attr_key_at_index_type.attr,
937 &dev_attr_key_at_index_data_length.attr,
938 &dev_attr_key_at_index_data.attr,
942 static const struct attribute_group key_enumeration_group =
943 { .attrs = key_enumeration_attributes };
946 * Macro defining SENSOR_DEVICE_ATTR for a fan sysfs entries.
947 * - show actual speed
948 * - show/store minimum speed
949 * - show maximum speed
951 * - show/store target speed
952 * - show/store manual mode
954 #define sysfs_fan_speeds_offset(offset) \
955 static SENSOR_DEVICE_ATTR_2(fan##offset##_input, S_IRUGO, \
956 applesmc_show_fan_speed, NULL, 0, offset-1); \
958 static SENSOR_DEVICE_ATTR_2(fan##offset##_min, S_IRUGO | S_IWUSR, \
959 applesmc_show_fan_speed, applesmc_store_fan_speed, 1, offset-1); \
961 static SENSOR_DEVICE_ATTR_2(fan##offset##_max, S_IRUGO, \
962 applesmc_show_fan_speed, NULL, 2, offset-1); \
964 static SENSOR_DEVICE_ATTR_2(fan##offset##_safe, S_IRUGO, \
965 applesmc_show_fan_speed, NULL, 3, offset-1); \
967 static SENSOR_DEVICE_ATTR_2(fan##offset##_output, S_IRUGO | S_IWUSR, \
968 applesmc_show_fan_speed, applesmc_store_fan_speed, 4, offset-1); \
970 static SENSOR_DEVICE_ATTR(fan##offset##_manual, S_IRUGO | S_IWUSR, \
971 applesmc_show_fan_manual, applesmc_store_fan_manual, offset-1); \
973 static SENSOR_DEVICE_ATTR(fan##offset##_label, S_IRUGO, \
974 applesmc_show_fan_position, NULL, offset-1); \
976 static struct attribute *fan##offset##_attributes[] = { \
977 &sensor_dev_attr_fan##offset##_input.dev_attr.attr, \
978 &sensor_dev_attr_fan##offset##_min.dev_attr.attr, \
979 &sensor_dev_attr_fan##offset##_max.dev_attr.attr, \
980 &sensor_dev_attr_fan##offset##_safe.dev_attr.attr, \
981 &sensor_dev_attr_fan##offset##_output.dev_attr.attr, \
982 &sensor_dev_attr_fan##offset##_manual.dev_attr.attr, \
983 &sensor_dev_attr_fan##offset##_label.dev_attr.attr, \
988 * Create the needed functions for each fan using the macro defined above
989 * (2 fans are supported)
991 sysfs_fan_speeds_offset(1);
992 sysfs_fan_speeds_offset(2);
994 static const struct attribute_group fan_attribute_groups[] = {
995 { .attrs = fan1_attributes },
996 { .attrs = fan2_attributes }
1000 * Temperature sensors sysfs entries.
1002 static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO,
1003 applesmc_show_temperature, NULL, 0);
1004 static SENSOR_DEVICE_ATTR(temp2_input, S_IRUGO,
1005 applesmc_show_temperature, NULL, 1);
1006 static SENSOR_DEVICE_ATTR(temp3_input, S_IRUGO,
1007 applesmc_show_temperature, NULL, 2);
1008 static SENSOR_DEVICE_ATTR(temp4_input, S_IRUGO,
1009 applesmc_show_temperature, NULL, 3);
1010 static SENSOR_DEVICE_ATTR(temp5_input, S_IRUGO,
1011 applesmc_show_temperature, NULL, 4);
1012 static SENSOR_DEVICE_ATTR(temp6_input, S_IRUGO,
1013 applesmc_show_temperature, NULL, 5);
1014 static SENSOR_DEVICE_ATTR(temp7_input, S_IRUGO,
1015 applesmc_show_temperature, NULL, 6);
1016 static SENSOR_DEVICE_ATTR(temp8_input, S_IRUGO,
1017 applesmc_show_temperature, NULL, 7);
1018 static SENSOR_DEVICE_ATTR(temp9_input, S_IRUGO,
1019 applesmc_show_temperature, NULL, 8);
1020 static SENSOR_DEVICE_ATTR(temp10_input, S_IRUGO,
1021 applesmc_show_temperature, NULL, 9);
1022 static SENSOR_DEVICE_ATTR(temp11_input, S_IRUGO,
1023 applesmc_show_temperature, NULL, 10);
1024 static SENSOR_DEVICE_ATTR(temp12_input, S_IRUGO,
1025 applesmc_show_temperature, NULL, 11);
1027 static struct attribute *temperature_attributes[] = {
1028 &sensor_dev_attr_temp1_input.dev_attr.attr,
1029 &sensor_dev_attr_temp2_input.dev_attr.attr,
1030 &sensor_dev_attr_temp3_input.dev_attr.attr,
1031 &sensor_dev_attr_temp4_input.dev_attr.attr,
1032 &sensor_dev_attr_temp5_input.dev_attr.attr,
1033 &sensor_dev_attr_temp6_input.dev_attr.attr,
1034 &sensor_dev_attr_temp7_input.dev_attr.attr,
1035 &sensor_dev_attr_temp8_input.dev_attr.attr,
1036 &sensor_dev_attr_temp9_input.dev_attr.attr,
1037 &sensor_dev_attr_temp10_input.dev_attr.attr,
1038 &sensor_dev_attr_temp11_input.dev_attr.attr,
1039 &sensor_dev_attr_temp12_input.dev_attr.attr,
1043 static const struct attribute_group temperature_attributes_group =
1044 { .attrs = temperature_attributes };
1049 * applesmc_dmi_match - found a match. return one, short-circuiting the hunt.
1051 static int applesmc_dmi_match(struct dmi_system_id *id)
1054 struct dmi_match_data* dmi_data = id->driver_data;
1055 printk(KERN_INFO "applesmc: %s detected:\n", id->ident);
1056 applesmc_accelerometer = dmi_data->accelerometer;
1057 printk(KERN_INFO "applesmc: - Model %s accelerometer\n",
1058 applesmc_accelerometer ? "with" : "without");
1059 applesmc_light = dmi_data->light;
1060 printk(KERN_INFO "applesmc: - Model %s light sensors and backlight\n",
1061 applesmc_light ? "with" : "without");
1063 applesmc_temperature_set = dmi_data->temperature_set;
1064 while (temperature_sensors_sets[applesmc_temperature_set][i] != NULL)
1066 printk(KERN_INFO "applesmc: - Model with %d temperature sensors\n", i);
1070 /* Create accelerometer ressources */
1071 static int applesmc_create_accelerometer(void)
1073 struct input_dev *idev;
1076 ret = sysfs_create_group(&pdev->dev.kobj,
1077 &accelerometer_attributes_group);
1081 applesmc_idev = input_allocate_polled_device();
1082 if (!applesmc_idev) {
1087 applesmc_idev->poll = applesmc_idev_poll;
1088 applesmc_idev->poll_interval = APPLESMC_POLL_INTERVAL;
1090 /* initial calibrate for the input device */
1091 applesmc_calibrate();
1093 /* initialize the input device */
1094 idev = applesmc_idev->input;
1095 idev->name = "applesmc";
1096 idev->id.bustype = BUS_HOST;
1097 idev->dev.parent = &pdev->dev;
1098 idev->evbit[0] = BIT(EV_ABS);
1099 input_set_abs_params(idev, ABS_X,
1100 -256, 256, APPLESMC_INPUT_FUZZ, APPLESMC_INPUT_FLAT);
1101 input_set_abs_params(idev, ABS_Y,
1102 -256, 256, APPLESMC_INPUT_FUZZ, APPLESMC_INPUT_FLAT);
1104 ret = input_register_polled_device(applesmc_idev);
1111 input_free_polled_device(applesmc_idev);
1114 sysfs_remove_group(&pdev->dev.kobj, &accelerometer_attributes_group);
1117 printk(KERN_WARNING "applesmc: driver init failed (ret=%d)!\n", ret);
1121 /* Release all ressources used by the accelerometer */
1122 static void applesmc_release_accelerometer(void)
1124 input_unregister_polled_device(applesmc_idev);
1125 input_free_polled_device(applesmc_idev);
1126 sysfs_remove_group(&pdev->dev.kobj, &accelerometer_attributes_group);
1129 static __initdata struct dmi_match_data applesmc_dmi_data[] = {
1130 /* MacBook Pro: accelerometer, backlight and temperature set 0 */
1131 { .accelerometer = 1, .light = 1, .temperature_set = 0 },
1132 /* MacBook: accelerometer and temperature set 0 */
1133 { .accelerometer = 1, .light = 0, .temperature_set = 0 },
1134 /* MacBook: temperature set 1 */
1135 { .accelerometer = 0, .light = 0, .temperature_set = 1 }
1138 /* Note that DMI_MATCH(...,"MacBook") will match "MacBookPro1,1".
1139 * So we need to put "Apple MacBook Pro" before "Apple MacBook". */
1140 static __initdata struct dmi_system_id applesmc_whitelist[] = {
1141 { applesmc_dmi_match, "Apple MacBook Pro", {
1142 DMI_MATCH(DMI_BOARD_VENDOR,"Apple"),
1143 DMI_MATCH(DMI_PRODUCT_NAME,"MacBookPro") },
1144 (void*)&applesmc_dmi_data[0]},
1145 { applesmc_dmi_match, "Apple MacBook", {
1146 DMI_MATCH(DMI_BOARD_VENDOR,"Apple"),
1147 DMI_MATCH(DMI_PRODUCT_NAME,"MacBook") },
1148 (void*)&applesmc_dmi_data[1]},
1149 { applesmc_dmi_match, "Apple Macmini", {
1150 DMI_MATCH(DMI_BOARD_VENDOR,"Apple"),
1151 DMI_MATCH(DMI_PRODUCT_NAME,"Macmini") },
1152 (void*)&applesmc_dmi_data[2]},
1156 static int __init applesmc_init(void)
1162 if (!dmi_check_system(applesmc_whitelist)) {
1163 printk(KERN_WARNING "applesmc: supported laptop not found!\n");
1168 if (!request_region(APPLESMC_DATA_PORT, APPLESMC_NR_PORTS,
1174 ret = platform_driver_register(&applesmc_driver);
1178 pdev = platform_device_register_simple("applesmc", APPLESMC_DATA_PORT,
1181 ret = PTR_ERR(pdev);
1185 ret = sysfs_create_file(&pdev->dev.kobj, &dev_attr_name.attr);
1189 /* Create key enumeration sysfs files */
1190 ret = sysfs_create_group(&pdev->dev.kobj, &key_enumeration_group);
1194 /* create fan files */
1195 count = applesmc_get_fan_count();
1197 printk(KERN_ERR "applesmc: Cannot get the number of fans.\n");
1199 printk(KERN_INFO "applesmc: %d fans found.\n", count);
1203 printk(KERN_WARNING "applesmc: More than 2 fans found,"
1204 " but at most 2 fans are supported"
1205 " by the driver.\n");
1207 ret = sysfs_create_group(&pdev->dev.kobj,
1208 &fan_attribute_groups[1]);
1210 goto out_key_enumeration;
1212 ret = sysfs_create_group(&pdev->dev.kobj,
1213 &fan_attribute_groups[0]);
1222 temperature_sensors_sets[applesmc_temperature_set][i] != NULL;
1224 if (temperature_attributes[i] == NULL) {
1225 printk(KERN_ERR "applesmc: More temperature sensors "
1226 "in temperature_sensors_sets (at least %i)"
1227 "than available sysfs files in "
1228 "temperature_attributes (%i), please report "
1229 "this bug.\n", i, i-1);
1230 goto out_temperature;
1232 ret = sysfs_create_file(&pdev->dev.kobj,
1233 temperature_attributes[i]);
1235 goto out_temperature;
1238 if (applesmc_accelerometer) {
1239 ret = applesmc_create_accelerometer();
1241 goto out_temperature;
1244 if (applesmc_light) {
1245 /* Add light sensor file */
1246 ret = sysfs_create_file(&pdev->dev.kobj, &dev_attr_light.attr);
1248 goto out_accelerometer;
1250 /* Create the workqueue */
1251 applesmc_led_wq = create_singlethread_workqueue("applesmc-led");
1252 if (!applesmc_led_wq) {
1254 goto out_light_sysfs;
1257 /* register as a led device */
1258 ret = led_classdev_register(&pdev->dev, &applesmc_backlight);
1263 hwmon_class_dev = hwmon_device_register(&pdev->dev);
1264 if (IS_ERR(hwmon_class_dev)) {
1265 ret = PTR_ERR(hwmon_class_dev);
1266 goto out_light_ledclass;
1269 printk(KERN_INFO "applesmc: driver successfully loaded.\n");
1275 led_classdev_unregister(&applesmc_backlight);
1278 destroy_workqueue(applesmc_led_wq);
1281 sysfs_remove_file(&pdev->dev.kobj, &dev_attr_light.attr);
1283 if (applesmc_accelerometer)
1284 applesmc_release_accelerometer();
1286 sysfs_remove_group(&pdev->dev.kobj, &temperature_attributes_group);
1287 sysfs_remove_group(&pdev->dev.kobj, &fan_attribute_groups[0]);
1289 sysfs_remove_group(&pdev->dev.kobj, &fan_attribute_groups[1]);
1290 out_key_enumeration:
1291 sysfs_remove_group(&pdev->dev.kobj, &key_enumeration_group);
1293 sysfs_remove_file(&pdev->dev.kobj, &dev_attr_name.attr);
1295 platform_device_unregister(pdev);
1297 platform_driver_unregister(&applesmc_driver);
1299 release_region(APPLESMC_DATA_PORT, APPLESMC_NR_PORTS);
1301 printk(KERN_WARNING "applesmc: driver init failed (ret=%d)!\n", ret);
1305 static void __exit applesmc_exit(void)
1307 hwmon_device_unregister(hwmon_class_dev);
1308 if (applesmc_light) {
1309 led_classdev_unregister(&applesmc_backlight);
1310 destroy_workqueue(applesmc_led_wq);
1311 sysfs_remove_file(&pdev->dev.kobj, &dev_attr_light.attr);
1313 if (applesmc_accelerometer)
1314 applesmc_release_accelerometer();
1315 sysfs_remove_group(&pdev->dev.kobj, &temperature_attributes_group);
1316 sysfs_remove_group(&pdev->dev.kobj, &fan_attribute_groups[0]);
1317 sysfs_remove_group(&pdev->dev.kobj, &fan_attribute_groups[1]);
1318 sysfs_remove_group(&pdev->dev.kobj, &key_enumeration_group);
1319 sysfs_remove_file(&pdev->dev.kobj, &dev_attr_name.attr);
1320 platform_device_unregister(pdev);
1321 platform_driver_unregister(&applesmc_driver);
1322 release_region(APPLESMC_DATA_PORT, APPLESMC_NR_PORTS);
1324 printk(KERN_INFO "applesmc: driver unloaded.\n");
1327 module_init(applesmc_init);
1328 module_exit(applesmc_exit);
1330 MODULE_AUTHOR("Nicolas Boichat");
1331 MODULE_DESCRIPTION("Apple SMC");
1332 MODULE_LICENSE("GPL v2");