blob: de696b67a4fcb71d49cf7f53839f50eb56d86023 [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 },
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/*
Henrik Rydberg02fcbd12008-10-18 20:27:39 -0700166 * __wait_status - Wait up to 10ms for the status port to get a certain value
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700167 * (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
Henrik Rydberg02fcbd12008-10-18 20:27:39 -0700176 for (i = 0; i < 1000; i++) {
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700177 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{
Henrik Rydberg8bd1a122008-10-18 20:27:39 -0700546 static int data_length;
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700547 int ret;
548 u8 left = 0, right = 0;
Henrik Rydberg8bd1a122008-10-18 20:27:39 -0700549 u8 buffer[10], query[6];
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700550
551 mutex_lock(&applesmc_lock);
552
Henrik Rydberg8bd1a122008-10-18 20:27:39 -0700553 if (!data_length) {
554 ret = applesmc_get_key_type(LIGHT_SENSOR_LEFT_KEY, query);
555 if (ret)
556 goto out;
557 data_length = clamp_val(query[0], 0, 10);
558 printk(KERN_INFO "applesmc: light sensor data length set to "
559 "%d\n", data_length);
560 }
561
562 ret = applesmc_read_key(LIGHT_SENSOR_LEFT_KEY, buffer, data_length);
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700563 left = buffer[2];
564 if (ret)
565 goto out;
Henrik Rydberg8bd1a122008-10-18 20:27:39 -0700566 ret = applesmc_read_key(LIGHT_SENSOR_RIGHT_KEY, buffer, data_length);
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700567 right = buffer[2];
568
569out:
570 mutex_unlock(&applesmc_lock);
571 if (ret)
572 return ret;
573 else
574 return snprintf(sysfsbuf, PAGE_SIZE, "(%d,%d)\n", left, right);
575}
576
577/* Displays degree Celsius * 1000 */
578static ssize_t applesmc_show_temperature(struct device *dev,
579 struct device_attribute *devattr, char *sysfsbuf)
580{
581 int ret;
582 u8 buffer[2];
583 unsigned int temp;
584 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
585 const char* key =
586 temperature_sensors_sets[applesmc_temperature_set][attr->index];
587
588 mutex_lock(&applesmc_lock);
589
590 ret = applesmc_read_key(key, buffer, 2);
591 temp = buffer[0]*1000;
592 temp += (buffer[1] >> 6) * 250;
593
594 mutex_unlock(&applesmc_lock);
595
596 if (ret)
597 return ret;
598 else
599 return snprintf(sysfsbuf, PAGE_SIZE, "%u\n", temp);
600}
601
602static ssize_t applesmc_show_fan_speed(struct device *dev,
603 struct device_attribute *attr, char *sysfsbuf)
604{
605 int ret;
606 unsigned int speed = 0;
607 char newkey[5];
608 u8 buffer[2];
609 struct sensor_device_attribute_2 *sensor_attr =
610 to_sensor_dev_attr_2(attr);
611
612 newkey[0] = fan_speed_keys[sensor_attr->nr][0];
613 newkey[1] = '0' + sensor_attr->index;
614 newkey[2] = fan_speed_keys[sensor_attr->nr][2];
615 newkey[3] = fan_speed_keys[sensor_attr->nr][3];
616 newkey[4] = 0;
617
618 mutex_lock(&applesmc_lock);
619
620 ret = applesmc_read_key(newkey, buffer, 2);
621 speed = ((buffer[0] << 8 | buffer[1]) >> 2);
622
623 mutex_unlock(&applesmc_lock);
624 if (ret)
625 return ret;
626 else
627 return snprintf(sysfsbuf, PAGE_SIZE, "%u\n", speed);
628}
629
630static ssize_t applesmc_store_fan_speed(struct device *dev,
631 struct device_attribute *attr,
632 const char *sysfsbuf, size_t count)
633{
634 int ret;
635 u32 speed;
636 char newkey[5];
637 u8 buffer[2];
638 struct sensor_device_attribute_2 *sensor_attr =
639 to_sensor_dev_attr_2(attr);
640
641 speed = simple_strtoul(sysfsbuf, NULL, 10);
642
643 if (speed > 0x4000) /* Bigger than a 14-bit value */
644 return -EINVAL;
645
646 newkey[0] = fan_speed_keys[sensor_attr->nr][0];
647 newkey[1] = '0' + sensor_attr->index;
648 newkey[2] = fan_speed_keys[sensor_attr->nr][2];
649 newkey[3] = fan_speed_keys[sensor_attr->nr][3];
650 newkey[4] = 0;
651
652 mutex_lock(&applesmc_lock);
653
654 buffer[0] = (speed >> 6) & 0xff;
655 buffer[1] = (speed << 2) & 0xff;
656 ret = applesmc_write_key(newkey, buffer, 2);
657
658 mutex_unlock(&applesmc_lock);
659 if (ret)
660 return ret;
661 else
662 return count;
663}
664
665static ssize_t applesmc_show_fan_manual(struct device *dev,
666 struct device_attribute *devattr, char *sysfsbuf)
667{
668 int ret;
669 u16 manual = 0;
670 u8 buffer[2];
671 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
672
673 mutex_lock(&applesmc_lock);
674
675 ret = applesmc_read_key(FANS_MANUAL, buffer, 2);
676 manual = ((buffer[0] << 8 | buffer[1]) >> attr->index) & 0x01;
677
678 mutex_unlock(&applesmc_lock);
679 if (ret)
680 return ret;
681 else
682 return snprintf(sysfsbuf, PAGE_SIZE, "%d\n", manual);
683}
684
685static ssize_t applesmc_store_fan_manual(struct device *dev,
686 struct device_attribute *devattr,
687 const char *sysfsbuf, size_t count)
688{
689 int ret;
690 u8 buffer[2];
691 u32 input;
692 u16 val;
693 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
694
695 input = simple_strtoul(sysfsbuf, NULL, 10);
696
697 mutex_lock(&applesmc_lock);
698
699 ret = applesmc_read_key(FANS_MANUAL, buffer, 2);
700 val = (buffer[0] << 8 | buffer[1]);
701 if (ret)
702 goto out;
703
704 if (input)
705 val = val | (0x01 << attr->index);
706 else
707 val = val & ~(0x01 << attr->index);
708
709 buffer[0] = (val >> 8) & 0xFF;
710 buffer[1] = val & 0xFF;
711
712 ret = applesmc_write_key(FANS_MANUAL, buffer, 2);
713
714out:
715 mutex_unlock(&applesmc_lock);
716 if (ret)
717 return ret;
718 else
719 return count;
720}
721
722static ssize_t applesmc_show_fan_position(struct device *dev,
723 struct device_attribute *attr, char *sysfsbuf)
724{
725 int ret;
726 char newkey[5];
727 u8 buffer[17];
728 struct sensor_device_attribute_2 *sensor_attr =
729 to_sensor_dev_attr_2(attr);
730
731 newkey[0] = FAN_POSITION[0];
732 newkey[1] = '0' + sensor_attr->index;
733 newkey[2] = FAN_POSITION[2];
734 newkey[3] = FAN_POSITION[3];
735 newkey[4] = 0;
736
737 mutex_lock(&applesmc_lock);
738
739 ret = applesmc_read_key(newkey, buffer, 16);
740 buffer[16] = 0;
741
742 mutex_unlock(&applesmc_lock);
743 if (ret)
744 return ret;
745 else
746 return snprintf(sysfsbuf, PAGE_SIZE, "%s\n", buffer+4);
747}
748
749static ssize_t applesmc_calibrate_show(struct device *dev,
750 struct device_attribute *attr, char *sysfsbuf)
751{
752 return snprintf(sysfsbuf, PAGE_SIZE, "(%d,%d)\n", rest_x, rest_y);
753}
754
755static ssize_t applesmc_calibrate_store(struct device *dev,
756 struct device_attribute *attr, const char *sysfsbuf, size_t count)
757{
758 mutex_lock(&applesmc_lock);
759 applesmc_calibrate();
760 mutex_unlock(&applesmc_lock);
761
762 return count;
763}
764
765/* Store the next backlight value to be written by the work */
766static unsigned int backlight_value;
767
768static void applesmc_backlight_set(struct work_struct *work)
769{
770 u8 buffer[2];
771
772 mutex_lock(&applesmc_lock);
773 buffer[0] = backlight_value;
774 buffer[1] = 0x00;
775 applesmc_write_key(BACKLIGHT_KEY, buffer, 2);
776 mutex_unlock(&applesmc_lock);
777}
778static DECLARE_WORK(backlight_work, &applesmc_backlight_set);
779
780static void applesmc_brightness_set(struct led_classdev *led_cdev,
781 enum led_brightness value)
782{
783 int ret;
784
785 backlight_value = value;
786 ret = queue_work(applesmc_led_wq, &backlight_work);
787
788 if (debug && (!ret))
789 printk(KERN_DEBUG "applesmc: work was already on the queue.\n");
790}
791
792static ssize_t applesmc_key_count_show(struct device *dev,
793 struct device_attribute *attr, char *sysfsbuf)
794{
795 int ret;
796 u8 buffer[4];
797 u32 count;
798
799 mutex_lock(&applesmc_lock);
800
801 ret = applesmc_read_key(KEY_COUNT_KEY, buffer, 4);
802 count = ((u32)buffer[0]<<24) + ((u32)buffer[1]<<16) +
803 ((u32)buffer[2]<<8) + buffer[3];
804
805 mutex_unlock(&applesmc_lock);
806 if (ret)
807 return ret;
808 else
809 return snprintf(sysfsbuf, PAGE_SIZE, "%d\n", count);
810}
811
812static ssize_t applesmc_key_at_index_read_show(struct device *dev,
813 struct device_attribute *attr, char *sysfsbuf)
814{
815 char key[5];
816 char info[6];
817 int ret;
818
819 mutex_lock(&applesmc_lock);
820
821 ret = applesmc_get_key_at_index(key_at_index, key);
822
823 if (ret || !key[0]) {
824 mutex_unlock(&applesmc_lock);
825
826 return -EINVAL;
827 }
828
829 ret = applesmc_get_key_type(key, info);
830
831 if (ret) {
832 mutex_unlock(&applesmc_lock);
833
834 return ret;
835 }
836
837 /*
838 * info[0] maximum value (APPLESMC_MAX_DATA_LENGTH) is much lower than
839 * PAGE_SIZE, so we don't need any checks before writing to sysfsbuf.
840 */
841 ret = applesmc_read_key(key, sysfsbuf, info[0]);
842
843 mutex_unlock(&applesmc_lock);
844
845 if (!ret) {
846 return info[0];
Dmitry Torokhovd5cf2b92007-09-26 00:01:35 -0400847 } else {
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700848 return ret;
849 }
850}
851
852static ssize_t applesmc_key_at_index_data_length_show(struct device *dev,
853 struct device_attribute *attr, char *sysfsbuf)
854{
855 char key[5];
856 char info[6];
857 int ret;
858
859 mutex_lock(&applesmc_lock);
860
861 ret = applesmc_get_key_at_index(key_at_index, key);
862
863 if (ret || !key[0]) {
864 mutex_unlock(&applesmc_lock);
865
866 return -EINVAL;
867 }
868
869 ret = applesmc_get_key_type(key, info);
870
871 mutex_unlock(&applesmc_lock);
872
873 if (!ret)
874 return snprintf(sysfsbuf, PAGE_SIZE, "%d\n", info[0]);
875 else
876 return ret;
877}
878
879static ssize_t applesmc_key_at_index_type_show(struct device *dev,
880 struct device_attribute *attr, char *sysfsbuf)
881{
882 char key[5];
883 char info[6];
884 int ret;
885
886 mutex_lock(&applesmc_lock);
887
888 ret = applesmc_get_key_at_index(key_at_index, key);
889
890 if (ret || !key[0]) {
891 mutex_unlock(&applesmc_lock);
892
893 return -EINVAL;
894 }
895
896 ret = applesmc_get_key_type(key, info);
897
898 mutex_unlock(&applesmc_lock);
899
900 if (!ret)
901 return snprintf(sysfsbuf, PAGE_SIZE, "%s\n", info+1);
902 else
903 return ret;
904}
905
906static ssize_t applesmc_key_at_index_name_show(struct device *dev,
907 struct device_attribute *attr, char *sysfsbuf)
908{
909 char key[5];
910 int ret;
911
912 mutex_lock(&applesmc_lock);
913
914 ret = applesmc_get_key_at_index(key_at_index, key);
915
916 mutex_unlock(&applesmc_lock);
917
918 if (!ret && key[0])
919 return snprintf(sysfsbuf, PAGE_SIZE, "%s\n", key);
920 else
921 return -EINVAL;
922}
923
924static ssize_t applesmc_key_at_index_show(struct device *dev,
925 struct device_attribute *attr, char *sysfsbuf)
926{
927 return snprintf(sysfsbuf, PAGE_SIZE, "%d\n", key_at_index);
928}
929
930static ssize_t applesmc_key_at_index_store(struct device *dev,
931 struct device_attribute *attr, const char *sysfsbuf, size_t count)
932{
933 mutex_lock(&applesmc_lock);
934
935 key_at_index = simple_strtoul(sysfsbuf, NULL, 10);
936
937 mutex_unlock(&applesmc_lock);
938
939 return count;
940}
941
942static struct led_classdev applesmc_backlight = {
Richard Purdie6c152be2007-10-31 15:00:07 +0100943 .name = "smc::kbd_backlight",
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700944 .default_trigger = "nand-disk",
945 .brightness_set = applesmc_brightness_set,
946};
947
Nicolas Boichatfa744192007-05-23 13:58:13 -0700948static DEVICE_ATTR(name, 0444, applesmc_name_show, NULL);
949
Nicolas Boichat6f2fad72007-05-08 00:24:52 -0700950static DEVICE_ATTR(position, 0444, applesmc_position_show, NULL);
951static DEVICE_ATTR(calibrate, 0644,
952 applesmc_calibrate_show, applesmc_calibrate_store);
953
954static struct attribute *accelerometer_attributes[] = {
955 &dev_attr_position.attr,
956 &dev_attr_calibrate.attr,
957 NULL
958};
959
960static const struct attribute_group accelerometer_attributes_group =
961 { .attrs = accelerometer_attributes };
962
963static DEVICE_ATTR(light, 0444, applesmc_light_show, NULL);
964
965static DEVICE_ATTR(key_count, 0444, applesmc_key_count_show, NULL);
966static DEVICE_ATTR(key_at_index, 0644,
967 applesmc_key_at_index_show, applesmc_key_at_index_store);
968static DEVICE_ATTR(key_at_index_name, 0444,
969 applesmc_key_at_index_name_show, NULL);
970static DEVICE_ATTR(key_at_index_type, 0444,
971 applesmc_key_at_index_type_show, NULL);
972static DEVICE_ATTR(key_at_index_data_length, 0444,
973 applesmc_key_at_index_data_length_show, NULL);
974static DEVICE_ATTR(key_at_index_data, 0444,
975 applesmc_key_at_index_read_show, NULL);
976
977static struct attribute *key_enumeration_attributes[] = {
978 &dev_attr_key_count.attr,
979 &dev_attr_key_at_index.attr,
980 &dev_attr_key_at_index_name.attr,
981 &dev_attr_key_at_index_type.attr,
982 &dev_attr_key_at_index_data_length.attr,
983 &dev_attr_key_at_index_data.attr,
984 NULL
985};
986
987static const struct attribute_group key_enumeration_group =
988 { .attrs = key_enumeration_attributes };
989
990/*
991 * Macro defining SENSOR_DEVICE_ATTR for a fan sysfs entries.
992 * - show actual speed
993 * - show/store minimum speed
994 * - show maximum speed
995 * - show safe speed
996 * - show/store target speed
997 * - show/store manual mode
998 */
999#define sysfs_fan_speeds_offset(offset) \
1000static SENSOR_DEVICE_ATTR_2(fan##offset##_input, S_IRUGO, \
1001 applesmc_show_fan_speed, NULL, 0, offset-1); \
1002\
1003static SENSOR_DEVICE_ATTR_2(fan##offset##_min, S_IRUGO | S_IWUSR, \
1004 applesmc_show_fan_speed, applesmc_store_fan_speed, 1, offset-1); \
1005\
1006static SENSOR_DEVICE_ATTR_2(fan##offset##_max, S_IRUGO, \
1007 applesmc_show_fan_speed, NULL, 2, offset-1); \
1008\
1009static SENSOR_DEVICE_ATTR_2(fan##offset##_safe, S_IRUGO, \
1010 applesmc_show_fan_speed, NULL, 3, offset-1); \
1011\
1012static SENSOR_DEVICE_ATTR_2(fan##offset##_output, S_IRUGO | S_IWUSR, \
1013 applesmc_show_fan_speed, applesmc_store_fan_speed, 4, offset-1); \
1014\
1015static SENSOR_DEVICE_ATTR(fan##offset##_manual, S_IRUGO | S_IWUSR, \
1016 applesmc_show_fan_manual, applesmc_store_fan_manual, offset-1); \
1017\
Jean Delvareda4e8ca2007-05-08 20:27:05 -07001018static SENSOR_DEVICE_ATTR(fan##offset##_label, S_IRUGO, \
Nicolas Boichat6f2fad72007-05-08 00:24:52 -07001019 applesmc_show_fan_position, NULL, offset-1); \
1020\
1021static struct attribute *fan##offset##_attributes[] = { \
1022 &sensor_dev_attr_fan##offset##_input.dev_attr.attr, \
1023 &sensor_dev_attr_fan##offset##_min.dev_attr.attr, \
1024 &sensor_dev_attr_fan##offset##_max.dev_attr.attr, \
1025 &sensor_dev_attr_fan##offset##_safe.dev_attr.attr, \
1026 &sensor_dev_attr_fan##offset##_output.dev_attr.attr, \
1027 &sensor_dev_attr_fan##offset##_manual.dev_attr.attr, \
Jean Delvareda4e8ca2007-05-08 20:27:05 -07001028 &sensor_dev_attr_fan##offset##_label.dev_attr.attr, \
Nicolas Boichat6f2fad72007-05-08 00:24:52 -07001029 NULL \
1030};
1031
1032/*
1033 * Create the needed functions for each fan using the macro defined above
René Rebe8de57702007-10-16 14:19:20 -07001034 * (4 fans are supported)
Nicolas Boichat6f2fad72007-05-08 00:24:52 -07001035 */
1036sysfs_fan_speeds_offset(1);
1037sysfs_fan_speeds_offset(2);
René Rebe8de57702007-10-16 14:19:20 -07001038sysfs_fan_speeds_offset(3);
1039sysfs_fan_speeds_offset(4);
Nicolas Boichat6f2fad72007-05-08 00:24:52 -07001040
1041static const struct attribute_group fan_attribute_groups[] = {
1042 { .attrs = fan1_attributes },
René Rebe8de57702007-10-16 14:19:20 -07001043 { .attrs = fan2_attributes },
1044 { .attrs = fan3_attributes },
1045 { .attrs = fan4_attributes },
Nicolas Boichat6f2fad72007-05-08 00:24:52 -07001046};
1047
1048/*
1049 * Temperature sensors sysfs entries.
1050 */
1051static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO,
1052 applesmc_show_temperature, NULL, 0);
1053static SENSOR_DEVICE_ATTR(temp2_input, S_IRUGO,
1054 applesmc_show_temperature, NULL, 1);
1055static SENSOR_DEVICE_ATTR(temp3_input, S_IRUGO,
1056 applesmc_show_temperature, NULL, 2);
1057static SENSOR_DEVICE_ATTR(temp4_input, S_IRUGO,
1058 applesmc_show_temperature, NULL, 3);
1059static SENSOR_DEVICE_ATTR(temp5_input, S_IRUGO,
1060 applesmc_show_temperature, NULL, 4);
1061static SENSOR_DEVICE_ATTR(temp6_input, S_IRUGO,
1062 applesmc_show_temperature, NULL, 5);
1063static SENSOR_DEVICE_ATTR(temp7_input, S_IRUGO,
1064 applesmc_show_temperature, NULL, 6);
1065static SENSOR_DEVICE_ATTR(temp8_input, S_IRUGO,
1066 applesmc_show_temperature, NULL, 7);
1067static SENSOR_DEVICE_ATTR(temp9_input, S_IRUGO,
1068 applesmc_show_temperature, NULL, 8);
1069static SENSOR_DEVICE_ATTR(temp10_input, S_IRUGO,
1070 applesmc_show_temperature, NULL, 9);
1071static SENSOR_DEVICE_ATTR(temp11_input, S_IRUGO,
1072 applesmc_show_temperature, NULL, 10);
1073static SENSOR_DEVICE_ATTR(temp12_input, S_IRUGO,
1074 applesmc_show_temperature, NULL, 11);
René Rebe8de57702007-10-16 14:19:20 -07001075static SENSOR_DEVICE_ATTR(temp13_input, S_IRUGO,
1076 applesmc_show_temperature, NULL, 12);
1077static SENSOR_DEVICE_ATTR(temp14_input, S_IRUGO,
1078 applesmc_show_temperature, NULL, 13);
1079static SENSOR_DEVICE_ATTR(temp15_input, S_IRUGO,
1080 applesmc_show_temperature, NULL, 14);
1081static SENSOR_DEVICE_ATTR(temp16_input, S_IRUGO,
1082 applesmc_show_temperature, NULL, 15);
1083static SENSOR_DEVICE_ATTR(temp17_input, S_IRUGO,
1084 applesmc_show_temperature, NULL, 16);
1085static SENSOR_DEVICE_ATTR(temp18_input, S_IRUGO,
1086 applesmc_show_temperature, NULL, 17);
1087static SENSOR_DEVICE_ATTR(temp19_input, S_IRUGO,
1088 applesmc_show_temperature, NULL, 18);
1089static SENSOR_DEVICE_ATTR(temp20_input, S_IRUGO,
1090 applesmc_show_temperature, NULL, 19);
1091static SENSOR_DEVICE_ATTR(temp21_input, S_IRUGO,
1092 applesmc_show_temperature, NULL, 20);
1093static SENSOR_DEVICE_ATTR(temp22_input, S_IRUGO,
1094 applesmc_show_temperature, NULL, 21);
1095static SENSOR_DEVICE_ATTR(temp23_input, S_IRUGO,
1096 applesmc_show_temperature, NULL, 22);
1097static SENSOR_DEVICE_ATTR(temp24_input, S_IRUGO,
1098 applesmc_show_temperature, NULL, 23);
1099static SENSOR_DEVICE_ATTR(temp25_input, S_IRUGO,
1100 applesmc_show_temperature, NULL, 24);
1101static SENSOR_DEVICE_ATTR(temp26_input, S_IRUGO,
1102 applesmc_show_temperature, NULL, 25);
1103static SENSOR_DEVICE_ATTR(temp27_input, S_IRUGO,
1104 applesmc_show_temperature, NULL, 26);
1105static SENSOR_DEVICE_ATTR(temp28_input, S_IRUGO,
1106 applesmc_show_temperature, NULL, 27);
1107static SENSOR_DEVICE_ATTR(temp29_input, S_IRUGO,
1108 applesmc_show_temperature, NULL, 28);
1109static SENSOR_DEVICE_ATTR(temp30_input, S_IRUGO,
1110 applesmc_show_temperature, NULL, 29);
1111static SENSOR_DEVICE_ATTR(temp31_input, S_IRUGO,
1112 applesmc_show_temperature, NULL, 30);
1113static SENSOR_DEVICE_ATTR(temp32_input, S_IRUGO,
1114 applesmc_show_temperature, NULL, 31);
1115static SENSOR_DEVICE_ATTR(temp33_input, S_IRUGO,
1116 applesmc_show_temperature, NULL, 32);
1117static SENSOR_DEVICE_ATTR(temp34_input, S_IRUGO,
1118 applesmc_show_temperature, NULL, 33);
1119static SENSOR_DEVICE_ATTR(temp35_input, S_IRUGO,
1120 applesmc_show_temperature, NULL, 34);
Nicolas Boichat6f2fad72007-05-08 00:24:52 -07001121
1122static struct attribute *temperature_attributes[] = {
1123 &sensor_dev_attr_temp1_input.dev_attr.attr,
1124 &sensor_dev_attr_temp2_input.dev_attr.attr,
1125 &sensor_dev_attr_temp3_input.dev_attr.attr,
1126 &sensor_dev_attr_temp4_input.dev_attr.attr,
1127 &sensor_dev_attr_temp5_input.dev_attr.attr,
1128 &sensor_dev_attr_temp6_input.dev_attr.attr,
1129 &sensor_dev_attr_temp7_input.dev_attr.attr,
1130 &sensor_dev_attr_temp8_input.dev_attr.attr,
1131 &sensor_dev_attr_temp9_input.dev_attr.attr,
1132 &sensor_dev_attr_temp10_input.dev_attr.attr,
1133 &sensor_dev_attr_temp11_input.dev_attr.attr,
1134 &sensor_dev_attr_temp12_input.dev_attr.attr,
René Rebe8de57702007-10-16 14:19:20 -07001135 &sensor_dev_attr_temp13_input.dev_attr.attr,
1136 &sensor_dev_attr_temp14_input.dev_attr.attr,
1137 &sensor_dev_attr_temp15_input.dev_attr.attr,
1138 &sensor_dev_attr_temp16_input.dev_attr.attr,
1139 &sensor_dev_attr_temp17_input.dev_attr.attr,
1140 &sensor_dev_attr_temp18_input.dev_attr.attr,
1141 &sensor_dev_attr_temp19_input.dev_attr.attr,
1142 &sensor_dev_attr_temp20_input.dev_attr.attr,
1143 &sensor_dev_attr_temp21_input.dev_attr.attr,
1144 &sensor_dev_attr_temp22_input.dev_attr.attr,
1145 &sensor_dev_attr_temp23_input.dev_attr.attr,
1146 &sensor_dev_attr_temp24_input.dev_attr.attr,
1147 &sensor_dev_attr_temp25_input.dev_attr.attr,
1148 &sensor_dev_attr_temp26_input.dev_attr.attr,
1149 &sensor_dev_attr_temp27_input.dev_attr.attr,
1150 &sensor_dev_attr_temp28_input.dev_attr.attr,
1151 &sensor_dev_attr_temp29_input.dev_attr.attr,
1152 &sensor_dev_attr_temp30_input.dev_attr.attr,
1153 &sensor_dev_attr_temp31_input.dev_attr.attr,
1154 &sensor_dev_attr_temp32_input.dev_attr.attr,
1155 &sensor_dev_attr_temp33_input.dev_attr.attr,
1156 &sensor_dev_attr_temp34_input.dev_attr.attr,
1157 &sensor_dev_attr_temp35_input.dev_attr.attr,
Nicolas Boichat6f2fad72007-05-08 00:24:52 -07001158 NULL
1159};
1160
1161static const struct attribute_group temperature_attributes_group =
1162 { .attrs = temperature_attributes };
1163
1164/* Module stuff */
1165
1166/*
1167 * applesmc_dmi_match - found a match. return one, short-circuiting the hunt.
1168 */
Jeff Garzik18552562007-10-03 15:15:40 -04001169static int applesmc_dmi_match(const struct dmi_system_id *id)
Nicolas Boichat6f2fad72007-05-08 00:24:52 -07001170{
1171 int i = 0;
1172 struct dmi_match_data* dmi_data = id->driver_data;
1173 printk(KERN_INFO "applesmc: %s detected:\n", id->ident);
1174 applesmc_accelerometer = dmi_data->accelerometer;
1175 printk(KERN_INFO "applesmc: - Model %s accelerometer\n",
1176 applesmc_accelerometer ? "with" : "without");
1177 applesmc_light = dmi_data->light;
1178 printk(KERN_INFO "applesmc: - Model %s light sensors and backlight\n",
1179 applesmc_light ? "with" : "without");
1180
1181 applesmc_temperature_set = dmi_data->temperature_set;
1182 while (temperature_sensors_sets[applesmc_temperature_set][i] != NULL)
1183 i++;
1184 printk(KERN_INFO "applesmc: - Model with %d temperature sensors\n", i);
1185 return 1;
1186}
1187
1188/* Create accelerometer ressources */
1189static int applesmc_create_accelerometer(void)
1190{
Dmitry Torokhovd5cf2b92007-09-26 00:01:35 -04001191 struct input_dev *idev;
Nicolas Boichat6f2fad72007-05-08 00:24:52 -07001192 int ret;
1193
1194 ret = sysfs_create_group(&pdev->dev.kobj,
1195 &accelerometer_attributes_group);
1196 if (ret)
1197 goto out;
1198
Dmitry Torokhovd5cf2b92007-09-26 00:01:35 -04001199 applesmc_idev = input_allocate_polled_device();
Nicolas Boichat6f2fad72007-05-08 00:24:52 -07001200 if (!applesmc_idev) {
1201 ret = -ENOMEM;
1202 goto out_sysfs;
1203 }
1204
Dmitry Torokhovd5cf2b92007-09-26 00:01:35 -04001205 applesmc_idev->poll = applesmc_idev_poll;
1206 applesmc_idev->poll_interval = APPLESMC_POLL_INTERVAL;
1207
Nicolas Boichat6f2fad72007-05-08 00:24:52 -07001208 /* initial calibrate for the input device */
1209 applesmc_calibrate();
1210
Dmitry Torokhovd5cf2b92007-09-26 00:01:35 -04001211 /* initialize the input device */
1212 idev = applesmc_idev->input;
1213 idev->name = "applesmc";
1214 idev->id.bustype = BUS_HOST;
1215 idev->dev.parent = &pdev->dev;
Jiri Slaby7b19ada2007-10-18 23:40:32 -07001216 idev->evbit[0] = BIT_MASK(EV_ABS);
Dmitry Torokhovd5cf2b92007-09-26 00:01:35 -04001217 input_set_abs_params(idev, ABS_X,
Nicolas Boichat6f2fad72007-05-08 00:24:52 -07001218 -256, 256, APPLESMC_INPUT_FUZZ, APPLESMC_INPUT_FLAT);
Dmitry Torokhovd5cf2b92007-09-26 00:01:35 -04001219 input_set_abs_params(idev, ABS_Y,
Nicolas Boichat6f2fad72007-05-08 00:24:52 -07001220 -256, 256, APPLESMC_INPUT_FUZZ, APPLESMC_INPUT_FLAT);
1221
Dmitry Torokhovd5cf2b92007-09-26 00:01:35 -04001222 ret = input_register_polled_device(applesmc_idev);
Nicolas Boichat6f2fad72007-05-08 00:24:52 -07001223 if (ret)
1224 goto out_idev;
1225
Nicolas Boichat6f2fad72007-05-08 00:24:52 -07001226 return 0;
1227
1228out_idev:
Dmitry Torokhovd5cf2b92007-09-26 00:01:35 -04001229 input_free_polled_device(applesmc_idev);
Nicolas Boichat6f2fad72007-05-08 00:24:52 -07001230
1231out_sysfs:
1232 sysfs_remove_group(&pdev->dev.kobj, &accelerometer_attributes_group);
1233
1234out:
1235 printk(KERN_WARNING "applesmc: driver init failed (ret=%d)!\n", ret);
1236 return ret;
1237}
1238
1239/* Release all ressources used by the accelerometer */
1240static void applesmc_release_accelerometer(void)
1241{
Dmitry Torokhovd5cf2b92007-09-26 00:01:35 -04001242 input_unregister_polled_device(applesmc_idev);
1243 input_free_polled_device(applesmc_idev);
Nicolas Boichat6f2fad72007-05-08 00:24:52 -07001244 sysfs_remove_group(&pdev->dev.kobj, &accelerometer_attributes_group);
1245}
1246
1247static __initdata struct dmi_match_data applesmc_dmi_data[] = {
1248/* MacBook Pro: accelerometer, backlight and temperature set 0 */
1249 { .accelerometer = 1, .light = 1, .temperature_set = 0 },
Riki Oktariantocd19ba12008-02-04 23:41:58 -08001250/* MacBook2: accelerometer and temperature set 1 */
Martin Szulecki1bed24b2007-07-09 11:41:36 -07001251 { .accelerometer = 1, .light = 0, .temperature_set = 1 },
Riki Oktariantocd19ba12008-02-04 23:41:58 -08001252/* MacBook: accelerometer and temperature set 2 */
1253 { .accelerometer = 1, .light = 0, .temperature_set = 2 },
1254/* MacMini: temperature set 3 */
René Rebe8de57702007-10-16 14:19:20 -07001255 { .accelerometer = 0, .light = 0, .temperature_set = 3 },
Riki Oktariantocd19ba12008-02-04 23:41:58 -08001256/* MacPro: temperature set 4 */
1257 { .accelerometer = 0, .light = 0, .temperature_set = 4 },
Roberto De Ioris9f86f282008-08-15 00:40:30 -07001258/* iMac: temperature set 5 */
1259 { .accelerometer = 0, .light = 0, .temperature_set = 5 },
Guilherme M. Schroederf91a79f2008-08-15 00:40:32 -07001260/* MacBook3: accelerometer and temperature set 6 */
1261 { .accelerometer = 1, .light = 0, .temperature_set = 6 },
Nicolas Boichat6f2fad72007-05-08 00:24:52 -07001262};
1263
1264/* Note that DMI_MATCH(...,"MacBook") will match "MacBookPro1,1".
1265 * So we need to put "Apple MacBook Pro" before "Apple MacBook". */
1266static __initdata struct dmi_system_id applesmc_whitelist[] = {
1267 { applesmc_dmi_match, "Apple MacBook Pro", {
1268 DMI_MATCH(DMI_BOARD_VENDOR,"Apple"),
1269 DMI_MATCH(DMI_PRODUCT_NAME,"MacBookPro") },
1270 (void*)&applesmc_dmi_data[0]},
Guilherme M. Schroederf91a79f2008-08-15 00:40:32 -07001271 { applesmc_dmi_match, "Apple MacBook (v2)", {
Nicolas Boichat6f2fad72007-05-08 00:24:52 -07001272 DMI_MATCH(DMI_BOARD_VENDOR,"Apple"),
Riki Oktariantocd19ba12008-02-04 23:41:58 -08001273 DMI_MATCH(DMI_PRODUCT_NAME,"MacBook2") },
Nicolas Boichat6f2fad72007-05-08 00:24:52 -07001274 (void*)&applesmc_dmi_data[1]},
Guilherme M. Schroederf91a79f2008-08-15 00:40:32 -07001275 { applesmc_dmi_match, "Apple MacBook (v3)", {
1276 DMI_MATCH(DMI_BOARD_VENDOR,"Apple"),
1277 DMI_MATCH(DMI_PRODUCT_NAME,"MacBook3") },
1278 (void*)&applesmc_dmi_data[6]},
Riki Oktariantocd19ba12008-02-04 23:41:58 -08001279 { applesmc_dmi_match, "Apple MacBook", {
1280 DMI_MATCH(DMI_BOARD_VENDOR,"Apple"),
1281 DMI_MATCH(DMI_PRODUCT_NAME,"MacBook") },
1282 (void*)&applesmc_dmi_data[2]},
Nicolas Boichat6f2fad72007-05-08 00:24:52 -07001283 { applesmc_dmi_match, "Apple Macmini", {
1284 DMI_MATCH(DMI_BOARD_VENDOR,"Apple"),
1285 DMI_MATCH(DMI_PRODUCT_NAME,"Macmini") },
Riki Oktariantocd19ba12008-02-04 23:41:58 -08001286 (void*)&applesmc_dmi_data[3]},
René Rebe8de57702007-10-16 14:19:20 -07001287 { applesmc_dmi_match, "Apple MacPro2", {
1288 DMI_MATCH(DMI_BOARD_VENDOR,"Apple"),
1289 DMI_MATCH(DMI_PRODUCT_NAME,"MacPro2") },
Riki Oktariantocd19ba12008-02-04 23:41:58 -08001290 (void*)&applesmc_dmi_data[4]},
Roberto De Ioris9f86f282008-08-15 00:40:30 -07001291 { applesmc_dmi_match, "Apple iMac", {
1292 DMI_MATCH(DMI_BOARD_VENDOR,"Apple"),
1293 DMI_MATCH(DMI_PRODUCT_NAME,"iMac") },
1294 (void*)&applesmc_dmi_data[5]},
Nicolas Boichat6f2fad72007-05-08 00:24:52 -07001295 { .ident = NULL }
1296};
1297
1298static int __init applesmc_init(void)
1299{
1300 int ret;
1301 int count;
1302 int i;
1303
Nicolas Boichat6f2fad72007-05-08 00:24:52 -07001304 if (!dmi_check_system(applesmc_whitelist)) {
1305 printk(KERN_WARNING "applesmc: supported laptop not found!\n");
1306 ret = -ENODEV;
1307 goto out;
1308 }
1309
1310 if (!request_region(APPLESMC_DATA_PORT, APPLESMC_NR_PORTS,
1311 "applesmc")) {
1312 ret = -ENXIO;
1313 goto out;
1314 }
1315
1316 ret = platform_driver_register(&applesmc_driver);
1317 if (ret)
1318 goto out_region;
1319
Jean Delvareddfbf2a2007-05-08 20:27:04 -07001320 pdev = platform_device_register_simple("applesmc", APPLESMC_DATA_PORT,
1321 NULL, 0);
Nicolas Boichat6f2fad72007-05-08 00:24:52 -07001322 if (IS_ERR(pdev)) {
1323 ret = PTR_ERR(pdev);
1324 goto out_driver;
1325 }
1326
Nicolas Boichatfa744192007-05-23 13:58:13 -07001327 ret = sysfs_create_file(&pdev->dev.kobj, &dev_attr_name.attr);
Nicolas Boichat6996abf2007-05-27 22:17:43 +02001328 if (ret)
1329 goto out_device;
Nicolas Boichatfa744192007-05-23 13:58:13 -07001330
Nicolas Boichat6f2fad72007-05-08 00:24:52 -07001331 /* Create key enumeration sysfs files */
1332 ret = sysfs_create_group(&pdev->dev.kobj, &key_enumeration_group);
1333 if (ret)
Nicolas Boichat6996abf2007-05-27 22:17:43 +02001334 goto out_name;
Nicolas Boichat6f2fad72007-05-08 00:24:52 -07001335
1336 /* create fan files */
1337 count = applesmc_get_fan_count();
1338 if (count < 0) {
1339 printk(KERN_ERR "applesmc: Cannot get the number of fans.\n");
1340 } else {
1341 printk(KERN_INFO "applesmc: %d fans found.\n", count);
1342
1343 switch (count) {
1344 default:
René Rebe8de57702007-10-16 14:19:20 -07001345 printk(KERN_WARNING "applesmc: More than 4 fans found,"
1346 " but at most 4 fans are supported"
Nicolas Boichat6f2fad72007-05-08 00:24:52 -07001347 " by the driver.\n");
René Rebe8de57702007-10-16 14:19:20 -07001348 case 4:
1349 ret = sysfs_create_group(&pdev->dev.kobj,
1350 &fan_attribute_groups[3]);
1351 if (ret)
1352 goto out_key_enumeration;
1353 case 3:
1354 ret = sysfs_create_group(&pdev->dev.kobj,
1355 &fan_attribute_groups[2]);
1356 if (ret)
1357 goto out_key_enumeration;
Nicolas Boichat6f2fad72007-05-08 00:24:52 -07001358 case 2:
1359 ret = sysfs_create_group(&pdev->dev.kobj,
1360 &fan_attribute_groups[1]);
1361 if (ret)
1362 goto out_key_enumeration;
1363 case 1:
1364 ret = sysfs_create_group(&pdev->dev.kobj,
1365 &fan_attribute_groups[0]);
1366 if (ret)
1367 goto out_fan_1;
1368 case 0:
1369 ;
1370 }
1371 }
1372
1373 for (i = 0;
1374 temperature_sensors_sets[applesmc_temperature_set][i] != NULL;
1375 i++) {
1376 if (temperature_attributes[i] == NULL) {
1377 printk(KERN_ERR "applesmc: More temperature sensors "
1378 "in temperature_sensors_sets (at least %i)"
1379 "than available sysfs files in "
1380 "temperature_attributes (%i), please report "
1381 "this bug.\n", i, i-1);
1382 goto out_temperature;
1383 }
1384 ret = sysfs_create_file(&pdev->dev.kobj,
1385 temperature_attributes[i]);
1386 if (ret)
1387 goto out_temperature;
1388 }
1389
1390 if (applesmc_accelerometer) {
1391 ret = applesmc_create_accelerometer();
1392 if (ret)
1393 goto out_temperature;
1394 }
1395
1396 if (applesmc_light) {
1397 /* Add light sensor file */
1398 ret = sysfs_create_file(&pdev->dev.kobj, &dev_attr_light.attr);
1399 if (ret)
1400 goto out_accelerometer;
1401
1402 /* Create the workqueue */
1403 applesmc_led_wq = create_singlethread_workqueue("applesmc-led");
1404 if (!applesmc_led_wq) {
1405 ret = -ENOMEM;
1406 goto out_light_sysfs;
1407 }
1408
1409 /* register as a led device */
1410 ret = led_classdev_register(&pdev->dev, &applesmc_backlight);
1411 if (ret < 0)
1412 goto out_light_wq;
1413 }
1414
Tony Jones1beeffe2007-08-20 13:46:20 -07001415 hwmon_dev = hwmon_device_register(&pdev->dev);
1416 if (IS_ERR(hwmon_dev)) {
1417 ret = PTR_ERR(hwmon_dev);
Nicolas Boichat6f2fad72007-05-08 00:24:52 -07001418 goto out_light_ledclass;
1419 }
1420
1421 printk(KERN_INFO "applesmc: driver successfully loaded.\n");
1422
1423 return 0;
1424
1425out_light_ledclass:
1426 if (applesmc_light)
1427 led_classdev_unregister(&applesmc_backlight);
1428out_light_wq:
1429 if (applesmc_light)
1430 destroy_workqueue(applesmc_led_wq);
1431out_light_sysfs:
1432 if (applesmc_light)
1433 sysfs_remove_file(&pdev->dev.kobj, &dev_attr_light.attr);
1434out_accelerometer:
1435 if (applesmc_accelerometer)
1436 applesmc_release_accelerometer();
1437out_temperature:
1438 sysfs_remove_group(&pdev->dev.kobj, &temperature_attributes_group);
1439 sysfs_remove_group(&pdev->dev.kobj, &fan_attribute_groups[0]);
1440out_fan_1:
1441 sysfs_remove_group(&pdev->dev.kobj, &fan_attribute_groups[1]);
1442out_key_enumeration:
1443 sysfs_remove_group(&pdev->dev.kobj, &key_enumeration_group);
Nicolas Boichat6996abf2007-05-27 22:17:43 +02001444out_name:
1445 sysfs_remove_file(&pdev->dev.kobj, &dev_attr_name.attr);
Nicolas Boichat6f2fad72007-05-08 00:24:52 -07001446out_device:
1447 platform_device_unregister(pdev);
1448out_driver:
1449 platform_driver_unregister(&applesmc_driver);
1450out_region:
1451 release_region(APPLESMC_DATA_PORT, APPLESMC_NR_PORTS);
1452out:
1453 printk(KERN_WARNING "applesmc: driver init failed (ret=%d)!\n", ret);
1454 return ret;
1455}
1456
1457static void __exit applesmc_exit(void)
1458{
Tony Jones1beeffe2007-08-20 13:46:20 -07001459 hwmon_device_unregister(hwmon_dev);
Nicolas Boichat6f2fad72007-05-08 00:24:52 -07001460 if (applesmc_light) {
1461 led_classdev_unregister(&applesmc_backlight);
1462 destroy_workqueue(applesmc_led_wq);
1463 sysfs_remove_file(&pdev->dev.kobj, &dev_attr_light.attr);
1464 }
1465 if (applesmc_accelerometer)
1466 applesmc_release_accelerometer();
1467 sysfs_remove_group(&pdev->dev.kobj, &temperature_attributes_group);
1468 sysfs_remove_group(&pdev->dev.kobj, &fan_attribute_groups[0]);
1469 sysfs_remove_group(&pdev->dev.kobj, &fan_attribute_groups[1]);
1470 sysfs_remove_group(&pdev->dev.kobj, &key_enumeration_group);
Nicolas Boichat6996abf2007-05-27 22:17:43 +02001471 sysfs_remove_file(&pdev->dev.kobj, &dev_attr_name.attr);
Nicolas Boichat6f2fad72007-05-08 00:24:52 -07001472 platform_device_unregister(pdev);
1473 platform_driver_unregister(&applesmc_driver);
1474 release_region(APPLESMC_DATA_PORT, APPLESMC_NR_PORTS);
1475
1476 printk(KERN_INFO "applesmc: driver unloaded.\n");
1477}
1478
1479module_init(applesmc_init);
1480module_exit(applesmc_exit);
1481
1482MODULE_AUTHOR("Nicolas Boichat");
1483MODULE_DESCRIPTION("Apple SMC");
1484MODULE_LICENSE("GPL v2");