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).
83 static const char* temperature_sensors_sets[][13] = {
84 /* Set 0: Macbook Pro */
85 { "TA0P", "TB0T", "TC0D", "TC0P", "TG0H", "TG0P", "TG0T", "Th0H",
86 "Th1H", "Tm0P", "Ts0P", "Ts1P", NULL },
87 /* Set 1: Macbook set */
88 { "TB0T", "TC0D", "TC0P", "TM0P", "TN0P", "TN1P", "Th0H", "Th0S",
89 "Th1H", "Ts0P", NULL },
90 /* Set 2: Macmini set */
91 { "TC0D", "TC0P", NULL }
94 /* List of keys used to read/write fan speeds */
95 static const char* fan_speed_keys[] = {
103 #define INIT_TIMEOUT_MSECS 5000 /* wait up to 5s for device init ... */
104 #define INIT_WAIT_MSECS 50 /* ... in 50ms increments */
106 #define APPLESMC_POLL_INTERVAL 50 /* msecs */
107 #define APPLESMC_INPUT_FUZZ 4 /* input event threshold */
108 #define APPLESMC_INPUT_FLAT 4
114 /* Structure to be passed to DMI_MATCH function */
115 struct dmi_match_data {
116 /* Indicates whether this computer has an accelerometer. */
118 /* Indicates whether this computer has light sensors and keyboard backlight. */
120 /* Indicates which temperature sensors set to use. */
124 static const int debug;
125 static struct platform_device *pdev;
128 static struct device *hwmon_dev;
129 static struct input_polled_dev *applesmc_idev;
131 /* Indicates whether this computer has an accelerometer. */
132 static unsigned int applesmc_accelerometer;
134 /* Indicates whether this computer has light sensors and keyboard backlight. */
135 static unsigned int applesmc_light;
137 /* Indicates which temperature sensors set to use. */
138 static unsigned int applesmc_temperature_set;
140 static DEFINE_MUTEX(applesmc_lock);
143 * Last index written to key_at_index sysfs file, and value to use for all other
144 * key_at_index_* sysfs files.
146 static unsigned int key_at_index;
148 static struct workqueue_struct *applesmc_led_wq;
151 * __wait_status - Wait up to 2ms for the status port to get a certain value
152 * (masked with 0x0f), returning zero if the value is obtained. Callers must
153 * hold applesmc_lock.
155 static int __wait_status(u8 val)
159 val = val & APPLESMC_STATUS_MASK;
161 for (i = 0; i < 200; i++) {
162 if ((inb(APPLESMC_CMD_PORT) & APPLESMC_STATUS_MASK) == val) {
165 "Waited %d us for status %x\n",
172 printk(KERN_WARNING "applesmc: wait status failed: %x != %x\n",
173 val, inb(APPLESMC_CMD_PORT));
179 * applesmc_read_key - reads len bytes from a given key, and put them in buffer.
180 * Returns zero on success or a negative error on failure. Callers must
181 * hold applesmc_lock.
183 static int applesmc_read_key(const char* key, u8* buffer, u8 len)
187 if (len > APPLESMC_MAX_DATA_LENGTH) {
188 printk(KERN_ERR "applesmc_read_key: cannot read more than "
189 "%d bytes\n", APPLESMC_MAX_DATA_LENGTH);
193 outb(APPLESMC_READ_CMD, APPLESMC_CMD_PORT);
194 if (__wait_status(0x0c))
197 for (i = 0; i < 4; i++) {
198 outb(key[i], APPLESMC_DATA_PORT);
199 if (__wait_status(0x04))
203 printk(KERN_DEBUG "<%s", key);
205 outb(len, APPLESMC_DATA_PORT);
207 printk(KERN_DEBUG ">%x", len);
209 for (i = 0; i < len; i++) {
210 if (__wait_status(0x05))
212 buffer[i] = inb(APPLESMC_DATA_PORT);
214 printk(KERN_DEBUG "<%x", buffer[i]);
217 printk(KERN_DEBUG "\n");
223 * applesmc_write_key - writes len bytes from buffer to a given key.
224 * Returns zero on success or a negative error on failure. Callers must
225 * hold applesmc_lock.
227 static int applesmc_write_key(const char* key, u8* buffer, u8 len)
231 if (len > APPLESMC_MAX_DATA_LENGTH) {
232 printk(KERN_ERR "applesmc_write_key: cannot write more than "
233 "%d bytes\n", APPLESMC_MAX_DATA_LENGTH);
237 outb(APPLESMC_WRITE_CMD, APPLESMC_CMD_PORT);
238 if (__wait_status(0x0c))
241 for (i = 0; i < 4; i++) {
242 outb(key[i], APPLESMC_DATA_PORT);
243 if (__wait_status(0x04))
247 outb(len, APPLESMC_DATA_PORT);
249 for (i = 0; i < len; i++) {
250 if (__wait_status(0x04))
252 outb(buffer[i], APPLESMC_DATA_PORT);
259 * applesmc_get_key_at_index - get key at index, and put the result in key
260 * (char[6]). Returns zero on success or a negative error on failure. Callers
261 * must hold applesmc_lock.
263 static int applesmc_get_key_at_index(int index, char* key)
267 readkey[0] = index >> 24;
268 readkey[1] = index >> 16;
269 readkey[2] = index >> 8;
272 outb(APPLESMC_GET_KEY_BY_INDEX_CMD, APPLESMC_CMD_PORT);
273 if (__wait_status(0x0c))
276 for (i = 0; i < 4; i++) {
277 outb(readkey[i], APPLESMC_DATA_PORT);
278 if (__wait_status(0x04))
282 outb(4, APPLESMC_DATA_PORT);
284 for (i = 0; i < 4; i++) {
285 if (__wait_status(0x05))
287 key[i] = inb(APPLESMC_DATA_PORT);
295 * applesmc_get_key_type - get key type, and put the result in type (char[6]).
296 * Returns zero on success or a negative error on failure. Callers must
297 * hold applesmc_lock.
299 static int applesmc_get_key_type(char* key, char* type)
303 outb(APPLESMC_GET_KEY_TYPE_CMD, APPLESMC_CMD_PORT);
304 if (__wait_status(0x0c))
307 for (i = 0; i < 4; i++) {
308 outb(key[i], APPLESMC_DATA_PORT);
309 if (__wait_status(0x04))
313 outb(5, APPLESMC_DATA_PORT);
315 for (i = 0; i < 6; i++) {
316 if (__wait_status(0x05))
318 type[i] = inb(APPLESMC_DATA_PORT);
326 * applesmc_read_motion_sensor - Read motion sensor (X, Y or Z). Callers must
327 * hold applesmc_lock.
329 static int applesmc_read_motion_sensor(int index, s16* value)
336 ret = applesmc_read_key(MOTION_SENSOR_X_KEY, buffer, 2);
339 ret = applesmc_read_key(MOTION_SENSOR_Y_KEY, buffer, 2);
342 ret = applesmc_read_key(MOTION_SENSOR_Z_KEY, buffer, 2);
348 *value = ((s16)buffer[0] << 8) | buffer[1];
354 * applesmc_device_init - initialize the accelerometer. Returns zero on success
355 * and negative error code on failure. Can sleep.
357 static int applesmc_device_init(void)
359 int total, ret = -ENXIO;
362 if (!applesmc_accelerometer)
365 mutex_lock(&applesmc_lock);
367 for (total = INIT_TIMEOUT_MSECS; total > 0; total -= INIT_WAIT_MSECS) {
369 printk(KERN_DEBUG "applesmc try %d\n", total);
370 if (!applesmc_read_key(MOTION_SENSOR_KEY, buffer, 2) &&
371 (buffer[0] != 0x00 || buffer[1] != 0x00)) {
372 if (total == INIT_TIMEOUT_MSECS) {
373 printk(KERN_DEBUG "applesmc: device has"
374 " already been initialized"
375 " (0x%02x, 0x%02x).\n",
376 buffer[0], buffer[1]);
378 printk(KERN_DEBUG "applesmc: device"
379 " successfully initialized"
380 " (0x%02x, 0x%02x).\n",
381 buffer[0], buffer[1]);
388 applesmc_write_key(MOTION_SENSOR_KEY, buffer, 2);
389 msleep(INIT_WAIT_MSECS);
392 printk(KERN_WARNING "applesmc: failed to init the device\n");
395 mutex_unlock(&applesmc_lock);
400 * applesmc_get_fan_count - get the number of fans. Callers must NOT hold
403 static int applesmc_get_fan_count(void)
408 mutex_lock(&applesmc_lock);
410 ret = applesmc_read_key(FANS_COUNT, buffer, 1);
412 mutex_unlock(&applesmc_lock);
419 /* Device model stuff */
420 static int applesmc_probe(struct platform_device *dev)
424 ret = applesmc_device_init();
428 printk(KERN_INFO "applesmc: device successfully initialized.\n");
432 static int applesmc_resume(struct platform_device *dev)
434 return applesmc_device_init();
437 static struct platform_driver applesmc_driver = {
438 .probe = applesmc_probe,
439 .resume = applesmc_resume,
442 .owner = THIS_MODULE,
447 * applesmc_calibrate - Set our "resting" values. Callers must
448 * hold applesmc_lock.
450 static void applesmc_calibrate(void)
452 applesmc_read_motion_sensor(SENSOR_X, &rest_x);
453 applesmc_read_motion_sensor(SENSOR_Y, &rest_y);
457 static void applesmc_idev_poll(struct input_polled_dev *dev)
459 struct input_dev *idev = dev->input;
462 mutex_lock(&applesmc_lock);
464 if (applesmc_read_motion_sensor(SENSOR_X, &x))
466 if (applesmc_read_motion_sensor(SENSOR_Y, &y))
470 input_report_abs(idev, ABS_X, x - rest_x);
471 input_report_abs(idev, ABS_Y, y - rest_y);
475 mutex_unlock(&applesmc_lock);
480 static ssize_t applesmc_name_show(struct device *dev,
481 struct device_attribute *attr, char *buf)
483 return snprintf(buf, PAGE_SIZE, "applesmc\n");
486 static ssize_t applesmc_position_show(struct device *dev,
487 struct device_attribute *attr, char *buf)
492 mutex_lock(&applesmc_lock);
494 ret = applesmc_read_motion_sensor(SENSOR_X, &x);
497 ret = applesmc_read_motion_sensor(SENSOR_Y, &y);
500 ret = applesmc_read_motion_sensor(SENSOR_Z, &z);
505 mutex_unlock(&applesmc_lock);
509 return snprintf(buf, PAGE_SIZE, "(%d,%d,%d)\n", x, y, z);
512 static ssize_t applesmc_light_show(struct device *dev,
513 struct device_attribute *attr, char *sysfsbuf)
516 u8 left = 0, right = 0;
519 mutex_lock(&applesmc_lock);
521 ret = applesmc_read_key(LIGHT_SENSOR_LEFT_KEY, buffer, 6);
525 ret = applesmc_read_key(LIGHT_SENSOR_RIGHT_KEY, buffer, 6);
529 mutex_unlock(&applesmc_lock);
533 return snprintf(sysfsbuf, PAGE_SIZE, "(%d,%d)\n", left, right);
536 /* Displays degree Celsius * 1000 */
537 static ssize_t applesmc_show_temperature(struct device *dev,
538 struct device_attribute *devattr, char *sysfsbuf)
543 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
545 temperature_sensors_sets[applesmc_temperature_set][attr->index];
547 mutex_lock(&applesmc_lock);
549 ret = applesmc_read_key(key, buffer, 2);
550 temp = buffer[0]*1000;
551 temp += (buffer[1] >> 6) * 250;
553 mutex_unlock(&applesmc_lock);
558 return snprintf(sysfsbuf, PAGE_SIZE, "%u\n", temp);
561 static ssize_t applesmc_show_fan_speed(struct device *dev,
562 struct device_attribute *attr, char *sysfsbuf)
565 unsigned int speed = 0;
568 struct sensor_device_attribute_2 *sensor_attr =
569 to_sensor_dev_attr_2(attr);
571 newkey[0] = fan_speed_keys[sensor_attr->nr][0];
572 newkey[1] = '0' + sensor_attr->index;
573 newkey[2] = fan_speed_keys[sensor_attr->nr][2];
574 newkey[3] = fan_speed_keys[sensor_attr->nr][3];
577 mutex_lock(&applesmc_lock);
579 ret = applesmc_read_key(newkey, buffer, 2);
580 speed = ((buffer[0] << 8 | buffer[1]) >> 2);
582 mutex_unlock(&applesmc_lock);
586 return snprintf(sysfsbuf, PAGE_SIZE, "%u\n", speed);
589 static ssize_t applesmc_store_fan_speed(struct device *dev,
590 struct device_attribute *attr,
591 const char *sysfsbuf, size_t count)
597 struct sensor_device_attribute_2 *sensor_attr =
598 to_sensor_dev_attr_2(attr);
600 speed = simple_strtoul(sysfsbuf, NULL, 10);
602 if (speed > 0x4000) /* Bigger than a 14-bit value */
605 newkey[0] = fan_speed_keys[sensor_attr->nr][0];
606 newkey[1] = '0' + sensor_attr->index;
607 newkey[2] = fan_speed_keys[sensor_attr->nr][2];
608 newkey[3] = fan_speed_keys[sensor_attr->nr][3];
611 mutex_lock(&applesmc_lock);
613 buffer[0] = (speed >> 6) & 0xff;
614 buffer[1] = (speed << 2) & 0xff;
615 ret = applesmc_write_key(newkey, buffer, 2);
617 mutex_unlock(&applesmc_lock);
624 static ssize_t applesmc_show_fan_manual(struct device *dev,
625 struct device_attribute *devattr, char *sysfsbuf)
630 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
632 mutex_lock(&applesmc_lock);
634 ret = applesmc_read_key(FANS_MANUAL, buffer, 2);
635 manual = ((buffer[0] << 8 | buffer[1]) >> attr->index) & 0x01;
637 mutex_unlock(&applesmc_lock);
641 return snprintf(sysfsbuf, PAGE_SIZE, "%d\n", manual);
644 static ssize_t applesmc_store_fan_manual(struct device *dev,
645 struct device_attribute *devattr,
646 const char *sysfsbuf, size_t count)
652 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
654 input = simple_strtoul(sysfsbuf, NULL, 10);
656 mutex_lock(&applesmc_lock);
658 ret = applesmc_read_key(FANS_MANUAL, buffer, 2);
659 val = (buffer[0] << 8 | buffer[1]);
664 val = val | (0x01 << attr->index);
666 val = val & ~(0x01 << attr->index);
668 buffer[0] = (val >> 8) & 0xFF;
669 buffer[1] = val & 0xFF;
671 ret = applesmc_write_key(FANS_MANUAL, buffer, 2);
674 mutex_unlock(&applesmc_lock);
681 static ssize_t applesmc_show_fan_position(struct device *dev,
682 struct device_attribute *attr, char *sysfsbuf)
687 struct sensor_device_attribute_2 *sensor_attr =
688 to_sensor_dev_attr_2(attr);
690 newkey[0] = FAN_POSITION[0];
691 newkey[1] = '0' + sensor_attr->index;
692 newkey[2] = FAN_POSITION[2];
693 newkey[3] = FAN_POSITION[3];
696 mutex_lock(&applesmc_lock);
698 ret = applesmc_read_key(newkey, buffer, 16);
701 mutex_unlock(&applesmc_lock);
705 return snprintf(sysfsbuf, PAGE_SIZE, "%s\n", buffer+4);
708 static ssize_t applesmc_calibrate_show(struct device *dev,
709 struct device_attribute *attr, char *sysfsbuf)
711 return snprintf(sysfsbuf, PAGE_SIZE, "(%d,%d)\n", rest_x, rest_y);
714 static ssize_t applesmc_calibrate_store(struct device *dev,
715 struct device_attribute *attr, const char *sysfsbuf, size_t count)
717 mutex_lock(&applesmc_lock);
718 applesmc_calibrate();
719 mutex_unlock(&applesmc_lock);
724 /* Store the next backlight value to be written by the work */
725 static unsigned int backlight_value;
727 static void applesmc_backlight_set(struct work_struct *work)
731 mutex_lock(&applesmc_lock);
732 buffer[0] = backlight_value;
734 applesmc_write_key(BACKLIGHT_KEY, buffer, 2);
735 mutex_unlock(&applesmc_lock);
737 static DECLARE_WORK(backlight_work, &applesmc_backlight_set);
739 static void applesmc_brightness_set(struct led_classdev *led_cdev,
740 enum led_brightness value)
744 backlight_value = value;
745 ret = queue_work(applesmc_led_wq, &backlight_work);
748 printk(KERN_DEBUG "applesmc: work was already on the queue.\n");
751 static ssize_t applesmc_key_count_show(struct device *dev,
752 struct device_attribute *attr, char *sysfsbuf)
758 mutex_lock(&applesmc_lock);
760 ret = applesmc_read_key(KEY_COUNT_KEY, buffer, 4);
761 count = ((u32)buffer[0]<<24) + ((u32)buffer[1]<<16) +
762 ((u32)buffer[2]<<8) + buffer[3];
764 mutex_unlock(&applesmc_lock);
768 return snprintf(sysfsbuf, PAGE_SIZE, "%d\n", count);
771 static ssize_t applesmc_key_at_index_read_show(struct device *dev,
772 struct device_attribute *attr, char *sysfsbuf)
778 mutex_lock(&applesmc_lock);
780 ret = applesmc_get_key_at_index(key_at_index, key);
782 if (ret || !key[0]) {
783 mutex_unlock(&applesmc_lock);
788 ret = applesmc_get_key_type(key, info);
791 mutex_unlock(&applesmc_lock);
797 * info[0] maximum value (APPLESMC_MAX_DATA_LENGTH) is much lower than
798 * PAGE_SIZE, so we don't need any checks before writing to sysfsbuf.
800 ret = applesmc_read_key(key, sysfsbuf, info[0]);
802 mutex_unlock(&applesmc_lock);
811 static ssize_t applesmc_key_at_index_data_length_show(struct device *dev,
812 struct device_attribute *attr, char *sysfsbuf)
818 mutex_lock(&applesmc_lock);
820 ret = applesmc_get_key_at_index(key_at_index, key);
822 if (ret || !key[0]) {
823 mutex_unlock(&applesmc_lock);
828 ret = applesmc_get_key_type(key, info);
830 mutex_unlock(&applesmc_lock);
833 return snprintf(sysfsbuf, PAGE_SIZE, "%d\n", info[0]);
838 static ssize_t applesmc_key_at_index_type_show(struct device *dev,
839 struct device_attribute *attr, char *sysfsbuf)
845 mutex_lock(&applesmc_lock);
847 ret = applesmc_get_key_at_index(key_at_index, key);
849 if (ret || !key[0]) {
850 mutex_unlock(&applesmc_lock);
855 ret = applesmc_get_key_type(key, info);
857 mutex_unlock(&applesmc_lock);
860 return snprintf(sysfsbuf, PAGE_SIZE, "%s\n", info+1);
865 static ssize_t applesmc_key_at_index_name_show(struct device *dev,
866 struct device_attribute *attr, char *sysfsbuf)
871 mutex_lock(&applesmc_lock);
873 ret = applesmc_get_key_at_index(key_at_index, key);
875 mutex_unlock(&applesmc_lock);
878 return snprintf(sysfsbuf, PAGE_SIZE, "%s\n", key);
883 static ssize_t applesmc_key_at_index_show(struct device *dev,
884 struct device_attribute *attr, char *sysfsbuf)
886 return snprintf(sysfsbuf, PAGE_SIZE, "%d\n", key_at_index);
889 static ssize_t applesmc_key_at_index_store(struct device *dev,
890 struct device_attribute *attr, const char *sysfsbuf, size_t count)
892 mutex_lock(&applesmc_lock);
894 key_at_index = simple_strtoul(sysfsbuf, NULL, 10);
896 mutex_unlock(&applesmc_lock);
901 static struct led_classdev applesmc_backlight = {
902 .name = "smc:kbd_backlight",
903 .default_trigger = "nand-disk",
904 .brightness_set = applesmc_brightness_set,
907 static DEVICE_ATTR(name, 0444, applesmc_name_show, NULL);
909 static DEVICE_ATTR(position, 0444, applesmc_position_show, NULL);
910 static DEVICE_ATTR(calibrate, 0644,
911 applesmc_calibrate_show, applesmc_calibrate_store);
913 static struct attribute *accelerometer_attributes[] = {
914 &dev_attr_position.attr,
915 &dev_attr_calibrate.attr,
919 static const struct attribute_group accelerometer_attributes_group =
920 { .attrs = accelerometer_attributes };
922 static DEVICE_ATTR(light, 0444, applesmc_light_show, NULL);
924 static DEVICE_ATTR(key_count, 0444, applesmc_key_count_show, NULL);
925 static DEVICE_ATTR(key_at_index, 0644,
926 applesmc_key_at_index_show, applesmc_key_at_index_store);
927 static DEVICE_ATTR(key_at_index_name, 0444,
928 applesmc_key_at_index_name_show, NULL);
929 static DEVICE_ATTR(key_at_index_type, 0444,
930 applesmc_key_at_index_type_show, NULL);
931 static DEVICE_ATTR(key_at_index_data_length, 0444,
932 applesmc_key_at_index_data_length_show, NULL);
933 static DEVICE_ATTR(key_at_index_data, 0444,
934 applesmc_key_at_index_read_show, NULL);
936 static struct attribute *key_enumeration_attributes[] = {
937 &dev_attr_key_count.attr,
938 &dev_attr_key_at_index.attr,
939 &dev_attr_key_at_index_name.attr,
940 &dev_attr_key_at_index_type.attr,
941 &dev_attr_key_at_index_data_length.attr,
942 &dev_attr_key_at_index_data.attr,
946 static const struct attribute_group key_enumeration_group =
947 { .attrs = key_enumeration_attributes };
950 * Macro defining SENSOR_DEVICE_ATTR for a fan sysfs entries.
951 * - show actual speed
952 * - show/store minimum speed
953 * - show maximum speed
955 * - show/store target speed
956 * - show/store manual mode
958 #define sysfs_fan_speeds_offset(offset) \
959 static SENSOR_DEVICE_ATTR_2(fan##offset##_input, S_IRUGO, \
960 applesmc_show_fan_speed, NULL, 0, offset-1); \
962 static SENSOR_DEVICE_ATTR_2(fan##offset##_min, S_IRUGO | S_IWUSR, \
963 applesmc_show_fan_speed, applesmc_store_fan_speed, 1, offset-1); \
965 static SENSOR_DEVICE_ATTR_2(fan##offset##_max, S_IRUGO, \
966 applesmc_show_fan_speed, NULL, 2, offset-1); \
968 static SENSOR_DEVICE_ATTR_2(fan##offset##_safe, S_IRUGO, \
969 applesmc_show_fan_speed, NULL, 3, offset-1); \
971 static SENSOR_DEVICE_ATTR_2(fan##offset##_output, S_IRUGO | S_IWUSR, \
972 applesmc_show_fan_speed, applesmc_store_fan_speed, 4, offset-1); \
974 static SENSOR_DEVICE_ATTR(fan##offset##_manual, S_IRUGO | S_IWUSR, \
975 applesmc_show_fan_manual, applesmc_store_fan_manual, offset-1); \
977 static SENSOR_DEVICE_ATTR(fan##offset##_label, S_IRUGO, \
978 applesmc_show_fan_position, NULL, offset-1); \
980 static struct attribute *fan##offset##_attributes[] = { \
981 &sensor_dev_attr_fan##offset##_input.dev_attr.attr, \
982 &sensor_dev_attr_fan##offset##_min.dev_attr.attr, \
983 &sensor_dev_attr_fan##offset##_max.dev_attr.attr, \
984 &sensor_dev_attr_fan##offset##_safe.dev_attr.attr, \
985 &sensor_dev_attr_fan##offset##_output.dev_attr.attr, \
986 &sensor_dev_attr_fan##offset##_manual.dev_attr.attr, \
987 &sensor_dev_attr_fan##offset##_label.dev_attr.attr, \
992 * Create the needed functions for each fan using the macro defined above
993 * (2 fans are supported)
995 sysfs_fan_speeds_offset(1);
996 sysfs_fan_speeds_offset(2);
998 static const struct attribute_group fan_attribute_groups[] = {
999 { .attrs = fan1_attributes },
1000 { .attrs = fan2_attributes }
1004 * Temperature sensors sysfs entries.
1006 static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO,
1007 applesmc_show_temperature, NULL, 0);
1008 static SENSOR_DEVICE_ATTR(temp2_input, S_IRUGO,
1009 applesmc_show_temperature, NULL, 1);
1010 static SENSOR_DEVICE_ATTR(temp3_input, S_IRUGO,
1011 applesmc_show_temperature, NULL, 2);
1012 static SENSOR_DEVICE_ATTR(temp4_input, S_IRUGO,
1013 applesmc_show_temperature, NULL, 3);
1014 static SENSOR_DEVICE_ATTR(temp5_input, S_IRUGO,
1015 applesmc_show_temperature, NULL, 4);
1016 static SENSOR_DEVICE_ATTR(temp6_input, S_IRUGO,
1017 applesmc_show_temperature, NULL, 5);
1018 static SENSOR_DEVICE_ATTR(temp7_input, S_IRUGO,
1019 applesmc_show_temperature, NULL, 6);
1020 static SENSOR_DEVICE_ATTR(temp8_input, S_IRUGO,
1021 applesmc_show_temperature, NULL, 7);
1022 static SENSOR_DEVICE_ATTR(temp9_input, S_IRUGO,
1023 applesmc_show_temperature, NULL, 8);
1024 static SENSOR_DEVICE_ATTR(temp10_input, S_IRUGO,
1025 applesmc_show_temperature, NULL, 9);
1026 static SENSOR_DEVICE_ATTR(temp11_input, S_IRUGO,
1027 applesmc_show_temperature, NULL, 10);
1028 static SENSOR_DEVICE_ATTR(temp12_input, S_IRUGO,
1029 applesmc_show_temperature, NULL, 11);
1031 static struct attribute *temperature_attributes[] = {
1032 &sensor_dev_attr_temp1_input.dev_attr.attr,
1033 &sensor_dev_attr_temp2_input.dev_attr.attr,
1034 &sensor_dev_attr_temp3_input.dev_attr.attr,
1035 &sensor_dev_attr_temp4_input.dev_attr.attr,
1036 &sensor_dev_attr_temp5_input.dev_attr.attr,
1037 &sensor_dev_attr_temp6_input.dev_attr.attr,
1038 &sensor_dev_attr_temp7_input.dev_attr.attr,
1039 &sensor_dev_attr_temp8_input.dev_attr.attr,
1040 &sensor_dev_attr_temp9_input.dev_attr.attr,
1041 &sensor_dev_attr_temp10_input.dev_attr.attr,
1042 &sensor_dev_attr_temp11_input.dev_attr.attr,
1043 &sensor_dev_attr_temp12_input.dev_attr.attr,
1047 static const struct attribute_group temperature_attributes_group =
1048 { .attrs = temperature_attributes };
1053 * applesmc_dmi_match - found a match. return one, short-circuiting the hunt.
1055 static int applesmc_dmi_match(const struct dmi_system_id *id)
1058 struct dmi_match_data* dmi_data = id->driver_data;
1059 printk(KERN_INFO "applesmc: %s detected:\n", id->ident);
1060 applesmc_accelerometer = dmi_data->accelerometer;
1061 printk(KERN_INFO "applesmc: - Model %s accelerometer\n",
1062 applesmc_accelerometer ? "with" : "without");
1063 applesmc_light = dmi_data->light;
1064 printk(KERN_INFO "applesmc: - Model %s light sensors and backlight\n",
1065 applesmc_light ? "with" : "without");
1067 applesmc_temperature_set = dmi_data->temperature_set;
1068 while (temperature_sensors_sets[applesmc_temperature_set][i] != NULL)
1070 printk(KERN_INFO "applesmc: - Model with %d temperature sensors\n", i);
1074 /* Create accelerometer ressources */
1075 static int applesmc_create_accelerometer(void)
1077 struct input_dev *idev;
1080 ret = sysfs_create_group(&pdev->dev.kobj,
1081 &accelerometer_attributes_group);
1085 applesmc_idev = input_allocate_polled_device();
1086 if (!applesmc_idev) {
1091 applesmc_idev->poll = applesmc_idev_poll;
1092 applesmc_idev->poll_interval = APPLESMC_POLL_INTERVAL;
1094 /* initial calibrate for the input device */
1095 applesmc_calibrate();
1097 /* initialize the input device */
1098 idev = applesmc_idev->input;
1099 idev->name = "applesmc";
1100 idev->id.bustype = BUS_HOST;
1101 idev->dev.parent = &pdev->dev;
1102 idev->evbit[0] = BIT_MASK(EV_ABS);
1103 input_set_abs_params(idev, ABS_X,
1104 -256, 256, APPLESMC_INPUT_FUZZ, APPLESMC_INPUT_FLAT);
1105 input_set_abs_params(idev, ABS_Y,
1106 -256, 256, APPLESMC_INPUT_FUZZ, APPLESMC_INPUT_FLAT);
1108 ret = input_register_polled_device(applesmc_idev);
1115 input_free_polled_device(applesmc_idev);
1118 sysfs_remove_group(&pdev->dev.kobj, &accelerometer_attributes_group);
1121 printk(KERN_WARNING "applesmc: driver init failed (ret=%d)!\n", ret);
1125 /* Release all ressources used by the accelerometer */
1126 static void applesmc_release_accelerometer(void)
1128 input_unregister_polled_device(applesmc_idev);
1129 input_free_polled_device(applesmc_idev);
1130 sysfs_remove_group(&pdev->dev.kobj, &accelerometer_attributes_group);
1133 static __initdata struct dmi_match_data applesmc_dmi_data[] = {
1134 /* MacBook Pro: accelerometer, backlight and temperature set 0 */
1135 { .accelerometer = 1, .light = 1, .temperature_set = 0 },
1136 /* MacBook: accelerometer and temperature set 1 */
1137 { .accelerometer = 1, .light = 0, .temperature_set = 1 },
1138 /* MacMini: temperature set 2 */
1139 { .accelerometer = 0, .light = 0, .temperature_set = 2 },
1142 /* Note that DMI_MATCH(...,"MacBook") will match "MacBookPro1,1".
1143 * So we need to put "Apple MacBook Pro" before "Apple MacBook". */
1144 static __initdata struct dmi_system_id applesmc_whitelist[] = {
1145 { applesmc_dmi_match, "Apple MacBook Pro", {
1146 DMI_MATCH(DMI_BOARD_VENDOR,"Apple"),
1147 DMI_MATCH(DMI_PRODUCT_NAME,"MacBookPro") },
1148 (void*)&applesmc_dmi_data[0]},
1149 { applesmc_dmi_match, "Apple MacBook", {
1150 DMI_MATCH(DMI_BOARD_VENDOR,"Apple"),
1151 DMI_MATCH(DMI_PRODUCT_NAME,"MacBook") },
1152 (void*)&applesmc_dmi_data[1]},
1153 { applesmc_dmi_match, "Apple Macmini", {
1154 DMI_MATCH(DMI_BOARD_VENDOR,"Apple"),
1155 DMI_MATCH(DMI_PRODUCT_NAME,"Macmini") },
1156 (void*)&applesmc_dmi_data[2]},
1160 static int __init applesmc_init(void)
1166 if (!dmi_check_system(applesmc_whitelist)) {
1167 printk(KERN_WARNING "applesmc: supported laptop not found!\n");
1172 if (!request_region(APPLESMC_DATA_PORT, APPLESMC_NR_PORTS,
1178 ret = platform_driver_register(&applesmc_driver);
1182 pdev = platform_device_register_simple("applesmc", APPLESMC_DATA_PORT,
1185 ret = PTR_ERR(pdev);
1189 ret = sysfs_create_file(&pdev->dev.kobj, &dev_attr_name.attr);
1193 /* Create key enumeration sysfs files */
1194 ret = sysfs_create_group(&pdev->dev.kobj, &key_enumeration_group);
1198 /* create fan files */
1199 count = applesmc_get_fan_count();
1201 printk(KERN_ERR "applesmc: Cannot get the number of fans.\n");
1203 printk(KERN_INFO "applesmc: %d fans found.\n", count);
1207 printk(KERN_WARNING "applesmc: More than 2 fans found,"
1208 " but at most 2 fans are supported"
1209 " by the driver.\n");
1211 ret = sysfs_create_group(&pdev->dev.kobj,
1212 &fan_attribute_groups[1]);
1214 goto out_key_enumeration;
1216 ret = sysfs_create_group(&pdev->dev.kobj,
1217 &fan_attribute_groups[0]);
1226 temperature_sensors_sets[applesmc_temperature_set][i] != NULL;
1228 if (temperature_attributes[i] == NULL) {
1229 printk(KERN_ERR "applesmc: More temperature sensors "
1230 "in temperature_sensors_sets (at least %i)"
1231 "than available sysfs files in "
1232 "temperature_attributes (%i), please report "
1233 "this bug.\n", i, i-1);
1234 goto out_temperature;
1236 ret = sysfs_create_file(&pdev->dev.kobj,
1237 temperature_attributes[i]);
1239 goto out_temperature;
1242 if (applesmc_accelerometer) {
1243 ret = applesmc_create_accelerometer();
1245 goto out_temperature;
1248 if (applesmc_light) {
1249 /* Add light sensor file */
1250 ret = sysfs_create_file(&pdev->dev.kobj, &dev_attr_light.attr);
1252 goto out_accelerometer;
1254 /* Create the workqueue */
1255 applesmc_led_wq = create_singlethread_workqueue("applesmc-led");
1256 if (!applesmc_led_wq) {
1258 goto out_light_sysfs;
1261 /* register as a led device */
1262 ret = led_classdev_register(&pdev->dev, &applesmc_backlight);
1267 hwmon_dev = hwmon_device_register(&pdev->dev);
1268 if (IS_ERR(hwmon_dev)) {
1269 ret = PTR_ERR(hwmon_dev);
1270 goto out_light_ledclass;
1273 printk(KERN_INFO "applesmc: driver successfully loaded.\n");
1279 led_classdev_unregister(&applesmc_backlight);
1282 destroy_workqueue(applesmc_led_wq);
1285 sysfs_remove_file(&pdev->dev.kobj, &dev_attr_light.attr);
1287 if (applesmc_accelerometer)
1288 applesmc_release_accelerometer();
1290 sysfs_remove_group(&pdev->dev.kobj, &temperature_attributes_group);
1291 sysfs_remove_group(&pdev->dev.kobj, &fan_attribute_groups[0]);
1293 sysfs_remove_group(&pdev->dev.kobj, &fan_attribute_groups[1]);
1294 out_key_enumeration:
1295 sysfs_remove_group(&pdev->dev.kobj, &key_enumeration_group);
1297 sysfs_remove_file(&pdev->dev.kobj, &dev_attr_name.attr);
1299 platform_device_unregister(pdev);
1301 platform_driver_unregister(&applesmc_driver);
1303 release_region(APPLESMC_DATA_PORT, APPLESMC_NR_PORTS);
1305 printk(KERN_WARNING "applesmc: driver init failed (ret=%d)!\n", ret);
1309 static void __exit applesmc_exit(void)
1311 hwmon_device_unregister(hwmon_dev);
1312 if (applesmc_light) {
1313 led_classdev_unregister(&applesmc_backlight);
1314 destroy_workqueue(applesmc_led_wq);
1315 sysfs_remove_file(&pdev->dev.kobj, &dev_attr_light.attr);
1317 if (applesmc_accelerometer)
1318 applesmc_release_accelerometer();
1319 sysfs_remove_group(&pdev->dev.kobj, &temperature_attributes_group);
1320 sysfs_remove_group(&pdev->dev.kobj, &fan_attribute_groups[0]);
1321 sysfs_remove_group(&pdev->dev.kobj, &fan_attribute_groups[1]);
1322 sysfs_remove_group(&pdev->dev.kobj, &key_enumeration_group);
1323 sysfs_remove_file(&pdev->dev.kobj, &dev_attr_name.attr);
1324 platform_device_unregister(pdev);
1325 platform_driver_unregister(&applesmc_driver);
1326 release_region(APPLESMC_DATA_PORT, APPLESMC_NR_PORTS);
1328 printk(KERN_INFO "applesmc: driver unloaded.\n");
1331 module_init(applesmc_init);
1332 module_exit(applesmc_exit);
1334 MODULE_AUTHOR("Nicolas Boichat");
1335 MODULE_DESCRIPTION("Apple SMC");
1336 MODULE_LICENSE("GPL v2");