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