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