blob: 282708860517e8de41e2f86c3aeb55a118aaca17 [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>
Henrik Rydberg41e71f92010-11-09 15:15:11 +00007 * Copyright (C) 2010 Henrik Rydberg <rydberg@euromail.se>
Nicolas Boichat6f2fad72007-05-08 00:24:52 -07008 *
9 * Based on hdaps.c driver:
10 * Copyright (C) 2005 Robert Love <rml@novell.com>
Jesper Juhlaa8521e2012-06-20 21:18:48 +020011 * Copyright (C) 2005 Jesper Juhl <jj@chaosbits.net>
Nicolas Boichat6f2fad72007-05-08 00:24:52 -070012 *
13 * Fan control based on smcFanControl:
14 * Copyright (C) 2006 Hendrik Holtmann <holtmann@mac.com>
15 *
16 * This program is free software; you can redistribute it and/or modify it
17 * under the terms of the GNU General Public License v2 as published by the
18 * Free Software Foundation.
19 *
20 * This program is distributed in the hope that it will be useful, but WITHOUT
21 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
22 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
23 * more details.
24 *
25 * You should have received a copy of the GNU General Public License along with
26 * this program; if not, write to the Free Software Foundation, Inc.,
27 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
28 */
29
Joe Perches1ee7c712010-11-09 15:15:03 +000030#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
31
Nicolas Boichat6f2fad72007-05-08 00:24:52 -070032#include <linux/delay.h>
33#include <linux/platform_device.h>
Dmitry Torokhovd5cf2b92007-09-26 00:01:35 -040034#include <linux/input-polldev.h>
Nicolas Boichat6f2fad72007-05-08 00:24:52 -070035#include <linux/kernel.h>
Henrik Rydberg58745832010-11-10 10:58:03 +000036#include <linux/slab.h>
Nicolas Boichat6f2fad72007-05-08 00:24:52 -070037#include <linux/module.h>
38#include <linux/timer.h>
39#include <linux/dmi.h>
40#include <linux/mutex.h>
41#include <linux/hwmon-sysfs.h>
H Hartley Sweeten6055fae2009-09-15 17:18:13 +020042#include <linux/io.h>
Nicolas Boichat6f2fad72007-05-08 00:24:52 -070043#include <linux/leds.h>
44#include <linux/hwmon.h>
45#include <linux/workqueue.h>
46
47/* data port used by Apple SMC */
48#define APPLESMC_DATA_PORT 0x300
49/* command/status port used by Apple SMC */
50#define APPLESMC_CMD_PORT 0x304
51
52#define APPLESMC_NR_PORTS 32 /* 0x300-0x31f */
53
54#define APPLESMC_MAX_DATA_LENGTH 32
55
Henrik Rydberg58745832010-11-10 10:58:03 +000056/* wait up to 32 ms for a status change. */
Henrik Rydberga332bf92012-07-09 12:10:26 +020057#define APPLESMC_MIN_WAIT 0x0010
Henrik Rydberg829917c2012-07-27 20:12:46 +020058#define APPLESMC_RETRY_WAIT 0x0100
Henrik Rydberg8c9398d2008-10-18 20:27:43 -070059#define APPLESMC_MAX_WAIT 0x8000
60
Nicolas Boichat6f2fad72007-05-08 00:24:52 -070061#define APPLESMC_READ_CMD 0x10
62#define APPLESMC_WRITE_CMD 0x11
63#define APPLESMC_GET_KEY_BY_INDEX_CMD 0x12
64#define APPLESMC_GET_KEY_TYPE_CMD 0x13
65
66#define KEY_COUNT_KEY "#KEY" /* r-o ui32 */
67
Henrik Rydberg8bd1a122008-10-18 20:27:39 -070068#define LIGHT_SENSOR_LEFT_KEY "ALV0" /* r-o {alv (6-10 bytes) */
69#define LIGHT_SENSOR_RIGHT_KEY "ALV1" /* r-o {alv (6-10 bytes) */
Dmitry Torokhovd5cf2b92007-09-26 00:01:35 -040070#define BACKLIGHT_KEY "LKSB" /* w-o {lkb (2 bytes) */
Nicolas Boichat6f2fad72007-05-08 00:24:52 -070071
Dmitry Torokhovd5cf2b92007-09-26 00:01:35 -040072#define CLAMSHELL_KEY "MSLD" /* r-o ui8 (unused) */
Nicolas Boichat6f2fad72007-05-08 00:24:52 -070073
74#define MOTION_SENSOR_X_KEY "MO_X" /* r-o sp78 (2 bytes) */
75#define MOTION_SENSOR_Y_KEY "MO_Y" /* r-o sp78 (2 bytes) */
76#define MOTION_SENSOR_Z_KEY "MO_Z" /* r-o sp78 (2 bytes) */
77#define MOTION_SENSOR_KEY "MOCN" /* r/w ui16 */
78
79#define FANS_COUNT "FNum" /* r-o ui8 */
80#define FANS_MANUAL "FS! " /* r-w ui16 */
Henrik Rydberg3eba2bf2010-11-09 15:15:08 +000081#define FAN_ID_FMT "F%dID" /* r-o char[16] */
Nicolas Boichat6f2fad72007-05-08 00:24:52 -070082
Henrik Rydbergb6e51222012-07-16 09:18:10 +020083#define TEMP_SENSOR_TYPE "sp78"
84
Nicolas Boichat6f2fad72007-05-08 00:24:52 -070085/* List of keys used to read/write fan speeds */
Henrik Rydberg3eba2bf2010-11-09 15:15:08 +000086static const char *const fan_speed_fmt[] = {
87 "F%dAc", /* actual speed */
88 "F%dMn", /* minimum speed (rw) */
89 "F%dMx", /* maximum speed */
90 "F%dSf", /* safe speed - not all models */
91 "F%dTg", /* target speed (manual: rw) */
Nicolas Boichat6f2fad72007-05-08 00:24:52 -070092};
93
94#define INIT_TIMEOUT_MSECS 5000 /* wait up to 5s for device init ... */
95#define INIT_WAIT_MSECS 50 /* ... in 50ms increments */
96
Dmitry Torokhovd5cf2b92007-09-26 00:01:35 -040097#define APPLESMC_POLL_INTERVAL 50 /* msecs */
Nicolas Boichat6f2fad72007-05-08 00:24:52 -070098#define APPLESMC_INPUT_FUZZ 4 /* input event threshold */
99#define APPLESMC_INPUT_FLAT 4
100
Henrik Rydberg3eba2bf2010-11-09 15:15:08 +0000101#define to_index(attr) (to_sensor_dev_attr(attr)->index & 0xffff)
102#define to_option(attr) (to_sensor_dev_attr(attr)->index >> 16)
Henrik Rydberg9792dad2010-11-10 10:58:04 +0000103
Henrik Rydberg9792dad2010-11-10 10:58:04 +0000104/* Dynamic device node attributes */
105struct applesmc_dev_attr {
106 struct sensor_device_attribute sda; /* hwmon attributes */
107 char name[32]; /* room for node file name */
108};
109
110/* Dynamic device node group */
111struct applesmc_node_group {
112 char *format; /* format string */
113 void *show; /* show function */
114 void *store; /* store function */
Henrik Rydberg3eba2bf2010-11-09 15:15:08 +0000115 int option; /* function argument */
Henrik Rydberg9792dad2010-11-10 10:58:04 +0000116 struct applesmc_dev_attr *nodes; /* dynamic node array */
117};
118
Henrik Rydberg58745832010-11-10 10:58:03 +0000119/* AppleSMC entry - cached register information */
120struct applesmc_entry {
121 char key[5]; /* four-letter key code */
122 u8 valid; /* set when entry is successfully read once */
123 u8 len; /* bounded by APPLESMC_MAX_DATA_LENGTH */
124 char type[5]; /* four-letter type code */
125 u8 flags; /* 0x10: func; 0x40: write; 0x80: read */
126};
127
128/* Register lookup and registers common to all SMCs */
129static struct applesmc_registers {
130 struct mutex mutex; /* register read/write mutex */
131 unsigned int key_count; /* number of SMC registers */
Henrik Rydberg3eba2bf2010-11-09 15:15:08 +0000132 unsigned int fan_count; /* number of fans */
Henrik Rydberg9792dad2010-11-10 10:58:04 +0000133 unsigned int temp_count; /* number of temperature registers */
134 unsigned int temp_begin; /* temperature lower index bound */
135 unsigned int temp_end; /* temperature upper index bound */
Henrik Rydberge30bca12012-07-16 09:18:11 +0200136 unsigned int index_count; /* size of temperature index array */
Henrik Rydberg40ef06f2010-11-10 10:58:05 +0000137 int num_light_sensors; /* number of light sensors */
138 bool has_accelerometer; /* has motion sensor */
139 bool has_key_backlight; /* has keyboard backlight */
Henrik Rydberg58745832010-11-10 10:58:03 +0000140 bool init_complete; /* true when fully initialized */
141 struct applesmc_entry *cache; /* cached key entries */
Henrik Rydberge30bca12012-07-16 09:18:11 +0200142 const char **index; /* temperature key index */
Henrik Rydberg58745832010-11-10 10:58:03 +0000143} smcreg = {
144 .mutex = __MUTEX_INITIALIZER(smcreg.mutex),
145};
146
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700147static const int debug;
148static struct platform_device *pdev;
149static s16 rest_x;
150static s16 rest_y;
Henrik Rydberga976f152009-09-21 17:04:50 -0700151static u8 backlight_state[2];
152
Tony Jones1beeffe2007-08-20 13:46:20 -0700153static struct device *hwmon_dev;
Dmitry Torokhovd5cf2b92007-09-26 00:01:35 -0400154static struct input_polled_dev *applesmc_idev;
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700155
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700156/*
157 * Last index written to key_at_index sysfs file, and value to use for all other
158 * key_at_index_* sysfs files.
159 */
160static unsigned int key_at_index;
161
162static struct workqueue_struct *applesmc_led_wq;
163
164/*
Henrik Rydberg829917c2012-07-27 20:12:46 +0200165 * wait_read - Wait for a byte to appear on SMC port. Callers must
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700166 * hold applesmc_lock.
167 */
Henrik Rydberg829917c2012-07-27 20:12:46 +0200168static int wait_read(void)
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700169{
Henrik Rydberg829917c2012-07-27 20:12:46 +0200170 u8 status;
Henrik Rydberg8c9398d2008-10-18 20:27:43 -0700171 int us;
Henrik Rydberg8c9398d2008-10-18 20:27:43 -0700172 for (us = APPLESMC_MIN_WAIT; us < APPLESMC_MAX_WAIT; us <<= 1) {
173 udelay(us);
Henrik Rydberg829917c2012-07-27 20:12:46 +0200174 status = inb(APPLESMC_CMD_PORT);
175 /* read: wait for smc to settle */
176 if (status & 0x01)
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700177 return 0;
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700178 }
179
Henrik Rydberg829917c2012-07-27 20:12:46 +0200180 pr_warn("wait_read() fail: 0x%02x\n", status);
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700181 return -EIO;
182}
183
184/*
Henrik Rydberg829917c2012-07-27 20:12:46 +0200185 * send_byte - Write to SMC port, retrying when necessary. Callers
186 * must hold applesmc_lock.
Henrik Rydberg84d2d7f2008-10-18 20:27:38 -0700187 */
Henrik Rydberg829917c2012-07-27 20:12:46 +0200188static int send_byte(u8 cmd, u16 port)
189{
190 u8 status;
191 int us;
192
193 outb(cmd, port);
194 for (us = APPLESMC_MIN_WAIT; us < APPLESMC_MAX_WAIT; us <<= 1) {
195 udelay(us);
196 status = inb(APPLESMC_CMD_PORT);
197 /* write: wait for smc to settle */
198 if (status & 0x02)
199 continue;
200 /* ready: cmd accepted, return */
201 if (status & 0x04)
202 return 0;
203 /* timeout: give up */
204 if (us << 1 == APPLESMC_MAX_WAIT)
205 break;
206 /* busy: long wait and resend */
207 udelay(APPLESMC_RETRY_WAIT);
208 outb(cmd, port);
209 }
210
211 pr_warn("send_byte(0x%02x, 0x%04x) fail: 0x%02x\n", cmd, port, status);
212 return -EIO;
213}
214
Henrik Rydberg84d2d7f2008-10-18 20:27:38 -0700215static int send_command(u8 cmd)
216{
Henrik Rydberg829917c2012-07-27 20:12:46 +0200217 return send_byte(cmd, APPLESMC_CMD_PORT);
Henrik Rydberg84d2d7f2008-10-18 20:27:38 -0700218}
219
Henrik Rydberg58745832010-11-10 10:58:03 +0000220static int send_argument(const char *key)
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700221{
222 int i;
223
Henrik Rydberg829917c2012-07-27 20:12:46 +0200224 for (i = 0; i < 4; i++)
225 if (send_byte(key[i], APPLESMC_DATA_PORT))
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700226 return -EIO;
Henrik Rydberg58745832010-11-10 10:58:03 +0000227 return 0;
228}
229
230static int read_smc(u8 cmd, const char *key, u8 *buffer, u8 len)
231{
232 int i;
233
234 if (send_command(cmd) || send_argument(key)) {
Henrik Rydbergac852ed2012-06-07 04:54:29 -0400235 pr_warn("%.4s: read arg fail\n", key);
Henrik Rydberg58745832010-11-10 10:58:03 +0000236 return -EIO;
237 }
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700238
Henrik Rydberg829917c2012-07-27 20:12:46 +0200239 if (send_byte(len, APPLESMC_DATA_PORT)) {
240 pr_warn("%.4s: read len fail\n", key);
241 return -EIO;
242 }
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700243
244 for (i = 0; i < len; i++) {
Henrik Rydberg829917c2012-07-27 20:12:46 +0200245 if (wait_read()) {
246 pr_warn("%.4s: read data[%d] fail\n", key, i);
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700247 return -EIO;
Henrik Rydberg58745832010-11-10 10:58:03 +0000248 }
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700249 buffer[i] = inb(APPLESMC_DATA_PORT);
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700250 }
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700251
252 return 0;
253}
254
Henrik Rydberg58745832010-11-10 10:58:03 +0000255static int write_smc(u8 cmd, const char *key, const u8 *buffer, u8 len)
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700256{
257 int i;
258
Henrik Rydberg58745832010-11-10 10:58:03 +0000259 if (send_command(cmd) || send_argument(key)) {
260 pr_warn("%s: write arg fail\n", key);
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700261 return -EIO;
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700262 }
263
Henrik Rydberg829917c2012-07-27 20:12:46 +0200264 if (send_byte(len, APPLESMC_DATA_PORT)) {
265 pr_warn("%.4s: write len fail\n", key);
266 return -EIO;
267 }
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700268
269 for (i = 0; i < len; i++) {
Henrik Rydberg829917c2012-07-27 20:12:46 +0200270 if (send_byte(buffer[i], APPLESMC_DATA_PORT)) {
Henrik Rydberg58745832010-11-10 10:58:03 +0000271 pr_warn("%s: write data fail\n", key);
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700272 return -EIO;
Henrik Rydberg58745832010-11-10 10:58:03 +0000273 }
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700274 }
275
276 return 0;
277}
278
Henrik Rydberg58745832010-11-10 10:58:03 +0000279static int read_register_count(unsigned int *count)
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700280{
Henrik Rydberg58745832010-11-10 10:58:03 +0000281 __be32 be;
282 int ret;
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700283
Henrik Rydberg58745832010-11-10 10:58:03 +0000284 ret = read_smc(APPLESMC_READ_CMD, KEY_COUNT_KEY, (u8 *)&be, 4);
285 if (ret)
286 return ret;
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700287
Henrik Rydberg58745832010-11-10 10:58:03 +0000288 *count = be32_to_cpu(be);
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700289 return 0;
290}
291
292/*
Henrik Rydberg58745832010-11-10 10:58:03 +0000293 * Serialized I/O
294 *
295 * Returns zero on success or a negative error on failure.
296 * All functions below are concurrency safe - callers should NOT hold lock.
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700297 */
Henrik Rydberg58745832010-11-10 10:58:03 +0000298
299static int applesmc_read_entry(const struct applesmc_entry *entry,
300 u8 *buf, u8 len)
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700301{
Henrik Rydberg58745832010-11-10 10:58:03 +0000302 int ret;
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700303
Henrik Rydberg58745832010-11-10 10:58:03 +0000304 if (entry->len != len)
305 return -EINVAL;
306 mutex_lock(&smcreg.mutex);
307 ret = read_smc(APPLESMC_READ_CMD, entry->key, buf, len);
308 mutex_unlock(&smcreg.mutex);
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700309
Henrik Rydberg58745832010-11-10 10:58:03 +0000310 return ret;
311}
312
313static int applesmc_write_entry(const struct applesmc_entry *entry,
314 const u8 *buf, u8 len)
315{
316 int ret;
317
318 if (entry->len != len)
319 return -EINVAL;
320 mutex_lock(&smcreg.mutex);
321 ret = write_smc(APPLESMC_WRITE_CMD, entry->key, buf, len);
322 mutex_unlock(&smcreg.mutex);
323 return ret;
324}
325
326static const struct applesmc_entry *applesmc_get_entry_by_index(int index)
327{
328 struct applesmc_entry *cache = &smcreg.cache[index];
329 u8 key[4], info[6];
330 __be32 be;
331 int ret = 0;
332
333 if (cache->valid)
334 return cache;
335
336 mutex_lock(&smcreg.mutex);
337
338 if (cache->valid)
339 goto out;
340 be = cpu_to_be32(index);
341 ret = read_smc(APPLESMC_GET_KEY_BY_INDEX_CMD, (u8 *)&be, key, 4);
342 if (ret)
343 goto out;
344 ret = read_smc(APPLESMC_GET_KEY_TYPE_CMD, key, info, 6);
345 if (ret)
346 goto out;
347
348 memcpy(cache->key, key, 4);
349 cache->len = info[0];
350 memcpy(cache->type, &info[1], 4);
351 cache->flags = info[5];
352 cache->valid = 1;
353
354out:
355 mutex_unlock(&smcreg.mutex);
356 if (ret)
357 return ERR_PTR(ret);
358 return cache;
359}
360
361static int applesmc_get_lower_bound(unsigned int *lo, const char *key)
362{
363 int begin = 0, end = smcreg.key_count;
364 const struct applesmc_entry *entry;
365
366 while (begin != end) {
367 int middle = begin + (end - begin) / 2;
368 entry = applesmc_get_entry_by_index(middle);
Henrik Rydberg0fc86ec2012-01-26 06:08:41 -0500369 if (IS_ERR(entry)) {
370 *lo = 0;
Henrik Rydberg58745832010-11-10 10:58:03 +0000371 return PTR_ERR(entry);
Henrik Rydberg0fc86ec2012-01-26 06:08:41 -0500372 }
Henrik Rydberg58745832010-11-10 10:58:03 +0000373 if (strcmp(entry->key, key) < 0)
374 begin = middle + 1;
375 else
376 end = middle;
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700377 }
378
Henrik Rydberg58745832010-11-10 10:58:03 +0000379 *lo = begin;
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700380 return 0;
381}
382
Henrik Rydberg58745832010-11-10 10:58:03 +0000383static int applesmc_get_upper_bound(unsigned int *hi, const char *key)
384{
385 int begin = 0, end = smcreg.key_count;
386 const struct applesmc_entry *entry;
387
388 while (begin != end) {
389 int middle = begin + (end - begin) / 2;
390 entry = applesmc_get_entry_by_index(middle);
Henrik Rydberg0fc86ec2012-01-26 06:08:41 -0500391 if (IS_ERR(entry)) {
392 *hi = smcreg.key_count;
Henrik Rydberg58745832010-11-10 10:58:03 +0000393 return PTR_ERR(entry);
Henrik Rydberg0fc86ec2012-01-26 06:08:41 -0500394 }
Henrik Rydberg58745832010-11-10 10:58:03 +0000395 if (strcmp(key, entry->key) < 0)
396 end = middle;
397 else
398 begin = middle + 1;
399 }
400
401 *hi = begin;
402 return 0;
403}
404
405static const struct applesmc_entry *applesmc_get_entry_by_key(const char *key)
406{
407 int begin, end;
408 int ret;
409
410 ret = applesmc_get_lower_bound(&begin, key);
411 if (ret)
412 return ERR_PTR(ret);
413 ret = applesmc_get_upper_bound(&end, key);
414 if (ret)
415 return ERR_PTR(ret);
416 if (end - begin != 1)
417 return ERR_PTR(-EINVAL);
418
419 return applesmc_get_entry_by_index(begin);
420}
421
422static int applesmc_read_key(const char *key, u8 *buffer, u8 len)
423{
424 const struct applesmc_entry *entry;
425
426 entry = applesmc_get_entry_by_key(key);
427 if (IS_ERR(entry))
428 return PTR_ERR(entry);
429
430 return applesmc_read_entry(entry, buffer, len);
431}
432
433static int applesmc_write_key(const char *key, const u8 *buffer, u8 len)
434{
435 const struct applesmc_entry *entry;
436
437 entry = applesmc_get_entry_by_key(key);
438 if (IS_ERR(entry))
439 return PTR_ERR(entry);
440
441 return applesmc_write_entry(entry, buffer, len);
442}
443
Henrik Rydberg40ef06f2010-11-10 10:58:05 +0000444static int applesmc_has_key(const char *key, bool *value)
445{
446 const struct applesmc_entry *entry;
447
448 entry = applesmc_get_entry_by_key(key);
449 if (IS_ERR(entry) && PTR_ERR(entry) != -EINVAL)
450 return PTR_ERR(entry);
451
452 *value = !IS_ERR(entry);
453 return 0;
454}
455
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700456/*
Henrik Rydbergedf48f32012-06-20 18:00:06 +0200457 * applesmc_read_s16 - Read 16-bit signed big endian register
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700458 */
Henrik Rydbergedf48f32012-06-20 18:00:06 +0200459static int applesmc_read_s16(const char *key, s16 *value)
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700460{
461 u8 buffer[2];
462 int ret;
463
Henrik Rydbergedf48f32012-06-20 18:00:06 +0200464 ret = applesmc_read_key(key, buffer, 2);
465 if (ret)
466 return ret;
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700467
468 *value = ((s16)buffer[0] << 8) | buffer[1];
Henrik Rydbergedf48f32012-06-20 18:00:06 +0200469 return 0;
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700470}
471
472/*
Henrik Rydberg2344cd02010-11-09 15:15:02 +0000473 * applesmc_device_init - initialize the accelerometer. Can sleep.
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700474 */
Henrik Rydberg2344cd02010-11-09 15:15:02 +0000475static void applesmc_device_init(void)
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700476{
Henrik Rydberg2344cd02010-11-09 15:15:02 +0000477 int total;
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700478 u8 buffer[2];
479
Henrik Rydberg40ef06f2010-11-10 10:58:05 +0000480 if (!smcreg.has_accelerometer)
Henrik Rydberg2344cd02010-11-09 15:15:02 +0000481 return;
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700482
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700483 for (total = INIT_TIMEOUT_MSECS; total > 0; total -= INIT_WAIT_MSECS) {
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700484 if (!applesmc_read_key(MOTION_SENSOR_KEY, buffer, 2) &&
Henrik Rydberg2344cd02010-11-09 15:15:02 +0000485 (buffer[0] != 0x00 || buffer[1] != 0x00))
Henrik Rydberg58745832010-11-10 10:58:03 +0000486 return;
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700487 buffer[0] = 0xe0;
488 buffer[1] = 0x00;
489 applesmc_write_key(MOTION_SENSOR_KEY, buffer, 2);
490 msleep(INIT_WAIT_MSECS);
491 }
492
Joe Perches1ee7c712010-11-09 15:15:03 +0000493 pr_warn("failed to init the device\n");
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700494}
495
Henrik Rydberge30bca12012-07-16 09:18:11 +0200496static int applesmc_init_index(struct applesmc_registers *s)
497{
498 const struct applesmc_entry *entry;
499 unsigned int i;
500
501 if (s->index)
502 return 0;
503
504 s->index = kcalloc(s->temp_count, sizeof(s->index[0]), GFP_KERNEL);
505 if (!s->index)
506 return -ENOMEM;
507
508 for (i = s->temp_begin; i < s->temp_end; i++) {
509 entry = applesmc_get_entry_by_index(i);
510 if (IS_ERR(entry))
511 continue;
512 if (strcmp(entry->type, TEMP_SENSOR_TYPE))
513 continue;
514 s->index[s->index_count++] = entry->key;
515 }
516
517 return 0;
518}
519
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700520/*
Henrik Rydberg58745832010-11-10 10:58:03 +0000521 * applesmc_init_smcreg_try - Try to initialize register cache. Idempotent.
522 */
523static int applesmc_init_smcreg_try(void)
524{
525 struct applesmc_registers *s = &smcreg;
Henrik Rydberg40ef06f2010-11-10 10:58:05 +0000526 bool left_light_sensor, right_light_sensor;
Henrik Rydberg3eba2bf2010-11-09 15:15:08 +0000527 u8 tmp[1];
Henrik Rydberg58745832010-11-10 10:58:03 +0000528 int ret;
529
530 if (s->init_complete)
531 return 0;
532
533 ret = read_register_count(&s->key_count);
534 if (ret)
535 return ret;
536
537 if (!s->cache)
538 s->cache = kcalloc(s->key_count, sizeof(*s->cache), GFP_KERNEL);
539 if (!s->cache)
540 return -ENOMEM;
541
Henrik Rydberg3eba2bf2010-11-09 15:15:08 +0000542 ret = applesmc_read_key(FANS_COUNT, tmp, 1);
543 if (ret)
544 return ret;
545 s->fan_count = tmp[0];
546
Henrik Rydberg9792dad2010-11-10 10:58:04 +0000547 ret = applesmc_get_lower_bound(&s->temp_begin, "T");
548 if (ret)
549 return ret;
550 ret = applesmc_get_lower_bound(&s->temp_end, "U");
551 if (ret)
552 return ret;
553 s->temp_count = s->temp_end - s->temp_begin;
554
Henrik Rydberge30bca12012-07-16 09:18:11 +0200555 ret = applesmc_init_index(s);
556 if (ret)
557 return ret;
558
Henrik Rydberg40ef06f2010-11-10 10:58:05 +0000559 ret = applesmc_has_key(LIGHT_SENSOR_LEFT_KEY, &left_light_sensor);
560 if (ret)
561 return ret;
562 ret = applesmc_has_key(LIGHT_SENSOR_RIGHT_KEY, &right_light_sensor);
563 if (ret)
564 return ret;
565 ret = applesmc_has_key(MOTION_SENSOR_KEY, &s->has_accelerometer);
566 if (ret)
567 return ret;
568 ret = applesmc_has_key(BACKLIGHT_KEY, &s->has_key_backlight);
569 if (ret)
570 return ret;
571
572 s->num_light_sensors = left_light_sensor + right_light_sensor;
Henrik Rydberg58745832010-11-10 10:58:03 +0000573 s->init_complete = true;
574
Henrik Rydberge30bca12012-07-16 09:18:11 +0200575 pr_info("key=%d fan=%d temp=%d index=%d acc=%d lux=%d kbd=%d\n",
576 s->key_count, s->fan_count, s->temp_count, s->index_count,
Henrik Rydberg40ef06f2010-11-10 10:58:05 +0000577 s->has_accelerometer,
578 s->num_light_sensors,
579 s->has_key_backlight);
Henrik Rydberg58745832010-11-10 10:58:03 +0000580
581 return 0;
582}
583
Henrik Rydberge30bca12012-07-16 09:18:11 +0200584static void applesmc_destroy_smcreg(void)
585{
586 kfree(smcreg.index);
587 smcreg.index = NULL;
588 kfree(smcreg.cache);
589 smcreg.cache = NULL;
590 smcreg.init_complete = false;
591}
592
Henrik Rydberg58745832010-11-10 10:58:03 +0000593/*
594 * applesmc_init_smcreg - Initialize register cache.
595 *
596 * Retries until initialization is successful, or the operation times out.
597 *
598 */
599static int applesmc_init_smcreg(void)
600{
601 int ms, ret;
602
603 for (ms = 0; ms < INIT_TIMEOUT_MSECS; ms += INIT_WAIT_MSECS) {
604 ret = applesmc_init_smcreg_try();
605 if (!ret) {
606 if (ms)
607 pr_info("init_smcreg() took %d ms\n", ms);
608 return 0;
609 }
610 msleep(INIT_WAIT_MSECS);
611 }
612
Henrik Rydberge30bca12012-07-16 09:18:11 +0200613 applesmc_destroy_smcreg();
Henrik Rydberg58745832010-11-10 10:58:03 +0000614
615 return ret;
616}
617
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700618/* Device model stuff */
619static int applesmc_probe(struct platform_device *dev)
620{
Henrik Rydberg58745832010-11-10 10:58:03 +0000621 int ret;
622
623 ret = applesmc_init_smcreg();
624 if (ret)
625 return ret;
626
Henrik Rydberg2344cd02010-11-09 15:15:02 +0000627 applesmc_device_init();
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700628
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700629 return 0;
630}
631
Henrik Rydberga976f152009-09-21 17:04:50 -0700632/* Synchronize device with memorized backlight state */
633static int applesmc_pm_resume(struct device *dev)
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700634{
Henrik Rydberg40ef06f2010-11-10 10:58:05 +0000635 if (smcreg.has_key_backlight)
Henrik Rydberga976f152009-09-21 17:04:50 -0700636 applesmc_write_key(BACKLIGHT_KEY, backlight_state, 2);
Henrik Rydberga976f152009-09-21 17:04:50 -0700637 return 0;
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700638}
639
Henrik Rydberga976f152009-09-21 17:04:50 -0700640/* Reinitialize device on resume from hibernation */
641static int applesmc_pm_restore(struct device *dev)
642{
Henrik Rydberg2344cd02010-11-09 15:15:02 +0000643 applesmc_device_init();
Henrik Rydberga976f152009-09-21 17:04:50 -0700644 return applesmc_pm_resume(dev);
645}
646
Alexey Dobriyan47145212009-12-14 18:00:08 -0800647static const struct dev_pm_ops applesmc_pm_ops = {
Henrik Rydberga976f152009-09-21 17:04:50 -0700648 .resume = applesmc_pm_resume,
649 .restore = applesmc_pm_restore,
650};
651
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700652static struct platform_driver applesmc_driver = {
653 .probe = applesmc_probe,
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700654 .driver = {
655 .name = "applesmc",
656 .owner = THIS_MODULE,
Henrik Rydberga976f152009-09-21 17:04:50 -0700657 .pm = &applesmc_pm_ops,
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700658 },
659};
660
661/*
662 * applesmc_calibrate - Set our "resting" values. Callers must
663 * hold applesmc_lock.
664 */
665static void applesmc_calibrate(void)
666{
Henrik Rydbergedf48f32012-06-20 18:00:06 +0200667 applesmc_read_s16(MOTION_SENSOR_X_KEY, &rest_x);
668 applesmc_read_s16(MOTION_SENSOR_Y_KEY, &rest_y);
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700669 rest_x = -rest_x;
670}
671
Dmitry Torokhovd5cf2b92007-09-26 00:01:35 -0400672static void applesmc_idev_poll(struct input_polled_dev *dev)
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700673{
Dmitry Torokhovd5cf2b92007-09-26 00:01:35 -0400674 struct input_dev *idev = dev->input;
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700675 s16 x, y;
676
Henrik Rydbergedf48f32012-06-20 18:00:06 +0200677 if (applesmc_read_s16(MOTION_SENSOR_X_KEY, &x))
Henrik Rydberg58745832010-11-10 10:58:03 +0000678 return;
Henrik Rydbergedf48f32012-06-20 18:00:06 +0200679 if (applesmc_read_s16(MOTION_SENSOR_Y_KEY, &y))
Henrik Rydberg58745832010-11-10 10:58:03 +0000680 return;
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700681
682 x = -x;
Dmitry Torokhovd5cf2b92007-09-26 00:01:35 -0400683 input_report_abs(idev, ABS_X, x - rest_x);
684 input_report_abs(idev, ABS_Y, y - rest_y);
685 input_sync(idev);
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700686}
687
688/* Sysfs Files */
689
Nicolas Boichatfa744192007-05-23 13:58:13 -0700690static ssize_t applesmc_name_show(struct device *dev,
691 struct device_attribute *attr, char *buf)
692{
693 return snprintf(buf, PAGE_SIZE, "applesmc\n");
694}
695
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700696static ssize_t applesmc_position_show(struct device *dev,
697 struct device_attribute *attr, char *buf)
698{
699 int ret;
700 s16 x, y, z;
701
Henrik Rydbergedf48f32012-06-20 18:00:06 +0200702 ret = applesmc_read_s16(MOTION_SENSOR_X_KEY, &x);
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700703 if (ret)
704 goto out;
Henrik Rydbergedf48f32012-06-20 18:00:06 +0200705 ret = applesmc_read_s16(MOTION_SENSOR_Y_KEY, &y);
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700706 if (ret)
707 goto out;
Henrik Rydbergedf48f32012-06-20 18:00:06 +0200708 ret = applesmc_read_s16(MOTION_SENSOR_Z_KEY, &z);
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700709 if (ret)
710 goto out;
711
712out:
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700713 if (ret)
714 return ret;
715 else
716 return snprintf(buf, PAGE_SIZE, "(%d,%d,%d)\n", x, y, z);
717}
718
719static ssize_t applesmc_light_show(struct device *dev,
720 struct device_attribute *attr, char *sysfsbuf)
721{
Henrik Rydberg58745832010-11-10 10:58:03 +0000722 const struct applesmc_entry *entry;
Henrik Rydberg8bd1a122008-10-18 20:27:39 -0700723 static int data_length;
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700724 int ret;
725 u8 left = 0, right = 0;
Henrik Rydberg58745832010-11-10 10:58:03 +0000726 u8 buffer[10];
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700727
Henrik Rydberg8bd1a122008-10-18 20:27:39 -0700728 if (!data_length) {
Henrik Rydberg58745832010-11-10 10:58:03 +0000729 entry = applesmc_get_entry_by_key(LIGHT_SENSOR_LEFT_KEY);
730 if (IS_ERR(entry))
731 return PTR_ERR(entry);
732 if (entry->len > 10)
733 return -ENXIO;
734 data_length = entry->len;
Joe Perches1ee7c712010-11-09 15:15:03 +0000735 pr_info("light sensor data length set to %d\n", data_length);
Henrik Rydberg8bd1a122008-10-18 20:27:39 -0700736 }
737
738 ret = applesmc_read_key(LIGHT_SENSOR_LEFT_KEY, buffer, data_length);
Alex Murrayc3d63622009-01-15 13:51:08 -0800739 /* newer macbooks report a single 10-bit bigendian value */
740 if (data_length == 10) {
741 left = be16_to_cpu(*(__be16 *)(buffer + 6)) >> 2;
742 goto out;
743 }
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700744 left = buffer[2];
745 if (ret)
746 goto out;
Henrik Rydberg8bd1a122008-10-18 20:27:39 -0700747 ret = applesmc_read_key(LIGHT_SENSOR_RIGHT_KEY, buffer, data_length);
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700748 right = buffer[2];
749
750out:
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700751 if (ret)
752 return ret;
753 else
754 return snprintf(sysfsbuf, PAGE_SIZE, "(%d,%d)\n", left, right);
755}
756
Alex Murrayfa5575c2010-05-27 19:58:54 +0200757/* Displays sensor key as label */
758static ssize_t applesmc_show_sensor_label(struct device *dev,
759 struct device_attribute *devattr, char *sysfsbuf)
760{
Henrik Rydberge30bca12012-07-16 09:18:11 +0200761 const char *key = smcreg.index[to_index(devattr)];
Alex Murrayfa5575c2010-05-27 19:58:54 +0200762
Henrik Rydberge30bca12012-07-16 09:18:11 +0200763 return snprintf(sysfsbuf, PAGE_SIZE, "%s\n", key);
Alex Murrayfa5575c2010-05-27 19:58:54 +0200764}
765
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700766/* Displays degree Celsius * 1000 */
767static ssize_t applesmc_show_temperature(struct device *dev,
768 struct device_attribute *devattr, char *sysfsbuf)
769{
Henrik Rydberge30bca12012-07-16 09:18:11 +0200770 const char *key = smcreg.index[to_index(devattr)];
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700771 int ret;
Henrik Rydbergb6e51222012-07-16 09:18:10 +0200772 s16 value;
773 int temp;
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700774
Henrik Rydberge30bca12012-07-16 09:18:11 +0200775 ret = applesmc_read_s16(key, &value);
Henrik Rydberg9792dad2010-11-10 10:58:04 +0000776 if (ret)
777 return ret;
778
Henrik Rydbergb6e51222012-07-16 09:18:10 +0200779 temp = 250 * (value >> 6);
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700780
Henrik Rydbergb6e51222012-07-16 09:18:10 +0200781 return snprintf(sysfsbuf, PAGE_SIZE, "%d\n", temp);
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700782}
783
784static ssize_t applesmc_show_fan_speed(struct device *dev,
785 struct device_attribute *attr, char *sysfsbuf)
786{
787 int ret;
788 unsigned int speed = 0;
789 char newkey[5];
790 u8 buffer[2];
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700791
Henrik Rydberg3eba2bf2010-11-09 15:15:08 +0000792 sprintf(newkey, fan_speed_fmt[to_option(attr)], to_index(attr));
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700793
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700794 ret = applesmc_read_key(newkey, buffer, 2);
795 speed = ((buffer[0] << 8 | buffer[1]) >> 2);
796
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700797 if (ret)
798 return ret;
799 else
800 return snprintf(sysfsbuf, PAGE_SIZE, "%u\n", speed);
801}
802
803static ssize_t applesmc_store_fan_speed(struct device *dev,
804 struct device_attribute *attr,
805 const char *sysfsbuf, size_t count)
806{
807 int ret;
Guenter Roeck2bfe8142010-11-10 10:42:21 -0800808 unsigned long speed;
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700809 char newkey[5];
810 u8 buffer[2];
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700811
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +0100812 if (kstrtoul(sysfsbuf, 10, &speed) < 0 || speed >= 0x4000)
Guenter Roeck2bfe8142010-11-10 10:42:21 -0800813 return -EINVAL; /* Bigger than a 14-bit value */
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700814
Henrik Rydberg3eba2bf2010-11-09 15:15:08 +0000815 sprintf(newkey, fan_speed_fmt[to_option(attr)], to_index(attr));
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700816
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700817 buffer[0] = (speed >> 6) & 0xff;
818 buffer[1] = (speed << 2) & 0xff;
819 ret = applesmc_write_key(newkey, buffer, 2);
820
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700821 if (ret)
822 return ret;
823 else
824 return count;
825}
826
827static ssize_t applesmc_show_fan_manual(struct device *dev,
Henrik Rydberg3eba2bf2010-11-09 15:15:08 +0000828 struct device_attribute *attr, char *sysfsbuf)
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700829{
830 int ret;
831 u16 manual = 0;
832 u8 buffer[2];
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700833
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700834 ret = applesmc_read_key(FANS_MANUAL, buffer, 2);
Henrik Rydberg3eba2bf2010-11-09 15:15:08 +0000835 manual = ((buffer[0] << 8 | buffer[1]) >> to_index(attr)) & 0x01;
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700836
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700837 if (ret)
838 return ret;
839 else
840 return snprintf(sysfsbuf, PAGE_SIZE, "%d\n", manual);
841}
842
843static ssize_t applesmc_store_fan_manual(struct device *dev,
Henrik Rydberg3eba2bf2010-11-09 15:15:08 +0000844 struct device_attribute *attr,
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700845 const char *sysfsbuf, size_t count)
846{
847 int ret;
848 u8 buffer[2];
Guenter Roeck2bfe8142010-11-10 10:42:21 -0800849 unsigned long input;
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700850 u16 val;
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700851
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +0100852 if (kstrtoul(sysfsbuf, 10, &input) < 0)
Guenter Roeck2bfe8142010-11-10 10:42:21 -0800853 return -EINVAL;
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700854
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700855 ret = applesmc_read_key(FANS_MANUAL, buffer, 2);
856 val = (buffer[0] << 8 | buffer[1]);
857 if (ret)
858 goto out;
859
860 if (input)
Henrik Rydberg3eba2bf2010-11-09 15:15:08 +0000861 val = val | (0x01 << to_index(attr));
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700862 else
Henrik Rydberg3eba2bf2010-11-09 15:15:08 +0000863 val = val & ~(0x01 << to_index(attr));
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700864
865 buffer[0] = (val >> 8) & 0xFF;
866 buffer[1] = val & 0xFF;
867
868 ret = applesmc_write_key(FANS_MANUAL, buffer, 2);
869
870out:
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700871 if (ret)
872 return ret;
873 else
874 return count;
875}
876
877static ssize_t applesmc_show_fan_position(struct device *dev,
878 struct device_attribute *attr, char *sysfsbuf)
879{
880 int ret;
881 char newkey[5];
882 u8 buffer[17];
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700883
Henrik Rydberg3eba2bf2010-11-09 15:15:08 +0000884 sprintf(newkey, FAN_ID_FMT, to_index(attr));
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700885
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700886 ret = applesmc_read_key(newkey, buffer, 16);
887 buffer[16] = 0;
888
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700889 if (ret)
890 return ret;
891 else
892 return snprintf(sysfsbuf, PAGE_SIZE, "%s\n", buffer+4);
893}
894
895static ssize_t applesmc_calibrate_show(struct device *dev,
896 struct device_attribute *attr, char *sysfsbuf)
897{
898 return snprintf(sysfsbuf, PAGE_SIZE, "(%d,%d)\n", rest_x, rest_y);
899}
900
901static ssize_t applesmc_calibrate_store(struct device *dev,
902 struct device_attribute *attr, const char *sysfsbuf, size_t count)
903{
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700904 applesmc_calibrate();
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700905
906 return count;
907}
908
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700909static void applesmc_backlight_set(struct work_struct *work)
910{
Henrik Rydberga976f152009-09-21 17:04:50 -0700911 applesmc_write_key(BACKLIGHT_KEY, backlight_state, 2);
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700912}
913static DECLARE_WORK(backlight_work, &applesmc_backlight_set);
914
915static void applesmc_brightness_set(struct led_classdev *led_cdev,
916 enum led_brightness value)
917{
918 int ret;
919
Henrik Rydberga976f152009-09-21 17:04:50 -0700920 backlight_state[0] = value;
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700921 ret = queue_work(applesmc_led_wq, &backlight_work);
922
923 if (debug && (!ret))
924 printk(KERN_DEBUG "applesmc: work was already on the queue.\n");
925}
926
927static ssize_t applesmc_key_count_show(struct device *dev,
928 struct device_attribute *attr, char *sysfsbuf)
929{
930 int ret;
931 u8 buffer[4];
932 u32 count;
933
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700934 ret = applesmc_read_key(KEY_COUNT_KEY, buffer, 4);
935 count = ((u32)buffer[0]<<24) + ((u32)buffer[1]<<16) +
936 ((u32)buffer[2]<<8) + buffer[3];
937
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700938 if (ret)
939 return ret;
940 else
941 return snprintf(sysfsbuf, PAGE_SIZE, "%d\n", count);
942}
943
944static ssize_t applesmc_key_at_index_read_show(struct device *dev,
945 struct device_attribute *attr, char *sysfsbuf)
946{
Henrik Rydberg58745832010-11-10 10:58:03 +0000947 const struct applesmc_entry *entry;
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700948 int ret;
949
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);
953 ret = applesmc_read_entry(entry, sysfsbuf, entry->len);
954 if (ret)
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700955 return ret;
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700956
Henrik Rydberg58745832010-11-10 10:58:03 +0000957 return entry->len;
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700958}
959
960static ssize_t applesmc_key_at_index_data_length_show(struct device *dev,
961 struct device_attribute *attr, char *sysfsbuf)
962{
Henrik Rydberg58745832010-11-10 10:58:03 +0000963 const struct applesmc_entry *entry;
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700964
Henrik Rydberg58745832010-11-10 10:58:03 +0000965 entry = applesmc_get_entry_by_index(key_at_index);
966 if (IS_ERR(entry))
967 return PTR_ERR(entry);
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700968
Henrik Rydberg58745832010-11-10 10:58:03 +0000969 return snprintf(sysfsbuf, PAGE_SIZE, "%d\n", entry->len);
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700970}
971
972static ssize_t applesmc_key_at_index_type_show(struct device *dev,
973 struct device_attribute *attr, char *sysfsbuf)
974{
Henrik Rydberg58745832010-11-10 10:58:03 +0000975 const struct applesmc_entry *entry;
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700976
Henrik Rydberg58745832010-11-10 10:58:03 +0000977 entry = applesmc_get_entry_by_index(key_at_index);
978 if (IS_ERR(entry))
979 return PTR_ERR(entry);
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700980
Henrik Rydberg58745832010-11-10 10:58:03 +0000981 return snprintf(sysfsbuf, PAGE_SIZE, "%s\n", entry->type);
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700982}
983
984static ssize_t applesmc_key_at_index_name_show(struct device *dev,
985 struct device_attribute *attr, char *sysfsbuf)
986{
Henrik Rydberg58745832010-11-10 10:58:03 +0000987 const struct applesmc_entry *entry;
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700988
Henrik Rydberg58745832010-11-10 10:58:03 +0000989 entry = applesmc_get_entry_by_index(key_at_index);
990 if (IS_ERR(entry))
991 return PTR_ERR(entry);
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700992
Henrik Rydberg58745832010-11-10 10:58:03 +0000993 return snprintf(sysfsbuf, PAGE_SIZE, "%s\n", entry->key);
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700994}
995
996static ssize_t applesmc_key_at_index_show(struct device *dev,
997 struct device_attribute *attr, char *sysfsbuf)
998{
999 return snprintf(sysfsbuf, PAGE_SIZE, "%d\n", key_at_index);
1000}
1001
1002static ssize_t applesmc_key_at_index_store(struct device *dev,
1003 struct device_attribute *attr, const char *sysfsbuf, size_t count)
1004{
Henrik Rydberg58745832010-11-10 10:58:03 +00001005 unsigned long newkey;
Nicolas Boichat6f2fad72007-05-08 00:24:52 -07001006
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +01001007 if (kstrtoul(sysfsbuf, 10, &newkey) < 0
Henrik Rydberg58745832010-11-10 10:58:03 +00001008 || newkey >= smcreg.key_count)
1009 return -EINVAL;
Nicolas Boichat6f2fad72007-05-08 00:24:52 -07001010
Henrik Rydberg58745832010-11-10 10:58:03 +00001011 key_at_index = newkey;
Nicolas Boichat6f2fad72007-05-08 00:24:52 -07001012 return count;
1013}
1014
1015static struct led_classdev applesmc_backlight = {
Richard Purdie6c152be2007-10-31 15:00:07 +01001016 .name = "smc::kbd_backlight",
Nicolas Boichat6f2fad72007-05-08 00:24:52 -07001017 .default_trigger = "nand-disk",
1018 .brightness_set = applesmc_brightness_set,
1019};
1020
Henrik Rydberg0b0b5df2010-11-09 15:15:09 +00001021static struct applesmc_node_group info_group[] = {
1022 { "name", applesmc_name_show },
1023 { "key_count", applesmc_key_count_show },
1024 { "key_at_index", applesmc_key_at_index_show, applesmc_key_at_index_store },
1025 { "key_at_index_name", applesmc_key_at_index_name_show },
1026 { "key_at_index_type", applesmc_key_at_index_type_show },
1027 { "key_at_index_data_length", applesmc_key_at_index_data_length_show },
1028 { "key_at_index_data", applesmc_key_at_index_read_show },
1029 { }
Nicolas Boichat6f2fad72007-05-08 00:24:52 -07001030};
1031
Henrik Rydberg0b0b5df2010-11-09 15:15:09 +00001032static struct applesmc_node_group accelerometer_group[] = {
1033 { "position", applesmc_position_show },
1034 { "calibrate", applesmc_calibrate_show, applesmc_calibrate_store },
1035 { }
Nicolas Boichat6f2fad72007-05-08 00:24:52 -07001036};
1037
Henrik Rydberg0b0b5df2010-11-09 15:15:09 +00001038static struct applesmc_node_group light_sensor_group[] = {
1039 { "light", applesmc_light_show },
1040 { }
1041};
Nicolas Boichat6f2fad72007-05-08 00:24:52 -07001042
Henrik Rydberg3eba2bf2010-11-09 15:15:08 +00001043static struct applesmc_node_group fan_group[] = {
1044 { "fan%d_label", applesmc_show_fan_position },
1045 { "fan%d_input", applesmc_show_fan_speed, NULL, 0 },
1046 { "fan%d_min", applesmc_show_fan_speed, applesmc_store_fan_speed, 1 },
1047 { "fan%d_max", applesmc_show_fan_speed, NULL, 2 },
1048 { "fan%d_safe", applesmc_show_fan_speed, NULL, 3 },
1049 { "fan%d_output", applesmc_show_fan_speed, applesmc_store_fan_speed, 4 },
1050 { "fan%d_manual", applesmc_show_fan_manual, applesmc_store_fan_manual },
1051 { }
Nicolas Boichat6f2fad72007-05-08 00:24:52 -07001052};
1053
Henrik Rydberg9792dad2010-11-10 10:58:04 +00001054static struct applesmc_node_group temp_group[] = {
1055 { "temp%d_label", applesmc_show_sensor_label },
1056 { "temp%d_input", applesmc_show_temperature },
1057 { }
Alex Murrayfa5575c2010-05-27 19:58:54 +02001058};
1059
Nicolas Boichat6f2fad72007-05-08 00:24:52 -07001060/* Module stuff */
1061
1062/*
Henrik Rydberg9792dad2010-11-10 10:58:04 +00001063 * applesmc_destroy_nodes - remove files and free associated memory
1064 */
1065static void applesmc_destroy_nodes(struct applesmc_node_group *groups)
1066{
1067 struct applesmc_node_group *grp;
1068 struct applesmc_dev_attr *node;
1069
1070 for (grp = groups; grp->nodes; grp++) {
1071 for (node = grp->nodes; node->sda.dev_attr.attr.name; node++)
1072 sysfs_remove_file(&pdev->dev.kobj,
1073 &node->sda.dev_attr.attr);
1074 kfree(grp->nodes);
1075 grp->nodes = NULL;
1076 }
1077}
1078
1079/*
1080 * applesmc_create_nodes - create a two-dimensional group of sysfs files
1081 */
1082static int applesmc_create_nodes(struct applesmc_node_group *groups, int num)
1083{
1084 struct applesmc_node_group *grp;
1085 struct applesmc_dev_attr *node;
1086 struct attribute *attr;
1087 int ret, i;
1088
1089 for (grp = groups; grp->format; grp++) {
1090 grp->nodes = kcalloc(num + 1, sizeof(*node), GFP_KERNEL);
1091 if (!grp->nodes) {
1092 ret = -ENOMEM;
1093 goto out;
1094 }
1095 for (i = 0; i < num; i++) {
1096 node = &grp->nodes[i];
1097 sprintf(node->name, grp->format, i + 1);
Henrik Rydberg3eba2bf2010-11-09 15:15:08 +00001098 node->sda.index = (grp->option << 16) | (i & 0xffff);
Henrik Rydberg9792dad2010-11-10 10:58:04 +00001099 node->sda.dev_attr.show = grp->show;
1100 node->sda.dev_attr.store = grp->store;
1101 attr = &node->sda.dev_attr.attr;
Henrik Rydberg9d1f8a402011-01-22 20:55:39 +01001102 sysfs_attr_init(attr);
Henrik Rydberg9792dad2010-11-10 10:58:04 +00001103 attr->name = node->name;
1104 attr->mode = S_IRUGO | (grp->store ? S_IWUSR : 0);
1105 ret = sysfs_create_file(&pdev->dev.kobj, attr);
1106 if (ret) {
1107 attr->name = NULL;
1108 goto out;
1109 }
1110 }
1111 }
1112
1113 return 0;
1114out:
1115 applesmc_destroy_nodes(groups);
1116 return ret;
1117}
1118
Nicolas Boichat6f2fad72007-05-08 00:24:52 -07001119/* Create accelerometer ressources */
1120static int applesmc_create_accelerometer(void)
1121{
Dmitry Torokhovd5cf2b92007-09-26 00:01:35 -04001122 struct input_dev *idev;
Nicolas Boichat6f2fad72007-05-08 00:24:52 -07001123 int ret;
1124
Henrik Rydberg0b0b5df2010-11-09 15:15:09 +00001125 if (!smcreg.has_accelerometer)
1126 return 0;
1127
1128 ret = applesmc_create_nodes(accelerometer_group, 1);
Nicolas Boichat6f2fad72007-05-08 00:24:52 -07001129 if (ret)
1130 goto out;
1131
Dmitry Torokhovd5cf2b92007-09-26 00:01:35 -04001132 applesmc_idev = input_allocate_polled_device();
Nicolas Boichat6f2fad72007-05-08 00:24:52 -07001133 if (!applesmc_idev) {
1134 ret = -ENOMEM;
1135 goto out_sysfs;
1136 }
1137
Dmitry Torokhovd5cf2b92007-09-26 00:01:35 -04001138 applesmc_idev->poll = applesmc_idev_poll;
1139 applesmc_idev->poll_interval = APPLESMC_POLL_INTERVAL;
1140
Nicolas Boichat6f2fad72007-05-08 00:24:52 -07001141 /* initial calibrate for the input device */
1142 applesmc_calibrate();
1143
Dmitry Torokhovd5cf2b92007-09-26 00:01:35 -04001144 /* initialize the input device */
1145 idev = applesmc_idev->input;
1146 idev->name = "applesmc";
1147 idev->id.bustype = BUS_HOST;
1148 idev->dev.parent = &pdev->dev;
Jiri Slaby7b19ada2007-10-18 23:40:32 -07001149 idev->evbit[0] = BIT_MASK(EV_ABS);
Dmitry Torokhovd5cf2b92007-09-26 00:01:35 -04001150 input_set_abs_params(idev, ABS_X,
Nicolas Boichat6f2fad72007-05-08 00:24:52 -07001151 -256, 256, APPLESMC_INPUT_FUZZ, APPLESMC_INPUT_FLAT);
Dmitry Torokhovd5cf2b92007-09-26 00:01:35 -04001152 input_set_abs_params(idev, ABS_Y,
Nicolas Boichat6f2fad72007-05-08 00:24:52 -07001153 -256, 256, APPLESMC_INPUT_FUZZ, APPLESMC_INPUT_FLAT);
1154
Dmitry Torokhovd5cf2b92007-09-26 00:01:35 -04001155 ret = input_register_polled_device(applesmc_idev);
Nicolas Boichat6f2fad72007-05-08 00:24:52 -07001156 if (ret)
1157 goto out_idev;
1158
Nicolas Boichat6f2fad72007-05-08 00:24:52 -07001159 return 0;
1160
1161out_idev:
Dmitry Torokhovd5cf2b92007-09-26 00:01:35 -04001162 input_free_polled_device(applesmc_idev);
Nicolas Boichat6f2fad72007-05-08 00:24:52 -07001163
1164out_sysfs:
Henrik Rydberg0b0b5df2010-11-09 15:15:09 +00001165 applesmc_destroy_nodes(accelerometer_group);
Nicolas Boichat6f2fad72007-05-08 00:24:52 -07001166
1167out:
Joe Perches1ee7c712010-11-09 15:15:03 +00001168 pr_warn("driver init failed (ret=%d)!\n", ret);
Nicolas Boichat6f2fad72007-05-08 00:24:52 -07001169 return ret;
1170}
1171
1172/* Release all ressources used by the accelerometer */
1173static void applesmc_release_accelerometer(void)
1174{
Henrik Rydberg0b0b5df2010-11-09 15:15:09 +00001175 if (!smcreg.has_accelerometer)
1176 return;
Dmitry Torokhovd5cf2b92007-09-26 00:01:35 -04001177 input_unregister_polled_device(applesmc_idev);
1178 input_free_polled_device(applesmc_idev);
Henrik Rydberg0b0b5df2010-11-09 15:15:09 +00001179 applesmc_destroy_nodes(accelerometer_group);
Nicolas Boichat6f2fad72007-05-08 00:24:52 -07001180}
Henrik Rydberg0b0b5df2010-11-09 15:15:09 +00001181
1182static int applesmc_create_light_sensor(void)
1183{
1184 if (!smcreg.num_light_sensors)
1185 return 0;
1186 return applesmc_create_nodes(light_sensor_group, 1);
1187}
1188
1189static void applesmc_release_light_sensor(void)
1190{
1191 if (!smcreg.num_light_sensors)
1192 return;
1193 applesmc_destroy_nodes(light_sensor_group);
1194}
1195
1196static int applesmc_create_key_backlight(void)
1197{
1198 if (!smcreg.has_key_backlight)
1199 return 0;
1200 applesmc_led_wq = create_singlethread_workqueue("applesmc-led");
1201 if (!applesmc_led_wq)
1202 return -ENOMEM;
1203 return led_classdev_register(&pdev->dev, &applesmc_backlight);
1204}
1205
1206static void applesmc_release_key_backlight(void)
1207{
1208 if (!smcreg.has_key_backlight)
1209 return;
1210 led_classdev_unregister(&applesmc_backlight);
1211 destroy_workqueue(applesmc_led_wq);
1212}
1213
Henrik Rydberg40ef06f2010-11-10 10:58:05 +00001214static int applesmc_dmi_match(const struct dmi_system_id *id)
1215{
1216 return 1;
1217}
Nicolas Boichat6f2fad72007-05-08 00:24:52 -07001218
Guenter Roeck85ebfd32012-01-19 11:02:15 -08001219/*
1220 * Note that DMI_MATCH(...,"MacBook") will match "MacBookPro1,1".
1221 * So we need to put "Apple MacBook Pro" before "Apple MacBook".
1222 */
Nicolas Boichat6f2fad72007-05-08 00:24:52 -07001223static __initdata struct dmi_system_id applesmc_whitelist[] = {
Henrik Rydbergf5274c92008-10-18 20:27:40 -07001224 { applesmc_dmi_match, "Apple MacBook Air", {
1225 DMI_MATCH(DMI_BOARD_VENDOR, "Apple"),
1226 DMI_MATCH(DMI_PRODUCT_NAME, "MacBookAir") },
Henrik Rydberg40ef06f2010-11-10 10:58:05 +00001227 },
Nicolas Boichat6f2fad72007-05-08 00:24:52 -07001228 { applesmc_dmi_match, "Apple MacBook Pro", {
Guenter Roeck2bfe8142010-11-10 10:42:21 -08001229 DMI_MATCH(DMI_BOARD_VENDOR, "Apple"),
1230 DMI_MATCH(DMI_PRODUCT_NAME, "MacBookPro") },
Henrik Rydberg40ef06f2010-11-10 10:58:05 +00001231 },
Riki Oktariantocd19ba12008-02-04 23:41:58 -08001232 { applesmc_dmi_match, "Apple MacBook", {
Guenter Roeck2bfe8142010-11-10 10:42:21 -08001233 DMI_MATCH(DMI_BOARD_VENDOR, "Apple"),
1234 DMI_MATCH(DMI_PRODUCT_NAME, "MacBook") },
Henrik Rydberg40ef06f2010-11-10 10:58:05 +00001235 },
Nicolas Boichat6f2fad72007-05-08 00:24:52 -07001236 { applesmc_dmi_match, "Apple Macmini", {
Guenter Roeck2bfe8142010-11-10 10:42:21 -08001237 DMI_MATCH(DMI_BOARD_VENDOR, "Apple"),
1238 DMI_MATCH(DMI_PRODUCT_NAME, "Macmini") },
Henrik Rydberg40ef06f2010-11-10 10:58:05 +00001239 },
Henrik Rydberg45a3a362008-11-19 15:36:42 -08001240 { applesmc_dmi_match, "Apple MacPro", {
1241 DMI_MATCH(DMI_BOARD_VENDOR, "Apple"),
1242 DMI_MATCH(DMI_PRODUCT_NAME, "MacPro") },
Henrik Rydberg40ef06f2010-11-10 10:58:05 +00001243 },
Roberto De Ioris9f86f282008-08-15 00:40:30 -07001244 { applesmc_dmi_match, "Apple iMac", {
Guenter Roeck2bfe8142010-11-10 10:42:21 -08001245 DMI_MATCH(DMI_BOARD_VENDOR, "Apple"),
1246 DMI_MATCH(DMI_PRODUCT_NAME, "iMac") },
Henrik Rydberg40ef06f2010-11-10 10:58:05 +00001247 },
Nicolas Boichat6f2fad72007-05-08 00:24:52 -07001248 { .ident = NULL }
1249};
1250
1251static int __init applesmc_init(void)
1252{
1253 int ret;
Nicolas Boichat6f2fad72007-05-08 00:24:52 -07001254
Nicolas Boichat6f2fad72007-05-08 00:24:52 -07001255 if (!dmi_check_system(applesmc_whitelist)) {
Joe Perches1ee7c712010-11-09 15:15:03 +00001256 pr_warn("supported laptop not found!\n");
Nicolas Boichat6f2fad72007-05-08 00:24:52 -07001257 ret = -ENODEV;
1258 goto out;
1259 }
1260
1261 if (!request_region(APPLESMC_DATA_PORT, APPLESMC_NR_PORTS,
1262 "applesmc")) {
1263 ret = -ENXIO;
1264 goto out;
1265 }
1266
1267 ret = platform_driver_register(&applesmc_driver);
1268 if (ret)
1269 goto out_region;
1270
Jean Delvareddfbf2a2007-05-08 20:27:04 -07001271 pdev = platform_device_register_simple("applesmc", APPLESMC_DATA_PORT,
1272 NULL, 0);
Nicolas Boichat6f2fad72007-05-08 00:24:52 -07001273 if (IS_ERR(pdev)) {
1274 ret = PTR_ERR(pdev);
1275 goto out_driver;
1276 }
1277
Henrik Rydberg58745832010-11-10 10:58:03 +00001278 /* create register cache */
1279 ret = applesmc_init_smcreg();
Nicolas Boichat6996abf2007-05-27 22:17:43 +02001280 if (ret)
1281 goto out_device;
Nicolas Boichatfa744192007-05-23 13:58:13 -07001282
Henrik Rydberg0b0b5df2010-11-09 15:15:09 +00001283 ret = applesmc_create_nodes(info_group, 1);
Henrik Rydberg58745832010-11-10 10:58:03 +00001284 if (ret)
1285 goto out_smcreg;
1286
Henrik Rydberg3eba2bf2010-11-09 15:15:08 +00001287 ret = applesmc_create_nodes(fan_group, smcreg.fan_count);
1288 if (ret)
1289 goto out_info;
Nicolas Boichat6f2fad72007-05-08 00:24:52 -07001290
Henrik Rydberge30bca12012-07-16 09:18:11 +02001291 ret = applesmc_create_nodes(temp_group, smcreg.index_count);
Henrik Rydberg9792dad2010-11-10 10:58:04 +00001292 if (ret)
1293 goto out_fans;
Nicolas Boichat6f2fad72007-05-08 00:24:52 -07001294
Henrik Rydberg0b0b5df2010-11-09 15:15:09 +00001295 ret = applesmc_create_accelerometer();
1296 if (ret)
1297 goto out_temperature;
Nicolas Boichat6f2fad72007-05-08 00:24:52 -07001298
Henrik Rydberg0b0b5df2010-11-09 15:15:09 +00001299 ret = applesmc_create_light_sensor();
1300 if (ret)
1301 goto out_accelerometer;
Nicolas Boichat6f2fad72007-05-08 00:24:52 -07001302
Henrik Rydberg0b0b5df2010-11-09 15:15:09 +00001303 ret = applesmc_create_key_backlight();
1304 if (ret)
1305 goto out_light_sysfs;
Nicolas Boichat6f2fad72007-05-08 00:24:52 -07001306
Tony Jones1beeffe2007-08-20 13:46:20 -07001307 hwmon_dev = hwmon_device_register(&pdev->dev);
1308 if (IS_ERR(hwmon_dev)) {
1309 ret = PTR_ERR(hwmon_dev);
Nicolas Boichat6f2fad72007-05-08 00:24:52 -07001310 goto out_light_ledclass;
1311 }
1312
Nicolas Boichat6f2fad72007-05-08 00:24:52 -07001313 return 0;
1314
1315out_light_ledclass:
Henrik Rydberg0b0b5df2010-11-09 15:15:09 +00001316 applesmc_release_key_backlight();
Nicolas Boichat6f2fad72007-05-08 00:24:52 -07001317out_light_sysfs:
Henrik Rydberg0b0b5df2010-11-09 15:15:09 +00001318 applesmc_release_light_sensor();
Nicolas Boichat6f2fad72007-05-08 00:24:52 -07001319out_accelerometer:
Henrik Rydberg0b0b5df2010-11-09 15:15:09 +00001320 applesmc_release_accelerometer();
Nicolas Boichat6f2fad72007-05-08 00:24:52 -07001321out_temperature:
Henrik Rydberg9792dad2010-11-10 10:58:04 +00001322 applesmc_destroy_nodes(temp_group);
Henrik Rydberg0559a532010-05-11 09:17:47 +02001323out_fans:
Henrik Rydberg3eba2bf2010-11-09 15:15:08 +00001324 applesmc_destroy_nodes(fan_group);
1325out_info:
Henrik Rydberg0b0b5df2010-11-09 15:15:09 +00001326 applesmc_destroy_nodes(info_group);
Henrik Rydberg58745832010-11-10 10:58:03 +00001327out_smcreg:
1328 applesmc_destroy_smcreg();
Nicolas Boichat6f2fad72007-05-08 00:24:52 -07001329out_device:
1330 platform_device_unregister(pdev);
1331out_driver:
1332 platform_driver_unregister(&applesmc_driver);
1333out_region:
1334 release_region(APPLESMC_DATA_PORT, APPLESMC_NR_PORTS);
1335out:
Joe Perches1ee7c712010-11-09 15:15:03 +00001336 pr_warn("driver init failed (ret=%d)!\n", ret);
Nicolas Boichat6f2fad72007-05-08 00:24:52 -07001337 return ret;
1338}
1339
1340static void __exit applesmc_exit(void)
1341{
Tony Jones1beeffe2007-08-20 13:46:20 -07001342 hwmon_device_unregister(hwmon_dev);
Henrik Rydberg0b0b5df2010-11-09 15:15:09 +00001343 applesmc_release_key_backlight();
1344 applesmc_release_light_sensor();
1345 applesmc_release_accelerometer();
Henrik Rydberg9792dad2010-11-10 10:58:04 +00001346 applesmc_destroy_nodes(temp_group);
Henrik Rydberg3eba2bf2010-11-09 15:15:08 +00001347 applesmc_destroy_nodes(fan_group);
Henrik Rydberg0b0b5df2010-11-09 15:15:09 +00001348 applesmc_destroy_nodes(info_group);
Henrik Rydberg58745832010-11-10 10:58:03 +00001349 applesmc_destroy_smcreg();
Nicolas Boichat6f2fad72007-05-08 00:24:52 -07001350 platform_device_unregister(pdev);
1351 platform_driver_unregister(&applesmc_driver);
1352 release_region(APPLESMC_DATA_PORT, APPLESMC_NR_PORTS);
Nicolas Boichat6f2fad72007-05-08 00:24:52 -07001353}
1354
1355module_init(applesmc_init);
1356module_exit(applesmc_exit);
1357
1358MODULE_AUTHOR("Nicolas Boichat");
1359MODULE_DESCRIPTION("Apple SMC");
1360MODULE_LICENSE("GPL v2");
Henrik Rydbergdc924ef2008-12-01 13:13:49 -08001361MODULE_DEVICE_TABLE(dmi, applesmc_whitelist);