2 * drivers/hwmon/applesmc.c - driver for Apple's SMC (accelerometer, temperature
3 * sensors, fan control, keyboard backlight control) used in Intel-based Apple
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
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.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>
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).
82 * First set for Macbook(Pro), second for Macmini.
84 static const char* temperature_sensors_sets
[][13] = {
85 { "TA0P", "TB0T", "TC0D", "TC0P", "TG0H", "TG0P", "TG0T", "Th0H",
86 "Th1H", "Tm0P", "Ts0P", "Ts1P", NULL
},
87 { "TC0D", "TC0P", NULL
}
90 /* List of keys used to read/write fan speeds */
91 static const char* fan_speed_keys
[] = {
99 #define INIT_TIMEOUT_MSECS 5000 /* wait up to 5s for device init ... */
100 #define INIT_WAIT_MSECS 50 /* ... in 50ms increments */
102 #define APPLESMC_POLL_PERIOD (HZ/20) /* poll for input every 1/20s */
103 #define APPLESMC_INPUT_FUZZ 4 /* input event threshold */
104 #define APPLESMC_INPUT_FLAT 4
110 /* Structure to be passed to DMI_MATCH function */
111 struct dmi_match_data
{
112 /* Indicates whether this computer has an accelerometer. */
114 /* Indicates whether this computer has light sensors and keyboard backlight. */
116 /* Indicates which temperature sensors set to use. */
120 static const int debug
;
121 static struct platform_device
*pdev
;
124 static struct timer_list applesmc_timer
;
125 static struct input_dev
*applesmc_idev
;
126 static struct class_device
*hwmon_class_dev
;
128 /* Indicates whether this computer has an accelerometer. */
129 static unsigned int applesmc_accelerometer
;
131 /* Indicates whether this computer has light sensors and keyboard backlight. */
132 static unsigned int applesmc_light
;
134 /* Indicates which temperature sensors set to use. */
135 static unsigned int applesmc_temperature_set
;
137 static struct mutex applesmc_lock
;
140 * Last index written to key_at_index sysfs file, and value to use for all other
141 * key_at_index_* sysfs files.
143 static unsigned int key_at_index
;
145 static struct workqueue_struct
*applesmc_led_wq
;
148 * __wait_status - Wait up to 2ms for the status port to get a certain value
149 * (masked with 0x0f), returning zero if the value is obtained. Callers must
150 * hold applesmc_lock.
152 static int __wait_status(u8 val
)
156 val
= val
& APPLESMC_STATUS_MASK
;
158 for (i
= 0; i
< 200; i
++) {
159 if ((inb(APPLESMC_CMD_PORT
) & APPLESMC_STATUS_MASK
) == val
) {
162 "Waited %d us for status %x\n",
169 printk(KERN_WARNING
"applesmc: wait status failed: %x != %x\n",
170 val
, inb(APPLESMC_CMD_PORT
));
176 * applesmc_read_key - reads len bytes from a given key, and put them in buffer.
177 * Returns zero on success or a negative error on failure. Callers must
178 * hold applesmc_lock.
180 static int applesmc_read_key(const char* key
, u8
* buffer
, u8 len
)
184 if (len
> APPLESMC_MAX_DATA_LENGTH
) {
185 printk(KERN_ERR
"applesmc_read_key: cannot read more than "
186 "%d bytes\n", APPLESMC_MAX_DATA_LENGTH
);
190 outb(APPLESMC_READ_CMD
, APPLESMC_CMD_PORT
);
191 if (__wait_status(0x0c))
194 for (i
= 0; i
< 4; i
++) {
195 outb(key
[i
], APPLESMC_DATA_PORT
);
196 if (__wait_status(0x04))
200 printk(KERN_DEBUG
"<%s", key
);
202 outb(len
, APPLESMC_DATA_PORT
);
204 printk(KERN_DEBUG
">%x", len
);
206 for (i
= 0; i
< len
; i
++) {
207 if (__wait_status(0x05))
209 buffer
[i
] = inb(APPLESMC_DATA_PORT
);
211 printk(KERN_DEBUG
"<%x", buffer
[i
]);
214 printk(KERN_DEBUG
"\n");
220 * applesmc_write_key - writes len bytes from buffer to a given key.
221 * Returns zero on success or a negative error on failure. Callers must
222 * hold applesmc_lock.
224 static int applesmc_write_key(const char* key
, u8
* buffer
, u8 len
)
228 if (len
> APPLESMC_MAX_DATA_LENGTH
) {
229 printk(KERN_ERR
"applesmc_write_key: cannot write more than "
230 "%d bytes\n", APPLESMC_MAX_DATA_LENGTH
);
234 outb(APPLESMC_WRITE_CMD
, APPLESMC_CMD_PORT
);
235 if (__wait_status(0x0c))
238 for (i
= 0; i
< 4; i
++) {
239 outb(key
[i
], APPLESMC_DATA_PORT
);
240 if (__wait_status(0x04))
244 outb(len
, APPLESMC_DATA_PORT
);
246 for (i
= 0; i
< len
; i
++) {
247 if (__wait_status(0x04))
249 outb(buffer
[i
], APPLESMC_DATA_PORT
);
256 * applesmc_get_key_at_index - get key at index, and put the result in key
257 * (char[6]). Returns zero on success or a negative error on failure. Callers
258 * must hold applesmc_lock.
260 static int applesmc_get_key_at_index(int index
, char* key
)
264 readkey
[0] = index
>> 24;
265 readkey
[1] = index
>> 16;
266 readkey
[2] = index
>> 8;
269 outb(APPLESMC_GET_KEY_BY_INDEX_CMD
, APPLESMC_CMD_PORT
);
270 if (__wait_status(0x0c))
273 for (i
= 0; i
< 4; i
++) {
274 outb(readkey
[i
], APPLESMC_DATA_PORT
);
275 if (__wait_status(0x04))
279 outb(4, APPLESMC_DATA_PORT
);
281 for (i
= 0; i
< 4; i
++) {
282 if (__wait_status(0x05))
284 key
[i
] = inb(APPLESMC_DATA_PORT
);
292 * applesmc_get_key_type - get key type, and put the result in type (char[6]).
293 * Returns zero on success or a negative error on failure. Callers must
294 * hold applesmc_lock.
296 static int applesmc_get_key_type(char* key
, char* type
)
300 outb(APPLESMC_GET_KEY_TYPE_CMD
, APPLESMC_CMD_PORT
);
301 if (__wait_status(0x0c))
304 for (i
= 0; i
< 4; i
++) {
305 outb(key
[i
], APPLESMC_DATA_PORT
);
306 if (__wait_status(0x04))
310 outb(5, APPLESMC_DATA_PORT
);
312 for (i
= 0; i
< 6; i
++) {
313 if (__wait_status(0x05))
315 type
[i
] = inb(APPLESMC_DATA_PORT
);
323 * applesmc_read_motion_sensor - Read motion sensor (X, Y or Z). Callers must
324 * hold applesmc_lock.
326 static int applesmc_read_motion_sensor(int index
, s16
* value
)
333 ret
= applesmc_read_key(MOTION_SENSOR_X_KEY
, buffer
, 2);
336 ret
= applesmc_read_key(MOTION_SENSOR_Y_KEY
, buffer
, 2);
339 ret
= applesmc_read_key(MOTION_SENSOR_Z_KEY
, buffer
, 2);
345 *value
= ((s16
)buffer
[0] << 8) | buffer
[1];
351 * applesmc_device_init - initialize the accelerometer. Returns zero on success
352 * and negative error code on failure. Can sleep.
354 static int applesmc_device_init(void)
356 int total
, ret
= -ENXIO
;
359 if (!applesmc_accelerometer
)
362 mutex_lock(&applesmc_lock
);
364 for (total
= INIT_TIMEOUT_MSECS
; total
> 0; total
-= INIT_WAIT_MSECS
) {
366 printk(KERN_DEBUG
"applesmc try %d\n", total
);
367 if (!applesmc_read_key(MOTION_SENSOR_KEY
, buffer
, 2) &&
368 (buffer
[0] != 0x00 || buffer
[1] != 0x00)) {
369 if (total
== INIT_TIMEOUT_MSECS
) {
370 printk(KERN_DEBUG
"applesmc: device has"
371 " already been initialized"
372 " (0x%02x, 0x%02x).\n",
373 buffer
[0], buffer
[1]);
375 printk(KERN_DEBUG
"applesmc: device"
376 " successfully initialized"
377 " (0x%02x, 0x%02x).\n",
378 buffer
[0], buffer
[1]);
385 applesmc_write_key(MOTION_SENSOR_KEY
, buffer
, 2);
386 msleep(INIT_WAIT_MSECS
);
389 printk(KERN_WARNING
"applesmc: failed to init the device\n");
392 mutex_unlock(&applesmc_lock
);
397 * applesmc_get_fan_count - get the number of fans. Callers must NOT hold
400 static int applesmc_get_fan_count(void)
405 mutex_lock(&applesmc_lock
);
407 ret
= applesmc_read_key(FANS_COUNT
, buffer
, 1);
409 mutex_unlock(&applesmc_lock
);
416 /* Device model stuff */
417 static int applesmc_probe(struct platform_device
*dev
)
421 ret
= applesmc_device_init();
425 printk(KERN_INFO
"applesmc: device successfully initialized.\n");
429 static int applesmc_resume(struct platform_device
*dev
)
431 return applesmc_device_init();
434 static struct platform_driver applesmc_driver
= {
435 .probe
= applesmc_probe
,
436 .resume
= applesmc_resume
,
439 .owner
= THIS_MODULE
,
444 * applesmc_calibrate - Set our "resting" values. Callers must
445 * hold applesmc_lock.
447 static void applesmc_calibrate(void)
449 applesmc_read_motion_sensor(SENSOR_X
, &rest_x
);
450 applesmc_read_motion_sensor(SENSOR_Y
, &rest_y
);
454 static int applesmc_idev_open(struct input_dev
*dev
)
456 add_timer(&applesmc_timer
);
461 static void applesmc_idev_close(struct input_dev
*dev
)
463 del_timer_sync(&applesmc_timer
);
466 static void applesmc_idev_poll(unsigned long unused
)
470 /* Cannot sleep. Try nonblockingly. If we fail, try again later. */
471 if (!mutex_trylock(&applesmc_lock
)) {
472 mod_timer(&applesmc_timer
, jiffies
+ APPLESMC_POLL_PERIOD
);
476 if (applesmc_read_motion_sensor(SENSOR_X
, &x
))
478 if (applesmc_read_motion_sensor(SENSOR_Y
, &y
))
482 input_report_abs(applesmc_idev
, ABS_X
, x
- rest_x
);
483 input_report_abs(applesmc_idev
, ABS_Y
, y
- rest_y
);
484 input_sync(applesmc_idev
);
487 mod_timer(&applesmc_timer
, jiffies
+ APPLESMC_POLL_PERIOD
);
489 mutex_unlock(&applesmc_lock
);
494 static ssize_t
applesmc_position_show(struct device
*dev
,
495 struct device_attribute
*attr
, char *buf
)
500 mutex_lock(&applesmc_lock
);
502 ret
= applesmc_read_motion_sensor(SENSOR_X
, &x
);
505 ret
= applesmc_read_motion_sensor(SENSOR_Y
, &y
);
508 ret
= applesmc_read_motion_sensor(SENSOR_Z
, &z
);
513 mutex_unlock(&applesmc_lock
);
517 return snprintf(buf
, PAGE_SIZE
, "(%d,%d,%d)\n", x
, y
, z
);
520 static ssize_t
applesmc_light_show(struct device
*dev
,
521 struct device_attribute
*attr
, char *sysfsbuf
)
524 u8 left
= 0, right
= 0;
527 mutex_lock(&applesmc_lock
);
529 ret
= applesmc_read_key(LIGHT_SENSOR_LEFT_KEY
, buffer
, 6);
533 ret
= applesmc_read_key(LIGHT_SENSOR_RIGHT_KEY
, buffer
, 6);
537 mutex_unlock(&applesmc_lock
);
541 return snprintf(sysfsbuf
, PAGE_SIZE
, "(%d,%d)\n", left
, right
);
544 /* Displays degree Celsius * 1000 */
545 static ssize_t
applesmc_show_temperature(struct device
*dev
,
546 struct device_attribute
*devattr
, char *sysfsbuf
)
551 struct sensor_device_attribute
*attr
= to_sensor_dev_attr(devattr
);
553 temperature_sensors_sets
[applesmc_temperature_set
][attr
->index
];
555 mutex_lock(&applesmc_lock
);
557 ret
= applesmc_read_key(key
, buffer
, 2);
558 temp
= buffer
[0]*1000;
559 temp
+= (buffer
[1] >> 6) * 250;
561 mutex_unlock(&applesmc_lock
);
566 return snprintf(sysfsbuf
, PAGE_SIZE
, "%u\n", temp
);
569 static ssize_t
applesmc_show_fan_speed(struct device
*dev
,
570 struct device_attribute
*attr
, char *sysfsbuf
)
573 unsigned int speed
= 0;
576 struct sensor_device_attribute_2
*sensor_attr
=
577 to_sensor_dev_attr_2(attr
);
579 newkey
[0] = fan_speed_keys
[sensor_attr
->nr
][0];
580 newkey
[1] = '0' + sensor_attr
->index
;
581 newkey
[2] = fan_speed_keys
[sensor_attr
->nr
][2];
582 newkey
[3] = fan_speed_keys
[sensor_attr
->nr
][3];
585 mutex_lock(&applesmc_lock
);
587 ret
= applesmc_read_key(newkey
, buffer
, 2);
588 speed
= ((buffer
[0] << 8 | buffer
[1]) >> 2);
590 mutex_unlock(&applesmc_lock
);
594 return snprintf(sysfsbuf
, PAGE_SIZE
, "%u\n", speed
);
597 static ssize_t
applesmc_store_fan_speed(struct device
*dev
,
598 struct device_attribute
*attr
,
599 const char *sysfsbuf
, size_t count
)
605 struct sensor_device_attribute_2
*sensor_attr
=
606 to_sensor_dev_attr_2(attr
);
608 speed
= simple_strtoul(sysfsbuf
, NULL
, 10);
610 if (speed
> 0x4000) /* Bigger than a 14-bit value */
613 newkey
[0] = fan_speed_keys
[sensor_attr
->nr
][0];
614 newkey
[1] = '0' + sensor_attr
->index
;
615 newkey
[2] = fan_speed_keys
[sensor_attr
->nr
][2];
616 newkey
[3] = fan_speed_keys
[sensor_attr
->nr
][3];
619 mutex_lock(&applesmc_lock
);
621 buffer
[0] = (speed
>> 6) & 0xff;
622 buffer
[1] = (speed
<< 2) & 0xff;
623 ret
= applesmc_write_key(newkey
, buffer
, 2);
625 mutex_unlock(&applesmc_lock
);
632 static ssize_t
applesmc_show_fan_manual(struct device
*dev
,
633 struct device_attribute
*devattr
, char *sysfsbuf
)
638 struct sensor_device_attribute
*attr
= to_sensor_dev_attr(devattr
);
640 mutex_lock(&applesmc_lock
);
642 ret
= applesmc_read_key(FANS_MANUAL
, buffer
, 2);
643 manual
= ((buffer
[0] << 8 | buffer
[1]) >> attr
->index
) & 0x01;
645 mutex_unlock(&applesmc_lock
);
649 return snprintf(sysfsbuf
, PAGE_SIZE
, "%d\n", manual
);
652 static ssize_t
applesmc_store_fan_manual(struct device
*dev
,
653 struct device_attribute
*devattr
,
654 const char *sysfsbuf
, size_t count
)
660 struct sensor_device_attribute
*attr
= to_sensor_dev_attr(devattr
);
662 input
= simple_strtoul(sysfsbuf
, NULL
, 10);
664 mutex_lock(&applesmc_lock
);
666 ret
= applesmc_read_key(FANS_MANUAL
, buffer
, 2);
667 val
= (buffer
[0] << 8 | buffer
[1]);
672 val
= val
| (0x01 << attr
->index
);
674 val
= val
& ~(0x01 << attr
->index
);
676 buffer
[0] = (val
>> 8) & 0xFF;
677 buffer
[1] = val
& 0xFF;
679 ret
= applesmc_write_key(FANS_MANUAL
, buffer
, 2);
682 mutex_unlock(&applesmc_lock
);
689 static ssize_t
applesmc_show_fan_position(struct device
*dev
,
690 struct device_attribute
*attr
, char *sysfsbuf
)
695 struct sensor_device_attribute_2
*sensor_attr
=
696 to_sensor_dev_attr_2(attr
);
698 newkey
[0] = FAN_POSITION
[0];
699 newkey
[1] = '0' + sensor_attr
->index
;
700 newkey
[2] = FAN_POSITION
[2];
701 newkey
[3] = FAN_POSITION
[3];
704 mutex_lock(&applesmc_lock
);
706 ret
= applesmc_read_key(newkey
, buffer
, 16);
709 mutex_unlock(&applesmc_lock
);
713 return snprintf(sysfsbuf
, PAGE_SIZE
, "%s\n", buffer
+4);
716 static ssize_t
applesmc_calibrate_show(struct device
*dev
,
717 struct device_attribute
*attr
, char *sysfsbuf
)
719 return snprintf(sysfsbuf
, PAGE_SIZE
, "(%d,%d)\n", rest_x
, rest_y
);
722 static ssize_t
applesmc_calibrate_store(struct device
*dev
,
723 struct device_attribute
*attr
, const char *sysfsbuf
, size_t count
)
725 mutex_lock(&applesmc_lock
);
726 applesmc_calibrate();
727 mutex_unlock(&applesmc_lock
);
732 /* Store the next backlight value to be written by the work */
733 static unsigned int backlight_value
;
735 static void applesmc_backlight_set(struct work_struct
*work
)
739 mutex_lock(&applesmc_lock
);
740 buffer
[0] = backlight_value
;
742 applesmc_write_key(BACKLIGHT_KEY
, buffer
, 2);
743 mutex_unlock(&applesmc_lock
);
745 static DECLARE_WORK(backlight_work
, &applesmc_backlight_set
);
747 static void applesmc_brightness_set(struct led_classdev
*led_cdev
,
748 enum led_brightness value
)
752 backlight_value
= value
;
753 ret
= queue_work(applesmc_led_wq
, &backlight_work
);
756 printk(KERN_DEBUG
"applesmc: work was already on the queue.\n");
759 static ssize_t
applesmc_key_count_show(struct device
*dev
,
760 struct device_attribute
*attr
, char *sysfsbuf
)
766 mutex_lock(&applesmc_lock
);
768 ret
= applesmc_read_key(KEY_COUNT_KEY
, buffer
, 4);
769 count
= ((u32
)buffer
[0]<<24) + ((u32
)buffer
[1]<<16) +
770 ((u32
)buffer
[2]<<8) + buffer
[3];
772 mutex_unlock(&applesmc_lock
);
776 return snprintf(sysfsbuf
, PAGE_SIZE
, "%d\n", count
);
779 static ssize_t
applesmc_key_at_index_read_show(struct device
*dev
,
780 struct device_attribute
*attr
, char *sysfsbuf
)
786 mutex_lock(&applesmc_lock
);
788 ret
= applesmc_get_key_at_index(key_at_index
, key
);
790 if (ret
|| !key
[0]) {
791 mutex_unlock(&applesmc_lock
);
796 ret
= applesmc_get_key_type(key
, info
);
799 mutex_unlock(&applesmc_lock
);
805 * info[0] maximum value (APPLESMC_MAX_DATA_LENGTH) is much lower than
806 * PAGE_SIZE, so we don't need any checks before writing to sysfsbuf.
808 ret
= applesmc_read_key(key
, sysfsbuf
, info
[0]);
810 mutex_unlock(&applesmc_lock
);
820 static ssize_t
applesmc_key_at_index_data_length_show(struct device
*dev
,
821 struct device_attribute
*attr
, char *sysfsbuf
)
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
);
837 ret
= applesmc_get_key_type(key
, info
);
839 mutex_unlock(&applesmc_lock
);
842 return snprintf(sysfsbuf
, PAGE_SIZE
, "%d\n", info
[0]);
847 static ssize_t
applesmc_key_at_index_type_show(struct device
*dev
,
848 struct device_attribute
*attr
, char *sysfsbuf
)
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
);
864 ret
= applesmc_get_key_type(key
, info
);
866 mutex_unlock(&applesmc_lock
);
869 return snprintf(sysfsbuf
, PAGE_SIZE
, "%s\n", info
+1);
874 static ssize_t
applesmc_key_at_index_name_show(struct device
*dev
,
875 struct device_attribute
*attr
, char *sysfsbuf
)
880 mutex_lock(&applesmc_lock
);
882 ret
= applesmc_get_key_at_index(key_at_index
, key
);
884 mutex_unlock(&applesmc_lock
);
887 return snprintf(sysfsbuf
, PAGE_SIZE
, "%s\n", key
);
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
);
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(position
, 0444, applesmc_position_show
, NULL
);
917 static DEVICE_ATTR(calibrate
, 0644,
918 applesmc_calibrate_show
, applesmc_calibrate_store
);
920 static struct attribute
*accelerometer_attributes
[] = {
921 &dev_attr_position
.attr
,
922 &dev_attr_calibrate
.attr
,
926 static const struct attribute_group accelerometer_attributes_group
=
927 { .attrs
= accelerometer_attributes
};
929 static DEVICE_ATTR(light
, 0444, applesmc_light_show
, NULL
);
931 static DEVICE_ATTR(key_count
, 0444, applesmc_key_count_show
, NULL
);
932 static DEVICE_ATTR(key_at_index
, 0644,
933 applesmc_key_at_index_show
, applesmc_key_at_index_store
);
934 static DEVICE_ATTR(key_at_index_name
, 0444,
935 applesmc_key_at_index_name_show
, NULL
);
936 static DEVICE_ATTR(key_at_index_type
, 0444,
937 applesmc_key_at_index_type_show
, NULL
);
938 static DEVICE_ATTR(key_at_index_data_length
, 0444,
939 applesmc_key_at_index_data_length_show
, NULL
);
940 static DEVICE_ATTR(key_at_index_data
, 0444,
941 applesmc_key_at_index_read_show
, NULL
);
943 static struct attribute
*key_enumeration_attributes
[] = {
944 &dev_attr_key_count
.attr
,
945 &dev_attr_key_at_index
.attr
,
946 &dev_attr_key_at_index_name
.attr
,
947 &dev_attr_key_at_index_type
.attr
,
948 &dev_attr_key_at_index_data_length
.attr
,
949 &dev_attr_key_at_index_data
.attr
,
953 static const struct attribute_group key_enumeration_group
=
954 { .attrs
= key_enumeration_attributes
};
957 * Macro defining SENSOR_DEVICE_ATTR for a fan sysfs entries.
958 * - show actual speed
959 * - show/store minimum speed
960 * - show maximum speed
962 * - show/store target speed
963 * - show/store manual mode
965 #define sysfs_fan_speeds_offset(offset) \
966 static SENSOR_DEVICE_ATTR_2(fan##offset##_input, S_IRUGO, \
967 applesmc_show_fan_speed, NULL, 0, offset-1); \
969 static SENSOR_DEVICE_ATTR_2(fan##offset##_min, S_IRUGO | S_IWUSR, \
970 applesmc_show_fan_speed, applesmc_store_fan_speed, 1, offset-1); \
972 static SENSOR_DEVICE_ATTR_2(fan##offset##_max, S_IRUGO, \
973 applesmc_show_fan_speed, NULL, 2, offset-1); \
975 static SENSOR_DEVICE_ATTR_2(fan##offset##_safe, S_IRUGO, \
976 applesmc_show_fan_speed, NULL, 3, offset-1); \
978 static SENSOR_DEVICE_ATTR_2(fan##offset##_output, S_IRUGO | S_IWUSR, \
979 applesmc_show_fan_speed, applesmc_store_fan_speed, 4, offset-1); \
981 static SENSOR_DEVICE_ATTR(fan##offset##_manual, S_IRUGO | S_IWUSR, \
982 applesmc_show_fan_manual, applesmc_store_fan_manual, offset-1); \
984 static SENSOR_DEVICE_ATTR(fan##offset##_label, S_IRUGO, \
985 applesmc_show_fan_position, NULL, offset-1); \
987 static struct attribute *fan##offset##_attributes[] = { \
988 &sensor_dev_attr_fan##offset##_input.dev_attr.attr, \
989 &sensor_dev_attr_fan##offset##_min.dev_attr.attr, \
990 &sensor_dev_attr_fan##offset##_max.dev_attr.attr, \
991 &sensor_dev_attr_fan##offset##_safe.dev_attr.attr, \
992 &sensor_dev_attr_fan##offset##_output.dev_attr.attr, \
993 &sensor_dev_attr_fan##offset##_manual.dev_attr.attr, \
994 &sensor_dev_attr_fan##offset##_label.dev_attr.attr, \
999 * Create the needed functions for each fan using the macro defined above
1000 * (2 fans are supported)
1002 sysfs_fan_speeds_offset(1);
1003 sysfs_fan_speeds_offset(2);
1005 static const struct attribute_group fan_attribute_groups
[] = {
1006 { .attrs
= fan1_attributes
},
1007 { .attrs
= fan2_attributes
}
1011 * Temperature sensors sysfs entries.
1013 static SENSOR_DEVICE_ATTR(temp1_input
, S_IRUGO
,
1014 applesmc_show_temperature
, NULL
, 0);
1015 static SENSOR_DEVICE_ATTR(temp2_input
, S_IRUGO
,
1016 applesmc_show_temperature
, NULL
, 1);
1017 static SENSOR_DEVICE_ATTR(temp3_input
, S_IRUGO
,
1018 applesmc_show_temperature
, NULL
, 2);
1019 static SENSOR_DEVICE_ATTR(temp4_input
, S_IRUGO
,
1020 applesmc_show_temperature
, NULL
, 3);
1021 static SENSOR_DEVICE_ATTR(temp5_input
, S_IRUGO
,
1022 applesmc_show_temperature
, NULL
, 4);
1023 static SENSOR_DEVICE_ATTR(temp6_input
, S_IRUGO
,
1024 applesmc_show_temperature
, NULL
, 5);
1025 static SENSOR_DEVICE_ATTR(temp7_input
, S_IRUGO
,
1026 applesmc_show_temperature
, NULL
, 6);
1027 static SENSOR_DEVICE_ATTR(temp8_input
, S_IRUGO
,
1028 applesmc_show_temperature
, NULL
, 7);
1029 static SENSOR_DEVICE_ATTR(temp9_input
, S_IRUGO
,
1030 applesmc_show_temperature
, NULL
, 8);
1031 static SENSOR_DEVICE_ATTR(temp10_input
, S_IRUGO
,
1032 applesmc_show_temperature
, NULL
, 9);
1033 static SENSOR_DEVICE_ATTR(temp11_input
, S_IRUGO
,
1034 applesmc_show_temperature
, NULL
, 10);
1035 static SENSOR_DEVICE_ATTR(temp12_input
, S_IRUGO
,
1036 applesmc_show_temperature
, NULL
, 11);
1038 static struct attribute
*temperature_attributes
[] = {
1039 &sensor_dev_attr_temp1_input
.dev_attr
.attr
,
1040 &sensor_dev_attr_temp2_input
.dev_attr
.attr
,
1041 &sensor_dev_attr_temp3_input
.dev_attr
.attr
,
1042 &sensor_dev_attr_temp4_input
.dev_attr
.attr
,
1043 &sensor_dev_attr_temp5_input
.dev_attr
.attr
,
1044 &sensor_dev_attr_temp6_input
.dev_attr
.attr
,
1045 &sensor_dev_attr_temp7_input
.dev_attr
.attr
,
1046 &sensor_dev_attr_temp8_input
.dev_attr
.attr
,
1047 &sensor_dev_attr_temp9_input
.dev_attr
.attr
,
1048 &sensor_dev_attr_temp10_input
.dev_attr
.attr
,
1049 &sensor_dev_attr_temp11_input
.dev_attr
.attr
,
1050 &sensor_dev_attr_temp12_input
.dev_attr
.attr
,
1054 static const struct attribute_group temperature_attributes_group
=
1055 { .attrs
= temperature_attributes
};
1060 * applesmc_dmi_match - found a match. return one, short-circuiting the hunt.
1062 static int applesmc_dmi_match(struct dmi_system_id
*id
)
1065 struct dmi_match_data
* dmi_data
= id
->driver_data
;
1066 printk(KERN_INFO
"applesmc: %s detected:\n", id
->ident
);
1067 applesmc_accelerometer
= dmi_data
->accelerometer
;
1068 printk(KERN_INFO
"applesmc: - Model %s accelerometer\n",
1069 applesmc_accelerometer
? "with" : "without");
1070 applesmc_light
= dmi_data
->light
;
1071 printk(KERN_INFO
"applesmc: - Model %s light sensors and backlight\n",
1072 applesmc_light
? "with" : "without");
1074 applesmc_temperature_set
= dmi_data
->temperature_set
;
1075 while (temperature_sensors_sets
[applesmc_temperature_set
][i
] != NULL
)
1077 printk(KERN_INFO
"applesmc: - Model with %d temperature sensors\n", i
);
1081 /* Create accelerometer ressources */
1082 static int applesmc_create_accelerometer(void)
1086 ret
= sysfs_create_group(&pdev
->dev
.kobj
,
1087 &accelerometer_attributes_group
);
1091 applesmc_idev
= input_allocate_device();
1092 if (!applesmc_idev
) {
1097 /* initial calibrate for the input device */
1098 applesmc_calibrate();
1100 /* initialize the input class */
1101 applesmc_idev
->name
= "applesmc";
1102 applesmc_idev
->id
.bustype
= BUS_HOST
;
1103 applesmc_idev
->cdev
.dev
= &pdev
->dev
;
1104 applesmc_idev
->evbit
[0] = BIT(EV_ABS
);
1105 applesmc_idev
->open
= applesmc_idev_open
;
1106 applesmc_idev
->close
= applesmc_idev_close
;
1107 input_set_abs_params(applesmc_idev
, ABS_X
,
1108 -256, 256, APPLESMC_INPUT_FUZZ
, APPLESMC_INPUT_FLAT
);
1109 input_set_abs_params(applesmc_idev
, ABS_Y
,
1110 -256, 256, APPLESMC_INPUT_FUZZ
, APPLESMC_INPUT_FLAT
);
1112 ret
= input_register_device(applesmc_idev
);
1116 /* start up our timer for the input device */
1117 init_timer(&applesmc_timer
);
1118 applesmc_timer
.function
= applesmc_idev_poll
;
1119 applesmc_timer
.expires
= jiffies
+ APPLESMC_POLL_PERIOD
;
1124 input_free_device(applesmc_idev
);
1127 sysfs_remove_group(&pdev
->dev
.kobj
, &accelerometer_attributes_group
);
1130 printk(KERN_WARNING
"applesmc: driver init failed (ret=%d)!\n", ret
);
1134 /* Release all ressources used by the accelerometer */
1135 static void applesmc_release_accelerometer(void)
1137 del_timer_sync(&applesmc_timer
);
1138 input_unregister_device(applesmc_idev
);
1139 sysfs_remove_group(&pdev
->dev
.kobj
, &accelerometer_attributes_group
);
1142 static __initdata
struct dmi_match_data applesmc_dmi_data
[] = {
1143 /* MacBook Pro: accelerometer, backlight and temperature set 0 */
1144 { .accelerometer
= 1, .light
= 1, .temperature_set
= 0 },
1145 /* MacBook: accelerometer and temperature set 0 */
1146 { .accelerometer
= 1, .light
= 0, .temperature_set
= 0 },
1147 /* MacBook: temperature set 1 */
1148 { .accelerometer
= 0, .light
= 0, .temperature_set
= 1 }
1151 /* Note that DMI_MATCH(...,"MacBook") will match "MacBookPro1,1".
1152 * So we need to put "Apple MacBook Pro" before "Apple MacBook". */
1153 static __initdata
struct dmi_system_id applesmc_whitelist
[] = {
1154 { applesmc_dmi_match
, "Apple MacBook Pro", {
1155 DMI_MATCH(DMI_BOARD_VENDOR
,"Apple"),
1156 DMI_MATCH(DMI_PRODUCT_NAME
,"MacBookPro") },
1157 (void*)&applesmc_dmi_data
[0]},
1158 { applesmc_dmi_match
, "Apple MacBook", {
1159 DMI_MATCH(DMI_BOARD_VENDOR
,"Apple"),
1160 DMI_MATCH(DMI_PRODUCT_NAME
,"MacBook") },
1161 (void*)&applesmc_dmi_data
[1]},
1162 { applesmc_dmi_match
, "Apple Macmini", {
1163 DMI_MATCH(DMI_BOARD_VENDOR
,"Apple"),
1164 DMI_MATCH(DMI_PRODUCT_NAME
,"Macmini") },
1165 (void*)&applesmc_dmi_data
[2]},
1169 static int __init
applesmc_init(void)
1175 mutex_init(&applesmc_lock
);
1177 if (!dmi_check_system(applesmc_whitelist
)) {
1178 printk(KERN_WARNING
"applesmc: supported laptop not found!\n");
1183 if (!request_region(APPLESMC_DATA_PORT
, APPLESMC_NR_PORTS
,
1189 ret
= platform_driver_register(&applesmc_driver
);
1193 pdev
= platform_device_register_simple("applesmc", APPLESMC_DATA_PORT
,
1196 ret
= PTR_ERR(pdev
);
1200 /* Create key enumeration sysfs files */
1201 ret
= sysfs_create_group(&pdev
->dev
.kobj
, &key_enumeration_group
);
1205 /* create fan files */
1206 count
= applesmc_get_fan_count();
1208 printk(KERN_ERR
"applesmc: Cannot get the number of fans.\n");
1210 printk(KERN_INFO
"applesmc: %d fans found.\n", count
);
1214 printk(KERN_WARNING
"applesmc: More than 2 fans found,"
1215 " but at most 2 fans are supported"
1216 " by the driver.\n");
1218 ret
= sysfs_create_group(&pdev
->dev
.kobj
,
1219 &fan_attribute_groups
[1]);
1221 goto out_key_enumeration
;
1223 ret
= sysfs_create_group(&pdev
->dev
.kobj
,
1224 &fan_attribute_groups
[0]);
1233 temperature_sensors_sets
[applesmc_temperature_set
][i
] != NULL
;
1235 if (temperature_attributes
[i
] == NULL
) {
1236 printk(KERN_ERR
"applesmc: More temperature sensors "
1237 "in temperature_sensors_sets (at least %i)"
1238 "than available sysfs files in "
1239 "temperature_attributes (%i), please report "
1240 "this bug.\n", i
, i
-1);
1241 goto out_temperature
;
1243 ret
= sysfs_create_file(&pdev
->dev
.kobj
,
1244 temperature_attributes
[i
]);
1246 goto out_temperature
;
1249 if (applesmc_accelerometer
) {
1250 ret
= applesmc_create_accelerometer();
1252 goto out_temperature
;
1255 if (applesmc_light
) {
1256 /* Add light sensor file */
1257 ret
= sysfs_create_file(&pdev
->dev
.kobj
, &dev_attr_light
.attr
);
1259 goto out_accelerometer
;
1261 /* Create the workqueue */
1262 applesmc_led_wq
= create_singlethread_workqueue("applesmc-led");
1263 if (!applesmc_led_wq
) {
1265 goto out_light_sysfs
;
1268 /* register as a led device */
1269 ret
= led_classdev_register(&pdev
->dev
, &applesmc_backlight
);
1274 hwmon_class_dev
= hwmon_device_register(&pdev
->dev
);
1275 if (IS_ERR(hwmon_class_dev
)) {
1276 ret
= PTR_ERR(hwmon_class_dev
);
1277 goto out_light_ledclass
;
1280 printk(KERN_INFO
"applesmc: driver successfully loaded.\n");
1286 led_classdev_unregister(&applesmc_backlight
);
1289 destroy_workqueue(applesmc_led_wq
);
1292 sysfs_remove_file(&pdev
->dev
.kobj
, &dev_attr_light
.attr
);
1294 if (applesmc_accelerometer
)
1295 applesmc_release_accelerometer();
1297 sysfs_remove_group(&pdev
->dev
.kobj
, &temperature_attributes_group
);
1298 sysfs_remove_group(&pdev
->dev
.kobj
, &fan_attribute_groups
[0]);
1300 sysfs_remove_group(&pdev
->dev
.kobj
, &fan_attribute_groups
[1]);
1301 out_key_enumeration
:
1302 sysfs_remove_group(&pdev
->dev
.kobj
, &key_enumeration_group
);
1304 platform_device_unregister(pdev
);
1306 platform_driver_unregister(&applesmc_driver
);
1308 release_region(APPLESMC_DATA_PORT
, APPLESMC_NR_PORTS
);
1310 printk(KERN_WARNING
"applesmc: driver init failed (ret=%d)!\n", ret
);
1314 static void __exit
applesmc_exit(void)
1316 hwmon_device_unregister(hwmon_class_dev
);
1317 if (applesmc_light
) {
1318 led_classdev_unregister(&applesmc_backlight
);
1319 destroy_workqueue(applesmc_led_wq
);
1320 sysfs_remove_file(&pdev
->dev
.kobj
, &dev_attr_light
.attr
);
1322 if (applesmc_accelerometer
)
1323 applesmc_release_accelerometer();
1324 sysfs_remove_group(&pdev
->dev
.kobj
, &temperature_attributes_group
);
1325 sysfs_remove_group(&pdev
->dev
.kobj
, &fan_attribute_groups
[0]);
1326 sysfs_remove_group(&pdev
->dev
.kobj
, &fan_attribute_groups
[1]);
1327 sysfs_remove_group(&pdev
->dev
.kobj
, &key_enumeration_group
);
1328 platform_device_unregister(pdev
);
1329 platform_driver_unregister(&applesmc_driver
);
1330 release_region(APPLESMC_DATA_PORT
, APPLESMC_NR_PORTS
);
1332 printk(KERN_INFO
"applesmc: driver unloaded.\n");
1335 module_init(applesmc_init
);
1336 module_exit(applesmc_exit
);
1338 MODULE_AUTHOR("Nicolas Boichat");
1339 MODULE_DESCRIPTION("Apple SMC");
1340 MODULE_LICENSE("GPL v2");