blob: 1d7f8aff998222e3f2bbdf09ab9b9226df959cfc [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);
Henrik Rydbergdcdea262010-11-09 15:15:06 +0000737 if (entry->len > 2)
738 return -EINVAL;
Henrik Rydberg9792dad2010-11-10 10:58:04 +0000739
Henrik Rydbergdcdea262010-11-09 15:15:06 +0000740 ret = applesmc_read_entry(entry, buffer, entry->len);
Henrik Rydberg9792dad2010-11-10 10:58:04 +0000741 if (ret)
742 return ret;
743
Henrik Rydbergdcdea262010-11-09 15:15:06 +0000744 if (entry->len == 2) {
745 temp = buffer[0] * 1000;
746 temp += (buffer[1] >> 6) * 250;
747 } else {
748 temp = buffer[0] * 4000;
749 }
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700750
Henrik Rydberg9792dad2010-11-10 10:58:04 +0000751 return snprintf(sysfsbuf, PAGE_SIZE, "%u\n", temp);
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700752}
753
754static ssize_t applesmc_show_fan_speed(struct device *dev,
755 struct device_attribute *attr, char *sysfsbuf)
756{
757 int ret;
758 unsigned int speed = 0;
759 char newkey[5];
760 u8 buffer[2];
761 struct sensor_device_attribute_2 *sensor_attr =
762 to_sensor_dev_attr_2(attr);
763
764 newkey[0] = fan_speed_keys[sensor_attr->nr][0];
765 newkey[1] = '0' + sensor_attr->index;
766 newkey[2] = fan_speed_keys[sensor_attr->nr][2];
767 newkey[3] = fan_speed_keys[sensor_attr->nr][3];
768 newkey[4] = 0;
769
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700770 ret = applesmc_read_key(newkey, buffer, 2);
771 speed = ((buffer[0] << 8 | buffer[1]) >> 2);
772
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700773 if (ret)
774 return ret;
775 else
776 return snprintf(sysfsbuf, PAGE_SIZE, "%u\n", speed);
777}
778
779static ssize_t applesmc_store_fan_speed(struct device *dev,
780 struct device_attribute *attr,
781 const char *sysfsbuf, size_t count)
782{
783 int ret;
784 u32 speed;
785 char newkey[5];
786 u8 buffer[2];
787 struct sensor_device_attribute_2 *sensor_attr =
788 to_sensor_dev_attr_2(attr);
789
790 speed = simple_strtoul(sysfsbuf, NULL, 10);
791
792 if (speed > 0x4000) /* Bigger than a 14-bit value */
793 return -EINVAL;
794
795 newkey[0] = fan_speed_keys[sensor_attr->nr][0];
796 newkey[1] = '0' + sensor_attr->index;
797 newkey[2] = fan_speed_keys[sensor_attr->nr][2];
798 newkey[3] = fan_speed_keys[sensor_attr->nr][3];
799 newkey[4] = 0;
800
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700801 buffer[0] = (speed >> 6) & 0xff;
802 buffer[1] = (speed << 2) & 0xff;
803 ret = applesmc_write_key(newkey, buffer, 2);
804
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700805 if (ret)
806 return ret;
807 else
808 return count;
809}
810
811static ssize_t applesmc_show_fan_manual(struct device *dev,
812 struct device_attribute *devattr, char *sysfsbuf)
813{
814 int ret;
815 u16 manual = 0;
816 u8 buffer[2];
817 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
818
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700819 ret = applesmc_read_key(FANS_MANUAL, buffer, 2);
820 manual = ((buffer[0] << 8 | buffer[1]) >> attr->index) & 0x01;
821
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700822 if (ret)
823 return ret;
824 else
825 return snprintf(sysfsbuf, PAGE_SIZE, "%d\n", manual);
826}
827
828static ssize_t applesmc_store_fan_manual(struct device *dev,
829 struct device_attribute *devattr,
830 const char *sysfsbuf, size_t count)
831{
832 int ret;
833 u8 buffer[2];
834 u32 input;
835 u16 val;
836 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
837
838 input = simple_strtoul(sysfsbuf, NULL, 10);
839
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700840 ret = applesmc_read_key(FANS_MANUAL, buffer, 2);
841 val = (buffer[0] << 8 | buffer[1]);
842 if (ret)
843 goto out;
844
845 if (input)
846 val = val | (0x01 << attr->index);
847 else
848 val = val & ~(0x01 << attr->index);
849
850 buffer[0] = (val >> 8) & 0xFF;
851 buffer[1] = val & 0xFF;
852
853 ret = applesmc_write_key(FANS_MANUAL, buffer, 2);
854
855out:
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700856 if (ret)
857 return ret;
858 else
859 return count;
860}
861
862static ssize_t applesmc_show_fan_position(struct device *dev,
863 struct device_attribute *attr, char *sysfsbuf)
864{
865 int ret;
866 char newkey[5];
867 u8 buffer[17];
868 struct sensor_device_attribute_2 *sensor_attr =
869 to_sensor_dev_attr_2(attr);
870
871 newkey[0] = FAN_POSITION[0];
872 newkey[1] = '0' + sensor_attr->index;
873 newkey[2] = FAN_POSITION[2];
874 newkey[3] = FAN_POSITION[3];
875 newkey[4] = 0;
876
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700877 ret = applesmc_read_key(newkey, buffer, 16);
878 buffer[16] = 0;
879
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700880 if (ret)
881 return ret;
882 else
883 return snprintf(sysfsbuf, PAGE_SIZE, "%s\n", buffer+4);
884}
885
886static ssize_t applesmc_calibrate_show(struct device *dev,
887 struct device_attribute *attr, char *sysfsbuf)
888{
889 return snprintf(sysfsbuf, PAGE_SIZE, "(%d,%d)\n", rest_x, rest_y);
890}
891
892static ssize_t applesmc_calibrate_store(struct device *dev,
893 struct device_attribute *attr, const char *sysfsbuf, size_t count)
894{
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700895 applesmc_calibrate();
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700896
897 return count;
898}
899
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700900static void applesmc_backlight_set(struct work_struct *work)
901{
Henrik Rydberga976f152009-09-21 17:04:50 -0700902 applesmc_write_key(BACKLIGHT_KEY, backlight_state, 2);
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700903}
904static DECLARE_WORK(backlight_work, &applesmc_backlight_set);
905
906static void applesmc_brightness_set(struct led_classdev *led_cdev,
907 enum led_brightness value)
908{
909 int ret;
910
Henrik Rydberga976f152009-09-21 17:04:50 -0700911 backlight_state[0] = value;
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700912 ret = queue_work(applesmc_led_wq, &backlight_work);
913
914 if (debug && (!ret))
915 printk(KERN_DEBUG "applesmc: work was already on the queue.\n");
916}
917
918static ssize_t applesmc_key_count_show(struct device *dev,
919 struct device_attribute *attr, char *sysfsbuf)
920{
921 int ret;
922 u8 buffer[4];
923 u32 count;
924
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700925 ret = applesmc_read_key(KEY_COUNT_KEY, buffer, 4);
926 count = ((u32)buffer[0]<<24) + ((u32)buffer[1]<<16) +
927 ((u32)buffer[2]<<8) + buffer[3];
928
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700929 if (ret)
930 return ret;
931 else
932 return snprintf(sysfsbuf, PAGE_SIZE, "%d\n", count);
933}
934
935static ssize_t applesmc_key_at_index_read_show(struct device *dev,
936 struct device_attribute *attr, char *sysfsbuf)
937{
Henrik Rydberg58745832010-11-10 10:58:03 +0000938 const struct applesmc_entry *entry;
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700939 int ret;
940
Henrik Rydberg58745832010-11-10 10:58:03 +0000941 entry = applesmc_get_entry_by_index(key_at_index);
942 if (IS_ERR(entry))
943 return PTR_ERR(entry);
944 ret = applesmc_read_entry(entry, sysfsbuf, entry->len);
945 if (ret)
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700946 return ret;
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700947
Henrik Rydberg58745832010-11-10 10:58:03 +0000948 return entry->len;
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700949}
950
951static ssize_t applesmc_key_at_index_data_length_show(struct device *dev,
952 struct device_attribute *attr, char *sysfsbuf)
953{
Henrik Rydberg58745832010-11-10 10:58:03 +0000954 const struct applesmc_entry *entry;
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700955
Henrik Rydberg58745832010-11-10 10:58:03 +0000956 entry = applesmc_get_entry_by_index(key_at_index);
957 if (IS_ERR(entry))
958 return PTR_ERR(entry);
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700959
Henrik Rydberg58745832010-11-10 10:58:03 +0000960 return snprintf(sysfsbuf, PAGE_SIZE, "%d\n", entry->len);
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700961}
962
963static ssize_t applesmc_key_at_index_type_show(struct device *dev,
964 struct device_attribute *attr, char *sysfsbuf)
965{
Henrik Rydberg58745832010-11-10 10:58:03 +0000966 const struct applesmc_entry *entry;
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700967
Henrik Rydberg58745832010-11-10 10:58:03 +0000968 entry = applesmc_get_entry_by_index(key_at_index);
969 if (IS_ERR(entry))
970 return PTR_ERR(entry);
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700971
Henrik Rydberg58745832010-11-10 10:58:03 +0000972 return snprintf(sysfsbuf, PAGE_SIZE, "%s\n", entry->type);
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700973}
974
975static ssize_t applesmc_key_at_index_name_show(struct device *dev,
976 struct device_attribute *attr, char *sysfsbuf)
977{
Henrik Rydberg58745832010-11-10 10:58:03 +0000978 const struct applesmc_entry *entry;
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700979
Henrik Rydberg58745832010-11-10 10:58:03 +0000980 entry = applesmc_get_entry_by_index(key_at_index);
981 if (IS_ERR(entry))
982 return PTR_ERR(entry);
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700983
Henrik Rydberg58745832010-11-10 10:58:03 +0000984 return snprintf(sysfsbuf, PAGE_SIZE, "%s\n", entry->key);
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700985}
986
987static ssize_t applesmc_key_at_index_show(struct device *dev,
988 struct device_attribute *attr, char *sysfsbuf)
989{
990 return snprintf(sysfsbuf, PAGE_SIZE, "%d\n", key_at_index);
991}
992
993static ssize_t applesmc_key_at_index_store(struct device *dev,
994 struct device_attribute *attr, const char *sysfsbuf, size_t count)
995{
Henrik Rydberg58745832010-11-10 10:58:03 +0000996 unsigned long newkey;
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700997
Henrik Rydberg58745832010-11-10 10:58:03 +0000998 if (strict_strtoul(sysfsbuf, 10, &newkey) < 0
999 || newkey >= smcreg.key_count)
1000 return -EINVAL;
Nicolas Boichat6f2fad72007-05-08 00:24:52 -07001001
Henrik Rydberg58745832010-11-10 10:58:03 +00001002 key_at_index = newkey;
Nicolas Boichat6f2fad72007-05-08 00:24:52 -07001003 return count;
1004}
1005
1006static struct led_classdev applesmc_backlight = {
Richard Purdie6c152be2007-10-31 15:00:07 +01001007 .name = "smc::kbd_backlight",
Nicolas Boichat6f2fad72007-05-08 00:24:52 -07001008 .default_trigger = "nand-disk",
1009 .brightness_set = applesmc_brightness_set,
1010};
1011
Nicolas Boichatfa744192007-05-23 13:58:13 -07001012static DEVICE_ATTR(name, 0444, applesmc_name_show, NULL);
1013
Nicolas Boichat6f2fad72007-05-08 00:24:52 -07001014static DEVICE_ATTR(position, 0444, applesmc_position_show, NULL);
1015static DEVICE_ATTR(calibrate, 0644,
1016 applesmc_calibrate_show, applesmc_calibrate_store);
1017
1018static struct attribute *accelerometer_attributes[] = {
1019 &dev_attr_position.attr,
1020 &dev_attr_calibrate.attr,
1021 NULL
1022};
1023
1024static const struct attribute_group accelerometer_attributes_group =
1025 { .attrs = accelerometer_attributes };
1026
1027static DEVICE_ATTR(light, 0444, applesmc_light_show, NULL);
1028
1029static DEVICE_ATTR(key_count, 0444, applesmc_key_count_show, NULL);
1030static DEVICE_ATTR(key_at_index, 0644,
1031 applesmc_key_at_index_show, applesmc_key_at_index_store);
1032static DEVICE_ATTR(key_at_index_name, 0444,
1033 applesmc_key_at_index_name_show, NULL);
1034static DEVICE_ATTR(key_at_index_type, 0444,
1035 applesmc_key_at_index_type_show, NULL);
1036static DEVICE_ATTR(key_at_index_data_length, 0444,
1037 applesmc_key_at_index_data_length_show, NULL);
1038static DEVICE_ATTR(key_at_index_data, 0444,
1039 applesmc_key_at_index_read_show, NULL);
1040
1041static struct attribute *key_enumeration_attributes[] = {
1042 &dev_attr_key_count.attr,
1043 &dev_attr_key_at_index.attr,
1044 &dev_attr_key_at_index_name.attr,
1045 &dev_attr_key_at_index_type.attr,
1046 &dev_attr_key_at_index_data_length.attr,
1047 &dev_attr_key_at_index_data.attr,
1048 NULL
1049};
1050
1051static const struct attribute_group key_enumeration_group =
1052 { .attrs = key_enumeration_attributes };
1053
1054/*
1055 * Macro defining SENSOR_DEVICE_ATTR for a fan sysfs entries.
1056 * - show actual speed
1057 * - show/store minimum speed
1058 * - show maximum speed
1059 * - show safe speed
1060 * - show/store target speed
1061 * - show/store manual mode
1062 */
1063#define sysfs_fan_speeds_offset(offset) \
1064static SENSOR_DEVICE_ATTR_2(fan##offset##_input, S_IRUGO, \
1065 applesmc_show_fan_speed, NULL, 0, offset-1); \
1066\
1067static SENSOR_DEVICE_ATTR_2(fan##offset##_min, S_IRUGO | S_IWUSR, \
1068 applesmc_show_fan_speed, applesmc_store_fan_speed, 1, offset-1); \
1069\
1070static SENSOR_DEVICE_ATTR_2(fan##offset##_max, S_IRUGO, \
1071 applesmc_show_fan_speed, NULL, 2, offset-1); \
1072\
1073static SENSOR_DEVICE_ATTR_2(fan##offset##_safe, S_IRUGO, \
1074 applesmc_show_fan_speed, NULL, 3, offset-1); \
1075\
1076static SENSOR_DEVICE_ATTR_2(fan##offset##_output, S_IRUGO | S_IWUSR, \
1077 applesmc_show_fan_speed, applesmc_store_fan_speed, 4, offset-1); \
1078\
1079static SENSOR_DEVICE_ATTR(fan##offset##_manual, S_IRUGO | S_IWUSR, \
1080 applesmc_show_fan_manual, applesmc_store_fan_manual, offset-1); \
1081\
Jean Delvareda4e8ca2007-05-08 20:27:05 -07001082static SENSOR_DEVICE_ATTR(fan##offset##_label, S_IRUGO, \
Nicolas Boichat6f2fad72007-05-08 00:24:52 -07001083 applesmc_show_fan_position, NULL, offset-1); \
1084\
1085static struct attribute *fan##offset##_attributes[] = { \
1086 &sensor_dev_attr_fan##offset##_input.dev_attr.attr, \
1087 &sensor_dev_attr_fan##offset##_min.dev_attr.attr, \
1088 &sensor_dev_attr_fan##offset##_max.dev_attr.attr, \
1089 &sensor_dev_attr_fan##offset##_safe.dev_attr.attr, \
1090 &sensor_dev_attr_fan##offset##_output.dev_attr.attr, \
1091 &sensor_dev_attr_fan##offset##_manual.dev_attr.attr, \
Jean Delvareda4e8ca2007-05-08 20:27:05 -07001092 &sensor_dev_attr_fan##offset##_label.dev_attr.attr, \
Nicolas Boichat6f2fad72007-05-08 00:24:52 -07001093 NULL \
1094};
1095
1096/*
1097 * Create the needed functions for each fan using the macro defined above
René Rebe8de57702007-10-16 14:19:20 -07001098 * (4 fans are supported)
Nicolas Boichat6f2fad72007-05-08 00:24:52 -07001099 */
1100sysfs_fan_speeds_offset(1);
1101sysfs_fan_speeds_offset(2);
René Rebe8de57702007-10-16 14:19:20 -07001102sysfs_fan_speeds_offset(3);
1103sysfs_fan_speeds_offset(4);
Nicolas Boichat6f2fad72007-05-08 00:24:52 -07001104
1105static const struct attribute_group fan_attribute_groups[] = {
1106 { .attrs = fan1_attributes },
René Rebe8de57702007-10-16 14:19:20 -07001107 { .attrs = fan2_attributes },
1108 { .attrs = fan3_attributes },
1109 { .attrs = fan4_attributes },
Nicolas Boichat6f2fad72007-05-08 00:24:52 -07001110};
1111
Henrik Rydberg9792dad2010-11-10 10:58:04 +00001112static struct applesmc_node_group temp_group[] = {
1113 { "temp%d_label", applesmc_show_sensor_label },
1114 { "temp%d_input", applesmc_show_temperature },
1115 { }
Alex Murrayfa5575c2010-05-27 19:58:54 +02001116};
1117
Nicolas Boichat6f2fad72007-05-08 00:24:52 -07001118/* Module stuff */
1119
1120/*
1121 * applesmc_dmi_match - found a match. return one, short-circuiting the hunt.
1122 */
Jeff Garzik18552562007-10-03 15:15:40 -04001123static int applesmc_dmi_match(const struct dmi_system_id *id)
Nicolas Boichat6f2fad72007-05-08 00:24:52 -07001124{
Nicolas Boichat6f2fad72007-05-08 00:24:52 -07001125 struct dmi_match_data* dmi_data = id->driver_data;
Joe Perches1ee7c712010-11-09 15:15:03 +00001126 pr_info("%s detected:\n", id->ident);
Nicolas Boichat6f2fad72007-05-08 00:24:52 -07001127 applesmc_accelerometer = dmi_data->accelerometer;
Joe Perches1ee7c712010-11-09 15:15:03 +00001128 pr_info(" - Model %s accelerometer\n",
1129 applesmc_accelerometer ? "with" : "without");
Nicolas Boichat6f2fad72007-05-08 00:24:52 -07001130 applesmc_light = dmi_data->light;
Joe Perches1ee7c712010-11-09 15:15:03 +00001131 pr_info(" - Model %s light sensors and backlight\n",
1132 applesmc_light ? "with" : "without");
Nicolas Boichat6f2fad72007-05-08 00:24:52 -07001133
Nicolas Boichat6f2fad72007-05-08 00:24:52 -07001134 return 1;
1135}
1136
Henrik Rydberg9792dad2010-11-10 10:58:04 +00001137/*
1138 * applesmc_destroy_nodes - remove files and free associated memory
1139 */
1140static void applesmc_destroy_nodes(struct applesmc_node_group *groups)
1141{
1142 struct applesmc_node_group *grp;
1143 struct applesmc_dev_attr *node;
1144
1145 for (grp = groups; grp->nodes; grp++) {
1146 for (node = grp->nodes; node->sda.dev_attr.attr.name; node++)
1147 sysfs_remove_file(&pdev->dev.kobj,
1148 &node->sda.dev_attr.attr);
1149 kfree(grp->nodes);
1150 grp->nodes = NULL;
1151 }
1152}
1153
1154/*
1155 * applesmc_create_nodes - create a two-dimensional group of sysfs files
1156 */
1157static int applesmc_create_nodes(struct applesmc_node_group *groups, int num)
1158{
1159 struct applesmc_node_group *grp;
1160 struct applesmc_dev_attr *node;
1161 struct attribute *attr;
1162 int ret, i;
1163
1164 for (grp = groups; grp->format; grp++) {
1165 grp->nodes = kcalloc(num + 1, sizeof(*node), GFP_KERNEL);
1166 if (!grp->nodes) {
1167 ret = -ENOMEM;
1168 goto out;
1169 }
1170 for (i = 0; i < num; i++) {
1171 node = &grp->nodes[i];
1172 sprintf(node->name, grp->format, i + 1);
1173 node->sda.index = i;
1174 node->sda.dev_attr.show = grp->show;
1175 node->sda.dev_attr.store = grp->store;
1176 attr = &node->sda.dev_attr.attr;
1177 attr->name = node->name;
1178 attr->mode = S_IRUGO | (grp->store ? S_IWUSR : 0);
1179 ret = sysfs_create_file(&pdev->dev.kobj, attr);
1180 if (ret) {
1181 attr->name = NULL;
1182 goto out;
1183 }
1184 }
1185 }
1186
1187 return 0;
1188out:
1189 applesmc_destroy_nodes(groups);
1190 return ret;
1191}
1192
Nicolas Boichat6f2fad72007-05-08 00:24:52 -07001193/* Create accelerometer ressources */
1194static int applesmc_create_accelerometer(void)
1195{
Dmitry Torokhovd5cf2b92007-09-26 00:01:35 -04001196 struct input_dev *idev;
Nicolas Boichat6f2fad72007-05-08 00:24:52 -07001197 int ret;
1198
1199 ret = sysfs_create_group(&pdev->dev.kobj,
1200 &accelerometer_attributes_group);
1201 if (ret)
1202 goto out;
1203
Dmitry Torokhovd5cf2b92007-09-26 00:01:35 -04001204 applesmc_idev = input_allocate_polled_device();
Nicolas Boichat6f2fad72007-05-08 00:24:52 -07001205 if (!applesmc_idev) {
1206 ret = -ENOMEM;
1207 goto out_sysfs;
1208 }
1209
Dmitry Torokhovd5cf2b92007-09-26 00:01:35 -04001210 applesmc_idev->poll = applesmc_idev_poll;
1211 applesmc_idev->poll_interval = APPLESMC_POLL_INTERVAL;
1212
Nicolas Boichat6f2fad72007-05-08 00:24:52 -07001213 /* initial calibrate for the input device */
1214 applesmc_calibrate();
1215
Dmitry Torokhovd5cf2b92007-09-26 00:01:35 -04001216 /* initialize the input device */
1217 idev = applesmc_idev->input;
1218 idev->name = "applesmc";
1219 idev->id.bustype = BUS_HOST;
1220 idev->dev.parent = &pdev->dev;
Jiri Slaby7b19ada2007-10-18 23:40:32 -07001221 idev->evbit[0] = BIT_MASK(EV_ABS);
Dmitry Torokhovd5cf2b92007-09-26 00:01:35 -04001222 input_set_abs_params(idev, ABS_X,
Nicolas Boichat6f2fad72007-05-08 00:24:52 -07001223 -256, 256, APPLESMC_INPUT_FUZZ, APPLESMC_INPUT_FLAT);
Dmitry Torokhovd5cf2b92007-09-26 00:01:35 -04001224 input_set_abs_params(idev, ABS_Y,
Nicolas Boichat6f2fad72007-05-08 00:24:52 -07001225 -256, 256, APPLESMC_INPUT_FUZZ, APPLESMC_INPUT_FLAT);
1226
Dmitry Torokhovd5cf2b92007-09-26 00:01:35 -04001227 ret = input_register_polled_device(applesmc_idev);
Nicolas Boichat6f2fad72007-05-08 00:24:52 -07001228 if (ret)
1229 goto out_idev;
1230
Nicolas Boichat6f2fad72007-05-08 00:24:52 -07001231 return 0;
1232
1233out_idev:
Dmitry Torokhovd5cf2b92007-09-26 00:01:35 -04001234 input_free_polled_device(applesmc_idev);
Nicolas Boichat6f2fad72007-05-08 00:24:52 -07001235
1236out_sysfs:
1237 sysfs_remove_group(&pdev->dev.kobj, &accelerometer_attributes_group);
1238
1239out:
Joe Perches1ee7c712010-11-09 15:15:03 +00001240 pr_warn("driver init failed (ret=%d)!\n", ret);
Nicolas Boichat6f2fad72007-05-08 00:24:52 -07001241 return ret;
1242}
1243
1244/* Release all ressources used by the accelerometer */
1245static void applesmc_release_accelerometer(void)
1246{
Dmitry Torokhovd5cf2b92007-09-26 00:01:35 -04001247 input_unregister_polled_device(applesmc_idev);
1248 input_free_polled_device(applesmc_idev);
Nicolas Boichat6f2fad72007-05-08 00:24:52 -07001249 sysfs_remove_group(&pdev->dev.kobj, &accelerometer_attributes_group);
1250}
1251
1252static __initdata struct dmi_match_data applesmc_dmi_data[] = {
1253/* MacBook Pro: accelerometer, backlight and temperature set 0 */
1254 { .accelerometer = 1, .light = 1, .temperature_set = 0 },
Riki Oktariantocd19ba12008-02-04 23:41:58 -08001255/* MacBook2: accelerometer and temperature set 1 */
Martin Szulecki1bed24b2007-07-09 11:41:36 -07001256 { .accelerometer = 1, .light = 0, .temperature_set = 1 },
Riki Oktariantocd19ba12008-02-04 23:41:58 -08001257/* MacBook: accelerometer and temperature set 2 */
1258 { .accelerometer = 1, .light = 0, .temperature_set = 2 },
1259/* MacMini: temperature set 3 */
René Rebe8de57702007-10-16 14:19:20 -07001260 { .accelerometer = 0, .light = 0, .temperature_set = 3 },
Riki Oktariantocd19ba12008-02-04 23:41:58 -08001261/* MacPro: temperature set 4 */
1262 { .accelerometer = 0, .light = 0, .temperature_set = 4 },
Roberto De Ioris9f86f282008-08-15 00:40:30 -07001263/* iMac: temperature set 5 */
1264 { .accelerometer = 0, .light = 0, .temperature_set = 5 },
Henrik Rydberg468cc032008-11-12 13:24:58 -08001265/* MacBook3, MacBook4: accelerometer and temperature set 6 */
Guilherme M. Schroederf91a79f2008-08-15 00:40:32 -07001266 { .accelerometer = 1, .light = 0, .temperature_set = 6 },
Henrik Rydbergf5274c92008-10-18 20:27:40 -07001267/* MacBook Air: accelerometer, backlight and temperature set 7 */
1268 { .accelerometer = 1, .light = 1, .temperature_set = 7 },
Henrik Rydbergd7549902008-10-18 20:27:41 -07001269/* MacBook Pro 4: accelerometer, backlight and temperature set 8 */
1270 { .accelerometer = 1, .light = 1, .temperature_set = 8 },
Henrik Rydberg07e8dbd2008-10-18 20:27:42 -07001271/* MacBook Pro 3: accelerometer, backlight and temperature set 9 */
1272 { .accelerometer = 1, .light = 1, .temperature_set = 9 },
Henrik Rydberg6e3530f2008-11-06 12:53:19 -08001273/* iMac 5: light sensor only, temperature set 10 */
1274 { .accelerometer = 0, .light = 0, .temperature_set = 10 },
Henrik Rydberg181209a12008-11-06 12:53:20 -08001275/* MacBook 5: accelerometer, backlight and temperature set 11 */
1276 { .accelerometer = 1, .light = 1, .temperature_set = 11 },
Henrik Rydberga6660322008-11-06 12:53:21 -08001277/* MacBook Pro 5: accelerometer, backlight and temperature set 12 */
1278 { .accelerometer = 1, .light = 1, .temperature_set = 12 },
Henrik Rydbergeefc4882008-11-06 12:53:22 -08001279/* iMac 8: light sensor only, temperature set 13 */
1280 { .accelerometer = 0, .light = 0, .temperature_set = 13 },
Henrik Rydberg9ca791b2008-11-19 15:36:06 -08001281/* iMac 6: light sensor only, temperature set 14 */
1282 { .accelerometer = 0, .light = 0, .temperature_set = 14 },
Henrik Rydberg85e0e5a2009-01-06 14:41:36 -08001283/* MacBook Air 2,1: accelerometer, backlight and temperature set 15 */
1284 { .accelerometer = 1, .light = 1, .temperature_set = 15 },
Bharath Rameshfb9f88e2009-01-29 14:25:24 -08001285/* MacPro3,1: temperature set 16 */
1286 { .accelerometer = 0, .light = 0, .temperature_set = 16 },
Justin P. Mattocke1741712010-04-14 16:14:10 +02001287/* iMac 9,1: light sensor only, temperature set 17 */
1288 { .accelerometer = 0, .light = 0, .temperature_set = 17 },
1289/* MacBook Pro 2,2: accelerometer, backlight and temperature set 18 */
1290 { .accelerometer = 1, .light = 1, .temperature_set = 18 },
Henrik Rydberg4e4a99d2010-05-27 19:58:50 +02001291/* MacBook Pro 5,3: accelerometer, backlight and temperature set 19 */
1292 { .accelerometer = 1, .light = 1, .temperature_set = 19 },
1293/* MacBook Pro 5,4: accelerometer, backlight and temperature set 20 */
1294 { .accelerometer = 1, .light = 1, .temperature_set = 20 },
Bernhard Froemel872bad52010-05-27 19:58:52 +02001295/* MacBook Pro 6,2: accelerometer, backlight and temperature set 21 */
1296 { .accelerometer = 1, .light = 1, .temperature_set = 21 },
Henrik Rydberg405eaa12010-05-27 19:58:53 +02001297/* MacBook Pro 7,1: accelerometer, backlight and temperature set 22 */
1298 { .accelerometer = 1, .light = 1, .temperature_set = 22 },
Edgar Hucek132af032010-11-09 15:15:01 +00001299/* MacBook Air 3,1: accelerometer, backlight and temperature set 23 */
1300 { .accelerometer = 0, .light = 0, .temperature_set = 23 },
Nicolas Boichat6f2fad72007-05-08 00:24:52 -07001301};
1302
1303/* Note that DMI_MATCH(...,"MacBook") will match "MacBookPro1,1".
1304 * So we need to put "Apple MacBook Pro" before "Apple MacBook". */
1305static __initdata struct dmi_system_id applesmc_whitelist[] = {
Edgar Hucek132af032010-11-09 15:15:01 +00001306 { applesmc_dmi_match, "Apple MacBook Air 3", {
1307 DMI_MATCH(DMI_BOARD_VENDOR, "Apple"),
1308 DMI_MATCH(DMI_PRODUCT_NAME, "MacBookAir3") },
1309 &applesmc_dmi_data[23]},
Henrik Rydberg85e0e5a2009-01-06 14:41:36 -08001310 { applesmc_dmi_match, "Apple MacBook Air 2", {
1311 DMI_MATCH(DMI_BOARD_VENDOR, "Apple"),
1312 DMI_MATCH(DMI_PRODUCT_NAME, "MacBookAir2") },
1313 &applesmc_dmi_data[15]},
Henrik Rydbergf5274c92008-10-18 20:27:40 -07001314 { applesmc_dmi_match, "Apple MacBook Air", {
1315 DMI_MATCH(DMI_BOARD_VENDOR, "Apple"),
1316 DMI_MATCH(DMI_PRODUCT_NAME, "MacBookAir") },
Andrew Morton7b5e3cb2008-10-18 20:27:41 -07001317 &applesmc_dmi_data[7]},
Henrik Rydberg405eaa12010-05-27 19:58:53 +02001318 { applesmc_dmi_match, "Apple MacBook Pro 7", {
1319 DMI_MATCH(DMI_BOARD_VENDOR, "Apple"),
1320 DMI_MATCH(DMI_PRODUCT_NAME, "MacBookPro7") },
1321 &applesmc_dmi_data[22]},
Henrik Rydberg4e4a99d2010-05-27 19:58:50 +02001322 { applesmc_dmi_match, "Apple MacBook Pro 5,4", {
1323 DMI_MATCH(DMI_BOARD_VENDOR, "Apple"),
1324 DMI_MATCH(DMI_PRODUCT_NAME, "MacBookPro5,4") },
1325 &applesmc_dmi_data[20]},
1326 { applesmc_dmi_match, "Apple MacBook Pro 5,3", {
1327 DMI_MATCH(DMI_BOARD_VENDOR, "Apple"),
1328 DMI_MATCH(DMI_PRODUCT_NAME, "MacBookPro5,3") },
1329 &applesmc_dmi_data[19]},
Bernhard Froemel872bad52010-05-27 19:58:52 +02001330 { applesmc_dmi_match, "Apple MacBook Pro 6", {
1331 DMI_MATCH(DMI_BOARD_VENDOR, "Apple"),
1332 DMI_MATCH(DMI_PRODUCT_NAME, "MacBookPro6") },
1333 &applesmc_dmi_data[21]},
Henrik Rydberga6660322008-11-06 12:53:21 -08001334 { applesmc_dmi_match, "Apple MacBook Pro 5", {
1335 DMI_MATCH(DMI_BOARD_VENDOR, "Apple"),
1336 DMI_MATCH(DMI_PRODUCT_NAME, "MacBookPro5") },
1337 &applesmc_dmi_data[12]},
Henrik Rydbergd7549902008-10-18 20:27:41 -07001338 { applesmc_dmi_match, "Apple MacBook Pro 4", {
1339 DMI_MATCH(DMI_BOARD_VENDOR, "Apple"),
1340 DMI_MATCH(DMI_PRODUCT_NAME, "MacBookPro4") },
1341 &applesmc_dmi_data[8]},
Henrik Rydberg07e8dbd2008-10-18 20:27:42 -07001342 { applesmc_dmi_match, "Apple MacBook Pro 3", {
1343 DMI_MATCH(DMI_BOARD_VENDOR, "Apple"),
1344 DMI_MATCH(DMI_PRODUCT_NAME, "MacBookPro3") },
1345 &applesmc_dmi_data[9]},
Justin P. Mattocke1741712010-04-14 16:14:10 +02001346 { applesmc_dmi_match, "Apple MacBook Pro 2,2", {
1347 DMI_MATCH(DMI_BOARD_VENDOR, "Apple Computer, Inc."),
1348 DMI_MATCH(DMI_PRODUCT_NAME, "MacBookPro2,2") },
1349 &applesmc_dmi_data[18]},
Nicolas Boichat6f2fad72007-05-08 00:24:52 -07001350 { applesmc_dmi_match, "Apple MacBook Pro", {
1351 DMI_MATCH(DMI_BOARD_VENDOR,"Apple"),
1352 DMI_MATCH(DMI_PRODUCT_NAME,"MacBookPro") },
Andrew Morton7b5e3cb2008-10-18 20:27:41 -07001353 &applesmc_dmi_data[0]},
Guilherme M. Schroederf91a79f2008-08-15 00:40:32 -07001354 { applesmc_dmi_match, "Apple MacBook (v2)", {
Nicolas Boichat6f2fad72007-05-08 00:24:52 -07001355 DMI_MATCH(DMI_BOARD_VENDOR,"Apple"),
Riki Oktariantocd19ba12008-02-04 23:41:58 -08001356 DMI_MATCH(DMI_PRODUCT_NAME,"MacBook2") },
Andrew Morton7b5e3cb2008-10-18 20:27:41 -07001357 &applesmc_dmi_data[1]},
Guilherme M. Schroederf91a79f2008-08-15 00:40:32 -07001358 { applesmc_dmi_match, "Apple MacBook (v3)", {
1359 DMI_MATCH(DMI_BOARD_VENDOR,"Apple"),
1360 DMI_MATCH(DMI_PRODUCT_NAME,"MacBook3") },
Andrew Morton7b5e3cb2008-10-18 20:27:41 -07001361 &applesmc_dmi_data[6]},
Henrik Rydberg468cc032008-11-12 13:24:58 -08001362 { applesmc_dmi_match, "Apple MacBook 4", {
1363 DMI_MATCH(DMI_BOARD_VENDOR, "Apple"),
1364 DMI_MATCH(DMI_PRODUCT_NAME, "MacBook4") },
1365 &applesmc_dmi_data[6]},
Henrik Rydberg181209a12008-11-06 12:53:20 -08001366 { applesmc_dmi_match, "Apple MacBook 5", {
1367 DMI_MATCH(DMI_BOARD_VENDOR, "Apple"),
1368 DMI_MATCH(DMI_PRODUCT_NAME, "MacBook5") },
1369 &applesmc_dmi_data[11]},
Riki Oktariantocd19ba12008-02-04 23:41:58 -08001370 { applesmc_dmi_match, "Apple MacBook", {
1371 DMI_MATCH(DMI_BOARD_VENDOR,"Apple"),
1372 DMI_MATCH(DMI_PRODUCT_NAME,"MacBook") },
Andrew Morton7b5e3cb2008-10-18 20:27:41 -07001373 &applesmc_dmi_data[2]},
Nicolas Boichat6f2fad72007-05-08 00:24:52 -07001374 { applesmc_dmi_match, "Apple Macmini", {
1375 DMI_MATCH(DMI_BOARD_VENDOR,"Apple"),
1376 DMI_MATCH(DMI_PRODUCT_NAME,"Macmini") },
Andrew Morton7b5e3cb2008-10-18 20:27:41 -07001377 &applesmc_dmi_data[3]},
René Rebe8de57702007-10-16 14:19:20 -07001378 { applesmc_dmi_match, "Apple MacPro2", {
1379 DMI_MATCH(DMI_BOARD_VENDOR,"Apple"),
1380 DMI_MATCH(DMI_PRODUCT_NAME,"MacPro2") },
Andrew Morton7b5e3cb2008-10-18 20:27:41 -07001381 &applesmc_dmi_data[4]},
Bharath Rameshfb9f88e2009-01-29 14:25:24 -08001382 { applesmc_dmi_match, "Apple MacPro3", {
1383 DMI_MATCH(DMI_BOARD_VENDOR, "Apple"),
1384 DMI_MATCH(DMI_PRODUCT_NAME, "MacPro3") },
1385 &applesmc_dmi_data[16]},
Henrik Rydberg45a3a362008-11-19 15:36:42 -08001386 { applesmc_dmi_match, "Apple MacPro", {
1387 DMI_MATCH(DMI_BOARD_VENDOR, "Apple"),
1388 DMI_MATCH(DMI_PRODUCT_NAME, "MacPro") },
1389 &applesmc_dmi_data[4]},
Justin P. Mattocke1741712010-04-14 16:14:10 +02001390 { applesmc_dmi_match, "Apple iMac 9,1", {
1391 DMI_MATCH(DMI_BOARD_VENDOR, "Apple Inc."),
1392 DMI_MATCH(DMI_PRODUCT_NAME, "iMac9,1") },
1393 &applesmc_dmi_data[17]},
Henrik Rydbergeefc4882008-11-06 12:53:22 -08001394 { applesmc_dmi_match, "Apple iMac 8", {
1395 DMI_MATCH(DMI_BOARD_VENDOR, "Apple"),
1396 DMI_MATCH(DMI_PRODUCT_NAME, "iMac8") },
1397 &applesmc_dmi_data[13]},
Henrik Rydberg9ca791b2008-11-19 15:36:06 -08001398 { applesmc_dmi_match, "Apple iMac 6", {
1399 DMI_MATCH(DMI_BOARD_VENDOR, "Apple"),
1400 DMI_MATCH(DMI_PRODUCT_NAME, "iMac6") },
1401 &applesmc_dmi_data[14]},
Henrik Rydberg6e3530f2008-11-06 12:53:19 -08001402 { applesmc_dmi_match, "Apple iMac 5", {
1403 DMI_MATCH(DMI_BOARD_VENDOR, "Apple"),
1404 DMI_MATCH(DMI_PRODUCT_NAME, "iMac5") },
1405 &applesmc_dmi_data[10]},
Roberto De Ioris9f86f282008-08-15 00:40:30 -07001406 { applesmc_dmi_match, "Apple iMac", {
1407 DMI_MATCH(DMI_BOARD_VENDOR,"Apple"),
1408 DMI_MATCH(DMI_PRODUCT_NAME,"iMac") },
Andrew Morton7b5e3cb2008-10-18 20:27:41 -07001409 &applesmc_dmi_data[5]},
Nicolas Boichat6f2fad72007-05-08 00:24:52 -07001410 { .ident = NULL }
1411};
1412
1413static int __init applesmc_init(void)
1414{
1415 int ret;
1416 int count;
Nicolas Boichat6f2fad72007-05-08 00:24:52 -07001417
Nicolas Boichat6f2fad72007-05-08 00:24:52 -07001418 if (!dmi_check_system(applesmc_whitelist)) {
Joe Perches1ee7c712010-11-09 15:15:03 +00001419 pr_warn("supported laptop not found!\n");
Nicolas Boichat6f2fad72007-05-08 00:24:52 -07001420 ret = -ENODEV;
1421 goto out;
1422 }
1423
1424 if (!request_region(APPLESMC_DATA_PORT, APPLESMC_NR_PORTS,
1425 "applesmc")) {
1426 ret = -ENXIO;
1427 goto out;
1428 }
1429
1430 ret = platform_driver_register(&applesmc_driver);
1431 if (ret)
1432 goto out_region;
1433
Jean Delvareddfbf2a2007-05-08 20:27:04 -07001434 pdev = platform_device_register_simple("applesmc", APPLESMC_DATA_PORT,
1435 NULL, 0);
Nicolas Boichat6f2fad72007-05-08 00:24:52 -07001436 if (IS_ERR(pdev)) {
1437 ret = PTR_ERR(pdev);
1438 goto out_driver;
1439 }
1440
Henrik Rydberg58745832010-11-10 10:58:03 +00001441 /* create register cache */
1442 ret = applesmc_init_smcreg();
Nicolas Boichat6996abf2007-05-27 22:17:43 +02001443 if (ret)
1444 goto out_device;
Nicolas Boichatfa744192007-05-23 13:58:13 -07001445
Henrik Rydberg58745832010-11-10 10:58:03 +00001446 ret = sysfs_create_file(&pdev->dev.kobj, &dev_attr_name.attr);
1447 if (ret)
1448 goto out_smcreg;
1449
Nicolas Boichat6f2fad72007-05-08 00:24:52 -07001450 /* Create key enumeration sysfs files */
1451 ret = sysfs_create_group(&pdev->dev.kobj, &key_enumeration_group);
1452 if (ret)
Nicolas Boichat6996abf2007-05-27 22:17:43 +02001453 goto out_name;
Nicolas Boichat6f2fad72007-05-08 00:24:52 -07001454
1455 /* create fan files */
1456 count = applesmc_get_fan_count();
Henrik Rydberg0559a532010-05-11 09:17:47 +02001457 if (count < 0)
Joe Perches1ee7c712010-11-09 15:15:03 +00001458 pr_err("Cannot get the number of fans\n");
Henrik Rydberg0559a532010-05-11 09:17:47 +02001459 else
Joe Perches1ee7c712010-11-09 15:15:03 +00001460 pr_info("%d fans found\n", count);
Nicolas Boichat6f2fad72007-05-08 00:24:52 -07001461
Henrik Rydberg0559a532010-05-11 09:17:47 +02001462 if (count > 4) {
1463 count = 4;
Joe Perches1ee7c712010-11-09 15:15:03 +00001464 pr_warn("A maximum of 4 fans are supported by this driver\n");
Henrik Rydberg0559a532010-05-11 09:17:47 +02001465 }
1466
1467 while (fans_handled < count) {
1468 ret = sysfs_create_group(&pdev->dev.kobj,
1469 &fan_attribute_groups[fans_handled]);
1470 if (ret)
1471 goto out_fans;
1472 fans_handled++;
Nicolas Boichat6f2fad72007-05-08 00:24:52 -07001473 }
1474
Henrik Rydberg9792dad2010-11-10 10:58:04 +00001475 ret = applesmc_create_nodes(temp_group, smcreg.temp_count);
1476 if (ret)
1477 goto out_fans;
Nicolas Boichat6f2fad72007-05-08 00:24:52 -07001478
1479 if (applesmc_accelerometer) {
1480 ret = applesmc_create_accelerometer();
1481 if (ret)
1482 goto out_temperature;
1483 }
1484
1485 if (applesmc_light) {
1486 /* Add light sensor file */
1487 ret = sysfs_create_file(&pdev->dev.kobj, &dev_attr_light.attr);
1488 if (ret)
1489 goto out_accelerometer;
1490
1491 /* Create the workqueue */
1492 applesmc_led_wq = create_singlethread_workqueue("applesmc-led");
1493 if (!applesmc_led_wq) {
1494 ret = -ENOMEM;
1495 goto out_light_sysfs;
1496 }
1497
1498 /* register as a led device */
1499 ret = led_classdev_register(&pdev->dev, &applesmc_backlight);
1500 if (ret < 0)
1501 goto out_light_wq;
1502 }
1503
Tony Jones1beeffe2007-08-20 13:46:20 -07001504 hwmon_dev = hwmon_device_register(&pdev->dev);
1505 if (IS_ERR(hwmon_dev)) {
1506 ret = PTR_ERR(hwmon_dev);
Nicolas Boichat6f2fad72007-05-08 00:24:52 -07001507 goto out_light_ledclass;
1508 }
1509
Joe Perches1ee7c712010-11-09 15:15:03 +00001510 pr_info("driver successfully loaded\n");
Nicolas Boichat6f2fad72007-05-08 00:24:52 -07001511
1512 return 0;
1513
1514out_light_ledclass:
1515 if (applesmc_light)
1516 led_classdev_unregister(&applesmc_backlight);
1517out_light_wq:
1518 if (applesmc_light)
1519 destroy_workqueue(applesmc_led_wq);
1520out_light_sysfs:
1521 if (applesmc_light)
1522 sysfs_remove_file(&pdev->dev.kobj, &dev_attr_light.attr);
1523out_accelerometer:
1524 if (applesmc_accelerometer)
1525 applesmc_release_accelerometer();
1526out_temperature:
Henrik Rydberg9792dad2010-11-10 10:58:04 +00001527 applesmc_destroy_nodes(temp_group);
Henrik Rydberg0559a532010-05-11 09:17:47 +02001528out_fans:
1529 while (fans_handled)
1530 sysfs_remove_group(&pdev->dev.kobj,
1531 &fan_attribute_groups[--fans_handled]);
Nicolas Boichat6f2fad72007-05-08 00:24:52 -07001532 sysfs_remove_group(&pdev->dev.kobj, &key_enumeration_group);
Nicolas Boichat6996abf2007-05-27 22:17:43 +02001533out_name:
1534 sysfs_remove_file(&pdev->dev.kobj, &dev_attr_name.attr);
Henrik Rydberg58745832010-11-10 10:58:03 +00001535out_smcreg:
1536 applesmc_destroy_smcreg();
Nicolas Boichat6f2fad72007-05-08 00:24:52 -07001537out_device:
1538 platform_device_unregister(pdev);
1539out_driver:
1540 platform_driver_unregister(&applesmc_driver);
1541out_region:
1542 release_region(APPLESMC_DATA_PORT, APPLESMC_NR_PORTS);
1543out:
Joe Perches1ee7c712010-11-09 15:15:03 +00001544 pr_warn("driver init failed (ret=%d)!\n", ret);
Nicolas Boichat6f2fad72007-05-08 00:24:52 -07001545 return ret;
1546}
1547
1548static void __exit applesmc_exit(void)
1549{
Tony Jones1beeffe2007-08-20 13:46:20 -07001550 hwmon_device_unregister(hwmon_dev);
Nicolas Boichat6f2fad72007-05-08 00:24:52 -07001551 if (applesmc_light) {
1552 led_classdev_unregister(&applesmc_backlight);
1553 destroy_workqueue(applesmc_led_wq);
1554 sysfs_remove_file(&pdev->dev.kobj, &dev_attr_light.attr);
1555 }
1556 if (applesmc_accelerometer)
1557 applesmc_release_accelerometer();
Henrik Rydberg9792dad2010-11-10 10:58:04 +00001558 applesmc_destroy_nodes(temp_group);
Henrik Rydberg0559a532010-05-11 09:17:47 +02001559 while (fans_handled)
1560 sysfs_remove_group(&pdev->dev.kobj,
1561 &fan_attribute_groups[--fans_handled]);
Nicolas Boichat6f2fad72007-05-08 00:24:52 -07001562 sysfs_remove_group(&pdev->dev.kobj, &key_enumeration_group);
Nicolas Boichat6996abf2007-05-27 22:17:43 +02001563 sysfs_remove_file(&pdev->dev.kobj, &dev_attr_name.attr);
Henrik Rydberg58745832010-11-10 10:58:03 +00001564 applesmc_destroy_smcreg();
Nicolas Boichat6f2fad72007-05-08 00:24:52 -07001565 platform_device_unregister(pdev);
1566 platform_driver_unregister(&applesmc_driver);
1567 release_region(APPLESMC_DATA_PORT, APPLESMC_NR_PORTS);
1568
Joe Perches1ee7c712010-11-09 15:15:03 +00001569 pr_info("driver unloaded\n");
Nicolas Boichat6f2fad72007-05-08 00:24:52 -07001570}
1571
1572module_init(applesmc_init);
1573module_exit(applesmc_exit);
1574
1575MODULE_AUTHOR("Nicolas Boichat");
1576MODULE_DESCRIPTION("Apple SMC");
1577MODULE_LICENSE("GPL v2");
Henrik Rydbergdc924ef2008-12-01 13:13:49 -08001578MODULE_DEVICE_TABLE(dmi, applesmc_whitelist);