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 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
31 #include <linux/delay.h>
32 #include <linux/platform_device.h>
33 #include <linux/input-polldev.h>
34 #include <linux/kernel.h>
35 #include <linux/slab.h>
36 #include <linux/module.h>
37 #include <linux/timer.h>
38 #include <linux/dmi.h>
39 #include <linux/mutex.h>
40 #include <linux/hwmon-sysfs.h>
42 #include <linux/leds.h>
43 #include <linux/hwmon.h>
44 #include <linux/workqueue.h>
46 /* data port used by Apple SMC */
47 #define APPLESMC_DATA_PORT 0x300
48 /* command/status port used by Apple SMC */
49 #define APPLESMC_CMD_PORT 0x304
51 #define APPLESMC_NR_PORTS 32 /* 0x300-0x31f */
53 #define APPLESMC_MAX_DATA_LENGTH 32
55 /* wait up to 32 ms for a status change. */
56 #define APPLESMC_MIN_WAIT 0x0040
57 #define APPLESMC_MAX_WAIT 0x8000
59 #define APPLESMC_STATUS_MASK 0x0f
60 #define APPLESMC_READ_CMD 0x10
61 #define APPLESMC_WRITE_CMD 0x11
62 #define APPLESMC_GET_KEY_BY_INDEX_CMD 0x12
63 #define APPLESMC_GET_KEY_TYPE_CMD 0x13
65 #define KEY_COUNT_KEY "#KEY" /* r-o ui32 */
67 #define LIGHT_SENSOR_LEFT_KEY "ALV0" /* r-o {alv (6-10 bytes) */
68 #define LIGHT_SENSOR_RIGHT_KEY "ALV1" /* r-o {alv (6-10 bytes) */
69 #define BACKLIGHT_KEY "LKSB" /* w-o {lkb (2 bytes) */
71 #define CLAMSHELL_KEY "MSLD" /* r-o ui8 (unused) */
73 #define MOTION_SENSOR_X_KEY "MO_X" /* r-o sp78 (2 bytes) */
74 #define MOTION_SENSOR_Y_KEY "MO_Y" /* r-o sp78 (2 bytes) */
75 #define MOTION_SENSOR_Z_KEY "MO_Z" /* r-o sp78 (2 bytes) */
76 #define MOTION_SENSOR_KEY "MOCN" /* r/w ui16 */
78 #define FANS_COUNT "FNum" /* r-o ui8 */
79 #define FANS_MANUAL "FS! " /* r-w ui16 */
80 #define FAN_ACTUAL_SPEED "F0Ac" /* r-o fpe2 (2 bytes) */
81 #define FAN_MIN_SPEED "F0Mn" /* r-o fpe2 (2 bytes) */
82 #define FAN_MAX_SPEED "F0Mx" /* r-o fpe2 (2 bytes) */
83 #define FAN_SAFE_SPEED "F0Sf" /* r-o fpe2 (2 bytes) */
84 #define FAN_TARGET_SPEED "F0Tg" /* r-w fpe2 (2 bytes) */
85 #define FAN_POSITION "F0ID" /* r-o char[16] */
87 /* List of keys used to read/write fan speeds */
88 static const char* fan_speed_keys[] = {
96 #define INIT_TIMEOUT_MSECS 5000 /* wait up to 5s for device init ... */
97 #define INIT_WAIT_MSECS 50 /* ... in 50ms increments */
99 #define APPLESMC_POLL_INTERVAL 50 /* msecs */
100 #define APPLESMC_INPUT_FUZZ 4 /* input event threshold */
101 #define APPLESMC_INPUT_FLAT 4
107 #define to_index(attr) (to_sensor_dev_attr(attr)->index)
109 /* Dynamic device node attributes */
110 struct applesmc_dev_attr {
111 struct sensor_device_attribute sda; /* hwmon attributes */
112 char name[32]; /* room for node file name */
115 /* Dynamic device node group */
116 struct applesmc_node_group {
117 char *format; /* format string */
118 void *show; /* show function */
119 void *store; /* store function */
120 struct applesmc_dev_attr *nodes; /* dynamic node array */
123 /* AppleSMC entry - cached register information */
124 struct applesmc_entry {
125 char key[5]; /* four-letter key code */
126 u8 valid; /* set when entry is successfully read once */
127 u8 len; /* bounded by APPLESMC_MAX_DATA_LENGTH */
128 char type[5]; /* four-letter type code */
129 u8 flags; /* 0x10: func; 0x40: write; 0x80: read */
132 /* Register lookup and registers common to all SMCs */
133 static struct applesmc_registers {
134 struct mutex mutex; /* register read/write mutex */
135 unsigned int key_count; /* number of SMC registers */
136 unsigned int temp_count; /* number of temperature registers */
137 unsigned int temp_begin; /* temperature lower index bound */
138 unsigned int temp_end; /* temperature upper index bound */
139 int num_light_sensors; /* number of light sensors */
140 bool has_accelerometer; /* has motion sensor */
141 bool has_key_backlight; /* has keyboard backlight */
142 bool init_complete; /* true when fully initialized */
143 struct applesmc_entry *cache; /* cached key entries */
145 .mutex = __MUTEX_INITIALIZER(smcreg.mutex),
148 static const int debug;
149 static struct platform_device *pdev;
152 static u8 backlight_state[2];
154 static struct device *hwmon_dev;
155 static struct input_polled_dev *applesmc_idev;
157 /* The number of fans handled by the driver */
158 static unsigned int fans_handled;
161 * Last index written to key_at_index sysfs file, and value to use for all other
162 * key_at_index_* sysfs files.
164 static unsigned int key_at_index;
166 static struct workqueue_struct *applesmc_led_wq;
169 * __wait_status - Wait up to 32ms for the status port to get a certain value
170 * (masked with 0x0f), returning zero if the value is obtained. Callers must
171 * hold applesmc_lock.
173 static int __wait_status(u8 val)
177 val = val & APPLESMC_STATUS_MASK;
179 for (us = APPLESMC_MIN_WAIT; us < APPLESMC_MAX_WAIT; us <<= 1) {
181 if ((inb(APPLESMC_CMD_PORT) & APPLESMC_STATUS_MASK) == val) {
190 * special treatment of command port - on newer macbooks, it seems necessary
191 * to resend the command byte before polling the status again. Callers must
192 * hold applesmc_lock.
194 static int send_command(u8 cmd)
197 for (us = APPLESMC_MIN_WAIT; us < APPLESMC_MAX_WAIT; us <<= 1) {
198 outb(cmd, APPLESMC_CMD_PORT);
200 if ((inb(APPLESMC_CMD_PORT) & APPLESMC_STATUS_MASK) == 0x0c)
206 static int send_argument(const char *key)
210 for (i = 0; i < 4; i++) {
211 outb(key[i], APPLESMC_DATA_PORT);
212 if (__wait_status(0x04))
218 static int read_smc(u8 cmd, const char *key, u8 *buffer, u8 len)
222 if (send_command(cmd) || send_argument(key)) {
223 pr_warn("%s: read arg fail\n", key);
227 outb(len, APPLESMC_DATA_PORT);
229 for (i = 0; i < len; i++) {
230 if (__wait_status(0x05)) {
231 pr_warn("%s: read data fail\n", key);
234 buffer[i] = inb(APPLESMC_DATA_PORT);
240 static int write_smc(u8 cmd, const char *key, const u8 *buffer, u8 len)
244 if (send_command(cmd) || send_argument(key)) {
245 pr_warn("%s: write arg fail\n", key);
249 outb(len, APPLESMC_DATA_PORT);
251 for (i = 0; i < len; i++) {
252 if (__wait_status(0x04)) {
253 pr_warn("%s: write data fail\n", key);
256 outb(buffer[i], APPLESMC_DATA_PORT);
262 static int read_register_count(unsigned int *count)
267 ret = read_smc(APPLESMC_READ_CMD, KEY_COUNT_KEY, (u8 *)&be, 4);
271 *count = be32_to_cpu(be);
278 * Returns zero on success or a negative error on failure.
279 * All functions below are concurrency safe - callers should NOT hold lock.
282 static int applesmc_read_entry(const struct applesmc_entry *entry,
287 if (entry->len != len)
289 mutex_lock(&smcreg.mutex);
290 ret = read_smc(APPLESMC_READ_CMD, entry->key, buf, len);
291 mutex_unlock(&smcreg.mutex);
296 static int applesmc_write_entry(const struct applesmc_entry *entry,
297 const u8 *buf, u8 len)
301 if (entry->len != len)
303 mutex_lock(&smcreg.mutex);
304 ret = write_smc(APPLESMC_WRITE_CMD, entry->key, buf, len);
305 mutex_unlock(&smcreg.mutex);
309 static const struct applesmc_entry *applesmc_get_entry_by_index(int index)
311 struct applesmc_entry *cache = &smcreg.cache[index];
319 mutex_lock(&smcreg.mutex);
323 be = cpu_to_be32(index);
324 ret = read_smc(APPLESMC_GET_KEY_BY_INDEX_CMD, (u8 *)&be, key, 4);
327 ret = read_smc(APPLESMC_GET_KEY_TYPE_CMD, key, info, 6);
331 memcpy(cache->key, key, 4);
332 cache->len = info[0];
333 memcpy(cache->type, &info[1], 4);
334 cache->flags = info[5];
338 mutex_unlock(&smcreg.mutex);
344 static int applesmc_get_lower_bound(unsigned int *lo, const char *key)
346 int begin = 0, end = smcreg.key_count;
347 const struct applesmc_entry *entry;
349 while (begin != end) {
350 int middle = begin + (end - begin) / 2;
351 entry = applesmc_get_entry_by_index(middle);
353 return PTR_ERR(entry);
354 if (strcmp(entry->key, key) < 0)
364 static int applesmc_get_upper_bound(unsigned int *hi, const char *key)
366 int begin = 0, end = smcreg.key_count;
367 const struct applesmc_entry *entry;
369 while (begin != end) {
370 int middle = begin + (end - begin) / 2;
371 entry = applesmc_get_entry_by_index(middle);
373 return PTR_ERR(entry);
374 if (strcmp(key, entry->key) < 0)
384 static const struct applesmc_entry *applesmc_get_entry_by_key(const char *key)
389 ret = applesmc_get_lower_bound(&begin, key);
392 ret = applesmc_get_upper_bound(&end, key);
395 if (end - begin != 1)
396 return ERR_PTR(-EINVAL);
398 return applesmc_get_entry_by_index(begin);
401 static int applesmc_read_key(const char *key, u8 *buffer, u8 len)
403 const struct applesmc_entry *entry;
405 entry = applesmc_get_entry_by_key(key);
407 return PTR_ERR(entry);
409 return applesmc_read_entry(entry, buffer, len);
412 static int applesmc_write_key(const char *key, const u8 *buffer, u8 len)
414 const struct applesmc_entry *entry;
416 entry = applesmc_get_entry_by_key(key);
418 return PTR_ERR(entry);
420 return applesmc_write_entry(entry, buffer, len);
423 static int applesmc_has_key(const char *key, bool *value)
425 const struct applesmc_entry *entry;
427 entry = applesmc_get_entry_by_key(key);
428 if (IS_ERR(entry) && PTR_ERR(entry) != -EINVAL)
429 return PTR_ERR(entry);
431 *value = !IS_ERR(entry);
436 * applesmc_read_motion_sensor - Read motion sensor (X, Y or Z).
438 static int applesmc_read_motion_sensor(int index, s16* value)
445 ret = applesmc_read_key(MOTION_SENSOR_X_KEY, buffer, 2);
448 ret = applesmc_read_key(MOTION_SENSOR_Y_KEY, buffer, 2);
451 ret = applesmc_read_key(MOTION_SENSOR_Z_KEY, buffer, 2);
457 *value = ((s16)buffer[0] << 8) | buffer[1];
463 * applesmc_device_init - initialize the accelerometer. Can sleep.
465 static void applesmc_device_init(void)
470 if (!smcreg.has_accelerometer)
473 for (total = INIT_TIMEOUT_MSECS; total > 0; total -= INIT_WAIT_MSECS) {
474 if (!applesmc_read_key(MOTION_SENSOR_KEY, buffer, 2) &&
475 (buffer[0] != 0x00 || buffer[1] != 0x00))
479 applesmc_write_key(MOTION_SENSOR_KEY, buffer, 2);
480 msleep(INIT_WAIT_MSECS);
483 pr_warn("failed to init the device\n");
487 * applesmc_get_fan_count - get the number of fans.
489 static int applesmc_get_fan_count(void)
494 ret = applesmc_read_key(FANS_COUNT, buffer, 1);
503 * applesmc_init_smcreg_try - Try to initialize register cache. Idempotent.
505 static int applesmc_init_smcreg_try(void)
507 struct applesmc_registers *s = &smcreg;
508 bool left_light_sensor, right_light_sensor;
511 if (s->init_complete)
514 ret = read_register_count(&s->key_count);
519 s->cache = kcalloc(s->key_count, sizeof(*s->cache), GFP_KERNEL);
523 ret = applesmc_get_lower_bound(&s->temp_begin, "T");
526 ret = applesmc_get_lower_bound(&s->temp_end, "U");
529 s->temp_count = s->temp_end - s->temp_begin;
531 ret = applesmc_has_key(LIGHT_SENSOR_LEFT_KEY, &left_light_sensor);
534 ret = applesmc_has_key(LIGHT_SENSOR_RIGHT_KEY, &right_light_sensor);
537 ret = applesmc_has_key(MOTION_SENSOR_KEY, &s->has_accelerometer);
540 ret = applesmc_has_key(BACKLIGHT_KEY, &s->has_key_backlight);
544 s->num_light_sensors = left_light_sensor + right_light_sensor;
545 s->init_complete = true;
547 pr_info("key=%d temp=%d acc=%d lux=%d kbd=%d\n",
548 s->key_count, s->temp_count,
549 s->has_accelerometer,
550 s->num_light_sensors,
551 s->has_key_backlight);
557 * applesmc_init_smcreg - Initialize register cache.
559 * Retries until initialization is successful, or the operation times out.
562 static int applesmc_init_smcreg(void)
566 for (ms = 0; ms < INIT_TIMEOUT_MSECS; ms += INIT_WAIT_MSECS) {
567 ret = applesmc_init_smcreg_try();
570 pr_info("init_smcreg() took %d ms\n", ms);
573 msleep(INIT_WAIT_MSECS);
582 static void applesmc_destroy_smcreg(void)
586 smcreg.init_complete = false;
589 /* Device model stuff */
590 static int applesmc_probe(struct platform_device *dev)
594 ret = applesmc_init_smcreg();
598 applesmc_device_init();
603 /* Synchronize device with memorized backlight state */
604 static int applesmc_pm_resume(struct device *dev)
606 if (smcreg.has_key_backlight)
607 applesmc_write_key(BACKLIGHT_KEY, backlight_state, 2);
611 /* Reinitialize device on resume from hibernation */
612 static int applesmc_pm_restore(struct device *dev)
614 applesmc_device_init();
615 return applesmc_pm_resume(dev);
618 static const struct dev_pm_ops applesmc_pm_ops = {
619 .resume = applesmc_pm_resume,
620 .restore = applesmc_pm_restore,
623 static struct platform_driver applesmc_driver = {
624 .probe = applesmc_probe,
627 .owner = THIS_MODULE,
628 .pm = &applesmc_pm_ops,
633 * applesmc_calibrate - Set our "resting" values. Callers must
634 * hold applesmc_lock.
636 static void applesmc_calibrate(void)
638 applesmc_read_motion_sensor(SENSOR_X, &rest_x);
639 applesmc_read_motion_sensor(SENSOR_Y, &rest_y);
643 static void applesmc_idev_poll(struct input_polled_dev *dev)
645 struct input_dev *idev = dev->input;
648 if (applesmc_read_motion_sensor(SENSOR_X, &x))
650 if (applesmc_read_motion_sensor(SENSOR_Y, &y))
654 input_report_abs(idev, ABS_X, x - rest_x);
655 input_report_abs(idev, ABS_Y, y - rest_y);
661 static ssize_t applesmc_name_show(struct device *dev,
662 struct device_attribute *attr, char *buf)
664 return snprintf(buf, PAGE_SIZE, "applesmc\n");
667 static ssize_t applesmc_position_show(struct device *dev,
668 struct device_attribute *attr, char *buf)
673 ret = applesmc_read_motion_sensor(SENSOR_X, &x);
676 ret = applesmc_read_motion_sensor(SENSOR_Y, &y);
679 ret = applesmc_read_motion_sensor(SENSOR_Z, &z);
687 return snprintf(buf, PAGE_SIZE, "(%d,%d,%d)\n", x, y, z);
690 static ssize_t applesmc_light_show(struct device *dev,
691 struct device_attribute *attr, char *sysfsbuf)
693 const struct applesmc_entry *entry;
694 static int data_length;
696 u8 left = 0, right = 0;
700 entry = applesmc_get_entry_by_key(LIGHT_SENSOR_LEFT_KEY);
702 return PTR_ERR(entry);
705 data_length = entry->len;
706 pr_info("light sensor data length set to %d\n", data_length);
709 ret = applesmc_read_key(LIGHT_SENSOR_LEFT_KEY, buffer, data_length);
710 /* newer macbooks report a single 10-bit bigendian value */
711 if (data_length == 10) {
712 left = be16_to_cpu(*(__be16 *)(buffer + 6)) >> 2;
718 ret = applesmc_read_key(LIGHT_SENSOR_RIGHT_KEY, buffer, data_length);
725 return snprintf(sysfsbuf, PAGE_SIZE, "(%d,%d)\n", left, right);
728 /* Displays sensor key as label */
729 static ssize_t applesmc_show_sensor_label(struct device *dev,
730 struct device_attribute *devattr, char *sysfsbuf)
732 int index = smcreg.temp_begin + to_index(devattr);
733 const struct applesmc_entry *entry;
735 entry = applesmc_get_entry_by_index(index);
737 return PTR_ERR(entry);
739 return snprintf(sysfsbuf, PAGE_SIZE, "%s\n", entry->key);
742 /* Displays degree Celsius * 1000 */
743 static ssize_t applesmc_show_temperature(struct device *dev,
744 struct device_attribute *devattr, char *sysfsbuf)
746 int index = smcreg.temp_begin + to_index(devattr);
747 const struct applesmc_entry *entry;
752 entry = applesmc_get_entry_by_index(index);
754 return PTR_ERR(entry);
758 ret = applesmc_read_entry(entry, buffer, entry->len);
762 if (entry->len == 2) {
763 temp = buffer[0] * 1000;
764 temp += (buffer[1] >> 6) * 250;
766 temp = buffer[0] * 4000;
769 return snprintf(sysfsbuf, PAGE_SIZE, "%u\n", temp);
772 static ssize_t applesmc_show_fan_speed(struct device *dev,
773 struct device_attribute *attr, char *sysfsbuf)
776 unsigned int speed = 0;
779 struct sensor_device_attribute_2 *sensor_attr =
780 to_sensor_dev_attr_2(attr);
782 newkey[0] = fan_speed_keys[sensor_attr->nr][0];
783 newkey[1] = '0' + sensor_attr->index;
784 newkey[2] = fan_speed_keys[sensor_attr->nr][2];
785 newkey[3] = fan_speed_keys[sensor_attr->nr][3];
788 ret = applesmc_read_key(newkey, buffer, 2);
789 speed = ((buffer[0] << 8 | buffer[1]) >> 2);
794 return snprintf(sysfsbuf, PAGE_SIZE, "%u\n", speed);
797 static ssize_t applesmc_store_fan_speed(struct device *dev,
798 struct device_attribute *attr,
799 const char *sysfsbuf, size_t count)
805 struct sensor_device_attribute_2 *sensor_attr =
806 to_sensor_dev_attr_2(attr);
808 speed = simple_strtoul(sysfsbuf, NULL, 10);
810 if (speed > 0x4000) /* Bigger than a 14-bit value */
813 newkey[0] = fan_speed_keys[sensor_attr->nr][0];
814 newkey[1] = '0' + sensor_attr->index;
815 newkey[2] = fan_speed_keys[sensor_attr->nr][2];
816 newkey[3] = fan_speed_keys[sensor_attr->nr][3];
819 buffer[0] = (speed >> 6) & 0xff;
820 buffer[1] = (speed << 2) & 0xff;
821 ret = applesmc_write_key(newkey, buffer, 2);
829 static ssize_t applesmc_show_fan_manual(struct device *dev,
830 struct device_attribute *devattr, char *sysfsbuf)
835 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
837 ret = applesmc_read_key(FANS_MANUAL, buffer, 2);
838 manual = ((buffer[0] << 8 | buffer[1]) >> attr->index) & 0x01;
843 return snprintf(sysfsbuf, PAGE_SIZE, "%d\n", manual);
846 static ssize_t applesmc_store_fan_manual(struct device *dev,
847 struct device_attribute *devattr,
848 const char *sysfsbuf, size_t count)
854 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
856 input = simple_strtoul(sysfsbuf, NULL, 10);
858 ret = applesmc_read_key(FANS_MANUAL, buffer, 2);
859 val = (buffer[0] << 8 | buffer[1]);
864 val = val | (0x01 << attr->index);
866 val = val & ~(0x01 << attr->index);
868 buffer[0] = (val >> 8) & 0xFF;
869 buffer[1] = val & 0xFF;
871 ret = applesmc_write_key(FANS_MANUAL, buffer, 2);
880 static ssize_t applesmc_show_fan_position(struct device *dev,
881 struct device_attribute *attr, char *sysfsbuf)
886 struct sensor_device_attribute_2 *sensor_attr =
887 to_sensor_dev_attr_2(attr);
889 newkey[0] = FAN_POSITION[0];
890 newkey[1] = '0' + sensor_attr->index;
891 newkey[2] = FAN_POSITION[2];
892 newkey[3] = FAN_POSITION[3];
895 ret = applesmc_read_key(newkey, buffer, 16);
901 return snprintf(sysfsbuf, PAGE_SIZE, "%s\n", buffer+4);
904 static ssize_t applesmc_calibrate_show(struct device *dev,
905 struct device_attribute *attr, char *sysfsbuf)
907 return snprintf(sysfsbuf, PAGE_SIZE, "(%d,%d)\n", rest_x, rest_y);
910 static ssize_t applesmc_calibrate_store(struct device *dev,
911 struct device_attribute *attr, const char *sysfsbuf, size_t count)
913 applesmc_calibrate();
918 static void applesmc_backlight_set(struct work_struct *work)
920 applesmc_write_key(BACKLIGHT_KEY, backlight_state, 2);
922 static DECLARE_WORK(backlight_work, &applesmc_backlight_set);
924 static void applesmc_brightness_set(struct led_classdev *led_cdev,
925 enum led_brightness value)
929 backlight_state[0] = value;
930 ret = queue_work(applesmc_led_wq, &backlight_work);
933 printk(KERN_DEBUG "applesmc: work was already on the queue.\n");
936 static ssize_t applesmc_key_count_show(struct device *dev,
937 struct device_attribute *attr, char *sysfsbuf)
943 ret = applesmc_read_key(KEY_COUNT_KEY, buffer, 4);
944 count = ((u32)buffer[0]<<24) + ((u32)buffer[1]<<16) +
945 ((u32)buffer[2]<<8) + buffer[3];
950 return snprintf(sysfsbuf, PAGE_SIZE, "%d\n", count);
953 static ssize_t applesmc_key_at_index_read_show(struct device *dev,
954 struct device_attribute *attr, char *sysfsbuf)
956 const struct applesmc_entry *entry;
959 entry = applesmc_get_entry_by_index(key_at_index);
961 return PTR_ERR(entry);
962 ret = applesmc_read_entry(entry, sysfsbuf, entry->len);
969 static ssize_t applesmc_key_at_index_data_length_show(struct device *dev,
970 struct device_attribute *attr, char *sysfsbuf)
972 const struct applesmc_entry *entry;
974 entry = applesmc_get_entry_by_index(key_at_index);
976 return PTR_ERR(entry);
978 return snprintf(sysfsbuf, PAGE_SIZE, "%d\n", entry->len);
981 static ssize_t applesmc_key_at_index_type_show(struct device *dev,
982 struct device_attribute *attr, char *sysfsbuf)
984 const struct applesmc_entry *entry;
986 entry = applesmc_get_entry_by_index(key_at_index);
988 return PTR_ERR(entry);
990 return snprintf(sysfsbuf, PAGE_SIZE, "%s\n", entry->type);
993 static ssize_t applesmc_key_at_index_name_show(struct device *dev,
994 struct device_attribute *attr, char *sysfsbuf)
996 const struct applesmc_entry *entry;
998 entry = applesmc_get_entry_by_index(key_at_index);
1000 return PTR_ERR(entry);
1002 return snprintf(sysfsbuf, PAGE_SIZE, "%s\n", entry->key);
1005 static ssize_t applesmc_key_at_index_show(struct device *dev,
1006 struct device_attribute *attr, char *sysfsbuf)
1008 return snprintf(sysfsbuf, PAGE_SIZE, "%d\n", key_at_index);
1011 static ssize_t applesmc_key_at_index_store(struct device *dev,
1012 struct device_attribute *attr, const char *sysfsbuf, size_t count)
1014 unsigned long newkey;
1016 if (strict_strtoul(sysfsbuf, 10, &newkey) < 0
1017 || newkey >= smcreg.key_count)
1020 key_at_index = newkey;
1024 static struct led_classdev applesmc_backlight = {
1025 .name = "smc::kbd_backlight",
1026 .default_trigger = "nand-disk",
1027 .brightness_set = applesmc_brightness_set,
1030 static DEVICE_ATTR(name, 0444, applesmc_name_show, NULL);
1032 static DEVICE_ATTR(position, 0444, applesmc_position_show, NULL);
1033 static DEVICE_ATTR(calibrate, 0644,
1034 applesmc_calibrate_show, applesmc_calibrate_store);
1036 static struct attribute *accelerometer_attributes[] = {
1037 &dev_attr_position.attr,
1038 &dev_attr_calibrate.attr,
1042 static const struct attribute_group accelerometer_attributes_group =
1043 { .attrs = accelerometer_attributes };
1045 static DEVICE_ATTR(light, 0444, applesmc_light_show, NULL);
1047 static DEVICE_ATTR(key_count, 0444, applesmc_key_count_show, NULL);
1048 static DEVICE_ATTR(key_at_index, 0644,
1049 applesmc_key_at_index_show, applesmc_key_at_index_store);
1050 static DEVICE_ATTR(key_at_index_name, 0444,
1051 applesmc_key_at_index_name_show, NULL);
1052 static DEVICE_ATTR(key_at_index_type, 0444,
1053 applesmc_key_at_index_type_show, NULL);
1054 static DEVICE_ATTR(key_at_index_data_length, 0444,
1055 applesmc_key_at_index_data_length_show, NULL);
1056 static DEVICE_ATTR(key_at_index_data, 0444,
1057 applesmc_key_at_index_read_show, NULL);
1059 static struct attribute *key_enumeration_attributes[] = {
1060 &dev_attr_key_count.attr,
1061 &dev_attr_key_at_index.attr,
1062 &dev_attr_key_at_index_name.attr,
1063 &dev_attr_key_at_index_type.attr,
1064 &dev_attr_key_at_index_data_length.attr,
1065 &dev_attr_key_at_index_data.attr,
1069 static const struct attribute_group key_enumeration_group =
1070 { .attrs = key_enumeration_attributes };
1073 * Macro defining SENSOR_DEVICE_ATTR for a fan sysfs entries.
1074 * - show actual speed
1075 * - show/store minimum speed
1076 * - show maximum speed
1078 * - show/store target speed
1079 * - show/store manual mode
1081 #define sysfs_fan_speeds_offset(offset) \
1082 static SENSOR_DEVICE_ATTR_2(fan##offset##_input, S_IRUGO, \
1083 applesmc_show_fan_speed, NULL, 0, offset-1); \
1085 static SENSOR_DEVICE_ATTR_2(fan##offset##_min, S_IRUGO | S_IWUSR, \
1086 applesmc_show_fan_speed, applesmc_store_fan_speed, 1, offset-1); \
1088 static SENSOR_DEVICE_ATTR_2(fan##offset##_max, S_IRUGO, \
1089 applesmc_show_fan_speed, NULL, 2, offset-1); \
1091 static SENSOR_DEVICE_ATTR_2(fan##offset##_safe, S_IRUGO, \
1092 applesmc_show_fan_speed, NULL, 3, offset-1); \
1094 static SENSOR_DEVICE_ATTR_2(fan##offset##_output, S_IRUGO | S_IWUSR, \
1095 applesmc_show_fan_speed, applesmc_store_fan_speed, 4, offset-1); \
1097 static SENSOR_DEVICE_ATTR(fan##offset##_manual, S_IRUGO | S_IWUSR, \
1098 applesmc_show_fan_manual, applesmc_store_fan_manual, offset-1); \
1100 static SENSOR_DEVICE_ATTR(fan##offset##_label, S_IRUGO, \
1101 applesmc_show_fan_position, NULL, offset-1); \
1103 static struct attribute *fan##offset##_attributes[] = { \
1104 &sensor_dev_attr_fan##offset##_input.dev_attr.attr, \
1105 &sensor_dev_attr_fan##offset##_min.dev_attr.attr, \
1106 &sensor_dev_attr_fan##offset##_max.dev_attr.attr, \
1107 &sensor_dev_attr_fan##offset##_safe.dev_attr.attr, \
1108 &sensor_dev_attr_fan##offset##_output.dev_attr.attr, \
1109 &sensor_dev_attr_fan##offset##_manual.dev_attr.attr, \
1110 &sensor_dev_attr_fan##offset##_label.dev_attr.attr, \
1115 * Create the needed functions for each fan using the macro defined above
1116 * (4 fans are supported)
1118 sysfs_fan_speeds_offset(1);
1119 sysfs_fan_speeds_offset(2);
1120 sysfs_fan_speeds_offset(3);
1121 sysfs_fan_speeds_offset(4);
1123 static const struct attribute_group fan_attribute_groups[] = {
1124 { .attrs = fan1_attributes },
1125 { .attrs = fan2_attributes },
1126 { .attrs = fan3_attributes },
1127 { .attrs = fan4_attributes },
1130 static struct applesmc_node_group temp_group[] = {
1131 { "temp%d_label", applesmc_show_sensor_label },
1132 { "temp%d_input", applesmc_show_temperature },
1139 * applesmc_destroy_nodes - remove files and free associated memory
1141 static void applesmc_destroy_nodes(struct applesmc_node_group *groups)
1143 struct applesmc_node_group *grp;
1144 struct applesmc_dev_attr *node;
1146 for (grp = groups; grp->nodes; grp++) {
1147 for (node = grp->nodes; node->sda.dev_attr.attr.name; node++)
1148 sysfs_remove_file(&pdev->dev.kobj,
1149 &node->sda.dev_attr.attr);
1156 * applesmc_create_nodes - create a two-dimensional group of sysfs files
1158 static int applesmc_create_nodes(struct applesmc_node_group *groups, int num)
1160 struct applesmc_node_group *grp;
1161 struct applesmc_dev_attr *node;
1162 struct attribute *attr;
1165 for (grp = groups; grp->format; grp++) {
1166 grp->nodes = kcalloc(num + 1, sizeof(*node), GFP_KERNEL);
1171 for (i = 0; i < num; i++) {
1172 node = &grp->nodes[i];
1173 sprintf(node->name, grp->format, i + 1);
1174 node->sda.index = i;
1175 node->sda.dev_attr.show = grp->show;
1176 node->sda.dev_attr.store = grp->store;
1177 attr = &node->sda.dev_attr.attr;
1178 attr->name = node->name;
1179 attr->mode = S_IRUGO | (grp->store ? S_IWUSR : 0);
1180 ret = sysfs_create_file(&pdev->dev.kobj, attr);
1190 applesmc_destroy_nodes(groups);
1194 /* Create accelerometer ressources */
1195 static int applesmc_create_accelerometer(void)
1197 struct input_dev *idev;
1200 ret = sysfs_create_group(&pdev->dev.kobj,
1201 &accelerometer_attributes_group);
1205 applesmc_idev = input_allocate_polled_device();
1206 if (!applesmc_idev) {
1211 applesmc_idev->poll = applesmc_idev_poll;
1212 applesmc_idev->poll_interval = APPLESMC_POLL_INTERVAL;
1214 /* initial calibrate for the input device */
1215 applesmc_calibrate();
1217 /* initialize the input device */
1218 idev = applesmc_idev->input;
1219 idev->name = "applesmc";
1220 idev->id.bustype = BUS_HOST;
1221 idev->dev.parent = &pdev->dev;
1222 idev->evbit[0] = BIT_MASK(EV_ABS);
1223 input_set_abs_params(idev, ABS_X,
1224 -256, 256, APPLESMC_INPUT_FUZZ, APPLESMC_INPUT_FLAT);
1225 input_set_abs_params(idev, ABS_Y,
1226 -256, 256, APPLESMC_INPUT_FUZZ, APPLESMC_INPUT_FLAT);
1228 ret = input_register_polled_device(applesmc_idev);
1235 input_free_polled_device(applesmc_idev);
1238 sysfs_remove_group(&pdev->dev.kobj, &accelerometer_attributes_group);
1241 pr_warn("driver init failed (ret=%d)!\n", ret);
1245 /* Release all ressources used by the accelerometer */
1246 static void applesmc_release_accelerometer(void)
1248 input_unregister_polled_device(applesmc_idev);
1249 input_free_polled_device(applesmc_idev);
1250 sysfs_remove_group(&pdev->dev.kobj, &accelerometer_attributes_group);
1252 static int applesmc_dmi_match(const struct dmi_system_id *id)
1257 /* Note that DMI_MATCH(...,"MacBook") will match "MacBookPro1,1".
1258 * So we need to put "Apple MacBook Pro" before "Apple MacBook". */
1259 static __initdata struct dmi_system_id applesmc_whitelist[] = {
1260 { applesmc_dmi_match, "Apple MacBook Air", {
1261 DMI_MATCH(DMI_BOARD_VENDOR, "Apple"),
1262 DMI_MATCH(DMI_PRODUCT_NAME, "MacBookAir") },
1264 { applesmc_dmi_match, "Apple MacBook Pro", {
1265 DMI_MATCH(DMI_BOARD_VENDOR,"Apple"),
1266 DMI_MATCH(DMI_PRODUCT_NAME,"MacBookPro") },
1268 { applesmc_dmi_match, "Apple MacBook", {
1269 DMI_MATCH(DMI_BOARD_VENDOR,"Apple"),
1270 DMI_MATCH(DMI_PRODUCT_NAME,"MacBook") },
1272 { applesmc_dmi_match, "Apple Macmini", {
1273 DMI_MATCH(DMI_BOARD_VENDOR,"Apple"),
1274 DMI_MATCH(DMI_PRODUCT_NAME,"Macmini") },
1276 { applesmc_dmi_match, "Apple MacPro", {
1277 DMI_MATCH(DMI_BOARD_VENDOR, "Apple"),
1278 DMI_MATCH(DMI_PRODUCT_NAME, "MacPro") },
1280 { applesmc_dmi_match, "Apple iMac", {
1281 DMI_MATCH(DMI_BOARD_VENDOR,"Apple"),
1282 DMI_MATCH(DMI_PRODUCT_NAME,"iMac") },
1287 static int __init applesmc_init(void)
1292 if (!dmi_check_system(applesmc_whitelist)) {
1293 pr_warn("supported laptop not found!\n");
1298 if (!request_region(APPLESMC_DATA_PORT, APPLESMC_NR_PORTS,
1304 ret = platform_driver_register(&applesmc_driver);
1308 pdev = platform_device_register_simple("applesmc", APPLESMC_DATA_PORT,
1311 ret = PTR_ERR(pdev);
1315 /* create register cache */
1316 ret = applesmc_init_smcreg();
1320 ret = sysfs_create_file(&pdev->dev.kobj, &dev_attr_name.attr);
1324 /* Create key enumeration sysfs files */
1325 ret = sysfs_create_group(&pdev->dev.kobj, &key_enumeration_group);
1329 /* create fan files */
1330 count = applesmc_get_fan_count();
1332 pr_err("Cannot get the number of fans\n");
1334 pr_info("%d fans found\n", count);
1338 pr_warn("A maximum of 4 fans are supported by this driver\n");
1341 while (fans_handled < count) {
1342 ret = sysfs_create_group(&pdev->dev.kobj,
1343 &fan_attribute_groups[fans_handled]);
1349 ret = applesmc_create_nodes(temp_group, smcreg.temp_count);
1353 if (smcreg.has_accelerometer) {
1354 ret = applesmc_create_accelerometer();
1356 goto out_temperature;
1359 if (smcreg.num_light_sensors) {
1360 /* Add light sensor file */
1361 ret = sysfs_create_file(&pdev->dev.kobj, &dev_attr_light.attr);
1363 goto out_accelerometer;
1366 if (smcreg.has_key_backlight) {
1367 /* Create the workqueue */
1368 applesmc_led_wq = create_singlethread_workqueue("applesmc-led");
1369 if (!applesmc_led_wq) {
1371 goto out_light_sysfs;
1374 /* register as a led device */
1375 ret = led_classdev_register(&pdev->dev, &applesmc_backlight);
1380 hwmon_dev = hwmon_device_register(&pdev->dev);
1381 if (IS_ERR(hwmon_dev)) {
1382 ret = PTR_ERR(hwmon_dev);
1383 goto out_light_ledclass;
1386 pr_info("driver successfully loaded\n");
1391 if (smcreg.has_key_backlight)
1392 led_classdev_unregister(&applesmc_backlight);
1394 if (smcreg.has_key_backlight)
1395 destroy_workqueue(applesmc_led_wq);
1397 if (smcreg.num_light_sensors)
1398 sysfs_remove_file(&pdev->dev.kobj, &dev_attr_light.attr);
1400 if (smcreg.has_accelerometer)
1401 applesmc_release_accelerometer();
1403 applesmc_destroy_nodes(temp_group);
1405 while (fans_handled)
1406 sysfs_remove_group(&pdev->dev.kobj,
1407 &fan_attribute_groups[--fans_handled]);
1408 sysfs_remove_group(&pdev->dev.kobj, &key_enumeration_group);
1410 sysfs_remove_file(&pdev->dev.kobj, &dev_attr_name.attr);
1412 applesmc_destroy_smcreg();
1414 platform_device_unregister(pdev);
1416 platform_driver_unregister(&applesmc_driver);
1418 release_region(APPLESMC_DATA_PORT, APPLESMC_NR_PORTS);
1420 pr_warn("driver init failed (ret=%d)!\n", ret);
1424 static void __exit applesmc_exit(void)
1426 hwmon_device_unregister(hwmon_dev);
1427 if (smcreg.has_key_backlight) {
1428 led_classdev_unregister(&applesmc_backlight);
1429 destroy_workqueue(applesmc_led_wq);
1431 if (smcreg.num_light_sensors)
1432 sysfs_remove_file(&pdev->dev.kobj, &dev_attr_light.attr);
1433 if (smcreg.has_accelerometer)
1434 applesmc_release_accelerometer();
1435 applesmc_destroy_nodes(temp_group);
1436 while (fans_handled)
1437 sysfs_remove_group(&pdev->dev.kobj,
1438 &fan_attribute_groups[--fans_handled]);
1439 sysfs_remove_group(&pdev->dev.kobj, &key_enumeration_group);
1440 sysfs_remove_file(&pdev->dev.kobj, &dev_attr_name.attr);
1441 applesmc_destroy_smcreg();
1442 platform_device_unregister(pdev);
1443 platform_driver_unregister(&applesmc_driver);
1444 release_region(APPLESMC_DATA_PORT, APPLESMC_NR_PORTS);
1446 pr_info("driver unloaded\n");
1449 module_init(applesmc_init);
1450 module_exit(applesmc_exit);
1452 MODULE_AUTHOR("Nicolas Boichat");
1453 MODULE_DESCRIPTION("Apple SMC");
1454 MODULE_LICENSE("GPL v2");
1455 MODULE_DEVICE_TABLE(dmi, applesmc_whitelist);