applesmc.c 33 KB

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