hwmon: applesmc: prolong status wait
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / hwmon / applesmc.c
blob9a43c4b60ea92f73cc966965f70261a1fb71b078
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.
6 * Copyright (C) 2007 Nicolas Boichat <nicolas@boichat.ch>
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>
12 * Fan control based on smcFanControl:
13 * Copyright (C) 2006 Hendrik Holtmann <holtmann@mac.com>
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.
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.
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
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>
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
48 #define APPLESMC_NR_PORTS 32 /* 0x300-0x31f */
50 #define APPLESMC_MAX_DATA_LENGTH 32
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
58 #define KEY_COUNT_KEY "#KEY" /* r-o ui32 */
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) */
64 #define CLAMSHELL_KEY "MSLD" /* r-o ui8 (unused) */
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 */
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] */
81 * Temperature sensors keys (sp78 - 2 bytes).
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 },
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
118 #define INIT_TIMEOUT_MSECS 5000 /* wait up to 5s for device init ... */
119 #define INIT_WAIT_MSECS 50 /* ... in 50ms increments */
121 #define APPLESMC_POLL_INTERVAL 50 /* msecs */
122 #define APPLESMC_INPUT_FUZZ 4 /* input event threshold */
123 #define APPLESMC_INPUT_FLAT 4
125 #define SENSOR_X 0
126 #define SENSOR_Y 1
127 #define SENSOR_Z 2
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;
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;
146 /* Indicates whether this computer has an accelerometer. */
147 static unsigned int applesmc_accelerometer;
149 /* Indicates whether this computer has light sensors and keyboard backlight. */
150 static unsigned int applesmc_light;
152 /* Indicates which temperature sensors set to use. */
153 static unsigned int applesmc_temperature_set;
155 static DEFINE_MUTEX(applesmc_lock);
158 * Last index written to key_at_index sysfs file, and value to use for all other
159 * key_at_index_* sysfs files.
161 static unsigned int key_at_index;
163 static struct workqueue_struct *applesmc_led_wq;
166 * __wait_status - Wait up to 10ms 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.
170 static int __wait_status(u8 val)
172 unsigned int i;
174 val = val & APPLESMC_STATUS_MASK;
176 for (i = 0; i < 1000; 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;
184 udelay(10);
187 printk(KERN_WARNING "applesmc: wait status failed: %x != %x\n",
188 val, inb(APPLESMC_CMD_PORT));
190 return -EIO;
194 * special treatment of command port - on newer macbooks, it seems necessary
195 * to resend the command byte before polling the status again. Callers must
196 * hold applesmc_lock.
198 static int send_command(u8 cmd)
200 int i;
201 for (i = 0; i < 1000; i++) {
202 outb(cmd, APPLESMC_CMD_PORT);
203 udelay(5);
204 if ((inb(APPLESMC_CMD_PORT) & APPLESMC_STATUS_MASK) == 0x0c)
205 return 0;
206 udelay(5);
208 printk(KERN_WARNING "applesmc: command failed: %x -> %x\n",
209 cmd, inb(APPLESMC_CMD_PORT));
210 return -EIO;
214 * applesmc_read_key - reads len bytes from a given key, and put them in buffer.
215 * Returns zero on success or a negative error on failure. Callers must
216 * hold applesmc_lock.
218 static int applesmc_read_key(const char* key, u8* buffer, u8 len)
220 int i;
222 if (len > APPLESMC_MAX_DATA_LENGTH) {
223 printk(KERN_ERR "applesmc_read_key: cannot read more than "
224 "%d bytes\n", APPLESMC_MAX_DATA_LENGTH);
225 return -EINVAL;
228 if (send_command(APPLESMC_READ_CMD))
229 return -EIO;
231 for (i = 0; i < 4; i++) {
232 outb(key[i], APPLESMC_DATA_PORT);
233 if (__wait_status(0x04))
234 return -EIO;
236 if (debug)
237 printk(KERN_DEBUG "<%s", key);
239 outb(len, APPLESMC_DATA_PORT);
240 if (debug)
241 printk(KERN_DEBUG ">%x", len);
243 for (i = 0; i < len; i++) {
244 if (__wait_status(0x05))
245 return -EIO;
246 buffer[i] = inb(APPLESMC_DATA_PORT);
247 if (debug)
248 printk(KERN_DEBUG "<%x", buffer[i]);
250 if (debug)
251 printk(KERN_DEBUG "\n");
253 return 0;
257 * applesmc_write_key - writes len bytes from buffer to a given key.
258 * Returns zero on success or a negative error on failure. Callers must
259 * hold applesmc_lock.
261 static int applesmc_write_key(const char* key, u8* buffer, u8 len)
263 int i;
265 if (len > APPLESMC_MAX_DATA_LENGTH) {
266 printk(KERN_ERR "applesmc_write_key: cannot write more than "
267 "%d bytes\n", APPLESMC_MAX_DATA_LENGTH);
268 return -EINVAL;
271 if (send_command(APPLESMC_WRITE_CMD))
272 return -EIO;
274 for (i = 0; i < 4; i++) {
275 outb(key[i], APPLESMC_DATA_PORT);
276 if (__wait_status(0x04))
277 return -EIO;
280 outb(len, APPLESMC_DATA_PORT);
282 for (i = 0; i < len; i++) {
283 if (__wait_status(0x04))
284 return -EIO;
285 outb(buffer[i], APPLESMC_DATA_PORT);
288 return 0;
292 * applesmc_get_key_at_index - get key at index, and put the result in key
293 * (char[6]). Returns zero on success or a negative error on failure. Callers
294 * must hold applesmc_lock.
296 static int applesmc_get_key_at_index(int index, char* key)
298 int i;
299 u8 readkey[4];
300 readkey[0] = index >> 24;
301 readkey[1] = index >> 16;
302 readkey[2] = index >> 8;
303 readkey[3] = index;
305 if (send_command(APPLESMC_GET_KEY_BY_INDEX_CMD))
306 return -EIO;
308 for (i = 0; i < 4; i++) {
309 outb(readkey[i], APPLESMC_DATA_PORT);
310 if (__wait_status(0x04))
311 return -EIO;
314 outb(4, APPLESMC_DATA_PORT);
316 for (i = 0; i < 4; i++) {
317 if (__wait_status(0x05))
318 return -EIO;
319 key[i] = inb(APPLESMC_DATA_PORT);
321 key[4] = 0;
323 return 0;
327 * applesmc_get_key_type - get key type, and put the result in type (char[6]).
328 * Returns zero on success or a negative error on failure. Callers must
329 * hold applesmc_lock.
331 static int applesmc_get_key_type(char* key, char* type)
333 int i;
335 if (send_command(APPLESMC_GET_KEY_TYPE_CMD))
336 return -EIO;
338 for (i = 0; i < 4; i++) {
339 outb(key[i], APPLESMC_DATA_PORT);
340 if (__wait_status(0x04))
341 return -EIO;
344 outb(6, APPLESMC_DATA_PORT);
346 for (i = 0; i < 6; i++) {
347 if (__wait_status(0x05))
348 return -EIO;
349 type[i] = inb(APPLESMC_DATA_PORT);
351 type[5] = 0;
353 return 0;
357 * applesmc_read_motion_sensor - Read motion sensor (X, Y or Z). Callers must
358 * hold applesmc_lock.
360 static int applesmc_read_motion_sensor(int index, s16* value)
362 u8 buffer[2];
363 int ret;
365 switch (index) {
366 case SENSOR_X:
367 ret = applesmc_read_key(MOTION_SENSOR_X_KEY, buffer, 2);
368 break;
369 case SENSOR_Y:
370 ret = applesmc_read_key(MOTION_SENSOR_Y_KEY, buffer, 2);
371 break;
372 case SENSOR_Z:
373 ret = applesmc_read_key(MOTION_SENSOR_Z_KEY, buffer, 2);
374 break;
375 default:
376 ret = -EINVAL;
379 *value = ((s16)buffer[0] << 8) | buffer[1];
381 return ret;
385 * applesmc_device_init - initialize the accelerometer. Returns zero on success
386 * and negative error code on failure. Can sleep.
388 static int applesmc_device_init(void)
390 int total, ret = -ENXIO;
391 u8 buffer[2];
393 if (!applesmc_accelerometer)
394 return 0;
396 mutex_lock(&applesmc_lock);
398 for (total = INIT_TIMEOUT_MSECS; total > 0; total -= INIT_WAIT_MSECS) {
399 if (debug)
400 printk(KERN_DEBUG "applesmc try %d\n", total);
401 if (!applesmc_read_key(MOTION_SENSOR_KEY, buffer, 2) &&
402 (buffer[0] != 0x00 || buffer[1] != 0x00)) {
403 if (total == INIT_TIMEOUT_MSECS) {
404 printk(KERN_DEBUG "applesmc: device has"
405 " already been initialized"
406 " (0x%02x, 0x%02x).\n",
407 buffer[0], buffer[1]);
408 } else {
409 printk(KERN_DEBUG "applesmc: device"
410 " successfully initialized"
411 " (0x%02x, 0x%02x).\n",
412 buffer[0], buffer[1]);
414 ret = 0;
415 goto out;
417 buffer[0] = 0xe0;
418 buffer[1] = 0x00;
419 applesmc_write_key(MOTION_SENSOR_KEY, buffer, 2);
420 msleep(INIT_WAIT_MSECS);
423 printk(KERN_WARNING "applesmc: failed to init the device\n");
425 out:
426 mutex_unlock(&applesmc_lock);
427 return ret;
431 * applesmc_get_fan_count - get the number of fans. Callers must NOT hold
432 * applesmc_lock.
434 static int applesmc_get_fan_count(void)
436 int ret;
437 u8 buffer[1];
439 mutex_lock(&applesmc_lock);
441 ret = applesmc_read_key(FANS_COUNT, buffer, 1);
443 mutex_unlock(&applesmc_lock);
444 if (ret)
445 return ret;
446 else
447 return buffer[0];
450 /* Device model stuff */
451 static int applesmc_probe(struct platform_device *dev)
453 int ret;
455 ret = applesmc_device_init();
456 if (ret)
457 return ret;
459 printk(KERN_INFO "applesmc: device successfully initialized.\n");
460 return 0;
463 static int applesmc_resume(struct platform_device *dev)
465 return applesmc_device_init();
468 static struct platform_driver applesmc_driver = {
469 .probe = applesmc_probe,
470 .resume = applesmc_resume,
471 .driver = {
472 .name = "applesmc",
473 .owner = THIS_MODULE,
478 * applesmc_calibrate - Set our "resting" values. Callers must
479 * hold applesmc_lock.
481 static void applesmc_calibrate(void)
483 applesmc_read_motion_sensor(SENSOR_X, &rest_x);
484 applesmc_read_motion_sensor(SENSOR_Y, &rest_y);
485 rest_x = -rest_x;
488 static void applesmc_idev_poll(struct input_polled_dev *dev)
490 struct input_dev *idev = dev->input;
491 s16 x, y;
493 mutex_lock(&applesmc_lock);
495 if (applesmc_read_motion_sensor(SENSOR_X, &x))
496 goto out;
497 if (applesmc_read_motion_sensor(SENSOR_Y, &y))
498 goto out;
500 x = -x;
501 input_report_abs(idev, ABS_X, x - rest_x);
502 input_report_abs(idev, ABS_Y, y - rest_y);
503 input_sync(idev);
505 out:
506 mutex_unlock(&applesmc_lock);
509 /* Sysfs Files */
511 static ssize_t applesmc_name_show(struct device *dev,
512 struct device_attribute *attr, char *buf)
514 return snprintf(buf, PAGE_SIZE, "applesmc\n");
517 static ssize_t applesmc_position_show(struct device *dev,
518 struct device_attribute *attr, char *buf)
520 int ret;
521 s16 x, y, z;
523 mutex_lock(&applesmc_lock);
525 ret = applesmc_read_motion_sensor(SENSOR_X, &x);
526 if (ret)
527 goto out;
528 ret = applesmc_read_motion_sensor(SENSOR_Y, &y);
529 if (ret)
530 goto out;
531 ret = applesmc_read_motion_sensor(SENSOR_Z, &z);
532 if (ret)
533 goto out;
535 out:
536 mutex_unlock(&applesmc_lock);
537 if (ret)
538 return ret;
539 else
540 return snprintf(buf, PAGE_SIZE, "(%d,%d,%d)\n", x, y, z);
543 static ssize_t applesmc_light_show(struct device *dev,
544 struct device_attribute *attr, char *sysfsbuf)
546 int ret;
547 u8 left = 0, right = 0;
548 u8 buffer[6];
550 mutex_lock(&applesmc_lock);
552 ret = applesmc_read_key(LIGHT_SENSOR_LEFT_KEY, buffer, 6);
553 left = buffer[2];
554 if (ret)
555 goto out;
556 ret = applesmc_read_key(LIGHT_SENSOR_RIGHT_KEY, buffer, 6);
557 right = buffer[2];
559 out:
560 mutex_unlock(&applesmc_lock);
561 if (ret)
562 return ret;
563 else
564 return snprintf(sysfsbuf, PAGE_SIZE, "(%d,%d)\n", left, right);
567 /* Displays degree Celsius * 1000 */
568 static ssize_t applesmc_show_temperature(struct device *dev,
569 struct device_attribute *devattr, char *sysfsbuf)
571 int ret;
572 u8 buffer[2];
573 unsigned int temp;
574 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
575 const char* key =
576 temperature_sensors_sets[applesmc_temperature_set][attr->index];
578 mutex_lock(&applesmc_lock);
580 ret = applesmc_read_key(key, buffer, 2);
581 temp = buffer[0]*1000;
582 temp += (buffer[1] >> 6) * 250;
584 mutex_unlock(&applesmc_lock);
586 if (ret)
587 return ret;
588 else
589 return snprintf(sysfsbuf, PAGE_SIZE, "%u\n", temp);
592 static ssize_t applesmc_show_fan_speed(struct device *dev,
593 struct device_attribute *attr, char *sysfsbuf)
595 int ret;
596 unsigned int speed = 0;
597 char newkey[5];
598 u8 buffer[2];
599 struct sensor_device_attribute_2 *sensor_attr =
600 to_sensor_dev_attr_2(attr);
602 newkey[0] = fan_speed_keys[sensor_attr->nr][0];
603 newkey[1] = '0' + sensor_attr->index;
604 newkey[2] = fan_speed_keys[sensor_attr->nr][2];
605 newkey[3] = fan_speed_keys[sensor_attr->nr][3];
606 newkey[4] = 0;
608 mutex_lock(&applesmc_lock);
610 ret = applesmc_read_key(newkey, buffer, 2);
611 speed = ((buffer[0] << 8 | buffer[1]) >> 2);
613 mutex_unlock(&applesmc_lock);
614 if (ret)
615 return ret;
616 else
617 return snprintf(sysfsbuf, PAGE_SIZE, "%u\n", speed);
620 static ssize_t applesmc_store_fan_speed(struct device *dev,
621 struct device_attribute *attr,
622 const char *sysfsbuf, size_t count)
624 int ret;
625 u32 speed;
626 char newkey[5];
627 u8 buffer[2];
628 struct sensor_device_attribute_2 *sensor_attr =
629 to_sensor_dev_attr_2(attr);
631 speed = simple_strtoul(sysfsbuf, NULL, 10);
633 if (speed > 0x4000) /* Bigger than a 14-bit value */
634 return -EINVAL;
636 newkey[0] = fan_speed_keys[sensor_attr->nr][0];
637 newkey[1] = '0' + sensor_attr->index;
638 newkey[2] = fan_speed_keys[sensor_attr->nr][2];
639 newkey[3] = fan_speed_keys[sensor_attr->nr][3];
640 newkey[4] = 0;
642 mutex_lock(&applesmc_lock);
644 buffer[0] = (speed >> 6) & 0xff;
645 buffer[1] = (speed << 2) & 0xff;
646 ret = applesmc_write_key(newkey, buffer, 2);
648 mutex_unlock(&applesmc_lock);
649 if (ret)
650 return ret;
651 else
652 return count;
655 static ssize_t applesmc_show_fan_manual(struct device *dev,
656 struct device_attribute *devattr, char *sysfsbuf)
658 int ret;
659 u16 manual = 0;
660 u8 buffer[2];
661 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
663 mutex_lock(&applesmc_lock);
665 ret = applesmc_read_key(FANS_MANUAL, buffer, 2);
666 manual = ((buffer[0] << 8 | buffer[1]) >> attr->index) & 0x01;
668 mutex_unlock(&applesmc_lock);
669 if (ret)
670 return ret;
671 else
672 return snprintf(sysfsbuf, PAGE_SIZE, "%d\n", manual);
675 static ssize_t applesmc_store_fan_manual(struct device *dev,
676 struct device_attribute *devattr,
677 const char *sysfsbuf, size_t count)
679 int ret;
680 u8 buffer[2];
681 u32 input;
682 u16 val;
683 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
685 input = simple_strtoul(sysfsbuf, NULL, 10);
687 mutex_lock(&applesmc_lock);
689 ret = applesmc_read_key(FANS_MANUAL, buffer, 2);
690 val = (buffer[0] << 8 | buffer[1]);
691 if (ret)
692 goto out;
694 if (input)
695 val = val | (0x01 << attr->index);
696 else
697 val = val & ~(0x01 << attr->index);
699 buffer[0] = (val >> 8) & 0xFF;
700 buffer[1] = val & 0xFF;
702 ret = applesmc_write_key(FANS_MANUAL, buffer, 2);
704 out:
705 mutex_unlock(&applesmc_lock);
706 if (ret)
707 return ret;
708 else
709 return count;
712 static ssize_t applesmc_show_fan_position(struct device *dev,
713 struct device_attribute *attr, char *sysfsbuf)
715 int ret;
716 char newkey[5];
717 u8 buffer[17];
718 struct sensor_device_attribute_2 *sensor_attr =
719 to_sensor_dev_attr_2(attr);
721 newkey[0] = FAN_POSITION[0];
722 newkey[1] = '0' + sensor_attr->index;
723 newkey[2] = FAN_POSITION[2];
724 newkey[3] = FAN_POSITION[3];
725 newkey[4] = 0;
727 mutex_lock(&applesmc_lock);
729 ret = applesmc_read_key(newkey, buffer, 16);
730 buffer[16] = 0;
732 mutex_unlock(&applesmc_lock);
733 if (ret)
734 return ret;
735 else
736 return snprintf(sysfsbuf, PAGE_SIZE, "%s\n", buffer+4);
739 static ssize_t applesmc_calibrate_show(struct device *dev,
740 struct device_attribute *attr, char *sysfsbuf)
742 return snprintf(sysfsbuf, PAGE_SIZE, "(%d,%d)\n", rest_x, rest_y);
745 static ssize_t applesmc_calibrate_store(struct device *dev,
746 struct device_attribute *attr, const char *sysfsbuf, size_t count)
748 mutex_lock(&applesmc_lock);
749 applesmc_calibrate();
750 mutex_unlock(&applesmc_lock);
752 return count;
755 /* Store the next backlight value to be written by the work */
756 static unsigned int backlight_value;
758 static void applesmc_backlight_set(struct work_struct *work)
760 u8 buffer[2];
762 mutex_lock(&applesmc_lock);
763 buffer[0] = backlight_value;
764 buffer[1] = 0x00;
765 applesmc_write_key(BACKLIGHT_KEY, buffer, 2);
766 mutex_unlock(&applesmc_lock);
768 static DECLARE_WORK(backlight_work, &applesmc_backlight_set);
770 static void applesmc_brightness_set(struct led_classdev *led_cdev,
771 enum led_brightness value)
773 int ret;
775 backlight_value = value;
776 ret = queue_work(applesmc_led_wq, &backlight_work);
778 if (debug && (!ret))
779 printk(KERN_DEBUG "applesmc: work was already on the queue.\n");
782 static ssize_t applesmc_key_count_show(struct device *dev,
783 struct device_attribute *attr, char *sysfsbuf)
785 int ret;
786 u8 buffer[4];
787 u32 count;
789 mutex_lock(&applesmc_lock);
791 ret = applesmc_read_key(KEY_COUNT_KEY, buffer, 4);
792 count = ((u32)buffer[0]<<24) + ((u32)buffer[1]<<16) +
793 ((u32)buffer[2]<<8) + buffer[3];
795 mutex_unlock(&applesmc_lock);
796 if (ret)
797 return ret;
798 else
799 return snprintf(sysfsbuf, PAGE_SIZE, "%d\n", count);
802 static ssize_t applesmc_key_at_index_read_show(struct device *dev,
803 struct device_attribute *attr, char *sysfsbuf)
805 char key[5];
806 char info[6];
807 int ret;
809 mutex_lock(&applesmc_lock);
811 ret = applesmc_get_key_at_index(key_at_index, key);
813 if (ret || !key[0]) {
814 mutex_unlock(&applesmc_lock);
816 return -EINVAL;
819 ret = applesmc_get_key_type(key, info);
821 if (ret) {
822 mutex_unlock(&applesmc_lock);
824 return ret;
828 * info[0] maximum value (APPLESMC_MAX_DATA_LENGTH) is much lower than
829 * PAGE_SIZE, so we don't need any checks before writing to sysfsbuf.
831 ret = applesmc_read_key(key, sysfsbuf, info[0]);
833 mutex_unlock(&applesmc_lock);
835 if (!ret) {
836 return info[0];
837 } else {
838 return ret;
842 static ssize_t applesmc_key_at_index_data_length_show(struct device *dev,
843 struct device_attribute *attr, char *sysfsbuf)
845 char key[5];
846 char info[6];
847 int ret;
849 mutex_lock(&applesmc_lock);
851 ret = applesmc_get_key_at_index(key_at_index, key);
853 if (ret || !key[0]) {
854 mutex_unlock(&applesmc_lock);
856 return -EINVAL;
859 ret = applesmc_get_key_type(key, info);
861 mutex_unlock(&applesmc_lock);
863 if (!ret)
864 return snprintf(sysfsbuf, PAGE_SIZE, "%d\n", info[0]);
865 else
866 return ret;
869 static ssize_t applesmc_key_at_index_type_show(struct device *dev,
870 struct device_attribute *attr, char *sysfsbuf)
872 char key[5];
873 char info[6];
874 int ret;
876 mutex_lock(&applesmc_lock);
878 ret = applesmc_get_key_at_index(key_at_index, key);
880 if (ret || !key[0]) {
881 mutex_unlock(&applesmc_lock);
883 return -EINVAL;
886 ret = applesmc_get_key_type(key, info);
888 mutex_unlock(&applesmc_lock);
890 if (!ret)
891 return snprintf(sysfsbuf, PAGE_SIZE, "%s\n", info+1);
892 else
893 return ret;
896 static ssize_t applesmc_key_at_index_name_show(struct device *dev,
897 struct device_attribute *attr, char *sysfsbuf)
899 char key[5];
900 int ret;
902 mutex_lock(&applesmc_lock);
904 ret = applesmc_get_key_at_index(key_at_index, key);
906 mutex_unlock(&applesmc_lock);
908 if (!ret && key[0])
909 return snprintf(sysfsbuf, PAGE_SIZE, "%s\n", key);
910 else
911 return -EINVAL;
914 static ssize_t applesmc_key_at_index_show(struct device *dev,
915 struct device_attribute *attr, char *sysfsbuf)
917 return snprintf(sysfsbuf, PAGE_SIZE, "%d\n", key_at_index);
920 static ssize_t applesmc_key_at_index_store(struct device *dev,
921 struct device_attribute *attr, const char *sysfsbuf, size_t count)
923 mutex_lock(&applesmc_lock);
925 key_at_index = simple_strtoul(sysfsbuf, NULL, 10);
927 mutex_unlock(&applesmc_lock);
929 return count;
932 static struct led_classdev applesmc_backlight = {
933 .name = "smc::kbd_backlight",
934 .default_trigger = "nand-disk",
935 .brightness_set = applesmc_brightness_set,
938 static DEVICE_ATTR(name, 0444, applesmc_name_show, NULL);
940 static DEVICE_ATTR(position, 0444, applesmc_position_show, NULL);
941 static DEVICE_ATTR(calibrate, 0644,
942 applesmc_calibrate_show, applesmc_calibrate_store);
944 static struct attribute *accelerometer_attributes[] = {
945 &dev_attr_position.attr,
946 &dev_attr_calibrate.attr,
947 NULL
950 static const struct attribute_group accelerometer_attributes_group =
951 { .attrs = accelerometer_attributes };
953 static DEVICE_ATTR(light, 0444, applesmc_light_show, NULL);
955 static DEVICE_ATTR(key_count, 0444, applesmc_key_count_show, NULL);
956 static DEVICE_ATTR(key_at_index, 0644,
957 applesmc_key_at_index_show, applesmc_key_at_index_store);
958 static DEVICE_ATTR(key_at_index_name, 0444,
959 applesmc_key_at_index_name_show, NULL);
960 static DEVICE_ATTR(key_at_index_type, 0444,
961 applesmc_key_at_index_type_show, NULL);
962 static DEVICE_ATTR(key_at_index_data_length, 0444,
963 applesmc_key_at_index_data_length_show, NULL);
964 static DEVICE_ATTR(key_at_index_data, 0444,
965 applesmc_key_at_index_read_show, NULL);
967 static struct attribute *key_enumeration_attributes[] = {
968 &dev_attr_key_count.attr,
969 &dev_attr_key_at_index.attr,
970 &dev_attr_key_at_index_name.attr,
971 &dev_attr_key_at_index_type.attr,
972 &dev_attr_key_at_index_data_length.attr,
973 &dev_attr_key_at_index_data.attr,
974 NULL
977 static const struct attribute_group key_enumeration_group =
978 { .attrs = key_enumeration_attributes };
981 * Macro defining SENSOR_DEVICE_ATTR for a fan sysfs entries.
982 * - show actual speed
983 * - show/store minimum speed
984 * - show maximum speed
985 * - show safe speed
986 * - show/store target speed
987 * - show/store manual mode
989 #define sysfs_fan_speeds_offset(offset) \
990 static SENSOR_DEVICE_ATTR_2(fan##offset##_input, S_IRUGO, \
991 applesmc_show_fan_speed, NULL, 0, offset-1); \
993 static SENSOR_DEVICE_ATTR_2(fan##offset##_min, S_IRUGO | S_IWUSR, \
994 applesmc_show_fan_speed, applesmc_store_fan_speed, 1, offset-1); \
996 static SENSOR_DEVICE_ATTR_2(fan##offset##_max, S_IRUGO, \
997 applesmc_show_fan_speed, NULL, 2, offset-1); \
999 static SENSOR_DEVICE_ATTR_2(fan##offset##_safe, S_IRUGO, \
1000 applesmc_show_fan_speed, NULL, 3, offset-1); \
1002 static SENSOR_DEVICE_ATTR_2(fan##offset##_output, S_IRUGO | S_IWUSR, \
1003 applesmc_show_fan_speed, applesmc_store_fan_speed, 4, offset-1); \
1005 static SENSOR_DEVICE_ATTR(fan##offset##_manual, S_IRUGO | S_IWUSR, \
1006 applesmc_show_fan_manual, applesmc_store_fan_manual, offset-1); \
1008 static SENSOR_DEVICE_ATTR(fan##offset##_label, S_IRUGO, \
1009 applesmc_show_fan_position, NULL, offset-1); \
1011 static struct attribute *fan##offset##_attributes[] = { \
1012 &sensor_dev_attr_fan##offset##_input.dev_attr.attr, \
1013 &sensor_dev_attr_fan##offset##_min.dev_attr.attr, \
1014 &sensor_dev_attr_fan##offset##_max.dev_attr.attr, \
1015 &sensor_dev_attr_fan##offset##_safe.dev_attr.attr, \
1016 &sensor_dev_attr_fan##offset##_output.dev_attr.attr, \
1017 &sensor_dev_attr_fan##offset##_manual.dev_attr.attr, \
1018 &sensor_dev_attr_fan##offset##_label.dev_attr.attr, \
1019 NULL \
1023 * Create the needed functions for each fan using the macro defined above
1024 * (4 fans are supported)
1026 sysfs_fan_speeds_offset(1);
1027 sysfs_fan_speeds_offset(2);
1028 sysfs_fan_speeds_offset(3);
1029 sysfs_fan_speeds_offset(4);
1031 static const struct attribute_group fan_attribute_groups[] = {
1032 { .attrs = fan1_attributes },
1033 { .attrs = fan2_attributes },
1034 { .attrs = fan3_attributes },
1035 { .attrs = fan4_attributes },
1039 * Temperature sensors sysfs entries.
1041 static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO,
1042 applesmc_show_temperature, NULL, 0);
1043 static SENSOR_DEVICE_ATTR(temp2_input, S_IRUGO,
1044 applesmc_show_temperature, NULL, 1);
1045 static SENSOR_DEVICE_ATTR(temp3_input, S_IRUGO,
1046 applesmc_show_temperature, NULL, 2);
1047 static SENSOR_DEVICE_ATTR(temp4_input, S_IRUGO,
1048 applesmc_show_temperature, NULL, 3);
1049 static SENSOR_DEVICE_ATTR(temp5_input, S_IRUGO,
1050 applesmc_show_temperature, NULL, 4);
1051 static SENSOR_DEVICE_ATTR(temp6_input, S_IRUGO,
1052 applesmc_show_temperature, NULL, 5);
1053 static SENSOR_DEVICE_ATTR(temp7_input, S_IRUGO,
1054 applesmc_show_temperature, NULL, 6);
1055 static SENSOR_DEVICE_ATTR(temp8_input, S_IRUGO,
1056 applesmc_show_temperature, NULL, 7);
1057 static SENSOR_DEVICE_ATTR(temp9_input, S_IRUGO,
1058 applesmc_show_temperature, NULL, 8);
1059 static SENSOR_DEVICE_ATTR(temp10_input, S_IRUGO,
1060 applesmc_show_temperature, NULL, 9);
1061 static SENSOR_DEVICE_ATTR(temp11_input, S_IRUGO,
1062 applesmc_show_temperature, NULL, 10);
1063 static SENSOR_DEVICE_ATTR(temp12_input, S_IRUGO,
1064 applesmc_show_temperature, NULL, 11);
1065 static SENSOR_DEVICE_ATTR(temp13_input, S_IRUGO,
1066 applesmc_show_temperature, NULL, 12);
1067 static SENSOR_DEVICE_ATTR(temp14_input, S_IRUGO,
1068 applesmc_show_temperature, NULL, 13);
1069 static SENSOR_DEVICE_ATTR(temp15_input, S_IRUGO,
1070 applesmc_show_temperature, NULL, 14);
1071 static SENSOR_DEVICE_ATTR(temp16_input, S_IRUGO,
1072 applesmc_show_temperature, NULL, 15);
1073 static SENSOR_DEVICE_ATTR(temp17_input, S_IRUGO,
1074 applesmc_show_temperature, NULL, 16);
1075 static SENSOR_DEVICE_ATTR(temp18_input, S_IRUGO,
1076 applesmc_show_temperature, NULL, 17);
1077 static SENSOR_DEVICE_ATTR(temp19_input, S_IRUGO,
1078 applesmc_show_temperature, NULL, 18);
1079 static SENSOR_DEVICE_ATTR(temp20_input, S_IRUGO,
1080 applesmc_show_temperature, NULL, 19);
1081 static SENSOR_DEVICE_ATTR(temp21_input, S_IRUGO,
1082 applesmc_show_temperature, NULL, 20);
1083 static SENSOR_DEVICE_ATTR(temp22_input, S_IRUGO,
1084 applesmc_show_temperature, NULL, 21);
1085 static SENSOR_DEVICE_ATTR(temp23_input, S_IRUGO,
1086 applesmc_show_temperature, NULL, 22);
1087 static SENSOR_DEVICE_ATTR(temp24_input, S_IRUGO,
1088 applesmc_show_temperature, NULL, 23);
1089 static SENSOR_DEVICE_ATTR(temp25_input, S_IRUGO,
1090 applesmc_show_temperature, NULL, 24);
1091 static SENSOR_DEVICE_ATTR(temp26_input, S_IRUGO,
1092 applesmc_show_temperature, NULL, 25);
1093 static SENSOR_DEVICE_ATTR(temp27_input, S_IRUGO,
1094 applesmc_show_temperature, NULL, 26);
1095 static SENSOR_DEVICE_ATTR(temp28_input, S_IRUGO,
1096 applesmc_show_temperature, NULL, 27);
1097 static SENSOR_DEVICE_ATTR(temp29_input, S_IRUGO,
1098 applesmc_show_temperature, NULL, 28);
1099 static SENSOR_DEVICE_ATTR(temp30_input, S_IRUGO,
1100 applesmc_show_temperature, NULL, 29);
1101 static SENSOR_DEVICE_ATTR(temp31_input, S_IRUGO,
1102 applesmc_show_temperature, NULL, 30);
1103 static SENSOR_DEVICE_ATTR(temp32_input, S_IRUGO,
1104 applesmc_show_temperature, NULL, 31);
1105 static SENSOR_DEVICE_ATTR(temp33_input, S_IRUGO,
1106 applesmc_show_temperature, NULL, 32);
1107 static SENSOR_DEVICE_ATTR(temp34_input, S_IRUGO,
1108 applesmc_show_temperature, NULL, 33);
1109 static SENSOR_DEVICE_ATTR(temp35_input, S_IRUGO,
1110 applesmc_show_temperature, NULL, 34);
1112 static struct attribute *temperature_attributes[] = {
1113 &sensor_dev_attr_temp1_input.dev_attr.attr,
1114 &sensor_dev_attr_temp2_input.dev_attr.attr,
1115 &sensor_dev_attr_temp3_input.dev_attr.attr,
1116 &sensor_dev_attr_temp4_input.dev_attr.attr,
1117 &sensor_dev_attr_temp5_input.dev_attr.attr,
1118 &sensor_dev_attr_temp6_input.dev_attr.attr,
1119 &sensor_dev_attr_temp7_input.dev_attr.attr,
1120 &sensor_dev_attr_temp8_input.dev_attr.attr,
1121 &sensor_dev_attr_temp9_input.dev_attr.attr,
1122 &sensor_dev_attr_temp10_input.dev_attr.attr,
1123 &sensor_dev_attr_temp11_input.dev_attr.attr,
1124 &sensor_dev_attr_temp12_input.dev_attr.attr,
1125 &sensor_dev_attr_temp13_input.dev_attr.attr,
1126 &sensor_dev_attr_temp14_input.dev_attr.attr,
1127 &sensor_dev_attr_temp15_input.dev_attr.attr,
1128 &sensor_dev_attr_temp16_input.dev_attr.attr,
1129 &sensor_dev_attr_temp17_input.dev_attr.attr,
1130 &sensor_dev_attr_temp18_input.dev_attr.attr,
1131 &sensor_dev_attr_temp19_input.dev_attr.attr,
1132 &sensor_dev_attr_temp20_input.dev_attr.attr,
1133 &sensor_dev_attr_temp21_input.dev_attr.attr,
1134 &sensor_dev_attr_temp22_input.dev_attr.attr,
1135 &sensor_dev_attr_temp23_input.dev_attr.attr,
1136 &sensor_dev_attr_temp24_input.dev_attr.attr,
1137 &sensor_dev_attr_temp25_input.dev_attr.attr,
1138 &sensor_dev_attr_temp26_input.dev_attr.attr,
1139 &sensor_dev_attr_temp27_input.dev_attr.attr,
1140 &sensor_dev_attr_temp28_input.dev_attr.attr,
1141 &sensor_dev_attr_temp29_input.dev_attr.attr,
1142 &sensor_dev_attr_temp30_input.dev_attr.attr,
1143 &sensor_dev_attr_temp31_input.dev_attr.attr,
1144 &sensor_dev_attr_temp32_input.dev_attr.attr,
1145 &sensor_dev_attr_temp33_input.dev_attr.attr,
1146 &sensor_dev_attr_temp34_input.dev_attr.attr,
1147 &sensor_dev_attr_temp35_input.dev_attr.attr,
1148 NULL
1151 static const struct attribute_group temperature_attributes_group =
1152 { .attrs = temperature_attributes };
1154 /* Module stuff */
1157 * applesmc_dmi_match - found a match. return one, short-circuiting the hunt.
1159 static int applesmc_dmi_match(const struct dmi_system_id *id)
1161 int i = 0;
1162 struct dmi_match_data* dmi_data = id->driver_data;
1163 printk(KERN_INFO "applesmc: %s detected:\n", id->ident);
1164 applesmc_accelerometer = dmi_data->accelerometer;
1165 printk(KERN_INFO "applesmc: - Model %s accelerometer\n",
1166 applesmc_accelerometer ? "with" : "without");
1167 applesmc_light = dmi_data->light;
1168 printk(KERN_INFO "applesmc: - Model %s light sensors and backlight\n",
1169 applesmc_light ? "with" : "without");
1171 applesmc_temperature_set = dmi_data->temperature_set;
1172 while (temperature_sensors_sets[applesmc_temperature_set][i] != NULL)
1173 i++;
1174 printk(KERN_INFO "applesmc: - Model with %d temperature sensors\n", i);
1175 return 1;
1178 /* Create accelerometer ressources */
1179 static int applesmc_create_accelerometer(void)
1181 struct input_dev *idev;
1182 int ret;
1184 ret = sysfs_create_group(&pdev->dev.kobj,
1185 &accelerometer_attributes_group);
1186 if (ret)
1187 goto out;
1189 applesmc_idev = input_allocate_polled_device();
1190 if (!applesmc_idev) {
1191 ret = -ENOMEM;
1192 goto out_sysfs;
1195 applesmc_idev->poll = applesmc_idev_poll;
1196 applesmc_idev->poll_interval = APPLESMC_POLL_INTERVAL;
1198 /* initial calibrate for the input device */
1199 applesmc_calibrate();
1201 /* initialize the input device */
1202 idev = applesmc_idev->input;
1203 idev->name = "applesmc";
1204 idev->id.bustype = BUS_HOST;
1205 idev->dev.parent = &pdev->dev;
1206 idev->evbit[0] = BIT_MASK(EV_ABS);
1207 input_set_abs_params(idev, ABS_X,
1208 -256, 256, APPLESMC_INPUT_FUZZ, APPLESMC_INPUT_FLAT);
1209 input_set_abs_params(idev, ABS_Y,
1210 -256, 256, APPLESMC_INPUT_FUZZ, APPLESMC_INPUT_FLAT);
1212 ret = input_register_polled_device(applesmc_idev);
1213 if (ret)
1214 goto out_idev;
1216 return 0;
1218 out_idev:
1219 input_free_polled_device(applesmc_idev);
1221 out_sysfs:
1222 sysfs_remove_group(&pdev->dev.kobj, &accelerometer_attributes_group);
1224 out:
1225 printk(KERN_WARNING "applesmc: driver init failed (ret=%d)!\n", ret);
1226 return ret;
1229 /* Release all ressources used by the accelerometer */
1230 static void applesmc_release_accelerometer(void)
1232 input_unregister_polled_device(applesmc_idev);
1233 input_free_polled_device(applesmc_idev);
1234 sysfs_remove_group(&pdev->dev.kobj, &accelerometer_attributes_group);
1237 static __initdata struct dmi_match_data applesmc_dmi_data[] = {
1238 /* MacBook Pro: accelerometer, backlight and temperature set 0 */
1239 { .accelerometer = 1, .light = 1, .temperature_set = 0 },
1240 /* MacBook2: accelerometer and temperature set 1 */
1241 { .accelerometer = 1, .light = 0, .temperature_set = 1 },
1242 /* MacBook: accelerometer and temperature set 2 */
1243 { .accelerometer = 1, .light = 0, .temperature_set = 2 },
1244 /* MacMini: temperature set 3 */
1245 { .accelerometer = 0, .light = 0, .temperature_set = 3 },
1246 /* MacPro: temperature set 4 */
1247 { .accelerometer = 0, .light = 0, .temperature_set = 4 },
1248 /* iMac: temperature set 5 */
1249 { .accelerometer = 0, .light = 0, .temperature_set = 5 },
1250 /* MacBook3: accelerometer and temperature set 6 */
1251 { .accelerometer = 1, .light = 0, .temperature_set = 6 },
1254 /* Note that DMI_MATCH(...,"MacBook") will match "MacBookPro1,1".
1255 * So we need to put "Apple MacBook Pro" before "Apple MacBook". */
1256 static __initdata struct dmi_system_id applesmc_whitelist[] = {
1257 { applesmc_dmi_match, "Apple MacBook Pro", {
1258 DMI_MATCH(DMI_BOARD_VENDOR,"Apple"),
1259 DMI_MATCH(DMI_PRODUCT_NAME,"MacBookPro") },
1260 (void*)&applesmc_dmi_data[0]},
1261 { applesmc_dmi_match, "Apple MacBook (v2)", {
1262 DMI_MATCH(DMI_BOARD_VENDOR,"Apple"),
1263 DMI_MATCH(DMI_PRODUCT_NAME,"MacBook2") },
1264 (void*)&applesmc_dmi_data[1]},
1265 { applesmc_dmi_match, "Apple MacBook (v3)", {
1266 DMI_MATCH(DMI_BOARD_VENDOR,"Apple"),
1267 DMI_MATCH(DMI_PRODUCT_NAME,"MacBook3") },
1268 (void*)&applesmc_dmi_data[6]},
1269 { applesmc_dmi_match, "Apple MacBook", {
1270 DMI_MATCH(DMI_BOARD_VENDOR,"Apple"),
1271 DMI_MATCH(DMI_PRODUCT_NAME,"MacBook") },
1272 (void*)&applesmc_dmi_data[2]},
1273 { applesmc_dmi_match, "Apple Macmini", {
1274 DMI_MATCH(DMI_BOARD_VENDOR,"Apple"),
1275 DMI_MATCH(DMI_PRODUCT_NAME,"Macmini") },
1276 (void*)&applesmc_dmi_data[3]},
1277 { applesmc_dmi_match, "Apple MacPro2", {
1278 DMI_MATCH(DMI_BOARD_VENDOR,"Apple"),
1279 DMI_MATCH(DMI_PRODUCT_NAME,"MacPro2") },
1280 (void*)&applesmc_dmi_data[4]},
1281 { applesmc_dmi_match, "Apple iMac", {
1282 DMI_MATCH(DMI_BOARD_VENDOR,"Apple"),
1283 DMI_MATCH(DMI_PRODUCT_NAME,"iMac") },
1284 (void*)&applesmc_dmi_data[5]},
1285 { .ident = NULL }
1288 static int __init applesmc_init(void)
1290 int ret;
1291 int count;
1292 int i;
1294 if (!dmi_check_system(applesmc_whitelist)) {
1295 printk(KERN_WARNING "applesmc: supported laptop not found!\n");
1296 ret = -ENODEV;
1297 goto out;
1300 if (!request_region(APPLESMC_DATA_PORT, APPLESMC_NR_PORTS,
1301 "applesmc")) {
1302 ret = -ENXIO;
1303 goto out;
1306 ret = platform_driver_register(&applesmc_driver);
1307 if (ret)
1308 goto out_region;
1310 pdev = platform_device_register_simple("applesmc", APPLESMC_DATA_PORT,
1311 NULL, 0);
1312 if (IS_ERR(pdev)) {
1313 ret = PTR_ERR(pdev);
1314 goto out_driver;
1317 ret = sysfs_create_file(&pdev->dev.kobj, &dev_attr_name.attr);
1318 if (ret)
1319 goto out_device;
1321 /* Create key enumeration sysfs files */
1322 ret = sysfs_create_group(&pdev->dev.kobj, &key_enumeration_group);
1323 if (ret)
1324 goto out_name;
1326 /* create fan files */
1327 count = applesmc_get_fan_count();
1328 if (count < 0) {
1329 printk(KERN_ERR "applesmc: Cannot get the number of fans.\n");
1330 } else {
1331 printk(KERN_INFO "applesmc: %d fans found.\n", count);
1333 switch (count) {
1334 default:
1335 printk(KERN_WARNING "applesmc: More than 4 fans found,"
1336 " but at most 4 fans are supported"
1337 " by the driver.\n");
1338 case 4:
1339 ret = sysfs_create_group(&pdev->dev.kobj,
1340 &fan_attribute_groups[3]);
1341 if (ret)
1342 goto out_key_enumeration;
1343 case 3:
1344 ret = sysfs_create_group(&pdev->dev.kobj,
1345 &fan_attribute_groups[2]);
1346 if (ret)
1347 goto out_key_enumeration;
1348 case 2:
1349 ret = sysfs_create_group(&pdev->dev.kobj,
1350 &fan_attribute_groups[1]);
1351 if (ret)
1352 goto out_key_enumeration;
1353 case 1:
1354 ret = sysfs_create_group(&pdev->dev.kobj,
1355 &fan_attribute_groups[0]);
1356 if (ret)
1357 goto out_fan_1;
1358 case 0:
1363 for (i = 0;
1364 temperature_sensors_sets[applesmc_temperature_set][i] != NULL;
1365 i++) {
1366 if (temperature_attributes[i] == NULL) {
1367 printk(KERN_ERR "applesmc: More temperature sensors "
1368 "in temperature_sensors_sets (at least %i)"
1369 "than available sysfs files in "
1370 "temperature_attributes (%i), please report "
1371 "this bug.\n", i, i-1);
1372 goto out_temperature;
1374 ret = sysfs_create_file(&pdev->dev.kobj,
1375 temperature_attributes[i]);
1376 if (ret)
1377 goto out_temperature;
1380 if (applesmc_accelerometer) {
1381 ret = applesmc_create_accelerometer();
1382 if (ret)
1383 goto out_temperature;
1386 if (applesmc_light) {
1387 /* Add light sensor file */
1388 ret = sysfs_create_file(&pdev->dev.kobj, &dev_attr_light.attr);
1389 if (ret)
1390 goto out_accelerometer;
1392 /* Create the workqueue */
1393 applesmc_led_wq = create_singlethread_workqueue("applesmc-led");
1394 if (!applesmc_led_wq) {
1395 ret = -ENOMEM;
1396 goto out_light_sysfs;
1399 /* register as a led device */
1400 ret = led_classdev_register(&pdev->dev, &applesmc_backlight);
1401 if (ret < 0)
1402 goto out_light_wq;
1405 hwmon_dev = hwmon_device_register(&pdev->dev);
1406 if (IS_ERR(hwmon_dev)) {
1407 ret = PTR_ERR(hwmon_dev);
1408 goto out_light_ledclass;
1411 printk(KERN_INFO "applesmc: driver successfully loaded.\n");
1413 return 0;
1415 out_light_ledclass:
1416 if (applesmc_light)
1417 led_classdev_unregister(&applesmc_backlight);
1418 out_light_wq:
1419 if (applesmc_light)
1420 destroy_workqueue(applesmc_led_wq);
1421 out_light_sysfs:
1422 if (applesmc_light)
1423 sysfs_remove_file(&pdev->dev.kobj, &dev_attr_light.attr);
1424 out_accelerometer:
1425 if (applesmc_accelerometer)
1426 applesmc_release_accelerometer();
1427 out_temperature:
1428 sysfs_remove_group(&pdev->dev.kobj, &temperature_attributes_group);
1429 sysfs_remove_group(&pdev->dev.kobj, &fan_attribute_groups[0]);
1430 out_fan_1:
1431 sysfs_remove_group(&pdev->dev.kobj, &fan_attribute_groups[1]);
1432 out_key_enumeration:
1433 sysfs_remove_group(&pdev->dev.kobj, &key_enumeration_group);
1434 out_name:
1435 sysfs_remove_file(&pdev->dev.kobj, &dev_attr_name.attr);
1436 out_device:
1437 platform_device_unregister(pdev);
1438 out_driver:
1439 platform_driver_unregister(&applesmc_driver);
1440 out_region:
1441 release_region(APPLESMC_DATA_PORT, APPLESMC_NR_PORTS);
1442 out:
1443 printk(KERN_WARNING "applesmc: driver init failed (ret=%d)!\n", ret);
1444 return ret;
1447 static void __exit applesmc_exit(void)
1449 hwmon_device_unregister(hwmon_dev);
1450 if (applesmc_light) {
1451 led_classdev_unregister(&applesmc_backlight);
1452 destroy_workqueue(applesmc_led_wq);
1453 sysfs_remove_file(&pdev->dev.kobj, &dev_attr_light.attr);
1455 if (applesmc_accelerometer)
1456 applesmc_release_accelerometer();
1457 sysfs_remove_group(&pdev->dev.kobj, &temperature_attributes_group);
1458 sysfs_remove_group(&pdev->dev.kobj, &fan_attribute_groups[0]);
1459 sysfs_remove_group(&pdev->dev.kobj, &fan_attribute_groups[1]);
1460 sysfs_remove_group(&pdev->dev.kobj, &key_enumeration_group);
1461 sysfs_remove_file(&pdev->dev.kobj, &dev_attr_name.attr);
1462 platform_device_unregister(pdev);
1463 platform_driver_unregister(&applesmc_driver);
1464 release_region(APPLESMC_DATA_PORT, APPLESMC_NR_PORTS);
1466 printk(KERN_INFO "applesmc: driver unloaded.\n");
1469 module_init(applesmc_init);
1470 module_exit(applesmc_exit);
1472 MODULE_AUTHOR("Nicolas Boichat");
1473 MODULE_DESCRIPTION("Apple SMC");
1474 MODULE_LICENSE("GPL v2");