blob: 0c94770b7f83cd5fca272ba22877a9f045121904 [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).
Nicolas Boichat6f2fad72007-05-08 00:24:52 -070082 */
René Rebe8de57702007-10-16 14:19:20 -070083static const char* temperature_sensors_sets[][36] = {
Martin Szulecki1bed24b2007-07-09 11:41:36 -070084/* Set 0: Macbook Pro */
Nicolas Boichat6f2fad72007-05-08 00:24:52 -070085 { "TA0P", "TB0T", "TC0D", "TC0P", "TG0H", "TG0P", "TG0T", "Th0H",
86 "Th1H", "Tm0P", "Ts0P", "Ts1P", NULL },
Martin Szulecki1bed24b2007-07-09 11:41:36 -070087/* Set 1: Macbook set */
88 { "TB0T", "TC0D", "TC0P", "TM0P", "TN0P", "TN1P", "Th0H", "Th0S",
89 "Th1H", "Ts0P", NULL },
90/* Set 2: Macmini set */
René Rebe8de57702007-10-16 14:19:20 -070091 { "TC0D", "TC0P", NULL },
92/* Set 3: Mac Pro (2 x Quad-Core) */
93 { "TA0P", "TCAG", "TCAH", "TCBG", "TCBH", "TC0C", "TC0D", "TC0P",
94 "TC1C", "TC1D", "TC2C", "TC2D", "TC3C", "TC3D", "THTG", "TH0P",
95 "TH1P", "TH2P", "TH3P", "TMAP", "TMAS", "TMBS", "TM0P", "TM0S",
96 "TM1P", "TM1S", "TM2P", "TM2S", "TM3S", "TM8P", "TM8S", "TM9P",
97 "TM9S", "TN0H", "TS0C", NULL },
Nicolas Boichat6f2fad72007-05-08 00:24:52 -070098};
99
100/* List of keys used to read/write fan speeds */
101static const char* fan_speed_keys[] = {
102 FAN_ACTUAL_SPEED,
103 FAN_MIN_SPEED,
104 FAN_MAX_SPEED,
105 FAN_SAFE_SPEED,
106 FAN_TARGET_SPEED
107};
108
109#define INIT_TIMEOUT_MSECS 5000 /* wait up to 5s for device init ... */
110#define INIT_WAIT_MSECS 50 /* ... in 50ms increments */
111
Dmitry Torokhovd5cf2b92007-09-26 00:01:35 -0400112#define APPLESMC_POLL_INTERVAL 50 /* msecs */
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700113#define APPLESMC_INPUT_FUZZ 4 /* input event threshold */
114#define APPLESMC_INPUT_FLAT 4
115
116#define SENSOR_X 0
117#define SENSOR_Y 1
118#define SENSOR_Z 2
119
120/* Structure to be passed to DMI_MATCH function */
121struct dmi_match_data {
122/* Indicates whether this computer has an accelerometer. */
123 int accelerometer;
124/* Indicates whether this computer has light sensors and keyboard backlight. */
125 int light;
126/* Indicates which temperature sensors set to use. */
127 int temperature_set;
128};
129
130static const int debug;
131static struct platform_device *pdev;
132static s16 rest_x;
133static s16 rest_y;
Tony Jones1beeffe2007-08-20 13:46:20 -0700134static struct device *hwmon_dev;
Dmitry Torokhovd5cf2b92007-09-26 00:01:35 -0400135static struct input_polled_dev *applesmc_idev;
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700136
137/* Indicates whether this computer has an accelerometer. */
138static unsigned int applesmc_accelerometer;
139
140/* Indicates whether this computer has light sensors and keyboard backlight. */
141static unsigned int applesmc_light;
142
143/* Indicates which temperature sensors set to use. */
144static unsigned int applesmc_temperature_set;
145
Dmitry Torokhovd5cf2b92007-09-26 00:01:35 -0400146static DEFINE_MUTEX(applesmc_lock);
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700147
148/*
149 * Last index written to key_at_index sysfs file, and value to use for all other
150 * key_at_index_* sysfs files.
151 */
152static unsigned int key_at_index;
153
154static struct workqueue_struct *applesmc_led_wq;
155
156/*
157 * __wait_status - Wait up to 2ms for the status port to get a certain value
158 * (masked with 0x0f), returning zero if the value is obtained. Callers must
159 * hold applesmc_lock.
160 */
161static int __wait_status(u8 val)
162{
163 unsigned int i;
164
165 val = val & APPLESMC_STATUS_MASK;
166
167 for (i = 0; i < 200; i++) {
168 if ((inb(APPLESMC_CMD_PORT) & APPLESMC_STATUS_MASK) == val) {
169 if (debug)
170 printk(KERN_DEBUG
171 "Waited %d us for status %x\n",
172 i*10, val);
173 return 0;
174 }
175 udelay(10);
176 }
177
178 printk(KERN_WARNING "applesmc: wait status failed: %x != %x\n",
179 val, inb(APPLESMC_CMD_PORT));
180
181 return -EIO;
182}
183
184/*
185 * applesmc_read_key - reads len bytes from a given key, and put them in buffer.
186 * Returns zero on success or a negative error on failure. Callers must
187 * hold applesmc_lock.
188 */
189static int applesmc_read_key(const char* key, u8* buffer, u8 len)
190{
191 int i;
192
193 if (len > APPLESMC_MAX_DATA_LENGTH) {
194 printk(KERN_ERR "applesmc_read_key: cannot read more than "
195 "%d bytes\n", APPLESMC_MAX_DATA_LENGTH);
196 return -EINVAL;
197 }
198
199 outb(APPLESMC_READ_CMD, APPLESMC_CMD_PORT);
200 if (__wait_status(0x0c))
201 return -EIO;
202
203 for (i = 0; i < 4; i++) {
204 outb(key[i], APPLESMC_DATA_PORT);
205 if (__wait_status(0x04))
206 return -EIO;
207 }
208 if (debug)
209 printk(KERN_DEBUG "<%s", key);
210
211 outb(len, APPLESMC_DATA_PORT);
212 if (debug)
213 printk(KERN_DEBUG ">%x", len);
214
215 for (i = 0; i < len; i++) {
216 if (__wait_status(0x05))
217 return -EIO;
218 buffer[i] = inb(APPLESMC_DATA_PORT);
219 if (debug)
220 printk(KERN_DEBUG "<%x", buffer[i]);
221 }
222 if (debug)
223 printk(KERN_DEBUG "\n");
224
225 return 0;
226}
227
228/*
229 * applesmc_write_key - writes len bytes from buffer to a given key.
230 * Returns zero on success or a negative error on failure. Callers must
231 * hold applesmc_lock.
232 */
233static int applesmc_write_key(const char* key, u8* buffer, u8 len)
234{
235 int i;
236
237 if (len > APPLESMC_MAX_DATA_LENGTH) {
238 printk(KERN_ERR "applesmc_write_key: cannot write more than "
239 "%d bytes\n", APPLESMC_MAX_DATA_LENGTH);
240 return -EINVAL;
241 }
242
243 outb(APPLESMC_WRITE_CMD, APPLESMC_CMD_PORT);
244 if (__wait_status(0x0c))
245 return -EIO;
246
247 for (i = 0; i < 4; i++) {
248 outb(key[i], APPLESMC_DATA_PORT);
249 if (__wait_status(0x04))
250 return -EIO;
251 }
252
253 outb(len, APPLESMC_DATA_PORT);
254
255 for (i = 0; i < len; i++) {
256 if (__wait_status(0x04))
257 return -EIO;
258 outb(buffer[i], APPLESMC_DATA_PORT);
259 }
260
261 return 0;
262}
263
264/*
265 * applesmc_get_key_at_index - get key at index, and put the result in key
266 * (char[6]). Returns zero on success or a negative error on failure. Callers
267 * must hold applesmc_lock.
268 */
269static int applesmc_get_key_at_index(int index, char* key)
270{
271 int i;
272 u8 readkey[4];
273 readkey[0] = index >> 24;
274 readkey[1] = index >> 16;
275 readkey[2] = index >> 8;
276 readkey[3] = index;
277
278 outb(APPLESMC_GET_KEY_BY_INDEX_CMD, APPLESMC_CMD_PORT);
279 if (__wait_status(0x0c))
280 return -EIO;
281
282 for (i = 0; i < 4; i++) {
283 outb(readkey[i], APPLESMC_DATA_PORT);
284 if (__wait_status(0x04))
285 return -EIO;
286 }
287
288 outb(4, APPLESMC_DATA_PORT);
289
290 for (i = 0; i < 4; i++) {
291 if (__wait_status(0x05))
292 return -EIO;
293 key[i] = inb(APPLESMC_DATA_PORT);
294 }
295 key[4] = 0;
296
297 return 0;
298}
299
300/*
301 * applesmc_get_key_type - get key type, and put the result in type (char[6]).
302 * Returns zero on success or a negative error on failure. Callers must
303 * hold applesmc_lock.
304 */
305static int applesmc_get_key_type(char* key, char* type)
306{
307 int i;
308
309 outb(APPLESMC_GET_KEY_TYPE_CMD, APPLESMC_CMD_PORT);
310 if (__wait_status(0x0c))
311 return -EIO;
312
313 for (i = 0; i < 4; i++) {
314 outb(key[i], APPLESMC_DATA_PORT);
315 if (__wait_status(0x04))
316 return -EIO;
317 }
318
319 outb(5, APPLESMC_DATA_PORT);
320
321 for (i = 0; i < 6; i++) {
322 if (__wait_status(0x05))
323 return -EIO;
324 type[i] = inb(APPLESMC_DATA_PORT);
325 }
326 type[5] = 0;
327
328 return 0;
329}
330
331/*
332 * applesmc_read_motion_sensor - Read motion sensor (X, Y or Z). Callers must
333 * hold applesmc_lock.
334 */
335static int applesmc_read_motion_sensor(int index, s16* value)
336{
337 u8 buffer[2];
338 int ret;
339
340 switch (index) {
341 case SENSOR_X:
342 ret = applesmc_read_key(MOTION_SENSOR_X_KEY, buffer, 2);
343 break;
344 case SENSOR_Y:
345 ret = applesmc_read_key(MOTION_SENSOR_Y_KEY, buffer, 2);
346 break;
347 case SENSOR_Z:
348 ret = applesmc_read_key(MOTION_SENSOR_Z_KEY, buffer, 2);
349 break;
350 default:
351 ret = -EINVAL;
352 }
353
354 *value = ((s16)buffer[0] << 8) | buffer[1];
355
356 return ret;
357}
358
359/*
360 * applesmc_device_init - initialize the accelerometer. Returns zero on success
361 * and negative error code on failure. Can sleep.
362 */
363static int applesmc_device_init(void)
364{
365 int total, ret = -ENXIO;
366 u8 buffer[2];
367
368 if (!applesmc_accelerometer)
369 return 0;
370
371 mutex_lock(&applesmc_lock);
372
373 for (total = INIT_TIMEOUT_MSECS; total > 0; total -= INIT_WAIT_MSECS) {
374 if (debug)
375 printk(KERN_DEBUG "applesmc try %d\n", total);
376 if (!applesmc_read_key(MOTION_SENSOR_KEY, buffer, 2) &&
377 (buffer[0] != 0x00 || buffer[1] != 0x00)) {
378 if (total == INIT_TIMEOUT_MSECS) {
379 printk(KERN_DEBUG "applesmc: device has"
380 " already been initialized"
381 " (0x%02x, 0x%02x).\n",
382 buffer[0], buffer[1]);
383 } else {
384 printk(KERN_DEBUG "applesmc: device"
385 " successfully initialized"
386 " (0x%02x, 0x%02x).\n",
387 buffer[0], buffer[1]);
388 }
389 ret = 0;
390 goto out;
391 }
392 buffer[0] = 0xe0;
393 buffer[1] = 0x00;
394 applesmc_write_key(MOTION_SENSOR_KEY, buffer, 2);
395 msleep(INIT_WAIT_MSECS);
396 }
397
398 printk(KERN_WARNING "applesmc: failed to init the device\n");
399
400out:
401 mutex_unlock(&applesmc_lock);
402 return ret;
403}
404
405/*
406 * applesmc_get_fan_count - get the number of fans. Callers must NOT hold
407 * applesmc_lock.
408 */
409static int applesmc_get_fan_count(void)
410{
411 int ret;
412 u8 buffer[1];
413
414 mutex_lock(&applesmc_lock);
415
416 ret = applesmc_read_key(FANS_COUNT, buffer, 1);
417
418 mutex_unlock(&applesmc_lock);
419 if (ret)
420 return ret;
421 else
422 return buffer[0];
423}
424
425/* Device model stuff */
426static int applesmc_probe(struct platform_device *dev)
427{
428 int ret;
429
430 ret = applesmc_device_init();
431 if (ret)
432 return ret;
433
434 printk(KERN_INFO "applesmc: device successfully initialized.\n");
435 return 0;
436}
437
438static int applesmc_resume(struct platform_device *dev)
439{
440 return applesmc_device_init();
441}
442
443static struct platform_driver applesmc_driver = {
444 .probe = applesmc_probe,
445 .resume = applesmc_resume,
446 .driver = {
447 .name = "applesmc",
448 .owner = THIS_MODULE,
449 },
450};
451
452/*
453 * applesmc_calibrate - Set our "resting" values. Callers must
454 * hold applesmc_lock.
455 */
456static void applesmc_calibrate(void)
457{
458 applesmc_read_motion_sensor(SENSOR_X, &rest_x);
459 applesmc_read_motion_sensor(SENSOR_Y, &rest_y);
460 rest_x = -rest_x;
461}
462
Dmitry Torokhovd5cf2b92007-09-26 00:01:35 -0400463static void applesmc_idev_poll(struct input_polled_dev *dev)
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700464{
Dmitry Torokhovd5cf2b92007-09-26 00:01:35 -0400465 struct input_dev *idev = dev->input;
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700466 s16 x, y;
467
Dmitry Torokhovd5cf2b92007-09-26 00:01:35 -0400468 mutex_lock(&applesmc_lock);
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700469
470 if (applesmc_read_motion_sensor(SENSOR_X, &x))
471 goto out;
472 if (applesmc_read_motion_sensor(SENSOR_Y, &y))
473 goto out;
474
475 x = -x;
Dmitry Torokhovd5cf2b92007-09-26 00:01:35 -0400476 input_report_abs(idev, ABS_X, x - rest_x);
477 input_report_abs(idev, ABS_Y, y - rest_y);
478 input_sync(idev);
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700479
480out:
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700481 mutex_unlock(&applesmc_lock);
482}
483
484/* Sysfs Files */
485
Nicolas Boichatfa744192007-05-23 13:58:13 -0700486static ssize_t applesmc_name_show(struct device *dev,
487 struct device_attribute *attr, char *buf)
488{
489 return snprintf(buf, PAGE_SIZE, "applesmc\n");
490}
491
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700492static ssize_t applesmc_position_show(struct device *dev,
493 struct device_attribute *attr, char *buf)
494{
495 int ret;
496 s16 x, y, z;
497
498 mutex_lock(&applesmc_lock);
499
500 ret = applesmc_read_motion_sensor(SENSOR_X, &x);
501 if (ret)
502 goto out;
503 ret = applesmc_read_motion_sensor(SENSOR_Y, &y);
504 if (ret)
505 goto out;
506 ret = applesmc_read_motion_sensor(SENSOR_Z, &z);
507 if (ret)
508 goto out;
509
510out:
511 mutex_unlock(&applesmc_lock);
512 if (ret)
513 return ret;
514 else
515 return snprintf(buf, PAGE_SIZE, "(%d,%d,%d)\n", x, y, z);
516}
517
518static ssize_t applesmc_light_show(struct device *dev,
519 struct device_attribute *attr, char *sysfsbuf)
520{
521 int ret;
522 u8 left = 0, right = 0;
523 u8 buffer[6];
524
525 mutex_lock(&applesmc_lock);
526
527 ret = applesmc_read_key(LIGHT_SENSOR_LEFT_KEY, buffer, 6);
528 left = buffer[2];
529 if (ret)
530 goto out;
531 ret = applesmc_read_key(LIGHT_SENSOR_RIGHT_KEY, buffer, 6);
532 right = buffer[2];
533
534out:
535 mutex_unlock(&applesmc_lock);
536 if (ret)
537 return ret;
538 else
539 return snprintf(sysfsbuf, PAGE_SIZE, "(%d,%d)\n", left, right);
540}
541
542/* Displays degree Celsius * 1000 */
543static ssize_t applesmc_show_temperature(struct device *dev,
544 struct device_attribute *devattr, char *sysfsbuf)
545{
546 int ret;
547 u8 buffer[2];
548 unsigned int temp;
549 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
550 const char* key =
551 temperature_sensors_sets[applesmc_temperature_set][attr->index];
552
553 mutex_lock(&applesmc_lock);
554
555 ret = applesmc_read_key(key, buffer, 2);
556 temp = buffer[0]*1000;
557 temp += (buffer[1] >> 6) * 250;
558
559 mutex_unlock(&applesmc_lock);
560
561 if (ret)
562 return ret;
563 else
564 return snprintf(sysfsbuf, PAGE_SIZE, "%u\n", temp);
565}
566
567static ssize_t applesmc_show_fan_speed(struct device *dev,
568 struct device_attribute *attr, char *sysfsbuf)
569{
570 int ret;
571 unsigned int speed = 0;
572 char newkey[5];
573 u8 buffer[2];
574 struct sensor_device_attribute_2 *sensor_attr =
575 to_sensor_dev_attr_2(attr);
576
577 newkey[0] = fan_speed_keys[sensor_attr->nr][0];
578 newkey[1] = '0' + sensor_attr->index;
579 newkey[2] = fan_speed_keys[sensor_attr->nr][2];
580 newkey[3] = fan_speed_keys[sensor_attr->nr][3];
581 newkey[4] = 0;
582
583 mutex_lock(&applesmc_lock);
584
585 ret = applesmc_read_key(newkey, buffer, 2);
586 speed = ((buffer[0] << 8 | buffer[1]) >> 2);
587
588 mutex_unlock(&applesmc_lock);
589 if (ret)
590 return ret;
591 else
592 return snprintf(sysfsbuf, PAGE_SIZE, "%u\n", speed);
593}
594
595static ssize_t applesmc_store_fan_speed(struct device *dev,
596 struct device_attribute *attr,
597 const char *sysfsbuf, size_t count)
598{
599 int ret;
600 u32 speed;
601 char newkey[5];
602 u8 buffer[2];
603 struct sensor_device_attribute_2 *sensor_attr =
604 to_sensor_dev_attr_2(attr);
605
606 speed = simple_strtoul(sysfsbuf, NULL, 10);
607
608 if (speed > 0x4000) /* Bigger than a 14-bit value */
609 return -EINVAL;
610
611 newkey[0] = fan_speed_keys[sensor_attr->nr][0];
612 newkey[1] = '0' + sensor_attr->index;
613 newkey[2] = fan_speed_keys[sensor_attr->nr][2];
614 newkey[3] = fan_speed_keys[sensor_attr->nr][3];
615 newkey[4] = 0;
616
617 mutex_lock(&applesmc_lock);
618
619 buffer[0] = (speed >> 6) & 0xff;
620 buffer[1] = (speed << 2) & 0xff;
621 ret = applesmc_write_key(newkey, buffer, 2);
622
623 mutex_unlock(&applesmc_lock);
624 if (ret)
625 return ret;
626 else
627 return count;
628}
629
630static ssize_t applesmc_show_fan_manual(struct device *dev,
631 struct device_attribute *devattr, char *sysfsbuf)
632{
633 int ret;
634 u16 manual = 0;
635 u8 buffer[2];
636 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
637
638 mutex_lock(&applesmc_lock);
639
640 ret = applesmc_read_key(FANS_MANUAL, buffer, 2);
641 manual = ((buffer[0] << 8 | buffer[1]) >> attr->index) & 0x01;
642
643 mutex_unlock(&applesmc_lock);
644 if (ret)
645 return ret;
646 else
647 return snprintf(sysfsbuf, PAGE_SIZE, "%d\n", manual);
648}
649
650static ssize_t applesmc_store_fan_manual(struct device *dev,
651 struct device_attribute *devattr,
652 const char *sysfsbuf, size_t count)
653{
654 int ret;
655 u8 buffer[2];
656 u32 input;
657 u16 val;
658 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
659
660 input = simple_strtoul(sysfsbuf, NULL, 10);
661
662 mutex_lock(&applesmc_lock);
663
664 ret = applesmc_read_key(FANS_MANUAL, buffer, 2);
665 val = (buffer[0] << 8 | buffer[1]);
666 if (ret)
667 goto out;
668
669 if (input)
670 val = val | (0x01 << attr->index);
671 else
672 val = val & ~(0x01 << attr->index);
673
674 buffer[0] = (val >> 8) & 0xFF;
675 buffer[1] = val & 0xFF;
676
677 ret = applesmc_write_key(FANS_MANUAL, buffer, 2);
678
679out:
680 mutex_unlock(&applesmc_lock);
681 if (ret)
682 return ret;
683 else
684 return count;
685}
686
687static ssize_t applesmc_show_fan_position(struct device *dev,
688 struct device_attribute *attr, char *sysfsbuf)
689{
690 int ret;
691 char newkey[5];
692 u8 buffer[17];
693 struct sensor_device_attribute_2 *sensor_attr =
694 to_sensor_dev_attr_2(attr);
695
696 newkey[0] = FAN_POSITION[0];
697 newkey[1] = '0' + sensor_attr->index;
698 newkey[2] = FAN_POSITION[2];
699 newkey[3] = FAN_POSITION[3];
700 newkey[4] = 0;
701
702 mutex_lock(&applesmc_lock);
703
704 ret = applesmc_read_key(newkey, buffer, 16);
705 buffer[16] = 0;
706
707 mutex_unlock(&applesmc_lock);
708 if (ret)
709 return ret;
710 else
711 return snprintf(sysfsbuf, PAGE_SIZE, "%s\n", buffer+4);
712}
713
714static ssize_t applesmc_calibrate_show(struct device *dev,
715 struct device_attribute *attr, char *sysfsbuf)
716{
717 return snprintf(sysfsbuf, PAGE_SIZE, "(%d,%d)\n", rest_x, rest_y);
718}
719
720static ssize_t applesmc_calibrate_store(struct device *dev,
721 struct device_attribute *attr, const char *sysfsbuf, size_t count)
722{
723 mutex_lock(&applesmc_lock);
724 applesmc_calibrate();
725 mutex_unlock(&applesmc_lock);
726
727 return count;
728}
729
730/* Store the next backlight value to be written by the work */
731static unsigned int backlight_value;
732
733static void applesmc_backlight_set(struct work_struct *work)
734{
735 u8 buffer[2];
736
737 mutex_lock(&applesmc_lock);
738 buffer[0] = backlight_value;
739 buffer[1] = 0x00;
740 applesmc_write_key(BACKLIGHT_KEY, buffer, 2);
741 mutex_unlock(&applesmc_lock);
742}
743static DECLARE_WORK(backlight_work, &applesmc_backlight_set);
744
745static void applesmc_brightness_set(struct led_classdev *led_cdev,
746 enum led_brightness value)
747{
748 int ret;
749
750 backlight_value = value;
751 ret = queue_work(applesmc_led_wq, &backlight_work);
752
753 if (debug && (!ret))
754 printk(KERN_DEBUG "applesmc: work was already on the queue.\n");
755}
756
757static ssize_t applesmc_key_count_show(struct device *dev,
758 struct device_attribute *attr, char *sysfsbuf)
759{
760 int ret;
761 u8 buffer[4];
762 u32 count;
763
764 mutex_lock(&applesmc_lock);
765
766 ret = applesmc_read_key(KEY_COUNT_KEY, buffer, 4);
767 count = ((u32)buffer[0]<<24) + ((u32)buffer[1]<<16) +
768 ((u32)buffer[2]<<8) + buffer[3];
769
770 mutex_unlock(&applesmc_lock);
771 if (ret)
772 return ret;
773 else
774 return snprintf(sysfsbuf, PAGE_SIZE, "%d\n", count);
775}
776
777static ssize_t applesmc_key_at_index_read_show(struct device *dev,
778 struct device_attribute *attr, char *sysfsbuf)
779{
780 char key[5];
781 char info[6];
782 int ret;
783
784 mutex_lock(&applesmc_lock);
785
786 ret = applesmc_get_key_at_index(key_at_index, key);
787
788 if (ret || !key[0]) {
789 mutex_unlock(&applesmc_lock);
790
791 return -EINVAL;
792 }
793
794 ret = applesmc_get_key_type(key, info);
795
796 if (ret) {
797 mutex_unlock(&applesmc_lock);
798
799 return ret;
800 }
801
802 /*
803 * info[0] maximum value (APPLESMC_MAX_DATA_LENGTH) is much lower than
804 * PAGE_SIZE, so we don't need any checks before writing to sysfsbuf.
805 */
806 ret = applesmc_read_key(key, sysfsbuf, info[0]);
807
808 mutex_unlock(&applesmc_lock);
809
810 if (!ret) {
811 return info[0];
Dmitry Torokhovd5cf2b92007-09-26 00:01:35 -0400812 } else {
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700813 return ret;
814 }
815}
816
817static ssize_t applesmc_key_at_index_data_length_show(struct device *dev,
818 struct device_attribute *attr, char *sysfsbuf)
819{
820 char key[5];
821 char info[6];
822 int ret;
823
824 mutex_lock(&applesmc_lock);
825
826 ret = applesmc_get_key_at_index(key_at_index, key);
827
828 if (ret || !key[0]) {
829 mutex_unlock(&applesmc_lock);
830
831 return -EINVAL;
832 }
833
834 ret = applesmc_get_key_type(key, info);
835
836 mutex_unlock(&applesmc_lock);
837
838 if (!ret)
839 return snprintf(sysfsbuf, PAGE_SIZE, "%d\n", info[0]);
840 else
841 return ret;
842}
843
844static ssize_t applesmc_key_at_index_type_show(struct device *dev,
845 struct device_attribute *attr, char *sysfsbuf)
846{
847 char key[5];
848 char info[6];
849 int ret;
850
851 mutex_lock(&applesmc_lock);
852
853 ret = applesmc_get_key_at_index(key_at_index, key);
854
855 if (ret || !key[0]) {
856 mutex_unlock(&applesmc_lock);
857
858 return -EINVAL;
859 }
860
861 ret = applesmc_get_key_type(key, info);
862
863 mutex_unlock(&applesmc_lock);
864
865 if (!ret)
866 return snprintf(sysfsbuf, PAGE_SIZE, "%s\n", info+1);
867 else
868 return ret;
869}
870
871static ssize_t applesmc_key_at_index_name_show(struct device *dev,
872 struct device_attribute *attr, char *sysfsbuf)
873{
874 char key[5];
875 int ret;
876
877 mutex_lock(&applesmc_lock);
878
879 ret = applesmc_get_key_at_index(key_at_index, key);
880
881 mutex_unlock(&applesmc_lock);
882
883 if (!ret && key[0])
884 return snprintf(sysfsbuf, PAGE_SIZE, "%s\n", key);
885 else
886 return -EINVAL;
887}
888
889static ssize_t applesmc_key_at_index_show(struct device *dev,
890 struct device_attribute *attr, char *sysfsbuf)
891{
892 return snprintf(sysfsbuf, PAGE_SIZE, "%d\n", key_at_index);
893}
894
895static ssize_t applesmc_key_at_index_store(struct device *dev,
896 struct device_attribute *attr, const char *sysfsbuf, size_t count)
897{
898 mutex_lock(&applesmc_lock);
899
900 key_at_index = simple_strtoul(sysfsbuf, NULL, 10);
901
902 mutex_unlock(&applesmc_lock);
903
904 return count;
905}
906
907static struct led_classdev applesmc_backlight = {
Richard Purdie6c152be2007-10-31 15:00:07 +0100908 .name = "smc::kbd_backlight",
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700909 .default_trigger = "nand-disk",
910 .brightness_set = applesmc_brightness_set,
911};
912
Nicolas Boichatfa744192007-05-23 13:58:13 -0700913static DEVICE_ATTR(name, 0444, applesmc_name_show, NULL);
914
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700915static DEVICE_ATTR(position, 0444, applesmc_position_show, NULL);
916static DEVICE_ATTR(calibrate, 0644,
917 applesmc_calibrate_show, applesmc_calibrate_store);
918
919static struct attribute *accelerometer_attributes[] = {
920 &dev_attr_position.attr,
921 &dev_attr_calibrate.attr,
922 NULL
923};
924
925static const struct attribute_group accelerometer_attributes_group =
926 { .attrs = accelerometer_attributes };
927
928static DEVICE_ATTR(light, 0444, applesmc_light_show, NULL);
929
930static DEVICE_ATTR(key_count, 0444, applesmc_key_count_show, NULL);
931static DEVICE_ATTR(key_at_index, 0644,
932 applesmc_key_at_index_show, applesmc_key_at_index_store);
933static DEVICE_ATTR(key_at_index_name, 0444,
934 applesmc_key_at_index_name_show, NULL);
935static DEVICE_ATTR(key_at_index_type, 0444,
936 applesmc_key_at_index_type_show, NULL);
937static DEVICE_ATTR(key_at_index_data_length, 0444,
938 applesmc_key_at_index_data_length_show, NULL);
939static DEVICE_ATTR(key_at_index_data, 0444,
940 applesmc_key_at_index_read_show, NULL);
941
942static struct attribute *key_enumeration_attributes[] = {
943 &dev_attr_key_count.attr,
944 &dev_attr_key_at_index.attr,
945 &dev_attr_key_at_index_name.attr,
946 &dev_attr_key_at_index_type.attr,
947 &dev_attr_key_at_index_data_length.attr,
948 &dev_attr_key_at_index_data.attr,
949 NULL
950};
951
952static const struct attribute_group key_enumeration_group =
953 { .attrs = key_enumeration_attributes };
954
955/*
956 * Macro defining SENSOR_DEVICE_ATTR for a fan sysfs entries.
957 * - show actual speed
958 * - show/store minimum speed
959 * - show maximum speed
960 * - show safe speed
961 * - show/store target speed
962 * - show/store manual mode
963 */
964#define sysfs_fan_speeds_offset(offset) \
965static SENSOR_DEVICE_ATTR_2(fan##offset##_input, S_IRUGO, \
966 applesmc_show_fan_speed, NULL, 0, offset-1); \
967\
968static SENSOR_DEVICE_ATTR_2(fan##offset##_min, S_IRUGO | S_IWUSR, \
969 applesmc_show_fan_speed, applesmc_store_fan_speed, 1, offset-1); \
970\
971static SENSOR_DEVICE_ATTR_2(fan##offset##_max, S_IRUGO, \
972 applesmc_show_fan_speed, NULL, 2, offset-1); \
973\
974static SENSOR_DEVICE_ATTR_2(fan##offset##_safe, S_IRUGO, \
975 applesmc_show_fan_speed, NULL, 3, offset-1); \
976\
977static SENSOR_DEVICE_ATTR_2(fan##offset##_output, S_IRUGO | S_IWUSR, \
978 applesmc_show_fan_speed, applesmc_store_fan_speed, 4, offset-1); \
979\
980static SENSOR_DEVICE_ATTR(fan##offset##_manual, S_IRUGO | S_IWUSR, \
981 applesmc_show_fan_manual, applesmc_store_fan_manual, offset-1); \
982\
Jean Delvareda4e8ca2007-05-08 20:27:05 -0700983static SENSOR_DEVICE_ATTR(fan##offset##_label, S_IRUGO, \
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700984 applesmc_show_fan_position, NULL, offset-1); \
985\
986static struct attribute *fan##offset##_attributes[] = { \
987 &sensor_dev_attr_fan##offset##_input.dev_attr.attr, \
988 &sensor_dev_attr_fan##offset##_min.dev_attr.attr, \
989 &sensor_dev_attr_fan##offset##_max.dev_attr.attr, \
990 &sensor_dev_attr_fan##offset##_safe.dev_attr.attr, \
991 &sensor_dev_attr_fan##offset##_output.dev_attr.attr, \
992 &sensor_dev_attr_fan##offset##_manual.dev_attr.attr, \
Jean Delvareda4e8ca2007-05-08 20:27:05 -0700993 &sensor_dev_attr_fan##offset##_label.dev_attr.attr, \
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700994 NULL \
995};
996
997/*
998 * Create the needed functions for each fan using the macro defined above
René Rebe8de57702007-10-16 14:19:20 -0700999 * (4 fans are supported)
Nicolas Boichat6f2fad72007-05-08 00:24:52 -07001000 */
1001sysfs_fan_speeds_offset(1);
1002sysfs_fan_speeds_offset(2);
René Rebe8de57702007-10-16 14:19:20 -07001003sysfs_fan_speeds_offset(3);
1004sysfs_fan_speeds_offset(4);
Nicolas Boichat6f2fad72007-05-08 00:24:52 -07001005
1006static const struct attribute_group fan_attribute_groups[] = {
1007 { .attrs = fan1_attributes },
René Rebe8de57702007-10-16 14:19:20 -07001008 { .attrs = fan2_attributes },
1009 { .attrs = fan3_attributes },
1010 { .attrs = fan4_attributes },
Nicolas Boichat6f2fad72007-05-08 00:24:52 -07001011};
1012
1013/*
1014 * Temperature sensors sysfs entries.
1015 */
1016static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO,
1017 applesmc_show_temperature, NULL, 0);
1018static SENSOR_DEVICE_ATTR(temp2_input, S_IRUGO,
1019 applesmc_show_temperature, NULL, 1);
1020static SENSOR_DEVICE_ATTR(temp3_input, S_IRUGO,
1021 applesmc_show_temperature, NULL, 2);
1022static SENSOR_DEVICE_ATTR(temp4_input, S_IRUGO,
1023 applesmc_show_temperature, NULL, 3);
1024static SENSOR_DEVICE_ATTR(temp5_input, S_IRUGO,
1025 applesmc_show_temperature, NULL, 4);
1026static SENSOR_DEVICE_ATTR(temp6_input, S_IRUGO,
1027 applesmc_show_temperature, NULL, 5);
1028static SENSOR_DEVICE_ATTR(temp7_input, S_IRUGO,
1029 applesmc_show_temperature, NULL, 6);
1030static SENSOR_DEVICE_ATTR(temp8_input, S_IRUGO,
1031 applesmc_show_temperature, NULL, 7);
1032static SENSOR_DEVICE_ATTR(temp9_input, S_IRUGO,
1033 applesmc_show_temperature, NULL, 8);
1034static SENSOR_DEVICE_ATTR(temp10_input, S_IRUGO,
1035 applesmc_show_temperature, NULL, 9);
1036static SENSOR_DEVICE_ATTR(temp11_input, S_IRUGO,
1037 applesmc_show_temperature, NULL, 10);
1038static SENSOR_DEVICE_ATTR(temp12_input, S_IRUGO,
1039 applesmc_show_temperature, NULL, 11);
René Rebe8de57702007-10-16 14:19:20 -07001040static SENSOR_DEVICE_ATTR(temp13_input, S_IRUGO,
1041 applesmc_show_temperature, NULL, 12);
1042static SENSOR_DEVICE_ATTR(temp14_input, S_IRUGO,
1043 applesmc_show_temperature, NULL, 13);
1044static SENSOR_DEVICE_ATTR(temp15_input, S_IRUGO,
1045 applesmc_show_temperature, NULL, 14);
1046static SENSOR_DEVICE_ATTR(temp16_input, S_IRUGO,
1047 applesmc_show_temperature, NULL, 15);
1048static SENSOR_DEVICE_ATTR(temp17_input, S_IRUGO,
1049 applesmc_show_temperature, NULL, 16);
1050static SENSOR_DEVICE_ATTR(temp18_input, S_IRUGO,
1051 applesmc_show_temperature, NULL, 17);
1052static SENSOR_DEVICE_ATTR(temp19_input, S_IRUGO,
1053 applesmc_show_temperature, NULL, 18);
1054static SENSOR_DEVICE_ATTR(temp20_input, S_IRUGO,
1055 applesmc_show_temperature, NULL, 19);
1056static SENSOR_DEVICE_ATTR(temp21_input, S_IRUGO,
1057 applesmc_show_temperature, NULL, 20);
1058static SENSOR_DEVICE_ATTR(temp22_input, S_IRUGO,
1059 applesmc_show_temperature, NULL, 21);
1060static SENSOR_DEVICE_ATTR(temp23_input, S_IRUGO,
1061 applesmc_show_temperature, NULL, 22);
1062static SENSOR_DEVICE_ATTR(temp24_input, S_IRUGO,
1063 applesmc_show_temperature, NULL, 23);
1064static SENSOR_DEVICE_ATTR(temp25_input, S_IRUGO,
1065 applesmc_show_temperature, NULL, 24);
1066static SENSOR_DEVICE_ATTR(temp26_input, S_IRUGO,
1067 applesmc_show_temperature, NULL, 25);
1068static SENSOR_DEVICE_ATTR(temp27_input, S_IRUGO,
1069 applesmc_show_temperature, NULL, 26);
1070static SENSOR_DEVICE_ATTR(temp28_input, S_IRUGO,
1071 applesmc_show_temperature, NULL, 27);
1072static SENSOR_DEVICE_ATTR(temp29_input, S_IRUGO,
1073 applesmc_show_temperature, NULL, 28);
1074static SENSOR_DEVICE_ATTR(temp30_input, S_IRUGO,
1075 applesmc_show_temperature, NULL, 29);
1076static SENSOR_DEVICE_ATTR(temp31_input, S_IRUGO,
1077 applesmc_show_temperature, NULL, 30);
1078static SENSOR_DEVICE_ATTR(temp32_input, S_IRUGO,
1079 applesmc_show_temperature, NULL, 31);
1080static SENSOR_DEVICE_ATTR(temp33_input, S_IRUGO,
1081 applesmc_show_temperature, NULL, 32);
1082static SENSOR_DEVICE_ATTR(temp34_input, S_IRUGO,
1083 applesmc_show_temperature, NULL, 33);
1084static SENSOR_DEVICE_ATTR(temp35_input, S_IRUGO,
1085 applesmc_show_temperature, NULL, 34);
Nicolas Boichat6f2fad72007-05-08 00:24:52 -07001086
1087static struct attribute *temperature_attributes[] = {
1088 &sensor_dev_attr_temp1_input.dev_attr.attr,
1089 &sensor_dev_attr_temp2_input.dev_attr.attr,
1090 &sensor_dev_attr_temp3_input.dev_attr.attr,
1091 &sensor_dev_attr_temp4_input.dev_attr.attr,
1092 &sensor_dev_attr_temp5_input.dev_attr.attr,
1093 &sensor_dev_attr_temp6_input.dev_attr.attr,
1094 &sensor_dev_attr_temp7_input.dev_attr.attr,
1095 &sensor_dev_attr_temp8_input.dev_attr.attr,
1096 &sensor_dev_attr_temp9_input.dev_attr.attr,
1097 &sensor_dev_attr_temp10_input.dev_attr.attr,
1098 &sensor_dev_attr_temp11_input.dev_attr.attr,
1099 &sensor_dev_attr_temp12_input.dev_attr.attr,
René Rebe8de57702007-10-16 14:19:20 -07001100 &sensor_dev_attr_temp13_input.dev_attr.attr,
1101 &sensor_dev_attr_temp14_input.dev_attr.attr,
1102 &sensor_dev_attr_temp15_input.dev_attr.attr,
1103 &sensor_dev_attr_temp16_input.dev_attr.attr,
1104 &sensor_dev_attr_temp17_input.dev_attr.attr,
1105 &sensor_dev_attr_temp18_input.dev_attr.attr,
1106 &sensor_dev_attr_temp19_input.dev_attr.attr,
1107 &sensor_dev_attr_temp20_input.dev_attr.attr,
1108 &sensor_dev_attr_temp21_input.dev_attr.attr,
1109 &sensor_dev_attr_temp22_input.dev_attr.attr,
1110 &sensor_dev_attr_temp23_input.dev_attr.attr,
1111 &sensor_dev_attr_temp24_input.dev_attr.attr,
1112 &sensor_dev_attr_temp25_input.dev_attr.attr,
1113 &sensor_dev_attr_temp26_input.dev_attr.attr,
1114 &sensor_dev_attr_temp27_input.dev_attr.attr,
1115 &sensor_dev_attr_temp28_input.dev_attr.attr,
1116 &sensor_dev_attr_temp29_input.dev_attr.attr,
1117 &sensor_dev_attr_temp30_input.dev_attr.attr,
1118 &sensor_dev_attr_temp31_input.dev_attr.attr,
1119 &sensor_dev_attr_temp32_input.dev_attr.attr,
1120 &sensor_dev_attr_temp33_input.dev_attr.attr,
1121 &sensor_dev_attr_temp34_input.dev_attr.attr,
1122 &sensor_dev_attr_temp35_input.dev_attr.attr,
Nicolas Boichat6f2fad72007-05-08 00:24:52 -07001123 NULL
1124};
1125
1126static const struct attribute_group temperature_attributes_group =
1127 { .attrs = temperature_attributes };
1128
1129/* Module stuff */
1130
1131/*
1132 * applesmc_dmi_match - found a match. return one, short-circuiting the hunt.
1133 */
Jeff Garzik18552562007-10-03 15:15:40 -04001134static int applesmc_dmi_match(const struct dmi_system_id *id)
Nicolas Boichat6f2fad72007-05-08 00:24:52 -07001135{
1136 int i = 0;
1137 struct dmi_match_data* dmi_data = id->driver_data;
1138 printk(KERN_INFO "applesmc: %s detected:\n", id->ident);
1139 applesmc_accelerometer = dmi_data->accelerometer;
1140 printk(KERN_INFO "applesmc: - Model %s accelerometer\n",
1141 applesmc_accelerometer ? "with" : "without");
1142 applesmc_light = dmi_data->light;
1143 printk(KERN_INFO "applesmc: - Model %s light sensors and backlight\n",
1144 applesmc_light ? "with" : "without");
1145
1146 applesmc_temperature_set = dmi_data->temperature_set;
1147 while (temperature_sensors_sets[applesmc_temperature_set][i] != NULL)
1148 i++;
1149 printk(KERN_INFO "applesmc: - Model with %d temperature sensors\n", i);
1150 return 1;
1151}
1152
1153/* Create accelerometer ressources */
1154static int applesmc_create_accelerometer(void)
1155{
Dmitry Torokhovd5cf2b92007-09-26 00:01:35 -04001156 struct input_dev *idev;
Nicolas Boichat6f2fad72007-05-08 00:24:52 -07001157 int ret;
1158
1159 ret = sysfs_create_group(&pdev->dev.kobj,
1160 &accelerometer_attributes_group);
1161 if (ret)
1162 goto out;
1163
Dmitry Torokhovd5cf2b92007-09-26 00:01:35 -04001164 applesmc_idev = input_allocate_polled_device();
Nicolas Boichat6f2fad72007-05-08 00:24:52 -07001165 if (!applesmc_idev) {
1166 ret = -ENOMEM;
1167 goto out_sysfs;
1168 }
1169
Dmitry Torokhovd5cf2b92007-09-26 00:01:35 -04001170 applesmc_idev->poll = applesmc_idev_poll;
1171 applesmc_idev->poll_interval = APPLESMC_POLL_INTERVAL;
1172
Nicolas Boichat6f2fad72007-05-08 00:24:52 -07001173 /* initial calibrate for the input device */
1174 applesmc_calibrate();
1175
Dmitry Torokhovd5cf2b92007-09-26 00:01:35 -04001176 /* initialize the input device */
1177 idev = applesmc_idev->input;
1178 idev->name = "applesmc";
1179 idev->id.bustype = BUS_HOST;
1180 idev->dev.parent = &pdev->dev;
Jiri Slaby7b19ada2007-10-18 23:40:32 -07001181 idev->evbit[0] = BIT_MASK(EV_ABS);
Dmitry Torokhovd5cf2b92007-09-26 00:01:35 -04001182 input_set_abs_params(idev, ABS_X,
Nicolas Boichat6f2fad72007-05-08 00:24:52 -07001183 -256, 256, APPLESMC_INPUT_FUZZ, APPLESMC_INPUT_FLAT);
Dmitry Torokhovd5cf2b92007-09-26 00:01:35 -04001184 input_set_abs_params(idev, ABS_Y,
Nicolas Boichat6f2fad72007-05-08 00:24:52 -07001185 -256, 256, APPLESMC_INPUT_FUZZ, APPLESMC_INPUT_FLAT);
1186
Dmitry Torokhovd5cf2b92007-09-26 00:01:35 -04001187 ret = input_register_polled_device(applesmc_idev);
Nicolas Boichat6f2fad72007-05-08 00:24:52 -07001188 if (ret)
1189 goto out_idev;
1190
Nicolas Boichat6f2fad72007-05-08 00:24:52 -07001191 return 0;
1192
1193out_idev:
Dmitry Torokhovd5cf2b92007-09-26 00:01:35 -04001194 input_free_polled_device(applesmc_idev);
Nicolas Boichat6f2fad72007-05-08 00:24:52 -07001195
1196out_sysfs:
1197 sysfs_remove_group(&pdev->dev.kobj, &accelerometer_attributes_group);
1198
1199out:
1200 printk(KERN_WARNING "applesmc: driver init failed (ret=%d)!\n", ret);
1201 return ret;
1202}
1203
1204/* Release all ressources used by the accelerometer */
1205static void applesmc_release_accelerometer(void)
1206{
Dmitry Torokhovd5cf2b92007-09-26 00:01:35 -04001207 input_unregister_polled_device(applesmc_idev);
1208 input_free_polled_device(applesmc_idev);
Nicolas Boichat6f2fad72007-05-08 00:24:52 -07001209 sysfs_remove_group(&pdev->dev.kobj, &accelerometer_attributes_group);
1210}
1211
1212static __initdata struct dmi_match_data applesmc_dmi_data[] = {
1213/* MacBook Pro: accelerometer, backlight and temperature set 0 */
1214 { .accelerometer = 1, .light = 1, .temperature_set = 0 },
Martin Szulecki1bed24b2007-07-09 11:41:36 -07001215/* MacBook: accelerometer and temperature set 1 */
1216 { .accelerometer = 1, .light = 0, .temperature_set = 1 },
1217/* MacMini: temperature set 2 */
1218 { .accelerometer = 0, .light = 0, .temperature_set = 2 },
René Rebe8de57702007-10-16 14:19:20 -07001219/* MacPro: temperature set 3 */
1220 { .accelerometer = 0, .light = 0, .temperature_set = 3 },
Nicolas Boichat6f2fad72007-05-08 00:24:52 -07001221};
1222
1223/* Note that DMI_MATCH(...,"MacBook") will match "MacBookPro1,1".
1224 * So we need to put "Apple MacBook Pro" before "Apple MacBook". */
1225static __initdata struct dmi_system_id applesmc_whitelist[] = {
1226 { applesmc_dmi_match, "Apple MacBook Pro", {
1227 DMI_MATCH(DMI_BOARD_VENDOR,"Apple"),
1228 DMI_MATCH(DMI_PRODUCT_NAME,"MacBookPro") },
1229 (void*)&applesmc_dmi_data[0]},
1230 { applesmc_dmi_match, "Apple MacBook", {
1231 DMI_MATCH(DMI_BOARD_VENDOR,"Apple"),
1232 DMI_MATCH(DMI_PRODUCT_NAME,"MacBook") },
1233 (void*)&applesmc_dmi_data[1]},
1234 { applesmc_dmi_match, "Apple Macmini", {
1235 DMI_MATCH(DMI_BOARD_VENDOR,"Apple"),
1236 DMI_MATCH(DMI_PRODUCT_NAME,"Macmini") },
1237 (void*)&applesmc_dmi_data[2]},
René Rebe8de57702007-10-16 14:19:20 -07001238 { applesmc_dmi_match, "Apple MacPro2", {
1239 DMI_MATCH(DMI_BOARD_VENDOR,"Apple"),
1240 DMI_MATCH(DMI_PRODUCT_NAME,"MacPro2") },
1241 (void*)&applesmc_dmi_data[3]},
Nicolas Boichat6f2fad72007-05-08 00:24:52 -07001242 { .ident = NULL }
1243};
1244
1245static int __init applesmc_init(void)
1246{
1247 int ret;
1248 int count;
1249 int i;
1250
Nicolas Boichat6f2fad72007-05-08 00:24:52 -07001251 if (!dmi_check_system(applesmc_whitelist)) {
1252 printk(KERN_WARNING "applesmc: supported laptop not found!\n");
1253 ret = -ENODEV;
1254 goto out;
1255 }
1256
1257 if (!request_region(APPLESMC_DATA_PORT, APPLESMC_NR_PORTS,
1258 "applesmc")) {
1259 ret = -ENXIO;
1260 goto out;
1261 }
1262
1263 ret = platform_driver_register(&applesmc_driver);
1264 if (ret)
1265 goto out_region;
1266
Jean Delvareddfbf2a2007-05-08 20:27:04 -07001267 pdev = platform_device_register_simple("applesmc", APPLESMC_DATA_PORT,
1268 NULL, 0);
Nicolas Boichat6f2fad72007-05-08 00:24:52 -07001269 if (IS_ERR(pdev)) {
1270 ret = PTR_ERR(pdev);
1271 goto out_driver;
1272 }
1273
Nicolas Boichatfa744192007-05-23 13:58:13 -07001274 ret = sysfs_create_file(&pdev->dev.kobj, &dev_attr_name.attr);
Nicolas Boichat6996abf2007-05-27 22:17:43 +02001275 if (ret)
1276 goto out_device;
Nicolas Boichatfa744192007-05-23 13:58:13 -07001277
Nicolas Boichat6f2fad72007-05-08 00:24:52 -07001278 /* Create key enumeration sysfs files */
1279 ret = sysfs_create_group(&pdev->dev.kobj, &key_enumeration_group);
1280 if (ret)
Nicolas Boichat6996abf2007-05-27 22:17:43 +02001281 goto out_name;
Nicolas Boichat6f2fad72007-05-08 00:24:52 -07001282
1283 /* create fan files */
1284 count = applesmc_get_fan_count();
1285 if (count < 0) {
1286 printk(KERN_ERR "applesmc: Cannot get the number of fans.\n");
1287 } else {
1288 printk(KERN_INFO "applesmc: %d fans found.\n", count);
1289
1290 switch (count) {
1291 default:
René Rebe8de57702007-10-16 14:19:20 -07001292 printk(KERN_WARNING "applesmc: More than 4 fans found,"
1293 " but at most 4 fans are supported"
Nicolas Boichat6f2fad72007-05-08 00:24:52 -07001294 " by the driver.\n");
René Rebe8de57702007-10-16 14:19:20 -07001295 case 4:
1296 ret = sysfs_create_group(&pdev->dev.kobj,
1297 &fan_attribute_groups[3]);
1298 if (ret)
1299 goto out_key_enumeration;
1300 case 3:
1301 ret = sysfs_create_group(&pdev->dev.kobj,
1302 &fan_attribute_groups[2]);
1303 if (ret)
1304 goto out_key_enumeration;
Nicolas Boichat6f2fad72007-05-08 00:24:52 -07001305 case 2:
1306 ret = sysfs_create_group(&pdev->dev.kobj,
1307 &fan_attribute_groups[1]);
1308 if (ret)
1309 goto out_key_enumeration;
1310 case 1:
1311 ret = sysfs_create_group(&pdev->dev.kobj,
1312 &fan_attribute_groups[0]);
1313 if (ret)
1314 goto out_fan_1;
1315 case 0:
1316 ;
1317 }
1318 }
1319
1320 for (i = 0;
1321 temperature_sensors_sets[applesmc_temperature_set][i] != NULL;
1322 i++) {
1323 if (temperature_attributes[i] == NULL) {
1324 printk(KERN_ERR "applesmc: More temperature sensors "
1325 "in temperature_sensors_sets (at least %i)"
1326 "than available sysfs files in "
1327 "temperature_attributes (%i), please report "
1328 "this bug.\n", i, i-1);
1329 goto out_temperature;
1330 }
1331 ret = sysfs_create_file(&pdev->dev.kobj,
1332 temperature_attributes[i]);
1333 if (ret)
1334 goto out_temperature;
1335 }
1336
1337 if (applesmc_accelerometer) {
1338 ret = applesmc_create_accelerometer();
1339 if (ret)
1340 goto out_temperature;
1341 }
1342
1343 if (applesmc_light) {
1344 /* Add light sensor file */
1345 ret = sysfs_create_file(&pdev->dev.kobj, &dev_attr_light.attr);
1346 if (ret)
1347 goto out_accelerometer;
1348
1349 /* Create the workqueue */
1350 applesmc_led_wq = create_singlethread_workqueue("applesmc-led");
1351 if (!applesmc_led_wq) {
1352 ret = -ENOMEM;
1353 goto out_light_sysfs;
1354 }
1355
1356 /* register as a led device */
1357 ret = led_classdev_register(&pdev->dev, &applesmc_backlight);
1358 if (ret < 0)
1359 goto out_light_wq;
1360 }
1361
Tony Jones1beeffe2007-08-20 13:46:20 -07001362 hwmon_dev = hwmon_device_register(&pdev->dev);
1363 if (IS_ERR(hwmon_dev)) {
1364 ret = PTR_ERR(hwmon_dev);
Nicolas Boichat6f2fad72007-05-08 00:24:52 -07001365 goto out_light_ledclass;
1366 }
1367
1368 printk(KERN_INFO "applesmc: driver successfully loaded.\n");
1369
1370 return 0;
1371
1372out_light_ledclass:
1373 if (applesmc_light)
1374 led_classdev_unregister(&applesmc_backlight);
1375out_light_wq:
1376 if (applesmc_light)
1377 destroy_workqueue(applesmc_led_wq);
1378out_light_sysfs:
1379 if (applesmc_light)
1380 sysfs_remove_file(&pdev->dev.kobj, &dev_attr_light.attr);
1381out_accelerometer:
1382 if (applesmc_accelerometer)
1383 applesmc_release_accelerometer();
1384out_temperature:
1385 sysfs_remove_group(&pdev->dev.kobj, &temperature_attributes_group);
1386 sysfs_remove_group(&pdev->dev.kobj, &fan_attribute_groups[0]);
1387out_fan_1:
1388 sysfs_remove_group(&pdev->dev.kobj, &fan_attribute_groups[1]);
1389out_key_enumeration:
1390 sysfs_remove_group(&pdev->dev.kobj, &key_enumeration_group);
Nicolas Boichat6996abf2007-05-27 22:17:43 +02001391out_name:
1392 sysfs_remove_file(&pdev->dev.kobj, &dev_attr_name.attr);
Nicolas Boichat6f2fad72007-05-08 00:24:52 -07001393out_device:
1394 platform_device_unregister(pdev);
1395out_driver:
1396 platform_driver_unregister(&applesmc_driver);
1397out_region:
1398 release_region(APPLESMC_DATA_PORT, APPLESMC_NR_PORTS);
1399out:
1400 printk(KERN_WARNING "applesmc: driver init failed (ret=%d)!\n", ret);
1401 return ret;
1402}
1403
1404static void __exit applesmc_exit(void)
1405{
Tony Jones1beeffe2007-08-20 13:46:20 -07001406 hwmon_device_unregister(hwmon_dev);
Nicolas Boichat6f2fad72007-05-08 00:24:52 -07001407 if (applesmc_light) {
1408 led_classdev_unregister(&applesmc_backlight);
1409 destroy_workqueue(applesmc_led_wq);
1410 sysfs_remove_file(&pdev->dev.kobj, &dev_attr_light.attr);
1411 }
1412 if (applesmc_accelerometer)
1413 applesmc_release_accelerometer();
1414 sysfs_remove_group(&pdev->dev.kobj, &temperature_attributes_group);
1415 sysfs_remove_group(&pdev->dev.kobj, &fan_attribute_groups[0]);
1416 sysfs_remove_group(&pdev->dev.kobj, &fan_attribute_groups[1]);
1417 sysfs_remove_group(&pdev->dev.kobj, &key_enumeration_group);
Nicolas Boichat6996abf2007-05-27 22:17:43 +02001418 sysfs_remove_file(&pdev->dev.kobj, &dev_attr_name.attr);
Nicolas Boichat6f2fad72007-05-08 00:24:52 -07001419 platform_device_unregister(pdev);
1420 platform_driver_unregister(&applesmc_driver);
1421 release_region(APPLESMC_DATA_PORT, APPLESMC_NR_PORTS);
1422
1423 printk(KERN_INFO "applesmc: driver unloaded.\n");
1424}
1425
1426module_init(applesmc_init);
1427module_exit(applesmc_exit);
1428
1429MODULE_AUTHOR("Nicolas Boichat");
1430MODULE_DESCRIPTION("Apple SMC");
1431MODULE_LICENSE("GPL v2");