ext4: Don't use ext4_dec_count() if not needed
[linux-2.6/mini2440.git] / drivers / hwmon / applesmc.c
blobaacc0c4b809cf408bc7f41e233d215c993815c7c
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 },
103 /* List of keys used to read/write fan speeds */
104 static const char* fan_speed_keys[] = {
105 FAN_ACTUAL_SPEED,
106 FAN_MIN_SPEED,
107 FAN_MAX_SPEED,
108 FAN_SAFE_SPEED,
109 FAN_TARGET_SPEED
112 #define INIT_TIMEOUT_MSECS 5000 /* wait up to 5s for device init ... */
113 #define INIT_WAIT_MSECS 50 /* ... in 50ms increments */
115 #define APPLESMC_POLL_INTERVAL 50 /* msecs */
116 #define APPLESMC_INPUT_FUZZ 4 /* input event threshold */
117 #define APPLESMC_INPUT_FLAT 4
119 #define SENSOR_X 0
120 #define SENSOR_Y 1
121 #define SENSOR_Z 2
123 /* Structure to be passed to DMI_MATCH function */
124 struct dmi_match_data {
125 /* Indicates whether this computer has an accelerometer. */
126 int accelerometer;
127 /* Indicates whether this computer has light sensors and keyboard backlight. */
128 int light;
129 /* Indicates which temperature sensors set to use. */
130 int temperature_set;
133 static const int debug;
134 static struct platform_device *pdev;
135 static s16 rest_x;
136 static s16 rest_y;
137 static struct device *hwmon_dev;
138 static struct input_polled_dev *applesmc_idev;
140 /* Indicates whether this computer has an accelerometer. */
141 static unsigned int applesmc_accelerometer;
143 /* Indicates whether this computer has light sensors and keyboard backlight. */
144 static unsigned int applesmc_light;
146 /* Indicates which temperature sensors set to use. */
147 static unsigned int applesmc_temperature_set;
149 static DEFINE_MUTEX(applesmc_lock);
152 * Last index written to key_at_index sysfs file, and value to use for all other
153 * key_at_index_* sysfs files.
155 static unsigned int key_at_index;
157 static struct workqueue_struct *applesmc_led_wq;
160 * __wait_status - Wait up to 2ms for the status port to get a certain value
161 * (masked with 0x0f), returning zero if the value is obtained. Callers must
162 * hold applesmc_lock.
164 static int __wait_status(u8 val)
166 unsigned int i;
168 val = val & APPLESMC_STATUS_MASK;
170 for (i = 0; i < 200; i++) {
171 if ((inb(APPLESMC_CMD_PORT) & APPLESMC_STATUS_MASK) == val) {
172 if (debug)
173 printk(KERN_DEBUG
174 "Waited %d us for status %x\n",
175 i*10, val);
176 return 0;
178 udelay(10);
181 printk(KERN_WARNING "applesmc: wait status failed: %x != %x\n",
182 val, inb(APPLESMC_CMD_PORT));
184 return -EIO;
188 * applesmc_read_key - reads len bytes from a given key, and put them in buffer.
189 * Returns zero on success or a negative error on failure. Callers must
190 * hold applesmc_lock.
192 static int applesmc_read_key(const char* key, u8* buffer, u8 len)
194 int i;
196 if (len > APPLESMC_MAX_DATA_LENGTH) {
197 printk(KERN_ERR "applesmc_read_key: cannot read more than "
198 "%d bytes\n", APPLESMC_MAX_DATA_LENGTH);
199 return -EINVAL;
202 outb(APPLESMC_READ_CMD, APPLESMC_CMD_PORT);
203 if (__wait_status(0x0c))
204 return -EIO;
206 for (i = 0; i < 4; i++) {
207 outb(key[i], APPLESMC_DATA_PORT);
208 if (__wait_status(0x04))
209 return -EIO;
211 if (debug)
212 printk(KERN_DEBUG "<%s", key);
214 outb(len, APPLESMC_DATA_PORT);
215 if (debug)
216 printk(KERN_DEBUG ">%x", len);
218 for (i = 0; i < len; i++) {
219 if (__wait_status(0x05))
220 return -EIO;
221 buffer[i] = inb(APPLESMC_DATA_PORT);
222 if (debug)
223 printk(KERN_DEBUG "<%x", buffer[i]);
225 if (debug)
226 printk(KERN_DEBUG "\n");
228 return 0;
232 * applesmc_write_key - writes len bytes from buffer to a given key.
233 * Returns zero on success or a negative error on failure. Callers must
234 * hold applesmc_lock.
236 static int applesmc_write_key(const char* key, u8* buffer, u8 len)
238 int i;
240 if (len > APPLESMC_MAX_DATA_LENGTH) {
241 printk(KERN_ERR "applesmc_write_key: cannot write more than "
242 "%d bytes\n", APPLESMC_MAX_DATA_LENGTH);
243 return -EINVAL;
246 outb(APPLESMC_WRITE_CMD, APPLESMC_CMD_PORT);
247 if (__wait_status(0x0c))
248 return -EIO;
250 for (i = 0; i < 4; i++) {
251 outb(key[i], APPLESMC_DATA_PORT);
252 if (__wait_status(0x04))
253 return -EIO;
256 outb(len, APPLESMC_DATA_PORT);
258 for (i = 0; i < len; i++) {
259 if (__wait_status(0x04))
260 return -EIO;
261 outb(buffer[i], APPLESMC_DATA_PORT);
264 return 0;
268 * applesmc_get_key_at_index - get key at index, and put the result in key
269 * (char[6]). Returns zero on success or a negative error on failure. Callers
270 * must hold applesmc_lock.
272 static int applesmc_get_key_at_index(int index, char* key)
274 int i;
275 u8 readkey[4];
276 readkey[0] = index >> 24;
277 readkey[1] = index >> 16;
278 readkey[2] = index >> 8;
279 readkey[3] = index;
281 outb(APPLESMC_GET_KEY_BY_INDEX_CMD, APPLESMC_CMD_PORT);
282 if (__wait_status(0x0c))
283 return -EIO;
285 for (i = 0; i < 4; i++) {
286 outb(readkey[i], APPLESMC_DATA_PORT);
287 if (__wait_status(0x04))
288 return -EIO;
291 outb(4, APPLESMC_DATA_PORT);
293 for (i = 0; i < 4; i++) {
294 if (__wait_status(0x05))
295 return -EIO;
296 key[i] = inb(APPLESMC_DATA_PORT);
298 key[4] = 0;
300 return 0;
304 * applesmc_get_key_type - get key type, and put the result in type (char[6]).
305 * Returns zero on success or a negative error on failure. Callers must
306 * hold applesmc_lock.
308 static int applesmc_get_key_type(char* key, char* type)
310 int i;
312 outb(APPLESMC_GET_KEY_TYPE_CMD, APPLESMC_CMD_PORT);
313 if (__wait_status(0x0c))
314 return -EIO;
316 for (i = 0; i < 4; i++) {
317 outb(key[i], APPLESMC_DATA_PORT);
318 if (__wait_status(0x04))
319 return -EIO;
322 outb(5, APPLESMC_DATA_PORT);
324 for (i = 0; i < 6; i++) {
325 if (__wait_status(0x05))
326 return -EIO;
327 type[i] = inb(APPLESMC_DATA_PORT);
329 type[5] = 0;
331 return 0;
335 * applesmc_read_motion_sensor - Read motion sensor (X, Y or Z). Callers must
336 * hold applesmc_lock.
338 static int applesmc_read_motion_sensor(int index, s16* value)
340 u8 buffer[2];
341 int ret;
343 switch (index) {
344 case SENSOR_X:
345 ret = applesmc_read_key(MOTION_SENSOR_X_KEY, buffer, 2);
346 break;
347 case SENSOR_Y:
348 ret = applesmc_read_key(MOTION_SENSOR_Y_KEY, buffer, 2);
349 break;
350 case SENSOR_Z:
351 ret = applesmc_read_key(MOTION_SENSOR_Z_KEY, buffer, 2);
352 break;
353 default:
354 ret = -EINVAL;
357 *value = ((s16)buffer[0] << 8) | buffer[1];
359 return ret;
363 * applesmc_device_init - initialize the accelerometer. Returns zero on success
364 * and negative error code on failure. Can sleep.
366 static int applesmc_device_init(void)
368 int total, ret = -ENXIO;
369 u8 buffer[2];
371 if (!applesmc_accelerometer)
372 return 0;
374 mutex_lock(&applesmc_lock);
376 for (total = INIT_TIMEOUT_MSECS; total > 0; total -= INIT_WAIT_MSECS) {
377 if (debug)
378 printk(KERN_DEBUG "applesmc try %d\n", total);
379 if (!applesmc_read_key(MOTION_SENSOR_KEY, buffer, 2) &&
380 (buffer[0] != 0x00 || buffer[1] != 0x00)) {
381 if (total == INIT_TIMEOUT_MSECS) {
382 printk(KERN_DEBUG "applesmc: device has"
383 " already been initialized"
384 " (0x%02x, 0x%02x).\n",
385 buffer[0], buffer[1]);
386 } else {
387 printk(KERN_DEBUG "applesmc: device"
388 " successfully initialized"
389 " (0x%02x, 0x%02x).\n",
390 buffer[0], buffer[1]);
392 ret = 0;
393 goto out;
395 buffer[0] = 0xe0;
396 buffer[1] = 0x00;
397 applesmc_write_key(MOTION_SENSOR_KEY, buffer, 2);
398 msleep(INIT_WAIT_MSECS);
401 printk(KERN_WARNING "applesmc: failed to init the device\n");
403 out:
404 mutex_unlock(&applesmc_lock);
405 return ret;
409 * applesmc_get_fan_count - get the number of fans. Callers must NOT hold
410 * applesmc_lock.
412 static int applesmc_get_fan_count(void)
414 int ret;
415 u8 buffer[1];
417 mutex_lock(&applesmc_lock);
419 ret = applesmc_read_key(FANS_COUNT, buffer, 1);
421 mutex_unlock(&applesmc_lock);
422 if (ret)
423 return ret;
424 else
425 return buffer[0];
428 /* Device model stuff */
429 static int applesmc_probe(struct platform_device *dev)
431 int ret;
433 ret = applesmc_device_init();
434 if (ret)
435 return ret;
437 printk(KERN_INFO "applesmc: device successfully initialized.\n");
438 return 0;
441 static int applesmc_resume(struct platform_device *dev)
443 return applesmc_device_init();
446 static struct platform_driver applesmc_driver = {
447 .probe = applesmc_probe,
448 .resume = applesmc_resume,
449 .driver = {
450 .name = "applesmc",
451 .owner = THIS_MODULE,
456 * applesmc_calibrate - Set our "resting" values. Callers must
457 * hold applesmc_lock.
459 static void applesmc_calibrate(void)
461 applesmc_read_motion_sensor(SENSOR_X, &rest_x);
462 applesmc_read_motion_sensor(SENSOR_Y, &rest_y);
463 rest_x = -rest_x;
466 static void applesmc_idev_poll(struct input_polled_dev *dev)
468 struct input_dev *idev = dev->input;
469 s16 x, y;
471 mutex_lock(&applesmc_lock);
473 if (applesmc_read_motion_sensor(SENSOR_X, &x))
474 goto out;
475 if (applesmc_read_motion_sensor(SENSOR_Y, &y))
476 goto out;
478 x = -x;
479 input_report_abs(idev, ABS_X, x - rest_x);
480 input_report_abs(idev, ABS_Y, y - rest_y);
481 input_sync(idev);
483 out:
484 mutex_unlock(&applesmc_lock);
487 /* Sysfs Files */
489 static ssize_t applesmc_name_show(struct device *dev,
490 struct device_attribute *attr, char *buf)
492 return snprintf(buf, PAGE_SIZE, "applesmc\n");
495 static ssize_t applesmc_position_show(struct device *dev,
496 struct device_attribute *attr, char *buf)
498 int ret;
499 s16 x, y, z;
501 mutex_lock(&applesmc_lock);
503 ret = applesmc_read_motion_sensor(SENSOR_X, &x);
504 if (ret)
505 goto out;
506 ret = applesmc_read_motion_sensor(SENSOR_Y, &y);
507 if (ret)
508 goto out;
509 ret = applesmc_read_motion_sensor(SENSOR_Z, &z);
510 if (ret)
511 goto out;
513 out:
514 mutex_unlock(&applesmc_lock);
515 if (ret)
516 return ret;
517 else
518 return snprintf(buf, PAGE_SIZE, "(%d,%d,%d)\n", x, y, z);
521 static ssize_t applesmc_light_show(struct device *dev,
522 struct device_attribute *attr, char *sysfsbuf)
524 int ret;
525 u8 left = 0, right = 0;
526 u8 buffer[6];
528 mutex_lock(&applesmc_lock);
530 ret = applesmc_read_key(LIGHT_SENSOR_LEFT_KEY, buffer, 6);
531 left = buffer[2];
532 if (ret)
533 goto out;
534 ret = applesmc_read_key(LIGHT_SENSOR_RIGHT_KEY, buffer, 6);
535 right = buffer[2];
537 out:
538 mutex_unlock(&applesmc_lock);
539 if (ret)
540 return ret;
541 else
542 return snprintf(sysfsbuf, PAGE_SIZE, "(%d,%d)\n", left, right);
545 /* Displays degree Celsius * 1000 */
546 static ssize_t applesmc_show_temperature(struct device *dev,
547 struct device_attribute *devattr, char *sysfsbuf)
549 int ret;
550 u8 buffer[2];
551 unsigned int temp;
552 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
553 const char* key =
554 temperature_sensors_sets[applesmc_temperature_set][attr->index];
556 mutex_lock(&applesmc_lock);
558 ret = applesmc_read_key(key, buffer, 2);
559 temp = buffer[0]*1000;
560 temp += (buffer[1] >> 6) * 250;
562 mutex_unlock(&applesmc_lock);
564 if (ret)
565 return ret;
566 else
567 return snprintf(sysfsbuf, PAGE_SIZE, "%u\n", temp);
570 static ssize_t applesmc_show_fan_speed(struct device *dev,
571 struct device_attribute *attr, char *sysfsbuf)
573 int ret;
574 unsigned int speed = 0;
575 char newkey[5];
576 u8 buffer[2];
577 struct sensor_device_attribute_2 *sensor_attr =
578 to_sensor_dev_attr_2(attr);
580 newkey[0] = fan_speed_keys[sensor_attr->nr][0];
581 newkey[1] = '0' + sensor_attr->index;
582 newkey[2] = fan_speed_keys[sensor_attr->nr][2];
583 newkey[3] = fan_speed_keys[sensor_attr->nr][3];
584 newkey[4] = 0;
586 mutex_lock(&applesmc_lock);
588 ret = applesmc_read_key(newkey, buffer, 2);
589 speed = ((buffer[0] << 8 | buffer[1]) >> 2);
591 mutex_unlock(&applesmc_lock);
592 if (ret)
593 return ret;
594 else
595 return snprintf(sysfsbuf, PAGE_SIZE, "%u\n", speed);
598 static ssize_t applesmc_store_fan_speed(struct device *dev,
599 struct device_attribute *attr,
600 const char *sysfsbuf, size_t count)
602 int ret;
603 u32 speed;
604 char newkey[5];
605 u8 buffer[2];
606 struct sensor_device_attribute_2 *sensor_attr =
607 to_sensor_dev_attr_2(attr);
609 speed = simple_strtoul(sysfsbuf, NULL, 10);
611 if (speed > 0x4000) /* Bigger than a 14-bit value */
612 return -EINVAL;
614 newkey[0] = fan_speed_keys[sensor_attr->nr][0];
615 newkey[1] = '0' + sensor_attr->index;
616 newkey[2] = fan_speed_keys[sensor_attr->nr][2];
617 newkey[3] = fan_speed_keys[sensor_attr->nr][3];
618 newkey[4] = 0;
620 mutex_lock(&applesmc_lock);
622 buffer[0] = (speed >> 6) & 0xff;
623 buffer[1] = (speed << 2) & 0xff;
624 ret = applesmc_write_key(newkey, buffer, 2);
626 mutex_unlock(&applesmc_lock);
627 if (ret)
628 return ret;
629 else
630 return count;
633 static ssize_t applesmc_show_fan_manual(struct device *dev,
634 struct device_attribute *devattr, char *sysfsbuf)
636 int ret;
637 u16 manual = 0;
638 u8 buffer[2];
639 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
641 mutex_lock(&applesmc_lock);
643 ret = applesmc_read_key(FANS_MANUAL, buffer, 2);
644 manual = ((buffer[0] << 8 | buffer[1]) >> attr->index) & 0x01;
646 mutex_unlock(&applesmc_lock);
647 if (ret)
648 return ret;
649 else
650 return snprintf(sysfsbuf, PAGE_SIZE, "%d\n", manual);
653 static ssize_t applesmc_store_fan_manual(struct device *dev,
654 struct device_attribute *devattr,
655 const char *sysfsbuf, size_t count)
657 int ret;
658 u8 buffer[2];
659 u32 input;
660 u16 val;
661 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
663 input = simple_strtoul(sysfsbuf, NULL, 10);
665 mutex_lock(&applesmc_lock);
667 ret = applesmc_read_key(FANS_MANUAL, buffer, 2);
668 val = (buffer[0] << 8 | buffer[1]);
669 if (ret)
670 goto out;
672 if (input)
673 val = val | (0x01 << attr->index);
674 else
675 val = val & ~(0x01 << attr->index);
677 buffer[0] = (val >> 8) & 0xFF;
678 buffer[1] = val & 0xFF;
680 ret = applesmc_write_key(FANS_MANUAL, buffer, 2);
682 out:
683 mutex_unlock(&applesmc_lock);
684 if (ret)
685 return ret;
686 else
687 return count;
690 static ssize_t applesmc_show_fan_position(struct device *dev,
691 struct device_attribute *attr, char *sysfsbuf)
693 int ret;
694 char newkey[5];
695 u8 buffer[17];
696 struct sensor_device_attribute_2 *sensor_attr =
697 to_sensor_dev_attr_2(attr);
699 newkey[0] = FAN_POSITION[0];
700 newkey[1] = '0' + sensor_attr->index;
701 newkey[2] = FAN_POSITION[2];
702 newkey[3] = FAN_POSITION[3];
703 newkey[4] = 0;
705 mutex_lock(&applesmc_lock);
707 ret = applesmc_read_key(newkey, buffer, 16);
708 buffer[16] = 0;
710 mutex_unlock(&applesmc_lock);
711 if (ret)
712 return ret;
713 else
714 return snprintf(sysfsbuf, PAGE_SIZE, "%s\n", buffer+4);
717 static ssize_t applesmc_calibrate_show(struct device *dev,
718 struct device_attribute *attr, char *sysfsbuf)
720 return snprintf(sysfsbuf, PAGE_SIZE, "(%d,%d)\n", rest_x, rest_y);
723 static ssize_t applesmc_calibrate_store(struct device *dev,
724 struct device_attribute *attr, const char *sysfsbuf, size_t count)
726 mutex_lock(&applesmc_lock);
727 applesmc_calibrate();
728 mutex_unlock(&applesmc_lock);
730 return count;
733 /* Store the next backlight value to be written by the work */
734 static unsigned int backlight_value;
736 static void applesmc_backlight_set(struct work_struct *work)
738 u8 buffer[2];
740 mutex_lock(&applesmc_lock);
741 buffer[0] = backlight_value;
742 buffer[1] = 0x00;
743 applesmc_write_key(BACKLIGHT_KEY, buffer, 2);
744 mutex_unlock(&applesmc_lock);
746 static DECLARE_WORK(backlight_work, &applesmc_backlight_set);
748 static void applesmc_brightness_set(struct led_classdev *led_cdev,
749 enum led_brightness value)
751 int ret;
753 backlight_value = value;
754 ret = queue_work(applesmc_led_wq, &backlight_work);
756 if (debug && (!ret))
757 printk(KERN_DEBUG "applesmc: work was already on the queue.\n");
760 static ssize_t applesmc_key_count_show(struct device *dev,
761 struct device_attribute *attr, char *sysfsbuf)
763 int ret;
764 u8 buffer[4];
765 u32 count;
767 mutex_lock(&applesmc_lock);
769 ret = applesmc_read_key(KEY_COUNT_KEY, buffer, 4);
770 count = ((u32)buffer[0]<<24) + ((u32)buffer[1]<<16) +
771 ((u32)buffer[2]<<8) + buffer[3];
773 mutex_unlock(&applesmc_lock);
774 if (ret)
775 return ret;
776 else
777 return snprintf(sysfsbuf, PAGE_SIZE, "%d\n", count);
780 static ssize_t applesmc_key_at_index_read_show(struct device *dev,
781 struct device_attribute *attr, char *sysfsbuf)
783 char key[5];
784 char info[6];
785 int ret;
787 mutex_lock(&applesmc_lock);
789 ret = applesmc_get_key_at_index(key_at_index, key);
791 if (ret || !key[0]) {
792 mutex_unlock(&applesmc_lock);
794 return -EINVAL;
797 ret = applesmc_get_key_type(key, info);
799 if (ret) {
800 mutex_unlock(&applesmc_lock);
802 return ret;
806 * info[0] maximum value (APPLESMC_MAX_DATA_LENGTH) is much lower than
807 * PAGE_SIZE, so we don't need any checks before writing to sysfsbuf.
809 ret = applesmc_read_key(key, sysfsbuf, info[0]);
811 mutex_unlock(&applesmc_lock);
813 if (!ret) {
814 return info[0];
815 } else {
816 return ret;
820 static ssize_t applesmc_key_at_index_data_length_show(struct device *dev,
821 struct device_attribute *attr, char *sysfsbuf)
823 char key[5];
824 char info[6];
825 int ret;
827 mutex_lock(&applesmc_lock);
829 ret = applesmc_get_key_at_index(key_at_index, key);
831 if (ret || !key[0]) {
832 mutex_unlock(&applesmc_lock);
834 return -EINVAL;
837 ret = applesmc_get_key_type(key, info);
839 mutex_unlock(&applesmc_lock);
841 if (!ret)
842 return snprintf(sysfsbuf, PAGE_SIZE, "%d\n", info[0]);
843 else
844 return ret;
847 static ssize_t applesmc_key_at_index_type_show(struct device *dev,
848 struct device_attribute *attr, char *sysfsbuf)
850 char key[5];
851 char info[6];
852 int ret;
854 mutex_lock(&applesmc_lock);
856 ret = applesmc_get_key_at_index(key_at_index, key);
858 if (ret || !key[0]) {
859 mutex_unlock(&applesmc_lock);
861 return -EINVAL;
864 ret = applesmc_get_key_type(key, info);
866 mutex_unlock(&applesmc_lock);
868 if (!ret)
869 return snprintf(sysfsbuf, PAGE_SIZE, "%s\n", info+1);
870 else
871 return ret;
874 static ssize_t applesmc_key_at_index_name_show(struct device *dev,
875 struct device_attribute *attr, char *sysfsbuf)
877 char key[5];
878 int ret;
880 mutex_lock(&applesmc_lock);
882 ret = applesmc_get_key_at_index(key_at_index, key);
884 mutex_unlock(&applesmc_lock);
886 if (!ret && key[0])
887 return snprintf(sysfsbuf, PAGE_SIZE, "%s\n", key);
888 else
889 return -EINVAL;
892 static ssize_t applesmc_key_at_index_show(struct device *dev,
893 struct device_attribute *attr, char *sysfsbuf)
895 return snprintf(sysfsbuf, PAGE_SIZE, "%d\n", key_at_index);
898 static ssize_t applesmc_key_at_index_store(struct device *dev,
899 struct device_attribute *attr, const char *sysfsbuf, size_t count)
901 mutex_lock(&applesmc_lock);
903 key_at_index = simple_strtoul(sysfsbuf, NULL, 10);
905 mutex_unlock(&applesmc_lock);
907 return count;
910 static struct led_classdev applesmc_backlight = {
911 .name = "smc::kbd_backlight",
912 .default_trigger = "nand-disk",
913 .brightness_set = applesmc_brightness_set,
916 static DEVICE_ATTR(name, 0444, applesmc_name_show, NULL);
918 static DEVICE_ATTR(position, 0444, applesmc_position_show, NULL);
919 static DEVICE_ATTR(calibrate, 0644,
920 applesmc_calibrate_show, applesmc_calibrate_store);
922 static struct attribute *accelerometer_attributes[] = {
923 &dev_attr_position.attr,
924 &dev_attr_calibrate.attr,
925 NULL
928 static const struct attribute_group accelerometer_attributes_group =
929 { .attrs = accelerometer_attributes };
931 static DEVICE_ATTR(light, 0444, applesmc_light_show, NULL);
933 static DEVICE_ATTR(key_count, 0444, applesmc_key_count_show, NULL);
934 static DEVICE_ATTR(key_at_index, 0644,
935 applesmc_key_at_index_show, applesmc_key_at_index_store);
936 static DEVICE_ATTR(key_at_index_name, 0444,
937 applesmc_key_at_index_name_show, NULL);
938 static DEVICE_ATTR(key_at_index_type, 0444,
939 applesmc_key_at_index_type_show, NULL);
940 static DEVICE_ATTR(key_at_index_data_length, 0444,
941 applesmc_key_at_index_data_length_show, NULL);
942 static DEVICE_ATTR(key_at_index_data, 0444,
943 applesmc_key_at_index_read_show, NULL);
945 static struct attribute *key_enumeration_attributes[] = {
946 &dev_attr_key_count.attr,
947 &dev_attr_key_at_index.attr,
948 &dev_attr_key_at_index_name.attr,
949 &dev_attr_key_at_index_type.attr,
950 &dev_attr_key_at_index_data_length.attr,
951 &dev_attr_key_at_index_data.attr,
952 NULL
955 static const struct attribute_group key_enumeration_group =
956 { .attrs = key_enumeration_attributes };
959 * Macro defining SENSOR_DEVICE_ATTR for a fan sysfs entries.
960 * - show actual speed
961 * - show/store minimum speed
962 * - show maximum speed
963 * - show safe speed
964 * - show/store target speed
965 * - show/store manual mode
967 #define sysfs_fan_speeds_offset(offset) \
968 static SENSOR_DEVICE_ATTR_2(fan##offset##_input, S_IRUGO, \
969 applesmc_show_fan_speed, NULL, 0, offset-1); \
971 static SENSOR_DEVICE_ATTR_2(fan##offset##_min, S_IRUGO | S_IWUSR, \
972 applesmc_show_fan_speed, applesmc_store_fan_speed, 1, offset-1); \
974 static SENSOR_DEVICE_ATTR_2(fan##offset##_max, S_IRUGO, \
975 applesmc_show_fan_speed, NULL, 2, offset-1); \
977 static SENSOR_DEVICE_ATTR_2(fan##offset##_safe, S_IRUGO, \
978 applesmc_show_fan_speed, NULL, 3, offset-1); \
980 static SENSOR_DEVICE_ATTR_2(fan##offset##_output, S_IRUGO | S_IWUSR, \
981 applesmc_show_fan_speed, applesmc_store_fan_speed, 4, offset-1); \
983 static SENSOR_DEVICE_ATTR(fan##offset##_manual, S_IRUGO | S_IWUSR, \
984 applesmc_show_fan_manual, applesmc_store_fan_manual, offset-1); \
986 static SENSOR_DEVICE_ATTR(fan##offset##_label, S_IRUGO, \
987 applesmc_show_fan_position, NULL, offset-1); \
989 static struct attribute *fan##offset##_attributes[] = { \
990 &sensor_dev_attr_fan##offset##_input.dev_attr.attr, \
991 &sensor_dev_attr_fan##offset##_min.dev_attr.attr, \
992 &sensor_dev_attr_fan##offset##_max.dev_attr.attr, \
993 &sensor_dev_attr_fan##offset##_safe.dev_attr.attr, \
994 &sensor_dev_attr_fan##offset##_output.dev_attr.attr, \
995 &sensor_dev_attr_fan##offset##_manual.dev_attr.attr, \
996 &sensor_dev_attr_fan##offset##_label.dev_attr.attr, \
997 NULL \
1001 * Create the needed functions for each fan using the macro defined above
1002 * (4 fans are supported)
1004 sysfs_fan_speeds_offset(1);
1005 sysfs_fan_speeds_offset(2);
1006 sysfs_fan_speeds_offset(3);
1007 sysfs_fan_speeds_offset(4);
1009 static const struct attribute_group fan_attribute_groups[] = {
1010 { .attrs = fan1_attributes },
1011 { .attrs = fan2_attributes },
1012 { .attrs = fan3_attributes },
1013 { .attrs = fan4_attributes },
1017 * Temperature sensors sysfs entries.
1019 static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO,
1020 applesmc_show_temperature, NULL, 0);
1021 static SENSOR_DEVICE_ATTR(temp2_input, S_IRUGO,
1022 applesmc_show_temperature, NULL, 1);
1023 static SENSOR_DEVICE_ATTR(temp3_input, S_IRUGO,
1024 applesmc_show_temperature, NULL, 2);
1025 static SENSOR_DEVICE_ATTR(temp4_input, S_IRUGO,
1026 applesmc_show_temperature, NULL, 3);
1027 static SENSOR_DEVICE_ATTR(temp5_input, S_IRUGO,
1028 applesmc_show_temperature, NULL, 4);
1029 static SENSOR_DEVICE_ATTR(temp6_input, S_IRUGO,
1030 applesmc_show_temperature, NULL, 5);
1031 static SENSOR_DEVICE_ATTR(temp7_input, S_IRUGO,
1032 applesmc_show_temperature, NULL, 6);
1033 static SENSOR_DEVICE_ATTR(temp8_input, S_IRUGO,
1034 applesmc_show_temperature, NULL, 7);
1035 static SENSOR_DEVICE_ATTR(temp9_input, S_IRUGO,
1036 applesmc_show_temperature, NULL, 8);
1037 static SENSOR_DEVICE_ATTR(temp10_input, S_IRUGO,
1038 applesmc_show_temperature, NULL, 9);
1039 static SENSOR_DEVICE_ATTR(temp11_input, S_IRUGO,
1040 applesmc_show_temperature, NULL, 10);
1041 static SENSOR_DEVICE_ATTR(temp12_input, S_IRUGO,
1042 applesmc_show_temperature, NULL, 11);
1043 static SENSOR_DEVICE_ATTR(temp13_input, S_IRUGO,
1044 applesmc_show_temperature, NULL, 12);
1045 static SENSOR_DEVICE_ATTR(temp14_input, S_IRUGO,
1046 applesmc_show_temperature, NULL, 13);
1047 static SENSOR_DEVICE_ATTR(temp15_input, S_IRUGO,
1048 applesmc_show_temperature, NULL, 14);
1049 static SENSOR_DEVICE_ATTR(temp16_input, S_IRUGO,
1050 applesmc_show_temperature, NULL, 15);
1051 static SENSOR_DEVICE_ATTR(temp17_input, S_IRUGO,
1052 applesmc_show_temperature, NULL, 16);
1053 static SENSOR_DEVICE_ATTR(temp18_input, S_IRUGO,
1054 applesmc_show_temperature, NULL, 17);
1055 static SENSOR_DEVICE_ATTR(temp19_input, S_IRUGO,
1056 applesmc_show_temperature, NULL, 18);
1057 static SENSOR_DEVICE_ATTR(temp20_input, S_IRUGO,
1058 applesmc_show_temperature, NULL, 19);
1059 static SENSOR_DEVICE_ATTR(temp21_input, S_IRUGO,
1060 applesmc_show_temperature, NULL, 20);
1061 static SENSOR_DEVICE_ATTR(temp22_input, S_IRUGO,
1062 applesmc_show_temperature, NULL, 21);
1063 static SENSOR_DEVICE_ATTR(temp23_input, S_IRUGO,
1064 applesmc_show_temperature, NULL, 22);
1065 static SENSOR_DEVICE_ATTR(temp24_input, S_IRUGO,
1066 applesmc_show_temperature, NULL, 23);
1067 static SENSOR_DEVICE_ATTR(temp25_input, S_IRUGO,
1068 applesmc_show_temperature, NULL, 24);
1069 static SENSOR_DEVICE_ATTR(temp26_input, S_IRUGO,
1070 applesmc_show_temperature, NULL, 25);
1071 static SENSOR_DEVICE_ATTR(temp27_input, S_IRUGO,
1072 applesmc_show_temperature, NULL, 26);
1073 static SENSOR_DEVICE_ATTR(temp28_input, S_IRUGO,
1074 applesmc_show_temperature, NULL, 27);
1075 static SENSOR_DEVICE_ATTR(temp29_input, S_IRUGO,
1076 applesmc_show_temperature, NULL, 28);
1077 static SENSOR_DEVICE_ATTR(temp30_input, S_IRUGO,
1078 applesmc_show_temperature, NULL, 29);
1079 static SENSOR_DEVICE_ATTR(temp31_input, S_IRUGO,
1080 applesmc_show_temperature, NULL, 30);
1081 static SENSOR_DEVICE_ATTR(temp32_input, S_IRUGO,
1082 applesmc_show_temperature, NULL, 31);
1083 static SENSOR_DEVICE_ATTR(temp33_input, S_IRUGO,
1084 applesmc_show_temperature, NULL, 32);
1085 static SENSOR_DEVICE_ATTR(temp34_input, S_IRUGO,
1086 applesmc_show_temperature, NULL, 33);
1087 static SENSOR_DEVICE_ATTR(temp35_input, S_IRUGO,
1088 applesmc_show_temperature, NULL, 34);
1090 static struct attribute *temperature_attributes[] = {
1091 &sensor_dev_attr_temp1_input.dev_attr.attr,
1092 &sensor_dev_attr_temp2_input.dev_attr.attr,
1093 &sensor_dev_attr_temp3_input.dev_attr.attr,
1094 &sensor_dev_attr_temp4_input.dev_attr.attr,
1095 &sensor_dev_attr_temp5_input.dev_attr.attr,
1096 &sensor_dev_attr_temp6_input.dev_attr.attr,
1097 &sensor_dev_attr_temp7_input.dev_attr.attr,
1098 &sensor_dev_attr_temp8_input.dev_attr.attr,
1099 &sensor_dev_attr_temp9_input.dev_attr.attr,
1100 &sensor_dev_attr_temp10_input.dev_attr.attr,
1101 &sensor_dev_attr_temp11_input.dev_attr.attr,
1102 &sensor_dev_attr_temp12_input.dev_attr.attr,
1103 &sensor_dev_attr_temp13_input.dev_attr.attr,
1104 &sensor_dev_attr_temp14_input.dev_attr.attr,
1105 &sensor_dev_attr_temp15_input.dev_attr.attr,
1106 &sensor_dev_attr_temp16_input.dev_attr.attr,
1107 &sensor_dev_attr_temp17_input.dev_attr.attr,
1108 &sensor_dev_attr_temp18_input.dev_attr.attr,
1109 &sensor_dev_attr_temp19_input.dev_attr.attr,
1110 &sensor_dev_attr_temp20_input.dev_attr.attr,
1111 &sensor_dev_attr_temp21_input.dev_attr.attr,
1112 &sensor_dev_attr_temp22_input.dev_attr.attr,
1113 &sensor_dev_attr_temp23_input.dev_attr.attr,
1114 &sensor_dev_attr_temp24_input.dev_attr.attr,
1115 &sensor_dev_attr_temp25_input.dev_attr.attr,
1116 &sensor_dev_attr_temp26_input.dev_attr.attr,
1117 &sensor_dev_attr_temp27_input.dev_attr.attr,
1118 &sensor_dev_attr_temp28_input.dev_attr.attr,
1119 &sensor_dev_attr_temp29_input.dev_attr.attr,
1120 &sensor_dev_attr_temp30_input.dev_attr.attr,
1121 &sensor_dev_attr_temp31_input.dev_attr.attr,
1122 &sensor_dev_attr_temp32_input.dev_attr.attr,
1123 &sensor_dev_attr_temp33_input.dev_attr.attr,
1124 &sensor_dev_attr_temp34_input.dev_attr.attr,
1125 &sensor_dev_attr_temp35_input.dev_attr.attr,
1126 NULL
1129 static const struct attribute_group temperature_attributes_group =
1130 { .attrs = temperature_attributes };
1132 /* Module stuff */
1135 * applesmc_dmi_match - found a match. return one, short-circuiting the hunt.
1137 static int applesmc_dmi_match(const struct dmi_system_id *id)
1139 int i = 0;
1140 struct dmi_match_data* dmi_data = id->driver_data;
1141 printk(KERN_INFO "applesmc: %s detected:\n", id->ident);
1142 applesmc_accelerometer = dmi_data->accelerometer;
1143 printk(KERN_INFO "applesmc: - Model %s accelerometer\n",
1144 applesmc_accelerometer ? "with" : "without");
1145 applesmc_light = dmi_data->light;
1146 printk(KERN_INFO "applesmc: - Model %s light sensors and backlight\n",
1147 applesmc_light ? "with" : "without");
1149 applesmc_temperature_set = dmi_data->temperature_set;
1150 while (temperature_sensors_sets[applesmc_temperature_set][i] != NULL)
1151 i++;
1152 printk(KERN_INFO "applesmc: - Model with %d temperature sensors\n", i);
1153 return 1;
1156 /* Create accelerometer ressources */
1157 static int applesmc_create_accelerometer(void)
1159 struct input_dev *idev;
1160 int ret;
1162 ret = sysfs_create_group(&pdev->dev.kobj,
1163 &accelerometer_attributes_group);
1164 if (ret)
1165 goto out;
1167 applesmc_idev = input_allocate_polled_device();
1168 if (!applesmc_idev) {
1169 ret = -ENOMEM;
1170 goto out_sysfs;
1173 applesmc_idev->poll = applesmc_idev_poll;
1174 applesmc_idev->poll_interval = APPLESMC_POLL_INTERVAL;
1176 /* initial calibrate for the input device */
1177 applesmc_calibrate();
1179 /* initialize the input device */
1180 idev = applesmc_idev->input;
1181 idev->name = "applesmc";
1182 idev->id.bustype = BUS_HOST;
1183 idev->dev.parent = &pdev->dev;
1184 idev->evbit[0] = BIT_MASK(EV_ABS);
1185 input_set_abs_params(idev, ABS_X,
1186 -256, 256, APPLESMC_INPUT_FUZZ, APPLESMC_INPUT_FLAT);
1187 input_set_abs_params(idev, ABS_Y,
1188 -256, 256, APPLESMC_INPUT_FUZZ, APPLESMC_INPUT_FLAT);
1190 ret = input_register_polled_device(applesmc_idev);
1191 if (ret)
1192 goto out_idev;
1194 return 0;
1196 out_idev:
1197 input_free_polled_device(applesmc_idev);
1199 out_sysfs:
1200 sysfs_remove_group(&pdev->dev.kobj, &accelerometer_attributes_group);
1202 out:
1203 printk(KERN_WARNING "applesmc: driver init failed (ret=%d)!\n", ret);
1204 return ret;
1207 /* Release all ressources used by the accelerometer */
1208 static void applesmc_release_accelerometer(void)
1210 input_unregister_polled_device(applesmc_idev);
1211 input_free_polled_device(applesmc_idev);
1212 sysfs_remove_group(&pdev->dev.kobj, &accelerometer_attributes_group);
1215 static __initdata struct dmi_match_data applesmc_dmi_data[] = {
1216 /* MacBook Pro: accelerometer, backlight and temperature set 0 */
1217 { .accelerometer = 1, .light = 1, .temperature_set = 0 },
1218 /* MacBook2: accelerometer and temperature set 1 */
1219 { .accelerometer = 1, .light = 0, .temperature_set = 1 },
1220 /* MacBook: accelerometer and temperature set 2 */
1221 { .accelerometer = 1, .light = 0, .temperature_set = 2 },
1222 /* MacMini: temperature set 3 */
1223 { .accelerometer = 0, .light = 0, .temperature_set = 3 },
1224 /* MacPro: temperature set 4 */
1225 { .accelerometer = 0, .light = 0, .temperature_set = 4 },
1228 /* Note that DMI_MATCH(...,"MacBook") will match "MacBookPro1,1".
1229 * So we need to put "Apple MacBook Pro" before "Apple MacBook". */
1230 static __initdata struct dmi_system_id applesmc_whitelist[] = {
1231 { applesmc_dmi_match, "Apple MacBook Pro", {
1232 DMI_MATCH(DMI_BOARD_VENDOR,"Apple"),
1233 DMI_MATCH(DMI_PRODUCT_NAME,"MacBookPro") },
1234 (void*)&applesmc_dmi_data[0]},
1235 { applesmc_dmi_match, "Apple MacBook", {
1236 DMI_MATCH(DMI_BOARD_VENDOR,"Apple"),
1237 DMI_MATCH(DMI_PRODUCT_NAME,"MacBook2") },
1238 (void*)&applesmc_dmi_data[1]},
1239 { applesmc_dmi_match, "Apple MacBook", {
1240 DMI_MATCH(DMI_BOARD_VENDOR,"Apple"),
1241 DMI_MATCH(DMI_PRODUCT_NAME,"MacBook") },
1242 (void*)&applesmc_dmi_data[2]},
1243 { applesmc_dmi_match, "Apple Macmini", {
1244 DMI_MATCH(DMI_BOARD_VENDOR,"Apple"),
1245 DMI_MATCH(DMI_PRODUCT_NAME,"Macmini") },
1246 (void*)&applesmc_dmi_data[3]},
1247 { applesmc_dmi_match, "Apple MacPro2", {
1248 DMI_MATCH(DMI_BOARD_VENDOR,"Apple"),
1249 DMI_MATCH(DMI_PRODUCT_NAME,"MacPro2") },
1250 (void*)&applesmc_dmi_data[4]},
1251 { .ident = NULL }
1254 static int __init applesmc_init(void)
1256 int ret;
1257 int count;
1258 int i;
1260 if (!dmi_check_system(applesmc_whitelist)) {
1261 printk(KERN_WARNING "applesmc: supported laptop not found!\n");
1262 ret = -ENODEV;
1263 goto out;
1266 if (!request_region(APPLESMC_DATA_PORT, APPLESMC_NR_PORTS,
1267 "applesmc")) {
1268 ret = -ENXIO;
1269 goto out;
1272 ret = platform_driver_register(&applesmc_driver);
1273 if (ret)
1274 goto out_region;
1276 pdev = platform_device_register_simple("applesmc", APPLESMC_DATA_PORT,
1277 NULL, 0);
1278 if (IS_ERR(pdev)) {
1279 ret = PTR_ERR(pdev);
1280 goto out_driver;
1283 ret = sysfs_create_file(&pdev->dev.kobj, &dev_attr_name.attr);
1284 if (ret)
1285 goto out_device;
1287 /* Create key enumeration sysfs files */
1288 ret = sysfs_create_group(&pdev->dev.kobj, &key_enumeration_group);
1289 if (ret)
1290 goto out_name;
1292 /* create fan files */
1293 count = applesmc_get_fan_count();
1294 if (count < 0) {
1295 printk(KERN_ERR "applesmc: Cannot get the number of fans.\n");
1296 } else {
1297 printk(KERN_INFO "applesmc: %d fans found.\n", count);
1299 switch (count) {
1300 default:
1301 printk(KERN_WARNING "applesmc: More than 4 fans found,"
1302 " but at most 4 fans are supported"
1303 " by the driver.\n");
1304 case 4:
1305 ret = sysfs_create_group(&pdev->dev.kobj,
1306 &fan_attribute_groups[3]);
1307 if (ret)
1308 goto out_key_enumeration;
1309 case 3:
1310 ret = sysfs_create_group(&pdev->dev.kobj,
1311 &fan_attribute_groups[2]);
1312 if (ret)
1313 goto out_key_enumeration;
1314 case 2:
1315 ret = sysfs_create_group(&pdev->dev.kobj,
1316 &fan_attribute_groups[1]);
1317 if (ret)
1318 goto out_key_enumeration;
1319 case 1:
1320 ret = sysfs_create_group(&pdev->dev.kobj,
1321 &fan_attribute_groups[0]);
1322 if (ret)
1323 goto out_fan_1;
1324 case 0:
1329 for (i = 0;
1330 temperature_sensors_sets[applesmc_temperature_set][i] != NULL;
1331 i++) {
1332 if (temperature_attributes[i] == NULL) {
1333 printk(KERN_ERR "applesmc: More temperature sensors "
1334 "in temperature_sensors_sets (at least %i)"
1335 "than available sysfs files in "
1336 "temperature_attributes (%i), please report "
1337 "this bug.\n", i, i-1);
1338 goto out_temperature;
1340 ret = sysfs_create_file(&pdev->dev.kobj,
1341 temperature_attributes[i]);
1342 if (ret)
1343 goto out_temperature;
1346 if (applesmc_accelerometer) {
1347 ret = applesmc_create_accelerometer();
1348 if (ret)
1349 goto out_temperature;
1352 if (applesmc_light) {
1353 /* Add light sensor file */
1354 ret = sysfs_create_file(&pdev->dev.kobj, &dev_attr_light.attr);
1355 if (ret)
1356 goto out_accelerometer;
1358 /* Create the workqueue */
1359 applesmc_led_wq = create_singlethread_workqueue("applesmc-led");
1360 if (!applesmc_led_wq) {
1361 ret = -ENOMEM;
1362 goto out_light_sysfs;
1365 /* register as a led device */
1366 ret = led_classdev_register(&pdev->dev, &applesmc_backlight);
1367 if (ret < 0)
1368 goto out_light_wq;
1371 hwmon_dev = hwmon_device_register(&pdev->dev);
1372 if (IS_ERR(hwmon_dev)) {
1373 ret = PTR_ERR(hwmon_dev);
1374 goto out_light_ledclass;
1377 printk(KERN_INFO "applesmc: driver successfully loaded.\n");
1379 return 0;
1381 out_light_ledclass:
1382 if (applesmc_light)
1383 led_classdev_unregister(&applesmc_backlight);
1384 out_light_wq:
1385 if (applesmc_light)
1386 destroy_workqueue(applesmc_led_wq);
1387 out_light_sysfs:
1388 if (applesmc_light)
1389 sysfs_remove_file(&pdev->dev.kobj, &dev_attr_light.attr);
1390 out_accelerometer:
1391 if (applesmc_accelerometer)
1392 applesmc_release_accelerometer();
1393 out_temperature:
1394 sysfs_remove_group(&pdev->dev.kobj, &temperature_attributes_group);
1395 sysfs_remove_group(&pdev->dev.kobj, &fan_attribute_groups[0]);
1396 out_fan_1:
1397 sysfs_remove_group(&pdev->dev.kobj, &fan_attribute_groups[1]);
1398 out_key_enumeration:
1399 sysfs_remove_group(&pdev->dev.kobj, &key_enumeration_group);
1400 out_name:
1401 sysfs_remove_file(&pdev->dev.kobj, &dev_attr_name.attr);
1402 out_device:
1403 platform_device_unregister(pdev);
1404 out_driver:
1405 platform_driver_unregister(&applesmc_driver);
1406 out_region:
1407 release_region(APPLESMC_DATA_PORT, APPLESMC_NR_PORTS);
1408 out:
1409 printk(KERN_WARNING "applesmc: driver init failed (ret=%d)!\n", ret);
1410 return ret;
1413 static void __exit applesmc_exit(void)
1415 hwmon_device_unregister(hwmon_dev);
1416 if (applesmc_light) {
1417 led_classdev_unregister(&applesmc_backlight);
1418 destroy_workqueue(applesmc_led_wq);
1419 sysfs_remove_file(&pdev->dev.kobj, &dev_attr_light.attr);
1421 if (applesmc_accelerometer)
1422 applesmc_release_accelerometer();
1423 sysfs_remove_group(&pdev->dev.kobj, &temperature_attributes_group);
1424 sysfs_remove_group(&pdev->dev.kobj, &fan_attribute_groups[0]);
1425 sysfs_remove_group(&pdev->dev.kobj, &fan_attribute_groups[1]);
1426 sysfs_remove_group(&pdev->dev.kobj, &key_enumeration_group);
1427 sysfs_remove_file(&pdev->dev.kobj, &dev_attr_name.attr);
1428 platform_device_unregister(pdev);
1429 platform_driver_unregister(&applesmc_driver);
1430 release_region(APPLESMC_DATA_PORT, APPLESMC_NR_PORTS);
1432 printk(KERN_INFO "applesmc: driver unloaded.\n");
1435 module_init(applesmc_init);
1436 module_exit(applesmc_exit);
1438 MODULE_AUTHOR("Nicolas Boichat");
1439 MODULE_DESCRIPTION("Apple SMC");
1440 MODULE_LICENSE("GPL v2");