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 },
113 /* Set 9: Macbook Pro 3,1 (Santa Rosa) */
114 { "TALP", "TB0T", "TC0D", "TC0P", "TG0D", "TG0H", "TTF0", "TW0P",
115 "Th0H", "Th1H", "Th2H", "Tm0P", "Ts0P", NULL },
118 /* List of keys used to read/write fan speeds */
119 static const char* fan_speed_keys[] = {
127 #define INIT_TIMEOUT_MSECS 5000 /* wait up to 5s for device init ... */
128 #define INIT_WAIT_MSECS 50 /* ... in 50ms increments */
130 #define APPLESMC_POLL_INTERVAL 50 /* msecs */
131 #define APPLESMC_INPUT_FUZZ 4 /* input event threshold */
132 #define APPLESMC_INPUT_FLAT 4
138 /* Structure to be passed to DMI_MATCH function */
139 struct dmi_match_data {
140 /* Indicates whether this computer has an accelerometer. */
142 /* Indicates whether this computer has light sensors and keyboard backlight. */
144 /* Indicates which temperature sensors set to use. */
148 static const int debug;
149 static struct platform_device *pdev;
152 static struct device *hwmon_dev;
153 static struct input_polled_dev *applesmc_idev;
155 /* Indicates whether this computer has an accelerometer. */
156 static unsigned int applesmc_accelerometer;
158 /* Indicates whether this computer has light sensors and keyboard backlight. */
159 static unsigned int applesmc_light;
161 /* Indicates which temperature sensors set to use. */
162 static unsigned int applesmc_temperature_set;
164 static DEFINE_MUTEX(applesmc_lock);
167 * Last index written to key_at_index sysfs file, and value to use for all other
168 * key_at_index_* sysfs files.
170 static unsigned int key_at_index;
172 static struct workqueue_struct *applesmc_led_wq;
175 * __wait_status - Wait up to 10ms for the status port to get a certain value
176 * (masked with 0x0f), returning zero if the value is obtained. Callers must
177 * hold applesmc_lock.
179 static int __wait_status(u8 val)
183 val = val & APPLESMC_STATUS_MASK;
185 for (i = 0; i < 1000; i++) {
186 if ((inb(APPLESMC_CMD_PORT) & APPLESMC_STATUS_MASK) == val) {
189 "Waited %d us for status %x\n",
196 printk(KERN_WARNING "applesmc: wait status failed: %x != %x\n",
197 val, inb(APPLESMC_CMD_PORT));
203 * special treatment of command port - on newer macbooks, it seems necessary
204 * to resend the command byte before polling the status again. Callers must
205 * hold applesmc_lock.
207 static int send_command(u8 cmd)
210 for (i = 0; i < 1000; i++) {
211 outb(cmd, APPLESMC_CMD_PORT);
213 if ((inb(APPLESMC_CMD_PORT) & APPLESMC_STATUS_MASK) == 0x0c)
217 printk(KERN_WARNING "applesmc: command failed: %x -> %x\n",
218 cmd, inb(APPLESMC_CMD_PORT));
223 * applesmc_read_key - reads len bytes from a given key, and put them in buffer.
224 * Returns zero on success or a negative error on failure. Callers must
225 * hold applesmc_lock.
227 static int applesmc_read_key(const char* key, u8* buffer, u8 len)
231 if (len > APPLESMC_MAX_DATA_LENGTH) {
232 printk(KERN_ERR "applesmc_read_key: cannot read more than "
233 "%d bytes\n", APPLESMC_MAX_DATA_LENGTH);
237 if (send_command(APPLESMC_READ_CMD))
240 for (i = 0; i < 4; i++) {
241 outb(key[i], APPLESMC_DATA_PORT);
242 if (__wait_status(0x04))
246 printk(KERN_DEBUG "<%s", key);
248 outb(len, APPLESMC_DATA_PORT);
250 printk(KERN_DEBUG ">%x", len);
252 for (i = 0; i < len; i++) {
253 if (__wait_status(0x05))
255 buffer[i] = inb(APPLESMC_DATA_PORT);
257 printk(KERN_DEBUG "<%x", buffer[i]);
260 printk(KERN_DEBUG "\n");
266 * applesmc_write_key - writes len bytes from buffer to a given key.
267 * Returns zero on success or a negative error on failure. Callers must
268 * hold applesmc_lock.
270 static int applesmc_write_key(const char* key, u8* buffer, u8 len)
274 if (len > APPLESMC_MAX_DATA_LENGTH) {
275 printk(KERN_ERR "applesmc_write_key: cannot write more than "
276 "%d bytes\n", APPLESMC_MAX_DATA_LENGTH);
280 if (send_command(APPLESMC_WRITE_CMD))
283 for (i = 0; i < 4; i++) {
284 outb(key[i], APPLESMC_DATA_PORT);
285 if (__wait_status(0x04))
289 outb(len, APPLESMC_DATA_PORT);
291 for (i = 0; i < len; i++) {
292 if (__wait_status(0x04))
294 outb(buffer[i], APPLESMC_DATA_PORT);
301 * applesmc_get_key_at_index - get key at index, and put the result in key
302 * (char[6]). Returns zero on success or a negative error on failure. Callers
303 * must hold applesmc_lock.
305 static int applesmc_get_key_at_index(int index, char* key)
309 readkey[0] = index >> 24;
310 readkey[1] = index >> 16;
311 readkey[2] = index >> 8;
314 if (send_command(APPLESMC_GET_KEY_BY_INDEX_CMD))
317 for (i = 0; i < 4; i++) {
318 outb(readkey[i], APPLESMC_DATA_PORT);
319 if (__wait_status(0x04))
323 outb(4, APPLESMC_DATA_PORT);
325 for (i = 0; i < 4; i++) {
326 if (__wait_status(0x05))
328 key[i] = inb(APPLESMC_DATA_PORT);
336 * applesmc_get_key_type - get key type, and put the result in type (char[6]).
337 * Returns zero on success or a negative error on failure. Callers must
338 * hold applesmc_lock.
340 static int applesmc_get_key_type(char* key, char* type)
344 if (send_command(APPLESMC_GET_KEY_TYPE_CMD))
347 for (i = 0; i < 4; i++) {
348 outb(key[i], APPLESMC_DATA_PORT);
349 if (__wait_status(0x04))
353 outb(6, APPLESMC_DATA_PORT);
355 for (i = 0; i < 6; i++) {
356 if (__wait_status(0x05))
358 type[i] = inb(APPLESMC_DATA_PORT);
366 * applesmc_read_motion_sensor - Read motion sensor (X, Y or Z). Callers must
367 * hold applesmc_lock.
369 static int applesmc_read_motion_sensor(int index, s16* value)
376 ret = applesmc_read_key(MOTION_SENSOR_X_KEY, buffer, 2);
379 ret = applesmc_read_key(MOTION_SENSOR_Y_KEY, buffer, 2);
382 ret = applesmc_read_key(MOTION_SENSOR_Z_KEY, buffer, 2);
388 *value = ((s16)buffer[0] << 8) | buffer[1];
394 * applesmc_device_init - initialize the accelerometer. Returns zero on success
395 * and negative error code on failure. Can sleep.
397 static int applesmc_device_init(void)
399 int total, ret = -ENXIO;
402 if (!applesmc_accelerometer)
405 mutex_lock(&applesmc_lock);
407 for (total = INIT_TIMEOUT_MSECS; total > 0; total -= INIT_WAIT_MSECS) {
409 printk(KERN_DEBUG "applesmc try %d\n", total);
410 if (!applesmc_read_key(MOTION_SENSOR_KEY, buffer, 2) &&
411 (buffer[0] != 0x00 || buffer[1] != 0x00)) {
412 if (total == INIT_TIMEOUT_MSECS) {
413 printk(KERN_DEBUG "applesmc: device has"
414 " already been initialized"
415 " (0x%02x, 0x%02x).\n",
416 buffer[0], buffer[1]);
418 printk(KERN_DEBUG "applesmc: device"
419 " successfully initialized"
420 " (0x%02x, 0x%02x).\n",
421 buffer[0], buffer[1]);
428 applesmc_write_key(MOTION_SENSOR_KEY, buffer, 2);
429 msleep(INIT_WAIT_MSECS);
432 printk(KERN_WARNING "applesmc: failed to init the device\n");
435 mutex_unlock(&applesmc_lock);
440 * applesmc_get_fan_count - get the number of fans. Callers must NOT hold
443 static int applesmc_get_fan_count(void)
448 mutex_lock(&applesmc_lock);
450 ret = applesmc_read_key(FANS_COUNT, buffer, 1);
452 mutex_unlock(&applesmc_lock);
459 /* Device model stuff */
460 static int applesmc_probe(struct platform_device *dev)
464 ret = applesmc_device_init();
468 printk(KERN_INFO "applesmc: device successfully initialized.\n");
472 static int applesmc_resume(struct platform_device *dev)
474 return applesmc_device_init();
477 static struct platform_driver applesmc_driver = {
478 .probe = applesmc_probe,
479 .resume = applesmc_resume,
482 .owner = THIS_MODULE,
487 * applesmc_calibrate - Set our "resting" values. Callers must
488 * hold applesmc_lock.
490 static void applesmc_calibrate(void)
492 applesmc_read_motion_sensor(SENSOR_X, &rest_x);
493 applesmc_read_motion_sensor(SENSOR_Y, &rest_y);
497 static void applesmc_idev_poll(struct input_polled_dev *dev)
499 struct input_dev *idev = dev->input;
502 mutex_lock(&applesmc_lock);
504 if (applesmc_read_motion_sensor(SENSOR_X, &x))
506 if (applesmc_read_motion_sensor(SENSOR_Y, &y))
510 input_report_abs(idev, ABS_X, x - rest_x);
511 input_report_abs(idev, ABS_Y, y - rest_y);
515 mutex_unlock(&applesmc_lock);
520 static ssize_t applesmc_name_show(struct device *dev,
521 struct device_attribute *attr, char *buf)
523 return snprintf(buf, PAGE_SIZE, "applesmc\n");
526 static ssize_t applesmc_position_show(struct device *dev,
527 struct device_attribute *attr, char *buf)
532 mutex_lock(&applesmc_lock);
534 ret = applesmc_read_motion_sensor(SENSOR_X, &x);
537 ret = applesmc_read_motion_sensor(SENSOR_Y, &y);
540 ret = applesmc_read_motion_sensor(SENSOR_Z, &z);
545 mutex_unlock(&applesmc_lock);
549 return snprintf(buf, PAGE_SIZE, "(%d,%d,%d)\n", x, y, z);
552 static ssize_t applesmc_light_show(struct device *dev,
553 struct device_attribute *attr, char *sysfsbuf)
555 static int data_length;
557 u8 left = 0, right = 0;
558 u8 buffer[10], query[6];
560 mutex_lock(&applesmc_lock);
563 ret = applesmc_get_key_type(LIGHT_SENSOR_LEFT_KEY, query);
566 data_length = clamp_val(query[0], 0, 10);
567 printk(KERN_INFO "applesmc: light sensor data length set to "
568 "%d\n", data_length);
571 ret = applesmc_read_key(LIGHT_SENSOR_LEFT_KEY, buffer, data_length);
575 ret = applesmc_read_key(LIGHT_SENSOR_RIGHT_KEY, buffer, data_length);
579 mutex_unlock(&applesmc_lock);
583 return snprintf(sysfsbuf, PAGE_SIZE, "(%d,%d)\n", left, right);
586 /* Displays degree Celsius * 1000 */
587 static ssize_t applesmc_show_temperature(struct device *dev,
588 struct device_attribute *devattr, char *sysfsbuf)
593 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
595 temperature_sensors_sets[applesmc_temperature_set][attr->index];
597 mutex_lock(&applesmc_lock);
599 ret = applesmc_read_key(key, buffer, 2);
600 temp = buffer[0]*1000;
601 temp += (buffer[1] >> 6) * 250;
603 mutex_unlock(&applesmc_lock);
608 return snprintf(sysfsbuf, PAGE_SIZE, "%u\n", temp);
611 static ssize_t applesmc_show_fan_speed(struct device *dev,
612 struct device_attribute *attr, char *sysfsbuf)
615 unsigned int speed = 0;
618 struct sensor_device_attribute_2 *sensor_attr =
619 to_sensor_dev_attr_2(attr);
621 newkey[0] = fan_speed_keys[sensor_attr->nr][0];
622 newkey[1] = '0' + sensor_attr->index;
623 newkey[2] = fan_speed_keys[sensor_attr->nr][2];
624 newkey[3] = fan_speed_keys[sensor_attr->nr][3];
627 mutex_lock(&applesmc_lock);
629 ret = applesmc_read_key(newkey, buffer, 2);
630 speed = ((buffer[0] << 8 | buffer[1]) >> 2);
632 mutex_unlock(&applesmc_lock);
636 return snprintf(sysfsbuf, PAGE_SIZE, "%u\n", speed);
639 static ssize_t applesmc_store_fan_speed(struct device *dev,
640 struct device_attribute *attr,
641 const char *sysfsbuf, size_t count)
647 struct sensor_device_attribute_2 *sensor_attr =
648 to_sensor_dev_attr_2(attr);
650 speed = simple_strtoul(sysfsbuf, NULL, 10);
652 if (speed > 0x4000) /* Bigger than a 14-bit value */
655 newkey[0] = fan_speed_keys[sensor_attr->nr][0];
656 newkey[1] = '0' + sensor_attr->index;
657 newkey[2] = fan_speed_keys[sensor_attr->nr][2];
658 newkey[3] = fan_speed_keys[sensor_attr->nr][3];
661 mutex_lock(&applesmc_lock);
663 buffer[0] = (speed >> 6) & 0xff;
664 buffer[1] = (speed << 2) & 0xff;
665 ret = applesmc_write_key(newkey, buffer, 2);
667 mutex_unlock(&applesmc_lock);
674 static ssize_t applesmc_show_fan_manual(struct device *dev,
675 struct device_attribute *devattr, char *sysfsbuf)
680 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
682 mutex_lock(&applesmc_lock);
684 ret = applesmc_read_key(FANS_MANUAL, buffer, 2);
685 manual = ((buffer[0] << 8 | buffer[1]) >> attr->index) & 0x01;
687 mutex_unlock(&applesmc_lock);
691 return snprintf(sysfsbuf, PAGE_SIZE, "%d\n", manual);
694 static ssize_t applesmc_store_fan_manual(struct device *dev,
695 struct device_attribute *devattr,
696 const char *sysfsbuf, size_t count)
702 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
704 input = simple_strtoul(sysfsbuf, NULL, 10);
706 mutex_lock(&applesmc_lock);
708 ret = applesmc_read_key(FANS_MANUAL, buffer, 2);
709 val = (buffer[0] << 8 | buffer[1]);
714 val = val | (0x01 << attr->index);
716 val = val & ~(0x01 << attr->index);
718 buffer[0] = (val >> 8) & 0xFF;
719 buffer[1] = val & 0xFF;
721 ret = applesmc_write_key(FANS_MANUAL, buffer, 2);
724 mutex_unlock(&applesmc_lock);
731 static ssize_t applesmc_show_fan_position(struct device *dev,
732 struct device_attribute *attr, char *sysfsbuf)
737 struct sensor_device_attribute_2 *sensor_attr =
738 to_sensor_dev_attr_2(attr);
740 newkey[0] = FAN_POSITION[0];
741 newkey[1] = '0' + sensor_attr->index;
742 newkey[2] = FAN_POSITION[2];
743 newkey[3] = FAN_POSITION[3];
746 mutex_lock(&applesmc_lock);
748 ret = applesmc_read_key(newkey, buffer, 16);
751 mutex_unlock(&applesmc_lock);
755 return snprintf(sysfsbuf, PAGE_SIZE, "%s\n", buffer+4);
758 static ssize_t applesmc_calibrate_show(struct device *dev,
759 struct device_attribute *attr, char *sysfsbuf)
761 return snprintf(sysfsbuf, PAGE_SIZE, "(%d,%d)\n", rest_x, rest_y);
764 static ssize_t applesmc_calibrate_store(struct device *dev,
765 struct device_attribute *attr, const char *sysfsbuf, size_t count)
767 mutex_lock(&applesmc_lock);
768 applesmc_calibrate();
769 mutex_unlock(&applesmc_lock);
774 /* Store the next backlight value to be written by the work */
775 static unsigned int backlight_value;
777 static void applesmc_backlight_set(struct work_struct *work)
781 mutex_lock(&applesmc_lock);
782 buffer[0] = backlight_value;
784 applesmc_write_key(BACKLIGHT_KEY, buffer, 2);
785 mutex_unlock(&applesmc_lock);
787 static DECLARE_WORK(backlight_work, &applesmc_backlight_set);
789 static void applesmc_brightness_set(struct led_classdev *led_cdev,
790 enum led_brightness value)
794 backlight_value = value;
795 ret = queue_work(applesmc_led_wq, &backlight_work);
798 printk(KERN_DEBUG "applesmc: work was already on the queue.\n");
801 static ssize_t applesmc_key_count_show(struct device *dev,
802 struct device_attribute *attr, char *sysfsbuf)
808 mutex_lock(&applesmc_lock);
810 ret = applesmc_read_key(KEY_COUNT_KEY, buffer, 4);
811 count = ((u32)buffer[0]<<24) + ((u32)buffer[1]<<16) +
812 ((u32)buffer[2]<<8) + buffer[3];
814 mutex_unlock(&applesmc_lock);
818 return snprintf(sysfsbuf, PAGE_SIZE, "%d\n", count);
821 static ssize_t applesmc_key_at_index_read_show(struct device *dev,
822 struct device_attribute *attr, char *sysfsbuf)
828 mutex_lock(&applesmc_lock);
830 ret = applesmc_get_key_at_index(key_at_index, key);
832 if (ret || !key[0]) {
833 mutex_unlock(&applesmc_lock);
838 ret = applesmc_get_key_type(key, info);
841 mutex_unlock(&applesmc_lock);
847 * info[0] maximum value (APPLESMC_MAX_DATA_LENGTH) is much lower than
848 * PAGE_SIZE, so we don't need any checks before writing to sysfsbuf.
850 ret = applesmc_read_key(key, sysfsbuf, info[0]);
852 mutex_unlock(&applesmc_lock);
861 static ssize_t applesmc_key_at_index_data_length_show(struct device *dev,
862 struct device_attribute *attr, char *sysfsbuf)
868 mutex_lock(&applesmc_lock);
870 ret = applesmc_get_key_at_index(key_at_index, key);
872 if (ret || !key[0]) {
873 mutex_unlock(&applesmc_lock);
878 ret = applesmc_get_key_type(key, info);
880 mutex_unlock(&applesmc_lock);
883 return snprintf(sysfsbuf, PAGE_SIZE, "%d\n", info[0]);
888 static ssize_t applesmc_key_at_index_type_show(struct device *dev,
889 struct device_attribute *attr, char *sysfsbuf)
895 mutex_lock(&applesmc_lock);
897 ret = applesmc_get_key_at_index(key_at_index, key);
899 if (ret || !key[0]) {
900 mutex_unlock(&applesmc_lock);
905 ret = applesmc_get_key_type(key, info);
907 mutex_unlock(&applesmc_lock);
910 return snprintf(sysfsbuf, PAGE_SIZE, "%s\n", info+1);
915 static ssize_t applesmc_key_at_index_name_show(struct device *dev,
916 struct device_attribute *attr, char *sysfsbuf)
921 mutex_lock(&applesmc_lock);
923 ret = applesmc_get_key_at_index(key_at_index, key);
925 mutex_unlock(&applesmc_lock);
928 return snprintf(sysfsbuf, PAGE_SIZE, "%s\n", key);
933 static ssize_t applesmc_key_at_index_show(struct device *dev,
934 struct device_attribute *attr, char *sysfsbuf)
936 return snprintf(sysfsbuf, PAGE_SIZE, "%d\n", key_at_index);
939 static ssize_t applesmc_key_at_index_store(struct device *dev,
940 struct device_attribute *attr, const char *sysfsbuf, size_t count)
942 mutex_lock(&applesmc_lock);
944 key_at_index = simple_strtoul(sysfsbuf, NULL, 10);
946 mutex_unlock(&applesmc_lock);
951 static struct led_classdev applesmc_backlight = {
952 .name = "smc::kbd_backlight",
953 .default_trigger = "nand-disk",
954 .brightness_set = applesmc_brightness_set,
957 static DEVICE_ATTR(name, 0444, applesmc_name_show, NULL);
959 static DEVICE_ATTR(position, 0444, applesmc_position_show, NULL);
960 static DEVICE_ATTR(calibrate, 0644,
961 applesmc_calibrate_show, applesmc_calibrate_store);
963 static struct attribute *accelerometer_attributes[] = {
964 &dev_attr_position.attr,
965 &dev_attr_calibrate.attr,
969 static const struct attribute_group accelerometer_attributes_group =
970 { .attrs = accelerometer_attributes };
972 static DEVICE_ATTR(light, 0444, applesmc_light_show, NULL);
974 static DEVICE_ATTR(key_count, 0444, applesmc_key_count_show, NULL);
975 static DEVICE_ATTR(key_at_index, 0644,
976 applesmc_key_at_index_show, applesmc_key_at_index_store);
977 static DEVICE_ATTR(key_at_index_name, 0444,
978 applesmc_key_at_index_name_show, NULL);
979 static DEVICE_ATTR(key_at_index_type, 0444,
980 applesmc_key_at_index_type_show, NULL);
981 static DEVICE_ATTR(key_at_index_data_length, 0444,
982 applesmc_key_at_index_data_length_show, NULL);
983 static DEVICE_ATTR(key_at_index_data, 0444,
984 applesmc_key_at_index_read_show, NULL);
986 static struct attribute *key_enumeration_attributes[] = {
987 &dev_attr_key_count.attr,
988 &dev_attr_key_at_index.attr,
989 &dev_attr_key_at_index_name.attr,
990 &dev_attr_key_at_index_type.attr,
991 &dev_attr_key_at_index_data_length.attr,
992 &dev_attr_key_at_index_data.attr,
996 static const struct attribute_group key_enumeration_group =
997 { .attrs = key_enumeration_attributes };
1000 * Macro defining SENSOR_DEVICE_ATTR for a fan sysfs entries.
1001 * - show actual speed
1002 * - show/store minimum speed
1003 * - show maximum speed
1005 * - show/store target speed
1006 * - show/store manual mode
1008 #define sysfs_fan_speeds_offset(offset) \
1009 static SENSOR_DEVICE_ATTR_2(fan##offset##_input, S_IRUGO, \
1010 applesmc_show_fan_speed, NULL, 0, offset-1); \
1012 static SENSOR_DEVICE_ATTR_2(fan##offset##_min, S_IRUGO | S_IWUSR, \
1013 applesmc_show_fan_speed, applesmc_store_fan_speed, 1, offset-1); \
1015 static SENSOR_DEVICE_ATTR_2(fan##offset##_max, S_IRUGO, \
1016 applesmc_show_fan_speed, NULL, 2, offset-1); \
1018 static SENSOR_DEVICE_ATTR_2(fan##offset##_safe, S_IRUGO, \
1019 applesmc_show_fan_speed, NULL, 3, offset-1); \
1021 static SENSOR_DEVICE_ATTR_2(fan##offset##_output, S_IRUGO | S_IWUSR, \
1022 applesmc_show_fan_speed, applesmc_store_fan_speed, 4, offset-1); \
1024 static SENSOR_DEVICE_ATTR(fan##offset##_manual, S_IRUGO | S_IWUSR, \
1025 applesmc_show_fan_manual, applesmc_store_fan_manual, offset-1); \
1027 static SENSOR_DEVICE_ATTR(fan##offset##_label, S_IRUGO, \
1028 applesmc_show_fan_position, NULL, offset-1); \
1030 static struct attribute *fan##offset##_attributes[] = { \
1031 &sensor_dev_attr_fan##offset##_input.dev_attr.attr, \
1032 &sensor_dev_attr_fan##offset##_min.dev_attr.attr, \
1033 &sensor_dev_attr_fan##offset##_max.dev_attr.attr, \
1034 &sensor_dev_attr_fan##offset##_safe.dev_attr.attr, \
1035 &sensor_dev_attr_fan##offset##_output.dev_attr.attr, \
1036 &sensor_dev_attr_fan##offset##_manual.dev_attr.attr, \
1037 &sensor_dev_attr_fan##offset##_label.dev_attr.attr, \
1042 * Create the needed functions for each fan using the macro defined above
1043 * (4 fans are supported)
1045 sysfs_fan_speeds_offset(1);
1046 sysfs_fan_speeds_offset(2);
1047 sysfs_fan_speeds_offset(3);
1048 sysfs_fan_speeds_offset(4);
1050 static const struct attribute_group fan_attribute_groups[] = {
1051 { .attrs = fan1_attributes },
1052 { .attrs = fan2_attributes },
1053 { .attrs = fan3_attributes },
1054 { .attrs = fan4_attributes },
1058 * Temperature sensors sysfs entries.
1060 static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO,
1061 applesmc_show_temperature, NULL, 0);
1062 static SENSOR_DEVICE_ATTR(temp2_input, S_IRUGO,
1063 applesmc_show_temperature, NULL, 1);
1064 static SENSOR_DEVICE_ATTR(temp3_input, S_IRUGO,
1065 applesmc_show_temperature, NULL, 2);
1066 static SENSOR_DEVICE_ATTR(temp4_input, S_IRUGO,
1067 applesmc_show_temperature, NULL, 3);
1068 static SENSOR_DEVICE_ATTR(temp5_input, S_IRUGO,
1069 applesmc_show_temperature, NULL, 4);
1070 static SENSOR_DEVICE_ATTR(temp6_input, S_IRUGO,
1071 applesmc_show_temperature, NULL, 5);
1072 static SENSOR_DEVICE_ATTR(temp7_input, S_IRUGO,
1073 applesmc_show_temperature, NULL, 6);
1074 static SENSOR_DEVICE_ATTR(temp8_input, S_IRUGO,
1075 applesmc_show_temperature, NULL, 7);
1076 static SENSOR_DEVICE_ATTR(temp9_input, S_IRUGO,
1077 applesmc_show_temperature, NULL, 8);
1078 static SENSOR_DEVICE_ATTR(temp10_input, S_IRUGO,
1079 applesmc_show_temperature, NULL, 9);
1080 static SENSOR_DEVICE_ATTR(temp11_input, S_IRUGO,
1081 applesmc_show_temperature, NULL, 10);
1082 static SENSOR_DEVICE_ATTR(temp12_input, S_IRUGO,
1083 applesmc_show_temperature, NULL, 11);
1084 static SENSOR_DEVICE_ATTR(temp13_input, S_IRUGO,
1085 applesmc_show_temperature, NULL, 12);
1086 static SENSOR_DEVICE_ATTR(temp14_input, S_IRUGO,
1087 applesmc_show_temperature, NULL, 13);
1088 static SENSOR_DEVICE_ATTR(temp15_input, S_IRUGO,
1089 applesmc_show_temperature, NULL, 14);
1090 static SENSOR_DEVICE_ATTR(temp16_input, S_IRUGO,
1091 applesmc_show_temperature, NULL, 15);
1092 static SENSOR_DEVICE_ATTR(temp17_input, S_IRUGO,
1093 applesmc_show_temperature, NULL, 16);
1094 static SENSOR_DEVICE_ATTR(temp18_input, S_IRUGO,
1095 applesmc_show_temperature, NULL, 17);
1096 static SENSOR_DEVICE_ATTR(temp19_input, S_IRUGO,
1097 applesmc_show_temperature, NULL, 18);
1098 static SENSOR_DEVICE_ATTR(temp20_input, S_IRUGO,
1099 applesmc_show_temperature, NULL, 19);
1100 static SENSOR_DEVICE_ATTR(temp21_input, S_IRUGO,
1101 applesmc_show_temperature, NULL, 20);
1102 static SENSOR_DEVICE_ATTR(temp22_input, S_IRUGO,
1103 applesmc_show_temperature, NULL, 21);
1104 static SENSOR_DEVICE_ATTR(temp23_input, S_IRUGO,
1105 applesmc_show_temperature, NULL, 22);
1106 static SENSOR_DEVICE_ATTR(temp24_input, S_IRUGO,
1107 applesmc_show_temperature, NULL, 23);
1108 static SENSOR_DEVICE_ATTR(temp25_input, S_IRUGO,
1109 applesmc_show_temperature, NULL, 24);
1110 static SENSOR_DEVICE_ATTR(temp26_input, S_IRUGO,
1111 applesmc_show_temperature, NULL, 25);
1112 static SENSOR_DEVICE_ATTR(temp27_input, S_IRUGO,
1113 applesmc_show_temperature, NULL, 26);
1114 static SENSOR_DEVICE_ATTR(temp28_input, S_IRUGO,
1115 applesmc_show_temperature, NULL, 27);
1116 static SENSOR_DEVICE_ATTR(temp29_input, S_IRUGO,
1117 applesmc_show_temperature, NULL, 28);
1118 static SENSOR_DEVICE_ATTR(temp30_input, S_IRUGO,
1119 applesmc_show_temperature, NULL, 29);
1120 static SENSOR_DEVICE_ATTR(temp31_input, S_IRUGO,
1121 applesmc_show_temperature, NULL, 30);
1122 static SENSOR_DEVICE_ATTR(temp32_input, S_IRUGO,
1123 applesmc_show_temperature, NULL, 31);
1124 static SENSOR_DEVICE_ATTR(temp33_input, S_IRUGO,
1125 applesmc_show_temperature, NULL, 32);
1126 static SENSOR_DEVICE_ATTR(temp34_input, S_IRUGO,
1127 applesmc_show_temperature, NULL, 33);
1128 static SENSOR_DEVICE_ATTR(temp35_input, S_IRUGO,
1129 applesmc_show_temperature, NULL, 34);
1131 static struct attribute *temperature_attributes[] = {
1132 &sensor_dev_attr_temp1_input.dev_attr.attr,
1133 &sensor_dev_attr_temp2_input.dev_attr.attr,
1134 &sensor_dev_attr_temp3_input.dev_attr.attr,
1135 &sensor_dev_attr_temp4_input.dev_attr.attr,
1136 &sensor_dev_attr_temp5_input.dev_attr.attr,
1137 &sensor_dev_attr_temp6_input.dev_attr.attr,
1138 &sensor_dev_attr_temp7_input.dev_attr.attr,
1139 &sensor_dev_attr_temp8_input.dev_attr.attr,
1140 &sensor_dev_attr_temp9_input.dev_attr.attr,
1141 &sensor_dev_attr_temp10_input.dev_attr.attr,
1142 &sensor_dev_attr_temp11_input.dev_attr.attr,
1143 &sensor_dev_attr_temp12_input.dev_attr.attr,
1144 &sensor_dev_attr_temp13_input.dev_attr.attr,
1145 &sensor_dev_attr_temp14_input.dev_attr.attr,
1146 &sensor_dev_attr_temp15_input.dev_attr.attr,
1147 &sensor_dev_attr_temp16_input.dev_attr.attr,
1148 &sensor_dev_attr_temp17_input.dev_attr.attr,
1149 &sensor_dev_attr_temp18_input.dev_attr.attr,
1150 &sensor_dev_attr_temp19_input.dev_attr.attr,
1151 &sensor_dev_attr_temp20_input.dev_attr.attr,
1152 &sensor_dev_attr_temp21_input.dev_attr.attr,
1153 &sensor_dev_attr_temp22_input.dev_attr.attr,
1154 &sensor_dev_attr_temp23_input.dev_attr.attr,
1155 &sensor_dev_attr_temp24_input.dev_attr.attr,
1156 &sensor_dev_attr_temp25_input.dev_attr.attr,
1157 &sensor_dev_attr_temp26_input.dev_attr.attr,
1158 &sensor_dev_attr_temp27_input.dev_attr.attr,
1159 &sensor_dev_attr_temp28_input.dev_attr.attr,
1160 &sensor_dev_attr_temp29_input.dev_attr.attr,
1161 &sensor_dev_attr_temp30_input.dev_attr.attr,
1162 &sensor_dev_attr_temp31_input.dev_attr.attr,
1163 &sensor_dev_attr_temp32_input.dev_attr.attr,
1164 &sensor_dev_attr_temp33_input.dev_attr.attr,
1165 &sensor_dev_attr_temp34_input.dev_attr.attr,
1166 &sensor_dev_attr_temp35_input.dev_attr.attr,
1170 static const struct attribute_group temperature_attributes_group =
1171 { .attrs = temperature_attributes };
1176 * applesmc_dmi_match - found a match. return one, short-circuiting the hunt.
1178 static int applesmc_dmi_match(const struct dmi_system_id *id)
1181 struct dmi_match_data* dmi_data = id->driver_data;
1182 printk(KERN_INFO "applesmc: %s detected:\n", id->ident);
1183 applesmc_accelerometer = dmi_data->accelerometer;
1184 printk(KERN_INFO "applesmc: - Model %s accelerometer\n",
1185 applesmc_accelerometer ? "with" : "without");
1186 applesmc_light = dmi_data->light;
1187 printk(KERN_INFO "applesmc: - Model %s light sensors and backlight\n",
1188 applesmc_light ? "with" : "without");
1190 applesmc_temperature_set = dmi_data->temperature_set;
1191 while (temperature_sensors_sets[applesmc_temperature_set][i] != NULL)
1193 printk(KERN_INFO "applesmc: - Model with %d temperature sensors\n", i);
1197 /* Create accelerometer ressources */
1198 static int applesmc_create_accelerometer(void)
1200 struct input_dev *idev;
1203 ret = sysfs_create_group(&pdev->dev.kobj,
1204 &accelerometer_attributes_group);
1208 applesmc_idev = input_allocate_polled_device();
1209 if (!applesmc_idev) {
1214 applesmc_idev->poll = applesmc_idev_poll;
1215 applesmc_idev->poll_interval = APPLESMC_POLL_INTERVAL;
1217 /* initial calibrate for the input device */
1218 applesmc_calibrate();
1220 /* initialize the input device */
1221 idev = applesmc_idev->input;
1222 idev->name = "applesmc";
1223 idev->id.bustype = BUS_HOST;
1224 idev->dev.parent = &pdev->dev;
1225 idev->evbit[0] = BIT_MASK(EV_ABS);
1226 input_set_abs_params(idev, ABS_X,
1227 -256, 256, APPLESMC_INPUT_FUZZ, APPLESMC_INPUT_FLAT);
1228 input_set_abs_params(idev, ABS_Y,
1229 -256, 256, APPLESMC_INPUT_FUZZ, APPLESMC_INPUT_FLAT);
1231 ret = input_register_polled_device(applesmc_idev);
1238 input_free_polled_device(applesmc_idev);
1241 sysfs_remove_group(&pdev->dev.kobj, &accelerometer_attributes_group);
1244 printk(KERN_WARNING "applesmc: driver init failed (ret=%d)!\n", ret);
1248 /* Release all ressources used by the accelerometer */
1249 static void applesmc_release_accelerometer(void)
1251 input_unregister_polled_device(applesmc_idev);
1252 input_free_polled_device(applesmc_idev);
1253 sysfs_remove_group(&pdev->dev.kobj, &accelerometer_attributes_group);
1256 static __initdata struct dmi_match_data applesmc_dmi_data[] = {
1257 /* MacBook Pro: accelerometer, backlight and temperature set 0 */
1258 { .accelerometer = 1, .light = 1, .temperature_set = 0 },
1259 /* MacBook2: accelerometer and temperature set 1 */
1260 { .accelerometer = 1, .light = 0, .temperature_set = 1 },
1261 /* MacBook: accelerometer and temperature set 2 */
1262 { .accelerometer = 1, .light = 0, .temperature_set = 2 },
1263 /* MacMini: temperature set 3 */
1264 { .accelerometer = 0, .light = 0, .temperature_set = 3 },
1265 /* MacPro: temperature set 4 */
1266 { .accelerometer = 0, .light = 0, .temperature_set = 4 },
1267 /* iMac: temperature set 5 */
1268 { .accelerometer = 0, .light = 0, .temperature_set = 5 },
1269 /* MacBook3: accelerometer and temperature set 6 */
1270 { .accelerometer = 1, .light = 0, .temperature_set = 6 },
1271 /* MacBook Air: accelerometer, backlight and temperature set 7 */
1272 { .accelerometer = 1, .light = 1, .temperature_set = 7 },
1273 /* MacBook Pro 4: accelerometer, backlight and temperature set 8 */
1274 { .accelerometer = 1, .light = 1, .temperature_set = 8 },
1275 /* MacBook Pro 3: accelerometer, backlight and temperature set 9 */
1276 { .accelerometer = 1, .light = 1, .temperature_set = 9 },
1279 /* Note that DMI_MATCH(...,"MacBook") will match "MacBookPro1,1".
1280 * So we need to put "Apple MacBook Pro" before "Apple MacBook". */
1281 static __initdata struct dmi_system_id applesmc_whitelist[] = {
1282 { applesmc_dmi_match, "Apple MacBook Air", {
1283 DMI_MATCH(DMI_BOARD_VENDOR, "Apple"),
1284 DMI_MATCH(DMI_PRODUCT_NAME, "MacBookAir") },
1285 &applesmc_dmi_data[7]},
1286 { applesmc_dmi_match, "Apple MacBook Pro 4", {
1287 DMI_MATCH(DMI_BOARD_VENDOR, "Apple"),
1288 DMI_MATCH(DMI_PRODUCT_NAME, "MacBookPro4") },
1289 &applesmc_dmi_data[8]},
1290 { applesmc_dmi_match, "Apple MacBook Pro 3", {
1291 DMI_MATCH(DMI_BOARD_VENDOR, "Apple"),
1292 DMI_MATCH(DMI_PRODUCT_NAME, "MacBookPro3") },
1293 &applesmc_dmi_data[9]},
1294 { applesmc_dmi_match, "Apple MacBook Pro", {
1295 DMI_MATCH(DMI_BOARD_VENDOR,"Apple"),
1296 DMI_MATCH(DMI_PRODUCT_NAME,"MacBookPro") },
1297 &applesmc_dmi_data[0]},
1298 { applesmc_dmi_match, "Apple MacBook (v2)", {
1299 DMI_MATCH(DMI_BOARD_VENDOR,"Apple"),
1300 DMI_MATCH(DMI_PRODUCT_NAME,"MacBook2") },
1301 &applesmc_dmi_data[1]},
1302 { applesmc_dmi_match, "Apple MacBook (v3)", {
1303 DMI_MATCH(DMI_BOARD_VENDOR,"Apple"),
1304 DMI_MATCH(DMI_PRODUCT_NAME,"MacBook3") },
1305 &applesmc_dmi_data[6]},
1306 { applesmc_dmi_match, "Apple MacBook", {
1307 DMI_MATCH(DMI_BOARD_VENDOR,"Apple"),
1308 DMI_MATCH(DMI_PRODUCT_NAME,"MacBook") },
1309 &applesmc_dmi_data[2]},
1310 { applesmc_dmi_match, "Apple Macmini", {
1311 DMI_MATCH(DMI_BOARD_VENDOR,"Apple"),
1312 DMI_MATCH(DMI_PRODUCT_NAME,"Macmini") },
1313 &applesmc_dmi_data[3]},
1314 { applesmc_dmi_match, "Apple MacPro2", {
1315 DMI_MATCH(DMI_BOARD_VENDOR,"Apple"),
1316 DMI_MATCH(DMI_PRODUCT_NAME,"MacPro2") },
1317 &applesmc_dmi_data[4]},
1318 { applesmc_dmi_match, "Apple iMac", {
1319 DMI_MATCH(DMI_BOARD_VENDOR,"Apple"),
1320 DMI_MATCH(DMI_PRODUCT_NAME,"iMac") },
1321 &applesmc_dmi_data[5]},
1325 static int __init applesmc_init(void)
1331 if (!dmi_check_system(applesmc_whitelist)) {
1332 printk(KERN_WARNING "applesmc: supported laptop not found!\n");
1337 if (!request_region(APPLESMC_DATA_PORT, APPLESMC_NR_PORTS,
1343 ret = platform_driver_register(&applesmc_driver);
1347 pdev = platform_device_register_simple("applesmc", APPLESMC_DATA_PORT,
1350 ret = PTR_ERR(pdev);
1354 ret = sysfs_create_file(&pdev->dev.kobj, &dev_attr_name.attr);
1358 /* Create key enumeration sysfs files */
1359 ret = sysfs_create_group(&pdev->dev.kobj, &key_enumeration_group);
1363 /* create fan files */
1364 count = applesmc_get_fan_count();
1366 printk(KERN_ERR "applesmc: Cannot get the number of fans.\n");
1368 printk(KERN_INFO "applesmc: %d fans found.\n", count);
1372 printk(KERN_WARNING "applesmc: More than 4 fans found,"
1373 " but at most 4 fans are supported"
1374 " by the driver.\n");
1376 ret = sysfs_create_group(&pdev->dev.kobj,
1377 &fan_attribute_groups[3]);
1379 goto out_key_enumeration;
1381 ret = sysfs_create_group(&pdev->dev.kobj,
1382 &fan_attribute_groups[2]);
1384 goto out_key_enumeration;
1386 ret = sysfs_create_group(&pdev->dev.kobj,
1387 &fan_attribute_groups[1]);
1389 goto out_key_enumeration;
1391 ret = sysfs_create_group(&pdev->dev.kobj,
1392 &fan_attribute_groups[0]);
1401 temperature_sensors_sets[applesmc_temperature_set][i] != NULL;
1403 if (temperature_attributes[i] == NULL) {
1404 printk(KERN_ERR "applesmc: More temperature sensors "
1405 "in temperature_sensors_sets (at least %i)"
1406 "than available sysfs files in "
1407 "temperature_attributes (%i), please report "
1408 "this bug.\n", i, i-1);
1409 goto out_temperature;
1411 ret = sysfs_create_file(&pdev->dev.kobj,
1412 temperature_attributes[i]);
1414 goto out_temperature;
1417 if (applesmc_accelerometer) {
1418 ret = applesmc_create_accelerometer();
1420 goto out_temperature;
1423 if (applesmc_light) {
1424 /* Add light sensor file */
1425 ret = sysfs_create_file(&pdev->dev.kobj, &dev_attr_light.attr);
1427 goto out_accelerometer;
1429 /* Create the workqueue */
1430 applesmc_led_wq = create_singlethread_workqueue("applesmc-led");
1431 if (!applesmc_led_wq) {
1433 goto out_light_sysfs;
1436 /* register as a led device */
1437 ret = led_classdev_register(&pdev->dev, &applesmc_backlight);
1442 hwmon_dev = hwmon_device_register(&pdev->dev);
1443 if (IS_ERR(hwmon_dev)) {
1444 ret = PTR_ERR(hwmon_dev);
1445 goto out_light_ledclass;
1448 printk(KERN_INFO "applesmc: driver successfully loaded.\n");
1454 led_classdev_unregister(&applesmc_backlight);
1457 destroy_workqueue(applesmc_led_wq);
1460 sysfs_remove_file(&pdev->dev.kobj, &dev_attr_light.attr);
1462 if (applesmc_accelerometer)
1463 applesmc_release_accelerometer();
1465 sysfs_remove_group(&pdev->dev.kobj, &temperature_attributes_group);
1466 sysfs_remove_group(&pdev->dev.kobj, &fan_attribute_groups[0]);
1468 sysfs_remove_group(&pdev->dev.kobj, &fan_attribute_groups[1]);
1469 out_key_enumeration:
1470 sysfs_remove_group(&pdev->dev.kobj, &key_enumeration_group);
1472 sysfs_remove_file(&pdev->dev.kobj, &dev_attr_name.attr);
1474 platform_device_unregister(pdev);
1476 platform_driver_unregister(&applesmc_driver);
1478 release_region(APPLESMC_DATA_PORT, APPLESMC_NR_PORTS);
1480 printk(KERN_WARNING "applesmc: driver init failed (ret=%d)!\n", ret);
1484 static void __exit applesmc_exit(void)
1486 hwmon_device_unregister(hwmon_dev);
1487 if (applesmc_light) {
1488 led_classdev_unregister(&applesmc_backlight);
1489 destroy_workqueue(applesmc_led_wq);
1490 sysfs_remove_file(&pdev->dev.kobj, &dev_attr_light.attr);
1492 if (applesmc_accelerometer)
1493 applesmc_release_accelerometer();
1494 sysfs_remove_group(&pdev->dev.kobj, &temperature_attributes_group);
1495 sysfs_remove_group(&pdev->dev.kobj, &fan_attribute_groups[0]);
1496 sysfs_remove_group(&pdev->dev.kobj, &fan_attribute_groups[1]);
1497 sysfs_remove_group(&pdev->dev.kobj, &key_enumeration_group);
1498 sysfs_remove_file(&pdev->dev.kobj, &dev_attr_name.attr);
1499 platform_device_unregister(pdev);
1500 platform_driver_unregister(&applesmc_driver);
1501 release_region(APPLESMC_DATA_PORT, APPLESMC_NR_PORTS);
1503 printk(KERN_INFO "applesmc: driver unloaded.\n");
1506 module_init(applesmc_init);
1507 module_exit(applesmc_exit);
1509 MODULE_AUTHOR("Nicolas Boichat");
1510 MODULE_DESCRIPTION("Apple SMC");
1511 MODULE_LICENSE("GPL v2");