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