blob: d4d647522331d0e6157df2b0d771b3b4e0c506f8 [file] [log] [blame]
Nicolas Boichat6f2fad72007-05-08 00:24:52 -07001/*
2 * drivers/hwmon/applesmc.c - driver for Apple's SMC (accelerometer, temperature
3 * sensors, fan control, keyboard backlight control) used in Intel-based Apple
4 * computers.
5 *
6 * Copyright (C) 2007 Nicolas Boichat <nicolas@boichat.ch>
7 *
8 * Based on hdaps.c driver:
9 * Copyright (C) 2005 Robert Love <rml@novell.com>
10 * Copyright (C) 2005 Jesper Juhl <jesper.juhl@gmail.com>
11 *
12 * Fan control based on smcFanControl:
13 * Copyright (C) 2006 Hendrik Holtmann <holtmann@mac.com>
14 *
15 * This program is free software; you can redistribute it and/or modify it
16 * under the terms of the GNU General Public License v2 as published by the
17 * Free Software Foundation.
18 *
19 * This program is distributed in the hope that it will be useful, but WITHOUT
20 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
21 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
22 * more details.
23 *
24 * You should have received a copy of the GNU General Public License along with
25 * this program; if not, write to the Free Software Foundation, Inc.,
26 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
27 */
28
Joe Perches1ee7c712010-11-09 15:15:03 +000029#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
30
Nicolas Boichat6f2fad72007-05-08 00:24:52 -070031#include <linux/delay.h>
32#include <linux/platform_device.h>
Dmitry Torokhovd5cf2b92007-09-26 00:01:35 -040033#include <linux/input-polldev.h>
Nicolas Boichat6f2fad72007-05-08 00:24:52 -070034#include <linux/kernel.h>
Henrik Rydberg58745832010-11-10 10:58:03 +000035#include <linux/slab.h>
Nicolas Boichat6f2fad72007-05-08 00:24:52 -070036#include <linux/module.h>
37#include <linux/timer.h>
38#include <linux/dmi.h>
39#include <linux/mutex.h>
40#include <linux/hwmon-sysfs.h>
H Hartley Sweeten6055fae2009-09-15 17:18:13 +020041#include <linux/io.h>
Nicolas Boichat6f2fad72007-05-08 00:24:52 -070042#include <linux/leds.h>
43#include <linux/hwmon.h>
44#include <linux/workqueue.h>
45
46/* data port used by Apple SMC */
47#define APPLESMC_DATA_PORT 0x300
48/* command/status port used by Apple SMC */
49#define APPLESMC_CMD_PORT 0x304
50
51#define APPLESMC_NR_PORTS 32 /* 0x300-0x31f */
52
53#define APPLESMC_MAX_DATA_LENGTH 32
54
Henrik Rydberg58745832010-11-10 10:58:03 +000055/* wait up to 32 ms for a status change. */
Henrik Rydberg8c9398d2008-10-18 20:27:43 -070056#define APPLESMC_MIN_WAIT 0x0040
57#define APPLESMC_MAX_WAIT 0x8000
58
Nicolas Boichat6f2fad72007-05-08 00:24:52 -070059#define APPLESMC_STATUS_MASK 0x0f
60#define APPLESMC_READ_CMD 0x10
61#define APPLESMC_WRITE_CMD 0x11
62#define APPLESMC_GET_KEY_BY_INDEX_CMD 0x12
63#define APPLESMC_GET_KEY_TYPE_CMD 0x13
64
65#define KEY_COUNT_KEY "#KEY" /* r-o ui32 */
66
Henrik Rydberg8bd1a122008-10-18 20:27:39 -070067#define LIGHT_SENSOR_LEFT_KEY "ALV0" /* r-o {alv (6-10 bytes) */
68#define LIGHT_SENSOR_RIGHT_KEY "ALV1" /* r-o {alv (6-10 bytes) */
Dmitry Torokhovd5cf2b92007-09-26 00:01:35 -040069#define BACKLIGHT_KEY "LKSB" /* w-o {lkb (2 bytes) */
Nicolas Boichat6f2fad72007-05-08 00:24:52 -070070
Dmitry Torokhovd5cf2b92007-09-26 00:01:35 -040071#define CLAMSHELL_KEY "MSLD" /* r-o ui8 (unused) */
Nicolas Boichat6f2fad72007-05-08 00:24:52 -070072
73#define MOTION_SENSOR_X_KEY "MO_X" /* r-o sp78 (2 bytes) */
74#define MOTION_SENSOR_Y_KEY "MO_Y" /* r-o sp78 (2 bytes) */
75#define MOTION_SENSOR_Z_KEY "MO_Z" /* r-o sp78 (2 bytes) */
76#define MOTION_SENSOR_KEY "MOCN" /* r/w ui16 */
77
78#define FANS_COUNT "FNum" /* r-o ui8 */
79#define FANS_MANUAL "FS! " /* r-w ui16 */
80#define FAN_ACTUAL_SPEED "F0Ac" /* r-o fpe2 (2 bytes) */
81#define FAN_MIN_SPEED "F0Mn" /* r-o fpe2 (2 bytes) */
82#define FAN_MAX_SPEED "F0Mx" /* r-o fpe2 (2 bytes) */
83#define FAN_SAFE_SPEED "F0Sf" /* r-o fpe2 (2 bytes) */
84#define FAN_TARGET_SPEED "F0Tg" /* r-w fpe2 (2 bytes) */
85#define FAN_POSITION "F0ID" /* r-o char[16] */
86
Nicolas Boichat6f2fad72007-05-08 00:24:52 -070087/* List of keys used to read/write fan speeds */
88static const char* fan_speed_keys[] = {
89 FAN_ACTUAL_SPEED,
90 FAN_MIN_SPEED,
91 FAN_MAX_SPEED,
92 FAN_SAFE_SPEED,
93 FAN_TARGET_SPEED
94};
95
96#define INIT_TIMEOUT_MSECS 5000 /* wait up to 5s for device init ... */
97#define INIT_WAIT_MSECS 50 /* ... in 50ms increments */
98
Dmitry Torokhovd5cf2b92007-09-26 00:01:35 -040099#define APPLESMC_POLL_INTERVAL 50 /* msecs */
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700100#define APPLESMC_INPUT_FUZZ 4 /* input event threshold */
101#define APPLESMC_INPUT_FLAT 4
102
103#define SENSOR_X 0
104#define SENSOR_Y 1
105#define SENSOR_Z 2
106
Henrik Rydberg9792dad2010-11-10 10:58:04 +0000107#define to_index(attr) (to_sensor_dev_attr(attr)->index)
108
Henrik Rydberg9792dad2010-11-10 10:58:04 +0000109/* Dynamic device node attributes */
110struct applesmc_dev_attr {
111 struct sensor_device_attribute sda; /* hwmon attributes */
112 char name[32]; /* room for node file name */
113};
114
115/* Dynamic device node group */
116struct applesmc_node_group {
117 char *format; /* format string */
118 void *show; /* show function */
119 void *store; /* store function */
120 struct applesmc_dev_attr *nodes; /* dynamic node array */
121};
122
Henrik Rydberg58745832010-11-10 10:58:03 +0000123/* AppleSMC entry - cached register information */
124struct applesmc_entry {
125 char key[5]; /* four-letter key code */
126 u8 valid; /* set when entry is successfully read once */
127 u8 len; /* bounded by APPLESMC_MAX_DATA_LENGTH */
128 char type[5]; /* four-letter type code */
129 u8 flags; /* 0x10: func; 0x40: write; 0x80: read */
130};
131
132/* Register lookup and registers common to all SMCs */
133static struct applesmc_registers {
134 struct mutex mutex; /* register read/write mutex */
135 unsigned int key_count; /* number of SMC registers */
Henrik Rydberg9792dad2010-11-10 10:58:04 +0000136 unsigned int temp_count; /* number of temperature registers */
137 unsigned int temp_begin; /* temperature lower index bound */
138 unsigned int temp_end; /* temperature upper index bound */
Henrik Rydberg40ef06f2010-11-10 10:58:05 +0000139 int num_light_sensors; /* number of light sensors */
140 bool has_accelerometer; /* has motion sensor */
141 bool has_key_backlight; /* has keyboard backlight */
Henrik Rydberg58745832010-11-10 10:58:03 +0000142 bool init_complete; /* true when fully initialized */
143 struct applesmc_entry *cache; /* cached key entries */
144} smcreg = {
145 .mutex = __MUTEX_INITIALIZER(smcreg.mutex),
146};
147
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700148static const int debug;
149static struct platform_device *pdev;
150static s16 rest_x;
151static s16 rest_y;
Henrik Rydberga976f152009-09-21 17:04:50 -0700152static u8 backlight_state[2];
153
Tony Jones1beeffe2007-08-20 13:46:20 -0700154static struct device *hwmon_dev;
Dmitry Torokhovd5cf2b92007-09-26 00:01:35 -0400155static struct input_polled_dev *applesmc_idev;
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700156
Henrik Rydberg0559a532010-05-11 09:17:47 +0200157/* The number of fans handled by the driver */
158static unsigned int fans_handled;
159
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700160/*
161 * Last index written to key_at_index sysfs file, and value to use for all other
162 * key_at_index_* sysfs files.
163 */
164static unsigned int key_at_index;
165
166static struct workqueue_struct *applesmc_led_wq;
167
168/*
Henrik Rydberg8c9398d2008-10-18 20:27:43 -0700169 * __wait_status - Wait up to 32ms for the status port to get a certain value
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700170 * (masked with 0x0f), returning zero if the value is obtained. Callers must
171 * hold applesmc_lock.
172 */
173static int __wait_status(u8 val)
174{
Henrik Rydberg8c9398d2008-10-18 20:27:43 -0700175 int us;
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700176
177 val = val & APPLESMC_STATUS_MASK;
178
Henrik Rydberg8c9398d2008-10-18 20:27:43 -0700179 for (us = APPLESMC_MIN_WAIT; us < APPLESMC_MAX_WAIT; us <<= 1) {
180 udelay(us);
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700181 if ((inb(APPLESMC_CMD_PORT) & APPLESMC_STATUS_MASK) == val) {
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700182 return 0;
183 }
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700184 }
185
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700186 return -EIO;
187}
188
189/*
Henrik Rydberg84d2d7f2008-10-18 20:27:38 -0700190 * special treatment of command port - on newer macbooks, it seems necessary
191 * to resend the command byte before polling the status again. Callers must
192 * hold applesmc_lock.
193 */
194static int send_command(u8 cmd)
195{
Henrik Rydberg8c9398d2008-10-18 20:27:43 -0700196 int us;
197 for (us = APPLESMC_MIN_WAIT; us < APPLESMC_MAX_WAIT; us <<= 1) {
Henrik Rydberg84d2d7f2008-10-18 20:27:38 -0700198 outb(cmd, APPLESMC_CMD_PORT);
Henrik Rydberg8c9398d2008-10-18 20:27:43 -0700199 udelay(us);
Henrik Rydberg84d2d7f2008-10-18 20:27:38 -0700200 if ((inb(APPLESMC_CMD_PORT) & APPLESMC_STATUS_MASK) == 0x0c)
201 return 0;
Henrik Rydberg84d2d7f2008-10-18 20:27:38 -0700202 }
Henrik Rydberg84d2d7f2008-10-18 20:27:38 -0700203 return -EIO;
204}
205
Henrik Rydberg58745832010-11-10 10:58:03 +0000206static int send_argument(const char *key)
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700207{
208 int i;
209
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700210 for (i = 0; i < 4; i++) {
211 outb(key[i], APPLESMC_DATA_PORT);
212 if (__wait_status(0x04))
213 return -EIO;
214 }
Henrik Rydberg58745832010-11-10 10:58:03 +0000215 return 0;
216}
217
218static int read_smc(u8 cmd, const char *key, u8 *buffer, u8 len)
219{
220 int i;
221
222 if (send_command(cmd) || send_argument(key)) {
223 pr_warn("%s: read arg fail\n", key);
224 return -EIO;
225 }
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700226
227 outb(len, APPLESMC_DATA_PORT);
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700228
229 for (i = 0; i < len; i++) {
Henrik Rydberg58745832010-11-10 10:58:03 +0000230 if (__wait_status(0x05)) {
231 pr_warn("%s: read data fail\n", key);
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700232 return -EIO;
Henrik Rydberg58745832010-11-10 10:58:03 +0000233 }
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700234 buffer[i] = inb(APPLESMC_DATA_PORT);
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700235 }
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700236
237 return 0;
238}
239
Henrik Rydberg58745832010-11-10 10:58:03 +0000240static int write_smc(u8 cmd, const char *key, const u8 *buffer, u8 len)
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700241{
242 int i;
243
Henrik Rydberg58745832010-11-10 10:58:03 +0000244 if (send_command(cmd) || send_argument(key)) {
245 pr_warn("%s: write arg fail\n", key);
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700246 return -EIO;
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700247 }
248
249 outb(len, APPLESMC_DATA_PORT);
250
251 for (i = 0; i < len; i++) {
Henrik Rydberg58745832010-11-10 10:58:03 +0000252 if (__wait_status(0x04)) {
253 pr_warn("%s: write data fail\n", key);
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700254 return -EIO;
Henrik Rydberg58745832010-11-10 10:58:03 +0000255 }
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700256 outb(buffer[i], APPLESMC_DATA_PORT);
257 }
258
259 return 0;
260}
261
Henrik Rydberg58745832010-11-10 10:58:03 +0000262static int read_register_count(unsigned int *count)
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700263{
Henrik Rydberg58745832010-11-10 10:58:03 +0000264 __be32 be;
265 int ret;
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700266
Henrik Rydberg58745832010-11-10 10:58:03 +0000267 ret = read_smc(APPLESMC_READ_CMD, KEY_COUNT_KEY, (u8 *)&be, 4);
268 if (ret)
269 return ret;
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700270
Henrik Rydberg58745832010-11-10 10:58:03 +0000271 *count = be32_to_cpu(be);
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700272 return 0;
273}
274
275/*
Henrik Rydberg58745832010-11-10 10:58:03 +0000276 * Serialized I/O
277 *
278 * Returns zero on success or a negative error on failure.
279 * All functions below are concurrency safe - callers should NOT hold lock.
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700280 */
Henrik Rydberg58745832010-11-10 10:58:03 +0000281
282static int applesmc_read_entry(const struct applesmc_entry *entry,
283 u8 *buf, u8 len)
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700284{
Henrik Rydberg58745832010-11-10 10:58:03 +0000285 int ret;
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700286
Henrik Rydberg58745832010-11-10 10:58:03 +0000287 if (entry->len != len)
288 return -EINVAL;
289 mutex_lock(&smcreg.mutex);
290 ret = read_smc(APPLESMC_READ_CMD, entry->key, buf, len);
291 mutex_unlock(&smcreg.mutex);
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700292
Henrik Rydberg58745832010-11-10 10:58:03 +0000293 return ret;
294}
295
296static int applesmc_write_entry(const struct applesmc_entry *entry,
297 const u8 *buf, u8 len)
298{
299 int ret;
300
301 if (entry->len != len)
302 return -EINVAL;
303 mutex_lock(&smcreg.mutex);
304 ret = write_smc(APPLESMC_WRITE_CMD, entry->key, buf, len);
305 mutex_unlock(&smcreg.mutex);
306 return ret;
307}
308
309static const struct applesmc_entry *applesmc_get_entry_by_index(int index)
310{
311 struct applesmc_entry *cache = &smcreg.cache[index];
312 u8 key[4], info[6];
313 __be32 be;
314 int ret = 0;
315
316 if (cache->valid)
317 return cache;
318
319 mutex_lock(&smcreg.mutex);
320
321 if (cache->valid)
322 goto out;
323 be = cpu_to_be32(index);
324 ret = read_smc(APPLESMC_GET_KEY_BY_INDEX_CMD, (u8 *)&be, key, 4);
325 if (ret)
326 goto out;
327 ret = read_smc(APPLESMC_GET_KEY_TYPE_CMD, key, info, 6);
328 if (ret)
329 goto out;
330
331 memcpy(cache->key, key, 4);
332 cache->len = info[0];
333 memcpy(cache->type, &info[1], 4);
334 cache->flags = info[5];
335 cache->valid = 1;
336
337out:
338 mutex_unlock(&smcreg.mutex);
339 if (ret)
340 return ERR_PTR(ret);
341 return cache;
342}
343
344static int applesmc_get_lower_bound(unsigned int *lo, const char *key)
345{
346 int begin = 0, end = smcreg.key_count;
347 const struct applesmc_entry *entry;
348
349 while (begin != end) {
350 int middle = begin + (end - begin) / 2;
351 entry = applesmc_get_entry_by_index(middle);
352 if (IS_ERR(entry))
353 return PTR_ERR(entry);
354 if (strcmp(entry->key, key) < 0)
355 begin = middle + 1;
356 else
357 end = middle;
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700358 }
359
Henrik Rydberg58745832010-11-10 10:58:03 +0000360 *lo = begin;
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700361 return 0;
362}
363
Henrik Rydberg58745832010-11-10 10:58:03 +0000364static int applesmc_get_upper_bound(unsigned int *hi, const char *key)
365{
366 int begin = 0, end = smcreg.key_count;
367 const struct applesmc_entry *entry;
368
369 while (begin != end) {
370 int middle = begin + (end - begin) / 2;
371 entry = applesmc_get_entry_by_index(middle);
372 if (IS_ERR(entry))
373 return PTR_ERR(entry);
374 if (strcmp(key, entry->key) < 0)
375 end = middle;
376 else
377 begin = middle + 1;
378 }
379
380 *hi = begin;
381 return 0;
382}
383
384static const struct applesmc_entry *applesmc_get_entry_by_key(const char *key)
385{
386 int begin, end;
387 int ret;
388
389 ret = applesmc_get_lower_bound(&begin, key);
390 if (ret)
391 return ERR_PTR(ret);
392 ret = applesmc_get_upper_bound(&end, key);
393 if (ret)
394 return ERR_PTR(ret);
395 if (end - begin != 1)
396 return ERR_PTR(-EINVAL);
397
398 return applesmc_get_entry_by_index(begin);
399}
400
401static int applesmc_read_key(const char *key, u8 *buffer, u8 len)
402{
403 const struct applesmc_entry *entry;
404
405 entry = applesmc_get_entry_by_key(key);
406 if (IS_ERR(entry))
407 return PTR_ERR(entry);
408
409 return applesmc_read_entry(entry, buffer, len);
410}
411
412static int applesmc_write_key(const char *key, const u8 *buffer, u8 len)
413{
414 const struct applesmc_entry *entry;
415
416 entry = applesmc_get_entry_by_key(key);
417 if (IS_ERR(entry))
418 return PTR_ERR(entry);
419
420 return applesmc_write_entry(entry, buffer, len);
421}
422
Henrik Rydberg40ef06f2010-11-10 10:58:05 +0000423static int applesmc_has_key(const char *key, bool *value)
424{
425 const struct applesmc_entry *entry;
426
427 entry = applesmc_get_entry_by_key(key);
428 if (IS_ERR(entry) && PTR_ERR(entry) != -EINVAL)
429 return PTR_ERR(entry);
430
431 *value = !IS_ERR(entry);
432 return 0;
433}
434
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700435/*
Henrik Rydberg58745832010-11-10 10:58:03 +0000436 * applesmc_read_motion_sensor - Read motion sensor (X, Y or Z).
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700437 */
438static int applesmc_read_motion_sensor(int index, s16* value)
439{
440 u8 buffer[2];
441 int ret;
442
443 switch (index) {
444 case SENSOR_X:
445 ret = applesmc_read_key(MOTION_SENSOR_X_KEY, buffer, 2);
446 break;
447 case SENSOR_Y:
448 ret = applesmc_read_key(MOTION_SENSOR_Y_KEY, buffer, 2);
449 break;
450 case SENSOR_Z:
451 ret = applesmc_read_key(MOTION_SENSOR_Z_KEY, buffer, 2);
452 break;
453 default:
454 ret = -EINVAL;
455 }
456
457 *value = ((s16)buffer[0] << 8) | buffer[1];
458
459 return ret;
460}
461
462/*
Henrik Rydberg2344cd02010-11-09 15:15:02 +0000463 * applesmc_device_init - initialize the accelerometer. Can sleep.
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700464 */
Henrik Rydberg2344cd02010-11-09 15:15:02 +0000465static void applesmc_device_init(void)
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700466{
Henrik Rydberg2344cd02010-11-09 15:15:02 +0000467 int total;
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700468 u8 buffer[2];
469
Henrik Rydberg40ef06f2010-11-10 10:58:05 +0000470 if (!smcreg.has_accelerometer)
Henrik Rydberg2344cd02010-11-09 15:15:02 +0000471 return;
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700472
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700473 for (total = INIT_TIMEOUT_MSECS; total > 0; total -= INIT_WAIT_MSECS) {
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700474 if (!applesmc_read_key(MOTION_SENSOR_KEY, buffer, 2) &&
Henrik Rydberg2344cd02010-11-09 15:15:02 +0000475 (buffer[0] != 0x00 || buffer[1] != 0x00))
Henrik Rydberg58745832010-11-10 10:58:03 +0000476 return;
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700477 buffer[0] = 0xe0;
478 buffer[1] = 0x00;
479 applesmc_write_key(MOTION_SENSOR_KEY, buffer, 2);
480 msleep(INIT_WAIT_MSECS);
481 }
482
Joe Perches1ee7c712010-11-09 15:15:03 +0000483 pr_warn("failed to init the device\n");
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700484}
485
486/*
Henrik Rydberg58745832010-11-10 10:58:03 +0000487 * applesmc_get_fan_count - get the number of fans.
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700488 */
489static int applesmc_get_fan_count(void)
490{
491 int ret;
492 u8 buffer[1];
493
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700494 ret = applesmc_read_key(FANS_COUNT, buffer, 1);
495
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700496 if (ret)
497 return ret;
498 else
499 return buffer[0];
500}
501
Henrik Rydberg58745832010-11-10 10:58:03 +0000502/*
503 * applesmc_init_smcreg_try - Try to initialize register cache. Idempotent.
504 */
505static int applesmc_init_smcreg_try(void)
506{
507 struct applesmc_registers *s = &smcreg;
Henrik Rydberg40ef06f2010-11-10 10:58:05 +0000508 bool left_light_sensor, right_light_sensor;
Henrik Rydberg58745832010-11-10 10:58:03 +0000509 int ret;
510
511 if (s->init_complete)
512 return 0;
513
514 ret = read_register_count(&s->key_count);
515 if (ret)
516 return ret;
517
518 if (!s->cache)
519 s->cache = kcalloc(s->key_count, sizeof(*s->cache), GFP_KERNEL);
520 if (!s->cache)
521 return -ENOMEM;
522
Henrik Rydberg9792dad2010-11-10 10:58:04 +0000523 ret = applesmc_get_lower_bound(&s->temp_begin, "T");
524 if (ret)
525 return ret;
526 ret = applesmc_get_lower_bound(&s->temp_end, "U");
527 if (ret)
528 return ret;
529 s->temp_count = s->temp_end - s->temp_begin;
530
Henrik Rydberg40ef06f2010-11-10 10:58:05 +0000531 ret = applesmc_has_key(LIGHT_SENSOR_LEFT_KEY, &left_light_sensor);
532 if (ret)
533 return ret;
534 ret = applesmc_has_key(LIGHT_SENSOR_RIGHT_KEY, &right_light_sensor);
535 if (ret)
536 return ret;
537 ret = applesmc_has_key(MOTION_SENSOR_KEY, &s->has_accelerometer);
538 if (ret)
539 return ret;
540 ret = applesmc_has_key(BACKLIGHT_KEY, &s->has_key_backlight);
541 if (ret)
542 return ret;
543
544 s->num_light_sensors = left_light_sensor + right_light_sensor;
Henrik Rydberg58745832010-11-10 10:58:03 +0000545 s->init_complete = true;
546
Henrik Rydberg40ef06f2010-11-10 10:58:05 +0000547 pr_info("key=%d temp=%d acc=%d lux=%d kbd=%d\n",
548 s->key_count, s->temp_count,
549 s->has_accelerometer,
550 s->num_light_sensors,
551 s->has_key_backlight);
Henrik Rydberg58745832010-11-10 10:58:03 +0000552
553 return 0;
554}
555
556/*
557 * applesmc_init_smcreg - Initialize register cache.
558 *
559 * Retries until initialization is successful, or the operation times out.
560 *
561 */
562static int applesmc_init_smcreg(void)
563{
564 int ms, ret;
565
566 for (ms = 0; ms < INIT_TIMEOUT_MSECS; ms += INIT_WAIT_MSECS) {
567 ret = applesmc_init_smcreg_try();
568 if (!ret) {
569 if (ms)
570 pr_info("init_smcreg() took %d ms\n", ms);
571 return 0;
572 }
573 msleep(INIT_WAIT_MSECS);
574 }
575
576 kfree(smcreg.cache);
577 smcreg.cache = NULL;
578
579 return ret;
580}
581
582static void applesmc_destroy_smcreg(void)
583{
584 kfree(smcreg.cache);
585 smcreg.cache = NULL;
586 smcreg.init_complete = false;
587}
588
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700589/* Device model stuff */
590static int applesmc_probe(struct platform_device *dev)
591{
Henrik Rydberg58745832010-11-10 10:58:03 +0000592 int ret;
593
594 ret = applesmc_init_smcreg();
595 if (ret)
596 return ret;
597
Henrik Rydberg2344cd02010-11-09 15:15:02 +0000598 applesmc_device_init();
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700599
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700600 return 0;
601}
602
Henrik Rydberga976f152009-09-21 17:04:50 -0700603/* Synchronize device with memorized backlight state */
604static int applesmc_pm_resume(struct device *dev)
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700605{
Henrik Rydberg40ef06f2010-11-10 10:58:05 +0000606 if (smcreg.has_key_backlight)
Henrik Rydberga976f152009-09-21 17:04:50 -0700607 applesmc_write_key(BACKLIGHT_KEY, backlight_state, 2);
Henrik Rydberga976f152009-09-21 17:04:50 -0700608 return 0;
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700609}
610
Henrik Rydberga976f152009-09-21 17:04:50 -0700611/* Reinitialize device on resume from hibernation */
612static int applesmc_pm_restore(struct device *dev)
613{
Henrik Rydberg2344cd02010-11-09 15:15:02 +0000614 applesmc_device_init();
Henrik Rydberga976f152009-09-21 17:04:50 -0700615 return applesmc_pm_resume(dev);
616}
617
Alexey Dobriyan47145212009-12-14 18:00:08 -0800618static const struct dev_pm_ops applesmc_pm_ops = {
Henrik Rydberga976f152009-09-21 17:04:50 -0700619 .resume = applesmc_pm_resume,
620 .restore = applesmc_pm_restore,
621};
622
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700623static struct platform_driver applesmc_driver = {
624 .probe = applesmc_probe,
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700625 .driver = {
626 .name = "applesmc",
627 .owner = THIS_MODULE,
Henrik Rydberga976f152009-09-21 17:04:50 -0700628 .pm = &applesmc_pm_ops,
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700629 },
630};
631
632/*
633 * applesmc_calibrate - Set our "resting" values. Callers must
634 * hold applesmc_lock.
635 */
636static void applesmc_calibrate(void)
637{
638 applesmc_read_motion_sensor(SENSOR_X, &rest_x);
639 applesmc_read_motion_sensor(SENSOR_Y, &rest_y);
640 rest_x = -rest_x;
641}
642
Dmitry Torokhovd5cf2b92007-09-26 00:01:35 -0400643static void applesmc_idev_poll(struct input_polled_dev *dev)
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700644{
Dmitry Torokhovd5cf2b92007-09-26 00:01:35 -0400645 struct input_dev *idev = dev->input;
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700646 s16 x, y;
647
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700648 if (applesmc_read_motion_sensor(SENSOR_X, &x))
Henrik Rydberg58745832010-11-10 10:58:03 +0000649 return;
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700650 if (applesmc_read_motion_sensor(SENSOR_Y, &y))
Henrik Rydberg58745832010-11-10 10:58:03 +0000651 return;
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700652
653 x = -x;
Dmitry Torokhovd5cf2b92007-09-26 00:01:35 -0400654 input_report_abs(idev, ABS_X, x - rest_x);
655 input_report_abs(idev, ABS_Y, y - rest_y);
656 input_sync(idev);
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700657}
658
659/* Sysfs Files */
660
Nicolas Boichatfa744192007-05-23 13:58:13 -0700661static ssize_t applesmc_name_show(struct device *dev,
662 struct device_attribute *attr, char *buf)
663{
664 return snprintf(buf, PAGE_SIZE, "applesmc\n");
665}
666
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700667static ssize_t applesmc_position_show(struct device *dev,
668 struct device_attribute *attr, char *buf)
669{
670 int ret;
671 s16 x, y, z;
672
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700673 ret = applesmc_read_motion_sensor(SENSOR_X, &x);
674 if (ret)
675 goto out;
676 ret = applesmc_read_motion_sensor(SENSOR_Y, &y);
677 if (ret)
678 goto out;
679 ret = applesmc_read_motion_sensor(SENSOR_Z, &z);
680 if (ret)
681 goto out;
682
683out:
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700684 if (ret)
685 return ret;
686 else
687 return snprintf(buf, PAGE_SIZE, "(%d,%d,%d)\n", x, y, z);
688}
689
690static ssize_t applesmc_light_show(struct device *dev,
691 struct device_attribute *attr, char *sysfsbuf)
692{
Henrik Rydberg58745832010-11-10 10:58:03 +0000693 const struct applesmc_entry *entry;
Henrik Rydberg8bd1a122008-10-18 20:27:39 -0700694 static int data_length;
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700695 int ret;
696 u8 left = 0, right = 0;
Henrik Rydberg58745832010-11-10 10:58:03 +0000697 u8 buffer[10];
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700698
Henrik Rydberg8bd1a122008-10-18 20:27:39 -0700699 if (!data_length) {
Henrik Rydberg58745832010-11-10 10:58:03 +0000700 entry = applesmc_get_entry_by_key(LIGHT_SENSOR_LEFT_KEY);
701 if (IS_ERR(entry))
702 return PTR_ERR(entry);
703 if (entry->len > 10)
704 return -ENXIO;
705 data_length = entry->len;
Joe Perches1ee7c712010-11-09 15:15:03 +0000706 pr_info("light sensor data length set to %d\n", data_length);
Henrik Rydberg8bd1a122008-10-18 20:27:39 -0700707 }
708
709 ret = applesmc_read_key(LIGHT_SENSOR_LEFT_KEY, buffer, data_length);
Alex Murrayc3d63622009-01-15 13:51:08 -0800710 /* newer macbooks report a single 10-bit bigendian value */
711 if (data_length == 10) {
712 left = be16_to_cpu(*(__be16 *)(buffer + 6)) >> 2;
713 goto out;
714 }
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700715 left = buffer[2];
716 if (ret)
717 goto out;
Henrik Rydberg8bd1a122008-10-18 20:27:39 -0700718 ret = applesmc_read_key(LIGHT_SENSOR_RIGHT_KEY, buffer, data_length);
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700719 right = buffer[2];
720
721out:
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700722 if (ret)
723 return ret;
724 else
725 return snprintf(sysfsbuf, PAGE_SIZE, "(%d,%d)\n", left, right);
726}
727
Alex Murrayfa5575c2010-05-27 19:58:54 +0200728/* Displays sensor key as label */
729static ssize_t applesmc_show_sensor_label(struct device *dev,
730 struct device_attribute *devattr, char *sysfsbuf)
731{
Henrik Rydberg9792dad2010-11-10 10:58:04 +0000732 int index = smcreg.temp_begin + to_index(devattr);
733 const struct applesmc_entry *entry;
Alex Murrayfa5575c2010-05-27 19:58:54 +0200734
Henrik Rydberg9792dad2010-11-10 10:58:04 +0000735 entry = applesmc_get_entry_by_index(index);
736 if (IS_ERR(entry))
737 return PTR_ERR(entry);
738
739 return snprintf(sysfsbuf, PAGE_SIZE, "%s\n", entry->key);
Alex Murrayfa5575c2010-05-27 19:58:54 +0200740}
741
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700742/* Displays degree Celsius * 1000 */
743static ssize_t applesmc_show_temperature(struct device *dev,
744 struct device_attribute *devattr, char *sysfsbuf)
745{
Henrik Rydberg9792dad2010-11-10 10:58:04 +0000746 int index = smcreg.temp_begin + to_index(devattr);
747 const struct applesmc_entry *entry;
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700748 int ret;
749 u8 buffer[2];
750 unsigned int temp;
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700751
Henrik Rydberg9792dad2010-11-10 10:58:04 +0000752 entry = applesmc_get_entry_by_index(index);
753 if (IS_ERR(entry))
754 return PTR_ERR(entry);
Henrik Rydbergdcdea262010-11-09 15:15:06 +0000755 if (entry->len > 2)
756 return -EINVAL;
Henrik Rydberg9792dad2010-11-10 10:58:04 +0000757
Henrik Rydbergdcdea262010-11-09 15:15:06 +0000758 ret = applesmc_read_entry(entry, buffer, entry->len);
Henrik Rydberg9792dad2010-11-10 10:58:04 +0000759 if (ret)
760 return ret;
761
Henrik Rydbergdcdea262010-11-09 15:15:06 +0000762 if (entry->len == 2) {
763 temp = buffer[0] * 1000;
764 temp += (buffer[1] >> 6) * 250;
765 } else {
766 temp = buffer[0] * 4000;
767 }
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700768
Henrik Rydberg9792dad2010-11-10 10:58:04 +0000769 return snprintf(sysfsbuf, PAGE_SIZE, "%u\n", temp);
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700770}
771
772static ssize_t applesmc_show_fan_speed(struct device *dev,
773 struct device_attribute *attr, char *sysfsbuf)
774{
775 int ret;
776 unsigned int speed = 0;
777 char newkey[5];
778 u8 buffer[2];
779 struct sensor_device_attribute_2 *sensor_attr =
780 to_sensor_dev_attr_2(attr);
781
782 newkey[0] = fan_speed_keys[sensor_attr->nr][0];
783 newkey[1] = '0' + sensor_attr->index;
784 newkey[2] = fan_speed_keys[sensor_attr->nr][2];
785 newkey[3] = fan_speed_keys[sensor_attr->nr][3];
786 newkey[4] = 0;
787
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700788 ret = applesmc_read_key(newkey, buffer, 2);
789 speed = ((buffer[0] << 8 | buffer[1]) >> 2);
790
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700791 if (ret)
792 return ret;
793 else
794 return snprintf(sysfsbuf, PAGE_SIZE, "%u\n", speed);
795}
796
797static ssize_t applesmc_store_fan_speed(struct device *dev,
798 struct device_attribute *attr,
799 const char *sysfsbuf, size_t count)
800{
801 int ret;
802 u32 speed;
803 char newkey[5];
804 u8 buffer[2];
805 struct sensor_device_attribute_2 *sensor_attr =
806 to_sensor_dev_attr_2(attr);
807
808 speed = simple_strtoul(sysfsbuf, NULL, 10);
809
810 if (speed > 0x4000) /* Bigger than a 14-bit value */
811 return -EINVAL;
812
813 newkey[0] = fan_speed_keys[sensor_attr->nr][0];
814 newkey[1] = '0' + sensor_attr->index;
815 newkey[2] = fan_speed_keys[sensor_attr->nr][2];
816 newkey[3] = fan_speed_keys[sensor_attr->nr][3];
817 newkey[4] = 0;
818
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700819 buffer[0] = (speed >> 6) & 0xff;
820 buffer[1] = (speed << 2) & 0xff;
821 ret = applesmc_write_key(newkey, buffer, 2);
822
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700823 if (ret)
824 return ret;
825 else
826 return count;
827}
828
829static ssize_t applesmc_show_fan_manual(struct device *dev,
830 struct device_attribute *devattr, char *sysfsbuf)
831{
832 int ret;
833 u16 manual = 0;
834 u8 buffer[2];
835 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
836
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700837 ret = applesmc_read_key(FANS_MANUAL, buffer, 2);
838 manual = ((buffer[0] << 8 | buffer[1]) >> attr->index) & 0x01;
839
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700840 if (ret)
841 return ret;
842 else
843 return snprintf(sysfsbuf, PAGE_SIZE, "%d\n", manual);
844}
845
846static ssize_t applesmc_store_fan_manual(struct device *dev,
847 struct device_attribute *devattr,
848 const char *sysfsbuf, size_t count)
849{
850 int ret;
851 u8 buffer[2];
852 u32 input;
853 u16 val;
854 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
855
856 input = simple_strtoul(sysfsbuf, NULL, 10);
857
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700858 ret = applesmc_read_key(FANS_MANUAL, buffer, 2);
859 val = (buffer[0] << 8 | buffer[1]);
860 if (ret)
861 goto out;
862
863 if (input)
864 val = val | (0x01 << attr->index);
865 else
866 val = val & ~(0x01 << attr->index);
867
868 buffer[0] = (val >> 8) & 0xFF;
869 buffer[1] = val & 0xFF;
870
871 ret = applesmc_write_key(FANS_MANUAL, buffer, 2);
872
873out:
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700874 if (ret)
875 return ret;
876 else
877 return count;
878}
879
880static ssize_t applesmc_show_fan_position(struct device *dev,
881 struct device_attribute *attr, char *sysfsbuf)
882{
883 int ret;
884 char newkey[5];
885 u8 buffer[17];
886 struct sensor_device_attribute_2 *sensor_attr =
887 to_sensor_dev_attr_2(attr);
888
889 newkey[0] = FAN_POSITION[0];
890 newkey[1] = '0' + sensor_attr->index;
891 newkey[2] = FAN_POSITION[2];
892 newkey[3] = FAN_POSITION[3];
893 newkey[4] = 0;
894
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700895 ret = applesmc_read_key(newkey, buffer, 16);
896 buffer[16] = 0;
897
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700898 if (ret)
899 return ret;
900 else
901 return snprintf(sysfsbuf, PAGE_SIZE, "%s\n", buffer+4);
902}
903
904static ssize_t applesmc_calibrate_show(struct device *dev,
905 struct device_attribute *attr, char *sysfsbuf)
906{
907 return snprintf(sysfsbuf, PAGE_SIZE, "(%d,%d)\n", rest_x, rest_y);
908}
909
910static ssize_t applesmc_calibrate_store(struct device *dev,
911 struct device_attribute *attr, const char *sysfsbuf, size_t count)
912{
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700913 applesmc_calibrate();
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700914
915 return count;
916}
917
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700918static void applesmc_backlight_set(struct work_struct *work)
919{
Henrik Rydberga976f152009-09-21 17:04:50 -0700920 applesmc_write_key(BACKLIGHT_KEY, backlight_state, 2);
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700921}
922static DECLARE_WORK(backlight_work, &applesmc_backlight_set);
923
924static void applesmc_brightness_set(struct led_classdev *led_cdev,
925 enum led_brightness value)
926{
927 int ret;
928
Henrik Rydberga976f152009-09-21 17:04:50 -0700929 backlight_state[0] = value;
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700930 ret = queue_work(applesmc_led_wq, &backlight_work);
931
932 if (debug && (!ret))
933 printk(KERN_DEBUG "applesmc: work was already on the queue.\n");
934}
935
936static ssize_t applesmc_key_count_show(struct device *dev,
937 struct device_attribute *attr, char *sysfsbuf)
938{
939 int ret;
940 u8 buffer[4];
941 u32 count;
942
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700943 ret = applesmc_read_key(KEY_COUNT_KEY, buffer, 4);
944 count = ((u32)buffer[0]<<24) + ((u32)buffer[1]<<16) +
945 ((u32)buffer[2]<<8) + buffer[3];
946
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700947 if (ret)
948 return ret;
949 else
950 return snprintf(sysfsbuf, PAGE_SIZE, "%d\n", count);
951}
952
953static ssize_t applesmc_key_at_index_read_show(struct device *dev,
954 struct device_attribute *attr, char *sysfsbuf)
955{
Henrik Rydberg58745832010-11-10 10:58:03 +0000956 const struct applesmc_entry *entry;
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700957 int ret;
958
Henrik Rydberg58745832010-11-10 10:58:03 +0000959 entry = applesmc_get_entry_by_index(key_at_index);
960 if (IS_ERR(entry))
961 return PTR_ERR(entry);
962 ret = applesmc_read_entry(entry, sysfsbuf, entry->len);
963 if (ret)
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700964 return ret;
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700965
Henrik Rydberg58745832010-11-10 10:58:03 +0000966 return entry->len;
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700967}
968
969static ssize_t applesmc_key_at_index_data_length_show(struct device *dev,
970 struct device_attribute *attr, char *sysfsbuf)
971{
Henrik Rydberg58745832010-11-10 10:58:03 +0000972 const struct applesmc_entry *entry;
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700973
Henrik Rydberg58745832010-11-10 10:58:03 +0000974 entry = applesmc_get_entry_by_index(key_at_index);
975 if (IS_ERR(entry))
976 return PTR_ERR(entry);
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700977
Henrik Rydberg58745832010-11-10 10:58:03 +0000978 return snprintf(sysfsbuf, PAGE_SIZE, "%d\n", entry->len);
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700979}
980
981static ssize_t applesmc_key_at_index_type_show(struct device *dev,
982 struct device_attribute *attr, char *sysfsbuf)
983{
Henrik Rydberg58745832010-11-10 10:58:03 +0000984 const struct applesmc_entry *entry;
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700985
Henrik Rydberg58745832010-11-10 10:58:03 +0000986 entry = applesmc_get_entry_by_index(key_at_index);
987 if (IS_ERR(entry))
988 return PTR_ERR(entry);
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700989
Henrik Rydberg58745832010-11-10 10:58:03 +0000990 return snprintf(sysfsbuf, PAGE_SIZE, "%s\n", entry->type);
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700991}
992
993static ssize_t applesmc_key_at_index_name_show(struct device *dev,
994 struct device_attribute *attr, char *sysfsbuf)
995{
Henrik Rydberg58745832010-11-10 10:58:03 +0000996 const struct applesmc_entry *entry;
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700997
Henrik Rydberg58745832010-11-10 10:58:03 +0000998 entry = applesmc_get_entry_by_index(key_at_index);
999 if (IS_ERR(entry))
1000 return PTR_ERR(entry);
Nicolas Boichat6f2fad72007-05-08 00:24:52 -07001001
Henrik Rydberg58745832010-11-10 10:58:03 +00001002 return snprintf(sysfsbuf, PAGE_SIZE, "%s\n", entry->key);
Nicolas Boichat6f2fad72007-05-08 00:24:52 -07001003}
1004
1005static ssize_t applesmc_key_at_index_show(struct device *dev,
1006 struct device_attribute *attr, char *sysfsbuf)
1007{
1008 return snprintf(sysfsbuf, PAGE_SIZE, "%d\n", key_at_index);
1009}
1010
1011static ssize_t applesmc_key_at_index_store(struct device *dev,
1012 struct device_attribute *attr, const char *sysfsbuf, size_t count)
1013{
Henrik Rydberg58745832010-11-10 10:58:03 +00001014 unsigned long newkey;
Nicolas Boichat6f2fad72007-05-08 00:24:52 -07001015
Henrik Rydberg58745832010-11-10 10:58:03 +00001016 if (strict_strtoul(sysfsbuf, 10, &newkey) < 0
1017 || newkey >= smcreg.key_count)
1018 return -EINVAL;
Nicolas Boichat6f2fad72007-05-08 00:24:52 -07001019
Henrik Rydberg58745832010-11-10 10:58:03 +00001020 key_at_index = newkey;
Nicolas Boichat6f2fad72007-05-08 00:24:52 -07001021 return count;
1022}
1023
1024static struct led_classdev applesmc_backlight = {
Richard Purdie6c152be2007-10-31 15:00:07 +01001025 .name = "smc::kbd_backlight",
Nicolas Boichat6f2fad72007-05-08 00:24:52 -07001026 .default_trigger = "nand-disk",
1027 .brightness_set = applesmc_brightness_set,
1028};
1029
Nicolas Boichatfa744192007-05-23 13:58:13 -07001030static DEVICE_ATTR(name, 0444, applesmc_name_show, NULL);
1031
Nicolas Boichat6f2fad72007-05-08 00:24:52 -07001032static DEVICE_ATTR(position, 0444, applesmc_position_show, NULL);
1033static DEVICE_ATTR(calibrate, 0644,
1034 applesmc_calibrate_show, applesmc_calibrate_store);
1035
1036static struct attribute *accelerometer_attributes[] = {
1037 &dev_attr_position.attr,
1038 &dev_attr_calibrate.attr,
1039 NULL
1040};
1041
1042static const struct attribute_group accelerometer_attributes_group =
1043 { .attrs = accelerometer_attributes };
1044
1045static DEVICE_ATTR(light, 0444, applesmc_light_show, NULL);
1046
1047static DEVICE_ATTR(key_count, 0444, applesmc_key_count_show, NULL);
1048static DEVICE_ATTR(key_at_index, 0644,
1049 applesmc_key_at_index_show, applesmc_key_at_index_store);
1050static DEVICE_ATTR(key_at_index_name, 0444,
1051 applesmc_key_at_index_name_show, NULL);
1052static DEVICE_ATTR(key_at_index_type, 0444,
1053 applesmc_key_at_index_type_show, NULL);
1054static DEVICE_ATTR(key_at_index_data_length, 0444,
1055 applesmc_key_at_index_data_length_show, NULL);
1056static DEVICE_ATTR(key_at_index_data, 0444,
1057 applesmc_key_at_index_read_show, NULL);
1058
1059static struct attribute *key_enumeration_attributes[] = {
1060 &dev_attr_key_count.attr,
1061 &dev_attr_key_at_index.attr,
1062 &dev_attr_key_at_index_name.attr,
1063 &dev_attr_key_at_index_type.attr,
1064 &dev_attr_key_at_index_data_length.attr,
1065 &dev_attr_key_at_index_data.attr,
1066 NULL
1067};
1068
1069static const struct attribute_group key_enumeration_group =
1070 { .attrs = key_enumeration_attributes };
1071
1072/*
1073 * Macro defining SENSOR_DEVICE_ATTR for a fan sysfs entries.
1074 * - show actual speed
1075 * - show/store minimum speed
1076 * - show maximum speed
1077 * - show safe speed
1078 * - show/store target speed
1079 * - show/store manual mode
1080 */
1081#define sysfs_fan_speeds_offset(offset) \
1082static SENSOR_DEVICE_ATTR_2(fan##offset##_input, S_IRUGO, \
1083 applesmc_show_fan_speed, NULL, 0, offset-1); \
1084\
1085static SENSOR_DEVICE_ATTR_2(fan##offset##_min, S_IRUGO | S_IWUSR, \
1086 applesmc_show_fan_speed, applesmc_store_fan_speed, 1, offset-1); \
1087\
1088static SENSOR_DEVICE_ATTR_2(fan##offset##_max, S_IRUGO, \
1089 applesmc_show_fan_speed, NULL, 2, offset-1); \
1090\
1091static SENSOR_DEVICE_ATTR_2(fan##offset##_safe, S_IRUGO, \
1092 applesmc_show_fan_speed, NULL, 3, offset-1); \
1093\
1094static SENSOR_DEVICE_ATTR_2(fan##offset##_output, S_IRUGO | S_IWUSR, \
1095 applesmc_show_fan_speed, applesmc_store_fan_speed, 4, offset-1); \
1096\
1097static SENSOR_DEVICE_ATTR(fan##offset##_manual, S_IRUGO | S_IWUSR, \
1098 applesmc_show_fan_manual, applesmc_store_fan_manual, offset-1); \
1099\
Jean Delvareda4e8ca2007-05-08 20:27:05 -07001100static SENSOR_DEVICE_ATTR(fan##offset##_label, S_IRUGO, \
Nicolas Boichat6f2fad72007-05-08 00:24:52 -07001101 applesmc_show_fan_position, NULL, offset-1); \
1102\
1103static struct attribute *fan##offset##_attributes[] = { \
1104 &sensor_dev_attr_fan##offset##_input.dev_attr.attr, \
1105 &sensor_dev_attr_fan##offset##_min.dev_attr.attr, \
1106 &sensor_dev_attr_fan##offset##_max.dev_attr.attr, \
1107 &sensor_dev_attr_fan##offset##_safe.dev_attr.attr, \
1108 &sensor_dev_attr_fan##offset##_output.dev_attr.attr, \
1109 &sensor_dev_attr_fan##offset##_manual.dev_attr.attr, \
Jean Delvareda4e8ca2007-05-08 20:27:05 -07001110 &sensor_dev_attr_fan##offset##_label.dev_attr.attr, \
Nicolas Boichat6f2fad72007-05-08 00:24:52 -07001111 NULL \
1112};
1113
1114/*
1115 * Create the needed functions for each fan using the macro defined above
René Rebe8de57702007-10-16 14:19:20 -07001116 * (4 fans are supported)
Nicolas Boichat6f2fad72007-05-08 00:24:52 -07001117 */
1118sysfs_fan_speeds_offset(1);
1119sysfs_fan_speeds_offset(2);
René Rebe8de57702007-10-16 14:19:20 -07001120sysfs_fan_speeds_offset(3);
1121sysfs_fan_speeds_offset(4);
Nicolas Boichat6f2fad72007-05-08 00:24:52 -07001122
1123static const struct attribute_group fan_attribute_groups[] = {
1124 { .attrs = fan1_attributes },
René Rebe8de57702007-10-16 14:19:20 -07001125 { .attrs = fan2_attributes },
1126 { .attrs = fan3_attributes },
1127 { .attrs = fan4_attributes },
Nicolas Boichat6f2fad72007-05-08 00:24:52 -07001128};
1129
Henrik Rydberg9792dad2010-11-10 10:58:04 +00001130static struct applesmc_node_group temp_group[] = {
1131 { "temp%d_label", applesmc_show_sensor_label },
1132 { "temp%d_input", applesmc_show_temperature },
1133 { }
Alex Murrayfa5575c2010-05-27 19:58:54 +02001134};
1135
Nicolas Boichat6f2fad72007-05-08 00:24:52 -07001136/* Module stuff */
1137
1138/*
Henrik Rydberg9792dad2010-11-10 10:58:04 +00001139 * applesmc_destroy_nodes - remove files and free associated memory
1140 */
1141static void applesmc_destroy_nodes(struct applesmc_node_group *groups)
1142{
1143 struct applesmc_node_group *grp;
1144 struct applesmc_dev_attr *node;
1145
1146 for (grp = groups; grp->nodes; grp++) {
1147 for (node = grp->nodes; node->sda.dev_attr.attr.name; node++)
1148 sysfs_remove_file(&pdev->dev.kobj,
1149 &node->sda.dev_attr.attr);
1150 kfree(grp->nodes);
1151 grp->nodes = NULL;
1152 }
1153}
1154
1155/*
1156 * applesmc_create_nodes - create a two-dimensional group of sysfs files
1157 */
1158static int applesmc_create_nodes(struct applesmc_node_group *groups, int num)
1159{
1160 struct applesmc_node_group *grp;
1161 struct applesmc_dev_attr *node;
1162 struct attribute *attr;
1163 int ret, i;
1164
1165 for (grp = groups; grp->format; grp++) {
1166 grp->nodes = kcalloc(num + 1, sizeof(*node), GFP_KERNEL);
1167 if (!grp->nodes) {
1168 ret = -ENOMEM;
1169 goto out;
1170 }
1171 for (i = 0; i < num; i++) {
1172 node = &grp->nodes[i];
1173 sprintf(node->name, grp->format, i + 1);
1174 node->sda.index = i;
1175 node->sda.dev_attr.show = grp->show;
1176 node->sda.dev_attr.store = grp->store;
1177 attr = &node->sda.dev_attr.attr;
1178 attr->name = node->name;
1179 attr->mode = S_IRUGO | (grp->store ? S_IWUSR : 0);
1180 ret = sysfs_create_file(&pdev->dev.kobj, attr);
1181 if (ret) {
1182 attr->name = NULL;
1183 goto out;
1184 }
1185 }
1186 }
1187
1188 return 0;
1189out:
1190 applesmc_destroy_nodes(groups);
1191 return ret;
1192}
1193
Nicolas Boichat6f2fad72007-05-08 00:24:52 -07001194/* Create accelerometer ressources */
1195static int applesmc_create_accelerometer(void)
1196{
Dmitry Torokhovd5cf2b92007-09-26 00:01:35 -04001197 struct input_dev *idev;
Nicolas Boichat6f2fad72007-05-08 00:24:52 -07001198 int ret;
1199
1200 ret = sysfs_create_group(&pdev->dev.kobj,
1201 &accelerometer_attributes_group);
1202 if (ret)
1203 goto out;
1204
Dmitry Torokhovd5cf2b92007-09-26 00:01:35 -04001205 applesmc_idev = input_allocate_polled_device();
Nicolas Boichat6f2fad72007-05-08 00:24:52 -07001206 if (!applesmc_idev) {
1207 ret = -ENOMEM;
1208 goto out_sysfs;
1209 }
1210
Dmitry Torokhovd5cf2b92007-09-26 00:01:35 -04001211 applesmc_idev->poll = applesmc_idev_poll;
1212 applesmc_idev->poll_interval = APPLESMC_POLL_INTERVAL;
1213
Nicolas Boichat6f2fad72007-05-08 00:24:52 -07001214 /* initial calibrate for the input device */
1215 applesmc_calibrate();
1216
Dmitry Torokhovd5cf2b92007-09-26 00:01:35 -04001217 /* initialize the input device */
1218 idev = applesmc_idev->input;
1219 idev->name = "applesmc";
1220 idev->id.bustype = BUS_HOST;
1221 idev->dev.parent = &pdev->dev;
Jiri Slaby7b19ada2007-10-18 23:40:32 -07001222 idev->evbit[0] = BIT_MASK(EV_ABS);
Dmitry Torokhovd5cf2b92007-09-26 00:01:35 -04001223 input_set_abs_params(idev, ABS_X,
Nicolas Boichat6f2fad72007-05-08 00:24:52 -07001224 -256, 256, APPLESMC_INPUT_FUZZ, APPLESMC_INPUT_FLAT);
Dmitry Torokhovd5cf2b92007-09-26 00:01:35 -04001225 input_set_abs_params(idev, ABS_Y,
Nicolas Boichat6f2fad72007-05-08 00:24:52 -07001226 -256, 256, APPLESMC_INPUT_FUZZ, APPLESMC_INPUT_FLAT);
1227
Dmitry Torokhovd5cf2b92007-09-26 00:01:35 -04001228 ret = input_register_polled_device(applesmc_idev);
Nicolas Boichat6f2fad72007-05-08 00:24:52 -07001229 if (ret)
1230 goto out_idev;
1231
Nicolas Boichat6f2fad72007-05-08 00:24:52 -07001232 return 0;
1233
1234out_idev:
Dmitry Torokhovd5cf2b92007-09-26 00:01:35 -04001235 input_free_polled_device(applesmc_idev);
Nicolas Boichat6f2fad72007-05-08 00:24:52 -07001236
1237out_sysfs:
1238 sysfs_remove_group(&pdev->dev.kobj, &accelerometer_attributes_group);
1239
1240out:
Joe Perches1ee7c712010-11-09 15:15:03 +00001241 pr_warn("driver init failed (ret=%d)!\n", ret);
Nicolas Boichat6f2fad72007-05-08 00:24:52 -07001242 return ret;
1243}
1244
1245/* Release all ressources used by the accelerometer */
1246static void applesmc_release_accelerometer(void)
1247{
Dmitry Torokhovd5cf2b92007-09-26 00:01:35 -04001248 input_unregister_polled_device(applesmc_idev);
1249 input_free_polled_device(applesmc_idev);
Nicolas Boichat6f2fad72007-05-08 00:24:52 -07001250 sysfs_remove_group(&pdev->dev.kobj, &accelerometer_attributes_group);
1251}
Henrik Rydberg40ef06f2010-11-10 10:58:05 +00001252static int applesmc_dmi_match(const struct dmi_system_id *id)
1253{
1254 return 1;
1255}
Nicolas Boichat6f2fad72007-05-08 00:24:52 -07001256
1257/* Note that DMI_MATCH(...,"MacBook") will match "MacBookPro1,1".
1258 * So we need to put "Apple MacBook Pro" before "Apple MacBook". */
1259static __initdata struct dmi_system_id applesmc_whitelist[] = {
Henrik Rydbergf5274c92008-10-18 20:27:40 -07001260 { applesmc_dmi_match, "Apple MacBook Air", {
1261 DMI_MATCH(DMI_BOARD_VENDOR, "Apple"),
1262 DMI_MATCH(DMI_PRODUCT_NAME, "MacBookAir") },
Henrik Rydberg40ef06f2010-11-10 10:58:05 +00001263 },
Nicolas Boichat6f2fad72007-05-08 00:24:52 -07001264 { applesmc_dmi_match, "Apple MacBook Pro", {
1265 DMI_MATCH(DMI_BOARD_VENDOR,"Apple"),
1266 DMI_MATCH(DMI_PRODUCT_NAME,"MacBookPro") },
Henrik Rydberg40ef06f2010-11-10 10:58:05 +00001267 },
Riki Oktariantocd19ba12008-02-04 23:41:58 -08001268 { applesmc_dmi_match, "Apple MacBook", {
1269 DMI_MATCH(DMI_BOARD_VENDOR,"Apple"),
1270 DMI_MATCH(DMI_PRODUCT_NAME,"MacBook") },
Henrik Rydberg40ef06f2010-11-10 10:58:05 +00001271 },
Nicolas Boichat6f2fad72007-05-08 00:24:52 -07001272 { applesmc_dmi_match, "Apple Macmini", {
1273 DMI_MATCH(DMI_BOARD_VENDOR,"Apple"),
1274 DMI_MATCH(DMI_PRODUCT_NAME,"Macmini") },
Henrik Rydberg40ef06f2010-11-10 10:58:05 +00001275 },
Henrik Rydberg45a3a362008-11-19 15:36:42 -08001276 { applesmc_dmi_match, "Apple MacPro", {
1277 DMI_MATCH(DMI_BOARD_VENDOR, "Apple"),
1278 DMI_MATCH(DMI_PRODUCT_NAME, "MacPro") },
Henrik Rydberg40ef06f2010-11-10 10:58:05 +00001279 },
Roberto De Ioris9f86f282008-08-15 00:40:30 -07001280 { applesmc_dmi_match, "Apple iMac", {
1281 DMI_MATCH(DMI_BOARD_VENDOR,"Apple"),
1282 DMI_MATCH(DMI_PRODUCT_NAME,"iMac") },
Henrik Rydberg40ef06f2010-11-10 10:58:05 +00001283 },
Nicolas Boichat6f2fad72007-05-08 00:24:52 -07001284 { .ident = NULL }
1285};
1286
1287static int __init applesmc_init(void)
1288{
1289 int ret;
1290 int count;
Nicolas Boichat6f2fad72007-05-08 00:24:52 -07001291
Nicolas Boichat6f2fad72007-05-08 00:24:52 -07001292 if (!dmi_check_system(applesmc_whitelist)) {
Joe Perches1ee7c712010-11-09 15:15:03 +00001293 pr_warn("supported laptop not found!\n");
Nicolas Boichat6f2fad72007-05-08 00:24:52 -07001294 ret = -ENODEV;
1295 goto out;
1296 }
1297
1298 if (!request_region(APPLESMC_DATA_PORT, APPLESMC_NR_PORTS,
1299 "applesmc")) {
1300 ret = -ENXIO;
1301 goto out;
1302 }
1303
1304 ret = platform_driver_register(&applesmc_driver);
1305 if (ret)
1306 goto out_region;
1307
Jean Delvareddfbf2a2007-05-08 20:27:04 -07001308 pdev = platform_device_register_simple("applesmc", APPLESMC_DATA_PORT,
1309 NULL, 0);
Nicolas Boichat6f2fad72007-05-08 00:24:52 -07001310 if (IS_ERR(pdev)) {
1311 ret = PTR_ERR(pdev);
1312 goto out_driver;
1313 }
1314
Henrik Rydberg58745832010-11-10 10:58:03 +00001315 /* create register cache */
1316 ret = applesmc_init_smcreg();
Nicolas Boichat6996abf2007-05-27 22:17:43 +02001317 if (ret)
1318 goto out_device;
Nicolas Boichatfa744192007-05-23 13:58:13 -07001319
Henrik Rydberg58745832010-11-10 10:58:03 +00001320 ret = sysfs_create_file(&pdev->dev.kobj, &dev_attr_name.attr);
1321 if (ret)
1322 goto out_smcreg;
1323
Nicolas Boichat6f2fad72007-05-08 00:24:52 -07001324 /* Create key enumeration sysfs files */
1325 ret = sysfs_create_group(&pdev->dev.kobj, &key_enumeration_group);
1326 if (ret)
Nicolas Boichat6996abf2007-05-27 22:17:43 +02001327 goto out_name;
Nicolas Boichat6f2fad72007-05-08 00:24:52 -07001328
1329 /* create fan files */
1330 count = applesmc_get_fan_count();
Henrik Rydberg0559a532010-05-11 09:17:47 +02001331 if (count < 0)
Joe Perches1ee7c712010-11-09 15:15:03 +00001332 pr_err("Cannot get the number of fans\n");
Henrik Rydberg0559a532010-05-11 09:17:47 +02001333 else
Joe Perches1ee7c712010-11-09 15:15:03 +00001334 pr_info("%d fans found\n", count);
Nicolas Boichat6f2fad72007-05-08 00:24:52 -07001335
Henrik Rydberg0559a532010-05-11 09:17:47 +02001336 if (count > 4) {
1337 count = 4;
Joe Perches1ee7c712010-11-09 15:15:03 +00001338 pr_warn("A maximum of 4 fans are supported by this driver\n");
Henrik Rydberg0559a532010-05-11 09:17:47 +02001339 }
1340
1341 while (fans_handled < count) {
1342 ret = sysfs_create_group(&pdev->dev.kobj,
1343 &fan_attribute_groups[fans_handled]);
1344 if (ret)
1345 goto out_fans;
1346 fans_handled++;
Nicolas Boichat6f2fad72007-05-08 00:24:52 -07001347 }
1348
Henrik Rydberg9792dad2010-11-10 10:58:04 +00001349 ret = applesmc_create_nodes(temp_group, smcreg.temp_count);
1350 if (ret)
1351 goto out_fans;
Nicolas Boichat6f2fad72007-05-08 00:24:52 -07001352
Henrik Rydberg40ef06f2010-11-10 10:58:05 +00001353 if (smcreg.has_accelerometer) {
Nicolas Boichat6f2fad72007-05-08 00:24:52 -07001354 ret = applesmc_create_accelerometer();
1355 if (ret)
1356 goto out_temperature;
1357 }
1358
Henrik Rydberg40ef06f2010-11-10 10:58:05 +00001359 if (smcreg.num_light_sensors) {
Nicolas Boichat6f2fad72007-05-08 00:24:52 -07001360 /* Add light sensor file */
1361 ret = sysfs_create_file(&pdev->dev.kobj, &dev_attr_light.attr);
1362 if (ret)
1363 goto out_accelerometer;
Henrik Rydberg40ef06f2010-11-10 10:58:05 +00001364 }
Nicolas Boichat6f2fad72007-05-08 00:24:52 -07001365
Henrik Rydberg40ef06f2010-11-10 10:58:05 +00001366 if (smcreg.has_key_backlight) {
Nicolas Boichat6f2fad72007-05-08 00:24:52 -07001367 /* Create the workqueue */
1368 applesmc_led_wq = create_singlethread_workqueue("applesmc-led");
1369 if (!applesmc_led_wq) {
1370 ret = -ENOMEM;
1371 goto out_light_sysfs;
1372 }
1373
1374 /* register as a led device */
1375 ret = led_classdev_register(&pdev->dev, &applesmc_backlight);
1376 if (ret < 0)
1377 goto out_light_wq;
1378 }
1379
Tony Jones1beeffe2007-08-20 13:46:20 -07001380 hwmon_dev = hwmon_device_register(&pdev->dev);
1381 if (IS_ERR(hwmon_dev)) {
1382 ret = PTR_ERR(hwmon_dev);
Nicolas Boichat6f2fad72007-05-08 00:24:52 -07001383 goto out_light_ledclass;
1384 }
1385
Joe Perches1ee7c712010-11-09 15:15:03 +00001386 pr_info("driver successfully loaded\n");
Nicolas Boichat6f2fad72007-05-08 00:24:52 -07001387
1388 return 0;
1389
1390out_light_ledclass:
Henrik Rydberg40ef06f2010-11-10 10:58:05 +00001391 if (smcreg.has_key_backlight)
Nicolas Boichat6f2fad72007-05-08 00:24:52 -07001392 led_classdev_unregister(&applesmc_backlight);
1393out_light_wq:
Henrik Rydberg40ef06f2010-11-10 10:58:05 +00001394 if (smcreg.has_key_backlight)
Nicolas Boichat6f2fad72007-05-08 00:24:52 -07001395 destroy_workqueue(applesmc_led_wq);
1396out_light_sysfs:
Henrik Rydberg40ef06f2010-11-10 10:58:05 +00001397 if (smcreg.num_light_sensors)
Nicolas Boichat6f2fad72007-05-08 00:24:52 -07001398 sysfs_remove_file(&pdev->dev.kobj, &dev_attr_light.attr);
1399out_accelerometer:
Henrik Rydberg40ef06f2010-11-10 10:58:05 +00001400 if (smcreg.has_accelerometer)
Nicolas Boichat6f2fad72007-05-08 00:24:52 -07001401 applesmc_release_accelerometer();
1402out_temperature:
Henrik Rydberg9792dad2010-11-10 10:58:04 +00001403 applesmc_destroy_nodes(temp_group);
Henrik Rydberg0559a532010-05-11 09:17:47 +02001404out_fans:
1405 while (fans_handled)
1406 sysfs_remove_group(&pdev->dev.kobj,
1407 &fan_attribute_groups[--fans_handled]);
Nicolas Boichat6f2fad72007-05-08 00:24:52 -07001408 sysfs_remove_group(&pdev->dev.kobj, &key_enumeration_group);
Nicolas Boichat6996abf2007-05-27 22:17:43 +02001409out_name:
1410 sysfs_remove_file(&pdev->dev.kobj, &dev_attr_name.attr);
Henrik Rydberg58745832010-11-10 10:58:03 +00001411out_smcreg:
1412 applesmc_destroy_smcreg();
Nicolas Boichat6f2fad72007-05-08 00:24:52 -07001413out_device:
1414 platform_device_unregister(pdev);
1415out_driver:
1416 platform_driver_unregister(&applesmc_driver);
1417out_region:
1418 release_region(APPLESMC_DATA_PORT, APPLESMC_NR_PORTS);
1419out:
Joe Perches1ee7c712010-11-09 15:15:03 +00001420 pr_warn("driver init failed (ret=%d)!\n", ret);
Nicolas Boichat6f2fad72007-05-08 00:24:52 -07001421 return ret;
1422}
1423
1424static void __exit applesmc_exit(void)
1425{
Tony Jones1beeffe2007-08-20 13:46:20 -07001426 hwmon_device_unregister(hwmon_dev);
Henrik Rydberg40ef06f2010-11-10 10:58:05 +00001427 if (smcreg.has_key_backlight) {
Nicolas Boichat6f2fad72007-05-08 00:24:52 -07001428 led_classdev_unregister(&applesmc_backlight);
1429 destroy_workqueue(applesmc_led_wq);
Nicolas Boichat6f2fad72007-05-08 00:24:52 -07001430 }
Henrik Rydberg40ef06f2010-11-10 10:58:05 +00001431 if (smcreg.num_light_sensors)
1432 sysfs_remove_file(&pdev->dev.kobj, &dev_attr_light.attr);
1433 if (smcreg.has_accelerometer)
Nicolas Boichat6f2fad72007-05-08 00:24:52 -07001434 applesmc_release_accelerometer();
Henrik Rydberg9792dad2010-11-10 10:58:04 +00001435 applesmc_destroy_nodes(temp_group);
Henrik Rydberg0559a532010-05-11 09:17:47 +02001436 while (fans_handled)
1437 sysfs_remove_group(&pdev->dev.kobj,
1438 &fan_attribute_groups[--fans_handled]);
Nicolas Boichat6f2fad72007-05-08 00:24:52 -07001439 sysfs_remove_group(&pdev->dev.kobj, &key_enumeration_group);
Nicolas Boichat6996abf2007-05-27 22:17:43 +02001440 sysfs_remove_file(&pdev->dev.kobj, &dev_attr_name.attr);
Henrik Rydberg58745832010-11-10 10:58:03 +00001441 applesmc_destroy_smcreg();
Nicolas Boichat6f2fad72007-05-08 00:24:52 -07001442 platform_device_unregister(pdev);
1443 platform_driver_unregister(&applesmc_driver);
1444 release_region(APPLESMC_DATA_PORT, APPLESMC_NR_PORTS);
1445
Joe Perches1ee7c712010-11-09 15:15:03 +00001446 pr_info("driver unloaded\n");
Nicolas Boichat6f2fad72007-05-08 00:24:52 -07001447}
1448
1449module_init(applesmc_init);
1450module_exit(applesmc_exit);
1451
1452MODULE_AUTHOR("Nicolas Boichat");
1453MODULE_DESCRIPTION("Apple SMC");
1454MODULE_LICENSE("GPL v2");
Henrik Rydbergdc924ef2008-12-01 13:13:49 -08001455MODULE_DEVICE_TABLE(dmi, applesmc_whitelist);