hwmon: (applesmc) Allow negative temperature values
[deliverable/linux.git] / drivers / hwmon / applesmc.c
CommitLineData
6f2fad74
NB
1/*
2 * drivers/hwmon/applesmc.c - driver for Apple's SMC (accelerometer, temperature
3 * sensors, fan control, keyboard backlight control) used in Intel-based Apple
4 * computers.
5 *
6 * Copyright (C) 2007 Nicolas Boichat <nicolas@boichat.ch>
41e71f97 7 * Copyright (C) 2010 Henrik Rydberg <rydberg@euromail.se>
6f2fad74
NB
8 *
9 * Based on hdaps.c driver:
10 * Copyright (C) 2005 Robert Love <rml@novell.com>
aa8521ec 11 * Copyright (C) 2005 Jesper Juhl <jj@chaosbits.net>
6f2fad74
NB
12 *
13 * Fan control based on smcFanControl:
14 * Copyright (C) 2006 Hendrik Holtmann <holtmann@mac.com>
15 *
16 * This program is free software; you can redistribute it and/or modify it
17 * under the terms of the GNU General Public License v2 as published by the
18 * Free Software Foundation.
19 *
20 * This program is distributed in the hope that it will be useful, but WITHOUT
21 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
22 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
23 * more details.
24 *
25 * You should have received a copy of the GNU General Public License along with
26 * this program; if not, write to the Free Software Foundation, Inc.,
27 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
28 */
29
1ee7c71b
JP
30#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
31
6f2fad74
NB
32#include <linux/delay.h>
33#include <linux/platform_device.h>
d5cf2b99 34#include <linux/input-polldev.h>
6f2fad74 35#include <linux/kernel.h>
5874583d 36#include <linux/slab.h>
6f2fad74
NB
37#include <linux/module.h>
38#include <linux/timer.h>
39#include <linux/dmi.h>
40#include <linux/mutex.h>
41#include <linux/hwmon-sysfs.h>
6055fae8 42#include <linux/io.h>
6f2fad74
NB
43#include <linux/leds.h>
44#include <linux/hwmon.h>
45#include <linux/workqueue.h>
46
47/* data port used by Apple SMC */
48#define APPLESMC_DATA_PORT 0x300
49/* command/status port used by Apple SMC */
50#define APPLESMC_CMD_PORT 0x304
51
52#define APPLESMC_NR_PORTS 32 /* 0x300-0x31f */
53
54#define APPLESMC_MAX_DATA_LENGTH 32
55
5874583d 56/* wait up to 32 ms for a status change. */
a332bf9a 57#define APPLESMC_MIN_WAIT 0x0010
8c9398d1
HR
58#define APPLESMC_MAX_WAIT 0x8000
59
6f2fad74
NB
60#define APPLESMC_STATUS_MASK 0x0f
61#define APPLESMC_READ_CMD 0x10
62#define APPLESMC_WRITE_CMD 0x11
63#define APPLESMC_GET_KEY_BY_INDEX_CMD 0x12
64#define APPLESMC_GET_KEY_TYPE_CMD 0x13
65
66#define KEY_COUNT_KEY "#KEY" /* r-o ui32 */
67
8bd1a12a
HR
68#define LIGHT_SENSOR_LEFT_KEY "ALV0" /* r-o {alv (6-10 bytes) */
69#define LIGHT_SENSOR_RIGHT_KEY "ALV1" /* r-o {alv (6-10 bytes) */
d5cf2b99 70#define BACKLIGHT_KEY "LKSB" /* w-o {lkb (2 bytes) */
6f2fad74 71
d5cf2b99 72#define CLAMSHELL_KEY "MSLD" /* r-o ui8 (unused) */
6f2fad74
NB
73
74#define MOTION_SENSOR_X_KEY "MO_X" /* r-o sp78 (2 bytes) */
75#define MOTION_SENSOR_Y_KEY "MO_Y" /* r-o sp78 (2 bytes) */
76#define MOTION_SENSOR_Z_KEY "MO_Z" /* r-o sp78 (2 bytes) */
77#define MOTION_SENSOR_KEY "MOCN" /* r/w ui16 */
78
79#define FANS_COUNT "FNum" /* r-o ui8 */
80#define FANS_MANUAL "FS! " /* r-w ui16 */
3eba2bf7 81#define FAN_ID_FMT "F%dID" /* r-o char[16] */
6f2fad74 82
b6e5122f
HR
83#define TEMP_SENSOR_TYPE "sp78"
84
6f2fad74 85/* List of keys used to read/write fan speeds */
3eba2bf7
HR
86static const char *const fan_speed_fmt[] = {
87 "F%dAc", /* actual speed */
88 "F%dMn", /* minimum speed (rw) */
89 "F%dMx", /* maximum speed */
90 "F%dSf", /* safe speed - not all models */
91 "F%dTg", /* target speed (manual: rw) */
6f2fad74
NB
92};
93
94#define INIT_TIMEOUT_MSECS 5000 /* wait up to 5s for device init ... */
95#define INIT_WAIT_MSECS 50 /* ... in 50ms increments */
96
d5cf2b99 97#define APPLESMC_POLL_INTERVAL 50 /* msecs */
6f2fad74
NB
98#define APPLESMC_INPUT_FUZZ 4 /* input event threshold */
99#define APPLESMC_INPUT_FLAT 4
100
3eba2bf7
HR
101#define to_index(attr) (to_sensor_dev_attr(attr)->index & 0xffff)
102#define to_option(attr) (to_sensor_dev_attr(attr)->index >> 16)
9792dadf 103
9792dadf
HR
104/* Dynamic device node attributes */
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 */
3eba2bf7 115 int option; /* function argument */
9792dadf
HR
116 struct applesmc_dev_attr *nodes; /* dynamic node array */
117};
118
5874583d
HR
119/* AppleSMC entry - cached register information */
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 */
3eba2bf7 132 unsigned int fan_count; /* number of fans */
9792dadf
HR
133 unsigned int temp_count; /* number of temperature registers */
134 unsigned int temp_begin; /* temperature lower index bound */
135 unsigned int temp_end; /* temperature upper index bound */
40ef06f1
HR
136 int num_light_sensors; /* number of light sensors */
137 bool has_accelerometer; /* has motion sensor */
138 bool has_key_backlight; /* has keyboard backlight */
5874583d
HR
139 bool init_complete; /* true when fully initialized */
140 struct applesmc_entry *cache; /* cached key entries */
141} smcreg = {
142 .mutex = __MUTEX_INITIALIZER(smcreg.mutex),
143};
144
6f2fad74
NB
145static const int debug;
146static struct platform_device *pdev;
147static s16 rest_x;
148static s16 rest_y;
a976f150
HR
149static u8 backlight_state[2];
150
1beeffe4 151static struct device *hwmon_dev;
d5cf2b99 152static struct input_polled_dev *applesmc_idev;
6f2fad74 153
6f2fad74
NB
154/*
155 * Last index written to key_at_index sysfs file, and value to use for all other
156 * key_at_index_* sysfs files.
157 */
158static unsigned int key_at_index;
159
160static struct workqueue_struct *applesmc_led_wq;
161
162/*
8c9398d1 163 * __wait_status - Wait up to 32ms for the status port to get a certain value
6f2fad74
NB
164 * (masked with 0x0f), returning zero if the value is obtained. Callers must
165 * hold applesmc_lock.
166 */
167static int __wait_status(u8 val)
168{
8c9398d1 169 int us;
6f2fad74
NB
170
171 val = val & APPLESMC_STATUS_MASK;
172
8c9398d1
HR
173 for (us = APPLESMC_MIN_WAIT; us < APPLESMC_MAX_WAIT; us <<= 1) {
174 udelay(us);
2bfe8148 175 if ((inb(APPLESMC_CMD_PORT) & APPLESMC_STATUS_MASK) == val)
6f2fad74 176 return 0;
6f2fad74
NB
177 }
178
6f2fad74
NB
179 return -EIO;
180}
181
84d2d7f2
HR
182/*
183 * special treatment of command port - on newer macbooks, it seems necessary
184 * to resend the command byte before polling the status again. Callers must
185 * hold applesmc_lock.
186 */
187static int send_command(u8 cmd)
188{
8c9398d1
HR
189 int us;
190 for (us = APPLESMC_MIN_WAIT; us < APPLESMC_MAX_WAIT; us <<= 1) {
84d2d7f2 191 outb(cmd, APPLESMC_CMD_PORT);
8c9398d1 192 udelay(us);
84d2d7f2
HR
193 if ((inb(APPLESMC_CMD_PORT) & APPLESMC_STATUS_MASK) == 0x0c)
194 return 0;
84d2d7f2 195 }
84d2d7f2
HR
196 return -EIO;
197}
198
5874583d 199static int send_argument(const char *key)
6f2fad74
NB
200{
201 int i;
202
6f2fad74
NB
203 for (i = 0; i < 4; i++) {
204 outb(key[i], APPLESMC_DATA_PORT);
205 if (__wait_status(0x04))
206 return -EIO;
207 }
5874583d
HR
208 return 0;
209}
210
211static int read_smc(u8 cmd, const char *key, u8 *buffer, u8 len)
212{
213 int i;
214
215 if (send_command(cmd) || send_argument(key)) {
ac852edb 216 pr_warn("%.4s: read arg fail\n", key);
5874583d
HR
217 return -EIO;
218 }
6f2fad74
NB
219
220 outb(len, APPLESMC_DATA_PORT);
6f2fad74
NB
221
222 for (i = 0; i < len; i++) {
5874583d 223 if (__wait_status(0x05)) {
ac852edb 224 pr_warn("%.4s: read data fail\n", key);
6f2fad74 225 return -EIO;
5874583d 226 }
6f2fad74 227 buffer[i] = inb(APPLESMC_DATA_PORT);
6f2fad74 228 }
6f2fad74
NB
229
230 return 0;
231}
232
5874583d 233static int write_smc(u8 cmd, const char *key, const u8 *buffer, u8 len)
6f2fad74
NB
234{
235 int i;
236
5874583d
HR
237 if (send_command(cmd) || send_argument(key)) {
238 pr_warn("%s: write arg fail\n", key);
6f2fad74 239 return -EIO;
6f2fad74
NB
240 }
241
242 outb(len, APPLESMC_DATA_PORT);
243
244 for (i = 0; i < len; i++) {
5874583d
HR
245 if (__wait_status(0x04)) {
246 pr_warn("%s: write data fail\n", key);
6f2fad74 247 return -EIO;
5874583d 248 }
6f2fad74
NB
249 outb(buffer[i], APPLESMC_DATA_PORT);
250 }
251
252 return 0;
253}
254
5874583d
HR
255static int read_register_count(unsigned int *count)
256{
257 __be32 be;
258 int ret;
259
260 ret = read_smc(APPLESMC_READ_CMD, KEY_COUNT_KEY, (u8 *)&be, 4);
261 if (ret)
262 return ret;
263
264 *count = be32_to_cpu(be);
265 return 0;
266}
267
6f2fad74 268/*
5874583d
HR
269 * Serialized I/O
270 *
271 * Returns zero on success or a negative error on failure.
272 * All functions below are concurrency safe - callers should NOT hold lock.
6f2fad74 273 */
5874583d
HR
274
275static int applesmc_read_entry(const struct applesmc_entry *entry,
276 u8 *buf, u8 len)
6f2fad74 277{
5874583d 278 int ret;
6f2fad74 279
5874583d
HR
280 if (entry->len != len)
281 return -EINVAL;
282 mutex_lock(&smcreg.mutex);
283 ret = read_smc(APPLESMC_READ_CMD, entry->key, buf, len);
284 mutex_unlock(&smcreg.mutex);
6f2fad74 285
5874583d
HR
286 return ret;
287}
288
289static int applesmc_write_entry(const struct applesmc_entry *entry,
290 const u8 *buf, u8 len)
291{
292 int ret;
293
294 if (entry->len != len)
295 return -EINVAL;
296 mutex_lock(&smcreg.mutex);
297 ret = write_smc(APPLESMC_WRITE_CMD, entry->key, buf, len);
298 mutex_unlock(&smcreg.mutex);
299 return ret;
300}
301
302static const struct applesmc_entry *applesmc_get_entry_by_index(int index)
303{
304 struct applesmc_entry *cache = &smcreg.cache[index];
305 u8 key[4], info[6];
306 __be32 be;
307 int ret = 0;
308
309 if (cache->valid)
310 return cache;
311
312 mutex_lock(&smcreg.mutex);
313
314 if (cache->valid)
315 goto out;
316 be = cpu_to_be32(index);
317 ret = read_smc(APPLESMC_GET_KEY_BY_INDEX_CMD, (u8 *)&be, key, 4);
318 if (ret)
319 goto out;
320 ret = read_smc(APPLESMC_GET_KEY_TYPE_CMD, key, info, 6);
321 if (ret)
322 goto out;
323
324 memcpy(cache->key, key, 4);
325 cache->len = info[0];
326 memcpy(cache->type, &info[1], 4);
327 cache->flags = info[5];
328 cache->valid = 1;
329
330out:
331 mutex_unlock(&smcreg.mutex);
332 if (ret)
333 return ERR_PTR(ret);
334 return cache;
335}
336
337static int applesmc_get_lower_bound(unsigned int *lo, const char *key)
338{
339 int begin = 0, end = smcreg.key_count;
340 const struct applesmc_entry *entry;
341
342 while (begin != end) {
343 int middle = begin + (end - begin) / 2;
344 entry = applesmc_get_entry_by_index(middle);
0fc86eca
HR
345 if (IS_ERR(entry)) {
346 *lo = 0;
5874583d 347 return PTR_ERR(entry);
0fc86eca 348 }
5874583d
HR
349 if (strcmp(entry->key, key) < 0)
350 begin = middle + 1;
351 else
352 end = middle;
6f2fad74
NB
353 }
354
5874583d
HR
355 *lo = begin;
356 return 0;
357}
6f2fad74 358
5874583d
HR
359static int applesmc_get_upper_bound(unsigned int *hi, const char *key)
360{
361 int begin = 0, end = smcreg.key_count;
362 const struct applesmc_entry *entry;
363
364 while (begin != end) {
365 int middle = begin + (end - begin) / 2;
366 entry = applesmc_get_entry_by_index(middle);
0fc86eca
HR
367 if (IS_ERR(entry)) {
368 *hi = smcreg.key_count;
5874583d 369 return PTR_ERR(entry);
0fc86eca 370 }
5874583d
HR
371 if (strcmp(key, entry->key) < 0)
372 end = middle;
373 else
374 begin = middle + 1;
6f2fad74 375 }
6f2fad74 376
5874583d 377 *hi = begin;
6f2fad74
NB
378 return 0;
379}
380
5874583d 381static const struct applesmc_entry *applesmc_get_entry_by_key(const char *key)
6f2fad74 382{
5874583d
HR
383 int begin, end;
384 int ret;
6f2fad74 385
5874583d
HR
386 ret = applesmc_get_lower_bound(&begin, key);
387 if (ret)
388 return ERR_PTR(ret);
389 ret = applesmc_get_upper_bound(&end, key);
390 if (ret)
391 return ERR_PTR(ret);
392 if (end - begin != 1)
393 return ERR_PTR(-EINVAL);
6f2fad74 394
5874583d
HR
395 return applesmc_get_entry_by_index(begin);
396}
6f2fad74 397
5874583d
HR
398static int applesmc_read_key(const char *key, u8 *buffer, u8 len)
399{
400 const struct applesmc_entry *entry;
6f2fad74 401
5874583d
HR
402 entry = applesmc_get_entry_by_key(key);
403 if (IS_ERR(entry))
404 return PTR_ERR(entry);
6f2fad74 405
5874583d
HR
406 return applesmc_read_entry(entry, buffer, len);
407}
408
409static int applesmc_write_key(const char *key, const u8 *buffer, u8 len)
410{
411 const struct applesmc_entry *entry;
412
413 entry = applesmc_get_entry_by_key(key);
414 if (IS_ERR(entry))
415 return PTR_ERR(entry);
416
417 return applesmc_write_entry(entry, buffer, len);
6f2fad74
NB
418}
419
40ef06f1
HR
420static int applesmc_has_key(const char *key, bool *value)
421{
422 const struct applesmc_entry *entry;
423
424 entry = applesmc_get_entry_by_key(key);
425 if (IS_ERR(entry) && PTR_ERR(entry) != -EINVAL)
426 return PTR_ERR(entry);
427
428 *value = !IS_ERR(entry);
429 return 0;
430}
431
6f2fad74 432/*
edf48f3a 433 * applesmc_read_s16 - Read 16-bit signed big endian register
6f2fad74 434 */
edf48f3a 435static int applesmc_read_s16(const char *key, s16 *value)
6f2fad74
NB
436{
437 u8 buffer[2];
438 int ret;
439
edf48f3a
HR
440 ret = applesmc_read_key(key, buffer, 2);
441 if (ret)
442 return ret;
6f2fad74
NB
443
444 *value = ((s16)buffer[0] << 8) | buffer[1];
edf48f3a 445 return 0;
6f2fad74
NB
446}
447
448/*
2344cd0c 449 * applesmc_device_init - initialize the accelerometer. Can sleep.
6f2fad74 450 */
2344cd0c 451static void applesmc_device_init(void)
6f2fad74 452{
2344cd0c 453 int total;
6f2fad74
NB
454 u8 buffer[2];
455
40ef06f1 456 if (!smcreg.has_accelerometer)
2344cd0c 457 return;
6f2fad74 458
6f2fad74 459 for (total = INIT_TIMEOUT_MSECS; total > 0; total -= INIT_WAIT_MSECS) {
6f2fad74 460 if (!applesmc_read_key(MOTION_SENSOR_KEY, buffer, 2) &&
2344cd0c 461 (buffer[0] != 0x00 || buffer[1] != 0x00))
5874583d 462 return;
6f2fad74
NB
463 buffer[0] = 0xe0;
464 buffer[1] = 0x00;
465 applesmc_write_key(MOTION_SENSOR_KEY, buffer, 2);
466 msleep(INIT_WAIT_MSECS);
467 }
468
1ee7c71b 469 pr_warn("failed to init the device\n");
6f2fad74
NB
470}
471
5874583d
HR
472/*
473 * applesmc_init_smcreg_try - Try to initialize register cache. Idempotent.
474 */
475static int applesmc_init_smcreg_try(void)
476{
477 struct applesmc_registers *s = &smcreg;
40ef06f1 478 bool left_light_sensor, right_light_sensor;
3eba2bf7 479 u8 tmp[1];
5874583d
HR
480 int ret;
481
482 if (s->init_complete)
483 return 0;
484
485 ret = read_register_count(&s->key_count);
486 if (ret)
487 return ret;
488
489 if (!s->cache)
490 s->cache = kcalloc(s->key_count, sizeof(*s->cache), GFP_KERNEL);
491 if (!s->cache)
492 return -ENOMEM;
493
3eba2bf7
HR
494 ret = applesmc_read_key(FANS_COUNT, tmp, 1);
495 if (ret)
496 return ret;
497 s->fan_count = tmp[0];
498
9792dadf
HR
499 ret = applesmc_get_lower_bound(&s->temp_begin, "T");
500 if (ret)
501 return ret;
502 ret = applesmc_get_lower_bound(&s->temp_end, "U");
503 if (ret)
504 return ret;
505 s->temp_count = s->temp_end - s->temp_begin;
506
40ef06f1
HR
507 ret = applesmc_has_key(LIGHT_SENSOR_LEFT_KEY, &left_light_sensor);
508 if (ret)
509 return ret;
510 ret = applesmc_has_key(LIGHT_SENSOR_RIGHT_KEY, &right_light_sensor);
511 if (ret)
512 return ret;
513 ret = applesmc_has_key(MOTION_SENSOR_KEY, &s->has_accelerometer);
514 if (ret)
515 return ret;
516 ret = applesmc_has_key(BACKLIGHT_KEY, &s->has_key_backlight);
517 if (ret)
518 return ret;
519
520 s->num_light_sensors = left_light_sensor + right_light_sensor;
5874583d
HR
521 s->init_complete = true;
522
3eba2bf7
HR
523 pr_info("key=%d fan=%d temp=%d acc=%d lux=%d kbd=%d\n",
524 s->key_count, s->fan_count, s->temp_count,
40ef06f1
HR
525 s->has_accelerometer,
526 s->num_light_sensors,
527 s->has_key_backlight);
5874583d
HR
528
529 return 0;
530}
531
532/*
533 * applesmc_init_smcreg - Initialize register cache.
534 *
535 * Retries until initialization is successful, or the operation times out.
536 *
537 */
538static int applesmc_init_smcreg(void)
539{
540 int ms, ret;
541
542 for (ms = 0; ms < INIT_TIMEOUT_MSECS; ms += INIT_WAIT_MSECS) {
543 ret = applesmc_init_smcreg_try();
544 if (!ret) {
545 if (ms)
546 pr_info("init_smcreg() took %d ms\n", ms);
547 return 0;
548 }
549 msleep(INIT_WAIT_MSECS);
550 }
551
552 kfree(smcreg.cache);
553 smcreg.cache = NULL;
554
555 return ret;
556}
557
558static void applesmc_destroy_smcreg(void)
559{
560 kfree(smcreg.cache);
561 smcreg.cache = NULL;
562 smcreg.init_complete = false;
563}
564
6f2fad74
NB
565/* Device model stuff */
566static int applesmc_probe(struct platform_device *dev)
567{
5874583d
HR
568 int ret;
569
570 ret = applesmc_init_smcreg();
571 if (ret)
572 return ret;
573
2344cd0c 574 applesmc_device_init();
6f2fad74 575
6f2fad74
NB
576 return 0;
577}
578
a976f150
HR
579/* Synchronize device with memorized backlight state */
580static int applesmc_pm_resume(struct device *dev)
581{
40ef06f1 582 if (smcreg.has_key_backlight)
a976f150 583 applesmc_write_key(BACKLIGHT_KEY, backlight_state, 2);
a976f150
HR
584 return 0;
585}
586
587/* Reinitialize device on resume from hibernation */
588static int applesmc_pm_restore(struct device *dev)
6f2fad74 589{
2344cd0c 590 applesmc_device_init();
a976f150 591 return applesmc_pm_resume(dev);
6f2fad74
NB
592}
593
47145210 594static const struct dev_pm_ops applesmc_pm_ops = {
a976f150
HR
595 .resume = applesmc_pm_resume,
596 .restore = applesmc_pm_restore,
597};
598
6f2fad74
NB
599static struct platform_driver applesmc_driver = {
600 .probe = applesmc_probe,
6f2fad74
NB
601 .driver = {
602 .name = "applesmc",
603 .owner = THIS_MODULE,
a976f150 604 .pm = &applesmc_pm_ops,
6f2fad74
NB
605 },
606};
607
608/*
609 * applesmc_calibrate - Set our "resting" values. Callers must
610 * hold applesmc_lock.
611 */
612static void applesmc_calibrate(void)
613{
edf48f3a
HR
614 applesmc_read_s16(MOTION_SENSOR_X_KEY, &rest_x);
615 applesmc_read_s16(MOTION_SENSOR_Y_KEY, &rest_y);
6f2fad74
NB
616 rest_x = -rest_x;
617}
618
d5cf2b99 619static void applesmc_idev_poll(struct input_polled_dev *dev)
6f2fad74 620{
d5cf2b99 621 struct input_dev *idev = dev->input;
6f2fad74
NB
622 s16 x, y;
623
edf48f3a 624 if (applesmc_read_s16(MOTION_SENSOR_X_KEY, &x))
5874583d 625 return;
edf48f3a 626 if (applesmc_read_s16(MOTION_SENSOR_Y_KEY, &y))
5874583d 627 return;
6f2fad74
NB
628
629 x = -x;
d5cf2b99
DT
630 input_report_abs(idev, ABS_X, x - rest_x);
631 input_report_abs(idev, ABS_Y, y - rest_y);
632 input_sync(idev);
6f2fad74
NB
633}
634
635/* Sysfs Files */
636
fa74419b
NB
637static ssize_t applesmc_name_show(struct device *dev,
638 struct device_attribute *attr, char *buf)
639{
640 return snprintf(buf, PAGE_SIZE, "applesmc\n");
641}
642
6f2fad74
NB
643static ssize_t applesmc_position_show(struct device *dev,
644 struct device_attribute *attr, char *buf)
645{
646 int ret;
647 s16 x, y, z;
648
edf48f3a 649 ret = applesmc_read_s16(MOTION_SENSOR_X_KEY, &x);
6f2fad74
NB
650 if (ret)
651 goto out;
edf48f3a 652 ret = applesmc_read_s16(MOTION_SENSOR_Y_KEY, &y);
6f2fad74
NB
653 if (ret)
654 goto out;
edf48f3a 655 ret = applesmc_read_s16(MOTION_SENSOR_Z_KEY, &z);
6f2fad74
NB
656 if (ret)
657 goto out;
658
659out:
6f2fad74
NB
660 if (ret)
661 return ret;
662 else
663 return snprintf(buf, PAGE_SIZE, "(%d,%d,%d)\n", x, y, z);
664}
665
666static ssize_t applesmc_light_show(struct device *dev,
667 struct device_attribute *attr, char *sysfsbuf)
668{
5874583d 669 const struct applesmc_entry *entry;
8bd1a12a 670 static int data_length;
6f2fad74
NB
671 int ret;
672 u8 left = 0, right = 0;
5874583d 673 u8 buffer[10];
6f2fad74 674
8bd1a12a 675 if (!data_length) {
5874583d
HR
676 entry = applesmc_get_entry_by_key(LIGHT_SENSOR_LEFT_KEY);
677 if (IS_ERR(entry))
678 return PTR_ERR(entry);
679 if (entry->len > 10)
680 return -ENXIO;
681 data_length = entry->len;
1ee7c71b 682 pr_info("light sensor data length set to %d\n", data_length);
8bd1a12a
HR
683 }
684
685 ret = applesmc_read_key(LIGHT_SENSOR_LEFT_KEY, buffer, data_length);
c3d6362b
AM
686 /* newer macbooks report a single 10-bit bigendian value */
687 if (data_length == 10) {
688 left = be16_to_cpu(*(__be16 *)(buffer + 6)) >> 2;
689 goto out;
690 }
6f2fad74
NB
691 left = buffer[2];
692 if (ret)
693 goto out;
8bd1a12a 694 ret = applesmc_read_key(LIGHT_SENSOR_RIGHT_KEY, buffer, data_length);
6f2fad74
NB
695 right = buffer[2];
696
697out:
6f2fad74
NB
698 if (ret)
699 return ret;
700 else
701 return snprintf(sysfsbuf, PAGE_SIZE, "(%d,%d)\n", left, right);
702}
703
fa5575cf
AM
704/* Displays sensor key as label */
705static ssize_t applesmc_show_sensor_label(struct device *dev,
706 struct device_attribute *devattr, char *sysfsbuf)
707{
9792dadf
HR
708 int index = smcreg.temp_begin + to_index(devattr);
709 const struct applesmc_entry *entry;
fa5575cf 710
9792dadf
HR
711 entry = applesmc_get_entry_by_index(index);
712 if (IS_ERR(entry))
713 return PTR_ERR(entry);
714
715 return snprintf(sysfsbuf, PAGE_SIZE, "%s\n", entry->key);
fa5575cf
AM
716}
717
6f2fad74
NB
718/* Displays degree Celsius * 1000 */
719static ssize_t applesmc_show_temperature(struct device *dev,
720 struct device_attribute *devattr, char *sysfsbuf)
721{
9792dadf
HR
722 int index = smcreg.temp_begin + to_index(devattr);
723 const struct applesmc_entry *entry;
6f2fad74 724 int ret;
b6e5122f
HR
725 s16 value;
726 int temp;
6f2fad74 727
9792dadf
HR
728 entry = applesmc_get_entry_by_index(index);
729 if (IS_ERR(entry))
730 return PTR_ERR(entry);
b6e5122f 731 if (strcmp(entry->type, TEMP_SENSOR_TYPE))
dcdea261 732 return -EINVAL;
6f2fad74 733
b6e5122f 734 ret = applesmc_read_s16(entry->key, &value);
6f2fad74
NB
735 if (ret)
736 return ret;
9792dadf 737
b6e5122f 738 temp = 250 * (value >> 6);
9792dadf 739
b6e5122f 740 return snprintf(sysfsbuf, PAGE_SIZE, "%d\n", temp);
6f2fad74
NB
741}
742
743static ssize_t applesmc_show_fan_speed(struct device *dev,
744 struct device_attribute *attr, char *sysfsbuf)
745{
746 int ret;
747 unsigned int speed = 0;
748 char newkey[5];
749 u8 buffer[2];
6f2fad74 750
3eba2bf7 751 sprintf(newkey, fan_speed_fmt[to_option(attr)], to_index(attr));
6f2fad74 752
6f2fad74
NB
753 ret = applesmc_read_key(newkey, buffer, 2);
754 speed = ((buffer[0] << 8 | buffer[1]) >> 2);
755
6f2fad74
NB
756 if (ret)
757 return ret;
758 else
759 return snprintf(sysfsbuf, PAGE_SIZE, "%u\n", speed);
760}
761
762static ssize_t applesmc_store_fan_speed(struct device *dev,
763 struct device_attribute *attr,
764 const char *sysfsbuf, size_t count)
765{
766 int ret;
2bfe8148 767 unsigned long speed;
6f2fad74
NB
768 char newkey[5];
769 u8 buffer[2];
6f2fad74 770
179c4fdb 771 if (kstrtoul(sysfsbuf, 10, &speed) < 0 || speed >= 0x4000)
2bfe8148 772 return -EINVAL; /* Bigger than a 14-bit value */
6f2fad74 773
3eba2bf7 774 sprintf(newkey, fan_speed_fmt[to_option(attr)], to_index(attr));
6f2fad74 775
6f2fad74
NB
776 buffer[0] = (speed >> 6) & 0xff;
777 buffer[1] = (speed << 2) & 0xff;
778 ret = applesmc_write_key(newkey, buffer, 2);
779
6f2fad74
NB
780 if (ret)
781 return ret;
782 else
783 return count;
784}
785
786static ssize_t applesmc_show_fan_manual(struct device *dev,
3eba2bf7 787 struct device_attribute *attr, char *sysfsbuf)
6f2fad74
NB
788{
789 int ret;
790 u16 manual = 0;
791 u8 buffer[2];
6f2fad74 792
6f2fad74 793 ret = applesmc_read_key(FANS_MANUAL, buffer, 2);
3eba2bf7 794 manual = ((buffer[0] << 8 | buffer[1]) >> to_index(attr)) & 0x01;
6f2fad74 795
6f2fad74
NB
796 if (ret)
797 return ret;
798 else
799 return snprintf(sysfsbuf, PAGE_SIZE, "%d\n", manual);
800}
801
802static ssize_t applesmc_store_fan_manual(struct device *dev,
3eba2bf7 803 struct device_attribute *attr,
6f2fad74
NB
804 const char *sysfsbuf, size_t count)
805{
806 int ret;
807 u8 buffer[2];
2bfe8148 808 unsigned long input;
6f2fad74 809 u16 val;
6f2fad74 810
179c4fdb 811 if (kstrtoul(sysfsbuf, 10, &input) < 0)
2bfe8148 812 return -EINVAL;
6f2fad74 813
6f2fad74
NB
814 ret = applesmc_read_key(FANS_MANUAL, buffer, 2);
815 val = (buffer[0] << 8 | buffer[1]);
816 if (ret)
817 goto out;
818
819 if (input)
3eba2bf7 820 val = val | (0x01 << to_index(attr));
6f2fad74 821 else
3eba2bf7 822 val = val & ~(0x01 << to_index(attr));
6f2fad74
NB
823
824 buffer[0] = (val >> 8) & 0xFF;
825 buffer[1] = val & 0xFF;
826
827 ret = applesmc_write_key(FANS_MANUAL, buffer, 2);
828
829out:
6f2fad74
NB
830 if (ret)
831 return ret;
832 else
833 return count;
834}
835
836static ssize_t applesmc_show_fan_position(struct device *dev,
837 struct device_attribute *attr, char *sysfsbuf)
838{
839 int ret;
840 char newkey[5];
841 u8 buffer[17];
6f2fad74 842
3eba2bf7 843 sprintf(newkey, FAN_ID_FMT, to_index(attr));
6f2fad74 844
6f2fad74
NB
845 ret = applesmc_read_key(newkey, buffer, 16);
846 buffer[16] = 0;
847
6f2fad74
NB
848 if (ret)
849 return ret;
850 else
851 return snprintf(sysfsbuf, PAGE_SIZE, "%s\n", buffer+4);
852}
853
854static ssize_t applesmc_calibrate_show(struct device *dev,
855 struct device_attribute *attr, char *sysfsbuf)
856{
857 return snprintf(sysfsbuf, PAGE_SIZE, "(%d,%d)\n", rest_x, rest_y);
858}
859
860static ssize_t applesmc_calibrate_store(struct device *dev,
861 struct device_attribute *attr, const char *sysfsbuf, size_t count)
862{
6f2fad74 863 applesmc_calibrate();
6f2fad74
NB
864
865 return count;
866}
867
6f2fad74
NB
868static void applesmc_backlight_set(struct work_struct *work)
869{
a976f150 870 applesmc_write_key(BACKLIGHT_KEY, backlight_state, 2);
6f2fad74
NB
871}
872static DECLARE_WORK(backlight_work, &applesmc_backlight_set);
873
874static void applesmc_brightness_set(struct led_classdev *led_cdev,
875 enum led_brightness value)
876{
877 int ret;
878
a976f150 879 backlight_state[0] = value;
6f2fad74
NB
880 ret = queue_work(applesmc_led_wq, &backlight_work);
881
882 if (debug && (!ret))
883 printk(KERN_DEBUG "applesmc: work was already on the queue.\n");
884}
885
886static ssize_t applesmc_key_count_show(struct device *dev,
887 struct device_attribute *attr, char *sysfsbuf)
888{
889 int ret;
890 u8 buffer[4];
891 u32 count;
892
6f2fad74
NB
893 ret = applesmc_read_key(KEY_COUNT_KEY, buffer, 4);
894 count = ((u32)buffer[0]<<24) + ((u32)buffer[1]<<16) +
895 ((u32)buffer[2]<<8) + buffer[3];
896
6f2fad74
NB
897 if (ret)
898 return ret;
899 else
900 return snprintf(sysfsbuf, PAGE_SIZE, "%d\n", count);
901}
902
903static ssize_t applesmc_key_at_index_read_show(struct device *dev,
904 struct device_attribute *attr, char *sysfsbuf)
905{
5874583d 906 const struct applesmc_entry *entry;
6f2fad74
NB
907 int ret;
908
5874583d
HR
909 entry = applesmc_get_entry_by_index(key_at_index);
910 if (IS_ERR(entry))
911 return PTR_ERR(entry);
912 ret = applesmc_read_entry(entry, sysfsbuf, entry->len);
913 if (ret)
6f2fad74 914 return ret;
6f2fad74 915
5874583d 916 return entry->len;
6f2fad74
NB
917}
918
919static ssize_t applesmc_key_at_index_data_length_show(struct device *dev,
920 struct device_attribute *attr, char *sysfsbuf)
921{
5874583d 922 const struct applesmc_entry *entry;
6f2fad74 923
5874583d
HR
924 entry = applesmc_get_entry_by_index(key_at_index);
925 if (IS_ERR(entry))
926 return PTR_ERR(entry);
6f2fad74 927
5874583d 928 return snprintf(sysfsbuf, PAGE_SIZE, "%d\n", entry->len);
6f2fad74
NB
929}
930
931static ssize_t applesmc_key_at_index_type_show(struct device *dev,
932 struct device_attribute *attr, char *sysfsbuf)
933{
5874583d 934 const struct applesmc_entry *entry;
6f2fad74 935
5874583d
HR
936 entry = applesmc_get_entry_by_index(key_at_index);
937 if (IS_ERR(entry))
938 return PTR_ERR(entry);
6f2fad74 939
5874583d 940 return snprintf(sysfsbuf, PAGE_SIZE, "%s\n", entry->type);
6f2fad74
NB
941}
942
943static ssize_t applesmc_key_at_index_name_show(struct device *dev,
944 struct device_attribute *attr, char *sysfsbuf)
945{
5874583d 946 const struct applesmc_entry *entry;
6f2fad74 947
5874583d
HR
948 entry = applesmc_get_entry_by_index(key_at_index);
949 if (IS_ERR(entry))
950 return PTR_ERR(entry);
6f2fad74 951
5874583d 952 return snprintf(sysfsbuf, PAGE_SIZE, "%s\n", entry->key);
6f2fad74
NB
953}
954
955static ssize_t applesmc_key_at_index_show(struct device *dev,
956 struct device_attribute *attr, char *sysfsbuf)
957{
958 return snprintf(sysfsbuf, PAGE_SIZE, "%d\n", key_at_index);
959}
960
961static ssize_t applesmc_key_at_index_store(struct device *dev,
962 struct device_attribute *attr, const char *sysfsbuf, size_t count)
963{
5874583d 964 unsigned long newkey;
6f2fad74 965
179c4fdb 966 if (kstrtoul(sysfsbuf, 10, &newkey) < 0
5874583d
HR
967 || newkey >= smcreg.key_count)
968 return -EINVAL;
6f2fad74 969
5874583d 970 key_at_index = newkey;
6f2fad74
NB
971 return count;
972}
973
974static struct led_classdev applesmc_backlight = {
6c152bee 975 .name = "smc::kbd_backlight",
6f2fad74
NB
976 .default_trigger = "nand-disk",
977 .brightness_set = applesmc_brightness_set,
978};
979
0b0b5dff
HR
980static struct applesmc_node_group info_group[] = {
981 { "name", applesmc_name_show },
982 { "key_count", applesmc_key_count_show },
983 { "key_at_index", applesmc_key_at_index_show, applesmc_key_at_index_store },
984 { "key_at_index_name", applesmc_key_at_index_name_show },
985 { "key_at_index_type", applesmc_key_at_index_type_show },
986 { "key_at_index_data_length", applesmc_key_at_index_data_length_show },
987 { "key_at_index_data", applesmc_key_at_index_read_show },
988 { }
6f2fad74
NB
989};
990
0b0b5dff
HR
991static struct applesmc_node_group accelerometer_group[] = {
992 { "position", applesmc_position_show },
993 { "calibrate", applesmc_calibrate_show, applesmc_calibrate_store },
994 { }
6f2fad74
NB
995};
996
0b0b5dff
HR
997static struct applesmc_node_group light_sensor_group[] = {
998 { "light", applesmc_light_show },
999 { }
1000};
6f2fad74 1001
3eba2bf7
HR
1002static struct applesmc_node_group fan_group[] = {
1003 { "fan%d_label", applesmc_show_fan_position },
1004 { "fan%d_input", applesmc_show_fan_speed, NULL, 0 },
1005 { "fan%d_min", applesmc_show_fan_speed, applesmc_store_fan_speed, 1 },
1006 { "fan%d_max", applesmc_show_fan_speed, NULL, 2 },
1007 { "fan%d_safe", applesmc_show_fan_speed, NULL, 3 },
1008 { "fan%d_output", applesmc_show_fan_speed, applesmc_store_fan_speed, 4 },
1009 { "fan%d_manual", applesmc_show_fan_manual, applesmc_store_fan_manual },
1010 { }
6f2fad74
NB
1011};
1012
9792dadf
HR
1013static struct applesmc_node_group temp_group[] = {
1014 { "temp%d_label", applesmc_show_sensor_label },
1015 { "temp%d_input", applesmc_show_temperature },
1016 { }
fa5575cf
AM
1017};
1018
6f2fad74
NB
1019/* Module stuff */
1020
9792dadf
HR
1021/*
1022 * applesmc_destroy_nodes - remove files and free associated memory
1023 */
1024static void applesmc_destroy_nodes(struct applesmc_node_group *groups)
1025{
1026 struct applesmc_node_group *grp;
1027 struct applesmc_dev_attr *node;
1028
1029 for (grp = groups; grp->nodes; grp++) {
1030 for (node = grp->nodes; node->sda.dev_attr.attr.name; node++)
1031 sysfs_remove_file(&pdev->dev.kobj,
1032 &node->sda.dev_attr.attr);
1033 kfree(grp->nodes);
1034 grp->nodes = NULL;
1035 }
1036}
1037
1038/*
1039 * applesmc_create_nodes - create a two-dimensional group of sysfs files
1040 */
1041static int applesmc_create_nodes(struct applesmc_node_group *groups, int num)
1042{
1043 struct applesmc_node_group *grp;
1044 struct applesmc_dev_attr *node;
1045 struct attribute *attr;
1046 int ret, i;
1047
1048 for (grp = groups; grp->format; grp++) {
1049 grp->nodes = kcalloc(num + 1, sizeof(*node), GFP_KERNEL);
1050 if (!grp->nodes) {
1051 ret = -ENOMEM;
1052 goto out;
1053 }
1054 for (i = 0; i < num; i++) {
1055 node = &grp->nodes[i];
1056 sprintf(node->name, grp->format, i + 1);
3eba2bf7 1057 node->sda.index = (grp->option << 16) | (i & 0xffff);
9792dadf
HR
1058 node->sda.dev_attr.show = grp->show;
1059 node->sda.dev_attr.store = grp->store;
1060 attr = &node->sda.dev_attr.attr;
9d1f8a40 1061 sysfs_attr_init(attr);
9792dadf
HR
1062 attr->name = node->name;
1063 attr->mode = S_IRUGO | (grp->store ? S_IWUSR : 0);
1064 ret = sysfs_create_file(&pdev->dev.kobj, attr);
1065 if (ret) {
1066 attr->name = NULL;
1067 goto out;
1068 }
1069 }
1070 }
1071
1072 return 0;
1073out:
1074 applesmc_destroy_nodes(groups);
1075 return ret;
1076}
1077
6f2fad74
NB
1078/* Create accelerometer ressources */
1079static int applesmc_create_accelerometer(void)
1080{
d5cf2b99 1081 struct input_dev *idev;
6f2fad74
NB
1082 int ret;
1083
0b0b5dff
HR
1084 if (!smcreg.has_accelerometer)
1085 return 0;
1086
1087 ret = applesmc_create_nodes(accelerometer_group, 1);
6f2fad74
NB
1088 if (ret)
1089 goto out;
1090
d5cf2b99 1091 applesmc_idev = input_allocate_polled_device();
6f2fad74
NB
1092 if (!applesmc_idev) {
1093 ret = -ENOMEM;
1094 goto out_sysfs;
1095 }
1096
d5cf2b99
DT
1097 applesmc_idev->poll = applesmc_idev_poll;
1098 applesmc_idev->poll_interval = APPLESMC_POLL_INTERVAL;
1099
6f2fad74
NB
1100 /* initial calibrate for the input device */
1101 applesmc_calibrate();
1102
d5cf2b99
DT
1103 /* initialize the input device */
1104 idev = applesmc_idev->input;
1105 idev->name = "applesmc";
1106 idev->id.bustype = BUS_HOST;
1107 idev->dev.parent = &pdev->dev;
7b19ada2 1108 idev->evbit[0] = BIT_MASK(EV_ABS);
d5cf2b99 1109 input_set_abs_params(idev, ABS_X,
6f2fad74 1110 -256, 256, APPLESMC_INPUT_FUZZ, APPLESMC_INPUT_FLAT);
d5cf2b99 1111 input_set_abs_params(idev, ABS_Y,
6f2fad74
NB
1112 -256, 256, APPLESMC_INPUT_FUZZ, APPLESMC_INPUT_FLAT);
1113
d5cf2b99 1114 ret = input_register_polled_device(applesmc_idev);
6f2fad74
NB
1115 if (ret)
1116 goto out_idev;
1117
6f2fad74
NB
1118 return 0;
1119
1120out_idev:
d5cf2b99 1121 input_free_polled_device(applesmc_idev);
6f2fad74
NB
1122
1123out_sysfs:
0b0b5dff 1124 applesmc_destroy_nodes(accelerometer_group);
6f2fad74
NB
1125
1126out:
1ee7c71b 1127 pr_warn("driver init failed (ret=%d)!\n", ret);
6f2fad74
NB
1128 return ret;
1129}
1130
1131/* Release all ressources used by the accelerometer */
1132static void applesmc_release_accelerometer(void)
1133{
0b0b5dff
HR
1134 if (!smcreg.has_accelerometer)
1135 return;
d5cf2b99
DT
1136 input_unregister_polled_device(applesmc_idev);
1137 input_free_polled_device(applesmc_idev);
0b0b5dff 1138 applesmc_destroy_nodes(accelerometer_group);
6f2fad74 1139}
0b0b5dff
HR
1140
1141static int applesmc_create_light_sensor(void)
1142{
1143 if (!smcreg.num_light_sensors)
1144 return 0;
1145 return applesmc_create_nodes(light_sensor_group, 1);
1146}
1147
1148static void applesmc_release_light_sensor(void)
1149{
1150 if (!smcreg.num_light_sensors)
1151 return;
1152 applesmc_destroy_nodes(light_sensor_group);
1153}
1154
1155static int applesmc_create_key_backlight(void)
1156{
1157 if (!smcreg.has_key_backlight)
1158 return 0;
1159 applesmc_led_wq = create_singlethread_workqueue("applesmc-led");
1160 if (!applesmc_led_wq)
1161 return -ENOMEM;
1162 return led_classdev_register(&pdev->dev, &applesmc_backlight);
1163}
1164
1165static void applesmc_release_key_backlight(void)
1166{
1167 if (!smcreg.has_key_backlight)
1168 return;
1169 led_classdev_unregister(&applesmc_backlight);
1170 destroy_workqueue(applesmc_led_wq);
1171}
1172
40ef06f1
HR
1173static int applesmc_dmi_match(const struct dmi_system_id *id)
1174{
1175 return 1;
1176}
6f2fad74 1177
85ebfd3e
GR
1178/*
1179 * Note that DMI_MATCH(...,"MacBook") will match "MacBookPro1,1".
1180 * So we need to put "Apple MacBook Pro" before "Apple MacBook".
1181 */
6f2fad74 1182static __initdata struct dmi_system_id applesmc_whitelist[] = {
f5274c97
HR
1183 { applesmc_dmi_match, "Apple MacBook Air", {
1184 DMI_MATCH(DMI_BOARD_VENDOR, "Apple"),
1185 DMI_MATCH(DMI_PRODUCT_NAME, "MacBookAir") },
40ef06f1 1186 },
6f2fad74 1187 { applesmc_dmi_match, "Apple MacBook Pro", {
2bfe8148
GR
1188 DMI_MATCH(DMI_BOARD_VENDOR, "Apple"),
1189 DMI_MATCH(DMI_PRODUCT_NAME, "MacBookPro") },
40ef06f1 1190 },
cd19ba13 1191 { applesmc_dmi_match, "Apple MacBook", {
2bfe8148
GR
1192 DMI_MATCH(DMI_BOARD_VENDOR, "Apple"),
1193 DMI_MATCH(DMI_PRODUCT_NAME, "MacBook") },
40ef06f1 1194 },
6f2fad74 1195 { applesmc_dmi_match, "Apple Macmini", {
2bfe8148
GR
1196 DMI_MATCH(DMI_BOARD_VENDOR, "Apple"),
1197 DMI_MATCH(DMI_PRODUCT_NAME, "Macmini") },
40ef06f1 1198 },
45a3a36b
HR
1199 { applesmc_dmi_match, "Apple MacPro", {
1200 DMI_MATCH(DMI_BOARD_VENDOR, "Apple"),
1201 DMI_MATCH(DMI_PRODUCT_NAME, "MacPro") },
40ef06f1 1202 },
9f86f28d 1203 { applesmc_dmi_match, "Apple iMac", {
2bfe8148
GR
1204 DMI_MATCH(DMI_BOARD_VENDOR, "Apple"),
1205 DMI_MATCH(DMI_PRODUCT_NAME, "iMac") },
40ef06f1 1206 },
6f2fad74
NB
1207 { .ident = NULL }
1208};
1209
1210static int __init applesmc_init(void)
1211{
1212 int ret;
6f2fad74 1213
6f2fad74 1214 if (!dmi_check_system(applesmc_whitelist)) {
1ee7c71b 1215 pr_warn("supported laptop not found!\n");
6f2fad74
NB
1216 ret = -ENODEV;
1217 goto out;
1218 }
1219
1220 if (!request_region(APPLESMC_DATA_PORT, APPLESMC_NR_PORTS,
1221 "applesmc")) {
1222 ret = -ENXIO;
1223 goto out;
1224 }
1225
1226 ret = platform_driver_register(&applesmc_driver);
1227 if (ret)
1228 goto out_region;
1229
ddfbf2af
JD
1230 pdev = platform_device_register_simple("applesmc", APPLESMC_DATA_PORT,
1231 NULL, 0);
6f2fad74
NB
1232 if (IS_ERR(pdev)) {
1233 ret = PTR_ERR(pdev);
1234 goto out_driver;
1235 }
1236
5874583d
HR
1237 /* create register cache */
1238 ret = applesmc_init_smcreg();
6996abf0
NB
1239 if (ret)
1240 goto out_device;
fa74419b 1241
0b0b5dff 1242 ret = applesmc_create_nodes(info_group, 1);
5874583d
HR
1243 if (ret)
1244 goto out_smcreg;
1245
3eba2bf7
HR
1246 ret = applesmc_create_nodes(fan_group, smcreg.fan_count);
1247 if (ret)
1248 goto out_info;
6f2fad74 1249
9792dadf
HR
1250 ret = applesmc_create_nodes(temp_group, smcreg.temp_count);
1251 if (ret)
1252 goto out_fans;
6f2fad74 1253
0b0b5dff
HR
1254 ret = applesmc_create_accelerometer();
1255 if (ret)
1256 goto out_temperature;
6f2fad74 1257
0b0b5dff
HR
1258 ret = applesmc_create_light_sensor();
1259 if (ret)
1260 goto out_accelerometer;
6f2fad74 1261
0b0b5dff
HR
1262 ret = applesmc_create_key_backlight();
1263 if (ret)
1264 goto out_light_sysfs;
6f2fad74 1265
1beeffe4
TJ
1266 hwmon_dev = hwmon_device_register(&pdev->dev);
1267 if (IS_ERR(hwmon_dev)) {
1268 ret = PTR_ERR(hwmon_dev);
6f2fad74
NB
1269 goto out_light_ledclass;
1270 }
1271
6f2fad74
NB
1272 return 0;
1273
1274out_light_ledclass:
0b0b5dff 1275 applesmc_release_key_backlight();
6f2fad74 1276out_light_sysfs:
0b0b5dff 1277 applesmc_release_light_sensor();
6f2fad74 1278out_accelerometer:
0b0b5dff 1279 applesmc_release_accelerometer();
6f2fad74 1280out_temperature:
9792dadf 1281 applesmc_destroy_nodes(temp_group);
0559a538 1282out_fans:
3eba2bf7
HR
1283 applesmc_destroy_nodes(fan_group);
1284out_info:
0b0b5dff 1285 applesmc_destroy_nodes(info_group);
5874583d
HR
1286out_smcreg:
1287 applesmc_destroy_smcreg();
6f2fad74
NB
1288out_device:
1289 platform_device_unregister(pdev);
1290out_driver:
1291 platform_driver_unregister(&applesmc_driver);
1292out_region:
1293 release_region(APPLESMC_DATA_PORT, APPLESMC_NR_PORTS);
1294out:
1ee7c71b 1295 pr_warn("driver init failed (ret=%d)!\n", ret);
6f2fad74
NB
1296 return ret;
1297}
1298
1299static void __exit applesmc_exit(void)
1300{
1beeffe4 1301 hwmon_device_unregister(hwmon_dev);
0b0b5dff
HR
1302 applesmc_release_key_backlight();
1303 applesmc_release_light_sensor();
1304 applesmc_release_accelerometer();
9792dadf 1305 applesmc_destroy_nodes(temp_group);
3eba2bf7 1306 applesmc_destroy_nodes(fan_group);
0b0b5dff 1307 applesmc_destroy_nodes(info_group);
5874583d 1308 applesmc_destroy_smcreg();
6f2fad74
NB
1309 platform_device_unregister(pdev);
1310 platform_driver_unregister(&applesmc_driver);
1311 release_region(APPLESMC_DATA_PORT, APPLESMC_NR_PORTS);
6f2fad74
NB
1312}
1313
1314module_init(applesmc_init);
1315module_exit(applesmc_exit);
1316
1317MODULE_AUTHOR("Nicolas Boichat");
1318MODULE_DESCRIPTION("Apple SMC");
1319MODULE_LICENSE("GPL v2");
dc924efb 1320MODULE_DEVICE_TABLE(dmi, applesmc_whitelist);
This page took 0.501032 seconds and 5 git commands to generate.