applesmc.c 53.4 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
/*
 * drivers/hwmon/applesmc.c - driver for Apple's SMC (accelerometer, temperature
 * sensors, fan control, keyboard backlight control) used in Intel-based Apple
 * computers.
 *
 * Copyright (C) 2007 Nicolas Boichat <nicolas@boichat.ch>
 *
 * Based on hdaps.c driver:
 * Copyright (C) 2005 Robert Love <rml@novell.com>
 * Copyright (C) 2005 Jesper Juhl <jesper.juhl@gmail.com>
 *
 * Fan control based on smcFanControl:
 * Copyright (C) 2006 Hendrik Holtmann <holtmann@mac.com>
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License v2 as published by the
 * Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
 * more details.
 *
 * You should have received a copy of the GNU General Public License along with
 * this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
 */

29 30
#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt

31 32
#include <linux/delay.h>
#include <linux/platform_device.h>
33
#include <linux/input-polldev.h>
34 35 36 37 38 39
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/timer.h>
#include <linux/dmi.h>
#include <linux/mutex.h>
#include <linux/hwmon-sysfs.h>
40
#include <linux/io.h>
41 42 43 44 45 46 47 48 49 50 51 52 53
#include <linux/leds.h>
#include <linux/hwmon.h>
#include <linux/workqueue.h>

/* data port used by Apple SMC */
#define APPLESMC_DATA_PORT	0x300
/* command/status port used by Apple SMC */
#define APPLESMC_CMD_PORT	0x304

#define APPLESMC_NR_PORTS	32 /* 0x300-0x31f */

#define APPLESMC_MAX_DATA_LENGTH 32

54 55 56
#define APPLESMC_MIN_WAIT	0x0040
#define APPLESMC_MAX_WAIT	0x8000

57 58 59 60 61 62 63 64
#define APPLESMC_STATUS_MASK	0x0f
#define APPLESMC_READ_CMD	0x10
#define APPLESMC_WRITE_CMD	0x11
#define APPLESMC_GET_KEY_BY_INDEX_CMD	0x12
#define APPLESMC_GET_KEY_TYPE_CMD	0x13

#define KEY_COUNT_KEY		"#KEY" /* r-o ui32 */

65 66
#define LIGHT_SENSOR_LEFT_KEY	"ALV0" /* r-o {alv (6-10 bytes) */
#define LIGHT_SENSOR_RIGHT_KEY	"ALV1" /* r-o {alv (6-10 bytes) */
67
#define BACKLIGHT_KEY		"LKSB" /* w-o {lkb (2 bytes) */
68

69
#define CLAMSHELL_KEY		"MSLD" /* r-o ui8 (unused) */
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87

#define MOTION_SENSOR_X_KEY	"MO_X" /* r-o sp78 (2 bytes) */
#define MOTION_SENSOR_Y_KEY	"MO_Y" /* r-o sp78 (2 bytes) */
#define MOTION_SENSOR_Z_KEY	"MO_Z" /* r-o sp78 (2 bytes) */
#define MOTION_SENSOR_KEY	"MOCN" /* r/w ui16 */

#define FANS_COUNT		"FNum" /* r-o ui8 */
#define FANS_MANUAL		"FS! " /* r-w ui16 */
#define FAN_ACTUAL_SPEED	"F0Ac" /* r-o fpe2 (2 bytes) */
#define FAN_MIN_SPEED		"F0Mn" /* r-o fpe2 (2 bytes) */
#define FAN_MAX_SPEED		"F0Mx" /* r-o fpe2 (2 bytes) */
#define FAN_SAFE_SPEED		"F0Sf" /* r-o fpe2 (2 bytes) */
#define FAN_TARGET_SPEED	"F0Tg" /* r-w fpe2 (2 bytes) */
#define FAN_POSITION		"F0ID" /* r-o char[16] */

/*
 * Temperature sensors keys (sp78 - 2 bytes).
 */
88
static const char *temperature_sensors_sets[][41] = {
89
/* Set 0: Macbook Pro */
90 91
	{ "TA0P", "TB0T", "TC0D", "TC0P", "TG0H", "TG0P", "TG0T", "Th0H",
	  "Th1H", "Tm0P", "Ts0P", "Ts1P", NULL },
92 93 94 95
/* Set 1: Macbook2 set */
	{ "TB0T", "TC0D", "TC0P", "TM0P", "TN0P", "TN1P", "TTF0", "Th0H",
	  "Th0S", "Th1H", NULL },
/* Set 2: Macbook set */
96 97
	{ "TB0T", "TC0D", "TC0P", "TM0P", "TN0P", "TN1P", "Th0H", "Th0S",
	  "Th1H", "Ts0P", NULL },
98
/* Set 3: Macmini set */
99
	{ "TC0D", "TC0P", NULL },
100
/* Set 4: Mac Pro (2 x Quad-Core) */
101 102 103 104 105
	{ "TA0P", "TCAG", "TCAH", "TCBG", "TCBH", "TC0C", "TC0D", "TC0P",
	  "TC1C", "TC1D", "TC2C", "TC2D", "TC3C", "TC3D", "THTG", "TH0P",
	  "TH1P", "TH2P", "TH3P", "TMAP", "TMAS", "TMBS", "TM0P", "TM0S",
	  "TM1P", "TM1S", "TM2P", "TM2S", "TM3S", "TM8P", "TM8S", "TM9P",
	  "TM9S", "TN0H", "TS0C", NULL },
106 107 108
/* Set 5: iMac */
	{ "TC0D", "TA0P", "TG0P", "TG0D", "TG0H", "TH0P", "Tm0P", "TO0P",
	  "Tp0C", NULL },
109 110 111
/* Set 6: Macbook3 set */
	{ "TB0T", "TC0D", "TC0P", "TM0P", "TN0P", "TTF0", "TW0P", "Th0H",
	  "Th0S", "Th1H", NULL },
112 113 114
/* Set 7: Macbook Air */
	{ "TB0T", "TB1S", "TB1T", "TB2S", "TB2T", "TC0D", "TC0P", "TCFP",
	  "TTF0", "TW0P", "Th0H", "Tp0P", "TpFP", "Ts0P", "Ts0S", NULL },
115 116 117
/* Set 8: Macbook Pro 4,1 (Penryn) */
	{ "TB0T", "TC0D", "TC0P", "TG0D", "TG0H", "TTF0", "TW0P", "Th0H",
	  "Th1H", "Th2H", "Tm0P", "Ts0P", NULL },
118 119 120
/* Set 9: Macbook Pro 3,1 (Santa Rosa) */
	{ "TALP", "TB0T", "TC0D", "TC0P", "TG0D", "TG0H", "TTF0", "TW0P",
	  "Th0H", "Th1H", "Th2H", "Tm0P", "Ts0P", NULL },
121 122
/* Set 10: iMac 5,1 */
	{ "TA0P", "TC0D", "TC0P", "TG0D", "TH0P", "TO0P", "Tm0P", NULL },
123 124 125
/* Set 11: Macbook 5,1 */
	{ "TB0T", "TB1T", "TB2T", "TB3T", "TC0D", "TC0P", "TN0D", "TN0P",
	  "TTF0", "Th0H", "Th1H", "ThFH", "Ts0P", "Ts0S", NULL },
126 127 128 129
/* Set 12: Macbook Pro 5,1 */
	{ "TB0T", "TB1T", "TB2T", "TB3T", "TC0D", "TC0F", "TC0P", "TG0D",
	  "TG0F", "TG0H", "TG0P", "TG0T", "TG1H", "TN0D", "TN0P", "TTF0",
	  "Th2H", "Tm0P", "Ts0P", "Ts0S", NULL },
130 131 132
/* Set 13: iMac 8,1 */
	{ "TA0P", "TC0D", "TC0H", "TC0P", "TG0D", "TG0H", "TG0P", "TH0P",
	  "TL0P", "TO0P", "TW0P", "Tm0P", "Tp0P", NULL },
133 134 135
/* Set 14: iMac 6,1 */
	{ "TA0P", "TC0D", "TC0H", "TC0P", "TG0D", "TG0H", "TG0P", "TH0P",
	  "TO0P", "Tp0P", NULL },
136 137 138 139
/* Set 15: MacBook Air 2,1 */
	{ "TB0T", "TB1S", "TB1T", "TB2S", "TB2T", "TC0D", "TN0D", "TTF0",
	  "TV0P", "TVFP", "TW0P", "Th0P", "Tp0P", "Tp1P", "TpFP", "Ts0P",
	  "Ts0S", NULL },
140 141 142 143 144 145 146
/* Set 16: Mac Pro 3,1 (2 x Quad-Core) */
	{ "TA0P", "TCAG", "TCAH", "TCBG", "TCBH", "TC0C", "TC0D", "TC0P",
	  "TC1C", "TC1D", "TC2C", "TC2D", "TC3C", "TC3D", "TH0P", "TH1P",
	  "TH2P", "TH3P", "TMAP", "TMAS", "TMBS", "TM0P", "TM0S", "TM1P",
	  "TM1S", "TM2P", "TM2S", "TM3S", "TM8P", "TM8S", "TM9P", "TM9S",
	  "TN0C", "TN0D", "TN0H", "TS0C", "Tp0C", "Tp1C", "Tv0S", "Tv1S",
	  NULL },
147 148 149 150 151 152
/* Set 17: iMac 9,1 */
	{ "TA0P", "TC0D", "TC0H", "TC0P", "TG0D", "TG0H", "TH0P", "TL0P",
	  "TN0D", "TN0H", "TN0P", "TO0P", "Tm0P", "Tp0P", NULL },
/* Set 18: MacBook Pro 2,2 */
	{ "TB0T", "TC0D", "TC0P", "TG0H", "TG0P", "TG0T", "TM0P", "TTF0",
	  "Th0H", "Th1H", "Tm0P", "Ts0P", NULL },
153 154 155 156 157 158 159
/* Set 19: Macbook Pro 5,3 */
	{ "TB0T", "TB1T", "TB2T", "TB3T", "TC0D", "TC0F", "TC0P", "TG0D",
	  "TG0F", "TG0H", "TG0P", "TG0T", "TN0D", "TN0P", "TTF0", "Th2H",
	  "Tm0P", "Ts0P", "Ts0S", NULL },
/* Set 20: MacBook Pro 5,4 */
	{ "TB0T", "TB1T", "TB2T", "TB3T", "TC0D", "TC0F", "TC0P", "TN0D",
	  "TN0P", "TTF0", "Th2H", "Ts0P", "Ts0S", NULL },
160 161 162 163
/* Set 21: MacBook Pro 6,2 */
	{ "TB0T", "TB1T", "TB2T", "TC0C", "TC0D", "TC0P", "TC1C", "TG0D",
	  "TG0P", "TG0T", "TMCD", "TP0P", "TPCD", "Th1H", "Th2H", "Tm0P",
	  "Ts0P", "Ts0S", NULL },
164 165 166
/* Set 22: MacBook Pro 7,1 */
	{ "TB0T", "TB1T", "TB2T", "TC0D", "TC0P", "TN0D", "TN0P", "TN0S",
	  "TN1D", "TN1F", "TN1G", "TN1S", "Th1H", "Ts0P", "Ts0S", NULL },
167 168 169 170
/* Set 23: MacBook Air 3,1 */
	{ "TB0T", "TB1T", "TB2T", "TC0D", "TC0E", "TC0P", "TC1E", "TCZ3",
	  "TCZ4", "TCZ5", "TG0E", "TG1E", "TG2E", "TGZ3", "TGZ4", "TGZ5",
	  "TH0F", "TH0O", "TM0P" },
171 172 173 174 175 176 177 178 179 180 181 182 183 184
};

