blob: eb81a6430fe4b0af24b679fd2deef1ecd816b7d2 [file] [log] [blame]
Nicolas Boichat6f2fad72007-05-08 00:24:52 -07001/*
2 * drivers/hwmon/applesmc.c - driver for Apple's SMC (accelerometer, temperature
3 * sensors, fan control, keyboard backlight control) used in Intel-based Apple
4 * computers.
5 *
6 * Copyright (C) 2007 Nicolas Boichat <nicolas@boichat.ch>
7 *
8 * Based on hdaps.c driver:
9 * Copyright (C) 2005 Robert Love <rml@novell.com>
10 * Copyright (C) 2005 Jesper Juhl <jesper.juhl@gmail.com>
11 *
12 * Fan control based on smcFanControl:
13 * Copyright (C) 2006 Hendrik Holtmann <holtmann@mac.com>
14 *
15 * This program is free software; you can redistribute it and/or modify it
16 * under the terms of the GNU General Public License v2 as published by the
17 * Free Software Foundation.
18 *
19 * This program is distributed in the hope that it will be useful, but WITHOUT
20 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
21 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
22 * more details.
23 *
24 * You should have received a copy of the GNU General Public License along with
25 * this program; if not, write to the Free Software Foundation, Inc.,
26 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
27 */
28
29#include <linux/delay.h>
30#include <linux/platform_device.h>
Dmitry Torokhovd5cf2b92007-09-26 00:01:35 -040031#include <linux/input-polldev.h>
Nicolas Boichat6f2fad72007-05-08 00:24:52 -070032#include <linux/kernel.h>
33#include <linux/module.h>
34#include <linux/timer.h>
35#include <linux/dmi.h>
36#include <linux/mutex.h>
37#include <linux/hwmon-sysfs.h>
38#include <asm/io.h>
39#include <linux/leds.h>
40#include <linux/hwmon.h>
41#include <linux/workqueue.h>
42
43/* data port used by Apple SMC */
44#define APPLESMC_DATA_PORT 0x300
45/* command/status port used by Apple SMC */
46#define APPLESMC_CMD_PORT 0x304
47
48#define APPLESMC_NR_PORTS 32 /* 0x300-0x31f */
49
50#define APPLESMC_MAX_DATA_LENGTH 32
51
52#define APPLESMC_STATUS_MASK 0x0f
53#define APPLESMC_READ_CMD 0x10
54#define APPLESMC_WRITE_CMD 0x11
55#define APPLESMC_GET_KEY_BY_INDEX_CMD 0x12
56#define APPLESMC_GET_KEY_TYPE_CMD 0x13
57
58#define KEY_COUNT_KEY "#KEY" /* r-o ui32 */
59
60#define LIGHT_SENSOR_LEFT_KEY "ALV0" /* r-o {alv (6 bytes) */
61#define LIGHT_SENSOR_RIGHT_KEY "ALV1" /* r-o {alv (6 bytes) */
Dmitry Torokhovd5cf2b92007-09-26 00:01:35 -040062#define BACKLIGHT_KEY "LKSB" /* w-o {lkb (2 bytes) */
Nicolas Boichat6f2fad72007-05-08 00:24:52 -070063
Dmitry Torokhovd5cf2b92007-09-26 00:01:35 -040064#define CLAMSHELL_KEY "MSLD" /* r-o ui8 (unused) */
Nicolas Boichat6f2fad72007-05-08 00:24:52 -070065
66#define MOTION_SENSOR_X_KEY "MO_X" /* r-o sp78 (2 bytes) */
67#define MOTION_SENSOR_Y_KEY "MO_Y" /* r-o sp78 (2 bytes) */
68#define MOTION_SENSOR_Z_KEY "MO_Z" /* r-o sp78 (2 bytes) */
69#define MOTION_SENSOR_KEY "MOCN" /* r/w ui16 */
70
71#define FANS_COUNT "FNum" /* r-o ui8 */
72#define FANS_MANUAL "FS! " /* r-w ui16 */
73#define FAN_ACTUAL_SPEED "F0Ac" /* r-o fpe2 (2 bytes) */
74#define FAN_MIN_SPEED "F0Mn" /* r-o fpe2 (2 bytes) */
75#define FAN_MAX_SPEED "F0Mx" /* r-o fpe2 (2 bytes) */
76#define FAN_SAFE_SPEED "F0Sf" /* r-o fpe2 (2 bytes) */
77#define FAN_TARGET_SPEED "F0Tg" /* r-w fpe2 (2 bytes) */
78#define FAN_POSITION "F0ID" /* r-o char[16] */
79
80/*
81 * Temperature sensors keys (sp78 - 2 bytes).
82 * First set for Macbook(Pro), second for Macmini.
83 */
84static const char* temperature_sensors_sets[][13] = {
85 { "TA0P", "TB0T", "TC0D", "TC0P", "TG0H", "TG0P", "TG0T", "Th0H",
86 "Th1H", "Tm0P", "Ts0P", "Ts1P", NULL },
87 { "TC0D", "TC0P", NULL }
88};
89
90/* List of keys used to read/write fan speeds */
91static const char* fan_speed_keys[] = {
92 FAN_ACTUAL_SPEED,
93 FAN_MIN_SPEED,
94 FAN_MAX_SPEED,
95 FAN_SAFE_SPEED,
96 FAN_TARGET_SPEED
97};
98
99#define INIT_TIMEOUT_MSECS 5000 /* wait up to 5s for device init ... */
100#define INIT_WAIT_MSECS 50 /* ... in 50ms increments */
101
Dmitry Torokhovd5cf2b92007-09-26 00:01:35 -0400102#define APPLESMC_POLL_INTERVAL 50 /* msecs */
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700103#define APPLESMC_INPUT_FUZZ 4 /* input event threshold */
104#define APPLESMC_INPUT_FLAT 4
105
106#define SENSOR_X 0
107#define SENSOR_Y 1
108#define SENSOR_Z 2
109
110/* Structure to be passed to DMI_MATCH function */
111struct dmi_match_data {
112/* Indicates whether this computer has an accelerometer. */
113 int accelerometer;
114/* Indicates whether this computer has light sensors and keyboard backlight. */
115 int light;
116/* Indicates which temperature sensors set to use. */
117 int temperature_set;
118};
119
120static const int debug;
121static struct platform_device *pdev;
122static s16 rest_x;
123static s16 rest_y;
Dmitry Torokhovd5cf2b92007-09-26 00:01:35 -0400124static struct input_polled_dev *applesmc_idev;
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700125static struct class_device *hwmon_class_dev;
126
127/* Indicates whether this computer has an accelerometer. */
128static unsigned int applesmc_accelerometer;
129
130/* Indicates whether this computer has light sensors and keyboard backlight. */
131static unsigned int applesmc_light;
132
133/* Indicates which temperature sensors set to use. */
134static unsigned int applesmc_temperature_set;
135
Dmitry Torokhovd5cf2b92007-09-26 00:01:35 -0400136static DEFINE_MUTEX(applesmc_lock);
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700137
138/*
139 * Last index written to key_at_index sysfs file, and value to use for all other
140 * key_at_index_* sysfs files.
141 */
142static unsigned int key_at_index;
143
144static struct workqueue_struct *applesmc_led_wq;
145
146/*
147 * __wait_status - Wait up to 2ms for the status port to get a certain value
148 * (masked with 0x0f), returning zero if the value is obtained. Callers must
149 * hold applesmc_lock.
150 */
151static int __wait_status(u8 val)
152{
153 unsigned int i;
154
155 val = val & APPLESMC_STATUS_MASK;
156
157 for (i = 0; i < 200; i++) {
158 if ((inb(APPLESMC_CMD_PORT) & APPLESMC_STATUS_MASK) == val) {
159 if (debug)
160 printk(KERN_DEBUG
161 "Waited %d us for status %x\n",
162 i*10, val);
163 return 0;
164 }
165 udelay(10);
166 }
167
168 printk(KERN_WARNING "applesmc: wait status failed: %x != %x\n",
169 val, inb(APPLESMC_CMD_PORT));
170
171 return -EIO;
172}
173
174/*
175 * applesmc_read_key - reads len bytes from a given key, and put them in buffer.
176 * Returns zero on success or a negative error on failure. Callers must
177 * hold applesmc_lock.
178 */
179static int applesmc_read_key(const char* key, u8* buffer, u8 len)
180{
181 int i;
182
183 if (len > APPLESMC_MAX_DATA_LENGTH) {
184 printk(KERN_ERR "applesmc_read_key: cannot read more than "
185 "%d bytes\n", APPLESMC_MAX_DATA_LENGTH);
186 return -EINVAL;
187 }
188
189 outb(APPLESMC_READ_CMD, APPLESMC_CMD_PORT);
190 if (__wait_status(0x0c))
191 return -EIO;
192
193 for (i = 0; i < 4; i++) {
194 outb(key[i], APPLESMC_DATA_PORT);
195 if (__wait_status(0x04))
196 return -EIO;
197 }
198 if (debug)
199 printk(KERN_DEBUG "<%s", key);
200
201 outb(len, APPLESMC_DATA_PORT);
202 if (debug)
203 printk(KERN_DEBUG ">%x", len);
204
205 for (i = 0; i < len; i++) {
206 if (__wait_status(0x05))
207 return -EIO;
208 buffer[i] = inb(APPLESMC_DATA_PORT);
209 if (debug)
210 printk(KERN_DEBUG "<%x", buffer[i]);
211 }
212 if (debug)
213 printk(KERN_DEBUG "\n");
214
215 return 0;
216}
217
218/*
219 * applesmc_write_key - writes len bytes from buffer to a given key.
220 * Returns zero on success or a negative error on failure. Callers must
221 * hold applesmc_lock.
222 */
223static int applesmc_write_key(const char* key, u8* buffer, u8 len)
224{
225 int i;
226
227 if (len > APPLESMC_MAX_DATA_LENGTH) {
228 printk(KERN_ERR "applesmc_write_key: cannot write more than "
229 "%d bytes\n", APPLESMC_MAX_DATA_LENGTH);
230 return -EINVAL;
231 }
232
233 outb(APPLESMC_WRITE_CMD, APPLESMC_CMD_PORT);
234 if (__wait_status(0x0c))
235 return -EIO;
236
237 for (i = 0; i < 4; i++) {
238 outb(key[i], APPLESMC_DATA_PORT);
239 if (__wait_status(0x04))
240 return -EIO;
241 }
242
243 outb(len, APPLESMC_DATA_PORT);
244
245 for (i = 0; i < len; i++) {
246 if (__wait_status(0x04))
247 return -EIO;
248 outb(buffer[i], APPLESMC_DATA_PORT);
249 }
250
251 return 0;
252}
253
254/*
255 * applesmc_get_key_at_index - get key at index, and put the result in key
256 * (char[6]). Returns zero on success or a negative error on failure. Callers
257 * must hold applesmc_lock.
258 */
259static int applesmc_get_key_at_index(int index, char* key)
260{
261 int i;
262 u8 readkey[4];
263 readkey[0] = index >> 24;
264 readkey[1] = index >> 16;
265 readkey[2] = index >> 8;
266 readkey[3] = index;
267
268 outb(APPLESMC_GET_KEY_BY_INDEX_CMD, APPLESMC_CMD_PORT);
269 if (__wait_status(0x0c))
270 return -EIO;
271
272 for (i = 0; i < 4; i++) {
273 outb(readkey[i], APPLESMC_DATA_PORT);
274 if (__wait_status(0x04))
275 return -EIO;
276 }
277
278 outb(4, APPLESMC_DATA_PORT);
279
280 for (i = 0; i < 4; i++) {
281 if (__wait_status(0x05))
282 return -EIO;
283 key[i] = inb(APPLESMC_DATA_PORT);
284 }
285 key[4] = 0;
286
287 return 0;
288}
289
290/*
291 * applesmc_get_key_type - get key type, and put the result in type (char[6]).
292 * Returns zero on success or a negative error on failure. Callers must
293 * hold applesmc_lock.
294 */
295static int applesmc_get_key_type(char* key, char* type)
296{
297 int i;
298
299 outb(APPLESMC_GET_KEY_TYPE_CMD, APPLESMC_CMD_PORT);
300 if (__wait_status(0x0c))
301 return -EIO;
302
303 for (i = 0; i < 4; i++) {
304 outb(key[i], APPLESMC_DATA_PORT);
305 if (__wait_status(0x04))
306 return -EIO;
307 }
308
309 outb(5, APPLESMC_DATA_PORT);
310
311 for (i = 0; i < 6; i++) {
312 if (__wait_status(0x05))
313 return -EIO;
314 type[i] = inb(APPLESMC_DATA_PORT);
315 }
316 type[5] = 0;
317
318 return 0;
319}
320
321/*
322 * applesmc_read_motion_sensor - Read motion sensor (X, Y or Z). Callers must
323 * hold applesmc_lock.
324 */
325static int applesmc_read_motion_sensor(int index, s16* value)
326{
327 u8 buffer[2];
328 int ret;
329
330 switch (index) {
331 case SENSOR_X:
332 ret = applesmc_read_key(MOTION_SENSOR_X_KEY, buffer, 2);
333 break;
334 case SENSOR_Y:
335 ret = applesmc_read_key(MOTION_SENSOR_Y_KEY, buffer, 2);
336 break;
337 case SENSOR_Z:
338 ret = applesmc_read_key(MOTION_SENSOR_Z_KEY, buffer, 2);
339 break;
340 default:
341 ret = -EINVAL;
342 }
343
344 *value = ((s16)buffer[0] << 8) | buffer[1];
345
346 return ret;
347}
348
349/*
350 * applesmc_device_init - initialize the accelerometer. Returns zero on success
351 * and negative error code on failure. Can sleep.
352 */
353static int applesmc_device_init(void)
354{
355 int total, ret = -ENXIO;
356 u8 buffer[2];
357
358 if (!applesmc_accelerometer)
359 return 0;
360
361 mutex_lock(&applesmc_lock);
362
363 for (total = INIT_TIMEOUT_MSECS; total > 0; total -= INIT_WAIT_MSECS) {
364 if (debug)
365 printk(KERN_DEBUG "applesmc try %d\n", total);
366 if (!applesmc_read_key(MOTION_SENSOR_KEY, buffer, 2) &&
367 (buffer[0] != 0x00 || buffer[1] != 0x00)) {
368 if (total == INIT_TIMEOUT_MSECS) {
369 printk(KERN_DEBUG "applesmc: device has"
370 " already been initialized"
371 " (0x%02x, 0x%02x).\n",
372 buffer[0], buffer[1]);
373 } else {
374 printk(KERN_DEBUG "applesmc: device"
375 " successfully initialized"
376 " (0x%02x, 0x%02x).\n",
377 buffer[0], buffer[1]);
378 }
379 ret = 0;
380 goto out;
381 }
382 buffer[0] = 0xe0;
383 buffer[1] = 0x00;
384 applesmc_write_key(MOTION_SENSOR_KEY, buffer, 2);
385 msleep(INIT_WAIT_MSECS);
386 }
387
388 printk(KERN_WARNING "applesmc: failed to init the device\n");
389
390out:
391 mutex_unlock(&applesmc_lock);
392 return ret;
393}
394
395/*
396 * applesmc_get_fan_count - get the number of fans. Callers must NOT hold
397 * applesmc_lock.
398 */
399static int applesmc_get_fan_count(void)
400{
401 int ret;
402 u8 buffer[1];
403
404 mutex_lock(&applesmc_lock);
405
406 ret = applesmc_read_key(FANS_COUNT, buffer, 1);
407
408 mutex_unlock(&applesmc_lock);
409 if (ret)
410 return ret;
411 else
412 return buffer[0];
413}
414
415/* Device model stuff */
416static int applesmc_probe(struct platform_device *dev)
417{
418 int ret;
419
420 ret = applesmc_device_init();
421 if (ret)
422 return ret;
423
424 printk(KERN_INFO "applesmc: device successfully initialized.\n");
425 return 0;
426}
427
428static int applesmc_resume(struct platform_device *dev)
429{
430 return applesmc_device_init();
431}
432
433static struct platform_driver applesmc_driver = {
434 .probe = applesmc_probe,
435 .resume = applesmc_resume,
436 .driver = {
437 .name = "applesmc",
438 .owner = THIS_MODULE,
439 },
440};
441
442/*
443 * applesmc_calibrate - Set our "resting" values. Callers must
444 * hold applesmc_lock.
445 */
446static void applesmc_calibrate(void)
447{
448 applesmc_read_motion_sensor(SENSOR_X, &rest_x);
449 applesmc_read_motion_sensor(SENSOR_Y, &rest_y);
450 rest_x = -rest_x;
451}
452
Dmitry Torokhovd5cf2b92007-09-26 00:01:35 -0400453static void applesmc_idev_poll(struct input_polled_dev *dev)
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700454{
Dmitry Torokhovd5cf2b92007-09-26 00:01:35 -0400455 struct input_dev *idev = dev->input;
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700456 s16 x, y;
457
Dmitry Torokhovd5cf2b92007-09-26 00:01:35 -0400458 mutex_lock(&applesmc_lock);
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700459
460 if (applesmc_read_motion_sensor(SENSOR_X, &x))
461 goto out;
462 if (applesmc_read_motion_sensor(SENSOR_Y, &y))
463 goto out;
464
465 x = -x;
Dmitry Torokhovd5cf2b92007-09-26 00:01:35 -0400466 input_report_abs(idev, ABS_X, x - rest_x);
467 input_report_abs(idev, ABS_Y, y - rest_y);
468 input_sync(idev);
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700469
470out:
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700471 mutex_unlock(&applesmc_lock);
472}
473
474/* Sysfs Files */
475
Nicolas Boichatfa744192007-05-23 13:58:13 -0700476static ssize_t applesmc_name_show(struct device *dev,
477 struct device_attribute *attr, char *buf)
478{
479 return snprintf(buf, PAGE_SIZE, "applesmc\n");
480}
481
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700482static ssize_t applesmc_position_show(struct device *dev,
483 struct device_attribute *attr, char *buf)
484{
485 int ret;
486 s16 x, y, z;
487
488 mutex_lock(&applesmc_lock);
489
490 ret = applesmc_read_motion_sensor(SENSOR_X, &x);
491 if (ret)
492 goto out;
493 ret = applesmc_read_motion_sensor(SENSOR_Y, &y);
494 if (ret)
495 goto out;
496 ret = applesmc_read_motion_sensor(SENSOR_Z, &z);
497 if (ret)
498 goto out;
499
500out:
501 mutex_unlock(&applesmc_lock);
502 if (ret)
503 return ret;
504 else
505 return snprintf(buf, PAGE_SIZE, "(%d,%d,%d)\n", x, y, z);
506}
507
508static ssize_t applesmc_light_show(struct device *dev,
509 struct device_attribute *attr, char *sysfsbuf)
510{
511 int ret;
512 u8 left = 0, right = 0;
513 u8 buffer[6];
514
515 mutex_lock(&applesmc_lock);
516
517 ret = applesmc_read_key(LIGHT_SENSOR_LEFT_KEY, buffer, 6);
518 left = buffer[2];
519 if (ret)
520 goto out;
521 ret = applesmc_read_key(LIGHT_SENSOR_RIGHT_KEY, buffer, 6);
522 right = buffer[2];
523
524out:
525 mutex_unlock(&applesmc_lock);
526 if (ret)
527 return ret;
528 else
529 return snprintf(sysfsbuf, PAGE_SIZE, "(%d,%d)\n", left, right);
530}
531
532/* Displays degree Celsius * 1000 */
533static ssize_t applesmc_show_temperature(struct device *dev,
534 struct device_attribute *devattr, char *sysfsbuf)
535{
536 int ret;
537 u8 buffer[2];
538 unsigned int temp;
539 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
540 const char* key =
541 temperature_sensors_sets[applesmc_temperature_set][attr->index];
542
543 mutex_lock(&applesmc_lock);
544
545 ret = applesmc_read_key(key, buffer, 2);
546 temp = buffer[0]*1000;
547 temp += (buffer[1] >> 6) * 250;
548
549 mutex_unlock(&applesmc_lock);
550
551 if (ret)
552 return ret;
553 else
554 return snprintf(sysfsbuf, PAGE_SIZE, "%u\n", temp);
555}
556
557static ssize_t applesmc_show_fan_speed(struct device *dev,
558 struct device_attribute *attr, char *sysfsbuf)
559{
560 int ret;
561 unsigned int speed = 0;
562 char newkey[5];
563 u8 buffer[2];
564 struct sensor_device_attribute_2 *sensor_attr =
565 to_sensor_dev_attr_2(attr);
566
567 newkey[0] = fan_speed_keys[sensor_attr->nr][0];
568 newkey[1] = '0' + sensor_attr->index;
569 newkey[2] = fan_speed_keys[sensor_attr->nr][2];
570 newkey[3] = fan_speed_keys[sensor_attr->nr][3];
571 newkey[4] = 0;
572
573 mutex_lock(&applesmc_lock);
574
575 ret = applesmc_read_key(newkey, buffer, 2);
576 speed = ((buffer[0] << 8 | buffer[1]) >> 2);
577
578 mutex_unlock(&applesmc_lock);
579 if (ret)
580 return ret;
581 else
582 return snprintf(sysfsbuf, PAGE_SIZE, "%u\n", speed);
583}
584
585static ssize_t applesmc_store_fan_speed(struct device *dev,
586 struct device_attribute *attr,
587 const char *sysfsbuf, size_t count)
588{
589 int ret;
590 u32 speed;
591 char newkey[5];
592 u8 buffer[2];
593 struct sensor_device_attribute_2 *sensor_attr =
594 to_sensor_dev_attr_2(attr);
595
596 speed = simple_strtoul(sysfsbuf, NULL, 10);
597
598 if (speed > 0x4000) /* Bigger than a 14-bit value */
599 return -EINVAL;
600
601 newkey[0] = fan_speed_keys[sensor_attr->nr][0];
602 newkey[1] = '0' + sensor_attr->index;
603 newkey[2] = fan_speed_keys[sensor_attr->nr][2];
604 newkey[3] = fan_speed_keys[sensor_attr->nr][3];
605 newkey[4] = 0;
606
607 mutex_lock(&applesmc_lock);
608
609 buffer[0] = (speed >> 6) & 0xff;
610 buffer[1] = (speed << 2) & 0xff;
611 ret = applesmc_write_key(newkey, buffer, 2);
612
613 mutex_unlock(&applesmc_lock);
614 if (ret)
615 return ret;
616 else
617 return count;
618}
619
620static ssize_t applesmc_show_fan_manual(struct device *dev,
621 struct device_attribute *devattr, char *sysfsbuf)
622{
623 int ret;
624 u16 manual = 0;
625 u8 buffer[2];
626 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
627
628 mutex_lock(&applesmc_lock);
629
630 ret = applesmc_read_key(FANS_MANUAL, buffer, 2);
631 manual = ((buffer[0] << 8 | buffer[1]) >> attr->index) & 0x01;
632
633 mutex_unlock(&applesmc_lock);
634 if (ret)
635 return ret;
636 else
637 return snprintf(sysfsbuf, PAGE_SIZE, "%d\n", manual);
638}
639
640static ssize_t applesmc_store_fan_manual(struct device *dev,
641 struct device_attribute *devattr,
642 const char *sysfsbuf, size_t count)
643{
644 int ret;
645 u8 buffer[2];
646 u32 input;
647 u16 val;
648 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
649
650 input = simple_strtoul(sysfsbuf, NULL, 10);
651
652 mutex_lock(&applesmc_lock);
653
654 ret = applesmc_read_key(FANS_MANUAL, buffer, 2);
655 val = (buffer[0] << 8 | buffer[1]);
656 if (ret)
657 goto out;
658
659 if (input)
660 val = val | (0x01 << attr->index);
661 else
662 val = val & ~(0x01 << attr->index);
663
664 buffer[0] = (val >> 8) & 0xFF;
665 buffer[1] = val & 0xFF;
666
667 ret = applesmc_write_key(FANS_MANUAL, buffer, 2);
668
669out:
670 mutex_unlock(&applesmc_lock);
671 if (ret)
672 return ret;
673 else
674 return count;
675}
676
677static ssize_t applesmc_show_fan_position(struct device *dev,
678 struct device_attribute *attr, char *sysfsbuf)
679{
680 int ret;
681 char newkey[5];
682 u8 buffer[17];
683 struct sensor_device_attribute_2 *sensor_attr =
684 to_sensor_dev_attr_2(attr);
685
686 newkey[0] = FAN_POSITION[0];
687 newkey[1] = '0' + sensor_attr->index;
688 newkey[2] = FAN_POSITION[2];
689 newkey[3] = FAN_POSITION[3];
690 newkey[4] = 0;
691
692 mutex_lock(&applesmc_lock);
693
694 ret = applesmc_read_key(newkey, buffer, 16);
695 buffer[16] = 0;
696
697 mutex_unlock(&applesmc_lock);
698 if (ret)
699 return ret;
700 else
701 return snprintf(sysfsbuf, PAGE_SIZE, "%s\n", buffer+4);
702}
703
704static ssize_t applesmc_calibrate_show(struct device *dev,
705 struct device_attribute *attr, char *sysfsbuf)
706{
707 return snprintf(sysfsbuf, PAGE_SIZE, "(%d,%d)\n", rest_x, rest_y);
708}
709
710static ssize_t applesmc_calibrate_store(struct device *dev,
711 struct device_attribute *attr, const char *sysfsbuf, size_t count)
712{
713 mutex_lock(&applesmc_lock);
714 applesmc_calibrate();
715 mutex_unlock(&applesmc_lock);
716
717 return count;
718}
719
720/* Store the next backlight value to be written by the work */
721static unsigned int backlight_value;
722
723static void applesmc_backlight_set(struct work_struct *work)
724{
725 u8 buffer[2];
726
727 mutex_lock(&applesmc_lock);
728 buffer[0] = backlight_value;
729 buffer[1] = 0x00;
730 applesmc_write_key(BACKLIGHT_KEY, buffer, 2);
731 mutex_unlock(&applesmc_lock);
732}
733static DECLARE_WORK(backlight_work, &applesmc_backlight_set);
734
735static void applesmc_brightness_set(struct led_classdev *led_cdev,
736 enum led_brightness value)
737{
738 int ret;
739
740 backlight_value = value;
741 ret = queue_work(applesmc_led_wq, &backlight_work);
742
743 if (debug && (!ret))
744 printk(KERN_DEBUG "applesmc: work was already on the queue.\n");
745}
746
747static ssize_t applesmc_key_count_show(struct device *dev,
748 struct device_attribute *attr, char *sysfsbuf)
749{
750 int ret;
751 u8 buffer[4];
752 u32 count;
753
754 mutex_lock(&applesmc_lock);
755
756 ret = applesmc_read_key(KEY_COUNT_KEY, buffer, 4);
757 count = ((u32)buffer[0]<<24) + ((u32)buffer[1]<<16) +
758 ((u32)buffer[2]<<8) + buffer[3];
759
760 mutex_unlock(&applesmc_lock);
761 if (ret)
762 return ret;
763 else
764 return snprintf(sysfsbuf, PAGE_SIZE, "%d\n", count);
765}
766
767static ssize_t applesmc_key_at_index_read_show(struct device *dev,
768 struct device_attribute *attr, char *sysfsbuf)
769{
770 char key[5];
771 char info[6];
772 int ret;
773
774 mutex_lock(&applesmc_lock);
775
776 ret = applesmc_get_key_at_index(key_at_index, key);
777
778 if (ret || !key[0]) {
779 mutex_unlock(&applesmc_lock);
780
781 return -EINVAL;
782 }
783
784 ret = applesmc_get_key_type(key, info);
785
786 if (ret) {
787 mutex_unlock(&applesmc_lock);
788
789 return ret;
790 }
791
792 /*
793 * info[0] maximum value (APPLESMC_MAX_DATA_LENGTH) is much lower than
794 * PAGE_SIZE, so we don't need any checks before writing to sysfsbuf.
795 */
796 ret = applesmc_read_key(key, sysfsbuf, info[0]);
797
798 mutex_unlock(&applesmc_lock);
799
800 if (!ret) {
801 return info[0];
Dmitry Torokhovd5cf2b92007-09-26 00:01:35 -0400802 } else {
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700803 return ret;
804 }
805}
806
807static ssize_t applesmc_key_at_index_data_length_show(struct device *dev,
808 struct device_attribute *attr, char *sysfsbuf)
809{
810 char key[5];
811 char info[6];
812 int ret;
813
814 mutex_lock(&applesmc_lock);
815
816 ret = applesmc_get_key_at_index(key_at_index, key);
817
818 if (ret || !key[0]) {
819 mutex_unlock(&applesmc_lock);
820
821 return -EINVAL;
822 }
823
824 ret = applesmc_get_key_type(key, info);
825
826 mutex_unlock(&applesmc_lock);
827
828 if (!ret)
829 return snprintf(sysfsbuf, PAGE_SIZE, "%d\n", info[0]);
830 else
831 return ret;
832}
833
834static ssize_t applesmc_key_at_index_type_show(struct device *dev,
835 struct device_attribute *attr, char *sysfsbuf)
836{
837 char key[5];
838 char info[6];
839 int ret;
840
841 mutex_lock(&applesmc_lock);
842
843 ret = applesmc_get_key_at_index(key_at_index, key);
844
845 if (ret || !key[0]) {
846 mutex_unlock(&applesmc_lock);
847
848 return -EINVAL;
849 }
850
851 ret = applesmc_get_key_type(key, info);
852
853 mutex_unlock(&applesmc_lock);
854
855 if (!ret)
856 return snprintf(sysfsbuf, PAGE_SIZE, "%s\n", info+1);
857 else
858 return ret;
859}
860
861static ssize_t applesmc_key_at_index_name_show(struct device *dev,
862 struct device_attribute *attr, char *sysfsbuf)
863{
864 char key[5];
865 int ret;
866
867 mutex_lock(&applesmc_lock);
868
869 ret = applesmc_get_key_at_index(key_at_index, key);
870
871 mutex_unlock(&applesmc_lock);
872
873 if (!ret && key[0])
874 return snprintf(sysfsbuf, PAGE_SIZE, "%s\n", key);
875 else
876 return -EINVAL;
877}
878
879static ssize_t applesmc_key_at_index_show(struct device *dev,
880 struct device_attribute *attr, char *sysfsbuf)
881{
882 return snprintf(sysfsbuf, PAGE_SIZE, "%d\n", key_at_index);
883}
884
885static ssize_t applesmc_key_at_index_store(struct device *dev,
886 struct device_attribute *attr, const char *sysfsbuf, size_t count)
887{
888 mutex_lock(&applesmc_lock);
889
890 key_at_index = simple_strtoul(sysfsbuf, NULL, 10);
891
892 mutex_unlock(&applesmc_lock);
893
894 return count;
895}
896
897static struct led_classdev applesmc_backlight = {
898 .name = "smc:kbd_backlight",
899 .default_trigger = "nand-disk",
900 .brightness_set = applesmc_brightness_set,
901};
902
Nicolas Boichatfa744192007-05-23 13:58:13 -0700903static DEVICE_ATTR(name, 0444, applesmc_name_show, NULL);
904
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700905static DEVICE_ATTR(position, 0444, applesmc_position_show, NULL);
906static DEVICE_ATTR(calibrate, 0644,
907 applesmc_calibrate_show, applesmc_calibrate_store);
908
909static struct attribute *accelerometer_attributes[] = {
910 &dev_attr_position.attr,
911 &dev_attr_calibrate.attr,
912 NULL
913};
914
915static const struct attribute_group accelerometer_attributes_group =
916 { .attrs = accelerometer_attributes };
917
918static DEVICE_ATTR(light, 0444, applesmc_light_show, NULL);
919
920static DEVICE_ATTR(key_count, 0444, applesmc_key_count_show, NULL);
921static DEVICE_ATTR(key_at_index, 0644,
922 applesmc_key_at_index_show, applesmc_key_at_index_store);
923static DEVICE_ATTR(key_at_index_name, 0444,
924 applesmc_key_at_index_name_show, NULL);
925static DEVICE_ATTR(key_at_index_type, 0444,
926 applesmc_key_at_index_type_show, NULL);
927static DEVICE_ATTR(key_at_index_data_length, 0444,
928 applesmc_key_at_index_data_length_show, NULL);
929static DEVICE_ATTR(key_at_index_data, 0444,
930 applesmc_key_at_index_read_show, NULL);
931
932static struct attribute *key_enumeration_attributes[] = {
933 &dev_attr_key_count.attr,
934 &dev_attr_key_at_index.attr,
935 &dev_attr_key_at_index_name.attr,
936 &dev_attr_key_at_index_type.attr,
937 &dev_attr_key_at_index_data_length.attr,
938 &dev_attr_key_at_index_data.attr,
939 NULL
940};
941
942static const struct attribute_group key_enumeration_group =
943 { .attrs = key_enumeration_attributes };
944
945/*
946 * Macro defining SENSOR_DEVICE_ATTR for a fan sysfs entries.
947 * - show actual speed
948 * - show/store minimum speed
949 * - show maximum speed
950 * - show safe speed
951 * - show/store target speed
952 * - show/store manual mode
953 */
954#define sysfs_fan_speeds_offset(offset) \
955static SENSOR_DEVICE_ATTR_2(fan##offset##_input, S_IRUGO, \
956 applesmc_show_fan_speed, NULL, 0, offset-1); \
957\
958static SENSOR_DEVICE_ATTR_2(fan##offset##_min, S_IRUGO | S_IWUSR, \
959 applesmc_show_fan_speed, applesmc_store_fan_speed, 1, offset-1); \
960\
961static SENSOR_DEVICE_ATTR_2(fan##offset##_max, S_IRUGO, \
962 applesmc_show_fan_speed, NULL, 2, offset-1); \
963\
964static SENSOR_DEVICE_ATTR_2(fan##offset##_safe, S_IRUGO, \
965 applesmc_show_fan_speed, NULL, 3, offset-1); \
966\
967static SENSOR_DEVICE_ATTR_2(fan##offset##_output, S_IRUGO | S_IWUSR, \
968 applesmc_show_fan_speed, applesmc_store_fan_speed, 4, offset-1); \
969\
970static SENSOR_DEVICE_ATTR(fan##offset##_manual, S_IRUGO | S_IWUSR, \
971 applesmc_show_fan_manual, applesmc_store_fan_manual, offset-1); \
972\
Jean Delvareda4e8ca2007-05-08 20:27:05 -0700973static SENSOR_DEVICE_ATTR(fan##offset##_label, S_IRUGO, \
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700974 applesmc_show_fan_position, NULL, offset-1); \
975\
976static struct attribute *fan##offset##_attributes[] = { \
977 &sensor_dev_attr_fan##offset##_input.dev_attr.attr, \
978 &sensor_dev_attr_fan##offset##_min.dev_attr.attr, \
979 &sensor_dev_attr_fan##offset##_max.dev_attr.attr, \
980 &sensor_dev_attr_fan##offset##_safe.dev_attr.attr, \
981 &sensor_dev_attr_fan##offset##_output.dev_attr.attr, \
982 &sensor_dev_attr_fan##offset##_manual.dev_attr.attr, \
Jean Delvareda4e8ca2007-05-08 20:27:05 -0700983 &sensor_dev_attr_fan##offset##_label.dev_attr.attr, \
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700984 NULL \
985};
986
987/*
988 * Create the needed functions for each fan using the macro defined above
989 * (2 fans are supported)
990 */
991sysfs_fan_speeds_offset(1);
992sysfs_fan_speeds_offset(2);
993
994static const struct attribute_group fan_attribute_groups[] = {
995 { .attrs = fan1_attributes },
996 { .attrs = fan2_attributes }
997};
998
999/*
1000 * Temperature sensors sysfs entries.
1001 */
1002static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO,
1003 applesmc_show_temperature, NULL, 0);
1004static SENSOR_DEVICE_ATTR(temp2_input, S_IRUGO,
1005 applesmc_show_temperature, NULL, 1);
1006static SENSOR_DEVICE_ATTR(temp3_input, S_IRUGO,
1007 applesmc_show_temperature, NULL, 2);
1008static SENSOR_DEVICE_ATTR(temp4_input, S_IRUGO,
1009 applesmc_show_temperature, NULL, 3);
1010static SENSOR_DEVICE_ATTR(temp5_input, S_IRUGO,
1011 applesmc_show_temperature, NULL, 4);
1012static SENSOR_DEVICE_ATTR(temp6_input, S_IRUGO,
1013 applesmc_show_temperature, NULL, 5);
1014static SENSOR_DEVICE_ATTR(temp7_input, S_IRUGO,
1015 applesmc_show_temperature, NULL, 6);
1016static SENSOR_DEVICE_ATTR(temp8_input, S_IRUGO,
1017 applesmc_show_temperature, NULL, 7);
1018static SENSOR_DEVICE_ATTR(temp9_input, S_IRUGO,
1019 applesmc_show_temperature, NULL, 8);
1020static SENSOR_DEVICE_ATTR(temp10_input, S_IRUGO,
1021 applesmc_show_temperature, NULL, 9);
1022static SENSOR_DEVICE_ATTR(temp11_input, S_IRUGO,
1023 applesmc_show_temperature, NULL, 10);
1024static SENSOR_DEVICE_ATTR(temp12_input, S_IRUGO,
1025 applesmc_show_temperature, NULL, 11);
1026
1027static struct attribute *temperature_attributes[] = {
1028 &sensor_dev_attr_temp1_input.dev_attr.attr,
1029 &sensor_dev_attr_temp2_input.dev_attr.attr,
1030 &sensor_dev_attr_temp3_input.dev_attr.attr,
1031 &sensor_dev_attr_temp4_input.dev_attr.attr,
1032 &sensor_dev_attr_temp5_input.dev_attr.attr,
1033 &sensor_dev_attr_temp6_input.dev_attr.attr,
1034 &sensor_dev_attr_temp7_input.dev_attr.attr,
1035 &sensor_dev_attr_temp8_input.dev_attr.attr,
1036 &sensor_dev_attr_temp9_input.dev_attr.attr,
1037 &sensor_dev_attr_temp10_input.dev_attr.attr,
1038 &sensor_dev_attr_temp11_input.dev_attr.attr,
1039 &sensor_dev_attr_temp12_input.dev_attr.attr,
1040 NULL
1041};
1042
1043static const struct attribute_group temperature_attributes_group =
1044 { .attrs = temperature_attributes };
1045
1046/* Module stuff */
1047
1048/*
1049 * applesmc_dmi_match - found a match. return one, short-circuiting the hunt.
1050 */
1051static int applesmc_dmi_match(struct dmi_system_id *id)
1052{
1053 int i = 0;
1054 struct dmi_match_data* dmi_data = id->driver_data;
1055 printk(KERN_INFO "applesmc: %s detected:\n", id->ident);
1056 applesmc_accelerometer = dmi_data->accelerometer;
1057 printk(KERN_INFO "applesmc: - Model %s accelerometer\n",
1058 applesmc_accelerometer ? "with" : "without");
1059 applesmc_light = dmi_data->light;
1060 printk(KERN_INFO "applesmc: - Model %s light sensors and backlight\n",
1061 applesmc_light ? "with" : "without");
1062
1063 applesmc_temperature_set = dmi_data->temperature_set;
1064 while (temperature_sensors_sets[applesmc_temperature_set][i] != NULL)
1065 i++;
1066 printk(KERN_INFO "applesmc: - Model with %d temperature sensors\n", i);
1067 return 1;
1068}
1069
1070/* Create accelerometer ressources */
1071static int applesmc_create_accelerometer(void)
1072{
Dmitry Torokhovd5cf2b92007-09-26 00:01:35 -04001073 struct input_dev *idev;
Nicolas Boichat6f2fad72007-05-08 00:24:52 -07001074 int ret;
1075
1076 ret = sysfs_create_group(&pdev->dev.kobj,
1077 &accelerometer_attributes_group);
1078 if (ret)
1079 goto out;
1080
Dmitry Torokhovd5cf2b92007-09-26 00:01:35 -04001081 applesmc_idev = input_allocate_polled_device();
Nicolas Boichat6f2fad72007-05-08 00:24:52 -07001082 if (!applesmc_idev) {
1083 ret = -ENOMEM;
1084 goto out_sysfs;
1085 }
1086
Dmitry Torokhovd5cf2b92007-09-26 00:01:35 -04001087 applesmc_idev->poll = applesmc_idev_poll;
1088 applesmc_idev->poll_interval = APPLESMC_POLL_INTERVAL;
1089
Nicolas Boichat6f2fad72007-05-08 00:24:52 -07001090 /* initial calibrate for the input device */
1091 applesmc_calibrate();
1092
Dmitry Torokhovd5cf2b92007-09-26 00:01:35 -04001093 /* initialize the input device */
1094 idev = applesmc_idev->input;
1095 idev->name = "applesmc";
1096 idev->id.bustype = BUS_HOST;
1097 idev->dev.parent = &pdev->dev;
1098 idev->evbit[0] = BIT(EV_ABS);
1099 input_set_abs_params(idev, ABS_X,
Nicolas Boichat6f2fad72007-05-08 00:24:52 -07001100 -256, 256, APPLESMC_INPUT_FUZZ, APPLESMC_INPUT_FLAT);
Dmitry Torokhovd5cf2b92007-09-26 00:01:35 -04001101 input_set_abs_params(idev, ABS_Y,
Nicolas Boichat6f2fad72007-05-08 00:24:52 -07001102 -256, 256, APPLESMC_INPUT_FUZZ, APPLESMC_INPUT_FLAT);
1103
Dmitry Torokhovd5cf2b92007-09-26 00:01:35 -04001104 ret = input_register_polled_device(applesmc_idev);
Nicolas Boichat6f2fad72007-05-08 00:24:52 -07001105 if (ret)
1106 goto out_idev;
1107
Nicolas Boichat6f2fad72007-05-08 00:24:52 -07001108 return 0;
1109
1110out_idev:
Dmitry Torokhovd5cf2b92007-09-26 00:01:35 -04001111 input_free_polled_device(applesmc_idev);
Nicolas Boichat6f2fad72007-05-08 00:24:52 -07001112
1113out_sysfs:
1114 sysfs_remove_group(&pdev->dev.kobj, &accelerometer_attributes_group);
1115
1116out:
1117 printk(KERN_WARNING "applesmc: driver init failed (ret=%d)!\n", ret);
1118 return ret;
1119}
1120
1121/* Release all ressources used by the accelerometer */
1122static void applesmc_release_accelerometer(void)
1123{
Dmitry Torokhovd5cf2b92007-09-26 00:01:35 -04001124 input_unregister_polled_device(applesmc_idev);
1125 input_free_polled_device(applesmc_idev);
Nicolas Boichat6f2fad72007-05-08 00:24:52 -07001126 sysfs_remove_group(&pdev->dev.kobj, &accelerometer_attributes_group);
1127}
1128
1129static __initdata struct dmi_match_data applesmc_dmi_data[] = {
1130/* MacBook Pro: accelerometer, backlight and temperature set 0 */
1131 { .accelerometer = 1, .light = 1, .temperature_set = 0 },
1132/* MacBook: accelerometer and temperature set 0 */
1133 { .accelerometer = 1, .light = 0, .temperature_set = 0 },
1134/* MacBook: temperature set 1 */
1135 { .accelerometer = 0, .light = 0, .temperature_set = 1 }
1136};
1137
1138/* Note that DMI_MATCH(...,"MacBook") will match "MacBookPro1,1".
1139 * So we need to put "Apple MacBook Pro" before "Apple MacBook". */
1140static __initdata struct dmi_system_id applesmc_whitelist[] = {
1141 { applesmc_dmi_match, "Apple MacBook Pro", {
1142 DMI_MATCH(DMI_BOARD_VENDOR,"Apple"),
1143 DMI_MATCH(DMI_PRODUCT_NAME,"MacBookPro") },
1144 (void*)&applesmc_dmi_data[0]},
1145 { applesmc_dmi_match, "Apple MacBook", {
1146 DMI_MATCH(DMI_BOARD_VENDOR,"Apple"),
1147 DMI_MATCH(DMI_PRODUCT_NAME,"MacBook") },
1148 (void*)&applesmc_dmi_data[1]},
1149 { applesmc_dmi_match, "Apple Macmini", {
1150 DMI_MATCH(DMI_BOARD_VENDOR,"Apple"),
1151 DMI_MATCH(DMI_PRODUCT_NAME,"Macmini") },
1152 (void*)&applesmc_dmi_data[2]},
1153 { .ident = NULL }
1154};
1155
1156static int __init applesmc_init(void)
1157{
1158 int ret;
1159 int count;
1160 int i;
1161
Nicolas Boichat6f2fad72007-05-08 00:24:52 -07001162 if (!dmi_check_system(applesmc_whitelist)) {
1163 printk(KERN_WARNING "applesmc: supported laptop not found!\n");
1164 ret = -ENODEV;
1165 goto out;
1166 }
1167
1168 if (!request_region(APPLESMC_DATA_PORT, APPLESMC_NR_PORTS,
1169 "applesmc")) {
1170 ret = -ENXIO;
1171 goto out;
1172 }
1173
1174 ret = platform_driver_register(&applesmc_driver);
1175 if (ret)
1176 goto out_region;
1177
Jean Delvareddfbf2a2007-05-08 20:27:04 -07001178 pdev = platform_device_register_simple("applesmc", APPLESMC_DATA_PORT,
1179 NULL, 0);
Nicolas Boichat6f2fad72007-05-08 00:24:52 -07001180 if (IS_ERR(pdev)) {
1181 ret = PTR_ERR(pdev);
1182 goto out_driver;
1183 }
1184
Nicolas Boichatfa744192007-05-23 13:58:13 -07001185 ret = sysfs_create_file(&pdev->dev.kobj, &dev_attr_name.attr);
Nicolas Boichat6996abf2007-05-27 22:17:43 +02001186 if (ret)
1187 goto out_device;
Nicolas Boichatfa744192007-05-23 13:58:13 -07001188
Nicolas Boichat6f2fad72007-05-08 00:24:52 -07001189 /* Create key enumeration sysfs files */
1190 ret = sysfs_create_group(&pdev->dev.kobj, &key_enumeration_group);
1191 if (ret)
Nicolas Boichat6996abf2007-05-27 22:17:43 +02001192 goto out_name;
Nicolas Boichat6f2fad72007-05-08 00:24:52 -07001193
1194 /* create fan files */
1195 count = applesmc_get_fan_count();
1196 if (count < 0) {
1197 printk(KERN_ERR "applesmc: Cannot get the number of fans.\n");
1198 } else {
1199 printk(KERN_INFO "applesmc: %d fans found.\n", count);
1200
1201 switch (count) {
1202 default:
1203 printk(KERN_WARNING "applesmc: More than 2 fans found,"
1204 " but at most 2 fans are supported"
1205 " by the driver.\n");
1206 case 2:
1207 ret = sysfs_create_group(&pdev->dev.kobj,
1208 &fan_attribute_groups[1]);
1209 if (ret)
1210 goto out_key_enumeration;
1211 case 1:
1212 ret = sysfs_create_group(&pdev->dev.kobj,
1213 &fan_attribute_groups[0]);
1214 if (ret)
1215 goto out_fan_1;
1216 case 0:
1217 ;
1218 }
1219 }
1220
1221 for (i = 0;
1222 temperature_sensors_sets[applesmc_temperature_set][i] != NULL;
1223 i++) {
1224 if (temperature_attributes[i] == NULL) {
1225 printk(KERN_ERR "applesmc: More temperature sensors "
1226 "in temperature_sensors_sets (at least %i)"
1227 "than available sysfs files in "
1228 "temperature_attributes (%i), please report "
1229 "this bug.\n", i, i-1);
1230 goto out_temperature;
1231 }
1232 ret = sysfs_create_file(&pdev->dev.kobj,
1233 temperature_attributes[i]);
1234 if (ret)
1235 goto out_temperature;
1236 }
1237
1238 if (applesmc_accelerometer) {
1239 ret = applesmc_create_accelerometer();
1240 if (ret)
1241 goto out_temperature;
1242 }
1243
1244 if (applesmc_light) {
1245 /* Add light sensor file */
1246 ret = sysfs_create_file(&pdev->dev.kobj, &dev_attr_light.attr);
1247 if (ret)
1248 goto out_accelerometer;
1249
1250 /* Create the workqueue */
1251 applesmc_led_wq = create_singlethread_workqueue("applesmc-led");
1252 if (!applesmc_led_wq) {
1253 ret = -ENOMEM;
1254 goto out_light_sysfs;
1255 }
1256
1257 /* register as a led device */
1258 ret = led_classdev_register(&pdev->dev, &applesmc_backlight);
1259 if (ret < 0)
1260 goto out_light_wq;
1261 }
1262
1263 hwmon_class_dev = hwmon_device_register(&pdev->dev);
1264 if (IS_ERR(hwmon_class_dev)) {
1265 ret = PTR_ERR(hwmon_class_dev);
1266 goto out_light_ledclass;
1267 }
1268
1269 printk(KERN_INFO "applesmc: driver successfully loaded.\n");
1270
1271 return 0;
1272
1273out_light_ledclass:
1274 if (applesmc_light)
1275 led_classdev_unregister(&applesmc_backlight);
1276out_light_wq:
1277 if (applesmc_light)
1278 destroy_workqueue(applesmc_led_wq);
1279out_light_sysfs:
1280 if (applesmc_light)
1281 sysfs_remove_file(&pdev->dev.kobj, &dev_attr_light.attr);
1282out_accelerometer:
1283 if (applesmc_accelerometer)
1284 applesmc_release_accelerometer();
1285out_temperature:
1286 sysfs_remove_group(&pdev->dev.kobj, &temperature_attributes_group);
1287 sysfs_remove_group(&pdev->dev.kobj, &fan_attribute_groups[0]);
1288out_fan_1:
1289 sysfs_remove_group(&pdev->dev.kobj, &fan_attribute_groups[1]);
1290out_key_enumeration:
1291 sysfs_remove_group(&pdev->dev.kobj, &key_enumeration_group);
Nicolas Boichat6996abf2007-05-27 22:17:43 +02001292out_name:
1293 sysfs_remove_file(&pdev->dev.kobj, &dev_attr_name.attr);
Nicolas Boichat6f2fad72007-05-08 00:24:52 -07001294out_device:
1295 platform_device_unregister(pdev);
1296out_driver:
1297 platform_driver_unregister(&applesmc_driver);
1298out_region:
1299 release_region(APPLESMC_DATA_PORT, APPLESMC_NR_PORTS);
1300out:
1301 printk(KERN_WARNING "applesmc: driver init failed (ret=%d)!\n", ret);
1302 return ret;
1303}
1304
1305static void __exit applesmc_exit(void)
1306{
1307 hwmon_device_unregister(hwmon_class_dev);
1308 if (applesmc_light) {
1309 led_classdev_unregister(&applesmc_backlight);
1310 destroy_workqueue(applesmc_led_wq);
1311 sysfs_remove_file(&pdev->dev.kobj, &dev_attr_light.attr);
1312 }
1313 if (applesmc_accelerometer)
1314 applesmc_release_accelerometer();
1315 sysfs_remove_group(&pdev->dev.kobj, &temperature_attributes_group);
1316 sysfs_remove_group(&pdev->dev.kobj, &fan_attribute_groups[0]);
1317 sysfs_remove_group(&pdev->dev.kobj, &fan_attribute_groups[1]);
1318 sysfs_remove_group(&pdev->dev.kobj, &key_enumeration_group);
Nicolas Boichat6996abf2007-05-27 22:17:43 +02001319 sysfs_remove_file(&pdev->dev.kobj, &dev_attr_name.attr);
Nicolas Boichat6f2fad72007-05-08 00:24:52 -07001320 platform_device_unregister(pdev);
1321 platform_driver_unregister(&applesmc_driver);
1322 release_region(APPLESMC_DATA_PORT, APPLESMC_NR_PORTS);
1323
1324 printk(KERN_INFO "applesmc: driver unloaded.\n");
1325}
1326
1327module_init(applesmc_init);
1328module_exit(applesmc_exit);
1329
1330MODULE_AUTHOR("Nicolas Boichat");
1331MODULE_DESCRIPTION("Apple SMC");
1332MODULE_LICENSE("GPL v2");