applesmc.c 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339
  1. /*
  2. * drivers/hwmon/applesmc.c - driver for Apple's SMC (accelerometer, temperature
  3. * sensors, fan control, keyboard backlight control) used in Intel-based Apple
  4. * computers.
  5. *
  6. * Copyright (C) 2007 Nicolas Boichat <nicolas@boichat.ch>
  7. * Copyright (C) 2010 Henrik Rydberg <rydberg@euromail.se>
  8. *
  9. * Based on hdaps.c driver:
  10. * Copyright (C) 2005 Robert Love <rml@novell.com>
  11. * Copyright (C) 2005 Jesper Juhl <jj@chaosbits.net>
  12. *
  13. * Fan control based on smcFanControl:
  14. * Copyright (C) 2006 Hendrik Holtmann <holtmann@mac.com>
  15. *
  16. * This program is free software; you can redistribute it and/or modify it
  17. * under the terms of the GNU General Public License v2 as published by the
  18. * Free Software Foundation.
  19. *
  20. * This program is distributed in the hope that it will be useful, but WITHOUT
  21. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  22. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  23. * more details.
  24. *
  25. * You should have received a copy of the GNU General Public License along with
  26. * this program; if not, write to the Free Software Foundation, Inc.,
  27. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
  28. */
  29. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  30. #include <linux/delay.h>
  31. #include <linux/platform_device.h>
  32. #include <linux/input-polldev.h>
  33. #include <linux/kernel.h>
  34. #include <linux/slab.h>
  35. #include <linux/module.h>
  36. #include <linux/timer.h>
  37. #include <linux/dmi.h>
  38. #include <linux/mutex.h>
  39. #include <linux/hwmon-sysfs.h>
  40. #include <linux/io.h>
  41. #include <linux/leds.h>
  42. #include <linux/hwmon.h>
  43. #include <linux/workqueue.h>
  44. /* data port used by Apple SMC */
  45. #define APPLESMC_DATA_PORT 0x300
  46. /* command/status port used by Apple SMC */
  47. #define APPLESMC_CMD_PORT 0x304
  48. #define APPLESMC_NR_PORTS 32 /* 0x300-0x31f */
  49. #define APPLESMC_MAX_DATA_LENGTH 32
  50. /* wait up to 32 ms for a status change. */
  51. #define APPLESMC_MIN_WAIT 0x0010
  52. #define APPLESMC_MAX_WAIT 0x8000
  53. #define APPLESMC_STATUS_MASK 0x0f
  54. #define APPLESMC_READ_CMD 0x10
  55. #define APPLESMC_WRITE_CMD 0x11
  56. #define APPLESMC_GET_KEY_BY_INDEX_CMD 0x12
  57. #define APPLESMC_GET_KEY_TYPE_CMD 0x13
  58. #define KEY_COUNT_KEY "#KEY" /* r-o ui32 */
  59. #define LIGHT_SENSOR_LEFT_KEY "ALV0" /* r-o {alv (6-10 bytes) */
  60. #define LIGHT_SENSOR_RIGHT_KEY "ALV1" /* r-o {alv (6-10 bytes) */
  61. #define BACKLIGHT_KEY "LKSB" /* w-o {lkb (2 bytes) */
  62. #define CLAMSHELL_KEY "MSLD" /* r-o ui8 (unused) */
  63. #define MOTION_SENSOR_X_KEY "MO_X" /* r-o sp78 (2 bytes) */
  64. #define MOTION_SENSOR_Y_KEY "MO_Y" /* r-o sp78 (2 bytes) */
  65. #define MOTION_SENSOR_Z_KEY "MO_Z" /* r-o sp78 (2 bytes) */
  66. #define MOTION_SENSOR_KEY "MOCN" /* r/w ui16 */
  67. #define FANS_COUNT "FNum" /* r-o ui8 */
  68. #define FANS_MANUAL "FS! " /* r-w ui16 */
  69. #define FAN_ID_FMT "F%dID" /* r-o char[16] */
  70. #define TEMP_SENSOR_TYPE "sp78"
  71. /* List of keys used to read/write fan speeds */
  72. static const char *const fan_speed_fmt[] = {
  73. "F%dAc", /* actual speed */
  74. "F%dMn", /* minimum speed (rw) */
  75. "F%dMx", /* maximum speed */
  76. "F%dSf", /* safe speed - not all models */
  77. "F%dTg", /* target speed (manual: rw) */
  78. };
  79. #define INIT_TIMEOUT_MSECS 5000 /* wait up to 5s for device init ... */
  80. #define INIT_WAIT_MSECS 50 /* ... in 50ms increments */
  81. #define APPLESMC_POLL_INTERVAL 50 /* msecs */
  82. #define APPLESMC_INPUT_FUZZ 4 /* input event threshold */
  83. #define APPLESMC_INPUT_FLAT 4
  84. #define to_index(attr) (to_sensor_dev_attr(attr)->index & 0xffff)
  85. #define to_option(attr) (to_sensor_dev_attr(attr)->index >> 16)
  86. /* Dynamic device node attributes */
  87. struct applesmc_dev_attr {
  88. struct sensor_device_attribute sda; /* hwmon attributes */
  89. char name[32]; /* room for node file name */
  90. };
  91. /* Dynamic device node group */
  92. struct applesmc_node_group {
  93. char *format; /* format string */
  94. void *show; /* show function */
  95. void *store; /* store function */
  96. int option; /* function argument */
  97. struct applesmc_dev_attr *nodes; /* dynamic node array */
  98. };
  99. /* AppleSMC entry - cached register information */
  100. struct applesmc_entry {
  101. char key[5]; /* four-letter key code */
  102. u8 valid; /* set when entry is successfully read once */
  103. u8 len; /* bounded by APPLESMC_MAX_DATA_LENGTH */
  104. char type[5]; /* four-letter type code */
  105. u8 flags; /* 0x10: func; 0x40: write; 0x80: read */
  106. };
  107. /* Register lookup and registers common to all SMCs */
  108. static struct applesmc_registers {
  109. struct mutex mutex; /* register read/write mutex */
  110. unsigned int key_count; /* number of SMC registers */
  111. unsigned int fan_count; /* number of fans */
  112. unsigned int temp_count; /* number of temperature registers */
  113. unsigned int temp_begin; /* temperature lower index bound */
  114. unsigned int temp_end; /* temperature upper index bound */
  115. unsigned int index_count; /* size of temperature index array */
  116. int num_light_sensors; /* number of light sensors */
  117. bool has_accelerometer; /* has motion sensor */
  118. bool has_key_backlight; /* has keyboard backlight */
  119. bool init_complete; /* true when fully initialized */
  120. struct applesmc_entry *cache; /* cached key entries */
  121. const char **index; /* temperature key index */
  122. } smcreg = {
  123. .mutex = __MUTEX_INITIALIZER(smcreg.mutex),
  124. };
  125. static const int debug;
  126. static struct platform_device *pdev;
  127. static s16 rest_x;
  128. static s16 rest_y;
  129. static u8 backlight_state[2];
  130. static struct device *hwmon_dev;
  131. static struct input_polled_dev *applesmc_idev;
  132. /*
  133. * Last index written to key_at_index sysfs file, and value to use for all other
  134. * key_at_index_* sysfs files.
  135. */
  136. static unsigned int key_at_index;
  137. static struct workqueue_struct *applesmc_led_wq;
  138. /*
  139. * __wait_status - Wait up to 32ms for the status port to get a certain value
  140. * (masked with 0x0f), returning zero if the value is obtained. Callers must
  141. * hold applesmc_lock.
  142. */
  143. static int __wait_status(u8 val)
  144. {
  145. int us;
  146. val = val & APPLESMC_STATUS_MASK;
  147. for (us = APPLESMC_MIN_WAIT; us < APPLESMC_MAX_WAIT; us <<= 1) {
  148. udelay(us);
  149. if ((inb(APPLESMC_CMD_PORT) & APPLESMC_STATUS_MASK) == val)
  150. return 0;
  151. }
  152. return -EIO;
  153. }
  154. /*
  155. * special treatment of command port - on newer macbooks, it seems necessary
  156. * to resend the command byte before polling the status again. Callers must
  157. * hold applesmc_lock.
  158. */
  159. static int send_command(u8 cmd)
  160. {
  161. int us;
  162. for (us = APPLESMC_MIN_WAIT; us < APPLESMC_MAX_WAIT; us <<= 1) {
  163. outb(cmd, APPLESMC_CMD_PORT);
  164. udelay(us);
  165. if ((inb(APPLESMC_CMD_PORT) & APPLESMC_STATUS_MASK) == 0x0c)
  166. return 0;
  167. }
  168. return -EIO;
  169. }
  170. static int send_argument(const char *key)
  171. {
  172. int i;
  173. for (i = 0; i < 4; i++) {
  174. outb(key[i], APPLESMC_DATA_PORT);
  175. if (__wait_status(0x04))
  176. return -EIO;
  177. }
  178. return 0;
  179. }
  180. static int read_smc(u8 cmd, const char *key, u8 *buffer, u8 len)
  181. {
  182. int i;
  183. if (send_command(cmd) || send_argument(key)) {
  184. pr_warn("%.4s: read arg fail\n", key);
  185. return -EIO;
  186. }
  187. outb(len, APPLESMC_DATA_PORT);
  188. for (i = 0; i < len; i++) {
  189. if (__wait_status(0x05)) {
  190. pr_warn("%.4s: read data fail\n", key);
  191. return -EIO;
  192. }
  193. buffer[i] = inb(APPLESMC_DATA_PORT);
  194. }
  195. return 0;
  196. }
  197. static int write_smc(u8 cmd, const char *key, const u8 *buffer, u8 len)
  198. {
  199. int i;
  200. if (send_command(cmd) || send_argument(key)) {
  201. pr_warn("%s: write arg fail\n", key);
  202. return -EIO;
  203. }
  204. outb(len, APPLESMC_DATA_PORT);
  205. for (i = 0; i < len; i++) {
  206. if (__wait_status(0x04)) {
  207. pr_warn("%s: write data fail\n", key);
  208. return -EIO;
  209. }
  210. outb(buffer[i], APPLESMC_DATA_PORT);
  211. }
  212. return 0;
  213. }
  214. static int read_register_count(unsigned int *count)
  215. {
  216. __be32 be;
  217. int ret;
  218. ret = read_smc(APPLESMC_READ_CMD, KEY_COUNT_KEY, (u8 *)&be, 4);
  219. if (ret)
  220. return ret;
  221. *count = be32_to_cpu(be);
  222. return 0;
  223. }
  224. /*
  225. * Serialized I/O
  226. *
  227. * Returns zero on success or a negative error on failure.
  228. * All functions below are concurrency safe - callers should NOT hold lock.
  229. */
  230. static int applesmc_read_entry(const struct applesmc_entry *entry,
  231. u8 *buf, u8 len)
  232. {
  233. int ret;
  234. if (entry->len != len)
  235. return -EINVAL;
  236. mutex_lock(&smcreg.mutex);
  237. ret = read_smc(APPLESMC_READ_CMD, entry->key, buf, len);
  238. mutex_unlock(&smcreg.mutex);
  239. return ret;
  240. }
  241. static int applesmc_write_entry(const struct applesmc_entry *entry,
  242. const u8 *buf, u8 len)
  243. {
  244. int ret;
  245. if (entry->len != len)
  246. return -EINVAL;
  247. mutex_lock(&smcreg.mutex);
  248. ret = write_smc(APPLESMC_WRITE_CMD, entry->key, buf, len);
  249. mutex_unlock(&smcreg.mutex);
  250. return ret;
  251. }
  252. static const struct applesmc_entry *applesmc_get_entry_by_index(int index)
  253. {
  254. struct applesmc_entry *cache = &smcreg.cache[index];
  255. u8 key[4], info[6];
  256. __be32 be;
  257. int ret = 0;
  258. if (cache->valid)
  259. return cache;
  260. mutex_lock(&smcreg.mutex);
  261. if (cache->valid)
  262. goto out;
  263. be = cpu_to_be32(index);
  264. ret = read_smc(APPLESMC_GET_KEY_BY_INDEX_CMD, (u8 *)&be, key, 4);
  265. if (ret)
  266. goto out;
  267. ret = read_smc(APPLESMC_GET_KEY_TYPE_CMD, key, info, 6);
  268. if (ret)
  269. goto out;
  270. memcpy(cache->key, key, 4);
  271. cache->len = info[0];
  272. memcpy(cache->type, &info[1], 4);
  273. cache->flags = info[5];
  274. cache->valid = 1;
  275. out:
  276. mutex_unlock(&smcreg.mutex);
  277. if (ret)
  278. return ERR_PTR(ret);
  279. return cache;
  280. }
  281. static int applesmc_get_lower_bound(unsigned int *lo, const char *key)
  282. {
  283. int begin = 0, end = smcreg.key_count;
  284. const struct applesmc_entry *entry;
  285. while (begin != end) {
  286. int middle = begin + (end - begin) / 2;
  287. entry = applesmc_get_entry_by_index(middle);
  288. if (IS_ERR(entry)) {
  289. *lo = 0;
  290. return PTR_ERR(entry);
  291. }
  292. if (strcmp(entry->key, key) < 0)
  293. begin = middle + 1;
  294. else
  295. end = middle;
  296. }
  297. *lo = begin;
  298. return 0;
  299. }
  300. static int applesmc_get_upper_bound(unsigned int *hi, const char *key)
  301. {
  302. int begin = 0, end = smcreg.key_count;
  303. const struct applesmc_entry *entry;
  304. while (begin != end) {
  305. int middle = begin + (end - begin) / 2;
  306. entry = applesmc_get_entry_by_index(middle);
  307. if (IS_ERR(entry)) {
  308. *hi = smcreg.key_count;
  309. return PTR_ERR(entry);
  310. }
  311. if (strcmp(key, entry->key) < 0)
  312. end = middle;
  313. else
  314. begin = middle + 1;
  315. }
  316. *hi = begin;
  317. return 0;
  318. }
  319. static const struct applesmc_entry *applesmc_get_entry_by_key(const char *key)
  320. {
  321. int begin, end;
  322. int ret;
  323. ret = applesmc_get_lower_bound(&begin, key);
  324. if (ret)
  325. return ERR_PTR(ret);
  326. ret = applesmc_get_upper_bound(&end, key);
  327. if (ret)
  328. return ERR_PTR(ret);
  329. if (end - begin != 1)
  330. return ERR_PTR(-EINVAL);
  331. return applesmc_get_entry_by_index(begin);
  332. }
  333. static int applesmc_read_key(const char *key, u8 *buffer, u8 len)
  334. {
  335. const struct applesmc_entry *entry;
  336. entry = applesmc_get_entry_by_key(key);
  337. if (IS_ERR(entry))
  338. return PTR_ERR(entry);
  339. return applesmc_read_entry(entry, buffer, len);
  340. }
  341. static int applesmc_write_key(const char *key, const u8 *buffer, u8 len)
  342. {
  343. const struct applesmc_entry *entry;
  344. entry = applesmc_get_entry_by_key(key);
  345. if (IS_ERR(entry))
  346. return PTR_ERR(entry);
  347. return applesmc_write_entry(entry, buffer, len);
  348. }
  349. static int applesmc_has_key(const char *key, bool *value)
  350. {
  351. const struct applesmc_entry *entry;
  352. entry = applesmc_get_entry_by_key(key);
  353. if (IS_ERR(entry) && PTR_ERR(entry) != -EINVAL)
  354. return PTR_ERR(entry);
  355. *value = !IS_ERR(entry);
  356. return 0;
  357. }
  358. /*
  359. * applesmc_read_s16 - Read 16-bit signed big endian register
  360. */
  361. static int applesmc_read_s16(const char *key, s16 *value)
  362. {
  363. u8 buffer[2];
  364. int ret;
  365. ret = applesmc_read_key(key, buffer, 2);
  366. if (ret)
  367. return ret;
  368. *value = ((s16)buffer[0] << 8) | buffer[1];
  369. return 0;
  370. }
  371. /*
  372. * applesmc_device_init - initialize the accelerometer. Can sleep.
  373. */
  374. static void applesmc_device_init(void)
  375. {
  376. int total;
  377. u8 buffer[2];
  378. if (!smcreg.has_accelerometer)
  379. return;
  380. for (total = INIT_TIMEOUT_MSECS; total > 0; total -= INIT_WAIT_MSECS) {
  381. if (!applesmc_read_key(MOTION_SENSOR_KEY, buffer, 2) &&
  382. (buffer[0] != 0x00 || buffer[1] != 0x00))
  383. return;
  384. buffer[0] = 0xe0;
  385. buffer[1] = 0x00;
  386. applesmc_write_key(MOTION_SENSOR_KEY, buffer, 2);
  387. msleep(INIT_WAIT_MSECS);
  388. }
  389. pr_warn("failed to init the device\n");
  390. }
  391. static int applesmc_init_index(struct applesmc_registers *s)
  392. {
  393. const struct applesmc_entry *entry;
  394. unsigned int i;
  395. if (s->index)
  396. return 0;
  397. s->index = kcalloc(s->temp_count, sizeof(s->index[0]), GFP_KERNEL);
  398. if (!s->index)
  399. return -ENOMEM;
  400. for (i = s->temp_begin; i < s->temp_end; i++) {
  401. entry = applesmc_get_entry_by_index(i);
  402. if (IS_ERR(entry))
  403. continue;
  404. if (strcmp(entry->type, TEMP_SENSOR_TYPE))
  405. continue;
  406. s->index[s->index_count++] = entry->key;
  407. }
  408. return 0;
  409. }
  410. /*
  411. * applesmc_init_smcreg_try - Try to initialize register cache. Idempotent.
  412. */
  413. static int applesmc_init_smcreg_try(void)
  414. {
  415. struct applesmc_registers *s = &smcreg;
  416. bool left_light_sensor, right_light_sensor;
  417. u8 tmp[1];
  418. int ret;
  419. if (s->init_complete)
  420. return 0;
  421. ret = read_register_count(&s->key_count);
  422. if (ret)
  423. return ret;
  424. if (!s->cache)
  425. s->cache = kcalloc(s->key_count, sizeof(*s->cache), GFP_KERNEL);
  426. if (!s->cache)
  427. return -ENOMEM;
  428. ret = applesmc_read_key(FANS_COUNT, tmp, 1);
  429. if (ret)
  430. return ret;
  431. s->fan_count = tmp[0];
  432. ret = applesmc_get_lower_bound(&s->temp_begin, "T");
  433. if (ret)
  434. return ret;
  435. ret = applesmc_get_lower_bound(&s->temp_end, "U");
  436. if (ret)
  437. return ret;
  438. s->temp_count = s->temp_end - s->temp_begin;
  439. ret = applesmc_init_index(s);
  440. if (ret)
  441. return ret;
  442. ret = applesmc_has_key(LIGHT_SENSOR_LEFT_KEY, &left_light_sensor);
  443. if (ret)
  444. return ret;
  445. ret = applesmc_has_key(LIGHT_SENSOR_RIGHT_KEY, &right_light_sensor);
  446. if (ret)
  447. return ret;
  448. ret = applesmc_has_key(MOTION_SENSOR_KEY, &s->has_accelerometer);
  449. if (ret)
  450. return ret;
  451. ret = applesmc_has_key(BACKLIGHT_KEY, &s->has_key_backlight);
  452. if (ret)
  453. return ret;
  454. s->num_light_sensors = left_light_sensor + right_light_sensor;
  455. s->init_complete = true;
  456. pr_info("key=%d fan=%d temp=%d index=%d acc=%d lux=%d kbd=%d\n",
  457. s->key_count, s->fan_count, s->temp_count, s->index_count,
  458. s->has_accelerometer,
  459. s->num_light_sensors,
  460. s->has_key_backlight);
  461. return 0;
  462. }
  463. static void applesmc_destroy_smcreg(void)
  464. {
  465. kfree(smcreg.index);
  466. smcreg.index = NULL;
  467. kfree(smcreg.cache);
  468. smcreg.cache = NULL;
  469. smcreg.init_complete = false;
  470. }
  471. /*
  472. * applesmc_init_smcreg - Initialize register cache.
  473. *
  474. * Retries until initialization is successful, or the operation times out.
  475. *
  476. */
  477. static int applesmc_init_smcreg(void)
  478. {
  479. int ms, ret;
  480. for (ms = 0; ms < INIT_TIMEOUT_MSECS; ms += INIT_WAIT_MSECS) {
  481. ret = applesmc_init_smcreg_try();
  482. if (!ret) {
  483. if (ms)
  484. pr_info("init_smcreg() took %d ms\n", ms);
  485. return 0;
  486. }
  487. msleep(INIT_WAIT_MSECS);
  488. }
  489. applesmc_destroy_smcreg();
  490. return ret;
  491. }
  492. /* Device model stuff */
  493. static int applesmc_probe(struct platform_device *dev)
  494. {
  495. int ret;
  496. ret = applesmc_init_smcreg();
  497. if (ret)
  498. return ret;
  499. applesmc_device_init();
  500. return 0;
  501. }
  502. /* Synchronize device with memorized backlight state */
  503. static int applesmc_pm_resume(struct device *dev)
  504. {
  505. if (smcreg.has_key_backlight)
  506. applesmc_write_key(BACKLIGHT_KEY, backlight_state, 2);
  507. return 0;
  508. }
  509. /* Reinitialize device on resume from hibernation */
  510. static int applesmc_pm_restore(struct device *dev)
  511. {
  512. applesmc_device_init();
  513. return applesmc_pm_resume(dev);
  514. }
  515. static const struct dev_pm_ops applesmc_pm_ops = {
  516. .resume = applesmc_pm_resume,
  517. .restore = applesmc_pm_restore,
  518. };
  519. static struct platform_driver applesmc_driver = {
  520. .probe = applesmc_probe,
  521. .driver = {
  522. .name = "applesmc",
  523. .owner = THIS_MODULE,
  524. .pm = &applesmc_pm_ops,
  525. },
  526. };
  527. /*
  528. * applesmc_calibrate - Set our "resting" values. Callers must
  529. * hold applesmc_lock.
  530. */
  531. static void applesmc_calibrate(void)
  532. {
  533. applesmc_read_s16(MOTION_SENSOR_X_KEY, &rest_x);
  534. applesmc_read_s16(MOTION_SENSOR_Y_KEY, &rest_y);
  535. rest_x = -rest_x;
  536. }
  537. static void applesmc_idev_poll(struct input_polled_dev *dev)
  538. {
  539. struct input_dev *idev = dev->input;
  540. s16 x, y;
  541. if (applesmc_read_s16(MOTION_SENSOR_X_KEY, &x))
  542. return;
  543. if (applesmc_read_s16(MOTION_SENSOR_Y_KEY, &y))
  544. return;
  545. x = -x;
  546. input_report_abs(idev, ABS_X, x - rest_x);
  547. input_report_abs(idev, ABS_Y, y - rest_y);
  548. input_sync(idev);
  549. }
  550. /* Sysfs Files */
  551. static ssize_t applesmc_name_show(struct device *dev,
  552. struct device_attribute *attr, char *buf)
  553. {
  554. return snprintf(buf, PAGE_SIZE, "applesmc\n");
  555. }
  556. static ssize_t applesmc_position_show(struct device *dev,
  557. struct device_attribute *attr, char *buf)
  558. {
  559. int ret;
  560. s16 x, y, z;
  561. ret = applesmc_read_s16(MOTION_SENSOR_X_KEY, &x);
  562. if (ret)
  563. goto out;
  564. ret = applesmc_read_s16(MOTION_SENSOR_Y_KEY, &y);
  565. if (ret)
  566. goto out;
  567. ret = applesmc_read_s16(MOTION_SENSOR_Z_KEY, &z);
  568. if (ret)
  569. goto out;
  570. out:
  571. if (ret)
  572. return ret;
  573. else
  574. return snprintf(buf, PAGE_SIZE, "(%d,%d,%d)\n", x, y, z);
  575. }
  576. static ssize_t applesmc_light_show(struct device *dev,
  577. struct device_attribute *attr, char *sysfsbuf)
  578. {
  579. const struct applesmc_entry *entry;
  580. static int data_length;
  581. int ret;
  582. u8 left = 0, right = 0;
  583. u8 buffer[10];
  584. if (!data_length) {
  585. entry = applesmc_get_entry_by_key(LIGHT_SENSOR_LEFT_KEY);
  586. if (IS_ERR(entry))
  587. return PTR_ERR(entry);
  588. if (entry->len > 10)
  589. return -ENXIO;
  590. data_length = entry->len;
  591. pr_info("light sensor data length set to %d\n", data_length);
  592. }
  593. ret = applesmc_read_key(LIGHT_SENSOR_LEFT_KEY, buffer, data_length);
  594. /* newer macbooks report a single 10-bit bigendian value */
  595. if (data_length == 10) {
  596. left = be16_to_cpu(*(__be16 *)(buffer + 6)) >> 2;
  597. goto out;
  598. }
  599. left = buffer[2];
  600. if (ret)
  601. goto out;
  602. ret = applesmc_read_key(LIGHT_SENSOR_RIGHT_KEY, buffer, data_length);
  603. right = buffer[2];
  604. out:
  605. if (ret)
  606. return ret;
  607. else
  608. return snprintf(sysfsbuf, PAGE_SIZE, "(%d,%d)\n", left, right);
  609. }
  610. /* Displays sensor key as label */
  611. static ssize_t applesmc_show_sensor_label(struct device *dev,
  612. struct device_attribute *devattr, char *sysfsbuf)
  613. {
  614. const char *key = smcreg.index[to_index(devattr)];
  615. return snprintf(sysfsbuf, PAGE_SIZE, "%s\n", key);
  616. }
  617. /* Displays degree Celsius * 1000 */
  618. static ssize_t applesmc_show_temperature(struct device *dev,
  619. struct device_attribute *devattr, char *sysfsbuf)
  620. {
  621. const char *key = smcreg.index[to_index(devattr)];
  622. int ret;
  623. s16 value;
  624. int temp;
  625. ret = applesmc_read_s16(key, &value);
  626. if (ret)
  627. return ret;
  628. temp = 250 * (value >> 6);
  629. return snprintf(sysfsbuf, PAGE_SIZE, "%d\n", temp);
  630. }
  631. static ssize_t applesmc_show_fan_speed(struct device *dev,
  632. struct device_attribute *attr, char *sysfsbuf)
  633. {
  634. int ret;
  635. unsigned int speed = 0;
  636. char newkey[5];
  637. u8 buffer[2];
  638. sprintf(newkey, fan_speed_fmt[to_option(attr)], to_index(attr));
  639. ret = applesmc_read_key(newkey, buffer, 2);
  640. speed = ((buffer[0] << 8 | buffer[1]) >> 2);
  641. if (ret)
  642. return ret;
  643. else
  644. return snprintf(sysfsbuf, PAGE_SIZE, "%u\n", speed);
  645. }
  646. static ssize_t applesmc_store_fan_speed(struct device *dev,
  647. struct device_attribute *attr,
  648. const char *sysfsbuf, size_t count)
  649. {
  650. int ret;
  651. unsigned long speed;
  652. char newkey[5];
  653. u8 buffer[2];
  654. if (kstrtoul(sysfsbuf, 10, &speed) < 0 || speed >= 0x4000)
  655. return -EINVAL; /* Bigger than a 14-bit value */
  656. sprintf(newkey, fan_speed_fmt[to_option(attr)], to_index(attr));
  657. buffer[0] = (speed >> 6) & 0xff;
  658. buffer[1] = (speed << 2) & 0xff;
  659. ret = applesmc_write_key(newkey, buffer, 2);
  660. if (ret)
  661. return ret;
  662. else
  663. return count;
  664. }
  665. static ssize_t applesmc_show_fan_manual(struct device *dev,
  666. struct device_attribute *attr, char *sysfsbuf)
  667. {
  668. int ret;
  669. u16 manual = 0;
  670. u8 buffer[2];
  671. ret = applesmc_read_key(FANS_MANUAL, buffer, 2);
  672. manual = ((buffer[0] << 8 | buffer[1]) >> to_index(attr)) & 0x01;
  673. if (ret)
  674. return ret;
  675. else
  676. return snprintf(sysfsbuf, PAGE_SIZE, "%d\n", manual);
  677. }
  678. static ssize_t applesmc_store_fan_manual(struct device *dev,
  679. struct device_attribute *attr,
  680. const char *sysfsbuf, size_t count)
  681. {
  682. int ret;
  683. u8 buffer[2];
  684. unsigned long input;
  685. u16 val;
  686. if (kstrtoul(sysfsbuf, 10, &input) < 0)
  687. return -EINVAL;
  688. ret = applesmc_read_key(FANS_MANUAL, buffer, 2);
  689. val = (buffer[0] << 8 | buffer[1]);
  690. if (ret)
  691. goto out;
  692. if (input)
  693. val = val | (0x01 << to_index(attr));
  694. else
  695. val = val & ~(0x01 << to_index(attr));
  696. buffer[0] = (val >> 8) & 0xFF;
  697. buffer[1] = val & 0xFF;
  698. ret = applesmc_write_key(FANS_MANUAL, buffer, 2);
  699. out:
  700. if (ret)
  701. return ret;
  702. else
  703. return count;
  704. }
  705. static ssize_t applesmc_show_fan_position(struct device *dev,
  706. struct device_attribute *attr, char *sysfsbuf)
  707. {
  708. int ret;
  709. char newkey[5];
  710. u8 buffer[17];
  711. sprintf(newkey, FAN_ID_FMT, to_index(attr));
  712. ret = applesmc_read_key(newkey, buffer, 16);
  713. buffer[16] = 0;
  714. if (ret)
  715. return ret;
  716. else
  717. return snprintf(sysfsbuf, PAGE_SIZE, "%s\n", buffer+4);
  718. }
  719. static ssize_t applesmc_calibrate_show(struct device *dev,
  720. struct device_attribute *attr, char *sysfsbuf)
  721. {
  722. return snprintf(sysfsbuf, PAGE_SIZE, "(%d,%d)\n", rest_x, rest_y);
  723. }
  724. static ssize_t applesmc_calibrate_store(struct device *dev,
  725. struct device_attribute *attr, const char *sysfsbuf, size_t count)
  726. {
  727. applesmc_calibrate();
  728. return count;
  729. }
  730. static void applesmc_backlight_set(struct work_struct *work)
  731. {
  732. applesmc_write_key(BACKLIGHT_KEY, backlight_state, 2);
  733. }
  734. static DECLARE_WORK(backlight_work, &applesmc_backlight_set);
  735. static void applesmc_brightness_set(struct led_classdev *led_cdev,
  736. enum led_brightness value)
  737. {
  738. int ret;
  739. backlight_state[0] = value;
  740. ret = queue_work(applesmc_led_wq, &backlight_work);
  741. if (debug && (!ret))
  742. printk(KERN_DEBUG "applesmc: work was already on the queue.\n");
  743. }
  744. static ssize_t applesmc_key_count_show(struct device *dev,
  745. struct device_attribute *attr, char *sysfsbuf)
  746. {
  747. int ret;
  748. u8 buffer[4];
  749. u32 count;
  750. ret = applesmc_read_key(KEY_COUNT_KEY, buffer, 4);
  751. count = ((u32)buffer[0]<<24) + ((u32)buffer[1]<<16) +
  752. ((u32)buffer[2]<<8) + buffer[3];
  753. if (ret)
  754. return ret;
  755. else
  756. return snprintf(sysfsbuf, PAGE_SIZE, "%d\n", count);
  757. }
  758. static ssize_t applesmc_key_at_index_read_show(struct device *dev,
  759. struct device_attribute *attr, char *sysfsbuf)
  760. {
  761. const struct applesmc_entry *entry;
  762. int ret;
  763. entry = applesmc_get_entry_by_index(key_at_index);
  764. if (IS_ERR(entry))
  765. return PTR_ERR(entry);
  766. ret = applesmc_read_entry(entry, sysfsbuf, entry->len);
  767. if (ret)
  768. return ret;
  769. return entry->len;
  770. }
  771. static ssize_t applesmc_key_at_index_data_length_show(struct device *dev,
  772. struct device_attribute *attr, char *sysfsbuf)
  773. {
  774. const struct applesmc_entry *entry;
  775. entry = applesmc_get_entry_by_index(key_at_index);
  776. if (IS_ERR(entry))
  777. return PTR_ERR(entry);
  778. return snprintf(sysfsbuf, PAGE_SIZE, "%d\n", entry->len);
  779. }
  780. static ssize_t applesmc_key_at_index_type_show(struct device *dev,
  781. struct device_attribute *attr, char *sysfsbuf)
  782. {
  783. const struct applesmc_entry *entry;
  784. entry = applesmc_get_entry_by_index(key_at_index);
  785. if (IS_ERR(entry))
  786. return PTR_ERR(entry);
  787. return snprintf(sysfsbuf, PAGE_SIZE, "%s\n", entry->type);
  788. }
  789. static ssize_t applesmc_key_at_index_name_show(struct device *dev,
  790. struct device_attribute *attr, char *sysfsbuf)
  791. {
  792. const struct applesmc_entry *entry;
  793. entry = applesmc_get_entry_by_index(key_at_index);
  794. if (IS_ERR(entry))
  795. return PTR_ERR(entry);
  796. return snprintf(sysfsbuf, PAGE_SIZE, "%s\n", entry->key);
  797. }
  798. static ssize_t applesmc_key_at_index_show(struct device *dev,
  799. struct device_attribute *attr, char *sysfsbuf)
  800. {
  801. return snprintf(sysfsbuf, PAGE_SIZE, "%d\n", key_at_index);
  802. }
  803. static ssize_t applesmc_key_at_index_store(struct device *dev,
  804. struct device_attribute *attr, const char *sysfsbuf, size_t count)
  805. {
  806. unsigned long newkey;
  807. if (kstrtoul(sysfsbuf, 10, &newkey) < 0
  808. || newkey >= smcreg.key_count)
  809. return -EINVAL;
  810. key_at_index = newkey;
  811. return count;
  812. }
  813. static struct led_classdev applesmc_backlight = {
  814. .name = "smc::kbd_backlight",
  815. .default_trigger = "nand-disk",
  816. .brightness_set = applesmc_brightness_set,
  817. };
  818. static struct applesmc_node_group info_group[] = {
  819. { "name", applesmc_name_show },
  820. { "key_count", applesmc_key_count_show },
  821. { "key_at_index", applesmc_key_at_index_show, applesmc_key_at_index_store },
  822. { "key_at_index_name", applesmc_key_at_index_name_show },
  823. { "key_at_index_type", applesmc_key_at_index_type_show },
  824. { "key_at_index_data_length", applesmc_key_at_index_data_length_show },
  825. { "key_at_index_data", applesmc_key_at_index_read_show },
  826. { }
  827. };
  828. static struct applesmc_node_group accelerometer_group[] = {
  829. { "position", applesmc_position_show },
  830. { "calibrate", applesmc_calibrate_show, applesmc_calibrate_store },
  831. { }
  832. };
  833. static struct applesmc_node_group light_sensor_group[] = {
  834. { "light", applesmc_light_show },
  835. { }
  836. };
  837. static struct applesmc_node_group fan_group[] = {
  838. { "fan%d_label", applesmc_show_fan_position },
  839. { "fan%d_input", applesmc_show_fan_speed, NULL, 0 },
  840. { "fan%d_min", applesmc_show_fan_speed, applesmc_store_fan_speed, 1 },
  841. { "fan%d_max", applesmc_show_fan_speed, NULL, 2 },
  842. { "fan%d_safe", applesmc_show_fan_speed, NULL, 3 },
  843. { "fan%d_output", applesmc_show_fan_speed, applesmc_store_fan_speed, 4 },
  844. { "fan%d_manual", applesmc_show_fan_manual, applesmc_store_fan_manual },
  845. { }
  846. };
  847. static struct applesmc_node_group temp_group[] = {
  848. { "temp%d_label", applesmc_show_sensor_label },
  849. { "temp%d_input", applesmc_show_temperature },
  850. { }
  851. };
  852. /* Module stuff */
  853. /*
  854. * applesmc_destroy_nodes - remove files and free associated memory
  855. */
  856. static void applesmc_destroy_nodes(struct applesmc_node_group *groups)
  857. {
  858. struct applesmc_node_group *grp;
  859. struct applesmc_dev_attr *node;
  860. for (grp = groups; grp->nodes; grp++) {
  861. for (node = grp->nodes; node->sda.dev_attr.attr.name; node++)
  862. sysfs_remove_file(&pdev->dev.kobj,
  863. &node->sda.dev_attr.attr);
  864. kfree(grp->nodes);
  865. grp->nodes = NULL;
  866. }
  867. }
  868. /*
  869. * applesmc_create_nodes - create a two-dimensional group of sysfs files
  870. */
  871. static int applesmc_create_nodes(struct applesmc_node_group *groups, int num)
  872. {
  873. struct applesmc_node_group *grp;
  874. struct applesmc_dev_attr *node;
  875. struct attribute *attr;
  876. int ret, i;
  877. for (grp = groups; grp->format; grp++) {
  878. grp->nodes = kcalloc(num + 1, sizeof(*node), GFP_KERNEL);
  879. if (!grp->nodes) {
  880. ret = -ENOMEM;
  881. goto out;
  882. }
  883. for (i = 0; i < num; i++) {
  884. node = &grp->nodes[i];
  885. sprintf(node->name, grp->format, i + 1);
  886. node->sda.index = (grp->option << 16) | (i & 0xffff);
  887. node->sda.dev_attr.show = grp->show;
  888. node->sda.dev_attr.store = grp->store;
  889. attr = &node->sda.dev_attr.attr;
  890. sysfs_attr_init(attr);
  891. attr->name = node->name;
  892. attr->mode = S_IRUGO | (grp->store ? S_IWUSR : 0);
  893. ret = sysfs_create_file(&pdev->dev.kobj, attr);
  894. if (ret) {
  895. attr->name = NULL;
  896. goto out;
  897. }
  898. }
  899. }
  900. return 0;
  901. out:
  902. applesmc_destroy_nodes(groups);
  903. return ret;
  904. }
  905. /* Create accelerometer ressources */
  906. static int applesmc_create_accelerometer(void)
  907. {
  908. struct input_dev *idev;
  909. int ret;
  910. if (!smcreg.has_accelerometer)
  911. return 0;
  912. ret = applesmc_create_nodes(accelerometer_group, 1);
  913. if (ret)
  914. goto out;
  915. applesmc_idev = input_allocate_polled_device();
  916. if (!applesmc_idev) {
  917. ret = -ENOMEM;
  918. goto out_sysfs;
  919. }
  920. applesmc_idev->poll = applesmc_idev_poll;
  921. applesmc_idev->poll_interval = APPLESMC_POLL_INTERVAL;
  922. /* initial calibrate for the input device */
  923. applesmc_calibrate();
  924. /* initialize the input device */
  925. idev = applesmc_idev->input;
  926. idev->name = "applesmc";
  927. idev->id.bustype = BUS_HOST;
  928. idev->dev.parent = &pdev->dev;
  929. idev->evbit[0] = BIT_MASK(EV_ABS);
  930. input_set_abs_params(idev, ABS_X,
  931. -256, 256, APPLESMC_INPUT_FUZZ, APPLESMC_INPUT_FLAT);
  932. input_set_abs_params(idev, ABS_Y,
  933. -256, 256, APPLESMC_INPUT_FUZZ, APPLESMC_INPUT_FLAT);
  934. ret = input_register_polled_device(applesmc_idev);
  935. if (ret)
  936. goto out_idev;
  937. return 0;
  938. out_idev:
  939. input_free_polled_device(applesmc_idev);
  940. out_sysfs:
  941. applesmc_destroy_nodes(accelerometer_group);
  942. out:
  943. pr_warn("driver init failed (ret=%d)!\n", ret);
  944. return ret;
  945. }
  946. /* Release all ressources used by the accelerometer */
  947. static void applesmc_release_accelerometer(void)
  948. {
  949. if (!smcreg.has_accelerometer)
  950. return;
  951. input_unregister_polled_device(applesmc_idev);
  952. input_free_polled_device(applesmc_idev);
  953. applesmc_destroy_nodes(accelerometer_group);
  954. }
  955. static int applesmc_create_light_sensor(void)
  956. {
  957. if (!smcreg.num_light_sensors)
  958. return 0;
  959. return applesmc_create_nodes(light_sensor_group, 1);
  960. }
  961. static void applesmc_release_light_sensor(void)
  962. {
  963. if (!smcreg.num_light_sensors)
  964. return;
  965. applesmc_destroy_nodes(light_sensor_group);
  966. }
  967. static int applesmc_create_key_backlight(void)
  968. {
  969. if (!smcreg.has_key_backlight)
  970. return 0;
  971. applesmc_led_wq = create_singlethread_workqueue("applesmc-led");
  972. if (!applesmc_led_wq)
  973. return -ENOMEM;
  974. return led_classdev_register(&pdev->dev, &applesmc_backlight);
  975. }
  976. static void applesmc_release_key_backlight(void)
  977. {
  978. if (!smcreg.has_key_backlight)
  979. return;
  980. led_classdev_unregister(&applesmc_backlight);
  981. destroy_workqueue(applesmc_led_wq);
  982. }
  983. static int applesmc_dmi_match(const struct dmi_system_id *id)
  984. {
  985. return 1;
  986. }
  987. /*
  988. * Note that DMI_MATCH(...,"MacBook") will match "MacBookPro1,1".
  989. * So we need to put "Apple MacBook Pro" before "Apple MacBook".
  990. */
  991. static __initdata struct dmi_system_id applesmc_whitelist[] = {
  992. { applesmc_dmi_match, "Apple MacBook Air", {
  993. DMI_MATCH(DMI_BOARD_VENDOR, "Apple"),
  994. DMI_MATCH(DMI_PRODUCT_NAME, "MacBookAir") },
  995. },
  996. { applesmc_dmi_match, "Apple MacBook Pro", {
  997. DMI_MATCH(DMI_BOARD_VENDOR, "Apple"),
  998. DMI_MATCH(DMI_PRODUCT_NAME, "MacBookPro") },
  999. },
  1000. { applesmc_dmi_match, "Apple MacBook", {
  1001. DMI_MATCH(DMI_BOARD_VENDOR, "Apple"),
  1002. DMI_MATCH(DMI_PRODUCT_NAME, "MacBook") },
  1003. },
  1004. { applesmc_dmi_match, "Apple Macmini", {
  1005. DMI_MATCH(DMI_BOARD_VENDOR, "Apple"),
  1006. DMI_MATCH(DMI_PRODUCT_NAME, "Macmini") },
  1007. },
  1008. { applesmc_dmi_match, "Apple MacPro", {
  1009. DMI_MATCH(DMI_BOARD_VENDOR, "Apple"),
  1010. DMI_MATCH(DMI_PRODUCT_NAME, "MacPro") },
  1011. },
  1012. { applesmc_dmi_match, "Apple iMac", {
  1013. DMI_MATCH(DMI_BOARD_VENDOR, "Apple"),
  1014. DMI_MATCH(DMI_PRODUCT_NAME, "iMac") },
  1015. },
  1016. { .ident = NULL }
  1017. };
  1018. static int __init applesmc_init(void)
  1019. {
  1020. int ret;
  1021. if (!dmi_check_system(applesmc_whitelist)) {
  1022. pr_warn("supported laptop not found!\n");
  1023. ret = -ENODEV;
  1024. goto out;
  1025. }
  1026. if (!request_region(APPLESMC_DATA_PORT, APPLESMC_NR_PORTS,
  1027. "applesmc")) {
  1028. ret = -ENXIO;
  1029. goto out;
  1030. }
  1031. ret = platform_driver_register(&applesmc_driver);
  1032. if (ret)
  1033. goto out_region;
  1034. pdev = platform_device_register_simple("applesmc", APPLESMC_DATA_PORT,
  1035. NULL, 0);
  1036. if (IS_ERR(pdev)) {
  1037. ret = PTR_ERR(pdev);
  1038. goto out_driver;
  1039. }
  1040. /* create register cache */
  1041. ret = applesmc_init_smcreg();
  1042. if (ret)
  1043. goto out_device;
  1044. ret = applesmc_create_nodes(info_group, 1);
  1045. if (ret)
  1046. goto out_smcreg;
  1047. ret = applesmc_create_nodes(fan_group, smcreg.fan_count);
  1048. if (ret)
  1049. goto out_info;
  1050. ret = applesmc_create_nodes(temp_group, smcreg.index_count);
  1051. if (ret)
  1052. goto out_fans;
  1053. ret = applesmc_create_accelerometer();
  1054. if (ret)
  1055. goto out_temperature;
  1056. ret = applesmc_create_light_sensor();
  1057. if (ret)
  1058. goto out_accelerometer;
  1059. ret = applesmc_create_key_backlight();
  1060. if (ret)
  1061. goto out_light_sysfs;
  1062. hwmon_dev = hwmon_device_register(&pdev->dev);
  1063. if (IS_ERR(hwmon_dev)) {
  1064. ret = PTR_ERR(hwmon_dev);
  1065. goto out_light_ledclass;
  1066. }
  1067. return 0;
  1068. out_light_ledclass:
  1069. applesmc_release_key_backlight();
  1070. out_light_sysfs:
  1071. applesmc_release_light_sensor();
  1072. out_accelerometer:
  1073. applesmc_release_accelerometer();
  1074. out_temperature:
  1075. applesmc_destroy_nodes(temp_group);
  1076. out_fans:
  1077. applesmc_destroy_nodes(fan_group);
  1078. out_info:
  1079. applesmc_destroy_nodes(info_group);
  1080. out_smcreg:
  1081. applesmc_destroy_smcreg();
  1082. out_device:
  1083. platform_device_unregister(pdev);
  1084. out_driver:
  1085. platform_driver_unregister(&applesmc_driver);
  1086. out_region:
  1087. release_region(APPLESMC_DATA_PORT, APPLESMC_NR_PORTS);
  1088. out:
  1089. pr_warn("driver init failed (ret=%d)!\n", ret);
  1090. return ret;
  1091. }
  1092. static void __exit applesmc_exit(void)
  1093. {
  1094. hwmon_device_unregister(hwmon_dev);
  1095. applesmc_release_key_backlight();
  1096. applesmc_release_light_sensor();
  1097. applesmc_release_accelerometer();
  1098. applesmc_destroy_nodes(temp_group);
  1099. applesmc_destroy_nodes(fan_group);
  1100. applesmc_destroy_nodes(info_group);
  1101. applesmc_destroy_smcreg();
  1102. platform_device_unregister(pdev);
  1103. platform_driver_unregister(&applesmc_driver);
  1104. release_region(APPLESMC_DATA_PORT, APPLESMC_NR_PORTS);
  1105. }
  1106. module_init(applesmc_init);
  1107. module_exit(applesmc_exit);
  1108. MODULE_AUTHOR("Nicolas Boichat");
  1109. MODULE_DESCRIPTION("Apple SMC");
  1110. MODULE_LICENSE("GPL v2");
  1111. MODULE_DEVICE_TABLE(dmi, applesmc_whitelist);