/* List of keys used to read/write fan speeds */
static const char* fan_speed_keys[] = {
	FAN_ACTUAL_SPEED,
	FAN_MIN_SPEED,
	FAN_MAX_SPEED,
	FAN_SAFE_SPEED,
	FAN_TARGET_SPEED
};

#define INIT_TIMEOUT_MSECS	5000	/* wait up to 5s for device init ... */
#define INIT_WAIT_MSECS		50	/* ... in 50ms increments */

185
#define APPLESMC_POLL_INTERVAL	50	/* msecs */
186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206
#define APPLESMC_INPUT_FUZZ	4	/* input event threshold */
#define APPLESMC_INPUT_FLAT	4

#define SENSOR_X 0
#define SENSOR_Y 1
#define SENSOR_Z 2

/* Structure to be passed to DMI_MATCH function */
struct dmi_match_data {
/* Indicates whether this computer has an accelerometer. */
	int accelerometer;
/* Indicates whether this computer has light sensors and keyboard backlight. */
	int light;
/* Indicates which temperature sensors set to use. */
	int temperature_set;
};

static const int debug;
static struct platform_device *pdev;
static s16 rest_x;
static s16 rest_y;
207 208
static u8 backlight_state[2];

209
static struct device *hwmon_dev;
210
static struct input_polled_dev *applesmc_idev;
211 212 213 214 215 216 217

/* Indicates whether this computer has an accelerometer. */
static unsigned int applesmc_accelerometer;

/* Indicates whether this computer has light sensors and keyboard backlight. */
static unsigned int applesmc_light;

218 219 220
/* The number of fans handled by the driver */
static unsigned int fans_handled;

221 222 223
/* Indicates which temperature sensors set to use. */
static unsigned int applesmc_temperature_set;

224
static DEFINE_MUTEX(applesmc_lock);
225 226 227 228 229 230 231 232 233 234

/*
 * Last index written to key_at_index sysfs file, and value to use for all other
 * key_at_index_* sysfs files.
 */
static unsigned int key_at_index;

static struct workqueue_struct *applesmc_led_wq;

/*
235
 * __wait_status - Wait up to 32ms for the status port to get a certain value
236 237 238 239 240
 * (masked with 0x0f), returning zero if the value is obtained.  Callers must
 * hold applesmc_lock.
 */
static int __wait_status(u8 val)
{
241
	int us;
242 243 244

	val = val & APPLESMC_STATUS_MASK;

245 246
	for (us = APPLESMC_MIN_WAIT; us < APPLESMC_MAX_WAIT; us <<= 1) {
		udelay(us);
247 248 249
		if ((inb(APPLESMC_CMD_PORT) & APPLESMC_STATUS_MASK) == val) {
			if (debug)
				printk(KERN_DEBUG
250 251
					"Waited %d us for status %x\n",
					2 * us - APPLESMC_MIN_WAIT, val);
252 253 254 255
			return 0;
		}
	}

256
	pr_warn("wait status failed: %x != %x\n", val, inb(APPLESMC_CMD_PORT));
257 258 259 260

	return -EIO;
}

261 262 263 264 265 266 267
/*
 * special treatment of command port - on newer macbooks, it seems necessary
 * to resend the command byte before polling the status again. Callers must
 * hold applesmc_lock.
 */
static int send_command(u8 cmd)
{
268 269
	int us;
	for (us = APPLESMC_MIN_WAIT; us < APPLESMC_MAX_WAIT; us <<= 1) {
270
		outb(cmd, APPLESMC_CMD_PORT);
271
		udelay(us);
272 273 274
		if ((inb(APPLESMC_CMD_PORT) & APPLESMC_STATUS_MASK) == 0x0c)
			return 0;
	}
275
	pr_warn("command failed: %x -> %x\n", cmd, inb(APPLESMC_CMD_PORT));
276 277 278
	return -EIO;
}

279 280 281 282 283 284 285 286 287 288
/*
 * applesmc_read_key - reads len bytes from a given key, and put them in buffer.
 * Returns zero on success or a negative error on failure. Callers must
 * hold applesmc_lock.
 */
static int applesmc_read_key(const char* key, u8* buffer, u8 len)
{
	int i;

	if (len > APPLESMC_MAX_DATA_LENGTH) {
289 290
		pr_err("%s(): cannot read more than %d bytes\n",
		       __func__, APPLESMC_MAX_DATA_LENGTH);
291 292 293
		return -EINVAL;
	}

294
	if (send_command(APPLESMC_READ_CMD))
295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331
		return -EIO;

	for (i = 0; i < 4; i++) {
		outb(key[i], APPLESMC_DATA_PORT);
		if (__wait_status(0x04))
			return -EIO;
	}
	if (debug)
		printk(KERN_DEBUG "<%s", key);

	outb(len, APPLESMC_DATA_PORT);
	if (debug)
		printk(KERN_DEBUG ">%x", len);

	for (i = 0; i < len; i++) {
		if (__wait_status(0x05))
			return -EIO;
		buffer[i] = inb(APPLESMC_DATA_PORT);
		if (debug)
			printk(KERN_DEBUG "<%x", buffer[i]);
	}
	if (debug)
		printk(KERN_DEBUG "\n");

	return 0;
}

/*
 * applesmc_write_key - writes len bytes from buffer to a given key.
 * Returns zero on success or a negative error on failure. Callers must
 * hold applesmc_lock.
 */
static int applesmc_write_key(const char* key, u8* buffer, u8 len)
{
	int i;

	if (len > APPLESMC_MAX_DATA_LENGTH) {
332 333
		pr_err("%s(): cannot write more than %d bytes\n",
		       __func__, APPLESMC_MAX_DATA_LENGTH);
334 335 336
		return -EINVAL;
	}

337
	if (send_command(APPLESMC_WRITE_CMD))
338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370
		return -EIO;

	for (i = 0; i < 4; i++) {
		outb(key[i], APPLESMC_DATA_PORT);
		if (__wait_status(0x04))
			return -EIO;
	}

	outb(len, APPLESMC_DATA_PORT);

	for (i = 0; i < len; i++) {
		if (__wait_status(0x04))
			return -EIO;
		outb(buffer[i], APPLESMC_DATA_PORT);
	}

	return 0;
}

/*
 * applesmc_get_key_at_index - get key at index, and put the result in key
 * (char[6]). Returns zero on success or a negative error on failure. Callers
 * must hold applesmc_lock.
 */
static int applesmc_get_key_at_index(int index, char* key)
{
	int i;
	u8 readkey[4];
	readkey[0] = index >> 24;
	readkey[1] = index >> 16;
	readkey[2] = index >> 8;
	readkey[3] = index;

371
	if (send_command(APPLESMC_GET_KEY_BY_INDEX_CMD))
372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400
		return -EIO;

	for (i = 0; i < 4; i++) {
		outb(readkey[i], APPLESMC_DATA_PORT);
		if (__wait_status(0x04))
			return -EIO;
	}

	outb(4, APPLESMC_DATA_PORT);

	for (i = 0; i < 4; i++) {
		if (__wait_status(0x05))
			return -EIO;
		key[i] = inb(APPLESMC_DATA_PORT);
	}
	key[4] = 0;

	return 0;
}

/*
 * applesmc_get_key_type - get key type, and put the result in type (char[6]).
 * Returns zero on success or a negative error on failure. Callers must
 * hold applesmc_lock.
 */
static int applesmc_get_key_type(char* key, char* type)
{
	int i;

401
	if (send_command(APPLESMC_GET_KEY_TYPE_CMD))
402 403 404 405 406 407 408 409
		return -EIO;

	for (i = 0; i < 4; i++) {
		outb(key[i], APPLESMC_DATA_PORT);
		if (__wait_status(0x04))
			return -EIO;
	}

410
	outb(6, APPLESMC_DATA_PORT);
411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450

	for (i = 0; i < 6; i++) {
		if (__wait_status(0x05))
			return -EIO;
		type[i] = inb(APPLESMC_DATA_PORT);
	}
	type[5] = 0;

	return 0;
}

/*
 * applesmc_read_motion_sensor - Read motion sensor (X, Y or Z). Callers must
 * hold applesmc_lock.
 */
static int applesmc_read_motion_sensor(int index, s16* value)
{
	u8 buffer[2];
	int ret;

	switch (index) {
	case SENSOR_X:
		ret = applesmc_read_key(MOTION_SENSOR_X_KEY, buffer, 2);
		break;
	case SENSOR_Y:
		ret = applesmc_read_key(MOTION_SENSOR_Y_KEY, buffer, 2);
		break;
	case SENSOR_Z:
		ret = applesmc_read_key(MOTION_SENSOR_Z_KEY, buffer, 2);
		break;
	default:
		ret = -EINVAL;
	}

	*value = ((s16)buffer[0] << 8) | buffer[1];

	return ret;
}

