Merge branch 'for-linus' of git://oss.sgi.com:8090/xfs/xfs-2.6
[sfrench/cifs-2.6.git] / drivers / hwmon / applesmc.c
1 /*
2  * drivers/hwmon/applesmc.c - driver for Apple's SMC (accelerometer, temperature
3  * sensors, fan control, keyboard backlight control) used in Intel-based Apple
4  * computers.
5  *
6  * Copyright (C) 2007 Nicolas Boichat <nicolas@boichat.ch>
7  *
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>
11  *
12  * Fan control based on smcFanControl:
13  * Copyright (C) 2006 Hendrik Holtmann <holtmann@mac.com>
14  *
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.
18  *
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
22  * more details.
23  *
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
27  */
28
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>
38 #include <asm/io.h>
39 #include <linux/leds.h>
40 #include <linux/hwmon.h>
41 #include <linux/workqueue.h>
42
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
47
48 #define APPLESMC_NR_PORTS       32 /* 0x300-0x31f */
49
50 #define APPLESMC_MAX_DATA_LENGTH 32
51
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
57
58 #define KEY_COUNT_KEY           "#KEY" /* r-o ui32 */
59
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) */
63
64 #define CLAMSHELL_KEY           "MSLD" /* r-o ui8 (unused) */
65
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 */
70
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] */
79
80 /*
81  * Temperature sensors keys (sp78 - 2 bytes).
82  */
83 static const char* temperature_sensors_sets[][13] = {
84 /* Set 0: Macbook Pro */
85         { "TA0P", "TB0T", "TC0D", "TC0P", "TG0H", "TG0P", "TG0T", "Th0H",
86           "Th1H", "Tm0P", "Ts0P", "Ts1P", NULL },
87 /* Set 1: Macbook set */
88         { "TB0T", "TC0D", "TC0P", "TM0P", "TN0P", "TN1P", "Th0H", "Th0S",
89           "Th1H", "Ts0P", NULL },
90 /* Set 2: Macmini set */
91         { "TC0D", "TC0P", NULL }
92 };
93
94 /* List of keys used to read/write fan speeds */
95 static const char* fan_speed_keys[] = {
96         FAN_ACTUAL_SPEED,
97         FAN_MIN_SPEED,
98         FAN_MAX_SPEED,
99         FAN_SAFE_SPEED,
100         FAN_TARGET_SPEED
101 };
102
103 #define INIT_TIMEOUT_MSECS      5000    /* wait up to 5s for device init ... */
104 #define INIT_WAIT_MSECS         50      /* ... in 50ms increments */
105
106 #define APPLESMC_POLL_INTERVAL  50      /* msecs */
107 #define APPLESMC_INPUT_FUZZ     4       /* input event threshold */
108 #define APPLESMC_INPUT_FLAT     4
109
110 #define SENSOR_X 0
111 #define SENSOR_Y 1
112 #define SENSOR_Z 2
113
114 /* Structure to be passed to DMI_MATCH function */
115 struct dmi_match_data {
116 /* Indicates whether this computer has an accelerometer. */
117         int accelerometer;
118 /* Indicates whether this computer has light sensors and keyboard backlight. */
119         int light;
120 /* Indicates which temperature sensors set to use. */
121         int temperature_set;
122 };
123
124 static const int debug;
125 static struct platform_device *pdev;
126 static s16 rest_x;
127 static s16 rest_y;
128 static struct device *hwmon_dev;
129 static struct input_polled_dev *applesmc_idev;
130
131 /* Indicates whether this computer has an accelerometer. */
132 static unsigned int applesmc_accelerometer;
133
134 /* Indicates whether this computer has light sensors and keyboard backlight. */
135 static unsigned int applesmc_light;
136
137 /* Indicates which temperature sensors set to use. */
138 static unsigned int applesmc_temperature_set;
139
140 static DEFINE_MUTEX(applesmc_lock);
141
142 /*
143  * Last index written to key_at_index sysfs file, and value to use for all other
144  * key_at_index_* sysfs files.
145  */
146 static unsigned int key_at_index;
147
148 static struct workqueue_struct *applesmc_led_wq;
149
150 /*
151  * __wait_status - Wait up to 2ms for the status port to get a certain value
152  * (masked with 0x0f), returning zero if the value is obtained.  Callers must
153  * hold applesmc_lock.
154  */
155 static int __wait_status(u8 val)
156 {
157         unsigned int i;
158
159         val = val & APPLESMC_STATUS_MASK;
160
161         for (i = 0; i < 200; i++) {
162                 if ((inb(APPLESMC_CMD_PORT) & APPLESMC_STATUS_MASK) == val) {
163                         if (debug)
164                                 printk(KERN_DEBUG
165                                                 "Waited %d us for status %x\n",
166                                                 i*10, val);
167                         return 0;
168                 }
169                 udelay(10);
170         }
171
172         printk(KERN_WARNING "applesmc: wait status failed: %x != %x\n",
173                                                 val, inb(APPLESMC_CMD_PORT));
174
175         return -EIO;
176 }
177
178 /*
179  * applesmc_read_key - reads len bytes from a given key, and put them in buffer.
180  * Returns zero on success or a negative error on failure. Callers must
181  * hold applesmc_lock.
182  */
183 static int applesmc_read_key(const char* key, u8* buffer, u8 len)
184 {
185         int i;
186
187         if (len > APPLESMC_MAX_DATA_LENGTH) {
188                 printk(KERN_ERR "applesmc_read_key: cannot read more than "
189                                         "%d bytes\n", APPLESMC_MAX_DATA_LENGTH);
190                 return -EINVAL;
191         }
192
193         outb(APPLESMC_READ_CMD, APPLESMC_CMD_PORT);
194         if (__wait_status(0x0c))
195                 return -EIO;
196
197         for (i = 0; i < 4; i++) {
198                 outb(key[i], APPLESMC_DATA_PORT);
199                 if (__wait_status(0x04))
200                         return -EIO;
201         }
202         if (debug)
203                 printk(KERN_DEBUG "<%s", key);
204
205         outb(len, APPLESMC_DATA_PORT);
206         if (debug)
207                 printk(KERN_DEBUG ">%x", len);
208
209         for (i = 0; i < len; i++) {
210                 if (__wait_status(0x05))
211                         return -EIO;
212                 buffer[i] = inb(APPLESMC_DATA_PORT);
213                 if (debug)
214                         printk(KERN_DEBUG "<%x", buffer[i]);
215         }
216         if (debug)
217                 printk(KERN_DEBUG "\n");
218
219         return 0;
220 }
221
222 /*
223  * applesmc_write_key - writes len bytes from buffer to a given key.
224  * Returns zero on success or a negative error on failure. Callers must
225  * hold applesmc_lock.
226  */
227 static int applesmc_write_key(const char* key, u8* buffer, u8 len)
228 {
229         int i;
230
231         if (len > APPLESMC_MAX_DATA_LENGTH) {
232                 printk(KERN_ERR "applesmc_write_key: cannot write more than "
233                                         "%d bytes\n", APPLESMC_MAX_DATA_LENGTH);
234                 return -EINVAL;
235         }
236
237         outb(APPLESMC_WRITE_CMD, APPLESMC_CMD_PORT);
238         if (__wait_status(0x0c))
239                 return -EIO;
240
241         for (i = 0; i < 4; i++) {
242                 outb(key[i], APPLESMC_DATA_PORT);
243                 if (__wait_status(0x04))
244                         return -EIO;
245         }
246
247         outb(len, APPLESMC_DATA_PORT);
248
249         for (i = 0; i < len; i++) {
250                 if (__wait_status(0x04))
251                         return -EIO;
252                 outb(buffer[i], APPLESMC_DATA_PORT);
253         }
254
255         return 0;
256 }
257
258 /*
259  * applesmc_get_key_at_index - get key at index, and put the result in key
260  * (char[6]). Returns zero on success or a negative error on failure. Callers
261  * must hold applesmc_lock.
262  */
263 static int applesmc_get_key_at_index(int index, char* key)
264 {
265         int i;
266         u8 readkey[4];
267         readkey[0] = index >> 24;
268         readkey[1] = index >> 16;
269         readkey[2] = index >> 8;
270         readkey[3] = index;
271
272         outb(APPLESMC_GET_KEY_BY_INDEX_CMD, APPLESMC_CMD_PORT);
273         if (__wait_status(0x0c))
274                 return -EIO;
275
276         for (i = 0; i < 4; i++) {
277                 outb(readkey[i], APPLESMC_DATA_PORT);
278                 if (__wait_status(0x04))
279                         return -EIO;
280         }
281
282         outb(4, APPLESMC_DATA_PORT);
283
284         for (i = 0; i < 4; i++) {
285                 if (__wait_status(0x05))
286                         return -EIO;
287                 key[i] = inb(APPLESMC_DATA_PORT);
288         }
289         key[4] = 0;
290
291         return 0;
292 }
293
294 /*
295  * applesmc_get_key_type - get key type, and put the result in type (char[6]).
296  * Returns zero on success or a negative error on failure. Callers must
297  * hold applesmc_lock.
298  */
299 static int applesmc_get_key_type(char* key, char* type)
300 {
301         int i;
302
303         outb(APPLESMC_GET_KEY_TYPE_CMD, APPLESMC_CMD_PORT);
304         if (__wait_status(0x0c))
305                 return -EIO;
306
307         for (i = 0; i < 4; i++) {
308                 outb(key[i], APPLESMC_DATA_PORT);
309                 if (__wait_status(0x04))
310                         return -EIO;
311         }
312
313         outb(5, APPLESMC_DATA_PORT);
314
315         for (i = 0; i < 6; i++) {
316                 if (__wait_status(0x05))
317                         return -EIO;
318                 type[i] = inb(APPLESMC_DATA_PORT);
319         }
320         type[5] = 0;
321
322         return 0;
323 }
324
325 /*
326  * applesmc_read_motion_sensor - Read motion sensor (X, Y or Z). Callers must
327  * hold applesmc_lock.
328  */
329 static int applesmc_read_motion_sensor(int index, s16* value)
330 {
331         u8 buffer[2];
332         int ret;
333
334         switch (index) {
335         case SENSOR_X:
336                 ret = applesmc_read_key(MOTION_SENSOR_X_KEY, buffer, 2);
337                 break;
338         case SENSOR_Y:
339                 ret = applesmc_read_key(MOTION_SENSOR_Y_KEY, buffer, 2);
340                 break;
341         case SENSOR_Z:
342                 ret = applesmc_read_key(MOTION_SENSOR_Z_KEY, buffer, 2);
343                 break;
344         default:
345                 ret = -EINVAL;
346         }
347
348         *value = ((s16)buffer[0] << 8) | buffer[1];
349
350         return ret;
351 }
352
353 /*
354  * applesmc_device_init - initialize the accelerometer.  Returns zero on success
355  * and negative error code on failure.  Can sleep.
356  */
357 static int applesmc_device_init(void)
358 {
359         int total, ret = -ENXIO;
360         u8 buffer[2];
361
362         if (!applesmc_accelerometer)
363                 return 0;
364
365         mutex_lock(&applesmc_lock);
366
367         for (total = INIT_TIMEOUT_MSECS; total > 0; total -= INIT_WAIT_MSECS) {
368                 if (debug)
369                         printk(KERN_DEBUG "applesmc try %d\n", total);
370                 if (!applesmc_read_key(MOTION_SENSOR_KEY, buffer, 2) &&
371                                 (buffer[0] != 0x00 || buffer[1] != 0x00)) {
372                         if (total == INIT_TIMEOUT_MSECS) {
373                                 printk(KERN_DEBUG "applesmc: device has"
374                                                 " already been initialized"
375                                                 " (0x%02x, 0x%02x).\n",
376                                                 buffer[0], buffer[1]);
377                         } else {
378                                 printk(KERN_DEBUG "applesmc: device"
379                                                 " successfully initialized"
380                                                 " (0x%02x, 0x%02x).\n",
381                                                 buffer[0], buffer[1]);
382                         }
383                         ret = 0;
384                         goto out;
385                 }
386                 buffer[0] = 0xe0;
387                 buffer[1] = 0x00;
388                 applesmc_write_key(MOTION_SENSOR_KEY, buffer, 2);
389                 msleep(INIT_WAIT_MSECS);
390         }
391
392         printk(KERN_WARNING "applesmc: failed to init the device\n");
393
394 out:
395         mutex_unlock(&applesmc_lock);
396         return ret;
397 }
398
399 /*
400  * applesmc_get_fan_count - get the number of fans. Callers must NOT hold
401  * applesmc_lock.
402  */
403 static int applesmc_get_fan_count(void)
404 {
405         int ret;
406         u8 buffer[1];
407
408         mutex_lock(&applesmc_lock);
409
410         ret = applesmc_read_key(FANS_COUNT, buffer, 1);
411
412         mutex_unlock(&applesmc_lock);
413         if (ret)
414                 return ret;
415         else
416                 return buffer[0];
417 }
418
419 /* Device model stuff */
420 static int applesmc_probe(struct platform_device *dev)
421 {
422         int ret;
423
424         ret = applesmc_device_init();
425         if (ret)
426                 return ret;
427
428         printk(KERN_INFO "applesmc: device successfully initialized.\n");
429         return 0;
430 }
431
432 static int applesmc_resume(struct platform_device *dev)
433 {
434         return applesmc_device_init();
435 }
436
437 static struct platform_driver applesmc_driver = {
438         .probe = applesmc_probe,
439         .resume = applesmc_resume,
440         .driver = {
441                 .name = "applesmc",
442                 .owner = THIS_MODULE,
443         },
444 };
445
446 /*
447  * applesmc_calibrate - Set our "resting" values.  Callers must
448  * hold applesmc_lock.
449  */
450 static void applesmc_calibrate(void)
451 {
452         applesmc_read_motion_sensor(SENSOR_X, &rest_x);
453         applesmc_read_motion_sensor(SENSOR_Y, &rest_y);
454         rest_x = -rest_x;
455 }
456
457 static void applesmc_idev_poll(struct input_polled_dev *dev)
458 {
459         struct input_dev *idev = dev->input;
460         s16 x, y;
461
462         mutex_lock(&applesmc_lock);
463
464         if (applesmc_read_motion_sensor(SENSOR_X, &x))
465                 goto out;
466         if (applesmc_read_motion_sensor(SENSOR_Y, &y))
467                 goto out;
468
469         x = -x;
470         input_report_abs(idev, ABS_X, x - rest_x);
471         input_report_abs(idev, ABS_Y, y - rest_y);
472         input_sync(idev);
473
474 out:
475         mutex_unlock(&applesmc_lock);
476 }
477
478 /* Sysfs Files */
479
480 static ssize_t applesmc_name_show(struct device *dev,
481                                    struct device_attribute *attr, char *buf)
482 {
483         return snprintf(buf, PAGE_SIZE, "applesmc\n");
484 }
485
486 static ssize_t applesmc_position_show(struct device *dev,
487                                    struct device_attribute *attr, char *buf)
488 {
489         int ret;
490         s16 x, y, z;
491
492         mutex_lock(&applesmc_lock);
493
494         ret = applesmc_read_motion_sensor(SENSOR_X, &x);
495         if (ret)
496                 goto out;
497         ret = applesmc_read_motion_sensor(SENSOR_Y, &y);
498         if (ret)
499                 goto out;
500         ret = applesmc_read_motion_sensor(SENSOR_Z, &z);
501         if (ret)
502                 goto out;
503
504 out:
505         mutex_unlock(&applesmc_lock);
506         if (ret)
507                 return ret;
508         else
509                 return snprintf(buf, PAGE_SIZE, "(%d,%d,%d)\n", x, y, z);
510 }
511
512 static ssize_t applesmc_light_show(struct device *dev,
513                                 struct device_attribute *attr, char *sysfsbuf)
514 {
515         int ret;
516         u8 left = 0, right = 0;
517         u8 buffer[6];
518
519         mutex_lock(&applesmc_lock);
520
521         ret = applesmc_read_key(LIGHT_SENSOR_LEFT_KEY, buffer, 6);
522         left = buffer[2];
523         if (ret)
524                 goto out;
525         ret = applesmc_read_key(LIGHT_SENSOR_RIGHT_KEY, buffer, 6);
526         right = buffer[2];
527
528 out:
529         mutex_unlock(&applesmc_lock);
530         if (ret)
531                 return ret;
532         else
533                 return snprintf(sysfsbuf, PAGE_SIZE, "(%d,%d)\n", left, right);
534 }
535
536 /* Displays degree Celsius * 1000 */
537 static ssize_t applesmc_show_temperature(struct device *dev,
538                         struct device_attribute *devattr, char *sysfsbuf)
539 {
540         int ret;
541         u8 buffer[2];
542         unsigned int temp;
543         struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
544         const char* key =
545                 temperature_sensors_sets[applesmc_temperature_set][attr->index];
546
547         mutex_lock(&applesmc_lock);
548
549         ret = applesmc_read_key(key, buffer, 2);
550         temp = buffer[0]*1000;
551         temp += (buffer[1] >> 6) * 250;
552
553         mutex_unlock(&applesmc_lock);
554
555         if (ret)
556                 return ret;
557         else
558                 return snprintf(sysfsbuf, PAGE_SIZE, "%u\n", temp);
559 }
560
561 static ssize_t applesmc_show_fan_speed(struct device *dev,
562                                 struct device_attribute *attr, char *sysfsbuf)
563 {
564         int ret;
565         unsigned int speed = 0;
566         char newkey[5];
567         u8 buffer[2];
568         struct sensor_device_attribute_2 *sensor_attr =
569                                                 to_sensor_dev_attr_2(attr);
570
571         newkey[0] = fan_speed_keys[sensor_attr->nr][0];
572         newkey[1] = '0' + sensor_attr->index;
573         newkey[2] = fan_speed_keys[sensor_attr->nr][2];
574         newkey[3] = fan_speed_keys[sensor_attr->nr][3];
575         newkey[4] = 0;
576
577         mutex_lock(&applesmc_lock);
578
579         ret = applesmc_read_key(newkey, buffer, 2);
580         speed = ((buffer[0] << 8 | buffer[1]) >> 2);
581
582         mutex_unlock(&applesmc_lock);
583         if (ret)
584                 return ret;
585         else
586                 return snprintf(sysfsbuf, PAGE_SIZE, "%u\n", speed);
587 }
588
589 static ssize_t applesmc_store_fan_speed(struct device *dev,
590                                         struct device_attribute *attr,
591                                         const char *sysfsbuf, size_t count)
592 {
593         int ret;
594         u32 speed;
595         char newkey[5];
596         u8 buffer[2];
597         struct sensor_device_attribute_2 *sensor_attr =
598                                                 to_sensor_dev_attr_2(attr);
599
600         speed = simple_strtoul(sysfsbuf, NULL, 10);
601
602         if (speed > 0x4000) /* Bigger than a 14-bit value */
603                 return -EINVAL;
604
605         newkey[0] = fan_speed_keys[sensor_attr->nr][0];
606         newkey[1] = '0' + sensor_attr->index;
607         newkey[2] = fan_speed_keys[sensor_attr->nr][2];
608         newkey[3] = fan_speed_keys[sensor_attr->nr][3];
609         newkey[4] = 0;
610
611         mutex_lock(&applesmc_lock);
612
613         buffer[0] = (speed >> 6) & 0xff;
614         buffer[1] = (speed << 2) & 0xff;
615         ret = applesmc_write_key(newkey, buffer, 2);
616
617         mutex_unlock(&applesmc_lock);
618         if (ret)
619                 return ret;
620         else
621                 return count;
622 }
623
624 static ssize_t applesmc_show_fan_manual(struct device *dev,
625                         struct device_attribute *devattr, char *sysfsbuf)
626 {
627         int ret;
628         u16 manual = 0;
629         u8 buffer[2];
630         struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
631
632         mutex_lock(&applesmc_lock);
633
634         ret = applesmc_read_key(FANS_MANUAL, buffer, 2);
635         manual = ((buffer[0] << 8 | buffer[1]) >> attr->index) & 0x01;
636
637         mutex_unlock(&applesmc_lock);
638         if (ret)
639                 return ret;
640         else
641                 return snprintf(sysfsbuf, PAGE_SIZE, "%d\n", manual);
642 }
643
644 static ssize_t applesmc_store_fan_manual(struct device *dev,
645                                          struct device_attribute *devattr,
646                                          const char *sysfsbuf, size_t count)
647 {
648         int ret;
649         u8 buffer[2];
650         u32 input;
651         u16 val;
652         struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
653
654         input = simple_strtoul(sysfsbuf, NULL, 10);
655
656         mutex_lock(&applesmc_lock);
657
658         ret = applesmc_read_key(FANS_MANUAL, buffer, 2);
659         val = (buffer[0] << 8 | buffer[1]);
660         if (ret)
661                 goto out;
662
663         if (input)
664                 val = val | (0x01 << attr->index);
665         else
666                 val = val & ~(0x01 << attr->index);
667
668         buffer[0] = (val >> 8) & 0xFF;
669         buffer[1] = val & 0xFF;
670
671         ret = applesmc_write_key(FANS_MANUAL, buffer, 2);
672
673 out:
674         mutex_unlock(&applesmc_lock);
675         if (ret)
676                 return ret;
677         else
678                 return count;
679 }
680
681 static ssize_t applesmc_show_fan_position(struct device *dev,
682                                 struct device_attribute *attr, char *sysfsbuf)
683 {
684         int ret;
685         char newkey[5];
686         u8 buffer[17];
687         struct sensor_device_attribute_2 *sensor_attr =
688                                                 to_sensor_dev_attr_2(attr);
689
690         newkey[0] = FAN_POSITION[0];
691         newkey[1] = '0' + sensor_attr->index;
692         newkey[2] = FAN_POSITION[2];
693         newkey[3] = FAN_POSITION[3];
694         newkey[4] = 0;
695
696         mutex_lock(&applesmc_lock);
697
698         ret = applesmc_read_key(newkey, buffer, 16);
699         buffer[16] = 0;
700
701         mutex_unlock(&applesmc_lock);
702         if (ret)
703                 return ret;
704         else
705                 return snprintf(sysfsbuf, PAGE_SIZE, "%s\n", buffer+4);
706 }
707
708 static ssize_t applesmc_calibrate_show(struct device *dev,
709                                 struct device_attribute *attr, char *sysfsbuf)
710 {
711         return snprintf(sysfsbuf, PAGE_SIZE, "(%d,%d)\n", rest_x, rest_y);
712 }
713
714 static ssize_t applesmc_calibrate_store(struct device *dev,
715         struct device_attribute *attr, const char *sysfsbuf, size_t count)
716 {
717         mutex_lock(&applesmc_lock);
718         applesmc_calibrate();
719         mutex_unlock(&applesmc_lock);
720
721         return count;
722 }
723
724 /* Store the next backlight value to be written by the work */
725 static unsigned int backlight_value;
726
727 static void applesmc_backlight_set(struct work_struct *work)
728 {
729         u8 buffer[2];
730
731         mutex_lock(&applesmc_lock);
732         buffer[0] = backlight_value;
733         buffer[1] = 0x00;
734         applesmc_write_key(BACKLIGHT_KEY, buffer, 2);
735         mutex_unlock(&applesmc_lock);
736 }
737 static DECLARE_WORK(backlight_work, &applesmc_backlight_set);
738
739 static void applesmc_brightness_set(struct led_classdev *led_cdev,
740                                                 enum led_brightness value)
741 {
742         int ret;
743
744         backlight_value = value;
745         ret = queue_work(applesmc_led_wq, &backlight_work);
746
747         if (debug && (!ret))
748                 printk(KERN_DEBUG "applesmc: work was already on the queue.\n");
749 }
750
751 static ssize_t applesmc_key_count_show(struct device *dev,
752                                 struct device_attribute *attr, char *sysfsbuf)
753 {
754         int ret;
755         u8 buffer[4];
756         u32 count;
757
758         mutex_lock(&applesmc_lock);
759
760         ret = applesmc_read_key(KEY_COUNT_KEY, buffer, 4);
761         count = ((u32)buffer[0]<<24) + ((u32)buffer[1]<<16) +
762                                                 ((u32)buffer[2]<<8) + buffer[3];
763
764         mutex_unlock(&applesmc_lock);
765         if (ret)
766                 return ret;
767         else
768                 return snprintf(sysfsbuf, PAGE_SIZE, "%d\n", count);
769 }
770
771 static ssize_t applesmc_key_at_index_read_show(struct device *dev,
772                                 struct device_attribute *attr, char *sysfsbuf)
773 {
774         char key[5];
775         char info[6];
776         int ret;
777
778         mutex_lock(&applesmc_lock);
779
780         ret = applesmc_get_key_at_index(key_at_index, key);
781
782         if (ret || !key[0]) {
783                 mutex_unlock(&applesmc_lock);
784
785                 return -EINVAL;
786         }
787
788         ret = applesmc_get_key_type(key, info);
789
790         if (ret) {
791                 mutex_unlock(&applesmc_lock);
792
793                 return ret;
794         }
795
796         /*
797          * info[0] maximum value (APPLESMC_MAX_DATA_LENGTH) is much lower than
798          * PAGE_SIZE, so we don't need any checks before writing to sysfsbuf.
799          */
800         ret = applesmc_read_key(key, sysfsbuf, info[0]);
801
802         mutex_unlock(&applesmc_lock);
803
804         if (!ret) {
805                 return info[0];
806         } else {
807                 return ret;
808         }
809 }
810
811 static ssize_t applesmc_key_at_index_data_length_show(struct device *dev,
812                                 struct device_attribute *attr, char *sysfsbuf)
813 {
814         char key[5];
815         char info[6];
816         int ret;
817
818         mutex_lock(&applesmc_lock);
819
820         ret = applesmc_get_key_at_index(key_at_index, key);
821
822         if (ret || !key[0]) {
823                 mutex_unlock(&applesmc_lock);
824
825                 return -EINVAL;
826         }
827
828         ret = applesmc_get_key_type(key, info);
829
830         mutex_unlock(&applesmc_lock);
831
832         if (!ret)
833                 return snprintf(sysfsbuf, PAGE_SIZE, "%d\n", info[0]);
834         else
835                 return ret;
836 }
837
838 static ssize_t applesmc_key_at_index_type_show(struct device *dev,
839                                 struct device_attribute *attr, char *sysfsbuf)
840 {
841         char key[5];
842         char info[6];
843         int ret;
844
845         mutex_lock(&applesmc_lock);
846
847         ret = applesmc_get_key_at_index(key_at_index, key);
848
849         if (ret || !key[0]) {
850                 mutex_unlock(&applesmc_lock);
851
852                 return -EINVAL;
853         }
854
855         ret = applesmc_get_key_type(key, info);
856
857         mutex_unlock(&applesmc_lock);
858
859         if (!ret)
860                 return snprintf(sysfsbuf, PAGE_SIZE, "%s\n", info+1);
861         else
862                 return ret;
863 }
864
865 static ssize_t applesmc_key_at_index_name_show(struct device *dev,
866                                 struct device_attribute *attr, char *sysfsbuf)
867 {
868         char key[5];
869         int ret;
870
871         mutex_lock(&applesmc_lock);
872
873         ret = applesmc_get_key_at_index(key_at_index, key);
874
875         mutex_unlock(&applesmc_lock);
876
877         if (!ret && key[0])
878                 return snprintf(sysfsbuf, PAGE_SIZE, "%s\n", key);
879         else
880                 return -EINVAL;
881 }
882
883 static ssize_t applesmc_key_at_index_show(struct device *dev,
884                                 struct device_attribute *attr, char *sysfsbuf)
885 {
886         return snprintf(sysfsbuf, PAGE_SIZE, "%d\n", key_at_index);
887 }
888
889 static ssize_t applesmc_key_at_index_store(struct device *dev,
890         struct device_attribute *attr, const char *sysfsbuf, size_t count)
891 {
892         mutex_lock(&applesmc_lock);
893
894         key_at_index = simple_strtoul(sysfsbuf, NULL, 10);
895
896         mutex_unlock(&applesmc_lock);
897
898         return count;
899 }
900
901 static struct led_classdev applesmc_backlight = {
902         .name                   = "smc:kbd_backlight",
903         .default_trigger        = "nand-disk",
904         .brightness_set         = applesmc_brightness_set,
905 };
906
907 static DEVICE_ATTR(name, 0444, applesmc_name_show, NULL);
908
909 static DEVICE_ATTR(position, 0444, applesmc_position_show, NULL);
910 static DEVICE_ATTR(calibrate, 0644,
911                         applesmc_calibrate_show, applesmc_calibrate_store);
912
913 static struct attribute *accelerometer_attributes[] = {
914         &dev_attr_position.attr,
915         &dev_attr_calibrate.attr,
916         NULL
917 };
918
919 static const struct attribute_group accelerometer_attributes_group =
920         { .attrs = accelerometer_attributes };
921
922 static DEVICE_ATTR(light, 0444, applesmc_light_show, NULL);
923
924 static DEVICE_ATTR(key_count, 0444, applesmc_key_count_show, NULL);
925 static DEVICE_ATTR(key_at_index, 0644,
926                 applesmc_key_at_index_show, applesmc_key_at_index_store);
927 static DEVICE_ATTR(key_at_index_name, 0444,
928                                         applesmc_key_at_index_name_show, NULL);
929 static DEVICE_ATTR(key_at_index_type, 0444,
930                                         applesmc_key_at_index_type_show, NULL);
931 static DEVICE_ATTR(key_at_index_data_length, 0444,
932                                 applesmc_key_at_index_data_length_show, NULL);
933 static DEVICE_ATTR(key_at_index_data, 0444,
934                                 applesmc_key_at_index_read_show, NULL);
935
936 static struct attribute *key_enumeration_attributes[] = {
937         &dev_attr_key_count.attr,
938         &dev_attr_key_at_index.attr,
939         &dev_attr_key_at_index_name.attr,
940         &dev_attr_key_at_index_type.attr,
941         &dev_attr_key_at_index_data_length.attr,
942         &dev_attr_key_at_index_data.attr,
943         NULL
944 };
945
946 static const struct attribute_group key_enumeration_group =
947         { .attrs = key_enumeration_attributes };
948
949 /*
950  * Macro defining SENSOR_DEVICE_ATTR for a fan sysfs entries.
951  *  - show actual speed
952  *  - show/store minimum speed
953  *  - show maximum speed
954  *  - show safe speed
955  *  - show/store target speed
956  *  - show/store manual mode
957  */
958 #define sysfs_fan_speeds_offset(offset) \
959 static SENSOR_DEVICE_ATTR_2(fan##offset##_input, S_IRUGO, \
960                         applesmc_show_fan_speed, NULL, 0, offset-1); \
961 \
962 static SENSOR_DEVICE_ATTR_2(fan##offset##_min, S_IRUGO | S_IWUSR, \
963         applesmc_show_fan_speed, applesmc_store_fan_speed, 1, offset-1); \
964 \
965 static SENSOR_DEVICE_ATTR_2(fan##offset##_max, S_IRUGO, \
966                         applesmc_show_fan_speed, NULL, 2, offset-1); \
967 \
968 static SENSOR_DEVICE_ATTR_2(fan##offset##_safe, S_IRUGO, \
969                         applesmc_show_fan_speed, NULL, 3, offset-1); \
970 \
971 static SENSOR_DEVICE_ATTR_2(fan##offset##_output, S_IRUGO | S_IWUSR, \
972         applesmc_show_fan_speed, applesmc_store_fan_speed, 4, offset-1); \
973 \
974 static SENSOR_DEVICE_ATTR(fan##offset##_manual, S_IRUGO | S_IWUSR, \
975         applesmc_show_fan_manual, applesmc_store_fan_manual, offset-1); \
976 \
977 static SENSOR_DEVICE_ATTR(fan##offset##_label, S_IRUGO, \
978         applesmc_show_fan_position, NULL, offset-1); \
979 \
980 static struct attribute *fan##offset##_attributes[] = { \
981         &sensor_dev_attr_fan##offset##_input.dev_attr.attr, \
982         &sensor_dev_attr_fan##offset##_min.dev_attr.attr, \
983         &sensor_dev_attr_fan##offset##_max.dev_attr.attr, \
984         &sensor_dev_attr_fan##offset##_safe.dev_attr.attr, \
985         &sensor_dev_attr_fan##offset##_output.dev_attr.attr, \
986         &sensor_dev_attr_fan##offset##_manual.dev_attr.attr, \
987         &sensor_dev_attr_fan##offset##_label.dev_attr.attr, \
988         NULL \
989 };
990
991 /*
992  * Create the needed functions for each fan using the macro defined above
993  * (2 fans are supported)
994  */
995 sysfs_fan_speeds_offset(1);
996 sysfs_fan_speeds_offset(2);
997
998 static const struct attribute_group fan_attribute_groups[] = {
999         { .attrs = fan1_attributes },
1000         { .attrs = fan2_attributes }
1001 };
1002
1003 /*
1004  * Temperature sensors sysfs entries.
1005  */
1006 static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO,
1007                                         applesmc_show_temperature, NULL, 0);
1008 static SENSOR_DEVICE_ATTR(temp2_input, S_IRUGO,
1009                                         applesmc_show_temperature, NULL, 1);
1010 static SENSOR_DEVICE_ATTR(temp3_input, S_IRUGO,
1011                                         applesmc_show_temperature, NULL, 2);
1012 static SENSOR_DEVICE_ATTR(temp4_input, S_IRUGO,
1013                                         applesmc_show_temperature, NULL, 3);
1014 static SENSOR_DEVICE_ATTR(temp5_input, S_IRUGO,
1015                                         applesmc_show_temperature, NULL, 4);
1016 static SENSOR_DEVICE_ATTR(temp6_input, S_IRUGO,
1017                                         applesmc_show_temperature, NULL, 5);
1018 static SENSOR_DEVICE_ATTR(temp7_input, S_IRUGO,
1019                                         applesmc_show_temperature, NULL, 6);
1020 static SENSOR_DEVICE_ATTR(temp8_input, S_IRUGO,
1021                                         applesmc_show_temperature, NULL, 7);
1022 static SENSOR_DEVICE_ATTR(temp9_input, S_IRUGO,
1023                                         applesmc_show_temperature, NULL, 8);
1024 static SENSOR_DEVICE_ATTR(temp10_input, S_IRUGO,
1025                                         applesmc_show_temperature, NULL, 9);
1026 static SENSOR_DEVICE_ATTR(temp11_input, S_IRUGO,
1027                                         applesmc_show_temperature, NULL, 10);
1028 static SENSOR_DEVICE_ATTR(temp12_input, S_IRUGO,
1029                                         applesmc_show_temperature, NULL, 11);
1030
1031 static struct attribute *temperature_attributes[] = {
1032         &sensor_dev_attr_temp1_input.dev_attr.attr,
1033         &sensor_dev_attr_temp2_input.dev_attr.attr,
1034         &sensor_dev_attr_temp3_input.dev_attr.attr,
1035         &sensor_dev_attr_temp4_input.dev_attr.attr,
1036         &sensor_dev_attr_temp5_input.dev_attr.attr,
1037         &sensor_dev_attr_temp6_input.dev_attr.attr,
1038         &sensor_dev_attr_temp7_input.dev_attr.attr,
1039         &sensor_dev_attr_temp8_input.dev_attr.attr,
1040         &sensor_dev_attr_temp9_input.dev_attr.attr,
1041         &sensor_dev_attr_temp10_input.dev_attr.attr,
1042         &sensor_dev_attr_temp11_input.dev_attr.attr,
1043         &sensor_dev_attr_temp12_input.dev_attr.attr,
1044         NULL
1045 };
1046
1047 static const struct attribute_group temperature_attributes_group =
1048         { .attrs = temperature_attributes };
1049
1050 /* Module stuff */
1051
1052 /*
1053  * applesmc_dmi_match - found a match.  return one, short-circuiting the hunt.
1054  */
1055 static int applesmc_dmi_match(const struct dmi_system_id *id)
1056 {
1057         int i = 0;
1058         struct dmi_match_data* dmi_data = id->driver_data;
1059         printk(KERN_INFO "applesmc: %s detected:\n", id->ident);
1060         applesmc_accelerometer = dmi_data->accelerometer;
1061         printk(KERN_INFO "applesmc:  - Model %s accelerometer\n",
1062                                 applesmc_accelerometer ? "with" : "without");
1063         applesmc_light = dmi_data->light;
1064         printk(KERN_INFO "applesmc:  - Model %s light sensors and backlight\n",
1065                                         applesmc_light ? "with" : "without");
1066
1067         applesmc_temperature_set =  dmi_data->temperature_set;
1068         while (temperature_sensors_sets[applesmc_temperature_set][i] != NULL)
1069                 i++;
1070         printk(KERN_INFO "applesmc:  - Model with %d temperature sensors\n", i);
1071         return 1;
1072 }
1073
1074 /* Create accelerometer ressources */
1075 static int applesmc_create_accelerometer(void)
1076 {
1077         struct input_dev *idev;
1078         int ret;
1079
1080         ret = sysfs_create_group(&pdev->dev.kobj,
1081                                         &accelerometer_attributes_group);
1082         if (ret)
1083                 goto out;
1084
1085         applesmc_idev = input_allocate_polled_device();
1086         if (!applesmc_idev) {
1087                 ret = -ENOMEM;
1088                 goto out_sysfs;
1089         }
1090
1091         applesmc_idev->poll = applesmc_idev_poll;
1092         applesmc_idev->poll_interval = APPLESMC_POLL_INTERVAL;
1093
1094         /* initial calibrate for the input device */
1095         applesmc_calibrate();
1096
1097         /* initialize the input device */
1098         idev = applesmc_idev->input;
1099         idev->name = "applesmc";
1100         idev->id.bustype = BUS_HOST;
1101         idev->dev.parent = &pdev->dev;
1102         idev->evbit[0] = BIT(EV_ABS);
1103         input_set_abs_params(idev, ABS_X,
1104                         -256, 256, APPLESMC_INPUT_FUZZ, APPLESMC_INPUT_FLAT);
1105         input_set_abs_params(idev, ABS_Y,
1106                         -256, 256, APPLESMC_INPUT_FUZZ, APPLESMC_INPUT_FLAT);
1107
1108         ret = input_register_polled_device(applesmc_idev);
1109         if (ret)
1110                 goto out_idev;
1111
1112         return 0;
1113
1114 out_idev:
1115         input_free_polled_device(applesmc_idev);
1116
1117 out_sysfs:
1118         sysfs_remove_group(&pdev->dev.kobj, &accelerometer_attributes_group);
1119
1120 out:
1121         printk(KERN_WARNING "applesmc: driver init failed (ret=%d)!\n", ret);
1122         return ret;
1123 }
1124
1125 /* Release all ressources used by the accelerometer */
1126 static void applesmc_release_accelerometer(void)
1127 {
1128         input_unregister_polled_device(applesmc_idev);
1129         input_free_polled_device(applesmc_idev);
1130         sysfs_remove_group(&pdev->dev.kobj, &accelerometer_attributes_group);
1131 }
1132
1133 static __initdata struct dmi_match_data applesmc_dmi_data[] = {
1134 /* MacBook Pro: accelerometer, backlight and temperature set 0 */
1135         { .accelerometer = 1, .light = 1, .temperature_set = 0 },
1136 /* MacBook: accelerometer and temperature set 1 */
1137         { .accelerometer = 1, .light = 0, .temperature_set = 1 },
1138 /* MacMini: temperature set 2 */
1139         { .accelerometer = 0, .light = 0, .temperature_set = 2 },
1140 };
1141
1142 /* Note that DMI_MATCH(...,"MacBook") will match "MacBookPro1,1".
1143  * So we need to put "Apple MacBook Pro" before "Apple MacBook". */
1144 static __initdata struct dmi_system_id applesmc_whitelist[] = {
1145         { applesmc_dmi_match, "Apple MacBook Pro", {
1146           DMI_MATCH(DMI_BOARD_VENDOR,"Apple"),
1147           DMI_MATCH(DMI_PRODUCT_NAME,"MacBookPro") },
1148                 (void*)&applesmc_dmi_data[0]},
1149         { applesmc_dmi_match, "Apple MacBook", {
1150           DMI_MATCH(DMI_BOARD_VENDOR,"Apple"),
1151           DMI_MATCH(DMI_PRODUCT_NAME,"MacBook") },
1152                 (void*)&applesmc_dmi_data[1]},
1153         { applesmc_dmi_match, "Apple Macmini", {
1154           DMI_MATCH(DMI_BOARD_VENDOR,"Apple"),
1155           DMI_MATCH(DMI_PRODUCT_NAME,"Macmini") },
1156                 (void*)&applesmc_dmi_data[2]},
1157         { .ident = NULL }
1158 };
1159
1160 static int __init applesmc_init(void)
1161 {
1162         int ret;
1163         int count;
1164         int i;
1165
1166         if (!dmi_check_system(applesmc_whitelist)) {
1167                 printk(KERN_WARNING "applesmc: supported laptop not found!\n");
1168                 ret = -ENODEV;
1169                 goto out;
1170         }
1171
1172         if (!request_region(APPLESMC_DATA_PORT, APPLESMC_NR_PORTS,
1173                                                                 "applesmc")) {
1174                 ret = -ENXIO;
1175                 goto out;
1176         }
1177
1178         ret = platform_driver_register(&applesmc_driver);
1179         if (ret)
1180                 goto out_region;
1181
1182         pdev = platform_device_register_simple("applesmc", APPLESMC_DATA_PORT,
1183                                                NULL, 0);
1184         if (IS_ERR(pdev)) {
1185                 ret = PTR_ERR(pdev);
1186                 goto out_driver;
1187         }
1188
1189         ret = sysfs_create_file(&pdev->dev.kobj, &dev_attr_name.attr);
1190         if (ret)
1191                 goto out_device;
1192
1193         /* Create key enumeration sysfs files */
1194         ret = sysfs_create_group(&pdev->dev.kobj, &key_enumeration_group);
1195         if (ret)
1196                 goto out_name;
1197
1198         /* create fan files */
1199         count = applesmc_get_fan_count();
1200         if (count < 0) {
1201                 printk(KERN_ERR "applesmc: Cannot get the number of fans.\n");
1202         } else {
1203                 printk(KERN_INFO "applesmc: %d fans found.\n", count);
1204
1205                 switch (count) {
1206                 default:
1207                         printk(KERN_WARNING "applesmc: More than 2 fans found,"
1208                                         " but at most 2 fans are supported"
1209                                                 " by the driver.\n");
1210                 case 2:
1211                         ret = sysfs_create_group(&pdev->dev.kobj,
1212                                                  &fan_attribute_groups[1]);
1213                         if (ret)
1214                                 goto out_key_enumeration;
1215                 case 1:
1216                         ret = sysfs_create_group(&pdev->dev.kobj,
1217                                                  &fan_attribute_groups[0]);
1218                         if (ret)
1219                                 goto out_fan_1;
1220                 case 0:
1221                         ;
1222                 }
1223         }
1224
1225         for (i = 0;
1226              temperature_sensors_sets[applesmc_temperature_set][i] != NULL;
1227              i++) {
1228                 if (temperature_attributes[i] == NULL) {
1229                         printk(KERN_ERR "applesmc: More temperature sensors "
1230                                 "in temperature_sensors_sets (at least %i)"
1231                                 "than available sysfs files in "
1232                                 "temperature_attributes (%i), please report "
1233                                 "this bug.\n", i, i-1);
1234                         goto out_temperature;
1235                 }
1236                 ret = sysfs_create_file(&pdev->dev.kobj,
1237                                                 temperature_attributes[i]);
1238                 if (ret)
1239                         goto out_temperature;
1240         }
1241
1242         if (applesmc_accelerometer) {
1243                 ret = applesmc_create_accelerometer();
1244                 if (ret)
1245                         goto out_temperature;
1246         }
1247
1248         if (applesmc_light) {
1249                 /* Add light sensor file */
1250                 ret = sysfs_create_file(&pdev->dev.kobj, &dev_attr_light.attr);
1251                 if (ret)
1252                         goto out_accelerometer;
1253
1254                 /* Create the workqueue */
1255                 applesmc_led_wq = create_singlethread_workqueue("applesmc-led");
1256                 if (!applesmc_led_wq) {
1257                         ret = -ENOMEM;
1258                         goto out_light_sysfs;
1259                 }
1260
1261                 /* register as a led device */
1262                 ret = led_classdev_register(&pdev->dev, &applesmc_backlight);
1263                 if (ret < 0)
1264                         goto out_light_wq;
1265         }
1266
1267         hwmon_dev = hwmon_device_register(&pdev->dev);
1268         if (IS_ERR(hwmon_dev)) {
1269                 ret = PTR_ERR(hwmon_dev);
1270                 goto out_light_ledclass;
1271         }
1272
1273         printk(KERN_INFO "applesmc: driver successfully loaded.\n");
1274
1275         return 0;
1276
1277 out_light_ledclass:
1278         if (applesmc_light)
1279                 led_classdev_unregister(&applesmc_backlight);
1280 out_light_wq:
1281         if (applesmc_light)
1282                 destroy_workqueue(applesmc_led_wq);
1283 out_light_sysfs:
1284         if (applesmc_light)
1285                 sysfs_remove_file(&pdev->dev.kobj, &dev_attr_light.attr);
1286 out_accelerometer:
1287         if (applesmc_accelerometer)
1288                 applesmc_release_accelerometer();
1289 out_temperature:
1290         sysfs_remove_group(&pdev->dev.kobj, &temperature_attributes_group);
1291         sysfs_remove_group(&pdev->dev.kobj, &fan_attribute_groups[0]);
1292 out_fan_1:
1293         sysfs_remove_group(&pdev->dev.kobj, &fan_attribute_groups[1]);
1294 out_key_enumeration:
1295         sysfs_remove_group(&pdev->dev.kobj, &key_enumeration_group);
1296 out_name:
1297         sysfs_remove_file(&pdev->dev.kobj, &dev_attr_name.attr);
1298 out_device:
1299         platform_device_unregister(pdev);
1300 out_driver:
1301         platform_driver_unregister(&applesmc_driver);
1302 out_region:
1303         release_region(APPLESMC_DATA_PORT, APPLESMC_NR_PORTS);
1304 out:
1305         printk(KERN_WARNING "applesmc: driver init failed (ret=%d)!\n", ret);
1306         return ret;
1307 }
1308
1309 static void __exit applesmc_exit(void)
1310 {
1311         hwmon_device_unregister(hwmon_dev);
1312         if (applesmc_light) {
1313                 led_classdev_unregister(&applesmc_backlight);
1314                 destroy_workqueue(applesmc_led_wq);
1315                 sysfs_remove_file(&pdev->dev.kobj, &dev_attr_light.attr);
1316         }
1317         if (applesmc_accelerometer)
1318                 applesmc_release_accelerometer();
1319         sysfs_remove_group(&pdev->dev.kobj, &temperature_attributes_group);
1320         sysfs_remove_group(&pdev->dev.kobj, &fan_attribute_groups[0]);
1321         sysfs_remove_group(&pdev->dev.kobj, &fan_attribute_groups[1]);
1322         sysfs_remove_group(&pdev->dev.kobj, &key_enumeration_group);
1323         sysfs_remove_file(&pdev->dev.kobj, &dev_attr_name.attr);
1324         platform_device_unregister(pdev);
1325         platform_driver_unregister(&applesmc_driver);
1326         release_region(APPLESMC_DATA_PORT, APPLESMC_NR_PORTS);
1327
1328         printk(KERN_INFO "applesmc: driver unloaded.\n");
1329 }
1330
1331 module_init(applesmc_init);
1332 module_exit(applesmc_exit);
1333
1334 MODULE_AUTHOR("Nicolas Boichat");
1335 MODULE_DESCRIPTION("Apple SMC");
1336 MODULE_LICENSE("GPL v2");