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