applesmc.c 32 KB

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