/*
451
 * applesmc_device_init - initialize the accelerometer.  Can sleep.
452
 */
453
static void applesmc_device_init(void)
454
{
455
	int total;
456 457 458
	u8 buffer[2];

	if (!applesmc_accelerometer)
459
		return;
460 461 462 463 464

	mutex_lock(&applesmc_lock);

	for (total = INIT_TIMEOUT_MSECS; total > 0; total -= INIT_WAIT_MSECS) {
		if (!applesmc_read_key(MOTION_SENSOR_KEY, buffer, 2) &&
465
				(buffer[0] != 0x00 || buffer[1] != 0x00))
466 467 468 469 470 471 472
			goto out;
		buffer[0] = 0xe0;
		buffer[1] = 0x00;
		applesmc_write_key(MOTION_SENSOR_KEY, buffer, 2);
		msleep(INIT_WAIT_MSECS);
	}

473
	pr_warn("failed to init the device\n");
474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501

out:
	mutex_unlock(&applesmc_lock);
}

/*
 * applesmc_get_fan_count - get the number of fans. Callers must NOT hold
 * applesmc_lock.
 */
static int applesmc_get_fan_count(void)
{
	int ret;
	u8 buffer[1];

	mutex_lock(&applesmc_lock);

	ret = applesmc_read_key(FANS_COUNT, buffer, 1);

	mutex_unlock(&applesmc_lock);
	if (ret)
		return ret;
	else
		return buffer[0];
}

/* Device model stuff */
static int applesmc_probe(struct platform_device *dev)
{
502
	applesmc_device_init();
503 504 505 506

	return 0;
}

507 508 509 510 511 512 513 514 515 516 517 518
/* Synchronize device with memorized backlight state */
static int applesmc_pm_resume(struct device *dev)
{
	mutex_lock(&applesmc_lock);
	if (applesmc_light)
		applesmc_write_key(BACKLIGHT_KEY, backlight_state, 2);
	mutex_unlock(&applesmc_lock);
	return 0;
}

/* Reinitialize device on resume from hibernation */
static int applesmc_pm_restore(struct device *dev)
519
{
520
	applesmc_device_init();
521
	return applesmc_pm_resume(dev);
522 523
}

524
static const struct dev_pm_ops applesmc_pm_ops = {
525 526 527 528
	.resume = applesmc_pm_resume,
	.restore = applesmc_pm_restore,
};

529 530 531 532 533
static struct platform_driver applesmc_driver = {
	.probe = applesmc_probe,
	.driver	= {
		.name = "applesmc",
		.owner = THIS_MODULE,
534
		.pm = &applesmc_pm_ops,
535 536 537 538 539 540 541 542 543 544 545 546 547 548
	},
};

/*
 * applesmc_calibrate - Set our "resting" values.  Callers must
 * hold applesmc_lock.
 */
static void applesmc_calibrate(void)
{
	applesmc_read_motion_sensor(SENSOR_X, &rest_x);
	applesmc_read_motion_sensor(SENSOR_Y, &rest_y);
	rest_x = -rest_x;
}

549
static void applesmc_idev_poll(struct input_polled_dev *dev)
550
{
551
	struct input_dev *idev = dev->input;
552 553
	s16 x, y;

554
	mutex_lock(&applesmc_lock);
555 556 557 558 559 560 561

	if (applesmc_read_motion_sensor(SENSOR_X, &x))
		goto out;
	if (applesmc_read_motion_sensor(SENSOR_Y, &y))
		goto out;

	x = -x;
562 563 564
	input_report_abs(idev, ABS_X, x - rest_x);
	input_report_abs(idev, ABS_Y, y - rest_y);
	input_sync(idev);
565 566 567 568 569 570 571

out:
	mutex_unlock(&applesmc_lock);
}

/* Sysfs Files */

572 573 574 575 576 577
static ssize_t applesmc_name_show(struct device *dev,
				   struct device_attribute *attr, char *buf)
{
	return snprintf(buf, PAGE_SIZE, "applesmc\n");
}

578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606
static ssize_t applesmc_position_show(struct device *dev,
				   struct device_attribute *attr, char *buf)
{
	int ret;
	s16 x, y, z;

	mutex_lock(&applesmc_lock);

	ret = applesmc_read_motion_sensor(SENSOR_X, &x);
	if (ret)
		goto out;
	ret = applesmc_read_motion_sensor(SENSOR_Y, &y);
	if (ret)
		goto out;
	ret = applesmc_read_motion_sensor(SENSOR_Z, &z);
	if (ret)
		goto out;

out:
	mutex_unlock(&applesmc_lock);
	if (ret)
		return ret;
	else
		return snprintf(buf, PAGE_SIZE, "(%d,%d,%d)\n", x, y, z);
}

static ssize_t applesmc_light_show(struct device *dev,
				struct device_attribute *attr, char *sysfsbuf)
{
607
	static int data_length;
608 609
	int ret;
	u8 left = 0, right = 0;
610
	u8 buffer[10], query[6];
611 612 613

	mutex_lock(&applesmc_lock);

614 615 616 617 618
	if (!data_length) {
		ret = applesmc_get_key_type(LIGHT_SENSOR_LEFT_KEY, query);
		if (ret)
			goto out;
		data_length = clamp_val(query[0], 0, 10);
619
		pr_info("light sensor data length set to %d\n", data_length);
620 621 622
	}

	ret = applesmc_read_key(LIGHT_SENSOR_LEFT_KEY, buffer, data_length);
623 624 625 626 627
	/* newer macbooks report a single 10-bit bigendian value */
	if (data_length == 10) {
		left = be16_to_cpu(*(__be16 *)(buffer + 6)) >> 2;
		goto out;
	}
628 629 630
	left = buffer[2];
	if (ret)
		goto out;
631
	ret = applesmc_read_key(LIGHT_SENSOR_RIGHT_KEY, buffer, data_length);
632 633 634 635 636 637 638 639 640 641
	right = buffer[2];

out:
	mutex_unlock(&applesmc_lock);
	if (ret)
		return ret;
	else
		return snprintf(sysfsbuf, PAGE_SIZE, "(%d,%d)\n", left, right);
}

642 643 644 645 646 647 648 649 650 651 652
/* Displays sensor key as label */
static ssize_t applesmc_show_sensor_label(struct device *dev,
			struct device_attribute *devattr, char *sysfsbuf)
{
	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
	const char *key =
		temperature_sensors_sets[applesmc_temperature_set][attr->index];

	return snprintf(sysfsbuf, PAGE_SIZE, "%s\n", key);
}

653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843
/* Displays degree Celsius * 1000 */
static ssize_t applesmc_show_temperature(struct device *dev,
			struct device_attribute *devattr, char *sysfsbuf)
{
	int ret;
	u8 buffer[2];
	unsigned int temp;
	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
	const char* key =
		temperature_sensors_sets[applesmc_temperature_set][attr->index];

	mutex_lock(&applesmc_lock);

	ret = applesmc_read_key(key, buffer, 2);
	temp = buffer[0]*1000;
	temp += (buffer[1] >> 6) * 250;

	mutex_unlock(&applesmc_lock);

	if (ret)
		return ret;
	else
		return snprintf(sysfsbuf, PAGE_SIZE, "%u\n", temp);
}

static ssize_t applesmc_show_fan_speed(struct device *dev,
				struct device_attribute *attr, char *sysfsbuf)
{
	int ret;
	unsigned int speed = 0;
	char newkey[5];
	u8 buffer[2];
	struct sensor_device_attribute_2 *sensor_attr =
						to_sensor_dev_attr_2(attr);

	newkey[0] = fan_speed_keys[sensor_attr->nr][0];
	newkey[1] = '0' + sensor_attr->index;
	newkey[2] = fan_speed_keys[sensor_attr->nr][2];
	newkey[3] = fan_speed_keys[sensor_attr->nr][3];
	newkey[4] = 0;

	mutex_lock(&applesmc_lock);

	ret = applesmc_read_key(newkey, buffer, 2);
	speed = ((buffer[0] << 8 | buffer[1]) >> 2);

	mutex_unlock(&applesmc_lock);
	if (ret)
		return ret;
	else
		return snprintf(sysfsbuf, PAGE_SIZE, "%u\n", speed);
}

static ssize_t applesmc_store_fan_speed(struct device *dev,
					struct device_attribute *attr,
					const char *sysfsbuf, size_t count)
{
	int ret;
	u32 speed;
	char newkey[5];
	u8 buffer[2];
	struct sensor_device_attribute_2 *sensor_attr =
						to_sensor_dev_attr_2(attr);

	speed = simple_strtoul(sysfsbuf, NULL, 10);

	if (speed > 0x4000) /* Bigger than a 14-bit value */
		return -EINVAL;

	newkey[0] = fan_speed_keys[sensor_attr->nr][0];
	newkey[1] = '0' + sensor_attr->index;
	newkey[2] = fan_speed_keys[sensor_attr->nr][2];
	newkey[3] = fan_speed_keys[sensor_attr->nr][3];
	newkey[4] = 0;

	mutex_lock(&applesmc_lock);

	buffer[0] = (speed >> 6) & 0xff;
	buffer[1] = (speed << 2) & 0xff;
	ret = applesmc_write_key(newkey, buffer, 2);

	mutex_unlock(&applesmc_lock);
	if (ret)
		return ret;
	else
		return count;
}

static ssize_t applesmc_show_fan_manual(struct device *dev,
			struct device_attribute *devattr, char *sysfsbuf)
{
	int ret;
	u16 manual = 0;
	u8 buffer[2];
	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);

	mutex_lock(&applesmc_lock);

	ret = applesmc_read_key(FANS_MANUAL, buffer, 2);
	manual = ((buffer[0] << 8 | buffer[1]) >> attr->index) & 0x01;

	mutex_unlock(&applesmc_lock);
	if (ret)
		return ret;
	else
		return snprintf(sysfsbuf, PAGE_SIZE, "%d\n", manual);
}

