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 | |
| 29 | #include <linux/delay.h> |
| 30 | #include <linux/platform_device.h> |
Dmitry Torokhov | d5cf2b9 | 2007-09-26 00:01:35 -0400 | [diff] [blame] | 31 | #include <linux/input-polldev.h> |
Nicolas Boichat | 6f2fad7 | 2007-05-08 00:24:52 -0700 | [diff] [blame] | 32 | #include <linux/kernel.h> |
| 33 | #include <linux/module.h> |
| 34 | #include <linux/timer.h> |
| 35 | #include <linux/dmi.h> |
| 36 | #include <linux/mutex.h> |
| 37 | #include <linux/hwmon-sysfs.h> |
| 38 | #include <asm/io.h> |
| 39 | #include <linux/leds.h> |
| 40 | #include <linux/hwmon.h> |
| 41 | #include <linux/workqueue.h> |
| 42 | |
| 43 | /* data port used by Apple SMC */ |
| 44 | #define APPLESMC_DATA_PORT 0x300 |
| 45 | /* command/status port used by Apple SMC */ |
| 46 | #define APPLESMC_CMD_PORT 0x304 |
| 47 | |
| 48 | #define APPLESMC_NR_PORTS 32 /* 0x300-0x31f */ |
| 49 | |
| 50 | #define APPLESMC_MAX_DATA_LENGTH 32 |
| 51 | |
| 52 | #define APPLESMC_STATUS_MASK 0x0f |
| 53 | #define APPLESMC_READ_CMD 0x10 |
| 54 | #define APPLESMC_WRITE_CMD 0x11 |
| 55 | #define APPLESMC_GET_KEY_BY_INDEX_CMD 0x12 |
| 56 | #define APPLESMC_GET_KEY_TYPE_CMD 0x13 |
| 57 | |
| 58 | #define KEY_COUNT_KEY "#KEY" /* r-o ui32 */ |
| 59 | |
Henrik Rydberg | 8bd1a12 | 2008-10-18 20:27:39 -0700 | [diff] [blame] | 60 | #define LIGHT_SENSOR_LEFT_KEY "ALV0" /* r-o {alv (6-10 bytes) */ |
| 61 | #define LIGHT_SENSOR_RIGHT_KEY "ALV1" /* r-o {alv (6-10 bytes) */ |
Dmitry Torokhov | d5cf2b9 | 2007-09-26 00:01:35 -0400 | [diff] [blame] | 62 | #define BACKLIGHT_KEY "LKSB" /* w-o {lkb (2 bytes) */ |
Nicolas Boichat | 6f2fad7 | 2007-05-08 00:24:52 -0700 | [diff] [blame] | 63 | |
Dmitry Torokhov | d5cf2b9 | 2007-09-26 00:01:35 -0400 | [diff] [blame] | 64 | #define CLAMSHELL_KEY "MSLD" /* r-o ui8 (unused) */ |
Nicolas Boichat | 6f2fad7 | 2007-05-08 00:24:52 -0700 | [diff] [blame] | 65 | |
| 66 | #define MOTION_SENSOR_X_KEY "MO_X" /* r-o sp78 (2 bytes) */ |
| 67 | #define MOTION_SENSOR_Y_KEY "MO_Y" /* r-o sp78 (2 bytes) */ |
| 68 | #define MOTION_SENSOR_Z_KEY "MO_Z" /* r-o sp78 (2 bytes) */ |
| 69 | #define MOTION_SENSOR_KEY "MOCN" /* r/w ui16 */ |
| 70 | |
| 71 | #define FANS_COUNT "FNum" /* r-o ui8 */ |
| 72 | #define FANS_MANUAL "FS! " /* r-w ui16 */ |
| 73 | #define FAN_ACTUAL_SPEED "F0Ac" /* r-o fpe2 (2 bytes) */ |
| 74 | #define FAN_MIN_SPEED "F0Mn" /* r-o fpe2 (2 bytes) */ |
| 75 | #define FAN_MAX_SPEED "F0Mx" /* r-o fpe2 (2 bytes) */ |
| 76 | #define FAN_SAFE_SPEED "F0Sf" /* r-o fpe2 (2 bytes) */ |
| 77 | #define FAN_TARGET_SPEED "F0Tg" /* r-w fpe2 (2 bytes) */ |
| 78 | #define FAN_POSITION "F0ID" /* r-o char[16] */ |
| 79 | |
| 80 | /* |
| 81 | * Temperature sensors keys (sp78 - 2 bytes). |
Nicolas Boichat | 6f2fad7 | 2007-05-08 00:24:52 -0700 | [diff] [blame] | 82 | */ |
René Rebe | 8de5770 | 2007-10-16 14:19:20 -0700 | [diff] [blame] | 83 | static const char* temperature_sensors_sets[][36] = { |
Martin Szulecki | 1bed24b | 2007-07-09 11:41:36 -0700 | [diff] [blame] | 84 | /* Set 0: Macbook Pro */ |
Nicolas Boichat | 6f2fad7 | 2007-05-08 00:24:52 -0700 | [diff] [blame] | 85 | { "TA0P", "TB0T", "TC0D", "TC0P", "TG0H", "TG0P", "TG0T", "Th0H", |
| 86 | "Th1H", "Tm0P", "Ts0P", "Ts1P", NULL }, |
Riki Oktarianto | cd19ba1 | 2008-02-04 23:41:58 -0800 | [diff] [blame] | 87 | /* Set 1: Macbook2 set */ |
| 88 | { "TB0T", "TC0D", "TC0P", "TM0P", "TN0P", "TN1P", "TTF0", "Th0H", |
| 89 | "Th0S", "Th1H", NULL }, |
| 90 | /* Set 2: Macbook set */ |
Martin Szulecki | 1bed24b | 2007-07-09 11:41:36 -0700 | [diff] [blame] | 91 | { "TB0T", "TC0D", "TC0P", "TM0P", "TN0P", "TN1P", "Th0H", "Th0S", |
| 92 | "Th1H", "Ts0P", NULL }, |
Riki Oktarianto | cd19ba1 | 2008-02-04 23:41:58 -0800 | [diff] [blame] | 93 | /* Set 3: Macmini set */ |
René Rebe | 8de5770 | 2007-10-16 14:19:20 -0700 | [diff] [blame] | 94 | { "TC0D", "TC0P", NULL }, |
Riki Oktarianto | cd19ba1 | 2008-02-04 23:41:58 -0800 | [diff] [blame] | 95 | /* Set 4: Mac Pro (2 x Quad-Core) */ |
René Rebe | 8de5770 | 2007-10-16 14:19:20 -0700 | [diff] [blame] | 96 | { "TA0P", "TCAG", "TCAH", "TCBG", "TCBH", "TC0C", "TC0D", "TC0P", |
| 97 | "TC1C", "TC1D", "TC2C", "TC2D", "TC3C", "TC3D", "THTG", "TH0P", |
| 98 | "TH1P", "TH2P", "TH3P", "TMAP", "TMAS", "TMBS", "TM0P", "TM0S", |
| 99 | "TM1P", "TM1S", "TM2P", "TM2S", "TM3S", "TM8P", "TM8S", "TM9P", |
| 100 | "TM9S", "TN0H", "TS0C", NULL }, |
Roberto De Ioris | 9f86f28 | 2008-08-15 00:40:30 -0700 | [diff] [blame] | 101 | /* Set 5: iMac */ |
| 102 | { "TC0D", "TA0P", "TG0P", "TG0D", "TG0H", "TH0P", "Tm0P", "TO0P", |
| 103 | "Tp0C", NULL }, |
Guilherme M. Schroeder | f91a79f | 2008-08-15 00:40:32 -0700 | [diff] [blame] | 104 | /* Set 6: Macbook3 set */ |
| 105 | { "TB0T", "TC0D", "TC0P", "TM0P", "TN0P", "TTF0", "TW0P", "Th0H", |
| 106 | "Th0S", "Th1H", NULL }, |
Henrik Rydberg | f5274c9 | 2008-10-18 20:27:40 -0700 | [diff] [blame] | 107 | /* Set 7: Macbook Air */ |
| 108 | { "TB0T", "TB1S", "TB1T", "TB2S", "TB2T", "TC0D", "TC0P", "TCFP", |
| 109 | "TTF0", "TW0P", "Th0H", "Tp0P", "TpFP", "Ts0P", "Ts0S", NULL }, |
Henrik Rydberg | d754990 | 2008-10-18 20:27:41 -0700 | [diff] [blame^] | 110 | /* Set 8: Macbook Pro 4,1 (Penryn) */ |
| 111 | { "TB0T", "TC0D", "TC0P", "TG0D", "TG0H", "TTF0", "TW0P", "Th0H", |
| 112 | "Th1H", "Th2H", "Tm0P", "Ts0P", NULL }, |
Nicolas Boichat | 6f2fad7 | 2007-05-08 00:24:52 -0700 | [diff] [blame] | 113 | }; |
| 114 | |
| 115 | /* List of keys used to read/write fan speeds */ |
| 116 | static const char* fan_speed_keys[] = { |
| 117 | FAN_ACTUAL_SPEED, |
| 118 | FAN_MIN_SPEED, |
| 119 | FAN_MAX_SPEED, |
| 120 | FAN_SAFE_SPEED, |
| 121 | FAN_TARGET_SPEED |
| 122 | }; |
| 123 | |
| 124 | #define INIT_TIMEOUT_MSECS 5000 /* wait up to 5s for device init ... */ |
| 125 | #define INIT_WAIT_MSECS 50 /* ... in 50ms increments */ |
| 126 | |
Dmitry Torokhov | d5cf2b9 | 2007-09-26 00:01:35 -0400 | [diff] [blame] | 127 | #define APPLESMC_POLL_INTERVAL 50 /* msecs */ |
Nicolas Boichat | 6f2fad7 | 2007-05-08 00:24:52 -0700 | [diff] [blame] | 128 | #define APPLESMC_INPUT_FUZZ 4 /* input event threshold */ |
| 129 | #define APPLESMC_INPUT_FLAT 4 |
| 130 | |
| 131 | #define SENSOR_X 0 |
| 132 | #define SENSOR_Y 1 |
| 133 | #define SENSOR_Z 2 |
| 134 | |
| 135 | /* Structure to be passed to DMI_MATCH function */ |
| 136 | struct dmi_match_data { |
| 137 | /* Indicates whether this computer has an accelerometer. */ |
| 138 | int accelerometer; |
| 139 | /* Indicates whether this computer has light sensors and keyboard backlight. */ |
| 140 | int light; |
| 141 | /* Indicates which temperature sensors set to use. */ |
| 142 | int temperature_set; |
| 143 | }; |
| 144 | |
| 145 | static const int debug; |
| 146 | static struct platform_device *pdev; |
| 147 | static s16 rest_x; |
| 148 | static s16 rest_y; |
Tony Jones | 1beeffe | 2007-08-20 13:46:20 -0700 | [diff] [blame] | 149 | static struct device *hwmon_dev; |
Dmitry Torokhov | d5cf2b9 | 2007-09-26 00:01:35 -0400 | [diff] [blame] | 150 | static struct input_polled_dev *applesmc_idev; |
Nicolas Boichat | 6f2fad7 | 2007-05-08 00:24:52 -0700 | [diff] [blame] | 151 | |
| 152 | /* Indicates whether this computer has an accelerometer. */ |
| 153 | static unsigned int applesmc_accelerometer; |
| 154 | |
| 155 | /* Indicates whether this computer has light sensors and keyboard backlight. */ |
| 156 | static unsigned int applesmc_light; |
| 157 | |
| 158 | /* Indicates which temperature sensors set to use. */ |
| 159 | static unsigned int applesmc_temperature_set; |
| 160 | |
Dmitry Torokhov | d5cf2b9 | 2007-09-26 00:01:35 -0400 | [diff] [blame] | 161 | static DEFINE_MUTEX(applesmc_lock); |
Nicolas Boichat | 6f2fad7 | 2007-05-08 00:24:52 -0700 | [diff] [blame] | 162 | |
| 163 | /* |
| 164 | * Last index written to key_at_index sysfs file, and value to use for all other |
| 165 | * key_at_index_* sysfs files. |
| 166 | */ |
| 167 | static unsigned int key_at_index; |
| 168 | |
| 169 | static struct workqueue_struct *applesmc_led_wq; |
| 170 | |
| 171 | /* |
Henrik Rydberg | 02fcbd1 | 2008-10-18 20:27:39 -0700 | [diff] [blame] | 172 | * __wait_status - Wait up to 10ms for the status port to get a certain value |
Nicolas Boichat | 6f2fad7 | 2007-05-08 00:24:52 -0700 | [diff] [blame] | 173 | * (masked with 0x0f), returning zero if the value is obtained. Callers must |
| 174 | * hold applesmc_lock. |
| 175 | */ |
| 176 | static int __wait_status(u8 val) |
| 177 | { |
| 178 | unsigned int i; |
| 179 | |
| 180 | val = val & APPLESMC_STATUS_MASK; |
| 181 | |
Henrik Rydberg | 02fcbd1 | 2008-10-18 20:27:39 -0700 | [diff] [blame] | 182 | for (i = 0; i < 1000; i++) { |
Nicolas Boichat | 6f2fad7 | 2007-05-08 00:24:52 -0700 | [diff] [blame] | 183 | if ((inb(APPLESMC_CMD_PORT) & APPLESMC_STATUS_MASK) == val) { |
| 184 | if (debug) |
| 185 | printk(KERN_DEBUG |
| 186 | "Waited %d us for status %x\n", |
| 187 | i*10, val); |
| 188 | return 0; |
| 189 | } |
| 190 | udelay(10); |
| 191 | } |
| 192 | |
| 193 | printk(KERN_WARNING "applesmc: wait status failed: %x != %x\n", |
| 194 | val, inb(APPLESMC_CMD_PORT)); |
| 195 | |
| 196 | return -EIO; |
| 197 | } |
| 198 | |
| 199 | /* |
Henrik Rydberg | 84d2d7f | 2008-10-18 20:27:38 -0700 | [diff] [blame] | 200 | * special treatment of command port - on newer macbooks, it seems necessary |
| 201 | * to resend the command byte before polling the status again. Callers must |
| 202 | * hold applesmc_lock. |
| 203 | */ |
| 204 | static int send_command(u8 cmd) |
| 205 | { |
| 206 | int i; |
| 207 | for (i = 0; i < 1000; i++) { |
| 208 | outb(cmd, APPLESMC_CMD_PORT); |
| 209 | udelay(5); |
| 210 | if ((inb(APPLESMC_CMD_PORT) & APPLESMC_STATUS_MASK) == 0x0c) |
| 211 | return 0; |
| 212 | udelay(5); |
| 213 | } |
| 214 | printk(KERN_WARNING "applesmc: command failed: %x -> %x\n", |
| 215 | cmd, inb(APPLESMC_CMD_PORT)); |
| 216 | return -EIO; |
| 217 | } |
| 218 | |
| 219 | /* |
Nicolas Boichat | 6f2fad7 | 2007-05-08 00:24:52 -0700 | [diff] [blame] | 220 | * applesmc_read_key - reads len bytes from a given key, and put them in buffer. |
| 221 | * Returns zero on success or a negative error on failure. Callers must |
| 222 | * hold applesmc_lock. |
| 223 | */ |
| 224 | static int applesmc_read_key(const char* key, u8* buffer, u8 len) |
| 225 | { |
| 226 | int i; |
| 227 | |
| 228 | if (len > APPLESMC_MAX_DATA_LENGTH) { |
| 229 | printk(KERN_ERR "applesmc_read_key: cannot read more than " |
| 230 | "%d bytes\n", APPLESMC_MAX_DATA_LENGTH); |
| 231 | return -EINVAL; |
| 232 | } |
| 233 | |
Henrik Rydberg | 84d2d7f | 2008-10-18 20:27:38 -0700 | [diff] [blame] | 234 | if (send_command(APPLESMC_READ_CMD)) |
Nicolas Boichat | 6f2fad7 | 2007-05-08 00:24:52 -0700 | [diff] [blame] | 235 | return -EIO; |
| 236 | |
| 237 | for (i = 0; i < 4; i++) { |
| 238 | outb(key[i], APPLESMC_DATA_PORT); |
| 239 | if (__wait_status(0x04)) |
| 240 | return -EIO; |
| 241 | } |
| 242 | if (debug) |
| 243 | printk(KERN_DEBUG "<%s", key); |
| 244 | |
| 245 | outb(len, APPLESMC_DATA_PORT); |
| 246 | if (debug) |
| 247 | printk(KERN_DEBUG ">%x", len); |
| 248 | |
| 249 | for (i = 0; i < len; i++) { |
| 250 | if (__wait_status(0x05)) |
| 251 | return -EIO; |
| 252 | buffer[i] = inb(APPLESMC_DATA_PORT); |
| 253 | if (debug) |
| 254 | printk(KERN_DEBUG "<%x", buffer[i]); |
| 255 | } |
| 256 | if (debug) |
| 257 | printk(KERN_DEBUG "\n"); |
| 258 | |
| 259 | return 0; |
| 260 | } |
| 261 | |
| 262 | /* |
| 263 | * applesmc_write_key - writes len bytes from buffer to a given key. |
| 264 | * Returns zero on success or a negative error on failure. Callers must |
| 265 | * hold applesmc_lock. |
| 266 | */ |
| 267 | static int applesmc_write_key(const char* key, u8* buffer, u8 len) |
| 268 | { |
| 269 | int i; |
| 270 | |
| 271 | if (len > APPLESMC_MAX_DATA_LENGTH) { |
| 272 | printk(KERN_ERR "applesmc_write_key: cannot write more than " |
| 273 | "%d bytes\n", APPLESMC_MAX_DATA_LENGTH); |
| 274 | return -EINVAL; |
| 275 | } |
| 276 | |
Henrik Rydberg | 84d2d7f | 2008-10-18 20:27:38 -0700 | [diff] [blame] | 277 | if (send_command(APPLESMC_WRITE_CMD)) |
Nicolas Boichat | 6f2fad7 | 2007-05-08 00:24:52 -0700 | [diff] [blame] | 278 | return -EIO; |
| 279 | |
| 280 | for (i = 0; i < 4; i++) { |
| 281 | outb(key[i], APPLESMC_DATA_PORT); |
| 282 | if (__wait_status(0x04)) |
| 283 | return -EIO; |
| 284 | } |
| 285 | |
| 286 | outb(len, APPLESMC_DATA_PORT); |
| 287 | |
| 288 | for (i = 0; i < len; i++) { |
| 289 | if (__wait_status(0x04)) |
| 290 | return -EIO; |
| 291 | outb(buffer[i], APPLESMC_DATA_PORT); |
| 292 | } |
| 293 | |
| 294 | return 0; |
| 295 | } |
| 296 | |
| 297 | /* |
| 298 | * applesmc_get_key_at_index - get key at index, and put the result in key |
| 299 | * (char[6]). Returns zero on success or a negative error on failure. Callers |
| 300 | * must hold applesmc_lock. |
| 301 | */ |
| 302 | static int applesmc_get_key_at_index(int index, char* key) |
| 303 | { |
| 304 | int i; |
| 305 | u8 readkey[4]; |
| 306 | readkey[0] = index >> 24; |
| 307 | readkey[1] = index >> 16; |
| 308 | readkey[2] = index >> 8; |
| 309 | readkey[3] = index; |
| 310 | |
Henrik Rydberg | 84d2d7f | 2008-10-18 20:27:38 -0700 | [diff] [blame] | 311 | if (send_command(APPLESMC_GET_KEY_BY_INDEX_CMD)) |
Nicolas Boichat | 6f2fad7 | 2007-05-08 00:24:52 -0700 | [diff] [blame] | 312 | return -EIO; |
| 313 | |
| 314 | for (i = 0; i < 4; i++) { |
| 315 | outb(readkey[i], APPLESMC_DATA_PORT); |
| 316 | if (__wait_status(0x04)) |
| 317 | return -EIO; |
| 318 | } |
| 319 | |
| 320 | outb(4, APPLESMC_DATA_PORT); |
| 321 | |
| 322 | for (i = 0; i < 4; i++) { |
| 323 | if (__wait_status(0x05)) |
| 324 | return -EIO; |
| 325 | key[i] = inb(APPLESMC_DATA_PORT); |
| 326 | } |
| 327 | key[4] = 0; |
| 328 | |
| 329 | return 0; |
| 330 | } |
| 331 | |
| 332 | /* |
| 333 | * applesmc_get_key_type - get key type, and put the result in type (char[6]). |
| 334 | * Returns zero on success or a negative error on failure. Callers must |
| 335 | * hold applesmc_lock. |
| 336 | */ |
| 337 | static int applesmc_get_key_type(char* key, char* type) |
| 338 | { |
| 339 | int i; |
| 340 | |
Henrik Rydberg | 84d2d7f | 2008-10-18 20:27:38 -0700 | [diff] [blame] | 341 | if (send_command(APPLESMC_GET_KEY_TYPE_CMD)) |
Nicolas Boichat | 6f2fad7 | 2007-05-08 00:24:52 -0700 | [diff] [blame] | 342 | return -EIO; |
| 343 | |
| 344 | for (i = 0; i < 4; i++) { |
| 345 | outb(key[i], APPLESMC_DATA_PORT); |
| 346 | if (__wait_status(0x04)) |
| 347 | return -EIO; |
| 348 | } |
| 349 | |
Henrik Rydberg | 0522409 | 2008-10-18 20:27:35 -0700 | [diff] [blame] | 350 | outb(6, APPLESMC_DATA_PORT); |
Nicolas Boichat | 6f2fad7 | 2007-05-08 00:24:52 -0700 | [diff] [blame] | 351 | |
| 352 | for (i = 0; i < 6; i++) { |
| 353 | if (__wait_status(0x05)) |
| 354 | return -EIO; |
| 355 | type[i] = inb(APPLESMC_DATA_PORT); |
| 356 | } |
| 357 | type[5] = 0; |
| 358 | |
| 359 | return 0; |
| 360 | } |
| 361 | |
| 362 | /* |
| 363 | * applesmc_read_motion_sensor - Read motion sensor (X, Y or Z). Callers must |
| 364 | * hold applesmc_lock. |
| 365 | */ |
| 366 | static int applesmc_read_motion_sensor(int index, s16* value) |
| 367 | { |
| 368 | u8 buffer[2]; |
| 369 | int ret; |
| 370 | |
| 371 | switch (index) { |
| 372 | case SENSOR_X: |
| 373 | ret = applesmc_read_key(MOTION_SENSOR_X_KEY, buffer, 2); |
| 374 | break; |
| 375 | case SENSOR_Y: |
| 376 | ret = applesmc_read_key(MOTION_SENSOR_Y_KEY, buffer, 2); |
| 377 | break; |
| 378 | case SENSOR_Z: |
| 379 | ret = applesmc_read_key(MOTION_SENSOR_Z_KEY, buffer, 2); |
| 380 | break; |
| 381 | default: |
| 382 | ret = -EINVAL; |
| 383 | } |
| 384 | |
| 385 | *value = ((s16)buffer[0] << 8) | buffer[1]; |
| 386 | |
| 387 | return ret; |
| 388 | } |
| 389 | |
| 390 | /* |
| 391 | * applesmc_device_init - initialize the accelerometer. Returns zero on success |
| 392 | * and negative error code on failure. Can sleep. |
| 393 | */ |
| 394 | static int applesmc_device_init(void) |
| 395 | { |
| 396 | int total, ret = -ENXIO; |
| 397 | u8 buffer[2]; |
| 398 | |
| 399 | if (!applesmc_accelerometer) |
| 400 | return 0; |
| 401 | |
| 402 | mutex_lock(&applesmc_lock); |
| 403 | |
| 404 | for (total = INIT_TIMEOUT_MSECS; total > 0; total -= INIT_WAIT_MSECS) { |
| 405 | if (debug) |
| 406 | printk(KERN_DEBUG "applesmc try %d\n", total); |
| 407 | if (!applesmc_read_key(MOTION_SENSOR_KEY, buffer, 2) && |
| 408 | (buffer[0] != 0x00 || buffer[1] != 0x00)) { |
| 409 | if (total == INIT_TIMEOUT_MSECS) { |
| 410 | printk(KERN_DEBUG "applesmc: device has" |
| 411 | " already been initialized" |
| 412 | " (0x%02x, 0x%02x).\n", |
| 413 | buffer[0], buffer[1]); |
| 414 | } else { |
| 415 | printk(KERN_DEBUG "applesmc: device" |
| 416 | " successfully initialized" |
| 417 | " (0x%02x, 0x%02x).\n", |
| 418 | buffer[0], buffer[1]); |
| 419 | } |
| 420 | ret = 0; |
| 421 | goto out; |
| 422 | } |
| 423 | buffer[0] = 0xe0; |
| 424 | buffer[1] = 0x00; |
| 425 | applesmc_write_key(MOTION_SENSOR_KEY, buffer, 2); |
| 426 | msleep(INIT_WAIT_MSECS); |
| 427 | } |
| 428 | |
| 429 | printk(KERN_WARNING "applesmc: failed to init the device\n"); |
| 430 | |
| 431 | out: |
| 432 | mutex_unlock(&applesmc_lock); |
| 433 | return ret; |
| 434 | } |
| 435 | |
| 436 | /* |
| 437 | * applesmc_get_fan_count - get the number of fans. Callers must NOT hold |
| 438 | * applesmc_lock. |
| 439 | */ |
| 440 | static int applesmc_get_fan_count(void) |
| 441 | { |
| 442 | int ret; |
| 443 | u8 buffer[1]; |
| 444 | |
| 445 | mutex_lock(&applesmc_lock); |
| 446 | |
| 447 | ret = applesmc_read_key(FANS_COUNT, buffer, 1); |
| 448 | |
| 449 | mutex_unlock(&applesmc_lock); |
| 450 | if (ret) |
| 451 | return ret; |
| 452 | else |
| 453 | return buffer[0]; |
| 454 | } |
| 455 | |
| 456 | /* Device model stuff */ |
| 457 | static int applesmc_probe(struct platform_device *dev) |
| 458 | { |
| 459 | int ret; |
| 460 | |
| 461 | ret = applesmc_device_init(); |
| 462 | if (ret) |
| 463 | return ret; |
| 464 | |
| 465 | printk(KERN_INFO "applesmc: device successfully initialized.\n"); |
| 466 | return 0; |
| 467 | } |
| 468 | |
| 469 | static int applesmc_resume(struct platform_device *dev) |
| 470 | { |
| 471 | return applesmc_device_init(); |
| 472 | } |
| 473 | |
| 474 | static struct platform_driver applesmc_driver = { |
| 475 | .probe = applesmc_probe, |
| 476 | .resume = applesmc_resume, |
| 477 | .driver = { |
| 478 | .name = "applesmc", |
| 479 | .owner = THIS_MODULE, |
| 480 | }, |
| 481 | }; |
| 482 | |
| 483 | /* |
| 484 | * applesmc_calibrate - Set our "resting" values. Callers must |
| 485 | * hold applesmc_lock. |
| 486 | */ |
| 487 | static void applesmc_calibrate(void) |
| 488 | { |
| 489 | applesmc_read_motion_sensor(SENSOR_X, &rest_x); |
| 490 | applesmc_read_motion_sensor(SENSOR_Y, &rest_y); |
| 491 | rest_x = -rest_x; |
| 492 | } |
| 493 | |
Dmitry Torokhov | d5cf2b9 | 2007-09-26 00:01:35 -0400 | [diff] [blame] | 494 | static void applesmc_idev_poll(struct input_polled_dev *dev) |
Nicolas Boichat | 6f2fad7 | 2007-05-08 00:24:52 -0700 | [diff] [blame] | 495 | { |
Dmitry Torokhov | d5cf2b9 | 2007-09-26 00:01:35 -0400 | [diff] [blame] | 496 | struct input_dev *idev = dev->input; |
Nicolas Boichat | 6f2fad7 | 2007-05-08 00:24:52 -0700 | [diff] [blame] | 497 | s16 x, y; |
| 498 | |
Dmitry Torokhov | d5cf2b9 | 2007-09-26 00:01:35 -0400 | [diff] [blame] | 499 | mutex_lock(&applesmc_lock); |
Nicolas Boichat | 6f2fad7 | 2007-05-08 00:24:52 -0700 | [diff] [blame] | 500 | |
| 501 | if (applesmc_read_motion_sensor(SENSOR_X, &x)) |
| 502 | goto out; |
| 503 | if (applesmc_read_motion_sensor(SENSOR_Y, &y)) |
| 504 | goto out; |
| 505 | |
| 506 | x = -x; |
Dmitry Torokhov | d5cf2b9 | 2007-09-26 00:01:35 -0400 | [diff] [blame] | 507 | input_report_abs(idev, ABS_X, x - rest_x); |
| 508 | input_report_abs(idev, ABS_Y, y - rest_y); |
| 509 | input_sync(idev); |
Nicolas Boichat | 6f2fad7 | 2007-05-08 00:24:52 -0700 | [diff] [blame] | 510 | |
| 511 | out: |
Nicolas Boichat | 6f2fad7 | 2007-05-08 00:24:52 -0700 | [diff] [blame] | 512 | mutex_unlock(&applesmc_lock); |
| 513 | } |
| 514 | |
| 515 | /* Sysfs Files */ |
| 516 | |
Nicolas Boichat | fa74419 | 2007-05-23 13:58:13 -0700 | [diff] [blame] | 517 | static ssize_t applesmc_name_show(struct device *dev, |
| 518 | struct device_attribute *attr, char *buf) |
| 519 | { |
| 520 | return snprintf(buf, PAGE_SIZE, "applesmc\n"); |
| 521 | } |
| 522 | |
Nicolas Boichat | 6f2fad7 | 2007-05-08 00:24:52 -0700 | [diff] [blame] | 523 | static ssize_t applesmc_position_show(struct device *dev, |
| 524 | struct device_attribute *attr, char *buf) |
| 525 | { |
| 526 | int ret; |
| 527 | s16 x, y, z; |
| 528 | |
| 529 | mutex_lock(&applesmc_lock); |
| 530 | |
| 531 | ret = applesmc_read_motion_sensor(SENSOR_X, &x); |
| 532 | if (ret) |
| 533 | goto out; |
| 534 | ret = applesmc_read_motion_sensor(SENSOR_Y, &y); |
| 535 | if (ret) |
| 536 | goto out; |
| 537 | ret = applesmc_read_motion_sensor(SENSOR_Z, &z); |
| 538 | if (ret) |
| 539 | goto out; |
| 540 | |
| 541 | out: |
| 542 | mutex_unlock(&applesmc_lock); |
| 543 | if (ret) |
| 544 | return ret; |
| 545 | else |
| 546 | return snprintf(buf, PAGE_SIZE, "(%d,%d,%d)\n", x, y, z); |
| 547 | } |
| 548 | |
| 549 | static ssize_t applesmc_light_show(struct device *dev, |
| 550 | struct device_attribute *attr, char *sysfsbuf) |
| 551 | { |
Henrik Rydberg | 8bd1a12 | 2008-10-18 20:27:39 -0700 | [diff] [blame] | 552 | static int data_length; |
Nicolas Boichat | 6f2fad7 | 2007-05-08 00:24:52 -0700 | [diff] [blame] | 553 | int ret; |
| 554 | u8 left = 0, right = 0; |
Henrik Rydberg | 8bd1a12 | 2008-10-18 20:27:39 -0700 | [diff] [blame] | 555 | u8 buffer[10], query[6]; |
Nicolas Boichat | 6f2fad7 | 2007-05-08 00:24:52 -0700 | [diff] [blame] | 556 | |
| 557 | mutex_lock(&applesmc_lock); |
| 558 | |
Henrik Rydberg | 8bd1a12 | 2008-10-18 20:27:39 -0700 | [diff] [blame] | 559 | if (!data_length) { |
| 560 | ret = applesmc_get_key_type(LIGHT_SENSOR_LEFT_KEY, query); |
| 561 | if (ret) |
| 562 | goto out; |
| 563 | data_length = clamp_val(query[0], 0, 10); |
| 564 | printk(KERN_INFO "applesmc: light sensor data length set to " |
| 565 | "%d\n", data_length); |
| 566 | } |
| 567 | |
| 568 | ret = applesmc_read_key(LIGHT_SENSOR_LEFT_KEY, buffer, data_length); |
Nicolas Boichat | 6f2fad7 | 2007-05-08 00:24:52 -0700 | [diff] [blame] | 569 | left = buffer[2]; |
| 570 | if (ret) |
| 571 | goto out; |
Henrik Rydberg | 8bd1a12 | 2008-10-18 20:27:39 -0700 | [diff] [blame] | 572 | ret = applesmc_read_key(LIGHT_SENSOR_RIGHT_KEY, buffer, data_length); |
Nicolas Boichat | 6f2fad7 | 2007-05-08 00:24:52 -0700 | [diff] [blame] | 573 | right = buffer[2]; |
| 574 | |
| 575 | out: |
| 576 | mutex_unlock(&applesmc_lock); |
| 577 | if (ret) |
| 578 | return ret; |
| 579 | else |
| 580 | return snprintf(sysfsbuf, PAGE_SIZE, "(%d,%d)\n", left, right); |
| 581 | } |
| 582 | |
| 583 | /* Displays degree Celsius * 1000 */ |
| 584 | static ssize_t applesmc_show_temperature(struct device *dev, |
| 585 | struct device_attribute *devattr, char *sysfsbuf) |
| 586 | { |
| 587 | int ret; |
| 588 | u8 buffer[2]; |
| 589 | unsigned int temp; |
| 590 | struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); |
| 591 | const char* key = |
| 592 | temperature_sensors_sets[applesmc_temperature_set][attr->index]; |
| 593 | |
| 594 | mutex_lock(&applesmc_lock); |
| 595 | |
| 596 | ret = applesmc_read_key(key, buffer, 2); |
| 597 | temp = buffer[0]*1000; |
| 598 | temp += (buffer[1] >> 6) * 250; |
| 599 | |
| 600 | mutex_unlock(&applesmc_lock); |
| 601 | |
| 602 | if (ret) |
| 603 | return ret; |
| 604 | else |
| 605 | return snprintf(sysfsbuf, PAGE_SIZE, "%u\n", temp); |
| 606 | } |
| 607 | |
| 608 | static ssize_t applesmc_show_fan_speed(struct device *dev, |
| 609 | struct device_attribute *attr, char *sysfsbuf) |
| 610 | { |
| 611 | int ret; |
| 612 | unsigned int speed = 0; |
| 613 | char newkey[5]; |
| 614 | u8 buffer[2]; |
| 615 | struct sensor_device_attribute_2 *sensor_attr = |
| 616 | to_sensor_dev_attr_2(attr); |
| 617 | |
| 618 | newkey[0] = fan_speed_keys[sensor_attr->nr][0]; |
| 619 | newkey[1] = '0' + sensor_attr->index; |
| 620 | newkey[2] = fan_speed_keys[sensor_attr->nr][2]; |
| 621 | newkey[3] = fan_speed_keys[sensor_attr->nr][3]; |
| 622 | newkey[4] = 0; |
| 623 | |
| 624 | mutex_lock(&applesmc_lock); |
| 625 | |
| 626 | ret = applesmc_read_key(newkey, buffer, 2); |
| 627 | speed = ((buffer[0] << 8 | buffer[1]) >> 2); |
| 628 | |
| 629 | mutex_unlock(&applesmc_lock); |
| 630 | if (ret) |
| 631 | return ret; |
| 632 | else |
| 633 | return snprintf(sysfsbuf, PAGE_SIZE, "%u\n", speed); |
| 634 | } |
| 635 | |
| 636 | static ssize_t applesmc_store_fan_speed(struct device *dev, |
| 637 | struct device_attribute *attr, |
| 638 | const char *sysfsbuf, size_t count) |
| 639 | { |
| 640 | int ret; |
| 641 | u32 speed; |
| 642 | char newkey[5]; |
| 643 | u8 buffer[2]; |
| 644 | struct sensor_device_attribute_2 *sensor_attr = |
| 645 | to_sensor_dev_attr_2(attr); |
| 646 | |
| 647 | speed = simple_strtoul(sysfsbuf, NULL, 10); |
| 648 | |
| 649 | if (speed > 0x4000) /* Bigger than a 14-bit value */ |
| 650 | return -EINVAL; |
| 651 | |
| 652 | newkey[0] = fan_speed_keys[sensor_attr->nr][0]; |
| 653 | newkey[1] = '0' + sensor_attr->index; |
| 654 | newkey[2] = fan_speed_keys[sensor_attr->nr][2]; |
| 655 | newkey[3] = fan_speed_keys[sensor_attr->nr][3]; |
| 656 | newkey[4] = 0; |
| 657 | |
| 658 | mutex_lock(&applesmc_lock); |
| 659 | |
| 660 | buffer[0] = (speed >> 6) & 0xff; |
| 661 | buffer[1] = (speed << 2) & 0xff; |
| 662 | ret = applesmc_write_key(newkey, buffer, 2); |
| 663 | |
| 664 | mutex_unlock(&applesmc_lock); |
| 665 | if (ret) |
| 666 | return ret; |
| 667 | else |
| 668 | return count; |
| 669 | } |
| 670 | |
| 671 | static ssize_t applesmc_show_fan_manual(struct device *dev, |
| 672 | struct device_attribute *devattr, char *sysfsbuf) |
| 673 | { |
| 674 | int ret; |
| 675 | u16 manual = 0; |
| 676 | u8 buffer[2]; |
| 677 | struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); |
| 678 | |
| 679 | mutex_lock(&applesmc_lock); |
| 680 | |
| 681 | ret = applesmc_read_key(FANS_MANUAL, buffer, 2); |
| 682 | manual = ((buffer[0] << 8 | buffer[1]) >> attr->index) & 0x01; |
| 683 | |
| 684 | mutex_unlock(&applesmc_lock); |
| 685 | if (ret) |
| 686 | return ret; |
| 687 | else |
| 688 | return snprintf(sysfsbuf, PAGE_SIZE, "%d\n", manual); |
| 689 | } |
| 690 | |
| 691 | static ssize_t applesmc_store_fan_manual(struct device *dev, |
| 692 | struct device_attribute *devattr, |
| 693 | const char *sysfsbuf, size_t count) |
| 694 | { |
| 695 | int ret; |
| 696 | u8 buffer[2]; |
| 697 | u32 input; |
| 698 | u16 val; |
| 699 | struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); |
| 700 | |
| 701 | input = simple_strtoul(sysfsbuf, NULL, 10); |
| 702 | |
| 703 | mutex_lock(&applesmc_lock); |
| 704 | |
| 705 | ret = applesmc_read_key(FANS_MANUAL, buffer, 2); |
| 706 | val = (buffer[0] << 8 | buffer[1]); |
| 707 | if (ret) |
| 708 | goto out; |
| 709 | |
| 710 | if (input) |
| 711 | val = val | (0x01 << attr->index); |
| 712 | else |
| 713 | val = val & ~(0x01 << attr->index); |
| 714 | |
| 715 | buffer[0] = (val >> 8) & 0xFF; |
| 716 | buffer[1] = val & 0xFF; |
| 717 | |
| 718 | ret = applesmc_write_key(FANS_MANUAL, buffer, 2); |
| 719 | |
| 720 | out: |
| 721 | mutex_unlock(&applesmc_lock); |
| 722 | if (ret) |
| 723 | return ret; |
| 724 | else |
| 725 | return count; |
| 726 | } |
| 727 | |
| 728 | static ssize_t applesmc_show_fan_position(struct device *dev, |
| 729 | struct device_attribute *attr, char *sysfsbuf) |
| 730 | { |
| 731 | int ret; |
| 732 | char newkey[5]; |
| 733 | u8 buffer[17]; |
| 734 | struct sensor_device_attribute_2 *sensor_attr = |
| 735 | to_sensor_dev_attr_2(attr); |
| 736 | |
| 737 | newkey[0] = FAN_POSITION[0]; |
| 738 | newkey[1] = '0' + sensor_attr->index; |
| 739 | newkey[2] = FAN_POSITION[2]; |
| 740 | newkey[3] = FAN_POSITION[3]; |
| 741 | newkey[4] = 0; |
| 742 | |
| 743 | mutex_lock(&applesmc_lock); |
| 744 | |
| 745 | ret = applesmc_read_key(newkey, buffer, 16); |
| 746 | buffer[16] = 0; |
| 747 | |
| 748 | mutex_unlock(&applesmc_lock); |
| 749 | if (ret) |
| 750 | return ret; |
| 751 | else |
| 752 | return snprintf(sysfsbuf, PAGE_SIZE, "%s\n", buffer+4); |
| 753 | } |
| 754 | |
| 755 | static ssize_t applesmc_calibrate_show(struct device *dev, |
| 756 | struct device_attribute *attr, char *sysfsbuf) |
| 757 | { |
| 758 | return snprintf(sysfsbuf, PAGE_SIZE, "(%d,%d)\n", rest_x, rest_y); |
| 759 | } |
| 760 | |
| 761 | static ssize_t applesmc_calibrate_store(struct device *dev, |
| 762 | struct device_attribute *attr, const char *sysfsbuf, size_t count) |
| 763 | { |
| 764 | mutex_lock(&applesmc_lock); |
| 765 | applesmc_calibrate(); |
| 766 | mutex_unlock(&applesmc_lock); |
| 767 | |
| 768 | return count; |
| 769 | } |
| 770 | |
| 771 | /* Store the next backlight value to be written by the work */ |
| 772 | static unsigned int backlight_value; |
| 773 | |
| 774 | static void applesmc_backlight_set(struct work_struct *work) |
| 775 | { |
| 776 | u8 buffer[2]; |
| 777 | |
| 778 | mutex_lock(&applesmc_lock); |
| 779 | buffer[0] = backlight_value; |
| 780 | buffer[1] = 0x00; |
| 781 | applesmc_write_key(BACKLIGHT_KEY, buffer, 2); |
| 782 | mutex_unlock(&applesmc_lock); |
| 783 | } |
| 784 | static DECLARE_WORK(backlight_work, &applesmc_backlight_set); |
| 785 | |
| 786 | static void applesmc_brightness_set(struct led_classdev *led_cdev, |
| 787 | enum led_brightness value) |
| 788 | { |
| 789 | int ret; |
| 790 | |
| 791 | backlight_value = value; |
| 792 | ret = queue_work(applesmc_led_wq, &backlight_work); |
| 793 | |
| 794 | if (debug && (!ret)) |
| 795 | printk(KERN_DEBUG "applesmc: work was already on the queue.\n"); |
| 796 | } |
| 797 | |
| 798 | static ssize_t applesmc_key_count_show(struct device *dev, |
| 799 | struct device_attribute *attr, char *sysfsbuf) |
| 800 | { |
| 801 | int ret; |
| 802 | u8 buffer[4]; |
| 803 | u32 count; |
| 804 | |
| 805 | mutex_lock(&applesmc_lock); |
| 806 | |
| 807 | ret = applesmc_read_key(KEY_COUNT_KEY, buffer, 4); |
| 808 | count = ((u32)buffer[0]<<24) + ((u32)buffer[1]<<16) + |
| 809 | ((u32)buffer[2]<<8) + buffer[3]; |
| 810 | |
| 811 | mutex_unlock(&applesmc_lock); |
| 812 | if (ret) |
| 813 | return ret; |
| 814 | else |
| 815 | return snprintf(sysfsbuf, PAGE_SIZE, "%d\n", count); |
| 816 | } |
| 817 | |
| 818 | static ssize_t applesmc_key_at_index_read_show(struct device *dev, |
| 819 | struct device_attribute *attr, char *sysfsbuf) |
| 820 | { |
| 821 | char key[5]; |
| 822 | char info[6]; |
| 823 | int ret; |
| 824 | |
| 825 | mutex_lock(&applesmc_lock); |
| 826 | |
| 827 | ret = applesmc_get_key_at_index(key_at_index, key); |
| 828 | |
| 829 | if (ret || !key[0]) { |
| 830 | mutex_unlock(&applesmc_lock); |
| 831 | |
| 832 | return -EINVAL; |
| 833 | } |
| 834 | |
| 835 | ret = applesmc_get_key_type(key, info); |
| 836 | |
| 837 | if (ret) { |
| 838 | mutex_unlock(&applesmc_lock); |
| 839 | |
| 840 | return ret; |
| 841 | } |
| 842 | |
| 843 | /* |
| 844 | * info[0] maximum value (APPLESMC_MAX_DATA_LENGTH) is much lower than |
| 845 | * PAGE_SIZE, so we don't need any checks before writing to sysfsbuf. |
| 846 | */ |
| 847 | ret = applesmc_read_key(key, sysfsbuf, info[0]); |
| 848 | |
| 849 | mutex_unlock(&applesmc_lock); |
| 850 | |
| 851 | if (!ret) { |
| 852 | return info[0]; |
Dmitry Torokhov | d5cf2b9 | 2007-09-26 00:01:35 -0400 | [diff] [blame] | 853 | } else { |
Nicolas Boichat | 6f2fad7 | 2007-05-08 00:24:52 -0700 | [diff] [blame] | 854 | return ret; |
| 855 | } |
| 856 | } |
| 857 | |
| 858 | static ssize_t applesmc_key_at_index_data_length_show(struct device *dev, |
| 859 | struct device_attribute *attr, char *sysfsbuf) |
| 860 | { |
| 861 | char key[5]; |
| 862 | char info[6]; |
| 863 | int ret; |
| 864 | |
| 865 | mutex_lock(&applesmc_lock); |
| 866 | |
| 867 | ret = applesmc_get_key_at_index(key_at_index, key); |
| 868 | |
| 869 | if (ret || !key[0]) { |
| 870 | mutex_unlock(&applesmc_lock); |
| 871 | |
| 872 | return -EINVAL; |
| 873 | } |
| 874 | |
| 875 | ret = applesmc_get_key_type(key, info); |
| 876 | |
| 877 | mutex_unlock(&applesmc_lock); |
| 878 | |
| 879 | if (!ret) |
| 880 | return snprintf(sysfsbuf, PAGE_SIZE, "%d\n", info[0]); |
| 881 | else |
| 882 | return ret; |
| 883 | } |
| 884 | |
| 885 | static ssize_t applesmc_key_at_index_type_show(struct device *dev, |
| 886 | struct device_attribute *attr, char *sysfsbuf) |
| 887 | { |
| 888 | char key[5]; |
| 889 | char info[6]; |
| 890 | int ret; |
| 891 | |
| 892 | mutex_lock(&applesmc_lock); |
| 893 | |
| 894 | ret = applesmc_get_key_at_index(key_at_index, key); |
| 895 | |
| 896 | if (ret || !key[0]) { |
| 897 | mutex_unlock(&applesmc_lock); |
| 898 | |
| 899 | return -EINVAL; |
| 900 | } |
| 901 | |
| 902 | ret = applesmc_get_key_type(key, info); |
| 903 | |
| 904 | mutex_unlock(&applesmc_lock); |
| 905 | |
| 906 | if (!ret) |
| 907 | return snprintf(sysfsbuf, PAGE_SIZE, "%s\n", info+1); |
| 908 | else |
| 909 | return ret; |
| 910 | } |
| 911 | |
| 912 | static ssize_t applesmc_key_at_index_name_show(struct device *dev, |
| 913 | struct device_attribute *attr, char *sysfsbuf) |
| 914 | { |
| 915 | char key[5]; |
| 916 | int ret; |
| 917 | |
| 918 | mutex_lock(&applesmc_lock); |
| 919 | |
| 920 | ret = applesmc_get_key_at_index(key_at_index, key); |
| 921 | |
| 922 | mutex_unlock(&applesmc_lock); |
| 923 | |
| 924 | if (!ret && key[0]) |
| 925 | return snprintf(sysfsbuf, PAGE_SIZE, "%s\n", key); |
| 926 | else |
| 927 | return -EINVAL; |
| 928 | } |
| 929 | |
| 930 | static ssize_t applesmc_key_at_index_show(struct device *dev, |
| 931 | struct device_attribute *attr, char *sysfsbuf) |
| 932 | { |
| 933 | return snprintf(sysfsbuf, PAGE_SIZE, "%d\n", key_at_index); |
| 934 | } |
| 935 | |
| 936 | static ssize_t applesmc_key_at_index_store(struct device *dev, |
| 937 | struct device_attribute *attr, const char *sysfsbuf, size_t count) |
| 938 | { |
| 939 | mutex_lock(&applesmc_lock); |
| 940 | |
| 941 | key_at_index = simple_strtoul(sysfsbuf, NULL, 10); |
| 942 | |
| 943 | mutex_unlock(&applesmc_lock); |
| 944 | |
| 945 | return count; |
| 946 | } |
| 947 | |
| 948 | static struct led_classdev applesmc_backlight = { |
Richard Purdie | 6c152be | 2007-10-31 15:00:07 +0100 | [diff] [blame] | 949 | .name = "smc::kbd_backlight", |
Nicolas Boichat | 6f2fad7 | 2007-05-08 00:24:52 -0700 | [diff] [blame] | 950 | .default_trigger = "nand-disk", |
| 951 | .brightness_set = applesmc_brightness_set, |
| 952 | }; |
| 953 | |
Nicolas Boichat | fa74419 | 2007-05-23 13:58:13 -0700 | [diff] [blame] | 954 | static DEVICE_ATTR(name, 0444, applesmc_name_show, NULL); |
| 955 | |
Nicolas Boichat | 6f2fad7 | 2007-05-08 00:24:52 -0700 | [diff] [blame] | 956 | static DEVICE_ATTR(position, 0444, applesmc_position_show, NULL); |
| 957 | static DEVICE_ATTR(calibrate, 0644, |
| 958 | applesmc_calibrate_show, applesmc_calibrate_store); |
| 959 | |
| 960 | static struct attribute *accelerometer_attributes[] = { |
| 961 | &dev_attr_position.attr, |
| 962 | &dev_attr_calibrate.attr, |
| 963 | NULL |
| 964 | }; |
| 965 | |
| 966 | static const struct attribute_group accelerometer_attributes_group = |
| 967 | { .attrs = accelerometer_attributes }; |
| 968 | |
| 969 | static DEVICE_ATTR(light, 0444, applesmc_light_show, NULL); |
| 970 | |
| 971 | static DEVICE_ATTR(key_count, 0444, applesmc_key_count_show, NULL); |
| 972 | static DEVICE_ATTR(key_at_index, 0644, |
| 973 | applesmc_key_at_index_show, applesmc_key_at_index_store); |
| 974 | static DEVICE_ATTR(key_at_index_name, 0444, |
| 975 | applesmc_key_at_index_name_show, NULL); |
| 976 | static DEVICE_ATTR(key_at_index_type, 0444, |
| 977 | applesmc_key_at_index_type_show, NULL); |
| 978 | static DEVICE_ATTR(key_at_index_data_length, 0444, |
| 979 | applesmc_key_at_index_data_length_show, NULL); |
| 980 | static DEVICE_ATTR(key_at_index_data, 0444, |
| 981 | applesmc_key_at_index_read_show, NULL); |
| 982 | |
| 983 | static struct attribute *key_enumeration_attributes[] = { |
| 984 | &dev_attr_key_count.attr, |
| 985 | &dev_attr_key_at_index.attr, |
| 986 | &dev_attr_key_at_index_name.attr, |
| 987 | &dev_attr_key_at_index_type.attr, |
| 988 | &dev_attr_key_at_index_data_length.attr, |
| 989 | &dev_attr_key_at_index_data.attr, |
| 990 | NULL |
| 991 | }; |
| 992 | |
| 993 | static const struct attribute_group key_enumeration_group = |
| 994 | { .attrs = key_enumeration_attributes }; |
| 995 | |
| 996 | /* |
| 997 | * Macro defining SENSOR_DEVICE_ATTR for a fan sysfs entries. |
| 998 | * - show actual speed |
| 999 | * - show/store minimum speed |
| 1000 | * - show maximum speed |
| 1001 | * - show safe speed |
| 1002 | * - show/store target speed |
| 1003 | * - show/store manual mode |
| 1004 | */ |
| 1005 | #define sysfs_fan_speeds_offset(offset) \ |
| 1006 | static SENSOR_DEVICE_ATTR_2(fan##offset##_input, S_IRUGO, \ |
| 1007 | applesmc_show_fan_speed, NULL, 0, offset-1); \ |
| 1008 | \ |
| 1009 | static SENSOR_DEVICE_ATTR_2(fan##offset##_min, S_IRUGO | S_IWUSR, \ |
| 1010 | applesmc_show_fan_speed, applesmc_store_fan_speed, 1, offset-1); \ |
| 1011 | \ |
| 1012 | static SENSOR_DEVICE_ATTR_2(fan##offset##_max, S_IRUGO, \ |
| 1013 | applesmc_show_fan_speed, NULL, 2, offset-1); \ |
| 1014 | \ |
| 1015 | static SENSOR_DEVICE_ATTR_2(fan##offset##_safe, S_IRUGO, \ |
| 1016 | applesmc_show_fan_speed, NULL, 3, offset-1); \ |
| 1017 | \ |
| 1018 | static SENSOR_DEVICE_ATTR_2(fan##offset##_output, S_IRUGO | S_IWUSR, \ |
| 1019 | applesmc_show_fan_speed, applesmc_store_fan_speed, 4, offset-1); \ |
| 1020 | \ |
| 1021 | static SENSOR_DEVICE_ATTR(fan##offset##_manual, S_IRUGO | S_IWUSR, \ |
| 1022 | applesmc_show_fan_manual, applesmc_store_fan_manual, offset-1); \ |
| 1023 | \ |
Jean Delvare | da4e8ca | 2007-05-08 20:27:05 -0700 | [diff] [blame] | 1024 | static SENSOR_DEVICE_ATTR(fan##offset##_label, S_IRUGO, \ |
Nicolas Boichat | 6f2fad7 | 2007-05-08 00:24:52 -0700 | [diff] [blame] | 1025 | applesmc_show_fan_position, NULL, offset-1); \ |
| 1026 | \ |
| 1027 | static struct attribute *fan##offset##_attributes[] = { \ |
| 1028 | &sensor_dev_attr_fan##offset##_input.dev_attr.attr, \ |
| 1029 | &sensor_dev_attr_fan##offset##_min.dev_attr.attr, \ |
| 1030 | &sensor_dev_attr_fan##offset##_max.dev_attr.attr, \ |
| 1031 | &sensor_dev_attr_fan##offset##_safe.dev_attr.attr, \ |
| 1032 | &sensor_dev_attr_fan##offset##_output.dev_attr.attr, \ |
| 1033 | &sensor_dev_attr_fan##offset##_manual.dev_attr.attr, \ |
Jean Delvare | da4e8ca | 2007-05-08 20:27:05 -0700 | [diff] [blame] | 1034 | &sensor_dev_attr_fan##offset##_label.dev_attr.attr, \ |
Nicolas Boichat | 6f2fad7 | 2007-05-08 00:24:52 -0700 | [diff] [blame] | 1035 | NULL \ |
| 1036 | }; |
| 1037 | |
| 1038 | /* |
| 1039 | * Create the needed functions for each fan using the macro defined above |
René Rebe | 8de5770 | 2007-10-16 14:19:20 -0700 | [diff] [blame] | 1040 | * (4 fans are supported) |
Nicolas Boichat | 6f2fad7 | 2007-05-08 00:24:52 -0700 | [diff] [blame] | 1041 | */ |
| 1042 | sysfs_fan_speeds_offset(1); |
| 1043 | sysfs_fan_speeds_offset(2); |
René Rebe | 8de5770 | 2007-10-16 14:19:20 -0700 | [diff] [blame] | 1044 | sysfs_fan_speeds_offset(3); |
| 1045 | sysfs_fan_speeds_offset(4); |
Nicolas Boichat | 6f2fad7 | 2007-05-08 00:24:52 -0700 | [diff] [blame] | 1046 | |
| 1047 | static const struct attribute_group fan_attribute_groups[] = { |
| 1048 | { .attrs = fan1_attributes }, |
René Rebe | 8de5770 | 2007-10-16 14:19:20 -0700 | [diff] [blame] | 1049 | { .attrs = fan2_attributes }, |
| 1050 | { .attrs = fan3_attributes }, |
| 1051 | { .attrs = fan4_attributes }, |
Nicolas Boichat | 6f2fad7 | 2007-05-08 00:24:52 -0700 | [diff] [blame] | 1052 | }; |
| 1053 | |
| 1054 | /* |
| 1055 | * Temperature sensors sysfs entries. |
| 1056 | */ |
| 1057 | static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, |
| 1058 | applesmc_show_temperature, NULL, 0); |
| 1059 | static SENSOR_DEVICE_ATTR(temp2_input, S_IRUGO, |
| 1060 | applesmc_show_temperature, NULL, 1); |
| 1061 | static SENSOR_DEVICE_ATTR(temp3_input, S_IRUGO, |
| 1062 | applesmc_show_temperature, NULL, 2); |
| 1063 | static SENSOR_DEVICE_ATTR(temp4_input, S_IRUGO, |
| 1064 | applesmc_show_temperature, NULL, 3); |
| 1065 | static SENSOR_DEVICE_ATTR(temp5_input, S_IRUGO, |
| 1066 | applesmc_show_temperature, NULL, 4); |
| 1067 | static SENSOR_DEVICE_ATTR(temp6_input, S_IRUGO, |
| 1068 | applesmc_show_temperature, NULL, 5); |
| 1069 | static SENSOR_DEVICE_ATTR(temp7_input, S_IRUGO, |
| 1070 | applesmc_show_temperature, NULL, 6); |
| 1071 | static SENSOR_DEVICE_ATTR(temp8_input, S_IRUGO, |
| 1072 | applesmc_show_temperature, NULL, 7); |
| 1073 | static SENSOR_DEVICE_ATTR(temp9_input, S_IRUGO, |
| 1074 | applesmc_show_temperature, NULL, 8); |
| 1075 | static SENSOR_DEVICE_ATTR(temp10_input, S_IRUGO, |
| 1076 | applesmc_show_temperature, NULL, 9); |
| 1077 | static SENSOR_DEVICE_ATTR(temp11_input, S_IRUGO, |
| 1078 | applesmc_show_temperature, NULL, 10); |
| 1079 | static SENSOR_DEVICE_ATTR(temp12_input, S_IRUGO, |
| 1080 | applesmc_show_temperature, NULL, 11); |
René Rebe | 8de5770 | 2007-10-16 14:19:20 -0700 | [diff] [blame] | 1081 | static SENSOR_DEVICE_ATTR(temp13_input, S_IRUGO, |
| 1082 | applesmc_show_temperature, NULL, 12); |
| 1083 | static SENSOR_DEVICE_ATTR(temp14_input, S_IRUGO, |
| 1084 | applesmc_show_temperature, NULL, 13); |
| 1085 | static SENSOR_DEVICE_ATTR(temp15_input, S_IRUGO, |
| 1086 | applesmc_show_temperature, NULL, 14); |
| 1087 | static SENSOR_DEVICE_ATTR(temp16_input, S_IRUGO, |
| 1088 | applesmc_show_temperature, NULL, 15); |
| 1089 | static SENSOR_DEVICE_ATTR(temp17_input, S_IRUGO, |
| 1090 | applesmc_show_temperature, NULL, 16); |
| 1091 | static SENSOR_DEVICE_ATTR(temp18_input, S_IRUGO, |
| 1092 | applesmc_show_temperature, NULL, 17); |
| 1093 | static SENSOR_DEVICE_ATTR(temp19_input, S_IRUGO, |
| 1094 | applesmc_show_temperature, NULL, 18); |
| 1095 | static SENSOR_DEVICE_ATTR(temp20_input, S_IRUGO, |
| 1096 | applesmc_show_temperature, NULL, 19); |
| 1097 | static SENSOR_DEVICE_ATTR(temp21_input, S_IRUGO, |
| 1098 | applesmc_show_temperature, NULL, 20); |
| 1099 | static SENSOR_DEVICE_ATTR(temp22_input, S_IRUGO, |
| 1100 | applesmc_show_temperature, NULL, 21); |
| 1101 | static SENSOR_DEVICE_ATTR(temp23_input, S_IRUGO, |
| 1102 | applesmc_show_temperature, NULL, 22); |
| 1103 | static SENSOR_DEVICE_ATTR(temp24_input, S_IRUGO, |
| 1104 | applesmc_show_temperature, NULL, 23); |
| 1105 | static SENSOR_DEVICE_ATTR(temp25_input, S_IRUGO, |
| 1106 | applesmc_show_temperature, NULL, 24); |
| 1107 | static SENSOR_DEVICE_ATTR(temp26_input, S_IRUGO, |
| 1108 | applesmc_show_temperature, NULL, 25); |
| 1109 | static SENSOR_DEVICE_ATTR(temp27_input, S_IRUGO, |
| 1110 | applesmc_show_temperature, NULL, 26); |
| 1111 | static SENSOR_DEVICE_ATTR(temp28_input, S_IRUGO, |
| 1112 | applesmc_show_temperature, NULL, 27); |
| 1113 | static SENSOR_DEVICE_ATTR(temp29_input, S_IRUGO, |
| 1114 | applesmc_show_temperature, NULL, 28); |
| 1115 | static SENSOR_DEVICE_ATTR(temp30_input, S_IRUGO, |
| 1116 | applesmc_show_temperature, NULL, 29); |
| 1117 | static SENSOR_DEVICE_ATTR(temp31_input, S_IRUGO, |
| 1118 | applesmc_show_temperature, NULL, 30); |
| 1119 | static SENSOR_DEVICE_ATTR(temp32_input, S_IRUGO, |
| 1120 | applesmc_show_temperature, NULL, 31); |
| 1121 | static SENSOR_DEVICE_ATTR(temp33_input, S_IRUGO, |
| 1122 | applesmc_show_temperature, NULL, 32); |
| 1123 | static SENSOR_DEVICE_ATTR(temp34_input, S_IRUGO, |
| 1124 | applesmc_show_temperature, NULL, 33); |
| 1125 | static SENSOR_DEVICE_ATTR(temp35_input, S_IRUGO, |
| 1126 | applesmc_show_temperature, NULL, 34); |
Nicolas Boichat | 6f2fad7 | 2007-05-08 00:24:52 -0700 | [diff] [blame] | 1127 | |
| 1128 | static struct attribute *temperature_attributes[] = { |
| 1129 | &sensor_dev_attr_temp1_input.dev_attr.attr, |
| 1130 | &sensor_dev_attr_temp2_input.dev_attr.attr, |
| 1131 | &sensor_dev_attr_temp3_input.dev_attr.attr, |
| 1132 | &sensor_dev_attr_temp4_input.dev_attr.attr, |
| 1133 | &sensor_dev_attr_temp5_input.dev_attr.attr, |
| 1134 | &sensor_dev_attr_temp6_input.dev_attr.attr, |
| 1135 | &sensor_dev_attr_temp7_input.dev_attr.attr, |
| 1136 | &sensor_dev_attr_temp8_input.dev_attr.attr, |
| 1137 | &sensor_dev_attr_temp9_input.dev_attr.attr, |
| 1138 | &sensor_dev_attr_temp10_input.dev_attr.attr, |
| 1139 | &sensor_dev_attr_temp11_input.dev_attr.attr, |
| 1140 | &sensor_dev_attr_temp12_input.dev_attr.attr, |
René Rebe | 8de5770 | 2007-10-16 14:19:20 -0700 | [diff] [blame] | 1141 | &sensor_dev_attr_temp13_input.dev_attr.attr, |
| 1142 | &sensor_dev_attr_temp14_input.dev_attr.attr, |
| 1143 | &sensor_dev_attr_temp15_input.dev_attr.attr, |
| 1144 | &sensor_dev_attr_temp16_input.dev_attr.attr, |
| 1145 | &sensor_dev_attr_temp17_input.dev_attr.attr, |
| 1146 | &sensor_dev_attr_temp18_input.dev_attr.attr, |
| 1147 | &sensor_dev_attr_temp19_input.dev_attr.attr, |
| 1148 | &sensor_dev_attr_temp20_input.dev_attr.attr, |
| 1149 | &sensor_dev_attr_temp21_input.dev_attr.attr, |
| 1150 | &sensor_dev_attr_temp22_input.dev_attr.attr, |
| 1151 | &sensor_dev_attr_temp23_input.dev_attr.attr, |
| 1152 | &sensor_dev_attr_temp24_input.dev_attr.attr, |
| 1153 | &sensor_dev_attr_temp25_input.dev_attr.attr, |
| 1154 | &sensor_dev_attr_temp26_input.dev_attr.attr, |
| 1155 | &sensor_dev_attr_temp27_input.dev_attr.attr, |
| 1156 | &sensor_dev_attr_temp28_input.dev_attr.attr, |
| 1157 | &sensor_dev_attr_temp29_input.dev_attr.attr, |
| 1158 | &sensor_dev_attr_temp30_input.dev_attr.attr, |
| 1159 | &sensor_dev_attr_temp31_input.dev_attr.attr, |
| 1160 | &sensor_dev_attr_temp32_input.dev_attr.attr, |
| 1161 | &sensor_dev_attr_temp33_input.dev_attr.attr, |
| 1162 | &sensor_dev_attr_temp34_input.dev_attr.attr, |
| 1163 | &sensor_dev_attr_temp35_input.dev_attr.attr, |
Nicolas Boichat | 6f2fad7 | 2007-05-08 00:24:52 -0700 | [diff] [blame] | 1164 | NULL |
| 1165 | }; |
| 1166 | |
| 1167 | static const struct attribute_group temperature_attributes_group = |
| 1168 | { .attrs = temperature_attributes }; |
| 1169 | |
| 1170 | /* Module stuff */ |
| 1171 | |
| 1172 | /* |
| 1173 | * applesmc_dmi_match - found a match. return one, short-circuiting the hunt. |
| 1174 | */ |
Jeff Garzik | 1855256 | 2007-10-03 15:15:40 -0400 | [diff] [blame] | 1175 | static int applesmc_dmi_match(const struct dmi_system_id *id) |
Nicolas Boichat | 6f2fad7 | 2007-05-08 00:24:52 -0700 | [diff] [blame] | 1176 | { |
| 1177 | int i = 0; |
| 1178 | struct dmi_match_data* dmi_data = id->driver_data; |
| 1179 | printk(KERN_INFO "applesmc: %s detected:\n", id->ident); |
| 1180 | applesmc_accelerometer = dmi_data->accelerometer; |
| 1181 | printk(KERN_INFO "applesmc: - Model %s accelerometer\n", |
| 1182 | applesmc_accelerometer ? "with" : "without"); |
| 1183 | applesmc_light = dmi_data->light; |
| 1184 | printk(KERN_INFO "applesmc: - Model %s light sensors and backlight\n", |
| 1185 | applesmc_light ? "with" : "without"); |
| 1186 | |
| 1187 | applesmc_temperature_set = dmi_data->temperature_set; |
| 1188 | while (temperature_sensors_sets[applesmc_temperature_set][i] != NULL) |
| 1189 | i++; |
| 1190 | printk(KERN_INFO "applesmc: - Model with %d temperature sensors\n", i); |
| 1191 | return 1; |
| 1192 | } |
| 1193 | |
| 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: |
| 1241 | printk(KERN_WARNING "applesmc: driver init failed (ret=%d)!\n", ret); |
| 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 | } |
| 1252 | |
| 1253 | static __initdata struct dmi_match_data applesmc_dmi_data[] = { |
| 1254 | /* MacBook Pro: accelerometer, backlight and temperature set 0 */ |
| 1255 | { .accelerometer = 1, .light = 1, .temperature_set = 0 }, |
Riki Oktarianto | cd19ba1 | 2008-02-04 23:41:58 -0800 | [diff] [blame] | 1256 | /* MacBook2: accelerometer and temperature set 1 */ |
Martin Szulecki | 1bed24b | 2007-07-09 11:41:36 -0700 | [diff] [blame] | 1257 | { .accelerometer = 1, .light = 0, .temperature_set = 1 }, |
Riki Oktarianto | cd19ba1 | 2008-02-04 23:41:58 -0800 | [diff] [blame] | 1258 | /* MacBook: accelerometer and temperature set 2 */ |
| 1259 | { .accelerometer = 1, .light = 0, .temperature_set = 2 }, |
| 1260 | /* MacMini: temperature set 3 */ |
René Rebe | 8de5770 | 2007-10-16 14:19:20 -0700 | [diff] [blame] | 1261 | { .accelerometer = 0, .light = 0, .temperature_set = 3 }, |
Riki Oktarianto | cd19ba1 | 2008-02-04 23:41:58 -0800 | [diff] [blame] | 1262 | /* MacPro: temperature set 4 */ |
| 1263 | { .accelerometer = 0, .light = 0, .temperature_set = 4 }, |
Roberto De Ioris | 9f86f28 | 2008-08-15 00:40:30 -0700 | [diff] [blame] | 1264 | /* iMac: temperature set 5 */ |
| 1265 | { .accelerometer = 0, .light = 0, .temperature_set = 5 }, |
Guilherme M. Schroeder | f91a79f | 2008-08-15 00:40:32 -0700 | [diff] [blame] | 1266 | /* MacBook3: accelerometer and temperature set 6 */ |
| 1267 | { .accelerometer = 1, .light = 0, .temperature_set = 6 }, |
Henrik Rydberg | f5274c9 | 2008-10-18 20:27:40 -0700 | [diff] [blame] | 1268 | /* MacBook Air: accelerometer, backlight and temperature set 7 */ |
| 1269 | { .accelerometer = 1, .light = 1, .temperature_set = 7 }, |
Henrik Rydberg | d754990 | 2008-10-18 20:27:41 -0700 | [diff] [blame^] | 1270 | /* MacBook Pro 4: accelerometer, backlight and temperature set 8 */ |
| 1271 | { .accelerometer = 1, .light = 1, .temperature_set = 8 }, |
Nicolas Boichat | 6f2fad7 | 2007-05-08 00:24:52 -0700 | [diff] [blame] | 1272 | }; |
| 1273 | |
| 1274 | /* Note that DMI_MATCH(...,"MacBook") will match "MacBookPro1,1". |
| 1275 | * So we need to put "Apple MacBook Pro" before "Apple MacBook". */ |
| 1276 | static __initdata struct dmi_system_id applesmc_whitelist[] = { |
Henrik Rydberg | f5274c9 | 2008-10-18 20:27:40 -0700 | [diff] [blame] | 1277 | { applesmc_dmi_match, "Apple MacBook Air", { |
| 1278 | DMI_MATCH(DMI_BOARD_VENDOR, "Apple"), |
| 1279 | DMI_MATCH(DMI_PRODUCT_NAME, "MacBookAir") }, |
Andrew Morton | 7b5e3cb | 2008-10-18 20:27:41 -0700 | [diff] [blame] | 1280 | &applesmc_dmi_data[7]}, |
Henrik Rydberg | d754990 | 2008-10-18 20:27:41 -0700 | [diff] [blame^] | 1281 | { applesmc_dmi_match, "Apple MacBook Pro 4", { |
| 1282 | DMI_MATCH(DMI_BOARD_VENDOR, "Apple"), |
| 1283 | DMI_MATCH(DMI_PRODUCT_NAME, "MacBookPro4") }, |
| 1284 | &applesmc_dmi_data[8]}, |
Nicolas Boichat | 6f2fad7 | 2007-05-08 00:24:52 -0700 | [diff] [blame] | 1285 | { applesmc_dmi_match, "Apple MacBook Pro", { |
| 1286 | DMI_MATCH(DMI_BOARD_VENDOR,"Apple"), |
| 1287 | DMI_MATCH(DMI_PRODUCT_NAME,"MacBookPro") }, |
Andrew Morton | 7b5e3cb | 2008-10-18 20:27:41 -0700 | [diff] [blame] | 1288 | &applesmc_dmi_data[0]}, |
Guilherme M. Schroeder | f91a79f | 2008-08-15 00:40:32 -0700 | [diff] [blame] | 1289 | { applesmc_dmi_match, "Apple MacBook (v2)", { |
Nicolas Boichat | 6f2fad7 | 2007-05-08 00:24:52 -0700 | [diff] [blame] | 1290 | DMI_MATCH(DMI_BOARD_VENDOR,"Apple"), |
Riki Oktarianto | cd19ba1 | 2008-02-04 23:41:58 -0800 | [diff] [blame] | 1291 | DMI_MATCH(DMI_PRODUCT_NAME,"MacBook2") }, |
Andrew Morton | 7b5e3cb | 2008-10-18 20:27:41 -0700 | [diff] [blame] | 1292 | &applesmc_dmi_data[1]}, |
Guilherme M. Schroeder | f91a79f | 2008-08-15 00:40:32 -0700 | [diff] [blame] | 1293 | { applesmc_dmi_match, "Apple MacBook (v3)", { |
| 1294 | DMI_MATCH(DMI_BOARD_VENDOR,"Apple"), |
| 1295 | DMI_MATCH(DMI_PRODUCT_NAME,"MacBook3") }, |
Andrew Morton | 7b5e3cb | 2008-10-18 20:27:41 -0700 | [diff] [blame] | 1296 | &applesmc_dmi_data[6]}, |
Riki Oktarianto | cd19ba1 | 2008-02-04 23:41:58 -0800 | [diff] [blame] | 1297 | { applesmc_dmi_match, "Apple MacBook", { |
| 1298 | DMI_MATCH(DMI_BOARD_VENDOR,"Apple"), |
| 1299 | DMI_MATCH(DMI_PRODUCT_NAME,"MacBook") }, |
Andrew Morton | 7b5e3cb | 2008-10-18 20:27:41 -0700 | [diff] [blame] | 1300 | &applesmc_dmi_data[2]}, |
Nicolas Boichat | 6f2fad7 | 2007-05-08 00:24:52 -0700 | [diff] [blame] | 1301 | { applesmc_dmi_match, "Apple Macmini", { |
| 1302 | DMI_MATCH(DMI_BOARD_VENDOR,"Apple"), |
| 1303 | DMI_MATCH(DMI_PRODUCT_NAME,"Macmini") }, |
Andrew Morton | 7b5e3cb | 2008-10-18 20:27:41 -0700 | [diff] [blame] | 1304 | &applesmc_dmi_data[3]}, |
René Rebe | 8de5770 | 2007-10-16 14:19:20 -0700 | [diff] [blame] | 1305 | { applesmc_dmi_match, "Apple MacPro2", { |
| 1306 | DMI_MATCH(DMI_BOARD_VENDOR,"Apple"), |
| 1307 | DMI_MATCH(DMI_PRODUCT_NAME,"MacPro2") }, |
Andrew Morton | 7b5e3cb | 2008-10-18 20:27:41 -0700 | [diff] [blame] | 1308 | &applesmc_dmi_data[4]}, |
Roberto De Ioris | 9f86f28 | 2008-08-15 00:40:30 -0700 | [diff] [blame] | 1309 | { applesmc_dmi_match, "Apple iMac", { |
| 1310 | DMI_MATCH(DMI_BOARD_VENDOR,"Apple"), |
| 1311 | DMI_MATCH(DMI_PRODUCT_NAME,"iMac") }, |
Andrew Morton | 7b5e3cb | 2008-10-18 20:27:41 -0700 | [diff] [blame] | 1312 | &applesmc_dmi_data[5]}, |
Nicolas Boichat | 6f2fad7 | 2007-05-08 00:24:52 -0700 | [diff] [blame] | 1313 | { .ident = NULL } |
| 1314 | }; |
| 1315 | |
| 1316 | static int __init applesmc_init(void) |
| 1317 | { |
| 1318 | int ret; |
| 1319 | int count; |
| 1320 | int i; |
| 1321 | |
Nicolas Boichat | 6f2fad7 | 2007-05-08 00:24:52 -0700 | [diff] [blame] | 1322 | if (!dmi_check_system(applesmc_whitelist)) { |
| 1323 | printk(KERN_WARNING "applesmc: supported laptop not found!\n"); |
| 1324 | ret = -ENODEV; |
| 1325 | goto out; |
| 1326 | } |
| 1327 | |
| 1328 | if (!request_region(APPLESMC_DATA_PORT, APPLESMC_NR_PORTS, |
| 1329 | "applesmc")) { |
| 1330 | ret = -ENXIO; |
| 1331 | goto out; |
| 1332 | } |
| 1333 | |
| 1334 | ret = platform_driver_register(&applesmc_driver); |
| 1335 | if (ret) |
| 1336 | goto out_region; |
| 1337 | |
Jean Delvare | ddfbf2a | 2007-05-08 20:27:04 -0700 | [diff] [blame] | 1338 | pdev = platform_device_register_simple("applesmc", APPLESMC_DATA_PORT, |
| 1339 | NULL, 0); |
Nicolas Boichat | 6f2fad7 | 2007-05-08 00:24:52 -0700 | [diff] [blame] | 1340 | if (IS_ERR(pdev)) { |
| 1341 | ret = PTR_ERR(pdev); |
| 1342 | goto out_driver; |
| 1343 | } |
| 1344 | |
Nicolas Boichat | fa74419 | 2007-05-23 13:58:13 -0700 | [diff] [blame] | 1345 | ret = sysfs_create_file(&pdev->dev.kobj, &dev_attr_name.attr); |
Nicolas Boichat | 6996abf | 2007-05-27 22:17:43 +0200 | [diff] [blame] | 1346 | if (ret) |
| 1347 | goto out_device; |
Nicolas Boichat | fa74419 | 2007-05-23 13:58:13 -0700 | [diff] [blame] | 1348 | |
Nicolas Boichat | 6f2fad7 | 2007-05-08 00:24:52 -0700 | [diff] [blame] | 1349 | /* Create key enumeration sysfs files */ |
| 1350 | ret = sysfs_create_group(&pdev->dev.kobj, &key_enumeration_group); |
| 1351 | if (ret) |
Nicolas Boichat | 6996abf | 2007-05-27 22:17:43 +0200 | [diff] [blame] | 1352 | goto out_name; |
Nicolas Boichat | 6f2fad7 | 2007-05-08 00:24:52 -0700 | [diff] [blame] | 1353 | |
| 1354 | /* create fan files */ |
| 1355 | count = applesmc_get_fan_count(); |
| 1356 | if (count < 0) { |
| 1357 | printk(KERN_ERR "applesmc: Cannot get the number of fans.\n"); |
| 1358 | } else { |
| 1359 | printk(KERN_INFO "applesmc: %d fans found.\n", count); |
| 1360 | |
| 1361 | switch (count) { |
| 1362 | default: |
René Rebe | 8de5770 | 2007-10-16 14:19:20 -0700 | [diff] [blame] | 1363 | printk(KERN_WARNING "applesmc: More than 4 fans found," |
| 1364 | " but at most 4 fans are supported" |
Nicolas Boichat | 6f2fad7 | 2007-05-08 00:24:52 -0700 | [diff] [blame] | 1365 | " by the driver.\n"); |
René Rebe | 8de5770 | 2007-10-16 14:19:20 -0700 | [diff] [blame] | 1366 | case 4: |
| 1367 | ret = sysfs_create_group(&pdev->dev.kobj, |
| 1368 | &fan_attribute_groups[3]); |
| 1369 | if (ret) |
| 1370 | goto out_key_enumeration; |
| 1371 | case 3: |
| 1372 | ret = sysfs_create_group(&pdev->dev.kobj, |
| 1373 | &fan_attribute_groups[2]); |
| 1374 | if (ret) |
| 1375 | goto out_key_enumeration; |
Nicolas Boichat | 6f2fad7 | 2007-05-08 00:24:52 -0700 | [diff] [blame] | 1376 | case 2: |
| 1377 | ret = sysfs_create_group(&pdev->dev.kobj, |
| 1378 | &fan_attribute_groups[1]); |
| 1379 | if (ret) |
| 1380 | goto out_key_enumeration; |
| 1381 | case 1: |
| 1382 | ret = sysfs_create_group(&pdev->dev.kobj, |
| 1383 | &fan_attribute_groups[0]); |
| 1384 | if (ret) |
| 1385 | goto out_fan_1; |
| 1386 | case 0: |
| 1387 | ; |
| 1388 | } |
| 1389 | } |
| 1390 | |
| 1391 | for (i = 0; |
| 1392 | temperature_sensors_sets[applesmc_temperature_set][i] != NULL; |
| 1393 | i++) { |
| 1394 | if (temperature_attributes[i] == NULL) { |
| 1395 | printk(KERN_ERR "applesmc: More temperature sensors " |
| 1396 | "in temperature_sensors_sets (at least %i)" |
| 1397 | "than available sysfs files in " |
| 1398 | "temperature_attributes (%i), please report " |
| 1399 | "this bug.\n", i, i-1); |
| 1400 | goto out_temperature; |
| 1401 | } |
| 1402 | ret = sysfs_create_file(&pdev->dev.kobj, |
| 1403 | temperature_attributes[i]); |
| 1404 | if (ret) |
| 1405 | goto out_temperature; |
| 1406 | } |
| 1407 | |
| 1408 | if (applesmc_accelerometer) { |
| 1409 | ret = applesmc_create_accelerometer(); |
| 1410 | if (ret) |
| 1411 | goto out_temperature; |
| 1412 | } |
| 1413 | |
| 1414 | if (applesmc_light) { |
| 1415 | /* Add light sensor file */ |
| 1416 | ret = sysfs_create_file(&pdev->dev.kobj, &dev_attr_light.attr); |
| 1417 | if (ret) |
| 1418 | goto out_accelerometer; |
| 1419 | |
| 1420 | /* Create the workqueue */ |
| 1421 | applesmc_led_wq = create_singlethread_workqueue("applesmc-led"); |
| 1422 | if (!applesmc_led_wq) { |
| 1423 | ret = -ENOMEM; |
| 1424 | goto out_light_sysfs; |
| 1425 | } |
| 1426 | |
| 1427 | /* register as a led device */ |
| 1428 | ret = led_classdev_register(&pdev->dev, &applesmc_backlight); |
| 1429 | if (ret < 0) |
| 1430 | goto out_light_wq; |
| 1431 | } |
| 1432 | |
Tony Jones | 1beeffe | 2007-08-20 13:46:20 -0700 | [diff] [blame] | 1433 | hwmon_dev = hwmon_device_register(&pdev->dev); |
| 1434 | if (IS_ERR(hwmon_dev)) { |
| 1435 | ret = PTR_ERR(hwmon_dev); |
Nicolas Boichat | 6f2fad7 | 2007-05-08 00:24:52 -0700 | [diff] [blame] | 1436 | goto out_light_ledclass; |
| 1437 | } |
| 1438 | |
| 1439 | printk(KERN_INFO "applesmc: driver successfully loaded.\n"); |
| 1440 | |
| 1441 | return 0; |
| 1442 | |
| 1443 | out_light_ledclass: |
| 1444 | if (applesmc_light) |
| 1445 | led_classdev_unregister(&applesmc_backlight); |
| 1446 | out_light_wq: |
| 1447 | if (applesmc_light) |
| 1448 | destroy_workqueue(applesmc_led_wq); |
| 1449 | out_light_sysfs: |
| 1450 | if (applesmc_light) |
| 1451 | sysfs_remove_file(&pdev->dev.kobj, &dev_attr_light.attr); |
| 1452 | out_accelerometer: |
| 1453 | if (applesmc_accelerometer) |
| 1454 | applesmc_release_accelerometer(); |
| 1455 | out_temperature: |
| 1456 | sysfs_remove_group(&pdev->dev.kobj, &temperature_attributes_group); |
| 1457 | sysfs_remove_group(&pdev->dev.kobj, &fan_attribute_groups[0]); |
| 1458 | out_fan_1: |
| 1459 | sysfs_remove_group(&pdev->dev.kobj, &fan_attribute_groups[1]); |
| 1460 | out_key_enumeration: |
| 1461 | sysfs_remove_group(&pdev->dev.kobj, &key_enumeration_group); |
Nicolas Boichat | 6996abf | 2007-05-27 22:17:43 +0200 | [diff] [blame] | 1462 | out_name: |
| 1463 | sysfs_remove_file(&pdev->dev.kobj, &dev_attr_name.attr); |
Nicolas Boichat | 6f2fad7 | 2007-05-08 00:24:52 -0700 | [diff] [blame] | 1464 | out_device: |
| 1465 | platform_device_unregister(pdev); |
| 1466 | out_driver: |
| 1467 | platform_driver_unregister(&applesmc_driver); |
| 1468 | out_region: |
| 1469 | release_region(APPLESMC_DATA_PORT, APPLESMC_NR_PORTS); |
| 1470 | out: |
| 1471 | printk(KERN_WARNING "applesmc: driver init failed (ret=%d)!\n", ret); |
| 1472 | return ret; |
| 1473 | } |
| 1474 | |
| 1475 | static void __exit applesmc_exit(void) |
| 1476 | { |
Tony Jones | 1beeffe | 2007-08-20 13:46:20 -0700 | [diff] [blame] | 1477 | hwmon_device_unregister(hwmon_dev); |
Nicolas Boichat | 6f2fad7 | 2007-05-08 00:24:52 -0700 | [diff] [blame] | 1478 | if (applesmc_light) { |
| 1479 | led_classdev_unregister(&applesmc_backlight); |
| 1480 | destroy_workqueue(applesmc_led_wq); |
| 1481 | sysfs_remove_file(&pdev->dev.kobj, &dev_attr_light.attr); |
| 1482 | } |
| 1483 | if (applesmc_accelerometer) |
| 1484 | applesmc_release_accelerometer(); |
| 1485 | sysfs_remove_group(&pdev->dev.kobj, &temperature_attributes_group); |
| 1486 | sysfs_remove_group(&pdev->dev.kobj, &fan_attribute_groups[0]); |
| 1487 | sysfs_remove_group(&pdev->dev.kobj, &fan_attribute_groups[1]); |
| 1488 | sysfs_remove_group(&pdev->dev.kobj, &key_enumeration_group); |
Nicolas Boichat | 6996abf | 2007-05-27 22:17:43 +0200 | [diff] [blame] | 1489 | sysfs_remove_file(&pdev->dev.kobj, &dev_attr_name.attr); |
Nicolas Boichat | 6f2fad7 | 2007-05-08 00:24:52 -0700 | [diff] [blame] | 1490 | platform_device_unregister(pdev); |
| 1491 | platform_driver_unregister(&applesmc_driver); |
| 1492 | release_region(APPLESMC_DATA_PORT, APPLESMC_NR_PORTS); |
| 1493 | |
| 1494 | printk(KERN_INFO "applesmc: driver unloaded.\n"); |
| 1495 | } |
| 1496 | |
| 1497 | module_init(applesmc_init); |
| 1498 | module_exit(applesmc_exit); |
| 1499 | |
| 1500 | MODULE_AUTHOR("Nicolas Boichat"); |
| 1501 | MODULE_DESCRIPTION("Apple SMC"); |
| 1502 | MODULE_LICENSE("GPL v2"); |