hwmon: (applesmc) Dynamic creation of temperature files
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / hwmon / applesmc.c
blob03c24b7c9cbde60b3c5c7e81d117c79468a78e45
1 /*
2 * drivers/hwmon/applesmc.c - driver for Apple's SMC (accelerometer, temperature
3 * sensors, fan control, keyboard backlight control) used in Intel-based Apple
4 * computers.
6 * Copyright (C) 2007 Nicolas Boichat <nicolas@boichat.ch>
8 * Based on hdaps.c driver:
9 * Copyright (C) 2005 Robert Love <rml@novell.com>
10 * Copyright (C) 2005 Jesper Juhl <jesper.juhl@gmail.com>
12 * Fan control based on smcFanControl:
13 * Copyright (C) 2006 Hendrik Holtmann <holtmann@mac.com>
15 * This program is free software; you can redistribute it and/or modify it
16 * under the terms of the GNU General Public License v2 as published by the
17 * Free Software Foundation.
19 * This program is distributed in the hope that it will be useful, but WITHOUT
20 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
21 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
22 * more details.
24 * You should have received a copy of the GNU General Public License along with
25 * this program; if not, write to the Free Software Foundation, Inc.,
26 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
29 #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>
41 #include <linux/io.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[] = {
89 FAN_ACTUAL_SPEED,
90 FAN_MIN_SPEED,
91 FAN_MAX_SPEED,
92 FAN_SAFE_SPEED,
93 FAN_TARGET_SPEED
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
103 #define SENSOR_X 0
104 #define SENSOR_Y 1
105 #define SENSOR_Z 2
107 #define to_index(attr) (to_sensor_dev_attr(attr)->index)
109 /* Structure to be passed to DMI_MATCH function */
110 struct dmi_match_data {
111 /* Indicates whether this computer has an accelerometer. */
112 int accelerometer;
113 /* Indicates whether this computer has light sensors and keyboard backlight. */
114 int light;
115 /* Indicates which temperature sensors set to use. */
116 int temperature_set;
119 /* Dynamic device node attributes */
120 struct applesmc_dev_attr {
121 struct sensor_device_attribute sda; /* hwmon attributes */
122 char name[32]; /* room for node file name */
125 /* Dynamic device node group */
126 struct applesmc_node_group {
127 char *format; /* format string */
128 void *show; /* show function */
129 void *store; /* store function */
130 struct applesmc_dev_attr *nodes; /* dynamic node array */
133 /* AppleSMC entry - cached register information */
134 struct applesmc_entry {
135 char key[5]; /* four-letter key code */
136 u8 valid; /* set when entry is successfully read once */
137 u8 len; /* bounded by APPLESMC_MAX_DATA_LENGTH */
138 char type[5]; /* four-letter type code */
139 u8 flags; /* 0x10: func; 0x40: write; 0x80: read */
142 /* Register lookup and registers common to all SMCs */
143 static struct applesmc_registers {
144 struct mutex mutex; /* register read/write mutex */
145 unsigned int key_count; /* number of SMC registers */
146 unsigned int temp_count; /* number of temperature registers */
147 unsigned int temp_begin; /* temperature lower index bound */
148 unsigned int temp_end; /* temperature upper index bound */
149 bool init_complete; /* true when fully initialized */
150 struct applesmc_entry *cache; /* cached key entries */
151 } smcreg = {
152 .mutex = __MUTEX_INITIALIZER(smcreg.mutex),
155 static const int debug;
156 static struct platform_device *pdev;
157 static s16 rest_x;
158 static s16 rest_y;
159 static u8 backlight_state[2];
161 static struct device *hwmon_dev;
162 static struct input_polled_dev *applesmc_idev;
164 /* Indicates whether this computer has an accelerometer. */
165 static unsigned int applesmc_accelerometer;
167 /* Indicates whether this computer has light sensors and keyboard backlight. */
168 static unsigned int applesmc_light;
170 /* The number of fans handled by the driver */
171 static unsigned int fans_handled;
174 * Last index written to key_at_index sysfs file, and value to use for all other
175 * key_at_index_* sysfs files.
177 static unsigned int key_at_index;
179 static struct workqueue_struct *applesmc_led_wq;
182 * __wait_status - Wait up to 32ms for the status port to get a certain value
183 * (masked with 0x0f), returning zero if the value is obtained. Callers must
184 * hold applesmc_lock.
186 static int __wait_status(u8 val)
188 int us;
190 val = val & APPLESMC_STATUS_MASK;
192 for (us = APPLESMC_MIN_WAIT; us < APPLESMC_MAX_WAIT; us <<= 1) {
193 udelay(us);
194 if ((inb(APPLESMC_CMD_PORT) & APPLESMC_STATUS_MASK) == val) {
195 return 0;
199 return -EIO;
203 * special treatment of command port - on newer macbooks, it seems necessary
204 * to resend the command byte before polling the status again. Callers must
205 * hold applesmc_lock.
207 static int send_command(u8 cmd)
209 int us;
210 for (us = APPLESMC_MIN_WAIT; us < APPLESMC_MAX_WAIT; us <<= 1) {
211 outb(cmd, APPLESMC_CMD_PORT);
212 udelay(us);
213 if ((inb(APPLESMC_CMD_PORT) & APPLESMC_STATUS_MASK) == 0x0c)
214 return 0;
216 return -EIO;
219 static int send_argument(const char *key)
221 int i;
223 for (i = 0; i < 4; i++) {
224 outb(key[i], APPLESMC_DATA_PORT);
225 if (__wait_status(0x04))
226 return -EIO;
228 return 0;
231 static int read_smc(u8 cmd, const char *key, u8 *buffer, u8 len)
233 int i;
235 if (send_command(cmd) || send_argument(key)) {
236 pr_warn("%s: read arg fail\n", key);
237 return -EIO;
240 outb(len, APPLESMC_DATA_PORT);
242 for (i = 0; i < len; i++) {
243 if (__wait_status(0x05)) {
244 pr_warn("%s: read data fail\n", key);
245 return -EIO;
247 buffer[i] = inb(APPLESMC_DATA_PORT);
250 return 0;
253 static int write_smc(u8 cmd, const char *key, const u8 *buffer, u8 len)
255 int i;
257 if (send_command(cmd) || send_argument(key)) {
258 pr_warn("%s: write arg fail\n", key);
259 return -EIO;
262 outb(len, APPLESMC_DATA_PORT);
264 for (i = 0; i < len; i++) {
265 if (__wait_status(0x04)) {
266 pr_warn("%s: write data fail\n", key);
267 return -EIO;
269 outb(buffer[i], APPLESMC_DATA_PORT);
272 return 0;
275 static int read_register_count(unsigned int *count)
277 __be32 be;
278 int ret;
280 ret = read_smc(APPLESMC_READ_CMD, KEY_COUNT_KEY, (u8 *)&be, 4);
281 if (ret)
282 return ret;
284 *count = be32_to_cpu(be);
285 return 0;
289 * Serialized I/O
291 * Returns zero on success or a negative error on failure.
292 * All functions below are concurrency safe - callers should NOT hold lock.
295 static int applesmc_read_entry(const struct applesmc_entry *entry,
296 u8 *buf, u8 len)
298 int ret;
300 if (entry->len != len)
301 return -EINVAL;
302 mutex_lock(&smcreg.mutex);
303 ret = read_smc(APPLESMC_READ_CMD, entry->key, buf, len);
304 mutex_unlock(&smcreg.mutex);
306 return ret;
309 static int applesmc_write_entry(const struct applesmc_entry *entry,
310 const u8 *buf, u8 len)
312 int ret;
314 if (entry->len != len)
315 return -EINVAL;
316 mutex_lock(&smcreg.mutex);
317 ret = write_smc(APPLESMC_WRITE_CMD, entry->key, buf, len);
318 mutex_unlock(&smcreg.mutex);
319 return ret;
322 static const struct applesmc_entry *applesmc_get_entry_by_index(int index)
324 struct applesmc_entry *cache = &smcreg.cache[index];
325 u8 key[4], info[6];
326 __be32 be;
327 int ret = 0;
329 if (cache->valid)
330 return cache;
332 mutex_lock(&smcreg.mutex);
334 if (cache->valid)
335 goto out;
336 be = cpu_to_be32(index);
337 ret = read_smc(APPLESMC_GET_KEY_BY_INDEX_CMD, (u8 *)&be, key, 4);
338 if (ret)
339 goto out;
340 ret = read_smc(APPLESMC_GET_KEY_TYPE_CMD, key, info, 6);
341 if (ret)
342 goto out;
344 memcpy(cache->key, key, 4);
345 cache->len = info[0];
346 memcpy(cache->type, &info[1], 4);
347 cache->flags = info[5];
348 cache->valid = 1;
350 out:
351 mutex_unlock(&smcreg.mutex);
352 if (ret)
353 return ERR_PTR(ret);
354 return cache;
357 static int applesmc_get_lower_bound(unsigned int *lo, const char *key)
359 int begin = 0, end = smcreg.key_count;
360 const struct applesmc_entry *entry;
362 while (begin != end) {
363 int middle = begin + (end - begin) / 2;
364 entry = applesmc_get_entry_by_index(middle);
365 if (IS_ERR(entry))
366 return PTR_ERR(entry);
367 if (strcmp(entry->key, key) < 0)
368 begin = middle + 1;
369 else
370 end = middle;
373 *lo = begin;
374 return 0;
377 static int applesmc_get_upper_bound(unsigned int *hi, const char *key)
379 int begin = 0, end = smcreg.key_count;
380 const struct applesmc_entry *entry;
382 while (begin != end) {
383 int middle = begin + (end - begin) / 2;
384 entry = applesmc_get_entry_by_index(middle);
385 if (IS_ERR(entry))
386 return PTR_ERR(entry);
387 if (strcmp(key, entry->key) < 0)
388 end = middle;
389 else
390 begin = middle + 1;
393 *hi = begin;
394 return 0;
397 static const struct applesmc_entry *applesmc_get_entry_by_key(const char *key)
399 int begin, end;
400 int ret;
402 ret = applesmc_get_lower_bound(&begin, key);
403 if (ret)
404 return ERR_PTR(ret);
405 ret = applesmc_get_upper_bound(&end, key);
406 if (ret)
407 return ERR_PTR(ret);
408 if (end - begin != 1)
409 return ERR_PTR(-EINVAL);
411 return applesmc_get_entry_by_index(begin);
414 static int applesmc_read_key(const char *key, u8 *buffer, u8 len)
416 const struct applesmc_entry *entry;
418 entry = applesmc_get_entry_by_key(key);
419 if (IS_ERR(entry))
420 return PTR_ERR(entry);
422 return applesmc_read_entry(entry, buffer, len);
425 static int applesmc_write_key(const char *key, const u8 *buffer, u8 len)
427 const struct applesmc_entry *entry;
429 entry = applesmc_get_entry_by_key(key);
430 if (IS_ERR(entry))
431 return PTR_ERR(entry);
433 return applesmc_write_entry(entry, buffer, len);
437 * applesmc_read_motion_sensor - Read motion sensor (X, Y or Z).
439 static int applesmc_read_motion_sensor(int index, s16* value)
441 u8 buffer[2];
442 int ret;
444 switch (index) {
445 case SENSOR_X:
446 ret = applesmc_read_key(MOTION_SENSOR_X_KEY, buffer, 2);
447 break;
448 case SENSOR_Y:
449 ret = applesmc_read_key(MOTION_SENSOR_Y_KEY, buffer, 2);
450 break;
451 case SENSOR_Z:
452 ret = applesmc_read_key(MOTION_SENSOR_Z_KEY, buffer, 2);
453 break;
454 default:
455 ret = -EINVAL;
458 *value = ((s16)buffer[0] << 8) | buffer[1];
460 return ret;
464 * applesmc_device_init - initialize the accelerometer. Can sleep.
466 static void applesmc_device_init(void)
468 int total;
469 u8 buffer[2];
471 if (!applesmc_accelerometer)
472 return;
474 for (total = INIT_TIMEOUT_MSECS; total > 0; total -= INIT_WAIT_MSECS) {
475 if (!applesmc_read_key(MOTION_SENSOR_KEY, buffer, 2) &&
476 (buffer[0] != 0x00 || buffer[1] != 0x00))
477 return;
478 buffer[0] = 0xe0;
479 buffer[1] = 0x00;
480 applesmc_write_key(MOTION_SENSOR_KEY, buffer, 2);
481 msleep(INIT_WAIT_MSECS);
484 pr_warn("failed to init the device\n");
488 * applesmc_get_fan_count - get the number of fans.
490 static int applesmc_get_fan_count(void)
492 int ret;
493 u8 buffer[1];
495 ret = applesmc_read_key(FANS_COUNT, buffer, 1);
497 if (ret)
498 return ret;
499 else
500 return buffer[0];
504 * applesmc_init_smcreg_try - Try to initialize register cache. Idempotent.
506 static int applesmc_init_smcreg_try(void)
508 struct applesmc_registers *s = &smcreg;
509 int ret;
511 if (s->init_complete)
512 return 0;
514 ret = read_register_count(&s->key_count);
515 if (ret)
516 return ret;
518 if (!s->cache)
519 s->cache = kcalloc(s->key_count, sizeof(*s->cache), GFP_KERNEL);
520 if (!s->cache)
521 return -ENOMEM;
523 ret = applesmc_get_lower_bound(&s->temp_begin, "T");
524 if (ret)
525 return ret;
526 ret = applesmc_get_lower_bound(&s->temp_end, "U");
527 if (ret)
528 return ret;
529 s->temp_count = s->temp_end - s->temp_begin;
531 s->init_complete = true;
533 pr_info("key=%d temp=%d\n", s->key_count, s->temp_count);
535 return 0;
539 * applesmc_init_smcreg - Initialize register cache.
541 * Retries until initialization is successful, or the operation times out.
544 static int applesmc_init_smcreg(void)
546 int ms, ret;
548 for (ms = 0; ms < INIT_TIMEOUT_MSECS; ms += INIT_WAIT_MSECS) {
549 ret = applesmc_init_smcreg_try();
550 if (!ret) {
551 if (ms)
552 pr_info("init_smcreg() took %d ms\n", ms);
553 return 0;
555 msleep(INIT_WAIT_MSECS);
558 kfree(smcreg.cache);
559 smcreg.cache = NULL;
561 return ret;
564 static void applesmc_destroy_smcreg(void)
566 kfree(smcreg.cache);
567 smcreg.cache = NULL;
568 smcreg.init_complete = false;
571 /* Device model stuff */
572 static int applesmc_probe(struct platform_device *dev)
574 int ret;
576 ret = applesmc_init_smcreg();
577 if (ret)
578 return ret;
580 applesmc_device_init();
582 return 0;
585 /* Synchronize device with memorized backlight state */
586 static int applesmc_pm_resume(struct device *dev)
588 if (applesmc_light)
589 applesmc_write_key(BACKLIGHT_KEY, backlight_state, 2);
590 return 0;
593 /* Reinitialize device on resume from hibernation */
594 static int applesmc_pm_restore(struct device *dev)
596 applesmc_device_init();
597 return applesmc_pm_resume(dev);
600 static const struct dev_pm_ops applesmc_pm_ops = {
601 .resume = applesmc_pm_resume,
602 .restore = applesmc_pm_restore,
605 static struct platform_driver applesmc_driver = {
606 .probe = applesmc_probe,
607 .driver = {
608 .name = "applesmc",
609 .owner = THIS_MODULE,
610 .pm = &applesmc_pm_ops,
615 * applesmc_calibrate - Set our "resting" values. Callers must
616 * hold applesmc_lock.
618 static void applesmc_calibrate(void)
620 applesmc_read_motion_sensor(SENSOR_X, &rest_x);
621 applesmc_read_motion_sensor(SENSOR_Y, &rest_y);
622 rest_x = -rest_x;
625 static void applesmc_idev_poll(struct input_polled_dev *dev)
627 struct input_dev *idev = dev->input;
628 s16 x, y;
630 if (applesmc_read_motion_sensor(SENSOR_X, &x))
631 return;
632 if (applesmc_read_motion_sensor(SENSOR_Y, &y))
633 return;
635 x = -x;
636 input_report_abs(idev, ABS_X, x - rest_x);
637 input_report_abs(idev, ABS_Y, y - rest_y);
638 input_sync(idev);
641 /* Sysfs Files */
643 static ssize_t applesmc_name_show(struct device *dev,
644 struct device_attribute *attr, char *buf)
646 return snprintf(buf, PAGE_SIZE, "applesmc\n");
649 static ssize_t applesmc_position_show(struct device *dev,
650 struct device_attribute *attr, char *buf)
652 int ret;
653 s16 x, y, z;
655 ret = applesmc_read_motion_sensor(SENSOR_X, &x);
656 if (ret)
657 goto out;
658 ret = applesmc_read_motion_sensor(SENSOR_Y, &y);
659 if (ret)
660 goto out;
661 ret = applesmc_read_motion_sensor(SENSOR_Z, &z);
662 if (ret)
663 goto out;
665 out:
666 if (ret)
667 return ret;
668 else
669 return snprintf(buf, PAGE_SIZE, "(%d,%d,%d)\n", x, y, z);
672 static ssize_t applesmc_light_show(struct device *dev,
673 struct device_attribute *attr, char *sysfsbuf)
675 const struct applesmc_entry *entry;
676 static int data_length;
677 int ret;
678 u8 left = 0, right = 0;
679 u8 buffer[10];
681 if (!data_length) {
682 entry = applesmc_get_entry_by_key(LIGHT_SENSOR_LEFT_KEY);
683 if (IS_ERR(entry))
684 return PTR_ERR(entry);
685 if (entry->len > 10)
686 return -ENXIO;
687 data_length = entry->len;
688 pr_info("light sensor data length set to %d\n", data_length);
691 ret = applesmc_read_key(LIGHT_SENSOR_LEFT_KEY, buffer, data_length);
692 /* newer macbooks report a single 10-bit bigendian value */
693 if (data_length == 10) {
694 left = be16_to_cpu(*(__be16 *)(buffer + 6)) >> 2;
695 goto out;
697 left = buffer[2];
698 if (ret)
699 goto out;
700 ret = applesmc_read_key(LIGHT_SENSOR_RIGHT_KEY, buffer, data_length);
701 right = buffer[2];
703 out:
704 if (ret)
705 return ret;
706 else
707 return snprintf(sysfsbuf, PAGE_SIZE, "(%d,%d)\n", left, right);
710 /* Displays sensor key as label */
711 static ssize_t applesmc_show_sensor_label(struct device *dev,
712 struct device_attribute *devattr, char *sysfsbuf)
714 int index = smcreg.temp_begin + to_index(devattr);
715 const struct applesmc_entry *entry;
717 entry = applesmc_get_entry_by_index(index);
718 if (IS_ERR(entry))
719 return PTR_ERR(entry);
721 return snprintf(sysfsbuf, PAGE_SIZE, "%s\n", entry->key);
724 /* Displays degree Celsius * 1000 */
725 static ssize_t applesmc_show_temperature(struct device *dev,
726 struct device_attribute *devattr, char *sysfsbuf)
728 int index = smcreg.temp_begin + to_index(devattr);
729 const struct applesmc_entry *entry;
730 int ret;
731 u8 buffer[2];
732 unsigned int temp;
734 entry = applesmc_get_entry_by_index(index);
735 if (IS_ERR(entry))
736 return PTR_ERR(entry);
738 ret = applesmc_read_entry(entry, buffer, 2);
739 if (ret)
740 return ret;
742 temp = buffer[0]*1000;
743 temp += (buffer[1] >> 6) * 250;
745 return snprintf(sysfsbuf, PAGE_SIZE, "%u\n", temp);
748 static ssize_t applesmc_show_fan_speed(struct device *dev,
749 struct device_attribute *attr, char *sysfsbuf)
751 int ret;
752 unsigned int speed = 0;
753 char newkey[5];
754 u8 buffer[2];
755 struct sensor_device_attribute_2 *sensor_attr =
756 to_sensor_dev_attr_2(attr);
758 newkey[0] = fan_speed_keys[sensor_attr->nr][0];
759 newkey[1] = '0' + sensor_attr->index;
760 newkey[2] = fan_speed_keys[sensor_attr->nr][2];
761 newkey[3] = fan_speed_keys[sensor_attr->nr][3];
762 newkey[4] = 0;
764 ret = applesmc_read_key(newkey, buffer, 2);
765 speed = ((buffer[0] << 8 | buffer[1]) >> 2);
767 if (ret)
768 return ret;
769 else
770 return snprintf(sysfsbuf, PAGE_SIZE, "%u\n", speed);
773 static ssize_t applesmc_store_fan_speed(struct device *dev,
774 struct device_attribute *attr,
775 const char *sysfsbuf, size_t count)
777 int ret;
778 u32 speed;
779 char newkey[5];
780 u8 buffer[2];
781 struct sensor_device_attribute_2 *sensor_attr =
782 to_sensor_dev_attr_2(attr);
784 speed = simple_strtoul(sysfsbuf, NULL, 10);
786 if (speed > 0x4000) /* Bigger than a 14-bit value */
787 return -EINVAL;
789 newkey[0] = fan_speed_keys[sensor_attr->nr][0];
790 newkey[1] = '0' + sensor_attr->index;
791 newkey[2] = fan_speed_keys[sensor_attr->nr][2];
792 newkey[3] = fan_speed_keys[sensor_attr->nr][3];
793 newkey[4] = 0;
795 buffer[0] = (speed >> 6) & 0xff;
796 buffer[1] = (speed << 2) & 0xff;
797 ret = applesmc_write_key(newkey, buffer, 2);
799 if (ret)
800 return ret;
801 else
802 return count;
805 static ssize_t applesmc_show_fan_manual(struct device *dev,
806 struct device_attribute *devattr, char *sysfsbuf)
808 int ret;
809 u16 manual = 0;
810 u8 buffer[2];
811 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
813 ret = applesmc_read_key(FANS_MANUAL, buffer, 2);
814 manual = ((buffer[0] << 8 | buffer[1]) >> attr->index) & 0x01;
816 if (ret)
817 return ret;
818 else
819 return snprintf(sysfsbuf, PAGE_SIZE, "%d\n", manual);
822 static ssize_t applesmc_store_fan_manual(struct device *dev,
823 struct device_attribute *devattr,
824 const char *sysfsbuf, size_t count)
826 int ret;
827 u8 buffer[2];
828 u32 input;
829 u16 val;
830 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
832 input = simple_strtoul(sysfsbuf, NULL, 10);
834 ret = applesmc_read_key(FANS_MANUAL, buffer, 2);
835 val = (buffer[0] << 8 | buffer[1]);
836 if (ret)
837 goto out;
839 if (input)
840 val = val | (0x01 << attr->index);
841 else
842 val = val & ~(0x01 << attr->index);
844 buffer[0] = (val >> 8) & 0xFF;
845 buffer[1] = val & 0xFF;
847 ret = applesmc_write_key(FANS_MANUAL, buffer, 2);
849 out:
850 if (ret)
851 return ret;
852 else
853 return count;
856 static ssize_t applesmc_show_fan_position(struct device *dev,
857 struct device_attribute *attr, char *sysfsbuf)
859 int ret;
860 char newkey[5];
861 u8 buffer[17];
862 struct sensor_device_attribute_2 *sensor_attr =
863 to_sensor_dev_attr_2(attr);
865 newkey[0] = FAN_POSITION[0];
866 newkey[1] = '0' + sensor_attr->index;
867 newkey[2] = FAN_POSITION[2];
868 newkey[3] = FAN_POSITION[3];
869 newkey[4] = 0;
871 ret = applesmc_read_key(newkey, buffer, 16);
872 buffer[16] = 0;
874 if (ret)
875 return ret;
876 else
877 return snprintf(sysfsbuf, PAGE_SIZE, "%s\n", buffer+4);
880 static ssize_t applesmc_calibrate_show(struct device *dev,
881 struct device_attribute *attr, char *sysfsbuf)
883 return snprintf(sysfsbuf, PAGE_SIZE, "(%d,%d)\n", rest_x, rest_y);
886 static ssize_t applesmc_calibrate_store(struct device *dev,
887 struct device_attribute *attr, const char *sysfsbuf, size_t count)
889 applesmc_calibrate();
891 return count;
894 static void applesmc_backlight_set(struct work_struct *work)
896 applesmc_write_key(BACKLIGHT_KEY, backlight_state, 2);
898 static DECLARE_WORK(backlight_work, &applesmc_backlight_set);
900 static void applesmc_brightness_set(struct led_classdev *led_cdev,
901 enum led_brightness value)
903 int ret;
905 backlight_state[0] = value;
906 ret = queue_work(applesmc_led_wq, &backlight_work);
908 if (debug && (!ret))
909 printk(KERN_DEBUG "applesmc: work was already on the queue.\n");
912 static ssize_t applesmc_key_count_show(struct device *dev,
913 struct device_attribute *attr, char *sysfsbuf)
915 int ret;
916 u8 buffer[4];
917 u32 count;
919 ret = applesmc_read_key(KEY_COUNT_KEY, buffer, 4);
920 count = ((u32)buffer[0]<<24) + ((u32)buffer[1]<<16) +
921 ((u32)buffer[2]<<8) + buffer[3];
923 if (ret)
924 return ret;
925 else
926 return snprintf(sysfsbuf, PAGE_SIZE, "%d\n", count);
929 static ssize_t applesmc_key_at_index_read_show(struct device *dev,
930 struct device_attribute *attr, char *sysfsbuf)
932 const struct applesmc_entry *entry;
933 int ret;
935 entry = applesmc_get_entry_by_index(key_at_index);
936 if (IS_ERR(entry))
937 return PTR_ERR(entry);
938 ret = applesmc_read_entry(entry, sysfsbuf, entry->len);
939 if (ret)
940 return ret;
942 return entry->len;
945 static ssize_t applesmc_key_at_index_data_length_show(struct device *dev,
946 struct device_attribute *attr, char *sysfsbuf)
948 const struct applesmc_entry *entry;
950 entry = applesmc_get_entry_by_index(key_at_index);
951 if (IS_ERR(entry))
952 return PTR_ERR(entry);
954 return snprintf(sysfsbuf, PAGE_SIZE, "%d\n", entry->len);
957 static ssize_t applesmc_key_at_index_type_show(struct device *dev,
958 struct device_attribute *attr, char *sysfsbuf)
960 const struct applesmc_entry *entry;
962 entry = applesmc_get_entry_by_index(key_at_index);
963 if (IS_ERR(entry))
964 return PTR_ERR(entry);
966 return snprintf(sysfsbuf, PAGE_SIZE, "%s\n", entry->type);
969 static ssize_t applesmc_key_at_index_name_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);
975 if (IS_ERR(entry))
976 return PTR_ERR(entry);
978 return snprintf(sysfsbuf, PAGE_SIZE, "%s\n", entry->key);
981 static ssize_t applesmc_key_at_index_show(struct device *dev,
982 struct device_attribute *attr, char *sysfsbuf)
984 return snprintf(sysfsbuf, PAGE_SIZE, "%d\n", key_at_index);
987 static ssize_t applesmc_key_at_index_store(struct device *dev,
988 struct device_attribute *attr, const char *sysfsbuf, size_t count)
990 unsigned long newkey;
992 if (strict_strtoul(sysfsbuf, 10, &newkey) < 0
993 || newkey >= smcreg.key_count)
994 return -EINVAL;
996 key_at_index = newkey;
997 return count;
1000 static struct led_classdev applesmc_backlight = {
1001 .name = "smc::kbd_backlight",
1002 .default_trigger = "nand-disk",
1003 .brightness_set = applesmc_brightness_set,
1006 static DEVICE_ATTR(name, 0444, applesmc_name_show, NULL);
1008 static DEVICE_ATTR(position, 0444, applesmc_position_show, NULL);
1009 static DEVICE_ATTR(calibrate, 0644,
1010 applesmc_calibrate_show, applesmc_calibrate_store);
1012 static struct attribute *accelerometer_attributes[] = {
1013 &dev_attr_position.attr,
1014 &dev_attr_calibrate.attr,
1015 NULL
1018 static const struct attribute_group accelerometer_attributes_group =
1019 { .attrs = accelerometer_attributes };
1021 static DEVICE_ATTR(light, 0444, applesmc_light_show, NULL);
1023 static DEVICE_ATTR(key_count, 0444, applesmc_key_count_show, NULL);
1024 static DEVICE_ATTR(key_at_index, 0644,
1025 applesmc_key_at_index_show, applesmc_key_at_index_store);
1026 static DEVICE_ATTR(key_at_index_name, 0444,
1027 applesmc_key_at_index_name_show, NULL);
1028 static DEVICE_ATTR(key_at_index_type, 0444,
1029 applesmc_key_at_index_type_show, NULL);
1030 static DEVICE_ATTR(key_at_index_data_length, 0444,
1031 applesmc_key_at_index_data_length_show, NULL);
1032 static DEVICE_ATTR(key_at_index_data, 0444,
1033 applesmc_key_at_index_read_show, NULL);
1035 static struct attribute *key_enumeration_attributes[] = {
1036 &dev_attr_key_count.attr,
1037 &dev_attr_key_at_index.attr,
1038 &dev_attr_key_at_index_name.attr,
1039 &dev_attr_key_at_index_type.attr,
1040 &dev_attr_key_at_index_data_length.attr,
1041 &dev_attr_key_at_index_data.attr,
1042 NULL
1045 static const struct attribute_group key_enumeration_group =
1046 { .attrs = key_enumeration_attributes };
1049 * Macro defining SENSOR_DEVICE_ATTR for a fan sysfs entries.
1050 * - show actual speed
1051 * - show/store minimum speed
1052 * - show maximum speed
1053 * - show safe speed
1054 * - show/store target speed
1055 * - show/store manual mode
1057 #define sysfs_fan_speeds_offset(offset) \
1058 static SENSOR_DEVICE_ATTR_2(fan##offset##_input, S_IRUGO, \
1059 applesmc_show_fan_speed, NULL, 0, offset-1); \
1061 static SENSOR_DEVICE_ATTR_2(fan##offset##_min, S_IRUGO | S_IWUSR, \
1062 applesmc_show_fan_speed, applesmc_store_fan_speed, 1, offset-1); \
1064 static SENSOR_DEVICE_ATTR_2(fan##offset##_max, S_IRUGO, \
1065 applesmc_show_fan_speed, NULL, 2, offset-1); \
1067 static SENSOR_DEVICE_ATTR_2(fan##offset##_safe, S_IRUGO, \
1068 applesmc_show_fan_speed, NULL, 3, offset-1); \
1070 static SENSOR_DEVICE_ATTR_2(fan##offset##_output, S_IRUGO | S_IWUSR, \
1071 applesmc_show_fan_speed, applesmc_store_fan_speed, 4, offset-1); \
1073 static SENSOR_DEVICE_ATTR(fan##offset##_manual, S_IRUGO | S_IWUSR, \
1074 applesmc_show_fan_manual, applesmc_store_fan_manual, offset-1); \
1076 static SENSOR_DEVICE_ATTR(fan##offset##_label, S_IRUGO, \
1077 applesmc_show_fan_position, NULL, offset-1); \
1079 static struct attribute *fan##offset##_attributes[] = { \
1080 &sensor_dev_attr_fan##offset##_input.dev_attr.attr, \
1081 &sensor_dev_attr_fan##offset##_min.dev_attr.attr, \
1082 &sensor_dev_attr_fan##offset##_max.dev_attr.attr, \
1083 &sensor_dev_attr_fan##offset##_safe.dev_attr.attr, \
1084 &sensor_dev_attr_fan##offset##_output.dev_attr.attr, \
1085 &sensor_dev_attr_fan##offset##_manual.dev_attr.attr, \
1086 &sensor_dev_attr_fan##offset##_label.dev_attr.attr, \
1087 NULL \
1091 * Create the needed functions for each fan using the macro defined above
1092 * (4 fans are supported)
1094 sysfs_fan_speeds_offset(1);
1095 sysfs_fan_speeds_offset(2);
1096 sysfs_fan_speeds_offset(3);
1097 sysfs_fan_speeds_offset(4);
1099 static const struct attribute_group fan_attribute_groups[] = {
1100 { .attrs = fan1_attributes },
1101 { .attrs = fan2_attributes },
1102 { .attrs = fan3_attributes },
1103 { .attrs = fan4_attributes },
1106 static struct applesmc_node_group temp_group[] = {
1107 { "temp%d_label", applesmc_show_sensor_label },
1108 { "temp%d_input", applesmc_show_temperature },
1112 /* Module stuff */
1115 * applesmc_dmi_match - found a match. return one, short-circuiting the hunt.
1117 static int applesmc_dmi_match(const struct dmi_system_id *id)
1119 struct dmi_match_data* dmi_data = id->driver_data;
1120 pr_info("%s detected:\n", id->ident);
1121 applesmc_accelerometer = dmi_data->accelerometer;
1122 pr_info(" - Model %s accelerometer\n",
1123 applesmc_accelerometer ? "with" : "without");
1124 applesmc_light = dmi_data->light;
1125 pr_info(" - Model %s light sensors and backlight\n",
1126 applesmc_light ? "with" : "without");
1128 return 1;
1132 * applesmc_destroy_nodes - remove files and free associated memory
1134 static void applesmc_destroy_nodes(struct applesmc_node_group *groups)
1136 struct applesmc_node_group *grp;
1137 struct applesmc_dev_attr *node;
1139 for (grp = groups; grp->nodes; grp++) {
1140 for (node = grp->nodes; node->sda.dev_attr.attr.name; node++)
1141 sysfs_remove_file(&pdev->dev.kobj,
1142 &node->sda.dev_attr.attr);
1143 kfree(grp->nodes);
1144 grp->nodes = NULL;
1149 * applesmc_create_nodes - create a two-dimensional group of sysfs files
1151 static int applesmc_create_nodes(struct applesmc_node_group *groups, int num)
1153 struct applesmc_node_group *grp;
1154 struct applesmc_dev_attr *node;
1155 struct attribute *attr;
1156 int ret, i;
1158 for (grp = groups; grp->format; grp++) {
1159 grp->nodes = kcalloc(num + 1, sizeof(*node), GFP_KERNEL);
1160 if (!grp->nodes) {
1161 ret = -ENOMEM;
1162 goto out;
1164 for (i = 0; i < num; i++) {
1165 node = &grp->nodes[i];
1166 sprintf(node->name, grp->format, i + 1);
1167 node->sda.index = i;
1168 node->sda.dev_attr.show = grp->show;
1169 node->sda.dev_attr.store = grp->store;
1170 attr = &node->sda.dev_attr.attr;
1171 attr->name = node->name;
1172 attr->mode = S_IRUGO | (grp->store ? S_IWUSR : 0);
1173 ret = sysfs_create_file(&pdev->dev.kobj, attr);
1174 if (ret) {
1175 attr->name = NULL;
1176 goto out;
1181 return 0;
1182 out:
1183 applesmc_destroy_nodes(groups);
1184 return ret;
1187 /* Create accelerometer ressources */
1188 static int applesmc_create_accelerometer(void)
1190 struct input_dev *idev;
1191 int ret;
1193 ret = sysfs_create_group(&pdev->dev.kobj,
1194 &accelerometer_attributes_group);
1195 if (ret)
1196 goto out;
1198 applesmc_idev = input_allocate_polled_device();
1199 if (!applesmc_idev) {
1200 ret = -ENOMEM;
1201 goto out_sysfs;
1204 applesmc_idev->poll = applesmc_idev_poll;
1205 applesmc_idev->poll_interval = APPLESMC_POLL_INTERVAL;
1207 /* initial calibrate for the input device */
1208 applesmc_calibrate();
1210 /* initialize the input device */
1211 idev = applesmc_idev->input;
1212 idev->name = "applesmc";
1213 idev->id.bustype = BUS_HOST;
1214 idev->dev.parent = &pdev->dev;
1215 idev->evbit[0] = BIT_MASK(EV_ABS);
1216 input_set_abs_params(idev, ABS_X,
1217 -256, 256, APPLESMC_INPUT_FUZZ, APPLESMC_INPUT_FLAT);
1218 input_set_abs_params(idev, ABS_Y,
1219 -256, 256, APPLESMC_INPUT_FUZZ, APPLESMC_INPUT_FLAT);
1221 ret = input_register_polled_device(applesmc_idev);
1222 if (ret)
1223 goto out_idev;
1225 return 0;
1227 out_idev:
1228 input_free_polled_device(applesmc_idev);
1230 out_sysfs:
1231 sysfs_remove_group(&pdev->dev.kobj, &accelerometer_attributes_group);
1233 out:
1234 pr_warn("driver init failed (ret=%d)!\n", ret);
1235 return ret;
1238 /* Release all ressources used by the accelerometer */
1239 static void applesmc_release_accelerometer(void)
1241 input_unregister_polled_device(applesmc_idev);
1242 input_free_polled_device(applesmc_idev);
1243 sysfs_remove_group(&pdev->dev.kobj, &accelerometer_attributes_group);
1246 static __initdata struct dmi_match_data applesmc_dmi_data[] = {
1247 /* MacBook Pro: accelerometer, backlight and temperature set 0 */
1248 { .accelerometer = 1, .light = 1, .temperature_set = 0 },
1249 /* MacBook2: accelerometer and temperature set 1 */
1250 { .accelerometer = 1, .light = 0, .temperature_set = 1 },
1251 /* MacBook: accelerometer and temperature set 2 */
1252 { .accelerometer = 1, .light = 0, .temperature_set = 2 },
1253 /* MacMini: temperature set 3 */
1254 { .accelerometer = 0, .light = 0, .temperature_set = 3 },
1255 /* MacPro: temperature set 4 */
1256 { .accelerometer = 0, .light = 0, .temperature_set = 4 },
1257 /* iMac: temperature set 5 */
1258 { .accelerometer = 0, .light = 0, .temperature_set = 5 },
1259 /* MacBook3, MacBook4: accelerometer and temperature set 6 */
1260 { .accelerometer = 1, .light = 0, .temperature_set = 6 },
1261 /* MacBook Air: accelerometer, backlight and temperature set 7 */
1262 { .accelerometer = 1, .light = 1, .temperature_set = 7 },
1263 /* MacBook Pro 4: accelerometer, backlight and temperature set 8 */
1264 { .accelerometer = 1, .light = 1, .temperature_set = 8 },
1265 /* MacBook Pro 3: accelerometer, backlight and temperature set 9 */
1266 { .accelerometer = 1, .light = 1, .temperature_set = 9 },
1267 /* iMac 5: light sensor only, temperature set 10 */
1268 { .accelerometer = 0, .light = 0, .temperature_set = 10 },
1269 /* MacBook 5: accelerometer, backlight and temperature set 11 */
1270 { .accelerometer = 1, .light = 1, .temperature_set = 11 },
1271 /* MacBook Pro 5: accelerometer, backlight and temperature set 12 */
1272 { .accelerometer = 1, .light = 1, .temperature_set = 12 },
1273 /* iMac 8: light sensor only, temperature set 13 */
1274 { .accelerometer = 0, .light = 0, .temperature_set = 13 },
1275 /* iMac 6: light sensor only, temperature set 14 */
1276 { .accelerometer = 0, .light = 0, .temperature_set = 14 },
1277 /* MacBook Air 2,1: accelerometer, backlight and temperature set 15 */
1278 { .accelerometer = 1, .light = 1, .temperature_set = 15 },
1279 /* MacPro3,1: temperature set 16 */
1280 { .accelerometer = 0, .light = 0, .temperature_set = 16 },
1281 /* iMac 9,1: light sensor only, temperature set 17 */
1282 { .accelerometer = 0, .light = 0, .temperature_set = 17 },
1283 /* MacBook Pro 2,2: accelerometer, backlight and temperature set 18 */
1284 { .accelerometer = 1, .light = 1, .temperature_set = 18 },
1285 /* MacBook Pro 5,3: accelerometer, backlight and temperature set 19 */
1286 { .accelerometer = 1, .light = 1, .temperature_set = 19 },
1287 /* MacBook Pro 5,4: accelerometer, backlight and temperature set 20 */
1288 { .accelerometer = 1, .light = 1, .temperature_set = 20 },
1289 /* MacBook Pro 6,2: accelerometer, backlight and temperature set 21 */
1290 { .accelerometer = 1, .light = 1, .temperature_set = 21 },
1291 /* MacBook Pro 7,1: accelerometer, backlight and temperature set 22 */
1292 { .accelerometer = 1, .light = 1, .temperature_set = 22 },
1293 /* MacBook Air 3,1: accelerometer, backlight and temperature set 23 */
1294 { .accelerometer = 0, .light = 0, .temperature_set = 23 },
1297 /* Note that DMI_MATCH(...,"MacBook") will match "MacBookPro1,1".
1298 * So we need to put "Apple MacBook Pro" before "Apple MacBook". */
1299 static __initdata struct dmi_system_id applesmc_whitelist[] = {
1300 { applesmc_dmi_match, "Apple MacBook Air 3", {
1301 DMI_MATCH(DMI_BOARD_VENDOR, "Apple"),
1302 DMI_MATCH(DMI_PRODUCT_NAME, "MacBookAir3") },
1303 &applesmc_dmi_data[23]},
1304 { applesmc_dmi_match, "Apple MacBook Air 2", {
1305 DMI_MATCH(DMI_BOARD_VENDOR, "Apple"),
1306 DMI_MATCH(DMI_PRODUCT_NAME, "MacBookAir2") },
1307 &applesmc_dmi_data[15]},
1308 { applesmc_dmi_match, "Apple MacBook Air", {
1309 DMI_MATCH(DMI_BOARD_VENDOR, "Apple"),
1310 DMI_MATCH(DMI_PRODUCT_NAME, "MacBookAir") },
1311 &applesmc_dmi_data[7]},
1312 { applesmc_dmi_match, "Apple MacBook Pro 7", {
1313 DMI_MATCH(DMI_BOARD_VENDOR, "Apple"),
1314 DMI_MATCH(DMI_PRODUCT_NAME, "MacBookPro7") },
1315 &applesmc_dmi_data[22]},
1316 { applesmc_dmi_match, "Apple MacBook Pro 5,4", {
1317 DMI_MATCH(DMI_BOARD_VENDOR, "Apple"),
1318 DMI_MATCH(DMI_PRODUCT_NAME, "MacBookPro5,4") },
1319 &applesmc_dmi_data[20]},
1320 { applesmc_dmi_match, "Apple MacBook Pro 5,3", {
1321 DMI_MATCH(DMI_BOARD_VENDOR, "Apple"),
1322 DMI_MATCH(DMI_PRODUCT_NAME, "MacBookPro5,3") },
1323 &applesmc_dmi_data[19]},
1324 { applesmc_dmi_match, "Apple MacBook Pro 6", {
1325 DMI_MATCH(DMI_BOARD_VENDOR, "Apple"),
1326 DMI_MATCH(DMI_PRODUCT_NAME, "MacBookPro6") },
1327 &applesmc_dmi_data[21]},
1328 { applesmc_dmi_match, "Apple MacBook Pro 5", {
1329 DMI_MATCH(DMI_BOARD_VENDOR, "Apple"),
1330 DMI_MATCH(DMI_PRODUCT_NAME, "MacBookPro5") },
1331 &applesmc_dmi_data[12]},
1332 { applesmc_dmi_match, "Apple MacBook Pro 4", {
1333 DMI_MATCH(DMI_BOARD_VENDOR, "Apple"),
1334 DMI_MATCH(DMI_PRODUCT_NAME, "MacBookPro4") },
1335 &applesmc_dmi_data[8]},
1336 { applesmc_dmi_match, "Apple MacBook Pro 3", {
1337 DMI_MATCH(DMI_BOARD_VENDOR, "Apple"),
1338 DMI_MATCH(DMI_PRODUCT_NAME, "MacBookPro3") },
1339 &applesmc_dmi_data[9]},
1340 { applesmc_dmi_match, "Apple MacBook Pro 2,2", {
1341 DMI_MATCH(DMI_BOARD_VENDOR, "Apple Computer, Inc."),
1342 DMI_MATCH(DMI_PRODUCT_NAME, "MacBookPro2,2") },
1343 &applesmc_dmi_data[18]},
1344 { applesmc_dmi_match, "Apple MacBook Pro", {
1345 DMI_MATCH(DMI_BOARD_VENDOR,"Apple"),
1346 DMI_MATCH(DMI_PRODUCT_NAME,"MacBookPro") },
1347 &applesmc_dmi_data[0]},
1348 { applesmc_dmi_match, "Apple MacBook (v2)", {
1349 DMI_MATCH(DMI_BOARD_VENDOR,"Apple"),
1350 DMI_MATCH(DMI_PRODUCT_NAME,"MacBook2") },
1351 &applesmc_dmi_data[1]},
1352 { applesmc_dmi_match, "Apple MacBook (v3)", {
1353 DMI_MATCH(DMI_BOARD_VENDOR,"Apple"),
1354 DMI_MATCH(DMI_PRODUCT_NAME,"MacBook3") },
1355 &applesmc_dmi_data[6]},
1356 { applesmc_dmi_match, "Apple MacBook 4", {
1357 DMI_MATCH(DMI_BOARD_VENDOR, "Apple"),
1358 DMI_MATCH(DMI_PRODUCT_NAME, "MacBook4") },
1359 &applesmc_dmi_data[6]},
1360 { applesmc_dmi_match, "Apple MacBook 5", {
1361 DMI_MATCH(DMI_BOARD_VENDOR, "Apple"),
1362 DMI_MATCH(DMI_PRODUCT_NAME, "MacBook5") },
1363 &applesmc_dmi_data[11]},
1364 { applesmc_dmi_match, "Apple MacBook", {
1365 DMI_MATCH(DMI_BOARD_VENDOR,"Apple"),
1366 DMI_MATCH(DMI_PRODUCT_NAME,"MacBook") },
1367 &applesmc_dmi_data[2]},
1368 { applesmc_dmi_match, "Apple Macmini", {
1369 DMI_MATCH(DMI_BOARD_VENDOR,"Apple"),
1370 DMI_MATCH(DMI_PRODUCT_NAME,"Macmini") },
1371 &applesmc_dmi_data[3]},
1372 { applesmc_dmi_match, "Apple MacPro2", {
1373 DMI_MATCH(DMI_BOARD_VENDOR,"Apple"),
1374 DMI_MATCH(DMI_PRODUCT_NAME,"MacPro2") },
1375 &applesmc_dmi_data[4]},
1376 { applesmc_dmi_match, "Apple MacPro3", {
1377 DMI_MATCH(DMI_BOARD_VENDOR, "Apple"),
1378 DMI_MATCH(DMI_PRODUCT_NAME, "MacPro3") },
1379 &applesmc_dmi_data[16]},
1380 { applesmc_dmi_match, "Apple MacPro", {
1381 DMI_MATCH(DMI_BOARD_VENDOR, "Apple"),
1382 DMI_MATCH(DMI_PRODUCT_NAME, "MacPro") },
1383 &applesmc_dmi_data[4]},
1384 { applesmc_dmi_match, "Apple iMac 9,1", {
1385 DMI_MATCH(DMI_BOARD_VENDOR, "Apple Inc."),
1386 DMI_MATCH(DMI_PRODUCT_NAME, "iMac9,1") },
1387 &applesmc_dmi_data[17]},
1388 { applesmc_dmi_match, "Apple iMac 8", {
1389 DMI_MATCH(DMI_BOARD_VENDOR, "Apple"),
1390 DMI_MATCH(DMI_PRODUCT_NAME, "iMac8") },
1391 &applesmc_dmi_data[13]},
1392 { applesmc_dmi_match, "Apple iMac 6", {
1393 DMI_MATCH(DMI_BOARD_VENDOR, "Apple"),
1394 DMI_MATCH(DMI_PRODUCT_NAME, "iMac6") },
1395 &applesmc_dmi_data[14]},
1396 { applesmc_dmi_match, "Apple iMac 5", {
1397 DMI_MATCH(DMI_BOARD_VENDOR, "Apple"),
1398 DMI_MATCH(DMI_PRODUCT_NAME, "iMac5") },
1399 &applesmc_dmi_data[10]},
1400 { applesmc_dmi_match, "Apple iMac", {
1401 DMI_MATCH(DMI_BOARD_VENDOR,"Apple"),
1402 DMI_MATCH(DMI_PRODUCT_NAME,"iMac") },
1403 &applesmc_dmi_data[5]},
1404 { .ident = NULL }
1407 static int __init applesmc_init(void)
1409 int ret;
1410 int count;
1412 if (!dmi_check_system(applesmc_whitelist)) {
1413 pr_warn("supported laptop not found!\n");
1414 ret = -ENODEV;
1415 goto out;
1418 if (!request_region(APPLESMC_DATA_PORT, APPLESMC_NR_PORTS,
1419 "applesmc")) {
1420 ret = -ENXIO;
1421 goto out;
1424 ret = platform_driver_register(&applesmc_driver);
1425 if (ret)
1426 goto out_region;
1428 pdev = platform_device_register_simple("applesmc", APPLESMC_DATA_PORT,
1429 NULL, 0);
1430 if (IS_ERR(pdev)) {
1431 ret = PTR_ERR(pdev);
1432 goto out_driver;
1435 /* create register cache */
1436 ret = applesmc_init_smcreg();
1437 if (ret)
1438 goto out_device;
1440 ret = sysfs_create_file(&pdev->dev.kobj, &dev_attr_name.attr);
1441 if (ret)
1442 goto out_smcreg;
1444 /* Create key enumeration sysfs files */
1445 ret = sysfs_create_group(&pdev->dev.kobj, &key_enumeration_group);
1446 if (ret)
1447 goto out_name;
1449 /* create fan files */
1450 count = applesmc_get_fan_count();
1451 if (count < 0)
1452 pr_err("Cannot get the number of fans\n");
1453 else
1454 pr_info("%d fans found\n", count);
1456 if (count > 4) {
1457 count = 4;
1458 pr_warn("A maximum of 4 fans are supported by this driver\n");
1461 while (fans_handled < count) {
1462 ret = sysfs_create_group(&pdev->dev.kobj,
1463 &fan_attribute_groups[fans_handled]);
1464 if (ret)
1465 goto out_fans;
1466 fans_handled++;
1469 ret = applesmc_create_nodes(temp_group, smcreg.temp_count);
1470 if (ret)
1471 goto out_fans;
1473 if (applesmc_accelerometer) {
1474 ret = applesmc_create_accelerometer();
1475 if (ret)
1476 goto out_temperature;
1479 if (applesmc_light) {
1480 /* Add light sensor file */
1481 ret = sysfs_create_file(&pdev->dev.kobj, &dev_attr_light.attr);
1482 if (ret)
1483 goto out_accelerometer;
1485 /* Create the workqueue */
1486 applesmc_led_wq = create_singlethread_workqueue("applesmc-led");
1487 if (!applesmc_led_wq) {
1488 ret = -ENOMEM;
1489 goto out_light_sysfs;
1492 /* register as a led device */
1493 ret = led_classdev_register(&pdev->dev, &applesmc_backlight);
1494 if (ret < 0)
1495 goto out_light_wq;
1498 hwmon_dev = hwmon_device_register(&pdev->dev);
1499 if (IS_ERR(hwmon_dev)) {
1500 ret = PTR_ERR(hwmon_dev);
1501 goto out_light_ledclass;
1504 pr_info("driver successfully loaded\n");
1506 return 0;
1508 out_light_ledclass:
1509 if (applesmc_light)
1510 led_classdev_unregister(&applesmc_backlight);
1511 out_light_wq:
1512 if (applesmc_light)
1513 destroy_workqueue(applesmc_led_wq);
1514 out_light_sysfs:
1515 if (applesmc_light)
1516 sysfs_remove_file(&pdev->dev.kobj, &dev_attr_light.attr);
1517 out_accelerometer:
1518 if (applesmc_accelerometer)
1519 applesmc_release_accelerometer();
1520 out_temperature:
1521 applesmc_destroy_nodes(temp_group);
1522 out_fans:
1523 while (fans_handled)
1524 sysfs_remove_group(&pdev->dev.kobj,
1525 &fan_attribute_groups[--fans_handled]);
1526 sysfs_remove_group(&pdev->dev.kobj, &key_enumeration_group);
1527 out_name:
1528 sysfs_remove_file(&pdev->dev.kobj, &dev_attr_name.attr);
1529 out_smcreg:
1530 applesmc_destroy_smcreg();
1531 out_device:
1532 platform_device_unregister(pdev);
1533 out_driver:
1534 platform_driver_unregister(&applesmc_driver);
1535 out_region:
1536 release_region(APPLESMC_DATA_PORT, APPLESMC_NR_PORTS);
1537 out:
1538 pr_warn("driver init failed (ret=%d)!\n", ret);
1539 return ret;
1542 static void __exit applesmc_exit(void)
1544 hwmon_device_unregister(hwmon_dev);
1545 if (applesmc_light) {
1546 led_classdev_unregister(&applesmc_backlight);
1547 destroy_workqueue(applesmc_led_wq);
1548 sysfs_remove_file(&pdev->dev.kobj, &dev_attr_light.attr);
1550 if (applesmc_accelerometer)
1551 applesmc_release_accelerometer();
1552 applesmc_destroy_nodes(temp_group);
1553 while (fans_handled)
1554 sysfs_remove_group(&pdev->dev.kobj,
1555 &fan_attribute_groups[--fans_handled]);
1556 sysfs_remove_group(&pdev->dev.kobj, &key_enumeration_group);
1557 sysfs_remove_file(&pdev->dev.kobj, &dev_attr_name.attr);
1558 applesmc_destroy_smcreg();
1559 platform_device_unregister(pdev);
1560 platform_driver_unregister(&applesmc_driver);
1561 release_region(APPLESMC_DATA_PORT, APPLESMC_NR_PORTS);
1563 pr_info("driver unloaded\n");
1566 module_init(applesmc_init);
1567 module_exit(applesmc_exit);
1569 MODULE_AUTHOR("Nicolas Boichat");
1570 MODULE_DESCRIPTION("Apple SMC");
1571 MODULE_LICENSE("GPL v2");
1572 MODULE_DEVICE_TABLE(dmi, applesmc_whitelist);