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-10 bytes) */
61 #define LIGHT_SENSOR_RIGHT_KEY "ALV1" /* r-o {alv (6-10 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: Macbook2 set */
88 { "TB0T", "TC0D", "TC0P", "TM0P", "TN0P", "TN1P", "TTF0", "Th0H",
89 "Th0S", "Th1H", NULL },
90 /* Set 2: Macbook set */
91 { "TB0T", "TC0D", "TC0P", "TM0P", "TN0P", "TN1P", "Th0H", "Th0S",
92 "Th1H", "Ts0P", NULL },
93 /* Set 3: Macmini set */
94 { "TC0D", "TC0P", NULL },
95 /* Set 4: Mac Pro (2 x Quad-Core) */
96 { "TA0P", "TCAG", "TCAH", "TCBG", "TCBH", "TC0C", "TC0D", "TC0P",
97 "TC1C", "TC1D", "TC2C", "TC2D", "TC3C", "TC3D", "THTG", "TH0P",
98 "TH1P", "TH2P", "TH3P", "TMAP", "TMAS", "TMBS", "TM0P", "TM0S",
99 "TM1P", "TM1S", "TM2P", "TM2S", "TM3S", "TM8P", "TM8S", "TM9P",
100 "TM9S", "TN0H", "TS0C", NULL },
102 { "TC0D", "TA0P", "TG0P", "TG0D", "TG0H", "TH0P", "Tm0P", "TO0P",
104 /* Set 6: Macbook3 set */
105 { "TB0T", "TC0D", "TC0P", "TM0P", "TN0P", "TTF0", "TW0P", "Th0H",
106 "Th0S", "Th1H", NULL },
107 /* Set 7: Macbook Air */
108 { "TB0T", "TB1S", "TB1T", "TB2S", "TB2T", "TC0D", "TC0P", "TCFP",
109 "TTF0", "TW0P", "Th0H", "Tp0P", "TpFP", "Ts0P", "Ts0S", NULL },
110 /* Set 8: Macbook Pro 4,1 (Penryn) */
111 { "TB0T", "TC0D", "TC0P", "TG0D", "TG0H", "TTF0", "TW0P", "Th0H",
112 "Th1H", "Th2H", "Tm0P", "Ts0P", NULL },
115 /* List of keys used to read/write fan speeds */
116 static const char* fan_speed_keys[] = {
124 #define INIT_TIMEOUT_MSECS 5000 /* wait up to 5s for device init ... */
125 #define INIT_WAIT_MSECS 50 /* ... in 50ms increments */
127 #define APPLESMC_POLL_INTERVAL 50 /* msecs */
128 #define APPLESMC_INPUT_FUZZ 4 /* input event threshold */
129 #define APPLESMC_INPUT_FLAT 4
135 /* Structure to be passed to DMI_MATCH function */
136 struct dmi_match_data {
137 /* Indicates whether this computer has an accelerometer. */
139 /* Indicates whether this computer has light sensors and keyboard backlight. */
141 /* Indicates which temperature sensors set to use. */
145 static const int debug;
146 static struct platform_device *pdev;
149 static struct device *hwmon_dev;
150 static struct input_polled_dev *applesmc_idev;
152 /* Indicates whether this computer has an accelerometer. */
153 static unsigned int applesmc_accelerometer;
155 /* Indicates whether this computer has light sensors and keyboard backlight. */
156 static unsigned int applesmc_light;
158 /* Indicates which temperature sensors set to use. */
159 static unsigned int applesmc_temperature_set;
161 static DEFINE_MUTEX(applesmc_lock);
164 * Last index written to key_at_index sysfs file, and value to use for all other
165 * key_at_index_* sysfs files.
167 static unsigned int key_at_index;
169 static struct workqueue_struct *applesmc_led_wq;
172 * __wait_status - Wait up to 10ms for the status port to get a certain value
173 * (masked with 0x0f), returning zero if the value is obtained. Callers must
174 * hold applesmc_lock.
176 static int __wait_status(u8 val)
180 val = val & APPLESMC_STATUS_MASK;
182 for (i = 0; i < 1000; i++) {
183 if ((inb(APPLESMC_CMD_PORT) & APPLESMC_STATUS_MASK) == val) {
186 "Waited %d us for status %x\n",
193 printk(KERN_WARNING "applesmc: wait status failed: %x != %x\n",
194 val, inb(APPLESMC_CMD_PORT));
200 * special treatment of command port - on newer macbooks, it seems necessary
201 * to resend the command byte before polling the status again. Callers must
202 * hold applesmc_lock.
204 static int send_command(u8 cmd)
207 for (i = 0; i < 1000; i++) {
208 outb(cmd, APPLESMC_CMD_PORT);
210 if ((inb(APPLESMC_CMD_PORT) & APPLESMC_STATUS_MASK) == 0x0c)
214 printk(KERN_WARNING "applesmc: command failed: %x -> %x\n",
215 cmd, inb(APPLESMC_CMD_PORT));
220 * applesmc_read_key - reads len bytes from a given key, and put them in buffer.
221 * Returns zero on success or a negative error on failure. Callers must
222 * hold applesmc_lock.
224 static int applesmc_read_key(const char* key, u8* buffer, u8 len)
228 if (len > APPLESMC_MAX_DATA_LENGTH) {
229 printk(KERN_ERR "applesmc_read_key: cannot read more than "
230 "%d bytes\n", APPLESMC_MAX_DATA_LENGTH);
234 if (send_command(APPLESMC_READ_CMD))
237 for (i = 0; i < 4; i++) {
238 outb(key[i], APPLESMC_DATA_PORT);
239 if (__wait_status(0x04))
243 printk(KERN_DEBUG "<%s", key);
245 outb(len, APPLESMC_DATA_PORT);
247 printk(KERN_DEBUG ">%x", len);
249 for (i = 0; i < len; i++) {
250 if (__wait_status(0x05))
252 buffer[i] = inb(APPLESMC_DATA_PORT);
254 printk(KERN_DEBUG "<%x", buffer[i]);
257 printk(KERN_DEBUG "\n");
263 * applesmc_write_key - writes len bytes from buffer to a given key.
264 * Returns zero on success or a negative error on failure. Callers must
265 * hold applesmc_lock.
267 static int applesmc_write_key(const char* key, u8* buffer, u8 len)
271 if (len > APPLESMC_MAX_DATA_LENGTH) {
272 printk(KERN_ERR "applesmc_write_key: cannot write more than "
273 "%d bytes\n", APPLESMC_MAX_DATA_LENGTH);
277 if (send_command(APPLESMC_WRITE_CMD))
280 for (i = 0; i < 4; i++) {
281 outb(key[i], APPLESMC_DATA_PORT);
282 if (__wait_status(0x04))
286 outb(len, APPLESMC_DATA_PORT);
288 for (i = 0; i < len; i++) {
289 if (__wait_status(0x04))
291 outb(buffer[i], APPLESMC_DATA_PORT);
298 * applesmc_get_key_at_index - get key at index, and put the result in key
299 * (char[6]). Returns zero on success or a negative error on failure. Callers
300 * must hold applesmc_lock.
302 static int applesmc_get_key_at_index(int index, char* key)
306 readkey[0] = index >> 24;
307 readkey[1] = index >> 16;
308 readkey[2] = index >> 8;
311 if (send_command(APPLESMC_GET_KEY_BY_INDEX_CMD))
314 for (i = 0; i < 4; i++) {
315 outb(readkey[i], APPLESMC_DATA_PORT);
316 if (__wait_status(0x04))
320 outb(4, APPLESMC_DATA_PORT);
322 for (i = 0; i < 4; i++) {
323 if (__wait_status(0x05))
325 key[i] = inb(APPLESMC_DATA_PORT);
333 * applesmc_get_key_type - get key type, and put the result in type (char[6]).
334 * Returns zero on success or a negative error on failure. Callers must
335 * hold applesmc_lock.
337 static int applesmc_get_key_type(char* key, char* type)
341 if (send_command(APPLESMC_GET_KEY_TYPE_CMD))
344 for (i = 0; i < 4; i++) {
345 outb(key[i], APPLESMC_DATA_PORT);
346 if (__wait_status(0x04))
350 outb(6, APPLESMC_DATA_PORT);
352 for (i = 0; i < 6; i++) {
353 if (__wait_status(0x05))
355 type[i] = inb(APPLESMC_DATA_PORT);
363 * applesmc_read_motion_sensor - Read motion sensor (X, Y or Z). Callers must
364 * hold applesmc_lock.
366 static int applesmc_read_motion_sensor(int index, s16* value)
373 ret = applesmc_read_key(MOTION_SENSOR_X_KEY, buffer, 2);
376 ret = applesmc_read_key(MOTION_SENSOR_Y_KEY, buffer, 2);
379 ret = applesmc_read_key(MOTION_SENSOR_Z_KEY, buffer, 2);
385 *value = ((s16)buffer[0] << 8) | buffer[1];
391 * applesmc_device_init - initialize the accelerometer. Returns zero on success
392 * and negative error code on failure. Can sleep.
394 static int applesmc_device_init(void)
396 int total, ret = -ENXIO;
399 if (!applesmc_accelerometer)
402 mutex_lock(&applesmc_lock);
404 for (total = INIT_TIMEOUT_MSECS; total > 0; total -= INIT_WAIT_MSECS) {
406 printk(KERN_DEBUG "applesmc try %d\n", total);
407 if (!applesmc_read_key(MOTION_SENSOR_KEY, buffer, 2) &&
408 (buffer[0] != 0x00 || buffer[1] != 0x00)) {
409 if (total == INIT_TIMEOUT_MSECS) {
410 printk(KERN_DEBUG "applesmc: device has"
411 " already been initialized"
412 " (0x%02x, 0x%02x).\n",
413 buffer[0], buffer[1]);
415 printk(KERN_DEBUG "applesmc: device"
416 " successfully initialized"
417 " (0x%02x, 0x%02x).\n",
418 buffer[0], buffer[1]);
425 applesmc_write_key(MOTION_SENSOR_KEY, buffer, 2);
426 msleep(INIT_WAIT_MSECS);
429 printk(KERN_WARNING "applesmc: failed to init the device\n");
432 mutex_unlock(&applesmc_lock);
437 * applesmc_get_fan_count - get the number of fans. Callers must NOT hold
440 static int applesmc_get_fan_count(void)
445 mutex_lock(&applesmc_lock);
447 ret = applesmc_read_key(FANS_COUNT, buffer, 1);
449 mutex_unlock(&applesmc_lock);
456 /* Device model stuff */
457 static int applesmc_probe(struct platform_device *dev)
461 ret = applesmc_device_init();
465 printk(KERN_INFO "applesmc: device successfully initialized.\n");
469 static int applesmc_resume(struct platform_device *dev)
471 return applesmc_device_init();
474 static struct platform_driver applesmc_driver = {
475 .probe = applesmc_probe,
476 .resume = applesmc_resume,
479 .owner = THIS_MODULE,
484 * applesmc_calibrate - Set our "resting" values. Callers must
485 * hold applesmc_lock.
487 static void applesmc_calibrate(void)
489 applesmc_read_motion_sensor(SENSOR_X, &rest_x);
490 applesmc_read_motion_sensor(SENSOR_Y, &rest_y);
494 static void applesmc_idev_poll(struct input_polled_dev *dev)
496 struct input_dev *idev = dev->input;
499 mutex_lock(&applesmc_lock);
501 if (applesmc_read_motion_sensor(SENSOR_X, &x))
503 if (applesmc_read_motion_sensor(SENSOR_Y, &y))
507 input_report_abs(idev, ABS_X, x - rest_x);
508 input_report_abs(idev, ABS_Y, y - rest_y);
512 mutex_unlock(&applesmc_lock);
517 static ssize_t applesmc_name_show(struct device *dev,
518 struct device_attribute *attr, char *buf)
520 return snprintf(buf, PAGE_SIZE, "applesmc\n");
523 static ssize_t applesmc_position_show(struct device *dev,
524 struct device_attribute *attr, char *buf)
529 mutex_lock(&applesmc_lock);
531 ret = applesmc_read_motion_sensor(SENSOR_X, &x);
534 ret = applesmc_read_motion_sensor(SENSOR_Y, &y);
537 ret = applesmc_read_motion_sensor(SENSOR_Z, &z);
542 mutex_unlock(&applesmc_lock);
546 return snprintf(buf, PAGE_SIZE, "(%d,%d,%d)\n", x, y, z);
549 static ssize_t applesmc_light_show(struct device *dev,
550 struct device_attribute *attr, char *sysfsbuf)
552 static int data_length;
554 u8 left = 0, right = 0;
555 u8 buffer[10], query[6];
557 mutex_lock(&applesmc_lock);
560 ret = applesmc_get_key_type(LIGHT_SENSOR_LEFT_KEY, query);
563 data_length = clamp_val(query[0], 0, 10);
564 printk(KERN_INFO "applesmc: light sensor data length set to "
565 "%d\n", data_length);
568 ret = applesmc_read_key(LIGHT_SENSOR_LEFT_KEY, buffer, data_length);
572 ret = applesmc_read_key(LIGHT_SENSOR_RIGHT_KEY, buffer, data_length);
576 mutex_unlock(&applesmc_lock);
580 return snprintf(sysfsbuf, PAGE_SIZE, "(%d,%d)\n", left, right);
583 /* Displays degree Celsius * 1000 */
584 static ssize_t applesmc_show_temperature(struct device *dev,
585 struct device_attribute *devattr, char *sysfsbuf)
590 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
592 temperature_sensors_sets[applesmc_temperature_set][attr->index];
594 mutex_lock(&applesmc_lock);
596 ret = applesmc_read_key(key, buffer, 2);
597 temp = buffer[0]*1000;
598 temp += (buffer[1] >> 6) * 250;
600 mutex_unlock(&applesmc_lock);
605 return snprintf(sysfsbuf, PAGE_SIZE, "%u\n", temp);
608 static ssize_t applesmc_show_fan_speed(struct device *dev,
609 struct device_attribute *attr, char *sysfsbuf)
612 unsigned int speed = 0;
615 struct sensor_device_attribute_2 *sensor_attr =
616 to_sensor_dev_attr_2(attr);
618 newkey[0] = fan_speed_keys[sensor_attr->nr][0];
619 newkey[1] = '0' + sensor_attr->index;
620 newkey[2] = fan_speed_keys[sensor_attr->nr][2];
621 newkey[3] = fan_speed_keys[sensor_attr->nr][3];
624 mutex_lock(&applesmc_lock);
626 ret = applesmc_read_key(newkey, buffer, 2);
627 speed = ((buffer[0] << 8 | buffer[1]) >> 2);
629 mutex_unlock(&applesmc_lock);
633 return snprintf(sysfsbuf, PAGE_SIZE, "%u\n", speed);
636 static ssize_t applesmc_store_fan_speed(struct device *dev,
637 struct device_attribute *attr,
638 const char *sysfsbuf, size_t count)
644 struct sensor_device_attribute_2 *sensor_attr =
645 to_sensor_dev_attr_2(attr);
647 speed = simple_strtoul(sysfsbuf, NULL, 10);
649 if (speed > 0x4000) /* Bigger than a 14-bit value */
652 newkey[0] = fan_speed_keys[sensor_attr->nr][0];
653 newkey[1] = '0' + sensor_attr->index;
654 newkey[2] = fan_speed_keys[sensor_attr->nr][2];
655 newkey[3] = fan_speed_keys[sensor_attr->nr][3];
658 mutex_lock(&applesmc_lock);
660 buffer[0] = (speed >> 6) & 0xff;
661 buffer[1] = (speed << 2) & 0xff;
662 ret = applesmc_write_key(newkey, buffer, 2);
664 mutex_unlock(&applesmc_lock);
671 static ssize_t applesmc_show_fan_manual(struct device *dev,
672 struct device_attribute *devattr, char *sysfsbuf)
677 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
679 mutex_lock(&applesmc_lock);
681 ret = applesmc_read_key(FANS_MANUAL, buffer, 2);
682 manual = ((buffer[0] << 8 | buffer[1]) >> attr->index) & 0x01;
684 mutex_unlock(&applesmc_lock);
688 return snprintf(sysfsbuf, PAGE_SIZE, "%d\n", manual);
691 static ssize_t applesmc_store_fan_manual(struct device *dev,
692 struct device_attribute *devattr,
693 const char *sysfsbuf, size_t count)
699 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
701 input = simple_strtoul(sysfsbuf, NULL, 10);
703 mutex_lock(&applesmc_lock);
705 ret = applesmc_read_key(FANS_MANUAL, buffer, 2);
706 val = (buffer[0] << 8 | buffer[1]);
711 val = val | (0x01 << attr->index);
713 val = val & ~(0x01 << attr->index);
715 buffer[0] = (val >> 8) & 0xFF;
716 buffer[1] = val & 0xFF;
718 ret = applesmc_write_key(FANS_MANUAL, buffer, 2);
721 mutex_unlock(&applesmc_lock);
728 static ssize_t applesmc_show_fan_position(struct device *dev,
729 struct device_attribute *attr, char *sysfsbuf)
734 struct sensor_device_attribute_2 *sensor_attr =
735 to_sensor_dev_attr_2(attr);
737 newkey[0] = FAN_POSITION[0];
738 newkey[1] = '0' + sensor_attr->index;
739 newkey[2] = FAN_POSITION[2];
740 newkey[3] = FAN_POSITION[3];
743 mutex_lock(&applesmc_lock);
745 ret = applesmc_read_key(newkey, buffer, 16);
748 mutex_unlock(&applesmc_lock);
752 return snprintf(sysfsbuf, PAGE_SIZE, "%s\n", buffer+4);
755 static ssize_t applesmc_calibrate_show(struct device *dev,
756 struct device_attribute *attr, char *sysfsbuf)
758 return snprintf(sysfsbuf, PAGE_SIZE, "(%d,%d)\n", rest_x, rest_y);
761 static ssize_t applesmc_calibrate_store(struct device *dev,
762 struct device_attribute *attr, const char *sysfsbuf, size_t count)
764 mutex_lock(&applesmc_lock);
765 applesmc_calibrate();
766 mutex_unlock(&applesmc_lock);
771 /* Store the next backlight value to be written by the work */
772 static unsigned int backlight_value;
774 static void applesmc_backlight_set(struct work_struct *work)
778 mutex_lock(&applesmc_lock);
779 buffer[0] = backlight_value;
781 applesmc_write_key(BACKLIGHT_KEY, buffer, 2);
782 mutex_unlock(&applesmc_lock);
784 static DECLARE_WORK(backlight_work, &applesmc_backlight_set);
786 static void applesmc_brightness_set(struct led_classdev *led_cdev,
787 enum led_brightness value)
791 backlight_value = value;
792 ret = queue_work(applesmc_led_wq, &backlight_work);
795 printk(KERN_DEBUG "applesmc: work was already on the queue.\n");
798 static ssize_t applesmc_key_count_show(struct device *dev,
799 struct device_attribute *attr, char *sysfsbuf)
805 mutex_lock(&applesmc_lock);
807 ret = applesmc_read_key(KEY_COUNT_KEY, buffer, 4);
808 count = ((u32)buffer[0]<<24) + ((u32)buffer[1]<<16) +
809 ((u32)buffer[2]<<8) + buffer[3];
811 mutex_unlock(&applesmc_lock);
815 return snprintf(sysfsbuf, PAGE_SIZE, "%d\n", count);
818 static ssize_t applesmc_key_at_index_read_show(struct device *dev,
819 struct device_attribute *attr, char *sysfsbuf)
825 mutex_lock(&applesmc_lock);
827 ret = applesmc_get_key_at_index(key_at_index, key);
829 if (ret || !key[0]) {
830 mutex_unlock(&applesmc_lock);
835 ret = applesmc_get_key_type(key, info);
838 mutex_unlock(&applesmc_lock);
844 * info[0] maximum value (APPLESMC_MAX_DATA_LENGTH) is much lower than
845 * PAGE_SIZE, so we don't need any checks before writing to sysfsbuf.
847 ret = applesmc_read_key(key, sysfsbuf, info[0]);
849 mutex_unlock(&applesmc_lock);
858 static ssize_t applesmc_key_at_index_data_length_show(struct device *dev,
859 struct device_attribute *attr, char *sysfsbuf)
865 mutex_lock(&applesmc_lock);
867 ret = applesmc_get_key_at_index(key_at_index, key);
869 if (ret || !key[0]) {
870 mutex_unlock(&applesmc_lock);
875 ret = applesmc_get_key_type(key, info);
877 mutex_unlock(&applesmc_lock);
880 return snprintf(sysfsbuf, PAGE_SIZE, "%d\n", info[0]);
885 static ssize_t applesmc_key_at_index_type_show(struct device *dev,
886 struct device_attribute *attr, char *sysfsbuf)
892 mutex_lock(&applesmc_lock);
894 ret = applesmc_get_key_at_index(key_at_index, key);
896 if (ret || !key[0]) {
897 mutex_unlock(&applesmc_lock);
902 ret = applesmc_get_key_type(key, info);
904 mutex_unlock(&applesmc_lock);
907 return snprintf(sysfsbuf, PAGE_SIZE, "%s\n", info+1);
912 static ssize_t applesmc_key_at_index_name_show(struct device *dev,
913 struct device_attribute *attr, char *sysfsbuf)
918 mutex_lock(&applesmc_lock);
920 ret = applesmc_get_key_at_index(key_at_index, key);
922 mutex_unlock(&applesmc_lock);
925 return snprintf(sysfsbuf, PAGE_SIZE, "%s\n", key);
930 static ssize_t applesmc_key_at_index_show(struct device *dev,
931 struct device_attribute *attr, char *sysfsbuf)
933 return snprintf(sysfsbuf, PAGE_SIZE, "%d\n", key_at_index);
936 static ssize_t applesmc_key_at_index_store(struct device *dev,
937 struct device_attribute *attr, const char *sysfsbuf, size_t count)
939 mutex_lock(&applesmc_lock);
941 key_at_index = simple_strtoul(sysfsbuf, NULL, 10);
943 mutex_unlock(&applesmc_lock);
948 static struct led_classdev applesmc_backlight = {
949 .name = "smc::kbd_backlight",
950 .default_trigger = "nand-disk",
951 .brightness_set = applesmc_brightness_set,
954 static DEVICE_ATTR(name, 0444, applesmc_name_show, NULL);
956 static DEVICE_ATTR(position, 0444, applesmc_position_show, NULL);
957 static DEVICE_ATTR(calibrate, 0644,
958 applesmc_calibrate_show, applesmc_calibrate_store);
960 static struct attribute *accelerometer_attributes[] = {
961 &dev_attr_position.attr,
962 &dev_attr_calibrate.attr,
966 static const struct attribute_group accelerometer_attributes_group =
967 { .attrs = accelerometer_attributes };
969 static DEVICE_ATTR(light, 0444, applesmc_light_show, NULL);
971 static DEVICE_ATTR(key_count, 0444, applesmc_key_count_show, NULL);
972 static DEVICE_ATTR(key_at_index, 0644,
973 applesmc_key_at_index_show, applesmc_key_at_index_store);
974 static DEVICE_ATTR(key_at_index_name, 0444,
975 applesmc_key_at_index_name_show, NULL);
976 static DEVICE_ATTR(key_at_index_type, 0444,
977 applesmc_key_at_index_type_show, NULL);
978 static DEVICE_ATTR(key_at_index_data_length, 0444,
979 applesmc_key_at_index_data_length_show, NULL);
980 static DEVICE_ATTR(key_at_index_data, 0444,
981 applesmc_key_at_index_read_show, NULL);
983 static struct attribute *key_enumeration_attributes[] = {
984 &dev_attr_key_count.attr,
985 &dev_attr_key_at_index.attr,
986 &dev_attr_key_at_index_name.attr,
987 &dev_attr_key_at_index_type.attr,
988 &dev_attr_key_at_index_data_length.attr,
989 &dev_attr_key_at_index_data.attr,
993 static const struct attribute_group key_enumeration_group =
994 { .attrs = key_enumeration_attributes };
997 * Macro defining SENSOR_DEVICE_ATTR for a fan sysfs entries.
998 * - show actual speed
999 * - show/store minimum speed
1000 * - show maximum speed
1002 * - show/store target speed
1003 * - show/store manual mode
1005 #define sysfs_fan_speeds_offset(offset) \
1006 static SENSOR_DEVICE_ATTR_2(fan##offset##_input, S_IRUGO, \
1007 applesmc_show_fan_speed, NULL, 0, offset-1); \
1009 static SENSOR_DEVICE_ATTR_2(fan##offset##_min, S_IRUGO | S_IWUSR, \
1010 applesmc_show_fan_speed, applesmc_store_fan_speed, 1, offset-1); \
1012 static SENSOR_DEVICE_ATTR_2(fan##offset##_max, S_IRUGO, \
1013 applesmc_show_fan_speed, NULL, 2, offset-1); \
1015 static SENSOR_DEVICE_ATTR_2(fan##offset##_safe, S_IRUGO, \
1016 applesmc_show_fan_speed, NULL, 3, offset-1); \
1018 static SENSOR_DEVICE_ATTR_2(fan##offset##_output, S_IRUGO | S_IWUSR, \
1019 applesmc_show_fan_speed, applesmc_store_fan_speed, 4, offset-1); \
1021 static SENSOR_DEVICE_ATTR(fan##offset##_manual, S_IRUGO | S_IWUSR, \
1022 applesmc_show_fan_manual, applesmc_store_fan_manual, offset-1); \
1024 static SENSOR_DEVICE_ATTR(fan##offset##_label, S_IRUGO, \
1025 applesmc_show_fan_position, NULL, offset-1); \
1027 static struct attribute *fan##offset##_attributes[] = { \
1028 &sensor_dev_attr_fan##offset##_input.dev_attr.attr, \
1029 &sensor_dev_attr_fan##offset##_min.dev_attr.attr, \
1030 &sensor_dev_attr_fan##offset##_max.dev_attr.attr, \
1031 &sensor_dev_attr_fan##offset##_safe.dev_attr.attr, \
1032 &sensor_dev_attr_fan##offset##_output.dev_attr.attr, \
1033 &sensor_dev_attr_fan##offset##_manual.dev_attr.attr, \
1034 &sensor_dev_attr_fan##offset##_label.dev_attr.attr, \
1039 * Create the needed functions for each fan using the macro defined above
1040 * (4 fans are supported)
1042 sysfs_fan_speeds_offset(1);
1043 sysfs_fan_speeds_offset(2);
1044 sysfs_fan_speeds_offset(3);
1045 sysfs_fan_speeds_offset(4);
1047 static const struct attribute_group fan_attribute_groups[] = {
1048 { .attrs = fan1_attributes },
1049 { .attrs = fan2_attributes },
1050 { .attrs = fan3_attributes },
1051 { .attrs = fan4_attributes },
1055 * Temperature sensors sysfs entries.
1057 static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO,
1058 applesmc_show_temperature, NULL, 0);
1059 static SENSOR_DEVICE_ATTR(temp2_input, S_IRUGO,
1060 applesmc_show_temperature, NULL, 1);
1061 static SENSOR_DEVICE_ATTR(temp3_input, S_IRUGO,
1062 applesmc_show_temperature, NULL, 2);
1063 static SENSOR_DEVICE_ATTR(temp4_input, S_IRUGO,
1064 applesmc_show_temperature, NULL, 3);
1065 static SENSOR_DEVICE_ATTR(temp5_input, S_IRUGO,
1066 applesmc_show_temperature, NULL, 4);
1067 static SENSOR_DEVICE_ATTR(temp6_input, S_IRUGO,
1068 applesmc_show_temperature, NULL, 5);
1069 static SENSOR_DEVICE_ATTR(temp7_input, S_IRUGO,
1070 applesmc_show_temperature, NULL, 6);
1071 static SENSOR_DEVICE_ATTR(temp8_input, S_IRUGO,
1072 applesmc_show_temperature, NULL, 7);
1073 static SENSOR_DEVICE_ATTR(temp9_input, S_IRUGO,
1074 applesmc_show_temperature, NULL, 8);
1075 static SENSOR_DEVICE_ATTR(temp10_input, S_IRUGO,
1076 applesmc_show_temperature, NULL, 9);
1077 static SENSOR_DEVICE_ATTR(temp11_input, S_IRUGO,
1078 applesmc_show_temperature, NULL, 10);
1079 static SENSOR_DEVICE_ATTR(temp12_input, S_IRUGO,
1080 applesmc_show_temperature, NULL, 11);
1081 static SENSOR_DEVICE_ATTR(temp13_input, S_IRUGO,
1082 applesmc_show_temperature, NULL, 12);
1083 static SENSOR_DEVICE_ATTR(temp14_input, S_IRUGO,
1084 applesmc_show_temperature, NULL, 13);
1085 static SENSOR_DEVICE_ATTR(temp15_input, S_IRUGO,
1086 applesmc_show_temperature, NULL, 14);
1087 static SENSOR_DEVICE_ATTR(temp16_input, S_IRUGO,
1088 applesmc_show_temperature, NULL, 15);
1089 static SENSOR_DEVICE_ATTR(temp17_input, S_IRUGO,
1090 applesmc_show_temperature, NULL, 16);
1091 static SENSOR_DEVICE_ATTR(temp18_input, S_IRUGO,
1092 applesmc_show_temperature, NULL, 17);
1093 static SENSOR_DEVICE_ATTR(temp19_input, S_IRUGO,
1094 applesmc_show_temperature, NULL, 18);
1095 static SENSOR_DEVICE_ATTR(temp20_input, S_IRUGO,
1096 applesmc_show_temperature, NULL, 19);
1097 static SENSOR_DEVICE_ATTR(temp21_input, S_IRUGO,
1098 applesmc_show_temperature, NULL, 20);
1099 static SENSOR_DEVICE_ATTR(temp22_input, S_IRUGO,
1100 applesmc_show_temperature, NULL, 21);
1101 static SENSOR_DEVICE_ATTR(temp23_input, S_IRUGO,
1102 applesmc_show_temperature, NULL, 22);
1103 static SENSOR_DEVICE_ATTR(temp24_input, S_IRUGO,
1104 applesmc_show_temperature, NULL, 23);
1105 static SENSOR_DEVICE_ATTR(temp25_input, S_IRUGO,
1106 applesmc_show_temperature, NULL, 24);
1107 static SENSOR_DEVICE_ATTR(temp26_input, S_IRUGO,
1108 applesmc_show_temperature, NULL, 25);
1109 static SENSOR_DEVICE_ATTR(temp27_input, S_IRUGO,
1110 applesmc_show_temperature, NULL, 26);
1111 static SENSOR_DEVICE_ATTR(temp28_input, S_IRUGO,
1112 applesmc_show_temperature, NULL, 27);
1113 static SENSOR_DEVICE_ATTR(temp29_input, S_IRUGO,
1114 applesmc_show_temperature, NULL, 28);
1115 static SENSOR_DEVICE_ATTR(temp30_input, S_IRUGO,
1116 applesmc_show_temperature, NULL, 29);
1117 static SENSOR_DEVICE_ATTR(temp31_input, S_IRUGO,
1118 applesmc_show_temperature, NULL, 30);
1119 static SENSOR_DEVICE_ATTR(temp32_input, S_IRUGO,
1120 applesmc_show_temperature, NULL, 31);
1121 static SENSOR_DEVICE_ATTR(temp33_input, S_IRUGO,
1122 applesmc_show_temperature, NULL, 32);
1123 static SENSOR_DEVICE_ATTR(temp34_input, S_IRUGO,
1124 applesmc_show_temperature, NULL, 33);
1125 static SENSOR_DEVICE_ATTR(temp35_input, S_IRUGO,
1126 applesmc_show_temperature, NULL, 34);
1128 static struct attribute *temperature_attributes[] = {
1129 &sensor_dev_attr_temp1_input.dev_attr.attr,
1130 &sensor_dev_attr_temp2_input.dev_attr.attr,
1131 &sensor_dev_attr_temp3_input.dev_attr.attr,
1132 &sensor_dev_attr_temp4_input.dev_attr.attr,
1133 &sensor_dev_attr_temp5_input.dev_attr.attr,
1134 &sensor_dev_attr_temp6_input.dev_attr.attr,
1135 &sensor_dev_attr_temp7_input.dev_attr.attr,
1136 &sensor_dev_attr_temp8_input.dev_attr.attr,
1137 &sensor_dev_attr_temp9_input.dev_attr.attr,
1138 &sensor_dev_attr_temp10_input.dev_attr.attr,
1139 &sensor_dev_attr_temp11_input.dev_attr.attr,
1140 &sensor_dev_attr_temp12_input.dev_attr.attr,
1141 &sensor_dev_attr_temp13_input.dev_attr.attr,
1142 &sensor_dev_attr_temp14_input.dev_attr.attr,
1143 &sensor_dev_attr_temp15_input.dev_attr.attr,
1144 &sensor_dev_attr_temp16_input.dev_attr.attr,
1145 &sensor_dev_attr_temp17_input.dev_attr.attr,
1146 &sensor_dev_attr_temp18_input.dev_attr.attr,
1147 &sensor_dev_attr_temp19_input.dev_attr.attr,
1148 &sensor_dev_attr_temp20_input.dev_attr.attr,
1149 &sensor_dev_attr_temp21_input.dev_attr.attr,
1150 &sensor_dev_attr_temp22_input.dev_attr.attr,
1151 &sensor_dev_attr_temp23_input.dev_attr.attr,
1152 &sensor_dev_attr_temp24_input.dev_attr.attr,
1153 &sensor_dev_attr_temp25_input.dev_attr.attr,
1154 &sensor_dev_attr_temp26_input.dev_attr.attr,
1155 &sensor_dev_attr_temp27_input.dev_attr.attr,
1156 &sensor_dev_attr_temp28_input.dev_attr.attr,
1157 &sensor_dev_attr_temp29_input.dev_attr.attr,
1158 &sensor_dev_attr_temp30_input.dev_attr.attr,
1159 &sensor_dev_attr_temp31_input.dev_attr.attr,
1160 &sensor_dev_attr_temp32_input.dev_attr.attr,
1161 &sensor_dev_attr_temp33_input.dev_attr.attr,
1162 &sensor_dev_attr_temp34_input.dev_attr.attr,
1163 &sensor_dev_attr_temp35_input.dev_attr.attr,
1167 static const struct attribute_group temperature_attributes_group =
1168 { .attrs = temperature_attributes };
1173 * applesmc_dmi_match - found a match. return one, short-circuiting the hunt.
1175 static int applesmc_dmi_match(const struct dmi_system_id *id)
1178 struct dmi_match_data* dmi_data = id->driver_data;
1179 printk(KERN_INFO "applesmc: %s detected:\n", id->ident);
1180 applesmc_accelerometer = dmi_data->accelerometer;
1181 printk(KERN_INFO "applesmc: - Model %s accelerometer\n",
1182 applesmc_accelerometer ? "with" : "without");
1183 applesmc_light = dmi_data->light;
1184 printk(KERN_INFO "applesmc: - Model %s light sensors and backlight\n",
1185 applesmc_light ? "with" : "without");
1187 applesmc_temperature_set = dmi_data->temperature_set;
1188 while (temperature_sensors_sets[applesmc_temperature_set][i] != NULL)
1190 printk(KERN_INFO "applesmc: - Model with %d temperature sensors\n", i);
1194 /* Create accelerometer ressources */
1195 static int applesmc_create_accelerometer(void)
1197 struct input_dev *idev;
1200 ret = sysfs_create_group(&pdev->dev.kobj,
1201 &accelerometer_attributes_group);
1205 applesmc_idev = input_allocate_polled_device();
1206 if (!applesmc_idev) {
1211 applesmc_idev->poll = applesmc_idev_poll;
1212 applesmc_idev->poll_interval = APPLESMC_POLL_INTERVAL;
1214 /* initial calibrate for the input device */
1215 applesmc_calibrate();
1217 /* initialize the input device */
1218 idev = applesmc_idev->input;
1219 idev->name = "applesmc";
1220 idev->id.bustype = BUS_HOST;
1221 idev->dev.parent = &pdev->dev;
1222 idev->evbit[0] = BIT_MASK(EV_ABS);
1223 input_set_abs_params(idev, ABS_X,
1224 -256, 256, APPLESMC_INPUT_FUZZ, APPLESMC_INPUT_FLAT);
1225 input_set_abs_params(idev, ABS_Y,
1226 -256, 256, APPLESMC_INPUT_FUZZ, APPLESMC_INPUT_FLAT);
1228 ret = input_register_polled_device(applesmc_idev);
1235 input_free_polled_device(applesmc_idev);
1238 sysfs_remove_group(&pdev->dev.kobj, &accelerometer_attributes_group);
1241 printk(KERN_WARNING "applesmc: driver init failed (ret=%d)!\n", ret);
1245 /* Release all ressources used by the accelerometer */
1246 static void applesmc_release_accelerometer(void)
1248 input_unregister_polled_device(applesmc_idev);
1249 input_free_polled_device(applesmc_idev);
1250 sysfs_remove_group(&pdev->dev.kobj, &accelerometer_attributes_group);
1253 static __initdata struct dmi_match_data applesmc_dmi_data[] = {
1254 /* MacBook Pro: accelerometer, backlight and temperature set 0 */
1255 { .accelerometer = 1, .light = 1, .temperature_set = 0 },
1256 /* MacBook2: accelerometer and temperature set 1 */
1257 { .accelerometer = 1, .light = 0, .temperature_set = 1 },
1258 /* MacBook: accelerometer and temperature set 2 */
1259 { .accelerometer = 1, .light = 0, .temperature_set = 2 },
1260 /* MacMini: temperature set 3 */
1261 { .accelerometer = 0, .light = 0, .temperature_set = 3 },
1262 /* MacPro: temperature set 4 */
1263 { .accelerometer = 0, .light = 0, .temperature_set = 4 },
1264 /* iMac: temperature set 5 */
1265 { .accelerometer = 0, .light = 0, .temperature_set = 5 },
1266 /* MacBook3: accelerometer and temperature set 6 */
1267 { .accelerometer = 1, .light = 0, .temperature_set = 6 },
1268 /* MacBook Air: accelerometer, backlight and temperature set 7 */
1269 { .accelerometer = 1, .light = 1, .temperature_set = 7 },
1270 /* MacBook Pro 4: accelerometer, backlight and temperature set 8 */
1271 { .accelerometer = 1, .light = 1, .temperature_set = 8 },
1274 /* Note that DMI_MATCH(...,"MacBook") will match "MacBookPro1,1".
1275 * So we need to put "Apple MacBook Pro" before "Apple MacBook". */
1276 static __initdata struct dmi_system_id applesmc_whitelist[] = {
1277 { applesmc_dmi_match, "Apple MacBook Air", {
1278 DMI_MATCH(DMI_BOARD_VENDOR, "Apple"),
1279 DMI_MATCH(DMI_PRODUCT_NAME, "MacBookAir") },
1280 &applesmc_dmi_data[7]},
1281 { applesmc_dmi_match, "Apple MacBook Pro 4", {
1282 DMI_MATCH(DMI_BOARD_VENDOR, "Apple"),
1283 DMI_MATCH(DMI_PRODUCT_NAME, "MacBookPro4") },
1284 &applesmc_dmi_data[8]},
1285 { applesmc_dmi_match, "Apple MacBook Pro", {
1286 DMI_MATCH(DMI_BOARD_VENDOR,"Apple"),
1287 DMI_MATCH(DMI_PRODUCT_NAME,"MacBookPro") },
1288 &applesmc_dmi_data[0]},
1289 { applesmc_dmi_match, "Apple MacBook (v2)", {
1290 DMI_MATCH(DMI_BOARD_VENDOR,"Apple"),
1291 DMI_MATCH(DMI_PRODUCT_NAME,"MacBook2") },
1292 &applesmc_dmi_data[1]},
1293 { applesmc_dmi_match, "Apple MacBook (v3)", {
1294 DMI_MATCH(DMI_BOARD_VENDOR,"Apple"),
1295 DMI_MATCH(DMI_PRODUCT_NAME,"MacBook3") },
1296 &applesmc_dmi_data[6]},
1297 { applesmc_dmi_match, "Apple MacBook", {
1298 DMI_MATCH(DMI_BOARD_VENDOR,"Apple"),
1299 DMI_MATCH(DMI_PRODUCT_NAME,"MacBook") },
1300 &applesmc_dmi_data[2]},
1301 { applesmc_dmi_match, "Apple Macmini", {
1302 DMI_MATCH(DMI_BOARD_VENDOR,"Apple"),
1303 DMI_MATCH(DMI_PRODUCT_NAME,"Macmini") },
1304 &applesmc_dmi_data[3]},
1305 { applesmc_dmi_match, "Apple MacPro2", {
1306 DMI_MATCH(DMI_BOARD_VENDOR,"Apple"),
1307 DMI_MATCH(DMI_PRODUCT_NAME,"MacPro2") },
1308 &applesmc_dmi_data[4]},
1309 { applesmc_dmi_match, "Apple iMac", {
1310 DMI_MATCH(DMI_BOARD_VENDOR,"Apple"),
1311 DMI_MATCH(DMI_PRODUCT_NAME,"iMac") },
1312 &applesmc_dmi_data[5]},
1316 static int __init applesmc_init(void)
1322 if (!dmi_check_system(applesmc_whitelist)) {
1323 printk(KERN_WARNING "applesmc: supported laptop not found!\n");
1328 if (!request_region(APPLESMC_DATA_PORT, APPLESMC_NR_PORTS,
1334 ret = platform_driver_register(&applesmc_driver);
1338 pdev = platform_device_register_simple("applesmc", APPLESMC_DATA_PORT,
1341 ret = PTR_ERR(pdev);
1345 ret = sysfs_create_file(&pdev->dev.kobj, &dev_attr_name.attr);
1349 /* Create key enumeration sysfs files */
1350 ret = sysfs_create_group(&pdev->dev.kobj, &key_enumeration_group);
1354 /* create fan files */
1355 count = applesmc_get_fan_count();
1357 printk(KERN_ERR "applesmc: Cannot get the number of fans.\n");
1359 printk(KERN_INFO "applesmc: %d fans found.\n", count);
1363 printk(KERN_WARNING "applesmc: More than 4 fans found,"
1364 " but at most 4 fans are supported"
1365 " by the driver.\n");
1367 ret = sysfs_create_group(&pdev->dev.kobj,
1368 &fan_attribute_groups[3]);
1370 goto out_key_enumeration;
1372 ret = sysfs_create_group(&pdev->dev.kobj,
1373 &fan_attribute_groups[2]);
1375 goto out_key_enumeration;
1377 ret = sysfs_create_group(&pdev->dev.kobj,
1378 &fan_attribute_groups[1]);
1380 goto out_key_enumeration;
1382 ret = sysfs_create_group(&pdev->dev.kobj,
1383 &fan_attribute_groups[0]);
1392 temperature_sensors_sets[applesmc_temperature_set][i] != NULL;
1394 if (temperature_attributes[i] == NULL) {
1395 printk(KERN_ERR "applesmc: More temperature sensors "
1396 "in temperature_sensors_sets (at least %i)"
1397 "than available sysfs files in "
1398 "temperature_attributes (%i), please report "
1399 "this bug.\n", i, i-1);
1400 goto out_temperature;
1402 ret = sysfs_create_file(&pdev->dev.kobj,
1403 temperature_attributes[i]);
1405 goto out_temperature;
1408 if (applesmc_accelerometer) {
1409 ret = applesmc_create_accelerometer();
1411 goto out_temperature;
1414 if (applesmc_light) {
1415 /* Add light sensor file */
1416 ret = sysfs_create_file(&pdev->dev.kobj, &dev_attr_light.attr);
1418 goto out_accelerometer;
1420 /* Create the workqueue */
1421 applesmc_led_wq = create_singlethread_workqueue("applesmc-led");
1422 if (!applesmc_led_wq) {
1424 goto out_light_sysfs;
1427 /* register as a led device */
1428 ret = led_classdev_register(&pdev->dev, &applesmc_backlight);
1433 hwmon_dev = hwmon_device_register(&pdev->dev);
1434 if (IS_ERR(hwmon_dev)) {
1435 ret = PTR_ERR(hwmon_dev);
1436 goto out_light_ledclass;
1439 printk(KERN_INFO "applesmc: driver successfully loaded.\n");
1445 led_classdev_unregister(&applesmc_backlight);
1448 destroy_workqueue(applesmc_led_wq);
1451 sysfs_remove_file(&pdev->dev.kobj, &dev_attr_light.attr);
1453 if (applesmc_accelerometer)
1454 applesmc_release_accelerometer();
1456 sysfs_remove_group(&pdev->dev.kobj, &temperature_attributes_group);
1457 sysfs_remove_group(&pdev->dev.kobj, &fan_attribute_groups[0]);
1459 sysfs_remove_group(&pdev->dev.kobj, &fan_attribute_groups[1]);
1460 out_key_enumeration:
1461 sysfs_remove_group(&pdev->dev.kobj, &key_enumeration_group);
1463 sysfs_remove_file(&pdev->dev.kobj, &dev_attr_name.attr);
1465 platform_device_unregister(pdev);
1467 platform_driver_unregister(&applesmc_driver);
1469 release_region(APPLESMC_DATA_PORT, APPLESMC_NR_PORTS);
1471 printk(KERN_WARNING "applesmc: driver init failed (ret=%d)!\n", ret);
1475 static void __exit applesmc_exit(void)
1477 hwmon_device_unregister(hwmon_dev);
1478 if (applesmc_light) {
1479 led_classdev_unregister(&applesmc_backlight);
1480 destroy_workqueue(applesmc_led_wq);
1481 sysfs_remove_file(&pdev->dev.kobj, &dev_attr_light.attr);
1483 if (applesmc_accelerometer)
1484 applesmc_release_accelerometer();
1485 sysfs_remove_group(&pdev->dev.kobj, &temperature_attributes_group);
1486 sysfs_remove_group(&pdev->dev.kobj, &fan_attribute_groups[0]);
1487 sysfs_remove_group(&pdev->dev.kobj, &fan_attribute_groups[1]);
1488 sysfs_remove_group(&pdev->dev.kobj, &key_enumeration_group);
1489 sysfs_remove_file(&pdev->dev.kobj, &dev_attr_name.attr);
1490 platform_device_unregister(pdev);
1491 platform_driver_unregister(&applesmc_driver);
1492 release_region(APPLESMC_DATA_PORT, APPLESMC_NR_PORTS);
1494 printk(KERN_INFO "applesmc: driver unloaded.\n");
1497 module_init(applesmc_init);
1498 module_exit(applesmc_exit);
1500 MODULE_AUTHOR("Nicolas Boichat");
1501 MODULE_DESCRIPTION("Apple SMC");
1502 MODULE_LICENSE("GPL v2");