static ssize_t applesmc_store_fan_manual(struct device *dev,
					 struct device_attribute *devattr,
					 const char *sysfsbuf, size_t count)
{
	int ret;
	u8 buffer[2];
	u32 input;
	u16 val;
	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);

	input = simple_strtoul(sysfsbuf, NULL, 10);

	mutex_lock(&applesmc_lock);

	ret = applesmc_read_key(FANS_MANUAL, buffer, 2);
	val = (buffer[0] << 8 | buffer[1]);
	if (ret)
		goto out;

	if (input)
		val = val | (0x01 << attr->index);
	else
		val = val & ~(0x01 << attr->index);

	buffer[0] = (val >> 8) & 0xFF;
	buffer[1] = val & 0xFF;

	ret = applesmc_write_key(FANS_MANUAL, buffer, 2);

out:
	mutex_unlock(&applesmc_lock);
	if (ret)
		return ret;
	else
		return count;
}

static ssize_t applesmc_show_fan_position(struct device *dev,
				struct device_attribute *attr, char *sysfsbuf)
{
	int ret;
	char newkey[5];
	u8 buffer[17];
	struct sensor_device_attribute_2 *sensor_attr =
						to_sensor_dev_attr_2(attr);

	newkey[0] = FAN_POSITION[0];
	newkey[1] = '0' + sensor_attr->index;
	newkey[2] = FAN_POSITION[2];
	newkey[3] = FAN_POSITION[3];
	newkey[4] = 0;

	mutex_lock(&applesmc_lock);

	ret = applesmc_read_key(newkey, buffer, 16);
	buffer[16] = 0;

	mutex_unlock(&applesmc_lock);
	if (ret)
		return ret;
	else
		return snprintf(sysfsbuf, PAGE_SIZE, "%s\n", buffer+4);
}

static ssize_t applesmc_calibrate_show(struct device *dev,
				struct device_attribute *attr, char *sysfsbuf)
{
	return snprintf(sysfsbuf, PAGE_SIZE, "(%d,%d)\n", rest_x, rest_y);
}

static ssize_t applesmc_calibrate_store(struct device *dev,
	struct device_attribute *attr, const char *sysfsbuf, size_t count)
{
	mutex_lock(&applesmc_lock);
	applesmc_calibrate();
	mutex_unlock(&applesmc_lock);

	return count;
}

static void applesmc_backlight_set(struct work_struct *work)
{
	mutex_lock(&applesmc_lock);
844
	applesmc_write_key(BACKLIGHT_KEY, backlight_state, 2);
845 846 847 848 849 850 851 852 853
	mutex_unlock(&applesmc_lock);
}
static DECLARE_WORK(backlight_work, &applesmc_backlight_set);

static void applesmc_brightness_set(struct led_classdev *led_cdev,
						enum led_brightness value)
{
	int ret;

854
	backlight_state[0] = value;
855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915
	ret = queue_work(applesmc_led_wq, &backlight_work);

	if (debug && (!ret))
		printk(KERN_DEBUG "applesmc: work was already on the queue.\n");
}

static ssize_t applesmc_key_count_show(struct device *dev,
				struct device_attribute *attr, char *sysfsbuf)
{
	int ret;
	u8 buffer[4];
	u32 count;

	mutex_lock(&applesmc_lock);

	ret = applesmc_read_key(KEY_COUNT_KEY, buffer, 4);
	count = ((u32)buffer[0]<<24) + ((u32)buffer[1]<<16) +
						((u32)buffer[2]<<8) + buffer[3];

	mutex_unlock(&applesmc_lock);
	if (ret)
		return ret;
	else
		return snprintf(sysfsbuf, PAGE_SIZE, "%d\n", count);
}

static ssize_t applesmc_key_at_index_read_show(struct device *dev,
				struct device_attribute *attr, char *sysfsbuf)
{
	char key[5];
	char info[6];
	int ret;

	mutex_lock(&applesmc_lock);

	ret = applesmc_get_key_at_index(key_at_index, key);

	if (ret || !key[0]) {
		mutex_unlock(&applesmc_lock);

		return -EINVAL;
	}

	ret = applesmc_get_key_type(key, info);

	if (ret) {
		mutex_unlock(&applesmc_lock);

		return ret;
	}

	/*
	 * info[0] maximum value (APPLESMC_MAX_DATA_LENGTH) is much lower than
	 * PAGE_SIZE, so we don't need any checks before writing to sysfsbuf.
	 */
	ret = applesmc_read_key(key, sysfsbuf, info[0]);

	mutex_unlock(&applesmc_lock);

	if (!ret) {
		return info[0];
916
	} else {
917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011
		return ret;
	}
}

static ssize_t applesmc_key_at_index_data_length_show(struct device *dev,
				struct device_attribute *attr, char *sysfsbuf)
{
	char key[5];
	char info[6];
	int ret;

	mutex_lock(&applesmc_lock);

	ret = applesmc_get_key_at_index(key_at_index, key);

	if (ret || !key[0]) {
		mutex_unlock(&applesmc_lock);

		return -EINVAL;
	}

	ret = applesmc_get_key_type(key, info);

	mutex_unlock(&applesmc_lock);

	if (!ret)
		return snprintf(sysfsbuf, PAGE_SIZE, "%d\n", info[0]);
	else
		return ret;
}

static ssize_t applesmc_key_at_index_type_show(struct device *dev,
				struct device_attribute *attr, char *sysfsbuf)
{
	char key[5];
	char info[6];
	int ret;

	mutex_lock(&applesmc_lock);

	ret = applesmc_get_key_at_index(key_at_index, key);

	if (ret || !key[0]) {
		mutex_unlock(&applesmc_lock);

		return -EINVAL;
	}

	ret = applesmc_get_key_type(key, info);

	mutex_unlock(&applesmc_lock);

	if (!ret)
		return snprintf(sysfsbuf, PAGE_SIZE, "%s\n", info+1);
	else
		return ret;
}

static ssize_t applesmc_key_at_index_name_show(struct device *dev,
				struct device_attribute *attr, char *sysfsbuf)
{
	char key[5];
	int ret;

	mutex_lock(&applesmc_lock);

	ret = applesmc_get_key_at_index(key_at_index, key);

	mutex_unlock(&applesmc_lock);

	if (!ret && key[0])
		return snprintf(sysfsbuf, PAGE_SIZE, "%s\n", key);
	else
		return -EINVAL;
}

static ssize_t applesmc_key_at_index_show(struct device *dev,
				struct device_attribute *attr, char *sysfsbuf)
{
	return snprintf(sysfsbuf, PAGE_SIZE, "%d\n", key_at_index);
}

static ssize_t applesmc_key_at_index_store(struct device *dev,
	struct device_attribute *attr, const char *sysfsbuf, size_t count)
{
	mutex_lock(&applesmc_lock);

	key_at_index = simple_strtoul(sysfsbuf, NULL, 10);

	mutex_unlock(&applesmc_lock);

	return count;
}

static struct led_classdev applesmc_backlight = {
1012
	.name			= "smc::kbd_backlight",
1013 1014 1015 1016
	.default_trigger	= "nand-disk",
	.brightness_set		= applesmc_brightness_set,
};

1017 1018
static DEVICE_ATTR(name, 0444, applesmc_name_show, NULL);

1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086
static DEVICE_ATTR(position, 0444, applesmc_position_show, NULL);
static DEVICE_ATTR(calibrate, 0644,
			applesmc_calibrate_show, applesmc_calibrate_store);

static struct attribute *accelerometer_attributes[] = {
	&dev_attr_position.attr,
	&dev_attr_calibrate.attr,
	NULL
};

static const struct attribute_group accelerometer_attributes_group =
	{ .attrs = accelerometer_attributes };

static DEVICE_ATTR(light, 0444, applesmc_light_show, NULL);

static DEVICE_ATTR(key_count, 0444, applesmc_key_count_show, NULL);
static DEVICE_ATTR(key_at_index, 0644,
		applesmc_key_at_index_show, applesmc_key_at_index_store);
static DEVICE_ATTR(key_at_index_name, 0444,
					applesmc_key_at_index_name_show, NULL);
static DEVICE_ATTR(key_at_index_type, 0444,
					applesmc_key_at_index_type_show, NULL);
static DEVICE_ATTR(key_at_index_data_length, 0444,
				applesmc_key_at_index_data_length_show, NULL);
static DEVICE_ATTR(key_at_index_data, 0444,
				applesmc_key_at_index_read_show, NULL);

static struct attribute *key_enumeration_attributes[] = {
	&dev_attr_key_count.attr,
	&dev_attr_key_at_index.attr,
	&dev_attr_key_at_index_name.attr,
	&dev_attr_key_at_index_type.attr,
	&dev_attr_key_at_index_data_length.attr,
	&dev_attr_key_at_index_data.attr,
	NULL
};

static const struct attribute_group key_enumeration_group =
	{ .attrs = key_enumeration_attributes };

/*
 * Macro defining SENSOR_DEVICE_ATTR for a fan sysfs entries.
 *  - show actual speed
 *  - show/store minimum speed
 *  - show maximum speed
 *  - show safe speed
 *  - show/store target speed
 *  - show/store manual mode
 */
#define sysfs_fan_speeds_offset(offset) \
static SENSOR_DEVICE_ATTR_2(fan##offset##_input, S_IRUGO, \
			applesmc_show_fan_speed, NULL, 0, offset-1); \
\
static SENSOR_DEVICE_ATTR_2(fan##offset##_min, S_IRUGO | S_IWUSR, \
	applesmc_show_fan_speed, applesmc_store_fan_speed, 1, offset-1); \
\
static SENSOR_DEVICE_ATTR_2(fan##offset##_max, S_IRUGO, \
			applesmc_show_fan_speed, NULL, 2, offset-1); \
\
static SENSOR_DEVICE_ATTR_2(fan##offset##_safe, S_IRUGO, \
			applesmc_show_fan_speed, NULL, 3, offset-1); \
\
static SENSOR_DEVICE_ATTR_2(fan##offset##_output, S_IRUGO | S_IWUSR, \
	applesmc_show_fan_speed, applesmc_store_fan_speed, 4, offset-1); \
\
static SENSOR_DEVICE_ATTR(fan##offset##_manual, S_IRUGO | S_IWUSR, \
	applesmc_show_fan_manual, applesmc_store_fan_manual, offset-1); \
