applesmc.c 32 KB

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