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).
83 static const char* temperature_sensors_sets
[][13] = {
84 /* Set 0: Macbook Pro */
85 { "TA0P", "TB0T", "TC0D", "TC0P", "TG0H", "TG0P", "TG0T", "Th0H",
86 "Th1H", "Tm0P", "Ts0P", "Ts1P", NULL
},
87 /* Set 1: Macbook set */
88 { "TB0T", "TC0D", "TC0P", "TM0P", "TN0P", "TN1P", "Th0H", "Th0S",
89 "Th1H", "Ts0P", NULL
},
90 /* Set 2: Macmini set */
91 { "TC0D", "TC0P", NULL
}
94 /* List of keys used to read/write fan speeds */
95 static const char* fan_speed_keys
[] = {
103 #define INIT_TIMEOUT_MSECS 5000 /* wait up to 5s for device init ... */
104 #define INIT_WAIT_MSECS 50 /* ... in 50ms increments */
106 #define APPLESMC_POLL_PERIOD (HZ/20) /* poll for input every 1/20s */
107 #define APPLESMC_INPUT_FUZZ 4 /* input event threshold */
108 #define APPLESMC_INPUT_FLAT 4
114 /* Structure to be passed to DMI_MATCH function */
115 struct dmi_match_data
{
116 /* Indicates whether this computer has an accelerometer. */
118 /* Indicates whether this computer has light sensors and keyboard backlight. */
120 /* Indicates which temperature sensors set to use. */
124 static const int debug
;
125 static struct platform_device
*pdev
;
128 static struct timer_list applesmc_timer
;
129 static struct input_dev
*applesmc_idev
;
130 static struct class_device
*hwmon_class_dev
;
132 /* Indicates whether this computer has an accelerometer. */
133 static unsigned int applesmc_accelerometer
;
135 /* Indicates whether this computer has light sensors and keyboard backlight. */
136 static unsigned int applesmc_light
;
138 /* Indicates which temperature sensors set to use. */
139 static unsigned int applesmc_temperature_set
;
141 static struct mutex applesmc_lock
;
144 * Last index written to key_at_index sysfs file, and value to use for all other
145 * key_at_index_* sysfs files.
147 static unsigned int key_at_index
;
149 static struct workqueue_struct
*applesmc_led_wq
;
152 * __wait_status - Wait up to 2ms for the status port to get a certain value
153 * (masked with 0x0f), returning zero if the value is obtained. Callers must
154 * hold applesmc_lock.
156 static int __wait_status(u8 val
)
160 val
= val
& APPLESMC_STATUS_MASK
;
162 for (i
= 0; i
< 200; i
++) {
163 if ((inb(APPLESMC_CMD_PORT
) & APPLESMC_STATUS_MASK
) == val
) {
166 "Waited %d us for status %x\n",
173 printk(KERN_WARNING
"applesmc: wait status failed: %x != %x\n",
174 val
, inb(APPLESMC_CMD_PORT
));
180 * applesmc_read_key - reads len bytes from a given key, and put them in buffer.
181 * Returns zero on success or a negative error on failure. Callers must
182 * hold applesmc_lock.
184 static int applesmc_read_key(const char* key
, u8
* buffer
, u8 len
)
188 if (len
> APPLESMC_MAX_DATA_LENGTH
) {
189 printk(KERN_ERR
"applesmc_read_key: cannot read more than "
190 "%d bytes\n", APPLESMC_MAX_DATA_LENGTH
);
194 outb(APPLESMC_READ_CMD
, APPLESMC_CMD_PORT
);
195 if (__wait_status(0x0c))
198 for (i
= 0; i
< 4; i
++) {
199 outb(key
[i
], APPLESMC_DATA_PORT
);
200 if (__wait_status(0x04))
204 printk(KERN_DEBUG
"<%s", key
);
206 outb(len
, APPLESMC_DATA_PORT
);
208 printk(KERN_DEBUG
">%x", len
);
210 for (i
= 0; i
< len
; i
++) {
211 if (__wait_status(0x05))
213 buffer
[i
] = inb(APPLESMC_DATA_PORT
);
215 printk(KERN_DEBUG
"<%x", buffer
[i
]);
218 printk(KERN_DEBUG
"\n");
224 * applesmc_write_key - writes len bytes from buffer to a given key.
225 * Returns zero on success or a negative error on failure. Callers must
226 * hold applesmc_lock.
228 static int applesmc_write_key(const char* key
, u8
* buffer
, u8 len
)
232 if (len
> APPLESMC_MAX_DATA_LENGTH
) {
233 printk(KERN_ERR
"applesmc_write_key: cannot write more than "
234 "%d bytes\n", APPLESMC_MAX_DATA_LENGTH
);
238 outb(APPLESMC_WRITE_CMD
, APPLESMC_CMD_PORT
);
239 if (__wait_status(0x0c))
242 for (i
= 0; i
< 4; i
++) {
243 outb(key
[i
], APPLESMC_DATA_PORT
);
244 if (__wait_status(0x04))
248 outb(len
, APPLESMC_DATA_PORT
);
250 for (i
= 0; i
< len
; i
++) {
251 if (__wait_status(0x04))
253 outb(buffer
[i
], APPLESMC_DATA_PORT
);
260 * applesmc_get_key_at_index - get key at index, and put the result in key
261 * (char[6]). Returns zero on success or a negative error on failure. Callers
262 * must hold applesmc_lock.
264 static int applesmc_get_key_at_index(int index
, char* key
)
268 readkey
[0] = index
>> 24;
269 readkey
[1] = index
>> 16;
270 readkey
[2] = index
>> 8;
273 outb(APPLESMC_GET_KEY_BY_INDEX_CMD
, APPLESMC_CMD_PORT
);
274 if (__wait_status(0x0c))
277 for (i
= 0; i
< 4; i
++) {
278 outb(readkey
[i
], APPLESMC_DATA_PORT
);
279 if (__wait_status(0x04))
283 outb(4, APPLESMC_DATA_PORT
);
285 for (i
= 0; i
< 4; i
++) {
286 if (__wait_status(0x05))
288 key
[i
] = inb(APPLESMC_DATA_PORT
);
296 * applesmc_get_key_type - get key type, and put the result in type (char[6]).
297 * Returns zero on success or a negative error on failure. Callers must
298 * hold applesmc_lock.
300 static int applesmc_get_key_type(char* key
, char* type
)
304 outb(APPLESMC_GET_KEY_TYPE_CMD
, APPLESMC_CMD_PORT
);
305 if (__wait_status(0x0c))
308 for (i
= 0; i
< 4; i
++) {
309 outb(key
[i
], APPLESMC_DATA_PORT
);
310 if (__wait_status(0x04))
314 outb(5, APPLESMC_DATA_PORT
);
316 for (i
= 0; i
< 6; i
++) {
317 if (__wait_status(0x05))
319 type
[i
] = inb(APPLESMC_DATA_PORT
);
327 * applesmc_read_motion_sensor - Read motion sensor (X, Y or Z). Callers must
328 * hold applesmc_lock.
330 static int applesmc_read_motion_sensor(int index
, s16
* value
)
337 ret
= applesmc_read_key(MOTION_SENSOR_X_KEY
, buffer
, 2);
340 ret
= applesmc_read_key(MOTION_SENSOR_Y_KEY
, buffer
, 2);
343 ret
= applesmc_read_key(MOTION_SENSOR_Z_KEY
, buffer
, 2);
349 *value
= ((s16
)buffer
[0] << 8) | buffer
[1];
355 * applesmc_device_init - initialize the accelerometer. Returns zero on success
356 * and negative error code on failure. Can sleep.
358 static int applesmc_device_init(void)
360 int total
, ret
= -ENXIO
;
363 if (!applesmc_accelerometer
)
366 mutex_lock(&applesmc_lock
);
368 for (total
= INIT_TIMEOUT_MSECS
; total
> 0; total
-= INIT_WAIT_MSECS
) {
370 printk(KERN_DEBUG
"applesmc try %d\n", total
);
371 if (!applesmc_read_key(MOTION_SENSOR_KEY
, buffer
, 2) &&
372 (buffer
[0] != 0x00 || buffer
[1] != 0x00)) {
373 if (total
== INIT_TIMEOUT_MSECS
) {
374 printk(KERN_DEBUG
"applesmc: device has"
375 " already been initialized"
376 " (0x%02x, 0x%02x).\n",
377 buffer
[0], buffer
[1]);
379 printk(KERN_DEBUG
"applesmc: device"
380 " successfully initialized"
381 " (0x%02x, 0x%02x).\n",
382 buffer
[0], buffer
[1]);
389 applesmc_write_key(MOTION_SENSOR_KEY
, buffer
, 2);
390 msleep(INIT_WAIT_MSECS
);
393 printk(KERN_WARNING
"applesmc: failed to init the device\n");
396 mutex_unlock(&applesmc_lock
);
401 * applesmc_get_fan_count - get the number of fans. Callers must NOT hold
404 static int applesmc_get_fan_count(void)
409 mutex_lock(&applesmc_lock
);
411 ret
= applesmc_read_key(FANS_COUNT
, buffer
, 1);
413 mutex_unlock(&applesmc_lock
);
420 /* Device model stuff */
421 static int applesmc_probe(struct platform_device
*dev
)
425 ret
= applesmc_device_init();
429 printk(KERN_INFO
"applesmc: device successfully initialized.\n");
433 static int applesmc_resume(struct platform_device
*dev
)
435 return applesmc_device_init();
438 static struct platform_driver applesmc_driver
= {
439 .probe
= applesmc_probe
,
440 .resume
= applesmc_resume
,
443 .owner
= THIS_MODULE
,
448 * applesmc_calibrate - Set our "resting" values. Callers must
449 * hold applesmc_lock.
451 static void applesmc_calibrate(void)
453 applesmc_read_motion_sensor(SENSOR_X
, &rest_x
);
454 applesmc_read_motion_sensor(SENSOR_Y
, &rest_y
);
458 static int applesmc_idev_open(struct input_dev
*dev
)
460 add_timer(&applesmc_timer
);
465 static void applesmc_idev_close(struct input_dev
*dev
)
467 del_timer_sync(&applesmc_timer
);
470 static void applesmc_idev_poll(unsigned long unused
)
474 /* Cannot sleep. Try nonblockingly. If we fail, try again later. */
475 if (!mutex_trylock(&applesmc_lock
)) {
476 mod_timer(&applesmc_timer
, jiffies
+ APPLESMC_POLL_PERIOD
);
480 if (applesmc_read_motion_sensor(SENSOR_X
, &x
))
482 if (applesmc_read_motion_sensor(SENSOR_Y
, &y
))
486 input_report_abs(applesmc_idev
, ABS_X
, x
- rest_x
);
487 input_report_abs(applesmc_idev
, ABS_Y
, y
- rest_y
);
488 input_sync(applesmc_idev
);
491 mod_timer(&applesmc_timer
, jiffies
+ APPLESMC_POLL_PERIOD
);
493 mutex_unlock(&applesmc_lock
);
498 static ssize_t
applesmc_name_show(struct device
*dev
,
499 struct device_attribute
*attr
, char *buf
)
501 return snprintf(buf
, PAGE_SIZE
, "applesmc\n");
504 static ssize_t
applesmc_position_show(struct device
*dev
,
505 struct device_attribute
*attr
, char *buf
)
510 mutex_lock(&applesmc_lock
);
512 ret
= applesmc_read_motion_sensor(SENSOR_X
, &x
);
515 ret
= applesmc_read_motion_sensor(SENSOR_Y
, &y
);
518 ret
= applesmc_read_motion_sensor(SENSOR_Z
, &z
);
523 mutex_unlock(&applesmc_lock
);
527 return snprintf(buf
, PAGE_SIZE
, "(%d,%d,%d)\n", x
, y
, z
);
530 static ssize_t
applesmc_light_show(struct device
*dev
,
531 struct device_attribute
*attr
, char *sysfsbuf
)
534 u8 left
= 0, right
= 0;
537 mutex_lock(&applesmc_lock
);
539 ret
= applesmc_read_key(LIGHT_SENSOR_LEFT_KEY
, buffer
, 6);
543 ret
= applesmc_read_key(LIGHT_SENSOR_RIGHT_KEY
, buffer
, 6);
547 mutex_unlock(&applesmc_lock
);
551 return snprintf(sysfsbuf
, PAGE_SIZE
, "(%d,%d)\n", left
, right
);
554 /* Displays degree Celsius * 1000 */
555 static ssize_t
applesmc_show_temperature(struct device
*dev
,
556 struct device_attribute
*devattr
, char *sysfsbuf
)
561 struct sensor_device_attribute
*attr
= to_sensor_dev_attr(devattr
);
563 temperature_sensors_sets
[applesmc_temperature_set
][attr
->index
];
565 mutex_lock(&applesmc_lock
);
567 ret
= applesmc_read_key(key
, buffer
, 2);
568 temp
= buffer
[0]*1000;
569 temp
+= (buffer
[1] >> 6) * 250;
571 mutex_unlock(&applesmc_lock
);
576 return snprintf(sysfsbuf
, PAGE_SIZE
, "%u\n", temp
);
579 static ssize_t
applesmc_show_fan_speed(struct device
*dev
,
580 struct device_attribute
*attr
, char *sysfsbuf
)
583 unsigned int speed
= 0;
586 struct sensor_device_attribute_2
*sensor_attr
=
587 to_sensor_dev_attr_2(attr
);
589 newkey
[0] = fan_speed_keys
[sensor_attr
->nr
][0];
590 newkey
[1] = '0' + sensor_attr
->index
;
591 newkey
[2] = fan_speed_keys
[sensor_attr
->nr
][2];
592 newkey
[3] = fan_speed_keys
[sensor_attr
->nr
][3];
595 mutex_lock(&applesmc_lock
);
597 ret
= applesmc_read_key(newkey
, buffer
, 2);
598 speed
= ((buffer
[0] << 8 | buffer
[1]) >> 2);
600 mutex_unlock(&applesmc_lock
);
604 return snprintf(sysfsbuf
, PAGE_SIZE
, "%u\n", speed
);
607 static ssize_t
applesmc_store_fan_speed(struct device
*dev
,
608 struct device_attribute
*attr
,
609 const char *sysfsbuf
, size_t count
)
615 struct sensor_device_attribute_2
*sensor_attr
=
616 to_sensor_dev_attr_2(attr
);
618 speed
= simple_strtoul(sysfsbuf
, NULL
, 10);
620 if (speed
> 0x4000) /* Bigger than a 14-bit value */
623 newkey
[0] = fan_speed_keys
[sensor_attr
->nr
][0];
624 newkey
[1] = '0' + sensor_attr
->index
;
625 newkey
[2] = fan_speed_keys
[sensor_attr
->nr
][2];
626 newkey
[3] = fan_speed_keys
[sensor_attr
->nr
][3];
629 mutex_lock(&applesmc_lock
);
631 buffer
[0] = (speed
>> 6) & 0xff;
632 buffer
[1] = (speed
<< 2) & 0xff;
633 ret
= applesmc_write_key(newkey
, buffer
, 2);
635 mutex_unlock(&applesmc_lock
);
642 static ssize_t
applesmc_show_fan_manual(struct device
*dev
,
643 struct device_attribute
*devattr
, char *sysfsbuf
)
648 struct sensor_device_attribute
*attr
= to_sensor_dev_attr(devattr
);
650 mutex_lock(&applesmc_lock
);
652 ret
= applesmc_read_key(FANS_MANUAL
, buffer
, 2);
653 manual
= ((buffer
[0] << 8 | buffer
[1]) >> attr
->index
) & 0x01;
655 mutex_unlock(&applesmc_lock
);
659 return snprintf(sysfsbuf
, PAGE_SIZE
, "%d\n", manual
);
662 static ssize_t
applesmc_store_fan_manual(struct device
*dev
,
663 struct device_attribute
*devattr
,
664 const char *sysfsbuf
, size_t count
)
670 struct sensor_device_attribute
*attr
= to_sensor_dev_attr(devattr
);
672 input
= simple_strtoul(sysfsbuf
, NULL
, 10);
674 mutex_lock(&applesmc_lock
);
676 ret
= applesmc_read_key(FANS_MANUAL
, buffer
, 2);
677 val
= (buffer
[0] << 8 | buffer
[1]);
682 val
= val
| (0x01 << attr
->index
);
684 val
= val
& ~(0x01 << attr
->index
);
686 buffer
[0] = (val
>> 8) & 0xFF;
687 buffer
[1] = val
& 0xFF;
689 ret
= applesmc_write_key(FANS_MANUAL
, buffer
, 2);
692 mutex_unlock(&applesmc_lock
);
699 static ssize_t
applesmc_show_fan_position(struct device
*dev
,
700 struct device_attribute
*attr
, char *sysfsbuf
)
705 struct sensor_device_attribute_2
*sensor_attr
=
706 to_sensor_dev_attr_2(attr
);
708 newkey
[0] = FAN_POSITION
[0];
709 newkey
[1] = '0' + sensor_attr
->index
;
710 newkey
[2] = FAN_POSITION
[2];
711 newkey
[3] = FAN_POSITION
[3];
714 mutex_lock(&applesmc_lock
);
716 ret
= applesmc_read_key(newkey
, buffer
, 16);
719 mutex_unlock(&applesmc_lock
);
723 return snprintf(sysfsbuf
, PAGE_SIZE
, "%s\n", buffer
+4);
726 static ssize_t
applesmc_calibrate_show(struct device
*dev
,
727 struct device_attribute
*attr
, char *sysfsbuf
)
729 return snprintf(sysfsbuf
, PAGE_SIZE
, "(%d,%d)\n", rest_x
, rest_y
);
732 static ssize_t
applesmc_calibrate_store(struct device
*dev
,
733 struct device_attribute
*attr
, const char *sysfsbuf
, size_t count
)
735 mutex_lock(&applesmc_lock
);
736 applesmc_calibrate();
737 mutex_unlock(&applesmc_lock
);
742 /* Store the next backlight value to be written by the work */
743 static unsigned int backlight_value
;
745 static void applesmc_backlight_set(struct work_struct
*work
)
749 mutex_lock(&applesmc_lock
);
750 buffer
[0] = backlight_value
;
752 applesmc_write_key(BACKLIGHT_KEY
, buffer
, 2);
753 mutex_unlock(&applesmc_lock
);
755 static DECLARE_WORK(backlight_work
, &applesmc_backlight_set
);
757 static void applesmc_brightness_set(struct led_classdev
*led_cdev
,
758 enum led_brightness value
)
762 backlight_value
= value
;
763 ret
= queue_work(applesmc_led_wq
, &backlight_work
);
766 printk(KERN_DEBUG
"applesmc: work was already on the queue.\n");
769 static ssize_t
applesmc_key_count_show(struct device
*dev
,
770 struct device_attribute
*attr
, char *sysfsbuf
)
776 mutex_lock(&applesmc_lock
);
778 ret
= applesmc_read_key(KEY_COUNT_KEY
, buffer
, 4);
779 count
= ((u32
)buffer
[0]<<24) + ((u32
)buffer
[1]<<16) +
780 ((u32
)buffer
[2]<<8) + buffer
[3];
782 mutex_unlock(&applesmc_lock
);
786 return snprintf(sysfsbuf
, PAGE_SIZE
, "%d\n", count
);
789 static ssize_t
applesmc_key_at_index_read_show(struct device
*dev
,
790 struct device_attribute
*attr
, char *sysfsbuf
)
796 mutex_lock(&applesmc_lock
);
798 ret
= applesmc_get_key_at_index(key_at_index
, key
);
800 if (ret
|| !key
[0]) {
801 mutex_unlock(&applesmc_lock
);
806 ret
= applesmc_get_key_type(key
, info
);
809 mutex_unlock(&applesmc_lock
);
815 * info[0] maximum value (APPLESMC_MAX_DATA_LENGTH) is much lower than
816 * PAGE_SIZE, so we don't need any checks before writing to sysfsbuf.
818 ret
= applesmc_read_key(key
, sysfsbuf
, info
[0]);
820 mutex_unlock(&applesmc_lock
);
830 static ssize_t
applesmc_key_at_index_data_length_show(struct device
*dev
,
831 struct device_attribute
*attr
, char *sysfsbuf
)
837 mutex_lock(&applesmc_lock
);
839 ret
= applesmc_get_key_at_index(key_at_index
, key
);
841 if (ret
|| !key
[0]) {
842 mutex_unlock(&applesmc_lock
);
847 ret
= applesmc_get_key_type(key
, info
);
849 mutex_unlock(&applesmc_lock
);
852 return snprintf(sysfsbuf
, PAGE_SIZE
, "%d\n", info
[0]);
857 static ssize_t
applesmc_key_at_index_type_show(struct device
*dev
,
858 struct device_attribute
*attr
, char *sysfsbuf
)
864 mutex_lock(&applesmc_lock
);
866 ret
= applesmc_get_key_at_index(key_at_index
, key
);
868 if (ret
|| !key
[0]) {
869 mutex_unlock(&applesmc_lock
);
874 ret
= applesmc_get_key_type(key
, info
);
876 mutex_unlock(&applesmc_lock
);
879 return snprintf(sysfsbuf
, PAGE_SIZE
, "%s\n", info
+1);
884 static ssize_t
applesmc_key_at_index_name_show(struct device
*dev
,
885 struct device_attribute
*attr
, char *sysfsbuf
)
890 mutex_lock(&applesmc_lock
);
892 ret
= applesmc_get_key_at_index(key_at_index
, key
);
894 mutex_unlock(&applesmc_lock
);
897 return snprintf(sysfsbuf
, PAGE_SIZE
, "%s\n", key
);
902 static ssize_t
applesmc_key_at_index_show(struct device
*dev
,
903 struct device_attribute
*attr
, char *sysfsbuf
)
905 return snprintf(sysfsbuf
, PAGE_SIZE
, "%d\n", key_at_index
);
908 static ssize_t
applesmc_key_at_index_store(struct device
*dev
,
909 struct device_attribute
*attr
, const char *sysfsbuf
, size_t count
)
911 mutex_lock(&applesmc_lock
);
913 key_at_index
= simple_strtoul(sysfsbuf
, NULL
, 10);
915 mutex_unlock(&applesmc_lock
);
920 static struct led_classdev applesmc_backlight
= {
921 .name
= "smc:kbd_backlight",
922 .default_trigger
= "nand-disk",
923 .brightness_set
= applesmc_brightness_set
,
926 static DEVICE_ATTR(name
, 0444, applesmc_name_show
, NULL
);
928 static DEVICE_ATTR(position
, 0444, applesmc_position_show
, NULL
);
929 static DEVICE_ATTR(calibrate
, 0644,
930 applesmc_calibrate_show
, applesmc_calibrate_store
);
932 static struct attribute
*accelerometer_attributes
[] = {
933 &dev_attr_position
.attr
,
934 &dev_attr_calibrate
.attr
,
938 static const struct attribute_group accelerometer_attributes_group
=
939 { .attrs
= accelerometer_attributes
};
941 static DEVICE_ATTR(light
, 0444, applesmc_light_show
, NULL
);
943 static DEVICE_ATTR(key_count
, 0444, applesmc_key_count_show
, NULL
);
944 static DEVICE_ATTR(key_at_index
, 0644,
945 applesmc_key_at_index_show
, applesmc_key_at_index_store
);
946 static DEVICE_ATTR(key_at_index_name
, 0444,
947 applesmc_key_at_index_name_show
, NULL
);
948 static DEVICE_ATTR(key_at_index_type
, 0444,
949 applesmc_key_at_index_type_show
, NULL
);
950 static DEVICE_ATTR(key_at_index_data_length
, 0444,
951 applesmc_key_at_index_data_length_show
, NULL
);
952 static DEVICE_ATTR(key_at_index_data
, 0444,
953 applesmc_key_at_index_read_show
, NULL
);
955 static struct attribute
*key_enumeration_attributes
[] = {
956 &dev_attr_key_count
.attr
,
957 &dev_attr_key_at_index
.attr
,
958 &dev_attr_key_at_index_name
.attr
,
959 &dev_attr_key_at_index_type
.attr
,
960 &dev_attr_key_at_index_data_length
.attr
,
961 &dev_attr_key_at_index_data
.attr
,
965 static const struct attribute_group key_enumeration_group
=
966 { .attrs
= key_enumeration_attributes
};
969 * Macro defining SENSOR_DEVICE_ATTR for a fan sysfs entries.
970 * - show actual speed
971 * - show/store minimum speed
972 * - show maximum speed
974 * - show/store target speed
975 * - show/store manual mode
977 #define sysfs_fan_speeds_offset(offset) \
978 static SENSOR_DEVICE_ATTR_2(fan##offset##_input, S_IRUGO, \
979 applesmc_show_fan_speed, NULL, 0, offset-1); \
981 static SENSOR_DEVICE_ATTR_2(fan##offset##_min, S_IRUGO | S_IWUSR, \
982 applesmc_show_fan_speed, applesmc_store_fan_speed, 1, offset-1); \
984 static SENSOR_DEVICE_ATTR_2(fan##offset##_max, S_IRUGO, \
985 applesmc_show_fan_speed, NULL, 2, offset-1); \
987 static SENSOR_DEVICE_ATTR_2(fan##offset##_safe, S_IRUGO, \
988 applesmc_show_fan_speed, NULL, 3, offset-1); \
990 static SENSOR_DEVICE_ATTR_2(fan##offset##_output, S_IRUGO | S_IWUSR, \
991 applesmc_show_fan_speed, applesmc_store_fan_speed, 4, offset-1); \
993 static SENSOR_DEVICE_ATTR(fan##offset##_manual, S_IRUGO | S_IWUSR, \
994 applesmc_show_fan_manual, applesmc_store_fan_manual, offset-1); \
996 static SENSOR_DEVICE_ATTR(fan##offset##_label, S_IRUGO, \
997 applesmc_show_fan_position, NULL, offset-1); \
999 static struct attribute *fan##offset##_attributes[] = { \
1000 &sensor_dev_attr_fan##offset##_input.dev_attr.attr, \
1001 &sensor_dev_attr_fan##offset##_min.dev_attr.attr, \
1002 &sensor_dev_attr_fan##offset##_max.dev_attr.attr, \
1003 &sensor_dev_attr_fan##offset##_safe.dev_attr.attr, \
1004 &sensor_dev_attr_fan##offset##_output.dev_attr.attr, \
1005 &sensor_dev_attr_fan##offset##_manual.dev_attr.attr, \
1006 &sensor_dev_attr_fan##offset##_label.dev_attr.attr, \
1011 * Create the needed functions for each fan using the macro defined above
1012 * (2 fans are supported)
1014 sysfs_fan_speeds_offset(1);
1015 sysfs_fan_speeds_offset(2);
1017 static const struct attribute_group fan_attribute_groups
[] = {
1018 { .attrs
= fan1_attributes
},
1019 { .attrs
= fan2_attributes
}
1023 * Temperature sensors sysfs entries.
1025 static SENSOR_DEVICE_ATTR(temp1_input
, S_IRUGO
,
1026 applesmc_show_temperature
, NULL
, 0);
1027 static SENSOR_DEVICE_ATTR(temp2_input
, S_IRUGO
,
1028 applesmc_show_temperature
, NULL
, 1);
1029 static SENSOR_DEVICE_ATTR(temp3_input
, S_IRUGO
,
1030 applesmc_show_temperature
, NULL
, 2);
1031 static SENSOR_DEVICE_ATTR(temp4_input
, S_IRUGO
,
1032 applesmc_show_temperature
, NULL
, 3);
1033 static SENSOR_DEVICE_ATTR(temp5_input
, S_IRUGO
,
1034 applesmc_show_temperature
, NULL
, 4);
1035 static SENSOR_DEVICE_ATTR(temp6_input
, S_IRUGO
,
1036 applesmc_show_temperature
, NULL
, 5);
1037 static SENSOR_DEVICE_ATTR(temp7_input
, S_IRUGO
,
1038 applesmc_show_temperature
, NULL
, 6);
1039 static SENSOR_DEVICE_ATTR(temp8_input
, S_IRUGO
,
1040 applesmc_show_temperature
, NULL
, 7);
1041 static SENSOR_DEVICE_ATTR(temp9_input
, S_IRUGO
,
1042 applesmc_show_temperature
, NULL
, 8);
1043 static SENSOR_DEVICE_ATTR(temp10_input
, S_IRUGO
,
1044 applesmc_show_temperature
, NULL
, 9);
1045 static SENSOR_DEVICE_ATTR(temp11_input
, S_IRUGO
,
1046 applesmc_show_temperature
, NULL
, 10);
1047 static SENSOR_DEVICE_ATTR(temp12_input
, S_IRUGO
,
1048 applesmc_show_temperature
, NULL
, 11);
1050 static struct attribute
*temperature_attributes
[] = {
1051 &sensor_dev_attr_temp1_input
.dev_attr
.attr
,
1052 &sensor_dev_attr_temp2_input
.dev_attr
.attr
,
1053 &sensor_dev_attr_temp3_input
.dev_attr
.attr
,
1054 &sensor_dev_attr_temp4_input
.dev_attr
.attr
,
1055 &sensor_dev_attr_temp5_input
.dev_attr
.attr
,
1056 &sensor_dev_attr_temp6_input
.dev_attr
.attr
,
1057 &sensor_dev_attr_temp7_input
.dev_attr
.attr
,
1058 &sensor_dev_attr_temp8_input
.dev_attr
.attr
,
1059 &sensor_dev_attr_temp9_input
.dev_attr
.attr
,
1060 &sensor_dev_attr_temp10_input
.dev_attr
.attr
,
1061 &sensor_dev_attr_temp11_input
.dev_attr
.attr
,
1062 &sensor_dev_attr_temp12_input
.dev_attr
.attr
,
1066 static const struct attribute_group temperature_attributes_group
=
1067 { .attrs
= temperature_attributes
};
1072 * applesmc_dmi_match - found a match. return one, short-circuiting the hunt.
1074 static int applesmc_dmi_match(struct dmi_system_id
*id
)
1077 struct dmi_match_data
* dmi_data
= id
->driver_data
;
1078 printk(KERN_INFO
"applesmc: %s detected:\n", id
->ident
);
1079 applesmc_accelerometer
= dmi_data
->accelerometer
;
1080 printk(KERN_INFO
"applesmc: - Model %s accelerometer\n",
1081 applesmc_accelerometer
? "with" : "without");
1082 applesmc_light
= dmi_data
->light
;
1083 printk(KERN_INFO
"applesmc: - Model %s light sensors and backlight\n",
1084 applesmc_light
? "with" : "without");
1086 applesmc_temperature_set
= dmi_data
->temperature_set
;
1087 while (temperature_sensors_sets
[applesmc_temperature_set
][i
] != NULL
)
1089 printk(KERN_INFO
"applesmc: - Model with %d temperature sensors\n", i
);
1093 /* Create accelerometer ressources */
1094 static int applesmc_create_accelerometer(void)
1098 ret
= sysfs_create_group(&pdev
->dev
.kobj
,
1099 &accelerometer_attributes_group
);
1103 applesmc_idev
= input_allocate_device();
1104 if (!applesmc_idev
) {
1109 /* initial calibrate for the input device */
1110 applesmc_calibrate();
1112 /* initialize the input class */
1113 applesmc_idev
->name
= "applesmc";
1114 applesmc_idev
->id
.bustype
= BUS_HOST
;
1115 applesmc_idev
->dev
.parent
= &pdev
->dev
;
1116 applesmc_idev
->evbit
[0] = BIT(EV_ABS
);
1117 applesmc_idev
->open
= applesmc_idev_open
;
1118 applesmc_idev
->close
= applesmc_idev_close
;
1119 input_set_abs_params(applesmc_idev
, ABS_X
,
1120 -256, 256, APPLESMC_INPUT_FUZZ
, APPLESMC_INPUT_FLAT
);
1121 input_set_abs_params(applesmc_idev
, ABS_Y
,
1122 -256, 256, APPLESMC_INPUT_FUZZ
, APPLESMC_INPUT_FLAT
);
1124 ret
= input_register_device(applesmc_idev
);
1128 /* start up our timer for the input device */
1129 init_timer(&applesmc_timer
);
1130 applesmc_timer
.function
= applesmc_idev_poll
;
1131 applesmc_timer
.expires
= jiffies
+ APPLESMC_POLL_PERIOD
;
1136 input_free_device(applesmc_idev
);
1139 sysfs_remove_group(&pdev
->dev
.kobj
, &accelerometer_attributes_group
);
1142 printk(KERN_WARNING
"applesmc: driver init failed (ret=%d)!\n", ret
);
1146 /* Release all ressources used by the accelerometer */
1147 static void applesmc_release_accelerometer(void)
1149 del_timer_sync(&applesmc_timer
);
1150 input_unregister_device(applesmc_idev
);
1151 sysfs_remove_group(&pdev
->dev
.kobj
, &accelerometer_attributes_group
);
1154 static __initdata
struct dmi_match_data applesmc_dmi_data
[] = {
1155 /* MacBook Pro: accelerometer, backlight and temperature set 0 */
1156 { .accelerometer
= 1, .light
= 1, .temperature_set
= 0 },
1157 /* MacBook: accelerometer and temperature set 1 */
1158 { .accelerometer
= 1, .light
= 0, .temperature_set
= 1 },
1159 /* MacMini: temperature set 2 */
1160 { .accelerometer
= 0, .light
= 0, .temperature_set
= 2 },
1163 /* Note that DMI_MATCH(...,"MacBook") will match "MacBookPro1,1".
1164 * So we need to put "Apple MacBook Pro" before "Apple MacBook". */
1165 static __initdata
struct dmi_system_id applesmc_whitelist
[] = {
1166 { applesmc_dmi_match
, "Apple MacBook Pro", {
1167 DMI_MATCH(DMI_BOARD_VENDOR
,"Apple"),
1168 DMI_MATCH(DMI_PRODUCT_NAME
,"MacBookPro") },
1169 (void*)&applesmc_dmi_data
[0]},
1170 { applesmc_dmi_match
, "Apple MacBook", {
1171 DMI_MATCH(DMI_BOARD_VENDOR
,"Apple"),
1172 DMI_MATCH(DMI_PRODUCT_NAME
,"MacBook") },
1173 (void*)&applesmc_dmi_data
[1]},
1174 { applesmc_dmi_match
, "Apple Macmini", {
1175 DMI_MATCH(DMI_BOARD_VENDOR
,"Apple"),
1176 DMI_MATCH(DMI_PRODUCT_NAME
,"Macmini") },
1177 (void*)&applesmc_dmi_data
[2]},
1181 static int __init
applesmc_init(void)
1187 mutex_init(&applesmc_lock
);
1189 if (!dmi_check_system(applesmc_whitelist
)) {
1190 printk(KERN_WARNING
"applesmc: supported laptop not found!\n");
1195 if (!request_region(APPLESMC_DATA_PORT
, APPLESMC_NR_PORTS
,
1201 ret
= platform_driver_register(&applesmc_driver
);
1205 pdev
= platform_device_register_simple("applesmc", APPLESMC_DATA_PORT
,
1208 ret
= PTR_ERR(pdev
);
1212 ret
= sysfs_create_file(&pdev
->dev
.kobj
, &dev_attr_name
.attr
);
1216 /* Create key enumeration sysfs files */
1217 ret
= sysfs_create_group(&pdev
->dev
.kobj
, &key_enumeration_group
);
1221 /* create fan files */
1222 count
= applesmc_get_fan_count();
1224 printk(KERN_ERR
"applesmc: Cannot get the number of fans.\n");
1226 printk(KERN_INFO
"applesmc: %d fans found.\n", count
);
1230 printk(KERN_WARNING
"applesmc: More than 2 fans found,"
1231 " but at most 2 fans are supported"
1232 " by the driver.\n");
1234 ret
= sysfs_create_group(&pdev
->dev
.kobj
,
1235 &fan_attribute_groups
[1]);
1237 goto out_key_enumeration
;
1239 ret
= sysfs_create_group(&pdev
->dev
.kobj
,
1240 &fan_attribute_groups
[0]);
1249 temperature_sensors_sets
[applesmc_temperature_set
][i
] != NULL
;
1251 if (temperature_attributes
[i
] == NULL
) {
1252 printk(KERN_ERR
"applesmc: More temperature sensors "
1253 "in temperature_sensors_sets (at least %i)"
1254 "than available sysfs files in "
1255 "temperature_attributes (%i), please report "
1256 "this bug.\n", i
, i
-1);
1257 goto out_temperature
;
1259 ret
= sysfs_create_file(&pdev
->dev
.kobj
,
1260 temperature_attributes
[i
]);
1262 goto out_temperature
;
1265 if (applesmc_accelerometer
) {
1266 ret
= applesmc_create_accelerometer();
1268 goto out_temperature
;
1271 if (applesmc_light
) {
1272 /* Add light sensor file */
1273 ret
= sysfs_create_file(&pdev
->dev
.kobj
, &dev_attr_light
.attr
);
1275 goto out_accelerometer
;
1277 /* Create the workqueue */
1278 applesmc_led_wq
= create_singlethread_workqueue("applesmc-led");
1279 if (!applesmc_led_wq
) {
1281 goto out_light_sysfs
;
1284 /* register as a led device */
1285 ret
= led_classdev_register(&pdev
->dev
, &applesmc_backlight
);
1290 hwmon_class_dev
= hwmon_device_register(&pdev
->dev
);
1291 if (IS_ERR(hwmon_class_dev
)) {
1292 ret
= PTR_ERR(hwmon_class_dev
);
1293 goto out_light_ledclass
;
1296 printk(KERN_INFO
"applesmc: driver successfully loaded.\n");
1302 led_classdev_unregister(&applesmc_backlight
);
1305 destroy_workqueue(applesmc_led_wq
);
1308 sysfs_remove_file(&pdev
->dev
.kobj
, &dev_attr_light
.attr
);
1310 if (applesmc_accelerometer
)
1311 applesmc_release_accelerometer();
1313 sysfs_remove_group(&pdev
->dev
.kobj
, &temperature_attributes_group
);
1314 sysfs_remove_group(&pdev
->dev
.kobj
, &fan_attribute_groups
[0]);
1316 sysfs_remove_group(&pdev
->dev
.kobj
, &fan_attribute_groups
[1]);
1317 out_key_enumeration
:
1318 sysfs_remove_group(&pdev
->dev
.kobj
, &key_enumeration_group
);
1320 sysfs_remove_file(&pdev
->dev
.kobj
, &dev_attr_name
.attr
);
1322 platform_device_unregister(pdev
);
1324 platform_driver_unregister(&applesmc_driver
);
1326 release_region(APPLESMC_DATA_PORT
, APPLESMC_NR_PORTS
);
1328 printk(KERN_WARNING
"applesmc: driver init failed (ret=%d)!\n", ret
);
1332 static void __exit
applesmc_exit(void)
1334 hwmon_device_unregister(hwmon_class_dev
);
1335 if (applesmc_light
) {
1336 led_classdev_unregister(&applesmc_backlight
);
1337 destroy_workqueue(applesmc_led_wq
);
1338 sysfs_remove_file(&pdev
->dev
.kobj
, &dev_attr_light
.attr
);
1340 if (applesmc_accelerometer
)
1341 applesmc_release_accelerometer();
1342 sysfs_remove_group(&pdev
->dev
.kobj
, &temperature_attributes_group
);
1343 sysfs_remove_group(&pdev
->dev
.kobj
, &fan_attribute_groups
[0]);
1344 sysfs_remove_group(&pdev
->dev
.kobj
, &fan_attribute_groups
[1]);
1345 sysfs_remove_group(&pdev
->dev
.kobj
, &key_enumeration_group
);
1346 sysfs_remove_file(&pdev
->dev
.kobj
, &dev_attr_name
.attr
);
1347 platform_device_unregister(pdev
);
1348 platform_driver_unregister(&applesmc_driver
);
1349 release_region(APPLESMC_DATA_PORT
, APPLESMC_NR_PORTS
);
1351 printk(KERN_INFO
"applesmc: driver unloaded.\n");
1354 module_init(applesmc_init
);
1355 module_exit(applesmc_exit
);
1357 MODULE_AUTHOR("Nicolas Boichat");
1358 MODULE_DESCRIPTION("Apple SMC");
1359 MODULE_LICENSE("GPL v2");