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