\
1087
static SENSOR_DEVICE_ATTR(fan##offset##_label, S_IRUGO, \
1088 1089 1090 1091 1092 1093 1094 1095 1096
	applesmc_show_fan_position, NULL, offset-1); \
\
static struct attribute *fan##offset##_attributes[] = { \
	&sensor_dev_attr_fan##offset##_input.dev_attr.attr, \
	&sensor_dev_attr_fan##offset##_min.dev_attr.attr, \
	&sensor_dev_attr_fan##offset##_max.dev_attr.attr, \
	&sensor_dev_attr_fan##offset##_safe.dev_attr.attr, \
	&sensor_dev_attr_fan##offset##_output.dev_attr.attr, \
	&sensor_dev_attr_fan##offset##_manual.dev_attr.attr, \
1097
	&sensor_dev_attr_fan##offset##_label.dev_attr.attr, \
1098 1099 1100 1101 1102
	NULL \
};

/*
 * Create the needed functions for each fan using the macro defined above
1103
 * (4 fans are supported)
1104 1105 1106
 */
sysfs_fan_speeds_offset(1);
sysfs_fan_speeds_offset(2);
1107 1108
sysfs_fan_speeds_offset(3);
sysfs_fan_speeds_offset(4);
1109 1110 1111

static const struct attribute_group fan_attribute_groups[] = {
	{ .attrs = fan1_attributes },
1112 1113 1114
	{ .attrs = fan2_attributes },
	{ .attrs = fan3_attributes },
	{ .attrs = fan4_attributes },
1115 1116 1117 1118 1119
};

/*
 * Temperature sensors sysfs entries.
 */
1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199
static SENSOR_DEVICE_ATTR(temp1_label, S_IRUGO,
					applesmc_show_sensor_label, NULL, 0);
static SENSOR_DEVICE_ATTR(temp2_label, S_IRUGO,
					applesmc_show_sensor_label, NULL, 1);
static SENSOR_DEVICE_ATTR(temp3_label, S_IRUGO,
					applesmc_show_sensor_label, NULL, 2);
static SENSOR_DEVICE_ATTR(temp4_label, S_IRUGO,
					applesmc_show_sensor_label, NULL, 3);
static SENSOR_DEVICE_ATTR(temp5_label, S_IRUGO,
					applesmc_show_sensor_label, NULL, 4);
static SENSOR_DEVICE_ATTR(temp6_label, S_IRUGO,
					applesmc_show_sensor_label, NULL, 5);
static SENSOR_DEVICE_ATTR(temp7_label, S_IRUGO,
					applesmc_show_sensor_label, NULL, 6);
static SENSOR_DEVICE_ATTR(temp8_label, S_IRUGO,
					applesmc_show_sensor_label, NULL, 7);
static SENSOR_DEVICE_ATTR(temp9_label, S_IRUGO,
					applesmc_show_sensor_label, NULL, 8);
static SENSOR_DEVICE_ATTR(temp10_label, S_IRUGO,
					applesmc_show_sensor_label, NULL, 9);
static SENSOR_DEVICE_ATTR(temp11_label, S_IRUGO,
					applesmc_show_sensor_label, NULL, 10);
static SENSOR_DEVICE_ATTR(temp12_label, S_IRUGO,
					applesmc_show_sensor_label, NULL, 11);
static SENSOR_DEVICE_ATTR(temp13_label, S_IRUGO,
					applesmc_show_sensor_label, NULL, 12);
static SENSOR_DEVICE_ATTR(temp14_label, S_IRUGO,
					applesmc_show_sensor_label, NULL, 13);
static SENSOR_DEVICE_ATTR(temp15_label, S_IRUGO,
					applesmc_show_sensor_label, NULL, 14);
static SENSOR_DEVICE_ATTR(temp16_label, S_IRUGO,
					applesmc_show_sensor_label, NULL, 15);
static SENSOR_DEVICE_ATTR(temp17_label, S_IRUGO,
					applesmc_show_sensor_label, NULL, 16);
static SENSOR_DEVICE_ATTR(temp18_label, S_IRUGO,
					applesmc_show_sensor_label, NULL, 17);
static SENSOR_DEVICE_ATTR(temp19_label, S_IRUGO,
					applesmc_show_sensor_label, NULL, 18);
static SENSOR_DEVICE_ATTR(temp20_label, S_IRUGO,
					applesmc_show_sensor_label, NULL, 19);
static SENSOR_DEVICE_ATTR(temp21_label, S_IRUGO,
					applesmc_show_sensor_label, NULL, 20);
static SENSOR_DEVICE_ATTR(temp22_label, S_IRUGO,
					applesmc_show_sensor_label, NULL, 21);
static SENSOR_DEVICE_ATTR(temp23_label, S_IRUGO,
					applesmc_show_sensor_label, NULL, 22);
static SENSOR_DEVICE_ATTR(temp24_label, S_IRUGO,
					applesmc_show_sensor_label, NULL, 23);
static SENSOR_DEVICE_ATTR(temp25_label, S_IRUGO,
					applesmc_show_sensor_label, NULL, 24);
static SENSOR_DEVICE_ATTR(temp26_label, S_IRUGO,
					applesmc_show_sensor_label, NULL, 25);
static SENSOR_DEVICE_ATTR(temp27_label, S_IRUGO,
					applesmc_show_sensor_label, NULL, 26);
static SENSOR_DEVICE_ATTR(temp28_label, S_IRUGO,
					applesmc_show_sensor_label, NULL, 27);
static SENSOR_DEVICE_ATTR(temp29_label, S_IRUGO,
					applesmc_show_sensor_label, NULL, 28);
static SENSOR_DEVICE_ATTR(temp30_label, S_IRUGO,
					applesmc_show_sensor_label, NULL, 29);
static SENSOR_DEVICE_ATTR(temp31_label, S_IRUGO,
					applesmc_show_sensor_label, NULL, 30);
static SENSOR_DEVICE_ATTR(temp32_label, S_IRUGO,
					applesmc_show_sensor_label, NULL, 31);
static SENSOR_DEVICE_ATTR(temp33_label, S_IRUGO,
					applesmc_show_sensor_label, NULL, 32);
static SENSOR_DEVICE_ATTR(temp34_label, S_IRUGO,
					applesmc_show_sensor_label, NULL, 33);
static SENSOR_DEVICE_ATTR(temp35_label, S_IRUGO,
					applesmc_show_sensor_label, NULL, 34);
static SENSOR_DEVICE_ATTR(temp36_label, S_IRUGO,
					applesmc_show_sensor_label, NULL, 35);
static SENSOR_DEVICE_ATTR(temp37_label, S_IRUGO,
					applesmc_show_sensor_label, NULL, 36);
static SENSOR_DEVICE_ATTR(temp38_label, S_IRUGO,
					applesmc_show_sensor_label, NULL, 37);
static SENSOR_DEVICE_ATTR(temp39_label, S_IRUGO,
					applesmc_show_sensor_label, NULL, 38);
static SENSOR_DEVICE_ATTR(temp40_label, S_IRUGO,
					applesmc_show_sensor_label, NULL, 39);
1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223
static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO,
					applesmc_show_temperature, NULL, 0);
static SENSOR_DEVICE_ATTR(temp2_input, S_IRUGO,
					applesmc_show_temperature, NULL, 1);
static SENSOR_DEVICE_ATTR(temp3_input, S_IRUGO,
					applesmc_show_temperature, NULL, 2);
static SENSOR_DEVICE_ATTR(temp4_input, S_IRUGO,
					applesmc_show_temperature, NULL, 3);
static SENSOR_DEVICE_ATTR(temp5_input, S_IRUGO,
					applesmc_show_temperature, NULL, 4);
static SENSOR_DEVICE_ATTR(temp6_input, S_IRUGO,
					applesmc_show_temperature, NULL, 5);
static SENSOR_DEVICE_ATTR(temp7_input, S_IRUGO,
					applesmc_show_temperature, NULL, 6);
static SENSOR_DEVICE_ATTR(temp8_input, S_IRUGO,
					applesmc_show_temperature, NULL, 7);
static SENSOR_DEVICE_ATTR(temp9_input, S_IRUGO,
					applesmc_show_temperature, NULL, 8);
static SENSOR_DEVICE_ATTR(temp10_input, S_IRUGO,
					applesmc_show_temperature, NULL, 9);
static SENSOR_DEVICE_ATTR(temp11_input, S_IRUGO,
					applesmc_show_temperature, NULL, 10);
static SENSOR_DEVICE_ATTR(temp12_input, S_IRUGO,
					applesmc_show_temperature, NULL, 11);
1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269
static SENSOR_DEVICE_ATTR(temp13_input, S_IRUGO,
					applesmc_show_temperature, NULL, 12);
static SENSOR_DEVICE_ATTR(temp14_input, S_IRUGO,
					applesmc_show_temperature, NULL, 13);
static SENSOR_DEVICE_ATTR(temp15_input, S_IRUGO,
					applesmc_show_temperature, NULL, 14);
static SENSOR_DEVICE_ATTR(temp16_input, S_IRUGO,
					applesmc_show_temperature, NULL, 15);
static SENSOR_DEVICE_ATTR(temp17_input, S_IRUGO,
					applesmc_show_temperature, NULL, 16);
static SENSOR_DEVICE_ATTR(temp18_input, S_IRUGO,
					applesmc_show_temperature, NULL, 17);
static SENSOR_DEVICE_ATTR(temp19_input, S_IRUGO,
					applesmc_show_temperature, NULL, 18);
static SENSOR_DEVICE_ATTR(temp20_input, S_IRUGO,
					applesmc_show_temperature, NULL, 19);
static SENSOR_DEVICE_ATTR(temp21_input, S_IRUGO,
					applesmc_show_temperature, NULL, 20);
static SENSOR_DEVICE_ATTR(temp22_input, S_IRUGO,
					applesmc_show_temperature, NULL, 21);
static SENSOR_DEVICE_ATTR(temp23_input, S_IRUGO,
					applesmc_show_temperature, NULL, 22);
static SENSOR_DEVICE_ATTR(temp24_input, S_IRUGO,
					applesmc_show_temperature, NULL, 23);
