Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  * drivers/hwmon/applesmc.c - driver for Apple's SMC (accelerometer, temperature
0004  * sensors, fan control, keyboard backlight control) used in Intel-based Apple
0005  * computers.
0006  *
0007  * Copyright (C) 2007 Nicolas Boichat <nicolas@boichat.ch>
0008  * Copyright (C) 2010 Henrik Rydberg <rydberg@euromail.se>
0009  *
0010  * Based on hdaps.c driver:
0011  * Copyright (C) 2005 Robert Love <rml@novell.com>
0012  * Copyright (C) 2005 Jesper Juhl <jj@chaosbits.net>
0013  *
0014  * Fan control based on smcFanControl:
0015  * Copyright (C) 2006 Hendrik Holtmann <holtmann@mac.com>
0016  */
0017 
0018 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
0019 
0020 #include <linux/delay.h>
0021 #include <linux/platform_device.h>
0022 #include <linux/input.h>
0023 #include <linux/kernel.h>
0024 #include <linux/slab.h>
0025 #include <linux/module.h>
0026 #include <linux/timer.h>
0027 #include <linux/dmi.h>
0028 #include <linux/mutex.h>
0029 #include <linux/hwmon-sysfs.h>
0030 #include <linux/io.h>
0031 #include <linux/leds.h>
0032 #include <linux/hwmon.h>
0033 #include <linux/workqueue.h>
0034 #include <linux/err.h>
0035 #include <linux/bits.h>
0036 
0037 /* data port used by Apple SMC */
0038 #define APPLESMC_DATA_PORT  0x300
0039 /* command/status port used by Apple SMC */
0040 #define APPLESMC_CMD_PORT   0x304
0041 
0042 #define APPLESMC_NR_PORTS   32 /* 0x300-0x31f */
0043 
0044 #define APPLESMC_MAX_DATA_LENGTH 32
0045 
0046 /* Apple SMC status bits */
0047 #define SMC_STATUS_AWAITING_DATA  BIT(0) /* SMC has data waiting to be read */
0048 #define SMC_STATUS_IB_CLOSED      BIT(1) /* Will ignore any input */
0049 #define SMC_STATUS_BUSY           BIT(2) /* Command in progress */
0050 
0051 /* Initial wait is 8us */
0052 #define APPLESMC_MIN_WAIT      0x0008
0053 
0054 #define APPLESMC_READ_CMD   0x10
0055 #define APPLESMC_WRITE_CMD  0x11
0056 #define APPLESMC_GET_KEY_BY_INDEX_CMD   0x12
0057 #define APPLESMC_GET_KEY_TYPE_CMD   0x13
0058 
0059 #define KEY_COUNT_KEY       "#KEY" /* r-o ui32 */
0060 
0061 #define LIGHT_SENSOR_LEFT_KEY   "ALV0" /* r-o {alv (6-10 bytes) */
0062 #define LIGHT_SENSOR_RIGHT_KEY  "ALV1" /* r-o {alv (6-10 bytes) */
0063 #define BACKLIGHT_KEY       "LKSB" /* w-o {lkb (2 bytes) */
0064 
0065 #define CLAMSHELL_KEY       "MSLD" /* r-o ui8 (unused) */
0066 
0067 #define MOTION_SENSOR_X_KEY "MO_X" /* r-o sp78 (2 bytes) */
0068 #define MOTION_SENSOR_Y_KEY "MO_Y" /* r-o sp78 (2 bytes) */
0069 #define MOTION_SENSOR_Z_KEY "MO_Z" /* r-o sp78 (2 bytes) */
0070 #define MOTION_SENSOR_KEY   "MOCN" /* r/w ui16 */
0071 
0072 #define FANS_COUNT      "FNum" /* r-o ui8 */
0073 #define FANS_MANUAL     "FS! " /* r-w ui16 */
0074 #define FAN_ID_FMT      "F%dID" /* r-o char[16] */
0075 
0076 #define TEMP_SENSOR_TYPE    "sp78"
0077 
0078 /* List of keys used to read/write fan speeds */
0079 static const char *const fan_speed_fmt[] = {
0080     "F%dAc",        /* actual speed */
0081     "F%dMn",        /* minimum speed (rw) */
0082     "F%dMx",        /* maximum speed */
0083     "F%dSf",        /* safe speed - not all models */
0084     "F%dTg",        /* target speed (manual: rw) */
0085 };
0086 
0087 #define INIT_TIMEOUT_MSECS  5000    /* wait up to 5s for device init ... */
0088 #define INIT_WAIT_MSECS     50  /* ... in 50ms increments */
0089 
0090 #define APPLESMC_POLL_INTERVAL  50  /* msecs */
0091 #define APPLESMC_INPUT_FUZZ 4   /* input event threshold */
0092 #define APPLESMC_INPUT_FLAT 4
0093 
0094 #define to_index(attr) (to_sensor_dev_attr(attr)->index & 0xffff)
0095 #define to_option(attr) (to_sensor_dev_attr(attr)->index >> 16)
0096 
0097 /* Dynamic device node attributes */
0098 struct applesmc_dev_attr {
0099     struct sensor_device_attribute sda; /* hwmon attributes */
0100     char name[32];              /* room for node file name */
0101 };
0102 
0103 /* Dynamic device node group */
0104 struct applesmc_node_group {
0105     char *format;               /* format string */
0106     void *show;             /* show function */
0107     void *store;                /* store function */
0108     int option;             /* function argument */
0109     struct applesmc_dev_attr *nodes;    /* dynamic node array */
0110 };
0111 
0112 /* AppleSMC entry - cached register information */
0113 struct applesmc_entry {
0114     char key[5];        /* four-letter key code */
0115     u8 valid;       /* set when entry is successfully read once */
0116     u8 len;         /* bounded by APPLESMC_MAX_DATA_LENGTH */
0117     char type[5];       /* four-letter type code */
0118     u8 flags;       /* 0x10: func; 0x40: write; 0x80: read */
0119 };
0120 
0121 /* Register lookup and registers common to all SMCs */
0122 static struct applesmc_registers {
0123     struct mutex mutex;     /* register read/write mutex */
0124     unsigned int key_count;     /* number of SMC registers */
0125     unsigned int fan_count;     /* number of fans */
0126     unsigned int temp_count;    /* number of temperature registers */
0127     unsigned int temp_begin;    /* temperature lower index bound */
0128     unsigned int temp_end;      /* temperature upper index bound */
0129     unsigned int index_count;   /* size of temperature index array */
0130     int num_light_sensors;      /* number of light sensors */
0131     bool has_accelerometer;     /* has motion sensor */
0132     bool has_key_backlight;     /* has keyboard backlight */
0133     bool init_complete;     /* true when fully initialized */
0134     struct applesmc_entry *cache;   /* cached key entries */
0135     const char **index;     /* temperature key index */
0136 } smcreg = {
0137     .mutex = __MUTEX_INITIALIZER(smcreg.mutex),
0138 };
0139 
0140 static const int debug;
0141 static struct platform_device *pdev;
0142 static s16 rest_x;
0143 static s16 rest_y;
0144 static u8 backlight_state[2];
0145 
0146 static struct device *hwmon_dev;
0147 static struct input_dev *applesmc_idev;
0148 
0149 /*
0150  * Last index written to key_at_index sysfs file, and value to use for all other
0151  * key_at_index_* sysfs files.
0152  */
0153 static unsigned int key_at_index;
0154 
0155 static struct workqueue_struct *applesmc_led_wq;
0156 
0157 /*
0158  * Wait for specific status bits with a mask on the SMC.
0159  * Used before all transactions.
0160  * This does 10 fast loops of 8us then exponentially backs off for a
0161  * minimum total wait of 262ms. Depending on usleep_range this could
0162  * run out past 500ms.
0163  */
0164 
0165 static int wait_status(u8 val, u8 mask)
0166 {
0167     u8 status;
0168     int us;
0169     int i;
0170 
0171     us = APPLESMC_MIN_WAIT;
0172     for (i = 0; i < 24 ; i++) {
0173         status = inb(APPLESMC_CMD_PORT);
0174         if ((status & mask) == val)
0175             return 0;
0176         usleep_range(us, us * 2);
0177         if (i > 9)
0178             us <<= 1;
0179     }
0180     return -EIO;
0181 }
0182 
0183 /* send_byte - Write to SMC data port. Callers must hold applesmc_lock. */
0184 
0185 static int send_byte(u8 cmd, u16 port)
0186 {
0187     int status;
0188 
0189     status = wait_status(0, SMC_STATUS_IB_CLOSED);
0190     if (status)
0191         return status;
0192     /*
0193      * This needs to be a separate read looking for bit 0x04
0194      * after bit 0x02 falls. If consolidated with the wait above
0195      * this extra read may not happen if status returns both
0196      * simultaneously and this would appear to be required.
0197      */
0198     status = wait_status(SMC_STATUS_BUSY, SMC_STATUS_BUSY);
0199     if (status)
0200         return status;
0201 
0202     outb(cmd, port);
0203     return 0;
0204 }
0205 
0206 /* send_command - Write a command to the SMC. Callers must hold applesmc_lock. */
0207 
0208 static int send_command(u8 cmd)
0209 {
0210     int ret;
0211 
0212     ret = wait_status(0, SMC_STATUS_IB_CLOSED);
0213     if (ret)
0214         return ret;
0215     outb(cmd, APPLESMC_CMD_PORT);
0216     return 0;
0217 }
0218 
0219 /*
0220  * Based on logic from the Apple driver. This is issued before any interaction
0221  * If busy is stuck high, issue a read command to reset the SMC state machine.
0222  * If busy is stuck high after the command then the SMC is jammed.
0223  */
0224 
0225 static int smc_sane(void)
0226 {
0227     int ret;
0228 
0229     ret = wait_status(0, SMC_STATUS_BUSY);
0230     if (!ret)
0231         return ret;
0232     ret = send_command(APPLESMC_READ_CMD);
0233     if (ret)
0234         return ret;
0235     return wait_status(0, SMC_STATUS_BUSY);
0236 }
0237 
0238 static int send_argument(const char *key)
0239 {
0240     int i;
0241 
0242     for (i = 0; i < 4; i++)
0243         if (send_byte(key[i], APPLESMC_DATA_PORT))
0244             return -EIO;
0245     return 0;
0246 }
0247 
0248 static int read_smc(u8 cmd, const char *key, u8 *buffer, u8 len)
0249 {
0250     u8 status, data = 0;
0251     int i;
0252     int ret;
0253 
0254     ret = smc_sane();
0255     if (ret)
0256         return ret;
0257 
0258     if (send_command(cmd) || send_argument(key)) {
0259         pr_warn("%.4s: read arg fail\n", key);
0260         return -EIO;
0261     }
0262 
0263     /* This has no effect on newer (2012) SMCs */
0264     if (send_byte(len, APPLESMC_DATA_PORT)) {
0265         pr_warn("%.4s: read len fail\n", key);
0266         return -EIO;
0267     }
0268 
0269     for (i = 0; i < len; i++) {
0270         if (wait_status(SMC_STATUS_AWAITING_DATA | SMC_STATUS_BUSY,
0271                 SMC_STATUS_AWAITING_DATA | SMC_STATUS_BUSY)) {
0272             pr_warn("%.4s: read data[%d] fail\n", key, i);
0273             return -EIO;
0274         }
0275         buffer[i] = inb(APPLESMC_DATA_PORT);
0276     }
0277 
0278     /* Read the data port until bit0 is cleared */
0279     for (i = 0; i < 16; i++) {
0280         udelay(APPLESMC_MIN_WAIT);
0281         status = inb(APPLESMC_CMD_PORT);
0282         if (!(status & SMC_STATUS_AWAITING_DATA))
0283             break;
0284         data = inb(APPLESMC_DATA_PORT);
0285     }
0286     if (i)
0287         pr_warn("flushed %d bytes, last value is: %d\n", i, data);
0288 
0289     return wait_status(0, SMC_STATUS_BUSY);
0290 }
0291 
0292 static int write_smc(u8 cmd, const char *key, const u8 *buffer, u8 len)
0293 {
0294     int i;
0295     int ret;
0296 
0297     ret = smc_sane();
0298     if (ret)
0299         return ret;
0300 
0301     if (send_command(cmd) || send_argument(key)) {
0302         pr_warn("%s: write arg fail\n", key);
0303         return -EIO;
0304     }
0305 
0306     if (send_byte(len, APPLESMC_DATA_PORT)) {
0307         pr_warn("%.4s: write len fail\n", key);
0308         return -EIO;
0309     }
0310 
0311     for (i = 0; i < len; i++) {
0312         if (send_byte(buffer[i], APPLESMC_DATA_PORT)) {
0313             pr_warn("%s: write data fail\n", key);
0314             return -EIO;
0315         }
0316     }
0317 
0318     return wait_status(0, SMC_STATUS_BUSY);
0319 }
0320 
0321 static int read_register_count(unsigned int *count)
0322 {
0323     __be32 be;
0324     int ret;
0325 
0326     ret = read_smc(APPLESMC_READ_CMD, KEY_COUNT_KEY, (u8 *)&be, 4);
0327     if (ret)
0328         return ret;
0329 
0330     *count = be32_to_cpu(be);
0331     return 0;
0332 }
0333 
0334 /*
0335  * Serialized I/O
0336  *
0337  * Returns zero on success or a negative error on failure.
0338  * All functions below are concurrency safe - callers should NOT hold lock.
0339  */
0340 
0341 static int applesmc_read_entry(const struct applesmc_entry *entry,
0342                    u8 *buf, u8 len)
0343 {
0344     int ret;
0345 
0346     if (entry->len != len)
0347         return -EINVAL;
0348     mutex_lock(&smcreg.mutex);
0349     ret = read_smc(APPLESMC_READ_CMD, entry->key, buf, len);
0350     mutex_unlock(&smcreg.mutex);
0351 
0352     return ret;
0353 }
0354 
0355 static int applesmc_write_entry(const struct applesmc_entry *entry,
0356                 const u8 *buf, u8 len)
0357 {
0358     int ret;
0359 
0360     if (entry->len != len)
0361         return -EINVAL;
0362     mutex_lock(&smcreg.mutex);
0363     ret = write_smc(APPLESMC_WRITE_CMD, entry->key, buf, len);
0364     mutex_unlock(&smcreg.mutex);
0365     return ret;
0366 }
0367 
0368 static const struct applesmc_entry *applesmc_get_entry_by_index(int index)
0369 {
0370     struct applesmc_entry *cache = &smcreg.cache[index];
0371     u8 key[4], info[6];
0372     __be32 be;
0373     int ret = 0;
0374 
0375     if (cache->valid)
0376         return cache;
0377 
0378     mutex_lock(&smcreg.mutex);
0379 
0380     if (cache->valid)
0381         goto out;
0382     be = cpu_to_be32(index);
0383     ret = read_smc(APPLESMC_GET_KEY_BY_INDEX_CMD, (u8 *)&be, key, 4);
0384     if (ret)
0385         goto out;
0386     ret = read_smc(APPLESMC_GET_KEY_TYPE_CMD, key, info, 6);
0387     if (ret)
0388         goto out;
0389 
0390     memcpy(cache->key, key, 4);
0391     cache->len = info[0];
0392     memcpy(cache->type, &info[1], 4);
0393     cache->flags = info[5];
0394     cache->valid = true;
0395 
0396 out:
0397     mutex_unlock(&smcreg.mutex);
0398     if (ret)
0399         return ERR_PTR(ret);
0400     return cache;
0401 }
0402 
0403 static int applesmc_get_lower_bound(unsigned int *lo, const char *key)
0404 {
0405     int begin = 0, end = smcreg.key_count;
0406     const struct applesmc_entry *entry;
0407 
0408     while (begin != end) {
0409         int middle = begin + (end - begin) / 2;
0410         entry = applesmc_get_entry_by_index(middle);
0411         if (IS_ERR(entry)) {
0412             *lo = 0;
0413             return PTR_ERR(entry);
0414         }
0415         if (strcmp(entry->key, key) < 0)
0416             begin = middle + 1;
0417         else
0418             end = middle;
0419     }
0420 
0421     *lo = begin;
0422     return 0;
0423 }
0424 
0425 static int applesmc_get_upper_bound(unsigned int *hi, const char *key)
0426 {
0427     int begin = 0, end = smcreg.key_count;
0428     const struct applesmc_entry *entry;
0429 
0430     while (begin != end) {
0431         int middle = begin + (end - begin) / 2;
0432         entry = applesmc_get_entry_by_index(middle);
0433         if (IS_ERR(entry)) {
0434             *hi = smcreg.key_count;
0435             return PTR_ERR(entry);
0436         }
0437         if (strcmp(key, entry->key) < 0)
0438             end = middle;
0439         else
0440             begin = middle + 1;
0441     }
0442 
0443     *hi = begin;
0444     return 0;
0445 }
0446 
0447 static const struct applesmc_entry *applesmc_get_entry_by_key(const char *key)
0448 {
0449     int begin, end;
0450     int ret;
0451 
0452     ret = applesmc_get_lower_bound(&begin, key);
0453     if (ret)
0454         return ERR_PTR(ret);
0455     ret = applesmc_get_upper_bound(&end, key);
0456     if (ret)
0457         return ERR_PTR(ret);
0458     if (end - begin != 1)
0459         return ERR_PTR(-EINVAL);
0460 
0461     return applesmc_get_entry_by_index(begin);
0462 }
0463 
0464 static int applesmc_read_key(const char *key, u8 *buffer, u8 len)
0465 {
0466     const struct applesmc_entry *entry;
0467 
0468     entry = applesmc_get_entry_by_key(key);
0469     if (IS_ERR(entry))
0470         return PTR_ERR(entry);
0471 
0472     return applesmc_read_entry(entry, buffer, len);
0473 }
0474 
0475 static int applesmc_write_key(const char *key, const u8 *buffer, u8 len)
0476 {
0477     const struct applesmc_entry *entry;
0478 
0479     entry = applesmc_get_entry_by_key(key);
0480     if (IS_ERR(entry))
0481         return PTR_ERR(entry);
0482 
0483     return applesmc_write_entry(entry, buffer, len);
0484 }
0485 
0486 static int applesmc_has_key(const char *key, bool *value)
0487 {
0488     const struct applesmc_entry *entry;
0489 
0490     entry = applesmc_get_entry_by_key(key);
0491     if (IS_ERR(entry) && PTR_ERR(entry) != -EINVAL)
0492         return PTR_ERR(entry);
0493 
0494     *value = !IS_ERR(entry);
0495     return 0;
0496 }
0497 
0498 /*
0499  * applesmc_read_s16 - Read 16-bit signed big endian register
0500  */
0501 static int applesmc_read_s16(const char *key, s16 *value)
0502 {
0503     u8 buffer[2];
0504     int ret;
0505 
0506     ret = applesmc_read_key(key, buffer, 2);
0507     if (ret)
0508         return ret;
0509 
0510     *value = ((s16)buffer[0] << 8) | buffer[1];
0511     return 0;
0512 }
0513 
0514 /*
0515  * applesmc_device_init - initialize the accelerometer.  Can sleep.
0516  */
0517 static void applesmc_device_init(void)
0518 {
0519     int total;
0520     u8 buffer[2];
0521 
0522     if (!smcreg.has_accelerometer)
0523         return;
0524 
0525     for (total = INIT_TIMEOUT_MSECS; total > 0; total -= INIT_WAIT_MSECS) {
0526         if (!applesmc_read_key(MOTION_SENSOR_KEY, buffer, 2) &&
0527                 (buffer[0] != 0x00 || buffer[1] != 0x00))
0528             return;
0529         buffer[0] = 0xe0;
0530         buffer[1] = 0x00;
0531         applesmc_write_key(MOTION_SENSOR_KEY, buffer, 2);
0532         msleep(INIT_WAIT_MSECS);
0533     }
0534 
0535     pr_warn("failed to init the device\n");
0536 }
0537 
0538 static int applesmc_init_index(struct applesmc_registers *s)
0539 {
0540     const struct applesmc_entry *entry;
0541     unsigned int i;
0542 
0543     if (s->index)
0544         return 0;
0545 
0546     s->index = kcalloc(s->temp_count, sizeof(s->index[0]), GFP_KERNEL);
0547     if (!s->index)
0548         return -ENOMEM;
0549 
0550     for (i = s->temp_begin; i < s->temp_end; i++) {
0551         entry = applesmc_get_entry_by_index(i);
0552         if (IS_ERR(entry))
0553             continue;
0554         if (strcmp(entry->type, TEMP_SENSOR_TYPE))
0555             continue;
0556         s->index[s->index_count++] = entry->key;
0557     }
0558 
0559     return 0;
0560 }
0561 
0562 /*
0563  * applesmc_init_smcreg_try - Try to initialize register cache. Idempotent.
0564  */
0565 static int applesmc_init_smcreg_try(void)
0566 {
0567     struct applesmc_registers *s = &smcreg;
0568     bool left_light_sensor = false, right_light_sensor = false;
0569     unsigned int count;
0570     u8 tmp[1];
0571     int ret;
0572 
0573     if (s->init_complete)
0574         return 0;
0575 
0576     ret = read_register_count(&count);
0577     if (ret)
0578         return ret;
0579 
0580     if (s->cache && s->key_count != count) {
0581         pr_warn("key count changed from %d to %d\n",
0582             s->key_count, count);
0583         kfree(s->cache);
0584         s->cache = NULL;
0585     }
0586     s->key_count = count;
0587 
0588     if (!s->cache)
0589         s->cache = kcalloc(s->key_count, sizeof(*s->cache), GFP_KERNEL);
0590     if (!s->cache)
0591         return -ENOMEM;
0592 
0593     ret = applesmc_read_key(FANS_COUNT, tmp, 1);
0594     if (ret)
0595         return ret;
0596     s->fan_count = tmp[0];
0597     if (s->fan_count > 10)
0598         s->fan_count = 10;
0599 
0600     ret = applesmc_get_lower_bound(&s->temp_begin, "T");
0601     if (ret)
0602         return ret;
0603     ret = applesmc_get_lower_bound(&s->temp_end, "U");
0604     if (ret)
0605         return ret;
0606     s->temp_count = s->temp_end - s->temp_begin;
0607 
0608     ret = applesmc_init_index(s);
0609     if (ret)
0610         return ret;
0611 
0612     ret = applesmc_has_key(LIGHT_SENSOR_LEFT_KEY, &left_light_sensor);
0613     if (ret)
0614         return ret;
0615     ret = applesmc_has_key(LIGHT_SENSOR_RIGHT_KEY, &right_light_sensor);
0616     if (ret)
0617         return ret;
0618     ret = applesmc_has_key(MOTION_SENSOR_KEY, &s->has_accelerometer);
0619     if (ret)
0620         return ret;
0621     ret = applesmc_has_key(BACKLIGHT_KEY, &s->has_key_backlight);
0622     if (ret)
0623         return ret;
0624 
0625     s->num_light_sensors = left_light_sensor + right_light_sensor;
0626     s->init_complete = true;
0627 
0628     pr_info("key=%d fan=%d temp=%d index=%d acc=%d lux=%d kbd=%d\n",
0629            s->key_count, s->fan_count, s->temp_count, s->index_count,
0630            s->has_accelerometer,
0631            s->num_light_sensors,
0632            s->has_key_backlight);
0633 
0634     return 0;
0635 }
0636 
0637 static void applesmc_destroy_smcreg(void)
0638 {
0639     kfree(smcreg.index);
0640     smcreg.index = NULL;
0641     kfree(smcreg.cache);
0642     smcreg.cache = NULL;
0643     smcreg.init_complete = false;
0644 }
0645 
0646 /*
0647  * applesmc_init_smcreg - Initialize register cache.
0648  *
0649  * Retries until initialization is successful, or the operation times out.
0650  *
0651  */
0652 static int applesmc_init_smcreg(void)
0653 {
0654     int ms, ret;
0655 
0656     for (ms = 0; ms < INIT_TIMEOUT_MSECS; ms += INIT_WAIT_MSECS) {
0657         ret = applesmc_init_smcreg_try();
0658         if (!ret) {
0659             if (ms)
0660                 pr_info("init_smcreg() took %d ms\n", ms);
0661             return 0;
0662         }
0663         msleep(INIT_WAIT_MSECS);
0664     }
0665 
0666     applesmc_destroy_smcreg();
0667 
0668     return ret;
0669 }
0670 
0671 /* Device model stuff */
0672 static int applesmc_probe(struct platform_device *dev)
0673 {
0674     int ret;
0675 
0676     ret = applesmc_init_smcreg();
0677     if (ret)
0678         return ret;
0679 
0680     applesmc_device_init();
0681 
0682     return 0;
0683 }
0684 
0685 /* Synchronize device with memorized backlight state */
0686 static int applesmc_pm_resume(struct device *dev)
0687 {
0688     if (smcreg.has_key_backlight)
0689         applesmc_write_key(BACKLIGHT_KEY, backlight_state, 2);
0690     return 0;
0691 }
0692 
0693 /* Reinitialize device on resume from hibernation */
0694 static int applesmc_pm_restore(struct device *dev)
0695 {
0696     applesmc_device_init();
0697     return applesmc_pm_resume(dev);
0698 }
0699 
0700 static const struct dev_pm_ops applesmc_pm_ops = {
0701     .resume = applesmc_pm_resume,
0702     .restore = applesmc_pm_restore,
0703 };
0704 
0705 static struct platform_driver applesmc_driver = {
0706     .probe = applesmc_probe,
0707     .driver = {
0708         .name = "applesmc",
0709         .pm = &applesmc_pm_ops,
0710     },
0711 };
0712 
0713 /*
0714  * applesmc_calibrate - Set our "resting" values.  Callers must
0715  * hold applesmc_lock.
0716  */
0717 static void applesmc_calibrate(void)
0718 {
0719     applesmc_read_s16(MOTION_SENSOR_X_KEY, &rest_x);
0720     applesmc_read_s16(MOTION_SENSOR_Y_KEY, &rest_y);
0721     rest_x = -rest_x;
0722 }
0723 
0724 static void applesmc_idev_poll(struct input_dev *idev)
0725 {
0726     s16 x, y;
0727 
0728     if (applesmc_read_s16(MOTION_SENSOR_X_KEY, &x))
0729         return;
0730     if (applesmc_read_s16(MOTION_SENSOR_Y_KEY, &y))
0731         return;
0732 
0733     x = -x;
0734     input_report_abs(idev, ABS_X, x - rest_x);
0735     input_report_abs(idev, ABS_Y, y - rest_y);
0736     input_sync(idev);
0737 }
0738 
0739 /* Sysfs Files */
0740 
0741 static ssize_t applesmc_name_show(struct device *dev,
0742                    struct device_attribute *attr, char *buf)
0743 {
0744     return sysfs_emit(buf, "applesmc\n");
0745 }
0746 
0747 static ssize_t applesmc_position_show(struct device *dev,
0748                    struct device_attribute *attr, char *buf)
0749 {
0750     int ret;
0751     s16 x, y, z;
0752 
0753     ret = applesmc_read_s16(MOTION_SENSOR_X_KEY, &x);
0754     if (ret)
0755         goto out;
0756     ret = applesmc_read_s16(MOTION_SENSOR_Y_KEY, &y);
0757     if (ret)
0758         goto out;
0759     ret = applesmc_read_s16(MOTION_SENSOR_Z_KEY, &z);
0760     if (ret)
0761         goto out;
0762 
0763 out:
0764     if (ret)
0765         return ret;
0766 
0767     return sysfs_emit(buf, "(%d,%d,%d)\n", x, y, z);
0768 }
0769 
0770 static ssize_t applesmc_light_show(struct device *dev,
0771                 struct device_attribute *attr, char *sysfsbuf)
0772 {
0773     const struct applesmc_entry *entry;
0774     static int data_length;
0775     int ret;
0776     u8 left = 0, right = 0;
0777     u8 buffer[10];
0778 
0779     if (!data_length) {
0780         entry = applesmc_get_entry_by_key(LIGHT_SENSOR_LEFT_KEY);
0781         if (IS_ERR(entry))
0782             return PTR_ERR(entry);
0783         if (entry->len > 10)
0784             return -ENXIO;
0785         data_length = entry->len;
0786         pr_info("light sensor data length set to %d\n", data_length);
0787     }
0788 
0789     ret = applesmc_read_key(LIGHT_SENSOR_LEFT_KEY, buffer, data_length);
0790     if (ret)
0791         goto out;
0792     /* newer macbooks report a single 10-bit bigendian value */
0793     if (data_length == 10) {
0794         left = be16_to_cpu(*(__be16 *)(buffer + 6)) >> 2;
0795         goto out;
0796     }
0797     left = buffer[2];
0798 
0799     ret = applesmc_read_key(LIGHT_SENSOR_RIGHT_KEY, buffer, data_length);
0800     if (ret)
0801         goto out;
0802     right = buffer[2];
0803 
0804 out:
0805     if (ret)
0806         return ret;
0807 
0808     return sysfs_emit(sysfsbuf, "(%d,%d)\n", left, right);
0809 }
0810 
0811 /* Displays sensor key as label */
0812 static ssize_t applesmc_show_sensor_label(struct device *dev,
0813             struct device_attribute *devattr, char *sysfsbuf)
0814 {
0815     const char *key = smcreg.index[to_index(devattr)];
0816 
0817     return sysfs_emit(sysfsbuf, "%s\n", key);
0818 }
0819 
0820 /* Displays degree Celsius * 1000 */
0821 static ssize_t applesmc_show_temperature(struct device *dev,
0822             struct device_attribute *devattr, char *sysfsbuf)
0823 {
0824     const char *key = smcreg.index[to_index(devattr)];
0825     int ret;
0826     s16 value;
0827     int temp;
0828 
0829     ret = applesmc_read_s16(key, &value);
0830     if (ret)
0831         return ret;
0832 
0833     temp = 250 * (value >> 6);
0834 
0835     return sysfs_emit(sysfsbuf, "%d\n", temp);
0836 }
0837 
0838 static ssize_t applesmc_show_fan_speed(struct device *dev,
0839                 struct device_attribute *attr, char *sysfsbuf)
0840 {
0841     int ret;
0842     unsigned int speed = 0;
0843     char newkey[5];
0844     u8 buffer[2];
0845 
0846     scnprintf(newkey, sizeof(newkey), fan_speed_fmt[to_option(attr)],
0847           to_index(attr));
0848 
0849     ret = applesmc_read_key(newkey, buffer, 2);
0850     if (ret)
0851         return ret;
0852 
0853     speed = ((buffer[0] << 8 | buffer[1]) >> 2);
0854     return sysfs_emit(sysfsbuf, "%u\n", speed);
0855 }
0856 
0857 static ssize_t applesmc_store_fan_speed(struct device *dev,
0858                     struct device_attribute *attr,
0859                     const char *sysfsbuf, size_t count)
0860 {
0861     int ret;
0862     unsigned long speed;
0863     char newkey[5];
0864     u8 buffer[2];
0865 
0866     if (kstrtoul(sysfsbuf, 10, &speed) < 0 || speed >= 0x4000)
0867         return -EINVAL;     /* Bigger than a 14-bit value */
0868 
0869     scnprintf(newkey, sizeof(newkey), fan_speed_fmt[to_option(attr)],
0870           to_index(attr));
0871 
0872     buffer[0] = (speed >> 6) & 0xff;
0873     buffer[1] = (speed << 2) & 0xff;
0874     ret = applesmc_write_key(newkey, buffer, 2);
0875 
0876     if (ret)
0877         return ret;
0878     else
0879         return count;
0880 }
0881 
0882 static ssize_t applesmc_show_fan_manual(struct device *dev,
0883             struct device_attribute *attr, char *sysfsbuf)
0884 {
0885     int ret;
0886     u16 manual = 0;
0887     u8 buffer[2];
0888 
0889     ret = applesmc_read_key(FANS_MANUAL, buffer, 2);
0890     if (ret)
0891         return ret;
0892 
0893     manual = ((buffer[0] << 8 | buffer[1]) >> to_index(attr)) & 0x01;
0894     return sysfs_emit(sysfsbuf, "%d\n", manual);
0895 }
0896 
0897 static ssize_t applesmc_store_fan_manual(struct device *dev,
0898                      struct device_attribute *attr,
0899                      const char *sysfsbuf, size_t count)
0900 {
0901     int ret;
0902     u8 buffer[2];
0903     unsigned long input;
0904     u16 val;
0905 
0906     if (kstrtoul(sysfsbuf, 10, &input) < 0)
0907         return -EINVAL;
0908 
0909     ret = applesmc_read_key(FANS_MANUAL, buffer, 2);
0910     if (ret)
0911         goto out;
0912 
0913     val = (buffer[0] << 8 | buffer[1]);
0914 
0915     if (input)
0916         val = val | (0x01 << to_index(attr));
0917     else
0918         val = val & ~(0x01 << to_index(attr));
0919 
0920     buffer[0] = (val >> 8) & 0xFF;
0921     buffer[1] = val & 0xFF;
0922 
0923     ret = applesmc_write_key(FANS_MANUAL, buffer, 2);
0924 
0925 out:
0926     if (ret)
0927         return ret;
0928     else
0929         return count;
0930 }
0931 
0932 static ssize_t applesmc_show_fan_position(struct device *dev,
0933                 struct device_attribute *attr, char *sysfsbuf)
0934 {
0935     int ret;
0936     char newkey[5];
0937     u8 buffer[17];
0938 
0939     scnprintf(newkey, sizeof(newkey), FAN_ID_FMT, to_index(attr));
0940 
0941     ret = applesmc_read_key(newkey, buffer, 16);
0942     buffer[16] = 0;
0943 
0944     if (ret)
0945         return ret;
0946 
0947     return sysfs_emit(sysfsbuf, "%s\n", buffer + 4);
0948 }
0949 
0950 static ssize_t applesmc_calibrate_show(struct device *dev,
0951                 struct device_attribute *attr, char *sysfsbuf)
0952 {
0953     return sysfs_emit(sysfsbuf, "(%d,%d)\n", rest_x, rest_y);
0954 }
0955 
0956 static ssize_t applesmc_calibrate_store(struct device *dev,
0957     struct device_attribute *attr, const char *sysfsbuf, size_t count)
0958 {
0959     applesmc_calibrate();
0960 
0961     return count;
0962 }
0963 
0964 static void applesmc_backlight_set(struct work_struct *work)
0965 {
0966     applesmc_write_key(BACKLIGHT_KEY, backlight_state, 2);
0967 }
0968 static DECLARE_WORK(backlight_work, &applesmc_backlight_set);
0969 
0970 static void applesmc_brightness_set(struct led_classdev *led_cdev,
0971                         enum led_brightness value)
0972 {
0973     int ret;
0974 
0975     backlight_state[0] = value;
0976     ret = queue_work(applesmc_led_wq, &backlight_work);
0977 
0978     if (debug && (!ret))
0979         dev_dbg(led_cdev->dev, "work was already on the queue.\n");
0980 }
0981 
0982 static ssize_t applesmc_key_count_show(struct device *dev,
0983                 struct device_attribute *attr, char *sysfsbuf)
0984 {
0985     int ret;
0986     u8 buffer[4];
0987     u32 count;
0988 
0989     ret = applesmc_read_key(KEY_COUNT_KEY, buffer, 4);
0990     if (ret)
0991         return ret;
0992 
0993     count = ((u32)buffer[0]<<24) + ((u32)buffer[1]<<16) +
0994                         ((u32)buffer[2]<<8) + buffer[3];
0995     return sysfs_emit(sysfsbuf, "%d\n", count);
0996 }
0997 
0998 static ssize_t applesmc_key_at_index_read_show(struct device *dev,
0999                 struct device_attribute *attr, char *sysfsbuf)
1000 {
1001     const struct applesmc_entry *entry;
1002     int ret;
1003 
1004     entry = applesmc_get_entry_by_index(key_at_index);
1005     if (IS_ERR(entry))
1006         return PTR_ERR(entry);
1007     ret = applesmc_read_entry(entry, sysfsbuf, entry->len);
1008     if (ret)
1009         return ret;
1010 
1011     return entry->len;
1012 }
1013 
1014 static ssize_t applesmc_key_at_index_data_length_show(struct device *dev,
1015                 struct device_attribute *attr, char *sysfsbuf)
1016 {
1017     const struct applesmc_entry *entry;
1018 
1019     entry = applesmc_get_entry_by_index(key_at_index);
1020     if (IS_ERR(entry))
1021         return PTR_ERR(entry);
1022 
1023     return sysfs_emit(sysfsbuf, "%d\n", entry->len);
1024 }
1025 
1026 static ssize_t applesmc_key_at_index_type_show(struct device *dev,
1027                 struct device_attribute *attr, char *sysfsbuf)
1028 {
1029     const struct applesmc_entry *entry;
1030 
1031     entry = applesmc_get_entry_by_index(key_at_index);
1032     if (IS_ERR(entry))
1033         return PTR_ERR(entry);
1034 
1035     return sysfs_emit(sysfsbuf, "%s\n", entry->type);
1036 }
1037 
1038 static ssize_t applesmc_key_at_index_name_show(struct device *dev,
1039                 struct device_attribute *attr, char *sysfsbuf)
1040 {
1041     const struct applesmc_entry *entry;
1042 
1043     entry = applesmc_get_entry_by_index(key_at_index);
1044     if (IS_ERR(entry))
1045         return PTR_ERR(entry);
1046 
1047     return sysfs_emit(sysfsbuf, "%s\n", entry->key);
1048 }
1049 
1050 static ssize_t applesmc_key_at_index_show(struct device *dev,
1051                 struct device_attribute *attr, char *sysfsbuf)
1052 {
1053     return sysfs_emit(sysfsbuf, "%d\n", key_at_index);
1054 }
1055 
1056 static ssize_t applesmc_key_at_index_store(struct device *dev,
1057     struct device_attribute *attr, const char *sysfsbuf, size_t count)
1058 {
1059     unsigned long newkey;
1060 
1061     if (kstrtoul(sysfsbuf, 10, &newkey) < 0
1062         || newkey >= smcreg.key_count)
1063         return -EINVAL;
1064 
1065     key_at_index = newkey;
1066     return count;
1067 }
1068 
1069 static struct led_classdev applesmc_backlight = {
1070     .name           = "smc::kbd_backlight",
1071     .default_trigger    = "nand-disk",
1072     .brightness_set     = applesmc_brightness_set,
1073 };
1074 
1075 static struct applesmc_node_group info_group[] = {
1076     { "name", applesmc_name_show },
1077     { "key_count", applesmc_key_count_show },
1078     { "key_at_index", applesmc_key_at_index_show, applesmc_key_at_index_store },
1079     { "key_at_index_name", applesmc_key_at_index_name_show },
1080     { "key_at_index_type", applesmc_key_at_index_type_show },
1081     { "key_at_index_data_length", applesmc_key_at_index_data_length_show },
1082     { "key_at_index_data", applesmc_key_at_index_read_show },
1083     { }
1084 };
1085 
1086 static struct applesmc_node_group accelerometer_group[] = {
1087     { "position", applesmc_position_show },
1088     { "calibrate", applesmc_calibrate_show, applesmc_calibrate_store },
1089     { }
1090 };
1091 
1092 static struct applesmc_node_group light_sensor_group[] = {
1093     { "light", applesmc_light_show },
1094     { }
1095 };
1096 
1097 static struct applesmc_node_group fan_group[] = {
1098     { "fan%d_label", applesmc_show_fan_position },
1099     { "fan%d_input", applesmc_show_fan_speed, NULL, 0 },
1100     { "fan%d_min", applesmc_show_fan_speed, applesmc_store_fan_speed, 1 },
1101     { "fan%d_max", applesmc_show_fan_speed, NULL, 2 },
1102     { "fan%d_safe", applesmc_show_fan_speed, NULL, 3 },
1103     { "fan%d_output", applesmc_show_fan_speed, applesmc_store_fan_speed, 4 },
1104     { "fan%d_manual", applesmc_show_fan_manual, applesmc_store_fan_manual },
1105     { }
1106 };
1107 
1108 static struct applesmc_node_group temp_group[] = {
1109     { "temp%d_label", applesmc_show_sensor_label },
1110     { "temp%d_input", applesmc_show_temperature },
1111     { }
1112 };
1113 
1114 /* Module stuff */
1115 
1116 /*
1117  * applesmc_destroy_nodes - remove files and free associated memory
1118  */
1119 static void applesmc_destroy_nodes(struct applesmc_node_group *groups)
1120 {
1121     struct applesmc_node_group *grp;
1122     struct applesmc_dev_attr *node;
1123 
1124     for (grp = groups; grp->nodes; grp++) {
1125         for (node = grp->nodes; node->sda.dev_attr.attr.name; node++)
1126             sysfs_remove_file(&pdev->dev.kobj,
1127                       &node->sda.dev_attr.attr);
1128         kfree(grp->nodes);
1129         grp->nodes = NULL;
1130     }
1131 }
1132 
1133 /*
1134  * applesmc_create_nodes - create a two-dimensional group of sysfs files
1135  */
1136 static int applesmc_create_nodes(struct applesmc_node_group *groups, int num)
1137 {
1138     struct applesmc_node_group *grp;
1139     struct applesmc_dev_attr *node;
1140     struct attribute *attr;
1141     int ret, i;
1142 
1143     for (grp = groups; grp->format; grp++) {
1144         grp->nodes = kcalloc(num + 1, sizeof(*node), GFP_KERNEL);
1145         if (!grp->nodes) {
1146             ret = -ENOMEM;
1147             goto out;
1148         }
1149         for (i = 0; i < num; i++) {
1150             node = &grp->nodes[i];
1151             scnprintf(node->name, sizeof(node->name), grp->format,
1152                   i + 1);
1153             node->sda.index = (grp->option << 16) | (i & 0xffff);
1154             node->sda.dev_attr.show = grp->show;
1155             node->sda.dev_attr.store = grp->store;
1156             attr = &node->sda.dev_attr.attr;
1157             sysfs_attr_init(attr);
1158             attr->name = node->name;
1159             attr->mode = 0444 | (grp->store ? 0200 : 0);
1160             ret = sysfs_create_file(&pdev->dev.kobj, attr);
1161             if (ret) {
1162                 attr->name = NULL;
1163                 goto out;
1164             }
1165         }
1166     }
1167 
1168     return 0;
1169 out:
1170     applesmc_destroy_nodes(groups);
1171     return ret;
1172 }
1173 
1174 /* Create accelerometer resources */
1175 static int applesmc_create_accelerometer(void)
1176 {
1177     int ret;
1178 
1179     if (!smcreg.has_accelerometer)
1180         return 0;
1181 
1182     ret = applesmc_create_nodes(accelerometer_group, 1);
1183     if (ret)
1184         goto out;
1185 
1186     applesmc_idev = input_allocate_device();
1187     if (!applesmc_idev) {
1188         ret = -ENOMEM;
1189         goto out_sysfs;
1190     }
1191 
1192     /* initial calibrate for the input device */
1193     applesmc_calibrate();
1194 
1195     /* initialize the input device */
1196     applesmc_idev->name = "applesmc";
1197     applesmc_idev->id.bustype = BUS_HOST;
1198     applesmc_idev->dev.parent = &pdev->dev;
1199     input_set_abs_params(applesmc_idev, ABS_X,
1200             -256, 256, APPLESMC_INPUT_FUZZ, APPLESMC_INPUT_FLAT);
1201     input_set_abs_params(applesmc_idev, ABS_Y,
1202             -256, 256, APPLESMC_INPUT_FUZZ, APPLESMC_INPUT_FLAT);
1203 
1204     ret = input_setup_polling(applesmc_idev, applesmc_idev_poll);
1205     if (ret)
1206         goto out_idev;
1207 
1208     input_set_poll_interval(applesmc_idev, APPLESMC_POLL_INTERVAL);
1209 
1210     ret = input_register_device(applesmc_idev);
1211     if (ret)
1212         goto out_idev;
1213 
1214     return 0;
1215 
1216 out_idev:
1217     input_free_device(applesmc_idev);
1218 
1219 out_sysfs:
1220     applesmc_destroy_nodes(accelerometer_group);
1221 
1222 out:
1223     pr_warn("driver init failed (ret=%d)!\n", ret);
1224     return ret;
1225 }
1226 
1227 /* Release all resources used by the accelerometer */
1228 static void applesmc_release_accelerometer(void)
1229 {
1230     if (!smcreg.has_accelerometer)
1231         return;
1232     input_unregister_device(applesmc_idev);
1233     applesmc_destroy_nodes(accelerometer_group);
1234 }
1235 
1236 static int applesmc_create_light_sensor(void)
1237 {
1238     if (!smcreg.num_light_sensors)
1239         return 0;
1240     return applesmc_create_nodes(light_sensor_group, 1);
1241 }
1242 
1243 static void applesmc_release_light_sensor(void)
1244 {
1245     if (!smcreg.num_light_sensors)
1246         return;
1247     applesmc_destroy_nodes(light_sensor_group);
1248 }
1249 
1250 static int applesmc_create_key_backlight(void)
1251 {
1252     if (!smcreg.has_key_backlight)
1253         return 0;
1254     applesmc_led_wq = create_singlethread_workqueue("applesmc-led");
1255     if (!applesmc_led_wq)
1256         return -ENOMEM;
1257     return led_classdev_register(&pdev->dev, &applesmc_backlight);
1258 }
1259 
1260 static void applesmc_release_key_backlight(void)
1261 {
1262     if (!smcreg.has_key_backlight)
1263         return;
1264     led_classdev_unregister(&applesmc_backlight);
1265     destroy_workqueue(applesmc_led_wq);
1266 }
1267 
1268 static int applesmc_dmi_match(const struct dmi_system_id *id)
1269 {
1270     return 1;
1271 }
1272 
1273 /*
1274  * Note that DMI_MATCH(...,"MacBook") will match "MacBookPro1,1".
1275  * So we need to put "Apple MacBook Pro" before "Apple MacBook".
1276  */
1277 static const struct dmi_system_id applesmc_whitelist[] __initconst = {
1278     { applesmc_dmi_match, "Apple MacBook Air", {
1279       DMI_MATCH(DMI_BOARD_VENDOR, "Apple"),
1280       DMI_MATCH(DMI_PRODUCT_NAME, "MacBookAir") },
1281     },
1282     { applesmc_dmi_match, "Apple MacBook Pro", {
1283       DMI_MATCH(DMI_BOARD_VENDOR, "Apple"),
1284       DMI_MATCH(DMI_PRODUCT_NAME, "MacBookPro") },
1285     },
1286     { applesmc_dmi_match, "Apple MacBook", {
1287       DMI_MATCH(DMI_BOARD_VENDOR, "Apple"),
1288       DMI_MATCH(DMI_PRODUCT_NAME, "MacBook") },
1289     },
1290     { applesmc_dmi_match, "Apple Macmini", {
1291       DMI_MATCH(DMI_BOARD_VENDOR, "Apple"),
1292       DMI_MATCH(DMI_PRODUCT_NAME, "Macmini") },
1293     },
1294     { applesmc_dmi_match, "Apple MacPro", {
1295       DMI_MATCH(DMI_BOARD_VENDOR, "Apple"),
1296       DMI_MATCH(DMI_PRODUCT_NAME, "MacPro") },
1297     },
1298     { applesmc_dmi_match, "Apple iMac", {
1299       DMI_MATCH(DMI_BOARD_VENDOR, "Apple"),
1300       DMI_MATCH(DMI_PRODUCT_NAME, "iMac") },
1301     },
1302     { applesmc_dmi_match, "Apple Xserve", {
1303       DMI_MATCH(DMI_BOARD_VENDOR, "Apple"),
1304       DMI_MATCH(DMI_PRODUCT_NAME, "Xserve") },
1305     },
1306     { .ident = NULL }
1307 };
1308 
1309 static int __init applesmc_init(void)
1310 {
1311     int ret;
1312 
1313     if (!dmi_check_system(applesmc_whitelist)) {
1314         pr_warn("supported laptop not found!\n");
1315         ret = -ENODEV;
1316         goto out;
1317     }
1318 
1319     if (!request_region(APPLESMC_DATA_PORT, APPLESMC_NR_PORTS,
1320                                 "applesmc")) {
1321         ret = -ENXIO;
1322         goto out;
1323     }
1324 
1325     ret = platform_driver_register(&applesmc_driver);
1326     if (ret)
1327         goto out_region;
1328 
1329     pdev = platform_device_register_simple("applesmc", APPLESMC_DATA_PORT,
1330                            NULL, 0);
1331     if (IS_ERR(pdev)) {
1332         ret = PTR_ERR(pdev);
1333         goto out_driver;
1334     }
1335 
1336     /* create register cache */
1337     ret = applesmc_init_smcreg();
1338     if (ret)
1339         goto out_device;
1340 
1341     ret = applesmc_create_nodes(info_group, 1);
1342     if (ret)
1343         goto out_smcreg;
1344 
1345     ret = applesmc_create_nodes(fan_group, smcreg.fan_count);
1346     if (ret)
1347         goto out_info;
1348 
1349     ret = applesmc_create_nodes(temp_group, smcreg.index_count);
1350     if (ret)
1351         goto out_fans;
1352 
1353     ret = applesmc_create_accelerometer();
1354     if (ret)
1355         goto out_temperature;
1356 
1357     ret = applesmc_create_light_sensor();
1358     if (ret)
1359         goto out_accelerometer;
1360 
1361     ret = applesmc_create_key_backlight();
1362     if (ret)
1363         goto out_light_sysfs;
1364 
1365     hwmon_dev = hwmon_device_register(&pdev->dev);
1366     if (IS_ERR(hwmon_dev)) {
1367         ret = PTR_ERR(hwmon_dev);
1368         goto out_light_ledclass;
1369     }
1370 
1371     return 0;
1372 
1373 out_light_ledclass:
1374     applesmc_release_key_backlight();
1375 out_light_sysfs:
1376     applesmc_release_light_sensor();
1377 out_accelerometer:
1378     applesmc_release_accelerometer();
1379 out_temperature:
1380     applesmc_destroy_nodes(temp_group);
1381 out_fans:
1382     applesmc_destroy_nodes(fan_group);
1383 out_info:
1384     applesmc_destroy_nodes(info_group);
1385 out_smcreg:
1386     applesmc_destroy_smcreg();
1387 out_device:
1388     platform_device_unregister(pdev);
1389 out_driver:
1390     platform_driver_unregister(&applesmc_driver);
1391 out_region:
1392     release_region(APPLESMC_DATA_PORT, APPLESMC_NR_PORTS);
1393 out:
1394     pr_warn("driver init failed (ret=%d)!\n", ret);
1395     return ret;
1396 }
1397 
1398 static void __exit applesmc_exit(void)
1399 {
1400     hwmon_device_unregister(hwmon_dev);
1401     applesmc_release_key_backlight();
1402     applesmc_release_light_sensor();
1403     applesmc_release_accelerometer();
1404     applesmc_destroy_nodes(temp_group);
1405     applesmc_destroy_nodes(fan_group);
1406     applesmc_destroy_nodes(info_group);
1407     applesmc_destroy_smcreg();
1408     platform_device_unregister(pdev);
1409     platform_driver_unregister(&applesmc_driver);
1410     release_region(APPLESMC_DATA_PORT, APPLESMC_NR_PORTS);
1411 }
1412 
1413 module_init(applesmc_init);
1414 module_exit(applesmc_exit);
1415 
1416 MODULE_AUTHOR("Nicolas Boichat");
1417 MODULE_DESCRIPTION("Apple SMC");
1418 MODULE_LICENSE("GPL v2");
1419 MODULE_DEVICE_TABLE(dmi, applesmc_whitelist);