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