hwmon: (applesmc) Handle new temperature format
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / hwmon / applesmc.c
blob1d7f8aff998222e3f2bbdf09ab9b9226df959cfc
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);
737 if (entry->len > 2)
738 return -EINVAL;
740 ret = applesmc_read_entry(entry, buffer, entry->len);
741 if (ret)
742 return ret;
744 if (entry->len == 2) {
745 temp = buffer[0] * 1000;
746 temp += (buffer[1] >> 6) * 250;
747 } else {
748 temp = buffer[0] * 4000;
751 return snprintf(sysfsbuf, PAGE_SIZE, "%u\n", temp);
754 static ssize_t applesmc_show_fan_speed(struct device *dev,
755 struct device_attribute *attr, char *sysfsbuf)
757 int ret;
758 unsigned int speed = 0;
759 char newkey[5];
760 u8 buffer[2];
761 struct sensor_device_attribute_2 *sensor_attr =
762 to_sensor_dev_attr_2(attr);
764 newkey[0] = fan_speed_keys[sensor_attr->nr][0];
765 newkey[1] = '0' + sensor_attr->index;
766 newkey[2] = fan_speed_keys[sensor_attr->nr][2];
767 newkey[3] = fan_speed_keys[sensor_attr->nr][3];
768 newkey[4] = 0;
770 ret = applesmc_read_key(newkey, buffer, 2);
771 speed = ((buffer[0] << 8 | buffer[1]) >> 2);
773 if (ret)
774 return ret;
775 else
776 return snprintf(sysfsbuf, PAGE_SIZE, "%u\n", speed);
779 static ssize_t applesmc_store_fan_speed(struct device *dev,
780 struct device_attribute *attr,
781 const char *sysfsbuf, size_t count)
783 int ret;
784 u32 speed;
785 char newkey[5];
786 u8 buffer[2];
787 struct sensor_device_attribute_2 *sensor_attr =
788 to_sensor_dev_attr_2(attr);
790 speed = simple_strtoul(sysfsbuf, NULL, 10);
792 if (speed > 0x4000) /* Bigger than a 14-bit value */
793 return -EINVAL;
795 newkey[0] = fan_speed_keys[sensor_attr->nr][0];
796 newkey[1] = '0' + sensor_attr->index;
797 newkey[2] = fan_speed_keys[sensor_attr->nr][2];
798 newkey[3] = fan_speed_keys[sensor_attr->nr][3];
799 newkey[4] = 0;
801 buffer[0] = (speed >> 6) & 0xff;
802 buffer[1] = (speed << 2) & 0xff;
803 ret = applesmc_write_key(newkey, buffer, 2);
805 if (ret)
806 return ret;
807 else
808 return count;
811 static ssize_t applesmc_show_fan_manual(struct device *dev,
812 struct device_attribute *devattr, char *sysfsbuf)
814 int ret;
815 u16 manual = 0;
816 u8 buffer[2];
817 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
819 ret = applesmc_read_key(FANS_MANUAL, buffer, 2);
820 manual = ((buffer[0] << 8 | buffer[1]) >> attr->index) & 0x01;
822 if (ret)
823 return ret;
824 else
825 return snprintf(sysfsbuf, PAGE_SIZE, "%d\n", manual);
828 static ssize_t applesmc_store_fan_manual(struct device *dev,
829 struct device_attribute *devattr,
830 const char *sysfsbuf, size_t count)
832 int ret;
833 u8 buffer[2];
834 u32 input;
835 u16 val;
836 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
838 input = simple_strtoul(sysfsbuf, NULL, 10);
840 ret = applesmc_read_key(FANS_MANUAL, buffer, 2);
841 val = (buffer[0] << 8 | buffer[1]);
842 if (ret)
843 goto out;
845 if (input)
846 val = val | (0x01 << attr->index);
847 else
848 val = val & ~(0x01 << attr->index);
850 buffer[0] = (val >> 8) & 0xFF;
851 buffer[1] = val & 0xFF;
853 ret = applesmc_write_key(FANS_MANUAL, buffer, 2);
855 out:
856 if (ret)
857 return ret;
858 else
859 return count;
862 static ssize_t applesmc_show_fan_position(struct device *dev,
863 struct device_attribute *attr, char *sysfsbuf)
865 int ret;
866 char newkey[5];
867 u8 buffer[17];
868 struct sensor_device_attribute_2 *sensor_attr =
869 to_sensor_dev_attr_2(attr);
871 newkey[0] = FAN_POSITION[0];
872 newkey[1] = '0' + sensor_attr->index;
873 newkey[2] = FAN_POSITION[2];
874 newkey[3] = FAN_POSITION[3];
875 newkey[4] = 0;
877 ret = applesmc_read_key(newkey, buffer, 16);
878 buffer[16] = 0;
880 if (ret)
881 return ret;
882 else
883 return snprintf(sysfsbuf, PAGE_SIZE, "%s\n", buffer+4);
886 static ssize_t applesmc_calibrate_show(struct device *dev,
887 struct device_attribute *attr, char *sysfsbuf)
889 return snprintf(sysfsbuf, PAGE_SIZE, "(%d,%d)\n", rest_x, rest_y);
892 static ssize_t applesmc_calibrate_store(struct device *dev,
893 struct device_attribute *attr, const char *sysfsbuf, size_t count)
895 applesmc_calibrate();
897 return count;
900 static void applesmc_backlight_set(struct work_struct *work)
902 applesmc_write_key(BACKLIGHT_KEY, backlight_state, 2);
904 static DECLARE_WORK(backlight_work, &applesmc_backlight_set);
906 static void applesmc_brightness_set(struct led_classdev *led_cdev,
907 enum led_brightness value)
909 int ret;
911 backlight_state[0] = value;
912 ret = queue_work(applesmc_led_wq, &backlight_work);
914 if (debug && (!ret))
915 printk(KERN_DEBUG "applesmc: work was already on the queue.\n");
918 static ssize_t applesmc_key_count_show(struct device *dev,
919 struct device_attribute *attr, char *sysfsbuf)
921 int ret;
922 u8 buffer[4];
923 u32 count;
925 ret = applesmc_read_key(KEY_COUNT_KEY, buffer, 4);
926 count = ((u32)buffer[0]<<24) + ((u32)buffer[1]<<16) +
927 ((u32)buffer[2]<<8) + buffer[3];
929 if (ret)
930 return ret;
931 else
932 return snprintf(sysfsbuf, PAGE_SIZE, "%d\n", count);
935 static ssize_t applesmc_key_at_index_read_show(struct device *dev,
936 struct device_attribute *attr, char *sysfsbuf)
938 const struct applesmc_entry *entry;
939 int ret;
941 entry = applesmc_get_entry_by_index(key_at_index);
942 if (IS_ERR(entry))
943 return PTR_ERR(entry);
944 ret = applesmc_read_entry(entry, sysfsbuf, entry->len);
945 if (ret)
946 return ret;
948 return entry->len;
951 static ssize_t applesmc_key_at_index_data_length_show(struct device *dev,
952 struct device_attribute *attr, char *sysfsbuf)
954 const struct applesmc_entry *entry;
956 entry = applesmc_get_entry_by_index(key_at_index);
957 if (IS_ERR(entry))
958 return PTR_ERR(entry);
960 return snprintf(sysfsbuf, PAGE_SIZE, "%d\n", entry->len);
963 static ssize_t applesmc_key_at_index_type_show(struct device *dev,
964 struct device_attribute *attr, char *sysfsbuf)
966 const struct applesmc_entry *entry;
968 entry = applesmc_get_entry_by_index(key_at_index);
969 if (IS_ERR(entry))
970 return PTR_ERR(entry);
972 return snprintf(sysfsbuf, PAGE_SIZE, "%s\n", entry->type);
975 static ssize_t applesmc_key_at_index_name_show(struct device *dev,
976 struct device_attribute *attr, char *sysfsbuf)
978 const struct applesmc_entry *entry;
980 entry = applesmc_get_entry_by_index(key_at_index);
981 if (IS_ERR(entry))
982 return PTR_ERR(entry);
984 return snprintf(sysfsbuf, PAGE_SIZE, "%s\n", entry->key);
987 static ssize_t applesmc_key_at_index_show(struct device *dev,
988 struct device_attribute *attr, char *sysfsbuf)
990 return snprintf(sysfsbuf, PAGE_SIZE, "%d\n", key_at_index);
993 static ssize_t applesmc_key_at_index_store(struct device *dev,
994 struct device_attribute *attr, const char *sysfsbuf, size_t count)
996 unsigned long newkey;
998 if (strict_strtoul(sysfsbuf, 10, &newkey) < 0
999 || newkey >= smcreg.key_count)
1000 return -EINVAL;
1002 key_at_index = newkey;
1003 return count;
1006 static struct led_classdev applesmc_backlight = {
1007 .name = "smc::kbd_backlight",
1008 .default_trigger = "nand-disk",
1009 .brightness_set = applesmc_brightness_set,
1012 static DEVICE_ATTR(name, 0444, applesmc_name_show, NULL);
1014 static DEVICE_ATTR(position, 0444, applesmc_position_show, NULL);
1015 static DEVICE_ATTR(calibrate, 0644,
1016 applesmc_calibrate_show, applesmc_calibrate_store);
1018 static struct attribute *accelerometer_attributes[] = {
1019 &dev_attr_position.attr,
1020 &dev_attr_calibrate.attr,
1021 NULL
1024 static const struct attribute_group accelerometer_attributes_group =
1025 { .attrs = accelerometer_attributes };
1027 static DEVICE_ATTR(light, 0444, applesmc_light_show, NULL);
1029 static DEVICE_ATTR(key_count, 0444, applesmc_key_count_show, NULL);
1030 static DEVICE_ATTR(key_at_index, 0644,
1031 applesmc_key_at_index_show, applesmc_key_at_index_store);
1032 static DEVICE_ATTR(key_at_index_name, 0444,
1033 applesmc_key_at_index_name_show, NULL);
1034 static DEVICE_ATTR(key_at_index_type, 0444,
1035 applesmc_key_at_index_type_show, NULL);
1036 static DEVICE_ATTR(key_at_index_data_length, 0444,
1037 applesmc_key_at_index_data_length_show, NULL);
1038 static DEVICE_ATTR(key_at_index_data, 0444,
1039 applesmc_key_at_index_read_show, NULL);
1041 static struct attribute *key_enumeration_attributes[] = {
1042 &dev_attr_key_count.attr,
1043 &dev_attr_key_at_index.attr,
1044 &dev_attr_key_at_index_name.attr,
1045 &dev_attr_key_at_index_type.attr,
1046 &dev_attr_key_at_index_data_length.attr,
1047 &dev_attr_key_at_index_data.attr,
1048 NULL
1051 static const struct attribute_group key_enumeration_group =
1052 { .attrs = key_enumeration_attributes };
1055 * Macro defining SENSOR_DEVICE_ATTR for a fan sysfs entries.
1056 * - show actual speed
1057 * - show/store minimum speed
1058 * - show maximum speed
1059 * - show safe speed
1060 * - show/store target speed
1061 * - show/store manual mode
1063 #define sysfs_fan_speeds_offset(offset) \
1064 static SENSOR_DEVICE_ATTR_2(fan##offset##_input, S_IRUGO, \
1065 applesmc_show_fan_speed, NULL, 0, offset-1); \
1067 static SENSOR_DEVICE_ATTR_2(fan##offset##_min, S_IRUGO | S_IWUSR, \
1068 applesmc_show_fan_speed, applesmc_store_fan_speed, 1, offset-1); \
1070 static SENSOR_DEVICE_ATTR_2(fan##offset##_max, S_IRUGO, \
1071 applesmc_show_fan_speed, NULL, 2, offset-1); \
1073 static SENSOR_DEVICE_ATTR_2(fan##offset##_safe, S_IRUGO, \
1074 applesmc_show_fan_speed, NULL, 3, offset-1); \
1076 static SENSOR_DEVICE_ATTR_2(fan##offset##_output, S_IRUGO | S_IWUSR, \
1077 applesmc_show_fan_speed, applesmc_store_fan_speed, 4, offset-1); \
1079 static SENSOR_DEVICE_ATTR(fan##offset##_manual, S_IRUGO | S_IWUSR, \
1080 applesmc_show_fan_manual, applesmc_store_fan_manual, offset-1); \
1082 static SENSOR_DEVICE_ATTR(fan##offset##_label, S_IRUGO, \
1083 applesmc_show_fan_position, NULL, offset-1); \
1085 static struct attribute *fan##offset##_attributes[] = { \
1086 &sensor_dev_attr_fan##offset##_input.dev_attr.attr, \
1087 &sensor_dev_attr_fan##offset##_min.dev_attr.attr, \
1088 &sensor_dev_attr_fan##offset##_max.dev_attr.attr, \
1089 &sensor_dev_attr_fan##offset##_safe.dev_attr.attr, \
1090 &sensor_dev_attr_fan##offset##_output.dev_attr.attr, \
1091 &sensor_dev_attr_fan##offset##_manual.dev_attr.attr, \
1092 &sensor_dev_attr_fan##offset##_label.dev_attr.attr, \
1093 NULL \
1097 * Create the needed functions for each fan using the macro defined above
1098 * (4 fans are supported)
1100 sysfs_fan_speeds_offset(1);
1101 sysfs_fan_speeds_offset(2);
1102 sysfs_fan_speeds_offset(3);
1103 sysfs_fan_speeds_offset(4);
1105 static const struct attribute_group fan_attribute_groups[] = {
1106 { .attrs = fan1_attributes },
1107 { .attrs = fan2_attributes },
1108 { .attrs = fan3_attributes },
1109 { .attrs = fan4_attributes },
1112 static struct applesmc_node_group temp_group[] = {
1113 { "temp%d_label", applesmc_show_sensor_label },
1114 { "temp%d_input", applesmc_show_temperature },
1118 /* Module stuff */
1121 * applesmc_dmi_match - found a match. return one, short-circuiting the hunt.
1123 static int applesmc_dmi_match(const struct dmi_system_id *id)
1125 struct dmi_match_data* dmi_data = id->driver_data;
1126 pr_info("%s detected:\n", id->ident);
1127 applesmc_accelerometer = dmi_data->accelerometer;
1128 pr_info(" - Model %s accelerometer\n",
1129 applesmc_accelerometer ? "with" : "without");
1130 applesmc_light = dmi_data->light;
1131 pr_info(" - Model %s light sensors and backlight\n",
1132 applesmc_light ? "with" : "without");
1134 return 1;
1138 * applesmc_destroy_nodes - remove files and free associated memory
1140 static void applesmc_destroy_nodes(struct applesmc_node_group *groups)
1142 struct applesmc_node_group *grp;
1143 struct applesmc_dev_attr *node;
1145 for (grp = groups; grp->nodes; grp++) {
1146 for (node = grp->nodes; node->sda.dev_attr.attr.name; node++)
1147 sysfs_remove_file(&pdev->dev.kobj,
1148 &node->sda.dev_attr.attr);
1149 kfree(grp->nodes);
1150 grp->nodes = NULL;
1155 * applesmc_create_nodes - create a two-dimensional group of sysfs files
1157 static int applesmc_create_nodes(struct applesmc_node_group *groups, int num)
1159 struct applesmc_node_group *grp;
1160 struct applesmc_dev_attr *node;
1161 struct attribute *attr;
1162 int ret, i;
1164 for (grp = groups; grp->format; grp++) {
1165 grp->nodes = kcalloc(num + 1, sizeof(*node), GFP_KERNEL);
1166 if (!grp->nodes) {
1167 ret = -ENOMEM;
1168 goto out;
1170 for (i = 0; i < num; i++) {
1171 node = &grp->nodes[i];
1172 sprintf(node->name, grp->format, i + 1);
1173 node->sda.index = i;
1174 node->sda.dev_attr.show = grp->show;
1175 node->sda.dev_attr.store = grp->store;
1176 attr = &node->sda.dev_attr.attr;
1177 attr->name = node->name;
1178 attr->mode = S_IRUGO | (grp->store ? S_IWUSR : 0);
1179 ret = sysfs_create_file(&pdev->dev.kobj, attr);
1180 if (ret) {
1181 attr->name = NULL;
1182 goto out;
1187 return 0;
1188 out:
1189 applesmc_destroy_nodes(groups);
1190 return ret;
1193 /* Create accelerometer ressources */
1194 static int applesmc_create_accelerometer(void)
1196 struct input_dev *idev;
1197 int ret;
1199 ret = sysfs_create_group(&pdev->dev.kobj,
1200 &accelerometer_attributes_group);
1201 if (ret)
1202 goto out;
1204 applesmc_idev = input_allocate_polled_device();
1205 if (!applesmc_idev) {
1206 ret = -ENOMEM;
1207 goto out_sysfs;
1210 applesmc_idev->poll = applesmc_idev_poll;
1211 applesmc_idev->poll_interval = APPLESMC_POLL_INTERVAL;
1213 /* initial calibrate for the input device */
1214 applesmc_calibrate();
1216 /* initialize the input device */
1217 idev = applesmc_idev->input;
1218 idev->name = "applesmc";
1219 idev->id.bustype = BUS_HOST;
1220 idev->dev.parent = &pdev->dev;
1221 idev->evbit[0] = BIT_MASK(EV_ABS);
1222 input_set_abs_params(idev, ABS_X,
1223 -256, 256, APPLESMC_INPUT_FUZZ, APPLESMC_INPUT_FLAT);
1224 input_set_abs_params(idev, ABS_Y,
1225 -256, 256, APPLESMC_INPUT_FUZZ, APPLESMC_INPUT_FLAT);
1227 ret = input_register_polled_device(applesmc_idev);
1228 if (ret)
1229 goto out_idev;
1231 return 0;
1233 out_idev:
1234 input_free_polled_device(applesmc_idev);
1236 out_sysfs:
1237 sysfs_remove_group(&pdev->dev.kobj, &accelerometer_attributes_group);
1239 out:
1240 pr_warn("driver init failed (ret=%d)!\n", ret);
1241 return ret;
1244 /* Release all ressources used by the accelerometer */
1245 static void applesmc_release_accelerometer(void)
1247 input_unregister_polled_device(applesmc_idev);
1248 input_free_polled_device(applesmc_idev);
1249 sysfs_remove_group(&pdev->dev.kobj, &accelerometer_attributes_group);
1252 static __initdata struct dmi_match_data applesmc_dmi_data[] = {
1253 /* MacBook Pro: accelerometer, backlight and temperature set 0 */
1254 { .accelerometer = 1, .light = 1, .temperature_set = 0 },
1255 /* MacBook2: accelerometer and temperature set 1 */
1256 { .accelerometer = 1, .light = 0, .temperature_set = 1 },
1257 /* MacBook: accelerometer and temperature set 2 */
1258 { .accelerometer = 1, .light = 0, .temperature_set = 2 },
1259 /* MacMini: temperature set 3 */
1260 { .accelerometer = 0, .light = 0, .temperature_set = 3 },
1261 /* MacPro: temperature set 4 */
1262 { .accelerometer = 0, .light = 0, .temperature_set = 4 },
1263 /* iMac: temperature set 5 */
1264 { .accelerometer = 0, .light = 0, .temperature_set = 5 },
1265 /* MacBook3, MacBook4: accelerometer and temperature set 6 */
1266 { .accelerometer = 1, .light = 0, .temperature_set = 6 },
1267 /* MacBook Air: accelerometer, backlight and temperature set 7 */
1268 { .accelerometer = 1, .light = 1, .temperature_set = 7 },
1269 /* MacBook Pro 4: accelerometer, backlight and temperature set 8 */
1270 { .accelerometer = 1, .light = 1, .temperature_set = 8 },
1271 /* MacBook Pro 3: accelerometer, backlight and temperature set 9 */
1272 { .accelerometer = 1, .light = 1, .temperature_set = 9 },
1273 /* iMac 5: light sensor only, temperature set 10 */
1274 { .accelerometer = 0, .light = 0, .temperature_set = 10 },
1275 /* MacBook 5: accelerometer, backlight and temperature set 11 */
1276 { .accelerometer = 1, .light = 1, .temperature_set = 11 },
1277 /* MacBook Pro 5: accelerometer, backlight and temperature set 12 */
1278 { .accelerometer = 1, .light = 1, .temperature_set = 12 },
1279 /* iMac 8: light sensor only, temperature set 13 */
1280 { .accelerometer = 0, .light = 0, .temperature_set = 13 },
1281 /* iMac 6: light sensor only, temperature set 14 */
1282 { .accelerometer = 0, .light = 0, .temperature_set = 14 },
1283 /* MacBook Air 2,1: accelerometer, backlight and temperature set 15 */
1284 { .accelerometer = 1, .light = 1, .temperature_set = 15 },
1285 /* MacPro3,1: temperature set 16 */
1286 { .accelerometer = 0, .light = 0, .temperature_set = 16 },
1287 /* iMac 9,1: light sensor only, temperature set 17 */
1288 { .accelerometer = 0, .light = 0, .temperature_set = 17 },
1289 /* MacBook Pro 2,2: accelerometer, backlight and temperature set 18 */
1290 { .accelerometer = 1, .light = 1, .temperature_set = 18 },
1291 /* MacBook Pro 5,3: accelerometer, backlight and temperature set 19 */
1292 { .accelerometer = 1, .light = 1, .temperature_set = 19 },
1293 /* MacBook Pro 5,4: accelerometer, backlight and temperature set 20 */
1294 { .accelerometer = 1, .light = 1, .temperature_set = 20 },
1295 /* MacBook Pro 6,2: accelerometer, backlight and temperature set 21 */
1296 { .accelerometer = 1, .light = 1, .temperature_set = 21 },
1297 /* MacBook Pro 7,1: accelerometer, backlight and temperature set 22 */
1298 { .accelerometer = 1, .light = 1, .temperature_set = 22 },
1299 /* MacBook Air 3,1: accelerometer, backlight and temperature set 23 */
1300 { .accelerometer = 0, .light = 0, .temperature_set = 23 },
1303 /* Note that DMI_MATCH(...,"MacBook") will match "MacBookPro1,1".
1304 * So we need to put "Apple MacBook Pro" before "Apple MacBook". */
1305 static __initdata struct dmi_system_id applesmc_whitelist[] = {
1306 { applesmc_dmi_match, "Apple MacBook Air 3", {
1307 DMI_MATCH(DMI_BOARD_VENDOR, "Apple"),
1308 DMI_MATCH(DMI_PRODUCT_NAME, "MacBookAir3") },
1309 &applesmc_dmi_data[23]},
1310 { applesmc_dmi_match, "Apple MacBook Air 2", {
1311 DMI_MATCH(DMI_BOARD_VENDOR, "Apple"),
1312 DMI_MATCH(DMI_PRODUCT_NAME, "MacBookAir2") },
1313 &applesmc_dmi_data[15]},
1314 { applesmc_dmi_match, "Apple MacBook Air", {
1315 DMI_MATCH(DMI_BOARD_VENDOR, "Apple"),
1316 DMI_MATCH(DMI_PRODUCT_NAME, "MacBookAir") },
1317 &applesmc_dmi_data[7]},
1318 { applesmc_dmi_match, "Apple MacBook Pro 7", {
1319 DMI_MATCH(DMI_BOARD_VENDOR, "Apple"),
1320 DMI_MATCH(DMI_PRODUCT_NAME, "MacBookPro7") },
1321 &applesmc_dmi_data[22]},
1322 { applesmc_dmi_match, "Apple MacBook Pro 5,4", {
1323 DMI_MATCH(DMI_BOARD_VENDOR, "Apple"),
1324 DMI_MATCH(DMI_PRODUCT_NAME, "MacBookPro5,4") },
1325 &applesmc_dmi_data[20]},
1326 { applesmc_dmi_match, "Apple MacBook Pro 5,3", {
1327 DMI_MATCH(DMI_BOARD_VENDOR, "Apple"),
1328 DMI_MATCH(DMI_PRODUCT_NAME, "MacBookPro5,3") },
1329 &applesmc_dmi_data[19]},
1330 { applesmc_dmi_match, "Apple MacBook Pro 6", {
1331 DMI_MATCH(DMI_BOARD_VENDOR, "Apple"),
1332 DMI_MATCH(DMI_PRODUCT_NAME, "MacBookPro6") },
1333 &applesmc_dmi_data[21]},
1334 { applesmc_dmi_match, "Apple MacBook Pro 5", {
1335 DMI_MATCH(DMI_BOARD_VENDOR, "Apple"),
1336 DMI_MATCH(DMI_PRODUCT_NAME, "MacBookPro5") },
1337 &applesmc_dmi_data[12]},
1338 { applesmc_dmi_match, "Apple MacBook Pro 4", {
1339 DMI_MATCH(DMI_BOARD_VENDOR, "Apple"),
1340 DMI_MATCH(DMI_PRODUCT_NAME, "MacBookPro4") },
1341 &applesmc_dmi_data[8]},
1342 { applesmc_dmi_match, "Apple MacBook Pro 3", {
1343 DMI_MATCH(DMI_BOARD_VENDOR, "Apple"),
1344 DMI_MATCH(DMI_PRODUCT_NAME, "MacBookPro3") },
1345 &applesmc_dmi_data[9]},
1346 { applesmc_dmi_match, "Apple MacBook Pro 2,2", {
1347 DMI_MATCH(DMI_BOARD_VENDOR, "Apple Computer, Inc."),
1348 DMI_MATCH(DMI_PRODUCT_NAME, "MacBookPro2,2") },
1349 &applesmc_dmi_data[18]},
1350 { applesmc_dmi_match, "Apple MacBook Pro", {
1351 DMI_MATCH(DMI_BOARD_VENDOR,"Apple"),
1352 DMI_MATCH(DMI_PRODUCT_NAME,"MacBookPro") },
1353 &applesmc_dmi_data[0]},
1354 { applesmc_dmi_match, "Apple MacBook (v2)", {
1355 DMI_MATCH(DMI_BOARD_VENDOR,"Apple"),
1356 DMI_MATCH(DMI_PRODUCT_NAME,"MacBook2") },
1357 &applesmc_dmi_data[1]},
1358 { applesmc_dmi_match, "Apple MacBook (v3)", {
1359 DMI_MATCH(DMI_BOARD_VENDOR,"Apple"),
1360 DMI_MATCH(DMI_PRODUCT_NAME,"MacBook3") },
1361 &applesmc_dmi_data[6]},
1362 { applesmc_dmi_match, "Apple MacBook 4", {
1363 DMI_MATCH(DMI_BOARD_VENDOR, "Apple"),
1364 DMI_MATCH(DMI_PRODUCT_NAME, "MacBook4") },
1365 &applesmc_dmi_data[6]},
1366 { applesmc_dmi_match, "Apple MacBook 5", {
1367 DMI_MATCH(DMI_BOARD_VENDOR, "Apple"),
1368 DMI_MATCH(DMI_PRODUCT_NAME, "MacBook5") },
1369 &applesmc_dmi_data[11]},
1370 { applesmc_dmi_match, "Apple MacBook", {
1371 DMI_MATCH(DMI_BOARD_VENDOR,"Apple"),
1372 DMI_MATCH(DMI_PRODUCT_NAME,"MacBook") },
1373 &applesmc_dmi_data[2]},
1374 { applesmc_dmi_match, "Apple Macmini", {
1375 DMI_MATCH(DMI_BOARD_VENDOR,"Apple"),
1376 DMI_MATCH(DMI_PRODUCT_NAME,"Macmini") },
1377 &applesmc_dmi_data[3]},
1378 { applesmc_dmi_match, "Apple MacPro2", {
1379 DMI_MATCH(DMI_BOARD_VENDOR,"Apple"),
1380 DMI_MATCH(DMI_PRODUCT_NAME,"MacPro2") },
1381 &applesmc_dmi_data[4]},
1382 { applesmc_dmi_match, "Apple MacPro3", {
1383 DMI_MATCH(DMI_BOARD_VENDOR, "Apple"),
1384 DMI_MATCH(DMI_PRODUCT_NAME, "MacPro3") },
1385 &applesmc_dmi_data[16]},
1386 { applesmc_dmi_match, "Apple MacPro", {
1387 DMI_MATCH(DMI_BOARD_VENDOR, "Apple"),
1388 DMI_MATCH(DMI_PRODUCT_NAME, "MacPro") },
1389 &applesmc_dmi_data[4]},
1390 { applesmc_dmi_match, "Apple iMac 9,1", {
1391 DMI_MATCH(DMI_BOARD_VENDOR, "Apple Inc."),
1392 DMI_MATCH(DMI_PRODUCT_NAME, "iMac9,1") },
1393 &applesmc_dmi_data[17]},
1394 { applesmc_dmi_match, "Apple iMac 8", {
1395 DMI_MATCH(DMI_BOARD_VENDOR, "Apple"),
1396 DMI_MATCH(DMI_PRODUCT_NAME, "iMac8") },
1397 &applesmc_dmi_data[13]},
1398 { applesmc_dmi_match, "Apple iMac 6", {
1399 DMI_MATCH(DMI_BOARD_VENDOR, "Apple"),
1400 DMI_MATCH(DMI_PRODUCT_NAME, "iMac6") },
1401 &applesmc_dmi_data[14]},
1402 { applesmc_dmi_match, "Apple iMac 5", {
1403 DMI_MATCH(DMI_BOARD_VENDOR, "Apple"),
1404 DMI_MATCH(DMI_PRODUCT_NAME, "iMac5") },
1405 &applesmc_dmi_data[10]},
1406 { applesmc_dmi_match, "Apple iMac", {
1407 DMI_MATCH(DMI_BOARD_VENDOR,"Apple"),
1408 DMI_MATCH(DMI_PRODUCT_NAME,"iMac") },
1409 &applesmc_dmi_data[5]},
1410 { .ident = NULL }
1413 static int __init applesmc_init(void)
1415 int ret;
1416 int count;
1418 if (!dmi_check_system(applesmc_whitelist)) {
1419 pr_warn("supported laptop not found!\n");
1420 ret = -ENODEV;
1421 goto out;
1424 if (!request_region(APPLESMC_DATA_PORT, APPLESMC_NR_PORTS,
1425 "applesmc")) {
1426 ret = -ENXIO;
1427 goto out;
1430 ret = platform_driver_register(&applesmc_driver);
1431 if (ret)
1432 goto out_region;
1434 pdev = platform_device_register_simple("applesmc", APPLESMC_DATA_PORT,
1435 NULL, 0);
1436 if (IS_ERR(pdev)) {
1437 ret = PTR_ERR(pdev);
1438 goto out_driver;
1441 /* create register cache */
1442 ret = applesmc_init_smcreg();
1443 if (ret)
1444 goto out_device;
1446 ret = sysfs_create_file(&pdev->dev.kobj, &dev_attr_name.attr);
1447 if (ret)
1448 goto out_smcreg;
1450 /* Create key enumeration sysfs files */
1451 ret = sysfs_create_group(&pdev->dev.kobj, &key_enumeration_group);
1452 if (ret)
1453 goto out_name;
1455 /* create fan files */
1456 count = applesmc_get_fan_count();
1457 if (count < 0)
1458 pr_err("Cannot get the number of fans\n");
1459 else
1460 pr_info("%d fans found\n", count);
1462 if (count > 4) {
1463 count = 4;
1464 pr_warn("A maximum of 4 fans are supported by this driver\n");
1467 while (fans_handled < count) {
1468 ret = sysfs_create_group(&pdev->dev.kobj,
1469 &fan_attribute_groups[fans_handled]);
1470 if (ret)
1471 goto out_fans;
1472 fans_handled++;
1475 ret = applesmc_create_nodes(temp_group, smcreg.temp_count);
1476 if (ret)
1477 goto out_fans;
1479 if (applesmc_accelerometer) {
1480 ret = applesmc_create_accelerometer();
1481 if (ret)
1482 goto out_temperature;
1485 if (applesmc_light) {
1486 /* Add light sensor file */
1487 ret = sysfs_create_file(&pdev->dev.kobj, &dev_attr_light.attr);
1488 if (ret)
1489 goto out_accelerometer;
1491 /* Create the workqueue */
1492 applesmc_led_wq = create_singlethread_workqueue("applesmc-led");
1493 if (!applesmc_led_wq) {
1494 ret = -ENOMEM;
1495 goto out_light_sysfs;
1498 /* register as a led device */
1499 ret = led_classdev_register(&pdev->dev, &applesmc_backlight);
1500 if (ret < 0)
1501 goto out_light_wq;
1504 hwmon_dev = hwmon_device_register(&pdev->dev);
1505 if (IS_ERR(hwmon_dev)) {
1506 ret = PTR_ERR(hwmon_dev);
1507 goto out_light_ledclass;
1510 pr_info("driver successfully loaded\n");
1512 return 0;
1514 out_light_ledclass:
1515 if (applesmc_light)
1516 led_classdev_unregister(&applesmc_backlight);
1517 out_light_wq:
1518 if (applesmc_light)
1519 destroy_workqueue(applesmc_led_wq);
1520 out_light_sysfs:
1521 if (applesmc_light)
1522 sysfs_remove_file(&pdev->dev.kobj, &dev_attr_light.attr);
1523 out_accelerometer:
1524 if (applesmc_accelerometer)
1525 applesmc_release_accelerometer();
1526 out_temperature:
1527 applesmc_destroy_nodes(temp_group);
1528 out_fans:
1529 while (fans_handled)
1530 sysfs_remove_group(&pdev->dev.kobj,
1531 &fan_attribute_groups[--fans_handled]);
1532 sysfs_remove_group(&pdev->dev.kobj, &key_enumeration_group);
1533 out_name:
1534 sysfs_remove_file(&pdev->dev.kobj, &dev_attr_name.attr);
1535 out_smcreg:
1536 applesmc_destroy_smcreg();
1537 out_device:
1538 platform_device_unregister(pdev);
1539 out_driver:
1540 platform_driver_unregister(&applesmc_driver);
1541 out_region:
1542 release_region(APPLESMC_DATA_PORT, APPLESMC_NR_PORTS);
1543 out:
1544 pr_warn("driver init failed (ret=%d)!\n", ret);
1545 return ret;
1548 static void __exit applesmc_exit(void)
1550 hwmon_device_unregister(hwmon_dev);
1551 if (applesmc_light) {
1552 led_classdev_unregister(&applesmc_backlight);
1553 destroy_workqueue(applesmc_led_wq);
1554 sysfs_remove_file(&pdev->dev.kobj, &dev_attr_light.attr);
1556 if (applesmc_accelerometer)
1557 applesmc_release_accelerometer();
1558 applesmc_destroy_nodes(temp_group);
1559 while (fans_handled)
1560 sysfs_remove_group(&pdev->dev.kobj,
1561 &fan_attribute_groups[--fans_handled]);
1562 sysfs_remove_group(&pdev->dev.kobj, &key_enumeration_group);
1563 sysfs_remove_file(&pdev->dev.kobj, &dev_attr_name.attr);
1564 applesmc_destroy_smcreg();
1565 platform_device_unregister(pdev);
1566 platform_driver_unregister(&applesmc_driver);
1567 release_region(APPLESMC_DATA_PORT, APPLESMC_NR_PORTS);
1569 pr_info("driver unloaded\n");
1572 module_init(applesmc_init);
1573 module_exit(applesmc_exit);
1575 MODULE_AUTHOR("Nicolas Boichat");
1576 MODULE_DESCRIPTION("Apple SMC");
1577 MODULE_LICENSE("GPL v2");
1578 MODULE_DEVICE_TABLE(dmi, applesmc_whitelist);