1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * drivers/hwmon/applesmc.c - driver for Apple's SMC (accelerometer, temperature
4 * sensors, fan control, keyboard backlight control) used in Intel-based Apple
5 * computers.
6 *
7 * Copyright (C) 2007 Nicolas Boichat <nicolas@boichat.ch>
8 * Copyright (C) 2010 Henrik Rydberg <rydberg@euromail.se>
9 *
10 * Based on hdaps.c driver:
11 * Copyright (C) 2005 Robert Love <rml@novell.com>
12 * Copyright (C) 2005 Jesper Juhl <jj@chaosbits.net>
13 *
14 * Fan control based on smcFanControl:
15 * Copyright (C) 2006 Hendrik Holtmann <holtmann@mac.com>
16 */
17
18 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
19
20 #include <linux/delay.h>
21 #include <linux/platform_device.h>
22 #include <linux/input.h>
23 #include <linux/kernel.h>
24 #include <linux/slab.h>
25 #include <linux/module.h>
26 #include <linux/timer.h>
27 #include <linux/dmi.h>
28 #include <linux/mutex.h>
29 #include <linux/hwmon-sysfs.h>
30 #include <linux/io.h>
31 #include <linux/leds.h>
32 #include <linux/hwmon.h>
33 #include <linux/workqueue.h>
34 #include <linux/err.h>
35 #include <linux/bits.h>
36 #include <asm/barrier.h>
37
38 /* data port used by Apple SMC */
39 #define APPLESMC_DATA_PORT 0x300
40 /* command/status port used by Apple SMC */
41 #define APPLESMC_CMD_PORT 0x304
42
43 #define APPLESMC_NR_PORTS 32 /* 0x300-0x31f */
44
45 #define APPLESMC_MAX_DATA_LENGTH 32
46
47 /* Apple SMC status bits */
48 #define SMC_STATUS_AWAITING_DATA BIT(0) /* SMC has data waiting to be read */
49 #define SMC_STATUS_IB_CLOSED BIT(1) /* Will ignore any input */
50 #define SMC_STATUS_BUSY BIT(2) /* Command in progress */
51
52 /* Initial wait is 8us */
53 #define APPLESMC_MIN_WAIT 0x0008
54
55 #define APPLESMC_READ_CMD 0x10
56 #define APPLESMC_WRITE_CMD 0x11
57 #define APPLESMC_GET_KEY_BY_INDEX_CMD 0x12
58 #define APPLESMC_GET_KEY_TYPE_CMD 0x13
59
60 #define KEY_COUNT_KEY "#KEY" /* r-o ui32 */
61
62 #define LIGHT_SENSOR_LEFT_KEY "ALV0" /* r-o {alv (6-10 bytes) */
63 #define LIGHT_SENSOR_RIGHT_KEY "ALV1" /* r-o {alv (6-10 bytes) */
64 #define BACKLIGHT_KEY "LKSB" /* w-o {lkb (2 bytes) */
65
66 #define CLAMSHELL_KEY "MSLD" /* r-o ui8 (unused) */
67
68 #define MOTION_SENSOR_X_KEY "MO_X" /* r-o sp78 (2 bytes) */
69 #define MOTION_SENSOR_Y_KEY "MO_Y" /* r-o sp78 (2 bytes) */
70 #define MOTION_SENSOR_Z_KEY "MO_Z" /* r-o sp78 (2 bytes) */
71 #define MOTION_SENSOR_KEY "MOCN" /* r/w ui16 */
72
73 #define FANS_COUNT "FNum" /* r-o ui8 */
74 #define FANS_MANUAL "FS! " /* r-w ui16 */
75 #define FAN_ID_FMT "F%dID" /* r-o char[16] */
76
77 #define TEMP_SENSOR_TYPE "sp78"
78
79 /* List of keys used to read/write fan speeds */
80 static const char *const fan_speed_fmt[] = {
81 "F%dAc", /* actual speed */
82 "F%dMn", /* minimum speed (rw) */
83 "F%dMx", /* maximum speed */
84 "F%dSf", /* safe speed - not all models */
85 "F%dTg", /* target speed (manual: rw) */
86 };
87
88 #define INIT_TIMEOUT_MSECS 5000 /* wait up to 5s for device init ... */
89 #define INIT_WAIT_MSECS 50 /* ... in 50ms increments */
90
91 #define APPLESMC_POLL_INTERVAL 50 /* msecs */
92 #define APPLESMC_INPUT_FUZZ 4 /* input event threshold */
93 #define APPLESMC_INPUT_FLAT 4
94
95 #define to_index(attr) (to_sensor_dev_attr(attr)->index & 0xffff)
96 #define to_option(attr) (to_sensor_dev_attr(attr)->index >> 16)
97
98 /* Dynamic device node attributes */
99 struct applesmc_dev_attr {
100 struct sensor_device_attribute sda; /* hwmon attributes */
101 char name[32]; /* room for node file name */
102 };
103
104 /* Dynamic device node group */
105 struct applesmc_node_group {
106 char *format; /* format string */
107 void *show; /* show function */
108 void *store; /* store function */
109 int option; /* function argument */
110 struct applesmc_dev_attr *nodes; /* dynamic node array */
111 };
112
113 /* AppleSMC entry - cached register information */
114 struct applesmc_entry {
115 char key[5]; /* four-letter key code */
116 u8 valid; /* set when entry is successfully read once */
117 u8 len; /* bounded by APPLESMC_MAX_DATA_LENGTH */
118 char type[5]; /* four-letter type code */
119 u8 flags; /* 0x10: func; 0x40: write; 0x80: read */
120 };
121
122 /* Register lookup and registers common to all SMCs */
123 static struct applesmc_registers {
124 struct mutex mutex; /* register read/write mutex */
125 unsigned int key_count; /* number of SMC registers */
126 unsigned int fan_count; /* number of fans */
127 unsigned int temp_count; /* number of temperature registers */
128 unsigned int temp_begin; /* temperature lower index bound */
129 unsigned int temp_end; /* temperature upper index bound */
130 unsigned int index_count; /* size of temperature index array */
131 int num_light_sensors; /* number of light sensors */
132 bool has_accelerometer; /* has motion sensor */
133 bool has_key_backlight; /* has keyboard backlight */
134 bool init_complete; /* true when fully initialized */
135 struct applesmc_entry *cache; /* cached key entries */
136 const char **index; /* temperature key index */
137 char fan_positions[10][17]; /* cached fan position labels */
138 } smcreg = {
139 .mutex = __MUTEX_INITIALIZER(smcreg.mutex),
140 };
141
142 static const int debug;
143 static struct platform_device *pdev;
144 static s16 rest_x;
145 static s16 rest_y;
146 static u8 backlight_state[2];
147
148 static struct input_dev *applesmc_idev;
149 static struct device *hwmon_dev;
150
151 /*
152 * Last index written to key_at_index sysfs file, and value to use for all other
153 * key_at_index_* sysfs files.
154 */
155 static unsigned int key_at_index;
156
157 static struct workqueue_struct *applesmc_led_wq;
158
159 /*
160 * Wait for specific status bits with a mask on the SMC.
161 * Used before all transactions.
162 * This does 10 fast loops of 8us then exponentially backs off for a
163 * minimum total wait of 262ms. Depending on usleep_range this could
164 * run out past 500ms.
165 */
166
wait_status(u8 val,u8 mask)167 static int wait_status(u8 val, u8 mask)
168 {
169 u8 status;
170 int us;
171 int i;
172
173 us = APPLESMC_MIN_WAIT;
174 for (i = 0; i < 24 ; i++) {
175 status = inb(APPLESMC_CMD_PORT);
176 if ((status & mask) == val)
177 return 0;
178 usleep_range(us, us * 2);
179 if (i > 9)
180 us <<= 1;
181 }
182 return -EIO;
183 }
184
185 /* send_byte - Write to SMC data port. Callers must hold applesmc_lock. */
186
send_byte(u8 cmd,u16 port)187 static int send_byte(u8 cmd, u16 port)
188 {
189 int status;
190
191 status = wait_status(0, SMC_STATUS_IB_CLOSED);
192 if (status)
193 return status;
194 /*
195 * This needs to be a separate read looking for bit 0x04
196 * after bit 0x02 falls. If consolidated with the wait above
197 * this extra read may not happen if status returns both
198 * simultaneously and this would appear to be required.
199 */
200 status = wait_status(SMC_STATUS_BUSY, SMC_STATUS_BUSY);
201 if (status)
202 return status;
203
204 outb(cmd, port);
205 return 0;
206 }
207
208 /* send_command - Write a command to the SMC. Callers must hold applesmc_lock. */
209
send_command(u8 cmd)210 static int send_command(u8 cmd)
211 {
212 int ret;
213
214 ret = wait_status(0, SMC_STATUS_IB_CLOSED);
215 if (ret)
216 return ret;
217 outb(cmd, APPLESMC_CMD_PORT);
218 return 0;
219 }
220
221 /*
222 * Based on logic from the Apple driver. This is issued before any interaction
223 * If busy is stuck high, issue a read command to reset the SMC state machine.
224 * If busy is stuck high after the command then the SMC is jammed.
225 */
226
smc_sane(void)227 static int smc_sane(void)
228 {
229 int ret;
230
231 ret = wait_status(0, SMC_STATUS_BUSY);
232 if (!ret)
233 return ret;
234 ret = send_command(APPLESMC_READ_CMD);
235 if (ret)
236 return ret;
237 return wait_status(0, SMC_STATUS_BUSY);
238 }
239
send_argument(const char * key)240 static int send_argument(const char *key)
241 {
242 int i;
243
244 for (i = 0; i < 4; i++)
245 if (send_byte(key[i], APPLESMC_DATA_PORT))
246 return -EIO;
247 return 0;
248 }
249
read_smc(u8 cmd,const char * key,u8 * buffer,u8 len)250 static int read_smc(u8 cmd, const char *key, u8 *buffer, u8 len)
251 {
252 u8 status, data = 0;
253 int i;
254 int ret;
255
256 ret = smc_sane();
257 if (ret)
258 return ret;
259
260 if (send_command(cmd) || send_argument(key)) {
261 pr_warn("%.4s: read arg fail\n", key);
262 return -EIO;
263 }
264
265 /* This has no effect on newer (2012) SMCs */
266 if (send_byte(len, APPLESMC_DATA_PORT)) {
267 pr_warn("%.4s: read len fail\n", key);
268 return -EIO;
269 }
270
271 for (i = 0; i < len; i++) {
272 if (wait_status(SMC_STATUS_AWAITING_DATA | SMC_STATUS_BUSY,
273 SMC_STATUS_AWAITING_DATA | SMC_STATUS_BUSY)) {
274 pr_warn("%.4s: read data[%d] fail\n", key, i);
275 return -EIO;
276 }
277 buffer[i] = inb(APPLESMC_DATA_PORT);
278 }
279
280 /* Read the data port until bit0 is cleared */
281 for (i = 0; i < 16; i++) {
282 udelay(APPLESMC_MIN_WAIT);
283 status = inb(APPLESMC_CMD_PORT);
284 if (!(status & SMC_STATUS_AWAITING_DATA))
285 break;
286 data = inb(APPLESMC_DATA_PORT);
287 }
288 if (i)
289 pr_warn("flushed %d bytes, last value is: %d\n", i, data);
290
291 return wait_status(0, SMC_STATUS_BUSY);
292 }
293
write_smc(u8 cmd,const char * key,const u8 * buffer,u8 len)294 static int write_smc(u8 cmd, const char *key, const u8 *buffer, u8 len)
295 {
296 int i;
297 int ret;
298
299 ret = smc_sane();
300 if (ret)
301 return ret;
302
303 if (send_command(cmd) || send_argument(key)) {
304 pr_warn("%s: write arg fail\n", key);
305 return -EIO;
306 }
307
308 if (send_byte(len, APPLESMC_DATA_PORT)) {
309 pr_warn("%.4s: write len fail\n", key);
310 return -EIO;
311 }
312
313 for (i = 0; i < len; i++) {
314 if (send_byte(buffer[i], APPLESMC_DATA_PORT)) {
315 pr_warn("%s: write data fail\n", key);
316 return -EIO;
317 }
318 }
319
320 return wait_status(0, SMC_STATUS_BUSY);
321 }
322
read_register_count(unsigned int * count)323 static int read_register_count(unsigned int *count)
324 {
325 __be32 be;
326 int ret;
327
328 ret = read_smc(APPLESMC_READ_CMD, KEY_COUNT_KEY, (u8 *)&be, 4);
329 if (ret)
330 return ret;
331
332 *count = be32_to_cpu(be);
333 return 0;
334 }
335
336 /*
337 * Serialized I/O
338 *
339 * Returns zero on success or a negative error on failure.
340 * All functions below are concurrency safe - callers should NOT hold lock.
341 */
342
applesmc_read_entry(const struct applesmc_entry * entry,u8 * buf,u8 len)343 static int applesmc_read_entry(const struct applesmc_entry *entry,
344 u8 *buf, u8 len)
345 {
346 int ret;
347
348 if (entry->len != len)
349 return -EINVAL;
350 mutex_lock(&smcreg.mutex);
351 ret = read_smc(APPLESMC_READ_CMD, entry->key, buf, len);
352 mutex_unlock(&smcreg.mutex);
353
354 return ret;
355 }
356
applesmc_write_entry(const struct applesmc_entry * entry,const u8 * buf,u8 len)357 static int applesmc_write_entry(const struct applesmc_entry *entry,
358 const u8 *buf, u8 len)
359 {
360 int ret;
361
362 if (entry->len != len)
363 return -EINVAL;
364 mutex_lock(&smcreg.mutex);
365 ret = write_smc(APPLESMC_WRITE_CMD, entry->key, buf, len);
366 mutex_unlock(&smcreg.mutex);
367 return ret;
368 }
369
applesmc_get_entry_by_index(int index)370 static const struct applesmc_entry *applesmc_get_entry_by_index(int index)
371 {
372 struct applesmc_entry *cache = &smcreg.cache[index];
373 u8 key[4], info[6];
374 __be32 be;
375 int ret = 0;
376
377 /* Pairs with smp_store_release() to ensure cache contents are visible */
378 if (smp_load_acquire(&cache->valid))
379 return cache;
380
381 mutex_lock(&smcreg.mutex);
382
383 if (cache->valid)
384 goto out;
385 be = cpu_to_be32(index);
386 ret = read_smc(APPLESMC_GET_KEY_BY_INDEX_CMD, (u8 *)&be, key, 4);
387 if (ret)
388 goto out;
389 ret = read_smc(APPLESMC_GET_KEY_TYPE_CMD, key, info, 6);
390 if (ret)
391 goto out;
392
393 memcpy(cache->key, key, 4);
394 cache->len = info[0];
395 memcpy(cache->type, &info[1], 4);
396 cache->flags = info[5];
397 /* Pairs with smp_load_acquire() to commit cache contents before setting valid */
398 smp_store_release(&cache->valid, true);
399
400 out:
401 mutex_unlock(&smcreg.mutex);
402 if (ret)
403 return ERR_PTR(ret);
404 return cache;
405 }
406
applesmc_get_lower_bound(unsigned int * lo,const char * key)407 static int applesmc_get_lower_bound(unsigned int *lo, const char *key)
408 {
409 int begin = 0, end = smcreg.key_count;
410 const struct applesmc_entry *entry;
411
412 while (begin != end) {
413 int middle = begin + (end - begin) / 2;
414 entry = applesmc_get_entry_by_index(middle);
415 if (IS_ERR(entry)) {
416 *lo = 0;
417 return PTR_ERR(entry);
418 }
419 if (strcmp(entry->key, key) < 0)
420 begin = middle + 1;
421 else
422 end = middle;
423 }
424
425 *lo = begin;
426 return 0;
427 }
428
applesmc_get_upper_bound(unsigned int * hi,const char * key)429 static int applesmc_get_upper_bound(unsigned int *hi, const char *key)
430 {
431 int begin = 0, end = smcreg.key_count;
432 const struct applesmc_entry *entry;
433
434 while (begin != end) {
435 int middle = begin + (end - begin) / 2;
436 entry = applesmc_get_entry_by_index(middle);
437 if (IS_ERR(entry)) {
438 *hi = smcreg.key_count;
439 return PTR_ERR(entry);
440 }
441 if (strcmp(key, entry->key) < 0)
442 end = middle;
443 else
444 begin = middle + 1;
445 }
446
447 *hi = begin;
448 return 0;
449 }
450
applesmc_get_entry_by_key(const char * key)451 static const struct applesmc_entry *applesmc_get_entry_by_key(const char *key)
452 {
453 int begin, end;
454 int ret;
455
456 ret = applesmc_get_lower_bound(&begin, key);
457 if (ret)
458 return ERR_PTR(ret);
459 ret = applesmc_get_upper_bound(&end, key);
460 if (ret)
461 return ERR_PTR(ret);
462 if (end - begin != 1)
463 return ERR_PTR(-EINVAL);
464
465 return applesmc_get_entry_by_index(begin);
466 }
467
applesmc_read_key(const char * key,u8 * buffer,u8 len)468 static int applesmc_read_key(const char *key, u8 *buffer, u8 len)
469 {
470 const struct applesmc_entry *entry;
471
472 entry = applesmc_get_entry_by_key(key);
473 if (IS_ERR(entry))
474 return PTR_ERR(entry);
475
476 return applesmc_read_entry(entry, buffer, len);
477 }
478
applesmc_write_key(const char * key,const u8 * buffer,u8 len)479 static int applesmc_write_key(const char *key, const u8 *buffer, u8 len)
480 {
481 const struct applesmc_entry *entry;
482
483 entry = applesmc_get_entry_by_key(key);
484 if (IS_ERR(entry))
485 return PTR_ERR(entry);
486
487 return applesmc_write_entry(entry, buffer, len);
488 }
489
applesmc_has_key(const char * key,bool * value)490 static int applesmc_has_key(const char *key, bool *value)
491 {
492 const struct applesmc_entry *entry;
493
494 entry = applesmc_get_entry_by_key(key);
495 if (IS_ERR(entry) && PTR_ERR(entry) != -EINVAL)
496 return PTR_ERR(entry);
497
498 *value = !IS_ERR(entry);
499 return 0;
500 }
501
502 /*
503 * applesmc_read_s16 - Read 16-bit signed big endian register
504 */
applesmc_read_s16(const char * key,s16 * value)505 static int applesmc_read_s16(const char *key, s16 *value)
506 {
507 u8 buffer[2];
508 int ret;
509
510 ret = applesmc_read_key(key, buffer, 2);
511 if (ret)
512 return ret;
513
514 *value = ((s16)buffer[0] << 8) | buffer[1];
515 return 0;
516 }
517
518 /*
519 * applesmc_device_init - initialize the accelerometer. Can sleep.
520 */
applesmc_device_init(void)521 static void applesmc_device_init(void)
522 {
523 int total;
524 u8 buffer[2];
525
526 if (!smcreg.has_accelerometer)
527 return;
528
529 for (total = INIT_TIMEOUT_MSECS; total > 0; total -= INIT_WAIT_MSECS) {
530 if (!applesmc_read_key(MOTION_SENSOR_KEY, buffer, 2) &&
531 (buffer[0] != 0x00 || buffer[1] != 0x00))
532 return;
533 buffer[0] = 0xe0;
534 buffer[1] = 0x00;
535 applesmc_write_key(MOTION_SENSOR_KEY, buffer, 2);
536 msleep(INIT_WAIT_MSECS);
537 }
538
539 pr_warn("failed to init the device\n");
540 }
541
applesmc_init_index(struct applesmc_registers * s)542 static int applesmc_init_index(struct applesmc_registers *s)
543 {
544 const struct applesmc_entry *entry;
545 unsigned int i;
546
547 if (s->index)
548 return 0;
549
550 s->index = kcalloc(s->temp_count, sizeof(s->index[0]), GFP_KERNEL);
551 if (!s->index)
552 return -ENOMEM;
553
554 for (i = s->temp_begin; i < s->temp_end; i++) {
555 entry = applesmc_get_entry_by_index(i);
556 if (IS_ERR(entry))
557 continue;
558 if (strcmp(entry->type, TEMP_SENSOR_TYPE))
559 continue;
560 s->index[s->index_count++] = entry->key;
561 }
562
563 return 0;
564 }
565
566 /*
567 * applesmc_init_smcreg_try - Try to initialize register cache. Idempotent.
568 */
applesmc_init_smcreg_try(void)569 static int applesmc_init_smcreg_try(void)
570 {
571 struct applesmc_registers *s = &smcreg;
572 bool left_light_sensor = false, right_light_sensor = false;
573 unsigned int count, i;
574 u8 tmp[1];
575 int ret;
576
577 if (s->init_complete)
578 return 0;
579
580 ret = read_register_count(&count);
581 if (ret)
582 return ret;
583
584 if (s->cache && s->key_count != count) {
585 pr_warn("key count changed from %d to %d\n",
586 s->key_count, count);
587 kfree(s->cache);
588 s->cache = NULL;
589 }
590 s->key_count = count;
591
592 if (!s->cache)
593 s->cache = kzalloc_objs(*s->cache, s->key_count);
594 if (!s->cache)
595 return -ENOMEM;
596
597 ret = applesmc_read_key(FANS_COUNT, tmp, 1);
598 if (ret)
599 return ret;
600 s->fan_count = tmp[0];
601 if (s->fan_count > 10)
602 s->fan_count = 10;
603
604 for (i = 0; i < s->fan_count; i++) {
605 char newkey[5];
606
607 scnprintf(newkey, sizeof(newkey), FAN_ID_FMT, i);
608 ret = applesmc_read_key(newkey, s->fan_positions[i], 16);
609 s->fan_positions[i][16] = 0;
610 if (ret)
611 scnprintf(s->fan_positions[i], 17, " Fan %d", i);
612 }
613
614 ret = applesmc_get_lower_bound(&s->temp_begin, "T");
615 if (ret)
616 return ret;
617 ret = applesmc_get_lower_bound(&s->temp_end, "U");
618 if (ret)
619 return ret;
620 s->temp_count = s->temp_end - s->temp_begin;
621
622 ret = applesmc_init_index(s);
623 if (ret)
624 return ret;
625
626 ret = applesmc_has_key(LIGHT_SENSOR_LEFT_KEY, &left_light_sensor);
627 if (ret)
628 return ret;
629 ret = applesmc_has_key(LIGHT_SENSOR_RIGHT_KEY, &right_light_sensor);
630 if (ret)
631 return ret;
632 ret = applesmc_has_key(MOTION_SENSOR_KEY, &s->has_accelerometer);
633 if (ret)
634 return ret;
635 ret = applesmc_has_key(BACKLIGHT_KEY, &s->has_key_backlight);
636 if (ret)
637 return ret;
638
639 s->num_light_sensors = left_light_sensor + right_light_sensor;
640 s->init_complete = true;
641
642 pr_info("key=%d fan=%d temp=%d index=%d acc=%d lux=%d kbd=%d\n",
643 s->key_count, s->fan_count, s->temp_count, s->index_count,
644 s->has_accelerometer,
645 s->num_light_sensors,
646 s->has_key_backlight);
647
648 return 0;
649 }
650
applesmc_destroy_smcreg(void)651 static void applesmc_destroy_smcreg(void)
652 {
653 kfree(smcreg.index);
654 smcreg.index = NULL;
655 kfree(smcreg.cache);
656 smcreg.cache = NULL;
657 smcreg.init_complete = false;
658 }
659
660 /*
661 * applesmc_init_smcreg - Initialize register cache.
662 *
663 * Retries until initialization is successful, or the operation times out.
664 *
665 */
applesmc_init_smcreg(void)666 static int applesmc_init_smcreg(void)
667 {
668 int ms, ret;
669
670 for (ms = 0; ms < INIT_TIMEOUT_MSECS; ms += INIT_WAIT_MSECS) {
671 ret = applesmc_init_smcreg_try();
672 if (!ret) {
673 if (ms)
674 pr_info("init_smcreg() took %d ms\n", ms);
675 return 0;
676 }
677 msleep(INIT_WAIT_MSECS);
678 }
679
680 applesmc_destroy_smcreg();
681
682 return ret;
683 }
684
685 /* Device model stuff */
applesmc_probe(struct platform_device * dev)686 static int applesmc_probe(struct platform_device *dev)
687 {
688 int ret;
689
690 ret = applesmc_init_smcreg();
691 if (ret)
692 return ret;
693
694 applesmc_device_init();
695
696 return 0;
697 }
698
699 /* Synchronize device with memorized backlight state */
applesmc_pm_resume(struct device * dev)700 static int applesmc_pm_resume(struct device *dev)
701 {
702 if (smcreg.has_key_backlight)
703 applesmc_write_key(BACKLIGHT_KEY, backlight_state, 2);
704 return 0;
705 }
706
707 /* Reinitialize device on resume from hibernation */
applesmc_pm_restore(struct device * dev)708 static int applesmc_pm_restore(struct device *dev)
709 {
710 applesmc_device_init();
711 return applesmc_pm_resume(dev);
712 }
713
714 static const struct dev_pm_ops applesmc_pm_ops = {
715 .resume = applesmc_pm_resume,
716 .restore = applesmc_pm_restore,
717 };
718
719 static struct platform_driver applesmc_driver = {
720 .probe = applesmc_probe,
721 .driver = {
722 .name = "applesmc",
723 .pm = &applesmc_pm_ops,
724 },
725 };
726
727 /*
728 * applesmc_calibrate - Set our "resting" values. Callers must
729 * hold applesmc_lock.
730 */
applesmc_calibrate(void)731 static void applesmc_calibrate(void)
732 {
733 applesmc_read_s16(MOTION_SENSOR_X_KEY, &rest_x);
734 applesmc_read_s16(MOTION_SENSOR_Y_KEY, &rest_y);
735 rest_x = -rest_x;
736 }
737
applesmc_idev_poll(struct input_dev * idev)738 static void applesmc_idev_poll(struct input_dev *idev)
739 {
740 s16 x, y;
741
742 if (applesmc_read_s16(MOTION_SENSOR_X_KEY, &x))
743 return;
744 if (applesmc_read_s16(MOTION_SENSOR_Y_KEY, &y))
745 return;
746
747 x = -x;
748 input_report_abs(idev, ABS_X, x - rest_x);
749 input_report_abs(idev, ABS_Y, y - rest_y);
750 input_sync(idev);
751 }
752
753 /* Sysfs Files */
754
applesmc_name_show(struct device * dev,struct device_attribute * attr,char * buf)755 static ssize_t applesmc_name_show(struct device *dev,
756 struct device_attribute *attr, char *buf)
757 {
758 return sysfs_emit(buf, "applesmc\n");
759 }
760
applesmc_position_show(struct device * dev,struct device_attribute * attr,char * buf)761 static ssize_t applesmc_position_show(struct device *dev,
762 struct device_attribute *attr, char *buf)
763 {
764 int ret;
765 s16 x, y, z;
766
767 ret = applesmc_read_s16(MOTION_SENSOR_X_KEY, &x);
768 if (ret)
769 goto out;
770 ret = applesmc_read_s16(MOTION_SENSOR_Y_KEY, &y);
771 if (ret)
772 goto out;
773 ret = applesmc_read_s16(MOTION_SENSOR_Z_KEY, &z);
774 if (ret)
775 goto out;
776
777 out:
778 if (ret)
779 return ret;
780
781 return sysfs_emit(buf, "(%d,%d,%d)\n", x, y, z);
782 }
783
applesmc_light_show(struct device * dev,struct device_attribute * attr,char * sysfsbuf)784 static ssize_t applesmc_light_show(struct device *dev,
785 struct device_attribute *attr, char *sysfsbuf)
786 {
787 const struct applesmc_entry *entry;
788 static int data_length;
789 int ret;
790 u8 left = 0, right = 0;
791 u8 buffer[10];
792
793 if (!data_length) {
794 entry = applesmc_get_entry_by_key(LIGHT_SENSOR_LEFT_KEY);
795 if (IS_ERR(entry))
796 return PTR_ERR(entry);
797 if (entry->len > 10)
798 return -ENXIO;
799 data_length = entry->len;
800 pr_info("light sensor data length set to %d\n", data_length);
801 }
802
803 ret = applesmc_read_key(LIGHT_SENSOR_LEFT_KEY, buffer, data_length);
804 if (ret)
805 goto out;
806 /* newer macbooks report a single 10-bit bigendian value */
807 if (data_length == 10) {
808 left = be16_to_cpu(*(__be16 *)(buffer + 6)) >> 2;
809 goto out;
810 }
811 left = buffer[2];
812
813 ret = applesmc_read_key(LIGHT_SENSOR_RIGHT_KEY, buffer, data_length);
814 if (ret)
815 goto out;
816 right = buffer[2];
817
818 out:
819 if (ret)
820 return ret;
821
822 return sysfs_emit(sysfsbuf, "(%d,%d)\n", left, right);
823 }
824
applesmc_show_fan_speed(struct device * dev,struct device_attribute * attr,char * sysfsbuf)825 static ssize_t applesmc_show_fan_speed(struct device *dev,
826 struct device_attribute *attr, char *sysfsbuf)
827 {
828 int ret;
829 unsigned int speed = 0;
830 char newkey[5];
831 u8 buffer[2];
832
833 scnprintf(newkey, sizeof(newkey), fan_speed_fmt[to_option(attr)],
834 to_index(attr));
835
836 ret = applesmc_read_key(newkey, buffer, 2);
837 if (ret)
838 return ret;
839
840 speed = ((buffer[0] << 8 | buffer[1]) >> 2);
841 return sysfs_emit(sysfsbuf, "%u\n", speed);
842 }
843
applesmc_calibrate_show(struct device * dev,struct device_attribute * attr,char * sysfsbuf)844 static ssize_t applesmc_calibrate_show(struct device *dev,
845 struct device_attribute *attr, char *sysfsbuf)
846 {
847 return sysfs_emit(sysfsbuf, "(%d,%d)\n", rest_x, rest_y);
848 }
849
applesmc_calibrate_store(struct device * dev,struct device_attribute * attr,const char * sysfsbuf,size_t count)850 static ssize_t applesmc_calibrate_store(struct device *dev,
851 struct device_attribute *attr, const char *sysfsbuf, size_t count)
852 {
853 applesmc_calibrate();
854
855 return count;
856 }
857
applesmc_backlight_set(struct work_struct * work)858 static void applesmc_backlight_set(struct work_struct *work)
859 {
860 applesmc_write_key(BACKLIGHT_KEY, backlight_state, 2);
861 }
862 static DECLARE_WORK(backlight_work, &applesmc_backlight_set);
863
applesmc_brightness_set(struct led_classdev * led_cdev,enum led_brightness value)864 static void applesmc_brightness_set(struct led_classdev *led_cdev,
865 enum led_brightness value)
866 {
867 int ret;
868
869 backlight_state[0] = value;
870 ret = queue_work(applesmc_led_wq, &backlight_work);
871
872 if (debug && (!ret))
873 dev_dbg(led_cdev->dev, "work was already on the queue.\n");
874 }
875
applesmc_key_count_show(struct device * dev,struct device_attribute * attr,char * sysfsbuf)876 static ssize_t applesmc_key_count_show(struct device *dev,
877 struct device_attribute *attr, char *sysfsbuf)
878 {
879 int ret;
880 u8 buffer[4];
881 u32 count;
882
883 ret = applesmc_read_key(KEY_COUNT_KEY, buffer, 4);
884 if (ret)
885 return ret;
886
887 count = ((u32)buffer[0]<<24) + ((u32)buffer[1]<<16) +
888 ((u32)buffer[2]<<8) + buffer[3];
889 return sysfs_emit(sysfsbuf, "%d\n", count);
890 }
891
applesmc_key_at_index_read_show(struct device * dev,struct device_attribute * attr,char * sysfsbuf)892 static ssize_t applesmc_key_at_index_read_show(struct device *dev,
893 struct device_attribute *attr, char *sysfsbuf)
894 {
895 const struct applesmc_entry *entry;
896 int ret;
897
898 entry = applesmc_get_entry_by_index(key_at_index);
899 if (IS_ERR(entry))
900 return PTR_ERR(entry);
901 ret = applesmc_read_entry(entry, sysfsbuf, entry->len);
902 if (ret)
903 return ret;
904
905 return entry->len;
906 }
907
applesmc_key_at_index_data_length_show(struct device * dev,struct device_attribute * attr,char * sysfsbuf)908 static ssize_t applesmc_key_at_index_data_length_show(struct device *dev,
909 struct device_attribute *attr, char *sysfsbuf)
910 {
911 const struct applesmc_entry *entry;
912
913 entry = applesmc_get_entry_by_index(key_at_index);
914 if (IS_ERR(entry))
915 return PTR_ERR(entry);
916
917 return sysfs_emit(sysfsbuf, "%d\n", entry->len);
918 }
919
applesmc_key_at_index_type_show(struct device * dev,struct device_attribute * attr,char * sysfsbuf)920 static ssize_t applesmc_key_at_index_type_show(struct device *dev,
921 struct device_attribute *attr, char *sysfsbuf)
922 {
923 const struct applesmc_entry *entry;
924
925 entry = applesmc_get_entry_by_index(key_at_index);
926 if (IS_ERR(entry))
927 return PTR_ERR(entry);
928
929 return sysfs_emit(sysfsbuf, "%s\n", entry->type);
930 }
931
applesmc_key_at_index_name_show(struct device * dev,struct device_attribute * attr,char * sysfsbuf)932 static ssize_t applesmc_key_at_index_name_show(struct device *dev,
933 struct device_attribute *attr, char *sysfsbuf)
934 {
935 const struct applesmc_entry *entry;
936
937 entry = applesmc_get_entry_by_index(key_at_index);
938 if (IS_ERR(entry))
939 return PTR_ERR(entry);
940
941 return sysfs_emit(sysfsbuf, "%s\n", entry->key);
942 }
943
applesmc_key_at_index_show(struct device * dev,struct device_attribute * attr,char * sysfsbuf)944 static ssize_t applesmc_key_at_index_show(struct device *dev,
945 struct device_attribute *attr, char *sysfsbuf)
946 {
947 return sysfs_emit(sysfsbuf, "%d\n", key_at_index);
948 }
949
applesmc_key_at_index_store(struct device * dev,struct device_attribute * attr,const char * sysfsbuf,size_t count)950 static ssize_t applesmc_key_at_index_store(struct device *dev,
951 struct device_attribute *attr, const char *sysfsbuf, size_t count)
952 {
953 unsigned long newkey;
954
955 if (kstrtoul(sysfsbuf, 10, &newkey) < 0
956 || newkey >= smcreg.key_count)
957 return -EINVAL;
958
959 key_at_index = newkey;
960 return count;
961 }
962
963 static struct led_classdev applesmc_backlight = {
964 .name = "smc::kbd_backlight",
965 .default_trigger = "nand-disk",
966 .brightness_set = applesmc_brightness_set,
967 };
968
969 static struct applesmc_node_group info_group[] = {
970 { "name", applesmc_name_show },
971 { "key_count", applesmc_key_count_show },
972 { "key_at_index", applesmc_key_at_index_show, applesmc_key_at_index_store },
973 { "key_at_index_name", applesmc_key_at_index_name_show },
974 { "key_at_index_type", applesmc_key_at_index_type_show },
975 { "key_at_index_data_length", applesmc_key_at_index_data_length_show },
976 { "key_at_index_data", applesmc_key_at_index_read_show },
977 { }
978 };
979
980 static struct applesmc_node_group accelerometer_group[] = {
981 { "position", applesmc_position_show },
982 { "calibrate", applesmc_calibrate_show, applesmc_calibrate_store },
983 { }
984 };
985
986 static struct applesmc_node_group light_sensor_group[] = {
987 { "light", applesmc_light_show },
988 { }
989 };
990
991
992
993 /* Module stuff */
994
995 /*
996 * applesmc_destroy_nodes - remove files and free associated memory
997 */
applesmc_destroy_nodes(struct applesmc_node_group * groups)998 static void applesmc_destroy_nodes(struct applesmc_node_group *groups)
999 {
1000 struct applesmc_node_group *grp;
1001 struct applesmc_dev_attr *node;
1002
1003 for (grp = groups; grp->nodes; grp++) {
1004 for (node = grp->nodes; node->sda.dev_attr.attr.name; node++)
1005 sysfs_remove_file(&pdev->dev.kobj,
1006 &node->sda.dev_attr.attr);
1007 kfree(grp->nodes);
1008 grp->nodes = NULL;
1009 }
1010 }
1011
1012 /*
1013 * applesmc_create_nodes - create a two-dimensional group of sysfs files
1014 */
applesmc_create_nodes(struct applesmc_node_group * groups,int num)1015 static int applesmc_create_nodes(struct applesmc_node_group *groups, int num)
1016 {
1017 struct applesmc_node_group *grp;
1018 struct applesmc_dev_attr *node;
1019 struct attribute *attr;
1020 int ret, i;
1021
1022 for (grp = groups; grp->format; grp++) {
1023 grp->nodes = kzalloc_objs(*node, num + 1);
1024 if (!grp->nodes) {
1025 ret = -ENOMEM;
1026 goto out;
1027 }
1028 for (i = 0; i < num; i++) {
1029 node = &grp->nodes[i];
1030 scnprintf(node->name, sizeof(node->name), grp->format,
1031 i + 1);
1032 node->sda.index = (grp->option << 16) | (i & 0xffff);
1033 node->sda.dev_attr.show = grp->show;
1034 node->sda.dev_attr.store = grp->store;
1035 attr = &node->sda.dev_attr.attr;
1036 sysfs_attr_init(attr);
1037 attr->name = node->name;
1038 attr->mode = 0444 | (grp->store ? 0200 : 0);
1039 ret = sysfs_create_file(&pdev->dev.kobj, attr);
1040 if (ret) {
1041 attr->name = NULL;
1042 goto out;
1043 }
1044 }
1045 }
1046
1047 return 0;
1048 out:
1049 applesmc_destroy_nodes(groups);
1050 return ret;
1051 }
1052
1053 /* Create accelerometer resources */
applesmc_create_accelerometer(void)1054 static int applesmc_create_accelerometer(void)
1055 {
1056 int ret;
1057
1058 if (!smcreg.has_accelerometer)
1059 return 0;
1060
1061 ret = applesmc_create_nodes(accelerometer_group, 1);
1062 if (ret)
1063 goto out;
1064
1065 applesmc_idev = input_allocate_device();
1066 if (!applesmc_idev) {
1067 ret = -ENOMEM;
1068 goto out_sysfs;
1069 }
1070
1071 /* initial calibrate for the input device */
1072 applesmc_calibrate();
1073
1074 /* initialize the input device */
1075 applesmc_idev->name = "applesmc";
1076 applesmc_idev->id.bustype = BUS_HOST;
1077 applesmc_idev->dev.parent = &pdev->dev;
1078 input_set_abs_params(applesmc_idev, ABS_X,
1079 -256, 256, APPLESMC_INPUT_FUZZ, APPLESMC_INPUT_FLAT);
1080 input_set_abs_params(applesmc_idev, ABS_Y,
1081 -256, 256, APPLESMC_INPUT_FUZZ, APPLESMC_INPUT_FLAT);
1082
1083 ret = input_setup_polling(applesmc_idev, applesmc_idev_poll);
1084 if (ret)
1085 goto out_idev;
1086
1087 input_set_poll_interval(applesmc_idev, APPLESMC_POLL_INTERVAL);
1088
1089 ret = input_register_device(applesmc_idev);
1090 if (ret)
1091 goto out_idev;
1092
1093 return 0;
1094
1095 out_idev:
1096 input_free_device(applesmc_idev);
1097
1098 out_sysfs:
1099 applesmc_destroy_nodes(accelerometer_group);
1100
1101 out:
1102 pr_warn("driver init failed (ret=%d)!\n", ret);
1103 return ret;
1104 }
1105
1106 /* Release all resources used by the accelerometer */
applesmc_release_accelerometer(void)1107 static void applesmc_release_accelerometer(void)
1108 {
1109 if (!smcreg.has_accelerometer)
1110 return;
1111 input_unregister_device(applesmc_idev);
1112 applesmc_destroy_nodes(accelerometer_group);
1113 }
1114
applesmc_create_light_sensor(void)1115 static int applesmc_create_light_sensor(void)
1116 {
1117 if (!smcreg.num_light_sensors)
1118 return 0;
1119 return applesmc_create_nodes(light_sensor_group, 1);
1120 }
1121
applesmc_release_light_sensor(void)1122 static void applesmc_release_light_sensor(void)
1123 {
1124 if (!smcreg.num_light_sensors)
1125 return;
1126 applesmc_destroy_nodes(light_sensor_group);
1127 }
1128
applesmc_create_key_backlight(void)1129 static int applesmc_create_key_backlight(void)
1130 {
1131 if (!smcreg.has_key_backlight)
1132 return 0;
1133 applesmc_led_wq = create_singlethread_workqueue("applesmc-led");
1134 if (!applesmc_led_wq)
1135 return -ENOMEM;
1136 return led_classdev_register(&pdev->dev, &applesmc_backlight);
1137 }
1138
applesmc_release_key_backlight(void)1139 static void applesmc_release_key_backlight(void)
1140 {
1141 if (!smcreg.has_key_backlight)
1142 return;
1143 led_classdev_unregister(&applesmc_backlight);
1144 destroy_workqueue(applesmc_led_wq);
1145 }
1146
applesmc_dmi_match(const struct dmi_system_id * id)1147 static int applesmc_dmi_match(const struct dmi_system_id *id)
1148 {
1149 return 1;
1150 }
1151
1152 /*
1153 * Note that DMI_MATCH(...,"MacBook") will match "MacBookPro1,1".
1154 * So we need to put "Apple MacBook Pro" before "Apple MacBook".
1155 */
1156 static const struct dmi_system_id applesmc_whitelist[] __initconst = {
1157 { applesmc_dmi_match, "Apple MacBook Air", {
1158 DMI_MATCH(DMI_BOARD_VENDOR, "Apple"),
1159 DMI_MATCH(DMI_PRODUCT_NAME, "MacBookAir") },
1160 },
1161 { applesmc_dmi_match, "Apple MacBook Pro", {
1162 DMI_MATCH(DMI_BOARD_VENDOR, "Apple"),
1163 DMI_MATCH(DMI_PRODUCT_NAME, "MacBookPro") },
1164 },
1165 { applesmc_dmi_match, "Apple MacBook", {
1166 DMI_MATCH(DMI_BOARD_VENDOR, "Apple"),
1167 DMI_MATCH(DMI_PRODUCT_NAME, "MacBook") },
1168 },
1169 { applesmc_dmi_match, "Apple Macmini", {
1170 DMI_MATCH(DMI_BOARD_VENDOR, "Apple"),
1171 DMI_MATCH(DMI_PRODUCT_NAME, "Macmini") },
1172 },
1173 { applesmc_dmi_match, "Apple MacPro", {
1174 DMI_MATCH(DMI_BOARD_VENDOR, "Apple"),
1175 DMI_MATCH(DMI_PRODUCT_NAME, "MacPro") },
1176 },
1177 { applesmc_dmi_match, "Apple iMac", {
1178 DMI_MATCH(DMI_BOARD_VENDOR, "Apple"),
1179 DMI_MATCH(DMI_PRODUCT_NAME, "iMac") },
1180 },
1181 { applesmc_dmi_match, "Apple Xserve", {
1182 DMI_MATCH(DMI_BOARD_VENDOR, "Apple"),
1183 DMI_MATCH(DMI_PRODUCT_NAME, "Xserve") },
1184 },
1185 { .ident = NULL }
1186 };
1187 MODULE_DEVICE_TABLE(dmi, applesmc_whitelist);
1188
1189 static struct applesmc_dev_attr *fan_safe_attrs;
1190 static struct attribute **fan_safe_attr_list;
1191 static struct attribute_group fan_safe_group;
1192 static const struct attribute_group *applesmc_extra_groups[2];
1193
1194 static u32 *applesmc_temp_config;
1195 static u32 *applesmc_fan_config;
1196 static u32 *applesmc_pwm_config;
1197 static struct hwmon_channel_info *applesmc_info_temp;
1198 static struct hwmon_channel_info *applesmc_info_fan;
1199 static struct hwmon_channel_info *applesmc_info_pwm;
1200 static const struct hwmon_channel_info **applesmc_info_arr;
1201 static struct hwmon_chip_info *applesmc_chip;
1202
applesmc_free_hwmon(void)1203 static void applesmc_free_hwmon(void)
1204 {
1205 kfree(applesmc_temp_config);
1206 kfree(applesmc_fan_config);
1207 kfree(applesmc_pwm_config);
1208 kfree(applesmc_info_temp);
1209 kfree(applesmc_info_fan);
1210 kfree(applesmc_info_pwm);
1211 kfree(applesmc_info_arr);
1212 kfree(applesmc_chip);
1213 kfree(fan_safe_attrs);
1214 kfree(fan_safe_attr_list);
1215 }
1216
applesmc_hwmon_is_visible(const void * drvdata,enum hwmon_sensor_types type,u32 attr,int channel)1217 static umode_t applesmc_hwmon_is_visible(const void *drvdata, enum hwmon_sensor_types type,
1218 u32 attr, int channel)
1219 {
1220 switch (type) {
1221 case hwmon_temp:
1222 if (attr == hwmon_temp_input || attr == hwmon_temp_label)
1223 return 0444;
1224 break;
1225 case hwmon_fan:
1226 switch (attr) {
1227 case hwmon_fan_input:
1228 case hwmon_fan_label:
1229 case hwmon_fan_max:
1230 return 0444;
1231 case hwmon_fan_min:
1232 case hwmon_fan_target:
1233 return 0644;
1234 default:
1235 break;
1236 }
1237 break;
1238 case hwmon_pwm:
1239 if (attr == hwmon_pwm_enable)
1240 return 0644;
1241 break;
1242 default:
1243 break;
1244 }
1245 return 0;
1246 }
1247
applesmc_hwmon_read(struct device * dev,enum hwmon_sensor_types type,u32 attr,int channel,long * val)1248 static int applesmc_hwmon_read(struct device *dev, enum hwmon_sensor_types type,
1249 u32 attr, int channel, long *val)
1250 {
1251 int ret;
1252
1253 switch (type) {
1254 case hwmon_temp:
1255 if (attr == hwmon_temp_input) {
1256 const char *key = smcreg.index[channel];
1257 s16 value;
1258
1259 ret = applesmc_read_s16(key, &value);
1260 if (ret)
1261 return ret;
1262 *val = 250 * (value >> 6);
1263 return 0;
1264 }
1265 break;
1266 case hwmon_fan:
1267 switch (attr) {
1268 case hwmon_fan_input: {
1269 char key[5];
1270 u8 buffer[2];
1271
1272 scnprintf(key, sizeof(key), "F%dAc", channel);
1273 ret = applesmc_read_key(key, buffer, 2);
1274 if (ret)
1275 return ret;
1276 *val = ((buffer[0] << 8 | buffer[1]) >> 2);
1277 return 0;
1278 }
1279 case hwmon_fan_min: {
1280 char key[5];
1281 u8 buffer[2];
1282
1283 scnprintf(key, sizeof(key), "F%dMn", channel);
1284 ret = applesmc_read_key(key, buffer, 2);
1285 if (ret)
1286 return ret;
1287 *val = ((buffer[0] << 8 | buffer[1]) >> 2);
1288 return 0;
1289 }
1290 case hwmon_fan_max: {
1291 char key[5];
1292 u8 buffer[2];
1293
1294 scnprintf(key, sizeof(key), "F%dMx", channel);
1295 ret = applesmc_read_key(key, buffer, 2);
1296 if (ret)
1297 return ret;
1298 *val = ((buffer[0] << 8 | buffer[1]) >> 2);
1299 return 0;
1300 }
1301 case hwmon_fan_target: {
1302 char key[5];
1303 u8 buffer[2];
1304
1305 scnprintf(key, sizeof(key), "F%dTg", channel);
1306 ret = applesmc_read_key(key, buffer, 2);
1307 if (ret)
1308 return ret;
1309 *val = ((buffer[0] << 8 | buffer[1]) >> 2);
1310 return 0;
1311 }
1312 default:
1313 break;
1314 }
1315 break;
1316 case hwmon_pwm:
1317 if (attr == hwmon_pwm_enable) {
1318 u8 buffer[2];
1319
1320 ret = applesmc_read_key(FANS_MANUAL, buffer, 2);
1321 if (ret)
1322 return ret;
1323 *val = (((buffer[0] << 8 | buffer[1]) >> channel) & 0x01) ? 1 : 2;
1324 return 0;
1325 }
1326 break;
1327 default:
1328 break;
1329 }
1330 return -EOPNOTSUPP;
1331 }
1332
applesmc_hwmon_write(struct device * dev,enum hwmon_sensor_types type,u32 attr,int channel,long val)1333 static int applesmc_hwmon_write(struct device *dev, enum hwmon_sensor_types type,
1334 u32 attr, int channel, long val)
1335 {
1336 int ret;
1337
1338 switch (type) {
1339 case hwmon_fan:
1340 if (attr == hwmon_fan_min || attr == hwmon_fan_target) {
1341 char key[5];
1342 u8 buffer[2];
1343 const char *fmt = (attr == hwmon_fan_min) ? "F%dMn" : "F%dTg";
1344
1345 if (val < 0 || val >= 0x4000)
1346 return -EINVAL;
1347 scnprintf(key, sizeof(key), fmt, channel);
1348 buffer[0] = (val >> 6) & 0xff;
1349 buffer[1] = (val << 2) & 0xff;
1350 return applesmc_write_key(key, buffer, 2);
1351 }
1352 break;
1353 case hwmon_pwm:
1354 if (attr == hwmon_pwm_enable) {
1355 u8 buffer[2];
1356 u16 manual_val;
1357 const struct applesmc_entry *entry;
1358
1359 if (val != 1 && val != 2)
1360 return -EINVAL;
1361
1362 entry = applesmc_get_entry_by_key(FANS_MANUAL);
1363 if (IS_ERR(entry))
1364 return PTR_ERR(entry);
1365
1366 mutex_lock(&smcreg.mutex);
1367 ret = read_smc(APPLESMC_READ_CMD, entry->key, buffer, 2);
1368 if (ret)
1369 goto out_unlock;
1370 manual_val = (buffer[0] << 8 | buffer[1]);
1371 if (val == 1)
1372 manual_val |= (0x01 << channel);
1373 else
1374 manual_val &= ~(0x01 << channel);
1375 buffer[0] = (manual_val >> 8) & 0xff;
1376 buffer[1] = manual_val & 0xff;
1377 ret = write_smc(APPLESMC_WRITE_CMD, entry->key, buffer, 2);
1378 out_unlock:
1379 mutex_unlock(&smcreg.mutex);
1380 return ret;
1381 }
1382 break;
1383 default:
1384 break;
1385 }
1386 return -EOPNOTSUPP;
1387 }
1388
applesmc_hwmon_read_string(struct device * dev,enum hwmon_sensor_types type,u32 attr,int channel,const char ** str)1389 static int applesmc_hwmon_read_string(struct device *dev, enum hwmon_sensor_types type,
1390 u32 attr, int channel, const char **str)
1391 {
1392 switch (type) {
1393 case hwmon_temp:
1394 if (attr == hwmon_temp_label) {
1395 *str = smcreg.index[channel];
1396 return 0;
1397 }
1398 break;
1399 case hwmon_fan:
1400 if (attr == hwmon_fan_label) {
1401 *str = smcreg.fan_positions[channel] + 4;
1402 return 0;
1403 }
1404 break;
1405 default:
1406 break;
1407 }
1408 return -EOPNOTSUPP;
1409 }
1410
1411 static const struct hwmon_ops applesmc_hwmon_ops = {
1412 .is_visible = applesmc_hwmon_is_visible,
1413 .read = applesmc_hwmon_read,
1414 .write = applesmc_hwmon_write,
1415 .read_string = applesmc_hwmon_read_string,
1416 };
1417
applesmc_init(void)1418 static int __init applesmc_init(void)
1419 {
1420 int i;
1421 int ret;
1422
1423 if (!dmi_check_system(applesmc_whitelist)) {
1424 pr_warn("supported laptop not found!\n");
1425 ret = -ENODEV;
1426 goto out;
1427 }
1428
1429 if (!request_region(APPLESMC_DATA_PORT, APPLESMC_NR_PORTS,
1430 "applesmc")) {
1431 ret = -ENXIO;
1432 goto out;
1433 }
1434
1435 ret = platform_driver_register(&applesmc_driver);
1436 if (ret)
1437 goto out_region;
1438
1439 pdev = platform_device_register_simple("applesmc", APPLESMC_DATA_PORT,
1440 NULL, 0);
1441 if (IS_ERR(pdev)) {
1442 ret = PTR_ERR(pdev);
1443 goto out_driver;
1444 }
1445
1446 /* create register cache */
1447 ret = applesmc_init_smcreg();
1448 if (ret)
1449 goto out_device;
1450
1451 ret = applesmc_create_nodes(info_group, 1);
1452 if (ret)
1453 goto out_smcreg;
1454
1455 /* allocate hwmon channel configs */
1456 applesmc_temp_config = kcalloc(smcreg.index_count + 1,
1457 sizeof(*applesmc_temp_config), GFP_KERNEL);
1458 applesmc_fan_config = kcalloc(smcreg.fan_count + 1,
1459 sizeof(*applesmc_fan_config), GFP_KERNEL);
1460 applesmc_pwm_config = kcalloc(smcreg.fan_count + 1,
1461 sizeof(*applesmc_pwm_config), GFP_KERNEL);
1462 if (!applesmc_temp_config || !applesmc_fan_config || !applesmc_pwm_config) {
1463 ret = -ENOMEM;
1464 goto out_info;
1465 }
1466
1467 for (i = 0; i < smcreg.index_count; i++)
1468 applesmc_temp_config[i] = HWMON_T_INPUT | HWMON_T_LABEL;
1469 applesmc_temp_config[smcreg.index_count] = 0;
1470
1471 for (i = 0; i < smcreg.fan_count; i++) {
1472 applesmc_fan_config[i] = HWMON_F_INPUT | HWMON_F_LABEL | HWMON_F_MIN |
1473 HWMON_F_MAX | HWMON_F_TARGET;
1474 applesmc_pwm_config[i] = HWMON_PWM_ENABLE;
1475 }
1476 applesmc_fan_config[smcreg.fan_count] = 0;
1477 applesmc_pwm_config[smcreg.fan_count] = 0;
1478
1479 applesmc_info_temp = kzalloc_obj(*applesmc_info_temp);
1480 applesmc_info_fan = kzalloc_obj(*applesmc_info_fan);
1481 applesmc_info_pwm = kzalloc_obj(*applesmc_info_pwm);
1482 if (!applesmc_info_temp || !applesmc_info_fan || !applesmc_info_pwm) {
1483 ret = -ENOMEM;
1484 goto out_info;
1485 }
1486
1487 applesmc_info_temp->type = hwmon_temp;
1488 applesmc_info_temp->config = applesmc_temp_config;
1489
1490 applesmc_info_fan->type = hwmon_fan;
1491 applesmc_info_fan->config = applesmc_fan_config;
1492
1493 applesmc_info_pwm->type = hwmon_pwm;
1494 applesmc_info_pwm->config = applesmc_pwm_config;
1495
1496 applesmc_info_arr = kzalloc_objs(*applesmc_info_arr, 4);
1497 if (!applesmc_info_arr) {
1498 ret = -ENOMEM;
1499 goto out_info;
1500 }
1501
1502 applesmc_info_arr[0] = applesmc_info_temp;
1503 applesmc_info_arr[1] = applesmc_info_fan;
1504 applesmc_info_arr[2] = applesmc_info_pwm;
1505 applesmc_info_arr[3] = NULL;
1506
1507 applesmc_chip = kzalloc_obj(*applesmc_chip);
1508 if (!applesmc_chip) {
1509 ret = -ENOMEM;
1510 goto out_info;
1511 }
1512
1513 applesmc_chip->ops = &applesmc_hwmon_ops;
1514 applesmc_chip->info = applesmc_info_arr;
1515
1516 /* Create non-standard fanX_safe attributes group */
1517 fan_safe_attrs = kzalloc_objs(*fan_safe_attrs, smcreg.fan_count);
1518 fan_safe_attr_list = kzalloc_objs(*fan_safe_attr_list,
1519 smcreg.fan_count + 1);
1520 if (!fan_safe_attrs || !fan_safe_attr_list) {
1521 ret = -ENOMEM;
1522 goto out_info;
1523 }
1524
1525 for (i = 0; i < smcreg.fan_count; i++) {
1526 struct applesmc_dev_attr *node = &fan_safe_attrs[i];
1527
1528 scnprintf(node->name, sizeof(node->name), "fan%d_safe", i + 1);
1529 node->sda.index = (3 << 16) | (i & 0xffff); /* Option 3 (safe speed) */
1530 node->sda.dev_attr.show = applesmc_show_fan_speed;
1531 node->sda.dev_attr.store = NULL;
1532 sysfs_attr_init(&node->sda.dev_attr.attr);
1533 node->sda.dev_attr.attr.name = node->name;
1534 node->sda.dev_attr.attr.mode = 0444;
1535 fan_safe_attr_list[i] = &node->sda.dev_attr.attr;
1536 }
1537 fan_safe_attr_list[smcreg.fan_count] = NULL;
1538 fan_safe_group.attrs = fan_safe_attr_list;
1539 applesmc_extra_groups[0] = &fan_safe_group;
1540 applesmc_extra_groups[1] = NULL;
1541
1542 ret = applesmc_create_accelerometer();
1543 if (ret)
1544 goto out_info;
1545
1546 ret = applesmc_create_light_sensor();
1547 if (ret)
1548 goto out_accelerometer;
1549
1550 ret = applesmc_create_key_backlight();
1551 if (ret)
1552 goto out_light_sysfs;
1553
1554 hwmon_dev = hwmon_device_register_with_info(&pdev->dev, "applesmc", NULL,
1555 applesmc_chip, applesmc_extra_groups);
1556 if (IS_ERR(hwmon_dev)) {
1557 ret = PTR_ERR(hwmon_dev);
1558 goto out_light_ledclass;
1559 }
1560
1561 return 0;
1562
1563 out_light_ledclass:
1564 applesmc_release_key_backlight();
1565 out_light_sysfs:
1566 applesmc_release_light_sensor();
1567 out_accelerometer:
1568 applesmc_release_accelerometer();
1569 out_info:
1570 applesmc_free_hwmon();
1571 applesmc_destroy_nodes(info_group);
1572 out_smcreg:
1573 applesmc_destroy_smcreg();
1574 out_device:
1575 platform_device_unregister(pdev);
1576 out_driver:
1577 platform_driver_unregister(&applesmc_driver);
1578 out_region:
1579 release_region(APPLESMC_DATA_PORT, APPLESMC_NR_PORTS);
1580 out:
1581 pr_warn("driver init failed (ret=%d)!\n", ret);
1582 return ret;
1583 }
1584
applesmc_exit(void)1585 static void __exit applesmc_exit(void)
1586 {
1587 hwmon_device_unregister(hwmon_dev);
1588 applesmc_release_key_backlight();
1589 applesmc_release_light_sensor();
1590 applesmc_release_accelerometer();
1591 applesmc_destroy_nodes(info_group);
1592 applesmc_destroy_smcreg();
1593 platform_device_unregister(pdev);
1594 platform_driver_unregister(&applesmc_driver);
1595 release_region(APPLESMC_DATA_PORT, APPLESMC_NR_PORTS);
1596 applesmc_free_hwmon();
1597 }
1598
1599 module_init(applesmc_init);
1600 module_exit(applesmc_exit);
1601
1602 MODULE_AUTHOR("Nicolas Boichat");
1603 MODULE_DESCRIPTION("Apple SMC");
1604 MODULE_LICENSE("GPL v2");
1605