static SENSOR_DEVICE_ATTR(temp25_input, S_IRUGO,
					applesmc_show_temperature, NULL, 24);
static SENSOR_DEVICE_ATTR(temp26_input, S_IRUGO,
					applesmc_show_temperature, NULL, 25);
static SENSOR_DEVICE_ATTR(temp27_input, S_IRUGO,
					applesmc_show_temperature, NULL, 26);
static SENSOR_DEVICE_ATTR(temp28_input, S_IRUGO,
					applesmc_show_temperature, NULL, 27);
static SENSOR_DEVICE_ATTR(temp29_input, S_IRUGO,
					applesmc_show_temperature, NULL, 28);
static SENSOR_DEVICE_ATTR(temp30_input, S_IRUGO,
					applesmc_show_temperature, NULL, 29);
static SENSOR_DEVICE_ATTR(temp31_input, S_IRUGO,
					applesmc_show_temperature, NULL, 30);
static SENSOR_DEVICE_ATTR(temp32_input, S_IRUGO,
					applesmc_show_temperature, NULL, 31);
static SENSOR_DEVICE_ATTR(temp33_input, S_IRUGO,
					applesmc_show_temperature, NULL, 32);
static SENSOR_DEVICE_ATTR(temp34_input, S_IRUGO,
					applesmc_show_temperature, NULL, 33);
static SENSOR_DEVICE_ATTR(temp35_input, S_IRUGO,
					applesmc_show_temperature, NULL, 34);
1270 1271 1272 1273 1274 1275 1276 1277 1278 1279
static SENSOR_DEVICE_ATTR(temp36_input, S_IRUGO,
					applesmc_show_temperature, NULL, 35);
static SENSOR_DEVICE_ATTR(temp37_input, S_IRUGO,
					applesmc_show_temperature, NULL, 36);
static SENSOR_DEVICE_ATTR(temp38_input, S_IRUGO,
					applesmc_show_temperature, NULL, 37);
static SENSOR_DEVICE_ATTR(temp39_input, S_IRUGO,
					applesmc_show_temperature, NULL, 38);
static SENSOR_DEVICE_ATTR(temp40_input, S_IRUGO,
					applesmc_show_temperature, NULL, 39);
1280

1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324
static struct attribute *label_attributes[] = {
	&sensor_dev_attr_temp1_label.dev_attr.attr,
	&sensor_dev_attr_temp2_label.dev_attr.attr,
	&sensor_dev_attr_temp3_label.dev_attr.attr,
	&sensor_dev_attr_temp4_label.dev_attr.attr,
	&sensor_dev_attr_temp5_label.dev_attr.attr,
	&sensor_dev_attr_temp6_label.dev_attr.attr,
	&sensor_dev_attr_temp7_label.dev_attr.attr,
	&sensor_dev_attr_temp8_label.dev_attr.attr,
	&sensor_dev_attr_temp9_label.dev_attr.attr,
	&sensor_dev_attr_temp10_label.dev_attr.attr,
	&sensor_dev_attr_temp11_label.dev_attr.attr,
	&sensor_dev_attr_temp12_label.dev_attr.attr,
	&sensor_dev_attr_temp13_label.dev_attr.attr,
	&sensor_dev_attr_temp14_label.dev_attr.attr,
	&sensor_dev_attr_temp15_label.dev_attr.attr,
	&sensor_dev_attr_temp16_label.dev_attr.attr,
	&sensor_dev_attr_temp17_label.dev_attr.attr,
	&sensor_dev_attr_temp18_label.dev_attr.attr,
	&sensor_dev_attr_temp19_label.dev_attr.attr,
	&sensor_dev_attr_temp20_label.dev_attr.attr,
	&sensor_dev_attr_temp21_label.dev_attr.attr,
	&sensor_dev_attr_temp22_label.dev_attr.attr,
	&sensor_dev_attr_temp23_label.dev_attr.attr,
	&sensor_dev_attr_temp24_label.dev_attr.attr,
	&sensor_dev_attr_temp25_label.dev_attr.attr,
	&sensor_dev_attr_temp26_label.dev_attr.attr,
	&sensor_dev_attr_temp27_label.dev_attr.attr,
	&sensor_dev_attr_temp28_label.dev_attr.attr,
	&sensor_dev_attr_temp29_label.dev_attr.attr,
	&sensor_dev_attr_temp30_label.dev_attr.attr,
	&sensor_dev_attr_temp31_label.dev_attr.attr,
	&sensor_dev_attr_temp32_label.dev_attr.attr,
	&sensor_dev_attr_temp33_label.dev_attr.attr,
	&sensor_dev_attr_temp34_label.dev_attr.attr,
	&sensor_dev_attr_temp35_label.dev_attr.attr,
	&sensor_dev_attr_temp36_label.dev_attr.attr,
	&sensor_dev_attr_temp37_label.dev_attr.attr,
	&sensor_dev_attr_temp38_label.dev_attr.attr,
	&sensor_dev_attr_temp39_label.dev_attr.attr,
	&sensor_dev_attr_temp40_label.dev_attr.attr,
	NULL
};

1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337
static struct attribute *temperature_attributes[] = {
	&sensor_dev_attr_temp1_input.dev_attr.attr,
	&sensor_dev_attr_temp2_input.dev_attr.attr,
	&sensor_dev_attr_temp3_input.dev_attr.attr,
	&sensor_dev_attr_temp4_input.dev_attr.attr,
	&sensor_dev_attr_temp5_input.dev_attr.attr,
	&sensor_dev_attr_temp6_input.dev_attr.attr,
	&sensor_dev_attr_temp7_input.dev_attr.attr,
	&sensor_dev_attr_temp8_input.dev_attr.attr,
	&sensor_dev_attr_temp9_input.dev_attr.attr,
	&sensor_dev_attr_temp10_input.dev_attr.attr,
	&sensor_dev_attr_temp11_input.dev_attr.attr,
	&sensor_dev_attr_temp12_input.dev_attr.attr,
1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360
	&sensor_dev_attr_temp13_input.dev_attr.attr,
	&sensor_dev_attr_temp14_input.dev_attr.attr,
	&sensor_dev_attr_temp15_input.dev_attr.attr,
	&sensor_dev_attr_temp16_input.dev_attr.attr,
	&sensor_dev_attr_temp17_input.dev_attr.attr,
	&sensor_dev_attr_temp18_input.dev_attr.attr,
	&sensor_dev_attr_temp19_input.dev_attr.attr,
	&sensor_dev_attr_temp20_input.dev_attr.attr,
	&sensor_dev_attr_temp21_input.dev_attr.attr,
	&sensor_dev_attr_temp22_input.dev_attr.attr,
	&sensor_dev_attr_temp23_input.dev_attr.attr,
	&sensor_dev_attr_temp24_input.dev_attr.attr,
	&sensor_dev_attr_temp25_input.dev_attr.attr,
	&sensor_dev_attr_temp26_input.dev_attr.attr,
	&sensor_dev_attr_temp27_input.dev_attr.attr,
	&sensor_dev_attr_temp28_input.dev_attr.attr,
	&sensor_dev_attr_temp29_input.dev_attr.attr,
	&sensor_dev_attr_temp30_input.dev_attr.attr,
	&sensor_dev_attr_temp31_input.dev_attr.attr,
	&sensor_dev_attr_temp32_input.dev_attr.attr,
	&sensor_dev_attr_temp33_input.dev_attr.attr,
	&sensor_dev_attr_temp34_input.dev_attr.attr,
	&sensor_dev_attr_temp35_input.dev_attr.attr,
1361 1362 1363 1364 1365
	&sensor_dev_attr_temp36_input.dev_attr.attr,
	&sensor_dev_attr_temp37_input.dev_attr.attr,
	&sensor_dev_attr_temp38_input.dev_attr.attr,
	&sensor_dev_attr_temp39_input.dev_attr.attr,
	&sensor_dev_attr_temp40_input.dev_attr.attr,
1366 1367 1368 1369 1370 1371
	NULL
};

static const struct attribute_group temperature_attributes_group =
	{ .attrs = temperature_attributes };

1372 1373 1374 1375
static const struct attribute_group label_attributes_group = {
	.attrs = label_attributes
};

1376 1377 1378 1379 1380
/* Module stuff */

/*
 * applesmc_dmi_match - found a match.  return one, short-circuiting the hunt.
 */
1381
static int applesmc_dmi_match(const struct dmi_system_id *id)
1382 1383 1384
{
	int i = 0;
	struct dmi_match_data* dmi_data = id->driver_data;
1385
	pr_info("%s detected:\n", id->ident);
1386
	applesmc_accelerometer = dmi_data->accelerometer;
1387 1388
	pr_info(" - Model %s accelerometer\n",
		applesmc_accelerometer ? "with" : "without");
1389
	applesmc_light = dmi_data->light;
1390 1391
	pr_info(" - Model %s light sensors and backlight\n",
		applesmc_light ? "with" : "without");
1392 1393 1394 1395

	applesmc_temperature_set =  dmi_data->temperature_set;
	while (temperature_sensors_sets[applesmc_temperature_set][i] != NULL)
		i++;
1396
	pr_info(" - Model with %d temperature sensors\n", i);
1397 1398 1399 1400 1401 1402
	return 1;
}

/* Create accelerometer ressources */
static int applesmc_create_accelerometer(void)
{
1403
	struct input_dev *idev;
1404 1405 1406 1407 1408 1409 1410
	int ret;

	ret = sysfs_create_group(&pdev->dev.kobj,
					&accelerometer_attributes_group);
	if (ret)
		goto out;

1411
	applesmc_idev = input_allocate_polled_device();
1412 1413 1414 1415 1416
	if (!applesmc_idev) {
		ret = -ENOMEM;
		goto out_sysfs;
	}

1417 1418 1419
	applesmc_idev->poll = applesmc_idev_poll;
	applesmc_idev->poll_interval = APPLESMC_POLL_INTERVAL;

1420 1421 1422
	/* initial calibrate for the input device */
	applesmc_calibrate();

1423 1424 1425 1426 1427
	/* initialize the input device */
	idev = applesmc_idev->input;
	idev->name = "applesmc";
	idev->id.bustype = BUS_HOST;
	idev->dev.parent = &pdev->dev;
1428
	idev->evbit[0] = BIT_MASK(EV_ABS);
1429
	input_set_abs_params(idev, ABS_X,
1430
			-256, 256, APPLESMC_INPUT_FUZZ, APPLESMC_INPUT_FLAT);
1431
	input_set_abs_params(idev, ABS_Y,
1432 1433
			-256, 256, APPLESMC_INPUT_FUZZ, APPLESMC_INPUT_FLAT);

1434
	ret = input_register_polled_device(applesmc_idev);
1435 1436 1437 1438 1439 1440
	if (ret)
		goto out_idev;

	return 0;

out_idev:
1441
	input_free_polled_device(applesmc_idev);
1442 1443 1444 1445 1446

out_sysfs:
	sysfs_remove_group(&pdev->dev.kobj, &accelerometer_attributes_group);

out:
1447
	pr_warn("driver init failed (ret=%d)!\n", ret);
1448 1449 1450 1451 1452 1453
	return ret;
}

