KVM: x86: fix CR8 handling
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / hwmon / applesmc.c
blobb6598aa557a0dcfe56fb87902319dff2394b6934
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 <linux/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_MIN_WAIT 0x0040
53 #define APPLESMC_MAX_WAIT 0x8000
55 #define APPLESMC_STATUS_MASK 0x0f
56 #define APPLESMC_READ_CMD 0x10
57 #define APPLESMC_WRITE_CMD 0x11
58 #define APPLESMC_GET_KEY_BY_INDEX_CMD 0x12
59 #define APPLESMC_GET_KEY_TYPE_CMD 0x13
61 #define KEY_COUNT_KEY "#KEY" /* r-o ui32 */
63 #define LIGHT_SENSOR_LEFT_KEY "ALV0" /* r-o {alv (6-10 bytes) */
64 #define LIGHT_SENSOR_RIGHT_KEY "ALV1" /* r-o {alv (6-10 bytes) */
65 #define BACKLIGHT_KEY "LKSB" /* w-o {lkb (2 bytes) */
67 #define CLAMSHELL_KEY "MSLD" /* r-o ui8 (unused) */
69 #define MOTION_SENSOR_X_KEY "MO_X" /* r-o sp78 (2 bytes) */
70 #define MOTION_SENSOR_Y_KEY "MO_Y" /* r-o sp78 (2 bytes) */
71 #define MOTION_SENSOR_Z_KEY "MO_Z" /* r-o sp78 (2 bytes) */
72 #define MOTION_SENSOR_KEY "MOCN" /* r/w ui16 */
74 #define FANS_COUNT "FNum" /* r-o ui8 */
75 #define FANS_MANUAL "FS! " /* r-w ui16 */
76 #define FAN_ACTUAL_SPEED "F0Ac" /* r-o fpe2 (2 bytes) */
77 #define FAN_MIN_SPEED "F0Mn" /* r-o fpe2 (2 bytes) */
78 #define FAN_MAX_SPEED "F0Mx" /* r-o fpe2 (2 bytes) */
79 #define FAN_SAFE_SPEED "F0Sf" /* r-o fpe2 (2 bytes) */
80 #define FAN_TARGET_SPEED "F0Tg" /* r-w fpe2 (2 bytes) */
81 #define FAN_POSITION "F0ID" /* r-o char[16] */
84 * Temperature sensors keys (sp78 - 2 bytes).
86 static const char *temperature_sensors_sets[][41] = {
87 /* Set 0: Macbook Pro */
88 { "TA0P", "TB0T", "TC0D", "TC0P", "TG0H", "TG0P", "TG0T", "Th0H",
89 "Th1H", "Tm0P", "Ts0P", "Ts1P", NULL },
90 /* Set 1: Macbook2 set */
91 { "TB0T", "TC0D", "TC0P", "TM0P", "TN0P", "TN1P", "TTF0", "Th0H",
92 "Th0S", "Th1H", NULL },
93 /* Set 2: Macbook set */
94 { "TB0T", "TC0D", "TC0P", "TM0P", "TN0P", "TN1P", "Th0H", "Th0S",
95 "Th1H", "Ts0P", NULL },
96 /* Set 3: Macmini set */
97 { "TC0D", "TC0P", NULL },
98 /* Set 4: Mac Pro (2 x Quad-Core) */
99 { "TA0P", "TCAG", "TCAH", "TCBG", "TCBH", "TC0C", "TC0D", "TC0P",
100 "TC1C", "TC1D", "TC2C", "TC2D", "TC3C", "TC3D", "THTG", "TH0P",
101 "TH1P", "TH2P", "TH3P", "TMAP", "TMAS", "TMBS", "TM0P", "TM0S",
102 "TM1P", "TM1S", "TM2P", "TM2S", "TM3S", "TM8P", "TM8S", "TM9P",
103 "TM9S", "TN0H", "TS0C", NULL },
104 /* Set 5: iMac */
105 { "TC0D", "TA0P", "TG0P", "TG0D", "TG0H", "TH0P", "Tm0P", "TO0P",
106 "Tp0C", NULL },
107 /* Set 6: Macbook3 set */
108 { "TB0T", "TC0D", "TC0P", "TM0P", "TN0P", "TTF0", "TW0P", "Th0H",
109 "Th0S", "Th1H", NULL },
110 /* Set 7: Macbook Air */
111 { "TB0T", "TB1S", "TB1T", "TB2S", "TB2T", "TC0D", "TC0P", "TCFP",
112 "TTF0", "TW0P", "Th0H", "Tp0P", "TpFP", "Ts0P", "Ts0S", NULL },
113 /* Set 8: Macbook Pro 4,1 (Penryn) */
114 { "TB0T", "TC0D", "TC0P", "TG0D", "TG0H", "TTF0", "TW0P", "Th0H",
115 "Th1H", "Th2H", "Tm0P", "Ts0P", NULL },
116 /* Set 9: Macbook Pro 3,1 (Santa Rosa) */
117 { "TALP", "TB0T", "TC0D", "TC0P", "TG0D", "TG0H", "TTF0", "TW0P",
118 "Th0H", "Th1H", "Th2H", "Tm0P", "Ts0P", NULL },
119 /* Set 10: iMac 5,1 */
120 { "TA0P", "TC0D", "TC0P", "TG0D", "TH0P", "TO0P", "Tm0P", NULL },
121 /* Set 11: Macbook 5,1 */
122 { "TB0T", "TB1T", "TB2T", "TB3T", "TC0D", "TC0P", "TN0D", "TN0P",
123 "TTF0", "Th0H", "Th1H", "ThFH", "Ts0P", "Ts0S", NULL },
124 /* Set 12: Macbook Pro 5,1 */
125 { "TB0T", "TB1T", "TB2T", "TB3T", "TC0D", "TC0F", "TC0P", "TG0D",
126 "TG0F", "TG0H", "TG0P", "TG0T", "TG1H", "TN0D", "TN0P", "TTF0",
127 "Th2H", "Tm0P", "Ts0P", "Ts0S", NULL },
128 /* Set 13: iMac 8,1 */
129 { "TA0P", "TC0D", "TC0H", "TC0P", "TG0D", "TG0H", "TG0P", "TH0P",
130 "TL0P", "TO0P", "TW0P", "Tm0P", "Tp0P", NULL },
131 /* Set 14: iMac 6,1 */
132 { "TA0P", "TC0D", "TC0H", "TC0P", "TG0D", "TG0H", "TG0P", "TH0P",
133 "TO0P", "Tp0P", NULL },
134 /* Set 15: MacBook Air 2,1 */
135 { "TB0T", "TB1S", "TB1T", "TB2S", "TB2T", "TC0D", "TN0D", "TTF0",
136 "TV0P", "TVFP", "TW0P", "Th0P", "Tp0P", "Tp1P", "TpFP", "Ts0P",
137 "Ts0S", NULL },
138 /* Set 16: Mac Pro 3,1 (2 x Quad-Core) */
139 { "TA0P", "TCAG", "TCAH", "TCBG", "TCBH", "TC0C", "TC0D", "TC0P",
140 "TC1C", "TC1D", "TC2C", "TC2D", "TC3C", "TC3D", "TH0P", "TH1P",
141 "TH2P", "TH3P", "TMAP", "TMAS", "TMBS", "TM0P", "TM0S", "TM1P",
142 "TM1S", "TM2P", "TM2S", "TM3S", "TM8P", "TM8S", "TM9P", "TM9S",
143 "TN0C", "TN0D", "TN0H", "TS0C", "Tp0C", "Tp1C", "Tv0S", "Tv1S",
144 NULL },
145 /* Set 17: iMac 9,1 */
146 { "TA0P", "TC0D", "TC0H", "TC0P", "TG0D", "TG0H", "TH0P", "TL0P",
147 "TN0D", "TN0H", "TN0P", "TO0P", "Tm0P", "Tp0P", NULL },
148 /* Set 18: MacBook Pro 2,2 */
149 { "TB0T", "TC0D", "TC0P", "TG0H", "TG0P", "TG0T", "TM0P", "TTF0",
150 "Th0H", "Th1H", "Tm0P", "Ts0P", NULL },
151 /* Set 19: Macbook Pro 5,3 */
152 { "TB0T", "TB1T", "TB2T", "TB3T", "TC0D", "TC0F", "TC0P", "TG0D",
153 "TG0F", "TG0H", "TG0P", "TG0T", "TN0D", "TN0P", "TTF0", "Th2H",
154 "Tm0P", "Ts0P", "Ts0S", NULL },
155 /* Set 20: MacBook Pro 5,4 */
156 { "TB0T", "TB1T", "TB2T", "TB3T", "TC0D", "TC0F", "TC0P", "TN0D",
157 "TN0P", "TTF0", "Th2H", "Ts0P", "Ts0S", NULL },
158 /* Set 21: MacBook Pro 6,2 */
159 { "TB0T", "TB1T", "TB2T", "TC0C", "TC0D", "TC0P", "TC1C", "TG0D",
160 "TG0P", "TG0T", "TMCD", "TP0P", "TPCD", "Th1H", "Th2H", "Tm0P",
161 "Ts0P", "Ts0S", NULL },
162 /* Set 22: MacBook Pro 7,1 */
163 { "TB0T", "TB1T", "TB2T", "TC0D", "TC0P", "TN0D", "TN0P", "TN0S",
164 "TN1D", "TN1F", "TN1G", "TN1S", "Th1H", "Ts0P", "Ts0S", NULL },
167 /* List of keys used to read/write fan speeds */
168 static const char* fan_speed_keys[] = {
169 FAN_ACTUAL_SPEED,
170 FAN_MIN_SPEED,
171 FAN_MAX_SPEED,
172 FAN_SAFE_SPEED,
173 FAN_TARGET_SPEED
176 #define INIT_TIMEOUT_MSECS 5000 /* wait up to 5s for device init ... */
177 #define INIT_WAIT_MSECS 50 /* ... in 50ms increments */
179 #define APPLESMC_POLL_INTERVAL 50 /* msecs */
180 #define APPLESMC_INPUT_FUZZ 4 /* input event threshold */
181 #define APPLESMC_INPUT_FLAT 4
183 #define SENSOR_X 0
184 #define SENSOR_Y 1
185 #define SENSOR_Z 2
187 /* Structure to be passed to DMI_MATCH function */
188 struct dmi_match_data {
189 /* Indicates whether this computer has an accelerometer. */
190 int accelerometer;
191 /* Indicates whether this computer has light sensors and keyboard backlight. */
192 int light;
193 /* Indicates which temperature sensors set to use. */
194 int temperature_set;
197 static const int debug;
198 static struct platform_device *pdev;
199 static s16 rest_x;
200 static s16 rest_y;
201 static u8 backlight_state[2];
203 static struct device *hwmon_dev;
204 static struct input_polled_dev *applesmc_idev;
206 /* Indicates whether this computer has an accelerometer. */
207 static unsigned int applesmc_accelerometer;
209 /* Indicates whether this computer has light sensors and keyboard backlight. */
210 static unsigned int applesmc_light;
212 /* The number of fans handled by the driver */
213 static unsigned int fans_handled;
215 /* Indicates which temperature sensors set to use. */
216 static unsigned int applesmc_temperature_set;
218 static DEFINE_MUTEX(applesmc_lock);
221 * Last index written to key_at_index sysfs file, and value to use for all other
222 * key_at_index_* sysfs files.
224 static unsigned int key_at_index;
226 static struct workqueue_struct *applesmc_led_wq;
229 * __wait_status - Wait up to 32ms for the status port to get a certain value
230 * (masked with 0x0f), returning zero if the value is obtained. Callers must
231 * hold applesmc_lock.
233 static int __wait_status(u8 val)
235 int us;
237 val = val & APPLESMC_STATUS_MASK;
239 for (us = APPLESMC_MIN_WAIT; us < APPLESMC_MAX_WAIT; us <<= 1) {
240 udelay(us);
241 if ((inb(APPLESMC_CMD_PORT) & APPLESMC_STATUS_MASK) == val) {
242 if (debug)
243 printk(KERN_DEBUG
244 "Waited %d us for status %x\n",
245 2 * us - APPLESMC_MIN_WAIT, val);
246 return 0;
250 printk(KERN_WARNING "applesmc: wait status failed: %x != %x\n",
251 val, inb(APPLESMC_CMD_PORT));
253 return -EIO;
257 * special treatment of command port - on newer macbooks, it seems necessary
258 * to resend the command byte before polling the status again. Callers must
259 * hold applesmc_lock.
261 static int send_command(u8 cmd)
263 int us;
264 for (us = APPLESMC_MIN_WAIT; us < APPLESMC_MAX_WAIT; us <<= 1) {
265 outb(cmd, APPLESMC_CMD_PORT);
266 udelay(us);
267 if ((inb(APPLESMC_CMD_PORT) & APPLESMC_STATUS_MASK) == 0x0c)
268 return 0;
270 printk(KERN_WARNING "applesmc: command failed: %x -> %x\n",
271 cmd, inb(APPLESMC_CMD_PORT));
272 return -EIO;
276 * applesmc_read_key - reads len bytes from a given key, and put them in buffer.
277 * Returns zero on success or a negative error on failure. Callers must
278 * hold applesmc_lock.
280 static int applesmc_read_key(const char* key, u8* buffer, u8 len)
282 int i;
284 if (len > APPLESMC_MAX_DATA_LENGTH) {
285 printk(KERN_ERR "applesmc_read_key: cannot read more than "
286 "%d bytes\n", APPLESMC_MAX_DATA_LENGTH);
287 return -EINVAL;
290 if (send_command(APPLESMC_READ_CMD))
291 return -EIO;
293 for (i = 0; i < 4; i++) {
294 outb(key[i], APPLESMC_DATA_PORT);
295 if (__wait_status(0x04))
296 return -EIO;
298 if (debug)
299 printk(KERN_DEBUG "<%s", key);
301 outb(len, APPLESMC_DATA_PORT);
302 if (debug)
303 printk(KERN_DEBUG ">%x", len);
305 for (i = 0; i < len; i++) {
306 if (__wait_status(0x05))
307 return -EIO;
308 buffer[i] = inb(APPLESMC_DATA_PORT);
309 if (debug)
310 printk(KERN_DEBUG "<%x", buffer[i]);
312 if (debug)
313 printk(KERN_DEBUG "\n");
315 return 0;
319 * applesmc_write_key - writes len bytes from buffer to a given key.
320 * Returns zero on success or a negative error on failure. Callers must
321 * hold applesmc_lock.
323 static int applesmc_write_key(const char* key, u8* buffer, u8 len)
325 int i;
327 if (len > APPLESMC_MAX_DATA_LENGTH) {
328 printk(KERN_ERR "applesmc_write_key: cannot write more than "
329 "%d bytes\n", APPLESMC_MAX_DATA_LENGTH);
330 return -EINVAL;
333 if (send_command(APPLESMC_WRITE_CMD))
334 return -EIO;
336 for (i = 0; i < 4; i++) {
337 outb(key[i], APPLESMC_DATA_PORT);
338 if (__wait_status(0x04))
339 return -EIO;
342 outb(len, APPLESMC_DATA_PORT);
344 for (i = 0; i < len; i++) {
345 if (__wait_status(0x04))
346 return -EIO;
347 outb(buffer[i], APPLESMC_DATA_PORT);
350 return 0;
354 * applesmc_get_key_at_index - get key at index, and put the result in key
355 * (char[6]). Returns zero on success or a negative error on failure. Callers
356 * must hold applesmc_lock.
358 static int applesmc_get_key_at_index(int index, char* key)
360 int i;
361 u8 readkey[4];
362 readkey[0] = index >> 24;
363 readkey[1] = index >> 16;
364 readkey[2] = index >> 8;
365 readkey[3] = index;
367 if (send_command(APPLESMC_GET_KEY_BY_INDEX_CMD))
368 return -EIO;
370 for (i = 0; i < 4; i++) {
371 outb(readkey[i], APPLESMC_DATA_PORT);
372 if (__wait_status(0x04))
373 return -EIO;
376 outb(4, APPLESMC_DATA_PORT);
378 for (i = 0; i < 4; i++) {
379 if (__wait_status(0x05))
380 return -EIO;
381 key[i] = inb(APPLESMC_DATA_PORT);
383 key[4] = 0;
385 return 0;
389 * applesmc_get_key_type - get key type, and put the result in type (char[6]).
390 * Returns zero on success or a negative error on failure. Callers must
391 * hold applesmc_lock.
393 static int applesmc_get_key_type(char* key, char* type)
395 int i;
397 if (send_command(APPLESMC_GET_KEY_TYPE_CMD))
398 return -EIO;
400 for (i = 0; i < 4; i++) {
401 outb(key[i], APPLESMC_DATA_PORT);
402 if (__wait_status(0x04))
403 return -EIO;
406 outb(6, APPLESMC_DATA_PORT);
408 for (i = 0; i < 6; i++) {
409 if (__wait_status(0x05))
410 return -EIO;
411 type[i] = inb(APPLESMC_DATA_PORT);
413 type[5] = 0;
415 return 0;
419 * applesmc_read_motion_sensor - Read motion sensor (X, Y or Z). Callers must
420 * hold applesmc_lock.
422 static int applesmc_read_motion_sensor(int index, s16* value)
424 u8 buffer[2];
425 int ret;
427 switch (index) {
428 case SENSOR_X:
429 ret = applesmc_read_key(MOTION_SENSOR_X_KEY, buffer, 2);
430 break;
431 case SENSOR_Y:
432 ret = applesmc_read_key(MOTION_SENSOR_Y_KEY, buffer, 2);
433 break;
434 case SENSOR_Z:
435 ret = applesmc_read_key(MOTION_SENSOR_Z_KEY, buffer, 2);
436 break;
437 default:
438 ret = -EINVAL;
441 *value = ((s16)buffer[0] << 8) | buffer[1];
443 return ret;
447 * applesmc_device_init - initialize the accelerometer. Returns zero on success
448 * and negative error code on failure. Can sleep.
450 static int applesmc_device_init(void)
452 int total, ret = -ENXIO;
453 u8 buffer[2];
455 if (!applesmc_accelerometer)
456 return 0;
458 mutex_lock(&applesmc_lock);
460 for (total = INIT_TIMEOUT_MSECS; total > 0; total -= INIT_WAIT_MSECS) {
461 if (debug)
462 printk(KERN_DEBUG "applesmc try %d\n", total);
463 if (!applesmc_read_key(MOTION_SENSOR_KEY, buffer, 2) &&
464 (buffer[0] != 0x00 || buffer[1] != 0x00)) {
465 if (total == INIT_TIMEOUT_MSECS) {
466 printk(KERN_DEBUG "applesmc: device has"
467 " already been initialized"
468 " (0x%02x, 0x%02x).\n",
469 buffer[0], buffer[1]);
470 } else {
471 printk(KERN_DEBUG "applesmc: device"
472 " successfully initialized"
473 " (0x%02x, 0x%02x).\n",
474 buffer[0], buffer[1]);
476 ret = 0;
477 goto out;
479 buffer[0] = 0xe0;
480 buffer[1] = 0x00;
481 applesmc_write_key(MOTION_SENSOR_KEY, buffer, 2);
482 msleep(INIT_WAIT_MSECS);
485 printk(KERN_WARNING "applesmc: failed to init the device\n");
487 out:
488 mutex_unlock(&applesmc_lock);
489 return ret;
493 * applesmc_get_fan_count - get the number of fans. Callers must NOT hold
494 * applesmc_lock.
496 static int applesmc_get_fan_count(void)
498 int ret;
499 u8 buffer[1];
501 mutex_lock(&applesmc_lock);
503 ret = applesmc_read_key(FANS_COUNT, buffer, 1);
505 mutex_unlock(&applesmc_lock);
506 if (ret)
507 return ret;
508 else
509 return buffer[0];
512 /* Device model stuff */
513 static int applesmc_probe(struct platform_device *dev)
515 int ret;
517 ret = applesmc_device_init();
518 if (ret)
519 return ret;
521 printk(KERN_INFO "applesmc: device successfully initialized.\n");
522 return 0;
525 /* Synchronize device with memorized backlight state */
526 static int applesmc_pm_resume(struct device *dev)
528 mutex_lock(&applesmc_lock);
529 if (applesmc_light)
530 applesmc_write_key(BACKLIGHT_KEY, backlight_state, 2);
531 mutex_unlock(&applesmc_lock);
532 return 0;
535 /* Reinitialize device on resume from hibernation */
536 static int applesmc_pm_restore(struct device *dev)
538 int ret = applesmc_device_init();
539 if (ret)
540 return ret;
541 return applesmc_pm_resume(dev);
544 static const struct dev_pm_ops applesmc_pm_ops = {
545 .resume = applesmc_pm_resume,
546 .restore = applesmc_pm_restore,
549 static struct platform_driver applesmc_driver = {
550 .probe = applesmc_probe,
551 .driver = {
552 .name = "applesmc",
553 .owner = THIS_MODULE,
554 .pm = &applesmc_pm_ops,
559 * applesmc_calibrate - Set our "resting" values. Callers must
560 * hold applesmc_lock.
562 static void applesmc_calibrate(void)
564 applesmc_read_motion_sensor(SENSOR_X, &rest_x);
565 applesmc_read_motion_sensor(SENSOR_Y, &rest_y);
566 rest_x = -rest_x;
569 static void applesmc_idev_poll(struct input_polled_dev *dev)
571 struct input_dev *idev = dev->input;
572 s16 x, y;
574 mutex_lock(&applesmc_lock);
576 if (applesmc_read_motion_sensor(SENSOR_X, &x))
577 goto out;
578 if (applesmc_read_motion_sensor(SENSOR_Y, &y))
579 goto out;
581 x = -x;
582 input_report_abs(idev, ABS_X, x - rest_x);
583 input_report_abs(idev, ABS_Y, y - rest_y);
584 input_sync(idev);
586 out:
587 mutex_unlock(&applesmc_lock);
590 /* Sysfs Files */
592 static ssize_t applesmc_name_show(struct device *dev,
593 struct device_attribute *attr, char *buf)
595 return snprintf(buf, PAGE_SIZE, "applesmc\n");
598 static ssize_t applesmc_position_show(struct device *dev,
599 struct device_attribute *attr, char *buf)
601 int ret;
602 s16 x, y, z;
604 mutex_lock(&applesmc_lock);
606 ret = applesmc_read_motion_sensor(SENSOR_X, &x);
607 if (ret)
608 goto out;
609 ret = applesmc_read_motion_sensor(SENSOR_Y, &y);
610 if (ret)
611 goto out;
612 ret = applesmc_read_motion_sensor(SENSOR_Z, &z);
613 if (ret)
614 goto out;
616 out:
617 mutex_unlock(&applesmc_lock);
618 if (ret)
619 return ret;
620 else
621 return snprintf(buf, PAGE_SIZE, "(%d,%d,%d)\n", x, y, z);
624 static ssize_t applesmc_light_show(struct device *dev,
625 struct device_attribute *attr, char *sysfsbuf)
627 static int data_length;
628 int ret;
629 u8 left = 0, right = 0;
630 u8 buffer[10], query[6];
632 mutex_lock(&applesmc_lock);
634 if (!data_length) {
635 ret = applesmc_get_key_type(LIGHT_SENSOR_LEFT_KEY, query);
636 if (ret)
637 goto out;
638 data_length = clamp_val(query[0], 0, 10);
639 printk(KERN_INFO "applesmc: light sensor data length set to "
640 "%d\n", data_length);
643 ret = applesmc_read_key(LIGHT_SENSOR_LEFT_KEY, buffer, data_length);
644 /* newer macbooks report a single 10-bit bigendian value */
645 if (data_length == 10) {
646 left = be16_to_cpu(*(__be16 *)(buffer + 6)) >> 2;
647 goto out;
649 left = buffer[2];
650 if (ret)
651 goto out;
652 ret = applesmc_read_key(LIGHT_SENSOR_RIGHT_KEY, buffer, data_length);
653 right = buffer[2];
655 out:
656 mutex_unlock(&applesmc_lock);
657 if (ret)
658 return ret;
659 else
660 return snprintf(sysfsbuf, PAGE_SIZE, "(%d,%d)\n", left, right);
663 /* Displays sensor key as label */
664 static ssize_t applesmc_show_sensor_label(struct device *dev,
665 struct device_attribute *devattr, char *sysfsbuf)
667 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
668 const char *key =
669 temperature_sensors_sets[applesmc_temperature_set][attr->index];
671 return snprintf(sysfsbuf, PAGE_SIZE, "%s\n", key);
674 /* Displays degree Celsius * 1000 */
675 static ssize_t applesmc_show_temperature(struct device *dev,
676 struct device_attribute *devattr, char *sysfsbuf)
678 int ret;
679 u8 buffer[2];
680 unsigned int temp;
681 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
682 const char* key =
683 temperature_sensors_sets[applesmc_temperature_set][attr->index];
685 mutex_lock(&applesmc_lock);
687 ret = applesmc_read_key(key, buffer, 2);
688 temp = buffer[0]*1000;
689 temp += (buffer[1] >> 6) * 250;
691 mutex_unlock(&applesmc_lock);
693 if (ret)
694 return ret;
695 else
696 return snprintf(sysfsbuf, PAGE_SIZE, "%u\n", temp);
699 static ssize_t applesmc_show_fan_speed(struct device *dev,
700 struct device_attribute *attr, char *sysfsbuf)
702 int ret;
703 unsigned int speed = 0;
704 char newkey[5];
705 u8 buffer[2];
706 struct sensor_device_attribute_2 *sensor_attr =
707 to_sensor_dev_attr_2(attr);
709 newkey[0] = fan_speed_keys[sensor_attr->nr][0];
710 newkey[1] = '0' + sensor_attr->index;
711 newkey[2] = fan_speed_keys[sensor_attr->nr][2];
712 newkey[3] = fan_speed_keys[sensor_attr->nr][3];
713 newkey[4] = 0;
715 mutex_lock(&applesmc_lock);
717 ret = applesmc_read_key(newkey, buffer, 2);
718 speed = ((buffer[0] << 8 | buffer[1]) >> 2);
720 mutex_unlock(&applesmc_lock);
721 if (ret)
722 return ret;
723 else
724 return snprintf(sysfsbuf, PAGE_SIZE, "%u\n", speed);
727 static ssize_t applesmc_store_fan_speed(struct device *dev,
728 struct device_attribute *attr,
729 const char *sysfsbuf, size_t count)
731 int ret;
732 u32 speed;
733 char newkey[5];
734 u8 buffer[2];
735 struct sensor_device_attribute_2 *sensor_attr =
736 to_sensor_dev_attr_2(attr);
738 speed = simple_strtoul(sysfsbuf, NULL, 10);
740 if (speed > 0x4000) /* Bigger than a 14-bit value */
741 return -EINVAL;
743 newkey[0] = fan_speed_keys[sensor_attr->nr][0];
744 newkey[1] = '0' + sensor_attr->index;
745 newkey[2] = fan_speed_keys[sensor_attr->nr][2];
746 newkey[3] = fan_speed_keys[sensor_attr->nr][3];
747 newkey[4] = 0;
749 mutex_lock(&applesmc_lock);
751 buffer[0] = (speed >> 6) & 0xff;
752 buffer[1] = (speed << 2) & 0xff;
753 ret = applesmc_write_key(newkey, buffer, 2);
755 mutex_unlock(&applesmc_lock);
756 if (ret)
757 return ret;
758 else
759 return count;
762 static ssize_t applesmc_show_fan_manual(struct device *dev,
763 struct device_attribute *devattr, char *sysfsbuf)
765 int ret;
766 u16 manual = 0;
767 u8 buffer[2];
768 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
770 mutex_lock(&applesmc_lock);
772 ret = applesmc_read_key(FANS_MANUAL, buffer, 2);
773 manual = ((buffer[0] << 8 | buffer[1]) >> attr->index) & 0x01;
775 mutex_unlock(&applesmc_lock);
776 if (ret)
777 return ret;
778 else
779 return snprintf(sysfsbuf, PAGE_SIZE, "%d\n", manual);
782 static ssize_t applesmc_store_fan_manual(struct device *dev,
783 struct device_attribute *devattr,
784 const char *sysfsbuf, size_t count)
786 int ret;
787 u8 buffer[2];
788 u32 input;
789 u16 val;
790 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
792 input = simple_strtoul(sysfsbuf, NULL, 10);
794 mutex_lock(&applesmc_lock);
796 ret = applesmc_read_key(FANS_MANUAL, buffer, 2);
797 val = (buffer[0] << 8 | buffer[1]);
798 if (ret)
799 goto out;
801 if (input)
802 val = val | (0x01 << attr->index);
803 else
804 val = val & ~(0x01 << attr->index);
806 buffer[0] = (val >> 8) & 0xFF;
807 buffer[1] = val & 0xFF;
809 ret = applesmc_write_key(FANS_MANUAL, buffer, 2);
811 out:
812 mutex_unlock(&applesmc_lock);
813 if (ret)
814 return ret;
815 else
816 return count;
819 static ssize_t applesmc_show_fan_position(struct device *dev,
820 struct device_attribute *attr, char *sysfsbuf)
822 int ret;
823 char newkey[5];
824 u8 buffer[17];
825 struct sensor_device_attribute_2 *sensor_attr =
826 to_sensor_dev_attr_2(attr);
828 newkey[0] = FAN_POSITION[0];
829 newkey[1] = '0' + sensor_attr->index;
830 newkey[2] = FAN_POSITION[2];
831 newkey[3] = FAN_POSITION[3];
832 newkey[4] = 0;
834 mutex_lock(&applesmc_lock);
836 ret = applesmc_read_key(newkey, buffer, 16);
837 buffer[16] = 0;
839 mutex_unlock(&applesmc_lock);
840 if (ret)
841 return ret;
842 else
843 return snprintf(sysfsbuf, PAGE_SIZE, "%s\n", buffer+4);
846 static ssize_t applesmc_calibrate_show(struct device *dev,
847 struct device_attribute *attr, char *sysfsbuf)
849 return snprintf(sysfsbuf, PAGE_SIZE, "(%d,%d)\n", rest_x, rest_y);
852 static ssize_t applesmc_calibrate_store(struct device *dev,
853 struct device_attribute *attr, const char *sysfsbuf, size_t count)
855 mutex_lock(&applesmc_lock);
856 applesmc_calibrate();
857 mutex_unlock(&applesmc_lock);
859 return count;
862 static void applesmc_backlight_set(struct work_struct *work)
864 mutex_lock(&applesmc_lock);
865 applesmc_write_key(BACKLIGHT_KEY, backlight_state, 2);
866 mutex_unlock(&applesmc_lock);
868 static DECLARE_WORK(backlight_work, &applesmc_backlight_set);
870 static void applesmc_brightness_set(struct led_classdev *led_cdev,
871 enum led_brightness value)
873 int ret;
875 backlight_state[0] = value;
876 ret = queue_work(applesmc_led_wq, &backlight_work);
878 if (debug && (!ret))
879 printk(KERN_DEBUG "applesmc: work was already on the queue.\n");
882 static ssize_t applesmc_key_count_show(struct device *dev,
883 struct device_attribute *attr, char *sysfsbuf)
885 int ret;
886 u8 buffer[4];
887 u32 count;
889 mutex_lock(&applesmc_lock);
891 ret = applesmc_read_key(KEY_COUNT_KEY, buffer, 4);
892 count = ((u32)buffer[0]<<24) + ((u32)buffer[1]<<16) +
893 ((u32)buffer[2]<<8) + buffer[3];
895 mutex_unlock(&applesmc_lock);
896 if (ret)
897 return ret;
898 else
899 return snprintf(sysfsbuf, PAGE_SIZE, "%d\n", count);
902 static ssize_t applesmc_key_at_index_read_show(struct device *dev,
903 struct device_attribute *attr, char *sysfsbuf)
905 char key[5];
906 char info[6];
907 int ret;
909 mutex_lock(&applesmc_lock);
911 ret = applesmc_get_key_at_index(key_at_index, key);
913 if (ret || !key[0]) {
914 mutex_unlock(&applesmc_lock);
916 return -EINVAL;
919 ret = applesmc_get_key_type(key, info);
921 if (ret) {
922 mutex_unlock(&applesmc_lock);
924 return ret;
928 * info[0] maximum value (APPLESMC_MAX_DATA_LENGTH) is much lower than
929 * PAGE_SIZE, so we don't need any checks before writing to sysfsbuf.
931 ret = applesmc_read_key(key, sysfsbuf, info[0]);
933 mutex_unlock(&applesmc_lock);
935 if (!ret) {
936 return info[0];
937 } else {
938 return ret;
942 static ssize_t applesmc_key_at_index_data_length_show(struct device *dev,
943 struct device_attribute *attr, char *sysfsbuf)
945 char key[5];
946 char info[6];
947 int ret;
949 mutex_lock(&applesmc_lock);
951 ret = applesmc_get_key_at_index(key_at_index, key);
953 if (ret || !key[0]) {
954 mutex_unlock(&applesmc_lock);
956 return -EINVAL;
959 ret = applesmc_get_key_type(key, info);
961 mutex_unlock(&applesmc_lock);
963 if (!ret)
964 return snprintf(sysfsbuf, PAGE_SIZE, "%d\n", info[0]);
965 else
966 return ret;
969 static ssize_t applesmc_key_at_index_type_show(struct device *dev,
970 struct device_attribute *attr, char *sysfsbuf)
972 char key[5];
973 char info[6];
974 int ret;
976 mutex_lock(&applesmc_lock);
978 ret = applesmc_get_key_at_index(key_at_index, key);
980 if (ret || !key[0]) {
981 mutex_unlock(&applesmc_lock);
983 return -EINVAL;
986 ret = applesmc_get_key_type(key, info);
988 mutex_unlock(&applesmc_lock);
990 if (!ret)
991 return snprintf(sysfsbuf, PAGE_SIZE, "%s\n", info+1);
992 else
993 return ret;
996 static ssize_t applesmc_key_at_index_name_show(struct device *dev,
997 struct device_attribute *attr, char *sysfsbuf)
999 char key[5];
1000 int ret;
1002 mutex_lock(&applesmc_lock);
1004 ret = applesmc_get_key_at_index(key_at_index, key);
1006 mutex_unlock(&applesmc_lock);
1008 if (!ret && key[0])
1009 return snprintf(sysfsbuf, PAGE_SIZE, "%s\n", key);
1010 else
1011 return -EINVAL;
1014 static ssize_t applesmc_key_at_index_show(struct device *dev,
1015 struct device_attribute *attr, char *sysfsbuf)
1017 return snprintf(sysfsbuf, PAGE_SIZE, "%d\n", key_at_index);
1020 static ssize_t applesmc_key_at_index_store(struct device *dev,
1021 struct device_attribute *attr, const char *sysfsbuf, size_t count)
1023 mutex_lock(&applesmc_lock);
1025 key_at_index = simple_strtoul(sysfsbuf, NULL, 10);
1027 mutex_unlock(&applesmc_lock);
1029 return count;
1032 static struct led_classdev applesmc_backlight = {
1033 .name = "smc::kbd_backlight",
1034 .default_trigger = "nand-disk",
1035 .brightness_set = applesmc_brightness_set,
1038 static DEVICE_ATTR(name, 0444, applesmc_name_show, NULL);
1040 static DEVICE_ATTR(position, 0444, applesmc_position_show, NULL);
1041 static DEVICE_ATTR(calibrate, 0644,
1042 applesmc_calibrate_show, applesmc_calibrate_store);
1044 static struct attribute *accelerometer_attributes[] = {
1045 &dev_attr_position.attr,
1046 &dev_attr_calibrate.attr,
1047 NULL
1050 static const struct attribute_group accelerometer_attributes_group =
1051 { .attrs = accelerometer_attributes };
1053 static DEVICE_ATTR(light, 0444, applesmc_light_show, NULL);
1055 static DEVICE_ATTR(key_count, 0444, applesmc_key_count_show, NULL);
1056 static DEVICE_ATTR(key_at_index, 0644,
1057 applesmc_key_at_index_show, applesmc_key_at_index_store);
1058 static DEVICE_ATTR(key_at_index_name, 0444,
1059 applesmc_key_at_index_name_show, NULL);
1060 static DEVICE_ATTR(key_at_index_type, 0444,
1061 applesmc_key_at_index_type_show, NULL);
1062 static DEVICE_ATTR(key_at_index_data_length, 0444,
1063 applesmc_key_at_index_data_length_show, NULL);
1064 static DEVICE_ATTR(key_at_index_data, 0444,
1065 applesmc_key_at_index_read_show, NULL);
1067 static struct attribute *key_enumeration_attributes[] = {
1068 &dev_attr_key_count.attr,
1069 &dev_attr_key_at_index.attr,
1070 &dev_attr_key_at_index_name.attr,
1071 &dev_attr_key_at_index_type.attr,
1072 &dev_attr_key_at_index_data_length.attr,
1073 &dev_attr_key_at_index_data.attr,
1074 NULL
1077 static const struct attribute_group key_enumeration_group =
1078 { .attrs = key_enumeration_attributes };
1081 * Macro defining SENSOR_DEVICE_ATTR for a fan sysfs entries.
1082 * - show actual speed
1083 * - show/store minimum speed
1084 * - show maximum speed
1085 * - show safe speed
1086 * - show/store target speed
1087 * - show/store manual mode
1089 #define sysfs_fan_speeds_offset(offset) \
1090 static SENSOR_DEVICE_ATTR_2(fan##offset##_input, S_IRUGO, \
1091 applesmc_show_fan_speed, NULL, 0, offset-1); \
1093 static SENSOR_DEVICE_ATTR_2(fan##offset##_min, S_IRUGO | S_IWUSR, \
1094 applesmc_show_fan_speed, applesmc_store_fan_speed, 1, offset-1); \
1096 static SENSOR_DEVICE_ATTR_2(fan##offset##_max, S_IRUGO, \
1097 applesmc_show_fan_speed, NULL, 2, offset-1); \
1099 static SENSOR_DEVICE_ATTR_2(fan##offset##_safe, S_IRUGO, \
1100 applesmc_show_fan_speed, NULL, 3, offset-1); \
1102 static SENSOR_DEVICE_ATTR_2(fan##offset##_output, S_IRUGO | S_IWUSR, \
1103 applesmc_show_fan_speed, applesmc_store_fan_speed, 4, offset-1); \
1105 static SENSOR_DEVICE_ATTR(fan##offset##_manual, S_IRUGO | S_IWUSR, \
1106 applesmc_show_fan_manual, applesmc_store_fan_manual, offset-1); \
1108 static SENSOR_DEVICE_ATTR(fan##offset##_label, S_IRUGO, \
1109 applesmc_show_fan_position, NULL, offset-1); \
1111 static struct attribute *fan##offset##_attributes[] = { \
1112 &sensor_dev_attr_fan##offset##_input.dev_attr.attr, \
1113 &sensor_dev_attr_fan##offset##_min.dev_attr.attr, \
1114 &sensor_dev_attr_fan##offset##_max.dev_attr.attr, \
1115 &sensor_dev_attr_fan##offset##_safe.dev_attr.attr, \
1116 &sensor_dev_attr_fan##offset##_output.dev_attr.attr, \
1117 &sensor_dev_attr_fan##offset##_manual.dev_attr.attr, \
1118 &sensor_dev_attr_fan##offset##_label.dev_attr.attr, \
1119 NULL \
1123 * Create the needed functions for each fan using the macro defined above
1124 * (4 fans are supported)
1126 sysfs_fan_speeds_offset(1);
1127 sysfs_fan_speeds_offset(2);
1128 sysfs_fan_speeds_offset(3);
1129 sysfs_fan_speeds_offset(4);
1131 static const struct attribute_group fan_attribute_groups[] = {
1132 { .attrs = fan1_attributes },
1133 { .attrs = fan2_attributes },
1134 { .attrs = fan3_attributes },
1135 { .attrs = fan4_attributes },
1139 * Temperature sensors sysfs entries.
1141 static SENSOR_DEVICE_ATTR(temp1_label, S_IRUGO,
1142 applesmc_show_sensor_label, NULL, 0);
1143 static SENSOR_DEVICE_ATTR(temp2_label, S_IRUGO,
1144 applesmc_show_sensor_label, NULL, 1);
1145 static SENSOR_DEVICE_ATTR(temp3_label, S_IRUGO,
1146 applesmc_show_sensor_label, NULL, 2);
1147 static SENSOR_DEVICE_ATTR(temp4_label, S_IRUGO,
1148 applesmc_show_sensor_label, NULL, 3);
1149 static SENSOR_DEVICE_ATTR(temp5_label, S_IRUGO,
1150 applesmc_show_sensor_label, NULL, 4);
1151 static SENSOR_DEVICE_ATTR(temp6_label, S_IRUGO,
1152 applesmc_show_sensor_label, NULL, 5);
1153 static SENSOR_DEVICE_ATTR(temp7_label, S_IRUGO,
1154 applesmc_show_sensor_label, NULL, 6);
1155 static SENSOR_DEVICE_ATTR(temp8_label, S_IRUGO,
1156 applesmc_show_sensor_label, NULL, 7);
1157 static SENSOR_DEVICE_ATTR(temp9_label, S_IRUGO,
1158 applesmc_show_sensor_label, NULL, 8);
1159 static SENSOR_DEVICE_ATTR(temp10_label, S_IRUGO,
1160 applesmc_show_sensor_label, NULL, 9);
1161 static SENSOR_DEVICE_ATTR(temp11_label, S_IRUGO,
1162 applesmc_show_sensor_label, NULL, 10);
1163 static SENSOR_DEVICE_ATTR(temp12_label, S_IRUGO,
1164 applesmc_show_sensor_label, NULL, 11);
1165 static SENSOR_DEVICE_ATTR(temp13_label, S_IRUGO,
1166 applesmc_show_sensor_label, NULL, 12);
1167 static SENSOR_DEVICE_ATTR(temp14_label, S_IRUGO,
1168 applesmc_show_sensor_label, NULL, 13);
1169 static SENSOR_DEVICE_ATTR(temp15_label, S_IRUGO,
1170 applesmc_show_sensor_label, NULL, 14);
1171 static SENSOR_DEVICE_ATTR(temp16_label, S_IRUGO,
1172 applesmc_show_sensor_label, NULL, 15);
1173 static SENSOR_DEVICE_ATTR(temp17_label, S_IRUGO,
1174 applesmc_show_sensor_label, NULL, 16);
1175 static SENSOR_DEVICE_ATTR(temp18_label, S_IRUGO,
1176 applesmc_show_sensor_label, NULL, 17);
1177 static SENSOR_DEVICE_ATTR(temp19_label, S_IRUGO,
1178 applesmc_show_sensor_label, NULL, 18);
1179 static SENSOR_DEVICE_ATTR(temp20_label, S_IRUGO,
1180 applesmc_show_sensor_label, NULL, 19);
1181 static SENSOR_DEVICE_ATTR(temp21_label, S_IRUGO,
1182 applesmc_show_sensor_label, NULL, 20);
1183 static SENSOR_DEVICE_ATTR(temp22_label, S_IRUGO,
1184 applesmc_show_sensor_label, NULL, 21);
1185 static SENSOR_DEVICE_ATTR(temp23_label, S_IRUGO,
1186 applesmc_show_sensor_label, NULL, 22);
1187 static SENSOR_DEVICE_ATTR(temp24_label, S_IRUGO,
1188 applesmc_show_sensor_label, NULL, 23);
1189 static SENSOR_DEVICE_ATTR(temp25_label, S_IRUGO,
1190 applesmc_show_sensor_label, NULL, 24);
1191 static SENSOR_DEVICE_ATTR(temp26_label, S_IRUGO,
1192 applesmc_show_sensor_label, NULL, 25);
1193 static SENSOR_DEVICE_ATTR(temp27_label, S_IRUGO,
1194 applesmc_show_sensor_label, NULL, 26);
1195 static SENSOR_DEVICE_ATTR(temp28_label, S_IRUGO,
1196 applesmc_show_sensor_label, NULL, 27);
1197 static SENSOR_DEVICE_ATTR(temp29_label, S_IRUGO,
1198 applesmc_show_sensor_label, NULL, 28);
1199 static SENSOR_DEVICE_ATTR(temp30_label, S_IRUGO,
1200 applesmc_show_sensor_label, NULL, 29);
1201 static SENSOR_DEVICE_ATTR(temp31_label, S_IRUGO,
1202 applesmc_show_sensor_label, NULL, 30);
1203 static SENSOR_DEVICE_ATTR(temp32_label, S_IRUGO,
1204 applesmc_show_sensor_label, NULL, 31);
1205 static SENSOR_DEVICE_ATTR(temp33_label, S_IRUGO,
1206 applesmc_show_sensor_label, NULL, 32);
1207 static SENSOR_DEVICE_ATTR(temp34_label, S_IRUGO,
1208 applesmc_show_sensor_label, NULL, 33);
1209 static SENSOR_DEVICE_ATTR(temp35_label, S_IRUGO,
1210 applesmc_show_sensor_label, NULL, 34);
1211 static SENSOR_DEVICE_ATTR(temp36_label, S_IRUGO,
1212 applesmc_show_sensor_label, NULL, 35);
1213 static SENSOR_DEVICE_ATTR(temp37_label, S_IRUGO,
1214 applesmc_show_sensor_label, NULL, 36);
1215 static SENSOR_DEVICE_ATTR(temp38_label, S_IRUGO,
1216 applesmc_show_sensor_label, NULL, 37);
1217 static SENSOR_DEVICE_ATTR(temp39_label, S_IRUGO,
1218 applesmc_show_sensor_label, NULL, 38);
1219 static SENSOR_DEVICE_ATTR(temp40_label, S_IRUGO,
1220 applesmc_show_sensor_label, NULL, 39);
1221 static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO,
1222 applesmc_show_temperature, NULL, 0);
1223 static SENSOR_DEVICE_ATTR(temp2_input, S_IRUGO,
1224 applesmc_show_temperature, NULL, 1);
1225 static SENSOR_DEVICE_ATTR(temp3_input, S_IRUGO,
1226 applesmc_show_temperature, NULL, 2);
1227 static SENSOR_DEVICE_ATTR(temp4_input, S_IRUGO,
1228 applesmc_show_temperature, NULL, 3);
1229 static SENSOR_DEVICE_ATTR(temp5_input, S_IRUGO,
1230 applesmc_show_temperature, NULL, 4);
1231 static SENSOR_DEVICE_ATTR(temp6_input, S_IRUGO,
1232 applesmc_show_temperature, NULL, 5);
1233 static SENSOR_DEVICE_ATTR(temp7_input, S_IRUGO,
1234 applesmc_show_temperature, NULL, 6);
1235 static SENSOR_DEVICE_ATTR(temp8_input, S_IRUGO,
1236 applesmc_show_temperature, NULL, 7);
1237 static SENSOR_DEVICE_ATTR(temp9_input, S_IRUGO,
1238 applesmc_show_temperature, NULL, 8);
1239 static SENSOR_DEVICE_ATTR(temp10_input, S_IRUGO,
1240 applesmc_show_temperature, NULL, 9);
1241 static SENSOR_DEVICE_ATTR(temp11_input, S_IRUGO,
1242 applesmc_show_temperature, NULL, 10);
1243 static SENSOR_DEVICE_ATTR(temp12_input, S_IRUGO,
1244 applesmc_show_temperature, NULL, 11);
1245 static SENSOR_DEVICE_ATTR(temp13_input, S_IRUGO,
1246 applesmc_show_temperature, NULL, 12);
1247 static SENSOR_DEVICE_ATTR(temp14_input, S_IRUGO,
1248 applesmc_show_temperature, NULL, 13);
1249 static SENSOR_DEVICE_ATTR(temp15_input, S_IRUGO,
1250 applesmc_show_temperature, NULL, 14);
1251 static SENSOR_DEVICE_ATTR(temp16_input, S_IRUGO,
1252 applesmc_show_temperature, NULL, 15);
1253 static SENSOR_DEVICE_ATTR(temp17_input, S_IRUGO,
1254 applesmc_show_temperature, NULL, 16);
1255 static SENSOR_DEVICE_ATTR(temp18_input, S_IRUGO,
1256 applesmc_show_temperature, NULL, 17);
1257 static SENSOR_DEVICE_ATTR(temp19_input, S_IRUGO,
1258 applesmc_show_temperature, NULL, 18);
1259 static SENSOR_DEVICE_ATTR(temp20_input, S_IRUGO,
1260 applesmc_show_temperature, NULL, 19);
1261 static SENSOR_DEVICE_ATTR(temp21_input, S_IRUGO,
1262 applesmc_show_temperature, NULL, 20);
1263 static SENSOR_DEVICE_ATTR(temp22_input, S_IRUGO,
1264 applesmc_show_temperature, NULL, 21);
1265 static SENSOR_DEVICE_ATTR(temp23_input, S_IRUGO,
1266 applesmc_show_temperature, NULL, 22);
1267 static SENSOR_DEVICE_ATTR(temp24_input, S_IRUGO,
1268 applesmc_show_temperature, NULL, 23);
1269 static SENSOR_DEVICE_ATTR(temp25_input, S_IRUGO,
1270 applesmc_show_temperature, NULL, 24);
1271 static SENSOR_DEVICE_ATTR(temp26_input, S_IRUGO,
1272 applesmc_show_temperature, NULL, 25);
1273 static SENSOR_DEVICE_ATTR(temp27_input, S_IRUGO,
1274 applesmc_show_temperature, NULL, 26);
1275 static SENSOR_DEVICE_ATTR(temp28_input, S_IRUGO,
1276 applesmc_show_temperature, NULL, 27);
1277 static SENSOR_DEVICE_ATTR(temp29_input, S_IRUGO,
1278 applesmc_show_temperature, NULL, 28);
1279 static SENSOR_DEVICE_ATTR(temp30_input, S_IRUGO,
1280 applesmc_show_temperature, NULL, 29);
1281 static SENSOR_DEVICE_ATTR(temp31_input, S_IRUGO,
1282 applesmc_show_temperature, NULL, 30);
1283 static SENSOR_DEVICE_ATTR(temp32_input, S_IRUGO,
1284 applesmc_show_temperature, NULL, 31);
1285 static SENSOR_DEVICE_ATTR(temp33_input, S_IRUGO,
1286 applesmc_show_temperature, NULL, 32);
1287 static SENSOR_DEVICE_ATTR(temp34_input, S_IRUGO,
1288 applesmc_show_temperature, NULL, 33);
1289 static SENSOR_DEVICE_ATTR(temp35_input, S_IRUGO,
1290 applesmc_show_temperature, NULL, 34);
1291 static SENSOR_DEVICE_ATTR(temp36_input, S_IRUGO,
1292 applesmc_show_temperature, NULL, 35);
1293 static SENSOR_DEVICE_ATTR(temp37_input, S_IRUGO,
1294 applesmc_show_temperature, NULL, 36);
1295 static SENSOR_DEVICE_ATTR(temp38_input, S_IRUGO,
1296 applesmc_show_temperature, NULL, 37);
1297 static SENSOR_DEVICE_ATTR(temp39_input, S_IRUGO,
1298 applesmc_show_temperature, NULL, 38);
1299 static SENSOR_DEVICE_ATTR(temp40_input, S_IRUGO,
1300 applesmc_show_temperature, NULL, 39);
1302 static struct attribute *label_attributes[] = {
1303 &sensor_dev_attr_temp1_label.dev_attr.attr,
1304 &sensor_dev_attr_temp2_label.dev_attr.attr,
1305 &sensor_dev_attr_temp3_label.dev_attr.attr,
1306 &sensor_dev_attr_temp4_label.dev_attr.attr,
1307 &sensor_dev_attr_temp5_label.dev_attr.attr,
1308 &sensor_dev_attr_temp6_label.dev_attr.attr,
1309 &sensor_dev_attr_temp7_label.dev_attr.attr,
1310 &sensor_dev_attr_temp8_label.dev_attr.attr,
1311 &sensor_dev_attr_temp9_label.dev_attr.attr,
1312 &sensor_dev_attr_temp10_label.dev_attr.attr,
1313 &sensor_dev_attr_temp11_label.dev_attr.attr,
1314 &sensor_dev_attr_temp12_label.dev_attr.attr,
1315 &sensor_dev_attr_temp13_label.dev_attr.attr,
1316 &sensor_dev_attr_temp14_label.dev_attr.attr,
1317 &sensor_dev_attr_temp15_label.dev_attr.attr,
1318 &sensor_dev_attr_temp16_label.dev_attr.attr,
1319 &sensor_dev_attr_temp17_label.dev_attr.attr,
1320 &sensor_dev_attr_temp18_label.dev_attr.attr,
1321 &sensor_dev_attr_temp19_label.dev_attr.attr,
1322 &sensor_dev_attr_temp20_label.dev_attr.attr,
1323 &sensor_dev_attr_temp21_label.dev_attr.attr,
1324 &sensor_dev_attr_temp22_label.dev_attr.attr,
1325 &sensor_dev_attr_temp23_label.dev_attr.attr,
1326 &sensor_dev_attr_temp24_label.dev_attr.attr,
1327 &sensor_dev_attr_temp25_label.dev_attr.attr,
1328 &sensor_dev_attr_temp26_label.dev_attr.attr,
1329 &sensor_dev_attr_temp27_label.dev_attr.attr,
1330 &sensor_dev_attr_temp28_label.dev_attr.attr,
1331 &sensor_dev_attr_temp29_label.dev_attr.attr,
1332 &sensor_dev_attr_temp30_label.dev_attr.attr,
1333 &sensor_dev_attr_temp31_label.dev_attr.attr,
1334 &sensor_dev_attr_temp32_label.dev_attr.attr,
1335 &sensor_dev_attr_temp33_label.dev_attr.attr,
1336 &sensor_dev_attr_temp34_label.dev_attr.attr,
1337 &sensor_dev_attr_temp35_label.dev_attr.attr,
1338 &sensor_dev_attr_temp36_label.dev_attr.attr,
1339 &sensor_dev_attr_temp37_label.dev_attr.attr,
1340 &sensor_dev_attr_temp38_label.dev_attr.attr,
1341 &sensor_dev_attr_temp39_label.dev_attr.attr,
1342 &sensor_dev_attr_temp40_label.dev_attr.attr,
1343 NULL
1346 static struct attribute *temperature_attributes[] = {
1347 &sensor_dev_attr_temp1_input.dev_attr.attr,
1348 &sensor_dev_attr_temp2_input.dev_attr.attr,
1349 &sensor_dev_attr_temp3_input.dev_attr.attr,
1350 &sensor_dev_attr_temp4_input.dev_attr.attr,
1351 &sensor_dev_attr_temp5_input.dev_attr.attr,
1352 &sensor_dev_attr_temp6_input.dev_attr.attr,
1353 &sensor_dev_attr_temp7_input.dev_attr.attr,
1354 &sensor_dev_attr_temp8_input.dev_attr.attr,
1355 &sensor_dev_attr_temp9_input.dev_attr.attr,
1356 &sensor_dev_attr_temp10_input.dev_attr.attr,
1357 &sensor_dev_attr_temp11_input.dev_attr.attr,
1358 &sensor_dev_attr_temp12_input.dev_attr.attr,
1359 &sensor_dev_attr_temp13_input.dev_attr.attr,
1360 &sensor_dev_attr_temp14_input.dev_attr.attr,
1361 &sensor_dev_attr_temp15_input.dev_attr.attr,
1362 &sensor_dev_attr_temp16_input.dev_attr.attr,
1363 &sensor_dev_attr_temp17_input.dev_attr.attr,
1364 &sensor_dev_attr_temp18_input.dev_attr.attr,
1365 &sensor_dev_attr_temp19_input.dev_attr.attr,
1366 &sensor_dev_attr_temp20_input.dev_attr.attr,
1367 &sensor_dev_attr_temp21_input.dev_attr.attr,
1368 &sensor_dev_attr_temp22_input.dev_attr.attr,
1369 &sensor_dev_attr_temp23_input.dev_attr.attr,
1370 &sensor_dev_attr_temp24_input.dev_attr.attr,
1371 &sensor_dev_attr_temp25_input.dev_attr.attr,
1372 &sensor_dev_attr_temp26_input.dev_attr.attr,
1373 &sensor_dev_attr_temp27_input.dev_attr.attr,
1374 &sensor_dev_attr_temp28_input.dev_attr.attr,
1375 &sensor_dev_attr_temp29_input.dev_attr.attr,
1376 &sensor_dev_attr_temp30_input.dev_attr.attr,
1377 &sensor_dev_attr_temp31_input.dev_attr.attr,
1378 &sensor_dev_attr_temp32_input.dev_attr.attr,
1379 &sensor_dev_attr_temp33_input.dev_attr.attr,
1380 &sensor_dev_attr_temp34_input.dev_attr.attr,
1381 &sensor_dev_attr_temp35_input.dev_attr.attr,
1382 &sensor_dev_attr_temp36_input.dev_attr.attr,
1383 &sensor_dev_attr_temp37_input.dev_attr.attr,
1384 &sensor_dev_attr_temp38_input.dev_attr.attr,
1385 &sensor_dev_attr_temp39_input.dev_attr.attr,
1386 &sensor_dev_attr_temp40_input.dev_attr.attr,
1387 NULL
1390 static const struct attribute_group temperature_attributes_group =
1391 { .attrs = temperature_attributes };
1393 static const struct attribute_group label_attributes_group = {
1394 .attrs = label_attributes
1397 /* Module stuff */
1400 * applesmc_dmi_match - found a match. return one, short-circuiting the hunt.
1402 static int applesmc_dmi_match(const struct dmi_system_id *id)
1404 int i = 0;
1405 struct dmi_match_data* dmi_data = id->driver_data;
1406 printk(KERN_INFO "applesmc: %s detected:\n", id->ident);
1407 applesmc_accelerometer = dmi_data->accelerometer;
1408 printk(KERN_INFO "applesmc: - Model %s accelerometer\n",
1409 applesmc_accelerometer ? "with" : "without");
1410 applesmc_light = dmi_data->light;
1411 printk(KERN_INFO "applesmc: - Model %s light sensors and backlight\n",
1412 applesmc_light ? "with" : "without");
1414 applesmc_temperature_set = dmi_data->temperature_set;
1415 while (temperature_sensors_sets[applesmc_temperature_set][i] != NULL)
1416 i++;
1417 printk(KERN_INFO "applesmc: - Model with %d temperature sensors\n", i);
1418 return 1;
1421 /* Create accelerometer ressources */
1422 static int applesmc_create_accelerometer(void)
1424 struct input_dev *idev;
1425 int ret;
1427 ret = sysfs_create_group(&pdev->dev.kobj,
1428 &accelerometer_attributes_group);
1429 if (ret)
1430 goto out;
1432 applesmc_idev = input_allocate_polled_device();
1433 if (!applesmc_idev) {
1434 ret = -ENOMEM;
1435 goto out_sysfs;
1438 applesmc_idev->poll = applesmc_idev_poll;
1439 applesmc_idev->poll_interval = APPLESMC_POLL_INTERVAL;
1441 /* initial calibrate for the input device */
1442 applesmc_calibrate();
1444 /* initialize the input device */
1445 idev = applesmc_idev->input;
1446 idev->name = "applesmc";
1447 idev->id.bustype = BUS_HOST;
1448 idev->dev.parent = &pdev->dev;
1449 idev->evbit[0] = BIT_MASK(EV_ABS);
1450 input_set_abs_params(idev, ABS_X,
1451 -256, 256, APPLESMC_INPUT_FUZZ, APPLESMC_INPUT_FLAT);
1452 input_set_abs_params(idev, ABS_Y,
1453 -256, 256, APPLESMC_INPUT_FUZZ, APPLESMC_INPUT_FLAT);
1455 ret = input_register_polled_device(applesmc_idev);
1456 if (ret)
1457 goto out_idev;
1459 return 0;
1461 out_idev:
1462 input_free_polled_device(applesmc_idev);
1464 out_sysfs:
1465 sysfs_remove_group(&pdev->dev.kobj, &accelerometer_attributes_group);
1467 out:
1468 printk(KERN_WARNING "applesmc: driver init failed (ret=%d)!\n", ret);
1469 return ret;
1472 /* Release all ressources used by the accelerometer */
1473 static void applesmc_release_accelerometer(void)
1475 input_unregister_polled_device(applesmc_idev);
1476 input_free_polled_device(applesmc_idev);
1477 sysfs_remove_group(&pdev->dev.kobj, &accelerometer_attributes_group);
1480 static __initdata struct dmi_match_data applesmc_dmi_data[] = {
1481 /* MacBook Pro: accelerometer, backlight and temperature set 0 */
1482 { .accelerometer = 1, .light = 1, .temperature_set = 0 },
1483 /* MacBook2: accelerometer and temperature set 1 */
1484 { .accelerometer = 1, .light = 0, .temperature_set = 1 },
1485 /* MacBook: accelerometer and temperature set 2 */
1486 { .accelerometer = 1, .light = 0, .temperature_set = 2 },
1487 /* MacMini: temperature set 3 */
1488 { .accelerometer = 0, .light = 0, .temperature_set = 3 },
1489 /* MacPro: temperature set 4 */
1490 { .accelerometer = 0, .light = 0, .temperature_set = 4 },
1491 /* iMac: temperature set 5 */
1492 { .accelerometer = 0, .light = 0, .temperature_set = 5 },
1493 /* MacBook3, MacBook4: accelerometer and temperature set 6 */
1494 { .accelerometer = 1, .light = 0, .temperature_set = 6 },
1495 /* MacBook Air: accelerometer, backlight and temperature set 7 */
1496 { .accelerometer = 1, .light = 1, .temperature_set = 7 },
1497 /* MacBook Pro 4: accelerometer, backlight and temperature set 8 */
1498 { .accelerometer = 1, .light = 1, .temperature_set = 8 },
1499 /* MacBook Pro 3: accelerometer, backlight and temperature set 9 */
1500 { .accelerometer = 1, .light = 1, .temperature_set = 9 },
1501 /* iMac 5: light sensor only, temperature set 10 */
1502 { .accelerometer = 0, .light = 0, .temperature_set = 10 },
1503 /* MacBook 5: accelerometer, backlight and temperature set 11 */
1504 { .accelerometer = 1, .light = 1, .temperature_set = 11 },
1505 /* MacBook Pro 5: accelerometer, backlight and temperature set 12 */
1506 { .accelerometer = 1, .light = 1, .temperature_set = 12 },
1507 /* iMac 8: light sensor only, temperature set 13 */
1508 { .accelerometer = 0, .light = 0, .temperature_set = 13 },
1509 /* iMac 6: light sensor only, temperature set 14 */
1510 { .accelerometer = 0, .light = 0, .temperature_set = 14 },
1511 /* MacBook Air 2,1: accelerometer, backlight and temperature set 15 */
1512 { .accelerometer = 1, .light = 1, .temperature_set = 15 },
1513 /* MacPro3,1: temperature set 16 */
1514 { .accelerometer = 0, .light = 0, .temperature_set = 16 },
1515 /* iMac 9,1: light sensor only, temperature set 17 */
1516 { .accelerometer = 0, .light = 0, .temperature_set = 17 },
1517 /* MacBook Pro 2,2: accelerometer, backlight and temperature set 18 */
1518 { .accelerometer = 1, .light = 1, .temperature_set = 18 },
1519 /* MacBook Pro 5,3: accelerometer, backlight and temperature set 19 */
1520 { .accelerometer = 1, .light = 1, .temperature_set = 19 },
1521 /* MacBook Pro 5,4: accelerometer, backlight and temperature set 20 */
1522 { .accelerometer = 1, .light = 1, .temperature_set = 20 },
1523 /* MacBook Pro 6,2: accelerometer, backlight and temperature set 21 */
1524 { .accelerometer = 1, .light = 1, .temperature_set = 21 },
1525 /* MacBook Pro 7,1: accelerometer, backlight and temperature set 22 */
1526 { .accelerometer = 1, .light = 1, .temperature_set = 22 },
1529 /* Note that DMI_MATCH(...,"MacBook") will match "MacBookPro1,1".
1530 * So we need to put "Apple MacBook Pro" before "Apple MacBook". */
1531 static __initdata struct dmi_system_id applesmc_whitelist[] = {
1532 { applesmc_dmi_match, "Apple MacBook Air 2", {
1533 DMI_MATCH(DMI_BOARD_VENDOR, "Apple"),
1534 DMI_MATCH(DMI_PRODUCT_NAME, "MacBookAir2") },
1535 &applesmc_dmi_data[15]},
1536 { applesmc_dmi_match, "Apple MacBook Air", {
1537 DMI_MATCH(DMI_BOARD_VENDOR, "Apple"),
1538 DMI_MATCH(DMI_PRODUCT_NAME, "MacBookAir") },
1539 &applesmc_dmi_data[7]},
1540 { applesmc_dmi_match, "Apple MacBook Pro 7", {
1541 DMI_MATCH(DMI_BOARD_VENDOR, "Apple"),
1542 DMI_MATCH(DMI_PRODUCT_NAME, "MacBookPro7") },
1543 &applesmc_dmi_data[22]},
1544 { applesmc_dmi_match, "Apple MacBook Pro 5,4", {
1545 DMI_MATCH(DMI_BOARD_VENDOR, "Apple"),
1546 DMI_MATCH(DMI_PRODUCT_NAME, "MacBookPro5,4") },
1547 &applesmc_dmi_data[20]},
1548 { applesmc_dmi_match, "Apple MacBook Pro 5,3", {
1549 DMI_MATCH(DMI_BOARD_VENDOR, "Apple"),
1550 DMI_MATCH(DMI_PRODUCT_NAME, "MacBookPro5,3") },
1551 &applesmc_dmi_data[19]},
1552 { applesmc_dmi_match, "Apple MacBook Pro 6", {
1553 DMI_MATCH(DMI_BOARD_VENDOR, "Apple"),
1554 DMI_MATCH(DMI_PRODUCT_NAME, "MacBookPro6") },
1555 &applesmc_dmi_data[21]},
1556 { applesmc_dmi_match, "Apple MacBook Pro 5", {
1557 DMI_MATCH(DMI_BOARD_VENDOR, "Apple"),
1558 DMI_MATCH(DMI_PRODUCT_NAME, "MacBookPro5") },
1559 &applesmc_dmi_data[12]},
1560 { applesmc_dmi_match, "Apple MacBook Pro 4", {
1561 DMI_MATCH(DMI_BOARD_VENDOR, "Apple"),
1562 DMI_MATCH(DMI_PRODUCT_NAME, "MacBookPro4") },
1563 &applesmc_dmi_data[8]},
1564 { applesmc_dmi_match, "Apple MacBook Pro 3", {
1565 DMI_MATCH(DMI_BOARD_VENDOR, "Apple"),
1566 DMI_MATCH(DMI_PRODUCT_NAME, "MacBookPro3") },
1567 &applesmc_dmi_data[9]},
1568 { applesmc_dmi_match, "Apple MacBook Pro 2,2", {
1569 DMI_MATCH(DMI_BOARD_VENDOR, "Apple Computer, Inc."),
1570 DMI_MATCH(DMI_PRODUCT_NAME, "MacBookPro2,2") },
1571 &applesmc_dmi_data[18]},
1572 { applesmc_dmi_match, "Apple MacBook Pro", {
1573 DMI_MATCH(DMI_BOARD_VENDOR,"Apple"),
1574 DMI_MATCH(DMI_PRODUCT_NAME,"MacBookPro") },
1575 &applesmc_dmi_data[0]},
1576 { applesmc_dmi_match, "Apple MacBook (v2)", {
1577 DMI_MATCH(DMI_BOARD_VENDOR,"Apple"),
1578 DMI_MATCH(DMI_PRODUCT_NAME,"MacBook2") },
1579 &applesmc_dmi_data[1]},
1580 { applesmc_dmi_match, "Apple MacBook (v3)", {
1581 DMI_MATCH(DMI_BOARD_VENDOR,"Apple"),
1582 DMI_MATCH(DMI_PRODUCT_NAME,"MacBook3") },
1583 &applesmc_dmi_data[6]},
1584 { applesmc_dmi_match, "Apple MacBook 4", {
1585 DMI_MATCH(DMI_BOARD_VENDOR, "Apple"),
1586 DMI_MATCH(DMI_PRODUCT_NAME, "MacBook4") },
1587 &applesmc_dmi_data[6]},
1588 { applesmc_dmi_match, "Apple MacBook 5", {
1589 DMI_MATCH(DMI_BOARD_VENDOR, "Apple"),
1590 DMI_MATCH(DMI_PRODUCT_NAME, "MacBook5") },
1591 &applesmc_dmi_data[11]},
1592 { applesmc_dmi_match, "Apple MacBook", {
1593 DMI_MATCH(DMI_BOARD_VENDOR,"Apple"),
1594 DMI_MATCH(DMI_PRODUCT_NAME,"MacBook") },
1595 &applesmc_dmi_data[2]},
1596 { applesmc_dmi_match, "Apple Macmini", {
1597 DMI_MATCH(DMI_BOARD_VENDOR,"Apple"),
1598 DMI_MATCH(DMI_PRODUCT_NAME,"Macmini") },
1599 &applesmc_dmi_data[3]},
1600 { applesmc_dmi_match, "Apple MacPro2", {
1601 DMI_MATCH(DMI_BOARD_VENDOR,"Apple"),
1602 DMI_MATCH(DMI_PRODUCT_NAME,"MacPro2") },
1603 &applesmc_dmi_data[4]},
1604 { applesmc_dmi_match, "Apple MacPro3", {
1605 DMI_MATCH(DMI_BOARD_VENDOR, "Apple"),
1606 DMI_MATCH(DMI_PRODUCT_NAME, "MacPro3") },
1607 &applesmc_dmi_data[16]},
1608 { applesmc_dmi_match, "Apple MacPro", {
1609 DMI_MATCH(DMI_BOARD_VENDOR, "Apple"),
1610 DMI_MATCH(DMI_PRODUCT_NAME, "MacPro") },
1611 &applesmc_dmi_data[4]},
1612 { applesmc_dmi_match, "Apple iMac 9,1", {
1613 DMI_MATCH(DMI_BOARD_VENDOR, "Apple Inc."),
1614 DMI_MATCH(DMI_PRODUCT_NAME, "iMac9,1") },
1615 &applesmc_dmi_data[17]},
1616 { applesmc_dmi_match, "Apple iMac 8", {
1617 DMI_MATCH(DMI_BOARD_VENDOR, "Apple"),
1618 DMI_MATCH(DMI_PRODUCT_NAME, "iMac8") },
1619 &applesmc_dmi_data[13]},
1620 { applesmc_dmi_match, "Apple iMac 6", {
1621 DMI_MATCH(DMI_BOARD_VENDOR, "Apple"),
1622 DMI_MATCH(DMI_PRODUCT_NAME, "iMac6") },
1623 &applesmc_dmi_data[14]},
1624 { applesmc_dmi_match, "Apple iMac 5", {
1625 DMI_MATCH(DMI_BOARD_VENDOR, "Apple"),
1626 DMI_MATCH(DMI_PRODUCT_NAME, "iMac5") },
1627 &applesmc_dmi_data[10]},
1628 { applesmc_dmi_match, "Apple iMac", {
1629 DMI_MATCH(DMI_BOARD_VENDOR,"Apple"),
1630 DMI_MATCH(DMI_PRODUCT_NAME,"iMac") },
1631 &applesmc_dmi_data[5]},
1632 { .ident = NULL }
1635 static int __init applesmc_init(void)
1637 int ret;
1638 int count;
1639 int i;
1641 if (!dmi_check_system(applesmc_whitelist)) {
1642 printk(KERN_WARNING "applesmc: supported laptop not found!\n");
1643 ret = -ENODEV;
1644 goto out;
1647 if (!request_region(APPLESMC_DATA_PORT, APPLESMC_NR_PORTS,
1648 "applesmc")) {
1649 ret = -ENXIO;
1650 goto out;
1653 ret = platform_driver_register(&applesmc_driver);
1654 if (ret)
1655 goto out_region;
1657 pdev = platform_device_register_simple("applesmc", APPLESMC_DATA_PORT,
1658 NULL, 0);
1659 if (IS_ERR(pdev)) {
1660 ret = PTR_ERR(pdev);
1661 goto out_driver;
1664 ret = sysfs_create_file(&pdev->dev.kobj, &dev_attr_name.attr);
1665 if (ret)
1666 goto out_device;
1668 /* Create key enumeration sysfs files */
1669 ret = sysfs_create_group(&pdev->dev.kobj, &key_enumeration_group);
1670 if (ret)
1671 goto out_name;
1673 /* create fan files */
1674 count = applesmc_get_fan_count();
1675 if (count < 0)
1676 printk(KERN_ERR "applesmc: Cannot get the number of fans.\n");
1677 else
1678 printk(KERN_INFO "applesmc: %d fans found.\n", count);
1680 if (count > 4) {
1681 count = 4;
1682 printk(KERN_WARNING "applesmc: More than 4 fans found,"
1683 " but at most 4 fans are supported"
1684 " by the driver.\n");
1687 while (fans_handled < count) {
1688 ret = sysfs_create_group(&pdev->dev.kobj,
1689 &fan_attribute_groups[fans_handled]);
1690 if (ret)
1691 goto out_fans;
1692 fans_handled++;
1695 for (i = 0;
1696 temperature_sensors_sets[applesmc_temperature_set][i] != NULL;
1697 i++) {
1698 if (temperature_attributes[i] == NULL ||
1699 label_attributes[i] == NULL) {
1700 printk(KERN_ERR "applesmc: More temperature sensors "
1701 "in temperature_sensors_sets (at least %i)"
1702 "than available sysfs files in "
1703 "temperature_attributes (%i), please report "
1704 "this bug.\n", i, i-1);
1705 goto out_temperature;
1707 ret = sysfs_create_file(&pdev->dev.kobj,
1708 temperature_attributes[i]);
1709 if (ret)
1710 goto out_temperature;
1711 ret = sysfs_create_file(&pdev->dev.kobj,
1712 label_attributes[i]);
1713 if (ret)
1714 goto out_temperature;
1717 if (applesmc_accelerometer) {
1718 ret = applesmc_create_accelerometer();
1719 if (ret)
1720 goto out_temperature;
1723 if (applesmc_light) {
1724 /* Add light sensor file */
1725 ret = sysfs_create_file(&pdev->dev.kobj, &dev_attr_light.attr);
1726 if (ret)
1727 goto out_accelerometer;
1729 /* Create the workqueue */
1730 applesmc_led_wq = create_singlethread_workqueue("applesmc-led");
1731 if (!applesmc_led_wq) {
1732 ret = -ENOMEM;
1733 goto out_light_sysfs;
1736 /* register as a led device */
1737 ret = led_classdev_register(&pdev->dev, &applesmc_backlight);
1738 if (ret < 0)
1739 goto out_light_wq;
1742 hwmon_dev = hwmon_device_register(&pdev->dev);
1743 if (IS_ERR(hwmon_dev)) {
1744 ret = PTR_ERR(hwmon_dev);
1745 goto out_light_ledclass;
1748 printk(KERN_INFO "applesmc: driver successfully loaded.\n");
1750 return 0;
1752 out_light_ledclass:
1753 if (applesmc_light)
1754 led_classdev_unregister(&applesmc_backlight);
1755 out_light_wq:
1756 if (applesmc_light)
1757 destroy_workqueue(applesmc_led_wq);
1758 out_light_sysfs:
1759 if (applesmc_light)
1760 sysfs_remove_file(&pdev->dev.kobj, &dev_attr_light.attr);
1761 out_accelerometer:
1762 if (applesmc_accelerometer)
1763 applesmc_release_accelerometer();
1764 out_temperature:
1765 sysfs_remove_group(&pdev->dev.kobj, &label_attributes_group);
1766 sysfs_remove_group(&pdev->dev.kobj, &temperature_attributes_group);
1767 out_fans:
1768 while (fans_handled)
1769 sysfs_remove_group(&pdev->dev.kobj,
1770 &fan_attribute_groups[--fans_handled]);
1771 sysfs_remove_group(&pdev->dev.kobj, &key_enumeration_group);
1772 out_name:
1773 sysfs_remove_file(&pdev->dev.kobj, &dev_attr_name.attr);
1774 out_device:
1775 platform_device_unregister(pdev);
1776 out_driver:
1777 platform_driver_unregister(&applesmc_driver);
1778 out_region:
1779 release_region(APPLESMC_DATA_PORT, APPLESMC_NR_PORTS);
1780 out:
1781 printk(KERN_WARNING "applesmc: driver init failed (ret=%d)!\n", ret);
1782 return ret;
1785 static void __exit applesmc_exit(void)
1787 hwmon_device_unregister(hwmon_dev);
1788 if (applesmc_light) {
1789 led_classdev_unregister(&applesmc_backlight);
1790 destroy_workqueue(applesmc_led_wq);
1791 sysfs_remove_file(&pdev->dev.kobj, &dev_attr_light.attr);
1793 if (applesmc_accelerometer)
1794 applesmc_release_accelerometer();
1795 sysfs_remove_group(&pdev->dev.kobj, &label_attributes_group);
1796 sysfs_remove_group(&pdev->dev.kobj, &temperature_attributes_group);
1797 while (fans_handled)
1798 sysfs_remove_group(&pdev->dev.kobj,
1799 &fan_attribute_groups[--fans_handled]);
1800 sysfs_remove_group(&pdev->dev.kobj, &key_enumeration_group);
1801 sysfs_remove_file(&pdev->dev.kobj, &dev_attr_name.attr);
1802 platform_device_unregister(pdev);
1803 platform_driver_unregister(&applesmc_driver);
1804 release_region(APPLESMC_DATA_PORT, APPLESMC_NR_PORTS);
1806 printk(KERN_INFO "applesmc: driver unloaded.\n");
1809 module_init(applesmc_init);
1810 module_exit(applesmc_exit);
1812 MODULE_AUTHOR("Nicolas Boichat");
1813 MODULE_DESCRIPTION("Apple SMC");
1814 MODULE_LICENSE("GPL v2");
1815 MODULE_DEVICE_TABLE(dmi, applesmc_whitelist);