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>
7 * Copyright (C) 2010 Henrik Rydberg <rydberg@euromail.se>
9 * Based on hdaps.c driver:
10 * Copyright (C) 2005 Robert Love <rml@novell.com>
11 * Copyright (C) 2005 Jesper Juhl <jj@chaosbits.net>
13 * Fan control based on smcFanControl:
14 * Copyright (C) 2006 Hendrik Holtmann <holtmann@mac.com>
16 * This program is free software; you can redistribute it and/or modify it
17 * under the terms of the GNU General Public License v2 as published by the
18 * Free Software Foundation.
20 * This program is distributed in the hope that it will be useful, but WITHOUT
21 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
22 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
25 * You should have received a copy of the GNU General Public License along with
26 * this program; if not, write to the Free Software Foundation, Inc.,
27 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
30 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
32 #include <linux/delay.h>
33 #include <linux/platform_device.h>
34 #include <linux/input-polldev.h>
35 #include <linux/kernel.h>
36 #include <linux/slab.h>
37 #include <linux/module.h>
38 #include <linux/timer.h>
39 #include <linux/dmi.h>
40 #include <linux/mutex.h>
41 #include <linux/hwmon-sysfs.h>
43 #include <linux/leds.h>
44 #include <linux/hwmon.h>
45 #include <linux/workqueue.h>
47 /* data port used by Apple SMC */
48 #define APPLESMC_DATA_PORT 0x300
49 /* command/status port used by Apple SMC */
50 #define APPLESMC_CMD_PORT 0x304
52 #define APPLESMC_NR_PORTS 32 /* 0x300-0x31f */
54 #define APPLESMC_MAX_DATA_LENGTH 32
56 /* wait up to 32 ms for a status change. */
57 #define APPLESMC_MIN_WAIT 0x0010
58 #define APPLESMC_RETRY_WAIT 0x0100
59 #define APPLESMC_MAX_WAIT 0x8000
61 #define APPLESMC_READ_CMD 0x10
62 #define APPLESMC_WRITE_CMD 0x11
63 #define APPLESMC_GET_KEY_BY_INDEX_CMD 0x12
64 #define APPLESMC_GET_KEY_TYPE_CMD 0x13
66 #define KEY_COUNT_KEY "#KEY" /* r-o ui32 */
68 #define LIGHT_SENSOR_LEFT_KEY "ALV0" /* r-o {alv (6-10 bytes) */
69 #define LIGHT_SENSOR_RIGHT_KEY "ALV1" /* r-o {alv (6-10 bytes) */
70 #define BACKLIGHT_KEY "LKSB" /* w-o {lkb (2 bytes) */
72 #define CLAMSHELL_KEY "MSLD" /* r-o ui8 (unused) */
74 #define MOTION_SENSOR_X_KEY "MO_X" /* r-o sp78 (2 bytes) */
75 #define MOTION_SENSOR_Y_KEY "MO_Y" /* r-o sp78 (2 bytes) */
76 #define MOTION_SENSOR_Z_KEY "MO_Z" /* r-o sp78 (2 bytes) */
77 #define MOTION_SENSOR_KEY "MOCN" /* r/w ui16 */
79 #define FANS_COUNT "FNum" /* r-o ui8 */
80 #define FANS_MANUAL "FS! " /* r-w ui16 */
81 #define FAN_ID_FMT "F%dID" /* r-o char[16] */
83 #define TEMP_SENSOR_TYPE "sp78"
85 /* List of keys used to read/write fan speeds */
86 static const char *const fan_speed_fmt[] = {
87 "F%dAc", /* actual speed */
88 "F%dMn", /* minimum speed (rw) */
89 "F%dMx", /* maximum speed */
90 "F%dSf", /* safe speed - not all models */
91 "F%dTg", /* target speed (manual: rw) */
94 #define INIT_TIMEOUT_MSECS 5000 /* wait up to 5s for device init ... */
95 #define INIT_WAIT_MSECS 50 /* ... in 50ms increments */
97 #define APPLESMC_POLL_INTERVAL 50 /* msecs */
98 #define APPLESMC_INPUT_FUZZ 4 /* input event threshold */
99 #define APPLESMC_INPUT_FLAT 4
101 #define to_index(attr) (to_sensor_dev_attr(attr)->index & 0xffff)
102 #define to_option(attr) (to_sensor_dev_attr(attr)->index >> 16)
104 /* Dynamic device node attributes */
105 struct applesmc_dev_attr {
106 struct sensor_device_attribute sda; /* hwmon attributes */
107 char name[32]; /* room for node file name */
110 /* Dynamic device node group */
111 struct applesmc_node_group {
112 char *format; /* format string */
113 void *show; /* show function */
114 void *store; /* store function */
115 int option; /* function argument */
116 struct applesmc_dev_attr *nodes; /* dynamic node array */
119 /* AppleSMC entry - cached register information */
120 struct applesmc_entry {
121 char key[5]; /* four-letter key code */
122 u8 valid; /* set when entry is successfully read once */
123 u8 len; /* bounded by APPLESMC_MAX_DATA_LENGTH */
124 char type[5]; /* four-letter type code */
125 u8 flags; /* 0x10: func; 0x40: write; 0x80: read */
128 /* Register lookup and registers common to all SMCs */
129 static struct applesmc_registers {
130 struct mutex mutex; /* register read/write mutex */
131 unsigned int key_count; /* number of SMC registers */
132 unsigned int fan_count; /* number of fans */
133 unsigned int temp_count; /* number of temperature registers */
134 unsigned int temp_begin; /* temperature lower index bound */
135 unsigned int temp_end; /* temperature upper index bound */
136 unsigned int index_count; /* size of temperature index array */
137 int num_light_sensors; /* number of light sensors */
138 bool has_accelerometer; /* has motion sensor */
139 bool has_key_backlight; /* has keyboard backlight */
140 bool init_complete; /* true when fully initialized */
141 struct applesmc_entry *cache; /* cached key entries */
142 const char **index; /* temperature key index */
144 .mutex = __MUTEX_INITIALIZER(smcreg.mutex),
147 static const int debug;
148 static struct platform_device *pdev;
151 static u8 backlight_state[2];
153 static struct device *hwmon_dev;
154 static struct input_polled_dev *applesmc_idev;
157 * Last index written to key_at_index sysfs file, and value to use for all other
158 * key_at_index_* sysfs files.
160 static unsigned int key_at_index;
162 static struct workqueue_struct *applesmc_led_wq;
165 * wait_read - Wait for a byte to appear on SMC port. Callers must
166 * hold applesmc_lock.
168 static int wait_read(void)
172 for (us = APPLESMC_MIN_WAIT; us < APPLESMC_MAX_WAIT; us <<= 1) {
174 status = inb(APPLESMC_CMD_PORT);
175 /* read: wait for smc to settle */
180 pr_warn("wait_read() fail: 0x%02x\n", status);
185 * send_byte - Write to SMC port, retrying when necessary. Callers
186 * must hold applesmc_lock.
188 static int send_byte(u8 cmd, u16 port)
194 for (us = APPLESMC_MIN_WAIT; us < APPLESMC_MAX_WAIT; us <<= 1) {
196 status = inb(APPLESMC_CMD_PORT);
197 /* write: wait for smc to settle */
200 /* ready: cmd accepted, return */
203 /* timeout: give up */
204 if (us << 1 == APPLESMC_MAX_WAIT)
206 /* busy: long wait and resend */
207 udelay(APPLESMC_RETRY_WAIT);
211 pr_warn("send_byte(0x%02x, 0x%04x) fail: 0x%02x\n", cmd, port, status);
215 static int send_command(u8 cmd)
217 return send_byte(cmd, APPLESMC_CMD_PORT);
220 static int send_argument(const char *key)
224 for (i = 0; i < 4; i++)
225 if (send_byte(key[i], APPLESMC_DATA_PORT))
230 static int read_smc(u8 cmd, const char *key, u8 *buffer, u8 len)
234 if (send_command(cmd) || send_argument(key)) {
235 pr_warn("%.4s: read arg fail\n", key);
239 if (send_byte(len, APPLESMC_DATA_PORT)) {
240 pr_warn("%.4s: read len fail\n", key);
244 for (i = 0; i < len; i++) {
246 pr_warn("%.4s: read data[%d] fail\n", key, i);
249 buffer[i] = inb(APPLESMC_DATA_PORT);
255 static int write_smc(u8 cmd, const char *key, const u8 *buffer, u8 len)
259 if (send_command(cmd) || send_argument(key)) {
260 pr_warn("%s: write arg fail\n", key);
264 if (send_byte(len, APPLESMC_DATA_PORT)) {
265 pr_warn("%.4s: write len fail\n", key);
269 for (i = 0; i < len; i++) {
270 if (send_byte(buffer[i], APPLESMC_DATA_PORT)) {
271 pr_warn("%s: write data fail\n", key);
279 static int read_register_count(unsigned int *count)
284 ret = read_smc(APPLESMC_READ_CMD, KEY_COUNT_KEY, (u8 *)&be, 4);
288 *count = be32_to_cpu(be);
295 * Returns zero on success or a negative error on failure.
296 * All functions below are concurrency safe - callers should NOT hold lock.
299 static int applesmc_read_entry(const struct applesmc_entry *entry,
304 if (entry->len != len)
306 mutex_lock(&smcreg.mutex);
307 ret = read_smc(APPLESMC_READ_CMD, entry->key, buf, len);
308 mutex_unlock(&smcreg.mutex);
313 static int applesmc_write_entry(const struct applesmc_entry *entry,
314 const u8 *buf, u8 len)
318 if (entry->len != len)
320 mutex_lock(&smcreg.mutex);
321 ret = write_smc(APPLESMC_WRITE_CMD, entry->key, buf, len);
322 mutex_unlock(&smcreg.mutex);
326 static const struct applesmc_entry *applesmc_get_entry_by_index(int index)
328 struct applesmc_entry *cache = &smcreg.cache[index];
336 mutex_lock(&smcreg.mutex);
340 be = cpu_to_be32(index);
341 ret = read_smc(APPLESMC_GET_KEY_BY_INDEX_CMD, (u8 *)&be, key, 4);
344 ret = read_smc(APPLESMC_GET_KEY_TYPE_CMD, key, info, 6);
348 memcpy(cache->key, key, 4);
349 cache->len = info[0];
350 memcpy(cache->type, &info[1], 4);
351 cache->flags = info[5];
355 mutex_unlock(&smcreg.mutex);
361 static int applesmc_get_lower_bound(unsigned int *lo, const char *key)
363 int begin = 0, end = smcreg.key_count;
364 const struct applesmc_entry *entry;
366 while (begin != end) {
367 int middle = begin + (end - begin) / 2;
368 entry = applesmc_get_entry_by_index(middle);
371 return PTR_ERR(entry);
373 if (strcmp(entry->key, key) < 0)
383 static int applesmc_get_upper_bound(unsigned int *hi, const char *key)
385 int begin = 0, end = smcreg.key_count;
386 const struct applesmc_entry *entry;
388 while (begin != end) {
389 int middle = begin + (end - begin) / 2;
390 entry = applesmc_get_entry_by_index(middle);
392 *hi = smcreg.key_count;
393 return PTR_ERR(entry);
395 if (strcmp(key, entry->key) < 0)
405 static const struct applesmc_entry *applesmc_get_entry_by_key(const char *key)
410 ret = applesmc_get_lower_bound(&begin, key);
413 ret = applesmc_get_upper_bound(&end, key);
416 if (end - begin != 1)
417 return ERR_PTR(-EINVAL);
419 return applesmc_get_entry_by_index(begin);
422 static int applesmc_read_key(const char *key, u8 *buffer, u8 len)
424 const struct applesmc_entry *entry;
426 entry = applesmc_get_entry_by_key(key);
428 return PTR_ERR(entry);
430 return applesmc_read_entry(entry, buffer, len);
433 static int applesmc_write_key(const char *key, const u8 *buffer, u8 len)
435 const struct applesmc_entry *entry;
437 entry = applesmc_get_entry_by_key(key);
439 return PTR_ERR(entry);
441 return applesmc_write_entry(entry, buffer, len);
444 static int applesmc_has_key(const char *key, bool *value)
446 const struct applesmc_entry *entry;
448 entry = applesmc_get_entry_by_key(key);
449 if (IS_ERR(entry) && PTR_ERR(entry) != -EINVAL)
450 return PTR_ERR(entry);
452 *value = !IS_ERR(entry);
457 * applesmc_read_s16 - Read 16-bit signed big endian register
459 static int applesmc_read_s16(const char *key, s16 *value)
464 ret = applesmc_read_key(key, buffer, 2);
468 *value = ((s16)buffer[0] << 8) | buffer[1];
473 * applesmc_device_init - initialize the accelerometer. Can sleep.
475 static void applesmc_device_init(void)
480 if (!smcreg.has_accelerometer)
483 for (total = INIT_TIMEOUT_MSECS; total > 0; total -= INIT_WAIT_MSECS) {
484 if (!applesmc_read_key(MOTION_SENSOR_KEY, buffer, 2) &&
485 (buffer[0] != 0x00 || buffer[1] != 0x00))
489 applesmc_write_key(MOTION_SENSOR_KEY, buffer, 2);
490 msleep(INIT_WAIT_MSECS);
493 pr_warn("failed to init the device\n");
496 static int applesmc_init_index(struct applesmc_registers *s)
498 const struct applesmc_entry *entry;
504 s->index = kcalloc(s->temp_count, sizeof(s->index[0]), GFP_KERNEL);
508 for (i = s->temp_begin; i < s->temp_end; i++) {
509 entry = applesmc_get_entry_by_index(i);
512 if (strcmp(entry->type, TEMP_SENSOR_TYPE))
514 s->index[s->index_count++] = entry->key;
521 * applesmc_init_smcreg_try - Try to initialize register cache. Idempotent.
523 static int applesmc_init_smcreg_try(void)
525 struct applesmc_registers *s = &smcreg;
526 bool left_light_sensor, right_light_sensor;
530 if (s->init_complete)
533 ret = read_register_count(&s->key_count);
538 s->cache = kcalloc(s->key_count, sizeof(*s->cache), GFP_KERNEL);
542 ret = applesmc_read_key(FANS_COUNT, tmp, 1);
545 s->fan_count = tmp[0];
547 ret = applesmc_get_lower_bound(&s->temp_begin, "T");
550 ret = applesmc_get_lower_bound(&s->temp_end, "U");
553 s->temp_count = s->temp_end - s->temp_begin;
555 ret = applesmc_init_index(s);
559 ret = applesmc_has_key(LIGHT_SENSOR_LEFT_KEY, &left_light_sensor);
562 ret = applesmc_has_key(LIGHT_SENSOR_RIGHT_KEY, &right_light_sensor);
565 ret = applesmc_has_key(MOTION_SENSOR_KEY, &s->has_accelerometer);
568 ret = applesmc_has_key(BACKLIGHT_KEY, &s->has_key_backlight);
572 s->num_light_sensors = left_light_sensor + right_light_sensor;
573 s->init_complete = true;
575 pr_info("key=%d fan=%d temp=%d index=%d acc=%d lux=%d kbd=%d\n",
576 s->key_count, s->fan_count, s->temp_count, s->index_count,
577 s->has_accelerometer,
578 s->num_light_sensors,
579 s->has_key_backlight);
584 static void applesmc_destroy_smcreg(void)
590 smcreg.init_complete = false;
594 * applesmc_init_smcreg - Initialize register cache.
596 * Retries until initialization is successful, or the operation times out.
599 static int applesmc_init_smcreg(void)
603 for (ms = 0; ms < INIT_TIMEOUT_MSECS; ms += INIT_WAIT_MSECS) {
604 ret = applesmc_init_smcreg_try();
607 pr_info("init_smcreg() took %d ms\n", ms);
610 msleep(INIT_WAIT_MSECS);
613 applesmc_destroy_smcreg();
618 /* Device model stuff */
619 static int applesmc_probe(struct platform_device *dev)
623 ret = applesmc_init_smcreg();
627 applesmc_device_init();
632 /* Synchronize device with memorized backlight state */
633 static int applesmc_pm_resume(struct device *dev)
635 if (smcreg.has_key_backlight)
636 applesmc_write_key(BACKLIGHT_KEY, backlight_state, 2);
640 /* Reinitialize device on resume from hibernation */
641 static int applesmc_pm_restore(struct device *dev)
643 applesmc_device_init();
644 return applesmc_pm_resume(dev);
647 static const struct dev_pm_ops applesmc_pm_ops = {
648 .resume = applesmc_pm_resume,
649 .restore = applesmc_pm_restore,
652 static struct platform_driver applesmc_driver = {
653 .probe = applesmc_probe,
656 .owner = THIS_MODULE,
657 .pm = &applesmc_pm_ops,
662 * applesmc_calibrate - Set our "resting" values. Callers must
663 * hold applesmc_lock.
665 static void applesmc_calibrate(void)
667 applesmc_read_s16(MOTION_SENSOR_X_KEY, &rest_x);
668 applesmc_read_s16(MOTION_SENSOR_Y_KEY, &rest_y);
672 static void applesmc_idev_poll(struct input_polled_dev *dev)
674 struct input_dev *idev = dev->input;
677 if (applesmc_read_s16(MOTION_SENSOR_X_KEY, &x))
679 if (applesmc_read_s16(MOTION_SENSOR_Y_KEY, &y))
683 input_report_abs(idev, ABS_X, x - rest_x);
684 input_report_abs(idev, ABS_Y, y - rest_y);
690 static ssize_t applesmc_name_show(struct device *dev,
691 struct device_attribute *attr, char *buf)
693 return snprintf(buf, PAGE_SIZE, "applesmc\n");
696 static ssize_t applesmc_position_show(struct device *dev,
697 struct device_attribute *attr, char *buf)
702 ret = applesmc_read_s16(MOTION_SENSOR_X_KEY, &x);
705 ret = applesmc_read_s16(MOTION_SENSOR_Y_KEY, &y);
708 ret = applesmc_read_s16(MOTION_SENSOR_Z_KEY, &z);
716 return snprintf(buf, PAGE_SIZE, "(%d,%d,%d)\n", x, y, z);
719 static ssize_t applesmc_light_show(struct device *dev,
720 struct device_attribute *attr, char *sysfsbuf)
722 const struct applesmc_entry *entry;
723 static int data_length;
725 u8 left = 0, right = 0;
729 entry = applesmc_get_entry_by_key(LIGHT_SENSOR_LEFT_KEY);
731 return PTR_ERR(entry);
734 data_length = entry->len;
735 pr_info("light sensor data length set to %d\n", data_length);
738 ret = applesmc_read_key(LIGHT_SENSOR_LEFT_KEY, buffer, data_length);
739 /* newer macbooks report a single 10-bit bigendian value */
740 if (data_length == 10) {
741 left = be16_to_cpu(*(__be16 *)(buffer + 6)) >> 2;
747 ret = applesmc_read_key(LIGHT_SENSOR_RIGHT_KEY, buffer, data_length);
754 return snprintf(sysfsbuf, PAGE_SIZE, "(%d,%d)\n", left, right);
757 /* Displays sensor key as label */
758 static ssize_t applesmc_show_sensor_label(struct device *dev,
759 struct device_attribute *devattr, char *sysfsbuf)
761 const char *key = smcreg.index[to_index(devattr)];
763 return snprintf(sysfsbuf, PAGE_SIZE, "%s\n", key);
766 /* Displays degree Celsius * 1000 */
767 static ssize_t applesmc_show_temperature(struct device *dev,
768 struct device_attribute *devattr, char *sysfsbuf)
770 const char *key = smcreg.index[to_index(devattr)];
775 ret = applesmc_read_s16(key, &value);
779 temp = 250 * (value >> 6);
781 return snprintf(sysfsbuf, PAGE_SIZE, "%d\n", temp);
784 static ssize_t applesmc_show_fan_speed(struct device *dev,
785 struct device_attribute *attr, char *sysfsbuf)
788 unsigned int speed = 0;
792 sprintf(newkey, fan_speed_fmt[to_option(attr)], to_index(attr));
794 ret = applesmc_read_key(newkey, buffer, 2);
795 speed = ((buffer[0] << 8 | buffer[1]) >> 2);
800 return snprintf(sysfsbuf, PAGE_SIZE, "%u\n", speed);
803 static ssize_t applesmc_store_fan_speed(struct device *dev,
804 struct device_attribute *attr,
805 const char *sysfsbuf, size_t count)
812 if (kstrtoul(sysfsbuf, 10, &speed) < 0 || speed >= 0x4000)
813 return -EINVAL; /* Bigger than a 14-bit value */
815 sprintf(newkey, fan_speed_fmt[to_option(attr)], to_index(attr));
817 buffer[0] = (speed >> 6) & 0xff;
818 buffer[1] = (speed << 2) & 0xff;
819 ret = applesmc_write_key(newkey, buffer, 2);
827 static ssize_t applesmc_show_fan_manual(struct device *dev,
828 struct device_attribute *attr, char *sysfsbuf)
834 ret = applesmc_read_key(FANS_MANUAL, buffer, 2);
835 manual = ((buffer[0] << 8 | buffer[1]) >> to_index(attr)) & 0x01;
840 return snprintf(sysfsbuf, PAGE_SIZE, "%d\n", manual);
843 static ssize_t applesmc_store_fan_manual(struct device *dev,
844 struct device_attribute *attr,
845 const char *sysfsbuf, size_t count)
852 if (kstrtoul(sysfsbuf, 10, &input) < 0)
855 ret = applesmc_read_key(FANS_MANUAL, buffer, 2);
856 val = (buffer[0] << 8 | buffer[1]);
861 val = val | (0x01 << to_index(attr));
863 val = val & ~(0x01 << to_index(attr));
865 buffer[0] = (val >> 8) & 0xFF;
866 buffer[1] = val & 0xFF;
868 ret = applesmc_write_key(FANS_MANUAL, buffer, 2);
877 static ssize_t applesmc_show_fan_position(struct device *dev,
878 struct device_attribute *attr, char *sysfsbuf)
884 sprintf(newkey, FAN_ID_FMT, to_index(attr));
886 ret = applesmc_read_key(newkey, buffer, 16);
892 return snprintf(sysfsbuf, PAGE_SIZE, "%s\n", buffer+4);
895 static ssize_t applesmc_calibrate_show(struct device *dev,
896 struct device_attribute *attr, char *sysfsbuf)
898 return snprintf(sysfsbuf, PAGE_SIZE, "(%d,%d)\n", rest_x, rest_y);
901 static ssize_t applesmc_calibrate_store(struct device *dev,
902 struct device_attribute *attr, const char *sysfsbuf, size_t count)
904 applesmc_calibrate();
909 static void applesmc_backlight_set(struct work_struct *work)
911 applesmc_write_key(BACKLIGHT_KEY, backlight_state, 2);
913 static DECLARE_WORK(backlight_work, &applesmc_backlight_set);
915 static void applesmc_brightness_set(struct led_classdev *led_cdev,
916 enum led_brightness value)
920 backlight_state[0] = value;
921 ret = queue_work(applesmc_led_wq, &backlight_work);
924 printk(KERN_DEBUG "applesmc: work was already on the queue.\n");
927 static ssize_t applesmc_key_count_show(struct device *dev,
928 struct device_attribute *attr, char *sysfsbuf)
934 ret = applesmc_read_key(KEY_COUNT_KEY, buffer, 4);
935 count = ((u32)buffer[0]<<24) + ((u32)buffer[1]<<16) +
936 ((u32)buffer[2]<<8) + buffer[3];
941 return snprintf(sysfsbuf, PAGE_SIZE, "%d\n", count);
944 static ssize_t applesmc_key_at_index_read_show(struct device *dev,
945 struct device_attribute *attr, char *sysfsbuf)
947 const struct applesmc_entry *entry;
950 entry = applesmc_get_entry_by_index(key_at_index);
952 return PTR_ERR(entry);
953 ret = applesmc_read_entry(entry, sysfsbuf, entry->len);
960 static ssize_t applesmc_key_at_index_data_length_show(struct device *dev,
961 struct device_attribute *attr, char *sysfsbuf)
963 const struct applesmc_entry *entry;
965 entry = applesmc_get_entry_by_index(key_at_index);
967 return PTR_ERR(entry);
969 return snprintf(sysfsbuf, PAGE_SIZE, "%d\n", entry->len);
972 static ssize_t applesmc_key_at_index_type_show(struct device *dev,
973 struct device_attribute *attr, char *sysfsbuf)
975 const struct applesmc_entry *entry;
977 entry = applesmc_get_entry_by_index(key_at_index);
979 return PTR_ERR(entry);
981 return snprintf(sysfsbuf, PAGE_SIZE, "%s\n", entry->type);
984 static ssize_t applesmc_key_at_index_name_show(struct device *dev,
985 struct device_attribute *attr, char *sysfsbuf)
987 const struct applesmc_entry *entry;
989 entry = applesmc_get_entry_by_index(key_at_index);
991 return PTR_ERR(entry);
993 return snprintf(sysfsbuf, PAGE_SIZE, "%s\n", entry->key);
996 static ssize_t applesmc_key_at_index_show(struct device *dev,
997 struct device_attribute *attr, char *sysfsbuf)
999 return snprintf(sysfsbuf, PAGE_SIZE, "%d\n", key_at_index);
1002 static ssize_t applesmc_key_at_index_store(struct device *dev,
1003 struct device_attribute *attr, const char *sysfsbuf, size_t count)
1005 unsigned long newkey;
1007 if (kstrtoul(sysfsbuf, 10, &newkey) < 0
1008 || newkey >= smcreg.key_count)
1011 key_at_index = newkey;
1015 static struct led_classdev applesmc_backlight = {
1016 .name = "smc::kbd_backlight",
1017 .default_trigger = "nand-disk",
1018 .brightness_set = applesmc_brightness_set,
1021 static struct applesmc_node_group info_group[] = {
1022 { "name", applesmc_name_show },
1023 { "key_count", applesmc_key_count_show },
1024 { "key_at_index", applesmc_key_at_index_show, applesmc_key_at_index_store },
1025 { "key_at_index_name", applesmc_key_at_index_name_show },
1026 { "key_at_index_type", applesmc_key_at_index_type_show },
1027 { "key_at_index_data_length", applesmc_key_at_index_data_length_show },
1028 { "key_at_index_data", applesmc_key_at_index_read_show },
1032 static struct applesmc_node_group accelerometer_group[] = {
1033 { "position", applesmc_position_show },
1034 { "calibrate", applesmc_calibrate_show, applesmc_calibrate_store },
1038 static struct applesmc_node_group light_sensor_group[] = {
1039 { "light", applesmc_light_show },
1043 static struct applesmc_node_group fan_group[] = {
1044 { "fan%d_label", applesmc_show_fan_position },
1045 { "fan%d_input", applesmc_show_fan_speed, NULL, 0 },
1046 { "fan%d_min", applesmc_show_fan_speed, applesmc_store_fan_speed, 1 },
1047 { "fan%d_max", applesmc_show_fan_speed, NULL, 2 },
1048 { "fan%d_safe", applesmc_show_fan_speed, NULL, 3 },
1049 { "fan%d_output", applesmc_show_fan_speed, applesmc_store_fan_speed, 4 },
1050 { "fan%d_manual", applesmc_show_fan_manual, applesmc_store_fan_manual },
1054 static struct applesmc_node_group temp_group[] = {
1055 { "temp%d_label", applesmc_show_sensor_label },
1056 { "temp%d_input", applesmc_show_temperature },
1063 * applesmc_destroy_nodes - remove files and free associated memory
1065 static void applesmc_destroy_nodes(struct applesmc_node_group *groups)
1067 struct applesmc_node_group *grp;
1068 struct applesmc_dev_attr *node;
1070 for (grp = groups; grp->nodes; grp++) {
1071 for (node = grp->nodes; node->sda.dev_attr.attr.name; node++)
1072 sysfs_remove_file(&pdev->dev.kobj,
1073 &node->sda.dev_attr.attr);
1080 * applesmc_create_nodes - create a two-dimensional group of sysfs files
1082 static int applesmc_create_nodes(struct applesmc_node_group *groups, int num)
1084 struct applesmc_node_group *grp;
1085 struct applesmc_dev_attr *node;
1086 struct attribute *attr;
1089 for (grp = groups; grp->format; grp++) {
1090 grp->nodes = kcalloc(num + 1, sizeof(*node), GFP_KERNEL);
1095 for (i = 0; i < num; i++) {
1096 node = &grp->nodes[i];
1097 sprintf(node->name, grp->format, i + 1);
1098 node->sda.index = (grp->option << 16) | (i & 0xffff);
1099 node->sda.dev_attr.show = grp->show;
1100 node->sda.dev_attr.store = grp->store;
1101 attr = &node->sda.dev_attr.attr;
1102 sysfs_attr_init(attr);
1103 attr->name = node->name;
1104 attr->mode = S_IRUGO | (grp->store ? S_IWUSR : 0);
1105 ret = sysfs_create_file(&pdev->dev.kobj, attr);
1115 applesmc_destroy_nodes(groups);
1119 /* Create accelerometer ressources */
1120 static int applesmc_create_accelerometer(void)
1122 struct input_dev *idev;
1125 if (!smcreg.has_accelerometer)
1128 ret = applesmc_create_nodes(accelerometer_group, 1);
1132 applesmc_idev = input_allocate_polled_device();
1133 if (!applesmc_idev) {
1138 applesmc_idev->poll = applesmc_idev_poll;
1139 applesmc_idev->poll_interval = APPLESMC_POLL_INTERVAL;
1141 /* initial calibrate for the input device */
1142 applesmc_calibrate();
1144 /* initialize the input device */
1145 idev = applesmc_idev->input;
1146 idev->name = "applesmc";
1147 idev->id.bustype = BUS_HOST;
1148 idev->dev.parent = &pdev->dev;
1149 idev->evbit[0] = BIT_MASK(EV_ABS);
1150 input_set_abs_params(idev, ABS_X,
1151 -256, 256, APPLESMC_INPUT_FUZZ, APPLESMC_INPUT_FLAT);
1152 input_set_abs_params(idev, ABS_Y,
1153 -256, 256, APPLESMC_INPUT_FUZZ, APPLESMC_INPUT_FLAT);
1155 ret = input_register_polled_device(applesmc_idev);
1162 input_free_polled_device(applesmc_idev);
1165 applesmc_destroy_nodes(accelerometer_group);
1168 pr_warn("driver init failed (ret=%d)!\n", ret);
1172 /* Release all ressources used by the accelerometer */
1173 static void applesmc_release_accelerometer(void)
1175 if (!smcreg.has_accelerometer)
1177 input_unregister_polled_device(applesmc_idev);
1178 input_free_polled_device(applesmc_idev);
1179 applesmc_destroy_nodes(accelerometer_group);
1182 static int applesmc_create_light_sensor(void)
1184 if (!smcreg.num_light_sensors)
1186 return applesmc_create_nodes(light_sensor_group, 1);
1189 static void applesmc_release_light_sensor(void)
1191 if (!smcreg.num_light_sensors)
1193 applesmc_destroy_nodes(light_sensor_group);
1196 static int applesmc_create_key_backlight(void)
1198 if (!smcreg.has_key_backlight)
1200 applesmc_led_wq = create_singlethread_workqueue("applesmc-led");
1201 if (!applesmc_led_wq)
1203 return led_classdev_register(&pdev->dev, &applesmc_backlight);
1206 static void applesmc_release_key_backlight(void)
1208 if (!smcreg.has_key_backlight)
1210 led_classdev_unregister(&applesmc_backlight);
1211 destroy_workqueue(applesmc_led_wq);
1214 static int applesmc_dmi_match(const struct dmi_system_id *id)
1220 * Note that DMI_MATCH(...,"MacBook") will match "MacBookPro1,1".
1221 * So we need to put "Apple MacBook Pro" before "Apple MacBook".
1223 static __initdata struct dmi_system_id applesmc_whitelist[] = {
1224 { applesmc_dmi_match, "Apple MacBook Air", {
1225 DMI_MATCH(DMI_BOARD_VENDOR, "Apple"),
1226 DMI_MATCH(DMI_PRODUCT_NAME, "MacBookAir") },
1228 { applesmc_dmi_match, "Apple MacBook Pro", {
1229 DMI_MATCH(DMI_BOARD_VENDOR, "Apple"),
1230 DMI_MATCH(DMI_PRODUCT_NAME, "MacBookPro") },
1232 { applesmc_dmi_match, "Apple MacBook", {
1233 DMI_MATCH(DMI_BOARD_VENDOR, "Apple"),
1234 DMI_MATCH(DMI_PRODUCT_NAME, "MacBook") },
1236 { applesmc_dmi_match, "Apple Macmini", {
1237 DMI_MATCH(DMI_BOARD_VENDOR, "Apple"),
1238 DMI_MATCH(DMI_PRODUCT_NAME, "Macmini") },
1240 { applesmc_dmi_match, "Apple MacPro", {
1241 DMI_MATCH(DMI_BOARD_VENDOR, "Apple"),
1242 DMI_MATCH(DMI_PRODUCT_NAME, "MacPro") },
1244 { applesmc_dmi_match, "Apple iMac", {
1245 DMI_MATCH(DMI_BOARD_VENDOR, "Apple"),
1246 DMI_MATCH(DMI_PRODUCT_NAME, "iMac") },
1251 static int __init applesmc_init(void)
1255 if (!dmi_check_system(applesmc_whitelist)) {
1256 pr_warn("supported laptop not found!\n");
1261 if (!request_region(APPLESMC_DATA_PORT, APPLESMC_NR_PORTS,
1267 ret = platform_driver_register(&applesmc_driver);
1271 pdev = platform_device_register_simple("applesmc", APPLESMC_DATA_PORT,
1274 ret = PTR_ERR(pdev);
1278 /* create register cache */
1279 ret = applesmc_init_smcreg();
1283 ret = applesmc_create_nodes(info_group, 1);
1287 ret = applesmc_create_nodes(fan_group, smcreg.fan_count);
1291 ret = applesmc_create_nodes(temp_group, smcreg.index_count);
1295 ret = applesmc_create_accelerometer();
1297 goto out_temperature;
1299 ret = applesmc_create_light_sensor();
1301 goto out_accelerometer;
1303 ret = applesmc_create_key_backlight();
1305 goto out_light_sysfs;
1307 hwmon_dev = hwmon_device_register(&pdev->dev);
1308 if (IS_ERR(hwmon_dev)) {
1309 ret = PTR_ERR(hwmon_dev);
1310 goto out_light_ledclass;
1316 applesmc_release_key_backlight();
1318 applesmc_release_light_sensor();
1320 applesmc_release_accelerometer();
1322 applesmc_destroy_nodes(temp_group);
1324 applesmc_destroy_nodes(fan_group);
1326 applesmc_destroy_nodes(info_group);
1328 applesmc_destroy_smcreg();
1330 platform_device_unregister(pdev);
1332 platform_driver_unregister(&applesmc_driver);
1334 release_region(APPLESMC_DATA_PORT, APPLESMC_NR_PORTS);
1336 pr_warn("driver init failed (ret=%d)!\n", ret);
1340 static void __exit applesmc_exit(void)
1342 hwmon_device_unregister(hwmon_dev);
1343 applesmc_release_key_backlight();
1344 applesmc_release_light_sensor();
1345 applesmc_release_accelerometer();
1346 applesmc_destroy_nodes(temp_group);
1347 applesmc_destroy_nodes(fan_group);
1348 applesmc_destroy_nodes(info_group);
1349 applesmc_destroy_smcreg();
1350 platform_device_unregister(pdev);
1351 platform_driver_unregister(&applesmc_driver);
1352 release_region(APPLESMC_DATA_PORT, APPLESMC_NR_PORTS);
1355 module_init(applesmc_init);
1356 module_exit(applesmc_exit);
1358 MODULE_AUTHOR("Nicolas Boichat");
1359 MODULE_DESCRIPTION("Apple SMC");
1360 MODULE_LICENSE("GPL v2");
1361 MODULE_DEVICE_TABLE(dmi, applesmc_whitelist);