blob: b57bd82504a763ac67e6b9c1d346edf3711b5d19 [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
Henrik Rydberg8bd1a122008-10-18 20:27:39 -070060#define LIGHT_SENSOR_LEFT_KEY "ALV0" /* r-o {alv (6-10 bytes) */
61#define LIGHT_SENSOR_RIGHT_KEY "ALV1" /* r-o {alv (6-10 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 },
Henrik Rydbergf5274c92008-10-18 20:27:40 -0700107/* Set 7: Macbook Air */
108 { "TB0T", "TB1S", "TB1T", "TB2S", "TB2T", "TC0D", "TC0P", "TCFP",
109 "TTF0", "TW0P", "Th0H", "Tp0P", "TpFP", "Ts0P", "Ts0S", NULL },
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700110};
111
112/* List of keys used to read/write fan speeds */
113static const char* fan_speed_keys[] = {
114 FAN_ACTUAL_SPEED,
115 FAN_MIN_SPEED,
116 FAN_MAX_SPEED,
117 FAN_SAFE_SPEED,
118 FAN_TARGET_SPEED
119};
120
121#define INIT_TIMEOUT_MSECS 5000 /* wait up to 5s for device init ... */
122#define INIT_WAIT_MSECS 50 /* ... in 50ms increments */
123
Dmitry Torokhovd5cf2b92007-09-26 00:01:35 -0400124#define APPLESMC_POLL_INTERVAL 50 /* msecs */
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700125#define APPLESMC_INPUT_FUZZ 4 /* input event threshold */
126#define APPLESMC_INPUT_FLAT 4
127
128#define SENSOR_X 0
129#define SENSOR_Y 1
130#define SENSOR_Z 2
131
132/* Structure to be passed to DMI_MATCH function */
133struct dmi_match_data {
134/* Indicates whether this computer has an accelerometer. */
135 int accelerometer;
136/* Indicates whether this computer has light sensors and keyboard backlight. */
137 int light;
138/* Indicates which temperature sensors set to use. */
139 int temperature_set;
140};
141
142static const int debug;
143static struct platform_device *pdev;
144static s16 rest_x;
145static s16 rest_y;
Tony Jones1beeffe2007-08-20 13:46:20 -0700146static struct device *hwmon_dev;
Dmitry Torokhovd5cf2b92007-09-26 00:01:35 -0400147static struct input_polled_dev *applesmc_idev;
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700148
149/* Indicates whether this computer has an accelerometer. */
150static unsigned int applesmc_accelerometer;
151
152/* Indicates whether this computer has light sensors and keyboard backlight. */
153static unsigned int applesmc_light;
154
155/* Indicates which temperature sensors set to use. */
156static unsigned int applesmc_temperature_set;
157
Dmitry Torokhovd5cf2b92007-09-26 00:01:35 -0400158static DEFINE_MUTEX(applesmc_lock);
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700159
160/*
161 * Last index written to key_at_index sysfs file, and value to use for all other
162 * key_at_index_* sysfs files.
163 */
164static unsigned int key_at_index;
165
166static struct workqueue_struct *applesmc_led_wq;
167
168/*
Henrik Rydberg02fcbd12008-10-18 20:27:39 -0700169 * __wait_status - Wait up to 10ms for the status port to get a certain value
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700170 * (masked with 0x0f), returning zero if the value is obtained. Callers must
171 * hold applesmc_lock.
172 */
173static int __wait_status(u8 val)
174{
175 unsigned int i;
176
177 val = val & APPLESMC_STATUS_MASK;
178
Henrik Rydberg02fcbd12008-10-18 20:27:39 -0700179 for (i = 0; i < 1000; i++) {
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700180 if ((inb(APPLESMC_CMD_PORT) & APPLESMC_STATUS_MASK) == val) {
181 if (debug)
182 printk(KERN_DEBUG
183 "Waited %d us for status %x\n",
184 i*10, val);
185 return 0;
186 }
187 udelay(10);
188 }
189
190 printk(KERN_WARNING "applesmc: wait status failed: %x != %x\n",
191 val, inb(APPLESMC_CMD_PORT));
192
193 return -EIO;
194}
195
196/*
Henrik Rydberg84d2d7f2008-10-18 20:27:38 -0700197 * special treatment of command port - on newer macbooks, it seems necessary
198 * to resend the command byte before polling the status again. Callers must
199 * hold applesmc_lock.
200 */
201static int send_command(u8 cmd)
202{
203 int i;
204 for (i = 0; i < 1000; i++) {
205 outb(cmd, APPLESMC_CMD_PORT);
206 udelay(5);
207 if ((inb(APPLESMC_CMD_PORT) & APPLESMC_STATUS_MASK) == 0x0c)
208 return 0;
209 udelay(5);
210 }
211 printk(KERN_WARNING "applesmc: command failed: %x -> %x\n",
212 cmd, inb(APPLESMC_CMD_PORT));
213 return -EIO;
214}
215
216/*
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700217 * applesmc_read_key - reads len bytes from a given key, and put them in buffer.
218 * Returns zero on success or a negative error on failure. Callers must
219 * hold applesmc_lock.
220 */
221static int applesmc_read_key(const char* key, u8* buffer, u8 len)
222{
223 int i;
224
225 if (len > APPLESMC_MAX_DATA_LENGTH) {
226 printk(KERN_ERR "applesmc_read_key: cannot read more than "
227 "%d bytes\n", APPLESMC_MAX_DATA_LENGTH);
228 return -EINVAL;
229 }
230
Henrik Rydberg84d2d7f2008-10-18 20:27:38 -0700231 if (send_command(APPLESMC_READ_CMD))
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700232 return -EIO;
233
234 for (i = 0; i < 4; i++) {
235 outb(key[i], APPLESMC_DATA_PORT);
236 if (__wait_status(0x04))
237 return -EIO;
238 }
239 if (debug)
240 printk(KERN_DEBUG "<%s", key);
241
242 outb(len, APPLESMC_DATA_PORT);
243 if (debug)
244 printk(KERN_DEBUG ">%x", len);
245
246 for (i = 0; i < len; i++) {
247 if (__wait_status(0x05))
248 return -EIO;
249 buffer[i] = inb(APPLESMC_DATA_PORT);
250 if (debug)
251 printk(KERN_DEBUG "<%x", buffer[i]);
252 }
253 if (debug)
254 printk(KERN_DEBUG "\n");
255
256 return 0;
257}
258
259/*
260 * applesmc_write_key - writes len bytes from buffer to a given key.
261 * Returns zero on success or a negative error on failure. Callers must
262 * hold applesmc_lock.
263 */
264static int applesmc_write_key(const char* key, u8* buffer, u8 len)
265{
266 int i;
267
268 if (len > APPLESMC_MAX_DATA_LENGTH) {
269 printk(KERN_ERR "applesmc_write_key: cannot write more than "
270 "%d bytes\n", APPLESMC_MAX_DATA_LENGTH);
271 return -EINVAL;
272 }
273
Henrik Rydberg84d2d7f2008-10-18 20:27:38 -0700274 if (send_command(APPLESMC_WRITE_CMD))
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700275 return -EIO;
276
277 for (i = 0; i < 4; i++) {
278 outb(key[i], APPLESMC_DATA_PORT);
279 if (__wait_status(0x04))
280 return -EIO;
281 }
282
283 outb(len, APPLESMC_DATA_PORT);
284
285 for (i = 0; i < len; i++) {
286 if (__wait_status(0x04))
287 return -EIO;
288 outb(buffer[i], APPLESMC_DATA_PORT);
289 }
290
291 return 0;
292}
293
294/*
295 * applesmc_get_key_at_index - get key at index, and put the result in key
296 * (char[6]). Returns zero on success or a negative error on failure. Callers
297 * must hold applesmc_lock.
298 */
299static int applesmc_get_key_at_index(int index, char* key)
300{
301 int i;
302 u8 readkey[4];
303 readkey[0] = index >> 24;
304 readkey[1] = index >> 16;
305 readkey[2] = index >> 8;
306 readkey[3] = index;
307
Henrik Rydberg84d2d7f2008-10-18 20:27:38 -0700308 if (send_command(APPLESMC_GET_KEY_BY_INDEX_CMD))
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700309 return -EIO;
310
311 for (i = 0; i < 4; i++) {
312 outb(readkey[i], APPLESMC_DATA_PORT);
313 if (__wait_status(0x04))
314 return -EIO;
315 }
316
317 outb(4, APPLESMC_DATA_PORT);
318
319 for (i = 0; i < 4; i++) {
320 if (__wait_status(0x05))
321 return -EIO;
322 key[i] = inb(APPLESMC_DATA_PORT);
323 }
324 key[4] = 0;
325
326 return 0;
327}
328
329/*
330 * applesmc_get_key_type - get key type, and put the result in type (char[6]).
331 * Returns zero on success or a negative error on failure. Callers must
332 * hold applesmc_lock.
333 */
334static int applesmc_get_key_type(char* key, char* type)
335{
336 int i;
337
Henrik Rydberg84d2d7f2008-10-18 20:27:38 -0700338 if (send_command(APPLESMC_GET_KEY_TYPE_CMD))
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700339 return -EIO;
340
341 for (i = 0; i < 4; i++) {
342 outb(key[i], APPLESMC_DATA_PORT);
343 if (__wait_status(0x04))
344 return -EIO;
345 }
346
Henrik Rydberg05224092008-10-18 20:27:35 -0700347 outb(6, APPLESMC_DATA_PORT);
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700348
349 for (i = 0; i < 6; i++) {
350 if (__wait_status(0x05))
351 return -EIO;
352 type[i] = inb(APPLESMC_DATA_PORT);
353 }
354 type[5] = 0;
355
356 return 0;
357}
358
359/*
360 * applesmc_read_motion_sensor - Read motion sensor (X, Y or Z). Callers must
361 * hold applesmc_lock.
362 */
363static int applesmc_read_motion_sensor(int index, s16* value)
364{
365 u8 buffer[2];
366 int ret;
367
368 switch (index) {
369 case SENSOR_X:
370 ret = applesmc_read_key(MOTION_SENSOR_X_KEY, buffer, 2);
371 break;
372 case SENSOR_Y:
373 ret = applesmc_read_key(MOTION_SENSOR_Y_KEY, buffer, 2);
374 break;
375 case SENSOR_Z:
376 ret = applesmc_read_key(MOTION_SENSOR_Z_KEY, buffer, 2);
377 break;
378 default:
379 ret = -EINVAL;
380 }
381
382 *value = ((s16)buffer[0] << 8) | buffer[1];
383
384 return ret;
385}
386
387/*
388 * applesmc_device_init - initialize the accelerometer. Returns zero on success
389 * and negative error code on failure. Can sleep.
390 */
391static int applesmc_device_init(void)
392{
393 int total, ret = -ENXIO;
394 u8 buffer[2];
395
396 if (!applesmc_accelerometer)
397 return 0;
398
399 mutex_lock(&applesmc_lock);
400
401 for (total = INIT_TIMEOUT_MSECS; total > 0; total -= INIT_WAIT_MSECS) {
402 if (debug)
403 printk(KERN_DEBUG "applesmc try %d\n", total);
404 if (!applesmc_read_key(MOTION_SENSOR_KEY, buffer, 2) &&
405 (buffer[0] != 0x00 || buffer[1] != 0x00)) {
406 if (total == INIT_TIMEOUT_MSECS) {
407 printk(KERN_DEBUG "applesmc: device has"
408 " already been initialized"
409 " (0x%02x, 0x%02x).\n",
410 buffer[0], buffer[1]);
411 } else {
412 printk(KERN_DEBUG "applesmc: device"
413 " successfully initialized"
414 " (0x%02x, 0x%02x).\n",
415 buffer[0], buffer[1]);
416 }
417 ret = 0;
418 goto out;
419 }
420 buffer[0] = 0xe0;
421 buffer[1] = 0x00;
422 applesmc_write_key(MOTION_SENSOR_KEY, buffer, 2);
423 msleep(INIT_WAIT_MSECS);
424 }
425
426 printk(KERN_WARNING "applesmc: failed to init the device\n");
427
428out:
429 mutex_unlock(&applesmc_lock);
430 return ret;
431}
432
433/*
434 * applesmc_get_fan_count - get the number of fans. Callers must NOT hold
435 * applesmc_lock.
436 */
437static int applesmc_get_fan_count(void)
438{
439 int ret;
440 u8 buffer[1];
441
442 mutex_lock(&applesmc_lock);
443
444 ret = applesmc_read_key(FANS_COUNT, buffer, 1);
445
446 mutex_unlock(&applesmc_lock);
447 if (ret)
448 return ret;
449 else
450 return buffer[0];
451}
452
453/* Device model stuff */
454static int applesmc_probe(struct platform_device *dev)
455{
456 int ret;
457
458 ret = applesmc_device_init();
459 if (ret)
460 return ret;
461
462 printk(KERN_INFO "applesmc: device successfully initialized.\n");
463 return 0;
464}
465
466static int applesmc_resume(struct platform_device *dev)
467{
468 return applesmc_device_init();
469}
470
471static struct platform_driver applesmc_driver = {
472 .probe = applesmc_probe,
473 .resume = applesmc_resume,
474 .driver = {
475 .name = "applesmc",
476 .owner = THIS_MODULE,
477 },
478};
479
480/*
481 * applesmc_calibrate - Set our "resting" values. Callers must
482 * hold applesmc_lock.
483 */
484static void applesmc_calibrate(void)
485{
486 applesmc_read_motion_sensor(SENSOR_X, &rest_x);
487 applesmc_read_motion_sensor(SENSOR_Y, &rest_y);
488 rest_x = -rest_x;
489}
490
Dmitry Torokhovd5cf2b92007-09-26 00:01:35 -0400491static void applesmc_idev_poll(struct input_polled_dev *dev)
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700492{
Dmitry Torokhovd5cf2b92007-09-26 00:01:35 -0400493 struct input_dev *idev = dev->input;
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700494 s16 x, y;
495
Dmitry Torokhovd5cf2b92007-09-26 00:01:35 -0400496 mutex_lock(&applesmc_lock);
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700497
498 if (applesmc_read_motion_sensor(SENSOR_X, &x))
499 goto out;
500 if (applesmc_read_motion_sensor(SENSOR_Y, &y))
501 goto out;
502
503 x = -x;
Dmitry Torokhovd5cf2b92007-09-26 00:01:35 -0400504 input_report_abs(idev, ABS_X, x - rest_x);
505 input_report_abs(idev, ABS_Y, y - rest_y);
506 input_sync(idev);
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700507
508out:
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700509 mutex_unlock(&applesmc_lock);
510}
511
512/* Sysfs Files */
513
Nicolas Boichatfa744192007-05-23 13:58:13 -0700514static ssize_t applesmc_name_show(struct device *dev,
515 struct device_attribute *attr, char *buf)
516{
517 return snprintf(buf, PAGE_SIZE, "applesmc\n");
518}
519
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700520static ssize_t applesmc_position_show(struct device *dev,
521 struct device_attribute *attr, char *buf)
522{
523 int ret;
524 s16 x, y, z;
525
526 mutex_lock(&applesmc_lock);
527
528 ret = applesmc_read_motion_sensor(SENSOR_X, &x);
529 if (ret)
530 goto out;
531 ret = applesmc_read_motion_sensor(SENSOR_Y, &y);
532 if (ret)
533 goto out;
534 ret = applesmc_read_motion_sensor(SENSOR_Z, &z);
535 if (ret)
536 goto out;
537
538out:
539 mutex_unlock(&applesmc_lock);
540 if (ret)
541 return ret;
542 else
543 return snprintf(buf, PAGE_SIZE, "(%d,%d,%d)\n", x, y, z);
544}
545
546static ssize_t applesmc_light_show(struct device *dev,
547 struct device_attribute *attr, char *sysfsbuf)
548{
Henrik Rydberg8bd1a122008-10-18 20:27:39 -0700549 static int data_length;
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700550 int ret;
551 u8 left = 0, right = 0;
Henrik Rydberg8bd1a122008-10-18 20:27:39 -0700552 u8 buffer[10], query[6];
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700553
554 mutex_lock(&applesmc_lock);
555
Henrik Rydberg8bd1a122008-10-18 20:27:39 -0700556 if (!data_length) {
557 ret = applesmc_get_key_type(LIGHT_SENSOR_LEFT_KEY, query);
558 if (ret)
559 goto out;
560 data_length = clamp_val(query[0], 0, 10);
561 printk(KERN_INFO "applesmc: light sensor data length set to "
562 "%d\n", data_length);
563 }
564
565 ret = applesmc_read_key(LIGHT_SENSOR_LEFT_KEY, buffer, data_length);
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700566 left = buffer[2];
567 if (ret)
568 goto out;
Henrik Rydberg8bd1a122008-10-18 20:27:39 -0700569 ret = applesmc_read_key(LIGHT_SENSOR_RIGHT_KEY, buffer, data_length);
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700570 right = buffer[2];
571
572out:
573 mutex_unlock(&applesmc_lock);
574 if (ret)
575 return ret;
576 else
577 return snprintf(sysfsbuf, PAGE_SIZE, "(%d,%d)\n", left, right);
578}
579
580/* Displays degree Celsius * 1000 */
581static ssize_t applesmc_show_temperature(struct device *dev,
582 struct device_attribute *devattr, char *sysfsbuf)
583{
584 int ret;
585 u8 buffer[2];
586 unsigned int temp;
587 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
588 const char* key =
589 temperature_sensors_sets[applesmc_temperature_set][attr->index];
590
591 mutex_lock(&applesmc_lock);
592
593 ret = applesmc_read_key(key, buffer, 2);
594 temp = buffer[0]*1000;
595 temp += (buffer[1] >> 6) * 250;
596
597 mutex_unlock(&applesmc_lock);
598
599 if (ret)
600 return ret;
601 else
602 return snprintf(sysfsbuf, PAGE_SIZE, "%u\n", temp);
603}
604
605static ssize_t applesmc_show_fan_speed(struct device *dev,
606 struct device_attribute *attr, char *sysfsbuf)
607{
608 int ret;
609 unsigned int speed = 0;
610 char newkey[5];
611 u8 buffer[2];
612 struct sensor_device_attribute_2 *sensor_attr =
613 to_sensor_dev_attr_2(attr);
614
615 newkey[0] = fan_speed_keys[sensor_attr->nr][0];
616 newkey[1] = '0' + sensor_attr->index;
617 newkey[2] = fan_speed_keys[sensor_attr->nr][2];
618 newkey[3] = fan_speed_keys[sensor_attr->nr][3];
619 newkey[4] = 0;
620
621 mutex_lock(&applesmc_lock);
622
623 ret = applesmc_read_key(newkey, buffer, 2);
624 speed = ((buffer[0] << 8 | buffer[1]) >> 2);
625
626 mutex_unlock(&applesmc_lock);
627 if (ret)
628 return ret;
629 else
630 return snprintf(sysfsbuf, PAGE_SIZE, "%u\n", speed);
631}
632
633static ssize_t applesmc_store_fan_speed(struct device *dev,
634 struct device_attribute *attr,
635 const char *sysfsbuf, size_t count)
636{
637 int ret;
638 u32 speed;
639 char newkey[5];
640 u8 buffer[2];
641 struct sensor_device_attribute_2 *sensor_attr =
642 to_sensor_dev_attr_2(attr);
643
644 speed = simple_strtoul(sysfsbuf, NULL, 10);
645
646 if (speed > 0x4000) /* Bigger than a 14-bit value */
647 return -EINVAL;
648
649 newkey[0] = fan_speed_keys[sensor_attr->nr][0];
650 newkey[1] = '0' + sensor_attr->index;
651 newkey[2] = fan_speed_keys[sensor_attr->nr][2];
652 newkey[3] = fan_speed_keys[sensor_attr->nr][3];
653 newkey[4] = 0;
654
655 mutex_lock(&applesmc_lock);
656
657 buffer[0] = (speed >> 6) & 0xff;
658 buffer[1] = (speed << 2) & 0xff;
659 ret = applesmc_write_key(newkey, buffer, 2);
660
661 mutex_unlock(&applesmc_lock);
662 if (ret)
663 return ret;
664 else
665 return count;
666}
667
668static ssize_t applesmc_show_fan_manual(struct device *dev,
669 struct device_attribute *devattr, char *sysfsbuf)
670{
671 int ret;
672 u16 manual = 0;
673 u8 buffer[2];
674 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
675
676 mutex_lock(&applesmc_lock);
677
678 ret = applesmc_read_key(FANS_MANUAL, buffer, 2);
679 manual = ((buffer[0] << 8 | buffer[1]) >> attr->index) & 0x01;
680
681 mutex_unlock(&applesmc_lock);
682 if (ret)
683 return ret;
684 else
685 return snprintf(sysfsbuf, PAGE_SIZE, "%d\n", manual);
686}
687
688static ssize_t applesmc_store_fan_manual(struct device *dev,
689 struct device_attribute *devattr,
690 const char *sysfsbuf, size_t count)
691{
692 int ret;
693 u8 buffer[2];
694 u32 input;
695 u16 val;
696 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
697
698 input = simple_strtoul(sysfsbuf, NULL, 10);
699
700 mutex_lock(&applesmc_lock);
701
702 ret = applesmc_read_key(FANS_MANUAL, buffer, 2);
703 val = (buffer[0] << 8 | buffer[1]);
704 if (ret)
705 goto out;
706
707 if (input)
708 val = val | (0x01 << attr->index);
709 else
710 val = val & ~(0x01 << attr->index);
711
712 buffer[0] = (val >> 8) & 0xFF;
713 buffer[1] = val & 0xFF;
714
715 ret = applesmc_write_key(FANS_MANUAL, buffer, 2);
716
717out:
718 mutex_unlock(&applesmc_lock);
719 if (ret)
720 return ret;
721 else
722 return count;
723}
724
725static ssize_t applesmc_show_fan_position(struct device *dev,
726 struct device_attribute *attr, char *sysfsbuf)
727{
728 int ret;
729 char newkey[5];
730 u8 buffer[17];
731 struct sensor_device_attribute_2 *sensor_attr =
732 to_sensor_dev_attr_2(attr);
733
734 newkey[0] = FAN_POSITION[0];
735 newkey[1] = '0' + sensor_attr->index;
736 newkey[2] = FAN_POSITION[2];
737 newkey[3] = FAN_POSITION[3];
738 newkey[4] = 0;
739
740 mutex_lock(&applesmc_lock);
741
742 ret = applesmc_read_key(newkey, buffer, 16);
743 buffer[16] = 0;
744
745 mutex_unlock(&applesmc_lock);
746 if (ret)
747 return ret;
748 else
749 return snprintf(sysfsbuf, PAGE_SIZE, "%s\n", buffer+4);
750}
751
752static ssize_t applesmc_calibrate_show(struct device *dev,
753 struct device_attribute *attr, char *sysfsbuf)
754{
755 return snprintf(sysfsbuf, PAGE_SIZE, "(%d,%d)\n", rest_x, rest_y);
756}
757
758static ssize_t applesmc_calibrate_store(struct device *dev,
759 struct device_attribute *attr, const char *sysfsbuf, size_t count)
760{
761 mutex_lock(&applesmc_lock);
762 applesmc_calibrate();
763 mutex_unlock(&applesmc_lock);
764
765 return count;
766}
767
768/* Store the next backlight value to be written by the work */
769static unsigned int backlight_value;
770
771static void applesmc_backlight_set(struct work_struct *work)
772{
773 u8 buffer[2];
774
775 mutex_lock(&applesmc_lock);
776 buffer[0] = backlight_value;
777 buffer[1] = 0x00;
778 applesmc_write_key(BACKLIGHT_KEY, buffer, 2);
779 mutex_unlock(&applesmc_lock);
780}
781static DECLARE_WORK(backlight_work, &applesmc_backlight_set);
782
783static void applesmc_brightness_set(struct led_classdev *led_cdev,
784 enum led_brightness value)
785{
786 int ret;
787
788 backlight_value = value;
789 ret = queue_work(applesmc_led_wq, &backlight_work);
790
791 if (debug && (!ret))
792 printk(KERN_DEBUG "applesmc: work was already on the queue.\n");
793}
794
795static ssize_t applesmc_key_count_show(struct device *dev,
796 struct device_attribute *attr, char *sysfsbuf)
797{
798 int ret;
799 u8 buffer[4];
800 u32 count;
801
802 mutex_lock(&applesmc_lock);
803
804 ret = applesmc_read_key(KEY_COUNT_KEY, buffer, 4);
805 count = ((u32)buffer[0]<<24) + ((u32)buffer[1]<<16) +
806 ((u32)buffer[2]<<8) + buffer[3];
807
808 mutex_unlock(&applesmc_lock);
809 if (ret)
810 return ret;
811 else
812 return snprintf(sysfsbuf, PAGE_SIZE, "%d\n", count);
813}
814
815static ssize_t applesmc_key_at_index_read_show(struct device *dev,
816 struct device_attribute *attr, char *sysfsbuf)
817{
818 char key[5];
819 char info[6];
820 int ret;
821
822 mutex_lock(&applesmc_lock);
823
824 ret = applesmc_get_key_at_index(key_at_index, key);
825
826 if (ret || !key[0]) {
827 mutex_unlock(&applesmc_lock);
828
829 return -EINVAL;
830 }
831
832 ret = applesmc_get_key_type(key, info);
833
834 if (ret) {
835 mutex_unlock(&applesmc_lock);
836
837 return ret;
838 }
839
840 /*
841 * info[0] maximum value (APPLESMC_MAX_DATA_LENGTH) is much lower than
842 * PAGE_SIZE, so we don't need any checks before writing to sysfsbuf.
843 */
844 ret = applesmc_read_key(key, sysfsbuf, info[0]);
845
846 mutex_unlock(&applesmc_lock);
847
848 if (!ret) {
849 return info[0];
Dmitry Torokhovd5cf2b92007-09-26 00:01:35 -0400850 } else {
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700851 return ret;
852 }
853}
854
855static ssize_t applesmc_key_at_index_data_length_show(struct device *dev,
856 struct device_attribute *attr, char *sysfsbuf)
857{
858 char key[5];
859 char info[6];
860 int ret;
861
862 mutex_lock(&applesmc_lock);
863
864 ret = applesmc_get_key_at_index(key_at_index, key);
865
866 if (ret || !key[0]) {
867 mutex_unlock(&applesmc_lock);
868
869 return -EINVAL;
870 }
871
872 ret = applesmc_get_key_type(key, info);
873
874 mutex_unlock(&applesmc_lock);
875
876 if (!ret)
877 return snprintf(sysfsbuf, PAGE_SIZE, "%d\n", info[0]);
878 else
879 return ret;
880}
881
882static ssize_t applesmc_key_at_index_type_show(struct device *dev,
883 struct device_attribute *attr, char *sysfsbuf)
884{
885 char key[5];
886 char info[6];
887 int ret;
888
889 mutex_lock(&applesmc_lock);
890
891 ret = applesmc_get_key_at_index(key_at_index, key);
892
893 if (ret || !key[0]) {
894 mutex_unlock(&applesmc_lock);
895
896 return -EINVAL;
897 }
898
899 ret = applesmc_get_key_type(key, info);
900
901 mutex_unlock(&applesmc_lock);
902
903 if (!ret)
904 return snprintf(sysfsbuf, PAGE_SIZE, "%s\n", info+1);
905 else
906 return ret;
907}
908
909static ssize_t applesmc_key_at_index_name_show(struct device *dev,
910 struct device_attribute *attr, char *sysfsbuf)
911{
912 char key[5];
913 int ret;
914
915 mutex_lock(&applesmc_lock);
916
917 ret = applesmc_get_key_at_index(key_at_index, key);
918
919 mutex_unlock(&applesmc_lock);
920
921 if (!ret && key[0])
922 return snprintf(sysfsbuf, PAGE_SIZE, "%s\n", key);
923 else
924 return -EINVAL;
925}
926
927static ssize_t applesmc_key_at_index_show(struct device *dev,
928 struct device_attribute *attr, char *sysfsbuf)
929{
930 return snprintf(sysfsbuf, PAGE_SIZE, "%d\n", key_at_index);
931}
932
933static ssize_t applesmc_key_at_index_store(struct device *dev,
934 struct device_attribute *attr, const char *sysfsbuf, size_t count)
935{
936 mutex_lock(&applesmc_lock);
937
938 key_at_index = simple_strtoul(sysfsbuf, NULL, 10);
939
940 mutex_unlock(&applesmc_lock);
941
942 return count;
943}
944
945static struct led_classdev applesmc_backlight = {
Richard Purdie6c152be2007-10-31 15:00:07 +0100946 .name = "smc::kbd_backlight",
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700947 .default_trigger = "nand-disk",
948 .brightness_set = applesmc_brightness_set,
949};
950
Nicolas Boichatfa744192007-05-23 13:58:13 -0700951static DEVICE_ATTR(name, 0444, applesmc_name_show, NULL);
952
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700953static DEVICE_ATTR(position, 0444, applesmc_position_show, NULL);
954static DEVICE_ATTR(calibrate, 0644,
955 applesmc_calibrate_show, applesmc_calibrate_store);
956
957static struct attribute *accelerometer_attributes[] = {
958 &dev_attr_position.attr,
959 &dev_attr_calibrate.attr,
960 NULL
961};
962
963static const struct attribute_group accelerometer_attributes_group =
964 { .attrs = accelerometer_attributes };
965
966static DEVICE_ATTR(light, 0444, applesmc_light_show, NULL);
967
968static DEVICE_ATTR(key_count, 0444, applesmc_key_count_show, NULL);
969static DEVICE_ATTR(key_at_index, 0644,
970 applesmc_key_at_index_show, applesmc_key_at_index_store);
971static DEVICE_ATTR(key_at_index_name, 0444,
972 applesmc_key_at_index_name_show, NULL);
973static DEVICE_ATTR(key_at_index_type, 0444,
974 applesmc_key_at_index_type_show, NULL);
975static DEVICE_ATTR(key_at_index_data_length, 0444,
976 applesmc_key_at_index_data_length_show, NULL);
977static DEVICE_ATTR(key_at_index_data, 0444,
978 applesmc_key_at_index_read_show, NULL);
979
980static struct attribute *key_enumeration_attributes[] = {
981 &dev_attr_key_count.attr,
982 &dev_attr_key_at_index.attr,
983 &dev_attr_key_at_index_name.attr,
984 &dev_attr_key_at_index_type.attr,
985 &dev_attr_key_at_index_data_length.attr,
986 &dev_attr_key_at_index_data.attr,
987 NULL
988};
989
990static const struct attribute_group key_enumeration_group =
991 { .attrs = key_enumeration_attributes };
992
993/*
994 * Macro defining SENSOR_DEVICE_ATTR for a fan sysfs entries.
995 * - show actual speed
996 * - show/store minimum speed
997 * - show maximum speed
998 * - show safe speed
999 * - show/store target speed
1000 * - show/store manual mode
1001 */
1002#define sysfs_fan_speeds_offset(offset) \
1003static SENSOR_DEVICE_ATTR_2(fan##offset##_input, S_IRUGO, \
1004 applesmc_show_fan_speed, NULL, 0, offset-1); \
1005\
1006static SENSOR_DEVICE_ATTR_2(fan##offset##_min, S_IRUGO | S_IWUSR, \
1007 applesmc_show_fan_speed, applesmc_store_fan_speed, 1, offset-1); \
1008\
1009static SENSOR_DEVICE_ATTR_2(fan##offset##_max, S_IRUGO, \
1010 applesmc_show_fan_speed, NULL, 2, offset-1); \
1011\
1012static SENSOR_DEVICE_ATTR_2(fan##offset##_safe, S_IRUGO, \
1013 applesmc_show_fan_speed, NULL, 3, offset-1); \
1014\
1015static SENSOR_DEVICE_ATTR_2(fan##offset##_output, S_IRUGO | S_IWUSR, \
1016 applesmc_show_fan_speed, applesmc_store_fan_speed, 4, offset-1); \
1017\
1018static SENSOR_DEVICE_ATTR(fan##offset##_manual, S_IRUGO | S_IWUSR, \
1019 applesmc_show_fan_manual, applesmc_store_fan_manual, offset-1); \
1020\
Jean Delvareda4e8ca2007-05-08 20:27:05 -07001021static SENSOR_DEVICE_ATTR(fan##offset##_label, S_IRUGO, \
Nicolas Boichat6f2fad72007-05-08 00:24:52 -07001022 applesmc_show_fan_position, NULL, offset-1); \
1023\
1024static struct attribute *fan##offset##_attributes[] = { \
1025 &sensor_dev_attr_fan##offset##_input.dev_attr.attr, \
1026 &sensor_dev_attr_fan##offset##_min.dev_attr.attr, \
1027 &sensor_dev_attr_fan##offset##_max.dev_attr.attr, \
1028 &sensor_dev_attr_fan##offset##_safe.dev_attr.attr, \
1029 &sensor_dev_attr_fan##offset##_output.dev_attr.attr, \
1030 &sensor_dev_attr_fan##offset##_manual.dev_attr.attr, \
Jean Delvareda4e8ca2007-05-08 20:27:05 -07001031 &sensor_dev_attr_fan##offset##_label.dev_attr.attr, \
Nicolas Boichat6f2fad72007-05-08 00:24:52 -07001032 NULL \
1033};
1034
1035/*
1036 * Create the needed functions for each fan using the macro defined above
René Rebe8de57702007-10-16 14:19:20 -07001037 * (4 fans are supported)
Nicolas Boichat6f2fad72007-05-08 00:24:52 -07001038 */
1039sysfs_fan_speeds_offset(1);
1040sysfs_fan_speeds_offset(2);
René Rebe8de57702007-10-16 14:19:20 -07001041sysfs_fan_speeds_offset(3);
1042sysfs_fan_speeds_offset(4);
Nicolas Boichat6f2fad72007-05-08 00:24:52 -07001043
1044static const struct attribute_group fan_attribute_groups[] = {
1045 { .attrs = fan1_attributes },
René Rebe8de57702007-10-16 14:19:20 -07001046 { .attrs = fan2_attributes },
1047 { .attrs = fan3_attributes },
1048 { .attrs = fan4_attributes },
Nicolas Boichat6f2fad72007-05-08 00:24:52 -07001049};
1050
1051/*
1052 * Temperature sensors sysfs entries.
1053 */
1054static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO,
1055 applesmc_show_temperature, NULL, 0);
1056static SENSOR_DEVICE_ATTR(temp2_input, S_IRUGO,
1057 applesmc_show_temperature, NULL, 1);
1058static SENSOR_DEVICE_ATTR(temp3_input, S_IRUGO,
1059 applesmc_show_temperature, NULL, 2);
1060static SENSOR_DEVICE_ATTR(temp4_input, S_IRUGO,
1061 applesmc_show_temperature, NULL, 3);
1062static SENSOR_DEVICE_ATTR(temp5_input, S_IRUGO,
1063 applesmc_show_temperature, NULL, 4);
1064static SENSOR_DEVICE_ATTR(temp6_input, S_IRUGO,
1065 applesmc_show_temperature, NULL, 5);
1066static SENSOR_DEVICE_ATTR(temp7_input, S_IRUGO,
1067 applesmc_show_temperature, NULL, 6);
1068static SENSOR_DEVICE_ATTR(temp8_input, S_IRUGO,
1069 applesmc_show_temperature, NULL, 7);
1070static SENSOR_DEVICE_ATTR(temp9_input, S_IRUGO,
1071 applesmc_show_temperature, NULL, 8);
1072static SENSOR_DEVICE_ATTR(temp10_input, S_IRUGO,
1073 applesmc_show_temperature, NULL, 9);
1074static SENSOR_DEVICE_ATTR(temp11_input, S_IRUGO,
1075 applesmc_show_temperature, NULL, 10);
1076static SENSOR_DEVICE_ATTR(temp12_input, S_IRUGO,
1077 applesmc_show_temperature, NULL, 11);
René Rebe8de57702007-10-16 14:19:20 -07001078static SENSOR_DEVICE_ATTR(temp13_input, S_IRUGO,
1079 applesmc_show_temperature, NULL, 12);
1080static SENSOR_DEVICE_ATTR(temp14_input, S_IRUGO,
1081 applesmc_show_temperature, NULL, 13);
1082static SENSOR_DEVICE_ATTR(temp15_input, S_IRUGO,
1083 applesmc_show_temperature, NULL, 14);
1084static SENSOR_DEVICE_ATTR(temp16_input, S_IRUGO,
1085 applesmc_show_temperature, NULL, 15);
1086static SENSOR_DEVICE_ATTR(temp17_input, S_IRUGO,
1087 applesmc_show_temperature, NULL, 16);
1088static SENSOR_DEVICE_ATTR(temp18_input, S_IRUGO,
1089 applesmc_show_temperature, NULL, 17);
1090static SENSOR_DEVICE_ATTR(temp19_input, S_IRUGO,
1091 applesmc_show_temperature, NULL, 18);
1092static SENSOR_DEVICE_ATTR(temp20_input, S_IRUGO,
1093 applesmc_show_temperature, NULL, 19);
1094static SENSOR_DEVICE_ATTR(temp21_input, S_IRUGO,
1095 applesmc_show_temperature, NULL, 20);
1096static SENSOR_DEVICE_ATTR(temp22_input, S_IRUGO,
1097 applesmc_show_temperature, NULL, 21);
1098static SENSOR_DEVICE_ATTR(temp23_input, S_IRUGO,
1099 applesmc_show_temperature, NULL, 22);
1100static SENSOR_DEVICE_ATTR(temp24_input, S_IRUGO,
1101 applesmc_show_temperature, NULL, 23);
1102static SENSOR_DEVICE_ATTR(temp25_input, S_IRUGO,
1103 applesmc_show_temperature, NULL, 24);
1104static SENSOR_DEVICE_ATTR(temp26_input, S_IRUGO,
1105 applesmc_show_temperature, NULL, 25);
1106static SENSOR_DEVICE_ATTR(temp27_input, S_IRUGO,
1107 applesmc_show_temperature, NULL, 26);
1108static SENSOR_DEVICE_ATTR(temp28_input, S_IRUGO,
1109 applesmc_show_temperature, NULL, 27);
1110static SENSOR_DEVICE_ATTR(temp29_input, S_IRUGO,
1111 applesmc_show_temperature, NULL, 28);
1112static SENSOR_DEVICE_ATTR(temp30_input, S_IRUGO,
1113 applesmc_show_temperature, NULL, 29);
1114static SENSOR_DEVICE_ATTR(temp31_input, S_IRUGO,
1115 applesmc_show_temperature, NULL, 30);
1116static SENSOR_DEVICE_ATTR(temp32_input, S_IRUGO,
1117 applesmc_show_temperature, NULL, 31);
1118static SENSOR_DEVICE_ATTR(temp33_input, S_IRUGO,
1119 applesmc_show_temperature, NULL, 32);
1120static SENSOR_DEVICE_ATTR(temp34_input, S_IRUGO,
1121 applesmc_show_temperature, NULL, 33);
1122static SENSOR_DEVICE_ATTR(temp35_input, S_IRUGO,
1123 applesmc_show_temperature, NULL, 34);
Nicolas Boichat6f2fad72007-05-08 00:24:52 -07001124
1125static struct attribute *temperature_attributes[] = {
1126 &sensor_dev_attr_temp1_input.dev_attr.attr,
1127 &sensor_dev_attr_temp2_input.dev_attr.attr,
1128 &sensor_dev_attr_temp3_input.dev_attr.attr,
1129 &sensor_dev_attr_temp4_input.dev_attr.attr,
1130 &sensor_dev_attr_temp5_input.dev_attr.attr,
1131 &sensor_dev_attr_temp6_input.dev_attr.attr,
1132 &sensor_dev_attr_temp7_input.dev_attr.attr,
1133 &sensor_dev_attr_temp8_input.dev_attr.attr,
1134 &sensor_dev_attr_temp9_input.dev_attr.attr,
1135 &sensor_dev_attr_temp10_input.dev_attr.attr,
1136 &sensor_dev_attr_temp11_input.dev_attr.attr,
1137 &sensor_dev_attr_temp12_input.dev_attr.attr,
René Rebe8de57702007-10-16 14:19:20 -07001138 &sensor_dev_attr_temp13_input.dev_attr.attr,
1139 &sensor_dev_attr_temp14_input.dev_attr.attr,
1140 &sensor_dev_attr_temp15_input.dev_attr.attr,
1141 &sensor_dev_attr_temp16_input.dev_attr.attr,
1142 &sensor_dev_attr_temp17_input.dev_attr.attr,
1143 &sensor_dev_attr_temp18_input.dev_attr.attr,
1144 &sensor_dev_attr_temp19_input.dev_attr.attr,
1145 &sensor_dev_attr_temp20_input.dev_attr.attr,
1146 &sensor_dev_attr_temp21_input.dev_attr.attr,
1147 &sensor_dev_attr_temp22_input.dev_attr.attr,
1148 &sensor_dev_attr_temp23_input.dev_attr.attr,
1149 &sensor_dev_attr_temp24_input.dev_attr.attr,
1150 &sensor_dev_attr_temp25_input.dev_attr.attr,
1151 &sensor_dev_attr_temp26_input.dev_attr.attr,
1152 &sensor_dev_attr_temp27_input.dev_attr.attr,
1153 &sensor_dev_attr_temp28_input.dev_attr.attr,
1154 &sensor_dev_attr_temp29_input.dev_attr.attr,
1155 &sensor_dev_attr_temp30_input.dev_attr.attr,
1156 &sensor_dev_attr_temp31_input.dev_attr.attr,
1157 &sensor_dev_attr_temp32_input.dev_attr.attr,
1158 &sensor_dev_attr_temp33_input.dev_attr.attr,
1159 &sensor_dev_attr_temp34_input.dev_attr.attr,
1160 &sensor_dev_attr_temp35_input.dev_attr.attr,
Nicolas Boichat6f2fad72007-05-08 00:24:52 -07001161 NULL
1162};
1163
1164static const struct attribute_group temperature_attributes_group =
1165 { .attrs = temperature_attributes };
1166
1167/* Module stuff */
1168
1169/*
1170 * applesmc_dmi_match - found a match. return one, short-circuiting the hunt.
1171 */
Jeff Garzik18552562007-10-03 15:15:40 -04001172static int applesmc_dmi_match(const struct dmi_system_id *id)
Nicolas Boichat6f2fad72007-05-08 00:24:52 -07001173{
1174 int i = 0;
1175 struct dmi_match_data* dmi_data = id->driver_data;
1176 printk(KERN_INFO "applesmc: %s detected:\n", id->ident);
1177 applesmc_accelerometer = dmi_data->accelerometer;
1178 printk(KERN_INFO "applesmc: - Model %s accelerometer\n",
1179 applesmc_accelerometer ? "with" : "without");
1180 applesmc_light = dmi_data->light;
1181 printk(KERN_INFO "applesmc: - Model %s light sensors and backlight\n",
1182 applesmc_light ? "with" : "without");
1183
1184 applesmc_temperature_set = dmi_data->temperature_set;
1185 while (temperature_sensors_sets[applesmc_temperature_set][i] != NULL)
1186 i++;
1187 printk(KERN_INFO "applesmc: - Model with %d temperature sensors\n", i);
1188 return 1;
1189}
1190
1191/* Create accelerometer ressources */
1192static int applesmc_create_accelerometer(void)
1193{
Dmitry Torokhovd5cf2b92007-09-26 00:01:35 -04001194 struct input_dev *idev;
Nicolas Boichat6f2fad72007-05-08 00:24:52 -07001195 int ret;
1196
1197 ret = sysfs_create_group(&pdev->dev.kobj,
1198 &accelerometer_attributes_group);
1199 if (ret)
1200 goto out;
1201
Dmitry Torokhovd5cf2b92007-09-26 00:01:35 -04001202 applesmc_idev = input_allocate_polled_device();
Nicolas Boichat6f2fad72007-05-08 00:24:52 -07001203 if (!applesmc_idev) {
1204 ret = -ENOMEM;
1205 goto out_sysfs;
1206 }
1207
Dmitry Torokhovd5cf2b92007-09-26 00:01:35 -04001208 applesmc_idev->poll = applesmc_idev_poll;
1209 applesmc_idev->poll_interval = APPLESMC_POLL_INTERVAL;
1210
Nicolas Boichat6f2fad72007-05-08 00:24:52 -07001211 /* initial calibrate for the input device */
1212 applesmc_calibrate();
1213
Dmitry Torokhovd5cf2b92007-09-26 00:01:35 -04001214 /* initialize the input device */
1215 idev = applesmc_idev->input;
1216 idev->name = "applesmc";
1217 idev->id.bustype = BUS_HOST;
1218 idev->dev.parent = &pdev->dev;
Jiri Slaby7b19ada2007-10-18 23:40:32 -07001219 idev->evbit[0] = BIT_MASK(EV_ABS);
Dmitry Torokhovd5cf2b92007-09-26 00:01:35 -04001220 input_set_abs_params(idev, ABS_X,
Nicolas Boichat6f2fad72007-05-08 00:24:52 -07001221 -256, 256, APPLESMC_INPUT_FUZZ, APPLESMC_INPUT_FLAT);
Dmitry Torokhovd5cf2b92007-09-26 00:01:35 -04001222 input_set_abs_params(idev, ABS_Y,
Nicolas Boichat6f2fad72007-05-08 00:24:52 -07001223 -256, 256, APPLESMC_INPUT_FUZZ, APPLESMC_INPUT_FLAT);
1224
Dmitry Torokhovd5cf2b92007-09-26 00:01:35 -04001225 ret = input_register_polled_device(applesmc_idev);
Nicolas Boichat6f2fad72007-05-08 00:24:52 -07001226 if (ret)
1227 goto out_idev;
1228
Nicolas Boichat6f2fad72007-05-08 00:24:52 -07001229 return 0;
1230
1231out_idev:
Dmitry Torokhovd5cf2b92007-09-26 00:01:35 -04001232 input_free_polled_device(applesmc_idev);
Nicolas Boichat6f2fad72007-05-08 00:24:52 -07001233
1234out_sysfs:
1235 sysfs_remove_group(&pdev->dev.kobj, &accelerometer_attributes_group);
1236
1237out:
1238 printk(KERN_WARNING "applesmc: driver init failed (ret=%d)!\n", ret);
1239 return ret;
1240}
1241
1242/* Release all ressources used by the accelerometer */
1243static void applesmc_release_accelerometer(void)
1244{
Dmitry Torokhovd5cf2b92007-09-26 00:01:35 -04001245 input_unregister_polled_device(applesmc_idev);
1246 input_free_polled_device(applesmc_idev);
Nicolas Boichat6f2fad72007-05-08 00:24:52 -07001247 sysfs_remove_group(&pdev->dev.kobj, &accelerometer_attributes_group);
1248}
1249
1250static __initdata struct dmi_match_data applesmc_dmi_data[] = {
1251/* MacBook Pro: accelerometer, backlight and temperature set 0 */
1252 { .accelerometer = 1, .light = 1, .temperature_set = 0 },
Riki Oktariantocd19ba12008-02-04 23:41:58 -08001253/* MacBook2: accelerometer and temperature set 1 */
Martin Szulecki1bed24b2007-07-09 11:41:36 -07001254 { .accelerometer = 1, .light = 0, .temperature_set = 1 },
Riki Oktariantocd19ba12008-02-04 23:41:58 -08001255/* MacBook: accelerometer and temperature set 2 */
1256 { .accelerometer = 1, .light = 0, .temperature_set = 2 },
1257/* MacMini: temperature set 3 */
René Rebe8de57702007-10-16 14:19:20 -07001258 { .accelerometer = 0, .light = 0, .temperature_set = 3 },
Riki Oktariantocd19ba12008-02-04 23:41:58 -08001259/* MacPro: temperature set 4 */
1260 { .accelerometer = 0, .light = 0, .temperature_set = 4 },
Roberto De Ioris9f86f282008-08-15 00:40:30 -07001261/* iMac: temperature set 5 */
1262 { .accelerometer = 0, .light = 0, .temperature_set = 5 },
Guilherme M. Schroederf91a79f2008-08-15 00:40:32 -07001263/* MacBook3: accelerometer and temperature set 6 */
1264 { .accelerometer = 1, .light = 0, .temperature_set = 6 },
Henrik Rydbergf5274c92008-10-18 20:27:40 -07001265/* MacBook Air: accelerometer, backlight and temperature set 7 */
1266 { .accelerometer = 1, .light = 1, .temperature_set = 7 },
Nicolas Boichat6f2fad72007-05-08 00:24:52 -07001267};
1268
1269/* Note that DMI_MATCH(...,"MacBook") will match "MacBookPro1,1".
1270 * So we need to put "Apple MacBook Pro" before "Apple MacBook". */
1271static __initdata struct dmi_system_id applesmc_whitelist[] = {
Henrik Rydbergf5274c92008-10-18 20:27:40 -07001272 { applesmc_dmi_match, "Apple MacBook Air", {
1273 DMI_MATCH(DMI_BOARD_VENDOR, "Apple"),
1274 DMI_MATCH(DMI_PRODUCT_NAME, "MacBookAir") },
1275 (void*)&applesmc_dmi_data[7]},
Nicolas Boichat6f2fad72007-05-08 00:24:52 -07001276 { applesmc_dmi_match, "Apple MacBook Pro", {
1277 DMI_MATCH(DMI_BOARD_VENDOR,"Apple"),
1278 DMI_MATCH(DMI_PRODUCT_NAME,"MacBookPro") },
1279 (void*)&applesmc_dmi_data[0]},
Guilherme M. Schroederf91a79f2008-08-15 00:40:32 -07001280 { applesmc_dmi_match, "Apple MacBook (v2)", {
Nicolas Boichat6f2fad72007-05-08 00:24:52 -07001281 DMI_MATCH(DMI_BOARD_VENDOR,"Apple"),
Riki Oktariantocd19ba12008-02-04 23:41:58 -08001282 DMI_MATCH(DMI_PRODUCT_NAME,"MacBook2") },
Nicolas Boichat6f2fad72007-05-08 00:24:52 -07001283 (void*)&applesmc_dmi_data[1]},
Guilherme M. Schroederf91a79f2008-08-15 00:40:32 -07001284 { applesmc_dmi_match, "Apple MacBook (v3)", {
1285 DMI_MATCH(DMI_BOARD_VENDOR,"Apple"),
1286 DMI_MATCH(DMI_PRODUCT_NAME,"MacBook3") },
1287 (void*)&applesmc_dmi_data[6]},
Riki Oktariantocd19ba12008-02-04 23:41:58 -08001288 { applesmc_dmi_match, "Apple MacBook", {
1289 DMI_MATCH(DMI_BOARD_VENDOR,"Apple"),
1290 DMI_MATCH(DMI_PRODUCT_NAME,"MacBook") },
1291 (void*)&applesmc_dmi_data[2]},
Nicolas Boichat6f2fad72007-05-08 00:24:52 -07001292 { applesmc_dmi_match, "Apple Macmini", {
1293 DMI_MATCH(DMI_BOARD_VENDOR,"Apple"),
1294 DMI_MATCH(DMI_PRODUCT_NAME,"Macmini") },
Riki Oktariantocd19ba12008-02-04 23:41:58 -08001295 (void*)&applesmc_dmi_data[3]},
René Rebe8de57702007-10-16 14:19:20 -07001296 { applesmc_dmi_match, "Apple MacPro2", {
1297 DMI_MATCH(DMI_BOARD_VENDOR,"Apple"),
1298 DMI_MATCH(DMI_PRODUCT_NAME,"MacPro2") },
Riki Oktariantocd19ba12008-02-04 23:41:58 -08001299 (void*)&applesmc_dmi_data[4]},
Roberto De Ioris9f86f282008-08-15 00:40:30 -07001300 { applesmc_dmi_match, "Apple iMac", {
1301 DMI_MATCH(DMI_BOARD_VENDOR,"Apple"),
1302 DMI_MATCH(DMI_PRODUCT_NAME,"iMac") },
1303 (void*)&applesmc_dmi_data[5]},
Nicolas Boichat6f2fad72007-05-08 00:24:52 -07001304 { .ident = NULL }
1305};
1306
1307static int __init applesmc_init(void)
1308{
1309 int ret;
1310 int count;
1311 int i;
1312
Nicolas Boichat6f2fad72007-05-08 00:24:52 -07001313 if (!dmi_check_system(applesmc_whitelist)) {
1314 printk(KERN_WARNING "applesmc: supported laptop not found!\n");
1315 ret = -ENODEV;
1316 goto out;
1317 }
1318
1319 if (!request_region(APPLESMC_DATA_PORT, APPLESMC_NR_PORTS,
1320 "applesmc")) {
1321 ret = -ENXIO;
1322 goto out;
1323 }
1324
1325 ret = platform_driver_register(&applesmc_driver);
1326 if (ret)
1327 goto out_region;
1328
Jean Delvareddfbf2a2007-05-08 20:27:04 -07001329 pdev = platform_device_register_simple("applesmc", APPLESMC_DATA_PORT,
1330 NULL, 0);
Nicolas Boichat6f2fad72007-05-08 00:24:52 -07001331 if (IS_ERR(pdev)) {
1332 ret = PTR_ERR(pdev);
1333 goto out_driver;
1334 }
1335
Nicolas Boichatfa744192007-05-23 13:58:13 -07001336 ret = sysfs_create_file(&pdev->dev.kobj, &dev_attr_name.attr);
Nicolas Boichat6996abf2007-05-27 22:17:43 +02001337 if (ret)
1338 goto out_device;
Nicolas Boichatfa744192007-05-23 13:58:13 -07001339
Nicolas Boichat6f2fad72007-05-08 00:24:52 -07001340 /* Create key enumeration sysfs files */
1341 ret = sysfs_create_group(&pdev->dev.kobj, &key_enumeration_group);
1342 if (ret)
Nicolas Boichat6996abf2007-05-27 22:17:43 +02001343 goto out_name;
Nicolas Boichat6f2fad72007-05-08 00:24:52 -07001344
1345 /* create fan files */
1346 count = applesmc_get_fan_count();
1347 if (count < 0) {
1348 printk(KERN_ERR "applesmc: Cannot get the number of fans.\n");
1349 } else {
1350 printk(KERN_INFO "applesmc: %d fans found.\n", count);
1351
1352 switch (count) {
1353 default:
René Rebe8de57702007-10-16 14:19:20 -07001354 printk(KERN_WARNING "applesmc: More than 4 fans found,"
1355 " but at most 4 fans are supported"
Nicolas Boichat6f2fad72007-05-08 00:24:52 -07001356 " by the driver.\n");
René Rebe8de57702007-10-16 14:19:20 -07001357 case 4:
1358 ret = sysfs_create_group(&pdev->dev.kobj,
1359 &fan_attribute_groups[3]);
1360 if (ret)
1361 goto out_key_enumeration;
1362 case 3:
1363 ret = sysfs_create_group(&pdev->dev.kobj,
1364 &fan_attribute_groups[2]);
1365 if (ret)
1366 goto out_key_enumeration;
Nicolas Boichat6f2fad72007-05-08 00:24:52 -07001367 case 2:
1368 ret = sysfs_create_group(&pdev->dev.kobj,
1369 &fan_attribute_groups[1]);
1370 if (ret)
1371 goto out_key_enumeration;
1372 case 1:
1373 ret = sysfs_create_group(&pdev->dev.kobj,
1374 &fan_attribute_groups[0]);
1375 if (ret)
1376 goto out_fan_1;
1377 case 0:
1378 ;
1379 }
1380 }
1381
1382 for (i = 0;
1383 temperature_sensors_sets[applesmc_temperature_set][i] != NULL;
1384 i++) {
1385 if (temperature_attributes[i] == NULL) {
1386 printk(KERN_ERR "applesmc: More temperature sensors "
1387 "in temperature_sensors_sets (at least %i)"
1388 "than available sysfs files in "
1389 "temperature_attributes (%i), please report "
1390 "this bug.\n", i, i-1);
1391 goto out_temperature;
1392 }
1393 ret = sysfs_create_file(&pdev->dev.kobj,
1394 temperature_attributes[i]);
1395 if (ret)
1396 goto out_temperature;
1397 }
1398
1399 if (applesmc_accelerometer) {
1400 ret = applesmc_create_accelerometer();
1401 if (ret)
1402 goto out_temperature;
1403 }
1404
1405 if (applesmc_light) {
1406 /* Add light sensor file */
1407 ret = sysfs_create_file(&pdev->dev.kobj, &dev_attr_light.attr);
1408 if (ret)
1409 goto out_accelerometer;
1410
1411 /* Create the workqueue */
1412 applesmc_led_wq = create_singlethread_workqueue("applesmc-led");
1413 if (!applesmc_led_wq) {
1414 ret = -ENOMEM;
1415 goto out_light_sysfs;
1416 }
1417
1418 /* register as a led device */
1419 ret = led_classdev_register(&pdev->dev, &applesmc_backlight);
1420 if (ret < 0)
1421 goto out_light_wq;
1422 }
1423
Tony Jones1beeffe2007-08-20 13:46:20 -07001424 hwmon_dev = hwmon_device_register(&pdev->dev);
1425 if (IS_ERR(hwmon_dev)) {
1426 ret = PTR_ERR(hwmon_dev);
Nicolas Boichat6f2fad72007-05-08 00:24:52 -07001427 goto out_light_ledclass;
1428 }
1429
1430 printk(KERN_INFO "applesmc: driver successfully loaded.\n");
1431
1432 return 0;
1433
1434out_light_ledclass:
1435 if (applesmc_light)
1436 led_classdev_unregister(&applesmc_backlight);
1437out_light_wq:
1438 if (applesmc_light)
1439 destroy_workqueue(applesmc_led_wq);
1440out_light_sysfs:
1441 if (applesmc_light)
1442 sysfs_remove_file(&pdev->dev.kobj, &dev_attr_light.attr);
1443out_accelerometer:
1444 if (applesmc_accelerometer)
1445 applesmc_release_accelerometer();
1446out_temperature:
1447 sysfs_remove_group(&pdev->dev.kobj, &temperature_attributes_group);
1448 sysfs_remove_group(&pdev->dev.kobj, &fan_attribute_groups[0]);
1449out_fan_1:
1450 sysfs_remove_group(&pdev->dev.kobj, &fan_attribute_groups[1]);
1451out_key_enumeration:
1452 sysfs_remove_group(&pdev->dev.kobj, &key_enumeration_group);
Nicolas Boichat6996abf2007-05-27 22:17:43 +02001453out_name:
1454 sysfs_remove_file(&pdev->dev.kobj, &dev_attr_name.attr);
Nicolas Boichat6f2fad72007-05-08 00:24:52 -07001455out_device:
1456 platform_device_unregister(pdev);
1457out_driver:
1458 platform_driver_unregister(&applesmc_driver);
1459out_region:
1460 release_region(APPLESMC_DATA_PORT, APPLESMC_NR_PORTS);
1461out:
1462 printk(KERN_WARNING "applesmc: driver init failed (ret=%d)!\n", ret);
1463 return ret;
1464}
1465
1466static void __exit applesmc_exit(void)
1467{
Tony Jones1beeffe2007-08-20 13:46:20 -07001468 hwmon_device_unregister(hwmon_dev);
Nicolas Boichat6f2fad72007-05-08 00:24:52 -07001469 if (applesmc_light) {
1470 led_classdev_unregister(&applesmc_backlight);
1471 destroy_workqueue(applesmc_led_wq);
1472 sysfs_remove_file(&pdev->dev.kobj, &dev_attr_light.attr);
1473 }
1474 if (applesmc_accelerometer)
1475 applesmc_release_accelerometer();
1476 sysfs_remove_group(&pdev->dev.kobj, &temperature_attributes_group);
1477 sysfs_remove_group(&pdev->dev.kobj, &fan_attribute_groups[0]);
1478 sysfs_remove_group(&pdev->dev.kobj, &fan_attribute_groups[1]);
1479 sysfs_remove_group(&pdev->dev.kobj, &key_enumeration_group);
Nicolas Boichat6996abf2007-05-27 22:17:43 +02001480 sysfs_remove_file(&pdev->dev.kobj, &dev_attr_name.attr);
Nicolas Boichat6f2fad72007-05-08 00:24:52 -07001481 platform_device_unregister(pdev);
1482 platform_driver_unregister(&applesmc_driver);
1483 release_region(APPLESMC_DATA_PORT, APPLESMC_NR_PORTS);
1484
1485 printk(KERN_INFO "applesmc: driver unloaded.\n");
1486}
1487
1488module_init(applesmc_init);
1489module_exit(applesmc_exit);
1490
1491MODULE_AUTHOR("Nicolas Boichat");
1492MODULE_DESCRIPTION("Apple SMC");
1493MODULE_LICENSE("GPL v2");