/* Release all ressources used by the accelerometer */
static void applesmc_release_accelerometer(void)
{
1454 1455
	input_unregister_polled_device(applesmc_idev);
	input_free_polled_device(applesmc_idev);
1456 1457 1458 1459 1460 1461
	sysfs_remove_group(&pdev->dev.kobj, &accelerometer_attributes_group);
}

static __initdata struct dmi_match_data applesmc_dmi_data[] = {
/* MacBook Pro: accelerometer, backlight and temperature set 0 */
	{ .accelerometer = 1, .light = 1, .temperature_set = 0 },
1462
/* MacBook2: accelerometer and temperature set 1 */
1463
	{ .accelerometer = 1, .light = 0, .temperature_set = 1 },
1464 1465 1466
/* MacBook: accelerometer and temperature set 2 */
	{ .accelerometer = 1, .light = 0, .temperature_set = 2 },
/* MacMini: temperature set 3 */
1467
	{ .accelerometer = 0, .light = 0, .temperature_set = 3 },
1468 1469
/* MacPro: temperature set 4 */
	{ .accelerometer = 0, .light = 0, .temperature_set = 4 },
1470 1471
/* iMac: temperature set 5 */
	{ .accelerometer = 0, .light = 0, .temperature_set = 5 },
1472
/* MacBook3, MacBook4: accelerometer and temperature set 6 */
1473
	{ .accelerometer = 1, .light = 0, .temperature_set = 6 },
1474 1475
/* MacBook Air: accelerometer, backlight and temperature set 7 */
	{ .accelerometer = 1, .light = 1, .temperature_set = 7 },
1476 1477
/* MacBook Pro 4: accelerometer, backlight and temperature set 8 */
	{ .accelerometer = 1, .light = 1, .temperature_set = 8 },
1478 1479
/* MacBook Pro 3: accelerometer, backlight and temperature set 9 */
	{ .accelerometer = 1, .light = 1, .temperature_set = 9 },
1480 1481
/* iMac 5: light sensor only, temperature set 10 */
	{ .accelerometer = 0, .light = 0, .temperature_set = 10 },
1482 1483
/* MacBook 5: accelerometer, backlight and temperature set 11 */
	{ .accelerometer = 1, .light = 1, .temperature_set = 11 },
1484 1485
/* MacBook Pro 5: accelerometer, backlight and temperature set 12 */
	{ .accelerometer = 1, .light = 1, .temperature_set = 12 },
1486 1487
/* iMac 8: light sensor only, temperature set 13 */
	{ .accelerometer = 0, .light = 0, .temperature_set = 13 },
1488 1489
/* iMac 6: light sensor only, temperature set 14 */
	{ .accelerometer = 0, .light = 0, .temperature_set = 14 },
1490 1491
/* MacBook Air 2,1: accelerometer, backlight and temperature set 15 */
	{ .accelerometer = 1, .light = 1, .temperature_set = 15 },
1492 1493
/* MacPro3,1: temperature set 16 */
	{ .accelerometer = 0, .light = 0, .temperature_set = 16 },
1494 1495 1496 1497
/* iMac 9,1: light sensor only, temperature set 17 */
	{ .accelerometer = 0, .light = 0, .temperature_set = 17 },
/* MacBook Pro 2,2: accelerometer, backlight and temperature set 18 */
	{ .accelerometer = 1, .light = 1, .temperature_set = 18 },
1498 1499 1500 1501
/* MacBook Pro 5,3: accelerometer, backlight and temperature set 19 */
	{ .accelerometer = 1, .light = 1, .temperature_set = 19 },
/* MacBook Pro 5,4: accelerometer, backlight and temperature set 20 */
	{ .accelerometer = 1, .light = 1, .temperature_set = 20 },
1502 1503
/* MacBook Pro 6,2: accelerometer, backlight and temperature set 21 */
	{ .accelerometer = 1, .light = 1, .temperature_set = 21 },
1504 1505
/* MacBook Pro 7,1: accelerometer, backlight and temperature set 22 */
	{ .accelerometer = 1, .light = 1, .temperature_set = 22 },
1506 1507
/* MacBook Air 3,1: accelerometer, backlight and temperature set 23 */
	{ .accelerometer = 0, .light = 0, .temperature_set = 23 },
1508 1509 1510 1511 1512
};

/* Note that DMI_MATCH(...,"MacBook") will match "MacBookPro1,1".
 * So we need to put "Apple MacBook Pro" before "Apple MacBook". */
static __initdata struct dmi_system_id applesmc_whitelist[] = {
1513 1514 1515 1516
	{ applesmc_dmi_match, "Apple MacBook Air 3", {
	  DMI_MATCH(DMI_BOARD_VENDOR, "Apple"),
	  DMI_MATCH(DMI_PRODUCT_NAME, "MacBookAir3") },
		&applesmc_dmi_data[23]},
1517 1518 1519 1520
	{ applesmc_dmi_match, "Apple MacBook Air 2", {
	  DMI_MATCH(DMI_BOARD_VENDOR, "Apple"),
	  DMI_MATCH(DMI_PRODUCT_NAME, "MacBookAir2") },
		&applesmc_dmi_data[15]},
1521 1522 1523
	{ applesmc_dmi_match, "Apple MacBook Air", {
	  DMI_MATCH(DMI_BOARD_VENDOR, "Apple"),
	  DMI_MATCH(DMI_PRODUCT_NAME, "MacBookAir") },
1524
		&applesmc_dmi_data[7]},
1525 1526 1527 1528
	{ applesmc_dmi_match, "Apple MacBook Pro 7", {
	  DMI_MATCH(DMI_BOARD_VENDOR, "Apple"),
	  DMI_MATCH(DMI_PRODUCT_NAME, "MacBookPro7") },
		&applesmc_dmi_data[22]},
1529 1530 1531 1532 1533 1534 1535 1536
	{ applesmc_dmi_match, "Apple MacBook Pro 5,4", {
	  DMI_MATCH(DMI_BOARD_VENDOR, "Apple"),
	  DMI_MATCH(DMI_PRODUCT_NAME, "MacBookPro5,4") },
		&applesmc_dmi_data[20]},
	{ applesmc_dmi_match, "Apple MacBook Pro 5,3", {
	  DMI_MATCH(DMI_BOARD_VENDOR, "Apple"),
	  DMI_MATCH(DMI_PRODUCT_NAME, "MacBookPro5,3") },
		&applesmc_dmi_data[19]},
1537 1538 1539 1540
	{ applesmc_dmi_match, "Apple MacBook Pro 6", {
	  DMI_MATCH(DMI_BOARD_VENDOR, "Apple"),
	  DMI_MATCH(DMI_PRODUCT_NAME, "MacBookPro6") },
		&applesmc_dmi_data[21]},
1541 1542 1543 1544
	{ applesmc_dmi_match, "Apple MacBook Pro 5", {
	  DMI_MATCH(DMI_BOARD_VENDOR, "Apple"),
	  DMI_MATCH(DMI_PRODUCT_NAME, "MacBookPro5") },
		&applesmc_dmi_data[12]},
1545 1546 1547 1548
	{ applesmc_dmi_match, "Apple MacBook Pro 4", {
	  DMI_MATCH(DMI_BOARD_VENDOR, "Apple"),
	  DMI_MATCH(DMI_PRODUCT_NAME, "MacBookPro4") },
		&applesmc_dmi_data[8]},
1549 1550 1551 1552
	{ applesmc_dmi_match, "Apple MacBook Pro 3", {
	  DMI_MATCH(DMI_BOARD_VENDOR, "Apple"),
	  DMI_MATCH(DMI_PRODUCT_NAME, "MacBookPro3") },
		&applesmc_dmi_data[9]},
1553 1554 1555 1556
	{ applesmc_dmi_match, "Apple MacBook Pro 2,2", {
	  DMI_MATCH(DMI_BOARD_VENDOR, "Apple Computer, Inc."),
	  DMI_MATCH(DMI_PRODUCT_NAME, "MacBookPro2,2") },
		&applesmc_dmi_data[18]},
1557 1558 1559
	{ applesmc_dmi_match, "Apple MacBook Pro", {
	  DMI_MATCH(DMI_BOARD_VENDOR,"Apple"),
	  DMI_MATCH(DMI_PRODUCT_NAME,"MacBookPro") },
1560
		&applesmc_dmi_data[0]},
1561
	{ applesmc_dmi_match, "Apple MacBook (v2)", {
1562
	  DMI_MATCH(DMI_BOARD_VENDOR,"Apple"),
1563
	  DMI_MATCH(DMI_PRODUCT_NAME,"MacBook2") },
1564
		&applesmc_dmi_data[1]},
1565 1566 1567
	{ applesmc_dmi_match, "Apple MacBook (v3)", {
	  DMI_MATCH(DMI_BOARD_VENDOR,"Apple"),
	  DMI_MATCH(DMI_PRODUCT_NAME,"MacBook3") },
1568
		&applesmc_dmi_data[6]},
1569 1570 1571 1572
	{ applesmc_dmi_match, "Apple MacBook 4", {
	  DMI_MATCH(DMI_BOARD_VENDOR, "Apple"),
	  DMI_MATCH(DMI_PRODUCT_NAME, "MacBook4") },
		&applesmc_dmi_data[6]},
1573 1574 1575 1576
	{ applesmc_dmi_match, "Apple MacBook 5", {
	  DMI_MATCH(DMI_BOARD_VENDOR, "Apple"),
	  DMI_MATCH(DMI_PRODUCT_NAME, "MacBook5") },
		&applesmc_dmi_data[11]},
1577 1578 1579
	{ applesmc_dmi_match, "Apple MacBook", {
	  DMI_MATCH(DMI_BOARD_VENDOR,"Apple"),
	  DMI_MATCH(DMI_PRODUCT_NAME,"MacBook") },
1580
		&applesmc_dmi_data[2]},
1581 1582 1583
	{ applesmc_dmi_match, "Apple Macmini", {
	  DMI_MATCH(DMI_BOARD_VENDOR,"Apple"),
	  DMI_MATCH(DMI_PRODUCT_NAME,"Macmini") },
1584
		&applesmc_dmi_data[3]},
1585 1586 1587
	{ applesmc_dmi_match, "Apple MacPro2", {
	  DMI_MATCH(DMI_BOARD_VENDOR,"Apple"),
	  DMI_MATCH(DMI_PRODUCT_NAME,"MacPro2") },
1588
		&applesmc_dmi_data[4]},
1589 1590 1591 1592
	{ applesmc_dmi_match, "Apple MacPro3", {
	  DMI_MATCH(DMI_BOARD_VENDOR, "Apple"),
	  DMI_MATCH(DMI_PRODUCT_NAME, "MacPro3") },
		&applesmc_dmi_data[16]},
1593 1594 1595 1596
	{ applesmc_dmi_match, "Apple MacPro", {
	  DMI_MATCH(DMI_BOARD_VENDOR, "Apple"),
	  DMI_MATCH(DMI_PRODUCT_NAME, "MacPro") },
		&applesmc_dmi_data[4]},
1597 1598 1599 1600
	{ applesmc_dmi_match, "Apple iMac 9,1", {
	  DMI_MATCH(DMI_BOARD_VENDOR, "Apple Inc."),
	  DMI_MATCH(DMI_PRODUCT_NAME, "iMac9,1") },
		&applesmc_dmi_data[17]},
1601 1602 1603 1604
	{ applesmc_dmi_match, "Apple iMac 8", {
	  DMI_MATCH(DMI_BOARD_VENDOR, "Apple"),
	  DMI_MATCH(DMI_PRODUCT_NAME, "iMac8") },
		&applesmc_dmi_data[13]},
1605 1606 1607 1608
	{ applesmc_dmi_match, "Apple iMac 6", {
	  DMI_MATCH(DMI_BOARD_VENDOR, "Apple"),
	  DMI_MATCH(DMI_PRODUCT_NAME, "iMac6") },
		&applesmc_dmi_data[14]},
1609 1610 1611 1612
	{ applesmc_dmi_match, "Apple iMac 5", {
	  DMI_MATCH(DMI_BOARD_VENDOR, "Apple"),
	  DMI_MATCH(DMI_PRODUCT_NAME, "iMac5") },
		&applesmc_dmi_data[10]},
1613 1614 1615
	{ applesmc_dmi_match, "Apple iMac", {
	  DMI_MATCH(DMI_BOARD_VENDOR,"Apple"),
	  DMI_MATCH(DMI_PRODUCT_NAME,"iMac") },
1616
		&applesmc_dmi_data[5]},
1617 1618 1619 1620 1621 1622 1623 1624 1625 1626
	{ .ident = NULL }
};

