blob: 03c24b7c9cbde60b3c5c7e81d117c79468a78e45 [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
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700109/* Structure to be passed to DMI_MATCH function */
110struct dmi_match_data {
111/* Indicates whether this computer has an accelerometer. */
112 int accelerometer;
113/* Indicates whether this computer has light sensors and keyboard backlight. */
114 int light;
115/* Indicates which temperature sensors set to use. */
116 int temperature_set;
117};
118
Henrik Rydberg9792dad2010-11-10 10:58:04 +0000119/* Dynamic device node attributes */
120struct applesmc_dev_attr {
121 struct sensor_device_attribute sda; /* hwmon attributes */
122 char name[32]; /* room for node file name */
123};
124
125/* Dynamic device node group */
126struct applesmc_node_group {
127 char *format; /* format string */
128 void *show; /* show function */
129 void *store; /* store function */
130 struct applesmc_dev_attr *nodes; /* dynamic node array */
131};
132
Henrik Rydberg58745832010-11-10 10:58:03 +0000133/* AppleSMC entry - cached register information */
134struct applesmc_entry {
135 char key[5]; /* four-letter key code */
136 u8 valid; /* set when entry is successfully read once */
137 u8 len; /* bounded by APPLESMC_MAX_DATA_LENGTH */
138 char type[5]; /* four-letter type code */
139 u8 flags; /* 0x10: func; 0x40: write; 0x80: read */
140};
141
142/* Register lookup and registers common to all SMCs */
143static struct applesmc_registers {
144 struct mutex mutex; /* register read/write mutex */
145 unsigned int key_count; /* number of SMC registers */
Henrik Rydberg9792dad2010-11-10 10:58:04 +0000146 unsigned int temp_count; /* number of temperature registers */
147 unsigned int temp_begin; /* temperature lower index bound */
148 unsigned int temp_end; /* temperature upper index bound */
Henrik Rydberg58745832010-11-10 10:58:03 +0000149 bool init_complete; /* true when fully initialized */
150 struct applesmc_entry *cache; /* cached key entries */
151} smcreg = {
152 .mutex = __MUTEX_INITIALIZER(smcreg.mutex),
153};
154
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700155static const int debug;
156static struct platform_device *pdev;
157static s16 rest_x;
158static s16 rest_y;
Henrik Rydberga976f152009-09-21 17:04:50 -0700159static u8 backlight_state[2];
160
Tony Jones1beeffe2007-08-20 13:46:20 -0700161static struct device *hwmon_dev;
Dmitry Torokhovd5cf2b92007-09-26 00:01:35 -0400162static struct input_polled_dev *applesmc_idev;
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700163
164/* Indicates whether this computer has an accelerometer. */
165static unsigned int applesmc_accelerometer;
166
167/* Indicates whether this computer has light sensors and keyboard backlight. */
168static unsigned int applesmc_light;
169
Henrik Rydberg0559a532010-05-11 09:17:47 +0200170/* The number of fans handled by the driver */
171static unsigned int fans_handled;
172
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700173/*
174 * Last index written to key_at_index sysfs file, and value to use for all other
175 * key_at_index_* sysfs files.
176 */
177static unsigned int key_at_index;
178
179static struct workqueue_struct *applesmc_led_wq;
180
181/*
Henrik Rydberg8c9398d2008-10-18 20:27:43 -0700182 * __wait_status - Wait up to 32ms for the status port to get a certain value
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700183 * (masked with 0x0f), returning zero if the value is obtained. Callers must
184 * hold applesmc_lock.
185 */
186static int __wait_status(u8 val)
187{
Henrik Rydberg8c9398d2008-10-18 20:27:43 -0700188 int us;
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700189
190 val = val & APPLESMC_STATUS_MASK;
191
Henrik Rydberg8c9398d2008-10-18 20:27:43 -0700192 for (us = APPLESMC_MIN_WAIT; us < APPLESMC_MAX_WAIT; us <<= 1) {
193 udelay(us);
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700194 if ((inb(APPLESMC_CMD_PORT) & APPLESMC_STATUS_MASK) == val) {
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700195 return 0;
196 }
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700197 }
198
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700199 return -EIO;
200}
201
202/*
Henrik Rydberg84d2d7f2008-10-18 20:27:38 -0700203 * special treatment of command port - on newer macbooks, it seems necessary
204 * to resend the command byte before polling the status again. Callers must
205 * hold applesmc_lock.
206 */
207static int send_command(u8 cmd)
208{
Henrik Rydberg8c9398d2008-10-18 20:27:43 -0700209 int us;
210 for (us = APPLESMC_MIN_WAIT; us < APPLESMC_MAX_WAIT; us <<= 1) {
Henrik Rydberg84d2d7f2008-10-18 20:27:38 -0700211 outb(cmd, APPLESMC_CMD_PORT);
Henrik Rydberg8c9398d2008-10-18 20:27:43 -0700212 udelay(us);
Henrik Rydberg84d2d7f2008-10-18 20:27:38 -0700213 if ((inb(APPLESMC_CMD_PORT) & APPLESMC_STATUS_MASK) == 0x0c)
214 return 0;
Henrik Rydberg84d2d7f2008-10-18 20:27:38 -0700215 }
Henrik Rydberg84d2d7f2008-10-18 20:27:38 -0700216 return -EIO;
217}
218
Henrik Rydberg58745832010-11-10 10:58:03 +0000219static int send_argument(const char *key)
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700220{
221 int i;
222
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700223 for (i = 0; i < 4; i++) {
224 outb(key[i], APPLESMC_DATA_PORT);
225 if (__wait_status(0x04))
226 return -EIO;
227 }
Henrik Rydberg58745832010-11-10 10:58:03 +0000228 return 0;
229}
230
231static int read_smc(u8 cmd, const char *key, u8 *buffer, u8 len)
232{
233 int i;
234
235 if (send_command(cmd) || send_argument(key)) {
236 pr_warn("%s: read arg fail\n", key);
237 return -EIO;
238 }
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700239
240 outb(len, APPLESMC_DATA_PORT);
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700241
242 for (i = 0; i < len; i++) {
Henrik Rydberg58745832010-11-10 10:58:03 +0000243 if (__wait_status(0x05)) {
244 pr_warn("%s: read data fail\n", key);
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700245 return -EIO;
Henrik Rydberg58745832010-11-10 10:58:03 +0000246 }
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700247 buffer[i] = inb(APPLESMC_DATA_PORT);
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700248 }
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700249
250 return 0;
251}
252
Henrik Rydberg58745832010-11-10 10:58:03 +0000253static int write_smc(u8 cmd, const char *key, const u8 *buffer, u8 len)
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700254{
255 int i;
256
Henrik Rydberg58745832010-11-10 10:58:03 +0000257 if (send_command(cmd) || send_argument(key)) {
258 pr_warn("%s: write arg fail\n", key);
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700259 return -EIO;
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700260 }
261
262 outb(len, APPLESMC_DATA_PORT);
263
264 for (i = 0; i < len; i++) {
Henrik Rydberg58745832010-11-10 10:58:03 +0000265 if (__wait_status(0x04)) {
266 pr_warn("%s: write data fail\n", key);
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700267 return -EIO;
Henrik Rydberg58745832010-11-10 10:58:03 +0000268 }
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700269 outb(buffer[i], APPLESMC_DATA_PORT);
270 }
271
272 return 0;
273}
274
Henrik Rydberg58745832010-11-10 10:58:03 +0000275static int read_register_count(unsigned int *count)
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700276{
Henrik Rydberg58745832010-11-10 10:58:03 +0000277 __be32 be;
278 int ret;
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700279
Henrik Rydberg58745832010-11-10 10:58:03 +0000280 ret = read_smc(APPLESMC_READ_CMD, KEY_COUNT_KEY, (u8 *)&be, 4);
281 if (ret)
282 return ret;
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700283
Henrik Rydberg58745832010-11-10 10:58:03 +0000284 *count = be32_to_cpu(be);
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700285 return 0;
286}
287
288/*
Henrik Rydberg58745832010-11-10 10:58:03 +0000289 * Serialized I/O
290 *
291 * Returns zero on success or a negative error on failure.
292 * All functions below are concurrency safe - callers should NOT hold lock.
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700293 */
Henrik Rydberg58745832010-11-10 10:58:03 +0000294
295static int applesmc_read_entry(const struct applesmc_entry *entry,
296 u8 *buf, u8 len)
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700297{
Henrik Rydberg58745832010-11-10 10:58:03 +0000298 int ret;
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700299
Henrik Rydberg58745832010-11-10 10:58:03 +0000300 if (entry->len != len)
301 return -EINVAL;
302 mutex_lock(&smcreg.mutex);
303 ret = read_smc(APPLESMC_READ_CMD, entry->key, buf, len);
304 mutex_unlock(&smcreg.mutex);
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700305
Henrik Rydberg58745832010-11-10 10:58:03 +0000306 return ret;
307}
308
309static int applesmc_write_entry(const struct applesmc_entry *entry,
310 const u8 *buf, u8 len)
311{
312 int ret;
313
314 if (entry->len != len)
315 return -EINVAL;
316 mutex_lock(&smcreg.mutex);
317 ret = write_smc(APPLESMC_WRITE_CMD, entry->key, buf, len);
318 mutex_unlock(&smcreg.mutex);
319 return ret;
320}
321
322static const struct applesmc_entry *applesmc_get_entry_by_index(int index)
323{
324 struct applesmc_entry *cache = &smcreg.cache[index];
325 u8 key[4], info[6];
326 __be32 be;
327 int ret = 0;
328
329 if (cache->valid)
330 return cache;
331
332 mutex_lock(&smcreg.mutex);
333
334 if (cache->valid)
335 goto out;
336 be = cpu_to_be32(index);
337 ret = read_smc(APPLESMC_GET_KEY_BY_INDEX_CMD, (u8 *)&be, key, 4);
338 if (ret)
339 goto out;
340 ret = read_smc(APPLESMC_GET_KEY_TYPE_CMD, key, info, 6);
341 if (ret)
342 goto out;
343
344 memcpy(cache->key, key, 4);
345 cache->len = info[0];
346 memcpy(cache->type, &info[1], 4);
347 cache->flags = info[5];
348 cache->valid = 1;
349
350out:
351 mutex_unlock(&smcreg.mutex);
352 if (ret)
353 return ERR_PTR(ret);
354 return cache;
355}
356
357static int applesmc_get_lower_bound(unsigned int *lo, const char *key)
358{
359 int begin = 0, end = smcreg.key_count;
360 const struct applesmc_entry *entry;
361
362 while (begin != end) {
363 int middle = begin + (end - begin) / 2;
364 entry = applesmc_get_entry_by_index(middle);
365 if (IS_ERR(entry))
366 return PTR_ERR(entry);
367 if (strcmp(entry->key, key) < 0)
368 begin = middle + 1;
369 else
370 end = middle;
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700371 }
372
Henrik Rydberg58745832010-11-10 10:58:03 +0000373 *lo = begin;
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700374 return 0;
375}
376
Henrik Rydberg58745832010-11-10 10:58:03 +0000377static int applesmc_get_upper_bound(unsigned int *hi, const char *key)
378{
379 int begin = 0, end = smcreg.key_count;
380 const struct applesmc_entry *entry;
381
382 while (begin != end) {
383 int middle = begin + (end - begin) / 2;
384 entry = applesmc_get_entry_by_index(middle);
385 if (IS_ERR(entry))
386 return PTR_ERR(entry);
387 if (strcmp(key, entry->key) < 0)
388 end = middle;
389 else
390 begin = middle + 1;
391 }
392
393 *hi = begin;
394 return 0;
395}
396
397static const struct applesmc_entry *applesmc_get_entry_by_key(const char *key)
398{
399 int begin, end;
400 int ret;
401
402 ret = applesmc_get_lower_bound(&begin, key);
403 if (ret)
404 return ERR_PTR(ret);
405 ret = applesmc_get_upper_bound(&end, key);
406 if (ret)
407 return ERR_PTR(ret);
408 if (end - begin != 1)
409 return ERR_PTR(-EINVAL);
410
411 return applesmc_get_entry_by_index(begin);
412}
413
414static int applesmc_read_key(const char *key, u8 *buffer, u8 len)
415{
416 const struct applesmc_entry *entry;
417
418 entry = applesmc_get_entry_by_key(key);
419 if (IS_ERR(entry))
420 return PTR_ERR(entry);
421
422 return applesmc_read_entry(entry, buffer, len);
423}
424
425static int applesmc_write_key(const char *key, const u8 *buffer, u8 len)
426{
427 const struct applesmc_entry *entry;
428
429 entry = applesmc_get_entry_by_key(key);
430 if (IS_ERR(entry))
431 return PTR_ERR(entry);
432
433 return applesmc_write_entry(entry, buffer, len);
434}
435
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700436/*
Henrik Rydberg58745832010-11-10 10:58:03 +0000437 * applesmc_read_motion_sensor - Read motion sensor (X, Y or Z).
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700438 */
439static int applesmc_read_motion_sensor(int index, s16* value)
440{
441 u8 buffer[2];
442 int ret;
443
444 switch (index) {
445 case SENSOR_X:
446 ret = applesmc_read_key(MOTION_SENSOR_X_KEY, buffer, 2);
447 break;
448 case SENSOR_Y:
449 ret = applesmc_read_key(MOTION_SENSOR_Y_KEY, buffer, 2);
450 break;
451 case SENSOR_Z:
452 ret = applesmc_read_key(MOTION_SENSOR_Z_KEY, buffer, 2);
453 break;
454 default:
455 ret = -EINVAL;
456 }
457
458 *value = ((s16)buffer[0] << 8) | buffer[1];
459
460 return ret;
461}
462
463/*
Henrik Rydberg2344cd02010-11-09 15:15:02 +0000464 * applesmc_device_init - initialize the accelerometer. Can sleep.
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700465 */
Henrik Rydberg2344cd02010-11-09 15:15:02 +0000466static void applesmc_device_init(void)
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700467{
Henrik Rydberg2344cd02010-11-09 15:15:02 +0000468 int total;
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700469 u8 buffer[2];
470
471 if (!applesmc_accelerometer)
Henrik Rydberg2344cd02010-11-09 15:15:02 +0000472 return;
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700473
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700474 for (total = INIT_TIMEOUT_MSECS; total > 0; total -= INIT_WAIT_MSECS) {
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700475 if (!applesmc_read_key(MOTION_SENSOR_KEY, buffer, 2) &&
Henrik Rydberg2344cd02010-11-09 15:15:02 +0000476 (buffer[0] != 0x00 || buffer[1] != 0x00))
Henrik Rydberg58745832010-11-10 10:58:03 +0000477 return;
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700478 buffer[0] = 0xe0;
479 buffer[1] = 0x00;
480 applesmc_write_key(MOTION_SENSOR_KEY, buffer, 2);
481 msleep(INIT_WAIT_MSECS);
482 }
483
Joe Perches1ee7c712010-11-09 15:15:03 +0000484 pr_warn("failed to init the device\n");
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700485}
486
487/*
Henrik Rydberg58745832010-11-10 10:58:03 +0000488 * applesmc_get_fan_count - get the number of fans.
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700489 */
490static int applesmc_get_fan_count(void)
491{
492 int ret;
493 u8 buffer[1];
494
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700495 ret = applesmc_read_key(FANS_COUNT, buffer, 1);
496
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700497 if (ret)
498 return ret;
499 else
500 return buffer[0];
501}
502
Henrik Rydberg58745832010-11-10 10:58:03 +0000503/*
504 * applesmc_init_smcreg_try - Try to initialize register cache. Idempotent.
505 */
506static int applesmc_init_smcreg_try(void)
507{
508 struct applesmc_registers *s = &smcreg;
509 int ret;
510
511 if (s->init_complete)
512 return 0;
513
514 ret = read_register_count(&s->key_count);
515 if (ret)
516 return ret;
517
518 if (!s->cache)
519 s->cache = kcalloc(s->key_count, sizeof(*s->cache), GFP_KERNEL);
520 if (!s->cache)
521 return -ENOMEM;
522
Henrik 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 Rydberg58745832010-11-10 10:58:03 +0000531 s->init_complete = true;
532
Henrik Rydberg9792dad2010-11-10 10:58:04 +0000533 pr_info("key=%d temp=%d\n", s->key_count, s->temp_count);
Henrik Rydberg58745832010-11-10 10:58:03 +0000534
535 return 0;
536}
537
538/*
539 * applesmc_init_smcreg - Initialize register cache.
540 *
541 * Retries until initialization is successful, or the operation times out.
542 *
543 */
544static int applesmc_init_smcreg(void)
545{
546 int ms, ret;
547
548 for (ms = 0; ms < INIT_TIMEOUT_MSECS; ms += INIT_WAIT_MSECS) {
549 ret = applesmc_init_smcreg_try();
550 if (!ret) {
551 if (ms)
552 pr_info("init_smcreg() took %d ms\n", ms);
553 return 0;
554 }
555 msleep(INIT_WAIT_MSECS);
556 }
557
558 kfree(smcreg.cache);
559 smcreg.cache = NULL;
560
561 return ret;
562}
563
564static void applesmc_destroy_smcreg(void)
565{
566 kfree(smcreg.cache);
567 smcreg.cache = NULL;
568 smcreg.init_complete = false;
569}
570
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700571/* Device model stuff */
572static int applesmc_probe(struct platform_device *dev)
573{
Henrik Rydberg58745832010-11-10 10:58:03 +0000574 int ret;
575
576 ret = applesmc_init_smcreg();
577 if (ret)
578 return ret;
579
Henrik Rydberg2344cd02010-11-09 15:15:02 +0000580 applesmc_device_init();
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700581
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700582 return 0;
583}
584
Henrik Rydberga976f152009-09-21 17:04:50 -0700585/* Synchronize device with memorized backlight state */
586static int applesmc_pm_resume(struct device *dev)
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700587{
Henrik Rydberga976f152009-09-21 17:04:50 -0700588 if (applesmc_light)
589 applesmc_write_key(BACKLIGHT_KEY, backlight_state, 2);
Henrik Rydberga976f152009-09-21 17:04:50 -0700590 return 0;
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700591}
592
Henrik Rydberga976f152009-09-21 17:04:50 -0700593/* Reinitialize device on resume from hibernation */
594static int applesmc_pm_restore(struct device *dev)
595{
Henrik Rydberg2344cd02010-11-09 15:15:02 +0000596 applesmc_device_init();
Henrik Rydberga976f152009-09-21 17:04:50 -0700597 return applesmc_pm_resume(dev);
598}
599
Alexey Dobriyan47145212009-12-14 18:00:08 -0800600static const struct dev_pm_ops applesmc_pm_ops = {
Henrik Rydberga976f152009-09-21 17:04:50 -0700601 .resume = applesmc_pm_resume,
602 .restore = applesmc_pm_restore,
603};
604
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700605static struct platform_driver applesmc_driver = {
606 .probe = applesmc_probe,
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700607 .driver = {
608 .name = "applesmc",
609 .owner = THIS_MODULE,
Henrik Rydberga976f152009-09-21 17:04:50 -0700610 .pm = &applesmc_pm_ops,
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700611 },
612};
613
614/*
615 * applesmc_calibrate - Set our "resting" values. Callers must
616 * hold applesmc_lock.
617 */
618static void applesmc_calibrate(void)
619{
620 applesmc_read_motion_sensor(SENSOR_X, &rest_x);
621 applesmc_read_motion_sensor(SENSOR_Y, &rest_y);
622 rest_x = -rest_x;
623}
624
Dmitry Torokhovd5cf2b92007-09-26 00:01:35 -0400625static void applesmc_idev_poll(struct input_polled_dev *dev)
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700626{
Dmitry Torokhovd5cf2b92007-09-26 00:01:35 -0400627 struct input_dev *idev = dev->input;
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700628 s16 x, y;
629
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700630 if (applesmc_read_motion_sensor(SENSOR_X, &x))
Henrik Rydberg58745832010-11-10 10:58:03 +0000631 return;
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700632 if (applesmc_read_motion_sensor(SENSOR_Y, &y))
Henrik Rydberg58745832010-11-10 10:58:03 +0000633 return;
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700634
635 x = -x;
Dmitry Torokhovd5cf2b92007-09-26 00:01:35 -0400636 input_report_abs(idev, ABS_X, x - rest_x);
637 input_report_abs(idev, ABS_Y, y - rest_y);
638 input_sync(idev);
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700639}
640
641/* Sysfs Files */
642
Nicolas Boichatfa744192007-05-23 13:58:13 -0700643static ssize_t applesmc_name_show(struct device *dev,
644 struct device_attribute *attr, char *buf)
645{
646 return snprintf(buf, PAGE_SIZE, "applesmc\n");
647}
648
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700649static ssize_t applesmc_position_show(struct device *dev,
650 struct device_attribute *attr, char *buf)
651{
652 int ret;
653 s16 x, y, z;
654
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700655 ret = applesmc_read_motion_sensor(SENSOR_X, &x);
656 if (ret)
657 goto out;
658 ret = applesmc_read_motion_sensor(SENSOR_Y, &y);
659 if (ret)
660 goto out;
661 ret = applesmc_read_motion_sensor(SENSOR_Z, &z);
662 if (ret)
663 goto out;
664
665out:
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700666 if (ret)
667 return ret;
668 else
669 return snprintf(buf, PAGE_SIZE, "(%d,%d,%d)\n", x, y, z);
670}
671
672static ssize_t applesmc_light_show(struct device *dev,
673 struct device_attribute *attr, char *sysfsbuf)
674{
Henrik Rydberg58745832010-11-10 10:58:03 +0000675 const struct applesmc_entry *entry;
Henrik Rydberg8bd1a122008-10-18 20:27:39 -0700676 static int data_length;
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700677 int ret;
678 u8 left = 0, right = 0;
Henrik Rydberg58745832010-11-10 10:58:03 +0000679 u8 buffer[10];
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700680
Henrik Rydberg8bd1a122008-10-18 20:27:39 -0700681 if (!data_length) {
Henrik Rydberg58745832010-11-10 10:58:03 +0000682 entry = applesmc_get_entry_by_key(LIGHT_SENSOR_LEFT_KEY);
683 if (IS_ERR(entry))
684 return PTR_ERR(entry);
685 if (entry->len > 10)
686 return -ENXIO;
687 data_length = entry->len;
Joe Perches1ee7c712010-11-09 15:15:03 +0000688 pr_info("light sensor data length set to %d\n", data_length);
Henrik Rydberg8bd1a122008-10-18 20:27:39 -0700689 }
690
691 ret = applesmc_read_key(LIGHT_SENSOR_LEFT_KEY, buffer, data_length);
Alex Murrayc3d63622009-01-15 13:51:08 -0800692 /* newer macbooks report a single 10-bit bigendian value */
693 if (data_length == 10) {
694 left = be16_to_cpu(*(__be16 *)(buffer + 6)) >> 2;
695 goto out;
696 }
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700697 left = buffer[2];
698 if (ret)
699 goto out;
Henrik Rydberg8bd1a122008-10-18 20:27:39 -0700700 ret = applesmc_read_key(LIGHT_SENSOR_RIGHT_KEY, buffer, data_length);
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700701 right = buffer[2];
702
703out:
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700704 if (ret)
705 return ret;
706 else
707 return snprintf(sysfsbuf, PAGE_SIZE, "(%d,%d)\n", left, right);
708}
709
Alex Murrayfa5575c2010-05-27 19:58:54 +0200710/* Displays sensor key as label */
711static ssize_t applesmc_show_sensor_label(struct device *dev,
712 struct device_attribute *devattr, char *sysfsbuf)
713{
Henrik Rydberg9792dad2010-11-10 10:58:04 +0000714 int index = smcreg.temp_begin + to_index(devattr);
715 const struct applesmc_entry *entry;
Alex Murrayfa5575c2010-05-27 19:58:54 +0200716
Henrik Rydberg9792dad2010-11-10 10:58:04 +0000717 entry = applesmc_get_entry_by_index(index);
718 if (IS_ERR(entry))
719 return PTR_ERR(entry);
720
721 return snprintf(sysfsbuf, PAGE_SIZE, "%s\n", entry->key);
Alex Murrayfa5575c2010-05-27 19:58:54 +0200722}
723
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700724/* Displays degree Celsius * 1000 */
725static ssize_t applesmc_show_temperature(struct device *dev,
726 struct device_attribute *devattr, char *sysfsbuf)
727{
Henrik Rydberg9792dad2010-11-10 10:58:04 +0000728 int index = smcreg.temp_begin + to_index(devattr);
729 const struct applesmc_entry *entry;
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700730 int ret;
731 u8 buffer[2];
732 unsigned int temp;
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700733
Henrik Rydberg9792dad2010-11-10 10:58:04 +0000734 entry = applesmc_get_entry_by_index(index);
735 if (IS_ERR(entry))
736 return PTR_ERR(entry);
737
738 ret = applesmc_read_entry(entry, buffer, 2);
739 if (ret)
740 return ret;
741
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700742 temp = buffer[0]*1000;
743 temp += (buffer[1] >> 6) * 250;
744
Henrik Rydberg9792dad2010-11-10 10:58:04 +0000745 return snprintf(sysfsbuf, PAGE_SIZE, "%u\n", temp);
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700746}
747
748static ssize_t applesmc_show_fan_speed(struct device *dev,
749 struct device_attribute *attr, char *sysfsbuf)
750{
751 int ret;
752 unsigned int speed = 0;
753 char newkey[5];
754 u8 buffer[2];
755 struct sensor_device_attribute_2 *sensor_attr =
756 to_sensor_dev_attr_2(attr);
757
758 newkey[0] = fan_speed_keys[sensor_attr->nr][0];
759 newkey[1] = '0' + sensor_attr->index;
760 newkey[2] = fan_speed_keys[sensor_attr->nr][2];
761 newkey[3] = fan_speed_keys[sensor_attr->nr][3];
762 newkey[4] = 0;
763
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700764 ret = applesmc_read_key(newkey, buffer, 2);
765 speed = ((buffer[0] << 8 | buffer[1]) >> 2);
766
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700767 if (ret)
768 return ret;
769 else
770 return snprintf(sysfsbuf, PAGE_SIZE, "%u\n", speed);
771}
772
773static ssize_t applesmc_store_fan_speed(struct device *dev,
774 struct device_attribute *attr,
775 const char *sysfsbuf, size_t count)
776{
777 int ret;
778 u32 speed;
779 char newkey[5];
780 u8 buffer[2];
781 struct sensor_device_attribute_2 *sensor_attr =
782 to_sensor_dev_attr_2(attr);
783
784 speed = simple_strtoul(sysfsbuf, NULL, 10);
785
786 if (speed > 0x4000) /* Bigger than a 14-bit value */
787 return -EINVAL;
788
789 newkey[0] = fan_speed_keys[sensor_attr->nr][0];
790 newkey[1] = '0' + sensor_attr->index;
791 newkey[2] = fan_speed_keys[sensor_attr->nr][2];
792 newkey[3] = fan_speed_keys[sensor_attr->nr][3];
793 newkey[4] = 0;
794
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700795 buffer[0] = (speed >> 6) & 0xff;
796 buffer[1] = (speed << 2) & 0xff;
797 ret = applesmc_write_key(newkey, buffer, 2);
798
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700799 if (ret)
800 return ret;
801 else
802 return count;
803}
804
805static ssize_t applesmc_show_fan_manual(struct device *dev,
806 struct device_attribute *devattr, char *sysfsbuf)
807{
808 int ret;
809 u16 manual = 0;
810 u8 buffer[2];
811 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
812
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700813 ret = applesmc_read_key(FANS_MANUAL, buffer, 2);
814 manual = ((buffer[0] << 8 | buffer[1]) >> attr->index) & 0x01;
815
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700816 if (ret)
817 return ret;
818 else
819 return snprintf(sysfsbuf, PAGE_SIZE, "%d\n", manual);
820}
821
822static ssize_t applesmc_store_fan_manual(struct device *dev,
823 struct device_attribute *devattr,
824 const char *sysfsbuf, size_t count)
825{
826 int ret;
827 u8 buffer[2];
828 u32 input;
829 u16 val;
830 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
831
832 input = simple_strtoul(sysfsbuf, NULL, 10);
833
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700834 ret = applesmc_read_key(FANS_MANUAL, buffer, 2);
835 val = (buffer[0] << 8 | buffer[1]);
836 if (ret)
837 goto out;
838
839 if (input)
840 val = val | (0x01 << attr->index);
841 else
842 val = val & ~(0x01 << attr->index);
843
844 buffer[0] = (val >> 8) & 0xFF;
845 buffer[1] = val & 0xFF;
846
847 ret = applesmc_write_key(FANS_MANUAL, buffer, 2);
848
849out:
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700850 if (ret)
851 return ret;
852 else
853 return count;
854}
855
856static ssize_t applesmc_show_fan_position(struct device *dev,
857 struct device_attribute *attr, char *sysfsbuf)
858{
859 int ret;
860 char newkey[5];
861 u8 buffer[17];
862 struct sensor_device_attribute_2 *sensor_attr =
863 to_sensor_dev_attr_2(attr);
864
865 newkey[0] = FAN_POSITION[0];
866 newkey[1] = '0' + sensor_attr->index;
867 newkey[2] = FAN_POSITION[2];
868 newkey[3] = FAN_POSITION[3];
869 newkey[4] = 0;
870
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700871 ret = applesmc_read_key(newkey, buffer, 16);
872 buffer[16] = 0;
873
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700874 if (ret)
875 return ret;
876 else
877 return snprintf(sysfsbuf, PAGE_SIZE, "%s\n", buffer+4);
878}
879
880static ssize_t applesmc_calibrate_show(struct device *dev,
881 struct device_attribute *attr, char *sysfsbuf)
882{
883 return snprintf(sysfsbuf, PAGE_SIZE, "(%d,%d)\n", rest_x, rest_y);
884}
885
886static ssize_t applesmc_calibrate_store(struct device *dev,
887 struct device_attribute *attr, const char *sysfsbuf, size_t count)
888{
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700889 applesmc_calibrate();
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700890
891 return count;
892}
893
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700894static void applesmc_backlight_set(struct work_struct *work)
895{
Henrik Rydberga976f152009-09-21 17:04:50 -0700896 applesmc_write_key(BACKLIGHT_KEY, backlight_state, 2);
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700897}
898static DECLARE_WORK(backlight_work, &applesmc_backlight_set);
899
900static void applesmc_brightness_set(struct led_classdev *led_cdev,
901 enum led_brightness value)
902{
903 int ret;
904
Henrik Rydberga976f152009-09-21 17:04:50 -0700905 backlight_state[0] = value;
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700906 ret = queue_work(applesmc_led_wq, &backlight_work);
907
908 if (debug && (!ret))
909 printk(KERN_DEBUG "applesmc: work was already on the queue.\n");
910}
911
912static ssize_t applesmc_key_count_show(struct device *dev,
913 struct device_attribute *attr, char *sysfsbuf)
914{
915 int ret;
916 u8 buffer[4];
917 u32 count;
918
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700919 ret = applesmc_read_key(KEY_COUNT_KEY, buffer, 4);
920 count = ((u32)buffer[0]<<24) + ((u32)buffer[1]<<16) +
921 ((u32)buffer[2]<<8) + buffer[3];
922
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700923 if (ret)
924 return ret;
925 else
926 return snprintf(sysfsbuf, PAGE_SIZE, "%d\n", count);
927}
928
929static ssize_t applesmc_key_at_index_read_show(struct device *dev,
930 struct device_attribute *attr, char *sysfsbuf)
931{
Henrik Rydberg58745832010-11-10 10:58:03 +0000932 const struct applesmc_entry *entry;
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700933 int ret;
934
Henrik Rydberg58745832010-11-10 10:58:03 +0000935 entry = applesmc_get_entry_by_index(key_at_index);
936 if (IS_ERR(entry))
937 return PTR_ERR(entry);
938 ret = applesmc_read_entry(entry, sysfsbuf, entry->len);
939 if (ret)
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700940 return ret;
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700941
Henrik Rydberg58745832010-11-10 10:58:03 +0000942 return entry->len;
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700943}
944
945static ssize_t applesmc_key_at_index_data_length_show(struct device *dev,
946 struct device_attribute *attr, char *sysfsbuf)
947{
Henrik Rydberg58745832010-11-10 10:58:03 +0000948 const struct applesmc_entry *entry;
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700949
Henrik Rydberg58745832010-11-10 10:58:03 +0000950 entry = applesmc_get_entry_by_index(key_at_index);
951 if (IS_ERR(entry))
952 return PTR_ERR(entry);
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700953
Henrik Rydberg58745832010-11-10 10:58:03 +0000954 return snprintf(sysfsbuf, PAGE_SIZE, "%d\n", entry->len);
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700955}
956
957static ssize_t applesmc_key_at_index_type_show(struct device *dev,
958 struct device_attribute *attr, char *sysfsbuf)
959{
Henrik Rydberg58745832010-11-10 10:58:03 +0000960 const struct applesmc_entry *entry;
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700961
Henrik Rydberg58745832010-11-10 10:58:03 +0000962 entry = applesmc_get_entry_by_index(key_at_index);
963 if (IS_ERR(entry))
964 return PTR_ERR(entry);
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700965
Henrik Rydberg58745832010-11-10 10:58:03 +0000966 return snprintf(sysfsbuf, PAGE_SIZE, "%s\n", entry->type);
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700967}
968
969static ssize_t applesmc_key_at_index_name_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, "%s\n", entry->key);
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700979}
980
981static ssize_t applesmc_key_at_index_show(struct device *dev,
982 struct device_attribute *attr, char *sysfsbuf)
983{
984 return snprintf(sysfsbuf, PAGE_SIZE, "%d\n", key_at_index);
985}
986
987static ssize_t applesmc_key_at_index_store(struct device *dev,
988 struct device_attribute *attr, const char *sysfsbuf, size_t count)
989{
Henrik Rydberg58745832010-11-10 10:58:03 +0000990 unsigned long newkey;
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700991
Henrik Rydberg58745832010-11-10 10:58:03 +0000992 if (strict_strtoul(sysfsbuf, 10, &newkey) < 0
993 || newkey >= smcreg.key_count)
994 return -EINVAL;
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700995
Henrik Rydberg58745832010-11-10 10:58:03 +0000996 key_at_index = newkey;
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700997 return count;
998}
999
1000static struct led_classdev applesmc_backlight = {
Richard Purdie6c152be2007-10-31 15:00:07 +01001001 .name = "smc::kbd_backlight",
Nicolas Boichat6f2fad72007-05-08 00:24:52 -07001002 .default_trigger = "nand-disk",
1003 .brightness_set = applesmc_brightness_set,
1004};
1005
Nicolas Boichatfa744192007-05-23 13:58:13 -07001006static DEVICE_ATTR(name, 0444, applesmc_name_show, NULL);
1007
Nicolas Boichat6f2fad72007-05-08 00:24:52 -07001008static DEVICE_ATTR(position, 0444, applesmc_position_show, NULL);
1009static DEVICE_ATTR(calibrate, 0644,
1010 applesmc_calibrate_show, applesmc_calibrate_store);
1011
1012static struct attribute *accelerometer_attributes[] = {
1013 &dev_attr_position.attr,
1014 &dev_attr_calibrate.attr,
1015 NULL
1016};
1017
1018static const struct attribute_group accelerometer_attributes_group =
1019 { .attrs = accelerometer_attributes };
1020
1021static DEVICE_ATTR(light, 0444, applesmc_light_show, NULL);
1022
1023static DEVICE_ATTR(key_count, 0444, applesmc_key_count_show, NULL);
1024static DEVICE_ATTR(key_at_index, 0644,
1025 applesmc_key_at_index_show, applesmc_key_at_index_store);
1026static DEVICE_ATTR(key_at_index_name, 0444,
1027 applesmc_key_at_index_name_show, NULL);
1028static DEVICE_ATTR(key_at_index_type, 0444,
1029 applesmc_key_at_index_type_show, NULL);
1030static DEVICE_ATTR(key_at_index_data_length, 0444,
1031 applesmc_key_at_index_data_length_show, NULL);
1032static DEVICE_ATTR(key_at_index_data, 0444,
1033 applesmc_key_at_index_read_show, NULL);
1034
1035static struct attribute *key_enumeration_attributes[] = {
1036 &dev_attr_key_count.attr,
1037 &dev_attr_key_at_index.attr,
1038 &dev_attr_key_at_index_name.attr,
1039 &dev_attr_key_at_index_type.attr,
1040 &dev_attr_key_at_index_data_length.attr,
1041 &dev_attr_key_at_index_data.attr,
1042 NULL
1043};
1044
1045static const struct attribute_group key_enumeration_group =
1046 { .attrs = key_enumeration_attributes };
1047
1048/*
1049 * Macro defining SENSOR_DEVICE_ATTR for a fan sysfs entries.
1050 * - show actual speed
1051 * - show/store minimum speed
1052 * - show maximum speed
1053 * - show safe speed
1054 * - show/store target speed
1055 * - show/store manual mode
1056 */
1057#define sysfs_fan_speeds_offset(offset) \
1058static SENSOR_DEVICE_ATTR_2(fan##offset##_input, S_IRUGO, \
1059 applesmc_show_fan_speed, NULL, 0, offset-1); \
1060\
1061static SENSOR_DEVICE_ATTR_2(fan##offset##_min, S_IRUGO | S_IWUSR, \
1062 applesmc_show_fan_speed, applesmc_store_fan_speed, 1, offset-1); \
1063\
1064static SENSOR_DEVICE_ATTR_2(fan##offset##_max, S_IRUGO, \
1065 applesmc_show_fan_speed, NULL, 2, offset-1); \
1066\
1067static SENSOR_DEVICE_ATTR_2(fan##offset##_safe, S_IRUGO, \
1068 applesmc_show_fan_speed, NULL, 3, offset-1); \
1069\
1070static SENSOR_DEVICE_ATTR_2(fan##offset##_output, S_IRUGO | S_IWUSR, \
1071 applesmc_show_fan_speed, applesmc_store_fan_speed, 4, offset-1); \
1072\
1073static SENSOR_DEVICE_ATTR(fan##offset##_manual, S_IRUGO | S_IWUSR, \
1074 applesmc_show_fan_manual, applesmc_store_fan_manual, offset-1); \
1075\
Jean Delvareda4e8ca2007-05-08 20:27:05 -07001076static SENSOR_DEVICE_ATTR(fan##offset##_label, S_IRUGO, \
Nicolas Boichat6f2fad72007-05-08 00:24:52 -07001077 applesmc_show_fan_position, NULL, offset-1); \
1078\
1079static struct attribute *fan##offset##_attributes[] = { \
1080 &sensor_dev_attr_fan##offset##_input.dev_attr.attr, \
1081 &sensor_dev_attr_fan##offset##_min.dev_attr.attr, \
1082 &sensor_dev_attr_fan##offset##_max.dev_attr.attr, \
1083 &sensor_dev_attr_fan##offset##_safe.dev_attr.attr, \
1084 &sensor_dev_attr_fan##offset##_output.dev_attr.attr, \
1085 &sensor_dev_attr_fan##offset##_manual.dev_attr.attr, \
Jean Delvareda4e8ca2007-05-08 20:27:05 -07001086 &sensor_dev_attr_fan##offset##_label.dev_attr.attr, \
Nicolas Boichat6f2fad72007-05-08 00:24:52 -07001087 NULL \
1088};
1089
1090/*
1091 * Create the needed functions for each fan using the macro defined above
René Rebe8de57702007-10-16 14:19:20 -07001092 * (4 fans are supported)
Nicolas Boichat6f2fad72007-05-08 00:24:52 -07001093 */
1094sysfs_fan_speeds_offset(1);
1095sysfs_fan_speeds_offset(2);
René Rebe8de57702007-10-16 14:19:20 -07001096sysfs_fan_speeds_offset(3);
1097sysfs_fan_speeds_offset(4);
Nicolas Boichat6f2fad72007-05-08 00:24:52 -07001098
1099static const struct attribute_group fan_attribute_groups[] = {
1100 { .attrs = fan1_attributes },
René Rebe8de57702007-10-16 14:19:20 -07001101 { .attrs = fan2_attributes },
1102 { .attrs = fan3_attributes },
1103 { .attrs = fan4_attributes },
Nicolas Boichat6f2fad72007-05-08 00:24:52 -07001104};
1105
Henrik Rydberg9792dad2010-11-10 10:58:04 +00001106static struct applesmc_node_group temp_group[] = {
1107 { "temp%d_label", applesmc_show_sensor_label },
1108 { "temp%d_input", applesmc_show_temperature },
1109 { }
Alex Murrayfa5575c2010-05-27 19:58:54 +02001110};
1111
Nicolas Boichat6f2fad72007-05-08 00:24:52 -07001112/* Module stuff */
1113
1114/*
1115 * applesmc_dmi_match - found a match. return one, short-circuiting the hunt.
1116 */
Jeff Garzik18552562007-10-03 15:15:40 -04001117static int applesmc_dmi_match(const struct dmi_system_id *id)
Nicolas Boichat6f2fad72007-05-08 00:24:52 -07001118{
Nicolas Boichat6f2fad72007-05-08 00:24:52 -07001119 struct dmi_match_data* dmi_data = id->driver_data;
Joe Perches1ee7c712010-11-09 15:15:03 +00001120 pr_info("%s detected:\n", id->ident);
Nicolas Boichat6f2fad72007-05-08 00:24:52 -07001121 applesmc_accelerometer = dmi_data->accelerometer;
Joe Perches1ee7c712010-11-09 15:15:03 +00001122 pr_info(" - Model %s accelerometer\n",
1123 applesmc_accelerometer ? "with" : "without");
Nicolas Boichat6f2fad72007-05-08 00:24:52 -07001124 applesmc_light = dmi_data->light;
Joe Perches1ee7c712010-11-09 15:15:03 +00001125 pr_info(" - Model %s light sensors and backlight\n",
1126 applesmc_light ? "with" : "without");
Nicolas Boichat6f2fad72007-05-08 00:24:52 -07001127
Nicolas Boichat6f2fad72007-05-08 00:24:52 -07001128 return 1;
1129}
1130
Henrik Rydberg9792dad2010-11-10 10:58:04 +00001131/*
1132 * applesmc_destroy_nodes - remove files and free associated memory
1133 */
1134static void applesmc_destroy_nodes(struct applesmc_node_group *groups)
1135{
1136 struct applesmc_node_group *grp;
1137 struct applesmc_dev_attr *node;
1138
1139 for (grp = groups; grp->nodes; grp++) {
1140 for (node = grp->nodes; node->sda.dev_attr.attr.name; node++)
1141 sysfs_remove_file(&pdev->dev.kobj,
1142 &node->sda.dev_attr.attr);
1143 kfree(grp->nodes);
1144 grp->nodes = NULL;
1145 }
1146}
1147
1148/*
1149 * applesmc_create_nodes - create a two-dimensional group of sysfs files
1150 */
1151static int applesmc_create_nodes(struct applesmc_node_group *groups, int num)
1152{
1153 struct applesmc_node_group *grp;
1154 struct applesmc_dev_attr *node;
1155 struct attribute *attr;
1156 int ret, i;
1157
1158 for (grp = groups; grp->format; grp++) {
1159 grp->nodes = kcalloc(num + 1, sizeof(*node), GFP_KERNEL);
1160 if (!grp->nodes) {
1161 ret = -ENOMEM;
1162 goto out;
1163 }
1164 for (i = 0; i < num; i++) {
1165 node = &grp->nodes[i];
1166 sprintf(node->name, grp->format, i + 1);
1167 node->sda.index = i;
1168 node->sda.dev_attr.show = grp->show;
1169 node->sda.dev_attr.store = grp->store;
1170 attr = &node->sda.dev_attr.attr;
1171 attr->name = node->name;
1172 attr->mode = S_IRUGO | (grp->store ? S_IWUSR : 0);
1173 ret = sysfs_create_file(&pdev->dev.kobj, attr);
1174 if (ret) {
1175 attr->name = NULL;
1176 goto out;
1177 }
1178 }
1179 }
1180
1181 return 0;
1182out:
1183 applesmc_destroy_nodes(groups);
1184 return ret;
1185}
1186
Nicolas Boichat6f2fad72007-05-08 00:24:52 -07001187/* Create accelerometer ressources */
1188static int applesmc_create_accelerometer(void)
1189{
Dmitry Torokhovd5cf2b92007-09-26 00:01:35 -04001190 struct input_dev *idev;
Nicolas Boichat6f2fad72007-05-08 00:24:52 -07001191 int ret;
1192
1193 ret = sysfs_create_group(&pdev->dev.kobj,
1194 &accelerometer_attributes_group);
1195 if (ret)
1196 goto out;
1197
Dmitry Torokhovd5cf2b92007-09-26 00:01:35 -04001198 applesmc_idev = input_allocate_polled_device();
Nicolas Boichat6f2fad72007-05-08 00:24:52 -07001199 if (!applesmc_idev) {
1200 ret = -ENOMEM;
1201 goto out_sysfs;
1202 }
1203
Dmitry Torokhovd5cf2b92007-09-26 00:01:35 -04001204 applesmc_idev->poll = applesmc_idev_poll;
1205 applesmc_idev->poll_interval = APPLESMC_POLL_INTERVAL;
1206
Nicolas Boichat6f2fad72007-05-08 00:24:52 -07001207 /* initial calibrate for the input device */
1208 applesmc_calibrate();
1209
Dmitry Torokhovd5cf2b92007-09-26 00:01:35 -04001210 /* initialize the input device */
1211 idev = applesmc_idev->input;
1212 idev->name = "applesmc";
1213 idev->id.bustype = BUS_HOST;
1214 idev->dev.parent = &pdev->dev;
Jiri Slaby7b19ada2007-10-18 23:40:32 -07001215 idev->evbit[0] = BIT_MASK(EV_ABS);
Dmitry Torokhovd5cf2b92007-09-26 00:01:35 -04001216 input_set_abs_params(idev, ABS_X,
Nicolas Boichat6f2fad72007-05-08 00:24:52 -07001217 -256, 256, APPLESMC_INPUT_FUZZ, APPLESMC_INPUT_FLAT);
Dmitry Torokhovd5cf2b92007-09-26 00:01:35 -04001218 input_set_abs_params(idev, ABS_Y,
Nicolas Boichat6f2fad72007-05-08 00:24:52 -07001219 -256, 256, APPLESMC_INPUT_FUZZ, APPLESMC_INPUT_FLAT);
1220
Dmitry Torokhovd5cf2b92007-09-26 00:01:35 -04001221 ret = input_register_polled_device(applesmc_idev);
Nicolas Boichat6f2fad72007-05-08 00:24:52 -07001222 if (ret)
1223 goto out_idev;
1224
Nicolas Boichat6f2fad72007-05-08 00:24:52 -07001225 return 0;
1226
1227out_idev:
Dmitry Torokhovd5cf2b92007-09-26 00:01:35 -04001228 input_free_polled_device(applesmc_idev);
Nicolas Boichat6f2fad72007-05-08 00:24:52 -07001229
1230out_sysfs:
1231 sysfs_remove_group(&pdev->dev.kobj, &accelerometer_attributes_group);
1232
1233out:
Joe Perches1ee7c712010-11-09 15:15:03 +00001234 pr_warn("driver init failed (ret=%d)!\n", ret);
Nicolas Boichat6f2fad72007-05-08 00:24:52 -07001235 return ret;
1236}
1237
1238/* Release all ressources used by the accelerometer */
1239static void applesmc_release_accelerometer(void)
1240{
Dmitry Torokhovd5cf2b92007-09-26 00:01:35 -04001241 input_unregister_polled_device(applesmc_idev);
1242 input_free_polled_device(applesmc_idev);
Nicolas Boichat6f2fad72007-05-08 00:24:52 -07001243 sysfs_remove_group(&pdev->dev.kobj, &accelerometer_attributes_group);
1244}
1245
1246static __initdata struct dmi_match_data applesmc_dmi_data[] = {
1247/* MacBook Pro: accelerometer, backlight and temperature set 0 */
1248 { .accelerometer = 1, .light = 1, .temperature_set = 0 },
Riki Oktariantocd19ba12008-02-04 23:41:58 -08001249/* MacBook2: accelerometer and temperature set 1 */
Martin Szulecki1bed24b2007-07-09 11:41:36 -07001250 { .accelerometer = 1, .light = 0, .temperature_set = 1 },
Riki Oktariantocd19ba12008-02-04 23:41:58 -08001251/* MacBook: accelerometer and temperature set 2 */
1252 { .accelerometer = 1, .light = 0, .temperature_set = 2 },
1253/* MacMini: temperature set 3 */
René Rebe8de57702007-10-16 14:19:20 -07001254 { .accelerometer = 0, .light = 0, .temperature_set = 3 },
Riki Oktariantocd19ba12008-02-04 23:41:58 -08001255/* MacPro: temperature set 4 */
1256 { .accelerometer = 0, .light = 0, .temperature_set = 4 },
Roberto De Ioris9f86f282008-08-15 00:40:30 -07001257/* iMac: temperature set 5 */
1258 { .accelerometer = 0, .light = 0, .temperature_set = 5 },
Henrik Rydberg468cc032008-11-12 13:24:58 -08001259/* MacBook3, MacBook4: accelerometer and temperature set 6 */
Guilherme M. Schroederf91a79f2008-08-15 00:40:32 -07001260 { .accelerometer = 1, .light = 0, .temperature_set = 6 },
Henrik Rydbergf5274c92008-10-18 20:27:40 -07001261/* MacBook Air: accelerometer, backlight and temperature set 7 */
1262 { .accelerometer = 1, .light = 1, .temperature_set = 7 },
Henrik Rydbergd7549902008-10-18 20:27:41 -07001263/* MacBook Pro 4: accelerometer, backlight and temperature set 8 */
1264 { .accelerometer = 1, .light = 1, .temperature_set = 8 },
Henrik Rydberg07e8dbd2008-10-18 20:27:42 -07001265/* MacBook Pro 3: accelerometer, backlight and temperature set 9 */
1266 { .accelerometer = 1, .light = 1, .temperature_set = 9 },
Henrik Rydberg6e3530f2008-11-06 12:53:19 -08001267/* iMac 5: light sensor only, temperature set 10 */
1268 { .accelerometer = 0, .light = 0, .temperature_set = 10 },
Henrik Rydberg181209a2008-11-06 12:53:20 -08001269/* MacBook 5: accelerometer, backlight and temperature set 11 */
1270 { .accelerometer = 1, .light = 1, .temperature_set = 11 },
Henrik Rydberga6660322008-11-06 12:53:21 -08001271/* MacBook Pro 5: accelerometer, backlight and temperature set 12 */
1272 { .accelerometer = 1, .light = 1, .temperature_set = 12 },
Henrik Rydbergeefc4882008-11-06 12:53:22 -08001273/* iMac 8: light sensor only, temperature set 13 */
1274 { .accelerometer = 0, .light = 0, .temperature_set = 13 },
Henrik Rydberg9ca791b2008-11-19 15:36:06 -08001275/* iMac 6: light sensor only, temperature set 14 */
1276 { .accelerometer = 0, .light = 0, .temperature_set = 14 },
Henrik Rydberg85e0e5a2009-01-06 14:41:36 -08001277/* MacBook Air 2,1: accelerometer, backlight and temperature set 15 */
1278 { .accelerometer = 1, .light = 1, .temperature_set = 15 },
Bharath Rameshfb9f88e2009-01-29 14:25:24 -08001279/* MacPro3,1: temperature set 16 */
1280 { .accelerometer = 0, .light = 0, .temperature_set = 16 },
Justin P. Mattocke1741712010-04-14 16:14:10 +02001281/* iMac 9,1: light sensor only, temperature set 17 */
1282 { .accelerometer = 0, .light = 0, .temperature_set = 17 },
1283/* MacBook Pro 2,2: accelerometer, backlight and temperature set 18 */
1284 { .accelerometer = 1, .light = 1, .temperature_set = 18 },
Henrik Rydberg4e4a99d2010-05-27 19:58:50 +02001285/* MacBook Pro 5,3: accelerometer, backlight and temperature set 19 */
1286 { .accelerometer = 1, .light = 1, .temperature_set = 19 },
1287/* MacBook Pro 5,4: accelerometer, backlight and temperature set 20 */
1288 { .accelerometer = 1, .light = 1, .temperature_set = 20 },
Bernhard Froemel872bad52010-05-27 19:58:52 +02001289/* MacBook Pro 6,2: accelerometer, backlight and temperature set 21 */
1290 { .accelerometer = 1, .light = 1, .temperature_set = 21 },
Henrik Rydberg405eaa12010-05-27 19:58:53 +02001291/* MacBook Pro 7,1: accelerometer, backlight and temperature set 22 */
1292 { .accelerometer = 1, .light = 1, .temperature_set = 22 },
Edgar Hucek132af032010-11-09 15:15:01 +00001293/* MacBook Air 3,1: accelerometer, backlight and temperature set 23 */
1294 { .accelerometer = 0, .light = 0, .temperature_set = 23 },
Nicolas Boichat6f2fad72007-05-08 00:24:52 -07001295};
1296
1297/* Note that DMI_MATCH(...,"MacBook") will match "MacBookPro1,1".
1298 * So we need to put "Apple MacBook Pro" before "Apple MacBook". */
1299static __initdata struct dmi_system_id applesmc_whitelist[] = {
Edgar Hucek132af032010-11-09 15:15:01 +00001300 { applesmc_dmi_match, "Apple MacBook Air 3", {
1301 DMI_MATCH(DMI_BOARD_VENDOR, "Apple"),
1302 DMI_MATCH(DMI_PRODUCT_NAME, "MacBookAir3") },
1303 &applesmc_dmi_data[23]},
Henrik Rydberg85e0e5a2009-01-06 14:41:36 -08001304 { applesmc_dmi_match, "Apple MacBook Air 2", {
1305 DMI_MATCH(DMI_BOARD_VENDOR, "Apple"),
1306 DMI_MATCH(DMI_PRODUCT_NAME, "MacBookAir2") },
1307 &applesmc_dmi_data[15]},
Henrik Rydbergf5274c92008-10-18 20:27:40 -07001308 { applesmc_dmi_match, "Apple MacBook Air", {
1309 DMI_MATCH(DMI_BOARD_VENDOR, "Apple"),
1310 DMI_MATCH(DMI_PRODUCT_NAME, "MacBookAir") },
Andrew Morton7b5e3cb2008-10-18 20:27:41 -07001311 &applesmc_dmi_data[7]},
Henrik Rydberg405eaa12010-05-27 19:58:53 +02001312 { applesmc_dmi_match, "Apple MacBook Pro 7", {
1313 DMI_MATCH(DMI_BOARD_VENDOR, "Apple"),
1314 DMI_MATCH(DMI_PRODUCT_NAME, "MacBookPro7") },
1315 &applesmc_dmi_data[22]},
Henrik Rydberg4e4a99d2010-05-27 19:58:50 +02001316 { applesmc_dmi_match, "Apple MacBook Pro 5,4", {
1317 DMI_MATCH(DMI_BOARD_VENDOR, "Apple"),
1318 DMI_MATCH(DMI_PRODUCT_NAME, "MacBookPro5,4") },
1319 &applesmc_dmi_data[20]},
1320 { applesmc_dmi_match, "Apple MacBook Pro 5,3", {
1321 DMI_MATCH(DMI_BOARD_VENDOR, "Apple"),
1322 DMI_MATCH(DMI_PRODUCT_NAME, "MacBookPro5,3") },
1323 &applesmc_dmi_data[19]},
Bernhard Froemel872bad52010-05-27 19:58:52 +02001324 { applesmc_dmi_match, "Apple MacBook Pro 6", {
1325 DMI_MATCH(DMI_BOARD_VENDOR, "Apple"),
1326 DMI_MATCH(DMI_PRODUCT_NAME, "MacBookPro6") },
1327 &applesmc_dmi_data[21]},
Henrik Rydberga6660322008-11-06 12:53:21 -08001328 { applesmc_dmi_match, "Apple MacBook Pro 5", {
1329 DMI_MATCH(DMI_BOARD_VENDOR, "Apple"),
1330 DMI_MATCH(DMI_PRODUCT_NAME, "MacBookPro5") },
1331 &applesmc_dmi_data[12]},
Henrik Rydbergd7549902008-10-18 20:27:41 -07001332 { applesmc_dmi_match, "Apple MacBook Pro 4", {
1333 DMI_MATCH(DMI_BOARD_VENDOR, "Apple"),
1334 DMI_MATCH(DMI_PRODUCT_NAME, "MacBookPro4") },
1335 &applesmc_dmi_data[8]},
Henrik Rydberg07e8dbd2008-10-18 20:27:42 -07001336 { applesmc_dmi_match, "Apple MacBook Pro 3", {
1337 DMI_MATCH(DMI_BOARD_VENDOR, "Apple"),
1338 DMI_MATCH(DMI_PRODUCT_NAME, "MacBookPro3") },
1339 &applesmc_dmi_data[9]},
Justin P. Mattocke1741712010-04-14 16:14:10 +02001340 { applesmc_dmi_match, "Apple MacBook Pro 2,2", {
1341 DMI_MATCH(DMI_BOARD_VENDOR, "Apple Computer, Inc."),
1342 DMI_MATCH(DMI_PRODUCT_NAME, "MacBookPro2,2") },
1343 &applesmc_dmi_data[18]},
Nicolas Boichat6f2fad72007-05-08 00:24:52 -07001344 { applesmc_dmi_match, "Apple MacBook Pro", {
1345 DMI_MATCH(DMI_BOARD_VENDOR,"Apple"),
1346 DMI_MATCH(DMI_PRODUCT_NAME,"MacBookPro") },
Andrew Morton7b5e3cb2008-10-18 20:27:41 -07001347 &applesmc_dmi_data[0]},
Guilherme M. Schroederf91a79f2008-08-15 00:40:32 -07001348 { applesmc_dmi_match, "Apple MacBook (v2)", {
Nicolas Boichat6f2fad72007-05-08 00:24:52 -07001349 DMI_MATCH(DMI_BOARD_VENDOR,"Apple"),
Riki Oktariantocd19ba12008-02-04 23:41:58 -08001350 DMI_MATCH(DMI_PRODUCT_NAME,"MacBook2") },
Andrew Morton7b5e3cb2008-10-18 20:27:41 -07001351 &applesmc_dmi_data[1]},
Guilherme M. Schroederf91a79f2008-08-15 00:40:32 -07001352 { applesmc_dmi_match, "Apple MacBook (v3)", {
1353 DMI_MATCH(DMI_BOARD_VENDOR,"Apple"),
1354 DMI_MATCH(DMI_PRODUCT_NAME,"MacBook3") },
Andrew Morton7b5e3cb2008-10-18 20:27:41 -07001355 &applesmc_dmi_data[6]},
Henrik Rydberg468cc032008-11-12 13:24:58 -08001356 { applesmc_dmi_match, "Apple MacBook 4", {
1357 DMI_MATCH(DMI_BOARD_VENDOR, "Apple"),
1358 DMI_MATCH(DMI_PRODUCT_NAME, "MacBook4") },
1359 &applesmc_dmi_data[6]},
Henrik Rydberg181209a2008-11-06 12:53:20 -08001360 { applesmc_dmi_match, "Apple MacBook 5", {
1361 DMI_MATCH(DMI_BOARD_VENDOR, "Apple"),
1362 DMI_MATCH(DMI_PRODUCT_NAME, "MacBook5") },
1363 &applesmc_dmi_data[11]},
Riki Oktariantocd19ba12008-02-04 23:41:58 -08001364 { applesmc_dmi_match, "Apple MacBook", {
1365 DMI_MATCH(DMI_BOARD_VENDOR,"Apple"),
1366 DMI_MATCH(DMI_PRODUCT_NAME,"MacBook") },
Andrew Morton7b5e3cb2008-10-18 20:27:41 -07001367 &applesmc_dmi_data[2]},
Nicolas Boichat6f2fad72007-05-08 00:24:52 -07001368 { applesmc_dmi_match, "Apple Macmini", {
1369 DMI_MATCH(DMI_BOARD_VENDOR,"Apple"),
1370 DMI_MATCH(DMI_PRODUCT_NAME,"Macmini") },
Andrew Morton7b5e3cb2008-10-18 20:27:41 -07001371 &applesmc_dmi_data[3]},
René Rebe8de57702007-10-16 14:19:20 -07001372 { applesmc_dmi_match, "Apple MacPro2", {
1373 DMI_MATCH(DMI_BOARD_VENDOR,"Apple"),
1374 DMI_MATCH(DMI_PRODUCT_NAME,"MacPro2") },
Andrew Morton7b5e3cb2008-10-18 20:27:41 -07001375 &applesmc_dmi_data[4]},
Bharath Rameshfb9f88e2009-01-29 14:25:24 -08001376 { applesmc_dmi_match, "Apple MacPro3", {
1377 DMI_MATCH(DMI_BOARD_VENDOR, "Apple"),
1378 DMI_MATCH(DMI_PRODUCT_NAME, "MacPro3") },
1379 &applesmc_dmi_data[16]},
Henrik Rydberg45a3a362008-11-19 15:36:42 -08001380 { applesmc_dmi_match, "Apple MacPro", {
1381 DMI_MATCH(DMI_BOARD_VENDOR, "Apple"),
1382 DMI_MATCH(DMI_PRODUCT_NAME, "MacPro") },
1383 &applesmc_dmi_data[4]},
Justin P. Mattocke1741712010-04-14 16:14:10 +02001384 { applesmc_dmi_match, "Apple iMac 9,1", {
1385 DMI_MATCH(DMI_BOARD_VENDOR, "Apple Inc."),
1386 DMI_MATCH(DMI_PRODUCT_NAME, "iMac9,1") },
1387 &applesmc_dmi_data[17]},
Henrik Rydbergeefc4882008-11-06 12:53:22 -08001388 { applesmc_dmi_match, "Apple iMac 8", {
1389 DMI_MATCH(DMI_BOARD_VENDOR, "Apple"),
1390 DMI_MATCH(DMI_PRODUCT_NAME, "iMac8") },
1391 &applesmc_dmi_data[13]},
Henrik Rydberg9ca791b2008-11-19 15:36:06 -08001392 { applesmc_dmi_match, "Apple iMac 6", {
1393 DMI_MATCH(DMI_BOARD_VENDOR, "Apple"),
1394 DMI_MATCH(DMI_PRODUCT_NAME, "iMac6") },
1395 &applesmc_dmi_data[14]},
Henrik Rydberg6e3530f2008-11-06 12:53:19 -08001396 { applesmc_dmi_match, "Apple iMac 5", {
1397 DMI_MATCH(DMI_BOARD_VENDOR, "Apple"),
1398 DMI_MATCH(DMI_PRODUCT_NAME, "iMac5") },
1399 &applesmc_dmi_data[10]},
Roberto De Ioris9f86f282008-08-15 00:40:30 -07001400 { applesmc_dmi_match, "Apple iMac", {
1401 DMI_MATCH(DMI_BOARD_VENDOR,"Apple"),
1402 DMI_MATCH(DMI_PRODUCT_NAME,"iMac") },
Andrew Morton7b5e3cb2008-10-18 20:27:41 -07001403 &applesmc_dmi_data[5]},
Nicolas Boichat6f2fad72007-05-08 00:24:52 -07001404 { .ident = NULL }
1405};
1406
1407static int __init applesmc_init(void)
1408{
1409 int ret;
1410 int count;
Nicolas Boichat6f2fad72007-05-08 00:24:52 -07001411
Nicolas Boichat6f2fad72007-05-08 00:24:52 -07001412 if (!dmi_check_system(applesmc_whitelist)) {
Joe Perches1ee7c712010-11-09 15:15:03 +00001413 pr_warn("supported laptop not found!\n");
Nicolas Boichat6f2fad72007-05-08 00:24:52 -07001414 ret = -ENODEV;
1415 goto out;
1416 }
1417
1418 if (!request_region(APPLESMC_DATA_PORT, APPLESMC_NR_PORTS,
1419 "applesmc")) {
1420 ret = -ENXIO;
1421 goto out;
1422 }
1423
1424 ret = platform_driver_register(&applesmc_driver);
1425 if (ret)
1426 goto out_region;
1427
Jean Delvareddfbf2a2007-05-08 20:27:04 -07001428 pdev = platform_device_register_simple("applesmc", APPLESMC_DATA_PORT,
1429 NULL, 0);
Nicolas Boichat6f2fad72007-05-08 00:24:52 -07001430 if (IS_ERR(pdev)) {
1431 ret = PTR_ERR(pdev);
1432 goto out_driver;
1433 }
1434
Henrik Rydberg58745832010-11-10 10:58:03 +00001435 /* create register cache */
1436 ret = applesmc_init_smcreg();
Nicolas Boichat6996abf2007-05-27 22:17:43 +02001437 if (ret)
1438 goto out_device;
Nicolas Boichatfa744192007-05-23 13:58:13 -07001439
Henrik Rydberg58745832010-11-10 10:58:03 +00001440 ret = sysfs_create_file(&pdev->dev.kobj, &dev_attr_name.attr);
1441 if (ret)
1442 goto out_smcreg;
1443
Nicolas Boichat6f2fad72007-05-08 00:24:52 -07001444 /* Create key enumeration sysfs files */
1445 ret = sysfs_create_group(&pdev->dev.kobj, &key_enumeration_group);
1446 if (ret)
Nicolas Boichat6996abf2007-05-27 22:17:43 +02001447 goto out_name;
Nicolas Boichat6f2fad72007-05-08 00:24:52 -07001448
1449 /* create fan files */
1450 count = applesmc_get_fan_count();
Henrik Rydberg0559a532010-05-11 09:17:47 +02001451 if (count < 0)
Joe Perches1ee7c712010-11-09 15:15:03 +00001452 pr_err("Cannot get the number of fans\n");
Henrik Rydberg0559a532010-05-11 09:17:47 +02001453 else
Joe Perches1ee7c712010-11-09 15:15:03 +00001454 pr_info("%d fans found\n", count);
Nicolas Boichat6f2fad72007-05-08 00:24:52 -07001455
Henrik Rydberg0559a532010-05-11 09:17:47 +02001456 if (count > 4) {
1457 count = 4;
Joe Perches1ee7c712010-11-09 15:15:03 +00001458 pr_warn("A maximum of 4 fans are supported by this driver\n");
Henrik Rydberg0559a532010-05-11 09:17:47 +02001459 }
1460
1461 while (fans_handled < count) {
1462 ret = sysfs_create_group(&pdev->dev.kobj,
1463 &fan_attribute_groups[fans_handled]);
1464 if (ret)
1465 goto out_fans;
1466 fans_handled++;
Nicolas Boichat6f2fad72007-05-08 00:24:52 -07001467 }
1468
Henrik Rydberg9792dad2010-11-10 10:58:04 +00001469 ret = applesmc_create_nodes(temp_group, smcreg.temp_count);
1470 if (ret)
1471 goto out_fans;
Nicolas Boichat6f2fad72007-05-08 00:24:52 -07001472
1473 if (applesmc_accelerometer) {
1474 ret = applesmc_create_accelerometer();
1475 if (ret)
1476 goto out_temperature;
1477 }
1478
1479 if (applesmc_light) {
1480 /* Add light sensor file */
1481 ret = sysfs_create_file(&pdev->dev.kobj, &dev_attr_light.attr);
1482 if (ret)
1483 goto out_accelerometer;
1484
1485 /* Create the workqueue */
1486 applesmc_led_wq = create_singlethread_workqueue("applesmc-led");
1487 if (!applesmc_led_wq) {
1488 ret = -ENOMEM;
1489 goto out_light_sysfs;
1490 }
1491
1492 /* register as a led device */
1493 ret = led_classdev_register(&pdev->dev, &applesmc_backlight);
1494 if (ret < 0)
1495 goto out_light_wq;
1496 }
1497
Tony Jones1beeffe2007-08-20 13:46:20 -07001498 hwmon_dev = hwmon_device_register(&pdev->dev);
1499 if (IS_ERR(hwmon_dev)) {
1500 ret = PTR_ERR(hwmon_dev);
Nicolas Boichat6f2fad72007-05-08 00:24:52 -07001501 goto out_light_ledclass;
1502 }
1503
Joe Perches1ee7c712010-11-09 15:15:03 +00001504 pr_info("driver successfully loaded\n");
Nicolas Boichat6f2fad72007-05-08 00:24:52 -07001505
1506 return 0;
1507
1508out_light_ledclass:
1509 if (applesmc_light)
1510 led_classdev_unregister(&applesmc_backlight);
1511out_light_wq:
1512 if (applesmc_light)
1513 destroy_workqueue(applesmc_led_wq);
1514out_light_sysfs:
1515 if (applesmc_light)
1516 sysfs_remove_file(&pdev->dev.kobj, &dev_attr_light.attr);
1517out_accelerometer:
1518 if (applesmc_accelerometer)
1519 applesmc_release_accelerometer();
1520out_temperature:
Henrik Rydberg9792dad2010-11-10 10:58:04 +00001521 applesmc_destroy_nodes(temp_group);
Henrik Rydberg0559a532010-05-11 09:17:47 +02001522out_fans:
1523 while (fans_handled)
1524 sysfs_remove_group(&pdev->dev.kobj,
1525 &fan_attribute_groups[--fans_handled]);
Nicolas Boichat6f2fad72007-05-08 00:24:52 -07001526 sysfs_remove_group(&pdev->dev.kobj, &key_enumeration_group);
Nicolas Boichat6996abf2007-05-27 22:17:43 +02001527out_name:
1528 sysfs_remove_file(&pdev->dev.kobj, &dev_attr_name.attr);
Henrik Rydberg58745832010-11-10 10:58:03 +00001529out_smcreg:
1530 applesmc_destroy_smcreg();
Nicolas Boichat6f2fad72007-05-08 00:24:52 -07001531out_device:
1532 platform_device_unregister(pdev);
1533out_driver:
1534 platform_driver_unregister(&applesmc_driver);
1535out_region:
1536 release_region(APPLESMC_DATA_PORT, APPLESMC_NR_PORTS);
1537out:
Joe Perches1ee7c712010-11-09 15:15:03 +00001538 pr_warn("driver init failed (ret=%d)!\n", ret);
Nicolas Boichat6f2fad72007-05-08 00:24:52 -07001539 return ret;
1540}
1541
1542static void __exit applesmc_exit(void)
1543{
Tony Jones1beeffe2007-08-20 13:46:20 -07001544 hwmon_device_unregister(hwmon_dev);
Nicolas Boichat6f2fad72007-05-08 00:24:52 -07001545 if (applesmc_light) {
1546 led_classdev_unregister(&applesmc_backlight);
1547 destroy_workqueue(applesmc_led_wq);
1548 sysfs_remove_file(&pdev->dev.kobj, &dev_attr_light.attr);
1549 }
1550 if (applesmc_accelerometer)
1551 applesmc_release_accelerometer();
Henrik Rydberg9792dad2010-11-10 10:58:04 +00001552 applesmc_destroy_nodes(temp_group);
Henrik Rydberg0559a532010-05-11 09:17:47 +02001553 while (fans_handled)
1554 sysfs_remove_group(&pdev->dev.kobj,
1555 &fan_attribute_groups[--fans_handled]);
Nicolas Boichat6f2fad72007-05-08 00:24:52 -07001556 sysfs_remove_group(&pdev->dev.kobj, &key_enumeration_group);
Nicolas Boichat6996abf2007-05-27 22:17:43 +02001557 sysfs_remove_file(&pdev->dev.kobj, &dev_attr_name.attr);
Henrik Rydberg58745832010-11-10 10:58:03 +00001558 applesmc_destroy_smcreg();
Nicolas Boichat6f2fad72007-05-08 00:24:52 -07001559 platform_device_unregister(pdev);
1560 platform_driver_unregister(&applesmc_driver);
1561 release_region(APPLESMC_DATA_PORT, APPLESMC_NR_PORTS);
1562
Joe Perches1ee7c712010-11-09 15:15:03 +00001563 pr_info("driver unloaded\n");
Nicolas Boichat6f2fad72007-05-08 00:24:52 -07001564}
1565
1566module_init(applesmc_init);
1567module_exit(applesmc_exit);
1568
1569MODULE_AUTHOR("Nicolas Boichat");
1570MODULE_DESCRIPTION("Apple SMC");
1571MODULE_LICENSE("GPL v2");
Henrik Rydbergdc924ef2008-12-01 13:13:49 -08001572MODULE_DEVICE_TABLE(dmi, applesmc_whitelist);