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