applesmc.c 32 KB

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