Commit | Line | Data |
---|---|---|
6f2fad74 NB |
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> | |
41e71f97 | 7 | * Copyright (C) 2010 Henrik Rydberg <rydberg@euromail.se> |
6f2fad74 NB |
8 | * |
9 | * Based on hdaps.c driver: | |
10 | * Copyright (C) 2005 Robert Love <rml@novell.com> | |
aa8521ec | 11 | * Copyright (C) 2005 Jesper Juhl <jj@chaosbits.net> |
6f2fad74 NB |
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 | ||
1ee7c71b JP |
30 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
31 | ||
6f2fad74 NB |
32 | #include <linux/delay.h> |
33 | #include <linux/platform_device.h> | |
d5cf2b99 | 34 | #include <linux/input-polldev.h> |
6f2fad74 | 35 | #include <linux/kernel.h> |
5874583d | 36 | #include <linux/slab.h> |
6f2fad74 NB |
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> | |
6055fae8 | 42 | #include <linux/io.h> |
6f2fad74 NB |
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 | ||
5874583d | 56 | /* wait up to 32 ms for a status change. */ |
a332bf9a | 57 | #define APPLESMC_MIN_WAIT 0x0010 |
829917cd | 58 | #define APPLESMC_RETRY_WAIT 0x0100 |
8c9398d1 HR |
59 | #define APPLESMC_MAX_WAIT 0x8000 |
60 | ||
6f2fad74 NB |
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 | ||
8bd1a12a HR |
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) */ | |
d5cf2b99 | 70 | #define BACKLIGHT_KEY "LKSB" /* w-o {lkb (2 bytes) */ |
6f2fad74 | 71 | |
d5cf2b99 | 72 | #define CLAMSHELL_KEY "MSLD" /* r-o ui8 (unused) */ |
6f2fad74 NB |
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 */ | |
3eba2bf7 | 81 | #define FAN_ID_FMT "F%dID" /* r-o char[16] */ |
6f2fad74 | 82 | |
b6e5122f HR |
83 | #define TEMP_SENSOR_TYPE "sp78" |
84 | ||
6f2fad74 | 85 | /* List of keys used to read/write fan speeds */ |
3eba2bf7 HR |
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) */ | |
6f2fad74 NB |
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 | ||
d5cf2b99 | 97 | #define APPLESMC_POLL_INTERVAL 50 /* msecs */ |
6f2fad74 NB |
98 | #define APPLESMC_INPUT_FUZZ 4 /* input event threshold */ |
99 | #define APPLESMC_INPUT_FLAT 4 | |
100 | ||
3eba2bf7 HR |
101 | #define to_index(attr) (to_sensor_dev_attr(attr)->index & 0xffff) |
102 | #define to_option(attr) (to_sensor_dev_attr(attr)->index >> 16) | |
9792dadf | 103 | |
9792dadf HR |
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 */ | |
3eba2bf7 | 115 | int option; /* function argument */ |
9792dadf HR |
116 | struct applesmc_dev_attr *nodes; /* dynamic node array */ |
117 | }; | |
118 | ||
5874583d HR |
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 */ | |
3eba2bf7 | 132 | unsigned int fan_count; /* number of fans */ |
9792dadf HR |
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 */ | |
e30bca12 | 136 | unsigned int index_count; /* size of temperature index array */ |
40ef06f1 HR |
137 | int num_light_sensors; /* number of light sensors */ |
138 | bool has_accelerometer; /* has motion sensor */ | |
139 | bool has_key_backlight; /* has keyboard backlight */ | |
5874583d HR |
140 | bool init_complete; /* true when fully initialized */ |
141 | struct applesmc_entry *cache; /* cached key entries */ | |
e30bca12 | 142 | const char **index; /* temperature key index */ |
5874583d HR |
143 | } smcreg = { |
144 | .mutex = __MUTEX_INITIALIZER(smcreg.mutex), | |
145 | }; | |
146 | ||
6f2fad74 NB |
147 | static const int debug; |
148 | static struct platform_device *pdev; | |
149 | static s16 rest_x; | |
150 | static s16 rest_y; | |
a976f150 HR |
151 | static u8 backlight_state[2]; |
152 | ||
1beeffe4 | 153 | static struct device *hwmon_dev; |
d5cf2b99 | 154 | static struct input_polled_dev *applesmc_idev; |
6f2fad74 | 155 | |
6f2fad74 NB |
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 | /* | |
829917cd | 165 | * wait_read - Wait for a byte to appear on SMC port. Callers must |
6f2fad74 NB |
166 | * hold applesmc_lock. |
167 | */ | |
829917cd | 168 | static int wait_read(void) |
6f2fad74 | 169 | { |
829917cd | 170 | u8 status; |
8c9398d1 | 171 | int us; |
8c9398d1 HR |
172 | for (us = APPLESMC_MIN_WAIT; us < APPLESMC_MAX_WAIT; us <<= 1) { |
173 | udelay(us); | |
829917cd HR |
174 | status = inb(APPLESMC_CMD_PORT); |
175 | /* read: wait for smc to settle */ | |
176 | if (status & 0x01) | |
6f2fad74 | 177 | return 0; |
6f2fad74 NB |
178 | } |
179 | ||
829917cd | 180 | pr_warn("wait_read() fail: 0x%02x\n", status); |
6f2fad74 NB |
181 | return -EIO; |
182 | } | |
183 | ||
84d2d7f2 | 184 | /* |
829917cd HR |
185 | * send_byte - Write to SMC port, retrying when necessary. Callers |
186 | * must hold applesmc_lock. | |
84d2d7f2 | 187 | */ |
829917cd | 188 | static int send_byte(u8 cmd, u16 port) |
84d2d7f2 | 189 | { |
829917cd | 190 | u8 status; |
8c9398d1 | 191 | int us; |
829917cd HR |
192 | |
193 | outb(cmd, port); | |
8c9398d1 | 194 | for (us = APPLESMC_MIN_WAIT; us < APPLESMC_MAX_WAIT; us <<= 1) { |
8c9398d1 | 195 | udelay(us); |
829917cd HR |
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) | |
84d2d7f2 | 202 | return 0; |
829917cd HR |
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); | |
84d2d7f2 | 209 | } |
829917cd HR |
210 | |
211 | pr_warn("send_byte(0x%02x, 0x%04x) fail: 0x%02x\n", cmd, port, status); | |
84d2d7f2 HR |
212 | return -EIO; |
213 | } | |
214 | ||
829917cd HR |
215 | static int send_command(u8 cmd) |
216 | { | |
217 | return send_byte(cmd, APPLESMC_CMD_PORT); | |
218 | } | |
219 | ||
5874583d | 220 | static int send_argument(const char *key) |
6f2fad74 NB |
221 | { |
222 | int i; | |
223 | ||
829917cd HR |
224 | for (i = 0; i < 4; i++) |
225 | if (send_byte(key[i], APPLESMC_DATA_PORT)) | |
6f2fad74 | 226 | return -EIO; |
5874583d HR |
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)) { | |
ac852edb | 235 | pr_warn("%.4s: read arg fail\n", key); |
5874583d HR |
236 | return -EIO; |
237 | } | |
6f2fad74 | 238 | |
829917cd HR |
239 | if (send_byte(len, APPLESMC_DATA_PORT)) { |
240 | pr_warn("%.4s: read len fail\n", key); | |
241 | return -EIO; | |
242 | } | |
6f2fad74 NB |
243 | |
244 | for (i = 0; i < len; i++) { | |
829917cd HR |
245 | if (wait_read()) { |
246 | pr_warn("%.4s: read data[%d] fail\n", key, i); | |
6f2fad74 | 247 | return -EIO; |
5874583d | 248 | } |
6f2fad74 | 249 | buffer[i] = inb(APPLESMC_DATA_PORT); |
6f2fad74 | 250 | } |
6f2fad74 NB |
251 | |
252 | return 0; | |
253 | } | |
254 | ||
5874583d | 255 | static int write_smc(u8 cmd, const char *key, const u8 *buffer, u8 len) |
6f2fad74 NB |
256 | { |
257 | int i; | |
258 | ||
5874583d HR |
259 | if (send_command(cmd) || send_argument(key)) { |
260 | pr_warn("%s: write arg fail\n", key); | |
6f2fad74 | 261 | return -EIO; |
6f2fad74 NB |
262 | } |
263 | ||
829917cd HR |
264 | if (send_byte(len, APPLESMC_DATA_PORT)) { |
265 | pr_warn("%.4s: write len fail\n", key); | |
266 | return -EIO; | |
267 | } | |
6f2fad74 NB |
268 | |
269 | for (i = 0; i < len; i++) { | |
829917cd | 270 | if (send_byte(buffer[i], APPLESMC_DATA_PORT)) { |
5874583d | 271 | pr_warn("%s: write data fail\n", key); |
6f2fad74 | 272 | return -EIO; |
5874583d | 273 | } |
6f2fad74 NB |
274 | } |
275 | ||
276 | return 0; | |
277 | } | |
278 | ||
5874583d HR |
279 | static int read_register_count(unsigned int *count) |
280 | { | |
281 | __be32 be; | |
282 | int ret; | |
283 | ||
284 | ret = read_smc(APPLESMC_READ_CMD, KEY_COUNT_KEY, (u8 *)&be, 4); | |
285 | if (ret) | |
286 | return ret; | |
287 | ||
288 | *count = be32_to_cpu(be); | |
289 | return 0; | |
290 | } | |
291 | ||
6f2fad74 | 292 | /* |
5874583d HR |
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. | |
6f2fad74 | 297 | */ |
5874583d HR |
298 | |
299 | static int applesmc_read_entry(const struct applesmc_entry *entry, | |
300 | u8 *buf, u8 len) | |
6f2fad74 | 301 | { |
5874583d | 302 | int ret; |
6f2fad74 | 303 | |
5874583d HR |
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); | |
6f2fad74 | 309 | |
5874583d HR |
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); | |
0fc86eca HR |
369 | if (IS_ERR(entry)) { |
370 | *lo = 0; | |
5874583d | 371 | return PTR_ERR(entry); |
0fc86eca | 372 | } |
5874583d HR |
373 | if (strcmp(entry->key, key) < 0) |
374 | begin = middle + 1; | |
375 | else | |
376 | end = middle; | |
6f2fad74 NB |
377 | } |
378 | ||
5874583d HR |
379 | *lo = begin; |
380 | return 0; | |
381 | } | |
6f2fad74 | 382 | |
5874583d HR |
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); | |
0fc86eca HR |
391 | if (IS_ERR(entry)) { |
392 | *hi = smcreg.key_count; | |
5874583d | 393 | return PTR_ERR(entry); |
0fc86eca | 394 | } |
5874583d HR |
395 | if (strcmp(key, entry->key) < 0) |
396 | end = middle; | |
397 | else | |
398 | begin = middle + 1; | |
6f2fad74 | 399 | } |
6f2fad74 | 400 | |
5874583d | 401 | *hi = begin; |
6f2fad74 NB |
402 | return 0; |
403 | } | |
404 | ||
5874583d | 405 | static const struct applesmc_entry *applesmc_get_entry_by_key(const char *key) |
6f2fad74 | 406 | { |
5874583d HR |
407 | int begin, end; |
408 | int ret; | |
6f2fad74 | 409 | |
5874583d HR |
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); | |
6f2fad74 | 418 | |
5874583d HR |
419 | return applesmc_get_entry_by_index(begin); |
420 | } | |
6f2fad74 | 421 | |
5874583d HR |
422 | static int applesmc_read_key(const char *key, u8 *buffer, u8 len) |
423 | { | |
424 | const struct applesmc_entry *entry; | |
6f2fad74 | 425 | |
5874583d HR |
426 | entry = applesmc_get_entry_by_key(key); |
427 | if (IS_ERR(entry)) | |
428 | return PTR_ERR(entry); | |
6f2fad74 | 429 | |
5874583d HR |
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); | |
6f2fad74 NB |
442 | } |
443 | ||
40ef06f1 HR |
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 | ||
6f2fad74 | 456 | /* |
edf48f3a | 457 | * applesmc_read_s16 - Read 16-bit signed big endian register |
6f2fad74 | 458 | */ |
edf48f3a | 459 | static int applesmc_read_s16(const char *key, s16 *value) |
6f2fad74 NB |
460 | { |
461 | u8 buffer[2]; | |
462 | int ret; | |
463 | ||
edf48f3a HR |
464 | ret = applesmc_read_key(key, buffer, 2); |
465 | if (ret) | |
466 | return ret; | |
6f2fad74 NB |
467 | |
468 | *value = ((s16)buffer[0] << 8) | buffer[1]; | |
edf48f3a | 469 | return 0; |
6f2fad74 NB |
470 | } |
471 | ||
472 | /* | |
2344cd0c | 473 | * applesmc_device_init - initialize the accelerometer. Can sleep. |
6f2fad74 | 474 | */ |
2344cd0c | 475 | static void applesmc_device_init(void) |
6f2fad74 | 476 | { |
2344cd0c | 477 | int total; |
6f2fad74 NB |
478 | u8 buffer[2]; |
479 | ||
40ef06f1 | 480 | if (!smcreg.has_accelerometer) |
2344cd0c | 481 | return; |
6f2fad74 | 482 | |
6f2fad74 | 483 | for (total = INIT_TIMEOUT_MSECS; total > 0; total -= INIT_WAIT_MSECS) { |
6f2fad74 | 484 | if (!applesmc_read_key(MOTION_SENSOR_KEY, buffer, 2) && |
2344cd0c | 485 | (buffer[0] != 0x00 || buffer[1] != 0x00)) |
5874583d | 486 | return; |
6f2fad74 NB |
487 | buffer[0] = 0xe0; |
488 | buffer[1] = 0x00; | |
489 | applesmc_write_key(MOTION_SENSOR_KEY, buffer, 2); | |
490 | msleep(INIT_WAIT_MSECS); | |
491 | } | |
492 | ||
1ee7c71b | 493 | pr_warn("failed to init the device\n"); |
6f2fad74 NB |
494 | } |
495 | ||
e30bca12 HR |
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 | ||
5874583d HR |
520 | /* |
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; | |
40ef06f1 | 526 | bool left_light_sensor, right_light_sensor; |
3eba2bf7 | 527 | u8 tmp[1]; |
5874583d HR |
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 | ||
3eba2bf7 HR |
542 | ret = applesmc_read_key(FANS_COUNT, tmp, 1); |
543 | if (ret) | |
544 | return ret; | |
545 | s->fan_count = tmp[0]; | |
546 | ||
9792dadf HR |
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 | ||
e30bca12 HR |
555 | ret = applesmc_init_index(s); |
556 | if (ret) | |
557 | return ret; | |
558 | ||
40ef06f1 HR |
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; | |
5874583d HR |
573 | s->init_complete = true; |
574 | ||
e30bca12 HR |
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, | |
40ef06f1 HR |
577 | s->has_accelerometer, |
578 | s->num_light_sensors, | |
579 | s->has_key_backlight); | |
5874583d HR |
580 | |
581 | return 0; | |
582 | } | |
583 | ||
e30bca12 HR |
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 | ||
5874583d HR |
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 | ||
e30bca12 | 613 | applesmc_destroy_smcreg(); |
5874583d HR |
614 | |
615 | return ret; | |
616 | } | |
617 | ||
6f2fad74 NB |
618 | /* Device model stuff */ |
619 | static int applesmc_probe(struct platform_device *dev) | |
620 | { | |
5874583d HR |
621 | int ret; |
622 | ||
623 | ret = applesmc_init_smcreg(); | |
624 | if (ret) | |
625 | return ret; | |
626 | ||
2344cd0c | 627 | applesmc_device_init(); |
6f2fad74 | 628 | |
6f2fad74 NB |
629 | return 0; |
630 | } | |
631 | ||
a976f150 HR |
632 | /* Synchronize device with memorized backlight state */ |
633 | static int applesmc_pm_resume(struct device *dev) | |
634 | { | |
40ef06f1 | 635 | if (smcreg.has_key_backlight) |
a976f150 | 636 | applesmc_write_key(BACKLIGHT_KEY, backlight_state, 2); |
a976f150 HR |
637 | return 0; |
638 | } | |
639 | ||
640 | /* Reinitialize device on resume from hibernation */ | |
641 | static int applesmc_pm_restore(struct device *dev) | |
6f2fad74 | 642 | { |
2344cd0c | 643 | applesmc_device_init(); |
a976f150 | 644 | return applesmc_pm_resume(dev); |
6f2fad74 NB |
645 | } |
646 | ||
47145210 | 647 | static const struct dev_pm_ops applesmc_pm_ops = { |
a976f150 HR |
648 | .resume = applesmc_pm_resume, |
649 | .restore = applesmc_pm_restore, | |
650 | }; | |
651 | ||
6f2fad74 NB |
652 | static struct platform_driver applesmc_driver = { |
653 | .probe = applesmc_probe, | |
6f2fad74 NB |
654 | .driver = { |
655 | .name = "applesmc", | |
656 | .owner = THIS_MODULE, | |
a976f150 | 657 | .pm = &applesmc_pm_ops, |
6f2fad74 NB |
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 | { | |
edf48f3a HR |
667 | applesmc_read_s16(MOTION_SENSOR_X_KEY, &rest_x); |
668 | applesmc_read_s16(MOTION_SENSOR_Y_KEY, &rest_y); | |
6f2fad74 NB |
669 | rest_x = -rest_x; |
670 | } | |
671 | ||
d5cf2b99 | 672 | static void applesmc_idev_poll(struct input_polled_dev *dev) |
6f2fad74 | 673 | { |
d5cf2b99 | 674 | struct input_dev *idev = dev->input; |
6f2fad74 NB |
675 | s16 x, y; |
676 | ||
edf48f3a | 677 | if (applesmc_read_s16(MOTION_SENSOR_X_KEY, &x)) |
5874583d | 678 | return; |
edf48f3a | 679 | if (applesmc_read_s16(MOTION_SENSOR_Y_KEY, &y)) |
5874583d | 680 | return; |
6f2fad74 NB |
681 | |
682 | x = -x; | |
d5cf2b99 DT |
683 | input_report_abs(idev, ABS_X, x - rest_x); |
684 | input_report_abs(idev, ABS_Y, y - rest_y); | |
685 | input_sync(idev); | |
6f2fad74 NB |
686 | } |
687 | ||
688 | /* Sysfs Files */ | |
689 | ||
fa74419b NB |
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 | ||
6f2fad74 NB |
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 | ||
edf48f3a | 702 | ret = applesmc_read_s16(MOTION_SENSOR_X_KEY, &x); |
6f2fad74 NB |
703 | if (ret) |
704 | goto out; | |
edf48f3a | 705 | ret = applesmc_read_s16(MOTION_SENSOR_Y_KEY, &y); |
6f2fad74 NB |
706 | if (ret) |
707 | goto out; | |
edf48f3a | 708 | ret = applesmc_read_s16(MOTION_SENSOR_Z_KEY, &z); |
6f2fad74 NB |
709 | if (ret) |
710 | goto out; | |
711 | ||
712 | out: | |
6f2fad74 NB |
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 | { | |
5874583d | 722 | const struct applesmc_entry *entry; |
8bd1a12a | 723 | static int data_length; |
6f2fad74 NB |
724 | int ret; |
725 | u8 left = 0, right = 0; | |
5874583d | 726 | u8 buffer[10]; |
6f2fad74 | 727 | |
8bd1a12a | 728 | if (!data_length) { |
5874583d HR |
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; | |
1ee7c71b | 735 | pr_info("light sensor data length set to %d\n", data_length); |
8bd1a12a HR |
736 | } |
737 | ||
738 | ret = applesmc_read_key(LIGHT_SENSOR_LEFT_KEY, buffer, data_length); | |
c3d6362b AM |
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 | } | |
6f2fad74 NB |
744 | left = buffer[2]; |
745 | if (ret) | |
746 | goto out; | |
8bd1a12a | 747 | ret = applesmc_read_key(LIGHT_SENSOR_RIGHT_KEY, buffer, data_length); |
6f2fad74 NB |
748 | right = buffer[2]; |
749 | ||
750 | out: | |
6f2fad74 NB |
751 | if (ret) |
752 | return ret; | |
753 | else | |
754 | return snprintf(sysfsbuf, PAGE_SIZE, "(%d,%d)\n", left, right); | |
755 | } | |
756 | ||
fa5575cf AM |
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 | { | |
e30bca12 | 761 | const char *key = smcreg.index[to_index(devattr)]; |
fa5575cf | 762 | |
e30bca12 | 763 | return snprintf(sysfsbuf, PAGE_SIZE, "%s\n", key); |
fa5575cf AM |
764 | } |
765 | ||
6f2fad74 NB |
766 | /* Displays degree Celsius * 1000 */ |
767 | static ssize_t applesmc_show_temperature(struct device *dev, | |
768 | struct device_attribute *devattr, char *sysfsbuf) | |
769 | { | |
e30bca12 | 770 | const char *key = smcreg.index[to_index(devattr)]; |
6f2fad74 | 771 | int ret; |
b6e5122f HR |
772 | s16 value; |
773 | int temp; | |
6f2fad74 | 774 | |
e30bca12 | 775 | ret = applesmc_read_s16(key, &value); |
6f2fad74 NB |
776 | if (ret) |
777 | return ret; | |
9792dadf | 778 | |
b6e5122f | 779 | temp = 250 * (value >> 6); |
9792dadf | 780 | |
b6e5122f | 781 | return snprintf(sysfsbuf, PAGE_SIZE, "%d\n", temp); |
6f2fad74 NB |
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]; | |
6f2fad74 | 791 | |
3eba2bf7 | 792 | sprintf(newkey, fan_speed_fmt[to_option(attr)], to_index(attr)); |
6f2fad74 | 793 | |
6f2fad74 NB |
794 | ret = applesmc_read_key(newkey, buffer, 2); |
795 | speed = ((buffer[0] << 8 | buffer[1]) >> 2); | |
796 | ||
6f2fad74 NB |
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; | |
2bfe8148 | 808 | unsigned long speed; |
6f2fad74 NB |
809 | char newkey[5]; |
810 | u8 buffer[2]; | |
6f2fad74 | 811 | |
179c4fdb | 812 | if (kstrtoul(sysfsbuf, 10, &speed) < 0 || speed >= 0x4000) |
2bfe8148 | 813 | return -EINVAL; /* Bigger than a 14-bit value */ |
6f2fad74 | 814 | |
3eba2bf7 | 815 | sprintf(newkey, fan_speed_fmt[to_option(attr)], to_index(attr)); |
6f2fad74 | 816 | |
6f2fad74 NB |
817 | buffer[0] = (speed >> 6) & 0xff; |
818 | buffer[1] = (speed << 2) & 0xff; | |
819 | ret = applesmc_write_key(newkey, buffer, 2); | |
820 | ||
6f2fad74 NB |
821 | if (ret) |
822 | return ret; | |
823 | else | |
824 | return count; | |
825 | } | |
826 | ||
827 | static ssize_t applesmc_show_fan_manual(struct device *dev, | |
3eba2bf7 | 828 | struct device_attribute *attr, char *sysfsbuf) |
6f2fad74 NB |
829 | { |
830 | int ret; | |
831 | u16 manual = 0; | |
832 | u8 buffer[2]; | |
6f2fad74 | 833 | |
6f2fad74 | 834 | ret = applesmc_read_key(FANS_MANUAL, buffer, 2); |
3eba2bf7 | 835 | manual = ((buffer[0] << 8 | buffer[1]) >> to_index(attr)) & 0x01; |
6f2fad74 | 836 | |
6f2fad74 NB |
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, | |
3eba2bf7 | 844 | struct device_attribute *attr, |
6f2fad74 NB |
845 | const char *sysfsbuf, size_t count) |
846 | { | |
847 | int ret; | |
848 | u8 buffer[2]; | |
2bfe8148 | 849 | unsigned long input; |
6f2fad74 | 850 | u16 val; |
6f2fad74 | 851 | |
179c4fdb | 852 | if (kstrtoul(sysfsbuf, 10, &input) < 0) |
2bfe8148 | 853 | return -EINVAL; |
6f2fad74 | 854 | |
6f2fad74 NB |
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) | |
3eba2bf7 | 861 | val = val | (0x01 << to_index(attr)); |
6f2fad74 | 862 | else |
3eba2bf7 | 863 | val = val & ~(0x01 << to_index(attr)); |
6f2fad74 NB |
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: | |
6f2fad74 NB |
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]; | |
6f2fad74 | 883 | |
3eba2bf7 | 884 | sprintf(newkey, FAN_ID_FMT, to_index(attr)); |
6f2fad74 | 885 | |
6f2fad74 NB |
886 | ret = applesmc_read_key(newkey, buffer, 16); |
887 | buffer[16] = 0; | |
888 | ||
6f2fad74 NB |
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 | { | |
6f2fad74 | 904 | applesmc_calibrate(); |
6f2fad74 NB |
905 | |
906 | return count; | |
907 | } | |
908 | ||
6f2fad74 NB |
909 | static void applesmc_backlight_set(struct work_struct *work) |
910 | { | |
a976f150 | 911 | applesmc_write_key(BACKLIGHT_KEY, backlight_state, 2); |
6f2fad74 NB |
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 | ||
a976f150 | 920 | backlight_state[0] = value; |
6f2fad74 NB |
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 | ||
6f2fad74 NB |
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 | ||
6f2fad74 NB |
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 | { | |
5874583d | 947 | const struct applesmc_entry *entry; |
6f2fad74 NB |
948 | int ret; |
949 | ||
5874583d HR |
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) | |
6f2fad74 | 955 | return ret; |
6f2fad74 | 956 | |
5874583d | 957 | return entry->len; |
6f2fad74 NB |
958 | } |
959 | ||
960 | static ssize_t applesmc_key_at_index_data_length_show(struct device *dev, | |
961 | struct device_attribute *attr, char *sysfsbuf) | |
962 | { | |
5874583d | 963 | const struct applesmc_entry *entry; |
6f2fad74 | 964 | |
5874583d HR |
965 | entry = applesmc_get_entry_by_index(key_at_index); |
966 | if (IS_ERR(entry)) | |
967 | return PTR_ERR(entry); | |
6f2fad74 | 968 | |
5874583d | 969 | return snprintf(sysfsbuf, PAGE_SIZE, "%d\n", entry->len); |
6f2fad74 NB |
970 | } |
971 | ||
972 | static ssize_t applesmc_key_at_index_type_show(struct device *dev, | |
973 | struct device_attribute *attr, char *sysfsbuf) | |
974 | { | |
5874583d | 975 | const struct applesmc_entry *entry; |
6f2fad74 | 976 | |
5874583d HR |
977 | entry = applesmc_get_entry_by_index(key_at_index); |
978 | if (IS_ERR(entry)) | |
979 | return PTR_ERR(entry); | |
6f2fad74 | 980 | |
5874583d | 981 | return snprintf(sysfsbuf, PAGE_SIZE, "%s\n", entry->type); |
6f2fad74 NB |
982 | } |
983 | ||
984 | static ssize_t applesmc_key_at_index_name_show(struct device *dev, | |
985 | struct device_attribute *attr, char *sysfsbuf) | |
986 | { | |
5874583d | 987 | const struct applesmc_entry *entry; |
6f2fad74 | 988 | |
5874583d HR |
989 | entry = applesmc_get_entry_by_index(key_at_index); |
990 | if (IS_ERR(entry)) | |
991 | return PTR_ERR(entry); | |
6f2fad74 | 992 | |
5874583d | 993 | return snprintf(sysfsbuf, PAGE_SIZE, "%s\n", entry->key); |
6f2fad74 NB |
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 | { | |
5874583d | 1005 | unsigned long newkey; |
6f2fad74 | 1006 | |
179c4fdb | 1007 | if (kstrtoul(sysfsbuf, 10, &newkey) < 0 |
5874583d HR |
1008 | || newkey >= smcreg.key_count) |
1009 | return -EINVAL; | |
6f2fad74 | 1010 | |
5874583d | 1011 | key_at_index = newkey; |
6f2fad74 NB |
1012 | return count; |
1013 | } | |
1014 | ||
1015 | static struct led_classdev applesmc_backlight = { | |
6c152bee | 1016 | .name = "smc::kbd_backlight", |
6f2fad74 NB |
1017 | .default_trigger = "nand-disk", |
1018 | .brightness_set = applesmc_brightness_set, | |
1019 | }; | |
1020 | ||
0b0b5dff HR |
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 | { } | |
6f2fad74 NB |
1030 | }; |
1031 | ||
0b0b5dff HR |
1032 | static struct applesmc_node_group accelerometer_group[] = { |
1033 | { "position", applesmc_position_show }, | |
1034 | { "calibrate", applesmc_calibrate_show, applesmc_calibrate_store }, | |
1035 | { } | |
6f2fad74 NB |
1036 | }; |
1037 | ||
0b0b5dff HR |
1038 | static struct applesmc_node_group light_sensor_group[] = { |
1039 | { "light", applesmc_light_show }, | |
1040 | { } | |
1041 | }; | |
6f2fad74 | 1042 | |
3eba2bf7 HR |
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 | { } | |
6f2fad74 NB |
1052 | }; |
1053 | ||
9792dadf HR |
1054 | static struct applesmc_node_group temp_group[] = { |
1055 | { "temp%d_label", applesmc_show_sensor_label }, | |
1056 | { "temp%d_input", applesmc_show_temperature }, | |
1057 | { } | |
fa5575cf AM |
1058 | }; |
1059 | ||
6f2fad74 NB |
1060 | /* Module stuff */ |
1061 | ||
9792dadf HR |
1062 | /* |
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); | |
3eba2bf7 | 1098 | node->sda.index = (grp->option << 16) | (i & 0xffff); |
9792dadf HR |
1099 | node->sda.dev_attr.show = grp->show; |
1100 | node->sda.dev_attr.store = grp->store; | |
1101 | attr = &node->sda.dev_attr.attr; | |
9d1f8a40 | 1102 | sysfs_attr_init(attr); |
9792dadf HR |
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 | ||
6f2fad74 NB |
1119 | /* Create accelerometer ressources */ |
1120 | static int applesmc_create_accelerometer(void) | |
1121 | { | |
d5cf2b99 | 1122 | struct input_dev *idev; |
6f2fad74 NB |
1123 | int ret; |
1124 | ||
0b0b5dff HR |
1125 | if (!smcreg.has_accelerometer) |
1126 | return 0; | |
1127 | ||
1128 | ret = applesmc_create_nodes(accelerometer_group, 1); | |
6f2fad74 NB |
1129 | if (ret) |
1130 | goto out; | |
1131 | ||
d5cf2b99 | 1132 | applesmc_idev = input_allocate_polled_device(); |
6f2fad74 NB |
1133 | if (!applesmc_idev) { |
1134 | ret = -ENOMEM; | |
1135 | goto out_sysfs; | |
1136 | } | |
1137 | ||
d5cf2b99 DT |
1138 | applesmc_idev->poll = applesmc_idev_poll; |
1139 | applesmc_idev->poll_interval = APPLESMC_POLL_INTERVAL; | |
1140 | ||
6f2fad74 NB |
1141 | /* initial calibrate for the input device */ |
1142 | applesmc_calibrate(); | |
1143 | ||
d5cf2b99 DT |
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; | |
7b19ada2 | 1149 | idev->evbit[0] = BIT_MASK(EV_ABS); |
d5cf2b99 | 1150 | input_set_abs_params(idev, ABS_X, |
6f2fad74 | 1151 | -256, 256, APPLESMC_INPUT_FUZZ, APPLESMC_INPUT_FLAT); |
d5cf2b99 | 1152 | input_set_abs_params(idev, ABS_Y, |
6f2fad74 NB |
1153 | -256, 256, APPLESMC_INPUT_FUZZ, APPLESMC_INPUT_FLAT); |
1154 | ||
d5cf2b99 | 1155 | ret = input_register_polled_device(applesmc_idev); |
6f2fad74 NB |
1156 | if (ret) |
1157 | goto out_idev; | |
1158 | ||
6f2fad74 NB |
1159 | return 0; |
1160 | ||
1161 | out_idev: | |
d5cf2b99 | 1162 | input_free_polled_device(applesmc_idev); |
6f2fad74 NB |
1163 | |
1164 | out_sysfs: | |
0b0b5dff | 1165 | applesmc_destroy_nodes(accelerometer_group); |
6f2fad74 NB |
1166 | |
1167 | out: | |
1ee7c71b | 1168 | pr_warn("driver init failed (ret=%d)!\n", ret); |
6f2fad74 NB |
1169 | return ret; |
1170 | } | |
1171 | ||
1172 | /* Release all ressources used by the accelerometer */ | |
1173 | static void applesmc_release_accelerometer(void) | |
1174 | { | |
0b0b5dff HR |
1175 | if (!smcreg.has_accelerometer) |
1176 | return; | |
d5cf2b99 DT |
1177 | input_unregister_polled_device(applesmc_idev); |
1178 | input_free_polled_device(applesmc_idev); | |
0b0b5dff | 1179 | applesmc_destroy_nodes(accelerometer_group); |
6f2fad74 | 1180 | } |
0b0b5dff HR |
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 | ||
40ef06f1 HR |
1214 | static int applesmc_dmi_match(const struct dmi_system_id *id) |
1215 | { | |
1216 | return 1; | |
1217 | } | |
6f2fad74 | 1218 | |
85ebfd3e GR |
1219 | /* |
1220 | * Note that DMI_MATCH(...,"MacBook") will match "MacBookPro1,1". | |
1221 | * So we need to put "Apple MacBook Pro" before "Apple MacBook". | |
1222 | */ | |
6f2fad74 | 1223 | static __initdata struct dmi_system_id applesmc_whitelist[] = { |
f5274c97 HR |
1224 | { applesmc_dmi_match, "Apple MacBook Air", { |
1225 | DMI_MATCH(DMI_BOARD_VENDOR, "Apple"), | |
1226 | DMI_MATCH(DMI_PRODUCT_NAME, "MacBookAir") }, | |
40ef06f1 | 1227 | }, |
6f2fad74 | 1228 | { applesmc_dmi_match, "Apple MacBook Pro", { |
2bfe8148 GR |
1229 | DMI_MATCH(DMI_BOARD_VENDOR, "Apple"), |
1230 | DMI_MATCH(DMI_PRODUCT_NAME, "MacBookPro") }, | |
40ef06f1 | 1231 | }, |
cd19ba13 | 1232 | { applesmc_dmi_match, "Apple MacBook", { |
2bfe8148 GR |
1233 | DMI_MATCH(DMI_BOARD_VENDOR, "Apple"), |
1234 | DMI_MATCH(DMI_PRODUCT_NAME, "MacBook") }, | |
40ef06f1 | 1235 | }, |
6f2fad74 | 1236 | { applesmc_dmi_match, "Apple Macmini", { |
2bfe8148 GR |
1237 | DMI_MATCH(DMI_BOARD_VENDOR, "Apple"), |
1238 | DMI_MATCH(DMI_PRODUCT_NAME, "Macmini") }, | |
40ef06f1 | 1239 | }, |
45a3a36b HR |
1240 | { applesmc_dmi_match, "Apple MacPro", { |
1241 | DMI_MATCH(DMI_BOARD_VENDOR, "Apple"), | |
1242 | DMI_MATCH(DMI_PRODUCT_NAME, "MacPro") }, | |
40ef06f1 | 1243 | }, |
9f86f28d | 1244 | { applesmc_dmi_match, "Apple iMac", { |
2bfe8148 GR |
1245 | DMI_MATCH(DMI_BOARD_VENDOR, "Apple"), |
1246 | DMI_MATCH(DMI_PRODUCT_NAME, "iMac") }, | |
40ef06f1 | 1247 | }, |
6f2fad74 NB |
1248 | { .ident = NULL } |
1249 | }; | |
1250 | ||
1251 | static int __init applesmc_init(void) | |
1252 | { | |
1253 | int ret; | |
6f2fad74 | 1254 | |
6f2fad74 | 1255 | if (!dmi_check_system(applesmc_whitelist)) { |
1ee7c71b | 1256 | pr_warn("supported laptop not found!\n"); |
6f2fad74 NB |
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 | ||
ddfbf2af JD |
1271 | pdev = platform_device_register_simple("applesmc", APPLESMC_DATA_PORT, |
1272 | NULL, 0); | |
6f2fad74 NB |
1273 | if (IS_ERR(pdev)) { |
1274 | ret = PTR_ERR(pdev); | |
1275 | goto out_driver; | |
1276 | } | |
1277 | ||
5874583d HR |
1278 | /* create register cache */ |
1279 | ret = applesmc_init_smcreg(); | |
6996abf0 NB |
1280 | if (ret) |
1281 | goto out_device; | |
fa74419b | 1282 | |
0b0b5dff | 1283 | ret = applesmc_create_nodes(info_group, 1); |
5874583d HR |
1284 | if (ret) |
1285 | goto out_smcreg; | |
1286 | ||
3eba2bf7 HR |
1287 | ret = applesmc_create_nodes(fan_group, smcreg.fan_count); |
1288 | if (ret) | |
1289 | goto out_info; | |
6f2fad74 | 1290 | |
e30bca12 | 1291 | ret = applesmc_create_nodes(temp_group, smcreg.index_count); |
9792dadf HR |
1292 | if (ret) |
1293 | goto out_fans; | |
6f2fad74 | 1294 | |
0b0b5dff HR |
1295 | ret = applesmc_create_accelerometer(); |
1296 | if (ret) | |
1297 | goto out_temperature; | |
6f2fad74 | 1298 | |
0b0b5dff HR |
1299 | ret = applesmc_create_light_sensor(); |
1300 | if (ret) | |
1301 | goto out_accelerometer; | |
6f2fad74 | 1302 | |
0b0b5dff HR |
1303 | ret = applesmc_create_key_backlight(); |
1304 | if (ret) | |
1305 | goto out_light_sysfs; | |
6f2fad74 | 1306 | |
1beeffe4 TJ |
1307 | hwmon_dev = hwmon_device_register(&pdev->dev); |
1308 | if (IS_ERR(hwmon_dev)) { | |
1309 | ret = PTR_ERR(hwmon_dev); | |
6f2fad74 NB |
1310 | goto out_light_ledclass; |
1311 | } | |
1312 | ||
6f2fad74 NB |
1313 | return 0; |
1314 | ||
1315 | out_light_ledclass: | |
0b0b5dff | 1316 | applesmc_release_key_backlight(); |
6f2fad74 | 1317 | out_light_sysfs: |
0b0b5dff | 1318 | applesmc_release_light_sensor(); |
6f2fad74 | 1319 | out_accelerometer: |
0b0b5dff | 1320 | applesmc_release_accelerometer(); |
6f2fad74 | 1321 | out_temperature: |
9792dadf | 1322 | applesmc_destroy_nodes(temp_group); |
0559a538 | 1323 | out_fans: |
3eba2bf7 HR |
1324 | applesmc_destroy_nodes(fan_group); |
1325 | out_info: | |
0b0b5dff | 1326 | applesmc_destroy_nodes(info_group); |
5874583d HR |
1327 | out_smcreg: |
1328 | applesmc_destroy_smcreg(); | |
6f2fad74 NB |
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: | |
1ee7c71b | 1336 | pr_warn("driver init failed (ret=%d)!\n", ret); |
6f2fad74 NB |
1337 | return ret; |
1338 | } | |
1339 | ||
1340 | static void __exit applesmc_exit(void) | |
1341 | { | |
1beeffe4 | 1342 | hwmon_device_unregister(hwmon_dev); |
0b0b5dff HR |
1343 | applesmc_release_key_backlight(); |
1344 | applesmc_release_light_sensor(); | |
1345 | applesmc_release_accelerometer(); | |
9792dadf | 1346 | applesmc_destroy_nodes(temp_group); |
3eba2bf7 | 1347 | applesmc_destroy_nodes(fan_group); |
0b0b5dff | 1348 | applesmc_destroy_nodes(info_group); |
5874583d | 1349 | applesmc_destroy_smcreg(); |
6f2fad74 NB |
1350 | platform_device_unregister(pdev); |
1351 | platform_driver_unregister(&applesmc_driver); | |
1352 | release_region(APPLESMC_DATA_PORT, APPLESMC_NR_PORTS); | |
6f2fad74 NB |
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"); | |
dc924efb | 1361 | MODULE_DEVICE_TABLE(dmi, applesmc_whitelist); |