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[][36] = {
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 },
92 /* Set 3: Mac Pro (2 x Quad-Core) */
93 { "TA0P", "TCAG", "TCAH", "TCBG", "TCBH", "TC0C", "TC0D", "TC0P",
94 "TC1C", "TC1D", "TC2C", "TC2D", "TC3C", "TC3D", "THTG", "TH0P",
95 "TH1P", "TH2P", "TH3P", "TMAP", "TMAS", "TMBS", "TM0P", "TM0S",
96 "TM1P", "TM1S", "TM2P", "TM2S", "TM3S", "TM8P", "TM8S", "TM9P",
97 "TM9S", "TN0H", "TS0C", NULL },
100 /* List of keys used to read/write fan speeds */
101 static const char* fan_speed_keys[] = {
109 #define INIT_TIMEOUT_MSECS 5000 /* wait up to 5s for device init ... */
110 #define INIT_WAIT_MSECS 50 /* ... in 50ms increments */
112 #define APPLESMC_POLL_INTERVAL 50 /* msecs */
113 #define APPLESMC_INPUT_FUZZ 4 /* input event threshold */
114 #define APPLESMC_INPUT_FLAT 4
120 /* Structure to be passed to DMI_MATCH function */
121 struct dmi_match_data {
122 /* Indicates whether this computer has an accelerometer. */
124 /* Indicates whether this computer has light sensors and keyboard backlight. */
126 /* Indicates which temperature sensors set to use. */
130 static const int debug;
131 static struct platform_device *pdev;
134 static struct device *hwmon_dev;
135 static struct input_polled_dev *applesmc_idev;
137 /* Indicates whether this computer has an accelerometer. */
138 static unsigned int applesmc_accelerometer;
140 /* Indicates whether this computer has light sensors and keyboard backlight. */
141 static unsigned int applesmc_light;
143 /* Indicates which temperature sensors set to use. */
144 static unsigned int applesmc_temperature_set;
146 static DEFINE_MUTEX(applesmc_lock);
149 * Last index written to key_at_index sysfs file, and value to use for all other
150 * key_at_index_* sysfs files.
152 static unsigned int key_at_index;
154 static struct workqueue_struct *applesmc_led_wq;
157 * __wait_status - Wait up to 2ms for the status port to get a certain value
158 * (masked with 0x0f), returning zero if the value is obtained. Callers must
159 * hold applesmc_lock.
161 static int __wait_status(u8 val)
165 val = val & APPLESMC_STATUS_MASK;
167 for (i = 0; i < 200; i++) {
168 if ((inb(APPLESMC_CMD_PORT) & APPLESMC_STATUS_MASK) == val) {
171 "Waited %d us for status %x\n",
178 printk(KERN_WARNING "applesmc: wait status failed: %x != %x\n",
179 val, inb(APPLESMC_CMD_PORT));
185 * applesmc_read_key - reads len bytes from a given key, and put them in buffer.
186 * Returns zero on success or a negative error on failure. Callers must
187 * hold applesmc_lock.
189 static int applesmc_read_key(const char* key, u8* buffer, u8 len)
193 if (len > APPLESMC_MAX_DATA_LENGTH) {
194 printk(KERN_ERR "applesmc_read_key: cannot read more than "
195 "%d bytes\n", APPLESMC_MAX_DATA_LENGTH);
199 outb(APPLESMC_READ_CMD, APPLESMC_CMD_PORT);
200 if (__wait_status(0x0c))
203 for (i = 0; i < 4; i++) {
204 outb(key[i], APPLESMC_DATA_PORT);
205 if (__wait_status(0x04))
209 printk(KERN_DEBUG "<%s", key);
211 outb(len, APPLESMC_DATA_PORT);
213 printk(KERN_DEBUG ">%x", len);
215 for (i = 0; i < len; i++) {
216 if (__wait_status(0x05))
218 buffer[i] = inb(APPLESMC_DATA_PORT);
220 printk(KERN_DEBUG "<%x", buffer[i]);
223 printk(KERN_DEBUG "\n");
229 * applesmc_write_key - writes len bytes from buffer to a given key.
230 * Returns zero on success or a negative error on failure. Callers must
231 * hold applesmc_lock.
233 static int applesmc_write_key(const char* key, u8* buffer, u8 len)
237 if (len > APPLESMC_MAX_DATA_LENGTH) {
238 printk(KERN_ERR "applesmc_write_key: cannot write more than "
239 "%d bytes\n", APPLESMC_MAX_DATA_LENGTH);
243 outb(APPLESMC_WRITE_CMD, APPLESMC_CMD_PORT);
244 if (__wait_status(0x0c))
247 for (i = 0; i < 4; i++) {
248 outb(key[i], APPLESMC_DATA_PORT);
249 if (__wait_status(0x04))
253 outb(len, APPLESMC_DATA_PORT);
255 for (i = 0; i < len; i++) {
256 if (__wait_status(0x04))
258 outb(buffer[i], APPLESMC_DATA_PORT);
265 * applesmc_get_key_at_index - get key at index, and put the result in key
266 * (char[6]). Returns zero on success or a negative error on failure. Callers
267 * must hold applesmc_lock.
269 static int applesmc_get_key_at_index(int index, char* key)
273 readkey[0] = index >> 24;
274 readkey[1] = index >> 16;
275 readkey[2] = index >> 8;
278 outb(APPLESMC_GET_KEY_BY_INDEX_CMD, APPLESMC_CMD_PORT);
279 if (__wait_status(0x0c))
282 for (i = 0; i < 4; i++) {
283 outb(readkey[i], APPLESMC_DATA_PORT);
284 if (__wait_status(0x04))
288 outb(4, APPLESMC_DATA_PORT);
290 for (i = 0; i < 4; i++) {
291 if (__wait_status(0x05))
293 key[i] = inb(APPLESMC_DATA_PORT);
301 * applesmc_get_key_type - get key type, and put the result in type (char[6]).
302 * Returns zero on success or a negative error on failure. Callers must
303 * hold applesmc_lock.
305 static int applesmc_get_key_type(char* key, char* type)
309 outb(APPLESMC_GET_KEY_TYPE_CMD, APPLESMC_CMD_PORT);
310 if (__wait_status(0x0c))
313 for (i = 0; i < 4; i++) {
314 outb(key[i], APPLESMC_DATA_PORT);
315 if (__wait_status(0x04))
319 outb(5, APPLESMC_DATA_PORT);
321 for (i = 0; i < 6; i++) {
322 if (__wait_status(0x05))
324 type[i] = inb(APPLESMC_DATA_PORT);
332 * applesmc_read_motion_sensor - Read motion sensor (X, Y or Z). Callers must
333 * hold applesmc_lock.
335 static int applesmc_read_motion_sensor(int index, s16* value)
342 ret = applesmc_read_key(MOTION_SENSOR_X_KEY, buffer, 2);
345 ret = applesmc_read_key(MOTION_SENSOR_Y_KEY, buffer, 2);
348 ret = applesmc_read_key(MOTION_SENSOR_Z_KEY, buffer, 2);
354 *value = ((s16)buffer[0] << 8) | buffer[1];
360 * applesmc_device_init - initialize the accelerometer. Returns zero on success
361 * and negative error code on failure. Can sleep.
363 static int applesmc_device_init(void)
365 int total, ret = -ENXIO;
368 if (!applesmc_accelerometer)
371 mutex_lock(&applesmc_lock);
373 for (total = INIT_TIMEOUT_MSECS; total > 0; total -= INIT_WAIT_MSECS) {
375 printk(KERN_DEBUG "applesmc try %d\n", total);
376 if (!applesmc_read_key(MOTION_SENSOR_KEY, buffer, 2) &&
377 (buffer[0] != 0x00 || buffer[1] != 0x00)) {
378 if (total == INIT_TIMEOUT_MSECS) {
379 printk(KERN_DEBUG "applesmc: device has"
380 " already been initialized"
381 " (0x%02x, 0x%02x).\n",
382 buffer[0], buffer[1]);
384 printk(KERN_DEBUG "applesmc: device"
385 " successfully initialized"
386 " (0x%02x, 0x%02x).\n",
387 buffer[0], buffer[1]);
394 applesmc_write_key(MOTION_SENSOR_KEY, buffer, 2);
395 msleep(INIT_WAIT_MSECS);
398 printk(KERN_WARNING "applesmc: failed to init the device\n");
401 mutex_unlock(&applesmc_lock);
406 * applesmc_get_fan_count - get the number of fans. Callers must NOT hold
409 static int applesmc_get_fan_count(void)
414 mutex_lock(&applesmc_lock);
416 ret = applesmc_read_key(FANS_COUNT, buffer, 1);
418 mutex_unlock(&applesmc_lock);
425 /* Device model stuff */
426 static int applesmc_probe(struct platform_device *dev)
430 ret = applesmc_device_init();
434 printk(KERN_INFO "applesmc: device successfully initialized.\n");
438 static int applesmc_resume(struct platform_device *dev)
440 return applesmc_device_init();
443 static struct platform_driver applesmc_driver = {
444 .probe = applesmc_probe,
445 .resume = applesmc_resume,
448 .owner = THIS_MODULE,
453 * applesmc_calibrate - Set our "resting" values. Callers must
454 * hold applesmc_lock.
456 static void applesmc_calibrate(void)
458 applesmc_read_motion_sensor(SENSOR_X, &rest_x);
459 applesmc_read_motion_sensor(SENSOR_Y, &rest_y);
463 static void applesmc_idev_poll(struct input_polled_dev *dev)
465 struct input_dev *idev = dev->input;
468 mutex_lock(&applesmc_lock);
470 if (applesmc_read_motion_sensor(SENSOR_X, &x))
472 if (applesmc_read_motion_sensor(SENSOR_Y, &y))
476 input_report_abs(idev, ABS_X, x - rest_x);
477 input_report_abs(idev, ABS_Y, y - rest_y);
481 mutex_unlock(&applesmc_lock);
486 static ssize_t applesmc_name_show(struct device *dev,
487 struct device_attribute *attr, char *buf)
489 return snprintf(buf, PAGE_SIZE, "applesmc\n");
492 static ssize_t applesmc_position_show(struct device *dev,
493 struct device_attribute *attr, char *buf)
498 mutex_lock(&applesmc_lock);
500 ret = applesmc_read_motion_sensor(SENSOR_X, &x);
503 ret = applesmc_read_motion_sensor(SENSOR_Y, &y);
506 ret = applesmc_read_motion_sensor(SENSOR_Z, &z);
511 mutex_unlock(&applesmc_lock);
515 return snprintf(buf, PAGE_SIZE, "(%d,%d,%d)\n", x, y, z);
518 static ssize_t applesmc_light_show(struct device *dev,
519 struct device_attribute *attr, char *sysfsbuf)
522 u8 left = 0, right = 0;
525 mutex_lock(&applesmc_lock);
527 ret = applesmc_read_key(LIGHT_SENSOR_LEFT_KEY, buffer, 6);
531 ret = applesmc_read_key(LIGHT_SENSOR_RIGHT_KEY, buffer, 6);
535 mutex_unlock(&applesmc_lock);
539 return snprintf(sysfsbuf, PAGE_SIZE, "(%d,%d)\n", left, right);
542 /* Displays degree Celsius * 1000 */
543 static ssize_t applesmc_show_temperature(struct device *dev,
544 struct device_attribute *devattr, char *sysfsbuf)
549 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
551 temperature_sensors_sets[applesmc_temperature_set][attr->index];
553 mutex_lock(&applesmc_lock);
555 ret = applesmc_read_key(key, buffer, 2);
556 temp = buffer[0]*1000;
557 temp += (buffer[1] >> 6) * 250;
559 mutex_unlock(&applesmc_lock);
564 return snprintf(sysfsbuf, PAGE_SIZE, "%u\n", temp);
567 static ssize_t applesmc_show_fan_speed(struct device *dev,
568 struct device_attribute *attr, char *sysfsbuf)
571 unsigned int speed = 0;
574 struct sensor_device_attribute_2 *sensor_attr =
575 to_sensor_dev_attr_2(attr);
577 newkey[0] = fan_speed_keys[sensor_attr->nr][0];
578 newkey[1] = '0' + sensor_attr->index;
579 newkey[2] = fan_speed_keys[sensor_attr->nr][2];
580 newkey[3] = fan_speed_keys[sensor_attr->nr][3];
583 mutex_lock(&applesmc_lock);
585 ret = applesmc_read_key(newkey, buffer, 2);
586 speed = ((buffer[0] << 8 | buffer[1]) >> 2);
588 mutex_unlock(&applesmc_lock);
592 return snprintf(sysfsbuf, PAGE_SIZE, "%u\n", speed);
595 static ssize_t applesmc_store_fan_speed(struct device *dev,
596 struct device_attribute *attr,
597 const char *sysfsbuf, size_t count)
603 struct sensor_device_attribute_2 *sensor_attr =
604 to_sensor_dev_attr_2(attr);
606 speed = simple_strtoul(sysfsbuf, NULL, 10);
608 if (speed > 0x4000) /* Bigger than a 14-bit value */
611 newkey[0] = fan_speed_keys[sensor_attr->nr][0];
612 newkey[1] = '0' + sensor_attr->index;
613 newkey[2] = fan_speed_keys[sensor_attr->nr][2];
614 newkey[3] = fan_speed_keys[sensor_attr->nr][3];
617 mutex_lock(&applesmc_lock);
619 buffer[0] = (speed >> 6) & 0xff;
620 buffer[1] = (speed << 2) & 0xff;
621 ret = applesmc_write_key(newkey, buffer, 2);
623 mutex_unlock(&applesmc_lock);
630 static ssize_t applesmc_show_fan_manual(struct device *dev,
631 struct device_attribute *devattr, char *sysfsbuf)
636 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
638 mutex_lock(&applesmc_lock);
640 ret = applesmc_read_key(FANS_MANUAL, buffer, 2);
641 manual = ((buffer[0] << 8 | buffer[1]) >> attr->index) & 0x01;
643 mutex_unlock(&applesmc_lock);
647 return snprintf(sysfsbuf, PAGE_SIZE, "%d\n", manual);
650 static ssize_t applesmc_store_fan_manual(struct device *dev,
651 struct device_attribute *devattr,
652 const char *sysfsbuf, size_t count)
658 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
660 input = simple_strtoul(sysfsbuf, NULL, 10);
662 mutex_lock(&applesmc_lock);
664 ret = applesmc_read_key(FANS_MANUAL, buffer, 2);
665 val = (buffer[0] << 8 | buffer[1]);
670 val = val | (0x01 << attr->index);
672 val = val & ~(0x01 << attr->index);
674 buffer[0] = (val >> 8) & 0xFF;
675 buffer[1] = val & 0xFF;
677 ret = applesmc_write_key(FANS_MANUAL, buffer, 2);
680 mutex_unlock(&applesmc_lock);
687 static ssize_t applesmc_show_fan_position(struct device *dev,
688 struct device_attribute *attr, char *sysfsbuf)
693 struct sensor_device_attribute_2 *sensor_attr =
694 to_sensor_dev_attr_2(attr);
696 newkey[0] = FAN_POSITION[0];
697 newkey[1] = '0' + sensor_attr->index;
698 newkey[2] = FAN_POSITION[2];
699 newkey[3] = FAN_POSITION[3];
702 mutex_lock(&applesmc_lock);
704 ret = applesmc_read_key(newkey, buffer, 16);
707 mutex_unlock(&applesmc_lock);
711 return snprintf(sysfsbuf, PAGE_SIZE, "%s\n", buffer+4);
714 static ssize_t applesmc_calibrate_show(struct device *dev,
715 struct device_attribute *attr, char *sysfsbuf)
717 return snprintf(sysfsbuf, PAGE_SIZE, "(%d,%d)\n", rest_x, rest_y);
720 static ssize_t applesmc_calibrate_store(struct device *dev,
721 struct device_attribute *attr, const char *sysfsbuf, size_t count)
723 mutex_lock(&applesmc_lock);
724 applesmc_calibrate();
725 mutex_unlock(&applesmc_lock);
730 /* Store the next backlight value to be written by the work */
731 static unsigned int backlight_value;
733 static void applesmc_backlight_set(struct work_struct *work)
737 mutex_lock(&applesmc_lock);
738 buffer[0] = backlight_value;
740 applesmc_write_key(BACKLIGHT_KEY, buffer, 2);
741 mutex_unlock(&applesmc_lock);
743 static DECLARE_WORK(backlight_work, &applesmc_backlight_set);
745 static void applesmc_brightness_set(struct led_classdev *led_cdev,
746 enum led_brightness value)
750 backlight_value = value;
751 ret = queue_work(applesmc_led_wq, &backlight_work);
754 printk(KERN_DEBUG "applesmc: work was already on the queue.\n");
757 static ssize_t applesmc_key_count_show(struct device *dev,
758 struct device_attribute *attr, char *sysfsbuf)
764 mutex_lock(&applesmc_lock);
766 ret = applesmc_read_key(KEY_COUNT_KEY, buffer, 4);
767 count = ((u32)buffer[0]<<24) + ((u32)buffer[1]<<16) +
768 ((u32)buffer[2]<<8) + buffer[3];
770 mutex_unlock(&applesmc_lock);
774 return snprintf(sysfsbuf, PAGE_SIZE, "%d\n", count);
777 static ssize_t applesmc_key_at_index_read_show(struct device *dev,
778 struct device_attribute *attr, char *sysfsbuf)
784 mutex_lock(&applesmc_lock);
786 ret = applesmc_get_key_at_index(key_at_index, key);
788 if (ret || !key[0]) {
789 mutex_unlock(&applesmc_lock);
794 ret = applesmc_get_key_type(key, info);
797 mutex_unlock(&applesmc_lock);
803 * info[0] maximum value (APPLESMC_MAX_DATA_LENGTH) is much lower than
804 * PAGE_SIZE, so we don't need any checks before writing to sysfsbuf.
806 ret = applesmc_read_key(key, sysfsbuf, info[0]);
808 mutex_unlock(&applesmc_lock);
817 static ssize_t applesmc_key_at_index_data_length_show(struct device *dev,
818 struct device_attribute *attr, char *sysfsbuf)
824 mutex_lock(&applesmc_lock);
826 ret = applesmc_get_key_at_index(key_at_index, key);
828 if (ret || !key[0]) {
829 mutex_unlock(&applesmc_lock);
834 ret = applesmc_get_key_type(key, info);
836 mutex_unlock(&applesmc_lock);
839 return snprintf(sysfsbuf, PAGE_SIZE, "%d\n", info[0]);
844 static ssize_t applesmc_key_at_index_type_show(struct device *dev,
845 struct device_attribute *attr, char *sysfsbuf)
851 mutex_lock(&applesmc_lock);
853 ret = applesmc_get_key_at_index(key_at_index, key);
855 if (ret || !key[0]) {
856 mutex_unlock(&applesmc_lock);
861 ret = applesmc_get_key_type(key, info);
863 mutex_unlock(&applesmc_lock);
866 return snprintf(sysfsbuf, PAGE_SIZE, "%s\n", info+1);
871 static ssize_t applesmc_key_at_index_name_show(struct device *dev,
872 struct device_attribute *attr, char *sysfsbuf)
877 mutex_lock(&applesmc_lock);
879 ret = applesmc_get_key_at_index(key_at_index, key);
881 mutex_unlock(&applesmc_lock);
884 return snprintf(sysfsbuf, PAGE_SIZE, "%s\n", key);
889 static ssize_t applesmc_key_at_index_show(struct device *dev,
890 struct device_attribute *attr, char *sysfsbuf)
892 return snprintf(sysfsbuf, PAGE_SIZE, "%d\n", key_at_index);
895 static ssize_t applesmc_key_at_index_store(struct device *dev,
896 struct device_attribute *attr, const char *sysfsbuf, size_t count)
898 mutex_lock(&applesmc_lock);
900 key_at_index = simple_strtoul(sysfsbuf, NULL, 10);
902 mutex_unlock(&applesmc_lock);
907 static struct led_classdev applesmc_backlight = {
908 .name = "smc::kbd_backlight",
909 .default_trigger = "nand-disk",
910 .brightness_set = applesmc_brightness_set,
913 static DEVICE_ATTR(name, 0444, applesmc_name_show, NULL);
915 static DEVICE_ATTR(position, 0444, applesmc_position_show, NULL);
916 static DEVICE_ATTR(calibrate, 0644,
917 applesmc_calibrate_show, applesmc_calibrate_store);
919 static struct attribute *accelerometer_attributes[] = {
920 &dev_attr_position.attr,
921 &dev_attr_calibrate.attr,
925 static const struct attribute_group accelerometer_attributes_group =
926 { .attrs = accelerometer_attributes };
928 static DEVICE_ATTR(light, 0444, applesmc_light_show, NULL);
930 static DEVICE_ATTR(key_count, 0444, applesmc_key_count_show, NULL);
931 static DEVICE_ATTR(key_at_index, 0644,
932 applesmc_key_at_index_show, applesmc_key_at_index_store);
933 static DEVICE_ATTR(key_at_index_name, 0444,
934 applesmc_key_at_index_name_show, NULL);
935 static DEVICE_ATTR(key_at_index_type, 0444,
936 applesmc_key_at_index_type_show, NULL);
937 static DEVICE_ATTR(key_at_index_data_length, 0444,
938 applesmc_key_at_index_data_length_show, NULL);
939 static DEVICE_ATTR(key_at_index_data, 0444,
940 applesmc_key_at_index_read_show, NULL);
942 static struct attribute *key_enumeration_attributes[] = {
943 &dev_attr_key_count.attr,
944 &dev_attr_key_at_index.attr,
945 &dev_attr_key_at_index_name.attr,
946 &dev_attr_key_at_index_type.attr,
947 &dev_attr_key_at_index_data_length.attr,
948 &dev_attr_key_at_index_data.attr,
952 static const struct attribute_group key_enumeration_group =
953 { .attrs = key_enumeration_attributes };
956 * Macro defining SENSOR_DEVICE_ATTR for a fan sysfs entries.
957 * - show actual speed
958 * - show/store minimum speed
959 * - show maximum speed
961 * - show/store target speed
962 * - show/store manual mode
964 #define sysfs_fan_speeds_offset(offset) \
965 static SENSOR_DEVICE_ATTR_2(fan##offset##_input, S_IRUGO, \
966 applesmc_show_fan_speed, NULL, 0, offset-1); \
968 static SENSOR_DEVICE_ATTR_2(fan##offset##_min, S_IRUGO | S_IWUSR, \
969 applesmc_show_fan_speed, applesmc_store_fan_speed, 1, offset-1); \
971 static SENSOR_DEVICE_ATTR_2(fan##offset##_max, S_IRUGO, \
972 applesmc_show_fan_speed, NULL, 2, offset-1); \
974 static SENSOR_DEVICE_ATTR_2(fan##offset##_safe, S_IRUGO, \
975 applesmc_show_fan_speed, NULL, 3, offset-1); \
977 static SENSOR_DEVICE_ATTR_2(fan##offset##_output, S_IRUGO | S_IWUSR, \
978 applesmc_show_fan_speed, applesmc_store_fan_speed, 4, offset-1); \
980 static SENSOR_DEVICE_ATTR(fan##offset##_manual, S_IRUGO | S_IWUSR, \
981 applesmc_show_fan_manual, applesmc_store_fan_manual, offset-1); \
983 static SENSOR_DEVICE_ATTR(fan##offset##_label, S_IRUGO, \
984 applesmc_show_fan_position, NULL, offset-1); \
986 static struct attribute *fan##offset##_attributes[] = { \
987 &sensor_dev_attr_fan##offset##_input.dev_attr.attr, \
988 &sensor_dev_attr_fan##offset##_min.dev_attr.attr, \
989 &sensor_dev_attr_fan##offset##_max.dev_attr.attr, \
990 &sensor_dev_attr_fan##offset##_safe.dev_attr.attr, \
991 &sensor_dev_attr_fan##offset##_output.dev_attr.attr, \
992 &sensor_dev_attr_fan##offset##_manual.dev_attr.attr, \
993 &sensor_dev_attr_fan##offset##_label.dev_attr.attr, \
998 * Create the needed functions for each fan using the macro defined above
999 * (4 fans are supported)
1001 sysfs_fan_speeds_offset(1);
1002 sysfs_fan_speeds_offset(2);
1003 sysfs_fan_speeds_offset(3);
1004 sysfs_fan_speeds_offset(4);
1006 static const struct attribute_group fan_attribute_groups[] = {
1007 { .attrs = fan1_attributes },
1008 { .attrs = fan2_attributes },
1009 { .attrs = fan3_attributes },
1010 { .attrs = fan4_attributes },
1014 * Temperature sensors sysfs entries.
1016 static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO,
1017 applesmc_show_temperature, NULL, 0);
1018 static SENSOR_DEVICE_ATTR(temp2_input, S_IRUGO,
1019 applesmc_show_temperature, NULL, 1);
1020 static SENSOR_DEVICE_ATTR(temp3_input, S_IRUGO,
1021 applesmc_show_temperature, NULL, 2);
1022 static SENSOR_DEVICE_ATTR(temp4_input, S_IRUGO,
1023 applesmc_show_temperature, NULL, 3);
1024 static SENSOR_DEVICE_ATTR(temp5_input, S_IRUGO,
1025 applesmc_show_temperature, NULL, 4);
1026 static SENSOR_DEVICE_ATTR(temp6_input, S_IRUGO,
1027 applesmc_show_temperature, NULL, 5);
1028 static SENSOR_DEVICE_ATTR(temp7_input, S_IRUGO,
1029 applesmc_show_temperature, NULL, 6);
1030 static SENSOR_DEVICE_ATTR(temp8_input, S_IRUGO,
1031 applesmc_show_temperature, NULL, 7);
1032 static SENSOR_DEVICE_ATTR(temp9_input, S_IRUGO,
1033 applesmc_show_temperature, NULL, 8);
1034 static SENSOR_DEVICE_ATTR(temp10_input, S_IRUGO,
1035 applesmc_show_temperature, NULL, 9);
1036 static SENSOR_DEVICE_ATTR(temp11_input, S_IRUGO,
1037 applesmc_show_temperature, NULL, 10);
1038 static SENSOR_DEVICE_ATTR(temp12_input, S_IRUGO,
1039 applesmc_show_temperature, NULL, 11);
1040 static SENSOR_DEVICE_ATTR(temp13_input, S_IRUGO,
1041 applesmc_show_temperature, NULL, 12);
1042 static SENSOR_DEVICE_ATTR(temp14_input, S_IRUGO,
1043 applesmc_show_temperature, NULL, 13);
1044 static SENSOR_DEVICE_ATTR(temp15_input, S_IRUGO,
1045 applesmc_show_temperature, NULL, 14);
1046 static SENSOR_DEVICE_ATTR(temp16_input, S_IRUGO,
1047 applesmc_show_temperature, NULL, 15);
1048 static SENSOR_DEVICE_ATTR(temp17_input, S_IRUGO,
1049 applesmc_show_temperature, NULL, 16);
1050 static SENSOR_DEVICE_ATTR(temp18_input, S_IRUGO,
1051 applesmc_show_temperature, NULL, 17);
1052 static SENSOR_DEVICE_ATTR(temp19_input, S_IRUGO,
1053 applesmc_show_temperature, NULL, 18);
1054 static SENSOR_DEVICE_ATTR(temp20_input, S_IRUGO,
1055 applesmc_show_temperature, NULL, 19);
1056 static SENSOR_DEVICE_ATTR(temp21_input, S_IRUGO,
1057 applesmc_show_temperature, NULL, 20);
1058 static SENSOR_DEVICE_ATTR(temp22_input, S_IRUGO,
1059 applesmc_show_temperature, NULL, 21);
1060 static SENSOR_DEVICE_ATTR(temp23_input, S_IRUGO,
1061 applesmc_show_temperature, NULL, 22);
1062 static SENSOR_DEVICE_ATTR(temp24_input, S_IRUGO,
1063 applesmc_show_temperature, NULL, 23);
1064 static SENSOR_DEVICE_ATTR(temp25_input, S_IRUGO,
1065 applesmc_show_temperature, NULL, 24);
1066 static SENSOR_DEVICE_ATTR(temp26_input, S_IRUGO,
1067 applesmc_show_temperature, NULL, 25);
1068 static SENSOR_DEVICE_ATTR(temp27_input, S_IRUGO,
1069 applesmc_show_temperature, NULL, 26);
1070 static SENSOR_DEVICE_ATTR(temp28_input, S_IRUGO,
1071 applesmc_show_temperature, NULL, 27);
1072 static SENSOR_DEVICE_ATTR(temp29_input, S_IRUGO,
1073 applesmc_show_temperature, NULL, 28);
1074 static SENSOR_DEVICE_ATTR(temp30_input, S_IRUGO,
1075 applesmc_show_temperature, NULL, 29);
1076 static SENSOR_DEVICE_ATTR(temp31_input, S_IRUGO,
1077 applesmc_show_temperature, NULL, 30);
1078 static SENSOR_DEVICE_ATTR(temp32_input, S_IRUGO,
1079 applesmc_show_temperature, NULL, 31);
1080 static SENSOR_DEVICE_ATTR(temp33_input, S_IRUGO,
1081 applesmc_show_temperature, NULL, 32);
1082 static SENSOR_DEVICE_ATTR(temp34_input, S_IRUGO,
1083 applesmc_show_temperature, NULL, 33);
1084 static SENSOR_DEVICE_ATTR(temp35_input, S_IRUGO,
1085 applesmc_show_temperature, NULL, 34);
1087 static struct attribute *temperature_attributes[] = {
1088 &sensor_dev_attr_temp1_input.dev_attr.attr,
1089 &sensor_dev_attr_temp2_input.dev_attr.attr,
1090 &sensor_dev_attr_temp3_input.dev_attr.attr,
1091 &sensor_dev_attr_temp4_input.dev_attr.attr,
1092 &sensor_dev_attr_temp5_input.dev_attr.attr,
1093 &sensor_dev_attr_temp6_input.dev_attr.attr,
1094 &sensor_dev_attr_temp7_input.dev_attr.attr,
1095 &sensor_dev_attr_temp8_input.dev_attr.attr,
1096 &sensor_dev_attr_temp9_input.dev_attr.attr,
1097 &sensor_dev_attr_temp10_input.dev_attr.attr,
1098 &sensor_dev_attr_temp11_input.dev_attr.attr,
1099 &sensor_dev_attr_temp12_input.dev_attr.attr,
1100 &sensor_dev_attr_temp13_input.dev_attr.attr,
1101 &sensor_dev_attr_temp14_input.dev_attr.attr,
1102 &sensor_dev_attr_temp15_input.dev_attr.attr,
1103 &sensor_dev_attr_temp16_input.dev_attr.attr,
1104 &sensor_dev_attr_temp17_input.dev_attr.attr,
1105 &sensor_dev_attr_temp18_input.dev_attr.attr,
1106 &sensor_dev_attr_temp19_input.dev_attr.attr,
1107 &sensor_dev_attr_temp20_input.dev_attr.attr,
1108 &sensor_dev_attr_temp21_input.dev_attr.attr,
1109 &sensor_dev_attr_temp22_input.dev_attr.attr,
1110 &sensor_dev_attr_temp23_input.dev_attr.attr,
1111 &sensor_dev_attr_temp24_input.dev_attr.attr,
1112 &sensor_dev_attr_temp25_input.dev_attr.attr,
1113 &sensor_dev_attr_temp26_input.dev_attr.attr,
1114 &sensor_dev_attr_temp27_input.dev_attr.attr,
1115 &sensor_dev_attr_temp28_input.dev_attr.attr,
1116 &sensor_dev_attr_temp29_input.dev_attr.attr,
1117 &sensor_dev_attr_temp30_input.dev_attr.attr,
1118 &sensor_dev_attr_temp31_input.dev_attr.attr,
1119 &sensor_dev_attr_temp32_input.dev_attr.attr,
1120 &sensor_dev_attr_temp33_input.dev_attr.attr,
1121 &sensor_dev_attr_temp34_input.dev_attr.attr,
1122 &sensor_dev_attr_temp35_input.dev_attr.attr,
1126 static const struct attribute_group temperature_attributes_group =
1127 { .attrs = temperature_attributes };
1132 * applesmc_dmi_match - found a match. return one, short-circuiting the hunt.
1134 static int applesmc_dmi_match(const struct dmi_system_id *id)
1137 struct dmi_match_data* dmi_data = id->driver_data;
1138 printk(KERN_INFO "applesmc: %s detected:\n", id->ident);
1139 applesmc_accelerometer = dmi_data->accelerometer;
1140 printk(KERN_INFO "applesmc: - Model %s accelerometer\n",
1141 applesmc_accelerometer ? "with" : "without");
1142 applesmc_light = dmi_data->light;
1143 printk(KERN_INFO "applesmc: - Model %s light sensors and backlight\n",
1144 applesmc_light ? "with" : "without");
1146 applesmc_temperature_set = dmi_data->temperature_set;
1147 while (temperature_sensors_sets[applesmc_temperature_set][i] != NULL)
1149 printk(KERN_INFO "applesmc: - Model with %d temperature sensors\n", i);
1153 /* Create accelerometer ressources */
1154 static int applesmc_create_accelerometer(void)
1156 struct input_dev *idev;
1159 ret = sysfs_create_group(&pdev->dev.kobj,
1160 &accelerometer_attributes_group);
1164 applesmc_idev = input_allocate_polled_device();
1165 if (!applesmc_idev) {
1170 applesmc_idev->poll = applesmc_idev_poll;
1171 applesmc_idev->poll_interval = APPLESMC_POLL_INTERVAL;
1173 /* initial calibrate for the input device */
1174 applesmc_calibrate();
1176 /* initialize the input device */
1177 idev = applesmc_idev->input;
1178 idev->name = "applesmc";
1179 idev->id.bustype = BUS_HOST;
1180 idev->dev.parent = &pdev->dev;
1181 idev->evbit[0] = BIT_MASK(EV_ABS);
1182 input_set_abs_params(idev, ABS_X,
1183 -256, 256, APPLESMC_INPUT_FUZZ, APPLESMC_INPUT_FLAT);
1184 input_set_abs_params(idev, ABS_Y,
1185 -256, 256, APPLESMC_INPUT_FUZZ, APPLESMC_INPUT_FLAT);
1187 ret = input_register_polled_device(applesmc_idev);
1194 input_free_polled_device(applesmc_idev);
1197 sysfs_remove_group(&pdev->dev.kobj, &accelerometer_attributes_group);
1200 printk(KERN_WARNING "applesmc: driver init failed (ret=%d)!\n", ret);
1204 /* Release all ressources used by the accelerometer */
1205 static void applesmc_release_accelerometer(void)
1207 input_unregister_polled_device(applesmc_idev);
1208 input_free_polled_device(applesmc_idev);
1209 sysfs_remove_group(&pdev->dev.kobj, &accelerometer_attributes_group);
1212 static __initdata struct dmi_match_data applesmc_dmi_data[] = {
1213 /* MacBook Pro: accelerometer, backlight and temperature set 0 */
1214 { .accelerometer = 1, .light = 1, .temperature_set = 0 },
1215 /* MacBook: accelerometer and temperature set 1 */
1216 { .accelerometer = 1, .light = 0, .temperature_set = 1 },
1217 /* MacMini: temperature set 2 */
1218 { .accelerometer = 0, .light = 0, .temperature_set = 2 },
1219 /* MacPro: temperature set 3 */
1220 { .accelerometer = 0, .light = 0, .temperature_set = 3 },
1223 /* Note that DMI_MATCH(...,"MacBook") will match "MacBookPro1,1".
1224 * So we need to put "Apple MacBook Pro" before "Apple MacBook". */
1225 static __initdata struct dmi_system_id applesmc_whitelist[] = {
1226 { applesmc_dmi_match, "Apple MacBook Pro", {
1227 DMI_MATCH(DMI_BOARD_VENDOR,"Apple"),
1228 DMI_MATCH(DMI_PRODUCT_NAME,"MacBookPro") },
1229 (void*)&applesmc_dmi_data[0]},
1230 { applesmc_dmi_match, "Apple MacBook", {
1231 DMI_MATCH(DMI_BOARD_VENDOR,"Apple"),
1232 DMI_MATCH(DMI_PRODUCT_NAME,"MacBook") },
1233 (void*)&applesmc_dmi_data[1]},
1234 { applesmc_dmi_match, "Apple Macmini", {
1235 DMI_MATCH(DMI_BOARD_VENDOR,"Apple"),
1236 DMI_MATCH(DMI_PRODUCT_NAME,"Macmini") },
1237 (void*)&applesmc_dmi_data[2]},
1238 { applesmc_dmi_match, "Apple MacPro2", {
1239 DMI_MATCH(DMI_BOARD_VENDOR,"Apple"),
1240 DMI_MATCH(DMI_PRODUCT_NAME,"MacPro2") },
1241 (void*)&applesmc_dmi_data[3]},
1245 static int __init applesmc_init(void)
1251 if (!dmi_check_system(applesmc_whitelist)) {
1252 printk(KERN_WARNING "applesmc: supported laptop not found!\n");
1257 if (!request_region(APPLESMC_DATA_PORT, APPLESMC_NR_PORTS,
1263 ret = platform_driver_register(&applesmc_driver);
1267 pdev = platform_device_register_simple("applesmc", APPLESMC_DATA_PORT,
1270 ret = PTR_ERR(pdev);
1274 ret = sysfs_create_file(&pdev->dev.kobj, &dev_attr_name.attr);
1278 /* Create key enumeration sysfs files */
1279 ret = sysfs_create_group(&pdev->dev.kobj, &key_enumeration_group);
1283 /* create fan files */
1284 count = applesmc_get_fan_count();
1286 printk(KERN_ERR "applesmc: Cannot get the number of fans.\n");
1288 printk(KERN_INFO "applesmc: %d fans found.\n", count);
1292 printk(KERN_WARNING "applesmc: More than 4 fans found,"
1293 " but at most 4 fans are supported"
1294 " by the driver.\n");
1296 ret = sysfs_create_group(&pdev->dev.kobj,
1297 &fan_attribute_groups[3]);
1299 goto out_key_enumeration;
1301 ret = sysfs_create_group(&pdev->dev.kobj,
1302 &fan_attribute_groups[2]);
1304 goto out_key_enumeration;
1306 ret = sysfs_create_group(&pdev->dev.kobj,
1307 &fan_attribute_groups[1]);
1309 goto out_key_enumeration;
1311 ret = sysfs_create_group(&pdev->dev.kobj,
1312 &fan_attribute_groups[0]);
1321 temperature_sensors_sets[applesmc_temperature_set][i] != NULL;
1323 if (temperature_attributes[i] == NULL) {
1324 printk(KERN_ERR "applesmc: More temperature sensors "
1325 "in temperature_sensors_sets (at least %i)"
1326 "than available sysfs files in "
1327 "temperature_attributes (%i), please report "
1328 "this bug.\n", i, i-1);
1329 goto out_temperature;
1331 ret = sysfs_create_file(&pdev->dev.kobj,
1332 temperature_attributes[i]);
1334 goto out_temperature;
1337 if (applesmc_accelerometer) {
1338 ret = applesmc_create_accelerometer();
1340 goto out_temperature;
1343 if (applesmc_light) {
1344 /* Add light sensor file */
1345 ret = sysfs_create_file(&pdev->dev.kobj, &dev_attr_light.attr);
1347 goto out_accelerometer;
1349 /* Create the workqueue */
1350 applesmc_led_wq = create_singlethread_workqueue("applesmc-led");
1351 if (!applesmc_led_wq) {
1353 goto out_light_sysfs;
1356 /* register as a led device */
1357 ret = led_classdev_register(&pdev->dev, &applesmc_backlight);
1362 hwmon_dev = hwmon_device_register(&pdev->dev);
1363 if (IS_ERR(hwmon_dev)) {
1364 ret = PTR_ERR(hwmon_dev);
1365 goto out_light_ledclass;
1368 printk(KERN_INFO "applesmc: driver successfully loaded.\n");
1374 led_classdev_unregister(&applesmc_backlight);
1377 destroy_workqueue(applesmc_led_wq);
1380 sysfs_remove_file(&pdev->dev.kobj, &dev_attr_light.attr);
1382 if (applesmc_accelerometer)
1383 applesmc_release_accelerometer();
1385 sysfs_remove_group(&pdev->dev.kobj, &temperature_attributes_group);
1386 sysfs_remove_group(&pdev->dev.kobj, &fan_attribute_groups[0]);
1388 sysfs_remove_group(&pdev->dev.kobj, &fan_attribute_groups[1]);
1389 out_key_enumeration:
1390 sysfs_remove_group(&pdev->dev.kobj, &key_enumeration_group);
1392 sysfs_remove_file(&pdev->dev.kobj, &dev_attr_name.attr);
1394 platform_device_unregister(pdev);
1396 platform_driver_unregister(&applesmc_driver);
1398 release_region(APPLESMC_DATA_PORT, APPLESMC_NR_PORTS);
1400 printk(KERN_WARNING "applesmc: driver init failed (ret=%d)!\n", ret);
1404 static void __exit applesmc_exit(void)
1406 hwmon_device_unregister(hwmon_dev);
1407 if (applesmc_light) {
1408 led_classdev_unregister(&applesmc_backlight);
1409 destroy_workqueue(applesmc_led_wq);
1410 sysfs_remove_file(&pdev->dev.kobj, &dev_attr_light.attr);
1412 if (applesmc_accelerometer)
1413 applesmc_release_accelerometer();
1414 sysfs_remove_group(&pdev->dev.kobj, &temperature_attributes_group);
1415 sysfs_remove_group(&pdev->dev.kobj, &fan_attribute_groups[0]);
1416 sysfs_remove_group(&pdev->dev.kobj, &fan_attribute_groups[1]);
1417 sysfs_remove_group(&pdev->dev.kobj, &key_enumeration_group);
1418 sysfs_remove_file(&pdev->dev.kobj, &dev_attr_name.attr);
1419 platform_device_unregister(pdev);
1420 platform_driver_unregister(&applesmc_driver);
1421 release_region(APPLESMC_DATA_PORT, APPLESMC_NR_PORTS);
1423 printk(KERN_INFO "applesmc: driver unloaded.\n");
1426 module_init(applesmc_init);
1427 module_exit(applesmc_exit);
1429 MODULE_AUTHOR("Nicolas Boichat");
1430 MODULE_DESCRIPTION("Apple SMC");
1431 MODULE_LICENSE("GPL v2");