Nicolas Boichat | 6f2fad7 | 2007-05-08 00:24:52 -0700 | [diff] [blame] | 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. |
| 5 | * |
| 6 | * Copyright (C) 2007 Nicolas Boichat <nicolas@boichat.ch> |
Henrik Rydberg | 41e71f9 | 2010-11-09 15:15:11 +0000 | [diff] [blame] | 7 | * Copyright (C) 2010 Henrik Rydberg <rydberg@euromail.se> |
Nicolas Boichat | 6f2fad7 | 2007-05-08 00:24:52 -0700 | [diff] [blame] | 8 | * |
| 9 | * Based on hdaps.c driver: |
| 10 | * Copyright (C) 2005 Robert Love <rml@novell.com> |
Jesper Juhl | aa8521e | 2012-06-20 21:18:48 +0200 | [diff] [blame] | 11 | * Copyright (C) 2005 Jesper Juhl <jj@chaosbits.net> |
Nicolas Boichat | 6f2fad7 | 2007-05-08 00:24:52 -0700 | [diff] [blame] | 12 | * |
| 13 | * Fan control based on smcFanControl: |
| 14 | * Copyright (C) 2006 Hendrik Holtmann <holtmann@mac.com> |
| 15 | * |
| 16 | * This program is free software; you can redistribute it and/or modify it |
| 17 | * under the terms of the GNU General Public License v2 as published by the |
| 18 | * Free Software Foundation. |
| 19 | * |
| 20 | * This program is distributed in the hope that it will be useful, but WITHOUT |
| 21 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 22 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for |
| 23 | * more details. |
| 24 | * |
| 25 | * You should have received a copy of the GNU General Public License along with |
| 26 | * this program; if not, write to the Free Software Foundation, Inc., |
| 27 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA |
| 28 | */ |
| 29 | |
Joe Perches | 1ee7c71 | 2010-11-09 15:15:03 +0000 | [diff] [blame] | 30 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
| 31 | |
Nicolas Boichat | 6f2fad7 | 2007-05-08 00:24:52 -0700 | [diff] [blame] | 32 | #include <linux/delay.h> |
| 33 | #include <linux/platform_device.h> |
Dmitry Torokhov | d5cf2b9 | 2007-09-26 00:01:35 -0400 | [diff] [blame] | 34 | #include <linux/input-polldev.h> |
Nicolas Boichat | 6f2fad7 | 2007-05-08 00:24:52 -0700 | [diff] [blame] | 35 | #include <linux/kernel.h> |
Henrik Rydberg | 5874583 | 2010-11-10 10:58:03 +0000 | [diff] [blame] | 36 | #include <linux/slab.h> |
Nicolas Boichat | 6f2fad7 | 2007-05-08 00:24:52 -0700 | [diff] [blame] | 37 | #include <linux/module.h> |
| 38 | #include <linux/timer.h> |
| 39 | #include <linux/dmi.h> |
| 40 | #include <linux/mutex.h> |
| 41 | #include <linux/hwmon-sysfs.h> |
H Hartley Sweeten | 6055fae | 2009-09-15 17:18:13 +0200 | [diff] [blame] | 42 | #include <linux/io.h> |
Nicolas Boichat | 6f2fad7 | 2007-05-08 00:24:52 -0700 | [diff] [blame] | 43 | #include <linux/leds.h> |
| 44 | #include <linux/hwmon.h> |
| 45 | #include <linux/workqueue.h> |
| 46 | |
| 47 | /* data port used by Apple SMC */ |
| 48 | #define APPLESMC_DATA_PORT 0x300 |
| 49 | /* command/status port used by Apple SMC */ |
| 50 | #define APPLESMC_CMD_PORT 0x304 |
| 51 | |
| 52 | #define APPLESMC_NR_PORTS 32 /* 0x300-0x31f */ |
| 53 | |
| 54 | #define APPLESMC_MAX_DATA_LENGTH 32 |
| 55 | |
Henrik Rydberg | 5874583 | 2010-11-10 10:58:03 +0000 | [diff] [blame] | 56 | /* wait up to 32 ms for a status change. */ |
Henrik Rydberg | a332bf9 | 2012-07-09 12:10:26 +0200 | [diff] [blame] | 57 | #define APPLESMC_MIN_WAIT 0x0010 |
Henrik Rydberg | 8c9398d | 2008-10-18 20:27:43 -0700 | [diff] [blame] | 58 | #define APPLESMC_MAX_WAIT 0x8000 |
| 59 | |
Nicolas Boichat | 6f2fad7 | 2007-05-08 00:24:52 -0700 | [diff] [blame] | 60 | #define APPLESMC_STATUS_MASK 0x0f |
| 61 | #define APPLESMC_READ_CMD 0x10 |
| 62 | #define APPLESMC_WRITE_CMD 0x11 |
| 63 | #define APPLESMC_GET_KEY_BY_INDEX_CMD 0x12 |
| 64 | #define APPLESMC_GET_KEY_TYPE_CMD 0x13 |
| 65 | |
| 66 | #define KEY_COUNT_KEY "#KEY" /* r-o ui32 */ |
| 67 | |
Henrik Rydberg | 8bd1a12 | 2008-10-18 20:27:39 -0700 | [diff] [blame] | 68 | #define LIGHT_SENSOR_LEFT_KEY "ALV0" /* r-o {alv (6-10 bytes) */ |
| 69 | #define LIGHT_SENSOR_RIGHT_KEY "ALV1" /* r-o {alv (6-10 bytes) */ |
Dmitry Torokhov | d5cf2b9 | 2007-09-26 00:01:35 -0400 | [diff] [blame] | 70 | #define BACKLIGHT_KEY "LKSB" /* w-o {lkb (2 bytes) */ |
Nicolas Boichat | 6f2fad7 | 2007-05-08 00:24:52 -0700 | [diff] [blame] | 71 | |
Dmitry Torokhov | d5cf2b9 | 2007-09-26 00:01:35 -0400 | [diff] [blame] | 72 | #define CLAMSHELL_KEY "MSLD" /* r-o ui8 (unused) */ |
Nicolas Boichat | 6f2fad7 | 2007-05-08 00:24:52 -0700 | [diff] [blame] | 73 | |
| 74 | #define MOTION_SENSOR_X_KEY "MO_X" /* r-o sp78 (2 bytes) */ |
| 75 | #define MOTION_SENSOR_Y_KEY "MO_Y" /* r-o sp78 (2 bytes) */ |
| 76 | #define MOTION_SENSOR_Z_KEY "MO_Z" /* r-o sp78 (2 bytes) */ |
| 77 | #define MOTION_SENSOR_KEY "MOCN" /* r/w ui16 */ |
| 78 | |
| 79 | #define FANS_COUNT "FNum" /* r-o ui8 */ |
| 80 | #define FANS_MANUAL "FS! " /* r-w ui16 */ |
Henrik Rydberg | 3eba2bf | 2010-11-09 15:15:08 +0000 | [diff] [blame] | 81 | #define FAN_ID_FMT "F%dID" /* r-o char[16] */ |
Nicolas Boichat | 6f2fad7 | 2007-05-08 00:24:52 -0700 | [diff] [blame] | 82 | |
Henrik Rydberg | b6e5122 | 2012-07-16 09:18:10 +0200 | [diff] [blame^] | 83 | #define TEMP_SENSOR_TYPE "sp78" |
| 84 | |
Nicolas Boichat | 6f2fad7 | 2007-05-08 00:24:52 -0700 | [diff] [blame] | 85 | /* List of keys used to read/write fan speeds */ |
Henrik Rydberg | 3eba2bf | 2010-11-09 15:15:08 +0000 | [diff] [blame] | 86 | static const char *const fan_speed_fmt[] = { |
| 87 | "F%dAc", /* actual speed */ |
| 88 | "F%dMn", /* minimum speed (rw) */ |
| 89 | "F%dMx", /* maximum speed */ |
| 90 | "F%dSf", /* safe speed - not all models */ |
| 91 | "F%dTg", /* target speed (manual: rw) */ |
Nicolas Boichat | 6f2fad7 | 2007-05-08 00:24:52 -0700 | [diff] [blame] | 92 | }; |
| 93 | |
| 94 | #define INIT_TIMEOUT_MSECS 5000 /* wait up to 5s for device init ... */ |
| 95 | #define INIT_WAIT_MSECS 50 /* ... in 50ms increments */ |
| 96 | |
Dmitry Torokhov | d5cf2b9 | 2007-09-26 00:01:35 -0400 | [diff] [blame] | 97 | #define APPLESMC_POLL_INTERVAL 50 /* msecs */ |
Nicolas Boichat | 6f2fad7 | 2007-05-08 00:24:52 -0700 | [diff] [blame] | 98 | #define APPLESMC_INPUT_FUZZ 4 /* input event threshold */ |
| 99 | #define APPLESMC_INPUT_FLAT 4 |
| 100 | |
Henrik Rydberg | 3eba2bf | 2010-11-09 15:15:08 +0000 | [diff] [blame] | 101 | #define to_index(attr) (to_sensor_dev_attr(attr)->index & 0xffff) |
| 102 | #define to_option(attr) (to_sensor_dev_attr(attr)->index >> 16) |
Henrik Rydberg | 9792dad | 2010-11-10 10:58:04 +0000 | [diff] [blame] | 103 | |
Henrik Rydberg | 9792dad | 2010-11-10 10:58:04 +0000 | [diff] [blame] | 104 | /* Dynamic device node attributes */ |
| 105 | struct applesmc_dev_attr { |
| 106 | struct sensor_device_attribute sda; /* hwmon attributes */ |
| 107 | char name[32]; /* room for node file name */ |
| 108 | }; |
| 109 | |
| 110 | /* Dynamic device node group */ |
| 111 | struct applesmc_node_group { |
| 112 | char *format; /* format string */ |
| 113 | void *show; /* show function */ |
| 114 | void *store; /* store function */ |
Henrik Rydberg | 3eba2bf | 2010-11-09 15:15:08 +0000 | [diff] [blame] | 115 | int option; /* function argument */ |
Henrik Rydberg | 9792dad | 2010-11-10 10:58:04 +0000 | [diff] [blame] | 116 | struct applesmc_dev_attr *nodes; /* dynamic node array */ |
| 117 | }; |
| 118 | |
Henrik Rydberg | 5874583 | 2010-11-10 10:58:03 +0000 | [diff] [blame] | 119 | /* AppleSMC entry - cached register information */ |
| 120 | struct applesmc_entry { |
| 121 | char key[5]; /* four-letter key code */ |
| 122 | u8 valid; /* set when entry is successfully read once */ |
| 123 | u8 len; /* bounded by APPLESMC_MAX_DATA_LENGTH */ |
| 124 | char type[5]; /* four-letter type code */ |
| 125 | u8 flags; /* 0x10: func; 0x40: write; 0x80: read */ |
| 126 | }; |
| 127 | |
| 128 | /* Register lookup and registers common to all SMCs */ |
| 129 | static struct applesmc_registers { |
| 130 | struct mutex mutex; /* register read/write mutex */ |
| 131 | unsigned int key_count; /* number of SMC registers */ |
Henrik Rydberg | 3eba2bf | 2010-11-09 15:15:08 +0000 | [diff] [blame] | 132 | unsigned int fan_count; /* number of fans */ |
Henrik Rydberg | 9792dad | 2010-11-10 10:58:04 +0000 | [diff] [blame] | 133 | unsigned int temp_count; /* number of temperature registers */ |
| 134 | unsigned int temp_begin; /* temperature lower index bound */ |
| 135 | unsigned int temp_end; /* temperature upper index bound */ |
Henrik Rydberg | 40ef06f | 2010-11-10 10:58:05 +0000 | [diff] [blame] | 136 | int num_light_sensors; /* number of light sensors */ |
| 137 | bool has_accelerometer; /* has motion sensor */ |
| 138 | bool has_key_backlight; /* has keyboard backlight */ |
Henrik Rydberg | 5874583 | 2010-11-10 10:58:03 +0000 | [diff] [blame] | 139 | bool init_complete; /* true when fully initialized */ |
| 140 | struct applesmc_entry *cache; /* cached key entries */ |
| 141 | } smcreg = { |
| 142 | .mutex = __MUTEX_INITIALIZER(smcreg.mutex), |
| 143 | }; |
| 144 | |
Nicolas Boichat | 6f2fad7 | 2007-05-08 00:24:52 -0700 | [diff] [blame] | 145 | static const int debug; |
| 146 | static struct platform_device *pdev; |
| 147 | static s16 rest_x; |
| 148 | static s16 rest_y; |
Henrik Rydberg | a976f15 | 2009-09-21 17:04:50 -0700 | [diff] [blame] | 149 | static u8 backlight_state[2]; |
| 150 | |
Tony Jones | 1beeffe | 2007-08-20 13:46:20 -0700 | [diff] [blame] | 151 | static struct device *hwmon_dev; |
Dmitry Torokhov | d5cf2b9 | 2007-09-26 00:01:35 -0400 | [diff] [blame] | 152 | static struct input_polled_dev *applesmc_idev; |
Nicolas Boichat | 6f2fad7 | 2007-05-08 00:24:52 -0700 | [diff] [blame] | 153 | |
Nicolas Boichat | 6f2fad7 | 2007-05-08 00:24:52 -0700 | [diff] [blame] | 154 | /* |
| 155 | * Last index written to key_at_index sysfs file, and value to use for all other |
| 156 | * key_at_index_* sysfs files. |
| 157 | */ |
| 158 | static unsigned int key_at_index; |
| 159 | |
| 160 | static struct workqueue_struct *applesmc_led_wq; |
| 161 | |
| 162 | /* |
Henrik Rydberg | 8c9398d | 2008-10-18 20:27:43 -0700 | [diff] [blame] | 163 | * __wait_status - Wait up to 32ms for the status port to get a certain value |
Nicolas Boichat | 6f2fad7 | 2007-05-08 00:24:52 -0700 | [diff] [blame] | 164 | * (masked with 0x0f), returning zero if the value is obtained. Callers must |
| 165 | * hold applesmc_lock. |
| 166 | */ |
| 167 | static int __wait_status(u8 val) |
| 168 | { |
Henrik Rydberg | 8c9398d | 2008-10-18 20:27:43 -0700 | [diff] [blame] | 169 | int us; |
Nicolas Boichat | 6f2fad7 | 2007-05-08 00:24:52 -0700 | [diff] [blame] | 170 | |
| 171 | val = val & APPLESMC_STATUS_MASK; |
| 172 | |
Henrik Rydberg | 8c9398d | 2008-10-18 20:27:43 -0700 | [diff] [blame] | 173 | for (us = APPLESMC_MIN_WAIT; us < APPLESMC_MAX_WAIT; us <<= 1) { |
| 174 | udelay(us); |
Guenter Roeck | 2bfe814 | 2010-11-10 10:42:21 -0800 | [diff] [blame] | 175 | if ((inb(APPLESMC_CMD_PORT) & APPLESMC_STATUS_MASK) == val) |
Nicolas Boichat | 6f2fad7 | 2007-05-08 00:24:52 -0700 | [diff] [blame] | 176 | return 0; |
Nicolas Boichat | 6f2fad7 | 2007-05-08 00:24:52 -0700 | [diff] [blame] | 177 | } |
| 178 | |
Nicolas Boichat | 6f2fad7 | 2007-05-08 00:24:52 -0700 | [diff] [blame] | 179 | return -EIO; |
| 180 | } |
| 181 | |
| 182 | /* |
Henrik Rydberg | 84d2d7f | 2008-10-18 20:27:38 -0700 | [diff] [blame] | 183 | * special treatment of command port - on newer macbooks, it seems necessary |
| 184 | * to resend the command byte before polling the status again. Callers must |
| 185 | * hold applesmc_lock. |
| 186 | */ |
| 187 | static int send_command(u8 cmd) |
| 188 | { |
Henrik Rydberg | 8c9398d | 2008-10-18 20:27:43 -0700 | [diff] [blame] | 189 | int us; |
| 190 | for (us = APPLESMC_MIN_WAIT; us < APPLESMC_MAX_WAIT; us <<= 1) { |
Henrik Rydberg | 84d2d7f | 2008-10-18 20:27:38 -0700 | [diff] [blame] | 191 | outb(cmd, APPLESMC_CMD_PORT); |
Henrik Rydberg | 8c9398d | 2008-10-18 20:27:43 -0700 | [diff] [blame] | 192 | udelay(us); |
Henrik Rydberg | 84d2d7f | 2008-10-18 20:27:38 -0700 | [diff] [blame] | 193 | if ((inb(APPLESMC_CMD_PORT) & APPLESMC_STATUS_MASK) == 0x0c) |
| 194 | return 0; |
Henrik Rydberg | 84d2d7f | 2008-10-18 20:27:38 -0700 | [diff] [blame] | 195 | } |
Henrik Rydberg | 84d2d7f | 2008-10-18 20:27:38 -0700 | [diff] [blame] | 196 | return -EIO; |
| 197 | } |
| 198 | |
Henrik Rydberg | 5874583 | 2010-11-10 10:58:03 +0000 | [diff] [blame] | 199 | static int send_argument(const char *key) |
Nicolas Boichat | 6f2fad7 | 2007-05-08 00:24:52 -0700 | [diff] [blame] | 200 | { |
| 201 | int i; |
| 202 | |
Nicolas Boichat | 6f2fad7 | 2007-05-08 00:24:52 -0700 | [diff] [blame] | 203 | for (i = 0; i < 4; i++) { |
| 204 | outb(key[i], APPLESMC_DATA_PORT); |
| 205 | if (__wait_status(0x04)) |
| 206 | return -EIO; |
| 207 | } |
Henrik Rydberg | 5874583 | 2010-11-10 10:58:03 +0000 | [diff] [blame] | 208 | return 0; |
| 209 | } |
| 210 | |
| 211 | static int read_smc(u8 cmd, const char *key, u8 *buffer, u8 len) |
| 212 | { |
| 213 | int i; |
| 214 | |
| 215 | if (send_command(cmd) || send_argument(key)) { |
Henrik Rydberg | ac852ed | 2012-06-07 04:54:29 -0400 | [diff] [blame] | 216 | pr_warn("%.4s: read arg fail\n", key); |
Henrik Rydberg | 5874583 | 2010-11-10 10:58:03 +0000 | [diff] [blame] | 217 | return -EIO; |
| 218 | } |
Nicolas Boichat | 6f2fad7 | 2007-05-08 00:24:52 -0700 | [diff] [blame] | 219 | |
| 220 | outb(len, APPLESMC_DATA_PORT); |
Nicolas Boichat | 6f2fad7 | 2007-05-08 00:24:52 -0700 | [diff] [blame] | 221 | |
| 222 | for (i = 0; i < len; i++) { |
Henrik Rydberg | 5874583 | 2010-11-10 10:58:03 +0000 | [diff] [blame] | 223 | if (__wait_status(0x05)) { |
Henrik Rydberg | ac852ed | 2012-06-07 04:54:29 -0400 | [diff] [blame] | 224 | pr_warn("%.4s: read data fail\n", key); |
Nicolas Boichat | 6f2fad7 | 2007-05-08 00:24:52 -0700 | [diff] [blame] | 225 | return -EIO; |
Henrik Rydberg | 5874583 | 2010-11-10 10:58:03 +0000 | [diff] [blame] | 226 | } |
Nicolas Boichat | 6f2fad7 | 2007-05-08 00:24:52 -0700 | [diff] [blame] | 227 | buffer[i] = inb(APPLESMC_DATA_PORT); |
Nicolas Boichat | 6f2fad7 | 2007-05-08 00:24:52 -0700 | [diff] [blame] | 228 | } |
Nicolas Boichat | 6f2fad7 | 2007-05-08 00:24:52 -0700 | [diff] [blame] | 229 | |
| 230 | return 0; |
| 231 | } |
| 232 | |
Henrik Rydberg | 5874583 | 2010-11-10 10:58:03 +0000 | [diff] [blame] | 233 | static int write_smc(u8 cmd, const char *key, const u8 *buffer, u8 len) |
Nicolas Boichat | 6f2fad7 | 2007-05-08 00:24:52 -0700 | [diff] [blame] | 234 | { |
| 235 | int i; |
| 236 | |
Henrik Rydberg | 5874583 | 2010-11-10 10:58:03 +0000 | [diff] [blame] | 237 | if (send_command(cmd) || send_argument(key)) { |
| 238 | pr_warn("%s: write arg fail\n", key); |
Nicolas Boichat | 6f2fad7 | 2007-05-08 00:24:52 -0700 | [diff] [blame] | 239 | return -EIO; |
Nicolas Boichat | 6f2fad7 | 2007-05-08 00:24:52 -0700 | [diff] [blame] | 240 | } |
| 241 | |
| 242 | outb(len, APPLESMC_DATA_PORT); |
| 243 | |
| 244 | for (i = 0; i < len; i++) { |
Henrik Rydberg | 5874583 | 2010-11-10 10:58:03 +0000 | [diff] [blame] | 245 | if (__wait_status(0x04)) { |
| 246 | pr_warn("%s: write data fail\n", key); |
Nicolas Boichat | 6f2fad7 | 2007-05-08 00:24:52 -0700 | [diff] [blame] | 247 | return -EIO; |
Henrik Rydberg | 5874583 | 2010-11-10 10:58:03 +0000 | [diff] [blame] | 248 | } |
Nicolas Boichat | 6f2fad7 | 2007-05-08 00:24:52 -0700 | [diff] [blame] | 249 | outb(buffer[i], APPLESMC_DATA_PORT); |
| 250 | } |
| 251 | |
| 252 | return 0; |
| 253 | } |
| 254 | |
Henrik Rydberg | 5874583 | 2010-11-10 10:58:03 +0000 | [diff] [blame] | 255 | static int read_register_count(unsigned int *count) |
Nicolas Boichat | 6f2fad7 | 2007-05-08 00:24:52 -0700 | [diff] [blame] | 256 | { |
Henrik Rydberg | 5874583 | 2010-11-10 10:58:03 +0000 | [diff] [blame] | 257 | __be32 be; |
| 258 | int ret; |
Nicolas Boichat | 6f2fad7 | 2007-05-08 00:24:52 -0700 | [diff] [blame] | 259 | |
Henrik Rydberg | 5874583 | 2010-11-10 10:58:03 +0000 | [diff] [blame] | 260 | ret = read_smc(APPLESMC_READ_CMD, KEY_COUNT_KEY, (u8 *)&be, 4); |
| 261 | if (ret) |
| 262 | return ret; |
Nicolas Boichat | 6f2fad7 | 2007-05-08 00:24:52 -0700 | [diff] [blame] | 263 | |
Henrik Rydberg | 5874583 | 2010-11-10 10:58:03 +0000 | [diff] [blame] | 264 | *count = be32_to_cpu(be); |
Nicolas Boichat | 6f2fad7 | 2007-05-08 00:24:52 -0700 | [diff] [blame] | 265 | return 0; |
| 266 | } |
| 267 | |
| 268 | /* |
Henrik Rydberg | 5874583 | 2010-11-10 10:58:03 +0000 | [diff] [blame] | 269 | * Serialized I/O |
| 270 | * |
| 271 | * Returns zero on success or a negative error on failure. |
| 272 | * All functions below are concurrency safe - callers should NOT hold lock. |
Nicolas Boichat | 6f2fad7 | 2007-05-08 00:24:52 -0700 | [diff] [blame] | 273 | */ |
Henrik Rydberg | 5874583 | 2010-11-10 10:58:03 +0000 | [diff] [blame] | 274 | |
| 275 | static int applesmc_read_entry(const struct applesmc_entry *entry, |
| 276 | u8 *buf, u8 len) |
Nicolas Boichat | 6f2fad7 | 2007-05-08 00:24:52 -0700 | [diff] [blame] | 277 | { |
Henrik Rydberg | 5874583 | 2010-11-10 10:58:03 +0000 | [diff] [blame] | 278 | int ret; |
Nicolas Boichat | 6f2fad7 | 2007-05-08 00:24:52 -0700 | [diff] [blame] | 279 | |
Henrik Rydberg | 5874583 | 2010-11-10 10:58:03 +0000 | [diff] [blame] | 280 | if (entry->len != len) |
| 281 | return -EINVAL; |
| 282 | mutex_lock(&smcreg.mutex); |
| 283 | ret = read_smc(APPLESMC_READ_CMD, entry->key, buf, len); |
| 284 | mutex_unlock(&smcreg.mutex); |
Nicolas Boichat | 6f2fad7 | 2007-05-08 00:24:52 -0700 | [diff] [blame] | 285 | |
Henrik Rydberg | 5874583 | 2010-11-10 10:58:03 +0000 | [diff] [blame] | 286 | return ret; |
| 287 | } |
| 288 | |
| 289 | static int applesmc_write_entry(const struct applesmc_entry *entry, |
| 290 | const u8 *buf, u8 len) |
| 291 | { |
| 292 | int ret; |
| 293 | |
| 294 | if (entry->len != len) |
| 295 | return -EINVAL; |
| 296 | mutex_lock(&smcreg.mutex); |
| 297 | ret = write_smc(APPLESMC_WRITE_CMD, entry->key, buf, len); |
| 298 | mutex_unlock(&smcreg.mutex); |
| 299 | return ret; |
| 300 | } |
| 301 | |
| 302 | static const struct applesmc_entry *applesmc_get_entry_by_index(int index) |
| 303 | { |
| 304 | struct applesmc_entry *cache = &smcreg.cache[index]; |
| 305 | u8 key[4], info[6]; |
| 306 | __be32 be; |
| 307 | int ret = 0; |
| 308 | |
| 309 | if (cache->valid) |
| 310 | return cache; |
| 311 | |
| 312 | mutex_lock(&smcreg.mutex); |
| 313 | |
| 314 | if (cache->valid) |
| 315 | goto out; |
| 316 | be = cpu_to_be32(index); |
| 317 | ret = read_smc(APPLESMC_GET_KEY_BY_INDEX_CMD, (u8 *)&be, key, 4); |
| 318 | if (ret) |
| 319 | goto out; |
| 320 | ret = read_smc(APPLESMC_GET_KEY_TYPE_CMD, key, info, 6); |
| 321 | if (ret) |
| 322 | goto out; |
| 323 | |
| 324 | memcpy(cache->key, key, 4); |
| 325 | cache->len = info[0]; |
| 326 | memcpy(cache->type, &info[1], 4); |
| 327 | cache->flags = info[5]; |
| 328 | cache->valid = 1; |
| 329 | |
| 330 | out: |
| 331 | mutex_unlock(&smcreg.mutex); |
| 332 | if (ret) |
| 333 | return ERR_PTR(ret); |
| 334 | return cache; |
| 335 | } |
| 336 | |
| 337 | static int applesmc_get_lower_bound(unsigned int *lo, const char *key) |
| 338 | { |
| 339 | int begin = 0, end = smcreg.key_count; |
| 340 | const struct applesmc_entry *entry; |
| 341 | |
| 342 | while (begin != end) { |
| 343 | int middle = begin + (end - begin) / 2; |
| 344 | entry = applesmc_get_entry_by_index(middle); |
Henrik Rydberg | 0fc86ec | 2012-01-26 06:08:41 -0500 | [diff] [blame] | 345 | if (IS_ERR(entry)) { |
| 346 | *lo = 0; |
Henrik Rydberg | 5874583 | 2010-11-10 10:58:03 +0000 | [diff] [blame] | 347 | return PTR_ERR(entry); |
Henrik Rydberg | 0fc86ec | 2012-01-26 06:08:41 -0500 | [diff] [blame] | 348 | } |
Henrik Rydberg | 5874583 | 2010-11-10 10:58:03 +0000 | [diff] [blame] | 349 | if (strcmp(entry->key, key) < 0) |
| 350 | begin = middle + 1; |
| 351 | else |
| 352 | end = middle; |
Nicolas Boichat | 6f2fad7 | 2007-05-08 00:24:52 -0700 | [diff] [blame] | 353 | } |
| 354 | |
Henrik Rydberg | 5874583 | 2010-11-10 10:58:03 +0000 | [diff] [blame] | 355 | *lo = begin; |
Nicolas Boichat | 6f2fad7 | 2007-05-08 00:24:52 -0700 | [diff] [blame] | 356 | return 0; |
| 357 | } |
| 358 | |
Henrik Rydberg | 5874583 | 2010-11-10 10:58:03 +0000 | [diff] [blame] | 359 | static int applesmc_get_upper_bound(unsigned int *hi, const char *key) |
| 360 | { |
| 361 | int begin = 0, end = smcreg.key_count; |
| 362 | const struct applesmc_entry *entry; |
| 363 | |
| 364 | while (begin != end) { |
| 365 | int middle = begin + (end - begin) / 2; |
| 366 | entry = applesmc_get_entry_by_index(middle); |
Henrik Rydberg | 0fc86ec | 2012-01-26 06:08:41 -0500 | [diff] [blame] | 367 | if (IS_ERR(entry)) { |
| 368 | *hi = smcreg.key_count; |
Henrik Rydberg | 5874583 | 2010-11-10 10:58:03 +0000 | [diff] [blame] | 369 | return PTR_ERR(entry); |
Henrik Rydberg | 0fc86ec | 2012-01-26 06:08:41 -0500 | [diff] [blame] | 370 | } |
Henrik Rydberg | 5874583 | 2010-11-10 10:58:03 +0000 | [diff] [blame] | 371 | if (strcmp(key, entry->key) < 0) |
| 372 | end = middle; |
| 373 | else |
| 374 | begin = middle + 1; |
| 375 | } |
| 376 | |
| 377 | *hi = begin; |
| 378 | return 0; |
| 379 | } |
| 380 | |
| 381 | static const struct applesmc_entry *applesmc_get_entry_by_key(const char *key) |
| 382 | { |
| 383 | int begin, end; |
| 384 | int ret; |
| 385 | |
| 386 | ret = applesmc_get_lower_bound(&begin, key); |
| 387 | if (ret) |
| 388 | return ERR_PTR(ret); |
| 389 | ret = applesmc_get_upper_bound(&end, key); |
| 390 | if (ret) |
| 391 | return ERR_PTR(ret); |
| 392 | if (end - begin != 1) |
| 393 | return ERR_PTR(-EINVAL); |
| 394 | |
| 395 | return applesmc_get_entry_by_index(begin); |
| 396 | } |
| 397 | |
| 398 | static int applesmc_read_key(const char *key, u8 *buffer, u8 len) |
| 399 | { |
| 400 | const struct applesmc_entry *entry; |
| 401 | |
| 402 | entry = applesmc_get_entry_by_key(key); |
| 403 | if (IS_ERR(entry)) |
| 404 | return PTR_ERR(entry); |
| 405 | |
| 406 | return applesmc_read_entry(entry, buffer, len); |
| 407 | } |
| 408 | |
| 409 | static int applesmc_write_key(const char *key, const u8 *buffer, u8 len) |
| 410 | { |
| 411 | const struct applesmc_entry *entry; |
| 412 | |
| 413 | entry = applesmc_get_entry_by_key(key); |
| 414 | if (IS_ERR(entry)) |
| 415 | return PTR_ERR(entry); |
| 416 | |
| 417 | return applesmc_write_entry(entry, buffer, len); |
| 418 | } |
| 419 | |
Henrik Rydberg | 40ef06f | 2010-11-10 10:58:05 +0000 | [diff] [blame] | 420 | static int applesmc_has_key(const char *key, bool *value) |
| 421 | { |
| 422 | const struct applesmc_entry *entry; |
| 423 | |
| 424 | entry = applesmc_get_entry_by_key(key); |
| 425 | if (IS_ERR(entry) && PTR_ERR(entry) != -EINVAL) |
| 426 | return PTR_ERR(entry); |
| 427 | |
| 428 | *value = !IS_ERR(entry); |
| 429 | return 0; |
| 430 | } |
| 431 | |
Nicolas Boichat | 6f2fad7 | 2007-05-08 00:24:52 -0700 | [diff] [blame] | 432 | /* |
Henrik Rydberg | edf48f3 | 2012-06-20 18:00:06 +0200 | [diff] [blame] | 433 | * applesmc_read_s16 - Read 16-bit signed big endian register |
Nicolas Boichat | 6f2fad7 | 2007-05-08 00:24:52 -0700 | [diff] [blame] | 434 | */ |
Henrik Rydberg | edf48f3 | 2012-06-20 18:00:06 +0200 | [diff] [blame] | 435 | static int applesmc_read_s16(const char *key, s16 *value) |
Nicolas Boichat | 6f2fad7 | 2007-05-08 00:24:52 -0700 | [diff] [blame] | 436 | { |
| 437 | u8 buffer[2]; |
| 438 | int ret; |
| 439 | |
Henrik Rydberg | edf48f3 | 2012-06-20 18:00:06 +0200 | [diff] [blame] | 440 | ret = applesmc_read_key(key, buffer, 2); |
| 441 | if (ret) |
| 442 | return ret; |
Nicolas Boichat | 6f2fad7 | 2007-05-08 00:24:52 -0700 | [diff] [blame] | 443 | |
| 444 | *value = ((s16)buffer[0] << 8) | buffer[1]; |
Henrik Rydberg | edf48f3 | 2012-06-20 18:00:06 +0200 | [diff] [blame] | 445 | return 0; |
Nicolas Boichat | 6f2fad7 | 2007-05-08 00:24:52 -0700 | [diff] [blame] | 446 | } |
| 447 | |
| 448 | /* |
Henrik Rydberg | 2344cd0 | 2010-11-09 15:15:02 +0000 | [diff] [blame] | 449 | * applesmc_device_init - initialize the accelerometer. Can sleep. |
Nicolas Boichat | 6f2fad7 | 2007-05-08 00:24:52 -0700 | [diff] [blame] | 450 | */ |
Henrik Rydberg | 2344cd0 | 2010-11-09 15:15:02 +0000 | [diff] [blame] | 451 | static void applesmc_device_init(void) |
Nicolas Boichat | 6f2fad7 | 2007-05-08 00:24:52 -0700 | [diff] [blame] | 452 | { |
Henrik Rydberg | 2344cd0 | 2010-11-09 15:15:02 +0000 | [diff] [blame] | 453 | int total; |
Nicolas Boichat | 6f2fad7 | 2007-05-08 00:24:52 -0700 | [diff] [blame] | 454 | u8 buffer[2]; |
| 455 | |
Henrik Rydberg | 40ef06f | 2010-11-10 10:58:05 +0000 | [diff] [blame] | 456 | if (!smcreg.has_accelerometer) |
Henrik Rydberg | 2344cd0 | 2010-11-09 15:15:02 +0000 | [diff] [blame] | 457 | return; |
Nicolas Boichat | 6f2fad7 | 2007-05-08 00:24:52 -0700 | [diff] [blame] | 458 | |
Nicolas Boichat | 6f2fad7 | 2007-05-08 00:24:52 -0700 | [diff] [blame] | 459 | for (total = INIT_TIMEOUT_MSECS; total > 0; total -= INIT_WAIT_MSECS) { |
Nicolas Boichat | 6f2fad7 | 2007-05-08 00:24:52 -0700 | [diff] [blame] | 460 | if (!applesmc_read_key(MOTION_SENSOR_KEY, buffer, 2) && |
Henrik Rydberg | 2344cd0 | 2010-11-09 15:15:02 +0000 | [diff] [blame] | 461 | (buffer[0] != 0x00 || buffer[1] != 0x00)) |
Henrik Rydberg | 5874583 | 2010-11-10 10:58:03 +0000 | [diff] [blame] | 462 | return; |
Nicolas Boichat | 6f2fad7 | 2007-05-08 00:24:52 -0700 | [diff] [blame] | 463 | buffer[0] = 0xe0; |
| 464 | buffer[1] = 0x00; |
| 465 | applesmc_write_key(MOTION_SENSOR_KEY, buffer, 2); |
| 466 | msleep(INIT_WAIT_MSECS); |
| 467 | } |
| 468 | |
Joe Perches | 1ee7c71 | 2010-11-09 15:15:03 +0000 | [diff] [blame] | 469 | pr_warn("failed to init the device\n"); |
Nicolas Boichat | 6f2fad7 | 2007-05-08 00:24:52 -0700 | [diff] [blame] | 470 | } |
| 471 | |
| 472 | /* |
Henrik Rydberg | 5874583 | 2010-11-10 10:58:03 +0000 | [diff] [blame] | 473 | * applesmc_init_smcreg_try - Try to initialize register cache. Idempotent. |
| 474 | */ |
| 475 | static int applesmc_init_smcreg_try(void) |
| 476 | { |
| 477 | struct applesmc_registers *s = &smcreg; |
Henrik Rydberg | 40ef06f | 2010-11-10 10:58:05 +0000 | [diff] [blame] | 478 | bool left_light_sensor, right_light_sensor; |
Henrik Rydberg | 3eba2bf | 2010-11-09 15:15:08 +0000 | [diff] [blame] | 479 | u8 tmp[1]; |
Henrik Rydberg | 5874583 | 2010-11-10 10:58:03 +0000 | [diff] [blame] | 480 | int ret; |
| 481 | |
| 482 | if (s->init_complete) |
| 483 | return 0; |
| 484 | |
| 485 | ret = read_register_count(&s->key_count); |
| 486 | if (ret) |
| 487 | return ret; |
| 488 | |
| 489 | if (!s->cache) |
| 490 | s->cache = kcalloc(s->key_count, sizeof(*s->cache), GFP_KERNEL); |
| 491 | if (!s->cache) |
| 492 | return -ENOMEM; |
| 493 | |
Henrik Rydberg | 3eba2bf | 2010-11-09 15:15:08 +0000 | [diff] [blame] | 494 | ret = applesmc_read_key(FANS_COUNT, tmp, 1); |
| 495 | if (ret) |
| 496 | return ret; |
| 497 | s->fan_count = tmp[0]; |
| 498 | |
Henrik Rydberg | 9792dad | 2010-11-10 10:58:04 +0000 | [diff] [blame] | 499 | ret = applesmc_get_lower_bound(&s->temp_begin, "T"); |
| 500 | if (ret) |
| 501 | return ret; |
| 502 | ret = applesmc_get_lower_bound(&s->temp_end, "U"); |
| 503 | if (ret) |
| 504 | return ret; |
| 505 | s->temp_count = s->temp_end - s->temp_begin; |
| 506 | |
Henrik Rydberg | 40ef06f | 2010-11-10 10:58:05 +0000 | [diff] [blame] | 507 | ret = applesmc_has_key(LIGHT_SENSOR_LEFT_KEY, &left_light_sensor); |
| 508 | if (ret) |
| 509 | return ret; |
| 510 | ret = applesmc_has_key(LIGHT_SENSOR_RIGHT_KEY, &right_light_sensor); |
| 511 | if (ret) |
| 512 | return ret; |
| 513 | ret = applesmc_has_key(MOTION_SENSOR_KEY, &s->has_accelerometer); |
| 514 | if (ret) |
| 515 | return ret; |
| 516 | ret = applesmc_has_key(BACKLIGHT_KEY, &s->has_key_backlight); |
| 517 | if (ret) |
| 518 | return ret; |
| 519 | |
| 520 | s->num_light_sensors = left_light_sensor + right_light_sensor; |
Henrik Rydberg | 5874583 | 2010-11-10 10:58:03 +0000 | [diff] [blame] | 521 | s->init_complete = true; |
| 522 | |
Henrik Rydberg | 3eba2bf | 2010-11-09 15:15:08 +0000 | [diff] [blame] | 523 | pr_info("key=%d fan=%d temp=%d acc=%d lux=%d kbd=%d\n", |
| 524 | s->key_count, s->fan_count, s->temp_count, |
Henrik Rydberg | 40ef06f | 2010-11-10 10:58:05 +0000 | [diff] [blame] | 525 | s->has_accelerometer, |
| 526 | s->num_light_sensors, |
| 527 | s->has_key_backlight); |
Henrik Rydberg | 5874583 | 2010-11-10 10:58:03 +0000 | [diff] [blame] | 528 | |
| 529 | return 0; |
| 530 | } |
| 531 | |
| 532 | /* |
| 533 | * applesmc_init_smcreg - Initialize register cache. |
| 534 | * |
| 535 | * Retries until initialization is successful, or the operation times out. |
| 536 | * |
| 537 | */ |
| 538 | static int applesmc_init_smcreg(void) |
| 539 | { |
| 540 | int ms, ret; |
| 541 | |
| 542 | for (ms = 0; ms < INIT_TIMEOUT_MSECS; ms += INIT_WAIT_MSECS) { |
| 543 | ret = applesmc_init_smcreg_try(); |
| 544 | if (!ret) { |
| 545 | if (ms) |
| 546 | pr_info("init_smcreg() took %d ms\n", ms); |
| 547 | return 0; |
| 548 | } |
| 549 | msleep(INIT_WAIT_MSECS); |
| 550 | } |
| 551 | |
| 552 | kfree(smcreg.cache); |
| 553 | smcreg.cache = NULL; |
| 554 | |
| 555 | return ret; |
| 556 | } |
| 557 | |
| 558 | static void applesmc_destroy_smcreg(void) |
| 559 | { |
| 560 | kfree(smcreg.cache); |
| 561 | smcreg.cache = NULL; |
| 562 | smcreg.init_complete = false; |
| 563 | } |
| 564 | |
Nicolas Boichat | 6f2fad7 | 2007-05-08 00:24:52 -0700 | [diff] [blame] | 565 | /* Device model stuff */ |
| 566 | static int applesmc_probe(struct platform_device *dev) |
| 567 | { |
Henrik Rydberg | 5874583 | 2010-11-10 10:58:03 +0000 | [diff] [blame] | 568 | int ret; |
| 569 | |
| 570 | ret = applesmc_init_smcreg(); |
| 571 | if (ret) |
| 572 | return ret; |
| 573 | |
Henrik Rydberg | 2344cd0 | 2010-11-09 15:15:02 +0000 | [diff] [blame] | 574 | applesmc_device_init(); |
Nicolas Boichat | 6f2fad7 | 2007-05-08 00:24:52 -0700 | [diff] [blame] | 575 | |
Nicolas Boichat | 6f2fad7 | 2007-05-08 00:24:52 -0700 | [diff] [blame] | 576 | return 0; |
| 577 | } |
| 578 | |
Henrik Rydberg | a976f15 | 2009-09-21 17:04:50 -0700 | [diff] [blame] | 579 | /* Synchronize device with memorized backlight state */ |
| 580 | static int applesmc_pm_resume(struct device *dev) |
Nicolas Boichat | 6f2fad7 | 2007-05-08 00:24:52 -0700 | [diff] [blame] | 581 | { |
Henrik Rydberg | 40ef06f | 2010-11-10 10:58:05 +0000 | [diff] [blame] | 582 | if (smcreg.has_key_backlight) |
Henrik Rydberg | a976f15 | 2009-09-21 17:04:50 -0700 | [diff] [blame] | 583 | applesmc_write_key(BACKLIGHT_KEY, backlight_state, 2); |
Henrik Rydberg | a976f15 | 2009-09-21 17:04:50 -0700 | [diff] [blame] | 584 | return 0; |
Nicolas Boichat | 6f2fad7 | 2007-05-08 00:24:52 -0700 | [diff] [blame] | 585 | } |
| 586 | |
Henrik Rydberg | a976f15 | 2009-09-21 17:04:50 -0700 | [diff] [blame] | 587 | /* Reinitialize device on resume from hibernation */ |
| 588 | static int applesmc_pm_restore(struct device *dev) |
| 589 | { |
Henrik Rydberg | 2344cd0 | 2010-11-09 15:15:02 +0000 | [diff] [blame] | 590 | applesmc_device_init(); |
Henrik Rydberg | a976f15 | 2009-09-21 17:04:50 -0700 | [diff] [blame] | 591 | return applesmc_pm_resume(dev); |
| 592 | } |
| 593 | |
Alexey Dobriyan | 4714521 | 2009-12-14 18:00:08 -0800 | [diff] [blame] | 594 | static const struct dev_pm_ops applesmc_pm_ops = { |
Henrik Rydberg | a976f15 | 2009-09-21 17:04:50 -0700 | [diff] [blame] | 595 | .resume = applesmc_pm_resume, |
| 596 | .restore = applesmc_pm_restore, |
| 597 | }; |
| 598 | |
Nicolas Boichat | 6f2fad7 | 2007-05-08 00:24:52 -0700 | [diff] [blame] | 599 | static struct platform_driver applesmc_driver = { |
| 600 | .probe = applesmc_probe, |
Nicolas Boichat | 6f2fad7 | 2007-05-08 00:24:52 -0700 | [diff] [blame] | 601 | .driver = { |
| 602 | .name = "applesmc", |
| 603 | .owner = THIS_MODULE, |
Henrik Rydberg | a976f15 | 2009-09-21 17:04:50 -0700 | [diff] [blame] | 604 | .pm = &applesmc_pm_ops, |
Nicolas Boichat | 6f2fad7 | 2007-05-08 00:24:52 -0700 | [diff] [blame] | 605 | }, |
| 606 | }; |
| 607 | |
| 608 | /* |
| 609 | * applesmc_calibrate - Set our "resting" values. Callers must |
| 610 | * hold applesmc_lock. |
| 611 | */ |
| 612 | static void applesmc_calibrate(void) |
| 613 | { |
Henrik Rydberg | edf48f3 | 2012-06-20 18:00:06 +0200 | [diff] [blame] | 614 | applesmc_read_s16(MOTION_SENSOR_X_KEY, &rest_x); |
| 615 | applesmc_read_s16(MOTION_SENSOR_Y_KEY, &rest_y); |
Nicolas Boichat | 6f2fad7 | 2007-05-08 00:24:52 -0700 | [diff] [blame] | 616 | rest_x = -rest_x; |
| 617 | } |
| 618 | |
Dmitry Torokhov | d5cf2b9 | 2007-09-26 00:01:35 -0400 | [diff] [blame] | 619 | static void applesmc_idev_poll(struct input_polled_dev *dev) |
Nicolas Boichat | 6f2fad7 | 2007-05-08 00:24:52 -0700 | [diff] [blame] | 620 | { |
Dmitry Torokhov | d5cf2b9 | 2007-09-26 00:01:35 -0400 | [diff] [blame] | 621 | struct input_dev *idev = dev->input; |
Nicolas Boichat | 6f2fad7 | 2007-05-08 00:24:52 -0700 | [diff] [blame] | 622 | s16 x, y; |
| 623 | |
Henrik Rydberg | edf48f3 | 2012-06-20 18:00:06 +0200 | [diff] [blame] | 624 | if (applesmc_read_s16(MOTION_SENSOR_X_KEY, &x)) |
Henrik Rydberg | 5874583 | 2010-11-10 10:58:03 +0000 | [diff] [blame] | 625 | return; |
Henrik Rydberg | edf48f3 | 2012-06-20 18:00:06 +0200 | [diff] [blame] | 626 | if (applesmc_read_s16(MOTION_SENSOR_Y_KEY, &y)) |
Henrik Rydberg | 5874583 | 2010-11-10 10:58:03 +0000 | [diff] [blame] | 627 | return; |
Nicolas Boichat | 6f2fad7 | 2007-05-08 00:24:52 -0700 | [diff] [blame] | 628 | |
| 629 | x = -x; |
Dmitry Torokhov | d5cf2b9 | 2007-09-26 00:01:35 -0400 | [diff] [blame] | 630 | input_report_abs(idev, ABS_X, x - rest_x); |
| 631 | input_report_abs(idev, ABS_Y, y - rest_y); |
| 632 | input_sync(idev); |
Nicolas Boichat | 6f2fad7 | 2007-05-08 00:24:52 -0700 | [diff] [blame] | 633 | } |
| 634 | |
| 635 | /* Sysfs Files */ |
| 636 | |
Nicolas Boichat | fa74419 | 2007-05-23 13:58:13 -0700 | [diff] [blame] | 637 | static ssize_t applesmc_name_show(struct device *dev, |
| 638 | struct device_attribute *attr, char *buf) |
| 639 | { |
| 640 | return snprintf(buf, PAGE_SIZE, "applesmc\n"); |
| 641 | } |
| 642 | |
Nicolas Boichat | 6f2fad7 | 2007-05-08 00:24:52 -0700 | [diff] [blame] | 643 | static ssize_t applesmc_position_show(struct device *dev, |
| 644 | struct device_attribute *attr, char *buf) |
| 645 | { |
| 646 | int ret; |
| 647 | s16 x, y, z; |
| 648 | |
Henrik Rydberg | edf48f3 | 2012-06-20 18:00:06 +0200 | [diff] [blame] | 649 | ret = applesmc_read_s16(MOTION_SENSOR_X_KEY, &x); |
Nicolas Boichat | 6f2fad7 | 2007-05-08 00:24:52 -0700 | [diff] [blame] | 650 | if (ret) |
| 651 | goto out; |
Henrik Rydberg | edf48f3 | 2012-06-20 18:00:06 +0200 | [diff] [blame] | 652 | ret = applesmc_read_s16(MOTION_SENSOR_Y_KEY, &y); |
Nicolas Boichat | 6f2fad7 | 2007-05-08 00:24:52 -0700 | [diff] [blame] | 653 | if (ret) |
| 654 | goto out; |
Henrik Rydberg | edf48f3 | 2012-06-20 18:00:06 +0200 | [diff] [blame] | 655 | ret = applesmc_read_s16(MOTION_SENSOR_Z_KEY, &z); |
Nicolas Boichat | 6f2fad7 | 2007-05-08 00:24:52 -0700 | [diff] [blame] | 656 | if (ret) |
| 657 | goto out; |
| 658 | |
| 659 | out: |
Nicolas Boichat | 6f2fad7 | 2007-05-08 00:24:52 -0700 | [diff] [blame] | 660 | if (ret) |
| 661 | return ret; |
| 662 | else |
| 663 | return snprintf(buf, PAGE_SIZE, "(%d,%d,%d)\n", x, y, z); |
| 664 | } |
| 665 | |
| 666 | static ssize_t applesmc_light_show(struct device *dev, |
| 667 | struct device_attribute *attr, char *sysfsbuf) |
| 668 | { |
Henrik Rydberg | 5874583 | 2010-11-10 10:58:03 +0000 | [diff] [blame] | 669 | const struct applesmc_entry *entry; |
Henrik Rydberg | 8bd1a12 | 2008-10-18 20:27:39 -0700 | [diff] [blame] | 670 | static int data_length; |
Nicolas Boichat | 6f2fad7 | 2007-05-08 00:24:52 -0700 | [diff] [blame] | 671 | int ret; |
| 672 | u8 left = 0, right = 0; |
Henrik Rydberg | 5874583 | 2010-11-10 10:58:03 +0000 | [diff] [blame] | 673 | u8 buffer[10]; |
Nicolas Boichat | 6f2fad7 | 2007-05-08 00:24:52 -0700 | [diff] [blame] | 674 | |
Henrik Rydberg | 8bd1a12 | 2008-10-18 20:27:39 -0700 | [diff] [blame] | 675 | if (!data_length) { |
Henrik Rydberg | 5874583 | 2010-11-10 10:58:03 +0000 | [diff] [blame] | 676 | entry = applesmc_get_entry_by_key(LIGHT_SENSOR_LEFT_KEY); |
| 677 | if (IS_ERR(entry)) |
| 678 | return PTR_ERR(entry); |
| 679 | if (entry->len > 10) |
| 680 | return -ENXIO; |
| 681 | data_length = entry->len; |
Joe Perches | 1ee7c71 | 2010-11-09 15:15:03 +0000 | [diff] [blame] | 682 | pr_info("light sensor data length set to %d\n", data_length); |
Henrik Rydberg | 8bd1a12 | 2008-10-18 20:27:39 -0700 | [diff] [blame] | 683 | } |
| 684 | |
| 685 | ret = applesmc_read_key(LIGHT_SENSOR_LEFT_KEY, buffer, data_length); |
Alex Murray | c3d6362 | 2009-01-15 13:51:08 -0800 | [diff] [blame] | 686 | /* newer macbooks report a single 10-bit bigendian value */ |
| 687 | if (data_length == 10) { |
| 688 | left = be16_to_cpu(*(__be16 *)(buffer + 6)) >> 2; |
| 689 | goto out; |
| 690 | } |
Nicolas Boichat | 6f2fad7 | 2007-05-08 00:24:52 -0700 | [diff] [blame] | 691 | left = buffer[2]; |
| 692 | if (ret) |
| 693 | goto out; |
Henrik Rydberg | 8bd1a12 | 2008-10-18 20:27:39 -0700 | [diff] [blame] | 694 | ret = applesmc_read_key(LIGHT_SENSOR_RIGHT_KEY, buffer, data_length); |
Nicolas Boichat | 6f2fad7 | 2007-05-08 00:24:52 -0700 | [diff] [blame] | 695 | right = buffer[2]; |
| 696 | |
| 697 | out: |
Nicolas Boichat | 6f2fad7 | 2007-05-08 00:24:52 -0700 | [diff] [blame] | 698 | if (ret) |
| 699 | return ret; |
| 700 | else |
| 701 | return snprintf(sysfsbuf, PAGE_SIZE, "(%d,%d)\n", left, right); |
| 702 | } |
| 703 | |
Alex Murray | fa5575c | 2010-05-27 19:58:54 +0200 | [diff] [blame] | 704 | /* Displays sensor key as label */ |
| 705 | static ssize_t applesmc_show_sensor_label(struct device *dev, |
| 706 | struct device_attribute *devattr, char *sysfsbuf) |
| 707 | { |
Henrik Rydberg | 9792dad | 2010-11-10 10:58:04 +0000 | [diff] [blame] | 708 | int index = smcreg.temp_begin + to_index(devattr); |
| 709 | const struct applesmc_entry *entry; |
Alex Murray | fa5575c | 2010-05-27 19:58:54 +0200 | [diff] [blame] | 710 | |
Henrik Rydberg | 9792dad | 2010-11-10 10:58:04 +0000 | [diff] [blame] | 711 | entry = applesmc_get_entry_by_index(index); |
| 712 | if (IS_ERR(entry)) |
| 713 | return PTR_ERR(entry); |
| 714 | |
| 715 | return snprintf(sysfsbuf, PAGE_SIZE, "%s\n", entry->key); |
Alex Murray | fa5575c | 2010-05-27 19:58:54 +0200 | [diff] [blame] | 716 | } |
| 717 | |
Nicolas Boichat | 6f2fad7 | 2007-05-08 00:24:52 -0700 | [diff] [blame] | 718 | /* Displays degree Celsius * 1000 */ |
| 719 | static ssize_t applesmc_show_temperature(struct device *dev, |
| 720 | struct device_attribute *devattr, char *sysfsbuf) |
| 721 | { |
Henrik Rydberg | 9792dad | 2010-11-10 10:58:04 +0000 | [diff] [blame] | 722 | int index = smcreg.temp_begin + to_index(devattr); |
| 723 | const struct applesmc_entry *entry; |
Nicolas Boichat | 6f2fad7 | 2007-05-08 00:24:52 -0700 | [diff] [blame] | 724 | int ret; |
Henrik Rydberg | b6e5122 | 2012-07-16 09:18:10 +0200 | [diff] [blame^] | 725 | s16 value; |
| 726 | int temp; |
Nicolas Boichat | 6f2fad7 | 2007-05-08 00:24:52 -0700 | [diff] [blame] | 727 | |
Henrik Rydberg | 9792dad | 2010-11-10 10:58:04 +0000 | [diff] [blame] | 728 | entry = applesmc_get_entry_by_index(index); |
| 729 | if (IS_ERR(entry)) |
| 730 | return PTR_ERR(entry); |
Henrik Rydberg | b6e5122 | 2012-07-16 09:18:10 +0200 | [diff] [blame^] | 731 | if (strcmp(entry->type, TEMP_SENSOR_TYPE)) |
Henrik Rydberg | dcdea26 | 2010-11-09 15:15:06 +0000 | [diff] [blame] | 732 | return -EINVAL; |
Henrik Rydberg | 9792dad | 2010-11-10 10:58:04 +0000 | [diff] [blame] | 733 | |
Henrik Rydberg | b6e5122 | 2012-07-16 09:18:10 +0200 | [diff] [blame^] | 734 | ret = applesmc_read_s16(entry->key, &value); |
Henrik Rydberg | 9792dad | 2010-11-10 10:58:04 +0000 | [diff] [blame] | 735 | if (ret) |
| 736 | return ret; |
| 737 | |
Henrik Rydberg | b6e5122 | 2012-07-16 09:18:10 +0200 | [diff] [blame^] | 738 | temp = 250 * (value >> 6); |
Nicolas Boichat | 6f2fad7 | 2007-05-08 00:24:52 -0700 | [diff] [blame] | 739 | |
Henrik Rydberg | b6e5122 | 2012-07-16 09:18:10 +0200 | [diff] [blame^] | 740 | return snprintf(sysfsbuf, PAGE_SIZE, "%d\n", temp); |
Nicolas Boichat | 6f2fad7 | 2007-05-08 00:24:52 -0700 | [diff] [blame] | 741 | } |
| 742 | |
| 743 | static ssize_t applesmc_show_fan_speed(struct device *dev, |
| 744 | struct device_attribute *attr, char *sysfsbuf) |
| 745 | { |
| 746 | int ret; |
| 747 | unsigned int speed = 0; |
| 748 | char newkey[5]; |
| 749 | u8 buffer[2]; |
Nicolas Boichat | 6f2fad7 | 2007-05-08 00:24:52 -0700 | [diff] [blame] | 750 | |
Henrik Rydberg | 3eba2bf | 2010-11-09 15:15:08 +0000 | [diff] [blame] | 751 | sprintf(newkey, fan_speed_fmt[to_option(attr)], to_index(attr)); |
Nicolas Boichat | 6f2fad7 | 2007-05-08 00:24:52 -0700 | [diff] [blame] | 752 | |
Nicolas Boichat | 6f2fad7 | 2007-05-08 00:24:52 -0700 | [diff] [blame] | 753 | ret = applesmc_read_key(newkey, buffer, 2); |
| 754 | speed = ((buffer[0] << 8 | buffer[1]) >> 2); |
| 755 | |
Nicolas Boichat | 6f2fad7 | 2007-05-08 00:24:52 -0700 | [diff] [blame] | 756 | if (ret) |
| 757 | return ret; |
| 758 | else |
| 759 | return snprintf(sysfsbuf, PAGE_SIZE, "%u\n", speed); |
| 760 | } |
| 761 | |
| 762 | static ssize_t applesmc_store_fan_speed(struct device *dev, |
| 763 | struct device_attribute *attr, |
| 764 | const char *sysfsbuf, size_t count) |
| 765 | { |
| 766 | int ret; |
Guenter Roeck | 2bfe814 | 2010-11-10 10:42:21 -0800 | [diff] [blame] | 767 | unsigned long speed; |
Nicolas Boichat | 6f2fad7 | 2007-05-08 00:24:52 -0700 | [diff] [blame] | 768 | char newkey[5]; |
| 769 | u8 buffer[2]; |
Nicolas Boichat | 6f2fad7 | 2007-05-08 00:24:52 -0700 | [diff] [blame] | 770 | |
Frans Meulenbroeks | 179c4fd | 2012-01-04 20:58:52 +0100 | [diff] [blame] | 771 | if (kstrtoul(sysfsbuf, 10, &speed) < 0 || speed >= 0x4000) |
Guenter Roeck | 2bfe814 | 2010-11-10 10:42:21 -0800 | [diff] [blame] | 772 | return -EINVAL; /* Bigger than a 14-bit value */ |
Nicolas Boichat | 6f2fad7 | 2007-05-08 00:24:52 -0700 | [diff] [blame] | 773 | |
Henrik Rydberg | 3eba2bf | 2010-11-09 15:15:08 +0000 | [diff] [blame] | 774 | sprintf(newkey, fan_speed_fmt[to_option(attr)], to_index(attr)); |
Nicolas Boichat | 6f2fad7 | 2007-05-08 00:24:52 -0700 | [diff] [blame] | 775 | |
Nicolas Boichat | 6f2fad7 | 2007-05-08 00:24:52 -0700 | [diff] [blame] | 776 | buffer[0] = (speed >> 6) & 0xff; |
| 777 | buffer[1] = (speed << 2) & 0xff; |
| 778 | ret = applesmc_write_key(newkey, buffer, 2); |
| 779 | |
Nicolas Boichat | 6f2fad7 | 2007-05-08 00:24:52 -0700 | [diff] [blame] | 780 | if (ret) |
| 781 | return ret; |
| 782 | else |
| 783 | return count; |
| 784 | } |
| 785 | |
| 786 | static ssize_t applesmc_show_fan_manual(struct device *dev, |
Henrik Rydberg | 3eba2bf | 2010-11-09 15:15:08 +0000 | [diff] [blame] | 787 | struct device_attribute *attr, char *sysfsbuf) |
Nicolas Boichat | 6f2fad7 | 2007-05-08 00:24:52 -0700 | [diff] [blame] | 788 | { |
| 789 | int ret; |
| 790 | u16 manual = 0; |
| 791 | u8 buffer[2]; |
Nicolas Boichat | 6f2fad7 | 2007-05-08 00:24:52 -0700 | [diff] [blame] | 792 | |
Nicolas Boichat | 6f2fad7 | 2007-05-08 00:24:52 -0700 | [diff] [blame] | 793 | ret = applesmc_read_key(FANS_MANUAL, buffer, 2); |
Henrik Rydberg | 3eba2bf | 2010-11-09 15:15:08 +0000 | [diff] [blame] | 794 | manual = ((buffer[0] << 8 | buffer[1]) >> to_index(attr)) & 0x01; |
Nicolas Boichat | 6f2fad7 | 2007-05-08 00:24:52 -0700 | [diff] [blame] | 795 | |
Nicolas Boichat | 6f2fad7 | 2007-05-08 00:24:52 -0700 | [diff] [blame] | 796 | if (ret) |
| 797 | return ret; |
| 798 | else |
| 799 | return snprintf(sysfsbuf, PAGE_SIZE, "%d\n", manual); |
| 800 | } |
| 801 | |
| 802 | static ssize_t applesmc_store_fan_manual(struct device *dev, |
Henrik Rydberg | 3eba2bf | 2010-11-09 15:15:08 +0000 | [diff] [blame] | 803 | struct device_attribute *attr, |
Nicolas Boichat | 6f2fad7 | 2007-05-08 00:24:52 -0700 | [diff] [blame] | 804 | const char *sysfsbuf, size_t count) |
| 805 | { |
| 806 | int ret; |
| 807 | u8 buffer[2]; |
Guenter Roeck | 2bfe814 | 2010-11-10 10:42:21 -0800 | [diff] [blame] | 808 | unsigned long input; |
Nicolas Boichat | 6f2fad7 | 2007-05-08 00:24:52 -0700 | [diff] [blame] | 809 | u16 val; |
Nicolas Boichat | 6f2fad7 | 2007-05-08 00:24:52 -0700 | [diff] [blame] | 810 | |
Frans Meulenbroeks | 179c4fd | 2012-01-04 20:58:52 +0100 | [diff] [blame] | 811 | if (kstrtoul(sysfsbuf, 10, &input) < 0) |
Guenter Roeck | 2bfe814 | 2010-11-10 10:42:21 -0800 | [diff] [blame] | 812 | return -EINVAL; |
Nicolas Boichat | 6f2fad7 | 2007-05-08 00:24:52 -0700 | [diff] [blame] | 813 | |
Nicolas Boichat | 6f2fad7 | 2007-05-08 00:24:52 -0700 | [diff] [blame] | 814 | ret = applesmc_read_key(FANS_MANUAL, buffer, 2); |
| 815 | val = (buffer[0] << 8 | buffer[1]); |
| 816 | if (ret) |
| 817 | goto out; |
| 818 | |
| 819 | if (input) |
Henrik Rydberg | 3eba2bf | 2010-11-09 15:15:08 +0000 | [diff] [blame] | 820 | val = val | (0x01 << to_index(attr)); |
Nicolas Boichat | 6f2fad7 | 2007-05-08 00:24:52 -0700 | [diff] [blame] | 821 | else |
Henrik Rydberg | 3eba2bf | 2010-11-09 15:15:08 +0000 | [diff] [blame] | 822 | val = val & ~(0x01 << to_index(attr)); |
Nicolas Boichat | 6f2fad7 | 2007-05-08 00:24:52 -0700 | [diff] [blame] | 823 | |
| 824 | buffer[0] = (val >> 8) & 0xFF; |
| 825 | buffer[1] = val & 0xFF; |
| 826 | |
| 827 | ret = applesmc_write_key(FANS_MANUAL, buffer, 2); |
| 828 | |
| 829 | out: |
Nicolas Boichat | 6f2fad7 | 2007-05-08 00:24:52 -0700 | [diff] [blame] | 830 | if (ret) |
| 831 | return ret; |
| 832 | else |
| 833 | return count; |
| 834 | } |
| 835 | |
| 836 | static ssize_t applesmc_show_fan_position(struct device *dev, |
| 837 | struct device_attribute *attr, char *sysfsbuf) |
| 838 | { |
| 839 | int ret; |
| 840 | char newkey[5]; |
| 841 | u8 buffer[17]; |
Nicolas Boichat | 6f2fad7 | 2007-05-08 00:24:52 -0700 | [diff] [blame] | 842 | |
Henrik Rydberg | 3eba2bf | 2010-11-09 15:15:08 +0000 | [diff] [blame] | 843 | sprintf(newkey, FAN_ID_FMT, to_index(attr)); |
Nicolas Boichat | 6f2fad7 | 2007-05-08 00:24:52 -0700 | [diff] [blame] | 844 | |
Nicolas Boichat | 6f2fad7 | 2007-05-08 00:24:52 -0700 | [diff] [blame] | 845 | ret = applesmc_read_key(newkey, buffer, 16); |
| 846 | buffer[16] = 0; |
| 847 | |
Nicolas Boichat | 6f2fad7 | 2007-05-08 00:24:52 -0700 | [diff] [blame] | 848 | if (ret) |
| 849 | return ret; |
| 850 | else |
| 851 | return snprintf(sysfsbuf, PAGE_SIZE, "%s\n", buffer+4); |
| 852 | } |
| 853 | |
| 854 | static ssize_t applesmc_calibrate_show(struct device *dev, |
| 855 | struct device_attribute *attr, char *sysfsbuf) |
| 856 | { |
| 857 | return snprintf(sysfsbuf, PAGE_SIZE, "(%d,%d)\n", rest_x, rest_y); |
| 858 | } |
| 859 | |
| 860 | static ssize_t applesmc_calibrate_store(struct device *dev, |
| 861 | struct device_attribute *attr, const char *sysfsbuf, size_t count) |
| 862 | { |
Nicolas Boichat | 6f2fad7 | 2007-05-08 00:24:52 -0700 | [diff] [blame] | 863 | applesmc_calibrate(); |
Nicolas Boichat | 6f2fad7 | 2007-05-08 00:24:52 -0700 | [diff] [blame] | 864 | |
| 865 | return count; |
| 866 | } |
| 867 | |
Nicolas Boichat | 6f2fad7 | 2007-05-08 00:24:52 -0700 | [diff] [blame] | 868 | static void applesmc_backlight_set(struct work_struct *work) |
| 869 | { |
Henrik Rydberg | a976f15 | 2009-09-21 17:04:50 -0700 | [diff] [blame] | 870 | applesmc_write_key(BACKLIGHT_KEY, backlight_state, 2); |
Nicolas Boichat | 6f2fad7 | 2007-05-08 00:24:52 -0700 | [diff] [blame] | 871 | } |
| 872 | static DECLARE_WORK(backlight_work, &applesmc_backlight_set); |
| 873 | |
| 874 | static void applesmc_brightness_set(struct led_classdev *led_cdev, |
| 875 | enum led_brightness value) |
| 876 | { |
| 877 | int ret; |
| 878 | |
Henrik Rydberg | a976f15 | 2009-09-21 17:04:50 -0700 | [diff] [blame] | 879 | backlight_state[0] = value; |
Nicolas Boichat | 6f2fad7 | 2007-05-08 00:24:52 -0700 | [diff] [blame] | 880 | ret = queue_work(applesmc_led_wq, &backlight_work); |
| 881 | |
| 882 | if (debug && (!ret)) |
| 883 | printk(KERN_DEBUG "applesmc: work was already on the queue.\n"); |
| 884 | } |
| 885 | |
| 886 | static ssize_t applesmc_key_count_show(struct device *dev, |
| 887 | struct device_attribute *attr, char *sysfsbuf) |
| 888 | { |
| 889 | int ret; |
| 890 | u8 buffer[4]; |
| 891 | u32 count; |
| 892 | |
Nicolas Boichat | 6f2fad7 | 2007-05-08 00:24:52 -0700 | [diff] [blame] | 893 | ret = applesmc_read_key(KEY_COUNT_KEY, buffer, 4); |
| 894 | count = ((u32)buffer[0]<<24) + ((u32)buffer[1]<<16) + |
| 895 | ((u32)buffer[2]<<8) + buffer[3]; |
| 896 | |
Nicolas Boichat | 6f2fad7 | 2007-05-08 00:24:52 -0700 | [diff] [blame] | 897 | if (ret) |
| 898 | return ret; |
| 899 | else |
| 900 | return snprintf(sysfsbuf, PAGE_SIZE, "%d\n", count); |
| 901 | } |
| 902 | |
| 903 | static ssize_t applesmc_key_at_index_read_show(struct device *dev, |
| 904 | struct device_attribute *attr, char *sysfsbuf) |
| 905 | { |
Henrik Rydberg | 5874583 | 2010-11-10 10:58:03 +0000 | [diff] [blame] | 906 | const struct applesmc_entry *entry; |
Nicolas Boichat | 6f2fad7 | 2007-05-08 00:24:52 -0700 | [diff] [blame] | 907 | int ret; |
| 908 | |
Henrik Rydberg | 5874583 | 2010-11-10 10:58:03 +0000 | [diff] [blame] | 909 | entry = applesmc_get_entry_by_index(key_at_index); |
| 910 | if (IS_ERR(entry)) |
| 911 | return PTR_ERR(entry); |
| 912 | ret = applesmc_read_entry(entry, sysfsbuf, entry->len); |
| 913 | if (ret) |
Nicolas Boichat | 6f2fad7 | 2007-05-08 00:24:52 -0700 | [diff] [blame] | 914 | return ret; |
Nicolas Boichat | 6f2fad7 | 2007-05-08 00:24:52 -0700 | [diff] [blame] | 915 | |
Henrik Rydberg | 5874583 | 2010-11-10 10:58:03 +0000 | [diff] [blame] | 916 | return entry->len; |
Nicolas Boichat | 6f2fad7 | 2007-05-08 00:24:52 -0700 | [diff] [blame] | 917 | } |
| 918 | |
| 919 | static ssize_t applesmc_key_at_index_data_length_show(struct device *dev, |
| 920 | struct device_attribute *attr, char *sysfsbuf) |
| 921 | { |
Henrik Rydberg | 5874583 | 2010-11-10 10:58:03 +0000 | [diff] [blame] | 922 | const struct applesmc_entry *entry; |
Nicolas Boichat | 6f2fad7 | 2007-05-08 00:24:52 -0700 | [diff] [blame] | 923 | |
Henrik Rydberg | 5874583 | 2010-11-10 10:58:03 +0000 | [diff] [blame] | 924 | entry = applesmc_get_entry_by_index(key_at_index); |
| 925 | if (IS_ERR(entry)) |
| 926 | return PTR_ERR(entry); |
Nicolas Boichat | 6f2fad7 | 2007-05-08 00:24:52 -0700 | [diff] [blame] | 927 | |
Henrik Rydberg | 5874583 | 2010-11-10 10:58:03 +0000 | [diff] [blame] | 928 | return snprintf(sysfsbuf, PAGE_SIZE, "%d\n", entry->len); |
Nicolas Boichat | 6f2fad7 | 2007-05-08 00:24:52 -0700 | [diff] [blame] | 929 | } |
| 930 | |
| 931 | static ssize_t applesmc_key_at_index_type_show(struct device *dev, |
| 932 | struct device_attribute *attr, char *sysfsbuf) |
| 933 | { |
Henrik Rydberg | 5874583 | 2010-11-10 10:58:03 +0000 | [diff] [blame] | 934 | const struct applesmc_entry *entry; |
Nicolas Boichat | 6f2fad7 | 2007-05-08 00:24:52 -0700 | [diff] [blame] | 935 | |
Henrik Rydberg | 5874583 | 2010-11-10 10:58:03 +0000 | [diff] [blame] | 936 | entry = applesmc_get_entry_by_index(key_at_index); |
| 937 | if (IS_ERR(entry)) |
| 938 | return PTR_ERR(entry); |
Nicolas Boichat | 6f2fad7 | 2007-05-08 00:24:52 -0700 | [diff] [blame] | 939 | |
Henrik Rydberg | 5874583 | 2010-11-10 10:58:03 +0000 | [diff] [blame] | 940 | return snprintf(sysfsbuf, PAGE_SIZE, "%s\n", entry->type); |
Nicolas Boichat | 6f2fad7 | 2007-05-08 00:24:52 -0700 | [diff] [blame] | 941 | } |
| 942 | |
| 943 | static ssize_t applesmc_key_at_index_name_show(struct device *dev, |
| 944 | struct device_attribute *attr, char *sysfsbuf) |
| 945 | { |
Henrik Rydberg | 5874583 | 2010-11-10 10:58:03 +0000 | [diff] [blame] | 946 | const struct applesmc_entry *entry; |
Nicolas Boichat | 6f2fad7 | 2007-05-08 00:24:52 -0700 | [diff] [blame] | 947 | |
Henrik Rydberg | 5874583 | 2010-11-10 10:58:03 +0000 | [diff] [blame] | 948 | entry = applesmc_get_entry_by_index(key_at_index); |
| 949 | if (IS_ERR(entry)) |
| 950 | return PTR_ERR(entry); |
Nicolas Boichat | 6f2fad7 | 2007-05-08 00:24:52 -0700 | [diff] [blame] | 951 | |
Henrik Rydberg | 5874583 | 2010-11-10 10:58:03 +0000 | [diff] [blame] | 952 | return snprintf(sysfsbuf, PAGE_SIZE, "%s\n", entry->key); |
Nicolas Boichat | 6f2fad7 | 2007-05-08 00:24:52 -0700 | [diff] [blame] | 953 | } |
| 954 | |
| 955 | static ssize_t applesmc_key_at_index_show(struct device *dev, |
| 956 | struct device_attribute *attr, char *sysfsbuf) |
| 957 | { |
| 958 | return snprintf(sysfsbuf, PAGE_SIZE, "%d\n", key_at_index); |
| 959 | } |
| 960 | |
| 961 | static ssize_t applesmc_key_at_index_store(struct device *dev, |
| 962 | struct device_attribute *attr, const char *sysfsbuf, size_t count) |
| 963 | { |
Henrik Rydberg | 5874583 | 2010-11-10 10:58:03 +0000 | [diff] [blame] | 964 | unsigned long newkey; |
Nicolas Boichat | 6f2fad7 | 2007-05-08 00:24:52 -0700 | [diff] [blame] | 965 | |
Frans Meulenbroeks | 179c4fd | 2012-01-04 20:58:52 +0100 | [diff] [blame] | 966 | if (kstrtoul(sysfsbuf, 10, &newkey) < 0 |
Henrik Rydberg | 5874583 | 2010-11-10 10:58:03 +0000 | [diff] [blame] | 967 | || newkey >= smcreg.key_count) |
| 968 | return -EINVAL; |
Nicolas Boichat | 6f2fad7 | 2007-05-08 00:24:52 -0700 | [diff] [blame] | 969 | |
Henrik Rydberg | 5874583 | 2010-11-10 10:58:03 +0000 | [diff] [blame] | 970 | key_at_index = newkey; |
Nicolas Boichat | 6f2fad7 | 2007-05-08 00:24:52 -0700 | [diff] [blame] | 971 | return count; |
| 972 | } |
| 973 | |
| 974 | static struct led_classdev applesmc_backlight = { |
Richard Purdie | 6c152be | 2007-10-31 15:00:07 +0100 | [diff] [blame] | 975 | .name = "smc::kbd_backlight", |
Nicolas Boichat | 6f2fad7 | 2007-05-08 00:24:52 -0700 | [diff] [blame] | 976 | .default_trigger = "nand-disk", |
| 977 | .brightness_set = applesmc_brightness_set, |
| 978 | }; |
| 979 | |
Henrik Rydberg | 0b0b5df | 2010-11-09 15:15:09 +0000 | [diff] [blame] | 980 | static struct applesmc_node_group info_group[] = { |
| 981 | { "name", applesmc_name_show }, |
| 982 | { "key_count", applesmc_key_count_show }, |
| 983 | { "key_at_index", applesmc_key_at_index_show, applesmc_key_at_index_store }, |
| 984 | { "key_at_index_name", applesmc_key_at_index_name_show }, |
| 985 | { "key_at_index_type", applesmc_key_at_index_type_show }, |
| 986 | { "key_at_index_data_length", applesmc_key_at_index_data_length_show }, |
| 987 | { "key_at_index_data", applesmc_key_at_index_read_show }, |
| 988 | { } |
Nicolas Boichat | 6f2fad7 | 2007-05-08 00:24:52 -0700 | [diff] [blame] | 989 | }; |
| 990 | |
Henrik Rydberg | 0b0b5df | 2010-11-09 15:15:09 +0000 | [diff] [blame] | 991 | static struct applesmc_node_group accelerometer_group[] = { |
| 992 | { "position", applesmc_position_show }, |
| 993 | { "calibrate", applesmc_calibrate_show, applesmc_calibrate_store }, |
| 994 | { } |
Nicolas Boichat | 6f2fad7 | 2007-05-08 00:24:52 -0700 | [diff] [blame] | 995 | }; |
| 996 | |
Henrik Rydberg | 0b0b5df | 2010-11-09 15:15:09 +0000 | [diff] [blame] | 997 | static struct applesmc_node_group light_sensor_group[] = { |
| 998 | { "light", applesmc_light_show }, |
| 999 | { } |
| 1000 | }; |
Nicolas Boichat | 6f2fad7 | 2007-05-08 00:24:52 -0700 | [diff] [blame] | 1001 | |
Henrik Rydberg | 3eba2bf | 2010-11-09 15:15:08 +0000 | [diff] [blame] | 1002 | static struct applesmc_node_group fan_group[] = { |
| 1003 | { "fan%d_label", applesmc_show_fan_position }, |
| 1004 | { "fan%d_input", applesmc_show_fan_speed, NULL, 0 }, |
| 1005 | { "fan%d_min", applesmc_show_fan_speed, applesmc_store_fan_speed, 1 }, |
| 1006 | { "fan%d_max", applesmc_show_fan_speed, NULL, 2 }, |
| 1007 | { "fan%d_safe", applesmc_show_fan_speed, NULL, 3 }, |
| 1008 | { "fan%d_output", applesmc_show_fan_speed, applesmc_store_fan_speed, 4 }, |
| 1009 | { "fan%d_manual", applesmc_show_fan_manual, applesmc_store_fan_manual }, |
| 1010 | { } |
Nicolas Boichat | 6f2fad7 | 2007-05-08 00:24:52 -0700 | [diff] [blame] | 1011 | }; |
| 1012 | |
Henrik Rydberg | 9792dad | 2010-11-10 10:58:04 +0000 | [diff] [blame] | 1013 | static struct applesmc_node_group temp_group[] = { |
| 1014 | { "temp%d_label", applesmc_show_sensor_label }, |
| 1015 | { "temp%d_input", applesmc_show_temperature }, |
| 1016 | { } |
Alex Murray | fa5575c | 2010-05-27 19:58:54 +0200 | [diff] [blame] | 1017 | }; |
| 1018 | |
Nicolas Boichat | 6f2fad7 | 2007-05-08 00:24:52 -0700 | [diff] [blame] | 1019 | /* Module stuff */ |
| 1020 | |
| 1021 | /* |
Henrik Rydberg | 9792dad | 2010-11-10 10:58:04 +0000 | [diff] [blame] | 1022 | * applesmc_destroy_nodes - remove files and free associated memory |
| 1023 | */ |
| 1024 | static void applesmc_destroy_nodes(struct applesmc_node_group *groups) |
| 1025 | { |
| 1026 | struct applesmc_node_group *grp; |
| 1027 | struct applesmc_dev_attr *node; |
| 1028 | |
| 1029 | for (grp = groups; grp->nodes; grp++) { |
| 1030 | for (node = grp->nodes; node->sda.dev_attr.attr.name; node++) |
| 1031 | sysfs_remove_file(&pdev->dev.kobj, |
| 1032 | &node->sda.dev_attr.attr); |
| 1033 | kfree(grp->nodes); |
| 1034 | grp->nodes = NULL; |
| 1035 | } |
| 1036 | } |
| 1037 | |
| 1038 | /* |
| 1039 | * applesmc_create_nodes - create a two-dimensional group of sysfs files |
| 1040 | */ |
| 1041 | static int applesmc_create_nodes(struct applesmc_node_group *groups, int num) |
| 1042 | { |
| 1043 | struct applesmc_node_group *grp; |
| 1044 | struct applesmc_dev_attr *node; |
| 1045 | struct attribute *attr; |
| 1046 | int ret, i; |
| 1047 | |
| 1048 | for (grp = groups; grp->format; grp++) { |
| 1049 | grp->nodes = kcalloc(num + 1, sizeof(*node), GFP_KERNEL); |
| 1050 | if (!grp->nodes) { |
| 1051 | ret = -ENOMEM; |
| 1052 | goto out; |
| 1053 | } |
| 1054 | for (i = 0; i < num; i++) { |
| 1055 | node = &grp->nodes[i]; |
| 1056 | sprintf(node->name, grp->format, i + 1); |
Henrik Rydberg | 3eba2bf | 2010-11-09 15:15:08 +0000 | [diff] [blame] | 1057 | node->sda.index = (grp->option << 16) | (i & 0xffff); |
Henrik Rydberg | 9792dad | 2010-11-10 10:58:04 +0000 | [diff] [blame] | 1058 | node->sda.dev_attr.show = grp->show; |
| 1059 | node->sda.dev_attr.store = grp->store; |
| 1060 | attr = &node->sda.dev_attr.attr; |
Henrik Rydberg | 9d1f8a40 | 2011-01-22 20:55:39 +0100 | [diff] [blame] | 1061 | sysfs_attr_init(attr); |
Henrik Rydberg | 9792dad | 2010-11-10 10:58:04 +0000 | [diff] [blame] | 1062 | attr->name = node->name; |
| 1063 | attr->mode = S_IRUGO | (grp->store ? S_IWUSR : 0); |
| 1064 | ret = sysfs_create_file(&pdev->dev.kobj, attr); |
| 1065 | if (ret) { |
| 1066 | attr->name = NULL; |
| 1067 | goto out; |
| 1068 | } |
| 1069 | } |
| 1070 | } |
| 1071 | |
| 1072 | return 0; |
| 1073 | out: |
| 1074 | applesmc_destroy_nodes(groups); |
| 1075 | return ret; |
| 1076 | } |
| 1077 | |
Nicolas Boichat | 6f2fad7 | 2007-05-08 00:24:52 -0700 | [diff] [blame] | 1078 | /* Create accelerometer ressources */ |
| 1079 | static int applesmc_create_accelerometer(void) |
| 1080 | { |
Dmitry Torokhov | d5cf2b9 | 2007-09-26 00:01:35 -0400 | [diff] [blame] | 1081 | struct input_dev *idev; |
Nicolas Boichat | 6f2fad7 | 2007-05-08 00:24:52 -0700 | [diff] [blame] | 1082 | int ret; |
| 1083 | |
Henrik Rydberg | 0b0b5df | 2010-11-09 15:15:09 +0000 | [diff] [blame] | 1084 | if (!smcreg.has_accelerometer) |
| 1085 | return 0; |
| 1086 | |
| 1087 | ret = applesmc_create_nodes(accelerometer_group, 1); |
Nicolas Boichat | 6f2fad7 | 2007-05-08 00:24:52 -0700 | [diff] [blame] | 1088 | if (ret) |
| 1089 | goto out; |
| 1090 | |
Dmitry Torokhov | d5cf2b9 | 2007-09-26 00:01:35 -0400 | [diff] [blame] | 1091 | applesmc_idev = input_allocate_polled_device(); |
Nicolas Boichat | 6f2fad7 | 2007-05-08 00:24:52 -0700 | [diff] [blame] | 1092 | if (!applesmc_idev) { |
| 1093 | ret = -ENOMEM; |
| 1094 | goto out_sysfs; |
| 1095 | } |
| 1096 | |
Dmitry Torokhov | d5cf2b9 | 2007-09-26 00:01:35 -0400 | [diff] [blame] | 1097 | applesmc_idev->poll = applesmc_idev_poll; |
| 1098 | applesmc_idev->poll_interval = APPLESMC_POLL_INTERVAL; |
| 1099 | |
Nicolas Boichat | 6f2fad7 | 2007-05-08 00:24:52 -0700 | [diff] [blame] | 1100 | /* initial calibrate for the input device */ |
| 1101 | applesmc_calibrate(); |
| 1102 | |
Dmitry Torokhov | d5cf2b9 | 2007-09-26 00:01:35 -0400 | [diff] [blame] | 1103 | /* initialize the input device */ |
| 1104 | idev = applesmc_idev->input; |
| 1105 | idev->name = "applesmc"; |
| 1106 | idev->id.bustype = BUS_HOST; |
| 1107 | idev->dev.parent = &pdev->dev; |
Jiri Slaby | 7b19ada | 2007-10-18 23:40:32 -0700 | [diff] [blame] | 1108 | idev->evbit[0] = BIT_MASK(EV_ABS); |
Dmitry Torokhov | d5cf2b9 | 2007-09-26 00:01:35 -0400 | [diff] [blame] | 1109 | input_set_abs_params(idev, ABS_X, |
Nicolas Boichat | 6f2fad7 | 2007-05-08 00:24:52 -0700 | [diff] [blame] | 1110 | -256, 256, APPLESMC_INPUT_FUZZ, APPLESMC_INPUT_FLAT); |
Dmitry Torokhov | d5cf2b9 | 2007-09-26 00:01:35 -0400 | [diff] [blame] | 1111 | input_set_abs_params(idev, ABS_Y, |
Nicolas Boichat | 6f2fad7 | 2007-05-08 00:24:52 -0700 | [diff] [blame] | 1112 | -256, 256, APPLESMC_INPUT_FUZZ, APPLESMC_INPUT_FLAT); |
| 1113 | |
Dmitry Torokhov | d5cf2b9 | 2007-09-26 00:01:35 -0400 | [diff] [blame] | 1114 | ret = input_register_polled_device(applesmc_idev); |
Nicolas Boichat | 6f2fad7 | 2007-05-08 00:24:52 -0700 | [diff] [blame] | 1115 | if (ret) |
| 1116 | goto out_idev; |
| 1117 | |
Nicolas Boichat | 6f2fad7 | 2007-05-08 00:24:52 -0700 | [diff] [blame] | 1118 | return 0; |
| 1119 | |
| 1120 | out_idev: |
Dmitry Torokhov | d5cf2b9 | 2007-09-26 00:01:35 -0400 | [diff] [blame] | 1121 | input_free_polled_device(applesmc_idev); |
Nicolas Boichat | 6f2fad7 | 2007-05-08 00:24:52 -0700 | [diff] [blame] | 1122 | |
| 1123 | out_sysfs: |
Henrik Rydberg | 0b0b5df | 2010-11-09 15:15:09 +0000 | [diff] [blame] | 1124 | applesmc_destroy_nodes(accelerometer_group); |
Nicolas Boichat | 6f2fad7 | 2007-05-08 00:24:52 -0700 | [diff] [blame] | 1125 | |
| 1126 | out: |
Joe Perches | 1ee7c71 | 2010-11-09 15:15:03 +0000 | [diff] [blame] | 1127 | pr_warn("driver init failed (ret=%d)!\n", ret); |
Nicolas Boichat | 6f2fad7 | 2007-05-08 00:24:52 -0700 | [diff] [blame] | 1128 | return ret; |
| 1129 | } |
| 1130 | |
| 1131 | /* Release all ressources used by the accelerometer */ |
| 1132 | static void applesmc_release_accelerometer(void) |
| 1133 | { |
Henrik Rydberg | 0b0b5df | 2010-11-09 15:15:09 +0000 | [diff] [blame] | 1134 | if (!smcreg.has_accelerometer) |
| 1135 | return; |
Dmitry Torokhov | d5cf2b9 | 2007-09-26 00:01:35 -0400 | [diff] [blame] | 1136 | input_unregister_polled_device(applesmc_idev); |
| 1137 | input_free_polled_device(applesmc_idev); |
Henrik Rydberg | 0b0b5df | 2010-11-09 15:15:09 +0000 | [diff] [blame] | 1138 | applesmc_destroy_nodes(accelerometer_group); |
Nicolas Boichat | 6f2fad7 | 2007-05-08 00:24:52 -0700 | [diff] [blame] | 1139 | } |
Henrik Rydberg | 0b0b5df | 2010-11-09 15:15:09 +0000 | [diff] [blame] | 1140 | |
| 1141 | static int applesmc_create_light_sensor(void) |
| 1142 | { |
| 1143 | if (!smcreg.num_light_sensors) |
| 1144 | return 0; |
| 1145 | return applesmc_create_nodes(light_sensor_group, 1); |
| 1146 | } |
| 1147 | |
| 1148 | static void applesmc_release_light_sensor(void) |
| 1149 | { |
| 1150 | if (!smcreg.num_light_sensors) |
| 1151 | return; |
| 1152 | applesmc_destroy_nodes(light_sensor_group); |
| 1153 | } |
| 1154 | |
| 1155 | static int applesmc_create_key_backlight(void) |
| 1156 | { |
| 1157 | if (!smcreg.has_key_backlight) |
| 1158 | return 0; |
| 1159 | applesmc_led_wq = create_singlethread_workqueue("applesmc-led"); |
| 1160 | if (!applesmc_led_wq) |
| 1161 | return -ENOMEM; |
| 1162 | return led_classdev_register(&pdev->dev, &applesmc_backlight); |
| 1163 | } |
| 1164 | |
| 1165 | static void applesmc_release_key_backlight(void) |
| 1166 | { |
| 1167 | if (!smcreg.has_key_backlight) |
| 1168 | return; |
| 1169 | led_classdev_unregister(&applesmc_backlight); |
| 1170 | destroy_workqueue(applesmc_led_wq); |
| 1171 | } |
| 1172 | |
Henrik Rydberg | 40ef06f | 2010-11-10 10:58:05 +0000 | [diff] [blame] | 1173 | static int applesmc_dmi_match(const struct dmi_system_id *id) |
| 1174 | { |
| 1175 | return 1; |
| 1176 | } |
Nicolas Boichat | 6f2fad7 | 2007-05-08 00:24:52 -0700 | [diff] [blame] | 1177 | |
Guenter Roeck | 85ebfd3 | 2012-01-19 11:02:15 -0800 | [diff] [blame] | 1178 | /* |
| 1179 | * Note that DMI_MATCH(...,"MacBook") will match "MacBookPro1,1". |
| 1180 | * So we need to put "Apple MacBook Pro" before "Apple MacBook". |
| 1181 | */ |
Nicolas Boichat | 6f2fad7 | 2007-05-08 00:24:52 -0700 | [diff] [blame] | 1182 | static __initdata struct dmi_system_id applesmc_whitelist[] = { |
Henrik Rydberg | f5274c9 | 2008-10-18 20:27:40 -0700 | [diff] [blame] | 1183 | { applesmc_dmi_match, "Apple MacBook Air", { |
| 1184 | DMI_MATCH(DMI_BOARD_VENDOR, "Apple"), |
| 1185 | DMI_MATCH(DMI_PRODUCT_NAME, "MacBookAir") }, |
Henrik Rydberg | 40ef06f | 2010-11-10 10:58:05 +0000 | [diff] [blame] | 1186 | }, |
Nicolas Boichat | 6f2fad7 | 2007-05-08 00:24:52 -0700 | [diff] [blame] | 1187 | { applesmc_dmi_match, "Apple MacBook Pro", { |
Guenter Roeck | 2bfe814 | 2010-11-10 10:42:21 -0800 | [diff] [blame] | 1188 | DMI_MATCH(DMI_BOARD_VENDOR, "Apple"), |
| 1189 | DMI_MATCH(DMI_PRODUCT_NAME, "MacBookPro") }, |
Henrik Rydberg | 40ef06f | 2010-11-10 10:58:05 +0000 | [diff] [blame] | 1190 | }, |
Riki Oktarianto | cd19ba1 | 2008-02-04 23:41:58 -0800 | [diff] [blame] | 1191 | { applesmc_dmi_match, "Apple MacBook", { |
Guenter Roeck | 2bfe814 | 2010-11-10 10:42:21 -0800 | [diff] [blame] | 1192 | DMI_MATCH(DMI_BOARD_VENDOR, "Apple"), |
| 1193 | DMI_MATCH(DMI_PRODUCT_NAME, "MacBook") }, |
Henrik Rydberg | 40ef06f | 2010-11-10 10:58:05 +0000 | [diff] [blame] | 1194 | }, |
Nicolas Boichat | 6f2fad7 | 2007-05-08 00:24:52 -0700 | [diff] [blame] | 1195 | { applesmc_dmi_match, "Apple Macmini", { |
Guenter Roeck | 2bfe814 | 2010-11-10 10:42:21 -0800 | [diff] [blame] | 1196 | DMI_MATCH(DMI_BOARD_VENDOR, "Apple"), |
| 1197 | DMI_MATCH(DMI_PRODUCT_NAME, "Macmini") }, |
Henrik Rydberg | 40ef06f | 2010-11-10 10:58:05 +0000 | [diff] [blame] | 1198 | }, |
Henrik Rydberg | 45a3a36 | 2008-11-19 15:36:42 -0800 | [diff] [blame] | 1199 | { applesmc_dmi_match, "Apple MacPro", { |
| 1200 | DMI_MATCH(DMI_BOARD_VENDOR, "Apple"), |
| 1201 | DMI_MATCH(DMI_PRODUCT_NAME, "MacPro") }, |
Henrik Rydberg | 40ef06f | 2010-11-10 10:58:05 +0000 | [diff] [blame] | 1202 | }, |
Roberto De Ioris | 9f86f28 | 2008-08-15 00:40:30 -0700 | [diff] [blame] | 1203 | { applesmc_dmi_match, "Apple iMac", { |
Guenter Roeck | 2bfe814 | 2010-11-10 10:42:21 -0800 | [diff] [blame] | 1204 | DMI_MATCH(DMI_BOARD_VENDOR, "Apple"), |
| 1205 | DMI_MATCH(DMI_PRODUCT_NAME, "iMac") }, |
Henrik Rydberg | 40ef06f | 2010-11-10 10:58:05 +0000 | [diff] [blame] | 1206 | }, |
Nicolas Boichat | 6f2fad7 | 2007-05-08 00:24:52 -0700 | [diff] [blame] | 1207 | { .ident = NULL } |
| 1208 | }; |
| 1209 | |
| 1210 | static int __init applesmc_init(void) |
| 1211 | { |
| 1212 | int ret; |
Nicolas Boichat | 6f2fad7 | 2007-05-08 00:24:52 -0700 | [diff] [blame] | 1213 | |
Nicolas Boichat | 6f2fad7 | 2007-05-08 00:24:52 -0700 | [diff] [blame] | 1214 | if (!dmi_check_system(applesmc_whitelist)) { |
Joe Perches | 1ee7c71 | 2010-11-09 15:15:03 +0000 | [diff] [blame] | 1215 | pr_warn("supported laptop not found!\n"); |
Nicolas Boichat | 6f2fad7 | 2007-05-08 00:24:52 -0700 | [diff] [blame] | 1216 | ret = -ENODEV; |
| 1217 | goto out; |
| 1218 | } |
| 1219 | |
| 1220 | if (!request_region(APPLESMC_DATA_PORT, APPLESMC_NR_PORTS, |
| 1221 | "applesmc")) { |
| 1222 | ret = -ENXIO; |
| 1223 | goto out; |
| 1224 | } |
| 1225 | |
| 1226 | ret = platform_driver_register(&applesmc_driver); |
| 1227 | if (ret) |
| 1228 | goto out_region; |
| 1229 | |
Jean Delvare | ddfbf2a | 2007-05-08 20:27:04 -0700 | [diff] [blame] | 1230 | pdev = platform_device_register_simple("applesmc", APPLESMC_DATA_PORT, |
| 1231 | NULL, 0); |
Nicolas Boichat | 6f2fad7 | 2007-05-08 00:24:52 -0700 | [diff] [blame] | 1232 | if (IS_ERR(pdev)) { |
| 1233 | ret = PTR_ERR(pdev); |
| 1234 | goto out_driver; |
| 1235 | } |
| 1236 | |
Henrik Rydberg | 5874583 | 2010-11-10 10:58:03 +0000 | [diff] [blame] | 1237 | /* create register cache */ |
| 1238 | ret = applesmc_init_smcreg(); |
Nicolas Boichat | 6996abf | 2007-05-27 22:17:43 +0200 | [diff] [blame] | 1239 | if (ret) |
| 1240 | goto out_device; |
Nicolas Boichat | fa74419 | 2007-05-23 13:58:13 -0700 | [diff] [blame] | 1241 | |
Henrik Rydberg | 0b0b5df | 2010-11-09 15:15:09 +0000 | [diff] [blame] | 1242 | ret = applesmc_create_nodes(info_group, 1); |
Henrik Rydberg | 5874583 | 2010-11-10 10:58:03 +0000 | [diff] [blame] | 1243 | if (ret) |
| 1244 | goto out_smcreg; |
| 1245 | |
Henrik Rydberg | 3eba2bf | 2010-11-09 15:15:08 +0000 | [diff] [blame] | 1246 | ret = applesmc_create_nodes(fan_group, smcreg.fan_count); |
| 1247 | if (ret) |
| 1248 | goto out_info; |
Nicolas Boichat | 6f2fad7 | 2007-05-08 00:24:52 -0700 | [diff] [blame] | 1249 | |
Henrik Rydberg | 9792dad | 2010-11-10 10:58:04 +0000 | [diff] [blame] | 1250 | ret = applesmc_create_nodes(temp_group, smcreg.temp_count); |
| 1251 | if (ret) |
| 1252 | goto out_fans; |
Nicolas Boichat | 6f2fad7 | 2007-05-08 00:24:52 -0700 | [diff] [blame] | 1253 | |
Henrik Rydberg | 0b0b5df | 2010-11-09 15:15:09 +0000 | [diff] [blame] | 1254 | ret = applesmc_create_accelerometer(); |
| 1255 | if (ret) |
| 1256 | goto out_temperature; |
Nicolas Boichat | 6f2fad7 | 2007-05-08 00:24:52 -0700 | [diff] [blame] | 1257 | |
Henrik Rydberg | 0b0b5df | 2010-11-09 15:15:09 +0000 | [diff] [blame] | 1258 | ret = applesmc_create_light_sensor(); |
| 1259 | if (ret) |
| 1260 | goto out_accelerometer; |
Nicolas Boichat | 6f2fad7 | 2007-05-08 00:24:52 -0700 | [diff] [blame] | 1261 | |
Henrik Rydberg | 0b0b5df | 2010-11-09 15:15:09 +0000 | [diff] [blame] | 1262 | ret = applesmc_create_key_backlight(); |
| 1263 | if (ret) |
| 1264 | goto out_light_sysfs; |
Nicolas Boichat | 6f2fad7 | 2007-05-08 00:24:52 -0700 | [diff] [blame] | 1265 | |
Tony Jones | 1beeffe | 2007-08-20 13:46:20 -0700 | [diff] [blame] | 1266 | hwmon_dev = hwmon_device_register(&pdev->dev); |
| 1267 | if (IS_ERR(hwmon_dev)) { |
| 1268 | ret = PTR_ERR(hwmon_dev); |
Nicolas Boichat | 6f2fad7 | 2007-05-08 00:24:52 -0700 | [diff] [blame] | 1269 | goto out_light_ledclass; |
| 1270 | } |
| 1271 | |
Nicolas Boichat | 6f2fad7 | 2007-05-08 00:24:52 -0700 | [diff] [blame] | 1272 | return 0; |
| 1273 | |
| 1274 | out_light_ledclass: |
Henrik Rydberg | 0b0b5df | 2010-11-09 15:15:09 +0000 | [diff] [blame] | 1275 | applesmc_release_key_backlight(); |
Nicolas Boichat | 6f2fad7 | 2007-05-08 00:24:52 -0700 | [diff] [blame] | 1276 | out_light_sysfs: |
Henrik Rydberg | 0b0b5df | 2010-11-09 15:15:09 +0000 | [diff] [blame] | 1277 | applesmc_release_light_sensor(); |
Nicolas Boichat | 6f2fad7 | 2007-05-08 00:24:52 -0700 | [diff] [blame] | 1278 | out_accelerometer: |
Henrik Rydberg | 0b0b5df | 2010-11-09 15:15:09 +0000 | [diff] [blame] | 1279 | applesmc_release_accelerometer(); |
Nicolas Boichat | 6f2fad7 | 2007-05-08 00:24:52 -0700 | [diff] [blame] | 1280 | out_temperature: |
Henrik Rydberg | 9792dad | 2010-11-10 10:58:04 +0000 | [diff] [blame] | 1281 | applesmc_destroy_nodes(temp_group); |
Henrik Rydberg | 0559a53 | 2010-05-11 09:17:47 +0200 | [diff] [blame] | 1282 | out_fans: |
Henrik Rydberg | 3eba2bf | 2010-11-09 15:15:08 +0000 | [diff] [blame] | 1283 | applesmc_destroy_nodes(fan_group); |
| 1284 | out_info: |
Henrik Rydberg | 0b0b5df | 2010-11-09 15:15:09 +0000 | [diff] [blame] | 1285 | applesmc_destroy_nodes(info_group); |
Henrik Rydberg | 5874583 | 2010-11-10 10:58:03 +0000 | [diff] [blame] | 1286 | out_smcreg: |
| 1287 | applesmc_destroy_smcreg(); |
Nicolas Boichat | 6f2fad7 | 2007-05-08 00:24:52 -0700 | [diff] [blame] | 1288 | out_device: |
| 1289 | platform_device_unregister(pdev); |
| 1290 | out_driver: |
| 1291 | platform_driver_unregister(&applesmc_driver); |
| 1292 | out_region: |
| 1293 | release_region(APPLESMC_DATA_PORT, APPLESMC_NR_PORTS); |
| 1294 | out: |
Joe Perches | 1ee7c71 | 2010-11-09 15:15:03 +0000 | [diff] [blame] | 1295 | pr_warn("driver init failed (ret=%d)!\n", ret); |
Nicolas Boichat | 6f2fad7 | 2007-05-08 00:24:52 -0700 | [diff] [blame] | 1296 | return ret; |
| 1297 | } |
| 1298 | |
| 1299 | static void __exit applesmc_exit(void) |
| 1300 | { |
Tony Jones | 1beeffe | 2007-08-20 13:46:20 -0700 | [diff] [blame] | 1301 | hwmon_device_unregister(hwmon_dev); |
Henrik Rydberg | 0b0b5df | 2010-11-09 15:15:09 +0000 | [diff] [blame] | 1302 | applesmc_release_key_backlight(); |
| 1303 | applesmc_release_light_sensor(); |
| 1304 | applesmc_release_accelerometer(); |
Henrik Rydberg | 9792dad | 2010-11-10 10:58:04 +0000 | [diff] [blame] | 1305 | applesmc_destroy_nodes(temp_group); |
Henrik Rydberg | 3eba2bf | 2010-11-09 15:15:08 +0000 | [diff] [blame] | 1306 | applesmc_destroy_nodes(fan_group); |
Henrik Rydberg | 0b0b5df | 2010-11-09 15:15:09 +0000 | [diff] [blame] | 1307 | applesmc_destroy_nodes(info_group); |
Henrik Rydberg | 5874583 | 2010-11-10 10:58:03 +0000 | [diff] [blame] | 1308 | applesmc_destroy_smcreg(); |
Nicolas Boichat | 6f2fad7 | 2007-05-08 00:24:52 -0700 | [diff] [blame] | 1309 | platform_device_unregister(pdev); |
| 1310 | platform_driver_unregister(&applesmc_driver); |
| 1311 | release_region(APPLESMC_DATA_PORT, APPLESMC_NR_PORTS); |
Nicolas Boichat | 6f2fad7 | 2007-05-08 00:24:52 -0700 | [diff] [blame] | 1312 | } |
| 1313 | |
| 1314 | module_init(applesmc_init); |
| 1315 | module_exit(applesmc_exit); |
| 1316 | |
| 1317 | MODULE_AUTHOR("Nicolas Boichat"); |
| 1318 | MODULE_DESCRIPTION("Apple SMC"); |
| 1319 | MODULE_LICENSE("GPL v2"); |
Henrik Rydberg | dc924ef | 2008-12-01 13:13:49 -0800 | [diff] [blame] | 1320 | MODULE_DEVICE_TABLE(dmi, applesmc_whitelist); |