applesmc.c 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333
  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 <jesper.juhl@gmail.com>
  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 0x0040
  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. /* List of keys used to read/write fan speeds */
  71. static const char *const fan_speed_fmt[] = {
  72. "F%dAc", /* actual speed */
  73. "F%dMn", /* minimum speed (rw) */
  74. "F%dMx", /* maximum speed */
  75. "F%dSf", /* safe speed - not all models */
  76. "F%dTg", /* target speed (manual: rw) */
  77. };
  78. #define INIT_TIMEOUT_MSECS 5000 /* wait up to 5s for device init ... */
  79. #define INIT_WAIT_MSECS 50 /* ... in 50ms increments */
  80. #define APPLESMC_POLL_INTERVAL 50 /* msecs */
  81. #define APPLESMC_INPUT_FUZZ 4 /* input event threshold */
  82. #define APPLESMC_INPUT_FLAT 4
  83. #define SENSOR_X 0
  84. #define SENSOR_Y 1
  85. #define SENSOR_Z 2
  86. #define to_index(attr) (to_sensor_dev_attr(attr)->index & 0xffff)
  87. #define to_option(attr) (to_sensor_dev_attr(attr)->index >> 16)
  88. /* Dynamic device node attributes */
  89. struct applesmc_dev_attr {
  90. struct sensor_device_attribute sda; /* hwmon attributes */
  91. char name[32]; /* room for node file name */
  92. };
  93. /* Dynamic device node group */
  94. struct applesmc_node_group {
  95. char *format; /* format string */
  96. void *show; /* show function */
  97. void *store; /* store function */
  98. int option; /* function argument */
  99. struct applesmc_dev_attr *nodes; /* dynamic node array */
  100. };
  101. /* AppleSMC entry - cached register information */
  102. struct applesmc_entry {
  103. char key[5]; /* four-letter key code */
  104. u8 valid; /* set when entry is successfully read once */
  105. u8 len; /* bounded by APPLESMC_MAX_DATA_LENGTH */
  106. char type[5]; /* four-letter type code */
  107. u8 flags; /* 0x10: func; 0x40: write; 0x80: read */
  108. };
  109. /* Register lookup and registers common to all SMCs */
  110. static struct applesmc_registers {
  111. struct mutex mutex; /* register read/write mutex */
  112. unsigned int key_count; /* number of SMC registers */
  113. unsigned int fan_count; /* number of fans */
  114. unsigned int temp_count; /* number of temperature registers */
  115. unsigned int temp_begin; /* temperature lower index bound */
  116. unsigned int temp_end; /* temperature upper index bound */
  117. int num_light_sensors; /* number of light sensors */
  118. bool has_accelerometer; /* has motion sensor */
  119. bool has_key_backlight; /* has keyboard backlight */
  120. bool init_complete; /* true when fully initialized */
  121. struct applesmc_entry *cache; /* cached key entries */
  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("%s: 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("%s: 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. return PTR_ERR(entry);
  290. if (strcmp(entry->key, key) < 0)
  291. begin = middle + 1;
  292. else
  293. end = middle;
  294. }
  295. *lo = begin;
  296. return 0;
  297. }
  298. static int applesmc_get_upper_bound(unsigned int *hi, const char *key)
  299. {
  300. int begin = 0, end = smcreg.key_count;
  301. const struct applesmc_entry *entry;
  302. while (begin != end) {
  303. int middle = begin + (end - begin) / 2;
  304. entry = applesmc_get_entry_by_index(middle);
  305. if (IS_ERR(entry))
  306. return PTR_ERR(entry);
  307. if (strcmp(key, entry->key) < 0)
  308. end = middle;
  309. else
  310. begin = middle + 1;
  311. }
  312. *hi = begin;
  313. return 0;
  314. }
  315. static const struct applesmc_entry *applesmc_get_entry_by_key(const char *key)
  316. {
  317. int begin, end;
  318. int ret;
  319. ret = applesmc_get_lower_bound(&begin, key);
  320. if (ret)
  321. return ERR_PTR(ret);
  322. ret = applesmc_get_upper_bound(&end, key);
  323. if (ret)
  324. return ERR_PTR(ret);
  325. if (end - begin != 1)
  326. return ERR_PTR(-EINVAL);
  327. return applesmc_get_entry_by_index(begin);
  328. }
  329. static int applesmc_read_key(const char *key, u8 *buffer, u8 len)
  330. {
  331. const struct applesmc_entry *entry;
  332. entry = applesmc_get_entry_by_key(key);
  333. if (IS_ERR(entry))
  334. return PTR_ERR(entry);
  335. return applesmc_read_entry(entry, buffer, len);
  336. }
  337. static int applesmc_write_key(const char *key, const u8 *buffer, u8 len)
  338. {
  339. const struct applesmc_entry *entry;
  340. entry = applesmc_get_entry_by_key(key);
  341. if (IS_ERR(entry))
  342. return PTR_ERR(entry);
  343. return applesmc_write_entry(entry, buffer, len);
  344. }
  345. static int applesmc_has_key(const char *key, bool *value)
  346. {
  347. const struct applesmc_entry *entry;
  348. entry = applesmc_get_entry_by_key(key);
  349. if (IS_ERR(entry) && PTR_ERR(entry) != -EINVAL)
  350. return PTR_ERR(entry);
  351. *value = !IS_ERR(entry);
  352. return 0;
  353. }
  354. /*
  355. * applesmc_read_motion_sensor - Read motion sensor (X, Y or Z).
  356. */
  357. static int applesmc_read_motion_sensor(int index, s16 *value)
  358. {
  359. u8 buffer[2];
  360. int ret;
  361. switch (index) {
  362. case SENSOR_X:
  363. ret = applesmc_read_key(MOTION_SENSOR_X_KEY, buffer, 2);
  364. break;
  365. case SENSOR_Y:
  366. ret = applesmc_read_key(MOTION_SENSOR_Y_KEY, buffer, 2);
  367. break;
  368. case SENSOR_Z:
  369. ret = applesmc_read_key(MOTION_SENSOR_Z_KEY, buffer, 2);
  370. break;
  371. default:
  372. ret = -EINVAL;
  373. }
  374. *value = ((s16)buffer[0] << 8) | buffer[1];
  375. return ret;
  376. }
  377. /*
  378. * applesmc_device_init - initialize the accelerometer. Can sleep.
  379. */
  380. static void applesmc_device_init(void)
  381. {
  382. int total;
  383. u8 buffer[2];
  384. if (!smcreg.has_accelerometer)
  385. return;
  386. for (total = INIT_TIMEOUT_MSECS; total > 0; total -= INIT_WAIT_MSECS) {
  387. if (!applesmc_read_key(MOTION_SENSOR_KEY, buffer, 2) &&
  388. (buffer[0] != 0x00 || buffer[1] != 0x00))
  389. return;
  390. buffer[0] = 0xe0;
  391. buffer[1] = 0x00;
  392. applesmc_write_key(MOTION_SENSOR_KEY, buffer, 2);
  393. msleep(INIT_WAIT_MSECS);
  394. }
  395. pr_warn("failed to init the device\n");
  396. }
  397. /*
  398. * applesmc_init_smcreg_try - Try to initialize register cache. Idempotent.
  399. */
  400. static int applesmc_init_smcreg_try(void)
  401. {
  402. struct applesmc_registers *s = &smcreg;
  403. bool left_light_sensor, right_light_sensor;
  404. u8 tmp[1];
  405. int ret;
  406. if (s->init_complete)
  407. return 0;
  408. ret = read_register_count(&s->key_count);
  409. if (ret)
  410. return ret;
  411. if (!s->cache)
  412. s->cache = kcalloc(s->key_count, sizeof(*s->cache), GFP_KERNEL);
  413. if (!s->cache)
  414. return -ENOMEM;
  415. ret = applesmc_read_key(FANS_COUNT, tmp, 1);
  416. if (ret)
  417. return ret;
  418. s->fan_count = tmp[0];
  419. ret = applesmc_get_lower_bound(&s->temp_begin, "T");
  420. if (ret)
  421. return ret;
  422. ret = applesmc_get_lower_bound(&s->temp_end, "U");
  423. if (ret)
  424. return ret;
  425. s->temp_count = s->temp_end - s->temp_begin;
  426. ret = applesmc_has_key(LIGHT_SENSOR_LEFT_KEY, &left_light_sensor);
  427. if (ret)
  428. return ret;
  429. ret = applesmc_has_key(LIGHT_SENSOR_RIGHT_KEY, &right_light_sensor);
  430. if (ret)
  431. return ret;
  432. ret = applesmc_has_key(MOTION_SENSOR_KEY, &s->has_accelerometer);
  433. if (ret)
  434. return ret;
  435. ret = applesmc_has_key(BACKLIGHT_KEY, &s->has_key_backlight);
  436. if (ret)
  437. return ret;
  438. s->num_light_sensors = left_light_sensor + right_light_sensor;
  439. s->init_complete = true;
  440. pr_info("key=%d fan=%d temp=%d acc=%d lux=%d kbd=%d\n",
  441. s->key_count, s->fan_count, s->temp_count,
  442. s->has_accelerometer,
  443. s->num_light_sensors,
  444. s->has_key_backlight);
  445. return 0;
  446. }
  447. /*
  448. * applesmc_init_smcreg - Initialize register cache.
  449. *
  450. * Retries until initialization is successful, or the operation times out.
  451. *
  452. */
  453. static int applesmc_init_smcreg(void)
  454. {
  455. int ms, ret;
  456. for (ms = 0; ms < INIT_TIMEOUT_MSECS; ms += INIT_WAIT_MSECS) {
  457. ret = applesmc_init_smcreg_try();
  458. if (!ret) {
  459. if (ms)
  460. pr_info("init_smcreg() took %d ms\n", ms);
  461. return 0;
  462. }
  463. msleep(INIT_WAIT_MSECS);
  464. }
  465. kfree(smcreg.cache);
  466. smcreg.cache = NULL;
  467. return ret;
  468. }
  469. static void applesmc_destroy_smcreg(void)
  470. {
  471. kfree(smcreg.cache);
  472. smcreg.cache = NULL;
  473. smcreg.init_complete = false;
  474. }
  475. /* Device model stuff */
  476. static int applesmc_probe(struct platform_device *dev)
  477. {
  478. int ret;
  479. ret = applesmc_init_smcreg();
  480. if (ret)
  481. return ret;
  482. applesmc_device_init();
  483. return 0;
  484. }
  485. /* Synchronize device with memorized backlight state */
  486. static int applesmc_pm_resume(struct device *dev)
  487. {
  488. if (smcreg.has_key_backlight)
  489. applesmc_write_key(BACKLIGHT_KEY, backlight_state, 2);
  490. return 0;
  491. }
  492. /* Reinitialize device on resume from hibernation */
  493. static int applesmc_pm_restore(struct device *dev)
  494. {
  495. applesmc_device_init();
  496. return applesmc_pm_resume(dev);
  497. }
  498. static const struct dev_pm_ops applesmc_pm_ops = {
  499. .resume = applesmc_pm_resume,
  500. .restore = applesmc_pm_restore,
  501. };
  502. static struct platform_driver applesmc_driver = {
  503. .probe = applesmc_probe,
  504. .driver = {
  505. .name = "applesmc",
  506. .owner = THIS_MODULE,
  507. .pm = &applesmc_pm_ops,
  508. },
  509. };
  510. /*
  511. * applesmc_calibrate - Set our "resting" values. Callers must
  512. * hold applesmc_lock.
  513. */
  514. static void applesmc_calibrate(void)
  515. {
  516. applesmc_read_motion_sensor(SENSOR_X, &rest_x);
  517. applesmc_read_motion_sensor(SENSOR_Y, &rest_y);
  518. rest_x = -rest_x;
  519. }
  520. static void applesmc_idev_poll(struct input_polled_dev *dev)
  521. {
  522. struct input_dev *idev = dev->input;
  523. s16 x, y;
  524. if (applesmc_read_motion_sensor(SENSOR_X, &x))
  525. return;
  526. if (applesmc_read_motion_sensor(SENSOR_Y, &y))
  527. return;
  528. x = -x;
  529. input_report_abs(idev, ABS_X, x - rest_x);
  530. input_report_abs(idev, ABS_Y, y - rest_y);
  531. input_sync(idev);
  532. }
  533. /* Sysfs Files */
  534. static ssize_t applesmc_name_show(struct device *dev,
  535. struct device_attribute *attr, char *buf)
  536. {
  537. return snprintf(buf, PAGE_SIZE, "applesmc\n");
  538. }
  539. static ssize_t applesmc_position_show(struct device *dev,
  540. struct device_attribute *attr, char *buf)
  541. {
  542. int ret;
  543. s16 x, y, z;
  544. ret = applesmc_read_motion_sensor(SENSOR_X, &x);
  545. if (ret)
  546. goto out;
  547. ret = applesmc_read_motion_sensor(SENSOR_Y, &y);
  548. if (ret)
  549. goto out;
  550. ret = applesmc_read_motion_sensor(SENSOR_Z, &z);
  551. if (ret)
  552. goto out;
  553. out:
  554. if (ret)
  555. return ret;
  556. else
  557. return snprintf(buf, PAGE_SIZE, "(%d,%d,%d)\n", x, y, z);
  558. }
  559. static ssize_t applesmc_light_show(struct device *dev,
  560. struct device_attribute *attr, char *sysfsbuf)
  561. {
  562. const struct applesmc_entry *entry;
  563. static int data_length;
  564. int ret;
  565. u8 left = 0, right = 0;
  566. u8 buffer[10];
  567. if (!data_length) {
  568. entry = applesmc_get_entry_by_key(LIGHT_SENSOR_LEFT_KEY);
  569. if (IS_ERR(entry))
  570. return PTR_ERR(entry);
  571. if (entry->len > 10)
  572. return -ENXIO;
  573. data_length = entry->len;
  574. pr_info("light sensor data length set to %d\n", data_length);
  575. }
  576. ret = applesmc_read_key(LIGHT_SENSOR_LEFT_KEY, buffer, data_length);
  577. /* newer macbooks report a single 10-bit bigendian value */
  578. if (data_length == 10) {
  579. left = be16_to_cpu(*(__be16 *)(buffer + 6)) >> 2;
  580. goto out;
  581. }
  582. left = buffer[2];
  583. if (ret)
  584. goto out;
  585. ret = applesmc_read_key(LIGHT_SENSOR_RIGHT_KEY, buffer, data_length);
  586. right = buffer[2];
  587. out:
  588. if (ret)
  589. return ret;
  590. else
  591. return snprintf(sysfsbuf, PAGE_SIZE, "(%d,%d)\n", left, right);
  592. }
  593. /* Displays sensor key as label */
  594. static ssize_t applesmc_show_sensor_label(struct device *dev,
  595. struct device_attribute *devattr, char *sysfsbuf)
  596. {
  597. int index = smcreg.temp_begin + to_index(devattr);
  598. const struct applesmc_entry *entry;
  599. entry = applesmc_get_entry_by_index(index);
  600. if (IS_ERR(entry))
  601. return PTR_ERR(entry);
  602. return snprintf(sysfsbuf, PAGE_SIZE, "%s\n", entry->key);
  603. }
  604. /* Displays degree Celsius * 1000 */
  605. static ssize_t applesmc_show_temperature(struct device *dev,
  606. struct device_attribute *devattr, char *sysfsbuf)
  607. {
  608. int index = smcreg.temp_begin + to_index(devattr);
  609. const struct applesmc_entry *entry;
  610. int ret;
  611. u8 buffer[2];
  612. unsigned int temp;
  613. entry = applesmc_get_entry_by_index(index);
  614. if (IS_ERR(entry))
  615. return PTR_ERR(entry);
  616. if (entry->len > 2)
  617. return -EINVAL;
  618. ret = applesmc_read_entry(entry, buffer, entry->len);
  619. if (ret)
  620. return ret;
  621. if (entry->len == 2) {
  622. temp = buffer[0] * 1000;
  623. temp += (buffer[1] >> 6) * 250;
  624. } else {
  625. temp = buffer[0] * 4000;
  626. }
  627. return snprintf(sysfsbuf, PAGE_SIZE, "%u\n", temp);
  628. }
  629. static ssize_t applesmc_show_fan_speed(struct device *dev,
  630. struct device_attribute *attr, char *sysfsbuf)
  631. {
  632. int ret;
  633. unsigned int speed = 0;
  634. char newkey[5];
  635. u8 buffer[2];
  636. sprintf(newkey, fan_speed_fmt[to_option(attr)], to_index(attr));
  637. ret = applesmc_read_key(newkey, buffer, 2);
  638. speed = ((buffer[0] << 8 | buffer[1]) >> 2);
  639. if (ret)
  640. return ret;
  641. else
  642. return snprintf(sysfsbuf, PAGE_SIZE, "%u\n", speed);
  643. }
  644. static ssize_t applesmc_store_fan_speed(struct device *dev,
  645. struct device_attribute *attr,
  646. const char *sysfsbuf, size_t count)
  647. {
  648. int ret;
  649. unsigned long speed;
  650. char newkey[5];
  651. u8 buffer[2];
  652. if (strict_strtoul(sysfsbuf, 10, &speed) < 0 || speed >= 0x4000)
  653. return -EINVAL; /* Bigger than a 14-bit value */
  654. sprintf(newkey, fan_speed_fmt[to_option(attr)], to_index(attr));
  655. buffer[0] = (speed >> 6) & 0xff;
  656. buffer[1] = (speed << 2) & 0xff;
  657. ret = applesmc_write_key(newkey, buffer, 2);
  658. if (ret)
  659. return ret;
  660. else
  661. return count;
  662. }
  663. static ssize_t applesmc_show_fan_manual(struct device *dev,
  664. struct device_attribute *attr, char *sysfsbuf)
  665. {
  666. int ret;
  667. u16 manual = 0;
  668. u8 buffer[2];
  669. ret = applesmc_read_key(FANS_MANUAL, buffer, 2);
  670. manual = ((buffer[0] << 8 | buffer[1]) >> to_index(attr)) & 0x01;
  671. if (ret)
  672. return ret;
  673. else
  674. return snprintf(sysfsbuf, PAGE_SIZE, "%d\n", manual);
  675. }
  676. static ssize_t applesmc_store_fan_manual(struct device *dev,
  677. struct device_attribute *attr,
  678. const char *sysfsbuf, size_t count)
  679. {
  680. int ret;
  681. u8 buffer[2];
  682. unsigned long input;
  683. u16 val;
  684. if (strict_strtoul(sysfsbuf, 10, &input) < 0)
  685. return -EINVAL;
  686. ret = applesmc_read_key(FANS_MANUAL, buffer, 2);
  687. val = (buffer[0] << 8 | buffer[1]);
  688. if (ret)
  689. goto out;
  690. if (input)
  691. val = val | (0x01 << to_index(attr));
  692. else
  693. val = val & ~(0x01 << to_index(attr));
  694. buffer[0] = (val >> 8) & 0xFF;
  695. buffer[1] = val & 0xFF;
  696. ret = applesmc_write_key(FANS_MANUAL, buffer, 2);
  697. out:
  698. if (ret)
  699. return ret;
  700. else
  701. return count;
  702. }
  703. static ssize_t applesmc_show_fan_position(struct device *dev,
  704. struct device_attribute *attr, char *sysfsbuf)
  705. {
  706. int ret;
  707. char newkey[5];
  708. u8 buffer[17];
  709. sprintf(newkey, FAN_ID_FMT, to_index(attr));
  710. ret = applesmc_read_key(newkey, buffer, 16);
  711. buffer[16] = 0;
  712. if (ret)
  713. return ret;
  714. else
  715. return snprintf(sysfsbuf, PAGE_SIZE, "%s\n", buffer+4);
  716. }
  717. static ssize_t applesmc_calibrate_show(struct device *dev,
  718. struct device_attribute *attr, char *sysfsbuf)
  719. {
  720. return snprintf(sysfsbuf, PAGE_SIZE, "(%d,%d)\n", rest_x, rest_y);
  721. }
  722. static ssize_t applesmc_calibrate_store(struct device *dev,
  723. struct device_attribute *attr, const char *sysfsbuf, size_t count)
  724. {
  725. applesmc_calibrate();
  726. return count;
  727. }
  728. static void applesmc_backlight_set(struct work_struct *work)
  729. {
  730. applesmc_write_key(BACKLIGHT_KEY, backlight_state, 2);
  731. }
  732. static DECLARE_WORK(backlight_work, &applesmc_backlight_set);
  733. static void applesmc_brightness_set(struct led_classdev *led_cdev,
  734. enum led_brightness value)
  735. {
  736. int ret;
  737. backlight_state[0] = value;
  738. ret = queue_work(applesmc_led_wq, &backlight_work);
  739. if (debug && (!ret))
  740. printk(KERN_DEBUG "applesmc: work was already on the queue.\n");
  741. }
  742. static ssize_t applesmc_key_count_show(struct device *dev,
  743. struct device_attribute *attr, char *sysfsbuf)
  744. {
  745. int ret;
  746. u8 buffer[4];
  747. u32 count;
  748. ret = applesmc_read_key(KEY_COUNT_KEY, buffer, 4);
  749. count = ((u32)buffer[0]<<24) + ((u32)buffer[1]<<16) +
  750. ((u32)buffer[2]<<8) + buffer[3];
  751. if (ret)
  752. return ret;
  753. else
  754. return snprintf(sysfsbuf, PAGE_SIZE, "%d\n", count);
  755. }
  756. static ssize_t applesmc_key_at_index_read_show(struct device *dev,
  757. struct device_attribute *attr, char *sysfsbuf)
  758. {
  759. const struct applesmc_entry *entry;
  760. int ret;
  761. entry = applesmc_get_entry_by_index(key_at_index);
  762. if (IS_ERR(entry))
  763. return PTR_ERR(entry);
  764. ret = applesmc_read_entry(entry, sysfsbuf, entry->len);
  765. if (ret)
  766. return ret;
  767. return entry->len;
  768. }
  769. static ssize_t applesmc_key_at_index_data_length_show(struct device *dev,
  770. struct device_attribute *attr, char *sysfsbuf)
  771. {
  772. const struct applesmc_entry *entry;
  773. entry = applesmc_get_entry_by_index(key_at_index);
  774. if (IS_ERR(entry))
  775. return PTR_ERR(entry);
  776. return snprintf(sysfsbuf, PAGE_SIZE, "%d\n", entry->len);
  777. }
  778. static ssize_t applesmc_key_at_index_type_show(struct device *dev,
  779. struct device_attribute *attr, char *sysfsbuf)
  780. {
  781. const struct applesmc_entry *entry;
  782. entry = applesmc_get_entry_by_index(key_at_index);
  783. if (IS_ERR(entry))
  784. return PTR_ERR(entry);
  785. return snprintf(sysfsbuf, PAGE_SIZE, "%s\n", entry->type);
  786. }
  787. static ssize_t applesmc_key_at_index_name_show(struct device *dev,
  788. struct device_attribute *attr, char *sysfsbuf)
  789. {
  790. const struct applesmc_entry *entry;
  791. entry = applesmc_get_entry_by_index(key_at_index);
  792. if (IS_ERR(entry))
  793. return PTR_ERR(entry);
  794. return snprintf(sysfsbuf, PAGE_SIZE, "%s\n", entry->key);
  795. }
  796. static ssize_t applesmc_key_at_index_show(struct device *dev,
  797. struct device_attribute *attr, char *sysfsbuf)
  798. {
  799. return snprintf(sysfsbuf, PAGE_SIZE, "%d\n", key_at_index);
  800. }
  801. static ssize_t applesmc_key_at_index_store(struct device *dev,
  802. struct device_attribute *attr, const char *sysfsbuf, size_t count)
  803. {
  804. unsigned long newkey;
  805. if (strict_strtoul(sysfsbuf, 10, &newkey) < 0
  806. || newkey >= smcreg.key_count)
  807. return -EINVAL;
  808. key_at_index = newkey;
  809. return count;
  810. }
  811. static struct led_classdev applesmc_backlight = {
  812. .name = "smc::kbd_backlight",
  813. .default_trigger = "nand-disk",
  814. .brightness_set = applesmc_brightness_set,
  815. };
  816. static struct applesmc_node_group info_group[] = {
  817. { "name", applesmc_name_show },
  818. { "key_count", applesmc_key_count_show },
  819. { "key_at_index", applesmc_key_at_index_show, applesmc_key_at_index_store },
  820. { "key_at_index_name", applesmc_key_at_index_name_show },
  821. { "key_at_index_type", applesmc_key_at_index_type_show },
  822. { "key_at_index_data_length", applesmc_key_at_index_data_length_show },
  823. { "key_at_index_data", applesmc_key_at_index_read_show },
  824. { }
  825. };
  826. static struct applesmc_node_group accelerometer_group[] = {
  827. { "position", applesmc_position_show },
  828. { "calibrate", applesmc_calibrate_show, applesmc_calibrate_store },
  829. { }
  830. };
  831. static struct applesmc_node_group light_sensor_group[] = {
  832. { "light", applesmc_light_show },
  833. { }
  834. };
  835. static struct applesmc_node_group fan_group[] = {
  836. { "fan%d_label", applesmc_show_fan_position },
  837. { "fan%d_input", applesmc_show_fan_speed, NULL, 0 },
  838. { "fan%d_min", applesmc_show_fan_speed, applesmc_store_fan_speed, 1 },
  839. { "fan%d_max", applesmc_show_fan_speed, NULL, 2 },
  840. { "fan%d_safe", applesmc_show_fan_speed, NULL, 3 },
  841. { "fan%d_output", applesmc_show_fan_speed, applesmc_store_fan_speed, 4 },
  842. { "fan%d_manual", applesmc_show_fan_manual, applesmc_store_fan_manual },
  843. { }
  844. };
  845. static struct applesmc_node_group temp_group[] = {
  846. { "temp%d_label", applesmc_show_sensor_label },
  847. { "temp%d_input", applesmc_show_temperature },
  848. { }
  849. };
  850. /* Module stuff */
  851. /*
  852. * applesmc_destroy_nodes - remove files and free associated memory
  853. */
  854. static void applesmc_destroy_nodes(struct applesmc_node_group *groups)
  855. {
  856. struct applesmc_node_group *grp;
  857. struct applesmc_dev_attr *node;
  858. for (grp = groups; grp->nodes; grp++) {
  859. for (node = grp->nodes; node->sda.dev_attr.attr.name; node++)
  860. sysfs_remove_file(&pdev->dev.kobj,
  861. &node->sda.dev_attr.attr);
  862. kfree(grp->nodes);
  863. grp->nodes = NULL;
  864. }
  865. }
  866. /*
  867. * applesmc_create_nodes - create a two-dimensional group of sysfs files
  868. */
  869. static int applesmc_create_nodes(struct applesmc_node_group *groups, int num)
  870. {
  871. struct applesmc_node_group *grp;
  872. struct applesmc_dev_attr *node;
  873. struct attribute *attr;
  874. int ret, i;
  875. for (grp = groups; grp->format; grp++) {
  876. grp->nodes = kcalloc(num + 1, sizeof(*node), GFP_KERNEL);
  877. if (!grp->nodes) {
  878. ret = -ENOMEM;
  879. goto out;
  880. }
  881. for (i = 0; i < num; i++) {
  882. node = &grp->nodes[i];
  883. sprintf(node->name, grp->format, i + 1);
  884. node->sda.index = (grp->option << 16) | (i & 0xffff);
  885. node->sda.dev_attr.show = grp->show;
  886. node->sda.dev_attr.store = grp->store;
  887. attr = &node->sda.dev_attr.attr;
  888. sysfs_attr_init(attr);
  889. attr->name = node->name;
  890. attr->mode = S_IRUGO | (grp->store ? S_IWUSR : 0);
  891. ret = sysfs_create_file(&pdev->dev.kobj, attr);
  892. if (ret) {
  893. attr->name = NULL;
  894. goto out;
  895. }
  896. }
  897. }
  898. return 0;
  899. out:
  900. applesmc_destroy_nodes(groups);
  901. return ret;
  902. }
  903. /* Create accelerometer ressources */
  904. static int applesmc_create_accelerometer(void)
  905. {
  906. struct input_dev *idev;
  907. int ret;
  908. if (!smcreg.has_accelerometer)
  909. return 0;
  910. ret = applesmc_create_nodes(accelerometer_group, 1);
  911. if (ret)
  912. goto out;
  913. applesmc_idev = input_allocate_polled_device();
  914. if (!applesmc_idev) {
  915. ret = -ENOMEM;
  916. goto out_sysfs;
  917. }
  918. applesmc_idev->poll = applesmc_idev_poll;
  919. applesmc_idev->poll_interval = APPLESMC_POLL_INTERVAL;
  920. /* initial calibrate for the input device */
  921. applesmc_calibrate();
  922. /* initialize the input device */
  923. idev = applesmc_idev->input;
  924. idev->name = "applesmc";
  925. idev->id.bustype = BUS_HOST;
  926. idev->dev.parent = &pdev->dev;
  927. idev->evbit[0] = BIT_MASK(EV_ABS);
  928. input_set_abs_params(idev, ABS_X,
  929. -256, 256, APPLESMC_INPUT_FUZZ, APPLESMC_INPUT_FLAT);
  930. input_set_abs_params(idev, ABS_Y,
  931. -256, 256, APPLESMC_INPUT_FUZZ, APPLESMC_INPUT_FLAT);
  932. ret = input_register_polled_device(applesmc_idev);
  933. if (ret)
  934. goto out_idev;
  935. return 0;
  936. out_idev:
  937. input_free_polled_device(applesmc_idev);
  938. out_sysfs:
  939. applesmc_destroy_nodes(accelerometer_group);
  940. out:
  941. pr_warn("driver init failed (ret=%d)!\n", ret);
  942. return ret;
  943. }
  944. /* Release all ressources used by the accelerometer */
  945. static void applesmc_release_accelerometer(void)
  946. {
  947. if (!smcreg.has_accelerometer)
  948. return;
  949. input_unregister_polled_device(applesmc_idev);
  950. input_free_polled_device(applesmc_idev);
  951. applesmc_destroy_nodes(accelerometer_group);
  952. }
  953. static int applesmc_create_light_sensor(void)
  954. {
  955. if (!smcreg.num_light_sensors)
  956. return 0;
  957. return applesmc_create_nodes(light_sensor_group, 1);
  958. }
  959. static void applesmc_release_light_sensor(void)
  960. {
  961. if (!smcreg.num_light_sensors)
  962. return;
  963. applesmc_destroy_nodes(light_sensor_group);
  964. }
  965. static int applesmc_create_key_backlight(void)
  966. {
  967. if (!smcreg.has_key_backlight)
  968. return 0;
  969. applesmc_led_wq = create_singlethread_workqueue("applesmc-led");
  970. if (!applesmc_led_wq)
  971. return -ENOMEM;
  972. return led_classdev_register(&pdev->dev, &applesmc_backlight);
  973. }
  974. static void applesmc_release_key_backlight(void)
  975. {
  976. if (!smcreg.has_key_backlight)
  977. return;
  978. led_classdev_unregister(&applesmc_backlight);
  979. destroy_workqueue(applesmc_led_wq);
  980. }
  981. static int applesmc_dmi_match(const struct dmi_system_id *id)
  982. {
  983. return 1;
  984. }
  985. /* Note that DMI_MATCH(...,"MacBook") will match "MacBookPro1,1".
  986. * So we need to put "Apple MacBook Pro" before "Apple MacBook". */
  987. static __initdata struct dmi_system_id applesmc_whitelist[] = {
  988. { applesmc_dmi_match, "Apple MacBook Air", {
  989. DMI_MATCH(DMI_BOARD_VENDOR, "Apple"),
  990. DMI_MATCH(DMI_PRODUCT_NAME, "MacBookAir") },
  991. },
  992. { applesmc_dmi_match, "Apple MacBook Pro", {
  993. DMI_MATCH(DMI_BOARD_VENDOR, "Apple"),
  994. DMI_MATCH(DMI_PRODUCT_NAME, "MacBookPro") },
  995. },
  996. { applesmc_dmi_match, "Apple MacBook", {
  997. DMI_MATCH(DMI_BOARD_VENDOR, "Apple"),
  998. DMI_MATCH(DMI_PRODUCT_NAME, "MacBook") },
  999. },
  1000. { applesmc_dmi_match, "Apple Macmini", {
  1001. DMI_MATCH(DMI_BOARD_VENDOR, "Apple"),
  1002. DMI_MATCH(DMI_PRODUCT_NAME, "Macmini") },
  1003. },
  1004. { applesmc_dmi_match, "Apple MacPro", {
  1005. DMI_MATCH(DMI_BOARD_VENDOR, "Apple"),
  1006. DMI_MATCH(DMI_PRODUCT_NAME, "MacPro") },
  1007. },
  1008. { applesmc_dmi_match, "Apple iMac", {
  1009. DMI_MATCH(DMI_BOARD_VENDOR, "Apple"),
  1010. DMI_MATCH(DMI_PRODUCT_NAME, "iMac") },
  1011. },
  1012. { .ident = NULL }
  1013. };
  1014. static int __init applesmc_init(void)
  1015. {
  1016. int ret;
  1017. if (!dmi_check_system(applesmc_whitelist)) {
  1018. pr_warn("supported laptop not found!\n");
  1019. ret = -ENODEV;
  1020. goto out;
  1021. }
  1022. if (!request_region(APPLESMC_DATA_PORT, APPLESMC_NR_PORTS,
  1023. "applesmc")) {
  1024. ret = -ENXIO;
  1025. goto out;
  1026. }
  1027. ret = platform_driver_register(&applesmc_driver);
  1028. if (ret)
  1029. goto out_region;
  1030. pdev = platform_device_register_simple("applesmc", APPLESMC_DATA_PORT,
  1031. NULL, 0);
  1032. if (IS_ERR(pdev)) {
  1033. ret = PTR_ERR(pdev);
  1034. goto out_driver;
  1035. }
  1036. /* create register cache */
  1037. ret = applesmc_init_smcreg();
  1038. if (ret)
  1039. goto out_device;
  1040. ret = applesmc_create_nodes(info_group, 1);
  1041. if (ret)
  1042. goto out_smcreg;
  1043. ret = applesmc_create_nodes(fan_group, smcreg.fan_count);
  1044. if (ret)
  1045. goto out_info;
  1046. ret = applesmc_create_nodes(temp_group, smcreg.temp_count);
  1047. if (ret)
  1048. goto out_fans;
  1049. ret = applesmc_create_accelerometer();
  1050. if (ret)
  1051. goto out_temperature;
  1052. ret = applesmc_create_light_sensor();
  1053. if (ret)
  1054. goto out_accelerometer;
  1055. ret = applesmc_create_key_backlight();
  1056. if (ret)
  1057. goto out_light_sysfs;
  1058. hwmon_dev = hwmon_device_register(&pdev->dev);
  1059. if (IS_ERR(hwmon_dev)) {
  1060. ret = PTR_ERR(hwmon_dev);
  1061. goto out_light_ledclass;
  1062. }
  1063. return 0;
  1064. out_light_ledclass:
  1065. applesmc_release_key_backlight();
  1066. out_light_sysfs:
  1067. applesmc_release_light_sensor();
  1068. out_accelerometer:
  1069. applesmc_release_accelerometer();
  1070. out_temperature:
  1071. applesmc_destroy_nodes(temp_group);
  1072. out_fans:
  1073. applesmc_destroy_nodes(fan_group);
  1074. out_info:
  1075. applesmc_destroy_nodes(info_group);
  1076. out_smcreg:
  1077. applesmc_destroy_smcreg();
  1078. out_device:
  1079. platform_device_unregister(pdev);
  1080. out_driver:
  1081. platform_driver_unregister(&applesmc_driver);
  1082. out_region:
  1083. release_region(APPLESMC_DATA_PORT, APPLESMC_NR_PORTS);
  1084. out:
  1085. pr_warn("driver init failed (ret=%d)!\n", ret);
  1086. return ret;
  1087. }
  1088. static void __exit applesmc_exit(void)
  1089. {
  1090. hwmon_device_unregister(hwmon_dev);
  1091. applesmc_release_key_backlight();
  1092. applesmc_release_light_sensor();
  1093. applesmc_release_accelerometer();
  1094. applesmc_destroy_nodes(temp_group);
  1095. applesmc_destroy_nodes(fan_group);
  1096. applesmc_destroy_nodes(info_group);
  1097. applesmc_destroy_smcreg();
  1098. platform_device_unregister(pdev);
  1099. platform_driver_unregister(&applesmc_driver);
  1100. release_region(APPLESMC_DATA_PORT, APPLESMC_NR_PORTS);
  1101. }
  1102. module_init(applesmc_init);
  1103. module_exit(applesmc_exit);
  1104. MODULE_AUTHOR("Nicolas Boichat");
  1105. MODULE_DESCRIPTION("Apple SMC");
  1106. MODULE_LICENSE("GPL v2");
  1107. MODULE_DEVICE_TABLE(dmi, applesmc_whitelist);