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