static int __init applesmc_init(void)
{
	int ret;
	int count;
	int i;

	if (!dmi_check_system(applesmc_whitelist)) {
1627
		pr_warn("supported laptop not found!\n");
1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641
		ret = -ENODEV;
		goto out;
	}

	if (!request_region(APPLESMC_DATA_PORT, APPLESMC_NR_PORTS,
								"applesmc")) {
		ret = -ENXIO;
		goto out;
	}

	ret = platform_driver_register(&applesmc_driver);
	if (ret)
		goto out_region;

1642 1643
	pdev = platform_device_register_simple("applesmc", APPLESMC_DATA_PORT,
					       NULL, 0);
1644 1645 1646 1647 1648
	if (IS_ERR(pdev)) {
		ret = PTR_ERR(pdev);
		goto out_driver;
	}

1649
	ret = sysfs_create_file(&pdev->dev.kobj, &dev_attr_name.attr);
1650 1651
	if (ret)
		goto out_device;
1652

1653 1654 1655
	/* Create key enumeration sysfs files */
	ret = sysfs_create_group(&pdev->dev.kobj, &key_enumeration_group);
	if (ret)
1656
		goto out_name;
1657 1658 1659

	/* create fan files */
	count = applesmc_get_fan_count();
1660
	if (count < 0)
1661
		pr_err("Cannot get the number of fans\n");
1662
	else
1663
		pr_info("%d fans found\n", count);
1664

1665 1666
	if (count > 4) {
		count = 4;
1667
		pr_warn("A maximum of 4 fans are supported by this driver\n");
1668 1669 1670 1671 1672 1673 1674 1675
	}

	while (fans_handled < count) {
		ret = sysfs_create_group(&pdev->dev.kobj,
					 &fan_attribute_groups[fans_handled]);
		if (ret)
			goto out_fans;
		fans_handled++;
1676 1677 1678 1679 1680
	}

	for (i = 0;
	     temperature_sensors_sets[applesmc_temperature_set][i] != NULL;
	     i++) {
1681 1682
		if (temperature_attributes[i] == NULL ||
		    label_attributes[i] == NULL) {
1683 1684
			pr_err("More temperature sensors in temperature_sensors_sets (at least %i) than available sysfs files in temperature_attributes (%i), please report this bug\n",
			       i, i-1);
1685 1686 1687 1688 1689 1690
			goto out_temperature;
		}
		ret = sysfs_create_file(&pdev->dev.kobj,
						temperature_attributes[i]);
		if (ret)
			goto out_temperature;
1691 1692 1693 1694
		ret = sysfs_create_file(&pdev->dev.kobj,
						label_attributes[i]);
		if (ret)
			goto out_temperature;
1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721
	}

	if (applesmc_accelerometer) {
		ret = applesmc_create_accelerometer();
		if (ret)
			goto out_temperature;
	}

	if (applesmc_light) {
		/* Add light sensor file */
		ret = sysfs_create_file(&pdev->dev.kobj, &dev_attr_light.attr);
		if (ret)
			goto out_accelerometer;

		/* Create the workqueue */
		applesmc_led_wq = create_singlethread_workqueue("applesmc-led");
		if (!applesmc_led_wq) {
			ret = -ENOMEM;
			goto out_light_sysfs;
		}

		/* register as a led device */
		ret = led_classdev_register(&pdev->dev, &applesmc_backlight);
		if (ret < 0)
			goto out_light_wq;
	}

1722 1723 1724
	hwmon_dev = hwmon_device_register(&pdev->dev);
	if (IS_ERR(hwmon_dev)) {
		ret = PTR_ERR(hwmon_dev);
1725 1726 1727
		goto out_light_ledclass;
	}

1728
	pr_info("driver successfully loaded\n");
1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744

	return 0;

out_light_ledclass:
	if (applesmc_light)
		led_classdev_unregister(&applesmc_backlight);
out_light_wq:
	if (applesmc_light)
		destroy_workqueue(applesmc_led_wq);
out_light_sysfs:
	if (applesmc_light)
		sysfs_remove_file(&pdev->dev.kobj, &dev_attr_light.attr);
out_accelerometer:
	if (applesmc_accelerometer)
		applesmc_release_accelerometer();
out_temperature:
1745
	sysfs_remove_group(&pdev->dev.kobj, &label_attributes_group);
1746
	sysfs_remove_group(&pdev->dev.kobj, &temperature_attributes_group);
1747 1748 1749 1750
out_fans:
	while (fans_handled)
		sysfs_remove_group(&pdev->dev.kobj,
				   &fan_attribute_groups[--fans_handled]);
1751
	sysfs_remove_group(&pdev->dev.kobj, &key_enumeration_group);
1752 1753
out_name:
	sysfs_remove_file(&pdev->dev.kobj, &dev_attr_name.attr);
1754 1755 1756 1757 1758 1759 1760
out_device:
	platform_device_unregister(pdev);
out_driver:
	platform_driver_unregister(&applesmc_driver);
out_region:
	release_region(APPLESMC_DATA_PORT, APPLESMC_NR_PORTS);
out:
1761
	pr_warn("driver init failed (ret=%d)!\n", ret);
1762 1763 1764 1765 1766
	return ret;
}

static void __exit applesmc_exit(void)
{
1767
	hwmon_device_unregister(hwmon_dev);
1768 1769 1770 1771 1772 1773 1774
	if (applesmc_light) {
		led_classdev_unregister(&applesmc_backlight);
		destroy_workqueue(applesmc_led_wq);
		sysfs_remove_file(&pdev->dev.kobj, &dev_attr_light.attr);
	}
	if (applesmc_accelerometer)
		applesmc_release_accelerometer();
1775
	sysfs_remove_group(&pdev->dev.kobj, &label_attributes_group);
1776
	sysfs_remove_group(&pdev->dev.kobj, &temperature_attributes_group);
1777 1778 1779
	while (fans_handled)
		sysfs_remove_group(&pdev->dev.kobj,
				   &fan_attribute_groups[--fans_handled]);
1780
	sysfs_remove_group(&pdev->dev.kobj, &key_enumeration_group);
1781
	sysfs_remove_file(&pdev->dev.kobj, &dev_attr_name.attr);
1782 1783 1784 1785
	platform_device_unregister(pdev);
	platform_driver_unregister(&applesmc_driver);
	release_region(APPLESMC_DATA_PORT, APPLESMC_NR_PORTS);

1786
	pr_info("driver unloaded\n");
1787 1788 1789 1790 1791 1792 1793 1794
}

module_init(applesmc_init);
module_exit(applesmc_exit);

MODULE_AUTHOR("Nicolas Boichat");
MODULE_DESCRIPTION("Apple SMC");
MODULE_LICENSE("GPL v2");
1795
MODULE_DEVICE_TABLE(dmi, applesmc_whitelist);