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